diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 7bcfa9ef6..3435c9296 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -4627,6 +4627,16 @@ if(TARGET Vulkan::Headers) target_compile_options(replay_khr_as_copy_postbuild_test PRIVATE /W4 /permissive- /utf-8) endif() add_test(NAME replay_khr_as_copy_postbuild COMMAND replay_khr_as_copy_postbuild_test) + # BindlessSet's kind bits: storage only inside the storage arrays, no sampled bit for a 1D array (fake device). + add_executable(bindless_kind_bits_test test/bindless_kind_bits_test.cpp) + target_link_libraries(bindless_kind_bits_test PRIVATE Vulkan::Headers) + if(WIN32) + target_compile_definitions(bindless_kind_bits_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + endif() + if(MSVC) + target_compile_options(bindless_kind_bits_test PRIVATE /W4 /permissive- /utf-8) + endif() + add_test(NAME bindless_kind_bits COMMAND bindless_kind_bits_test) # PreparedList::wrote_readback and readback_src_stages match the actual producer stage (depth24, rgb96, UAV clear; no GPU). add_executable(replay_readback_stages_test test/replay_readback_stages_test.cpp) target_link_libraries(replay_readback_stages_test PRIVATE Vulkan::Headers) diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h index fc1b2f555..2a890803b 100644 --- a/runtime/src/vulkan_bindless.h +++ b/runtime/src/vulkan_bindless.h @@ -557,8 +557,7 @@ class BindlessSet { if (index < storage_2d.size()) storage_2d[index] = mirror_storage(mirror) ? 1 : 0; } if (kind_out) { - *kind_out = kind_sampled_bit(std::uint32_t(dimension)) | - (stores ? kind_storage_bit(std::uint32_t(dimension)) : 0) | + *kind_out = sampled_kind(dimension) | (stores ? kind_storage_bit(std::uint32_t(dimension)) : 0) | (used_2d ? (kind_sampled_bit(std::uint32_t(ImageDimension::dim_2d)) | (mirror_storage(mirror) ? kind_storage_bit(std::uint32_t(ImageDimension::dim_2d)) : 0)) @@ -595,15 +594,19 @@ class BindlessSet { const bool live = index && index < used.size() && used[index] == in_use; if (live) { write_image(dimension, index, sampled, sampled_layout, storage, sampled_class); - const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui; + // Storage views are written only inside the storage arrays (write_image): an index add_image gave an + // image without them can be past their end, and then has none to mark. + const bool stores = (storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui) && + index < storage_capacity(dimension); if (index < storage_used_[std::uint32_t(dimension)].size()) storage_used_[std::uint32_t(dimension)][index] = stores ? 1 : 0; } if (live && mirror && mirror->sampled && mirror_of(dimension, index)) { write_image(ImageDimension::dim_2d, index, mirror->sampled, mirror->sampled_layout, mirror->storage, 0); - if (index < mirror_storage_used_.size()) mirror_storage_used_[index] = mirror_storage(mirror) ? 1 : 0; + const bool stores_2d = mirror_storage(mirror) && index < storage_capacity(ImageDimension::dim_2d); + if (index < mirror_storage_used_.size()) mirror_storage_used_[index] = stores_2d ? 1 : 0; auto& storage_2d = storage_used_[std::uint32_t(ImageDimension::dim_2d)]; - if (index < storage_2d.size()) storage_2d[index] = mirror_storage(mirror) ? 1 : 0; + if (index < storage_2d.size()) storage_2d[index] = stores_2d ? 1 : 0; } if (kind_out) *kind_out = image_kind_at(dimension, index); } @@ -649,18 +652,20 @@ class BindlessSet { if (table_dirty_) held_samplers_.push_back(index); } - // What an index of `dimension` is bound in, from what add_image wrote: that dimension's sampled bit, its storage - // bit when the slot holds storage views, and the 2D bits when this image owns the slot at the same number in the - // 2D arrays as its mirror (mirror_of). + // What an index of `dimension` is bound in, from what add_image wrote: that dimension's sampled bit (none for a + // dimension without a sampled array, the 1D array), its storage bit when the slot holds storage views (only ever + // inside the storage arrays), and the 2D bits when this image owns the slot at the same number in the 2D arrays + // as its mirror (mirror_of). std::uint32_t image_kind_at(ImageDimension dimension, std::uint32_t index) const { - const auto set = [&](const std::vector& used) { - return index < used.size() && used[index] != 0; + const auto set = [&](const std::vector& used, ImageDimension in) { + return index < used.size() && used[index] != 0 && index < storage_capacity(in); }; const std::uint32_t d = std::uint32_t(dimension); - std::uint32_t kind = kind_sampled_bit(d) | (set(storage_used_[d]) ? kind_storage_bit(d) : 0); + std::uint32_t kind = sampled_kind(dimension) | (set(storage_used_[d], dimension) ? kind_storage_bit(d) : 0); if (mirror_of(dimension, index)) { kind |= kind_sampled_bit(std::uint32_t(ImageDimension::dim_2d)); - if (set(mirror_storage_used_)) kind |= kind_storage_bit(std::uint32_t(ImageDimension::dim_2d)); + if (set(mirror_storage_used_, ImageDimension::dim_2d)) + kind |= kind_storage_bit(std::uint32_t(ImageDimension::dim_2d)); } return kind; } @@ -926,6 +931,13 @@ class BindlessSet { return 0; } + // The sampled bit of an image of `dimension`: none where the dimension has no sampled array (the 1D array, whose + // images are storage only), so a tagged module never reads a sampled descriptor that is not there. + static std::uint32_t sampled_kind(ImageDimension dimension) { + return bindless::sampled_binding(dimension) == bindless::no_binding ? 0 + : kind_sampled_bit(std::uint32_t(dimension)); + } + // A mirror is worth binding only when it has a view; its storage views make the 2D storage arrays valid too. static bool mirror_storage(const MirrorViews* mirror) { return mirror && (mirror->storage.as_float || mirror->storage.as_uint || mirror->storage.as_sint || diff --git a/runtime/test/bindless_kind_bits_test.cpp b/runtime/test/bindless_kind_bits_test.cpp new file mode 100644 index 000000000..6103561c9 --- /dev/null +++ b/runtime/test/bindless_kind_bits_test.cpp @@ -0,0 +1,203 @@ +// The kind bits BindlessSet gives an image index (runtime/src/vulkan_bindless.h; descriptor_table.h's kind bits), +// which a tagged module (descriptor_table: tagged) trusts to index the arrays they name: +// - a storage bit only where a storage descriptor was written, so never at an index past the storage arrays (an +// image added without storage views can get one, and update_image may give it storage views later); +// - no sampled bit for a 1D-array image, as that dimension has no sampled array. +// From kevincommits's finding in #1608 (RT-BINDLESS-KIND-BITS). No Vulkan loader, GPU, or game data: a fake device +// gives the set its handles and memory. +#include "../src/vulkan_bindless.h" + +#include +#include +#include +#include +#include + +namespace { + +std::uintptr_t next_handle = 0x1000; +template +T make_handle() { + return reinterpret_cast(next_handle += 0x10); +} + +// Mapped memory for the table buffers: each allocation gets its bytes. +std::vector> allocations; + +template +VKAPI_ATTR VkResult VKAPI_CALL create(VkDevice, const void*, const VkAllocationCallbacks*, T* out) { + *out = make_handle(); + return VK_SUCCESS; +} +template +VKAPI_ATTR void VKAPI_CALL destroy(VkDevice, T, const VkAllocationCallbacks*) {} + +VKAPI_ATTR VkResult VKAPI_CALL reset_pool(VkDevice, VkDescriptorPool, VkDescriptorPoolResetFlags) { return VK_SUCCESS; } +VKAPI_ATTR VkResult VKAPI_CALL allocate_sets(VkDevice, const VkDescriptorSetAllocateInfo* info, VkDescriptorSet* sets) { + for (std::uint32_t i = 0; i < info->descriptorSetCount; ++i) sets[i] = make_handle(); + return VK_SUCCESS; +} +VKAPI_ATTR void VKAPI_CALL update_sets(VkDevice, std::uint32_t, const VkWriteDescriptorSet*, std::uint32_t, + const VkCopyDescriptorSet*) {} +VKAPI_ATTR void VKAPI_CALL buffer_requirements(VkDevice, VkBuffer, VkMemoryRequirements* r) { + *r = {1u << 20, 256, 1}; +} +VKAPI_ATTR void VKAPI_CALL image_requirements(VkDevice, VkImage, VkMemoryRequirements* r) { *r = {4096, 256, 1}; } +VKAPI_ATTR VkResult VKAPI_CALL bind_buffer(VkDevice, VkBuffer, VkDeviceMemory, VkDeviceSize) { return VK_SUCCESS; } +VKAPI_ATTR VkResult VKAPI_CALL bind_image(VkDevice, VkImage, VkDeviceMemory, VkDeviceSize) { return VK_SUCCESS; } +VKAPI_ATTR VkResult VKAPI_CALL allocate_memory(VkDevice, const VkMemoryAllocateInfo* info, + const VkAllocationCallbacks*, VkDeviceMemory* memory) { + allocations.push_back(std::make_unique(std::size_t(info->allocationSize))); + *memory = reinterpret_cast(allocations.back().get()); + return VK_SUCCESS; +} +VKAPI_ATTR void VKAPI_CALL free_memory(VkDevice, VkDeviceMemory, const VkAllocationCallbacks*) {} +VKAPI_ATTR VkResult VKAPI_CALL map_memory(VkDevice, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize, + VkMemoryMapFlags, void** data) { + *data = reinterpret_cast(memory) + offset; + return VK_SUCCESS; +} +VKAPI_ATTR void VKAPI_CALL cmd_barrier(VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags, VkDependencyFlags, + std::uint32_t, const VkMemoryBarrier*, std::uint32_t, + const VkBufferMemoryBarrier*, std::uint32_t, const VkImageMemoryBarrier*) {} +VKAPI_ATTR void VKAPI_CALL cmd_copy(VkCommandBuffer, VkBuffer, VkBuffer, std::uint32_t, const VkBufferCopy*) {} +VKAPI_ATTR void VKAPI_CALL cmd_update(VkCommandBuffer, VkBuffer, VkDeviceSize, VkDeviceSize, const void*) {} +VKAPI_ATTR void VKAPI_CALL cmd_clear(VkCommandBuffer, VkImage, VkImageLayout, const VkClearColorValue*, std::uint32_t, + const VkImageSubresourceRange*) {} + +#define FAKE(name, fn) \ + if (!std::strcmp(n, #name)) return reinterpret_cast(static_cast(fn)); +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_proc(VkDevice, const char* n) { + FAKE(vkCreateDescriptorSetLayout, reinterpret_cast(&create)) + FAKE(vkDestroyDescriptorSetLayout, &destroy) + FAKE(vkCreateDescriptorPool, reinterpret_cast(&create)) + FAKE(vkDestroyDescriptorPool, &destroy) + FAKE(vkResetDescriptorPool, &reset_pool) + FAKE(vkAllocateDescriptorSets, &allocate_sets) + FAKE(vkUpdateDescriptorSets, &update_sets) + FAKE(vkCreateBuffer, reinterpret_cast(&create)) + FAKE(vkDestroyBuffer, &destroy) + FAKE(vkGetBufferMemoryRequirements, &buffer_requirements) + FAKE(vkBindBufferMemory, &bind_buffer) + FAKE(vkAllocateMemory, &allocate_memory) + FAKE(vkFreeMemory, &free_memory) + FAKE(vkMapMemory, &map_memory) + FAKE(vkCreateImage, reinterpret_cast(&create)) + FAKE(vkDestroyImage, &destroy) + FAKE(vkGetImageMemoryRequirements, &image_requirements) + FAKE(vkBindImageMemory, &bind_image) + FAKE(vkCreateImageView, reinterpret_cast(&create)) + FAKE(vkDestroyImageView, &destroy) + FAKE(vkCreateSampler, reinterpret_cast(&create)) + FAKE(vkDestroySampler, &destroy) + FAKE(vkCmdPipelineBarrier, &cmd_barrier) + FAKE(vkCmdCopyBuffer, &cmd_copy) + FAKE(vkCmdUpdateBuffer, &cmd_update) + FAKE(vkCmdClearColorImage, &cmd_clear) + return nullptr; +} +#undef FAKE + +int failures = 0; +void expect(bool ok, const char* what) { + if (!ok) { + std::fprintf(stderr, "FAIL: %s\n", what); + ++failures; + } +} + +VkImageView view() { return make_handle(); } + +} // namespace + +int main() { + using namespace leonida; + using D = ImageDimension; + const auto sampled_bit = [](D d) { return kind_sampled_bit(std::uint32_t(d)); }; + const auto storage_bit = [](D d) { return kind_storage_bit(std::uint32_t(d)); }; + + VkPhysicalDeviceMemoryProperties memory{}; + memory.memoryTypeCount = 1; + memory.memoryTypes[0].propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + memory.memoryHeapCount = 1; + memory.memoryHeaps[0].size = 1ull << 32; + BindlessSet set(get_proc, make_handle(), memory); // the default capacities + if (!set.ok()) { + std::fprintf(stderr, "FAIL: the set was not made on the fake device\n"); + return 1; + } + const std::uint32_t storage_2d = set.storage_capacity(D::dim_2d); + expect(storage_2d < set.image_capacity(D::dim_2d), "2D: the storage arrays are shorter than the sampled one"); + + // 1. A 2D sampled-only image gets the first index past the storage arrays; storage views given to it later are + // not written there, so its kind has no storage bit. + { + std::uint32_t kind = 0; + const std::uint32_t index = + set.add_image(D::dim_2d, view(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, {}, 0, nullptr, &kind); + expect(index == storage_2d, "2D sampled-only: the first index past the storage arrays"); + expect(kind == sampled_bit(D::dim_2d), "2D sampled-only: sampled bit only"); + StorageViews storage; + storage.as_float = view(); + set.update_image(D::dim_2d, index, view(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, storage, 0, nullptr, + &kind); + expect(kind == sampled_bit(D::dim_2d), "2D past the storage arrays, updated with a storage view: no storage bit"); + expect(set.image_kind_at(D::dim_2d, index) == kind, "image_kind_at agrees"); + // Inside the storage arrays an update does add the bit. + std::uint32_t low_kind = 0; + const std::uint32_t low = set.add_image(D::dim_2d, view(), VK_IMAGE_LAYOUT_GENERAL, storage, 0, nullptr, + &low_kind); + expect(low && low < storage_2d && low_kind == (sampled_bit(D::dim_2d) | storage_bit(D::dim_2d)), + "2D with a storage view: inside the storage arrays, both bits"); + set.update_image(D::dim_2d, low, view(), VK_IMAGE_LAYOUT_GENERAL, {}, 0, nullptr, &low_kind); + expect(low_kind == sampled_bit(D::dim_2d), "2D updated without storage views: the storage bit goes"); + set.update_image(D::dim_2d, low, view(), VK_IMAGE_LAYOUT_GENERAL, storage, 0, nullptr, &low_kind); + expect(low_kind == (sampled_bit(D::dim_2d) | storage_bit(D::dim_2d)), + "2D updated with a storage view inside the storage arrays: the storage bit comes back"); + } + + // 2. A mirrored 2D-array sampled-only image past both storage arrays, updated with storage views for itself and + // its 2D mirror: no storage bit for either. + { + BindlessSet::MirrorViews mirror; + mirror.sampled = view(); + mirror.sampled_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + std::uint32_t kind = 0; + const std::uint32_t index = + set.add_image(D::dim_2d_array, view(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, {}, 0, &mirror, &kind); + expect(index >= set.storage_capacity(D::dim_2d_array) && index >= storage_2d, + "2D array sampled-only, mirrored: past both storage arrays"); + expect(kind == (sampled_bit(D::dim_2d_array) | sampled_bit(D::dim_2d)), "mirrored: both sampled bits"); + StorageViews storage; + storage.as_float = view(); + mirror.storage.as_float = view(); + set.update_image(D::dim_2d_array, index, view(), VK_IMAGE_LAYOUT_GENERAL, storage, 0, &mirror, &kind); + expect(kind == (sampled_bit(D::dim_2d_array) | sampled_bit(D::dim_2d)), + "mirrored, past the storage arrays, updated with storage views: no storage bits"); + expect(set.image_kind_at(D::dim_2d_array, index) == kind, "image_kind_at agrees (mirrored)"); + } + + // 3. A storage-only 1D-array image: the dimension has no sampled array, so no sampled bit. + { + expect(bindless::sampled_binding(D::dim_1d_array) == bindless::no_binding, "1D array: no sampled binding"); + StorageViews storage; + storage.as_float = view(); + std::uint32_t kind = 0; + const std::uint32_t index = set.add_image(D::dim_1d_array, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, storage, 0, + nullptr, &kind); + expect(index == 1, "1D array storage-only: index 1"); + expect(kind == storage_bit(D::dim_1d_array), "1D array storage-only: storage bit only (add_image)"); + expect(set.image_kind_at(D::dim_1d_array, index) == storage_bit(D::dim_1d_array), + "1D array storage-only: storage bit only (image_kind_at)"); + set.update_image(D::dim_1d_array, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, storage, 0, nullptr, &kind); + expect(kind == storage_bit(D::dim_1d_array), "1D array storage-only: storage bit only (update_image)"); + } + + if (failures) { + std::fprintf(stderr, "%d failure(s)\n", failures); + return 1; + } + std::printf("bindless kind bits: storage only inside the storage arrays, no sampled bit for 1D arrays\n"); + return 0; +}