upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/indirect_dispatch_count_alloc_test.cpp
diff options
context:
space:
mode:
authortotonii <totonicontato@gmail.com>2026-11-23 16:54:12 -0300
committertotonii <totonicontato@gmail.com>2026-11-23 16:54:12 -0300
commit0c2effa27fea08d4df7c3ab40eef821864d9a1fa (patch)
treecf7a8096e9fcec790822c39e3e31e1af9a8b0682 /runtime/test/indirect_dispatch_count_alloc_test.cpp
parent867d922f09b86583859796a174df89555fb32da8 (diff)
fix(vulkan): use memory_type_order and validate stride/alignment in IndirectDispatchCounter
Diffstat (limited to 'runtime/test/indirect_dispatch_count_alloc_test.cpp')
-rw-r--r--runtime/test/indirect_dispatch_count_alloc_test.cpp285
1 files changed, 285 insertions, 0 deletions
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
22namespace {
23
24struct 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
36MockState* g_mock = nullptr;
37
38template<typename Handle>
39Handle make_handle(std::uint64_t id) {
40 return reinterpret_cast<Handle>(static_cast<std::uintptr_t>(id));
41}
42
43VKAPI_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
50VKAPI_ATTR void VKAPI_CALL mock_destroy_buffer(VkDevice, VkBuffer, const VkAllocationCallbacks*) {
51 ++g_mock->destroy_buffer_calls;
52}
53
54VKAPI_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
61VKAPI_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
72VKAPI_ATTR void VKAPI_CALL mock_free_memory(VkDevice, VkDeviceMemory, const VkAllocationCallbacks*) {
73 ++g_mock->free_memory_calls;
74}
75
76VKAPI_ATTR VkResult VKAPI_CALL mock_bind_buffer_memory(VkDevice, VkBuffer, VkDeviceMemory, VkDeviceSize) {
77 return VK_SUCCESS;
78}
79
80VKAPI_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
84VKAPI_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
90VKAPI_ATTR void VKAPI_CALL mock_destroy_shader_module(VkDevice, VkShaderModule, const VkAllocationCallbacks*) {}
91
92VKAPI_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
98VKAPI_ATTR void VKAPI_CALL mock_destroy_pipeline_layout(VkDevice, VkPipelineLayout, const VkAllocationCallbacks*) {}
99
100VKAPI_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
107VKAPI_ATTR void VKAPI_CALL mock_destroy_pipeline(VkDevice, VkPipeline, const VkAllocationCallbacks*) {}
108
109VKAPI_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
114VKAPI_ATTR void VKAPI_CALL mock_cmd_bind_pipeline(VkCommandBuffer, VkPipelineBindPoint, VkPipeline) {}
115
116VKAPI_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
125VKAPI_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
129VKAPI_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
158VkPhysicalDeviceLimits 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
166int 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}