diff --git a/Core/Compatibility.cpp b/Core/Compatibility.cpp index 7fb59b50f6..07ec5b627b 100644 --- a/Core/Compatibility.cpp +++ b/Core/Compatibility.cpp @@ -47,6 +47,7 @@ void Compatibility::Clear() { void Compatibility::CheckSettings(IniFile &iniFile, const std::string &gameID) { CheckSetting(iniFile, gameID, "VertexDepthRounding", flags_.VertexDepthRounding); CheckSetting(iniFile, gameID, "PixelDepthRounding", flags_.PixelDepthRounding); + CheckSetting(iniFile, gameID, "DepthRangeHack", flags_.DepthRangeHack); } void Compatibility::CheckSetting(IniFile &iniFile, const std::string &gameID, const char *option, bool &flag) { diff --git a/Core/Compatibility.h b/Core/Compatibility.h index b600c1f7e9..90413934cf 100644 --- a/Core/Compatibility.h +++ b/Core/Compatibility.h @@ -47,6 +47,7 @@ struct CompatFlags { bool VertexDepthRounding; bool PixelDepthRounding; + bool DepthRangeHack; }; class IniFile; diff --git a/GPU/Common/GPUStateUtils.cpp b/GPU/Common/GPUStateUtils.cpp index 52fe3be2cb..98a4e2bd0d 100644 --- a/GPU/Common/GPUStateUtils.cpp +++ b/GPU/Common/GPUStateUtils.cpp @@ -511,9 +511,19 @@ 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) { + if (!gstate_c.Supports(GPU_SUPPORTS_ACCURATE_DEPTH)) { + return z * (1.0f / 65535.0f); + } + 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 +589,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(); @@ -684,8 +694,19 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo // This adjusts the center from halfActualZRange to vpZCenter. float zOffset = halfActualZRange < std::numeric_limits::epsilon() ? 0.0f : (vpZCenter - (minz + halfActualZRange)) / halfActualZRange; - out.depthRangeMin = ToScaledDepthFromInteger(minz); - out.depthRangeMax = ToScaledDepthFromInteger(maxz); + if (!gstate_c.Supports(GPU_SUPPORTS_ACCURATE_DEPTH)) { + zScale = 1.0f; + zOffset = 0.0f; + out.depthRangeMin = ToScaledDepthFromInteger(vpZCenter - vpZScale); + out.depthRangeMax = ToScaledDepthFromInteger(vpZCenter + vpZScale); + } else { + out.depthRangeMin = ToScaledDepthFromInteger(minz); + out.depthRangeMax = ToScaledDepthFromInteger(maxz); + } + + // OpenGL will clamp these for us anyway, and Direct3D will error if not clamped. + out.depthRangeMin = std::max(out.depthRangeMin, 0.0f); + out.depthRangeMax = std::min(out.depthRangeMax, 1.0f); bool scaleChanged = gstate_c.vpWidthScale != wScale || gstate_c.vpHeightScale != hScale; bool offsetChanged = gstate_c.vpXOffset != xOffset || gstate_c.vpYOffset != yOffset; diff --git a/GPU/Directx9/GPU_DX9.cpp b/GPU/Directx9/GPU_DX9.cpp index b88e3d385f..1785abce39 100644 --- a/GPU/Directx9/GPU_DX9.cpp +++ b/GPU/Directx9/GPU_DX9.cpp @@ -477,6 +477,7 @@ void DIRECTX9_GPU::CheckGPUFeatures() { features |= GPU_SUPPORTS_BLEND_MINMAX; features |= GPU_SUPPORTS_TEXTURE_LOD_CONTROL; features |= GPU_PREFER_CPU_DOWNLOAD; + features |= GPU_SUPPORTS_ACCURATE_DEPTH; if (!g_Config.bHighQualityDepth) { features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; diff --git a/GPU/GLES/FragmentShaderGenerator.cpp b/GPU/GLES/FragmentShaderGenerator.cpp index 911a4d6c4e..2959a65c1e 100644 --- a/GPU/GLES/FragmentShaderGenerator.cpp +++ b/GPU/GLES/FragmentShaderGenerator.cpp @@ -679,12 +679,16 @@ bool GenerateFragmentShader(const ShaderID &id, char *buffer) { const double scale = DepthSliceFactor() * 65535.0; WRITE(p, " highp float z = gl_FragCoord.z;\n"); - // We center the depth with an offset, but only its fraction matters. - // When (DepthSliceFactor() - 1) is odd, it will be 0.5, otherwise 0. - if (((int)(DepthSliceFactor() - 1.0f) & 1) == 1) { - WRITE(p, " z = (floor((z * %f) - (1.0 / 2.0)) + (1.0 / 2.0)) * (1.0 / %f);\n", scale, scale); + if (gstate_c.Supports(GPU_SUPPORTS_ACCURATE_DEPTH)) { + // We center the depth with an offset, but only its fraction matters. + // When (DepthSliceFactor() - 1) is odd, it will be 0.5, otherwise 0. + if (((int)(DepthSliceFactor() - 1.0f) & 1) == 1) { + WRITE(p, " z = (floor((z * %f) - (1.0 / 2.0)) + (1.0 / 2.0)) * (1.0 / %f);\n", scale, scale); + } else { + WRITE(p, " z = floor(z * %f) * (1.0 / %f);\n", scale, scale); + } } else { - WRITE(p, " z = floor(z * %f) * (1.0 / %f);\n", scale, scale); + WRITE(p, " z = (1.0/65535.0) * floor(z * 65535.0);\n"); } WRITE(p, " gl_FragDepth = z;\n"); } diff --git a/GPU/GLES/GLES_GPU.cpp b/GPU/GLES/GLES_GPU.cpp index 10073fea38..9efdeb0b8d 100644 --- a/GPU/GLES/GLES_GPU.cpp +++ b/GPU/GLES/GLES_GPU.cpp @@ -567,13 +567,13 @@ void GLES_GPU::CheckGPUFeatures() { // If we already have a 16-bit depth buffer, we don't need to round. if (fbo_standard_z_depth() > 16) { - if (!g_Config.bHighQualityDepth) { + if (!g_Config.bHighQualityDepth && (features & GPU_SUPPORTS_ACCURATE_DEPTH) != 0) { features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; } else if (PSP_CoreParameter().compat.flags().PixelDepthRounding) { if (!gl_extensions.IsGLES || gl_extensions.GLES3) { // Use fragment rounding on desktop and GLES3, most accurate. features |= GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT; - } else if (fbo_standard_z_depth() == 24) { + } else if (fbo_standard_z_depth() == 24 && (features & GPU_SUPPORTS_ACCURATE_DEPTH) != 0) { // Here we can simulate a 16 bit depth buffer by scaling. // Note that the depth buffer is fixed point, not floating, so dividing by 256 is pretty good. features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; @@ -586,6 +586,11 @@ void GLES_GPU::CheckGPUFeatures() { } } + // The Phantasy Star hack :( + if (PSP_CoreParameter().compat.flags().DepthRangeHack && (features & GPU_SUPPORTS_ACCURATE_DEPTH) == 0) { + features |= GPU_USE_DEPTH_RANGE_HACK; + } + #ifdef MOBILE_DEVICE // Arguably, we should turn off GPU_IS_MOBILE on like modern Tegras, etc. features |= GPU_IS_MOBILE; diff --git a/GPU/GLES/ShaderManager.cpp b/GPU/GLES/ShaderManager.cpp index f4a1bfd008..fab370a0d7 100644 --- a/GPU/GLES/ShaderManager.cpp +++ b/GPU/GLES/ShaderManager.cpp @@ -437,6 +437,38 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid) { flippedMatrix[12] = -flippedMatrix[12]; } + // In Phantasy Star Portable 2, depth range sometimes goes negative and is clamped by glDepthRange to 0, + // causing graphics clipping glitch (issue #1788). This hack modifies the projection matrix to work around it. + if (gstate_c.Supports(GPU_USE_DEPTH_RANGE_HACK)) { + float zScale = gstate.getViewportZScale() / 65535.0f; + float zCenter = gstate.getViewportZCenter() / 65535.0f; + + // if far depth range < 0 + if (zCenter + zScale < 0.0f) { + // if perspective projection + if (flippedMatrix[11] < 0.0f) { + float depthMax = gstate.getDepthRangeMax() / 65535.0f; + float depthMin = gstate.getDepthRangeMin() / 65535.0f; + + float a = flippedMatrix[10]; + float b = flippedMatrix[14]; + + float n = b / (a - 1.0f); + float f = b / (a + 1.0f); + + f = (n * f) / (n + ((zCenter + zScale) * (n - f) / (depthMax - depthMin))); + + a = (n + f) / (n - f); + b = (2.0f * n * f) / (n - f); + + if (!my_isnan(a) && !my_isnan(b)) { + flippedMatrix[10] = a; + flippedMatrix[14] = b; + } + } + } + } + ScaleProjMatrix(flippedMatrix); glUniformMatrix4fv(u_proj, 1, GL_FALSE, flippedMatrix.m); @@ -598,8 +630,13 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid) { float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange; float viewZScale = halfActualZRange; float viewZCenter = minz + halfActualZRange; - float viewZInvScale; + if (!gstate_c.Supports(GPU_SUPPORTS_ACCURATE_DEPTH)) { + viewZScale = vpZScale; + viewZCenter = vpZCenter; + } + + float viewZInvScale; if (viewZScale != 0.0) { viewZInvScale = 1.0f / viewZScale; } else { diff --git a/GPU/GPUState.h b/GPU/GPUState.h index b3467c39b2..59671d1dd4 100644 --- a/GPU/GPUState.h +++ b/GPU/GPUState.h @@ -455,6 +455,8 @@ enum { GPU_SUPPORTS_UNPACK_SUBIMAGE = FLAG_BIT(3), GPU_SUPPORTS_BLEND_MINMAX = FLAG_BIT(4), GPU_SUPPORTS_LOGIC_OP = FLAG_BIT(5), + GPU_USE_DEPTH_RANGE_HACK = FLAG_BIT(6), + GPU_SUPPORTS_ACCURATE_DEPTH = FLAG_BIT(17), GPU_SUPPORTS_VAO = FLAG_BIT(18), GPU_SUPPORTS_ANY_COPY_IMAGE = FLAG_BIT(19), GPU_SUPPORTS_ANY_FRAMEBUFFER_FETCH = FLAG_BIT(20), diff --git a/assets/compat.ini b/assets/compat.ini index a8ff53d169..58d3a2f7f9 100644 --- a/assets/compat.ini +++ b/assets/compat.ini @@ -87,3 +87,17 @@ NPJH90002 = true # Phantasy Star Portable 2 JP Demo ULJM91018 = true NPJH90062 = true + +[DepthRangeHack] +# Phantasy Star Portable 2 and Infinity both use viewport depth outside [0, 1]. +# This gets clamped in our current implementation, but attempts to fix it run into +# Other bugs, so we've restored this hack for now. +ULJM05493 = true +NPJH50043 = true +ULJM08030 = true +ULES01439 = true +ULUS10529 = true +ULJM91018 = true # Infinity demo disc? +NPJH90157 = true # Infinity demo +ULJM05732 = true +NPJH50332 = true