From a98980d28b207bb98cebeb13e95162dddb93441c Mon Sep 17 00:00:00 2001 From: level99procrastinator Date: Sat, 8 Nov 2014 20:31:17 +0800 Subject: [PATCH 1/3] Workaround for graphics glitch in Phantasy Star Portable 2 --- Core/Config.cpp | 1 + Core/Config.h | 1 + GPU/GLES/StateMapping.cpp | 43 ++++++++++++++++++++++++++++++++++++--- UI/GameSettingsScreen.cpp | 3 +++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index a707a4e9e9..64ed3e27a2 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -444,6 +444,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/StateMapping.cpp b/GPU/GLES/StateMapping.cpp index a826854cc5..11313ae63c 100644 --- a/GPU/GLES/StateMapping.cpp +++ b/GPU/GLES/StateMapping.cpp @@ -805,9 +805,46 @@ void TransformDrawEngine::ApplyDrawState(int prim) { float zScale = getFloat24(gstate.viewportz1) / 65535.0f; float zOff = getFloat24(gstate.viewportz2) / 65535.0f; - float depthRangeMin = zOff - zScale; - float depthRangeMax = zOff + zScale; - glstate.depthRange.set(depthRangeMin, depthRangeMax); + + // 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) { + // if far depth range < 0 + if (zOff + zScale < 0.0f) { + // if perspective projection + if (gstate.projMatrix[11] < 0.0f) { + float depthMax = gstate.getDepthRangeMax() / 65535.0f; + float depthMin = gstate.getDepthRangeMin() / 65535.0f; + + float a = gstate.projMatrix[10]; + float b = gstate.projMatrix[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); + + gstate.projMatrix[10] = a; + gstate.projMatrix[14] = b; + + shaderManager_->DirtyUniform(DIRTY_PROJMATRIX); + + zOff = (zOff - zScale) / 2.0f; + zScale = -zOff; + + gstate.viewportz2 &= 0xFF000000; + gstate.viewportz2 |= toFloat24(zOff * 65535.0f); + + gstate.viewportz1 &= 0xFF000000; + gstate.viewportz1 |= toFloat24(zScale * 65535.0f); + } + } + } + + glstate.depthRange.set(zOff - zScale, zOff + zScale); } } 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" From 6fa0fe48a468d533c9b904bfd361cbd0103d7d68 Mon Sep 17 00:00:00 2001 From: level99procrastinator Date: Sun, 9 Nov 2014 01:26:24 +0800 Subject: [PATCH 2/3] Move the hack from GPU/GLES/StateMapping.cpp to GPU/GLES/ShaderManager.cpp --- GPU/GLES/ShaderManager.cpp | 33 ++++++++++++++++++++++++- GPU/GLES/StateMapping.cpp | 49 +++++--------------------------------- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/GPU/GLES/ShaderManager.cpp b/GPU/GLES/ShaderManager.cpp index 39a98e0aa0..5ea29d186d 100644 --- a/GPU/GLES/ShaderManager.cpp +++ b/GPU/GLES/ShaderManager.cpp @@ -305,7 +305,7 @@ static void SetColorUniform3Alpha255(int uniform, u32 color, u8 alpha) { (float)((color & 0xFF) >> 0), (float)((color & 0xFF00) >> 8), (float)((color & 0xFF0000) >> 16), - (float)alpha + (float)alpha }; glUniform4fv(uniform, 1, col); } @@ -388,6 +388,37 @@ 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); + + flippedMatrix[10] = a; + flippedMatrix[14] = b; + } + } + } + glUniformMatrix4fv(u_proj, 1, GL_FALSE, flippedMatrix); } if (dirty & DIRTY_PROJTHROUGHMATRIX) diff --git a/GPU/GLES/StateMapping.cpp b/GPU/GLES/StateMapping.cpp index 11313ae63c..b986792ef1 100644 --- a/GPU/GLES/StateMapping.cpp +++ b/GPU/GLES/StateMapping.cpp @@ -93,7 +93,7 @@ static const GLushort cullingMode[] = { }; static const GLushort ztests[] = { - GL_NEVER, GL_ALWAYS, GL_EQUAL, GL_NOTEQUAL, + GL_NEVER, GL_ALWAYS, GL_EQUAL, GL_NOTEQUAL, GL_LESS, GL_LEQUAL, GL_GREATER, GL_GEQUAL, }; @@ -732,7 +732,7 @@ void TransformDrawEngine::ApplyDrawState(int prim) { int scissorY2 = gstate.getScissorY2() + 1; // This is a bit of a hack as the render buffer isn't always that size - if (scissorX1 == 0 && scissorY1 == 0 + if (scissorX1 == 0 && scissorY1 == 0 && scissorX2 >= (int) gstate_c.curRTWidth && scissorY2 >= (int) gstate_c.curRTHeight) { glstate.scissorTest.disable(); @@ -765,7 +765,7 @@ void TransformDrawEngine::ApplyDrawState(int prim) { // No viewport transform here. Let's experiment with using region. glstate.viewport.set( - renderX + (0 + regionX1) * renderWidthFactor, + renderX + (0 + regionX1) * renderWidthFactor, renderY + (0 - regionY1) * renderHeightFactor, (regionX2 - regionX1) * renderWidthFactor, (regionY2 - regionY1) * renderHeightFactor); @@ -805,46 +805,9 @@ void TransformDrawEngine::ApplyDrawState(int prim) { float zScale = getFloat24(gstate.viewportz1) / 65535.0f; float zOff = getFloat24(gstate.viewportz2) / 65535.0f; - - // 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) { - // if far depth range < 0 - if (zOff + zScale < 0.0f) { - // if perspective projection - if (gstate.projMatrix[11] < 0.0f) { - float depthMax = gstate.getDepthRangeMax() / 65535.0f; - float depthMin = gstate.getDepthRangeMin() / 65535.0f; - - float a = gstate.projMatrix[10]; - float b = gstate.projMatrix[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); - - gstate.projMatrix[10] = a; - gstate.projMatrix[14] = b; - - shaderManager_->DirtyUniform(DIRTY_PROJMATRIX); - - zOff = (zOff - zScale) / 2.0f; - zScale = -zOff; - - gstate.viewportz2 &= 0xFF000000; - gstate.viewportz2 |= toFloat24(zOff * 65535.0f); - - gstate.viewportz1 &= 0xFF000000; - gstate.viewportz1 |= toFloat24(zScale * 65535.0f); - } - } - } - - glstate.depthRange.set(zOff - zScale, zOff + zScale); + float depthRangeMin = zOff - zScale; + float depthRangeMax = zOff + zScale; + glstate.depthRange.set(depthRangeMin, depthRangeMax); } } From 52d6f40ec8824e7347092c55a4a26d9e9609f1c0 Mon Sep 17 00:00:00 2001 From: level99procrastinator Date: Sun, 9 Nov 2014 20:34:23 +0800 Subject: [PATCH 3/3] Make sure no NaN goes into projection matrix. --- GPU/GLES/ShaderManager.cpp | 8 +++++--- GPU/GLES/StateMapping.cpp | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/GPU/GLES/ShaderManager.cpp b/GPU/GLES/ShaderManager.cpp index 5ea29d186d..e7365e1ae9 100644 --- a/GPU/GLES/ShaderManager.cpp +++ b/GPU/GLES/ShaderManager.cpp @@ -305,7 +305,7 @@ static void SetColorUniform3Alpha255(int uniform, u32 color, u8 alpha) { (float)((color & 0xFF) >> 0), (float)((color & 0xFF00) >> 8), (float)((color & 0xFF0000) >> 16), - (float)alpha + (float)alpha }; glUniform4fv(uniform, 1, col); } @@ -413,8 +413,10 @@ void LinkedShader::UpdateUniforms(u32 vertType) { a = (n + f) / (n - f); b = (2.0f * n * f) / (n - f); - flippedMatrix[10] = a; - flippedMatrix[14] = b; + if (!my_isnan(a) && !my_isnan(b)) { + flippedMatrix[10] = a; + flippedMatrix[14] = b; + } } } } diff --git a/GPU/GLES/StateMapping.cpp b/GPU/GLES/StateMapping.cpp index b986792ef1..a826854cc5 100644 --- a/GPU/GLES/StateMapping.cpp +++ b/GPU/GLES/StateMapping.cpp @@ -93,7 +93,7 @@ static const GLushort cullingMode[] = { }; static const GLushort ztests[] = { - GL_NEVER, GL_ALWAYS, GL_EQUAL, GL_NOTEQUAL, + GL_NEVER, GL_ALWAYS, GL_EQUAL, GL_NOTEQUAL, GL_LESS, GL_LEQUAL, GL_GREATER, GL_GEQUAL, }; @@ -732,7 +732,7 @@ void TransformDrawEngine::ApplyDrawState(int prim) { int scissorY2 = gstate.getScissorY2() + 1; // This is a bit of a hack as the render buffer isn't always that size - if (scissorX1 == 0 && scissorY1 == 0 + if (scissorX1 == 0 && scissorY1 == 0 && scissorX2 >= (int) gstate_c.curRTWidth && scissorY2 >= (int) gstate_c.curRTHeight) { glstate.scissorTest.disable(); @@ -765,7 +765,7 @@ void TransformDrawEngine::ApplyDrawState(int prim) { // No viewport transform here. Let's experiment with using region. glstate.viewport.set( - renderX + (0 + regionX1) * renderWidthFactor, + renderX + (0 + regionX1) * renderWidthFactor, renderY + (0 - regionY1) * renderHeightFactor, (regionX2 - regionX1) * renderWidthFactor, (regionY2 - regionY1) * renderHeightFactor);