Metal: GLSL shader compatibility changes for global uniform and interface name collision.

For the Metal shader translation support for shader-global uniforms are remapped via macro's, and in such cases where a uniform name matches a vertex attribute name, compilation errors will occur due to this injected syntax being incompatible with the immediate code.

Also adding source-level function interface alternatives where sized arrays are passed in. These are not supported directly in Metal shading language and are instead handled as pointers. These pointers require explicit address-space qualifiers in some cases, if device/constant address space memory is passed into the function.

Ref T96261

Reviewed By: fclem

Differential Revision: https://developer.blender.org/D15898
This commit is contained in:
Jason Fielder 2022-09-22 17:52:44 +02:00 committed by Clément Foucault
parent 1514e1a5b7
commit 18b45aabf9
25 changed files with 75 additions and 54 deletions

View File

@ -130,8 +130,10 @@ vec4 apply_dither(vec4 col, vec2 uv)
* \{ */
/* Prototypes: Implementation is generaterd and defined after. */
#ifndef GPU_METAL /* Forward declaration invalid in MSL. */
vec4 OCIO_to_scene_linear(vec4 pixel);
vec4 OCIO_to_display(vec4 pixel);
#endif
vec4 OCIO_ProcessColor(vec4 col, vec4 col_overlay)
{

View File

@ -140,11 +140,11 @@ void OVERLAY_armature_cache_init(OVERLAY_Data *vedata)
pd->armature_bone_select_act_grp = grp = DRW_shgroup_create(sh, psl->armature_bone_select_ps);
float4 color = {0.0f, 0.0f, 0.0f, alpha};
DRW_shgroup_uniform_vec4_copy(grp, "color", color);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", color);
pd->armature_bone_select_grp = grp = DRW_shgroup_create(sh, psl->armature_bone_select_ps);
color = {0.0f, 0.0f, 0.0f, powf(alpha, 4)};
DRW_shgroup_uniform_vec4_copy(grp, "color", color);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", color);
}
for (int i = 0; i < 2; i++) {

View File

@ -89,7 +89,7 @@ void OVERLAY_background_cache_init(OVERLAY_Data *vedata)
GPUShader *sh = OVERLAY_shader_clipbound();
DRWShadingGroup *grp = DRW_shgroup_create(sh, psl->clipping_frustum_ps);
DRW_shgroup_uniform_vec4_copy(grp, "color", G_draw.block.color_clipping_border);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", G_draw.block.color_clipping_border);
DRW_shgroup_uniform_vec3(grp, "boundbox", &bb->vec[0][0], 8);
struct GPUBatch *cube = DRW_cache_cube_get();

View File

@ -37,7 +37,7 @@ void OVERLAY_edit_text_cache_init(OVERLAY_Data *vedata)
sh = OVERLAY_shader_uniform_color();
pd->edit_text_wire_grp[i] = grp = DRW_shgroup_create(sh, psl->edit_text_wire_ps[i]);
DRW_shgroup_uniform_vec4_copy(grp, "color", G_draw.block.color_wire);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", G_draw.block.color_wire);
}
{
/* Cursor (text caret). */
@ -45,14 +45,14 @@ void OVERLAY_edit_text_cache_init(OVERLAY_Data *vedata)
DRW_PASS_CREATE(psl->edit_text_cursor_ps, state | pd->clipping_state);
sh = OVERLAY_shader_uniform_color();
pd->edit_text_cursor_grp = grp = DRW_shgroup_create(sh, psl->edit_text_cursor_ps);
DRW_shgroup_uniform_vec4(grp, "color", pd->edit_text.cursor_color, 1);
DRW_shgroup_uniform_vec4(grp, "ucolor", pd->edit_text.cursor_color, 1);
/* Selection boxes. */
state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA;
DRW_PASS_CREATE(psl->edit_text_selection_ps, state | pd->clipping_state);
sh = OVERLAY_shader_uniform_color();
pd->edit_text_selection_grp = grp = DRW_shgroup_create(sh, psl->edit_text_selection_ps);
DRW_shgroup_uniform_vec4(grp, "color", pd->edit_text.selection_color, 1);
DRW_shgroup_uniform_vec4(grp, "ucolor", pd->edit_text.selection_color, 1);
/* Highlight text within selection boxes. */
state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA | DRW_STATE_DEPTH_GREATER_EQUAL |

View File

@ -116,10 +116,10 @@ void OVERLAY_edit_uv_init(OVERLAY_Data *vedata)
const bool show_modified_uvs = sima->flag & SI_DRAWSHADOW;
const bool is_tiled_image = image && (image->source == IMA_SRC_TILED);
const bool do_edges_only = (ts->uv_flag & UV_SYNC_SELECTION) ?
/* NOTE: Ignore #SCE_SELECT_EDGE because a single selected edge
* on the mesh may cause single UV vertices to be selected. */
false :
(ts->uv_selectmode == UV_SELECT_EDGE);
/* NOTE: Ignore #SCE_SELECT_EDGE because a single selected edge
* on the mesh may cause single UV vertices to be selected. */
false :
(ts->uv_selectmode == UV_SELECT_EDGE);
const bool do_faces = ((sima->flag & SI_NO_DRAWFACES) == 0);
const bool do_face_dots = (ts->uv_flag & UV_SYNC_SELECTION) ?
(ts->selectmode & SCE_SELECT_FACE) != 0 :
@ -301,7 +301,7 @@ void OVERLAY_edit_uv_cache_init(OVERLAY_Data *vedata)
srgb_to_linearrgb_v4(selected_color, selected_color);
DRWShadingGroup *grp = DRW_shgroup_create(sh, psl->edit_uv_tiled_image_borders_ps);
DRW_shgroup_uniform_vec4_copy(grp, "color", theme_color);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", theme_color);
const float3 offset = {0.0f, 0.0f, 0.0f};
DRW_shgroup_uniform_vec3_copy(grp, "offset", offset);

View File

@ -195,23 +195,23 @@ void OVERLAY_extra_cache_init(OVERLAY_Data *vedata)
DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
grp_sub = DRW_shgroup_create_sub(grp);
DRW_shgroup_uniform_vec4_copy(grp_sub, "color", G_draw.block.color_active);
DRW_shgroup_uniform_vec4_copy(grp_sub, "ucolor", G_draw.block.color_active);
cb->center_active = BUF_POINT(grp_sub, format);
grp_sub = DRW_shgroup_create_sub(grp);
DRW_shgroup_uniform_vec4_copy(grp_sub, "color", G_draw.block.color_select);
DRW_shgroup_uniform_vec4_copy(grp_sub, "ucolor", G_draw.block.color_select);
cb->center_selected = BUF_POINT(grp_sub, format);
grp_sub = DRW_shgroup_create_sub(grp);
DRW_shgroup_uniform_vec4_copy(grp_sub, "color", G_draw.block.color_deselect);
DRW_shgroup_uniform_vec4_copy(grp_sub, "ucolor", G_draw.block.color_deselect);
cb->center_deselected = BUF_POINT(grp_sub, format);
grp_sub = DRW_shgroup_create_sub(grp);
DRW_shgroup_uniform_vec4_copy(grp_sub, "color", G_draw.block.color_library_select);
DRW_shgroup_uniform_vec4_copy(grp_sub, "ucolor", G_draw.block.color_library_select);
cb->center_selected_lib = BUF_POINT(grp_sub, format);
grp_sub = DRW_shgroup_create_sub(grp);
DRW_shgroup_uniform_vec4_copy(grp_sub, "color", G_draw.block.color_library);
DRW_shgroup_uniform_vec4_copy(grp_sub, "ucolor", G_draw.block.color_library);
cb->center_deselected_lib = BUF_POINT(grp_sub, format);
}
}

View File

@ -36,7 +36,7 @@ void OVERLAY_fade_cache_init(OVERLAY_Data *vedata)
if (draw_ctx->v3d->shading.background_type == V3D_SHADING_BACKGROUND_THEME) {
srgb_to_linearrgb_v4(color, color);
}
DRW_shgroup_uniform_vec4_copy(pd->fade_grp[i], "color", color);
DRW_shgroup_uniform_vec4_copy(pd->fade_grp[i], "ucolor", color);
}
if (!pd->use_in_front) {

View File

@ -290,7 +290,7 @@ void OVERLAY_grid_cache_init(OVERLAY_Data *ved)
/* add wire border */
GPUShader *sh = OVERLAY_shader_grid_image();
DRWShadingGroup *grp = DRW_shgroup_create(sh, psl->grid_ps);
DRW_shgroup_uniform_vec4_copy(grp, "color", theme_color);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", theme_color);
unit_m4(mat);
for (int x = 0; x < grid->size[0]; x++) {
mat[3][0] = x;

View File

@ -349,7 +349,7 @@ void OVERLAY_image_camera_cache_populate(OVERLAY_Data *vedata, Object *ob)
DRW_shgroup_uniform_bool_copy(grp, "imgAlphaBlend", true);
DRW_shgroup_uniform_bool_copy(grp, "isCameraBackground", true);
DRW_shgroup_uniform_bool_copy(grp, "depthSet", true);
DRW_shgroup_uniform_vec4_copy(grp, "color", color_premult_alpha);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", color_premult_alpha);
DRW_shgroup_call_obmat(grp, DRW_cache_quad_get(), mat);
}
}
@ -435,7 +435,7 @@ void OVERLAY_image_empty_cache_populate(OVERLAY_Data *vedata, Object *ob)
DRW_shgroup_uniform_bool_copy(grp, "imgAlphaBlend", use_alpha_blend);
DRW_shgroup_uniform_bool_copy(grp, "isCameraBackground", false);
DRW_shgroup_uniform_bool_copy(grp, "depthSet", depth_mode != OB_EMPTY_IMAGE_DEPTH_DEFAULT);
DRW_shgroup_uniform_vec4_copy(grp, "color", ob->color);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", ob->color);
DRW_shgroup_call_obmat(grp, DRW_cache_quad_get(), mat);
}
}

View File

@ -100,7 +100,7 @@ void OVERLAY_mode_transfer_cache_populate(OVERLAY_Data *vedata, Object *ob)
UI_GetThemeColor3fv(TH_VERTEX_SELECT, color);
color[3] = mode_transfer_alpha_for_animation_time_get(animation_time);
srgb_to_linearrgb_v4(color, color);
DRW_shgroup_uniform_vec4_copy(mode_transfer_grp[i], "color", color);
DRW_shgroup_uniform_vec4_copy(mode_transfer_grp[i], "ucolor", color);
}
if (!pd->use_in_front) {

View File

@ -168,7 +168,7 @@ void OVERLAY_paint_cache_init(OVERLAY_Data *vedata)
sh = OVERLAY_shader_paint_face();
pd->paint_face_grp = grp = DRW_shgroup_create(sh, psl->paint_overlay_ps);
const float4 color = {1.0f, 1.0f, 1.0f, 0.2f};
DRW_shgroup_uniform_vec4_copy(grp, "color", color);
DRW_shgroup_uniform_vec4_copy(grp, "ucolor", color);
DRW_shgroup_state_enable(grp, DRW_STATE_BLEND_ALPHA);
sh = OVERLAY_shader_paint_wire();

View File

@ -15,7 +15,7 @@ GPU_SHADER_CREATE_INFO(overlay_background)
GPU_SHADER_CREATE_INFO(overlay_clipbound)
.do_static_compilation(true)
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.push_constant(Type::VEC3, "boundbox", 8)
.vertex_source("overlay_clipbound_vert.glsl")
.fragment_out(0, Type::VEC4, "fragColor")

View File

@ -278,7 +278,7 @@ GPU_SHADER_CREATE_INFO(overlay_edit_uv_verts)
GPU_SHADER_CREATE_INFO(overlay_edit_uv_tiled_image_borders)
.do_static_compilation(true)
.vertex_in(0, Type::VEC3, "pos")
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.fragment_out(0, Type::VEC4, "fragColor")
.vertex_source("overlay_edit_uv_tiled_image_borders_vert.glsl")
.fragment_source("overlay_uniform_color_frag.glsl")
@ -294,7 +294,7 @@ GPU_SHADER_CREATE_INFO(overlay_edit_uv_stencil_image)
.sampler(0, ImageType::FLOAT_2D, "imgTexture")
.push_constant(Type::BOOL, "imgPremultiplied")
.push_constant(Type::BOOL, "imgAlphaBlend")
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.fragment_out(0, Type::VEC4, "fragColor")
.fragment_source("overlay_image_frag.glsl")
.additional_info("draw_mesh");
@ -581,7 +581,7 @@ GPU_SHADER_CREATE_INFO(overlay_depth_only_clipped)
GPU_SHADER_CREATE_INFO(overlay_uniform_color)
.do_static_compilation(true)
.vertex_in(0, Type::VEC3, "pos")
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.fragment_out(0, Type::VEC4, "fragColor")
.vertex_source("overlay_depth_only_vert.glsl")
.fragment_source("overlay_uniform_color_frag.glsl")

View File

@ -145,7 +145,7 @@ GPU_SHADER_CREATE_INFO(overlay_extra_point)
/* TODO(fclem): Move the vertex shader to Overlay engine and remove this bypass. */
.define("blender_srgb_to_framebuffer_space(a)", "a")
.vertex_in(0, Type::VEC3, "pos")
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.vertex_out(overlay_extra_point_iface)
.fragment_out(0, Type::VEC4, "fragColor")
.vertex_source("overlay_extra_point_vert.glsl")
@ -161,7 +161,7 @@ GPU_SHADER_INTERFACE_INFO(overlay_extra_loose_point_iface, "").smooth(Type::VEC4
GPU_SHADER_CREATE_INFO(overlay_extra_loose_point)
.do_static_compilation(true)
.vertex_in(0, Type::VEC3, "pos")
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.vertex_out(overlay_extra_loose_point_iface)
.fragment_out(0, Type::VEC4, "fragColor")
.fragment_out(1, Type::VEC4, "lineOutput")
@ -257,7 +257,7 @@ GPU_SHADER_CREATE_INFO(overlay_image)
.push_constant(Type::BOOL, "isCameraBackground")
.push_constant(Type::BOOL, "imgPremultiplied")
.push_constant(Type::BOOL, "imgAlphaBlend")
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.vertex_in(0, Type::VEC3, "pos")
.vertex_out(overlay_image_iface)
.sampler(0, ImageType::FLOAT_2D, "imgTexture")
@ -304,7 +304,7 @@ GPU_SHADER_INTERFACE_INFO(overlay_particle_iface, "").flat(Type::VEC4, "finalCol
GPU_SHADER_CREATE_INFO(overlay_particle)
.sampler(0, ImageType::FLOAT_1D, "weightTex")
.push_constant(Type::VEC4, "color") /* Draw-size packed in alpha. */
.push_constant(Type::VEC4, "ucolor") /* Draw-size packed in alpha. */
.vertex_in(0, Type::VEC3, "part_pos")
.vertex_in(1, Type::VEC4, "part_rot")
.vertex_in(2, Type::FLOAT, "part_val")

View File

@ -32,7 +32,7 @@ GPU_SHADER_CREATE_INFO(overlay_grid_background)
GPU_SHADER_CREATE_INFO(overlay_grid_image)
.do_static_compilation(true)
.vertex_in(0, Type::VEC3, "pos")
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.fragment_out(0, Type::VEC4, "fragColor")
.vertex_source("overlay_edit_uv_tiled_image_borders_vert.glsl")
.fragment_source("overlay_uniform_color_frag.glsl")

View File

@ -12,7 +12,7 @@ GPU_SHADER_CREATE_INFO(overlay_paint_face)
.do_static_compilation(true)
.vertex_in(0, Type::VEC3, "pos")
.vertex_in(1, Type::VEC4, "nor") /* Select flag on the 4th component. */
.push_constant(Type::VEC4, "color")
.push_constant(Type::VEC4, "ucolor")
.fragment_out(0, Type::VEC4, "fragColor")
.vertex_source("overlay_paint_face_vert.glsl")
.fragment_source("overlay_uniform_color_frag.glsl")

View File

@ -16,7 +16,7 @@ void main()
radii[3] = radius - outline_width - 1.0;
radii /= sizeObjectCenter;
fillColor = color;
fillColor = ucolor;
outlineColor = colorOutline;
view_clipping_distances(world_pos);

View File

@ -16,7 +16,7 @@ vec3 rotate(vec3 vec, vec4 quat)
void main()
{
/* Drawsize packed in alpha. */
float draw_size = color.a;
float draw_size = ucolor.a;
vec3 world_pos = part_pos;
@ -43,7 +43,7 @@ void main()
finalColor = vec4(clamp(pos * 10000.0, 0.0, 1.0), 1.0);
}
else if (part_val < 0.0) {
finalColor = vec4(color.rgb, 1.0);
finalColor = vec4(ucolor.rgb, 1.0);
}
else {
finalColor = vec4(texture(weightTex, part_val).rgb, 1.0);

View File

@ -1,4 +1,4 @@
void main()
{
fragColor = color;
fragColor = ucolor;
}

View File

@ -6,7 +6,7 @@
#pragma BLENDER_REQUIRE(workbench_world_light_lib.glsl)
/* Special function only to be used with calculate_transparent_weight(). */
float linear_zdepth(float depth, vec4 viewvecs[2], mat4 proj_mat)
float linear_zdepth(float depth, mat4 proj_mat)
{
if (proj_mat[3][3] == 0.0) {
float d = 2.0 * depth - 1.0;
@ -14,7 +14,7 @@ float linear_zdepth(float depth, vec4 viewvecs[2], mat4 proj_mat)
}
else {
/* Return depth from near plane. */
return depth * viewvecs[1].z;
return depth * drw_view.viewvecs[1].z;
}
}
@ -24,7 +24,7 @@ float linear_zdepth(float depth, vec4 viewvecs[2], mat4 proj_mat)
*/
float calculate_transparent_weight(void)
{
float z = linear_zdepth(gl_FragCoord.z, drw_view.viewvecs, drw_view.winmat);
float z = linear_zdepth(gl_FragCoord.z, drw_view.winmat);
#if 0
/* Eq 10 : Good for surfaces with varying opacity (like particles) */
float a = min(1.0, alpha * 10.0) + 0.01;

View File

@ -588,15 +588,18 @@ SamplerState PointSampler
# else
# define mad(a, b, c) (a * b + c)
# endif
# define float2 vec2
# define float3 vec3
# define float4 vec4
# define int2 ivec2
# define int3 ivec3
# define int4 ivec4
# define bool2 bvec2
# define bool3 bvec3
# define bool4 bvec4
/* NOTE(Metal): Types already natively declared in MSL. */
# ifndef GPU_METAL
# define float2 vec2
# define float3 vec3
# define float4 vec4
# define int2 ivec2
# define int3 ivec3
# define int4 ivec4
# define bool2 bvec2
# define bool3 bvec3
# define bool4 bvec4
# endif
#endif
/* clang-format off */
@ -658,7 +661,14 @@ void SMAAMovc(bool4 cond, inout float4 variable, float4 value)
/**
* Edge Detection Vertex Shader
*/
# ifdef GPU_METAL
/* NOTE: Metal API requires explicit address space qualifiers for pointer types.
* Arrays in functions are passed as pointers, and thus require explicit address
* space. */
void SMAAEdgeDetectionVS(float2 texcoord, thread float4 *offset)
# else
void SMAAEdgeDetectionVS(float2 texcoord, out float4 offset[3])
# endif
{
offset[0] = mad(SMAA_RT_METRICS.xyxy, float4(-1.0, 0.0, 0.0, -1.0), texcoord.xyxy);
offset[1] = mad(SMAA_RT_METRICS.xyxy, float4(1.0, 0.0, 0.0, 1.0), texcoord.xyxy);
@ -668,7 +678,16 @@ void SMAAEdgeDetectionVS(float2 texcoord, out float4 offset[3])
/**
* Blend Weight Calculation Vertex Shader
*/
# ifdef GPU_METAL
/* NOTE: Metal API requires explicit address space qualifiers for pointer types.
* Arrays in functions are passed as pointers, and thus require explicit address
* space. */
void SMAABlendingWeightCalculationVS(float2 texcoord,
thread float2 &pixcoord,
thread float4 *offset)
# else
void SMAABlendingWeightCalculationVS(float2 texcoord, out float2 pixcoord, out float4 offset[3])
# endif
{
pixcoord = texcoord * SMAA_RT_METRICS.zw;

View File

@ -7,7 +7,7 @@ void main()
discard;
}
#endif
fragColor = interp.color;
fragColor = interp.final_color;
if (lineSmooth) {
fragColor.a *= clamp((lineWidth + SMOOTH_WIDTH) * 0.5 - abs(interp.smoothline), 0.0, 1.0);
}

View File

@ -18,14 +18,14 @@ vec4 clip_line_point_homogeneous_space(vec4 p, vec4 q)
void do_vertex(const int i, vec4 pos, vec2 ofs)
{
#if defined(UNIFORM)
interp_out.color = color;
interp_out.final_color = color;
#elif defined(FLAT)
/* WATCH: Assuming last provoking vertex. */
interp_out.color = interp_in[1].color;
interp_out.final_color = interp_in[1].final_color;
#elif defined(SMOOTH)
interp_out.color = interp_in[i].color;
interp_out.final_color = interp_in[i].final_color;
#endif
#ifdef CLIP

View File

@ -3,7 +3,7 @@ void main()
{
gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
#ifndef UNIFORM
interp.color = color;
interp.final_color = color;
#endif
#ifdef CLIP
interp.clip = dot(ModelMatrix * vec4(pos, 1.0), ClipPlane);

View File

@ -9,7 +9,7 @@
#include "gpu_shader_create_info.hh"
GPU_SHADER_INTERFACE_INFO(gpu_shader_3D_polyline_iface, "interp")
.smooth(Type::VEC4, "color")
.smooth(Type::VEC4, "final_color")
.smooth(Type::FLOAT, "clip")
.no_perspective(Type::FLOAT, "smoothline");