Skip to content

Commit b230c9c

Browse files
committed
CPU/PGXP: Use GTE MAX_Z for Z normalization
Fixes clipping with depth buffer in some games, e.g. Final Fantasy VII battles.
1 parent 8cfd843 commit b230c9c

5 files changed

Lines changed: 25 additions & 16 deletions

File tree

src/core/cpu_pgxp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ bool CPU::PGXP::GetPreciseVertex(u32 addr, u32 value, int x, int y, int xOffs, i
613613
{
614614
*out_x = TruncateVertexPosition(vert->x) + static_cast<float>(xOffs);
615615
*out_y = TruncateVertexPosition(vert->y) + static_cast<float>(yOffs);
616-
*out_w = vert->z / 32768.0f;
616+
*out_w = vert->z / static_cast<float>(GTE::MAX_Z);
617617

618618
#ifdef LOG_LOOKUPS
619619
GL_INS_FMT("0x{:08X} {},{} => {},{} ({},{},{}) ({},{})", addr, x, y, *out_x, *out_y,
@@ -635,7 +635,7 @@ bool CPU::PGXP::GetPreciseVertex(u32 addr, u32 value, int x, int y, int xOffs, i
635635
{
636636
*out_x = TruncateVertexPosition(vert->x) + static_cast<float>(xOffs);
637637
*out_y = TruncateVertexPosition(vert->y) + static_cast<float>(yOffs);
638-
*out_w = vert->z / 32768.0f;
638+
*out_w = vert->z / static_cast<float>(GTE::MAX_Z);
639639

640640
if (IsWithinTolerance(*out_x, *out_y, x, y))
641641
return false;

src/core/gte.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ void GTE::RTPS(const s16 V[3], u8 shift, bool lm, bool last)
791791
}
792792
else
793793
{
794-
precise_x = static_cast<float>(x) / 4096.0f;
794+
precise_x = static_cast<float>(x) / (static_cast<float>(1 << shift));
795795
precise_y = static_cast<float>(y) / (static_cast<float>(1 << shift));
796-
precise_z = static_cast<float>(z) / (static_cast<float>(1 << shift));
796+
precise_z = static_cast<float>(z) / 4096.0f;
797797
}
798798

799799
precise_sz3 = precise_z;

src/core/gte_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ enum : u32
1111
{
1212
NUM_DATA_REGS = 32,
1313
NUM_CONTROL_REGS = 32,
14-
NUM_REGS = NUM_DATA_REGS + NUM_CONTROL_REGS
14+
NUM_REGS = NUM_DATA_REGS + NUM_CONTROL_REGS,
15+
MAX_Z = 65535,
1516
};
1617

1718
union FLAGS

src/core/settings.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "settings.h"
55
#include "achievements.h"
66
#include "controller.h"
7+
#include "gte.h"
78
#include "host.h"
89
#include "imgui_overlays.h"
910
#include "system.h"
@@ -90,7 +91,20 @@ float SettingInfo::FloatStepValue() const
9091
return step_value ? StringUtil::FromChars<float>(step_value).value_or(fallback_value) : fallback_value;
9192
}
9293

93-
GPUSettings::GPUSettings() = default;
94+
GPUSettings::GPUSettings()
95+
{
96+
SetPGXPDepthClearThreshold(DEFAULT_GPU_PGXP_DEPTH_THRESHOLD);
97+
}
98+
99+
float GPUSettings::GetPGXPDepthClearThreshold() const
100+
{
101+
return gpu_pgxp_depth_clear_threshold * static_cast<float>(GTE::MAX_Z);
102+
}
103+
104+
void GPUSettings::SetPGXPDepthClearThreshold(float value)
105+
{
106+
gpu_pgxp_depth_clear_threshold = value / static_cast<float>(GTE::MAX_Z);
107+
}
94108

95109
#ifdef DYNAMIC_HOST_PAGE_SIZE
96110
// See note in settings.h - 16K ends up faster with LUT because of nearby code/data.

src/core/settings.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ struct GPUSettings
150150
float display_osd_scale = DEFAULT_OSD_SCALE;
151151
float display_osd_margin = 0.0f;
152152
float gpu_pgxp_tolerance = -1.0f;
153-
float gpu_pgxp_depth_clear_threshold = DEFAULT_GPU_PGXP_DEPTH_THRESHOLD / GPU_PGXP_DEPTH_THRESHOLD_SCALE;
153+
float gpu_pgxp_depth_clear_threshold = 0.0f;
154154

155155
// texture replacements
156156
struct TextureReplacementSettings
@@ -209,6 +209,8 @@ struct GPUSettings
209209
std::string overlay_image_path;
210210

211211
float GetDisplayAspectRatioValue() const;
212+
float GetPGXPDepthClearThreshold() const;
213+
void SetPGXPDepthClearThreshold(float value);
212214

213215
ALWAYS_INLINE bool IsUsingSoftwareRenderer() const { return (gpu_renderer == GPURenderer::Software); }
214216
ALWAYS_INLINE bool IsUsingAccurateBlending() const { return (gpu_accurate_blending && !gpu_true_color); }
@@ -220,14 +222,6 @@ struct GPUSettings
220222

221223
ALWAYS_INLINE bool UsingPGXPCPUMode() const { return gpu_pgxp_enable && gpu_pgxp_cpu; }
222224
ALWAYS_INLINE bool UsingPGXPDepthBuffer() const { return gpu_pgxp_enable && gpu_pgxp_depth_buffer; }
223-
ALWAYS_INLINE float GetPGXPDepthClearThreshold() const
224-
{
225-
return gpu_pgxp_depth_clear_threshold * GPU_PGXP_DEPTH_THRESHOLD_SCALE;
226-
}
227-
ALWAYS_INLINE void SetPGXPDepthClearThreshold(float value)
228-
{
229-
gpu_pgxp_depth_clear_threshold = value / GPU_PGXP_DEPTH_THRESHOLD_SCALE;
230-
}
231225

232226
static constexpr GPURenderer DEFAULT_GPU_RENDERER = GPURenderer::Automatic;
233227
static constexpr GPUTextureFilter DEFAULT_GPU_TEXTURE_FILTER = GPUTextureFilter::Nearest;
@@ -236,7 +230,7 @@ struct GPUSettings
236230
static constexpr GPUWireframeMode DEFAULT_GPU_WIREFRAME_MODE = GPUWireframeMode::Disabled;
237231
static constexpr GPUDumpCompressionMode DEFAULT_GPU_DUMP_COMPRESSION_MODE = GPUDumpCompressionMode::ZstDefault;
238232
static constexpr float DEFAULT_GPU_PGXP_DEPTH_THRESHOLD = 300.0f;
239-
static constexpr float GPU_PGXP_DEPTH_THRESHOLD_SCALE = 4096.0f;
233+
static constexpr float GPU_PGXP_DEPTH_THRESHOLD_SCALE = 65536.0f;
240234

241235
static constexpr DisplayDeinterlacingMode DEFAULT_DISPLAY_DEINTERLACING_MODE = DisplayDeinterlacingMode::Progressive;
242236
static constexpr DisplayCropMode DEFAULT_DISPLAY_CROP_MODE = DisplayCropMode::Overscan;

0 commit comments

Comments
 (0)