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
blob: ba5d275c7bb9c1e986e59ed106ab167b2dd22900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// The "submit frame" and "submit floor" lines (runtime/src/submit_phases.h): their sums against the per-submission
// phases the "submit phases" line adds up.
//   1. A scripted run on the CPU, with known phase times: two frames, a submission on each of two queue kinds, a phase
//      on one of our threads (a WorkerScope), a title call (TitleCall) and a contended lock. The frame sums, the
//      split between title and own threads, the worst frame and the floor must come out as scripted.
//   2. Real submissions through VulkanQueue (empty lists; 77-free: without a Vulkan device this part is skipped and
//      the test still passes): the floor's phase sums must equal what the phase totals grew by, submission for
//      submission, and every one of them is a floor submission.
#include "vk_test_device.h"
#include "../src/vulkan_backend.h"

#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>

#define EXPECT(condition) do { \
    if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
} while (0)

namespace {

using namespace leonida;
namespace sp = leonida::submit_phases;
constexpr std::uint64_t ms = 1000000;  // ns

std::uint64_t load(const std::atomic<std::uint64_t>& a) { return a.load(std::memory_order_relaxed); }

std::uint64_t phase_total() {
    std::uint64_t n = 0;
    for (const auto& p : sp::totals().ns) n += load(p);
    return n;
}

int scripted() {
    sp::FrameTotals& f = sp::frame_totals();
    // Frame 1: a direct submission with heaps 1 ms and replay 2 ms, nothing changed (a floor submission).
    {
        const sp::Submission s(0);
        sp::add(sp::heaps, 1 * ms);
        sp::add(sp::replay, 2 * ms);
    }
    // One of our threads: its 5 ms of textures are the phase line's, not the title threads'.
    std::thread([] {
        const sp::WorkerScope busy(sp::worker_texture_reads);
        sp::add(sp::textures, 5 * ms);
    }).join();
    // A title call that spends about 3 ms, 1 ms of it in a phase: about 2 ms of fences.
    const auto call_start = std::chrono::steady_clock::now();
    {
        const sp::TitleCall call(sp::part_fences);
        sp::add(sp::replay, 1 * ms);
        std::this_thread::sleep_for(std::chrono::milliseconds(3));
    }
    const double call_ns = double(std::chrono::duration_cast<std::chrono::nanoseconds>(
                                      std::chrono::steady_clock::now() - call_start).count());
    const std::uint64_t fences = load(f.part_ns[sp::part_fences]);
    EXPECT(fences > 1 * ms && double(fences) <= call_ns - 1e6);
    // A lock another thread holds for 4 ms: counted as a lock wait, inside the other parts (not in the frame's sum).
    std::mutex m;
    std::atomic<bool> taken{false};
    std::thread holder([&] {  // takes the lock and lets it go on its own thread, as a mutex must be
        const std::lock_guard held(m);
        taken.store(true);
        std::this_thread::sleep_for(std::chrono::milliseconds(4));
    });
    while (!taken.load()) std::this_thread::yield();
    {
        std::unique_lock lock(m, std::defer_lock);
        sp::lock_timed(lock);
    }
    holder.join();
    EXPECT(load(f.part_ns[sp::part_lock_wait]) >= 2 * ms);
    const std::uint64_t frame1 = 3 * ms + 1 * ms + fences;  // heaps, replay, replay in the call, fences
    EXPECT(load(f.current_ns) == frame1);
    sp::frame_presented();
    // Frame 2: a compute submission with geometry 4 ms that uploaded geometry: not a floor submission.
    {
        const sp::Submission s(1);
        sp::add(sp::geometry, 4 * ms);
        sp::note_change(sp::change_geometry);
    }
    sp::frame_presented();

    EXPECT(load(f.frames) == 2);
    EXPECT(load(f.submissions[0]) == 1 && load(f.submissions[1]) == 1 && load(f.submissions[2]) == 0);
    EXPECT(load(f.phase_ns[sp::heaps]) == 1 * ms && load(f.phase_ns[sp::replay]) == 3 * ms);
    EXPECT(load(f.phase_ns[sp::geometry]) == 4 * ms && load(f.phase_ns[sp::textures]) == 0);
    EXPECT(load(f.worker_ns[sp::worker_texture_reads]) > 0);
    // The phase line counts every thread: the title threads' phases and the worker's add up to its total.
    std::uint64_t title = 0;
    for (const auto& p : f.phase_ns) title += load(p);
    EXPECT(phase_total() == title + 5 * ms);
    EXPECT(load(f.worst_ns) == (frame1 > 4 * ms ? frame1 : 4 * ms));
    EXPECT(load(f.all_count) == 2 && load(f.all_ns) == 7 * ms);
    EXPECT(load(f.floor_count) == 1 && load(f.floor_min_ns) == 3 * ms);
    EXPECT(load(f.floor_ns[sp::heaps]) == 1 * ms && load(f.floor_ns[sp::replay]) == 2 * ms &&
           load(f.floor_ns[sp::geometry]) == 0);
    sp::frame_lines(1.0);  // the two lines' format, on stderr; a new window
    EXPECT(load(f.frames) == 0 && load(f.all_count) == 0 && load(f.floor_count) == 0 && load(f.worst_ns) == 0);
    for (const auto& p : f.phase_ns) EXPECT(load(p) == 0);
    // Mirror refresh/write-back inside a list submission: time inside Scope(shadows) is billed to shadows, not lists,
    // and contended lock_timed inside it is billed to lock waits.
    {
        const sp::TitleCall call(sp::part_lists);
        const sp::Scope shadows_scope(sp::shadows);
        std::mutex mirror_m;
        std::atomic<bool> held_flag{false};
        std::thread holder([&] {
            const std::lock_guard held(mirror_m);
            held_flag.store(true);
            std::this_thread::sleep_for(std::chrono::milliseconds(4));
        });
        while (!held_flag.load()) std::this_thread::yield();
        {
            std::unique_lock lock(mirror_m, std::defer_lock);
            sp::lock_timed(lock);
        }
        holder.join();
    }
    EXPECT(load(f.phase_ns[sp::shadows]) >= 2 * ms);
    EXPECT(load(f.part_ns[sp::part_lock_wait]) >= 2 * ms);
    EXPECT(load(f.part_ns[sp::part_lists]) < load(f.phase_ns[sp::shadows]));
    sp::frame_lines(1.0);
    std::printf("scripted: frame sums, title and own threads, worst frame and floor as scripted\n");
    return 0;
}

int submissions() {
    vktest::Device d;
    if (!d.create()) {
        std::printf("no usable Vulkan device: the submissions part is skipped\n");
        return 0;
    }
    VulkanContext context(d.device_proc, d.device, true);
    EXPECT(context.loaded());
    VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
    EXPECT(direct.ok());
    CommandList list;
    EXPECT(list.close());
    const CommandList* lists[] = {&list};
    sp::FrameTotals& f = sp::frame_totals();
    const std::uint64_t phases_before = phase_total(), submitted_before = load(sp::totals().submissions);
    const std::uint64_t all_before = load(f.all_count), all_ns_before = load(f.all_ns);
    const std::uint64_t floor_before = load(f.floor_count), direct_before = load(f.submissions[0]);
    std::uint64_t floor_ns_before = 0;
    for (const auto& p : f.floor_ns) floor_ns_before += load(p);
    constexpr std::uint64_t kSubmissions = 20;
    for (std::uint64_t i = 0; i < kSubmissions; ++i) EXPECT(direct.execute(lists) == ReplayResult::success);
    EXPECT(direct.idle());
    std::uint64_t floor_ns = 0;
    for (const auto& p : f.floor_ns) floor_ns += load(p);
    const std::uint64_t phases = phase_total() - phases_before;
    std::printf("submissions: %llu, phases %.3f ms in all, floor %llu of them, %.3f ms\n",
                static_cast<unsigned long long>(load(f.all_count) - all_before), double(phases) * 1e-6,
                static_cast<unsigned long long>(load(f.floor_count) - floor_before),
                double(floor_ns - floor_ns_before) * 1e-6);
    EXPECT(load(sp::totals().submissions) - submitted_before == kSubmissions);
    EXPECT(load(f.submissions[0]) - direct_before == kSubmissions);
    EXPECT(load(f.all_count) - all_before == kSubmissions);
    EXPECT(load(f.floor_count) - floor_before == kSubmissions);  // empty lists: nothing to bring up to date
    // Every phase of these submissions ran on this thread inside them: the sums agree to the nanosecond.
    EXPECT(load(f.all_ns) - all_ns_before == phases);
    EXPECT(floor_ns - floor_ns_before == phases);
    EXPECT(phases > 0);
    return 0;
}

// Round 1D queue concurrency, batch coalescing, zero-copy CommandList snapshot, and async worker tests.
int queue_async_and_coalescing() {
    vktest::Device d;
    if (!d.create()) {
        std::printf("no usable Vulkan device: the queue_async_and_coalescing part is skipped\n");
        return 0;
    }

    constexpr VkDeviceSize kBufBytes = 256;
    const VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;

    auto make_copy_list = [](ResourceId dst, std::uint64_t dst_off, ResourceId src, std::uint64_t src_off,
                             std::uint64_t bytes) -> std::shared_ptr<CommandList> {
        auto list = std::make_shared<CommandList>();
        if (!list->copy_buffer({dst, dst_off, src, src_off, bytes}) || !list->close()) return {};
        return list;
    };

    // 1. Concurrent submission while before_execute_() is running must not block on state_mutex_, must preserve
    //    strict FIFO execution order, and must coalesce consecutive queued batches on drain.
    {
        VulkanContext context(d.device_proc, d.device, true);
        EXPECT(context.loaded());
        VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
        EXPECT(queue.ok());

        void* src_map = nullptr;
        void* dst_map = nullptr;
        VkBuffer src = d.buffer(kBufBytes, usage, &src_map);
        VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map);
        EXPECT(src && dst);
        auto* sb = static_cast<std::uint8_t*>(src_map);
        auto* db = static_cast<std::uint8_t*>(dst_map);
        std::memset(sb, 0, kBufBytes);
        std::memset(db, 0, kBufBytes);
        sb[0] = 0x11;
        sb[16] = 0x22;
        sb[32] = 0x33;

        ResourceBinding r_src{}, r_dst{};
        r_src.buffer = src;
        r_src.size = kBufBytes;
        r_dst.buffer = dst;
        r_dst.size = kBufBytes;
        r_dst.host_readable = true;
        context.add_resource(1, r_src);
        context.add_resource(2, r_dst);

        std::atomic<unsigned> before_calls{0};
        std::atomic<bool> in_first_before{false};
        std::atomic<bool> release_first_before{false};
        context.set_before_execute([&] {
            const unsigned n = before_calls.fetch_add(1, std::memory_order_acq_rel) + 1;
            if (n == 1) {
                in_first_before.store(true, std::memory_order_release);
                while (!release_first_before.load(std::memory_order_acquire)) std::this_thread::yield();
            }
        });

        // Each batch writes its marker to its own slot AND overwrites slot 0 (final value proves strict FIFO order).
        auto b1 = std::make_shared<CommandList>();
        EXPECT(b1->copy_buffer({2, 16, 1, 0, 1}) && b1->copy_buffer({2, 0, 1, 0, 1}) && b1->close());
        auto b2 = std::make_shared<CommandList>();
        EXPECT(b2->copy_buffer({2, 32, 1, 16, 1}) && b2->copy_buffer({2, 0, 1, 16, 1}) && b2->close());
        auto b3 = std::make_shared<CommandList>();
        EXPECT(b3->copy_buffer({2, 48, 1, 32, 1}) && b3->copy_buffer({2, 0, 1, 32, 1}) && b3->close());

        VulkanFence done(context, 0);
        EXPECT(done.semaphore() != VK_NULL_HANDLE);

        ReplayResult t_res = ReplayResult::unsupported;
        std::thread worker([&] {
            std::shared_ptr<const CommandList> s1 = b1;
            t_res = queue.execute({&s1, 1});
        });

        while (!in_first_before.load(std::memory_order_acquire)) std::this_thread::yield();

        // While worker is blocked inside before_execute_(), this thread submits b2, b3, and Signal(done, 1).
        // If state_mutex_ were held across before_execute_(), this would deadlock before release_first_before is set!
        std::shared_ptr<const CommandList> s2 = b2;
        std::shared_ptr<const CommandList> s3 = b3;
        EXPECT(queue.execute({&s2, 1}) == ReplayResult::success);
        EXPECT(queue.execute({&s3, 1}) == ReplayResult::success);
        EXPECT(queue.signal(done.semaphore(), 1));

        // Zero-copy proof: queue holds references to b2 and b3 without copying; simulating Reset() when use_count > 1
        // allocates fresh storage and never mutates the in-flight snapshot.
        EXPECT(b2.use_count() > 1 && b3.use_count() > 1);
        b2 = std::make_shared<CommandList>();
        EXPECT(b2->copy_buffer({2, 0, 1, 0, 1}) && b2->close());  // distinct content (0x11), must not affect in-flight

        release_first_before.store(true, std::memory_order_release);
        worker.join();
        EXPECT(t_res == ReplayResult::success);
        EXPECT(done.wait(1, 5'000'000'000ull));
        EXPECT(queue.idle());

        // b1 ran first (before_calls #1), then b2+b3 coalesced into ONE execute_now() (before_calls #2).
        EXPECT(before_calls.load(std::memory_order_acquire) == 2);
        EXPECT(db[16] == 0x11 && db[32] == 0x22 && db[48] == 0x33);
        EXPECT(db[0] == 0x33);  // strict FIFO: b1 -> b2 -> b3
        // After completion, s2/s3 snapshots are released (only local s3 holds b3's old CommandList).
        s2.reset();
        s3.reset();
        EXPECT(b1.use_count() == 1 && b3.use_count() == 1);
        EXPECT(b3->reset());  // steady-state reuse with zero allocation
        std::printf("queue sync: unlocked before_execute_, strict FIFO, coalesced drain, zero-copy Reset ok\n");
    }

    // 2. Held batches behind a fence gate coalesce consecutive ready batches into ONE before_execute_() call,
    //    while an intervening Signal or Wait stops coalescing. Also verify invalid-batch fallback inside a
    //    coalesced group does not drop valid peers.
    {
        VulkanContext context(d.device_proc, d.device, true);
        EXPECT(context.loaded());
        VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
        VulkanQueue copy_q(context, d.queue, d.family, VulkanQueue::QueueKind::copy);
        EXPECT(queue.ok() && copy_q.ok());

        void* src_map = nullptr;
        void* dst_map = nullptr;
        VkBuffer src = d.buffer(kBufBytes, usage, &src_map);
        VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map);
        EXPECT(src && dst);
        auto* sb = static_cast<std::uint8_t*>(src_map);
        auto* db = static_cast<std::uint8_t*>(dst_map);
        std::memset(db, 0, kBufBytes);
        for (int i = 0; i < 6; ++i) sb[i * 16] = std::uint8_t(0xA1 + i);

        ResourceBinding r_src{}, r_dst{};
        r_src.buffer = src;
        r_src.size = kBufBytes;
        r_dst.buffer = dst;
        r_dst.size = kBufBytes;
        r_dst.host_readable = true;
        context.add_resource(1, r_src);
        context.add_resource(2, r_dst);

        std::atomic<unsigned> before_calls{0};
        context.set_before_execute([&] { before_calls.fetch_add(1, std::memory_order_relaxed); });

        VulkanFence gate(context, 0), mid(context, 0), done(context, 0);
        EXPECT(gate.semaphore() && mid.semaphore() && done.semaphore());

        auto make_step = [&](int idx) {
            auto l = std::make_shared<CommandList>();
            if (!l->copy_buffer({2, std::uint64_t((idx + 1) * 16), 1, std::uint64_t(idx * 16), 1}) ||
                !l->copy_buffer({2, 0, 1, std::uint64_t(idx * 16), 1}) || !l->close())
                return std::shared_ptr<const CommandList>{};
            return std::shared_ptr<const CommandList>(std::move(l));
        };
        std::shared_ptr<const CommandList> steps[6];
        for (int i = 0; i < 6; ++i) {
            steps[i] = make_step(i);
            EXPECT(steps[i] != nullptr);
        }

        queue.wait(gate.semaphore(), 1);
        EXPECT(queue.execute({&steps[0], 1}) == ReplayResult::success);  // group 1: steps 0, 1, 2
        EXPECT(queue.execute({&steps[1], 1}) == ReplayResult::success);
        EXPECT(queue.execute({&steps[2], 1}) == ReplayResult::success);
        EXPECT(queue.signal(mid.semaphore(), 1));                        // Signal stops coalescing
        EXPECT(queue.execute({&steps[3], 1}) == ReplayResult::success);  // group 2: step 3 alone
        queue.wait(mid.semaphore(), 1);                                  // Wait stops coalescing
        EXPECT(queue.execute({&steps[4], 1}) == ReplayResult::success);  // group 3: steps 4, 5
        EXPECT(queue.execute({&steps[5], 1}) == ReplayResult::success);
        EXPECT(queue.signal(done.semaphore(), 1));

        EXPECT(before_calls.load() == 0);
        EXPECT(gate.signal(1));
        EXPECT(done.wait(1, 5'000'000'000ull));
        EXPECT(queue.idle());

        // Exactly 3 before_execute_() calls: {0,1,2}, {3}, {4,5}.
        EXPECT(before_calls.load() == 3);
        for (int i = 0; i < 6; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0xA1 + i));
        EXPECT(db[0] == std::uint8_t(0xA6));  // strict FIFO through all 6 batches

        // Coalesced fallback: [valid, invalid ClearRenderTarget on copy queue, valid] held behind gate2.
        VulkanFence gate2(context, 0), done2(context, 0);
        EXPECT(gate2.semaphore() && done2.semaphore());
        std::memset(db, 0, kBufBytes);
        auto bad_clear = std::make_shared<CommandList>();
        TargetView tv{};
        tv.resource = 2;
        tv.layer_count = 1;
        EXPECT(bad_clear->clear_render_target(tv, ClearColor{}) && bad_clear->close());
        std::shared_ptr<const CommandList> bad_snap = bad_clear;
        copy_q.wait(gate2.semaphore(), 1);
        EXPECT(copy_q.execute({&steps[0], 1}) == ReplayResult::success);
        EXPECT(copy_q.execute({&bad_snap, 1}) == ReplayResult::success);
        EXPECT(copy_q.execute({&steps[1], 1}) == ReplayResult::success);
        EXPECT(copy_q.signal(done2.semaphore(), 1));
        EXPECT(gate2.signal(1));
        EXPECT(done2.wait(1, 5'000'000'000ull));
        EXPECT(copy_q.idle());
        EXPECT(copy_q.take_held_failure() == ReplayResult::unsupported);
        EXPECT(db[16] == 0xA1 && db[32] == 0xA2 && db[0] == 0xA2);
        std::printf("queue held coalescing: 6 batches -> 3 submits, Signal/Wait split, invalid fallback ok\n");
    }

    // 3. LEONIDA_SUBMIT_ASYNC (per-queue async worker depth=4): inbox coalescing, Present barrier split,
    //    held-batch release through worker, and zero-copy CommandList Reset() while queued.
    {
        VulkanContext context(d.device_proc, d.device, true);
        EXPECT(context.loaded());
        VulkanQueue queue(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
        EXPECT(queue.ok());
        queue.set_async_depth(4);

        void* src_map = nullptr;
        void* dst_map = nullptr;
        VkBuffer src = d.buffer(kBufBytes, usage, &src_map);
        VkBuffer dst = d.buffer(kBufBytes, usage, &dst_map);
        EXPECT(src && dst);
        auto* sb = static_cast<std::uint8_t*>(src_map);
        auto* db = static_cast<std::uint8_t*>(dst_map);
        std::memset(db, 0, kBufBytes);
        for (int i = 0; i < 4; ++i) sb[i * 16] = std::uint8_t(0xC1 + i);

        ResourceBinding r_src{}, r_dst{};
        r_src.buffer = src;
        r_src.size = kBufBytes;
        r_dst.buffer = dst;
        r_dst.size = kBufBytes;
        r_dst.host_readable = true;
        context.add_resource(1, r_src);
        context.add_resource(2, r_dst);

        std::atomic<unsigned> before_calls{0};
        std::atomic<bool> in_first{false};
        std::atomic<bool> go_first{false};
        context.set_before_execute([&] {
            const unsigned n = before_calls.fetch_add(1, std::memory_order_acq_rel) + 1;
            if (n == 1) {
                in_first.store(true, std::memory_order_release);
                while (!go_first.load(std::memory_order_acquire)) std::this_thread::yield();
            }
        });

        auto c0 = make_copy_list(2, 16, 1, 0, 1);
        auto c1 = make_copy_list(2, 32, 1, 16, 1);
        auto c2 = make_copy_list(2, 48, 1, 32, 1);
        auto c3 = make_copy_list(2, 64, 1, 48, 1);
        EXPECT(c0 && c1 && c2 && c3);

        std::shared_ptr<const CommandList> s0 = c0, s1 = c1, s2 = c2, s3 = c3;
        EXPECT(queue.execute({&s0, 1}) == ReplayResult::success);
        while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield();

        // Enqueue c1, c2 (will coalesce), then a Present after_held barrier, then c3 (must NOT coalesce past barrier).
        EXPECT(queue.execute({&s1, 1}) == ReplayResult::success);
        EXPECT(queue.execute({&s2, 1}) == ReplayResult::success);
        EXPECT(c1.use_count() > 1 && c2.use_count() > 1);  // queued zero-copy

        std::atomic<unsigned> barrier_seen_before_calls{0};
        std::atomic<bool> barrier_ran{false};
        int barrier_owner = 0;
        context.holds().after_held(&barrier_owner, [&] {
            barrier_seen_before_calls.store(before_calls.load(std::memory_order_acquire), std::memory_order_release);
            barrier_ran.store(true, std::memory_order_release);
        });

        EXPECT(queue.execute({&s3, 1}) == ReplayResult::success);
        VulkanFence done(context, 0);
        EXPECT(done.semaphore() != VK_NULL_HANDLE);
        EXPECT(queue.signal(done.semaphore(), 1));

        // Simulate Reset() of c1 while still in inbox: allocating new storage leaves in-flight snapshot intact.
        c1 = make_copy_list(2, 32, 1, 0, 1);
        EXPECT(c1 != nullptr);

        go_first.store(true, std::memory_order_release);
        EXPECT(done.wait(1, 5'000'000'000ull));
        EXPECT(queue.idle());
        EXPECT(barrier_ran.load(std::memory_order_acquire));

        // 3 before_execute_() calls total: {c0}, {c1, c2}, {c3}; barrier ran after {c1, c2} (count == 2), before {c3}!
        EXPECT(before_calls.load() == 3);
        EXPECT(barrier_seen_before_calls.load() == 2);
        EXPECT(db[16] == 0xC1 && db[32] == 0xC2 && db[48] == 0xC3 && db[64] == 0xC4);

        // Held batches behind a gate in async mode: release_ready() wakes worker and coalesces them.
        before_calls.store(0);
        std::memset(db, 0, kBufBytes);
        VulkanFence gate(context, 0), done2(context, 0);
        EXPECT(gate.semaphore() && done2.semaphore());
        queue.wait(gate.semaphore(), 1);
        EXPECT(queue.execute({&s0, 1}) == ReplayResult::success);
        EXPECT(queue.execute({&s2, 1}) == ReplayResult::success);
        EXPECT(queue.signal(done2.semaphore(), 1));
        EXPECT(queue.idle());  // flushes inbox into held_ while gate is still closed
        EXPECT(before_calls.load() == 0);
        EXPECT(gate.signal(1));
        EXPECT(done2.wait(1, 5'000'000'000ull));
        EXPECT(queue.idle());
        EXPECT(before_calls.load() == 1);
        EXPECT(db[16] == 0xC1 && db[48] == 0xC3);
        std::printf("queue async (depth=4): inbox coalescing, Present barrier split, gate release ok\n");
    }

    return 0;
}

}  // namespace

int main() {
    if (scripted() || submissions() || queue_async_and_coalescing()) return 1;
    std::printf("submit_frame_test: OK\n");
    return 0;
}