From bfaa304461e99b1b088db08ecec4f78d10f6adc2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 27 Oct 2021 21:22:45 -0700 Subject: [PATCH] Vulkan: Correct geometry shader culling. --- GPU/Common/GeometryShaderGenerator.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/GPU/Common/GeometryShaderGenerator.cpp b/GPU/Common/GeometryShaderGenerator.cpp index 5ec7875e5c..2c2906d100 100644 --- a/GPU/Common/GeometryShaderGenerator.cpp +++ b/GPU/Common/GeometryShaderGenerator.cpp @@ -75,7 +75,7 @@ bool GenerateGeometryShader(const GShaderID &id, char *buffer, const ShaderLangu p.BeginGSMain(varyings, outVaryings); // Apply culling - p.C(" bool anyInside = false;\n"); // TODO: 3 or gl_in.length()? which will be faster? + p.C(" bool anyInside = false;\n"); p.C(" for (int i = 0; i < 3; i++) {\n"); // TODO: 3 or gl_in.length()? which will be faster? p.C(" vec4 outPos = gl_in[i].gl_Position;\n"); @@ -91,16 +91,20 @@ bool GenerateGeometryShader(const GShaderID &id, char *buffer, const ShaderLangu p.C(" }\n"); p.C(" if (u_cullRangeMin.w <= 0.0) {\n"); p.C(" if (projPos.z < u_cullRangeMin.z || projPos.z > u_cullRangeMax.z) {\n"); - p.C(" return;\n"); // Cull! + // When not clamping depth, cull the triangle of Z is outside the valid range (not based on clip Z.) + p.C(" return;\n"); p.C(" }\n"); p.C(" } else {\n"); - p.C(" if (projPos.z >= u_cullRangeMin.z && projPos.z <= u_cullRangeMax.z) { anyInside = true; }\n"); + p.C(" if (projPos.z >= u_cullRangeMin.z) { anyInside = true; }\n"); + p.C(" if (projPos.z <= u_cullRangeMax.z) { anyInside = true; }\n"); p.C(" }\n"); p.C(" } // for\n"); // Cull any triangle fully outside in the same direction when depth clamp enabled. // Basically simulate cull distances. - p.C(" if (!anyInside) { return; }\n"); + p.C(" if (u_cullRangeMin.w > 0.0 && !anyInside) {\n"); + p.C(" return;\n"); + p.C(" }\n"); const char *clip0 = compat.shaderLanguage == HLSL_D3D11 ? "" : "[0]"; @@ -119,7 +123,7 @@ bool GenerateGeometryShader(const GShaderID &id, char *buffer, const ShaderLangu p.F(" %s = %s[i];\n", outVaryings[i].name, varyings[i].name); } // Debug - null the red channel - p.C(" if (i == 0) v_color0Out.x = 0.0;\n"); + //p.C(" if (i == 0) v_color0Out.x = 0.0;\n"); p.C(" EmitVertex();\n"); p.C(" }\n");