TsundereStation/data
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
..
shaders core: pre-bake the downsample fragment shaders 2026-05-19 11:11:06 +02:00