diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 16:55:04 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 16:55:04 -0500 |
| commit | df92abca8ee2a8f9093efea7fcaa4dadc3ddaebc (patch) | |
| tree | 97ef8f95544fe263049d3f7360823f16100de209 /runtime/test/vk_bindless_dimensions_test.cpp | |
| parent | 49a0b69fcdb80db1f2a44ce7ca0ab8dfdec1e403 (diff) | |
| parent | e5a7c8942678b323f655939c81f5d8855d20c248 (diff) | |
Merge main e5a7c8942 into shadow-aliased-guard (#1867)
One conflict, runtime/README.md: this branch rewrote the LEONIDA_SHADOW_ALIAS_MS
row, main (#2163) added the LEONIDA_SHADOW_KEEP row after it. Both rows kept;
the keep row's "aliased" now reads "aliased while they can't be guarded at every
address", since aliased pages are no longer copied without a sign of a write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/vk_bindless_dimensions_test.cpp')
| -rw-r--r-- | runtime/test/vk_bindless_dimensions_test.cpp | 421 |
1 files changed, 421 insertions, 0 deletions
diff --git a/runtime/test/vk_bindless_dimensions_test.cpp b/runtime/test/vk_bindless_dimensions_test.cpp new file mode 100644 index 000000000..3da8370d6 --- /dev/null +++ b/runtime/test/vk_bindless_dimensions_test.cpp | |||
| @@ -0,0 +1,421 @@ | |||
| 1 | // More live images of each sampled dimension than 4096, through the bindless set 0 (src/vulkan_bindless.h), at the | ||
| 2 | // Vulkan level, and the index allocator behind them. | ||
| 3 | // | ||
| 4 | // An image keeps its index in its dimension's array for as long as it lives, so the array of a dimension must hold | ||
| 5 | // every live image of it a title has. A world run kept 4090 live 1D textures and filled the 4096 1D indices the set | ||
| 6 | // once stopped at (bindless::size_bindings capped 1D and 3D at 4096 whatever the device held); every later 1D T# then | ||
| 7 | // read the fallback's zeros. For 1D, 2D-array and 3D images alike this test sizes the set from the device's limits | ||
| 8 | // as the D3D12 device does, gives 5000 distinct images (one texel each, holding its own number) an index each, | ||
| 9 | // and has test/glsl/cs_bindless_dimensions.comp read every one of them through its index: each must read its own | ||
| 10 | // number. Then, on the same set: | ||
| 11 | // - the rest of the array is filled: the index after the last is refused, and image_refusal says the array of | ||
| 12 | // that dimension is full (the D3D12 device's "unbound T#s by reason" line names that reason); | ||
| 13 | // - an image removed while the table has no changes the GPU lacks gives its index back at once, and one removed | ||
| 14 | // while it has (an erased key) gives it back at the next flush, not before. | ||
| 15 | // Exit 77 without a Vulkan device with the bindless features or without the shader (no glslangValidator), and for | ||
| 16 | // a dimension whose device limits hold fewer than 5000 images (the test says so). | ||
| 17 | // LEONIDA_VK_VALIDATION=1 runs under VK_LAYER_KHRONOS_validation; a validation error fails the test. | ||
| 18 | #ifndef VK_NO_PROTOTYPES | ||
| 19 | #define VK_NO_PROTOTYPES | ||
| 20 | #endif | ||
| 21 | #include "vk_test_device.h" | ||
| 22 | #include "shader_fixture_path.h" | ||
| 23 | #include "../src/vulkan_bindless.h" | ||
| 24 | |||
| 25 | #include <algorithm> | ||
| 26 | #include <cstdio> | ||
| 27 | #include <cstring> | ||
| 28 | #include <fstream> | ||
| 29 | #include <iterator> | ||
| 30 | #include <vector> | ||
| 31 | |||
| 32 | #define EXPECT(condition) \ | ||
| 33 | do { \ | ||
| 34 | if (!(condition)) { \ | ||
| 35 | std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ | ||
| 36 | return 1; \ | ||
| 37 | } \ | ||
| 38 | } while (0) | ||
| 39 | |||
| 40 | namespace { | ||
| 41 | |||
| 42 | using namespace leonida; | ||
| 43 | |||
| 44 | constexpr std::uint32_t live_images = 5000; // more than the 4096 1D and 3D indices of the old sizes | ||
| 45 | |||
| 46 | std::vector<std::uint32_t> load_spirv(const char* path) { | ||
| 47 | std::ifstream file(vktest::shader_fixture_path(path), std::ios::binary); | ||
| 48 | const std::vector<char> bytes{std::istreambuf_iterator<char>(file), {}}; | ||
| 49 | std::vector<std::uint32_t> words(bytes.size() / 4); | ||
| 50 | if (!words.empty()) std::memcpy(words.data(), bytes.data(), words.size() * 4); | ||
| 51 | return words; | ||
| 52 | } | ||
| 53 | |||
| 54 | const char* name(ImageDimension dimension) { | ||
| 55 | return dimension == ImageDimension::dim_1d ? "1D" : dimension == ImageDimension::dim_2d_array ? "2D array" : "3D"; | ||
| 56 | } | ||
| 57 | |||
| 58 | // The images of one dimension, `count` texels in all, each in its own image (3D) or array layer (1D, 2D array), and a | ||
| 59 | // view of each: what the test reads through the set. Destroys what it made. | ||
| 60 | struct Images { | ||
| 61 | vktest::Device& d; | ||
| 62 | std::vector<VkImage> images; | ||
| 63 | std::vector<VkDeviceMemory> memory; | ||
| 64 | std::vector<VkImageView> views; | ||
| 65 | struct Texel { | ||
| 66 | VkImage image; | ||
| 67 | std::uint32_t layer; | ||
| 68 | }; | ||
| 69 | std::vector<Texel> texels; | ||
| 70 | |||
| 71 | explicit Images(vktest::Device& device) : d(device) {} | ||
| 72 | Images(const Images&) = delete; | ||
| 73 | Images& operator=(const Images&) = delete; | ||
| 74 | ~Images() { | ||
| 75 | for (const VkImageView view : views) d.fn<PFN_vkDestroyImageView>("vkDestroyImageView")(d.device, view, nullptr); | ||
| 76 | for (const VkImage image : images) d.fn<PFN_vkDestroyImage>("vkDestroyImage")(d.device, image, nullptr); | ||
| 77 | for (const VkDeviceMemory m : memory) d.fn<PFN_vkFreeMemory>("vkFreeMemory")(d.device, m, nullptr); | ||
| 78 | } | ||
| 79 | |||
| 80 | bool make(ImageDimension dimension, std::uint32_t count, std::uint32_t max_layers, | ||
| 81 | const VkPhysicalDeviceMemoryProperties& properties) { | ||
| 82 | const bool volume = dimension == ImageDimension::dim_3d; | ||
| 83 | const std::uint32_t per_image = volume ? 1 : std::min(max_layers, count); | ||
| 84 | // Images in groups that share one allocation (each a few MiB at most). | ||
| 85 | constexpr std::uint32_t group = 512; | ||
| 86 | for (std::uint32_t first = 0; first < count;) { | ||
| 87 | std::vector<VkImage> made; | ||
| 88 | std::uint32_t in_group = 0; | ||
| 89 | for (; in_group < group && first < count; ++in_group) { | ||
| 90 | VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; | ||
| 91 | info.imageType = volume ? VK_IMAGE_TYPE_3D | ||
| 92 | : dimension == ImageDimension::dim_1d ? VK_IMAGE_TYPE_1D : VK_IMAGE_TYPE_2D; | ||
| 93 | info.format = VK_FORMAT_R32_SFLOAT; | ||
| 94 | info.extent = {1, 1, 1}; | ||
| 95 | info.mipLevels = 1; | ||
| 96 | info.arrayLayers = std::min(per_image, count - first); | ||
| 97 | info.samples = VK_SAMPLE_COUNT_1_BIT; | ||
| 98 | info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; | ||
| 99 | VkImage image = VK_NULL_HANDLE; | ||
| 100 | if (d.fn<PFN_vkCreateImage>("vkCreateImage")(d.device, &info, nullptr, &image) != VK_SUCCESS) | ||
| 101 | return false; | ||
| 102 | images.push_back(image); | ||
| 103 | made.push_back(image); | ||
| 104 | for (std::uint32_t layer = 0; layer < info.arrayLayers; ++layer) texels.push_back({image, layer}); | ||
| 105 | first += info.arrayLayers; | ||
| 106 | } | ||
| 107 | VkMemoryRequirements requirements{}; | ||
| 108 | d.fn<PFN_vkGetImageMemoryRequirements>("vkGetImageMemoryRequirements")(d.device, made[0], &requirements); | ||
| 109 | const VkDeviceSize stride = | ||
| 110 | (requirements.size + requirements.alignment - 1) / requirements.alignment * requirements.alignment; | ||
| 111 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 112 | allocation.allocationSize = stride * made.size(); | ||
| 113 | allocation.memoryTypeIndex = 0xffffffffu; | ||
| 114 | for (std::uint32_t t = 0; t < properties.memoryTypeCount; ++t) | ||
| 115 | if (requirements.memoryTypeBits & (1u << t)) { | ||
| 116 | allocation.memoryTypeIndex = t; | ||
| 117 | break; | ||
| 118 | } | ||
| 119 | VkDeviceMemory block = VK_NULL_HANDLE; | ||
| 120 | if (allocation.memoryTypeIndex == 0xffffffffu || | ||
| 121 | d.fn<PFN_vkAllocateMemory>("vkAllocateMemory")(d.device, &allocation, nullptr, &block) != VK_SUCCESS) | ||
| 122 | return false; | ||
| 123 | memory.push_back(block); | ||
| 124 | for (std::size_t k = 0; k < made.size(); ++k) { | ||
| 125 | VkMemoryRequirements each{}; | ||
| 126 | d.fn<PFN_vkGetImageMemoryRequirements>("vkGetImageMemoryRequirements")(d.device, made[k], &each); | ||
| 127 | if (each.size > stride || !(each.memoryTypeBits & (1u << allocation.memoryTypeIndex)) || | ||
| 128 | d.fn<PFN_vkBindImageMemory>("vkBindImageMemory")(d.device, made[k], block, stride * k) != | ||
| 129 | VK_SUCCESS) | ||
| 130 | return false; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | for (const Texel& texel : texels) { | ||
| 134 | VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; | ||
| 135 | info.image = texel.image; | ||
| 136 | info.viewType = volume ? VK_IMAGE_VIEW_TYPE_3D | ||
| 137 | : dimension == ImageDimension::dim_1d ? VK_IMAGE_VIEW_TYPE_1D : VK_IMAGE_VIEW_TYPE_2D_ARRAY; | ||
| 138 | info.format = VK_FORMAT_R32_SFLOAT; | ||
| 139 | info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, texel.layer, 1}; | ||
| 140 | VkImageView view = VK_NULL_HANDLE; | ||
| 141 | if (d.fn<PFN_vkCreateImageView>("vkCreateImageView")(d.device, &info, nullptr, &view) != VK_SUCCESS) | ||
| 142 | return false; | ||
| 143 | views.push_back(view); | ||
| 144 | } | ||
| 145 | return true; | ||
| 146 | } | ||
| 147 | |||
| 148 | // Texel i holds i + 1, in GENERAL (the layout the set's descriptors name). | ||
| 149 | void record_fill(VkCommandBuffer cb) const { | ||
| 150 | std::vector<VkImageMemoryBarrier> barriers; | ||
| 151 | for (const VkImage image : images) { | ||
| 152 | VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; | ||
| 153 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 154 | barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; | ||
| 155 | barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL; | ||
| 156 | barrier.srcQueueFamilyIndex = barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 157 | barrier.image = image; | ||
| 158 | barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, VK_REMAINING_ARRAY_LAYERS}; | ||
| 159 | barriers.push_back(barrier); | ||
| 160 | } | ||
| 161 | d.fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, | ||
| 162 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, | ||
| 163 | nullptr, std::uint32_t(barriers.size()), barriers.data()); | ||
| 164 | for (std::size_t i = 0; i < texels.size(); ++i) { | ||
| 165 | VkClearColorValue value{}; | ||
| 166 | value.float32[0] = float(i + 1); | ||
| 167 | const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, texels[i].layer, 1}; | ||
| 168 | d.fn<PFN_vkCmdClearColorImage>("vkCmdClearColorImage")(cb, texels[i].image, VK_IMAGE_LAYOUT_GENERAL, | ||
| 169 | &value, 1, &range); | ||
| 170 | } | ||
| 171 | VkMemoryBarrier filled{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 172 | filled.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 173 | filled.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; | ||
| 174 | d.fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, | ||
| 175 | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, &filled, 0, | ||
| 176 | nullptr, 0, nullptr); | ||
| 177 | } | ||
| 178 | }; | ||
| 179 | |||
| 180 | // The compute pipeline over set 0 (the bindless set's layout) and set 1 (result and indices). | ||
| 181 | struct Reader { | ||
| 182 | vktest::Device& d; | ||
| 183 | VkShaderModule module = VK_NULL_HANDLE; | ||
| 184 | VkDescriptorSetLayout set1_layout = VK_NULL_HANDLE; | ||
| 185 | VkDescriptorPool pool = VK_NULL_HANDLE; | ||
| 186 | VkDescriptorSet set1 = VK_NULL_HANDLE; | ||
| 187 | VkPipelineLayout layout = VK_NULL_HANDLE; | ||
| 188 | VkPipeline pipeline = VK_NULL_HANDLE; | ||
| 189 | void* result_map = nullptr; | ||
| 190 | void* index_map = nullptr; | ||
| 191 | |||
| 192 | explicit Reader(vktest::Device& device) : d(device) {} | ||
| 193 | Reader(const Reader&) = delete; | ||
| 194 | Reader& operator=(const Reader&) = delete; | ||
| 195 | ~Reader() { | ||
| 196 | const VkDevice dev = d.device; | ||
| 197 | if (pipeline) d.fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(dev, pipeline, nullptr); | ||
| 198 | if (layout) d.fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(dev, layout, nullptr); | ||
| 199 | if (pool) d.fn<PFN_vkDestroyDescriptorPool>("vkDestroyDescriptorPool")(dev, pool, nullptr); | ||
| 200 | if (set1_layout) d.fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(dev, set1_layout, nullptr); | ||
| 201 | if (module) d.fn<PFN_vkDestroyShaderModule>("vkDestroyShaderModule")(dev, module, nullptr); | ||
| 202 | } | ||
| 203 | |||
| 204 | bool make(VkDescriptorSetLayout set0_layout, const std::vector<std::uint32_t>& code) { | ||
| 205 | const VkDevice dev = d.device; | ||
| 206 | VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; | ||
| 207 | module_info.codeSize = code.size() * 4; | ||
| 208 | module_info.pCode = code.data(); | ||
| 209 | if (d.fn<PFN_vkCreateShaderModule>("vkCreateShaderModule")(dev, &module_info, nullptr, &module) != VK_SUCCESS) | ||
| 210 | return false; | ||
| 211 | const VkBuffer result = d.buffer(live_images * 4, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &result_map); | ||
| 212 | const VkBuffer indices = d.buffer(live_images * 4, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &index_map); | ||
| 213 | if (!result || !indices) return false; | ||
| 214 | const VkDescriptorSetLayoutBinding bindings[2] = { | ||
| 215 | {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}, | ||
| 216 | {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}}; | ||
| 217 | VkDescriptorSetLayoutCreateInfo set1_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; | ||
| 218 | set1_info.bindingCount = 2; | ||
| 219 | set1_info.pBindings = bindings; | ||
| 220 | if (d.fn<PFN_vkCreateDescriptorSetLayout>("vkCreateDescriptorSetLayout")(dev, &set1_info, nullptr, | ||
| 221 | &set1_layout) != VK_SUCCESS) | ||
| 222 | return false; | ||
| 223 | const VkDescriptorPoolSize pool_size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}; | ||
| 224 | VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; | ||
| 225 | pool_info.maxSets = 1; | ||
| 226 | pool_info.poolSizeCount = 1; | ||
| 227 | pool_info.pPoolSizes = &pool_size; | ||
| 228 | if (d.fn<PFN_vkCreateDescriptorPool>("vkCreateDescriptorPool")(dev, &pool_info, nullptr, &pool) != VK_SUCCESS) | ||
| 229 | return false; | ||
| 230 | VkDescriptorSetAllocateInfo allocate{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; | ||
| 231 | allocate.descriptorPool = pool; | ||
| 232 | allocate.descriptorSetCount = 1; | ||
| 233 | allocate.pSetLayouts = &set1_layout; | ||
| 234 | if (d.fn<PFN_vkAllocateDescriptorSets>("vkAllocateDescriptorSets")(dev, &allocate, &set1) != VK_SUCCESS) | ||
| 235 | return false; | ||
| 236 | const VkDescriptorBufferInfo infos[2] = {{result, 0, VK_WHOLE_SIZE}, {indices, 0, VK_WHOLE_SIZE}}; | ||
| 237 | VkWriteDescriptorSet writes[2]{}; | ||
| 238 | for (std::uint32_t b = 0; b < 2; ++b) { | ||
| 239 | writes[b] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 240 | writes[b].dstSet = set1; | ||
| 241 | writes[b].dstBinding = b; | ||
| 242 | writes[b].descriptorCount = 1; | ||
| 243 | writes[b].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; | ||
| 244 | writes[b].pBufferInfo = &infos[b]; | ||
| 245 | } | ||
| 246 | d.fn<PFN_vkUpdateDescriptorSets>("vkUpdateDescriptorSets")(dev, 2, writes, 0, nullptr); | ||
| 247 | const VkDescriptorSetLayout layouts[2] = {set0_layout, set1_layout}; | ||
| 248 | const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, 8}; | ||
| 249 | VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; | ||
| 250 | layout_info.setLayoutCount = 2; | ||
| 251 | layout_info.pSetLayouts = layouts; | ||
| 252 | layout_info.pushConstantRangeCount = 1; | ||
| 253 | layout_info.pPushConstantRanges = &push; | ||
| 254 | if (d.fn<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(dev, &layout_info, nullptr, &layout) != | ||
| 255 | VK_SUCCESS) | ||
| 256 | return false; | ||
| 257 | VkComputePipelineCreateInfo pipeline_info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; | ||
| 258 | pipeline_info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO}; | ||
| 259 | pipeline_info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; | ||
| 260 | pipeline_info.stage.module = module; | ||
| 261 | pipeline_info.stage.pName = "main"; | ||
| 262 | pipeline_info.layout = layout; | ||
| 263 | return d.fn<PFN_vkCreateComputePipelines>("vkCreateComputePipelines")(dev, VK_NULL_HANDLE, 1, &pipeline_info, | ||
| 264 | nullptr, &pipeline) == VK_SUCCESS; | ||
| 265 | } | ||
| 266 | |||
| 267 | void record(VkCommandBuffer cb, VkDescriptorSet set0, std::uint32_t dimension, std::uint32_t count) const { | ||
| 268 | d.fn<PFN_vkCmdBindPipeline>("vkCmdBindPipeline")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); | ||
| 269 | const VkDescriptorSet sets[2] = {set0, set1}; | ||
| 270 | d.fn<PFN_vkCmdBindDescriptorSets>("vkCmdBindDescriptorSets")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout, 0, 2, | ||
| 271 | sets, 0, nullptr); | ||
| 272 | const std::uint32_t pass[2] = {dimension, count}; | ||
| 273 | d.fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb, layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 8, pass); | ||
| 274 | d.fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb, count, 1, 1); | ||
| 275 | VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 276 | host.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; | ||
| 277 | host.dstAccessMask = VK_ACCESS_HOST_READ_BIT; | ||
| 278 | d.fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, | ||
| 279 | VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &host, 0, nullptr, 0, | ||
| 280 | nullptr); | ||
| 281 | } | ||
| 282 | }; | ||
| 283 | |||
| 284 | int run(vktest::Device& d, const VkPhysicalDeviceMemoryProperties& memory, const BindlessFeatures& features, | ||
| 285 | std::uint32_t max_layers, const std::vector<std::uint32_t>& code, ImageDimension dimension) { | ||
| 286 | BindlessSet set(d.device_proc, d.device, memory, features); | ||
| 287 | EXPECT(set.ok()); | ||
| 288 | const std::uint32_t capacity = set.image_capacity(dimension); | ||
| 289 | std::printf("%s: %u indices in the set\n", name(dimension), capacity); | ||
| 290 | // Every live image needs an index, and index 0 is the fallback. | ||
| 291 | EXPECT(capacity > live_images); | ||
| 292 | |||
| 293 | Images images(d); | ||
| 294 | EXPECT(images.make(dimension, live_images, max_layers, memory)); | ||
| 295 | std::vector<std::uint32_t> indices; | ||
| 296 | for (const VkImageView view : images.views) { | ||
| 297 | const std::uint32_t index = set.add_image(dimension, view, VK_IMAGE_LAYOUT_GENERAL); | ||
| 298 | EXPECT(index != 0); | ||
| 299 | indices.push_back(index); | ||
| 300 | } | ||
| 301 | Reader reader(d); | ||
| 302 | EXPECT(reader.make(set.layout(), code)); | ||
| 303 | std::memcpy(reader.index_map, indices.data(), indices.size() * 4); | ||
| 304 | std::memset(reader.result_map, 0xcd, live_images * 4); | ||
| 305 | const std::uint32_t pass = dimension == ImageDimension::dim_1d ? 0 : dimension == ImageDimension::dim_2d_array ? 1 : 2; | ||
| 306 | bool flushed = false; | ||
| 307 | const bool ran = d.run([&](VkCommandBuffer cb) { | ||
| 308 | set.record_fallback_init(cb); | ||
| 309 | flushed = set.flush(cb, 0, [] { return std::uint64_t(0); }); | ||
| 310 | images.record_fill(cb); | ||
| 311 | reader.record(cb, set.set(), pass, live_images); | ||
| 312 | return true; | ||
| 313 | }); | ||
| 314 | set.table_upload_result(ran); | ||
| 315 | EXPECT(ran && flushed); | ||
| 316 | std::vector<std::uint32_t> result(live_images); | ||
| 317 | std::memcpy(result.data(), reader.result_map, live_images * 4); | ||
| 318 | std::uint32_t wrong = 0; | ||
| 319 | for (std::uint32_t i = 0; i < live_images; ++i) { | ||
| 320 | float red = 0; | ||
| 321 | std::memcpy(&red, &result[i], 4); | ||
| 322 | if (red != float(i + 1) && wrong++ < 4) | ||
| 323 | std::fprintf(stderr, "%s image %u at index %u read %g, expected %u\n", name(dimension), i, indices[i], | ||
| 324 | double(red), i + 1); | ||
| 325 | } | ||
| 326 | EXPECT(wrong == 0); | ||
| 327 | std::printf("%s: %u live images, each read its own texel through its index (the last at %u)\n", name(dimension), | ||
| 328 | live_images, indices.back()); | ||
| 329 | |||
| 330 | // The rest of the array (the same view again: an index each, as distinct T#s of one texture get), then one | ||
| 331 | // more, refused for the reason that the array of its dimension is full. | ||
| 332 | std::uint32_t more = 0; | ||
| 333 | while (set.add_image(dimension, images.views[0], VK_IMAGE_LAYOUT_GENERAL)) ++more; | ||
| 334 | EXPECT(live_images + more == capacity - 1 && set.free_images(dimension) == 0); | ||
| 335 | EXPECT(set.image_refusal(dimension, false) == BindlessSet::ImageRefusal::array_full); | ||
| 336 | // Freed with nothing pending in the table: the index is free again at once. | ||
| 337 | set.remove_image(dimension, indices[7]); | ||
| 338 | EXPECT(set.free_images(dimension) == 1); | ||
| 339 | EXPECT(set.add_image(dimension, images.views[7], VK_IMAGE_LAYOUT_GENERAL) == indices[7]); | ||
| 340 | // Freed after its key is erased (the GPU's table may still name it): held until a flush brings the table up to | ||
| 341 | // date, then free. | ||
| 342 | const DescriptorKey key{0x1234u, 0, 0, 8u << 28, 0, 0, 0, 0}; | ||
| 343 | set.insert_key(key, indices[9], dimension); | ||
| 344 | set.erase_key(key); | ||
| 345 | set.remove_image(dimension, indices[9]); | ||
| 346 | EXPECT(set.free_images(dimension) == 0 && set.add_image(dimension, images.views[9], VK_IMAGE_LAYOUT_GENERAL) == 0); | ||
| 347 | EXPECT(set.image_refusal(dimension, false) == BindlessSet::ImageRefusal::array_full); | ||
| 348 | const bool reflushed = d.run([&](VkCommandBuffer cb) { return set.flush(cb, 1, [] { return std::uint64_t(1); }); }); | ||
| 349 | set.table_upload_result(reflushed); | ||
| 350 | EXPECT(reflushed && set.free_images(dimension) == 1); | ||
| 351 | EXPECT(set.add_image(dimension, images.views[9], VK_IMAGE_LAYOUT_GENERAL) == indices[9]); | ||
| 352 | std::printf("%s: %u more filled the array; the next was refused as full, and removed indices came back\n", | ||
| 353 | name(dimension), more); | ||
| 354 | |||
| 355 | d.fn<PFN_vkDeviceWaitIdle>("vkDeviceWaitIdle")(d.device); | ||
| 356 | set.release_retired(); | ||
| 357 | return 0; | ||
| 358 | } | ||
| 359 | |||
| 360 | } // namespace | ||
| 361 | |||
| 362 | int main() { | ||
| 363 | #ifndef LEONIDA_TEST_BINDLESS_DIMENSIONS_SPV | ||
| 364 | std::printf("unsupported: the test shader was not built (no glslangValidator)\n"); | ||
| 365 | return 77; | ||
| 366 | #else | ||
| 367 | vktest::Device d; | ||
| 368 | if (const auto status = d.initialize(); status != vktest::DeviceStatus::ready) | ||
| 369 | return status == vktest::DeviceStatus::unavailable ? 77 : 1; | ||
| 370 | if (!d.bindless) { | ||
| 371 | std::printf("unsupported: the device lacks the bindless set's descriptor-indexing features\n"); | ||
| 372 | return 77; | ||
| 373 | } | ||
| 374 | VkPhysicalDeviceMemoryProperties memory{}; | ||
| 375 | d.instance_fn<PFN_vkGetPhysicalDeviceMemoryProperties>("vkGetPhysicalDeviceMemoryProperties")(d.physical, &memory); | ||
| 376 | VkPhysicalDeviceVulkan12Properties v12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES}; | ||
| 377 | VkPhysicalDeviceVulkan11Properties v11{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES, &v12}; | ||
| 378 | VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &v11}; | ||
| 379 | d.instance_fn<PFN_vkGetPhysicalDeviceProperties2>("vkGetPhysicalDeviceProperties2")(d.physical, &properties); | ||
| 380 | std::printf("device: %s\n", properties.properties.deviceName); | ||
| 381 | |||
| 382 | // Sized as the D3D12 device sizes it (d3d12_vulkan_device.cpp): from the update-after-bind limits, past | ||
| 383 | // maxPerSetDescriptors where vkGetDescriptorSetLayoutSupport accepts the layout. | ||
| 384 | BindlessFeatures features; | ||
| 385 | features.update_after_bind = true; | ||
| 386 | features.partially_bound = d.partially_bound; | ||
| 387 | const auto layout_support = | ||
| 388 | d.fn<PFN_vkGetDescriptorSetLayoutSupport>("vkGetDescriptorSetLayoutSupport"); | ||
| 389 | const auto layout_supported = [&](const BindlessFeatures& candidate) { | ||
| 390 | if (!layout_support) return false; | ||
| 391 | const bindless::SetLayout layout(candidate); | ||
| 392 | VkDescriptorSetLayoutSupport support{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_SUPPORT}; | ||
| 393 | layout_support(d.device, &layout.info, &support); | ||
| 394 | return support.supported == VK_TRUE; | ||
| 395 | }; | ||
| 396 | bindless::size_bindings(features, properties.properties.limits, v11, v12, layout_supported); | ||
| 397 | const std::uint32_t sampled_limit = std::min(v12.maxPerStageDescriptorUpdateAfterBindSampledImages, | ||
| 398 | v12.maxDescriptorSetUpdateAfterBindSampledImages); | ||
| 399 | // Room for 5000 images in each of the four sampled arrays besides 2D's: below that, the device itself holds | ||
| 400 | // fewer, and the test has nothing to say. | ||
| 401 | if (sampled_limit < bindless::max_images + 3 * (live_images + 1) + 2 * bindless::growth_headroom) { | ||
| 402 | std::printf("unsupported: the device holds %u sampled images in a set, too few for %u of each dimension\n", | ||
| 403 | sampled_limit, live_images); | ||
| 404 | return 77; | ||
| 405 | } | ||
| 406 | |||
| 407 | const std::vector<std::uint32_t> code = load_spirv(LEONIDA_TEST_BINDLESS_DIMENSIONS_SPV); | ||
| 408 | EXPECT(!code.empty()); | ||
| 409 | const std::uint32_t max_layers = properties.properties.limits.maxImageArrayLayers; | ||
| 410 | for (const ImageDimension dimension : | ||
| 411 | {ImageDimension::dim_1d, ImageDimension::dim_2d_array, ImageDimension::dim_3d}) | ||
| 412 | EXPECT(run(d, memory, features, max_layers, code, dimension) == 0); | ||
| 413 | |||
| 414 | if (d.validation_errors()) { | ||
| 415 | std::printf("%u validation errors\n", d.validation_errors()); | ||
| 416 | return 1; | ||
| 417 | } | ||
| 418 | std::printf("vk_bindless_dimensions: ok\n"); | ||
| 419 | return 0; | ||
| 420 | #endif | ||
| 421 | } | ||