diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 17:22:43 +0100 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 17:22:43 +0100 |
| commit | 3d1dbf1cef8b9240cce69b2bcdba0c09ea0f1d5e (patch) | |
| tree | 5d84c2e541aa31bcf23d9921dde7e9cb92b2ac59 /runtime/src/vulkan_bindless.h | |
| parent | 2560485f76c45779ec93a2a6dc001c93211f7b3f (diff) | |
| parent | 4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff) | |
Merge main into test/coverage-gaps (#1299)
Conflicts: runtime/test/shaders/check.py, scripts/run_gpu_tests.py
AI-assisted.
Diffstat (limited to 'runtime/src/vulkan_bindless.h')
| -rw-r--r-- | runtime/src/vulkan_bindless.h | 80 |
1 files changed, 51 insertions, 29 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h index c599eb7c2..5f484bf16 100644 --- a/runtime/src/vulkan_bindless.h +++ b/runtime/src/vulkan_bindless.h | |||
| @@ -47,6 +47,7 @@ | |||
| 47 | #include <array> | 47 | #include <array> |
| 48 | #include <cstdint> | 48 | #include <cstdint> |
| 49 | #include <cstring> | 49 | #include <cstring> |
| 50 | #include <functional> | ||
| 50 | #include <unordered_map> | 51 | #include <unordered_map> |
| 51 | #include <vector> | 52 | #include <vector> |
| 52 | 53 | ||
| @@ -162,6 +163,35 @@ constexpr std::uint32_t r32ui_binding(ImageDimension dimension) { | |||
| 162 | return dimension == ImageDimension::dim_2d ? 4 : dimension == ImageDimension::dim_3d ? 7 : no_binding; | 163 | return dimension == ImageDimension::dim_2d ? 4 : dimension == ImageDimension::dim_3d ? 7 : no_binding; |
| 163 | } | 164 | } |
| 164 | 165 | ||
| 166 | // Set 0's descriptor set layout at features' counts: what create_layout creates and what size_bindings' | ||
| 167 | // layout_supported asks vkGetDescriptorSetLayoutSupport about. It points into itself, so it is not copied. | ||
| 168 | struct SetLayout { | ||
| 169 | std::array<VkDescriptorSetLayoutBinding, binding_count> entries{}; | ||
| 170 | std::array<VkDescriptorBindingFlags, binding_count> flags{}; | ||
| 171 | VkDescriptorSetLayoutBindingFlagsCreateInfo flags_info{ | ||
| 172 | VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO}; | ||
| 173 | VkDescriptorSetLayoutCreateInfo info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; | ||
| 174 | |||
| 175 | explicit SetLayout(const BindlessFeatures& features) { | ||
| 176 | std::uint32_t count = 0; | ||
| 177 | for (std::uint32_t b = 0; b < binding_count; ++b) { | ||
| 178 | if (!binding_enabled(b, features)) continue; | ||
| 179 | entries[count] = {b, bindings[b].type, features.counts[b], VK_SHADER_STAGE_ALL, nullptr}; | ||
| 180 | flags[count] = (features.update_after_bind ? VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT : 0) | | ||
| 181 | (features.partially_bound ? VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT : 0); | ||
| 182 | ++count; | ||
| 183 | } | ||
| 184 | flags_info.bindingCount = count; | ||
| 185 | flags_info.pBindingFlags = flags.data(); | ||
| 186 | info.pNext = &flags_info; | ||
| 187 | info.flags = features.update_after_bind ? VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT : 0; | ||
| 188 | info.bindingCount = count; | ||
| 189 | info.pBindings = entries.data(); | ||
| 190 | } | ||
| 191 | SetLayout(const SetLayout&) = delete; | ||
| 192 | SetLayout& operator=(const SetLayout&) = delete; | ||
| 193 | }; | ||
| 194 | |||
| 165 | // What size_bindings() grows to where the device's limits allow. At 32-64 bytes a descriptor the whole set is then | 195 | // What size_bindings() grows to where the device's limits allow. At 32-64 bytes a descriptor the whole set is then |
| 166 | // 3-6 MiB (93793 descriptors on an RTX 5070). | 196 | // 3-6 MiB (93793 descriptors on an RTX 5070). |
| 167 | inline constexpr std::uint32_t max_2d_images = 65536; | 197 | inline constexpr std::uint32_t max_2d_images = 65536; |
| @@ -183,10 +213,14 @@ inline constexpr std::uint32_t growth_headroom = 64; | |||
| 183 | // what is sure to work (vkGetDescriptorSetLayoutSupport may allow more), so it never shrinks step 1. | 213 | // what is sure to work (vkGetDescriptorSetLayoutSupport may allow more), so it never shrinks step 1. |
| 184 | // A storage array holds the smaller of the storage count and its dimension's images: add_image never gives a | 214 | // A storage array holds the smaller of the storage count and its dimension's images: add_image never gives a |
| 185 | // storage view an index past the sampled array. The other bindings keep their default counts. | 215 | // storage view an index past the sampled array. The other bindings keep their default counts. |
| 216 | // layout_supported, when given, answers vkGetDescriptorSetLayoutSupport for set 0 at the counts in its argument: | ||
| 217 | // step 3 then also grows past maxPerSetDescriptors where the device says the layout works (Intel's Xe-LP reports | ||
| 218 | // 6159 and supports the full sizes, D3D-219). It is asked only once the sum is above maxPerSetDescriptors. | ||
| 186 | // Sets features.counts; returns false when a count is below its default because the device's limits are. | 219 | // Sets features.counts; returns false when a count is below its default because the device's limits are. |
| 187 | inline bool size_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimits& limits, | 220 | inline bool size_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimits& limits, |
| 188 | const VkPhysicalDeviceVulkan11Properties& v11, | 221 | const VkPhysicalDeviceVulkan11Properties& v11, |
| 189 | const VkPhysicalDeviceVulkan12Properties& v12) { | 222 | const VkPhysicalDeviceVulkan12Properties& v12, |
| 223 | const std::function<bool(const BindlessFeatures&)>& layout_supported = {}) { | ||
| 190 | const bool uab = features.update_after_bind; | 224 | const bool uab = features.update_after_bind; |
| 191 | const std::uint32_t sampled_limit = | 225 | const std::uint32_t sampled_limit = |
| 192 | uab ? std::min(v12.maxPerStageDescriptorUpdateAfterBindSampledImages, | 226 | uab ? std::min(v12.maxPerStageDescriptorUpdateAfterBindSampledImages, |
| @@ -233,7 +267,8 @@ inline bool size_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimi | |||
| 233 | const auto within = [&](std::uint64_t n, std::uint32_t limit) { return n + headroom <= limit; }; | 267 | const auto within = [&](std::uint64_t n, std::uint32_t limit) { return n + headroom <= limit; }; |
| 234 | const std::uint64_t all = sampled + sampler + stored + buffers; | 268 | const std::uint64_t all = sampled + sampler + stored + buffers; |
| 235 | return within(sampled, sampled_limit) && within(sampler, sampler_limit) && within(stored, storage_limit) && | 269 | return within(sampled, sampled_limit) && within(sampler, sampler_limit) && within(stored, storage_limit) && |
| 236 | within(sampled + stored + buffers, resource_limit) && all <= pool_limit && all <= set_limit; | 270 | within(sampled + stored + buffers, resource_limit) && all <= pool_limit && |
| 271 | (all <= set_limit || (layout_supported && layout_supported(features))); | ||
| 237 | }; | 272 | }; |
| 238 | // Raises n to the largest value up to `high` that fits; leaves it where it is when none does. | 273 | // Raises n to the largest value up to `high` that fits; leaves it where it is when none does. |
| 239 | const auto grow = [&](std::uint32_t& n, std::uint32_t high, std::uint32_t headroom, std::uint64_t set_limit) { | 274 | const auto grow = [&](std::uint32_t& n, std::uint32_t high, std::uint32_t headroom, std::uint64_t set_limit) { |
| @@ -400,19 +435,21 @@ public: | |||
| 400 | result == DescriptorTable::InsertResult::added_with_relocation; | 435 | result == DescriptorTable::InsertResult::added_with_relocation; |
| 401 | return true; | 436 | return true; |
| 402 | } | 437 | } |
| 403 | // Erasing compacts the key's probe cluster, so other keys' entries may change slots. | 438 | // Erasing empties only the key's own slot; no other entry moves (DescriptorTable::erase), so it needs no idle. |
| 439 | // The caller erases a key once no submitted work uses it (its texture is gone), like D3D12 requires. | ||
| 404 | bool erase_key(const DescriptorKey& key) { | 440 | bool erase_key(const DescriptorKey& key) { |
| 405 | if (!table_.erase(key)) return false; | 441 | if (!table_.erase(key)) return false; |
| 406 | table_dirty_ = existing_entries_changed_ = true; | 442 | table_dirty_ = true; |
| 407 | return true; | 443 | return true; |
| 408 | } | 444 | } |
| 409 | const DescriptorTable& table() const { return table_; } | 445 | const DescriptorTable& table() const { return table_; } |
| 410 | 446 | ||
| 411 | // True when the next flush() will change entries a running shader may be probing, or replace the table's | 447 | // True when the next flush() will change entries a running shader may be probing (a live key's index replaced |
| 412 | // buffer: the caller then flushes only once no command buffer that uses the set is still executing. Without | 448 | // or a live key moved by a relocating insert), or replace the table's buffer: the caller then flushes only once |
| 413 | // it the flush only adds entries, and the copy of an entry is not atomic: a shader probing a different key | 449 | // no command buffer that uses the set is still executing. Without it the flush only adds entries or empties the |
| 414 | // could in principle see a torn entry that matches it, which is accepted as vanishingly unlikely because the | 450 | // slots of erased keys, and the copy of an entry is not atomic: a shader probing a different key could in |
| 415 | // added key is new (there is no live key equal to it). | 451 | // principle see a torn entry that matches it, which is accepted as vanishingly unlikely because the added or |
| 452 | // erased key is not live (no running work uses it). | ||
| 416 | bool flush_needs_idle() const { | 453 | bool flush_needs_idle() const { |
| 417 | return table_dirty_ && (existing_entries_changed_ || force_full_upload_ || | 454 | return table_dirty_ && (existing_entries_changed_ || force_full_upload_ || |
| 418 | table_.size_bytes() > table_buffers_.table.size); | 455 | table_.size_bytes() > table_buffers_.table.size); |
| @@ -532,27 +569,12 @@ private: | |||
| 532 | } | 569 | } |
| 533 | 570 | ||
| 534 | bool create_layout() { | 571 | bool create_layout() { |
| 535 | std::array<VkDescriptorSetLayoutBinding, bindless::bindings.size()> entries{}; | 572 | const bindless::SetLayout layout(features_); |
| 536 | std::array<VkDescriptorBindingFlags, bindless::bindings.size()> flags{}; | 573 | const std::uint32_t count = layout.info.bindingCount; |
| 537 | std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{}; | 574 | std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{}; |
| 538 | std::uint32_t count = 0; | 575 | for (std::uint32_t i = 0; i < count; ++i) |
| 539 | for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) { | 576 | sizes[i] = {layout.entries[i].descriptorType, layout.entries[i].descriptorCount}; |
| 540 | if (!bindless::binding_enabled(b, features_)) continue; | 577 | if (vk_.vkCreateDescriptorSetLayout(device_, &layout.info, nullptr, &layout_) != VK_SUCCESS) return false; |
| 541 | entries[count] = {b, bindless::bindings[b].type, features_.counts[b], VK_SHADER_STAGE_ALL, nullptr}; | ||
| 542 | flags[count] = (features_.update_after_bind ? VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT : 0) | | ||
| 543 | (features_.partially_bound ? VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT : 0); | ||
| 544 | sizes[count] = {bindless::bindings[b].type, features_.counts[b]}; | ||
| 545 | ++count; | ||
| 546 | } | ||
| 547 | VkDescriptorSetLayoutBindingFlagsCreateInfo flags_info{ | ||
| 548 | VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO}; | ||
| 549 | flags_info.bindingCount = count; | ||
| 550 | flags_info.pBindingFlags = flags.data(); | ||
| 551 | VkDescriptorSetLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, &flags_info}; | ||
| 552 | layout_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT : 0; | ||
| 553 | layout_info.bindingCount = count; | ||
| 554 | layout_info.pBindings = entries.data(); | ||
| 555 | if (vk_.vkCreateDescriptorSetLayout(device_, &layout_info, nullptr, &layout_) != VK_SUCCESS) return false; | ||
| 556 | 578 | ||
| 557 | VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; | 579 | VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; |
| 558 | pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0; | 580 | pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0; |