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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 16:38:20 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 16:38:20 -0500
commitc116c3fa28983444cd47feea0461e1e7eaac83b5 (patch)
treef2f4821b744b678f0cf1ce98638e981ea737b0c6 /runtime/src/vulkan_bindless.h
parent5866d6974e86fc2d39e6ce0691af63b354f6c8b9 (diff)
parent24d8af66af151f024b0b520fb397d899924f5486 (diff)
Merge main into t-drive-overlay
docs/boot.md: main added LEONIDA_WRITE_DIR / LEONIDA_SAVE_DIR to the --overlay bullet and a new "Under Wine on Linux" section; keep both and the T: paragraph of this branch after main's sentences. 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.h510
1 files changed, 386 insertions, 124 deletions
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h
index cb5b61f65..e72225396 100644
--- a/runtime/src/vulkan_bindless.h
+++ b/runtime/src/vulkan_bindless.h
@@ -8,11 +8,13 @@
8// 2 the descriptor hash table 1 7 3D r32ui storage images 256 8// 2 the descriptor hash table 1 7 3D r32ui storage images 256
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 first four STORE_DIMS); class: 0 = float, 1 = uint, 2 = sint.
12// bindings 20/21: sampled multisample 2D images / 2D arrays, 256 each (runtime SRVs). 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// bindings 22/23 and 24/25 are their UINT and SINT equivalents on devices supporting those classes.
14// These are the default counts (BindlessFeatures::counts). size_bindings() sizes the 2D, sampler and storage arrays 14// bindings 26-28: storage-only 1D-array images by component class, 256 each (STORE_DIMS index 4).
15// from the device's limits instead; the shaders declare runtime arrays, so any count works for them. 15// These are the default counts (BindlessFeatures::counts). size_bindings() sizes the sampled arrays of every
16// single-sample dimension, the samplers and the storage arrays from the device's limits instead; the shaders declare
17// runtime arrays, so any count works for them.
16// 18//
17// A shader turns a T# or S# into an index through the hash table (descriptor_table.h) and uses that index in 19// A shader turns a T# or S# into an index through the hash table (descriptor_table.h) and uses that index in
18// whichever binding the instruction calls for: the sampled array of the image's dimension, or one of its storage 20// whichever binding the instruction calls for: the sampled array of the image's dimension, or one of its storage
@@ -23,6 +25,14 @@
23// 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
24// table's buffer. 26// table's buffer.
25// 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//
26// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind 36// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind
27// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while 37// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while
28// 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
@@ -37,12 +47,22 @@
37// flush() takes the command buffer that will use the set: it uploads only the slots that changed, from a mapped 47// flush() takes the command buffer that will use the set: it uploads only the slots that changed, from a mapped
38// staging buffer, and barriers the copy before the draws. The table at creation is empty until that first flush. 48// staging buffer, and barriers the copy before the draws. The table at creation is empty until that first flush.
39// 49//
50// When device-local memory runs out (a title that fills VRAM), a new generation's table goes to HOST_VISIBLE memory
51// instead: slower lookups beat no table. Released generations are kept as spares (at most max_spares) and reused,
52// pool and buffers, by the next replacing flush, so steady churn allocates nothing. If a generation still cannot be
53// made, flush() rewrites the current table in place when every submission that could read it is done, and otherwise
54// degrades: the descriptors are written, the table keeps its last uploaded contents (keys added since find the
55// fallback, index 0; keys rebound since keep their previous index, whose views stay alive; removed indices are not
56// given out again meanwhile) and the change waits for a later flush. A flush never fails the submission for want of
57// memory (last_flush_degraded(), degraded_flushes()).
58//
40// Not thread-safe: the owner serialises calls. 59// Not thread-safe: the owner serialises calls.
41 60
42#ifndef VK_NO_PROTOTYPES 61#ifndef VK_NO_PROTOTYPES
43#define VK_NO_PROTOTYPES 62#define VK_NO_PROTOTYPES
44#endif 63#endif
45#include "descriptor_table.h" 64#include "descriptor_table.h"
65#include "queue_families.h"
46 66
47#include <vulkan/vulkan.h> 67#include <vulkan/vulkan.h>
48 68
@@ -52,12 +72,13 @@
52#include <cstring> 72#include <cstring>
53#include <functional> 73#include <functional>
54#include <unordered_map> 74#include <unordered_map>
75#include <utility>
55#include <vector> 76#include <vector>
56 77
57namespace leonida { 78namespace leonida {
58 79
59// The first four dimensions are the translator's STORE_DIMS indices. A cube is a 2D array of six layers. 80// The first five dimensions are the translator's STORE_DIMS indices. A cube is a 2D array of six layers.
60enum class ImageDimension : std::uint32_t { dim_2d, dim_2d_array, dim_3d, dim_1d, dim_2d_ms, dim_2d_ms_array }; 81enum class ImageDimension : std::uint32_t { dim_2d, dim_2d_array, dim_3d, dim_1d, dim_1d_array, dim_2d_ms, dim_2d_ms_array };
61 82
62// Storage views of one image by the component type a shader reads or writes it with (the translator picks by the 83// Storage views of one image by the component type a shader reads or writes it with (the translator picks by the
63// T# format at run time). r32ui is for the atomic instructions, whose image type is R32ui (2D and 3D only). 84// T# format at run time). r32ui is for the atomic instructions, whose image type is R32ui (2D and 3D only).
@@ -70,13 +91,17 @@ struct StorageViews {
70}; 91};
71 92
72namespace bindless { 93namespace bindless {
73inline constexpr std::uint32_t binding_count = 26; 94inline constexpr std::uint32_t binding_count = 29;
74constexpr std::array<std::uint32_t, binding_count> default_counts(); 95constexpr std::array<std::uint32_t, binding_count> default_counts();
75} // namespace bindless 96} // namespace bindless
76 97
77struct BindlessFeatures { 98struct BindlessFeatures {
78 bool update_after_bind = false; 99 bool update_after_bind = false;
79 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;
80 // 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.
81 std::array<VkSampleCountFlagBits, 3> ms_samples{ 106 std::array<VkSampleCountFlagBits, 3> ms_samples{
82 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};
@@ -94,10 +119,12 @@ struct Binding {
94inline constexpr std::uint32_t table_binding = 2; 119inline constexpr std::uint32_t table_binding = 2;
95inline constexpr std::uint32_t sampler_binding = 1; 120inline constexpr std::uint32_t sampler_binding = 1;
96inline constexpr std::uint32_t first_ms_binding = 20; 121inline constexpr std::uint32_t first_ms_binding = 20;
122inline constexpr std::uint32_t ms_binding_count = 6;
123inline constexpr std::uint32_t first_1d_array_storage_binding = first_ms_binding + ms_binding_count;
97inline constexpr std::uint32_t no_binding = 0xffffffffu; 124inline constexpr std::uint32_t no_binding = 0xffffffffu;
98 125
99// Indexed by binding number. 126// Indexed by binding number. Keep the literal extent for tools/spirv_metal_set0.py.
100inline constexpr std::array<Binding, 26> bindings = {{ 127inline constexpr std::array<Binding, 29> bindings = {{
101 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4096}, {VK_DESCRIPTOR_TYPE_SAMPLER, 1024}, 128 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4096}, {VK_DESCRIPTOR_TYPE_SAMPLER, 1024},
102 {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024}, 129 {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024},
103 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, 130 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
@@ -111,6 +138,8 @@ inline constexpr std::array<Binding, 26> bindings = {{
111 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, 138 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
112 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, 139 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
113 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, 140 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
141 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256},
142 {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256},
114}}; 143}};
115 144
116static_assert(bindings.size() == binding_count); 145static_assert(bindings.size() == binding_count);
@@ -121,8 +150,12 @@ constexpr std::array<std::uint32_t, binding_count> default_counts() {
121 return counts; 150 return counts;
122} 151}
123 152
153constexpr bool ms_binding(std::uint32_t binding) {
154 return binding >= first_ms_binding && binding < first_ms_binding + ms_binding_count;
155}
156
124constexpr bool binding_enabled(std::uint32_t binding, const BindlessFeatures& features) { 157constexpr bool binding_enabled(std::uint32_t binding, const BindlessFeatures& features) {
125 return binding < first_ms_binding || features.ms_samples[(binding - first_ms_binding) / 2] != VK_SAMPLE_COUNT_1_BIT; 158 return !ms_binding(binding) || features.ms_samples[(binding - first_ms_binding) / 2] != VK_SAMPLE_COUNT_1_BIT;
126} 159}
127 160
128// Every binding is visible to every shader stage. Preserve the single-sample layout on devices whose 161// Every binding is visible to every shader stage. Preserve the single-sample layout on devices whose
@@ -131,7 +164,8 @@ constexpr void limit_ms_bindings(BindlessFeatures& features, std::uint32_t sampl
131 std::uint32_t resource_limit) { 164 std::uint32_t resource_limit) {
132 const auto& counts = features.counts; 165 const auto& counts = features.counts;
133 std::uint32_t sampled = 0, total = 0; 166 std::uint32_t sampled = 0, total = 0;
134 for (std::uint32_t b = 0; b < first_ms_binding; ++b) { 167 for (std::uint32_t b = 0; b < binding_count; ++b) {
168 if (ms_binding(b)) continue;
135 if (bindings[b].type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) sampled += counts[b]; 169 if (bindings[b].type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) sampled += counts[b];
136 if (bindings[b].type != VK_DESCRIPTOR_TYPE_SAMPLER) total += counts[b]; 170 if (bindings[b].type != VK_DESCRIPTOR_TYPE_SAMPLER) total += counts[b];
137 } 171 }
@@ -153,12 +187,13 @@ constexpr bool multisampled(ImageDimension dimension) {
153} 187}
154 188
155constexpr std::uint32_t sampled_binding(ImageDimension dimension, std::uint32_t component_class = 0) { 189constexpr std::uint32_t sampled_binding(ImageDimension dimension, std::uint32_t component_class = 0) {
156 constexpr std::uint32_t by_dimension[] = {0, 3, 6, 5, first_ms_binding, first_ms_binding + 1}; 190 constexpr std::uint32_t by_dimension[] = {0, 3, 6, 5, no_binding, first_ms_binding, first_ms_binding + 1};
157 return by_dimension[std::uint32_t(dimension)] + (multisampled(dimension) ? component_class * 2 : 0); 191 return by_dimension[std::uint32_t(dimension)] + (multisampled(dimension) ? component_class * 2 : 0);
158} 192}
159 193
160// component_class: 0 = float, 1 = uint, 2 = sint. 194// component_class: 0 = float, 1 = uint, 2 = sint.
161constexpr std::uint32_t storage_binding(ImageDimension dimension, std::uint32_t component_class) { 195constexpr std::uint32_t storage_binding(ImageDimension dimension, std::uint32_t component_class) {
196 if (dimension == ImageDimension::dim_1d_array) return first_1d_array_storage_binding + component_class;
162 return multisampled(dimension) ? no_binding : 8 + 3 * std::uint32_t(dimension) + component_class; 197 return multisampled(dimension) ? no_binding : 8 + 3 * std::uint32_t(dimension) + component_class;
163} 198}
164 199
@@ -196,8 +231,13 @@ struct SetLayout {
196}; 231};
197 232
198// What size_bindings() grows to where the device's limits allow. At 32-64 bytes a descriptor the whole set is then 233// What size_bindings() grows to where the device's limits allow. At 32-64 bytes a descriptor the whole set is then
199// 3-6 MiB (93793 descriptors on an RTX 5070). 234// 5-10 MiB with 153697 descriptors when all MS classes and array bindings reach their limits (an RTX 5070).
200inline constexpr std::uint32_t max_2d_images = 65536; 235inline constexpr std::uint32_t max_2d_images = 65536;
236// The other single-sample dimensions: a title streams far more cube maps and texture arrays (and 1D lookup
237// textures) than the 1024 and 256 of the defaults, each distinct T# of one taking its own index.
238inline constexpr std::uint32_t max_2d_array_images = 16384;
239inline constexpr std::uint32_t max_3d_images = 4096;
240inline constexpr std::uint32_t max_1d_images = 4096;
201inline constexpr std::uint32_t max_storage_indices = 4096; // in each storage array, at most its dimension's images 241inline constexpr std::uint32_t max_storage_indices = 4096; // in each storage array, at most its dimension's images
202// A full D3D12 shader-visible sampler heap (2048) and room for literal and static S#s, within what the device can 242// A full D3D12 shader-visible sampler heap (2048) and room for literal and static S#s, within what the device can
203// create (maxSamplerAllocationCount, at least 4000) less the samplers that are not in the set. 243// create (maxSamplerAllocationCount, at least 4000) less the samplers that are not in the set.
@@ -212,10 +252,12 @@ inline constexpr std::uint32_t growth_headroom = 64;
212// standalone samplers do not, though they count in an update-after-bind pool. 252// standalone samplers do not, though they count in an update-after-bind pool.
213// 1. The default counts, or as much of them as the device holds: samplers, then storage, then 2D images. 253// 1. The default counts, or as much of them as the device holds: samplers, then storage, then 2D images.
214// 2. The MS pairs that fit beside them (limit_ms_bindings, which may turn classes off). 254// 2. The MS pairs that fit beside them (limit_ms_bindings, which may turn classes off).
215// 3. Growth towards the maximums above, in the same order, within maxPerSetDescriptors. That limit only says 255// 3. Growth towards the maximums above, in the same order, then the 2D-array, 3D and 1D images, within
216// what is sure to work (vkGetDescriptorSetLayoutSupport may allow more), so it never shrinks step 1. 256// maxPerSetDescriptors. That limit only says what is sure to work (vkGetDescriptorSetLayoutSupport may allow
217// A storage array holds the smaller of the storage count and its dimension's images: add_image never gives a 257// more), so it never shrinks step 1.
218// storage view an index past the sampled array. The other bindings keep their default counts. 258// A storage array holds the smaller of the storage count and its dimension's sampled count: add_image never
259// gives a storage view an index past the sampled array. The storage-only 1D array is capped at its default of 256.
260// The other bindings keep their default counts.
219// layout_supported, when given, answers vkGetDescriptorSetLayoutSupport for set 0 at the counts in its argument: 261// layout_supported, when given, answers vkGetDescriptorSetLayoutSupport for set 0 at the counts in its argument:
220// step 3 then also grows past maxPerSetDescriptors where the device says the layout works (Intel's Xe-LP reports 262// step 3 then also grows past maxPerSetDescriptors where the device says the layout works (Intel's Xe-LP reports
221// 6159 and supports the full sizes, D3D-219). It is asked only once the sum is above maxPerSetDescriptors. 263// 6159 and supports the full sizes, D3D-219). It is asked only once the sum is above maxPerSetDescriptors.
@@ -243,15 +285,24 @@ inline bool size_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimi
243 auto& counts = features.counts; 285 auto& counts = features.counts;
244 counts = default_counts(); 286 counts = default_counts();
245 std::uint32_t images = 1, samplers = 1, storage = 1; 287 std::uint32_t images = 1, samplers = 1, storage = 1;
288 std::uint32_t arrays = counts[sampled_binding(ImageDimension::dim_2d_array)];
289 std::uint32_t volumes = counts[sampled_binding(ImageDimension::dim_3d)];
290 std::uint32_t lines = counts[sampled_binding(ImageDimension::dim_1d)];
246 // Until the end the 2D storage arrays are not capped by the 2D images, which grow after them: capping can only 291 // Until the end the 2D storage arrays are not capped by the 2D images, which grow after them: capping can only
247 // take descriptors away. 292 // take descriptors away.
248 const auto apply = [&](bool final) { 293 const auto apply = [&](bool final) {
249 counts[sampled_binding(ImageDimension::dim_2d)] = images; 294 counts[sampled_binding(ImageDimension::dim_2d)] = images;
295 counts[sampled_binding(ImageDimension::dim_2d_array)] = arrays;
296 counts[sampled_binding(ImageDimension::dim_3d)] = volumes;
297 counts[sampled_binding(ImageDimension::dim_1d)] = lines;
250 counts[sampler_binding] = samplers; 298 counts[sampler_binding] = samplers;
251 for (std::uint32_t d = 0; d < 4; ++d) { 299 for (std::uint32_t d = 0; d <= std::uint32_t(ImageDimension::dim_1d_array); ++d) {
252 const auto dimension = ImageDimension(d); 300 const auto dimension = ImageDimension(d);
301 const auto sampled_at = sampled_binding(dimension);
302 const auto capacity = sampled_at == no_binding ? bindings[storage_binding(dimension, 0)].count
303 : counts[sampled_at];
253 const bool capped = final || dimension != ImageDimension::dim_2d; 304 const bool capped = final || dimension != ImageDimension::dim_2d;
254 const std::uint32_t held = capped ? std::min(storage, counts[sampled_binding(dimension)]) : storage; 305 const std::uint32_t held = capped ? std::min(storage, capacity) : storage;
255 for (std::uint32_t c = 0; c < 3; ++c) counts[storage_binding(dimension, c)] = held; 306 for (std::uint32_t c = 0; c < 3; ++c) counts[storage_binding(dimension, c)] = held;
256 if (r32ui_binding(dimension) != no_binding) counts[r32ui_binding(dimension)] = held; 307 if (r32ui_binding(dimension) != no_binding) counts[r32ui_binding(dimension)] = held;
257 } 308 }
@@ -305,6 +356,9 @@ inline bool size_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimi
305 grow(samplers, std::max(samplers, std::min(max_samplers, creatable)), growth_headroom, set_limit); 356 grow(samplers, std::max(samplers, std::min(max_samplers, creatable)), growth_headroom, set_limit);
306 grow(images, std::max(images, max_2d_images), growth_headroom, set_limit); 357 grow(images, std::max(images, max_2d_images), growth_headroom, set_limit);
307 grow(storage, std::max(storage, max_storage_indices), growth_headroom, set_limit); 358 grow(storage, std::max(storage, max_storage_indices), growth_headroom, set_limit);
359 grow(arrays, std::max(arrays, max_2d_array_images), growth_headroom, set_limit);
360 grow(volumes, std::max(volumes, max_3d_images), growth_headroom, set_limit);
361 grow(lines, std::max(lines, max_1d_images), growth_headroom, set_limit);
308 apply(true); 362 apply(true);
309 return full; 363 return full;
310} 364}
@@ -313,21 +367,29 @@ inline bool size_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimi
313 367
314class BindlessSet { 368class BindlessSet {
315public: 369public:
370 // `descriptor_hash` is the finalizer the shaders of this device probe the table with (descriptor_table.h); it is
371 // handed to the table before its first key is inserted, so every insert, lookup and rehash agrees with them.
316 BindlessSet(PFN_vkGetDeviceProcAddr get, VkDevice device, const VkPhysicalDeviceMemoryProperties& memory, 372 BindlessSet(PFN_vkGetDeviceProcAddr get, VkDevice device, const VkPhysicalDeviceMemoryProperties& memory,
317 BindlessFeatures features = {}) 373 BindlessFeatures features = {}, DescriptorHashKind descriptor_hash = DescriptorHashKind::fnv1a,
318 : device_(device), memory_(memory), features_(features) { 374 std::vector<std::uint32_t> families = {})
375 : device_(device), memory_(memory), features_(features), families_(std::move(families)),
376 table_(64, descriptor_hash) {
319 ok_ = vk_.load(get, device) && create_layout() && create_fallbacks() && create_table_buffers() && fill(); 377 ok_ = vk_.load(get, device) && create_layout() && create_fallbacks() && create_table_buffers() && fill();
320 } 378 }
321 379
322 ~BindlessSet() { 380 ~BindlessSet() {
323 release_retired(); 381 release_retired();
382 for (Generation& spare : spares_) destroy(spare);
383 spares_.clear();
324 destroy(table_buffers_); 384 destroy(table_buffers_);
325 if (fallback_sampler_) vk_.vkDestroySampler(device_, fallback_sampler_, nullptr); 385 if (fallback_sampler_) vk_.vkDestroySampler(device_, fallback_sampler_, nullptr);
326 for (const VkImageView view : fallback_views_) vk_.vkDestroyImageView(device_, view, nullptr); 386 for (const VkImageView view : fallback_views_) vk_.vkDestroyImageView(device_, view, nullptr);
327 for (const auto& image : fallback_images_) { 387 const auto destroy_image = [&](const FallbackImage& image) {
328 if (image.image) vk_.vkDestroyImage(device_, image.image, nullptr); 388 if (image.image) vk_.vkDestroyImage(device_, image.image, nullptr);
329 if (image.memory) vk_.vkFreeMemory(device_, image.memory, nullptr); 389 if (image.memory) vk_.vkFreeMemory(device_, image.memory, nullptr);
330 } 390 };
391 for (const auto& image : fallback_images_) destroy_image(image);
392 for (const auto& image : storage_fallback_images_) destroy_image(image);
331 if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); // frees the set 393 if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); // frees the set
332 if (layout_) vk_.vkDestroyDescriptorSetLayout(device_, layout_, nullptr); 394 if (layout_) vk_.vkDestroyDescriptorSetLayout(device_, layout_, nullptr);
333 } 395 }
@@ -342,7 +404,8 @@ public:
342 // How many images of a dimension the set holds, the fallback included, and how many of the lowest of those 404 // How many images of a dimension the set holds, the fallback included, and how many of the lowest of those
343 // indices its storage arrays hold (0 for the MS dimensions, which have none). The same for samplers. 405 // indices its storage arrays hold (0 for the MS dimensions, which have none). The same for samplers.
344 std::uint32_t image_capacity(ImageDimension dimension) const { 406 std::uint32_t image_capacity(ImageDimension dimension) const {
345 return features_.counts[bindless::sampled_binding(dimension)]; 407 const auto binding = bindless::sampled_binding(dimension);
408 return binding == bindless::no_binding ? storage_capacity(dimension) : features_.counts[binding];
346 } 409 }
347 std::uint32_t storage_capacity(ImageDimension dimension) const { 410 std::uint32_t storage_capacity(ImageDimension dimension) const {
348 return bindless::multisampled(dimension) ? 0 : features_.counts[bindless::storage_binding(dimension, 0)]; 411 return bindless::multisampled(dimension) ? 0 : features_.counts[bindless::storage_binding(dimension, 0)];
@@ -369,8 +432,12 @@ public:
369 void record_fallback_init(VkCommandBuffer command_buffer) { 432 void record_fallback_init(VkCommandBuffer command_buffer) {
370 fallbacks_ready_ = true; 433 fallbacks_ready_ = true;
371 std::vector<VkImageMemoryBarrier> barriers; 434 std::vector<VkImageMemoryBarrier> barriers;
372 for (const auto& image : fallback_images_) { 435 std::vector<const FallbackImage*> images;
373 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
374 VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; 441 VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
375 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; 442 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
376 barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; 443 barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -401,6 +468,7 @@ public:
401 std::uint32_t add_image(ImageDimension dimension, VkImageView sampled, VkImageLayout sampled_layout, 468 std::uint32_t add_image(ImageDimension dimension, VkImageView sampled, VkImageLayout sampled_layout,
402 const StorageViews& storage = {}, std::uint32_t sampled_class = 0) { 469 const StorageViews& storage = {}, std::uint32_t sampled_class = 0) {
403 const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui; 470 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;
404 if (bindless::multisampled(dimension) && (stores || !supports_ms_class(sampled_class))) return 0; 472 if (bindless::multisampled(dimension) && (stores || !supports_ms_class(sampled_class))) return 0;
405 auto& used = image_used_[std::uint32_t(dimension)]; 473 auto& used = image_used_[std::uint32_t(dimension)];
406 const std::uint32_t low_end = std::min(std::uint32_t(used.size()), storage_capacity(dimension)); 474 const std::uint32_t low_end = std::min(std::uint32_t(used.size()), storage_capacity(dimension));
@@ -414,16 +482,20 @@ public:
414 void update_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout, 482 void update_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout,
415 const StorageViews& storage = {}, std::uint32_t sampled_class = 0) { 483 const StorageViews& storage = {}, std::uint32_t sampled_class = 0) {
416 const auto& used = image_used_[std::uint32_t(dimension)]; 484 const auto& used = image_used_[std::uint32_t(dimension)];
417 if (index && index < used.size() && used[index]) 485 if (index && index < used.size() && used[index] == in_use)
418 write_image(dimension, index, sampled, sampled_layout, storage, sampled_class); 486 write_image(dimension, index, sampled, sampled_layout, storage, sampled_class);
419 } 487 }
420 488
421 // Returns the index to the fallback and frees it. The caller removes the image's keys with erase_key() first. 489 // Returns the index to the fallback and frees it. The caller removes the image's keys with erase_key() first.
490 // While the table has changes the GPU's copy lacks (an erased key may still name the index there), the index is
491 // held until a flush has brought the table up to date (one that did not degrade), so that no new image answers
492 // that key; otherwise it is free at once.
422 void remove_image(ImageDimension dimension, std::uint32_t index) { 493 void remove_image(ImageDimension dimension, std::uint32_t index) {
423 auto& used = image_used_[std::uint32_t(dimension)]; 494 auto& used = image_used_[std::uint32_t(dimension)];
424 if (!index || index >= used.size() || !used[index]) return; 495 if (!index || index >= used.size() || used[index] != in_use) return;
425 write_image(dimension, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}, 0); 496 write_image(dimension, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}, 0);
426 used[index] = 0; 497 used[index] = table_dirty_ ? held : 0;
498 if (table_dirty_) held_images_.push_back({dimension, index});
427 } 499 }
428 500
429 // 0 when the sampler array is full. 501 // 0 when the sampler array is full.
@@ -434,9 +506,10 @@ public:
434 } 506 }
435 507
436 void remove_sampler(std::uint32_t index) { 508 void remove_sampler(std::uint32_t index) {
437 if (!index || index >= sampler_used_.size() || !sampler_used_[index]) return; 509 if (!index || index >= sampler_used_.size() || sampler_used_[index] != in_use) return;
438 write_sampler(index, fallback_sampler_); 510 write_sampler(index, fallback_sampler_);
439 sampler_used_[index] = 0; 511 sampler_used_[index] = table_dirty_ ? held : 0;
512 if (table_dirty_) held_samplers_.push_back(index);
440 } 513 }
441 514
442 // T# and S# keys to the indices above: the table of binding 2 (descriptor_table.h). Changes reach the shaders 515 // T# and S# keys to the indices above: the table of binding 2 (descriptor_table.h). Changes reach the shaders
@@ -479,7 +552,10 @@ public:
479 void table_upload_result(bool submitted) { 552 void table_upload_result(bool submitted) {
480 if (!upload_in_flight_) return; 553 if (!upload_in_flight_) return;
481 upload_in_flight_ = false; 554 upload_in_flight_ = false;
482 if (submitted) return; 555 if (submitted) {
556 table_on_gpu_ = true; // the current buffers hold a table the GPU was given
557 return;
558 }
483 table_dirty_ = true; 559 table_dirty_ = true;
484 force_full_upload_ = true; 560 force_full_upload_ = true;
485 } 561 }
@@ -488,17 +564,42 @@ public:
488 // the set are recorded after it. When flush_replaces_table() was true this starts a new generation (table 564 // the set are recorded after it. When flush_replaces_table() was true this starts a new generation (table
489 // buffers, descriptor set, binding 2 and every descriptor written so far) and puts the previous one on the retire 565 // buffers, descriptor set, binding 2 and every descriptor written so far) and puts the previous one on the retire
490 // list, stamped `retire_ticket`: the owner's number for "every submission made so far", which release_retired() 566 // list, stamped `retire_ticket`: the owner's number for "every submission made so far", which release_retired()
491 // compares with what has completed. False when the new generation cannot be made: the set then still has the 567 // compares with what has completed.
492 // table as of the last flush. 568 // When the new generation cannot be made (no memory for its buffers or descriptor pool) and `completed_ticket` is
493 bool flush(VkCommandBuffer command_buffer, std::uint64_t retire_ticket = 0) { 569 // given, it is asked what has completed: the generations retired up to there are released (to the spares) and
570 // the generation is tried again; when everything up to `retire_ticket` is done, nothing reads the current table
571 // and it is rewritten in place if it is large enough. Failing both, the flush degrades instead of failing: the
572 // descriptors are written in place, as on a flush that does not replace the table (add_image writes new indices,
573 // which no key of the GPU's table reaches; remove_image writes the fallback, and the index stays held until a
574 // flush that does not degrade), the table keeps what it had, and its changes wait for a later flush
575 // (last_flush_degraded()). False only without a command buffer, when nothing can be recorded.
576 bool flush(VkCommandBuffer command_buffer, std::uint64_t retire_ticket = 0,
577 const std::function<std::uint64_t()>& completed_ticket = {}) {
578 if (!command_buffer) return false;
494 bool replaced = false; 579 bool replaced = false;
495 bool written = true; 580 bool written = true;
496 const bool must_replace = flush_replaces_table(); 581 const bool must_replace = flush_replaces_table();
582 last_flush_degraded_ = false;
497 if (table_dirty_ || !table_uploaded_) written = upload_table(command_buffer, retire_ticket, replaced); 583 if (table_dirty_ || !table_uploaded_) written = upload_table(command_buffer, retire_ticket, replaced);
584 if (!written && must_replace && completed_ticket) {
585 const std::uint64_t completed = completed_ticket();
586 if (!retired_.empty()) {
587 release_retired(completed);
588 written = upload_table(command_buffer, retire_ticket, replaced);
589 }
590 if (!written && completed >= retire_ticket) written = rewrite_table_in_place(command_buffer);
591 }
498 last_flush_replaced_ = replaced; 592 last_flush_replaced_ = replaced;
499 // The new generation could not be made: the current set is the one running work reads, so its descriptors are 593 // Degrading keeps what the current buffers hold, so only once they hold a table that reached the queue: a
500 // not written in place either. The pending writes and the table's changes stay for the next flush. 594 // first flush under full memory, or one after a failed submission of the only upload, has nothing on the GPU
501 if (!written && must_replace) return false; 595 // (word 0 is the probe mask) and refuses as before (review of #64).
596 if (!written && must_replace && !table_on_gpu_) return false;
597 if (!written && must_replace) {
598 // The current set is the one running work reads, and the table's changes may move or rebind live keys:
599 // they stay for a later flush, and keys added since find the fallback until then.
600 ++degraded_flushes_;
601 last_flush_degraded_ = true;
602 }
502 for (const Pending& pending : pending_) { 603 for (const Pending& pending : pending_) {
503 if (pending.binding >= shadow_.size() || pending.index >= shadow_[pending.binding].size()) continue; 604 if (pending.binding >= shadow_.size() || pending.index >= shadow_[pending.binding].size()) continue;
504 shadow_[pending.binding][pending.index] = pending.info; 605 shadow_[pending.binding][pending.index] = pending.info;
@@ -521,17 +622,29 @@ public:
521 } 622 }
522 pending_.clear(); 623 pending_.clear();
523 pending_slots_.clear(); 624 pending_slots_.clear();
524 return written; 625 if (!last_flush_degraded_) {
626 // The table this flush leaves on the GPU names no removed index any more: they can be given out again.
627 for (const auto& [dimension, index] : held_images_) image_used_[std::uint32_t(dimension)][index] = 0;
628 for (const std::uint32_t index : held_samplers_) sampler_used_[index] = 0;
629 held_images_.clear();
630 held_samplers_.clear();
631 }
632 return true;
525 } 633 }
526 634
527 // Frees the generations (table buffers and descriptor set) that flush() retired with a ticket up to `completed`. 635 // Releases the generations (table buffers and descriptor set) that flush() retired with a ticket up to
528 // The default frees all of them: when nothing submitted before the last flush() still runs. 636 // `completed`: up to max_spares of them, large enough for the table, are kept for the next replacing flush to
637 // reuse; the others are freed. The default releases all of them: when nothing submitted before the last flush()
638 // still runs.
529 void release_retired(std::uint64_t completed = UINT64_MAX) { 639 void release_retired(std::uint64_t completed = UINT64_MAX) {
530 std::size_t kept = 0; 640 std::size_t kept = 0;
531 for (auto& generation : retired_) { 641 for (auto& generation : retired_) {
532 if (generation.ticket <= completed) { 642 if (generation.ticket <= completed) {
533 destroy(generation.buffers); 643 if (spares_.size() < max_spares && generation.pool && generation.buffers.staging.mapped &&
534 if (generation.pool) vk_.vkDestroyDescriptorPool(device_, generation.pool, nullptr); 644 generation.buffers.table.size >= table_.size_bytes())
645 spares_.push_back(generation);
646 else
647 destroy(generation);
535 } else { 648 } else {
536 retired_[kept++] = generation; 649 retired_[kept++] = generation;
537 } 650 }
@@ -548,11 +661,32 @@ public:
548 std::uint64_t generations_made() const { return generations_made_; } 661 std::uint64_t generations_made() const { return generations_made_; }
549 std::size_t retired_count() const { return retired_.size(); } 662 std::size_t retired_count() const { return retired_.size(); }
550 663
664 // Released generations kept for reuse, and how many replacing flushes reused one instead of allocating. One: the
665 // current generation and a spare are the two a replacing flush has at its peak anyway (the new one, and the one
666 // retired until its work completes), and a grown table can be large (64 MiB at a million slots).
667 static constexpr std::size_t max_spares = 1;
668 std::size_t spare_count() const { return spares_.size(); }
669 std::uint64_t spares_reused() const { return spares_reused_; }
670 // Flushes that could not make the generation they needed and kept the previous table (see flush()), and whether
671 // the last one did; flushes that rewrote the table in place instead, every submission being done.
672 std::uint64_t degraded_flushes() const { return degraded_flushes_; }
673 bool last_flush_degraded() const { return last_flush_degraded_; }
674 std::uint64_t in_place_rewrites() const { return in_place_rewrites_; }
675 // Table buffers placed in HOST_VISIBLE memory because the DEVICE_LOCAL allocation failed.
676 std::uint64_t table_host_fallbacks() const { return table_host_fallbacks_; }
677 // What the last generation that could not be made failed to get: "table buffer", "staging buffer" or
678 // "descriptor set" (nullptr before any failure), and the Vulkan result of that call.
679 const char* last_failure() const { return last_failure_; }
680 VkResult last_failure_result() const { return last_failure_result_; }
681 // Bytes of the current table buffer (the table grows by doubling its slots: size_bytes() of table()).
682 std::size_t table_buffer_bytes() const { return table_buffers_.table.size; }
683
551private: 684private:
552 struct Functions { 685 struct Functions {
553#define LEONIDA_BINDLESS_FUNCTIONS(X) \ 686#define LEONIDA_BINDLESS_FUNCTIONS(X) \
554 X(vkCreateDescriptorSetLayout) X(vkDestroyDescriptorSetLayout) X(vkCreateDescriptorPool) \ 687 X(vkCreateDescriptorSetLayout) X(vkDestroyDescriptorSetLayout) X(vkCreateDescriptorPool) \
555 X(vkDestroyDescriptorPool) X(vkAllocateDescriptorSets) X(vkUpdateDescriptorSets) X(vkCreateBuffer) \ 688 X(vkDestroyDescriptorPool) X(vkResetDescriptorPool) X(vkAllocateDescriptorSets) X(vkUpdateDescriptorSets) \
689 X(vkCreateBuffer) \
556 X(vkDestroyBuffer) X(vkGetBufferMemoryRequirements) X(vkBindBufferMemory) X(vkAllocateMemory) \ 690 X(vkDestroyBuffer) X(vkGetBufferMemoryRequirements) X(vkBindBufferMemory) X(vkAllocateMemory) \
557 X(vkFreeMemory) X(vkMapMemory) X(vkCreateImage) X(vkDestroyImage) X(vkGetImageMemoryRequirements) \ 691 X(vkFreeMemory) X(vkMapMemory) X(vkCreateImage) X(vkDestroyImage) X(vkGetImageMemoryRequirements) \
558 X(vkBindImageMemory) X(vkCreateImageView) X(vkDestroyImageView) X(vkCreateSampler) X(vkDestroySampler) \ 692 X(vkBindImageMemory) X(vkCreateImageView) X(vkDestroyImageView) X(vkCreateSampler) X(vkDestroySampler) \
@@ -583,6 +717,7 @@ private:
583 VkDeviceMemory memory = VK_NULL_HANDLE; 717 VkDeviceMemory memory = VK_NULL_HANDLE;
584 void* mapped = nullptr; 718 void* mapped = nullptr;
585 std::size_t size = 0; 719 std::size_t size = 0;
720 bool device_local = false; // the memory is DEVICE_LOCAL
586 }; 721 };
587 722
588 // The table and the mapped staging copy the CPU writes before vkCmdCopyBuffer moves the changes. 723 // The table and the mapped staging copy the CPU writes before vkCmdCopyBuffer moves the changes.
@@ -591,13 +726,23 @@ private:
591 TableBuffer staging; // HOST_VISIBLE | HOST_COHERENT 726 TableBuffer staging; // HOST_VISIBLE | HOST_COHERENT
592 }; 727 };
593 728
729 struct Generation { // what a replaced table leaves for release_retired()
730 TableBuffers buffers;
731 VkDescriptorPool pool = VK_NULL_HANDLE; // owns the descriptor set
732 VkDescriptorSet set = VK_NULL_HANDLE;
733 std::uint64_t ticket = 0;
734 };
735
594 struct Pending { 736 struct Pending {
595 std::uint32_t binding; 737 std::uint32_t binding;
596 std::uint32_t index; 738 std::uint32_t index;
597 VkDescriptorImageInfo info; 739 VkDescriptorImageInfo info;
598 }; 740 };
599 741
600 static constexpr std::uint32_t dimensions = 6, classes = 3; 742 static constexpr std::uint32_t dimensions = 7, classes = 3;
743 // image_used_ and sampler_used_: 0 free, in_use, or held (removed while the table was dirty: free after the next
744 // flush that does not degrade).
745 static constexpr std::uint8_t in_use = 1, held = 2;
601 746
602 // The first free index in [begin, end), marked used; 0 when there is none. 747 // The first free index in [begin, end), marked used; 0 when there is none.
603 static std::uint32_t allocate(std::vector<std::uint8_t>& used, std::uint32_t begin, std::uint32_t end) { 748 static std::uint32_t allocate(std::vector<std::uint8_t>& used, std::uint32_t begin, std::uint32_t end) {
@@ -645,21 +790,35 @@ private:
645 pool_info.maxSets = 1; 790 pool_info.maxSets = 1;
646 pool_info.poolSizeCount = pool_size_count_; 791 pool_info.poolSizeCount = pool_size_count_;
647 pool_info.pPoolSizes = pool_sizes_.data(); 792 pool_info.pPoolSizes = pool_sizes_.data();
648 if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool) != VK_SUCCESS) { 793 if (const VkResult result = vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool);
794 result != VK_SUCCESS) {
649 pool = VK_NULL_HANDLE; 795 pool = VK_NULL_HANDLE;
796 note_failure("descriptor set", result);
650 return false; 797 return false;
651 } 798 }
799 if (allocate_set(pool, set)) return true;
800 vk_.vkDestroyDescriptorPool(device_, pool, nullptr);
801 pool = VK_NULL_HANDLE;
802 return false;
803 }
804
805 bool allocate_set(VkDescriptorPool pool, VkDescriptorSet& set) const {
652 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; 806 VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
653 set_info.descriptorPool = pool; 807 set_info.descriptorPool = pool;
654 set_info.descriptorSetCount = 1; 808 set_info.descriptorSetCount = 1;
655 set_info.pSetLayouts = &layout_; 809 set_info.pSetLayouts = &layout_;
656 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set) == VK_SUCCESS) return true; 810 const VkResult result = vk_.vkAllocateDescriptorSets(device_, &set_info, &set);
657 vk_.vkDestroyDescriptorPool(device_, pool, nullptr); 811 if (result == VK_SUCCESS) return true;
658 pool = VK_NULL_HANDLE;
659 set = VK_NULL_HANDLE; 812 set = VK_NULL_HANDLE;
813 note_failure("descriptor set", result);
660 return false; 814 return false;
661 } 815 }
662 816
817 void note_failure(const char* what, VkResult result) const {
818 last_failure_ = what;
819 last_failure_result_ = result;
820 }
821
663 // Writes every descriptor written so far (the shadow) into the current set, one update per run of written 822 // Writes every descriptor written so far (the shadow) into the current set, one update per run of written
664 // elements: the set was just made, and unwritten elements stay unwritten, as partially_bound allows. 823 // elements: the set was just made, and unwritten elements stay unwritten, as partially_bound allows.
665 void write_shadow() const { 824 void write_shadow() const {
@@ -688,59 +847,82 @@ private:
688 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);
689 } 848 }
690 849
691 // 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.
692 bool create_fallbacks() { 853 bool create_fallbacks() {
693 // 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.
694 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};
695 constexpr std::uint32_t ms_type = 3; 857 constexpr std::uint32_t ms_type = 3;
696 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};
697 // dimension -> (index into types, view type) 859 // dimension -> (index into types, view type)
698 constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2, ms_type, ms_type}; 860 constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2, 2, ms_type, ms_type};
699 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,
700 VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D, 862 VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D,
701 VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY}; 863 VK_IMAGE_VIEW_TYPE_1D_ARRAY, VK_IMAGE_VIEW_TYPE_2D,
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 };
702 for (std::uint32_t t = 0; t < std::size(types); ++t) { 895 for (std::uint32_t t = 0; t < std::size(types); ++t) {
703 for (std::uint32_t c = 0; c < classes; ++c) { 896 for (std::uint32_t c = 0; c < classes; ++c) {
704 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),
705 VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; 898 // every supported class of MS.
706 info.imageType = types[t]; 899 if (t == ms_type ? supports_ms_class(c) : c == 0) {
707 info.format = formats[c]; 900 if (!create_image(fallback_images_[t * classes + c], t, c, VK_IMAGE_USAGE_SAMPLED_BIT)) return false;
708 info.extent = {1, 1, 1}; 901 }
709 info.mipLevels = 1; 902 if (t != ms_type && !features_.null_descriptor &&
710 info.arrayLayers = 1; 903 !create_image(storage_fallback_images_[t * classes + c], t, c, VK_IMAGE_USAGE_STORAGE_BIT))
711 info.samples = t == ms_type ? features_.ms_samples[c] : VK_SAMPLE_COUNT_1_BIT;
712 info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
713 if (t != ms_type) info.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
714 if (t == ms_type || c == 0) info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
715 FallbackImage& image = fallback_images_[t * classes + c];
716 if (vk_.vkCreateImage(device_, &info, nullptr, &image.image) != VK_SUCCESS) return false;
717 VkMemoryRequirements requirements{};
718 vk_.vkGetImageMemoryRequirements(device_, image.image, &requirements);
719 VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
720 allocation.allocationSize = requirements.size;
721 allocation.memoryTypeIndex = memory_type(requirements.memoryTypeBits, 0);
722 if (allocation.memoryTypeIndex == 0xffffffffu ||
723 vk_.vkAllocateMemory(device_, &allocation, nullptr, &image.memory) != VK_SUCCESS ||
724 vk_.vkBindImageMemory(device_, image.image, image.memory, 0) != VK_SUCCESS)
725 return false; 904 return false;
726 } 905 }
727 } 906 }
728 for (std::uint32_t d = 0; d < dimensions; ++d) { 907 for (std::uint32_t d = 0; d < dimensions; ++d) {
729 const ImageDimension dimension{d}; 908 const ImageDimension dimension{d};
730 for (std::uint32_t c = 0; c < classes; ++c) { 909 for (std::uint32_t c = 0; c < classes; ++c) {
731 VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; 910 const FallbackImage& sampled = fallback_images_[type_of[d] * classes + c];
732 info.image = fallback_images_[type_of[d] * classes + c].image; 911 if (sampled.image) {
733 if (!info.image) continue; 912 VkImageView view = VK_NULL_HANDLE;
734 info.viewType = view_types[d]; 913 if (!create_view(sampled, d, c, view)) return false;
735 info.format = formats[c]; 914 const auto sampled_at = bindless::sampled_binding(dimension, c);
736 info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; 915 if (sampled_at != bindless::no_binding && (c == 0 || bindless::multisampled(dimension)))
737 VkImageView view = VK_NULL_HANDLE; 916 fallback_by_binding_[sampled_at] = view;
738 if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false; 917 }
739 fallback_views_.push_back(view);
740 if (c == 0 || bindless::multisampled(dimension))
741 fallback_by_binding_[bindless::sampled_binding(dimension, c)] = view;
742 const auto storage_at = bindless::storage_binding(dimension, c); 918 const auto storage_at = bindless::storage_binding(dimension, c);
743 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;
744 const auto r32ui_at = bindless::r32ui_binding(dimension); 926 const auto r32ui_at = bindless::r32ui_binding(dimension);
745 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;
746 } 928 }
@@ -753,36 +935,54 @@ private:
753 return vk_.vkCreateSampler(device_, &sampler, nullptr, &fallback_sampler_) == VK_SUCCESS; 935 return vk_.vkCreateSampler(device_, &sampler, nullptr, &fallback_sampler_) == VK_SUCCESS;
754 } 936 }
755 937
756 bool create(TableBuffer& out, std::size_t bytes, bool device_local, bool* got_device_local = nullptr) const { 938 // device_local: the table (DEVICE_LOCAL, else HOST_VISIBLE when the device has no such type or that heap is out
939 // of memory); otherwise the mapped staging buffer (HOST_VISIBLE | HOST_COHERENT).
940 bool create(TableBuffer& out, std::size_t bytes, bool device_local) {
941 constexpr VkMemoryPropertyFlags host = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
942 const char* what = device_local ? "table buffer" : "staging buffer";
757 VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; 943 VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
758 info.size = bytes; 944 info.size = bytes;
759 info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | 945 info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
760 (device_local ? VK_BUFFER_USAGE_TRANSFER_DST_BIT : VK_BUFFER_USAGE_TRANSFER_SRC_BIT); 946 (device_local ? VK_BUFFER_USAGE_TRANSFER_DST_BIT : VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
761 TableBuffer made; 947 TableBuffer made;
762 made.size = bytes; 948 made.size = bytes;
763 if (vk_.vkCreateBuffer(device_, &info, nullptr, &made.buffer) != VK_SUCCESS) return false; 949 share_across_queue_families(info, families_);
950 if (const VkResult result = vk_.vkCreateBuffer(device_, &info, nullptr, &made.buffer); result != VK_SUCCESS) {
951 made.buffer = VK_NULL_HANDLE;
952 note_failure(what, result);
953 return false;
954 }
764 VkMemoryRequirements requirements{}; 955 VkMemoryRequirements requirements{};
765 vk_.vkGetBufferMemoryRequirements(device_, made.buffer, &requirements); 956 vk_.vkGetBufferMemoryRequirements(device_, made.buffer, &requirements);
766 VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; 957 VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
767 allocation.allocationSize = requirements.size; 958 allocation.allocationSize = requirements.size;
768 allocation.memoryTypeIndex = 959 const auto allocate = [&](std::uint32_t type) {
769 device_local ? memory_type(requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) 960 if (type == 0xffffffffu) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
770 : memory_type(requirements.memoryTypeBits, 961 allocation.memoryTypeIndex = type;
771 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); 962 const VkResult result = vk_.vkAllocateMemory(device_, &allocation, nullptr, &made.memory);
772 if (device_local && allocation.memoryTypeIndex == 0xffffffffu) 963 if (result != VK_SUCCESS) made.memory = VK_NULL_HANDLE;
773 allocation.memoryTypeIndex = memory_type(requirements.memoryTypeBits, 964 return result;
774 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | 965 };
775 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); 966 const std::uint32_t local = device_local
776 if (got_device_local) 967 ? memory_type(requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
777 *got_device_local = allocation.memoryTypeIndex != 0xffffffffu && 968 : 0xffffffffu;
778 (memory_.memoryTypes[allocation.memoryTypeIndex].propertyFlags & 969 VkResult result = device_local ? allocate(local) : allocate(memory_type(requirements.memoryTypeBits, host));
970 if (result != VK_SUCCESS && device_local) {
971 const std::uint32_t fallback = memory_type(requirements.memoryTypeBits, host);
972 if (fallback != local) {
973 result = allocate(fallback);
974 if (result == VK_SUCCESS && local != 0xffffffffu) ++table_host_fallbacks_;
975 }
976 }
977 if (result == VK_SUCCESS) {
978 made.device_local = (memory_.memoryTypes[allocation.memoryTypeIndex].propertyFlags &
779 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0; 979 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0;
780 bool ready = allocation.memoryTypeIndex != 0xffffffffu && 980 result = vk_.vkBindBufferMemory(device_, made.buffer, made.memory, 0);
781 vk_.vkAllocateMemory(device_, &allocation, nullptr, &made.memory) == VK_SUCCESS && 981 }
782 vk_.vkBindBufferMemory(device_, made.buffer, made.memory, 0) == VK_SUCCESS; 982 if (result == VK_SUCCESS && !device_local)
783 if (ready && !device_local) 983 result = vk_.vkMapMemory(device_, made.memory, 0, VK_WHOLE_SIZE, 0, &made.mapped);
784 ready = vk_.vkMapMemory(device_, made.memory, 0, VK_WHOLE_SIZE, 0, &made.mapped) == VK_SUCCESS; 984 if (result != VK_SUCCESS) {
785 if (!ready) { 985 note_failure(what, result);
786 destroy(made); 986 destroy(made);
787 return false; 987 return false;
788 } 988 }
@@ -801,17 +1001,55 @@ private:
801 destroy(buffers.table); 1001 destroy(buffers.table);
802 } 1002 }
803 1003
1004 void destroy(Generation& generation) const {
1005 destroy(generation.buffers);
1006 if (generation.pool) vk_.vkDestroyDescriptorPool(device_, generation.pool, nullptr); // frees the set
1007 generation.pool = VK_NULL_HANDLE;
1008 generation.set = VK_NULL_HANDLE;
1009 }
1010
1011 // A generation for a table of `bytes`: a spare when one is large enough (its pool reset for a fresh set), else
1012 // new buffers and a new pool. Spares too small for the table are freed: the table never shrinks.
1013 bool make_generation(std::size_t bytes, TableBuffers& buffers, VkDescriptorPool& pool, VkDescriptorSet& set) {
1014 std::size_t kept = 0;
1015 bool found = false;
1016 for (Generation& spare : spares_) {
1017 if (spare.buffers.table.size < bytes || spare.buffers.staging.size < bytes) {
1018 destroy(spare);
1019 continue;
1020 }
1021 if (!found && vk_.vkResetDescriptorPool(device_, spare.pool, 0) == VK_SUCCESS &&
1022 allocate_set(spare.pool, set)) {
1023 buffers = spare.buffers;
1024 pool = spare.pool;
1025 found = true;
1026 ++spares_reused_;
1027 continue;
1028 }
1029 spares_[kept++] = spare;
1030 }
1031 spares_.resize(kept);
1032 if (found) return true;
1033 TableBuffers made;
1034 if (!create(made.table, bytes, true) || !create(made.staging, bytes, false) || !make_set(pool, set)) {
1035 destroy(made);
1036 return false;
1037 }
1038 buffers = made;
1039 return true;
1040 }
1041
804 bool create_table_buffers() { 1042 bool create_table_buffers() {
805 const std::size_t bytes = table_.size_bytes(); 1043 const std::size_t bytes = table_.size_bytes();
806 TableBuffers made; 1044 TableBuffers made;
807 bool device_local = false; 1045 if (!create(made.table, bytes, true) || !create(made.staging, bytes, false)) {
808 if (!create(made.table, bytes, true, &device_local) || !create(made.staging, bytes, false)) {
809 destroy(made); 1046 destroy(made);
810 return false; 1047 return false;
811 } 1048 }
812 table_buffers_ = made; 1049 table_buffers_ = made;
813 table_device_local_ = device_local; 1050 table_device_local_ = made.table.device_local;
814 table_uploaded_ = false; // the first flush() uploads it, in the command buffer that will use it 1051 table_uploaded_ = false; // the first flush() uploads it, in the command buffer that will use it
1052 table_on_gpu_ = false;
815 table_dirty_ = existing_entries_changed_ = false; 1053 table_dirty_ = existing_entries_changed_ = false;
816 return true; 1054 return true;
817 } 1055 }
@@ -829,15 +1067,11 @@ private:
829 TableBuffers larger; 1067 TableBuffers larger;
830 VkDescriptorPool pool = VK_NULL_HANDLE; 1068 VkDescriptorPool pool = VK_NULL_HANDLE;
831 VkDescriptorSet set = VK_NULL_HANDLE; 1069 VkDescriptorSet set = VK_NULL_HANDLE;
832 bool device_local = false; 1070 if (!make_generation(bytes, larger, pool, set)) return false;
833 if (!create(larger.table, bytes, true, &device_local) || !create(larger.staging, bytes, false) || 1071 table_device_local_ = larger.table.device_local;
834 !make_set(pool, set)) {
835 destroy(larger);
836 return false;
837 }
838 table_device_local_ = device_local;
839 retired_.push_back({table_buffers_, pool_, set_, retire_ticket}); 1072 retired_.push_back({table_buffers_, pool_, set_, retire_ticket});
840 table_buffers_ = larger; 1073 table_buffers_ = larger;
1074 table_on_gpu_ = false; // until this generation's upload is submitted (table_upload_result)
841 pool_ = pool; 1075 pool_ = pool;
842 set_ = set; 1076 set_ = set;
843 write_table_binding(table_buffers_.table.buffer); 1077 write_table_binding(table_buffers_.table.buffer);
@@ -893,6 +1127,25 @@ private:
893 return true; 1127 return true;
894 } 1128 }
895 1129
1130 // The whole table into the current generation's buffers, for a flush whose new generation could not be made,
1131 // once every submission that could read the current table is done (flush()). False when the table outgrew them.
1132 bool rewrite_table_in_place(VkCommandBuffer command_buffer) {
1133 const std::size_t bytes = table_.size_bytes();
1134 if (bytes > table_buffers_.table.size || bytes > table_buffers_.staging.size || !table_buffers_.staging.mapped)
1135 return false;
1136 table_.take_changes(); // all of it goes
1137 // Nothing still runs that copies from the staging buffer or reads the table.
1138 table_.write_to_buffer(table_buffers_.staging.mapped);
1139 copy_table_range(command_buffer, 0, bytes);
1140 record_table_barrier(command_buffer);
1141 upload_in_flight_ = true;
1142 table_dirty_ = existing_entries_changed_ = false;
1143 table_uploaded_ = true;
1144 force_full_upload_ = false;
1145 ++in_place_rewrites_;
1146 return true;
1147 }
1148
896 // Copies the data into the command buffer while recording (64 KiB per call, the Vulkan limit), so no staging 1149 // Copies the data into the command buffer while recording (64 KiB per call, the Vulkan limit), so no staging
897 // buffer is read at execution: a later flush can rewrite its own staging without racing a pending copy. 1150 // buffer is read at execution: a later flush can rewrite its own staging without racing a pending copy.
898 void update_table_range(VkCommandBuffer command_buffer, VkDeviceSize offset, const void* data, 1151 void update_table_range(VkCommandBuffer command_buffer, VkDeviceSize offset, const void* data,
@@ -998,8 +1251,9 @@ private:
998 return; 1251 return;
999 } 1252 }
1000 const std::uint32_t sampled_at = bindless::sampled_binding(dimension); 1253 const std::uint32_t sampled_at = bindless::sampled_binding(dimension);
1001 queue(sampled_at, index, 1254 if (sampled_at != bindless::no_binding)
1002 sampled ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout} : fallback_info(sampled_at)); 1255 queue(sampled_at, index,
1256 sampled ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout} : fallback_info(sampled_at));
1003 const auto store = [&](std::uint32_t binding, VkImageView view) { 1257 const auto store = [&](std::uint32_t binding, VkImageView view) {
1004 if (binding == bindless::no_binding || index >= features_.counts[binding]) return; 1258 if (binding == bindless::no_binding || index >= features_.counts[binding]) return;
1005 queue(binding, index, 1259 queue(binding, index,
@@ -1014,37 +1268,45 @@ private:
1014 VkDevice device_; 1268 VkDevice device_;
1015 VkPhysicalDeviceMemoryProperties memory_; 1269 VkPhysicalDeviceMemoryProperties memory_;
1016 BindlessFeatures features_; 1270 BindlessFeatures features_;
1271 std::vector<std::uint32_t> families_;
1017 Functions vk_; 1272 Functions vk_;
1018 bool ok_ = false; 1273 bool ok_ = false;
1019 VkDescriptorSetLayout layout_ = VK_NULL_HANDLE; 1274 VkDescriptorSetLayout layout_ = VK_NULL_HANDLE;
1020 VkDescriptorPool pool_ = VK_NULL_HANDLE; 1275 VkDescriptorPool pool_ = VK_NULL_HANDLE;
1021 VkDescriptorSet set_ = VK_NULL_HANDLE; 1276 VkDescriptorSet set_ = VK_NULL_HANDLE;
1022 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_{};
1023 std::vector<VkImageView> fallback_views_; 1280 std::vector<VkImageView> fallback_views_;
1024 std::array<VkImageView, bindless::bindings.size()> fallback_by_binding_{}; 1281 std::array<VkImageView, bindless::bindings.size()> fallback_by_binding_{};
1025 VkSampler fallback_sampler_ = VK_NULL_HANDLE; 1282 VkSampler fallback_sampler_ = VK_NULL_HANDLE;
1026 bool fallbacks_ready_ = false; 1283 bool fallbacks_ready_ = false;
1027 std::array<std::vector<std::uint8_t>, dimensions> image_used_; 1284 std::array<std::vector<std::uint8_t>, dimensions> image_used_;
1028 std::vector<std::uint8_t> sampler_used_; 1285 std::vector<std::uint8_t> sampler_used_;
1286 std::vector<std::pair<ImageDimension, std::uint32_t>> held_images_; // removed, not yet free (remove_image)
1287 std::vector<std::uint32_t> held_samplers_;
1029 std::vector<Pending> pending_; 1288 std::vector<Pending> pending_;
1030 std::unordered_map<std::uint64_t, std::size_t> pending_slots_; // binding and index -> place in pending_ 1289 std::unordered_map<std::uint64_t, std::size_t> pending_slots_; // binding and index -> place in pending_
1031 DescriptorTable table_{64}; 1290 DescriptorTable table_; // 64 slots, and the device's hash kind, from the constructor
1032 TableBuffers table_buffers_; 1291 TableBuffers table_buffers_;
1033 bool table_uploaded_ = false; // the table's current contents are in table_buffers_.table 1292 bool table_uploaded_ = false; // the table's current contents are in table_buffers_.table
1293 bool table_on_gpu_ = false; // an upload into the current table buffers reached the queue (degrading is safe)
1034 bool table_device_local_ = false; 1294 bool table_device_local_ = false;
1035 bool table_dirty_ = false; // the table differs from the buffer 1295 bool table_dirty_ = false; // the table differs from the buffer
1036 bool existing_entries_changed_ = false; // ... and not only by entries added 1296 bool existing_entries_changed_ = false; // ... and not only by entries added
1037 bool upload_in_flight_ = false; // a copy is recorded, not yet known to have reached the queue 1297 bool upload_in_flight_ = false; // a copy is recorded, not yet known to have reached the queue
1038 bool force_full_upload_ = false; // the last submit failed: rebuild the whole table on the next flush 1298 bool force_full_upload_ = false; // the last submit failed: rebuild the whole table on the next flush
1039 struct Generation { // what a replaced table leaves for release_retired()
1040 TableBuffers buffers;
1041 VkDescriptorPool pool = VK_NULL_HANDLE; // owns the descriptor set
1042 VkDescriptorSet set = VK_NULL_HANDLE;
1043 std::uint64_t ticket = 0;
1044 };
1045 std::vector<Generation> retired_; 1299 std::vector<Generation> retired_;
1300 std::vector<Generation> spares_; // released generations kept for reuse (release_retired)
1046 std::uint64_t generations_made_ = 0; 1301 std::uint64_t generations_made_ = 0;
1047 bool last_flush_replaced_ = false; 1302 bool last_flush_replaced_ = false;
1303 bool last_flush_degraded_ = false;
1304 std::uint64_t spares_reused_ = 0;
1305 std::uint64_t degraded_flushes_ = 0;
1306 std::uint64_t in_place_rewrites_ = 0;
1307 std::uint64_t table_host_fallbacks_ = 0;
1308 mutable const char* last_failure_ = nullptr;
1309 mutable VkResult last_failure_result_ = VK_SUCCESS;
1048 std::array<VkDescriptorPoolSize, bindless::bindings.size()> pool_sizes_{}; 1310 std::array<VkDescriptorPoolSize, bindless::bindings.size()> pool_sizes_{};
1049 std::uint32_t pool_size_count_ = 0; 1311 std::uint32_t pool_size_count_ = 0;
1050 // Every descriptor written to the set (the table's binding aside), to fill the next generation's set. 1312 // Every descriptor written to the set (the table's binding aside), to fill the next generation's set.