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:
authorkawaii-shakal <deusex1998@gmail.com>2026-11-26 05:27:39 +0500
committerkawaii-shakal <deusex1998@gmail.com>2026-11-26 05:27:39 +0500
commita42bffdc3e0d30347eedb3c433405e9a141cc06b (patch)
tree600a611377c0575bbc7a967fd5894e9e6e4e32f6 /runtime/src/vulkan_bindless.h
parentaab7653566e4ecf80a8026cfbd48e74966a4d04c (diff)
parentc50ed94b35b37b697ab9c5fd176f3738480b4db5 (diff)
Merge origin/main into heavy-pipeline-cache
main's per-pipeline compute journal (pipeline_cache_journal.h) now persists every compute build and survives a crash, so the compute build takes main's build_compute_pipeline and the heavy cache keeps graphics pipelines only. CMake keeps both test sets; docs and the create_pipeline comment say so. 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.h192
1 files changed, 180 insertions, 12 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h
index e72225396..da6d680f0 100644
--- a/runtime/src/vulkan_bindless.h
+++ b/runtime/src/vulkan_bindless.h
@@ -25,6 +25,14 @@
25// and samplers it adds and keeps them alive while they are in the set; this class owns only the fallbacks and the 25// and samplers it adds and keeps them alive while they are in the set; this class owns only the fallbacks and the
26// table's buffer. 26// table's buffer.
27// 27//
28// The T#'s own type is the truth: the index was given out in ITS dimension's space, so a shader reading it through
29// another dimension's array (a 2D instruction on a 2D-array or cube T#, say) must not use it blindly. Every entry
30// therefore carries, next to the index, which arrays the index is bound in (descriptor_table.h's kind bits), and a
31// translated module of the tagged contract reads that word: it uses the index only in an array the entry names and
32// otherwise takes that array's fallback. For the one mismatch the hardware reads rather than drops - a 2D
33// instruction on a 2D-array or cube T#, which addresses slice 0 - add_image's MirrorViews bind a view of that slice
34// at the same index in the 2D arrays and name 2D in the kind, so the instruction finds it.
35//
28// The fallbacks stand for the console's null descriptors (a zero T#, a T# whose image the layer refused or never 36// The fallbacks stand for the console's null descriptors (a zero T#, a T# whose image the layer refused or never
29// saw): each is of the kind the instruction asks for, since the translator picks the binding by the instruction's 37// saw): each is of the kind the instruction asks for, since the translator picks the binding by the instruction's
30// dimension and the view by the T#'s numeric class, and a miss at any of them reads zeros. On the console a store or 38// dimension and the view by the T#'s numeric class, and a miss at any of them reads zeros. On the console a store or
@@ -68,6 +76,7 @@
68 76
69#include <algorithm> 77#include <algorithm>
70#include <array> 78#include <array>
79#include <cassert>
71#include <cstdint> 80#include <cstdint>
72#include <cstring> 81#include <cstring>
73#include <functional> 82#include <functional>
@@ -398,6 +407,8 @@ public:
398 BindlessSet& operator=(const BindlessSet&) = delete; 407 BindlessSet& operator=(const BindlessSet&) = delete;
399 408
400 bool ok() const { return ok_; } 409 bool ok() const { return ok_; }
410 // What the set layout was made from (the pipeline journal names compute pipelines by it).
411 const BindlessFeatures& features() const { return features_; }
401 bool supports_ms_class(std::uint32_t component_class) const { 412 bool supports_ms_class(std::uint32_t component_class) const {
402 return component_class < classes && features_.ms_samples[component_class] != VK_SAMPLE_COUNT_1_BIT; 413 return component_class < classes && features_.ms_samples[component_class] != VK_SAMPLE_COUNT_1_BIT;
403 } 414 }
@@ -461,29 +472,109 @@ public:
461 &done, 0, nullptr, 0, nullptr); 472 &done, 0, nullptr, 0, nullptr);
462 } 473 }
463 474
475 // A second view of the same image in the 2D arrays: the slice a 2D instruction reads through a 2D-array or cube
476 // T# (its first layer). The hardware addresses such a read as slice 0, and the translated shader reads binding 0
477 // with the identity the table gives, so the runtime binds this copy at the image's own index and records that
478 // index as valid in the 2D array too (kind_sampled_bit), owned by this image (mirror_of): another dimension's
479 // image with the same slot number does not inherit it. The storage views follow the same rule when the image has
480 // them, so a 2D store or atomic lands on layer 0.
481 struct MirrorViews {
482 VkImageView sampled = VK_NULL_HANDLE;
483 VkImageLayout sampled_layout = VK_IMAGE_LAYOUT_GENERAL;
484 StorageViews storage{};
485 };
486
464 // Gives an image an index in its dimension's bindings: 0 when the dimension is full. `sampled` goes to the 487 // Gives an image an index in its dimension's bindings: 0 when the dimension is full. `sampled` goes to the
465 // sampled array in `sampled_layout` (the layout the image is in when shaders read it); the storage views go to 488 // sampled array in `sampled_layout` (the layout the image is in when shaders read it); the storage views go to
466 // the storage arrays in GENERAL. For MS views, sampled_class selects the float, uint or sint sampled array. 489 // the storage arrays in GENERAL. For MS views, sampled_class selects the float, uint or sint sampled array.
467 // The caller maps the T# to the index with insert_key(). Takes effect at flush(). 490 // `mirror` adds the same index in the 2D arrays (a 2D-array or cube image's slice 0): the slot must then be free
491 // in both index spaces. `kind_out`, when given, receives what the index is bound in (descriptor_table.h's kind
492 // bits), which the caller records with insert_key. The caller maps the T# to the index with insert_key(). Takes
493 // effect at flush().
468 std::uint32_t add_image(ImageDimension dimension, VkImageView sampled, VkImageLayout sampled_layout, 494 std::uint32_t add_image(ImageDimension dimension, VkImageView sampled, VkImageLayout sampled_layout,
469 const StorageViews& storage = {}, std::uint32_t sampled_class = 0) { 495 const StorageViews& storage = {}, std::uint32_t sampled_class = 0,
496 const MirrorViews* mirror = nullptr, std::uint32_t* kind_out = nullptr) {
470 const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui; 497 const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui;
471 if (!stores && bindless::sampled_binding(dimension) == bindless::no_binding) return 0; 498 if (!stores && bindless::sampled_binding(dimension) == bindless::no_binding) return 0;
472 if (bindless::multisampled(dimension) && (stores || !supports_ms_class(sampled_class))) return 0; 499 if (bindless::multisampled(dimension) && (stores || !supports_ms_class(sampled_class))) return 0;
473 auto& used = image_used_[std::uint32_t(dimension)]; 500 auto& used = image_used_[std::uint32_t(dimension)];
474 const std::uint32_t low_end = std::min(std::uint32_t(used.size()), storage_capacity(dimension)); 501 const std::uint32_t low_end = std::min(std::uint32_t(used.size()), storage_capacity(dimension));
475 std::uint32_t index = stores ? 0 : allocate(used, low_end, std::uint32_t(used.size())); 502 std::vector<std::uint8_t>* used_2d = nullptr; // the mirror's index space, when there is one
476 if (!index) index = allocate(used, 1, low_end); 503 if (mirror && dimension != ImageDimension::dim_2d && mirror->sampled) {
477 if (index) write_image(dimension, index, sampled, sampled_layout, storage, sampled_class); 504 used_2d = &image_used_[std::uint32_t(ImageDimension::dim_2d)];
505 if (mirror->sampled_layout == VK_IMAGE_LAYOUT_UNDEFINED) used_2d = nullptr;
506 }
507 // A store through the array T# and one through its 2D slice both have to land on a view, so an index with
508 // storage views must be inside this dimension's storage arrays and, when the mirror has storage views too,
509 // inside the 2D arrays' (their range can be the shorter of the two).
510 const bool any_stores = stores || mirror_storage(mirror);
511 const std::uint32_t mirrored_storage_end = std::min(stores ? low_end : std::uint32_t(used.size()),
512 storage_capacity(ImageDimension::dim_2d));
513 std::uint32_t index = 0;
514 if (used_2d) {
515 index = any_stores ? allocate_pair(used, *used_2d, 1, mirrored_storage_end)
516 : allocate_pair(used, *used_2d, low_end, std::uint32_t(used.size()));
517 if (!index && !any_stores) index = allocate_pair(used, *used_2d, 1, low_end);
518 if (!index) {
519 // No shared slot: the image still gets its own dimension's slot, unmirrored (a 2D read then finds
520 // the fallback). A full array is what returns 0.
521 used_2d = nullptr;
522 }
523 }
524 if (!used_2d) {
525 index = stores ? allocate(used, 1, low_end) : allocate(used, low_end, std::uint32_t(used.size()));
526 if (!index) index = allocate(used, 1, low_end);
527 }
528 if (!index) return 0;
529 assert(!used_2d || !any_stores || index < storage_capacity(ImageDimension::dim_2d));
530 write_image(dimension, index, sampled, sampled_layout, storage, sampled_class);
531 if (index < storage_used_[std::uint32_t(dimension)].size())
532 storage_used_[std::uint32_t(dimension)][index] = stores ? 1 : 0;
533 if (used_2d) {
534 write_image(ImageDimension::dim_2d, index, mirror->sampled, mirror->sampled_layout, mirror->storage, 0);
535 used_2d->at(index) = in_use;
536 // The 2D slot is this image's mirror, and this image is its owner: the slot number alone does not say
537 // so, because every dimension's index space is dense and independent.
538 if (index < mirror_owner_.size()) {
539 mirror_owner_[index] = std::uint8_t(std::uint32_t(dimension) + 1);
540 mirror_storage_used_[index] = mirror_storage(mirror) ? 1 : 0;
541 }
542 auto& storage_2d = storage_used_[std::uint32_t(ImageDimension::dim_2d)];
543 if (index < storage_2d.size()) storage_2d[index] = mirror_storage(mirror) ? 1 : 0;
544 }
545 if (kind_out) {
546 *kind_out = kind_sampled_bit(std::uint32_t(dimension)) |
547 (stores ? kind_storage_bit(std::uint32_t(dimension)) : 0) |
548 (used_2d ? (kind_sampled_bit(std::uint32_t(ImageDimension::dim_2d)) |
549 (mirror_storage(mirror) ? kind_storage_bit(std::uint32_t(ImageDimension::dim_2d))
550 : 0))
551 : 0);
552 }
478 return index; 553 return index;
479 } 554 }
480 555
481 // New views or a new layout for an index add_image returned (a texture's state changed, say). 556 // New views or a new layout for an index add_image returned (a texture's state changed, say). `mirror` is
557 // refreshed too when the image was added with one (mirror_of says whose 2D slot it is). The slot bookkeeping
558 // follows what was written, and `kind_out`, when given, receives the index's kind word afterwards for a
559 // re-insert: the table keeps the kind it was given, so a caller that adds or drops storage views re-records it.
482 void update_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout, 560 void update_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout,
483 const StorageViews& storage = {}, std::uint32_t sampled_class = 0) { 561 const StorageViews& storage = {}, std::uint32_t sampled_class = 0,
562 const MirrorViews* mirror = nullptr, std::uint32_t* kind_out = nullptr) {
484 const auto& used = image_used_[std::uint32_t(dimension)]; 563 const auto& used = image_used_[std::uint32_t(dimension)];
485 if (index && index < used.size() && used[index] == in_use) 564 const bool live = index && index < used.size() && used[index] == in_use;
565 if (live) {
486 write_image(dimension, index, sampled, sampled_layout, storage, sampled_class); 566 write_image(dimension, index, sampled, sampled_layout, storage, sampled_class);
567 const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui;
568 if (index < storage_used_[std::uint32_t(dimension)].size())
569 storage_used_[std::uint32_t(dimension)][index] = stores ? 1 : 0;
570 }
571 if (live && mirror && mirror->sampled && mirror_of(dimension, index)) {
572 write_image(ImageDimension::dim_2d, index, mirror->sampled, mirror->sampled_layout, mirror->storage, 0);
573 if (index < mirror_storage_used_.size()) mirror_storage_used_[index] = mirror_storage(mirror) ? 1 : 0;
574 auto& storage_2d = storage_used_[std::uint32_t(ImageDimension::dim_2d)];
575 if (index < storage_2d.size()) storage_2d[index] = mirror_storage(mirror) ? 1 : 0;
576 }
577 if (kind_out) *kind_out = image_kind_at(dimension, index);
487 } 578 }
488 579
489 // Returns the index to the fallback and frees it. The caller removes the image's keys with erase_key() first. 580 // Returns the index to the fallback and frees it. The caller removes the image's keys with erase_key() first.
@@ -495,7 +586,22 @@ public:
495 if (!index || index >= used.size() || used[index] != in_use) return; 586 if (!index || index >= used.size() || used[index] != in_use) return;
496 write_image(dimension, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}, 0); 587 write_image(dimension, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}, 0);
497 used[index] = table_dirty_ ? held : 0; 588 used[index] = table_dirty_ ? held : 0;
589 if (index < storage_used_[std::uint32_t(dimension)].size())
590 storage_used_[std::uint32_t(dimension)][index] = 0;
498 if (table_dirty_) held_images_.push_back({dimension, index}); 591 if (table_dirty_) held_images_.push_back({dimension, index});
592 // Its 2D copy goes back to the fallback as well (add_image's mirror), when this image is the one that owns
593 // that slot: another dimension's image at the same slot number has its own index space and keeps its 2D
594 // descriptor (and, if it is a 2D-array image, its own mirror there).
595 if (mirror_of(dimension, index)) {
596 auto& used_2d = image_used_[std::uint32_t(ImageDimension::dim_2d)];
597 write_image(ImageDimension::dim_2d, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}, 0);
598 used_2d[index] = table_dirty_ ? held : 0;
599 mirror_owner_[index] = 0;
600 mirror_storage_used_[index] = 0;
601 auto& storage_2d = storage_used_[std::uint32_t(ImageDimension::dim_2d)];
602 if (index < storage_2d.size()) storage_2d[index] = 0;
603 if (table_dirty_) held_images_.push_back({ImageDimension::dim_2d, index});
604 }
499 } 605 }
500 606
501 // 0 when the sampler array is full. 607 // 0 when the sampler array is full.
@@ -512,15 +618,37 @@ public:
512 if (table_dirty_) held_samplers_.push_back(index); 618 if (table_dirty_) held_samplers_.push_back(index);
513 } 619 }
514 620
515 // T# and S# keys to the indices above: the table of binding 2 (descriptor_table.h). Changes reach the shaders 621 // What an index of `dimension` is bound in, from what add_image wrote: that dimension's sampled bit, its storage
516 // at flush(). 622 // bit when the slot holds storage views, and the 2D bits when this image owns the slot at the same number in the
517 bool insert_key(const DescriptorKey& key, std::uint32_t index) { 623 // 2D arrays as its mirror (mirror_of).
518 const auto result = table_.insert_or_update(key.data(), index); 624 std::uint32_t image_kind_at(ImageDimension dimension, std::uint32_t index) const {
625 const auto set = [&](const std::vector<std::uint8_t>& used) {
626 return index < used.size() && used[index] != 0;
627 };
628 const std::uint32_t d = std::uint32_t(dimension);
629 std::uint32_t kind = kind_sampled_bit(d) | (set(storage_used_[d]) ? kind_storage_bit(d) : 0);
630 if (mirror_of(dimension, index)) {
631 kind |= kind_sampled_bit(std::uint32_t(ImageDimension::dim_2d));
632 if (set(mirror_storage_used_)) kind |= kind_storage_bit(std::uint32_t(ImageDimension::dim_2d));
633 }
634 return kind;
635 }
636
637 // T# and S# keys to the indices above: the table of binding 2 (descriptor_table.h). `kind` is what the index is
638 // bound in, for the modules that read it (descriptor_table.h's kind bits); a sampler's entry has none. Changes
639 // reach the shaders at flush().
640 bool insert_key(const DescriptorKey& key, std::uint32_t index, std::uint32_t kind = 0) {
641 const auto result = table_.insert_or_update(key.data(), index, kind);
519 table_dirty_ |= result != DescriptorTable::InsertResult::unchanged; 642 table_dirty_ |= result != DescriptorTable::InsertResult::unchanged;
520 existing_entries_changed_ |= result == DescriptorTable::InsertResult::replaced || 643 existing_entries_changed_ |= result == DescriptorTable::InsertResult::replaced ||
521 result == DescriptorTable::InsertResult::added_with_relocation; 644 result == DescriptorTable::InsertResult::added_with_relocation;
522 return true; 645 return true;
523 } 646 }
647 // The T#'s own dimension, whose kind the set derives from the index it gave out (image_kind_at): a caller that
648 // knows the T#'s TYPE needs to pass no bits.
649 bool insert_key(const DescriptorKey& key, std::uint32_t index, ImageDimension dimension) {
650 return insert_key(key, index, image_kind_at(dimension, index));
651 }
524 // Erasing empties only the key's own slot; no other entry moves (DescriptorTable::erase), so it needs no idle. 652 // Erasing empties only the key's own slot; no other entry moves (DescriptorTable::erase), so it needs no idle.
525 // The caller erases a key once no submitted work uses it (its texture is gone), like D3D12 requires. 653 // The caller erases a key once no submitted work uses it (its texture is gone), like D3D12 requires.
526 bool erase_key(const DescriptorKey& key) { 654 bool erase_key(const DescriptorKey& key) {
@@ -754,6 +882,34 @@ private:
754 return 0; 882 return 0;
755 } 883 }
756 884
885 // The first index in [begin, end) free in both arrays, marked used in the first; 0 when there is none. The
886 // second array's size bounds the range: a mirrored index must exist in it (its 2D slot).
887 static std::uint32_t allocate_pair(std::vector<std::uint8_t>& used, std::vector<std::uint8_t>& other,
888 std::uint32_t begin, std::uint32_t end) {
889 end = std::min(end, std::uint32_t(other.size()));
890 for (std::uint32_t i = begin; i < end; ++i) {
891 if (used[i] || other[i]) continue;
892 used[i] = 1;
893 return i;
894 }
895 return 0;
896 }
897
898 // A mirror is worth binding only when it has a view; its storage views make the 2D storage arrays valid too.
899 static bool mirror_storage(const MirrorViews* mirror) {
900 return mirror && (mirror->storage.as_float || mirror->storage.as_uint || mirror->storage.as_sint ||
901 mirror->storage.r32ui);
902 }
903
904 // True when the 2D slot at `index` is the mirror of the image whose index is `index` in `dimension` (add_image's
905 // MirrorViews). A bare per-slot flag cannot say so: every dimension's index space is dense and independent, so
906 // the Nth image of one dimension shares its slot number with the Nth 2D-array image's mirror slot in the 2D
907 // arrays. The owner is the dimension + 1 (0: no mirror).
908 bool mirror_of(ImageDimension dimension, std::uint32_t index) const {
909 if (dimension == ImageDimension::dim_2d) return false;
910 return index < mirror_owner_.size() && mirror_owner_[index] == std::uint8_t(std::uint32_t(dimension) + 1);
911 }
912
757 std::uint32_t memory_type(std::uint32_t allowed, VkMemoryPropertyFlags wanted) const { 913 std::uint32_t memory_type(std::uint32_t allowed, VkMemoryPropertyFlags wanted) const {
758 for (std::uint32_t i = 0; i < memory_.memoryTypeCount; ++i) 914 for (std::uint32_t i = 0; i < memory_.memoryTypeCount; ++i)
759 if ((allowed & (1u << i)) && (memory_.memoryTypes[i].propertyFlags & wanted) == wanted) return i; 915 if ((allowed & (1u << i)) && (memory_.memoryTypes[i].propertyFlags & wanted) == wanted) return i;
@@ -778,6 +934,10 @@ private:
778 image_used_[d].assign(image_capacity(ImageDimension(d)), 0); 934 image_used_[d].assign(image_capacity(ImageDimension(d)), 0);
779 image_used_[d][0] = 1; 935 image_used_[d][0] = 1;
780 } 936 }
937 for (std::uint32_t d = 0; d < dimensions; ++d)
938 storage_used_[d].assign(image_capacity(ImageDimension(d)), 0);
939 mirror_owner_.assign(image_capacity(ImageDimension::dim_2d), 0);
940 mirror_storage_used_.assign(image_capacity(ImageDimension::dim_2d), 0);
781 sampler_used_.assign(sampler_capacity(), 0); 941 sampler_used_.assign(sampler_capacity(), 0);
782 sampler_used_[0] = 1; 942 sampler_used_[0] = 1;
783 return true; 943 return true;
@@ -1282,6 +1442,14 @@ private:
1282 VkSampler fallback_sampler_ = VK_NULL_HANDLE; 1442 VkSampler fallback_sampler_ = VK_NULL_HANDLE;
1283 bool fallbacks_ready_ = false; 1443 bool fallbacks_ready_ = false;
1284 std::array<std::vector<std::uint8_t>, dimensions> image_used_; 1444 std::array<std::vector<std::uint8_t>, dimensions> image_used_;
1445 // Which of those slots also hold storage views (add_image's `storage`), and which 2D slots hold the mirror of an
1446 // image in another dimension (add_image's MirrorViews): removing that image frees the 2D slot too. insert_key
1447 // reads both to say which arrays an index is bound in. mirror_owner_ says whose mirror each of those 2D slots is
1448 // (dimension + 1, 0 for none) and mirror_storage_used_ whether it has storage views: the 2D slot numbers line up
1449 // with another dimension's indices, so the slot alone does not identify its owner.
1450 std::array<std::vector<std::uint8_t>, dimensions> storage_used_;
1451 std::vector<std::uint8_t> mirror_owner_;
1452 std::vector<std::uint8_t> mirror_storage_used_;
1285 std::vector<std::uint8_t> sampler_used_; 1453 std::vector<std::uint8_t> sampler_used_;
1286 std::vector<std::pair<ImageDimension, std::uint32_t>> held_images_; // removed, not yet free (remove_image) 1454 std::vector<std::pair<ImageDimension, std::uint32_t>> held_images_; // removed, not yet free (remove_image)
1287 std::vector<std::uint32_t> held_samplers_; 1455 std::vector<std::uint32_t> held_samplers_;