Metal: Make GLSL shader source MSL compliant also

Metal shading language follows the C++ 14 standard and in some cases requires a greater level of explicitness than GLSL. There are also some small language differences:

- Explicit type-casts (C++ requirements)
- Explicit constant values (C++ requirements, e.g. floating point values using 0.0 instead of 0).
- Metal/OpenGL compatibility paths
- GLSL Function prototypes
- Explicit accessors for vector types when sampling textures.

Authored by Apple: Michael Parkin-White

Ref T96261

Reviewed By: fclem

Maniphest Tasks: T96261

Differential Revision: https://developer.blender.org/D14378
This commit is contained in:
Jason Fielder 2022-03-22 12:44:26 +01:00 committed by Clément Foucault
parent 309ea31485
commit 19c793af35
Notes: blender-bot 2023-02-13 22:38:46 +01:00
Referenced by issue #96261, Metal Viewport
37 changed files with 120 additions and 50 deletions

View File

@ -26,10 +26,13 @@ struct ClosureEvalGlossy {
#ifdef STEP_RESOLVE /* SSR */
/* Prototype. */
# ifndef GPU_METAL
/* MSL does not require prototypes. */
void raytrace_resolve(ClosureInputGlossy cl_in,
inout ClosureEvalGlossy cl_eval,
inout ClosureEvalCommon cl_common,
inout ClosureOutputGlossy cl_out);
# endif
#endif
ClosureEvalGlossy closure_Glossy_eval_init(inout ClosureInputGlossy cl_in,

View File

@ -32,8 +32,10 @@ struct Closure {
#endif
};
#ifndef GPU_METAL
/* Prototype */
Closure nodetree_exec(void);
#endif
/* clang-format off */
/* Avoid multi-line defines. */

View File

@ -150,7 +150,7 @@ vec4 step_blit(void)
float br = max_v3(m);
/* Under-threshold part: quadratic curve */
float rq = clamp(br - curveThreshold.x, 0, curveThreshold.y);
float rq = clamp(br - curveThreshold.x, 0.0, curveThreshold.y);
rq = curveThreshold.z * rq * rq;
/* Combine and apply the brightness response curve. */

View File

@ -142,7 +142,7 @@ void dof_resolve_load_layer(sampler2D color_tex,
out float out_weight)
{
vec2 pixel_co = gl_FragCoord.xy / 2.0;
vec2 uv = pixel_co / textureSize(color_tex, 0).xy;
vec2 uv = pixel_co / vec2(textureSize(color_tex, 0).xy);
out_color = textureLod(color_tex, uv, 0.0);
out_weight = textureLod(weight_tex, uv, 0.0).r;
}

View File

@ -95,7 +95,7 @@ void main()
weights = dof_layer_weight(cocs) * dof_sample_weight(cocs);
/* Filter NaNs. */
weights = mix(weights, vec4(0.0), equal(cocs, vec4(0.0)));
weights = select(weights, vec4(0.0), equal(cocs, vec4(0.0)));
color1 = colors[0] * weights[0];
color2 = colors[1] * weights[1];

View File

@ -54,12 +54,12 @@ void main()
bvec4 focus = lessThanEqual(cocs, vec4(0.5));
if (any(defocus) && any(focus)) {
/* For the same reason as in the flatten pass. This is a case we cannot optimize for. */
cocs = mix(cocs, vec4(DOF_TILE_MIXED), focus);
cocs = mix(cocs, vec4(DOF_TILE_MIXED), defocus);
cocs = select(cocs, vec4(DOF_TILE_MIXED), focus);
cocs = select(cocs, vec4(DOF_TILE_MIXED), defocus);
}
else {
cocs = mix(cocs, vec4(DOF_TILE_FOCUS), focus);
cocs = mix(cocs, vec4(DOF_TILE_DEFOCUS), defocus);
cocs = select(cocs, vec4(DOF_TILE_FOCUS), focus);
cocs = select(cocs, vec4(DOF_TILE_DEFOCUS), defocus);
}
outCoc.y = max_v4(cocs);
}

View File

@ -38,7 +38,7 @@ uniform vec2 texelSize;
/* On some AMD card / driver combination, it is needed otherwise,
* the shader does not write anything. */
#if defined(GPU_INTEL) || defined(GPU_ATI)
#if (defined(GPU_INTEL) || defined(GPU_ATI)) && defined(GPU_OPENGL)
out vec4 fragColor;
#endif
@ -68,9 +68,14 @@ void main()
float val = minmax4(samp.x, samp.y, samp.z, samp.w);
#endif
#if defined(GPU_INTEL) || defined(GPU_ATI)
#if (defined(GPU_INTEL) || defined(GPU_ATI)) && defined(GPU_OPENGL)
/* Use color format instead of 24bit depth texture */
fragColor = vec4(val);
#endif
#if !(defined(GPU_INTEL) && defined(GPU_OPENGL))
/* If using Intel workaround, do not write out depth as there will be no depth target and this is
* invalid. */
gl_FragDepth = val;
#endif
}

View File

@ -60,11 +60,11 @@ void main()
vec3 P = transform_point(ViewMatrixInverse, vP);
vec3 vV = viewCameraVec(vP);
vec3 V = cameraVec(P);
vec3 vN = normal_decode(texture(normalBuffer, uvs, 0).rg, vV);
vec3 vN = normal_decode(textureLod(normalBuffer, uvs, 0.0).rg, vV);
vec3 N = transform_direction(ViewMatrixInverse, vN);
/* Retrieve pixel data */
vec4 speccol_roughness = texture(specroughBuffer, uvs, 0).rgba;
vec4 speccol_roughness = textureLod(specroughBuffer, uvs, 0.0).rgba;
/* Early out */
if (dot(speccol_roughness.rgb, vec3(1.0)) == 0.0) {

View File

@ -74,7 +74,7 @@ bool neighbor_affect_this_tile(ivec2 offset, vec2 velocity)
* offset. If the offset coordinate is zero then
* velocity is irrelevant.
*/
vec2 point = sign(offset * velocity);
vec2 point = sign(vec2(offset) * velocity);
float dist = (point.x + point.y);
/**

View File

@ -289,7 +289,7 @@ vec3 probe_evaluate_grid(GridData gd, vec3 P, vec3 N, vec3 localpos)
weight += prbIrradianceSmooth;
/* Trilinear weights */
vec3 trilinear = mix(1.0 - trilinear_weight, trilinear_weight, offset);
vec3 trilinear = mix(1.0 - trilinear_weight, trilinear_weight, vec3(offset));
weight *= trilinear.x * trilinear.y * trilinear.z;
/* Avoid zero weight */

View File

@ -16,11 +16,11 @@ void main()
{
#if 0
/* Reconstructing Target uvs like this avoid missing pixels if NPO2 */
vec2 uvs = gl_FragCoord.xy * 2.0 / vec2(textureSize(source, 0));
vec2 uvs = gl_FragCoord.xy * 2.0 / vec2(textureSize(source, 0).xy);
FragColor = textureLod(source, vec3(uvs, layer), 0.0);
#else
vec2 texel_size = 1.0 / vec2(textureSize(source, 0));
vec2 texel_size = 1.0 / vec2(textureSize(source, 0).xy);
vec2 uvs = gl_FragCoord.xy * 2.0 * texel_size;
vec4 ofs = texel_size.xyxy * vec4(0.75, 0.75, -0.75, -0.75);

View File

@ -31,7 +31,7 @@ layout(location = 3) out vec4 volumePhase;
void main()
{
ivec3 volume_cell = ivec3(gl_FragCoord.xy, slice);
ivec3 volume_cell = ivec3(ivec2(gl_FragCoord.xy), slice);
vec3 ndc_cell = volume_to_ndc((vec3(volume_cell) + volJitter.xyz) * volInvTexSize.xyz);
viewPosition = get_view_space_from_depth(ndc_cell.xy, ndc_cell.z);

View File

@ -52,7 +52,7 @@ void main()
ivec2 texco = ivec2(gl_FragCoord.xy);
#endif
for (int i = 0; i <= slice; i++) {
ivec3 volume_cell = ivec3(gl_FragCoord.xy, i);
ivec3 volume_cell = ivec3(ivec2(gl_FragCoord.xy), i);
vec3 Lscat = texelFetch(volumeScattering, volume_cell, 0).rgb;
vec3 s_extinction = texelFetch(volumeExtinction, volume_cell, 0).rgb;

View File

@ -22,7 +22,7 @@ layout(location = 1) out vec4 outTransmittance;
void main()
{
ivec3 volume_cell = ivec3(gl_FragCoord.xy, slice);
ivec3 volume_cell = ivec3(ivec2(gl_FragCoord.xy), slice);
/* Emission */
outScattering = texelFetch(volumeEmission, volume_cell, 0);

View File

@ -38,8 +38,8 @@ void main()
* This removes the alpha channel and put the background behind reference images
* while masking the reference images by the render alpha.
*/
float alpha = texture(colorBuffer, uvcoordsvar.st).a;
float depth = texture(depthBuffer, uvcoordsvar.st).r;
float alpha = texture(colorBuffer, uvcoordsvar.xy).a;
float depth = texture(depthBuffer, uvcoordsvar.xy).r;
vec3 bg_col;
vec3 col_high;

View File

@ -169,7 +169,7 @@ void diag_dir(bvec4 edges1, bvec4 edges2, out vec2 line_start, out vec2 line_end
void main()
{
uint ref = textureLod(outlineId, uvcoordsvar.st, 0.0).r;
uint ref = textureLod(outlineId, uvcoordsvar.xy, 0.0).r;
uint ref_col = ref;
vec2 uvs = gl_FragCoord.xy * sizeViewportInv.xy;

View File

@ -53,7 +53,7 @@ vec4 flag_to_color(uint flag)
if (bool(flag & uint(16))) {
color.rgb += vec3(0.9, 0.3, 0.0); /* orange */
}
if (color.rgb == vec3(0.0)) {
if (is_zero(color.rgb)) {
color.rgb += vec3(0.5, 0.0, 0.0); /* medium red */
}
return color;

View File

@ -9,11 +9,11 @@ void main()
float cavity = 0.0, edges = 0.0, curvature = 0.0;
#ifdef USE_CAVITY
cavity_compute(uvcoordsvar.st, depthBuffer, normalBuffer, cavity, edges);
cavity_compute(uvcoordsvar.xy, depthBuffer, normalBuffer, cavity, edges);
#endif
#ifdef USE_CURVATURE
curvature_compute(uvcoordsvar.st, objectIdBuffer, normalBuffer, curvature);
curvature_compute(uvcoordsvar.xy, objectIdBuffer, normalBuffer, curvature);
#endif
float final_cavity_factor = clamp((1.0 - cavity) * (1.0 + edges) * (1.0 + curvature), 0.0, 4.0);

View File

@ -1,7 +1,7 @@
void main()
{
float depth = texture(depthBuffer, uvcoordsvar.st).r;
float depth = texture(depthBuffer, uvcoordsvar.xy).r;
/* Fix issues with Intel drivers (see T80023). */
fragColor = vec4(0.0);
/* Discard background pixels. */

View File

@ -9,8 +9,8 @@ void main()
/* Revealage is actually stored in transparentAccum alpha channel.
* This is a workaround to older hardware not having separate blend equation per render target.
*/
vec4 trans_accum = texture(transparentAccum, uvcoordsvar.st);
float trans_weight = texture(transparentRevealage, uvcoordsvar.st).r;
vec4 trans_accum = texture(transparentAccum, uvcoordsvar.xy);
float trans_weight = texture(transparentRevealage, uvcoordsvar.xy).r;
float trans_reveal = trans_accum.a;
/* Listing 4 */

View File

@ -94,7 +94,7 @@ vec4 flag_to_color(uint flag)
if (bool(flag & uint(16))) {
color.rgb += vec3(0.9, 0.3, 0.0); /* orange */
}
if (color.rgb == vec3(0.0)) {
if (is_zero(color.rgb)) {
color.rgb += vec3(0.5, 0.0, 0.0); /* medium red */
}
return color;

View File

@ -158,7 +158,7 @@ float hair_shaperadius(float shape, float root, float tip, float time)
return (radius * (root - tip)) + tip;
}
# ifdef OS_MAC
# if defined(OS_MAC) && defined(GPU_OPENGL)
in float dummy;
# endif
@ -178,7 +178,7 @@ void hair_get_pos_tan_binor_time(bool is_persp,
wpos = data.point_position;
time = data.point_time;
# ifdef OS_MAC
# if defined(OS_MAC) && defined(GPU_OPENGL)
/* Generate a dummy read to avoid the driver bug with shaders having no
* vertex reads on macOS (T60171) */
wpos.y += dummy * 0.0;

View File

@ -86,7 +86,7 @@ float line_unit_box_intersect_dist(vec3 lineorigin, vec3 linedirection)
float line_unit_box_intersect_dist_safe(vec3 lineorigin, vec3 linedirection)
{
vec3 safe_linedirection = max(vec3(1e-8), abs(linedirection)) *
mix(vec3(1.0), -vec3(1.0), lessThan(linedirection, vec3(0.0)));
select(vec3(1.0), -vec3(1.0), lessThan(linedirection, vec3(0.0)));
return line_unit_box_intersect_dist(lineorigin, safe_linedirection);
}

View File

@ -84,9 +84,9 @@ float avg(vec3 v) { return dot(vec3(1.0 / 3.0), v); }
float avg(vec4 v) { return dot(vec4(1.0 / 4.0), v); }
float safe_rcp(float a) { return (a != 0.0) ? (1.0 / a) : 0.0; }
vec2 safe_rcp(vec2 a) { return mix(vec2(0.0), (1.0 / a), notEqual(a, vec2(0.0))); }
vec3 safe_rcp(vec3 a) { return mix(vec3(0.0), (1.0 / a), notEqual(a, vec3(0.0))); }
vec4 safe_rcp(vec4 a) { return mix(vec4(0.0), (1.0 / a), notEqual(a, vec4(0.0))); }
vec2 safe_rcp(vec2 a) { return select(vec2(0.0), (1.0 / a), notEqual(a, vec2(0.0))); }
vec3 safe_rcp(vec3 a) { return select(vec3(0.0), (1.0 / a), notEqual(a, vec3(0.0))); }
vec4 safe_rcp(vec4 a) { return select(vec4(0.0), (1.0 / a), notEqual(a, vec4(0.0))); }
float safe_sqrt(float a) { return sqrt(max(a, 0.0)); }

View File

@ -174,7 +174,7 @@ flat in int resourceIDFrag;
/* Breaking this across multiple lines causes issues for some older GLSL compilers. */
/* clang-format off */
#if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && !defined(INSTANCED_ATTR) && !defined(DRW_LEGACY_MODEL_MATRIX)
#if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && (!defined(OS_MAC) || defined(GPU_METAL)) && !defined(INSTANCED_ATTR) && !defined(DRW_LEGACY_MODEL_MATRIX)
/* clang-format on */
/* Temporary until we fully make the switch. */
@ -251,7 +251,7 @@ uniform mat4 ModelMatrixInverse;
/* Due to some shader compiler bug, we somewhat need to access gl_VertexID
* to make vertex shaders work. even if it's actually dead code. */
#ifdef GPU_INTEL
#if defined(GPU_INTEL) && defined(GPU_OPENGL)
# define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID);
#else
# define GPU_INTEL_VERTEX_SHADER_WORKAROUND

View File

@ -210,6 +210,7 @@ endif()
set(GLSL_SRC
GPU_shader_shared.h
shaders/opengl/glsl_shader_defines.glsl
shaders/gpu_shader_depth_only_frag.glsl
shaders/gpu_shader_uniform_color_frag.glsl

View File

@ -88,6 +88,16 @@ static void standard_defines(Vector<const char *> &sources)
else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
sources.append("#define OS_UNIX\n");
}
/* API Definition */
eGPUBackendType backend = GPU_backend_get_type();
switch (backend) {
case GPU_BACKEND_OPENGL:
sources.append("#define GPU_OPENGL\n");
break;
default:
BLI_assert(false && "Invalid GPU Backend Type");
break;
}
if (GPU_crappy_amd_driver()) {
sources.append("#define GPU_DEPRECATED_AMD_DRIVER\n");

View File

@ -24,6 +24,8 @@ using namespace blender;
using namespace blender::gpu;
using namespace blender::gpu::shader;
extern char datatoc_glsl_shader_defines_glsl[];
/* -------------------------------------------------------------------- */
/** \name Creation / Destruction
* \{ */
@ -760,7 +762,7 @@ bool GLShader::do_geometry_shader_injection(const shader::ShaderCreateInfo *info
static char *glsl_patch_default_get()
{
/** Used for shader patching. Init once. */
static char patch[1024] = "\0";
static char patch[2048] = "\0";
if (patch[0] != '\0') {
return patch;
}
@ -827,6 +829,9 @@ static char *glsl_patch_default_get()
STR_CONCATF(patch, slen, "#define DFDX_SIGN %1.1f\n", GLContext::derivative_signs[0]);
STR_CONCATF(patch, slen, "#define DFDY_SIGN %1.1f\n", GLContext::derivative_signs[1]);
/* GLSL Backend Lib. */
STR_CONCAT(patch, slen, datatoc_glsl_shader_defines_glsl);
BLI_assert(slen < sizeof(patch));
return patch;
}

View File

@ -23,15 +23,15 @@ void main()
/* Use pos to select the right swizzle (instead of gl_VertexID)
* in order to workaround an OSX driver bug. */
if (pos == vec2(0.0, 0.0)) {
if (all(equal(pos, vec2(0.0, 0.0)))) {
rect.xy = rect.xz;
tex.xy = tex.xz;
}
else if (pos == vec2(0.0, 1.0)) {
else if (all(equal(pos, vec2(0.0, 1.0)))) {
rect.xy = rect.xw;
tex.xy = tex.xw;
}
else if (pos == vec2(1.0, 1.0)) {
else if (all(equal(pos, vec2(1.0, 1.0)))) {
rect.xy = rect.yw;
tex.xy = tex.yw;
}

View File

@ -15,6 +15,32 @@ void main()
{
vec2 uv;
vec2 co;
#ifdef GPU_METAL
/* Metal API does not support Triangle fan primitive topology.
* When this shader is called using Triangle-Strip, vertex ID's
* are in a different order. */
# define GPU_PRIM_TRI_STRIP
#endif
#ifdef GPU_PRIM_TRI_STRIP
if (gl_VertexID == 0) {
co = rect_geom.xw;
uv = rect_icon.xw;
}
else if (gl_VertexID == 1) {
co = rect_geom.xy;
uv = rect_icon.xy;
}
else if (gl_VertexID == 2) {
co = rect_geom.zw;
uv = rect_icon.zw;
}
else {
co = rect_geom.zy;
uv = rect_icon.zy;
}
#else
if (gl_VertexID == 0) {
co = rect_geom.xy;
uv = rect_icon.xy;
@ -31,6 +57,7 @@ void main()
co = rect_geom.zy;
uv = rect_icon.zy;
}
#endif
gl_Position = ModelViewProjectionMatrix * vec4(co, 0.0f, 1.0f);
texCoord_interp = uv;

View File

@ -53,7 +53,7 @@ flat out float lineWidth;
noperspective out float butCo;
flat out float discardFac;
# ifdef OS_MAC
# if defined(OS_MAC) && defined(GPU_OPENGL)
in float dummy;
# endif
#endif

View File

@ -32,9 +32,9 @@ void linearrgb_to_srgb(vec4 col_from, out vec4 col_to)
void main()
{
fragColor = texture(image_texture, texCoord_interp.st);
vec4 overlay_col = texture(overlays_texture, texCoord_interp.st);
fragColor = texture(image_texture, texCoord_interp.xy);
vec4 overlay_col = texture(overlays_texture, texCoord_interp.xy);
if (overlay) {
fragColor = clamp(fragColor, 0.0, 1.0);
fragColor *= 1.0 - overlay_col.a;

View File

@ -43,7 +43,7 @@ bool test(int bit)
vec2 line_thresholds(float width)
{
return vec2(max(0, width - line_falloff), width);
return vec2(max(0.0, width - line_falloff), width);
}
void main()

View File

@ -52,7 +52,7 @@ bool is_inside_box(ivec2 v)
float texture_1D_custom_bilinear_filter(vec2 uv)
{
vec2 texel_2d = uv * glyph_dim + 0.5;
vec2 texel_2d = uv * vec2(glyph_dim) + vec2(0.5);
ivec2 texel_2d_near = ivec2(texel_2d) - 1;
int frag_offset = glyph_offset + texel_2d_near.y * glyph_dim.x + texel_2d_near.x;
@ -100,7 +100,7 @@ void main()
fragColor.a = texture_1D_custom_bilinear_filter(texCoord_interp);
}
else {
vec2 texel = 1.0 / glyph_dim;
vec2 texel = 1.0 / vec2(glyph_dim);
fragColor.a = 0.0;
if (interp_size == 1) {

View File

@ -29,7 +29,7 @@ void main()
texCoord_interp = mix(-interp_offset, 1.0 + interp_offset, quad);
vec2 final_pos = mix(
pos.xy + ivec2(-interp_size, interp_size), pos.zw + ivec2(interp_size, -interp_size), quad);
vec2(ivec2(pos.xy) + ivec2(-interp_size, interp_size)), vec2(ivec2(pos.zw) + ivec2(interp_size, -interp_size)), quad);
gl_Position = ModelViewProjectionMatrix * vec4(final_pos, 0.0, 1.0);
}

View File

@ -1,7 +1,7 @@
void node_light_falloff(
float strength, float tsmooth, out float quadratic, out float linear, out float constant)
float strength, float tsmooth, out float quadratic, out float linear, out float falloff_constant)
{
quadratic = strength;
linear = strength;
constant = strength;
falloff_constant = strength;
}

View File

@ -0,0 +1,17 @@
/* Backend Functions. */
#define select(A, B, mask) mix(A, B, mask)
bool is_zero(vec2 A)
{
return all(equal(A, vec2(0.0)));
}
bool is_zero(vec3 A)
{
return all(equal(A, vec3(0.0)));
}
bool is_zero(vec4 A)
{
return all(equal(A, vec4(0.0)));
}