From 3f338d8cb21bdbd02eb7160848efbc866fdd5528 Mon Sep 17 00:00:00 2001 From: Michael Parkin-White Date: Wed, 31 May 2023 11:56:11 +0100 Subject: [PATCH] Fix #107159: Resolve wireframe depth issue in Metal Bug in Metal when gl_FragDepth is used but not written to by a shader causing depth-based navigation to fail. Patch ensures gl_FragDepth is assigned a default value at the current depth from the vertex shader to catch all edge-cases of this. Authored by Apple: Michael Parkin-White --- source/blender/gpu/metal/mtl_shader_generator.mm | 8 ++++++++ source/blender/gpu/metal/mtl_texture.mm | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/source/blender/gpu/metal/mtl_shader_generator.mm b/source/blender/gpu/metal/mtl_shader_generator.mm index 9621cf3db69..73792bd1ff7 100644 --- a/source/blender/gpu/metal/mtl_shader_generator.mm +++ b/source/blender/gpu/metal/mtl_shader_generator.mm @@ -3113,6 +3113,14 @@ std::string MSLGeneratorInterface::generate_msl_fragment_input_population() << this->vertex_output_varyings[0].name << ";" << std::endl; } + /* Assign default gl_FragDepth. + * If gl_FragDepth is used, it should default to the original depth value. Resolves #107159 where + * overlay_wireframe_frag may not write to gl_FragDepth. */ + if (this->uses_gl_FragDepth) { + out << "\t" << shader_stage_inst_name << ".gl_FragDepth = " << shader_stage_inst_name + << ".gl_FragCoord.z;" << std::endl; + } + /* NOTE: We will only assign to the intersection of the vertex output and fragment input. * Fragment input represents varying variables which are declared (but are not necessarily * used). The Vertex out defines the set which is passed into the fragment shader, which diff --git a/source/blender/gpu/metal/mtl_texture.mm b/source/blender/gpu/metal/mtl_texture.mm index 37a289af705..bf88495b90b 100644 --- a/source/blender/gpu/metal/mtl_texture.mm +++ b/source/blender/gpu/metal/mtl_texture.mm @@ -1551,7 +1551,7 @@ void gpu::MTLTexture::read_internal(int mip, MTLComputeState &cs = ctx->main_command_buffer.get_compute_state(); cs.bind_pso(pso); cs.bind_compute_bytes(¶ms, sizeof(params), 0); - cs.bind_compute_buffer(destination_buffer, 0, 1); + cs.bind_compute_buffer(destination_buffer, 0, 1, true); cs.bind_compute_texture(read_texture, 0); [compute_encoder dispatchThreads:MTLSizeMake(width, height, 1) /* Width, Height, Layer */ threadsPerThreadgroup:MTLSizeMake(8, 8, 1)]; @@ -1601,7 +1601,7 @@ void gpu::MTLTexture::read_internal(int mip, MTLComputeState &cs = ctx->main_command_buffer.get_compute_state(); cs.bind_pso(pso); cs.bind_compute_bytes(¶ms, sizeof(params), 0); - cs.bind_compute_buffer(destination_buffer, 0, 1); + cs.bind_compute_buffer(destination_buffer, 0, 1, true); cs.bind_compute_texture(read_texture, 0); [compute_encoder dispatchThreads:MTLSizeMake(width, height, depth) /* Width, Height, Layer */ -- 2.30.2