From c7de484a7278659f3ae1927e163c453ab6110f71 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Fri, 13 Nov 2026 00:44:01 -0500 Subject: [PATCH] Free what DLSS and FSR hold some presents after the upscaler is switched off, and the HUD-less picture once frame generation is off. Switched off in the menu or failed, the upscaler kept everything it had made for the process: per scene size its output, motion vector, mask and sharpening images and a DLSS feature (81 MiB of images at 2560x1440 with DLAA, 182 MiB at 2160 lines, and about 0.5 GB per DLSS feature), the reactive mask's snapshots, the HUD-less pair and the FSR 3.1 and FSR 4 upscale contexts. Eight presents after it goes off they are freed on the replay thread, which makes every evaluation, after a wait for the device to be idle, as the code already does for a size it replaces. Switched on again, it makes them anew with a new history, as any switch on does. The HUD-less picture was made and copied every frame, and kept, after frame generation went off, since the flag that asks for it was never cleared on Windows: the frame generator's release clears it now, and the pair goes once no frame generation swapchain is up. Co-Authored-By: Claude Opus 5.5 --- runtime/vk_dlaa.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ runtime/vk_ffx.cpp | 14 ++++++++++++ runtime/vk_ffx.h | 4 ++++ runtime/vk_ffx4.cpp | 7 ++++++ runtime/vk_fg.cpp | 3 +++ 5 files changed, 82 insertions(+) diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index c8ba78f1..5b87907f 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -31,6 +31,7 @@ #include "vk_dlaa.h" #include "vk_exec.h" #include "vk_ffx.h" +#include "vk_fg.h" #include "vk_rec.h" #include "dlaa_mv_spv.h" #include "dlaa_sharpen_spv.h" @@ -2523,8 +2524,61 @@ void vk_dlaa_upscaler(VkExec *ex, VkImage out, VkFormat fmt, uint32_t w, uint32_ evaluate(ex, &dst); } +// ---- what the upscaler holds after it is switched off +// Switched off in the menu, or failed (dl_fail), the upscaler kept everything it had made for the process: per scene +// size its output, motion vector, mask and sharpening images and a DLSS feature (at 2560x1440 with DLAA 81 MiB of +// images, at 2160 lines 182 MiB, and a DLSS feature is about 0.5 GB), the snapshots of the reactive mask, the HUD-less +// pair for frame generation (28 MiB, 63 MiB at 2160 lines) and the FSR 3.1 and FSR 4 contexts. RETIRE_PRESENTS presents +// after the upscaler goes off they are freed, here on the replay thread, which makes every evaluation and so cannot be +// inside one, after a wait for the device to be idle, as free_size does for a size it replaces: an evaluation may +// still run on the GPU, and the present thread only learns of the switch at its next frame. The HUD-less pair goes the +// same way while the upscaler runs and frame generation no longer asks for it (vk_dlaa_hudless_wanted, which the frame +// generator's release clears), once no frame generation swapchain is up that could still read it. Switched on again, +// the upscaler makes what it needs as at its first frame, with a new history, as any switch on does. +#define RETIRE_PRESENTS 8 +static uint64_t retire_off_at, retire_hl_at; // replay: D.presents when the upscaler, or the HUD-less picture, went unused +static uint64_t retired_n; // replay: times the upscaler's memory was given back + +static int up_holds(void) { + for (int i = 0; i < DLSS_SIZES; i++) + if (D.sizes[i].out || D.sizes[i].feat) return 1; + for (int i = 0; i < SNAPS; i++) + if (R.s[i].before) return 1; + return D.hl != VK_NULL_HANDLE || vk_ffx_holds(); +} + +static void retire_idle(void) { + uint64_t now = __atomic_load_n(&D.presents, __ATOMIC_RELAXED); + int off = !dl_active(), hl_unused = D.hl && !vk_dlaa_hudless_wanted && !vk_fg_active(); + if (!off) retire_off_at = 0; + if (!hl_unused) retire_hl_at = 0; + if (off && !retire_off_at) retire_off_at = now ? now : 1; + if (hl_unused && !retire_hl_at) retire_hl_at = now ? now : 1; + int all = off && now >= retire_off_at + RETIRE_PRESENTS && up_holds(); + int hl = hl_unused && now >= retire_hl_at + RETIRE_PRESENTS; + if (!all && !hl) return; + vk_queue_lock(), vkDeviceWaitIdle(vk_ctx()->device), vk_queue_unlock(); + if (hl || !vk_fg_active()) hl_destroy(), D.hl_fresh = 0; + if (all) { + int64_t was = vk_dlaa_vram(); + for (int i = 0; i < DLSS_SIZES; i++) destroy_size(&D.sizes[i]); + for (int i = 0; i < SNAPS; i++) destroy_snap(&R.s[i]); + vk_ffx_release_idle(); + D.last = NULL, D.host = NULL; + retired_n++; + rt_log("dlaa: the upscaler is off (%s) since %d presents: its images, DLSS features and FSR contexts are freed " + "(%llu time(s); DLSS reported %.0f MiB of its own); made again when it is switched on", + D.failed ? "it failed" : "the setting", RETIRE_PRESENTS, (unsigned long long)retired_n, + was > 0 ? was / 1048576.0 : 0.0); + } else { + rt_log("dlaa: frame generation no longer asks for the HUD-less picture: its images are freed"); + } + retire_off_at = retire_hl_at = 0; +} + void vk_dlaa_pass(VkExec *ex) { const VkExecTarget *c = &ex->color[0], *z = &ex->depth; + retire_idle(); if (!dl_active()) return; if (D.done) { // diagnostics: another scene drawn into a light target in the same present interval if (ex->pass_ncolor == 1 && c->image && scene_color(c->format) && z->image && z->format == VK_FORMAT_D32_SFLOAT && diff --git a/runtime/vk_ffx.cpp b/runtime/vk_ffx.cpp index 65a09f5d..b9d3ffb7 100644 --- a/runtime/vk_ffx.cpp +++ b/runtime/vk_ffx.cpp @@ -431,6 +431,20 @@ extern "C" ffxReturnCode_t vk_ffx_dispatch(ffxContext *ctx, const ffxDispatchDes return A.ok ? A.dispatch(ctx, desc) : FFX_API_RETURN_ERROR; } +// The upscaler is off (vk_dlaa.c retire_idle; the device is idle): FSR 3.1's and FSR 4's upscale contexts go, made again +// at the next upscale. Frame generation's contexts are not these. +extern "C" int vk_ffx_holds(void) { + for (Up &u : U) + if (u.ctx) return 1; + return vk_ffx4_holds(); +} +extern "C" void vk_ffx_release_idle(void) { + vk_ffx4_release(); + if (!A.ok) return; + for (Up &u : U) + if (u.ctx) destroy_up(&u, "the upscaler is off"); +} + extern "C" void vk_ffx_release(void) { vk_ffx4_release(); if (!A.ok) return; diff --git a/runtime/vk_ffx.h b/runtime/vk_ffx.h index 79419a62..76434188 100644 --- a/runtime/vk_ffx.h +++ b/runtime/vk_ffx.h @@ -30,6 +30,9 @@ int vk_ffx_framegen_available(void); int vk_ffx_upscale(VkCommandBuffer cb, const VkFfxImage *color, const VkFfxImage *depth, const VkFfxImage *mv, const VkFfxImage *out, float jx, float jy, int reset, float dt_ms, const VkFfxCamera *cam); void vk_ffx_release(void); // before the device goes away +// The upscale contexts of FSR 3.1 and FSR 4 (vk_dlaa.c retire_idle, the device idle): any held, and give them back. +int vk_ffx_holds(void); +void vk_ffx_release_idle(void); // FSR 3.1's contexts that no upscale used in the last `recent` calls, destroyed with the device idle (the caller waited // for it): room for an FSR 4 context whose creation failed. Their count. int vk_ffx_trim_idle(uint64_t recent); @@ -47,6 +50,7 @@ int vk_ffx4_upscale(VkCommandBuffer cb, const VkFfxImage *color, const VkFfxImag const VkFfxImage *out, float jx, float jy, int reset, float dt_ms, const VkFfxCamera *cam); // as vk_ffx_upscale void vk_ffx4_release(void); +int vk_ffx4_holds(void); // an FSR 4 upscale context is made int vk_ffx4_failed(void); // it failed three times in a row: FSR 3.1 serves until vk_ffx4_retry void vk_ffx4_retry(void); // the menu chose FSR 4 again: try it anew after such failures void vk_ffx4_trim(uint64_t now_ns); // at each upscale: FSR 4's contexts unused for a while go diff --git a/runtime/vk_ffx4.cpp b/runtime/vk_ffx4.cpp index 0ece6e55..efdc3a6d 100644 --- a/runtime/vk_ffx4.cpp +++ b/runtime/vk_ffx4.cpp @@ -28,6 +28,7 @@ extern "C" int vk_ffx4_upscale(VkCommandBuffer, const VkFfxImage *, const VkFfxI return 0; } extern "C" void vk_ffx4_release(void) {} +extern "C" int vk_ffx4_holds(void) { return 0; } extern "C" int vk_ffx4_failed(void) { return 0; } extern "C" void vk_ffx4_retry(void) {} extern "C" void vk_ffx4_trim(uint64_t) {} @@ -434,6 +435,12 @@ extern "C" ffxReturnCode_t vk_ffx4_api_dispatch(ffxContext *ctx, const ffxDispat return A.dispatch ? A.dispatch(ctx, desc) : FFX_API_RETURN_ERROR; } +extern "C" int vk_ffx4_holds(void) { + for (Up &u : U) + if (u.ctx) return 1; + return 0; +} + extern "C" void vk_ffx4_release(void) { if (!A.destroy) return; for (Up &u : U) diff --git a/runtime/vk_fg.cpp b/runtime/vk_fg.cpp index d0a57455..407acbb0 100644 --- a/runtime/vk_fg.cpp +++ b/runtime/vk_fg.cpp @@ -342,6 +342,9 @@ extern "C" void vk_fg_frame(VkCommandBuffer cb, VkSwapchainKHR sc, VkFormat fmt, } extern "C" void vk_fg_release(void) { + // the HUD-less picture is made for frame generation alone: a frame generator that runs again asks for it again + // (vk_fg_frame); left set, it was made and copied every frame after frame generation went off, and kept + vk_dlaa_hudless_wanted = 0; vk_fg4_release(); if (F.fg) vk_ffx_destroy(&F.fg), F.fg = nullptr; if (F.sc) vk_ffx_destroy(&F.sc), F.sc = nullptr;