From fbb6783664be511e4cab267f7347a0574aedf328 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Mon, 9 Nov 2026 14:00:12 -0500 Subject: [PATCH] Hold less device memory that only costs the system's commit: a smaller released-texture cache, no spare blocks while commit is low, large images in memory of their own. The driver charges device memory to the system's commit. A 32 GB PC lost the device at an area load with the system at 61 of 65.5 GB committed, the game at 28 GB: 12.35 GB of VRAM in use, of it up to 1.3 GB in released textures kept for reuse, 0.5 to 1.8 GB free inside VMA's blocks, and a spare 256 MiB block per memory type. - Released textures keep a thirty-second of the installed memory (512 MiB to 2 GiB) instead of a sixteenth (1 to 4 GiB); only those released STALE_FRAMES presents ago go, so the transient ones the game takes back stay. - The headroom thread keeps no spare block while the system's commit is low (vk_texture_commit_low), as it already keeps none under VRAM pressure. - A texture image of 32 MiB or more gets memory of its own (DEDICATED_MIN), so freeing it gives the memory back at once instead of leaving a hole in a shared block that the driver counts in full until the block empties. Co-Authored-By: Claude Opus 5.5 --- runtime/vk.h | 1 + runtime/vk_headroom.c | 4 +++- runtime/vk_texture.c | 22 ++++++++++++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 9dd9f253..676cb43a 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -520,6 +520,7 @@ typedef struct { uint64_t live, released, own_live, own_released, bytes_live, by void vk_texture_census(VkTexCensus *s); void vk_texture_census_log(uint64_t frame); // vk_memory.c vram_log: the live entries' memory by kind and format void vk_texture_want_room(uint64_t bytes); // a VRAM mirror that did not fit: idle textures make that much room +int vk_texture_commit_low(void); // the system's commit was low at the last check (vk_texture.c) // vk_mem_vram's device-local use less the free space inside VMA's blocks (vk_memory.c), for the evictions int vk_mem_vram_live(uint64_t *used, uint64_t *budget); void vk_texture_shutdown(void); // vk_mem_shutdown calls it diff --git a/runtime/vk_headroom.c b/runtime/vk_headroom.c index a5f9699b..cf09a823 100644 --- a/runtime/vk_headroom.c +++ b/runtime/vk_headroom.c @@ -12,7 +12,8 @@ // a block per memory type is held back. // Under VRAM pressure no headroom is kept: the spare block counts against the budget that decides whether a pool gets // its VRAM mirror (vk_memory.c), so while use plus two blocks passes 85% of the budget the anchor goes and nothing new -// is allocated. +// is allocated. Nor while the system's commit is low (vk_texture_commit_low): the driver charges device memory to it, +// and a spare block of each type held up to 256 MiB of it empty when an allocation that failed there ended the game. // NX2NAVI_VRAM_HEADROOM=0 turns it off. #include "cfg.h" @@ -63,6 +64,7 @@ static VkDeviceSize block_size(const VkCtx *c, uint32_t type) { } static int room(const VkCtx *c, uint32_t type, VkDeviceSize block) { + if (vk_texture_commit_low()) return 0; VmaBudget b[VK_MAX_MEMORY_HEAPS]; vmaGetHeapBudgets(c->vma, b); const VmaBudget *h = &b[c->mem_props.memoryTypes[type].heapIndex]; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index f36ace36..df39a347 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -353,6 +353,7 @@ static int pool_fits(VkCtx *c, const Tex *e, const VkMemoryRequirements *req, ui // (vkGetImageMemoryRequirements does most of the driver's layout work) cost 5% of the replay in Kokiri Forest. static nvn_hmap img_reqs = {.klen = sizeof(ImgKey)}; // ImgKey -> VkMemoryRequirements (malloc'd) static uint64_t n_bind_skips; // since the last perf line +#define DEDICATED_MIN ((VkDeviceSize)32 << 20) // create_tex: images this large get memory of their own static int bind_pool_memory(VkCtx *c, Tex *e, VkImage img, uint64_t guest_size) { VkMemoryRequirements req; @@ -694,6 +695,11 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { if (!img) { if (gpu_only) n_reuse_new++; VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; + // An image of DEDICATED_MIN or more gets memory of its own: freed inside one of VMA's 256 MiB blocks it left a hole + // the driver went on counting, with the rest of the block, against the budget and the system's commit (0.5 to 1.8 + // GiB of such free space in a session at 5120x2160), until the block emptied; its own memory goes back when it does. + const VkMemoryRequirements *known = nvn_hm_get(&img_reqs, &e->ik); + if ((known ? known->size : image_size(&ci)) >= DEDICATED_MIN) ai.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; VkResult r = vmaCreateImage(c->vma, &ci, &ai, &img, &e->alloc, NULL); if (r != VK_SUCCESS) { log_limited(&n_failed, "image creation failed (%d): nvn fmt %#x %ux%ux%u target %d levels %u layers %u", (int)r, @@ -891,10 +897,12 @@ static void touch(Tex *e) { } static int idle(const Tex *e, uint64_t now); -// NX2NAVI_TEX_RELEASED_MB (default a sixteenth of the installed memory, 1 to 4 GiB; 0 = no limit) bounds the own memory -// of the released entries: RELEASED_MAX alone kept 5-7 GiB of released render targets and decoded textures after an -// hour of play at a 2160p game resolution, device memory the driver also charges to the system's commit (the process's -// private bytes reached 31 GiB). NX2NAVI_TEX_COMMIT_FREE_MB (default the larger of 4 GiB and a tenth of the commit +// NX2NAVI_TEX_RELEASED_MB (default a thirty-second of the installed memory, 512 MiB to 2 GiB; 0 = no limit) bounds the +// own memory of the released entries: RELEASED_MAX alone kept 5-7 GiB of released render targets and decoded textures +// after an hour of play at a 2160p game resolution, device memory the driver also charges to the system's commit (the +// process's private bytes reached 31 GiB). A sixteenth (1 to 4 GiB) still let them hold 1.3 GiB on a 32 GB PC whose +// commit then ran out at an area load (device lost); only entries released STALE_FRAMES presents ago go, so the +// transient ones the game takes back within frames stay. NX2NAVI_TEX_COMMIT_FREE_MB (default the larger of 4 GiB and a tenth of the commit // limit; 0 = off) lowers that bound to COMMIT_LOW_KEEP while less of the system's commit is free: an allocation that // fails there ends the game. Both drop only stale entries (drop_stale), a few per check like the VRAM pressure. #define COMMIT_LOW_KEEP ((uint64_t)256 << 20) @@ -911,8 +919,8 @@ static uint64_t released_cap_bytes(void) { else { float used, total, proc; plat_mem_info(&used, &total, &proc); - mb = total > 0 ? (long long)(total / 16) : 2048; - mb = mb < 1024 ? 1024 : mb > 4096 ? 4096 : mb; + mb = total > 0 ? (long long)(total / 32) : 1024; + mb = mb < 512 ? 512 : mb > 2048 ? 2048 : mb; } released_cap = mb > 0 ? (uint64_t)mb << 20 : UINT64_MAX; if (mb > 0) rt_log("vk: released textures keep at most %lld MiB of their own memory", mb); @@ -943,6 +951,8 @@ static int commit_is_low(void) { return commit_low = low; } +int vk_texture_commit_low(void) { return __atomic_load_n(&commit_low, __ATOMIC_RELAXED); } + // Drops up to PRESSURE_STEP released entries that own memory and were released STALE_FRAMES presents ago or earlier, // oldest first and the intact ones before the others, while the released entries own more than keep. A transient // texture is taken back within frames, so dropping it frees nothing: the game initializes it again and it is created