mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 13:59:56 -04:00
Video memory: every VMA allocation by the heaps' live budgets, with room kept for FidelityFX
The heap limits were fixed at device creation, and only texture images looked at the budget. FidelityFX allocates outside VMA (FSR 3.1 contexts, frame generation): 882 MiB of it with 4644 MiB of VMA's filled the 5526 MiB budget of heap 0 while heap 2 had 1757 MiB free, and an upload submit failed with VK_ERROR_OUT_OF_DEVICE_MEMORY. vmaCreateImage, vmaCreateBuffer and vmaAllocateMemory now go through vk_vma_budget.c, which leaves out a device-local heap whose driver-reported use is within NX2NAVI_VRAM_RESERVE_MB (default 512) of its budget; a Lama patches row sets it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -93,6 +93,7 @@ static const struct { const char *key, *section; } sections[] = {
|
||||
// the menu's Lama patches tab (ui/menu.cpp tab_lama)
|
||||
{"TEX_RELEASED_MAX", "performance"}, {"MIRROR_HOT", "performance"}, {"FREEZE_IDLE", "performance"}, {"SUBMIT_FINISH_KINDS", "performance"}, {"FOLIAGE", "performance"},
|
||||
{"ZSTD_NATIVE", "performance"},
|
||||
{"VRAM_HEAP_LIMIT", "performance"}, {"VRAM_RESERVE_MB", "performance"}, {"UPLOAD_BATCH_MB", "performance"}, {"MESH_EMULATION", "performance"},
|
||||
{"MESH_EMU_MB", "performance"}, {"MESH_EMU_TASKS", "performance"}, {"MESH_EMU_BATCH", "performance"}, {"VEC_INDEX_FIX", "performance"},
|
||||
{"GUEST_OPT", "performance"}, {"PASS_TIMES", "diagnostics"}, {"DUMP_KEY", "diagnostics"},
|
||||
{"FPS_LIMIT", "frame rate"}, {"FPS60", "frame rate"}, {"RTSS", "frame rate"},
|
||||
@@ -306,6 +307,7 @@ static const CfgMeta metas[] = {
|
||||
{"SUBMIT_FINISH_KINDS", "31", "0..31", RESTART}, // vk_sync.c res_mask
|
||||
{"FOLIAGE", "full", "full|reduced|off", RESTART}, // vk_exec_draw.c foliage_skip
|
||||
{"VRAM_HEAP_LIMIT", "auto", NULL, RESTART}, // vk_device.c vram_heap_limits: auto, 0 or MiB
|
||||
{"VRAM_RESERVE_MB", "512", "0..4096", RESTART}, // vk_vma_budget.c: video memory kept free of VMA's allocations
|
||||
{"UPLOAD_BATCH_MB", "256", NULL, RESTART}, // vk_memory.c: MiB of staging before a batch goes early, 0 no limit
|
||||
{"MESH_EMULATION", "1", "0|1", RESTART}, {"MESH_EMU_MB", "256", "32..2048", RESTART},
|
||||
{"MESH_EMU_TASKS", "16384", "256..262144", RESTART}, {"MESH_EMU_BATCH", "1", "0|1", RESTART}, {"VEC_INDEX_FIX", "0", "0|auto|1", RESTART},
|
||||
|
||||
@@ -1640,6 +1640,10 @@ static void tab_lama(float scale) {
|
||||
const char *names[] = {menu_text(UI_MENU_AUTO), menu_text(UI_MENU_OFF)};
|
||||
opt_choice_list("NX2NAVI_VRAM_HEAP_LIMIT", UI_MENU_LAMA_HEAP_LIMIT, values, names, 2, UI_MENU_LAMA_HEAP_LIMIT_DESC);
|
||||
}
|
||||
{ // vk_vma_budget.c reserve_bytes
|
||||
static const char *const values[] = {"512", "256", "768", "1024", "0"};
|
||||
const char *names[] = {"512 MB", "256 MB", "768 MB", "1024 MB", menu_text(UI_MENU_OFF)};
|
||||
opt_choice_list("NX2NAVI_VRAM_RESERVE_MB", UI_MENU_LAMA_RESERVE, values, names, 5, UI_MENU_LAMA_RESERVE_DESC);
|
||||
}
|
||||
{ // vk_memory.c upload_batch_cap
|
||||
static const char *const values[] = {"128", "256", "512", "1024", "0"};
|
||||
|
||||
@@ -105,6 +105,10 @@ static const char *const text_en[UI_TEXT_COUNT] = {
|
||||
"its own. Fixes the teal splotches in Kokiri Forest's fog.",
|
||||
[UI_MENU_LAMA_MIRROR_HOT] = "Pools copied every frame stay in system memory",
|
||||
[UI_MENU_LAMA_MIRROR_HOT_DESC] = "A game memory pool whose copies to and from video memory each frame reach this "
|
||||
[UI_MENU_LAMA_RESERVE] = "Video memory kept free",
|
||||
[UI_MENU_LAMA_RESERVE_DESC] = "Room kept free in each video memory heap for what FidelityFX (FSR 3.1, frame generation) and the "
|
||||
"driver allocate on their own: a heap with less than this left takes no new allocation while "
|
||||
"another heap can. Off: video memory can fill up with frame generation on, and the game ends.",
|
||||
[UI_MENU_LAMA_HEAP_LIMIT] = "Video memory heap limit",
|
||||
[UI_MENU_LAMA_HEAP_LIMIT_DESC] = "Auto: on GPUs with a second video memory heap (an RX 580 shows 6 GB and 2 GB), "
|
||||
"each heap is filled only to its budget before the next one is used, so the "
|
||||
|
||||
@@ -342,6 +342,7 @@ typedef enum {
|
||||
UI_MENU_LAMA_RELEASED, UI_MENU_LAMA_RELEASED_DESC,
|
||||
UI_MENU_LAMA_CLOBBER, UI_MENU_LAMA_CLOBBER_DESC,
|
||||
UI_MENU_LAMA_MIRROR_HOT, UI_MENU_LAMA_MIRROR_HOT_DESC,
|
||||
UI_MENU_LAMA_HEAP_LIMIT, UI_MENU_LAMA_HEAP_LIMIT_DESC, UI_MENU_LAMA_RESERVE, UI_MENU_LAMA_RESERVE_DESC,
|
||||
UI_MENU_LAMA_UPLOAD_BATCH, UI_MENU_LAMA_UPLOAD_BATCH_DESC, UI_MENU_LAMA_NO_LIMIT,
|
||||
UI_MENU_LAMA_ASTC_KEEP, UI_MENU_LAMA_ASTC_KEEP_DESC,
|
||||
UI_MENU_LAMA_MESH_EMU, UI_MENU_LAMA_MESH_EMU_DESC, UI_MENU_LAMA_MESH_NATIVE,
|
||||
|
||||
14
runtime/vk.h
14
runtime/vk.h
@@ -6,6 +6,20 @@
|
||||
#pragma clang diagnostic ignored "-Wnullability-completeness"
|
||||
#include "vk_mem_alloc.h"
|
||||
#pragma clang diagnostic pop
|
||||
// Every VMA allocation goes by the device-local heaps' live budgets (vk_vma_budget.c): a heap within the reserve of its
|
||||
// budget takes nothing new while another heap can.
|
||||
VkResult vk_vma_create_image(VmaAllocator vma, const VkImageCreateInfo *ici, const VmaAllocationCreateInfo *ai, VkImage *img,
|
||||
VmaAllocation *a, VmaAllocationInfo *info);
|
||||
VkResult vk_vma_create_buffer(VmaAllocator vma, const VkBufferCreateInfo *bci, const VmaAllocationCreateInfo *ai,
|
||||
VkBuffer *buf, VmaAllocation *a, VmaAllocationInfo *info);
|
||||
VkResult vk_vma_allocate_memory(VmaAllocator vma, const VkMemoryRequirements *req, const VmaAllocationCreateInfo *ai,
|
||||
VmaAllocation *a, VmaAllocationInfo *info);
|
||||
uint64_t vk_vma_moved(void); // allocations placed outside a full heap so far
|
||||
#ifndef VK_VMA_BUDGET_IMPL
|
||||
#define vmaCreateImage vk_vma_create_image
|
||||
#define vmaCreateBuffer vk_vma_create_buffer
|
||||
#define vmaAllocateMemory vk_vma_allocate_memory
|
||||
#endif
|
||||
#include "nvn_obj.h" // pool_t, tex_t, view_t, samp_par, NVNcopyRegion (memory and textures block)
|
||||
#include "gfx.h" // GfxShader, GfxShaderRes, GfxShaderAnswers (pipelines block)
|
||||
|
||||
|
||||
124
runtime/vk_vma_budget.c
Normal file
124
runtime/vk_vma_budget.c
Normal file
@@ -0,0 +1,124 @@
|
||||
// Video memory by the heaps' live budgets, for every allocation through VMA (vk.h maps vmaCreateImage, vmaCreateBuffer
|
||||
// and vmaAllocateMemory here).
|
||||
//
|
||||
// An RX 580 (8 GiB, AMD 23.10) has a 6 GiB device-local heap and a 2 GiB device-local, host-visible one. The heap limits
|
||||
// of vk_device.c (vram_heap_limits) are fixed when the device is made: each heap's budget then, less a tenth for the
|
||||
// memory VMA does not see. That memory grows later: AMD's FidelityFX (FSR 3.1 upscaling contexts, up to six, and frame
|
||||
// generation with its swapchain and context) allocates its own, outside VMA. With FSR 3.1 and frame generation on at
|
||||
// 1080p it reached 882 MiB, and heap 0 held 4644 MiB of VMA's beside it: 5526 of a 5526 MiB budget, while heap 2 had
|
||||
// 1757 MiB free. The next upload submit failed with VK_ERROR_OUT_OF_DEVICE_MEMORY and the game ended (2026-11-11).
|
||||
// Only texture images looked at the budget (vk_texture.c image_alloc); render targets, pool memory, mirrors and the
|
||||
// rest were placed in heap 0 up to VMA's fixed limit.
|
||||
//
|
||||
// Here each allocation leaves out the memory types of a device-local heap whose use, as the driver reports it
|
||||
// (VK_EXT_memory_budget: VMA's budget, which counts the memory VMA does not see), is within the reserve of its budget,
|
||||
// and VMA places it in the other device-local heap, or system memory, as it does past the fixed limits. The reserve
|
||||
// (NX2NAVI_VRAM_RESERVE_MB, default 512, 0 off) is room left for allocations VMA does not make, which can come at
|
||||
// once: AMD's contexts are made when the upscaler or the frame generation setting changes. An allocation that finds
|
||||
// no room that way is made as asked, as before. Allocations from a VMA pool and those for one memory type alone (the
|
||||
// headroom's anchors, vk_headroom.c) are made as asked.
|
||||
#define VK_VMA_BUDGET_IMPL
|
||||
#include <stdlib.h>
|
||||
#include "vk.h"
|
||||
#include "cfg.h"
|
||||
#include "host.h"
|
||||
#include "platform/platform.h"
|
||||
|
||||
static VkDeviceSize reserve_bytes(void) {
|
||||
static int64_t r = -1;
|
||||
if (r < 0) {
|
||||
const char *e = cfg_get("NX2NAVI_VRAM_RESERVE_MB");
|
||||
long mb = e && *e ? atol(e) : 512;
|
||||
r = (int64_t)(mb < 0 ? 0 : mb) << 20;
|
||||
}
|
||||
return (VkDeviceSize)r;
|
||||
}
|
||||
|
||||
static volatile int64_t n_moved; // allocations placed outside a full heap
|
||||
static volatile uint32_t full_seen; // heaps already logged as full (bit per heap)
|
||||
|
||||
// The memory types of bits that may take a new allocation: those of device-local heaps within reserve of their budget
|
||||
// are left out. Returns bits itself when nothing is left out or nothing would be left.
|
||||
static uint32_t budget_bits(VmaAllocator vma, uint32_t bits) {
|
||||
VkDeviceSize reserve = reserve_bytes();
|
||||
if (!reserve) return bits;
|
||||
const VkPhysicalDeviceMemoryProperties *mp;
|
||||
vmaGetMemoryProperties(vma, &mp);
|
||||
VmaBudget b[VK_MAX_MEMORY_HEAPS];
|
||||
vmaGetHeapBudgets(vma, b);
|
||||
uint32_t full = 0;
|
||||
for (uint32_t h = 0; h < mp->memoryHeapCount && h < VK_MAX_MEMORY_HEAPS; h++)
|
||||
if ((mp->memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) && b[h].budget &&
|
||||
b[h].usage + reserve > b[h].budget)
|
||||
full |= 1u << h;
|
||||
if (!full) return bits;
|
||||
uint32_t out = 0;
|
||||
for (uint32_t t = 0; t < mp->memoryTypeCount; t++)
|
||||
if ((bits >> t & 1) && !(full >> mp->memoryTypes[t].heapIndex & 1)) out |= 1u << t;
|
||||
if (!out || out == bits) return bits;
|
||||
uint32_t told = __atomic_fetch_or(&full_seen, full, __ATOMIC_RELAXED);
|
||||
for (uint32_t h = 0; h < VK_MAX_MEMORY_HEAPS; h++)
|
||||
if ((full >> h & 1) && !(told >> h & 1))
|
||||
rt_log("vk: video memory heap %u full: %llu of its %llu MiB budget in use (%llu MiB of it VMA's), less %llu MiB "
|
||||
"kept free (NX2NAVI_VRAM_RESERVE_MB): new allocations go to the other heaps while it is", h,
|
||||
(unsigned long long)(b[h].usage >> 20), (unsigned long long)(b[h].budget >> 20),
|
||||
(unsigned long long)(b[h].statistics.blockBytes >> 20), (unsigned long long)(reserve >> 20));
|
||||
return out;
|
||||
}
|
||||
|
||||
static void note_moved(void) {
|
||||
int64_t n = __atomic_add_fetch(&n_moved, 1, __ATOMIC_RELAXED);
|
||||
if (!(n & (n - 1)) && n >= 16)
|
||||
rt_log("vk: video memory: %lld allocations placed outside a full heap so far", (long long)n);
|
||||
}
|
||||
|
||||
uint64_t vk_vma_moved(void) { return (uint64_t)__atomic_load_n(&n_moved, __ATOMIC_RELAXED); }
|
||||
|
||||
// Device-local types of bits (0: all), from the allocator's properties: whether the request could land in a heap
|
||||
// budget_bits leaves out
|
||||
static uint32_t all_bits(VmaAllocator vma, uint32_t bits) {
|
||||
if (bits) return bits;
|
||||
const VkPhysicalDeviceMemoryProperties *mp;
|
||||
vmaGetMemoryProperties(vma, &mp);
|
||||
return mp->memoryTypeCount >= 32 ? ~0u : (1u << mp->memoryTypeCount) - 1;
|
||||
}
|
||||
|
||||
VkResult vk_vma_create_image(VmaAllocator vma, const VkImageCreateInfo *ici, const VmaAllocationCreateInfo *ai, VkImage *img,
|
||||
VmaAllocation *a, VmaAllocationInfo *info) {
|
||||
if (!ai->pool) {
|
||||
uint32_t bits = all_bits(vma, ai->memoryTypeBits), nb = budget_bits(vma, bits);
|
||||
if (nb != bits) {
|
||||
VmaAllocationCreateInfo b = *ai;
|
||||
b.memoryTypeBits = nb;
|
||||
if (vmaCreateImage(vma, ici, &b, img, a, info) == VK_SUCCESS) return note_moved(), VK_SUCCESS;
|
||||
}
|
||||
}
|
||||
return vmaCreateImage(vma, ici, ai, img, a, info);
|
||||
}
|
||||
|
||||
VkResult vk_vma_create_buffer(VmaAllocator vma, const VkBufferCreateInfo *bci, const VmaAllocationCreateInfo *ai,
|
||||
VkBuffer *buf, VmaAllocation *a, VmaAllocationInfo *info) {
|
||||
if (!ai->pool) {
|
||||
uint32_t bits = all_bits(vma, ai->memoryTypeBits), nb = budget_bits(vma, bits);
|
||||
if (nb != bits) {
|
||||
VmaAllocationCreateInfo b = *ai;
|
||||
b.memoryTypeBits = nb;
|
||||
if (vmaCreateBuffer(vma, bci, &b, buf, a, info) == VK_SUCCESS) return note_moved(), VK_SUCCESS;
|
||||
}
|
||||
}
|
||||
return vmaCreateBuffer(vma, bci, ai, buf, a, info);
|
||||
}
|
||||
|
||||
VkResult vk_vma_allocate_memory(VmaAllocator vma, const VkMemoryRequirements *req, const VmaAllocationCreateInfo *ai,
|
||||
VmaAllocation *a, VmaAllocationInfo *info) {
|
||||
uint32_t bits = req->memoryTypeBits & all_bits(vma, ai->memoryTypeBits);
|
||||
if (!ai->pool && (bits & (bits - 1))) { // more than one type: one alone is asked for on purpose
|
||||
uint32_t nb = budget_bits(vma, bits);
|
||||
if (nb != bits) {
|
||||
VkMemoryRequirements r = *req;
|
||||
r.memoryTypeBits = nb;
|
||||
if (vmaAllocateMemory(vma, &r, ai, a, info) == VK_SUCCESS) return note_moved(), VK_SUCCESS;
|
||||
}
|
||||
}
|
||||
return vmaAllocateMemory(vma, req, ai, a, info);
|
||||
}
|
||||
Reference in New Issue
Block a user