diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 13:13:35 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 13:19:48 -0500 |
| commit | b0419cefad82faf3c953d3cd839978de62915eaf (patch) | |
| tree | 21ce42e11296c763390af5f16fb3df9c499f8783 /runtime/test/submit_frame_test.cpp | |
| parent | 03f1cd7f43c6f6f283a05ec354e717235cb92ef6 (diff) | |
runtime: preserve sync ExecuteCommandLists, drop unsafe batch coalescing, track active_seq_
Fix queue-sync bugs in VulkanQueue (runtime/src/vulkan_backend.h):
1. Synchronous execute_sync_impl(): when executing_ is active and this
batch is not blocked by an unmet Wait (blocked_by_wait_locked()), wait
on exec_cv_ with state_mutex_ unlocked instead of silently deferring
into held_. When !executing_ && !blocked_by_wait_locked(), take
executing_ and drain any already-ready held ops ahead of us first
(closing the gap between QueueHolds::signaled() and pump_ready()
reaching this queue), then hold behind a wait-blocked front if
must_hold() remains true or execute synchronously with before_execute_()
before returning. Wait, Signal, Marker, and genuinely wait-blocked
batches still enqueue under state_mutex_ without blocking on
before_execute_() or replay.
2. active_seq_ tracking: assign active_seq_ under state_mutex_ for direct
unheld execute_sync_impl(), signal_sync(), marker_sync(), update_tiles(),
and copy_tile_mappings(), clear active_seq_ before drain/release,
linearize oldest_held_seq() under state_mutex_, and flush
QueueHolds::pump_ready() after releasing state_mutex_ so after_held()
barriers never overtake active unheld submissions.
3. Remove unsafe cross-batch coalescing in drain_held_while_executing() and
async_worker() (along with batch_breaks, barrier_after,
run_single_held_batch, and run_coalesced_execute*) so every
ExecuteCommandLists runs its own before_execute_(), bindless_prepare_(),
prelude_record_(), and fault markers in strict FIFO order.
4. Fix stale prepared[] iteration in execute_now() statistics->mesh setup
by iterating only i < lists.size().
Update and expand submit_frame_test to cover sync exec_cv_ waiting,
unlocked wait-blocked enqueue, releaser-drain sync ordering, ready-front
drain while pump_mutex_ is held by another thread, active_seq_ protection
of after_held() across unheld execute() and signal(), per-batch FIFO
drain without coalescing, zero-copy CommandList Reset(), and QueueHolds
progress tracking.
Tested on Linux (AMD Ryzen 9 7950X3D iGPU, RADV RAPHAEL_MENDOCINO,
MESA_VK_DEVICE_SELECT=1002:164e!): 9/9 required queue ctest targets passed
(2.92s) plus 5/5 submit_frame_test stress runs.
AI-assisted: Antigravity (Gemini 4 Neon).
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
| -rw-r--r-- | runtime/test/submit_frame_test.cpp | 298 |
1 files changed, 253 insertions, 45 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp index edf690006..5619a4c66 100644 --- a/runtime/test/submit_frame_test.cpp +++ b/runtime/test/submit_frame_test.cpp | |||
| @@ -172,11 +172,12 @@ int submissions() { | |||
| 172 | return 0; | 172 | return 0; |
| 173 | } | 173 | } |
| 174 | 174 | ||
| 175 | // Round 1D queue concurrency, batch coalescing, zero-copy CommandList snapshot, and async worker tests. | 175 | // Queue sync contract, unlocked state_mutex_, per-batch FIFO drain, active_seq_ / after_held(), |
| 176 | int queue_async_and_coalescing() { | 176 | // zero-copy CommandList snapshot, async worker, and QueueHolds progress tracking tests. |
| 177 | int queue_sync_and_async() { | ||
| 177 | vktest::Device d; | 178 | vktest::Device d; |
| 178 | if (!d.create()) { | 179 | if (!d.create()) { |
| 179 | std::printf("no usable Vulkan device: the queue_async_and_coalescing part is skipped\n"); | 180 | std::printf("no usable Vulkan device: the queue_sync_and_async part is skipped\n"); |
| 180 | return 0; | 181 | return 0; |
| 181 | } | 182 | } |
| 182 | 183 | ||
| @@ -190,13 +191,19 @@ int queue_async_and_coalescing() { | |||
| 190 | return list; | 191 | return list; |
| 191 | }; | 192 | }; |
| 192 | 193 | ||
| 193 | // 1. Concurrent submission while before_execute_() is running must not block on state_mutex_, must preserve | 194 | // 1. Synchronous execute() arriving while executing_ == true: |
| 194 | // strict FIFO execution order, and must coalesce consecutive queued batches on drain. | 195 | // (a) With !must_hold(), waits on exec_cv_ and runs before_execute_() synchronously before returning. |
| 196 | // (b) With must_hold() (unmet Wait) or Signal, enqueues immediately under state_mutex_ without blocking on | ||
| 197 | // before_execute_(), preserves zero-copy CommandList snapshots across Reset(), and runs before_execute_() | ||
| 198 | // once per batch on drain. | ||
| 199 | // (c) Arriving while a releaser thread is draining multiple ready held batches waits until the ready held | ||
| 200 | // batches finish and then runs before_execute_() synchronously before returning (strict FIFO). | ||
| 195 | { | 201 | { |
| 196 | VulkanContext context(d.device_proc, d.device, true); | 202 | VulkanContext context(d.device_proc, d.device, true); |
| 197 | EXPECT(context.loaded()); | 203 | EXPECT(context.loaded()); |
| 198 | VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | 204 | VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct); |
| 199 | EXPECT(queue.ok()); | 205 | EXPECT(queue.ok()); |
| 206 | queue.set_async_depth(0); | ||
| 200 | 207 | ||
| 201 | void* src_map = nullptr; | 208 | void* src_map = nullptr; |
| 202 | void* dst_map = nullptr; | 209 | void* dst_map = nullptr; |
| @@ -239,19 +246,53 @@ int queue_async_and_coalescing() { | |||
| 239 | auto b3 = std::make_shared<CommandList>(); | 246 | auto b3 = std::make_shared<CommandList>(); |
| 240 | EXPECT(b3->copy_buffer({2, 48, 1, 32, 1}) && b3->copy_buffer({2, 0, 1, 32, 1}) && b3->close()); | 247 | EXPECT(b3->copy_buffer({2, 48, 1, 32, 1}) && b3->copy_buffer({2, 0, 1, 32, 1}) && b3->close()); |
| 241 | 248 | ||
| 242 | VulkanFence done(context, 0); | 249 | // (a) Unheld sync execute() while executing_ == true must wait on exec_cv_ and run before_execute_(). |
| 243 | EXPECT(done.semaphore() != VK_NULL_HANDLE); | 250 | ReplayResult t1_res = ReplayResult::unsupported, t2_res = ReplayResult::unsupported; |
| 244 | 251 | std::atomic<bool> t2_done{false}; | |
| 245 | ReplayResult t_res = ReplayResult::unsupported; | 252 | std::atomic<unsigned> t2_before_at_return{0}; |
| 246 | std::thread worker([&] { | 253 | std::thread worker1([&] { |
| 247 | std::shared_ptr<const CommandList> s1 = b1; | 254 | std::shared_ptr<const CommandList> s1 = b1; |
| 248 | t_res = queue.execute({&s1, 1}); | 255 | t1_res = queue.execute({&s1, 1}); |
| 256 | }); | ||
| 257 | while (!in_first_before.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 258 | |||
| 259 | std::thread worker2([&] { | ||
| 260 | std::shared_ptr<const CommandList> s2 = b2; | ||
| 261 | t2_res = queue.execute({&s2, 1}); | ||
| 262 | t2_before_at_return.store(before_calls.load(std::memory_order_acquire), std::memory_order_release); | ||
| 263 | t2_done.store(true, std::memory_order_release); | ||
| 249 | }); | 264 | }); |
| 265 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
| 266 | EXPECT(!t2_done.load(std::memory_order_acquire)); | ||
| 267 | EXPECT(before_calls.load(std::memory_order_acquire) == 1); | ||
| 250 | 268 | ||
| 269 | release_first_before.store(true, std::memory_order_release); | ||
| 270 | worker1.join(); | ||
| 271 | worker2.join(); | ||
| 272 | EXPECT(t1_res == ReplayResult::success && t2_res == ReplayResult::success); | ||
| 273 | EXPECT(t2_before_at_return.load(std::memory_order_acquire) == 2); | ||
| 274 | EXPECT(queue.idle()); | ||
| 275 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[0] == 0x22); | ||
| 276 | |||
| 277 | // (b) must_hold() ops (behind an unmet Wait) and Signal enqueue immediately without blocking on executing_. | ||
| 278 | std::memset(db, 0, kBufBytes); | ||
| 279 | before_calls.store(0, std::memory_order_release); | ||
| 280 | in_first_before.store(false, std::memory_order_release); | ||
| 281 | release_first_before.store(false, std::memory_order_release); | ||
| 282 | VulkanFence gate(context, 0), done(context, 0); | ||
| 283 | EXPECT(gate.semaphore() != VK_NULL_HANDLE && done.semaphore() != VK_NULL_HANDLE); | ||
| 284 | |||
| 285 | t1_res = ReplayResult::unsupported; | ||
| 286 | std::thread worker_b([&] { | ||
| 287 | std::shared_ptr<const CommandList> s1 = b1; | ||
| 288 | t1_res = queue.execute({&s1, 1}); | ||
| 289 | }); | ||
| 251 | while (!in_first_before.load(std::memory_order_acquire)) std::this_thread::yield(); | 290 | while (!in_first_before.load(std::memory_order_acquire)) std::this_thread::yield(); |
| 252 | 291 | ||
| 253 | // While worker is blocked inside before_execute_(), this thread submits b2, b3, and Signal(done, 1). | 292 | // While worker_b is blocked inside before_execute_(), this thread queues wait(gate), b2, b3, and Signal(done). |
| 254 | // If state_mutex_ were held across before_execute_(), this would deadlock before release_first_before is set! | 293 | // If state_mutex_ were held across before_execute_() or wait-blocked ops waited on exec_cv_, this would |
| 294 | // deadlock before release_first_before is set! | ||
| 295 | queue.wait(gate.semaphore(), 1); | ||
| 255 | std::shared_ptr<const CommandList> s2 = b2; | 296 | std::shared_ptr<const CommandList> s2 = b2; |
| 256 | std::shared_ptr<const CommandList> s3 = b3; | 297 | std::shared_ptr<const CommandList> s3 = b3; |
| 257 | EXPECT(queue.execute({&s2, 1}) == ReplayResult::success); | 298 | EXPECT(queue.execute({&s2, 1}) == ReplayResult::success); |
| @@ -265,32 +306,199 @@ int queue_async_and_coalescing() { | |||
| 265 | EXPECT(b2->copy_buffer({2, 0, 1, 0, 1}) && b2->close()); // distinct content (0x11), must not affect in-flight | 306 | EXPECT(b2->copy_buffer({2, 0, 1, 0, 1}) && b2->close()); // distinct content (0x11), must not affect in-flight |
| 266 | 307 | ||
| 267 | release_first_before.store(true, std::memory_order_release); | 308 | release_first_before.store(true, std::memory_order_release); |
| 268 | worker.join(); | 309 | worker_b.join(); |
| 269 | EXPECT(t_res == ReplayResult::success); | 310 | EXPECT(t1_res == ReplayResult::success); |
| 311 | EXPECT(before_calls.load(std::memory_order_acquire) == 1); // b2 & b3 stay held behind gate | ||
| 312 | |||
| 313 | EXPECT(gate.signal(1)); | ||
| 270 | EXPECT(done.wait(1, 5'000'000'000ull)); | 314 | EXPECT(done.wait(1, 5'000'000'000ull)); |
| 271 | EXPECT(queue.idle()); | 315 | EXPECT(queue.idle()); |
| 272 | 316 | EXPECT(before_calls.load(std::memory_order_acquire) == 3); // one before_execute_() per batch | |
| 273 | // b1 ran first (before_calls #1), then b2+b3 coalesced into ONE execute_now() (before_calls #2). | 317 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33 && db[0] == 0x33); |
| 274 | EXPECT(before_calls.load(std::memory_order_acquire) == 2); | ||
| 275 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33); | ||
| 276 | EXPECT(db[0] == 0x33); // strict FIFO: b1 -> b2 -> b3 | ||
| 277 | // After completion, s2/s3 snapshots are released (only local s3 holds b3's old CommandList). | ||
| 278 | s2.reset(); | 318 | s2.reset(); |
| 279 | s3.reset(); | 319 | s3.reset(); |
| 280 | EXPECT(b1.use_count() == 1 && b3.use_count() == 1); | 320 | EXPECT(b1.use_count() == 1 && b3.use_count() == 1); |
| 281 | EXPECT(b3->reset()); // steady-state reuse with zero allocation | 321 | EXPECT(b3->reset()); // steady-state reuse with zero allocation |
| 282 | std::printf("queue sync: unlocked before_execute_, strict FIFO, coalesced drain, zero-copy Reset ok\n"); | 322 | |
| 323 | // (c) Sync execute() arriving while a releaser drains [b1 (running), b2 (ready in held_)] must wait for both | ||
| 324 | // ready held batches to drain and run before_execute_() for b3 synchronously before returning. | ||
| 325 | auto b2_fresh = std::make_shared<CommandList>(); | ||
| 326 | EXPECT(b2_fresh->copy_buffer({2, 32, 1, 16, 1}) && b2_fresh->copy_buffer({2, 0, 1, 16, 1}) && | ||
| 327 | b2_fresh->close()); | ||
| 328 | EXPECT(b3->copy_buffer({2, 48, 1, 32, 1}) && b3->copy_buffer({2, 0, 1, 32, 1}) && b3->close()); | ||
| 329 | std::memset(db, 0, kBufBytes); | ||
| 330 | before_calls.store(0, std::memory_order_release); | ||
| 331 | in_first_before.store(false, std::memory_order_release); | ||
| 332 | release_first_before.store(false, std::memory_order_release); | ||
| 333 | VulkanFence gate_c(context, 0); | ||
| 334 | EXPECT(gate_c.semaphore() != VK_NULL_HANDLE); | ||
| 335 | |||
| 336 | queue.wait(gate_c.semaphore(), 1); | ||
| 337 | std::shared_ptr<const CommandList> c1 = b1, c2 = b2_fresh, c3 = b3; | ||
| 338 | EXPECT(queue.execute({&c1, 1}) == ReplayResult::success); | ||
| 339 | EXPECT(queue.execute({&c2, 1}) == ReplayResult::success); | ||
| 340 | EXPECT(before_calls.load(std::memory_order_acquire) == 0); | ||
| 341 | |||
| 342 | std::thread releaser([&] { (void)gate_c.signal(1); }); | ||
| 343 | while (!in_first_before.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 344 | |||
| 345 | ReplayResult title_res = ReplayResult::unsupported; | ||
| 346 | std::atomic<bool> title_done{false}; | ||
| 347 | std::atomic<unsigned> title_before_at_return{0}; | ||
| 348 | std::thread title([&] { | ||
| 349 | title_res = queue.execute({&c3, 1}); | ||
| 350 | title_before_at_return.store(before_calls.load(std::memory_order_acquire), std::memory_order_release); | ||
| 351 | title_done.store(true, std::memory_order_release); | ||
| 352 | }); | ||
| 353 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
| 354 | EXPECT(!title_done.load(std::memory_order_acquire)); | ||
| 355 | EXPECT(before_calls.load(std::memory_order_acquire) == 1); | ||
| 356 | |||
| 357 | release_first_before.store(true, std::memory_order_release); | ||
| 358 | releaser.join(); | ||
| 359 | title.join(); | ||
| 360 | EXPECT(title_res == ReplayResult::success); | ||
| 361 | EXPECT(title_before_at_return.load(std::memory_order_acquire) == 3); | ||
| 362 | EXPECT(queue.idle()); | ||
| 363 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33 && db[0] == 0x33); | ||
| 364 | |||
| 365 | // (d) Gate front held on `queue`, signaled while another thread holds pump_mutex_ (so pump_ready() has not | ||
| 366 | // reached `queue` yet and executing_ == false with !held_.empty() && ready(held_.front())): | ||
| 367 | // sync execute() must drain the ready front and run before_execute_() for itself before returning! | ||
| 368 | VulkanQueue other_q(context, d.queue, d.family, VulkanQueue::QueueKind::compute); | ||
| 369 | EXPECT(other_q.ok()); | ||
| 370 | other_q.set_async_depth(0); | ||
| 371 | VulkanFence gate_other(context, 0), gate_d(context, 0); | ||
| 372 | EXPECT(gate_other.semaphore() && gate_d.semaphore()); | ||
| 373 | auto empty_other = std::make_shared<CommandList>(); | ||
| 374 | EXPECT(empty_other->close()); | ||
| 375 | std::shared_ptr<const CommandList> s_other = empty_other; | ||
| 376 | |||
| 377 | std::memset(db, 0, kBufBytes); | ||
| 378 | std::atomic<bool> in_pumper_before{false}; | ||
| 379 | std::atomic<bool> release_pumper{false}; | ||
| 380 | std::atomic<unsigned> main_before_calls{0}; | ||
| 381 | const std::thread::id main_tid = std::this_thread::get_id(); | ||
| 382 | context.set_before_execute([&] { | ||
| 383 | if (std::this_thread::get_id() == main_tid) { | ||
| 384 | main_before_calls.fetch_add(1, std::memory_order_acq_rel); | ||
| 385 | } else { | ||
| 386 | in_pumper_before.store(true, std::memory_order_release); | ||
| 387 | while (!release_pumper.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 388 | } | ||
| 389 | }); | ||
| 390 | |||
| 391 | other_q.wait(gate_other.semaphore(), 1); | ||
| 392 | EXPECT(other_q.execute({&s_other, 1}) == ReplayResult::success); | ||
| 393 | queue.wait(gate_d.semaphore(), 1); | ||
| 394 | EXPECT(queue.execute({&c1, 1}) == ReplayResult::success); | ||
| 395 | |||
| 396 | // Pumper thread holds QueueHolds::pump_mutex_ while blocked inside other_q's before_execute_(). | ||
| 397 | std::thread pumper([&] { (void)gate_other.signal(1); }); | ||
| 398 | while (!in_pumper_before.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 399 | |||
| 400 | // Signal gate_d: QueueHolds::signaled() marks c1 ready, while pump_ready() returns immediately because | ||
| 401 | // pumper holds pump_mutex_. Thus queue has executing_ == false, !held_.empty(), and ready(held_.front()). | ||
| 402 | EXPECT(gate_d.signal(1)); | ||
| 403 | EXPECT(main_before_calls.load(std::memory_order_acquire) == 0); | ||
| 404 | |||
| 405 | // Sync execute(c2) must drain c1 and execute c2 synchronously before returning (while pumper still holds pump). | ||
| 406 | EXPECT(queue.execute({&c2, 1}) == ReplayResult::success); | ||
| 407 | EXPECT(main_before_calls.load(std::memory_order_acquire) == 2); | ||
| 408 | EXPECT(queue.idle()); | ||
| 409 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[0] == 0x22); | ||
| 410 | |||
| 411 | release_pumper.store(true, std::memory_order_release); | ||
| 412 | pumper.join(); | ||
| 413 | EXPECT(other_q.idle()); | ||
| 414 | std::printf("queue sync: exec_cv_ sync wait, unlocked must_hold, releaser-drain sync, ready-front drain, zero-copy Reset ok\n"); | ||
| 415 | } | ||
| 416 | |||
| 417 | // 2. after_held() must wait behind an active unheld synchronous execute() and an active unheld synchronous | ||
| 418 | // signal() (active_seq_ tracking via oldest_held_seq()). | ||
| 419 | { | ||
| 420 | VulkanContext context(d.device_proc, d.device, true); | ||
| 421 | EXPECT(context.loaded()); | ||
| 422 | VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | ||
| 423 | EXPECT(queue.ok()); | ||
| 424 | queue.set_async_depth(0); | ||
| 425 | |||
| 426 | auto empty = std::make_shared<CommandList>(); | ||
| 427 | EXPECT(empty->close()); | ||
| 428 | std::shared_ptr<const CommandList> snap = empty; | ||
| 429 | |||
| 430 | // (a) Active unheld sync execute(). | ||
| 431 | std::atomic<bool> in_before{false}, release_before{false}; | ||
| 432 | std::atomic<unsigned> after_calls{0}; | ||
| 433 | context.set_before_execute([&] { | ||
| 434 | in_before.store(true, std::memory_order_release); | ||
| 435 | while (!release_before.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 436 | }); | ||
| 437 | context.set_after_execute([&] { after_calls.fetch_add(1, std::memory_order_acq_rel); }); | ||
| 438 | |||
| 439 | ReplayResult res = ReplayResult::unsupported; | ||
| 440 | std::thread exec_t([&] { res = queue.execute({&snap, 1}); }); | ||
| 441 | while (!in_before.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 442 | |||
| 443 | std::atomic<bool> barrier_ran{false}; | ||
| 444 | std::atomic<unsigned> barrier_saw_after{0}; | ||
| 445 | int owner_a = 0; | ||
| 446 | context.holds().after_held(&owner_a, [&] { | ||
| 447 | barrier_saw_after.store(after_calls.load(std::memory_order_acquire), std::memory_order_release); | ||
| 448 | barrier_ran.store(true, std::memory_order_release); | ||
| 449 | }); | ||
| 450 | EXPECT(!barrier_ran.load(std::memory_order_acquire)); | ||
| 451 | |||
| 452 | release_before.store(true, std::memory_order_release); | ||
| 453 | exec_t.join(); | ||
| 454 | EXPECT(res == ReplayResult::success); | ||
| 455 | EXPECT(barrier_ran.load(std::memory_order_acquire)); | ||
| 456 | EXPECT(barrier_saw_after.load(std::memory_order_acquire) == 1); | ||
| 457 | |||
| 458 | // (b) Active unheld sync signal(). | ||
| 459 | context.set_before_execute({}); | ||
| 460 | context.set_after_execute({}); | ||
| 461 | VulkanFence sig_fence(context, 0); | ||
| 462 | EXPECT(sig_fence.semaphore() != VK_NULL_HANDLE); | ||
| 463 | |||
| 464 | std::atomic<bool> sig_ok{false}; | ||
| 465 | std::atomic<bool> sig_barrier_ran{false}; | ||
| 466 | std::atomic<bool> sig_barrier_saw_signaled{false}; | ||
| 467 | int owner_b = 0; | ||
| 468 | const std::uint64_t seq_before = context.holds().current_seq(); | ||
| 469 | std::thread sig_t; | ||
| 470 | { | ||
| 471 | std::lock_guard hold_submit(context.submit_mutex()); | ||
| 472 | sig_t = std::thread([&] { sig_ok.store(queue.signal(sig_fence.semaphore(), 1), std::memory_order_release); }); | ||
| 473 | // Wait until signal_sync() has acquired active_seq_ under state_mutex_ and is blocked in submit(). | ||
| 474 | while (context.holds().current_seq() == seq_before) std::this_thread::yield(); | ||
| 475 | |||
| 476 | context.holds().after_held(&owner_b, [&] { | ||
| 477 | sig_barrier_saw_signaled.store(!context.holds().pending(sig_fence.semaphore(), 1), | ||
| 478 | std::memory_order_release); | ||
| 479 | sig_barrier_ran.store(true, std::memory_order_release); | ||
| 480 | }); | ||
| 481 | EXPECT(!sig_barrier_ran.load(std::memory_order_acquire)); | ||
| 482 | } | ||
| 483 | sig_t.join(); | ||
| 484 | EXPECT(sig_ok.load(std::memory_order_acquire)); | ||
| 485 | EXPECT(sig_barrier_ran.load(std::memory_order_acquire)); | ||
| 486 | EXPECT(sig_barrier_saw_signaled.load(std::memory_order_acquire)); | ||
| 487 | EXPECT(sig_fence.wait(1, 5'000'000'000ull)); | ||
| 488 | EXPECT(queue.idle()); | ||
| 489 | std::printf("queue active_seq_: after_held waits behind active unheld execute() and signal() ok\n"); | ||
| 283 | } | 490 | } |
| 284 | 491 | ||
| 285 | // 2. Held batches behind a fence gate coalesce consecutive ready batches into ONE before_execute_() call, | 492 | // 3. Held batches drain in strict FIFO order with before_execute_() called once per batch (no cross-batch |
| 286 | // while an intervening Signal or Wait stops coalescing. Also verify invalid-batch fallback inside a | 493 | // coalescing), and an invalid held batch reports its failure without dropping valid peers. |
| 287 | // coalesced group does not drop valid peers. | ||
| 288 | { | 494 | { |
| 289 | VulkanContext context(d.device_proc, d.device, true); | 495 | VulkanContext context(d.device_proc, d.device, true); |
| 290 | EXPECT(context.loaded()); | 496 | EXPECT(context.loaded()); |
| 291 | VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | 497 | VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct); |
| 292 | VulkanQueue copy_q(context, d.queue, d.family, VulkanQueue::QueueKind::copy); | 498 | VulkanQueue copy_q(context, d.queue, d.family, VulkanQueue::QueueKind::copy); |
| 293 | EXPECT(queue.ok() && copy_q.ok()); | 499 | EXPECT(queue.ok() && copy_q.ok()); |
| 500 | queue.set_async_depth(0); | ||
| 501 | copy_q.set_async_depth(0); | ||
| 294 | 502 | ||
| 295 | void* src_map = nullptr; | 503 | void* src_map = nullptr; |
| 296 | void* dst_map = nullptr; | 504 | void* dst_map = nullptr; |
| @@ -331,13 +539,13 @@ int queue_async_and_coalescing() { | |||
| 331 | } | 539 | } |
| 332 | 540 | ||
| 333 | queue.wait(gate.semaphore(), 1); | 541 | queue.wait(gate.semaphore(), 1); |
| 334 | EXPECT(queue.execute({&steps[0], 1}) == ReplayResult::success); // group 1: steps 0, 1, 2 | 542 | EXPECT(queue.execute({&steps[0], 1}) == ReplayResult::success); |
| 335 | EXPECT(queue.execute({&steps[1], 1}) == ReplayResult::success); | 543 | EXPECT(queue.execute({&steps[1], 1}) == ReplayResult::success); |
| 336 | EXPECT(queue.execute({&steps[2], 1}) == ReplayResult::success); | 544 | EXPECT(queue.execute({&steps[2], 1}) == ReplayResult::success); |
| 337 | EXPECT(queue.signal(mid.semaphore(), 1)); // Signal stops coalescing | 545 | EXPECT(queue.signal(mid.semaphore(), 1)); |
| 338 | EXPECT(queue.execute({&steps[3], 1}) == ReplayResult::success); // group 2: step 3 alone | 546 | EXPECT(queue.execute({&steps[3], 1}) == ReplayResult::success); |
| 339 | queue.wait(mid.semaphore(), 1); // Wait stops coalescing | 547 | queue.wait(mid.semaphore(), 1); |
| 340 | EXPECT(queue.execute({&steps[4], 1}) == ReplayResult::success); // group 3: steps 4, 5 | 548 | EXPECT(queue.execute({&steps[4], 1}) == ReplayResult::success); |
| 341 | EXPECT(queue.execute({&steps[5], 1}) == ReplayResult::success); | 549 | EXPECT(queue.execute({&steps[5], 1}) == ReplayResult::success); |
| 342 | EXPECT(queue.signal(done.semaphore(), 1)); | 550 | EXPECT(queue.signal(done.semaphore(), 1)); |
| 343 | 551 | ||
| @@ -346,12 +554,12 @@ int queue_async_and_coalescing() { | |||
| 346 | EXPECT(done.wait(1, 5'000'000'000ull)); | 554 | EXPECT(done.wait(1, 5'000'000'000ull)); |
| 347 | EXPECT(queue.idle()); | 555 | EXPECT(queue.idle()); |
| 348 | 556 | ||
| 349 | // Exactly 3 before_execute_() calls: {0,1,2}, {3}, {4,5}. | 557 | // Every held batch runs its own execute_now() / before_execute_(): 6 batches -> 6 calls. |
| 350 | EXPECT(before_calls.load() == 3); | 558 | EXPECT(before_calls.load() == 6); |
| 351 | for (int i = 0; i < 6; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xA1 + i)); | 559 | for (int i = 0; i < 6; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xA1 + i)); |
| 352 | EXPECT(db[0] == std::uint8_t(0xA6)); // strict FIFO through all 6 batches | 560 | EXPECT(db[0] == std::uint8_t(0xA6)); // strict FIFO through all 6 batches |
| 353 | 561 | ||
| 354 | // Coalesced fallback: [valid, invalid ClearRenderTarget on copy queue, valid] held behind gate2. | 562 | // Invalid held batch [valid, invalid ClearRenderTarget on copy queue, valid] must not drop valid peers. |
| 355 | VulkanFence gate2(context, 0), done2(context, 0); | 563 | VulkanFence gate2(context, 0), done2(context, 0); |
| 356 | EXPECT(gate2.semaphore() && done2.semaphore()); | 564 | EXPECT(gate2.semaphore() && done2.semaphore()); |
| 357 | std::memset(db, 0, kBufBytes); | 565 | std::memset(db, 0, kBufBytes); |
| @@ -371,11 +579,11 @@ int queue_async_and_coalescing() { | |||
| 371 | EXPECT(copy_q.idle()); | 579 | EXPECT(copy_q.idle()); |
| 372 | EXPECT(copy_q.take_held_failure() == ReplayResult::unsupported); | 580 | EXPECT(copy_q.take_held_failure() == ReplayResult::unsupported); |
| 373 | EXPECT(db[16] == 0xA1 && db[32] == 0xA2 && db[0] == 0xA2); | 581 | EXPECT(db[16] == 0xA1 && db[32] == 0xA2 && db[0] == 0xA2); |
| 374 | std::printf("queue held coalescing: 6 batches -> 3 submits, Signal/Wait split, invalid fallback ok\n"); | 582 | std::printf("queue held FIFO drain: 6 batches -> 6 before_execute_ calls, invalid peer isolation ok\n"); |
| 375 | } | 583 | } |
| 376 | 584 | ||
| 377 | // 3. LEONIDA_SUBMIT_ASYNC (per-queue async worker depth=4): inbox coalescing, Present barrier split, | 585 | // 4. LEONIDA_SUBMIT_ASYNC (per-queue async worker depth=4): per-batch before_execute_() (no coalescing), |
| 378 | // held-batch release through worker, and zero-copy CommandList Reset() while queued. | 586 | // Present barrier ordering, held-batch release through worker, and zero-copy CommandList Reset() while queued. |
| 379 | { | 587 | { |
| 380 | VulkanContext context(d.device_proc, d.device, true); | 588 | VulkanContext context(d.device_proc, d.device, true); |
| 381 | EXPECT(context.loaded()); | 589 | EXPECT(context.loaded()); |
| @@ -423,7 +631,7 @@ int queue_async_and_coalescing() { | |||
| 423 | EXPECT(queue.execute({&s0, 1}) == ReplayResult::success); | 631 | EXPECT(queue.execute({&s0, 1}) == ReplayResult::success); |
| 424 | while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield(); | 632 | while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield(); |
| 425 | 633 | ||
| 426 | // Enqueue c1, c2 (will coalesce), then a Present after_held barrier, then c3 (must NOT coalesce past barrier). | 634 | // Enqueue c1, c2, then a Present after_held barrier, then c3. |
| 427 | EXPECT(queue.execute({&s1, 1}) == ReplayResult::success); | 635 | EXPECT(queue.execute({&s1, 1}) == ReplayResult::success); |
| 428 | EXPECT(queue.execute({&s2, 1}) == ReplayResult::success); | 636 | EXPECT(queue.execute({&s2, 1}) == ReplayResult::success); |
| 429 | EXPECT(c1.use_count() > 1 && c2.use_count() > 1); // queued zero-copy | 637 | EXPECT(c1.use_count() > 1 && c2.use_count() > 1); // queued zero-copy |
| @@ -450,12 +658,12 @@ int queue_async_and_coalescing() { | |||
| 450 | EXPECT(queue.idle()); | 658 | EXPECT(queue.idle()); |
| 451 | EXPECT(barrier_ran.load(std::memory_order_acquire)); | 659 | EXPECT(barrier_ran.load(std::memory_order_acquire)); |
| 452 | 660 | ||
| 453 | // 3 before_execute_() calls total: {c0}, {c1, c2}, {c3}; barrier ran after {c1, c2} (count == 2), before {c3}! | 661 | // 4 before_execute_() calls total: {c0}, {c1}, {c2}, {c3}; barrier ran after c2 (count == 3), before c3! |
| 454 | EXPECT(before_calls.load() == 3); | 662 | EXPECT(before_calls.load() == 4); |
| 455 | EXPECT(barrier_seen_before_calls.load() == 2); | 663 | EXPECT(barrier_seen_before_calls.load() == 3); |
| 456 | EXPECT(db[16] == 0xC1 && db[32] == 0xC2 && db[48] == 0xC3 && db[64] == 0xC4); | 664 | EXPECT(db[16] == 0xC1 && db[32] == 0xC2 && db[48] == 0xC3 && db[64] == 0xC4); |
| 457 | 665 | ||
| 458 | // Held batches behind a gate in async mode: release_ready() wakes worker and coalesces them. | 666 | // Held batches behind a gate in async mode: release_ready() wakes worker and runs before_execute_() per batch. |
| 459 | before_calls.store(0); | 667 | before_calls.store(0); |
| 460 | std::memset(db, 0, kBufBytes); | 668 | std::memset(db, 0, kBufBytes); |
| 461 | VulkanFence gate(context, 0), done2(context, 0); | 669 | VulkanFence gate(context, 0), done2(context, 0); |
| @@ -469,12 +677,12 @@ int queue_async_and_coalescing() { | |||
| 469 | EXPECT(gate.signal(1)); | 677 | EXPECT(gate.signal(1)); |
| 470 | EXPECT(done2.wait(1, 5'000'000'000ull)); | 678 | EXPECT(done2.wait(1, 5'000'000'000ull)); |
| 471 | EXPECT(queue.idle()); | 679 | EXPECT(queue.idle()); |
| 472 | EXPECT(before_calls.load() == 1); | 680 | EXPECT(before_calls.load() == 2); |
| 473 | EXPECT(db[16] == 0xC1 && db[48] == 0xC3); | 681 | EXPECT(db[16] == 0xC1 && db[48] == 0xC3); |
| 474 | std::printf("queue async (depth=4): inbox coalescing, Present barrier split, gate release ok\n"); | 682 | std::printf("queue async (depth=4): per-batch before_execute_, Present barrier order, gate release ok\n"); |
| 475 | } | 683 | } |
| 476 | 684 | ||
| 477 | // 4. Async queue progress tracking (LEONIDA_SUBMIT_ASYNC=4 + queued Present + busy producer worker + genuine | 685 | // 5. Async queue progress tracking (LEONIDA_SUBMIT_ASYNC=4 + queued Present + busy producer worker + genuine |
| 478 | // deadlock): | 686 | // deadlock): |
| 479 | // (a) While the direct producer async worker is busy inside before_execute_() with a WriteGroup Signal | 687 | // (a) While the direct producer async worker is busy inside before_execute_() with a WriteGroup Signal |
| 480 | // still queued in its inbox_, a consumer compute gate with a Present queued behind it must NOT be | 688 | // still queued in its inbox_, a consumer compute gate with a Present queued behind it must NOT be |
| @@ -617,7 +825,7 @@ int queue_async_and_coalescing() { | |||
| 617 | } // namespace | 825 | } // namespace |
| 618 | 826 | ||
| 619 | int main() { | 827 | int main() { |
| 620 | if (scripted() || submissions() || queue_async_and_coalescing()) return 1; | 828 | if (scripted() || submissions() || queue_sync_and_async()) return 1; |
| 621 | std::printf("submit_frame_test: OK\n"); | 829 | std::printf("submit_frame_test: OK\n"); |
| 622 | return 0; | 830 | return 0; |
| 623 | } | 831 | } |