From 116b3ba8cc1d02dd8a15b371a669a1ceddbad8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 11 Feb 2023 11:24:02 +0100 Subject: [PATCH] Increase precision of GetDepthScaleFactors to match ToScaledDepthFromIntegerScale --- GPU/Common/GPUStateUtils.cpp | 10 ++++++++-- unittest/UnitTest.cpp | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/GPU/Common/GPUStateUtils.cpp b/GPU/Common/GPUStateUtils.cpp index 8bbac0b9a2..c4e43bdd5c 100644 --- a/GPU/Common/GPUStateUtils.cpp +++ b/GPU/Common/GPUStateUtils.cpp @@ -573,8 +573,14 @@ DepthScaleFactors GetDepthScaleFactors(u32 useFlags) { } const double depthSliceFactor = DepthSliceFactor(useFlags); - const double offset = 0.5f * (depthSliceFactor - 1.0f) * (1.0f / depthSliceFactor); - return DepthScaleFactors(offset, (float)(depthSliceFactor * 65535.0)); + if (useFlags & GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT) { + const double offset = 0.5 * (depthSliceFactor - 1.0) / depthSliceFactor; + const double scale = 16777215.0; + return DepthScaleFactors(offset, scale); + } else { + const double offset = 0.5f * (depthSliceFactor - 1.0f) * (1.0f / depthSliceFactor); + return DepthScaleFactors(offset, (float)(depthSliceFactor * 65535.0)); + } } void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, float renderHeight, int bufferWidth, int bufferHeight, ViewportAndScissor &out) { diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index e7fe46e52b..1236349da5 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -810,7 +810,7 @@ static bool TestDepthMath() { GPU_USE_DEPTH_CLAMP | GPU_USE_ACCURATE_DEPTH, GPU_USE_DEPTH_CLAMP | GPU_USE_ACCURATE_DEPTH | GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT, // Here, GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT should take precedence over USE_DEPTH_CLAMP. }; - static const float expectedScale[] = { 65535.0f, 262140.0f, 16776960.0f, 65535.0f, 16776960.0f, }; + static const float expectedScale[] = { 65535.0f, 262140.0f, 16777215.0f, 65535.0f, 16777215.0f, }; static const float expectedOffset[] = { 0.0f, 0.375f, 0.498047f, 0.0f, 0.498047f, }; EXPECT_REL_EQ_FLOAT(100000.0f, 100001.0f, 0.00001f); @@ -822,7 +822,7 @@ static bool TestDepthMath() { EXPECT_EQ_FLOAT(factors.ScaleU16(), expectedScale[j]); EXPECT_REL_EQ_FLOAT(factors.Offset(), expectedOffset[j], 0.00001f); - EXPECT_EQ_FLOAT(factors.ScaleU16(), DepthSliceFactor(useFlags) * 65535.0f); + EXPECT_REL_EQ_FLOAT(factors.ScaleU16(), DepthSliceFactor(useFlags) * 65535.0f, 0.0001f); for (int i = 0; i < ARRAY_SIZE(testValues); i++) { float testValue = testValues[i] * 65535.0f;