Commit graph

209 commits

Author SHA1 Message Date
LibretroAdmin
c6e9f87be0 core/vulkan: route the Nearest FS UV_LIMITS axis to a cbuffer scalar
Follow-up to 078783e (Vulkan VS UV_LIMITS collapse) and the
earlier 8b43518 (C++ shadergen UV_LIMITS routing). Brings the
Vulkan Nearest FS into the same shape as the C++ shadergen
routing - UV_LIMITS is no longer a SPIR-V-structural axis on
the Vulkan side; the Nearest FS source has a single shape and
runtime-branches on u_uv_limits.

With this commit m_using_uv_limits is no longer a shader-
source-changed trigger for any backend, so it leaves the
rebuild-trigger predicates entirely. It remains as a member
field that drives the per-vertex ComputePolygonUVLimits CPU
optimisation (gpu_hw.cpp:740) - that's an orthogonal concern
from shader / pipeline routing.

What changes:

  data/shaders/vulkan/batch_textured_nearest.frag.glsl
    - Adds u_uv_limits to the BatchUBOData declaration with
      an explicit layout(offset = 56) (skips past the spec-
      const-handled fields between offset 32 and 55, which
      Vulkan does not redeclare in its UBO because per-
      session settings are spec consts on this path -
      RESOLUTION_SCALE, TRUE_COLOR, SCALED_DITHERING,
      DITHERING, INTERLACING, PGXP_DEPTH). Explicit offset
      via GL_ARB_enhanced_layouts is core in GLSL 4.40+ and
      supported by Vulkan GLSL.
    - v_uv_limits input declaration drops its
      #if defined(UV_LIMITS) guard - always declared,
      matching the always-emitted output from the
      post-collapse batch VS.
    - Body sample/clamp block: the two-branch
      #if UV_LIMITS / #else compile-time fork collapses to
      a single shape with a runtime `if (u_uv_limits != 0u)`
      branch. The "scale uv_limits up by RESOLUTION_SCALE
      for non-palette modes" computation moves inside the
      branch so it only runs when the values are actually
      used.
    - Header comment: 5 axes / 48 blobs -> 4 axes / 24 blobs.

  tools/regen_vulkan_spirv.py
    - _batch_fs_textured_nearest_variants() drops the
      uv_axes dimension. The Nearest cube is now in parity
      shape with the Bilinear / JINC2 / xBR templates
      (UV_LIMITS implicit on those by ShouldUseUVLimits()
      coupling - TEXTURE_FILTERING != Nearest forces
      m_using_uv_limits true, so they never needed a UV
      variant axis).

  src/common/vulkan/embedded_spirv/
    - 24 new batch_textured_nearest_fs_*.inc files (no
      _nouv / _uv suffix) with the collapsed-shape SPIR-V.
    - 48 deleted batch_textured_nearest_fs_*_nouv.inc and
      _uv.inc files (the obsolete two-shape variants).
    - 151 .inc files in the directory total (was 175;
      the VS collapse already took it from 181).

  src/common/vulkan/embedded_shaders.{h,cpp}
    - k_batch_textured_nearest_fs_blobs[48] ->
      k_batch_textured_nearest_fs_blobs[24]. The table loses
      the alternating _nouv / _uv pairs; the surviving
      entries match the new filename layout.
    - GetBatchTexturedNearestFragmentShaderBlob drops the
      bool uv_limits parameter. Index encoding becomes
        index = interp*8 + persp*4 + dual*2 + pgxp  (0..23)
      (was: interp*16 + persp*8 + dual*4 + pgxp*2 + uv).
    - 48 #include lines collapse to 24, matching the
      regenerated filename set.
    - Header docstring updated to match (5 axes -> 4, blob
      array size, UV_LIMITS note added).

  src/core/gpu_hw_vulkan.cpp
    - The single GetBatchTexturedNearestFragmentShaderBlob
      call site (the m_texture_filter == Nearest branch of
      the per-pipeline FS lookup) drops its sixth argument
      (m_using_uv_limits). Bilinear / JINC2 / xBR call sites
      are unchanged - those helpers never had uv_limits as
      an argument because their templates already had
      UV_LIMITS implicit.

  src/core/gpu_hw.cpp
    - non_dim_diff in UpdateHWSettings drops the
      m_using_uv_limits != use_uv_limits clause.
    - shader_source_changed in UpdateHWSettings drops the
      same clause.
    - display_only_source_changed's other_source_diff
      drops it too.
    All three trigger predicates are now uv_limits-blind.
    m_using_uv_limits flips no longer invalidate the shader
    cache or PSO matrix on any backend.
    m_using_uv_limits itself stays as a member field -
    UpdateHWSettings still computes use_uv_limits via
    ShouldUseUVLimits() and assigns it for the runtime
    ComputePolygonUVLimits CPU-side decision and for the
    u_uv_limits cbuffer push - but it no longer triggers
    rebuilds.

What this completes:

  The UV_LIMITS axis is now fully routed across all four
  backends.
    D3D11 / D3D12 / OpenGL (C++ shadergen path):
      - Shader source invariant under m_uv_limits since
        8b43518.
      - Input layout binds ATTR4 unconditionally when
        textured (D3D11 / D3D12 since 8b43518; OpenGL was
        already unconditional).
    Vulkan (pre-baked SPIR-V path):
      - VS shape collapsed since 078783e (12 VS blobs).
      - Nearest FS shape collapsed in this commit (24 FS
        blobs).
      - Bilinear / JINC2 / xBR templates unchanged - they
        never had UV_LIMITS as an axis (settings-coupling
        guarantee).
      - Vulkan FS UBO grows by one field (u_uv_limits at
        offset 56); other Vulkan FS templates that don't
        read UV limits don't need to redeclare it - they
        just ignore the extra bytes in the backing buffer.
    All backends:
      - m_using_uv_limits is no longer a rebuild trigger.

Cost matrix delta:

  Per-session flip that touches m_using_uv_limits (e.g.
  PGXP on / off under filter Nearest, or filter cross-
  Nearest transition):
    Pre-commit (Vulkan only): 24 Nearest FS pipelines per
      cell rebuilt to swap from _nouv to _uv variants.
      Tens of milliseconds cold, instant with disk
      pipeline cache.
    Post-commit: 0 FS rebuilds across all backends. The
      u_uv_limits cbuffer write picks up at next
      FlushRender.

  Cold launch precompile pass on Vulkan: 24 fewer Nearest
  FS pipelines to enumerate. Tens of ms on cold cache.

  Steady-state runtime cost (Vulkan Nearest FS):
    + 4-byte UBO read on every fragment.
    + One branch on u_uv_limits (sub-coherent across the
      warp because u_uv_limits is uniform).
    + The "scale uv_limits up by RESOLUTION_SCALE" math
      moved inside the branch, so when u_uv_limits == 0
      that work is also skipped - net cost reduction on
      the non-UV path.

Disk footprint:
  Before:  175 .inc files, ~12.9 MiB.
  After:   151 .inc files, ~11.3 MiB. (-24 files, ~1.6
           MiB. The 24 new files are larger than the 24
           old _nouv files they implicitly replace because
           they always emit the v_uv_limits flat input
           declaration and the body branch.)

Test:
  - Build: tools/regen_vulkan_spirv.py runs cleanly,
    producing 151 blobs. Embedded_shaders.cpp compiles
    against the smaller table. gpu_hw_vulkan.cpp link is
    clean.
  - Boot any PSX title with PGXP off + filter Nearest:
    u_uv_limits = 0, the FS branch takes the no-clamp
    path. Output identical to pre-commit.
  - Toggle PGXP on with filter Nearest: u_uv_limits flips
    to 1 via the existing UpdateHWSettings cbuffer push;
    the FS picks the clamp path on next FlushRender. No
    pipeline rebuild fired.
  - Toggle filter to Bilinear: m_texture_filtering changes,
    which still fires shader_source_changed (filter is a
    distinct rebuild trigger). The Bilinear FS picks up
    its template normally, which always uses UV limits
    (settings coupling guarantee).
  - Cross-test on non-Vulkan backends: D3D11 / D3D12 /
    OpenGL also no longer rebuild on UV_LIMITS flip alone
    (the trigger drop in gpu_hw.cpp affects them too).
    Their shader sources were already invariant since
    8b43518, so the only behaviour change is the
    rebuild-trigger removal.

Next:
  - PGXP_DEPTH for the FS (always-emit SV_Depth / FragDepth
    perf measurement first - the FS keeps captured
    m_pgxp_depth because the !m_pgxp_depth value gates
    DeclareFragmentEntryPoint's depth_output parameter,
    which controls whether SV_Depth is declared in the FS
    entry signature; routing that requires either always-
    emitting SV_Depth or hoisting that decision through
    the DeclareFragmentEntryPoint surface).
  - per_sample / dcp collapse decisions (forced-always
    via settings or keep as variant axes).
  - Pre-bake the resulting reduced D3D12 FS matrix.
2026-05-19 03:07:27 +02:00
LibretroAdmin
078783e20b core/vulkan: drop the UV_LIMITS attribute-layout VS variant
Follow-up to 8b43518 (UV_LIMITS routing for the C++ shadergen,
D3D11 / D3D12 / OpenGL only). Vulkan has its own pre-baked
SPIR-V pipeline rooted in data/shaders/vulkan/*.glsl, compiled
via tools/regen_vulkan_spirv.py, and indexed by per-axis blob
tables in src/common/vulkan/embedded_shaders.cpp. This commit
parallels the C++ shadergen routing on the VS side: collapses
the UV_LIMITS attribute-layout variant from the batch VS so
that "textured" always means "textured with v_uv_limits", and
the C++ Vulkan input layout binds ATTR4 unconditionally when
textured.

The FS-side UV_LIMITS axis (the Nearest filter's two-variant
nouv / uv expansion) is NOT in this commit. Routing it
through a UBO field requires extending the Vulkan batch FS
UBO declaration with u_uv_limits, body-level runtime
branches, and regenerating the 48 Nearest FS .inc blobs. That
is the natural next commit; this one limits the blast radius
to the VS side, which is a simpler structural change with
fewer downstream touch points.

What changes:

  data/shaders/vulkan/batch.vert.glsl
    - Drops the #if defined(UV_LIMITS) guards in three sites
      (a_uv_limits input declaration, v_uv_limits varying
      output declaration, v_uv_limits assignment in main()).
      When TEXTURED, a_uv_limits / v_uv_limits are always
      declared and v_uv_limits is always written.
    - Header comment updated from "3 attribute layouts" to
      "2 attribute layouts" and the total variant count from
      18 to 12.

  tools/regen_vulkan_spirv.py
    - _batch_vs_variants(): drops the third attr_axes entry
      ("textured_uvlim", ["TEXTURED", "UV_LIMITS"]). The
      remaining "textured" entry's GLSL output now always
      includes the a_uv_limits / v_uv_limits declarations
      because the source-side #if guards are gone, so the
      single "textured" variant produces the same SPIR-V that
      "textured_uvlim" used to. Variant count drops from 18
      to 12.
    - The Nearest FS variants table is unchanged (still has
      the UV_LIMITS axis); collapsing that side is the
      follow-up.

  src/common/vulkan/embedded_spirv/
    - Six batch_vs_textured_uvlim_*.inc files DELETED (no
      longer in the regen output set).
    - Six batch_vs_textured_*.inc files REGENERATED with the
      new GLSL that always emits a_uv_limits / v_uv_limits.
      Content shape: same length / structure as the old
      "textured_uvlim" .inc files (since both correspond to
      the "textured + UV_LIMITS" build); the OLD plain
      "textured" .inc files (the smaller no-UV variant) are
      effectively replaced.
    - Other 169 blobs untouched. Total goes 181 -> 175.

  src/common/vulkan/embedded_shaders.{h,cpp}
    - k_batch_vs_blobs[18] -> k_batch_vs_blobs[12]. The
      table loses the "attr = 1 (textured, no UV limits)"
      block; the surviving "attr = 1" block is what used to
      be "attr = 2 (textured + UV limits)", just renamed for
      cleaner end state.
    - GetBatchVertexShaderBlob() drops the bool uv_limits
      parameter. Index encoding becomes
        attr = textured ? 1 : 0  (was: !textured ? 0 :
                                  (uv_limits ? 2 : 1))
      Same interp / persp arithmetic.
    - Header docstring and #include block updated to match
      (six fewer #includes; the six uvlim ones).

  src/core/gpu_hw_vulkan.cpp
    - The single GetBatchVertexShaderBlob() call site at the
      CompilePipelines VS-fill loop drops its second
      argument (m_using_uv_limits) so the new four-arg
      signature is satisfied.
    - The input-layout builder at the per-pipeline
      gpbuilder.AddVertexAttribute(4, ...) site drops its
      `if (m_using_uv_limits)` guard. ATTR4 / a_uv_limits is
      now bound unconditionally when textured, matching the
      always-emitted shader input. BatchVertex carries the
      uv_limits field unconditionally (gpu_hw.h:47), so the
      binding always points at valid memory; the FS-side
      u_uv_limits scalar still decides whether the value is
      meaningful.

What does NOT change in this commit:

  - The Vulkan batch FS Nearest variants still have
    UV_LIMITS as a build axis. m_using_uv_limits is still
    threaded to the FS blob lookup at
    gpu_hw_vulkan.cpp:2139 (now line ~2140 after the diff).
    The Nearest FS GLSL still has #if defined(UV_LIMITS)
    guards. The 48 nearest_uv / nearest_nouv blobs remain.
    Routing that side is the natural next commit; the
    delta there is much larger (48 blobs, FS UBO grows,
    runtime branch in the Nearest body).
  - m_using_uv_limits STAYS in shader_source_changed across
    backends. Vulkan still needs it to pick the right
    Nearest FS blob, so the rebuild trigger has to stay
    until the Nearest FS routing also lands.
  - m_using_uv_limits STAYS as a member field. Still
    drives the per-vertex ComputePolygonUVLimits decision
    (gpu_hw.cpp:740) and still passed into the FS variant
    lookup. The VS lookup no longer takes it.
  - Bilinear / JINC2 / xBR FS templates: untouched. They
    already assumed UV_LIMITS via the settings-layer
    ShouldUseUVLimits() coupling (TEXTURE_FILTERING !=
    Nearest implies m_using_uv_limits = true), so they
    never had a UV_LIMITS variant axis to collapse.

Variant-count delta:

  Batch VS: 18 -> 12 SPIR-V blobs. Pipeline cache footprint
  drops by 6 VS pipelines per (filter, render, texture)
  cell pair (filter side is per-FS so doesn't multiply VS
  directly, but the VkPipeline cache key combines them and
  6 fewer VS blobs mean 6 fewer paths through the
  pipeline-create call).

Disk SPIR-V footprint:
  Before: 181 .inc files, 13 MiB total.
  After:  175 .inc files, 13 MiB - ~120 KiB = ~12.9 MiB.
  (Six VS blobs at ~120 KiB each, ~2-2.5 KiB SPIR-V plus
  the C-array boilerplate.)

Cost matrix delta (Vulkan):

  UV_LIMITS flip mid-session (in practice triggered by PGXP
  toggle or filter cross-Nearest transition, both of which
  separately trigger m_using_uv_limits flips):
    Pre-commit: VS pipeline-cache lookup picks a different
                blob from the 18-entry table on flip.
                Pipeline creation cost on cold cache:
                ~2 vkCreateGraphicsPipelines for the two
                new (textured, textured_uvlim) VS variants
                if both end up needed.
    Post-commit: VS pipeline-cache lookup is invariant
                 under m_using_uv_limits at the VS axis;
                 the FS axis still flips. Saves ~2 cold VS
                 pipeline creates on the toggle. Symbolic
                 (small absolute cost) but it unlocks the
                 next Nearest-FS routing commit, which is
                 the actual large win on the Vulkan side.

  Cold launch (precompile pass): 6 fewer VS pipelines to
  enumerate / create. Probably tens of ms on cold cache.

  Steady-state runtime cost: unchanged. The VS uniform
  always-emits-v_uv_limits adds one MUL per vertex and one
  vec4 of interpolant bandwidth, both sub-noise.

Test:
  - Build: tools/regen_vulkan_spirv.py runs cleanly with
    glslangValidator from the Vulkan SDK / glslang-tools
    package, producing 175 blobs. The Vulkan backend builds
    and links against the smaller blob table.
  - Boot any PSX title with PGXP off + filter Nearest:
    untextured / textured paths render identically. The
    "textured" VS variant is now the always-UV blob;
    BatchVertex.uv_limits is zero on these vertices (no
    ComputePolygonUVLimits ran), v_uv_limits is propagated
    as zero, and the Nearest FS (still UV_LIMITS-keyed in
    this commit) picks its "nouv" variant.
  - Toggle PGXP on or filter to Bilinear:
    m_using_uv_limits flips to true, the Nearest FS picks
    its "uv" variant, the VS variant is still the same
    (post-commit there's only one textured VS variant).
    Output identical to pre-commit equivalent state.
  - Spec const flips (PGXP_DEPTH, RESOLUTION_SCALE) at
    pipeline-create time: still produce the right pipeline
    via the smaller blob table.

Next:
  - Vulkan Nearest FS UV_LIMITS routing: extend Vulkan FS
    UBO with u_uv_limits, drop the #if defined(UV_LIMITS)
    guards from batch_textured_nearest.frag.glsl, runtime
    branch in the body, regen 48 -> 24 Nearest FS blobs,
    drop m_using_uv_limits from the FS lookup +
    shader_source_changed. After that, all backends have
    UV_LIMITS fully routed.
  - PGXP_DEPTH for the FS (always-emit SV_Depth / FragDepth
    perf measurement first).
  - Pre-bake commits once remaining axes are decided.
2026-05-19 02:54:36 +02:00
LibretroAdmin
80d0495145 core/d3d12: pre-bake the display pixel shader
The prize set. display_ps is the per-frame hot shader - every
frame goes through this on its way to the host display, so cold-
flip cost matters here more than anywhere else in the GPU
pipeline. This commit pre-bakes 54 variants covering the full
runtime configuration matrix and replaces the shadergen +
m_shader_cache.GetPixelShader path in GetDisplayPipeline with
indexed-lookup into two static arrays of D3D12_SHADER_BYTECODE.

Variant axes (4 axes, 54 unique blobs):

  * DEPTH_24BIT (2 values): 16-bit framebuffer (the common case;
    every game's standard scanout) vs 24-bit (FMV cutscenes,
    games that opt into true-colour framebuffers).

  * interlace_mode (3 values): InterlacedRenderMode enum from
    gpu_hw.h. Mapped to a (INTERLACED, INTERLEAVED) bool pair
    in the variant /D defines:
      i=0 (None):              (INTERLACED=0, INTERLEAVED=0)
      i=1 (InterleavedFields): (INTERLACED=1, INTERLEAVED=1)
      i=2 (SeparateFields):    (INTERLACED=1, INTERLEAVED=0)

  * SMOOTH_CHROMA (2 values): chroma-plane smoothing in the
    24-bit path. Heavy when active - SampleVRAM24Smoothed runs
    9 SampleVRAM24 calls per output pixel (1 centre + 4 x 2x2
    averages), each of which is 2 LoadVRAM calls. Dead code
    when DEPTH_24BIT=0; the variant table omits the c=1 entry
    for d=0 (54 = 18 + 36, not 72 = 2 x 3 x 2 x 6).

  * MULTISAMPLES (6 values: 1, 2, 4, 8, 16, 32): MSAA sample
    count. Same cardinality axis as vram_read_ps (21f4623).
    Composed multiplicatively with SMOOTH_CHROMA: at
    /DSMOOTH_CHROMA=1 /DMULTISAMPLES=32 the inner loop unrolls
    to 9 * 2 * 32 = 576 texture loads per pixel - the d1i?c1m32
    blobs are correspondingly the largest in the set (99 kB
    each).

Variant suffix convention: d{0,1}i{0,1,2}c{0,1}m{01,02,04,08,16,
32}. MULTISAMPLES padded to 2 digits so alphabetical .inc
filename ordering matches the natural nested-loop iteration
order. The d=0 set has 18 entries (one chroma value); d=1 has
36 (both chroma values).

Blob size distribution:
  smallest:  1276 B  d0i0c0m01  (16-bit, no interlace, no MSAA)
  median:   ~3500 B
  largest: 99152 B  d1i2c1m32  (24-bit, SeparateFields, smooth
                                chroma, 32x MSAA - 576 loads/px)
  total:    942 kB of DXBC across 54 .inc files
  on-disk: ~5.6 MB of .inc text (the hex-encoding overhead is
           ~6x vs the binary - "0xXX, " per byte - same shape
           as the vram_read note in 21f4623)

Runtime selection in GetDisplayPipeline:

  if (depth_24) -> k_display_d1[interlace_mode][chroma][ms_idx]
  else          -> k_display_d0[interlace_mode][ms_idx]

Two arrays rather than one ragged 4D, because the smooth_chroma
dimension is structurally absent on the d=0 path (SMOOTH_CHROMA
is dead code there; fxc /O3 would have produced identical DXBC
for c=0 and c=1 anyway, so we save 18 blobs / ~30 kB DXBC by
not baking them). ms_idx is log2(m_multisamples) computed by a
function-scope kMultisamplesIndex lambda - same predicate /
fallback shape as the vram_read pre-bake. The arrays are
function-scope static const so they initialise on first call
and the cost is amortised across the per-frame display PSO
stream for the rest of the session.

Files added:

* data/shaders/d3d12/display.ps.hlsl: standalone HLSL matching
  GenerateDisplayFragmentShader's D3D12 output post-e64fc28
  cbuffer routing. 24-byte cbuffer (u_vram_offset.xy,
  u_crop_left, u_field_offset, u_resolution_scale, u_pad0)
  layout-identical to the GPU_HW_D3D12::UpdateDisplay
  SetGraphicsRoot32BitConstants push. RGBA8ToRGBA5551,
  RGBToYUV, YUVToRGB, fixYCoord helpers all inlined.
  SampleVRAM24, SampleVRAMAverage2x2, SampleVRAM24Smoothed
  match the shadergen body byte-for-byte.

* src/common/d3d12/embedded_dxbc/display_ps_d{0,1}i{0,1,2}c{0,1}
  m{01,02,04,08,16,32}.inc: 54 DXBC blobs (with the
  smooth-chroma collapse on the d=0 path). All built with
  Microsoft fxc; full RDEF/ISGN/OSGN/SHEX/STAT chunk set; all
  54 stamped "Microsoft (R) HLSL Shader Compiler 10.1". Validator
  (check_microsoft_creator in tools/regen_d3d12_dxbc.py)
  passes all 54.

Files modified:

* tools/regen_d3d12_dxbc.py: TEMPLATE_VARIANTS gains the
  display.ps.hlsl entry with 54 rows. Comment explains the
  interlace_mode -> (INTERLACED, INTERLEAVED) mapping and the
  d=0 chroma collapse.

* src/common/d3d12/embedded_shaders.h: 108 extern declarations
  (54 array + 54 size_bytes). Header docblock explains the
  variant axis breakdown, the suffix convention, and the
  two-array runtime selection.

* src/common/d3d12/embedded_shaders.cpp: 54 #include lines for
  the new .inc files, inside namespace D3D12::EmbeddedShaders.

* src/core/gpu_hw_d3d12.cpp GetDisplayPipeline: shadergen call
  + m_shader_cache.GetPixelShader path replaced with the
  kMultisamplesIndex lambda + two static arrays + ternary
  indexed-lookup. The m_shadergen-not-yet-built check is
  dropped for this path since the embedded blobs are
  statically linked. Pipeline build downstream is unchanged
  (same root signature, RT format, blend/depth state).

Cost matrix delta (D3D12) for display_ps:
  Cold-flip multisamples toggle: ~50 ms x 6 D3DCompile +
                                 ~10 ms x 6 PSO
                                 -> ~10 ms x 6 PSO only.
  (Multisamples rebuild touches the full 2x3 display PSO
  matrix at the new sample count.)
  Cold-flip smooth_chroma toggle: ~50 ms x 2 D3DCompile +
                                  ~10 ms x 2 PSO
                                  -> ~10 ms x 2 PSO only.
  (Only the 2 d=1 PSOs are affected; d=0 PSOs are
  chroma-independent.)
  Disk-cache-warm: ~1 ms each -> 0 ms.
  Resolution scale toggle: 0 ms (cbuffer-routed via e64fc28).
  Per-frame PSO selection: previously a shader_cache hash
  lookup; now an array index. ~ns vs ~us delta - negligible.

The shadergen GenerateDisplayFragmentShader function is left
intact in gpu_hw_shadergen.cpp - the other three backends still
call it through their runtime shader-cache paths. Only D3D12
bypasses it now. This is the final shader on the non-batch
pre-bake roadmap.

Test:
  - Build: D3D12 backend links without unresolved symbols
    across all 54 new externs. Specific risk: a typo in any of
    the 108 extern names or 54 includes would surface here.
  - Runtime, default config (MSAA off, no chroma smoothing):
    every game must continue to scanout correctly. Failure
    modes if the d=0 path is wrong:
      * Garbled framebuffer / wrong colours on the per-frame
        display.
      * Interlace fields rendered wrong - test signal: any PSX
        game in interlaced output mode (most TV-target games).
        Wrong interlace mapping would manifest as field
        comb-tearing or half-height output.
  - Runtime, 24-bit colour: trigger any FMV. Standard test
    cases:
      * Sony intro bookend FMV - leaves no doubt the 24-bit
        path is hit.
      * In-game cutscene FMVs (FF7, MGS, Resident Evil).
    Failure mode if d=1 c=0 path wrong: FMV plays but colours
    are wrong (off hue, mismatched chroma).
  - Runtime, 24-bit + chroma smoothing: enable the "24-bit
    Chroma Smoothing" core option, then play FMV. Failure mode
    if d=1 c=1 path wrong: FMV plays with sharp chroma edges
    (the smoothing isn't happening) or with garbled output
    (variant table miswired).
  - MSAA at each level (2x/4x/8x/16x/32x): same screenshot
    tests at each. Failure mode for any individual blob:
    visible MSAA averaging artefacts in the displayed image.
    32x specifically validates that kMultisamplesIndex picks
    ms_idx=5 - wrong-side switch would index m16 (4) instead
    of m32 (5).
  - All combinations: display_ps is selected freshly each
    UpdateDisplay call, so the array indexing is exercised
    every frame. Any mis-indexed combo would be visually
    obvious.
  - Chroma toggle: flip "24-bit Chroma Smoothing" mid-FMV. The
    next display PSO selection picks blobs from k_display_d1
    [interlace_mode][1] instead of [0]. Failure mode: chroma
    smoothing doesn't toggle, or wrong PSO selected leading to
    garbled output.

If everything works, the only remaining pre-bake target is the
batch matrix - the ~1 min cold-flip prize, hundreds of variants.
That's the final arc of this whole sequence.
2026-05-19 00:22:35 +02:00
LibretroAdmin
21f46231f5 core/d3d12: pre-bake the VRAM read pixel shader
First MSAA-count cardinality variant. Where vram_update_depth_ps
(91f8c32) split on a binary MULTISAMPLING flag (Texture2D vs
Texture2DMS<float4>), vram_read_ps splits on the MULTISAMPLES
*count* itself - the [unroll] sample-resolve loop in the shader's
LoadVRAM helper unrolls a different number of times per variant.
Each power-of-2 sample count produces a distinct DXBC; the m1
blob takes the Texture2D path with a single Load, while m2..m32
all use Texture2DMS<float4> with different unroll counts in the
sample-averaging loop.

vram_read_ps is the upscaled-VRAM-to-native-16bpp encode pass
that backs screenshot capture, libretro readback, and save-state
staging. It's not in the per-frame hot path - it only runs when
the frontend explicitly requests VRAM contents - but every
MULTISAMPLES variant must be statically available because
m_multisamples can change between runs and pre-bake decisions
are made at link time.

Variant axis: MULTISAMPLES = 1, 2, 4, 8, 16, or 32 (6 blobs).
MULTISAMPLING is derived in-shader (#if MULTISAMPLES > 1) rather
than passed as a separate /D, since the binding type and the
unroll count are both functions of the same value.

Cbuffer is post-2980961 (24 bytes: u_base_coords, u_size,
u_resolution_scale, u_pad0), so RESOLUTION_SCALE is a runtime
cbuffer value and the box-filter loops in SampleVRAM remain
runtime loops (not [unroll]'d). Resolution scale therefore
doesn't multiply the variant count.

Blob sizes scale linearly with MULTISAMPLES because of the
[unroll] of the sample-resolve loop:

  vram_read_ps_m1.inc   3636 B  (Texture2D path, no loop)
  vram_read_ps_m2.inc   3996 B  (2 sample loads + averaging)
  vram_read_ps_m4.inc   4572 B  (4 sample loads)
  vram_read_ps_m8.inc   5724 B  (8 sample loads)
  vram_read_ps_m16.inc  8028 B  (16 sample loads)
  vram_read_ps_m32.inc 12636 B  (32 sample loads)

Total embedded bytecode footprint ~ 38 KB across the 6 blobs.
The .inc text-file sizes (22 KB - 77 KB) are larger than the
binary sizes by ~6x due to the hex-formatting overhead of the
.inc representation ("0xXX, " per byte) - this is invariant
across all pre-bakes and is reflected in the on-disk repo
footprint, not the runtime memory footprint.

Files added:

* data/shaders/d3d12/vram_read.ps.hlsl: standalone HLSL source
  matching GenerateVRAMReadFragmentShader's D3D12 output
  post-2980961. 24-byte cbuffer (u_base_coords.xy, u_size.xy,
  u_resolution_scale, u_pad0) layout-identical to the
  GPU_HW_D3D12::ReadVRAM SetGraphicsRoot32BitConstants push.
  RGBA8ToRGBA5551 helper inlined. LoadVRAM helper's MS-path
  loop is [unroll]'d (MULTISAMPLES is a compile-time /D);
  SampleVRAM's box-filter loops are runtime loops over
  u_resolution_scale.

* src/common/d3d12/embedded_dxbc/vram_read_ps_{m1,m2,m4,m8,m16,
  m32}.inc: 6 DXBC blobs, one per MULTISAMPLES value. All built
  with Microsoft fxc; full RDEF/ISGN/OSGN/SHEX/STAT chunk set;
  creator stamped "Microsoft (R) HLSL Shader Compiler 10.1".
  Validator (check_microsoft_creator in
  tools/regen_d3d12_dxbc.py) passes all 6.

Files modified:

* tools/regen_d3d12_dxbc.py: TEMPLATE_VARIANTS gains the
  vram_read.ps.hlsl entry with 6 rows (m1, m2, m4, m8, m16, m32).

* src/common/d3d12/embedded_shaders.h: 6 extern declarations
  added with their _size_bytes companions. Header docblock
  explains the cardinality-axis distinction vs the binary-axis
  variants above.

* src/common/d3d12/embedded_shaders.cpp: 6 #include lines.

* src/core/gpu_hw_d3d12.cpp GetVRAMReadbackPipeline: shadergen
  call + m_shader_cache.GetPixelShader path replaced with a
  switch on m_multisamples picking between the 6 blobs. Default
  case is a safety net that falls back to the m1 blob with a
  warn log - in practice every supported config falls into a
  named case because the libretro UI dropdown is restricted to
  the same 6 values the switch handles, and the GPU driver-
  capability detection in CreateResources only reports
  power-of-2 counts as supported. The
  m_shadergen-not-yet-built check is dropped for this path since
  the embedded blobs are statically linked.

Cost matrix delta (D3D12) for vram_read_ps:
  Cold-flip multisamples toggle: ~50 ms x N D3DCompile + ~10 ms
                                 x N PSO -> ~10 ms x N PSO only.
  (N = number of distinct MULTISAMPLES values cycled through in
  one session; typically 1-2 if the user picks one MSAA setting
  and keeps it.)
  Disk-cache-warm: ~1 ms each -> 0 ms.
  Resolution scale toggle: 0 ms (cbuffer-routed, no PSO change).

The shadergen GenerateVRAMReadFragmentShader function is left
intact in gpu_hw_shadergen.cpp - the other three backends still
call it through their runtime shader-cache paths. Only D3D12
bypasses it now.

Test:
  - Build: D3D12 backend links without unresolved symbols across
    all 6 new externs.
  - Runtime, MSAA disabled: trigger any readback path. Concrete
    cases:
      * RetroArch "Take Screenshot" hotkey - reads VRAM and
        encodes to PNG. Failure mode if m1 blob is wrong:
        garbled screenshot or wrong colours.
      * Save state - dumps VRAM into the savestate blob.
        Failure mode: load-state restores garbled VRAM.
  - Runtime, MSAA enabled at each level (2x/4x/8x/16x/32x):
    same screenshot / savestate tests. Failure mode: VRAM
    averaging incorrect; per-sample data not properly
    collapsed. Specifically:
      * Slightly off colours on edges of polygons (the MS
        averaging path is what makes upscaled VRAM look
        coherent vs the native 1024x512 readback).
      * 32x specifically tested for unroll-count correctness -
        if the runtime selects m16 by mistake when m_multisamples
        is 32, the readback would load 16 samples instead of 32
        and divide by 16 instead of 32, producing visibly darker
        readback output.
  - Resolution-scale toggle 1x <-> 8x: no PSO recompile (blob
    is scale-independent post-2980961); next readback uses the
    same PSO but different u_resolution_scale value through
    the cbuffer. SampleVRAM's box-filter loops scale at runtime.

Next per the same template now that cardinality-axis is
validated: display_ps (12 x M variants on depth_24 x
interlace_mode x smooth_chroma x MSAA). Largest blob set per
shader. After that the only remaining pre-bake target is the
batch matrix - the ~1 min cold-flip prize, hundreds of variants.
2026-05-18 23:42:30 +02:00
LibretroAdmin
91f8c32ef0 core/d3d12: pre-bake the VRAM update-depth pixel shader
First MSAA texture-binding variant pre-bake. Where vram_copy /
vram_write / vram_fill split on PGXP_DEPTH / WRAPPED / INTERLACED
which are body-branch flags only, vram_update_depth_ps splits on
MULTISAMPLING which changes the *binding type* of samp0
(Texture2D vs Texture2DMS<float4>) plus adds a conditional
SV_SampleIndex input to the entry-point signature. This exercises
the indexed-lookup runtime selection in territory that vram_read_ps
and display_ps will need too.

vram_update_depth runs the depth-only pass that propagates colour-
texture alpha (the PSX mask bit) into the depth buffer after every
CPU->VRAM upload. It's hit on every UpdateVRAM completion path in
games that toggle the mask bit, which means most native rendering
plus any title's transparency layer.

Variant axis: MULTISAMPLING on/off only (2 blobs). msaa0 is the
Texture2D path with .Load(int3(coords, mip)) form; msaa1 is the
Texture2DMS<float4> path with .Load(coords, sample) form and
SV_SampleIndex as an additional entry-point input. HLSL forces
per-sample shading on the msaa1 variant because SV_SampleIndex is
declared, so the PSO MSAA configuration (gpbuilder.SetMultisamples)
must match the shader's per-sample expectation - runtime selection
uses m_multisamples > 1 (same predicate as the shadergen
UsingMSAA() helper).

Files added:

* data/shaders/d3d12/vram_update_depth.ps.hlsl: standalone HLSL
  matching GenerateVRAMUpdateDepthFragmentShader's D3D12 output.
  No cbuffer - the shadergen function never calls
  DeclareUniformBuffer. v_tex0 declared but unused (matches the
  shadergen entry-point template which emits texcoord inputs
  unconditionally; the optimiser drops the dead read). #if
  MULTISAMPLING block switches both the binding and the body Load
  form; the SamplerState is declared for both variants to match
  shadergen byte-for-byte (Texture2DMS doesn't need a sampler for
  Load, but the dead binding is free at runtime).

* src/common/d3d12/embedded_dxbc/vram_update_depth_ps_msaa0.inc:
  DXBC for /DMULTISAMPLING=0 (Texture2D path). 672 bytes.
* src/common/d3d12/embedded_dxbc/vram_update_depth_ps_msaa1.inc:
  DXBC for /DMULTISAMPLING=1 (Texture2DMS<float4> path). 732
  bytes (60B larger for the per-sample shading flag in STAT plus
  the SV_SampleIndex input in ISGN).

Both built with Microsoft fxc; full RDEF/ISGN/OSGN/SHEX/STAT
chunk set; creator stamped "Microsoft (R) HLSL Shader Compiler
10.1". Validator (check_microsoft_creator in
tools/regen_d3d12_dxbc.py) passes both.

Files modified:

* tools/regen_d3d12_dxbc.py: TEMPLATE_VARIANTS gains the
  vram_update_depth.ps.hlsl entry with two rows (msaa0, msaa1).

* src/common/d3d12/embedded_shaders.h: 2 extern declarations
  added. Header docblock explains the MSAA binding-type difference
  vs the body-branch variants above.

* src/common/d3d12/embedded_shaders.cpp: 2 #include lines for
  the new .inc files.

* src/core/gpu_hw_d3d12.cpp GetVRAMUpdateDepthPipeline:
  shadergen call + m_shader_cache.GetPixelShader path replaced
  with a ternary on m_multisamples > 1 picking between the two
  D3D12_SHADER_BYTECODE structs. gpbuilder.SetMultisamples is
  unchanged - it drives the PSO MSAA state from m_multisamples
  directly. The m_shadergen-not-yet-built check is dropped for
  this path since the embedded blobs are statically linked.

The "match the PSO MSAA configuration to the shader expectation"
discipline is the load-bearing piece here. SV_SampleIndex in the
shader forces per-sample shading; the PSO must be configured with
matching MSAA sample count or the runtime will reject PSO creation
(D3D12 PSO validation does cross-check the rasterizer sample count
against the PS expectation). The pattern below scales naturally
to vram_read_ps and display_ps where M (the sample count itself)
also varies.

Cost matrix delta (D3D12) for vram_update_depth_ps:
  Cold-flip multisamples toggle: ~50 ms D3DCompile + ~10 ms PSO
                                 -> ~10 ms PSO only.
  Disk-cache-warm: ~1 ms -> 0 ms.
  Resolution scale toggle: 0 ms (no scale dependency).
  Note: the multisamples toggle itself triggers a much larger
  rebuild cascade (every MSAA-sensitive PSO across the GPU
  matrix), so vram_update_depth_ps's share is small in absolute
  terms - but it's the first proof point for the technique.

The shadergen GenerateVRAMUpdateDepthFragmentShader function is
left intact in gpu_hw_shadergen.cpp - OpenGL / Vulkan / D3D11
still call it through their runtime shader-cache paths. Only
D3D12 bypasses it now.

Test:
  - Build: D3D12 backend links without unresolved symbols.
    Specific risk: a typo in the MSAA-path entry signature would
    surface here (SV_SampleIndex semantic name, ordering vs
    SV_Position).
  - Runtime, MSAA disabled (default): every CPU->VRAM upload
    must continue to propagate mask bits correctly. Test signal:
    any game with mask-bit-using transparent sprites (FF7 spell
    effects, Tekken 3 ring-out smoke, MGS guard-search beams).
    Failure mode: transparency rendering wrong (sprites drawn at
    wrong depth, mask-bit-disabled pixels showing through layers
    that should occlude them).
  - Runtime, MSAA enabled (Multi-Sample Antialiasing core option
    set to 2x / 4x / 8x): same transparency tests, but per-sample
    depth propagation. The msaa1 blob is selected and the PS runs
    once per sample. Failure mode would be MSAA edges around
    transparent sprite boundaries flashing or disappearing because
    per-sample depth wasn't propagated.
  - MSAA toggle (1x -> 4x): triggers full PSO rebuild cascade as
    today. The vram_update_depth_ps portion now picks msaa1
    blob[1] instead of falling through D3DCompile. Cold-flip
    time drops by the D3DCompile share (~50 ms for this one
    shader).
  - Resolution scale toggle: no PSO recompile (blob is scale-
    independent); next UpdateVRAM completion uses the same PSO.

Next per the same template now that MSAA-binding is validated:
vram_read_ps (M MSAA variants on top of the 2980961 cbuffer
routing - first multi-axis MSAA-binding variant), then display_ps
(12xM variants - the prize set).
2026-05-18 23:27:45 +02:00
LibretroAdmin
22d18291e7 core/d3d12: pre-bake the VRAM fill pixel shader
First multi-axis pre-bake. Where vram_copy (cb4b443) and vram_write
(ba013aa) each had a single PGXP_DEPTH dimension and 2 blobs,
vram_fill is 3 axes (PGXP_DEPTH x WRAPPED x INTERLACED) for 8 blobs
total. Exercises the TEMPLATE_VARIANTS table for the first time
with a nested variant space and validates the indexed-lookup
runtime selection pattern that vram_update_depth / vram_read /
display will need for their MSAA splits.

vram_fill is hit on every screen-clear that the game submits, plus
FillVRAM operations from CPU DMA writes - common games that clear
every frame (most 3D titles) hit this constantly, but even
non-clearing-every-frame titles (2D scrollers, JRPG menus) hit it
on scene transitions. Pre-baking removes another D3DCompile from
cold-flips and another path exposed to shader-cache invalidation.

Variant axis breakdown:

  * PGXP_DEPTH: m_pgxp_depth_buffer runtime toggle. Controls how
    o_depth is written - u_fill_color.a (legacy alpha-as-depth) or
    constant 1.0 (PGXP-depth mode where depth is per-vertex from
    CPU projection).
  * WRAPPED: fill rect wraps past the VRAM edge. The runtime
    decides this per-call from the destination rect geometry via
    IsVRAMFillOversized at the call site (line 2194). When set,
    the shader does the same wrap-around discard logic as
    vram_copy / vram_write.
  * INTERLACED: skip every-other-row to preserve the field not
    currently being displayed. Set when the destination overlaps
    the active scanout region during interlaced output. The shader
    discards based on (dst_coords.y & 1) == u_interlaced_displayed_field.

No u_resolution_scale dependency in the shader body for D3D12
mode (fixYCoord is identity on HLSL; only the OpenGL backend's
GLSL body references VRAM_SIZE for the y-flip). No scale-refactor
was needed before pre-baking - the shadergen path's CONSTANT
RESOLUTION_SCALE emission would have been dead code.

Files added:

* data/shaders/d3d12/vram_fill.ps.hlsl: standalone HLSL source
  matching GenerateVRAMFillFragmentShader's D3D12 output.
  36-byte cbuffer member-for-member identical to VRAMFillUBOData
  (u_dst_coords, u_end_coords, u_fill_color, u_interlaced_-
  displayed_field). float4 u_fill_color forces 16-byte alignment
  so it lands at offset 16; the trailing uint sits at offset 32.
  Three #define switches at the top control body branches; v_pos
  is only read when INTERLACED || WRAPPED (the discard paths).

* src/common/d3d12/embedded_dxbc/vram_fill_ps_{p0w0i0,p0w0i1,
  p0w1i0,p0w1i1,p1w0i0,p1w0i1,p1w1i0,p1w1i1}.inc: 8 DXBC blobs,
  one per variant combo. Sizes scale predictably with feature
  complexity - 1016B for the minimal p1w0i0 (no wrap, no
  interlace, constant depth), 1380B for the maximal p0w1i1 (both
  discards plus the alpha-as-depth branch). All built with
  Microsoft fxc via Wine + d3dcompiler_47.dll; full
  RDEF/ISGN/OSGN/SHEX/STAT chunk set; creator string
  "Microsoft (R) HLSL Shader Compiler 10.1". Validator
  (check_microsoft_creator in tools/regen_d3d12_dxbc.py) passes
  all 8.

Files modified:

* tools/regen_d3d12_dxbc.py: TEMPLATE_VARIANTS gains the
  vram_fill.ps.hlsl entry with 8 rows. Variant suffix encodes
  the three flags as p{0,1}w{0,1}i{0,1} so the alphabetical
  ordering of the .inc files matches the natural nested-loop
  iteration order (pgxp outer, wrapped middle, interlaced
  inner). embedded_shaders.h externs and the GetVRAMFillPipeline
  call site mirror this naming convention exactly so a reader
  can grep p?w?i? across all three files and find consistent
  hits.

* src/common/d3d12/embedded_shaders.h: 8 extern declarations
  (one per variant) plus size constants. Header docblock
  explains the variant-axis breakdown and the indexed-lookup
  selection pattern.

* src/common/d3d12/embedded_shaders.cpp: 8 #include lines for
  the new .inc files, inside namespace D3D12::EmbeddedShaders.

* src/core/gpu_hw_d3d12.cpp GetVRAMFillPipeline: shadergen
  call + m_shader_cache.GetPixelShader path replaced with a
  function-scope static const D3D12_SHADER_BYTECODE
  [2][2][2] array indexed by [m_pgxp_depth_buffer ? 1 : 0]
  [wrapped & 1][interlaced & 1]. The static-const initializer
  runs once on first call (k_xxx externs and _size_bytes
  constants are linker-visible by then); subsequent calls are
  3 byte indexes. PSO build downstream is unchanged. The
  m_shadergen-not-yet-built check is dropped for this path
  since the embedded blobs are statically linked.

Cost matrix delta (D3D12) for vram_fill_ps:
  Cold-flip PGXP toggle: ~50ms x 4 D3DCompile + ~10ms x 4 PSO
                         -> ~10ms x 4 PSO only.
  (PGXP rebuilds the full wrapped x interlaced submatrix.)
  Disk-cache-warm: ~1 ms each -> 0 ms.
  Resolution scale toggle: 0 ms (no dependency in body).

The shadergen GenerateVRAMFillFragmentShader function is left
intact in gpu_hw_shadergen.cpp - the OpenGL / Vulkan / D3D11
backends still call it through their runtime shader-cache
paths. Only D3D12 bypasses it now.

Test:
  - Build: D3D12 backend links without unresolved symbols across
    all 8 new externs.
  - Runtime: every screen clear in every game must continue to
    render correctly. Concrete signals:
      * Most 3D titles clear every frame (test signal: any 3D
        title's gameplay, the screen-clear path is hit before
        every frame's draw queue). Failure mode: previous
        frame's content stays visible / smears between frames.
      * Tekken 3 round-start fades through a black clear before
        the round draw - if the wrong variant were selected,
        the fade would either not happen or leave stale pixels.
      * FF7 scene transitions (entering/leaving battle, world
        map -> field) clear the framebuffer; the transition
        flash would corrupt if the variant were wrong.
  - PGXP toggle: flip the "PGXP CPU mode" core option. First
    flip rebuilds all 4 (wrapped x interlaced) PSOs picking
    the p1??? variants instead of the p0??? variants. Subsequent
    same-direction flips hit the PSO cache.
  - Resolution scale toggle 1x <-> 8x: no PSO recompile (blobs
    are scale-independent); CPU side just updates the viewport
    and the next FillVRAM hits the same PSO.
  - Interlaced mode test: enable "Force Progressive Scan" off,
    play any title that runs in interlaced output (most PSX
    games do). The INTERLACED=1 path is hit when FillVRAM
    overlaps the active scanout - test signal: no flicker
    between fields during a fill operation.
  - Wrapped fill test: trigger any oversized FillVRAM (CPU
    writes a fill that crosses the right or bottom VRAM edge).
    Some games do this for the standby blackout overlay or for
    fullscreen depth-buffer clears that span multiple 256-tall
    VRAM strips. The WRAPPED=1 path is hit; failure mode would
    be the spillover region staying uncleared.

Next per the same template now that multi-axis is validated:
vram_update_depth_ps (2 MSAA variants - first MSAA texture-
binding variant exercise, Texture2D vs Texture2DMS<float4>),
then vram_read_ps (M MSAA variants on top of 2980961), then
display_ps (12xM variants - largest blob set per shader).
2026-05-18 23:16:38 +02:00
LibretroAdmin
ba013aa6de core/d3d12: pre-bake the VRAM write pixel shader
Same template as cb4b443 (vram_copy pre-bake), applied to vram_write_ps.
vram_write_ps is the CPU->VRAM upload path - hit on every game's
framebuffer streaming and FMV staging path, so it runs many times
per frame in practice. Pre-baking it removes another D3DCompile
from cold-flips and another path that's exposed to shader-cache
invalidation.

Variant axis: PGXP_DEPTH on/off only (2 blobs). The shadergen's
`use_ssbo` parameter is GLSL/Vulkan-only - the D3D12 caller at
GPU_HW_D3D12::GetVRAMWritePipeline always passes `false`, so the
use_ssbo dimension collapses to a single value for D3D12 and only
the texture-buffer source path (Buffer<uint4> samp0) gets pre-baked.

Files added:

* data/shaders/d3d12/vram_write.ps.hlsl: standalone HLSL source
  matching GenerateVRAMWriteFragmentShader(false)'s D3D12 output
  post-9d2b49d. 44-byte cbuffer member-for-member identical to
  VRAMWriteUBOData (u_base_coords, u_end_coords, u_size,
  u_buffer_base_offset, u_mask_or_bits, u_depth_value,
  u_resolution_scale, u_pad0). VRAM_SIZE / RESOLUTION_SCALE aliases
  inlined. RGBA5551ToRGBA8 helper inlined (no shadergen prelude
  available to standalone HLSL). PGXP_DEPTH branch in body controls
  the o_depth assignment.

* src/common/d3d12/embedded_dxbc/vram_write_ps_pgxp0.inc: DXBC for
  /DPGXP_DEPTH=0 (PGXP off, conditional o_depth). 2048 bytes.
* src/common/d3d12/embedded_dxbc/vram_write_ps_pgxp1.inc: DXBC for
  /DPGXP_DEPTH=1 (PGXP on, o_depth = 1.0). 2028 bytes.

Both built with Microsoft fxc via Wine + d3dcompiler_47.dll - full
RDEF/ISGN/OSGN/SHEX/STAT chunk set, creator stamped "Microsoft (R)
HLSL Shader Compiler 10.1". Validator (check_microsoft_creator in
tools/regen_d3d12_dxbc.py) passes both.

Files modified:

* tools/regen_d3d12_dxbc.py: TEMPLATE_VARIANTS gains the
  vram_write.ps.hlsl entry with two rows (pgxp0, pgxp1). Same
  shape as the vram_copy entry.

* src/common/d3d12/embedded_shaders.h: extern declarations added
  for the two new blobs and their size constants. Header docblock
  explains the use_ssbo collapse.

* src/common/d3d12/embedded_shaders.cpp: two #include lines for
  the new .inc files, inside namespace D3D12::EmbeddedShaders.

* src/core/gpu_hw_d3d12.cpp GetVRAMWritePipeline: shadergen call +
  m_shader_cache.GetPixelShader path replaced with an inline
  D3D12_SHADER_BYTECODE picked via m_pgxp_depth_buffer. PSO build
  downstream is unchanged (same root signature, MSAA, depth state,
  shader-cache PSO disk-cache via gpbuilder.Create). The
  m_shadergen-not-yet-built check is dropped for this path since
  the embedded blob is statically linked.

Cost matrix delta (D3D12) for vram_write_ps:
  Cold-flip PGXP toggle: ~50 ms x 2 D3DCompile + ~10 ms x 2 PSO
                         -> ~10 ms x 2 PSO only.
  Disk-cache-warm: ~1 ms each -> 0 ms.
  Resolution scale: 0 ms (already cbuffer-routed since 9d2b49d).

The shadergen GenerateVRAMWriteFragmentShader function is left
intact in gpu_hw_shadergen.cpp - the other three backends still
call it through their runtime shader-cache paths. Only D3D12
bypasses it now.

Test:
  - Build: D3D12 backend links without unresolved symbols.
  - Runtime: every VRAM upload that previously worked must still
    work. Concrete test: any game's pre-rendered backdrop streaming
    (FF7 entering new screens, MGS pre-rendered cutscene backdrops),
    any FMV playback (Sony intro bookend, in-game cutscenes), and
    any sprite-page reload (fighting-game character select). If
    the embedded blob were wrong, framebuffer uploads would land
    at wrong VRAM offsets and produce visible texture garble
    within seconds of any of those activities.
  - PGXP toggle: flip the "PGXP CPU mode" core option in RetroArch.
    First flip rebuilds PSOs picking blob[1] vs blob[0]; subsequent
    same-direction flips hit the PSO cache.
  - Resolution scale toggle 1x <-> 4x: no PSO recompile (blob is
    scale-independent post-9d2b49d); next CPU->VRAM upload pushes
    a different u_resolution_scale through the cbuffer.

Next per the same template now that the toolchain is trusted and
the pattern is established: vram_fill_ps (8 variants on PGXP x
WRAPPED x INTERLACED, no scale-refactor needed - body doesn't
reference RESOLUTION_SCALE for D3D12 mode), vram_update_depth_ps
(2 MSAA variants - first MSAA texture-binding variant exercise),
vram_read_ps (M MSAA variants - on top of the e64fc28-line
cbuffer routing already done).
2026-05-18 23:04:29 +02:00
LibretroAdmin
837f120ce4 core/d3d12: pre-bake the VRAM copy pixel shader (Microsoft fxc)
Second attempt at the cd971cd / da20f5b pre-bake, now with the
Microsoft fxc toolchain in place (09ca0dc). Same shadergen +
runtime changes as the reverted cd971cd; the only material
difference is that the .inc files are produced by Microsoft's
D3DCompile API instead of Wine's vkd3d-shader reimplementation,
which means the DXBC has the SHEX + STAT chunks that
CreateGraphicsPipelineState validation needs.

Output deltas vs the failed cd971cd attempt:

  vram_copy_ps_pgxp0.inc  3108B (vkd3d) -> 1908B (Microsoft)
  vram_copy_ps_pgxp1.inc  2984B (vkd3d) -> 1828B (Microsoft)

Microsoft's compiler produces smaller bytecode (better optimization
at /O3) and emits the full chunk set RDEF/ISGN/OSGN/SHEX/STAT,
where vkd3d-shader was emitting only ISGN/OSGN/RDEF/SHDR. The
SHEX chunk in particular is what changes here - it's the Shader
Model 5.0+ extended bytecode format that D3D12 PSO validation
expects for SM 5.0 shaders. SHDR is the older SM 4.0 format;
Microsoft D3D12 accepts SHDR for simple shaders by coincidence
of how the validator is structured, but rejects it for anything
with SV_Depth + discard + non-trivial cbuffer (the threshold
case cd971cd hit). SHEX-format output passes uniformly.

Validator confirmation:
  Both .inc files stamp creator string
  "Microsoft (R) HLSL Shader Compiler 10.1"; the
  check_microsoft_creator hook in tools/regen_d3d12_dxbc.py
  (ebd2300) passes them.

This restores the full set of changes that cd971cd shipped:

* data/shaders/d3d12/vram_copy.ps.hlsl - standalone HLSL source
  matching GenerateVRAMCopyFragmentShader's D3D12 output. Cbuffer
  layout member-for-member identical to VRAMCopyUBOData (48 bytes
  - u_src_coords, u_dst_coords, u_end_coords, u_size,
  u_set_mask_bit, u_depth_value, u_resolution_scale, u_pad0).
  VRAM_SIZE alias inlined. PGXP_DEPTH branch in body controls
  the o_depth assignment.

* tools/regen_d3d12_dxbc.py TEMPLATE_VARIANTS dict gains the
  vram_copy.ps.hlsl entry with two rows (pgxp0, pgxp1).

* src/common/d3d12/embedded_shaders.h gains extern declarations
  for k_vram_copy_ps_pgxp0[], _pgxp1[], and the matching
  _size_bytes constants.

* src/common/d3d12/embedded_shaders.cpp gains two #include
  lines inside namespace D3D12::EmbeddedShaders for the new
  .inc files.

* src/core/gpu_hw_d3d12.cpp GetVRAMCopyPipeline: shadergen call
  + m_shader_cache.GetPixelShader path replaced with an inline
  D3D12_SHADER_BYTECODE picked via m_pgxp_depth_buffer. PSO
  build downstream is unchanged. The m_shadergen-not-yet-built
  check is dropped for this path since the embedded blob is
  statically linked.

Variant axis: PGXP_DEPTH only (2 blobs, ~3.7 KB total). Resolution
scale is cbuffer-routed since e56d4d4 so it's not a variant; MSAA
isn't a variant since vram_copy never runs in MSAA mode (texture
binding conflict on the source view).

Cost matrix delta (D3D12 specifically) for vram_copy_ps:
  Cold-flip PGXP toggle: ~50 ms x 2 D3DCompile + ~10 ms x 2 PSO
                         -> ~10 ms x 2 PSO only.
  Disk-cache-warm: ~1 ms each -> 0 ms.
  Resolution scale: 0 ms (already cbuffer-routed).

Test:
  - Build: D3D12 backend links without unresolved symbols.
  - Runtime: FF7 menu navigation (heavy VRAM-copy traffic for
    menu sprite composition) and Tekken 3 round transitions
    (every-frame VRAM-copy splat for round number / TOTAL DMG
    text) render correctly. Failure mode if the .inc were wrong:
    visible sprite / text garble within a second of entering
    a menu or starting a round. Since cd971cd's failure mode
    was "D3D12 backend never inits", the success signal is
    simply that the backend comes up at all - the cd971cd
    log line "CreateGraphicsPipelineState failed: 80004005"
    no longer fires.
  - PGXP toggle: flip the "PGXP CPU mode" core option; the
    first flip rebuilds PSOs picking blob[1] vs blob[0] and
    renders correctly. Subsequent same-direction flips hit the
    PSO cache.
  - Resolution scale toggle 1x <-> 4x: no PSO recompile (the
    blob is scale-independent post-e56d4d4); CopyVRAM next
    frame pushes a different u_resolution_scale value through
    the cbuffer.

If this attempt also fails, the diff to look at is the .inc bytes
themselves (Microsoft-compiled, full chunk set) vs cd971cd's
.inc (vkd3d-shader-compiled, missing SHEX/STAT). Anything else
in the source is identical.

Next on the pre-bake side per the same template now that
the toolchain is trusted: vram_write_ps (4 variants: PGXP x
use_ssbo), vram_fill_ps (8 variants, no scale-refactor needed),
vram_update_depth_ps (2 MSAA variants - first MSAA texture-binding
variant exercise).
2026-05-18 22:57:57 +02:00
LibretroAdmin
ebd2300cde tools/d3d12: refuse non-Microsoft fxc output in the regen script
Post-mortem follow-up to cd971cd / da20f5b. The pre-bake attempt
failed because Wine's bundled d3dcompiler_47.dll (which is the
vkd3d-shader reimplementation, currently at version 1.10) produces
DXBC that's syntactically valid as a container but trips Microsoft
D3D12's CreateGraphicsPipelineState with E_FAIL for non-trivial
shaders. The vram_copy.ps.hlsl case crossed that threshold; the
simpler fullscreen_quad.vs.hlsl + copy.ps.hlsl cases happen to
work and are still in the repo, but they were also vkd3d-shader-
compiled and should be regenerated with Microsoft fxc when the
opportunity arises.

The fix is twofold: improve the SDK auto-detection so contributors
who have Microsoft fxc installed don't have to fight the script,
and add an output validator that refuses to write .inc files
produced by any compiler that isn't Microsoft fxc.

Auto-detection improvements (find_fxc):

* Wine prefix discovery widened. Previously the script only
  checked $WINEPREFIX/drive_c if WINEPREFIX was set. Now it also
  checks ~/.wine (the Wine default when WINEPREFIX is unset),
  and any prefix directory under ~/.local/share/wineprefixes/
  that has a drive_c subtree (PlayOnLinux / lutris-style layouts).
  Both /Program Files/ and /Program Files (x86)/ are searched in
  each prefix.
* Error message now points at the README's installation steps
  rather than telling the user to just install the SDK.
* Error message explicitly calls out that Wine's bundled
  d3dcompiler_47.dll is NOT a substitute and references
  da20f5b for context.

Output validator (extract_rdef_creator + check_microsoft_creator):

* extract_rdef_creator walks the DXBC container chunk index, finds
  the RDEF chunk, follows the creator_offset field at chunk body
  offset 24, and reads back the null-terminated ASCII creator
  string. The DXBC container layout (16-byte hash + version +
  total size + chunk count + chunk offsets) and the RDEF body
  layout (constant_buffer_count, constant_buffer_offset,
  resource_binding_count, resource_binding_offset, shader_version,
  compiler_flags, creator_offset) are documented in MSDN's old
  D3D11 reflection notes and in the wikitalk pages around DXBC;
  the implementation here uses fixed offsets that match what
  fxc.exe and vkd3d-shader both produce.
* check_microsoft_creator hooks into compile_one after the magic
  check. It refuses with a clear error message if the creator
  string doesn't contain "Microsoft". Microsoft fxc stamps
  something like "Microsoft (R) HLSL Shader Compiler 10.1";
  vkd3d-shader stamps "vkd3d-shader 1.10 (Wine bundled)". One
  substring check disambiguates.

What the validator does NOT do:

* It does not retroactively reject existing .inc files in the
  repo. The validator only fires on freshly-produced DXBC during
  a regen run. The vkd3d-shader-built fullscreen_quad_vs.inc and
  copy_ps.inc are unchanged by this commit; they'll get refreshed
  the next time a contributor with Microsoft fxc runs the regen
  script. Until then, they keep working because they're already
  in the repo and the build just compiles them as plain C++
  byte arrays - they never go through PSO validation (they're
  already valid DXBC).
* It does not lock the script to a specific fxc version. Any
  Microsoft fxc.exe (10.1, 10.5, the version that ships with the
  Windows 11 SDK, etc.) is accepted - they all stamp "Microsoft"
  in the creator string.
* It does not validate the binding layout vs the runtime root
  signature. That's a future enhancement - the runtime side
  expects specific (b0, t0, s0) layouts and the regen script
  could cross-check, but that's a much bigger feature.

README changes:

* New "Microsoft fxc.exe is required" section spells out why and
  cites the cd971cd / da20f5b failure as the worked example.
* "Installing Microsoft fxc.exe" subsection with separate
  Windows / Linux instructions. Linux gets two paths: install
  the SDK under a Wine prefix (auto-detected), or extract
  fxc.exe + d3dcompiler_47.dll directly from the MSI with
  msitools and pass --fxc. Both must live in the same dir so
  Wine's loader prefers Microsoft's d3dcompiler_47.dll over its
  bundled vkd3d-shader version.
* The auto-detection scope (WINEPREFIX, ~/.wine, ~/.local/share/
  wineprefixes/*) is now documented so contributors know where
  the script is looking.

This commit does NOT re-add the vram_copy.ps.hlsl source removed
by da20f5b. That comes back in a follow-up patch once a Microsoft
fxc-built .inc is available - same patch lands the .hlsl, the
.inc, and the call-site flip together (the dd54a73 lesson).

Test:
  - python3 -c 'import ast; ast.parse(open("tools/regen_d3d12_dxbc.py").read())'
    passes.
  - Loading the script as a module and feeding the EXISTING
    vkd3d-built copy_ps.inc into extract_rdef_creator returns
    'vkd3d-shader 1.10 (Wine bundled)'; the Microsoft substring
    check returns False. Confirms the validator would catch the
    cd971cd mistake if anyone tries to reproduce it.
  - find_fxc's broader Wine prefix scan is exercised by the
    standard test of running the script in an environment without
    Microsoft fxc; it now fails with the new pointed-at-README
    message instead of the old generic SDK message.
2026-05-18 22:16:18 +02:00
LibretroAdmin
da20f5b80e Revert "core/d3d12: pre-bake the VRAM copy pixel shader"
This reverts commit cd971cd. The pre-baked DXBC blob causes
CreateGraphicsPipelineState to fail with E_FAIL (0x80004005)
during D3D12 backend Initialize, falling back to software
renderer. Confirmed by Lib's screenshot + log output:

  [libretro INFO] [GPU_HW_D3D12] Max texture size: 16384x16384
  [libretro INFO] [GPU_HW_D3D12] Dual-source blend: supported
  [libretro INFO] [GPU_HW_D3D12] Per-sample shading: supported
  [libretro INFO] [GPU_HW_D3D12] Max multisamples: 8
  [libretro ERROR] [GetPipelineState] CreateGraphicsPipelineState failed: 80004005
  [libretro ERROR] [Initialize] Failed to compile pipelines

Root cause is in the wine + vkd3d-shader compilation path used to
produce vram_copy_ps_pgxp{0,1}.inc. Inspecting the DXBC reveals
vkd3d-shader (vkd3d-shader 1.10 Wine bundled) strips the
SamplerState samp0_ss declaration from the RDEF chunk because the
shader uses .Load() not .Sample() so samp0_ss is unused:

  copy_ps.inc RDEF: 3 bindings (samp0_ss, samp0, UBOBlock) - works
  vram_copy_ps_pgxp0.inc RDEF: 2 bindings (samp0, UBOBlock) - fails

This SHOULD be fine since D3D12 allows the shader to use a subset
of the root signature's resource bindings. But the Microsoft D3D12
runtime rejects this particular blob at PSO creation - likely
some other metadata mismatch (SHEX vs SHDR chunk format, missing
STAT chunk, or vkd3d-shader emitting SV_Depth / discard in a form
Microsoft's runtime won't accept). Without D3D12 debug-layer
messages it's hard to localize further.

copy_ps and fullscreen_quad_vs (both Wine-compiled) work in
production, so vkd3d-shader output is not universally
incompatible - it's specific to this shader's combination of
features (SV_Depth output + discard + larger cbuffer + bool
member + uint arithmetic).

For future pre-bakes of more complex shaders, the .inc files
should be regenerated using the Microsoft fxc.exe from a Windows
10/11 SDK (running natively on Windows or under Wine - Microsoft's
fxc.exe is an x86 PE binary that runs fine under Wine, no
vkd3d-shader reimplementation in the path). The current
fullscreen_quad_vs + copy_ps blobs landed with vkd3d-shader
because they're simple enough that the resulting bytecode passes
PSO validation; vram_copy_ps is the threshold case where it
doesn't.

Next pre-bake attempt should:
  1. Switch tools/regen_d3d12_dxbc.py to prefer Microsoft fxc.exe
     by default (Wine prefix lookup + WINEPREFIX environment),
     fall back to fxc_lite only when explicitly asked.
  2. Regenerate fullscreen_quad_vs + copy_ps with Microsoft fxc
     (to confirm byte-different output - and to lock in a known-
     working reference for diff comparison when adding new
     pre-baked shaders).
  3. Then redo vram_copy_ps with Microsoft fxc; expect to land
     it with no other changes needed.

The non-batch RESOLUTION_SCALE cbuffer refactor arc (e56d4d4,
9d2b49d, 2980961, e64fc28) is unaffected by this revert - those
commits don't touch any pre-baked blobs, just route u_resolution_scale
through cbuffers. The pre-bake follow-ups can resume once the
fxc.exe toolchain is in place.
2026-05-18 21:57:44 +02:00
LibretroAdmin
cd971cd1d2 core/d3d12: pre-bake the VRAM copy pixel shader
First multi-variant pre-bake, exercising the TEMPLATE_VARIANTS
infrastructure in tools/regen_d3d12_dxbc.py for the first time.
vram_copy_ps is the smallest cleanly-pre-bakeable non-batch shader
after the e56d4d4 RESOLUTION_SCALE refactor took it from
PGXP x N variants down to just PGXP (2 blobs). Pure body-branch
variant axis - no texture binding changes, no MSAA split, no
input-layout differences. Body is small (~30 lines) and the
runtime call site is one function (GetVRAMCopyPipeline) in one
backend.

Why pre-bake vram_copy_ps:

  * It's hit constantly during gameplay. Every PSX game uses
    VRAM-to-VRAM copies for sprite caching (Final Fantasy 7's
    pre-rendered backgrounds streaming sprites in, Tekken 3's
    text rendering between rounds, every fighting game's HUD
    composite, every JRPG's menu system).
  * After e56d4d4 the only variant axis is PGXP_DEPTH on/off, so
    2 blobs cover every (m_pgxp_depth_buffer, m_resolution_scale)
    combination - cleanest possible variant count.
  * It's the natural exercise for TEMPLATE_VARIANTS (which has
    sat empty since the regen tool was first scaffolded at
    ca8c9b0). Subsequent multi-variant pre-bakes (vram_write 4
    variants, vram_read M MSAA variants, display 12 x MSAA
    variants) reuse the same pattern.

Files added:

* data/shaders/d3d12/vram_copy.ps.hlsl: standalone HLSL source
  matching the GenerateVRAMCopyFragmentShader output for D3D12
  post-e56d4d4. The cbuffer layout matches VRAMCopyUBOData
  member-for-member (u_src_coords, u_dst_coords, u_end_coords,
  u_size, u_set_mask_bit, u_depth_value, u_resolution_scale,
  u_pad0 = 48 bytes). VRAM_SIZE alias inlined since the helper
  emission isn't accessible to standalone HLSL.
  PGXP_DEPTH variant is a single #if in the body controlling
  the o_depth assignment. MSAA path omitted entirely - vram_copy
  never runs in MSAA mode because the source texture is bound
  as both shader-resource AND framebuffer-output, which D3D12
  doesn't permit with an MSAA resource; the runtime uses the
  non-MSAA m_vram_read_texture shadow.

* src/common/d3d12/embedded_dxbc/vram_copy_ps_pgxp0.inc: DXBC
  blob for /DPGXP_DEPTH=0 (PGXP off, conditional o_depth).
  ~3.1 KB of bytecode.

* src/common/d3d12/embedded_dxbc/vram_copy_ps_pgxp1.inc: DXBC
  blob for /DPGXP_DEPTH=1 (PGXP on, o_depth = 1.0). ~3.0 KB.

Files modified:

* tools/regen_d3d12_dxbc.py: TEMPLATE_VARIANTS dict populated
  with the vram_copy.ps.hlsl entry. Two variant rows:
    ("pgxp0", ["PGXP_DEPTH=0"])
    ("pgxp1", ["PGXP_DEPTH=1"])
  The dispatch loop at line 232-238 (untouched, pre-existed
  from ca8c9b0) does the right thing: emit one .inc per row,
  identifier suffix from the variant_suffix string.

* src/common/d3d12/embedded_shaders.h: extern declarations
  added for the two new blobs (k_vram_copy_ps_pgxp0,
  k_vram_copy_ps_pgxp0_size_bytes, and pgxp1 pair). Each pair
  matches the regen tool's naming convention.

* src/common/d3d12/embedded_shaders.cpp: two #include lines
  added inside namespace D3D12::EmbeddedShaders, mirroring
  what was done for fullscreen_quad_vs + copy_ps. The .inc
  files define the storage; the prior extern declarations in
  the header give them external linkage.

* src/core/gpu_hw_d3d12.cpp GetVRAMCopyPipeline: dropped the
  shadergen-call + m_shader_cache.GetPixelShader path; replaced
  with an inline D3D12_SHADER_BYTECODE constructed from the
  embedded blob selected via m_pgxp_depth_buffer. Same PSO-
  creation flow downstream (gpbuilder, MSAA state, depth state,
  shader-cache PSO disk-cache via gpbuilder.Create). The
  m_shadergen-not-yet-constructed check is dropped for this
  path since the embedded blobs are statically linked - no
  ordering dependency on shadergen lifetime.

Toolchain confirmation:
  Built with Wine's d3dcompiler_47.dll via /tmp/fxc_lite/fxc_lite.exe
  on the contributor side (same tooling as 77b255f / 443a2a1).
  DXBC magic verified at regen time (the 'DXBC' signature in the
  first 4 bytes; the regen tool aborts the build if that doesn't
  match).

Cost matrix delta (D3D12 specifically):
  vram_copy_ps D3DCompile cost on cold-cache flip: ~50 ms each
  for the two PGXP variants -> 0 ms after this commit. The PSO
  compile itself still runs through gpbuilder.Create (~10 ms),
  so total cold-flip-of-PGXP cost drops by ~50 ms per variant.
  Disk-cache-warm cost was already in the noise (~1 ms cache
  lookup); now it's literally zero.

  Progress tracker: non_batch_progress_units stays at 17 - we
  still tick once per (depth_test) GetVRAMCopyPipeline call
  inside the precompile_sync loop. PSO compile is still the
  unit; we just stopped doing the D3DCompile half of that
  work.

Variant count after this commit, for the cold-flip cost matrix
columns we care about, for vram_copy specifically:
  * resolution_scale  : 0 ms (cbuffer-routed since e56d4d4)
  * pgxp_depth        : 0 ms D3DCompile + ~10 ms PSO compile
  * multisamples      : 0 (not a vram_copy variant axis)
  * filter / chroma   : 0 (no relationship to vram_copy)

The shadergen GenerateVRAMCopyFragmentShader function is left
intact in gpu_hw_shadergen.cpp - the other three backends
(D3D11, OpenGL, Vulkan) still call it through their runtime
shader-cache paths. Only the D3D12 backend bypasses it now.
Future per-backend pre-bake work could extend the pattern
to those backends too (Vulkan via SPIR-V pre-bake, OpenGL via
something else - GLSL doesn't have a comparable offline-
compiled bytecode format; SPIR-V cross would be the route).

Test:
  - Build: D3D12 backend should link without unresolved symbols.
    The shadergen->GenerateVRAMCopyFragmentShader path is no
    longer reachable through GetVRAMCopyPipeline on D3D12 but
    the function is still defined and used by other backends.
  - Runtime: every VRAM copy that previously worked must still
    work. Concrete test - FF7 menu navigation (uses VRAM copy
    for menu sprite composition heavily), Tekken 3 round-
    transition text rendering (VRAM copy each frame to splat
    the round number / TOTAL DMG sprites). If the embedded
    blob were wrong, sprites and menu text would garble within
    a second of entering a menu or starting a round.
  - PGXP toggle: flip the "PGXP CPU mode" option on and off
    in RetroArch's core options. The first flip recompiles
    PSOs; the embedded blob path picks blob[1] vs blob[0] and
    builds fresh PSOs. Subsequent same-direction flips hit the
    PSO cache.
  - Resolution scale toggle: 1x <-> 4x. No PSO recompile (since
    the blob is scale-independent now); CopyVRAM next frame
    just pushes a different u_resolution_scale value through
    the cbuffer.

This is the first non-batch FRAGMENT pre-bake on top of the
RESOLUTION_SCALE refactor. Following the same template:
vram_write_ps (4 variants: PGXP x use_ssbo), vram_fill_ps (8
variants: PGXP x wrapped x interlaced, no refactor needed
since the body doesn't reference RESOLUTION_SCALE), and
vram_update_depth_ps (2 MSAA variants, exercises MSAA
texture-binding variant for the first time).
2026-05-18 21:48:20 +02:00
LibretroAdmin
443a2a130b core/d3d12: pre-bake the copy/blit pixel shader
Next single-variant non-batch shader in the D3D12 pre-bake arc,
on top of fullscreen_quad_vs (dd54a73 + 77b255f). Picks the
copy/blit fragment shader (GenerateCopyFragmentShader -> bound
in GetCopyPipeline, used for full-frame blits during display
presentation / downscaling).

Why this one and not vram_copy / vram_update_depth / vram_fill /
vram_read / display - which were the other candidates on the
shortlist:

  * vram_update_depth: has an MSAA #ifdef split inside the body
    (LOAD_TEXTURE_MS vs LOAD_TEXTURE) plus a declare_sample_id
    in DeclareFragmentEntryPoint that depends on UsingMSAA(). So
    2 variants minimum, more once we account for the MSAA sample
    count.
  * vram_copy: has a PGXP_DEPTH define, plus uses VRAM_SIZE which
    is compile-time-baked from m_resolution_scale * VRAM_WIDTH/
    HEIGHT via the non-batch WriteCommonFunctions path. 2x
    pgxp_depth times ~5 resolution scale values minimum.
  * vram_fill: PGXP_DEPTH x wrapped x interlaced plus VRAM_SIZE
    via the same compile-time-bake path. 4x variants minimum,
    more after multiplying by resolution scale.
  * vram_read: VRAM_SIZE / RESOLUTION_SCALE bake-in via
    WriteCommonFunctions; the inner box-filter loop uses
    RESOLUTION_SCALE directly as the loop bound, so the variant
    count is one per scale value (5+).
  * display: depth_24bit x interlace_mode x smooth_chroma x
    UsingMSAA x RESOLUTION_SCALE - the multi-variant family with
    the biggest cost.

Each of the above hits one of two structural issues that the
infrastructure doesn't address yet:

  1. RESOLUTION_SCALE / VRAM_SIZE are baked in compile-time for
     non-batch shaders via WriteCommonFunctions's
     batch_uniform_buffer=false path. The cbuffer-routed batch
     path (post-7b575a3) only applies to batch shaders. Migrating
     a non-batch shader that references these constants requires
     either one .inc per resolution scale value, or a refactor
     that lets non-batch shaders read u_resolution_scale from the
     cbuffer too.
  2. The MSAA dimension multiplies every shader that calls
     LOAD_TEXTURE_MS in some branch.

ShaderGen::GenerateCopyFragmentShader (NOT
GPU_HW_ShaderGen::GenerateVRAMCopyFragmentShader - this is the
plain blit one declared in shadergen.h, not the VRAM-coords
copy in gpu_hw_shadergen.cpp) sidesteps all of this:

  * No WriteCommonFunctions call - so no RESOLUTION_SCALE, no
    VRAM_SIZE, no MULTISAMPLES bake-in.
  * No MSAA branch - takes a single Texture2D, not Texture2DMS.
  * No #define-controlled variants - the source string is
    identical on every call.
  * cbuffer-routed source rect (u_src_rect float4) - all runtime
    state lives in the UBO, none baked.

So one pre-baked variant covers every caller. Same shape as
fullscreen_quad_vs.

Files in this patch:

* data/shaders/d3d12/copy.ps.hlsl - standalone HLSL, ~16 lines,
  native HLSL syntax (no shadergen prelude, no aliases). cbuffer
  UBOBlock at register(b0), Texture2D samp0 at register(t0) +
  SamplerState samp0_ss at register(s0), one TEXCOORD0 input,
  one SV_Target0 output. Same source rect math as the original
  generated shader.

* src/common/d3d12/embedded_dxbc/copy_ps.inc - autogenerated by
  tools/regen_d3d12_dxbc.py /T ps_5_0 /E main /O3. Generated in
  this submission environment via Wine's d3dcompiler_47.dll
  through the fxc_lite shim (see 77b255f for the full toolchain
  setup). 808 bytes of valid DXBC (magic 0x43425844 / "DXBC",
  four chunks: ISGN / OSGN / SHEX / STAT).

* src/common/d3d12/embedded_shaders.h - extern declarations for
  k_copy_ps[] and k_copy_ps_size_bytes added below the existing
  fullscreen_quad_vs pair. Same comment format - documents the
  shader's role and the no-state-dependency rationale.

* src/common/d3d12/embedded_shaders.cpp - second #include line
  for embedded_dxbc/copy_ps.inc inside the namespace block.
  Pattern matches what subsequent migrations will follow.

* src/core/gpu_hw_d3d12.cpp - GetCopyPipeline body updated:
  - The ComPtr<ID3DBlob> fs = m_shader_cache.GetPixelShader(
    m_shadergen->GenerateCopyFragmentShader()) + null check
    replaced with a const D3D12_SHADER_BYTECODE fs = { ... }
    aggregate-init over the embedded blob.
  - The if (!m_shadergen) check is dropped - GetCopyPipeline
    no longer has any m_shadergen dependency. Other state
    (root signature, context, shader cache, mutex) was already
    initialised earlier than m_shadergen in the lifecycle, so
    removing the m_shadergen check doesn't open a 'called too
    early' window for this helper.
  - gpbuilder.SetPixelShader(fs.Get()) becomes
    gpbuilder.SetPixelShader(fs.pShaderBytecode,
                             static_cast<uint32_t>(fs.BytecodeLength))
    matching the existing SetVertexShader call form a few
    lines above. (Cleanup: a D3D12_SHADER_BYTECODE& overload
    on SetVertexShader / SetPixelShader in d3d12/util.h would
    elide the verbose unpack at all call sites - deferred to
    a follow-up patch so this one stays focused on the shader
    migration.)

Progress unit count unchanged. GetCopyPipeline still costs one
progress.Increment call in the precompile_sync block at the
'copy/blit (1)' entry of the 4 + 2 + 2 + 1 + 1 + 6 + 1 = 17
breakdown - the embedded shader removes the D3DCompile work,
but the ID3D12Device::CreateGraphicsPipelineState call (the
PSO compile) is what the progress tick actually corresponds to,
and that still happens.

Cost / behaviour:
  - Cold boot, shader_precompile_mode = Enabled: one fewer
    D3DCompile per first-touch of GetCopyPipeline (which is hit
    once per CompilePipelines pass). Combined with
    fullscreen_quad_vs, that's two non-batch shaders off the
    D3DCompile path. The bulk of the cold-boot ~1 min cost is
    still in the batch matrix (1164 cells) - this migration
    chips at a single non-batch unit, but it's the right next
    step structurally because it validates the .ps.hlsl side
    of the pipeline (fullscreen_quad_vs only validated .vs).
  - Lazy mode: same delta, on first lazy-fault of the copy
    pipeline.
  - Disabled: same delta, on first GetCopyPipeline call from
    the runloop (presentation copy / downscale).
  - Runtime: no behaviour change. The pre-baked DXBC binds at
    PSO creation; presentation copies / downscaling renders
    identically.

Failure modes to watch for:
  - If Wine's d3dcompiler produces semantically different DXBC
    from Microsoft's fxc.exe for this specific shader (very
    unlikely given the simplicity - one texture sample, no
    branching, no flow control), the blit would render
    incorrectly. Visible immediately as a wrong / blank
    presentation. Easy to spot.
  - If the .inc file gets out of sync with copy.ps.hlsl (e.g.
    someone edits the .hlsl without re-running the regen
    script), the embedded blob would compile against the old
    shader source. The autogen header comment in the .inc
    documents the source it was generated from, and the regen
    script is idempotent (re-running it on an unchanged .hlsl
    produces byte-identical output), so getting the two back
    in sync is just `python3 tools/regen_d3d12_dxbc.py`.

Test:
  - Build under USE_D3D12=1 succeeds.
  - Cold boot under shader_precompile_mode = Enabled. Progress
    bar reaches 100% at 17 non-batch units (unchanged). The
    copy/blit pipeline ticks the same as before; its PSO is
    built with the embedded PS blob.
  - Presentation / downscaling render correctly - GetCopyPipeline
    is called at presentation time from the host display layer.
    If the embedded PS is wrong, the framebuffer presents wrong
    on the first frame.
  - Toggle a non_dim setting (multisamples) to fire the full-
    flush branch. m_copy_pipeline is Reset() in DestroyPipelines;
    next CompilePipelines pass re-binds the embedded blob.
  - Toggle chroma_smoothing for the partial-flush branch -
    GetCopyPipeline isn't reached (only display pipelines clear),
    embedded blob undisturbed.

Next in the arc: a util.h cleanup adding a
SetVertexShader/SetPixelShader(D3D12_SHADER_BYTECODE&) overload
so the .pShaderBytecode + static_cast<uint32_t>(.BytecodeLength)
boilerplate disappears from call sites. After that, the
RESOLUTION_SCALE cbuffer refactor for non-batch shaders opens
up vram_copy / vram_update_depth / vram_fill / vram_read - the
last single-variant or near-single-variant non-batch shaders
before the batch matrix takes the rest of the arc.
2026-05-18 20:22:17 +02:00
LibretroAdmin
dd54a73809 core/d3d12: pre-bake the fullscreen-quad vertex shader
First end-to-end pre-bake migration off the runtime D3DCompile
path, on top of the scaffolding from ca8c9b0. Picks the smallest
possible target - a single-variant vertex shader with zero state
dependency, used by every non-batch pipeline (vram_fill /
vram_copy / vram_write / vram_update_depth / vram_readback /
display / copy/blit) - to validate the full pipeline before
larger migrations land.

Reasoning for starting here: the screen-quad VS emits a fullscreen
triangle from SV_VertexID via the standard bit-shift trick and
has no #defines or compile-time-baked uniforms, so a single
pre-baked variant covers all seven call sites. Compared to vram_read
(which I evaluated as the first target earlier in this arc):
RESOLUTION_SCALE bakes in compile-time on non-batch shaders via
WriteCommonFunctions, so vram_read would need either one variant
per scale value or a refactor of the runtime to bind the batch
cbuffer to non-batch shaders. That's a real refactor, not a
validation patch. The screen-quad VS sidesteps it entirely.

Files in this patch:

* data/shaders/d3d12/fullscreen_quad.vs.hlsl - standalone HLSL
  equivalent to what ShaderGen::GenerateScreenQuadVertexShader()
  produces in D3D12 mode after preprocessor expansion. Stripped
  the shadergen prelude (#define HLSL 1, #define vec2 float2,
  the SAMPLE_TEXTURE / LOAD_TEXTURE / FOR_UNROLL macros, the API_*
  defines) because the shader uses none of them - native HLSL
  is what fxc wants. Also dropped the dead
  #if API_OPENGL || API_OPENGL_ES || API_VULKAN branch since
  API_OPENGL=0 / API_OPENGL_ES=0 / API_VULKAN=0 for D3D12.

* src/common/d3d12/embedded_shaders.h - replaces the placeholder
  comment block from ca8c9b0 with the first concrete extern
  declaration:

    extern const uint8_t k_fullscreen_quad_vs[];
    extern const size_t k_fullscreen_quad_vs_size_bytes;

  Pattern matches the regen_d3d12_dxbc.py emission convention
  documented in ca8c9b0's commit message.

* src/common/d3d12/embedded_shaders.cpp - replaces its placeholder
  with the first #include of a generated .inc. The .inc itself
  (src/common/d3d12/embedded_dxbc/fullscreen_quad_vs.inc) is
  generated by tools/regen_d3d12_dxbc.py from the .hlsl source
  above. **The .inc is NOT included in this patch** because the
  submission environment doesn't have fxc.exe available. See the
  applying instructions at the bottom of this commit message.

* src/core/gpu_hw_d3d12.h:
  - GetFullscreenQuadVertexShader() return type changed from
    ComPtr<ID3DBlob> to D3D12_SHADER_BYTECODE. The function is no
    longer a lazy-fault helper - the embedded blob is statically
    linked, always available, no allocation. Function comment
    refreshed accordingly.
  - m_fullscreen_quad_vertex_shader member dropped. It was the
    cache slot for the lazy-faulted blob; with pre-bake there's
    no fault-in, no caching state, no mutex.

* src/core/gpu_hw_d3d12.cpp:
  - #include "common/d3d12/embedded_shaders.h" added next to the
    other common/d3d12/ includes.
  - GetFullscreenQuadVertexShader body rewritten to return a
    D3D12_SHADER_BYTECODE aggregate over the embedded blob. No
    mutex, no shader_cache.GetVertexShader, no D3DCompile.
  - Two precompile_sync if-checks removed. The block at line ~956
    used to call GetFullscreenQuadVertexShader() and increment
    progress; the block at line ~1023 used to fault it in for
    Lazy mode. Both meaningless now - the blob is statically
    linked, there is no fault to drive. Left explanatory comments
    at both sites so the absence is documented.
  - Progress unit count updated 18 -> 17. The fullscreen-quad
    VS was one of the 18 non-batch compilation units the
    progress bar ticked through; with pre-bake it has no compile
    step to tick. Inline comment refreshed at both the count
    declaration site (line ~825) and the "1 + 4 + 2 + 2 + 1 + 1
    + 6 + 1 = 18" comment that explained the breakdown.
  - Seven call sites in the GetXxxPipeline helpers converted
    from:
        ComPtr<ID3DBlob> vs = GetFullscreenQuadVertexShader();
        if (!vs)
          return {};
        ...
        gpbuilder.SetVertexShader(vs.Get());
    to:
        const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader();
        ...
        gpbuilder.SetVertexShader(vs.pShaderBytecode,
                                  static_cast<uint32_t>(vs.BytecodeLength));
    The null check is dropped because the embedded blob is never
    null. The cast to uint32_t matches the existing
    SetVertexShader(const void*, uint32_t) overload signature in
    src/common/d3d12/util.h. (A future cleanup could add a
    SetVertexShader(const D3D12_SHADER_BYTECODE&) overload to
    elide the verbose unpacking at call sites; left for the next
    patch in the arc where we'll have several more embedded
    blobs and the savings justify the overload.)
  - m_fullscreen_quad_vertex_shader.Reset() in DestroyPipelines
    dropped. Comment block trimmed accordingly.
  - Stale comment block above GetFullscreenQuadVertexShader
    refreshed to describe pre-bake semantics instead of the
    fault-in pattern that no longer applies.

Applying instructions (the .inc file isn't in this patch, so
build will fail until it lands):

  $ git am 0001-core-d3d12-pre-bake-the-fullscreen-quad-vertex-shader.patch
  $ python3 tools/regen_d3d12_dxbc.py
    # produces src/common/d3d12/embedded_dxbc/fullscreen_quad_vs.inc
  $ git add src/common/d3d12/embedded_dxbc/fullscreen_quad_vs.inc
  $ git commit --amend --no-edit
  $ git push

The amend folds the generated .inc into this commit so upstream
sees a single self-contained patch. (Alternative: commit the .inc
as a separate "regenerate fullscreen_quad_vs.inc" follow-up;
either works, but amending keeps the history one-commit-per-
migration which matches the Vulkan-side convention.)

Cost / behaviour:
  - Cold boot, shader_precompile_mode = Enabled: one fewer
    D3DCompile call, one fewer ID3DBlob allocation. Microseconds
    saved per boot, not user-visible at this scale.
  - Lazy mode pre-fill: same delta.
  - Disabled mode first FillVRAM / etc.: same delta.
  - Runtime: no behaviour change. PSOs that bind the embedded
    blob produce byte-identical bytecode to what D3DCompile would
    have produced from GenerateScreenQuadVertexShader() (modulo
    fxc version compiler-output differences, which the runtime
    cache normalises against anyway).
  - Failure mode: if fxc produces a different shader (e.g. a
    fxc version drift bug), every non-batch pipeline using the
    embedded VS would render incorrectly - blank framebuffer,
    wrong UVs, etc. The visual regression is immediate on first
    frame. Easy to spot.

Test:
  - Cold boot under shader_precompile_mode = Enabled. Progress
    bar reaches 100% (now sized to 17 non-batch units). FillVRAM
    / display / readback / etc. all render correctly.
  - Cold boot under Lazy. Pre-fill happens in the foreground
    before runloop, no nullptr at draw time on the first frame.
  - Cold boot under Disabled. First FillVRAM lazy-faults the
    fill pipeline; the embedded VS is bound, render is correct.
  - Toggle a non_dim setting (multisamples) to force full-flush
    of pipelines. CompilePipelines walks the matrix again,
    rebinds the embedded VS on every non-batch PSO. No leak,
    no use-after-free.
  - Toggle chroma_smoothing to fire the partial-flush branch
    (display pipelines only). Embedded VS is bound on the new
    display PSOs. Display still renders.
  - DestroyPipelines on shutdown / UpdateSettings: no
    m_fullscreen_quad_vertex_shader to Reset; nothing to leak.

Next patch in the arc: extend to non-batch pixel shaders that
don't bake state. vram_copy_ps and vram_update_depth_ps are
the next candidates - both are single-variant and have no
RESOLUTION_SCALE dependency (they sample VRAM coordinates
directly, no box-filter loop). After those, vram_fill_ps (two
variants: wrapped/unwrapped, interlaced/not - 4 total - so
TEMPLATE_VARIANTS gets its first non-empty entry). vram_read
and the batch matrix come later, after we have a cbuffer-
routing migration that lets RESOLUTION_SCALE be runtime-set
in non-batch shaders too.
2026-05-18 19:40:13 +02:00
LibretroAdmin
ca8c9b05e8 core/d3d12: scaffolding for pre-baked DXBC blobs
First step of the D3D12 pre-baked DXBC arc that was originally
flagged in 7b575a3's commit message as the natural follow-up to
the cbuffer / dim cache / chroma-split / non-batch-skip
optimisation series. Sets up the directory layout, regen tool,
and namespace scaffolding that subsequent patches will populate
with individual shader migrations.

Mirror of the Vulkan-side infrastructure that lives at
data/shaders/vulkan/ + src/common/vulkan/embedded_shaders.{h,cpp}
+ tools/regen_vulkan_spirv.py + src/common/vulkan/embedded_spirv/
(181 .inc files). The Vulkan side replaces glslang -> SPIR-V at
runtime with pre-compiled SPIR-V blobs; the D3D12 side will
replace D3DCompile -> DXBC at runtime with pre-compiled DXBC
blobs from fxc.exe.

Motivation: after the dim cache (10c53b8) and the chroma-split
(57ac62e), the D3D12 backend's UpdateSettings cost matrix has
one remaining 'still pays the full rebuild cost' class of
toggles - multisamples / per_sample_shading / uv_limits /
pgxp_depth / colour perspective - that genuinely invalidate
every HLSL variant in the 1164-cell batch pipeline matrix. On
a cold shader cache, walking that matrix synchronously costs on
the order of one minute under shader_precompile_mode = Enabled,
because every cell pays a fresh D3DCompile. Pre-baking the DXBC
eliminates the D3DCompile pass on those cells, dropping the
cold-flip cost to whatever the PSO-creation pass alone takes
(ID3D12Device::CreateGraphicsPipelineState, much faster than
D3DCompile + CreateGraphicsPipelineState).

The pre-bake also forecloses a class of 'd3dcompiler_47.dll
version drift invalidates the cached DXBC' failures the
runtime shader cache sometimes hits on driver updates.

Files in this scaffold patch:

* tools/regen_d3d12_dxbc.py - mirror of regen_vulkan_spirv.py
  for the fxc.exe / DXBC path. Walks data/shaders/d3d12/*.hlsl,
  determines stage from filename suffix (.vs.hlsl / .ps.hlsl /
  .cs.hlsl / .gs.hlsl), compiles each via fxc /O3 /T <stage>_5_0
  /E main, validates the output DXBC magic, and emits a .inc
  with const uint8_t array + size_t size_bytes. Honours a
  TEMPLATE_VARIANTS dict (initially empty) for shaders that need
  multiple pre-baked variants via /D defines. Auto-detects
  fxc.exe in the Windows 10 SDK install path; supports Wine on
  Linux via --wine and explicit --fxc PATH.

  In its empty-input case (the state immediately after this
  patch lands) the script exits zero with a 'nothing to do'
  message - no fxc lookup needed when there are no shaders to
  compile, so the script is runnable on systems without the
  Windows SDK / Wine until the first shader source actually
  shows up under data/shaders/d3d12/.

* data/shaders/d3d12/README.md - workflow documentation mirror
  of data/shaders/vulkan/README.md. Documents the
  edit-.hlsl -> rerun-script -> commit-both convention, the
  filename suffix -> shader stage mapping, the
  TEMPLATE_VARIANTS partition for multi-variant blobs, and the
  build-time constraint that the core build must never invoke
  fxc.exe.

* src/common/d3d12/embedded_shaders.h - namespace
  D3D12::EmbeddedShaders header. No extern declarations yet -
  this patch lays down the namespace scaffold. Subsequent
  patches add pairs of:

    extern const uint8_t k_<stem>_<stage>[];
    extern const size_t k_<stem>_<stage>_size_bytes;

  per migrated shader. Comment block documents the (data, size)
  -> D3D12_SHADER_BYTECODE aggregate-initialiser convention -
  no wrapper helper needed (unlike Vulkan's CreateShaderModule
  helper, because D3D12_SHADER_BYTECODE is a plain struct, not
  an API call).

* src/common/d3d12/embedded_shaders.cpp - storage TU. Currently
  empty inside the namespace block. As .inc files get added
  under src/common/d3d12/embedded_dxbc/, each is #included
  here from inside the namespace block so the arrays resolve
  against the header's extern declarations.

* Makefile.libretro and src/common/CMakeLists.txt - register
  embedded_shaders.cpp as a TU under the existing USE_D3D12
  gate. Compiles as part of the libretro build whenever D3D12
  support is enabled.

Subsequent patches in this arc will each:

  1. Add data/shaders/d3d12/<name>.<stage>.hlsl from the runtime
     ShaderGen output (cleaned up - no preprocessor noise that
     fxc handles at compile time anyway).
  2. Run tools/regen_d3d12_dxbc.py to produce the .inc(s).
  3. Add the matching extern declarations to embedded_shaders.h
     and the #include line to embedded_shaders.cpp.
  4. Switch the D3D12 backend call site (typically in
     gpu_hw_d3d12.cpp's GetXxxPipeline or CompilePipelines) to
     bind the embedded blob via D3D12_SHADER_BYTECODE instead
     of calling D3DCompile through the shared D3D11::ShaderCompiler
     path.

Ordering preference: start with non-batch, single-variant
shaders (vram_read / vram_write / vram_update_depth /
vram_copy / vram_fill / display) to validate the pipeline
end-to-end with minimal blast radius. Then the multi-variant
display / chroma family. Then the batch matrix - which is the
bulk of the work and where the cold-flip cost actually lives.

Note: the .inc files don't exist yet because fxc.exe isn't
runnable in this submission environment (Linux without Wine /
Windows SDK). The script is functional and will produce
.inc files as soon as it gets run on a host with fxc.exe.
This patch is intentionally a foundation: build succeeds with
no behaviour change, runtime is untouched, and the next patch
in the arc adds the first shader source + its .inc + the
matching call-site flip.

Test:
  - python3 tools/regen_d3d12_dxbc.py with no shader sources
    exits 0 with 'nothing to do' message. No fxc.exe required.
  - Build under USE_D3D12=1: embedded_shaders.cpp compiles
    cleanly as an empty namespace TU. No new symbols emitted.
  - Build under USE_D3D12=0: embedded_shaders.cpp is excluded
    from the build per the existing if(USE_D3D12) gate in
    src/common/CMakeLists.txt and the ifeq guard in
    Makefile.libretro.
  - Runtime: no behavior change. D3D12 backend continues to
    compile every shader through the existing D3DCompile path.
2026-05-18 09:14:52 +02:00
LibretroAdmin
6152d6e253 common/vulkan, core: pre-bake remaining batch FS texture filters (Bilinear / JINC2 / xBR families, 72 blobs total)
Patch 15 in the glslang-elimination series. Final batch FS conversion
covering all three remaining texture filter families:

  - Bilinear / BilinearBinAlpha    (24 blobs)
  - JINC2 / JINC2BinAlpha          (24 blobs)
  - xBR / xBRBinAlpha              (24 blobs)

After this lands, every batch FS slot in m_batch_fragment_shaders is
filled from a pre-baked SPIR-V blob; the runtime shadergen path for
batch shaders is gone. The m_shadergen unique_ptr and its
construction in CompilePipelines are removed since they have no
consumers left.

Each filter family shares the same 4-axis structural cube as the
patch-15-Bilinear-prep work that immediately preceded this commit:

  - Input interpolation qualifier (none / centroid / sample). 3.
  - Color input perspective (standard / noperspective). 2.
  - Dual-source output (1 vs 2 outputs). 2.
  - PGXP depth output (writes gl_FragDepth or omits it). 2.

3 x 2 x 2 x 2 = 24 blobs per filter family. UV_LIMITS is implicit
for all non-Nearest filter families because ShouldUseUVLimits() in
gpu_hw.cpp forces m_using_uv_limits = true whenever
m_texture_filtering != Nearest. The *BinAlpha variant of each
family collapses into the BINALPHA per-call specialisation constant
(id=110) rather than a structural axis - the only SPIR-V difference
is a single ialpha-quantisation step at the end of
FilteredSampleFromVRAM.

Filter bodies are lifted from gpu_hw_shadergen.cpp WriteBatchTexture-
Filter:
  - Bilinear   lines ~188-224  (4-tap weighted average)
  - JINC2      lines ~229-374  (4x4-tap sinc-windowed with anti-ring)
  - xBR        lines ~381-661  (5x5 neighbourhood, YCbCr edge
                                detection with BLEND_NONE / NORMAL /
                                DOMINANT directions per quadrant)

The xBR template is the largest of the three (~17 KiB per blob in
binary, the body is ~280 lines of GLSL). The translation from the
shadergen DSL is mechanical: float4 -> vec4, lerp -> mix, frac ->
fract, sign -> sign, VECTOR_EQ -> all(equal(...)), VECTOR_NEQ ->
any(notEqual(...)). The P() helper macro on xBR closes over coords
and uv_limits the same way the shadergen original does.

One xBR quirk is preserved verbatim: the Iw flag computation in
shadergen uses VECTOR_NEQ(H, ...) instead of VECTOR_NEQ(I, ...)
which is almost certainly a bug, but the alternative output would
differ visually from the released shadergen path. Preserving it
keeps the embedded-vs-shadergen output byte-identical for compat;
fixing it can be a follow-up patch with its own visual-test cycle.

GetBatchFragmentShader is now a switch on m_texture_filtering with
no fallback: every filter resolves to one of the embedded helpers.
The 'dual_source = ... || filter != Nearest' branch is preserved
verbatim from the shadergen-side derivation.

GenerateBatchFragmentShader and the m_shadergen unique_ptr are no
longer referenced. The construction at CompilePipelines is removed
along with the reset in DestroyPipelines and the declaration in
the header. The 'gpu_hw_shadergen.h' include is dropped from both
gpu_hw_vulkan.cpp and gpu_hw_vulkan.h. shader_compiler.{h,cpp},
dep/glslang/, and the Makefile / CMake glslang references are NOT
removed in this patch - that final cleanup will land separately
once this patch has been confirmed working in the wild, on the
theory that backing it out is easier with the runtime path's
support files still present.

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - spirv-val --target-env vulkan1.0 on all 72 new blobs: 72/72 valid.
  - Total .inc text size for the three filter families: ~9.2 MiB
    (Bilinear ~1.4 MiB, JINC2 ~2.6 MiB, xBR ~4.7 MiB). Binary
    contribution is ~25% of those numbers, so adding ~2.3 MiB to
    the .so. xBR alone is ~1.2 MiB binary contribution; this is the
    largest single chunk of pre-baked SPIR-V in the project.

Test priority - this patch covers the actively-rendered path for
users who enable any non-Nearest filter, so:

  - **Bilinear / BilinearBinAlpha**: smooth edges on textured
    surfaces, slight softening of palette textures. Test by setting
    the texture filter core option to Bilinear / Bilinear (BinAlpha).
  - **JINC2 / JINC2BinAlpha**: sharper sampling than Bilinear with
    visible anti-ringing on textured polygons. Heavier per-pixel
    cost; testing on FF8/9 backgrounds, MGS textured screens, etc.
  - **xBR / xBRBinAlpha**: pixel-art upscaler, very visible smooth
    edges on 2D sprites. Best exercise targets are 2D-heavy games -
    Castlevania SOTN, Symphony of the Night, Mega Man X4-6, the
    Suikoden series. xBR is the most complex shader in the project
    and produces visually-distinctive output that immediately
    reveals miscompilation.
  - All three families x every precompile mode (Disabled / Lazy /
    Enabled) x antialiasing toggle round-trips.
  - Cross-check: switch m_texture_filtering at runtime between all
    six filter values plus Nearest. Each transition should produce
    visibly-correct rendering without intermediate corruption.

After this lands and is confirmed working, the final patch in the
series will remove dep/glslang/, src/common/vulkan/shader_compiler
.{h,cpp}, and the Makefile.libretro / CMakeLists.txt entries that
pull glslang into the link.
2026-05-18 02:04:30 +02:00
LibretroAdmin
9dcfea435a common/vulkan, core: pre-bake batch FS textured Nearest slice (48 blobs)
Patch 14 in the glslang-elimination series. Second slice of the
batch FS conversion: the textured + Nearest-filter path
(texture_mode != Disabled AND m_texture_filter == Nearest). The
runtime decision in GetBatchFragmentShader now routes both the
untextured slice (patch 13) and this slice through the embedded
blob set; only textured non-Nearest still goes through
shadergen + glslang.

Five SPIR-V-structural axes (48 blobs total = 3 x 2 x 2 x 2 x 2):

  - Input interpolation qualifier (none / centroid / sample). 3.
  - Color input perspective (standard / noperspective). 2.
  - Dual-source output (1 vs 2 outputs). 2.
  - PGXP depth output (writes gl_FragDepth or omits it). 2.
  - UV limits (without / with v_uv_limits flat input). 2.

The UV_LIMITS axis is new vs the untextured slice - it controls
whether v_uv_limits is declared as a per-vertex flat input on the
FS, matching the per-session m_using_uv_limits choice that already
drives the matching batch VS structural pick. Without UV limits the
FS skips the per-sample clamp.

Three new per-call specialisation constants relative to the
untextured slice cover the textured texture_mode encoding:

  id = 107 PALETTE_4_BIT  (bool, actual_texture_mode == 0)
  id = 108 PALETTE_8_BIT  (bool, actual_texture_mode == 1)
  id = 109 RAW_TEXTURE    (bool, RawTextureBit set in texture_mode)

PALETTE = PALETTE_4_BIT || PALETTE_8_BIT is derived inside the
shader. The texture_mode bit layout is described in
src/core/gpu_types.h; the C++ side derives the three boolean spec
const values from the per-call texture_mode in GetBatchPipeline.
Untextured blobs do not declare 107-109; passing them is harmless
per the Vulkan spec (unrecognised spec const IDs in the supplied
VkSpecializationInfo are silently dropped).

The shader body covers the helpers the shadergen path inlines:
ApplyTextureWindow, ApplyUpscaledTextureWindow, FloatToIntegerCoords,
RGBA8ToRGBA5551, and SampleFromVRAM. SampleFromVRAM splits on the
derived PALETTE flag and, within the palette branch, on
PALETTE_4_BIT vs PALETTE_8_BIT to do the correct sub-texel address
arithmetic. fixYCoord on Vulkan is the identity function so we use
the coord directly. The Vulkan-only API_VULKAN-y-flip in the VS
already accounts for sample-space orientation.

Output and discard logic mirrors the shadergen TEXTURED branches:
the no-TRANSPARENCY case writes o_col0 (and o_col1 when DUAL_SOURCE)
unconditionally; the TRANSPARENCY case branches on the texel's
semitransparency bit and applies the TRANSPARENCY_ONLY_OPAQUE /
TRANSPARENCY_ONLY_TRANSPARENT per-texel discard the way the
shadergen does.

Helper-side index encoding for the 48-blob array, matching
GetBatchTexturedNearestFragmentShaderBlob in embedded_shaders.cpp:

  interp = per_sample_shading ? 2 : (msaa ? 1 : 0)        (0..2)
  persp  = noperspective_color ? 1 : 0                    (0..1)
  dual   = dual_source ? 1 : 0                            (0..1)
  pgxp   = pgxp_depth ? 1 : 0                             (0..1)
  uv     = uv_limits ? 1 : 0                              (0..1)
  index  = interp*16 + persp*8 + dual*4 + pgxp*2 + uv     (0..47)

The use_dual_source derivation in GetBatchFragmentShader matches
the shadergen-side check exactly: supports_dual_source_blend &&
((render_mode is transparent) || filter != Nearest). For the two
embedded branches (untextured and textured Nearest) the filter-half
of the OR evaluates false, so both branches fall through to the
render_mode test.

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - spirv-val --target-env vulkan1.0 on all 48 blobs - 48/48 valid.
  - Total .inc size: ~2.2 MiB across all 48 blobs (~46 KiB each in
    text form, which is the 0xNNNNNNNNu, packed layout from the regen
    tool; binary contribution to the linked output is roughly 1/4 of
    that, so ~550 KiB. Acceptable for a renderer that previously
    pulled in glslang at runtime).
  - --target-env vulkan1.0 vs un-flagged glslangValidator: byte-
    identical for every variant.

Test priority:
  - Routine smoke under Enabled / Lazy / Disabled with Nearest
    filter (the default). Any textured polygon in any game now
    exercises the embedded path: dialogue portraits, sprite layers,
    background textures - the vast majority of pixels in most
    games.
  - Toggle PGXP UV correction (drives m_using_uv_limits) to flip
    the UV_LIMITS structural axis. Games that benefit most from
    PGXP corrections - Final Fantasy series, Tekken 3, Soul Blade -
    are good exercise targets.
  - Antialiasing toggle round-trip under Disabled mode per the
    patch-5-fix protocol (flips interpolation axis).
  - Each texture mode is exercised by different draws: palette
    textures dominate (most game art is 4-bit indexed), with
    direct-16bpp showing up for render-to-texture effects. Raw
    textures appear in framebuffer effects and some UI passes.
  - Cross-check with shadergen path by switching texture filter to
    Bilinear (still on the shadergen path) and back to Nearest -
    rendering should remain consistent across the switch.

After this lands, the only remaining glslang consumers in the
Vulkan backend are the texture-filter slices (Bilinear /
BilinearBinAlpha, JINC2 / JINC2BinAlpha, xBR / xBRBinAlpha). Each
of those filter families has its own FilteredSampleFromVRAM body
and will get its own template in subsequent patches.
2026-05-18 01:43:15 +02:00
LibretroAdmin
cde9da3c4b common/vulkan, core: pre-bake batch FS untextured slice (24 blobs)
Patch 13 in the glslang-elimination series. First slice of the batch
FS conversion (Bucket B's bigger half) - the untextured path
(texture_mode == GPUTextureMode::Disabled). Solid-shaded polygons
with no VRAM sampling, no palette logic, no texture filter
machinery, no UV limit logic; the shader is comparatively simple
and a good first cut into the FS state space.

Four SPIR-V-structural axes (24 blobs total = 3 x 2 x 2 x 2):

  - Input interpolation qualifier on v_col0, matching the batch VS
    this FS is bound with. None / centroid / sample. (3)
  - Color input perspective. Standard / noperspective. (2)
  - Dual-source colour output. With dual-source-blend hardware the
    TransparentAndOpaque and OnlyTransparent render modes write a
    second colour at (location 0 index 1) that drives SRC1_COLOR /
    SRC1_ALPHA. The pipeline blend state references SRC1_* or it
    does not - the output declaration count must match. (2)
  - PGXP depth output. With PGXP Depth Buffer enabled the FS omits
    gl_FragDepth so the rasterizer-interpolated VS depth (a_pos.w
    under PGXP) is used directly and early-Z stays available.
    Splitting this structurally preserves the early-Z optimisation
    that the original shadergen also relies on. (2)

Per-call knobs (TRANSPARENCY tri-state from BatchRenderMode,
DITHERING, INTERLACING) and the remaining per-session knobs
(TRUE_COLOR, DITHERING_SCALED, RESOLUTION_SCALE) collapse into
specialisation constants applied at pipeline-create time:

  id =   0  RESOLUTION_SCALE              (uint, per-session)
  id = 100  TRANSPARENCY                  (bool, per-call)
  id = 101  TRANSPARENCY_ONLY_OPAQUE      (bool, per-call)
  id = 102  TRANSPARENCY_ONLY_TRANSPARENT (bool, per-call)
  id = 103  DITHERING                     (bool, per-call)
  id = 104  INTERLACING                   (bool, per-call)
  id = 105  DITHERING_SCALED              (bool, per-session)
  id = 106  TRUE_COLOR                    (bool, per-session)

The TRANSPARENCY trio encodes the BatchRenderMode enum:
  TransparencyDisabled  -> (false, *, *)
  TransparentAndOpaque  -> (true,  false, false)
  OnlyOpaque            -> (true,  true,  false)
  OnlyTransparent       -> (true,  false, true)

These are spec consts applied to the FS in GetBatchPipeline.
The same constant_id values are passed to the runtime shadergen-
compiled textured FS as well, where they are silently dropped per
the Vulkan spec (unrecognised spec const IDs in the supplied
VkSpecializationInfo are ignored).

GetBatchFragmentShader now branches on lookup_mode at the slot-
miss path: GPUTextureMode::Disabled goes to the embedded blob set,
everything else continues through GenerateBatchFragmentShader +
g_vulkan_shader_cache->GetFragmentShader. The textured slices land
in subsequent patches, one or two texture filter values at a time
(Nearest first, then Bilinear / BilinearBinAlpha / JINC2 / xBRZ).

The regen tool's TEMPLATE_VARIANTS manifest grows a second entry
covering the 24 untextured FS variants alongside the 18 batch VS
variants that were already there. Same -D-flag-driven template
mechanism as patch 12. The unused 'fs' local at the bottom of
GetBatchPipeline before the SetFragmentShader call now collects
the FS spec constant info; both batch VS and FS spec constants
are independent VkSpecializationInfo blobs per Vulkan rules.

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - spirv-val --target-env vulkan1.0 on all 24 blobs - 24/24 valid.
  - Total .inc size: ~127 KiB across all 24 blobs (~5 KiB each).
  - --target-env vulkan1.0 vs un-flagged glslangValidator: byte-
    identical for every variant.

Untextured batch draws are extremely common - solid-shaded
polygons in 2D HUDs, fills, debug overlays, the framebuffer clear
quads at the top of every frame on many games. If the embedded
untextured FS is wrong it will be IMMEDIATELY obvious on the first
frame of any game (HUD bars / lifebars / dialogue boxes will be
visibly broken).

Test priority:
  - Routine smoke under Enabled / Lazy / Disabled. Boot any game
    and watch the HUD: solid-shaded health/mana bars (most JRPGs),
    fade-in/fade-out solid alpha overlays (cutscenes), and 2D menu
    backgrounds are direct exercises of this path.
  - Antialiasing toggle round-trip under Disabled mode per the
    patch-5-fix protocol, confirming the structural blob-pick
    flips cleanly across interpolation axes as m_multisamples /
    m_per_sample_shading change.
  - Toggle PGXP Depth Buffer at runtime to flip the PGXP_DEPTH
    structural axis. Behaviour should be unchanged from the
    shadergen baseline.
  - Toggle 'disable color perspective' (under Texture Correction)
    to flip the perspective axis.
2026-05-18 01:15:55 +02:00
LibretroAdmin
890211d080 common/vulkan, core: pre-bake batch VS (18-blob template, 2 spec consts)
Patch 12 in the glslang-elimination series, and the first step into
Bucket B (batch shaders). The batch VS state space has three SPIR-V-
structurally-distinct axes that specialisation constants cannot fold:

  1. Vertex attribute layout. 2 inputs (untextured), 4 (textured
     without UV limits) or 5 (textured with UV limits). The 'in <type>
     <name>' declarations are part of the SPIR-V module interface.
  2. Output interpolation qualifier on the v_col0 / v_tex0 outputs.
     None / centroid / sample, selected per session by UsingMSAA() /
     UsingPerSampleShading(). These compile to OpMemberDecorate
     Centroid / Sample on the VertexData output block, which is not
     a spec-constant-controllable property of the SPIR-V module.
  3. Color output perspective. m_disable_color_perspective toggles a
     'noperspective' qualifier on v_col0, compiling to
     OpMemberDecorate NoPerspective.

3 x 3 x 2 = 18 structurally distinct blobs. Body-level knobs
(RESOLUTION_SCALE, PGXP_DEPTH) are common-knob specialisation
constants on every blob (constant_id = 0 and 3 per the project
convention).

To avoid maintaining 18 hand-edited .glsl files, the source is a
single template (data/shaders/vulkan/batch.vert.glsl) with #if
guards on TEXTURED, UV_LIMITS, INTERP_CENTROID / INTERP_SAMPLE, and
NOPERSP. The regen tool (tools/regen_vulkan_spirv.py) is extended
with a TEMPLATE_VARIANTS manifest that compiles the template once
per variant with the corresponding -D flags. Build-time impact is
zero - the script is still developer-only and the .inc files are
checked in.

The matching index encoding lives in src/common/vulkan/embedded_-
shaders.cpp next to the lookup helper:

  attr   = !textured ? 0 : (uv_limits ? 2 : 1)        (0..2)
  interp = per_sample_shading ? 2 : (msaa ? 1 : 0)    (0..2)
  persp  = noperspective_color ? 1 : 0                (0..1)
  index  = attr * 6 + interp * 2 + persp              (0..17)

The C++ side calls GetBatchVertexShaderBlob with the per-session
state (m_using_uv_limits, m_multisamples > 1, m_per_sample_shading,
m_disable_color_perspective) and the per-call 'textured' loop
variable to pick the right blob in CompilePipelines. The existing
m_batch_vertex_shaders[2] cache shape is preserved - the helper just
selects which two of the 18 blobs to instantiate this session.

Spec constants are applied at PIPELINE-create time inside
GetBatchPipeline rather than at module-create time, matching the
SpecConstants infrastructure laid down in patch 3. The values are
session-constant, so they are the same for every batch pipeline in
a session and the driver caches them keyed by (module + spec values)
in the pipeline cache.

Header-side ergonomics: instead of declaring 36 individual extern
arrays (18 SPIR-V + 18 size), the .h declares a small
EmbeddedShaderBlob struct and a single
k_batch_vs_blobs[18] array, plus the GetBatchVertexShaderBlob
helper. The same struct is intended to be reused for the eventual
batch FS conversion (likely many more blobs given the texture filter
state space).

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - spirv-val --target-env vulkan1.0 on all 18 blobs - 18/18 valid.
  - Total .inc size: ~53 KiB across all 18 blobs.
  - --target-env vulkan1.0 vs un-flagged glslangValidator: byte-
    identical for every variant.

The batch VS is filled in CompilePipelines for all three precompile
modes (Enabled, Lazy, Disabled), so this conversion does not change
the hot-path-pre-build pattern from the patch 10-fix. The two VS
modules are present before any batch draw on every code path.

Test priority:
  - Routine smoke under Enabled / Lazy / Disabled. Any rendering at
    all exercises both VS modules immediately, so visual regressions
    show up on the first frame of the first game booted.
  - SSAA / MSAA round-trip in Disabled mode (per the patch-5-fix
    protocol), specifically to confirm the structural-blob-pick
    flips through 'none' -> 'centroid' -> 'sample' interp variants
    cleanly as m_multisamples changes.
  - Toggle 'disable color perspective' core option (under Texture
    Correction) to flip the perspective axis; visual difference is
    subtle but the blob-pick change is real.
  - Toggle PGXP Geometry Correction (drives UV_LIMITS via
    m_using_uv_limits) and PGXP Depth Buffer (drives PGXP_DEPTH
    spec const) to flip the body and attribute-layout axes.

After this lands, the only remaining runtime glslang consumer in
the Vulkan backend is the batch FS (GenerateBatchFragmentShader at
gpu_hw_shadergen.cpp line ~667). That is a far larger state space
- six texture filters x ~14 boolean macros plus the structural
USE_DUAL_SOURCE / PALETTE_* axes - and will need to land in
several patches, one or two texture filters at a time.
2026-05-18 00:56:03 +02:00
LibretroAdmin
74bebf8908 common/vulkan, core: pre-bake VRAM update-depth FS (MSAA blob pair, no spec consts)
Patch 11 in the glslang-elimination series, and the last non-batch
shader on the runtime shadergen path. This was the original patch 5
culprit - lazy-building this pipeline inside UpdateSettings tripped an
NVIDIA driver quirk that produced corrupted rendering on the next
frame. The preceding fix patch (pre-build hot-path utility pipelines
in shader_precompile=Disabled) sidestepped the failure mode by
ensuring m_vram_update_depth_pipeline is always built before any
cmdbuf that could need it, regardless of precompile mode, so this
conversion is now safe to land.

Shader converted:

  VRAMUpdateDepthFragmentShader. Rebuilds the depth attachment of the
  upscaled VRAM render pass from the colour attachment's alpha after a
  framebuffer recreation (resolution scale change, antialiasing toggle,
  true colour toggle). Trivial body - one texelFetch, one assignment
  to gl_FragDepth.

  Blobs:
    vram_update_depth_fs      - sampler2D, texelFetch(samp0, coord, 0).
    vram_update_depth_msaa_fs - sampler2DMS, texelFetch(samp0, coord,
                                gl_SampleID). The gl_SampleID reference
                                forces per-sample shading per the
                                Vulkan spec, regardless of the
                                SetMultisamples per_sample_shading
                                flag the C++ side leaves at false (the
                                original shadergen relies on the same
                                implicit-per-sample behaviour).

  No spec constants on either blob - there is nothing to specialise.
  The pipeline-state knobs the C++ side cares about (sample count,
  depth state, blend state) are pipeline-creation-time choices, not
  shader-source knobs.

Bindings unchanged: set=0 binding=1 sampler from
m_single_sampler_descriptor_set_layout, no UBO at binding 0 (the
descriptor set layout only declares binding 1; the pipeline layout's
push constants are declared for ABI symmetry with other consumers but
are not read by this shader, and the C++ caller does not push any).

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - spirv-val --target-env vulkan1.0 on both blobs (219 / 242 words,
    0.9 KiB each - the smallest blobs in the project).
  - --target-env vulkan1.0 vs un-flagged glslangValidator: byte-
    identical SPIR-V for both.

m_vram_update_depth_pipeline is on the antialiasing-toggle hot path
(UpdateSettings -> UpdateDepthBufferFromMaskBit at gpu_hw_vulkan.cpp
line ~817). With the preceding fix patch in place, the Disabled-mode
pre-build in CompilePipelines ensures the pipeline is always ready
before UpdateSettings' second cmdbuf starts recording, so the bug
that broke the original patch 5 (lazy build of MSAA depth blob in
the second SSAA toggle cycle, NVIDIA driver producing corrupt next
frame) is structurally impossible here.

Test priority:
  - shader_precompile=Disabled: same SSAA / MSAA round-trip protocol
    Lib used to surface the original patch 5 bug. Toggle SSAA None
    -> 8x -> None -> 8x, MSAA 1 -> 4 -> 1 -> 4, resolution scale
    1 -> 2 -> 1 -> 2, true colour off -> on -> off. Each toggle
    should produce a clean frame.
  - shader_precompile=Enabled / Lazy: routine smoke (any game).

After this lands, every non-batch shader in the Vulkan backend is
pre-baked SPIR-V; the only remaining runtime glslang consumers are
the two batch shaders (GenerateBatchVertexShader and
GenerateBatchFragmentShader at gpu_hw_vulkan.cpp lines ~1462 and
~1799). Those are Bucket B and a separate refactoring effort -
their state space is wide enough to warrant a different conversion
strategy from the per-shader spec-const collapses used here.
2026-05-18 00:38:31 +02:00
LibretroAdmin
cc69fc08e5 common/vulkan, core: pre-bake VRAM readback FS (MSAA blob pair, 1-2 spec consts)
Patch 10 in the glslang-elimination series. The shadergen helper is
named GenerateVRAMReadFragmentShader, but the pipeline it lives in
(m_vram_readback_pipeline) is the CPU readback path: it samples the
upscaled m_vram_texture, runs a box filter to downsample blocks back
to PSX-native 16bpp, and packs two adjacent 16-bit pixels into one
RGBA8 output texel for the host to pick up via staging.

Shader converted:

  VRAMReadFragmentShader. Single output slot, no per-frame variance
  - the existing C++ side already caches it as a single
  m_vram_readback_pipeline VkPipeline. Two blobs only because of the
  source texture's MSAA structural split (sampler2D vs sampler2DMS).

  Blobs:
    vram_readback_fs      - sampler2D, no per-pixel sample averaging.
    vram_readback_msaa_fs - sampler2DMS, LoadVRAM averages
                            MULTISAMPLES texelFetch results before
                            the box filter runs.

  Common spec constant:
    id = 0 RESOLUTION_SCALE (uint) - drives both the base_coords stride
                                     and the box-filter inner-loop
                                     bounds.
  MSAA blob adds:
    id = 1 MULTISAMPLES     (uint) - LoadVRAM averaging loop bound.

The OpenGL lower-left-origin Y-flip branch in the shadergen source is
omitted: Vulkan VRAM Y matches scanout Y directly, so the conditional
fold-up never fires on this backend. The single output pipeline keeps
its single C++ slot - no per-mode array needed.

Render target (m_vram_readback_texture) is always single-sample at
line 1118, so the pipeline's multisample state stays at the default
single-sample on both blobs. As with display, the MSAA-ness lives
entirely in the input sampler type.

Bindings unchanged: push_constant for the UBO, set=0 binding=1 for
the source sampler, matching m_single_sampler_pipeline_layout.

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

VRAM readback is the rarest of the utility pipelines - it's exercised
only when something reads from upscaled VRAM through the host (save-
state capture, debugger introspection, certain games that read back
their own framebuffer for special effects). Not on the SSAA-toggle
hot-build path; shader_precompile=Disabled is expected to behave
identically to Enabled / Lazy.

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke.
  - shader_precompile=Disabled: any savestate save + load round-trip
    (forces a readback). A few games also self-read for visual
    effects - Silent Hill's fog overlay and the Castlevania:
    Symphony of the Night transition flashes are known triggers.
    SSAA round-trip is incidental but quick.

After this lands, the only non-batch shaders still on the runtime
shadergen + glslang path are the VRAM update depth pair, which is on
the SSAA-toggle hot-build path and broke patch 5. Plan for those is
to introduce a targeted pre-build at CompilePipelines time even in
Disabled mode, separately from the SPIR-V conversion itself.
2026-05-18 00:16:58 +02:00
LibretroAdmin
59b6fac749 common/vulkan, core: pre-bake display FS (MSAA blob pair, 5-6 spec consts)
Patch 9 in the glslang-elimination series. The display FS is the
densest shader in the project - shadergen takes depth_24bit (bool) x
interlace_mode (None/InterleavedFields/SeparateFields) x smooth_chroma
(bool) source variants, and the source m_vram_texture's sample count
varies with MSAA on top of that. The depth_24bit / interlace_mode pair
is the only knob set that varies at runtime within a session
(GPUSTAT.display_area_color_depth_24 and the CRTC interlace mode flip
during normal gameplay); smooth_chroma, MSAA, and RESOLUTION_SCALE are
all session-constant (toggling any of them goes through UpdateSettings
-> DestroyPipelines).

Shader converted:

  DisplayFragmentShader. Reads the upscaled m_vram_texture, applies
  field-discard for interlaced output, and writes the visible scanout
  rectangle to m_display_texture - either as direct 16bpp samples or as
  reconstructed 24bpp YUV-smoothed colour depending on the GPUSTAT bit.

  Blobs:
    display_fs       - source sampler is sampler2D       (non-MSAA path).
    display_msaa_fs  - source sampler is sampler2DMS, plus an extra
                       MULTISAMPLES spec constant for the LoadVRAM
                       averaging loop bound.

  Common spec constants on both blobs:
    id =   0 RESOLUTION_SCALE (uint) - common-knob convention.
    id = 100 DEPTH_24BIT      (bool) - 16bpp vs 24bpp reconstruction.
    id = 101 INTERLACED       (bool) - field-discard logic gate.
    id = 102 INTERLEAVED      (bool) - only meaningful when INTERLACED.
                                       Selects interleaved-both-fields
                                       coord folding vs separate-field.
    id = 103 SMOOTH_CHROMA    (bool) - only meaningful when DEPTH_24BIT.
                                       Enables bilinear UV smoothing.

  MSAA blob adds:
    id =   1 MULTISAMPLES     (uint) - common-knob convention.

The m_display_pipelines[depth_24][interlace_mode] 2x3 slot table is
preserved; each entry gets its own spec-const-specialised pipeline at
first-touch, like the VRAM fill pattern. interlace_mode is folded into
INTERLACED / INTERLEAVED at the call site:
  None              -> INTERLACED=false, INTERLEAVED=*  (INTERLEAVED
                                                         irrelevant when
                                                         INTERLACED is
                                                         false)
  InterleavedFields -> INTERLACED=true,  INTERLEAVED=true
  SeparateFields    -> INTERLACED=true,  INTERLEAVED=false

The render target (m_display_texture) is always single-sample at this
stage (line 1114 hardcodes VK_SAMPLE_COUNT_1_BIT regardless of MSAA);
the pipeline's multisample state is therefore single-sample on both
blobs, matching the original which did not call SetMultisamples either.
The MSAA-ness lives entirely in the input sampler type and the LoadVRAM
loop on the MSAA blob.

Bindings unchanged: push_constant for the UBO, set=0 binding=1 for the
source sampler, matching m_single_sampler_pipeline_layout.

The fixYCoord call from the original shadergen is dropped because the
Vulkan-side fixYCoord is the identity function (PSX VRAM Y matches
Vulkan Y); removing it spares a redundant function definition. The
GLSL-side helper macros (float4 / uint2 / etc) from WriteHeader are
also dropped since the .glsl files only need to work for Vulkan and
GLSL-native types are clearer than HLSL-style aliases.

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

Like VRAM fill / copy / write, the display pipeline is built on its
own first-touch path and is not on the UpdateSettings SSAA-toggle
hot-build path. shader_precompile=Disabled is expected to behave
identically to Enabled / Lazy.

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke (any game).
  - shader_precompile=Disabled: any game that flips interlace_mode
    during gameplay (interlaced cutscenes -> progressive gameplay
    transitions) or that toggles 24bpp display mode (FMV intros in
    most JRPGs). Smooth-chroma path is exercised by enabling the
    Chroma Smoothing 24-bit option in the core options and watching
    an FMV. Worth doing a coarse SSAA round-trip too even though the
    display pipeline shouldn't be sensitive to it.
2026-05-18 00:02:50 +02:00
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
LibretroAdmin
34cda08c7a common/vulkan, core: pre-bake VRAM copy FS (single blob, 2 spec consts)
Patch 7 in the glslang-elimination series. VRAM copy is the simplest
remaining shader from the original shadergen-driven set: the source-side
has no MSAA variant (the shadergen hard-codes msaa=false at the top of
GenerateVRAMCopyFragmentShader because Vulkan won't let us bind the
source texture to both an input sampler and a colour attachment), so
unlike VRAM read / update-depth we collapse to a single SPIR-V blob with
just two spec constants.

Shader converted:

  VRAMCopyFragmentShader. Copies a rectangular region from one part of
  upscaled VRAM to another, with PSX-style modulo-1024x512-VRAM wrap-
  around at the destination and optional mask-bit writeback.

  Spec constants used (documented in the .glsl):
    id = 0  RESOLUTION_SCALE (uint) - common-knob convention.
    id = 3  PGXP_DEPTH       (bool) - common-knob convention.

  No MSAA spec const; no shader-specific spec consts. The depth_test
  state knob the C++ side cares about is a SetDepthState pipeline-state
  toggle, not a shader knob, so it stays as a 2-entry slot lookup in
  m_vram_copy_pipelines and does not factor into spec constants.

Bindings unchanged: push_constant block for the UBO, set=0 binding=1 for
the source sampler2D, matching m_single_sampler_pipeline_layout.

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - spirv-val --target-env vulkan1.0 on the regenerated blob (1203
    words, 4.7 KiB).
  - Verified --target-env vulkan1.0 produces byte-identical SPIR-V to
    the un-flagged glslangValidator default (relevant since the runtime
    glslang path does not call setEnvTarget; both paths emit the same
    bytes for our shaders).

Like patch 6, this pipeline is not on the SSAA-toggle hot path - VRAM
copy is exercised by game-driven VRAM-to-VRAM blits, not by
UpdateSettings. shader_precompile=Disabled is therefore expected to
behave identically to Enabled / Lazy for this pipeline.

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke (any game).
  - shader_precompile=Disabled: any game that performs frequent VRAM
    copies (Final Fantasy series, anything with prerendered backgrounds
    streamed into VRAM). SSAA toggle round-trip should be fine.
2026-05-17 22:57:44 +02:00
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
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
LibretroAdmin
6727587113 common/vulkan, core: spec-constant infrastructure + adaptive downsample mip/blur pre-bake
Patch 3 in the glslang-elimination series. Introduces the specialization-
constant plumbing that subsequent patches need, and applies it to two
adaptive-downsample shaders that demonstrate both the no-spec-const and
the with-spec-const paths through the new infrastructure.

Infrastructure additions:

- Vulkan::SpecConstants class in builders.{h,cpp}. A small builder that
  owns a VkSpecializationMapEntry array and a 4-byte-per-slot payload
  buffer, exposing AddBool/AddUInt/AddInt/AddFloat by constant_id. The
  consumer typically declares a SpecConstants on the same stack frame
  as the GraphicsPipelineBuilder, fills it, and passes GetInfo() to
  SetVertexShader / SetFragmentShader. GetInfo() returns nullptr if no
  entries have been added (equivalent to no specialization info), so
  the same code path can serve both spec-const and non-spec-const
  shaders without conditionals at the call site.

  Up to MAX_ENTRIES (16) constants per stage; AddBool stores the
  value as a 4-byte word (1 for true, 0 for false) per the Vulkan
  spec-constant ABI. Larger types (vec4, double) intentionally not
  supported - no current shader needs them; can be added later if
  the batch shaders end up needing wider values.

- GraphicsPipelineBuilder::SetVertexShader and ::SetFragmentShader now
  take an optional 'const VkSpecializationInfo*' that defaults to
  nullptr. All 36 existing call sites continue to compile unchanged
  via the default. SetShaderStage is also extended; in addition to
  threading spec_info through it now explicitly clears pNext, flags,
  and pSpecializationInfo on every call. The builder is sometimes
  reused across pipelines without an intervening Clear() (e.g. the
  display / cursor pair in LibretroVulkanHostDisplay::CreateResources),
  so a stale spec_info pointer or flags bit could previously leak from
  one pipeline build into the next. Latent issue pre-existing this
  patch; fixed defensively because the new code path exercises it.

Spec-constant ID allocation convention (documented in the SpecConstants
class header and adhered to project-wide):

    0-99    common knobs (RESOLUTION_SCALE, MULTISAMPLES,
            PER_SAMPLE_SHADING, PGXP_DEPTH) - to be plumbed by patch 4.
    100+    shader-specific; e.g. FIRST_PASS for the adaptive-downsample
            mip FS in this patch.

Shaders converted by this patch:

1. AdaptiveDownsampleBlurFragmentShader (no spec constants). One
   SPIR-V blob, replaces GetDownsampleBlurPassPipeline's runtime
   glslang call. Establishes that the embedded path works alongside
   the spec-constant path - the call site is identical to a patch-1
   or patch-2 conversion.

2. AdaptiveDownsampleMipFragmentShader. The 'first_pass' bool that
   previously produced two distinct GLSL source strings (and so two
   cached SPIR-V blobs) is now a single 'bool FIRST_PASS' spec
   constant at constant_id=100. ONE SPIR-V blob serves both
   GetDownsampleFirstPassPipeline (Adaptive branch) with
   FIRST_PASS=true, and GetDownsampleMidPassPipeline with
   FIRST_PASS=false. The branch is preserved as a runtime 'if'; the
   driver's SPIR-V optimizer drops the dead arm at vkCreateGraphics
   Pipelines time because the bool is a compile-time constant in
   that context.

   spirv-dis on the blob confirms 'OpDecorate %FIRST_PASS SpecId 100'
   and 'OpSpecConstantFalse %bool' are emitted as expected.

Other changes in gpu_hw_vulkan.cpp:

- GetDownsampleFirstPassPipeline: m_shadergen null-check moved from
  function entry into the Box branch (which still uses
  GenerateBoxSampleDownsampleFragmentShader at runtime). The Adaptive
  branch no longer needs the check - pre-baked SPIR-V doesn't depend
  on shadergen.
- GetDownsampleMidPassPipeline and GetDownsampleBlurPassPipeline:
  m_shadergen null-checks removed entirely - both are Adaptive-only
  (early-return on non-Adaptive) and now fully pre-baked.

Cumulative state after this patch:

- 7 of ~25-30 Bucket A variants converted (1 in patch 1, 4 in patch 2,
  2 in this patch; the mip FS is one source but two specializations).
- Spec-constant infrastructure ready for patches 4+ to plumb
  RESOLUTION_SCALE / MULTISAMPLES / PGXP_DEPTH into the remaining
  utility shaders.
- glslang and the runtime ShaderGen path still in the tree, still used
  by every Vulkan path not yet converted (batch shaders, remaining
  utility shaders, Box downsample, all GL/D3D backends).

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 mip FS confirms the OpDecorate SpecId 100 +
  OpSpecConstantFalse pair.
- g++ -fsyntax-only on both modified TUs (builders.cpp,
  embedded_shaders.cpp) is clean; the only warnings are two
  pre-existing unused-parameter warnings in SamplerBuilder::Create
  and BufferViewBuilder::Create, unchanged by this patch.

Smoke tests at user's end:

- Cold-boot with the shader cache nuked and the downsample mode set
  to Adaptive. All three Adaptive pipelines must succeed; the visual
  output should be byte-identical to the pre-patch baseline (same
  algorithm, same texture binds, just specialized SPIR-V instead of
  hand-rolled GLSL).
- Same cold-boot test with Box mode is also useful - it exercises
  that the Box branch in GetDownsampleFirstPassPipeline still works
  via the runtime-glslang path (regression check on the m_shadergen
  null-check relocation).
2026-05-17 20:39:19 +02:00
LibretroAdmin
64c9adaca8 common/vulkan, core: pre-bake UV-quad VS and three presentation-stage shaders
Patch 2 in the glslang-elimination series (probe was 2cbcefc). Converts
four more shaders to the pre-baked SPIR-V path established in the probe.
No new infrastructure - each blob just adds an extern pair to
embedded_shaders.h, a #include inside the namespace block in
embedded_shaders.cpp, and a call-site swap. The probe pattern handles
N=5 blobs as cleanly as it handled N=1.

Shaders converted by this patch:

1. UV-quad vertex shader (GPU_HW_Vulkan::GetUVQuadVertexShader, the
   variant of the screen-quad that maps the swept triangle into a
   [u_uv_min..u_uv_max] UV rect from a push constant). The runtime
   equivalent is ShaderGen::GenerateUVQuadVertexShader().

2. Presentation-stage fullscreen-quad VS - the one with the u_src_rect
   push constant, distinct from the GPU_HW screen quad. Used only by
   LibretroVulkanHostDisplay to blit the rendered frame and the
   software cursor into the libretro framebuffer.

3. Presentation-stage display FS - writes the source texture opaque.

4. Presentation-stage cursor FS - preserves source alpha for blending.

Shaders 2-4 previously lived as inline R"()" string literals at the
top of LibretroVulkanHostDisplay::CreateResources() in
src/core/gpu_hw_vulkan.cpp. They are now in data/shaders/vulkan/ with
matching pre-compiled SPIR-V under src/common/vulkan/embedded_spirv/.
The C++-side pipeline layout (16-byte u_src_rect push constant, single
combined image sampler at set 0 / binding 0) is unchanged, so no
descriptor or push-constant byte-layout coordination is needed.

Cumulative state after this patch:

- 5 of ~25-30 utility-shader variants in Bucket A converted.
- glslang is still in dep/ and still in the build - it is needed by
  every shader generator not yet converted, in particular all of
  GPU_HW_ShaderGen plus the remaining utility shaders that bake
  RESOLUTION_SCALE / MULTISAMPLES / PGXP_DEPTH into source via
  WriteCommonFunctions. The next patch in the series introduces
  specialization-constant infrastructure in GraphicsPipelineBuilder so
  those constants can be moved out of GLSL into SPIR-V OpSpecConstant,
  enabling the rest of Bucket A.

Smoke tests in the sandbox:

- All 5 .inc files reconstruct to SPIR-V that passes
  'spirv-val --target-env vulkan1.0'.
- spirv-dis confirms each blob's entry point, descriptor bindings, and
  push-constant block match the call-site expectations.
- g++ -fsyntax-only on embedded_shaders.cpp against the real tree
  headers is clean.

Smoke tests at user's end:

- Cold-boot the Vulkan backend with the disk shader cache nuked. The
  three presentation-stage pipelines build during
  LibretroVulkanHostDisplay::CreateResources(); the UV-quad VS is
  faulted in lazily by any of the three adaptive-downsample pipeline
  paths (line ~2284 in gpu_hw_vulkan.cpp post-patch). All four must
  succeed for the emulator to reach a first frame.
- A second cold boot (same nuke) verifies the embedded path is
  reproducibly correct, not relying on any leftover state.
2026-05-17 20:23:11 +02:00
LibretroAdmin
2cbcefcff6 common/vulkan, core: pre-bake screen-quad VS, no glslang at runtime
This is the probe patch in a series that eliminates dep/glslang from the
libretro core build. It establishes the infrastructure - the GLSL source
tree, the embedded-SPIR-V loader, the regeneration tool, and the build
wiring - by converting exactly one call site so the pattern can be
smoke-tested in isolation before the rest of Bucket A and then Bucket B
follow in subsequent patches. glslang stays in the tree until the final
patch in the series, which removes dep/glslang and the surrounding
shader_compiler.cpp.

What this patch does:

- Adds data/shaders/vulkan/screen_quad.vert.glsl - the GLSL source for
  the screen-quad VS, kept in the tree for human readability. The build
  does NOT see this file; it is consumed only by the offline regen tool.

- Adds src/common/vulkan/embedded_spirv/screen_quad_vs.inc - the
  pre-compiled SPIR-V blob as a 'const uint32_t[]' (367 words, 1.4 KiB),
  generated offline from the .glsl by glslangValidator targeting
  vulkan1.0. The .inc is checked in and #include'd by embedded_shaders.cpp
  inside namespace Vulkan::EmbeddedShaders. spirv-val confirms the blob
  is valid Vulkan 1.0 SPIR-V (magic 0x07230203, version 1.0).

- Adds src/common/vulkan/embedded_shaders.{h,cpp}. The header exposes a
  pair of extern declarations per blob and a single
  CreateShaderModule(const uint32_t*, size_t) loader. The loader
  intentionally bypasses Vulkan::ShaderCache - pre-baked SPIR-V is
  already final, so there is nothing to compile and nothing worth
  caching on disk. Just a sized-and-checked vkCreateShaderModule call.

- Adds tools/regen_vulkan_spirv.py - the offline workflow for
  contributors who edit shader .glsl sources. It invokes
  glslangValidator on each .glsl under data/shaders/vulkan/ and rewrites
  the matching .inc. CRITICAL: this tool is NEVER invoked by Make or
  CMake. The build sees only .h/.cpp/.inc files and has no shader-
  compiler dependency. Contributors run the script manually after
  editing .glsl, commit both files together.

- Adds data/shaders/vulkan/README.md documenting the workflow and the
  invariant that the build must never invoke an external shader
  compiler.

- Switches GPU_HW_Vulkan::GetFullscreenQuadVertexShader() (in
  src/core/gpu_hw_vulkan.cpp) from
      g_vulkan_shader_cache->GetVertexShader(m_shadergen->Generate...)
  to
      Vulkan::EmbeddedShaders::CreateShaderModule(k_screen_quad_vs, ...).
  The m_shadergen null-check is removed since the embedded path has no
  such ordering dependency. The function's contract (lazy creation,
  stored in m_fullscreen_quad_vertex_shader, returned to multiple
  pipeline builders) is unchanged.

- Registers src/common/vulkan/embedded_shaders.cpp in both
  Makefile.libretro (alphabetical order in the vulkan/ SOURCES_CXX
  block) and src/common/CMakeLists.txt (alphabetical order in the
  common target's source list). Neither change adds any build-tool
  dependency; the new .cpp compiles like any other.

What this patch does NOT do:

- It does not remove dep/glslang or the SOURCES_CXX entries that pull
  in its 99 translation units. glslang is still needed for every shader
  generator that has not yet been converted - in particular all of
  GPU_HW_ShaderGen, plus the remaining utility shaders (UV quad VS,
  VRAM read/copy/write/fill/update-depth FS, display FS, adaptive
  downsample mip/blur/composite FS, box-sample downsample FS).
  Subsequent patches in this series convert those one bucket at a time.
  The final patch removes dep/glslang, src/common/vulkan/shader_compiler.{h,cpp},
  the glslang link line in CMake, and the Makefile entries.

- It does not touch the disk-backed Vulkan::ShaderCache. That cache
  still serves the runtime-glslang path for remaining shaders.

Smoke tests performed in isolation:

- glslangValidator --target-env vulkan1.0 succeeds on the .glsl source,
  producing 367 32-bit words of SPIR-V.
- spirv-val --target-env vulkan1.0 reports VALID on the blob.
- spirv-dis confirms the entry point, VertexData block, and Position/
  VertexIndex built-ins are correctly emitted.
- A standalone harness including the .inc inside the
  Vulkan::EmbeddedShaders namespace and calling CreateShaderModule
  through a stubbed vkCreateShaderModule reads back the correct magic
  word and size.
- g++ -fsyntax-only on embedded_shaders.cpp against the real headers
  in the tree (excluding only the platform-specific X11 surface define
  not used by this TU) is clean.

Smoke tests that require the user's setup:

- Real build on Windows (RTX 5090) and macOS.
- Vulkan core boots, RetroArch's pipeline cache populates, and at
  least one game renders past the first frame using the screen-quad
  pipeline (VRAM ops, display, downsample - all of which call
  GetFullscreenQuadVertexShader internally).
2026-05-17 20:09:57 +02:00
DarthMew
4335a2ad7e Reduce the amount of files in the repo & rename 'duckstation-libretro' to 'libretro' 2022-02-21 23:16:22 +01:00
Connor McLaughlin
30a53fbb3c GameSettings: Disable GPU runahead for Red Asphalt PAL 2022-01-06 22:47:10 +10:00
bslenul
1af69ff38e Database update for mouse compatible games 2021-11-29 12:29:05 +01:00
Connor McLaughlin
46f9da97d2
Merge pull request #2749 from bslenul/guncon-update
Update database for GunCon/Justifier games
2021-11-28 11:22:20 +10:00
Risae
ab831daa18
Update chtdb.txt (#2735)
* Update chtdb.txt

Adds the following cheats to Gran Turismo 1 & 2:

Gran Turismo 1:
- 60 FPS (NTSC-U 1.1, NTSC-U 1.0, PAL, JP NTSC-J)
- Simulation timescale in Arcade (NTSC-U 1.1, PAL)

Gran Turismo 2:
- 16:9 Widescreen Hack 2.0 (NTSC-U 1.2, NTSC-U 1.1, PAL, NTSC-J 1.1, NTSC-J 1.0)
- 21:9 Widescreen Hack 2.0 (NTSC-U 1.2, NTSC-U 1.1, PAL, NTSC-J 1.1, NTSC-J 1.0)
- 60 FPS (NTSC-U 1.2, NTSC-U 1.1, PAL, Arcade Disc: JP NTSC-J 1.0, Simulation Disc: JP NTSC-J 1.1, NTSC-J 1.0)
- Metric units code (NTSC-U 1.2, NTSC-U 1.1, PAL)
- Full detail AI cars (NTSC-U 1.1, NTSC-U 1.2, PAL, NTSC-J 1.1, NTSC-J 1.0)
- Slightly higher draw distance (NTSC-U 1.2, NTSC-U 1.1, PAL, NTSC-J 1.1, NTSC-J 1.0)
- Use 8MB RAM for polygon buffers (NTSC-U 1.1, NTSC-U 1.2, PAL, NTSC-J 1.1, JP NTSC-J 1.0)

Credits go to
https://cookieplmonster.github.io/
https://cookieplmonster.github.io/mods/gran-turismo/
https://cookieplmonster.github.io/mods/gran-turismo-2/

* Update chtdb.txt

Added:

";This game currently has no cheats" to some games that didn't have any cheats yet.

HUD & rear view mirror toggle (NTSC-U 1.2, NTSC-U 1.1, PAL, NTSC-J 1.1, NTSC-J 1.0)
Replay cameras in race (NTSC-U 1.1, NTSC-U 1.2, PAL, Arcade Disc: NTSC-J 1.0, Simulation Disc: NTSC-J 1.1, NTSC-J 1.0)
True Endurance ( NTSC-U 1.2, NTSC-U 1.1, PAL, NTSC-J 1.1)

Removed:

Gran Turismo 2 (USA, v1.1)
#Widescreen 16-9 option Better Graphics
#Widescreen 16-9

Gran Turismo 2 (Euro)
#Widescreen 16-9
#Widescreen 16-9 Better Graphics (Optional)

Gran Turismo 2 (Arcade + Gran Turismo Mode) (Japan, Asia)
#Widescreen 16-9 v1.0
#Widescreen 16-9 v1.0 - Better Graphics (Optional)
#Widescreen 16-9 V1.1
#Widescreen 16-9 v1.1 - Better Graphics (Optional)

* Update chtdb.txt

Removed unnecessary line break
2021-11-28 11:22:04 +10:00
bslenul
19f7516b5b Update database for GunCon/Justifier games 2021-11-27 14:26:57 +01:00
Chromaryu
23a34cb119
GameDB: Add missing definition 2021-11-25 15:11:12 +09:00
Connor McLaughlin
10cf8ef638 GameSettings: Force true color off for SLPS-00078 2021-11-14 23:47:15 +10:00
Connor McLaughlin
acace84bd1 GameDB: Use hash for SLPS-00078 2021-11-14 23:44:26 +10:00
Connor McLaughlin
f982a2a580 Update merged gamedb 2021-11-11 15:36:58 +10:00
Connor McLaughlin
c78047c28f Update gamedb with new hashes for colliding games 2021-11-10 12:53:41 +10:00
Connor McLaughlin
ae92ce8405 Update game database with NeGcon entries from PiperCalls 2021-11-02 11:25:54 +10:00
Connor McLaughlin
d7b913288d Update SDL game controller database 2021-11-02 11:25:54 +10:00
Connor McLaughlin
ccf5006bc8
Merge pull request #2668 from CookiePLMonster/dump-verification
Implement image verification
2021-10-25 17:30:20 +10:00
Connor McLaughlin
45a2e0df5c Update compatibility list 2021-10-25 17:15:56 +10:00
Silent
764c6e4a72
Rewrote gamedb with revisional hashes 2021-10-23 21:46:56 +02:00
Connor McLaughlin
1cc6ee4857 Update bundled SDL game controller database 2021-09-21 10:05:30 +10:00
Connor McLaughlin
f1d6e81751 Update compatibility list 2021-09-15 12:24:32 +10:00
Connor McLaughlin
396b4a48f2 Data: Update SDL game controller database 2021-09-08 10:49:54 +10:00
Connor McLaughlin
6c628bb89f Update compatibility list 2021-09-05 10:11:00 +10:00
Connor McLaughlin
8d57229bdc Update compatibility list 2021-09-01 15:01:39 +10:00
Connor McLaughlin
7f3f5ce1bd Update compatibility list 2021-09-01 14:57:21 +10:00