From 7e5436899827cd42e2bd5ef0614cfce21f371a48 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 3 Jan 2016 12:37:40 -0800 Subject: [PATCH] Correct depth offset when clamped. Of course, the scale is already half, so we just need to double for d3d9, and not double for OpenGL of course. Our offset is already in [0, 1] so we keep it. --- GPU/Directx9/ShaderManagerDX9.cpp | 11 +++++------ GPU/GLES/ShaderManager.cpp | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/GPU/Directx9/ShaderManagerDX9.cpp b/GPU/Directx9/ShaderManagerDX9.cpp index 3a2a8dd917..fe60271b5c 100644 --- a/GPU/Directx9/ShaderManagerDX9.cpp +++ b/GPU/Directx9/ShaderManagerDX9.cpp @@ -260,7 +260,7 @@ static void ConvertProjMatrixToD3D(Matrix4x4 &in, bool invertedX, bool invertedY if (invertedY) yoff = -yoff; - const Vec3 trans(xoff, yoff, gstate_c.vpZOffset * 0.5f + 0.5f); + const Vec3 trans(xoff, yoff, gstate_c.vpZOffset + 0.5f); const Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f); in.translateAndScale(trans, scale); } @@ -482,6 +482,7 @@ void ShaderManagerDX9::VSUpdateUniforms(int dirtyUniforms) { if (dirtyUniforms & DIRTY_DEPTHRANGE) { float viewZScale = gstate.getViewportZScale(); float viewZCenter = gstate.getViewportZCenter(); + float viewZInvScale; // We had to scale and translate Z to account for our clamped Z range. // Therefore, we also need to reverse this to round properly. @@ -493,12 +494,10 @@ void ShaderManagerDX9::VSUpdateUniforms(int dirtyUniforms) { // // The projection already accounts for those, so we need to reverse them. // - // Additionally, D3D9 uses a range from [0, 1] which makes things easy. - // We just correct the center. - viewZScale *= (1.0f / gstate_c.vpDepthScale); - viewZCenter -= 65535.0f * gstate_c.vpZOffset - 32767.5f; + // Additionally, D3D9 uses a range from [0, 1]. We double and move the center. + viewZScale *= (1.0f / gstate_c.vpDepthScale) * 2.0f; + viewZCenter -= 65535.0f * gstate_c.vpZOffset + 32768.5f; - float viewZInvScale; if (viewZScale != 0.0) { viewZInvScale = 1.0f / viewZScale; } else { diff --git a/GPU/GLES/ShaderManager.cpp b/GPU/GLES/ShaderManager.cpp index de5457e8ae..7b327932a0 100644 --- a/GPU/GLES/ShaderManager.cpp +++ b/GPU/GLES/ShaderManager.cpp @@ -603,9 +603,9 @@ void LinkedShader::UpdateUniforms(u32 vertType) { // // The projection already accounts for those, so we need to reverse them. // - // Additionally, OpenGL uses a range from [-1, 1]. So we halve the scale. - viewZScale *= (1.0f / gstate_c.vpDepthScale) * 0.5f; - viewZCenter -= 65535.0f * gstate_c.vpZOffset; + // Additionally, OpenGL uses a range from [-1, 1]. So we multiply by scale and add the center. + viewZScale *= (1.0f / gstate_c.vpDepthScale); + viewZCenter -= 65535.0f * (gstate_c.vpZOffset); if (viewZScale != 0.0) { viewZInvScale = 1.0f / viewZScale;