core: route PGXP_DEPTH through the batch UBO for the VS

Commit 5 of the batch matrix variant-shrinking arc. Routes
PGXP_DEPTH out of the batch VS source via the u_pgxp_depth
cbuffer slot reserved by eb42ffb's row-3 expansion. The VS
body's pos_z source selection becomes a uniform-control-flow
ternary on the cbuffer scalar; the VS source is now invariant
under the m_pgxp_depth member.

This is a partial routing - only the VS becomes pgxp-
independent. The FS retains m_pgxp_depth as a captured
shadergen-state field because it gates the SV_Depth
declaration in the FS entry-point signature (via
DeclareFragmentEntryPoint's `depth_output` parameter). A
signature change cannot be cbuffer-routed without always-
emitting SV_Depth from every batch FS, which disables early-Z
writeback for both arms of the PGXP toggle (vs the current
state where PGXP-off can early-Z-writeback). Routing the FS
side is a future commit pending perf measurement; the
expected impact is small for PSX content (back-to-front
rendering, minimal early-Z win to begin with) but worth
checking before committing to the trade.

The PSO depth-test state also still depends on m_pgxp_depth:

  GetBatchPipeline at gpbuilder.SetDepthState(...) picks
  D3D12_COMPARISON_FUNC_LESS_EQUAL when m_pgxp_depth_buffer
  is on, D3D12_COMPARISON_FUNC_GREATER_EQUAL when off.

That's a PSO state, not a shader concern. The matrix dim
collapse for PGXP would need a depth_test dim shape change
(currently 2 = {disabled, enabled}; would need 3 =
{disabled, enabled-greater-equal, enabled-less-equal}, or
two separate dims). A future commit can do that; for now
PGXP still triggers a full PSO rebuild because the PSO
state differs.

Net effect of this commit:

  PGXP toggle pre-commit:
    - VS source string changes -> VS matrix invalidated
    - FS source string changes -> FS matrix invalidated
    - PSO depth state changes -> PSO matrix invalidated

  PGXP toggle post-commit:
    - VS source string UNCHANGED -> VS matrix preserved
    - FS source string changes -> FS matrix invalidated (still)
    - PSO depth state changes -> PSO matrix invalidated (still)
    - u_pgxp_depth pushed to cbuffer -> VS reads new value on
      next FlushRender

VS rebuild is small (2 shaders, ~100ms cold). The dominant
cost on a PGXP toggle is still the FS + PSO rebuild. This
commit doesn't eliminate that cost, but it sets up the
cbuffer slot and the routing pattern; future commits that
route the FS side and the PSO depth state will plug into
the same u_pgxp_depth field and the same SetDrawMode
maintenance.

Cbuffer layout: u_pad0 (the first reserved slot in the row-3
area eb42ffb added) is renamed to u_pgxp_depth. No size
change - the cbuffer remains 64 bytes / 4 rows. The two
remaining reserved slots (u_pad1, u_pad2) carry forward for
the UV_LIMITS routing and one more axis.

Shadergen VS body:

  Was:
    #if PGXP_DEPTH
      float pos_z = a_pos.w;
      float pos_w = a_pos.w;
    #else
      float pos_z = a_pos.z;
      float pos_w = a_pos.w;
    #endif

  Now:
    float pos_z = (u_pgxp_depth != 0u) ? a_pos.w : a_pos.z;
    float pos_w = a_pos.w;

HLSL compiles a uniform ternary on a cbuffer scalar to a
single conditional-move instruction; both branches read from
the same input register, so there's no extra memory traffic.

OpenGL post-processing branch (pos_y += POS_EPSILON;
pos_z = (pos_z * 2.0) - 1.0) was previously inside one
#if API_OPENGL || API_OPENGL_ES block straddling the pgxp
gate. Split into two API_OPENGL blocks bracketing the new
pos_z assignment so the y-epsilon fixup runs before pos_z
is assigned and the depth-range fixup runs after. Same
generated code on OpenGL backends, no behavioural diff.

C++ side:

  BatchUBOData: u_pad0 -> u_pgxp_depth. Struct size unchanged
  at 64 bytes. The two remaining reserved slots become u_pad1
  and u_pad2 (numbering preserved at u_pad1/u_pad2 rather
  than renumbered to u_pad0/u_pad1 so the existing reserved
  area is bisect-friendly).

  Initialize / Reset: seed u_pgxp_depth alongside
  u_resolution_scale / u_true_color / u_scaled_dithering.
  All four are per-session fields that need explicit
  re-seeding after Reset's zero-init - same shape as the
  u_resolution_scale invariant that the original cbuffer
  refactor commit established.

  UpdateHWSettings: inside the existing
  m_pgxp_depth_buffer-flip block (line 340-346 pre-commit),
  also push the new value into m_batch_ubo_data.u_pgxp_depth.
  The dirty bit is already set in the bulk per-session push
  upstream at line 336, so the next FlushRender's upload
  picks up the new field. No new dirty tracking required.

Visible behaviour: zero. The VS produces identical pos_z
output (a_pos.w when PGXP is on, a_pos.z when off) just via
a runtime select rather than a compile-time pick. FS
behaviour and PSO depth state are unchanged - those still
follow m_pgxp_depth_buffer through shader_source_changed and
the PSO depth-comparison-func selection.

Cost matrix delta (D3D12):

  PGXP toggle in UpdateHWSettings:
    Pre-commit: 2 VS compiles + 252 FS compiles + 2520 PSO
                compiles -> ~tens of seconds cold.
    Post-commit: 0 VS compiles + 252 FS compiles + 2520
                 PSO compiles -> still tens of seconds.
    Saved: 2 VS compiles (~100ms cold). Symbolic on its
    own, but it unblocks future FS / PSO routing commits
    that consume the same cbuffer slot.

D3D11 / OpenGL / Vulkan: untouched on this commit. Their
GenerateBatchVertexShader call paths see the same source
output - the macro removal happens unconditionally in the
shadergen, so all backends benefit equally (no VS recompile
on PGXP toggle for any of them). Per-backend additional
cleanup (if they had separate PGXP-VS matrix dims) is not
required - the batch VS matrix has only [textured] as its
dim and was never keyed on PGXP at the matrix level.

Test:
  - Build: compile / link D3D12 backend.
  - Boot any PSX title with PGXP off (default): the
    runtime ternary picks a_pos.z, same as the legacy path.
    Geometry renders identically.
  - Toggle PGXP CPU mode on mid-session (Enhancements
    settings menu): the FS / PSO matrix rebuilds (no
    change from current behaviour) but the VS is preserved
    across the flip. The new u_pgxp_depth value rides the
    next FlushRender's UBO upload; geometry now uses
    a_pos.w. Visual output identical to pre-commit PGXP-on
    state.
  - Toggle PGXP off again: same path in reverse.
  - PGXP-heavy title (Crash Bandicoot, MGS, Tekken 3 -
    titles with PGXP depth-buffer mode in production
    use): PGXP-on render is identical to pre-commit.

Next:
  - Route UV_LIMITS through the batch UBO (chunkiest of
    the planned refactors: needs per-vertex buffer layout
    change to always emit a_uv_limits / v_uv_limits with
    junk values when not in use, plus VS / FS body branches
    on u_uv_limits).
  - Or: decide on PGXP_DEPTH for the FS (always-emit
    SV_Depth perf trade). Worth a perf measurement before
    committing.
This commit is contained in:
LibretroAdmin 2026-05-19 00:16:33 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 49c0f829fc
3 changed files with 57 additions and 17 deletions

View file

@ -95,6 +95,7 @@ bool GPU_HW::Initialize(HostDisplay* host_display)
m_batch_ubo_data.u_resolution_scale = m_resolution_scale;
m_batch_ubo_data.u_true_color = m_true_color ? 1u : 0u;
m_batch_ubo_data.u_scaled_dithering = m_scaled_dithering ? 1u : 0u;
m_batch_ubo_data.u_pgxp_depth = m_pgxp_depth_buffer ? 1u : 0u;
UpdateSoftwareRenderer(false);
@ -135,6 +136,7 @@ void GPU_HW::Reset(bool clear_vram)
m_batch_ubo_data.u_resolution_scale = m_resolution_scale;
m_batch_ubo_data.u_true_color = m_true_color ? 1u : 0u;
m_batch_ubo_data.u_scaled_dithering = m_scaled_dithering ? 1u : 0u;
m_batch_ubo_data.u_pgxp_depth = m_pgxp_depth_buffer ? 1u : 0u;
m_batch_ubo_dirty = true;
m_current_depth = 1;
@ -343,6 +345,16 @@ void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed,
m_batch.use_depth_buffer = false;
if (m_pgxp_depth_buffer)
ClearDepthBuffer();
// The VS reads u_pgxp_depth from the batch UBO to select between
// a_pos.z and a_pos.w as the depth source. The dirty bit is
// already set in the bulk push above (line 336), so this update
// rides the existing upload at the next FlushRender. The FS
// still recompiles on this flip because PGXP_DEPTH gates
// SV_Depth in the FS entry-point signature - shader_source_changed
// upstream of this block has already triggered the rebuild path,
// and the new shadergen state will pick up the fresh
// m_pgxp_depth value when it generates the new FS source.
m_batch_ubo_data.u_pgxp_depth = m_pgxp_depth_buffer ? 1u : 0u;
}
UpdateSoftwareRenderer(true);

View file

@ -146,15 +146,25 @@ protected:
// updated on its own cadence (field LSB per frame, interlacing
// on/off on display-mode changes).
uint32_t u_interlacing;
// u_pgxp_depth: per-session PGXP-depth-buffer mode toggle (0 = off,
// 1 = on). Used to live as a compile-time PGXP_DEPTH macro in the
// batch VS source, selecting between a_pos.z (legacy mask-Z) and
// a_pos.w (PGXP-computed Z) as the depth source. Now a runtime
// ternary on the cbuffer field. The FS-side PGXP_DEPTH usage
// (gating SV_Depth declaration) is NOT yet routed - it still
// affects the FS entry-point signature - so toggling
// m_pgxp_depth_buffer mid-session still triggers a FS / PSO
// matrix rebuild via shader_source_changed. VS, however, stays
// bound across the flip.
uint32_t u_pgxp_depth;
// Reserved cbuffer slots. The batch UBO has been progressively
// growing as compile-time #defines move to runtime cbuffer
// branches (RESOLUTION_SCALE, TRUE_COLOR, DITHERING_SCALED,
// DITHERING, INTERLACING). Pre-allocating the remaining slots
// in the 4th 16-byte row matches the layout HLSL would have
// padded to anyway, and lets follow-up commits route additional
// axes (PGXP_DEPTH, UV_LIMITS, etc.) into a named slot without
// changing the cbuffer's external size or alignment.
uint32_t u_pad0;
// DITHERING, INTERLACING, PGXP_DEPTH for VS). Pre-allocating
// the remaining slots in the 4th 16-byte row matches the layout
// HLSL would have padded to anyway, and lets follow-up commits
// route additional axes (UV_LIMITS, etc.) into a named slot
// without changing the cbuffer's external size or alignment.
uint32_t u_pad1;
uint32_t u_pad2;
};

View file

@ -85,7 +85,7 @@ void GPU_HW_ShaderGen::WriteBatchUniformBuffer(std::stringstream& ss)
"float u_dst_alpha_factor", "uint u_interlaced_displayed_field",
"bool u_set_mask_while_drawing", "uint u_resolution_scale", "uint u_true_color",
"uint u_scaled_dithering", "uint u_dithering",
"uint u_interlacing", "uint u_pad0", "uint u_pad1", "uint u_pad2"},
"uint u_interlacing", "uint u_pgxp_depth", "uint u_pad1", "uint u_pad2"},
false);
// Alias the historical compile-time constants to their cbuffer-
@ -120,7 +120,22 @@ std::string GPU_HW_ShaderGen::GenerateBatchVertexShader(bool textured)
WriteHeader(ss);
DefineMacro(ss, "TEXTURED", textured);
DefineMacro(ss, "UV_LIMITS", m_uv_limits);
DefineMacro(ss, "PGXP_DEPTH", m_pgxp_depth);
// PGXP_DEPTH used to live as a compile-time #define driving the
// pos_z source selection in the VS body. It is now routed through
// the batch UBO (u_pgxp_depth) and the former #if/#else block is
// a uniform-control-flow ternary. Toggling PGXP-depth mode mid-
// session is a single 4-byte cbuffer write picked up on the next
// FlushRender on the VS side - the VS source is now invariant
// under the m_pgxp_depth member.
// The FS still captures m_pgxp_depth: it gates SV_Depth declaration
// in the FS entry-point signature (DeclareFragmentEntryPoint's
// `depth_output` parameter), which is a signature change that
// can't be cbuffer-routed without always-emitting SV_Depth and
// taking the early-Z hit unconditionally. Routing the FS side is
// a future commit pending perf measurement. Until then, PGXP
// toggle still triggers a FS / PSO matrix rebuild via the
// shader_source_changed path - it just doesn't trigger a VS
// rebuild anymore.
// Order matters: WriteBatchUniformBuffer must emit before
// WriteCommonFunctions so the #define aliases for RESOLUTION_SCALE
@ -181,18 +196,21 @@ std::string GPU_HW_ShaderGen::GenerateBatchVertexShader(bool textured)
float pos_x = ((a_pos.x + vertex_offset) / 512.0) - 1.0;
float pos_y = ((a_pos.y + vertex_offset) / -256.0) + 1.0;
#if PGXP_DEPTH
// Ignore mask Z when using PGXP depth.
float pos_z = a_pos.w;
float pos_w = a_pos.w;
#else
float pos_z = a_pos.z;
float pos_w = a_pos.w;
#endif
#if API_OPENGL || API_OPENGL_ES
pos_y += POS_EPSILON;
#endif
// PGXP-depth mode (u_pgxp_depth != 0) ignores mask Z and uses
// a_pos.w as the depth source; the legacy path reads a_pos.z.
// u_pgxp_depth is a cbuffer scalar so this is a uniform-control-
// flow select - the driver collapses it to a single conditional
// move at compile time. Was a compile-time #if PGXP_DEPTH /
// #else / #endif gate (m_pgxp_depth captured at shadergen ctor);
// now a runtime branch on the cbuffer field.
float pos_z = (u_pgxp_depth != 0u) ? a_pos.w : a_pos.z;
float pos_w = a_pos.w;
#if API_OPENGL || API_OPENGL_ES
// 0..1 to -1..1 depth range.
pos_z = (pos_z * 2.0) - 1.0;
#endif