diff --git a/src/core/gpu_hw.cpp b/src/core/gpu_hw.cpp index a6d9b6d9..50ed0b75 100644 --- a/src/core/gpu_hw.cpp +++ b/src/core/gpu_hw.cpp @@ -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(); diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index cc191820..44733710 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -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 diff --git a/src/core/gpu_hw_shadergen.cpp b/src/core/gpu_hw_shadergen.cpp index ceafadf1..418fd302 100644 --- a/src/core/gpu_hw_shadergen.cpp +++ b/src/core/gpu_hw_shadergen.cpp @@ -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