From 691d41a026b62b3ace723bbc5fb56210b7c6a65c Mon Sep 17 00:00:00 2001 From: chrg6 Date: Fri, 13 Nov 2026 00:46:26 -0500 Subject: [PATCH] Free the images of HDR, FSR 1 and SMAA once they are no longer used, instead of keeping them for the process. Each made its images at the picture's or the scene's size the first time it ran and kept them until the process ended, also after the menu switched it off: - the HDR encode's picture and menu image (28 and 14 MiB at 2560x1440, 63 and 32 MiB at 2160 lines) now go when the swapchain is made without HDR, after a wait for the presents that may still read them; - FSR 1's two images (56 MiB, 127 MiB at 2160 lines) go after 300 presents it did not scale, with the same wait; - SMAA's three work images (35 MiB, 79 MiB at 2160 lines) go after 600 frames of another anti-aliasing, deferred past the submission being recorded. Each makes them anew when it runs again; one log line says when they go. Co-Authored-By: Claude Opus 5.5 --- runtime/gfx_present.c | 1 + runtime/vk.h | 3 ++- runtime/vk_aa.c | 19 +++++++++++++++++++ runtime/vk_fsr.c | 23 +++++++++++++++++++++-- runtime/vk_hdr.c | 16 ++++++++++++++++ 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 7ceb05c9..208a1435 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -432,6 +432,7 @@ static int create_swapchain(void) { VkSurfaceFormatKHR sf = fmts[0]; g_sc_hdr_wanted = vk_hdr_wanted(); int hdr = g_sc_hdr_wanted && vk_hdr_surface_format(fmts, nf, &sf), found = hdr; + if (!hdr) vk_hdr_images_release(); // HDR off, or not on this display: what its encode made goes for (uint32_t i = 0; i < nf && !found; i++) if (fmts[i].format == VK_FORMAT_B8G8R8A8_UNORM && fmts[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { sf = fmts[i]; found = 1; } for (uint32_t i = 0; i < nf && !found; i++) diff --git a/runtime/vk.h b/runtime/vk.h index 750bd2c5..7b6f0d50 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -243,7 +243,8 @@ void vk_hdr_to_srgb8(const uint8_t *in, uint8_t *out, size_t px, int a2r); // Luminance statistics of an HDR10 shot inside rect {x, y, w, h}: the darkest pixel, the 0.1st, 1st, 50th and 99th // percentiles and the brightest pixel in nits, and the share of pixels under 0.01 nits (vk_hdr.c). void vk_hdr_luminance_stats(const uint8_t *in, int stride, const int rect[4], int a2r, float out[7]); -void vk_hdr_release(void); // before the device goes away +void vk_hdr_release(void); +void vk_hdr_images_release(void); // the swapchain has no HDR: the encode's images go (waits for the device) // before the device goes away // The support buffer's HDR settings, a vec4 at 1440 (vk_exec_draw.c support_fill; read by the game's tonemap with // shaders/hdr_tonemap.frag spliced in, vk_hdr.c tonemap_splice). void vk_hdr_support(float out[4]); diff --git a/runtime/vk_aa.c b/runtime/vk_aa.c index 1798f013..bcb95609 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 smaa_idle(int mode); + 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; + smaa_idle(A.mode); // the frame before's mode const char *v = cfg_value("NX2NAVI_AA"); int want = VK_AA_GAME; for (int i = 1; i < 4; i++) @@ -262,6 +265,22 @@ static int tables_make(void) { return 1; } +// SMAA's work images (35 MiB at a 2560x1440 scene, 79 MiB at 2160 lines) were kept for the process once SMAA had run. +// After SMAA_IDLE_FRAMES frames of another mode they go, once the GPU is past the submission being recorded +// (img_free defers); SMAA chosen again makes them anew. The lookup textures, a few hundred KiB, stay. +#define SMAA_IDLE_FRAMES 600 +static void smaa_idle(int mode) { + static unsigned idle; + if (mode == VK_AA_SMAA || !P.luma.img) { + idle = 0; + return; + } + if (++idle < SMAA_IDLE_FRAMES) return; + img_free(&P.luma), img_free(&P.edges), img_free(&P.weights); + P.w = P.h = 0, idle = 0; + rt_log("aa: SMAA has not run for %d frames: its work images are freed", SMAA_IDLE_FRAMES); +} + // SMAA's work images at the scene's size, made again when it changes static int work_images(VkCommandBuffer cb, uint32_t w, uint32_t h) { if (P.luma.img && P.w == w && P.h == h) return 1; diff --git a/runtime/vk_fsr.c b/runtime/vk_fsr.c index 59491881..cd1864aa 100644 --- a/runtime/vk_fsr.c +++ b/runtime/vk_fsr.c @@ -198,18 +198,37 @@ static int shrink_sharpen(VkCommandBuffer cb, VkImage src, const int crop[4], Vk return 2; } +// Presents in a row that FSR 1 did not scale (another upscaler chosen, or a picture the window shows at its own size) +// after which its two images at the window's size go (56 MiB at 2560x1440, 127 MiB at 2160 lines; kept for the process +// before). A present that scales again makes them anew. +#define FSR_IDLE_PRESENTS 300 +static unsigned fsr_idle; + +static void fsr_unused(void) { + if (!F.img[0] || ++fsr_idle < FSR_IDLE_PRESENTS) return; + VkCtx *c = vk_ctx(); + vk_queue_lock(); + vkDeviceWaitIdle(c->device); // earlier presents may still read them + vk_queue_unlock(); + free_images(c); + fsr_idle = 0; + rt_log("vk: FSR 1 has not scaled a picture for %d presents: its images are freed", FSR_IDLE_PRESENTS); +} + int vk_fsr_record(VkCommandBuffer cb, unsigned slot, VkImage src, VkFormat fmt, int sw, int sh, const int crop[4], 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 fsr_unused(), 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 fsr_unused(), 0; + fsr_idle = 0; VkCtx *c = vk_ctx(); if (!F.tried) init(c); if (!F.ok || !ensure_images(c, dw, dh)) return 0; diff --git a/runtime/vk_hdr.c b/runtime/vk_hdr.c index 43ef0152..f532631f 100644 --- a/runtime/vk_hdr.c +++ b/runtime/vk_hdr.c @@ -1143,6 +1143,22 @@ void vk_hdr_ui_compose(VkCommandBuffer cb, VkImageView dst_view, VkFormat fmt, u vkCmdEndRendering(cb); } +// The swapchain is made without HDR (gfx_present.c create_swapchain): the encode's picture and the menu's image at the +// picture's size (28 and 14 MiB at 2560x1440, 63 and 32 MiB at 2160 lines) were kept for the process once HDR had been +// on. They go now, after a wait for the presents that may still read them; HDR switched on again makes them anew. +void vk_hdr_images_release(void) { + VkCtx *c = vk_ctx(); + if (!c || (!H.out.img && !H.menu.img)) return; + vk_queue_lock(); + vkDeviceWaitIdle(c->device); + vk_queue_unlock(); + for (int i = 0; i < SLOTS; i++) + if (H.src_view[i]) vkDestroyImageView(c->device, H.src_view[i], NULL), H.src_view[i] = VK_NULL_HANDLE; + img_free(c, &H.out); + img_free(c, &H.menu); + rt_log("vk_hdr: HDR is off: the encode's images are freed"); +} + void vk_hdr_release(void) { VkCtx *c = vk_ctx(); for (int i = 0; i < SLOTS; i++)