core: route RESOLUTION_SCALE through vram_read_ps cbuffer

Same refactor as e56d4d4 / 9d2b49d applied to vram_read_ps. Adds
u_resolution_scale + u_pad0 to its cbuffer, routes
RESOLUTION_SCALE / VRAM_SIZE / RCP_VRAM_SIZE through the cbuffer,
switches WriteCommonFunctions to the (ss, true) path.

vram_read_ps is the heaviest user of RESOLUTION_SCALE in the
non-batch shader family - its body has three direct references:

  * `if (RESOLUTION_SCALE == 1u)` early-out for the 1x fast path
    (no box-filter downsampling needed)
  * Inner loop bounds for the box filter:
        for (offset_x = 0u; offset_x < RESOLUTION_SCALE; offset_x++)
          for (offset_y = 0u; offset_y < RESOLUTION_SCALE; offset_y++)
            value += LoadVRAM(...);
  * Final divisor:
        value /= float(RESOLUTION_SCALE * RESOLUTION_SCALE);

When RESOLUTION_SCALE was a compile-time constant, fxc / glslang
fully unrolled the inner loops (at scale=4, 16 iterations inlined
into 16 sequential LoadVRAM calls) and constant-folded the
divisor. After this refactor, RESOLUTION_SCALE is a runtime
uniform value, so:

  * The early-out branch becomes a runtime branch. Uniform across
    the SIMD wave (every lane sees the same cbuffer value) so
    GPU divergence cost is zero - just one predicate test per
    wave.
  * The loops can no longer fully unroll at compile time.
    HLSL's [unroll] hint can still partially unroll with a known
    upper bound, and the runtime loop overhead is tiny next to
    the texture-load cost.
  * The divisor uses cbuffer-loaded values - one extra imul + i2f
    on the unscaled path.

Net per-pixel cost: a handful of extra cycles, dominated by the
texture loads either way. And vram_read runs on-demand only -
screenshot capture, libretro frontend's video format conversion
at scanout, etc. - not every frame. So the runtime cost is
imperceptible.

Files:

* gpu_hw_shadergen.cpp: GenerateVRAMReadFragmentShader reordered
  so DeclareUniformBuffer runs before WriteCommonFunctions(ss,
  true). u_resolution_scale + u_pad0 appended to the cbuffer
  member list (after u_size). WriteCBufferResolutionScaleAliases
  call inserted between cbuffer declaration and WriteCommonFunctions.
* gpu_hw_d3d11.cpp / gpu_hw_d3d12.cpp / gpu_hw_vulkan.cpp /
  gpu_hw_opengl.cpp: each backend's ReadVRAM constructs an
  inline `uint32_t uniforms[]` array of cbuffer DWORDs and pushes
  it via DrawUtilityShader / SetGraphicsRoot32BitConstants /
  vkCmdPushConstants / UploadUniformBuffer. All four go from
  uniforms[4] (u_base_coords.xy + u_size.xy) to uniforms[6]
  (adding u_resolution_scale + u_pad0).
  D3D12 push-constants ceiling check: 6 DWORDs = 24 bytes <<
  MAX_PUSH_CONSTANTS_SIZE = 64. Vulkan: same. OpenGL preserves
  its existing y-flip on copy_rect.top (lower-left origin).

Unlike e56d4d4 / 9d2b49d there's no formal VRAMReadbackUBOData
struct in gpu_hw.h - this shader has always been driven by
inline uniforms arrays at the per-backend call site. Could be
factored to a struct in a follow-up cleanup, but the inline
arrays are tightly localised (one site per backend) and the
4-DWORD-to-6-DWORD diff is small enough that a struct would
add more noise than it removes.

Variant axis dropped: vram_read_ps had MSAA (M) x
m_resolution_scale (N) variants. After this commit it's just M
(MSAA on/off) variants regardless of scale. MSAA refactor is a
separate concern - the body has a #if MULTISAMPLING branch
controlling LOAD_TEXTURE vs LOAD_TEXTURE_MS, plus DeclareTexture
emits Texture2D vs Texture2DMS<float4> based on UsingMSAA(),
plus the MULTISAMPLES count is baked from m_multisamples for
the wave-sum loop. Routing MSAA through the cbuffer would
require pre-baking BOTH the Texture2D and Texture2DMS bindings
and runtime-selecting which one is bound, which is a bigger
ABI change than the RESOLUTION_SCALE refactor and is deferred.

Shader-cache invalidation: vram_read_ps's source string changes
(new cbuffer members + #define aliases instead of CONSTANT
literals). First boot after this lands re-compiles it once; the
two MSAA variants get regenerated cache entries. Subsequent
boots hit the cache.

Test:
  - Build under each backend succeeds.
  - Take a screenshot in-game at every resolution scale value
    (1x, 2x, 4x, 8x). Screenshot output must match what the
    framebuffer shows on screen at native (1x) and the box-
    filter downsampled equivalent at >1x. If u_resolution_scale
    weren't populated correctly:
    * Wrong loop bounds: box filter would sample wrong pixel
      ranges - garbled screenshots at >1x.
    * Wrong fast-path branch: would fall through to box filter
      at 1x and sample beyond u_size, producing artifacts.
    * Wrong divisor: image would be too bright or too dark.
    All three are visible immediately as wrong screenshot output.
  - Libretro frontend video format conversion (the runtime
    readback path that supports RA's video filter chain and
    GPU-accelerated screenshot pipeline) keeps producing
    correct frames.

Next: display_ps - last non-batch shader using compile-time-baked
RESOLUTION_SCALE in body. After that, the only remaining users
are the adaptive_downsample_* shaders (separate concern - they
already take a u_rcp_resolution cbuffer field, structure is
different).
This commit is contained in:
LibretroAdmin 2026-05-18 19:17:41 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 2980961655
5 changed files with 41 additions and 7 deletions

View file

@ -1811,7 +1811,10 @@ void GPU_HW_D3D11::ReadVRAM(uint32_t x, uint32_t y, uint32_t width, uint32_t hei
const uint32_t encoded_height = copy_rect.GetHeight();
// Encode the 24-bit texture as 16-bit.
const uint32_t uniforms[4] = {copy_rect.left, copy_rect.top, copy_rect.GetWidth(), copy_rect.GetHeight()};
// 6 DWORDs to match the post-RESOLUTION_SCALE-refactor vram_read_ps
// cbuffer (u_base_coords.xy, u_size.xy, u_resolution_scale, u_pad0).
const uint32_t uniforms[6] = {copy_rect.left, copy_rect.top, copy_rect.GetWidth(), copy_rect.GetHeight(),
m_resolution_scale, 0u /* u_pad0 */};
m_context->RSSetState(m_cull_none_rasterizer_state_no_msaa.Get());
m_context->OMSetRenderTargets(1, m_vram_encoding_texture.GetD3DRTVArray(), nullptr);
m_context->OMSetDepthStencilState(m_depth_disabled_state.Get(), 0);

View file

@ -2116,7 +2116,10 @@ void GPU_HW_D3D12::ReadVRAM(uint32_t x, uint32_t y, uint32_t width, uint32_t hei
m_vram_readback_texture.TransitionToState(D3D12_RESOURCE_STATE_RENDER_TARGET);
// Encode the 24-bit texture as 16-bit.
const uint32_t uniforms[4] = {copy_rect.left, copy_rect.top, copy_rect.GetWidth(), copy_rect.GetHeight()};
// 6 DWORDs to match the post-RESOLUTION_SCALE-refactor vram_read_ps
// cbuffer (u_base_coords.xy, u_size.xy, u_resolution_scale, u_pad0).
const uint32_t uniforms[6] = {copy_rect.left, copy_rect.top, copy_rect.GetWidth(), copy_rect.GetHeight(),
m_resolution_scale, 0u /* u_pad0 */};
cmdlist->OMSetRenderTargets(1, &m_vram_readback_texture.GetRTVOrDSVDescriptor().cpu_handle, FALSE, nullptr);
cmdlist->SetGraphicsRootSignature(m_single_sampler_root_signature.Get());
cmdlist->SetGraphicsRoot32BitConstants(0, sizeof(uniforms) / sizeof(uint32_t), uniforms, 0);

View file

@ -1803,8 +1803,10 @@ void GPU_HW_OpenGL::ReadVRAM(uint32_t x, uint32_t y, uint32_t width, uint32_t he
const uint32_t encoded_height = copy_rect.GetHeight();
// Encode the 24-bit texture as 16-bit.
const uint32_t uniforms[4] = {copy_rect.left, VRAM_HEIGHT - copy_rect.top - copy_rect.GetHeight(), copy_rect.GetWidth(),
copy_rect.GetHeight()};
// 6 DWORDs to match the post-RESOLUTION_SCALE-refactor vram_read_ps
// cbuffer (u_base_coords.xy, u_size.xy, u_resolution_scale, u_pad0).
const uint32_t uniforms[6] = {copy_rect.left, VRAM_HEIGHT - copy_rect.top - copy_rect.GetHeight(), copy_rect.GetWidth(),
copy_rect.GetHeight(), m_resolution_scale, 0u /* u_pad0 */};
m_vram_encoding_texture.BindFramebuffer(GL_DRAW_FRAMEBUFFER);
m_vram_texture.Bind();
m_vram_read_program.Bind();

View file

@ -1187,8 +1187,31 @@ std::string GPU_HW_ShaderGen::GenerateVRAMReadFragmentShader()
{
std::stringstream ss;
WriteHeader(ss);
WriteCommonFunctions(ss);
DeclareUniformBuffer(ss, {"uint2 u_base_coords", "uint2 u_size"}, true);
// u_resolution_scale appended; u_pad0 keeps the cbuffer 16-byte-
// aligned. The 4 callers (D3D11 / D3D12 / OpenGL / Vulkan
// GPU_HW_*::ReadVRAM) must push uniforms in this exact order:
// u_base_coords.x, u_base_coords.y, u_size.x, u_size.y,
// u_resolution_scale, u_pad0.
DeclareUniformBuffer(ss,
{"uint2 u_base_coords", "uint2 u_size", "uint u_resolution_scale", "uint u_pad0"},
true);
// Route RESOLUTION_SCALE / VRAM_SIZE / RCP_VRAM_SIZE through the
// cbuffer above. Same pattern as e56d4d4 / 9d2b49d. vram_read_ps
// is the heaviest user of RESOLUTION_SCALE in the body:
// * `if (RESOLUTION_SCALE == 1u)` for the fast 1x path
// * Inner box-filter loops `for (offset < RESOLUTION_SCALE; ...)`
// * Final divisor `RESOLUTION_SCALE * RESOLUTION_SCALE`
// The fast-path branch becomes a runtime branch on
// u_resolution_scale, but the branch is uniform across the wave
// (same cbuffer value for all SIMD lanes) so divergence cost
// is zero. The loops can no longer be fully unrolled at compile
// time, but the runtime loop overhead is microscopic compared
// to the texture-load cost - and vram_read runs on-demand
// (screenshot capture, libretro readback) not every frame.
WriteCBufferResolutionScaleAliases(ss);
WriteCommonFunctions(ss, true);
DeclareTexture(ss, "samp0", 0, UsingMSAA());

View file

@ -3197,7 +3197,10 @@ void GPU_HW_Vulkan::ReadVRAM(uint32_t x, uint32_t y, uint32_t width, uint32_t he
BeginRenderPass(m_vram_readback_render_pass, m_vram_readback_framebuffer, 0, 0, rp_width, rp_height);
// Encode the 24-bit texture as 16-bit.
const uint32_t uniforms[4] = {copy_rect.left, copy_rect.top, copy_rect.GetWidth(), copy_rect.GetHeight()};
// 6 DWORDs to match the post-RESOLUTION_SCALE-refactor vram_read_ps
// cbuffer (u_base_coords.xy, u_size.xy, u_resolution_scale, u_pad0).
const uint32_t uniforms[6] = {copy_rect.left, copy_rect.top, copy_rect.GetWidth(), copy_rect.GetHeight(),
m_resolution_scale, 0u /* u_pad0 */};
vkCmdBindPipeline(cmdbuf, VK_PIPELINE_BIND_POINT_GRAPHICS, GetVRAMReadbackPipeline());
vkCmdPushConstants(cmdbuf, m_single_sampler_pipeline_layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(uniforms),
uniforms);