core/d3d12: port filter dim cache from Vulkan backend

Final piece of the cbuffer-refactor patch series. After 7b575a3
made resolution_scale / true_color / scaled_dithering invariant
under HLSL source and 418eb14 stopped DestroyPipelines + the
1164-PSO sync compile from firing on cbuffer-only toggles, the
remaining ~1-minute pause on D3D12 was on texture-filter toggles:
filter genuinely changes the FilteredSampleFromVRAM emission and
needs new DXBC, so 418eb14's shader_source_changed correctly
gated a full destroy + rebuild on every filter flip. That's the
last hot per-session setting the user can move through the menu
that still costs a full matrix walk.

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

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

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

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

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

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

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

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

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

Test:
  - D3D12, shader_precompile = Enabled. First boot: matrix walks
    only Nearest's sub-cube. PSO count and progress bar match
    pre-patch.
  - Toggle texture filter Nearest -> Bilinear: precompile_sync
    walks Bilinear's sub-cube. Bilinear PSOs build for the first
    time. Old Nearest sub-cube stays populated.
  - Toggle back Bilinear -> Nearest: precompile_sync loop hits
    every Nearest slot on its fast-path atomic load and returns
    instantly. No D3DCompile, no progress OSD.
  - Toggle Nearest -> Bilinear again: every Bilinear slot is also
    already populated. Instant.
  - Toggle resolution scale / true_color / scaled_dithering: still
    pays zero per 418eb14, unchanged.
  - Toggle MSAA / per_sample / UV limits / PGXP depth / colour
    perspective: DestroyPipelines clears the entire matrix
    (enumerate covers all filter sub-cubes), full rebuild walks
    the current filter's sub-cube. Same cost as 418eb14 - the
    full destroy is genuinely required because those settings
    bake into HLSL identically for every filter slot.
  - shader_precompile_mode = Lazy: worker captures
    m_texture_filtering at launch, walks just that sub-cube.
    Filter toggle joins worker, restarts with new filter
    snapshot, walks new sub-cube. Main thread can fault any
    cell ahead of the worker via GetBatchPipeline.
This commit is contained in:
LibretroAdmin 2026-05-18 04:38:52 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 10c53b8524
2 changed files with 180 additions and 65 deletions

View file

@ -396,19 +396,33 @@ void GPU_HW_D3D12::UpdateSettings()
{
GPU_HW::UpdateSettings();
// shader_source_changed is the narrower signal added by the
// cbuffer-refactor follow-up - it fires only when a setting that
// the shader generator bakes into the HLSL string has flipped.
// Toggling resolution_scale / true_color / scaled_dithering now
// costs no DXBC compile and no PSO rebuild: the HLSL is invariant
// under those three, and the new values ride the per-batch UBO
// upload on the next FlushRender. Without checking this signal,
// every option toggle would still walk the whole 1164-PSO batch
// matrix synchronously when shader_precompile_mode = Enabled,
// exactly the multi-tens-of-seconds pause this patch series is
// meant to remove.
bool framebuffer_changed, shaders_changed, shader_source_changed;
UpdateHWSettings(&framebuffer_changed, &shaders_changed, nullptr, nullptr, &shader_source_changed);
// Stop the background batch-compile worker BEFORE UpdateHWSettings
// writes m_texture_filtering. The worker captures m_texture_filtering
// at launch and uses it for every GetBatchPipeline / GetBatchFragmentShader
// call - if the runloop thread flips it mid-iteration the worker
// would split its filter-index read across old / new values,
// installing a FS blob compiled with one filter into a sub-cube
// indexed by another. Joining first eliminates the race; the next
// CompilePipelines below restarts a worker for the new filter as
// appropriate. StopShaderCompileThread is idempotent, so the
// matching call inside CompilePipelines itself just becomes a
// no-op on this path.
StopShaderCompileThread();
// shader_source_changed: any setting that affects HLSL source
// string changed. Excludes the cbuffer-only set
// (resolution_scale / true_color / scaled_dithering).
//
// only_dim_changed: dim-cube setting (filter / true_color /
// scaled_dithering) changed and nothing in non_dim_diff. With the
// dim cache (filter outermost) this means "filter sub-cube can be
// lazy-populated, other filters' sub-cubes stay valid". When set
// alongside shader_source_changed it picks out the filter-only-
// changed case; when set without shader_source_changed it picks
// out the cbuffer-only case (which the gate at the bottom
// implicitly handles by checking shader_source_changed).
bool framebuffer_changed, shaders_changed, only_dim_changed, shader_source_changed;
UpdateHWSettings(&framebuffer_changed, &shaders_changed, &only_dim_changed, nullptr, &shader_source_changed);
if (framebuffer_changed)
{
@ -426,9 +440,37 @@ void GPU_HW_D3D12::UpdateSettings()
if (shader_source_changed)
{
// clear it since we draw a loading screen and it's not in the correct state
DestroyPipelines();
CompilePipelines();
if (only_dim_changed)
{
// Filter changed but nothing in non_dim_diff (and the cbuffer-
// only members in dim_diff don't move HLSL, so this is
// effectively "only filter changed"). m_batch_pipelines and
// m_batch_fragment_shader_blobs are filter-dimensioned: the
// previous filter's sub-cube remains populated and reachable,
// so DestroyPipelines would just throw away valid PSOs. Skip
// it and just call CompilePipelines, which lazy-populates the
// new filter's sub-cube on top: in Enabled mode the
// precompile_sync loop walks the matrix calling
// GetBatchPipeline with the new m_texture_filtering, so the
// other sub-cubes are untouched. In Lazy / Disabled the new
// sub-cube stays empty until first draw / the worker reaches
// each cell.
//
// Cycling back to a previously-visited filter is instant - no
// D3DCompile call, no PSO rebuild, just an atomic load of an
// already-filled slot.
CompilePipelines();
}
else
{
// Full flush: a non-dim shader-affecting change
// (multisamples / per-sample shading / UV limits / chroma
// smoothing / PGXP depth / colour perspective / precompile
// mode) invalidates EVERY sub-cube because those settings
// bake into the HLSL identically for every filter slot.
DestroyPipelines();
CompilePipelines();
}
}
// this has to be done here, because otherwise we're using destroyed pipelines in the same cmdbuffer
@ -745,11 +787,10 @@ bool GPU_HW_D3D12::CompilePipelines()
// Vertex shaders are still always compiled synchronously - there
// are only two (textured / not) and we need them for the lazy
// PSO builder. Drop the previous local arrays and use the member
// m_batch_vertex_shader_blobs / m_batch_fragment_shader_blobs so
// GetBatchPipeline can reach them at draw time.
// PSO builder. The FS matrix is filled through the lazy helper
// GetBatchFragmentShader (in precompile_sync / on demand at draw
// time / via the worker), so no parallel reference is needed.
auto& batch_vertex_shaders = m_batch_vertex_shader_blobs;
auto& batch_fragment_shaders = m_batch_fragment_shader_blobs;
for (uint8_t textured = 0; textured < 2; textured++)
{
@ -785,6 +826,15 @@ bool GPU_HW_D3D12::CompilePipelines()
if (precompile_sync)
{
const bool dual_source = m_supports_dual_source_blend;
// The dim cache makes m_batch_pipelines / m_batch_fragment_shader_blobs
// filter-dimensioned. precompile_sync walks ONLY the current
// m_texture_filtering sub-cube, not the full 7-filter matrix -
// pre-filling six unused sub-cubes would multiply the cold-cache
// D3DCompile pass by 7x for no gain (the game can only be running
// under one filter at a time, and the other sub-cubes get faulted
// in on demand if the user later flips the filter setting and
// UpdateSettings calls CompilePipelines again).
const GPUTextureFilter cur_filter = m_texture_filtering;
for (uint8_t render_mode = 0; render_mode < 4; render_mode++)
{
for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++)
@ -796,7 +846,7 @@ bool GPU_HW_D3D12::CompilePipelines()
{
for (uint8_t interlacing = 0; interlacing < 2; interlacing++)
{
ComPtr<ID3DBlob> blob = GetBatchFragmentShader(render_mode, texture_mode,
ComPtr<ID3DBlob> blob = GetBatchFragmentShader(cur_filter, render_mode, texture_mode,
static_cast<bool>(dithering),
static_cast<bool>(interlacing));
if (!blob)
@ -823,7 +873,7 @@ bool GPU_HW_D3D12::CompilePipelines()
for (uint8_t interlacing = 0; interlacing < 2; interlacing++)
{
ComPtr<ID3D12PipelineState> pso =
GetBatchPipeline(depth_test, render_mode, texture_mode, transparency_mode,
GetBatchPipeline(cur_filter, depth_test, render_mode, texture_mode, transparency_mode,
static_cast<bool>(dithering), static_cast<bool>(interlacing));
if (!pso)
return false;
@ -1011,7 +1061,19 @@ void GPU_HW_D3D12::ShaderCompileThreadEntryPoint()
//
// Structurally unreachable cells are skipped via
// IsBatchShaderReachable.
//
// The worker captures the runtime-current m_texture_filtering at
// launch time and walks only that filter's sub-cube. Mirrors what
// CompilePipelines does in precompile_sync mode - both paths fill
// the active filter's sub-cube and leave the other six sub-cubes
// empty, to be lazy-faulted in via GetBatchPipeline on the main
// thread if a filter toggle later brings them into use.
// UpdateSettings calls StopShaderCompileThread BEFORE
// UpdateHWSettings writes m_texture_filtering, so the snapshot here
// matches the filter the worker is supposed to be warming - the
// next CompilePipelines starts a fresh worker for the new filter.
const bool dual_source = m_supports_dual_source_blend;
const GPUTextureFilter cur_filter = m_texture_filtering;
for (uint8_t depth_test = 0; depth_test < 2; depth_test++)
{
for (uint8_t render_mode = 0; render_mode < 4; render_mode++)
@ -1030,7 +1092,7 @@ void GPU_HW_D3D12::ShaderCompileThreadEntryPoint()
if (m_shader_compile_thread_quit.load(std::memory_order_relaxed))
return;
GetBatchPipeline(depth_test, render_mode, texture_mode, transparency_mode,
GetBatchPipeline(cur_filter, depth_test, render_mode, texture_mode, transparency_mode,
static_cast<bool>(dithering), static_cast<bool>(interlacing));
}
}
@ -1040,7 +1102,7 @@ void GPU_HW_D3D12::ShaderCompileThreadEntryPoint()
}
}
GPU_HW_D3D12::ComPtr<ID3DBlob> GPU_HW_D3D12::GetBatchFragmentShader(uint8_t render_mode, uint8_t texture_mode, bool dithering,
GPU_HW_D3D12::ComPtr<ID3DBlob> GPU_HW_D3D12::GetBatchFragmentShader(GPUTextureFilter filter, uint8_t render_mode, uint8_t texture_mode, bool dithering,
bool interlacing)
{
// Reserved_*Direct16Bit shader-source dedup is applied at the
@ -1053,13 +1115,14 @@ GPU_HW_D3D12::ComPtr<ID3DBlob> GPU_HW_D3D12::GetBatchFragmentShader(uint8_t rend
const uint8_t lookup_mode = (texture_mode == static_cast<uint8_t>(GPUTextureMode::Reserved_Direct16Bit)) ? 2u :
(texture_mode == static_cast<uint8_t>(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u :
texture_mode;
const uint8_t filter_idx = static_cast<uint8_t>(filter);
// Fast path: lock-free acquire-load. Both the worker (via
// GetBatchPipeline -> here) and the main thread (same) can read
// a slot concurrently without serialising against each other or
// each other's slow-path compiles.
std::atomic<ID3DBlob*>& fast_slot =
m_batch_fragment_shader_blobs_fastpath[render_mode][texture_mode][static_cast<uint8_t>(dithering)][static_cast<uint8_t>(interlacing)];
m_batch_fragment_shader_blobs_fastpath[filter_idx][render_mode][texture_mode][static_cast<uint8_t>(dithering)][static_cast<uint8_t>(interlacing)];
ID3DBlob* existing = fast_slot.load(std::memory_order_acquire);
if (existing)
{
@ -1075,18 +1138,28 @@ GPU_HW_D3D12::ComPtr<ID3DBlob> GPU_HW_D3D12::GetBatchFragmentShader(uint8_t rend
// different shaders here in parallel. Two threads compiling the
// SAME shader is harmless - both produce identical DXBC and the
// cache dedups internally via its own double-check.
if (!m_shadergen)
{
Log_ErrorPrint("GetBatchFragmentShader called before CompilePipelines constructed the shadergen");
return {};
}
const std::string fs = m_shadergen->GenerateBatchFragmentShader(
//
// Construct a per-call shadergen bound to the requested filter
// rather than reusing m_shadergen (which is pinned to the
// runtime-current m_texture_filtering for the non-batch helpers).
// Filter is the only setting that differs between this shadergen
// and m_shadergen - all other inputs come from member state that
// is single-valued per session. The construction itself is a
// handful of POD copies; the expensive work is the
// GenerateBatchFragmentShader call + D3DCompile that follow, and
// those are unchanged from the pre-dim-cache path.
GPU_HW_ShaderGen tmp_shadergen(
m_host_display->GetRenderAPI(), m_resolution_scale, m_multisamples, m_per_sample_shading, m_true_color,
m_scaled_dithering, filter, m_using_uv_limits, m_pgxp_depth_buffer, m_disable_color_perspective,
m_supports_dual_source_blend);
const std::string fs = tmp_shadergen.GenerateBatchFragmentShader(
static_cast<BatchRenderMode>(render_mode), static_cast<GPUTextureMode>(lookup_mode), dithering, interlacing);
ComPtr<ID3DBlob> fresh_blob = m_shader_cache.GetPixelShader(fs);
if (!fresh_blob)
{
Log_ErrorPrintf("Lazy batch fragment shader compile failed for (rm=%u, tm=%u, d=%u, i=%u)", render_mode,
texture_mode, static_cast<uint8_t>(dithering), static_cast<uint8_t>(interlacing));
Log_ErrorPrintf("Lazy batch fragment shader compile failed for (f=%u, rm=%u, tm=%u, d=%u, i=%u)",
static_cast<uint8_t>(filter), render_mode, texture_mode,
static_cast<uint8_t>(dithering), static_cast<uint8_t>(interlacing));
return {};
}
@ -1106,19 +1179,19 @@ GPU_HW_D3D12::ComPtr<ID3DBlob> GPU_HW_D3D12::GetBatchFragmentShader(uint8_t rend
}
ComPtr<ID3DBlob>& canonical_slot =
m_batch_fragment_shader_blobs[render_mode][lookup_mode][static_cast<uint8_t>(dithering)][static_cast<uint8_t>(interlacing)];
m_batch_fragment_shader_blobs[filter_idx][render_mode][lookup_mode][static_cast<uint8_t>(dithering)][static_cast<uint8_t>(interlacing)];
if (!canonical_slot)
{
canonical_slot = fresh_blob;
m_batch_fragment_shader_blobs_fastpath[render_mode][lookup_mode][static_cast<uint8_t>(dithering)][static_cast<uint8_t>(interlacing)]
m_batch_fragment_shader_blobs_fastpath[filter_idx][render_mode][lookup_mode][static_cast<uint8_t>(dithering)][static_cast<uint8_t>(interlacing)]
.store(canonical_slot.Get(), std::memory_order_release);
}
if (lookup_mode != texture_mode)
{
ComPtr<ID3DBlob>& dup_slot =
m_batch_fragment_shader_blobs[render_mode][texture_mode][static_cast<uint8_t>(dithering)][static_cast<uint8_t>(interlacing)];
m_batch_fragment_shader_blobs[filter_idx][render_mode][texture_mode][static_cast<uint8_t>(dithering)][static_cast<uint8_t>(interlacing)];
if (!dup_slot)
dup_slot = canonical_slot;
}
@ -1128,7 +1201,7 @@ GPU_HW_D3D12::ComPtr<ID3DBlob> GPU_HW_D3D12::GetBatchFragmentShader(uint8_t rend
return canonical_slot;
}
GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetBatchPipeline(uint8_t depth_test, uint8_t render_mode,
GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetBatchPipeline(GPUTextureFilter filter, uint8_t depth_test, uint8_t render_mode,
uint8_t texture_mode, uint8_t transparency_mode,
bool dithering, bool interlacing)
{
@ -1142,13 +1215,14 @@ GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetBatchPipeline(uint8_t
const uint8_t lookup_mode = (texture_mode == static_cast<uint8_t>(GPUTextureMode::Reserved_Direct16Bit)) ? 2u :
(texture_mode == static_cast<uint8_t>(GPUTextureMode::Reserved_RawDirect16Bit)) ? 6u :
texture_mode;
const uint8_t filter_idx = static_cast<uint8_t>(filter);
// Fast path: lock-free acquire-load. DrawBatchVertices hits this
// once a slot is filled (either by the precompile worker or by
// an earlier main-thread fault-in). No mutex, no contention
// against the worker.
std::atomic<ID3D12PipelineState*>& fast_slot =
m_batch_pipelines_fastpath[depth_test][render_mode][texture_mode][transparency_mode][static_cast<uint8_t>(dithering)]
m_batch_pipelines_fastpath[filter_idx][depth_test][render_mode][texture_mode][transparency_mode][static_cast<uint8_t>(dithering)]
[static_cast<uint8_t>(interlacing)];
ID3D12PipelineState* existing = fast_slot.load(std::memory_order_acquire);
if (existing)
@ -1168,7 +1242,7 @@ GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetBatchPipeline(uint8_t
// slot B. Two threads racing to compile the SAME slot is
// wasteful but harmless - both produce equivalent PSOs and the
// double-check below picks the winner.
ComPtr<ID3DBlob> fs_blob = GetBatchFragmentShader(render_mode, lookup_mode, dithering, interlacing);
ComPtr<ID3DBlob> fs_blob = GetBatchFragmentShader(filter, render_mode, lookup_mode, dithering, interlacing);
if (!fs_blob)
return {};
@ -1201,10 +1275,19 @@ GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetBatchPipeline(uint8_t
gpbuilder.SetNoBlendingState();
gpbuilder.SetMultisamples(m_multisamples);
// Blend state uses the FILTER PARAMETER (not m_texture_filtering)
// so a precompile-worker thread or a main-thread lazy fault for a
// non-current filter's sub-cube builds the PSO with that sub-cube's
// blend mode. The non-current sub-cube would otherwise inherit
// whatever filter the runtime happens to be at when this slot
// first gets faulted in - benign for Nearest <-> Nearest revisits,
// but produces wrong blend state when a Bilinear PSO is reached
// via a fault during a Nearest session and ends up running later
// after a filter switch.
if ((static_cast<GPUTransparencyMode>(transparency_mode) != GPUTransparencyMode::Disabled &&
(static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::TransparencyDisabled &&
static_cast<BatchRenderMode>(render_mode) != BatchRenderMode::OnlyOpaque)) ||
m_texture_filtering != GPUTextureFilter::Nearest)
filter != GPUTextureFilter::Nearest)
{
gpbuilder.SetBlendState(
0, true, D3D12_BLEND_ONE,
@ -1220,8 +1303,9 @@ GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetBatchPipeline(uint8_t
ComPtr<ID3D12PipelineState> fresh_pso = gpbuilder.Create(g_d3d12_context->GetDevice(), m_shader_cache);
if (!fresh_pso)
{
Log_ErrorPrintf("Lazy batch PSO compile failed for (dt=%u, rm=%u, tm=%u, tr=%u, d=%u, i=%u)", depth_test,
render_mode, texture_mode, transparency_mode, static_cast<uint8_t>(dithering), static_cast<uint8_t>(interlacing));
Log_ErrorPrintf("Lazy batch PSO compile failed for (f=%u, dt=%u, rm=%u, tm=%u, tr=%u, d=%u, i=%u)",
static_cast<uint8_t>(filter), depth_test, render_mode, texture_mode, transparency_mode,
static_cast<uint8_t>(dithering), static_cast<uint8_t>(interlacing));
return {};
}
@ -1242,18 +1326,18 @@ GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetBatchPipeline(uint8_t
}
ComPtr<ID3D12PipelineState>& canonical_slot =
m_batch_pipelines[depth_test][render_mode][lookup_mode][transparency_mode][static_cast<uint8_t>(dithering)]
m_batch_pipelines[filter_idx][depth_test][render_mode][lookup_mode][transparency_mode][static_cast<uint8_t>(dithering)]
[static_cast<uint8_t>(interlacing)];
if (!canonical_slot)
{
canonical_slot = fresh_pso;
D3D12::SetObjectNameFormatted(canonical_slot.Get(), "Batch Pipeline %u,%u,%u,%u,%u,%u", depth_test, render_mode,
D3D12::SetObjectNameFormatted(canonical_slot.Get(), "Batch Pipeline f%u,%u,%u,%u,%u,%u,%u", filter_idx, depth_test, render_mode,
lookup_mode, transparency_mode, static_cast<uint8_t>(dithering), static_cast<uint8_t>(interlacing));
// Publish the canonical raw pointer for future fast-path
// readers of the canonical slot.
m_batch_pipelines_fastpath[depth_test][render_mode][lookup_mode][transparency_mode][static_cast<uint8_t>(dithering)]
m_batch_pipelines_fastpath[filter_idx][depth_test][render_mode][lookup_mode][transparency_mode][static_cast<uint8_t>(dithering)]
[static_cast<uint8_t>(interlacing)]
.store(canonical_slot.Get(), std::memory_order_release);
}
@ -1261,7 +1345,7 @@ GPU_HW_D3D12::ComPtr<ID3D12PipelineState> GPU_HW_D3D12::GetBatchPipeline(uint8_t
if (lookup_mode != texture_mode)
{
ComPtr<ID3D12PipelineState>& dup_slot =
m_batch_pipelines[depth_test][render_mode][texture_mode][transparency_mode][static_cast<uint8_t>(dithering)]
m_batch_pipelines[filter_idx][depth_test][render_mode][texture_mode][transparency_mode][static_cast<uint8_t>(dithering)]
[static_cast<uint8_t>(interlacing)];
if (!dup_slot)
dup_slot = canonical_slot;
@ -1870,10 +1954,15 @@ void GPU_HW_D3D12::DrawBatchVertices(BatchRenderMode render_mode, uint32_t base_
// The mutex serialises both the PSO matrix and the shader-cache
// mutation; cost is ~20 ns uncontended per modern std::mutex impl.
//
// [depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
// [filter][depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
// m_texture_filtering selects the active filter's sub-cube. Filter
// is the outermost dim so a filter toggle in UpdateSettings can
// skip DestroyPipelines - the other filters' sub-cubes remain
// valid and reachable, switching back to a previously-visited
// filter is an atomic load on an already-filled slot.
const uint8_t depth_test = static_cast<uint8_t>(m_batch.check_mask_before_draw || m_batch.use_depth_buffer);
ComPtr<ID3D12PipelineState> pipeline =
GetBatchPipeline(depth_test, static_cast<uint8_t>(render_mode), static_cast<uint8_t>(m_batch.texture_mode),
GetBatchPipeline(m_texture_filtering, depth_test, static_cast<uint8_t>(render_mode), static_cast<uint8_t>(m_batch.texture_mode),
static_cast<uint8_t>(m_batch.transparency_mode), m_batch.dithering, m_batch.interlacing);
cmdlist->SetPipelineState(pipeline.Get());

View file

@ -126,26 +126,34 @@ private:
//
// GetBatchFragmentShader returns a non-null ComPtr to the
// ID3DBlob holding the compiled HLSL bytecode for the requested
// (render_mode, texture_mode, dithering, interlacing) tuple,
// generating the shader source via m_shadergen and feeding it
// through m_shader_cache.GetPixelShader on a cache miss. It also
// handles the Reserved_Direct16Bit (3) -> Direct16Bit (2) and
// (filter, render_mode, texture_mode, dithering, interlacing)
// tuple, generating the shader source via a temporary shadergen
// bound to that filter and feeding it through
// m_shader_cache.GetPixelShader on a cache miss. The temporary
// shadergen lets the worker / main thread fault in a non-current
// filter's sub-cube without having to reconstruct m_shadergen
// (which stays bound to the runtime-current m_texture_filtering
// for the non-batch helpers below). It also handles the
// Reserved_Direct16Bit (3) -> Direct16Bit (2) and
// Reserved_RawDirect16Bit (7) -> RawDirect16Bit (6) dedup at the
// blob array level.
//
// GetBatchPipeline returns the ID3D12PipelineState for the full
// 6-D PSO matrix slot. It builds the gpbuilder state on demand,
// calling GetBatchFragmentShader for the bound shader, then
// hands the descriptor to m_shader_cache.GetPipelineState (which
// hits the on-disk PSO cache where possible, only paying the
// actual driver compile on cold runs). Reserved_* texture-mode
// PSOs inherit the canonical PSO ComPtr.
// 7-D PSO matrix slot (filter added as the outermost dimension to
// the existing 6-D layout - mirrors the Vulkan dim-cache shape).
// It builds the gpbuilder state on demand, calling
// GetBatchFragmentShader for the bound shader with the same
// filter, then hands the descriptor to
// m_shader_cache.GetPipelineState (which hits the on-disk PSO
// cache where possible, only paying the actual driver compile on
// cold runs). Reserved_* texture-mode PSOs inherit the canonical
// PSO ComPtr.
//
// Both helpers serialise their cache + array mutations through
// m_batch_shader_mutex. The fast path is one uncontended lock
// per DrawBatchVertices call.
ComPtr<ID3DBlob> GetBatchFragmentShader(uint8_t render_mode, uint8_t texture_mode, bool dithering, bool interlacing);
ComPtr<ID3D12PipelineState> GetBatchPipeline(uint8_t depth_test, uint8_t render_mode, uint8_t texture_mode, uint8_t transparency_mode,
ComPtr<ID3DBlob> GetBatchFragmentShader(GPUTextureFilter filter, uint8_t render_mode, uint8_t texture_mode, bool dithering, bool interlacing);
ComPtr<ID3D12PipelineState> GetBatchPipeline(GPUTextureFilter filter, uint8_t depth_test, uint8_t render_mode, uint8_t texture_mode, uint8_t transparency_mode,
bool dithering, bool interlacing);
// Lazy non-batch PSO compile path.
@ -223,9 +231,27 @@ private:
// (in GetBatchPipeline's slow path); m_batch_pipelines_fastpath
// is the atomic view the runloop reads on the fast path.
//
// [depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
DimensionalArray<ComPtr<ID3D12PipelineState>, 2, 2, 5, 9, 4, 2> m_batch_pipelines;
DimensionalArray<std::atomic<ID3D12PipelineState*>, 2, 2, 5, 9, 4, 2> m_batch_pipelines_fastpath{};
// filter is the outermost dimension. With the cbuffer-refactor
// patch (7b575a3) the HLSL is invariant under
// resolution_scale / true_color / scaled_dithering, but it is
// STILL dependent on texture filter - the FilteredSampleFromVRAM
// helper is emitted differently for Nearest vs Bilinear vs
// JINC2 vs xBR vs the BinAlpha variants. Dimensioning over filter
// lets a filter toggle skip the DestroyPipelines round trip in
// UpdateSettings: the previous filter's sub-cube remains valid
// and reachable, and switching back to it later is an atomic
// load on an already-filled slot. Mirrors the same shape the
// Vulkan backend has carried for filter / true_color /
// scaled_dithering (Vulkan keeps true_color and scaled_dithering
// as dims too because those are VkSpecializationInfo spec
// constants and each combination needs a distinct VkPipeline;
// D3D12 doesn't because they're cbuffer fields now).
//
// GPUTextureFilter::Count = 7 -> the outermost dim size.
//
// [filter][depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing]
DimensionalArray<ComPtr<ID3D12PipelineState>, 2, 2, 5, 9, 4, 2, 7> m_batch_pipelines;
DimensionalArray<std::atomic<ID3D12PipelineState*>, 2, 2, 5, 9, 4, 2, 7> m_batch_pipelines_fastpath{};
// m_batch_shader_mutex serialises the SLOW path of the lazy
// helpers (cache mutation, ComPtr-array write, atomic-raw-pointer
@ -259,8 +285,8 @@ private:
// m_batch_fragment_shader_blobs owns the ComPtr; the parallel
// _fastpath array is the lock-free view.
DimensionalArray<ComPtr<ID3DBlob>, 2> m_batch_vertex_shader_blobs; // [textured]
DimensionalArray<ComPtr<ID3DBlob>, 2, 2, 9, 4> m_batch_fragment_shader_blobs; // [render][texture][dither][interlace]
DimensionalArray<std::atomic<ID3DBlob*>, 2, 2, 9, 4> m_batch_fragment_shader_blobs_fastpath{};
DimensionalArray<ComPtr<ID3DBlob>, 2, 2, 9, 4, 7> m_batch_fragment_shader_blobs; // [filter][render][texture][dither][interlace]
DimensionalArray<std::atomic<ID3DBlob*>, 2, 2, 9, 4, 7> m_batch_fragment_shader_blobs_fastpath{};
// [wrapped][interlaced]
DimensionalArray<ComPtr<ID3D12PipelineState>, 2, 2> m_vram_fill_pipelines;