diff options
Diffstat (limited to 'runtime/src/vulkan_bindless.h')
| -rw-r--r-- | runtime/src/vulkan_bindless.h | 579 |
1 files changed, 579 insertions, 0 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h new file mode 100644 index 000000000..8cb895adb --- /dev/null +++ b/runtime/src/vulkan_bindless.h | |||
| @@ -0,0 +1,579 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // The bindless descriptor set of translated shaders: set 0 of the contract in docs/design.md and | ||
| 4 | // docs/d3d12_vulkan.md section 5, as tools/rdna2spirv.py declares it. | ||
| 5 | // | ||
| 6 | // binding 0 sampled 2D images 4096 5 sampled 1D images 256 | ||
| 7 | // 1 samplers 1024 6 sampled 3D images 256 | ||
| 8 | // 2 the descriptor hash table 1 7 3D r32ui storage images 256 | ||
| 9 | // 3 sampled 2D-array images 1024 8 + 3 * dimension + class 256 each: | ||
| 10 | // 4 2D r32ui storage images 1024 storage images by component class | ||
| 11 | // dimension: 0 = 2D, 1 = 2D array, 2 = 3D, 3 = 1D (the translator's STORE_DIMS); class: 0 = float, 1 = uint, 2 = sint. | ||
| 12 | // The counts are those of runtime/test/vk_pipeline_check.cpp, which creates every translated shader against them. | ||
| 13 | // | ||
| 14 | // A shader turns a T# or S# into an index through the hash table (descriptor_table.h) and uses that index in | ||
| 15 | // whichever binding the instruction calls for: the sampled array of the image's dimension, or one of its storage | ||
| 16 | // arrays. So an image has one index, the same in every binding of its dimension, and each dimension has its own | ||
| 17 | // index space. The storage arrays hold 256, so an image with storage views gets an index below 256; one without | ||
| 18 | // takes a higher index while there is one. Index 0 of every array is a fallback, because a miss gives 0: a 1x1 | ||
| 19 | // image of zeros, a nearest clamping sampler, and 1x1 storage images. The caller owns the views and samplers it | ||
| 20 | // adds and keeps them alive while they are in the set; this class owns only the fallbacks and the table's buffer. | ||
| 21 | // | ||
| 22 | // Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind | ||
| 23 | // (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while | ||
| 24 | // command buffers that use the set are pending, as long as no descriptor they use changes and flush_needs_idle() | ||
| 25 | // is false; without it the caller flushes only when none is. With partially_bound (descriptorBindingPartiallyBound) only index 0 is written at | ||
| 26 | // creation; otherwise every element of every array starts as the fallback. | ||
| 27 | // | ||
| 28 | // Not thread-safe: the owner serialises calls. | ||
| 29 | |||
| 30 | #ifndef VK_NO_PROTOTYPES | ||
| 31 | #define VK_NO_PROTOTYPES | ||
| 32 | #endif | ||
| 33 | #include "descriptor_table.h" | ||
| 34 | |||
| 35 | #include <vulkan/vulkan.h> | ||
| 36 | |||
| 37 | #include <algorithm> | ||
| 38 | #include <array> | ||
| 39 | #include <cstdint> | ||
| 40 | #include <cstring> | ||
| 41 | #include <unordered_map> | ||
| 42 | #include <vector> | ||
| 43 | |||
| 44 | namespace leonida { | ||
| 45 | |||
| 46 | // The translator's image dimensions; the values are its STORE_DIMS indices. A cube is a 2D array of six layers. | ||
| 47 | enum class ImageDimension : std::uint32_t { dim_2d = 0, dim_2d_array = 1, dim_3d = 2, dim_1d = 3 }; | ||
| 48 | |||
| 49 | // Storage views of one image by the component type a shader reads or writes it with (the translator picks by the | ||
| 50 | // T# format at run time). r32ui is for the atomic instructions, whose image type is R32ui (2D and 3D only). | ||
| 51 | // A null view leaves the fallback in that binding. | ||
| 52 | struct StorageViews { | ||
| 53 | VkImageView as_float = VK_NULL_HANDLE; | ||
| 54 | VkImageView as_uint = VK_NULL_HANDLE; | ||
| 55 | VkImageView as_sint = VK_NULL_HANDLE; | ||
| 56 | VkImageView r32ui = VK_NULL_HANDLE; | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct BindlessFeatures { | ||
| 60 | bool update_after_bind = false; | ||
| 61 | bool partially_bound = false; | ||
| 62 | }; | ||
| 63 | |||
| 64 | namespace bindless { | ||
| 65 | |||
| 66 | struct Binding { | ||
| 67 | VkDescriptorType type; | ||
| 68 | std::uint32_t count; | ||
| 69 | }; | ||
| 70 | |||
| 71 | inline constexpr std::uint32_t table_binding = 2; | ||
| 72 | inline constexpr std::uint32_t sampler_binding = 1; | ||
| 73 | inline constexpr std::uint32_t storage_indices = 256; // what every storage array of bindings 8-19 holds | ||
| 74 | inline constexpr std::uint32_t no_binding = 0xffffffffu; | ||
| 75 | |||
| 76 | // Indexed by binding number. | ||
| 77 | inline constexpr std::array<Binding, 20> bindings = {{ | ||
| 78 | {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4096}, {VK_DESCRIPTOR_TYPE_SAMPLER, 1024}, | ||
| 79 | {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024}, | ||
| 80 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, | ||
| 81 | {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 82 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 83 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 84 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 85 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 86 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 87 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 88 | }}; | ||
| 89 | |||
| 90 | constexpr std::uint32_t sampled_binding(ImageDimension dimension) { | ||
| 91 | constexpr std::uint32_t by_dimension[] = {0, 3, 6, 5}; | ||
| 92 | return by_dimension[std::uint32_t(dimension)]; | ||
| 93 | } | ||
| 94 | |||
| 95 | // component_class: 0 = float, 1 = uint, 2 = sint. | ||
| 96 | constexpr std::uint32_t storage_binding(ImageDimension dimension, std::uint32_t component_class) { | ||
| 97 | return 8 + 3 * std::uint32_t(dimension) + component_class; | ||
| 98 | } | ||
| 99 | |||
| 100 | constexpr std::uint32_t r32ui_binding(ImageDimension dimension) { | ||
| 101 | return dimension == ImageDimension::dim_2d ? 4 : dimension == ImageDimension::dim_3d ? 7 : no_binding; | ||
| 102 | } | ||
| 103 | |||
| 104 | // How many images of a dimension the set holds, the fallback included. | ||
| 105 | constexpr std::uint32_t image_capacity(ImageDimension dimension) { return bindings[sampled_binding(dimension)].count; } | ||
| 106 | |||
| 107 | } // namespace bindless | ||
| 108 | |||
| 109 | class BindlessSet { | ||
| 110 | public: | ||
| 111 | BindlessSet(PFN_vkGetDeviceProcAddr get, VkDevice device, const VkPhysicalDeviceMemoryProperties& memory, | ||
| 112 | BindlessFeatures features = {}) | ||
| 113 | : device_(device), memory_(memory), features_(features) { | ||
| 114 | ok_ = vk_.load(get, device) && create_layout() && create_fallbacks() && create_table_buffer() && fill(); | ||
| 115 | } | ||
| 116 | |||
| 117 | ~BindlessSet() { | ||
| 118 | release_retired(); | ||
| 119 | if (table_buffer_.buffer) destroy(table_buffer_); | ||
| 120 | if (fallback_sampler_) vk_.vkDestroySampler(device_, fallback_sampler_, nullptr); | ||
| 121 | for (const VkImageView view : fallback_views_) vk_.vkDestroyImageView(device_, view, nullptr); | ||
| 122 | for (const auto& image : fallback_images_) { | ||
| 123 | if (image.image) vk_.vkDestroyImage(device_, image.image, nullptr); | ||
| 124 | if (image.memory) vk_.vkFreeMemory(device_, image.memory, nullptr); | ||
| 125 | } | ||
| 126 | if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); // frees the set | ||
| 127 | if (layout_) vk_.vkDestroyDescriptorSetLayout(device_, layout_, nullptr); | ||
| 128 | } | ||
| 129 | |||
| 130 | BindlessSet(const BindlessSet&) = delete; | ||
| 131 | BindlessSet& operator=(const BindlessSet&) = delete; | ||
| 132 | |||
| 133 | bool ok() const { return ok_; } | ||
| 134 | // Set 0 of every pipeline layout made for translated shaders. | ||
| 135 | VkDescriptorSetLayout layout() const { return layout_; } | ||
| 136 | // Bound at set 0. The same set for the life of this object: flush() updates it in place. | ||
| 137 | VkDescriptorSet set() const { return set_; } | ||
| 138 | |||
| 139 | // The fallback images are created with undefined contents: record this once, in a command buffer that executes | ||
| 140 | // before anything uses the set. It moves them to GENERAL, the layout their descriptors name, and zeroes them. | ||
| 141 | bool fallbacks_ready() const { return fallbacks_ready_; } | ||
| 142 | void record_fallback_init(VkCommandBuffer command_buffer) { | ||
| 143 | fallbacks_ready_ = true; | ||
| 144 | std::vector<VkImageMemoryBarrier> barriers; | ||
| 145 | for (const auto& image : fallback_images_) { | ||
| 146 | VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; | ||
| 147 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 148 | barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; | ||
| 149 | barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL; | ||
| 150 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 151 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 152 | barrier.image = image.image; | ||
| 153 | barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 154 | barriers.push_back(barrier); | ||
| 155 | } | ||
| 156 | vk_.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, | ||
| 157 | nullptr, 0, nullptr, std::uint32_t(barriers.size()), barriers.data()); | ||
| 158 | const VkClearColorValue zero{}; | ||
| 159 | const VkImageSubresourceRange whole{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 160 | for (const auto& image : fallback_images_) | ||
| 161 | vk_.vkCmdClearColorImage(command_buffer, image.image, VK_IMAGE_LAYOUT_GENERAL, &zero, 1, &whole); | ||
| 162 | VkMemoryBarrier done{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 163 | done.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 164 | done.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 165 | vk_.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, | ||
| 166 | &done, 0, nullptr, 0, nullptr); | ||
| 167 | } | ||
| 168 | |||
| 169 | // Gives an image an index in its dimension's bindings: 0 when the dimension is full. `sampled` goes to the | ||
| 170 | // sampled array in `sampled_layout` (the layout the image is in when shaders read it); the storage views go to | ||
| 171 | // the storage arrays in GENERAL. The caller maps the T# to the index with insert_key(). Takes effect at flush(). | ||
| 172 | std::uint32_t add_image(ImageDimension dimension, VkImageView sampled, VkImageLayout sampled_layout, | ||
| 173 | const StorageViews& storage = {}) { | ||
| 174 | const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui; | ||
| 175 | auto& used = image_used_[std::uint32_t(dimension)]; | ||
| 176 | const std::uint32_t low_end = std::min(std::uint32_t(used.size()), bindless::storage_indices); | ||
| 177 | std::uint32_t index = stores ? 0 : allocate(used, low_end, std::uint32_t(used.size())); | ||
| 178 | if (!index) index = allocate(used, 1, low_end); | ||
| 179 | if (index) write_image(dimension, index, sampled, sampled_layout, storage); | ||
| 180 | return index; | ||
| 181 | } | ||
| 182 | |||
| 183 | // New views or a new layout for an index add_image returned (a texture's state changed, say). | ||
| 184 | void update_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout, | ||
| 185 | const StorageViews& storage = {}) { | ||
| 186 | const auto& used = image_used_[std::uint32_t(dimension)]; | ||
| 187 | if (index && index < used.size() && used[index]) write_image(dimension, index, sampled, sampled_layout, storage); | ||
| 188 | } | ||
| 189 | |||
| 190 | // Returns the index to the fallback and frees it. The caller removes the image's keys with erase_key() first. | ||
| 191 | void remove_image(ImageDimension dimension, std::uint32_t index) { | ||
| 192 | auto& used = image_used_[std::uint32_t(dimension)]; | ||
| 193 | if (!index || index >= used.size() || !used[index]) return; | ||
| 194 | write_image(dimension, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}); | ||
| 195 | used[index] = 0; | ||
| 196 | } | ||
| 197 | |||
| 198 | // 0 when the sampler array is full. | ||
| 199 | std::uint32_t add_sampler(VkSampler sampler) { | ||
| 200 | const std::uint32_t index = allocate(sampler_used_, 1, std::uint32_t(sampler_used_.size())); | ||
| 201 | if (index) write_sampler(index, sampler); | ||
| 202 | return index; | ||
| 203 | } | ||
| 204 | |||
| 205 | void remove_sampler(std::uint32_t index) { | ||
| 206 | if (!index || index >= sampler_used_.size() || !sampler_used_[index]) return; | ||
| 207 | write_sampler(index, fallback_sampler_); | ||
| 208 | sampler_used_[index] = 0; | ||
| 209 | } | ||
| 210 | |||
| 211 | // T# and S# keys to the indices above: the table of binding 2 (descriptor_table.h). Changes reach the shaders | ||
| 212 | // at flush(). | ||
| 213 | bool insert_key(const DescriptorKey& key, std::uint32_t index) { | ||
| 214 | table_dirty_ = true; | ||
| 215 | return table_.insert(key, index); | ||
| 216 | } | ||
| 217 | // Erasing compacts the key's probe cluster, so other keys' entries may change slots. | ||
| 218 | bool erase_key(const DescriptorKey& key) { | ||
| 219 | if (!table_.erase(key)) return false; | ||
| 220 | table_dirty_ = entries_moved_ = true; | ||
| 221 | return true; | ||
| 222 | } | ||
| 223 | const DescriptorTable& table() const { return table_; } | ||
| 224 | |||
| 225 | // True when the next flush() will move entries a running shader may be probing, or replace the table's | ||
| 226 | // buffer: the caller then flushes only once no command buffer that uses the set is still executing. Without | ||
| 227 | // it the flush only adds entries to the buffer in place, which a running lookup cannot see half done. | ||
| 228 | bool flush_needs_idle() const { | ||
| 229 | return table_dirty_ && (entries_moved_ || table_.size_bytes() > table_buffer_.size); | ||
| 230 | } | ||
| 231 | |||
| 232 | // Writes the pending descriptor changes and the table: before the commands that use the set are executed. | ||
| 233 | // A table that has grown gets a new buffer (binding 2 is rewritten); the old one is kept until | ||
| 234 | // release_retired(). False when that buffer cannot be made: the set then still has the table as of the last | ||
| 235 | // flush. | ||
| 236 | bool flush() { | ||
| 237 | bool written = true; | ||
| 238 | if (table_dirty_) { | ||
| 239 | const std::vector<std::uint32_t> words = table_.build_gpu_buffer(); | ||
| 240 | const VkDeviceSize bytes = words.size() * sizeof(std::uint32_t); | ||
| 241 | if (bytes > table_buffer_.size) { | ||
| 242 | TableBuffer larger; | ||
| 243 | if (create(larger, bytes)) { | ||
| 244 | retired_.push_back(table_buffer_); | ||
| 245 | table_buffer_ = larger; | ||
| 246 | write_table_binding(); | ||
| 247 | } else { | ||
| 248 | written = false; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | if (written) { | ||
| 252 | std::memcpy(table_buffer_.mapped, words.data(), bytes); | ||
| 253 | table_dirty_ = entries_moved_ = false; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | if (!pending_.empty()) { | ||
| 257 | std::vector<VkWriteDescriptorSet> writes(pending_.size()); | ||
| 258 | for (std::size_t i = 0; i < pending_.size(); ++i) { | ||
| 259 | writes[i] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 260 | writes[i].dstSet = set_; | ||
| 261 | writes[i].dstBinding = pending_[i].binding; | ||
| 262 | writes[i].dstArrayElement = pending_[i].index; | ||
| 263 | writes[i].descriptorCount = 1; | ||
| 264 | writes[i].descriptorType = bindless::bindings[pending_[i].binding].type; | ||
| 265 | writes[i].pImageInfo = &pending_[i].info; | ||
| 266 | } | ||
| 267 | vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); | ||
| 268 | pending_.clear(); | ||
| 269 | pending_slots_.clear(); | ||
| 270 | } | ||
| 271 | return written; | ||
| 272 | } | ||
| 273 | |||
| 274 | // Frees the buffers of tables that have since grown: when nothing submitted before the last flush() still runs. | ||
| 275 | void release_retired() { | ||
| 276 | for (auto& buffer : retired_) destroy(buffer); | ||
| 277 | retired_.clear(); | ||
| 278 | } | ||
| 279 | |||
| 280 | private: | ||
| 281 | struct Functions { | ||
| 282 | #define LEONIDA_BINDLESS_FUNCTIONS(X) \ | ||
| 283 | X(vkCreateDescriptorSetLayout) X(vkDestroyDescriptorSetLayout) X(vkCreateDescriptorPool) \ | ||
| 284 | X(vkDestroyDescriptorPool) X(vkAllocateDescriptorSets) X(vkUpdateDescriptorSets) X(vkCreateBuffer) \ | ||
| 285 | X(vkDestroyBuffer) X(vkGetBufferMemoryRequirements) X(vkBindBufferMemory) X(vkAllocateMemory) \ | ||
| 286 | X(vkFreeMemory) X(vkMapMemory) X(vkCreateImage) X(vkDestroyImage) X(vkGetImageMemoryRequirements) \ | ||
| 287 | X(vkBindImageMemory) X(vkCreateImageView) X(vkDestroyImageView) X(vkCreateSampler) X(vkDestroySampler) \ | ||
| 288 | X(vkCmdPipelineBarrier) X(vkCmdClearColorImage) | ||
| 289 | #define LEONIDA_BINDLESS_MEMBER(name) PFN_##name name = nullptr; | ||
| 290 | LEONIDA_BINDLESS_FUNCTIONS(LEONIDA_BINDLESS_MEMBER) | ||
| 291 | #undef LEONIDA_BINDLESS_MEMBER | ||
| 292 | |||
| 293 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | ||
| 294 | bool all = true; | ||
| 295 | #define LEONIDA_BINDLESS_LOAD(name) \ | ||
| 296 | name = reinterpret_cast<PFN_##name>(get(device, #name)); \ | ||
| 297 | all = all && name != nullptr; | ||
| 298 | LEONIDA_BINDLESS_FUNCTIONS(LEONIDA_BINDLESS_LOAD) | ||
| 299 | #undef LEONIDA_BINDLESS_LOAD | ||
| 300 | #undef LEONIDA_BINDLESS_FUNCTIONS | ||
| 301 | return all; | ||
| 302 | } | ||
| 303 | }; | ||
| 304 | |||
| 305 | struct FallbackImage { | ||
| 306 | VkImage image = VK_NULL_HANDLE; | ||
| 307 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 308 | }; | ||
| 309 | |||
| 310 | struct TableBuffer { | ||
| 311 | VkBuffer buffer = VK_NULL_HANDLE; | ||
| 312 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 313 | void* mapped = nullptr; | ||
| 314 | std::size_t size = 0; | ||
| 315 | }; | ||
| 316 | |||
| 317 | struct Pending { | ||
| 318 | std::uint32_t binding; | ||
| 319 | std::uint32_t index; | ||
| 320 | VkDescriptorImageInfo info; | ||
| 321 | }; | ||
| 322 | |||
| 323 | static constexpr std::uint32_t dimensions = 4, classes = 3; | ||
| 324 | |||
| 325 | // The first free index in [begin, end), marked used; 0 when there is none. | ||
| 326 | static std::uint32_t allocate(std::vector<std::uint8_t>& used, std::uint32_t begin, std::uint32_t end) { | ||
| 327 | for (std::uint32_t i = begin; i < end; ++i) { | ||
| 328 | if (used[i]) continue; | ||
| 329 | used[i] = 1; | ||
| 330 | return i; | ||
| 331 | } | ||
| 332 | return 0; | ||
| 333 | } | ||
| 334 | |||
| 335 | std::uint32_t memory_type(std::uint32_t allowed, VkMemoryPropertyFlags wanted) const { | ||
| 336 | for (std::uint32_t i = 0; i < memory_.memoryTypeCount; ++i) | ||
| 337 | if ((allowed & (1u << i)) && (memory_.memoryTypes[i].propertyFlags & wanted) == wanted) return i; | ||
| 338 | return 0xffffffffu; | ||
| 339 | } | ||
| 340 | |||
| 341 | bool create_layout() { | ||
| 342 | std::array<VkDescriptorSetLayoutBinding, bindless::bindings.size()> entries{}; | ||
| 343 | std::array<VkDescriptorBindingFlags, bindless::bindings.size()> flags{}; | ||
| 344 | std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{}; | ||
| 345 | for (std::uint32_t b = 0; b < entries.size(); ++b) { | ||
| 346 | entries[b] = {b, bindless::bindings[b].type, bindless::bindings[b].count, VK_SHADER_STAGE_ALL, nullptr}; | ||
| 347 | flags[b] = (features_.update_after_bind ? VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT : 0) | | ||
| 348 | (features_.partially_bound ? VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT : 0); | ||
| 349 | sizes[b] = {bindless::bindings[b].type, bindless::bindings[b].count}; | ||
| 350 | } | ||
| 351 | VkDescriptorSetLayoutBindingFlagsCreateInfo flags_info{ | ||
| 352 | VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO}; | ||
| 353 | flags_info.bindingCount = std::uint32_t(flags.size()); | ||
| 354 | flags_info.pBindingFlags = flags.data(); | ||
| 355 | VkDescriptorSetLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, &flags_info}; | ||
| 356 | layout_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT : 0; | ||
| 357 | layout_info.bindingCount = std::uint32_t(entries.size()); | ||
| 358 | layout_info.pBindings = entries.data(); | ||
| 359 | if (vk_.vkCreateDescriptorSetLayout(device_, &layout_info, nullptr, &layout_) != VK_SUCCESS) return false; | ||
| 360 | |||
| 361 | VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; | ||
| 362 | pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0; | ||
| 363 | pool_info.maxSets = 1; | ||
| 364 | pool_info.poolSizeCount = std::uint32_t(sizes.size()); | ||
| 365 | pool_info.pPoolSizes = sizes.data(); | ||
| 366 | if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool_) != VK_SUCCESS) return false; | ||
| 367 | VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; | ||
| 368 | set_info.descriptorPool = pool_; | ||
| 369 | set_info.descriptorSetCount = 1; | ||
| 370 | set_info.pSetLayouts = &layout_; | ||
| 371 | if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set_) != VK_SUCCESS) return false; | ||
| 372 | |||
| 373 | for (std::uint32_t d = 0; d < dimensions; ++d) { | ||
| 374 | image_used_[d].assign(bindless::image_capacity(ImageDimension(d)), 0); | ||
| 375 | image_used_[d][0] = 1; | ||
| 376 | } | ||
| 377 | sampler_used_.assign(bindless::bindings[bindless::sampler_binding].count, 0); | ||
| 378 | sampler_used_[0] = 1; | ||
| 379 | return true; | ||
| 380 | } | ||
| 381 | |||
| 382 | // One 1x1 image for each image type and component class, and the views of them every binding needs. | ||
| 383 | bool create_fallbacks() { | ||
| 384 | constexpr VkImageType types[] = {VK_IMAGE_TYPE_2D, VK_IMAGE_TYPE_3D, VK_IMAGE_TYPE_1D}; | ||
| 385 | constexpr VkFormat formats[classes] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_UINT, VK_FORMAT_R32_SINT}; | ||
| 386 | // dimension -> (index into types, view type) | ||
| 387 | constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2}; | ||
| 388 | constexpr VkImageViewType view_types[dimensions] = {VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY, | ||
| 389 | VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D}; | ||
| 390 | for (std::uint32_t t = 0; t < 3; ++t) { | ||
| 391 | for (std::uint32_t c = 0; c < classes; ++c) { | ||
| 392 | VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; | ||
| 393 | info.imageType = types[t]; | ||
| 394 | info.format = formats[c]; | ||
| 395 | info.extent = {1, 1, 1}; | ||
| 396 | info.mipLevels = 1; | ||
| 397 | info.arrayLayers = 1; | ||
| 398 | info.samples = VK_SAMPLE_COUNT_1_BIT; | ||
| 399 | info.usage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | | ||
| 400 | (c == 0 ? VK_IMAGE_USAGE_SAMPLED_BIT : 0); | ||
| 401 | FallbackImage& image = fallback_images_[t * classes + c]; | ||
| 402 | if (vk_.vkCreateImage(device_, &info, nullptr, &image.image) != VK_SUCCESS) return false; | ||
| 403 | VkMemoryRequirements requirements{}; | ||
| 404 | vk_.vkGetImageMemoryRequirements(device_, image.image, &requirements); | ||
| 405 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 406 | allocation.allocationSize = requirements.size; | ||
| 407 | allocation.memoryTypeIndex = memory_type(requirements.memoryTypeBits, 0); | ||
| 408 | if (allocation.memoryTypeIndex == 0xffffffffu || | ||
| 409 | vk_.vkAllocateMemory(device_, &allocation, nullptr, &image.memory) != VK_SUCCESS || | ||
| 410 | vk_.vkBindImageMemory(device_, image.image, image.memory, 0) != VK_SUCCESS) | ||
| 411 | return false; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | for (std::uint32_t d = 0; d < dimensions; ++d) { | ||
| 415 | for (std::uint32_t c = 0; c < classes; ++c) { | ||
| 416 | VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; | ||
| 417 | info.image = fallback_images_[type_of[d] * classes + c].image; | ||
| 418 | info.viewType = view_types[d]; | ||
| 419 | info.format = formats[c]; | ||
| 420 | info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 421 | VkImageView view = VK_NULL_HANDLE; | ||
| 422 | if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false; | ||
| 423 | fallback_views_.push_back(view); | ||
| 424 | fallback_view_[d][c] = view; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | VkSamplerCreateInfo sampler{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO}; | ||
| 428 | sampler.magFilter = sampler.minFilter = VK_FILTER_NEAREST; | ||
| 429 | sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; | ||
| 430 | sampler.addressModeU = sampler.addressModeV = sampler.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; | ||
| 431 | sampler.maxLod = VK_LOD_CLAMP_NONE; | ||
| 432 | return vk_.vkCreateSampler(device_, &sampler, nullptr, &fallback_sampler_) == VK_SUCCESS; | ||
| 433 | } | ||
| 434 | |||
| 435 | bool create(TableBuffer& out, std::size_t bytes) const { | ||
| 436 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 437 | info.size = bytes; | ||
| 438 | info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; | ||
| 439 | TableBuffer made; | ||
| 440 | made.size = bytes; | ||
| 441 | if (vk_.vkCreateBuffer(device_, &info, nullptr, &made.buffer) != VK_SUCCESS) return false; | ||
| 442 | VkMemoryRequirements requirements{}; | ||
| 443 | vk_.vkGetBufferMemoryRequirements(device_, made.buffer, &requirements); | ||
| 444 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 445 | allocation.allocationSize = requirements.size; | ||
| 446 | allocation.memoryTypeIndex = memory_type( | ||
| 447 | requirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); | ||
| 448 | const bool ready = allocation.memoryTypeIndex != 0xffffffffu && | ||
| 449 | vk_.vkAllocateMemory(device_, &allocation, nullptr, &made.memory) == VK_SUCCESS && | ||
| 450 | vk_.vkBindBufferMemory(device_, made.buffer, made.memory, 0) == VK_SUCCESS && | ||
| 451 | vk_.vkMapMemory(device_, made.memory, 0, VK_WHOLE_SIZE, 0, &made.mapped) == VK_SUCCESS; | ||
| 452 | if (!ready) { | ||
| 453 | destroy(made); | ||
| 454 | return false; | ||
| 455 | } | ||
| 456 | out = made; | ||
| 457 | return true; | ||
| 458 | } | ||
| 459 | |||
| 460 | void destroy(TableBuffer& buffer) const { | ||
| 461 | if (buffer.buffer) vk_.vkDestroyBuffer(device_, buffer.buffer, nullptr); | ||
| 462 | if (buffer.memory) vk_.vkFreeMemory(device_, buffer.memory, nullptr); // unmaps it | ||
| 463 | buffer = {}; | ||
| 464 | } | ||
| 465 | |||
| 466 | bool create_table_buffer() { | ||
| 467 | const std::vector<std::uint32_t> words = table_.build_gpu_buffer(); | ||
| 468 | const VkDeviceSize bytes = words.size() * sizeof(std::uint32_t); | ||
| 469 | if (!create(table_buffer_, bytes)) return false; | ||
| 470 | std::memcpy(table_buffer_.mapped, words.data(), bytes); | ||
| 471 | table_dirty_ = entries_moved_ = false; | ||
| 472 | return true; | ||
| 473 | } | ||
| 474 | |||
| 475 | void write_table_binding() const { | ||
| 476 | const VkDescriptorBufferInfo info{table_buffer_.buffer, 0, VK_WHOLE_SIZE}; | ||
| 477 | VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 478 | write.dstSet = set_; | ||
| 479 | write.dstBinding = bindless::table_binding; | ||
| 480 | write.descriptorCount = 1; | ||
| 481 | write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; | ||
| 482 | write.pBufferInfo = &info; | ||
| 483 | vk_.vkUpdateDescriptorSets(device_, 1, &write, 0, nullptr); | ||
| 484 | } | ||
| 485 | |||
| 486 | VkDescriptorImageInfo fallback_info(std::uint32_t binding) const { | ||
| 487 | if (binding == bindless::sampler_binding) return {fallback_sampler_, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_UNDEFINED}; | ||
| 488 | for (std::uint32_t d = 0; d < dimensions; ++d) { | ||
| 489 | const ImageDimension dimension{d}; | ||
| 490 | std::uint32_t component_class = classes; // of the fallback view this binding takes | ||
| 491 | if (binding == bindless::sampled_binding(dimension)) component_class = 0; | ||
| 492 | if (binding == bindless::r32ui_binding(dimension)) component_class = 1; | ||
| 493 | for (std::uint32_t c = 0; c < classes; ++c) | ||
| 494 | if (binding == bindless::storage_binding(dimension, c)) component_class = c; | ||
| 495 | if (component_class < classes) | ||
| 496 | return {VK_NULL_HANDLE, fallback_view_[d][component_class], VK_IMAGE_LAYOUT_GENERAL}; | ||
| 497 | } | ||
| 498 | return {}; | ||
| 499 | } | ||
| 500 | |||
| 501 | // The table at binding 2 and the fallback at index 0 of every array, or in every element without partially_bound. | ||
| 502 | bool fill() { | ||
| 503 | write_table_binding(); | ||
| 504 | std::vector<std::vector<VkDescriptorImageInfo>> infos; | ||
| 505 | std::vector<VkWriteDescriptorSet> writes; | ||
| 506 | infos.reserve(bindless::bindings.size()); | ||
| 507 | for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) { | ||
| 508 | if (b == bindless::table_binding) continue; | ||
| 509 | const std::uint32_t count = features_.partially_bound ? 1 : bindless::bindings[b].count; | ||
| 510 | infos.emplace_back(count, fallback_info(b)); | ||
| 511 | VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 512 | write.dstSet = set_; | ||
| 513 | write.dstBinding = b; | ||
| 514 | write.descriptorCount = count; | ||
| 515 | write.descriptorType = bindless::bindings[b].type; | ||
| 516 | write.pImageInfo = infos.back().data(); | ||
| 517 | writes.push_back(write); | ||
| 518 | } | ||
| 519 | vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); | ||
| 520 | return true; | ||
| 521 | } | ||
| 522 | |||
| 523 | // A slot has one pending write: a later one takes its place. So a view or sampler that is removed before the | ||
| 524 | // next flush(), and may be destroyed by then, is never written. | ||
| 525 | void queue(std::uint32_t binding, std::uint32_t index, const VkDescriptorImageInfo& info) { | ||
| 526 | const std::uint64_t slot = (std::uint64_t(binding) << 32) | index; | ||
| 527 | const auto [at, added] = pending_slots_.try_emplace(slot, pending_.size()); | ||
| 528 | if (added) | ||
| 529 | pending_.push_back({binding, index, info}); | ||
| 530 | else | ||
| 531 | pending_[at->second].info = info; | ||
| 532 | } | ||
| 533 | |||
| 534 | void write_sampler(std::uint32_t index, VkSampler sampler) { | ||
| 535 | queue(bindless::sampler_binding, index, {sampler, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_UNDEFINED}); | ||
| 536 | } | ||
| 537 | |||
| 538 | // Queues every binding of the dimension at `index`: the given views, or the fallback where one is null. | ||
| 539 | void write_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout, | ||
| 540 | const StorageViews& storage) { | ||
| 541 | const std::uint32_t sampled_at = bindless::sampled_binding(dimension); | ||
| 542 | queue(sampled_at, index, | ||
| 543 | sampled ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout} : fallback_info(sampled_at)); | ||
| 544 | const auto store = [&](std::uint32_t binding, VkImageView view) { | ||
| 545 | if (binding == bindless::no_binding || index >= bindless::bindings[binding].count) return; | ||
| 546 | queue(binding, index, | ||
| 547 | view ? VkDescriptorImageInfo{VK_NULL_HANDLE, view, VK_IMAGE_LAYOUT_GENERAL} : fallback_info(binding)); | ||
| 548 | }; | ||
| 549 | store(bindless::storage_binding(dimension, 0), storage.as_float); | ||
| 550 | store(bindless::storage_binding(dimension, 1), storage.as_uint); | ||
| 551 | store(bindless::storage_binding(dimension, 2), storage.as_sint); | ||
| 552 | store(bindless::r32ui_binding(dimension), storage.r32ui); | ||
| 553 | } | ||
| 554 | |||
| 555 | VkDevice device_; | ||
| 556 | VkPhysicalDeviceMemoryProperties memory_; | ||
| 557 | BindlessFeatures features_; | ||
| 558 | Functions vk_; | ||
| 559 | bool ok_ = false; | ||
| 560 | VkDescriptorSetLayout layout_ = VK_NULL_HANDLE; | ||
| 561 | VkDescriptorPool pool_ = VK_NULL_HANDLE; | ||
| 562 | VkDescriptorSet set_ = VK_NULL_HANDLE; | ||
| 563 | std::array<FallbackImage, 3 * classes> fallback_images_{}; // [image type: 2D, 3D, 1D][class] | ||
| 564 | std::vector<VkImageView> fallback_views_; | ||
| 565 | VkImageView fallback_view_[dimensions][classes] = {}; | ||
| 566 | VkSampler fallback_sampler_ = VK_NULL_HANDLE; | ||
| 567 | bool fallbacks_ready_ = false; | ||
| 568 | std::array<std::vector<std::uint8_t>, dimensions> image_used_; | ||
| 569 | std::vector<std::uint8_t> sampler_used_; | ||
| 570 | std::vector<Pending> pending_; | ||
| 571 | std::unordered_map<std::uint64_t, std::size_t> pending_slots_; // binding and index -> place in pending_ | ||
| 572 | DescriptorTable table_{64}; | ||
| 573 | TableBuffer table_buffer_; | ||
| 574 | bool table_dirty_ = false; // the table differs from the buffer | ||
| 575 | bool entries_moved_ = false; // ... and not only by entries added | ||
| 576 | std::vector<TableBuffer> retired_; | ||
| 577 | }; | ||
| 578 | |||
| 579 | } // namespace leonida | ||