TsundereStation/data/shaders/vulkan
LibretroAdmin dae19ff67b core/vulkan: route the FS PGXP_DEPTH axis to a cbuffer scalar
Follow-up to 116a70e (C++ shadergen FS-side PGXP_DEPTH
routing) which brought D3D11 / D3D12 / OpenGL on. This commit
parallels it on the Vulkan path: collapses the PGXP_DEPTH
SPIR-V-structural axis from all 5 batch FS templates
(untextured + nearest + bilinear + jinc2 + xbr), making
gl_FragDepth always written with a runtime branch on
u_pgxp_depth.

Vulkan FS templates each had PGXP_DEPTH as one of their four
structural axes (3 interp x 2 persp x 2 dual x 2 pgxp = 24
blobs per template, 120 total batch FS blobs). After this
commit each template has 3 x 2 x 2 = 12 blobs, 60 total.

Routing for each template follows the same shape:

  GLSL UBO grows by u_pgxp_depth at offset 52. For the
  filter family (bilinear / jinc2 / xbr) the UBO previously
  declared the first 6 fields and stopped at offset 32; this
  commit adds a `layout(offset = 52) uint u_pgxp_depth;`
  trailing field to skip past the spec-const-handled
  intermediate slots (u_resolution_scale through
  u_interlacing). For Nearest the UBO already had
  u_uv_limits at offset 56; u_pgxp_depth slots in before it
  at offset 52. For the untextured template the UBO
  previously declared only the first 6 fields; same trailing
  layout(offset=52) treatment.

  GLSL FS body: drop the
    #if !defined(PGXP_DEPTH)
      gl_FragDepth = oalpha * gl_FragCoord.z;
    #endif
  block, replace with the unconditional
    gl_FragDepth = (u_pgxp_depth != 0u)
                     ? gl_FragCoord.z
                     : (oalpha * gl_FragCoord.z);
  Same expression shape as the C++ shadergen routing in
  116a70e but using Vulkan's gl_FragCoord.z (rasterizer-
  interpolated depth) instead of HLSL's v_pos.z.

  Regen tool: drop pgxp_axes from each of the three
  variant-generator helpers
  (_batch_fs_untextured_variants,
  _batch_fs_textured_nearest_variants,
  _batch_fs_textured_filter_variants - the last shared by
  bilinear / jinc2 / xbr). Filename suffix shortens from
  `_<interp>_<persp>_<dual>_<pgxp>` to
  `_<interp>_<persp>_<dual>`.

  embedded_spirv/: 120 obsolete files deleted (60 _pgxpoff
  + 60 _pgxpon, 24 per template), 60 new files added (12
  per template) carrying the runtime-branch SPIR-V. Total
  .inc count: 151 -> 91.

  embedded_shaders.{h,cpp}: 5 FS blob tables drop from [24]
  to [12]. 5 helper functions
  (GetBatch{Untextured,TexturedNearest,TexturedBilinear,
  TexturedJINC2,TexturedXBR}FragmentShaderBlob) drop the
  bool pgxp_depth parameter. Index encoding becomes
    index = interp*4 + persp*2 + dual  (0..11)
  (was: interp*8 + persp*4 + dual*2 + pgxp). 60 #include
  lines collapse to 60 (renamed, no count change for that
  block, just filename change).

  gpu_hw_vulkan.cpp: 5 FS lookup call sites
  (GetBatchUntexturedFragmentShaderBlob and the four
  textured filter variants in the switch on `filter`) drop
  their sixth argument (m_pgxp_depth_buffer).

What does NOT change in this commit:

  - The Vulkan batch VS still uses PGXP_DEPTH as a spec
    constant (id=3, declared in batch.vert.glsl, set via
    vs_spec.AddBool(3, m_pgxp_depth_buffer) at pipeline
    build). The C++ shadergen VS was routed to u_pgxp_depth
    in 49c0f82 but the Vulkan VS still has spec const
    PGXP_DEPTH. A separate small follow-up can route the VS
    GLSL through u_pgxp_depth too; the cost matters less on
    Vulkan because spec consts don't fork SPIR-V (one VS
    blob, two pipelines per PGXP value), only PSO assembly,
    and Vulkan already has depth_test as a PSO matrix dim
    that swaps the depth comparison func at draw time
    without rebuild.

  - The non-batch FS spec const id=3 PGXP_DEPTH sites
    (gpu_hw_vulkan.cpp lines 2477, 2523, 2581 - the
    vram_fill, vram_copy, vram_write_* paths) are
    unrelated to the batch FS arc. Those shaders genuinely
    declare PGXP_DEPTH as a spec const
    (data/shaders/vulkan/vram_fill.frag.glsl:80 etc.) and
    have their own pre-bake plans.

  - m_pgxp_depth_buffer STAYS in shader_source_changed
    across all backends. The Vulkan FS bytecode is now
    invariant under it but the C++ shadergen FS still has
    PSO-state dependency (gpu_hw_d3d12.cpp:1317-1319 picks
    LESS_EQUAL vs GREATER_EQUAL based on
    m_pgxp_depth_buffer at PSO build time), so PSO rebuild
    on flip is still required for D3D11 / D3D12 / OpenGL.
    Vulkan handles the depth-comp swap via its depth_test
    PSO matrix dim (depth_test=1 GREATER_OR_EQUAL,
    depth_test=2 LESS_OR_EQUAL - see
    gpu_hw_vulkan.cpp:2233's depth_test_values array) so
    Vulkan doesn't actually need a rebuild on flip
    post-this-commit. Decoupling the cross-backend
    rebuild trigger from Vulkan's no-longer-needing-it
    is a separate cleanup.

Variant-count delta:

  Per-template Vulkan FS: 24 -> 12 SPIR-V blobs.
  Across 5 templates: 120 -> 60 SPIR-V blobs.
  Pipeline cache footprint drops by 60 FS pipelines per
  (filter, render, texture) cell - actual PSO count depends
  on how many of those cells get exercised in practice but
  the savings are proportional.

Disk SPIR-V footprint:

  Before: 151 .inc files, ~11.3 MiB.
  After:  91 .inc files, ~7.5 MiB. (-60 files, ~3.8 MiB.
          The new files are slightly larger than the old
          _pgxpoff ones - they always emit the gl_FragDepth
          write and the runtime branch - but smaller than
          two old files combined, so net footprint drops.)

Cost matrix delta (Vulkan):

  Per-session m_pgxp_depth_buffer flip:
    Pre-commit: 60 batch FS pipelines per (filter, render,
                texture) cell rebuilt to swap from
                _pgxpoff -> _pgxpon. Tens of ms on cold
                pipeline cache, ~0 with warm cache.
    Post-commit: 0 FS pipeline rebuilds. The u_pgxp_depth
                 UBO write picks up at next FlushRender;
                 the FS bytecode is invariant across the
                 flip. The PSO depth-comp swap is handled
                 by Vulkan's depth_test matrix axis (and
                 the C++ side passes the right
                 depth_test index based on
                 m_pgxp_depth_buffer at draw time).

  Cold launch precompile pass on Vulkan: 60 fewer batch FS
  pipelines to enumerate / create.

  Steady-state runtime cost (Vulkan batch FS):
    + 4-byte UBO read on every fragment.
    + One uniform branch on u_pgxp_depth (cbuffer scalar,
      uniform across the warp) - resolves to a predicated
      select on modern hardware.
    + The early-Z TEST trade discussed in 116a70e's commit
      message applies: pre-commit, the _pgxpon variants on
      Vulkan retained early-Z TEST (no FS depth write);
      post-commit those variants always write gl_FragDepth
      so early-Z TEST is disabled. For PSX content on
      modern GPUs the cost is sub-noise (low overdraw,
      cheap FS, painter's-sorted geometry, depth values
      that are essentially random and Hi-Z can't compress).

Test:
  - Build: tools/regen_vulkan_spirv.py runs cleanly,
    producing 91 blobs. Embedded_shaders.cpp compiles
    against the smaller tables. gpu_hw_vulkan.cpp links
    clean.
  - Boot any PSX title on Vulkan with PGXP-depth off:
    u_pgxp_depth=0, the FS ternary picks oalpha *
    gl_FragCoord.z. Output identical to pre-commit.
  - Toggle PGXP-depth on: u_pgxp_depth flips to 1 via the
    existing UpdateHWSettings cbuffer push. The depth_test
    index passed to the PSO lookup also changes to select
    the LESS_OR_EQUAL pipeline. FS picks the
    gl_FragCoord.z pass-through path. No pipeline rebuild
    fired on Vulkan. Output identical to pre-commit
    PGXP-on state.
  - Mask-bit-heavy titles (Crash 1/2/3, Final Fantasy
    combat backgrounds, Silent Hill 1 fog plane): visual
    sanity check that the mask-bit-depth-encode path still
    works at all 5 filters (Nearest baseline, Bilinear,
    JINC2, xBR, BilinearBinAlpha).
  - PGXP-corrected titles (Tekken 3, Tomb Raider 1/2,
    Soul Blade): visual sanity that the pass-through write
    is value-equivalent to the omit-gl_FragDepth-declaration
    pre-routing behaviour. Watch for z-fighting on
    co-planar stage geometry.

Next:
  - Optionally route the Vulkan VS PGXP_DEPTH spec const
    through u_pgxp_depth for parity (small follow-up; cuts
    one spec const per VS pipeline build).
  - per_sample / dcp collapse decisions.
  - Pre-bake the resulting D3D12 FS matrix.
2026-05-19 04:14:04 +02:00
..
adaptive_downsample_blur.frag.glsl common/vulkan, core: spec-constant infrastructure + adaptive downsample mip/blur pre-bake 2026-05-17 20:39:19 +02:00
adaptive_downsample_composite.frag.glsl common/vulkan, core: pre-bake adaptive composite + box-sample downsample FS 2026-05-17 20:49:05 +02:00
adaptive_downsample_mip.frag.glsl common/vulkan, core: spec-constant infrastructure + adaptive downsample mip/blur pre-bake 2026-05-17 20:39:19 +02:00
batch.vert.glsl core/vulkan: drop the UV_LIMITS attribute-layout VS variant 2026-05-19 02:54:36 +02:00
batch_textured_bilinear.frag.glsl core/vulkan: route the FS PGXP_DEPTH axis to a cbuffer scalar 2026-05-19 04:14:04 +02:00
batch_textured_jinc2.frag.glsl core/vulkan: route the FS PGXP_DEPTH axis to a cbuffer scalar 2026-05-19 04:14:04 +02:00
batch_textured_nearest.frag.glsl core/vulkan: route the FS PGXP_DEPTH axis to a cbuffer scalar 2026-05-19 04:14:04 +02:00
batch_textured_xbr.frag.glsl core/vulkan: route the FS PGXP_DEPTH axis to a cbuffer scalar 2026-05-19 04:14:04 +02:00
batch_untextured.frag.glsl core/vulkan: route the FS PGXP_DEPTH axis to a cbuffer scalar 2026-05-19 04:14:04 +02:00
box_sample_downsample.frag.glsl common/vulkan, core: pre-bake adaptive composite + box-sample downsample FS 2026-05-17 20:49:05 +02:00
display.frag.glsl common/vulkan, core: pre-bake display FS (MSAA blob pair, 5-6 spec consts) 2026-05-18 00:02:50 +02:00
display_msaa.frag.glsl common/vulkan, core: pre-bake display FS (MSAA blob pair, 5-6 spec consts) 2026-05-18 00:02:50 +02:00
present_cursor.frag.glsl common/vulkan, core: pre-bake UV-quad VS and three presentation-stage shaders 2026-05-17 20:23:11 +02:00
present_display.frag.glsl common/vulkan, core: pre-bake UV-quad VS and three presentation-stage shaders 2026-05-17 20:23:11 +02:00
present_fullscreen.vert.glsl common/vulkan, core: pre-bake UV-quad VS and three presentation-stage shaders 2026-05-17 20:23:11 +02:00
README.md common/vulkan, core: pre-bake screen-quad VS, no glslang at runtime 2026-05-17 20:09:57 +02:00
screen_quad.vert.glsl common/vulkan, core: pre-bake screen-quad VS, no glslang at runtime 2026-05-17 20:09:57 +02:00
uv_quad.vert.glsl common/vulkan, core: pre-bake UV-quad VS and three presentation-stage shaders 2026-05-17 20:23:11 +02:00
vram_copy.frag.glsl common/vulkan, core: pre-bake VRAM copy FS (single blob, 2 spec consts) 2026-05-17 22:57:44 +02:00
vram_fill.frag.glsl common/vulkan, core: pre-bake VRAM fill FS (single blob, 8 specialisations) 2026-05-17 22:51:37 +02:00
vram_readback.frag.glsl common/vulkan, core: pre-bake VRAM readback FS (MSAA blob pair, 1-2 spec consts) 2026-05-18 00:16:58 +02:00
vram_readback_msaa.frag.glsl common/vulkan, core: pre-bake VRAM readback FS (MSAA blob pair, 1-2 spec consts) 2026-05-18 00:16:58 +02:00
vram_update_depth.frag.glsl common/vulkan, core: pre-bake VRAM update-depth FS (MSAA blob pair, no spec consts) 2026-05-18 00:38:31 +02:00
vram_update_depth_msaa.frag.glsl common/vulkan, core: pre-bake VRAM update-depth FS (MSAA blob pair, no spec consts) 2026-05-18 00:38:31 +02:00
vram_write_ssbo.frag.glsl common/vulkan, core: pre-bake VRAM write FS (SSBO + texbuf blob pair) 2026-05-17 23:46:01 +02:00
vram_write_texbuf.frag.glsl common/vulkan, core: pre-bake VRAM write FS (SSBO + texbuf blob pair) 2026-05-17 23:46:01 +02:00

Vulkan shader sources (pre-baked SPIR-V)

This directory holds the GLSL sources for shaders used by the Vulkan backend that have been moved off the runtime ShaderGen path. Each .glsl file here has a matching pre-compiled SPIR-V blob checked in to src/common/vulkan/embedded_spirv/.

Why this exists

Historically the Vulkan backend generated GLSL at runtime via src/core/shadergen.cpp and src/core/gpu_hw_shadergen.cpp, then handed the text to glslang inside src/common/vulkan/shader_compiler.cpp to produce SPIR-V. Both dep/glslang/ (4.4 MB of vendored source, 99 translation units) and the disk-backed runtime shader cache exist solely to support that path.

Pre-baking the SPIR-V removes:

  • the glslang dependency from the libretro core build,
  • the per-launch shader compilation stall, and
  • a class of "stale cache after glslang version bump" runtime failures.

The OpenGL / OpenGL ES / D3D11 / D3D12 backends still use the runtime ShaderGen path; only the Vulkan backend reads from the embedded SPIR-V.

Build-time constraint

The core build (Makefile.libretro and src/common/CMakeLists.txt) must never invoke an external shader compiler. The .inc files under src/common/vulkan/embedded_spirv/ are checked into the repository and compiled like any other C++ source. The tools/regen_vulkan_spirv.py helper exists for contributors who edit the .glsl sources in this directory; it is not part of the build.

Regenerating the SPIR-V blobs

After editing a .glsl file here, run:

python3 tools/regen_vulkan_spirv.py

This invokes glslangValidator --target-env vulkan1.0 on each .glsl file and rewrites the matching .inc under src/common/vulkan/embedded_spirv/. Both the .glsl and the regenerated .inc must be committed together so the two stay in sync.

glslangValidator is part of the Vulkan SDK (or the glslang-tools package on Debian / Ubuntu). It is not invoked by the core build.