diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 23b758897d..f739eaa435 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -680,9 +680,13 @@ side inwards: covers a full 2048-sampler D3D12 heap plus room for literal and static S#s. - 2D images: up to 65536. - storage arrays: up to 4096 each, capped by their dimension's sampled array or by 256 for storage-only 1D arrays. - - 2D-array images up to 16384, 3D and 1D images up to 4096 each (the title streams more cube maps, texture - arrays and 1D lookups than the defaults hold: each distinct T# of one takes its own index, and a native boot - filled the 1024 2D-array and 256 1D indices during the story load). + - 2D-array, 3D and 1D images: up to 65536 each, as many as 2D, grown together (one count for the three while the + limits stop them short of it), so no dimension is favoured. A texture keeps an index for each distinct T# of + it for as long as it lives, and which dimensions its textures have is the title's choice: a native boot + filled the default 1024 2D-array and 256 1D indices during the story load, and world runs filled the 4096 1D + indices of the sizes before this with 4092 T#s of 4090 live 1D textures. The bound is memory, not the + device's limits (RADV allows 8388606 sampled images): every element of every array is in each generation's + descriptor pool, used or not. Growth leaves 64 descriptors of each limit to the other sets of a pipeline layout. It also stays within `maxPerSetDescriptors`, which never shrinks the default counts, because a layout past it may still be @@ -698,9 +702,18 @@ side inwards: shader reads the fallback. `d3d12_descriptor_test` then fills each short array to its last index and checks that the next view and sampler are refused. The MS arrays keep their default counts. On an RTX 5070 (driver 621.24, 1048576 of each type - and 4000 samplers) the set has 65536 2D images, 3936 samplers, 16384 2D-array, 4096 3D and 4096 1D images and - 4096 storage indices in each dimension: 152929 descriptors, 5-10 MiB at 32-64 bytes a descriptor. The device prints the counts it chose, and - `bindless_sizing_test` checks the rules on synthetic limits. + and 4000 samplers) the set has 65536 2D, 2D-array, 3D and 1D images each, 3936 samplers and 4096 storage indices + in each dimension: 325729 descriptors with the MS pairs, 10-20 MiB at 32-64 bytes a descriptor. RADV (Mesa 26.3, + the Raphael iGPU and the RX 7900 XTX) and llvmpipe get the same image counts and 4096 samplers. The device prints + the counts it chose (`bindless set:`), `bindless_sizing_test` checks the rules on synthetic limits, and + `vk_bindless_dimensions_test` reads 5000 live 1D, 2D-array and 3D images each through their indices in a set + sized from the device's own limits. + - A T# the title writes into a console descriptor heap that set 0 has no room for is refused, and counted in the + heap's `unbound T#s by reason` line, as `the bindless array of the T#'s dimension is full` (or `the bindless + storage arrays of the T#'s dimension are full` for a UAV whose dimension has free indices only above the shorter + storage arrays), not as `no view for the T#'s format or range`. The first such refusal of a dimension also logs + `a bindless image array is full; dimension N (name)` with the indices, the T#s and textures that hold them and how + many of those textures are still registered at their address (a texture's indices go back when it goes). - An image view has one index in its dimension (2D, 2D array, 3D, 1D, 2DMS, 2DMS array or 1D array), shared by the bindings for that dimension. The runtime adds sampled-image bindings in pairs for 2DMS and 2DMS array: 20/21 for floating-point and normalized color, 22/23 for unsigned integers, and 24/25 for signed integers. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 566a8a0730..6dfb8d0b5d 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2284,6 +2284,34 @@ if(TARGET Vulkan::Headers) add_test(NAME vk_null_descriptor COMMAND vk_null_descriptor_test) set_tests_properties(vk_null_descriptor PROPERTIES SKIP_RETURN_CODE 77) endif() + # More than 4096 live 1D, 2D-array and 3D images, each read through its bindless index, in a set sized from the + # device's limits; a full array's refusal reason and indices coming back. The shader is GLSL; without + # glslangValidator the test skips. + add_executable(vk_bindless_dimensions_test test/vk_bindless_dimensions_test.cpp) + target_link_libraries(vk_bindless_dimensions_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS}) + if(WIN32) + target_compile_definitions(vk_bindless_dimensions_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + endif() + if(MSVC) + target_compile_options(vk_bindless_dimensions_test PRIVATE /W4 /permissive- /utf-8) + endif() + if(Vulkan_GLSLANG_VALIDATOR_EXECUTABLE) + set(_bindless_dimensions_spv ${CMAKE_CURRENT_BINARY_DIR}/generated/cs_bindless_dimensions.spv) + add_custom_command(OUTPUT ${_bindless_dimensions_spv} + COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} -V --target-env vulkan1.3 -o ${_bindless_dimensions_spv} + ${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/cs_bindless_dimensions.comp + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/test/glsl/cs_bindless_dimensions.comp + COMMENT "Compiling the bindless dimensions test shader" VERBATIM) + target_sources(vk_bindless_dimensions_test PRIVATE ${_bindless_dimensions_spv}) + target_compile_definitions(vk_bindless_dimensions_test PRIVATE + LEONIDA_TEST_BINDLESS_DIMENSIONS_SPV="${_bindless_dimensions_spv}") + else() + message(STATUS "glslangValidator not found: vk_bindless_dimensions_test skips") + endif() + if(BUILD_TESTING) + add_test(NAME vk_bindless_dimensions COMMAND vk_bindless_dimensions_test) + set_tests_properties(vk_bindless_dimensions PROPERTIES SKIP_RETURN_CODE 77) + endif() # CPU-only: the bindless set's array sizes from synthetic device limits. add_executable(bindless_sizing_test test/bindless_sizing_test.cpp) target_link_libraries(bindless_sizing_test PRIVATE Vulkan::Headers) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index b03081c7ed..e344c6eff6 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -7116,7 +7116,9 @@ struct DeviceState { // Gives the T# `words` a view in set 0 and an entry in the hash table, unless it has them. `storage`: shaders // write through it too (an unordered-access view), so it also goes into the storage arrays, which are - // shorter. False when set 0 cannot show the image; the descriptor then finds the fallback at index 0. + // shorter. False when set 0 cannot show the image; the descriptor then finds the fallback at index 0. `why`, when + // given, is set to the reason when it is that set 0 has no room (or no array) for the image's dimension, and left + // alone otherwise (no view could be made for the T#). bool bind_image(const leonida::gfx10::ImageWords& words, const leonida::gfx10::Image& image, VkImage vk_image, VkFormat image_format, VkImageAspectFlags aspects, bool storage, bool allow_channel_cast = false, VkFormat uint_backed = VK_FORMAT_UNDEFINED) { @@ -7126,7 +7128,8 @@ struct DeviceState { } bool bind_image_locked(const leonida::gfx10::ImageWords& words, const leonida::gfx10::Image& image, VkImage vk_image, VkFormat image_format, VkImageAspectFlags aspects, bool storage, - bool allow_channel_cast = false, VkFormat uint_backed = VK_FORMAT_UNDEFINED) { + bool allow_channel_cast = false, VkFormat uint_backed = VK_FORMAT_UNDEFINED, + const char** why = nullptr) { namespace g = leonida::gfx10; if (!bindless) return false; auto pending = pending_image_keys.find(vk_image); @@ -7171,7 +7174,7 @@ struct DeviceState { const bool sampled_only = uint_backed || (view_format != image_format && view_format != VK_FORMAT_UNDEFINED); return bind_image_views_locked(words, image, vk_image, view_format, channels_format, uint_backed ? image_format : view_format, aspects, true, storage, {}, - sampled_only); + sampled_only, why); } // The same for a view whose formats the caller has worked out: `view_format` of the sampled view (made when // `sampled`), `channels_format` whose channels the T#'s swizzle selects from, `storage_format` of the storage view. @@ -7185,7 +7188,7 @@ struct DeviceState { bool bind_image_views_locked(const leonida::gfx10::ImageWords& words, const leonida::gfx10::Image& image, VkImage vk_image, VkFormat view_format, VkFormat channels_format, VkFormat storage_format, VkImageAspectFlags aspects, bool sampled, bool storage, - VolumeSlices slices, bool sampled_only) { + VolumeSlices slices, bool sampled_only, const char** why = nullptr) { namespace g = leonida::gfx10; if (!bindless) { if (storage) log_once("unordered-access view needs the Vulkan bindless set; type", int(image.type)); @@ -7340,24 +7343,37 @@ struct DeviceState { vk.vkDestroyImageView(info.device, entry.mirror_storage, nullptr); entry.mirror_storage = VK_NULL_HANDLE; } - if (first_diagnostic("a bindless image array is full; dimension", int(dimension))) { + using Refusal = leonida::BindlessSet::ImageRefusal; + const Refusal refusal = bindless->image_refusal(dimension, entry.storage != VK_NULL_HANDLE, sampled_class); + if (why) + *why = refusal == Refusal::no_array ? "set 0 has no array for the T#'s dimension" + : refusal == Refusal::storage_full ? "the bindless storage arrays of the T#'s dimension are full" + : "the bindless array of the T#'s dimension is full"; + if (refusal != Refusal::no_array && + first_diagnostic("a bindless image array is full; dimension", int(dimension))) { // Whether the array holds live textures or stale views: the T#s it holds, the textures they name, - // and the most T#s of one texture (a texture whose T#s keep changing, a streamed mip clamp say, - // keeps an index for each until it goes). - std::size_t entries = 0, replaced = 0, most = 0; + // the most T#s of one texture (a texture whose T#s keep changing, a streamed mip clamp say, keeps an + // index for each until it goes), and how many of those textures are still the one registered at + // their address (a texture leaves the set when the one registered there goes: forget_image). + std::size_t entries = 0, replaced = 0, most = 0, registered = 0; std::map per_texture; for (const auto& [key, held] : image_entries) if (held.dimension == dimension) { ++entries; most = std::max(most, ++per_texture[held.address]); } + for (const auto& texture : per_texture) registered += images_by_address.count(texture.first); for (const auto& held : replaced_images) replaced += held.dimension == dimension; + constexpr const char* names[] = {"2D", "2D array", "3D", "1D", "1D array", "2D MS", "2D MS array"}; std::fprintf(stderr, - "[rt] d3d12: a bindless image array is full; dimension %d: %u indices, %zu T#s of %zu " - "textures (at most %zu T#s of one), %zu replaced views kept; further T#s of this " - "dimension read the fallback\n", - int(dimension), bindless->image_capacity(dimension), entries, per_texture.size(), most, - replaced); + "[rt] d3d12: a bindless image array is full; dimension %d (%s)%s: %u indices, %u in the " + "storage arrays, %zu T#s of %zu textures (%zu registered at their address, at most %zu " + "T#s of one), %zu replaced views kept; further T#s of this dimension read the " + "fallback\n", + int(dimension), names[int(dimension)], + refusal == Refusal::storage_full ? ", its storage range" : "", + bindless->image_capacity(dimension), bindless->storage_capacity(dimension), entries, + per_texture.size(), registered, most, replaced); } if (entry.sampled) vk.vkDestroyImageView(info.device, entry.sampled, nullptr); if (entry.storage) vk.vkDestroyImageView(info.device, entry.storage, nullptr); @@ -13665,9 +13681,10 @@ TitleImage bind_title_image(DeviceState& device, const leonida::gfx10::ImageWord return refuse("T# names layers the texture lacks"); const bool storage = (d.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) && !msaa && r.aspects() == VK_IMAGE_ASPECT_COLOR_BIT; + const char* unbound = "no view for the T#'s format or range"; // unless set 0 has no room (bind_image) if (!device.bind_image_locked(words, image, r.descriptor_image(), r.format(), r.aspects(), storage, - r.has_view_format_list(), r.uint_backed())) - return refuse("no view for the T#'s format or range"); + r.has_view_format_list(), r.uint_backed(), &unbound)) + return refuse(unbound); r.note_shader_view(); // Include descriptors written directly into title heaps. if (r.sparse_image()) sparse = r.id(); } diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h index da6d680f02..fc1b2f555e 100644 --- a/runtime/src/vulkan_bindless.h +++ b/runtime/src/vulkan_bindless.h @@ -239,14 +239,15 @@ struct SetLayout { SetLayout& operator=(const SetLayout&) = delete; }; -// What size_bindings() grows to where the device's limits allow. At 32-64 bytes a descriptor the whole set is then -// 5-10 MiB with 153697 descriptors when all MS classes and array bindings reach their limits (an RTX 5070). -inline constexpr std::uint32_t max_2d_images = 65536; -// The other single-sample dimensions: a title streams far more cube maps and texture arrays (and 1D lookup -// textures) than the 1024 and 256 of the defaults, each distinct T# of one taking its own index. -inline constexpr std::uint32_t max_2d_array_images = 16384; -inline constexpr std::uint32_t max_3d_images = 4096; -inline constexpr std::uint32_t max_1d_images = 4096; +// What size_bindings() grows the sampled array of every single-sample dimension (2D, 2D array, 3D and 1D) to where +// the device's limits allow: one size for all of them. Which dimensions its textures have is the title's choice, and +// an image keeps its index (one for each distinct T# of it) for as long as the texture lives, so no dimension gets +// less room than another: a world run kept 4090 live 1D textures, the whole of the 4096 1D indices the set once +// stopped at, while the 2D arrays had room to spare. The bound is the set's memory, not the device's descriptor +// limits (8388606 sampled images on RADV): every element of every array is in each generation's descriptor pool, +// used or not. At 32-64 bytes a descriptor the whole set is 10-20 MiB with 325729 descriptors when all MS classes +// and arrays reach their limits (an RTX 5070). +inline constexpr std::uint32_t max_images = 65536; inline constexpr std::uint32_t max_storage_indices = 4096; // in each storage array, at most its dimension's images // A full D3D12 shader-visible sampler heap (2048) and room for literal and static S#s, within what the device can // create (maxSamplerAllocationCount, at least 4000) less the samplers that are not in the set. @@ -261,7 +262,8 @@ inline constexpr std::uint32_t growth_headroom = 64; // standalone samplers do not, though they count in an update-after-bind pool. // 1. The default counts, or as much of them as the device holds: samplers, then storage, then 2D images. // 2. The MS pairs that fit beside them (limit_ms_bindings, which may turn classes off). -// 3. Growth towards the maximums above, in the same order, then the 2D-array, 3D and 1D images, within +// 3. Growth towards the maximums above, in the same order, then the 2D-array, 3D and 1D images together (one +// count for the three, so a device whose limits stop growth short of max_images favours none of them), within // maxPerSetDescriptors. That limit only says what is sure to work (vkGetDescriptorSetLayoutSupport may allow // more), so it never shrinks step 1. // A storage array holds the smaller of the storage count and its dimension's sampled count: add_image never @@ -363,11 +365,23 @@ inline bool size_bindings(BindlessFeatures& features, const VkPhysicalDeviceLimi const std::uint32_t creatable = limits.maxSamplerAllocationCount - std::min(limits.maxSamplerAllocationCount, reserved_samplers); grow(samplers, std::max(samplers, std::min(max_samplers, creatable)), growth_headroom, set_limit); - grow(images, std::max(images, max_2d_images), growth_headroom, set_limit); + grow(images, std::max(images, max_images), growth_headroom, set_limit); grow(storage, std::max(storage, max_storage_indices), growth_headroom, set_limit); - grow(arrays, std::max(arrays, max_2d_array_images), growth_headroom, set_limit); - grow(volumes, std::max(volumes, max_3d_images), growth_headroom, set_limit); - grow(lines, std::max(lines, max_1d_images), growth_headroom, set_limit); + // The other three dimensions grow as one: each gets the larger of its count so far and the shared one. + const std::uint32_t arrays_before = arrays, volumes_before = volumes, lines_before = lines; + std::uint32_t together = 0; + const auto set_together = [&](std::uint32_t n) { + arrays = std::max(arrays_before, n); + volumes = std::max(volumes_before, n); + lines = std::max(lines_before, n); + }; + for (std::uint32_t high = max_images; together < high;) { + const std::uint32_t n = together + (high - together + 1) / 2; + set_together(n); + if (fits(growth_headroom, set_limit)) together = n; + else high = n - 1; + } + set_together(together); apply(true); return full; } @@ -484,7 +498,7 @@ class BindlessSet { StorageViews storage{}; }; - // Gives an image an index in its dimension's bindings: 0 when the dimension is full. `sampled` goes to the + // Gives an image an index in its dimension's bindings: 0 when the dimension is full (image_refusal says why). `sampled` goes to the // sampled array in `sampled_layout` (the layout the image is in when shaders read it); the storage views go to // the storage arrays in GENERAL. For MS views, sampled_class selects the float, uint or sint sampled array. // `mirror` adds the same index in the 2D arrays (a 2D-array or cube image's slice 0): the slot must then be free @@ -553,6 +567,23 @@ class BindlessSet { return index; } + // Why add_image returned 0 for an image of `dimension` (with storage views when `stores`; sampled_class for the + // MS dimensions): the set has no array for it; every index of its dimension is taken (array_full: in use, or + // removed and held until the next flush that brings the table up to date); or only the storage arrays' shorter + // range is, which an image with storage views needs an index in (storage_full). + enum class ImageRefusal { no_array, array_full, storage_full }; + ImageRefusal image_refusal(ImageDimension dimension, bool stores, std::uint32_t sampled_class = 0) const { + if (!stores && bindless::sampled_binding(dimension) == bindless::no_binding) return ImageRefusal::no_array; + if (bindless::multisampled(dimension) && (stores || !supports_ms_class(sampled_class))) + return ImageRefusal::no_array; + const auto& used = image_used_[std::uint32_t(dimension)]; + const std::size_t low_end = std::min(used.size(), storage_capacity(dimension)); + const auto storage_end = used.begin() + std::ptrdiff_t(low_end); + const bool storage_free = low_end > 1 && std::find(used.begin() + 1, storage_end, std::uint8_t(0)) != storage_end; + return stores && !storage_free && free_images(dimension) ? ImageRefusal::storage_full + : ImageRefusal::array_full; + } + // New views or a new layout for an index add_image returned (a texture's state changed, say). `mirror` is // refreshed too when the image was added with one (mirror_of says whose 2D slot it is). The slot bookkeeping // follows what was written, and `kind_out`, when given, receives the index's kind word afterwards for a diff --git a/runtime/test/bindless_sizing_test.cpp b/runtime/test/bindless_sizing_test.cpp index b7d501aab1..b83294852b 100644 --- a/runtime/test/bindless_sizing_test.cpp +++ b/runtime/test/bindless_sizing_test.cpp @@ -133,18 +133,18 @@ int main() { const Device d = uniform(1048576, 1048576); BindlessFeatures f = with_ms(update_after_bind); EXPECT(bindless::size_bindings(f, d.limits, d.v11, d.v12)); - EXPECT(count(f, D::dim_2d) == bindless::max_2d_images); + EXPECT(count(f, D::dim_2d) == bindless::max_images); EXPECT(f.counts[bindless::sampler_binding] == 4000 - bindless::reserved_samplers); EXPECT(f.counts[bindless::sampler_binding] > 2048); // a full sampler heap and the fallback EXPECT(storage(f, D::dim_2d) == bindless::max_storage_indices); - // The other dimensions grow too (a title's cube maps, texture arrays and 1D lookups outnumber the defaults); - // their storage arrays hold as many indices as the 2D one. The storage-only 1D array keeps its 256. - EXPECT(count(f, D::dim_2d_array) == bindless::max_2d_array_images && - count(f, D::dim_3d) == bindless::max_3d_images && count(f, D::dim_1d) == bindless::max_1d_images); + // The other dimensions grow as far as 2D (a title's textures are of whichever dimension it chose); their + // storage arrays hold as many indices as the 2D one. The storage-only 1D array keeps its 256. + EXPECT(count(f, D::dim_2d_array) == bindless::max_images && count(f, D::dim_3d) == bindless::max_images && + count(f, D::dim_1d) == bindless::max_images); EXPECT(storage(f, D::dim_2d_array) == bindless::max_storage_indices && storage(f, D::dim_3d) == bindless::max_storage_indices && storage(f, D::dim_1d) == bindless::max_storage_indices); - EXPECT(storage(f, D::dim_1d_array) == 256 && totals(f).all == 153697); + EXPECT(storage(f, D::dim_1d_array) == 256 && totals(f).all == 325729); EXPECT(f.ms_samples[0] == VK_SAMPLE_COUNT_4_BIT && f.ms_samples[2] == VK_SAMPLE_COUNT_4_BIT); EXPECT(storage_consistent(f)); if (update_after_bind) @@ -152,6 +152,44 @@ int main() { count(f, D::dim_2d), f.counts[bindless::sampler_binding], storage(f, D::dim_2d), totals(f).all); } + // RADV (Mesa 26.3, a Raphael iGPU and an RX 7900 XTX alike) and llvmpipe: their update-after-bind limits hold + // every sampled dimension at the full size. A world run kept 4090 live 1D textures, filling the 4096 1D indices + // the set used to stop at whatever the device held; now 1D, 3D and 2D-array images get as many as 2D. + { + Device radv = uniform(8388606, 8388606); + radv.limits.maxPerStageResources = radv.v12.maxPerStageUpdateAfterBindResources = 8388606; + radv.v12.maxUpdateAfterBindDescriptorsInAllPools = 67108863; + radv.v11.maxPerSetDescriptors = 22369621; + radv.limits.maxSamplerAllocationCount = 4294967295u; + Device lvp = uniform(1015808, 1015808); + lvp.limits.maxPerStageResources = lvp.v12.maxPerStageUpdateAfterBindResources = 1015808; + lvp.v11.maxPerSetDescriptors = 1015808; + lvp.limits.maxSamplerAllocationCount = 32768; + for (const Device* d : {&radv, &lvp}) { + BindlessFeatures f = with_ms(true); + EXPECT(bindless::size_bindings(f, d->limits, d->v11, d->v12)); + for (const D dimension : {D::dim_2d, D::dim_2d_array, D::dim_3d, D::dim_1d}) { + EXPECT(count(f, dimension) == bindless::max_images); + EXPECT(count(f, dimension) > 4096 + 1); // more than 4096 live images and the fallback + } + EXPECT(storage_consistent(f)); + } + } + + // A device whose limits stop growth short of max_images: the 2D-array, 3D and 1D arrays share what is left alike, + // whichever is grown first. + { + Device d = uniform(1048576, 1048576); + d.v12.maxPerStageDescriptorUpdateAfterBindSampledImages = 65536 + 3 * 20000 + 6 * 256 + bindless::growth_headroom; + BindlessFeatures f = with_ms(true); + EXPECT(bindless::size_bindings(f, d.limits, d.v11, d.v12)); + EXPECT(count(f, D::dim_2d) == bindless::max_images); + EXPECT(count(f, D::dim_2d_array) == count(f, D::dim_3d) && count(f, D::dim_3d) == count(f, D::dim_1d)); + EXPECT(count(f, D::dim_1d) > 4096 && count(f, D::dim_1d) <= 20000); + EXPECT(totals(f).sampled + bindless::growth_headroom <= d.v12.maxPerStageDescriptorUpdateAfterBindSampledImages); + EXPECT(storage_consistent(f)); + } + // At today's sizes exactly (5632 sampled images, 1024 samplers, 4352 storage images): no MS pairs. { Device d = uniform(1048576, 1048576); @@ -191,7 +229,7 @@ int main() { BindlessFeatures uab = with_ms(true), plain = with_ms(false); EXPECT(bindless::size_bindings(uab, d.limits, d.v11, d.v12)); EXPECT(bindless::size_bindings(plain, d.limits, d.v11, d.v12)); - EXPECT(count(uab, D::dim_2d) == bindless::max_2d_images); + EXPECT(count(uab, D::dim_2d) == bindless::max_images); const Totals t = totals(plain); EXPECT(count(plain, D::dim_2d) > 4096 && count(plain, D::dim_2d) < 16384); EXPECT(t.sampled + bindless::growth_headroom <= 16384 && t.storage + bindless::growth_headroom <= 16384); @@ -232,7 +270,7 @@ int main() { BindlessFeatures f = with_ms(true); EXPECT(bindless::size_bindings(f, d.limits, d.v11, d.v12, all_supported)); EXPECT(asked > 0 && asked_within == 0 && missing_storage == 0); - EXPECT(count(f, D::dim_2d) == bindless::max_2d_images && + EXPECT(count(f, D::dim_2d) == bindless::max_images && f.counts[bindless::sampler_binding] == 4000 - bindless::reserved_samplers); EXPECT(storage(f, D::dim_2d) == bindless::max_storage_indices && storage_consistent(f)); diff --git a/runtime/test/glsl/cs_bindless_dimensions.comp b/runtime/test/glsl/cs_bindless_dimensions.comp new file mode 100644 index 0000000000..4a26fe276b --- /dev/null +++ b/runtime/test/glsl/cs_bindless_dimensions.comp @@ -0,0 +1,30 @@ +#version 460 +// Reads one texel of each of many images through the bindless set 0 (src/vulkan_bindless.h), for +// vk_bindless_dimensions_test: thread i reads the image at index indices[i] of the sampled array of one dimension +// (pc.dimension: 0 1D, 1 2D array, 2 3D; bindings 5, 3 and 6, as tools/rdna2spirv.py declares them) and writes the +// texel's red channel, as bits, to result[i]. One thread per workgroup, so each index is dynamically uniform and +// the test needs no non-uniform indexing feature. +#extension GL_EXT_nonuniform_qualifier : require +#extension GL_EXT_samplerless_texture_functions : require + +layout(local_size_x = 1) in; // one image per workgroup: the index is uniform there + +layout(push_constant) uniform Pass { uint dimension; uint count; } pc; + +layout(set = 0, binding = 3) uniform texture2DArray layers[]; +layout(set = 0, binding = 5) uniform texture1D lines[]; +layout(set = 0, binding = 6) uniform texture3D volumes[]; + +layout(set = 1, binding = 0, std430) writeonly buffer Result { uint result[]; }; +layout(set = 1, binding = 1, std430) readonly buffer Indices { uint indices[]; }; + +void main() { + const uint i = gl_GlobalInvocationID.x; + if (i >= pc.count) return; + const uint index = indices[i]; + float red; + if (pc.dimension == 0) red = texelFetch(lines[index], 0, 0).r; + else if (pc.dimension == 1) red = texelFetch(layers[index], ivec3(0), 0).r; + else red = texelFetch(volumes[index], ivec3(0), 0).r; + result[i] = floatBitsToUint(red); +} diff --git a/runtime/test/vk_bindless_dimensions_test.cpp b/runtime/test/vk_bindless_dimensions_test.cpp new file mode 100644 index 0000000000..3da8370d66 --- /dev/null +++ b/runtime/test/vk_bindless_dimensions_test.cpp @@ -0,0 +1,421 @@ +// More live images of each sampled dimension than 4096, through the bindless set 0 (src/vulkan_bindless.h), at the +// Vulkan level, and the index allocator behind them. +// +// An image keeps its index in its dimension's array for as long as it lives, so the array of a dimension must hold +// every live image of it a title has. A world run kept 4090 live 1D textures and filled the 4096 1D indices the set +// once stopped at (bindless::size_bindings capped 1D and 3D at 4096 whatever the device held); every later 1D T# then +// read the fallback's zeros. For 1D, 2D-array and 3D images alike this test sizes the set from the device's limits +// as the D3D12 device does, gives 5000 distinct images (one texel each, holding its own number) an index each, +// and has test/glsl/cs_bindless_dimensions.comp read every one of them through its index: each must read its own +// number. Then, on the same set: +// - the rest of the array is filled: the index after the last is refused, and image_refusal says the array of +// that dimension is full (the D3D12 device's "unbound T#s by reason" line names that reason); +// - an image removed while the table has no changes the GPU lacks gives its index back at once, and one removed +// while it has (an erased key) gives it back at the next flush, not before. +// Exit 77 without a Vulkan device with the bindless features or without the shader (no glslangValidator), and for +// a dimension whose device limits hold fewer than 5000 images (the test says so). +// LEONIDA_VK_VALIDATION=1 runs under VK_LAYER_KHRONOS_validation; a validation error fails the test. +#ifndef VK_NO_PROTOTYPES +#define VK_NO_PROTOTYPES +#endif +#include "vk_test_device.h" +#include "shader_fixture_path.h" +#include "../src/vulkan_bindless.h" + +#include +#include +#include +#include +#include +#include + +#define EXPECT(condition) \ + do { \ + if (!(condition)) { \ + std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ + return 1; \ + } \ + } while (0) + +namespace { + +using namespace leonida; + +constexpr std::uint32_t live_images = 5000; // more than the 4096 1D and 3D indices of the old sizes + +std::vector load_spirv(const char* path) { + std::ifstream file(vktest::shader_fixture_path(path), std::ios::binary); + const std::vector bytes{std::istreambuf_iterator(file), {}}; + std::vector words(bytes.size() / 4); + if (!words.empty()) std::memcpy(words.data(), bytes.data(), words.size() * 4); + return words; +} + +const char* name(ImageDimension dimension) { + return dimension == ImageDimension::dim_1d ? "1D" : dimension == ImageDimension::dim_2d_array ? "2D array" : "3D"; +} + +// The images of one dimension, `count` texels in all, each in its own image (3D) or array layer (1D, 2D array), and a +// view of each: what the test reads through the set. Destroys what it made. +struct Images { + vktest::Device& d; + std::vector images; + std::vector memory; + std::vector views; + struct Texel { + VkImage image; + std::uint32_t layer; + }; + std::vector texels; + + explicit Images(vktest::Device& device) : d(device) {} + Images(const Images&) = delete; + Images& operator=(const Images&) = delete; + ~Images() { + for (const VkImageView view : views) d.fn("vkDestroyImageView")(d.device, view, nullptr); + for (const VkImage image : images) d.fn("vkDestroyImage")(d.device, image, nullptr); + for (const VkDeviceMemory m : memory) d.fn("vkFreeMemory")(d.device, m, nullptr); + } + + bool make(ImageDimension dimension, std::uint32_t count, std::uint32_t max_layers, + const VkPhysicalDeviceMemoryProperties& properties) { + const bool volume = dimension == ImageDimension::dim_3d; + const std::uint32_t per_image = volume ? 1 : std::min(max_layers, count); + // Images in groups that share one allocation (each a few MiB at most). + constexpr std::uint32_t group = 512; + for (std::uint32_t first = 0; first < count;) { + std::vector made; + std::uint32_t in_group = 0; + for (; in_group < group && first < count; ++in_group) { + VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; + info.imageType = volume ? VK_IMAGE_TYPE_3D + : dimension == ImageDimension::dim_1d ? VK_IMAGE_TYPE_1D : VK_IMAGE_TYPE_2D; + info.format = VK_FORMAT_R32_SFLOAT; + info.extent = {1, 1, 1}; + info.mipLevels = 1; + info.arrayLayers = std::min(per_image, count - first); + info.samples = VK_SAMPLE_COUNT_1_BIT; + info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; + VkImage image = VK_NULL_HANDLE; + if (d.fn("vkCreateImage")(d.device, &info, nullptr, &image) != VK_SUCCESS) + return false; + images.push_back(image); + made.push_back(image); + for (std::uint32_t layer = 0; layer < info.arrayLayers; ++layer) texels.push_back({image, layer}); + first += info.arrayLayers; + } + VkMemoryRequirements requirements{}; + d.fn("vkGetImageMemoryRequirements")(d.device, made[0], &requirements); + const VkDeviceSize stride = + (requirements.size + requirements.alignment - 1) / requirements.alignment * requirements.alignment; + VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; + allocation.allocationSize = stride * made.size(); + allocation.memoryTypeIndex = 0xffffffffu; + for (std::uint32_t t = 0; t < properties.memoryTypeCount; ++t) + if (requirements.memoryTypeBits & (1u << t)) { + allocation.memoryTypeIndex = t; + break; + } + VkDeviceMemory block = VK_NULL_HANDLE; + if (allocation.memoryTypeIndex == 0xffffffffu || + d.fn("vkAllocateMemory")(d.device, &allocation, nullptr, &block) != VK_SUCCESS) + return false; + memory.push_back(block); + for (std::size_t k = 0; k < made.size(); ++k) { + VkMemoryRequirements each{}; + d.fn("vkGetImageMemoryRequirements")(d.device, made[k], &each); + if (each.size > stride || !(each.memoryTypeBits & (1u << allocation.memoryTypeIndex)) || + d.fn("vkBindImageMemory")(d.device, made[k], block, stride * k) != + VK_SUCCESS) + return false; + } + } + for (const Texel& texel : texels) { + VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; + info.image = texel.image; + info.viewType = volume ? VK_IMAGE_VIEW_TYPE_3D + : dimension == ImageDimension::dim_1d ? VK_IMAGE_VIEW_TYPE_1D : VK_IMAGE_VIEW_TYPE_2D_ARRAY; + info.format = VK_FORMAT_R32_SFLOAT; + info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, texel.layer, 1}; + VkImageView view = VK_NULL_HANDLE; + if (d.fn("vkCreateImageView")(d.device, &info, nullptr, &view) != VK_SUCCESS) + return false; + views.push_back(view); + } + return true; + } + + // Texel i holds i + 1, in GENERAL (the layout the set's descriptors name). + void record_fill(VkCommandBuffer cb) const { + std::vector barriers; + for (const VkImage image : images) { + VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; + barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; + barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL; + barrier.srcQueueFamilyIndex = barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + barrier.image = image; + barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, VK_REMAINING_ARRAY_LAYERS}; + barriers.push_back(barrier); + } + d.fn("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, + nullptr, std::uint32_t(barriers.size()), barriers.data()); + for (std::size_t i = 0; i < texels.size(); ++i) { + VkClearColorValue value{}; + value.float32[0] = float(i + 1); + const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, texels[i].layer, 1}; + d.fn("vkCmdClearColorImage")(cb, texels[i].image, VK_IMAGE_LAYOUT_GENERAL, + &value, 1, &range); + } + VkMemoryBarrier filled{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; + filled.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + filled.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; + d.fn("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, &filled, 0, + nullptr, 0, nullptr); + } +}; + +// The compute pipeline over set 0 (the bindless set's layout) and set 1 (result and indices). +struct Reader { + vktest::Device& d; + VkShaderModule module = VK_NULL_HANDLE; + VkDescriptorSetLayout set1_layout = VK_NULL_HANDLE; + VkDescriptorPool pool = VK_NULL_HANDLE; + VkDescriptorSet set1 = VK_NULL_HANDLE; + VkPipelineLayout layout = VK_NULL_HANDLE; + VkPipeline pipeline = VK_NULL_HANDLE; + void* result_map = nullptr; + void* index_map = nullptr; + + explicit Reader(vktest::Device& device) : d(device) {} + Reader(const Reader&) = delete; + Reader& operator=(const Reader&) = delete; + ~Reader() { + const VkDevice dev = d.device; + if (pipeline) d.fn("vkDestroyPipeline")(dev, pipeline, nullptr); + if (layout) d.fn("vkDestroyPipelineLayout")(dev, layout, nullptr); + if (pool) d.fn("vkDestroyDescriptorPool")(dev, pool, nullptr); + if (set1_layout) d.fn("vkDestroyDescriptorSetLayout")(dev, set1_layout, nullptr); + if (module) d.fn("vkDestroyShaderModule")(dev, module, nullptr); + } + + bool make(VkDescriptorSetLayout set0_layout, const std::vector& code) { + const VkDevice dev = d.device; + VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; + module_info.codeSize = code.size() * 4; + module_info.pCode = code.data(); + if (d.fn("vkCreateShaderModule")(dev, &module_info, nullptr, &module) != VK_SUCCESS) + return false; + const VkBuffer result = d.buffer(live_images * 4, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &result_map); + const VkBuffer indices = d.buffer(live_images * 4, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &index_map); + if (!result || !indices) return false; + const VkDescriptorSetLayoutBinding bindings[2] = { + {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}, + {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}}; + VkDescriptorSetLayoutCreateInfo set1_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; + set1_info.bindingCount = 2; + set1_info.pBindings = bindings; + if (d.fn("vkCreateDescriptorSetLayout")(dev, &set1_info, nullptr, + &set1_layout) != VK_SUCCESS) + return false; + const VkDescriptorPoolSize pool_size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}; + VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; + pool_info.maxSets = 1; + pool_info.poolSizeCount = 1; + pool_info.pPoolSizes = &pool_size; + if (d.fn("vkCreateDescriptorPool")(dev, &pool_info, nullptr, &pool) != VK_SUCCESS) + return false; + VkDescriptorSetAllocateInfo allocate{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; + allocate.descriptorPool = pool; + allocate.descriptorSetCount = 1; + allocate.pSetLayouts = &set1_layout; + if (d.fn("vkAllocateDescriptorSets")(dev, &allocate, &set1) != VK_SUCCESS) + return false; + const VkDescriptorBufferInfo infos[2] = {{result, 0, VK_WHOLE_SIZE}, {indices, 0, VK_WHOLE_SIZE}}; + VkWriteDescriptorSet writes[2]{}; + for (std::uint32_t b = 0; b < 2; ++b) { + writes[b] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; + writes[b].dstSet = set1; + writes[b].dstBinding = b; + writes[b].descriptorCount = 1; + writes[b].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + writes[b].pBufferInfo = &infos[b]; + } + d.fn("vkUpdateDescriptorSets")(dev, 2, writes, 0, nullptr); + const VkDescriptorSetLayout layouts[2] = {set0_layout, set1_layout}; + const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, 8}; + VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; + layout_info.setLayoutCount = 2; + layout_info.pSetLayouts = layouts; + layout_info.pushConstantRangeCount = 1; + layout_info.pPushConstantRanges = &push; + if (d.fn("vkCreatePipelineLayout")(dev, &layout_info, nullptr, &layout) != + VK_SUCCESS) + return false; + VkComputePipelineCreateInfo pipeline_info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; + pipeline_info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}; + pipeline_info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; + pipeline_info.stage.module = module; + pipeline_info.stage.pName = "main"; + pipeline_info.layout = layout; + return d.fn("vkCreateComputePipelines")(dev, VK_NULL_HANDLE, 1, &pipeline_info, + nullptr, &pipeline) == VK_SUCCESS; + } + + void record(VkCommandBuffer cb, VkDescriptorSet set0, std::uint32_t dimension, std::uint32_t count) const { + d.fn("vkCmdBindPipeline")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); + const VkDescriptorSet sets[2] = {set0, set1}; + d.fn("vkCmdBindDescriptorSets")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout, 0, 2, + sets, 0, nullptr); + const std::uint32_t pass[2] = {dimension, count}; + d.fn("vkCmdPushConstants")(cb, layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 8, pass); + d.fn("vkCmdDispatch")(cb, count, 1, 1); + VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; + host.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; + host.dstAccessMask = VK_ACCESS_HOST_READ_BIT; + d.fn("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, + VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &host, 0, nullptr, 0, + nullptr); + } +}; + +int run(vktest::Device& d, const VkPhysicalDeviceMemoryProperties& memory, const BindlessFeatures& features, + std::uint32_t max_layers, const std::vector& code, ImageDimension dimension) { + BindlessSet set(d.device_proc, d.device, memory, features); + EXPECT(set.ok()); + const std::uint32_t capacity = set.image_capacity(dimension); + std::printf("%s: %u indices in the set\n", name(dimension), capacity); + // Every live image needs an index, and index 0 is the fallback. + EXPECT(capacity > live_images); + + Images images(d); + EXPECT(images.make(dimension, live_images, max_layers, memory)); + std::vector indices; + for (const VkImageView view : images.views) { + const std::uint32_t index = set.add_image(dimension, view, VK_IMAGE_LAYOUT_GENERAL); + EXPECT(index != 0); + indices.push_back(index); + } + Reader reader(d); + EXPECT(reader.make(set.layout(), code)); + std::memcpy(reader.index_map, indices.data(), indices.size() * 4); + std::memset(reader.result_map, 0xcd, live_images * 4); + const std::uint32_t pass = dimension == ImageDimension::dim_1d ? 0 : dimension == ImageDimension::dim_2d_array ? 1 : 2; + bool flushed = false; + const bool ran = d.run([&](VkCommandBuffer cb) { + set.record_fallback_init(cb); + flushed = set.flush(cb, 0, [] { return std::uint64_t(0); }); + images.record_fill(cb); + reader.record(cb, set.set(), pass, live_images); + return true; + }); + set.table_upload_result(ran); + EXPECT(ran && flushed); + std::vector result(live_images); + std::memcpy(result.data(), reader.result_map, live_images * 4); + std::uint32_t wrong = 0; + for (std::uint32_t i = 0; i < live_images; ++i) { + float red = 0; + std::memcpy(&red, &result[i], 4); + if (red != float(i + 1) && wrong++ < 4) + std::fprintf(stderr, "%s image %u at index %u read %g, expected %u\n", name(dimension), i, indices[i], + double(red), i + 1); + } + EXPECT(wrong == 0); + std::printf("%s: %u live images, each read its own texel through its index (the last at %u)\n", name(dimension), + live_images, indices.back()); + + // The rest of the array (the same view again: an index each, as distinct T#s of one texture get), then one + // more, refused for the reason that the array of its dimension is full. + std::uint32_t more = 0; + while (set.add_image(dimension, images.views[0], VK_IMAGE_LAYOUT_GENERAL)) ++more; + EXPECT(live_images + more == capacity - 1 && set.free_images(dimension) == 0); + EXPECT(set.image_refusal(dimension, false) == BindlessSet::ImageRefusal::array_full); + // Freed with nothing pending in the table: the index is free again at once. + set.remove_image(dimension, indices[7]); + EXPECT(set.free_images(dimension) == 1); + EXPECT(set.add_image(dimension, images.views[7], VK_IMAGE_LAYOUT_GENERAL) == indices[7]); + // Freed after its key is erased (the GPU's table may still name it): held until a flush brings the table up to + // date, then free. + const DescriptorKey key{0x1234u, 0, 0, 8u << 28, 0, 0, 0, 0}; + set.insert_key(key, indices[9], dimension); + set.erase_key(key); + set.remove_image(dimension, indices[9]); + EXPECT(set.free_images(dimension) == 0 && set.add_image(dimension, images.views[9], VK_IMAGE_LAYOUT_GENERAL) == 0); + EXPECT(set.image_refusal(dimension, false) == BindlessSet::ImageRefusal::array_full); + const bool reflushed = d.run([&](VkCommandBuffer cb) { return set.flush(cb, 1, [] { return std::uint64_t(1); }); }); + set.table_upload_result(reflushed); + EXPECT(reflushed && set.free_images(dimension) == 1); + EXPECT(set.add_image(dimension, images.views[9], VK_IMAGE_LAYOUT_GENERAL) == indices[9]); + std::printf("%s: %u more filled the array; the next was refused as full, and removed indices came back\n", + name(dimension), more); + + d.fn("vkDeviceWaitIdle")(d.device); + set.release_retired(); + return 0; +} + +} // namespace + +int main() { +#ifndef LEONIDA_TEST_BINDLESS_DIMENSIONS_SPV + std::printf("unsupported: the test shader was not built (no glslangValidator)\n"); + return 77; +#else + vktest::Device d; + if (const auto status = d.initialize(); status != vktest::DeviceStatus::ready) + return status == vktest::DeviceStatus::unavailable ? 77 : 1; + if (!d.bindless) { + std::printf("unsupported: the device lacks the bindless set's descriptor-indexing features\n"); + return 77; + } + VkPhysicalDeviceMemoryProperties memory{}; + d.instance_fn("vkGetPhysicalDeviceMemoryProperties")(d.physical, &memory); + VkPhysicalDeviceVulkan12Properties v12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES}; + VkPhysicalDeviceVulkan11Properties v11{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES, &v12}; + VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &v11}; + d.instance_fn("vkGetPhysicalDeviceProperties2")(d.physical, &properties); + std::printf("device: %s\n", properties.properties.deviceName); + + // Sized as the D3D12 device sizes it (d3d12_vulkan_device.cpp): from the update-after-bind limits, past + // maxPerSetDescriptors where vkGetDescriptorSetLayoutSupport accepts the layout. + BindlessFeatures features; + features.update_after_bind = true; + features.partially_bound = d.partially_bound; + const auto layout_support = + d.fn("vkGetDescriptorSetLayoutSupport"); + const auto layout_supported = [&](const BindlessFeatures& candidate) { + if (!layout_support) return false; + const bindless::SetLayout layout(candidate); + VkDescriptorSetLayoutSupport support{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_SUPPORT}; + layout_support(d.device, &layout.info, &support); + return support.supported == VK_TRUE; + }; + bindless::size_bindings(features, properties.properties.limits, v11, v12, layout_supported); + const std::uint32_t sampled_limit = std::min(v12.maxPerStageDescriptorUpdateAfterBindSampledImages, + v12.maxDescriptorSetUpdateAfterBindSampledImages); + // Room for 5000 images in each of the four sampled arrays besides 2D's: below that, the device itself holds + // fewer, and the test has nothing to say. + if (sampled_limit < bindless::max_images + 3 * (live_images + 1) + 2 * bindless::growth_headroom) { + std::printf("unsupported: the device holds %u sampled images in a set, too few for %u of each dimension\n", + sampled_limit, live_images); + return 77; + } + + const std::vector code = load_spirv(LEONIDA_TEST_BINDLESS_DIMENSIONS_SPV); + EXPECT(!code.empty()); + const std::uint32_t max_layers = properties.properties.limits.maxImageArrayLayers; + for (const ImageDimension dimension : + {ImageDimension::dim_1d, ImageDimension::dim_2d_array, ImageDimension::dim_3d}) + EXPECT(run(d, memory, features, max_layers, code, dimension) == 0); + + if (d.validation_errors()) { + std::printf("%u validation errors\n", d.validation_errors()); + return 1; + } + std::printf("vk_bindless_dimensions: ok\n"); + return 0; +#endif +} diff --git a/runtime/test/vk_bindless_test.cpp b/runtime/test/vk_bindless_test.cpp index dc33e7d918..e81181849f 100644 --- a/runtime/test/vk_bindless_test.cpp +++ b/runtime/test/vk_bindless_test.cpp @@ -967,7 +967,14 @@ int main(int argc, char** argv) { std::vector filled; for (std::uint32_t i = 1; i < set.image_capacity(ImageDimension::dim_1d); ++i) filled.push_back(set.add_image(ImageDimension::dim_1d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL)); - EXPECT(filled.front() == 1 && filled.back() == set.image_capacity(ImageDimension::dim_1d) - 1); + // Images without storage views take the indices above the storage arrays first, then the ones below: every + // index but the fallback's, once each. + std::vector sorted = filled; + std::sort(sorted.begin(), sorted.end()); + EXPECT(sorted.front() == 1 && sorted.back() == set.image_capacity(ImageDimension::dim_1d) - 1 && + std::adjacent_find(sorted.begin(), sorted.end()) == sorted.end()); + const std::uint32_t storage_end = set.storage_capacity(ImageDimension::dim_1d); + EXPECT(filled.front() == (storage_end < set.image_capacity(ImageDimension::dim_1d) ? storage_end : 1u)); EXPECT(set.free_images(ImageDimension::dim_1d) == 0); EXPECT(set.add_image(ImageDimension::dim_1d, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL) == 0); set.remove_image(ImageDimension::dim_1d, 7);