upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/vk_queue_test.cpp
blob: 124f1bd2dccbb6b74fb8ad6f356048a4d8b43933 (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
// D3D12 copy and compute queues on their own Vulkan queues, with cross-queue fences. No shaders or game data.
//
// The device gets a queue per D3D12 type (dedicated families when the GPU has them). A copy queue uploads a buffer
// and a texture, the way asset streaming does, signals a fence, and the direct queue waits on that fence on the GPU
// before reading both back. The compute queue does the same handoff the other way. A copy queue must refuse a
// render-target clear. Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation; any validation error
// fails the test.
#include "vk_test_device.h"
#include "../src/vulkan_backend.h"

#include <array>
#include <atomic>
#include <cstdint>
#include <cstring>
#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;
using Kind = VulkanQueue::QueueKind;

// Counts the command buffers a queue allocates, for the backlog check in main().
PFN_vkAllocateCommandBuffers real_allocate_command_buffers = nullptr;
std::uint32_t allocated_command_buffers = 0;

VKAPI_ATTR VkResult VKAPI_CALL counting_allocate_command_buffers(VkDevice device, const VkCommandBufferAllocateInfo* info,
                                                                 VkCommandBuffer* buffers) {
    allocated_command_buffers += info->commandBufferCount;
    return real_allocate_command_buffers(device, info, buffers);
}

// Makes the queue's first kStalls waits for room time out at once, as on a GPU that stopped making progress.
// While hold_progress is set, timeline counter queries report 0 so a fast GPU cannot retire command buffers
// before the loop reaches kMaxQueuedCommandBuffers; once kStalls timeouts have been injected, real progress
// resumes and the wait completes normally.
constexpr std::uint32_t kStalls = 3;
PFN_vkGetSemaphoreCounterValue real_get_semaphore_counter_value = nullptr;
PFN_vkWaitSemaphores real_wait_semaphores = nullptr;
std::atomic<std::uint32_t> injected_stalls{0};
std::atomic<bool> hold_progress{false};

VKAPI_ATTR VkResult VKAPI_CALL holding_semaphore_counter(VkDevice device, VkSemaphore semaphore,
                                                         std::uint64_t* value) {
    if (hold_progress.load()) {
        *value = 0;
        return VK_SUCCESS;
    }
    return real_get_semaphore_counter_value(device, semaphore, value);
}

VKAPI_ATTR VkResult VKAPI_CALL stalling_wait_semaphores(VkDevice device, const VkSemaphoreWaitInfo* info,
                                                        std::uint64_t timeout) {
    if (timeout == VulkanQueue::kQueuedWaitNs) {
        if (injected_stalls.load() < kStalls) {
            ++injected_stalls;
            return VK_TIMEOUT;
        }
        hold_progress.store(false);
    }
    return real_wait_semaphores(device, info, timeout);
}

// Each submission's command buffer starts with a full memory barrier (D3D12's ExecuteCommandLists boundary): counts
// the command buffers begun and those whose first barrier is that one.
PFN_vkBeginCommandBuffer real_begin = nullptr;
PFN_vkCmdPipelineBarrier real_barrier = nullptr;
std::uint32_t begun = 0, led_by_full_barrier = 0;
bool awaiting_first_barrier = false;

VKAPI_ATTR VkResult VKAPI_CALL counting_begin(VkCommandBuffer cb, const VkCommandBufferBeginInfo* info) {
    ++begun;
    awaiting_first_barrier = true;
    return real_begin(cb, info);
}

VKAPI_ATTR void VKAPI_CALL watching_barrier(VkCommandBuffer cb, VkPipelineStageFlags src, VkPipelineStageFlags dst,
                                            VkDependencyFlags flags, std::uint32_t memory_count,
                                            const VkMemoryBarrier* memory, std::uint32_t buffer_count,
                                            const VkBufferMemoryBarrier* buffers, std::uint32_t image_count,
                                            const VkImageMemoryBarrier* images) {
    if (awaiting_first_barrier) {
        awaiting_first_barrier = false;
        if (src == VK_PIPELINE_STAGE_ALL_COMMANDS_BIT && dst == VK_PIPELINE_STAGE_ALL_COMMANDS_BIT &&
            memory_count == 1 && (memory[0].srcAccessMask & VK_ACCESS_MEMORY_WRITE_BIT))
            ++led_by_full_barrier;
    }
    real_barrier(cb, src, dst, flags, memory_count, memory, buffer_count, buffers, image_count, images);
}

const char* flags_text(VkQueueFlags f) {
    static char buf[32];
    std::snprintf(buf, sizeof buf, "%s%s%s", f & VK_QUEUE_GRAPHICS_BIT ? "G" : "", f & VK_QUEUE_COMPUTE_BIT ? "C" : "",
                  f & VK_QUEUE_TRANSFER_BIT ? "T" : "");
    return buf;
}

int check_picker() {
    // Synthetic family layouts: the picker's choices don't depend on the GPU running the test.
    auto fam = [](VkQueueFlags flags, std::uint32_t granularity = 1) {
        VkQueueFamilyProperties p{};
        p.queueFlags = flags;
        p.queueCount = 1;
        p.minImageTransferGranularity = {granularity, granularity, granularity};
        return p;
    };
    constexpr VkQueueFlags G = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT;
    constexpr VkQueueFlags C = VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT;
    constexpr VkQueueFlags T = VK_QUEUE_TRANSFER_BIT;
    {
        const std::array f{fam(G), fam(T), fam(C)};  // the common NVIDIA layout
        const auto q = pick_queue_families(f, 0);
        EXPECT(q.direct == 0 && q.compute == 2 && q.copy == 1);
    }
    {
        const std::array f{fam(G)};  // one family for everything (integrated GPUs, llvmpipe)
        const auto q = pick_queue_families(f, 0);
        EXPECT(q.direct == 0 && q.compute == 0 && q.copy == 0);
    }
    {
        const std::array f{fam(G), fam(C)};  // no DMA family: copies go to async compute
        const auto q = pick_queue_families(f, 0);
        EXPECT(q.compute == 1 && q.copy == 1);
    }
    {
        const std::array f{fam(G), fam(T, 8), fam(C)};  // a coarse DMA family is skipped
        const auto q = pick_queue_families(f, 0);
        EXPECT(q.copy == 2);
    }
    VkPhysicalDeviceFeatures sparse_buffers{};
    sparse_buffers.sparseBinding = VK_TRUE;
    sparse_buffers.sparseResidencyAliased = VK_TRUE;
    sparse_buffers.sparseResidencyBuffer = VK_TRUE;
    VkPhysicalDeviceSparseProperties strict_residency{};
    strict_residency.residencyNonResidentStrict = VK_TRUE;
    {
        constexpr VkQueueFlags S = VK_QUEUE_SPARSE_BINDING_BIT;
        const std::array f{fam(G | S), fam(T), fam(C)};
        const auto preferred = pick_queue_families(f, 0);
        EXPECT(preferred.compute == 2 && preferred.copy == 1);
        const auto corrected = sparse_capable_queue_families(f, preferred, sparse_buffers, strict_residency);
        EXPECT(corrected.direct == 0 && corrected.compute == 0 && corrected.copy == 0);
        auto sparse_images = sparse_buffers;
        sparse_images.sparseResidencyBuffer = VK_FALSE;
        sparse_images.sparseResidencyImage2D = VK_TRUE;
        const auto image_only = sparse_capable_queue_families(f, preferred, sparse_images, strict_residency);
        EXPECT(image_only.direct == 0 && image_only.compute == 0 && image_only.copy == 0);
        const auto non_strict = sparse_capable_queue_families(f, preferred, sparse_images, {});
        EXPECT(non_strict.compute == 2 && non_strict.copy == 1);
        const auto no_sparse = sparse_capable_queue_families(f, preferred, {}, strict_residency);
        EXPECT(no_sparse.compute == 2 && no_sparse.copy == 1);
    }
    {
        constexpr VkQueueFlags S = VK_QUEUE_SPARSE_BINDING_BIT;
        const std::array f{fam(G | S), fam(T | S), fam(C | S)};
        const auto preferred = pick_queue_families(f, 0);
        const auto corrected = sparse_capable_queue_families(f, preferred, sparse_buffers, strict_residency);
        EXPECT(corrected.compute == 2 && corrected.copy == 1);  // preserve dedicated queues
    }
    {
        constexpr VkQueueFlags S = VK_QUEUE_SPARSE_BINDING_BIT;
        const std::array f{fam(G | S), fam(T), fam(C | S)};
        const auto preferred = pick_queue_families(f, 0);
        const auto corrected = sparse_capable_queue_families(f, preferred, sparse_buffers, strict_residency);
        EXPECT(corrected.compute == 2 && corrected.copy == 0);  // mixed capabilities
    }
    std::printf("queue family picker: 7 layouts ok\n");
    return 0;
}

// Which VkQueue each new D3D12 queue leases (CreateCommandQueue): an unused one while there is one, its own family's
// first, then the one with the fewest D3D12 queues. `own` and `graphics` count the D3D12 queues on each VkQueue.
int check_queue_slots() {
    const auto lease = [](std::vector<std::uint32_t>& own, std::vector<std::uint32_t>& graphics) {
        const auto slot = pick_queue_slot({own, graphics});
        if (slot.pool != SIZE_MAX) ++(slot.pool == 0 ? own : graphics)[slot.index];
        return slot;
    };
    {   // Enough queues (an RTX 5080: 8 compute, 16 graphics): every COMPUTE queue gets a VkQueue of its own.
        std::vector<std::uint32_t> compute(8, 0), graphics(16, 0);
        for (std::size_t i = 0; i < 8; ++i) {
            const auto slot = lease(compute, graphics);
            EXPECT(slot.pool == 0 && slot.index == i);
        }
        const auto ninth = lease(compute, graphics);
        EXPECT(ninth.pool == 1 && ninth.index == 0);  // then an unused graphics queue, not a shared compute one
    }
    {   // One VkQueue per family (LEONIDA_VK_MAX_QUEUES_PER_FAMILY=1): DIRECT, COMPUTE, then they share.
        std::vector<std::uint32_t> none, compute(1, 0), graphics(1, 0);
        EXPECT(lease(none, graphics).pool == 1);
        EXPECT(lease(compute, graphics).pool == 0);
        const auto second_compute = lease(compute, graphics);
        EXPECT(second_compute.pool == 0 && compute[0] == 2);  // a tie goes to the type's own family
        const auto third_compute = lease(compute, graphics);
        EXPECT(third_compute.pool == 1 && graphics[0] == 2);  // then the queue with fewer D3D12 queues
        --compute[0];
        --compute[0];  // two COMPUTE queues released
        EXPECT(lease(compute, graphics).pool == 0);
    }
    {   // A queue released by its last D3D12 queue is unused again and preferred to sharing.
        std::vector<std::uint32_t> none, graphics{3, 0, 2};
        const auto slot = lease(none, graphics);
        EXPECT(slot.pool == 1 && slot.index == 1);
    }
    {   // No VkQueue at all.
        std::vector<std::uint32_t> none;
        EXPECT(pick_queue_slot({none, none}).pool == SIZE_MAX);
    }
    std::printf("queue slots: distinct while free, shared by fewest users after\n");
    return 0;
}

constexpr std::uint32_t kSize = 64 * 1024;
constexpr std::uint32_t kTex = 64;  // 64x64 RGBA8: row pitch 256, already aligned

std::uint8_t pattern(std::uint32_t i, std::uint8_t seed) { return std::uint8_t(i * 31 + seed); }

}  // namespace

int main() {
    if (check_picker() || check_queue_slots()) return 1;

    // Even with a capable dedicated family, the graphics fallback must be usable. With only a caller-owned
    // graphics queue, its actual family index (which need not be zero) determines support.
    constexpr std::array<std::uint32_t, 2> copy_only_timestamps{64, 0};
    constexpr std::array<std::uint32_t, 2> both_timestamps{64, 32};
    static_assert(!copy_queue_timestamp_families_supported(copy_only_timestamps, 1, 0));
    static_assert(!copy_queue_timestamp_families_supported(copy_only_timestamps, 1, std::nullopt));
    static_assert(!copy_queue_timestamp_families_supported(copy_only_timestamps, 0, 1));
    static_assert(copy_queue_timestamp_families_supported(both_timestamps, 1, 0));
    static_assert(copy_queue_timestamp_families_supported(both_timestamps, 1, std::nullopt));

    vktest::Device d;
    d.all_queue_types = true;
    if (!d.create()) {
        std::printf("SKIP: no usable Vulkan device\n");
        return 77;
    }
    std::printf("families: direct %u (%s)", d.family, flags_text(d.families[d.family].queueFlags));
    std::printf(", compute %u (%s)", d.compute_family, flags_text(d.families[d.compute_family].queueFlags));
    std::printf(", copy %u (%s)\n", d.copy_family, flags_text(d.families[d.copy_family].queueFlags));

    VulkanContext context(d.device_proc, d.device, true);
    EXPECT(context.loaded());
    VulkanQueue direct(context, d.queue, d.family, Kind::direct);
    VulkanQueue compute(context, d.compute_queue, d.compute_family, Kind::compute);
    VulkanQueue copy(context, d.copy_queue, d.copy_family, Kind::copy);
    EXPECT(direct.ok() && compute.ok() && copy.ok());

    // Buffers: an upload source, a GPU-side destination and a readback, all host-visible for the test.
    void *upload_map = nullptr, *mid_map = nullptr, *readback_map = nullptr, *tex_readback_map = nullptr;
    void* stale_src_map = nullptr;
    const VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
    VkBuffer upload = d.buffer(kSize, usage, &upload_map);
    VkBuffer mid = d.buffer(kSize, usage, &mid_map);
    VkBuffer readback = d.buffer(kSize, usage, &readback_map);
    VkBuffer tex_readback = d.buffer(kTex * kTex * 4, usage, &tex_readback_map);
    VkBuffer stale_src = d.buffer(kSize, usage, &stale_src_map);
    VkImage texture = d.image(VK_FORMAT_R8G8B8A8_UNORM, kTex, kTex, 1, VK_IMAGE_USAGE_SAMPLED_BIT);
    EXPECT(upload && mid && readback && tex_readback && stale_src && texture);
    auto* up = static_cast<std::uint8_t*>(upload_map);
    for (std::uint32_t i = 0; i < kSize; ++i) up[i] = pattern(i, 7);

    const ResourceId upload_id = 1, mid_id = 2, readback_id = 3, tex_id = 4, tex_readback_id = 5, stale_src_id = 7;
    auto add_buffer = [&](ResourceId id, VkBuffer b, VkDeviceSize size) {
        ResourceBinding r;
        r.buffer = b;
        r.size = size;
        context.add_resource(id, r);
    };
    add_buffer(upload_id, upload, kSize);
    add_buffer(mid_id, mid, kSize);
    add_buffer(readback_id, readback, kSize);
    add_buffer(tex_readback_id, tex_readback, kTex * kTex * 4);
    add_buffer(stale_src_id, stale_src, kSize);
    ResourceBinding tex;
    tex.image = texture;
    tex.format = VK_FORMAT_R8G8B8A8_UNORM;
    tex.width = kTex;
    tex.height = kTex;
    // As the device registers a texture: tracked, still undefined, in D3D12's common state. Its first use is the
    // copy-queue upload, which can't zero-fill it first.
    context.add_resource(tex_id, tex, VK_IMAGE_LAYOUT_UNDEFINED);

    VulkanFence fence(context, 0);
    EXPECT(fence.semaphore());

    // 1. Copy queue: buffer upload and a texture upload from the same staging buffer (footprint copy), then signal.
    CommandList on_copy;
    EXPECT(on_copy.copy_buffer({mid_id, 0, upload_id, 0, kSize}));
    CopyTexture upload_tex{};
    upload_tex.dst = {tex_id, false, 0, 0, 0, 0, 0, 0};
    upload_tex.src = {upload_id, true, 0, 0, kTex, kTex, 1, kTex * 4};
    EXPECT(on_copy.copy_texture(upload_tex));
    EXPECT(on_copy.close());
    const CommandList* copy_lists[] = {&on_copy};
    EXPECT(copy.execute(copy_lists) == ReplayResult::success);
    EXPECT(copy.signal(fence.semaphore(), 1));

    // 2. Direct queue: wait for the copy queue on the GPU, then read both back.
    direct.wait(fence.semaphore(), 1);
    CommandList on_direct;
    EXPECT(on_direct.copy_buffer({readback_id, 0, mid_id, 0, kSize}));
    CopyTexture read_tex{};
    read_tex.dst = {tex_readback_id, true, 0, 0, kTex, kTex, 1, kTex * 4};
    read_tex.src = {tex_id, false, 0, 0, 0, 0, 0, 0};
    EXPECT(on_direct.copy_texture(read_tex));
    EXPECT(on_direct.close());
    const CommandList* direct_lists[] = {&on_direct};
    EXPECT(direct.execute(direct_lists) == ReplayResult::success);
    EXPECT(direct.signal(fence.semaphore(), 2));
    EXPECT(fence.wait(2, 5'000'000'000ull));

    std::uint32_t bad = 0;
    auto* rb = static_cast<std::uint8_t*>(readback_map);
    for (std::uint32_t i = 0; i < kSize; ++i) bad += rb[i] != pattern(i, 7);
    auto* tr = static_cast<std::uint8_t*>(tex_readback_map);
    for (std::uint32_t i = 0; i < kTex * kTex * 4; ++i) bad += tr[i] != pattern(i, 7);
    std::printf("copy -> direct: buffer and texture upload read back, %u bad bytes\n", bad);
    EXPECT(bad == 0);

    // 3. Compute queue the other way: wait for the direct queue, copy, signal; the host waits on the fence.
    for (std::uint32_t i = 0; i < kSize; ++i) up[i] = pattern(i, 99);
    CommandList on_direct2;
    EXPECT(on_direct2.copy_buffer({mid_id, 0, upload_id, 0, kSize}));
    EXPECT(on_direct2.close());
    const CommandList* direct_lists2[] = {&on_direct2};
    EXPECT(direct.execute(direct_lists2) == ReplayResult::success);
    EXPECT(direct.signal(fence.semaphore(), 3));
    compute.wait(fence.semaphore(), 3);
    CommandList on_compute;
    EXPECT(on_compute.copy_buffer({readback_id, 0, mid_id, 0, kSize}));
    EXPECT(on_compute.close());
    const CommandList* compute_lists[] = {&on_compute};
    EXPECT(compute.execute(compute_lists) == ReplayResult::success);
    EXPECT(compute.signal(fence.semaphore(), 4));
    EXPECT(fence.wait(4, 5'000'000'000ull));
    bad = 0;
    for (std::uint32_t i = 0; i < kSize; ++i) bad += rb[i] != pattern(i, 99);
    std::printf("direct -> compute: %u bad bytes\n", bad);
    EXPECT(bad == 0);

    // 3b. A smaller ExecuteCommandLists must keep the prepared storage but must not replay stale entries.
    // Eight lists, each copying a non-overlapping chunk from a source that stays at 0xaa, grow prepared_ to eight.
    // A one-list submission then copies only its own chunk from the upload source, now 0xbb. With resize() down and
    // range-for over all entries, the seven stale copies run after that one and put 0xaa back into mid.
    constexpr std::uint32_t kChunk = kSize / 8;
    auto* old = static_cast<std::uint8_t*>(stale_src_map);
    std::memset(old, 0xaa, kSize);
    CommandList stale[8];
    const CommandList* many[8];
    for (std::uint32_t i = 0; i < 8; ++i) {
        EXPECT(stale[i].copy_buffer({mid_id, i * kChunk, stale_src_id, i * kChunk, kChunk}));
        EXPECT(stale[i].close());
        many[i] = &stale[i];
    }
    EXPECT(direct.execute(many) == ReplayResult::success);
    EXPECT(direct.signal(fence.semaphore(), 10));
    EXPECT(fence.wait(10, 5'000'000'000ull));
    auto* md = static_cast<std::uint8_t*>(mid_map);
    std::memset(md, 0, kSize);
    for (std::uint32_t i = 0; i < kSize; ++i) up[i] = 0xbb;
    CommandList final_copy;
    EXPECT(final_copy.copy_buffer({mid_id, 7 * kChunk, upload_id, 7 * kChunk, kChunk}));
    EXPECT(final_copy.close());
    const CommandList* one[1] = {&final_copy};
    EXPECT(direct.execute(one) == ReplayResult::success);
    EXPECT(direct.signal(fence.semaphore(), 11));
    EXPECT(fence.wait(11, 5'000'000'000ull));
    EXPECT(direct.prepared_entries() == 8);
    bad = 0;
    for (std::uint32_t i = 0; i < kSize; ++i)
        bad += md[i] != (i >= 7 * kChunk ? 0xbb : 0x00);
    std::printf("prepared storage: %zu entries kept, %u stale bytes\n", direct.prepared_entries(), bad);
    EXPECT(bad == 0);

    // 4. A copy queue refuses a render-target clear and submits nothing.
    VkImage rt_image = d.image(VK_FORMAT_R8G8B8A8_UNORM, 8, 8, 1, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
    ResourceBinding rt;
    rt.image = rt_image;
    rt.format = VK_FORMAT_R8G8B8A8_UNORM;
    rt.width = rt.height = 8;
    context.add_resource(6, rt, VK_IMAGE_LAYOUT_UNDEFINED);
    CommandList clear;
    TargetView view{};
    view.resource = 6;
    view.layer_count = 1;
    EXPECT(clear.clear_render_target(view, ClearColor{}));
    EXPECT(clear.close());
    const CommandList* clear_lists[] = {&clear};
    EXPECT(copy.execute(clear_lists) == ReplayResult::unsupported);
    EXPECT(compute.execute(clear_lists) == ReplayResult::unsupported);
    std::printf("copy and compute queues refuse a render-target clear\n");

    // 4b. Two ExecuteCommandLists copying into the same buffer, with no barrier in either list: D3D12 orders them (each
    // ends with its work done and visible), so each submission's command buffer must start with a full memory barrier.
    // Before, none did, and synchronization validation reported WRITE_AFTER_WRITE between them (in 5 below).
    {
        void* map = nullptr;
        VkBuffer twice_src = d.buffer(4096, usage, &map);
        VkBuffer twice_dst = d.buffer(4096, usage, &map);
        EXPECT(twice_src && twice_dst);
        add_buffer(9, twice_src, 4096);
        add_buffer(10, twice_dst, 4096);
        CommandList twice;
        EXPECT(twice.copy_buffer({10, 0, 9, 0, 4096}));
        EXPECT(twice.close());
        const CommandList* twice_lists[] = {&twice};
        real_begin = context.vk.vkBeginCommandBuffer;
        real_barrier = context.vk.vkCmdPipelineBarrier;
        context.vk.vkBeginCommandBuffer = counting_begin;
        context.vk.vkCmdPipelineBarrier = watching_barrier;
        begun = led_by_full_barrier = 0;
        for (Kind kind : {Kind::direct, Kind::copy}) {
            VulkanQueue& q = kind == Kind::direct ? direct : copy;
            EXPECT(q.execute(twice_lists) == ReplayResult::success);
            EXPECT(q.execute(twice_lists) == ReplayResult::success);
            EXPECT(q.idle());
        }
        context.vk.vkBeginCommandBuffer = real_begin;
        context.vk.vkCmdPipelineBarrier = real_barrier;
        std::printf("ExecuteCommandLists boundary: %u of %u submissions start with a full memory barrier\n",
                    led_by_full_barrier, begun);
        EXPECT(begun >= 4 && led_by_full_barrier == begun);
    }

    // 5. A caller that submits faster than the GPU finishes must not grow the queue's command buffers without bound:
    // at the cap it waits for the oldest one. Big host-visible copies keep the GPU behind the submitting loop. The first
    // waits time out (injected), as on a stalled GPU: the cap must hold through a timeout too.
    {
        constexpr VkDeviceSize kBig = 16u << 20;
        constexpr std::uint32_t kSubmits = 4 * VulkanQueue::kMaxQueuedCommandBuffers;
        void *src_map = nullptr, *dst_map = nullptr;
        VkBuffer big_src = d.buffer(kBig, usage, &src_map);
        VkBuffer big_dst = d.buffer(kBig, usage, &dst_map);
        EXPECT(big_src && big_dst);
        add_buffer(7, big_src, kBig);
        add_buffer(8, big_dst, kBig);
        std::memset(src_map, 0x5A, kBig);
        CommandList big_copy;
        EXPECT(big_copy.copy_buffer({8, 0, 7, 0, kBig}));
        EXPECT(big_copy.close());
        const CommandList* big_lists[] = {&big_copy};

        real_allocate_command_buffers = context.vk.vkAllocateCommandBuffers;
        real_get_semaphore_counter_value = context.vk.vkGetSemaphoreCounterValue;
        real_wait_semaphores = context.vk.vkWaitSemaphores;
        context.vk.vkAllocateCommandBuffers = counting_allocate_command_buffers;
        context.vk.vkGetSemaphoreCounterValue = holding_semaphore_counter;
        context.vk.vkWaitSemaphores = stalling_wait_semaphores;
        allocated_command_buffers = 0;
        injected_stalls.store(0);
        hold_progress.store(true);
        for (std::uint32_t i = 0; i < kSubmits; ++i) EXPECT(direct.execute(big_lists) == ReplayResult::success);
        hold_progress.store(false);
        context.vk.vkAllocateCommandBuffers = real_allocate_command_buffers;
        context.vk.vkGetSemaphoreCounterValue = real_get_semaphore_counter_value;
        context.vk.vkWaitSemaphores = real_wait_semaphores;
        EXPECT(direct.signal(fence.semaphore(), 5));
        EXPECT(fence.wait(5, 20'000'000'000ull));
        std::printf("backlog: %u submits, %u command buffers allocated (cap %zu), %u waits timed out\n", kSubmits,
                    allocated_command_buffers, VulkanQueue::kMaxQueuedCommandBuffers, injected_stalls.load());
        EXPECT(injected_stalls.load() == kStalls);  // the queue did reach the cap and wait
        EXPECT(allocated_command_buffers <= VulkanQueue::kMaxQueuedCommandBuffers);
        EXPECT(static_cast<std::uint8_t*>(dst_map)[kBig - 1] == 0x5A);
    }

    EXPECT(direct.idle() && compute.idle() && copy.idle());
    if (d.validation) {
        std::printf("validation errors: %u\n", d.validation_errors());
        EXPECT(d.validation_errors() == 0);
    }
    std::printf("vk_queue_test: OK\n");
    return 0;
}