upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/vulkan_bindless.h
diff options
context:
space:
mode:
authorAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 19:05:06 +0100
committerAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 19:05:06 +0100
commite8a23c1afcf8c2a6b64793984c0548af5331197a (patch)
tree6efc13b47d3f22e53f79461e2b0b194c23581bd8 /runtime/src/vulkan_bindless.h
parent49475796c226c8511b9470bf97a4daf57b8af323 (diff)
Retire bindless table generations by timeline instead of waiting for the device (D3D-314)
A flush that grows the table or changes live keys now makes a new generation (table buffers, descriptor set, descriptors rewritten from a CPU shadow) that replay binds, and frees the old one once every submission up to its ticket has completed. No vkDeviceWaitIdle. Adds d3d12_bindless_churn_test and the LEONIDA_BINDLESS_IDLE_WAIT=1 comparison knob. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_bindless.h')
-rw-r--r--runtime/src/vulkan_bindless.h184
1 files changed, 139 insertions, 45 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h
index 5f484bf16..9ca6cdc1a 100644
--- a/runtime/src/vulkan_bindless.h
+++ b/runtime/src/vulkan_bindless.h
@@ -25,8 +25,11 @@
25// 25//
26// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind 26// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind
27// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while 27// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while
28// command buffers that use the set are pending, as long as no descriptor they use changes and flush_needs_idle() 28// command buffers that use the set are pending, as long as no descriptor they use changes and
29// is false; without it the caller flushes only when none is. With partially_bound (descriptorBindingPartiallyBound) only index 0 is written at 29// flush_replaces_table() is false; without it the caller flushes only when none is. When it is true the flush
30// does not touch what pending work reads: it makes a new generation (table buffers and descriptor set, the
31// descriptors copied from a CPU shadow) and retires the old one, which release_retired() frees once the owner
32// says the submissions that could have used it are done (D3D-314). With partially_bound (descriptorBindingPartiallyBound) only index 0 is written at
30// creation; otherwise every element of every array starts as the fallback. 33// creation; otherwise every element of every array starts as the fallback.
31// 34//
32// The table of binding 2 lives in DEVICE_LOCAL memory: every divergent T#/S# lookup reads it, and a HOST_VISIBLE 35// The table of binding 2 lives in DEVICE_LOCAL memory: every divergent T#/S# lookup reads it, and a HOST_VISIBLE
@@ -347,7 +350,9 @@ public:
347 std::uint32_t sampler_capacity() const { return features_.counts[bindless::sampler_binding]; } 350 std::uint32_t sampler_capacity() const { return features_.counts[bindless::sampler_binding]; }
348 // Set 0 of every pipeline layout made for translated shaders. 351 // Set 0 of every pipeline layout made for translated shaders.
349 VkDescriptorSetLayout layout() const { return layout_; } 352 VkDescriptorSetLayout layout() const { return layout_; }
350 // Bound at set 0. The same set for the life of this object: flush() updates it in place. 353 // Bound at set 0. flush() updates it in place until flush_replaces_table() is true: then the flush makes a new
354 // set (and table buffers), so read set() again after it, when recording the commands that use it. A submission
355 // that bound the previous set keeps it, intact, until release_retired() frees it.
351 VkDescriptorSet set() const { return set_; } 356 VkDescriptorSet set() const { return set_; }
352 357
353 // The fallback images are created with undefined contents: record this once, in a command buffer that executes 358 // The fallback images are created with undefined contents: record this once, in a command buffer that executes
@@ -444,13 +449,14 @@ public:
444 } 449 }
445 const DescriptorTable& table() const { return table_; } 450 const DescriptorTable& table() const { return table_; }
446 451
447 // True when the next flush() will change entries a running shader may be probing (a live key's index replaced 452 // True when the next flush() would change entries a running shader may be probing (a live key's index replaced
448 // or a live key moved by a relocating insert), or replace the table's buffer: the caller then flushes only once 453 // or a live key moved by a relocating insert), or replace the table's buffer. flush() then does not write in
449 // no command buffer that uses the set is still executing. Without it the flush only adds entries or empties the 454 // place: it uploads the whole table to new buffers, names them in a new descriptor set, and retires the old
450 // slots of erased keys, and the copy of an entry is not atomic: a shader probing a different key could in 455 // generation, so running work keeps reading an unchanged table and nobody waits for it. Without it the flush only
451 // principle see a torn entry that matches it, which is accepted as vanishingly unlikely because the added or 456 // adds entries or empties the slots of erased keys, and the copy of an entry is not atomic: a shader probing a
452 // erased key is not live (no running work uses it). 457 // different key could in principle see a torn entry that matches it, which is accepted as vanishingly unlikely
453 bool flush_needs_idle() const { 458 // because the added or erased key is not live (no running work uses it).
459 bool flush_replaces_table() const {
454 return table_dirty_ && (existing_entries_changed_ || force_full_upload_ || 460 return table_dirty_ && (existing_entries_changed_ || force_full_upload_ ||
455 table_.size_bytes() > table_buffers_.table.size); 461 table_.size_bytes() > table_buffers_.table.size);
456 } 462 }
@@ -471,13 +477,23 @@ public:
471 } 477 }
472 478
473 // Writes the pending descriptor changes and uploads the table, in `command_buffer`, before the commands that use 479 // Writes the pending descriptor changes and uploads the table, in `command_buffer`, before the commands that use
474 // the set are recorded after it. A table that has grown gets new buffers (binding 2 is rewritten); the old ones 480 // the set are recorded after it. When flush_replaces_table() was true this starts a new generation (table
475 // stay until release_retired(). False when they cannot be made: the set then still has the table as of the last 481 // buffers, descriptor set, binding 2 and every descriptor written so far) and puts the previous one on the retire
476 // flush. 482 // list, stamped `retire_ticket`: the owner's number for "every submission made so far", which release_retired()
477 bool flush(VkCommandBuffer command_buffer) { 483 // compares with what has completed. False when the new generation cannot be made: the set then still has the
484 // table as of the last flush.
485 bool flush(VkCommandBuffer command_buffer, std::uint64_t retire_ticket = 0) {
486 bool replaced = false;
478 bool written = true; 487 bool written = true;
479 if (table_dirty_ || !table_uploaded_) written = upload_table(command_buffer); 488 if (table_dirty_ || !table_uploaded_) written = upload_table(command_buffer, retire_ticket, replaced);
480 if (!pending_.empty()) { 489 for (const Pending& pending : pending_) {
490 if (pending.binding >= shadow_.size() || pending.index >= shadow_[pending.binding].size()) continue;
491 shadow_[pending.binding][pending.index] = pending.info;
492 shadow_written_[pending.binding][pending.index] = 1;
493 }
494 if (replaced) {
495 write_shadow(); // includes the pending writes just folded in
496 } else if (!pending_.empty()) {
481 std::vector<VkWriteDescriptorSet> writes(pending_.size()); 497 std::vector<VkWriteDescriptorSet> writes(pending_.size());
482 for (std::size_t i = 0; i < pending_.size(); ++i) { 498 for (std::size_t i = 0; i < pending_.size(); ++i) {
483 writes[i] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; 499 writes[i] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
@@ -489,17 +505,30 @@ public:
489 writes[i].pImageInfo = &pending_[i].info; 505 writes[i].pImageInfo = &pending_[i].info;
490 } 506 }
491 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); 507 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr);
492 pending_.clear();
493 pending_slots_.clear();
494 } 508 }
509 pending_.clear();
510 pending_slots_.clear();
495 return written; 511 return written;
496 } 512 }
497 513
498 // Frees the buffers of tables that have since grown: when nothing submitted before the last flush() still runs. 514 // Frees the generations (table buffers and descriptor set) that flush() retired with a ticket up to `completed`.
499 void release_retired() { 515 // The default frees all of them: when nothing submitted before the last flush() still runs.
500 for (auto& buffers : retired_) destroy(buffers); 516 void release_retired(std::uint64_t completed = UINT64_MAX) {
501 retired_.clear(); 517 std::size_t kept = 0;
518 for (auto& generation : retired_) {
519 if (generation.ticket <= completed) {
520 destroy(generation.buffers);
521 if (generation.pool) vk_.vkDestroyDescriptorPool(device_, generation.pool, nullptr);
522 } else {
523 retired_[kept++] = generation;
524 }
525 }
526 retired_.resize(kept);
502 } 527 }
528 bool has_retired() const { return !retired_.empty(); }
529 // How many generations flush() has made after the first, and how many wait for release_retired().
530 std::uint64_t generations_made() const { return generations_made_; }
531 std::size_t retired_count() const { return retired_.size(); }
503 532
504private: 533private:
505 struct Functions { 534 struct Functions {
@@ -571,30 +600,74 @@ private:
571 bool create_layout() { 600 bool create_layout() {
572 const bindless::SetLayout layout(features_); 601 const bindless::SetLayout layout(features_);
573 const std::uint32_t count = layout.info.bindingCount; 602 const std::uint32_t count = layout.info.bindingCount;
574 std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{}; 603 pool_size_count_ = count;
575 for (std::uint32_t i = 0; i < count; ++i) 604 for (std::uint32_t i = 0; i < count; ++i)
576 sizes[i] = {layout.entries[i].descriptorType, layout.entries[i].descriptorCount}; 605 pool_sizes_[i] = {layout.entries[i].descriptorType, layout.entries[i].descriptorCount};
577 if (vk_.vkCreateDescriptorSetLayout(device_, &layout.info, nullptr, &layout_) != VK_SUCCESS) return false; 606 if (vk_.vkCreateDescriptorSetLayout(device_, &layout.info, nullptr, &layout_) != VK_SUCCESS) return false;
607 if (!make_set(pool_, set_)) return false;
608 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
609 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue;
610 shadow_[b].assign(features_.counts[b], VkDescriptorImageInfo{});
611 shadow_written_[b].assign(features_.counts[b], 0);
612 }
613
614 for (std::uint32_t d = 0; d < dimensions; ++d) {
615 image_used_[d].assign(image_capacity(ImageDimension(d)), 0);
616 image_used_[d][0] = 1;
617 }
618 sampler_used_.assign(sampler_capacity(), 0);
619 sampler_used_[0] = 1;
620 return true;
621 }
578 622
623 // A one-set pool and its set at the layout's sizes: the first generation's, and each replacement's.
624 bool make_set(VkDescriptorPool& pool, VkDescriptorSet& set) const {
579 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; 625 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
580 pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0; 626 pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0;
581 pool_info.maxSets = 1; 627 pool_info.maxSets = 1;
582 pool_info.poolSizeCount = count; 628 pool_info.poolSizeCount = pool_size_count_;
583 pool_info.pPoolSizes = sizes.data(); 629 pool_info.pPoolSizes = pool_sizes_.data();
584 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool_) != VK_SUCCESS) return false; 630 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool) != VK_SUCCESS) {
631 pool = VK_NULL_HANDLE;
632 return false;
633 }
585 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; 634 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
586 set_info.descriptorPool = pool_; 635 set_info.descriptorPool = pool;
587 set_info.descriptorSetCount = 1; 636 set_info.descriptorSetCount = 1;
588 set_info.pSetLayouts = &layout_; 637 set_info.pSetLayouts = &layout_;
589 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set_) != VK_SUCCESS) return false; 638 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set) == VK_SUCCESS) return true;
639 vk_.vkDestroyDescriptorPool(device_, pool, nullptr);
640 pool = VK_NULL_HANDLE;
641 set = VK_NULL_HANDLE;
642 return false;
643 }
590 644
591 for (std::uint32_t d = 0; d < dimensions; ++d) { 645 // Writes every descriptor written so far (the shadow) into the current set, one update per run of written
592 image_used_[d].assign(image_capacity(ImageDimension(d)), 0); 646 // elements: the set was just made, and unwritten elements stay unwritten, as partially_bound allows.
593 image_used_[d][0] = 1; 647 void write_shadow() const {
648 std::vector<VkWriteDescriptorSet> writes;
649 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
650 const auto& written = shadow_written_[b];
651 for (std::size_t i = 0; i < written.size();) {
652 if (!written[i]) {
653 ++i;
654 continue;
655 }
656 std::size_t end = i;
657 while (end < written.size() && written[end]) ++end;
658 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
659 write.dstSet = set_;
660 write.dstBinding = b;
661 write.dstArrayElement = std::uint32_t(i);
662 write.descriptorCount = std::uint32_t(end - i);
663 write.descriptorType = bindless::bindings[b].type;
664 write.pImageInfo = &shadow_[b][i];
665 writes.push_back(write);
666 i = end;
667 }
594 } 668 }
595 sampler_used_.assign(sampler_capacity(), 0); 669 if (!writes.empty())
596 sampler_used_[0] = 1; 670 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr);
597 return true;
598 } 671 }
599 672
600 // One 1x1 image for each image type and component class, and the views of them every binding needs. 673 // One 1x1 image for each image type and component class, and the views of them every binding needs.
@@ -727,24 +800,31 @@ private:
727 800
728 // Uploads what changed since the last call: the whole table when it grew (the mask moved) or was never uploaded, 801 // Uploads what changed since the last call: the whole table when it grew (the mask moved) or was never uploaded,
729 // the changed slots otherwise. The copy and its barrier go into `command_buffer`, before the draws. 802 // the changed slots otherwise. The copy and its barrier go into `command_buffer`, before the draws.
730 bool upload_table(VkCommandBuffer command_buffer) { 803 bool upload_table(VkCommandBuffer command_buffer, std::uint64_t retire_ticket, bool& replaced) {
731 if (!command_buffer) return false; 804 if (!command_buffer) return false;
732 const std::size_t bytes = table_.size_bytes(); 805 const std::size_t bytes = table_.size_bytes();
733 bool full = force_full_upload_ || !table_uploaded_ || bytes > table_buffers_.table.size; 806 // A table that grew, or whose live entries change, goes to a new generation; the old one may still be read.
734 bool grew = false; 807 const bool replace = bytes > table_buffers_.table.size ||
735 if (bytes > table_buffers_.table.size) { 808 (table_uploaded_ && (existing_entries_changed_ || force_full_upload_));
809 bool full = force_full_upload_ || !table_uploaded_ || replace;
810 if (replace) {
736 TableBuffers larger; 811 TableBuffers larger;
812 VkDescriptorPool pool = VK_NULL_HANDLE;
813 VkDescriptorSet set = VK_NULL_HANDLE;
737 bool device_local = false; 814 bool device_local = false;
738 if (!create(larger.table, bytes, true, &device_local) || !create(larger.staging, bytes, false)) { 815 if (!create(larger.table, bytes, true, &device_local) || !create(larger.staging, bytes, false) ||
816 !make_set(pool, set)) {
739 destroy(larger); 817 destroy(larger);
740 return false; 818 return false;
741 } 819 }
742 table_device_local_ = device_local; 820 table_device_local_ = device_local;
743 retired_.push_back(table_buffers_); 821 retired_.push_back({table_buffers_, pool_, set_, retire_ticket});
744 table_buffers_ = larger; 822 table_buffers_ = larger;
823 pool_ = pool;
824 set_ = set;
745 write_table_binding(table_buffers_.table.buffer); 825 write_table_binding(table_buffers_.table.buffer);
746 full = true; 826 replaced = true;
747 grew = true; 827 ++generations_made_;
748 } 828 }
749 DescriptorTable::Changes changes = table_.take_changes(); 829 DescriptorTable::Changes changes = table_.take_changes();
750 full = full || changes.all; 830 full = full || changes.all;
@@ -752,7 +832,7 @@ private:
752 // The staging buffer is written only for buffers that were just created (first upload or growth): a later 832 // The staging buffer is written only for buffers that were just created (first upload or growth): a later
753 // flush then either updates slots with vkCmdUpdateBuffer or rebuilds from a fresh pair, so no copy still in 833 // flush then either updates slots with vkCmdUpdateBuffer or rebuilds from a fresh pair, so no copy still in
754 // flight can be reading bytes the CPU rewrites. 834 // flight can be reading bytes the CPU rewrites.
755 const bool fresh = grew || !table_uploaded_; 835 const bool fresh = replace || !table_uploaded_;
756 const auto upload_all = [&]() { 836 const auto upload_all = [&]() {
757 if (fresh) { 837 if (fresh) {
758 table_.write_to_buffer(table_buffers_.staging.mapped); 838 table_.write_to_buffer(table_buffers_.staging.mapped);
@@ -857,6 +937,8 @@ private:
857 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue; 937 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue;
858 const std::uint32_t count = features_.partially_bound ? 1 : features_.counts[b]; 938 const std::uint32_t count = features_.partially_bound ? 1 : features_.counts[b];
859 infos.emplace_back(count, fallback_info(b)); 939 infos.emplace_back(count, fallback_info(b));
940 std::fill_n(shadow_[b].begin(), count, infos.back()[0]);
941 std::fill_n(shadow_written_[b].begin(), count, std::uint8_t(1));
860 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; 942 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
861 write.dstSet = set_; 943 write.dstSet = set_;
862 write.dstBinding = b; 944 write.dstBinding = b;
@@ -936,7 +1018,19 @@ private:
936 bool existing_entries_changed_ = false; // ... and not only by entries added 1018 bool existing_entries_changed_ = false; // ... and not only by entries added
937 bool upload_in_flight_ = false; // a copy is recorded, not yet known to have reached the queue 1019 bool upload_in_flight_ = false; // a copy is recorded, not yet known to have reached the queue
938 bool force_full_upload_ = false; // the last submit failed: rebuild the whole table on the next flush 1020 bool force_full_upload_ = false; // the last submit failed: rebuild the whole table on the next flush
939 std::vector<TableBuffers> retired_; 1021 struct Generation { // what a replaced table leaves for release_retired()
1022 TableBuffers buffers;
1023 VkDescriptorPool pool = VK_NULL_HANDLE; // owns the descriptor set
1024 VkDescriptorSet set = VK_NULL_HANDLE;
1025 std::uint64_t ticket = 0;
1026 };
1027 std::vector<Generation> retired_;
1028 std::uint64_t generations_made_ = 0;
1029 std::array<VkDescriptorPoolSize, bindless::bindings.size()> pool_sizes_{};
1030 std::uint32_t pool_size_count_ = 0;
1031 // Every descriptor written to the set (the table's binding aside), to fill the next generation's set.
1032 std::array<std::vector<VkDescriptorImageInfo>, bindless::bindings.size()> shadow_;
1033 std::array<std::vector<std::uint8_t>, bindless::bindings.size()> shadow_written_;
940}; 1034};
941 1035
942} // namespace leonida 1036} // namespace leonida