From e52e9094ca5afb60bfbb67768302e3d9c8c30a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 12 Jul 2022 00:12:57 +0200 Subject: [PATCH 1/2] Vulkan fragment shaders: Don't opportunistically emit "layout (early_fragment_tests)" The purpose of this is not really for optimization, it's to get a specific behavior that we really don't care about. Drivers are pretty good at getting this potential performance improvement automatically if it's possible without changing the output. git blame says I added it, heh. Don't think it'll make much difference one way or another, though, but trying to reduce complexity. --- GPU/Common/FragmentShaderGenerator.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/GPU/Common/FragmentShaderGenerator.cpp b/GPU/Common/FragmentShaderGenerator.cpp index cb965bf782..2571690e4b 100644 --- a/GPU/Common/FragmentShaderGenerator.cpp +++ b/GPU/Common/FragmentShaderGenerator.cpp @@ -111,7 +111,6 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu if (compat.glslES30 || compat.shaderLanguage == ShaderLanguage::GLSL_VULKAN) shading = doFlatShading ? "flat" : ""; - bool earlyFragmentTests = ((!enableAlphaTest && !enableColorTest) || testForceToZero) && !gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT); bool useAdrenoBugWorkaround = id.Bit(FS_BIT_NO_DEPTH_CANNOT_DISCARD_STENCIL); bool readFramebuffer = replaceBlend == REPLACE_BLEND_COPY_FBO || colorWriteMask; @@ -131,9 +130,7 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu } if (compat.shaderLanguage == ShaderLanguage::GLSL_VULKAN) { - if (earlyFragmentTests) { - WRITE(p, "layout (early_fragment_tests) in;\n"); - } else if (useAdrenoBugWorkaround && !gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT)) { + if (useAdrenoBugWorkaround && !gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT)) { WRITE(p, "layout (depth_unchanged) out float gl_FragDepth;\n"); } @@ -1052,7 +1049,7 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu WRITE(p, " z = (1.0/65535.0) * floor(z * 65535.0);\n"); } WRITE(p, " gl_FragDepth = z;\n"); - } else if (!earlyFragmentTests && useAdrenoBugWorkaround) { + } else if (useAdrenoBugWorkaround) { // Adreno (and possibly MESA/others) apply early frag tests even with discard in the shader. // Writing depth prevents the bug, even with depth_unchanged specified. WRITE(p, " gl_FragDepth = gl_FragCoord.z;\n"); From 0ff8c721e4c594cd69ccbbb65b6aced02e57501a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 12 Jul 2022 00:17:26 +0200 Subject: [PATCH 2/2] Improve comment, variable name --- GPU/Common/FragmentShaderGenerator.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/GPU/Common/FragmentShaderGenerator.cpp b/GPU/Common/FragmentShaderGenerator.cpp index 2571690e4b..df12e041d4 100644 --- a/GPU/Common/FragmentShaderGenerator.cpp +++ b/GPU/Common/FragmentShaderGenerator.cpp @@ -111,7 +111,7 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu if (compat.glslES30 || compat.shaderLanguage == ShaderLanguage::GLSL_VULKAN) shading = doFlatShading ? "flat" : ""; - bool useAdrenoBugWorkaround = id.Bit(FS_BIT_NO_DEPTH_CANNOT_DISCARD_STENCIL); + bool useDiscardStencilBugWorkaround = id.Bit(FS_BIT_NO_DEPTH_CANNOT_DISCARD_STENCIL); bool readFramebuffer = replaceBlend == REPLACE_BLEND_COPY_FBO || colorWriteMask; bool readFramebufferTex = readFramebuffer && !gstate_c.Supports(GPU_SUPPORTS_ANY_FRAMEBUFFER_FETCH); @@ -130,7 +130,7 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu } if (compat.shaderLanguage == ShaderLanguage::GLSL_VULKAN) { - if (useAdrenoBugWorkaround && !gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT)) { + if (useDiscardStencilBugWorkaround && !gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT)) { WRITE(p, "layout (depth_unchanged) out float gl_FragDepth;\n"); } @@ -1049,9 +1049,11 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu WRITE(p, " z = (1.0/65535.0) * floor(z * 65535.0);\n"); } WRITE(p, " gl_FragDepth = z;\n"); - } else if (useAdrenoBugWorkaround) { - // Adreno (and possibly MESA/others) apply early frag tests even with discard in the shader. - // Writing depth prevents the bug, even with depth_unchanged specified. + } else if (useDiscardStencilBugWorkaround) { + // Adreno and some Mali drivers apply early frag tests even with discard in the shader, + // when only stencil is used. The exact situation seems to vary by driver. + // Writing depth prevents the bug for both vendors, even with depth_unchanged specified. + // This doesn't make a ton of sense, but empirically does work. WRITE(p, " gl_FragDepth = gl_FragCoord.z;\n"); }