upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/submit_frame_test.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-27 10:59:34 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-27 10:59:34 -0500
commit50c5eddcd285b036280eeabef2cdefe7db73f0ef (patch)
treecfba3acf3dd022d761f3a10f49a97b32f1090dca /runtime/test/submit_frame_test.cpp
parent6eb37271300f182c6747331e5f81674a4b872949 (diff)
runtime: unlock VulkanQueue state_mutex_ across execution, coalesce ready batches, zero-copy CommandList
Round 1D runtime CPU optimization: 1. Split VulkanQueue state bookkeeping (state_mutex_, microsecond hold) from execution serialization (executing_ + exec_cv_ + execution-local wait/pin staging). Callers arriving while executing_ is active append to held_ in strict FIFO order without blocking on before_execute_() or replay, and the executing thread drains ready held work before releasing executing_. 2. Coalesce consecutive ready Deferred::Kind::batch ops in release_held() / drain_held_while_executing() and consecutive Call::Kind::execute ops in async_worker() (stopping on Signal, Wait, Marker, or Present after_held barrier_after), inserting D3D12 inter-batch full memory barriers and falling back to per-batch execution on validation failure. 3. Pass closed CommandList snapshots via std::shared_ptr<const CommandList> from GraphicsCommandList to VulkanQueue (zero deep copies on submit; Reset() reuses storage with an acquire fence when unique or allocates fresh storage while in flight), and skip redundant visible_heaps scan_title_table() recording for console descriptor heaps when table-scan diagnostics are off. 4. Add unit tests in submit_frame_test covering unlocked before_execute_(), strict FIFO order, held-batch coalescing and Signal/Wait splits, invalid batch fallback, zero-copy Reset(), and async worker (depth=4) coalescing with Present barrier ordering. AI-assisted: Antigravity (Gemini 4 Neon). Tested on Linux (RADV RAPHAEL_MENDOCINO): 15/15 queue/submit/replay ctest targets pass (list_value_progress, vk_queue, vk_queue_query_sync, submit_frame, vk_queue_constructor_oom, vk_semaphore_lifetime, vk_fence_dispatcher_sync, vk_fence_lost_by_counter, vk_command_replay, vk_predicated_copy, vk_predicate_capture, vk_overlapping_occlusion, vk_host_at_aliasing, vk_host_texture_order).
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
-rw-r--r--runtime/test/submit_frame_test.cpp311
1 files changed, 310 insertions, 1 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp
index 7f0abebc4..ba5d275c7 100644
--- a/runtime/test/submit_frame_test.cpp
+++ b/runtime/test/submit_frame_test.cpp
@@ -9,11 +9,15 @@
9#include "vk_test_device.h" 9#include "vk_test_device.h"
10#include "../src/vulkan_backend.h" 10#include "../src/vulkan_backend.h"
11 11
12#include <atomic>
12#include <chrono> 13#include <chrono>
13#include <cstdint> 14#include <cstdint>
14#include <cstdio> 15#include <cstdio>
16#include <cstring>
17#include <memory>
15#include <mutex> 18#include <mutex>
16#include <thread> 19#include <thread>
20#include <vector>
17 21
18#define EXPECT(condition) do { \ 22#define EXPECT(condition) do { \
19 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ 23 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
@@ -167,10 +171,315 @@ int submissions() {
167 return 0; 171 return 0;
168} 172}
169 173
174// Round 1D queue concurrency, batch coalescing, zero-copy CommandList snapshot, and async worker tests.
175int queue_async_and_coalescing() {
176 vktest::Device d;
177 if (!d.create()) {
178 std::printf("no usable Vulkan device: the queue_async_and_coalescing part is skipped\n");
179 return 0;
180 }
181
182 constexpr VkDeviceSize kBufBytes = 256;
183 const VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
184
185 auto make_copy_list = [](ResourceId dst, std::uint64_t dst_off, ResourceId src, std::uint64_t src_off,
186 std::uint64_t bytes) -> std::shared_ptr<CommandList> {
187 auto list = std::make_shared<CommandList>();
188 if (!list->copy_buffer({dst, dst_off, src, src_off, bytes}) || !list->close()) return {};
189 return list;
190 };
191
192 // 1. Concurrent submission while before_execute_() is running must not block on state_mutex_, must preserve
193 // strict FIFO execution order, and must coalesce consecutive queued batches on drain.
194 {
195 VulkanContext context(d.device_proc, d.device, true);
196 EXPECT(context.loaded());
197 VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
198 EXPECT(queue.ok());
199
200 void* src_map = nullptr;
201 void* dst_map = nullptr;
202 VkBuffer src = d.buffer(kBufBytes, usage, &src_map);
203 VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map);
204 EXPECT(src && dst);
205 auto* sb = static_cast<std::uint8_t*>(src_map);
206 auto* db = static_cast<std::uint8_t*>(dst_map);
207 std::memset(sb, 0, kBufBytes);
208 std::memset(db, 0, kBufBytes);
209 sb[0] = 0x11;
210 sb[16] = 0x22;
211 sb[32] = 0x33;
212
213 ResourceBinding r_src{}, r_dst{};
214 r_src.buffer = src;
215 r_src.size = kBufBytes;
216 r_dst.buffer = dst;
217 r_dst.size = kBufBytes;
218 r_dst.host_readable = true;
219 context.add_resource(1, r_src);
220 context.add_resource(2, r_dst);
221
222 std::atomic<unsigned> before_calls{0};
223 std::atomic<bool> in_first_before{false};
224 std::atomic<bool> release_first_before{false};
225 context.set_before_execute([&] {
226 const unsigned n = before_calls.fetch_add(1, std::memory_order_acq_rel) + 1;
227 if (n == 1) {
228 in_first_before.store(true, std::memory_order_release);
229 while (!release_first_before.load(std::memory_order_acquire)) std::this_thread::yield();
230 }
231 });
232
233 // Each batch writes its marker to its own slot AND overwrites slot 0 (final value proves strict FIFO order).
234 auto b1 = std::make_shared<CommandList>();
235 EXPECT(b1->copy_buffer({2, 16, 1, 0, 1}) && b1->copy_buffer({2, 0, 1, 0, 1}) && b1->close());
236 auto b2 = std::make_shared<CommandList>();
237 EXPECT(b2->copy_buffer({2, 32, 1, 16, 1}) && b2->copy_buffer({2, 0, 1, 16, 1}) && b2->close());
238 auto b3 = std::make_shared<CommandList>();
239 EXPECT(b3->copy_buffer({2, 48, 1, 32, 1}) && b3->copy_buffer({2, 0, 1, 32, 1}) && b3->close());
240
241 VulkanFence done(context, 0);
242 EXPECT(done.semaphore() != VK_NULL_HANDLE);
243
244 ReplayResult t_res = ReplayResult::unsupported;
245 std::thread worker([&] {
246 std::shared_ptr<const CommandList> s1 = b1;
247 t_res = queue.execute({&s1, 1});
248 });
249
250 while (!in_first_before.load(std::memory_order_acquire)) std::this_thread::yield();
251
252 // While worker is blocked inside before_execute_(), this thread submits b2, b3, and Signal(done, 1).
253 // If state_mutex_ were held across before_execute_(), this would deadlock before release_first_before is set!
254 std::shared_ptr<const CommandList> s2 = b2;
255 std::shared_ptr<const CommandList> s3 = b3;
256 EXPECT(queue.execute({&s2, 1}) == ReplayResult::success);
257 EXPECT(queue.execute({&s3, 1}) == ReplayResult::success);
258 EXPECT(queue.signal(done.semaphore(), 1));
259
260 // Zero-copy proof: queue holds references to b2 and b3 without copying; simulating Reset() when use_count > 1
261 // allocates fresh storage and never mutates the in-flight snapshot.
262 EXPECT(b2.use_count() > 1 && b3.use_count() > 1);
263 b2 = std::make_shared<CommandList>();
264 EXPECT(b2->copy_buffer({2, 0, 1, 0, 1}) && b2->close()); // distinct content (0x11), must not affect in-flight
265
266 release_first_before.store(true, std::memory_order_release);
267 worker.join();
268 EXPECT(t_res == ReplayResult::success);
269 EXPECT(done.wait(1, 5'000'000'000ull));
270 EXPECT(queue.idle());
271
272 // b1 ran first (before_calls #1), then b2+b3 coalesced into ONE execute_now() (before_calls #2).
273 EXPECT(before_calls.load(std::memory_order_acquire) == 2);
274 EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33);
275 EXPECT(db[0] == 0x33); // strict FIFO: b1 -> b2 -> b3
276 // After completion, s2/s3 snapshots are released (only local s3 holds b3's old CommandList).
277 s2.reset();
278 s3.reset();
279 EXPECT(b1.use_count() == 1 && b3.use_count() == 1);
280 EXPECT(b3->reset()); // steady-state reuse with zero allocation
281 std::printf("queue sync: unlocked before_execute_, strict FIFO, coalesced drain, zero-copy Reset ok\n");
282 }
283
284 // 2. Held batches behind a fence gate coalesce consecutive ready batches into ONE before_execute_() call,
285 // while an intervening Signal or Wait stops coalescing. Also verify invalid-batch fallback inside a
286 // coalesced group does not drop valid peers.
287 {
288 VulkanContext context(d.device_proc, d.device, true);
289 EXPECT(context.loaded());
290 VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
291 VulkanQueue copy_q(context, d.queue, d.family, VulkanQueue::QueueKind::copy);
292 EXPECT(queue.ok() && copy_q.ok());
293
294 void* src_map = nullptr;
295 void* dst_map = nullptr;
296 VkBuffer src = d.buffer(kBufBytes, usage, &src_map);
297 VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map);
298 EXPECT(src && dst);
299 auto* sb = static_cast<std::uint8_t*>(src_map);
300 auto* db = static_cast<std::uint8_t*>(dst_map);
301 std::memset(db, 0, kBufBytes);
302 for (int i = 0; i < 6; ++i) sb[i * 16] = std::uint8_t(0xA1 + i);
303
304 ResourceBinding r_src{}, r_dst{};
305 r_src.buffer = src;
306 r_src.size = kBufBytes;
307 r_dst.buffer = dst;
308 r_dst.size = kBufBytes;
309 r_dst.host_readable = true;
310 context.add_resource(1, r_src);
311 context.add_resource(2, r_dst);
312
313 std::atomic<unsigned> before_calls{0};
314 context.set_before_execute([&] { before_calls.fetch_add(1, std::memory_order_relaxed); });
315
316 VulkanFence gate(context, 0), mid(context, 0), done(context, 0);
317 EXPECT(gate.semaphore() && mid.semaphore() && done.semaphore());
318
319 auto make_step = [&](int idx) {
320 auto l = std::make_shared<CommandList>();
321 if (!l->copy_buffer({2, std::uint64_t((idx + 1) * 16), 1, std::uint64_t(idx * 16), 1}) ||
322 !l->copy_buffer({2, 0, 1, std::uint64_t(idx * 16), 1}) || !l->close())
323 return std::shared_ptr<const CommandList>{};
324 return std::shared_ptr<const CommandList>(std::move(l));
325 };
326 std::shared_ptr<const CommandList> steps[6];
327 for (int i = 0; i < 6; ++i) {
328 steps[i] = make_step(i);
329 EXPECT(steps[i] != nullptr);
330 }
331
332 queue.wait(gate.semaphore(), 1);
333 EXPECT(queue.execute({&steps[0], 1}) == ReplayResult::success); // group 1: steps 0, 1, 2
334 EXPECT(queue.execute({&steps[1], 1}) == ReplayResult::success);
335 EXPECT(queue.execute({&steps[2], 1}) == ReplayResult::success);
336 EXPECT(queue.signal(mid.semaphore(), 1)); // Signal stops coalescing
337 EXPECT(queue.execute({&steps[3], 1}) == ReplayResult::success); // group 2: step 3 alone
338 queue.wait(mid.semaphore(), 1); // Wait stops coalescing
339 EXPECT(queue.execute({&steps[4], 1}) == ReplayResult::success); // group 3: steps 4, 5
340 EXPECT(queue.execute({&steps[5], 1}) == ReplayResult::success);
341 EXPECT(queue.signal(done.semaphore(), 1));
342
343 EXPECT(before_calls.load() == 0);
344 EXPECT(gate.signal(1));
345 EXPECT(done.wait(1, 5'000'000'000ull));
346 EXPECT(queue.idle());
347
348 // Exactly 3 before_execute_() calls: {0,1,2}, {3}, {4,5}.
349 EXPECT(before_calls.load() == 3);
350 for (int i = 0; i < 6; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xA1 + i));
351 EXPECT(db[0] == std::uint8_t(0xA6)); // strict FIFO through all 6 batches
352
353 // Coalesced fallback: [valid, invalid ClearRenderTarget on copy queue, valid] held behind gate2.
354 VulkanFence gate2(context, 0), done2(context, 0);
355 EXPECT(gate2.semaphore() && done2.semaphore());
356 std::memset(db, 0, kBufBytes);
357 auto bad_clear = std::make_shared<CommandList>();
358 TargetView tv{};
359 tv.resource = 2;
360 tv.layer_count = 1;
361 EXPECT(bad_clear->clear_render_target(tv, ClearColor{}) && bad_clear->close());
362 std::shared_ptr<const CommandList> bad_snap = bad_clear;
363 copy_q.wait(gate2.semaphore(), 1);
364 EXPECT(copy_q.execute({&steps[0], 1}) == ReplayResult::success);
365 EXPECT(copy_q.execute({&bad_snap, 1}) == ReplayResult::success);
366 EXPECT(copy_q.execute({&steps[1], 1}) == ReplayResult::success);
367 EXPECT(copy_q.signal(done2.semaphore(), 1));
368 EXPECT(gate2.signal(1));
369 EXPECT(done2.wait(1, 5'000'000'000ull));
370 EXPECT(copy_q.idle());
371 EXPECT(copy_q.take_held_failure() == ReplayResult::unsupported);
372 EXPECT(db[16] == 0xA1 && db[32] == 0xA2 && db[0] == 0xA2);
373 std::printf("queue held coalescing: 6 batches -> 3 submits, Signal/Wait split, invalid fallback ok\n");
374 }
375
376 // 3. LEONIDA_SUBMIT_ASYNC (per-queue async worker depth=4): inbox coalescing, Present barrier split,
377 // held-batch release through worker, and zero-copy CommandList Reset() while queued.
378 {
379 VulkanContext context(d.device_proc, d.device, true);
380 EXPECT(context.loaded());
381 VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
382 EXPECT(queue.ok());
383 queue.set_async_depth(4);
384
385 void* src_map = nullptr;
386 void* dst_map = nullptr;
387 VkBuffer src = d.buffer(kBufBytes, usage, &src_map);
388 VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map);
389 EXPECT(src && dst);
390 auto* sb = static_cast<std::uint8_t*>(src_map);
391 auto* db = static_cast<std::uint8_t*>(dst_map);
392 std::memset(db, 0, kBufBytes);
393 for (int i = 0; i < 4; ++i) sb[i * 16] = std::uint8_t(0xC1 + i);
394
395 ResourceBinding r_src{}, r_dst{};
396 r_src.buffer = src;
397 r_src.size = kBufBytes;
398 r_dst.buffer = dst;
399 r_dst.size = kBufBytes;
400 r_dst.host_readable = true;
401 context.add_resource(1, r_src);
402 context.add_resource(2, r_dst);
403
404 std::atomic<unsigned> before_calls{0};
405 std::atomic<bool> in_first{false};
406 std::atomic<bool> go_first{false};
407 context.set_before_execute([&] {
408 const unsigned n = before_calls.fetch_add(1, std::memory_order_acq_rel) + 1;
409 if (n == 1) {
410 in_first.store(true, std::memory_order_release);
411 while (!go_first.load(std::memory_order_acquire)) std::this_thread::yield();
412 }
413 });
414
415 auto c0 = make_copy_list(2, 16, 1, 0, 1);
416 auto c1 = make_copy_list(2, 32, 1, 16, 1);
417 auto c2 = make_copy_list(2, 48, 1, 32, 1);
418 auto c3 = make_copy_list(2, 64, 1, 48, 1);
419 EXPECT(c0 && c1 && c2 && c3);
420
421 std::shared_ptr<const CommandList> s0 = c0, s1 = c1, s2 = c2, s3 = c3;
422 EXPECT(queue.execute({&s0, 1}) == ReplayResult::success);
423 while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield();
424
425 // Enqueue c1, c2 (will coalesce), then a Present after_held barrier, then c3 (must NOT coalesce past barrier).
426 EXPECT(queue.execute({&s1, 1}) == ReplayResult::success);
427 EXPECT(queue.execute({&s2, 1}) == ReplayResult::success);
428 EXPECT(c1.use_count() > 1 && c2.use_count() > 1); // queued zero-copy
429
430 std::atomic<unsigned> barrier_seen_before_calls{0};
431 std::atomic<bool> barrier_ran{false};
432 int barrier_owner = 0;
433 context.holds().after_held(&barrier_owner, [&] {
434 barrier_seen_before_calls.store(before_calls.load(std::memory_order_acquire), std::memory_order_release);
435 barrier_ran.store(true, std::memory_order_release);
436 });
437
438 EXPECT(queue.execute({&s3, 1}) == ReplayResult::success);
439 VulkanFence done(context, 0);
440 EXPECT(done.semaphore() != VK_NULL_HANDLE);
441 EXPECT(queue.signal(done.semaphore(), 1));
442
443 // Simulate Reset() of c1 while still in inbox: allocating new storage leaves in-flight snapshot intact.
444 c1 = make_copy_list(2, 32, 1, 0, 1);
445 EXPECT(c1 != nullptr);
446
447 go_first.store(true, std::memory_order_release);
448 EXPECT(done.wait(1, 5'000'000'000ull));
449 EXPECT(queue.idle());
450 EXPECT(barrier_ran.load(std::memory_order_acquire));
451
452 // 3 before_execute_() calls total: {c0}, {c1, c2}, {c3}; barrier ran after {c1, c2} (count == 2), before {c3}!
453 EXPECT(before_calls.load() == 3);
454 EXPECT(barrier_seen_before_calls.load() == 2);
455 EXPECT(db[16] == 0xC1 && db[32] == 0xC2 && db[48] == 0xC3 && db[64] == 0xC4);
456
457 // Held batches behind a gate in async mode: release_ready() wakes worker and coalesces them.
458 before_calls.store(0);
459 std::memset(db, 0, kBufBytes);
460 VulkanFence gate(context, 0), done2(context, 0);
461 EXPECT(gate.semaphore() && done2.semaphore());
462 queue.wait(gate.semaphore(), 1);
463 EXPECT(queue.execute({&s0, 1}) == ReplayResult::success);
464 EXPECT(queue.execute({&s2, 1}) == ReplayResult::success);
465 EXPECT(queue.signal(done2.semaphore(), 1));
466 EXPECT(queue.idle()); // flushes inbox into held_ while gate is still closed
467 EXPECT(before_calls.load() == 0);
468 EXPECT(gate.signal(1));
469 EXPECT(done2.wait(1, 5'000'000'000ull));
470 EXPECT(queue.idle());
471 EXPECT(before_calls.load() == 1);
472 EXPECT(db[16] == 0xC1 && db[48] == 0xC3);
473 std::printf("queue async (depth=4): inbox coalescing, Present barrier split, gate release ok\n");
474 }
475
476 return 0;
477}
478
170} // namespace 479} // namespace
171 480
172int main() { 481int main() {
173 if (scripted() || submissions()) return 1; 482 if (scripted() || submissions() || queue_async_and_coalescing()) return 1;
174 std::printf("submit_frame_test: OK\n"); 483 std::printf("submit_frame_test: OK\n");
175 return 0; 484 return 0;
176} 485}