diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 16:38:20 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 16:38:20 -0500 |
| commit | c116c3fa28983444cd47feea0461e1e7eaac83b5 (patch) | |
| tree | f2f4821b744b678f0cf1ce98638e981ea737b0c6 /runtime/src/vulkan_scratch.h | |
| parent | 5866d6974e86fc2d39e6ce0691af63b354f6c8b9 (diff) | |
| parent | 24d8af66af151f024b0b520fb397d899924f5486 (diff) | |
Merge main into t-drive-overlay
docs/boot.md: main added LEONIDA_WRITE_DIR / LEONIDA_SAVE_DIR to the
--overlay bullet and a new "Under Wine on Linux" section; keep both and
the T: paragraph of this branch after main's sentences.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_scratch.h')
| -rw-r--r-- | runtime/src/vulkan_scratch.h | 134 |
1 files changed, 96 insertions, 38 deletions
diff --git a/runtime/src/vulkan_scratch.h b/runtime/src/vulkan_scratch.h index 33d84bf72..958f27ca5 100644 --- a/runtime/src/vulkan_scratch.h +++ b/runtime/src/vulkan_scratch.h | |||
| @@ -1,14 +1,20 @@ | |||
| 1 | // Scratch buffers that live until the submission that used them has completed: device-local chunks handed out | 1 | // Scratch buffers that live until the submission that used them has completed: device-local chunks handed out |
| 2 | // while a list is prepared, retired with the submission's timeline value, and reused (or freed) once the GPU is past | 2 | // while a list is prepared, retired with the submission's timeline value, and reused (or freed) once the GPU is past |
| 3 | // it. The replay stages copies Vulkan cannot do in one command through them (vulkan_replay.h, depth <-> colour | 3 | // it. The replay stages copies Vulkan cannot do in one command through them (vulkan_replay.h, depth <-> colour |
| 4 | // copies). Single-threaded: the caller holds the context's lock. The same life cycle as the depth24 converter's own | 4 | // copies), and the ExecuteIndirect argument clamp keeps its record scratch in one too (vulkan_indirect_clamp.h). |
| 5 | // Single-threaded: the caller holds the context's lock. The same life cycle as the depth24 converter's own | ||
| 5 | // scratch (vulkan_depth24.h), which also needs descriptor pools and exists only on GPUs without D24. | 6 | // scratch (vulkan_depth24.h), which also needs descriptor pools and exists only on GPUs without D24. |
| 7 | // The chunks are transfer sources and destinations; `extra_usage` adds what another user needs (the expanded indices | ||
| 8 | // of quad lists, quad_index_expand.h: an index buffer written through its device address). | ||
| 6 | #pragma once | 9 | #pragma once |
| 7 | 10 | ||
| 8 | #ifndef VK_NO_PROTOTYPES | 11 | #ifndef VK_NO_PROTOTYPES |
| 9 | #define VK_NO_PROTOTYPES | 12 | #define VK_NO_PROTOTYPES |
| 10 | #endif | 13 | #endif |
| 11 | #include <vulkan/vulkan.h> | 14 | #include <vulkan/vulkan.h> |
| 15 | #include "queue_families.h" | ||
| 16 | |||
| 17 | #include "vulkan_device_fault.h" | ||
| 12 | 18 | ||
| 13 | #include <algorithm> | 19 | #include <algorithm> |
| 14 | #include <cstdint> | 20 | #include <cstdint> |
| @@ -22,15 +28,36 @@ class ScratchBuffers { | |||
| 22 | public: | 28 | public: |
| 23 | // A device-local allocation for a chunk; null on failure. | 29 | // A device-local allocation for a chunk; null on failure. |
| 24 | using Allocate = std::function<VkDeviceMemory(const VkMemoryRequirements&)>; | 30 | using Allocate = std::function<VkDeviceMemory(const VkMemoryRequirements&)>; |
| 31 | // What the copy staging needs: transfer both ways. | ||
| 32 | static constexpr VkBufferUsageFlags transfer_usage = | ||
| 33 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; | ||
| 34 | |||
| 35 | // Legacy copy-scratch constructor. `extra_usage` is added to the transfer usage, and | ||
| 36 | // `families` keeps retired chunks shareable across queue families. | ||
| 37 | ScratchBuffers(PFN_vkGetDeviceProcAddr get, VkDevice device, Allocate allocate, VkBufferUsageFlags extra_usage = 0, | ||
| 38 | std::vector<std::uint32_t> families = {}, DeviceFaultDiagnostics* diagnostics = nullptr) | ||
| 39 | : device_(device), diagnostics_(diagnostics), allocate_(std::move(allocate)), | ||
| 40 | usage_(transfer_usage | extra_usage), families_(std::move(families)) { | ||
| 41 | loaded_ = vk_.load(get, device, false); | ||
| 42 | addressable_ = (usage_ & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) && vk_.vkGetBufferDeviceAddress; | ||
| 43 | } | ||
| 25 | 44 | ||
| 26 | ScratchBuffers(PFN_vkGetDeviceProcAddr get, VkDevice device, Allocate allocate) | 45 | // `usage` is what the chunks are created with. A chunk that declares VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT |
| 27 | : device_(device), allocate_(std::move(allocate)) { | 46 | // gets its device address from get() as well, and its memory must have been allocated with |
| 28 | loaded_ = vk_.load(get, device); | 47 | // VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT (the allocate callback's job). `families` keeps retired chunks shareable |
| 48 | // across queue families (graphics + async compute). | ||
| 49 | ScratchBuffers(PFN_vkGetDeviceProcAddr get, VkDevice device, Allocate allocate, | ||
| 50 | DeviceFaultDiagnostics* diagnostics, VkBufferUsageFlags usage, | ||
| 51 | std::vector<std::uint32_t> families = {}) | ||
| 52 | : device_(device), diagnostics_(diagnostics), allocate_(std::move(allocate)), usage_(usage), | ||
| 53 | families_(std::move(families)), | ||
| 54 | addressable_((usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) != 0) { | ||
| 55 | loaded_ = vk_.load(get, device, addressable_); | ||
| 29 | } | 56 | } |
| 30 | 57 | ||
| 31 | ~ScratchBuffers() { | 58 | ~ScratchBuffers() { |
| 32 | if (!loaded_) return; | 59 | if (!loaded_) return; |
| 33 | vk_.vkDeviceWaitIdle(device_); | 60 | DeviceFaultDiagnostics::observe(diagnostics_, vk_.vkDeviceWaitIdle(device_), "vkDeviceWaitIdle"); |
| 34 | release(current_); | 61 | release(current_); |
| 35 | for (auto& batch : retired_) release(batch); | 62 | for (auto& batch : retired_) release(batch); |
| 36 | for (auto& chunk : spare_) destroy(chunk); | 63 | for (auto& chunk : spare_) destroy(chunk); |
| @@ -41,36 +68,14 @@ public: | |||
| 41 | 68 | ||
| 42 | bool loaded() const { return loaded_; } | 69 | bool loaded() const { return loaded_; } |
| 43 | 70 | ||
| 44 | // `bytes` of transfer scratch for the next submission, at a 256-byte aligned offset (more than any copy needs). | 71 | // `bytes` of scratch for the next submission, at a 256-byte aligned offset (more than any copy needs). |
| 45 | bool get(std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset) { | 72 | bool get(std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset) { |
| 46 | constexpr VkDeviceSize alignment = 256, minimum = 4u << 20; | 73 | return claim(bytes, buffer, offset, nullptr); |
| 47 | if (!loaded_ || !bytes) return false; | 74 | } |
| 48 | for (auto& chunk : current_.chunks) { | 75 | |
| 49 | const VkDeviceSize rem = chunk.used % alignment; | 76 | // The same for chunks created with a device address: `address` is the device address of the returned offset. |
| 50 | const VkDeviceSize pad = rem ? alignment - rem : 0; | 77 | bool get(std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset, VkDeviceAddress& address) { |
| 51 | if (pad <= chunk.size - chunk.used) { | 78 | return claim(bytes, buffer, offset, &address); |
| 52 | const VkDeviceSize start = chunk.used + pad; | ||
| 53 | if (bytes <= chunk.size - start) { | ||
| 54 | chunk.used = start + bytes; | ||
| 55 | buffer = chunk.buffer; | ||
| 56 | offset = start; | ||
| 57 | return true; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | Chunk chunk; | ||
| 62 | for (std::size_t i = 0; i < spare_.size(); ++i) | ||
| 63 | if (spare_[i].size >= bytes) { | ||
| 64 | chunk = spare_[i]; | ||
| 65 | spare_.erase(spare_.begin() + std::ptrdiff_t(i)); | ||
| 66 | break; | ||
| 67 | } | ||
| 68 | if (!chunk.buffer && !make_chunk(std::max<std::uint64_t>(bytes, minimum), chunk)) return false; | ||
| 69 | chunk.used = bytes; | ||
| 70 | buffer = chunk.buffer; | ||
| 71 | offset = 0; | ||
| 72 | current_.chunks.push_back(chunk); | ||
| 73 | return true; | ||
| 74 | } | 79 | } |
| 75 | 80 | ||
| 76 | // The chunks handed out since the last call belong to a submission that `timeline` reaches `ticket` for | 81 | // The chunks handed out since the last call belong to a submission that `timeline` reaches `ticket` for |
| @@ -101,7 +106,9 @@ public: | |||
| 101 | if (retired_[i].ticket != 0) { | 106 | if (retired_[i].ticket != 0) { |
| 102 | std::uint64_t done = 0; | 107 | std::uint64_t done = 0; |
| 103 | if (!retired_[i].timeline || | 108 | if (!retired_[i].timeline || |
| 104 | vk_.vkGetSemaphoreCounterValue(device_, retired_[i].timeline, &done) != VK_SUCCESS || | 109 | DeviceFaultDiagnostics::observe(diagnostics_, |
| 110 | vk_.vkGetSemaphoreCounterValue(device_, retired_[i].timeline, &done), | ||
| 111 | "vkGetSemaphoreCounterValue (scratch)") != VK_SUCCESS || | ||
| 105 | done < retired_[i].ticket) { | 112 | done < retired_[i].ticket) { |
| 106 | ++i; | 113 | ++i; |
| 107 | continue; | 114 | continue; |
| @@ -136,8 +143,9 @@ private: | |||
| 136 | #define LEONIDA_SCRATCH_MEMBER(name) PFN_##name name = nullptr; | 143 | #define LEONIDA_SCRATCH_MEMBER(name) PFN_##name name = nullptr; |
| 137 | LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_MEMBER) | 144 | LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_MEMBER) |
| 138 | #undef LEONIDA_SCRATCH_MEMBER | 145 | #undef LEONIDA_SCRATCH_MEMBER |
| 146 | PFN_vkGetBufferDeviceAddress vkGetBufferDeviceAddress = nullptr; // optional: addressable pools only | ||
| 139 | 147 | ||
| 140 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | 148 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device, bool addressable) { |
| 141 | bool all = true; | 149 | bool all = true; |
| 142 | #define LEONIDA_SCRATCH_LOAD(name) \ | 150 | #define LEONIDA_SCRATCH_LOAD(name) \ |
| 143 | name = reinterpret_cast<PFN_##name>(get(device, #name)); \ | 151 | name = reinterpret_cast<PFN_##name>(get(device, #name)); \ |
| @@ -145,14 +153,60 @@ private: | |||
| 145 | LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_LOAD) | 153 | LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_LOAD) |
| 146 | #undef LEONIDA_SCRATCH_LOAD | 154 | #undef LEONIDA_SCRATCH_LOAD |
| 147 | #undef LEONIDA_SCRATCH_FUNCTIONS | 155 | #undef LEONIDA_SCRATCH_FUNCTIONS |
| 148 | return all; | 156 | // Only an addressable pool (the indirect clamp's) needs the addresses: a Vulkan 1.0/1.1 device without |
| 157 | // bufferDeviceAddress keeps the transfer scratch. | ||
| 158 | this->vkGetBufferDeviceAddress = | ||
| 159 | reinterpret_cast<PFN_vkGetBufferDeviceAddress>(get(device, "vkGetBufferDeviceAddress")); | ||
| 160 | return all && (!addressable || this->vkGetBufferDeviceAddress != nullptr); | ||
| 149 | } | 161 | } |
| 150 | }; | 162 | }; |
| 151 | 163 | ||
| 164 | // Hands out `bytes` from this submission's chunks, making a new chunk where none fits. | ||
| 165 | bool claim(std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset, VkDeviceAddress* address) { | ||
| 166 | constexpr VkDeviceSize alignment = 256, minimum = 4u << 20; | ||
| 167 | if (!loaded_ || !bytes) return false; | ||
| 168 | if (address && !addressable_) return false; | ||
| 169 | for (auto& chunk : current_.chunks) { | ||
| 170 | const VkDeviceSize rem = chunk.used % alignment; | ||
| 171 | const VkDeviceSize pad = rem ? alignment - rem : 0; | ||
| 172 | if (pad > chunk.size - chunk.used) continue; | ||
| 173 | const VkDeviceSize start = chunk.used + pad; | ||
| 174 | if (bytes <= chunk.size - start) { | ||
| 175 | chunk.used = start + bytes; | ||
| 176 | buffer = chunk.buffer; | ||
| 177 | offset = start; | ||
| 178 | if (address) *address = device_address(chunk.buffer) + start; | ||
| 179 | return true; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | Chunk chunk; | ||
| 183 | for (std::size_t i = 0; i < spare_.size(); ++i) | ||
| 184 | if (spare_[i].size >= bytes) { | ||
| 185 | chunk = spare_[i]; | ||
| 186 | spare_.erase(spare_.begin() + std::ptrdiff_t(i)); | ||
| 187 | break; | ||
| 188 | } | ||
| 189 | if (!chunk.buffer && !make_chunk(std::max<std::uint64_t>(bytes, minimum), chunk)) return false; | ||
| 190 | chunk.used = bytes; | ||
| 191 | buffer = chunk.buffer; | ||
| 192 | offset = 0; | ||
| 193 | current_.chunks.push_back(chunk); | ||
| 194 | if (address) *address = device_address(buffer); | ||
| 195 | return true; | ||
| 196 | } | ||
| 197 | |||
| 198 | VkDeviceAddress device_address(VkBuffer buffer) const { | ||
| 199 | VkBufferDeviceAddressInfo info{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO}; | ||
| 200 | info.buffer = buffer; | ||
| 201 | return vk_.vkGetBufferDeviceAddress(device_, &info); | ||
| 202 | } | ||
| 203 | |||
| 152 | bool make_chunk(std::uint64_t size, Chunk& out) { | 204 | bool make_chunk(std::uint64_t size, Chunk& out) { |
| 153 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | 205 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; |
| 154 | info.size = size; | 206 | info.size = size; |
| 155 | info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; | 207 | info.usage = usage_; |
| 208 | // A retired chunk can be reused by a submission on a different queue family. | ||
| 209 | share_across_queue_families(info, families_); | ||
| 156 | if (vk_.vkCreateBuffer(device_, &info, nullptr, &out.buffer) != VK_SUCCESS) return false; | 210 | if (vk_.vkCreateBuffer(device_, &info, nullptr, &out.buffer) != VK_SUCCESS) return false; |
| 157 | VkMemoryRequirements requirements{}; | 211 | VkMemoryRequirements requirements{}; |
| 158 | vk_.vkGetBufferMemoryRequirements(device_, out.buffer, &requirements); | 212 | vk_.vkGetBufferMemoryRequirements(device_, out.buffer, &requirements); |
| @@ -175,7 +229,11 @@ private: | |||
| 175 | } | 229 | } |
| 176 | 230 | ||
| 177 | VkDevice device_; | 231 | VkDevice device_; |
| 232 | DeviceFaultDiagnostics* diagnostics_; | ||
| 178 | Allocate allocate_; | 233 | Allocate allocate_; |
| 234 | VkBufferUsageFlags usage_ = transfer_usage; | ||
| 235 | std::vector<std::uint32_t> families_; | ||
| 236 | bool addressable_ = false; | ||
| 179 | Functions vk_; | 237 | Functions vk_; |
| 180 | bool loaded_ = false; | 238 | bool loaded_ = false; |
| 181 | Batch current_; // handed out, not yet submitted | 239 | Batch current_; // handed out, not yet submitted |