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.
This commit is contained in:
LibretroAdmin 2026-05-18 18:17:17 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 443a2a130b
5 changed files with 108 additions and 9 deletions

View file

@ -0,0 +1,25 @@
// Pre-baked copy/blit pixel shader for the D3D12 backend.
//
// Equivalent to ShaderGen::GenerateCopyFragmentShader() in
// src/core/shadergen.cpp under HostDisplay::RenderAPI::D3D12. Used
// by GetCopyPipeline for general full-frame blits (presentation
// copies, downscaling). No state dependency at all - the shader has
// no shadergen prelude, no MULTISAMPLING split, no RESOLUTION_SCALE
// bake-in, and reads its single source rect from the cbuffer at
// runtime. One pre-baked variant covers every caller.
cbuffer UBOBlock : register(b0)
{
float4 u_src_rect;
};
Texture2D samp0 : register(t0);
SamplerState samp0_ss : register(s0);
void main(
in float2 v_tex0 : TEXCOORD0,
out float4 o_col0 : SV_Target0)
{
float2 coords = u_src_rect.xy + v_tex0 * u_src_rect.zw;
o_col0 = samp0.Sample(samp0_ss, coords);
}

View file

@ -0,0 +1,62 @@
// Autogenerated from data/shaders/d3d12/copy.ps.hlsl.
// DO NOT EDIT. Regenerate with tools/regen_d3d12_dxbc.py.
// fxc /T ps_5_0 /E main /O3 (no /D)
//
// Included inside namespace D3D12::EmbeddedShaders in
// embedded_shaders.cpp. The matching extern declaration lives in
// embedded_shaders.h.
const uint8_t k_copy_ps[] = {
0x44, 0x58, 0x42, 0x43, 0x19, 0xeb, 0xec, 0xa1, 0x72, 0x68, 0xaf, 0x90, 0xfb, 0xba, 0xfa, 0xe4,
0xd2, 0xbe, 0x05, 0x38, 0x01, 0x00, 0x00, 0x00, 0x28, 0x03, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x30, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00,
0x49, 0x53, 0x47, 0x4e, 0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x54, 0x45, 0x58, 0x43, 0x4f, 0x4f, 0x52, 0x44,
0x00, 0xab, 0xab, 0xab, 0x4f, 0x53, 0x47, 0x4e, 0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x54,
0x61, 0x72, 0x67, 0x65, 0x74, 0x00, 0xab, 0xab, 0x52, 0x44, 0x45, 0x46, 0x64, 0x01, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0xbc, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
0x00, 0x05, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x52, 0x44, 0x31, 0x31,
0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
0x24, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9c, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0xb0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x73, 0x61, 0x6d, 0x70,
0x30, 0x5f, 0x73, 0x73, 0x00, 0xab, 0xab, 0xab, 0x73, 0x61, 0x6d, 0x70, 0x30, 0x00, 0xab, 0xab,
0x55, 0x42, 0x4f, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x00, 0xab, 0xab, 0xab, 0xd4, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x55, 0x42, 0x4f, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x00, 0xab, 0xab, 0xab,
0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x1c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x5f, 0x73, 0x72, 0x63, 0x5f, 0x72, 0x65,
0x63, 0x74, 0x00, 0xab, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x34, 0x00, 0xab, 0x01, 0x00, 0x03, 0x00,
0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x01, 0x00, 0x00,
0x76, 0x6b, 0x64, 0x33, 0x64, 0x2d, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x31, 0x2e, 0x31,
0x30, 0x20, 0x28, 0x57, 0x69, 0x6e, 0x65, 0x20, 0x62, 0x75, 0x6e, 0x64, 0x6c, 0x65, 0x64, 0x29,
0x00, 0xab, 0xab, 0xab, 0x53, 0x48, 0x44, 0x52, 0x1c, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
0x47, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x04, 0x46, 0x8e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x03, 0x00, 0x60, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x58, 0x18, 0x00, 0x04, 0x00, 0x70, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0x00, 0x00,
0x62, 0x10, 0x00, 0x03, 0x32, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x03,
0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00,
0x36, 0x00, 0x00, 0x05, 0x32, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x10, 0x10, 0x00,
0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x06, 0xf2, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00,
0x46, 0x8e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x05,
0xc2, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x04, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00,
0x36, 0x00, 0x00, 0x05, 0x32, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0xe6, 0x00, 0x10, 0x00,
0x01, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x07, 0x32, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x46, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07, 0x32, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe6, 0x00, 0x10, 0x00,
0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x09,
0xf2, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x46, 0x7e, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x36, 0x00, 0x00, 0x05, 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x0e, 0x10, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x01,
};
const size_t k_copy_ps_size_bytes = sizeof(k_copy_ps);

View file

@ -18,5 +18,6 @@
namespace D3D12::EmbeddedShaders {
#include "embedded_dxbc/fullscreen_quad_vs.inc"
#include "embedded_dxbc/copy_ps.inc"
} // namespace D3D12::EmbeddedShaders

View file

@ -51,4 +51,15 @@ namespace D3D12::EmbeddedShaders {
extern const uint8_t k_fullscreen_quad_vs[];
extern const size_t k_fullscreen_quad_vs_size_bytes;
// Copy/blit pixel shader. Equivalent to
// ShaderGen::GenerateCopyFragmentShader() in D3D12 mode. Single
// texture sample with a cbuffer-driven source rect; zero state
// dependency (no MULTISAMPLING split, no RESOLUTION_SCALE bake-in,
// no preprocessor variant axes). Used by GetCopyPipeline for full-
// frame blits (presentation copies, downscaling).
//
// Source: data/shaders/d3d12/copy.ps.hlsl
extern const uint8_t k_copy_ps[];
extern const size_t k_copy_ps_size_bytes;
} // namespace D3D12::EmbeddedShaders

View file

@ -1803,20 +1803,20 @@ GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetCopyPipeline()
const D3D12_SHADER_BYTECODE vs = GetFullscreenQuadVertexShader();
if (!m_shadergen)
{
Log_ErrorPrint("GetCopyPipeline called before CompilePipelines constructed the shadergen");
return {};
}
ComPtr<ID3DBlob> fs = m_shader_cache.GetPixelShader(m_shadergen->GenerateCopyFragmentShader());
if (!fs)
return {};
// Pre-baked DXBC blob, same shape as the VS - statically linked
// from src/common/d3d12/embedded_dxbc/copy_ps.inc, single variant,
// no shader_cache lookup, no D3DCompile, no m_shadergen
// dependency. The PSO compile below is the only remaining work.
const D3D12_SHADER_BYTECODE fs = {
D3D12::EmbeddedShaders::k_copy_ps,
D3D12::EmbeddedShaders::k_copy_ps_size_bytes,
};
D3D12::GraphicsPipelineBuilder gpbuilder;
gpbuilder.SetRootSignature(m_single_sampler_root_signature.Get());
gpbuilder.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE);
gpbuilder.SetVertexShader(vs.pShaderBytecode, static_cast<uint32_t>(vs.BytecodeLength));
gpbuilder.SetPixelShader(fs.Get());
gpbuilder.SetPixelShader(fs.pShaderBytecode, static_cast<uint32_t>(fs.BytecodeLength));
gpbuilder.SetNoCullRasterizationState();
gpbuilder.SetNoDepthTestState();
gpbuilder.SetNoBlendingState();