diff options
| author | totonii <totonicontato@gmail.com> | 2026-11-23 16:54:12 -0300 |
|---|---|---|
| committer | totonii <totonicontato@gmail.com> | 2026-11-23 16:54:12 -0300 |
| commit | 0c2effa27fea08d4df7c3ab40eef821864d9a1fa (patch) | |
| tree | cf7a8096e9fcec790822c39e3e31e1af9a8b0682 /runtime | |
| parent | 867d922f09b86583859796a174df89555fb32da8 (diff) | |
fix(vulkan): use memory_type_order and validate stride/alignment in IndirectDispatchCounter
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | runtime/src/vulkan_indirect_dispatch_count.h | 35 | ||||
| -rw-r--r-- | runtime/test/indirect_dispatch_count_alloc_test.cpp | 285 |
3 files changed, 309 insertions, 17 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 7080d87bf..e290b3dbb 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -2984,6 +2984,12 @@ if(TARGET Vulkan::Headers) | |||
| 2984 | target_compile_definitions(d3d12_state_map_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) | 2984 | target_compile_definitions(d3d12_state_map_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) |
| 2985 | endif() | 2985 | endif() |
| 2986 | add_test(NAME d3d12_state_map COMMAND d3d12_state_map_test) | 2986 | add_test(NAME d3d12_state_map COMMAND d3d12_state_map_test) |
| 2987 | add_executable(indirect_dispatch_count_alloc_test test/indirect_dispatch_count_alloc_test.cpp) | ||
| 2988 | target_link_libraries(indirect_dispatch_count_alloc_test PRIVATE Vulkan::Headers) | ||
| 2989 | if(WIN32) | ||
| 2990 | target_compile_definitions(indirect_dispatch_count_alloc_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) | ||
| 2991 | endif() | ||
| 2992 | add_test(NAME indirect_dispatch_count_alloc COMMAND indirect_dispatch_count_alloc_test) | ||
| 2987 | # A buffer UAV clear writes where its resource starts in a shared VkBuffer (no GPU). | 2993 | # A buffer UAV clear writes where its resource starts in a shared VkBuffer (no GPU). |
| 2988 | add_executable(replay_uav_clear_offset_test test/replay_uav_clear_offset_test.cpp) | 2994 | add_executable(replay_uav_clear_offset_test test/replay_uav_clear_offset_test.cpp) |
| 2989 | target_link_libraries(replay_uav_clear_offset_test PRIVATE Vulkan::Headers) | 2995 | target_link_libraries(replay_uav_clear_offset_test PRIVATE Vulkan::Headers) |
diff --git a/runtime/src/vulkan_indirect_dispatch_count.h b/runtime/src/vulkan_indirect_dispatch_count.h index 225f7c1fc..510a4dcad 100644 --- a/runtime/src/vulkan_indirect_dispatch_count.h +++ b/runtime/src/vulkan_indirect_dispatch_count.h | |||
| @@ -12,6 +12,7 @@ | |||
| 12 | #include <memory> | 12 | #include <memory> |
| 13 | #include <vector> | 13 | #include <vector> |
| 14 | #include "indirect_dispatch_count_spv.h" | 14 | #include "indirect_dispatch_count_spv.h" |
| 15 | #include "vulkan_memory.h" | ||
| 15 | 16 | ||
| 16 | namespace leonida { | 17 | namespace leonida { |
| 17 | 18 | ||
| @@ -59,9 +60,10 @@ public: | |||
| 59 | VkDeviceSize count_offset, std::uint32_t stride, std::uint32_t max_count, | 60 | VkDeviceSize count_offset, std::uint32_t stride, std::uint32_t max_count, |
| 60 | CountedDispatch& out) { | 61 | CountedDispatch& out) { |
| 61 | out = {}; | 62 | out = {}; |
| 62 | if (!arguments || !count || !stride || stride % 4 || !max_count || | ||
| 63 | !owner.limits_.maxComputeWorkGroupCount[0] || !owner.make_pipeline()) return false; | ||
| 64 | constexpr VkDeviceSize record_bytes = 3 * sizeof(std::uint32_t); | 63 | constexpr VkDeviceSize record_bytes = 3 * sizeof(std::uint32_t); |
| 64 | if (!arguments || !count || stride < record_bytes || stride % 4 || | ||
| 65 | (argument_offset % 4) != 0 || (count_offset % 4) != 0 || !max_count || | ||
| 66 | !owner.limits_.maxComputeWorkGroupCount[0] || !owner.make_pipeline()) return false; | ||
| 65 | if (VkDeviceSize(max_count) > std::numeric_limits<VkDeviceSize>::max() / record_bytes) | 67 | if (VkDeviceSize(max_count) > std::numeric_limits<VkDeviceSize>::max() / record_bytes) |
| 66 | return false; | 68 | return false; |
| 67 | const VkDeviceSize bytes = VkDeviceSize(max_count) * record_bytes; | 69 | const VkDeviceSize bytes = VkDeviceSize(max_count) * record_bytes; |
| @@ -77,22 +79,20 @@ public: | |||
| 77 | VkMemoryRequirements requirements{}; | 79 | VkMemoryRequirements requirements{}; |
| 78 | owner.fn<PFN_vkGetBufferMemoryRequirements>("vkGetBufferMemoryRequirements")( | 80 | owner.fn<PFN_vkGetBufferMemoryRequirements>("vkGetBufferMemoryRequirements")( |
| 79 | owner.device_, a.buffer, &requirements); | 81 | owner.device_, a.buffer, &requirements); |
| 80 | std::uint32_t memory_type = UINT32_MAX; | 82 | const auto order = memory_type_order(owner.memory_, requirements.memoryTypeBits, 0, |
| 81 | for (std::uint32_t i = 0; i < owner.memory_.memoryTypeCount; ++i) { | 83 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, requirements.size); |
| 82 | if (!(requirements.memoryTypeBits & (1u << i))) continue; | 84 | if (order.empty()) return false; |
| 83 | if (memory_type == UINT32_MAX) memory_type = i; | ||
| 84 | if (owner.memory_.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { | ||
| 85 | memory_type = i; | ||
| 86 | break; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | if (memory_type == UINT32_MAX) return false; | ||
| 90 | VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; | 85 | VkMemoryAllocateFlagsInfo flags{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO}; |
| 91 | flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; | 86 | flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; |
| 92 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &flags}; | 87 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &flags}; |
| 93 | allocation.allocationSize = requirements.size; | 88 | allocation.allocationSize = requirements.size; |
| 94 | allocation.memoryTypeIndex = memory_type; | 89 | const auto alloc_memory = owner.fn<PFN_vkAllocateMemory>("vkAllocateMemory"); |
| 95 | if (owner.fn<PFN_vkAllocateMemory>("vkAllocateMemory")(owner.device_, &allocation, nullptr, &a.memory) != VK_SUCCESS || | 90 | for (const std::uint32_t memory_type : order) { |
| 91 | allocation.memoryTypeIndex = memory_type; | ||
| 92 | if (alloc_memory(owner.device_, &allocation, nullptr, &a.memory) == VK_SUCCESS) break; | ||
| 93 | a.memory = VK_NULL_HANDLE; | ||
| 94 | } | ||
| 95 | if (!a.memory || | ||
| 96 | owner.fn<PFN_vkBindBufferMemory>("vkBindBufferMemory")(owner.device_, a.buffer, a.memory, 0) != VK_SUCCESS) | 96 | owner.fn<PFN_vkBindBufferMemory>("vkBindBufferMemory")(owner.device_, a.buffer, a.memory, 0) != VK_SUCCESS) |
| 97 | return false; | 97 | return false; |
| 98 | const auto address = owner.fn<PFN_vkGetBufferDeviceAddress>("vkGetBufferDeviceAddress"); | 98 | const auto address = owner.fn<PFN_vkGetBufferDeviceAddress>("vkGetBufferDeviceAddress"); |
| @@ -129,8 +129,9 @@ public: | |||
| 129 | static_assert(sizeof(Push) == 40); | 129 | static_assert(sizeof(Push) == 40); |
| 130 | const VkDeviceSize dispatch_limit = VkDeviceSize(limits_.maxComputeWorkGroupCount[0]) * 64; | 130 | const VkDeviceSize dispatch_limit = VkDeviceSize(limits_.maxComputeWorkGroupCount[0]) * 64; |
| 131 | // Bound per-dispatch indexing to 32 bits, even when the D3D12 stride is very large. | 131 | // Bound per-dispatch indexing to 32 bits, even when the D3D12 stride is very large. |
| 132 | // The shader loads three consecutive dwords, so leave room for the final +2. | 132 | // The shader loads and stores three consecutive dwords per record, so leave room for the final +2. |
| 133 | const VkDeviceSize index_limit = (UINT32_MAX - 2u) / (job.stride / 4); | 133 | const std::uint32_t index_stride_words = std::max<std::uint32_t>(3u, job.stride / 4); |
| 134 | const VkDeviceSize index_limit = (UINT32_MAX - 2u) / index_stride_words; | ||
| 134 | const VkDeviceSize chunk_limit = std::min(dispatch_limit, index_limit + 1); | 135 | const VkDeviceSize chunk_limit = std::min(dispatch_limit, index_limit + 1); |
| 135 | for (VkDeviceSize first = 0; first < job.max_count;) { | 136 | for (VkDeviceSize first = 0; first < job.max_count;) { |
| 136 | const std::uint32_t count = std::uint32_t(std::min<VkDeviceSize>(chunk_limit, job.max_count - first)); | 137 | const std::uint32_t count = std::uint32_t(std::min<VkDeviceSize>(chunk_limit, job.max_count - first)); |
| @@ -138,7 +139,7 @@ public: | |||
| 138 | job.stride / 4, std::uint32_t(first), count}; | 139 | job.stride / 4, std::uint32_t(first), count}; |
| 139 | fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, | 140 | fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, |
| 140 | 0, sizeof(push), &push); | 141 | 0, sizeof(push), &push); |
| 141 | fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb, (count + 63u) / 64u, 1, 1); | 142 | fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb, (count - 1u) / 64u + 1u, 1, 1); |
| 142 | first += count; | 143 | first += count; |
| 143 | } | 144 | } |
| 144 | VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | 145 | VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; |
diff --git a/runtime/test/indirect_dispatch_count_alloc_test.cpp b/runtime/test/indirect_dispatch_count_alloc_test.cpp new file mode 100644 index 000000000..6365cbdf9 --- /dev/null +++ b/runtime/test/indirect_dispatch_count_alloc_test.cpp | |||
| @@ -0,0 +1,285 @@ | |||
| 1 | // Pure-CPU tests for runtime/src/vulkan_indirect_dispatch_count.h: | ||
| 2 | // - skips undersized memory heaps (VUID-vkAllocateMemory-pAllocateInfo-01713) via memory_type_order; | ||
| 3 | // - falls back to subsequent compatible memory types on VK_ERROR_OUT_OF_DEVICE_MEMORY; | ||
| 4 | // - rejects stride < 12 (4 or 8) and non-4-byte-aligned argument/count offsets; | ||
| 5 | // - chunks emit() dispatches cleanly without zero-group or 32-bit workgroup overflow. | ||
| 6 | // No GPU, Vulkan loader, or game files required. | ||
| 7 | #ifndef VK_NO_PROTOTYPES | ||
| 8 | #define VK_NO_PROTOTYPES | ||
| 9 | #endif | ||
| 10 | #include "../src/vulkan_indirect_dispatch_count.h" | ||
| 11 | |||
| 12 | #include <cstdint> | ||
| 13 | #include <cstdio> | ||
| 14 | #include <cstring> | ||
| 15 | #include <utility> | ||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | #define CHECK(cond) do { \ | ||
| 19 | if (!(cond)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #cond); return 1; } \ | ||
| 20 | } while (0) | ||
| 21 | |||
| 22 | namespace { | ||
| 23 | |||
| 24 | struct MockState { | ||
| 25 | std::uint32_t memory_type_bits = 0x3u; | ||
| 26 | std::uint32_t fail_oom_type = UINT32_MAX; | ||
| 27 | std::uint64_t next_handle = 1; | ||
| 28 | int create_buffer_calls = 0; | ||
| 29 | int destroy_buffer_calls = 0; | ||
| 30 | int free_memory_calls = 0; | ||
| 31 | std::vector<std::pair<std::uint32_t, VkDeviceSize>> alloc_attempts; | ||
| 32 | std::vector<std::uint32_t> dispatch_groups_x; | ||
| 33 | std::vector<std::uint32_t> push_records; | ||
| 34 | }; | ||
| 35 | |||
| 36 | MockState* g_mock = nullptr; | ||
| 37 | |||
| 38 | template<typename Handle> | ||
| 39 | Handle make_handle(std::uint64_t id) { | ||
| 40 | return reinterpret_cast<Handle>(static_cast<std::uintptr_t>(id)); | ||
| 41 | } | ||
| 42 | |||
| 43 | VKAPI_ATTR VkResult VKAPI_CALL mock_create_buffer(VkDevice, const VkBufferCreateInfo* info, | ||
| 44 | const VkAllocationCallbacks*, VkBuffer* out) { | ||
| 45 | ++g_mock->create_buffer_calls; | ||
| 46 | *out = make_handle<VkBuffer>(info->size); // encode buffer byte size in handle for requirements lookup | ||
| 47 | return VK_SUCCESS; | ||
| 48 | } | ||
| 49 | |||
| 50 | VKAPI_ATTR void VKAPI_CALL mock_destroy_buffer(VkDevice, VkBuffer, const VkAllocationCallbacks*) { | ||
| 51 | ++g_mock->destroy_buffer_calls; | ||
| 52 | } | ||
| 53 | |||
| 54 | VKAPI_ATTR void VKAPI_CALL mock_get_buffer_memory_requirements(VkDevice, VkBuffer buffer, | ||
| 55 | VkMemoryRequirements* req) { | ||
| 56 | req->size = static_cast<VkDeviceSize>(reinterpret_cast<std::uintptr_t>(buffer)); | ||
| 57 | req->alignment = 256; | ||
| 58 | req->memoryTypeBits = g_mock->memory_type_bits; | ||
| 59 | } | ||
| 60 | |||
| 61 | VKAPI_ATTR VkResult VKAPI_CALL mock_allocate_memory(VkDevice, const VkMemoryAllocateInfo* info, | ||
| 62 | const VkAllocationCallbacks*, VkDeviceMemory* out) { | ||
| 63 | g_mock->alloc_attempts.push_back({info->memoryTypeIndex, info->allocationSize}); | ||
| 64 | if (info->memoryTypeIndex == g_mock->fail_oom_type) { | ||
| 65 | *out = VK_NULL_HANDLE; | ||
| 66 | return VK_ERROR_OUT_OF_DEVICE_MEMORY; | ||
| 67 | } | ||
| 68 | *out = make_handle<VkDeviceMemory>(++g_mock->next_handle); | ||
| 69 | return VK_SUCCESS; | ||
| 70 | } | ||
| 71 | |||
| 72 | VKAPI_ATTR void VKAPI_CALL mock_free_memory(VkDevice, VkDeviceMemory, const VkAllocationCallbacks*) { | ||
| 73 | ++g_mock->free_memory_calls; | ||
| 74 | } | ||
| 75 | |||
| 76 | VKAPI_ATTR VkResult VKAPI_CALL mock_bind_buffer_memory(VkDevice, VkBuffer, VkDeviceMemory, VkDeviceSize) { | ||
| 77 | return VK_SUCCESS; | ||
| 78 | } | ||
| 79 | |||
| 80 | VKAPI_ATTR VkDeviceAddress VKAPI_CALL mock_get_buffer_device_address(VkDevice, const VkBufferDeviceAddressInfo* info) { | ||
| 81 | return static_cast<VkDeviceAddress>(0x10000000ull + reinterpret_cast<std::uintptr_t>(info->buffer) * 16ull); | ||
| 82 | } | ||
| 83 | |||
| 84 | VKAPI_ATTR VkResult VKAPI_CALL mock_create_shader_module(VkDevice, const VkShaderModuleCreateInfo*, | ||
| 85 | const VkAllocationCallbacks*, VkShaderModule* out) { | ||
| 86 | *out = make_handle<VkShaderModule>(++g_mock->next_handle); | ||
| 87 | return VK_SUCCESS; | ||
| 88 | } | ||
| 89 | |||
| 90 | VKAPI_ATTR void VKAPI_CALL mock_destroy_shader_module(VkDevice, VkShaderModule, const VkAllocationCallbacks*) {} | ||
| 91 | |||
| 92 | VKAPI_ATTR VkResult VKAPI_CALL mock_create_pipeline_layout(VkDevice, const VkPipelineLayoutCreateInfo*, | ||
| 93 | const VkAllocationCallbacks*, VkPipelineLayout* out) { | ||
| 94 | *out = make_handle<VkPipelineLayout>(++g_mock->next_handle); | ||
| 95 | return VK_SUCCESS; | ||
| 96 | } | ||
| 97 | |||
| 98 | VKAPI_ATTR void VKAPI_CALL mock_destroy_pipeline_layout(VkDevice, VkPipelineLayout, const VkAllocationCallbacks*) {} | ||
| 99 | |||
| 100 | VKAPI_ATTR VkResult VKAPI_CALL mock_create_compute_pipelines(VkDevice, VkPipelineCache, std::uint32_t, | ||
| 101 | const VkComputePipelineCreateInfo*, | ||
| 102 | const VkAllocationCallbacks*, VkPipeline* out) { | ||
| 103 | *out = make_handle<VkPipeline>(++g_mock->next_handle); | ||
| 104 | return VK_SUCCESS; | ||
| 105 | } | ||
| 106 | |||
| 107 | VKAPI_ATTR void VKAPI_CALL mock_destroy_pipeline(VkDevice, VkPipeline, const VkAllocationCallbacks*) {} | ||
| 108 | |||
| 109 | VKAPI_ATTR void VKAPI_CALL mock_cmd_pipeline_barrier(VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags, | ||
| 110 | VkDependencyFlags, std::uint32_t, const VkMemoryBarrier*, | ||
| 111 | std::uint32_t, const VkBufferMemoryBarrier*, | ||
| 112 | std::uint32_t, const VkImageMemoryBarrier*) {} | ||
| 113 | |||
| 114 | VKAPI_ATTR void VKAPI_CALL mock_cmd_bind_pipeline(VkCommandBuffer, VkPipelineBindPoint, VkPipeline) {} | ||
| 115 | |||
| 116 | VKAPI_ATTR void VKAPI_CALL mock_cmd_push_constants(VkCommandBuffer, VkPipelineLayout, VkShaderStageFlags, | ||
| 117 | std::uint32_t, std::uint32_t size, const void* values) { | ||
| 118 | if (size == 40) { | ||
| 119 | std::uint32_t records = 0; | ||
| 120 | std::memcpy(&records, static_cast<const std::uint8_t*>(values) + 32, sizeof(records)); | ||
| 121 | g_mock->push_records.push_back(records); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | VKAPI_ATTR void VKAPI_CALL mock_cmd_dispatch(VkCommandBuffer, std::uint32_t x, std::uint32_t, std::uint32_t) { | ||
| 126 | g_mock->dispatch_groups_x.push_back(x); | ||
| 127 | } | ||
| 128 | |||
| 129 | VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL mock_get_device_proc_addr(VkDevice, const char* name) { | ||
| 130 | if (std::strcmp(name, "vkCreateBuffer") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_create_buffer); | ||
| 131 | if (std::strcmp(name, "vkDestroyBuffer") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_destroy_buffer); | ||
| 132 | if (std::strcmp(name, "vkGetBufferMemoryRequirements") == 0) | ||
| 133 | return reinterpret_cast<PFN_vkVoidFunction>(mock_get_buffer_memory_requirements); | ||
| 134 | if (std::strcmp(name, "vkAllocateMemory") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_allocate_memory); | ||
| 135 | if (std::strcmp(name, "vkFreeMemory") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_free_memory); | ||
| 136 | if (std::strcmp(name, "vkBindBufferMemory") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_bind_buffer_memory); | ||
| 137 | if (std::strcmp(name, "vkGetBufferDeviceAddress") == 0) | ||
| 138 | return reinterpret_cast<PFN_vkVoidFunction>(mock_get_buffer_device_address); | ||
| 139 | if (std::strcmp(name, "vkCreateShaderModule") == 0) | ||
| 140 | return reinterpret_cast<PFN_vkVoidFunction>(mock_create_shader_module); | ||
| 141 | if (std::strcmp(name, "vkDestroyShaderModule") == 0) | ||
| 142 | return reinterpret_cast<PFN_vkVoidFunction>(mock_destroy_shader_module); | ||
| 143 | if (std::strcmp(name, "vkCreatePipelineLayout") == 0) | ||
| 144 | return reinterpret_cast<PFN_vkVoidFunction>(mock_create_pipeline_layout); | ||
| 145 | if (std::strcmp(name, "vkDestroyPipelineLayout") == 0) | ||
| 146 | return reinterpret_cast<PFN_vkVoidFunction>(mock_destroy_pipeline_layout); | ||
| 147 | if (std::strcmp(name, "vkCreateComputePipelines") == 0) | ||
| 148 | return reinterpret_cast<PFN_vkVoidFunction>(mock_create_compute_pipelines); | ||
| 149 | if (std::strcmp(name, "vkDestroyPipeline") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_destroy_pipeline); | ||
| 150 | if (std::strcmp(name, "vkCmdPipelineBarrier") == 0) | ||
| 151 | return reinterpret_cast<PFN_vkVoidFunction>(mock_cmd_pipeline_barrier); | ||
| 152 | if (std::strcmp(name, "vkCmdBindPipeline") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_cmd_bind_pipeline); | ||
| 153 | if (std::strcmp(name, "vkCmdPushConstants") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_cmd_push_constants); | ||
| 154 | if (std::strcmp(name, "vkCmdDispatch") == 0) return reinterpret_cast<PFN_vkVoidFunction>(mock_cmd_dispatch); | ||
| 155 | return nullptr; | ||
| 156 | } | ||
| 157 | |||
| 158 | VkPhysicalDeviceLimits make_limits(std::uint32_t max_wg_x = 65535u) { | ||
| 159 | VkPhysicalDeviceLimits limits{}; | ||
| 160 | limits.maxComputeWorkGroupCount[0] = max_wg_x; | ||
| 161 | return limits; | ||
| 162 | } | ||
| 163 | |||
| 164 | } // namespace | ||
| 165 | |||
| 166 | int main() { | ||
| 167 | using namespace leonida; | ||
| 168 | const VkDevice dummy_device = make_handle<VkDevice>(1); | ||
| 169 | const VkBuffer args_buf = make_handle<VkBuffer>(100); | ||
| 170 | const VkBuffer count_buf = make_handle<VkBuffer>(200); | ||
| 171 | const VkCommandBuffer dummy_cb = make_handle<VkCommandBuffer>(1); | ||
| 172 | |||
| 173 | // 1. Undersized first DEVICE_LOCAL heap (256 MiB BAR) must be skipped (VUID-vkAllocateMemory-pAllocateInfo-01713) | ||
| 174 | // and allocation must succeed on the larger fitting DEVICE_LOCAL heap (Type 1). | ||
| 175 | { | ||
| 176 | MockState state; | ||
| 177 | g_mock = &state; | ||
| 178 | VkPhysicalDeviceMemoryProperties mem{}; | ||
| 179 | mem.memoryHeapCount = 2; | ||
| 180 | mem.memoryHeaps[0] = {VkDeviceSize(256) << 20, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; | ||
| 181 | mem.memoryHeaps[1] = {VkDeviceSize(8) << 30, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; | ||
| 182 | mem.memoryTypeCount = 2; | ||
| 183 | mem.memoryTypes[0] = {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, 0}; | ||
| 184 | mem.memoryTypes[1] = {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 1}; | ||
| 185 | |||
| 186 | IndirectDispatchCounter counter(mock_get_device_proc_addr, dummy_device, mem, make_limits()); | ||
| 187 | auto batch = counter.batch(); | ||
| 188 | CountedDispatch out{}; | ||
| 189 | // 24,000,000 * 12 = 288,000,000 bytes (> 256 MiB heap 0, fits in 8 GiB heap 1). | ||
| 190 | CHECK(batch->prepare(args_buf, 0, count_buf, 0, 12, 24000000u, out)); | ||
| 191 | CHECK(state.alloc_attempts.size() == 1); | ||
| 192 | CHECK(state.alloc_attempts[0].first == 1u); | ||
| 193 | batch.reset(); | ||
| 194 | CHECK(state.destroy_buffer_calls == 1 && state.free_memory_calls == 1); | ||
| 195 | } | ||
| 196 | |||
| 197 | // 2. OOM on the first DEVICE_LOCAL type falls back to the next compatible memory type. | ||
| 198 | { | ||
| 199 | MockState state; | ||
| 200 | state.fail_oom_type = 0; | ||
| 201 | g_mock = &state; | ||
| 202 | VkPhysicalDeviceMemoryProperties mem{}; | ||
| 203 | mem.memoryHeapCount = 2; | ||
| 204 | mem.memoryHeaps[0] = {VkDeviceSize(8) << 30, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; | ||
| 205 | mem.memoryHeaps[1] = {VkDeviceSize(16) << 30, 0}; | ||
| 206 | mem.memoryTypeCount = 2; | ||
| 207 | mem.memoryTypes[0] = {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 0}; | ||
| 208 | mem.memoryTypes[1] = {VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 1}; | ||
| 209 | |||
| 210 | IndirectDispatchCounter counter(mock_get_device_proc_addr, dummy_device, mem, make_limits()); | ||
| 211 | auto batch = counter.batch(); | ||
| 212 | CountedDispatch out{}; | ||
| 213 | CHECK(batch->prepare(args_buf, 0, count_buf, 0, 12, 16u, out)); | ||
| 214 | CHECK(state.alloc_attempts.size() == 2); | ||
| 215 | CHECK(state.alloc_attempts[0].first == 0u); | ||
| 216 | CHECK(state.alloc_attempts[1].first == 1u); | ||
| 217 | batch.reset(); | ||
| 218 | CHECK(state.destroy_buffer_calls == 1 && state.free_memory_calls == 1); | ||
| 219 | } | ||
| 220 | |||
| 221 | // 3. When every compatible heap is smaller than requirements.size, prepare() fails without calling vkAllocateMemory. | ||
| 222 | { | ||
| 223 | MockState state; | ||
| 224 | state.memory_type_bits = 0x1u; | ||
| 225 | g_mock = &state; | ||
| 226 | VkPhysicalDeviceMemoryProperties mem{}; | ||
| 227 | mem.memoryHeapCount = 1; | ||
| 228 | mem.memoryHeaps[0] = {64, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; | ||
| 229 | mem.memoryTypeCount = 1; | ||
| 230 | mem.memoryTypes[0] = {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 0}; | ||
| 231 | |||
| 232 | IndirectDispatchCounter counter(mock_get_device_proc_addr, dummy_device, mem, make_limits()); | ||
| 233 | auto batch = counter.batch(); | ||
| 234 | CountedDispatch out{}; | ||
| 235 | CHECK(!batch->prepare(args_buf, 0, count_buf, 0, 12, 16u, out)); | ||
| 236 | CHECK(state.alloc_attempts.empty()); | ||
| 237 | batch.reset(); | ||
| 238 | CHECK(state.destroy_buffer_calls == 1 && state.free_memory_calls == 0); | ||
| 239 | } | ||
| 240 | |||
| 241 | // 4. Stride < 12 (4 or 8) and non-4-byte-aligned offsets are rejected before creating any VkBuffer. | ||
| 242 | { | ||
| 243 | MockState state; | ||
| 244 | g_mock = &state; | ||
| 245 | VkPhysicalDeviceMemoryProperties mem{}; | ||
| 246 | mem.memoryHeapCount = 1; | ||
| 247 | mem.memoryHeaps[0] = {VkDeviceSize(1) << 30, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; | ||
| 248 | mem.memoryTypeCount = 1; | ||
| 249 | mem.memoryTypes[0] = {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 0}; | ||
| 250 | |||
| 251 | IndirectDispatchCounter counter(mock_get_device_proc_addr, dummy_device, mem, make_limits()); | ||
| 252 | auto batch = counter.batch(); | ||
| 253 | CountedDispatch out{}; | ||
| 254 | CHECK(!batch->prepare(args_buf, 0, count_buf, 0, 4, 16u, out)); | ||
| 255 | CHECK(!batch->prepare(args_buf, 0, count_buf, 0, 8, 16u, out)); | ||
| 256 | CHECK(!batch->prepare(args_buf, 2, count_buf, 0, 12, 16u, out)); | ||
| 257 | CHECK(!batch->prepare(args_buf, 0, count_buf, 6, 12, 16u, out)); | ||
| 258 | CHECK(state.create_buffer_calls == 0); | ||
| 259 | CHECK(out.indirect == VK_NULL_HANDLE); | ||
| 260 | } | ||
| 261 | |||
| 262 | // 5. Happy path chunking in emit(): 130 records with maxComputeWorkGroupCount[0] = 2 -> chunks of 128 and 2. | ||
| 263 | { | ||
| 264 | MockState state; | ||
| 265 | g_mock = &state; | ||
| 266 | VkPhysicalDeviceMemoryProperties mem{}; | ||
| 267 | mem.memoryHeapCount = 1; | ||
| 268 | mem.memoryHeaps[0] = {VkDeviceSize(1) << 30, VK_MEMORY_HEAP_DEVICE_LOCAL_BIT}; | ||
| 269 | mem.memoryTypeCount = 1; | ||
| 270 | mem.memoryTypes[0] = {VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, 0}; | ||
| 271 | |||
| 272 | IndirectDispatchCounter counter(mock_get_device_proc_addr, dummy_device, mem, make_limits(2u)); | ||
| 273 | auto batch = counter.batch(); | ||
| 274 | CountedDispatch out{}; | ||
| 275 | CHECK(batch->prepare(args_buf, 16, count_buf, 4, 16, 130u, out)); | ||
| 276 | counter.emit(dummy_cb, out); | ||
| 277 | CHECK(state.push_records.size() == 2); | ||
| 278 | CHECK(state.push_records[0] == 128u && state.push_records[1] == 2u); | ||
| 279 | CHECK(state.dispatch_groups_x.size() == 2); | ||
| 280 | CHECK(state.dispatch_groups_x[0] == 2u && state.dispatch_groups_x[1] == 1u); | ||
| 281 | } | ||
| 282 | |||
| 283 | std::printf("indirect_dispatch_count_alloc_test: all checks passed\n"); | ||
| 284 | return 0; | ||
| 285 | } | ||