From 0e85225e294774ecc38ffd84e19d44c614588eb3 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 6 Feb 2016 20:28:45 -0800 Subject: [PATCH] Bring back the depth range hack. --- Core/Compatibility.cpp | 1 + Core/Compatibility.h | 1 + GPU/GLES/GLES_GPU.cpp | 5 +++++ GPU/GLES/ShaderManager.cpp | 32 ++++++++++++++++++++++++++++++++ GPU/GPUState.h | 1 + assets/compat.ini | 14 ++++++++++++++ 6 files changed, 54 insertions(+) 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/GLES/GLES_GPU.cpp b/GPU/GLES/GLES_GPU.cpp index 10073fea38..649d77b123 100644 --- a/GPU/GLES/GLES_GPU.cpp +++ b/GPU/GLES/GLES_GPU.cpp @@ -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 8644599b29..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); diff --git a/GPU/GPUState.h b/GPU/GPUState.h index 1de1a4724b..59671d1dd4 100644 --- a/GPU/GPUState.h +++ b/GPU/GPUState.h @@ -455,6 +455,7 @@ 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), 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