upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan_scratch.h
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-26 13:17:55 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-26 13:17:55 -0500
commitbbf861fbb4ea51ca18cd6f372797111984b6f8aa (patch)
tree81ac99a1a6d61841cca3562fab529cf38348c73f /runtime/src/vulkan_scratch.h
parent4645a3f62a7655ce69b930da70bdc1a858d1d0a1 (diff)
parentbdbc7e9236465c5d6a1305de6edd3fc09c2d6fd9 (diff)
Merge main into fix/depth16-quantise-fallback
runtime/CMakeLists.txt: main added the heavy_pipeline_cache, pipeline_cache_journal and compile_helper tests where this branch adds depth24_fragment; both sides are kept. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_scratch.h')
-rw-r--r--runtime/src/vulkan_scratch.h118
1 files changed, 81 insertions, 37 deletions
diff --git a/runtime/src/vulkan_scratch.h b/runtime/src/vulkan_scratch.h
index 753701e1c..958f27ca5 100644
--- a/runtime/src/vulkan_scratch.h
+++ b/runtime/src/vulkan_scratch.h
@@ -1,7 +1,8 @@
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.
6// The chunks are transfer sources and destinations; `extra_usage` adds what another user needs (the expanded indices 7// The chunks are transfer sources and destinations; `extra_usage` adds what another user needs (the expanded indices
7// of quad lists, quad_index_expand.h: an index buffer written through its device address). 8// of quad lists, quad_index_expand.h: an index buffer written through its device address).
@@ -27,12 +28,31 @@ class ScratchBuffers {
27public: 28public:
28 // A device-local allocation for a chunk; null on failure. 29 // A device-local allocation for a chunk; null on failure.
29 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;
30 34
35 // Legacy copy-scratch constructor. `extra_usage` is added to the transfer usage, and
36 // `families` keeps retired chunks shareable across queue families.
31 ScratchBuffers(PFN_vkGetDeviceProcAddr get, VkDevice device, Allocate allocate, VkBufferUsageFlags extra_usage = 0, 37 ScratchBuffers(PFN_vkGetDeviceProcAddr get, VkDevice device, Allocate allocate, VkBufferUsageFlags extra_usage = 0,
32 std::vector<std::uint32_t> families = {}, DeviceFaultDiagnostics* diagnostics = nullptr) 38 std::vector<std::uint32_t> families = {}, DeviceFaultDiagnostics* diagnostics = nullptr)
33 : device_(device), diagnostics_(diagnostics), allocate_(std::move(allocate)), extra_usage_(extra_usage), 39 : device_(device), diagnostics_(diagnostics), allocate_(std::move(allocate)),
34 families_(std::move(families)) { 40 usage_(transfer_usage | extra_usage), families_(std::move(families)) {
35 loaded_ = vk_.load(get, device); 41 loaded_ = vk_.load(get, device, false);
42 addressable_ = (usage_ & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) && vk_.vkGetBufferDeviceAddress;
43 }
44
45 // `usage` is what the chunks are created with. A chunk that declares VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT
46 // gets its device address from get() as well, and its memory must have been allocated with
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_);
36 } 56 }
37 57
38 ~ScratchBuffers() { 58 ~ScratchBuffers() {
@@ -48,36 +68,14 @@ public:
48 68
49 bool loaded() const { return loaded_; } 69 bool loaded() const { return loaded_; }
50 70
51 // `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).
52 bool get(std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset) { 72 bool get(std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset) {
53 constexpr VkDeviceSize alignment = 256, minimum = 4u << 20; 73 return claim(bytes, buffer, offset, nullptr);
54 if (!loaded_ || !bytes) return false; 74 }
55 for (auto& chunk : current_.chunks) { 75
56 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.
57 const VkDeviceSize pad = rem ? alignment - rem : 0; 77 bool get(std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset, VkDeviceAddress& address) {
58 if (pad <= chunk.size - chunk.used) { 78 return claim(bytes, buffer, offset, &address);
59 const VkDeviceSize start = chunk.used + pad;
60 if (bytes <= chunk.size - start) {
61 chunk.used = start + bytes;
62 buffer = chunk.buffer;
63 offset = start;
64 return true;
65 }
66 }
67 }
68 Chunk chunk;
69 for (std::size_t i = 0; i < spare_.size(); ++i)
70 if (spare_[i].size >= bytes) {
71 chunk = spare_[i];
72 spare_.erase(spare_.begin() + std::ptrdiff_t(i));
73 break;
74 }
75 if (!chunk.buffer && !make_chunk(std::max<std::uint64_t>(bytes, minimum), chunk)) return false;
76 chunk.used = bytes;
77 buffer = chunk.buffer;
78 offset = 0;
79 current_.chunks.push_back(chunk);
80 return true;
81 } 79 }
82 80
83 // 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
@@ -145,8 +143,9 @@ private:
145#define LEONIDA_SCRATCH_MEMBER(name) PFN_##name name = nullptr; 143#define LEONIDA_SCRATCH_MEMBER(name) PFN_##name name = nullptr;
146 LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_MEMBER) 144 LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_MEMBER)
147#undef LEONIDA_SCRATCH_MEMBER 145#undef LEONIDA_SCRATCH_MEMBER
146 PFN_vkGetBufferDeviceAddress vkGetBufferDeviceAddress = nullptr; // optional: addressable pools only
148 147
149 bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { 148 bool load(PFN_vkGetDeviceProcAddr get, VkDevice device, bool addressable) {
150 bool all = true; 149 bool all = true;
151#define LEONIDA_SCRATCH_LOAD(name) \ 150#define LEONIDA_SCRATCH_LOAD(name) \
152 name = reinterpret_cast<PFN_##name>(get(device, #name)); \ 151 name = reinterpret_cast<PFN_##name>(get(device, #name)); \
@@ -154,14 +153,58 @@ private:
154 LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_LOAD) 153 LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_LOAD)
155#undef LEONIDA_SCRATCH_LOAD 154#undef LEONIDA_SCRATCH_LOAD
156#undef LEONIDA_SCRATCH_FUNCTIONS 155#undef LEONIDA_SCRATCH_FUNCTIONS
157 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);
158 } 161 }
159 }; 162 };
160 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
161 bool make_chunk(std::uint64_t size, Chunk& out) { 204 bool make_chunk(std::uint64_t size, Chunk& out) {
162 VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; 205 VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
163 info.size = size; 206 info.size = size;
164 info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | extra_usage_; 207 info.usage = usage_;
165 // A retired chunk can be reused by a submission on a different queue family. 208 // A retired chunk can be reused by a submission on a different queue family.
166 share_across_queue_families(info, families_); 209 share_across_queue_families(info, families_);
167 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;
@@ -188,8 +231,9 @@ private:
188 VkDevice device_; 231 VkDevice device_;
189 DeviceFaultDiagnostics* diagnostics_; 232 DeviceFaultDiagnostics* diagnostics_;
190 Allocate allocate_; 233 Allocate allocate_;
191 VkBufferUsageFlags extra_usage_ = 0; 234 VkBufferUsageFlags usage_ = transfer_usage;
192 std::vector<std::uint32_t> families_; 235 std::vector<std::uint32_t> families_;
236 bool addressable_ = false;
193 Functions vk_; 237 Functions vk_;
194 bool loaded_ = false; 238 bool loaded_ = false;
195 Batch current_; // handed out, not yet submitted 239 Batch current_; // handed out, not yet submitted