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:
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 f855ead32..07332a71c 100644
--- a/runtime/src/vulkan_bindless.h
+++ b/runtime/src/vulkan_bindless.h
@@ -26,8 +26,11 @@
26// 26//
27// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind 27// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind
28// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while 28// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while
29// command buffers that use the set are pending, as long as no descriptor they use changes and flush_needs_idle() 29// command buffers that use the set are pending, as long as no descriptor they use changes and
30// is false; without it the caller flushes only when none is. With partially_bound (descriptorBindingPartiallyBound) only index 0 is written at 30// flush_replaces_table() is false; without it the caller flushes only when none is. When it is true the flush
31// does not touch what pending work reads: it makes a new generation (table buffers and descriptor set, the
32// descriptors copied from a CPU shadow) and retires the old one, which release_retired() frees once the owner
33// says the submissions that could have used it are done (D3D-314). With partially_bound (descriptorBindingPartiallyBound) only index 0 is written at
31// creation; otherwise every element of every array starts as the fallback. 34// creation; otherwise every element of every array starts as the fallback.
32// 35//
33// The table of binding 2 lives in DEVICE_LOCAL memory: every divergent T#/S# lookup reads it, and a HOST_VISIBLE 36// The table of binding 2 lives in DEVICE_LOCAL memory: every divergent T#/S# lookup reads it, and a HOST_VISIBLE
@@ -371,7 +374,9 @@ public:
371 } 374 }
372 // Set 0 of every pipeline layout made for translated shaders. 375 // Set 0 of every pipeline layout made for translated shaders.
373 VkDescriptorSetLayout layout() const { return layout_; } 376 VkDescriptorSetLayout layout() const { return layout_; }
374 // Bound at set 0. The same set for the life of this object: flush() updates it in place. 377 // Bound at set 0. flush() updates it in place until flush_replaces_table() is true: then the flush makes a new
378 // set (and table buffers), so read set() again after it, when recording the commands that use it. A submission
379 // that bound the previous set keeps it, intact, until release_retired() frees it.
375 VkDescriptorSet set() const { return set_; } 380 VkDescriptorSet set() const { return set_; }
376 381
377 // The fallback images are created with undefined contents: record this once, in a command buffer that executes 382 // The fallback images are created with undefined contents: record this once, in a command buffer that executes
@@ -469,13 +474,14 @@ public:
469 } 474 }
470 const DescriptorTable& table() const { return table_; } 475 const DescriptorTable& table() const { return table_; }
471 476
472 // True when the next flush() will change entries a running shader may be probing (a live key's index replaced 477 // True when the next flush() would change entries a running shader may be probing (a live key's index replaced
473 // or a live key moved by a relocating insert), or replace the table's buffer: the caller then flushes only once 478 // or a live key moved by a relocating insert), or replace the table's buffer. flush() then does not write in
474 // no command buffer that uses the set is still executing. Without it the flush only adds entries or empties the 479 // place: it uploads the whole table to new buffers, names them in a new descriptor set, and retires the old
475 // slots of erased keys, and the copy of an entry is not atomic: a shader probing a different key could in 480 // generation, so running work keeps reading an unchanged table and nobody waits for it. Without it the flush only
476 // principle see a torn entry that matches it, which is accepted as vanishingly unlikely because the added or 481 // adds entries or empties the slots of erased keys, and the copy of an entry is not atomic: a shader probing a
477 // erased key is not live (no running work uses it). 482 // different key could in principle see a torn entry that matches it, which is accepted as vanishingly unlikely
478 bool flush_needs_idle() const { 483 // because the added or erased key is not live (no running work uses it).
484 bool flush_replaces_table() const {
479 return table_dirty_ && (existing_entries_changed_ || force_full_upload_ || 485 return table_dirty_ && (existing_entries_changed_ || force_full_upload_ ||
480 table_.size_bytes() > table_buffers_.table.size); 486 table_.size_bytes() > table_buffers_.table.size);
481 } 487 }
@@ -496,13 +502,28 @@ public:
496 } 502 }
497 503
498 // Writes the pending descriptor changes and uploads the table, in `command_buffer`, before the commands that use 504 // Writes the pending descriptor changes and uploads the table, in `command_buffer`, before the commands that use
499 // the set are recorded after it. A table that has grown gets new buffers (binding 2 is rewritten); the old ones 505 // the set are recorded after it. When flush_replaces_table() was true this starts a new generation (table
500 // stay until release_retired(). False when they cannot be made: the set then still has the table as of the last 506 // buffers, descriptor set, binding 2 and every descriptor written so far) and puts the previous one on the retire
501 // flush. 507 // list, stamped `retire_ticket`: the owner's number for "every submission made so far", which release_retired()
502 bool flush(VkCommandBuffer command_buffer) { 508 // compares with what has completed. False when the new generation cannot be made: the set then still has the
509 // table as of the last flush.
510 bool flush(VkCommandBuffer command_buffer, std::uint64_t retire_ticket = 0) {
511 bool replaced = false;
503 bool written = true; 512 bool written = true;
504 if (table_dirty_ || !table_uploaded_) written = upload_table(command_buffer); 513 const bool must_replace = flush_replaces_table();
505 if (!pending_.empty()) { 514 if (table_dirty_ || !table_uploaded_) written = upload_table(command_buffer, retire_ticket, replaced);
515 last_flush_replaced_ = replaced;
516 // The new generation could not be made: the current set is the one running work reads, so its descriptors are
517 // not written in place either. The pending writes and the table's changes stay for the next flush.
518 if (!written && must_replace) return false;
519 for (const Pending& pending : pending_) {
520 if (pending.binding >= shadow_.size() || pending.index >= shadow_[pending.binding].size()) continue;
521 shadow_[pending.binding][pending.index] = pending.info;
522 shadow_written_[pending.binding][pending.index] = 1;
523 }
524 if (replaced) {
525 write_shadow(); // includes the pending writes just folded in
526 } else if (!pending_.empty()) {
506 std::vector<VkWriteDescriptorSet> writes(pending_.size()); 527 std::vector<VkWriteDescriptorSet> writes(pending_.size());
507 for (std::size_t i = 0; i < pending_.size(); ++i) { 528 for (std::size_t i = 0; i < pending_.size(); ++i) {
508 writes[i] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; 529 writes[i] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
@@ -514,17 +535,35 @@ public:
514 writes[i].pImageInfo = &pending_[i].info; 535 writes[i].pImageInfo = &pending_[i].info;
515 } 536 }
516 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); 537 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr);
517 pending_.clear();
518 pending_slots_.clear();
519 } 538 }
539 pending_.clear();
540 pending_slots_.clear();
520 return written; 541 return written;
521 } 542 }
522 543
523 // Frees the buffers of tables that have since grown: when nothing submitted before the last flush() still runs. 544 // Frees the generations (table buffers and descriptor set) that flush() retired with a ticket up to `completed`.
524 void release_retired() { 545 // The default frees all of them: when nothing submitted before the last flush() still runs.
525 for (auto& buffers : retired_) destroy(buffers); 546 void release_retired(std::uint64_t completed = UINT64_MAX) {
526 retired_.clear(); 547 std::size_t kept = 0;
548 for (auto& generation : retired_) {
549 if (generation.ticket <= completed) {
550 destroy(generation.buffers);
551 if (generation.pool) vk_.vkDestroyDescriptorPool(device_, generation.pool, nullptr);
552 } else {
553 retired_[kept++] = generation;
554 }
555 }
556 retired_.resize(kept);
527 } 557 }
558 bool has_retired() const { return !retired_.empty(); }
559 // True when the last flush() started a new generation: its table upload is in that flush's command buffer, so
560 // work on other queues that binds the new set must run after it (the owner orders them).
561 bool last_flush_replaced() const { return last_flush_replaced_; }
562 // The ticket of the oldest retired generation (0 when there is none).
563 std::uint64_t oldest_retired_ticket() const { return retired_.empty() ? 0 : retired_.front().ticket; }
564 // How many generations flush() has made after the first, and how many wait for release_retired().
565 std::uint64_t generations_made() const { return generations_made_; }
566 std::size_t retired_count() const { return retired_.size(); }
528 567
529private: 568private:
530 struct Functions { 569 struct Functions {
@@ -596,30 +635,74 @@ private:
596 bool create_layout() { 635 bool create_layout() {
597 const bindless::SetLayout layout(features_); 636 const bindless::SetLayout layout(features_);
598 const std::uint32_t count = layout.info.bindingCount; 637 const std::uint32_t count = layout.info.bindingCount;
599 std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{}; 638 pool_size_count_ = count;
600 for (std::uint32_t i = 0; i < count; ++i) 639 for (std::uint32_t i = 0; i < count; ++i)
601 sizes[i] = {layout.entries[i].descriptorType, layout.entries[i].descriptorCount}; 640 pool_sizes_[i] = {layout.entries[i].descriptorType, layout.entries[i].descriptorCount};
602 if (vk_.vkCreateDescriptorSetLayout(device_, &layout.info, nullptr, &layout_) != VK_SUCCESS) return false; 641 if (vk_.vkCreateDescriptorSetLayout(device_, &layout.info, nullptr, &layout_) != VK_SUCCESS) return false;
642 if (!make_set(pool_, set_)) return false;
643 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
644 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue;
645 shadow_[b].assign(features_.counts[b], VkDescriptorImageInfo{});
646 shadow_written_[b].assign(features_.counts[b], 0);
647 }
648
649 for (std::uint32_t d = 0; d < dimensions; ++d) {
650 image_used_[d].assign(image_capacity(ImageDimension(d)), 0);
651 image_used_[d][0] = 1;
652 }
653 sampler_used_.assign(sampler_capacity(), 0);
654 sampler_used_[0] = 1;
655 return true;
656 }
603 657
658 // A one-set pool and its set at the layout's sizes: the first generation's, and each replacement's.
659 bool make_set(VkDescriptorPool& pool, VkDescriptorSet& set) const {
604 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; 660 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
605 pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0; 661 pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0;
606 pool_info.maxSets = 1; 662 pool_info.maxSets = 1;
607 pool_info.poolSizeCount = count; 663 pool_info.poolSizeCount = pool_size_count_;
608 pool_info.pPoolSizes = sizes.data(); 664 pool_info.pPoolSizes = pool_sizes_.data();
609 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool_) != VK_SUCCESS) return false; 665 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool) != VK_SUCCESS) {
666 pool = VK_NULL_HANDLE;
667 return false;
668 }
610 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; 669 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
611 set_info.descriptorPool = pool_; 670 set_info.descriptorPool = pool;
612 set_info.descriptorSetCount = 1; 671 set_info.descriptorSetCount = 1;
613 set_info.pSetLayouts = &layout_; 672 set_info.pSetLayouts = &layout_;
614 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set_) != VK_SUCCESS) return false; 673 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set) == VK_SUCCESS) return true;
674 vk_.vkDestroyDescriptorPool(device_, pool, nullptr);
675 pool = VK_NULL_HANDLE;
676 set = VK_NULL_HANDLE;
677 return false;
678 }
615 679
616 for (std::uint32_t d = 0; d < dimensions; ++d) { 680 // Writes every descriptor written so far (the shadow) into the current set, one update per run of written
617 image_used_[d].assign(image_capacity(ImageDimension(d)), 0); 681 // elements: the set was just made, and unwritten elements stay unwritten, as partially_bound allows.
618 image_used_[d][0] = 1; 682 void write_shadow() const {
683 std::vector<VkWriteDescriptorSet> writes;
684 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
685 const auto& written = shadow_written_[b];
686 for (std::size_t i = 0; i < written.size();) {
687 if (!written[i]) {
688 ++i;
689 continue;
690 }
691 std::size_t end = i;
692 while (end < written.size() && written[end]) ++end;
693 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
694 write.dstSet = set_;
695 write.dstBinding = b;
696 write.dstArrayElement = std::uint32_t(i);
697 write.descriptorCount = std::uint32_t(end - i);
698 write.descriptorType = bindless::bindings[b].type;
699 write.pImageInfo = &shadow_[b][i];
700 writes.push_back(write);
701 i = end;
702 }
619 } 703 }
620 sampler_used_.assign(sampler_capacity(), 0); 704 if (!writes.empty())
621 sampler_used_[0] = 1; 705 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr);
622 return true;
623 } 706 }
624 707
625 // One 1x1 image for each image type and component class, and the views of them every binding needs. 708 // One 1x1 image for each image type and component class, and the views of them every binding needs.
@@ -754,24 +837,31 @@ private:
754 837
755 // Uploads what changed since the last call: the whole table when it grew (the mask moved) or was never uploaded, 838 // Uploads what changed since the last call: the whole table when it grew (the mask moved) or was never uploaded,
756 // the changed slots otherwise. The copy and its barrier go into `command_buffer`, before the draws. 839 // the changed slots otherwise. The copy and its barrier go into `command_buffer`, before the draws.
757 bool upload_table(VkCommandBuffer command_buffer) { 840 bool upload_table(VkCommandBuffer command_buffer, std::uint64_t retire_ticket, bool& replaced) {
758 if (!command_buffer) return false; 841 if (!command_buffer) return false;
759 const std::size_t bytes = table_.size_bytes(); 842 const std::size_t bytes = table_.size_bytes();
760 bool full = force_full_upload_ || !table_uploaded_ || bytes > table_buffers_.table.size; 843 // A table that grew, or whose live entries change, goes to a new generation; the old one may still be read.
761 bool grew = false; 844 const bool replace = bytes > table_buffers_.table.size ||
762 if (bytes > table_buffers_.table.size) { 845 (table_uploaded_ && (existing_entries_changed_ || force_full_upload_));
846 bool full = force_full_upload_ || !table_uploaded_ || replace;
847 if (replace) {
763 TableBuffers larger; 848 TableBuffers larger;
849 VkDescriptorPool pool = VK_NULL_HANDLE;
850 VkDescriptorSet set = VK_NULL_HANDLE;
764 bool device_local = false; 851 bool device_local = false;
765 if (!create(larger.table, bytes, true, &device_local) || !create(larger.staging, bytes, false)) { 852 if (!create(larger.table, bytes, true, &device_local) || !create(larger.staging, bytes, false) ||
853 !make_set(pool, set)) {
766 destroy(larger); 854 destroy(larger);
767 return false; 855 return false;
768 } 856 }
769 table_device_local_ = device_local; 857 table_device_local_ = device_local;
770 retired_.push_back(table_buffers_); 858 retired_.push_back({table_buffers_, pool_, set_, retire_ticket});
771 table_buffers_ = larger; 859 table_buffers_ = larger;
860 pool_ = pool;
861 set_ = set;
772 write_table_binding(table_buffers_.table.buffer); 862 write_table_binding(table_buffers_.table.buffer);
773 full = true; 863 replaced = true;
774 grew = true; 864 ++generations_made_;
775 } 865 }
776 DescriptorTable::Changes changes = table_.take_changes(); 866 DescriptorTable::Changes changes = table_.take_changes();
777 full = full || changes.all; 867 full = full || changes.all;
@@ -779,7 +869,7 @@ private:
779 // The staging buffer is written only for buffers that were just created (first upload or growth): a later 869 // The staging buffer is written only for buffers that were just created (first upload or growth): a later
780 // flush then either updates slots with vkCmdUpdateBuffer or rebuilds from a fresh pair, so no copy still in 870 // flush then either updates slots with vkCmdUpdateBuffer or rebuilds from a fresh pair, so no copy still in
781 // flight can be reading bytes the CPU rewrites. 871 // flight can be reading bytes the CPU rewrites.
782 const bool fresh = grew || !table_uploaded_; 872 const bool fresh = replace || !table_uploaded_;
783 const auto upload_all = [&]() { 873 const auto upload_all = [&]() {
784 if (fresh) { 874 if (fresh) {
785 table_.write_to_buffer(table_buffers_.staging.mapped); 875 table_.write_to_buffer(table_buffers_.staging.mapped);
@@ -884,6 +974,8 @@ private:
884 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue; 974 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue;
885 const std::uint32_t count = features_.partially_bound ? 1 : features_.counts[b]; 975 const std::uint32_t count = features_.partially_bound ? 1 : features_.counts[b];
886 infos.emplace_back(count, fallback_info(b)); 976 infos.emplace_back(count, fallback_info(b));
977 std::fill_n(shadow_[b].begin(), count, infos.back()[0]);
978 std::fill_n(shadow_written_[b].begin(), count, std::uint8_t(1));
887 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; 979 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
888 write.dstSet = set_; 980 write.dstSet = set_;
889 write.dstBinding = b; 981 write.dstBinding = b;
@@ -964,7 +1056,20 @@ private:
964 bool existing_entries_changed_ = false; // ... and not only by entries added 1056 bool existing_entries_changed_ = false; // ... and not only by entries added
965 bool upload_in_flight_ = false; // a copy is recorded, not yet known to have reached the queue 1057 bool upload_in_flight_ = false; // a copy is recorded, not yet known to have reached the queue
966 bool force_full_upload_ = false; // the last submit failed: rebuild the whole table on the next flush 1058 bool force_full_upload_ = false; // the last submit failed: rebuild the whole table on the next flush
967 std::vector<TableBuffers> retired_; 1059 struct Generation { // what a replaced table leaves for release_retired()
1060 TableBuffers buffers;
1061 VkDescriptorPool pool = VK_NULL_HANDLE; // owns the descriptor set
1062 VkDescriptorSet set = VK_NULL_HANDLE;
1063 std::uint64_t ticket = 0;
1064 };
1065 std::vector<Generation> retired_;
1066 std::uint64_t generations_made_ = 0;
1067 bool last_flush_replaced_ = false;
1068 std::array<VkDescriptorPoolSize, bindless::bindings.size()> pool_sizes_{};
1069 std::uint32_t pool_size_count_ = 0;
1070 // Every descriptor written to the set (the table's binding aside), to fill the next generation's set.
1071 std::array<std::vector<VkDescriptorImageInfo>, bindless::bindings.size()> shadow_;
1072 std::array<std::vector<std::uint8_t>, bindless::bindings.size()> shadow_written_;
968}; 1073};
969 1074
970} // namespace leonida 1075} // namespace leonida