diff options
| author | tedin7 <110300616+tedin7@users.noreply.github.com> | 2026-11-23 15:11:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 08:11:02 -0600 |
| commit | e76cb47fb500b74f51d0aa51e680cc72beed09c9 (patch) | |
| tree | c958a0f618e0b3d6b1b54f7298635e04ba43160f /runtime/test/d3d12_queue_share_test.cpp | |
| parent | 82c6e08dd5a18ee46e8f82eefd265d9d9687afd0 (diff) | |
Runtime: D3D12 queues share a VkQueue when the GPU has no free one (Intel UHD 770 queueCount 1), plus a CPU-signal hang fix (#5110)
Squash-merged after review of PR #5110 - Runtime: D3D12 queues share a VkQueue when the GPU has no free one (Intel UHD 770 queueCount 1), plus a CPU-signal hang fix
Diffstat (limited to 'runtime/test/d3d12_queue_share_test.cpp')
| -rw-r--r-- | runtime/test/d3d12_queue_share_test.cpp | 442 |
1 files changed, 442 insertions, 0 deletions
diff --git a/runtime/test/d3d12_queue_share_test.cpp b/runtime/test/d3d12_queue_share_test.cpp new file mode 100644 index 000000000..4eca02cd6 --- /dev/null +++ b/runtime/test/d3d12_queue_share_test.cpp | |||
| @@ -0,0 +1,442 @@ | |||
| 1 | // D3D12 queues on shared Vulkan queues (docs/d3d12_vulkan.md section 4.4, "More D3D12 queues than Vulkan queues"). | ||
| 2 | // | ||
| 3 | // A GPU whose queue family exposes one VkQueue (the Intel UHD 770 on Windows, lavapipe) used to refuse the second | ||
| 4 | // D3D12 queue with E_OUTOFMEMORY, so a title with a DIRECT and a COMPUTE queue stopped at boot. The layer now puts a | ||
| 5 | // D3D12 queue on a VkQueue another one already uses when every VkQueue of its families is taken. Cases: | ||
| 6 | // many 36 queues (12 per type, more than the VkQueues of any GPU we know: the RTX 5080 has 26), then a buffer | ||
| 7 | // copied through all 36 in a chain linked by fences, submitted consumer first (every Wait before its | ||
| 8 | // Signal) and then producer first; the readback must be exact. | ||
| 9 | // deferred a CPU Signal that has to wait behind a queue Signal the GPU has not reached, and a queue that waits | ||
| 10 | // for the CPU value: the queue must run once the CPU value lands (on one VkQueue or two). | ||
| 11 | // release a fence released right after the GPU passed the queue Signal its deferred CPU Signal waited behind, | ||
| 12 | // while another queue holds a batch for the CPU value: the batch runs, and under the validation layer | ||
| 13 | // the semaphore is not destroyed under it. | ||
| 14 | // stress 4 threads, 9 queues (3 per type), each thread copying a buffer through 3 queues per iteration with | ||
| 15 | // a CPU-gated producer; every iteration's readback must be exact. | ||
| 16 | // With LEONIDA_VK_MAX_QUEUES_PER_FAMILY=1 and LEONIDA_VK_GRAPHICS_FAMILY_ONLY=1 every GPU looks like the UHD 770 (one | ||
| 17 | // family, one VkQueue) and every D3D12 queue shares that VkQueue, as on lavapipe; with the first alone, same-type | ||
| 18 | // queues share their family's VkQueue. No game data. | ||
| 19 | #include "../shims/d3d12_com.h" | ||
| 20 | |||
| 21 | #include <wrl/client.h> | ||
| 22 | |||
| 23 | #include <atomic> | ||
| 24 | #include <cerrno> | ||
| 25 | #include <cstdint> | ||
| 26 | #include <cstdio> | ||
| 27 | #include <cstdlib> | ||
| 28 | #include <cstring> | ||
| 29 | #include <string> | ||
| 30 | #include <thread> | ||
| 31 | #include <vector> | ||
| 32 | |||
| 33 | using Microsoft::WRL::ComPtr; | ||
| 34 | |||
| 35 | namespace { | ||
| 36 | |||
| 37 | constexpr UINT64 bytes = 1024; | ||
| 38 | constexpr UINT words = UINT(bytes / 4); | ||
| 39 | constexpr D3D12_COMMAND_LIST_TYPE types[] = {D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_LIST_TYPE_COMPUTE, | ||
| 40 | D3D12_COMMAND_LIST_TYPE_COPY}; | ||
| 41 | |||
| 42 | const char* type_name(D3D12_COMMAND_LIST_TYPE type) { | ||
| 43 | return type == D3D12_COMMAND_LIST_TYPE_DIRECT ? "DIRECT" : type == D3D12_COMMAND_LIST_TYPE_COMPUTE ? "COMPUTE" | ||
| 44 | : "COPY"; | ||
| 45 | } | ||
| 46 | |||
| 47 | // The watchdog: a stuck case ends the process with its name instead of a ctest timeout with none. | ||
| 48 | std::atomic<ULONGLONG> deadline{0}; | ||
| 49 | std::atomic<const char*> current_case{"start"}; | ||
| 50 | std::string case_names[64]; | ||
| 51 | int case_count = 0; | ||
| 52 | void arm(const std::string& name, ULONGLONG ms) { // called from the main thread only | ||
| 53 | std::string& slot = case_names[case_count++ % 64]; | ||
| 54 | slot = name; | ||
| 55 | current_case = slot.c_str(); | ||
| 56 | deadline = GetTickCount64() + ms; | ||
| 57 | } | ||
| 58 | |||
| 59 | bool fail(const char* what, int line) { | ||
| 60 | std::fprintf(stderr, "case \"%s\": line %d: %s\n", current_case.load(), line, what); | ||
| 61 | return false; | ||
| 62 | } | ||
| 63 | #define CHECK(c) do { if (!(c)) return fail(#c, __LINE__); } while (0) | ||
| 64 | |||
| 65 | std::uint32_t pattern(std::uint32_t seed, std::uint32_t i) { | ||
| 66 | std::uint32_t x = seed * 0x9e3779b1u + i * 0x85ebca6bu + 0x1234567u; | ||
| 67 | x ^= x >> 15; x *= 0x2c1b3c6du; x ^= x >> 12; x *= 0x297a2d39u; x ^= x >> 15; | ||
| 68 | return x | 0x01000000u; // never zero, so a zero-filled or stale buffer cannot match | ||
| 69 | } | ||
| 70 | |||
| 71 | ComPtr<ID3D12Resource> buffer(ID3D12Device* device, D3D12_HEAP_TYPE heap, D3D12_RESOURCE_STATES state) { | ||
| 72 | D3D12_HEAP_PROPERTIES properties{}; | ||
| 73 | properties.Type = heap; | ||
| 74 | D3D12_RESOURCE_DESC desc{}; | ||
| 75 | desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; | ||
| 76 | desc.Width = bytes; | ||
| 77 | desc.Height = desc.DepthOrArraySize = desc.MipLevels = 1; | ||
| 78 | desc.SampleDesc.Count = 1; | ||
| 79 | desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; | ||
| 80 | ComPtr<ID3D12Resource> resource; | ||
| 81 | if (FAILED(device->CreateCommittedResource(&properties, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr, | ||
| 82 | IID_PPV_ARGS(&resource)))) | ||
| 83 | return {}; | ||
| 84 | return resource; | ||
| 85 | } | ||
| 86 | |||
| 87 | bool fill(ID3D12Resource* upload, std::uint32_t seed) { | ||
| 88 | std::uint32_t* mapped = nullptr; | ||
| 89 | const D3D12_RANGE none{0, 0}; | ||
| 90 | CHECK(SUCCEEDED(upload->Map(0, &none, reinterpret_cast<void**>(&mapped)))); | ||
| 91 | for (UINT i = 0; i < words; ++i) mapped[i] = pattern(seed, i); | ||
| 92 | upload->Unmap(0, nullptr); | ||
| 93 | return true; | ||
| 94 | } | ||
| 95 | |||
| 96 | bool exact(ID3D12Resource* readback, std::uint32_t seed) { | ||
| 97 | std::uint32_t* mapped = nullptr; | ||
| 98 | CHECK(SUCCEEDED(readback->Map(0, nullptr, reinterpret_cast<void**>(&mapped)))); | ||
| 99 | unsigned bad = 0; | ||
| 100 | for (UINT i = 0; i < words; ++i) | ||
| 101 | if (mapped[i] != pattern(seed, i) && bad++ < 3) | ||
| 102 | std::fprintf(stderr, "word %u: got %08x want %08x\n", i, mapped[i], pattern(seed, i)); | ||
| 103 | const D3D12_RANGE none{0, 0}; | ||
| 104 | readback->Unmap(0, &none); | ||
| 105 | if (bad) std::fprintf(stderr, "%u of %u words differ\n", bad, words); | ||
| 106 | return bad == 0 ? true : fail("the readback differs from the data the CPU wrote", __LINE__); | ||
| 107 | } | ||
| 108 | |||
| 109 | D3D12_RESOURCE_BARRIER transition(ID3D12Resource* resource, D3D12_RESOURCE_STATES before, | ||
| 110 | D3D12_RESOURCE_STATES after) { | ||
| 111 | D3D12_RESOURCE_BARRIER barrier{}; | ||
| 112 | barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; | ||
| 113 | barrier.Transition = {resource, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, before, after}; | ||
| 114 | return barrier; | ||
| 115 | } | ||
| 116 | |||
| 117 | // Records src -> dst into the open list and closes it. A default-heap buffer (dst_default, src_default) goes from | ||
| 118 | // COMMON to COPY_SOURCE / COPY_DEST and back; the upload and readback buffers stay in their heap's state. | ||
| 119 | bool record_copy(ID3D12GraphicsCommandList* list, ID3D12Resource* dst, bool dst_default, ID3D12Resource* src, | ||
| 120 | bool src_default) { | ||
| 121 | D3D12_RESOURCE_BARRIER before[2], after[2]; | ||
| 122 | UINT count = 0; | ||
| 123 | if (src_default) { | ||
| 124 | before[count] = transition(src, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_SOURCE); | ||
| 125 | after[count++] = transition(src, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_COMMON); | ||
| 126 | } | ||
| 127 | if (dst_default) { | ||
| 128 | before[count] = transition(dst, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST); | ||
| 129 | after[count++] = transition(dst, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COMMON); | ||
| 130 | } | ||
| 131 | if (count) list->ResourceBarrier(count, before); | ||
| 132 | list->CopyBufferRegion(dst, 0, src, 0, bytes); | ||
| 133 | if (count) list->ResourceBarrier(count, after); | ||
| 134 | return SUCCEEDED(list->Close()); | ||
| 135 | } | ||
| 136 | |||
| 137 | // One closed copy list and its allocator. | ||
| 138 | struct CopyList { | ||
| 139 | ComPtr<ID3D12CommandAllocator> allocator; | ||
| 140 | ComPtr<ID3D12GraphicsCommandList> list; | ||
| 141 | bool make(ID3D12Device* device, D3D12_COMMAND_LIST_TYPE type, ID3D12Resource* dst, bool dst_default, | ||
| 142 | ID3D12Resource* src, bool src_default) { | ||
| 143 | allocator.Reset(); | ||
| 144 | list.Reset(); | ||
| 145 | return SUCCEEDED(device->CreateCommandAllocator(type, IID_PPV_ARGS(&allocator))) && | ||
| 146 | SUCCEEDED(device->CreateCommandList(0, type, allocator.Get(), nullptr, IID_PPV_ARGS(&list))) && | ||
| 147 | record_copy(list.Get(), dst, dst_default, src, src_default); | ||
| 148 | } | ||
| 149 | void execute(ID3D12CommandQueue* queue) const { | ||
| 150 | ID3D12CommandList* lists[] = {list.Get()}; | ||
| 151 | queue->ExecuteCommandLists(1, lists); | ||
| 152 | } | ||
| 153 | }; | ||
| 154 | |||
| 155 | ComPtr<ID3D12Fence> make_fence(ID3D12Device* device) { | ||
| 156 | ComPtr<ID3D12Fence> fence; | ||
| 157 | if (FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)))) return {}; | ||
| 158 | return fence; | ||
| 159 | } | ||
| 160 | |||
| 161 | ComPtr<ID3D12CommandQueue> make_queue(ID3D12Device* device, D3D12_COMMAND_LIST_TYPE type, HRESULT* result = nullptr) { | ||
| 162 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 163 | desc.Type = type; | ||
| 164 | ComPtr<ID3D12CommandQueue> queue; | ||
| 165 | const HRESULT hr = device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue)); | ||
| 166 | if (result) *result = hr; | ||
| 167 | return SUCCEEDED(hr) ? queue : nullptr; | ||
| 168 | } | ||
| 169 | |||
| 170 | // Waits for the fence; a fence that never gets there ends the process (a stuck queue keeps destructors blocked). | ||
| 171 | void wait_for(ID3D12Fence* fence, UINT64 value, DWORD ms = 20000) { | ||
| 172 | HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 173 | if (event && SUCCEEDED(fence->SetEventOnCompletion(value, event)) && | ||
| 174 | WaitForSingleObject(event, ms) == WAIT_OBJECT_0) { | ||
| 175 | CloseHandle(event); | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | std::fprintf(stderr, "case \"%s\": a fence never reached %llu (completed %llu): the queues did not make progress\n", | ||
| 179 | current_case.load(), static_cast<unsigned long long>(value), | ||
| 180 | static_cast<unsigned long long>(fence->GetCompletedValue())); | ||
| 181 | std::fflush(stderr); | ||
| 182 | std::_Exit(1); | ||
| 183 | } | ||
| 184 | |||
| 185 | // 36 queues, more than the VkQueues of any GPU we know, and a buffer copied through all of them. | ||
| 186 | bool many_queues(ID3D12Device* device) { | ||
| 187 | constexpr int per_type = 12; | ||
| 188 | arm("36 queues", 120000); | ||
| 189 | std::vector<ComPtr<ID3D12CommandQueue>> queues; | ||
| 190 | std::vector<D3D12_COMMAND_LIST_TYPE> queue_types; | ||
| 191 | for (int i = 0; i < per_type; ++i) | ||
| 192 | for (const auto type : types) { | ||
| 193 | HRESULT hr = S_OK; | ||
| 194 | auto queue = make_queue(device, type, &hr); | ||
| 195 | if (!queue) { | ||
| 196 | std::fprintf(stderr, "CreateCommandQueue(%s) number %zu: 0x%08lx\n", type_name(type), | ||
| 197 | queues.size() + 1, static_cast<unsigned long>(hr)); | ||
| 198 | return fail("a D3D12 queue was refused", __LINE__); | ||
| 199 | } | ||
| 200 | queues.push_back(queue); | ||
| 201 | queue_types.push_back(type); | ||
| 202 | } | ||
| 203 | std::printf("created %zu D3D12 queues (%d per type)\n", queues.size(), per_type); | ||
| 204 | const std::size_t n = queues.size(); | ||
| 205 | for (const bool consumer_first : {true, false}) { | ||
| 206 | arm(std::string("chain through 36 queues, ") + (consumer_first ? "consumer first" : "producer first"), 120000); | ||
| 207 | const std::uint32_t seed = consumer_first ? 11 : 12; | ||
| 208 | auto upload = buffer(device, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ); | ||
| 209 | auto readback = buffer(device, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); | ||
| 210 | CHECK(upload && readback && fill(upload.Get(), seed)); | ||
| 211 | std::vector<ComPtr<ID3D12Resource>> stage(n - 1); | ||
| 212 | for (auto& s : stage) CHECK((s = buffer(device, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON))); | ||
| 213 | std::vector<ComPtr<ID3D12Fence>> fences(n); | ||
| 214 | for (auto& f : fences) CHECK((f = make_fence(device))); | ||
| 215 | // Step k on queue k: Wait(fence k-1), copy stage k-1 -> stage k (from the upload first, to the readback | ||
| 216 | // last), Signal(fence k). | ||
| 217 | std::vector<CopyList> lists(n); | ||
| 218 | for (std::size_t k = 0; k < n; ++k) { | ||
| 219 | ID3D12Resource* src = k == 0 ? upload.Get() : stage[k - 1].Get(); | ||
| 220 | ID3D12Resource* dst = k == n - 1 ? readback.Get() : stage[k].Get(); | ||
| 221 | CHECK(lists[k].make(device, queue_types[k], dst, k != n - 1, src, k != 0)); | ||
| 222 | } | ||
| 223 | for (std::size_t i = 0; i < n; ++i) { | ||
| 224 | const std::size_t k = consumer_first ? n - 1 - i : i; | ||
| 225 | if (k) CHECK(SUCCEEDED(queues[k]->Wait(fences[k - 1].Get(), 1))); | ||
| 226 | lists[k].execute(queues[k].Get()); | ||
| 227 | CHECK(SUCCEEDED(queues[k]->Signal(fences[k].Get(), 1))); | ||
| 228 | } | ||
| 229 | wait_for(fences[n - 1].Get(), 1); | ||
| 230 | if (!exact(readback.Get(), seed)) return false; | ||
| 231 | std::printf("%s: exact\n", current_case.load()); | ||
| 232 | } | ||
| 233 | return true; | ||
| 234 | } | ||
| 235 | |||
| 236 | // Queue a signals f = 1 behind a gate the CPU opens last; the CPU signals f = 2 before that, which the layer applies | ||
| 237 | // only once the GPU has passed 1 (a Vulkan timeline must not be set past a pending signal); queue b waits for f = 2 | ||
| 238 | // and copies. b must run once the CPU value lands. | ||
| 239 | bool deferred_cpu_signal(ID3D12Device* device, D3D12_COMMAND_LIST_TYPE ta, D3D12_COMMAND_LIST_TYPE tb) { | ||
| 240 | arm(std::string("deferred CPU Signal, ") + type_name(ta) + " then " + type_name(tb), 30000); | ||
| 241 | auto a = make_queue(device, ta); | ||
| 242 | auto b = make_queue(device, tb); | ||
| 243 | auto gate = make_fence(device), f = make_fence(device), done = make_fence(device); | ||
| 244 | auto upload = buffer(device, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ); | ||
| 245 | auto readback = buffer(device, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); | ||
| 246 | const std::uint32_t seed = 100 + UINT(ta) * 8 + UINT(tb); | ||
| 247 | CopyList copy; | ||
| 248 | CHECK(a && b && gate && f && done && upload && readback && fill(upload.Get(), seed)); | ||
| 249 | CHECK(copy.make(device, tb, readback.Get(), false, upload.Get(), false)); | ||
| 250 | CHECK(SUCCEEDED(a->Wait(gate.Get(), 1))); | ||
| 251 | CHECK(SUCCEEDED(a->Signal(f.Get(), 1))); | ||
| 252 | CHECK(SUCCEEDED(f->Signal(2))); | ||
| 253 | CHECK(SUCCEEDED(b->Wait(f.Get(), 2))); | ||
| 254 | copy.execute(b.Get()); | ||
| 255 | CHECK(SUCCEEDED(b->Signal(done.Get(), 1))); | ||
| 256 | CHECK(SUCCEEDED(gate->Signal(1))); | ||
| 257 | wait_for(done.Get(), 1); | ||
| 258 | if (!exact(readback.Get(), seed)) return false; | ||
| 259 | std::printf("%s: completed, exact\n", current_case.load()); | ||
| 260 | return true; | ||
| 261 | } | ||
| 262 | |||
| 263 | // A fence released just after the GPU passed queue a's Signal of 1, while the CPU's Signal of 2 is still deferred | ||
| 264 | // behind it and queue b holds a batch that waits for 2. Either the fence wait thread or the release applies the CPU | ||
| 265 | // value; the fence reached 2 before it was released, so b's batch must run (as on D3D12, where the CPU Signal lands at | ||
| 266 | // once), and the semaphore must outlive that batch (VUID-vkDestroySemaphore-semaphore-05149, formerly -01137, under | ||
| 267 | // the validation layer). Every iteration's readback must hold the copy, never the sentinel the CPU left there. | ||
| 268 | bool release_while_held(ID3D12Device* device, D3D12_COMMAND_LIST_TYPE type, int iterations) { | ||
| 269 | arm(std::string("fence released while a batch waits for its deferred CPU Signal, ") + type_name(type), | ||
| 270 | 30000 + ULONGLONG(iterations) * 200); | ||
| 271 | constexpr std::uint32_t sentinel = 0xdeadbeefu; | ||
| 272 | auto a = make_queue(device, type), b = make_queue(device, type); | ||
| 273 | auto upload = buffer(device, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ); | ||
| 274 | auto readback = buffer(device, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); | ||
| 275 | CHECK(a && b && upload && readback && fill(upload.Get(), 7)); | ||
| 276 | int dropped = 0; | ||
| 277 | for (int i = 0; i < iterations; ++i) { | ||
| 278 | std::uint32_t* mapped = nullptr; | ||
| 279 | CHECK(SUCCEEDED(readback->Map(0, nullptr, reinterpret_cast<void**>(&mapped)))); | ||
| 280 | for (UINT w = 0; w < words; ++w) mapped[w] = sentinel; | ||
| 281 | readback->Unmap(0, nullptr); | ||
| 282 | auto gate = make_fence(device), f = make_fence(device), done = make_fence(device); | ||
| 283 | CopyList copy; | ||
| 284 | CHECK(gate && f && done && copy.make(device, type, readback.Get(), false, upload.Get(), false)); | ||
| 285 | CHECK(SUCCEEDED(a->Wait(gate.Get(), 1))); | ||
| 286 | CHECK(SUCCEEDED(a->Signal(f.Get(), 1))); | ||
| 287 | CHECK(SUCCEEDED(f->Signal(2))); // deferred: a's Signal of 1 is not on the GPU yet | ||
| 288 | CHECK(SUCCEEDED(b->Wait(f.Get(), 2))); | ||
| 289 | copy.execute(b.Get()); | ||
| 290 | CHECK(SUCCEEDED(b->Signal(done.Get(), 1))); | ||
| 291 | CHECK(SUCCEEDED(gate->Signal(1))); | ||
| 292 | const ULONGLONG give_up = GetTickCount64() + 5000; | ||
| 293 | while (f->GetCompletedValue() < 1) | ||
| 294 | CHECK(GetTickCount64() < give_up); | ||
| 295 | f.Reset(); // the release, as soon as the GPU has passed a's Signal | ||
| 296 | wait_for(done.Get(), 1); // b's Signal comes after its batch | ||
| 297 | CHECK(SUCCEEDED(readback->Map(0, nullptr, reinterpret_cast<void**>(&mapped)))); | ||
| 298 | bool copied = true; | ||
| 299 | for (UINT w = 0; w < words; ++w) copied &= mapped[w] == pattern(7, w); | ||
| 300 | const D3D12_RANGE none{0, 0}; | ||
| 301 | readback->Unmap(0, &none); | ||
| 302 | dropped += !copied; | ||
| 303 | } | ||
| 304 | std::printf("%s: %d of %d batches ran\n", current_case.load(), iterations - dropped, iterations); | ||
| 305 | CHECK(dropped == 0); | ||
| 306 | return true; | ||
| 307 | } | ||
| 308 | |||
| 309 | // 4 threads copy a buffer through 3 of 9 queues (3 per type) per iteration, the producer held until the thread's | ||
| 310 | // CPU gate opens; submissions are in dependency order, so no thread waits for another. | ||
| 311 | bool stress(ID3D12Device* device, int iterations) { | ||
| 312 | constexpr int threads = 4, per_type = 3, queue_count = 3 * per_type; | ||
| 313 | arm("stress: 4 threads on 9 queues", 30000 + ULONGLONG(iterations) * 500); | ||
| 314 | ComPtr<ID3D12CommandQueue> queues[queue_count]; | ||
| 315 | D3D12_COMMAND_LIST_TYPE queue_types[queue_count]; | ||
| 316 | for (int i = 0; i < queue_count; ++i) { | ||
| 317 | queue_types[i] = types[i / per_type]; // same-type queues next to each other: a chain shares VkQueues | ||
| 318 | CHECK((queues[i] = make_queue(device, queue_types[i]))); | ||
| 319 | } | ||
| 320 | std::atomic<int> failures{0}, completed{0}; | ||
| 321 | auto worker = [&](int t) { | ||
| 322 | auto upload = buffer(device, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ); | ||
| 323 | auto readback = buffer(device, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); | ||
| 324 | auto mid0 = buffer(device, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON); | ||
| 325 | auto mid1 = buffer(device, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COMMON); | ||
| 326 | ComPtr<ID3D12Fence> gate = make_fence(device); | ||
| 327 | ComPtr<ID3D12Fence> fences[3] = {make_fence(device), make_fence(device), make_fence(device)}; | ||
| 328 | if (!upload || !readback || !mid0 || !mid1 || !gate || !fences[0] || !fences[1] || !fences[2]) { | ||
| 329 | ++failures; | ||
| 330 | return; | ||
| 331 | } | ||
| 332 | for (int i = 0; i < iterations && !failures; ++i) { | ||
| 333 | const std::uint32_t seed = std::uint32_t(t) * 100003u + std::uint32_t(i); | ||
| 334 | const int k = (2 * t + i) % queue_count; | ||
| 335 | const int q[3] = {k, (k + 1) % queue_count, (k + 2) % queue_count}; | ||
| 336 | CopyList lists[3]; | ||
| 337 | const UINT64 v = UINT64(i) + 1; | ||
| 338 | if (!fill(upload.Get(), seed) || | ||
| 339 | !lists[0].make(device, queue_types[q[0]], mid0.Get(), true, upload.Get(), false) || | ||
| 340 | !lists[1].make(device, queue_types[q[1]], mid1.Get(), true, mid0.Get(), true) || | ||
| 341 | !lists[2].make(device, queue_types[q[2]], readback.Get(), false, mid1.Get(), true) || | ||
| 342 | FAILED(queues[q[0]]->Wait(gate.Get(), v))) { | ||
| 343 | ++failures; | ||
| 344 | return; | ||
| 345 | } | ||
| 346 | for (int s = 0; s < 3; ++s) { | ||
| 347 | if (s && FAILED(queues[q[s]]->Wait(fences[s - 1].Get(), v))) ++failures; | ||
| 348 | lists[s].execute(queues[q[s]].Get()); | ||
| 349 | if (FAILED(queues[q[s]]->Signal(fences[s].Get(), v))) ++failures; | ||
| 350 | } | ||
| 351 | if (FAILED(gate->Signal(v))) ++failures; | ||
| 352 | wait_for(fences[2].Get(), v); | ||
| 353 | if (!exact(readback.Get(), seed)) ++failures; | ||
| 354 | ++completed; | ||
| 355 | } | ||
| 356 | }; | ||
| 357 | std::vector<std::thread> pool; | ||
| 358 | for (int t = 0; t < threads; ++t) pool.emplace_back(worker, t); | ||
| 359 | for (auto& thread : pool) thread.join(); | ||
| 360 | std::printf("stress: %d of %d iterations completed exact over %d threads\n", completed.load(), | ||
| 361 | threads * iterations, threads); | ||
| 362 | CHECK(failures == 0 && completed == threads * iterations); | ||
| 363 | return true; | ||
| 364 | } | ||
| 365 | |||
| 366 | constexpr int most_iterations = 1000000; | ||
| 367 | |||
| 368 | // Exit code 2 and the usage line, before any device exists: a misspelt case or count must not pass with nothing run. | ||
| 369 | int usage(const char* problem, const char* value) { | ||
| 370 | std::fprintf(stderr, "%s%s\nusage: d3d12_queue_share_test [--case many|deferred|release|stress|all] " | ||
| 371 | "[--iterations N] (N from 1 to %d, default 100)\n", | ||
| 372 | problem, value, most_iterations); | ||
| 373 | return 2; | ||
| 374 | } | ||
| 375 | |||
| 376 | bool known_case(const std::string& which) { | ||
| 377 | for (const char* name : {"many", "deferred", "release", "stress", "all"}) | ||
| 378 | if (which == name) return true; | ||
| 379 | return false; | ||
| 380 | } | ||
| 381 | |||
| 382 | // The whole argument as a decimal count from 1 to most_iterations, or 0. | ||
| 383 | int parse_iterations(const char* text) { | ||
| 384 | char* end = nullptr; | ||
| 385 | errno = 0; | ||
| 386 | const long value = std::strtol(text, &end, 10); | ||
| 387 | if (end == text || *end || errno == ERANGE || value < 1 || value > most_iterations) return 0; | ||
| 388 | return int(value); | ||
| 389 | } | ||
| 390 | |||
| 391 | } // namespace | ||
| 392 | |||
| 393 | int main(int argc, char** argv) { | ||
| 394 | std::string which = "all"; | ||
| 395 | int iterations = 100; | ||
| 396 | for (int i = 1; i < argc; ++i) { | ||
| 397 | if (!std::strcmp(argv[i], "--case") && i + 1 < argc) { | ||
| 398 | which = argv[++i]; | ||
| 399 | if (!known_case(which)) return usage("unknown case: ", which.c_str()); | ||
| 400 | } else if (!std::strcmp(argv[i], "--iterations") && i + 1 < argc) { | ||
| 401 | iterations = parse_iterations(argv[++i]); | ||
| 402 | if (!iterations) return usage("not a whole number of iterations in range: ", argv[i]); | ||
| 403 | } else { | ||
| 404 | return usage("unknown argument: ", argv[i]); | ||
| 405 | } | ||
| 406 | } | ||
| 407 | std::thread([] { | ||
| 408 | for (;;) { | ||
| 409 | Sleep(100); | ||
| 410 | const ULONGLONG limit = deadline.load(); | ||
| 411 | if (limit && GetTickCount64() > limit) { | ||
| 412 | std::fprintf(stderr, "HANG in case \"%s\"\n", current_case.load()); | ||
| 413 | std::fflush(stderr); | ||
| 414 | std::_Exit(1); | ||
| 415 | } | ||
| 416 | } | ||
| 417 | }).detach(); | ||
| 418 | |||
| 419 | ComPtr<ID3D12Device> device; | ||
| 420 | if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&device)))) { | ||
| 421 | // With LEONIDA_VK_VALIDATION set, no device means no validation (the layer is missing): a failure, not a skip, | ||
| 422 | // so a ctest run that asked for validation never passes or skips without it. | ||
| 423 | char validation[8] = {}; | ||
| 424 | size_t validation_length = 0; | ||
| 425 | const bool wants_validation = getenv_s(&validation_length, validation, "LEONIDA_VK_VALIDATION") == 0 && | ||
| 426 | validation_length > 1 && validation[0] != '0'; | ||
| 427 | std::puts(wants_validation ? "Vulkan standalone device unavailable with LEONIDA_VK_VALIDATION set (see the [rt] line above)" | ||
| 428 | : "Vulkan standalone device unavailable"); | ||
| 429 | return wants_validation ? 1 : 77; | ||
| 430 | } | ||
| 431 | bool ok = true; | ||
| 432 | if (which == "all" || which == "many") ok &= many_queues(device.Get()); | ||
| 433 | if (which == "all" || which == "deferred") | ||
| 434 | for (const auto ta : types) | ||
| 435 | for (const auto tb : types) ok &= deferred_cpu_signal(device.Get(), ta, tb); | ||
| 436 | if (which == "all" || which == "release") | ||
| 437 | for (const auto type : types) ok &= release_while_held(device.Get(), type, iterations); | ||
| 438 | if (which == "all" || which == "stress") ok &= stress(device.Get(), iterations); | ||
| 439 | deadline = 0; | ||
| 440 | std::puts(ok ? "PASS" : "FAIL"); | ||
| 441 | return ok ? 0 : 1; | ||
| 442 | } | ||