TsundereStation/data
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
..
shaders core/vulkan: route the FS PGXP_DEPTH axis to a cbuffer scalar 2026-05-19 04:14:04 +02:00