diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 17:53:44 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-27 17:53:44 -0500 |
| commit | 34173ab04b622ec5004f19b25d7fd4449ec7c0da (patch) | |
| tree | 2b81b02b039ce7580af7089e0dee4bb41152a95b /runtime/test/submit_frame_test.cpp | |
| parent | aa5915112d4bcca2d28e86c525f5679b8bccc6cb (diff) | |
runtime: Stage 4 same-queue submit merge, async geometry epoch, and O(1) floor
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
| -rw-r--r-- | runtime/test/submit_frame_test.cpp | 285 |
1 files changed, 270 insertions, 15 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp index 5619a4c66..fe596a914 100644 --- a/runtime/test/submit_frame_test.cpp +++ b/runtime/test/submit_frame_test.cpp | |||
| @@ -313,15 +313,16 @@ int queue_sync_and_async() { | |||
| 313 | EXPECT(gate.signal(1)); | 313 | EXPECT(gate.signal(1)); |
| 314 | EXPECT(done.wait(1, 5'000'000'000ull)); | 314 | EXPECT(done.wait(1, 5'000'000'000ull)); |
| 315 | EXPECT(queue.idle()); | 315 | EXPECT(queue.idle()); |
| 316 | EXPECT(before_calls.load(std::memory_order_acquire) == 3); // one before_execute_() per batch | 316 | // b1 ran first (1), then held {b2, b3} coalesced into one before_execute_() + one submit (2). |
| 317 | EXPECT(before_calls.load(std::memory_order_acquire) == 2); | ||
| 317 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33 && db[0] == 0x33); | 318 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33 && db[0] == 0x33); |
| 318 | s2.reset(); | 319 | s2.reset(); |
| 319 | s3.reset(); | 320 | s3.reset(); |
| 320 | EXPECT(b1.use_count() == 1 && b3.use_count() == 1); | 321 | EXPECT(b1.use_count() == 1 && b3.use_count() == 1); |
| 321 | EXPECT(b3->reset()); // steady-state reuse with zero allocation | 322 | EXPECT(b3->reset()); // steady-state reuse with zero allocation |
| 322 | 323 | ||
| 323 | // (c) Sync execute() arriving while a releaser drains [b1 (running), b2 (ready in held_)] must wait for both | 324 | // (c) Sync execute() arriving while a releaser drains [b1, b2 (coalesced in held_)] must wait for the held |
| 324 | // ready held batches to drain and run before_execute_() for b3 synchronously before returning. | 325 | // drain to finish and run before_execute_() for b3 synchronously before returning. |
| 325 | auto b2_fresh = std::make_shared<CommandList>(); | 326 | 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 | EXPECT(b2_fresh->copy_buffer({2, 32, 1, 16, 1}) && b2_fresh->copy_buffer({2, 0, 1, 16, 1}) && |
| 327 | b2_fresh->close()); | 328 | b2_fresh->close()); |
| @@ -358,7 +359,7 @@ int queue_sync_and_async() { | |||
| 358 | releaser.join(); | 359 | releaser.join(); |
| 359 | title.join(); | 360 | title.join(); |
| 360 | EXPECT(title_res == ReplayResult::success); | 361 | EXPECT(title_res == ReplayResult::success); |
| 361 | EXPECT(title_before_at_return.load(std::memory_order_acquire) == 3); | 362 | EXPECT(title_before_at_return.load(std::memory_order_acquire) == 2); |
| 362 | EXPECT(queue.idle()); | 363 | EXPECT(queue.idle()); |
| 363 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33 && db[0] == 0x33); | 364 | EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33 && db[0] == 0x33); |
| 364 | 365 | ||
| @@ -554,8 +555,9 @@ int queue_sync_and_async() { | |||
| 554 | EXPECT(done.wait(1, 5'000'000'000ull)); | 555 | EXPECT(done.wait(1, 5'000'000'000ull)); |
| 555 | EXPECT(queue.idle()); | 556 | EXPECT(queue.idle()); |
| 556 | 557 | ||
| 557 | // Every held batch runs its own execute_now() / before_execute_(): 6 batches -> 6 calls. | 558 | // Held consecutive batches coalesce across {steps[0..2]} (1), split at Signal(mid), run {steps[3]} (2), |
| 558 | EXPECT(before_calls.load() == 6); | 559 | // split at Wait(mid), and coalesce {steps[4..5]} (3): 6 batches -> 3 before_execute_() calls! |
| 560 | EXPECT(before_calls.load() == 3); | ||
| 559 | for (int i = 0; i < 6; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xA1 + i)); | 561 | for (int i = 0; i < 6; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xA1 + i)); |
| 560 | EXPECT(db[0] == std::uint8_t(0xA6)); // strict FIFO through all 6 batches | 562 | EXPECT(db[0] == std::uint8_t(0xA6)); // strict FIFO through all 6 batches |
| 561 | 563 | ||
| @@ -579,11 +581,11 @@ int queue_sync_and_async() { | |||
| 579 | EXPECT(copy_q.idle()); | 581 | EXPECT(copy_q.idle()); |
| 580 | EXPECT(copy_q.take_held_failure() == ReplayResult::unsupported); | 582 | EXPECT(copy_q.take_held_failure() == ReplayResult::unsupported); |
| 581 | EXPECT(db[16] == 0xA1 && db[32] == 0xA2 && db[0] == 0xA2); | 583 | EXPECT(db[16] == 0xA1 && db[32] == 0xA2 && db[0] == 0xA2); |
| 582 | std::printf("queue held FIFO drain: 6 batches -> 6 before_execute_ calls, invalid peer isolation ok\n"); | 584 | std::printf("queue held FIFO drain: 6 batches -> 3 merged before_execute_ calls, invalid peer isolation ok\n"); |
| 583 | } | 585 | } |
| 584 | 586 | ||
| 585 | // 4. LEONIDA_SUBMIT_ASYNC (per-queue async worker depth=4): per-batch before_execute_() (no coalescing), | 587 | // 4. LEONIDA_SUBMIT_ASYNC (per-queue async worker depth=4): same-queue inbox coalescing, Present barrier split & |
| 586 | // Present barrier ordering, held-batch release through worker, and zero-copy CommandList Reset() while queued. | 588 | // ordering, held-batch release coalescing through worker, and zero-copy CommandList Reset() while queued. |
| 587 | { | 589 | { |
| 588 | VulkanContext context(d.device_proc, d.device, true); | 590 | VulkanContext context(d.device_proc, d.device, true); |
| 589 | EXPECT(context.loaded()); | 591 | EXPECT(context.loaded()); |
| @@ -658,16 +660,17 @@ int queue_sync_and_async() { | |||
| 658 | EXPECT(queue.idle()); | 660 | EXPECT(queue.idle()); |
| 659 | EXPECT(barrier_ran.load(std::memory_order_acquire)); | 661 | EXPECT(barrier_ran.load(std::memory_order_acquire)); |
| 660 | 662 | ||
| 661 | // 4 before_execute_() calls total: {c0}, {c1}, {c2}, {c3}; barrier ran after c2 (count == 3), before c3! | 663 | // 3 before_execute_() calls total: {c0}, coalesced {c1, c2}, {c3}; barrier ran after {c1, c2} (count == 2)! |
| 662 | EXPECT(before_calls.load() == 4); | 664 | EXPECT(before_calls.load() == 3); |
| 663 | EXPECT(barrier_seen_before_calls.load() == 3); | 665 | EXPECT(barrier_seen_before_calls.load() == 2); |
| 664 | EXPECT(db[16] == 0xC1 && db[32] == 0xC2 && db[48] == 0xC3 && db[64] == 0xC4); | 666 | EXPECT(db[16] == 0xC1 && db[32] == 0xC2 && db[48] == 0xC3 && db[64] == 0xC4); |
| 665 | 667 | ||
| 666 | // Held batches behind a gate in async mode: release_ready() wakes worker and runs before_execute_() per batch. | 668 | // Held batches behind a gate in async mode: release_ready() wakes worker and coalesces {s0, s2} into 1 submit. |
| 667 | before_calls.store(0); | 669 | before_calls.store(0); |
| 668 | std::memset(db, 0, kBufBytes); | 670 | std::memset(db, 0, kBufBytes); |
| 669 | VulkanFence gate(context, 0), done2(context, 0); | 671 | VulkanFence gate(context, 0), done2(context, 0); |
| 670 | EXPECT(gate.semaphore() && done2.semaphore()); | 672 | EXPECT(gate.semaphore() && done2.semaphore()); |
| 673 | const std::uint64_t title_replay_before = load(sp::frame_totals().phase_ns[sp::replay]); | ||
| 671 | queue.wait(gate.semaphore(), 1); | 674 | queue.wait(gate.semaphore(), 1); |
| 672 | EXPECT(queue.execute({&s0, 1}) == ReplayResult::success); | 675 | EXPECT(queue.execute({&s0, 1}) == ReplayResult::success); |
| 673 | EXPECT(queue.execute({&s2, 1}) == ReplayResult::success); | 676 | EXPECT(queue.execute({&s2, 1}) == ReplayResult::success); |
| @@ -677,9 +680,11 @@ int queue_sync_and_async() { | |||
| 677 | EXPECT(gate.signal(1)); | 680 | EXPECT(gate.signal(1)); |
| 678 | EXPECT(done2.wait(1, 5'000'000'000ull)); | 681 | EXPECT(done2.wait(1, 5'000'000'000ull)); |
| 679 | EXPECT(queue.idle()); | 682 | EXPECT(queue.idle()); |
| 680 | EXPECT(before_calls.load() == 2); | 683 | EXPECT(before_calls.load() == 1); |
| 681 | EXPECT(db[16] == 0xC1 && db[48] == 0xC3); | 684 | EXPECT(db[16] == 0xC1 && db[48] == 0xC3); |
| 682 | std::printf("queue async (depth=4): per-batch before_execute_, Present barrier order, gate release ok\n"); | 685 | // WorkerScope(worker_submit) covers release_held() on async_worker, so title-thread replay_ns stays unchanged! |
| 686 | EXPECT(load(sp::frame_totals().phase_ns[sp::replay]) == title_replay_before); | ||
| 687 | std::printf("queue async (depth=4): inbox coalescing, Present barrier split, gate release & WorkerScope ok\n"); | ||
| 683 | } | 688 | } |
| 684 | 689 | ||
| 685 | // 5. Async queue progress tracking (LEONIDA_SUBMIT_ASYNC=4 + queued Present + busy producer worker + genuine | 690 | // 5. Async queue progress tracking (LEONIDA_SUBMIT_ASYNC=4 + queued Present + busy producer worker + genuine |
| @@ -819,6 +824,256 @@ int queue_sync_and_async() { | |||
| 819 | std::printf("queue async progress: held gate protected during busy worker/executing_, genuine deadlock breaks ok\n"); | 824 | std::printf("queue async progress: held gate protected during busy worker/executing_, genuine deadlock breaks ok\n"); |
| 820 | } | 825 | } |
| 821 | 826 | ||
| 827 | // 6. Explicit async inbox submit merging + vkQueueSubmit count + Signal / Wait / Present splits: | ||
| 828 | // Consecutive Call::Kind::execute items in async_worker() coalesce into ONE before_execute_() + ONE | ||
| 829 | // command-buffer vkQueueSubmit, while intervening Signal, Wait, and Present (after_held) each split cleanly. | ||
| 830 | { | ||
| 831 | static PFN_vkQueueSubmit s_real_submit = nullptr; | ||
| 832 | static std::atomic<unsigned> s_cb_submits{0}; | ||
| 833 | struct HookSubmit { | ||
| 834 | static VKAPI_ATTR VkResult VKAPI_CALL call(VkQueue q, std::uint32_t count, const VkSubmitInfo* infos, | ||
| 835 | VkFence fence) { | ||
| 836 | for (std::uint32_t i = 0; i < count; ++i) | ||
| 837 | if (infos[i].commandBufferCount > 0) s_cb_submits.fetch_add(1, std::memory_order_relaxed); | ||
| 838 | return s_real_submit(q, count, infos, fence); | ||
| 839 | } | ||
| 840 | }; | ||
| 841 | |||
| 842 | VulkanContext context(d.device_proc, d.device, true); | ||
| 843 | EXPECT(context.loaded()); | ||
| 844 | s_real_submit = context.vk.vkQueueSubmit; | ||
| 845 | s_cb_submits.store(0, std::memory_order_relaxed); | ||
| 846 | context.vk.vkQueueSubmit = &HookSubmit::call; | ||
| 847 | |||
| 848 | VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | ||
| 849 | EXPECT(queue.ok()); | ||
| 850 | queue.set_async_depth(12); | ||
| 851 | |||
| 852 | void* src_map = nullptr; | ||
| 853 | void* dst_map = nullptr; | ||
| 854 | VkBuffer src = d.buffer(kBufBytes, usage, &src_map); | ||
| 855 | VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map); | ||
| 856 | EXPECT(src && dst); | ||
| 857 | auto* sb = static_cast<std::uint8_t*>(src_map); | ||
| 858 | auto* db = static_cast<std::uint8_t*>(dst_map); | ||
| 859 | std::memset(db, 0, kBufBytes); | ||
| 860 | for (int i = 0; i < 9; ++i) sb[i * 16] = std::uint8_t(0xD0 + i); | ||
| 861 | |||
| 862 | ResourceBinding r_src{}, r_dst{}; | ||
| 863 | r_src.buffer = src; | ||
| 864 | r_src.size = kBufBytes; | ||
| 865 | r_dst.buffer = dst; | ||
| 866 | r_dst.size = kBufBytes; | ||
| 867 | r_dst.host_readable = true; | ||
| 868 | context.add_resource(1, r_src); | ||
| 869 | context.add_resource(2, r_dst); | ||
| 870 | |||
| 871 | std::atomic<unsigned> before_calls{0}; | ||
| 872 | std::atomic<bool> in_first{false}; | ||
| 873 | std::atomic<bool> go_first{false}; | ||
| 874 | context.set_before_execute([&] { | ||
| 875 | const unsigned n = before_calls.fetch_add(1, std::memory_order_acq_rel) + 1; | ||
| 876 | if (n == 1) { | ||
| 877 | in_first.store(true, std::memory_order_release); | ||
| 878 | while (!go_first.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 879 | } | ||
| 880 | }); | ||
| 881 | |||
| 882 | std::shared_ptr<const CommandList> steps[9]; | ||
| 883 | for (int i = 0; i < 9; ++i) { | ||
| 884 | auto l = std::make_shared<CommandList>(); | ||
| 885 | EXPECT(l->copy_buffer({2, std::uint64_t((i + 1) * 16), 1, std::uint64_t(i * 16), 1}) && | ||
| 886 | l->copy_buffer({2, 0, 1, std::uint64_t(i * 16), 1}) && l->close()); | ||
| 887 | steps[i] = std::move(l); | ||
| 888 | } | ||
| 889 | |||
| 890 | VulkanFence mid(context, 0), done(context, 0); | ||
| 891 | EXPECT(mid.semaphore() && done.semaphore()); | ||
| 892 | |||
| 893 | // Batch 0 starts on worker and holds in before_execute_() while the remaining 8 batches queue in inbox_. | ||
| 894 | EXPECT(queue.execute({&steps[0], 1}) == ReplayResult::success); | ||
| 895 | while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 896 | |||
| 897 | // Group A: 3 consecutive execute batches -> coalesces into 1 submit (#2). | ||
| 898 | EXPECT(queue.execute({&steps[1], 1}) == ReplayResult::success); | ||
| 899 | EXPECT(queue.execute({&steps[2], 1}) == ReplayResult::success); | ||
| 900 | EXPECT(queue.execute({&steps[3], 1}) == ReplayResult::success); | ||
| 901 | // Split 1: Signal(mid, 1). | ||
| 902 | EXPECT(queue.signal(mid.semaphore(), 1)); | ||
| 903 | // Group B: 2 consecutive execute batches -> coalesces into 1 submit (#3). | ||
| 904 | EXPECT(queue.execute({&steps[4], 1}) == ReplayResult::success); | ||
| 905 | EXPECT(queue.execute({&steps[5], 1}) == ReplayResult::success); | ||
| 906 | // Split 2: Wait(mid, 1). | ||
| 907 | queue.wait(mid.semaphore(), 1); | ||
| 908 | // Group C: 2 consecutive execute batches -> coalesces into 1 submit (#4). | ||
| 909 | EXPECT(queue.execute({&steps[6], 1}) == ReplayResult::success); | ||
| 910 | EXPECT(queue.execute({&steps[7], 1}) == ReplayResult::success); | ||
| 911 | // Split 3: Present barrier (after_held). | ||
| 912 | std::atomic<unsigned> present_saw_submits{0}; | ||
| 913 | int present_owner = 0; | ||
| 914 | context.holds().after_held(&present_owner, [&] { | ||
| 915 | present_saw_submits.store(s_cb_submits.load(std::memory_order_acquire), std::memory_order_release); | ||
| 916 | }); | ||
| 917 | // Group D: 1 execute batch after Present -> 1 submit (#5). | ||
| 918 | EXPECT(queue.execute({&steps[8], 1}) == ReplayResult::success); | ||
| 919 | EXPECT(queue.signal(done.semaphore(), 1)); | ||
| 920 | |||
| 921 | go_first.store(true, std::memory_order_release); | ||
| 922 | EXPECT(done.wait(1, 5'000'000'000ull)); | ||
| 923 | EXPECT(queue.idle()); | ||
| 924 | |||
| 925 | // 9 execute calls coalesced into exactly 5 before_execute_() calls AND 5 command-buffer vkQueueSubmits! | ||
| 926 | EXPECT(before_calls.load(std::memory_order_acquire) == 5); | ||
| 927 | EXPECT(s_cb_submits.load(std::memory_order_acquire) == 5); | ||
| 928 | EXPECT(present_saw_submits.load(std::memory_order_acquire) == 4); | ||
| 929 | for (int i = 0; i < 9; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xD0 + i)); | ||
| 930 | EXPECT(db[0] == std::uint8_t(0xD8)); // exact FIFO overwrite order preserved across inter-batch barriers | ||
| 931 | std::printf("queue async submit merging: 9 batches -> 5 submits, split cleanly on Signal/Wait/Present ok\n"); | ||
| 932 | } | ||
| 933 | |||
| 934 | // 7. ASYNC enqueue-serial retirement epoch contract (set_on_enqueue_execute / set_on_finish_execute): | ||
| 935 | // A view retired with kAfterExecutes during batch 1's before_execute_() must NOT be stamped or reaped while | ||
| 936 | // an already-enqueued batch 2 is still waiting in async inbox_, even after batch 1's GPU work completes! | ||
| 937 | { | ||
| 938 | // Mirrors GeometryViews' exact execute_enqueue / retire(kAfterExecutes) / execute_end / reap state machine. | ||
| 939 | struct EpochTracker { | ||
| 940 | enum : std::uint64_t { kAfterExecutes = UINT64_MAX }; | ||
| 941 | struct Retired { | ||
| 942 | std::uint64_t id = 0; | ||
| 943 | std::uint64_t ticket = kAfterExecutes; | ||
| 944 | std::uint64_t epoch = 0; | ||
| 945 | }; | ||
| 946 | std::mutex mutex, execute_mutex; | ||
| 947 | std::vector<std::uint64_t> executing; | ||
| 948 | std::vector<Retired> retired; | ||
| 949 | std::uint64_t execute_serial = 0; | ||
| 950 | std::atomic<bool> unstamped{false}; | ||
| 951 | std::uint64_t reaped = 0; | ||
| 952 | |||
| 953 | std::uint64_t execute_enqueue() { | ||
| 954 | std::lock_guard lock(execute_mutex); | ||
| 955 | executing.push_back(++execute_serial); | ||
| 956 | return execute_serial; | ||
| 957 | } | ||
| 958 | void retire(std::uint64_t id) { | ||
| 959 | std::lock_guard lock(mutex); | ||
| 960 | std::uint64_t epoch = 0; | ||
| 961 | { | ||
| 962 | std::lock_guard exec(execute_mutex); | ||
| 963 | epoch = execute_serial; | ||
| 964 | } | ||
| 965 | unstamped.store(true, std::memory_order_release); | ||
| 966 | retired.push_back({id, kAfterExecutes, epoch}); | ||
| 967 | } | ||
| 968 | void execute_end(std::uint64_t serial, const std::function<std::uint64_t()>& submitted) { | ||
| 969 | if (!serial) return; | ||
| 970 | { | ||
| 971 | std::lock_guard exec(execute_mutex); | ||
| 972 | std::erase(executing, serial); | ||
| 973 | } | ||
| 974 | if (!unstamped.load(std::memory_order_acquire)) return; | ||
| 975 | std::lock_guard lock(mutex); | ||
| 976 | std::uint64_t oldest = kAfterExecutes; | ||
| 977 | { | ||
| 978 | std::lock_guard exec(execute_mutex); | ||
| 979 | for (const auto s : executing) oldest = std::min(oldest, s); | ||
| 980 | } | ||
| 981 | const std::uint64_t ticket = submitted(); | ||
| 982 | bool left = false; | ||
| 983 | for (auto& r : retired) { | ||
| 984 | if (r.ticket != kAfterExecutes) continue; | ||
| 985 | if (r.epoch < oldest) r.ticket = ticket; | ||
| 986 | else left = true; | ||
| 987 | } | ||
| 988 | unstamped.store(left, std::memory_order_release); | ||
| 989 | } | ||
| 990 | std::size_t reap(std::uint64_t completed) { | ||
| 991 | std::lock_guard lock(mutex); | ||
| 992 | std::size_t freed = 0, keep = 0; | ||
| 993 | for (std::size_t i = 0; i < retired.size(); ++i) { | ||
| 994 | if (retired[i].ticket != kAfterExecutes && retired[i].ticket <= completed) { | ||
| 995 | ++reaped; | ||
| 996 | ++freed; | ||
| 997 | } else { | ||
| 998 | if (keep != i) retired[keep] = retired[i]; | ||
| 999 | ++keep; | ||
| 1000 | } | ||
| 1001 | } | ||
| 1002 | retired.resize(keep); | ||
| 1003 | return freed; | ||
| 1004 | } | ||
| 1005 | } tracker; | ||
| 1006 | |||
| 1007 | VulkanContext context(d.device_proc, d.device, true); | ||
| 1008 | EXPECT(context.loaded()); | ||
| 1009 | context.set_on_enqueue_execute([&] { return tracker.execute_enqueue(); }); | ||
| 1010 | context.set_on_finish_execute( | ||
| 1011 | [&](std::uint64_t serial) { tracker.execute_end(serial, [&] { return context.submitted_retire_ticket(); }); }); | ||
| 1012 | |||
| 1013 | VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct); | ||
| 1014 | EXPECT(queue.ok()); | ||
| 1015 | queue.set_async_depth(4); | ||
| 1016 | |||
| 1017 | auto empty_list = std::make_shared<CommandList>(); | ||
| 1018 | EXPECT(empty_list->close()); | ||
| 1019 | std::shared_ptr<const CommandList> snap = empty_list; | ||
| 1020 | |||
| 1021 | std::atomic<unsigned> before_n{0}; | ||
| 1022 | std::atomic<bool> b1_entered{false}, b1_go{false}, b2_entered{false}, b2_go{false}; | ||
| 1023 | std::atomic<std::uint64_t> b1_seen_serial{0}, b2_seen_serial{0}; | ||
| 1024 | context.set_before_execute([&] { | ||
| 1025 | const unsigned n = before_n.fetch_add(1, std::memory_order_acq_rel) + 1; | ||
| 1026 | if (n == 1) { | ||
| 1027 | b1_seen_serial.store(VulkanContext::current_enqueue_serial(), std::memory_order_release); | ||
| 1028 | b1_entered.store(true, std::memory_order_release); | ||
| 1029 | while (!b1_go.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1030 | // Batch 1 retires view #1 AFTER batch 2 has already been enqueued in inbox_! | ||
| 1031 | tracker.retire(1); | ||
| 1032 | } else if (n == 2) { | ||
| 1033 | b2_seen_serial.store(VulkanContext::current_enqueue_serial(), std::memory_order_release); | ||
| 1034 | b2_entered.store(true, std::memory_order_release); | ||
| 1035 | while (!b2_go.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1036 | } | ||
| 1037 | }); | ||
| 1038 | |||
| 1039 | // 1. Enqueue batch 1 (serial 1); wait until worker enters batch 1's before_execute_(). | ||
| 1040 | EXPECT(queue.execute({&snap, 1}) == ReplayResult::success); | ||
| 1041 | while (!b1_entered.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1042 | EXPECT(b1_seen_serial.load(std::memory_order_acquire) == 1); | ||
| 1043 | |||
| 1044 | // 2. Title thread enqueues batch 2 (serial 2) + batch 3 (serial 3) into inbox_ while batch 1 is in flight! | ||
| 1045 | VulkanFence done(context, 0); | ||
| 1046 | EXPECT(done.semaphore() != VK_NULL_HANDLE); | ||
| 1047 | EXPECT(queue.execute({&snap, 1}) == ReplayResult::success); | ||
| 1048 | EXPECT(queue.execute({&snap, 1}) == ReplayResult::success); | ||
| 1049 | EXPECT(queue.signal(done.semaphore(), 1)); | ||
| 1050 | |||
| 1051 | // 3. Let batch 1 retire view #1, submit to Vulkan, and finish; worker then coalesces {batch 2, batch 3} and | ||
| 1052 | // pauses at n == 2 inside before_execute_(). | ||
| 1053 | b1_go.store(true, std::memory_order_release); | ||
| 1054 | while (!b2_entered.load(std::memory_order_acquire)) std::this_thread::yield(); | ||
| 1055 | EXPECT(b2_seen_serial.load(std::memory_order_acquire) == 2); | ||
| 1056 | |||
| 1057 | // Wait until batch 1's GPU submission has completely retired on the GPU! | ||
| 1058 | const std::uint64_t b1_ticket = context.submitted_retire_ticket(); | ||
| 1059 | EXPECT(b1_ticket >= 1); | ||
| 1060 | EXPECT(context.wait_retire_ticket(b1_ticket) >= b1_ticket); | ||
| 1061 | |||
| 1062 | // CRITICAL INVARIANT: Even though batch 1's GPU work is 100% done, view #1 MUST NOT be reaped because | ||
| 1063 | // {batch 2, batch 3} (enqueued BEFORE view #1 retired!) have not submitted yet! | ||
| 1064 | EXPECT(tracker.reap(context.completed_retire_ticket()) == 0); | ||
| 1065 | EXPECT(tracker.reaped == 0); | ||
| 1066 | |||
| 1067 | // 4. Release {batch 2, batch 3}: both serials 2 and 3 finish after the coalesced vkQueueSubmit, stamping | ||
| 1068 | // view #1 with the coalesced submit ticket; once GPU completes, view #1 reaps cleanly! | ||
| 1069 | b2_go.store(true, std::memory_order_release); | ||
| 1070 | EXPECT(done.wait(1, 5'000'000'000ull)); | ||
| 1071 | EXPECT(queue.idle()); | ||
| 1072 | EXPECT(tracker.reap(context.completed_retire_ticket()) == 1); | ||
| 1073 | EXPECT(tracker.reaped == 1); | ||
| 1074 | std::printf("queue async retirement epoch: view deferred across queued & coalesced inbox batches ok\n"); | ||
| 1075 | } | ||
| 1076 | |||
| 822 | return 0; | 1077 | return 0; |
| 823 | } | 1078 | } |
| 824 | 1079 | ||