TsundereStation/data/shaders/vulkan/vram_fill.frag.glsl
LibretroAdmin af9c287efb common/vulkan, core: pre-bake VRAM fill FS (single blob, 8 specialisations)
Patch 6 in the glslang-elimination series. Demonstrates the
'multiple-spec-constants-collapse-source-variants' pattern: the original
shadergen produced four GLSL source strings (wrapped x interlaced) and
a per-session constructor-level PGXP_DEPTH bake, for up to eight cached
SPIR-V blobs on the disk-backed cache. This patch collapses all of that
into a single SPIR-V blob with three boolean specialisation constants.

Shader converted:

  VRAMFillFragmentShader. Writes u_fill_color to a destination rectangle
  in upscaled VRAM, optionally discarding pixels for interlace masking
  or out-of-bounds-wrapped fills.

  Spec constants used (documented in the .glsl):
    id =   3 PGXP_DEPTH  (bool, common-knob convention).
    id = 100 INTERLACED  (bool, shader-specific).
    id = 101 WRAPPED     (bool, shader-specific).

  Cross-shader id reuse note: id=100 is also FIRST_PASS in
  adaptive_downsample_mip.frag.glsl. SPIR-V spec scopes spec-constant
  ids per-stage, so reuse across different shaders is harmless. Each
  shader file documents its own ids at the top.

Call site changes in src/core/gpu_hw_vulkan.cpp:

- GetVRAMFillPipeline: switched to embedded path. The 'wrapped' and
  'interlaced' uint8 inputs (each 0 or 1) now drive AddBool calls into
  the SpecConstants helper rather than picking a GLSL source string.
  PGXP_DEPTH is sourced from GPU_HW::m_pgxp_depth_buffer. m_shadergen
  null-check removed.

  Pipeline cache shape unchanged: m_vram_fill_pipelines[wrapped][interlaced]
  still gives a 2x2 grid of distinct VkPipeline handles, since
  specialisation is per-pipeline. (A future patch could fold this into
  one VkPipeline per visible PGXP_DEPTH state if pipeline-create cost
  becomes a measurable burden, but that's a different optimisation;
  current state matches the pre-patch pipeline count.)

Notes on spec-const evaluation:

- The dst_coords uvec2 conversion at the top of main() is always
  computed in the SPIR-V even when neither INTERLACED nor WRAPPED is
  taken. The driver's optimiser is expected to dead-code-eliminate it
  at vkCreateGraphicsPipelines time once both spec constants are bound
  to false; spirv-dis on the non-specialised blob shows the IR allows
  this. If a driver doesn't optimise it the cost is negligible (a few
  ALU ops per fragment).

- gl_FragDepth = PGXP_DEPTH ? 1.0 : u_fill_color.a translates to an
  OpSelect on a spec-constant condition. After specialisation the
  OpSelect collapses to one of its two operands; modern drivers do
  this fold during pipeline compile.

Cumulative state after this patch:

- 14 of ~25-30 Bucket A blob variants converted (counting this single
  blob with eight specialisations as one blob - the on-disk pipeline
  cache shape across the four wrapped x interlaced combos and two
  PGXP states is what matters operationally, and that remains 4-or-8
  distinct VkPipelines as before).
- Spec-constant infrastructure exercised with 3 booleans driving
  source-level variants - the densest specialisation use so far.
- First use of constant_id=3 (PGXP_DEPTH common knob).
- Remaining Bucket A: VRAM copy FS (MSAA pair + spec consts),
  VRAM write FS (SSBO vs uniform-buffer pair + spec consts),
  Display FS (the densest one).

Smoke tests in the sandbox:

- vram_fill_fs.inc reconstructs to SPIR-V that passes
  spirv-val --target-env vulkan1.0.
- spirv-dis confirms three SpecId decorations (3, 100, 101) on three
  separate OpSpecConstantFalse %bool entries.
- g++ -fsyntax-only on embedded_shaders.cpp is clean.

Smoke tests at user's end:

- VRAM fill is exercised heavily during VRAM-to-VRAM copies and
  initial framebuffer clears at game boot. Cold-boot with cache nuked
  at scales 1x and 4x, run any game past the first few frames.
  Wrong behaviour in the INTERLACED branch would show as missing
  every-other-line in interlaced video modes (most visible in some
  PSX RPG cutscenes). Wrong behaviour in the WRAPPED branch would
  cause incorrect VRAM-to-VRAM rectangles in games that DMA between
  overlapping regions.

- Toggle PGXP Depth Buffer on/off at runtime. This forces a
  pipeline destroy + rebuild with the new spec-constant value for
  PGXP_DEPTH (id=3). If the spec const isn't being re-applied on
  rebuild, the depth output stays at the old value across the
  toggle - visually that means correct-vs-broken depth ordering in
  PGXP-affected games (Tekken 3 is a known fixture for this).

- The four wrapped/interlaced combinations are exercised by
  oversized VRAM fills and by interlaced-video games respectively.
  At least one of each in normal gameplay should hit a code path
  that runs through every spec-const combination.
2026-05-17 22:51:37 +02:00

69 lines
2.2 KiB
GLSL

// Pre-baked Vulkan source for
// GPU_HW_ShaderGen::GenerateVRAMFillFragmentShader(wrapped, interlaced).
//
// Single SPIR-V blob; the original four source-level variants (wrapped x
// interlaced) plus the PGXP_DEPTH constructor-level toggle collapse into
// three boolean specialisation constants. One source -> four (or eight,
// if you count PGXP_DEPTH) specialised pipelines.
//
// Spec constants used by this shader:
// constant_id = 3 PGXP_DEPTH (bool, common-knob convention).
// constant_id = 100 INTERLACED (bool, shader-specific).
// constant_id = 101 WRAPPED (bool, shader-specific).
//
// Note on cross-shader id reuse: 100 is also used by
// adaptive_downsample_mip.frag.glsl (for FIRST_PASS). Spec-constant ids
// are scoped per-stage in the SPIR-V spec, so reuse across different
// shaders is harmless; each file documents its own ids at the top.
//
// No texture sampling, no descriptor sets - the pipeline layout used by
// this pipeline (m_no_samplers_pipeline_layout) declares only the push
// constant block below.
#version 450 core
layout(constant_id = 3) const bool PGXP_DEPTH = false;
layout(constant_id = 100) const bool INTERLACED = false;
layout(constant_id = 101) const bool WRAPPED = false;
layout(push_constant) uniform PushConstants {
uvec2 u_dst_coords;
uvec2 u_end_coords;
vec4 u_fill_color;
uint u_interlaced_displayed_field;
};
// 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;
void main()
{
// dst_coords is only needed by the INTERLACED / WRAPPED branches. The
// driver folds away the uvec2 conversion at pipeline-create time when
// both spec constants are false.
uvec2 dst_coords = uvec2(gl_FragCoord.x, gl_FragCoord.y);
if (INTERLACED)
{
if ((dst_coords.y & 1u) == u_interlaced_displayed_field)
discard;
}
if (WRAPPED)
{
// Ensure the fragment is inside the (possibly wrapped) destination
// rectangle; discard otherwise.
if ((dst_coords.x < u_dst_coords.x && dst_coords.x >= u_end_coords.x) ||
(dst_coords.y < u_dst_coords.y && dst_coords.y >= u_end_coords.y))
{
discard;
}
}
o_col0 = u_fill_color;
gl_FragDepth = PGXP_DEPTH ? 1.0 : u_fill_color.a;
}