Commit graph

223 commits

Author SHA1 Message Date
LibretroAdmin
0786bc85fb core/d3d: fix box downsample PS signature for D3D12 PSO linkage
The pre-baked box_sample_downsample.ps.hlsl declared only
SV_Position as its pixel-shader input. With no preceding TEXCOORD0,
fxc packed SV_Position at input register 0. The fullscreen-quad
vertex shader these PSOs bind emits TEXCOORD0 at output register 0
and SV_Position at output register 1, so on D3D12 the PS input
register 0 had no matching VS output carrying the POSITION system
value. D3D12 links VS-output to PS-input by register at
CreateGraphicsPipelineState time, so the box PSO failed to compile
and the build returned null. UpdateDisplay then bound that null
pipeline (SetPipelineState dereferences its argument), faulting
inside D3D12Core when downsampling was switched on at runtime.

Declare an (unused) TEXCOORD0 input ahead of SV_Position so fxc
packs SV_Position at register 1, matching the vertex shader output
and the display pixel shaders (which likewise keep an unused
TEXCOORD0 at register 0). fxc preserves declared-but-unread inputs
in the signature, so the resolved layout is TEXCOORD0->reg0,
SV_Position->reg1 for every scale variant. This is also what
GenerateBoxSampleDownsampleFragmentShader already emits via
DeclareFragmentEntryPoint(ss, 0, 1, ...). D3D11 was unaffected
because CreatePixelShader does not validate against the bound VS.

Regenerated all 15 box DXBC variants (s2..s16).
2026-05-20 13:36:44 +02:00
LibretroAdmin
8090c79af6 core/d3d11: pre-bake the quad vertex shaders; no more shadergen calls
The screen-quad and UV-quad vertex shaders were the last two shadergen
+ D3DCompile calls on the D3D11 backend. Pre-baking them removes the
last runtime HLSL compilation from D3D11 - CompileShaders no longer
calls shadergen.Generate* or shader_cache.Get* at all.

screen-quad VS reuses an existing blob. ShaderGen::
GenerateScreenQuadVertexShader produces, on D3D, the exact same
SV_VertexID bit-shift fullscreen triangle as fullscreen_quad.vs.hlsl
(the blob D3D12 already consumes via GetFullscreenQuadVertexShader) -
the shadergen's only extra is a "#if API_OPENGL... v_pos.y = -v_pos.y"
block that is dead on D3D. So m_screen_quad_vertex_shader is built from
the existing k_fullscreen_quad_vs blob; no new blob is baked for it.

UV-quad VS is newly pre-baked. data/shaders/d3d_common/uv_quad.vs.hlsl
mirrors GenerateUVQuadVertexShader: the same fullscreen-triangle base
plus a cbuffer (UBOBlock @ b0: float2 u_uv_min, float2 u_uv_max - the
16-byte layout ShaderGen::DeclareUniformBuffer emits for D3D and that
gpu_hw_d3d11 already uploads) and the v_tex0 remap into the
[u_uv_min, u_uv_max] sub-rect. Single variant (no -D axes), so the
regen tool's no-variants fallback bakes it to uv_quad_vs.inc /
k_uv_quad_vs (1044 bytes). It's consumed by the D3D11 adaptive-
downsample mip-chain pass.

Changes:
  - data/shaders/d3d_common/uv_quad.vs.hlsl (new) +
    src/common/d3d_common/embedded_dxbc/uv_quad_vs.inc (new, fxc
    vs_5_0 /O3).
  - embedded_shaders.h: extern k_uv_quad_vs + a note documenting that
    the screen-quad VS has no dedicated blob and reuses
    k_fullscreen_quad_vs. embedded_shaders.cpp: include the new .inc.
  - gpu_hw_d3d11.cpp CompileShaders: m_screen_quad_vertex_shader from
    k_fullscreen_quad_vs, m_uv_quad_vertex_shader from k_uv_quad_vs,
    both via D3D11::ShaderCompiler::CreateVertexShader. Removed the two
    now-unused local aliases (shadergen, shader_cache).

m_shadergen / m_shader_cache are now vestigial on D3D11 (still
constructed / Open'd / reset, but nothing consumes them). They're left
in place here intentionally - the next commit removes m_shadergen,
m_shader_cache, ShaderCompiler::CompileShader and the D3DCompile
linkage across both backends together, and deletes the now-empty
d3d_shaders_* on-disk bytecode cache (the two files in the screenshot).

No blob duplication: the screen-quad path reuses the single
k_fullscreen_quad_vs definition; only the genuinely-distinct UV-quad VS
adds bytes (1 KB). All DXBC arrays remain defined once in
embedded_shaders.o.

Test:
  - uv_quad.vs.hlsl compiles under fxc (Microsoft d3dcompiler_47.dll
    via wine), 1044-byte vs_5_0 DXBC; .inc regenerated through the
    regen tool's compile_one/emit_inc no-variant path (auto-discovered
    via the HLSL_DIR glob, not added to TEMPLATE_VARIANTS).
  - x86_64-w64-mingw32-g++ -fsyntax-only -std=c++17 on gpu_hw_d3d11.cpp:
    exit 0, zero diagnostics (the removed locals leave no unused-var
    fallout).
  - embedded_shaders.cpp standalone g++ -c: exit 0. regen tool parses.
  - Brace balance verified; zero live shadergen.Generate* calls remain
    in either d3d11.cpp or d3d12.cpp.

Next:
  - Remove m_shadergen / m_shader_cache / ShaderCompiler::CompileShader
    / D3DCompile from both D3D backends; delete the vestigial
    d3d_shaders_* bytecode cache (scrub-on-open).
  - Then: GL MD5 -> xxh3.
2026-05-20 08:51:51 +02:00
LibretroAdmin
6f7d032352 core: pre-bake the batch vertex shader; D3D12 is now D3DCompile-free
The batch vertex shader was the last shadergen + D3DCompile call on
the D3D12 backend - the batch fragment shaders (e07ce04 .. f2620c1
arc) and the non-batch VRAM-ops / display shaders were already
pre-baked. Pre-baking it as DXBC and consuming the blobs on both
backends removes that last call, so the D3D12 backend issues ZERO
D3DCompile calls at runtime and no longer instantiates
GPU_HW_ShaderGen at all. The D3D11 backend drops one of its ten
shadergen calls (the screen/uv quad VS and the copy/display/vram_*
shaders still go through shadergen there - separate follow-ups).

Why only 2 blobs, not the FS-style 12. The runtime shadergen calls
DeclareVertexEntryPoint with msaa / per_sample / disable_color_
perspective, which add centroid / sample / noperspective qualifiers
to the v_col0 / v_tex0 OUTPUTS. In Direct3D the interpolation mode is
fixed by the PIXEL shader INPUT signature, not the vertex shader
output signature, so fxc emits byte-identical VS DXBC regardless of
those qualifiers - verified by hashing all 6 interp x persp
permutations per TEXTURED value (identical). So the only real axis is
the vertex-format split (TEXTURED), giving 2 blobs. This matches the
[textured] 2-slot batch VS array both backends already kept.
RESOLUTION_SCALE / PGXP_DEPTH / UV_LIMITS are cbuffer-routed
(7b575a3 et al), so they don't multiply the matrix either.

Foundation:
  - data/shaders/d3d_common/batch_vertex.vs.hlsl - mirror of
    GPU_HW_ShaderGen::GenerateBatchVertexShader. Shared 64-byte
    BatchUBOData cbuffer (VS reads u_resolution_scale + u_pgxp_depth);
    the pos transform + textured tex/texpage/uv_limits passthrough.
    The GL/Vulkan-only API_* blocks (POS_EPSILON, depth-range remap,
    Y-flip) are omitted - this is the D3D path.
  - tools/regen_d3d_common_dxbc.py - TEMPLATE_VARIANTS entry: 1 axis
    (TEXTURED) = 2 variants (untextured / textured).
  - src/common/d3d_common/embedded_dxbc/batch_vertex_vs_{untextured,
    textured}.inc - fxc vs_5_0 /O3 output (1908 / 2556 bytes).

Activation:
  - embedded_shaders.{h,cpp} - externs for the 2 blobs + the includes
    + Bytecode PickBatchVertexShader(bool textured) returning the
    matching DXBC. The header documents why there is no interp/persp
    axis.
  - D3D12 (gpu_hw_d3d12.{cpp,h}): GetBatchPipeline wraps
    PickBatchVertexShader(textured) into a D3D12_SHADER_BYTECODE
    inline (same shape as GetFullscreenQuadVertexShader). Removed: the
    synchronous batch VS compile loop in CompilePipelines, the
    m_batch_vertex_shader_blobs member + its DestroyPipelines reset,
    the m_shadergen member + its setup + its DestroyPipelines reset,
    the now-unused shader_cache local alias, and the 2-unit VS
    allotment in the progress total. Dropped the
    #include "gpu_hw_shadergen.h" from both files - D3D12 no longer
    references GPU_HW_ShaderGen (except one doc cross-reference
    comment). m_shader_cache stays: it still backs the PSO pipeline-
    library cache. Refreshed stale comments (ClearDisplayPipelines
    display-regen text, the copy-PSO d3d12/embedded_dxbc path, the
    GetBatchPipeline VS-source line).
  - D3D11 (gpu_hw_d3d11.cpp): CreateInputLayout consumes
    PickBatchVertexShader(true).data/size directly (the input layout
    only needs the bytecode's input signature); the
    m_batch_vertex_shaders[textured] objects are built via
    D3D11::ShaderCompiler::CreateVertexShader from the picked DXBC.
    shadergen + m_shader_cache stay alive on D3D11 for the
    not-yet-pre-baked screen/uv quad VS and non-batch shaders.

Both backends consume the same .inc blobs - DXBC at vs_5_0 is
honoured identically by D3D11 CreateVertexShader and D3D12 PSO
creation, the same arrangement the batch FS and VRAM-ops shaders
already use.

Test:
  - Regenerated the 2 blobs with the regen tool's own compile_one /
    emit_inc (fxc via the Microsoft d3dcompiler_47.dll under wine);
    confirmed Microsoft-creator DXBC, magic 0x43425844. Verified the
    6 interp x persp permutations hash identically per TEXTURED value
    (the basis for collapsing to 2 blobs).
  - x86_64-w64-mingw32-g++ -fsyntax-only -std=c++17 on
    gpu_hw_d3d11.cpp, gpu_hw_d3d12.cpp: exit 0, zero diagnostics.
  - embedded_shaders.cpp standalone g++ -c: exit 0 (embed.o 14 MiB).
  - regen tool parses (ast); brace balance + symbol/extern names
    cross-checked; no stray .dxbc.tmp.

Next (D3DCompile-removal arc):
  - D3D11 non-batch wiring: consume the already-pre-baked copy /
    display / vram_* blobs (the pickers exist) in place of the
    shader_cache.GetPixelShader compiles.
  - D3D11 quad VS pre-bake: screen_quad_vs, uv_quad_vs.
  - Remove shadergen + ShaderCompiler::CompileShader + the D3DCompile
    linkage once D3D11 no longer compiles anything.
  - Then: GL MD5 -> xxh3 hash swap (per the audit).
2026-05-20 07:26:40 +02:00
LibretroAdmin
e07ce046f0 core: pre-bake the textured xBR batch FS slice
Fifth and final batch FS template to be pre-baked, after
batch_untextured (9e6f933 + b6d1903 + c01f8ae),
batch_textured_nearest (9e4c33d + a7f5717),
batch_textured_bilinear (269a2a0 + f2ae92a), and
batch_textured_jinc2 (17a0c66 + 6afe8c2). Adds the HLSL template
+ regen recipe + 144 DXBC blobs for the (texture_mode !=
GPUTextureMode::Disabled AND m_texture_filter in {xBR,
xBRBinAlpha}) slice. No C++ wiring yet - the activation commit
lands next and completes the batch FS pre-bake arc.

Variant matrix is identical in shape to Bilinear / JINC2:

  * 6 (tex_mode) x 2 (dual) x 3 (interp) x 2 (persp) x 2 (BinAlpha)
    = 144 blobs.

Only the FilteredSampleFromVRAM body differs. xBR (eXtended Block
Resampling) is an edge-detection-based scaling algorithm:

  - Samples a 5x5 neighbourhood around each source texel via the
    P() helper macro that wraps SampleFromVRAM with UV-limit
    clamping.
  - Carries a per-texel "non-transparent" flag in the W field,
    repurposed from the VRAM "0000h" sentinel test.
  - Computes YCbCr distances (DistYCbCr) between adjacent samples
    using the rec.2020 W_YCBCR weights (0.2627, 0.6780, 0.0593,
    0.5).
  - For each of 4 quadrants (x = upper-left, y = upper-right, z =
    lower-right, w = lower-left), decides blend level: BLEND_NONE,
    BLEND_NORMAL, or BLEND_DOMINANT. The decision is gated by a
    gradient comparison: if the sum of YCbCr distances along one
    diagonal beats the sum along the other by
    DOMINANT_DIRECTION_THRESHOLD (3.6x), the dominant blend
    applies; otherwise normal blend.
  - Per-quadrant line-blend special case: if the gradient pattern
    matches a steep or shallow line shape (filtered by
    STEEP_DIRECTION_THRESHOLD = 2.2), adjust origin + direction
    vectors for the get_left_ratio mix factor.
  - Final colour is a per-quadrant lerp from E (centre) toward
    the chosen blend pixel, weighted by get_left_ratio which
    uses smoothstep on the projected distance to the blend line.

One documented behavioural quirk is preserved byte-for-byte: the
"non-transparent" flag for the I sample (lower-right of the 3x3)
is computed from H's vec4 rather than I's. This is a longstanding
shadergen bug in the original PCSX2 xBR implementation; we
reproduce it so the pre-baked DXBC matches the runtime
D3DCompile output exactly. Fixing it would be a semantic change
to xBR's visual output and is out of scope for the pre-bake arc.

The HLSL body is the largest of any filter template:
~250 lines of FilteredSampleFromVRAM body + helper functions,
producing ~50 KiB DXBC per variant on average (48-60 KiB across
the texture-mode / dual / persp / binalpha axis combos).
144 x ~50 KiB = ~7 MiB on disk and ~7 MiB embedded DXBC. Per-
variant compile time on Linux+wine is ~1s; the full 144-variant
regen takes 3-4 minutes uninterrupted (the wine-flake retry
logic at 269a2a0 catches transient misses).

Without the c532a34 TRANSPARENCY routing, this template would
have been 144 x 4 = 576 variants on the [render_mode] axis. Same
4x saving as Bilinear / JINC2.

Files in this commit (146 total, no C++ touched):

  data/shaders/d3d_common/batch_textured_xbr.ps.hlsl
    Hand-translated mirror of shadergen's textured-xBR-family
    output. Preamble identical to Bilinear / JINC2: BatchUBOData
    cbuffer, Texture2D + SamplerState bindings at register t0/s0,
    ApplyDithering / ApplyTextureWindow / ApplyUpscaledTextureWindow /
    FloatToIntegerCoords / RGBA8ToRGBA5551 / SampleFromVRAM
    helpers. FilteredSampleFromVRAM swaps in xBR's algorithm:
    DistYCbCr, IsPixEqual, get_left_ratio helpers + the 4-quadrant
    blend decision tree + per-quadrant blend application loops.
    main() is structurally identical to Bilinear / JINC2 - same
    `ialpha < 0.5` discard, same UV-limits unconditional clamp,
    same render_mode / pgxp_depth cbuffer-driven runtime branches.

  tools/regen_d3d_common_dxbc.py
    TEMPLATE_VARIANTS gains a batch_textured_xbr.ps.hlsl entry
    with all 144 variant tuples positioned after the JINC2 entry
    (chronological foundation-commit order: nearest, bilinear,
    jinc2, xbr, then the downsample templates). Variant suffix
    encoding identical to Bilinear / JINC2:
      pXrY_d{0,1}_{none,centroid,sample}_n{0,1}_b{0,1}
    where the b{0,1} digit encodes BINALPHA (0 = xBR,
    1 = xBRBinAlpha). Alphabetical sort matches the natural
    nested-loop iteration order in the matching picker.

  src/common/d3d_common/embedded_dxbc/batch_textured_xbr_ps_*.inc
    144 new .inc files, ~48-60 KiB DXBC per variant (~50 KiB
    average), totalling ~7 MiB on disk and ~7 MiB embedded DXBC
    in the .so. Per-variant size is the largest of any pre-baked
    batch FS template:
      Untextured:                 ~2-3 KiB
      Nearest:                    ~5 KiB
      Bilinear:                   ~6 KiB
      JINC2:                      ~17-24 KiB
      xBR:                        ~48-60 KiB
    The 5x5 sample fan-out + 4-quadrant blend decision tree + 4
    per-quadrant blend application blocks dominate the bytecode
    size; fxc /O3 cannot fold the data-dependent control flow
    even though many of the boolean inputs are compile-time
    constants from the BINALPHA macro.

What does NOT change in this commit:

  - GPU_HW_D3D12::GetBatchPipeline still routes xBR / xBRBinAlpha
    through the shadergen + D3DCompile fallback arm (the else
    branch at gpu_hw_d3d12.cpp after the JINC2 activation). The
    144 pre-baked xBR blobs are not yet consulted - they sit on
    disk waiting for the activation commit.

  - GPU_HW_D3D11::GetBatchPixelShader same: still falls through
    to the tmp_shadergen + D3DCompile path for xBR / xBRBinAlpha.

  - src/common/d3d_common/embedded_shaders.{h,cpp} untouched -
    they need 288 new extern declarations / includes to surface
    the .inc blob symbols to the C++ linker; the activation
    commit adds them along with the xBR-family picker.

  - m_texture_filter == xBR / xBRBinAlpha sessions still go
    through D3DCompile on both D3D11 and D3D12 backends until
    the activation commit lands.

  - The shadergen-fallback else arm in GetBatchPipeline and
    GetBatchPixelShader still exists. After the next activation
    commit it becomes unreachable and is deleted, together with
    the tmp_shadergen helper construction on the D3D11 side.

Test:
  - python3 tools/regen_d3d_common_dxbc.py --fxc /tmp/fxc_lite/
    fxc_lite.exe --wine /usr/bin/wine: 614 .inc files generated
    (470 pre-existing + 144 new xBR). Wine flakiness on Linux
    triggered transient retry warnings on the larger xBR variants
    (their longer fxc compile time leaves more room for wineserver
    state to drift); the retry logic added in 269a2a0 caught them.
  - Hand-compile of representative variants:
      no /D (p0r0_d0_none_n0_b0):                    49 692 bytes
      /D PALETTE_4_BIT=1:                            60 020 bytes
      /D PALETTE_4_BIT=1 /D BINALPHA=1:              60 048 bytes
      /D PALETTE_8_BIT=1 /D BINALPHA=1 /D RAW_TEXTURE=1
        /D USE_DUAL_SOURCE=1:                        58 912 bytes
      /D RAW_TEXTURE=1 /D INTERP_SAMPLE=1 /D NOPERSP=1
        /D BINALPHA=1 /D USE_DUAL_SOURCE=1:          48 584 bytes
    All compile cleanly; no fxc warnings or errors. Palette modes
    are noticeably larger than direct modes for the same reason
    as JINC2: the palette-index lookup adds another half-dozen
    SampleFromVRAM-equivalent dependent texture fetches per texel
    even after fxc's dead-code elimination, and xBR's 5x5
    neighbourhood multiplies the cost.

Cumulative pre-bake variant count for D3D batch FS:

  Pre-arc:                                    75 264 (every axis a -D)
  Post-cbuffer-routing arc:                   1 176
  Post-c01f8ae / a7f5717 (Untextured + Nearest, both backends):
                                              168 cell-equivalents
  Post-2fcf4cc (D3D11 downsample live):       22 additional cells
  Post-269a2a0 + f2ae92a (Bilinear-family live):
                                              +288 cell-equivalents
  Post-17a0c66 + 6afe8c2 (JINC2-family live):
                                              +288 cell-equivalents
  Post-this-commit (xBR slice on disk):       +144 baked
    (consumers wired in the activation commit)

Next:
  - Activation: 288 extern decls + 144 #include lines in
    embedded_shaders.{h,cpp}, plus a PickBatchTexturedXBRFS helper
    that takes the additional `binalpha` parameter, and the
    GetBatchPipeline / GetBatchPixelShader branches that route
    m_texture_filter in {xBR, xBRBinAlpha} + texture_mode !=
    Disabled to the new picker. Parallel to f2ae92a (Bilinear
    activation) and 6afe8c2 (JINC2 activation), but additionally
    this activation will:
      - Delete the now-unreachable shadergen-fallback else arm
        from GetBatchPipeline (D3D12) and GetBatchPixelShader
        (D3D11).
      - Delete the tmp_shadergen helper construction on the
        D3D11 side and the GetBatchFragmentShader call on the
        D3D12 side, since neither has any remaining consumer.
      - Tighten the D3D11 m_batch_pixel_shaders matrix
        dimensions if the [dithering] / [interlacing] dims
        become unused post-cbuffer-routing - they should, but
        the dim collapse is a separate concern from picker
        wiring and may land in a follow-up.
  - Independent: Phase 2 D3D12 downsample port - PSOs, explicit
    resource barriers, texture / mip-view creation. Uses the
    pre-baked blobs from 251e457 + the d3d_common rename at
    0ed7261.
2026-05-20 05:08:52 +02:00
LibretroAdmin
17a0c66232 core: pre-bake the textured JINC2 batch FS slice
Fourth batch FS template to be pre-baked, after batch_untextured
(9e6f933 + b6d1903 + c01f8ae), batch_textured_nearest (9e4c33d +
a7f5717), and batch_textured_bilinear (269a2a0 + f2ae92a). Adds
the HLSL template + regen recipe + 144 DXBC blobs for the
(texture_mode != GPUTextureMode::Disabled AND m_texture_filter
in {JINC2, JINC2BinAlpha}) slice. No C++ wiring yet - the
activation commit lands next, mirroring the Bilinear activation
arc.

Variant matrix is identical in shape to Bilinear:

  * 6 (tex_mode) x 2 (dual) x 3 (interp) x 2 (persp) x 2 (BinAlpha)
    = 144 blobs.

Only the FilteredSampleFromVRAM body differs. JINC2 implements a
4x4 = 16-tap sinc-windowed resampler with anti-ringing:

  - 16 SampleFromVRAM corner taps across a 4x4 footprint centred
    on (floor(coords - 0.5) + 0.5)
  - jinc2_resampler weight function:
      sinc(WA*x) * sinc(WB*x) / x^2   for x != 0
      WA*WB                            for x == 0
    where WA = 1.382300768 and WB = 2.576105976. Constants
    transcribed from the Vulkan template at
    data/shaders/vulkan/batch_textured_jinc2.frag.glsl.
  - Per-tap alpha is 0 for the VRAM "0000h" transparency
    sentinel, 1 otherwise. The resampler weights the alpha the
    same way it weights colour.
  - Anti-ringing: clamp resampler output to the colour range of
    the central 2x2 cell, then lerp back toward the unclamped
    value at JINC2_AR_STRENGTH = 0.8. Suppresses Gibbs-style
    ringing at hard edges that the sinc kernel would otherwise
    produce.
  - BINALPHA (when 1): quantises the resampler-weighted alpha to
    {0, 1} before the body's `ialpha < 0.5 ? discard : ...`
    test. Drops the partial-coverage smooth-alpha behaviour at
    texture edges; produces sharper alpha cutouts at the cost of
    stair-stepping. Identical to the BINALPHA arm in the Bilinear
    template.

Without the c532a34 TRANSPARENCY routing, this template would
have been 144 x 4 = 576 variants on the [render_mode] axis. Same
4x saving as Bilinear.

Files in this commit (146 total, no C++ touched):

  data/shaders/d3d_common/batch_textured_jinc2.ps.hlsl
    Hand-translated mirror of shadergen's textured-JINC2-family
    output. Reuses the Bilinear template's preamble verbatim:
    BatchUBOData cbuffer, Texture2D + SamplerState bindings at
    register t0/s0, ApplyDithering / ApplyTextureWindow /
    ApplyUpscaledTextureWindow / FloatToIntegerCoords /
    RGBA8ToRGBA5551 / SampleFromVRAM helpers. The
    FilteredSampleFromVRAM body is the only meaningful diff:
    JINC2's 16-tap sinc-windowed resampler + anti-ringing replaces
    Bilinear's 4-tap weighted average. main() is structurally
    identical to Bilinear - same `ialpha < 0.5` discard, same UV
    limits-unconditional clamp, same render_mode / pgxp_depth
    cbuffer-driven runtime branches.

  tools/regen_d3d_common_dxbc.py
    TEMPLATE_VARIANTS gains a batch_textured_jinc2.ps.hlsl entry
    with all 144 variant tuples. Variant suffix encoding
    identical to Bilinear:
      pXrY_d{0,1}_{none,centroid,sample}_n{0,1}_b{0,1}
    where the b{0,1} digit encodes BINALPHA (0 = JINC2,
    1 = JINC2BinAlpha). Alphabetical sort matches the natural
    nested-loop iteration order in the matching picker.

  src/common/d3d_common/embedded_dxbc/batch_textured_jinc2_ps_*.inc
    144 new .inc files, ~14-24 KiB DXBC per variant
    (~20 KiB average), totalling ~3 MiB on disk and ~3 MiB
    embedded DXBC in the .so. Per-variant size is ~3x larger
    than Bilinear because JINC2's 16 SampleFromVRAM calls + the
    4x4 weight matrix + the AR clamp produce substantially more
    instructions. fxc dead-strips the unused BINALPHA arm but
    cannot fold the 16-tap arithmetic itself.

What does NOT change in this commit:

  - GPU_HW_D3D12::GetBatchPipeline still routes JINC2 /
    JINC2BinAlpha through the shadergen + D3DCompile fallback
    arm (the else branch at gpu_hw_d3d12.cpp:1366 after the
    Bilinear activation). The 144 pre-baked JINC2 blobs are not
    yet consulted - they sit on disk waiting for the activation
    commit.

  - GPU_HW_D3D11::GetBatchPixelShader same: still falls through
    to the tmp_shadergen + D3DCompile path for JINC2 / xBR
    families.

  - src/common/d3d_common/embedded_shaders.{h,cpp} untouched -
    they need 288 new extern declarations / includes to surface
    the .inc blob symbols to the C++ linker; the activation
    commit adds them along with the JINC2-family picker.

  - m_texture_filter == JINC2 / JINC2BinAlpha sessions still go
    through D3DCompile on both D3D11 and D3D12 backends until
    the activation commit lands.

Test:
  - python3 tools/regen_d3d_common_dxbc.py --fxc /tmp/fxc_lite/
    fxc_lite.exe --wine /usr/bin/wine: 470 .inc files generated
    (326 pre-existing + 144 new JINC2). The retry logic added in
    269a2a0 catches transient wine misses; no hard fxc failures.
  - Hand-compile of representative variants:
      no /D (p0r0_d0_none_n0_b0):                    17 768 bytes
      /D PALETTE_4_BIT=1:                            24 092 bytes
      /D PALETTE_4_BIT=1 /D BINALPHA=1:              24 120 bytes
      /D PALETTE_8_BIT=1 /D BINALPHA=1 /D RAW_TEXTURE=1
        /D USE_DUAL_SOURCE=1:                        22 984 bytes
      /D RAW_TEXTURE=1 /D INTERP_SAMPLE=1 /D NOPERSP=1
        /D BINALPHA=1 /D USE_DUAL_SOURCE=1:          16 660 bytes
    All compile cleanly; no fxc warnings or errors on any
    combination tested. Palette modes are noticeably larger than
    direct modes (the palette-index lookup adds another half-dozen
    SampleFromVRAM calls per texel even with fxc's dead-code
    elimination).

Cumulative pre-bake variant count for D3D batch FS:

  Pre-arc:                                    75 264 (every axis a -D)
  Post-cbuffer-routing arc:                   1 176
  Post-c01f8ae (untextured slice live, D3D12 + D3D11):
                                                12 live
  Post-a7f5717 + 74b28be (Nearest slice live, both backends):
                                                84 live (12 + 72)
  Post-269a2a0 + f2ae92a (Bilinear slice live, both backends):
                                                228 live (12 + 72 + 144)
  Post-this-commit (JINC2 slice on disk):     +144 baked
    (consumers wired in the activation commit)

Next:
  - Activation: 288 extern decls + 144 #include lines in
    embedded_shaders.{h,cpp}, plus a PickBatchTexturedJINC2FS
    helper that takes the additional `binalpha` parameter, and
    the GetBatchPipeline / GetBatchPixelShader branches that
    route m_texture_filter in {JINC2, JINC2BinAlpha} +
    texture_mode != Disabled to the new picker. Parallel to
    f2ae92a (Bilinear activation).
  - Then the final filter template (xBR / xBRBinAlpha) as
    foundation + activation pair. After that, the shadergen
    fallback arm in GetBatchPipeline / GetBatchPixelShader
    becomes unreachable.
2026-05-20 02:11:59 +02:00
LibretroAdmin
269a2a0763 core: pre-bake the textured Bilinear batch FS slice
Third batch FS template to be pre-baked, after batch_untextured
(9e6f933 + b6d1903 + c01f8ae) and batch_textured_nearest
(9e4c33d + a7f5717). Adds the HLSL template + regen recipe + 144
DXBC blobs for the (texture_mode != GPUTextureMode::Disabled
AND m_texture_filter in {Bilinear, BilinearBinAlpha}) slice. No
C++ wiring yet - the activation commit lands next, mirroring the
b6d1903 / a7f5717 untextured / Nearest activation arcs.

Variant matrix (post-c532a34 TRANSPARENCY routing):

  * Texture mode (6 combos via 3 -D macros):
    Same encoding as batch_textured_nearest. See that template
    for the (PALETTE_4_BIT, PALETTE_8_BIT, RAW_TEXTURE) tuple
    mapping. PALETTE_4_BIT / PALETTE_8_BIT mutually exclusive.

  * USE_DUAL_SOURCE (0/1): drives o_col1 declaration + write.
    Same axis as the other batch FS templates. The Bilinear
    family is always non-Nearest, so the shadergen formula's
    `filter != Nearest` clause is true here; use_dual_source
    is just m_supports_dual_source_blend.

  * INTERP_CENTROID / INTERP_SAMPLE: interp tri-state qualifier.
    Same axis as the other batch FS templates.

  * NOPERSP (0/1): `noperspective` on v_col0. Same axis.

  * BINALPHA (0/1): NEW axis vs Nearest. Gates the ialpha >= 0.5
    quantisation step at the end of FilteredSampleFromVRAM
    (BINALPHA=1 => BilinearBinAlpha enum value, BINALPHA=0 =>
    standard Bilinear). The two enum values share a single
    template with fxc dead-stripping the unused arm.

6 (tex_mode) x 2 (dual) x 3 (interp) x 2 (persp) x 2 (BinAlpha)
= 144 blobs. Double the Nearest count (72) because BINALPHA adds
one dimension. The MSAA axis still does NOT multiply this template
- the batch FS reads from the single-sample shadow VRAM
regardless of m_multisamples.

UV_LIMITS is implicit for all non-Nearest filters: ShouldUseUVLimits
in gpu_hw.cpp forces m_using_uv_limits true when the filter is
non-Nearest. The shader unconditionally clamps to v_uv_limits
without a runtime cbuffer gate; the Nearest template's
`if (u_uv_limits != 0u)` branch becomes dead code here and is
deleted from this template.

Without the c532a34 TRANSPARENCY routing, this template would
have been 144 x 4 = 576 variants on the [render_mode] axis. Worth
the routing commit.

Files in this commit (146 total, no C++ touched):

  data/shaders/d3d_common/batch_textured_bilinear.ps.hlsl
    Hand-translated mirror of shadergen's textured-Bilinear-
    family output. Same shape as the Nearest template:
    BatchUBOData cbuffer, Texture2D + SamplerState bindings at
    register t0/s0, full ApplyDithering / ApplyTextureWindow /
    ApplyUpscaledTextureWindow / FloatToIntegerCoords /
    RGBA8ToRGBA5551 / SampleFromVRAM helpers. NEW vs Nearest:
    FilteredSampleFromVRAM helper performing 4-tap bilinear
    with VRAM-rgba-0000-sentinel handling per-corner, ialpha
    weighted-coverage compensation, and the BINALPHA-gated
    quantisation. The main() body's transparent-texel discard
    becomes `ialpha < 0.5` (replaces the Nearest template's
    all-zero discard).

  tools/regen_d3d_common_dxbc.py
    1. TEMPLATE_VARIANTS gains a batch_textured_bilinear.ps.hlsl
       entry with all 144 variant tuples. Variant suffix
       encoding: pXrY_d{0,1}_{none,centroid,sample}_n{0,1}_b{0,1}
       where the b{0,1} digit encodes BINALPHA (0 = Bilinear,
       1 = BilinearBinAlpha).
    2. NEW: compile_one() retries fxc invocations that return
       success but produce no output file. This is a wine
       flakiness symptom (wineserver hangs briefly under load
       when wine32 components are in transient bad state) that
       only manifests during bulk regeneration on Linux
       cross-compile setups. fxc.exe running natively on
       Windows doesn't hit this. Up to 5 retries with a warning
       to stderr on each transient miss; after 5 the script
       exits with a hard error including the last stdout/stderr
       for diagnosis. Hard fxc errors (HLSL syntax violations,
       non-zero exit code) still fast-fail without retry. The
       regenerated bytecode is identical regardless of which
       attempt succeeds - fxc is deterministic on identical
       source + flags.

  src/common/d3d_common/embedded_dxbc/batch_textured_bilinear_ps_*.inc
    144 new .inc files, ~5-8 KiB DXBC per variant (~6 KiB
    average), totalling ~860 KiB on disk and ~900 KiB embedded
    DXBC in the .so. Larger per-variant than the Nearest blobs
    (~5500 bytes avg) because the 4-tap bilinear adds ~1 KiB of
    code per variant.

What does NOT change in this commit:

  - GPU_HW_D3D12::GetBatchPipeline still calls GetBatchFragmentShader
    for textured non-Nearest slices (Bilinear / JINC2 / xBR),
    which runs shadergen + D3DCompile. The 144 pre-baked Bilinear
    blobs are not yet consulted - they sit on disk waiting for
    the activation commit.

  - GPU_HW_D3D11::GetBatchPixelShader still falls through to
    the shadergen + D3DCompile path for non-Nearest filters
    (the `else` arm at line 1349+ of gpu_hw_d3d11.cpp).

  - src/common/d3d_common/embedded_shaders.{h,cpp} untouched -
    they need 288 new extern declarations / includes to surface
    the .inc blob symbols to the C++ linker; the activation
    commit adds them along with the Bilinear-family picker.

  - m_texture_filter == Bilinear / BilinearBinAlpha sessions
    still go through D3DCompile on both D3D11 and D3D12 backends
    until the activation commit.

Test:
  - python3 tools/regen_d3d_common_dxbc.py --fxc /tmp/fxc_lite/
    fxc_lite.exe --wine /usr/bin/wine: 326 .inc files generated
    (182 pre-existing batch / display / vram_* / downsample +
    144 new Bilinear). The new retry logic catches a handful of
    transient wine misses per full regen pass; warnings appear
    in stderr but the script completes.
  - Hand-compile of representative variants:
      no /D (p0r0_d0_none_n0_b0):     7228 bytes DXBC
      /D PALETTE_4_BIT=1:              8732 bytes
      /D PALETTE_4_BIT=1 /D BINALPHA=1: 8760 bytes
      /D PALETTE_8_BIT=1 /D BINALPHA=1 /D RAW_TEXTURE=1
        /D USE_DUAL_SOURCE=1:          7624 bytes
      /D RAW_TEXTURE=1 /D INTERP_SAMPLE=1 /D NOPERSP=1
        /D BINALPHA=1 /D USE_DUAL_SOURCE=1: 6120 bytes
    All compile cleanly; no fxc warnings or errors on any
    combination tested.

Cumulative pre-bake variant count for D3D batch FS:

  Pre-arc:                                    75 264 (every axis a -D)
  Post-cbuffer-routing arc:                   1 176
  Post-c01f8ae (untextured slice live, D3D12 + D3D11):
                                                12 live
  Post-a7f5717 (Nearest slice live, D3D12 + D3D11):
                                                84 live (12 + 72)
  Post-this-commit (Bilinear slice on disk):  +144 baked
    (consumers wired in the activation commit)

Next:
  - Activation: 288 extern decls + 144 #include lines in
    embedded_shaders.{h,cpp}, plus a PickBatchTexturedBilinearFS
    helper in D3DCommon::EmbeddedShaders that takes the
    additional `binalpha` parameter, and the
    GetBatchPipeline / GetBatchPixelShader branches that route
    m_texture_filter in {Bilinear, BilinearBinAlpha} +
    texture_mode != Disabled to the new picker. Parallel to
    a7f5717 (D3D12 Nearest activation) + 74b28be (D3D11 Nearest
    activation).
  - Then the remaining 2 textured filter templates (JINC2 / xBR
    each with BinAlpha) as foundation + activation pairs.
2026-05-20 00:53:52 +02:00
LibretroAdmin
0ed7261c70 core: rename data/shaders/d3d12 -> data/shaders/d3d_common
The .inc consumers and the C++ host namespace moved to d3d_common
in 24bacbf, but the HLSL sources and the regen tool kept their
d3d12 tags for "historical / churn-avoidance reasons" - the
commit message there explicitly flagged it as deferred cleanup
("data/shaders/d3d12 -> data/shaders/d3d_common + tool rename.
Currently kept as d3d12 for historical/churn-avoidance
reasons"). With the downsample foundation landed at 251e457
also putting its HLSL under data/shaders/d3d12/ (despite being
consumed only by D3D11 today, with D3D12 consumption arriving
in a later phase), the naming inconsistency is now actively
confusing. This commit completes the rename.

What changes (mechanically, via git mv + sweep):

  data/shaders/d3d12/ -> data/shaders/d3d_common/
    Whole directory renames via git mv. 16 files move:
    README.md + 10 .hlsl source templates (5 batch + display +
    copy + vram_{copy,fill,read,update_depth,write} +
    fullscreen_quad + 4 downsample). Each .hlsl content
    is unchanged except where a comment string referenced the
    old path (3 files: box_sample_downsample.ps.hlsl,
    adaptive_downsample_composite.ps.hlsl, vram_fill.ps.hlsl).

  tools/regen_d3d12_dxbc.py -> tools/regen_d3d_common_dxbc.py
    Renamed via git mv. The HLSL_DIR constant repointed to the
    new HLSL directory; the .inc preamble template updated to
    emit the new paths; the top-of-file comment updated to
    drop the "kept as d3d12 for historical reasons" note now
    that it doesn't apply. Behaviour unchanged: same HLSL ->
    fxc /T ps_5_0 /E main /O3 -> same .inc bytes.

  src/common/d3d_common/embedded_dxbc/*.inc (182 files)
    Preamble line 1 updated:
      // Autogenerated from data/shaders/d3d12/<name>.hlsl.
      -> // Autogenerated from data/shaders/d3d_common/<name>.hlsl.
    Preamble line 2 updated:
      // DO NOT EDIT. Regenerate with tools/regen_d3d12_dxbc.py.
      -> // DO NOT EDIT. Regenerate with tools/regen_d3d_common_dxbc.py.
    The byte arrays themselves are bit-identical pre/post-commit
    (fxc emits identical DXBC from the same HLSL source). Only
    the two comment lines of each .inc change.

  src/common/d3d_common/embedded_shaders.h
    14 doc comments updated:
      "// Source: data/shaders/d3d12/<name>.hlsl"
      -> "// Source: data/shaders/d3d_common/<name>.hlsl"
    Plus the top-of-file rationale block updated to drop the
    d3d12-tagged path reference.

  src/core/gpu_hw_d3d12.cpp
    Two comment lines referencing the old data/shaders/d3d12/
    path and the old regen tool name. The pre-bake call sites
    themselves were already namespace-qualified (D3DCommon::
    EmbeddedShaders::*) and need no change.

  data/shaders/d3d_common/README.md
    8 doc references to the old paths updated.

What doesn't change in this commit:

  - DXBC byte content. fxc is deterministic on identical source +
    flags; the post-rename HLSL is byte-identical to the
    pre-rename HLSL except for comment text, and comments don't
    survive into the compiled bytecode. Re-running
    tools/regen_d3d_common_dxbc.py produces .inc files that
    match this commit's preamble lines + the pre-existing byte
    arrays.

  - C++ namespaces. Already D3DCommon::EmbeddedShaders since
    24bacbf. Not touched here.

  - Header / library / TU layout. embedded_shaders.{h,cpp}
    already live at src/common/d3d_common/ since 24bacbf;
    embedded_dxbc/ already lives at
    src/common/d3d_common/embedded_dxbc/. This commit completes
    the symmetric rename on the data-side and tools-side.

  - Build system files (CMakeLists.txt, Makefile.libretro):
    none of them references the data/shaders directory tree or
    the regen tool - the data is consumed at HLSL-source-edit
    time only, not at build time. Untouched.

  - Runtime behaviour: zero diff across D3D11 + D3D12. Same
    blobs, same pickers, same PSOs.

Test:
  - git status post-rename shows 16 renames + 184 minor edits
    (preamble-only diffs in the .inc files + comment-only diffs
    in the regen tool + embedded_shaders.h + gpu_hw_d3d12.cpp).
    No moved file's content changed structurally.
  - Hand-compile of a representative variant
    (box_sample_downsample at RESOLUTION_SCALE=4) post-rename:
    1044 bytes DXBC, identical to pre-rename.
  - String search across the tree: zero remaining references to
    "data/shaders/d3d12" or "regen_d3d12_dxbc" outside of
    .git history.

Next:
  - Activation commit for the downsample pre-bake: add 22 extern
    pairs + 22 #include lines to embedded_shaders.{h,cpp},
    add 4 picker functions, rewrite the 5 shadergen calls in
    GPU_HW_D3D11::CompileShaders. The d3d_common naming is now
    coherent across HLSL sources, regen tool, .inc files, host
    TU, and host namespace - new pre-bake additions don't have
    to thread the old d3d12 naming.
  - Phase 2 (separate effort): port the D3D11 downsample
    machinery to D3D12 - PSOs, explicit resource barriers,
    texture / mip-view creation - and set
    m_supports_adaptive_downsampling = true on the D3D12 caps
    path. The pre-baked blobs are already in place by then.
2026-05-19 11:19:24 +02:00
LibretroAdmin
251e457bd9 core: pre-bake the downsample fragment shaders
Backport of the 4 Vulkan downsample shader templates to HLSL for
the D3D backends. Lands the data + recipe; no C++ wiring yet.
Activation in the next commit swaps the D3D11 backend's runtime
shadergen + D3DCompile calls in GPU_HW_D3D11::CompileShaders for
the matching pre-baked pickers.

D3D12 currently has no downsample implementation - the backend
inherits m_downsample_mode = Disabled and m_supports_adaptive_
downsampling = false from GPU_HW. Adding D3D12 downsample is a
separate phase that will reuse the pre-baked blobs this commit
lands; the d3d_common location (24bacbf) means there is no path-
churn to repeat.

Templates (4 HLSL files, 22 reachable variants total):

  data/shaders/d3d12/adaptive_downsample_blur.ps.hlsl
    Mirror of mipmap_blur.glsl from parallel-rsx. 3x3 weighted
    kernel on the alpha channel of the prior-pass mip. No
    variant axes - the body references no compile-time toggles
    and no RESOLUTION_SCALE.
    -> 1 variant (no /D).

  data/shaders/d3d12/adaptive_downsample_mip.ps.hlsl
    Mirror of mipmap_energy.glsl from parallel-rsx. Measures
    "energy" (variance) across a 2x2 footprint and writes a
    scalar bias used by composite to choose mip levels. One
    axis:
      FIRST_PASS (0/1) - the Vulkan template uses spec const
      id=100; HLSL has no spec-constant mechanism so we use
      -D FIRST_PASS=1 and fxc dead-strips the unused arm.
    -> 2 variants. Suffix f{0,1}.

  data/shaders/d3d12/adaptive_downsample_composite.ps.hlsl
    Mirror of mipmap_resolve.glsl from parallel-rsx. Reads the
    per-pixel mip-bias scalar (samp1, the blur pass output) and
    samples the colour pyramid (samp0) at the per-pixel mip
    level. One axis:
      RESOLUTION_SCALE (integer literal) - drives RCP_VRAM_SIZE
      and the mip multiplier. Adaptive mode forces RESOLUTION_SCALE
      to a power of 2 in [2, m_max_resolution_scale] via
      GPU_HW::CalculateResolutionScale (gpu_hw.cpp:391). With
      D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION / VRAM_WIDTH the cap
      is 16, so the reachable set is {2, 4, 8, 16}.
    -> 4 variants. Suffix s{2,4,8,16}.

  data/shaders/d3d12/box_sample_downsample.ps.hlsl
    Box-filter downsample - averages a RESOLUTION_SCALE x
    RESOLUTION_SCALE block of upscaled texels back down to one
    PSX-native-resolution pixel. One axis:
      RESOLUTION_SCALE (integer literal) - drives base_coords
      scaling, the two inner loop bounds, and the averaging
      divisor. Box mode accepts any RESOLUTION_SCALE in
      [2, m_max_resolution_scale] (no power-of-2 constraint,
      unlike Adaptive); the cap is 16, so the reachable set is
      {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}.
      Scale=1 disables downsampling entirely (GetDownsampleMode
      gpu_hw.cpp:399), so we don't bake a variant for it.
    -> 15 variants. Suffix s{2..16}.

Each .inc is small. blur is 2292 bytes of DXBC; mip is
1844/1872 bytes per FIRST_PASS arm; composite is 900 bytes per
scale (RESOLUTION_SCALE folds at fxc time to a literal in the
operand encoding without growing the bytecode length); box is
1044 bytes per scale (md5 differs across scales, confirming the
literal is embedded distinctly even though the byte count is
overhead-dominated). Total embedded payload: ~28 KiB across the
22 blobs - negligible compared to the batch FS slices.

The HLSL is API-neutral; the same .inc files serve both D3D11
(which has the runtime downsample path that the activation
commit swaps out) and D3D12 (which has no downsample
implementation yet but inherits the pre-baked blobs for the
follow-up phase). fxc /T ps_5_0 /E main /O3 emits DXBC that
both backends consume identically. The d3d_common location set
up at 24bacbf is the right home for these.

What changes:

  data/shaders/d3d12/{adaptive_downsample_blur,
                       adaptive_downsample_mip,
                       adaptive_downsample_composite,
                       box_sample_downsample}.ps.hlsl
    4 new files. Each carries a top-of-file comment describing
    the Vulkan-side origin, the variant axes (or lack thereof),
    bindings (cbuffer / textures / sampler slots), and the
    /D macro names with reachable values.

  tools/regen_d3d12_dxbc.py
    TEMPLATE_VARIANTS gains 3 entries: adaptive_downsample_mip,
    adaptive_downsample_composite, box_sample_downsample. Each
    entry documents its axes and the reachable variant range.
    adaptive_downsample_blur is intentionally NOT listed - it
    falls through to the script's no-variants-table fallback
    (compile once with no /D defines, emit a single
    .inc with the base identifier), which produces the right
    output (k_adaptive_downsample_blur_ps).

  src/common/d3d_common/embedded_dxbc/
    22 new .inc files:
      adaptive_downsample_blur_ps.inc                       (1)
      adaptive_downsample_mip_ps_{f0,f1}.inc                (2)
      adaptive_downsample_composite_ps_{s2,s4,s8,s16}.inc   (4)
      box_sample_downsample_ps_{s2..s16}.inc                (15)

What doesn't change in this commit:

  - src/common/d3d_common/embedded_shaders.{h,cpp}: untouched.
    No extern declarations for the new blobs yet. Adding them
    without consumers would yield -Wunused warnings the build
    treats as errors. Both the externs and the picker functions
    land together in the activation commit.

  - src/core/gpu_hw_d3d11.cpp: untouched. The 5 shadergen calls
    in CompileShaders (lines 1203/1205/1207/1209/1220) still go
    through shader_cache.GetPixelShader on the runtime
    shadergen + D3DCompile path. Activation commit replaces
    them with D3DCommon::EmbeddedShaders picker + CreatePixelShader.

  - src/core/gpu_hw_d3d12.cpp: untouched. No downsample
    implementation exists yet. The D3D12 phase is a separate
    project that ports the D3D11 downsample machinery (110 lines
    across DownsampleFramebufferAdaptive +
    DownsampleFramebufferBoxFilter plus ~50 lines of texture/
    view creation in CompileShaders) to D3D12's command-list /
    explicit-barrier model, sets m_supports_adaptive_downsampling
    = true on the D3D12 SetCapabilities path, and consumes the
    pre-baked blobs from this commit.

  - Visual output: zero diff. D3D11 still uses runtime D3DCompile;
    D3D12 still has no downsample.

Test:
  - Hand-compile of representative variants via
    /tmp/fxc_lite/fxc_lite.exe /T ps_5_0 /E main /O3:
      blur: 2292 bytes (no /D)
      mip f0: 1872 bytes (FIRST_PASS=0)
      mip f1: 1844 bytes (FIRST_PASS=1)
      composite s4: 900 bytes (RESOLUTION_SCALE=4)
      composite s16: 900 bytes (RESOLUTION_SCALE=16)
      box s2: 1044 bytes
      box s8: 1044 bytes
      box s16: 1044 bytes
    All compile cleanly; no fxc warnings or errors. md5sum
    across box scales 2/4/8/16 confirms each scale produces a
    distinct DXBC byte sequence (literal RESOLUTION_SCALE is
    embedded per variant), not 4 identical blobs.

  - python3 tools/regen_d3d12_dxbc.py --fxc /tmp/fxc_lite/fxc_lite.exe
    --wine /usr/bin/wine emits 182 .inc files total: 160
    pre-existing + 22 downsample. No regressions on the existing
    batch FS / display / vram_* blobs - they regenerate
    bit-identical (same HLSL, same fxc invocation).

Cumulative pre-bake variant count:

  Pre-arc:                                     75 264 (every axis a -D)
  Post-cbuffer-routing arc:                    1 176
  Post-c01f8ae / a7f5717 (D3D12 batch FS):      84 baked + live
  Post-this-commit (downsample blobs on disk): 22 additional baked
    (consumers wired in the activation commit)

Next:
  - Activation: add 22 new (uint8_t array + size_t) extern pairs
    + #include lines to D3DCommon::EmbeddedShaders, add 4 picker
    functions (Box / AdaptiveMip / AdaptiveBlur / AdaptiveComposite),
    and rewrite the 5 shadergen calls in GPU_HW_D3D11::CompileShaders
    to use the pickers + D3D11::ShaderCompiler::CreatePixelShader.
    Same shape as the b6d1903 / a7f5717 batch FS activations.
  - Phase 2 (separate effort, not part of the pre-bake arc):
    port the D3D11 downsample machinery to D3D12 - PSO builds,
    explicit resource barriers, texture/mip-view creation - and
    set m_supports_adaptive_downsampling = true on the D3D12
    backend. The pre-baked blobs are already in place by then.
2026-05-19 11:11:06 +02:00
LibretroAdmin
9e4c33d53c core/d3d12: pre-bake the textured Nearest batch FS slice
Second batch FS template to be pre-baked, after batch_untextured.
Adds the HLSL template + regen recipe + 72 DXBC blobs for the
(texture_mode != GPUTextureMode::Disabled AND m_texture_filter
== GPUTextureFilter::Nearest) slice. No C++ wiring yet - the
activation commit lands next, mirroring the
9e6f933 -> b6d1903 untextured arc.

Variant matrix (post-c532a34 TRANSPARENCY routing):

  * Texture mode (6 combos via 3 `-D` macros):
      (PALETTE_4_BIT=0, PALETTE_8_BIT=0, RAW_TEXTURE=0): Direct16Bit
      (PALETTE_4_BIT=0, PALETTE_8_BIT=0, RAW_TEXTURE=1): RawDirect16Bit
      (PALETTE_4_BIT=1, PALETTE_8_BIT=0, RAW_TEXTURE=0): Palette4Bit
      (PALETTE_4_BIT=1, PALETTE_8_BIT=0, RAW_TEXTURE=1): RawPalette4Bit
      (PALETTE_4_BIT=0, PALETTE_8_BIT=1, RAW_TEXTURE=0): Palette8Bit
      (PALETTE_4_BIT=0, PALETTE_8_BIT=1, RAW_TEXTURE=1): RawPalette8Bit
    PALETTE_4_BIT and PALETTE_8_BIT are mutually exclusive in the
    shadergen #elif chain - no variant has both set. The
    Reserved_Direct16Bit / Reserved_RawDirect16Bit enum values fold
    to their non-Reserved counterparts at the C++ picker level
    (their shader source is identical).

  * USE_DUAL_SOURCE (0/1): drives o_col1 declaration + write.
    Same axis as untextured (b6d1903).
  * INTERP_CENTROID / INTERP_SAMPLE: interp tri-state qualifier.
    Same axis as untextured.
  * NOPERSP (0/1): `noperspective` on v_col0. Same axis as
    untextured.

6 (tex_mode) x 2 (dual) x 3 (interp) x 2 (persp) = 72 blobs. The
MSAA axis (m_multisamples 1/2/4/8/16/32) does NOT multiply this
template - the batch FS samples from the single-sample shadow
VRAM regardless of m_multisamples, so there's no LOAD_TEXTURE_MS
unroll. MSAA only affects the interp qualifier (centroid /
sample), already captured by INTERP_*.

Without c532a34 (TRANSPARENCY -> u_render_mode routing) this
template would have been 6 x 4 x 2 x 3 x 2 = 288 variants on the
[render_mode] axis - 4x bigger. Worth the routing commit.

Files in this commit (74 total, no C++ touched):

  data/shaders/d3d12/batch_textured_nearest.ps.hlsl
    Hand-translated mirror of shadergen's textured-Nearest output:
    BatchUBOData cbuffer, Texture2D + SamplerState bindings at
    register t0/s0 (matches m_batch_root_signature - see
    CreateRootSignatures in gpu_hw_d3d12.cpp), full
    ApplyDithering / ApplyTextureWindow / ApplyUpscaledTextureWindow
    / FloatToIntegerCoords / RGBA8ToRGBA5551 / SampleFromVRAM
    helpers, and the main() body with palette / direct / raw /
    semitransparent / discard logic. The post-c532a34 runtime
    branches on u_render_mode (premultiply, output arms,
    OnlyOpaque/OnlyTransparent discards) and u_uv_limits (UV
    clamping), u_dithering, u_interlacing, u_pgxp_depth, u_uv_limits
    cbuffer scalars are all wired the same way as the Vulkan
    template at batch_textured_nearest.frag.glsl.

  tools/regen_d3d12_dxbc.py
    TEMPLATE_VARIANTS gains a batch_textured_nearest.ps.hlsl entry
    with all 72 variant tuples. Variant suffix encoding:
    pXrY_d{0,1}_{none,centroid,sample}_n{0,1} where X is the
    palette mode digit (4 / 8 / 0), Y is RawTextureBit, the d/n
    digits encode the dual_source / nopersp bits, and the interp
    word names the qualifier.

  src/common/d3d12/embedded_dxbc/batch_textured_nearest_ps_*.inc
    72 new .inc files, ~2592 KiB total on disk, ~400 KiB embedded
    DXBC in the .so. Per-variant DXBC sizes range from 4132 bytes
    (RawDirect16Bit + no dual + no qual) to 6656 bytes
    (Palette8Bit + dual + centroid + nopersp); average ~5500
    bytes. The raw variants are smaller because RAW_TEXTURE=1
    skips the colour-modulation path; the palette variants are
    larger because they unroll the index extraction + palette
    sample.

What does NOT change in this commit:

  - GPU_HW_D3D12::GetBatchPipeline still calls
    GetBatchFragmentShader for textured slices, which runs
    shadergen + D3DCompile. The 72 pre-baked Nearest blobs are
    not yet consulted - they sit on disk waiting for the
    activation commit (parallel to b6d1903 for untextured).

  - src/common/d3d12/embedded_shaders.{h,cpp} untouched here -
    they need 72 new extern declarations / includes to actually
    surface the .inc blob symbols to the C++ linker, but adding
    those without C++ consumers would yield -Wunused warnings
    that the build flags treat as errors. Both land together in
    the activation commit.

  - m_texture_filter == Nearest sessions still go through
    D3DCompile until the activation commit lands. The picker
    function PickBatchUntexturedFSBytecode (gpu_hw_d3d12.cpp) is
    untouched - the textured analogue lands with the activation.

Test:
  - python3 tools/regen_d3d12_dxbc.py --fxc /tmp/fxc_lite/fxc_lite.exe
    --wine /usr/bin/wine: 88 .inc files generated, 72 of which are
    the new batch_textured_nearest_ps_* set. Build clean across all
    72 variants - no fxc warnings or errors on any combination.
  - Hand-compile of representative variants
    (PALETTE_4_BIT=1 / PALETTE_8_BIT=1 / RAW_TEXTURE=1 / dual /
     INTERP_SAMPLE / NOPERSP combos): all produce valid DXBC
    blobs at expected sizes.

Cumulative pre-bake variant count progress for D3D12 batch FS:

  Pre-arc:                                    75 264 (every axis a -D)
  Post-cbuffer-routing of:
    DITHERING/INTERLACING/UV_LIMITS/PGXP_DEPTH 4 704
  Post-c532a34 (TRANSPARENCY -> u_render_mode) 1 176
  Post-c01f8ae (untextured re-bake, slice live)  12 baked
  Post-this-commit (Nearest .inc on disk):
    Untextured slice (live):    12
    Nearest slice (on disk):    72
    Remaining filters (Bilinear/JINC2/xBR): 3 templates x 72 = 216

Next:
  - Activation: 144 new extern decls + 72 new #includes in
    embedded_shaders.{h,cpp}, plus a PickBatchNearestFSBytecode-
    style helper in gpu_hw_d3d12.cpp and the GetBatchPipeline
    branch that routes m_texture_filter == Nearest +
    texture_mode != Disabled to the new picker. Parallel to
    b6d1903 for untextured.
  - Then the remaining 3 textured filter templates (Bilinear /
    JINC2 / xBR) each as a foundation + activation pair.
2026-05-19 09:12:23 +02:00
LibretroAdmin
c01f8ae757 core/d3d12: re-bake the untextured FS slice on the routed matrix
Drops the TRANSPARENCY `-D` axis from the untextured pre-bake
template, collapsing the variant count 24 -> 12. The 4-state
BatchRenderMode enum that used to drive a compile-time
TRANSPARENCY macro at fxc invocation time landed at c532a34
("core: route TRANSPARENCY through the batch UBO") as a runtime
branch on u_render_mode at cbuffer offset 60 - the DXBC bytecode
is invariant across render_mode post-routing, so a single set of
12 variants covers all 4 BatchRenderMode values.

What changes:

  data/shaders/d3d12/batch_untextured.ps.hlsl
    The `#if TRANSPARENCY` premultiply branch becomes
      `if (u_render_mode != 0u) premultiply_alpha = ialpha * ...;`
    The `#if TRANSPARENCY / #else` output-arm pair collapses to
    a single colour write with the dual_source o_col1 expression
    chosen by the runtime u_render_mode branch:
      o_col0 = float4(color, oalpha);
      #if USE_DUAL_SOURCE
        if (u_render_mode != 0u)
          o_col1 = float4(0,0,0, u_dst_alpha_factor / ialpha);
        else
          o_col1 = float4(0,0,0, 1.0 - ialpha);
      #endif
    cbuffer field rename u_pad2 -> u_render_mode mirrors the
    BatchUBOData rename from c532a34. Top-of-file comment block
    updated for the new 4-axis (was 5-axis) matrix.

  tools/regen_d3d12_dxbc.py
    batch_untextured.ps.hlsl entry: 24 variant tuples drop to
    12. The former t{0,1} prefix is gone; suffix is now
    d{0,1}_{none,centroid,sample}_p{0,1}. Variant comment
    block updated to call out the c532a34 dependency.

  src/common/d3d12/embedded_dxbc/batch_untextured_ps_*.inc
    24 old t{0,1}d{0,1}_*_p{0,1} files deleted; 12 new
    d{0,1}_*_p{0,1} files created. Net: -12 .inc files,
    ~240 KiB on disk down from ~445 KiB (-46%). Per-variant
    DXBC sizes:
      d=0 (no dual): 3052 bytes
      d=1 (dual):    3164 bytes
    Both invariant under interp and persp (the qualifier-only
    changes don't grow the DXBC body; just reflection metadata).
    Total embedded DXBC in the .so: ~37 KiB (was ~72 KiB).

  src/common/d3d12/embedded_shaders.h
    48 t{0,1}d{0,1}_*_p{0,1} extern declarations replaced with
    24 d{0,1}_*_p{0,1} ones. Section comment updated to call
    out the 4-axis variant matrix and the c532a34 dependency.

  src/common/d3d12/embedded_shaders.cpp
    24 #include lines for the old .inc files replaced with 12
    for the new ones.

  src/core/gpu_hw_d3d12.cpp
    PickBatchUntexturedFSBytecode signature changes:
      old: (uint8_t render_mode, GPUTextureFilter filter,
            bool supports_dual_source_blend, uint32_t multisamples,
            bool per_sample_shading, bool disable_color_perspective)
      new: (bool use_dual_source, uint32_t multisamples,
            bool per_sample_shading, bool disable_color_perspective)
    The transparency axis derivation drops entirely. The
    use_dual_source axis moves to the caller because it depends
    on render_mode + filter + supports_dual_source_blend - the
    same formula the PSO blend state needs, so computing it once
    at the call site is more honest than splitting the
    computation across two locations. The static table shrinks
    from k_blobs[2][2][3][2] (24 entries) to k_blobs[2][3][2]
    (12 entries).

    GetBatchPipeline call site computes use_dual_source via the
    full shadergen formula (m_supports_dual_source_blend &&
    ((render_mode != Disabled && render_mode != OnlyOpaque) ||
     filter != Nearest)) and hands the resulting bit to both
    PickBatchUntexturedFSBytecode and (later in the same
    function) the PSO blend-state setup that referenced SRC1_*.

What does NOT change in this commit:

  - PSO matrix shape stays <5, 9, 4, 2, 7> = 2520 PSOs. The
    [render_mode] dim survives because PSO blend state still
    varies per BatchRenderMode value - this commit only
    eliminates the FS bytecode dependency on render_mode for
    the untextured slice.

  - Behavioural diff: zero. Both the old t* and new d* DXBC
    blobs run the same instructions for the active render_mode
    selected at draw time. The old encoding baked the
    transparency choice in fxc /D; the new encoding reads it
    from u_render_mode at runtime. fxc -O3 collapses the
    uniform-flow branch on u_render_mode to the same primitive
    ops the compile-time #if used to produce - just one extra
    LE/EQ + JZ pair guarding the premultiply / dual_source
    arm. Compiled DXBC instruction count grows by 2-3 ops per
    variant (negligible).

  - Two-pass NeedsTwoPassRendering still re-uploads the UBO
    between the OnlyOpaque and OnlyTransparent DrawInstanced
    calls (FlushRender path landed at c532a34). The pre-baked
    DXBC reads the new u_render_mode value on each draw, so
    each pass sees the matching enum.

  - m_batch_fragment_shader_blobs's [render_mode] dim stays
    4-wide. For the untextured slice the dim is unused (the
    slot is null - pre-bake bypasses the matrix); for the
    textured slices it still holds distinct FS bytecode per
    render_mode pre-textured-pre-bake landing. A future
    cleanup can collapse it to 1 once we trust the C++
    shadergen output to be invariant under render_mode (it is,
    post-c532a34, but the matrix-shrink is structurally
    independent of this slice's re-bake).

Test:
  - g++ -std=c++17 -c on src/common/d3d12/embedded_shaders.cpp
    compiles cleanly with the 12 new includes.
  - g++ -std=c++17 syntax check on PickBatchUntexturedFSBytecode
    against a shim D3D12_SHADER_BYTECODE / nested
    GPU_HW::BatchRenderMode / GPUTextureFilter compiles cleanly.
  - fxc /T ps_5_0 /E main /O3 produces all 12 expected DXBC
    blobs without warnings (Wine warnings ignored per repo
    convention).
  - Boot any PSX title on D3D12 default settings (Nearest, no
    PGXP, no MSAA): the untextured slice picks
    k_batch_untextured_ps_d0_none_p0 for non-trans render_mode
    and the SAME blob for trans render_mode (since dual=false
    for Nearest + non-trans/OnlyOpaque). Two-pass semitransparent
    geometry sees u_render_mode change between OnlyOpaque and
    OnlyTransparent passes via the c532a34 re-upload path.
    Visual diff: zero.
  - Filter flip Nearest -> Bilinear: use_dual_source flips to
    true for ALL render_modes (since filter != Nearest at the
    || arm), picking the d1_* variants for every transparency
    mode. No D3DCompile fired for the untextured slots.

Cumulative pre-bake variant count progress for D3D12 batch FS:

  Pre-arc:                                    75 264 (every axis a -D)
  Post-cbuffer-routing of:
    DITHERING/INTERLACING/UV_LIMITS/PGXP_DEPTH 4 704
  Post-c532a34 (TRANSPARENCY -> u_render_mode) 1 176
  Post-this-commit (untextured re-baked):
    Untextured slice:        12 (was 24)
    Textured slices target:  72 each x 4 = 288 (still TBD)
    Grand total when textured baked: 300

Next:
  - Pre-bake the 4 textured filter slices (Nearest / Bilinear /
    JINC2 / xBR) on the routed matrix. 72 variants per template
    via the same dual / interp / persp axes plus the 6-value
    texture_mode axis (palette / palette8bit / raw combos -
    Disabled and the Reserved_*Direct16Bit dedups skipped). Each
    slice gets a foundation commit (HLSL + regen + .inc) and an
    activation commit (extern decls + picker extension +
    GetBatchPipeline wiring), mirroring the untextured arc.
2026-05-19 08:56:03 +02:00
LibretroAdmin
c532a34871 core: route TRANSPARENCY through the batch UBO
Cross-backend cbuffer-routing for the 4-value BatchRenderMode
enum that drove the batch FS body's transparency-mode branches.
Previously TRANSPARENCY / TRANSPARENCY_ONLY_OPAQUE /
TRANSPARENCY_ONLY_TRANSPARENT lived as 3 compile-time DefineMacro
calls on the C++ shadergen path and 3 specialisation constants
(constant_id 100 / 101 / 102) on the Vulkan pre-baked SPIR-V
path. Both encodings collapsed the 4 enum values across 3
booleans:

  TransparencyDisabled (0): TRANS=0, OO=0, OT=0
  TransparentAndOpaque (1): TRANS=1, OO=0, OT=0
  OnlyOpaque           (2): TRANS=1, OO=1, OT=0
  OnlyTransparent      (3): TRANS=1, OO=0, OT=1

Post-routing the 3 macros / spec consts are gone. The FS body
branches on u_render_mode (4-state uint at cbuffer offset 60 -
the former u_pad2 reserved slot) and derives the three former
booleans inline as (u_render_mode != 0u), (u_render_mode == 2u),
(u_render_mode == 3u). PSO blend state still varies per
BatchRenderMode at the GraphicsPipelineBuilder level
(m_batch_pipelines's [render_mode] matrix dim stays); the win
is purely on the FS bytecode side - 4x fewer distinct DXBC /
SPIR-V variants per (filter, render, texture) cell.

What changes:

  src/core/gpu_hw.h
    BatchUBOData: rename u_pad2 -> u_render_mode at offset 60.
    Same 64-byte total cbuffer size, same alignment, same offset
    for every other field. No external ABI break - the binding
    layout is opaque-blob-shaped from the host's perspective.

  src/core/gpu_hw_shadergen.cpp
    Drop the 3 DefineMacro calls. WriteBatchUniformBuffer's
    field list trades u_pad2 for u_render_mode. The FS body's
    `#if TRANSPARENCY ... #elif TRANSPARENCY && TEXTURED ...
    #else` pre-routing structure (with inner
    `#if TRANSPARENCY_ONLY_OPAQUE / #if TRANSPARENCY_ONLY_TRANSPARENT`
    discards) collapses to:

      if (u_render_mode != 0u) {
        if (semitransparent) {
          // emit colour + dual-source aux + optional OO discard
          if (u_render_mode == 2u) discard;
        } else {
          // emit colour + dual-source aux + optional OT discard
          if (u_render_mode == 3u) discard;
        }
      } else {
        // emit colour + dual-source aux without transparency premultiply
      }

    The TEXTURED gate stays compile-time - it's a structural
    shader-source dimension (texture_mode-dependent shadergen
    branch that decides whether to emit SampleFromVRAM /
    palette / texture-window helpers at all).

  src/core/gpu_hw.cpp
    FlushRender now stamps u_render_mode in m_batch_ubo_data
    before each draw:
      - Single-pass: set u_render_mode = m_batch.GetRenderMode(),
        mark dirty if it changed, then the existing
        m_batch_ubo_dirty check uploads. Same upload count as
        pre-routing for the common case.
      - Two-pass NeedsTwoPassRendering(): set u_render_mode =
        OnlyOpaque, upload, draw; set u_render_mode =
        OnlyTransparent, upload, draw. +1 ~64-byte UBO upload
        per two-pass FlushRender. Worth it - the FS variant
        matrix collapses by 4x on every backend that pre-bakes.

  src/core/gpu_hw_shadergen.cpp (premultiply branch)
    `#if TRANSPARENCY` -> `if (u_render_mode != 0u)` for the
    src_alpha_factor premultiply.

  src/core/gpu_hw_vulkan.cpp
    Drop the 3 fs_spec.AddBool calls for id=100/101/102 from
    the batch FS pipeline build. RESOLUTION_SCALE / DITHERING /
    INTERLACING / DITHERING_SCALED / TRUE_COLOR / PALETTE_*BIT /
    RAW_TEXTURE / BINALPHA stay (their spec const semantics
    are unchanged). The local `brm` BatchRenderMode local
    drops too; render_mode reaches the FS via u_render_mode
    in the cbuffer, populated by gpu_hw.cpp before each draw.

  data/shaders/vulkan/batch_untextured.frag.glsl
  data/shaders/vulkan/batch_textured_nearest.frag.glsl
  data/shaders/vulkan/batch_textured_bilinear.frag.glsl
  data/shaders/vulkan/batch_textured_jinc2.frag.glsl
  data/shaders/vulkan/batch_textured_xbr.frag.glsl
    5 Vulkan FS templates drop the 3 spec const declarations
    (constant_id = 100/101/102), add u_render_mode to the
    UBO declaration (layout(offset = 60) uint u_render_mode),
    and replace TRANSPARENCY / _ONLY_OPAQUE / _ONLY_TRANSPARENT
    body references with (u_render_mode != 0u) / (==2u) /
    (==3u). Same shape changes as the prior cbuffer-routing
    arc commits (DITHERING, INTERLACING, UV_LIMITS, PGXP_DEPTH).

  src/common/vulkan/embedded_spirv/batch_*_fs_*.inc (60 files)
    All 60 batch FS SPIR-V blobs regenerated. The SPIR-V
    instruction count grows by ~30-50 words per blob (one UBO
    field load + 1-3 compare + branch instructions per former
    spec-const reference site - 5 sites per untextured / 4-7
    sites per textured filter family). Net SPIR-V footprint
    grows by ~50-100 KiB across the 60 blobs.

  src/common/vulkan/embedded_spirv/batch_vs_*.inc (12 files)
    Untouched - the VS doesn't reference TRANSPARENCY.

What does NOT change in this commit:

  - D3D12 pre-baked untextured DXBC (24 blobs at
    batch_untextured_ps_*.inc) stays as-is. The blobs were
    built from data/shaders/d3d12/batch_untextured.ps.hlsl
    pre-this-commit with TRANSPARENCY as a `-D` macro; they
    still produce correct output because each baked variant
    has the right transparency behavior baked in, and the
    PickBatchUntexturedFSBytecode picker still selects the
    right variant from render_mode. The new u_render_mode
    cbuffer field is set in the UBO but the pre-baked DXBC
    doesn't read it (it doesn't reference the field).
    A follow-up commit can re-bake those 24 blobs with the
    routed HLSL, dropping the transparency axis to take the
    untextured slice from 24 -> 12 variants (or 6 if we also
    drop the dual_source axis for untextured-where-it-doesn't-
    matter cases - the d=true case for untextured non-trans
    is reachable only when filter != Nearest which is rare).
    Not blocking on this commit.

  - PSO matrix shape stays <5, 9, 4, 2, 7> = 2520 PSOs (filter
    x texture x render x depth_test x transparency). The
    [render_mode] dim stays because PSO blend state still
    varies per BatchRenderMode value. m_batch_fragment_shader_blobs
    stays <9, 4, 7> = 252 slots, though now the [render_mode]
    dim's 4 slots can store identical FS bytecode if every
    backend's runtime compile path produces the same DXBC for
    all 4 render_modes (which it will, post-routing). A
    follow-up could shrink the dim from 4 to 1 once we trust
    the C++ shadergen output to be invariant under render_mode.

  - The C++ shadergen TEXTURED branch is still compile-time.
    Routing TEXTURED to cbuffer would require declaring all
    textured-only helpers (SampleFromVRAM, ApplyTextureWindow,
    FloatToIntegerCoords) and the texture binding even for
    untextured shaders - bigger architecture change without
    proportional payoff.

  - render_mode dependency at the call site GetBatchFragmentShader
    on the D3D12 side. The function still takes render_mode as
    a parameter so it can hash into the right slot in
    m_batch_fragment_shader_blobs. The shader source it
    generates is now invariant under render_mode (the shadergen
    call's transparency parameter is unused for the FS body),
    so technically the 4 [render_mode] slots end up holding
    identical ID3DBlob refs. Cleaning that up is the follow-up
    "shrink [render_mode] dim" work mentioned above.

Test:
  - glslangValidator on all 5 Vulkan FS templates: passes.
  - python3 tools/regen_vulkan_spirv.py: produces 91 SPIR-V
    blobs (12 VS + 60 FS + 19 non-batch), all valid.
  - Boot any PSX title on Vulkan: render_mode flips per-batch
    pick up the new u_render_mode value via cbuffer. Two-pass
    semitransparent draws (e.g. Final Fantasy menus, MGS
    smoke) see the matching enum value on each pass via the
    +1 UBO re-upload between OnlyOpaque / OnlyTransparent
    DrawIndexed calls. Visual diff: zero.
  - Boot any PSX title on D3D11 / D3D12 / OpenGL: the
    runtime-compiled FS bytecode loses the 3 TRANSPARENCY
    macros and gains a u_render_mode runtime branch. Pre-
    baked D3D12 untextured slots still work via the picker.
    Visual diff: zero.
  - PGXP-flip mid-session: independent of this commit;
    behaviour unchanged.

Next:
  - Re-bake D3D12 untextured DXBC with the routed HLSL
    template (24 -> 12 variants, drop the transparency axis).
    Small follow-up.
  - Pre-bake the 4 textured filter slices on the routed
    matrix: 72 variants per template (3 interp x 2 persp x 2
    dual x 6 texture_mode) instead of 288 with TRANSPARENCY
    as a `-D` axis. Each slice gets a foundation commit
    (HLSL + regen + .inc) and an activation commit
    (extern decls + picker + GetBatchPipeline wiring),
    mirroring the untextured arc.
2026-05-19 08:13:03 +02:00
LibretroAdmin
9e6f933c98 core/d3d12: pre-bake the untextured batch FS slice
First batch-FS pre-bake foundation commit. Lands the HLSL
template + regen recipe + generated DXBC blobs for the
untextured slice of GPU_HW_ShaderGen::GenerateBatchFragmentShader
(texture_mode == GPUTextureMode::Disabled). The matching C++
side that consumes these blobs - extern declarations in
src/common/d3d12/embedded_shaders.{h,cpp} and the
GetBatchFragmentShader call site in gpu_hw_d3d12.cpp - lands
in a follow-up so this commit is reviewable as a pure
data/recipe drop without behavioural change.

What's here:

  data/shaders/d3d12/batch_untextured.ps.hlsl
    New HLSL template. Hand-translated mirror of the shadergen
    output for the untextured slice, with all the cbuffer-
    routed axes consumed at runtime (u_dithering,
    u_interlacing, u_uv_limits, u_pgxp_depth, u_true_color,
    u_scaled_dithering, u_resolution_scale - per the routing
    arc 3af8e02 / eb42ffb / 8b43518 / 49c0f82 / 116a70e and
    earlier). The 64-byte cbuffer field order matches
    GPU_HW::BatchUBOData in gpu_hw.h:111 exactly so the C++
    side can keep its existing WriteBatchUniformBuffer push
    untouched.

    Variant axes (5, all `-D` to fxc):
      * TRANSPARENCY (0/1)
      * USE_DUAL_SOURCE (0/1)
      * INTERP_CENTROID (mutually exclusive with INTERP_SAMPLE)
      * INTERP_SAMPLE (wins over INTERP_CENTROID when set)
      * NOPERSP (0/1)

    The 4-value BatchRenderMode enum funnels onto 2
    TRANSPARENCY values for the untextured slice -
    TRANSPARENCY_ONLY_OPAQUE / TRANSPARENCY_ONLY_TRANSPARENT
    never reach this template (the textured FS is where the
    discard arms live). Per the shadergen comment at
    gpu_hw_shadergen.cpp:1148: "We shouldn't be rendering
    opaque geometry only when untextured, so no need to test/
    discard here."

    The MSAA cardinality axis (m_multisamples 1/2/4/8/16/32)
    does NOT multiply the untextured slice. MSAA only affects
    the input interpolation qualifier (centroid for MSAA-no-
    per-sample, sample for MSAA-with-per-sample) which the
    INTERP_* axes capture; the LOAD_TEXTURE_MS sample-resolve
    loop unroll that drives the multisamples multiplier on
    the textured filter templates has no equivalent here
    (there's no texture sample to resolve).

  tools/regen_d3d12_dxbc.py
    TEMPLATE_VARIANTS entry for batch_untextured.ps.hlsl
    enumerating the 24 valid (transparency x dual x interp x
    persp) combinations. Variant suffix
    "t{0,1}d{0,1}_{none,centroid,sample}_p{0,1}" sorts
    alphabetically into the natural nested-loop iteration
    order the matching helper at embedded_shaders.cpp will
    use.

  src/common/d3d12/embedded_dxbc/batch_untextured_ps_*.inc
    24 generated DXBC blobs. fxc /T ps_5_0 /E main /O3 with
    the corresponding /D defines per variant. Compiles
    cleanly under #include <cstdint>; the empty `(no /D)`
    autogenerated comment on t0d0_none_p0.inc is expected -
    that's the all-zero default.

Size sanity (the empirical numbers Lib asked about pre-decision):

  Per-variant DXBC: 2956..3100 bytes (mean 3022, tight
                    spread - the structural differences here
                    are small).
  Per-variant .inc:  ~19 KiB (6.3x text-encoding overhead).
  Untextured slice total: 24 .inc files, ~445 KiB on disk,
                          ~72 KiB embedded DXBC.

  The 3-KiB-per-variant figure is far smaller than the
  ~20-KiB pessimistic estimate from the pre-bake-scope
  analysis. Implications for the eventual full bake:
    - Untextured slice: ~72 KiB DXBC, ~445 KiB .inc text.
    - Textured filter templates will be larger (texture
      sampling + filter math) but probably 5-15 KiB each
      rather than 20+ KiB. Final full-bake total likely
      lands in the 80-150 MiB .inc / 15-25 MiB embedded
      DXBC range - aligned with the Option 1 estimate but
      not blown out.

Why this slice first:

  Untextured is the simplest batch FS template. No
  SampleFromVRAM call, no palette / raw_texture branches,
  no UV-limits clamping, no texture filtering body. The
  remaining 4 templates (Nearest / Bilinear / JINC2 / xBR)
  add increasing layers of complexity on top of the same
  cbuffer + main()-body skeleton; landing untextured first
  pins the conventions (cbuffer layout, interp / persp /
  dual macro shapes, variant suffix encoding, regen entry
  format) so the textured slices land as more-of-the-same
  PRs rather than each one redesigning the shape.

Test:

  - tools/regen_d3d12_dxbc.py runs cleanly on Linux+Wine
    against the fxc_lite toolchain at /tmp/fxc_lite/. 24
    DXBC blobs produced; existing 76 .inc files unchanged
    (76 + 24 = 100 total, matching the regen tool's
    "regenerated 100 .inc file(s)" log line).

  - g++ -std=c++17 -c on a wrapper file
    (#include <cstdint><cstddef>, namespace test { ...
    .inc content ... }) compiles cleanly. Same shape as
    embedded_shaders.cpp's actual #include site so the
    follow-up commit's translation unit will compile too.

  - No runtime path consumes these blobs yet - they sit
    on disk waiting for the C++ side. PSO compile path
    still goes through D3DCompile via the shadergen.
    Behavioural diff for this commit: zero.

Next:

  - C++ side: add the 24 extern declarations to
    src/common/d3d12/embedded_shaders.h, the
    EmbeddedShaderBlob table and GetBatchUntexturedFragmentShaderBlob
    helper to embedded_shaders.cpp, and wire
    GPU_HW_D3D12::GetBatchFragmentShader's untextured
    branch to pick from the table instead of calling
    shadergen.GenerateBatchFragmentShader. Smaller commit;
    activates this slice.

  - Then the 4 textured filter templates, one slice per
    commit, each repeating the same shape.
2026-05-19 06:26:35 +02:00
LibretroAdmin
f4e1af39e2 core/vulkan: route the VS PGXP_DEPTH spec const to u_pgxp_depth
Completes the PGXP_DEPTH cbuffer-routing arc on the Vulkan
side. The batch VS used to declare PGXP_DEPTH as a
specialisation constant at constant_id = 3, switched via
vs_spec.AddBool(3, m_pgxp_depth_buffer) at pipeline build,
with the body using a `PGXP_DEPTH ? a_pos.w : a_pos.z` static
ternary on the spec const value. The C++ shadergen VS was
already routed in 49c0f82 and the Vulkan FS in the preceding
commit; this aligns the Vulkan VS with both.

Spec constants are not SPIR-V structural axes - one VS blob,
two VkPipelines per PGXP value (the spec const value is baked
in at vkCreateGraphicsPipelines time, producing two
specialised executables from the same module). So the
pre-routing footprint was small: zero extra .inc files, but
two PSO objects per VS variant rather than one, and a PSO
rebuild on m_pgxp_depth_buffer flip to swap them. Post-
routing, the VS becomes a single PSO across the flip (just a
4-byte UBO read picked up at next FlushRender).

What changes:

  data/shaders/vulkan/batch.vert.glsl
    - Drops `layout(constant_id = 3) const bool PGXP_DEPTH = false;`
    - Adds a Batch UBO declaration at set=0 binding=0 with a
      single `layout(offset = 52) uint u_pgxp_depth;` field.
      Matches the std140 offset of the C++ BatchUBOData
      member (gpu_hw.h:111). The pipeline layout
      m_batch_pipeline_layout already exposes the UBO for
      the FS - the VS just reads from the same descriptor.
    - Body: `pos_z = PGXP_DEPTH ? a_pos.w : a_pos.z` ->
            `pos_z = (u_pgxp_depth != 0u) ? a_pos.w : a_pos.z`
    - Header docstring: drops the constant_id=3 section,
      describes the routing.

  src/common/vulkan/embedded_spirv/
    - 12 batch_vs_*.inc files regenerated with the UBO read
      + uniform branch in place of the spec const ternary.
      Each .inc grows ~100 SPIR-V words (UBO declaration,
      load, branch). Total VS SPIR-V footprint grows by
      ~5 KiB across 12 blobs.

  src/core/gpu_hw_vulkan.cpp
    - Drops `vs_spec.AddBool(3, m_pgxp_depth_buffer);` from
      the batch-pipeline build site. RESOLUTION_SCALE
      (id=0) remains as a VS spec const.

What this completes:

  PGXP_DEPTH is now fully routed end-to-end on both
  pipeline-emission paths across all four backends:

    D3D11 / D3D12 / OpenGL (C++ shadergen):
      VS routed in 49c0f82 (u_pgxp_depth runtime ternary)
      FS routed in 116a70e (always-emit SV_Depth + runtime
        ternary in the consolidated end-of-main write)

    Vulkan (pre-baked SPIR-V):
      VS routed in this commit (u_pgxp_depth runtime
        ternary, replacing the spec const)
      FS routed in dae19ff (always-write gl_FragDepth +
        runtime ternary; 120 -> 60 SPIR-V blobs across the
        5 FS templates)

  Cost matrix on PGXP toggle across the four backends is
  now governed by the PSO depth comparison func dependency
  only:

    D3D11 / D3D12 / OpenGL: PSO depth-comp built in to the
      PSO immutable state (gpu_hw_d3d12.cpp:1317-1319 picks
      LESS_EQUAL vs GREATER_EQUAL on m_pgxp_depth_buffer at
      PSO build time). Flipping PGXP still triggers a PSO
      rebuild, but FS bytecode is cache-served (invariant
      across the flip).

    Vulkan: depth comparison func is selected per-draw via
      the depth_test PSO matrix dim (depth_test=1 ->
      GREATER_OR_EQUAL, depth_test=2 -> LESS_OR_EQUAL, see
      gpu_hw_vulkan.cpp:2233). The C++ side chooses the
      depth_test index based on m_pgxp_depth_buffer at draw
      time without rebuild. Vulkan no longer needs ANY
      shader / PSO rebuild on PGXP toggle.

  m_pgxp_depth_buffer stays in shader_source_changed for
  the C++ shadergen path's PSO rebuild requirement. A
  Vulkan-aware cleanup that decouples Vulkan from this
  trigger (since it no longer needs it) is a separate
  follow-up; the current state is correct but wastes a
  Vulkan pipeline cache lookup on flip.

Test:
  - Build: tools/regen_vulkan_spirv.py runs cleanly. The
    new VS reads the UBO; SPIR-V validation passes.
  - Boot any title on Vulkan, PGXP-depth off: u_pgxp_depth
    = 0, VS picks a_pos.z (mask-bit Z) path. Output
    identical to pre-commit.
  - Toggle PGXP-depth on: u_pgxp_depth flips to 1; VS
    picks a_pos.w (PGXP-replayed depth) path on next draw.
    No pipeline rebuild on the VS side, no PSO rebuild
    overall on Vulkan (the depth_test index also switches
    via the matrix). Output identical to pre-commit PGXP-
    on state.
  - PGXP-corrected geometry sanity (Tekken 3, Tomb Raider
    1/2, Soul Blade): the runtime branch should produce
    byte-identical depth values to the pre-routing spec-
    const-baked variant.

Next:
  - per_sample / dcp collapse decisions (forced-always
    settings or keep as variant axes - these are
    interpolation qualifiers that can't be cbuffer-routed,
    only collapsed via settings).
  - Pre-bake the resulting D3D12 FS matrix.
2026-05-19 04:25:05 +02:00
LibretroAdmin
dae19ff67b core/vulkan: route the FS PGXP_DEPTH axis to a cbuffer scalar
Follow-up to 116a70e (C++ shadergen FS-side PGXP_DEPTH
routing) which brought D3D11 / D3D12 / OpenGL on. This commit
parallels it on the Vulkan path: collapses the PGXP_DEPTH
SPIR-V-structural axis from all 5 batch FS templates
(untextured + nearest + bilinear + jinc2 + xbr), making
gl_FragDepth always written with a runtime branch on
u_pgxp_depth.

Vulkan FS templates each had PGXP_DEPTH as one of their four
structural axes (3 interp x 2 persp x 2 dual x 2 pgxp = 24
blobs per template, 120 total batch FS blobs). After this
commit each template has 3 x 2 x 2 = 12 blobs, 60 total.

Routing for each template follows the same shape:

  GLSL UBO grows by u_pgxp_depth at offset 52. For the
  filter family (bilinear / jinc2 / xbr) the UBO previously
  declared the first 6 fields and stopped at offset 32; this
  commit adds a `layout(offset = 52) uint u_pgxp_depth;`
  trailing field to skip past the spec-const-handled
  intermediate slots (u_resolution_scale through
  u_interlacing). For Nearest the UBO already had
  u_uv_limits at offset 56; u_pgxp_depth slots in before it
  at offset 52. For the untextured template the UBO
  previously declared only the first 6 fields; same trailing
  layout(offset=52) treatment.

  GLSL FS body: drop the
    #if !defined(PGXP_DEPTH)
      gl_FragDepth = oalpha * gl_FragCoord.z;
    #endif
  block, replace with the unconditional
    gl_FragDepth = (u_pgxp_depth != 0u)
                     ? gl_FragCoord.z
                     : (oalpha * gl_FragCoord.z);
  Same expression shape as the C++ shadergen routing in
  116a70e but using Vulkan's gl_FragCoord.z (rasterizer-
  interpolated depth) instead of HLSL's v_pos.z.

  Regen tool: drop pgxp_axes from each of the three
  variant-generator helpers
  (_batch_fs_untextured_variants,
  _batch_fs_textured_nearest_variants,
  _batch_fs_textured_filter_variants - the last shared by
  bilinear / jinc2 / xbr). Filename suffix shortens from
  `_<interp>_<persp>_<dual>_<pgxp>` to
  `_<interp>_<persp>_<dual>`.

  embedded_spirv/: 120 obsolete files deleted (60 _pgxpoff
  + 60 _pgxpon, 24 per template), 60 new files added (12
  per template) carrying the runtime-branch SPIR-V. Total
  .inc count: 151 -> 91.

  embedded_shaders.{h,cpp}: 5 FS blob tables drop from [24]
  to [12]. 5 helper functions
  (GetBatch{Untextured,TexturedNearest,TexturedBilinear,
  TexturedJINC2,TexturedXBR}FragmentShaderBlob) drop the
  bool pgxp_depth parameter. Index encoding becomes
    index = interp*4 + persp*2 + dual  (0..11)
  (was: interp*8 + persp*4 + dual*2 + pgxp). 60 #include
  lines collapse to 60 (renamed, no count change for that
  block, just filename change).

  gpu_hw_vulkan.cpp: 5 FS lookup call sites
  (GetBatchUntexturedFragmentShaderBlob and the four
  textured filter variants in the switch on `filter`) drop
  their sixth argument (m_pgxp_depth_buffer).

What does NOT change in this commit:

  - The Vulkan batch VS still uses PGXP_DEPTH as a spec
    constant (id=3, declared in batch.vert.glsl, set via
    vs_spec.AddBool(3, m_pgxp_depth_buffer) at pipeline
    build). The C++ shadergen VS was routed to u_pgxp_depth
    in 49c0f82 but the Vulkan VS still has spec const
    PGXP_DEPTH. A separate small follow-up can route the VS
    GLSL through u_pgxp_depth too; the cost matters less on
    Vulkan because spec consts don't fork SPIR-V (one VS
    blob, two pipelines per PGXP value), only PSO assembly,
    and Vulkan already has depth_test as a PSO matrix dim
    that swaps the depth comparison func at draw time
    without rebuild.

  - The non-batch FS spec const id=3 PGXP_DEPTH sites
    (gpu_hw_vulkan.cpp lines 2477, 2523, 2581 - the
    vram_fill, vram_copy, vram_write_* paths) are
    unrelated to the batch FS arc. Those shaders genuinely
    declare PGXP_DEPTH as a spec const
    (data/shaders/vulkan/vram_fill.frag.glsl:80 etc.) and
    have their own pre-bake plans.

  - m_pgxp_depth_buffer STAYS in shader_source_changed
    across all backends. The Vulkan FS bytecode is now
    invariant under it but the C++ shadergen FS still has
    PSO-state dependency (gpu_hw_d3d12.cpp:1317-1319 picks
    LESS_EQUAL vs GREATER_EQUAL based on
    m_pgxp_depth_buffer at PSO build time), so PSO rebuild
    on flip is still required for D3D11 / D3D12 / OpenGL.
    Vulkan handles the depth-comp swap via its depth_test
    PSO matrix dim (depth_test=1 GREATER_OR_EQUAL,
    depth_test=2 LESS_OR_EQUAL - see
    gpu_hw_vulkan.cpp:2233's depth_test_values array) so
    Vulkan doesn't actually need a rebuild on flip
    post-this-commit. Decoupling the cross-backend
    rebuild trigger from Vulkan's no-longer-needing-it
    is a separate cleanup.

Variant-count delta:

  Per-template Vulkan FS: 24 -> 12 SPIR-V blobs.
  Across 5 templates: 120 -> 60 SPIR-V blobs.
  Pipeline cache footprint drops by 60 FS pipelines per
  (filter, render, texture) cell - actual PSO count depends
  on how many of those cells get exercised in practice but
  the savings are proportional.

Disk SPIR-V footprint:

  Before: 151 .inc files, ~11.3 MiB.
  After:  91 .inc files, ~7.5 MiB. (-60 files, ~3.8 MiB.
          The new files are slightly larger than the old
          _pgxpoff ones - they always emit the gl_FragDepth
          write and the runtime branch - but smaller than
          two old files combined, so net footprint drops.)

Cost matrix delta (Vulkan):

  Per-session m_pgxp_depth_buffer flip:
    Pre-commit: 60 batch FS pipelines per (filter, render,
                texture) cell rebuilt to swap from
                _pgxpoff -> _pgxpon. Tens of ms on cold
                pipeline cache, ~0 with warm cache.
    Post-commit: 0 FS pipeline rebuilds. The u_pgxp_depth
                 UBO write picks up at next FlushRender;
                 the FS bytecode is invariant across the
                 flip. The PSO depth-comp swap is handled
                 by Vulkan's depth_test matrix axis (and
                 the C++ side passes the right
                 depth_test index based on
                 m_pgxp_depth_buffer at draw time).

  Cold launch precompile pass on Vulkan: 60 fewer batch FS
  pipelines to enumerate / create.

  Steady-state runtime cost (Vulkan batch FS):
    + 4-byte UBO read on every fragment.
    + One uniform branch on u_pgxp_depth (cbuffer scalar,
      uniform across the warp) - resolves to a predicated
      select on modern hardware.
    + The early-Z TEST trade discussed in 116a70e's commit
      message applies: pre-commit, the _pgxpon variants on
      Vulkan retained early-Z TEST (no FS depth write);
      post-commit those variants always write gl_FragDepth
      so early-Z TEST is disabled. For PSX content on
      modern GPUs the cost is sub-noise (low overdraw,
      cheap FS, painter's-sorted geometry, depth values
      that are essentially random and Hi-Z can't compress).

Test:
  - Build: tools/regen_vulkan_spirv.py runs cleanly,
    producing 91 blobs. Embedded_shaders.cpp compiles
    against the smaller tables. gpu_hw_vulkan.cpp links
    clean.
  - Boot any PSX title on Vulkan with PGXP-depth off:
    u_pgxp_depth=0, the FS ternary picks oalpha *
    gl_FragCoord.z. Output identical to pre-commit.
  - Toggle PGXP-depth on: u_pgxp_depth flips to 1 via the
    existing UpdateHWSettings cbuffer push. The depth_test
    index passed to the PSO lookup also changes to select
    the LESS_OR_EQUAL pipeline. FS picks the
    gl_FragCoord.z pass-through path. No pipeline rebuild
    fired on Vulkan. Output identical to pre-commit
    PGXP-on state.
  - Mask-bit-heavy titles (Crash 1/2/3, Final Fantasy
    combat backgrounds, Silent Hill 1 fog plane): visual
    sanity check that the mask-bit-depth-encode path still
    works at all 5 filters (Nearest baseline, Bilinear,
    JINC2, xBR, BilinearBinAlpha).
  - PGXP-corrected titles (Tekken 3, Tomb Raider 1/2,
    Soul Blade): visual sanity that the pass-through write
    is value-equivalent to the omit-gl_FragDepth-declaration
    pre-routing behaviour. Watch for z-fighting on
    co-planar stage geometry.

Next:
  - Optionally route the Vulkan VS PGXP_DEPTH spec const
    through u_pgxp_depth for parity (small follow-up; cuts
    one spec const per VS pipeline build).
  - per_sample / dcp collapse decisions.
  - Pre-bake the resulting D3D12 FS matrix.
2026-05-19 04:14:04 +02:00
LibretroAdmin
c6e9f87be0 core/vulkan: route the Nearest FS UV_LIMITS axis to a cbuffer scalar
Follow-up to 078783e (Vulkan VS UV_LIMITS collapse) and the
earlier 8b43518 (C++ shadergen UV_LIMITS routing). Brings the
Vulkan Nearest FS into the same shape as the C++ shadergen
routing - UV_LIMITS is no longer a SPIR-V-structural axis on
the Vulkan side; the Nearest FS source has a single shape and
runtime-branches on u_uv_limits.

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

What changes:

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

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

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

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

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

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

What this completes:

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

Cost matrix delta:

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

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

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

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

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

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

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

What changes:

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

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

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

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

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

What does NOT change in this commit:

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

Variant-count delta:

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

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

Cost matrix delta (Vulkan):

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

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

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

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

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

Variant axes (4 axes, 54 unique blobs):

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

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

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

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

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

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

Runtime selection in GetDisplayPipeline:

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

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

Files added:

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

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

Files modified:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Files added:

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

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

Files modified:

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

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

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

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

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

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

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

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

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

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

Files added:

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

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

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

Files modified:

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

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

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

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

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

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

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

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

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

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

Variant axis breakdown:

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

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

Files added:

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

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

Files modified:

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

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

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

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

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

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

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

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

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

Files added:

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

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

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

Files modified:

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

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

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

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

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

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

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

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

Output deltas vs the failed cd971cd attempt:

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

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

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

This restores the full set of changes that cd971cd shipped:

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

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

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

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

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

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

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

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

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

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

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

Auto-detection improvements (find_fxc):

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

Output validator (extract_rdef_creator + check_microsoft_creator):

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

What the validator does NOT do:

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

README changes:

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

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

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

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

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

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

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

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

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

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

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

Why pre-bake vram_copy_ps:

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

Files added:

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

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

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

Files modified:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Files in this patch:

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

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

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

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

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

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

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

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

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

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

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

Files in this patch:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Files in this scaffold patch:

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

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

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

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

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

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

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

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

Subsequent patches in this arc will each:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

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

Shader converted:

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

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

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

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

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

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

Notes on spec-const evaluation:

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

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

Cumulative state after this patch:

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

Smoke tests in the sandbox:

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

Smoke tests at user's end:

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

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

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

Shaders converted by this patch:

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

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

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

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

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

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

Cumulative state after this patch:

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

Smoke tests in the sandbox:

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

Smoke tests at user's end:

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

Infrastructure additions:

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

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

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

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

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

Shaders converted by this patch:

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

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

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

Other changes in gpu_hw_vulkan.cpp:

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

Cumulative state after this patch:

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

Smoke tests in the sandbox:

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

Smoke tests at user's end:

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

Shaders converted by this patch:

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

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

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

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

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

Cumulative state after this patch:

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

Smoke tests in the sandbox:

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

Smoke tests at user's end:

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

What this patch does:

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

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

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

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

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

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

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

What this patch does NOT do:

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

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

Smoke tests performed in isolation:

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

Smoke tests that require the user's setup:

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

Adds the following cheats to Gran Turismo 1 & 2:

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

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

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

* Update chtdb.txt

Added:

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

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

Removed:

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

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

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

* Update chtdb.txt

Removed unnecessary line break
2021-11-28 11:22:04 +10:00
bslenul
19f7516b5b Update database for GunCon/Justifier games 2021-11-27 14:26:57 +01:00
Chromaryu
23a34cb119
GameDB: Add missing definition 2021-11-25 15:11:12 +09:00
Connor McLaughlin
10cf8ef638 GameSettings: Force true color off for SLPS-00078 2021-11-14 23:47:15 +10:00