diff --git a/src/core/gpu_hw.cpp b/src/core/gpu_hw.cpp index 3e5696b4..b2d2f1ee 100644 --- a/src/core/gpu_hw.cpp +++ b/src/core/gpu_hw.cpp @@ -129,7 +129,7 @@ bool GPU_HW::DoState(StateWrapper& sw, HostDisplayTexture** host_texture, bool u } void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed, - bool* only_filter_changed) + bool* only_dim_changed) { const uint32_t resolution_scale = CalculateResolutionScale(); const uint32_t multisamples = std::min(m_max_multisamples, g_settings.gpu_multisamples); @@ -141,29 +141,39 @@ void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed, *framebuffer_changed = (m_resolution_scale != resolution_scale || m_multisamples != multisamples || m_downsample_mode != downsample_mode); - // Split the shader-affecting setting comparison into "filter - // changed" and "anything else changed". A filter-only change can - // be served by a backend that keeps a per-filter sub-cube of - // batch pipelines populated across swaps - the previous filter's - // PSOs are still valid and reachable, so DestroyPipelines can be - // skipped and CompilePipelines just lazy-populates the new - // filter's sub-cube on top of whatever was already there. Any - // non-filter change (resolution, MSAA, true colour, PGXP depth, - // dithering, precompile mode, ...) flips the per-session spec - // constants or the structural SPIR-V blob choice applied to - // EVERY filter sub-cube and therefore requires a full flush. - const bool filter_diff = (m_texture_filtering != g_settings.gpu_texture_filter); - const bool non_filter_diff = + // Split the shader-affecting setting comparison into "cache- + // dimensioned settings changed" and "non-dimensioned settings + // changed". A change confined to dimensioned settings can be + // served by a backend that keeps a per-(filter, true_color, + // scaled_dithering) sub-cube of batch pipelines populated across + // toggles - the previous sub-cube's PSOs are still valid and + // reachable, so DestroyPipelines can be skipped and + // CompilePipelines just lazy-populates the new sub-cube on top of + // whatever was already there. Any non-dimensioned change + // (resolution scale, MSAA, per-sample shading, UV limits, chroma + // smoothing, downsample mode, PGXP depth, colour perspective, + // precompile mode) flips per-session spec constants or structural + // SPIR-V blob choice applied to EVERY sub-cube and therefore + // requires a full flush. + // + // GPUTextureFilter, m_true_color and m_scaled_dithering are the + // three settings the Vulkan backend currently dimensions over. + // The set may extend to other 2-value spec consts in the future + // without changing this API. + const bool filter_diff = (m_texture_filtering != g_settings.gpu_texture_filter); + const bool true_color_diff = (m_true_color != g_settings.gpu_true_color); + const bool scaled_dithering_diff = (m_scaled_dithering != g_settings.gpu_scaled_dithering); + const bool dim_diff = filter_diff || true_color_diff || scaled_dithering_diff; + const bool non_dim_diff = (m_resolution_scale != resolution_scale || m_multisamples != multisamples || - m_true_color != g_settings.gpu_true_color || m_per_sample_shading != per_sample_shading || - m_scaled_dithering != g_settings.gpu_scaled_dithering || + m_per_sample_shading != per_sample_shading || m_using_uv_limits != use_uv_limits || m_chroma_smoothing != g_settings.gpu_24bit_chroma_smoothing || m_downsample_mode != downsample_mode || m_pgxp_depth_buffer != g_settings.UsingPGXPDepthBuffer() || m_disable_color_perspective != disable_color_perspective || m_shader_precompile_mode != g_settings.gpu_shader_precompile_mode); - *shaders_changed = filter_diff || non_filter_diff; - if (only_filter_changed) - *only_filter_changed = filter_diff && !non_filter_diff; + *shaders_changed = dim_diff || non_dim_diff; + if (only_dim_changed) + *only_dim_changed = dim_diff && !non_dim_diff; m_resolution_scale = resolution_scale; m_multisamples = multisamples; diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index 9a7ad917..d1110c7a 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -180,18 +180,18 @@ protected: static_cast(rgba >> 24) * (1.0f / 255.0f)); } - // The optional out-parameter only_filter_changed is set to true - // iff *shaders_changed is true AND the texture filter setting is - // the only shader-affecting setting that differs from the cached - // session state. Backends with a filter-dimensioned batch - // pipeline cache (Vulkan) can read this to skip DestroyPipelines - // on a filter-only swap, preserving previously-built sub-cubes - // for instant return when the user cycles back through filters. + // The optional out-parameter only_dim_changed is set to true iff + // *shaders_changed is true AND the change is confined to the + // settings the backend's batch pipeline cache is dimensioned over + // (currently: texture filter, true colour, scaled dithering). + // Backends with a dimensioned batch cache (Vulkan) can read this + // to skip DestroyPipelines on such a toggle, preserving previously- + // built sub-cubes for instant return when the user cycles back. // Backends without the dimension (D3D11 / D3D12 today) can ignore // this and continue doing a full destroy + recompile on any // shaders_changed event. void UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed, - bool* only_filter_changed = nullptr); + bool* only_dim_changed = nullptr); virtual void UpdateVRAMReadTexture(); virtual void UpdateDepthBufferFromMaskBit() = 0; diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index d5f500e3..c17bd191 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -797,8 +797,8 @@ void GPU_HW_Vulkan::UpdateSettings() // becomes a no-op for paths that pre-stop here.) StopShaderCompileThread(); - bool framebuffer_changed, shaders_changed, only_filter_changed; - UpdateHWSettings(&framebuffer_changed, &shaders_changed, &only_filter_changed); + bool framebuffer_changed, shaders_changed, only_dim_changed; + UpdateHWSettings(&framebuffer_changed, &shaders_changed, &only_dim_changed); if (framebuffer_changed) { @@ -816,31 +816,33 @@ void GPU_HW_Vulkan::UpdateSettings() if (shaders_changed) { - if (only_filter_changed) + if (only_dim_changed) { - // Filter is the only shader-affecting setting that changed. - // m_batch_pipelines / m_batch_fragment_shaders are filter- - // dimensioned, the previous filter's sub-cube is still valid - // and stays populated. CompilePipelines just lazy-populates - // the new filter's sub-cube on top: in Enabled mode the - // precompile_sync loop walks the matrix calling GetBatchPipeline - // which now indexes by the new m_texture_filtering, so the - // OTHER filter sub-cubes are untouched. In Lazy / Disabled the - // new sub-cube stays empty until first draw / the worker - // reaches each cell. + // Only cache-dimensioned settings changed (texture filter, + // true colour, and/or scaled dithering). m_batch_pipelines + // is dimensioned over all three; the previous tuple's sub- + // cube is still valid and stays populated. CompilePipelines + // just lazy-populates the new tuple's sub-cube on top: in + // Enabled mode the precompile_sync loop walks the matrix + // calling GetBatchPipeline with the new (m_texture_filtering, + // m_true_color, m_scaled_dithering) tuple, 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 + // Cycling back to a previously-visited tuple is instant - no // vkCreateGraphicsPipelines call, no destroy/recreate, just an // atomic load of an already-filled slot. CompilePipelines(); } else { - // Full flush: any non-filter shader-affecting change - // (resolution scale, MSAA, true colour, PGXP depth, ...) - // invalidates every filter's sub-cube because per-session - // spec constants and structural SPIR-V choice apply - // identically to all of them. + // Full flush: any non-dimensioned shader-affecting change + // (resolution scale, MSAA, per-sample shading, UV limits, + // chroma smoothing, downsample mode, PGXP depth, colour + // perspective, precompile mode) invalidates every sub-cube + // because per-session spec constants and structural SPIR-V + // choice apply identically to all of them. DestroyPipelines(); CompilePipelines(); } @@ -1589,7 +1591,8 @@ bool GPU_HW_Vulkan::CompilePipelines() for (uint8_t interlacing = 0; interlacing < 2; interlacing++) { VkPipeline pipeline = - GetBatchPipeline(m_texture_filtering, depth_test, render_mode, texture_mode, transparency_mode, + GetBatchPipeline(m_texture_filtering, m_true_color, m_scaled_dithering, + depth_test, render_mode, texture_mode, transparency_mode, static_cast(dithering), static_cast(interlacing)); if (pipeline == VK_NULL_HANDLE) return false; @@ -1875,6 +1878,20 @@ void GPU_HW_Vulkan::ShaderCompileThreadEntryPoint() // sub-cubes - they would never be looked up at draw time and the // PSOs would just sit unused. // + // The (m_true_color, m_scaled_dithering) snapshot is taken once at + // worker entry. The worker is responsible only for the current + // (true_color, scaled_dithering) combo's seven filter sub-cubes - + // walking all four combos in the background would balloon warm-up + // time to several minutes on a cold pipeline cache, well past + // "useful". Toggling true_color or scaled_dithering at runtime is + // serviced the same way a filter swap is: the previous combo's + // sub-cubes stay populated and are instantly addressable on a + // cycle-back, while the new combo gets a sync compile of its + // current filter sub-cube (Enabled) or lazy fault-in (Lazy / + // Disabled), plus a new background worker rooted at the new + // (true_color, scaled_dithering) tuple covering the other six + // filters. + // // The quit 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 @@ -1884,6 +1901,8 @@ void GPU_HW_Vulkan::ShaderCompileThreadEntryPoint() // IsBatchShaderReachable. const bool dual_source = m_supports_dual_source_blend; const uint8_t current_filter = static_cast(m_texture_filtering); + const bool true_color = m_true_color; + const bool scaled_dithering = m_scaled_dithering; for (uint8_t f_offset = 0; f_offset < static_cast(GPUTextureFilter::Count); f_offset++) { const uint8_t filter_idx = @@ -1911,7 +1930,8 @@ void GPU_HW_Vulkan::ShaderCompileThreadEntryPoint() if (m_shader_compile_thread_quit.load(std::memory_order_relaxed)) return; - GetBatchPipeline(filter, depth_test, render_mode, texture_mode, transparency_mode, + GetBatchPipeline(filter, true_color, scaled_dithering, depth_test, render_mode, + texture_mode, transparency_mode, static_cast(dithering), static_cast(interlacing)); } } @@ -2028,7 +2048,8 @@ VkShaderModule GPU_HW_Vulkan::GetBatchFragmentShader(GPUTextureFilter filter, ui return fresh; } -VkPipeline GPU_HW_Vulkan::GetBatchPipeline(GPUTextureFilter filter, uint8_t depth_test, uint8_t render_mode, +VkPipeline GPU_HW_Vulkan::GetBatchPipeline(GPUTextureFilter filter, bool true_color, bool scaled_dithering, + uint8_t depth_test, uint8_t render_mode, uint8_t texture_mode, uint8_t transparency_mode, bool dithering, bool interlacing) { @@ -2040,7 +2061,8 @@ VkPipeline GPU_HW_Vulkan::GetBatchPipeline(GPUTextureFilter filter, uint8_t dept texture_mode; std::atomic& slot = - m_batch_pipelines[static_cast(filter)][depth_test][render_mode][lookup_mode] + m_batch_pipelines[static_cast(filter)][static_cast(true_color)] + [static_cast(scaled_dithering)][depth_test][render_mode][lookup_mode] [transparency_mode][static_cast(dithering)][static_cast(interlacing)]; // Fast path: lock-free atomic load. This is what DrawBatchVertices @@ -2113,8 +2135,14 @@ VkPipeline GPU_HW_Vulkan::GetBatchPipeline(GPUTextureFilter filter, uint8_t dept // id = 102 TRANSPARENCY_ONLY_TRANSPARENT (bool, per-call) // id = 103 DITHERING (bool, per-call) // id = 104 INTERLACING (bool, per-call) - // id = 105 DITHERING_SCALED (bool, per-session) - // id = 106 TRUE_COLOR (bool, per-session) + // id = 105 DITHERING_SCALED (bool, cache-dim - the + // scaled_dithering + // argument is a dimension + // of m_batch_pipelines so + // toggling does not flush + // other sub-cubes) + // id = 106 TRUE_COLOR (bool, cache-dim - same + // mechanism as id 105) // id = 107 PALETTE_4_BIT (bool, per-call - // actual_texture_mode == 0) // id = 108 PALETTE_8_BIT (bool, per-call - @@ -2148,8 +2176,8 @@ VkPipeline GPU_HW_Vulkan::GetBatchPipeline(GPUTextureFilter filter, uint8_t dept fs_spec.AddBool(102, brm == BatchRenderMode::OnlyTransparent); fs_spec.AddBool(103, dithering); fs_spec.AddBool(104, interlacing); - fs_spec.AddBool(105, m_scaled_dithering); - fs_spec.AddBool(106, m_true_color); + fs_spec.AddBool(105, scaled_dithering); + fs_spec.AddBool(106, true_color); fs_spec.AddBool(107, palette_4_bit); fs_spec.AddBool(108, palette_8_bit); fs_spec.AddBool(109, raw_texture); @@ -2881,10 +2909,11 @@ void GPU_HW_Vulkan::DrawBatchVertices(BatchRenderMode render_mode, uint32_t base // load, Lazy mode either grabs the worker's result or compiles on // the main thread on race, Disabled compiles on every first use. // - // [filter][depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing] + // [filter][true_color][scaled_dithering][depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing] const uint8_t depth_test = m_batch.use_depth_buffer ? static_cast(2) : static_cast(m_batch.check_mask_before_draw); VkPipeline pipeline = - GetBatchPipeline(m_texture_filtering, depth_test, static_cast(render_mode), + GetBatchPipeline(m_texture_filtering, m_true_color, m_scaled_dithering, + depth_test, static_cast(render_mode), static_cast(m_batch.texture_mode), static_cast(m_batch.transparency_mode), m_batch.dithering, m_batch.interlacing); diff --git a/src/core/gpu_hw_vulkan.h b/src/core/gpu_hw_vulkan.h index aec725ec..ccabea59 100644 --- a/src/core/gpu_hw_vulkan.h +++ b/src/core/gpu_hw_vulkan.h @@ -164,16 +164,20 @@ private: // gracefully so DestroyPipelines doesn't trip on them. // The filter parameter selects which sub-cube of m_batch_pipelines / // m_batch_fragment_shaders the lookup and slow-path compile target. - // Main-thread draw callers pass m_texture_filtering (current - // session filter). The background warm-up worker iterates filter - // independently of m_texture_filtering so it can populate sub- - // cubes for filters the user has not yet selected, while the main - // thread continues using the current filter unaffected. Both - // helpers are reentrant under the same (filter, ...) tuple - the + // The true_color / scaled_dithering parameters extend the same + // pattern to those per-session spec consts on m_batch_pipelines + // only - they do not affect SPIR-V blob choice so the FS module + // cache stays a single 5D array shared across (true_color, + // scaled_dithering) combos. Main-thread draw callers pass + // (m_texture_filtering, m_true_color, m_scaled_dithering); the + // background warm-up worker passes the filter it is currently + // warming alongside its captured (m_true_color, m_scaled_dithering) + // snapshot. Both helpers are reentrant under the same tuple - the // slot publish under m_batch_shader_mutex handles the race winner. VkShaderModule GetBatchFragmentShader(GPUTextureFilter filter, uint8_t render_mode, uint8_t texture_mode, bool dithering, bool interlacing); - VkPipeline GetBatchPipeline(GPUTextureFilter filter, uint8_t depth_test, uint8_t render_mode, uint8_t texture_mode, + VkPipeline GetBatchPipeline(GPUTextureFilter filter, bool true_color, bool scaled_dithering, + 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. Mirrors the D3D12 backend's @@ -268,24 +272,46 @@ private: uint32_t m_current_uniform_buffer_offset = 0; VkBufferView m_texture_stream_buffer_view = VK_NULL_HANDLE; - // [filter][depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing] + // [filter][true_color][scaled_dithering][depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing] // - // Filter is the outermost dimension - one sub-cube per - // GPUTextureFilter value (Nearest, Bilinear / BilinearBinAlpha, - // JINC2 / JINC2BinAlpha, xBR / xBRBinAlpha = 7 entries). Filter - // selects which pre-baked batch FS SPIR-V blob backs every PSO in - // its sub-cube; the other 6 dimensions are the structural axes - // shared across all filters. Indexing by current m_texture_filtering - // means filter-only setting changes do NOT need to flush the cache: - // the previous filter's sub-cube remains valid, populated, and - // instantly addressable when the user cycles back to it. - // CompilePipelines on a filter-only change populates the new - // sub-cube (in Enabled mode) or leaves it empty for lazy fault-in - // (in Lazy / Disabled). DestroyPipelines still does a full sweep - // - it is reached only on a non-filter shader-affecting change - // (resolution scale, MSAA, true colour, ...), which flips per- - // session state applied to every sub-cube and therefore invalidates - // all of them at once. + // Three outermost dimensions are the "near-instant toggle" + // settings: each represents a per-session value baked into every + // PSO via spec const (true_color = id 106, scaled_dithering = id + // 105) or structural SPIR-V blob selection (filter). Indexing by + // the current m_texture_filtering / m_true_color / m_scaled_dith- + // ering means toggling any of them in the core options menu does + // NOT flush the cache: the previous sub-cube remains valid, + // populated, and instantly addressable when the user cycles back. + // CompilePipelines on such a toggle populates the new sub-cube + // (Enabled mode synchronous, Lazy via worker, Disabled lazy + // fault-in). DestroyPipelines still does a full sweep - it is + // reached only on a NON-dimensioned shader-affecting change + // (resolution scale, MSAA, per-sample shading, UV limits, chroma + // smoothing, downsample mode, PGXP depth, colour perspective, + // precompile mode), which flips per-session state applied to + // EVERY sub-cube and therefore invalidates all of them at once. + // + // The six inner dimensions are the per-batch / per-draw state + // that GetBatchPipeline indexes today: depth-test mode, render + // mode, texture mode, transparency mode, per-call dithering bit + // (m_batch.dithering, NOT m_scaled_dithering), and the + // interlacing bit. These are passed to GetBatchPipeline as + // explicit arguments from DrawBatchVertices and the precompile + // loop. + // + // The background warm-up worker scopes its walk to the CURRENT + // (m_true_color, m_scaled_dithering) snapshot taken at thread + // entry, varying only filter and the six inner dims. We + // intentionally do NOT walk the off-current spec const sub-cubes + // in the background: a full 7 x 2 x 2 = 28 sub-cube warm-up on + // a cold pipeline cache would take a few minutes of background + // CPU per session, well past "useful" into "annoying". Instead + // the first toggle of true_color or scaled_dithering pays the + // usual sync compile (Enabled) or fault-in stutter (Lazy / + // Disabled) for the new combo's current filter sub-cube; the + // worker then walks the new combo's six other filter sub-cubes + // in the background as before. Cycling back is instant in all + // modes thanks to the dimensioned cache. // // std::atomic rather than a plain VkPipeline so the // draw path can sample a slot without taking any lock. The slow @@ -296,15 +322,7 @@ private: // by the precompile worker or by an earlier main-thread fault-in // - is a single memory_order_acquire load with no kernel calls // and no serialisation against the worker. - // - // Without the lock-free fast path the user-visible experience - // after a texture-filter change was a multi-second hang: the - // precompile worker held m_batch_shader_mutex for the duration of - // each PSO compile (~hundreds of ms on the heavier shaders), so - // every concurrent main-thread DrawBatchVertices stalled behind - // it for at least one full compile, and the runloop never made - // forward progress until the worker finished the entire matrix. - DimensionalArray, 2, 2, 5, 9, 4, 3, 7> m_batch_pipelines{}; + DimensionalArray, 2, 2, 5, 9, 4, 3, 2, 2, 7> m_batch_pipelines{}; // Persistent vertex / fragment shader modules and shadergen for // the lazy and background-thread compile paths. These used to be