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.h322
1 files changed, 265 insertions, 57 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h
index 589cadffd..da6d680f0 100644
--- a/runtime/src/vulkan_bindless.h
+++ b/runtime/src/vulkan_bindless.h
@@ -25,6 +25,22 @@
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//
36// The fallbacks stand for the console's null descriptors (a zero T#, a T# whose image the layer refused or never
37// saw): each is of the kind the instruction asks for, since the translator picks the binding by the instruction's
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
39// atomic through a null descriptor is dropped and a later read still gives zeros. So the storage fallbacks never
40// share an image with the sampled ones (a store through a null UAV would otherwise show in every null SRV of that
41// dimension), and with BindlessFeatures::null_descriptor (VK_EXT_robustness2 nullDescriptor) they are null
42// descriptors, whose stores Vulkan discards and whose reads are zero.
43//
28// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind 44// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind
29// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while 45// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while
30// command buffers that use the set are pending, as long as no descriptor they use changes and 46// command buffers that use the set are pending, as long as no descriptor they use changes and
@@ -60,6 +76,7 @@
60 76
61#include <algorithm> 77#include <algorithm>
62#include <array> 78#include <array>
79#include <cassert>
63#include <cstdint> 80#include <cstdint>
64#include <cstring> 81#include <cstring>
65#include <functional> 82#include <functional>
@@ -90,6 +107,10 @@ constexpr std::array<std::uint32_t, binding_count> default_counts();
90struct BindlessFeatures { 107struct BindlessFeatures {
91 bool update_after_bind = false; 108 bool update_after_bind = false;
92 bool partially_bound = false; 109 bool partially_bound = false;
110 // VK_EXT_robustness2's nullDescriptor is enabled on the device: the storage arrays' fallbacks are null
111 // descriptors (stores and atomics through them are discarded, loads read zero). Without it they are 1x1 images
112 // of their own, apart from the sampled fallbacks, which no store reaches.
113 bool null_descriptor = false;
93 // Fallback sample counts for RGBA8_UNORM, R32_UINT, R32_SINT. A single sample omits that class's MS bindings. 114 // Fallback sample counts for RGBA8_UNORM, R32_UINT, R32_SINT. A single sample omits that class's MS bindings.
94 std::array<VkSampleCountFlagBits, 3> ms_samples{ 115 std::array<VkSampleCountFlagBits, 3> ms_samples{
95 VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT}; 116 VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT};
@@ -372,10 +393,12 @@ public:
372 destroy(table_buffers_); 393 destroy(table_buffers_);
373 if (fallback_sampler_) vk_.vkDestroySampler(device_, fallback_sampler_, nullptr); 394 if (fallback_sampler_) vk_.vkDestroySampler(device_, fallback_sampler_, nullptr);
374 for (const VkImageView view : fallback_views_) vk_.vkDestroyImageView(device_, view, nullptr); 395 for (const VkImageView view : fallback_views_) vk_.vkDestroyImageView(device_, view, nullptr);
375 for (const auto& image : fallback_images_) { 396 const auto destroy_image = [&](const FallbackImage& image) {
376 if (image.image) vk_.vkDestroyImage(device_, image.image, nullptr); 397 if (image.image) vk_.vkDestroyImage(device_, image.image, nullptr);
377 if (image.memory) vk_.vkFreeMemory(device_, image.memory, nullptr); 398 if (image.memory) vk_.vkFreeMemory(device_, image.memory, nullptr);
378 } 399 };
400 for (const auto& image : fallback_images_) destroy_image(image);
401 for (const auto& image : storage_fallback_images_) destroy_image(image);
379 if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); // frees the set 402 if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); // frees the set
380 if (layout_) vk_.vkDestroyDescriptorSetLayout(device_, layout_, nullptr); 403 if (layout_) vk_.vkDestroyDescriptorSetLayout(device_, layout_, nullptr);
381 } 404 }
@@ -384,6 +407,8 @@ public:
384 BindlessSet& operator=(const BindlessSet&) = delete; 407 BindlessSet& operator=(const BindlessSet&) = delete;
385 408
386 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_; }
387 bool supports_ms_class(std::uint32_t component_class) const { 412 bool supports_ms_class(std::uint32_t component_class) const {
388 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;
389 } 414 }
@@ -418,8 +443,12 @@ public:
418 void record_fallback_init(VkCommandBuffer command_buffer) { 443 void record_fallback_init(VkCommandBuffer command_buffer) {
419 fallbacks_ready_ = true; 444 fallbacks_ready_ = true;
420 std::vector<VkImageMemoryBarrier> barriers; 445 std::vector<VkImageMemoryBarrier> barriers;
421 for (const auto& image : fallback_images_) { 446 std::vector<const FallbackImage*> images;
422 if (!image.image) continue; // unsupported multisample component class 447 for (const auto& image : fallback_images_) images.push_back(&image);
448 for (const auto& image : storage_fallback_images_) images.push_back(&image);
449 for (const FallbackImage* fallback : images) {
450 const FallbackImage& image = *fallback;
451 if (!image.image) continue; // an unused class, or a storage fallback that is a null descriptor
423 VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; 452 VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
424 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; 453 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
425 barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; 454 barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -443,29 +472,109 @@ public:
443 &done, 0, nullptr, 0, nullptr); 472 &done, 0, nullptr, 0, nullptr);
444 } 473 }
445 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
446 // 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
447 // 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
448 // 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.
449 // 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().
450 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,
451 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) {
452 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;
453 if (!stores && bindless::sampled_binding(dimension) == bindless::no_binding) return 0; 498 if (!stores && bindless::sampled_binding(dimension) == bindless::no_binding) return 0;
454 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;
455 auto& used = image_used_[std::uint32_t(dimension)]; 500 auto& used = image_used_[std::uint32_t(dimension)];
456 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));
457 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
458 if (!index) index = allocate(used, 1, low_end); 503 if (mirror && dimension != ImageDimension::dim_2d && mirror->sampled) {
459 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 }
460 return index; 553 return index;
461 } 554 }
462 555
463 // 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.
464 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,
465 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) {
466 const auto& used = image_used_[std::uint32_t(dimension)]; 563 const auto& used = image_used_[std::uint32_t(dimension)];
467 if (index && index < used.size() && used[index] == in_use) 564 const bool live = index && index < used.size() && used[index] == in_use;
565 if (live) {
468 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);
469 } 578 }
470 579
471 // 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.
@@ -477,7 +586,22 @@ public:
477 if (!index || index >= used.size() || used[index] != in_use) return; 586 if (!index || index >= used.size() || used[index] != in_use) return;
478 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);
479 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;
480 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 }
481 } 605 }
482 606
483 // 0 when the sampler array is full. 607 // 0 when the sampler array is full.
@@ -494,15 +618,37 @@ public:
494 if (table_dirty_) held_samplers_.push_back(index); 618 if (table_dirty_) held_samplers_.push_back(index);
495 } 619 }
496 620
497 // 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
498 // 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
499 bool insert_key(const DescriptorKey& key, std::uint32_t index) { 623 // 2D arrays as its mirror (mirror_of).
500 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);
501 table_dirty_ |= result != DescriptorTable::InsertResult::unchanged; 642 table_dirty_ |= result != DescriptorTable::InsertResult::unchanged;
502 existing_entries_changed_ |= result == DescriptorTable::InsertResult::replaced || 643 existing_entries_changed_ |= result == DescriptorTable::InsertResult::replaced ||
503 result == DescriptorTable::InsertResult::added_with_relocation; 644 result == DescriptorTable::InsertResult::added_with_relocation;
504 return true; 645 return true;
505 } 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 }
506 // 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.
507 // 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.
508 bool erase_key(const DescriptorKey& key) { 654 bool erase_key(const DescriptorKey& key) {
@@ -736,6 +882,34 @@ private:
736 return 0; 882 return 0;
737 } 883 }
738 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
739 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 {
740 for (std::uint32_t i = 0; i < memory_.memoryTypeCount; ++i) 914 for (std::uint32_t i = 0; i < memory_.memoryTypeCount; ++i)
741 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;
@@ -760,6 +934,10 @@ private:
760 image_used_[d].assign(image_capacity(ImageDimension(d)), 0); 934 image_used_[d].assign(image_capacity(ImageDimension(d)), 0);
761 image_used_[d][0] = 1; 935 image_used_[d][0] = 1;
762 } 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);
763 sampler_used_.assign(sampler_capacity(), 0); 941 sampler_used_.assign(sampler_capacity(), 0);
764 sampler_used_[0] = 1; 942 sampler_used_[0] = 1;
765 return true; 943 return true;
@@ -829,62 +1007,82 @@ private:
829 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); 1007 vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr);
830 } 1008 }
831 1009
832 // One 1x1 image for each image type and component class, and the views of them every binding needs. 1010 // The fallbacks of every binding (the class comment): a 1x1 sampled image for each image type (each MS class
1011 // its own), and the storage arrays' 1x1 images of each type and component class, or null descriptors with
1012 // features_.null_descriptor. A sampled and a storage fallback never share an image.
833 bool create_fallbacks() { 1013 bool create_fallbacks() {
834 // Each MS class shares one sampled-only fallback between its 2D and 2D-array views. 1014 // Each MS class shares one sampled-only fallback between its 2D and 2D-array views; so does each single-sample
1015 // type between its 2D and 2D-array views, sampled and storage alike.
835 constexpr VkImageType types[] = {VK_IMAGE_TYPE_2D, VK_IMAGE_TYPE_3D, VK_IMAGE_TYPE_1D, VK_IMAGE_TYPE_2D}; 1016 constexpr VkImageType types[] = {VK_IMAGE_TYPE_2D, VK_IMAGE_TYPE_3D, VK_IMAGE_TYPE_1D, VK_IMAGE_TYPE_2D};
836 constexpr std::uint32_t ms_type = 3; 1017 constexpr std::uint32_t ms_type = 3;
837 constexpr VkFormat formats[classes] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_UINT, VK_FORMAT_R32_SINT}; 1018 constexpr VkFormat formats[classes] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_UINT, VK_FORMAT_R32_SINT};
838 // dimension -> (index into types, view type) 1019 // dimension -> (index into types, view type)
839 constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2, 2, ms_type, ms_type}; 1020 constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2, 2, ms_type, ms_type};
840 constexpr VkImageViewType view_types[dimensions] = {VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY, 1021 constexpr VkImageViewType view_types[dimensions] = {VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY,
841 VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D, 1022 VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D,
842 VK_IMAGE_VIEW_TYPE_1D_ARRAY, 1023 VK_IMAGE_VIEW_TYPE_1D_ARRAY, VK_IMAGE_VIEW_TYPE_2D,
843 VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY}; 1024 VK_IMAGE_VIEW_TYPE_2D_ARRAY};
1025 const auto create_image = [&](FallbackImage& image, std::uint32_t t, std::uint32_t c, VkImageUsageFlags usage) {
1026 VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
1027 info.imageType = types[t];
1028 info.format = formats[c];
1029 info.extent = {1, 1, 1};
1030 info.mipLevels = 1;
1031 info.arrayLayers = 1;
1032 info.samples = t == ms_type ? features_.ms_samples[c] : VK_SAMPLE_COUNT_1_BIT;
1033 info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | usage;
1034 share_across_queue_families(info, families_);
1035 if (vk_.vkCreateImage(device_, &info, nullptr, &image.image) != VK_SUCCESS) return false;
1036 VkMemoryRequirements requirements{};
1037 vk_.vkGetImageMemoryRequirements(device_, image.image, &requirements);
1038 VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
1039 allocation.allocationSize = requirements.size;
1040 allocation.memoryTypeIndex = memory_type(requirements.memoryTypeBits, 0);
1041 return allocation.memoryTypeIndex != 0xffffffffu &&
1042 vk_.vkAllocateMemory(device_, &allocation, nullptr, &image.memory) == VK_SUCCESS &&
1043 vk_.vkBindImageMemory(device_, image.image, image.memory, 0) == VK_SUCCESS;
1044 };
1045 const auto create_view = [&](const FallbackImage& image, std::uint32_t d, std::uint32_t c, VkImageView& view) {
1046 VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
1047 info.image = image.image;
1048 info.viewType = view_types[d];
1049 info.format = formats[c];
1050 info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
1051 if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false;
1052 fallback_views_.push_back(view);
1053 return true;
1054 };
844 for (std::uint32_t t = 0; t < std::size(types); ++t) { 1055 for (std::uint32_t t = 0; t < std::size(types); ++t) {
845 for (std::uint32_t c = 0; c < classes; ++c) { 1056 for (std::uint32_t c = 0; c < classes; ++c) {
846 if (t == ms_type && !supports_ms_class(c)) continue; 1057 // Sampled: class 0 of the single-sample types (the translator reads a miss through the float view),
847 VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; 1058 // every supported class of MS.
848 info.imageType = types[t]; 1059 if (t == ms_type ? supports_ms_class(c) : c == 0) {
849 info.format = formats[c]; 1060 if (!create_image(fallback_images_[t * classes + c], t, c, VK_IMAGE_USAGE_SAMPLED_BIT)) return false;
850 info.extent = {1, 1, 1}; 1061 }
851 info.mipLevels = 1; 1062 if (t != ms_type && !features_.null_descriptor &&
852 info.arrayLayers = 1; 1063 !create_image(storage_fallback_images_[t * classes + c], t, c, VK_IMAGE_USAGE_STORAGE_BIT))
853 info.samples = t == ms_type ? features_.ms_samples[c] : VK_SAMPLE_COUNT_1_BIT;
854 info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
855 if (t != ms_type) info.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
856 if (t == ms_type || c == 0) info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
857 FallbackImage& image = fallback_images_[t * classes + c];
858 share_across_queue_families(info, families_);
859 if (vk_.vkCreateImage(device_, &info, nullptr, &image.image) != VK_SUCCESS) return false;
860 VkMemoryRequirements requirements{};
861 vk_.vkGetImageMemoryRequirements(device_, image.image, &requirements);
862 VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
863 allocation.allocationSize = requirements.size;
864 allocation.memoryTypeIndex = memory_type(requirements.memoryTypeBits, 0);
865 if (allocation.memoryTypeIndex == 0xffffffffu ||
866 vk_.vkAllocateMemory(device_, &allocation, nullptr, &image.memory) != VK_SUCCESS ||
867 vk_.vkBindImageMemory(device_, image.image, image.memory, 0) != VK_SUCCESS)
868 return false; 1064 return false;
869 } 1065 }
870 } 1066 }
871 for (std::uint32_t d = 0; d < dimensions; ++d) { 1067 for (std::uint32_t d = 0; d < dimensions; ++d) {
872 const ImageDimension dimension{d}; 1068 const ImageDimension dimension{d};
873 for (std::uint32_t c = 0; c < classes; ++c) { 1069 for (std::uint32_t c = 0; c < classes; ++c) {
874 VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; 1070 const FallbackImage& sampled = fallback_images_[type_of[d] * classes + c];
875 info.image = fallback_images_[type_of[d] * classes + c].image; 1071 if (sampled.image) {
876 if (!info.image) continue; 1072 VkImageView view = VK_NULL_HANDLE;
877 info.viewType = view_types[d]; 1073 if (!create_view(sampled, d, c, view)) return false;
878 info.format = formats[c]; 1074 const auto sampled_at = bindless::sampled_binding(dimension, c);
879 info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; 1075 if (sampled_at != bindless::no_binding && (c == 0 || bindless::multisampled(dimension)))
880 VkImageView view = VK_NULL_HANDLE; 1076 fallback_by_binding_[sampled_at] = view;
881 if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false; 1077 }
882 fallback_views_.push_back(view);
883 const auto sampled_at = bindless::sampled_binding(dimension, c);
884 if (sampled_at != bindless::no_binding && (c == 0 || bindless::multisampled(dimension)))
885 fallback_by_binding_[sampled_at] = view;
886 const auto storage_at = bindless::storage_binding(dimension, c); 1078 const auto storage_at = bindless::storage_binding(dimension, c);
887 if (storage_at != bindless::no_binding) fallback_by_binding_[storage_at] = view; 1079 if (storage_at == bindless::no_binding) continue; // MS: no storage arrays
1080 // With null_descriptor the storage fallbacks stay VK_NULL_HANDLE: null descriptors.
1081 VkImageView view = VK_NULL_HANDLE;
1082 if (!features_.null_descriptor &&
1083 !create_view(storage_fallback_images_[type_of[d] * classes + c], d, c, view))
1084 return false;
1085 fallback_by_binding_[storage_at] = view;
888 const auto r32ui_at = bindless::r32ui_binding(dimension); 1086 const auto r32ui_at = bindless::r32ui_binding(dimension);
889 if (c == 1 && r32ui_at != bindless::no_binding) fallback_by_binding_[r32ui_at] = view; 1087 if (c == 1 && r32ui_at != bindless::no_binding) fallback_by_binding_[r32ui_at] = view;
890 } 1088 }
@@ -1236,12 +1434,22 @@ private:
1236 VkDescriptorSetLayout layout_ = VK_NULL_HANDLE; 1434 VkDescriptorSetLayout layout_ = VK_NULL_HANDLE;
1237 VkDescriptorPool pool_ = VK_NULL_HANDLE; 1435 VkDescriptorPool pool_ = VK_NULL_HANDLE;
1238 VkDescriptorSet set_ = VK_NULL_HANDLE; 1436 VkDescriptorSet set_ = VK_NULL_HANDLE;
1239 std::array<FallbackImage, 4 * classes> fallback_images_{}; // [2D, 3D, 1D, 2D MS][class] 1437 std::array<FallbackImage, 4 * classes> fallback_images_{}; // sampled: [2D, 3D, 1D, 2D MS][class]
1438 // Storage, without null_descriptor: [2D, 3D, 1D][class]. Never the image of a sampled fallback.
1439 std::array<FallbackImage, 3 * classes> storage_fallback_images_{};
1240 std::vector<VkImageView> fallback_views_; 1440 std::vector<VkImageView> fallback_views_;
1241 std::array<VkImageView, bindless::bindings.size()> fallback_by_binding_{}; 1441 std::array<VkImageView, bindless::bindings.size()> fallback_by_binding_{};
1242 VkSampler fallback_sampler_ = VK_NULL_HANDLE; 1442 VkSampler fallback_sampler_ = VK_NULL_HANDLE;
1243 bool fallbacks_ready_ = false; 1443 bool fallbacks_ready_ = false;
1244 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_;
1245 std::vector<std::uint8_t> sampler_used_; 1453 std::vector<std::uint8_t> sampler_used_;
1246 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)
1247 std::vector<std::uint32_t> held_samplers_; 1455 std::vector<std::uint32_t> held_samplers_;