From d77572c69ccd63dd8d235c921b00f4fb3802eb76 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Thu, 12 Nov 2026 23:17:23 -0500 Subject: [PATCH] Free what the upscalers and SMAA hold once they are switched off. DLSS's sizes (their images and features), the HUD-less picture for frame generation, FSR 1's two window-size images and SMAA's three work images stayed for the rest of the run after their setting was turned off. They now go after ten seconds without use (SMAA's after a minute, since its passes only run where the game's own SMAA does): the DLSS sizes on the replaying thread with no wait for the GPU, FSR's once every present slot that read them has been waited for, SMAA's through the deferred destruction. Switching a feature on again makes them again, as its first frame in a run does. At 5120x2142 that is about 0.9 GiB with DLSS off, 168 MiB of FSR and 109 MiB of SMAA. Co-Authored-By: Claude Opus 5.5 --- nx2navi.ini.example | 4 +++- runtime/vk_aa.c | 20 ++++++++++++++++++++ runtime/vk_dlaa.c | 31 +++++++++++++++++++++++++++++++ runtime/vk_fsr.c | 24 ++++++++++++++++++++++-- 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 71ac3ded..65da0cea 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -605,7 +605,9 @@ # characters, a little more flicker). Any other model by its letter, a to m: on slower RTX cards the older model e is # cheaper. Changes while the game runs take effect at once (DLSS starts a new history). # dlss_preset = default -# DLSS keeps one network per picture size (gameplay, the pause screen). 1: one unused for 3,600 frames is freed. +# DLSS keeps one network per picture size (gameplay, the pause screen). 1: one unused for 3,600 frames is freed, and +# ten seconds after the upscaler or frame generation is switched off in the menu, the networks and pictures they held +# are freed too (switched on again later, they are made again, which the first frame waits for). # 0: none is ever freed (about 0.5 GB more video memory after the first pause). 2: as 1, and the networks that stay # are not made again when another is made (shows striped pause screens; for tests). # dlss_idle_free = 1 diff --git a/runtime/vk_aa.c b/runtime/vk_aa.c index 1798f013..84b43b49 100644 --- a/runtime/vk_aa.c +++ b/runtime/vk_aa.c @@ -75,10 +75,13 @@ static struct { int none_told; } A = {.frame_read = ~0ull, .composed = 1}; +static void work_idle(uint64_t frame); + int vk_aa_mode(void) { uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); if (f == A.frame_read) return A.mode; A.frame_read = f; + work_idle(f); const char *v = cfg_value("NX2NAVI_AA"); int want = VK_AA_GAME; for (int i = 1; i < 4; i++) @@ -134,8 +137,24 @@ static struct { Img area, search; Img luma, edges, weights; // at the scene's size (w, h) uint32_t w, h; + uint64_t smaa_frame; // the last frame that recorded SMAA's passes } P; +static void img_free(Img *m); + +// SMAA's work images stayed for the rest of the run once aa left smaa: three pictures of the scene's size, 109 MiB at +// 5120x2142. They go WORK_IDLE presents after the last frame that used them, through the deferred destruction (no +// submission still names them then); smaa again makes them as a change of the scene's size does (work_images), in the +// command buffer of its first frame. A minute, not less: the passes run only where the game runs its own SMAA, so a +// long pause or loading screen with smaa set also counts as unused, and the images are made again after it. +#define WORK_IDLE 3600 +static void work_idle(uint64_t frame) { + if (!P.luma.img || frame - P.smaa_frame < WORK_IDLE) return; + rt_log("aa: SMAA's work images unused for %d frames: freed (%ux%u)", WORK_IDLE, P.w, P.h); + img_free(&P.luma), img_free(&P.edges), img_free(&P.weights); + P.w = P.h = 0; +} + static int pipes_init(void) { if (P.tried) return P.ok; P.tried = 1; @@ -319,6 +338,7 @@ int vk_aa_record(VkCommandBuffer cb, int mode, VkImageView src, VkImageView dst, pass(cb, P_POST, v, s, &pc, sizeof pc, w, h); return 1; } + P.smaa_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); const float metrics[4] = {1.0f / (float)w, 1.0f / (float)h, (float)w, (float)h}; const VkSampler lin[4] = {P.linear, P.linear, P.linear, P.linear}; pass(cb, P_LUMA, (const VkImageView[]){src, P.luma.view}, lin, metrics, sizeof metrics, w, h); diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index c8ba78f1..814323c9 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -2695,6 +2695,35 @@ int vk_dlaa_resized_take(void) { return 1; } +// What DLSS holds while nothing uses it (NX2NAVI_DLSS_IDLE_FREE 1 or 2, the default 1). With the upscaler switched off +// in the menu, the sizes kept their images and features for the rest of the run, since only an evaluation frees a size +// (stale_sizes): at a 5120x2142 output that is the output, sharpening, mask, class and distance images, about 0.4 GiB, +// and about 0.5 GiB per feature. With frame generation off and DLSS on, the HUD-less picture (hudless) stayed the same +// way. After IDLE_FREE_PRESENTS presents without their use they go here, on the replaying thread that makes every +// other NGX call, without waiting for the GPU: the last evaluation that used them is that many presents back, and the +// CPU is never more than a few ahead. A feature made later is made with none alive, the case release_kept leaves +// clean. What it costs: switching DLSS on again more than IDLE_FREE_PRESENTS presents after switching it off makes +// the size's images and feature again, which its first evaluation waits for, as at the first frame DLSS ran in a run. +#define IDLE_FREE_PRESENTS 600 +static uint64_t off_at; // D.presents when DLSS went off +static void idle_release(void) { + if (!idle_free()) return; + if (D.hl && D.presents - D.hl_at >= IDLE_FREE_PRESENTS && !D.hl_fresh) { + rt_log("dlaa: the HUD-less picture for frame generation went unused for %d presents: freed", IDLE_FREE_PRESENTS); + hl_destroy(); + } + if (D.on || D.presents - off_at < IDLE_FREE_PRESENTS) return; + int n = 0; + for (int i = 0; i < DLSS_SIZES; i++) + if (D.sizes[i].out || D.sizes[i].feat) { + rt_log("dlaa: the DLSS size %ux%u -> %ux%u went unused for %d presents with the upscaler off: freed", + D.sizes[i].w, D.sizes[i].h, D.sizes[i].ow, D.sizes[i].oh, IDLE_FREE_PRESENTS); + destroy_size(&D.sizes[i]), n++; + } + if (n) // frame generation's motion vectors were a size's (evaluate); none is handed out this late (fg_current) + D.last = NULL, D.host = NULL, D.fg_fresh = 0, D.fg_mv_view = VK_NULL_HANDLE, D.fg_mv = (VkFfxImage){0}; +} + void vk_dlaa_frame(void) { static unsigned cfg_seen; static int cfg_known; @@ -2714,6 +2743,7 @@ void vk_dlaa_frame(void) { if (on != D.on) { rt_log("dlaa: %s", on ? "on" : "off"); D.on = on; + off_at = D.presents; // either way the next DLAA frame starts a new history from an unjittered frame D.reset = 1, D.cam_prev_ok = 0, D.jit_ok = D.jit_used = D.armed = D.done = D.gbuf = D.ended = D.up = 0; D.hold = 0; @@ -2721,6 +2751,7 @@ void vk_dlaa_frame(void) { memset(&F, 0, sizeof F); return; } + idle_release(); if (!dl_active()) return; if (D.share_off) D.share_off--; if (!D.done && D.armed) D.share_off = 120; // vk_dlaa_pass: the output pass without the mode's share for a while diff --git a/runtime/vk_fsr.c b/runtime/vk_fsr.c index 59491881..73fcd3e3 100644 --- a/runtime/vk_fsr.c +++ b/runtime/vk_fsr.c @@ -39,8 +39,26 @@ static struct { uint32_t w, h; // their size VkImageView src_view[SLOTS]; // the source view each slot's last frame read int logged; + uint32_t used_slots, since; // slots whose frames used the images / that recorded without them since the last use + uint32_t idle; // presents recorded without them since their last use } F; +// The two images while the upscaler is not fsr (or the picture needs no scaling): they stayed for the rest of the run, +// two RGBA16F pictures of the window's size, 168 MiB at 5120x2142. They go once IDLE_FREE presents recorded without +// them and every slot that ever used them has recorded since: the caller waited for that slot's fence before it +// recorded (see the top of the file), so no submission still reads them. Taking fsr up again makes them as a window +// size change does (ensure_images: a wait for the idle device and two images). +#define IDLE_FREE 600 +static void free_images(VkCtx *c); +static void idle_note(unsigned slot) { + if (!F.img[0]) return; + F.since |= 1u << (slot % SLOTS); + if (++F.idle < IDLE_FREE || (F.since & F.used_slots) != F.used_slots) return; + rt_log("vk: FSR's images unused for %u presents: freed (%ux%u)", F.idle, F.w, F.h); + free_images(vk_ctx()); + F.used_slots = F.since = F.idle = 0; +} + static VkPipeline make_pipeline(VkCtx *c, const uint32_t *code, size_t size) { VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = size, .pCode = code}; VkShaderModule m = VK_NULL_HANDLE; @@ -202,17 +220,19 @@ int vk_fsr_record(VkCommandBuffer cb, unsigned slot, VkImage src, VkFormat fmt, VkImage dst, const int out[4]) { int cw = crop[2], ch = crop[3]; uint32_t dw = (uint32_t)out[2], dh = (uint32_t)out[3]; - if (strcmp(cfg_value("NX2NAVI_UPSCALER"), "fsr") || !fmt || (dw == (uint32_t)cw && dh == (uint32_t)ch)) return 0; + if (strcmp(cfg_value("NX2NAVI_UPSCALER"), "fsr") || !fmt || (dw == (uint32_t)cw && dh == (uint32_t)ch)) + return idle_note(slot), 0; int sharp = cfg_int("NX2NAVI_SHARPNESS"); if (sharp < 0) sharp = 0; if (sharp > 100) sharp = 100; // EASU only enlarges: a picture smaller than the crop in either direction takes the shrink path, which without // sharpening is the plain blit of the caller int shrink = dw < (uint32_t)cw || dh < (uint32_t)ch; - if (shrink && !sharp) return 0; + if (shrink && !sharp) return idle_note(slot), 0; VkCtx *c = vk_ctx(); if (!F.tried) init(c); if (!F.ok || !ensure_images(c, dw, dh)) return 0; + F.used_slots |= 1u << (slot % SLOTS), F.since = 0, F.idle = 0; if (shrink) return shrink_sharpen(cb, src, crop, dst, out, sharp); slot %= SLOTS; if (F.src_view[slot]) vkDestroyImageView(c->device, F.src_view[slot], NULL);