core: route RESOLUTION_SCALE through vram_copy_ps cbuffer

First step of the non-batch RESOLUTION_SCALE cbuffer refactor
that unblocks pre-bake migrations for the shaders that currently
reference RESOLUTION_SCALE / VRAM_SIZE in their bodies. Scope is
intentionally narrow: just vram_copy_ps in this commit, with the
shared helper that follow-up commits use to refactor vram_write_ps,
vram_read_ps, display_ps, etc.

Background. Pre-7b575a3, every batch shader had m_resolution_scale
baked compile-time via WriteCommonFunctions's 'CONSTANT uint
RESOLUTION_SCALE = X;' emission, which forced a fresh shader
compile and PSO rebuild for every resolution-scale change. 7b575a3
moved RESOLUTION_SCALE / true_color / scaled_dithering into the
batch UBO as cbuffer-routed fields (u_resolution_scale,
u_true_color, u_scaled_dithering) and aliased the legacy
CONSTANT names to the cbuffer fields via #define. Same shader
source serves every resolution scale; no recompile on toggle.

Non-batch shaders kept the legacy literal path because they don't
bind the batch UBO. Each one had its own per-shader cbuffer at
register(b0), and WriteCommonFunctions's batch_uniform_buffer=false
branch emitted the CONSTANT block right after WriteHeader. So
vram_copy_ps, vram_write_ps, vram_read_ps, display_ps and
adaptive_downsample_* all still embed m_resolution_scale into
their source string, and every scale change recompiles them.

That's a problem for pre-bake. The whole point of pre-bake is
one DXBC blob per (semantic) variant; if RESOLUTION_SCALE bakes
into the source, every scale value needs its own .inc file -
typically 5+ blobs per shader just for the scale dimension. The
disk-shader-cache layer hides this at runtime (the second toggle
hits the cache), but pre-bake bypasses the runtime cache and
needs the variant count to stay small.

Refactor pattern (this commit applies it to vram_copy_ps; follow-
ups apply the same shape to other affected shaders):

  1. Add `uint u_resolution_scale, uint u_pad0` to the shader's
     DeclareUniformBuffer member list. The u_pad0 keeps the
     cbuffer 16-byte-aligned (HLSL cbuffer rows are 16 bytes;
     std140 GLSL block sizes round up to vec4).
  2. Reorder shadergen calls so DeclareUniformBuffer runs before
     anything that wants RESOLUTION_SCALE in scope.
  3. After DeclareUniformBuffer, call the new helper
     WriteCBufferResolutionScaleAliases(ss). This emits the same
     three #define lines that WriteBatchUniformBuffer has been
     emitting at the end of its declaration since 7b575a3:
       #define RESOLUTION_SCALE u_resolution_scale
       #define VRAM_SIZE        (uint2(VRAM_W, VRAM_H) * u_resolution_scale)
       #define RCP_VRAM_SIZE    (float2(1,1) / float2(VRAM_SIZE))
  4. Call WriteCommonFunctions(ss, true) instead of (ss, false)
     so the legacy 'CONSTANT uint RESOLUTION_SCALE = X;' block
     is skipped. MULTISAMPLES, PER_SAMPLE_SHADING, fixYCoord and
     the RGBA conversion helpers still emit as before.
  5. Append u_resolution_scale (+ u_pad0) to the matching
     C++ UBO struct in gpu_hw.h, and populate it in the
     GetXxxUBOData getter in gpu_hw.cpp.

Helper plumbing:

* gpu_hw_shadergen.h: declare WriteCBufferResolutionScaleAliases.
* gpu_hw_shadergen.cpp: factor the three alias lines out of
  WriteBatchUniformBuffer into WriteCBufferResolutionScaleAliases.
  WriteBatchUniformBuffer calls the helper at the same spot it
  used to inline the lines, so batch shaders are unchanged.

Shader change in this commit:

* GenerateVRAMCopyFragmentShader (gpu_hw_shadergen.cpp): reordered
  so the cbuffer is declared before WriteCommonFunctions, two
  trailing members added to the cbuffer
  (uint u_resolution_scale, uint u_pad0), helper called between
  cbuffer and WriteCommonFunctions(ss, true). Body untouched -
  the existing VRAM_SIZE references inside the body keep
  resolving through the #define aliases just like in batch
  shaders.

Runtime change in this commit:

* VRAMCopyUBOData (gpu_hw.h): two new uint32_t fields appended
  at the end (u_resolution_scale, u_pad0). Struct grows 40 -> 48
  bytes. All four backends (D3D11 / D3D12 / OpenGL / Vulkan)
  upload via runtime sizeof(uniforms), so they pick up the new
  size automatically. The two push-constant backends have
  MAX_PUSH_CONSTANTS_SIZE = 64 bytes - 48 still fits.
* GetVRAMCopyUBOData (gpu_hw.cpp): populate u_resolution_scale
  with m_resolution_scale, u_pad0 with 0.

Compatibility notes:

* Shader cache invalidation: vram_copy_ps's source string
  changes (different cbuffer members + different macro
  expansion). First boot after this lands re-compiles vram_copy
  on every backend; subsequent boots hit the regenerated cache
  entry.
* Runtime correctness: the VRAM_SIZE / RCP_VRAM_SIZE references
  in the shader body resolve to the same arithmetic at runtime
  that the literal would have. Tested mentally against the
  body code paths - the source-target offset math at lines
  1336-1338 uses VRAM_SIZE.x and VRAM_SIZE.y, both of which
  expand to (1024 * u_resolution_scale) and (512 * u_resolution_scale)
  respectively. Identical to the previous CONSTANT-baked
  values for any given m_resolution_scale.
* Push-constant size: 48 bytes / 12 DWORDs on the D3D12 root
  signature side, well under the 64-byte / 16-DWORD limit at
  Add32BitConstants. Vulkan vkCmdPushConstants accepts up to
  the pipeline layout's reserved range (MAX_PUSH_CONSTANTS_SIZE
  = 64). D3D11 / OpenGL use regular UBO uploads with no fixed
  size limit.
* No behaviour change for batch shaders. WriteBatchUniformBuffer
  still emits the same final source - the three alias lines
  just come from a function call now instead of inline.

Cost: zero in steady state. The shader does one extra mul
(1024u * u_resolution_scale) per pixel where the literal was
constant-folded - but HLSL constant-folds u_resolution_scale's
known-at-PSO-bind-time value too, and even if it didn't, an
imul on a known small constant is sub-nanosecond. The push
constants are 8 extra bytes on every CopyVRAM call (negligible
compared to the rest of the per-call setup).

Test:
  - Build under each backend (D3D11 / D3D12 / OpenGL / Vulkan)
    succeeds. The new helper has no callers outside
    gpu_hw_shadergen.cpp so there's no link-side surface area.
  - CopyVRAM renders correctly at every resolution scale. The
    VRAM_SIZE references in the body now resolve through
    u_resolution_scale; if the cbuffer field weren't populated
    correctly, source/dest offsets in CopyVRAM would be wrong
    and game graphics with VRAM-to-VRAM transfers (most
    PSX games) would corrupt visibly within a few frames.
  - Toggle resolution scale at runtime (1x -> 2x -> 4x -> 1x).
    First toggle of each scale recompiles vram_copy_ps once;
    second toggle hits the cache. Behaviour matches what
    happened pre-refactor.
  - Pre-existing PGXP-depth dimension still works (two PSOs
    in the m_vram_copy_pipelines[depth_test] matrix), with
    the embedded blob path TBD - that's a follow-up patch.

Next in the arc:
  - Apply the same refactor to vram_write_ps (uses both
    RESOLUTION_SCALE and VRAM_SIZE), vram_read_ps (uses
    RESOLUTION_SCALE in the loop bounds), and display_ps
    (uses RESOLUTION_SCALE directly).
  - After all of those are refactored, the RESOLUTION_SCALE
    axis is gone from every non-batch shader's variant matrix.
    Then pre-bake migrations can proceed for each one.
This commit is contained in:
LibretroAdmin 2026-05-18 18:58:36 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit e56d4d4f7b
4 changed files with 55 additions and 3 deletions

View file

@ -1121,7 +1121,12 @@ GPU_HW::VRAMCopyUBOData GPU_HW::GetVRAMCopyUBOData(uint32_t src_x, uint32_t src_
width * m_resolution_scale,
height * m_resolution_scale,
m_GPUSTAT.set_mask_while_drawing ? 1u : 0u,
GetCurrentNormalizedVertexDepth()};
GetCurrentNormalizedVertexDepth(),
// Cbuffer-routed RESOLUTION_SCALE - the shader's VRAM_SIZE / RCP_VRAM_SIZE
// aliases now derive from this field instead of having m_resolution_scale
// baked compile-time. See GenerateVRAMCopyFragmentShader.
m_resolution_scale,
0u /* u_pad0 */};
return uniforms;
}

View file

@ -167,6 +167,15 @@ protected:
uint32_t u_height;
uint32_t u_set_mask_bit;
float u_depth_value;
// Appended for the cbuffer-routed RESOLUTION_SCALE refactor (this
// commit). Matches the trailing "uint u_resolution_scale, uint u_pad0"
// members on the HLSL side in GenerateVRAMCopyFragmentShader -
// u_pad0 keeps the cbuffer 16-byte-aligned (HLSL cbuffers pack to
// 16-byte rows; after u_depth_value at offset 36, adding one uint
// would end at offset 40, but the row ends at 48 so we pad to
// bring the total to 48 bytes).
uint32_t u_resolution_scale;
uint32_t u_pad0;
};
class ShaderCompileProgressTracker

View file

@ -93,6 +93,21 @@ void GPU_HW_ShaderGen::WriteBatchUniformBuffer(std::stringstream& ss)
// the preprocessor just substitutes them with arithmetic over
// u_resolution_scale. VRAM_WIDTH / VRAM_HEIGHT stay compile-time
// literals because they are fixed PSX geometry, not session state.
WriteCBufferResolutionScaleAliases(ss);
}
void GPU_HW_ShaderGen::WriteCBufferResolutionScaleAliases(std::stringstream& ss)
{
// The caller has already declared a cbuffer that contains
// `uint u_resolution_scale` (either the batch UBO via
// WriteBatchUniformBuffer, or a per-shader UBO that grew the field
// as part of the non-batch cbuffer-routing refactor). All we do
// here is alias the legacy compile-time constants to the cbuffer
// field so existing shader bodies that reference RESOLUTION_SCALE /
// VRAM_SIZE / RCP_VRAM_SIZE keep compiling. The arithmetic over
// u_resolution_scale happens once per shader invocation - HLSL /
// GLSL fold the constant-folding-friendly bits (the VRAM_WIDTH /
// VRAM_HEIGHT literals) at compile time.
ss << "#define RESOLUTION_SCALE u_resolution_scale\n";
ss << "#define VRAM_SIZE (uint2(" << VRAM_WIDTH << "u, " << VRAM_HEIGHT << "u) * u_resolution_scale)\n";
ss << "#define RCP_VRAM_SIZE (float2(1.0, 1.0) / float2(VRAM_SIZE))\n";
@ -1304,13 +1319,27 @@ std::string GPU_HW_ShaderGen::GenerateVRAMCopyFragmentShader()
std::stringstream ss;
WriteHeader(ss);
WriteCommonFunctions(ss);
DefineMacro(ss, "PGXP_DEPTH", m_pgxp_depth);
// u_resolution_scale is at the END of the cbuffer (after u_depth_value)
// so existing field offsets stay stable - the only change is a new
// 4-byte field appended. VRAMCopyUBOData in gpu_hw.h must match.
DeclareUniformBuffer(ss,
{"uint2 u_src_coords", "uint2 u_dst_coords", "uint2 u_end_coords", "uint2 u_size",
"bool u_set_mask_bit", "float u_depth_value"},
"bool u_set_mask_bit", "float u_depth_value", "uint u_resolution_scale", "uint u_pad0"},
true);
// Route RESOLUTION_SCALE / VRAM_SIZE / RCP_VRAM_SIZE through
// u_resolution_scale in the cbuffer above instead of having
// m_resolution_scale baked compile-time via WriteCommonFunctions's
// CONSTANT-emit path. After this, a resolution-scale toggle no
// longer changes the HLSL source string - same source, same hash,
// same DXBC - which is what makes vram_copy_ps pre-bakeable in a
// follow-up patch (cuts the variant axis from PGXP x scale down
// to just PGXP).
WriteCBufferResolutionScaleAliases(ss);
WriteCommonFunctions(ss, true);
DeclareTexture(ss, "samp0", 0, msaa);
DefineMacro(ss, "MSAA_COPY", msaa);
DeclareFragmentEntryPoint(ss, 0, 1, {}, true, 1, true, false, false, msaa);

View file

@ -32,6 +32,15 @@ private:
void WriteCommonFunctions(std::stringstream& ss, bool batch_uniform_buffer = false);
void WriteBatchUniformBuffer(std::stringstream& ss);
// Emit the #define aliases that route RESOLUTION_SCALE / VRAM_SIZE /
// RCP_VRAM_SIZE through u_resolution_scale in the shader's currently-
// in-scope cbuffer. Used by both WriteBatchUniformBuffer (where the
// cbuffer is the batch UBO) and by non-batch shaders that have added
// u_resolution_scale to their own per-shader UBO. Must be called
// AFTER the cbuffer declaration so u_resolution_scale is in scope.
void WriteCBufferResolutionScaleAliases(std::stringstream& ss);
void WriteBatchTextureFilter(std::stringstream& ss, GPUTextureFilter texture_filter);
uint32_t m_resolution_scale;