From 798077b7cbf71bc2869301c394f3d467d1360e52 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Wed, 25 Nov 2026 05:40:47 +0100 Subject: [PATCH 1/3] d3d12_vulkan: leave the BAR window to host-visible VRAM allocations and stop retrying it when full Without Resizable BAR the DEVICE_LOCAL | HOST_VISIBLE type sits in a heap of about 256 MiB. memory_type_order put it second for every texture and default-heap buffer that prefers VRAM, so once VRAM filled up each such allocation failed there too, and the NVIDIA driver's VK_ERROR_OUT_OF_HOST_MEMORY answer was retried six times with sleeps under memory_mutex before falling back to system memory. Only allocations that ask for both DEVICE_LOCAL and HOST_VISIBLE now try the window in its place; the others try it last, and an out-of-host-memory answer from the window is believed the first time. Failures past the first 16 are counted at powers of two. --- runtime/shims/d3d12_vulkan_device.cpp | 11 ++++++++++- runtime/src/vulkan_memory.h | 24 ++++++++++++++++++++++-- runtime/test/d3d12_state_map_test.cpp | 20 +++++++++++++++++--- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 12494d201d..6fc5e6ab25 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -3958,8 +3958,11 @@ struct DeviceState { // and then while other threads import host pointers that the kernel module cannot pin (journal: "Cannot // map memory with base addr ..."), with gigabytes free. The title asserts on the texture it loses. So // allocations and imports take turns (memory_mutex), and that answer is retried before it is believed. + // Once in the BAR window: without Resizable BAR the driver gives the same answer when its 256 MiB are + // used up, and five more tries under the mutex only hold up every other allocation. + const int attempts = leonida::bar_window_type(memory, i) ? 1 : 6; VkResult allocated = VK_ERROR_OUT_OF_HOST_MEMORY; - for (int attempt = 0; attempt < 6 && allocated == VK_ERROR_OUT_OF_HOST_MEMORY; ++attempt) { + for (int attempt = 0; attempt < attempts && allocated == VK_ERROR_OUT_OF_HOST_MEMORY; ++attempt) { if (attempt) Sleep(attempt); std::lock_guard serial(memory_mutex()); allocated = context->vk.vkAllocateMemory(info.device, &allocate, nullptr, &out); @@ -3974,6 +3977,12 @@ struct DeviceState { } if (allocated != VK_SUCCESS) { static std::atomic logged{0}; + static std::atomic failures{0}; + const std::uint64_t failed = failures.fetch_add(1, std::memory_order_relaxed) + 1; + if (failed > 16 && (failed & (failed - 1)) == 0) + std::fprintf(stderr, "[rt] d3d12: vkAllocateMemory has failed %llu times (last: type %u, %llu bytes)\n", + static_cast(failed), i, + static_cast(requirements.size)); if (logged.fetch_add(1, std::memory_order_relaxed) < 16) std::fprintf(stderr, "[rt] d3d12: vkAllocateMemory failed (%d): %llu bytes, memory type %u (flags 0x%x, heap " diff --git a/runtime/src/vulkan_memory.h b/runtime/src/vulkan_memory.h index a818ce2054..6c5e148406 100644 --- a/runtime/src/vulkan_memory.h +++ b/runtime/src/vulkan_memory.h @@ -35,24 +35,44 @@ inline bool resizable_bar(const VkPhysicalDeviceMemoryProperties& memory) { return false; } +// True for a DEVICE_LOCAL | HOST_VISIBLE type in a device-local heap no larger than the BAR window. Without +// Resizable BAR that heap is the only VRAM the CPU can write, shared by every host-visible allocation that prefers +// VRAM (the arena, upload buffers). Once it is full the NVIDIA driver answers VK_ERROR_OUT_OF_HOST_MEMORY there. +inline bool bar_window_type(const VkPhysicalDeviceMemoryProperties& memory, std::uint32_t index) { + constexpr VkMemoryPropertyFlags wanted = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; + if (index >= memory.memoryTypeCount || index >= VK_MAX_MEMORY_TYPES) return false; + const VkMemoryType& type = memory.memoryTypes[index]; + if ((type.propertyFlags & wanted) != wanted || type.heapIndex >= memory.memoryHeapCount) return false; + const VkMemoryHeap& heap = memory.memoryHeaps[type.heapIndex]; + return (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) && heap.size <= kBarWindowBytes; +} + // The memory types to try for an allocation of `size` bytes, best first: those with `required | preferred`, then // those with only `required`. A type whose heap is smaller than the allocation is left out, as the allocation would // break VUID-vkAllocateMemory-pAllocateInfo-01713 (some drivers still return VK_SUCCESS). Without Resizable BAR the // heap of DEVICE_LOCAL | HOST_VISIBLE memory is typically 256 MiB or less, so a larger host-visible allocation falls // through to system memory. A heap that is big enough but already partly used is left to the driver: a failed -// vkAllocateMemory moves on to the next type. +// vkAllocateMemory moves on to the next type. Only an allocation that asks for both DEVICE_LOCAL and HOST_VISIBLE +// (the arena, upload buffers with Resizable BAR) tries a BAR-window type (bar_window_type) in its place; the others +// try it last. A texture or a staging buffer there takes the window from the arena, and once VRAM is full the window +// is too, so trying it early costs a failed call on every allocation before system memory. inline std::vector memory_type_order(const VkPhysicalDeviceMemoryProperties& memory, std::uint32_t type_bits, VkMemoryPropertyFlags required, VkMemoryPropertyFlags preferred, VkDeviceSize size) { std::vector order; + std::vector window; + constexpr VkMemoryPropertyFlags both = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; + const bool wants_window = ((required | preferred) & both) == both; for (const VkMemoryPropertyFlags flags : {required | preferred, required}) { for (std::uint32_t i = 0; i < memory.memoryTypeCount && i < VK_MAX_MEMORY_TYPES; ++i) { const VkMemoryType& type = memory.memoryTypes[i]; if (!(type_bits & (1u << i)) || (type.propertyFlags & flags) != flags) continue; if (type.heapIndex >= memory.memoryHeapCount || memory.memoryHeaps[type.heapIndex].size < size) continue; - if (std::find(order.begin(), order.end(), i) == order.end()) order.push_back(i); + std::vector& list = !wants_window && bar_window_type(memory, i) ? window : order; + if (std::find(list.begin(), list.end(), i) == list.end()) list.push_back(i); } } + order.insert(order.end(), window.begin(), window.end()); return order; } diff --git a/runtime/test/d3d12_state_map_test.cpp b/runtime/test/d3d12_state_map_test.cpp index 2fec30ba43..4c15354a68 100644 --- a/runtime/test/d3d12_state_map_test.cpp +++ b/runtime/test/d3d12_state_map_test.cpp @@ -4,7 +4,7 @@ #define VK_NO_PROTOTYPES #endif #include "../src/vulkan_replay.h" -#include "../src/vulkan_memory.h" // resizable_bar, memory_type_order +#include "../src/vulkan_memory.h" // resizable_bar, bar_window_type, memory_type_order #ifdef _WIN32 #include @@ -702,14 +702,28 @@ int main() { CHECK(memory_type_order(memory, 0xF, host, local, 128 * mib) == (order{3, 1, 2})); // fits the BAR heap CHECK(memory_type_order(memory, 0xF, host, local, 224395264) == (order{3, 1, 2})); // exactly fits CHECK(memory_type_order(memory, 0x7, host, local, 128 * mib) == (order{1, 2})); // type 3 not allowed - CHECK(memory_type_order(memory, 0xF, 0, local, 1 * mib) == (order{0, 3, 1, 2})); + // A texture that wants no host access tries the BAR window last, after system memory: there it would take + // the window from the arena, and with VRAM full the window is full too. + CHECK(memory_type_order(memory, 0xF, 0, local, 1 * mib) == (order{0, 1, 2, 3})); + CHECK(memory_type_order(memory, 0x8, 0, local, 1 * mib) == (order{3})); // the only type allowed + // A staging buffer (host access, no VRAM preference) also leaves the window to the arena. + memory.memoryTypes[1] = {local | host, 2}; + memory.memoryTypes[3] = {host, 1}; + CHECK(memory_type_order(memory, 0xF, host, 0, 1 * mib) == (order{2, 3, 1})); + CHECK(memory_type_order(memory, 0xF, host, local, 1 * mib) == (order{1, 2, 3})); // the arena keeps it first + memory.memoryTypes[1] = {host, 1}; + memory.memoryTypes[3] = {local | host, 2}; + CHECK(bar_window_type(memory, 3)); + CHECK(!bar_window_type(memory, 0) && !bar_window_type(memory, 1) && !bar_window_type(memory, 4)); // D3D-299: types 1 and 2 lack DEVICE_LOCAL; when preferred local fails, allocations fall back to them (host spill). const order spill_order = memory_type_order(memory, 0xF, 0, local, 1 * mib); + CHECK((memory.memoryTypes[spill_order[1]].propertyFlags & local) == 0); CHECK((memory.memoryTypes[spill_order[2]].propertyFlags & local) == 0); - CHECK((memory.memoryTypes[spill_order[3]].propertyFlags & local) == 0); CHECK(memory_type_order(memory, 0xF, host, local, 64ull << 30).empty()); // nothing that large memory.memoryHeaps[2].size = 24ull << 30; // Resizable BAR on CHECK(memory_type_order(memory, 0xF, host, local, 256 * mib) == (order{3, 1, 2})); + CHECK(!bar_window_type(memory, 3)); + CHECK(memory_type_order(memory, 0xF, 0, local, 1 * mib) == (order{0, 3, 1, 2})); } // block_format: bytes per texel, or per 4x4 block for block-compressed formats, of one aspect as a copy lays a From 092cc9e07efb0d04e1fca0094a87b36a423b3ed4 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Wed, 25 Nov 2026 06:57:33 +0100 Subject: [PATCH 2/3] d3d12_vulkan: try a memory type once while its last six tries all failed With VRAM full the NVIDIA driver answers VK_ERROR_OUT_OF_HOST_MEMORY for the device-local heap too, so every texture that spilled to system memory first slept through five retries under memory_mutex. After a type fails all six tries, allocate tries it once for the next two seconds. The driver's spurious answer with memory free still gets its retries, since a retry that succeeds never marks the type. --- runtime/shims/d3d12_vulkan_device.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 6fc5e6ab25..91cb8384a1 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -1835,6 +1835,8 @@ struct DeviceState { bool upload_in_vram = false; // D3D-299: the first texture or default-heap buffer that lands outside device-local memory is logged, once. mutable std::once_flag spill_logged; + // GetTickCount64 until which allocate tries a memory type once instead of six times (see allocate). + mutable std::array, VK_MAX_MEMORY_TYPES> type_exhausted_until{}; // What D3D12_OPTIONS1 answers with: the physical device's own subgroup facts, read on first use. struct SubgroupFacts { @@ -3959,8 +3961,10 @@ struct DeviceState { // map memory with base addr ..."), with gigabytes free. The title asserts on the texture it loses. So // allocations and imports take turns (memory_mutex), and that answer is retried before it is believed. // Once in the BAR window: without Resizable BAR the driver gives the same answer when its 256 MiB are - // used up, and five more tries under the mutex only hold up every other allocation. - const int attempts = leonida::bar_window_type(memory, i) ? 1 : 6; + // used up. Once too in a type whose last six tries all failed in the past two seconds: the driver also + // answers so when VRAM is full, and more tries under the mutex only hold up every other allocation. + const bool exhausted = GetTickCount64() < type_exhausted_until[i].load(std::memory_order_relaxed); + const int attempts = leonida::bar_window_type(memory, i) || exhausted ? 1 : 6; VkResult allocated = VK_ERROR_OUT_OF_HOST_MEMORY; for (int attempt = 0; attempt < attempts && allocated == VK_ERROR_OUT_OF_HOST_MEMORY; ++attempt) { if (attempt) Sleep(attempt); @@ -3975,6 +3979,8 @@ struct DeviceState { attempt, static_cast(requirements.size), i); } } + if (allocated == VK_ERROR_OUT_OF_HOST_MEMORY && attempts > 1) + type_exhausted_until[i].store(GetTickCount64() + 2000, std::memory_order_relaxed); if (allocated != VK_SUCCESS) { static std::atomic logged{0}; static std::atomic failures{0}; From 1940c2ac4636f614132a778f171f7fcf23987a06 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Wed, 25 Nov 2026 07:00:47 +0100 Subject: [PATCH 3/3] d3d12_vulkan: a tile heap prefers device-local memory instead of requiring it ID3D12Heap tiles for reserved resources were allocated with DEVICE_LOCAL required, so once VRAM was full (an 8 GB card without Resizable BAR, during the story load) the allocation failed for every type and tiles() returned nothing. The memory types are the sparse resource's own memoryTypeBits, so a system-memory type there is valid, and the heap now comes from it when VRAM has no room. --- runtime/shims/d3d12_vulkan_device.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 91cb8384a1..3fbf3d033c 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6497,7 +6497,9 @@ class Heap final if (!image) memory_types = device_.sparse_buffer_memory_types; const VkMemoryRequirements requirements{desc_.SizeInBytes, leonida::tile_size_in_bytes, memory_types}; - const auto memory = device_.allocate(requirements, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 0, + // VRAM preferred, not required: with VRAM full a tile heap the title asked for has to come from system + // memory rather than not at all. memory_types is the sparse resource's own memoryTypeBits. + const auto memory = device_.allocate(requirements, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, nullptr, !image, &memory_type); if (!memory) return {}; image_tiles_ = image;