diff options
| author | aslan2142 <jan.trencansky@protonmail.com> | 2026-11-25 15:38:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-25 15:38:52 +0100 |
| commit | c3b58ef03dba482eef1b0d5a7dda5fec07ad382f (patch) | |
| tree | 89284ceb01dc763e80cf7e312a9b9239b49ebf1c /runtime/src | |
| parent | 24a29f8b4e17e186882d08d2aa0cc71f8eb5c388 (diff) | |
| parent | a607b779c4342436ca7d408630733223fac49246 (diff) | |
Merge pull request #1732 from kawaii-shakal/arena-rebar-cap
d3d12: keep a Resizable BAR arena in VRAM when the driver excludes it from a very large buffer (RT-ARENA-REBAR-FIT)
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/arena_size.h | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/runtime/src/arena_size.h b/runtime/src/arena_size.h index 196b03e3e..66eed0dc2 100644 --- a/runtime/src/arena_size.h +++ b/runtime/src/arena_size.h | |||
| @@ -9,7 +9,7 @@ | |||
| 9 | // The decision is a pure function of the variable's text and the two limits, so it is tested without a GPU | 9 | // The decision is a pure function of the variable's text and the two limits, so it is tested without a GPU |
| 10 | // (arena_size_test). It depends only on the Vulkan headers. | 10 | // (arena_size_test). It depends only on the Vulkan headers. |
| 11 | 11 | ||
| 12 | #include <vulkan/vulkan_core.h> | 12 | #include "vulkan_memory.h" |
| 13 | 13 | ||
| 14 | #include <algorithm> | 14 | #include <algorithm> |
| 15 | #include <cstdint> | 15 | #include <cstdint> |
| @@ -21,6 +21,9 @@ constexpr VkDeviceSize kArenaMiB = VkDeviceSize(1) << 20; | |||
| 21 | constexpr VkDeviceSize kArenaDefaultMiB = 256; | 21 | constexpr VkDeviceSize kArenaDefaultMiB = 256; |
| 22 | constexpr VkDeviceSize kArenaMinMiB = 16; | 22 | constexpr VkDeviceSize kArenaMinMiB = 16; |
| 23 | constexpr VkDeviceSize kArenaMaxMiB = 4095; | 23 | constexpr VkDeviceSize kArenaMaxMiB = 4095; |
| 24 | constexpr VkMemoryPropertyFlags kArenaRequiredMemory = | ||
| 25 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | ||
| 26 | constexpr VkMemoryPropertyFlags kArenaPreferredMemory = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; | ||
| 24 | 27 | ||
| 25 | // The device's limits; 0 is "not known", which caps nothing. | 28 | // The device's limits; 0 is "not known", which caps nothing. |
| 26 | struct ArenaLimits { | 29 | struct ArenaLimits { |
| @@ -43,6 +46,7 @@ enum ArenaCap : unsigned { | |||
| 43 | arena_cap_none = 0, | 46 | arena_cap_none = 0, |
| 44 | arena_cap_buffer = 1, // maxBufferSize | 47 | arena_cap_buffer = 1, // maxBufferSize |
| 45 | arena_cap_allocation = 2, // maxMemoryAllocationSize | 48 | arena_cap_allocation = 2, // maxMemoryAllocationSize |
| 49 | arena_cap_bar = 4, // the largest size whose buffer may use DEVICE_LOCAL | HOST_VISIBLE memory | ||
| 46 | }; | 50 | }; |
| 47 | 51 | ||
| 48 | struct ArenaSize { | 52 | struct ArenaSize { |
| @@ -82,6 +86,7 @@ inline ArenaSize decide_arena_size(const char* env, const ArenaLimits& limits, V | |||
| 82 | 86 | ||
| 83 | // The Vulkan names of the limits in `capped`, for the log line. | 87 | // The Vulkan names of the limits in `capped`, for the log line. |
| 84 | inline const char* arena_cap_name(unsigned capped) { | 88 | inline const char* arena_cap_name(unsigned capped) { |
| 89 | if (capped & arena_cap_bar) return "Resizable BAR memory types for a buffer that large"; | ||
| 85 | switch (capped) { | 90 | switch (capped) { |
| 86 | case arena_cap_buffer: return "maxBufferSize"; | 91 | case arena_cap_buffer: return "maxBufferSize"; |
| 87 | case arena_cap_allocation: return "maxMemoryAllocationSize"; | 92 | case arena_cap_allocation: return "maxMemoryAllocationSize"; |
| @@ -90,4 +95,28 @@ inline const char* arena_cap_name(unsigned capped) { | |||
| 90 | } | 95 | } |
| 91 | } | 96 | } |
| 92 | 97 | ||
| 98 | constexpr VkDeviceSize kArenaBarStep = 16 * kArenaMiB; | ||
| 99 | |||
| 100 | // Apply the same memory-type eligibility checks as the arena allocation, requiring its preferred device-local type. | ||
| 101 | inline bool device_local_arena_fits(const VkPhysicalDeviceMemoryProperties& memory, | ||
| 102 | const VkMemoryRequirements& requirements) { | ||
| 103 | return !memory_type_order(memory, requirements.memoryTypeBits, kArenaRequiredMemory | kArenaPreferredMemory, | ||
| 104 | 0, requirements.size).empty(); | ||
| 105 | } | ||
| 106 | |||
| 107 | // With Resizable BAR the arena belongs in VRAM the CPU can write. A driver can leave those memory types out of a | ||
| 108 | // large buffer's memoryTypeBits: on an RTX 5070 Ti (NVIDIA 617.42, Windows 11) a 4,090 MiB buffer may use the | ||
| 109 | // DEVICE_LOCAL | HOST_VISIBLE type and a 4,092 MiB one may not, so LEONIDA_ARENA_MB=4096 (4,095 MiB) put the whole | ||
| 110 | // arena in system memory. `fits(bytes)` says whether a buffer of that size may use such a type. Returns the size to | ||
| 111 | // create: `bytes` when it fits (or when nothing in reach does), else the largest size below it, in 16 MiB steps | ||
| 112 | // and losing at most an eighth, that fits. | ||
| 113 | template <class Fits> | ||
| 114 | VkDeviceSize device_local_arena_bytes(VkDeviceSize bytes, Fits fits) { | ||
| 115 | if (fits(bytes)) return bytes; | ||
| 116 | const VkDeviceSize floor = std::max(bytes - bytes / 8, kArenaMinMiB * kArenaMiB); | ||
| 117 | for (VkDeviceSize size = bytes / kArenaBarStep * kArenaBarStep; size >= floor && size > 0; size -= kArenaBarStep) | ||
| 118 | if (size < bytes && fits(size)) return size; | ||
| 119 | return bytes; | ||
| 120 | } | ||
| 121 | |||
| 93 | } // namespace leonida | 122 | } // namespace leonida |