diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 19:22:39 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 19:22:39 +0100 |
| commit | fbc11e973d5a1de721f8b70cd5721a8fe709fc3c (patch) | |
| tree | c541babd5f1994164514bf5ddaa212d1fe3b3c1c /runtime/test/d3d12_queue_handover_test.cpp | |
| parent | 49475796c226c8511b9470bf97a4daf57b8af323 (diff) | |
D3D12 queues: hold a batch that waits for a Signal not yet submitted, so layouts follow dependency order
A queue's ExecuteCommandLists is replayed against one LayoutTracker in call order, so a batch that waits (cross-queue) for a Signal submitted later was
recorded against layouts from before the work it waits for. Track, per fence, the highest Signal value submitted (CPU Signal and the creation value
count); a queue holds a batch, and what follows it, while a Wait names a higher value, and the thread that submits the satisfying Signal releases the
held work in dependency order on its own VkQueue. Reports a queue destroyed with held work and a fence released under a waiter; nothing blocks.
Adds d3d12_queue_handover_test (27 queue-type assignments x 6 submission orders, two producers, CPU Signal cases, never-signaled and released fences,
byte-exact against the host runtime) and updates docs section 4.4 and the M4 row. Two existing tests that used an unsignaled Wait as a GPU gate or
expected a synchronous submit failure under a pending Wait now declare the Signal first.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/d3d12_queue_handover_test.cpp')
| -rw-r--r-- | runtime/test/d3d12_queue_handover_test.cpp | 577 |
1 files changed, 577 insertions, 0 deletions
diff --git a/runtime/test/d3d12_queue_handover_test.cpp b/runtime/test/d3d12_queue_handover_test.cpp new file mode 100644 index 000000000..c445fdfe6 --- /dev/null +++ b/runtime/test/d3d12_queue_handover_test.cpp | |||
| @@ -0,0 +1,577 @@ | |||
| 1 | // Cross-queue Wait-before-Signal with a texture and a buffer handed over (docs/d3d12_vulkan.md section 4.4). | ||
| 2 | // | ||
| 3 | // d3d12_queue_wait_test covers Wait-before-Signal with nothing handed over. Here the COPY, COMPUTE and DIRECT queues | ||
| 4 | // pass a texture and a buffer around, and the consumer is submitted BEFORE the producer it waits for, in every order: | ||
| 5 | // produce(queue a) upload buffer -> texture + buffer (CopyTextureRegion / CopyBufferRegion, COMMON -> COPY_DEST) | ||
| 6 | // relay(queue b) texture + buffer -> second texture + buffer | ||
| 7 | // consume(queue c) texture + buffer -> readback buffer | ||
| 8 | // linked by fences (a signals f1, b waits f1 and signals f2, c waits f2 and signals f3, the CPU waits f3), for every | ||
| 9 | // assignment of queue types to a, b and c (27, repeats on separate queues) and every one of the 6 orders the three | ||
| 10 | // submissions can reach the queues in. Then: a consumer waiting for a texture from one queue and a buffer from another | ||
| 11 | // (two waits), a chain released by a CPU Signal (the producer waits for a fence the CPU sets after everything is | ||
| 12 | // submitted), a CPU Signal before the Wait, and two layer-only cases that must not hang: a fence nobody signals (the | ||
| 13 | // other queues keep working, the held batch never runs, the queue is destroyed and the layer reports it), and a fence | ||
| 14 | // released while a batch waits for it. | ||
| 15 | // | ||
| 16 | // The same cases run on the Vulkan layer and on the host D3D12 runtime (the adapter is named in the output). Results are | ||
| 17 | // compared byte for byte with the data written by the CPU: a consumer replayed against the layout from before its | ||
| 18 | // producer ran reads a zero-filled texture and fails here (and with LEONIDA_VK_VALIDATION=1 under sync validation). | ||
| 19 | // No game data. | ||
| 20 | #include "../shims/d3d12_com.h" | ||
| 21 | |||
| 22 | #include <dxgi1_4.h> | ||
| 23 | #include <io.h> | ||
| 24 | #include <share.h> | ||
| 25 | #include <sys/stat.h> | ||
| 26 | #include <fcntl.h> | ||
| 27 | #include <wrl/client.h> | ||
| 28 | |||
| 29 | #include <algorithm> | ||
| 30 | #include <array> | ||
| 31 | #include <atomic> | ||
| 32 | #include <cstdint> | ||
| 33 | #include <cstdio> | ||
| 34 | #include <cstdlib> | ||
| 35 | #include <cstring> | ||
| 36 | #include <functional> | ||
| 37 | #include <string> | ||
| 38 | #include <thread> | ||
| 39 | #include <vector> | ||
| 40 | |||
| 41 | using Microsoft::WRL::ComPtr; | ||
| 42 | |||
| 43 | namespace { | ||
| 44 | |||
| 45 | constexpr UINT dim = 16; // 16 x 16 RGBA8 | ||
| 46 | constexpr UINT pitch = D3D12_TEXTURE_DATA_PITCH_ALIGNMENT; | ||
| 47 | constexpr UINT64 buffer_bytes = 1024; | ||
| 48 | constexpr UINT64 texture_offset = 1024; // in the upload and readback buffers; a multiple of 512 | ||
| 49 | constexpr UINT64 staging_bytes = texture_offset + UINT64(dim) * pitch; | ||
| 50 | |||
| 51 | // The watchdog: a stuck case ends the process with a message instead of a ctest timeout with none. | ||
| 52 | std::atomic<ULONGLONG> deadline{0}; | ||
| 53 | std::atomic<const char*> current_case{"start"}; | ||
| 54 | void arm(const char* name, ULONGLONG ms = 20000) { | ||
| 55 | current_case = name; | ||
| 56 | deadline = GetTickCount64() + ms; | ||
| 57 | } | ||
| 58 | |||
| 59 | std::uint32_t pattern(std::uint32_t seed, std::uint32_t i) { | ||
| 60 | std::uint32_t x = seed * 0x9e3779b1u + i * 0x85ebca6bu + 0x1234567u; | ||
| 61 | x ^= x >> 15; x *= 0x2c1b3c6du; x ^= x >> 12; x *= 0x297a2d39u; x ^= x >> 15; | ||
| 62 | return x | 0x01000000u; // never zero, so a zero-filled or stale resource cannot match | ||
| 63 | } | ||
| 64 | |||
| 65 | enum Type { copy_q = 0, compute_q = 1, direct_q = 2 }; | ||
| 66 | constexpr const char* type_name[] = {"COPY", "COMPUTE", "DIRECT"}; | ||
| 67 | constexpr D3D12_COMMAND_LIST_TYPE list_type[] = {D3D12_COMMAND_LIST_TYPE_COPY, D3D12_COMMAND_LIST_TYPE_COMPUTE, | ||
| 68 | D3D12_COMMAND_LIST_TYPE_DIRECT}; | ||
| 69 | constexpr int pool_size = 3; // queues per type: distinct roles of one type need distinct queues | ||
| 70 | |||
| 71 | struct Rig { | ||
| 72 | ID3D12Device* device = nullptr; | ||
| 73 | ComPtr<ID3D12CommandQueue> queues[3][pool_size]; | ||
| 74 | int available[3] = {0, 0, 0}; | ||
| 75 | }; | ||
| 76 | |||
| 77 | D3D12_RESOURCE_DESC buffer_desc(UINT64 bytes) { | ||
| 78 | D3D12_RESOURCE_DESC desc{}; | ||
| 79 | desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; | ||
| 80 | desc.Width = bytes; | ||
| 81 | desc.Height = desc.DepthOrArraySize = desc.MipLevels = 1; | ||
| 82 | desc.SampleDesc.Count = 1; | ||
| 83 | desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; | ||
| 84 | return desc; | ||
| 85 | } | ||
| 86 | |||
| 87 | D3D12_RESOURCE_DESC texture_desc() { | ||
| 88 | D3D12_RESOURCE_DESC desc{}; | ||
| 89 | desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 90 | desc.Width = desc.Height = dim; | ||
| 91 | desc.DepthOrArraySize = desc.MipLevels = 1; | ||
| 92 | desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 93 | desc.SampleDesc.Count = 1; | ||
| 94 | return desc; | ||
| 95 | } | ||
| 96 | |||
| 97 | D3D12_RESOURCE_BARRIER transition(ID3D12Resource* resource, D3D12_RESOURCE_STATES before, | ||
| 98 | D3D12_RESOURCE_STATES after) { | ||
| 99 | D3D12_RESOURCE_BARRIER barrier{}; | ||
| 100 | barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; | ||
| 101 | barrier.Transition = {resource, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, before, after}; | ||
| 102 | return barrier; | ||
| 103 | } | ||
| 104 | |||
| 105 | struct Set { | ||
| 106 | ComPtr<ID3D12Resource> texture, buffer; | ||
| 107 | }; | ||
| 108 | |||
| 109 | struct Case { | ||
| 110 | Rig& rig; | ||
| 111 | int seed; | ||
| 112 | ComPtr<ID3D12Resource> upload, readback; | ||
| 113 | Set stage[2]; // [0] written by the producer, [1] by the relay | ||
| 114 | std::vector<ComPtr<ID3D12CommandAllocator>> allocators; | ||
| 115 | std::vector<ComPtr<ID3D12GraphicsCommandList>> lists; | ||
| 116 | std::vector<ComPtr<ID3D12Fence>> fences; | ||
| 117 | bool ok = true; | ||
| 118 | |||
| 119 | explicit Case(Rig& r, int s) : rig(r), seed(s) {} | ||
| 120 | |||
| 121 | bool fail(const char* what, int line) { | ||
| 122 | std::fprintf(stderr, "case \"%s\" (seed %d): line %d: %s\n", current_case.load(), seed, line, what); | ||
| 123 | ok = false; | ||
| 124 | return false; | ||
| 125 | } | ||
| 126 | #define CHECK(c) do { if (!(c)) return fail(#c, __LINE__); } while (0) | ||
| 127 | |||
| 128 | bool make_resources() { | ||
| 129 | ID3D12Device* d = rig.device; | ||
| 130 | D3D12_HEAP_PROPERTIES def{D3D12_HEAP_TYPE_DEFAULT}, up{D3D12_HEAP_TYPE_UPLOAD}, rb{D3D12_HEAP_TYPE_READBACK}; | ||
| 131 | const auto staging = buffer_desc(staging_bytes); | ||
| 132 | const auto buf = buffer_desc(buffer_bytes); | ||
| 133 | const auto tex = texture_desc(); | ||
| 134 | CHECK(SUCCEEDED(d->CreateCommittedResource(&up, D3D12_HEAP_FLAG_NONE, &staging, | ||
| 135 | D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&upload)))); | ||
| 136 | CHECK(SUCCEEDED(d->CreateCommittedResource(&rb, D3D12_HEAP_FLAG_NONE, &staging, D3D12_RESOURCE_STATE_COPY_DEST, | ||
| 137 | nullptr, IID_PPV_ARGS(&readback)))); | ||
| 138 | for (auto& s : stage) { | ||
| 139 | CHECK(SUCCEEDED(d->CreateCommittedResource(&def, D3D12_HEAP_FLAG_NONE, &tex, D3D12_RESOURCE_STATE_COMMON, | ||
| 140 | nullptr, IID_PPV_ARGS(&s.texture)))); | ||
| 141 | CHECK(SUCCEEDED(d->CreateCommittedResource(&def, D3D12_HEAP_FLAG_NONE, &buf, D3D12_RESOURCE_STATE_COMMON, | ||
| 142 | nullptr, IID_PPV_ARGS(&s.buffer)))); | ||
| 143 | } | ||
| 144 | std::uint8_t* mapped = nullptr; | ||
| 145 | const D3D12_RANGE none{0, 0}; | ||
| 146 | CHECK(SUCCEEDED(upload->Map(0, &none, reinterpret_cast<void**>(&mapped)))); | ||
| 147 | for (UINT64 i = 0; i < buffer_bytes / 4; ++i) | ||
| 148 | reinterpret_cast<std::uint32_t*>(mapped)[i] = pattern(seed, std::uint32_t(i)); | ||
| 149 | for (UINT y = 0; y < dim; ++y) | ||
| 150 | for (UINT x = 0; x < dim; ++x) | ||
| 151 | reinterpret_cast<std::uint32_t*>(mapped + texture_offset + y * pitch)[x] = | ||
| 152 | pattern(seed + 7919, y * dim + x); | ||
| 153 | upload->Unmap(0, nullptr); | ||
| 154 | return true; | ||
| 155 | } | ||
| 156 | |||
| 157 | ID3D12Fence* fence(int i) { | ||
| 158 | while (int(fences.size()) <= i) { | ||
| 159 | ComPtr<ID3D12Fence> f; | ||
| 160 | if (FAILED(rig.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&f)))) return nullptr; | ||
| 161 | fences.push_back(f); | ||
| 162 | } | ||
| 163 | return fences[i].Get(); | ||
| 164 | } | ||
| 165 | |||
| 166 | static D3D12_TEXTURE_COPY_LOCATION footprint(ID3D12Resource* resource) { | ||
| 167 | D3D12_TEXTURE_COPY_LOCATION loc{}; | ||
| 168 | loc.pResource = resource; | ||
| 169 | loc.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; | ||
| 170 | loc.PlacedFootprint = {texture_offset, {DXGI_FORMAT_R8G8B8A8_UNORM, dim, dim, 1, pitch}}; | ||
| 171 | return loc; | ||
| 172 | } | ||
| 173 | static D3D12_TEXTURE_COPY_LOCATION subresource(ID3D12Resource* resource) { | ||
| 174 | D3D12_TEXTURE_COPY_LOCATION loc{}; | ||
| 175 | loc.pResource = resource; | ||
| 176 | loc.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; | ||
| 177 | return loc; | ||
| 178 | } | ||
| 179 | |||
| 180 | // A closed list of `type`: moves `src` (null: the upload buffer) to `dst` (null: the readback buffer), the parts in | ||
| 181 | // `mask` (1 texture, 2 buffer), with COMMON -> COPY_SOURCE / COPY_DEST before and COMMON again after. | ||
| 182 | ID3D12GraphicsCommandList* make_list(Type type, const Set* src, const Set* dst, int mask) { | ||
| 183 | ComPtr<ID3D12CommandAllocator> alloc; | ||
| 184 | ComPtr<ID3D12GraphicsCommandList> list; | ||
| 185 | if (FAILED(rig.device->CreateCommandAllocator(list_type[type], IID_PPV_ARGS(&alloc))) || | ||
| 186 | FAILED(rig.device->CreateCommandList(0, list_type[type], alloc.Get(), nullptr, IID_PPV_ARGS(&list)))) | ||
| 187 | return nullptr; | ||
| 188 | std::vector<D3D12_RESOURCE_BARRIER> before, after; | ||
| 189 | const auto track = [&](ID3D12Resource* r, D3D12_RESOURCE_STATES state) { | ||
| 190 | before.push_back(transition(r, D3D12_RESOURCE_STATE_COMMON, state)); | ||
| 191 | after.push_back(transition(r, state, D3D12_RESOURCE_STATE_COMMON)); | ||
| 192 | }; | ||
| 193 | if (src) { | ||
| 194 | if (mask & 1) track(src->texture.Get(), D3D12_RESOURCE_STATE_COPY_SOURCE); | ||
| 195 | if (mask & 2) track(src->buffer.Get(), D3D12_RESOURCE_STATE_COPY_SOURCE); | ||
| 196 | } | ||
| 197 | if (dst) { | ||
| 198 | if (mask & 1) track(dst->texture.Get(), D3D12_RESOURCE_STATE_COPY_DEST); | ||
| 199 | if (mask & 2) track(dst->buffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST); | ||
| 200 | } | ||
| 201 | if (!before.empty()) list->ResourceBarrier(UINT(before.size()), before.data()); | ||
| 202 | if (mask & 2) | ||
| 203 | list->CopyBufferRegion(dst ? dst->buffer.Get() : readback.Get(), 0, | ||
| 204 | src ? src->buffer.Get() : upload.Get(), 0, buffer_bytes); | ||
| 205 | if (mask & 1) { | ||
| 206 | const auto to = dst ? subresource(dst->texture.Get()) : footprint(readback.Get()); | ||
| 207 | const auto from = src ? subresource(src->texture.Get()) : footprint(upload.Get()); | ||
| 208 | list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr); | ||
| 209 | } | ||
| 210 | if (!after.empty()) list->ResourceBarrier(UINT(after.size()), after.data()); | ||
| 211 | if (FAILED(list->Close())) return nullptr; | ||
| 212 | allocators.push_back(alloc); | ||
| 213 | lists.push_back(list); | ||
| 214 | return list.Get(); | ||
| 215 | } | ||
| 216 | |||
| 217 | // One submission, in the order the call reaches the queue: Wait(s), the list, Signal. | ||
| 218 | struct Job { | ||
| 219 | Type type; | ||
| 220 | int slot; // which queue of the type | ||
| 221 | const Set* src; | ||
| 222 | const Set* dst; | ||
| 223 | int mask; | ||
| 224 | std::vector<std::pair<int, UINT64>> waits; // (fence, value) | ||
| 225 | int signal = -1; // fence signaled with 1; -1: none | ||
| 226 | }; | ||
| 227 | |||
| 228 | bool submit(const Job& job) { | ||
| 229 | ID3D12CommandQueue* queue = rig.queues[job.type][job.slot].Get(); | ||
| 230 | ID3D12GraphicsCommandList* list = make_list(job.type, job.src, job.dst, job.mask); | ||
| 231 | CHECK(queue && list); | ||
| 232 | for (const auto& [f, v] : job.waits) { | ||
| 233 | ID3D12Fence* fe = fence(f); | ||
| 234 | CHECK(fe && SUCCEEDED(queue->Wait(fe, v))); | ||
| 235 | } | ||
| 236 | ID3D12CommandList* batch[] = {list}; | ||
| 237 | queue->ExecuteCommandLists(1, batch); | ||
| 238 | if (job.signal >= 0) { | ||
| 239 | ID3D12Fence* fe = fence(job.signal); | ||
| 240 | CHECK(fe && SUCCEEDED(queue->Signal(fe, 1))); | ||
| 241 | } | ||
| 242 | return true; | ||
| 243 | } | ||
| 244 | |||
| 245 | bool wait_fence(int f, UINT64 value) { | ||
| 246 | HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 247 | ID3D12Fence* fe = fence(f); | ||
| 248 | CHECK(event && fe && SUCCEEDED(fe->SetEventOnCompletion(value, event))); | ||
| 249 | if (WaitForSingleObject(event, 15000) != WAIT_OBJECT_0) { | ||
| 250 | // A stuck queue can keep the destructors blocked: end the process without cleaning up. | ||
| 251 | std::fprintf(stderr, "case \"%s\" (seed %d): fence %d never reached %llu (completed %llu)\n", | ||
| 252 | current_case.load(), seed, f, static_cast<unsigned long long>(value), | ||
| 253 | static_cast<unsigned long long>(fe->GetCompletedValue())); | ||
| 254 | std::fflush(stderr); | ||
| 255 | std::_Exit(1); | ||
| 256 | } | ||
| 257 | CloseHandle(event); | ||
| 258 | return true; | ||
| 259 | } | ||
| 260 | |||
| 261 | // Compares the readback buffer with what the upload buffer held. | ||
| 262 | bool verify() { | ||
| 263 | std::uint8_t* mapped = nullptr; | ||
| 264 | CHECK(SUCCEEDED(readback->Map(0, nullptr, reinterpret_cast<void**>(&mapped)))); | ||
| 265 | unsigned bad = 0; | ||
| 266 | for (UINT64 i = 0; i < buffer_bytes / 4; ++i) { | ||
| 267 | const auto got = reinterpret_cast<const std::uint32_t*>(mapped)[i]; | ||
| 268 | if (got != pattern(seed, std::uint32_t(i)) && bad++ < 3) | ||
| 269 | std::fprintf(stderr, "buffer word %llu: got %08x want %08x\n", static_cast<unsigned long long>(i), got, | ||
| 270 | pattern(seed, std::uint32_t(i))); | ||
| 271 | } | ||
| 272 | for (UINT y = 0; y < dim; ++y) | ||
| 273 | for (UINT x = 0; x < dim; ++x) { | ||
| 274 | const auto got = reinterpret_cast<const std::uint32_t*>(mapped + texture_offset + y * pitch)[x]; | ||
| 275 | if (got != pattern(seed + 7919, y * dim + x) && bad++ < 3) | ||
| 276 | std::fprintf(stderr, "texel (%u,%u): got %08x want %08x\n", x, y, got, | ||
| 277 | pattern(seed + 7919, y * dim + x)); | ||
| 278 | } | ||
| 279 | readback->Unmap(0, nullptr); | ||
| 280 | if (bad) std::fprintf(stderr, "%u words differ\n", bad); | ||
| 281 | return bad == 0 ? true : fail("result differs from the data the CPU wrote", __LINE__); | ||
| 282 | } | ||
| 283 | }; | ||
| 284 | |||
| 285 | // A queue slot per role: the n-th role of a type uses the n-th queue of the type. | ||
| 286 | struct Roles { | ||
| 287 | Type type[3]; | ||
| 288 | int slot[3]; | ||
| 289 | bool fits(const Rig& rig) const { | ||
| 290 | for (int r = 0; r < 3; ++r) | ||
| 291 | if (slot[r] >= rig.available[type[r]]) return false; | ||
| 292 | return true; | ||
| 293 | } | ||
| 294 | static Roles make(Type a, Type b, Type c) { | ||
| 295 | Roles roles{{a, b, c}, {0, 0, 0}}; | ||
| 296 | for (int r = 0; r < 3; ++r) | ||
| 297 | for (int p = 0; p < r; ++p) | ||
| 298 | if (roles.type[p] == roles.type[r]) ++roles.slot[r]; | ||
| 299 | return roles; | ||
| 300 | } | ||
| 301 | }; | ||
| 302 | |||
| 303 | int cases_run = 0; | ||
| 304 | |||
| 305 | // Chain a -> b -> c, with the three submissions in `order` (a permutation of 0 = produce, 1 = relay, 2 = consume). | ||
| 306 | bool chain(Rig& rig, const Roles& roles, const std::array<int, 3>& order, int seed) { | ||
| 307 | std::string name = std::string("chain ") + type_name[roles.type[0]] + "->" + type_name[roles.type[1]] + "->" + | ||
| 308 | type_name[roles.type[2]] + " submitted " + char('A' + order[0]) + char('A' + order[1]) + | ||
| 309 | char('A' + order[2]); | ||
| 310 | arm(name.c_str()); | ||
| 311 | Case c(rig, seed); | ||
| 312 | if (!c.make_resources()) return false; | ||
| 313 | const Case::Job jobs[3] = { | ||
| 314 | {roles.type[0], roles.slot[0], nullptr, &c.stage[0], 3, {}, 0}, | ||
| 315 | {roles.type[1], roles.slot[1], &c.stage[0], &c.stage[1], 3, {{0, 1}}, 1}, | ||
| 316 | {roles.type[2], roles.slot[2], &c.stage[1], nullptr, 3, {{1, 1}}, 2}, | ||
| 317 | }; | ||
| 318 | for (int i : order) | ||
| 319 | if (!c.submit(jobs[i])) return false; | ||
| 320 | ++cases_run; | ||
| 321 | return c.wait_fence(2, 1) && c.verify(); | ||
| 322 | } | ||
| 323 | |||
| 324 | // The consumer waits for the texture from one queue and the buffer from another. | ||
| 325 | bool two_producers(Rig& rig, Type a, Type b, Type consumer, const std::array<int, 3>& order, int seed) { | ||
| 326 | const int sa = 0, sb = a == b ? 1 : 0, sc = (consumer == a) + (consumer == b); | ||
| 327 | if (sa >= rig.available[a] || sb >= rig.available[b] || sc >= rig.available[consumer]) return true; | ||
| 328 | std::string name = std::string("two producers ") + type_name[a] + "(texture)+" + type_name[b] + "(buffer)->" + | ||
| 329 | type_name[consumer] + " submitted " + char('A' + order[0]) + char('A' + order[1]) + | ||
| 330 | char('A' + order[2]); | ||
| 331 | arm(name.c_str()); | ||
| 332 | Case c(rig, seed); | ||
| 333 | if (!c.make_resources()) return false; | ||
| 334 | const Case::Job jobs[3] = { | ||
| 335 | {a, sa, nullptr, &c.stage[0], 1, {}, 0}, | ||
| 336 | {b, sb, nullptr, &c.stage[0], 2, {}, 1}, | ||
| 337 | {consumer, sc, &c.stage[0], nullptr, 3, {{0, 1}, {1, 1}}, 2}, | ||
| 338 | }; | ||
| 339 | for (int i : order) | ||
| 340 | if (!c.submit(jobs[i])) return false; | ||
| 341 | ++cases_run; | ||
| 342 | return c.wait_fence(2, 1) && c.verify(); | ||
| 343 | } | ||
| 344 | |||
| 345 | // The whole chain is submitted consumer first and the producer waits for a fence the CPU signals last. | ||
| 346 | bool cpu_released_chain(Rig& rig, const Roles& roles, int seed) { | ||
| 347 | std::string name = std::string("CPU Signal releases chain ") + type_name[roles.type[0]] + "->" + | ||
| 348 | type_name[roles.type[1]] + "->" + type_name[roles.type[2]]; | ||
| 349 | arm(name.c_str()); | ||
| 350 | Case c(rig, seed); | ||
| 351 | if (!c.make_resources()) return false; | ||
| 352 | const Case::Job jobs[3] = { | ||
| 353 | {roles.type[0], roles.slot[0], nullptr, &c.stage[0], 3, {{3, 1}}, 0}, | ||
| 354 | {roles.type[1], roles.slot[1], &c.stage[0], &c.stage[1], 3, {{0, 1}}, 1}, | ||
| 355 | {roles.type[2], roles.slot[2], &c.stage[1], nullptr, 3, {{1, 1}}, 2}, | ||
| 356 | }; | ||
| 357 | if (!c.submit(jobs[2]) || !c.submit(jobs[1]) || !c.submit(jobs[0])) return false; | ||
| 358 | Sleep(20); | ||
| 359 | if (c.fence(2)->GetCompletedValue() != 0) return c.fail("the chain ran before the CPU signaled its gate", __LINE__); | ||
| 360 | if (FAILED(c.fence(3)->Signal(1))) return c.fail("CPU Signal failed", __LINE__); | ||
| 361 | ++cases_run; | ||
| 362 | return c.wait_fence(2, 1) && c.verify(); | ||
| 363 | } | ||
| 364 | |||
| 365 | // A CPU Signal that comes before the Wait (nothing is held), and one that comes after the batch was submitted. | ||
| 366 | bool cpu_gate(Rig& rig, Type producer, Type consumer, bool signal_first, int seed) { | ||
| 367 | if (producer == consumer && rig.available[producer] < 2) return true; | ||
| 368 | const int cslot = producer == consumer ? 1 : 0; | ||
| 369 | std::string name = std::string("CPU Signal ") + (signal_first ? "before" : "after") + " the consumer's Wait, " + | ||
| 370 | type_name[producer] + "->" + type_name[consumer]; | ||
| 371 | arm(name.c_str()); | ||
| 372 | Case c(rig, seed); | ||
| 373 | if (!c.make_resources()) return false; | ||
| 374 | if (!c.submit({producer, 0, nullptr, &c.stage[0], 3, {}, 0}) || !c.wait_fence(0, 1)) return false; | ||
| 375 | if (signal_first && FAILED(c.fence(1)->Signal(1))) return c.fail("CPU Signal failed", __LINE__); | ||
| 376 | if (!c.submit({consumer, cslot, &c.stage[0], nullptr, 3, {{1, 1}}, 2})) return false; | ||
| 377 | if (!signal_first) { | ||
| 378 | Sleep(20); | ||
| 379 | if (c.fence(2)->GetCompletedValue() != 0) return c.fail("the consumer ran before the CPU Signal", __LINE__); | ||
| 380 | if (FAILED(c.fence(1)->Signal(1))) return c.fail("CPU Signal failed", __LINE__); | ||
| 381 | } | ||
| 382 | ++cases_run; | ||
| 383 | return c.wait_fence(2, 1) && c.verify(); | ||
| 384 | } | ||
| 385 | |||
| 386 | // Runs `body` with stderr redirected to a file and returns what it wrote. | ||
| 387 | std::string capture_stderr(const std::function<void()>& body) { | ||
| 388 | char dir[MAX_PATH], path[MAX_PATH]; | ||
| 389 | GetTempPathA(MAX_PATH, dir); | ||
| 390 | GetTempFileNameA(dir, "qho", 0, path); | ||
| 391 | std::fflush(stderr); | ||
| 392 | const int saved = _dup(2); | ||
| 393 | int file = -1; | ||
| 394 | _sopen_s(&file, path, _O_RDWR | _O_CREAT | _O_TRUNC | _O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE); | ||
| 395 | _dup2(file, 2); | ||
| 396 | body(); | ||
| 397 | std::fflush(stderr); | ||
| 398 | _dup2(saved, 2); | ||
| 399 | _close(saved); | ||
| 400 | _close(file); | ||
| 401 | std::string text; | ||
| 402 | FILE* f = nullptr; | ||
| 403 | if (fopen_s(&f, path, "rb") == 0 && f) { | ||
| 404 | char buffer[512]; | ||
| 405 | std::size_t n; | ||
| 406 | while ((n = std::fread(buffer, 1, sizeof(buffer), f)) > 0) text.append(buffer, n); | ||
| 407 | std::fclose(f); | ||
| 408 | } | ||
| 409 | std::remove(path); | ||
| 410 | return text; | ||
| 411 | } | ||
| 412 | |||
| 413 | // A batch waits for a fence nobody signals. The other queues keep working, the batch never runs, destroying the queue | ||
| 414 | // does not hang and the layer says why. Layer only: the host runtime has nothing to report. | ||
| 415 | bool never_signaled(Rig& rig, int seed) { | ||
| 416 | arm("fence nobody signals"); | ||
| 417 | Case c(rig, seed); | ||
| 418 | if (!c.make_resources()) return false; | ||
| 419 | ComPtr<ID3D12CommandQueue> victim; | ||
| 420 | const D3D12_COMMAND_QUEUE_DESC desc{D3D12_COMMAND_LIST_TYPE_DIRECT}; | ||
| 421 | if (FAILED(rig.device->CreateCommandQueue(&desc, IID_PPV_ARGS(&victim)))) { | ||
| 422 | std::puts("never-signaled case: no spare queue, skipped"); | ||
| 423 | return true; | ||
| 424 | } | ||
| 425 | // The victim's batch would overwrite stage[1]; the working chain below uses stage[0] and the readback buffer. | ||
| 426 | ID3D12Fence* gate = c.fence(5); | ||
| 427 | ID3D12GraphicsCommandList* held = c.make_list(direct_q, nullptr, &c.stage[1], 3); | ||
| 428 | if (!gate || !held) return c.fail("setup", __LINE__); | ||
| 429 | ID3D12CommandList* batch[] = {held}; | ||
| 430 | if (FAILED(victim->Wait(gate, 5))) return c.fail("Wait", __LINE__); | ||
| 431 | victim->ExecuteCommandLists(1, batch); | ||
| 432 | if (FAILED(victim->Signal(c.fence(6), 1))) return c.fail("Signal", __LINE__); | ||
| 433 | // Other queues are not held up behind it. | ||
| 434 | const Case::Job work[2] = {{copy_q, 0, nullptr, &c.stage[0], 3, {}, 0}, | ||
| 435 | {direct_q, 0, &c.stage[0], nullptr, 3, {{0, 1}}, 2}}; | ||
| 436 | if (!c.submit(work[1]) || !c.submit(work[0]) || !c.wait_fence(2, 1) || !c.verify()) return false; | ||
| 437 | Sleep(100); | ||
| 438 | if (c.fence(6)->GetCompletedValue() != 0) return c.fail("the held batch's Signal was reached", __LINE__); | ||
| 439 | arm("destroy a queue with a held batch"); | ||
| 440 | const std::string log = capture_stderr([&] { victim.Reset(); }); | ||
| 441 | if (log.find("queue destroyed with") == std::string::npos || log.find("never signaled") == std::string::npos) { | ||
| 442 | std::fprintf(stderr, "the layer did not report the held batch; stderr was: \"%s\"\n", log.c_str()); | ||
| 443 | return c.fail("no report for the held batch", __LINE__); | ||
| 444 | } | ||
| 445 | std::printf("never-signaled fence: held, others unaffected, destroyed without a hang; layer says: %s", | ||
| 446 | log.c_str()); | ||
| 447 | ++cases_run; | ||
| 448 | return true; | ||
| 449 | } | ||
| 450 | |||
| 451 | // The fence a batch waits for is released: the batch is dropped and reported, nothing hangs. | ||
| 452 | bool fence_released(Rig& rig, int seed) { | ||
| 453 | arm("fence released while a batch waits"); | ||
| 454 | Case c(rig, seed); | ||
| 455 | if (!c.make_resources()) return false; | ||
| 456 | ComPtr<ID3D12CommandQueue> victim; | ||
| 457 | const D3D12_COMMAND_QUEUE_DESC desc{D3D12_COMMAND_LIST_TYPE_DIRECT}; | ||
| 458 | if (FAILED(rig.device->CreateCommandQueue(&desc, IID_PPV_ARGS(&victim)))) return true; | ||
| 459 | ComPtr<ID3D12Fence> doomed; | ||
| 460 | if (FAILED(rig.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&doomed)))) return false; | ||
| 461 | ID3D12GraphicsCommandList* held = c.make_list(direct_q, nullptr, &c.stage[1], 3); | ||
| 462 | if (!held) return c.fail("setup", __LINE__); | ||
| 463 | ID3D12CommandList* batch[] = {held}; | ||
| 464 | if (FAILED(victim->Wait(doomed.Get(), 9))) return c.fail("Wait", __LINE__); | ||
| 465 | victim->ExecuteCommandLists(1, batch); | ||
| 466 | const std::string log = capture_stderr([&] { doomed.Reset(); victim.Reset(); }); | ||
| 467 | if (log.find("fence was released while a queue still held work") == std::string::npos) { | ||
| 468 | std::fprintf(stderr, "stderr was: \"%s\"\n", log.c_str()); | ||
| 469 | return c.fail("no report for the dropped batch", __LINE__); | ||
| 470 | } | ||
| 471 | ++cases_run; | ||
| 472 | return true; | ||
| 473 | } | ||
| 474 | |||
| 475 | bool run_all(ID3D12Device* device, bool layer) { | ||
| 476 | Rig rig; | ||
| 477 | rig.device = device; | ||
| 478 | for (int t = 0; t < 3; ++t) | ||
| 479 | for (int i = 0; i < pool_size; ++i) { | ||
| 480 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 481 | desc.Type = list_type[t]; | ||
| 482 | if (FAILED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&rig.queues[t][i])))) break; | ||
| 483 | ++rig.available[t]; | ||
| 484 | } | ||
| 485 | std::printf("queues available per type (COPY/COMPUTE/DIRECT): %d/%d/%d\n", rig.available[0], rig.available[1], | ||
| 486 | rig.available[2]); | ||
| 487 | if (!rig.available[0] || !rig.available[1] || !rig.available[2]) return true; // nothing to hand over between | ||
| 488 | |||
| 489 | bool ok = true; | ||
| 490 | int seed = 100, skipped = 0; | ||
| 491 | std::array<int, 3> order{0, 1, 2}; | ||
| 492 | for (int a = 0; a < 3; ++a) | ||
| 493 | for (int b = 0; b < 3; ++b) | ||
| 494 | for (int cc = 0; cc < 3; ++cc) { | ||
| 495 | const Roles roles = Roles::make(Type(a), Type(b), Type(cc)); | ||
| 496 | if (!roles.fits(rig)) { ++skipped; continue; } | ||
| 497 | order = {0, 1, 2}; | ||
| 498 | do { | ||
| 499 | ok &= chain(rig, roles, order, ++seed); | ||
| 500 | } while (std::next_permutation(order.begin(), order.end())); | ||
| 501 | ok &= cpu_released_chain(rig, roles, ++seed); | ||
| 502 | } | ||
| 503 | for (int a = 0; a < 3; ++a) | ||
| 504 | for (int b = 0; b < 3; ++b) | ||
| 505 | for (int cc = 0; cc < 3; ++cc) { | ||
| 506 | order = {0, 1, 2}; | ||
| 507 | do { | ||
| 508 | ok &= two_producers(rig, Type(a), Type(b), Type(cc), order, ++seed); | ||
| 509 | } while (std::next_permutation(order.begin(), order.end())); | ||
| 510 | } | ||
| 511 | for (int p = 0; p < 3; ++p) | ||
| 512 | for (int q = 0; q < 3; ++q) | ||
| 513 | for (bool first : {true, false}) ok &= cpu_gate(rig, Type(p), Type(q), first, ++seed); | ||
| 514 | if (layer) { | ||
| 515 | // The layer hands out a limited number of VkQueues: give the spare ones back for the two cases below. | ||
| 516 | for (auto& type_queues : rig.queues) | ||
| 517 | for (int i = 1; i < pool_size; ++i) type_queues[i].Reset(); | ||
| 518 | ok &= never_signaled(rig, ++seed); | ||
| 519 | ok &= fence_released(rig, ++seed); | ||
| 520 | } | ||
| 521 | if (skipped) std::printf("(%d type assignments need more queues than the device gave)\n", skipped); | ||
| 522 | return ok; | ||
| 523 | } | ||
| 524 | |||
| 525 | ComPtr<ID3D12Device> native_device(std::wstring& name) { | ||
| 526 | ComPtr<IDXGIFactory4> factory; | ||
| 527 | if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return {}; | ||
| 528 | for (UINT i = 0;; ++i) { | ||
| 529 | ComPtr<IDXGIAdapter1> adapter; | ||
| 530 | if (factory->EnumAdapters1(i, &adapter) != S_OK) return {}; | ||
| 531 | DXGI_ADAPTER_DESC1 desc{}; | ||
| 532 | if (FAILED(adapter->GetDesc1(&desc)) || (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) continue; | ||
| 533 | ComPtr<ID3D12Device> device; | ||
| 534 | if (FAILED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device)))) continue; | ||
| 535 | name = desc.Description; | ||
| 536 | return device; | ||
| 537 | } | ||
| 538 | } | ||
| 539 | |||
| 540 | } // namespace | ||
| 541 | |||
| 542 | int main() { | ||
| 543 | std::thread([] { | ||
| 544 | for (;;) { | ||
| 545 | Sleep(100); | ||
| 546 | const ULONGLONG limit = deadline.load(); | ||
| 547 | if (limit && GetTickCount64() > limit) { | ||
| 548 | std::fprintf(stderr, "HANG in case \"%s\"\n", current_case.load()); | ||
| 549 | std::fflush(stderr); | ||
| 550 | std::_Exit(1); | ||
| 551 | } | ||
| 552 | } | ||
| 553 | }).detach(); | ||
| 554 | |||
| 555 | ComPtr<ID3D12Device> layer; | ||
| 556 | if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&layer)))) { | ||
| 557 | std::puts("Vulkan standalone device unavailable"); | ||
| 558 | return 77; | ||
| 559 | } | ||
| 560 | arm("Vulkan layer", 300000); | ||
| 561 | cases_run = 0; | ||
| 562 | if (!run_all(layer.Get(), true)) return 1; | ||
| 563 | const int layer_cases = cases_run; | ||
| 564 | std::printf("Vulkan layer: %d cases passed, every one exact\n", layer_cases); | ||
| 565 | |||
| 566 | std::wstring name; | ||
| 567 | if (auto host = native_device(name)) { | ||
| 568 | arm("host runtime", 300000); | ||
| 569 | cases_run = 0; | ||
| 570 | if (!run_all(host.Get(), false)) return 1; | ||
| 571 | std::printf("Host D3D12 (%ls): %d cases passed, every one exact\n", name.c_str(), cases_run); | ||
| 572 | } else { | ||
| 573 | std::puts("no host D3D12 adapter: the host comparison was skipped"); | ||
| 574 | } | ||
| 575 | deadline = 0; | ||
| 576 | return 0; | ||
| 577 | } | ||