upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/vk_queue_test.cpp
diff options
context:
space:
mode:
authorRüzgar Koray Genç <koray_genc1@hotmail.com>2026-11-23 21:17:24 -0800
committerRüzgar Koray Genç <koray_genc1@hotmail.com>2026-11-23 21:50:58 -0800
commit6e81b921996cd60f8fa67b5e1247a5b96af51066 (patch)
treed810ca9181ce32798de72413979d81f765c4ad05 /runtime/test/vk_queue_test.cpp
parent8f481cf16f261c7f37458aeb45e8f4e19994146d (diff)
Every submission starts with a full memory barrier (ExecuteCommandLists boundary)
D3D12 ends every ExecuteCommandLists with its work done and its writes visible to the next one on the queue (implicit state decay relies on it), so a list may touch memory an earlier submission wrote without any barrier: two submissions copying into one buffer, say. Vulkan orders submissions on a queue only by what their barriers say, and nothing in a VulkanQueue's command buffer ordered it after the previous one. Synchronization validation reported it on leonida/work: vk_queue_test's back-to-back copies into one buffer, WRITE_AFTER_WRITE between submissions (10 errors, the test failed under LEONIDA_VK_VALIDATION=1). execute_now now records an ALL_COMMANDS -> ALL_COMMANDS memory barrier (MEMORY_WRITE -> MEMORY_READ | MEMORY_WRITE) right after vkBeginCommandBuffer. Test: vk_queue_test 4b counts the command buffers begun and those whose first barrier is that one, over two submissions each on the direct and the copy queue: 4 of 4 (0 before). Under LEONIDA_VK_VALIDATION=1 the whole test now has 0 validation errors (RTX 3080 Laptop). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/vk_queue_test.cpp')
-rw-r--r--runtime/test/vk_queue_test.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/runtime/test/vk_queue_test.cpp b/runtime/test/vk_queue_test.cpp
index 744adfab8..8dad391b0 100644
--- a/runtime/test/vk_queue_test.cpp
+++ b/runtime/test/vk_queue_test.cpp
@@ -64,6 +64,33 @@ VKAPI_ATTR VkResult VKAPI_CALL stalling_wait_semaphores(VkDevice device, const V
64 return real_wait_semaphores(device, info, timeout); 64 return real_wait_semaphores(device, info, timeout);
65} 65}
66 66
67// Each submission's command buffer starts with a full memory barrier (D3D12's ExecuteCommandLists boundary): counts
68// the command buffers begun and those whose first barrier is that one.
69PFN_vkBeginCommandBuffer real_begin = nullptr;
70PFN_vkCmdPipelineBarrier real_barrier = nullptr;
71std::uint32_t begun = 0, led_by_full_barrier = 0;
72bool awaiting_first_barrier = false;
73
74VKAPI_ATTR VkResult VKAPI_CALL counting_begin(VkCommandBuffer cb, const VkCommandBufferBeginInfo* info) {
75 ++begun;
76 awaiting_first_barrier = true;
77 return real_begin(cb, info);
78}
79
80VKAPI_ATTR void VKAPI_CALL watching_barrier(VkCommandBuffer cb, VkPipelineStageFlags src, VkPipelineStageFlags dst,
81 VkDependencyFlags flags, std::uint32_t memory_count,
82 const VkMemoryBarrier* memory, std::uint32_t buffer_count,
83 const VkBufferMemoryBarrier* buffers, std::uint32_t image_count,
84 const VkImageMemoryBarrier* images) {
85 if (awaiting_first_barrier) {
86 awaiting_first_barrier = false;
87 if (src == VK_PIPELINE_STAGE_ALL_COMMANDS_BIT && dst == VK_PIPELINE_STAGE_ALL_COMMANDS_BIT &&
88 memory_count == 1 && (memory[0].srcAccessMask & VK_ACCESS_MEMORY_WRITE_BIT))
89 ++led_by_full_barrier;
90 }
91 real_barrier(cb, src, dst, flags, memory_count, memory, buffer_count, buffers, image_count, images);
92}
93
67const char* flags_text(VkQueueFlags f) { 94const char* flags_text(VkQueueFlags f) {
68 static char buf[32]; 95 static char buf[32];
69 std::snprintf(buf, sizeof buf, "%s%s%s", f & VK_QUEUE_GRAPHICS_BIT ? "G" : "", f & VK_QUEUE_COMPUTE_BIT ? "C" : "", 96 std::snprintf(buf, sizeof buf, "%s%s%s", f & VK_QUEUE_GRAPHICS_BIT ? "G" : "", f & VK_QUEUE_COMPUTE_BIT ? "C" : "",
@@ -347,6 +374,38 @@ int main() {
347 EXPECT(compute.execute(clear_lists) == ReplayResult::unsupported); 374 EXPECT(compute.execute(clear_lists) == ReplayResult::unsupported);
348 std::printf("copy and compute queues refuse a render-target clear\n"); 375 std::printf("copy and compute queues refuse a render-target clear\n");
349 376
377 // 4b. Two ExecuteCommandLists copying into the same buffer, with no barrier in either list: D3D12 orders them (each
378 // ends with its work done and visible), so each submission's command buffer must start with a full memory barrier.
379 // Before, none did, and synchronization validation reported WRITE_AFTER_WRITE between them (in 5 below).
380 {
381 void* map = nullptr;
382 VkBuffer twice_src = d.buffer(4096, usage, &map);
383 VkBuffer twice_dst = d.buffer(4096, usage, &map);
384 EXPECT(twice_src && twice_dst);
385 add_buffer(9, twice_src, 4096);
386 add_buffer(10, twice_dst, 4096);
387 CommandList twice;
388 EXPECT(twice.copy_buffer({10, 0, 9, 0, 4096}));
389 EXPECT(twice.close());
390 const CommandList* twice_lists[] = {&twice};
391 real_begin = context.vk.vkBeginCommandBuffer;
392 real_barrier = context.vk.vkCmdPipelineBarrier;
393 context.vk.vkBeginCommandBuffer = counting_begin;
394 context.vk.vkCmdPipelineBarrier = watching_barrier;
395 begun = led_by_full_barrier = 0;
396 for (Kind kind : {Kind::direct, Kind::copy}) {
397 VulkanQueue& q = kind == Kind::direct ? direct : copy;
398 EXPECT(q.execute(twice_lists) == ReplayResult::success);
399 EXPECT(q.execute(twice_lists) == ReplayResult::success);
400 EXPECT(q.idle());
401 }
402 context.vk.vkBeginCommandBuffer = real_begin;
403 context.vk.vkCmdPipelineBarrier = real_barrier;
404 std::printf("ExecuteCommandLists boundary: %u of %u submissions start with a full memory barrier\n",
405 led_by_full_barrier, begun);
406 EXPECT(begun >= 4 && led_by_full_barrier == begun);
407 }
408
350 // 5. A caller that submits faster than the GPU finishes must not grow the queue's command buffers without bound: 409 // 5. A caller that submits faster than the GPU finishes must not grow the queue's command buffers without bound:
351 // at the cap it waits for the oldest one. Big host-visible copies keep the GPU behind the submitting loop. The first 410 // at the cap it waits for the oldest one. Big host-visible copies keep the GPU behind the submitting loop. The first
352 // waits time out (injected), as on a stalled GPU: the cap must hold through a timeout too. 411 // waits time out (injected), as on a stalled GPU: the cap must hold through a timeout too.