From 87b75235e7208cb6f580e092f433b643cf8dbc17 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 6 Feb 2016 20:02:18 -0800 Subject: [PATCH] Improve depth accuracy a bit in 16-bit slice mode. --- GPU/Common/GPUStateUtils.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/GPU/Common/GPUStateUtils.cpp b/GPU/Common/GPUStateUtils.cpp index 52fe3be2cb..76454f8a6f 100644 --- a/GPU/Common/GPUStateUtils.cpp +++ b/GPU/Common/GPUStateUtils.cpp @@ -512,8 +512,14 @@ float DepthSliceFactor() { // This is used for float values which might not be integers, but are in the integer scale of 65535. static float ToScaledDepthFromInteger(float z) { const float depthSliceFactor = DepthSliceFactor(); - const float offset = 0.5f * (depthSliceFactor - 1.0f) * (1.0f / depthSliceFactor); - return z * (1.0f / depthSliceFactor) * (1.0f / 65535.0f) + offset; + if (gstate_c.Supports(GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT)) { + const double doffset = 0.5 * (depthSliceFactor - 1.0) * (1.0 / depthSliceFactor); + // Use one bit for each value, rather than 1.0 / (25535.0 * 256.0). + return (float)((double)z * (1.0 / 16777215.0) + doffset); + } else { + const float offset = 0.5f * (depthSliceFactor - 1.0f) * (1.0f / depthSliceFactor); + return z * (1.0f / depthSliceFactor) * (1.0f / 65535.0f) + offset; + } } float ToScaledDepth(u16 z) { @@ -579,8 +585,8 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo out.viewportY = renderY + displayOffsetY; out.viewportW = curRTWidth * renderWidthFactor; out.viewportH = curRTHeight * renderHeightFactor; - out.depthRangeMin = ToScaledDepth(0); - out.depthRangeMax = ToScaledDepth(65535); + out.depthRangeMin = ToScaledDepthFromInteger(0); + out.depthRangeMax = ToScaledDepthFromInteger(65536); } else { // These we can turn into a glViewport call, offset by offsetX and offsetY. Math after. float vpXScale = gstate.getViewportXScale();