diff options
| author | BubsMcgee <alexbraden99@gmail.com> | 2026-11-25 14:12:01 -0500 |
|---|---|---|
| committer | BubsMcgee <alexbraden99@gmail.com> | 2026-11-25 14:12:01 -0500 |
| commit | 4cfbdfc0b2e39e9137b7f40c48863f1e0e444adc (patch) | |
| tree | 6c656db1c34d59b2b188fc4822badc0f351bffad /runtime/src/vulkan_bindless.h | |
| parent | a5dd540bccc6ce20971a6680f076beedc13f2a95 (diff) | |
| parent | 9f9a7c5f30f8559c52675780cf84004571b8d6e5 (diff) | |
Merge remote-tracking branch 'origin/main' into rt-xapu-opus-on-1144
Diffstat (limited to 'runtime/src/vulkan_bindless.h')
| -rw-r--r-- | runtime/src/vulkan_bindless.h | 130 |
1 files changed, 85 insertions, 45 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h index 589cadffd..e72225396 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 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 | ||
| 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 | ||
| 31 | // atomic through a null descriptor is dropped and a later read still gives zeros. So the storage fallbacks never | ||
| 32 | // share an image with the sampled ones (a store through a null UAV would otherwise show in every null SRV of that | ||
| 33 | // dimension), and with BindlessFeatures::null_descriptor (VK_EXT_robustness2 nullDescriptor) they are null | ||
| 34 | // descriptors, whose stores Vulkan discards and whose reads are zero. | ||
| 35 | // | ||
| 28 | // Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind | 36 | // Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind |
| 29 | // (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while | 37 | // (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 | 38 | // command buffers that use the set are pending, as long as no descriptor they use changes and |
| @@ -90,6 +98,10 @@ constexpr std::array<std::uint32_t, binding_count> default_counts(); | |||
| 90 | struct BindlessFeatures { | 98 | struct BindlessFeatures { |
| 91 | bool update_after_bind = false; | 99 | bool update_after_bind = false; |
| 92 | bool partially_bound = false; | 100 | bool partially_bound = false; |
| 101 | // VK_EXT_robustness2's nullDescriptor is enabled on the device: the storage arrays' fallbacks are null | ||
| 102 | // descriptors (stores and atomics through them are discarded, loads read zero). Without it they are 1x1 images | ||
| 103 | // of their own, apart from the sampled fallbacks, which no store reaches. | ||
| 104 | bool null_descriptor = false; | ||
| 93 | // Fallback sample counts for RGBA8_UNORM, R32_UINT, R32_SINT. A single sample omits that class's MS bindings. | 105 | // 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{ | 106 | std::array<VkSampleCountFlagBits, 3> ms_samples{ |
| 95 | VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT}; | 107 | VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT}; |
| @@ -372,10 +384,12 @@ public: | |||
| 372 | destroy(table_buffers_); | 384 | destroy(table_buffers_); |
| 373 | if (fallback_sampler_) vk_.vkDestroySampler(device_, fallback_sampler_, nullptr); | 385 | if (fallback_sampler_) vk_.vkDestroySampler(device_, fallback_sampler_, nullptr); |
| 374 | for (const VkImageView view : fallback_views_) vk_.vkDestroyImageView(device_, view, nullptr); | 386 | for (const VkImageView view : fallback_views_) vk_.vkDestroyImageView(device_, view, nullptr); |
| 375 | for (const auto& image : fallback_images_) { | 387 | const auto destroy_image = [&](const FallbackImage& image) { |
| 376 | if (image.image) vk_.vkDestroyImage(device_, image.image, nullptr); | 388 | if (image.image) vk_.vkDestroyImage(device_, image.image, nullptr); |
| 377 | if (image.memory) vk_.vkFreeMemory(device_, image.memory, nullptr); | 389 | if (image.memory) vk_.vkFreeMemory(device_, image.memory, nullptr); |
| 378 | } | 390 | }; |
| 391 | for (const auto& image : fallback_images_) destroy_image(image); | ||
| 392 | for (const auto& image : storage_fallback_images_) destroy_image(image); | ||
| 379 | if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); // frees the set | 393 | if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); // frees the set |
| 380 | if (layout_) vk_.vkDestroyDescriptorSetLayout(device_, layout_, nullptr); | 394 | if (layout_) vk_.vkDestroyDescriptorSetLayout(device_, layout_, nullptr); |
| 381 | } | 395 | } |
| @@ -418,8 +432,12 @@ public: | |||
| 418 | void record_fallback_init(VkCommandBuffer command_buffer) { | 432 | void record_fallback_init(VkCommandBuffer command_buffer) { |
| 419 | fallbacks_ready_ = true; | 433 | fallbacks_ready_ = true; |
| 420 | std::vector<VkImageMemoryBarrier> barriers; | 434 | std::vector<VkImageMemoryBarrier> barriers; |
| 421 | for (const auto& image : fallback_images_) { | 435 | std::vector<const FallbackImage*> images; |
| 422 | if (!image.image) continue; // unsupported multisample component class | 436 | for (const auto& image : fallback_images_) images.push_back(&image); |
| 437 | for (const auto& image : storage_fallback_images_) images.push_back(&image); | ||
| 438 | for (const FallbackImage* fallback : images) { | ||
| 439 | const FallbackImage& image = *fallback; | ||
| 440 | 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}; | 441 | VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; |
| 424 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | 442 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 425 | barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; | 443 | barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| @@ -829,62 +847,82 @@ private: | |||
| 829 | vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); | 847 | vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); |
| 830 | } | 848 | } |
| 831 | 849 | ||
| 832 | // One 1x1 image for each image type and component class, and the views of them every binding needs. | 850 | // The fallbacks of every binding (the class comment): a 1x1 sampled image for each image type (each MS class |
| 851 | // its own), and the storage arrays' 1x1 images of each type and component class, or null descriptors with | ||
| 852 | // features_.null_descriptor. A sampled and a storage fallback never share an image. | ||
| 833 | bool create_fallbacks() { | 853 | bool create_fallbacks() { |
| 834 | // Each MS class shares one sampled-only fallback between its 2D and 2D-array views. | 854 | // Each MS class shares one sampled-only fallback between its 2D and 2D-array views; so does each single-sample |
| 855 | // 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}; | 856 | 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; | 857 | constexpr std::uint32_t ms_type = 3; |
| 837 | constexpr VkFormat formats[classes] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_UINT, VK_FORMAT_R32_SINT}; | 858 | constexpr VkFormat formats[classes] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_UINT, VK_FORMAT_R32_SINT}; |
| 838 | // dimension -> (index into types, view type) | 859 | // dimension -> (index into types, view type) |
| 839 | constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2, 2, ms_type, ms_type}; | 860 | 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, | 861 | 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, | 862 | VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D, |
| 842 | VK_IMAGE_VIEW_TYPE_1D_ARRAY, | 863 | VK_IMAGE_VIEW_TYPE_1D_ARRAY, VK_IMAGE_VIEW_TYPE_2D, |
| 843 | VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY}; | 864 | VK_IMAGE_VIEW_TYPE_2D_ARRAY}; |
| 865 | const auto create_image = [&](FallbackImage& image, std::uint32_t t, std::uint32_t c, VkImageUsageFlags usage) { | ||
| 866 | VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; | ||
| 867 | info.imageType = types[t]; | ||
| 868 | info.format = formats[c]; | ||
| 869 | info.extent = {1, 1, 1}; | ||
| 870 | info.mipLevels = 1; | ||
| 871 | info.arrayLayers = 1; | ||
| 872 | info.samples = t == ms_type ? features_.ms_samples[c] : VK_SAMPLE_COUNT_1_BIT; | ||
| 873 | info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | usage; | ||
| 874 | share_across_queue_families(info, families_); | ||
| 875 | if (vk_.vkCreateImage(device_, &info, nullptr, &image.image) != VK_SUCCESS) return false; | ||
| 876 | VkMemoryRequirements requirements{}; | ||
| 877 | vk_.vkGetImageMemoryRequirements(device_, image.image, &requirements); | ||
| 878 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 879 | allocation.allocationSize = requirements.size; | ||
| 880 | allocation.memoryTypeIndex = memory_type(requirements.memoryTypeBits, 0); | ||
| 881 | return allocation.memoryTypeIndex != 0xffffffffu && | ||
| 882 | vk_.vkAllocateMemory(device_, &allocation, nullptr, &image.memory) == VK_SUCCESS && | ||
| 883 | vk_.vkBindImageMemory(device_, image.image, image.memory, 0) == VK_SUCCESS; | ||
| 884 | }; | ||
| 885 | const auto create_view = [&](const FallbackImage& image, std::uint32_t d, std::uint32_t c, VkImageView& view) { | ||
| 886 | VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; | ||
| 887 | info.image = image.image; | ||
| 888 | info.viewType = view_types[d]; | ||
| 889 | info.format = formats[c]; | ||
| 890 | info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 891 | if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false; | ||
| 892 | fallback_views_.push_back(view); | ||
| 893 | return true; | ||
| 894 | }; | ||
| 844 | for (std::uint32_t t = 0; t < std::size(types); ++t) { | 895 | for (std::uint32_t t = 0; t < std::size(types); ++t) { |
| 845 | for (std::uint32_t c = 0; c < classes; ++c) { | 896 | for (std::uint32_t c = 0; c < classes; ++c) { |
| 846 | if (t == ms_type && !supports_ms_class(c)) continue; | 897 | // 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}; | 898 | // every supported class of MS. |
| 848 | info.imageType = types[t]; | 899 | if (t == ms_type ? supports_ms_class(c) : c == 0) { |
| 849 | info.format = formats[c]; | 900 | if (!create_image(fallback_images_[t * classes + c], t, c, VK_IMAGE_USAGE_SAMPLED_BIT)) return false; |
| 850 | info.extent = {1, 1, 1}; | 901 | } |
| 851 | info.mipLevels = 1; | 902 | if (t != ms_type && !features_.null_descriptor && |
| 852 | info.arrayLayers = 1; | 903 | !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; | 904 | return false; |
| 869 | } | 905 | } |
| 870 | } | 906 | } |
| 871 | for (std::uint32_t d = 0; d < dimensions; ++d) { | 907 | for (std::uint32_t d = 0; d < dimensions; ++d) { |
| 872 | const ImageDimension dimension{d}; | 908 | const ImageDimension dimension{d}; |
| 873 | for (std::uint32_t c = 0; c < classes; ++c) { | 909 | for (std::uint32_t c = 0; c < classes; ++c) { |
| 874 | VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; | 910 | const FallbackImage& sampled = fallback_images_[type_of[d] * classes + c]; |
| 875 | info.image = fallback_images_[type_of[d] * classes + c].image; | 911 | if (sampled.image) { |
| 876 | if (!info.image) continue; | 912 | VkImageView view = VK_NULL_HANDLE; |
| 877 | info.viewType = view_types[d]; | 913 | if (!create_view(sampled, d, c, view)) return false; |
| 878 | info.format = formats[c]; | 914 | const auto sampled_at = bindless::sampled_binding(dimension, c); |
| 879 | info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | 915 | if (sampled_at != bindless::no_binding && (c == 0 || bindless::multisampled(dimension))) |
| 880 | VkImageView view = VK_NULL_HANDLE; | 916 | fallback_by_binding_[sampled_at] = view; |
| 881 | if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false; | 917 | } |
| 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); | 918 | const auto storage_at = bindless::storage_binding(dimension, c); |
| 887 | if (storage_at != bindless::no_binding) fallback_by_binding_[storage_at] = view; | 919 | if (storage_at == bindless::no_binding) continue; // MS: no storage arrays |
| 920 | // With null_descriptor the storage fallbacks stay VK_NULL_HANDLE: null descriptors. | ||
| 921 | VkImageView view = VK_NULL_HANDLE; | ||
| 922 | if (!features_.null_descriptor && | ||
| 923 | !create_view(storage_fallback_images_[type_of[d] * classes + c], d, c, view)) | ||
| 924 | return false; | ||
| 925 | fallback_by_binding_[storage_at] = view; | ||
| 888 | const auto r32ui_at = bindless::r32ui_binding(dimension); | 926 | const auto r32ui_at = bindless::r32ui_binding(dimension); |
| 889 | if (c == 1 && r32ui_at != bindless::no_binding) fallback_by_binding_[r32ui_at] = view; | 927 | if (c == 1 && r32ui_at != bindless::no_binding) fallback_by_binding_[r32ui_at] = view; |
| 890 | } | 928 | } |
| @@ -1236,7 +1274,9 @@ private: | |||
| 1236 | VkDescriptorSetLayout layout_ = VK_NULL_HANDLE; | 1274 | VkDescriptorSetLayout layout_ = VK_NULL_HANDLE; |
| 1237 | VkDescriptorPool pool_ = VK_NULL_HANDLE; | 1275 | VkDescriptorPool pool_ = VK_NULL_HANDLE; |
| 1238 | VkDescriptorSet set_ = VK_NULL_HANDLE; | 1276 | VkDescriptorSet set_ = VK_NULL_HANDLE; |
| 1239 | std::array<FallbackImage, 4 * classes> fallback_images_{}; // [2D, 3D, 1D, 2D MS][class] | 1277 | std::array<FallbackImage, 4 * classes> fallback_images_{}; // sampled: [2D, 3D, 1D, 2D MS][class] |
| 1278 | // Storage, without null_descriptor: [2D, 3D, 1D][class]. Never the image of a sampled fallback. | ||
| 1279 | std::array<FallbackImage, 3 * classes> storage_fallback_images_{}; | ||
| 1240 | std::vector<VkImageView> fallback_views_; | 1280 | std::vector<VkImageView> fallback_views_; |
| 1241 | std::array<VkImageView, bindless::bindings.size()> fallback_by_binding_{}; | 1281 | std::array<VkImageView, bindless::bindings.size()> fallback_by_binding_{}; |
| 1242 | VkSampler fallback_sampler_ = VK_NULL_HANDLE; | 1282 | VkSampler fallback_sampler_ = VK_NULL_HANDLE; |