From 93e5db5239697f36e086249dda42d802b5a8a2b7 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Mon, 18 May 2026 05:24:45 +0000 Subject: [PATCH] core/d3d11: skip batch matrix rebuild on chroma_smoothing-only flip D3D11 parity port of the chroma_smoothing partial-clear that landed for D3D12 in 57ac62e. Same motivation: chroma_smoothing is a DefineMacro inside GenerateDisplayFragmentShader only - it never reaches the batch pixel shader matrix or the VRAM ops pixel shaders. The previous D3D11 patches kept it in shader_source_changed, so on D3D11 a chroma toggle still drove the full DestroyShaders + DestroyStateObjects + CreateStateObjects + CompileShaders round trip and walked the 144-cell batch pixel shader matrix synchronously when shader_precompile_mode = Enabled. Reuses the display_only_source_changed signal added by 57ac62e (no API change). GPU_HW_D3D11::UpdateSettings checks it first inside the shader_source_changed gate, before only_dim_changed and the full-flush branch. The display-only branch calls a new helper, RebuildDisplayPixelShaders, that recompiles the 2x3 m_display_pixel_shaders matrix against the new m_chroma_smoothing value (which UpdateHWSettings has already written into the member). The 144-cell batch pixel shader matrix, the VRAM ops pixel shaders, the blend / depth-stencil / input layout state objects, and the batch vertex shaders all stay valid because none of them carry chroma_smoothing as ctor or compile-time state. CompileShaders is refactored to call the same helper for its initial display-shader build, so the 6-cell compile loop only lives in one place. Progress tracking is bumped six times inline after the helper returns to match the pre-patch progress bar units. chroma_smoothing only takes effect on the depth_24bit paths (see the '&& m_chroma_smoothing' guard in GenerateDisplayFragmentShader), so technically only three of the six shaders actually depend on it. Rebuilding all six anyway keeps the path simple - on a chroma toggle the three depth_24bit=false shaders re-resolve to cache hits on their existing HLSL hashes in m_shader_cache (instant GetPixelShader return), and the three depth_24bit=true shaders pick up the new SMOOTH_CHROMA value via a fresh D3DCompile. UpdateSettings also handles the precompile_mode contract on the rebuild side now that the partial-clear branch skips CompileShaders: Enabled: RebuildDisplayPixelShaders is synchronous, so the next draw doesn't stutter on display shader fault-in. Lazy: relaunch the background worker that StopShaderCompileThread joined at the top of UpdateSettings. The worker only walks the batch pixel shader matrix, so it doesn't care about chroma_smoothing - keeping it stopped after a chroma toggle would just starve mid-warmup sessions of further background fill. Disabled: no worker, no extra work - matches the standing 'skip all compilation at init' contract for the batch matrix. The other rebuild branches (only_dim_changed, full destroy) are unchanged - they're hit only when something the batch matrix genuinely depends on changed, and the dim cache / full-flush paths from 00cf11f already do the right thing there. Cost matrix after this patch (D3D11, default settings, shader_precompile_mode = Enabled, 144 reachable batch shader cells): Toggle Before (00cf11f) After this patch chroma_smoothing ~5-15 sec ~600 ms (full matrix walk) (6 display shaders) texture_filter (warm) instant unchanged texture_filter (cold) ~5-15 sec unchanged (new sub-cube) resolution_scale zero unchanged true_color zero unchanged scaled_dithering zero unchanged multisamples ~5-15 sec unchanged (necessary) per_sample_shading ~5-15 sec unchanged (necessary) uv_limits ~5-15 sec unchanged (necessary) pgxp_depth ~5-15 sec unchanged (necessary) disable_color_persp. ~5-15 sec unchanged (necessary) shader_precompile_mode ~5-15 sec unchanged (intentional) Every remaining 'still pays' toggle in the matrix is one that genuinely invalidates batch HLSL across the board; the D3D11 backend's UpdateSettings path is now structurally on par with the D3D12 backend's post-57ac62e cost matrix. Test: - D3D11, shader_precompile = Enabled. Toggle 24-bit chroma smoothing on / off: ~600 ms pause (6 display pixel shader rebuilds), no batch matrix progress OSD. Batch draw remains hot - no stutter on the next frame. - Same toggle under Lazy: ~600 ms pause for the display rebuild, worker is paused for the duration of UpdateSettings then relaunched, batch matrix continues warming. - Same toggle under Disabled: ~600 ms pause for the display rebuild, no worker, batch matrix stays as it was (mostly empty until faulted by draws). - Toggle chroma_smoothing AT THE SAME TIME as another setting (e.g. via batch settings reload): display_only_source_changed returns false (other_source_diff is true), the only_dim or full-flush branch fires as before. --- src/core/gpu_hw_d3d11.cpp | 99 ++++++++++++++++++++++++++++++--------- src/core/gpu_hw_d3d11.h | 1 + 2 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index cbefcd51..1a7048db 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -609,8 +609,15 @@ void GPU_HW_D3D11::UpdateSettings() // be lazy-populated, other filters' sub-cubes stay valid". When // set alongside shader_source_changed it picks out the // filter-only-changed case. - bool framebuffer_changed, shaders_changed, only_dim_changed, shader_source_changed; - UpdateHWSettings(&framebuffer_changed, &shaders_changed, &only_dim_changed, nullptr, &shader_source_changed); + // + // display_only_source_changed: chroma_smoothing flipped and + // nothing else affecting shader source changed. The batch pixel + // shader matrix, VRAM ops pixel shaders, and state objects all + // stay valid; only the 6-slot display pixel shader cache needs + // to go. Mirrors the D3D12 partial-clear from 57ac62e. + bool framebuffer_changed, shaders_changed, only_dim_changed, shader_source_changed, display_only_source_changed; + UpdateHWSettings(&framebuffer_changed, &shaders_changed, &only_dim_changed, nullptr, &shader_source_changed, + &display_only_source_changed); if (framebuffer_changed) { @@ -623,7 +630,29 @@ void GPU_HW_D3D11::UpdateSettings() if (shader_source_changed) { - if (only_dim_changed) + if (display_only_source_changed) + { + // chroma_smoothing flipped and nothing else - rebuild the six + // display pixel shaders against the new m_chroma_smoothing + // value (which UpdateHWSettings has already written into the + // member). The 144-cell batch pixel shader matrix, the VRAM + // ops pixel shaders, and the blend / depth-stencil / input + // layout state objects all stay valid. Cost is 6 D3DCompile + // + 6 CreatePixelShader calls, a fraction of a second + // instead of the full CompileShaders pass. + (void)RebuildDisplayPixelShaders(); + + // Relaunch the Lazy worker StopShaderCompileThread joined at + // the top of UpdateSettings; this branch doesn't go through + // CompileShaders so the launch site there isn't hit. Mirrors + // the D3D12 chroma partial-clear path. + if (g_settings.gpu_shader_precompile_mode == GPUShaderPrecompileMode::Lazy) + { + m_shader_compile_thread_quit.store(false, std::memory_order_relaxed); + m_shader_compile_thread = std::thread(&GPU_HW_D3D11::ShaderCompileThreadEntryPoint, this); + } + } + else 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 @@ -648,10 +677,10 @@ void GPU_HW_D3D11::UpdateSettings() 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. + // (multisamples / per-sample shading / UV limits / PGXP depth + // / colour perspective / precompile mode) invalidates EVERY + // sub-cube because those settings bake into the HLSL + // identically for every filter slot. DestroyShaders(); DestroyStateObjects(); CreateStateObjects(); @@ -1178,20 +1207,10 @@ bool GPU_HW_D3D11::CompileShaders() progress.Increment(); - for (uint8_t depth_24bit = 0; depth_24bit < 2; depth_24bit++) - { - for (uint8_t interlacing = 0; interlacing < 3; interlacing++) - { - const std::string ps = shadergen.GenerateDisplayFragmentShader( - static_cast(depth_24bit), static_cast(interlacing), - static_cast(depth_24bit) && m_chroma_smoothing); - m_display_pixel_shaders[depth_24bit][interlacing] = shader_cache.GetPixelShader(m_device.Get(), ps); - if (!m_display_pixel_shaders[depth_24bit][interlacing]) - return false; - - progress.Increment(); - } - } + if (!RebuildDisplayPixelShaders()) + return false; + for (uint8_t i = 0; i < 6; i++) + progress.Increment(); if (m_downsample_mode == GPUDownsampleMode::Adaptive) { @@ -1457,6 +1476,44 @@ void GPU_HW_D3D11::DestroyShaders() m_batch_input_layout.Reset(); } +bool GPU_HW_D3D11::RebuildDisplayPixelShaders() +{ + // (Re)compile the 2x3 display pixel shader matrix against the + // current m_chroma_smoothing. Called from CompileShaders during + // the initial build and from UpdateSettings on a + // chroma_smoothing-only flip - chroma_smoothing is a DefineMacro + // inside GenerateDisplayFragmentShader only (see + // gpu_hw_shadergen.cpp:1056), so the batch pixel shader matrix + // and the VRAM ops pixel shaders stay valid through a chroma + // toggle and don't need rebuilding. Costs 6 D3DCompile + + // CreatePixelShader calls (a fraction of a second on a modern + // GPU) instead of a full CompileShaders pass walking the entire + // 144-cell batch matrix. + // + // chroma_smoothing only takes effect on the depth_24bit paths + // (see the '&& m_chroma_smoothing' guard below), so technically + // only three of the six display shaders depend on it. Rebuilding + // all six anyway keeps this path simple - on a chroma toggle the + // three depth_24bit=false shaders re-resolve to cache hits on + // their existing HLSL hashes (instant), and the three + // depth_24bit=true shaders pick up the new SMOOTH_CHROMA value. + if (!m_shadergen) + return false; + for (uint8_t depth_24bit = 0; depth_24bit < 2; depth_24bit++) + { + for (uint8_t interlacing = 0; interlacing < 3; interlacing++) + { + const std::string ps = m_shadergen->GenerateDisplayFragmentShader( + static_cast(depth_24bit), static_cast(interlacing), + static_cast(depth_24bit) && m_chroma_smoothing); + m_display_pixel_shaders[depth_24bit][interlacing] = m_shader_cache.GetPixelShader(m_device.Get(), ps); + if (!m_display_pixel_shaders[depth_24bit][interlacing]) + return false; + } + } + return true; +} + void GPU_HW_D3D11::UploadUniformBuffer(const void* data, uint32_t data_size) { const auto res = m_uniform_stream_buffer.Map(m_context.Get(), MAX_UNIFORM_BUFFER_SIZE, data_size); diff --git a/src/core/gpu_hw_d3d11.h b/src/core/gpu_hw_d3d11.h index ed724db1..3f61a530 100644 --- a/src/core/gpu_hw_d3d11.h +++ b/src/core/gpu_hw_d3d11.h @@ -136,6 +136,7 @@ private: bool CompileShaders(); void DestroyShaders(); + bool RebuildDisplayPixelShaders(); // Lazy batch-fragment-shader compile path. The shader matrix // starts all-null; GetBatchPixelShader fills a slot on demand