core, common/vulkan: add filter dim to batch pipeline cache (Option B)

Filter swap in the core options menu used to take 5-10 seconds on a
cold pipeline cache and 1-2 seconds even on a warm one, because every
filter change blew away the entire batch pipeline cache and rebuilt
it from scratch for the new filter. The cache (m_batch_pipelines,
m_batch_fragment_shaders) had no filter dimension - the active filter
just selected which pre-baked SPIR-V blob backed every PSO in the
single shared cache, so any change to it invalidated everything.

Add filter as the outermost dimension of both cache arrays:

  m_batch_pipelines : 6D -> 7D
    was [depth_test][render_mode][texture_mode][transparency_mode]
        [dithering][interlacing]
    now [filter][depth_test][render_mode][texture_mode]
        [transparency_mode][dithering][interlacing]

  m_batch_fragment_shaders : 4D -> 5D
    was [render_mode][texture_mode][dithering][interlacing]
    now [filter][render_mode][texture_mode][dithering][interlacing]

GetBatchPipeline / GetBatchFragmentShader prepend the current
m_texture_filtering as the first index. Each filter now has its own
sub-cube of slots; populating one does not touch any other. Filter
swap behaviour:

  - First visit to a filter            : same slow PSO compile as
                                         before (cold pipeline cache),
                                         or a couple of seconds with
                                         the driver cache warm.
  - Cycling back to a previous filter  : instant. The slot is already
                                         filled, GetBatchPipeline
                                         returns it from a single
                                         atomic load, no
                                         vkCreateGraphicsPipelines /
                                         vkCreateShaderModule call.

The skip-destroy decision is made by UpdateHWSettings, which now
emits an additional out-parameter only_filter_changed. The signature
keeps backwards compat for the D3D11 / D3D12 backends - the
parameter defaults to nullptr and they continue passing only the
existing two flags (their batch caches are not filter-dimensioned
yet, so a full DestroyPipelines + CompilePipelines is still
required there). Splitting the comparison:

  filter_diff      = (m_texture_filtering != g_settings.gpu_texture_filter)
  non_filter_diff  = any other shader-affecting setting changed
                     (resolution scale, MSAA, true colour, per-sample
                      shading, scaled dithering, UV limits, chroma
                      smoothing, downsample mode, PGXP depth, color
                      perspective, precompile mode)
  *shaders_changed         = filter_diff || non_filter_diff
  *only_filter_changed     = filter_diff && !non_filter_diff

GPU_HW_Vulkan::UpdateSettings routes accordingly:

  shaders_changed && !only_filter_changed
    -> DestroyPipelines + CompilePipelines (current full-flush
       behaviour, every sub-cube invalidated because per-session
       spec consts / structural SPIR-V choice apply to all of them)
  shaders_changed && only_filter_changed
    -> CompilePipelines only. The previous filter's sub-cube remains
       populated; the new filter's sub-cube is lazy-populated by the
       precompile_sync loop (Enabled mode), filled in by the
       background worker (Lazy), or left empty for draw-time fault-
       in (Disabled).

To make this safe under Lazy mode, CompilePipelines must tolerate
being called on a partially-populated cache. Two changes:

  - The batch VS bake loop at the top of CompilePipelines was
    unconditional create + store; it now load-checks each slot first
    and skips if populated, so re-entry on a filter-only change does
    not leak the existing module. The non-batch GetXxxPipeline
    helpers were already idempotent (each guards on slot !=
    VK_NULL_HANDLE at entry) and need no change.

  - Stop the background compile worker as the very first action in
    GPU_HW_Vulkan::UpdateSettings, BEFORE UpdateHWSettings writes
    m_texture_filtering. Without this, the worker could split its
    two reads of m_texture_filtering (one inside GetBatchPipeline
    for the slot index, one inside GetBatchFragmentShader for both
    the slot index AND the SPIR-V blob selection) across the
    write, installing a FS module compiled for one filter into a
    PSO slot indexed by another. Pre-B this race was benign
    because DestroyPipelines reliably swept up after the worker;
    with B the filter-only path skips DestroyPipelines, so a stale
    cross-wired slot would persist indefinitely.
    StopShaderCompileThread is idempotent so the existing call at
    the top of CompilePipelines is unchanged.

Smoke tests:
  - g++ -fsyntax-only across the touched TUs (clean).
  - Tree-wide grep on m_batch_pipelines / m_batch_fragment_shaders:
    only Vulkan-side hits change shape; D3D12 has its own
    same-named member at a different scope (gpu_hw_d3d12.h) which
    is unrelated and unaffected.

Memory impact:
  - m_batch_pipelines: 3*4*9*5*2*2 = 2160 slots -> 7x = 15120 slots
    (std::atomic<VkPipeline> = 8 bytes typical -> ~120 KiB total)
  - m_batch_fragment_shaders: 4*9*2*2 = 144 slots -> 7x = 1008 slots
    (std::atomic<VkShaderModule> = 8 bytes typical -> ~8 KiB total)
  - VkPipeline / VkShaderModule driver-side memory only grows when
    slots are populated. A typical session visits maybe 10-20% of
    one filter's reachable cells; visiting all 7 filters would
    grow driver-side pipeline storage by ~7x of that fraction. On
    a desktop GPU this is well under 100 MiB worst case.

Test priority:
  - With the shader cache nuked: boot a game in each precompile mode
    (Enabled / Lazy / Disabled). Verify nothing regressed.
  - In Enabled: change filter (xBR -> Nearest -> Bilinear -> xBR).
    First visit to each new filter should be the usual 5-10s
    progress-bar pause on a cold pipeline cache. Cycling back to
    xBR after visiting Nearest and Bilinear should now be INSTANT -
    no progress bar, no perceptible pause, no
    vkCreateGraphicsPipelines calls (the slots are already
    populated). On a warm pipeline cache (second run) the first
    visit drops to ~1-2s, the revisit stays instant.
  - In Lazy: same cycle. First visit pauses briefly for non-batch
    pre-fill then resumes gameplay with background fill in
    progress. Revisits are instant; background fill of the new
    filter's sub-cube continues to populate even while the user
    plays.
  - In Disabled: same cycle. First visit has the usual lazy fault-
    in stutters per (render_mode, texture_mode, ...) combo as the
    game touches new ones. Revisits are instant.
  - Confirm non-filter shader-affecting changes still trigger a
    full flush: change resolution scale, MSAA, true-colour toggle,
    PGXP depth toggle, etc. Should see the full
    DestroyPipelines + CompilePipelines pause regardless of mode.
  - Confirm settings-only changes (audio volume, save state slot,
    etc.) do not flush the cache. (UpdateSettings is gated on
    GPU-relevant settings in host_interface.cpp:285-298, so this
    should be safe by construction.)

Option C (background-precompile the OTHER filter sub-cubes after
Enabled's primary work finishes, so first-visit-to-new-filter is
ALSO instant) is the natural follow-up and now trivially builds on
this patch's filter-dimensioned cache. Will come as a separate
patch after B is validated.
This commit is contained in:
LibretroAdmin 2026-05-18 00:48:43 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 32704a9b78
4 changed files with 131 additions and 29 deletions

View file

@ -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;