From 96bbe55ce88ab79ccdbfaf26f78ce34154ac47b7 Mon Sep 17 00:00:00 2001 From: kawaii-shakal Date: Wed, 25 Nov 2026 15:01:30 +0500 Subject: [PATCH 1/2] d3d12: keep a Resizable BAR arena in VRAM when the driver excludes it from a very large buffer (RT-ARENA-REBAR-FIT) On an RTX 5070 Ti (NVIDIA 617.42) a 4,090 MiB arena-shaped buffer may use the DEVICE_LOCAL | HOST_VISIBLE type and a 4,092 MiB one may not, so LEONIDA_ARENA_MB=4096 (4,095 MiB, the workaround from #1537) placed the whole arena in system memory: 4 GiB more host commit, and every default-heap buffer read over PCIe. With Resizable BAR, create_arena now tries 16 MiB smaller sizes, by at most an eighth, and keeps the largest whose buffer may still use that type (device_local_arena_bytes in arena_size.h). The cap is logged like the other device caps. Without Resizable BAR nothing changes. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 8 +++++++ runtime/shims/d3d12_vulkan_device.cpp | 31 +++++++++++++++++++++++++++ runtime/src/arena_size.h | 19 ++++++++++++++++ runtime/test/arena_size_test.cpp | 23 ++++++++++++++++++-- 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index d988a45f14..922b6d8eed 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1679,6 +1679,14 @@ The runtime allocates image keys from `0x100000000` (4 GiB) in 256-byte steps, a heap of 256 MiB or less (214 MiB on an RTX 3090 with driver 616.56), so the 256 MiB default arena lands in system memory there. Asking that heap for it broke VUID-vkAllocateMemory-pAllocateInfo-01713 even though the driver returned `VK_SUCCESS` (reported by CharlyRien). + - With Resizable BAR a driver can still leave the `DEVICE_LOCAL | HOST_VISIBLE` type out of a large buffer's + `memoryTypeBits`. On an RTX 5070 Ti (NVIDIA 617.42, Windows 11) an arena-shaped buffer of 4,090 MiB may use + it and one of 4,092 MiB may not, so `LEONIDA_ARENA_MB=4096` (4,095 MiB) put the whole arena in system memory: + 4 GiB more host commit, and every default-heap buffer read over PCIe. `create_arena` therefore tries 16 MiB + smaller sizes, by at most an eighth, and keeps the largest whose buffer may use that type + (`device_local_arena_bytes` in `runtime/src/arena_size.h`). It logs the result like the other caps: + `GPU address arena: 4080 MiB of the 4095 MiB asked for, capped by the device's Resizable BAR memory types`. + Without Resizable BAR nothing changes. - M4+: allocate the arena `DEVICE_LOCAL` and stage CPU writes (descriptor heaps, shader blobs, buffer uploads) through a host-visible staging buffer, copied before the commands that use them. The hash table is not in the arena either: it is a normal storage buffer in `DEVICE_LOCAL` memory, uploaded from a mapped staging copy before diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 5dd6b42e47..1a487ee7b4 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10247,6 +10247,37 @@ class Device final return false; } } + // With Resizable BAR, keep the arena where its buffer may still use DEVICE_LOCAL | HOST_VISIBLE memory + // (arena_size.h, device_local_arena_bytes): a driver can leave that type out of a very large buffer. + if (leonida::resizable_bar(state_.memory)) { + const auto device_local = [&](std::uint32_t bits) { + constexpr VkMemoryPropertyFlags wanted = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + for (std::uint32_t i = 0; i < state_.memory.memoryTypeCount; ++i) + if ((bits & (1u << i)) && (state_.memory.memoryTypes[i].propertyFlags & wanted) == wanted) return true; + return false; + }; + const VkDeviceSize asked = info.size; + const VkDeviceSize fitted = leonida::device_local_arena_bytes(asked, [&](VkDeviceSize bytes) { + if (bytes == asked) return device_local(requirements.memoryTypeBits); + VkBufferCreateInfo probe = info; + probe.size = bytes; + VkBuffer buffer = VK_NULL_HANDLE; + if (vk.vkCreateBuffer(state_.info.device, &probe, nullptr, &buffer) != VK_SUCCESS) return false; + VkMemoryRequirements r{}; + vk.vkGetBufferMemoryRequirements(state_.info.device, buffer, &r); + vk.vkDestroyBuffer(state_.info.device, buffer, nullptr); + return device_local(r.memoryTypeBits); + }); + if (fitted != asked) { + vk.vkDestroyBuffer(state_.info.device, arena.buffer, nullptr); + info.size = fitted; + if (!create_buffer()) return false; + size.bytes = fitted; + size.capped |= leonida::arena_cap_bar; + } + } if (size.capped) std::fprintf(stderr, "[rt] d3d12: GPU address arena: %llu MiB of the %llu MiB asked for, capped by the " "device's %s\n", diff --git a/runtime/src/arena_size.h b/runtime/src/arena_size.h index 196b03e3ee..829b9c6b2f 100644 --- a/runtime/src/arena_size.h +++ b/runtime/src/arena_size.h @@ -43,6 +43,7 @@ enum ArenaCap : unsigned { arena_cap_none = 0, arena_cap_buffer = 1, // maxBufferSize arena_cap_allocation = 2, // maxMemoryAllocationSize + arena_cap_bar = 4, // the largest size whose buffer may use DEVICE_LOCAL | HOST_VISIBLE memory }; struct ArenaSize { @@ -82,6 +83,7 @@ inline ArenaSize decide_arena_size(const char* env, const ArenaLimits& limits, V // The Vulkan names of the limits in `capped`, for the log line. inline const char* arena_cap_name(unsigned capped) { + if (capped & arena_cap_bar) return "Resizable BAR memory types for a buffer that large"; switch (capped) { case arena_cap_buffer: return "maxBufferSize"; case arena_cap_allocation: return "maxMemoryAllocationSize"; @@ -90,4 +92,21 @@ inline const char* arena_cap_name(unsigned capped) { } } +constexpr VkDeviceSize kArenaBarStep = 16 * kArenaMiB; + +// With Resizable BAR the arena belongs in VRAM the CPU can write. A driver can leave those memory types out of a +// large buffer's memoryTypeBits: on an RTX 5070 Ti (NVIDIA 617.42, Windows 11) a 4,090 MiB buffer may use the +// DEVICE_LOCAL | HOST_VISIBLE type and a 4,092 MiB one may not, so LEONIDA_ARENA_MB=4096 (4,095 MiB) put the whole +// arena in system memory. `fits(bytes)` says whether a buffer of that size may use such a type. Returns the size to +// create: `bytes` when it fits (or when nothing in reach does), else the largest size below it, in 16 MiB steps +// and losing at most an eighth, that fits. +template +VkDeviceSize device_local_arena_bytes(VkDeviceSize bytes, Fits fits) { + if (fits(bytes)) return bytes; + const VkDeviceSize floor = std::max(bytes - bytes / 8, kArenaMinMiB * kArenaMiB); + for (VkDeviceSize size = bytes / kArenaBarStep * kArenaBarStep; size >= floor && size > 0; size -= kArenaBarStep) + if (size < bytes && fits(size)) return size; + return bytes; +} + } // namespace leonida diff --git a/runtime/test/arena_size_test.cpp b/runtime/test/arena_size_test.cpp index 77dcf1f651..e4e2d342a8 100644 --- a/runtime/test/arena_size_test.cpp +++ b/runtime/test/arena_size_test.cpp @@ -106,11 +106,30 @@ int floor_and_limits() { return 0; } +// A driver that leaves Resizable BAR memory out of a large buffer's memoryTypeBits (RTX 5070 Ti, NVIDIA 617.42: +// above about 4,091 MiB): the arena shrinks in 16 MiB steps to the largest size that still may use it, by at most an +// eighth, and stays as asked when it already fits or when nothing in reach does. +int resizable_bar_fit() { + const auto under = [](VkDeviceSize limit) { return [limit](VkDeviceSize bytes) { return bytes <= limit; }; }; + CHECK(device_local_arena_bytes(4095 * MiB, under(4091 * MiB)) == 4080 * MiB); + CHECK(device_local_arena_bytes(4095 * MiB, under(5000 * MiB)) == 4095 * MiB); // fits: unchanged + CHECK(device_local_arena_bytes(256 * MiB, under(4091 * MiB)) == 256 * MiB); + CHECK(device_local_arena_bytes(4095 * MiB, under(1 * GiB)) == 4095 * MiB); // past an eighth: unchanged + CHECK(device_local_arena_bytes(4095 * MiB, [](VkDeviceSize) { return false; }) == 4095 * MiB); + unsigned calls = 0; + CHECK(device_local_arena_bytes(4095 * MiB, [&](VkDeviceSize bytes) { ++calls; return bytes <= 4064 * MiB; }) == + 4064 * MiB); + CHECK(calls == 3); // 4095 (as asked), then 4080 and 4064 MiB: one buffer probe per step tried + CHECK(std::string_view(arena_cap_name(arena_cap_bar | arena_cap_buffer)).find("Resizable BAR") == 0); + return 0; +} + } // namespace int main() { - for (const auto test : {request_without_limits, each_limit, unknown_limits, allocation_overhead, floor_and_limits}) + for (const auto test : {request_without_limits, each_limit, unknown_limits, allocation_overhead, floor_and_limits, + resizable_bar_fit}) if (test()) return 1; - std::printf("arena_size: request clamp, each device limit, unknown limits, allocation overhead and the floor\n"); + std::printf("arena_size: request clamp, each device limit, unknown limits, allocation overhead, the floor and the Resizable BAR fit\n"); return 0; } From a607b779c4342436ca7d408630733223fac49246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=9A=D0=BE?= =?UTF-8?q?=D1=80=D0=BB=D0=BE=D0=B4=D0=BD=D1=8B=D0=B9?= Date: Wed, 25 Nov 2026 10:53:03 +0000 Subject: [PATCH 2/2] d3d12: check heap eligibility when sizing the ReBAR arena AI-assisted by Codex. --- docs/d3d12_vulkan.md | 9 +++++---- runtime/shims/d3d12_vulkan_device.cpp | 20 ++++++-------------- runtime/src/arena_size.h | 12 +++++++++++- runtime/test/arena_size_test.cpp | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 922b6d8eed..2c1700bf50 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1681,10 +1681,11 @@ The runtime allocates image keys from `0x100000000` (4 GiB) in 256-byte steps, a driver returned `VK_SUCCESS` (reported by CharlyRien). - With Resizable BAR a driver can still leave the `DEVICE_LOCAL | HOST_VISIBLE` type out of a large buffer's `memoryTypeBits`. On an RTX 5070 Ti (NVIDIA 617.42, Windows 11) an arena-shaped buffer of 4,090 MiB may use - it and one of 4,092 MiB may not, so `LEONIDA_ARENA_MB=4096` (4,095 MiB) put the whole arena in system memory: - 4 GiB more host commit, and every default-heap buffer read over PCIe. `create_arena` therefore tries 16 MiB - smaller sizes, by at most an eighth, and keeps the largest whose buffer may use that type - (`device_local_arena_bytes` in `runtime/src/arena_size.h`). It logs the result like the other caps: + it and one of 4,092 MiB may not, so `LEONIDA_ARENA_MB=4096` (4,095 MiB) put the whole arena in system memory. + `create_arena` tries smaller sizes in 16 MiB steps, by at most an eighth, and keeps the largest whose + requirements allow that type and fit its heap. Placing the arena in ReBAR does not guarantee a reduction in + host commit. An [RTX 4080 comparison](https://github.com/ubiquitarianism/leonida/pull/1732#issuecomment-7159574385) + confirmed ReBAR placement and measured higher process private bytes. The runtime logs the size cap: `GPU address arena: 4080 MiB of the 4095 MiB asked for, capped by the device's Resizable BAR memory types`. Without Resizable BAR nothing changes. - M4+: allocate the arena `DEVICE_LOCAL` and stage CPU writes (descriptor heaps, shader blobs, buffer uploads) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 1a487ee7b4..1855c74164 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10248,19 +10248,11 @@ class Device final } } // With Resizable BAR, keep the arena where its buffer may still use DEVICE_LOCAL | HOST_VISIBLE memory - // (arena_size.h, device_local_arena_bytes): a driver can leave that type out of a very large buffer. + // A large buffer can exclude that type, or require more bytes than its heap can hold. if (leonida::resizable_bar(state_.memory)) { - const auto device_local = [&](std::uint32_t bits) { - constexpr VkMemoryPropertyFlags wanted = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | - VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; - for (std::uint32_t i = 0; i < state_.memory.memoryTypeCount; ++i) - if ((bits & (1u << i)) && (state_.memory.memoryTypes[i].propertyFlags & wanted) == wanted) return true; - return false; - }; const VkDeviceSize asked = info.size; const VkDeviceSize fitted = leonida::device_local_arena_bytes(asked, [&](VkDeviceSize bytes) { - if (bytes == asked) return device_local(requirements.memoryTypeBits); + if (bytes == asked) return leonida::device_local_arena_fits(state_.memory, requirements); VkBufferCreateInfo probe = info; probe.size = bytes; VkBuffer buffer = VK_NULL_HANDLE; @@ -10268,7 +10260,8 @@ class Device final VkMemoryRequirements r{}; vk.vkGetBufferMemoryRequirements(state_.info.device, buffer, &r); vk.vkDestroyBuffer(state_.info.device, buffer, nullptr); - return device_local(r.memoryTypeBits); + return (!limits.max_allocation_size || r.size <= limits.max_allocation_size) && + leonida::device_local_arena_fits(state_.memory, r); }); if (fitted != asked) { vk.vkDestroyBuffer(state_.info.device, arena.buffer, nullptr); @@ -10283,9 +10276,8 @@ class Device final "device's %s\n", static_cast(size.bytes >> 20), static_cast(size.requested >> 20), leonida::arena_cap_name(size.capped)); - arena.memory = state_.allocate(requirements, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, nullptr, true, &arena.memory_type); + arena.memory = state_.allocate(requirements, leonida::kArenaRequiredMemory, leonida::kArenaPreferredMemory, + nullptr, true, &arena.memory_type); void* mapped = nullptr; if (!arena.memory || vk.vkBindBufferMemory(state_.info.device, arena.buffer, arena.memory, 0) != VK_SUCCESS || vk.vkMapMemory(state_.info.device, arena.memory, 0, VK_WHOLE_SIZE, 0, &mapped) != VK_SUCCESS) { diff --git a/runtime/src/arena_size.h b/runtime/src/arena_size.h index 829b9c6b2f..66eed0dc23 100644 --- a/runtime/src/arena_size.h +++ b/runtime/src/arena_size.h @@ -9,7 +9,7 @@ // The decision is a pure function of the variable's text and the two limits, so it is tested without a GPU // (arena_size_test). It depends only on the Vulkan headers. -#include +#include "vulkan_memory.h" #include #include @@ -21,6 +21,9 @@ constexpr VkDeviceSize kArenaMiB = VkDeviceSize(1) << 20; constexpr VkDeviceSize kArenaDefaultMiB = 256; constexpr VkDeviceSize kArenaMinMiB = 16; constexpr VkDeviceSize kArenaMaxMiB = 4095; +constexpr VkMemoryPropertyFlags kArenaRequiredMemory = + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; +constexpr VkMemoryPropertyFlags kArenaPreferredMemory = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; // The device's limits; 0 is "not known", which caps nothing. struct ArenaLimits { @@ -94,6 +97,13 @@ inline const char* arena_cap_name(unsigned capped) { constexpr VkDeviceSize kArenaBarStep = 16 * kArenaMiB; +// Apply the same memory-type eligibility checks as the arena allocation, requiring its preferred device-local type. +inline bool device_local_arena_fits(const VkPhysicalDeviceMemoryProperties& memory, + const VkMemoryRequirements& requirements) { + return !memory_type_order(memory, requirements.memoryTypeBits, kArenaRequiredMemory | kArenaPreferredMemory, + 0, requirements.size).empty(); +} + // With Resizable BAR the arena belongs in VRAM the CPU can write. A driver can leave those memory types out of a // large buffer's memoryTypeBits: on an RTX 5070 Ti (NVIDIA 617.42, Windows 11) a 4,090 MiB buffer may use the // DEVICE_LOCAL | HOST_VISIBLE type and a 4,092 MiB one may not, so LEONIDA_ARENA_MB=4096 (4,095 MiB) put the whole diff --git a/runtime/test/arena_size_test.cpp b/runtime/test/arena_size_test.cpp index e4e2d342a8..c45af1a7eb 100644 --- a/runtime/test/arena_size_test.cpp +++ b/runtime/test/arena_size_test.cpp @@ -120,6 +120,21 @@ int resizable_bar_fit() { CHECK(device_local_arena_bytes(4095 * MiB, [&](VkDeviceSize bytes) { ++calls; return bytes <= 4064 * MiB; }) == 4064 * MiB); CHECK(calls == 3); // 4095 (as asked), then 4080 and 4064 MiB: one buffer probe per step tried + + VkPhysicalDeviceMemoryProperties memory{}; + memory.memoryHeapCount = 2; + memory.memoryHeaps[0] = {8 * GiB, 0}; + memory.memoryHeaps[1] = {4070 * MiB, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; + memory.memoryTypeCount = 2; + memory.memoryTypes[0] = {kArenaRequiredMemory, 0}; + memory.memoryTypes[1] = {kArenaRequiredMemory | kArenaPreferredMemory, 1}; + CHECK(device_local_arena_bytes(4095 * MiB, [&](VkDeviceSize bytes) { + const std::uint32_t bits = bytes > 4080 * MiB ? 0b01 : 0b11; + const VkMemoryRequirements requirements{bytes, 4096, bits}; + return device_local_arena_fits(memory, requirements); + }) == 4064 * MiB); + const VkMemoryRequirements padded{4071 * MiB, 4096, 0b11}; + CHECK(!device_local_arena_fits(memory, padded)); CHECK(std::string_view(arena_cap_name(arena_cap_bar | arena_cap_buffer)).find("Resizable BAR") == 0); return 0; }