Commit graph

6,069 commits

Author SHA1 Message Date
LibretroAdmin
e64fc28a7f core: route RESOLUTION_SCALE through display_ps cbuffer
Last shader in the non-batch RESOLUTION_SCALE arc. Same refactor
as e56d4d4 / 9d2b49d / 2980961, applied to display_ps - the
fragment shader that composites the upscaled VRAM texture into
the host display framebuffer every frame.

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

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

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

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

Files:

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

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

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

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

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

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

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

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

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

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

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

Files:

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

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

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

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

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

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

vram_write_ps body references both RESOLUTION_SCALE and VRAM_SIZE
directly:

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

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

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

Files:

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

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

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

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

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

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

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

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

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

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

Helper plumbing:

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

Shader change in this commit:

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

Runtime change in this commit:

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

Compatibility notes:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Next in the arc: the RESOLUTION_SCALE cbuffer refactor for non-
batch shaders (lets non-batch HLSL read u_resolution_scale from
the cbuffer instead of having m_resolution_scale baked compile-
time via WriteCommonFunctions's batch_uniform_buffer=false path).
After that lands, vram_copy / vram_update_depth / vram_fill /
vram_read all become single-variant or near-single-variant pre-
bake targets.
2026-05-18 20:44:24 +02:00
LibretroAdmin
443a2a130b core/d3d12: pre-bake the copy/blit pixel shader
Next single-variant non-batch shader in the D3D12 pre-bake arc,
on top of fullscreen_quad_vs (dd54a73 + 77b255f). Picks the
copy/blit fragment shader (GenerateCopyFragmentShader -> bound
in GetCopyPipeline, used for full-frame blits during display
presentation / downscaling).

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

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

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

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

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

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

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

Files in this patch:

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

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

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

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

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

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

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

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

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

Next in the arc: a util.h cleanup adding a
SetVertexShader/SetPixelShader(D3D12_SHADER_BYTECODE&) overload
so the .pShaderBytecode + static_cast<uint32_t>(.BytecodeLength)
boilerplate disappears from call sites. After that, the
RESOLUTION_SCALE cbuffer refactor for non-batch shaders opens
up vram_copy / vram_update_depth / vram_fill / vram_read - the
last single-variant or near-single-variant non-batch shaders
before the batch matrix takes the rest of the arc.
2026-05-18 20:22:17 +02:00
LibretroAdmin
77b255fb7b core/d3d12: add pre-baked DXBC for fullscreen-quad VS
Build fix for dd54a73. That commit flipped GPU_HW_D3D12 onto an
embedded DXBC blob included from
src/common/d3d12/embedded_dxbc/fullscreen_quad_vs.inc, but the
.inc itself wasn't in the patch - the apply instructions told
you to run tools/regen_d3d12_dxbc.py + git commit --amend. That
contract was wrong of me to ship: if the regen step is skipped
or fxc.exe isn't available on the applier's host, master ships
with a missing header reference and every D3D12 build fails:

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

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

Generation method (for reproducibility):

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

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

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

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

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

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

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

Test: build under USE_D3D12=1 succeeds (no missing header).
Runtime: cold boot with shader_precompile_mode = Enabled walks
the precompile loop without compiling the fullscreen-quad VS
(it's pre-baked); every non-batch PSO that binds the embedded
VS - vram_fill, vram_copy, vram_write, vram_update_depth,
vram_readback, display, copy/blit - renders correctly.
2026-05-18 20:08:19 +02:00
LibretroAdmin
dd54a73809 core/d3d12: pre-bake the fullscreen-quad vertex shader
First end-to-end pre-bake migration off the runtime D3DCompile
path, on top of the scaffolding from ca8c9b0. Picks the smallest
possible target - a single-variant vertex shader with zero state
dependency, used by every non-batch pipeline (vram_fill /
vram_copy / vram_write / vram_update_depth / vram_readback /
display / copy/blit) - to validate the full pipeline before
larger migrations land.

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

Files in this patch:

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

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

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

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

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

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

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

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

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

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

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

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

Next patch in the arc: extend to non-batch pixel shaders that
don't bake state. vram_copy_ps and vram_update_depth_ps are
the next candidates - both are single-variant and have no
RESOLUTION_SCALE dependency (they sample VRAM coordinates
directly, no box-filter loop). After those, vram_fill_ps (two
variants: wrapped/unwrapped, interlaced/not - 4 total - so
TEMPLATE_VARIANTS gets its first non-empty entry). vram_read
and the batch matrix come later, after we have a cbuffer-
routing migration that lets RESOLUTION_SCALE be runtime-set
in non-batch shaders too.
2026-05-18 19:40:13 +02:00
LibretroAdmin
b833b43414 core/opengl: restore lost GetBatchProgram declarator line
Fixup for 5490cdf. When the precompile_sync block was extracted
out of CompilePrograms into the new PrecompileBatchPrograms
helper, the str_replace boundary used to insert the helper
definition above GetBatchProgram accidentally chewed the
GetBatchProgram function's signature line:

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

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

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

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

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

Test: build under USE_D3D12=1 / USE_OPENGL=1 succeeds; runtime is
unchanged from what 5490cdf intended (PrecompileBatchPrograms
still present and used, GetBatchProgram still has the same
signature and behaviour it had immediately before 5490cdf landed).
2026-05-18 09:18:03 +02:00
LibretroAdmin
ca8c9b05e8 core/d3d12: scaffolding for pre-baked DXBC blobs
First step of the D3D12 pre-baked DXBC arc that was originally
flagged in 7b575a3's commit message as the natural follow-up to
the cbuffer / dim cache / chroma-split / non-batch-skip
optimisation series. Sets up the directory layout, regen tool,
and namespace scaffolding that subsequent patches will populate
with individual shader migrations.

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

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

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

Files in this scaffold patch:

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

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

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

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

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

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

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

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

Subsequent patches in this arc will each:

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

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

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

Test:
  - python3 tools/regen_d3d12_dxbc.py with no shader sources
    exits 0 with 'nothing to do' message. No fxc.exe required.
  - Build under USE_D3D12=1: embedded_shaders.cpp compiles
    cleanly as an empty namespace TU. No new symbols emitted.
  - Build under USE_D3D12=0: embedded_shaders.cpp is excluded
    from the build per the existing if(USE_D3D12) gate in
    src/common/CMakeLists.txt and the ifeq guard in
    Makefile.libretro.
  - Runtime: no behavior change. D3D12 backend continues to
    compile every shader through the existing D3DCompile path.
2026-05-18 09:14:52 +02:00
LibretroAdmin
ccfa88fc4a core/d3d11: skip non-batch shader rebuild on filter-only flip
D3D11 parity port of the non-batch-skip optimisation that
landed for OpenGL in 5490cdf. Same motivation: the dim cache
port from 00cf11f made the only_dim_changed fast path in
UpdateSettings preserve the previous filter's sub-cube of
m_batch_pixel_shaders, but it still called CompileShaders
unconditionally - which in turn rebuilt every non-batch pixel
shader (copy / vram_fill / vram_read / vram_write /
vram_update_depth / display via RebuildDisplayPixelShaders /
downsample) plus the vertex shaders and the input layout. None
of those depend on m_texture_filter inside shadergen:
GenerateBatchFragmentShader and the WriteBatchTextureFilter
helper at gpu_hw_shadergen.cpp:706, 728, 839 are the only places
m_texture_filter is read, and they only run on the batch FS
generation path. Every other shadergen entry point ignores the
filter setting entirely.

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

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

  bool GPU_HW_D3D11::PrecompileBatchShaders(
      ShaderCompileProgressTracker& progress);

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

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

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

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

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

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

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

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

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

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

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

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

  bool GPU_HW_OpenGL::PrecompileBatchPrograms(
      ShaderCompileProgressTracker& progress);

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

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

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

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

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

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

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

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

The two dim caches are structurally different:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UpdateSettings's rebuild gate gains the only_dim_changed branch:

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

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

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

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

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

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

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

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

  display_only_source_changed (default nullptr, opt-in for callers)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  shader_source_changed (default nullptr, opt-in for callers)

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

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

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

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

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

Same shape on D3D11 and OpenGL.

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

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

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

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

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

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

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

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

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

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

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

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

Mechanically:

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

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

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

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

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

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

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

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

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

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

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

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

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

The libretro spec also gives us SET_GEOMETRY:

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

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

Decouple base from max in GetSystemAVInfo:

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

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

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

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

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

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

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

Three changes that work together:

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

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

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

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

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

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

Extend the same trick to both:

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

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

API changes:

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

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

Call site updates:

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

Behaviour after this patch:

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

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

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

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

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

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

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

Plumbing changes to make the worker filter-agnostic:

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

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

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

Worker loop changes:

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

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

Mode dispatch in CompilePipelines:

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

Behaviour by mode after this lands:

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

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

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

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

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

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

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

Add filter as the outermost dimension of both cache arrays:

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

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

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

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

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

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

GPU_HW_Vulkan::UpdateSettings routes accordingly:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Test priority:
  - Routine smoke under all three precompile modes. The first launch
    after this lands should delete the stale 0-byte vulkan_shaders
    .bin and 1-byte vulkan_shaders.idx automatically.
  - vulkan_pipelines.bin should continue to grow / persist correctly
    across runs (it is the only on-disk cache that matters now).
  - After this, src/common/vulkan/shader_compiler.{h,cpp} still
    exists but is unreachable from the Vulkan backend (only
    util.cpp's stale include keeps a transitive reference). The
    final infrastructure-removal patch in this series drops those
    files and dep/glslang/ alongside the Makefile.libretro and
    CMakeLists.txt entries that link them in.
2026-05-18 02:15:35 +02:00
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
427317c2ff core: pre-build hot-path utility pipelines in shader_precompile=Disabled
Three Vulkan utility pipelines are exercised inside UpdateSettings
when a setting change triggers framebuffer recreation (antialiasing
toggle - MSAA, SSAA, per-sample shading - resolution-scale change,
or any other setting that flips m_multisamples / m_resolution_scale
and forces a fresh m_vram_render_pass / m_vram_texture pair):

  - m_vram_readback_pipeline    (ReadVRAM before the cmdbuf drain).
  - m_vram_write_pipelines[0/1] (UpdateVRAM after the new framebuffer
                                 is in place).
  - m_vram_update_depth_pipeline (UpdateDepthBufferFromMaskBit
                                  directly after UpdateVRAM).

Lazy-building any of these inside their respective UpdateSettings
cmdbufs hits an NVIDIA driver quirk that produces corrupted
rendering on the frame immediately after the toggle: on the second
toggle cycle, HUD and background tiles render as blocky / wrong-
colour blocks while the foreground geometry (drawn through the
already-cached batch pipelines) looks correct. shader_precompile
modes Enabled and Lazy already pre-build the full non-batch
pipeline set as part of their startup sequence, so neither hits
this path; only Disabled was deferring these pipelines until the
in-cmdbuf lazy fault.

Fix: in the Disabled branch of CompilePipelines, do a targeted pre-
build of just the three pipelines above (plus the fullscreen-quad
VS they all share). The rest of Disabled's contract is preserved:
batch pipelines stay truly lazy (which is where the bulk of the
init-time cost actually lives), and the other utility pipelines
(VRAM fill / copy / display / downsample / readback for non-toggle
paths) remain lazy too because they are only ever exercised on
game-driven draws - well after the UpdateSettings cmdbuf has been
submitted and a fresh cmdbuf is recording.

Cost: four extra vkCreateGraphicsPipelines calls on the main thread
at boot and on each subsequent setting-change rebuild. Well under
50 ms in aggregate on a desktop GPU; invisible to the user.

Note that this lands as a separate behavioural fix BEFORE the next
embedded-SPIR-V conversion patch in the series (VRAM readback). The
fix is correct on the current shadergen-driven implementation of all
three pipelines too - the NVIDIA quirk is independent of the SPIR-V
source path, and Disabled-mode toggling was already a fragile path
under the original code. Verified by inspection that the existing
Lazy branch comments call out the equivalent pre-fill as 'mandatory,
not an optimisation' for an unrelated reason
(PipelineCacheMutex / std::mutex FIFO interaction with the batch
worker thread); the comment block above the precompile_sync
non-batch loop is updated accordingly.

Smoke tests:
  - g++ -fsyntax-only on src/core/gpu_hw_vulkan.cpp.

Test priority:
  - shader_precompile=Disabled, Tekken 2 (or any game with a
    discernible 2D HUD over 3D geometry): toggle SSAA None -> 8x ->
    None -> 8x, MSAA 1 -> 4 -> 1 -> 4, resolution scale 1 -> 2 -> 1
    -> 2. Each toggle should produce a clean frame on the very next
    draw, with no HUD or background corruption.
  - shader_precompile=Enabled / Lazy: regression check. These paths
    are unchanged structurally (the new code is gated behind the
    Disabled branch of the if/else if/else if ladder); behaviour
    should be byte-identical to before.
2026-05-18 00:30:49 +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
LibretroAdmin
f5feac555a core, common/d3d12: revive on-disk PSO cache via ID3D12PipelineLibrary
# Background

Commit 81e3a2b8 dropped the per-PSO disk cache because the previous
implementation had the wrong granularity - each cached PSO blob
carried its own copy of the embedded VS+PS bytecode, producing
~75 MB on disk for a fully populated session with no meaningful
cold-start benefit. The comment in shader_cache.cpp explicitly
flagged ID3D12PipelineLibrary (D3D12.1, Windows 10 v1703+) as the
right replacement: a single driver-managed blob that internally
dedupes shader bytecode across PSOs, comparable in size to the
existing ~5 MB shader bytecode cache.

This change wires up that path. It does NOT undo the 81e3a2b8 drop -
the per-PSO blob cache stays gated off via m_use_pipeline_cache and
its scrub-on-Open logic stays in place. The pipeline library is a
parallel, independently-gated mechanism behind m_use_pipeline_library.

# Runtime detection

ShaderCache::Open now takes an ID3D12Device*. Inside Open we
QueryInterface for ID3D12Device1 - present on Windows 10 v1703+
(April 2017). On success, m_use_pipeline_library is set true and
the library path is used. On a pre-D3D12.1 device or any failure,
m_use_pipeline_library stays false and behaviour reverts to the
current (post-81e3a2b8) "no PSO cache" path.

# Behaviour change

For a session on D3D12.1-capable hardware:

  GetPipelineState first calls LoadGraphicsPipeline on the library
  with a deterministic 32-wchar hex name derived from the existing
  CacheIndexKey hash. On hit: returns a PSO in microseconds, no
  D3DCompile, no CreateGraphicsPipelineState. On miss: compiles
  fresh via CreateGraphicsPipelineState (same as today), then
  best-effort StorePipeline. StorePipeline returning E_INVALIDARG
  (race loser, name already exists) is expected and ignored.

  At session end, ~ShaderCache calls GetSerializedSize +
  Serialize and writes atomically (.tmp + filestream_rename) to
  d3d12_pipeline_library_sm50.bin. Library invalidation on driver
  update returns D3D12_ERROR_DRIVER_VERSION_MISMATCH from
  CreatePipelineLibrary at the next Open; we delete the file and
  recreate empty.

# Effect

Per CompilePipelines walk through the batch matrix (after the
reachability skip from 19b118b):

  Enabled mode cold start: ~1-2 s (was ~52 s on dual-source
    hardware). The library load + ~1040 LoadGraphicsPipeline hits
    replace ~1040 CreateGraphicsPipelineState calls.
  Lazy mode: worker walk completes ~50 s sooner. User-invisible
    (already playing) but pollutes m_batch_pipeline_states less
    quickly.
  Disabled mode: first-draw stutter eliminated. Every cell's
    first fault was a ~50 ms CreateGraphicsPipelineState compile;
    becomes a microsecond LoadGraphicsPipeline. Disabled becomes
    "instant startup, no stutter, no upfront cost" - now a viable
    default for users who want minimum disk usage and quickest
    in-game-arrival.

Disk footprint: comparable to the existing ~5 MB shader bytecode
cache; not the 75 MB the dropped per-PSO cache reached. The library
internally shares VS+PS bytecode across PSOs that reference the
same shader source, which is most of the batch matrix.

# Lock-free property preserved

ID3D12PipelineLibrary methods (LoadGraphicsPipeline, StorePipeline,
Serialize) are documented thread-safe by Microsoft. No cache mutex
is held across either Load or Create, so the parallel-compile
property from 75e8269 (the D3D12 lazy non-batch PSO commit) and
the broader f46be3e-family lock-free-compile pattern continue to
hold. The Lazy worker can race the main thread on any (name,
desc) cell and the library's own race-loser handling on
StorePipeline is correct.

# mingw-w64 header compatibility

The MinGW-w64 d3d12.h ships only the CreatePipelineLibrary method
signature on ID3D12Device1 - the ID3D12PipelineLibrary interface
itself, plus D3D12_ERROR_DRIVER_VERSION_MISMATCH and
D3D12_ERROR_ADAPTER_NOT_FOUND, are missing. New header
src/common/d3d12/pipeline_library_compat.h provides them under
the standard __ID3D12PipelineLibrary_INTERFACE_DEFINED__ guard,
matching the published Microsoft spec. On toolchains that DO have
the full d3d12.h declarations the guard fires and our forward
declaration is skipped; on toolchains that don't (mingw-w64, some
older SDKs) our declaration provides the interface. Same pattern
Wine and various cross-compiled D3D12 projects use.

The IID and method layout have been stable since the D3D12.1
release. The interface inherits from ID3D12DeviceChild (which
inherits ID3D12Object, which inherits IUnknown), so the COM
vtable slots line up with what runtime objects expose.

# Files

  - src/common/d3d12/pipeline_library_compat.h: NEW. Forward
    declares ID3D12PipelineLibrary plus the two error HRESULTs
    that drive the cache-invalidation path, with the standard
    SDK guard so we no-op when present.
  - src/common/d3d12/shader_cache.h: include the compat header,
    add m_pipeline_library / m_pipeline_library_blob /
    m_use_pipeline_library members, declare the three new
    helpers (TryOpenPipelineLibrary, SerializePipelineLibrary,
    GetPipelineLibraryFilename, FormatPipelineLibraryName), and
    update the Open signature to accept an ID3D12Device*.
  - src/common/d3d12/shader_cache.cpp: implement detection in
    Open, library-fast-path in GetPipelineState, and
    Serialize-on-destroy. The legacy m_use_pipeline_cache path
    (currently always false) is untouched.
  - src/core/gpu_hw_d3d12.cpp: pass g_d3d12_context->GetDevice()
    to the Open call.
2026-05-15 06:18:13 +02:00
LibretroAdmin
19b118b9ae core, common/d3d11,d3d12,vulkan: skip unreachable cells in batch matrices
The 4 x 9 x 2 x 2 batch shader matrix (and the larger D3D12 / Vulkan
PSO matrices that extend it with depth_test and transparency_mode)
have structurally unreachable cells that the precompile loop and
background-compile worker were burning shader-compiler time on
anyway. Add IsBatchShaderReachable to gpu_hw.h and consult it from
all walk sites in all three backends.

# Three classes of unreachable cell

  - Texture modes 3 (Reserved_Direct16Bit) and 7
    (Reserved_RawDirect16Bit). GetBatchPixelShader's lookup_mode
    dedup aliases these to canonical modes 2 and 6, so the slot
    fills lazily on first runtime fault via a ComPtr copy. No
    compile required.

  - OnlyOpaque / OnlyTransparent with texture_mode == Disabled.
    NeedsTwoPassRendering returns false for untextured polys, so
    FlushRender never selects the two-pass fallback path for them.
    Unique source compiles on all hardware.

  - TransparentAndOpaque with texture_mode != Disabled, on hardware
    without dual-source blend. NeedsTwoPassRendering returns true
    in that combination, so the single-pass dual-source render
    mode is never selected. Unique source compiles, but only on
    non-dual-source hardware.

# Effect

On hardware that supports dual-source blend (modern desktop /
most discrete GPUs):

  - D3D11 shader matrix: 104/144 cells reachable, 40 skipped.
    ~7% cold-compile time saved in Enabled mode (most of the
    saved cells were cache-hits anyway).
  - D3D12 shader matrix: same. PSO matrix: 1040/1440 cells
    reachable (28% saved, ~400 cells). With ~50 ms per PSO
    that's roughly 20 s saved on cold compile.
  - Vulkan shader matrix: same. PSO matrix: 1560/2160
    reachable (28% saved, ~600 cells). ~30 s saved on cold
    compile.

On hardware WITHOUT dual-source blend (some integrated /
legacy parts):

  - D3D11: 80/144 cells (44% saved), ~29% cold-compile reduction.
  - D3D12: PSO matrix 800/1440 (44%), ~33 s saved.
  - Vulkan: PSO matrix 1200/2160 (44%), ~48 s saved.

Zero runtime cost. We're not changing what gets drawn, just not
pre-compiling shaders that wouldn't be drawn. The runtime fault
paths through GetBatchPixelShader / GetBatchPipeline (slow-path
mutex publish, dup_slot aliasing for reserved texture modes)
are unchanged and handle the first draw to a skipped cell
correctly without falling over - the dedup just kicks in via
the canonical slot on first fault, exactly as it would for any
other unfilled slot in Disabled mode.

The depth_test and transparency_mode axes don't affect
reachability (NeedsTwoPassRendering doesn't depend on them), so
the PSO progress unit count is just
CountReachableBatchShaders * <depth_test_count> * 5.

# What's NOT changed

  - GetBatchPixelShader / GetBatchFragmentShader / GetBatchPipeline
    themselves. The lookup_mode dedup and the slow-path publish
    step still handle reserved texture modes and unforeseen
    runtime cell selections correctly.
  - Matrix storage layouts. The arrays stay dense at their full
    sizes - skipping cells leaves them null, which is harmless.
  - Non-batch shaders. The non-batch upfront ticks still apply.

# Files touched

  - src/core/gpu_hw.h: IsBatchShaderReachable +
    CountReachableBatchShaders helpers (static, inline, no
    per-instance state).
  - src/core/gpu_hw_d3d11.cpp: skip in ShaderCompileThreadEntryPoint
    worker loop and in the precompile_sync block in CompileShaders.
  - src/core/gpu_hw_d3d12.cpp: skip in CompilePipelines (shader
    precompile + PSO precompile) and in
    ShaderCompileThreadEntryPoint.
  - src/core/gpu_hw_vulkan.cpp: skip in CompilePipelines (shader-
    module precompile + PSO precompile) and in
    ShaderCompileThreadEntryPoint.

Progress unit counts now use CountReachableBatchShaders so the
Enabled progress bar lands at exactly the compile count for the
running hardware.
2026-05-15 05:21:19 +02:00
LibretroAdmin
5d8a12acea core, common/d3d11: lock-free shader compile in lazy batch path
Symmetry pass for the D3D11 backend matching the D3D12 / Vulkan
fixes. The same Lazy-mode lockup pattern was reproducible: the
worker held m_batch_shader_mutex across the slow D3DCompile, and
the main thread's first DrawBatchVertices stalled on the mutex
for the duration of one shader compile - in practice locking up
the runloop before the BIOS screen as the worker walked the
144-entry batch matrix on its first run.

# What changed

  * common/d3d11/shader_cache: D3D11::ShaderCache is now thread-
    safe. One internal mutex (m_shader_cache_mutex) covers the
    DXBC index (unordered_map), the blob file, and the index
    file. NOT held across ShaderCompiler::CompileShader, so two
    threads compiling different shaders run truly in parallel.
    Two threads racing to compile the same shader is harmless:
    D3DCompile is deterministic on identical source so both
    produce identical DXBC; the double-check under the lock
    picks whichever publishes first and the loser's DXBC is
    discarded.

    Unlike D3D12 / Vulkan, no pipeline-cache analog is needed -
    D3D11 binds individual state objects rather than monolithic
    PSOs, and ID3D11Device::CreatePixelShader is documented
    free-threaded by Microsoft.

  * core/gpu_hw_d3d11: GetBatchPixelShader restructured. Shadergen
    + m_shader_cache.GetPixelShader (D3DCompile HLSL -> DXBC +
    CreatePixelShader) now run WITHOUT m_batch_shader_mutex held.
    The mutex is taken BRIEFLY at the end for the publish step -
    a slot.store + race-loser detection under double-check.
    Window measured in microseconds instead of the previous
    50-200 ms per compile.

    Race-loser handling: two threads racing to compile the same
    slot both produce equivalent ID3D11PixelShader objects. The
    loser's ComPtr falls out of scope at function exit and
    releases its COM reference automatically; the winner's
    canonical_slot ownership is the one published.

# Behaviour summary

Same shape as the D3D12 / Vulkan commits:

  * Disabled: CompileShaders builds the ~20 non-batch shaders
              upfront (D3D11 has no lazy non-batch path). Batch
              matrix stays empty; faulted on first draw. ~200 KB
              cache footprint after a session.
  * Lazy:     CompileShaders builds the ~20 non-batch shaders
              upfront, spawns the worker to walk the 144-entry
              batch matrix in the background. Gameplay starts
              instantly; worker fills slots opportunistically.
              ~5 MB cache footprint when worker finishes.

              Pre-fix this mode locked up before BIOS as the
              worker held the compile mutex across each ~50-200 ms
              D3DCompile call and the runloop starved on its first
              DrawBatchVertices.
  * Enabled:  CompileShaders builds every shader upfront. ~60 s
              first-run cold compile (D3DCompile is slow on the
              heavier shaders); near-instant warm-cache runs
              afterward. ~5 MB cache footprint immediately.

# Files touched

  * src/common/d3d11/shader_cache.h: include <mutex>, add
    m_shader_cache_mutex.
  * src/common/d3d11/shader_cache.cpp: scope-narrow
    m_shader_cache_mutex around index + file I/O in GetShaderBlob;
    add the double-check + publish pattern to
    CompileAndAddShaderBlob.
  * src/core/gpu_hw_d3d11.cpp: move shadergen + compile out
    before the publish-only m_batch_shader_mutex window in
    GetBatchPixelShader; update worker-entrypoint comment to
    reflect the new mutex semantics.
  * src/core/gpu_hw_d3d11.h: update the two-level-sync comment on
    GetBatchPixelShader and the storage-mutex comment on
    m_batch_pixel_shaders to describe the publish-only window.
2026-05-15 04:40:56 +02:00
LibretroAdmin
65d02fd1cc core, common/vulkan: lazy-fault non-batch PSOs
Symmetry pass for the Vulkan backend matching the D3D12 fix.

# What changed

Three threads now share the lazy-fault PSO matrix without
serialising on a single compile mutex:

  * common/vulkan/shader_cache: ShaderCache is now thread-safe.
    Two internal mutexes:
      - m_shader_cache_mutex covers the SPIR-V index
        (unordered_map), the blob file, and the index file. NOT
        held across ShaderCompiler::CompileShader, so two threads
        compiling different shaders run truly in parallel. Two
        threads compiling the SAME shader both run glslang; the
        double-check under the lock picks whichever publishes
        first and the loser's SPIR-V is discarded.
      - m_pipeline_cache_mutex serialises the VkPipelineCache.
        Per Vulkan 1.0 spec, the pipelineCache parameter to
        vkCreateGraphicsPipelines is in the host-synchronised
        parameter list - the application must guarantee no
        concurrent use of the same VkPipelineCache. Exposed via
        PipelineCacheMutex() for callers that need to honour the
        contract.

  * core/gpu_hw_vulkan: non-batch pipelines (VRAM fill / copy /
    write / update depth / readback, display, downsample) and
    their shared fullscreen-quad / UV-quad vertex shaders are now
    lazy-fault helpers, same shape as the D3D12 backend's
    GetVRAMFillPipeline / GetDisplayPipeline / etc. Each helper
    runs glslang lock-free, takes PipelineCacheMutex briefly
    around vkCreateGraphicsPipelines, and writes the slot.
    Twelve sites that bound the raw member pipelines via
    vkCmdBindPipeline now go through the helpers.

  * core/gpu_hw_vulkan: the batch helpers (GetBatchFragmentShader,
    GetBatchPipeline) no longer hold m_batch_shader_mutex across
    the slow operations. Each runs glslang + vkCreateShaderModule
    lock-free, takes PipelineCacheMutex around the
    vkCreateGraphicsPipelines call, then takes m_batch_shader_mutex
    BRIEFLY to publish under a double-check - the mutex is now a
    publish-step lock only, scoped to a slot.store + race-loser
    vkDestroy*. Window measured in microseconds instead of the
    previous hundreds of milliseconds.

# Why this matters (Vulkan-specific reasoning)

In the pre-fix design, m_batch_shader_mutex covered the entire
compile-and-publish sequence, including the glslang -> SPIR-V
step. After a UpdateSettings change (e.g. texture filter,
resolution scale) the precompile worker would hold the mutex
across each ~150-300 ms glslang compile of the heavier shaders,
and every concurrent main-thread DrawBatchVertices stalled
behind it for the full duration. The runloop never made forward
progress until the worker finished the entire 2160-entry batch
matrix - same user-visible multi-second hang the D3D12 backend
had.

Unlike D3D12, Vulkan 1.0 cannot drop the lock entirely:
vkCreateGraphicsPipelines requires external sync on the
pipelineCache parameter (D3D12's CreateGraphicsPipelineState
is thread-safe by Microsoft's documentation, no equivalent
guarantee in Vulkan 1.0). The
VK_PIPELINE_CACHE_CREATE_EXTERNALLY_SYNCHRONIZED_BIT flag from
Vulkan 1.3 would let us elide this, but SwanStation targets
VK_API_VERSION_1_0, so the lock around vkCreateGraphicsPipelines
is mandatory.

The win is still substantial because the EXPENSIVE step -
glslang -> SPIR-V translation, which has the D3DCompile-style
optimiser cost characteristic - now runs lock-free across
threads. vkCreateGraphicsPipelines itself is mostly the driver
enqueueing SPIR-V -> GPU-ISA work onto its own background
threads; the caller-thread time inside that call is short. So
PipelineCacheMutex is held for tens of milliseconds per
compile, not hundreds.

# Lazy main-thread pre-fill (mandatory)

Same pattern as D3D12: in Lazy mode, after the non-batch
upfront block (which is now empty - all non-batch PSOs are
lazy too), the main thread pre-fills every non-batch helper
before spawning the worker. Without this, the worker could
hold PipelineCacheMutex through the batch matrix walk and the
runloop's first FillVRAM / UpdateDisplay would starve on
non-FIFO Windows std::mutex.

# Behaviour summary (same shape as D3D12 commit)

  * Disabled: CompilePipelines builds only the two batch vertex
              shaders. All other PSOs faulted in on first draw.
              ~150 KB shader-cache + ~150 KB pipeline-cache
              footprint after a session.
  * Lazy:     CompilePipelines builds the two batch vertex
              shaders + all non-batch PSOs via the main-thread
              pre-fill, then spawns the worker to walk the
              batch matrix in the background. Gameplay starts
              instantly; worker fills slots opportunistically.
              ~5 MB cache footprint when worker finishes.
  * Enabled:  CompilePipelines builds the two batch vertex
              shaders + every batch shader-module + every PSO
              upfront via the same helpers (so the progress bar
              ticks per pipeline). Original ~35 s upfront pause.
              ~5 MB cache footprint immediately.

# Files touched

  * src/common/vulkan/shader_cache.h: declare
    m_shader_cache_mutex, m_pipeline_cache_mutex, add
    PipelineCacheMutex() accessor.
  * src/common/vulkan/shader_cache.cpp: scope-narrow
    m_shader_cache_mutex around index + file I/O in
    GetShaderSPV; add the double-check + publish pattern to
    CompileAndAddShaderSPV.
  * src/core/gpu_hw_vulkan.h: declare 13 lazy helpers, two
    member shader-module slots for the shared
    fullscreen-quad / UV-quad vertex shaders.
  * src/core/gpu_hw_vulkan.cpp: implement the helpers, gate the
    upfront non-batch block on precompile_sync, add the
    main-thread pre-fill for Lazy, swap 12 draw-path bindings
    onto the helpers, update DestroyPipelines to tear down the
    member shaders.

The 25 retro_* libretro exports are preserved. .so size is
unchanged versus master.
2026-05-15 03:49:01 +02:00