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:
authorcemalyakla <105042441+cemalyakla@users.noreply.github.com>2026-11-22 02:34:09 +0000
committercemalyakla <105042441+cemalyakla@users.noreply.github.com>2026-11-22 02:34:09 +0000
commit88dc8beac1b0169efa616fee67e6caf2eb670d1c (patch)
tree94fdcebea87ba19a08dd3e7f74d30dfdeccc999c /runtime/src/vulkan_bindless.h
parent067d3e8b97189fcf272dc699fdbb871eae496660 (diff)
Runtime: bind multisampled 2D and array shader-resource views
Diffstat (limited to 'runtime/src/vulkan_bindless.h')
-rw-r--r--runtime/src/vulkan_bindless.h175
1 files changed, 126 insertions, 49 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h
index 6f1c18e1c..f68b6f292 100644
--- a/runtime/src/vulkan_bindless.h
+++ b/runtime/src/vulkan_bindless.h
@@ -9,7 +9,8 @@
9// 3 sampled 2D-array images 1024 8 + 3 * dimension + class 256 each: 9// 3 sampled 2D-array images 1024 8 + 3 * dimension + class 256 each:
10// 4 2D r32ui storage images 1024 storage images by component class 10// 4 2D r32ui storage images 1024 storage images by component class
11// dimension: 0 = 2D, 1 = 2D array, 2 = 3D, 3 = 1D (the translator's STORE_DIMS); class: 0 = float, 1 = uint, 2 = sint. 11// dimension: 0 = 2D, 1 = 2D array, 2 = 3D, 3 = 1D (the translator's STORE_DIMS); class: 0 = float, 1 = uint, 2 = sint.
12// The counts are those of runtime/test/vk_pipeline_check.cpp, which creates every translated shader against them. 12// bindings 20/21: sampled multisample 2D images / 2D arrays, 256 each (runtime SRVs).
13// bindings 22/23 and 24/25 are their UINT and SINT equivalents on devices supporting those classes.
13// 14//
14// A shader turns a T# or S# into an index through the hash table (descriptor_table.h) and uses that index in 15// A shader turns a T# or S# into an index through the hash table (descriptor_table.h) and uses that index in
15// whichever binding the instruction calls for: the sampled array of the image's dimension, or one of its storage 16// whichever binding the instruction calls for: the sampled array of the image's dimension, or one of its storage
@@ -48,8 +49,8 @@
48 49
49namespace leonida { 50namespace leonida {
50 51
51// The translator's image dimensions; the values are its STORE_DIMS indices. A cube is a 2D array of six layers. 52// The first four dimensions are the translator's STORE_DIMS indices. A cube is a 2D array of six layers.
52enum class ImageDimension : std::uint32_t { dim_2d = 0, dim_2d_array = 1, dim_3d = 2, dim_1d = 3 }; 53enum class ImageDimension : std::uint32_t { dim_2d, dim_2d_array, dim_3d, dim_1d, dim_2d_ms, dim_2d_ms_array };
53 54
54// Storage views of one image by the component type a shader reads or writes it with (the translator picks by the 55// Storage views of one image by the component type a shader reads or writes it with (the translator picks by the
55// T# format at run time). r32ui is for the atomic instructions, whose image type is R32ui (2D and 3D only). 56// T# format at run time). r32ui is for the atomic instructions, whose image type is R32ui (2D and 3D only).
@@ -64,6 +65,9 @@ struct StorageViews {
64struct BindlessFeatures { 65struct BindlessFeatures {
65 bool update_after_bind = false; 66 bool update_after_bind = false;
66 bool partially_bound = false; 67 bool partially_bound = false;
68 // Fallback sample counts for RGBA8_UNORM, R32_UINT, R32_SINT. A single sample omits that class's MS bindings.
69 std::array<VkSampleCountFlagBits, 3> ms_samples{
70 VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT};
67}; 71};
68 72
69namespace bindless { 73namespace bindless {
@@ -76,10 +80,11 @@ struct Binding {
76inline constexpr std::uint32_t table_binding = 2; 80inline constexpr std::uint32_t table_binding = 2;
77inline constexpr std::uint32_t sampler_binding = 1; 81inline constexpr std::uint32_t sampler_binding = 1;
78inline constexpr std::uint32_t storage_indices = 256; // what every storage array of bindings 8-19 holds 82inline constexpr std::uint32_t storage_indices = 256; // what every storage array of bindings 8-19 holds
83inline constexpr std::uint32_t first_ms_binding = 20;
79inline constexpr std::uint32_t no_binding = 0xffffffffu; 84inline constexpr std::uint32_t no_binding = 0xffffffffu;
80 85
81// Indexed by binding number. 86// Indexed by binding number.
82inline constexpr std::array<Binding, 20> bindings = {{ 87inline constexpr std::array<Binding, 26> bindings = {{
83 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4096}, {VK_DESCRIPTOR_TYPE_SAMPLER, 1024}, 88 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4096}, {VK_DESCRIPTOR_TYPE_SAMPLER, 1024},
84 {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024}, 89 {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024},
85 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, 90 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
@@ -90,16 +95,66 @@ inline constexpr std::array<Binding, 20> bindings = {{
90 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, 95 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256},
91 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, 96 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256},
92 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, 97 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256},
98 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
99 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
100 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
93}}; 101}};
94 102
95constexpr std::uint32_t sampled_binding(ImageDimension dimension) { 103constexpr bool binding_enabled(std::uint32_t binding, const BindlessFeatures& features) {
96 constexpr std::uint32_t by_dimension[] = {0, 3, 6, 5}; 104 return binding < first_ms_binding || features.ms_samples[(binding - first_ms_binding) / 2] != VK_SAMPLE_COUNT_1_BIT;
97 return by_dimension[std::uint32_t(dimension)]; 105}
106
107// Every binding is visible to every shader stage. Preserve the single-sample layout on devices whose
108// per-stage or per-set limits have no room for additional pairs of MS arrays.
109constexpr void limit_ms_bindings(BindlessFeatures& features, std::uint32_t sampled_limit,
110 std::uint32_t resource_limit) {
111 std::uint32_t sampled = 0, total = 0;
112 for (std::uint32_t b = 0; b < first_ms_binding; ++b) {
113 if (bindings[b].type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) sampled += bindings[b].count;
114 if (bindings[b].type != VK_DESCRIPTOR_TYPE_SAMPLER) total += bindings[b].count;
115 }
116 for (std::uint32_t c = 0; c < features.ms_samples.size(); ++c) {
117 if (features.ms_samples[c] == VK_SAMPLE_COUNT_1_BIT) continue;
118 const auto b = first_ms_binding + 2 * c;
119 const auto pair = bindings[b].count + bindings[b + 1].count;
120 if (sampled + pair > sampled_limit || total + pair > resource_limit) {
121 features.ms_samples[c] = VK_SAMPLE_COUNT_1_BIT;
122 } else {
123 sampled += pair;
124 total += pair;
125 }
126 }
127}
128
129inline void limit_ms_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimits& limits,
130 const VkPhysicalDeviceVulkan12Properties& indexed) {
131 const auto sampled = features.update_after_bind
132 ? std::min(indexed.maxPerStageDescriptorUpdateAfterBindSampledImages,
133 indexed.maxDescriptorSetUpdateAfterBindSampledImages)
134 : std::min(limits.maxPerStageDescriptorSampledImages, limits.maxDescriptorSetSampledImages);
135 auto resources = features.update_after_bind ? indexed.maxPerStageUpdateAfterBindResources : limits.maxPerStageResources;
136 // Fragment outputs count as stage resources. D3D12 allows up to eight render targets.
137 resources -= std::min(resources, std::min(limits.maxColorAttachments, 8u));
138 if (features.update_after_bind) {
139 // Standalone samplers count in the pool, but do not count against the per-stage resource limit.
140 const auto pool = indexed.maxUpdateAfterBindDescriptorsInAllPools;
141 resources = std::min(resources, pool - std::min(pool, bindings[sampler_binding].count));
142 }
143 limit_ms_bindings(features, sampled, resources);
144}
145
146constexpr bool multisampled(ImageDimension dimension) {
147 return dimension == ImageDimension::dim_2d_ms || dimension == ImageDimension::dim_2d_ms_array;
148}
149
150constexpr std::uint32_t sampled_binding(ImageDimension dimension, std::uint32_t component_class = 0) {
151 constexpr std::uint32_t by_dimension[] = {0, 3, 6, 5, first_ms_binding, first_ms_binding + 1};
152 return by_dimension[std::uint32_t(dimension)] + (multisampled(dimension) ? component_class * 2 : 0);
98} 153}
99 154
100// component_class: 0 = float, 1 = uint, 2 = sint. 155// component_class: 0 = float, 1 = uint, 2 = sint.
101constexpr std::uint32_t storage_binding(ImageDimension dimension, std::uint32_t component_class) { 156constexpr std::uint32_t storage_binding(ImageDimension dimension, std::uint32_t component_class) {
102 return 8 + 3 * std::uint32_t(dimension) + component_class; 157 return multisampled(dimension) ? no_binding : 8 + 3 * std::uint32_t(dimension) + component_class;
103} 158}
104 159
105constexpr std::uint32_t r32ui_binding(ImageDimension dimension) { 160constexpr std::uint32_t r32ui_binding(ImageDimension dimension) {
@@ -136,6 +191,9 @@ public:
136 BindlessSet& operator=(const BindlessSet&) = delete; 191 BindlessSet& operator=(const BindlessSet&) = delete;
137 192
138 bool ok() const { return ok_; } 193 bool ok() const { return ok_; }
194 bool supports_ms_class(std::uint32_t component_class) const {
195 return component_class < classes && features_.ms_samples[component_class] != VK_SAMPLE_COUNT_1_BIT;
196 }
139 // Set 0 of every pipeline layout made for translated shaders. 197 // Set 0 of every pipeline layout made for translated shaders.
140 VkDescriptorSetLayout layout() const { return layout_; } 198 VkDescriptorSetLayout layout() const { return layout_; }
141 // Bound at set 0. The same set for the life of this object: flush() updates it in place. 199 // Bound at set 0. The same set for the life of this object: flush() updates it in place.
@@ -148,6 +206,7 @@ public:
148 fallbacks_ready_ = true; 206 fallbacks_ready_ = true;
149 std::vector<VkImageMemoryBarrier> barriers; 207 std::vector<VkImageMemoryBarrier> barriers;
150 for (const auto& image : fallback_images_) { 208 for (const auto& image : fallback_images_) {
209 if (!image.image) continue; // unsupported multisample component class
151 VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; 210 VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
152 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; 211 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
153 barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; 212 barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -161,9 +220,9 @@ public:
161 vk_.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, 220 vk_.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0,
162 nullptr, 0, nullptr, std::uint32_t(barriers.size()), barriers.data()); 221 nullptr, 0, nullptr, std::uint32_t(barriers.size()), barriers.data());
163 const VkClearColorValue zero{}; 222 const VkClearColorValue zero{};
164 const VkImageSubresourceRange whole{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; 223 for (const auto& barrier : barriers)
165 for (const auto& image : fallback_images_) 224 vk_.vkCmdClearColorImage(command_buffer, barrier.image, VK_IMAGE_LAYOUT_GENERAL, &zero, 1,
166 vk_.vkCmdClearColorImage(command_buffer, image.image, VK_IMAGE_LAYOUT_GENERAL, &zero, 1, &whole); 225 &barrier.subresourceRange);
167 VkMemoryBarrier done{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; 226 VkMemoryBarrier done{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
168 done.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; 227 done.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
169 done.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT; 228 done.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
@@ -173,30 +232,33 @@ public:
173 232
174 // Gives an image an index in its dimension's bindings: 0 when the dimension is full. `sampled` goes to the 233 // Gives an image an index in its dimension's bindings: 0 when the dimension is full. `sampled` goes to the
175 // sampled array in `sampled_layout` (the layout the image is in when shaders read it); the storage views go to 234 // sampled array in `sampled_layout` (the layout the image is in when shaders read it); the storage views go to
176 // the storage arrays in GENERAL. The caller maps the T# to the index with insert_key(). Takes effect at flush(). 235 // the storage arrays in GENERAL. For MS views, sampled_class selects the float, uint or sint sampled array.
236 // The caller maps the T# to the index with insert_key(). Takes effect at flush().
177 std::uint32_t add_image(ImageDimension dimension, VkImageView sampled, VkImageLayout sampled_layout, 237 std::uint32_t add_image(ImageDimension dimension, VkImageView sampled, VkImageLayout sampled_layout,
178 const StorageViews& storage = {}) { 238 const StorageViews& storage = {}, std::uint32_t sampled_class = 0) {
179 const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui; 239 const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui;
240 if (bindless::multisampled(dimension) && (stores || !supports_ms_class(sampled_class))) return 0;
180 auto& used = image_used_[std::uint32_t(dimension)]; 241 auto& used = image_used_[std::uint32_t(dimension)];
181 const std::uint32_t low_end = std::min(std::uint32_t(used.size()), bindless::storage_indices); 242 const std::uint32_t low_end = std::min(std::uint32_t(used.size()), bindless::storage_indices);
182 std::uint32_t index = stores ? 0 : allocate(used, low_end, std::uint32_t(used.size())); 243 std::uint32_t index = stores ? 0 : allocate(used, low_end, std::uint32_t(used.size()));
183 if (!index) index = allocate(used, 1, low_end); 244 if (!index) index = allocate(used, 1, low_end);
184 if (index) write_image(dimension, index, sampled, sampled_layout, storage); 245 if (index) write_image(dimension, index, sampled, sampled_layout, storage, sampled_class);
185 return index; 246 return index;
186 } 247 }
187 248
188 // New views or a new layout for an index add_image returned (a texture's state changed, say). 249 // New views or a new layout for an index add_image returned (a texture's state changed, say).
189 void update_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout, 250 void update_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout,
190 const StorageViews& storage = {}) { 251 const StorageViews& storage = {}, std::uint32_t sampled_class = 0) {
191 const auto& used = image_used_[std::uint32_t(dimension)]; 252 const auto& used = image_used_[std::uint32_t(dimension)];
192 if (index && index < used.size() && used[index]) write_image(dimension, index, sampled, sampled_layout, storage); 253 if (index && index < used.size() && used[index])
254 write_image(dimension, index, sampled, sampled_layout, storage, sampled_class);
193 } 255 }
194 256
195 // Returns the index to the fallback and frees it. The caller removes the image's keys with erase_key() first. 257 // Returns the index to the fallback and frees it. The caller removes the image's keys with erase_key() first.
196 void remove_image(ImageDimension dimension, std::uint32_t index) { 258 void remove_image(ImageDimension dimension, std::uint32_t index) {
197 auto& used = image_used_[std::uint32_t(dimension)]; 259 auto& used = image_used_[std::uint32_t(dimension)];
198 if (!index || index >= used.size() || !used[index]) return; 260 if (!index || index >= used.size() || !used[index]) return;
199 write_image(dimension, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}); 261 write_image(dimension, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}, 0);
200 used[index] = 0; 262 used[index] = 0;
201 } 263 }
202 264
@@ -335,7 +397,7 @@ private:
335 VkDescriptorImageInfo info; 397 VkDescriptorImageInfo info;
336 }; 398 };
337 399
338 static constexpr std::uint32_t dimensions = 4, classes = 3; 400 static constexpr std::uint32_t dimensions = 6, classes = 3;
339 401
340 // The first free index in [begin, end), marked used; 0 when there is none. 402 // The first free index in [begin, end), marked used; 0 when there is none.
341 static std::uint32_t allocate(std::vector<std::uint8_t>& used, std::uint32_t begin, std::uint32_t end) { 403 static std::uint32_t allocate(std::vector<std::uint8_t>& used, std::uint32_t begin, std::uint32_t end) {
@@ -357,26 +419,29 @@ private:
357 std::array<VkDescriptorSetLayoutBinding, bindless::bindings.size()> entries{}; 419 std::array<VkDescriptorSetLayoutBinding, bindless::bindings.size()> entries{};
358 std::array<VkDescriptorBindingFlags, bindless::bindings.size()> flags{}; 420 std::array<VkDescriptorBindingFlags, bindless::bindings.size()> flags{};
359 std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{}; 421 std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{};
360 for (std::uint32_t b = 0; b < entries.size(); ++b) { 422 std::uint32_t count = 0;
361 entries[b] = {b, bindless::bindings[b].type, bindless::bindings[b].count, VK_SHADER_STAGE_ALL, nullptr}; 423 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
362 flags[b] = (features_.update_after_bind ? VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT : 0) | 424 if (!bindless::binding_enabled(b, features_)) continue;
363 (features_.partially_bound ? VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT : 0); 425 entries[count] = {b, bindless::bindings[b].type, bindless::bindings[b].count, VK_SHADER_STAGE_ALL, nullptr};
364 sizes[b] = {bindless::bindings[b].type, bindless::bindings[b].count}; 426 flags[count] = (features_.update_after_bind ? VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT : 0) |
427 (features_.partially_bound ? VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT : 0);
428 sizes[count] = {bindless::bindings[b].type, bindless::bindings[b].count};
429 ++count;
365 } 430 }
366 VkDescriptorSetLayoutBindingFlagsCreateInfo flags_info{ 431 VkDescriptorSetLayoutBindingFlagsCreateInfo flags_info{
367 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO}; 432 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO};
368 flags_info.bindingCount = std::uint32_t(flags.size()); 433 flags_info.bindingCount = count;
369 flags_info.pBindingFlags = flags.data(); 434 flags_info.pBindingFlags = flags.data();
370 VkDescriptorSetLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, &flags_info}; 435 VkDescriptorSetLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, &flags_info};
371 layout_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT : 0; 436 layout_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT : 0;
372 layout_info.bindingCount = std::uint32_t(entries.size()); 437 layout_info.bindingCount = count;
373 layout_info.pBindings = entries.data(); 438 layout_info.pBindings = entries.data();
374 if (vk_.vkCreateDescriptorSetLayout(device_, &layout_info, nullptr, &layout_) != VK_SUCCESS) return false; 439 if (vk_.vkCreateDescriptorSetLayout(device_, &layout_info, nullptr, &layout_) != VK_SUCCESS) return false;
375 440
376 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; 441 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
377 pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0; 442 pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0;
378 pool_info.maxSets = 1; 443 pool_info.maxSets = 1;
379 pool_info.poolSizeCount = std::uint32_t(sizes.size()); 444 pool_info.poolSizeCount = count;
380 pool_info.pPoolSizes = sizes.data(); 445 pool_info.pPoolSizes = sizes.data();
381 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool_) != VK_SUCCESS) return false; 446 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool_) != VK_SUCCESS) return false;
382 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; 447 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
@@ -396,23 +461,28 @@ private:
396 461
397 // One 1x1 image for each image type and component class, and the views of them every binding needs. 462 // One 1x1 image for each image type and component class, and the views of them every binding needs.
398 bool create_fallbacks() { 463 bool create_fallbacks() {
399 constexpr VkImageType types[] = {VK_IMAGE_TYPE_2D, VK_IMAGE_TYPE_3D, VK_IMAGE_TYPE_1D}; 464 // Each MS class shares one sampled-only fallback between its 2D and 2D-array views.
465 constexpr VkImageType types[] = {VK_IMAGE_TYPE_2D, VK_IMAGE_TYPE_3D, VK_IMAGE_TYPE_1D, VK_IMAGE_TYPE_2D};
466 constexpr std::uint32_t ms_type = 3;
400 constexpr VkFormat formats[classes] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_UINT, VK_FORMAT_R32_SINT}; 467 constexpr VkFormat formats[classes] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_UINT, VK_FORMAT_R32_SINT};
401 // dimension -> (index into types, view type) 468 // dimension -> (index into types, view type)
402 constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2}; 469 constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2, ms_type, ms_type};
403 constexpr VkImageViewType view_types[dimensions] = {VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY, 470 constexpr VkImageViewType view_types[dimensions] = {VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY,
404 VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D}; 471 VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D,
405 for (std::uint32_t t = 0; t < 3; ++t) { 472 VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY};
473 for (std::uint32_t t = 0; t < std::size(types); ++t) {
406 for (std::uint32_t c = 0; c < classes; ++c) { 474 for (std::uint32_t c = 0; c < classes; ++c) {
475 if (t == ms_type && !supports_ms_class(c)) continue;
407 VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; 476 VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
408 info.imageType = types[t]; 477 info.imageType = types[t];
409 info.format = formats[c]; 478 info.format = formats[c];
410 info.extent = {1, 1, 1}; 479 info.extent = {1, 1, 1};
411 info.mipLevels = 1; 480 info.mipLevels = 1;
412 info.arrayLayers = 1; 481 info.arrayLayers = 1;
413 info.samples = VK_SAMPLE_COUNT_1_BIT; 482 info.samples = t == ms_type ? features_.ms_samples[c] : VK_SAMPLE_COUNT_1_BIT;
414 info.usage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | 483 info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
415 (c == 0 ? VK_IMAGE_USAGE_SAMPLED_BIT : 0); 484 if (t != ms_type) info.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
485 if (t == ms_type || c == 0) info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
416 FallbackImage& image = fallback_images_[t * classes + c]; 486 FallbackImage& image = fallback_images_[t * classes + c];
417 if (vk_.vkCreateImage(device_, &info, nullptr, &image.image) != VK_SUCCESS) return false; 487 if (vk_.vkCreateImage(device_, &info, nullptr, &image.image) != VK_SUCCESS) return false;
418 VkMemoryRequirements requirements{}; 488 VkMemoryRequirements requirements{};
@@ -427,16 +497,23 @@ private:
427 } 497 }
428 } 498 }
429 for (std::uint32_t d = 0; d < dimensions; ++d) { 499 for (std::uint32_t d = 0; d < dimensions; ++d) {
500 const ImageDimension dimension{d};
430 for (std::uint32_t c = 0; c < classes; ++c) { 501 for (std::uint32_t c = 0; c < classes; ++c) {
431 VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; 502 VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
432 info.image = fallback_images_[type_of[d] * classes + c].image; 503 info.image = fallback_images_[type_of[d] * classes + c].image;
504 if (!info.image) continue;
433 info.viewType = view_types[d]; 505 info.viewType = view_types[d];
434 info.format = formats[c]; 506 info.format = formats[c];
435 info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; 507 info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
436 VkImageView view = VK_NULL_HANDLE; 508 VkImageView view = VK_NULL_HANDLE;
437 if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false; 509 if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false;
438 fallback_views_.push_back(view); 510 fallback_views_.push_back(view);
439 fallback_view_[d][c] = view; 511 if (c == 0 || bindless::multisampled(dimension))
512 fallback_by_binding_[bindless::sampled_binding(dimension, c)] = view;
513 const auto storage_at = bindless::storage_binding(dimension, c);
514 if (storage_at != bindless::no_binding) fallback_by_binding_[storage_at] = view;
515 const auto r32ui_at = bindless::r32ui_binding(dimension);
516 if (c == 1 && r32ui_at != bindless::no_binding) fallback_by_binding_[r32ui_at] = view;
440 } 517 }
441 } 518 }
442 VkSamplerCreateInfo sampler{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO}; 519 VkSamplerCreateInfo sampler{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
@@ -628,17 +705,7 @@ private:
628 705
629 VkDescriptorImageInfo fallback_info(std::uint32_t binding) const { 706 VkDescriptorImageInfo fallback_info(std::uint32_t binding) const {
630 if (binding == bindless::sampler_binding) return {fallback_sampler_, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_UNDEFINED}; 707 if (binding == bindless::sampler_binding) return {fallback_sampler_, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_UNDEFINED};
631 for (std::uint32_t d = 0; d < dimensions; ++d) { 708 return {VK_NULL_HANDLE, fallback_by_binding_[binding], VK_IMAGE_LAYOUT_GENERAL};
632 const ImageDimension dimension{d};
633 std::uint32_t component_class = classes; // of the fallback view this binding takes
634 if (binding == bindless::sampled_binding(dimension)) component_class = 0;
635 if (binding == bindless::r32ui_binding(dimension)) component_class = 1;
636 for (std::uint32_t c = 0; c < classes; ++c)
637 if (binding == bindless::storage_binding(dimension, c)) component_class = c;
638 if (component_class < classes)
639 return {VK_NULL_HANDLE, fallback_view_[d][component_class], VK_IMAGE_LAYOUT_GENERAL};
640 }
641 return {};
642 } 709 }
643 710
644 // The table at binding 2 and the fallback at index 0 of every array, or in every element without partially_bound. 711 // The table at binding 2 and the fallback at index 0 of every array, or in every element without partially_bound.
@@ -648,7 +715,7 @@ private:
648 std::vector<VkWriteDescriptorSet> writes; 715 std::vector<VkWriteDescriptorSet> writes;
649 infos.reserve(bindless::bindings.size()); 716 infos.reserve(bindless::bindings.size());
650 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) { 717 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
651 if (b == bindless::table_binding) continue; 718 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue;
652 const std::uint32_t count = features_.partially_bound ? 1 : bindless::bindings[b].count; 719 const std::uint32_t count = features_.partially_bound ? 1 : bindless::bindings[b].count;
653 infos.emplace_back(count, fallback_info(b)); 720 infos.emplace_back(count, fallback_info(b));
654 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; 721 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
@@ -680,7 +747,17 @@ private:
680 747
681 // Queues every binding of the dimension at `index`: the given views, or the fallback where one is null. 748 // Queues every binding of the dimension at `index`: the given views, or the fallback where one is null.
682 void write_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout, 749 void write_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout,
683 const StorageViews& storage) { 750 const StorageViews& storage, std::uint32_t sampled_class) {
751 if (bindless::multisampled(dimension)) {
752 for (std::uint32_t c = 0; c < classes; ++c) {
753 if (!supports_ms_class(c)) continue;
754 const auto binding = bindless::sampled_binding(dimension, c);
755 queue(binding, index, c == sampled_class && sampled
756 ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout}
757 : fallback_info(binding));
758 }
759 return;
760 }
684 const std::uint32_t sampled_at = bindless::sampled_binding(dimension); 761 const std::uint32_t sampled_at = bindless::sampled_binding(dimension);
685 queue(sampled_at, index, 762 queue(sampled_at, index,
686 sampled ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout} : fallback_info(sampled_at)); 763 sampled ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout} : fallback_info(sampled_at));
@@ -703,9 +780,9 @@ private:
703 VkDescriptorSetLayout layout_ = VK_NULL_HANDLE; 780 VkDescriptorSetLayout layout_ = VK_NULL_HANDLE;
704 VkDescriptorPool pool_ = VK_NULL_HANDLE; 781 VkDescriptorPool pool_ = VK_NULL_HANDLE;
705 VkDescriptorSet set_ = VK_NULL_HANDLE; 782 VkDescriptorSet set_ = VK_NULL_HANDLE;
706 std::array<FallbackImage, 3 * classes> fallback_images_{}; // [image type: 2D, 3D, 1D][class] 783 std::array<FallbackImage, 4 * classes> fallback_images_{}; // [2D, 3D, 1D, 2D MS][class]
707 std::vector<VkImageView> fallback_views_; 784 std::vector<VkImageView> fallback_views_;
708 VkImageView fallback_view_[dimensions][classes] = {}; 785 std::array<VkImageView, bindless::bindings.size()> fallback_by_binding_{};
709 VkSampler fallback_sampler_ = VK_NULL_HANDLE; 786 VkSampler fallback_sampler_ = VK_NULL_HANDLE;
710 bool fallbacks_ready_ = false; 787 bool fallbacks_ready_ = false;
711 std::array<std::vector<std::uint8_t>, dimensions> image_used_; 788 std::array<std::vector<std::uint8_t>, dimensions> image_used_;