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:
authorRafael <rafaelalvesp2005@gmail.com>2026-11-23 15:36:26 -0300
committerRafael <rafaelalvesp2005@gmail.com>2026-11-23 15:36:26 -0300
commit63f0e0dea3a9d44201db408c4e2c3f2f5b2b94d6 (patch)
tree86f0dcad8070e2a3a693ef4897aa83950cfeb224 /runtime/src/vulkan_bindless.h
parentf78f7d6a2a9364cb128c03ac539fe540d5c70df1 (diff)
parent6dda0630bd6fc0514baadb94fb98e36bd7d64c0b (diff)
Merge main into fix/walk-bink_names
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.h195
1 files changed, 150 insertions, 45 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h
index d8a4c9846..cb5b61f65 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
@@ -355,7 +358,9 @@ public:
355 } 358 }
356 // Set 0 of every pipeline layout made for translated shaders. 359 // Set 0 of every pipeline layout made for translated shaders.
357 VkDescriptorSetLayout layout() const { return layout_; } 360 VkDescriptorSetLayout layout() const { return layout_; }
358 // Bound at set 0. The same set for the life of this object: flush() updates it in place. 361 // Bound at set 0. flush() updates it in place until flush_replaces_table() is true: then the flush makes a new
362 // set (and table buffers), so read set() again after it, when recording the commands that use it. A submission
363 // that bound the previous set keeps it, intact, until release_retired() frees it.
359 VkDescriptorSet set() const { return set_; } 364 VkDescriptorSet set() const { return set_; }
360 365
361 // The fallback images are created with undefined contents: record this once, in a command buffer that executes 366 // The fallback images are created with undefined contents: record this once, in a command buffer that executes
@@ -452,13 +457,14 @@ public:
452 } 457 }
453 const DescriptorTable& table() const { return table_; } 458 const DescriptorTable& table() const { return table_; }
454 459
455 // True when the next flush() will change entries a running shader may be probing (a live key's index replaced 460 // True when the next flush() would change entries a running shader may be probing (a live key's index replaced
456 // or a live key moved by a relocating insert), or replace the table's buffer: the caller then flushes only once 461 // or a live key moved by a relocating insert), or replace the table's buffer. flush() then does not write in
457 // no command buffer that uses the set is still executing. Without it the flush only adds entries or empties the 462 // place: it uploads the whole table to new buffers, names them in a new descriptor set, and retires the old
458 // slots of erased keys, and the copy of an entry is not atomic: a shader probing a different key could in 463 // generation, so running work keeps reading an unchanged table and nobody waits for it. Without it the flush only
459 // principle see a torn entry that matches it, which is accepted as vanishingly unlikely because the added or 464 // adds entries or empties the slots of erased keys, and the copy of an entry is not atomic: a shader probing a
460 // erased key is not live (no running work uses it). 465 // different key could in principle see a torn entry that matches it, which is accepted as vanishingly unlikely
461 bool flush_needs_idle() const { 466 // because the added or erased key is not live (no running work uses it).
467 bool flush_replaces_table() const {
462 return table_dirty_ && (existing_entries_changed_ || force_full_upload_ || 468 return table_dirty_ && (existing_entries_changed_ || force_full_upload_ ||
463 table_.size_bytes() > table_buffers_.table.size); 469 table_.size_bytes() > table_buffers_.table.size);
464 } 470 }
@@ -479,13 +485,28 @@ public:
479 } 485 }
480 486
481 // Writes the pending descriptor changes and uploads the table, in `command_buffer`, before the commands that use 487 // Writes the pending descriptor changes and uploads the table, in `command_buffer`, before the commands that use
482 // the set are recorded after it. A table that has grown gets new buffers (binding 2 is rewritten); the old ones 488 // the set are recorded after it. When flush_replaces_table() was true this starts a new generation (table
483 // stay until release_retired(). False when they cannot be made: the set then still has the table as of the last 489 // buffers, descriptor set, binding 2 and every descriptor written so far) and puts the previous one on the retire
484 // flush. 490 // list, stamped `retire_ticket`: the owner's number for "every submission made so far", which release_retired()
485 bool flush(VkCommandBuffer command_buffer) { 491 // compares with what has completed. False when the new generation cannot be made: the set then still has the
492 // table as of the last flush.
493 bool flush(VkCommandBuffer command_buffer, std::uint64_t retire_ticket = 0) {
494 bool replaced = false;
486 bool written = true; 495 bool written = true;
487 if (table_dirty_ || !table_uploaded_) written = upload_table(command_buffer); 496 const bool must_replace = flush_replaces_table();
488 if (!pending_.empty()) { 497 if (table_dirty_ || !table_uploaded_) written = upload_table(command_buffer, retire_ticket, replaced);
498 last_flush_replaced_ = replaced;
499 // The new generation could not be made: the current set is the one running work reads, so its descriptors are
500 // not written in place either. The pending writes and the table's changes stay for the next flush.
501 if (!written && must_replace) return false;
502 for (const Pending& pending : pending_) {
503 if (pending.binding >= shadow_.size() || pending.index >= shadow_[pending.binding].size()) continue;
504 shadow_[pending.binding][pending.index] = pending.info;
505 shadow_written_[pending.binding][pending.index] = 1;
506 }
507 if (replaced) {
508 write_shadow(); // includes the pending writes just folded in
509 } else if (!pending_.empty()) {
489 std::vector<VkWriteDescriptorSet> writes(pending_.size()); 510 std::vector<VkWriteDescriptorSet> writes(pending_.size());
490 for (std::size_t i = 0; i < pending_.size(); ++i) { 511 for (std::size_t i = 0; i < pending_.size(); ++i) {
491 writes[i] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; 512 writes[i] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
@@ -497,17 +518,35 @@ public:
497 writes[i].pImageInfo = &pending_[i].info; 518 writes[i].pImageInfo = &pending_[i].info;
498 } 519 }
499 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); 520 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr);
500 pending_.clear();
501 pending_slots_.clear();
502 } 521 }
522 pending_.clear();
523 pending_slots_.clear();
503 return written; 524 return written;
504 } 525 }
505 526
506 // Frees the buffers of tables that have since grown: when nothing submitted before the last flush() still runs. 527 // Frees the generations (table buffers and descriptor set) that flush() retired with a ticket up to `completed`.
507 void release_retired() { 528 // The default frees all of them: when nothing submitted before the last flush() still runs.
508 for (auto& buffers : retired_) destroy(buffers); 529 void release_retired(std::uint64_t completed = UINT64_MAX) {
509 retired_.clear(); 530 std::size_t kept = 0;
531 for (auto& generation : retired_) {
532 if (generation.ticket <= completed) {
533 destroy(generation.buffers);
534 if (generation.pool) vk_.vkDestroyDescriptorPool(device_, generation.pool, nullptr);
535 } else {
536 retired_[kept++] = generation;
537 }
538 }
539 retired_.resize(kept);
510 } 540 }
541 bool has_retired() const { return !retired_.empty(); }
542 // True when the last flush() started a new generation: its table upload is in that flush's command buffer, so
543 // work on other queues that binds the new set must run after it (the owner orders them).
544 bool last_flush_replaced() const { return last_flush_replaced_; }
545 // The ticket of the oldest retired generation (0 when there is none).
546 std::uint64_t oldest_retired_ticket() const { return retired_.empty() ? 0 : retired_.front().ticket; }
547 // How many generations flush() has made after the first, and how many wait for release_retired().
548 std::uint64_t generations_made() const { return generations_made_; }
549 std::size_t retired_count() const { return retired_.size(); }
511 550
512private: 551private:
513 struct Functions { 552 struct Functions {
@@ -579,30 +618,74 @@ private:
579 bool create_layout() { 618 bool create_layout() {
580 const bindless::SetLayout layout(features_); 619 const bindless::SetLayout layout(features_);
581 const std::uint32_t count = layout.info.bindingCount; 620 const std::uint32_t count = layout.info.bindingCount;
582 std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{}; 621 pool_size_count_ = count;
583 for (std::uint32_t i = 0; i < count; ++i) 622 for (std::uint32_t i = 0; i < count; ++i)
584 sizes[i] = {layout.entries[i].descriptorType, layout.entries[i].descriptorCount}; 623 pool_sizes_[i] = {layout.entries[i].descriptorType, layout.entries[i].descriptorCount};
585 if (vk_.vkCreateDescriptorSetLayout(device_, &layout.info, nullptr, &layout_) != VK_SUCCESS) return false; 624 if (vk_.vkCreateDescriptorSetLayout(device_, &layout.info, nullptr, &layout_) != VK_SUCCESS) return false;
625 if (!make_set(pool_, set_)) return false;
626 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
627 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue;
628 shadow_[b].assign(features_.counts[b], VkDescriptorImageInfo{});
629 shadow_written_[b].assign(features_.counts[b], 0);
630 }
631
632 for (std::uint32_t d = 0; d < dimensions; ++d) {
633 image_used_[d].assign(image_capacity(ImageDimension(d)), 0);
634 image_used_[d][0] = 1;
635 }
636 sampler_used_.assign(sampler_capacity(), 0);
637 sampler_used_[0] = 1;
638 return true;
639 }
586 640
641 // A one-set pool and its set at the layout's sizes: the first generation's, and each replacement's.
642 bool make_set(VkDescriptorPool& pool, VkDescriptorSet& set) const {
587 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; 643 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
588 pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0; 644 pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0;
589 pool_info.maxSets = 1; 645 pool_info.maxSets = 1;
590 pool_info.poolSizeCount = count; 646 pool_info.poolSizeCount = pool_size_count_;
591 pool_info.pPoolSizes = sizes.data(); 647 pool_info.pPoolSizes = pool_sizes_.data();
592 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool_) != VK_SUCCESS) return false; 648 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool) != VK_SUCCESS) {
649 pool = VK_NULL_HANDLE;
650 return false;
651 }
593 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; 652 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
594 set_info.descriptorPool = pool_; 653 set_info.descriptorPool = pool;
595 set_info.descriptorSetCount = 1; 654 set_info.descriptorSetCount = 1;
596 set_info.pSetLayouts = &layout_; 655 set_info.pSetLayouts = &layout_;
597 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set_) != VK_SUCCESS) return false; 656 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set) == VK_SUCCESS) return true;
657 vk_.vkDestroyDescriptorPool(device_, pool, nullptr);
658 pool = VK_NULL_HANDLE;
659 set = VK_NULL_HANDLE;
660 return false;
661 }
598 662
599 for (std::uint32_t d = 0; d < dimensions; ++d) { 663 // Writes every descriptor written so far (the shadow) into the current set, one update per run of written
600 image_used_[d].assign(image_capacity(ImageDimension(d)), 0); 664 // elements: the set was just made, and unwritten elements stay unwritten, as partially_bound allows.
601 image_used_[d][0] = 1; 665 void write_shadow() const {
666 std::vector<VkWriteDescriptorSet> writes;
667 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
668 const auto& written = shadow_written_[b];
669 for (std::size_t i = 0; i < written.size();) {
670 if (!written[i]) {
671 ++i;
672 continue;
673 }
674 std::size_t end = i;
675 while (end < written.size() && written[end]) ++end;
676 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
677 write.dstSet = set_;
678 write.dstBinding = b;
679 write.dstArrayElement = std::uint32_t(i);
680 write.descriptorCount = std::uint32_t(end - i);
681 write.descriptorType = bindless::bindings[b].type;
682 write.pImageInfo = &shadow_[b][i];
683 writes.push_back(write);
684 i = end;
685 }
602 } 686 }
603 sampler_used_.assign(sampler_capacity(), 0); 687 if (!writes.empty())
604 sampler_used_[0] = 1; 688 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr);
605 return true;
606 } 689 }
607 690
608 // One 1x1 image for each image type and component class, and the views of them every binding needs. 691 // One 1x1 image for each image type and component class, and the views of them every binding needs.
@@ -735,24 +818,31 @@ private:
735 818
736 // Uploads what changed since the last call: the whole table when it grew (the mask moved) or was never uploaded, 819 // Uploads what changed since the last call: the whole table when it grew (the mask moved) or was never uploaded,
737 // the changed slots otherwise. The copy and its barrier go into `command_buffer`, before the draws. 820 // the changed slots otherwise. The copy and its barrier go into `command_buffer`, before the draws.
738 bool upload_table(VkCommandBuffer command_buffer) { 821 bool upload_table(VkCommandBuffer command_buffer, std::uint64_t retire_ticket, bool& replaced) {
739 if (!command_buffer) return false; 822 if (!command_buffer) return false;
740 const std::size_t bytes = table_.size_bytes(); 823 const std::size_t bytes = table_.size_bytes();
741 bool full = force_full_upload_ || !table_uploaded_ || bytes > table_buffers_.table.size; 824 // A table that grew, or whose live entries change, goes to a new generation; the old one may still be read.
742 bool grew = false; 825 const bool replace = bytes > table_buffers_.table.size ||
743 if (bytes > table_buffers_.table.size) { 826 (table_uploaded_ && (existing_entries_changed_ || force_full_upload_));
827 bool full = force_full_upload_ || !table_uploaded_ || replace;
828 if (replace) {
744 TableBuffers larger; 829 TableBuffers larger;
830 VkDescriptorPool pool = VK_NULL_HANDLE;
831 VkDescriptorSet set = VK_NULL_HANDLE;
745 bool device_local = false; 832 bool device_local = false;
746 if (!create(larger.table, bytes, true, &device_local) || !create(larger.staging, bytes, false)) { 833 if (!create(larger.table, bytes, true, &device_local) || !create(larger.staging, bytes, false) ||
834 !make_set(pool, set)) {
747 destroy(larger); 835 destroy(larger);
748 return false; 836 return false;
749 } 837 }
750 table_device_local_ = device_local; 838 table_device_local_ = device_local;
751 retired_.push_back(table_buffers_); 839 retired_.push_back({table_buffers_, pool_, set_, retire_ticket});
752 table_buffers_ = larger; 840 table_buffers_ = larger;
841 pool_ = pool;
842 set_ = set;
753 write_table_binding(table_buffers_.table.buffer); 843 write_table_binding(table_buffers_.table.buffer);
754 full = true; 844 replaced = true;
755 grew = true; 845 ++generations_made_;
756 } 846 }
757 DescriptorTable::Changes changes = table_.take_changes(); 847 DescriptorTable::Changes changes = table_.take_changes();
758 full = full || changes.all; 848 full = full || changes.all;
@@ -760,7 +850,7 @@ private:
760 // The staging buffer is written only for buffers that were just created (first upload or growth): a later 850 // The staging buffer is written only for buffers that were just created (first upload or growth): a later
761 // flush then either updates slots with vkCmdUpdateBuffer or rebuilds from a fresh pair, so no copy still in 851 // flush then either updates slots with vkCmdUpdateBuffer or rebuilds from a fresh pair, so no copy still in
762 // flight can be reading bytes the CPU rewrites. 852 // flight can be reading bytes the CPU rewrites.
763 const bool fresh = grew || !table_uploaded_; 853 const bool fresh = replace || !table_uploaded_;
764 const auto upload_all = [&]() { 854 const auto upload_all = [&]() {
765 if (fresh) { 855 if (fresh) {
766 table_.write_to_buffer(table_buffers_.staging.mapped); 856 table_.write_to_buffer(table_buffers_.staging.mapped);
@@ -865,6 +955,8 @@ private:
865 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue; 955 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue;
866 const std::uint32_t count = features_.partially_bound ? 1 : features_.counts[b]; 956 const std::uint32_t count = features_.partially_bound ? 1 : features_.counts[b];
867 infos.emplace_back(count, fallback_info(b)); 957 infos.emplace_back(count, fallback_info(b));
958 std::fill_n(shadow_[b].begin(), count, infos.back()[0]);
959 std::fill_n(shadow_written_[b].begin(), count, std::uint8_t(1));
868 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; 960 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
869 write.dstSet = set_; 961 write.dstSet = set_;
870 write.dstBinding = b; 962 write.dstBinding = b;
@@ -944,7 +1036,20 @@ private:
944 bool existing_entries_changed_ = false; // ... and not only by entries added 1036 bool existing_entries_changed_ = false; // ... and not only by entries added
945 bool upload_in_flight_ = false; // a copy is recorded, not yet known to have reached the queue 1037 bool upload_in_flight_ = false; // a copy is recorded, not yet known to have reached the queue
946 bool force_full_upload_ = false; // the last submit failed: rebuild the whole table on the next flush 1038 bool force_full_upload_ = false; // the last submit failed: rebuild the whole table on the next flush
947 std::vector<TableBuffers> retired_; 1039 struct Generation { // what a replaced table leaves for release_retired()
1040 TableBuffers buffers;
1041 VkDescriptorPool pool = VK_NULL_HANDLE; // owns the descriptor set
1042 VkDescriptorSet set = VK_NULL_HANDLE;
1043 std::uint64_t ticket = 0;
1044 };
1045 std::vector<Generation> retired_;
1046 std::uint64_t generations_made_ = 0;
1047 bool last_flush_replaced_ = false;
1048 std::array<VkDescriptorPoolSize, bindless::bindings.size()> pool_sizes_{};
1049 std::uint32_t pool_size_count_ = 0;
1050 // Every descriptor written to the set (the table's binding aside), to fill the next generation's set.
1051 std::array<std::vector<VkDescriptorImageInfo>, bindless::bindings.size()> shadow_;
1052 std::array<std::vector<std::uint8_t>, bindless::bindings.size()> shadow_written_;
948}; 1053};
949 1054
950} // namespace leonida 1055} // namespace leonida