Commit graph

6,088 commits

Author SHA1 Message Date
LibretroAdmin
116a70eae2 core: route PGXP_DEPTH through the batch UBO for the FS
Companion to 49c0f82 (VS-side PGXP_DEPTH routing). The FS-side
routing converts SV_Depth from a per-variant signature decision
to an always-declared output, with a single runtime-branch write
at the end of main() picking the right depth expression based on
the u_pgxp_depth cbuffer scalar (added to BatchUBOData in
49c0f82 and already populated by Initialize / Reset /
UpdateHWSettings).

C++ shadergen path only this commit (D3D11 / D3D12 / OpenGL).
Vulkan FS templates still have PGXP_DEPTH as a -D variant axis
and a follow-up commit will collapse it across all five FS
templates (parallel to the UV_LIMITS Nearest-FS-routing
follow-up pattern). m_pgxp_depth_buffer stays in
shader_source_changed for now because Vulkan still needs the
rebuild to switch pre-baked SPIR-V variants and because the
PSO depth comparison func (LESS_EQUAL vs GREATER_EQUAL) still
depends on m_pgxp_depth_buffer at PSO build time across all
backends.

What changes in the shadergen:

  DefineMacro(ss, "PGXP_DEPTH", m_pgxp_depth);
  -> dropped from the FS prelude. m_pgxp_depth at shadergen
  ctor capture is now unused for the batch FS body / signature.
  (The member field stays - the VS still captures it for the
  body comment / documentation, the constructor still accepts
  it because the gpu_hw_*.cpp shadergen instantiations still
  pass it, and the vram_write / vram_copy / vram_fill non-
  batch shaders below in the same file still use PGXP_DEPTH as
  a -D macro on their own variant matrices.)

  DeclareFragmentEntryPoint(ss, ..., !m_pgxp_depth, ...)
  -> DeclareFragmentEntryPoint(ss, ..., true, ...)
  Both call sites (textured and untextured arms of the batch
  FS) unconditionally request SV_Depth declaration.

  FS body o_depth writes: four `#if !PGXP_DEPTH / o_depth =
  oalpha * v_pos.z / #endif` blocks inside the four
  transparency arms (TRANSPARENCY + TEXTURED semitransparent,
  TRANSPARENCY + TEXTURED opaque, TRANSPARENCY-only-untextured,
  non-TRANSPARENCY) collapse to a single unconditional write
  at the end of main():

    o_depth = (u_pgxp_depth != 0u) ? v_pos.z : (oalpha * v_pos.z);

  All four old sites wrote the same expression with the same
  operand values in scope, so hoisting to a single end-of-
  function site is value-equivalent. The discards in the
  TRANSPARENCY_ONLY_OPAQUE / _TRANSPARENT arms above end the
  FS before the o_depth write runs, just as they did
  pre-routing.

Why the ternary is correct:

  u_pgxp_depth != 0 (PGXP-depth mode):
    The VS computes pos_z from a_pos.w (PGXP-replayed depth)
    and writes it through v_pos.z. The rasterizer
    interpolates it. Writing v_pos.z back to o_depth is a
    pass-through that produces the same depth value the
    pre-routing PGXP-on path got from omitting SV_Depth
    entirely (rasterizer-interpolated depth goes straight to
    the depth buffer).

  u_pgxp_depth == 0 (legacy mask-bit mode):
    The VS computes pos_z from a_pos.z (mask-bit Z). oalpha
    is 0 or 1 in this mode (set by the C++ side based on
    whether the pixel should mask). oalpha * v_pos.z is
    either 0 (mask off) or v_pos.z (mask on) - the same
    encoding the pre-routing PGXP-off path wrote.

Performance trade:

  Pre-routing, only the PGXP-on batch FS variants retained
  early-Z TEST. Every PGXP-off variant lost it to SV_Depth
  declaration, and every batch FS variant (both PGXP modes)
  lost early-Z WRITEBACK to the discard keyword's static
  presence (texture-transparent discard plus the
  u_interlacing discard added in eb42ffb). Post-routing, all
  batch FS variants lose early-Z TEST.

  This is the same character of trade Lib accepted for
  earlier cbuffer-routing commits (the discard-driven
  WRITEBACK loss in eb42ffb, called out in the
  GenerateBatchFragmentShader comment around the
  u_interlacing branch). The PGXP-on path was the last
  remaining variant with early-Z TEST; this commit takes it.

  For PSX content on modern GPUs:
    - Native framebuffer is 1024x512 = ~0.5 Mpix. At 16x
      resolution scale that's ~134 Mpix. Even with 5x
      overdraw that's ~670 Mpix of FS work, well within
      sub-millisecond throughput on a 5090.
    - PSX overdraw is low. The original hardware can't
      handle much, so games are designed around 1-3x typical
      / 5-10x in particle-heavy moments, not the 20-50x
      modern engines see.
    - PSX renders are typically painter's-algorithm sorted
      back-to-front (for transparency correctness). Front-
      to-back sort + early-Z is the configuration where
      early-Z TEST pays off - PSX deliberately does the
      opposite to be sort-correct.
    - The depth buffer in legacy mode is mask-bit storage,
      not perspective depth. Hi-Z hierarchical compression
      can't compress essentially-random values, so even when
      early-Z TEST was active, Hi-Z wasn't doing much.

  Net: the early-Z TEST loss is theoretical; on the target
  hardware it is sub-noise. Same character as the prior
  cbuffer-routing trades.

PSO depth-comp dependency is OUTSIDE this commit:

  Per gpu_hw_d3d12.cpp:1317-1319, the PSO depth comparison
  func is set at PSO build time:
    depth_test ? (m_pgxp_depth_buffer ? LESS_EQUAL
                                       : GREATER_EQUAL)
               : ALWAYS;
  m_pgxp_depth_buffer flips therefore still require PSO
  rebuilds to swap the comparison func. The FS bytecode is
  now invariant across the flip (cache-served) but the PSO
  cache key still includes m_pgxp_depth_buffer indirectly
  via the depth-comp field. This commit does not unify the
  depth-comp func across PGXP modes - that would require
  either always-using one comparison func and inverting the
  depth values in the other mode (a meaningful algorithmic
  change) or making depth-comp a matrix dim (which doubles
  m_batch_pipelines count). Neither belongs in a routing
  commit. The eventual decision: most likely keep depth-comp
  as a PSO-state dep on m_pgxp_depth_buffer and accept that
  PGXP toggles re-assemble PSOs from cached FS blobs.

Pre-bake variant count, cumulative arc progress:

  Pre-arc:               75 264
  After DITHERING:       37 632
  After INTERLACING:     18 816
  After PGXP-VS:         no change (FS pre-bake unaffected)
  After UV_LIMITS:        9 408
  After PGXP-FS (this):   4 704

  Remaining axes that would need decision before pre-bake:
    multisamples           x 6  (kept as a variant axis)
    per_sample_shading     x 2  (decision pending)
    disable_color_perspective x 2  (decision pending)

  If both per_sample / dcp collapse via forced-always
  settings: 4704 / 4 = 1176. That is the rough target the
  pre-bake commits will aim at.

Cost matrix delta (D3D12):

  PGXP toggle mid-session (m_pgxp_depth_buffer flips):
    Pre-commit:  FS bytecode varies across the flip, so
                 shader_source_changed fires the full
                 ~252 FS blob + ~2520 PSO rebuild. With
                 disk shader cache populated, the FS
                 compiles are cache hits and the cost is
                 ~2520 PSO assembly calls.
    Post-commit: FS bytecode is invariant. The full
                 rebuild path STILL fires (PSO depth-comp
                 swap), but every FS lookup is now a
                 byte-identical hash hit. PSO assembly
                 is unchanged. Net: 252 fewer D3DCompile
                 calls on a cold cache (was zero on a
                 warm cache, so no change there). Pre-bake
                 variant count halved.

  Steady-state runtime cost:
    + one runtime branch + one MUL in every batch FS
      invocation. The branch is uniform across the warp
      (u_pgxp_depth is a cbuffer scalar) so it costs a
      single divergent-free predicated select on modern
      hardware. Sub-microsecond at the framebuffer sizes
      PSX rendering produces.

Cross-backend impact:

  D3D11 / D3D12 / OpenGL: get the routed FS bytecode
  starting this commit. PSO depth-comp dep on
  m_pgxp_depth_buffer unchanged.

  Vulkan: untouched - its batch FS templates live in
  data/shaders/vulkan/batch_*.frag.glsl and have their own
  PGXP_DEPTH -D variant axis. Routing those is a follow-up
  commit (parallel to the UV_LIMITS Nearest FS routing
  pattern: add u_pgxp_depth to the Vulkan FS UBO, drop the
  #if defined(PGXP_DEPTH) guards, regen 5 FS template
  variant sets).

Test:
  - Build: D3D11 / D3D12 / OpenGL compile. Vulkan
    untouched.
  - Default render (PGXP off): u_pgxp_depth = 0, FS
    ternary picks oalpha * v_pos.z (mask-bit-depth-encode).
    PSO depth comp is GREATER_EQUAL (m_pgxp_depth_buffer
    is false). Output identical to pre-commit.
  - Toggle PGXP-depth-buffer on: m_pgxp_depth_buffer flips,
    shader_source_changed fires, the FS / PSO matrix
    rebuilds. With disk shader cache warmed, the FS blobs
    are cache hits (byte-identical pre vs post commit, AND
    pre vs post PGXP toggle); PSO assembly runs with the
    new LESS_EQUAL depth comp. u_pgxp_depth = 1, FS
    ternary picks v_pos.z (pass-through). Output identical
    to pre-commit PGXP-on state.
  - Specific titles to validate the mask-bit-depth-encode
    path:
      * Crash 1/2/3 - heavy semitransparent particle work
        relies on mask-bit encoding for correct depth
        masking. Any visual difference vs pre-commit
        would show up as wrong-Z particle layering.
      * Final Fantasy VII/VIII/IX combat backgrounds -
        the framebuffer-effects layer uses the mask bit
        for compositing. Visual sanity at 4-16x resolution
        scale.
      * Silent Hill 1 - heavy use of the mask bit for the
        fog plane. PGXP off baseline.
  - Specific titles for the PGXP-on pass-through:
      * Tekken 3 / Soul Blade - PGXP-on textured polygons.
        Any FS depth-writing error would show up as
        z-fighting on co-planar stage geometry.
      * Tomb Raider 1/2 - PGXP-corrected world geometry,
        polygon-level visibility.

Next:
  - Vulkan FS PGXP_DEPTH routing across all 5 templates
    (parallel structure to the UV_LIMITS Nearest FS
    routing).
  - per_sample / dcp collapse decisions.
  - Pre-bake the resulting D3D12 FS matrix.
2026-05-19 03:28:19 +02:00
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
8b435183ea core: route UV_LIMITS through the batch UBO
Commit 6 of the batch matrix variant-shrinking arc. Routes
the UV_LIMITS axis out of the C++ shadergen source via the
u_uv_limits cbuffer slot (the second reserved slot in row 3
of the batch UBO; renamed from u_pad1). The VS / FS source is
now invariant under m_uv_limits for the D3D11 / D3D12 /
OpenGL backends that consume the C++ shadergen.

Wider blast radius than the prior cbuffer-routing commits in
this arc: UV_LIMITS doesn't gate body logic only - it gates
the a_uv_limits VS input attribute, the v_uv_limits varying,
and the FS-side v_uv_limits input. The shader SIGNATURE
differs between the two variants, which means the C++ input
layout has to follow suit. D3D11 and D3D12 both had
conditional ATTR4 bindings keyed on m_using_uv_limits; this
commit makes them unconditional. OpenGL was already
unconditional. Vulkan untouched - its batch shaders live in
data/shaders/vulkan/batch.vert.glsl and are pre-compiled
to SPIR-V via tools/regen_vulkan_spirv.py separately from
the C++ shadergen, with their own UV_LIMITS spec axis;
routing that side requires a regen pass and is left as a
follow-up.

Why the input layout can be unconditionally bound:

  BatchVertex (gpu_hw.h:47) carries the uv_limits field
  unconditionally. Every BatchVertex::Set call site
  populates it - with real values from
  ComputePolygonUVLimits when m_using_uv_limits is true,
  with zero otherwise (the .Set overloads always take a
  uv_limits_ parameter). The vertex buffer always has the
  4-byte slot at offsetof(BatchVertex, uv_limits); binding
  ATTR4 to it always points at valid memory, and reading
  zeros when ComputePolygonUVLimits didn't run is safe
  because the FS short-circuits on u_uv_limits.

Why the FS texture-filtering arm can use v_uv_limits without
a runtime guard:

  ShouldUseUVLimits() (gpu_hw.cpp:23) defines
  m_using_uv_limits as "PGXP on OR texture filter !=
  Nearest". Whenever TEXTURE_FILTERING != Nearest in the
  compiled FS source, m_using_uv_limits is also true at
  runtime, so u_uv_limits is 1 and v_uv_limits is valid.
  The settings-layer coupling guarantees this; no shader-
  side defensive check is needed. The non-filtered FS arm
  (TEXTURE_FILTERING == Nearest in source) DOES gate on
  u_uv_limits at runtime because PGXP can flip
  m_using_uv_limits to true under a Nearest-filter
  session.

Shadergen VS prelude: drops DefineMacro UV_LIMITS, replaces
the two-branch DeclareVertexEntryPoint (with vs. without
a_uv_limits / v_uv_limits) with a single textured branch
that always emits both. VS body: drops the inner #if
UV_LIMITS guard around the v_uv_limits write so the varying
is always populated.

Shadergen FS prelude: drops DefineMacro UV_LIMITS, replaces
the two-branch DeclareFragmentEntryPoint (with vs. without
v_uv_limits input) with a single textured branch that
always declares v_uv_limits. FS body: refactors the
texture-sample block from

    #if UV_LIMITS
      float4 uv_limits = v_uv_limits;
      ...scale fixup...
    #endif

    #if TEXTURE_FILTERING
      FilteredSampleFromVRAM(..., uv_limits, ...);
    #else
      #if UV_LIMITS
        texcol = SampleFromVRAM(..., clamp(coords, uv_limits.xy, uv_limits.zw));
      #else
        texcol = SampleFromVRAM(..., coords);
      #endif
    #endif

  to

    #if TEXTURE_FILTERING
      float4 uv_limits = v_uv_limits;
      ...scale fixup...
      FilteredSampleFromVRAM(..., uv_limits, ...);
    #else
      if (u_uv_limits != 0u)
      {
        float4 uv_limits = v_uv_limits;
        ...scale fixup...
        texcol = SampleFromVRAM(..., clamp(coords, uv_limits.xy, uv_limits.zw));
      }
      else
      {
        texcol = SampleFromVRAM(..., coords);
      }
    #endif

The texture-filtering arm uses v_uv_limits unconditionally
(settings coupling guarantee); the non-filtered arm gates
on the cbuffer scalar.

C++ side:

  BatchUBOData: u_pad1 -> u_uv_limits. Struct size and
  alignment unchanged (still 64 bytes / 4 rows). The last
  remaining reserved slot is u_pad2.

  Initialize / Reset / UpdateHWSettings: seed u_uv_limits
  alongside the other per-session fields. Same shape as
  the prior per-session cbuffer-routing commits (u_true_color,
  u_scaled_dithering, u_pgxp_depth).

  D3D11 CompileShaders: drop the
    num_attributes = attributes.size() - (m_using_uv_limits ? 0 : 1)
  truncation. The input layout always has 5 entries.
  CreateInputLayout's signature-match validation against the
  VS bytecode now matches up because the VS unconditionally
  declares all 5 inputs.

  D3D12 GetBatchPipeline: drop the
    if (m_using_uv_limits) gpbuilder.AddVertexAttribute(...)
  guard around ATTR4. Same reasoning.

  OpenGL: untouched. The VAO setup at
  gpu_hw_opengl.cpp:1099-1107 already bound ATTR4
  unconditionally, predating this commit.

  Vulkan: untouched. gpu_hw_vulkan.cpp:2247 still has the
  if (m_using_uv_limits) input-attribute guard, and the
  pre-baked SPIR-V from data/shaders/vulkan/batch.vert.glsl
  is still UV_LIMITS-conditional. Routing the Vulkan side
  needs three changes that aren't in this commit: edit the
  GLSL source to drop the #if UV_LIMITS guards, run
  tools/regen_vulkan_spirv.py to regenerate the embedded
  SPIR-V blobs, and drop the gpu_hw_vulkan.cpp conditional.
  Until then Vulkan continues working as before - the
  C++ shadergen change doesn't reach Vulkan because its
  batch shaders don't go through the C++ shadergen path.

m_using_uv_limits is still kept as a member field and still
appears in shader_source_changed / non_dim_diff. The
shadergen now ignores it (uses m_uv_limits at ctor capture
time but never reads it back for the batch shaders), but
Vulkan still needs it to pick the right pre-baked SPIR-V
blob at runtime (gpu_hw_vulkan.cpp:1654). Removing the
rebuild trigger would break Vulkan; leaving it in is wasted
work for D3D11 / D3D12 / OpenGL but harmless (the
re-emitted shader source is identical, so the disk shader
cache picks the same hash and serves the cached entry). The
proper cleanup happens when Vulkan is also routed.

m_using_uv_limits also still drives the per-vertex
ComputePolygonUVLimits decision (gpu_hw.cpp:740). That's
a CPU-side optimization (skip a computation when the FS
won't use the result) and is independent of the shader
routing - it stays.

Cost matrix delta (D3D12):

  UV_LIMITS flip (in practice triggered by PGXP toggle or
  by texture filter cross-Nearest transition, both of which
  separately trigger rebuilds):
    Pre-commit: 2 VS compiles + 252 FS compiles + 2520 PSO
                compiles to update for new UV_LIMITS
                variant.
    Post-commit: identical compile count, but every
                 compiled blob is now bit-identical to its
                 pre-flip counterpart so the disk shader
                 cache serves all of them from cache.
                 ~252 cache hits + ~2520 cache hits
                 instead of ~252 D3DCompile calls + ~2520
                 driver PSO assembly calls.

  Pre-bake matrix size delta: UV_LIMITS was a captured
  shadergen-state axis (not a matrix dim), so the matrix
  itself doesn't shrink today. The win is on the pre-bake
  side: a future bake of (filter x render x texture x ms)
  no longer has UV_LIMITS as a multiplier, halving the
  variant count we'd otherwise have to bake.

Pre-bake variant count, cumulative arc progress:

  Pre-arc:          7 x 4 x 7 x 2 x 2 x 2 x 6 x 2 x 2 x 2 = 75 264
                    (filter x render x texture x dither x interlace x
                     pgxp x ms x per_sample x dcp x uv_limits)
  After DITHERING:    7 x 4 x 7 x 2 x 2 x 6 x 2 x 2 x 2  = 37 632
  After INTERLACING:  7 x 4 x 7 x 2 x 6 x 2 x 2 x 2      = 18 816
  After PGXP-VS:      no change to FS pre-bake count
  After UV_LIMITS:    7 x 4 x 7 x 2 x 6 x 2 x 2          =  9 408

  Still too big for a single-commit pre-bake; the remaining
  axes (pgxp, per_sample, dcp) need either routing or a
  pragmatic decision to keep them as variant axes. PGXP-FS
  routing halves to 4704; collapsing per_sample / dcp via
  forced-always settings halves twice more to 1176. That's
  the rough target for the eventual pre-bake.

Test:
  - Build: D3D11 / D3D12 / OpenGL compile. (Vulkan also
    compiles - the C++ shadergen path it doesn't use is
    untouched.)
  - Default render (PGXP off, filter Nearest): the FS
    runtime branch picks the non-clamping path; output is
    visually identical to pre-commit.
  - Toggle texture filter to Bilinear / JINC2 / xBR:
    m_using_uv_limits flips to true on the C++ side, the
    FS rebuild produces identical DXBC, u_uv_limits=1 is
    pushed to cbuffer, the FS texture-filtering arm uses
    v_uv_limits (which is now populated from the also-
    unconditional VS write). Output identical to pre-commit
    filter-on state.
  - Toggle PGXP on with filter Nearest: m_using_uv_limits
    flips to true, FS rebuild produces identical DXBC,
    u_uv_limits=1 is pushed, the FS non-filtered arm takes
    its u_uv_limits != 0 branch and clamps. Output
    identical to pre-commit PGXP-on Nearest state.
  - Toggle PGXP + filter together: behaves as above.
  - Vulkan backend: identical behaviour to pre-commit (its
    code path is untouched).

Next:
  - The Vulkan-side UV_LIMITS routing follow-up (edit
    data/shaders/vulkan/batch.vert.glsl + regen SPIR-V +
    drop the .cpp conditional + drop m_using_uv_limits
    from shader_source_changed across all backends).
  - PGXP_DEPTH for the FS (always-emit SV_Depth perf
    measurement first).
  - Pre-bake commits once the remaining axes are decided.
2026-05-19 02:35:59 +02:00
LibretroAdmin
49c0f829fc core: route PGXP_DEPTH through the batch UBO for the VS
Commit 5 of the batch matrix variant-shrinking arc. Routes
PGXP_DEPTH out of the batch VS source via the u_pgxp_depth
cbuffer slot reserved by eb42ffb's row-3 expansion. The VS
body's pos_z source selection becomes a uniform-control-flow
ternary on the cbuffer scalar; the VS source is now invariant
under the m_pgxp_depth member.

This is a partial routing - only the VS becomes pgxp-
independent. The FS retains m_pgxp_depth as a captured
shadergen-state field because it gates the SV_Depth
declaration in the FS entry-point signature (via
DeclareFragmentEntryPoint's `depth_output` parameter). A
signature change cannot be cbuffer-routed without always-
emitting SV_Depth from every batch FS, which disables early-Z
writeback for both arms of the PGXP toggle (vs the current
state where PGXP-off can early-Z-writeback). Routing the FS
side is a future commit pending perf measurement; the
expected impact is small for PSX content (back-to-front
rendering, minimal early-Z win to begin with) but worth
checking before committing to the trade.

The PSO depth-test state also still depends on m_pgxp_depth:

  GetBatchPipeline at gpbuilder.SetDepthState(...) picks
  D3D12_COMPARISON_FUNC_LESS_EQUAL when m_pgxp_depth_buffer
  is on, D3D12_COMPARISON_FUNC_GREATER_EQUAL when off.

That's a PSO state, not a shader concern. The matrix dim
collapse for PGXP would need a depth_test dim shape change
(currently 2 = {disabled, enabled}; would need 3 =
{disabled, enabled-greater-equal, enabled-less-equal}, or
two separate dims). A future commit can do that; for now
PGXP still triggers a full PSO rebuild because the PSO
state differs.

Net effect of this commit:

  PGXP toggle pre-commit:
    - VS source string changes -> VS matrix invalidated
    - FS source string changes -> FS matrix invalidated
    - PSO depth state changes -> PSO matrix invalidated

  PGXP toggle post-commit:
    - VS source string UNCHANGED -> VS matrix preserved
    - FS source string changes -> FS matrix invalidated (still)
    - PSO depth state changes -> PSO matrix invalidated (still)
    - u_pgxp_depth pushed to cbuffer -> VS reads new value on
      next FlushRender

VS rebuild is small (2 shaders, ~100ms cold). The dominant
cost on a PGXP toggle is still the FS + PSO rebuild. This
commit doesn't eliminate that cost, but it sets up the
cbuffer slot and the routing pattern; future commits that
route the FS side and the PSO depth state will plug into
the same u_pgxp_depth field and the same SetDrawMode
maintenance.

Cbuffer layout: u_pad0 (the first reserved slot in the row-3
area eb42ffb added) is renamed to u_pgxp_depth. No size
change - the cbuffer remains 64 bytes / 4 rows. The two
remaining reserved slots (u_pad1, u_pad2) carry forward for
the UV_LIMITS routing and one more axis.

Shadergen VS body:

  Was:
    #if PGXP_DEPTH
      float pos_z = a_pos.w;
      float pos_w = a_pos.w;
    #else
      float pos_z = a_pos.z;
      float pos_w = a_pos.w;
    #endif

  Now:
    float pos_z = (u_pgxp_depth != 0u) ? a_pos.w : a_pos.z;
    float pos_w = a_pos.w;

HLSL compiles a uniform ternary on a cbuffer scalar to a
single conditional-move instruction; both branches read from
the same input register, so there's no extra memory traffic.

OpenGL post-processing branch (pos_y += POS_EPSILON;
pos_z = (pos_z * 2.0) - 1.0) was previously inside one
#if API_OPENGL || API_OPENGL_ES block straddling the pgxp
gate. Split into two API_OPENGL blocks bracketing the new
pos_z assignment so the y-epsilon fixup runs before pos_z
is assigned and the depth-range fixup runs after. Same
generated code on OpenGL backends, no behavioural diff.

C++ side:

  BatchUBOData: u_pad0 -> u_pgxp_depth. Struct size unchanged
  at 64 bytes. The two remaining reserved slots become u_pad1
  and u_pad2 (numbering preserved at u_pad1/u_pad2 rather
  than renumbered to u_pad0/u_pad1 so the existing reserved
  area is bisect-friendly).

  Initialize / Reset: seed u_pgxp_depth alongside
  u_resolution_scale / u_true_color / u_scaled_dithering.
  All four are per-session fields that need explicit
  re-seeding after Reset's zero-init - same shape as the
  u_resolution_scale invariant that the original cbuffer
  refactor commit established.

  UpdateHWSettings: inside the existing
  m_pgxp_depth_buffer-flip block (line 340-346 pre-commit),
  also push the new value into m_batch_ubo_data.u_pgxp_depth.
  The dirty bit is already set in the bulk per-session push
  upstream at line 336, so the next FlushRender's upload
  picks up the new field. No new dirty tracking required.

Visible behaviour: zero. The VS produces identical pos_z
output (a_pos.w when PGXP is on, a_pos.z when off) just via
a runtime select rather than a compile-time pick. FS
behaviour and PSO depth state are unchanged - those still
follow m_pgxp_depth_buffer through shader_source_changed and
the PSO depth-comparison-func selection.

Cost matrix delta (D3D12):

  PGXP toggle in UpdateHWSettings:
    Pre-commit: 2 VS compiles + 252 FS compiles + 2520 PSO
                compiles -> ~tens of seconds cold.
    Post-commit: 0 VS compiles + 252 FS compiles + 2520
                 PSO compiles -> still tens of seconds.
    Saved: 2 VS compiles (~100ms cold). Symbolic on its
    own, but it unblocks future FS / PSO routing commits
    that consume the same cbuffer slot.

D3D11 / OpenGL / Vulkan: untouched on this commit. Their
GenerateBatchVertexShader call paths see the same source
output - the macro removal happens unconditionally in the
shadergen, so all backends benefit equally (no VS recompile
on PGXP toggle for any of them). Per-backend additional
cleanup (if they had separate PGXP-VS matrix dims) is not
required - the batch VS matrix has only [textured] as its
dim and was never keyed on PGXP at the matrix level.

Test:
  - Build: compile / link D3D12 backend.
  - Boot any PSX title with PGXP off (default): the
    runtime ternary picks a_pos.z, same as the legacy path.
    Geometry renders identically.
  - Toggle PGXP CPU mode on mid-session (Enhancements
    settings menu): the FS / PSO matrix rebuilds (no
    change from current behaviour) but the VS is preserved
    across the flip. The new u_pgxp_depth value rides the
    next FlushRender's UBO upload; geometry now uses
    a_pos.w. Visual output identical to pre-commit PGXP-on
    state.
  - Toggle PGXP off again: same path in reverse.
  - PGXP-heavy title (Crash Bandicoot, MGS, Tekken 3 -
    titles with PGXP depth-buffer mode in production
    use): PGXP-on render is identical to pre-commit.

Next:
  - Route UV_LIMITS through the batch UBO (chunkiest of
    the planned refactors: needs per-vertex buffer layout
    change to always emit a_uv_limits / v_uv_limits with
    junk values when not in use, plus VS / FS body branches
    on u_uv_limits).
  - Or: decide on PGXP_DEPTH for the FS (always-emit
    SV_Depth perf trade). Worth a perf measurement before
    committing.
2026-05-19 02:19:49 +02:00
LibretroAdmin
1686726aeb core/d3d12: drop the [interlacing] dim from the batch matrices
Commit 4 of the batch matrix variant-shrinking arc. Same
shape as f7f9e53 (the [dithering] dim collapse): follow-on
to eb42ffb's INTERLACING-via-cbuffer routing, which made the
FS source invariant under the interlacing bit. The two
[interlacing=0] and [interlacing=1] slots in the PSO and FS
blob matrices have been compiling to identical DXBC and
holding the same PSO pointer since eb42ffb landed; this
commit removes the redundant dim.

Matrices:

  m_batch_pipelines:
    <2, 5, 9, 4, 2, 7> -> <5, 9, 4, 2, 7>
    [filter][depth_test][render_mode][texture_mode][transparency_mode][interlacing]
    -> [filter][depth_test][render_mode][texture_mode][transparency_mode]
    Same for m_batch_pipelines_fastpath.

  m_batch_fragment_shader_blobs:
    <2, 9, 4, 7> -> <9, 4, 7>
    [filter][render][texture][interlace]
    -> [filter][render][texture]
    Same for m_batch_fragment_shader_blobs_fastpath.

Slot-count delta:

  Batch FS blobs:  504 -> 252  (-50%)
  Batch PSO grid: 5040 -> 2520 (-50%)

Cumulative from start-of-arc (commit 1, pre-3af8e02):

  Batch FS blobs: 1008 -> 252  (-75%)
  Batch PSO grid:10080 -> 2520 (-75%)

Helper signatures, the parameter through-line, and the
precompile loops (sync + background worker) follow exactly
f7f9e53's shape:

  GetBatchFragmentShader: drops `bool interlacing`
  GetBatchPipeline:       drops `bool interlacing`
  DrawBatchVertices:      stops passing m_batch.interlacing
                          through to GetBatchPipeline.
                          m_batch.interlacing still lives - it
                          owns the UBO push via
                          m_batch_ubo_data.u_interlacing in
                          SetDrawMode (eb42ffb) - it's just no
                          longer a PSO key.
  Sync precompile loop:   inner `for (interlacing)` removed
                          from both the FS and PSO passes.
  Background worker:      inner `for (interlacing)` removed.

Pass-through to shadergen: GetBatchFragmentShader now passes
literal `false` to BOTH `dithering` and `interlacing` shadergen
parameters. Both are no-ops on the FS source as of 3af8e02 /
eb42ffb respectively; the shadergen function still takes them
for D3D11 / OpenGL / Vulkan compatibility but every D3D12 call
site is now (..., false, false). Same cache-hash-stability
reasoning as f7f9e53 - the source string is invariant under
both parameters, the on-disk shader cache hash is stable, any
existing dithering=0/interlacing=0 cache entries continue to
hit and the other three quadrants become unreachable garbage
that get evicted at next cache GC.

Visible behaviour: zero. PSOs at each (filter, depth_test,
render, texture, transparency) coordinate are bit-identical
to the pre-commit PSOs at (filter, depth_test, render,
texture, transparency, interlacing=anything). m_batch.dithering
still flips per-batch via u_dithering;
m_batch.interlacing still flips per-batch via u_interlacing;
the FS still discards when (y & 1u) matches the displayed
field; output frames pixel-identical.

Cost matrix delta (D3D12):

  Cold sync precompile (Enabled mode):
    504 FS compiles + 5040 PSO compiles
    -> 252 FS compiles + 2520 PSO compiles. Half the work
    to bring the matrix online.

  Background worker walk (Lazy mode):
    5040 cells -> 2520 cells. Worker reaches steady state
    twice as fast.

  Filter switch lazy fault-in:
    56 lazy FS compiles -> 28 lazy FS compiles for the new
    filter's sub-cube. The big "texture filter change takes
    ungodly time" pain - already halved by f7f9e53 from the
    original 144 - is now down to 28 lazy compiles.

  Display-mode interlace flip mid-session: ~4 byte UBO write
    (unchanged from eb42ffb - that commit eliminated the
    recompile path; this commit removes the redundant matrix
    slots).

Cumulative cost-matrix delta from arc start:

  Cold sync precompile:
    1008 FS + 10080 PSO -> 252 FS + 2520 PSO (-75%).
  Background worker walk:
    10080 cells -> 2520 cells (-75%).
  Filter switch lazy fault-in:
    144 -> 28 lazy compiles (-80%).

The 504->252 / 5040->2520 collapse is the last
matrix-shrinking commit driven by the FS-source-invariance
work done in commits 1-3 of this arc. Further FS variant
reduction needs either:

  - Cbuffer-routing more state (the planned next commits:
    PGXP_DEPTH for VS, UV_LIMITS, etc.). Each follow-on
    routing kills 0 dims on its own (those axes aren't in
    the current PSO matrix - they're shadergen-state-
    captured and trigger a full matrix rebuild on flip).
    Their value is on the rebuild-trigger side: routing
    means filter / multisamples / etc. become the only
    remaining session-state rebuild triggers.

  - Cbuffer-routing the remaining matrix axes (filter,
    multisamples) - not possible without losing the
    filter-specific sample paths and the MS-unroll
    optimization respectively.

So after this commit the matrix is at its terminal shape
modulo a final pre-bake pass: <9, 4, 7> FS blobs and
<5, 9, 4, 2, 7> PSOs are what the rest of the arc will
operate over. The next refactors (PGXP_DEPTH for VS,
UV_LIMITS) shrink rebuild triggers, not matrix size.

D3D11 / OpenGL / Vulkan untouched. They still pass
`interlacing` to GenerateBatchFragmentShader and key their
batch matrices on it. Same dim-redundancy as D3D12 had
pre-commit. Per-backend cleanup commits can follow on the
f7f9e53 / this-commit shape.

Test:
  - Build: D3D12 backend compiles, links.
  - Boot any progressive-mode title: PSO content at each
    surviving slot is identical to what would have been
    fetched at interlacing=anything before; visual output
    unchanged.
  - Boot any interlaced-mode title (480i - FFVII battle
    screens, MGS cutscenes, Tekken intros): the FS's
    runtime discard branch handles the field alternation
    via u_interlacing / u_interlaced_displayed_field. Output
    frames pixel-identical to pre-commit.
  - Display-mode flip mid-session: no shader recompile, no
    PSO swap, just the cbuffer write that eb42ffb made
    sufficient.
  - Shader cache: existing on-disk cache continues to serve
    hits at the (dithering=0, interlacing=0) hash; new
    misses populate the further-shrunk matrix.

Next: route PGXP_DEPTH through the batch UBO. The slot
already exists (u_pad0 in the row-3 reserved area added by
eb42ffb), and the VS body is a single `#if PGXP_DEPTH`
ternary on pos_z source. Same shape as DITHERING /
INTERLACING routing - smallest of the remaining axes to
route, validates the row-3 reserved-slot allocation pattern.
2026-05-19 01:44:54 +02:00
LibretroAdmin
eb42ffb055 core: route INTERLACING through the batch UBO
Commit 3 of the batch matrix variant-shrinking arc. Same shape
as the DITHERING routing in 3af8e02: lift the INTERLACING
compile-time #define into a runtime cbuffer scalar so the FS
source is invariant under the display-mode interlace state,
collapsing what was a 2x matrix axis into a single shader.

Smaller and tighter than DITHERING - INTERLACING has only one
#if site in the batch FS body (the y-LSB / displayed-field
discard guard), no register-bit-mid-frame considerations (the
display-mode interlace state only flips on display-mode changes,
which are rare), and the in-batch displayed-field LSB
(u_interlaced_displayed_field) was already cbuffer-routed for
the same shader site - the on/off bit is the only piece that
was still compile-time.

Cbuffer layout, before:

  Row 0: u_texture_window_and (8) + u_texture_window_or (8)
  Row 1: u_src_alpha_factor (4) + u_dst_alpha_factor (4) +
         u_interlaced_displayed_field (4) +
         u_set_mask_while_drawing (4)
  Row 2: u_resolution_scale (4) + u_true_color (4) +
         u_scaled_dithering (4) + u_dithering (4)
  Total: 48 bytes / 3 rows.

After:

  Row 0-2: unchanged.
  Row 3: u_interlacing (4) + u_pad0 (4) + u_pad1 (4) + u_pad2 (4)
  Total: 64 bytes / 4 rows.

The three u_padN slots are pre-allocated reserved cbuffer slots
for the next refactors in this arc: PGXP_DEPTH and UV_LIMITS
each need one slot, leaving one for whatever follows. HLSL
cbuffer rows are 16-byte aligned anyway, so growing the cbuffer
by these three uints is free in layout terms. Naming them
explicitly (rather than implicit-padding) matches the pre-3af8e02
"u_pad0 brings total to 16-byte cbuffer row" pattern and lets
the next commits do a 1-line rename (u_pad0 -> u_pgxp_depth, etc.)
the same way 3af8e02 did u_pad0 -> u_dithering.

Shadergen body:

  Was:
    #if INTERLACING
      if ((fixYCoord(uint(v_pos.y)) & 1u) == u_interlaced_displayed_field)
        discard;
    #endif

  Now:
    if (u_interlacing != 0u &&
        (fixYCoord(uint(v_pos.y)) & 1u) == u_interlaced_displayed_field)
      discard;

HLSL guarantees short-circuit evaluation of &&, so when
u_interlacing is 0 the fixYCoord call and the LSB compare don't
run - the runtime branch on u_interlacing collapses to a single
cbuffer-scalar test plus a no-op when off. u_interlacing is a
cbuffer field, every SIMD lane reads the same value, no
divergence cost on either arm.

Known trade: the discard keyword's presence in the FS source
disables early-Z writeback regardless of whether the discard
ever fires at runtime - the previous code had two FS variants
where the interlacing=0 variant was discard-free and could
participate in early-Z writeback. After this commit both arms
of the runtime branch live in the same DXBC, so the
interlacing=0 case loses that optimization. The cost is one
extra depth-write per fragment in the post-FS phase rather
than the pre-FS phase; depth TEST still runs early because
v_pos.z is interpolated by the rasterizer before the FS is
invoked, so depth REJECTION still works. For PSX content -
mostly back-to-front rendering with minimal depth-test win
to begin with - this is invisible at the FPS counter. If
some PGXP-heavy title shows a measurable regression we can
revisit by using [earlydepthstencil] on the FS (forces
early-Z at the cost of correctness if a draw both writes
depth and discards, which the batch FS doesn't), but the
default expectation is "no measurable change".

C++ side:

  BatchUBOData gains u_interlacing + u_pad0..2. Total struct
  size 48 -> 64 bytes. The struct's existing layout up to the
  u_dithering field is unchanged, so all existing field
  offsets stay put - the FlushRender upload picks up the new
  4-row layout via the existing sizeof-based push.

  SetDrawMode (where m_batch.interlacing is set per-batch):
  also push m_batch_ubo_data.u_interlacing with a dirty-bit
  guard, so the next FlushRender's upload sees the new
  value. u_interlaced_displayed_field stays in its existing
  conditional update block (only refreshed when interlacing
  is on, since its value is meaningless otherwise).

  The pattern matches u_dithering's: every per-batch state
  change site that affects the FS now does a cbuffer write +
  dirty mark in the same block. Same dirty-bit shape, same
  cost (one branch + one memcmp-equivalent on flip), same
  upload coalescing.

Visible behaviour: zero. The DXBC produced for the
(interlacing=on, displayed_field=X, *) configuration is now
identical to the previous (interlacing=on, *) variant's DXBC
post-conditional-evaluation; the (interlacing=off, *) DXBC
arms now share that same source with a runtime-skipped
discard. m_batch.interlacing still flips per-batch, the
FlushRender upload still publishes the new value, the
discard still fires when (y & 1u) matches the displayed
field. Output frames are pixel-identical to pre-commit.

Cost matrix delta (D3D12):

  Interlaced-vs-progressive display-mode flip mid-session:
    ~50 ms D3DCompile + ~10 ms PSO (or 1 ms disk cache hit
    if warm) -> ~4 byte UBO write.

  Field-LSB alternation per frame (already cbuffer-routed
  pre-3af8e02): unchanged, ~4 byte UBO write per frame.

The [interlacing] matrix dim still lives in
m_batch_pipelines<2, 5, 9, 4, 2, 7> and
m_batch_fragment_shader_blobs<2, 9, 4, 7>. Both slots compile
to identical DXBC after this commit. Follow-up D3D12 commit
will drop the dim:

  m_batch_pipelines:           5 040 -> 2 520 slots
  m_batch_fragment_shader_blobs: 504 ->   252 slots

Same pattern, same blast radius as the [dithering] dim drop
in commit f7f9e53 - the change is one fewer for loop in
precompile_sync and the background worker, one fewer
parameter on GetBatchFragmentShader / GetBatchPipeline /
DrawBatchVertices, one fewer dim on each of the four matrix
declarations.

D3D11 / OpenGL / Vulkan untouched. They still pass
`interlacing` to GenerateBatchFragmentShader and key their
batch matrices on it. Like with DITHERING, they produce
identical DXBC for the two values and waste matrix slots in
the same way. Per-backend cleanup commits can follow.

Test:
  - Build: D3D12 backend compiles, links, runs.
  - Boot any progressive-mode title (default render): the
    discard branch is skipped at runtime; output is
    indistinguishable from pre-commit. Exercises
    u_interlacing = 0 arm.
  - Boot an interlaced-mode title (any 480i game - FFVII
    battle screens, MGS cutscenes, Tekken intros, Resident
    Evil ending sequences): the discard alternates per
    field. Should look identical to pre-commit.
  - Toggle a display setting that flips IsInterlacedRenderingEnabled
    mid-frame (unusual but the FS path supports it): no
    shader recompile; the next FlushRender picks up the new
    u_interlacing.

Next in arc:
  - Drop the [interlacing] dim from D3D12 batch matrices
    (f7f9e53-shape cleanup commit).
  - Route PGXP_DEPTH for the VS (small body branch; uses
    u_pad0 slot).
  - Decide on PGXP_DEPTH for the FS (always-emit SV_Depth
    tradeoff to measure).
  - Route UV_LIMITS (chunkiest - per-vertex buffer layout
    change).
  - Pre-bake the resulting reduced FS matrix.
2026-05-19 01:21:46 +02:00
LibretroAdmin
f7f9e5352b core/d3d12: drop the [dithering] dim from the batch matrices
Commit 2 of the batch matrix variant-shrinking arc. Follow-on
to 3af8e02 (DITHERING-via-cbuffer routing): the FS source is
now invariant under the dithering bit, so the two slots at
[..., dithering=0, ...] and [..., dithering=1, ...] in the
batch PSO and batch FS blob matrices held identical ComPtr
contents - a redundant dim that compiled the same DXBC twice
and held the same PSO pointer twice. Cleaning up the dim
removes 2x of wasted slots, 2x of useless precompile-worker
iterations, and one parameter from the three hot-path
helpers.

Matrices (DimensionalArray template-param order is
inner-to-outer in indexing space; the rightmost template arg
is the outermost array index):

  m_batch_pipelines:
    <2, 2, 5, 9, 4, 2, 7> -> <2, 5, 9, 4, 2, 7>
    [filter][depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
    -> [filter][depth_test][render_mode][texture_mode][transparency_mode][interlacing]
    Same for m_batch_pipelines_fastpath.

  m_batch_fragment_shader_blobs:
    <2, 2, 9, 4, 7> -> <2, 9, 4, 7>
    [filter][render][texture][dither][interlace]
    -> [filter][render][texture][interlace]
    Same for m_batch_fragment_shader_blobs_fastpath.

Slot-count delta:

  Batch FS blobs:  1008 -> 504  (-50%)
  Batch PSO grid: 10080 -> 5040 (-50%)

Helper signatures and the parameter through-line:

  GetBatchFragmentShader: drops `bool dithering`
  GetBatchPipeline:       drops `bool dithering`
  DrawBatchVertices:      stops passing m_batch.dithering through
                          to GetBatchPipeline. m_batch.dithering
                          still lives - it owns the UBO push via
                          m_batch_ubo_data.u_dithering in SetDrawMode
                          (3af8e02) - it's just no longer a PSO key.

Precompile loops (both the synchronous CompilePipelines path
under shader_precompile_mode = Enabled and the background
worker for shader_precompile_mode = Lazy) lose their inner
`for (dithering)` loop. The progress counter is recomputed
implicitly - each call site walks one fewer dim, so the cell
count drops in lockstep with the matrix collapse.

Pass-through to shadergen: GetBatchFragmentShader passes a
literal `false` to tmp_shadergen.GenerateBatchFragmentShader's
`dithering` parameter. The shadergen function still takes the
parameter (the four backends share that signature and D3D11 /
OpenGL / Vulkan still pass their own dithering values from
their own matrix iterations), but as of 3af8e02 the parameter
is a no-op - the FS source is identical regardless of what's
passed. Picking `false` consistently keeps the disk shader
cache hash stable across the matrix collapse: the cache key
is the source string, the source string is invariant under
the parameter, every cell hashes to the same key it would
have hashed to at dithering=0 before the collapse. Any
pre-existing on-disk cache from 3af8e02 (which produced
identical DXBC for dithering=0 / dithering=1 anyway) remains
valid; the dithering=1 entries become unreachable garbage
that get evicted on the next cache GC pass but are otherwise
harmless.

D3D11 / OpenGL / Vulkan untouched. They still pass
`dithering` to GenerateBatchFragmentShader and key their batch
matrices on it. They produce identical DXBC for the two
dithering values, so each is wasting matrix slots in the
same way D3D12 was before this commit. Equivalent per-backend
cleanup commits can follow: same shape, just different
matrix template params and helper signatures.

Visible behaviour: zero. Every (filter, depth_test,
render_mode, texture_mode, transparency_mode, dithering=X,
interlacing) call site that previously routed to slot
[..., dithering=X, ...] now routes to the single [...]
slot. The PSO at that slot was already bit-identical to the
removed slot, the FS blob at that slot was already
bit-identical to the removed slot, and the runtime PSO
binding it produces is unchanged. m_batch.dithering still
flips per-batch (PSX GP0(E1).dither_enable register write),
SetDrawMode still pushes the new value into the batch UBO,
the FS still reads it via u_dithering and branches at the
ApplyDithering call sites - just no PSO swap is needed for
the flip.

Cost matrix delta (D3D12):

  Cold sync precompile (shader_precompile_mode = Enabled):
    1008 FS compiles + 10080 PSO compiles
    -> 504 FS compiles + 5040 PSO compiles. ~50% faster.

  Background worker walk (shader_precompile_mode = Lazy):
    10080 cells -> 5040 cells. ~50% faster to warm the matrix
    on second launch.

  Filter switch mid-session:
    144 lazy FS compiles -> 56 lazy FS compiles for the new
    filter's sub-cube. (Filter is still a matrix dim and
    flipping it still trips lazy fault-ins for the new
    sub-cube; that's the next axis to route via cbuffer if
    we want filter changes to be free as well.)

  PSX GP0(E1).dither_enable mid-frame flip: ~4 byte UBO write
    (unchanged from 3af8e02 - that commit already eliminated
    the recompile path; this commit just removes the
    redundant matrix slots).

Test:
  - Build: D3D12 backend compiles, links.
  - Boot any PSX title: matrix-resize is invisible to the
    user; the runtime fetches the same PSO content it would
    have fetched before, just from a smaller indexed slot.
  - Dither toggle mid-game: visual output unchanged.
  - Filter switch mid-game: same lazy-warmup behaviour as
    before for the new filter's sub-cube (the sub-cube is
    just half the size now).
  - Shader cache: existing on-disk cache continues to serve
    hits; new misses populate the smaller matrix.

Follow-up arc (next commits):
  - Route INTERLACING through u_interlacing cbuffer slot
    (same shape as DITHERING - one #if in the FS body, no
    register-bit-mid-frame considerations). After that the
    [interlacing] dim is the next collapse candidate, taking
    the FS matrix to <2, 9, 4>:252 slots and the PSO matrix
    to <2, 5, 9, 4, 2>:2520 slots.
  - Route PGXP_DEPTH for the VS through cbuffer.
  - Decide on PGXP_DEPTH for the FS (requires always-emit
    SV_Depth).
  - Route UV_LIMITS (requires per-vertex buffer layout
    change).
  - Pre-bake the resulting reduced FS matrix.
2026-05-19 01:05:59 +02:00
LibretroAdmin
3af8e0247a core: route DITHERING through the batch UBO
First refactor of the batch-matrix variant-shrinking arc. Lifts
the dithering bit out of compile-time #define territory and into
the per-batch cbuffer slot that already carries u_resolution_scale
/ u_true_color / u_scaled_dithering, on the same pattern as the
prior 7b575a3-line work. The PSX GP0(E1).dither_enable bit flips
constantly mid-frame in real games (interior vs exterior in MGS,
overhead vs gameplay in Crash, mode transitions everywhere) - the
former #if DITHERING / #else / #endif in the FS body would have
required a different DXBC per dithering value, which is exactly
the kind of per-frame churn pre-bake is supposed to make
disappear.

Shape:

* WriteBatchUniformBuffer: replace the u_pad0 trailing slot with
  uint u_dithering. Same cbuffer footprint (48 bytes / three
  16-byte rows); the trailing row is now
  (u_resolution_scale, u_true_color, u_scaled_dithering,
  u_dithering) with no padding.

* GenerateBatchFragmentShader: drop
  DefineMacro(ss, "DITHERING", dithering). The `dithering`
  function parameter stays in the signature for now so the four
  backends (D3D11, D3D12, Vulkan, OpenGL) and their matrix
  dimensions don't need touching in this commit - the value is
  simply ignored when generating source. Follow-up commits per
  backend can drop the dim from m_batch_pipelines /
  m_batch_fragment_shader_blobs once the runtime callers stop
  caring.

* Three #if DITHERING / #else / #endif body blocks become
  if (u_dithering != 0u) / else runtime branches:
    - The textured /  u_true_color = 1 / !RAW_TEXTURE branch
      (ApplyDithering vs min-clamp to 255)
    - The textured /  u_true_color = 0 / !RAW_TEXTURE branch
      (ApplyDithering vs >>3 clamp to 31)
    - The untextured branch (ApplyDithering vs the u_true_color
      shift)
  All three are uniform-control-flow branches - u_dithering is
  a cbuffer scalar so every SIMD lane reads the same value and
  no divergence penalty applies. ApplyDithering's body is
  ~10 instructions of integer math; the branch costs are
  negligible compared to the texture sample + palette lookup
  that dominates the textured branch anyway.

* BatchUBOData: u_pad0 renamed to u_dithering. The struct's
  layout is byte-for-byte unchanged (uint32_t at offset 44 in
  the 48-byte struct), so the existing FlushRender upload path
  picks up the new value without any size or alignment work.

* SetDrawMode (where m_batch.dithering is set per-batch): also
  set m_batch_ubo_data.u_dithering, conditional dirty-bit
  toggle so the UBO upload only runs when the value actually
  changes. The flush condition at line 1369 that triggers a
  FlushRender on dithering_enable != m_batch.dithering stays;
  it's no longer required for shader correctness (same DXBC
  serves both values), but the existing batch-state-change
  flush model is preserved for this commit to avoid changing
  two things at once. Follow-up could drop it for a small
  per-frame save.

Visible behaviour: zero. The DXBC produced for any single
(dithering_enable, *) configuration is now identical to what
the previous DXBC for (dithering_enable=N, *) would have been
for the matching N - the if/else picks the same code path the
#if/#else would have picked, with the same operands and the
same outputs. The only observable difference is the timing of
when dithering settings flip - previously a compile, now a
cbuffer write.

Cost matrix delta (D3D12):
  PSX GP0(E1).dither_enable flip (mid-frame, very common):
    ~50 ms D3DCompile + ~10 ms PSO -> ~4 byte UBO write.
  Disk-cache-warm version of the same:
    ~1 ms shader-cache hit -> ~4 byte UBO write.

The matrix-dim cleanup for D3D12 (drop the [dithering] axis
from m_batch_pipelines<2, 2, 5, 9, 4, 2, 7> and
m_batch_fragment_shader_blobs<2, 2, 9, 4, 7>, and the dithering
parameter from GetBatchFragmentShader / GetBatchPipeline /
DrawBatchVertices, and the precompile worker's outer loop) is
a separate commit because it touches the .h's DimensionalArray
templates, all the call sites, and the bg-thread enumeration
loop - none of that is required for the source-side correctness
of the cbuffer routing. Doing the dim collapse and the source
refactor together in one commit would conflate two unrelated
concerns and make the bisect window bigger if a regression shows
up.

The other three backends (D3D11, OpenGL, Vulkan) still pass
`dithering` to GenerateBatchFragmentShader and key their batch
matrices by it. They benefit from the cbuffer routing too -
the dithering toggle no longer triggers a shader recompile in
any of them - they just hold redundant dim-0 / dim-1 slots in
their matrices that compile to identical DXBC. Same follow-up
shape per backend.

Test:
  - Build: D3D12 backend compiles, links, runs.
  - Default rendering: any PSX title boots and renders correctly.
    Dithering is on by default in most games' default render
    state so this exercises the u_dithering = 1 branch
    immediately.
  - Toggle "True Color" off (which is what makes dithering
    visible): the dither pattern must appear in the output for
    any non-RAW_TEXTURE polygon. FF7 backgrounds, MGS interiors,
    Crash Bandicoot overhead views are good test signals - all
    have visible smooth-gradient surfaces that show dither
    pattern in lo-bit mode.
  - Toggle "True Color" on: the dithering branch still runs in
    the shader (u_dithering may still be 1 from the game's
    register state) but ApplyDithering produces visually
    indistinguishable output at high bit-depth.
  - Toggle "True Color" mid-game: no shader recompile should
    happen (was already true post-7b575a3); same expectation
    holds post-this-commit.
  - Mid-frame dither flip from a game that toggles register
    state: any game that does this (most do) should continue
    to render correctly. Previously: a flush + PSO swap; now:
    a flush + cbuffer write. Same visual output, less work.

Follow-ups in this arc (in rough order of effort):
  - Drop the [dithering] dim from D3D12 batch matrices.
  - Route INTERLACING macro through u_interlacing cbuffer slot
    (same shape, smaller blast radius - one #if in the FS, no
    register-bit-bit-flip-mid-frame considerations).
  - Route PGXP_DEPTH_VS through cbuffer (VS body branch only).
  - Decide whether to route PGXP_DEPTH_FS (requires always-emit
    SV_Depth - perf-tradeoff worth measuring).
  - Route UV_LIMITS through cbuffer (largest of the four;
    requires per-vertex buffer layout change).
  - Pre-bake the resulting reduced FS matrix.
2026-05-19 00:52:18 +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
3df285a10e core/d3d12: regenerate embedded DXBC with Microsoft fxc
Regenerates fullscreen_quad_vs.inc and copy_ps.inc from their
existing .hlsl sources using Microsoft's actual D3DCompile API
(via Wine + Microsoft's d3dcompiler_47.dll), not Wine's bundled
vkd3d-shader reimplementation. The .hlsl sources are unchanged
in this commit; only the compiled bytecode rotates.

Toolchain setup (contributor side, Linux):

* Microsoft's d3dcompiler_47.dll is dropped next to the
  fxc.exe-compatible front-end so Wine's loader (which always
  searches the application directory first) picks up Microsoft's
  compiler before falling back to its bundled vkd3d-shader
  implementation. The d3dcompiler_47.dll used here is a stock
  Microsoft binary (verified via the embedded "Microsoft (R)
  HLSL Shader Compiler 10.1" version string in the file's
  resource section); redistribution is covered by Microsoft's
  Windows SDK license. mozilla/fxc2 ships the same DLL on
  GitHub for the same reason.
* Wine remains 9.0; no other Wine config changes.

Output deltas vs the prior vkd3d-shader-compiled .inc files:

  fullscreen_quad_vs.inc  828B (vkd3d) -> 736B (Microsoft)
                          chunks ISGN/OSGN/RDEF/SHDR
                            ->   RDEF/ISGN/OSGN/SHEX/STAT

  copy_ps.inc             808B (vkd3d) -> 848B (Microsoft)
                          chunks ISGN/OSGN/RDEF/SHDR
                            ->   RDEF/ISGN/OSGN/SHEX/STAT

The chunk-set delta is the load-bearing piece. Microsoft's fxc
emits SHEX (the Shader Model 5.0+ extended bytecode chunk) and
STAT (compiler-stats metadata) in addition to RDEF / ISGN /
OSGN. Wine's vkd3d-shader 1.10 emits SHDR (the older SM 4.0
bytecode chunk) and omits STAT entirely. The Microsoft D3D12
runtime is happy with either for simple shaders - that's why
the original ca8c9b0 / dd54a73 / 443a2a1 pre-bakes with
vkd3d-shader-compiled blobs worked - but as commit cd971cd
demonstrated, the moment the shader uses features whose
validation paths in D3D12 require SHEX-format metadata
(SV_Depth output combined with conditional discard and a
non-trivial cbuffer, in that specific case), the vkd3d-shader
output is rejected by CreateGraphicsPipelineState with E_FAIL.
Regenerating with Microsoft's compiler removes the entire
class of failures.

Creator-string change visible in the .inc top-of-file comment:

  "vkd3d-shader 1.10 (Wine bundled)"
    -> "Microsoft (R) HLSL Shader Compiler 10.1"

The validator added in ebd2300 (check_microsoft_creator in
tools/regen_d3d12_dxbc.py) refused vkd3d-shader-stamped output
post-hoc; this commit produces the Microsoft-stamped equivalent
that passes that check. Future regens going forward will continue
to use Microsoft's compiler.

This commit deliberately changes only the .inc bytes. Validator
runs cleanly on both regenerated blobs; the runtime path is
unchanged (.inc files compile as plain C++ byte arrays, no
build-side fxc.exe invocation). The two pre-baked shaders that
already ship - fullscreen-quad VS (every non-batch pipeline)
and copy/blit PS (presentation copies, downscale) - are the
load-bearing test cases. If the regenerated blobs don't pass
PSO validation, D3D12 backend init fails the same way cd971cd
did, and the diff is small enough to bisect cleanly.

The vram_copy pre-bake that cd971cd / da20f5b chronicles can
now be re-attempted on this toolchain - that's a follow-up
commit. Same .hlsl source as before, but compiled with the
Microsoft path here; the resulting blob will have the SHEX/STAT
chunks that the cd971cd attempt was missing, and the PSO
validation that failed before should now pass.

Test:
  - Run the regen script under wine + Microsoft d3dcompiler_47.dll:
    python3 tools/regen_d3d12_dxbc.py --fxc fxc_lite.exe --wine wine
    Output: "regenerated 2 .inc file(s)" with no validator errors.
  - Parse the regenerated .inc files post-hoc to confirm the
    chunk set (RDEF, ISGN, OSGN, SHEX, STAT) and creator
    ("Microsoft (R) HLSL Shader Compiler 10.1").
  - Lib-side: rebuild swanstation, launch with D3D12, confirm
    the backend initializes without the cd971cd-style
    "CreateGraphicsPipelineState failed: 80004005" /
    "Failed to compile pipelines" error. The two pre-baked
    shaders are hit during precompile, so the failure mode
    would be obvious at launch.
2026-05-18 22:39:10 +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
e64fc28a7f core: route RESOLUTION_SCALE through display_ps cbuffer
Last shader in the non-batch RESOLUTION_SCALE arc. Same refactor
as e56d4d4 / 9d2b49d / 2980961, applied to display_ps - the
fragment shader that composites the upscaled VRAM texture into
the host display framebuffer every frame.

display_ps body uses RESOLUTION_SCALE / VRAM_SIZE in two distinct
spots driven by the DEPTH_24BIT preprocessor variant:

  * 24-bit colour path (FMV playback, true-colour framebuffer
    games): SampleVRAM24 helper multiplies native VRAM coords by
    RESOLUTION_SCALE to map into the upscaled VRAM texture:
        LoadVRAM(int2((vram_coords % clamp_size) * RESOLUTION_SCALE))
    Hit twice per output pixel (left + right 16-bit packed
    texel pair).
  * 16-bit colour path (common case, every game's standard
    framebuffer present): VRAM_SIZE used for upscaled-coord
    modulo wrap:
        LoadVRAM(int2((icoords + u_vram_offset) % VRAM_SIZE))

Both expand to the same #define aliases as u_resolution_scale-
derived arithmetic after this commit. Per-pixel cost difference
vs the literal-baked version is negligible - the multiplication
with a cbuffer-loaded uint operates at the same throughput as
with a constant, and the modulo wrap was always a real ALU op
not a folded constant.

Subtle but important: this commit pushes m_resolution_scale to
u_resolution_scale, NOT the local `resolution_scale` variable
in each backend's UpdateDisplay scope. The local is
   const uint32_t resolution_scale =
     m_GPUSTAT.display_area_color_depth_24 ? 1 : m_resolution_scale;
which forces 1 in 24-bit mode to keep u_vram_offset / u_crop_left
in NATIVE coords (the 24-bit display path runs the viewport at
native size, not upscaled - see scaled_display_width =
display_width * resolution_scale at the call site). The shader's
RESOLUTION_SCALE macro has always been the session m_resolution_scale
because SampleVRAM24's `vram_coords * RESOLUTION_SCALE` needs to
map native VRAM coords into the upscaled VRAM texture regardless
of how the display target itself is sized. Pushing the local
resolution_scale (=1 in 24-bit) would break SampleVRAM24's
upscale mapping and produce wrong 24-bit colour output (would
sample VRAM at native coords instead of upscaled ones).

Files:

* gpu_hw_shadergen.cpp: GenerateDisplayFragmentShader reordered
  so DeclareUniformBuffer runs before WriteCommonFunctions(ss,
  true). u_resolution_scale + u_pad0 appended to the cbuffer
  member list (after u_field_offset). WriteCBufferResolutionScaleAliases
  call inserted between cbuffer and WriteCommonFunctions.
* gpu_hw_d3d11.cpp / gpu_hw_d3d12.cpp / gpu_hw_vulkan.cpp /
  gpu_hw_opengl.cpp: each backend's UpdateDisplay constructs an
  inline uniforms[] array at its present site. All four grow
  from uniforms[4] (u_vram_offset.xy + u_crop_left + u_field_offset)
  to uniforms[6] (adding u_resolution_scale + u_pad0).
  D3D11 / D3D12 / Vulkan share the same form; OpenGL keeps its
  scaled_flipped_vram_offset_y for lower-left origin handling
  and gets m_resolution_scale + u_pad0 appended in parallel.
  Push-constants ceiling check: 6 DWORDs = 24 bytes, well under
  MAX_PUSH_CONSTANTS_SIZE = 64 on both D3D12 and Vulkan.

Variant axis dropped: display_ps had depth_24bit (2) x
interlace_mode (3) x smooth_chroma (2) x MSAA (M) x
m_resolution_scale (N) variants. After this commit it's 2 x 3 x
2 x M = 12 x M variants, with the scale dimension fully gone.
Pre-bake for display_ps becomes viable in a follow-up - 12
blobs (or 12 x M with MSAA still as a variant axis), which is
manageable. Multi-variant pre-bake exercises TEMPLATE_VARIANTS
in regen_d3d12_dxbc.py for the first time.

Shader-cache invalidation: display_ps's source string changes
on every backend. First boot after this lands re-compiles all
12 (depth_24 x interlace x smooth_chroma) variants once for
each MSAA count the user has enabled; subsequent boots hit the
regenerated cache entries.

Test:
  - Build under each backend succeeds.
  - Render a normal 16-bit framebuffer game (any normal title)
    at every resolution scale (1x, 2x, 4x, 8x). The frame
    composite uses the 16-bit path's `% VRAM_SIZE` for wrap-
    around handling - if u_resolution_scale weren't populated
    correctly, the modulo would be off by a factor and the
    presented image would either be the wrong region of VRAM
    or wrap onto itself with visible tearing. Visible immediately.
  - Render a 24-bit framebuffer source (FMV playback in any
    game with pre-rendered videos - the bookend Sony intro, FF7
    cutscenes, MGS pre-render scenes, etc.) at every resolution
    scale. The SampleVRAM24 path's `vram_coords * RESOLUTION_SCALE`
    is the heaviest user; if it weren't populated, FMVs would
    show garbled pixels sourced from the wrong upscaled VRAM
    location.
  - Toggle resolution scale at runtime (1x -> 2x -> 4x -> 1x).
    Each switch triggers an UpdateDisplay; if the cbuffer field
    didn't track the change, the display would freeze on the
    previous scale's coords or render at wrong size.

This concludes the non-batch RESOLUTION_SCALE refactor arc. After
this lands, every non-batch shader that referenced RESOLUTION_SCALE
or VRAM_SIZE in its body now reads u_resolution_scale from its
own cbuffer, identical pattern to what 7b575a3 did for batch
shaders. The only remaining literal-RESOLUTION_SCALE consumers
are the adaptive_downsample_* shader family, which is a separate
concern - those shaders take a u_rcp_resolution cbuffer field
already, and they use RCP_VRAM_SIZE differently (for mip-level
sampling derivatives, not just coord scaling), so they'd need
a slightly different refactor pattern.

Next on the pre-bake side: with the scale axis removed from
vram_copy / vram_write / vram_read / display, each one becomes
a candidate for offline DXBC pre-bake. Smallest first:
vram_update_depth (2 MSAA variants, body has no RESOLUTION_SCALE
reference so doesn't need this refactor at all) is the natural
TEMPLATE_VARIANTS exercise.
2026-05-18 21:36:00 +02:00
LibretroAdmin
2980961655 core: route RESOLUTION_SCALE through vram_read_ps cbuffer
Same refactor as e56d4d4 / 9d2b49d applied to vram_read_ps. Adds
u_resolution_scale + u_pad0 to its cbuffer, routes
RESOLUTION_SCALE / VRAM_SIZE / RCP_VRAM_SIZE through the cbuffer,
switches WriteCommonFunctions to the (ss, true) path.

vram_read_ps is the heaviest user of RESOLUTION_SCALE in the
non-batch shader family - its body has three direct references:

  * `if (RESOLUTION_SCALE == 1u)` early-out for the 1x fast path
    (no box-filter downsampling needed)
  * Inner loop bounds for the box filter:
        for (offset_x = 0u; offset_x < RESOLUTION_SCALE; offset_x++)
          for (offset_y = 0u; offset_y < RESOLUTION_SCALE; offset_y++)
            value += LoadVRAM(...);
  * Final divisor:
        value /= float(RESOLUTION_SCALE * RESOLUTION_SCALE);

When RESOLUTION_SCALE was a compile-time constant, fxc / glslang
fully unrolled the inner loops (at scale=4, 16 iterations inlined
into 16 sequential LoadVRAM calls) and constant-folded the
divisor. After this refactor, RESOLUTION_SCALE is a runtime
uniform value, so:

  * The early-out branch becomes a runtime branch. Uniform across
    the SIMD wave (every lane sees the same cbuffer value) so
    GPU divergence cost is zero - just one predicate test per
    wave.
  * The loops can no longer fully unroll at compile time.
    HLSL's [unroll] hint can still partially unroll with a known
    upper bound, and the runtime loop overhead is tiny next to
    the texture-load cost.
  * The divisor uses cbuffer-loaded values - one extra imul + i2f
    on the unscaled path.

Net per-pixel cost: a handful of extra cycles, dominated by the
texture loads either way. And vram_read runs on-demand only -
screenshot capture, libretro frontend's video format conversion
at scanout, etc. - not every frame. So the runtime cost is
imperceptible.

Files:

* gpu_hw_shadergen.cpp: GenerateVRAMReadFragmentShader reordered
  so DeclareUniformBuffer runs before WriteCommonFunctions(ss,
  true). u_resolution_scale + u_pad0 appended to the cbuffer
  member list (after u_size). WriteCBufferResolutionScaleAliases
  call inserted between cbuffer declaration and WriteCommonFunctions.
* gpu_hw_d3d11.cpp / gpu_hw_d3d12.cpp / gpu_hw_vulkan.cpp /
  gpu_hw_opengl.cpp: each backend's ReadVRAM constructs an
  inline `uint32_t uniforms[]` array of cbuffer DWORDs and pushes
  it via DrawUtilityShader / SetGraphicsRoot32BitConstants /
  vkCmdPushConstants / UploadUniformBuffer. All four go from
  uniforms[4] (u_base_coords.xy + u_size.xy) to uniforms[6]
  (adding u_resolution_scale + u_pad0).
  D3D12 push-constants ceiling check: 6 DWORDs = 24 bytes <<
  MAX_PUSH_CONSTANTS_SIZE = 64. Vulkan: same. OpenGL preserves
  its existing y-flip on copy_rect.top (lower-left origin).

Unlike e56d4d4 / 9d2b49d there's no formal VRAMReadbackUBOData
struct in gpu_hw.h - this shader has always been driven by
inline uniforms arrays at the per-backend call site. Could be
factored to a struct in a follow-up cleanup, but the inline
arrays are tightly localised (one site per backend) and the
4-DWORD-to-6-DWORD diff is small enough that a struct would
add more noise than it removes.

Variant axis dropped: vram_read_ps had MSAA (M) x
m_resolution_scale (N) variants. After this commit it's just M
(MSAA on/off) variants regardless of scale. MSAA refactor is a
separate concern - the body has a #if MULTISAMPLING branch
controlling LOAD_TEXTURE vs LOAD_TEXTURE_MS, plus DeclareTexture
emits Texture2D vs Texture2DMS<float4> based on UsingMSAA(),
plus the MULTISAMPLES count is baked from m_multisamples for
the wave-sum loop. Routing MSAA through the cbuffer would
require pre-baking BOTH the Texture2D and Texture2DMS bindings
and runtime-selecting which one is bound, which is a bigger
ABI change than the RESOLUTION_SCALE refactor and is deferred.

Shader-cache invalidation: vram_read_ps's source string changes
(new cbuffer members + #define aliases instead of CONSTANT
literals). First boot after this lands re-compiles it once; the
two MSAA variants get regenerated cache entries. Subsequent
boots hit the cache.

Test:
  - Build under each backend succeeds.
  - Take a screenshot in-game at every resolution scale value
    (1x, 2x, 4x, 8x). Screenshot output must match what the
    framebuffer shows on screen at native (1x) and the box-
    filter downsampled equivalent at >1x. If u_resolution_scale
    weren't populated correctly:
    * Wrong loop bounds: box filter would sample wrong pixel
      ranges - garbled screenshots at >1x.
    * Wrong fast-path branch: would fall through to box filter
      at 1x and sample beyond u_size, producing artifacts.
    * Wrong divisor: image would be too bright or too dark.
    All three are visible immediately as wrong screenshot output.
  - Libretro frontend video format conversion (the runtime
    readback path that supports RA's video filter chain and
    GPU-accelerated screenshot pipeline) keeps producing
    correct frames.

Next: display_ps - last non-batch shader using compile-time-baked
RESOLUTION_SCALE in body. After that, the only remaining users
are the adaptive_downsample_* shaders (separate concern - they
already take a u_rcp_resolution cbuffer field, structure is
different).
2026-05-18 21:25:28 +02:00
LibretroAdmin
9d2b49d226 core: route RESOLUTION_SCALE through vram_write_ps cbuffer
Same refactor as e56d4d4 applied to vram_write_ps. Adds
`uint u_resolution_scale, uint u_pad0` to its cbuffer, routes
RESOLUTION_SCALE / VRAM_SIZE / RCP_VRAM_SIZE through the cbuffer
via WriteCBufferResolutionScaleAliases, switches WriteCommonFunctions
to the cbuffer-aware path (true) instead of the legacy literal
emission (false default).

vram_write_ps body references both RESOLUTION_SCALE and VRAM_SIZE
directly:

  uint2 coords = uint2(uint(v_pos.x) / RESOLUTION_SCALE,
                       fixYCoord(uint(v_pos.y)) / RESOLUTION_SCALE);
  ...
  offset.x = (coords.x < u_base_coords.x)
    ? ((VRAM_SIZE.x / RESOLUTION_SCALE) - u_base_coords.x + coords.x)
    : (coords.x - u_base_coords.x);
  offset.y = (coords.y < u_base_coords.y)
    ? ((VRAM_SIZE.y / RESOLUTION_SCALE) - u_base_coords.y + coords.y)
    : (coords.y - u_base_coords.y);

The #define aliases route both names to u_resolution_scale-based
arithmetic, identical to what the literal-baked constants
expanded to for any given m_resolution_scale. The shader body is
unchanged.

Variant axis dropped: vram_write_ps previously had PGXP_DEPTH
(2) x use_ssbo (2) x m_resolution_scale (N) variants. After this
commit it's 4 variants (PGXP_DEPTH x use_ssbo) regardless of
the current resolution scale.

Files:

* gpu_hw_shadergen.cpp: GenerateVRAMWriteFragmentShader reordered
  so DeclareUniformBuffer runs before WriteCommonFunctions(ss,
  true). u_resolution_scale + u_pad0 appended to the cbuffer
  member list. WriteCBufferResolutionScaleAliases call inserted
  between the cbuffer and WriteCommonFunctions.
* gpu_hw.h: VRAMWriteUBOData grows by 8 bytes (u_resolution_scale
  + u_pad0). 36 -> 44 bytes total. The HLSL/GLSL compiler rounds
  the cbuffer up to a multiple of 16 internally (48 bytes
  visible to the shader), but we only upload sizeof(struct) =
  44 bytes via the runtime push - the shader never reads beyond
  u_resolution_scale at offset 36-39, so the trailing 4 bytes
  the GPU view sees but we don't push are never accessed.
* gpu_hw.cpp: GetVRAMWriteUBOData populates the two new fields.
  m_resolution_scale into u_resolution_scale; 0 into u_pad0.

Cross-backend compat: same as vram_copy_ps (e56d4d4). D3D11 /
OpenGL upload via runtime sizeof; D3D12 / Vulkan use root /
push constants and have MAX_PUSH_CONSTANTS_SIZE = 64 byte
ceiling (44 fits with headroom).

Shader-cache invalidation: vram_write_ps's source string changes
on every backend - first boot after this lands re-compiles all
4 (PGXP x use_ssbo) variants once, subsequent boots hit the
regenerated cache entries.

Test: VRAM writes (which run on every game's framebuffer
update path - so every frame in practice) render correctly at
every resolution scale. The coords / offset math in the body
is the heaviest user of RESOLUTION_SCALE / VRAM_SIZE across
the non-batch shader family, so this is the strongest validation
that the alias plumbing is correct. If u_resolution_scale
weren't populated, dst_x / dst_y / offset computations would be
wrong and games would render with VRAM offsets off by a factor
of m_resolution_scale - visible immediately as garbled
framebuffers.

Next: vram_read_ps (RESOLUTION_SCALE in the box-filter loop
bounds) and display_ps (RESOLUTION_SCALE for chroma sampling
coords + the box-filter downsampling on 24-bit modes). After
those, the only remaining non-batch shader using compile-time-
baked RESOLUTION_SCALE will be the adaptive_downsample family,
which is a separate concern.
2026-05-18 21:10:59 +02:00
LibretroAdmin
e56d4d4f7b core: route RESOLUTION_SCALE through vram_copy_ps cbuffer
First step of the non-batch RESOLUTION_SCALE cbuffer refactor
that unblocks pre-bake migrations for the shaders that currently
reference RESOLUTION_SCALE / VRAM_SIZE in their bodies. Scope is
intentionally narrow: just vram_copy_ps in this commit, with the
shared helper that follow-up commits use to refactor vram_write_ps,
vram_read_ps, display_ps, etc.

Background. Pre-7b575a3, every batch shader had m_resolution_scale
baked compile-time via WriteCommonFunctions's 'CONSTANT uint
RESOLUTION_SCALE = X;' emission, which forced a fresh shader
compile and PSO rebuild for every resolution-scale change. 7b575a3
moved RESOLUTION_SCALE / true_color / scaled_dithering into the
batch UBO as cbuffer-routed fields (u_resolution_scale,
u_true_color, u_scaled_dithering) and aliased the legacy
CONSTANT names to the cbuffer fields via #define. Same shader
source serves every resolution scale; no recompile on toggle.

Non-batch shaders kept the legacy literal path because they don't
bind the batch UBO. Each one had its own per-shader cbuffer at
register(b0), and WriteCommonFunctions's batch_uniform_buffer=false
branch emitted the CONSTANT block right after WriteHeader. So
vram_copy_ps, vram_write_ps, vram_read_ps, display_ps and
adaptive_downsample_* all still embed m_resolution_scale into
their source string, and every scale change recompiles them.

That's a problem for pre-bake. The whole point of pre-bake is
one DXBC blob per (semantic) variant; if RESOLUTION_SCALE bakes
into the source, every scale value needs its own .inc file -
typically 5+ blobs per shader just for the scale dimension. The
disk-shader-cache layer hides this at runtime (the second toggle
hits the cache), but pre-bake bypasses the runtime cache and
needs the variant count to stay small.

Refactor pattern (this commit applies it to vram_copy_ps; follow-
ups apply the same shape to other affected shaders):

  1. Add `uint u_resolution_scale, uint u_pad0` to the shader's
     DeclareUniformBuffer member list. The u_pad0 keeps the
     cbuffer 16-byte-aligned (HLSL cbuffer rows are 16 bytes;
     std140 GLSL block sizes round up to vec4).
  2. Reorder shadergen calls so DeclareUniformBuffer runs before
     anything that wants RESOLUTION_SCALE in scope.
  3. After DeclareUniformBuffer, call the new helper
     WriteCBufferResolutionScaleAliases(ss). This emits the same
     three #define lines that WriteBatchUniformBuffer has been
     emitting at the end of its declaration since 7b575a3:
       #define RESOLUTION_SCALE u_resolution_scale
       #define VRAM_SIZE        (uint2(VRAM_W, VRAM_H) * u_resolution_scale)
       #define RCP_VRAM_SIZE    (float2(1,1) / float2(VRAM_SIZE))
  4. Call WriteCommonFunctions(ss, true) instead of (ss, false)
     so the legacy 'CONSTANT uint RESOLUTION_SCALE = X;' block
     is skipped. MULTISAMPLES, PER_SAMPLE_SHADING, fixYCoord and
     the RGBA conversion helpers still emit as before.
  5. Append u_resolution_scale (+ u_pad0) to the matching
     C++ UBO struct in gpu_hw.h, and populate it in the
     GetXxxUBOData getter in gpu_hw.cpp.

Helper plumbing:

* gpu_hw_shadergen.h: declare WriteCBufferResolutionScaleAliases.
* gpu_hw_shadergen.cpp: factor the three alias lines out of
  WriteBatchUniformBuffer into WriteCBufferResolutionScaleAliases.
  WriteBatchUniformBuffer calls the helper at the same spot it
  used to inline the lines, so batch shaders are unchanged.

Shader change in this commit:

* GenerateVRAMCopyFragmentShader (gpu_hw_shadergen.cpp): reordered
  so the cbuffer is declared before WriteCommonFunctions, two
  trailing members added to the cbuffer
  (uint u_resolution_scale, uint u_pad0), helper called between
  cbuffer and WriteCommonFunctions(ss, true). Body untouched -
  the existing VRAM_SIZE references inside the body keep
  resolving through the #define aliases just like in batch
  shaders.

Runtime change in this commit:

* VRAMCopyUBOData (gpu_hw.h): two new uint32_t fields appended
  at the end (u_resolution_scale, u_pad0). Struct grows 40 -> 48
  bytes. All four backends (D3D11 / D3D12 / OpenGL / Vulkan)
  upload via runtime sizeof(uniforms), so they pick up the new
  size automatically. The two push-constant backends have
  MAX_PUSH_CONSTANTS_SIZE = 64 bytes - 48 still fits.
* GetVRAMCopyUBOData (gpu_hw.cpp): populate u_resolution_scale
  with m_resolution_scale, u_pad0 with 0.

Compatibility notes:

* Shader cache invalidation: vram_copy_ps's source string
  changes (different cbuffer members + different macro
  expansion). First boot after this lands re-compiles vram_copy
  on every backend; subsequent boots hit the regenerated cache
  entry.
* Runtime correctness: the VRAM_SIZE / RCP_VRAM_SIZE references
  in the shader body resolve to the same arithmetic at runtime
  that the literal would have. Tested mentally against the
  body code paths - the source-target offset math at lines
  1336-1338 uses VRAM_SIZE.x and VRAM_SIZE.y, both of which
  expand to (1024 * u_resolution_scale) and (512 * u_resolution_scale)
  respectively. Identical to the previous CONSTANT-baked
  values for any given m_resolution_scale.
* Push-constant size: 48 bytes / 12 DWORDs on the D3D12 root
  signature side, well under the 64-byte / 16-DWORD limit at
  Add32BitConstants. Vulkan vkCmdPushConstants accepts up to
  the pipeline layout's reserved range (MAX_PUSH_CONSTANTS_SIZE
  = 64). D3D11 / OpenGL use regular UBO uploads with no fixed
  size limit.
* No behaviour change for batch shaders. WriteBatchUniformBuffer
  still emits the same final source - the three alias lines
  just come from a function call now instead of inline.

Cost: zero in steady state. The shader does one extra mul
(1024u * u_resolution_scale) per pixel where the literal was
constant-folded - but HLSL constant-folds u_resolution_scale's
known-at-PSO-bind-time value too, and even if it didn't, an
imul on a known small constant is sub-nanosecond. The push
constants are 8 extra bytes on every CopyVRAM call (negligible
compared to the rest of the per-call setup).

Test:
  - Build under each backend (D3D11 / D3D12 / OpenGL / Vulkan)
    succeeds. The new helper has no callers outside
    gpu_hw_shadergen.cpp so there's no link-side surface area.
  - CopyVRAM renders correctly at every resolution scale. The
    VRAM_SIZE references in the body now resolve through
    u_resolution_scale; if the cbuffer field weren't populated
    correctly, source/dest offsets in CopyVRAM would be wrong
    and game graphics with VRAM-to-VRAM transfers (most
    PSX games) would corrupt visibly within a few frames.
  - Toggle resolution scale at runtime (1x -> 2x -> 4x -> 1x).
    First toggle of each scale recompiles vram_copy_ps once;
    second toggle hits the cache. Behaviour matches what
    happened pre-refactor.
  - Pre-existing PGXP-depth dimension still works (two PSOs
    in the m_vram_copy_pipelines[depth_test] matrix), with
    the embedded blob path TBD - that's a follow-up patch.

Next in the arc:
  - Apply the same refactor to vram_write_ps (uses both
    RESOLUTION_SCALE and VRAM_SIZE), vram_read_ps (uses
    RESOLUTION_SCALE in the loop bounds), and display_ps
    (uses RESOLUTION_SCALE directly).
  - After all of those are refactored, the RESOLUTION_SCALE
    axis is gone from every non-batch shader's variant matrix.
    Then pre-bake migrations can proceed for each one.
2026-05-18 20:59:54 +02:00
LibretroAdmin
5df9316d13 common/d3d12: SetVertexShader / SetPixelShader bytecode overload
Cleanup deferred from 443a2a1 (pre-bake copy/blit PS) and dd54a73
(pre-bake fullscreen-quad VS). Both migrations unpacked the
D3D12_SHADER_BYTECODE aggregate by hand at every call site:

    gpbuilder.SetVertexShader(vs.pShaderBytecode,
                              static_cast<uint32_t>(vs.BytecodeLength));

That form is correct but verbose, and the verbosity grows linearly
with each pre-bake migration - the arc has roughly a dozen more
non-batch shaders to migrate before the batch matrix takes over,
plus a follow-up RESOLUTION_SCALE cbuffer refactor that opens up
the rest of the non-batch family. Wanting to keep the new call
sites clean rather than copy-pasting the unpack everywhere.

Add a third pair of overloads on GraphicsPipelineBuilder for each
of the three shader stages that this codebase uses:

  void SetVertexShader(const D3D12_SHADER_BYTECODE& bc);
  void SetGeometryShader(const D3D12_SHADER_BYTECODE& bc);
  void SetPixelShader(const D3D12_SHADER_BYTECODE& bc);

Implementation is the same one-liner as the existing ID3DBlob*
overloads - just delegate to the (data, size) form with the
SIZE_T -> uint32_t narrowing. No new code in the PSO desc
population path, just a more pleasant call site shape:

    gpbuilder.SetVertexShader(vs);
    gpbuilder.SetPixelShader(fs);

The GeometryShader overload isn't used yet by any current call
site but is included for symmetry - if a future migration brings
in a geometry shader, the API matches expectations.

Refactor every existing call site in gpu_hw_d3d12.cpp to use the
new overload. Eight sites total:

  - GetVRAMFillPipeline / GetVRAMCopyPipeline / GetVRAMWritePipeline
    / GetVRAMUpdateDepthPipeline / GetVRAMReadbackPipeline /
    GetDisplayPipeline / GetCopyPipeline: seven SetVertexShader
    sites all taking the pre-baked fullscreen-quad VS as a
    D3D12_SHADER_BYTECODE local (from GetFullscreenQuadVertexShader,
    landed dd54a73).
  - GetCopyPipeline: one SetPixelShader site taking the pre-baked
    copy/blit PS as a D3D12_SHADER_BYTECODE local (from the
    EmbeddedShaders accessor, landed 443a2a1).

Call sites at lines 1320-1321 are NOT touched - those bind the
batch VS / batch FS via ComPtr<ID3DBlob>.Get(), which goes through
the existing SetXxxShader(ID3DBlob*) overload. Same for the
ComPtr<ID3DBlob> fs in GetVRAMFillPipeline / GetVRAMCopyPipeline /
GetVRAMWritePipeline / GetVRAMUpdateDepthPipeline /
GetVRAMReadbackPipeline / GetDisplayPipeline, which are the
runtime-compiled (non-pre-baked) fragment shaders. Those continue
to use SetPixelShader(fs.Get()) until the migration arc reaches
them.

Cost / behaviour: zero. The new overloads compile down to the
same PSO desc field assignments as the (data, size) form they
delegate to. The narrowing cast from SIZE_T to uint32_t is done
inside the overload now instead of at the call site, but it's
the same cast on the same value with the same risk profile (DXBC
blobs are well under 4 GB on any reasonable shader).

Test:
  - Build under USE_D3D12=1 succeeds. New overloads link cleanly.
  - Cold boot with shader_precompile_mode = Enabled. Every non-
    batch pipeline (vram fill / copy / write / update_depth /
    readback / display / copy/blit) compiles and renders correctly
    - the bind is byte-equivalent to what the verbose unpack
    form produced.
  - Toggle multisamples for the full-flush branch. Pipelines
    rebuild via the new overload. Render correct.
  - Toggle chroma_smoothing for the partial-flush. Display
    pipelines lazy-fault through the new overload. Render correct.

Next in the arc: the RESOLUTION_SCALE cbuffer refactor for non-
batch shaders (lets non-batch HLSL read u_resolution_scale from
the cbuffer instead of having m_resolution_scale baked compile-
time via WriteCommonFunctions's batch_uniform_buffer=false path).
After that lands, vram_copy / vram_update_depth / vram_fill /
vram_read all become single-variant or near-single-variant pre-
bake targets.
2026-05-18 20:44:24 +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
77b255fb7b core/d3d12: add pre-baked DXBC for fullscreen-quad VS
Build fix for dd54a73. That commit flipped GPU_HW_D3D12 onto an
embedded DXBC blob included from
src/common/d3d12/embedded_dxbc/fullscreen_quad_vs.inc, but the
.inc itself wasn't in the patch - the apply instructions told
you to run tools/regen_d3d12_dxbc.py + git commit --amend. That
contract was wrong of me to ship: if the regen step is skipped
or fxc.exe isn't available on the applier's host, master ships
with a missing header reference and every D3D12 build fails:

    src/common/d3d12/embedded_shaders.cpp:20:10: fatal error:
    embedded_dxbc/fullscreen_quad_vs.inc: No such file or
    directory

This commit adds the missing .inc. After this lands, every
file dd54a73 references exists, the build succeeds, and the
pre-bake migration is complete.

Generation method (for reproducibility):

The submission environment doesn't have Microsoft's Windows
SDK fxc.exe available, so the .inc was generated via Wine's
reimplementation of d3dcompiler_47.dll - the same one used by
Proton / Steam Play for shipping Linux gaming. A small
fxc.exe-compatible front-end was cross-built with mingw and
invoked through Wine:

    apt-get install wine mingw-w64
    g++ -static -o fxc_lite.exe fxc_lite.cpp -ld3dcompiler  # mingw
    python3 tools/regen_d3d12_dxbc.py \
        --fxc /path/to/fxc_lite.exe --wine wine

The resulting fullscreen_quad_vs.inc is 828 bytes of valid DXBC
(magic 0x43425844, four chunks: ISGN / OSGN / SHEX / STAT,
covering the vertex input signature with SV_VertexID and the two
outputs TEXCOORD0 / SV_Position). Direct3D 12 validates and
loads DXBC produced by Wine's d3dcompiler the same as DXBC
produced by Microsoft's fxc.exe - both target the same SM 5.0
spec, both are byte-shaped DXBC containers, and the runtime
shader cache normalises over compiler version drift anyway.

Anyone with the real Windows SDK fxc.exe can regenerate the .inc
at any time:

    python3 tools/regen_d3d12_dxbc.py --fxc 'C:/Program Files (x86)/Windows Kits/10/bin/<version>/x64/fxc.exe'

The exact bytecode will differ slightly (different compiler
versions produce different instruction scheduling / register
allocation), but the shader is semantically equivalent. The
shader cache + PSO creation accept either.

What I'm going to do going forward to prevent this class of
mistake:
  1. Every pre-bake migration patch ships its .inc(s) in the
     same patch as the .hlsl source + extern decls + call-site
     flip. No "run this step after applying" workflow that can
     drift from the patch.
  2. If for some reason a .inc can't be generated in the
     submission environment, the migration patch doesn't ship -
     the .hlsl + scaffolding can land first, the runtime flip
     comes later in a separate patch that has the .inc as a
     prerequisite.
  3. The Wine + mingw + fxc_lite toolchain documented above
     gives me a working DXBC compiler in the submission
     environment, so option 1 is achievable for every future
     migration in this arc. A follow-up patch will check in
     fxc_lite (or equivalent) as tools/fxc_lite/ alongside the
     regen script, so the whole pipeline is self-hosted in the
     repo and any contributor with Wine + a mingw cross
     compiler can reproduce.

Test: build under USE_D3D12=1 succeeds (no missing header).
Runtime: cold boot with shader_precompile_mode = Enabled walks
the precompile loop without compiling the fullscreen-quad VS
(it's pre-baked); every non-batch PSO that binds the embedded
VS - vram_fill, vram_copy, vram_write, vram_update_depth,
vram_readback, display, copy/blit - renders correctly.
2026-05-18 20:08:19 +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
b833b43414 core/opengl: restore lost GetBatchProgram declarator line
Fixup for 5490cdf. When the precompile_sync block was extracted
out of CompilePrograms into the new PrecompileBatchPrograms
helper, the str_replace boundary used to insert the helper
definition above GetBatchProgram accidentally chewed the
GetBatchProgram function's signature line:

  -const GL::Program* GPU_HW_OpenGL::GetBatchProgram(GPUTextureFilter filter, ...
  +bool GPU_HW_OpenGL::PrecompileBatchPrograms(ShaderCompileProgressTracker& progress)
  +{

The replacement inserted PrecompileBatchPrograms' signature plus
opening brace, but the diff hunk also consumed the line below it
(the GetBatchProgram declarator) without re-emitting it, so the
post-helper source looked like:

  bool GPU_HW_OpenGL::PrecompileBatchPrograms(ShaderCompileProgressTracker& progress)
  {
    ...body...
    return true;
  }
  {            <-- orphan opening brace
    // Reserved_*Direct16Bit dedup...

g++ reports this as 'expected unqualified-id before { token' at
the orphan brace site, which is what the build broke on.

Restore the GetBatchProgram declarator line. Single insertion,
zero behaviour change for any other commit - the function body
inside the orphan-brace block was unchanged across the bad edit,
only the declarator was missing.

Test: build under USE_D3D12=1 / USE_OPENGL=1 succeeds; runtime is
unchanged from what 5490cdf intended (PrecompileBatchPrograms
still present and used, GetBatchProgram still has the same
signature and behaviour it had immediately before 5490cdf landed).
2026-05-18 09:18:03 +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
ccfa88fc4a core/d3d11: skip non-batch shader rebuild on filter-only flip
D3D11 parity port of the non-batch-skip optimisation that
landed for OpenGL in 5490cdf. Same motivation: the dim cache
port from 00cf11f made the only_dim_changed fast path in
UpdateSettings preserve the previous filter's sub-cube of
m_batch_pixel_shaders, but it still called CompileShaders
unconditionally - which in turn rebuilt every non-batch pixel
shader (copy / vram_fill / vram_read / vram_write /
vram_update_depth / display via RebuildDisplayPixelShaders /
downsample) plus the vertex shaders and the input layout. None
of those depend on m_texture_filter inside shadergen:
GenerateBatchFragmentShader and the WriteBatchTextureFilter
helper at gpu_hw_shadergen.cpp:706, 728, 839 are the only places
m_texture_filter is read, and they only run on the batch FS
generation path. Every other shadergen entry point ignores the
filter setting entirely.

So on the post-dim-cache only_dim_changed branch, calling
CompileShaders was making ~14 ID3D11PixelShader + ~3
ID3D11VertexShader + 1 ID3D11InputLayout objects via the disk-
backed shader cache, hitting on every existing HLSL hash
(CreatePixelShader / CreateVertexShader / CreateInputLayout each
take a DXBC blob and a few hundred microseconds), only to land
on functionally identical handles. Measured cost ~10-50 ms per
filter toggle of pure ComPtr churn that the user pays before the
batch matrix walk even starts.

Factor the precompile_sync block and the Lazy worker launch out
of CompileShaders into a new private helper:

  bool GPU_HW_D3D11::PrecompileBatchShaders(
      ShaderCompileProgressTracker& progress);

CompileShaders calls this for its initial / full-rebuild pass;
the only_dim_changed branch in UpdateSettings calls it directly
after reconstructing m_shadergen and building a progress tracker
sized for just the batch matrix (CountReachableBatchShaders). The
non-batch builds (input layout, vertex shaders, all non-batch
pixel shaders, display + downsample) are simply skipped on the
fast path - the existing ID3D11* objects in those slots stay
valid because filter is the only setting that changed and none of
them read it.

The Lazy worker launch moves from the tail of CompileShaders into
PrecompileBatchShaders. Safe to launch before CompileShaders'
remaining non-batch builds finish because the worker only walks
m_batch_pixel_shaders (and the cache + per-call tmp_shadergen
used by GetBatchPixelShader, both valid by the time
PrecompileBatchShaders runs). No render-loop overlap concern
either: CompileShaders is only called from Initialize and
UpdateSettings, both of which run between retro_run cycles, not
during them.

m_shadergen is reconstructed on the only_dim_changed branch even
though no shadergen consumer currently reads filter from it
(GetBatchPixelShader uses per-call tmp_shadergen post-dim-cache,
RebuildDisplayPixelShaders' GenerateDisplayFragmentShader is
filter-independent). The reconstruction is cheap (POD copies in
the GPU_HW_ShaderGen ctor) and forecloses any latent bug if a
future code path adds a shadergen consumer that DOES read
m_texture_filter from there. Same defensive bias as 5490cdf.

State objects (blend / depth / input layout via
CreateStateObjects) were already being skipped on this branch
since 00cf11f - this patch extends the skip to cover the
non-batch shader handles too.

Cost matrix after this patch (D3D11, default settings,
shader_precompile_mode = Enabled, 144 reachable batch shader
cells, warm shader cache):

  Toggle                  Before          After this patch
  texture_filter (warm)   ~10-50 ms       instant
                          (non-batch
                           cache hits)
  texture_filter (cold)   ~5-15 sec       ~5-15 sec - ~10-50 ms
                          + ~10-50 ms     (necessary, new sub-cube,
                                          non-batch overhead gone)
  All other toggles       unchanged       unchanged

The cold-flip win is small in relative terms (the new filter sub-
cube compile dominates) but the warm-flip win is the real one:
cycling Nearest -> Bilinear -> Nearest -> Bilinear is now bounded
by IPC latency rather than by the per-toggle non-batch cache walk.

After this lands the dim-cache cost matrix is closed on D3D11 and
OpenGL (both shipped this optimisation); D3D12 doesn't need it
(non-batch PSOs are lazy via GetXxxPipeline); Vulkan doesn't
either (same reason).

Test:
  - D3D11, shader_precompile = Enabled. Toggle texture filter
    Nearest -> Bilinear -> Nearest. First Nearest -> Bilinear
    pays the new-sub-cube cost minus the ~10-50ms; subsequent
    cycle-backs hit instant. No flicker / regression on display
    output between toggles - display_pixel_shaders aren't dropped.
  - Toggle filter under Lazy: worker stops at top of
    UpdateSettings via StopShaderCompileThread, restarts inside
    PrecompileBatchShaders for the new filter. No flicker / null
    PSO at draw time during the handoff.
  - Toggle filter under Disabled: no worker, fast path skips
    all rebuild, returns immediately. Next batch draw lazy-faults
    the cell from GetBatchPixelShader.
  - Toggle a non-dim source-affecting setting (multisamples /
    per_sample_shading / uv_limits / pgxp_depth / colour
    perspective): full-flush branch fires, DestroyShaders +
    DestroyStateObjects + CreateStateObjects + CompileShaders -
    the new helper path is bypassed and the existing full-rebuild
    behaviour is unchanged.
  - Toggle chroma_smoothing: display_only_source_changed branch
    fires, RebuildDisplayPixelShaders rebuilds the 6 display
    shaders, the helper path is not entered. Unchanged from
    93e5db5.

Note: this patch was originally submitted in the same batch as
the OpenGL companion that landed as 5490cdf, but the D3D11
companion was lost between turns (git format-patch only exported
the OpenGL commit). Resubmitting now; the diff is the same as the
original b3aef83 modulo rebase context.
2026-05-18 09:03:50 +02:00
LibretroAdmin
5490cdfb10 core/opengl: skip non-batch program rebuild on filter-only flip
OpenGL parity port of the non-batch-skip optimisation that
landed for D3D11 in the previous commit. Same motivation: the
dim cache port from f8b4a41 made the only_dim_changed fast
path in UpdateSettings preserve the previous filter's sub-cube
of m_render_programs, but it still called CompilePrograms
(with clear_existing_render_programs=false), which rebuilt the
filter-independent non-batch programs (display via
RebuildDisplayPrograms / vram_fill / vram_read / vram_write /
vram_copy / vram_update_depth / downsample) on every filter
flip. None of those read m_texture_filter inside shadergen -
GenerateBatchFragmentShader and the WriteBatchTextureFilter
helper at gpu_hw_shadergen.cpp:706, 728, 839 are the only
places m_texture_filter is referenced.

So the post-dim-cache only_dim_changed branch was making ~14
GL::Programs via the disk-backed program cache - cache hits
that reload the linked binary via glProgramBinary, plus the
shader cache lookup machinery and the GL::Program move-assign
that calls glDeleteProgram on the previous slot tenant. Measured
cost ~10-100ms per filter toggle of pure GL::Program churn,
which on OpenGL is wider than the D3D11 equivalent because the
OpenGL non-batch program set is slightly larger (m_downsample_*
adds entries the D3D11 backend handles in a different layer) and
because glProgramBinary reload has more driver overhead than
CreatePixelShader from a DXBC blob.

Factor the precompile_sync block out of CompilePrograms into a
new private helper:

  bool GPU_HW_OpenGL::PrecompileBatchPrograms(
      ShaderCompileProgressTracker& progress);

CompilePrograms calls this for its initial / full-rebuild pass;
the only_dim_changed branch in UpdateSettings calls it directly
after reconstructing m_shadergen and building a progress tracker
sized for just the batch matrix (CountReachableBatchShaders). The
non-batch builds (display + vram ops + downsample + the
m_use_binding_layout flag) are simply skipped on the fast path -
the existing GL::Program handles in those slots stay valid
because filter is the only setting that changed and none of them
read it.

Unlike the D3D11 version, this helper has no worker-launch
component: OpenGL is single-threaded by the libretro hardware-
renderer protocol (one GL context bound to the runloop), so
'Lazy' degrades to 'Disabled' shape on this backend. The helper
walks the matrix synchronously in Enabled and does nothing in
Lazy / Disabled - filter cycling under Lazy / Disabled just
faults each new sub-cube cell on first use via GetBatchProgram
at draw time, same as the original behaviour.

m_shadergen is reconstructed on the only_dim_changed branch even
though no shadergen consumer currently reads filter from it
(GetBatchProgram uses per-call tmp_shadergen post-dim-cache,
RebuildDisplayPrograms' GenerateDisplayFragmentShader is filter-
independent, the other non-batch generators ignore filter
entirely). The reconstruction is cheap (POD copies in the
GPU_HW_ShaderGen ctor) and forecloses any latent bug if a future
code path adds a shadergen consumer that DOES read m_texture_filter
from there. Same defensive bias as the D3D11 port.

Cleanup: the clear_existing_render_programs parameter on
CompilePrograms is now dead - both remaining callers (Initialize
and UpdateSettings' full-rebuild branch) want the clean-slate
behaviour. Drop the parameter; CompilePrograms always clears
m_render_programs at the top now. Reverts the dim-cache fast
path's intermediate API from f8b4a41 cleanly (the dim-cache
fast path itself isn't reverted - it just moves out of
CompilePrograms entirely and into the new direct
PrecompileBatchPrograms call site in UpdateSettings).

Cost matrix after this patch (OpenGL,
shader_precompile_mode = Enabled, 144 reachable batch shader
cells, warm program cache):

  Toggle                  Before          After this patch
  texture_filter (warm)   ~10-100 ms      instant
                          (non-batch
                           cache hits)
  texture_filter (cold)   ~5-15 sec       ~5-15 sec - ~10-100 ms
                          + ~10-100 ms    (necessary, new sub-cube,
                                          non-batch overhead gone)
  All other toggles       unchanged       unchanged

The cold-flip win is small in relative terms (the new filter sub-
cube glLinkProgram pass dominates) but the warm-flip win is the
real one: cycling Nearest -> Bilinear -> Nearest -> Bilinear is
now bounded by GL context overhead rather than by the per-toggle
non-batch cache walk.

Test:
  - OpenGL, shader_precompile = Enabled. Toggle texture filter
    Nearest -> Bilinear -> Nearest. First Nearest -> Bilinear
    pays the new-sub-cube cost minus the ~10-100ms; subsequent
    cycle-backs hit instant. No flicker / regression on display
    output between toggles - m_display_programs aren't dropped.
  - Toggle filter under Lazy: helper returns immediately (no
    worker on this backend), next batch draw lazy-faults the
    cell from GetBatchProgram. Same as Disabled in shape.
  - Toggle filter under Disabled: helper returns immediately,
    next batch draw lazy-faults.
  - Toggle a non-dim source-affecting setting (multisamples /
    per_sample_shading / uv_limits / pgxp_depth / colour
    perspective): full-rebuild branch fires, CompilePrograms
    clears all sub-cubes and walks the current filter's sub-
    cube. The new helper path is bypassed and the existing
    full-rebuild behaviour is unchanged.
  - Toggle chroma_smoothing: display_only_source_changed branch
    fires, RebuildDisplayPrograms rebuilds the 6 display
    programs, the helper path is not entered. Unchanged from
    722e98a.
  - Initialize: calls CompilePrograms(), which clears (no-op on
    default-constructed m_render_programs) and walks the full
    init flow including PrecompileBatchPrograms for the current
    filter sub-cube. Same as before, just with the parameter
    gone from the call site.
2026-05-18 08:58:55 +02:00
LibretroAdmin
594b63a7f4 core: refresh shader_source_changed comment post-dim-cache arc
The shader_source_changed comment block in UpdateHWSettings was
written when only Vulkan carried a batch pipeline dim cache, so
it characterised D3D11 / D3D12 / OpenGL as 'backends that don't
carry the Vulkan-style per-spec-const pipeline dim cache' and
positioned shader_source_changed as their gate for the rebuild
round trip. After the dim cache port arc (00cf11f for D3D11,
10c53b8 for D3D12, f8b4a41 for OpenGL) those three backends DO
carry a dim cache - just a narrower one over filter only,
because their other dim-relevant settings (true_color,
scaled_dithering) are cbuffer-routed by 7b575a3 / 418eb14 and
don't need any pipeline state change.

The two dim caches are structurally different:

  Vulkan: dim cache over (filter, true_color, scaled_dithering)
          via spec consts on m_batch_pipelines. shaders_changed
          is the gate; only_dim_changed inside that gate picks
          out the dim-cache-friendly subset.

  D3D11 / D3D12 / OpenGL: dim cache over (filter) via shader
          source branching on m_batch_pixel_shaders /
          m_batch_pipelines / m_render_programs.
          shader_source_changed is the gate (the cbuffer-routed
          three are explicitly excluded); only_dim_changed
          inside that gate picks out the filter-only-changed
          case.

Both dim caches preserve sub-cubes across the toggle the cache
is keyed on, so cycling back to a previously-visited value is
instant on all four backends. The exclusion list and the why-
chroma-stays-in clarification at the bottom of the comment are
unchanged; the patch only refreshes the cross-backend framing
in the middle to mention the cross-backend dim cache without
implying the D3D / GL backends still take the destroy-everything
path.

Comment-only change. No functional difference.
2026-05-18 08:37:54 +02:00
LibretroAdmin
7ebd5175e5 core/opengl: skip structurally unreachable cells in batch precompile loop
OpenGL parity port of the IsBatchShaderReachable filter used by
the D3D11 / D3D12 / Vulkan precompile_sync loops and their
background workers. Same motivation: cells that the runtime can
never select at draw time (NeedsTwoPassRendering short-circuits,
Reserved_*Direct16Bit dedup, single-pass dual-source on hardware
without dual-source support) burn glCompileShader + glLinkProgram
for nothing.

The three structurally unreachable cell classes are documented in
IsBatchShaderReachable's comment in gpu_hw.h. For the OpenGL
backend specifically:

  - Reserved_Direct16Bit (texture_mode 3) and
    Reserved_RawDirect16Bit (texture_mode 7) collapse to canonical
    modes 2 / 6 inside GetBatchProgram via the lookup_mode
    remapping, but unlike the D3D / Vulkan backends which alias
    the canonical slot's handle into the reserved slot at the
    matrix level, OpenGL's GL::Program is move-only and can't be
    shared - GetBatchProgram instead RE-LINKS the canonical source
    string into a fresh program object. The disk-backed program
    cache makes the second link cheap (it reloads the linked
    binary via glProgramBinary instead of recompiling from source),
    but 'cheap' is not 'free' - 8 reserved-mode cells * 2 dither *
    2 interlace = 32 program-cache lookups + glLinkProgram calls.

  - OnlyOpaque / OnlyTransparent with texture_mode == Disabled: 4
    cells each, 8 total. NeedsTwoPassRendering short-circuits on
    (texture_mode != Disabled), so untextured polys never reach
    the two-pass fallback.

  - TransparentAndOpaque with texture_mode != Disabled on hardware
    without dual-source blend: up to 28 cells, 0 on common
    desktop hardware that supports dual-source blend.

Reachable cell count on dual-source hardware: 144 - 32 - 8 = 104.
On hardware without dual-source: 144 - 32 - 8 - 28 = 76. The
shader cache absorbs most of the cost on warm boots, but cold
boots and shader cache invalidations (driver / GPU change, version
bump via SHADER_CACHE_VERSION) re-pay the full glCompileShader /
glLinkProgram cost on each unreachable cell, so this is a real
init-time win at the moments the user is most likely to notice.

batch_progress_units switches to CountReachableBatchShaders() so
the progress bar lands at 100% on the new walk count. The other
progress increments (display, vram_fill, vram_read, vram_write,
vram_update_depth, vram_copy, downsample) are unchanged - the
sizing constants (2*3 + 2*2 + 1+1+1+1+1) only cover the non-batch
helpers.

There's no background worker on OpenGL (single-threaded GL
context per the libretro hardware-renderer protocol), so this is
the only place reachability filtering needs to land - GetBatchProgram
on the runloop lazy-fault path doesn't filter because the runtime
can only ask for cells it actually intends to use.

Cost change on a cold cache (Enabled mode, dual-source hardware):
~104 / 144 = ~28% fewer glLinkProgram calls. At ~50-100 ms per
cold link, that's ~2-4 seconds off OpenGL's initial CompilePrograms
pass. On hardware without dual-source blend the win is larger
(~47%).

Test:
  - OpenGL, shader_precompile = Enabled. Cold shader cache. Boot
    progress bar reaches 100% at the end of the batch walk, no
    overshoot, no undershoot. Walk count matches
    CountReachableBatchShaders(m_supports_dual_source_blend).
  - Same boot warm cache: progress bar still aligned, all walked
    cells hit cache, no glProgramBinary fallback.
  - Game uses every texture_mode (Tekken 3 cycles through all
    palette / direct paths): no missing-program errors at draw
    time from the cells the loop now skips. The skipped cells
    are unreachable by construction, so a runtime lookup against
    one of them would be a bug elsewhere in the GPU front-end,
    not in this filter.
2026-05-18 08:19:43 +02:00
LibretroAdmin
4f6edc295f core/vulkan: skip batch matrix rebuild on chroma_smoothing-only flip
Vulkan parity port of the chroma_smoothing partial-clear that
landed for D3D12 in 57ac62e, D3D11 in 93e5db5, and OpenGL in
722e98a. Same motivation: SMOOTH_CHROMA is constant_id=103 inside
the display FS (see GetDisplayPipeline at gpu_hw_vulkan.cpp:2688)
and that spec const is referenced nowhere else. Every other PSO -
the 1164-cell m_batch_pipelines matrix and its FS modules, the
VRAM ops PSOs (vram_fill / vram_read / vram_write / vram_copy /
vram_update_depth / vram_readback), and the downsample PSOs - is
invariant under chroma_smoothing, but the previous Vulkan path
kept chroma_smoothing in the non_dim_diff set that triggered the
full-flush branch, so a chroma toggle drove the full
DestroyPipelines + CompilePipelines round trip and walked every
reachable cell of the batch matrix synchronously when
shader_precompile_mode = Enabled.

Reuses the display_only_source_changed signal added by 57ac62e
- no API change to UpdateHWSettings, just an extended call site
that requests the new out-parameter. GPU_HW_Vulkan::UpdateSettings
checks it first inside the shaders_changed gate, before
only_dim_changed and the full-flush branch.

The display-only branch destroys just m_display_pipelines (6
VkPipeline slots) via the existing enumerate +
Vulkan::Util::SafeDestroyPipeline pattern that DestroyPipelines
already uses. GetDisplayPipeline lazy-faults a fresh pipeline
with the new SMOOTH_CHROMA spec const on the next UpdateDisplay
- display pipelines are always lazy-built on Vulkan (no
precompile pre-fill path for them in any mode), so there's no
synchronous rebuild required here either; the next UpdateDisplay
call from the runloop allocates and binds the new pipeline
inline. The 6 VkShaderModule fragment shader handles are
released inside GetDisplayPipeline immediately after each
pipeline creates (the SPIR-V blob is embedded so module
construction is cheap), so there's no module leak to chase.

The background batch warm-up worker that StopShaderCompileThread
joined at the top of UpdateSettings needs to be relaunched on
this branch - it doesn't go through CompilePipelines so the
launch site near line ~1929 isn't reached. The worker only walks
m_batch_pipelines, which doesn't carry SMOOTH_CHROMA in its spec
const set anywhere, so keeping it stopped would just starve a
mid-warmup session of further background fill. The relaunch
gate matches the CompilePipelines launch verbatim - Lazy AND
Enabled both run the worker on Vulkan (Enabled's first pass over
the current filter sub-cube hits the lock-free fast-return path
since precompile_sync already filled it, then warms the other
six sub-cubes; that asymmetry between Vulkan and D3D11 / D3D12
exists for unrelated reasons and stays put here).

Resource ordering safety: UpdateSettings calls
g_vulkan_context->ExecuteCommandBuffer(true) at line 812 BEFORE
the shaders_changed block, so the GPU is idle by the time we
reach the m_display_pipelines.enumerate call - no command buffer
in flight is referencing the old display pipelines. The render
passes and pipeline layouts the display pipelines were built
against (m_display_load_render_pass, m_single_sampler_pipeline_
layout) are unchanged on a chroma toggle; the new pipelines on
lazy-fault reuse those handles.

The comment block on the full-flush branch is updated to drop
chroma_smoothing from the list of settings that hit that path -
chroma now routes through display_only_source_changed instead.
The other settings on that list (resolution scale, MSAA, per-
sample shading, UV limits, PGXP depth, colour perspective,
precompile mode) genuinely do invalidate every sub-cube and
still hit DestroyPipelines + CompilePipelines.

Cost matrix after this patch (Vulkan,
shader_precompile_mode = Enabled, 1164 reachable batch PSO cells):

  Toggle                  Before              After this patch
  chroma_smoothing        ~1 min              ~0 sec
                          (full matrix walk   (1 PSO lazy-faulted
                          + DestroyPipelines)  on next UpdateDisplay)
  texture_filter (warm)   instant             unchanged
  texture_filter (cold)   ~1 min              unchanged (necessary,
                                              new filter sub-cube)
  true_color (warm)       instant             unchanged
  true_color (cold)       ~1 min              unchanged (necessary,
                                              new tuple sub-cube)
  scaled_dithering        same as true_color  unchanged
  resolution_scale        ~1 min              unchanged (per-session
                                              spec const)
  multisamples            ~1 min              unchanged (structural
                                              SPIR-V choice)
  per_sample_shading      ~1 min              unchanged (necessary)
  uv_limits               ~1 min              unchanged (necessary)
  pgxp_depth              ~1 min              unchanged (necessary)
  disable_color_persp.    ~1 min              unchanged (necessary)
  shader_precompile_mode  ~1 min              unchanged (intentional)

Vulkan's absolute cost on chroma flip drops from ~1 min (the worst
case in the matrix, because Vulkan's PSO count is 8x D3D11's batch
shader count) to effectively zero - one VkPipeline create on the
next UpdateDisplay. After this patch, every backend's
UpdateSettings cost matrix has the same shape: only the toggles
that genuinely invalidate batch state pay the per-cell rebuild
cost.

Test:
  - Vulkan, shader_precompile = Enabled. Toggle 24-bit chroma
    smoothing on / off: no progress OSD spike, no audible runloop
    hitch. Next UpdateDisplay lazy-builds one VkPipeline for the
    current (depth_24, interlace_mode) cell with the new
    SMOOTH_CHROMA spec const. Subsequent UpdateDisplay calls hit
    populated slots for any other (depth_24, interlace_mode)
    combinations the game reaches.
  - Same toggle under Lazy: identical behaviour to Enabled here -
    display pipelines are always lazy-faulted, only the worker
    relaunch differs (relaunched against the same m_batch_pipelines
    state it left, so cells already in flight or warmed stay
    populated).
  - Same toggle under Disabled: identical to the above but the
    worker isn't relaunched (Disabled never starts one).
  - Toggle chroma_smoothing AT THE SAME TIME as another non-dim
    setting via batch reload: display_only_source_changed returns
    false (other_source_diff is true), the full-flush branch fires
    with the new chroma value picked up by every regenerated
    display pipeline at GetDisplayPipeline time.
  - Cycle chroma on -> off -> on: each toggle clears
    m_display_pipelines and re-lazy-faults one pipeline. The
    on-state and off-state SPIR-V binaries are both cached in the
    Vulkan pipeline cache after the first cycle, so subsequent
    chroma flips create the corresponding VkPipeline from cache
    (microseconds) rather than re-compiling the SPIR-V.
2026-05-18 08:13:35 +02:00
LibretroAdmin
722e98aacb core/opengl: skip batch matrix rebuild on chroma_smoothing-only flip
OpenGL parity port of the chroma_smoothing partial-clear that
landed for D3D12 in 57ac62e and for D3D11 in 93e5db5. Same
motivation: chroma_smoothing is a DefineMacro inside
GenerateDisplayFragmentShader only - it never reaches the batch
program matrix or the VRAM ops programs. The previous OpenGL
patches kept it in shader_source_changed but didn't split it out
from the dim-cache branch, so on OpenGL a chroma toggle still
called CompilePrograms with clear_existing_render_programs=true
(only_dim_changed is false for chroma, since chroma sits in
non_dim_diff), which wiped every filter sub-cube and walked the
144-cell batch program matrix synchronously when
shader_precompile_mode = Enabled.

Reuses the display_only_source_changed signal added by 57ac62e
(no API change). GPU_HW_OpenGL::UpdateSettings checks it first
inside the shader_source_changed gate, before the
CompilePrograms call that handles the dim-cache and full-flush
paths. The display-only branch calls a new helper,
RebuildDisplayPrograms, that recompiles the 2x3
m_display_programs matrix against the new m_chroma_smoothing
value (which UpdateHWSettings has already written into the
member). The 144-cell batch program matrix, the VRAM ops
programs (vram_fill / vram_read / vram_write / vram_copy /
vram_update_depth), and the downsample programs all stay valid
because none of them carry chroma_smoothing as ctor or
compile-time state.

CompilePrograms is refactored to call the same helper for its
initial display program build, so the 6-cell compile loop only
lives in one place. Progress tracking is bumped six times
inline after the helper returns to match the pre-patch progress
bar units.

chroma_smoothing only takes effect on the depth_24bit paths
(see the SMOOTH_CHROMA DefineMacro guard in
GenerateDisplayFragmentShader), so technically only three of
the six programs actually depend on it. Rebuilding all six
anyway keeps the path simple - on a chroma toggle the three
depth_24bit=false programs re-resolve to cache hits on their
existing GLSL hashes in m_shader_cache (instant GetProgram
return - the cache reloads the linked binary from disk via
glProgramBinary instead of recompiling and re-linking), and the
three depth_24bit=true programs pick up the new SMOOTH_CHROMA
value via a fresh GL_FRAGMENT_SHADER compile + glLinkProgram.

Unlike the D3D11 / D3D12 ports, this patch has no worker-thread
relaunch step - the libretro hardware-renderer protocol gives
OpenGL a single GL context bound to the runloop thread, so
there's no background compile worker to stop or restart. The
precompile_mode contract on this path is simply
'RebuildDisplayPrograms is synchronous' in every mode (Enabled,
Lazy, Disabled).

The other rebuild branches (only_dim_changed via
clear_existing_render_programs=false, full-flush via
clear_existing_render_programs=true) are unchanged - they're
hit only when something the batch matrix genuinely depends on
changed, and the dim cache / full-flush paths from f8b4a41
already do the right thing there.

Cost matrix after this patch (OpenGL,
shader_precompile_mode = Enabled, 144 batch program cells):

  Toggle                  Before (f8b4a41)    After this patch
  chroma_smoothing        ~5-15 sec           ~600 ms
                          (full matrix walk)  (6 display programs)
  texture_filter (warm)   instant             unchanged
  texture_filter (cold)   ~5-15 sec           unchanged
                          (new sub-cube)
  resolution_scale        zero                unchanged
  true_color              zero                unchanged
  scaled_dithering        zero                unchanged
  multisamples            ~5-15 sec           unchanged (necessary)
  per_sample_shading      ~5-15 sec           unchanged (necessary)
  uv_limits               ~5-15 sec           unchanged (necessary)
  pgxp_depth              ~5-15 sec           unchanged (necessary)
  disable_color_persp.    ~5-15 sec           unchanged (necessary)
  shader_precompile_mode  ~5-15 sec           unchanged (intentional)

Every remaining 'still pays' toggle in the matrix is one that
genuinely invalidates batch GLSL across the board. The OpenGL
backend's UpdateSettings path is now structurally on par with
the D3D11 / D3D12 backends' post-57ac62e / 93e5db5 cost
matrices. The only remaining backend without this optimisation
is Vulkan - same pattern would apply (Vulkan's chroma toggle
currently does a full DestroyPipelines + CompilePipelines and
could clear just the 6-slot display VkPipeline cache).

Test:
  - OpenGL, shader_precompile = Enabled. Toggle 24-bit chroma
    smoothing on / off: ~600 ms pause (6 display program
    rebuilds), no batch matrix progress OSD. Batch draw remains
    hot - no stutter on the next frame.
  - Same toggle under Lazy / Disabled: ~600 ms pause for the
    display rebuild, no worker behavior to verify (OpenGL has
    none). Batch matrix stays as it was - empty in Disabled
    mode, partially populated in Lazy if any draws have run.
  - Toggle chroma_smoothing AT THE SAME TIME as another setting
    (e.g. via batch settings reload): display_only_source_changed
    returns false (other_source_diff is true), the
    CompilePrograms branch fires with the right
    clear_existing_render_programs flag as before.
  - Cycle chroma on -> off -> on: first toggle pays the 600ms
    compile, second toggle hits all 6 entries as cache hits in
    m_shader_cache and returns instantly. Third toggle hits
    cache hits on the on-state programs too. Steady-state
    chroma cycling is effectively free.
2026-05-18 08:04:58 +02:00
LibretroAdmin
f8b4a41bd9 core/opengl: port filter dim cache from D3D backends
OpenGL parity port of the filter dim cache that landed for D3D12
in 10c53b8 and for D3D11 in 00cf11f. Same motivation: after
418eb14 the cbuffer-refactor series made resolution_scale /
true_color / scaled_dithering toggles instant on OpenGL, but a
texture_filter flip still drove CompilePrograms and walked the
144-cell batch program matrix from scratch in Enabled precompile
mode, or worse, dropped the previous filter's compiled programs
and re-compiled them on the runloop the next time the user
flipped back.

Add GPUTextureFilter::Count (7) as the outermost dimension of
m_render_programs:

  std::array<...<GL::Program, 2>, 2>, 9>, 4>, 7>
                                          ^
                                       filter

(OpenGL keeps its existing nested-std::array style rather than
switching to DimensionalArray, matching the D3D11 dim-cache port
at 00cf11f; D3D12 uses DimensionalArray, but the indexing
semantics are the same.)

GetBatchProgram gains a GPUTextureFilter filter parameter as the
first argument, indexes m_render_programs with [filter_idx][...],
and constructs a per-call GPU_HW_ShaderGen against the requested
filter rather than reusing m_shadergen. The single-threaded GL
context never reaches the helper with a non-current filter
today, but the per-call shadergen keeps the API symmetric with
the D3D / Vulkan ports and future-proofs the helper if a worker
ever gets added (or if pre-baked GLSL ever wants to land here
the way pre-baked SPIR-V did for Vulkan). The
'GetBatchProgram called before CompilePrograms constructed the
shadergen' defensive check is dropped for the same reason - the
per-call shadergen has no dependency on the lifetime of
m_shadergen.

CompilePrograms gains a new clear_existing_render_programs
parameter (default true). On the only_dim_changed fast path
UpdateSettings passes false, so the previously-built filter
sub-cubes are preserved instead of being walked and reset to
default-constructed GL::Programs (which would glDeleteProgram
each one and force a fresh link on the next visit). The
precompile_sync loop walks just the current filter's sub-cube
- snapshots cur_filter = m_texture_filtering once and uses it
for every GetBatchProgram call inside the loop, matching the
D3D11 / D3D12 / Vulkan precompile patterns. Slots that were
already populated by a previous visit short-circuit on
IsValid() in GetBatchProgram and return without doing any work;
empty slots (first visit to this filter) get compiled.

CompilePrograms's clear loop gains a fifth level of nesting to
walk the new 5-D shape when clear_existing_render_programs is
true.

DrawBatchVertices passes m_texture_filtering as the first arg to
GetBatchProgram so the lookup hits the active sub-cube.

UpdateSettings's rebuild gate stays a single-branch
'if (shader_source_changed) CompilePrograms(...)' but the call
now takes the only_dim_changed-derived flag:

  if (shader_source_changed)
    CompilePrograms(/*clear_existing_render_programs=*/!only_dim_changed);

Cycling back to a previously-visited filter (Nearest -> Bilinear
-> Nearest) is now a slot-validity walk on OpenGL in any
precompile mode - the second Nearest hit walks the precompile_sync
loop, every cell short-circuits on IsValid(), no shader compile,
no program link. A fresh-cold filter flip (first time visiting
Bilinear) still pays the full matrix walk to populate the new
sub-cube, but the OLD filter's programs stay alive in their
slots and are reachable the moment the user flips back.

Cost matrix after this patch (OpenGL,
shader_precompile_mode = Enabled, 144 batch program cells):

  Toggle                  Before        After this patch
  texture_filter (cold)   ~5-15 sec     ~5-15 sec (necessary,
                                        new sub-cube)
  texture_filter (warm)   ~5-15 sec     instant (IsValid() walk)
  resolution_scale        zero          unchanged
  true_color              zero          unchanged
  scaled_dithering        zero          unchanged
  multisamples            ~5-15 sec     unchanged (necessary -
                                        invalidates every filter
                                        sub-cube)
  per_sample_shading      ~5-15 sec     unchanged (necessary)
  uv_limits               ~5-15 sec     unchanged (necessary)
  pgxp_depth              ~5-15 sec     unchanged (necessary)
  disable_color_persp.    ~5-15 sec     unchanged (necessary)
  chroma_smoothing        ~5-15 sec     unchanged (suboptimal,
                                        splittable as a follow-up
                                        analogous to 57ac62e /
                                        93e5db5)
  shader_precompile_mode  ~5-15 sec     unchanged (intentional)

OpenGL's absolute cost is on par with D3D11 (same 144-cell matrix,
similar link cost per cell). The relative improvement on warm
filter cycling is the same as the D3D11 / D3D12 / Vulkan ports.

Test:
  - OpenGL, shader_precompile = Enabled. First boot: matrix walks
    only Nearest's sub-cube. Program compile count and progress
    bar match pre-patch.
  - Toggle texture filter Nearest -> Bilinear: precompile_sync
    walks Bilinear's sub-cube, Bilinear programs link for the
    first time. Old Nearest sub-cube stays populated.
  - Toggle back Bilinear -> Nearest: precompile_sync loop hits
    every Nearest slot on its IsValid() fast path and returns
    instantly. No GL_FRAGMENT_SHADER compile, no glLinkProgram,
    no progress OSD spike.
  - Toggle Nearest -> Bilinear again: every Bilinear slot is
    already populated. Instant.
  - Toggle resolution scale / true_color / scaled_dithering:
    still pays zero per 418eb14, unchanged.
  - Toggle MSAA / per_sample / UV limits / PGXP depth / colour
    perspective: CompilePrograms with clear_existing=true wipes
    all sub-cubes, full rebuild walks the current filter's sub-
    cube. Same cost as pre-patch.
  - shader_precompile_mode = Lazy / Disabled: GetBatchProgram
    faults each cell on first draw, same as before. Filter
    toggle doesn't clear previously-visited sub-cubes; revisits
    are instant once the cells are populated.
2026-05-18 07:36:40 +02:00
LibretroAdmin
93e5db5239 core/d3d11: skip batch matrix rebuild on chroma_smoothing-only flip
D3D11 parity port of the chroma_smoothing partial-clear that
landed for D3D12 in 57ac62e. Same motivation: chroma_smoothing
is a DefineMacro inside GenerateDisplayFragmentShader only - it
never reaches the batch pixel shader matrix or the VRAM ops
pixel shaders. The previous D3D11 patches kept it in
shader_source_changed, so on D3D11 a chroma toggle still drove
the full DestroyShaders + DestroyStateObjects +
CreateStateObjects + CompileShaders round trip and walked the
144-cell batch pixel shader matrix synchronously when
shader_precompile_mode = Enabled.

Reuses the display_only_source_changed signal added by 57ac62e
(no API change). GPU_HW_D3D11::UpdateSettings checks it first
inside the shader_source_changed gate, before only_dim_changed
and the full-flush branch. The display-only branch calls a new
helper, RebuildDisplayPixelShaders, that recompiles the 2x3
m_display_pixel_shaders matrix against the new m_chroma_smoothing
value (which UpdateHWSettings has already written into the
member). The 144-cell batch pixel shader matrix, the VRAM ops
pixel shaders, the blend / depth-stencil / input layout state
objects, and the batch vertex shaders all stay valid because
none of them carry chroma_smoothing as ctor or compile-time
state.

CompileShaders is refactored to call the same helper for its
initial display-shader build, so the 6-cell compile loop only
lives in one place. Progress tracking is bumped six times
inline after the helper returns to match the pre-patch progress
bar units.

chroma_smoothing only takes effect on the depth_24bit paths (see
the '&& m_chroma_smoothing' guard in GenerateDisplayFragmentShader),
so technically only three of the six shaders actually depend on
it. Rebuilding all six anyway keeps the path simple - on a chroma
toggle the three depth_24bit=false shaders re-resolve to cache
hits on their existing HLSL hashes in m_shader_cache (instant
GetPixelShader return), and the three depth_24bit=true shaders
pick up the new SMOOTH_CHROMA value via a fresh D3DCompile.

UpdateSettings also handles the precompile_mode contract on the
rebuild side now that the partial-clear branch skips
CompileShaders:

  Enabled: RebuildDisplayPixelShaders is synchronous, so the
           next draw doesn't stutter on display shader fault-in.
  Lazy:    relaunch the background worker that
           StopShaderCompileThread joined at the top of
           UpdateSettings. The worker only walks the batch
           pixel shader matrix, so it doesn't care about
           chroma_smoothing - keeping it stopped after a chroma
           toggle would just starve mid-warmup sessions of
           further background fill.
  Disabled: no worker, no extra work - matches the standing
            'skip all compilation at init' contract for the
            batch matrix.

The other rebuild branches (only_dim_changed, full destroy) are
unchanged - they're hit only when something the batch matrix
genuinely depends on changed, and the dim cache / full-flush
paths from 00cf11f already do the right thing there.

Cost matrix after this patch (D3D11, default settings,
shader_precompile_mode = Enabled, 144 reachable batch shader
cells):

  Toggle                  Before (00cf11f)    After this patch
  chroma_smoothing        ~5-15 sec           ~600 ms
                          (full matrix walk)  (6 display shaders)
  texture_filter (warm)   instant             unchanged
  texture_filter (cold)   ~5-15 sec           unchanged
                          (new sub-cube)
  resolution_scale        zero                unchanged
  true_color              zero                unchanged
  scaled_dithering        zero                unchanged
  multisamples            ~5-15 sec           unchanged (necessary)
  per_sample_shading      ~5-15 sec           unchanged (necessary)
  uv_limits               ~5-15 sec           unchanged (necessary)
  pgxp_depth              ~5-15 sec           unchanged (necessary)
  disable_color_persp.    ~5-15 sec           unchanged (necessary)
  shader_precompile_mode  ~5-15 sec           unchanged (intentional)

Every remaining 'still pays' toggle in the matrix is one that
genuinely invalidates batch HLSL across the board; the D3D11
backend's UpdateSettings path is now structurally on par with
the D3D12 backend's post-57ac62e cost matrix.

Test:
  - D3D11, shader_precompile = Enabled. Toggle 24-bit chroma
    smoothing on / off: ~600 ms pause (6 display pixel shader
    rebuilds), no batch matrix progress OSD. Batch draw remains
    hot - no stutter on the next frame.
  - Same toggle under Lazy: ~600 ms pause for the display
    rebuild, worker is paused for the duration of UpdateSettings
    then relaunched, batch matrix continues warming.
  - Same toggle under Disabled: ~600 ms pause for the display
    rebuild, no worker, batch matrix stays as it was (mostly
    empty until faulted by draws).
  - Toggle chroma_smoothing AT THE SAME TIME as another setting
    (e.g. via batch settings reload): display_only_source_changed
    returns false (other_source_diff is true), the only_dim or
    full-flush branch fires as before.
2026-05-18 07:26:52 +02:00
LibretroAdmin
00cf11f5d0 core/d3d11: port filter dim cache from D3D12 backend
D3D11 parity port of the dim cache that landed for D3D12 in
10c53b8. Same motivation: after 418eb14 the cbuffer-refactor
series made resolution_scale / true_color / scaled_dithering
toggles instant on D3D11, but a texture_filter flip still drove
DestroyShaders + CompileShaders and recompiled the whole 144-cell
batch pixel shader matrix (plus state objects, but those are a
handful of cheap CreateBlendState / CreateRasterizerState calls).

Add GPUTextureFilter::Count (7) as the outermost dimension of
m_batch_pixel_shaders and m_batch_pixel_shader_fastpath:

  std::array<...<ComPtr<ID3D11PixelShader>, 2>, 2>, 9>, 4>, 7>
                                                        ^
                                                     filter

(D3D11 keeps its existing nested-std::array style rather than
switching to DimensionalArray; matching the D3D12 backend's
DimensionalArray<..., 7> at the indexing level is what matters,
not the underlying container.)

GetBatchPixelShader gains a GPUTextureFilter filter parameter as
the first argument, indexes both arrays with [filter_idx][...],
and constructs a per-call GPU_HW_ShaderGen against the requested
filter rather than reusing m_shadergen (which stays bound to the
runtime-current m_texture_filtering for the non-batch helpers
called elsewhere - they don't depend on filter so rebuilding
m_shadergen on every batch shader compile would be pure overhead).
Filter is the only ctor input that differs between m_shadergen
and the per-call shadergen; all other inputs come from member
state that's single-valued per session. The expensive work
(GenerateBatchFragmentShader call + D3DCompile +
CreatePixelShader) is unchanged from the pre-dim-cache path. The
error-log thread-safety check on m_shadergen is dropped because
the per-call shadergen makes the helper independent of
CompileShaders having constructed m_shadergen first; all call
sites still go through CompileShaders or its descendants so the
invariant is preserved structurally.

CompileShaders's precompile_sync block snapshots cur_filter =
m_texture_filtering once and walks only that sub-cube. Same
pattern for ShaderCompileThreadEntryPoint - the worker captures
cur_filter at launch and walks just that sub-cube. UpdateSettings
calls StopShaderCompileThread BEFORE UpdateHWSettings writes
m_texture_filtering, so the snapshot the worker takes always
matches the filter it's supposed to be warming. The next
CompileShaders pass after a filter flip launches a fresh worker
captured to the new filter. Mirrors the D3D12 ordering verbatim.

DrawBatchVertices passes m_texture_filtering as the first arg to
GetBatchPixelShader so the lookup hits the active sub-cube.

UpdateSettings's rebuild gate gains the only_dim_changed branch:

  if (shader_source_changed) {
    if (only_dim_changed) {
      // Filter sub-cube changed; others stay valid.
      // CompileShaders lazy-populates the new filter on top.
      // State objects are filter-independent so CreateStateObjects
      // can be skipped on this path - blend states key on
      // transparency_mode, depth-stencil on depth-test mode,
      // input layout on uv_limits (handled by non_dim_diff above).
      CompileShaders();
    } else {
      // Non-dim shader change (MSAA / per-sample / UV limits /
      // chroma smoothing / PGXP depth / colour perspective /
      // precompile mode): every sub-cube is invalidated.
      DestroyShaders();
      DestroyStateObjects();
      CreateStateObjects();
      CompileShaders();
    }
  }

Cycling back to a previously-visited filter (Nearest -> Bilinear
-> Nearest) is now instant on D3D11 in any precompile mode - the
second Nearest hit walks the precompile_sync loop and the
fast-path atomic-load wins on every cell, no D3DCompile, no
CreatePixelShader. A fresh-cold filter flip (first time visiting
Bilinear) still pays the full matrix walk to populate the new
sub-cube, but the OLD filter's shaders stay alive in their slots
and are reachable via an atomic load the moment the user flips
back.

DestroyShaders's fast-path-clear loop gains a fifth level of
nesting to match the new 5-D shape.

Cost matrix after this patch (D3D11, default settings,
shader_precompile_mode = Enabled, 144 reachable batch shader
cells):

  Toggle                  Before        After this patch
  texture_filter (cold)   ~5-15 sec     ~5-15 sec (necessary,
                                        new sub-cube)
  texture_filter (warm)   ~5-15 sec     instant (atomic load)
  resolution_scale        zero          unchanged
  true_color              zero          unchanged
  scaled_dithering        zero          unchanged
  multisamples            ~5-15 sec     unchanged (necessary -
                                        invalidates every filter
                                        sub-cube)
  per_sample_shading      ~5-15 sec     unchanged (necessary)
  uv_limits               ~5-15 sec     unchanged (necessary)
  pgxp_depth              ~5-15 sec     unchanged (necessary)
  disable_color_persp.    ~5-15 sec     unchanged (necessary)
  chroma_smoothing        ~5-15 sec     unchanged (suboptimal,
                                        same as D3D12 pre-57ac62e
                                        - splittable as a
                                        follow-up)
  shader_precompile_mode  ~5-15 sec     unchanged (intentional)

D3D11 is fewer-tens-of-seconds than D3D12 in absolute terms
because D3D11 has 144 batch shader cells vs D3D12's 1164 PSO
cells (PSO state combinatorics inflate the count), but the
relative improvement on warm filter cycling is the same.

Test:
  - D3D11, shader_precompile = Enabled. First boot: matrix walks
    only Nearest's sub-cube. Shader compile count and progress
    bar match pre-patch.
  - Toggle texture filter Nearest -> Bilinear: precompile_sync
    walks Bilinear's sub-cube. Bilinear pixel shaders build for
    the first time. Old Nearest sub-cube stays populated.
  - Toggle back Bilinear -> Nearest: precompile_sync loop hits
    every Nearest slot on its fast-path atomic load and returns
    instantly. No D3DCompile, no progress OSD.
  - Toggle Nearest -> Bilinear again: every Bilinear slot is
    already populated. Instant.
  - Toggle resolution scale / true_color / scaled_dithering:
    still pays zero per 418eb14, unchanged.
  - Toggle MSAA / per_sample / UV limits / PGXP depth / colour
    perspective: DestroyShaders clears all sub-cubes, full
    rebuild walks the current filter's sub-cube. Same cost as
    pre-patch.
  - shader_precompile_mode = Lazy: worker captures
    m_texture_filtering at launch, walks just that sub-cube.
    Filter toggle joins worker, restarts with new filter
    snapshot, walks new sub-cube. Main thread can fault any
    cell ahead of the worker via GetBatchPixelShader.
2026-05-18 07:16:41 +02:00
LibretroAdmin
57ac62e792 core/d3d12: skip batch matrix rebuild on chroma_smoothing-only flip
Final 'unnecessary recompile' in the cbuffer-refactor patch series:
chroma_smoothing is a DefineMacro inside
GenerateDisplayFragmentShader only - it never reaches the batch
FS, the batch VS, or any of the VRAM ops PSOs. But every previous
patch in this series kept it in shader_source_changed, so on
D3D12 a chroma toggle still drove the full DestroyPipelines +
CompilePipelines round trip and walked the ~1164-PSO batch matrix
synchronously when shader_precompile_mode = Enabled. Same
multi-tens-of-seconds pause the user kept hitting.

Add a new narrower out-parameter to GPU_HW::UpdateHWSettings:

  display_only_source_changed (default nullptr, opt-in for callers)

It is true only when chroma_smoothing flipped AND nothing else in
the shader-source-affecting set changed. The asymmetric carve-out
is intentional - multisamples / per_sample_shading affect the
VRAM fill / copy / write / update depth non-batch PSOs too (see
the gpbuilder.SetMultisamples calls), so a generic
'non-batch source changed' signal wouldn't actually buy a clean
partial-rebuild path; chroma_smoothing is the one non-batch source
that's truly localised to the display FS. shader_precompile_mode
is excluded for a separate reason - flipping it is a request to
walk the matrix synchronously NOW, which the full destroy +
rebuild correctly handles.

GPU_HW_D3D12::UpdateSettings checks the new signal first inside
the shader_source_changed gate. The display-only branch calls a
new helper, ClearDisplayPipelines, that drops only
m_display_pipelines + m_display_pipelines_fastpath (6 slots, 2x3
over (depth_24, interlace_mode)). The batch matrix, the batch
shader blobs, the VRAM ops PSOs, the copy PSO, and the
fullscreen-quad VS all stay valid because none of them carry
chroma_smoothing as ctor or compile-time state. The next
UpdateDisplay's GetDisplayPipeline lazy-faults each cleared slot
with HLSL re-generated against the new m_chroma_smoothing - which
UpdateHWSettings has already written into the member by the time
ClearDisplayPipelines runs.

UpdateSettings also handles the precompile_mode contract on the
rebuild side now that the partial-clear branch skips
CompilePipelines:

  Enabled: pre-fault the six display PSOs synchronously so the
           first UpdateDisplay after the toggle doesn't stutter.
           Six D3DCompile + six PSO creates run in roughly a
           second on a modern GPU.
  Lazy:    relaunch the background worker that
           StopShaderCompileThread joined at the top of
           UpdateSettings. The worker only walks the batch
           matrix, so it doesn't care about chroma_smoothing -
           keeping it stopped after a chroma toggle would just
           starve mid-warmup sessions of further background
           fill. Display PSOs themselves are faulted on first
           use, same as the standing Lazy contract for the
           non-batch pipelines.
  Disabled: nothing to do - matches 'skip all compilation at
            init', display PSOs fault on first use.

The other rebuild branches (only_dim_changed, full destroy) are
unchanged - they're hit only when something the batch matrix
genuinely depends on changed, and the dim-cache / full-flush
paths from 10c53b8 + 418eb14 already do the right thing there.

Vulkan, D3D11, and OpenGL UpdateSettings keep their existing
calls to UpdateHWSettings (no display_only_source_changed param
passed) and therefore keep their existing behaviour - full
destroy + rebuild on chroma_smoothing. The default-nullptr
parameter design means none of them have to change to compile;
optimising those backends for the same case is left as follow-up
work.

Cost matrix after this patch (D3D12, default settings,
shader_precompile_mode = Enabled, ~1164 reachable PSOs):

  Toggle                  Before (10c53b8)    After this patch
  chroma_smoothing        ~1 minute           ~1 second
                          (full matrix walk)  (6 display PSOs)
  texture_filter (warm)   instant             unchanged
  texture_filter (cold)   ~1 minute           unchanged
                          (new sub-cube)
  resolution_scale        framebuffer resize  unchanged
  true_color              zero                unchanged
  scaled_dithering        zero                unchanged
  multisamples            ~1 minute           unchanged (necessary)
  per_sample_shading      ~1 minute           unchanged (necessary)
  uv_limits               ~1 minute           unchanged (necessary)
  pgxp_depth              ~1 minute           unchanged (necessary)
  disable_color_persp.    ~1 minute           unchanged (necessary)
  shader_precompile_mode  ~1 minute           unchanged (intentional)

Every remaining 'still pays' toggle in the matrix is one that
genuinely invalidates batch HLSL across the board; no further
free wins survive in the D3D12 UpdateSettings path.

Test:
  - D3D12, shader_precompile = Enabled. Toggle 24-bit chroma
    smoothing on / off: ~1 second pause (6 display PSO rebuilds),
    no batch matrix progress OSD. Batch draw remains hot - no
    stutter on the next frame.
  - Same toggle under Lazy: instant. Worker is paused for the
    duration of UpdateSettings, then relaunched; display PSOs
    lazy-fault on first UpdateDisplay (visible as a single-frame
    stutter on the first refresh, identical to Lazy's standing
    behaviour for any non-batch PSO).
  - Same toggle under Disabled: instant. Display PSOs fault on
    first UpdateDisplay.
  - Toggle chroma_smoothing AT THE SAME TIME as another setting
    (e.g. via batch settings reload): display_only_source_changed
    returns false (other_source_diff is true), the only_dim or
    full-flush branch fires as before.
2026-05-18 07:08:19 +02:00
LibretroAdmin
10c53b8524 core/d3d12: port filter dim cache from Vulkan backend
Final piece of the cbuffer-refactor patch series. After 7b575a3
made resolution_scale / true_color / scaled_dithering invariant
under HLSL source and 418eb14 stopped DestroyPipelines + the
1164-PSO sync compile from firing on cbuffer-only toggles, the
remaining ~1-minute pause on D3D12 was on texture-filter toggles:
filter genuinely changes the FilteredSampleFromVRAM emission and
needs new DXBC, so 418eb14's shader_source_changed correctly
gated a full destroy + rebuild on every filter flip. That's the
last hot per-session setting the user can move through the menu
that still costs a full matrix walk.

Port the dim-cache shape the Vulkan backend has carried since the
glslang-elimination series: add GPUTextureFilter::Count (7) as
the outermost dimension of m_batch_pipelines /
m_batch_pipelines_fastpath / m_batch_fragment_shader_blobs /
m_batch_fragment_shader_blobs_fastpath and wire it through
GetBatchPipeline + GetBatchFragmentShader as the first parameter.
D3D12 doesn't need to dimension over true_color / scaled_dithering
the way Vulkan does (those bake into VkSpecializationInfo at
pipeline-create time on Vulkan and need a distinct VkPipeline per
spec-const combo - on D3D12 they're cbuffer fields now after the
7b575a3 refactor and don't enter the PSO state), so the shape is
the simpler 7-D layout
'[filter][depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]'
rather than Vulkan's 9-D
'[filter][true_color][scaled_dithering][...]'.

GetBatchFragmentShader builds the HLSL through a per-call
GPU_HW_ShaderGen rather than reusing m_shadergen (which stays
bound to the runtime-current m_texture_filtering for the
non-batch helpers - those don't depend on filter so reconstructing
m_shadergen on every batch FS compile would be pure overhead).
Filter is the only ctor input that differs between m_shadergen and
the per-call shadergen; all other inputs come from member state
that's single-valued per session. The expensive work
(GenerateBatchFragmentShader call + D3DCompile) is unchanged from
the pre-dim-cache path. The error-log thread-safety check on
m_shadergen is dropped because the per-call shadergen makes the
helper independent of CompilePipelines having constructed
m_shadergen first; all call sites still go through CompilePipelines
or its descendants so the invariant is preserved structurally.

GetBatchPipeline indexes both m_batch_pipelines and
m_batch_pipelines_fastpath with [filter_idx][...]. The blend-state
gate moves from m_texture_filtering to the filter PARAMETER -
critical for worker / lazy-fault on a non-current filter's
sub-cube, otherwise that sub-cube's PSOs would inherit whatever
the runtime filter happens to be when they get faulted in and
produce wrong blend mode after a later filter switch. PSO debug
name format gains a leading 'f%u' so the debugger can disambiguate
the per-filter clones of the same structural cell.

CompilePipelines's precompile_sync block snapshots cur_filter =
m_texture_filtering once and walks only that sub-cube. Pre-filling
the other six filter sub-cubes would multiply the cold-cache
D3DCompile pass by 7x for no gain: the game can only run under
one filter at a time, and the other sub-cubes get lazy-populated
the first time UpdateSettings flips to them. Same pattern for
ShaderCompileThreadEntryPoint - the worker captures cur_filter at
launch and walks just that sub-cube. UpdateSettings calls
StopShaderCompileThread BEFORE UpdateHWSettings writes
m_texture_filtering, so the snapshot the worker takes always
matches the filter it's supposed to be warming. The next
CompilePipelines pass after a filter flip launches a fresh worker
captured to the new filter.

UpdateSettings's rebuild gate gains the only_dim_changed branch
that mirrors Vulkan's UpdateSettings line ~864:

  if (shader_source_changed) {
    if (only_dim_changed) {
      // Filter sub-cube changed; other sub-cubes stay valid.
      // CompilePipelines lazy-populates the new filter on top.
      CompilePipelines();
    } else {
      // Non-dim shader change (MSAA / per-sample / UV limits /
      // chroma smoothing / PGXP depth / colour perspective /
      // precompile mode): every sub-cube is invalidated.
      DestroyPipelines();
      CompilePipelines();
    }
  }

Cycling back to a previously-visited filter (Nearest -> Bilinear
-> Nearest) is now instant in any precompile mode - the second
Nearest hit just walks the precompile_sync loop and the fast-path
atomic-load wins on every cell, no D3DCompile, no
CreateGraphicsPipelineState, no descriptor work. A fresh-cold
filter flip (first time visiting Bilinear) still pays the full
matrix walk to populate the new sub-cube, but the OLD filter's
PSOs stay alive in their slots and are reachable via an atomic
load the moment the user flips back.

Cost matrix after this patch (D3D12, default settings,
shader_precompile_mode = Enabled, ~1164 reachable PSOs):

  Toggle                  Before (418eb14)    After this patch
  texture_filter (cold)   ~1 minute           ~1 minute (necessary,
                                              new sub-cube)
  texture_filter (warm)   ~1 minute           instant (atomic load)
  resolution_scale        framebuffer resize  unchanged (already
                                              optimal post-418eb14)
  true_color              zero                unchanged
  scaled_dithering        zero                unchanged
  multisamples            ~1 minute           ~1 minute (necessary -
                                              invalidates every
                                              filter sub-cube)
  per_sample_shading      ~1 minute           ~1 minute (necessary)
  uv_limits               ~1 minute           ~1 minute (necessary)
  pgxp_depth              ~1 minute           ~1 minute (necessary)
  disable_color_persp.    ~1 minute           ~1 minute (necessary)
  chroma_smoothing        ~1 minute           ~1 minute (suboptimal,
                                              same as 418eb14)
  shader_precompile_mode  ~1 minute           ~1 minute (intentional)

Test:
  - D3D12, shader_precompile = Enabled. First boot: matrix walks
    only Nearest's sub-cube. PSO count and progress bar match
    pre-patch.
  - Toggle texture filter Nearest -> Bilinear: precompile_sync
    walks Bilinear's sub-cube. Bilinear PSOs build for the first
    time. Old Nearest sub-cube stays populated.
  - Toggle back Bilinear -> Nearest: precompile_sync loop hits
    every Nearest slot on its fast-path atomic load and returns
    instantly. No D3DCompile, no progress OSD.
  - Toggle Nearest -> Bilinear again: every Bilinear slot is also
    already populated. Instant.
  - Toggle resolution scale / true_color / scaled_dithering: still
    pays zero per 418eb14, unchanged.
  - Toggle MSAA / per_sample / UV limits / PGXP depth / colour
    perspective: DestroyPipelines clears the entire matrix
    (enumerate covers all filter sub-cubes), full rebuild walks
    the current filter's sub-cube. Same cost as 418eb14 - the
    full destroy is genuinely required because those settings
    bake into HLSL identically for every filter slot.
  - shader_precompile_mode = Lazy: worker captures
    m_texture_filtering at launch, walks just that sub-cube.
    Filter toggle joins worker, restarts with new filter
    snapshot, walks new sub-cube. Main thread can fault any
    cell ahead of the worker via GetBatchPipeline.
2026-05-18 06:39:38 +02:00
LibretroAdmin
418eb1430e core, common: skip pipeline rebuild on cbuffer-only setting changes
Post 7b575a3 (cbuffer refactor), HLSL/GLSL batch shader source is
invariant under resolution_scale / true_color / scaled_dithering -
those three settings ride the per-batch UBO and are picked up by
the next FlushRender at no shader-compile cost on D3D11 / D3D12 /
OpenGL. The backends were still treating every toggle of any of
the three as a full shader rebuild though, because their
UpdateSettings handlers checked shaders_changed and that signal
still fired on cbuffer-only diffs - shaders_changed was defined as
'dim_diff || non_dim_diff', which includes both filter (real source
change) AND true_color/scaled_dithering (now cbuffer-only) AND
resolution_scale (now cbuffer-only).

With shader_precompile_mode = Enabled and a populated batch matrix
(~1164 reachable PSO cells in the default-user configuration), the
DestroyPipelines + CompilePipelines round trip walks every cell
synchronously through D3DCompile / glCompileShader before
UpdateSettings returns. On the user's system that's the full
'changing options can take up to a minute' pause they kept hitting
after every res-scale or true-color toggle, even though by
construction the patch that landed before this one made those
toggles trivially cheap at the shader level.

Add a new narrower out-parameter to GPU_HW::UpdateHWSettings:

  shader_source_changed (default nullptr, opt-in for callers)

It is true only when a setting that the shader generator bakes
into the emitted HLSL / GLSL string has flipped: texture filter,
multisamples, per-sample shading, UV limits, chroma smoothing,
PGXP depth, disable-color-perspective, and shader precompile mode.
resolution_scale, true_color, and scaled_dithering are explicitly
excluded - they are pure batch UBO cbuffer fields now. Both
chroma_smoothing and shader_precompile_mode stay in because they
do change shader-side work (chroma_smoothing is baked into the
display FS at GenerateDisplayFragmentShader call sites on the
D3D11 / D3D12 / OpenGL paths, and a Disabled -> Enabled precompile
flip is a request to compile the matrix synchronously now even
though no source has actually changed).

The existing shaders_changed signal is preserved verbatim. Vulkan
relies on it together with only_dim_changed to drive its
spec-const-aware dim-cache path, which still has to fire on
true_color / scaled_dithering flips because those spec constants
bake into VkPipeline at create time and demand a new pipeline per
sub-cube even though the SPIR-V text is identical. Vulkan keeps
calling UpdateHWSettings with the old four-argument form (no
shader_source_changed) and behaves exactly as before.

GPU_HW_D3D11::UpdateSettings, GPU_HW_D3D12::UpdateSettings, and
GPU_HW_OpenGL::UpdateSettings switch their compile-rebuild check
from shaders_changed to shader_source_changed. State-object
recreation in the D3D11 path is gated on the same signal: state
descriptions key on MSAA / dual-source / depth-test / blend mode,
all of which sit in the source-changed set, so existing state
objects stay correct through cbuffer-only toggles. framebuffer
rebuild is unchanged - it still fires on resolution_scale via the
framebuffer_changed signal, just without dragging the pipeline
rebuild along with it.

Concrete cost matrix after this patch (D3D12, default settings,
shader_precompile_mode = Enabled, populated matrix of ~1164 PSOs):

  Toggle                       Before     After
  resolution_scale             ~1 minute  framebuffer resize only
  true_color                   ~1 minute  zero
  scaled_dithering             ~1 minute  zero
  texture_filter               ~1 minute  ~1 minute (necessary)
  multisamples                 ~1 minute  ~1 minute (necessary)
  per_sample_shading           ~1 minute  ~1 minute (necessary)
  uv_limits                    ~1 minute  ~1 minute (necessary)
  chroma_smoothing             ~1 minute  ~1 minute (display FS;
                                          could be split later)
  pgxp_depth                   ~1 minute  ~1 minute (necessary)
  disable_color_perspective    ~1 minute  ~1 minute (necessary)
  shader_precompile_mode       ~1 minute  ~1 minute (intentional)
  downsample_mode (non-adapt.) currently no-op on D3D12, unchanged

Same shape on D3D11 and OpenGL.

Test:
  - D3D12, shader_precompile = Enabled, populated cache. Toggle
    resolution scale 4x -> 1x -> 4x: each transition is
    framebuffer-resize fast, no 'Compiling Pipelines N/1164' OSD.
  - Toggle true_color on / off: instant, no OSD.
  - Toggle scaled_dithering on / off: instant.
  - Toggle texture filter Nearest -> Bilinear: still pays the
    precompile (necessary - filter changes batch FS source).
  - Toggle MSAA / per-sample / UV limits / PGXP depth / colour
    perspective: still pays (necessary).
  - chroma_smoothing toggle: still pays (suboptimal - the batch
    matrix doesn't actually depend on it, only the display FS
    does, but splitting batch / non-batch rebuild is a separate
    change; leaving it at the conservative correct behaviour for
    now).
  - Same battery on D3D11 and OpenGL.
  - Vulkan: behaviour unchanged. Still uses dim-cache for filter /
    true_color / scaled_dithering and full rebuild for everything
    else.
2026-05-18 06:14:16 +02:00
LibretroAdmin
6a90885384 core: re-seed per-session cbuffer fields in Reset
GPU_HW::Reset zero-inits m_batch_ubo_data via 'm_batch_ubo_data = {};',
then sets m_batch_ubo_dirty = true so the next FlushRender uploads.

Before the cbuffer-refactor patch (7b575a3), every field in BatchUBOData
was per-draw-owned and got refilled by the SetDrawMode / SetTextureWindow
/ interlace / mask-bit paths before the next batch flushed; zeroing them
in Reset was therefore safe - the zeroed values were transient and the
next draw overwrote them before they were ever uploaded with the dirty
flag.

The refactor introduced three per-session fields (u_resolution_scale,
u_true_color, u_scaled_dithering) that are written ONLY in Initialize
and UpdateHWSettings, not on the per-draw path. After Reset zeroed
them, nothing put the correct values back unless the user then changed
a renderer setting. The very first FlushRender after Reset uploaded
u_resolution_scale = 0, and every subsequent batch ran the FS with
that value until UpdateHWSettings happened to fire.

Symptom on D3D12 (and on any backend; D3D12 was just where the user
landed first): every textured fragment that touched RESOLUTION_SCALE
through the macro alias path sampled VRAM at infinite or
not-a-number coordinates and discarded. RCP_VRAM_SIZE expands to
float2(1.0) / float2(uint2(1024u, 512u) * u_resolution_scale); with
u_resolution_scale = 0 that's float2(1.0) / float2(0.0, 0.0) = INF,
multiplied through every SampleFromVRAM coord. Palette lookups hit
index 0 (the PSX 'transparent' palette entry), every glyph fragment
hit the VECTOR_EQ(texcol, TRANSPARENT_PIXEL_COLOR) discard, and the
text disappeared entirely. Untextured polygons rendered fine because
the FS for the !TEXTURED branch never references RESOLUTION_SCALE,
and the only VS use is the vertex_offset ternary
(RESOLUTION_SCALE == 1u) ? 0.5 : 0.0, which falls to the same 0.0
branch for scale == 0 and scale > 1 alike.

Reproduction case from testing: D3D12 backend, fresh cache nuked,
shader precompilation = Enabled, resolution_scale 4x. Boot Tekken 3 -
title screen logo and PS BIOS logo render fine, but BIOS text below
the logo is missing and the highlighted 'ARCADE MODE' menu entry
shows only its gradient backdrop with no glyphs on top. Vulkan
unaffected because it does not share this code path - its per-session
settings ride VkSpecializationInfo against pre-baked SPIR-V, and a
zero spec const would produce the same INF coordinates if the same
bug were present, but the Vulkan path simply doesn't zero its
specialization state on Reset.

Fix: after 'm_batch_ubo_data = {};' in GPU_HW::Reset, re-seed the
three per-session fields from their backing m_resolution_scale /
m_true_color / m_scaled_dithering members. These members are set in
Initialize before any Reset can fire and are kept in sync by
UpdateHWSettings, so they are always correct at Reset time. The
re-seed runs before m_batch_ubo_dirty = true on the next line, so
the upload that the dirty flag triggers carries the seeded values.

The fix is minimal and targeted: it does not change the zero-init
behaviour for the per-draw fields (which still get overwritten by
SetDrawMode etc. before any FlushRender that matters), does not
move any state machinery between Reset and Initialize, and does
not touch the cbuffer layout or shader generation. The three
restored writes mirror exactly what Initialize and UpdateHWSettings
do for the same fields.

Test:
  - D3D12, nuked cache, shader_precompile = Enabled, resolution 4x.
    Boot the BIOS - text below the PS logo should render.
  - Load Tekken 3 - highlighted ARCADE MODE entry text should
    appear over the gradient.
  - Same on D3D11 and OpenGL backends (the bug was present on
    those too but only D3D12 was tested first; same fix applies).
  - Vulkan should be unchanged before and after.

This is a Reset-path follow-up to 7b575a3 ('route resolution_scale,
true_color, scaled_dithering through batch cbuffer'), not a revert
or partial revert. The cbuffer refactor still stands: the FS / VS
HLSL remains invariant under the three settings, the dim-cache
explosion is still avoided, and patch 2 (pre-baked DXBC) can land
on top of this without further changes.
2026-05-18 05:53:36 +02:00
LibretroAdmin
7b575a3f87 core, common: route resolution_scale, true_color, scaled_dithering through batch cbuffer
Three per-session settings used to be baked into the HLSL / GLSL
source string at shadergen time:

  - RESOLUTION_SCALE: emitted as 'CONSTANT uint RESOLUTION_SCALE =
    N u;' at the top of every batch shader. VRAM_SIZE / RCP_VRAM_SIZE
    derived from it inline. Every distinct resolution scale produced
    a distinct source string -> distinct DXBC / GLSL blob.
  - TRUE_COLOR: DefineMacro'd as a #define, guarded around #if /
    #if ! / #else / #endif blocks in ApplyDithering, the textured
    colour-truncation path, the untextured non-DITHERING fallback,
    and the final colour premultiply. Two distinct source strings
    per shader cell (true_color on / off).
  - DITHERING_SCALED: DefineMacro'd as a #define around the dither
    fragment-coordinate selector in ApplyDithering. Two distinct
    source strings (scaled_dithering on / off).

Net result: each batch fragment shader cell could produce up to
~5 (typical resolution scales) x 2 x 2 = 20 distinct DXBC blobs
across a session's settings space, every one of them requiring a
fresh D3DCompile invocation on first encounter. On D3D12 with
shader precompilation Enabled and a cold disk cache, the
combinatorial explosion is exactly the 'roughly one minute initial
startup' pain.

This patch moves the three settings out of the source string and
into the per-batch uniform buffer. The HLSL / GLSL produced by
GenerateBatchFragmentShader / GenerateBatchVertexShader becomes
INVARIANT under those three settings - a session-state toggle is
a single 4-byte cbuffer write picked up on the next FlushRender,
with no shader recompile, no PSO rebuild, no batch matrix flush.

Mechanically:

  - BatchUBOData (gpu_hw.h) grows by 16 bytes:
      uint32_t u_resolution_scale;
      uint32_t u_true_color;       // bool packed as uint32_t
      uint32_t u_scaled_dithering; // same
      uint32_t u_pad0;             // pad to 16-byte cbuffer row
    The new fields are written from m_resolution_scale /
    m_true_color / m_scaled_dithering in two places: GPU_HW::
    Initialize after the member values are set, and
    GPU_HW::UpdateHWSettings right after the same members are
    updated. Both sites set m_batch_ubo_dirty = true so the next
    FlushRender uploads.

  - WriteCommonFunctions (gpu_hw_shadergen.cpp) gains a
    'batch_uniform_buffer' parameter (default false). When true it
    skips the file-scope 'CONSTANT uint RESOLUTION_SCALE = ...',
    'CONSTANT uint2 VRAM_SIZE = ...', and 'CONSTANT float2
    RCP_VRAM_SIZE = ...' emissions; the cbuffer + macro alias path
    supplies them instead. Non-batch shaders (VRAM fill / copy /
    write / read / update-depth, display, downsample) continue
    using the file-scope CONSTANT path - they're a handful of
    shaders with a much smaller combinatorial space and don't
    benefit from the cbuffer indirection.

  - WriteBatchUniformBuffer adds the three new cbuffer fields and
    a u_pad0 entry, then emits three preprocessor aliases:
      #define RESOLUTION_SCALE u_resolution_scale
      #define VRAM_SIZE (uint2(VRAM_WIDTH u, VRAM_HEIGHT u) *
                         u_resolution_scale)
      #define RCP_VRAM_SIZE (float2(1.0, 1.0) / float2(VRAM_SIZE))
    Every existing reference in the shader body keeps working as-is
    via macro substitution. VRAM_WIDTH / VRAM_HEIGHT stay
    compile-time literals - they are fixed PSX geometry (1024 x
    512), not session state.

  - GenerateBatchVertexShader / GenerateBatchFragmentShader call
    WriteBatchUniformBuffer BEFORE WriteCommonFunctions. The
    ordering matters: fixYCoord (defined inside
    WriteCommonFunctions) references RCP_VRAM_SIZE and VRAM_SIZE,
    which after the cbuffer-aware refactor are macros expanding to
    expressions involving u_resolution_scale. u_resolution_scale
    must be declared (cbuffer emitted) before the macros are
    expanded in fixYCoord's body, otherwise the HLSL / GLSL
    single-pass parser sees an undeclared identifier.

  - GenerateBatchFragmentShader drops the DefineMacro calls for
    TRUE_COLOR and DITHERING_SCALED, and the four #if (!)TRUE_COLOR
    / #if DITHERING_SCALED preprocessor blocks become runtime
    'if (u_true_color != 0u)' / 'if (u_scaled_dithering != 0u)'
    branches. These are uniform-control-flow branches on
    cbuffer-uniform values - free on every modern GPU. The inner
    RAW_TEXTURE / DITHERING / etc. #ifs remain compile-time
    because those dimensions are still structural in the batch
    matrix.

  - Shader cache file format versions bumped on D3D11 (2 -> 3),
    D3D12 (1 -> 2), and OpenGL (3 -> 4). Existing on-disk caches
    were keyed by HLSL / GLSL source hashes that included the
    inlined RESOLUTION_SCALE / TRUE_COLOR / DITHERING_SCALED
    literals; after this patch the source no longer contains those
    literals, so the hashes change. Bumping the file version
    forces a clean rebuild of the cache file on first launch
    rather than leaving stale entries to grow the file
    indefinitely.

Vulkan is unaffected at runtime. The Vulkan backend uses the
pre-baked SPIR-V templates under data/shaders/vulkan/*.glsl,
which handle RESOLUTION_SCALE / TRUE_COLOR / DITHERING_SCALED via
VkSpecializationInfo spec constants (IDs 0, 106, 105). Those
templates are independent of GPU_HW_ShaderGen; the runtime path
patches the spec consts at vkCreateGraphicsPipelines time and the
result is the same architectural shape this patch gives D3D11 /
D3D12 / OpenGL via the cbuffer.

Smoke checks:
  - Tree-wide grep confirms no DefineMacro for TRUE_COLOR or
    DITHERING_SCALED survives in gpu_hw_shadergen.cpp; only one
    explanatory comment references them by name.
  - u_resolution_scale / u_true_color / u_scaled_dithering are
    referenced in gpu_hw_shadergen.cpp (the cbuffer declaration
    plus the runtime branch sites), gpu_hw.cpp (the two write
    paths in Initialize and UpdateHWSettings), and gpu_hw.h
    (the BatchUBOData struct definition).

Test priority:
  - D3D12, shader cache nuked. Boot a game at default settings
    (resolution scale 4x, true_color off, scaled_dithering on,
    no MSAA, Nearest). Initial precompile_sync runs - it still
    has to compile ~120 fragment shader sources once, but they
    are now invariant under the three migrated settings.
    Subsequent toggles of true_color or scaled_dithering should
    no longer trigger any D3DCompile.
  - Toggle resolution scale (1x -> 4x -> 2x -> ...). The HLSL
    source is identical across all scale values; the cache
    should hit every time after the first compile pass. The
    framebuffer is still rebuilt (textures resize), and PSOs are
    still rebuilt (driver bytecode-to-ISA pass), but no fresh
    D3DCompile invocations.
  - Toggle true_color on and off mid-session. Visually identical
    behaviour to before this patch (24-bit vs 16-bit framebuffer
    look). No precompile pause.
  - Toggle scaled_dithering on and off mid-session. Visually
    identical dithering pattern behaviour. No precompile pause.
  - Same battery on D3D11 and OpenGL backends.
  - Vulkan: no behavioural change expected (still uses pre-baked
    SPIR-V via spec consts). Confirm filter / true_color /
    scaled_dithering toggles continue to be instant on revisit
    via the dim cache from earlier work.

This is patch 1 of the D3D12 startup work. Patch 2 will add the
pre-baked DXBC infrastructure (regen_d3d12_dxbc.py-style offline
tool plus .inc tables) that pre-baked SPIR-V did for Vulkan in
the glslang elimination series. With the source invariance from
this patch, the pre-bake size is bounded to the structural
matrix (~120 blobs for the default-user MSAA=1 / no-per_sample /
no-color-perspective case) rather than the full per-session
explosion.
2026-05-18 05:24:27 +02:00
LibretroAdmin
bc40ae59fb libretro: route downsample mode changes through SET_GEOMETRY, not SET_SYSTEM_AV_INFO
The Vulkan-side surgical downsample handling from c369aa5 worked
correctly inside GPU_HW_Vulkan::UpdateSettings - no batch matrix
flush, no VRAM round-trip on Disabled <-> Box - but the user still
saw a multi-second freeze on every downsample toggle. The freeze
was coming from a completely different layer.

HostInterface::UpdateSettings in the libretro shim does this when
either gpu_resolution_scale or gpu_downsample_mode differs from the
cached value:

    UpdateSystemAVInfo(true);     // <- this is the killer

UpdateSystemAVInfo issues RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO.
Per the libretro spec, that callback is the only way to change
geometry.max_width / max_height, and most frontends - RetroArch
included - respond to it by tearing down the entire hardware
rendering context (swapchain, FBOs, hw negotiation) and asking the
core to rebuild from scratch. With the new dimensioned batch
pipeline cache (filter / true_color / scaled_dithering sub-cubes)
the cost is catastrophic: every warmed sub-cube the worker has
built is discarded, and the user pays a full cold-cache rebuild on
every toggle of any downsampling submenu entry.

The libretro spec also gives us SET_GEOMETRY:

    'max_width/max_height arguments are ignored and cannot be
     changed with this call as this could potentially require a
     reinitialization or a non-constant time operation. If
     max_width/max_height are to be changed, SET_SYSTEM_AV_INFO
     is required.'

That is exactly the right primitive for a downsample-only change -
provided max_width / max_height do NOT in fact need to change. The
previous behaviour did move max with downsample mode because
GetSystemAVInfo computed both base AND max from GetResolutionScale(),
which returns 1 for Box and gpu_resolution_scale otherwise. So Box
toggles forced max to flip between VRAM_WIDTH * 1 and
VRAM_WIDTH * gpu_resolution_scale, which legitimately required the
heavy SET_SYSTEM_AV_INFO path.

Decouple base from max in GetSystemAVInfo:

  - base_scale uses GetResolutionScale() (downsample-aware) - reflects
    the actual output texture size the core writes each frame.
    Box -> 1, Disabled / Adaptive -> gpu_resolution_scale.
  - max_scale uses g_settings.gpu_resolution_scale directly - the
    upper bound on the texture size for the entire session,
    independent of downsample mode. Box mode hands the frontend a
    smaller texture than max, which is perfectly legal libretro
    behaviour - the frontend allocates the larger backing buffer
    and the core fills only the lower-left base_width x base_height
    region.

With max no longer flipping on Box toggles, only an actual
gpu_resolution_scale change moves it. Route the two paths
accordingly in HostInterface::UpdateSettings:

  - resolution_scale_changed: SET_SYSTEM_AV_INFO is unavoidable
    (max grows / shrinks). Existing flow preserved: ReportMessage,
    UpdateSystemAVInfo(true), RequestHardwareRendererContext /
    SwitchToHardwareRenderer as needed.
  - downsample_mode_changed without resolution_scale_changed:
    UpdateGeometry() instead. SET_GEOMETRY updates base width /
    height and aspect ratio in-place; no driver reinit, no
    swapchain rebuild, no batch pipeline cache wipe.
    GPU_HW_Vulkan::UpdateSettings already handles the actual
    downsample resource swap in <2 ms via the surgical path from
    c369aa5.

Smoke test:
  - Tree-wide grep for UpdateSystemAVInfo / UpdateGeometry confirms
    both helpers exist and the spelling matches.
  - max_scale uses std::max which is already used elsewhere in this
    TU, so the implicit <algorithm> include is preserved.

Test priority:
  - Vulkan, shader cache nuked, Enabled mode. Boot a game at
    resolution scale 3x. Wait for the warm-up to settle (filter
    sub-cubes populated). Toggle Downsampling from None to 'Box -
    downsample 3D / smooth all'. Expected: no freeze, the new
    output appears within a frame or two. Toggle to 'Adaptive -
    smooth boundaries'. Expected: same near-instant transition,
    the batch matrix is preserved.
  - Change Resolution Scale from 3x to 4x. Expected: the heavy path
    still runs (max grows, SET_SYSTEM_AV_INFO required), the
    batch matrix is rebuilt - same as before this patch, no
    regression there.
  - Hardware renderer switch (GL <-> Vulkan, Vulkan <-> Software):
    unchanged - those go through RequestHardwareRendererContext /
    SwitchToHardwareRenderer which only fire on the resolution
    scale path.
  - Box mode rendering: the frontend texture is base_width x
    base_height (display * 1) inside a max_width x max_height
    (VRAM * gpu_resolution_scale) allocation. The display output
    should appear identical to before this patch - the frontend
    is reading from the same region of the same texture handed
    over by the libretro hw interface.

This and c369aa5 together get Vulkan downsample toggles from
multi-second freeze to perceptually instant. The D3D11 / D3D12
follow-ups will gain the same surgical handling on the backend
side; the libretro shim change here applies regardless of
backend.
2026-05-18 04:20:13 +02:00
LibretroAdmin
c369aa5d8c core, common/vulkan: surgical downsample mode handling - no VRAM round-trip on Disabled <-> Box
Toggling the 'downsampling' core option from None to Box (or back)
previously triggered the same full UpdateSettings reset path that a
resolution-scale change does: a ReadVRAM -> DestroyFramebuffer ->
CreateFramebuffer -> UpdateVRAM round-trip combined with
DestroyPipelines + CompilePipelines. On a Vulkan backend with the
new dimensioned batch cache this is even more painful than it used
to be, because the full pipeline rebuild also discards every filter
/ true_color / scaled_dithering sub-cube the worker has been
populating and forces the warm-up to start over.

Downsample mode actually affects very little of the backend state:
the batch matrix never references it (no spec const, no structural
SPIR-V choice), and the only framebuffer dimensions that depend on
it are the display texture width and the existence of the optional
downsample weight texture - and even those only differ when Adaptive
is involved on one side of the transition. Disabled <-> Box keeps
the display texture at GPU_MAX_DISPLAY_WIDTH * res_scale and just
needs the downsample_texture / render pass / framebuffer / first-
pass PSO swapped in.

Three changes that work together:

1. UpdateHWSettings:
   - *framebuffer_changed no longer trips on every downsample diff.
     Now only when Adaptive is involved on at least one end of the
     transition (Disabled <-> Adaptive, Box <-> Adaptive).
     Disabled <-> Box leaves the flag false because the main
     framebuffer dimensions are unchanged.
   - Downsample mode dropped from non_dim_diff: it has never
     affected the batch matrix, and surfacing it through
     *shaders_changed forced every backend into DestroyPipelines +
     CompilePipelines when the only thing that needed to change was
     a handful of downsample resources.
   - New optional out-parameter *downsample_changed - simple
     boolean diff between cached m_downsample_mode and the new
     value. Default nullptr so the D3D11 / D3D12 callers compile
     unchanged (they will get the same targeted treatment in a
     follow-up patch once their downsample resource lifecycle is
     factored the same way; for now they continue to do the full
     reset on a downsample toggle, matching the pre-patch
     behaviour).

2. New Vulkan helpers extracted from Create/DestroyFramebuffer and
   DestroyPipelines:
   - CreateDownsampleResources(texture_width, texture_height,
                               texture_format) -> bool
     Builds the downsample textures, render pass(es), framebuffers,
     image views, and descriptor sets for the current
     m_downsample_mode. Called from the end of CreateFramebuffer
     for the full-rebuild path and directly from UpdateSettings for
     the Disabled <-> Box path.
   - DestroyDownsampleResources()
     Drops everything CreateDownsampleResources owns - textures,
     framebuffers, image views, descriptor sets. Called from
     DestroyFramebuffer and from the downsample-only path. Does
     NOT touch pipelines (those have a separate lifecycle gated on
     the render pass).
   - DestroyDownsamplePipelines()
     Drops the 4 downsample VkPipeline handles. Called from
     DestroyPipelines (existing behaviour) and from the downsample-
     only path in UpdateSettings (new behaviour - the PSOs
     reference the OLD render pass and would otherwise leak a
     stale handle).

3. GPU_HW_Vulkan::UpdateSettings:
   - Receives downsample_changed alongside the existing flags.
   - downsample_changed && !framebuffer_changed (the Disabled <->
     Box case): DestroyDownsamplePipelines +
     DestroyDownsampleResources + CreateDownsampleResources. ~1-2
     ms of work plus 1 PSO compile. No VRAM round-trip, no batch
     matrix touch.
   - downsample_changed && framebuffer_changed (Disabled <->
     Adaptive / Box <-> Adaptive): CreateFramebuffer already
     rebuilt the downsample resources via its end-of-function
     CreateDownsampleResources call. We just need to drop the
     stale PSOs.
   - downsample_changed && !shaders_changed: pre-build the new
     downsample PSOs via the existing GetDownsampleXxxPipeline
     helpers in Enabled / Lazy modes (so the next display call
     doesn't lazy-fault); Disabled stays truly lazy by contract.
   - Adaptive's full lifecycle (the side of the transition that
     does need the framebuffer round-trip) is unchanged - the
     display texture width and weight texture creation still
     require it.

Smoke tests:
  - Tree-wide grep confirms every CreateDownsampleResources /
    DestroyDownsampleResources / DestroyDownsamplePipelines call
    site is intentional, and no leftover inline copy of the
    Adaptive / Box construction blocks survives outside the helper.
  - The two GetBatchPipeline helper paths and the precompile worker
    do not touch any downsample state, so this patch is orthogonal
    to the dim-cache work from the preceding commits.

Test priority:
  - In Vulkan, with shader cache nuked, in Enabled mode: toggle
    'Downsampling' from None to 'Box - downsample 3D / smooth all'.
    Expected: no progress-bar pause, the new Box-mode output
    appears within ~one frame. Toggle back to None: same instant
    behaviour. Cycle several times. Previously each toggle paid
    5-10 seconds of pipeline rebuild plus VRAM round-trip.
  - None <-> Adaptive and Box <-> Adaptive: framebuffer rebuild is
    still required (display texture width changes), but the pause
    should now be limited to the framebuffer work (~tens of ms);
    the batch matrix is preserved across the toggle.
  - Adaptive output should still look correct after each toggle
    (the helper extraction preserves the original code byte-for-
    byte, just moved into CreateDownsampleResources).
  - With downsample at Box, toggle resolution scale or MSAA -
    these trigger the full framebuffer + shaders rebuild as
    before (downsample mode unchanged, downsample_changed=false),
    no behavioural change.
  - With downsample at Adaptive, toggle texture filter -
    only_dim_changed=true path runs, batch matrix sub-cube
    preserves other filter values, downsample resources
    untouched. Cycle back to original filter is instant.

The same factoring will be done for D3D12 and D3D11 in follow-up
patches; their downsample resource lifecycle is structurally
identical, just with ID3D12PipelineState / ID3D11PixelShader
instead of VkPipeline. For now those backends ignore the new
downsample_changed flag (default nullptr) and continue with the
full DestroyPipelines + CompilePipelines reset on a downsample
toggle - same as before this patch.
2026-05-18 04:07:34 +02:00
LibretroAdmin
dbdd273622 core, common/vulkan: add true_color and scaled_dithering as batch pipeline cache dimensions
Option B added texture filter as the outermost dimension of
m_batch_pipelines, making filter swaps instant on revisit. The same
problem still bit the user on toggling 'scaled dithering' and 'true
colour' in the core options menu: both are pure per-session spec
constants (DITHERING_SCALED = id 105, TRUE_COLOR = id 106) applied
at vkCreateGraphicsPipelines time, so the driver bakes them into
each cached PSO. With no cache dimension over them, the existing
non_filter_diff comparison in UpdateHWSettings would set
*only_filter_changed = false on a toggle, routing UpdateSettings
into the full DestroyPipelines + CompilePipelines path - the same
5-10 second pause Option B got rid of for filter.

Extend the same trick to both:

  m_batch_pipelines: 7D -> 9D
    was [filter][depth_test][render_mode][texture_mode]
        [transparency_mode][dithering][interlacing]
    now [filter][true_color][scaled_dithering][depth_test]
        [render_mode][texture_mode][transparency_mode]
        [dithering][interlacing]

m_batch_fragment_shaders stays 5D - true_color and scaled_dithering
do NOT affect SPIR-V blob choice, only the spec const value at PSO
bake time, so the same VkShaderModule backs both values. The PSO
cache doubles for each new dimension (atomic slots only - driver-
side memory grows only when a slot is populated).

API changes:

  - GPU_HW::UpdateHWSettings out-parameter renamed
    only_filter_changed -> only_dim_changed, with the semantics
    broadened: the flag is true iff the only shader-affecting
    changes are in dimensioned settings (filter, true_color,
    scaled_dithering). Default-nullptr param so the D3D11 / D3D12
    callers stay untouched - they continue doing a full destroy +
    recompile on any shaders_changed event because their batch
    caches are not yet dimensioned over anything.

  - GetBatchPipeline gains two leading parameters:
        GetBatchPipeline(filter, true_color, scaled_dithering, ...)
    The internal m_true_color / m_scaled_dithering reads (slot
    indexing context and the AddBool(105/106, ...) lines in the
    spec const setup) are replaced with the passed parameters.
    The same param-snapshot pattern Option C introduced for
    filter now covers all three dimensioned settings, so the
    helper has no implicit dependency on class-member state for
    any of its cache axes.

Call site updates:

  - DrawBatchVertices: passes (m_texture_filtering, m_true_color,
    m_scaled_dithering).
  - precompile_sync PSO loop in CompilePipelines: same.
  - ShaderCompileThreadEntryPoint: snapshots m_true_color and
    m_scaled_dithering at worker entry alongside the existing
    filter loop. The worker is responsible only for the CURRENT
    (true_color, scaled_dithering) combo's seven filter sub-
    cubes - walking all 7 x 2 x 2 = 28 sub-cubes in the
    background would balloon cold-cache warm-up to several
    minutes, well past 'useful' into 'annoying'.

Behaviour after this patch:

  - Cycling back to a previously-visited (filter, true_color,
    scaled_dithering) tuple: instant. The slot is already filled,
    GetBatchPipeline returns it from a single atomic load.
  - First time toggling true_color or scaled_dithering in a
    session: 5-10s sync compile in Enabled mode for the current
    filter's sub-cube under the new value, then the worker warms
    the other six filter sub-cubes in the background. Lazy mode
    fault-ins on first draws as before.
  - Toggling filter under the new (true_color, scaled_dithering):
    same as Option C - instant on revisit, sync compile + back-
    ground warm-up on first visit.

After enough cycling through the three settings (typically a few
back-and-forth toggles of each), all reachable combinations end up
cached and every toggle becomes instant for the rest of the
session.

Memory cost during warm-up:
  - PSO cache atomic-slot count grows by 4x relative to pre-B,
    7x already happened in Option B for filter, this patch makes
    it 14x (true_color) and 28x (scaled_dithering). Slot array
    size: ~480 KiB total. Driver-side PSO storage only grows when
    slots are populated; a typical session touches maybe 10-20%
    of one (filter, true_color, scaled_dithering) combo's
    reachable cells.
  - On hardware lacking dual-source blend the *BinAlpha and other
    non-Nearest filters are clamped to Nearest by UpdateHWSettings
    and the worker skips them via TextureFilterRequiresDualSource-
    Blend, so the actual populated count is lower.

Smoke tests:
  - Tree-wide grep for m_true_color / m_scaled_dithering /
    only_filter_changed inside the helpers and their callers:
    every remaining hit is either a comment, a callsite that
    correctly snapshots the class member to pass downward, or
    elsewhere in the file outside the batch pipeline path
    (m_true_color / m_scaled_dithering still drive other Vulkan
    structural choices like resolution scale).
  - The two GetBatchPipeline helper signatures match in header
    and impl; no stale 7D declaration of m_batch_pipelines
    survives in the header.

Test priority:
  - With shader cache nuked, in Enabled mode, toggle the
    'scaled dithering' core option on then off then on. First
    toggle pays the usual 5-10s pause for the current filter
    sub-cube under the new dithering value. Second toggle back
    to the original should be INSTANT - the slot is already
    filled.
  - Same test with 'true colour'. Same behaviour expected.
  - Toggle both: scaled dithering off, true colour off, then
    scaled dithering on, true colour off, ... cycle through all
    four (true_color, scaled_dithering) combos. After one cycle,
    every transition is instant for the rest of the session.
  - Confirm non-dimensioned changes (resolution scale, MSAA,
    PGXP depth, etc.) still trigger the full
    DestroyPipelines + CompilePipelines pause - that path is
    unchanged and required for those settings to be applied
    correctly.
  - On hardware without dual-source blend: confirm the worker
    still walks only Nearest's sub-cube across the current
    (true_color, scaled_dithering) snapshot.
  - Long session (10+ minutes) of toggling the three dimensioned
    settings: no driver-side OOM. Watch GPU memory in nvidia-smi
    or Task Manager.

The set of dimensioned settings could be extended further to
pgxp_depth_buffer or other 2-value spec consts, but those toggle
much less frequently in practice and would only add another 2x
slot cost each. Defer until requested.

After this lands the Vulkan filter / dithering / true-colour
trio is functionally complete: every common user-toggled batch
shader setting is dimensioned, with current-combo background
warm-up keeping cold-cache cost bounded.
2026-05-18 03:41:44 +02:00
LibretroAdmin
8cfd9f7e5c core/gpu_hw_vulkan: background warm-up of other filter sub-cubes (Option C)
Option B made each filter's batch pipeline sub-cube persist across
filter swaps, so revisits to a previously-used filter became instant.
But the FIRST visit to any new filter still paid the synchronous
Enabled-mode precompile (5-10s on cold pipeline cache) or the first-
draw fault-in stutters (Lazy / Disabled). Option C extends the
existing background batch worker to also populate the OTHER 6 filter
sub-cubes, so after a one-time 30-60 second warm-up window the entire
filter matrix is cached and every swap is instant going forward.

Plumbing changes to make the worker filter-agnostic:

  - GetBatchPipeline / GetBatchFragmentShader now take an explicit
    GPUTextureFilter parameter rather than implicitly reading
    m_texture_filtering. The parameter routes both slot indexing
    (which sub-cube of m_batch_pipelines / m_batch_fragment_shaders)
    and SPIR-V blob selection (which embedded shader backs the
    pipeline). Main-thread draw callers pass m_texture_filtering;
    the worker passes whatever filter value it is currently
    warming.

  - All m_texture_filtering reads inside the two helpers replaced
    with the new parameter: the slot index, the dual_source
    decision in GetBatchFragmentShader, the binalpha derivation
    and the dual-source-blend trigger in GetBatchPipeline. This
    also resolves the residual issue from Option B where the
    helpers each made an independent read of m_texture_filtering -
    a write between the two could split slot index from blob
    selection. With the parameter the snapshot is taken once at
    the helper entry and used consistently.

  - All four call sites updated: the two precompile_sync loops
    (FS slice and PSO slice) pass m_texture_filtering (current
    filter only - Enabled's foreground work stays current-filter-
    focused), DrawBatchVertices passes m_texture_filtering (same),
    and the worker passes its loop iterator.

Worker loop changes:

  - Outermost filter loop added. Walks the 7 GPUTextureFilter
    values in current-first order:
        (current + f_offset) % Count   for f_offset = 0..6
    so the runloop's first draws (Lazy mode, where the current
    filter's sub-cube is also still cold) hit populated slots
    ASAP, and only then does the worker move on to populate the
    other six.

  - Filters that require dual-source blend are skipped on
    hardware that lacks the feature - UpdateHWSettings already
    clamps m_texture_filtering to Nearest in that case, so the
    BinAlpha and other dual-source-only sub-cubes would never be
    used at draw time and the PSOs would just sit unused. Skip
    via TextureFilterRequiresDualSourceBlend.

Mode dispatch in CompilePipelines:

  - Worker-start hoisted out of the Lazy block and into a unified
    'precompile_mode == Lazy || == Enabled' guard at the end of
    CompilePipelines. In Enabled mode the precompile_sync block
    above already populated the current filter's sub-cube
    synchronously (the user-visible 'Compiling Pipelines'
    progress bar is unchanged); the worker's first pass over
    that sub-cube hits the lock-free fast-return on every cell
    and then moves on to the other six. Disabled stays
    unstarted - the documented contract is no compile / no
    background compile.

Behaviour by mode after this lands:

  Enabled
    - Boot: same synchronous 'Compiling Pipelines' pause as
      before, just for the current filter's sub-cube.
    - Within ~30-60s after the runloop starts: all six other
      filter sub-cubes populated in the background at lowered
      thread priority, with quit-flag checked between cells.
    - Filter swap to ANY filter from that point: instant.
      Cycling xBR -> Nearest -> Bilinear -> JINC2 -> xBR is
      seven instant transitions with no progress bar, no
      vkCreateGraphicsPipelines call.
    - Cold-cache first launch still pays the per-filter driver
      compile cost on background fill; warm-cache subsequent
      launches see the worker breeze through.

  Lazy
    - Boot: same sub-second non-batch pre-fill, then worker
      starts. First draws may briefly lazy-fault if the runloop
      reaches them before the worker has the slot ready (same
      as today), but the gap closes within the current filter's
      sub-cube portion of the warm-up.
    - After ~30-60s: all 7 filter sub-cubes populated.
    - Filter swap: instant from the start (Option B), gains
      additional 'no background fill needed' headroom after C
      completes warm-up.

  Disabled
    - Unchanged. No worker, no background compile. First touch
      of any (filter, render_mode, texture_mode, ...) combo
      still faults in on the main thread - the documented
      Disabled contract.

Smoke tests:
  - g++ -fsyntax-only across touched TUs (clean).
  - Helper-internal grep for m_texture_filtering inside the two
    helpers - zero hits, the filter param is the sole source of
    truth.

Memory cost during warm-up:
  - Up to 7x the per-filter PSO + ShaderModule driver-side memory
    if the user's hardware supports all dual-source filters.
    On a desktop GPU each batch PSO is ~10-50 KiB of driver
    storage and there are ~1000 reachable cells per filter, so
    full population is on the order of 100-300 MiB worst case.
    Lower on hardware lacking dual-source blend (most
    *BinAlpha filters skipped).

Test priority:
  - Enabled mode, shader cache nuked, boot a game with filter
    set to Nearest. Wait ~60s of gameplay. Switch to xBR. The
    transition should now be instant (or close to it) on the
    FIRST visit too, because the worker has already populated
    xBR's sub-cube in the background. Repeat the cycle through
    all filters - all transitions instant.
  - Lazy mode, same test. The first 30s have the runloop and
    worker competing under PipelineCacheMutex; once the
    current filter's sub-cube is full the runloop runs stutter-
    free while the worker continues filling the other six.
  - On hardware without dual-source blend, verify that only
    Nearest's sub-cube is populated (other six entries in the
    filter index map to filters that are clamped to Nearest by
    UpdateHWSettings, so warming them would be pointless).
    Check via a debugger or by adding a one-shot log line in
    the worker's per-filter loop and counting the active
    iterations.
  - On a long session (10+ minutes), confirm no driver-side OOM
    from the 7x PSO storage. Watch GPU memory in nvidia-smi or
    Windows Task Manager Performance / GPU tab.
  - Confirm DestroyPipelines still works correctly: change
    resolution scale, MSAA, or any non-filter shader-affecting
    setting. The full 7x sub-cube destroy should complete
    quickly; CompilePipelines re-runs and the worker restarts
    to re-warm all 7 sub-cubes for the new settings.

After this patch the Vulkan filter-swap experience is functionally
complete. Next in the series: port B + C to the D3D12 and D3D11
backends, which have the same structural issue and a larger payoff
per swap (D3D paths still go through runtime shadergen + d3dcompiler
on every fault-in, so revisits save not just driver PSO compile
but also HLSL -> DXBC compile).
2026-05-18 03:17:43 +02:00
LibretroAdmin
32704a9b78 core, common/vulkan: add filter dim to batch pipeline cache (Option B)
Filter swap in the core options menu used to take 5-10 seconds on a
cold pipeline cache and 1-2 seconds even on a warm one, because every
filter change blew away the entire batch pipeline cache and rebuilt
it from scratch for the new filter. The cache (m_batch_pipelines,
m_batch_fragment_shaders) had no filter dimension - the active filter
just selected which pre-baked SPIR-V blob backed every PSO in the
single shared cache, so any change to it invalidated everything.

Add filter as the outermost dimension of both cache arrays:

  m_batch_pipelines : 6D -> 7D
    was [depth_test][render_mode][texture_mode][transparency_mode]
        [dithering][interlacing]
    now [filter][depth_test][render_mode][texture_mode]
        [transparency_mode][dithering][interlacing]

  m_batch_fragment_shaders : 4D -> 5D
    was [render_mode][texture_mode][dithering][interlacing]
    now [filter][render_mode][texture_mode][dithering][interlacing]

GetBatchPipeline / GetBatchFragmentShader prepend the current
m_texture_filtering as the first index. Each filter now has its own
sub-cube of slots; populating one does not touch any other. Filter
swap behaviour:

  - First visit to a filter            : same slow PSO compile as
                                         before (cold pipeline cache),
                                         or a couple of seconds with
                                         the driver cache warm.
  - Cycling back to a previous filter  : instant. The slot is already
                                         filled, GetBatchPipeline
                                         returns it from a single
                                         atomic load, no
                                         vkCreateGraphicsPipelines /
                                         vkCreateShaderModule call.

The skip-destroy decision is made by UpdateHWSettings, which now
emits an additional out-parameter only_filter_changed. The signature
keeps backwards compat for the D3D11 / D3D12 backends - the
parameter defaults to nullptr and they continue passing only the
existing two flags (their batch caches are not filter-dimensioned
yet, so a full DestroyPipelines + CompilePipelines is still
required there). Splitting the comparison:

  filter_diff      = (m_texture_filtering != g_settings.gpu_texture_filter)
  non_filter_diff  = any other shader-affecting setting changed
                     (resolution scale, MSAA, true colour, per-sample
                      shading, scaled dithering, UV limits, chroma
                      smoothing, downsample mode, PGXP depth, color
                      perspective, precompile mode)
  *shaders_changed         = filter_diff || non_filter_diff
  *only_filter_changed     = filter_diff && !non_filter_diff

GPU_HW_Vulkan::UpdateSettings routes accordingly:

  shaders_changed && !only_filter_changed
    -> DestroyPipelines + CompilePipelines (current full-flush
       behaviour, every sub-cube invalidated because per-session
       spec consts / structural SPIR-V choice apply to all of them)
  shaders_changed && only_filter_changed
    -> CompilePipelines only. The previous filter's sub-cube remains
       populated; the new filter's sub-cube is lazy-populated by the
       precompile_sync loop (Enabled mode), filled in by the
       background worker (Lazy), or left empty for draw-time fault-
       in (Disabled).

To make this safe under Lazy mode, CompilePipelines must tolerate
being called on a partially-populated cache. Two changes:

  - The batch VS bake loop at the top of CompilePipelines was
    unconditional create + store; it now load-checks each slot first
    and skips if populated, so re-entry on a filter-only change does
    not leak the existing module. The non-batch GetXxxPipeline
    helpers were already idempotent (each guards on slot !=
    VK_NULL_HANDLE at entry) and need no change.

  - Stop the background compile worker as the very first action in
    GPU_HW_Vulkan::UpdateSettings, BEFORE UpdateHWSettings writes
    m_texture_filtering. Without this, the worker could split its
    two reads of m_texture_filtering (one inside GetBatchPipeline
    for the slot index, one inside GetBatchFragmentShader for both
    the slot index AND the SPIR-V blob selection) across the
    write, installing a FS module compiled for one filter into a
    PSO slot indexed by another. Pre-B this race was benign
    because DestroyPipelines reliably swept up after the worker;
    with B the filter-only path skips DestroyPipelines, so a stale
    cross-wired slot would persist indefinitely.
    StopShaderCompileThread is idempotent so the existing call at
    the top of CompilePipelines is unchanged.

Smoke tests:
  - g++ -fsyntax-only across the touched TUs (clean).
  - Tree-wide grep on m_batch_pipelines / m_batch_fragment_shaders:
    only Vulkan-side hits change shape; D3D12 has its own
    same-named member at a different scope (gpu_hw_d3d12.h) which
    is unrelated and unaffected.

Memory impact:
  - m_batch_pipelines: 3*4*9*5*2*2 = 2160 slots -> 7x = 15120 slots
    (std::atomic<VkPipeline> = 8 bytes typical -> ~120 KiB total)
  - m_batch_fragment_shaders: 4*9*2*2 = 144 slots -> 7x = 1008 slots
    (std::atomic<VkShaderModule> = 8 bytes typical -> ~8 KiB total)
  - VkPipeline / VkShaderModule driver-side memory only grows when
    slots are populated. A typical session visits maybe 10-20% of
    one filter's reachable cells; visiting all 7 filters would
    grow driver-side pipeline storage by ~7x of that fraction. On
    a desktop GPU this is well under 100 MiB worst case.

Test priority:
  - With the shader cache nuked: boot a game in each precompile mode
    (Enabled / Lazy / Disabled). Verify nothing regressed.
  - In Enabled: change filter (xBR -> Nearest -> Bilinear -> xBR).
    First visit to each new filter should be the usual 5-10s
    progress-bar pause on a cold pipeline cache. Cycling back to
    xBR after visiting Nearest and Bilinear should now be INSTANT -
    no progress bar, no perceptible pause, no
    vkCreateGraphicsPipelines calls (the slots are already
    populated). On a warm pipeline cache (second run) the first
    visit drops to ~1-2s, the revisit stays instant.
  - In Lazy: same cycle. First visit pauses briefly for non-batch
    pre-fill then resumes gameplay with background fill in
    progress. Revisits are instant; background fill of the new
    filter's sub-cube continues to populate even while the user
    plays.
  - In Disabled: same cycle. First visit has the usual lazy fault-
    in stutters per (render_mode, texture_mode, ...) combo as the
    game touches new ones. Revisits are instant.
  - Confirm non-filter shader-affecting changes still trigger a
    full flush: change resolution scale, MSAA, true-colour toggle,
    PGXP depth toggle, etc. Should see the full
    DestroyPipelines + CompilePipelines pause regardless of mode.
  - Confirm settings-only changes (audio volume, save state slot,
    etc.) do not flush the cache. (UpdateSettings is gated on
    GPU-relevant settings in host_interface.cpp:285-298, so this
    should be safe by construction.)

Option C (background-precompile the OTHER filter sub-cubes after
Enabled's primary work finishes, so first-visit-to-new-filter is
ALSO instant) is the natural follow-up and now trivially builds on
this patch's filter-dimensioned cache. Will come as a separate
patch after B is validated.
2026-05-18 03:00:42 +02:00
LibretroAdmin
2523b0ddc8 dep, common/vulkan, core, build: remove dep/glslang and the Vulkan ShaderCompiler infrastructure
Final patch in the glslang-elimination series. After this lands the
runtime glslang library and the wrapper that drove it are completely
gone from the project on the Vulkan side; the only path that still
performs runtime shader compilation is the Windows-only D3D11 / D3D12
backend, which uses Microsoft's own d3dcompiler.dll and has no
glslang relationship.

Build fix: src/core/gpu_hw_vulkan.cpp:395 still called
Vulkan::ShaderCompiler::DeinitializeGlslang() from
DestroyResources(), which broke after the previous patch dropped
the transitive include of shader_compiler.h via shader_cache.h.
Since CompileShader() is unreachable from anywhere in the build
glslang's lazy InitializeGlslang() inside CompileShader never fires
either, so the matching Deinit was already a no-op. Drop the call.

Removed:
  - src/common/vulkan/shader_compiler.cpp + shader_compiler.h
    (the Vulkan glslang -> SPIR-V wrapper). The whole API
    (ShaderCompiler::Type, CompileShader, InitializeGlslang,
    DeinitializeGlslang, SPIRVCodeVector, SPIRVCodeType) had no
    callers after the patch-15 batch FS / VS conversion.
  - dep/glslang/ (~4.4 MiB, ~110 source files including the
    MachineIndependent parser, the Pp preprocessor, the
    SPIRV serialiser, the StandAlone resource limits, and the
    Windows / Unix OSDependent abstraction). Last consumer was
    src/common/vulkan/shader_compiler.cpp.
  - The stale 'shader_compiler.h' include in
    src/common/vulkan/util.cpp (never used anything from the
    header; kept compiling only because it pulled in transitively
    reachable types via shader_compiler -> types.h).
  - Makefile.libretro: GLSLANG_DIR variable, -I$(GLSLANG_DIR) entry
    in INCFLAGS, vulkan/shader_compiler.cpp from the SOURCES_CXX
    list, the entire 'dep/glslang' block (~43 source files), and
    the OS-specific OSDependent ifeq.
  - src/common/CMakeLists.txt: vulkan/shader_compiler.cpp and
    vulkan/shader_compiler.h from the common target, and glslang
    from the target_link_libraries(common ...) call.
  - dep/CMakeLists.txt: add_subdirectory(glslang).

Also refreshed a couple of stale glslang-mentioning comments in
gpu_hw_vulkan.cpp's GetBatchFragmentShader / GetBatchPipeline slow-
path documentation - the multi-thread reasoning is unchanged but it
is no longer 'glslang lock-free' since there is no glslang.

Smoke tests:
  - g++ -fsyntax-only on the Vulkan common TUs touched here.
  - Tree-wide grep for 'glslang' / 'GLSLANG' / 'ShaderCompiler' /
    'shader_compiler' across src/, dep/CMakeLists.txt, and
    Makefile.libretro: every remaining hit is either the D3D11
    backend's own (still-live) ShaderCompiler namespace, or
    historical comments in embedded_shaders.{h,cpp} /
    shader_cache.{h,cpp} that explain why the pre-baked path
    exists. The latter are documentation, not code dependencies.

Library size impact:
  - dep/glslang/ tree removal: ~101,000 lines of C++ vendored
    third-party code deleted from the repo.
  - Linked output: the Vulkan-side glslang+SPIRV code paths were
    contributing roughly 700-900 KiB to the stripped libretro
    core on x86_64 release builds (varies by toolchain). After
    this patch the Vulkan backend's binary contribution is just
    the embedded SPIR-V .inc payload (~2.5 MiB) plus the small
    EmbeddedShaders / ShaderCache / Util / etc. wrappers - a net
    reduction overall.

Test priority:
  - Routine smoke under all three precompile modes (Disabled / Lazy /
    Enabled). Nothing changes functionally vs the previous patch -
    the only behavioural delta is that vkCreateShaderModule no
    longer goes through Vulkan::ShaderCompiler. Visual output and
    perf characteristics should be byte-identical.
  - Build verification on each supported toolchain: MSYS2 MinGW
    on Windows, the macOS qb / xcodeproj path, Linux GCC and
    Clang, plus the libnx and emscripten paths if those are still
    being exercised. The Makefile and CMake changes are mechanical
    but worth a cold build on each.
  - On first launch after this lands, vulkan_pipelines.bin should
    continue to grow / persist correctly and the legacy 0-byte
    vulkan_shaders.bin / 1-byte vulkan_shaders.idx files from
    pre-patch-16 builds should already be gone (cleaned up by the
    patch-16 Open() sweep that landed last cycle).

This closes the glslang-elimination series. Subsequent patches in
this area can focus on the corresponding work for other backends -
the OpenGL backend's runtime glslang path is still live, and the
D3D11 / D3D12 backends still ship their own runtime d3dcompiler
flow - but those are separate efforts.
2026-05-18 02:21:53 +02:00
LibretroAdmin
559f88a963 common/vulkan: prune dead SPIR-V cache from ShaderCache
ShaderCache historically managed two things: the on-disk SPIR-V blob
cache (vulkan_shaders.bin + vulkan_shaders.idx) keyed by hash of
glslang-input GLSL, and the driver's VkPipelineCache
(vulkan_pipelines.bin). With every shader in the Vulkan backend now
pre-baked into the build at .inc-include time and instantiated via
Vulkan::EmbeddedShaders::CreateShaderModule, nothing in the codebase
calls ShaderCache::GetVertexShader / GetFragmentShader / GetShader-
Module / GetShaderSPV / CompileAndAddShaderSPV anymore - the SPIR-V
cache files are opened on every launch and never written to, leaving
a 0-byte vulkan_shaders.bin and a 1-byte vulkan_shaders.idx sitting
on disk forever.

Drop the SPIR-V cache half of the class. The header is reduced to a
thin VkPipelineCache wrapper:

  - ShaderCache::Create(base_path, debug)         [Open lifecycle]
  - ShaderCache::Destroy()                        [Open lifecycle]
  - ShaderCache::GetPipelineCache(set_dirty)      [used by gpbuilder]
  - ShaderCache::PipelineCacheMutex()             [serialises gpbuilder.Create]
  - ShaderCache::FlushPipelineCache()             [destructor + future explicit flush]

Open() additionally sweeps any leftover vulkan_shaders.bin / .idx
files from earlier builds so users with stale 0-byte caches end up
with a tidy system/swanstation/ directory automatically. The sweep
uses the same GetLegacyShaderCacheBaseFileName() naming the old
SPIR-V cache used, so legacy_debug variants are picked up too.

The version parameter to Create() and Open() is dropped: it only
ever influenced the SPIR-V cache file invalidation (and was also
folded into the file name via the version field), neither of which
exist anymore. The include of core/shader_cache_version.h from
gpu_hw_vulkan.cpp is also dropped on the Vulkan side; the constant
itself stays for the D3D11 and OpenGL backends which still maintain
their own runtime-glslang-driven SPIR-V / DXBC caches.

Removed from ShaderCache:
  - GetVertexShader / GetFragmentShader / GetShaderModule /
    GetShaderSPV / CompileAndAddShaderSPV (all called nothing now)
  - CacheIndexKey / CacheIndexData / CacheIndex / CacheIndexEntry
    types and the unordered_map storing them
  - m_index, m_index_file, m_blob_file, m_shader_cache_mutex,
    m_version, m_debug
  - GetCacheKey / GetShaderCacheBaseFileName / CreateNewShaderCache /
    ReadExistingShaderCache / CloseShaderCache
  - The shader_compiler.h include and the FILE_VERSION constant

Smoke tests:
  - g++ -fsyntax-only on the touched common TU.
  - The slim class compiles in isolation; embedded_shaders.cpp
    (which is the only other Vulkan-side common TU touched by
    recent patches) also still compiles cleanly.

Test priority:
  - Routine smoke under all three precompile modes. The first launch
    after this lands should delete the stale 0-byte vulkan_shaders
    .bin and 1-byte vulkan_shaders.idx automatically.
  - vulkan_pipelines.bin should continue to grow / persist correctly
    across runs (it is the only on-disk cache that matters now).
  - After this, src/common/vulkan/shader_compiler.{h,cpp} still
    exists but is unreachable from the Vulkan backend (only
    util.cpp's stale include keeps a transitive reference). The
    final infrastructure-removal patch in this series drops those
    files and dep/glslang/ alongside the Makefile.libretro and
    CMakeLists.txt entries that link them in.
2026-05-18 02:15:35 +02:00