From 74b28befe3a52c71a42cca7481c7c202c608c6a2 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Tue, 19 May 2026 07:59:48 +0000 Subject: [PATCH] core/d3d11: consume the pre-baked batch FS DXBC blobs Wires the D3D11 backend to the shared D3DCommon::EmbeddedShaders pickers extracted in the previous commit. GetBatchPixelShader's slow path now forks on the pre-baked vs runtime-compiled axes: Pre-baked path (texture_mode == Disabled OR filter == Nearest with a non-Disabled texture): 1. Compute use_dual_source from the same shadergen formula the D3D12 backend uses (and the same one the picker tables expect). 2. Call the shared picker: D3DCommon::EmbeddedShaders::PickBatchUntexturedFS or PickBatchTexturedNearestFS. Returns a Bytecode { data, size }. 3. D3D11::ShaderCompiler::CreatePixelShader(device, data, size) wraps ID3D11Device::CreatePixelShader and produces the ID3D11PixelShader directly. No shadergen, no D3DCompile, no m_shader_cache lookup, no disk cache writeback. Runtime path (Bilinear / BilinearBinAlpha / JINC2 / JINC2BinAlpha / xBR / xBRBinAlpha): Unchanged: per-call shadergen + GenerateBatchFragmentShader + m_shader_cache.GetPixelShader, exactly the same flow that covers every cell pre-this-commit. Both paths converge on a `fresh` ComPtr that the publish step (mutex + canonical_slot fill + fastpath store + Reserved-dedup alias fill) consumes identically. The pre-baked path skips the shader-cache disk-backed entries for those cells. The D3D11 shader cache is a sourcecode-hash -> DXBC mapping; the picker bypasses both the source string and the D3DCompile step, so there's no on-disk entry to write and no hash to look up. On first launch with no cache, this is what makes the difference - cold-start CompileShaders / lazy-fault times drop on every (untextured) and (textured Nearest) cell they touch. With a warm disk cache the speedup is smaller but still real (no D3DCompile or shader-cache mutex contention). The D3D11 batch-shader matrix is 5-dim ([filter][render_mode][texture_mode][dithering][interlacing]) where the [dithering][interlacing] dims hold pre-existing post-cbuffer-routing redundancy: each (dithering, interlacing) slot now contains a ComPtr to the same ID3D11PixelShader as the opposite slot. That waste exists pre-this-commit; we don't shrink the matrix here because it would touch many call sites and is a separable cleanup. The pre-baked path inherits the same redundancy - 4 matrix slots get filled with the same ID3D11PixelShader for each pre-baked variant - but the additional CreatePixelShader calls are cheap (no compile, just a refcounted device object) so the cost is sub-millisecond per slot. What changes: src/core/gpu_hw_d3d11.cpp 1. New include: common/d3d_common/embedded_shaders.h. 2. GetBatchPixelShader's slow path restructured. The previous shadergen + D3DCompile body is preserved verbatim inside the new `else` branch (the runtime / non-pre-baked path). Two new `if` arms cover the pre-baked path with shared picker calls + CreatePixelShader. The publish step (mutex acquire, canonical_slot fill, fastpath atomic store, Reserved-dedup alias) is unchanged - it operates on the same ComPtr regardless of how `fresh` was produced. 3. use_dual_source bool computed once at the top of the slow path. Same formula the D3D12 caller uses (m_supports_dual_source_blend AND ((render_mode is transparent) OR filter != Nearest)). Same bit the pickers index on, same bit any future PSO blend-state branching would need to consult. What doesn't change: - HLSL sources, .inc blob bytes, regen tool, shader-cache disk format. None touched. - D3D12 runtime behaviour. The shared pickers are still called from gpu_hw_d3d12.cpp as well (the 9cc64ef commit hooked them up); both backends now consume the same byte stream. - precompile_sync / PrecompileBatchShaders on D3D11. GetBatchPixelShader still gets called for every reachable cell at precompile time - the matrix still needs to fill so the per-PSO-build fast path hits later. The fill just runs faster on the pre-baked cells (no D3DCompile cost). The progress count is unchanged. - Visual output. fxc emits ps_5_0 DXBC bytecode that D3D11::CreatePixelShader and the runtime-D3DCompile path produce identically from the same HLSL source (the shadergen output is byte-for-byte the same across API_D3D11 / API_D3D12 macros for the batch FS body, see shadergen.cpp:130-131 and the audit notes in the d3d_common move commit). Pixel diff: zero. Behavioural change for users (D3D11 default settings): - PSX content runs with texture_filter=Nearest by default. The typical Palette4Bit Nearest cells are the hot ones; first draw of each new (render_mode, texture_mode) combo hits the pre-baked picker + CreatePixelShader rather than shadergen + D3DCompile. Cold-launch shader-compile latency drops proportionally. - On systems with no on-disk shader cache (first launch, cache wipe, switch to a new RetroArch install): the (untextured) and (textured Nearest) cells skip the slowest part of the pre-pipeline-build cost entirely. - With a warm shader cache, the pre-baked path still saves the cache mutex acquire + hash compute + disk read for those cells. Smaller win but consistent. - Disk cache footprint shrinks: 84 (12 untextured + 72 textured Nearest) entries no longer get written. Existing entries from previous versions remain - they hit but produce bit-identical-or-equivalent ID3D11PixelShader objects when consumed; over time the user's cache shrinks as invalidations turn over. Test: - g++ -std=c++17 syntax check on the new slow-path block using a shim TU (ComPtr / ID3D11Device / ID3D11PixelShader / enum stand-ins + the real embedded_shaders.h) compiles cleanly. Both pre-baked picker calls and the runtime else-branch coexist correctly. - The shared pickers' tables (D3DCommon::EmbeddedShaders {Untextured, TexturedNearest}FS in src/common/d3d_common/embedded_shaders.cpp) are validated by the existing D3D12 path, exercised in src/core/gpu_hw_d3d12.cpp's GetBatchPipeline. The D3D11 consumer hits the same tables. Cumulative pre-bake variant count progress, both backends: Pre-arc: 75 264 (every axis -D) Post-cbuffer-routing arc: 1 176 Post-c01f8ae / a7f5717 (D3D12 untextured + Nearest live): 84 baked Post-this-commit (D3D11 untextured + Nearest also consume the 84 baked blobs): 168 cell-equivalents Remaining filters (Bilinear / JINC2 / xBR): 3 templates x ~144 variants. Each template's foundation + activation pair lands D3D12 first; D3D11 picks up the new pickers as they land in D3DCommon::EmbeddedShaders. Next: - Pre-bake the Bilinear filter slice. Foundation + activation pair on D3D12; the activation commit also extends D3D11's GetBatchPixelShader fork to consume the new picker (it's a one-line `else if (filter == Bilinear || filter == BilinearBinAlpha)` next to the existing Nearest arm). - Same shape for JINC2 / xBR. After the 4th template lands, the D3D11 slow path's runtime else-branch is unreachable and can be deleted along with the dim-cache redundancy cleanup. --- src/core/gpu_hw_d3d11.cpp | 85 +++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index 09943218..e02091ed 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -1,5 +1,6 @@ #include "gpu_hw_d3d11.h" #include "common/d3d11/shader_compiler.h" +#include "common/d3d_common/embedded_shaders.h" #include "common/display.hlsl.h" #include "common/log.h" #include "common/state_wrapper.h" @@ -1337,22 +1338,74 @@ ID3D11PixelShader* GPU_HW_D3D11::GetBatchPixelShader(GPUTextureFilter filter, ui // the loser's ComPtr is released when it falls out of scope // below. // - // 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. Construction is a handful of POD - // copies; the expensive work is GenerateBatchFragmentShader + - // D3DCompile + CreatePixelShader that follow, all 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 ps = tmp_shadergen.GenerateBatchFragmentShader( - static_cast(render_mode), static_cast(lookup_mode), dithering, interlacing); - ComPtr fresh = m_shader_cache.GetPixelShader(m_device.Get(), ps); + // Two paths converge on the same `fresh` ComPtr: + // 1. Pre-baked (texture_mode == Disabled OR + // filter == Nearest with a non-Disabled texture_mode): no + // shadergen, no D3DCompile, no m_shader_cache lookup. The + // shared picker in D3DCommon::EmbeddedShaders returns the + // matching .inc-supplied DXBC blob and we hand it straight + // to D3D11::ShaderCompiler::CreatePixelShader (which wraps + // ID3D11Device::CreatePixelShader with the byte / size pair). + // Same blobs the D3D12 backend consumes; fxc emits ps_5_0 + // that both APIs honour identically. No shader-cache + // entries are produced for these cells. + // 2. Runtime (every other filter): the existing shadergen + + // D3DCompile + m_shader_cache path, unchanged. Bilinear / + // JINC2 / xBR (and their *BinAlpha variants) still take + // this path. As subsequent pre-bake commits land their + // filter slices for the D3D12 backend, the same pre-baked + // blobs become reusable here. + // + // The use_dual_source bit is the same shadergen formula the + // pickers consume on D3D12 (m_supports_dual_source_blend AND + // (transparent render_mode OR non-Nearest filter)). Computed + // once at the call site so the pre-baked picker and any future + // PSO blend-state branching see the same value. + const bool use_dual_source = + m_supports_dual_source_blend && + ((render_mode != static_cast(BatchRenderMode::TransparencyDisabled) && + render_mode != static_cast(BatchRenderMode::OnlyOpaque)) || + filter != GPUTextureFilter::Nearest); + const bool untextured = + (static_cast(lookup_mode) == GPUTextureMode::Disabled); + const bool textured_nearest = + !untextured && (filter == GPUTextureFilter::Nearest); + + ComPtr fresh; + if (untextured) + { + const auto bc = D3DCommon::EmbeddedShaders::PickBatchUntexturedFS( + use_dual_source, m_multisamples, m_per_sample_shading, + m_disable_color_perspective); + fresh = D3D11::ShaderCompiler::CreatePixelShader(m_device.Get(), bc.data, bc.size); + } + else if (textured_nearest) + { + const auto bc = D3DCommon::EmbeddedShaders::PickBatchTexturedNearestFS( + lookup_mode, use_dual_source, m_multisamples, m_per_sample_shading, + m_disable_color_perspective); + fresh = D3D11::ShaderCompiler::CreatePixelShader(m_device.Get(), bc.data, bc.size); + } + else + { + // 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. Construction is a handful of POD + // copies; the expensive work is GenerateBatchFragmentShader + + // D3DCompile + CreatePixelShader that follow, all 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 ps = tmp_shadergen.GenerateBatchFragmentShader( + static_cast(render_mode), static_cast(lookup_mode), dithering, interlacing); + fresh = m_shader_cache.GetPixelShader(m_device.Get(), ps); + } + if (!fresh) { Log_ErrorPrintf("Lazy batch pixel shader compile failed for (f=%u, rm=%u, tm=%u, d=%u, i=%u)",