core, common/vulkan: surgical downsample mode handling - no VRAM round-trip on Disabled <-> Box

Toggling the 'downsampling' core option from None to Box (or back)
previously triggered the same full UpdateSettings reset path that a
resolution-scale change does: a ReadVRAM -> DestroyFramebuffer ->
CreateFramebuffer -> UpdateVRAM round-trip combined with
DestroyPipelines + CompilePipelines. On a Vulkan backend with the
new dimensioned batch cache this is even more painful than it used
to be, because the full pipeline rebuild also discards every filter
/ true_color / scaled_dithering sub-cube the worker has been
populating and forces the warm-up to start over.

Downsample mode actually affects very little of the backend state:
the batch matrix never references it (no spec const, no structural
SPIR-V choice), and the only framebuffer dimensions that depend on
it are the display texture width and the existence of the optional
downsample weight texture - and even those only differ when Adaptive
is involved on one side of the transition. Disabled <-> Box keeps
the display texture at GPU_MAX_DISPLAY_WIDTH * res_scale and just
needs the downsample_texture / render pass / framebuffer / first-
pass PSO swapped in.

Three changes that work together:

1. UpdateHWSettings:
   - *framebuffer_changed no longer trips on every downsample diff.
     Now only when Adaptive is involved on at least one end of the
     transition (Disabled <-> Adaptive, Box <-> Adaptive).
     Disabled <-> Box leaves the flag false because the main
     framebuffer dimensions are unchanged.
   - Downsample mode dropped from non_dim_diff: it has never
     affected the batch matrix, and surfacing it through
     *shaders_changed forced every backend into DestroyPipelines +
     CompilePipelines when the only thing that needed to change was
     a handful of downsample resources.
   - New optional out-parameter *downsample_changed - simple
     boolean diff between cached m_downsample_mode and the new
     value. Default nullptr so the D3D11 / D3D12 callers compile
     unchanged (they will get the same targeted treatment in a
     follow-up patch once their downsample resource lifecycle is
     factored the same way; for now they continue to do the full
     reset on a downsample toggle, matching the pre-patch
     behaviour).

2. New Vulkan helpers extracted from Create/DestroyFramebuffer and
   DestroyPipelines:
   - CreateDownsampleResources(texture_width, texture_height,
                               texture_format) -> bool
     Builds the downsample textures, render pass(es), framebuffers,
     image views, and descriptor sets for the current
     m_downsample_mode. Called from the end of CreateFramebuffer
     for the full-rebuild path and directly from UpdateSettings for
     the Disabled <-> Box path.
   - DestroyDownsampleResources()
     Drops everything CreateDownsampleResources owns - textures,
     framebuffers, image views, descriptor sets. Called from
     DestroyFramebuffer and from the downsample-only path. Does
     NOT touch pipelines (those have a separate lifecycle gated on
     the render pass).
   - DestroyDownsamplePipelines()
     Drops the 4 downsample VkPipeline handles. Called from
     DestroyPipelines (existing behaviour) and from the downsample-
     only path in UpdateSettings (new behaviour - the PSOs
     reference the OLD render pass and would otherwise leak a
     stale handle).

3. GPU_HW_Vulkan::UpdateSettings:
   - Receives downsample_changed alongside the existing flags.
   - downsample_changed && !framebuffer_changed (the Disabled <->
     Box case): DestroyDownsamplePipelines +
     DestroyDownsampleResources + CreateDownsampleResources. ~1-2
     ms of work plus 1 PSO compile. No VRAM round-trip, no batch
     matrix touch.
   - downsample_changed && framebuffer_changed (Disabled <->
     Adaptive / Box <-> Adaptive): CreateFramebuffer already
     rebuilt the downsample resources via its end-of-function
     CreateDownsampleResources call. We just need to drop the
     stale PSOs.
   - downsample_changed && !shaders_changed: pre-build the new
     downsample PSOs via the existing GetDownsampleXxxPipeline
     helpers in Enabled / Lazy modes (so the next display call
     doesn't lazy-fault); Disabled stays truly lazy by contract.
   - Adaptive's full lifecycle (the side of the transition that
     does need the framebuffer round-trip) is unchanged - the
     display texture width and weight texture creation still
     require it.

Smoke tests:
  - Tree-wide grep confirms every CreateDownsampleResources /
    DestroyDownsampleResources / DestroyDownsamplePipelines call
    site is intentional, and no leftover inline copy of the
    Adaptive / Box construction blocks survives outside the helper.
  - The two GetBatchPipeline helper paths and the precompile worker
    do not touch any downsample state, so this patch is orthogonal
    to the dim-cache work from the preceding commits.

Test priority:
  - In Vulkan, with shader cache nuked, in Enabled mode: toggle
    'Downsampling' from None to 'Box - downsample 3D / smooth all'.
    Expected: no progress-bar pause, the new Box-mode output
    appears within ~one frame. Toggle back to None: same instant
    behaviour. Cycle several times. Previously each toggle paid
    5-10 seconds of pipeline rebuild plus VRAM round-trip.
  - None <-> Adaptive and Box <-> Adaptive: framebuffer rebuild is
    still required (display texture width changes), but the pause
    should now be limited to the framebuffer work (~tens of ms);
    the batch matrix is preserved across the toggle.
  - Adaptive output should still look correct after each toggle
    (the helper extraction preserves the original code byte-for-
    byte, just moved into CreateDownsampleResources).
  - With downsample at Box, toggle resolution scale or MSAA -
    these trigger the full framebuffer + shaders rebuild as
    before (downsample mode unchanged, downsample_changed=false),
    no behavioural change.
  - With downsample at Adaptive, toggle texture filter -
    only_dim_changed=true path runs, batch matrix sub-cube
    preserves other filter values, downsample resources
    untouched. Cycle back to original filter is instant.

The same factoring will be done for D3D12 and D3D11 in follow-up
patches; their downsample resource lifecycle is structurally
identical, just with ID3D12PipelineState / ID3D11PixelShader
instead of VkPipeline. For now those backends ignore the new
downsample_changed flag (default nullptr) and continue with the
full DestroyPipelines + CompilePipelines reset on a downsample
toggle - same as before this patch.
This commit is contained in:
LibretroAdmin 2026-05-18 02:02:24 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit c369aa5d8c
4 changed files with 169 additions and 33 deletions

View file

@ -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_dim_changed)
bool* only_dim_changed, bool* downsample_changed)
{
const uint32_t resolution_scale = CalculateResolutionScale();
const uint32_t multisamples = std::min(m_max_multisamples, g_settings.gpu_multisamples);
@ -139,7 +139,18 @@ void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed,
const bool disable_color_perspective = m_supports_disable_color_perspective && ShouldDisableColorPerspective();
*framebuffer_changed =
(m_resolution_scale != resolution_scale || m_multisamples != multisamples || m_downsample_mode != downsample_mode);
(m_resolution_scale != resolution_scale || m_multisamples != multisamples ||
// Downsample mode only changes framebuffer dimensions when Adaptive is
// involved (Adaptive uses VRAM_WIDTH * res_scale for the display
// texture and creates an additional weight texture; Disabled and Box
// both use GPU_MAX_DISPLAY_WIDTH * res_scale and only differ in
// whether the downsample texture / framebuffer chain exists at all).
// The backend's UpdateSettings handler picks up the Disabled <-> Box
// and other non-Adaptive downsample transitions via the dedicated
// downsample_changed out-parameter below, which does NOT trigger the
// full ReadVRAM -> CreateFramebuffer -> UpdateVRAM round-trip.
(m_downsample_mode != downsample_mode &&
(m_downsample_mode == GPUDownsampleMode::Adaptive || downsample_mode == GPUDownsampleMode::Adaptive)));
// Split the shader-affecting setting comparison into "cache-
// dimensioned settings changed" and "non-dimensioned settings
@ -151,10 +162,16 @@ void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed,
// 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.
// smoothing, 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.
//
// Downsample mode is intentionally NOT in either diff list - it
// does not affect the batch matrix at all (no spec const, no
// structural SPIR-V choice). Downsample mode transitions are
// surfaced separately via downsample_changed and serviced by the
// backend without touching the batch pipeline cache.
//
// GPUTextureFilter, m_true_color and m_scaled_dithering are the
// three settings the Vulkan backend currently dimensions over.
@ -168,12 +185,14 @@ void GPU_HW::UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed,
(m_resolution_scale != resolution_scale || 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_downsample_mode != downsample_mode || m_pgxp_depth_buffer != g_settings.UsingPGXPDepthBuffer() ||
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 = dim_diff || non_dim_diff;
if (only_dim_changed)
*only_dim_changed = dim_diff && !non_dim_diff;
if (downsample_changed)
*downsample_changed = (m_downsample_mode != downsample_mode);
m_resolution_scale = resolution_scale;
m_multisamples = multisamples;

View file

@ -190,8 +190,22 @@ protected:
// Backends without the dimension (D3D11 / D3D12 today) can ignore
// this and continue doing a full destroy + recompile on any
// shaders_changed event.
//
// The optional out-parameter downsample_changed is set to true iff
// the downsample mode setting has changed from the cached session
// value. Downsample mode does NOT participate in either
// *framebuffer_changed (unless Adaptive is involved on either
// side of the transition, since Adaptive uses a different
// display-texture width and an additional weight texture) or
// *shaders_changed (downsample mode never affects batch
// pipelines / batch fragment shaders). Backends opt into a
// targeted downsample-only rebuild path by reading this flag;
// Disabled <-> Box transitions cost a couple of small VkPipeline
// / texture / framebuffer creates rather than a full VRAM
// round-trip.
void UpdateHWSettings(bool* framebuffer_changed, bool* shaders_changed,
bool* only_dim_changed = nullptr);
bool* only_dim_changed = nullptr,
bool* downsample_changed = nullptr);
virtual void UpdateVRAMReadTexture();
virtual void UpdateDepthBufferFromMaskBit() = 0;

View file

@ -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_dim_changed;
UpdateHWSettings(&framebuffer_changed, &shaders_changed, &only_dim_changed);
bool framebuffer_changed, shaders_changed, only_dim_changed, downsample_changed;
UpdateHWSettings(&framebuffer_changed, &shaders_changed, &only_dim_changed, &downsample_changed);
if (framebuffer_changed)
{
@ -814,6 +814,53 @@ void GPU_HW_Vulkan::UpdateSettings()
if (framebuffer_changed)
CreateFramebuffer();
// Downsample-only resource rebuild. Triggered when downsample mode
// changed but framebuffer_changed did NOT (i.e. Disabled <-> Box,
// neither end Adaptive). Without this branch the only way the
// backend could service a downsample toggle was via the full
// ReadVRAM -> CreateFramebuffer -> UpdateVRAM round-trip - which
// produced a multi-second freeze on every toggle even though the
// batch matrix and main VRAM textures don't depend on downsample
// mode at all. The work here is bounded by the downsample
// resource and PSO count: 1 texture + 1 framebuffer + 1 PSO for
// Box; ~4-12 textures/views + framebuffers + 4 PSOs for
// Adaptive (which would only land here when transitioning from
// Box - the Disabled <-> Adaptive and Box <-> Adaptive cases go
// through framebuffer_changed above).
//
// Order matters relative to shaders_changed below. If
// shaders_changed is also true and a full DestroyPipelines /
// CompilePipelines pair runs, that pair would (a) destroy the
// downsample PSOs anyway and (b) pre-fill them again via
// CompilePipelines' Lazy/Enabled non-batch pre-fill block - using
// the resources we recreate here. Doing the resource swap first
// means the PSO pre-fill picks up the new render pass.
if (downsample_changed && !framebuffer_changed)
{
DestroyDownsamplePipelines();
DestroyDownsampleResources();
if (m_downsample_mode != GPUDownsampleMode::Disabled)
{
const uint32_t texture_width = VRAM_WIDTH * m_resolution_scale;
const uint32_t texture_height = VRAM_HEIGHT * m_resolution_scale;
const VkFormat texture_format = VK_FORMAT_R8G8B8A8_UNORM;
CreateDownsampleResources(texture_width, texture_height, texture_format);
}
}
else if (downsample_changed)
{
// framebuffer_changed = true ALSO implies downsample_changed for
// Disabled <-> Adaptive / Box <-> Adaptive. CreateFramebuffer
// above already rebuilt the downsample resources for the new
// mode (via DestroyFramebuffer + the CreateDownsampleResources
// call at the bottom of CreateFramebuffer), so all that remains
// is to drop the stale downsample PSOs - they referenced the
// OLD render pass and would otherwise be left referencing
// destroyed Vulkan handles. The shaders_changed path or the
// post-shaders rebuild step below pre-fills the new PSOs.
DestroyDownsamplePipelines();
}
if (shaders_changed)
{
if (only_dim_changed)
@ -839,14 +886,36 @@ void GPU_HW_Vulkan::UpdateSettings()
{
// 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.
// chroma smoothing, 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();
}
}
else if (downsample_changed)
{
// No shader change. Pre-build new downsample PSOs in Enabled /
// Lazy modes so the next display call doesn't pay the lazy-
// fault stutter. Disabled stays truly lazy by contract.
const GPUShaderPrecompileMode precompile_mode = g_settings.gpu_shader_precompile_mode;
if (precompile_mode == GPUShaderPrecompileMode::Enabled ||
precompile_mode == GPUShaderPrecompileMode::Lazy)
{
if (m_downsample_mode == GPUDownsampleMode::Adaptive)
{
(void)GetDownsampleFirstPassPipeline();
(void)GetDownsampleMidPassPipeline();
(void)GetDownsampleBlurPassPipeline();
(void)GetDownsampleCompositePassPipeline();
}
else if (m_downsample_mode == GPUDownsampleMode::Box)
{
(void)GetDownsampleFirstPassPipeline();
}
}
}
// this has to be done here, because otherwise we're using destroyed pipelines in the same cmdbuffer
if (framebuffer_changed)
@ -1230,6 +1299,23 @@ bool GPU_HW_Vulkan::CreateFramebuffer()
m_point_sampler, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
dsubuilder.Update(g_vulkan_context->GetDevice());
if (m_downsample_mode != GPUDownsampleMode::Disabled)
{
if (!CreateDownsampleResources(texture_width, texture_height, texture_format))
return false;
}
ClearDisplay();
SetFullVRAMDirtyRectangle();
return true;
}
bool GPU_HW_Vulkan::CreateDownsampleResources(uint32_t texture_width, uint32_t texture_height, VkFormat texture_format)
{
VkCommandBuffer cmdbuf = g_vulkan_context->GetCurrentCommandBuffer();
Vulkan::DescriptorSetUpdateBuilder dsubuilder;
Vulkan::FramebufferBuilder fbb;
if (m_downsample_mode == GPUDownsampleMode::Adaptive)
{
const uint32_t levels = GetAdaptiveDownsamplingMipLevels();
@ -1327,12 +1413,32 @@ bool GPU_HW_Vulkan::CreateFramebuffer()
if (m_downsample_mip_views[0].framebuffer == VK_NULL_HANDLE)
return false;
}
ClearDisplay();
SetFullVRAMDirtyRectangle();
return true;
}
void GPU_HW_Vulkan::DestroyDownsampleResources()
{
Vulkan::Util::SafeFreeGlobalDescriptorSet(m_downsample_composite_descriptor_set);
for (SmoothMipView& mv : m_downsample_mip_views)
{
Vulkan::Util::SafeFreeGlobalDescriptorSet(mv.descriptor_set);
Vulkan::Util::SafeDestroyImageView(mv.image_view);
Vulkan::Util::SafeDestroyFramebuffer(mv.framebuffer);
}
m_downsample_mip_views.clear();
m_downsample_texture.Destroy(false);
Vulkan::Util::SafeDestroyFramebuffer(m_downsample_weight_framebuffer);
m_downsample_weight_texture.Destroy(false);
}
void GPU_HW_Vulkan::DestroyDownsamplePipelines()
{
Vulkan::Util::SafeDestroyPipeline(m_downsample_first_pass_pipeline);
Vulkan::Util::SafeDestroyPipeline(m_downsample_mid_pass_pipeline);
Vulkan::Util::SafeDestroyPipeline(m_downsample_blur_pass_pipeline);
Vulkan::Util::SafeDestroyPipeline(m_downsample_composite_pass_pipeline);
}
void GPU_HW_Vulkan::ClearFramebuffer()
{
VkCommandBuffer cmdbuf = g_vulkan_context->GetCurrentCommandBuffer();
@ -1357,18 +1463,7 @@ void GPU_HW_Vulkan::ClearFramebuffer()
void GPU_HW_Vulkan::DestroyFramebuffer()
{
Vulkan::Util::SafeFreeGlobalDescriptorSet(m_downsample_composite_descriptor_set);
for (SmoothMipView& mv : m_downsample_mip_views)
{
Vulkan::Util::SafeFreeGlobalDescriptorSet(mv.descriptor_set);
Vulkan::Util::SafeDestroyImageView(mv.image_view);
Vulkan::Util::SafeDestroyFramebuffer(mv.framebuffer);
}
m_downsample_mip_views.clear();
m_downsample_texture.Destroy(false);
Vulkan::Util::SafeDestroyFramebuffer(m_downsample_weight_framebuffer);
m_downsample_weight_texture.Destroy(false);
DestroyDownsampleResources();
Vulkan::Util::SafeFreeGlobalDescriptorSet(m_batch_descriptor_set);
Vulkan::Util::SafeFreeGlobalDescriptorSet(m_vram_copy_descriptor_set);
@ -2873,10 +2968,7 @@ void GPU_HW_Vulkan::DestroyPipelines()
Vulkan::Util::SafeDestroyPipeline(m_vram_readback_pipeline);
Vulkan::Util::SafeDestroyPipeline(m_vram_update_depth_pipeline);
Vulkan::Util::SafeDestroyPipeline(m_downsample_first_pass_pipeline);
Vulkan::Util::SafeDestroyPipeline(m_downsample_mid_pass_pipeline);
Vulkan::Util::SafeDestroyPipeline(m_downsample_blur_pass_pipeline);
Vulkan::Util::SafeDestroyPipeline(m_downsample_composite_pass_pipeline);
DestroyDownsamplePipelines();
// Cached shared vertex shaders for the non-batch helpers. Reset
// here so the next CompilePipelines pass picks up any shadergen

View file

@ -139,6 +139,17 @@ private:
void ClearFramebuffer();
void DestroyFramebuffer();
// Downsample-only resource lifecycle. Extracted from
// Create/DestroyFramebuffer so a downsample-mode toggle can be
// serviced WITHOUT the full framebuffer round-trip (ReadVRAM ->
// recreate-everything -> UpdateVRAM). Used directly by
// UpdateSettings when downsample_changed is the only thing that
// differs, and called from Create/DestroyFramebuffer for the
// full-rebuild path.
bool CreateDownsampleResources(uint32_t texture_width, uint32_t texture_height, VkFormat texture_format);
void DestroyDownsampleResources();
void DestroyDownsamplePipelines();
bool CreateVertexBuffer();
bool CreateUniformBuffer();
bool CreateTextureBuffer();