Skip to content
Open
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
4 changes: 3 additions & 1 deletion runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7532,7 +7532,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;
Expand Down
24 changes: 22 additions & 2 deletions runtime/src/vulkan_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint32_t> memory_type_order(const VkPhysicalDeviceMemoryProperties& memory,
std::uint32_t type_bits, VkMemoryPropertyFlags required,
VkMemoryPropertyFlags preferred, VkDeviceSize size) {
std::vector<std::uint32_t> order;
std::vector<std::uint32_t> 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<std::uint32_t>& 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;
}

Expand Down
20 changes: 17 additions & 3 deletions runtime/test/d3d12_state_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "../src/vulkan_replay.h"
#include "../src/vulkan_index_view.h"
#include <cstring>
#include "../src/vulkan_memory.h" // resizable_bar, memory_type_order
#include "../src/vulkan_memory.h" // resizable_bar, bar_window_type, memory_type_order

// Argument-buffer bounds only, no Vulkan calls. A 64-byte compound record has padding to an
// 80-byte stride between records, but needs none after the last. A terminal DRAW_INDEXED alone is only 20 bytes.
Expand Down Expand Up @@ -893,14 +893,28 @@ static int memory_tests() {
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
Expand Down