diff --git a/Core/Config.cpp b/Core/Config.cpp index 10df16ea4e..96867bc9c1 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -451,6 +451,7 @@ static ConfigSetting graphicsSettings[] = { ConfigSetting("VSyncInterval", &g_Config.bVSync, false), ReportedConfigSetting("DisableStencilTest", &g_Config.bDisableStencilTest, false), ReportedConfigSetting("AlwaysDepthWrite", &g_Config.bAlwaysDepthWrite, false), + ReportedConfigSetting("DepthRangeHack", &g_Config.bDepthRangeHack, false), // Not really a graphics setting... ReportedConfigSetting("TimerHack", &g_Config.bTimerHack, &DefaultTimerHack), diff --git a/Core/Config.h b/Core/Config.h index f271bb5d51..f036ae37d2 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -165,6 +165,7 @@ public: bool bReloadCheats; bool bDisableStencilTest; bool bAlwaysDepthWrite; + bool bDepthRangeHack; bool bTimerHack; bool bAlphaMaskHack; bool bBlockTransferGPU; diff --git a/GPU/GLES/ShaderManager.cpp b/GPU/GLES/ShaderManager.cpp index 39a98e0aa0..e7365e1ae9 100644 --- a/GPU/GLES/ShaderManager.cpp +++ b/GPU/GLES/ShaderManager.cpp @@ -388,6 +388,39 @@ void LinkedShader::UpdateUniforms(u32 vertType) { flippedMatrix[0] = -flippedMatrix[0]; 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 (g_Config.bDepthRangeHack) { + float zScale = getFloat24(gstate.viewportz1) / 65535.0f; + float zOff = getFloat24(gstate.viewportz2) / 65535.0f; + + // if far depth range < 0 + if (zOff + 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 + ((zOff + 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; + } + } + } + } + glUniformMatrix4fv(u_proj, 1, GL_FALSE, flippedMatrix); } if (dirty & DIRTY_PROJTHROUGHMATRIX) diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 2ed0afc049..a2c050c3f3 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -278,6 +278,9 @@ void GameSettingsScreen::CreateViews() { CheckBox *prescale = graphicsSettings->Add(new CheckBox(&g_Config.bPrescaleUV, gs->T("Texture Coord Speedhack"))); prescale->SetDisabledPtr(&g_Config.bSoftwareRendering); + CheckBox *depthRange = graphicsSettings->Add(new CheckBox(&g_Config.bDepthRangeHack, gs->T("Depth Range Hack (Phantasy Star Portable 2)"))); + depthRange->SetDisabledPtr(&g_Config.bSoftwareRendering); + graphicsSettings->Add(new ItemHeader(gs->T("Overlay Information"))); static const char *fpsChoices[] = { "None", "Speed", "FPS", "Both"