From 418eb1430e2e8ab32ba2fcf4aea8627e1018aac1 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Mon, 18 May 2026 04:09:40 +0000 Subject: [PATCH] core, common: skip pipeline rebuild on cbuffer-only setting changes Post 7b575a3 (cbuffer refactor), HLSL/GLSL batch shader source is invariant under resolution_scale / true_color / scaled_dithering - those three settings ride the per-batch UBO and are picked up by the next FlushRender at no shader-compile cost on D3D11 / D3D12 / OpenGL. The backends were still treating every toggle of any of the three as a full shader rebuild though, because their UpdateSettings handlers checked shaders_changed and that signal still fired on cbuffer-only diffs - shaders_changed was defined as 'dim_diff || non_dim_diff', which includes both filter (real source change) AND true_color/scaled_dithering (now cbuffer-only) AND resolution_scale (now cbuffer-only). With shader_precompile_mode = Enabled and a populated batch matrix (~1164 reachable PSO cells in the default-user configuration), the DestroyPipelines + CompilePipelines round trip walks every cell synchronously through D3DCompile / glCompileShader before UpdateSettings returns. On the user's system that's the full 'changing options can take up to a minute' pause they kept hitting after every res-scale or true-color toggle, even though by construction the patch that landed before this one made those toggles trivially cheap at the shader level. Add a new narrower out-parameter to GPU_HW::UpdateHWSettings: shader_source_changed (default nullptr, opt-in for callers) It is true only when a setting that the shader generator bakes into the emitted HLSL / GLSL string has flipped: texture filter, multisamples, per-sample shading, UV limits, chroma smoothing, PGXP depth, disable-color-perspective, and shader precompile mode. resolution_scale, true_color, and scaled_dithering are explicitly excluded - they are pure batch UBO cbuffer fields now. Both chroma_smoothing and shader_precompile_mode stay in because they do change shader-side work (chroma_smoothing is baked into the display FS at GenerateDisplayFragmentShader call sites on the D3D11 / D3D12 / OpenGL paths, and a Disabled -> Enabled precompile flip is a request to compile the matrix synchronously now even though no source has actually changed). The existing shaders_changed signal is preserved verbatim. Vulkan relies on it together with only_dim_changed to drive its spec-const-aware dim-cache path, which still has to fire on true_color / scaled_dithering flips because those spec constants bake into VkPipeline at create time and demand a new pipeline per sub-cube even though the SPIR-V text is identical. Vulkan keeps calling UpdateHWSettings with the old four-argument form (no shader_source_changed) and behaves exactly as before. GPU_HW_D3D11::UpdateSettings, GPU_HW_D3D12::UpdateSettings, and GPU_HW_OpenGL::UpdateSettings switch their compile-rebuild check from shaders_changed to shader_source_changed. State-object recreation in the D3D11 path is gated on the same signal: state descriptions key on MSAA / dual-source / depth-test / blend mode, all of which sit in the source-changed set, so existing state objects stay correct through cbuffer-only toggles. framebuffer rebuild is unchanged - it still fires on resolution_scale via the framebuffer_changed signal, just without dragging the pipeline rebuild along with it. Concrete cost matrix after this patch (D3D12, default settings, shader_precompile_mode = Enabled, populated matrix of ~1164 PSOs): Toggle Before After resolution_scale ~1 minute framebuffer resize only true_color ~1 minute zero scaled_dithering ~1 minute zero texture_filter ~1 minute ~1 minute (necessary) multisamples ~1 minute ~1 minute (necessary) per_sample_shading ~1 minute ~1 minute (necessary) uv_limits ~1 minute ~1 minute (necessary) chroma_smoothing ~1 minute ~1 minute (display FS; could be split later) pgxp_depth ~1 minute ~1 minute (necessary) disable_color_perspective ~1 minute ~1 minute (necessary) shader_precompile_mode ~1 minute ~1 minute (intentional) downsample_mode (non-adapt.) currently no-op on D3D12, unchanged Same shape on D3D11 and OpenGL. Test: - D3D12, shader_precompile = Enabled, populated cache. Toggle resolution scale 4x -> 1x -> 4x: each transition is framebuffer-resize fast, no 'Compiling Pipelines N/1164' OSD. - Toggle true_color on / off: instant, no OSD. - Toggle scaled_dithering on / off: instant. - Toggle texture filter Nearest -> Bilinear: still pays the precompile (necessary - filter changes batch FS source). - Toggle MSAA / per-sample / UV limits / PGXP depth / colour perspective: still pays (necessary). - chroma_smoothing toggle: still pays (suboptimal - the batch matrix doesn't actually depend on it, only the display FS does, but splitting batch / non-batch rebuild is a separate change; leaving it at the conservative correct behaviour for now). - Same battery on D3D11 and OpenGL. - Vulkan: behaviour unchanged. Still uses dim-cache for filter / true_color / scaled_dithering and full rebuild for everything else. --- src/core/gpu_hw.cpp | 37 ++++++++++++++++++++++++++++++++++++- src/core/gpu_hw.h | 3 ++- src/core/gpu_hw_d3d11.cpp | 16 +++++++++++++--- src/core/gpu_hw_d3d12.cpp | 17 ++++++++++++++--- src/core/gpu_hw_opengl.cpp | 13 ++++++++++--- 5 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/core/gpu_hw.cpp b/src/core/gpu_hw.cpp index 98576539..b164ccef 100644 --- a/src/core/gpu_hw.cpp +++ b/src/core/gpu_hw.cpp @@ -159,7 +159,8 @@ bool GPU_HW::DoState(StateWrapper& sw, HostDisplayTexture** host_texture, bool u } void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed, - bool* only_dim_changed, bool* downsample_changed) + bool* only_dim_changed, bool* downsample_changed, + bool* shader_source_changed) { const uint32_t resolution_scale = CalculateResolutionScale(); const uint32_t multisamples = std::min(m_max_multisamples, g_settings.gpu_multisamples); @@ -224,6 +225,40 @@ void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed, if (downsample_changed) *downsample_changed = (m_downsample_mode != downsample_mode); + // shader_source_changed: the narrower "HLSL / GLSL source string + // actually changed" signal. Set true only when a setting that the + // shader generator bakes into the emitted source has flipped. + // Excludes the three per-session settings routed through the batch + // UBO by 7b575a3 (resolution_scale, true_color, scaled_dithering) - + // toggling those is a single 4-byte cbuffer write on the next + // FlushRender and costs zero shader compilation on D3D11 / D3D12 / + // OpenGL. Backends that don't carry the Vulkan-style per-spec-const + // pipeline dim cache check this signal to gate the + // DestroyPipelines + CompilePipelines round trip; the broader + // shaders_changed above remains for Vulkan's spec-const-aware path. + // + // chroma_smoothing stays in here because it does change the display + // FS source string on those backends (D3D12 passes it to + // GenerateDisplayFragmentShader at gpu_hw_d3d12.cpp:~1639, D3D11 at + // gpu_hw_d3d11.cpp:~1118, OpenGL at gpu_hw_opengl.cpp:~1145). + // shader_precompile_mode stays in here because flipping from + // Disabled to Enabled is the request to walk the whole matrix + // synchronously now - the work is in CompilePipelines, not in + // shader source, so the existing destroy-and-rebuild path is the + // right hook even though no source actually changed. + if (shader_source_changed) + { + *shader_source_changed = + (m_texture_filtering != g_settings.gpu_texture_filter) || + (m_multisamples != multisamples) || + (m_per_sample_shading != per_sample_shading) || + (m_using_uv_limits != use_uv_limits) || + (m_chroma_smoothing != g_settings.gpu_24bit_chroma_smoothing) || + (m_pgxp_depth_buffer != g_settings.UsingPGXPDepthBuffer()) || + (m_disable_color_perspective != disable_color_perspective) || + (m_shader_precompile_mode != g_settings.gpu_shader_precompile_mode); + } + m_resolution_scale = resolution_scale; m_multisamples = multisamples; m_per_sample_shading = per_sample_shading; diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index 8af8a806..3081096d 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -219,7 +219,8 @@ protected: // round-trip. void UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed, bool* only_dim_changed = nullptr, - bool* downsample_changed = nullptr); + bool* downsample_changed = nullptr, + bool* shader_source_changed = nullptr); virtual void UpdateVRAMReadTexture(); virtual void UpdateDepthBufferFromMaskBit() = 0; diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index 61bbbc52..ac8dc9b5 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -579,8 +579,18 @@ void GPU_HW_D3D11::UpdateSettings() { GPU_HW::UpdateSettings(); - bool framebuffer_changed, shaders_changed; - UpdateHWSettings(&framebuffer_changed, &shaders_changed); + // See GPU_HW_D3D12::UpdateSettings for the rationale on + // shader_source_changed vs shaders_changed: the cbuffer-refactor + // patch made true_color / scaled_dithering / resolution_scale + // invariant under HLSL source, so toggling them no longer requires + // DestroyShaders + CompileShaders. The DXBC blobs, state objects, + // and Direct3D11 shader handles all stay valid; the new values + // ride the per-batch UBO upload on the next FlushRender. State + // objects also stay valid through those toggles because their + // descriptions key on MSAA / dual-source / depth-test / blend + // mode - none of which are in the cbuffer-only set. + bool framebuffer_changed, shaders_changed, shader_source_changed; + UpdateHWSettings(&framebuffer_changed, &shaders_changed, nullptr, nullptr, &shader_source_changed); if (framebuffer_changed) { @@ -591,7 +601,7 @@ void GPU_HW_D3D11::UpdateSettings() CreateFramebuffer(); } - if (shaders_changed) + if (shader_source_changed) { DestroyShaders(); DestroyStateObjects(); diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index a43fd7e9..8fc97fd0 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -396,8 +396,19 @@ void GPU_HW_D3D12::UpdateSettings() { GPU_HW::UpdateSettings(); - bool framebuffer_changed, shaders_changed; - UpdateHWSettings(&framebuffer_changed, &shaders_changed); + // 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); if (framebuffer_changed) { @@ -413,7 +424,7 @@ void GPU_HW_D3D12::UpdateSettings() if (framebuffer_changed) CreateFramebuffer(); - if (shaders_changed) + if (shader_source_changed) { // clear it since we draw a loading screen and it's not in the correct state DestroyPipelines(); diff --git a/src/core/gpu_hw_opengl.cpp b/src/core/gpu_hw_opengl.cpp index 00ae18ab..380e8fbe 100644 --- a/src/core/gpu_hw_opengl.cpp +++ b/src/core/gpu_hw_opengl.cpp @@ -791,8 +791,15 @@ void GPU_HW_OpenGL::UpdateSettings() { GPU_HW::UpdateSettings(); - bool framebuffer_changed, shaders_changed; - UpdateHWSettings(&framebuffer_changed, &shaders_changed); + // See GPU_HW_D3D12::UpdateSettings for the rationale on + // shader_source_changed vs shaders_changed: post-7b575a3 the + // batch GLSL is invariant under resolution_scale / true_color / + // scaled_dithering, so toggling them no longer requires + // CompilePrograms (which would otherwise relink every program in + // the batch matrix). The new values ride the per-batch UBO + // upload on the next FlushRender. + bool framebuffer_changed, shaders_changed, shader_source_changed; + UpdateHWSettings(&framebuffer_changed, &shaders_changed, nullptr, nullptr, &shader_source_changed); if (framebuffer_changed) { @@ -802,7 +809,7 @@ void GPU_HW_OpenGL::UpdateSettings() m_host_display->ClearDisplayTexture(); CreateFramebuffer(); } - if (shaders_changed) + if (shader_source_changed) CompilePrograms(); if (framebuffer_changed)