TsundereStation/data/shaders/vulkan/box_sample_downsample.frag.glsl
LibretroAdmin bc2776687f common/vulkan, core: pre-bake adaptive composite + box-sample downsample FS
Patch 4 in the glslang-elimination series. Completes the downsample
subsystem: all four downsample pipeline build paths in
src/core/gpu_hw_vulkan.cpp now use embedded SPIR-V. This patch is the
first to plumb the common-knobs spec constant (RESOLUTION_SCALE at
constant_id=0), establishing the project-wide pattern for the
larger remaining utility shaders.

Shaders converted by this patch:

1. AdaptiveDownsampleCompositeFragmentShader. The runtime-baked
   RESOLUTION_SCALE used to derive RCP_VRAM_SIZE = 1/(VRAM_SIZE) and a
   mip-level multiplier '(RESOLUTION_SCALE - 1)'. Both are now SPIR-V
   OpSpecConstantOp arithmetic chained from the constant_id=0 input;
   the driver folds them to literals at vkCreateGraphicsPipelines time
   once GPU_HW_Vulkan::m_resolution_scale is bound. Reads two textures
   at bindings 1 and 2 (matching ShaderGen's 'index + 1' convention
   for Vulkan).

2. BoxSampleDownsampleFragmentShader. Same RESOLUTION_SCALE spec
   constant, used three ways: in 'base_coords = gl_FragCoord *
   uvec2(RESOLUTION_SCALE, RESOLUTION_SCALE)' (OpSpecConstantComposite),
   in the inner loop bounds 'offset < RESOLUTION_SCALE', and in the
   final divisor 'color /= RESOLUTION_SCALE * RESOLUTION_SCALE'
   (OpSpecConstantOp IMul). Whether the loop unrolls is left to the
   driver; this matches the original runtime path's behaviour where
   GLSL frontend often unrolled the bound-as-literal version. At
   extreme RESOLUTION_SCALE=16 the inner body runs 256 times either
   way.

GLSL constraint discovered (documented in the composite shader for
future contributors): glslangValidator rejects spec-const-derived
expressions at global 'const' scope - 'global const initializers must
be constant'. The workaround is to compute the derived value inside
main() as a regular vec2; the SPIR-V will then contain runtime
ALU ops chained from the OpSpecConstant rather than the cleaner
OpSpecConstantOp form, but the driver constant-folds either at
specialisation time.

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

- GetDownsampleCompositePassPipeline: switched to embedded blob with
  RESOLUTION_SCALE spec const. m_shadergen null-check dropped (it
  was only there for the glslang path).

- GetDownsampleFirstPassPipeline (Box branch): switched to embedded
  blob with RESOLUTION_SCALE spec const. m_shadergen null-check that
  patch 3 had left inside this branch is now also removed - both
  branches of this function are fully pre-baked. The comment block
  at function entry is updated to reflect the new state.

Cumulative state after this patch:

- 9 of ~25-30 Bucket A variants converted. The entire downsample
  subsystem (4 pipelines: Adaptive first-pass, mid-pass, blur,
  composite, plus Box first-pass) is now glslang-free.
- Spec constant infrastructure exercised on its second-stage use
  case: a numeric uint (RESOLUTION_SCALE) rather than a bool
  (FIRST_PASS from patch 3). Confirms the
  OpSpecConstant/OpSpecConstantOp paths through the SpecConstants
  helper work for both flavours.
- Remaining Bucket A: VRAM read FS, VRAM update-depth FS (both
  trivial - just need MULTISAMPLES + possibly an MSAA-vs-not blob
  pair), VRAM copy FS (MSAA blob pair + RESOLUTION_SCALE +
  PGXP_DEPTH), VRAM write FS (SSBO vs uniform-buffer blob pair +
  RESOLUTION_SCALE), VRAM fill FS (RESOLUTION_SCALE + PGXP_DEPTH +
  WRAPPED + INTERLACED), Display FS (the multi-variant one).

Smoke tests in the sandbox:

- Both new .inc files reconstruct to SPIR-V that passes
  'spirv-val --target-env vulkan1.0'.
- spirv-dis on the composite FS confirms 'OpDecorate %RESOLUTION_SCALE
  SpecId 0' and 'OpSpecConstantOp %uint ISub %RESOLUTION_SCALE
  %uint_1' for the mip-level multiplier.
- spirv-dis on the box-sample FS confirms 'OpDecorate
  %RESOLUTION_SCALE SpecId 0', 'OpSpecConstantComposite %v2uint'
  for the base_coords scaling, and 'OpSpecConstantOp %uint IMul' for
  the final divisor.
- g++ -fsyntax-only on embedded_shaders.cpp against the real tree
  headers is clean.

Smoke tests at user's end:

- Cold-boot with shader cache nuked, downsample mode = Adaptive at
  several resolution scales (1x, 4x, 8x, ideally 16x). The composite
  pipeline now drives a spec-constant for RESOLUTION_SCALE; if the
  plumbing is wrong, the downsampled output will be either at the
  wrong scale (mip 0 used everywhere -> overly sharp) or at the
  maximum mip everywhere (overly blurry). Both modes should be
  visually unchanged from the patch-3 baseline.
- Cold-boot with cache nuked, downsample mode = Box, same scales.
  Box-sample now also takes the spec constant; if mis-plumbed at
  scale > 1 the output will be all-black (loop runs 0 times if
  RESOLUTION_SCALE = 0 in SPIR-V) or wildly wrong colors (if the
  averaging divisor is wrong).
- Toggle resolution scale at runtime (which triggers pipeline
  destroy + rebuild). Confirms the SpecConstants helper correctly
  re-specialises on each build with the new m_resolution_scale
  value. This is the test most likely to catch a 'spec value latched
  on first build and never updates' bug, if one exists.
2026-05-17 20:49:05 +02:00

49 lines
1.9 KiB
GLSL

// Pre-baked Vulkan source for
// GPU_HW_ShaderGen::GenerateBoxSampleDownsampleFragmentShader().
//
// Vulkan-only port. Used when downsample mode = Box: averages a
// RESOLUTION_SCALE x RESOLUTION_SCALE block of upscaled texels back down
// to a single PSX-native-resolution pixel.
//
// Spec constant: RESOLUTION_SCALE at constant_id=0. Drives:
// - the block origin scaling (base_coords)
// - the two inner loop bounds (so the driver can unroll if it chooses)
// - the final averaging divisor (RESOLUTION_SCALE * RESOLUTION_SCALE)
//
// All three uses become OpSpecConstantOp arithmetic in the SPIR-V; the
// driver folds them at pipeline-create time once the value is bound.
//
// Note on loop unrolling: at scale=16 the inner body runs 256 times.
// Whether the driver unrolls is a heuristic call - the original runtime
// path baked RESOLUTION_SCALE as a literal so the GLSL frontend often
// unrolled there too. Behaviour parity is the goal; perf is at worst
// identical, at best better because pipeline-create-time specialisation
// gives the driver a cleaner IR than runtime-shaderlang generation did.
//
// Descriptor binding 1 matches ShaderGen's "textures start at 1"
// convention for Vulkan (m_single_sampler_descriptor_set_layout).
#version 450 core
layout(constant_id = 0) const uint RESOLUTION_SCALE = 1u;
layout(set = 0, binding = 1) uniform sampler2D samp0;
layout(location = 0) in VertexData {
vec2 v_tex0;
};
layout(location = 0) out vec4 o_col0;
void main()
{
vec3 color = vec3(0.0, 0.0, 0.0);
uvec2 base_coords = uvec2(gl_FragCoord.xy) * uvec2(RESOLUTION_SCALE, RESOLUTION_SCALE);
for (uint offset_x = 0u; offset_x < RESOLUTION_SCALE; offset_x++)
{
for (uint offset_y = 0u; offset_y < RESOLUTION_SCALE; offset_y++)
color += texelFetch(samp0, ivec2(base_coords + uvec2(offset_x, offset_y)), 0).rgb;
}
color /= float(RESOLUTION_SCALE * RESOLUTION_SCALE);
o_col0 = vec4(color, 1.0);
}