// Null descriptors of every kind through the bindless set 0 (src/vulkan_bindless.h), at the Vulkan level. // // On the console a null descriptor (a zero T#, or a T# whose image the layer refused or never saw) reads zeros and // drops stores and atomics. Translated shaders find their image by hashing the T# (a miss is index 0) and read it // from the binding of the instruction's own dimension, so index 0 of each binding must be a fallback of that // binding's kind, and nothing written through a null UAV may show in a later read through a null SRV. // test/glsl/cs_null_descriptors.comp looks up a zero T#, T#s of a 3D, a cube and a 1D image that no image answers, // a zero S#, and the T# of one real 2D image, then: // pass 0: stores through the storage fallback of every dimension and component class, and adds to both r32ui // fallbacks (2D and 3D); // pass 1: reads every sampled fallback (fetched and sampled, the cube through the 2D-array binding at layer 3) and // every storage fallback, and adds to the r32ui fallbacks again. // Every key but the real one must find index 0, every sampled read must be zero after the stores, and the real image // must read its clear colour (so the reads are not vacuous). Twice: with BindlessFeatures::null_descriptor (where the // device has VK_EXT_robustness2 nullDescriptor), where the stores and atomics are dropped (storage reads and atomic // results zero too); and without it, where the storage fallbacks are images of their own that keep the stores, // apart from the sampled ones. // Exit 77 without a Vulkan device with the bindless features, or without the shader (no glslangValidator). // 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) // Everything but main needs the shader glslangValidator builds; without it CMake leaves the path undefined and // main reports the test unsupported, so none of this may name it. #ifdef LEONIDA_TEST_NULL_DESCRIPTORS_SPV namespace { using namespace leonida; constexpr std::uint32_t result_words = 128; // T# word 3 bits [31:28] hold the image TYPE (gfx10_descriptors.h): 8 1D, 9 2D, 10 3D, 11 cube. DescriptorKey tsharp_of_type(std::uint32_t type, std::uint32_t salt) { return {0x00001000u + salt, 0x00000000u, 0x00000000u, (type << 28) | 0x00000fac, 0, 0, 0, 0}; } 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; } // One run of the scenario on a new set with `null_descriptor`; the result words in `out`. int run(vktest::Device& d, const VkPhysicalDeviceMemoryProperties& memory, bool null_descriptor, std::array& out) { const VkDevice dev = d.device; BindlessFeatures features; features.update_after_bind = true; features.partially_bound = d.partially_bound; features.null_descriptor = null_descriptor; BindlessSet set(d.device_proc, dev, memory, features); EXPECT(set.ok()); // The real 2D image: 1x1 RGBA8, cleared to a colour in the command buffer below. const VkImage image = d.image(VK_FORMAT_R8G8B8A8_UNORM, 1, 1, 1, VK_IMAGE_USAGE_SAMPLED_BIT); EXPECT(image); VkImageViewCreateInfo view_info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; view_info.image = image; view_info.viewType = VK_IMAGE_VIEW_TYPE_2D; view_info.format = VK_FORMAT_R8G8B8A8_UNORM; view_info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; VkImageView view = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateImageView")(dev, &view_info, nullptr, &view) == VK_SUCCESS); const DescriptorKey real_key = tsharp_of_type(9, 0x40); const std::uint32_t real_index = set.add_image(ImageDimension::dim_2d, view, VK_IMAGE_LAYOUT_GENERAL); EXPECT(real_index != 0); set.insert_key(real_key, real_index); const std::array keys{DescriptorKey{}, tsharp_of_type(10, 0x10), tsharp_of_type(11, 0x20), tsharp_of_type(8, 0x30), real_key, make_ssharp_key(std::array{}.data())}; void* result_map = nullptr; void* key_map = nullptr; const VkBuffer result = d.buffer(result_words * 4, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &result_map); const VkBuffer key_buffer = d.buffer(sizeof(keys), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &key_map); EXPECT(result && key_buffer); std::memset(result_map, 0xcd, result_words * 4); std::memcpy(key_map, keys.data(), sizeof(keys)); const std::vector code = load_spirv(LEONIDA_TEST_NULL_DESCRIPTORS_SPV); EXPECT(!code.empty()); VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; module_info.codeSize = code.size() * 4; module_info.pCode = code.data(); VkShaderModule module = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateShaderModule")(dev, &module_info, nullptr, &module) == VK_SUCCESS); const std::array bindings{{ {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 = std::uint32_t(bindings.size()); set1_info.pBindings = bindings.data(); VkDescriptorSetLayout set1_layout = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateDescriptorSetLayout")(dev, &set1_info, nullptr, &set1_layout) == VK_SUCCESS); 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; VkDescriptorPool pool = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateDescriptorPool")(dev, &pool_info, nullptr, &pool) == VK_SUCCESS); VkDescriptorSetAllocateInfo allocate{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; allocate.descriptorPool = pool; allocate.descriptorSetCount = 1; allocate.pSetLayouts = &set1_layout; VkDescriptorSet set1 = VK_NULL_HANDLE; EXPECT(d.fn("vkAllocateDescriptorSets")(dev, &allocate, &set1) == VK_SUCCESS); const VkDescriptorBufferInfo buffer_infos[2] = {{result, 0, VK_WHOLE_SIZE}, {key_buffer, 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 = &buffer_infos[b]; } d.fn("vkUpdateDescriptorSets")(dev, 2, writes, 0, nullptr); const VkDescriptorSetLayout layouts[2] = {set.layout(), set1_layout}; const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, 4}; 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; VkPipelineLayout layout = VK_NULL_HANDLE; EXPECT(d.fn("vkCreatePipelineLayout")(dev, &layout_info, nullptr, &layout) == VK_SUCCESS); 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; VkPipeline pipeline = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateComputePipelines")(dev, VK_NULL_HANDLE, 1, &pipeline_info, nullptr, &pipeline) == VK_SUCCESS); 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); }); VkImageMemoryBarrier to_general{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; to_general.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; to_general.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; to_general.newLayout = VK_IMAGE_LAYOUT_GENERAL; to_general.srcQueueFamilyIndex = to_general.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; to_general.image = image; to_general.subresourceRange = view_info.subresourceRange; d.fn("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &to_general); const VkClearColorValue colour{{0.5f, 0.25f, 0.75f, 1.0f}}; d.fn("vkCmdClearColorImage")(cb, image, VK_IMAGE_LAYOUT_GENERAL, &colour, 1, &view_info.subresourceRange); VkMemoryBarrier cleared{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; cleared.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; cleared.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; d.fn("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, &cleared, 0, nullptr, 0, nullptr); d.fn("vkCmdBindPipeline")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); const VkDescriptorSet sets[2] = {set.set(), set1}; d.fn("vkCmdBindDescriptorSets")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout, 0, 2, sets, 0, nullptr); for (std::uint32_t mode = 0; mode < 2; ++mode) { d.fn("vkCmdPushConstants")(cb, layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 4, &mode); d.fn("vkCmdDispatch")(cb, 1, 1, 1); VkMemoryBarrier between{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; between.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; between.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_HOST_READ_BIT; d.fn("vkCmdPipelineBarrier")( cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &between, 0, nullptr, 0, nullptr); } return true; }); set.table_upload_result(ran); EXPECT(ran && flushed); std::memcpy(out.data(), result_map, sizeof(out)); d.fn("vkDestroyPipeline")(dev, pipeline, nullptr); d.fn("vkDestroyPipelineLayout")(dev, layout, nullptr); d.fn("vkDestroyDescriptorPool")(dev, pool, nullptr); d.fn("vkDestroyDescriptorSetLayout")(dev, set1_layout, nullptr); d.fn("vkDestroyShaderModule")(dev, module, nullptr); set.release_retired(); d.fn("vkDestroyImageView")(dev, view, nullptr); return 0; } void print(const char* what, const std::array& r, std::uint32_t first, std::uint32_t count) { std::printf(" %-28s", what); for (std::uint32_t i = 0; i < count; ++i) std::printf(" %08x", r[first + i]); std::printf("\n"); } // The checks every mode shares: the keys' indices, the sampled fallbacks and the real image. int check_common(const std::array& r) { print("indices (0 3D cube 1D real S#)", r, 0, 6); print("fetch 2D / 2D array", r, 16, 8); print("fetch 3D / 1D", r, 24, 8); print("fetch real 2D", r, 32, 4); print("sample 2D / cube", r, 36, 8); print("sample 3D / 1D", r, 44, 8); print("storage 2D f u i", r, 64, 12); print("storage 2D array f u i", r, 76, 12); print("storage 3D f u i", r, 88, 12); print("storage 1D f u i", r, 100, 12); print("atomics pass 0 / pass 1", r, 8, 2); print("", r, 112, 2); // Every null key finds index 0, the fallback of the binding the instruction picks; the real one its image. EXPECT(r[0] == 0 && r[1] == 0 && r[2] == 0 && r[3] == 0 && r[5] == 0); EXPECT(r[4] != 0); // Every sampled fallback reads zeros, though pass 0 stored and added through every storage fallback. for (std::uint32_t i = 16; i < 32; ++i) EXPECT(r[i] == 0); for (std::uint32_t i = 36; i < 52; ++i) EXPECT(r[i] == 0); // The real image reads its clear colour (0.5, 0.25, 0.75, 1) as RGBA8: the shader does reach images. EXPECT(r[32] != 0 && r[35] == 0x3f800000u); return 0; } // Binding 29 (2D r64ui storage images, VK_EXT_shader_image_atomic_int64): verifies a 2D R32G32_UINT image aliased // with VK_FORMAT_R64_UINT receives 64-bit OpAtomicUMax through set 0 binding 29 and that slot 0 behaves as a null // descriptor (when null_descriptor is true) or an isolated 1x1 R64_UINT fallback image (when false). int run_r64ui(vktest::Device& d, const VkPhysicalDeviceMemoryProperties& memory, bool null_descriptor) { // Compiled with spirv-as/spirv-val --target-env vulkan1.3: // layout(set=0, binding=29, r64ui) uniform u64image2D atomics_64[]; // layout(set=1, binding=0, std430) buffer Out { uint64_t old_val; }; // layout(push_constant) uniform PC { uint idx; uint lo; uint hi; } pc; // old_val = imageAtomicMax(atomics_64[nonuniformEXT(pc.idx)], ivec2(0), (uint64_t(pc.hi)<<32)|uint64_t(pc.lo)); static constexpr std::uint32_t kR64AtomicSpv[] = { 0x07230203u, 0x00010600u, 0x00070000u, 0x0000002cu, 0x00000000u, 0x00020011u, 0x00000001u, 0x00020011u, 0x0000000bu, 0x00020011u, 0x0000000cu, 0x00020011u, 0x00001398u, 0x00020011u, 0x000014b6u, 0x00020011u, 0x0000001fu, 0x0008000au, 0x5f565053u, 0x5f545845u, 0x64616873u, 0x695f7265u, 0x6567616du, 0x746e695fu, 0x00003436u, 0x0003000eu, 0x00000000u, 0x00000001u, 0x0008000fu, 0x00000005u, 0x00000001u, 0x6e69616du, 0x00000000u, 0x00000002u, 0x00000003u, 0x00000004u, 0x00060010u, 0x00000001u, 0x00000011u, 0x00000001u, 0x00000001u, 0x00000001u, 0x00030047u, 0x00000005u, 0x00000002u, 0x00050048u, 0x00000005u, 0x00000000u, 0x00000023u, 0x00000000u, 0x00050048u, 0x00000005u, 0x00000001u, 0x00000023u, 0x00000004u, 0x00050048u, 0x00000005u, 0x00000002u, 0x00000023u, 0x00000008u, 0x00030047u, 0x00000006u, 0x00000002u, 0x00050048u, 0x00000006u, 0x00000000u, 0x00000023u, 0x00000000u, 0x00040047u, 0x00000003u, 0x00000022u, 0x00000001u, 0x00040047u, 0x00000003u, 0x00000021u, 0x00000000u, 0x00040047u, 0x00000004u, 0x00000022u, 0x00000000u, 0x00040047u, 0x00000004u, 0x00000021u, 0x0000001du, 0x00020013u, 0x00000007u, 0x00030021u, 0x00000008u, 0x00000007u, 0x00040015u, 0x00000009u, 0x00000020u, 0x00000000u, 0x00040015u, 0x0000000au, 0x00000020u, 0x00000001u, 0x00040015u, 0x0000000bu, 0x00000040u, 0x00000000u, 0x0005001eu, 0x00000005u, 0x00000009u, 0x00000009u, 0x00000009u, 0x00040020u, 0x0000000cu, 0x00000009u, 0x00000005u, 0x0004003bu, 0x0000000cu, 0x00000002u, 0x00000009u, 0x00040020u, 0x0000000du, 0x00000009u, 0x00000009u, 0x0003001eu, 0x00000006u, 0x0000000bu, 0x00040020u, 0x0000000eu, 0x0000000cu, 0x00000006u, 0x0004003bu, 0x0000000eu, 0x00000003u, 0x0000000cu, 0x00040020u, 0x0000000fu, 0x0000000cu, 0x0000000bu, 0x00090019u, 0x00000010u, 0x0000000bu, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000002u, 0x00000028u, 0x0003001du, 0x00000011u, 0x00000010u, 0x00040020u, 0x00000012u, 0x00000000u, 0x00000011u, 0x0004003bu, 0x00000012u, 0x00000004u, 0x00000000u, 0x00040020u, 0x00000013u, 0x00000000u, 0x00000010u, 0x00040020u, 0x00000014u, 0x0000000bu, 0x0000000bu, 0x00040017u, 0x00000015u, 0x0000000au, 0x00000002u, 0x0004002bu, 0x0000000au, 0x00000016u, 0x00000000u, 0x0004002bu, 0x0000000au, 0x00000017u, 0x00000001u, 0x0004002bu, 0x0000000au, 0x00000018u, 0x00000002u, 0x0004002bu, 0x00000009u, 0x00000019u, 0x00000000u, 0x0004002bu, 0x00000009u, 0x0000001au, 0x00000001u, 0x0004002bu, 0x00000009u, 0x0000001bu, 0x00000020u, 0x0005002cu, 0x00000015u, 0x0000001cu, 0x00000016u, 0x00000016u, 0x00050036u, 0x00000007u, 0x00000001u, 0x00000000u, 0x00000008u, 0x000200f8u, 0x0000001du, 0x00050041u, 0x0000000du, 0x0000001eu, 0x00000002u, 0x00000016u, 0x0004003du, 0x00000009u, 0x0000001fu, 0x0000001eu, 0x00050041u, 0x0000000du, 0x00000020u, 0x00000002u, 0x00000017u, 0x0004003du, 0x00000009u, 0x00000021u, 0x00000020u, 0x00050041u, 0x0000000du, 0x00000022u, 0x00000002u, 0x00000018u, 0x0004003du, 0x00000009u, 0x00000023u, 0x00000022u, 0x00040071u, 0x0000000bu, 0x00000024u, 0x00000021u, 0x00040071u, 0x0000000bu, 0x00000025u, 0x00000023u, 0x000500c4u, 0x0000000bu, 0x00000026u, 0x00000025u, 0x0000001bu, 0x000500c5u, 0x0000000bu, 0x00000027u, 0x00000026u, 0x00000024u, 0x00050041u, 0x00000013u, 0x00000028u, 0x00000004u, 0x0000001fu, 0x0006003cu, 0x00000014u, 0x00000029u, 0x00000028u, 0x0000001cu, 0x00000019u, 0x000700efu, 0x0000000bu, 0x0000002au, 0x00000029u, 0x0000001au, 0x00000019u, 0x00000027u, 0x00050041u, 0x0000000fu, 0x0000002bu, 0x00000003u, 0x00000016u, 0x0003003eu, 0x0000002bu, 0x0000002au, 0x000100fdu, 0x00010038u, }; const VkDevice dev = d.device; BindlessFeatures features; features.update_after_bind = true; features.partially_bound = d.partially_bound; features.null_descriptor = null_descriptor; features.shader_image_int64_atomics = true; BindlessSet set(d.device_proc, dev, memory, features); EXPECT(set.ok()); // Mirror d3d12_vulkan_device.cpp's 2D R32G32_UINT UAV image: mutable with format list {R32G32_UINT, R64_UINT} // and EXTENDED_USAGE_BIT (since R64_UINT supports STORAGE_BIT but not SAMPLED_BIT or COLOR_ATTACHMENT_BIT). const std::array view_formats{VK_FORMAT_R32G32_UINT, VK_FORMAT_R64_UINT}; VkImageFormatListCreateInfo format_list{VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO}; format_list.viewFormatCount = std::uint32_t(view_formats.size()); format_list.pViewFormats = view_formats.data(); VkImageCreateInfo image_info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, &format_list}; image_info.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT; image_info.imageType = VK_IMAGE_TYPE_2D; image_info.format = VK_FORMAT_R32G32_UINT; image_info.extent = {1, 1, 1}; image_info.mipLevels = 1; image_info.arrayLayers = 1; image_info.samples = VK_SAMPLE_COUNT_1_BIT; image_info.tiling = VK_IMAGE_TILING_OPTIMAL; image_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; if (const auto get_fmt_props2 = d.instance_fn( "vkGetPhysicalDeviceImageFormatProperties2")) { VkPhysicalDeviceImageFormatInfo2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2, &format_list}; query.format = image_info.format; query.type = image_info.imageType; query.tiling = image_info.tiling; query.usage = image_info.usage; query.flags = image_info.flags; VkImageFormatProperties2 props{VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2}; EXPECT(get_fmt_props2(d.physical, &query, &props) == VK_SUCCESS); } VkImage image = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateImage")(dev, &image_info, nullptr, &image) == VK_SUCCESS); VkMemoryRequirements req{}; d.fn("vkGetImageMemoryRequirements")(dev, image, &req); VkMemoryAllocateInfo alloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; alloc.allocationSize = req.size; alloc.memoryTypeIndex = 0xffffffffu; for (std::uint32_t i = 0; i < memory.memoryTypeCount; ++i) if (req.memoryTypeBits & (1u << i)) { alloc.memoryTypeIndex = i; break; } EXPECT(alloc.memoryTypeIndex != 0xffffffffu); VkDeviceMemory image_mem = VK_NULL_HANDLE; EXPECT(d.fn("vkAllocateMemory")(dev, &alloc, nullptr, &image_mem) == VK_SUCCESS); EXPECT(d.fn("vkBindImageMemory")(dev, image, image_mem, 0) == VK_SUCCESS); VkImageViewUsageCreateInfo sampled_usage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO}; sampled_usage.usage = VK_IMAGE_USAGE_SAMPLED_BIT; VkImageViewCreateInfo view_info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, &sampled_usage}; view_info.image = image; view_info.viewType = VK_IMAGE_VIEW_TYPE_2D; view_info.format = VK_FORMAT_R32G32_UINT; view_info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; VkImageView sampled_view = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateImageView")(dev, &view_info, nullptr, &sampled_view) == VK_SUCCESS); VkImageViewUsageCreateInfo storage_usage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO}; storage_usage.usage = VK_IMAGE_USAGE_STORAGE_BIT; view_info.pNext = &storage_usage; VkImageView u32x2_view = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateImageView")(dev, &view_info, nullptr, &u32x2_view) == VK_SUCCESS); view_info.format = VK_FORMAT_R64_UINT; VkImageView r64_view = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateImageView")(dev, &view_info, nullptr, &r64_view) == VK_SUCCESS); StorageViews views{}; views.as_uint = u32x2_view; views.r64ui = r64_view; const std::uint32_t real_index = set.add_image(ImageDimension::dim_2d, sampled_view, VK_IMAGE_LAYOUT_GENERAL, views); EXPECT(real_index != 0); void* old_map = nullptr; void* readback_map = nullptr; const VkBuffer old_buf = d.buffer(sizeof(std::uint64_t), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &old_map); const VkBuffer readback = d.buffer(sizeof(std::uint64_t), VK_BUFFER_USAGE_TRANSFER_DST_BIT, &readback_map); EXPECT(old_buf && readback); VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; module_info.codeSize = sizeof(kR64AtomicSpv); module_info.pCode = kR64AtomicSpv; VkShaderModule module = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateShaderModule")(dev, &module_info, nullptr, &module) == VK_SUCCESS); const VkDescriptorSetLayoutBinding b0{0, 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 = 1; set1_info.pBindings = &b0; VkDescriptorSetLayout set1_layout = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateDescriptorSetLayout")(dev, &set1_info, nullptr, &set1_layout) == VK_SUCCESS); const VkDescriptorPoolSize pool_size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}; VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; pool_info.maxSets = 1; pool_info.poolSizeCount = 1; pool_info.pPoolSizes = &pool_size; VkDescriptorPool pool = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateDescriptorPool")(dev, &pool_info, nullptr, &pool) == VK_SUCCESS); VkDescriptorSetAllocateInfo allocate{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; allocate.descriptorPool = pool; allocate.descriptorSetCount = 1; allocate.pSetLayouts = &set1_layout; VkDescriptorSet set1 = VK_NULL_HANDLE; EXPECT(d.fn("vkAllocateDescriptorSets")(dev, &allocate, &set1) == VK_SUCCESS); const VkDescriptorBufferInfo out_info{old_buf, 0, VK_WHOLE_SIZE}; VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; write.dstSet = set1; write.dstBinding = 0; write.descriptorCount = 1; write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; write.pBufferInfo = &out_info; d.fn("vkUpdateDescriptorSets")(dev, 1, &write, 0, nullptr); const VkDescriptorSetLayout layouts[2] = {set.layout(), set1_layout}; const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, 12}; 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; VkPipelineLayout layout = VK_NULL_HANDLE; EXPECT(d.fn("vkCreatePipelineLayout")(dev, &layout_info, nullptr, &layout) == VK_SUCCESS); 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; VkPipeline pipeline = VK_NULL_HANDLE; EXPECT(d.fn("vkCreateComputePipelines")(dev, VK_NULL_HANDLE, 1, &pipeline_info, nullptr, &pipeline) == VK_SUCCESS); const auto dispatch_umax = [&](std::uint32_t idx, std::uint64_t val, bool init, std::uint64_t& old_out) -> bool { *static_cast(old_map) = ~0ull; bool flushed = true; const bool ran = d.run([&](VkCommandBuffer cb) { if (init) { set.record_fallback_init(cb); flushed = set.flush(cb, 0, [] { return std::uint64_t(0); }); VkImageMemoryBarrier to_general{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; to_general.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; to_general.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; to_general.newLayout = VK_IMAGE_LAYOUT_GENERAL; to_general.srcQueueFamilyIndex = to_general.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; to_general.image = image; to_general.subresourceRange = view_info.subresourceRange; d.fn("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &to_general); const VkClearColorValue zero{}; d.fn("vkCmdClearColorImage")(cb, image, VK_IMAGE_LAYOUT_GENERAL, &zero, 1, &view_info.subresourceRange); VkMemoryBarrier cleared{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; cleared.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; cleared.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; d.fn("vkCmdPipelineBarrier")(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, &cleared, 0, nullptr, 0, nullptr); } d.fn("vkCmdBindPipeline")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); const VkDescriptorSet sets[2] = {set.set(), set1}; d.fn("vkCmdBindDescriptorSets")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout, 0, 2, sets, 0, nullptr); const std::uint32_t pc[3] = {idx, std::uint32_t(val), std::uint32_t(val >> 32)}; d.fn("vkCmdPushConstants")(cb, layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), pc); d.fn("vkCmdDispatch")(cb, 1, 1, 1); VkMemoryBarrier done{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; done.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; done.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_HOST_READ_BIT; d.fn("vkCmdPipelineBarrier")( cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &done, 0, nullptr, 0, nullptr); VkBufferImageCopy copy{}; copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; copy.imageExtent = {1, 1, 1}; d.fn("vkCmdCopyImageToBuffer")(cb, image, VK_IMAGE_LAYOUT_GENERAL, readback, 1, ©); return true; }); if (init) set.table_upload_result(ran); old_out = *static_cast(old_map); return ran && flushed; }; std::uint64_t old = 0; EXPECT(dispatch_umax(real_index, 0x000000123456789Aull, true, old)); EXPECT(old == 0 && *static_cast(readback_map) == 0x000000123456789Aull); EXPECT(dispatch_umax(real_index, 0x0000001211111111ull, false, old)); EXPECT(old == 0x000000123456789Aull && *static_cast(readback_map) == 0x000000123456789Aull); EXPECT(dispatch_umax(real_index, 0x0000001300000001ull, false, old)); EXPECT(old == 0x000000123456789Aull && *static_cast(readback_map) == 0x0000001300000001ull); // Fallback slot 0: null descriptor drops atomics (always returns 0); 1x1 fallback image keeps the max. EXPECT(dispatch_umax(0, 0x000000FF00000005ull, false, old)); EXPECT(old == 0); EXPECT(dispatch_umax(0, 0x000000FF00000007ull, false, old)); EXPECT(old == (null_descriptor ? 0ull : 0x000000FF00000005ull)); d.fn("vkDestroyPipeline")(dev, pipeline, nullptr); d.fn("vkDestroyPipelineLayout")(dev, layout, nullptr); d.fn("vkDestroyDescriptorPool")(dev, pool, nullptr); d.fn("vkDestroyDescriptorSetLayout")(dev, set1_layout, nullptr); d.fn("vkDestroyShaderModule")(dev, module, nullptr); set.release_retired(); d.fn("vkDestroyImageView")(dev, r64_view, nullptr); d.fn("vkDestroyImageView")(dev, u32x2_view, nullptr); d.fn("vkDestroyImageView")(dev, sampled_view, nullptr); d.fn("vkDestroyImage")(dev, image, nullptr); d.fn("vkFreeMemory")(dev, image_mem, nullptr); return 0; } } // namespace #endif // LEONIDA_TEST_NULL_DESCRIPTORS_SPV int main() { #ifndef LEONIDA_TEST_NULL_DESCRIPTORS_SPV std::printf("unsupported: the test shader was not built (no glslangValidator)\n"); return 77; #else vktest::Device d; d.request_null_descriptor = true; d.request_shader_image_int64_atomics = true; 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); std::array r{}; if (d.null_descriptor) { std::printf("null descriptors (VK_EXT_robustness2 nullDescriptor):\n"); EXPECT(run(d, memory, true, r) == 0); EXPECT(check_common(r) == 0); // The stores were dropped: every storage fallback reads zero, and the atomics returned zero both times. for (std::uint32_t i = 64; i < 112; ++i) EXPECT(r[i] == 0); EXPECT(r[8] == 0 && r[9] == 0 && r[112] == 0 && r[113] == 0); } else { std::printf("the device has no VK_EXT_robustness2 nullDescriptor: only the image fallbacks are tested\n"); } std::printf("storage fallback images:\n"); EXPECT(run(d, memory, false, r) == 0); EXPECT(check_common(r) == 0); // The storage fallbacks are images of their own: they took the stores (which the sampled reads above did not // see), and the atomics of pass 1 found what pass 0 left. (The uint class's storage fallback is also the // r32ui one, so the exact values depend on the order of pass 0's store and add, which is not checked.) EXPECT(r[64] != 0 && r[88] != 0 && r[100] != 0); EXPECT(r[112] != 0 && r[113] != 0); if (d.shader_image_int64_atomics) { if (d.null_descriptor) { std::printf("binding 29 r64ui (null descriptor):\n"); EXPECT(run_r64ui(d, memory, true) == 0); } if (d.r64ui_transfer_dst) { std::printf("binding 29 r64ui (1x1 fallback image):\n"); EXPECT(run_r64ui(d, memory, false) == 0); } } else { std::printf("the device has no VK_EXT_shader_image_atomic_int64: binding 29 r64ui skipped\n"); } if (d.validation_errors()) { std::printf("%u validation errors\n", d.validation_errors()); return 1; } std::printf("vk_null_descriptor: ok\n"); return 0; #endif }