TsundereStation/data/shaders/vulkan/vram_readback.frag.glsl
LibretroAdmin cc69fc08e5 common/vulkan, core: pre-bake VRAM readback FS (MSAA blob pair, 1-2 spec consts)
Patch 10 in the glslang-elimination series. The shadergen helper is
named GenerateVRAMReadFragmentShader, but the pipeline it lives in
(m_vram_readback_pipeline) is the CPU readback path: it samples the
upscaled m_vram_texture, runs a box filter to downsample blocks back
to PSX-native 16bpp, and packs two adjacent 16-bit pixels into one
RGBA8 output texel for the host to pick up via staging.

Shader converted:

  VRAMReadFragmentShader. Single output slot, no per-frame variance
  - the existing C++ side already caches it as a single
  m_vram_readback_pipeline VkPipeline. Two blobs only because of the
  source texture's MSAA structural split (sampler2D vs sampler2DMS).

  Blobs:
    vram_readback_fs      - sampler2D, no per-pixel sample averaging.
    vram_readback_msaa_fs - sampler2DMS, LoadVRAM averages
                            MULTISAMPLES texelFetch results before
                            the box filter runs.

  Common spec constant:
    id = 0 RESOLUTION_SCALE (uint) - drives both the base_coords stride
                                     and the box-filter inner-loop
                                     bounds.
  MSAA blob adds:
    id = 1 MULTISAMPLES     (uint) - LoadVRAM averaging loop bound.

The OpenGL lower-left-origin Y-flip branch in the shadergen source is
omitted: Vulkan VRAM Y matches scanout Y directly, so the conditional
fold-up never fires on this backend. The single output pipeline keeps
its single C++ slot - no per-mode array needed.

Render target (m_vram_readback_texture) is always single-sample at
line 1118, so the pipeline's multisample state stays at the default
single-sample on both blobs. As with display, the MSAA-ness lives
entirely in the input sampler type.

Bindings unchanged: push_constant for the UBO, set=0 binding=1 for
the source sampler, matching m_single_sampler_pipeline_layout.

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - spirv-val --target-env vulkan1.0 on both blobs (1158 / 1302
    words, 4.5K / 5.1K).
  - --target-env vulkan1.0 vs un-flagged glslangValidator: byte-
    identical SPIR-V for both.

VRAM readback is the rarest of the utility pipelines - it's exercised
only when something reads from upscaled VRAM through the host (save-
state capture, debugger introspection, certain games that read back
their own framebuffer for special effects). Not on the SSAA-toggle
hot-build path; shader_precompile=Disabled is expected to behave
identically to Enabled / Lazy.

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke.
  - shader_precompile=Disabled: any savestate save + load round-trip
    (forces a readback). A few games also self-read for visual
    effects - Silent Hill's fog overlay and the Castlevania:
    Symphony of the Night transition flashes are known triggers.
    SSAA round-trip is incidental but quick.

After this lands, the only non-batch shaders still on the runtime
shadergen + glslang path are the VRAM update depth pair, which is on
the SSAA-toggle hot-build path and broke patch 5. Plan for those is
to introduce a targeted pre-build at CompilePipelines time even in
Disabled mode, separately from the SPIR-V conversion itself.
2026-05-18 00:16:58 +02:00

88 lines
3 KiB
GLSL

// Pre-baked Vulkan source for
// GPU_HW_ShaderGen::GenerateVRAMReadFragmentShader() when the source
// m_vram_texture is single-sample (sampler2D).
//
// "VRAM read" is the misleading shadergen name. The pipeline this
// shader belongs to (m_vram_readback_pipeline in gpu_hw_vulkan.cpp) is
// CPU readback: it samples the upscaled m_vram_texture, downsamples
// blocks back to PSX-native 16bpp, and packs two adjacent 16-bit
// pixels into one RGBA8 output texel. The host then transfers that
// output texture to staging and reads it on the CPU.
//
// Two blobs handle the structural MSAA split (sampler2D vs sampler2DMS,
// matching the display FS pattern); RESOLUTION_SCALE folds into a spec
// constant, so a single blob covers all upscale factors. The OpenGL
// "lower-left origin flip" branch in the shadergen source is dropped
// because Vulkan VRAM Y matches scanout Y directly.
//
// Spec constants used by this shader:
// constant_id = 0 RESOLUTION_SCALE (uint, common-knob convention).
// Drives both the base_coords stride and the
// box-filter inner-loop bounds.
//
// Bindings (match m_single_sampler_pipeline_layout):
// push_constant - the UBO block below
// set=0 binding=1 - uniform sampler2D samp0 (m_vram_texture view)
#version 450 core
layout(constant_id = 0) const uint RESOLUTION_SCALE = 1u;
layout(push_constant) uniform PushConstants {
uvec2 u_base_coords;
uvec2 u_size;
};
layout(set = 0, binding = 1) uniform sampler2D samp0;
// Unused; kept for interface-match with the screen-quad VS.
layout(location = 0) in VertexData {
vec2 v_tex0;
};
layout(location = 0) out vec4 o_col0;
uint RGBA8ToRGBA5551(vec4 v)
{
uint r = uint(roundEven(v.r * 31.0));
uint g = uint(roundEven(v.g * 31.0));
uint b = uint(roundEven(v.b * 31.0));
uint a = (v.a != 0.0) ? 1u : 0u;
return r | (g << 5) | (b << 10) | (a << 15);
}
vec4 LoadVRAM(ivec2 coords)
{
return texelFetch(samp0, coords, 0);
}
uint SampleVRAM(uvec2 coords)
{
if (RESOLUTION_SCALE == 1u)
return RGBA8ToRGBA5551(LoadVRAM(ivec2(coords)));
// Box filter for downsampling upscaled VRAM back to native.
vec4 value = vec4(0.0);
uvec2 base_coords = coords * RESOLUTION_SCALE;
for (uint oy = 0u; oy < RESOLUTION_SCALE; oy++)
for (uint ox = 0u; ox < RESOLUTION_SCALE; ox++)
value += LoadVRAM(ivec2(base_coords + uvec2(ox, oy)));
value /= float(RESOLUTION_SCALE * RESOLUTION_SCALE);
return RGBA8ToRGBA5551(value);
}
void main()
{
uvec2 sample_coords = uvec2(uint(gl_FragCoord.x) * 2u,
uint(gl_FragCoord.y));
sample_coords += u_base_coords;
// Output is 32-bit-per-texel; two PSX 16bpp pixels packed per output.
uint left = SampleVRAM(sample_coords);
uint right = SampleVRAM(uvec2(sample_coords.x + 1u, sample_coords.y));
o_col0 = vec4(float( left & 0xFFu),
float((left >> 8u) & 0xFFu),
float( right & 0xFFu),
float((right >> 8u) & 0xFFu)) / 255.0;
}