diff --git a/src/core/gpu_hw.cpp b/src/core/gpu_hw.cpp index 5c359ec1..3e5696b4 100644 --- a/src/core/gpu_hw.cpp +++ b/src/core/gpu_hw.cpp @@ -128,7 +128,8 @@ bool GPU_HW::DoState(StateWrapper& sw, HostDisplayTexture** host_texture, bool u return true; } -void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed) +void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed, + bool* only_filter_changed) { const uint32_t resolution_scale = CalculateResolutionScale(); const uint32_t multisamples = std::min(m_max_multisamples, g_settings.gpu_multisamples); @@ -139,14 +140,30 @@ 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); - *shaders_changed = + + // 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 = (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_texture_filtering != g_settings.gpu_texture_filter || + m_scaled_dithering != g_settings.gpu_scaled_dithering || 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; m_resolution_scale = resolution_scale; m_multisamples = multisamples; diff --git a/src/core/gpu_hw.h b/src/core/gpu_hw.h index b50a2274..9a7ad917 100644 --- a/src/core/gpu_hw.h +++ b/src/core/gpu_hw.h @@ -180,7 +180,18 @@ protected: static_cast(rgba >> 24) * (1.0f / 255.0f)); } - void UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed); + // 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. + // 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); virtual void UpdateVRAMReadTexture(); virtual void UpdateDepthBufferFromMaskBit() = 0; diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index 9e87fcc5..98b4982c 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -781,8 +781,24 @@ void GPU_HW_Vulkan::UpdateSettings() { GPU_HW::UpdateSettings(); - bool framebuffer_changed, shaders_changed; - UpdateHWSettings(&framebuffer_changed, &shaders_changed); + // Stop the background batch-compile worker BEFORE UpdateHWSettings + // writes m_texture_filtering. With B's filter-dimensioned batch + // cache the worker reads m_texture_filtering implicitly on every + // call to GetBatchPipeline / GetBatchFragmentShader (it determines + // both the slot index AND the SPIR-V blob selection). An + // unsynchronised write here while the worker is still iterating + // could split those two reads across old / new filter values, + // installing a FS module for one filter into a slot indexed by + // another - which then persists indefinitely because the filter- + // only path skips DestroyPipelines. Joining first eliminates the + // race entirely; CompilePipelines below will restart the worker + // for the new filter as appropriate. (StopShaderCompileThread is + // idempotent - the call inside CompilePipelines at line ~1406 + // 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); if (framebuffer_changed) { @@ -800,9 +816,34 @@ void GPU_HW_Vulkan::UpdateSettings() if (shaders_changed) { - // clear it since we draw a loading screen and it's not in the correct state - DestroyPipelines(); - CompilePipelines(); + if (only_filter_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. + // + // Cycling back to a previously-visited filter 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. + DestroyPipelines(); + CompilePipelines(); + } } // this has to be done here, because otherwise we're using destroyed pipelines in the same cmdbuffer @@ -1452,11 +1493,24 @@ bool GPU_HW_Vulkan::CompilePipelines() // PGXP_DEPTH and RESOLUTION_SCALE are spec constants applied at // pipeline-create time inside GetBatchPipeline, not at module-create // time here. + // + // The slot-populated guard makes this loop idempotent. On a filter- + // only setting change UpdateSettings skips DestroyPipelines and + // re-enters CompilePipelines; the existing VS modules are still + // valid (filter is FS-only), so we skip the create. On a fresh + // call (initial init or post-DestroyPipelines) every slot is + // VK_NULL_HANDLE and the bake proceeds normally. const bool msaa = (m_multisamples > 1); const bool per_sample_shading = m_per_sample_shading; const bool noperspective_col = m_disable_color_perspective; for (uint8_t textured = 0; textured < 2; textured++) { + if (m_batch_vertex_shaders[textured].load(std::memory_order_acquire) != VK_NULL_HANDLE) + { + progress.Increment(); + continue; + } + const Vulkan::EmbeddedShaders::EmbeddedShaderBlob& blob = Vulkan::EmbeddedShaders::GetBatchVertexShaderBlob(textured != 0, m_using_uv_limits, msaa, per_sample_shading, noperspective_col); @@ -1827,8 +1881,13 @@ VkShaderModule GPU_HW_Vulkan::GetBatchFragmentShader(uint8_t render_mode, uint8_ // Fast path: lock-free load. Both the precompile worker and the // main thread can read a slot concurrently without contending // with each other or with each other's slow-path compiles. + // Filter is indexed by current m_texture_filtering - other filter + // sub-cubes are populated independently and stay valid across a + // filter-only setting change (UpdateSettings skips + // DestroyPipelines in that case). std::atomic& slot = - m_batch_fragment_shaders[render_mode][lookup_mode][static_cast(dithering)][static_cast(interlacing)]; + m_batch_fragment_shaders[static_cast(m_texture_filtering)][render_mode][lookup_mode] + [static_cast(dithering)][static_cast(interlacing)]; VkShaderModule existing = slot.load(std::memory_order_acquire); if (existing != VK_NULL_HANDLE) return existing; @@ -1923,8 +1982,8 @@ VkPipeline GPU_HW_Vulkan::GetBatchPipeline(uint8_t depth_test, uint8_t render_mo texture_mode; std::atomic& slot = - m_batch_pipelines[depth_test][render_mode][lookup_mode][transparency_mode][static_cast(dithering)] - [static_cast(interlacing)]; + m_batch_pipelines[static_cast(m_texture_filtering)][depth_test][render_mode][lookup_mode] + [transparency_mode][static_cast(dithering)][static_cast(interlacing)]; // Fast path: lock-free atomic load. This is what DrawBatchVertices // hits once a slot is filled (either by the precompile worker or diff --git a/src/core/gpu_hw_vulkan.h b/src/core/gpu_hw_vulkan.h index 9b4500d0..743da706 100644 --- a/src/core/gpu_hw_vulkan.h +++ b/src/core/gpu_hw_vulkan.h @@ -258,28 +258,43 @@ private: uint32_t m_current_uniform_buffer_offset = 0; VkBufferView m_texture_stream_buffer_view = VK_NULL_HANDLE; - // [depth_test][render_mode][texture_mode][transparency_mode][dithering][interlacing] + // [filter][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. // // std::atomic rather than a plain VkPipeline so the // draw path can sample a slot without taking any lock. The slow - // path (slot still null after the atomic load) compiles glslang - // + vkCreateGraphicsPipelines lock-free, then takes - // m_batch_shader_mutex briefly to publish into the slot under a - // double-check. The fast path - which is what DrawBatchVertices - // hits once a slot has been filled either 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. + // path (slot still null after the atomic load) compiles the PSO + // lock-free, then takes m_batch_shader_mutex briefly to publish + // into the slot under a double-check. The fast path - which is + // what DrawBatchVertices hits once a slot has been filled either + // 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 this, 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 glslang -> - // SPIR-V compile (~hundreds of ms on the heavier shaders), so + // 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 2160-entry - // matrix. - DimensionalArray, 2, 2, 5, 9, 4, 3> m_batch_pipelines{}; + // forward progress until the worker finished the entire matrix. + DimensionalArray, 2, 2, 5, 9, 4, 3, 7> m_batch_pipelines{}; // Persistent vertex / fragment shader modules and shadergen for // the lazy and background-thread compile paths. These used to be @@ -314,7 +329,7 @@ private: std::atomic m_shader_compile_thread_quit{false}; DimensionalArray, 2> m_batch_vertex_shaders{}; // [textured] - DimensionalArray, 2, 2, 9, 4> m_batch_fragment_shaders{}; // [render][texture][dither][interlace] + DimensionalArray, 2, 2, 9, 4, 7> m_batch_fragment_shaders{}; // [filter][render][texture][dither][interlace] // Shared vertex shaders used by all non-batch pipelines. Cached // here so the lazy non-batch helpers don't run glslang +