core: route INTERLACING through the batch UBO

Commit 3 of the batch matrix variant-shrinking arc. Same shape
as the DITHERING routing in 3af8e02: lift the INTERLACING
compile-time #define into a runtime cbuffer scalar so the FS
source is invariant under the display-mode interlace state,
collapsing what was a 2x matrix axis into a single shader.

Smaller and tighter than DITHERING - INTERLACING has only one
#if site in the batch FS body (the y-LSB / displayed-field
discard guard), no register-bit-mid-frame considerations (the
display-mode interlace state only flips on display-mode changes,
which are rare), and the in-batch displayed-field LSB
(u_interlaced_displayed_field) was already cbuffer-routed for
the same shader site - the on/off bit is the only piece that
was still compile-time.

Cbuffer layout, before:

  Row 0: u_texture_window_and (8) + u_texture_window_or (8)
  Row 1: u_src_alpha_factor (4) + u_dst_alpha_factor (4) +
         u_interlaced_displayed_field (4) +
         u_set_mask_while_drawing (4)
  Row 2: u_resolution_scale (4) + u_true_color (4) +
         u_scaled_dithering (4) + u_dithering (4)
  Total: 48 bytes / 3 rows.

After:

  Row 0-2: unchanged.
  Row 3: u_interlacing (4) + u_pad0 (4) + u_pad1 (4) + u_pad2 (4)
  Total: 64 bytes / 4 rows.

The three u_padN slots are pre-allocated reserved cbuffer slots
for the next refactors in this arc: PGXP_DEPTH and UV_LIMITS
each need one slot, leaving one for whatever follows. HLSL
cbuffer rows are 16-byte aligned anyway, so growing the cbuffer
by these three uints is free in layout terms. Naming them
explicitly (rather than implicit-padding) matches the pre-3af8e02
"u_pad0 brings total to 16-byte cbuffer row" pattern and lets
the next commits do a 1-line rename (u_pad0 -> u_pgxp_depth, etc.)
the same way 3af8e02 did u_pad0 -> u_dithering.

Shadergen body:

  Was:
    #if INTERLACING
      if ((fixYCoord(uint(v_pos.y)) & 1u) == u_interlaced_displayed_field)
        discard;
    #endif

  Now:
    if (u_interlacing != 0u &&
        (fixYCoord(uint(v_pos.y)) & 1u) == u_interlaced_displayed_field)
      discard;

HLSL guarantees short-circuit evaluation of &&, so when
u_interlacing is 0 the fixYCoord call and the LSB compare don't
run - the runtime branch on u_interlacing collapses to a single
cbuffer-scalar test plus a no-op when off. u_interlacing is a
cbuffer field, every SIMD lane reads the same value, no
divergence cost on either arm.

Known trade: the discard keyword's presence in the FS source
disables early-Z writeback regardless of whether the discard
ever fires at runtime - the previous code had two FS variants
where the interlacing=0 variant was discard-free and could
participate in early-Z writeback. After this commit both arms
of the runtime branch live in the same DXBC, so the
interlacing=0 case loses that optimization. The cost is one
extra depth-write per fragment in the post-FS phase rather
than the pre-FS phase; depth TEST still runs early because
v_pos.z is interpolated by the rasterizer before the FS is
invoked, so depth REJECTION still works. For PSX content -
mostly back-to-front rendering with minimal depth-test win
to begin with - this is invisible at the FPS counter. If
some PGXP-heavy title shows a measurable regression we can
revisit by using [earlydepthstencil] on the FS (forces
early-Z at the cost of correctness if a draw both writes
depth and discards, which the batch FS doesn't), but the
default expectation is "no measurable change".

C++ side:

  BatchUBOData gains u_interlacing + u_pad0..2. Total struct
  size 48 -> 64 bytes. The struct's existing layout up to the
  u_dithering field is unchanged, so all existing field
  offsets stay put - the FlushRender upload picks up the new
  4-row layout via the existing sizeof-based push.

  SetDrawMode (where m_batch.interlacing is set per-batch):
  also push m_batch_ubo_data.u_interlacing with a dirty-bit
  guard, so the next FlushRender's upload sees the new
  value. u_interlaced_displayed_field stays in its existing
  conditional update block (only refreshed when interlacing
  is on, since its value is meaningless otherwise).

  The pattern matches u_dithering's: every per-batch state
  change site that affects the FS now does a cbuffer write +
  dirty mark in the same block. Same dirty-bit shape, same
  cost (one branch + one memcmp-equivalent on flip), same
  upload coalescing.

Visible behaviour: zero. The DXBC produced for the
(interlacing=on, displayed_field=X, *) configuration is now
identical to the previous (interlacing=on, *) variant's DXBC
post-conditional-evaluation; the (interlacing=off, *) DXBC
arms now share that same source with a runtime-skipped
discard. m_batch.interlacing still flips per-batch, the
FlushRender upload still publishes the new value, the
discard still fires when (y & 1u) matches the displayed
field. Output frames are pixel-identical to pre-commit.

Cost matrix delta (D3D12):

  Interlaced-vs-progressive display-mode flip mid-session:
    ~50 ms D3DCompile + ~10 ms PSO (or 1 ms disk cache hit
    if warm) -> ~4 byte UBO write.

  Field-LSB alternation per frame (already cbuffer-routed
  pre-3af8e02): unchanged, ~4 byte UBO write per frame.

The [interlacing] matrix dim still lives in
m_batch_pipelines<2, 5, 9, 4, 2, 7> and
m_batch_fragment_shader_blobs<2, 9, 4, 7>. Both slots compile
to identical DXBC after this commit. Follow-up D3D12 commit
will drop the dim:

  m_batch_pipelines:           5 040 -> 2 520 slots
  m_batch_fragment_shader_blobs: 504 ->   252 slots

Same pattern, same blast radius as the [dithering] dim drop
in commit f7f9e53 - the change is one fewer for loop in
precompile_sync and the background worker, one fewer
parameter on GetBatchFragmentShader / GetBatchPipeline /
DrawBatchVertices, one fewer dim on each of the four matrix
declarations.

D3D11 / OpenGL / Vulkan untouched. They still pass
`interlacing` to GenerateBatchFragmentShader and key their
batch matrices on it. Like with DITHERING, they produce
identical DXBC for the two values and waste matrix slots in
the same way. Per-backend cleanup commits can follow.

Test:
  - Build: D3D12 backend compiles, links, runs.
  - Boot any progressive-mode title (default render): the
    discard branch is skipped at runtime; output is
    indistinguishable from pre-commit. Exercises
    u_interlacing = 0 arm.
  - Boot an interlaced-mode title (any 480i game - FFVII
    battle screens, MGS cutscenes, Tekken intros, Resident
    Evil ending sequences): the discard alternates per
    field. Should look identical to pre-commit.
  - Toggle a display setting that flips IsInterlacedRenderingEnabled
    mid-frame (unusual but the FS path supports it): no
    shader recompile; the next FlushRender picks up the new
    u_interlacing.

Next in arc:
  - Drop the [interlacing] dim from D3D12 batch matrices
    (f7f9e53-shape cleanup commit).
  - Route PGXP_DEPTH for the VS (small body branch; uses
    u_pad0 slot).
  - Decide on PGXP_DEPTH for the FS (always-emit SV_Depth
    tradeoff to measure).
  - Route UV_LIMITS (chunkiest - per-vertex buffer layout
    change).
  - Pre-bake the resulting reduced FS matrix.
This commit is contained in:
LibretroAdmin 2026-05-18 23:15:52 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit eb42ffb055
3 changed files with 59 additions and 6 deletions

View file

@ -1398,6 +1398,17 @@ void GPU_HW::DispatchRenderCommand()
}
m_batch.interlacing = IsInterlacedRenderingEnabled();
// u_interlacing gates the discard in the FS body; the value is
// updated on every SetDrawMode so a display-mode flip mid-frame
// is picked up by the next FlushRender. The displayed-field LSB
// is only meaningful when interlacing is on (the FS discard short-
// circuits on u_interlacing == 0), but pushing it unconditionally
// when on keeps the existing per-frame field-flip handling -
// the C++ alternation cost is one branch + one cbuffer compare,
// negligible compared to the FlushRender that follows.
const uint32_t new_interlacing = m_batch.interlacing ? 1u : 0u;
m_batch_ubo_dirty |= (m_batch_ubo_data.u_interlacing != new_interlacing);
m_batch_ubo_data.u_interlacing = new_interlacing;
if (m_batch.interlacing)
{
const uint32_t displayed_field = GetActiveLineLSB();

View file

@ -137,6 +137,26 @@ protected:
// u_scaled_dithering's per-session shape, but the per-batch
// granularity matches the underlying register write rate.
uint32_t u_dithering;
// u_interlacing: per-batch interlaced-rendering gate (0 = off,
// 1 = on). Used to live as a compile-time INTERLACING macro
// gating a discard block; now a runtime short-circuit branch on
// the cbuffer field. u_interlaced_displayed_field above carries
// the active-field LSB and is updated independently in the same
// SetDrawMode block - the two fields are split so each can be
// updated on its own cadence (field LSB per frame, interlacing
// on/off on display-mode changes).
uint32_t u_interlacing;
// 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;
uint32_t u_pad1;
uint32_t u_pad2;
};
struct VRAMFillUBOData

View file

@ -84,7 +84,8 @@ void GPU_HW_ShaderGen::WriteBatchUniformBuffer(std::stringstream& ss)
{"uint2 u_texture_window_and", "uint2 u_texture_window_or", "float u_src_alpha_factor",
"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_scaled_dithering", "uint u_dithering",
"uint u_interlacing", "uint u_pad0", "uint u_pad1", "uint u_pad2"},
false);
// Alias the historical compile-time constants to their cbuffer-
@ -748,7 +749,19 @@ std::string GPU_HW_ShaderGen::GenerateBatchFragmentShader(GPU_HW::BatchRenderMod
// branches in the FS body. Toggling either is a single cbuffer
// write picked up on the next FlushRender, with no DXBC recompile
// and no PSO rebuild.
DefineMacro(ss, "INTERLACING", interlacing);
// INTERLACING used to live as a compile-time #define driving the
// y-LSB / u_interlaced_displayed_field discard guard in the FS
// body. It is now routed through the batch UBO (u_interlacing) and
// the former #if site below is a uniform-control-flow short-circuit
// branch on the cbuffer scalar. Same shape as the u_dithering
// routing in 3af8e02 - one runtime check instead of two compiled
// shader variants, and toggling display-mode interlace state is a
// single 4-byte cbuffer write. u_interlaced_displayed_field still
// carries the active-field LSB (0 or 1) when interlacing is on;
// u_interlacing gates whether the discard runs at all. The two
// are kept as separate fields so each can be updated independently
// - the field LSB changes per frame, the interlacing on/off bit
// changes on display-mode changes.
DefineMacro(ss, "TEXTURE_FILTERING", m_texture_filter != GPUTextureFilter::Nearest);
DefineMacro(ss, "UV_LIMITS", m_uv_limits);
DefineMacro(ss, "USE_DUAL_SOURCE", use_dual_source);
@ -892,10 +905,19 @@ float4 SampleFromVRAM(uint4 texpage, float2 coords)
float ialpha;
float oalpha;
#if INTERLACING
if ((fixYCoord(uint(v_pos.y)) & 1u) == u_interlaced_displayed_field)
discard;
#endif
// Was a compile-time #if INTERLACING guard. Now u_interlacing is
// a cbuffer scalar (0 = off, 1 = on) and the inner LSB compare
// gates on it. HLSL short-circuits &&, so the y-LSB / fixYCoord
// arithmetic only runs when interlacing is actually on. The
// discard keyword's presence in the source disables early-Z
// writeback for both the on and off cases - a known trade for
// cbuffer routing - but for PSX content this matters little:
// most geometry is back-to-front, the depth values come from
// VS interpolation so early depth TEST still works, and the
// 5090's FS throughput is many orders of magnitude in excess
// of what PSX rendering can saturate.
if (u_interlacing != 0u && (fixYCoord(uint(v_pos.y)) & 1u) == u_interlaced_displayed_field)
discard;
#if TEXTURED