diff options
Diffstat (limited to 'runtime/src/vulkan_scratch.h')
| -rw-r--r-- | runtime/src/vulkan_scratch.h | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/runtime/src/vulkan_scratch.h b/runtime/src/vulkan_scratch.h new file mode 100644 index 000000000..fe29d1a5c --- /dev/null +++ b/runtime/src/vulkan_scratch.h | |||
| @@ -0,0 +1,179 @@ | |||
| 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 | ||
| 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 | ||
| 5 | // scratch (vulkan_depth24.h), which also needs descriptor pools and exists only on GPUs without D24. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #ifndef VK_NO_PROTOTYPES | ||
| 9 | #define VK_NO_PROTOTYPES | ||
| 10 | #endif | ||
| 11 | #include <vulkan/vulkan.h> | ||
| 12 | |||
| 13 | #include <algorithm> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <functional> | ||
| 16 | #include <utility> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace leonida { | ||
| 20 | |||
| 21 | class ScratchBuffers { | ||
| 22 | public: | ||
| 23 | // A device-local allocation for a chunk; null on failure. | ||
| 24 | using Allocate = std::function<VkDeviceMemory(const VkMemoryRequirements&)>; | ||
| 25 | |||
| 26 | ScratchBuffers(PFN_vkGetDeviceProcAddr get, VkDevice device, Allocate allocate) | ||
| 27 | : device_(device), allocate_(std::move(allocate)) { | ||
| 28 | loaded_ = vk_.load(get, device); | ||
| 29 | } | ||
| 30 | |||
| 31 | ~ScratchBuffers() { | ||
| 32 | if (!loaded_) return; | ||
| 33 | vk_.vkDeviceWaitIdle(device_); | ||
| 34 | release(current_); | ||
| 35 | for (auto& batch : retired_) release(batch); | ||
| 36 | for (auto& chunk : spare_) destroy(chunk); | ||
| 37 | } | ||
| 38 | |||
| 39 | ScratchBuffers(const ScratchBuffers&) = delete; | ||
| 40 | ScratchBuffers& operator=(const ScratchBuffers&) = delete; | ||
| 41 | |||
| 42 | bool loaded() const { return loaded_; } | ||
| 43 | |||
| 44 | // `bytes` of transfer 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) { | ||
| 46 | constexpr VkDeviceSize alignment = 256, minimum = 4u << 20; | ||
| 47 | if (!loaded_ || !bytes) return false; | ||
| 48 | for (auto& chunk : current_.chunks) { | ||
| 49 | const VkDeviceSize start = (chunk.used + alignment - 1) / alignment * alignment; | ||
| 50 | if (start <= chunk.size && bytes <= chunk.size - start) { | ||
| 51 | chunk.used = start + bytes; | ||
| 52 | buffer = chunk.buffer; | ||
| 53 | offset = start; | ||
| 54 | return true; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | Chunk chunk; | ||
| 58 | for (std::size_t i = 0; i < spare_.size(); ++i) | ||
| 59 | if (spare_[i].size >= bytes) { | ||
| 60 | chunk = spare_[i]; | ||
| 61 | spare_.erase(spare_.begin() + std::ptrdiff_t(i)); | ||
| 62 | break; | ||
| 63 | } | ||
| 64 | if (!chunk.buffer && !make_chunk(std::max<std::uint64_t>(bytes, minimum), chunk)) return false; | ||
| 65 | chunk.used = bytes; | ||
| 66 | buffer = chunk.buffer; | ||
| 67 | offset = 0; | ||
| 68 | current_.chunks.push_back(chunk); | ||
| 69 | return true; | ||
| 70 | } | ||
| 71 | |||
| 72 | // The chunks handed out since the last call belong to a submission that `timeline` reaches `ticket` for | ||
| 73 | // (ticket 0: nothing was submitted, they are free once `collect` runs). | ||
| 74 | void retire(VkSemaphore timeline, std::uint64_t ticket) { | ||
| 75 | if (current_.chunks.empty()) return; | ||
| 76 | current_.timeline = timeline; | ||
| 77 | current_.ticket = ticket; | ||
| 78 | retired_.push_back(std::move(current_)); | ||
| 79 | current_ = {}; | ||
| 80 | } | ||
| 81 | |||
| 82 | // The submissions on `timeline` are over and the semaphore is about to go (its queue is idle). | ||
| 83 | void forget(VkSemaphore timeline) { | ||
| 84 | for (std::size_t i = 0; i < retired_.size();) { | ||
| 85 | if (retired_[i].timeline != timeline) { | ||
| 86 | ++i; | ||
| 87 | continue; | ||
| 88 | } | ||
| 89 | release(retired_[i]); | ||
| 90 | retired_.erase(retired_.begin() + std::ptrdiff_t(i)); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | // Keeps for reuse (or frees) the chunks of finished submissions. | ||
| 95 | void collect() { | ||
| 96 | for (std::size_t i = 0; i < retired_.size();) { | ||
| 97 | std::uint64_t done = 0; | ||
| 98 | if (retired_[i].timeline) vk_.vkGetSemaphoreCounterValue(device_, retired_[i].timeline, &done); | ||
| 99 | if (done < retired_[i].ticket) { | ||
| 100 | ++i; | ||
| 101 | continue; | ||
| 102 | } | ||
| 103 | for (auto& chunk : retired_[i].chunks) { | ||
| 104 | chunk.used = 0; | ||
| 105 | if (spare_.size() < 4) spare_.push_back(chunk); | ||
| 106 | else destroy(chunk); | ||
| 107 | } | ||
| 108 | retired_[i].chunks.clear(); | ||
| 109 | retired_.erase(retired_.begin() + std::ptrdiff_t(i)); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | private: | ||
| 114 | struct Chunk { | ||
| 115 | VkBuffer buffer = VK_NULL_HANDLE; | ||
| 116 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 117 | VkDeviceSize size = 0, used = 0; | ||
| 118 | }; | ||
| 119 | struct Batch { | ||
| 120 | std::vector<Chunk> chunks; | ||
| 121 | VkSemaphore timeline = VK_NULL_HANDLE; | ||
| 122 | std::uint64_t ticket = 0; | ||
| 123 | }; | ||
| 124 | |||
| 125 | struct Functions { | ||
| 126 | #define LEONIDA_SCRATCH_FUNCTIONS(X) \ | ||
| 127 | X(vkCreateBuffer) X(vkDestroyBuffer) X(vkGetBufferMemoryRequirements) X(vkBindBufferMemory) X(vkFreeMemory) \ | ||
| 128 | X(vkGetSemaphoreCounterValue) X(vkDeviceWaitIdle) | ||
| 129 | #define LEONIDA_SCRATCH_MEMBER(name) PFN_##name name = nullptr; | ||
| 130 | LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_MEMBER) | ||
| 131 | #undef LEONIDA_SCRATCH_MEMBER | ||
| 132 | |||
| 133 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | ||
| 134 | bool all = true; | ||
| 135 | #define LEONIDA_SCRATCH_LOAD(name) \ | ||
| 136 | name = reinterpret_cast<PFN_##name>(get(device, #name)); \ | ||
| 137 | all = all && name != nullptr; | ||
| 138 | LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_LOAD) | ||
| 139 | #undef LEONIDA_SCRATCH_LOAD | ||
| 140 | #undef LEONIDA_SCRATCH_FUNCTIONS | ||
| 141 | return all; | ||
| 142 | } | ||
| 143 | }; | ||
| 144 | |||
| 145 | bool make_chunk(std::uint64_t size, Chunk& out) { | ||
| 146 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 147 | info.size = size; | ||
| 148 | info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; | ||
| 149 | if (vk_.vkCreateBuffer(device_, &info, nullptr, &out.buffer) != VK_SUCCESS) return false; | ||
| 150 | VkMemoryRequirements requirements{}; | ||
| 151 | vk_.vkGetBufferMemoryRequirements(device_, out.buffer, &requirements); | ||
| 152 | out.memory = allocate_ ? allocate_(requirements) : VK_NULL_HANDLE; | ||
| 153 | if (!out.memory || vk_.vkBindBufferMemory(device_, out.buffer, out.memory, 0) != VK_SUCCESS) { | ||
| 154 | destroy(out); | ||
| 155 | return false; | ||
| 156 | } | ||
| 157 | out.size = size; | ||
| 158 | return true; | ||
| 159 | } | ||
| 160 | void destroy(Chunk& chunk) { | ||
| 161 | if (chunk.buffer) vk_.vkDestroyBuffer(device_, chunk.buffer, nullptr); | ||
| 162 | if (chunk.memory) vk_.vkFreeMemory(device_, chunk.memory, nullptr); | ||
| 163 | chunk = {}; | ||
| 164 | } | ||
| 165 | void release(Batch& batch) { | ||
| 166 | for (auto& chunk : batch.chunks) destroy(chunk); | ||
| 167 | batch.chunks.clear(); | ||
| 168 | } | ||
| 169 | |||
| 170 | VkDevice device_; | ||
| 171 | Allocate allocate_; | ||
| 172 | Functions vk_; | ||
| 173 | bool loaded_ = false; | ||
| 174 | Batch current_; // handed out, not yet submitted | ||
| 175 | std::vector<Batch> retired_; // submitted, waiting for the GPU | ||
| 176 | std::vector<Chunk> spare_; // finished chunks kept for reuse | ||
| 177 | }; | ||
| 178 | |||
| 179 | } // namespace leonida | ||