Skip to content

Commit

Permalink
Merge pull request #18535 from hrydgard/fix-zig-sample
Browse files Browse the repository at this point in the history
Eliminate inf values resulting from depth range computation.
  • Loading branch information
hrydgard authored Dec 12, 2023
2 parents 8d05826 + 6d3c34e commit 9f35f90
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Common/Math/math_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ inline bool my_isnanorinf(float f) {
return ((f2u.u & 0x7F800000) == 0x7F800000);
}

inline float InfToZero(float f) {
return my_isinf(f) ? 0.0f : f;
}

inline int is_even(float d) {
float int_part;
modff(d / 2.0f, &int_part);
Expand Down
5 changes: 3 additions & 2 deletions GPU/Common/ShaderUniforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipView
float vpZCenter = gstate.getViewportZCenter();

// These are just the reverse of the formulas in GPUStateUtils.
float halfActualZRange = gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f;
float inverseDepthScale = gstate_c.vpDepthScale != 0.0f ? 1.0f / gstate_c.vpDepthScale : 0.0f;
float halfActualZRange = InfToZero(gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f);
float inverseDepthScale = InfToZero(gstate_c.vpDepthScale != 0.0f ? 1.0f / gstate_c.vpDepthScale : 0.0f);

float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange;
float viewZScale = halfActualZRange * 2.0f;
float viewZCenter = minz;
Expand Down
4 changes: 2 additions & 2 deletions GPU/Directx9/ShaderManagerDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,11 @@ void ShaderManagerDX9::VSUpdateUniforms(u64 dirtyUniforms) {
float vpZCenter = gstate.getViewportZCenter();

// These are just the reverse of the formulas in GPUStateUtils.
float halfActualZRange = gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f;
float halfActualZRange = InfToZero(gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f);
float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange;
float viewZScale = halfActualZRange * 2.0f;
float viewZCenter = minz;
float reverseScale = gstate_c.vpDepthScale != 0.0f ? 2.0f * (1.0f / gstate_c.vpDepthScale) : 0.0f;
float reverseScale = InfToZero(gstate_c.vpDepthScale != 0.0f ? 2.0f * (1.0f / gstate_c.vpDepthScale) : 0.0f);
float reverseTranslate = gstate_c.vpZOffset * 0.5f + 0.5f;

float data[4] = { viewZScale, viewZCenter, reverseTranslate, reverseScale };
Expand Down
4 changes: 2 additions & 2 deletions GPU/GLES/ShaderManagerGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ void LinkedShader::UpdateUniforms(const ShaderID &vsid, bool useBufferedRenderin
float vpZCenter = gstate.getViewportZCenter();

// These are just the reverse of the formulas in GPUStateUtils.
float halfActualZRange = gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f;
float inverseDepthScale = gstate_c.vpDepthScale != 0.0f ? 1.0f / gstate_c.vpDepthScale : 0.0f;
float halfActualZRange = InfToZero(gstate_c.vpDepthScale != 0.0f ? vpZScale / gstate_c.vpDepthScale : 0.0f);
float inverseDepthScale = InfToZero(gstate_c.vpDepthScale != 0.0f ? 1.0f / gstate_c.vpDepthScale : 0.0f);
float minz = -((gstate_c.vpZOffset * halfActualZRange) - vpZCenter) - halfActualZRange;
float viewZScale = halfActualZRange;
float viewZCenter = minz + halfActualZRange;
Expand Down

0 comments on commit 9f35f90

Please sign in to comment.