mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 04:59:57 -04:00
Put textures and allocations past a full video memory heap only in another device-local heap, never in system memory.
On an RX 580 the second device-local heap (2 GiB) takes what the first has no room for. On a GPU with one device-local heap (an RTX 3050 Laptop, 4 GiB, 16 GiB of RAM) the budget wrapper and the texture placement sent everything past the heap's reserve to system memory instead: 4096 and more texture images and every other allocation, held in the process's commit (21 GiB). The system's commit ran out (31730 of 32023 MB), host memory imports failed, a pool was left without a GPU buffer and the GPU wrote through its address: device lost. Now an allocation leaves out a full heap only when another device-local heap has room, and a texture's second step allocates as before, past the budget, where the driver keeps it in video memory or moves it out itself, and NX2navi's own VRAM pressure drops textures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -131,7 +131,8 @@ static const char *const text_en[UI_TEXT_COUNT] = {
|
||||
[UI_MENU_LOWEND_RESERVE] = "Video memory kept free",
|
||||
[UI_MENU_LOWEND_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.",
|
||||
"another video memory heap can (an RX 580 has two; cards with one are not affected). Off: video "
|
||||
"memory can fill up with frame generation on, and the game ends.",
|
||||
[UI_MENU_LOWEND_HEAP_LIMIT] = "Video memory heap limit",
|
||||
[UI_MENU_LOWEND_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 "
|
||||
|
||||
@@ -1140,8 +1140,12 @@ static uint32_t detail_skip(const tex_t *t, const TexKey *k, const TexVkFormat *
|
||||
// device-local heap and a 2 GiB device-local, host-visible one. Asked for device-local memory without a budget, VMA
|
||||
// took the first heap every time: a save load put 5.6 GiB into it against a budget of 5.1 GiB while the other heap
|
||||
// held 69 MiB, and the next submit failed with VK_ERROR_OUT_OF_DEVICE_MEMORY. Each step below allocates within the
|
||||
// heaps' budgets (VK_EXT_memory_budget): device-local memory of any heap, then any memory the image allows (system
|
||||
// memory, slower to sample); only the last step allocates past the budget, as before.
|
||||
// heaps' budgets (VK_EXT_memory_budget): device-local memory of any heap; the second allocates as before, past the
|
||||
// budget, where the driver keeps it in video memory or moves it out itself. System memory is never asked for: on a GPU
|
||||
// with one device-local heap (an RTX 3050 Laptop, 4 GiB) a step for any memory within the budget put every texture past
|
||||
// the budget in system memory, 4096 and more images, which the process's commit then held: with 16 GiB of RAM the
|
||||
// system's commit ran out, host memory imports failed, and the GPU wrote through the address of a pool left without a
|
||||
// buffer (device lost).
|
||||
static uint64_t placed[VK_MAX_MEMORY_HEAPS], n_spilled;
|
||||
|
||||
static void note_placement(VkCtx *c, VmaAllocation a, int step) { // tex_lock held
|
||||
@@ -1163,17 +1167,16 @@ static void note_placement(VkCtx *c, VmaAllocation a, int step) { // tex_lock
|
||||
|
||||
static VkResult image_alloc(VkCtx *c, const VkImageCreateInfo *ci, VmaAllocationCreateFlags own, VkImage *img,
|
||||
VmaAllocation *a) {
|
||||
static const struct { VkMemoryPropertyFlags req; VmaAllocationCreateFlags flags; } step[3] = {
|
||||
static const struct { VkMemoryPropertyFlags req; VmaAllocationCreateFlags flags; } step[2] = {
|
||||
{VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT},
|
||||
{0, VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT},
|
||||
{0, 0}};
|
||||
VkResult r = VK_ERROR_OUT_OF_DEVICE_MEMORY;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
VmaAllocationCreateInfo ai = {.flags = step[i].flags | own, .usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
|
||||
.requiredFlags = step[i].req};
|
||||
r = vmaCreateImage(c->vma, ci, &ai, img, a, NULL);
|
||||
if (r == VK_SUCCESS) {
|
||||
note_placement(c, *a, i);
|
||||
note_placement(c, *a, i ? 2 : 0); // 2: past the budget
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,10 @@
|
||||
//
|
||||
// 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
|
||||
// and VMA places it in another device-local heap with room. Only there: on a GPU with one device-local heap (an RTX 3050
|
||||
// Laptop, 4 GiB) leaving that heap out sent every allocation past the reserve to system memory, more than 4096 of them,
|
||||
// and the process's commit with them: with 16 GiB of RAM the system's commit ran out, host memory imports failed and the
|
||||
// device was lost. Without another device-local heap with room the allocation is made as asked. The reserve
|
||||
// (NX2NAVI_VRAM_RESERVE_MB, 512 with low_end_gpu = 1, else 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
|
||||
@@ -53,15 +56,18 @@ static uint32_t budget_bits(VmaAllocator vma, uint32_t bits) {
|
||||
b[h].usage + reserve > b[h].budget)
|
||||
full |= 1u << h;
|
||||
if (!full) return bits;
|
||||
uint32_t out = 0;
|
||||
uint32_t out = 0, dl = 0; // dl: types of device-local heaps with room among them
|
||||
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;
|
||||
if ((bits >> t & 1) && !(full >> mp->memoryTypes[t].heapIndex & 1)) {
|
||||
out |= 1u << t;
|
||||
if (mp->memoryHeaps[mp->memoryTypes[t].heapIndex].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) dl |= 1u << t;
|
||||
}
|
||||
if (!dl || out == bits) return bits; // no other video memory with room: as asked, not system memory
|
||||
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,
|
||||
"kept free (NX2NAVI_VRAM_RESERVE_MB): new allocations go to the other device-local 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;
|
||||
|
||||
Reference in New Issue
Block a user