Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,15 @@ 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.
`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)
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
Expand Down
29 changes: 26 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10247,14 +10247,37 @@ class Device final
return false;
}
}
// With Resizable BAR, keep the arena where its buffer may still use DEVICE_LOCAL | HOST_VISIBLE memory
// A large buffer can exclude that type, or require more bytes than its heap can hold.
if (leonida::resizable_bar(state_.memory)) {
const VkDeviceSize asked = info.size;
const VkDeviceSize fitted = leonida::device_local_arena_bytes(asked, [&](VkDeviceSize bytes) {
if (bytes == asked) return leonida::device_local_arena_fits(state_.memory, requirements);
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 (!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);
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",
static_cast<unsigned long long>(size.bytes >> 20),
static_cast<unsigned long long>(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) {
Expand Down
31 changes: 30 additions & 1 deletion runtime/src/arena_size.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <vulkan/vulkan_core.h>
#include "vulkan_memory.h"

#include <algorithm>
#include <cstdint>
Expand All @@ -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 {
Expand All @@ -43,6 +46,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 {
Expand Down Expand Up @@ -82,6 +86,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";
Expand All @@ -90,4 +95,28 @@ 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
// 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 <class Fits>
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
38 changes: 36 additions & 2 deletions runtime/test/arena_size_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,45 @@ 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

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;
}

} // 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;
}
Loading