From 19b118b9aecef0f8b596d9c958cc3951dc2b8651 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Fri, 15 May 2026 03:21:46 +0000 Subject: [PATCH] core, common/d3d11,d3d12,vulkan: skip unreachable cells in batch matrices The 4 x 9 x 2 x 2 batch shader matrix (and the larger D3D12 / Vulkan PSO matrices that extend it with depth_test and transparency_mode) have structurally unreachable cells that the precompile loop and background-compile worker were burning shader-compiler time on anyway. Add IsBatchShaderReachable to gpu_hw.h and consult it from all walk sites in all three backends. # Three classes of unreachable cell - Texture modes 3 (Reserved_Direct16Bit) and 7 (Reserved_RawDirect16Bit). GetBatchPixelShader's lookup_mode dedup aliases these to canonical modes 2 and 6, so the slot fills lazily on first runtime fault via a ComPtr copy. No compile required. - OnlyOpaque / OnlyTransparent with texture_mode == Disabled. NeedsTwoPassRendering returns false for untextured polys, so FlushRender never selects the two-pass fallback path for them. Unique source compiles on all hardware. - TransparentAndOpaque with texture_mode != Disabled, on hardware without dual-source blend. NeedsTwoPassRendering returns true in that combination, so the single-pass dual-source render mode is never selected. Unique source compiles, but only on non-dual-source hardware. # Effect On hardware that supports dual-source blend (modern desktop / most discrete GPUs): - D3D11 shader matrix: 104/144 cells reachable, 40 skipped. ~7% cold-compile time saved in Enabled mode (most of the saved cells were cache-hits anyway). - D3D12 shader matrix: same. PSO matrix: 1040/1440 cells reachable (28% saved, ~400 cells). With ~50 ms per PSO that's roughly 20 s saved on cold compile. - Vulkan shader matrix: same. PSO matrix: 1560/2160 reachable (28% saved, ~600 cells). ~30 s saved on cold compile. On hardware WITHOUT dual-source blend (some integrated / legacy parts): - D3D11: 80/144 cells (44% saved), ~29% cold-compile reduction. - D3D12: PSO matrix 800/1440 (44%), ~33 s saved. - Vulkan: PSO matrix 1200/2160 (44%), ~48 s saved. Zero runtime cost. We're not changing what gets drawn, just not pre-compiling shaders that wouldn't be drawn. The runtime fault paths through GetBatchPixelShader / GetBatchPipeline (slow-path mutex publish, dup_slot aliasing for reserved texture modes) are unchanged and handle the first draw to a skipped cell correctly without falling over - the dedup just kicks in via the canonical slot on first fault, exactly as it would for any other unfilled slot in Disabled mode. The depth_test and transparency_mode axes don't affect reachability (NeedsTwoPassRendering doesn't depend on them), so the PSO progress unit count is just CountReachableBatchShaders * * 5. # What's NOT changed - GetBatchPixelShader / GetBatchFragmentShader / GetBatchPipeline themselves. The lookup_mode dedup and the slow-path publish step still handle reserved texture modes and unforeseen runtime cell selections correctly. - Matrix storage layouts. The arrays stay dense at their full sizes - skipping cells leaves them null, which is harmless. - Non-batch shaders. The non-batch upfront ticks still apply. # Files touched - src/core/gpu_hw.h: IsBatchShaderReachable + CountReachableBatchShaders helpers (static, inline, no per-instance state). - src/core/gpu_hw_d3d11.cpp: skip in ShaderCompileThreadEntryPoint worker loop and in the precompile_sync block in CompileShaders. - src/core/gpu_hw_d3d12.cpp: skip in CompilePipelines (shader precompile + PSO precompile) and in ShaderCompileThreadEntryPoint. - src/core/gpu_hw_vulkan.cpp: skip in CompilePipelines (shader- module precompile + PSO precompile) and in ShaderCompileThreadEntryPoint. Progress unit counts now use CountReachableBatchShaders so the Enabled progress bar lands at exactly the compile count for the running hardware. --- src/core/gpu_hw.h | 66 ++++++++++++++++++++++++++++++++++++++ src/core/gpu_hw_d3d11.cpp | 29 ++++++++++++++++- src/core/gpu_hw_d3d12.cpp | 28 ++++++++++++++-- src/core/gpu_hw_vulkan.cpp | 28 ++++++++++++++-- 4 files changed, 144 insertions(+), 7 deletions(-) diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index f3d94b80..b50a2274 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -259,6 +259,72 @@ protected: (!m_supports_dual_source_blend && m_batch.transparency_mode != GPUTransparencyMode::Disabled))); } + /// Returns true if a given (render_mode, texture_mode) batch shader slot can + /// actually be selected by the runtime at draw time. Used by the precompile + /// loop and the background-compile worker to skip cells that the runtime + /// would never bind, so we don't burn D3DCompile / glslang time on shaders + /// that can't ever be used. + /// + /// Three classes of cell are structurally unreachable: + /// + /// - texture_mode 3 (Reserved_Direct16Bit) and 7 (Reserved_RawDirect16Bit) + /// dedupe to canonical modes 2 and 6 inside GetBatchPixelShader / + /// GetBatchPipeline. The first runtime fault on the reserved slot just + /// aliases the canonical slot's ComPtr; no fresh compile is needed. + /// + /// - OnlyOpaque / OnlyTransparent with texture_mode == Disabled. + /// NeedsTwoPassRendering short-circuits on (texture_mode != Disabled), + /// so untextured polys never go through the two-pass fallback path. + /// + /// - TransparentAndOpaque with texture_mode != Disabled on hardware + /// WITHOUT dual-source blend support. NeedsTwoPassRendering returns + /// true for textured transparent draws on this hardware, so the + /// single-pass dual-source path is never selected. (Untextured + /// transparent draws on the same hardware still use + /// TransparentAndOpaque - NeedsTwoPassRendering returns false for + /// them - so this skip is texture_mode-dependent.) + ALWAYS_INLINE static bool IsBatchShaderReachable(BatchRenderMode render_mode, uint8_t texture_mode, + bool supports_dual_source_blend) + { + if (texture_mode == static_cast(GPUTextureMode::Reserved_Direct16Bit) || + texture_mode == static_cast(GPUTextureMode::Reserved_RawDirect16Bit)) + return false; + + if ((render_mode == BatchRenderMode::OnlyOpaque || render_mode == BatchRenderMode::OnlyTransparent) && + texture_mode == static_cast(GPUTextureMode::Disabled)) + return false; + + if (!supports_dual_source_blend && render_mode == BatchRenderMode::TransparentAndOpaque && + texture_mode != static_cast(GPUTextureMode::Disabled)) + return false; + + return true; + } + + /// Total reachable cell count in the batch shader matrix, for sizing the + /// progress tracker upfront in Enabled mode. + static uint32_t CountReachableBatchShaders(bool supports_dual_source_blend) + { + uint32_t count = 0; + for (uint8_t rm = 0; rm < 4; rm++) + { + for (uint8_t tm = 0; tm < 9; tm++) + { + for (uint8_t d = 0; d < 2; d++) + { + for (uint8_t i = 0; i < 2; i++) + { + (void)d; + (void)i; + if (IsBatchShaderReachable(static_cast(rm), tm, supports_dual_source_blend)) + count++; + } + } + } + } + return count; + } + /// Returns true if the specified VRAM fill is oversized. ALWAYS_INLINE static bool IsVRAMFillOversized(uint32_t x, uint32_t y, uint32_t width, uint32_t height) { diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index dfdc6d2f..61bbbc52 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -954,8 +954,12 @@ bool GPU_HW_D3D11::CompileShaders() // in core/types.h. const GPUShaderPrecompileMode precompile_mode = g_settings.gpu_shader_precompile_mode; const bool precompile_sync = (precompile_mode == GPUShaderPrecompileMode::Enabled); + // batch_progress_units counts only reachable cells in the (render, + // texture, dither, interlace) matrix - see IsBatchShaderReachable in + // gpu_hw.h. This lets the progress bar end at exactly the number of + // compiles the precompile loop will perform. const uint32_t batch_progress_units = - (precompile_mode == GPUShaderPrecompileMode::Enabled) ? static_cast(4 * 9 * 2 * 2) : 0u; + precompile_sync ? CountReachableBatchShaders(m_supports_dual_source_blend) : 0u; ShaderCompileProgressTracker progress("Compiling Shaders", 1 + 1 + 2 + batch_progress_units + 1 + (2 * 2) + 4 + (2 * 3) + 1); @@ -1022,12 +1026,22 @@ bool GPU_HW_D3D11::CompileShaders() // internally, so the precompile loop here doesn't need to special- // case those - it'll call GetBatchPixelShader and the second // duplicate will be a cheap cache hit / pointer copy. + // + // Structurally unreachable cells (reserved texture modes, two-pass + // fallback modes for untextured polys, single-pass dual-source on + // hardware that lacks it) are skipped via IsBatchShaderReachable. + // batch_progress_units is sized to the same reachable count so the + // progress bar lands at 100%. if (precompile_sync) { + const bool dual_source = m_supports_dual_source_blend; for (uint8_t render_mode = 0; render_mode < 4; render_mode++) { for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++) { + if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) + continue; + for (uint8_t dithering = 0; dithering < 2; dithering++) { for (uint8_t interlacing = 0; interlacing < 2; interlacing++) @@ -1185,10 +1199,23 @@ void GPU_HW_D3D11::ShaderCompileThreadEntryPoint() // any slot the main thread filled first. The quit flag is // checked between cells so DestroyShaders can bring the worker // down within at most one shader-compile worth of latency. + // + // Structurally unreachable cells are skipped via + // IsBatchShaderReachable - see the comment on that helper in + // gpu_hw.h. Reserved texture modes alias the canonical slot + // through the dedup logic in GetBatchPixelShader, so first-fault + // on the alias only does a ComPtr copy, not a compile. The + // two-pass-fallback render modes with no texture are never + // selected by FlushRender. TransparentAndOpaque with a texture + // mode is never selected on hardware without dual-source blend. + const bool dual_source = m_supports_dual_source_blend; for (uint8_t render_mode = 0; render_mode < 4; render_mode++) { for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++) { + if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) + continue; + for (uint8_t dithering = 0; dithering < 2; dithering++) { for (uint8_t interlacing = 0; interlacing < 2; interlacing++) diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index f7f275cd..d5715a36 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -712,10 +712,14 @@ bool GPU_HW_D3D12::CompilePipelines() // here. const GPUShaderPrecompileMode precompile_mode = g_settings.gpu_shader_precompile_mode; const bool precompile_sync = (precompile_mode == GPUShaderPrecompileMode::Enabled); - const uint32_t batch_shader_progress_units = - precompile_sync ? static_cast(4 * 9 * 2 * 2) : 0u; + // Reachable cell counts for each matrix - see IsBatchShaderReachable + // in gpu_hw.h. The PSO matrix multiplies by 2 (depth_test) * 5 + // (transparency_mode); these axes don't affect reachability, so the + // per-(render_mode, texture_mode) skip rule is independent of them. + const uint32_t reachable_batch_cells = CountReachableBatchShaders(m_supports_dual_source_blend); + const uint32_t batch_shader_progress_units = precompile_sync ? reachable_batch_cells : 0u; const uint32_t batch_pipeline_progress_units = - precompile_sync ? static_cast(2 * 4 * 5 * 9 * 2 * 2) : 0u; + precompile_sync ? reachable_batch_cells * 2u * 5u : 0u; // Non-batch units only counted when we build them upfront. The // fullscreen-quad VS (1), VRAM fill (4), VRAM copy (2), VRAM // write (2), VRAM update depth (1), VRAM readback (1), display @@ -762,12 +766,20 @@ bool GPU_HW_D3D12::CompilePipelines() // doesn't need to special-case them - the second call for a // duplicate is a cheap pointer copy after the canonical mode has // filled the canonical slot. + // + // Structurally unreachable cells (reserved texture modes, two-pass + // fallback render modes with no texture, single-pass dual-source + // on hardware that lacks it) are skipped via IsBatchShaderReachable. if (precompile_sync) { + const bool dual_source = m_supports_dual_source_blend; for (uint8_t render_mode = 0; render_mode < 4; render_mode++) { for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++) { + if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) + continue; + for (uint8_t dithering = 0; dithering < 2; dithering++) { for (uint8_t interlacing = 0; interlacing < 2; interlacing++) @@ -791,6 +803,9 @@ bool GPU_HW_D3D12::CompilePipelines() { for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++) { + if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) + continue; + for (uint8_t dithering = 0; dithering < 2; dithering++) { for (uint8_t interlacing = 0; interlacing < 2; interlacing++) @@ -981,6 +996,10 @@ void GPU_HW_D3D12::ShaderCompileThreadEntryPoint() // PSO compile worth of latency (D3D12 PSO compiles can be ~50-200 // ms for the heavier filters - the same cell-level bound applies // to ShaderCompileThreadEntryPoint on the D3D11 side). + // + // Structurally unreachable cells are skipped via + // IsBatchShaderReachable. + const bool dual_source = m_supports_dual_source_blend; for (uint8_t depth_test = 0; depth_test < 2; depth_test++) { for (uint8_t render_mode = 0; render_mode < 4; render_mode++) @@ -989,6 +1008,9 @@ void GPU_HW_D3D12::ShaderCompileThreadEntryPoint() { for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++) { + if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) + continue; + for (uint8_t dithering = 0; dithering < 2; dithering++) { for (uint8_t interlacing = 0; interlacing < 2; interlacing++) diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index 36ac0406..e3c4fd47 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -1467,10 +1467,14 @@ bool GPU_HW_Vulkan::CompilePipelines() // still build everything upfront here. const GPUShaderPrecompileMode precompile_mode = g_settings.gpu_shader_precompile_mode; const bool precompile_sync = (precompile_mode == GPUShaderPrecompileMode::Enabled); - const uint32_t batch_shader_progress_units = - precompile_sync ? static_cast(4 * 9 * 2 * 2) : 0u; + // Reachable cell counts for each matrix - see IsBatchShaderReachable + // in gpu_hw.h. The PSO matrix multiplies by 3 (depth_test) * 5 + // (transparency_mode); these axes don't affect reachability, so the + // per-(render_mode, texture_mode) skip rule is independent of them. + const uint32_t reachable_batch_cells = CountReachableBatchShaders(m_supports_dual_source_blend); + const uint32_t batch_shader_progress_units = precompile_sync ? reachable_batch_cells : 0u; const uint32_t batch_pipeline_progress_units = - precompile_sync ? static_cast(3 * 4 * 5 * 9 * 2 * 2) : 0u; + precompile_sync ? reachable_batch_cells * 3u * 5u : 0u; // Non-batch units only counted when we build them upfront. The // fullscreen-quad VS (1), VRAM fill (4), VRAM copy (2), VRAM // write (2), VRAM update depth (1), VRAM readback (1), display @@ -1518,12 +1522,20 @@ bool GPU_HW_Vulkan::CompilePipelines() // array index), so the precompile loop doesn't need to special- // case them - the entry for 3 / 7 simply forwards to the // canonical slot. + // + // Structurally unreachable cells (reserved texture modes, two-pass + // fallback render modes with no texture, single-pass dual-source + // on hardware that lacks it) are skipped via IsBatchShaderReachable. if (precompile_sync) { + const bool dual_source = m_supports_dual_source_blend; for (uint8_t render_mode = 0; render_mode < 4; render_mode++) { for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++) { + if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) + continue; + for (uint8_t dithering = 0; dithering < 2; dithering++) { for (uint8_t interlacing = 0; interlacing < 2; interlacing++) @@ -1547,6 +1559,9 @@ bool GPU_HW_Vulkan::CompilePipelines() { for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++) { + if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) + continue; + for (uint8_t dithering = 0; dithering < 2; dithering++) { for (uint8_t interlacing = 0; interlacing < 2; interlacing++) @@ -1750,6 +1765,10 @@ void GPU_HW_Vulkan::ShaderCompileThreadEntryPoint() // flag is checked between cells so DestroyPipelines can stop the // worker within at most one PSO compile of latency (Vulkan PSO // compiles can be ~50-200 ms with the heavier texture filters). + // + // Structurally unreachable cells are skipped via + // IsBatchShaderReachable. + const bool dual_source = m_supports_dual_source_blend; for (uint8_t depth_test = 0; depth_test < 3; depth_test++) { for (uint8_t render_mode = 0; render_mode < 4; render_mode++) @@ -1758,6 +1777,9 @@ void GPU_HW_Vulkan::ShaderCompileThreadEntryPoint() { for (uint8_t texture_mode = 0; texture_mode < 9; texture_mode++) { + if (!IsBatchShaderReachable(static_cast(render_mode), texture_mode, dual_source)) + continue; + for (uint8_t dithering = 0; dithering < 2; dithering++) { for (uint8_t interlacing = 0; interlacing < 2; interlacing++)