Skip to content

Commit

Permalink
Bring back the depth range hack.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Feb 7, 2016
1 parent f1c06d2 commit 0e85225
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions Core/Compatibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Core/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
struct CompatFlags {
bool VertexDepthRounding;
bool PixelDepthRounding;
bool DepthRangeHack;
};

class IniFile;
Expand Down
5 changes: 5 additions & 0 deletions GPU/GLES/GLES_GPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions GPU/GLES/ShaderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions GPU/GPUState.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 14 additions & 0 deletions assets/compat.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0e85225

Please sign in to comment.