Skip to content

Commit 74f9b55

Browse files
authored
Merge pull request #17055 from unknownbrackets/depth-zero-scale
GPU: Correct depth clip/cull for zero scale
2 parents 7a52f3a + 9fcc150 commit 74f9b55

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

GPU/Common/ShaderUniforms.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,15 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipView
252252

253253
// These are just the reverse of the formulas in GPUStateUtils.
254254
float halfActualZRange = gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f;
255+
float inverseDepthScale = gstate_c.vpDepthScale != 0.0f ? 1.0f / gstate_c.vpDepthScale : 0.0f;
255256
float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange;
256257
float viewZScale = halfActualZRange * 2.0f;
257258
float viewZCenter = minz;
258259

259260
ub->depthRange[0] = viewZScale;
260261
ub->depthRange[1] = viewZCenter;
261262
ub->depthRange[2] = gstate_c.vpZOffset * 0.5f + 0.5f;
262-
ub->depthRange[3] = 2.0f * (1.0f / gstate_c.vpDepthScale);
263+
ub->depthRange[3] = 2.0f * inverseDepthScale;
263264
}
264265

265266
if (dirtyUniforms & DIRTY_CULLRANGE) {

GPU/Common/VertexShaderGenerator.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,8 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag
13501350
}
13511351
if (gstate_c.Use(GPU_USE_CULL_DISTANCE)) {
13521352
// Cull any triangle fully outside in the same direction when depth clamp enabled.
1353-
WRITE(p, " if (u_cullRangeMin.w > 0.0) {\n");
1353+
// We check u_depthRange in case depthScale was zero - in that case we can't work out the cull distance.
1354+
WRITE(p, " if (u_cullRangeMin.w > 0.0 && u_depthRange.w != 0.0f) {\n");
13541355
WRITE(p, " %sgl_CullDistance%s = projPos.z - u_cullRangeMin.z;\n", compat.vsOutPrefix, cull0);
13551356
WRITE(p, " %sgl_CullDistance%s = u_cullRangeMax.z - projPos.z;\n", compat.vsOutPrefix, cull1);
13561357
WRITE(p, " } else {\n");

GPU/Directx9/ShaderManagerDX9.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ void ShaderManagerDX9::VSUpdateUniforms(u64 dirtyUniforms) {
466466
float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange;
467467
float viewZScale = halfActualZRange * 2.0f;
468468
float viewZCenter = minz;
469-
float reverseScale = 2.0f * (1.0f / gstate_c.vpDepthScale);
469+
float reverseScale = gstate_c.vpDepthScale != 0.0f ? 2.0f * (1.0f / gstate_c.vpDepthScale) : 0.0f;
470470
float reverseTranslate = gstate_c.vpZOffset * 0.5f + 0.5f;
471471

472472
float data[4] = { viewZScale, viewZCenter, reverseTranslate, reverseScale };

GPU/GLES/ShaderManagerGLES.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ void LinkedShader::UpdateUniforms(const ShaderID &vsid, bool useBufferedRenderin
589589

590590
// These are just the reverse of the formulas in GPUStateUtils.
591591
float halfActualZRange = gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f;
592+
float inverseDepthScale = gstate_c.vpDepthScale != 0.0f ? 1.0f / gstate_c.vpDepthScale : 0.0f;
592593
float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange;
593594
float viewZScale = halfActualZRange;
594595
float viewZCenter = minz + halfActualZRange;
@@ -598,7 +599,7 @@ void LinkedShader::UpdateUniforms(const ShaderID &vsid, bool useBufferedRenderin
598599
viewZCenter = vpZCenter;
599600
}
600601

601-
float data[4] = { viewZScale, viewZCenter, gstate_c.vpZOffset, 1.0f / gstate_c.vpDepthScale };
602+
float data[4] = { viewZScale, viewZCenter, gstate_c.vpZOffset, inverseDepthScale };
602603
SetFloatUniform4(render_, &u_depthRange, data);
603604
}
604605
if (dirty & DIRTY_CULLRANGE) {

0 commit comments

Comments
 (0)