upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 03:14:28 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 03:14:28 -0500
commit209f5e1f2a42fcfbf9916c2d0333fa906bb931ef (patch)
tree953cab766d5e0a6aac2d53aa32c434689875ad30 /runtime
parentde4d3321a34a6c4bed5ba5afe5fb5328095ceab1 (diff)
parentea512f284ce3b58c144850d70371091e1522ce72 (diff)
Merge pull request #1199 from quackottu
Diffstat (limited to 'runtime')
-rw-r--r--runtime/CMakeLists.txt6
-rw-r--r--runtime/shims/d3d12_vulkan_device.cpp18
-rw-r--r--runtime/src/vulkan_bindless.h184
-rw-r--r--runtime/test/bindless_sizing_test.cpp179
-rw-r--r--runtime/test/d3d12_descriptor_test.cpp82
-rw-r--r--runtime/test/vk_bindless_test.cpp47
6 files changed, 467 insertions, 49 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index 787dff10c..7594136d0 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -640,6 +640,12 @@ if(TARGET Vulkan::Headers)
640 if(BUILD_TESTING) 640 if(BUILD_TESTING)
641 add_test(NAME vk_device_pick COMMAND vk_device_pick_test) 641 add_test(NAME vk_device_pick COMMAND vk_device_pick_test)
642 endif() 642 endif()
643 # CPU-only: the bindless set's array sizes from synthetic device limits.
644 add_executable(bindless_sizing_test test/bindless_sizing_test.cpp)
645 target_link_libraries(bindless_sizing_test PRIVATE Vulkan::Headers)
646 if(BUILD_TESTING)
647 add_test(NAME bindless_sizing COMMAND bindless_sizing_test)
648 endif()
643 if(NOT Vulkan_GLSLANG_VALIDATOR_EXECUTABLE) 649 if(NOT Vulkan_GLSLANG_VALIDATOR_EXECUTABLE)
644 find_program(Vulkan_GLSLANG_VALIDATOR_EXECUTABLE glslangValidator HINTS "$ENV{VULKAN_SDK}/Bin" "$ENV{VULKAN_SDK}/bin") 650 find_program(Vulkan_GLSLANG_VALIDATOR_EXECUTABLE glslangValidator HINTS "$ENV{VULKAN_SDK}/Bin" "$ENV{VULKAN_SDK}/bin")
645 endif() 651 endif()
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp
index 2bbfc4b45..f92c07581 100644
--- a/runtime/shims/d3d12_vulkan_device.cpp
+++ b/runtime/shims/d3d12_vulkan_device.cpp
@@ -1539,12 +1539,24 @@ public:
1539 state_.info.get_instance_proc(state_.info.instance, "vkGetPhysicalDeviceProperties2")); 1539 state_.info.get_instance_proc(state_.info.instance, "vkGetPhysicalDeviceProperties2"));
1540 if (get_properties) { 1540 if (get_properties) {
1541 VkPhysicalDeviceVulkan12Properties indexed{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES}; 1541 VkPhysicalDeviceVulkan12Properties indexed{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES};
1542 VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &indexed}; 1542 VkPhysicalDeviceVulkan11Properties v11{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES, &indexed};
1543 VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &v11};
1543 get_properties(state_.info.physical, &properties); 1544 get_properties(state_.info.physical, &properties);
1544 features.ms_samples = {VK_SAMPLE_COUNT_4_BIT, state_.ms_fallback_samples(VK_FORMAT_R32_UINT), 1545 features.ms_samples = {VK_SAMPLE_COUNT_4_BIT, state_.ms_fallback_samples(VK_FORMAT_R32_UINT),
1545 state_.ms_fallback_samples(VK_FORMAT_R32_SINT)}; 1546 state_.ms_fallback_samples(VK_FORMAT_R32_SINT)};
1546 leonida::bindless::limit_ms_bindings(features, properties.properties.limits, indexed); 1547 if (!leonida::bindless::size_bindings(features, properties.properties.limits, v11, indexed))
1547 } 1548 std::fprintf(stderr, "[rt] d3d12: the device's descriptor limits are below the bindless set's default "
1549 "sizes; it holds less\n");
1550 }
1551 namespace b = leonida::bindless;
1552 std::fprintf(stderr, "[rt] d3d12: bindless set: %u 2D images, %u samplers, %u 2D-array, %u 3D, %u 1D, "
1553 "%u storage indices in 2D\n",
1554 features.counts[b::sampled_binding(leonida::ImageDimension::dim_2d)],
1555 features.counts[b::sampler_binding],
1556 features.counts[b::sampled_binding(leonida::ImageDimension::dim_2d_array)],
1557 features.counts[b::sampled_binding(leonida::ImageDimension::dim_3d)],
1558 features.counts[b::sampled_binding(leonida::ImageDimension::dim_1d)],
1559 features.counts[b::storage_binding(leonida::ImageDimension::dim_2d, 0)]);
1548 state_.bindless = std::make_unique<leonida::BindlessSet>(get_device, state_.info.device, state_.memory, features); 1560 state_.bindless = std::make_unique<leonida::BindlessSet>(get_device, state_.info.device, state_.memory, features);
1549 if (!state_.bindless->ok() || !state_.context->vk.vkCreateSampler) { 1561 if (!state_.bindless->ok() || !state_.context->vk.vkCreateSampler) {
1550 state_.bindless.reset(); 1562 state_.bindless.reset();
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h
index f68b6f292..3bf07fa5d 100644
--- a/runtime/src/vulkan_bindless.h
+++ b/runtime/src/vulkan_bindless.h
@@ -11,14 +11,17 @@
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// 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
15// from the device's limits instead; the shaders declare runtime arrays, so any count works for them.
14// 16//
15// A shader turns a T# or S# into an index through the hash table (descriptor_table.h) and uses that index in 17// A shader turns a T# or S# into an index through the hash table (descriptor_table.h) and uses that index in
16// whichever binding the instruction calls for: the sampled array of the image's dimension, or one of its storage 18// whichever binding the instruction calls for: the sampled array of the image's dimension, or one of its storage
17// arrays. So an image has one index, the same in every binding of its dimension, and each dimension has its own 19// arrays. So an image has one index, the same in every binding of its dimension, and each dimension has its own
18// index space. The storage arrays hold 256, so an image with storage views gets an index below 256; one without 20// index space. The storage arrays are shorter (storage_capacity()), so an image with storage views gets an index
19// takes a higher index while there is one. Index 0 of every array is a fallback, because a miss gives 0: a 1x1 21// they hold; one without takes a higher index while there is one. Index 0 of every array is a fallback, because a
20// image of zeros, a nearest clamping sampler, and 1x1 storage images. The caller owns the views and samplers it 22// miss gives 0: a 1x1 image of zeros, a nearest clamping sampler, and 1x1 storage images. The caller owns the views
21// adds and keeps them alive while they are in the set; this class owns only the fallbacks and the table's buffer. 23// 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.
22// 25//
23// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind 26// Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind
24// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while 27// (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while
@@ -62,12 +65,19 @@ struct StorageViews {
62 VkImageView r32ui = VK_NULL_HANDLE; 65 VkImageView r32ui = VK_NULL_HANDLE;
63}; 66};
64 67
68namespace bindless {
69inline constexpr std::uint32_t binding_count = 26;
70constexpr std::array<std::uint32_t, binding_count> default_counts();
71} // namespace bindless
72
65struct BindlessFeatures { 73struct BindlessFeatures {
66 bool update_after_bind = false; 74 bool update_after_bind = false;
67 bool partially_bound = false; 75 bool partially_bound = false;
68 // Fallback sample counts for RGBA8_UNORM, R32_UINT, R32_SINT. A single sample omits that class's MS bindings. 76 // 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{ 77 std::array<VkSampleCountFlagBits, 3> ms_samples{
70 VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT}; 78 VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT};
79 // Descriptors in each binding, by binding number: bindless::bindings by default, or what size_bindings() chose.
80 std::array<std::uint32_t, bindless::binding_count> counts = bindless::default_counts();
71}; 81};
72 82
73namespace bindless { 83namespace bindless {
@@ -79,7 +89,6 @@ struct Binding {
79 89
80inline constexpr std::uint32_t table_binding = 2; 90inline constexpr std::uint32_t table_binding = 2;
81inline constexpr std::uint32_t sampler_binding = 1; 91inline constexpr std::uint32_t sampler_binding = 1;
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; 92inline constexpr std::uint32_t first_ms_binding = 20;
84inline constexpr std::uint32_t no_binding = 0xffffffffu; 93inline constexpr std::uint32_t no_binding = 0xffffffffu;
85 94
@@ -100,6 +109,14 @@ inline constexpr std::array<Binding, 26> bindings = {{
100 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, 109 {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256},
101}}; 110}};
102 111
112static_assert(bindings.size() == binding_count);
113
114constexpr std::array<std::uint32_t, binding_count> default_counts() {
115 std::array<std::uint32_t, binding_count> counts{};
116 for (std::uint32_t b = 0; b < binding_count; ++b) counts[b] = bindings[b].count;
117 return counts;
118}
119
103constexpr bool binding_enabled(std::uint32_t binding, const BindlessFeatures& features) { 120constexpr bool binding_enabled(std::uint32_t binding, const BindlessFeatures& features) {
104 return binding < first_ms_binding || features.ms_samples[(binding - first_ms_binding) / 2] != VK_SAMPLE_COUNT_1_BIT; 121 return binding < first_ms_binding || features.ms_samples[(binding - first_ms_binding) / 2] != VK_SAMPLE_COUNT_1_BIT;
105} 122}
@@ -108,15 +125,16 @@ constexpr bool binding_enabled(std::uint32_t binding, const BindlessFeatures& fe
108// per-stage or per-set limits have no room for additional pairs of MS arrays. 125// 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, 126constexpr void limit_ms_bindings(BindlessFeatures& features, std::uint32_t sampled_limit,
110 std::uint32_t resource_limit) { 127 std::uint32_t resource_limit) {
128 const auto& counts = features.counts;
111 std::uint32_t sampled = 0, total = 0; 129 std::uint32_t sampled = 0, total = 0;
112 for (std::uint32_t b = 0; b < first_ms_binding; ++b) { 130 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; 131 if (bindings[b].type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) sampled += counts[b];
114 if (bindings[b].type != VK_DESCRIPTOR_TYPE_SAMPLER) total += bindings[b].count; 132 if (bindings[b].type != VK_DESCRIPTOR_TYPE_SAMPLER) total += counts[b];
115 } 133 }
116 for (std::uint32_t c = 0; c < features.ms_samples.size(); ++c) { 134 for (std::uint32_t c = 0; c < features.ms_samples.size(); ++c) {
117 if (features.ms_samples[c] == VK_SAMPLE_COUNT_1_BIT) continue; 135 if (features.ms_samples[c] == VK_SAMPLE_COUNT_1_BIT) continue;
118 const auto b = first_ms_binding + 2 * c; 136 const auto b = first_ms_binding + 2 * c;
119 const auto pair = bindings[b].count + bindings[b + 1].count; 137 const auto pair = counts[b] + counts[b + 1];
120 if (sampled + pair > sampled_limit || total + pair > resource_limit) { 138 if (sampled + pair > sampled_limit || total + pair > resource_limit) {
121 features.ms_samples[c] = VK_SAMPLE_COUNT_1_BIT; 139 features.ms_samples[c] = VK_SAMPLE_COUNT_1_BIT;
122 } else { 140 } else {
@@ -126,23 +144,6 @@ constexpr void limit_ms_bindings(BindlessFeatures& features, std::uint32_t sampl
126 } 144 }
127} 145}
128 146
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) { 147constexpr bool multisampled(ImageDimension dimension) {
147 return dimension == ImageDimension::dim_2d_ms || dimension == ImageDimension::dim_2d_ms_array; 148 return dimension == ImageDimension::dim_2d_ms || dimension == ImageDimension::dim_2d_ms_array;
148} 149}
@@ -161,8 +162,114 @@ constexpr std::uint32_t r32ui_binding(ImageDimension dimension) {
161 return dimension == ImageDimension::dim_2d ? 4 : dimension == ImageDimension::dim_3d ? 7 : no_binding; 162 return dimension == ImageDimension::dim_2d ? 4 : dimension == ImageDimension::dim_3d ? 7 : no_binding;
162} 163}
163 164
164// How many images of a dimension the set holds, the fallback included. 165// What size_bindings() grows to where the device's limits allow. At 32-64 bytes a descriptor the whole set is then
165constexpr std::uint32_t image_capacity(ImageDimension dimension) { return bindings[sampled_binding(dimension)].count; } 166// 3-6 MiB (93793 descriptors on an RTX 5070).
167inline constexpr std::uint32_t max_2d_images = 65536;
168inline constexpr std::uint32_t max_storage_indices = 4096; // in each storage array, at most its dimension's images
169// A full D3D12 shader-visible sampler heap (2048) and room for literal and static S#s, within what the device can
170// create (maxSamplerAllocationCount, at least 4000) less the samplers that are not in the set.
171inline constexpr std::uint32_t max_samplers = 4096;
172inline constexpr std::uint32_t reserved_samplers = 64;
173// Growth leaves this many descriptors of each limit to the other sets of a pipeline layout.
174inline constexpr std::uint32_t growth_headroom = 64;
175
176// Sizes the 2D image, sampler and storage arrays from the device's limits: the update-after-bind ones with
177// features.update_after_bind, the plain ones otherwise. Every binding is visible to every stage, so the per-stage
178// limits apply to the whole set; fragment outputs count as stage resources (D3D12 allows eight render targets) and
179// standalone samplers do not, though they count in an update-after-bind pool.
180// 1. The default counts, or as much of them as the device holds: samplers, then storage, then 2D images.
181// 2. The MS pairs that fit beside them (limit_ms_bindings, which may turn classes off).
182// 3. Growth towards the maximums above, in the same order, within maxPerSetDescriptors. That limit only says
183// what is sure to work (vkGetDescriptorSetLayoutSupport may allow more), so it never shrinks step 1.
184// A storage array holds the smaller of the storage count and its dimension's images: add_image never gives a
185// storage view an index past the sampled array. The other bindings keep their default counts.
186// Sets features.counts; returns false when a count is below its default because the device's limits are.
187inline bool size_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimits& limits,
188 const VkPhysicalDeviceVulkan11Properties& v11,
189 const VkPhysicalDeviceVulkan12Properties& v12) {
190 const bool uab = features.update_after_bind;
191 const std::uint32_t sampled_limit =
192 uab ? std::min(v12.maxPerStageDescriptorUpdateAfterBindSampledImages,
193 v12.maxDescriptorSetUpdateAfterBindSampledImages)
194 : std::min(limits.maxPerStageDescriptorSampledImages, limits.maxDescriptorSetSampledImages);
195 const std::uint32_t sampler_limit =
196 uab ? std::min(v12.maxPerStageDescriptorUpdateAfterBindSamplers, v12.maxDescriptorSetUpdateAfterBindSamplers)
197 : std::min(limits.maxPerStageDescriptorSamplers, limits.maxDescriptorSetSamplers);
198 const std::uint32_t storage_limit =
199 uab ? std::min(v12.maxPerStageDescriptorUpdateAfterBindStorageImages,
200 v12.maxDescriptorSetUpdateAfterBindStorageImages)
201 : std::min(limits.maxPerStageDescriptorStorageImages, limits.maxDescriptorSetStorageImages);
202 std::uint32_t resource_limit = uab ? v12.maxPerStageUpdateAfterBindResources : limits.maxPerStageResources;
203 resource_limit -= std::min(resource_limit, std::min(limits.maxColorAttachments, 8u));
204 const std::uint32_t pool_limit = uab ? v12.maxUpdateAfterBindDescriptorsInAllPools : 0xffffffffu;
205
206 auto& counts = features.counts;
207 counts = default_counts();
208 std::uint32_t images = 1, samplers = 1, storage = 1;
209 // Until the end the 2D storage arrays are not capped by the 2D images, which grow after them: capping can only
210 // take descriptors away.
211 const auto apply = [&](bool final) {
212 counts[sampled_binding(ImageDimension::dim_2d)] = images;
213 counts[sampler_binding] = samplers;
214 for (std::uint32_t d = 0; d < 4; ++d) {
215 const auto dimension = ImageDimension(d);
216 const bool capped = final || dimension != ImageDimension::dim_2d;
217 const std::uint32_t held = capped ? std::min(storage, counts[sampled_binding(dimension)]) : storage;
218 for (std::uint32_t c = 0; c < 3; ++c) counts[storage_binding(dimension, c)] = held;
219 if (r32ui_binding(dimension) != no_binding) counts[r32ui_binding(dimension)] = held;
220 }
221 };
222 const auto fits = [&](std::uint32_t headroom, std::uint64_t set_limit) {
223 apply(false);
224 std::uint64_t sampled = 0, sampler = 0, stored = 0, buffers = 0;
225 for (std::uint32_t b = 0; b < binding_count; ++b) {
226 if (!binding_enabled(b, features)) continue;
227 const auto type = bindings[b].type;
228 (type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE ? sampled
229 : type == VK_DESCRIPTOR_TYPE_SAMPLER ? sampler
230 : type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE ? stored
231 : buffers) += counts[b];
232 }
233 const auto within = [&](std::uint64_t n, std::uint32_t limit) { return n + headroom <= limit; };
234 const std::uint64_t all = sampled + sampler + stored + buffers;
235 return within(sampled, sampled_limit) && within(sampler, sampler_limit) && within(stored, storage_limit) &&
236 within(sampled + stored + buffers, resource_limit) && all <= pool_limit && all <= set_limit;
237 };
238 // Raises n to the largest value up to `high` that fits; leaves it where it is when none does.
239 const auto grow = [&](std::uint32_t& n, std::uint32_t high, std::uint32_t headroom, std::uint64_t set_limit) {
240 std::uint32_t low = n;
241 while (low < high) {
242 n = low + (high - low + 1) / 2;
243 if (fits(headroom, set_limit)) low = n;
244 else high = n - 1;
245 }
246 n = low;
247 };
248
249 constexpr std::uint64_t no_limit = ~0ull;
250 const auto ms_samples = features.ms_samples;
251 features.ms_samples.fill(VK_SAMPLE_COUNT_1_BIT);
252 grow(samplers, bindings[sampler_binding].count, 0, no_limit);
253 grow(storage, bindings[storage_binding(ImageDimension::dim_2d, 0)].count, 0, no_limit);
254 grow(images, bindings[sampled_binding(ImageDimension::dim_2d)].count, 0, no_limit);
255 apply(false);
256 const bool full = images == bindings[0].count && samplers == bindings[sampler_binding].count &&
257 storage == bindings[storage_binding(ImageDimension::dim_2d, 0)].count;
258 features.ms_samples = ms_samples;
259 limit_ms_bindings(features, sampled_limit, std::min(resource_limit, pool_limit - std::min(pool_limit, samplers)));
260
261 std::uint64_t set_limit = 0;
262 for (std::uint32_t b = 0; b < binding_count; ++b)
263 if (binding_enabled(b, features)) set_limit += counts[b];
264 set_limit = std::max(set_limit, std::uint64_t(v11.maxPerSetDescriptors));
265 const std::uint32_t creatable =
266 limits.maxSamplerAllocationCount - std::min(limits.maxSamplerAllocationCount, reserved_samplers);
267 grow(samplers, std::max(samplers, std::min(max_samplers, creatable)), growth_headroom, set_limit);
268 grow(images, std::max(images, max_2d_images), growth_headroom, set_limit);
269 grow(storage, std::max(storage, max_storage_indices), growth_headroom, set_limit);
270 apply(true);
271 return full;
272}
166 273
167} // namespace bindless 274} // namespace bindless
168 275
@@ -194,6 +301,15 @@ public:
194 bool supports_ms_class(std::uint32_t component_class) const { 301 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; 302 return component_class < classes && features_.ms_samples[component_class] != VK_SAMPLE_COUNT_1_BIT;
196 } 303 }
304 // How many images of a dimension the set holds, the fallback included, and how many of the lowest of those
305 // indices its storage arrays hold (0 for the MS dimensions, which have none). The same for samplers.
306 std::uint32_t image_capacity(ImageDimension dimension) const {
307 return features_.counts[bindless::sampled_binding(dimension)];
308 }
309 std::uint32_t storage_capacity(ImageDimension dimension) const {
310 return bindless::multisampled(dimension) ? 0 : features_.counts[bindless::storage_binding(dimension, 0)];
311 }
312 std::uint32_t sampler_capacity() const { return features_.counts[bindless::sampler_binding]; }
197 // Set 0 of every pipeline layout made for translated shaders. 313 // Set 0 of every pipeline layout made for translated shaders.
198 VkDescriptorSetLayout layout() const { return layout_; } 314 VkDescriptorSetLayout layout() const { return layout_; }
199 // Bound at set 0. The same set for the life of this object: flush() updates it in place. 315 // Bound at set 0. The same set for the life of this object: flush() updates it in place.
@@ -239,7 +355,7 @@ public:
239 const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui; 355 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; 356 if (bindless::multisampled(dimension) && (stores || !supports_ms_class(sampled_class))) return 0;
241 auto& used = image_used_[std::uint32_t(dimension)]; 357 auto& used = image_used_[std::uint32_t(dimension)];
242 const std::uint32_t low_end = std::min(std::uint32_t(used.size()), bindless::storage_indices); 358 const std::uint32_t low_end = std::min(std::uint32_t(used.size()), storage_capacity(dimension));
243 std::uint32_t index = stores ? 0 : allocate(used, low_end, std::uint32_t(used.size())); 359 std::uint32_t index = stores ? 0 : allocate(used, low_end, std::uint32_t(used.size()));
244 if (!index) index = allocate(used, 1, low_end); 360 if (!index) index = allocate(used, 1, low_end);
245 if (index) write_image(dimension, index, sampled, sampled_layout, storage, sampled_class); 361 if (index) write_image(dimension, index, sampled, sampled_layout, storage, sampled_class);
@@ -422,10 +538,10 @@ private:
422 std::uint32_t count = 0; 538 std::uint32_t count = 0;
423 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) { 539 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
424 if (!bindless::binding_enabled(b, features_)) continue; 540 if (!bindless::binding_enabled(b, features_)) continue;
425 entries[count] = {b, bindless::bindings[b].type, bindless::bindings[b].count, VK_SHADER_STAGE_ALL, nullptr}; 541 entries[count] = {b, bindless::bindings[b].type, features_.counts[b], VK_SHADER_STAGE_ALL, nullptr};
426 flags[count] = (features_.update_after_bind ? VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT : 0) | 542 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); 543 (features_.partially_bound ? VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT : 0);
428 sizes[count] = {bindless::bindings[b].type, bindless::bindings[b].count}; 544 sizes[count] = {bindless::bindings[b].type, features_.counts[b]};
429 ++count; 545 ++count;
430 } 546 }
431 VkDescriptorSetLayoutBindingFlagsCreateInfo flags_info{ 547 VkDescriptorSetLayoutBindingFlagsCreateInfo flags_info{
@@ -451,10 +567,10 @@ private:
451 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set_) != VK_SUCCESS) return false; 567 if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set_) != VK_SUCCESS) return false;
452 568
453 for (std::uint32_t d = 0; d < dimensions; ++d) { 569 for (std::uint32_t d = 0; d < dimensions; ++d) {
454 image_used_[d].assign(bindless::image_capacity(ImageDimension(d)), 0); 570 image_used_[d].assign(image_capacity(ImageDimension(d)), 0);
455 image_used_[d][0] = 1; 571 image_used_[d][0] = 1;
456 } 572 }
457 sampler_used_.assign(bindless::bindings[bindless::sampler_binding].count, 0); 573 sampler_used_.assign(sampler_capacity(), 0);
458 sampler_used_[0] = 1; 574 sampler_used_[0] = 1;
459 return true; 575 return true;
460 } 576 }
@@ -716,7 +832,7 @@ private:
716 infos.reserve(bindless::bindings.size()); 832 infos.reserve(bindless::bindings.size());
717 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) { 833 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) {
718 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue; 834 if (b == bindless::table_binding || !bindless::binding_enabled(b, features_)) continue;
719 const std::uint32_t count = features_.partially_bound ? 1 : bindless::bindings[b].count; 835 const std::uint32_t count = features_.partially_bound ? 1 : features_.counts[b];
720 infos.emplace_back(count, fallback_info(b)); 836 infos.emplace_back(count, fallback_info(b));
721 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; 837 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
722 write.dstSet = set_; 838 write.dstSet = set_;
@@ -762,7 +878,7 @@ private:
762 queue(sampled_at, index, 878 queue(sampled_at, index,
763 sampled ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout} : fallback_info(sampled_at)); 879 sampled ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout} : fallback_info(sampled_at));
764 const auto store = [&](std::uint32_t binding, VkImageView view) { 880 const auto store = [&](std::uint32_t binding, VkImageView view) {
765 if (binding == bindless::no_binding || index >= bindless::bindings[binding].count) return; 881 if (binding == bindless::no_binding || index >= features_.counts[binding]) return;
766 queue(binding, index, 882 queue(binding, index,
767 view ? VkDescriptorImageInfo{VK_NULL_HANDLE, view, VK_IMAGE_LAYOUT_GENERAL} : fallback_info(binding)); 883 view ? VkDescriptorImageInfo{VK_NULL_HANDLE, view, VK_IMAGE_LAYOUT_GENERAL} : fallback_info(binding));
768 }; 884 };
diff --git a/runtime/test/bindless_sizing_test.cpp b/runtime/test/bindless_sizing_test.cpp
new file mode 100644
index 000000000..469274d56
--- /dev/null
+++ b/runtime/test/bindless_sizing_test.cpp
@@ -0,0 +1,179 @@
1// bindless::size_bindings with synthetic device limits: no GPU. The counts of set 0 come from the device's descriptor
2// limits (vulkan_bindless.h): today's sizes where a device is at them, larger 2D, sampler and storage arrays where it
3// has room, and less where it is below them.
4#ifndef VK_NO_PROTOTYPES
5#define VK_NO_PROTOTYPES
6#endif
7#include "../src/vulkan_bindless.h"
8
9#include <cstdio>
10
11#define EXPECT(condition) do { \
12 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
13} while (0)
14
15using namespace leonida;
16
17namespace {
18
19struct Device {
20 VkPhysicalDeviceLimits limits{};
21 VkPhysicalDeviceVulkan11Properties v11{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES};
22 VkPhysicalDeviceVulkan12Properties v12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES};
23};
24
25// The same descriptor limit for every type, update-after-bind or not; the others are unlimited.
26Device uniform(std::uint32_t plain, std::uint32_t update_after_bind) {
27 Device d;
28 auto& l = d.limits;
29 l.maxPerStageDescriptorSampledImages = l.maxDescriptorSetSampledImages = plain;
30 l.maxPerStageDescriptorSamplers = l.maxDescriptorSetSamplers = plain;
31 l.maxPerStageDescriptorStorageImages = l.maxDescriptorSetStorageImages = plain;
32 l.maxPerStageResources = 0xffffffffu;
33 l.maxColorAttachments = 8;
34 l.maxSamplerAllocationCount = 4000; // NVIDIA's; the minimum Vulkan allows
35 auto& u = d.v12;
36 u.maxPerStageDescriptorUpdateAfterBindSampledImages = update_after_bind;
37 u.maxDescriptorSetUpdateAfterBindSampledImages = update_after_bind;
38 u.maxPerStageDescriptorUpdateAfterBindSamplers = u.maxDescriptorSetUpdateAfterBindSamplers = update_after_bind;
39 u.maxPerStageDescriptorUpdateAfterBindStorageImages = update_after_bind;
40 u.maxDescriptorSetUpdateAfterBindStorageImages = update_after_bind;
41 u.maxPerStageUpdateAfterBindResources = 0xffffffffu;
42 u.maxUpdateAfterBindDescriptorsInAllPools = 0xffffffffu;
43 d.v11.maxPerSetDescriptors = 0xffffffffu;
44 return d;
45}
46
47BindlessFeatures with_ms(bool update_after_bind) {
48 BindlessFeatures f{};
49 f.update_after_bind = update_after_bind;
50 f.ms_samples.fill(VK_SAMPLE_COUNT_4_BIT);
51 return f;
52}
53
54struct Totals { std::uint32_t sampled = 0, samplers = 0, storage = 0, all = 0; };
55Totals totals(const BindlessFeatures& f) {
56 Totals t;
57 for (std::uint32_t b = 0; b < bindless::binding_count; ++b) {
58 if (!bindless::binding_enabled(b, f)) continue;
59 const auto type = bindless::bindings[b].type;
60 if (type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) t.sampled += f.counts[b];
61 if (type == VK_DESCRIPTOR_TYPE_SAMPLER) t.samplers += f.counts[b];
62 if (type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) t.storage += f.counts[b];
63 t.all += f.counts[b];
64 }
65 return t;
66}
67
68std::uint32_t count(const BindlessFeatures& f, ImageDimension dimension) {
69 return f.counts[bindless::sampled_binding(dimension)];
70}
71std::uint32_t storage(const BindlessFeatures& f, ImageDimension dimension) {
72 return f.counts[bindless::storage_binding(dimension, 0)];
73}
74
75// Every storage array of a dimension, the r32ui one included, holds the same indices.
76bool storage_consistent(const BindlessFeatures& f) {
77 for (std::uint32_t d = 0; d < 4; ++d) {
78 const auto dimension = ImageDimension(d);
79 const auto held = storage(f, dimension);
80 if (held > count(f, dimension)) return false;
81 for (std::uint32_t c = 0; c < 3; ++c)
82 if (f.counts[bindless::storage_binding(dimension, c)] != held) return false;
83 if (bindless::r32ui_binding(dimension) != bindless::no_binding &&
84 f.counts[bindless::r32ui_binding(dimension)] != held)
85 return false;
86 }
87 return true;
88}
89
90} // namespace
91
92int main() {
93 using D = ImageDimension;
94 constexpr auto defaults = bindless::default_counts();
95 static_assert(defaults[0] == 4096 && defaults[bindless::sampler_binding] == 1024 && defaults[8] == 256);
96
97 // An RTX 5070 (driver 621.24): 1048576 of each descriptor type, update-after-bind or not, 4000 samplers.
98 for (const bool update_after_bind : {true, false}) {
99 const Device d = uniform(1048576, 1048576);
100 BindlessFeatures f = with_ms(update_after_bind);
101 EXPECT(bindless::size_bindings(f, d.limits, d.v11, d.v12));
102 EXPECT(count(f, D::dim_2d) == bindless::max_2d_images);
103 EXPECT(f.counts[bindless::sampler_binding] == 4000 - bindless::reserved_samplers);
104 EXPECT(f.counts[bindless::sampler_binding] > 2048); // a full sampler heap and the fallback
105 EXPECT(storage(f, D::dim_2d) == bindless::max_storage_indices);
106 EXPECT(storage(f, D::dim_2d_array) == 1024 && storage(f, D::dim_3d) == 256 && storage(f, D::dim_1d) == 256);
107 EXPECT(count(f, D::dim_2d_array) == 1024 && count(f, D::dim_3d) == 256 && count(f, D::dim_1d) == 256);
108 EXPECT(f.ms_samples[0] == VK_SAMPLE_COUNT_4_BIT && f.ms_samples[2] == VK_SAMPLE_COUNT_4_BIT);
109 EXPECT(storage_consistent(f));
110 if (update_after_bind)
111 std::printf("RTX 5070-like: %u 2D images, %u samplers, %u storage indices in 2D, %u descriptors\n",
112 count(f, D::dim_2d), f.counts[bindless::sampler_binding], storage(f, D::dim_2d), totals(f).all);
113 }
114
115 // At today's sizes exactly (5632 sampled images, 1024 samplers, 3584 storage images): today's set, no MS pairs.
116 {
117 Device d = uniform(1048576, 1048576);
118 d.v12.maxPerStageDescriptorUpdateAfterBindSampledImages = 5632;
119 d.v12.maxPerStageDescriptorUpdateAfterBindSamplers = 1024;
120 d.v12.maxPerStageDescriptorUpdateAfterBindStorageImages = 3584;
121 BindlessFeatures f = with_ms(true);
122 EXPECT(bindless::size_bindings(f, d.limits, d.v11, d.v12));
123 EXPECT(count(f, D::dim_2d) == 4096 && f.counts[bindless::sampler_binding] == 1024 &&
124 storage(f, D::dim_2d) == 256);
125 for (std::uint32_t b = 0; b < bindless::first_ms_binding; ++b)
126 if (bindless::bindings[b].type != VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) EXPECT(f.counts[b] == defaults[b]);
127 EXPECT(f.ms_samples == (std::array{VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT, VK_SAMPLE_COUNT_1_BIT}));
128 EXPECT(storage_consistent(f));
129 }
130
131 // A small device below today's sizes: smaller arrays that fit, and false.
132 {
133 Device d = uniform(1048576, 1048576);
134 d.limits.maxPerStageDescriptorSampledImages = 3000;
135 d.limits.maxPerStageDescriptorSamplers = 512;
136 d.limits.maxPerStageDescriptorStorageImages = 2000;
137 d.limits.maxPerStageResources = 4500;
138 BindlessFeatures f = with_ms(false);
139 EXPECT(!bindless::size_bindings(f, d.limits, d.v11, d.v12));
140 const Totals t = totals(f);
141 EXPECT(t.sampled <= 3000 && t.samplers <= 512 && t.storage <= 2000 && t.sampled + t.storage + 1 + 8 <= 4500);
142 EXPECT(count(f, D::dim_2d) >= 1 && count(f, D::dim_2d) < 4096 && f.counts[bindless::sampler_binding] == 512);
143 EXPECT(f.ms_samples[0] == VK_SAMPLE_COUNT_1_BIT);
144 EXPECT(storage_consistent(f));
145 }
146
147 // Update-after-bind and plain limits differ: each layout is sized by its own.
148 {
149 const Device d = uniform(16384, 1048576);
150 BindlessFeatures uab = with_ms(true), plain = with_ms(false);
151 EXPECT(bindless::size_bindings(uab, d.limits, d.v11, d.v12));
152 EXPECT(bindless::size_bindings(plain, d.limits, d.v11, d.v12));
153 EXPECT(count(uab, D::dim_2d) == bindless::max_2d_images);
154 const Totals t = totals(plain);
155 EXPECT(count(plain, D::dim_2d) > 4096 && count(plain, D::dim_2d) < 16384);
156 EXPECT(t.sampled + bindless::growth_headroom <= 16384 && t.storage + bindless::growth_headroom <= 16384);
157 EXPECT(plain.ms_samples[0] == VK_SAMPLE_COUNT_4_BIT && storage_consistent(plain));
158 EXPECT(plain.counts[bindless::sampler_binding] == 4000 - bindless::reserved_samplers);
159 }
160
161 // maxPerSetDescriptors stops growth but never takes a count below today's.
162 {
163 Device d = uniform(1048576, 1048576);
164 d.v11.maxPerSetDescriptors = 1024;
165 BindlessFeatures f = with_ms(true);
166 EXPECT(bindless::size_bindings(f, d.limits, d.v11, d.v12));
167 EXPECT(count(f, D::dim_2d) == 4096 && f.counts[bindless::sampler_binding] == 1024 &&
168 storage(f, D::dim_2d) == 256);
169 d.v11.maxPerSetDescriptors = 30000;
170 f = with_ms(true);
171 EXPECT(bindless::size_bindings(f, d.limits, d.v11, d.v12));
172 EXPECT(totals(f).all <= 30000 && count(f, D::dim_2d) > 4096);
173 }
174
175 // The default features are today's counts, which the device-less users (vk_image_fuzz) still get.
176 EXPECT(BindlessFeatures{}.counts == defaults);
177 std::printf("bindless sizing: synthetic limits gave the expected counts\n");
178 return 0;
179}
diff --git a/runtime/test/d3d12_descriptor_test.cpp b/runtime/test/d3d12_descriptor_test.cpp
index 0b07ceb99..9c58e9ff5 100644
--- a/runtime/test/d3d12_descriptor_test.cpp
+++ b/runtime/test/d3d12_descriptor_test.cpp
@@ -1474,6 +1474,88 @@ int main() {
1474 for (auto& resource : test.textures) resource.reset(); 1474 for (auto& resource : test.textures) resource.reset();
1475 std::printf("MSAA format %u: 2D and array integer reads and zero fallbacks matched\n", UINT(test.format)); 1475 std::printf("MSAA format %u: 2D and array integer reads and zero fallbacks matched\n", UINT(test.format));
1476 } 1476 }
1477
1478 // More than 4096 SRVs and more than a full shader-visible sampler heap (2048): 5000 distinct views of the
1479 // texture (mip ranges by swizzles), then one more that samples as the texture, and 2100 distinct wrapping
1480 // samplers (MaxLOD apart). The last view through the last sampler must give the texture, one tile to the
1481 // right; a fallback view or the fallback (clamping) sampler would not.
1482 {
1483 constexpr UINT many_views = 5000, many_samplers = 2100, heap_samplers = 2048;
1484 Ref<ID3D12DescriptorHeap> many_view_heap, many_sampler_heaps[2];
1485 D3D12_DESCRIPTOR_HEAP_DESC many_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, many_views + 1,
1486 D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0};
1487 EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&many_desc, IID_PPV_ARGS(&many_view_heap.p))));
1488 many_desc = {D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, heap_samplers, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
1489 0};
1490 for (auto& heap : many_sampler_heaps)
1491 EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&many_desc, IID_PPV_ARGS(&heap.p))));
1492 const auto cpu_start = [&](ID3D12DescriptorHeap* heap) {
1493 return reinterpret_cast<XboxHeapHandleFn>(slot(heap, 9))(heap);
1494 };
1495 const std::uint64_t many_view_cpu = cpu_start(many_view_heap.p);
1496 std::vector<std::array<std::uint32_t, 8>> tsharps;
1497 constexpr UINT mip_ranges[][2] = {{0, 1}, {0, 2}, {1, 1}, {1, 2}};
1498 for (UINT i = 0; i < many_views; ++i) {
1499 const UINT swizzle = i / UINT(std::size(mip_ranges));
1500 const UINT c0 = swizzle % 6, c1 = swizzle / 6 % 6, c2 = swizzle / 36 % 6, c3 = swizzle / 216 % 6;
1501 D3D12_SHADER_RESOURCE_VIEW_DESC srv{};
1502 srv.Format = sampled_desc.Format;
1503 srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
1504 srv.Shader4ComponentMapping = D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(c0, c1, c2, c3);
1505 const auto& mips = mip_ranges[i % std::size(mip_ranges)];
1506 srv.Texture2D = {mips[0], mips[1], 0, 0};
1507 const D3D12_CPU_DESCRIPTOR_HANDLE at{SIZE_T(many_view_cpu + i * step)};
1508 device->CreateShaderResourceView(texture.p, &srv, at);
1509 std::memcpy(tsharps.emplace_back().data(), reinterpret_cast<const void*>(many_view_cpu + i * step), 32);
1510 }
1511 // Every mip with alpha forced to one: the texture's texels (alpha 255), under a T# no other view has.
1512 D3D12_SHADER_RESOURCE_VIEW_DESC last_srv{};
1513 last_srv.Format = sampled_desc.Format;
1514 last_srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
1515 last_srv.Shader4ComponentMapping =
1516 D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(0, 1, 2, D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1);
1517 last_srv.Texture2D = {0, UINT(-1), 0, 0};
1518 const std::uint64_t last_view = many_view_cpu + many_views * step;
1519 device->CreateShaderResourceView(texture.p, &last_srv, D3D12_CPU_DESCRIPTOR_HANDLE{SIZE_T(last_view)});
1520 std::memcpy(tsharps.emplace_back().data(), reinterpret_cast<const void*>(last_view), 32);
1521 std::memcpy(tsharps.emplace_back().data(), view_bytes(own), 32);
1522 std::sort(tsharps.begin(), tsharps.end());
1523 EXPECT(std::unique(tsharps.begin(), tsharps.end()) == tsharps.end()); // distinct set entries
1524
1525 std::vector<std::array<std::uint32_t, 4>> ssharps;
1526 std::uint64_t last_sampler = 0;
1527 for (UINT i = 0; i < many_samplers; ++i) {
1528 D3D12_SAMPLER_DESC many_sampler = sampler_desc;
1529 many_sampler.MaxLOD = float(i + 1) / 256.0f; // the S# keeps 8 fraction bits; LOD 0 is sampled
1530 last_sampler = cpu_start(many_sampler_heaps[i / heap_samplers].p) + (i % heap_samplers) * sampler_step;
1531 device->CreateSampler(&many_sampler, D3D12_CPU_DESCRIPTOR_HANDLE{SIZE_T(last_sampler)});
1532 std::memcpy(ssharps.emplace_back().data(), reinterpret_cast<const void*>(last_sampler), 16);
1533 }
1534 std::memcpy(ssharps.emplace_back().data(), sampler_bytes, 16);
1535 std::sort(ssharps.begin(), ssharps.end());
1536 EXPECT(std::unique(ssharps.begin(), ssharps.end()) == ssharps.end());
1537
1538 // The next ExecuteCommandLists writes them into the set.
1539 EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
1540 EXPECT(list->Close() == S_OK);
1541 queue->ExecuteCommandLists(1, lists);
1542 EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
1543 d.fn<PFN_vkDeviceWaitIdle>("vkDeviceWaitIdle")(d.device);
1544 SampleKeys last_keys{};
1545 std::memcpy(last_keys.tsharp, reinterpret_cast<const void*>(last_view), sizeof(last_keys.tsharp));
1546 std::memcpy(last_keys.ssharp, reinterpret_cast<const void*>(last_sampler), sizeof(last_keys.ssharp));
1547 std::array<std::uint8_t, bytes> last_texels{};
1548 EXPECT(sample_through_set(d, set0, set0_layout, {&last_keys, 1}, last_texels.data()));
1549 UINT differ = 0;
1550 for (UINT i = 0; i < texels; ++i)
1551 if (std::memcmp(last_texels.data() + 4 * i, texel(i % size, i / size).data(), 4) != 0 && differ++ < 4)
1552 std::fprintf(stderr, "view %u through sampler %u, texel %u: got %u,%u,%u,%u\n", many_views + 1,
1553 many_samplers, i, last_texels[4 * i], last_texels[4 * i + 1], last_texels[4 * i + 2],
1554 last_texels[4 * i + 3]);
1555 EXPECT(differ == 0);
1556 std::printf("bindless capacity: the %uth SRV through the %uth sampler gave the texture\n", many_views + 1,
1557 many_samplers);
1558 }
1477#endif 1559#endif
1478 1560
1479 // A destroyed texture takes its views out of the set: the next ExecuteCommandLists writes that, and nothing 1561 // A destroyed texture takes its views out of the set: the next ExecuteCommandLists writes that, and nothing
diff --git a/runtime/test/vk_bindless_test.cpp b/runtime/test/vk_bindless_test.cpp
index 61de1a91e..f1cc27637 100644
--- a/runtime/test/vk_bindless_test.cpp
+++ b/runtime/test/vk_bindless_test.cpp
@@ -166,7 +166,8 @@ int main(int argc, char** argv) {
166 return unsupported; 166 return unsupported;
167 } 167 }
168 VkPhysicalDeviceVulkan12Properties props12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES}; 168 VkPhysicalDeviceVulkan12Properties props12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES};
169 VkPhysicalDeviceProperties2 props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &props12}; 169 VkPhysicalDeviceVulkan11Properties props11{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES, &props12};
170 VkPhysicalDeviceProperties2 props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &props11};
170 vkGetPhysicalDeviceProperties2(pd, &props); 171 vkGetPhysicalDeviceProperties2(pd, &props);
171 std::printf("device: %s%s\n", props.properties.deviceName, validation ? " (validation on)" : ""); 172 std::printf("device: %s%s\n", props.properties.deviceName, validation ? " (validation on)" : "");
172 // The pipeline layout pushes the 144-byte translated user-data block. A device with fewer push constant bytes 173 // The pipeline layout pushes the 144-byte translated user-data block. A device with fewer push constant bytes
@@ -462,7 +463,7 @@ int main(int argc, char** argv) {
462 std::uint32_t count = 0; 463 std::uint32_t count = 0;
463 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) 464 for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b)
464 if (bindless::bindings[b].type == type && bindless::binding_enabled(b, f)) 465 if (bindless::bindings[b].type == type && bindless::binding_enabled(b, f))
465 count += bindless::bindings[b].count; 466 count += f.counts[b];
466 return count; 467 return count;
467 }; 468 };
468 const auto fits = [&](const BindlessFeatures& f) { 469 const auto fits = [&](const BindlessFeatures& f) {
@@ -513,10 +514,11 @@ int main(int argc, char** argv) {
513 for (int configuration = 0; configuration < configuration_count; ++configuration) { 514 for (int configuration = 0; configuration < configuration_count; ++configuration) {
514 BindlessFeatures flags = configurations[configuration]; 515 BindlessFeatures flags = configurations[configuration];
515 flags.ms_samples[0] = VK_SAMPLE_COUNT_4_BIT; 516 flags.ms_samples[0] = VK_SAMPLE_COUNT_4_BIT;
516 bindless::limit_ms_bindings(flags, props.properties.limits, props12); 517 // A device below the default counts gets a smaller set; this test needs at least those.
518 const bool full = bindless::size_bindings(flags, props.properties.limits, props11, props12);
517 const char* name = flags.update_after_bind && flags.partially_bound ? "update after bind, partially bound" 519 const char* name = flags.update_after_bind && flags.partially_bound ? "update after bind, partially bound"
518 : flags.update_after_bind ? "update after bind" : flags.partially_bound ? "partially bound" : "no flags"; 520 : flags.update_after_bind ? "update after bind" : flags.partially_bound ? "partially bound" : "no flags";
519 if (!fits(flags)) { 521 if (!full || !fits(flags)) {
520 std::printf("%s: skipped, the set does not fit this device's descriptor limits\n", name); 522 std::printf("%s: skipped, the set does not fit this device's descriptor limits\n", name);
521 continue; 523 continue;
522 } 524 }
@@ -622,13 +624,13 @@ int main(int argc, char** argv) {
622 const std::uint32_t stored_index = set.add_image(ImageDimension::dim_2d, stored_view, VK_IMAGE_LAYOUT_GENERAL, as_storage); 624 const std::uint32_t stored_index = set.add_image(ImageDimension::dim_2d, stored_view, VK_IMAGE_LAYOUT_GENERAL, as_storage);
623 const std::uint32_t decoy_index = set.add_image(ImageDimension::dim_2d, decoy_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); 625 const std::uint32_t decoy_index = set.add_image(ImageDimension::dim_2d, decoy_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
624 const std::uint32_t gradient_index = set.add_image(ImageDimension::dim_2d, gradient_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); 626 const std::uint32_t gradient_index = set.add_image(ImageDimension::dim_2d, gradient_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
625 EXPECT(stored_index >= 1 && stored_index < bindless::storage_indices); 627 EXPECT(stored_index >= 1 && stored_index < set.storage_capacity(ImageDimension::dim_2d));
626 EXPECT(decoy_index >= bindless::storage_indices && gradient_index > decoy_index); 628 EXPECT(decoy_index >= set.storage_capacity(ImageDimension::dim_2d) && gradient_index > decoy_index);
627 EXPECT(gradient_index < bindless::image_capacity(ImageDimension::dim_2d)); 629 EXPECT(gradient_index < set.image_capacity(ImageDimension::dim_2d));
628 std::vector<std::uint32_t> filled; 630 std::vector<std::uint32_t> filled;
629 for (std::uint32_t i = 1; i < bindless::image_capacity(ImageDimension::dim_1d); ++i) 631 for (std::uint32_t i = 1; i < set.image_capacity(ImageDimension::dim_1d); ++i)
630 filled.push_back(set.add_image(ImageDimension::dim_1d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL)); 632 filled.push_back(set.add_image(ImageDimension::dim_1d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL));
631 EXPECT(filled.front() == 1 && filled.back() == bindless::image_capacity(ImageDimension::dim_1d) - 1); 633 EXPECT(filled.front() == 1 && filled.back() == set.image_capacity(ImageDimension::dim_1d) - 1);
632 EXPECT(set.add_image(ImageDimension::dim_1d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL) == 0); 634 EXPECT(set.add_image(ImageDimension::dim_1d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL) == 0);
633 set.remove_image(ImageDimension::dim_1d, 7); 635 set.remove_image(ImageDimension::dim_1d, 7);
634 EXPECT(set.add_image(ImageDimension::dim_1d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL) == 7); 636 EXPECT(set.add_image(ImageDimension::dim_1d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL) == 7);
@@ -651,7 +653,8 @@ int main(int argc, char** argv) {
651 StorageViews brief_storage; 653 StorageViews brief_storage;
652 brief_storage.as_float = brief; 654 brief_storage.as_float = brief;
653 const std::uint32_t brief_index = set.add_image(ImageDimension::dim_2d, brief, VK_IMAGE_LAYOUT_GENERAL, brief_storage); 655 const std::uint32_t brief_index = set.add_image(ImageDimension::dim_2d, brief, VK_IMAGE_LAYOUT_GENERAL, brief_storage);
654 EXPECT(brief_index >= 1 && brief_index < bindless::storage_indices && brief_index != stored_index); 656 EXPECT(brief_index >= 1 && brief_index < set.storage_capacity(ImageDimension::dim_2d) &&
657 brief_index != stored_index);
655 set.remove_image(ImageDimension::dim_2d, brief_index); 658 set.remove_image(ImageDimension::dim_2d, brief_index);
656 vkDestroyImageView(dev, brief, nullptr); 659 vkDestroyImageView(dev, brief, nullptr);
657 } 660 }
@@ -755,10 +758,30 @@ int main(int argc, char** argv) {
755 compare("8 removed", zeros); 758 compare("8 removed", zeros);
756 EXPECT(set.add_image(ImageDimension::dim_2d, gradient_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) == gradient_index); 759 EXPECT(set.add_image(ImageDimension::dim_2d, gradient_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) == gradient_index);
757 760
761 // 9. More than 4096 2D images and more than a full D3D12 sampler heap (2048): the gradient and the S#
762 // through the last indices of both arrays, where the device's limits gave the set room for them.
763 std::uint32_t far_image = 0, far_sampler = 0;
764 if (set.image_capacity(ImageDimension::dim_2d) > 5000 + 8 && set.sampler_capacity() > 2100 + 8) {
765 for (std::uint32_t i = 0; i < 5000; ++i)
766 EXPECT(set.add_image(ImageDimension::dim_2d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL) != 0);
767 far_image = set.add_image(ImageDimension::dim_2d, gradient_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
768 for (std::uint32_t i = 0; i < 2100; ++i) far_sampler = set.add_sampler(nearest);
769 EXPECT(far_image > 5000 && far_sampler > 2100);
770 set.insert_key(tsharp, far_image);
771 set.insert_key(make_ssharp_key(ssharp.data()), far_sampler);
772 EXPECT(flush());
773 CHECK(draw());
774 compare("9 past 4096 images and 2048 samplers", nullptr);
775 } else {
776 std::printf("%s: no room past 5000 2D images and 2100 samplers on this device; step 9 skipped\n", name);
777 }
778
758 std::printf("%s: descriptor table in %s memory\n", name, 779 std::printf("%s: descriptor table in %s memory\n", name,
759 set.table_in_device_local_memory() ? "device-local" : "host-visible"); 780 set.table_in_device_local_memory() ? "device-local" : "host-visible");
760 std::printf("%s: gradient at the %uth slot probed, miss, table grown %u -> %u slots, rebound, restored, other view, removed\n", name, 781 std::printf("%s: gradient at the %uth slot probed, miss, table grown %u -> %u slots, rebound, restored, "
761 depth + 1, slots, slot_count()); 782 "other view, removed, index %u of %u 2D images and %u of %u samplers\n", name, depth + 1, slots,
783 slot_count(), far_image, set.image_capacity(ImageDimension::dim_2d), far_sampler,
784 set.sampler_capacity());
762 ++runs; 785 ++runs;
763 vkDeviceWaitIdle(dev); 786 vkDeviceWaitIdle(dev);
764 vkDestroyPipeline(dev, pipeline, nullptr); 787 vkDestroyPipeline(dev, pipeline, nullptr);