TsundereStation/data/shaders/vulkan/vram_write_texbuf.frag.glsl
LibretroAdmin 30793791f8 common/vulkan, core: pre-bake VRAM write FS (SSBO + texbuf blob pair)
Patch 8 in the glslang-elimination series. VRAM write has a structural
variant the rest of Bucket A has not had to deal with: the descriptor
type at set=0 binding=0 differs depending on
m_use_ssbos_for_vram_writes (storage buffer vs uniform texel buffer),
so a single SPIR-V blob with a spec constant cannot collapse it. Two
blobs, otherwise sharing the same spec-constant interface.

Shader converted:

  VRAMWriteFragmentShader. Writes one upscaled PSX pixel per fragment
  from a host-staged 16-bit-per-pixel VRAM tile.

  Blobs:
    vram_write_ssbo_fs   - readonly restrict std430 SSBO ssbo_data[],
                           two PSX pixels packed per uint32 element.
    vram_write_texbuf_fs - uniform usamplerBuffer samp0 with R16_UINT
                           buffer view, one PSX pixel per element.

  Spec constants (identical between the two blobs):
    id = 0  RESOLUTION_SCALE (uint) - common-knob convention. Drives
                                      both the upscale-aware coord
                                      conversion (gl_FragCoord.xy
                                      divided by RESOLUTION_SCALE) and
                                      the VRAM_SIZE wrap-around math.
    id = 3  PGXP_DEPTH       (bool) - common-knob convention. Selects
                                      between sampled-alpha-derived
                                      depth and a fixed 1.0.

The depth_test pipeline knob remains a SetDepthState toggle, not a
spec constant; the C++ side keeps the 2-entry slot lookup in
m_vram_write_pipelines and selects the SSBO-vs-texbuf blob inside the
slot miss path. m_use_ssbos_for_vram_writes is a per-session decision
(set in CompilePipelines based on device features), so in any given
session only one of the two blobs will ever be touched at
vkCreateShaderModule time.

Inlined the RGBA5551ToRGBA8 helper directly into both .glsl files
rather than relying on what WriteCommonFunctions would have emitted -
keeps each blob self-contained and avoids dragging in the other
WriteCommonFunctions helpers (fixYCoord, RGBA8ToRGBA5551) that are not
used here. The Vulkan-side fixYCoord is the identity function in any
case.

Bindings unchanged from the shadergen path - both blobs target
m_vram_write_pipeline_layout, which is constructed with the matching
descriptor type at CreateLayouts time.

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

Like patches 6 and 7, VRAM write is exercised by game-driven VRAM
uploads, not by UpdateSettings, so this pipeline does not see the
SSAA-toggle lazy-build path. shader_precompile=Disabled is expected to
match Enabled / Lazy behaviour.

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke (any game).
  - shader_precompile=Disabled: any game with heavy texture streaming
    over CPU-side DMA - JRPG cutscenes, pre-rendered backgrounds,
    movie-frame VRAM uploads. The SSBO blob is the common one on
    desktop GPUs; the texbuf blob may be exercised by retesting on
    something with the SSBO path disabled.
2026-05-17 23:46:01 +02:00

86 lines
2.8 KiB
GLSL

// Pre-baked Vulkan source for
// GPU_HW_ShaderGen::GenerateVRAMWriteFragmentShader(use_ssbo=false).
//
// Uniform-texel-buffer variant of VRAM write. Used on drivers / device
// configurations where the SSBO path is unavailable or undesirable; the
// host packs the same PSX 16-bit VRAM data into a buffer view of format
// R16_UINT, and we read it via samplerBuffer texelFetch. One 16-bit
// value per buffer element.
//
// See vram_write_ssbo.frag.glsl for the structural-variant rationale -
// the descriptor type at binding 0 differs from the SSBO variant
// (UNIFORM_TEXEL_BUFFER vs STORAGE_BUFFER), so two SPIR-V blobs are
// required.
//
// Spec constants (same as the SSBO variant):
// constant_id = 0 RESOLUTION_SCALE (uint, common-knob convention).
// constant_id = 3 PGXP_DEPTH (bool, common-knob convention).
//
// Bindings (match m_vram_write_pipeline_layout when
// m_use_ssbos_for_vram_writes is false):
// push_constant - the UBO block below
// set=0 binding=0 - uniform usamplerBuffer samp0 (R16_UINT view)
#version 450 core
layout(constant_id = 0) const uint RESOLUTION_SCALE = 1u;
layout(constant_id = 3) const bool PGXP_DEPTH = false;
layout(push_constant) uniform PushConstants {
uvec2 u_base_coords;
uvec2 u_end_coords;
uvec2 u_size;
uint u_buffer_base_offset;
uint u_mask_or_bits;
float u_depth_value;
};
layout(set = 0, binding = 0) uniform usamplerBuffer 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;
vec4 RGBA5551ToRGBA8(uint v)
{
uint r = (v ) & 31u;
uint g = (v >> 5u) & 31u;
uint b = (v >> 10u) & 31u;
uint a = (v >> 15u) & 1u;
return vec4(float(r) / 31.0, float(g) / 31.0, float(b) / 31.0, float(a));
}
void main()
{
uvec2 VRAM_SIZE = uvec2(1024u, 512u) * RESOLUTION_SCALE;
uvec2 native_size = VRAM_SIZE / RESOLUTION_SCALE;
uvec2 coords = uvec2(uint(gl_FragCoord.x) / RESOLUTION_SCALE,
uint(gl_FragCoord.y) / RESOLUTION_SCALE);
if ((coords.x < u_base_coords.x && coords.x >= u_end_coords.x) ||
(coords.y < u_base_coords.y && coords.y >= u_end_coords.y))
{
discard;
}
uvec2 offset;
offset.x = (coords.x < u_base_coords.x)
? (native_size.x - u_base_coords.x + coords.x)
: (coords.x - u_base_coords.x);
offset.y = (coords.y < u_base_coords.y)
? (native_size.y - u_base_coords.y + coords.y)
: (coords.y - u_base_coords.y);
// One 16-bit value per buffer element (R16_UINT format).
uint buffer_offset = u_buffer_base_offset + (offset.y * u_size.x) + offset.x;
uint value = texelFetch(samp0, int(buffer_offset)).r | u_mask_or_bits;
o_col0 = RGBA5551ToRGBA8(value);
gl_FragDepth = PGXP_DEPTH ? 1.0
: ((o_col0.a == 1.0) ? u_depth_value : 0.0);
}