Commit graph

24 commits

Author SHA1 Message Date
LibretroAdmin
cd971cd1d2 core/d3d12: pre-bake the VRAM copy pixel shader
First multi-variant pre-bake, exercising the TEMPLATE_VARIANTS
infrastructure in tools/regen_d3d12_dxbc.py for the first time.
vram_copy_ps is the smallest cleanly-pre-bakeable non-batch shader
after the e56d4d4 RESOLUTION_SCALE refactor took it from
PGXP x N variants down to just PGXP (2 blobs). Pure body-branch
variant axis - no texture binding changes, no MSAA split, no
input-layout differences. Body is small (~30 lines) and the
runtime call site is one function (GetVRAMCopyPipeline) in one
backend.

Why pre-bake vram_copy_ps:

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

Files added:

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

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

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

Files modified:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Files in this patch:

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

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

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

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

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

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

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

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

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

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

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

Files in this patch:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Files in this scaffold patch:

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

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

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

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

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

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

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

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

Subsequent patches in this arc will each:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

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

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

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

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

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke (any game).
  - shader_precompile=Disabled: any game that flips interlace_mode
    during gameplay (interlaced cutscenes -> progressive gameplay
    transitions) or that toggles 24bpp display mode (FMV intros in
    most JRPGs). Smooth-chroma path is exercised by enabling the
    Chroma Smoothing 24-bit option in the core options and watching
    an FMV. Worth doing a coarse SSAA round-trip too even though the
    display pipeline shouldn't be sensitive to it.
2026-05-18 00:02:50 +02:00
LibretroAdmin
30793791f8 common/vulkan, core: pre-bake VRAM write FS (SSBO + texbuf blob pair)
Patch 8 in the glslang-elimination series. VRAM write has a structural
variant the rest of Bucket A has not had to deal with: the descriptor
type at set=0 binding=0 differs depending on
m_use_ssbos_for_vram_writes (storage buffer vs uniform texel buffer),
so a single SPIR-V blob with a spec constant cannot collapse it. Two
blobs, otherwise sharing the same spec-constant interface.

Shader converted:

  VRAMWriteFragmentShader. Writes one upscaled PSX pixel per fragment
  from a host-staged 16-bit-per-pixel VRAM tile.

  Blobs:
    vram_write_ssbo_fs   - readonly restrict std430 SSBO ssbo_data[],
                           two PSX pixels packed per uint32 element.
    vram_write_texbuf_fs - uniform usamplerBuffer samp0 with R16_UINT
                           buffer view, one PSX pixel per element.

  Spec constants (identical between the two blobs):
    id = 0  RESOLUTION_SCALE (uint) - common-knob convention. Drives
                                      both the upscale-aware coord
                                      conversion (gl_FragCoord.xy
                                      divided by RESOLUTION_SCALE) and
                                      the VRAM_SIZE wrap-around math.
    id = 3  PGXP_DEPTH       (bool) - common-knob convention. Selects
                                      between sampled-alpha-derived
                                      depth and a fixed 1.0.

The depth_test pipeline knob remains a SetDepthState toggle, not a
spec constant; the C++ side keeps the 2-entry slot lookup in
m_vram_write_pipelines and selects the SSBO-vs-texbuf blob inside the
slot miss path. m_use_ssbos_for_vram_writes is a per-session decision
(set in CompilePipelines based on device features), so in any given
session only one of the two blobs will ever be touched at
vkCreateShaderModule time.

Inlined the RGBA5551ToRGBA8 helper directly into both .glsl files
rather than relying on what WriteCommonFunctions would have emitted -
keeps each blob self-contained and avoids dragging in the other
WriteCommonFunctions helpers (fixYCoord, RGBA8ToRGBA5551) that are not
used here. The Vulkan-side fixYCoord is the identity function in any
case.

Bindings unchanged from the shadergen path - both blobs target
m_vram_write_pipeline_layout, which is constructed with the matching
descriptor type at CreateLayouts time.

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

Like patches 6 and 7, VRAM write is exercised by game-driven VRAM
uploads, not by UpdateSettings, so this pipeline does not see the
SSAA-toggle lazy-build path. shader_precompile=Disabled is expected to
match Enabled / Lazy behaviour.

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke (any game).
  - shader_precompile=Disabled: any game with heavy texture streaming
    over CPU-side DMA - JRPG cutscenes, pre-rendered backgrounds,
    movie-frame VRAM uploads. The SSBO blob is the common one on
    desktop GPUs; the texbuf blob may be exercised by retesting on
    something with the SSBO path disabled.
2026-05-17 23:46:01 +02:00
LibretroAdmin
34cda08c7a common/vulkan, core: pre-bake VRAM copy FS (single blob, 2 spec consts)
Patch 7 in the glslang-elimination series. VRAM copy is the simplest
remaining shader from the original shadergen-driven set: the source-side
has no MSAA variant (the shadergen hard-codes msaa=false at the top of
GenerateVRAMCopyFragmentShader because Vulkan won't let us bind the
source texture to both an input sampler and a colour attachment), so
unlike VRAM read / update-depth we collapse to a single SPIR-V blob with
just two spec constants.

Shader converted:

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

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

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

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

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

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

Test priority:
  - shader_precompile=Enabled / Lazy: routine smoke (any game).
  - shader_precompile=Disabled: any game that performs frequent VRAM
    copies (Final Fantasy series, anything with prerendered backgrounds
    streamed into VRAM). SSAA toggle round-trip should be fine.
2026-05-17 22:57:44 +02:00
LibretroAdmin
af9c287efb common/vulkan, core: pre-bake VRAM fill FS (single blob, 8 specialisations)
Patch 6 in the glslang-elimination series. Demonstrates the
'multiple-spec-constants-collapse-source-variants' pattern: the original
shadergen produced four GLSL source strings (wrapped x interlaced) and
a per-session constructor-level PGXP_DEPTH bake, for up to eight cached
SPIR-V blobs on the disk-backed cache. This patch collapses all of that
into a single SPIR-V blob with three boolean specialisation constants.

Shader converted:

  VRAMFillFragmentShader. Writes u_fill_color to a destination rectangle
  in upscaled VRAM, optionally discarding pixels for interlace masking
  or out-of-bounds-wrapped fills.

  Spec constants used (documented in the .glsl):
    id =   3 PGXP_DEPTH  (bool, common-knob convention).
    id = 100 INTERLACED  (bool, shader-specific).
    id = 101 WRAPPED     (bool, shader-specific).

  Cross-shader id reuse note: id=100 is also FIRST_PASS in
  adaptive_downsample_mip.frag.glsl. SPIR-V spec scopes spec-constant
  ids per-stage, so reuse across different shaders is harmless. Each
  shader file documents its own ids at the top.

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

- GetVRAMFillPipeline: switched to embedded path. The 'wrapped' and
  'interlaced' uint8 inputs (each 0 or 1) now drive AddBool calls into
  the SpecConstants helper rather than picking a GLSL source string.
  PGXP_DEPTH is sourced from GPU_HW::m_pgxp_depth_buffer. m_shadergen
  null-check removed.

  Pipeline cache shape unchanged: m_vram_fill_pipelines[wrapped][interlaced]
  still gives a 2x2 grid of distinct VkPipeline handles, since
  specialisation is per-pipeline. (A future patch could fold this into
  one VkPipeline per visible PGXP_DEPTH state if pipeline-create cost
  becomes a measurable burden, but that's a different optimisation;
  current state matches the pre-patch pipeline count.)

Notes on spec-const evaluation:

- The dst_coords uvec2 conversion at the top of main() is always
  computed in the SPIR-V even when neither INTERLACED nor WRAPPED is
  taken. The driver's optimiser is expected to dead-code-eliminate it
  at vkCreateGraphicsPipelines time once both spec constants are bound
  to false; spirv-dis on the non-specialised blob shows the IR allows
  this. If a driver doesn't optimise it the cost is negligible (a few
  ALU ops per fragment).

- gl_FragDepth = PGXP_DEPTH ? 1.0 : u_fill_color.a translates to an
  OpSelect on a spec-constant condition. After specialisation the
  OpSelect collapses to one of its two operands; modern drivers do
  this fold during pipeline compile.

Cumulative state after this patch:

- 14 of ~25-30 Bucket A blob variants converted (counting this single
  blob with eight specialisations as one blob - the on-disk pipeline
  cache shape across the four wrapped x interlaced combos and two
  PGXP states is what matters operationally, and that remains 4-or-8
  distinct VkPipelines as before).
- Spec-constant infrastructure exercised with 3 booleans driving
  source-level variants - the densest specialisation use so far.
- First use of constant_id=3 (PGXP_DEPTH common knob).
- Remaining Bucket A: VRAM copy FS (MSAA pair + spec consts),
  VRAM write FS (SSBO vs uniform-buffer pair + spec consts),
  Display FS (the densest one).

Smoke tests in the sandbox:

- vram_fill_fs.inc reconstructs to SPIR-V that passes
  spirv-val --target-env vulkan1.0.
- spirv-dis confirms three SpecId decorations (3, 100, 101) on three
  separate OpSpecConstantFalse %bool entries.
- g++ -fsyntax-only on embedded_shaders.cpp is clean.

Smoke tests at user's end:

- VRAM fill is exercised heavily during VRAM-to-VRAM copies and
  initial framebuffer clears at game boot. Cold-boot with cache nuked
  at scales 1x and 4x, run any game past the first few frames.
  Wrong behaviour in the INTERLACED branch would show as missing
  every-other-line in interlaced video modes (most visible in some
  PSX RPG cutscenes). Wrong behaviour in the WRAPPED branch would
  cause incorrect VRAM-to-VRAM rectangles in games that DMA between
  overlapping regions.

- Toggle PGXP Depth Buffer on/off at runtime. This forces a
  pipeline destroy + rebuild with the new spec-constant value for
  PGXP_DEPTH (id=3). If the spec const isn't being re-applied on
  rebuild, the depth output stays at the old value across the
  toggle - visually that means correct-vs-broken depth ordering in
  PGXP-affected games (Tekken 3 is a known fixture for this).

- The four wrapped/interlaced combinations are exercised by
  oversized VRAM fills and by interlaced-video games respectively.
  At least one of each in normal gameplay should hit a code path
  that runs through every spec-const combination.
2026-05-17 22:51:37 +02:00
LibretroAdmin
bc2776687f common/vulkan, core: pre-bake adaptive composite + box-sample downsample FS
Patch 4 in the glslang-elimination series. Completes the downsample
subsystem: all four downsample pipeline build paths in
src/core/gpu_hw_vulkan.cpp now use embedded SPIR-V. This patch is the
first to plumb the common-knobs spec constant (RESOLUTION_SCALE at
constant_id=0), establishing the project-wide pattern for the
larger remaining utility shaders.

Shaders converted by this patch:

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

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

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

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

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

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

Cumulative state after this patch:

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

Smoke tests in the sandbox:

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

Smoke tests at user's end:

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

Infrastructure additions:

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

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

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

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

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

Shaders converted by this patch:

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

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

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

Other changes in gpu_hw_vulkan.cpp:

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

Cumulative state after this patch:

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

Smoke tests in the sandbox:

- Both new .inc files reconstruct to SPIR-V that passes
  spirv-val --target-env vulkan1.0.
- spirv-dis on the mip FS confirms the OpDecorate SpecId 100 +
  OpSpecConstantFalse pair.
- g++ -fsyntax-only on both modified TUs (builders.cpp,
  embedded_shaders.cpp) is clean; the only warnings are two
  pre-existing unused-parameter warnings in SamplerBuilder::Create
  and BufferViewBuilder::Create, unchanged by this patch.

Smoke tests at user's end:

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

Shaders converted by this patch:

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

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

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

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

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

Cumulative state after this patch:

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

Smoke tests in the sandbox:

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

Smoke tests at user's end:

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

What this patch does:

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

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

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

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

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

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

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

What this patch does NOT do:

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

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

Smoke tests performed in isolation:

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

Smoke tests that require the user's setup:

- Real build on Windows (RTX 5090) and macOS.
- Vulkan core boots, RetroArch's pipeline cache populates, and at
  least one game renders past the first frame using the screen-quad
  pipeline (VRAM ops, display, downsample - all of which call
  GetFullscreenQuadVertexShader internally).
2026-05-17 20:09:57 +02:00
DarthMew
4335a2ad7e Reduce the amount of files in the repo & rename 'duckstation-libretro' to 'libretro' 2022-02-21 23:16:22 +01:00
Connor McLaughlin
f393ea618e Shaders/Cccalibrator: Fix compile errors 2020-12-23 01:39:49 +10:00
Connor McLaughlin
7ca0d357cb shaders/cccalibrator: Fix GLSL compile error 2020-12-19 17:02:01 +10:00
PavelDurov1488
9310a736c4
Cccalibrator
Its not complete, i need to add dynamic noise and blur and make saturation tunable
2020-12-15 00:21:42 +08:00
Connor McLaughlin
82f00237af Data: Add simple-brightness and simple-gamma shaders 2020-10-14 00:48:48 +10:00
Connor McLaughlin
6ababf7e53 Add simple shaders and some ported from DolphinFX 2020-09-13 20:32:34 +10:00