diff options
| author | AimLogin <4jt8mn8wjn@privaterelay.appleid.com> | 2026-11-22 10:25:02 +0100 |
|---|---|---|
| committer | AimLogin <4jt8mn8wjn@privaterelay.appleid.com> | 2026-11-22 11:08:53 +0100 |
| commit | 62593f47ca3bfd9f68261292c19567da9f117daa (patch) | |
| tree | 06b688e80f510792b168ebe8317abb29922168b4 /runtime/test/queue_proxy_test.cpp | |
| parent | c0ac5fd6518b3ba9fa66765b8b07099990dadd74 (diff) | |
Runtime: Xbox-ABI proxies for the command queue and the fence (BUILD-231)
Device slots 8 (CreateCommandQueue), 75 (CreateCommandQueue1), 36
(CreateFence) and 32 (OpenSharedHandle of a fence) now return queue and
fence proxies. They forward the public chain in PC order (19 queue slots,
11 or 12 fence slots), refuse and log a slot past it up to the end of the
proxy's table, and name a read past the table through a guard region and
a vectored handler.
Heap, queue and fence proxies are unwrapped wherever a device or queue
method hands them to the real runtime: queue Signal/Wait, device slots
31/34/35/45/46/50. Arrays are copied without exceptions. QueryInterface
answers only the modelled interfaces, and logs one the real object has
but the proxy does not model. Outputs are cleared on failure as for
slots 14/62/76. --trace-d3d12 records proxy calls in the README grammar.
Every function a thunk hands its words to keeps them in the same
positions, because a title passes only as many as the method takes.
Tests: queue_proxy_test (host WARP, host hardware, Vulkan device;
differential per-process error cases, guard faults, identity, threads,
raw-slot client, child-runner self-test), xs_proxy_mock_test (older
devices, fences without Fence1, OpenSharedHandle branches, allocation
failures; three guard-setup fault modes), and device_proxy_test through
the D3D12XboxCreateDevice shim. No game files.
AI-assisted (Claude Code).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/queue_proxy_test.cpp')
| -rw-r--r-- | runtime/test/queue_proxy_test.cpp | 1692 |
1 files changed, 1692 insertions, 0 deletions
diff --git a/runtime/test/queue_proxy_test.cpp b/runtime/test/queue_proxy_test.cpp new file mode 100644 index 000000000..23a550685 --- /dev/null +++ b/runtime/test/queue_proxy_test.cpp | |||
| @@ -0,0 +1,1692 @@ | |||
| 1 | // Xbox-ABI queue and fence proxies (d3d12_xs_device.h), behind the device proxy, on the host PC runtime (WARP, and | ||
| 2 | // the first hardware adapter when there is one) and on the Vulkan-backed device: | ||
| 3 | // - device slots 8, 75, 36 and 32 wrap what they create, and every public queue and fence slot is forwarded to | ||
| 4 | // the slot it names (counted per slot) with the effect the method has; | ||
| 5 | // - error and edge cases give the same HRESULT through the proxy as on the real object (a differential check); | ||
| 6 | // a case the real runtime itself faults on is reported and not compared; | ||
| 7 | // - QueryInterface identity, AddRef/Release across interfaces and release orders; | ||
| 8 | // - heap, queue and fence proxies are unwrapped where the real runtime receives them (Signal/Wait, | ||
| 9 | // SetEventOnMultipleFenceCompletion, MakeResident/Evict/SetResidencyPriority/EnqueueMakeResident, | ||
| 10 | // CreateSharedHandle), and OpenSharedHandle wraps a shared fence; | ||
| 11 | // - slots past the public chain are refused (E_NOTIMPL) up to the end of the table, and a slot past the table | ||
| 12 | // faults on the vtable read in the guard region and is counted; | ||
| 13 | // - calls from eight threads at once, and repeated device, queue and fence creation without leaks; | ||
| 14 | // - the child-process runner of the differential check itself: large output, hangs, crashes, failed starts. | ||
| 15 | // No shaders, game data or Xbox DLLs. LEONIDA_VK_VALIDATION=1 runs the Vulkan device under | ||
| 16 | // VK_LAYER_KHRONOS_validation (with synchronization validation); any validation error fails the test. Under Wine only | ||
| 17 | // the Vulkan part runs. --trace-d3d12 also records every proxy call on the trace channel. Exits 77 when neither a | ||
| 18 | // host D3D12 device nor a Vulkan device can be made. | ||
| 19 | #include "vk_test_device.h" | ||
| 20 | #include "../shims/d3d12_com.h" | ||
| 21 | #include "../shims/d3d12_trace.h" | ||
| 22 | #include "../shims/d3d12_xs_device.h" | ||
| 23 | |||
| 24 | #include <d3d12.h> | ||
| 25 | #include <dxgi1_4.h> | ||
| 26 | |||
| 27 | #include <crtdbg.h> | ||
| 28 | |||
| 29 | #include <atomic> | ||
| 30 | #include <cstdint> | ||
| 31 | #include <cstdio> | ||
| 32 | #include <cstdlib> | ||
| 33 | #include <cstring> | ||
| 34 | #include <cwchar> | ||
| 35 | #include <iterator> | ||
| 36 | #include <string> | ||
| 37 | #include <system_error> | ||
| 38 | #include <thread> | ||
| 39 | #include <vector> | ||
| 40 | |||
| 41 | #define EXPECT(condition) do { \ | ||
| 42 | if (!(condition)) { std::fprintf(stderr, "%s line %d: %s\n", label, __LINE__, #condition); return 1; } \ | ||
| 43 | } while (0) | ||
| 44 | |||
| 45 | namespace { | ||
| 46 | |||
| 47 | namespace xs = rt::d3d12_xs; | ||
| 48 | |||
| 49 | template<class T> | ||
| 50 | struct Ref { | ||
| 51 | T* p = nullptr; | ||
| 52 | Ref() = default; | ||
| 53 | Ref(const Ref&) = delete; | ||
| 54 | Ref& operator=(const Ref&) = delete; | ||
| 55 | ~Ref() { reset(); } | ||
| 56 | void reset() { | ||
| 57 | if (p) p->Release(); | ||
| 58 | p = nullptr; | ||
| 59 | } | ||
| 60 | T* operator->() const { return p; } | ||
| 61 | }; | ||
| 62 | |||
| 63 | struct Event { | ||
| 64 | HANDLE h = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 65 | ~Event() { CloseHandle(h); } | ||
| 66 | bool set_within(DWORD ms) const { return WaitForSingleObject(h, ms) == WAIT_OBJECT_0; } | ||
| 67 | }; | ||
| 68 | |||
| 69 | // {6f1f2d3e-8a4b-4c5d-9e0f-1a2b3c4d5e6f}, {7a2b3c4d-...}: private-data keys of this test. | ||
| 70 | constexpr GUID kDataKey{0x6f1f2d3e, 0x8a4b, 0x4c5d, {0x9e, 0x0f, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e, 0x6f}}; | ||
| 71 | constexpr GUID kInterfaceKey{0x7a2b3c4d, 0x9b5c, 0x4d6e, {0xaf, 0x10, 0x2b, 0x3c, 0x4d, 0x5e, 0x6f, 0x70}}; | ||
| 72 | constexpr GUID kMissingKey{0x8b3c4d5e, 0xac6d, 0x4e7f, {0xb0, 0x21, 0x3c, 0x4d, 0x5e, 0x6f, 0x70, 0x81}}; | ||
| 73 | |||
| 74 | using SlotFn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, | ||
| 75 | std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, | ||
| 76 | std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t); | ||
| 77 | |||
| 78 | // A raw call through slot `slot` of an object's vtable with all words zero, the way a title calls a console method | ||
| 79 | // the PC interface does not declare. | ||
| 80 | std::uint64_t call_slot(void* object, unsigned slot) { | ||
| 81 | void** vtable = *reinterpret_cast<void***>(object); | ||
| 82 | return reinterpret_cast<SlotFn>(vtable[slot])(object, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | ||
| 83 | } | ||
| 84 | |||
| 85 | HRESULT as_hr(std::uint64_t value) { return static_cast<HRESULT>(static_cast<std::uint32_t>(value)); } | ||
| 86 | |||
| 87 | // A raw call through slot `slot` with the given words (the rest zero). | ||
| 88 | std::uint64_t raw_call(void* object, unsigned slot, std::uint64_t a1 = 0, std::uint64_t a2 = 0, | ||
| 89 | std::uint64_t a3 = 0, std::uint64_t a4 = 0) { | ||
| 90 | void** vtable = *reinterpret_cast<void***>(object); | ||
| 91 | return reinterpret_cast<SlotFn>(vtable[slot])(object, a1, a2, a3, a4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); | ||
| 92 | } | ||
| 93 | |||
| 94 | // ---- Structured-exception guards (no C++ objects in these frames) ---- | ||
| 95 | |||
| 96 | ULONG_PTR g_fault_address = 0; | ||
| 97 | bool g_shared_checked = false; // check_shared ran OpenSharedHandle (the backend has shared fences) | ||
| 98 | bool g_shared_created = false; // check_shared made a shared fence and called CreateSharedHandle (slot 31) | ||
| 99 | bool g_reserved_checked = false; // check_slots had reserved resources for UpdateTileMappings/CopyTileMappings | ||
| 100 | |||
| 101 | int record_fault(EXCEPTION_POINTERS* info) { | ||
| 102 | g_fault_address = info->ExceptionRecord->NumberParameters >= 2 ? info->ExceptionRecord->ExceptionInformation[1] : 0; | ||
| 103 | return EXCEPTION_EXECUTE_HANDLER; | ||
| 104 | } | ||
| 105 | |||
| 106 | // The read itself, in a function of its own: clang-cl's __try only catches a fault raised inside a call, not one | ||
| 107 | // raised by a load in the __try body itself (MSVC catches both). | ||
| 108 | __declspec(noinline) void* read_entry(void* const* vtable, unsigned slot) { | ||
| 109 | return *static_cast<void* const volatile*>(&vtable[slot]); | ||
| 110 | } | ||
| 111 | |||
| 112 | // Reads vtable[slot] the way a call through it does. Returns the exception code, or 0 when the read worked. | ||
| 113 | DWORD read_slot_guarded(void* const* vtable, unsigned slot) { | ||
| 114 | g_fault_address = 0; | ||
| 115 | __try { | ||
| 116 | void* volatile entry = read_entry(vtable, slot); | ||
| 117 | (void)entry; | ||
| 118 | return 0; | ||
| 119 | } __except (record_fault(GetExceptionInformation())) { | ||
| 120 | return GetExceptionCode(); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | using Case = HRESULT (*)(void* context); | ||
| 125 | |||
| 126 | // Runs a case; false when it raised an access violation (the real runtime dereferencing a null argument). Any other | ||
| 127 | // exception is not this test's to handle and takes the child process down, which the parent reports as a failure. | ||
| 128 | bool run_guarded(Case fn, void* context, HRESULT* result) { | ||
| 129 | __try { | ||
| 130 | *result = fn(context); | ||
| 131 | return true; | ||
| 132 | } __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER | ||
| 133 | : EXCEPTION_CONTINUE_SEARCH) { | ||
| 134 | return false; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | int wait_for(const char* label, ID3D12Fence* fence, UINT64 value) { | ||
| 139 | Event event; | ||
| 140 | EXPECT(SUCCEEDED(fence->SetEventOnCompletion(value, event.h))); | ||
| 141 | EXPECT(event.set_within(5000)); | ||
| 142 | EXPECT(fence->GetCompletedValue() >= value); | ||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | struct Backend { | ||
| 147 | const char* label; | ||
| 148 | ID3D12Device* device; // the device proxy | ||
| 149 | ID3D12Device* real; // the device behind it (PC runtime or Vulkan layer) | ||
| 150 | bool vulkan; | ||
| 151 | const char* kind; // "warp", "hw" or "vulkan": what a --diff child creates | ||
| 152 | }; | ||
| 153 | |||
| 154 | // ---- 1. Creation, every public slot, and the refused tail ---- | ||
| 155 | |||
| 156 | int check_slots(const Backend& b) { | ||
| 157 | const char* label = b.label; | ||
| 158 | ID3D12Device* device = b.device; | ||
| 159 | EXPECT(xs::is_device_proxy(device)); | ||
| 160 | xs::reset_device_proxy_stats(); | ||
| 161 | const unsigned long device_refs = xs::device_proxy_ref_count(device); | ||
| 162 | { | ||
| 163 | D3D12_COMMAND_QUEUE_DESC direct_desc{}; | ||
| 164 | direct_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; | ||
| 165 | Ref<ID3D12CommandQueue> queue; | ||
| 166 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&direct_desc, IID_PPV_ARGS(&queue.p)))); | ||
| 167 | EXPECT(xs::device_slot_hits(8) == 1); | ||
| 168 | EXPECT(xs::is_queue_proxy(queue.p)); | ||
| 169 | ID3D12CommandQueue* real_queue = xs::queue_proxy_real_queue(queue.p); | ||
| 170 | EXPECT(real_queue && static_cast<void*>(real_queue) != static_cast<void*>(queue.p)); | ||
| 171 | EXPECT(!xs::is_queue_proxy(real_queue) && !xs::is_fence_proxy(queue.p)); | ||
| 172 | EXPECT(xs::queue_proxy_slot_count(queue.p) == 19 && xs::queue_proxy_table_size() == 128); | ||
| 173 | EXPECT(xs::command_object_proxy_ref_count(queue.p) == 1); | ||
| 174 | // The queue keeps its device alive; on the Vulkan layer the real queue also holds the device proxy (its | ||
| 175 | // identity), so the count may rise by two. The end of this function checks that all are given back. | ||
| 176 | EXPECT(xs::device_proxy_ref_count(device) >= device_refs + 1); | ||
| 177 | |||
| 178 | Ref<ID3D12Fence> fence, second; | ||
| 179 | EXPECT(SUCCEEDED(device->CreateFence(5, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); | ||
| 180 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&second.p)))); | ||
| 181 | EXPECT(xs::device_slot_hits(36) == 2); | ||
| 182 | EXPECT(xs::is_fence_proxy(fence.p) && xs::is_fence_proxy(second.p) && !xs::is_queue_proxy(fence.p)); | ||
| 183 | ID3D12Fence* real_fence = xs::fence_proxy_real_fence(fence.p); | ||
| 184 | EXPECT(real_fence && static_cast<void*>(real_fence) != static_cast<void*>(fence.p)); | ||
| 185 | const unsigned fence_slots = xs::fence_proxy_slot_count(fence.p); | ||
| 186 | EXPECT(fence_slots == 11 || fence_slots == 12); | ||
| 187 | EXPECT(xs::fence_proxy_table_size() == 64); | ||
| 188 | |||
| 189 | // Every public queue slot, each counted exactly once per call, with its effect. | ||
| 190 | const auto qhits = [](unsigned s) { return xs::queue_slot_hits(s); }; | ||
| 191 | const auto fhits = [](unsigned s) { return xs::fence_slot_hits(s); }; | ||
| 192 | std::uint64_t before = qhits(4); | ||
| 193 | const UINT value = 0x1234abcd; | ||
| 194 | EXPECT(SUCCEEDED(queue->SetPrivateData(kDataKey, sizeof(value), &value))); // 4 | ||
| 195 | EXPECT(qhits(4) == before + 1); | ||
| 196 | UINT read = 0, size = sizeof(read); | ||
| 197 | before = qhits(3); | ||
| 198 | EXPECT(SUCCEEDED(queue->GetPrivateData(kDataKey, &size, &read)) && read == value && size == sizeof(read)); // 3 | ||
| 199 | EXPECT(qhits(3) == before + 1); | ||
| 200 | before = qhits(5); | ||
| 201 | EXPECT(SUCCEEDED(queue->SetPrivateDataInterface(kInterfaceKey, second.p))); // 5 | ||
| 202 | EXPECT(qhits(5) == before + 1); | ||
| 203 | { | ||
| 204 | IUnknown* stored = nullptr; | ||
| 205 | UINT stored_size = sizeof(stored); | ||
| 206 | EXPECT(SUCCEEDED(queue->GetPrivateData(kInterfaceKey, &stored_size, &stored))); | ||
| 207 | EXPECT(static_cast<void*>(stored) == static_cast<void*>(second.p)); // the proxy, not the real fence | ||
| 208 | if (stored) stored->Release(); | ||
| 209 | } | ||
| 210 | EXPECT(SUCCEEDED(queue->SetPrivateDataInterface(kInterfaceKey, nullptr))); | ||
| 211 | before = qhits(6); | ||
| 212 | EXPECT(SUCCEEDED(queue->SetName(L"queue_proxy_test queue"))); // 6 | ||
| 213 | EXPECT(qhits(6) == before + 1); | ||
| 214 | { | ||
| 215 | Ref<ID3D12Device> owner; | ||
| 216 | before = qhits(7); | ||
| 217 | EXPECT(SUCCEEDED(queue->GetDevice(IID_PPV_ARGS(&owner.p))) && owner.p == device); // 7 | ||
| 218 | EXPECT(qhits(7) == before + 1); | ||
| 219 | } | ||
| 220 | |||
| 221 | // 8/9: tile mappings need a reserved resource; skipped (and said so) where the device has none. | ||
| 222 | { | ||
| 223 | D3D12_RESOURCE_DESC reserved_desc{}; | ||
| 224 | reserved_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; | ||
| 225 | reserved_desc.Width = 2 * D3D12_TILED_RESOURCE_TILE_SIZE_IN_BYTES; | ||
| 226 | reserved_desc.Height = reserved_desc.DepthOrArraySize = reserved_desc.MipLevels = 1; | ||
| 227 | reserved_desc.SampleDesc.Count = 1; | ||
| 228 | reserved_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; | ||
| 229 | Ref<ID3D12Resource> a, c; | ||
| 230 | const bool reserved = | ||
| 231 | SUCCEEDED(device->CreateReservedResource(&reserved_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, | ||
| 232 | IID_PPV_ARGS(&a.p))) && | ||
| 233 | SUCCEEDED(device->CreateReservedResource(&reserved_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, | ||
| 234 | IID_PPV_ARGS(&c.p))); | ||
| 235 | if (reserved) { | ||
| 236 | g_reserved_checked = true; | ||
| 237 | const D3D12_TILED_RESOURCE_COORDINATE origin{}; | ||
| 238 | D3D12_TILE_REGION_SIZE region{}; | ||
| 239 | region.NumTiles = 1; | ||
| 240 | const D3D12_TILE_RANGE_FLAGS null_range = D3D12_TILE_RANGE_FLAG_NULL; | ||
| 241 | const UINT one = 1; | ||
| 242 | before = qhits(8); | ||
| 243 | queue->UpdateTileMappings(a.p, 1, &origin, ®ion, nullptr, 1, &null_range, nullptr, &one, | ||
| 244 | D3D12_TILE_MAPPING_FLAG_NONE); // 8 | ||
| 245 | EXPECT(qhits(8) == before + 1); | ||
| 246 | before = qhits(9); | ||
| 247 | queue->CopyTileMappings(c.p, &origin, a.p, &origin, ®ion, D3D12_TILE_MAPPING_FLAG_NONE); // 9 | ||
| 248 | EXPECT(qhits(9) == before + 1); | ||
| 249 | } else { | ||
| 250 | std::printf("%s: no reserved resources, UpdateTileMappings/CopyTileMappings not called\n", label); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | // 10: ExecuteCommandLists passes the (real, not wrapped) command lists through. | ||
| 255 | { | ||
| 256 | Ref<ID3D12CommandAllocator> allocator; | ||
| 257 | EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, | ||
| 258 | IID_PPV_ARGS(&allocator.p)))); | ||
| 259 | Ref<ID3D12GraphicsCommandList> list; | ||
| 260 | EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, | ||
| 261 | IID_PPV_ARGS(&list.p)))); | ||
| 262 | EXPECT(SUCCEEDED(list->Close())); | ||
| 263 | ID3D12CommandList* lists[] = {list.p}; | ||
| 264 | before = qhits(10); | ||
| 265 | queue->ExecuteCommandLists(1, lists); | ||
| 266 | EXPECT(qhits(10) == before + 1); | ||
| 267 | EXPECT(SUCCEEDED(queue->Signal(fence.p, 6))); | ||
| 268 | if (wait_for(label, fence.p, 6)) return 1; | ||
| 269 | if (b.vulkan) { | ||
| 270 | // The layer's own helpers find their queue behind the proxy. | ||
| 271 | EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); | ||
| 272 | (void)rt::d3d12::queue_uses_calibrated_timestamps(queue.p); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | // 11-13: markers. | ||
| 277 | static const char marker[] = "queue_proxy_test"; | ||
| 278 | before = qhits(11); | ||
| 279 | queue->SetMarker(1, marker, sizeof(marker)); | ||
| 280 | EXPECT(qhits(11) == before + 1); | ||
| 281 | before = qhits(12); | ||
| 282 | queue->BeginEvent(1, marker, sizeof(marker)); | ||
| 283 | EXPECT(qhits(12) == before + 1); | ||
| 284 | before = qhits(13); | ||
| 285 | queue->EndEvent(); | ||
| 286 | EXPECT(qhits(13) == before + 1); | ||
| 287 | |||
| 288 | // 14/15: Signal and Wait take fence proxies; the real queue gets the real fences. | ||
| 289 | before = qhits(14); | ||
| 290 | EXPECT(SUCCEEDED(queue->Signal(fence.p, 7))); | ||
| 291 | EXPECT(qhits(14) == before + 1); | ||
| 292 | if (wait_for(label, fence.p, 7)) return 1; | ||
| 293 | before = qhits(15); | ||
| 294 | EXPECT(SUCCEEDED(queue->Wait(fence.p, 8))); // the queue waits until the CPU signals 8 | ||
| 295 | EXPECT(qhits(15) == before + 1); | ||
| 296 | EXPECT(SUCCEEDED(queue->Signal(second.p, 1))); | ||
| 297 | Sleep(50); | ||
| 298 | EXPECT(second->GetCompletedValue() == 0); // still held by the Wait | ||
| 299 | before = fhits(10); | ||
| 300 | EXPECT(SUCCEEDED(fence->Signal(8))); // fence 10, from the CPU | ||
| 301 | EXPECT(fhits(10) == before + 1); | ||
| 302 | if (wait_for(label, second.p, 1)) return 1; | ||
| 303 | |||
| 304 | // A real fence (made on the real device) passes through a proxy queue unchanged. | ||
| 305 | { | ||
| 306 | Ref<ID3D12Fence> raw; | ||
| 307 | EXPECT(SUCCEEDED(b.real->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&raw.p)))); | ||
| 308 | EXPECT(!xs::is_fence_proxy(raw.p)); | ||
| 309 | EXPECT(SUCCEEDED(queue->Signal(raw.p, 3))); | ||
| 310 | if (wait_for(label, raw.p, 3)) return 1; | ||
| 311 | } | ||
| 312 | |||
| 313 | // 16-18. | ||
| 314 | UINT64 frequency = 0; | ||
| 315 | before = qhits(16); | ||
| 316 | EXPECT(SUCCEEDED(queue->GetTimestampFrequency(&frequency)) && frequency > 0); | ||
| 317 | EXPECT(qhits(16) == before + 1); | ||
| 318 | UINT64 gpu = 0, cpu = 0; | ||
| 319 | before = qhits(17); | ||
| 320 | EXPECT(SUCCEEDED(queue->GetClockCalibration(&gpu, &cpu)) && cpu > 0); | ||
| 321 | EXPECT(qhits(17) == before + 1); | ||
| 322 | before = qhits(18); | ||
| 323 | EXPECT(queue->GetDesc().Type == D3D12_COMMAND_LIST_TYPE_DIRECT); // struct through the hidden pointer | ||
| 324 | EXPECT(qhits(18) == before + 1); | ||
| 325 | D3D12_COMMAND_QUEUE_DESC compute_desc{}; | ||
| 326 | compute_desc.Type = D3D12_COMMAND_LIST_TYPE_COMPUTE; | ||
| 327 | compute_desc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_HIGH; | ||
| 328 | Ref<ID3D12CommandQueue> compute; | ||
| 329 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&compute_desc, IID_PPV_ARGS(&compute.p)))); | ||
| 330 | const D3D12_COMMAND_QUEUE_DESC compute_back = compute->GetDesc(); | ||
| 331 | EXPECT(xs::is_queue_proxy(compute.p) && compute_back.Type == D3D12_COMMAND_LIST_TYPE_COMPUTE); | ||
| 332 | EXPECT(compute_back.Priority == D3D12_COMMAND_QUEUE_PRIORITY_HIGH); | ||
| 333 | |||
| 334 | // Every public fence slot. | ||
| 335 | before = fhits(4); | ||
| 336 | EXPECT(SUCCEEDED(fence->SetPrivateData(kDataKey, sizeof(value), &value))); | ||
| 337 | EXPECT(fhits(4) == before + 1); | ||
| 338 | read = 0; | ||
| 339 | size = sizeof(read); | ||
| 340 | before = fhits(3); | ||
| 341 | EXPECT(SUCCEEDED(fence->GetPrivateData(kDataKey, &size, &read)) && read == value); | ||
| 342 | EXPECT(fhits(3) == before + 1); | ||
| 343 | before = fhits(5); | ||
| 344 | EXPECT(SUCCEEDED(fence->SetPrivateDataInterface(kInterfaceKey, nullptr))); | ||
| 345 | EXPECT(fhits(5) == before + 1); | ||
| 346 | before = fhits(6); | ||
| 347 | EXPECT(SUCCEEDED(fence->SetName(L"queue_proxy_test fence"))); | ||
| 348 | EXPECT(fhits(6) == before + 1); | ||
| 349 | { | ||
| 350 | Ref<ID3D12Device> owner; | ||
| 351 | before = fhits(7); | ||
| 352 | EXPECT(SUCCEEDED(fence->GetDevice(IID_PPV_ARGS(&owner.p))) && owner.p == device); | ||
| 353 | EXPECT(fhits(7) == before + 1); | ||
| 354 | } | ||
| 355 | before = fhits(8); | ||
| 356 | EXPECT(fence->GetCompletedValue() == 8); | ||
| 357 | EXPECT(fhits(8) == before + 1); | ||
| 358 | { | ||
| 359 | Event done; | ||
| 360 | before = fhits(9); | ||
| 361 | EXPECT(SUCCEEDED(fence->SetEventOnCompletion(8, done.h)) && done.set_within(5000)); | ||
| 362 | EXPECT(fhits(9) == before + 1); | ||
| 363 | } | ||
| 364 | if (fence_slots == 12) { | ||
| 365 | Ref<ID3D12Fence1> fence1; | ||
| 366 | EXPECT(SUCCEEDED(fence->QueryInterface(IID_PPV_ARGS(&fence1.p)))); | ||
| 367 | before = fhits(11); | ||
| 368 | EXPECT(fence1->GetCreationFlags() == D3D12_FENCE_FLAG_NONE); | ||
| 369 | EXPECT(fhits(11) == before + 1); | ||
| 370 | } | ||
| 371 | |||
| 372 | // Device slot 75 (ID3D12Device9::CreateCommandQueue1) also wraps its queue. | ||
| 373 | { | ||
| 374 | Ref<ID3D12Device9> device9; | ||
| 375 | if (SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&device9.p)))) { | ||
| 376 | Ref<ID3D12CommandQueue> queue1; | ||
| 377 | const HRESULT hr = device9->CreateCommandQueue1(&direct_desc, __uuidof(ID3D12Device), | ||
| 378 | IID_PPV_ARGS(&queue1.p)); | ||
| 379 | EXPECT(xs::device_slot_hits(75) == 1); | ||
| 380 | EXPECT(SUCCEEDED(hr) && xs::is_queue_proxy(queue1.p)); | ||
| 381 | } else { | ||
| 382 | std::printf("%s: no ID3D12Device9, CreateCommandQueue1 not checked\n", label); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | // The refused tail: every slot from the end of the public chain to the end of the table. | ||
| 387 | for (unsigned slot = 19; slot < xs::queue_proxy_table_size(); ++slot) { | ||
| 388 | before = qhits(slot); | ||
| 389 | EXPECT(as_hr(call_slot(queue.p, slot)) == E_NOTIMPL); | ||
| 390 | EXPECT(qhits(slot) == before + 1 && xs::queue_slot_first_call_seen(slot)); | ||
| 391 | } | ||
| 392 | for (unsigned slot = fence_slots; slot < xs::fence_proxy_table_size(); ++slot) { | ||
| 393 | before = fhits(slot); | ||
| 394 | EXPECT(as_hr(call_slot(fence.p, slot)) == E_NOTIMPL); | ||
| 395 | EXPECT(fhits(slot) == before + 1 && xs::fence_slot_first_call_seen(slot)); | ||
| 396 | } | ||
| 397 | // The objects still work after refused calls. | ||
| 398 | EXPECT(SUCCEEDED(queue->Signal(fence.p, 10))); | ||
| 399 | if (wait_for(label, fence.p, 10)) return 1; | ||
| 400 | EXPECT(xs::command_object_proxy_ref_count(queue.p) == 1 && xs::command_object_proxy_ref_count(fence.p) == 1); | ||
| 401 | } | ||
| 402 | EXPECT(xs::device_proxy_ref_count(device) == device_refs); // every queue and fence gave its reference back | ||
| 403 | std::printf("%s: every public queue and fence slot forwarded and counted; tail refused\n", label); | ||
| 404 | return 0; | ||
| 405 | } | ||
| 406 | |||
| 407 | // ---- 2. The guard region past the tables ---- | ||
| 408 | |||
| 409 | int check_guard(const Backend& b) { | ||
| 410 | const char* label = b.label; | ||
| 411 | EXPECT(xs::command_object_tables_guarded()); | ||
| 412 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 413 | Ref<ID3D12CommandQueue> queue; | ||
| 414 | Ref<ID3D12Fence> fence; | ||
| 415 | EXPECT(SUCCEEDED(b.device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue.p)))); | ||
| 416 | EXPECT(SUCCEEDED(b.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); | ||
| 417 | struct Probe { | ||
| 418 | void* object; | ||
| 419 | unsigned table; | ||
| 420 | std::uint64_t (*overruns)(); | ||
| 421 | } probes[] = {{queue.p, xs::queue_proxy_table_size(), &xs::queue_table_overruns}, | ||
| 422 | {fence.p, xs::fence_proxy_table_size(), &xs::fence_table_overruns}}; | ||
| 423 | for (const Probe& probe : probes) { | ||
| 424 | void* const* vtable = *reinterpret_cast<void* const* const*>(probe.object); | ||
| 425 | EXPECT(read_slot_guarded(vtable, probe.table - 1) == 0); // the last table slot is readable | ||
| 426 | // The first slot past the table, one a page further, and one near the end of the reservation. | ||
| 427 | for (unsigned slot : {probe.table, probe.table + 512, probe.table + 7000}) { | ||
| 428 | const std::uint64_t before = probe.overruns(); | ||
| 429 | EXPECT(read_slot_guarded(vtable, slot) == EXCEPTION_ACCESS_VIOLATION); | ||
| 430 | EXPECT(g_fault_address == reinterpret_cast<ULONG_PTR>(&vtable[slot])); | ||
| 431 | EXPECT(probe.overruns() == before + 1); | ||
| 432 | } | ||
| 433 | } | ||
| 434 | EXPECT(SUCCEEDED(queue->Signal(fence.p, 1))); | ||
| 435 | if (wait_for(label, fence.p, 1)) return 1; | ||
| 436 | std::printf("%s: a slot past either table faults in the guard region and is counted\n", label); | ||
| 437 | return 0; | ||
| 438 | } | ||
| 439 | |||
| 440 | // ---- 3. Same HRESULT through the proxy as on the real object ---- | ||
| 441 | |||
| 442 | // What a case returns when the device lacks the interface the case needs. Not S_FALSE: CreateCommandQueue1 with a | ||
| 443 | // null output returns S_FALSE for a valid description, so a proxy wrongly refusing ID3D12Device9 would match it. | ||
| 444 | constexpr HRESULT kNoInterface = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); | ||
| 445 | |||
| 446 | struct DiffContext { | ||
| 447 | ID3D12Device* device; // proxy or real | ||
| 448 | ID3D12CommandQueue* queue; | ||
| 449 | ID3D12Fence* fence; | ||
| 450 | ID3D12Fence* second; | ||
| 451 | HANDLE event; | ||
| 452 | }; | ||
| 453 | |||
| 454 | struct DiffCase { | ||
| 455 | const char* name; | ||
| 456 | Case run; | ||
| 457 | }; | ||
| 458 | |||
| 459 | // The cases pass null and invalid arguments on purpose: that is what they compare. | ||
| 460 | #pragma warning(push) | ||
| 461 | #pragma warning(disable : 6387) | ||
| 462 | |||
| 463 | HRESULT create_queue_no_output(void* c) { | ||
| 464 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 465 | return static_cast<DiffContext*>(c)->device->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), nullptr); | ||
| 466 | } | ||
| 467 | HRESULT create_queue_bad_type(void* c) { | ||
| 468 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 469 | desc.Type = static_cast<D3D12_COMMAND_LIST_TYPE>(99); | ||
| 470 | void* out = nullptr; | ||
| 471 | const HRESULT hr = static_cast<DiffContext*>(c)->device->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), | ||
| 472 | &out); | ||
| 473 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 474 | return hr; | ||
| 475 | } | ||
| 476 | HRESULT create_queue_bad_priority(void* c) { | ||
| 477 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 478 | desc.Priority = 7; | ||
| 479 | void* out = nullptr; | ||
| 480 | const HRESULT hr = static_cast<DiffContext*>(c)->device->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), | ||
| 481 | &out); | ||
| 482 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 483 | return hr; | ||
| 484 | } | ||
| 485 | HRESULT create_queue_as_fence(void* c) { | ||
| 486 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 487 | void* out = reinterpret_cast<void*>(1); | ||
| 488 | const HRESULT hr = static_cast<DiffContext*>(c)->device->CreateCommandQueue(&desc, __uuidof(ID3D12Fence), &out); | ||
| 489 | if (SUCCEEDED(hr) && out) static_cast<IUnknown*>(out)->Release(); | ||
| 490 | return SUCCEEDED(hr) || out == nullptr ? hr : E_UNEXPECTED; // a failed create must clear the output | ||
| 491 | } | ||
| 492 | HRESULT create_fence_no_output(void* c) { | ||
| 493 | return static_cast<DiffContext*>(c)->device->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), nullptr); | ||
| 494 | } | ||
| 495 | HRESULT create_fence_bad_flags(void* c) { | ||
| 496 | void* out = nullptr; | ||
| 497 | const HRESULT hr = static_cast<DiffContext*>(c)->device->CreateFence(0, static_cast<D3D12_FENCE_FLAGS>(0x80), | ||
| 498 | __uuidof(ID3D12Fence), &out); | ||
| 499 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 500 | return hr; | ||
| 501 | } | ||
| 502 | HRESULT create_fence_as_queue(void* c) { | ||
| 503 | void* out = reinterpret_cast<void*>(1); | ||
| 504 | const HRESULT hr = static_cast<DiffContext*>(c)->device->CreateFence(0, D3D12_FENCE_FLAG_NONE, | ||
| 505 | __uuidof(ID3D12CommandQueue), &out); | ||
| 506 | if (SUCCEEDED(hr) && out) static_cast<IUnknown*>(out)->Release(); | ||
| 507 | return SUCCEEDED(hr) || out == nullptr ? hr : E_UNEXPECTED; | ||
| 508 | } | ||
| 509 | HRESULT create_queue1_no_output(void* c) { | ||
| 510 | ID3D12Device9* device9 = nullptr; | ||
| 511 | if (FAILED(static_cast<DiffContext*>(c)->device->QueryInterface(IID_PPV_ARGS(&device9)))) return kNoInterface; | ||
| 512 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 513 | const HRESULT hr = device9->CreateCommandQueue1(&desc, __uuidof(ID3D12Device), __uuidof(ID3D12CommandQueue), | ||
| 514 | nullptr); | ||
| 515 | device9->Release(); | ||
| 516 | return hr; | ||
| 517 | } | ||
| 518 | HRESULT queue_get_device_wrong_iid(void* c) { | ||
| 519 | void* out = reinterpret_cast<void*>(1); | ||
| 520 | const HRESULT hr = static_cast<DiffContext*>(c)->queue->GetDevice(__uuidof(ID3D12Fence), &out); | ||
| 521 | if (SUCCEEDED(hr) && out) static_cast<IUnknown*>(out)->Release(); | ||
| 522 | return hr; | ||
| 523 | } | ||
| 524 | HRESULT queue_get_device_no_output(void* c) { | ||
| 525 | return static_cast<DiffContext*>(c)->queue->GetDevice(__uuidof(ID3D12Device), nullptr); | ||
| 526 | } | ||
| 527 | HRESULT queue_query_no_output(void* c) { | ||
| 528 | return static_cast<DiffContext*>(c)->queue->QueryInterface(__uuidof(ID3D12CommandQueue), nullptr); | ||
| 529 | } | ||
| 530 | HRESULT queue_query_wrong_iid(void* c) { | ||
| 531 | void* out = reinterpret_cast<void*>(1); | ||
| 532 | const HRESULT hr = static_cast<DiffContext*>(c)->queue->QueryInterface(__uuidof(ID3D12Device), &out); | ||
| 533 | if (SUCCEEDED(hr) && out) static_cast<IUnknown*>(out)->Release(); | ||
| 534 | return SUCCEEDED(hr) || out == nullptr ? hr : E_UNEXPECTED; | ||
| 535 | } | ||
| 536 | HRESULT queue_private_data_missing(void* c) { | ||
| 537 | UINT size = 4; | ||
| 538 | UINT data = 0; | ||
| 539 | return static_cast<DiffContext*>(c)->queue->GetPrivateData(kMissingKey, &size, &data); | ||
| 540 | } | ||
| 541 | HRESULT queue_private_data_small(void* c) { | ||
| 542 | const UINT64 value = 42; | ||
| 543 | ID3D12CommandQueue* queue = static_cast<DiffContext*>(c)->queue; | ||
| 544 | if (FAILED(queue->SetPrivateData(kDataKey, sizeof(value), &value))) return E_FAIL; | ||
| 545 | UINT size = 1; | ||
| 546 | UINT64 data = 0; | ||
| 547 | const HRESULT hr = queue->GetPrivateData(kDataKey, &size, &data); | ||
| 548 | return hr == S_OK && size != 1 ? E_UNEXPECTED : hr; | ||
| 549 | } | ||
| 550 | HRESULT queue_private_data_size_query(void* c) { | ||
| 551 | const UINT64 value = 42; | ||
| 552 | ID3D12CommandQueue* queue = static_cast<DiffContext*>(c)->queue; | ||
| 553 | if (FAILED(queue->SetPrivateData(kDataKey, sizeof(value), &value))) return E_FAIL; | ||
| 554 | UINT size = 0; | ||
| 555 | const HRESULT hr = queue->GetPrivateData(kDataKey, &size, nullptr); | ||
| 556 | return SUCCEEDED(hr) && size != sizeof(value) ? E_UNEXPECTED : hr; | ||
| 557 | } | ||
| 558 | HRESULT queue_signal_null(void* c) { return static_cast<DiffContext*>(c)->queue->Signal(nullptr, 1); } | ||
| 559 | HRESULT queue_wait_null(void* c) { return static_cast<DiffContext*>(c)->queue->Wait(nullptr, 1); } | ||
| 560 | HRESULT queue_frequency_null(void* c) { return static_cast<DiffContext*>(c)->queue->GetTimestampFrequency(nullptr); } | ||
| 561 | HRESULT queue_calibration_null(void* c) { | ||
| 562 | return static_cast<DiffContext*>(c)->queue->GetClockCalibration(nullptr, nullptr); | ||
| 563 | } | ||
| 564 | HRESULT fence_private_data_missing(void* c) { | ||
| 565 | UINT size = 4; | ||
| 566 | UINT data = 0; | ||
| 567 | return static_cast<DiffContext*>(c)->fence->GetPrivateData(kMissingKey, &size, &data); | ||
| 568 | } | ||
| 569 | HRESULT fence_event_reached_null_handle(void* c) { | ||
| 570 | return static_cast<DiffContext*>(c)->fence->SetEventOnCompletion(0, nullptr); // reached: returns at once | ||
| 571 | } | ||
| 572 | HRESULT multiple_fences_count0(void* c) { | ||
| 573 | auto* d = static_cast<DiffContext*>(c); | ||
| 574 | ID3D12Device1* device1 = nullptr; | ||
| 575 | if (FAILED(d->device->QueryInterface(IID_PPV_ARGS(&device1)))) return kNoInterface; | ||
| 576 | ID3D12Fence* both[] = {d->fence, d->second}; | ||
| 577 | const UINT64 values[] = {0, 0}; | ||
| 578 | const HRESULT hr = device1->SetEventOnMultipleFenceCompletion(both, values, 0, D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL, | ||
| 579 | d->event); | ||
| 580 | device1->Release(); | ||
| 581 | return hr; | ||
| 582 | } | ||
| 583 | HRESULT multiple_fences_bad_flags(void* c) { | ||
| 584 | auto* d = static_cast<DiffContext*>(c); | ||
| 585 | ID3D12Device1* device1 = nullptr; | ||
| 586 | if (FAILED(d->device->QueryInterface(IID_PPV_ARGS(&device1)))) return kNoInterface; | ||
| 587 | ID3D12Fence* both[] = {d->fence, d->second}; | ||
| 588 | const UINT64 values[] = {0, 0}; | ||
| 589 | const HRESULT hr = device1->SetEventOnMultipleFenceCompletion( | ||
| 590 | both, values, 2, static_cast<D3D12_MULTIPLE_FENCE_WAIT_FLAGS>(7), d->event); | ||
| 591 | device1->Release(); | ||
| 592 | return hr; | ||
| 593 | } | ||
| 594 | HRESULT multiple_fences_null_entry(void* c) { | ||
| 595 | auto* d = static_cast<DiffContext*>(c); | ||
| 596 | ID3D12Device1* device1 = nullptr; | ||
| 597 | if (FAILED(d->device->QueryInterface(IID_PPV_ARGS(&device1)))) return kNoInterface; | ||
| 598 | ID3D12Fence* both[] = {d->fence, nullptr}; | ||
| 599 | const UINT64 values[] = {0, 0}; | ||
| 600 | const HRESULT hr = device1->SetEventOnMultipleFenceCompletion(both, values, 2, D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL, | ||
| 601 | d->event); | ||
| 602 | device1->Release(); | ||
| 603 | return hr; | ||
| 604 | } | ||
| 605 | HRESULT make_resident_fence_and_queue(void* c) { | ||
| 606 | auto* d = static_cast<DiffContext*>(c); | ||
| 607 | ID3D12Pageable* objects[] = {d->fence, d->queue}; | ||
| 608 | return d->device->MakeResident(2, objects); | ||
| 609 | } | ||
| 610 | HRESULT evict_fence_and_queue(void* c) { | ||
| 611 | auto* d = static_cast<DiffContext*>(c); | ||
| 612 | ID3D12Pageable* objects[] = {d->fence, d->queue}; | ||
| 613 | const HRESULT hr = d->device->Evict(2, objects); | ||
| 614 | d->device->MakeResident(2, objects); | ||
| 615 | return hr; | ||
| 616 | } | ||
| 617 | HRESULT residency_priority(void* c) { | ||
| 618 | auto* d = static_cast<DiffContext*>(c); | ||
| 619 | ID3D12Device1* device1 = nullptr; | ||
| 620 | if (FAILED(d->device->QueryInterface(IID_PPV_ARGS(&device1)))) return kNoInterface; | ||
| 621 | ID3D12Pageable* objects[] = {d->fence, d->queue}; | ||
| 622 | const D3D12_RESIDENCY_PRIORITY priorities[] = {D3D12_RESIDENCY_PRIORITY_NORMAL, D3D12_RESIDENCY_PRIORITY_HIGH}; | ||
| 623 | const HRESULT hr = device1->SetResidencyPriority(2, objects, priorities); | ||
| 624 | device1->Release(); | ||
| 625 | return hr; | ||
| 626 | } | ||
| 627 | HRESULT enqueue_make_resident(void* c) { | ||
| 628 | auto* d = static_cast<DiffContext*>(c); | ||
| 629 | ID3D12Device3* device3 = nullptr; | ||
| 630 | if (FAILED(d->device->QueryInterface(IID_PPV_ARGS(&device3)))) return kNoInterface; | ||
| 631 | ID3D12Pageable* objects[] = {d->fence, d->queue}; | ||
| 632 | const UINT64 target = d->second->GetCompletedValue() + 1; | ||
| 633 | HRESULT hr = device3->EnqueueMakeResident(D3D12_RESIDENCY_FLAG_NONE, 2, objects, d->second, target); | ||
| 634 | device3->Release(); | ||
| 635 | if (SUCCEEDED(hr)) { // the fence must reach the value (the call signals it when the objects are resident) | ||
| 636 | if (FAILED(d->second->SetEventOnCompletion(target, d->event)) || | ||
| 637 | WaitForSingleObject(d->event, 5000) != WAIT_OBJECT_0) | ||
| 638 | return E_UNEXPECTED; | ||
| 639 | } | ||
| 640 | return hr; | ||
| 641 | } | ||
| 642 | HRESULT shared_handle_unshared_fence(void* c) { | ||
| 643 | auto* d = static_cast<DiffContext*>(c); | ||
| 644 | HANDLE handle = nullptr; | ||
| 645 | const HRESULT hr = d->device->CreateSharedHandle(d->fence, nullptr, GENERIC_ALL, nullptr, &handle); | ||
| 646 | if (handle) CloseHandle(handle); | ||
| 647 | return hr; | ||
| 648 | } | ||
| 649 | HRESULT shared_handle_queue(void* c) { | ||
| 650 | auto* d = static_cast<DiffContext*>(c); | ||
| 651 | HANDLE handle = nullptr; | ||
| 652 | const HRESULT hr = d->device->CreateSharedHandle(d->queue, nullptr, GENERIC_ALL, nullptr, &handle); | ||
| 653 | if (handle) CloseHandle(handle); | ||
| 654 | return hr; | ||
| 655 | } | ||
| 656 | // A descriptor heap from device slot 14 (a heap proxy behind the device proxy) is a pageable as well. | ||
| 657 | HRESULT residency_descriptor_heap(void* c, bool evict) { | ||
| 658 | auto* d = static_cast<DiffContext*>(c); | ||
| 659 | D3D12_DESCRIPTOR_HEAP_DESC desc{}; | ||
| 660 | desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; | ||
| 661 | desc.NumDescriptors = 4; | ||
| 662 | ID3D12DescriptorHeap* heap = nullptr; | ||
| 663 | if (FAILED(d->device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&heap)))) return E_FAIL; | ||
| 664 | ID3D12Pageable* objects[] = {heap, d->fence}; | ||
| 665 | HRESULT hr = evict ? d->device->Evict(2, objects) : d->device->MakeResident(2, objects); | ||
| 666 | if (evict && SUCCEEDED(hr)) hr = d->device->MakeResident(2, objects); | ||
| 667 | heap->Release(); | ||
| 668 | return hr; | ||
| 669 | } | ||
| 670 | HRESULT make_resident_descriptor_heap(void* c) { return residency_descriptor_heap(c, false); } | ||
| 671 | HRESULT evict_descriptor_heap(void* c) { return residency_descriptor_heap(c, true); } | ||
| 672 | |||
| 673 | #pragma warning(pop) | ||
| 674 | |||
| 675 | const DiffCase kDiffCases[] = { | ||
| 676 | {"CreateCommandQueue, no output", &create_queue_no_output}, | ||
| 677 | {"MakeResident(descriptor heap, fence)", &make_resident_descriptor_heap}, | ||
| 678 | {"Evict + MakeResident(descriptor heap, fence)", &evict_descriptor_heap}, | ||
| 679 | {"CreateCommandQueue, type 99", &create_queue_bad_type}, | ||
| 680 | {"CreateCommandQueue, priority 7", &create_queue_bad_priority}, | ||
| 681 | {"CreateCommandQueue as IID_ID3D12Fence", &create_queue_as_fence}, | ||
| 682 | {"CreateFence, no output", &create_fence_no_output}, | ||
| 683 | {"CreateFence, flags 0x80", &create_fence_bad_flags}, | ||
| 684 | {"CreateFence as IID_ID3D12CommandQueue", &create_fence_as_queue}, | ||
| 685 | {"CreateCommandQueue1, no output", &create_queue1_no_output}, | ||
| 686 | {"queue GetDevice as IID_ID3D12Fence", &queue_get_device_wrong_iid}, | ||
| 687 | {"queue GetDevice, no output", &queue_get_device_no_output}, | ||
| 688 | {"queue QueryInterface, no output", &queue_query_no_output}, | ||
| 689 | {"queue QueryInterface as IID_ID3D12Device", &queue_query_wrong_iid}, | ||
| 690 | {"queue GetPrivateData, missing key", &queue_private_data_missing}, | ||
| 691 | {"queue GetPrivateData, buffer too small", &queue_private_data_small}, | ||
| 692 | {"queue GetPrivateData, size query", &queue_private_data_size_query}, | ||
| 693 | {"queue Signal(null)", &queue_signal_null}, | ||
| 694 | {"queue Wait(null)", &queue_wait_null}, | ||
| 695 | {"queue GetTimestampFrequency(null)", &queue_frequency_null}, | ||
| 696 | {"queue GetClockCalibration(null, null)", &queue_calibration_null}, | ||
| 697 | {"fence GetPrivateData, missing key", &fence_private_data_missing}, | ||
| 698 | {"fence SetEventOnCompletion(reached, null)", &fence_event_reached_null_handle}, | ||
| 699 | {"SetEventOnMultipleFenceCompletion, count 0", &multiple_fences_count0}, | ||
| 700 | {"SetEventOnMultipleFenceCompletion, flags 7", &multiple_fences_bad_flags}, | ||
| 701 | {"SetEventOnMultipleFenceCompletion, null entry", &multiple_fences_null_entry}, | ||
| 702 | {"MakeResident(fence, queue)", &make_resident_fence_and_queue}, | ||
| 703 | {"Evict(fence, queue)", &evict_fence_and_queue}, | ||
| 704 | {"SetResidencyPriority(fence, queue)", &residency_priority}, | ||
| 705 | {"EnqueueMakeResident(fence, queue) -> second fence", &enqueue_make_resident}, | ||
| 706 | {"CreateSharedHandle(unshared fence)", &shared_handle_unshared_fence}, | ||
| 707 | {"CreateSharedHandle(queue)", &shared_handle_queue}, | ||
| 708 | }; | ||
| 709 | |||
| 710 | int make_context(const char* label, ID3D12Device* device, DiffContext& c, Ref<ID3D12CommandQueue>& queue, | ||
| 711 | Ref<ID3D12Fence>& fence, Ref<ID3D12Fence>& second) { | ||
| 712 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 713 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue.p)))); | ||
| 714 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); | ||
| 715 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&second.p)))); | ||
| 716 | c.device = device; | ||
| 717 | c.queue = queue.p; | ||
| 718 | c.fence = fence.p; | ||
| 719 | c.second = second.p; | ||
| 720 | return 0; | ||
| 721 | } | ||
| 722 | |||
| 723 | // The real device for a backend kind: "warp", "hw" (the first hardware adapter) or "vulkan" (on `vk`). One | ||
| 724 | // reference, or null. | ||
| 725 | ID3D12Device* create_backend_device(const char* kind, vktest::Device& vk) { | ||
| 726 | ID3D12Device* device = nullptr; | ||
| 727 | if (!std::strcmp(kind, "vulkan")) { | ||
| 728 | if (!vk.create()) return nullptr; | ||
| 729 | rt::d3d12::VulkanDeviceInfo info{}; | ||
| 730 | info.get_instance_proc = vk.instance_proc; | ||
| 731 | info.instance = vk.instance; | ||
| 732 | info.physical = vk.physical; | ||
| 733 | info.device = vk.device; | ||
| 734 | info.queue = vk.queue; | ||
| 735 | info.queue_family = vk.family; | ||
| 736 | return SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device))) ? device : nullptr; | ||
| 737 | } | ||
| 738 | Ref<IDXGIFactory4> factory; | ||
| 739 | if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p)))) return nullptr; | ||
| 740 | Ref<IDXGIAdapter1> adapter; | ||
| 741 | if (!std::strcmp(kind, "warp")) { | ||
| 742 | if (FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter.p)))) return nullptr; | ||
| 743 | } else { | ||
| 744 | for (UINT i = 0; !adapter.p; ++i) { | ||
| 745 | if (factory->EnumAdapters1(i, &adapter.p) == DXGI_ERROR_NOT_FOUND) return nullptr; | ||
| 746 | DXGI_ADAPTER_DESC1 desc{}; | ||
| 747 | if (FAILED(adapter->GetDesc1(&desc)) || (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) adapter.reset(); | ||
| 748 | } | ||
| 749 | } | ||
| 750 | return SUCCEEDED(D3D12CreateDevice(adapter.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device))) ? device : nullptr; | ||
| 751 | } | ||
| 752 | |||
| 753 | // Child mode (--diff <kind> <case> real|proxy): one case on its own device, in its own process, because the PC | ||
| 754 | // runtime's device is one object per adapter and process, and some error cases make it remove that device (it does | ||
| 755 | // for SetResidencyPriority on a fence) or fault inside it. Prints "DIFF <ran> <hr> <removed> <validation errors>". | ||
| 756 | int run_diff_child(const char* kind, int index, bool through_proxy) { | ||
| 757 | if (index < 0 || index >= static_cast<int>(std::size(kDiffCases))) return 2; | ||
| 758 | vktest::Device vk; | ||
| 759 | ID3D12Device* real = create_backend_device(kind, vk); | ||
| 760 | if (!real) return 3; | ||
| 761 | Ref<ID3D12Device> device; | ||
| 762 | if (through_proxy) { | ||
| 763 | if (FAILED(xs::create_device_proxy(real, __uuidof(ID3D12Device), reinterpret_cast<void**>(&device.p)))) { | ||
| 764 | real->Release(); | ||
| 765 | return 3; | ||
| 766 | } | ||
| 767 | if (!std::strcmp(kind, "vulkan")) rt::d3d12::set_device_outer(real, device.p); | ||
| 768 | } else { | ||
| 769 | device.p = real; | ||
| 770 | } | ||
| 771 | const char* label = kind; | ||
| 772 | Event event; | ||
| 773 | DiffContext context{}; | ||
| 774 | Ref<ID3D12CommandQueue> queue; | ||
| 775 | Ref<ID3D12Fence> fence, second; | ||
| 776 | if (make_context(label, device.p, context, queue, fence, second)) return 3; | ||
| 777 | EXPECT(xs::is_queue_proxy(context.queue) == through_proxy && xs::is_fence_proxy(context.fence) == through_proxy); | ||
| 778 | context.event = event.h; | ||
| 779 | HRESULT result = S_OK; | ||
| 780 | const bool ran = run_guarded(kDiffCases[index].run, &context, &result); | ||
| 781 | const HRESULT removed = ran ? device->GetDeviceRemovedReason() : S_OK; | ||
| 782 | std::printf("DIFF %d 0x%08lx 0x%08lx %u\n", ran ? 1 : 0, static_cast<unsigned long>(result), | ||
| 783 | static_cast<unsigned long>(removed), vk.validation_errors()); | ||
| 784 | std::fflush(stdout); | ||
| 785 | // A faulted case may have left the runtime in any state: leave without releasing anything. | ||
| 786 | if (!ran) TerminateProcess(GetCurrentProcess(), 0); | ||
| 787 | return 0; | ||
| 788 | } | ||
| 789 | |||
| 790 | struct ChildResult { | ||
| 791 | bool ok = false; // the child ran and reported | ||
| 792 | int ran = 0; | ||
| 793 | unsigned long hr = 0, removed = 0, validation = 0; | ||
| 794 | }; | ||
| 795 | |||
| 796 | // How a child of this executable ended: it exited by itself, it outlived the limit and was ended, or it could not be | ||
| 797 | // started or watched (the reason is printed on stderr). | ||
| 798 | enum class ChildEnd { exited, timed_out, failed }; | ||
| 799 | |||
| 800 | constexpr DWORD kChildLimitMs = 60000; // one differential case, including device creation | ||
| 801 | constexpr std::size_t kChildOutputKeep = 1u << 20; // the most of a child's stdout kept: its last 1 MiB | ||
| 802 | |||
| 803 | // Failures the runner's self-test injects: the pipe, or the thread that drains it, cannot be made. | ||
| 804 | enum class RunnerFault { none, pipe, thread }; | ||
| 805 | |||
| 806 | struct ChildRun { | ||
| 807 | ChildEnd end = ChildEnd::failed; | ||
| 808 | DWORD exit_code = ~0ul; // the child's exit code (1 when the runner ended it), ~0 when it never ran | ||
| 809 | unsigned long long printed = 0; // bytes it printed on stdout in all | ||
| 810 | std::string tail; // the last kChildOutputKeep of them, at most | ||
| 811 | }; | ||
| 812 | |||
| 813 | void report_runner_error(const char* what) { | ||
| 814 | std::fprintf(stderr, "child runner: %s failed (error %lu)\n", what, GetLastError()); | ||
| 815 | } | ||
| 816 | |||
| 817 | // Runs `exe` with `arguments` and collects its stdout. A thread drains the pipe while the child runs, so a child that | ||
| 818 | // prints more than the pipe buffers keeps running instead of blocking in WriteFile until the limit ends it and it is | ||
| 819 | // taken for a hung one. Only the last kChildOutputKeep bytes are kept. A child still running after `limit_ms` is | ||
| 820 | // ended with exit code 1, and what it printed until then is still returned. | ||
| 821 | ChildRun run_process(const wchar_t* exe, const wchar_t* arguments, DWORD limit_ms, | ||
| 822 | RunnerFault fault = RunnerFault::none) { | ||
| 823 | ChildRun run; | ||
| 824 | std::wstring command = L"\"" + std::wstring(exe) + L"\" " + arguments; | ||
| 825 | SECURITY_ATTRIBUTES inherit{sizeof(inherit), nullptr, TRUE}; | ||
| 826 | HANDLE read_end = nullptr, write_end = nullptr; | ||
| 827 | if (fault == RunnerFault::pipe) SetLastError(ERROR_NOT_ENOUGH_MEMORY); | ||
| 828 | if (fault == RunnerFault::pipe || !CreatePipe(&read_end, &write_end, &inherit, 0)) { | ||
| 829 | report_runner_error("CreatePipe"); | ||
| 830 | return run; | ||
| 831 | } | ||
| 832 | if (!SetHandleInformation(read_end, HANDLE_FLAG_INHERIT, 0)) { | ||
| 833 | report_runner_error("SetHandleInformation"); | ||
| 834 | CloseHandle(read_end); | ||
| 835 | CloseHandle(write_end); | ||
| 836 | return run; | ||
| 837 | } | ||
| 838 | STARTUPINFOW startup{sizeof(startup)}; | ||
| 839 | startup.dwFlags = STARTF_USESTDHANDLES; | ||
| 840 | startup.hStdOutput = write_end; | ||
| 841 | startup.hStdError = GetStdHandle(STD_ERROR_HANDLE); | ||
| 842 | startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | ||
| 843 | PROCESS_INFORMATION process{}; | ||
| 844 | const BOOL started = | ||
| 845 | CreateProcessW(exe, command.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &startup, &process); | ||
| 846 | const DWORD start_error = GetLastError(); | ||
| 847 | CloseHandle(write_end); // the child holds the only write end now: the reader sees the pipe end when it is gone | ||
| 848 | if (!started) { | ||
| 849 | std::fprintf(stderr, "child runner: CreateProcessW \"%ls\" failed (error %lu)\n", exe, start_error); | ||
| 850 | CloseHandle(read_end); | ||
| 851 | return run; | ||
| 852 | } | ||
| 853 | CloseHandle(process.hThread); | ||
| 854 | const auto end_child = [&] { | ||
| 855 | if (!TerminateProcess(process.hProcess, 1) && WaitForSingleObject(process.hProcess, 0) != WAIT_OBJECT_0) | ||
| 856 | report_runner_error("TerminateProcess"); | ||
| 857 | if (WaitForSingleObject(process.hProcess, 5000) != WAIT_OBJECT_0) | ||
| 858 | std::fprintf(stderr, "child runner: the ended child is still there after 5 s\n"); | ||
| 859 | }; | ||
| 860 | |||
| 861 | std::atomic<bool> stop{false}; | ||
| 862 | std::thread reader; | ||
| 863 | try { | ||
| 864 | if (fault == RunnerFault::thread) | ||
| 865 | throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again)); | ||
| 866 | reader = std::thread([read_end, &run, &stop] { | ||
| 867 | char buffer[4096]; | ||
| 868 | DWORD got = 0; | ||
| 869 | while (!stop.load() && ReadFile(read_end, buffer, sizeof(buffer), &got, nullptr) && got) { | ||
| 870 | run.printed += got; | ||
| 871 | run.tail.append(buffer, got); | ||
| 872 | if (run.tail.size() > 2 * kChildOutputKeep) run.tail.erase(0, run.tail.size() - kChildOutputKeep); | ||
| 873 | } | ||
| 874 | }); | ||
| 875 | } catch (const std::system_error& e) { | ||
| 876 | std::fprintf(stderr, "child runner: no thread to read the child's output (%s); the child is ended\n", e.what()); | ||
| 877 | end_child(); | ||
| 878 | if (!GetExitCodeProcess(process.hProcess, &run.exit_code)) run.exit_code = ~0ul; | ||
| 879 | CloseHandle(process.hProcess); | ||
| 880 | CloseHandle(read_end); | ||
| 881 | return run; | ||
| 882 | } | ||
| 883 | |||
| 884 | const DWORD waited = WaitForSingleObject(process.hProcess, limit_ms); | ||
| 885 | if (waited == WAIT_OBJECT_0) { | ||
| 886 | run.end = ChildEnd::exited; | ||
| 887 | } else { | ||
| 888 | if (waited == WAIT_FAILED) report_runner_error("WaitForSingleObject"); | ||
| 889 | end_child(); | ||
| 890 | run.end = waited == WAIT_TIMEOUT ? ChildEnd::timed_out : ChildEnd::failed; | ||
| 891 | } | ||
| 892 | // The child and its write end are gone, so the reader stops at the end of the pipe. Should something else still | ||
| 893 | // hold the write end (a process the child started), its blocked read is cancelled rather than waited on. | ||
| 894 | const HANDLE thread = reader.native_handle(); | ||
| 895 | if (WaitForSingleObject(thread, 5000) != WAIT_OBJECT_0) { | ||
| 896 | std::fprintf(stderr, "child runner: the pipe is still open after the child ended; the read is cancelled\n"); | ||
| 897 | stop.store(true); | ||
| 898 | DWORD waited_reader = WAIT_TIMEOUT; | ||
| 899 | for (int tries = 0; tries < 50 && waited_reader == WAIT_TIMEOUT; ++tries) { // 5 s at most | ||
| 900 | CancelSynchronousIo(thread); | ||
| 901 | waited_reader = WaitForSingleObject(thread, 100); | ||
| 902 | } | ||
| 903 | if (waited_reader != WAIT_OBJECT_0) { | ||
| 904 | // The reader can neither be joined nor left behind (it writes into `run`): fail now rather than hang. | ||
| 905 | std::fprintf(stderr, "child runner: the reader thread cannot be stopped (wait 0x%lx, error %lu)\n", | ||
| 906 | waited_reader, GetLastError()); | ||
| 907 | std::fflush(stderr); | ||
| 908 | TerminateProcess(GetCurrentProcess(), 1); | ||
| 909 | } | ||
| 910 | } | ||
| 911 | reader.join(); | ||
| 912 | if (run.tail.size() > kChildOutputKeep) run.tail.erase(0, run.tail.size() - kChildOutputKeep); | ||
| 913 | if (!GetExitCodeProcess(process.hProcess, &run.exit_code)) { | ||
| 914 | report_runner_error("GetExitCodeProcess"); | ||
| 915 | run.exit_code = ~0ul; | ||
| 916 | } | ||
| 917 | CloseHandle(process.hProcess); | ||
| 918 | CloseHandle(read_end); | ||
| 919 | return run; | ||
| 920 | } | ||
| 921 | |||
| 922 | // This executable's own path, for its children; empty (and reported) when it cannot be had in full. | ||
| 923 | std::wstring self_path() { | ||
| 924 | wchar_t exe[MAX_PATH] = {}; | ||
| 925 | const DWORD length = GetModuleFileNameW(nullptr, exe, MAX_PATH); | ||
| 926 | if (!length || length >= MAX_PATH) { | ||
| 927 | report_runner_error("GetModuleFileNameW"); | ||
| 928 | return {}; | ||
| 929 | } | ||
| 930 | return exe; | ||
| 931 | } | ||
| 932 | |||
| 933 | ChildRun run_self(const wchar_t* arguments, DWORD limit_ms, RunnerFault fault = RunnerFault::none) { | ||
| 934 | const std::wstring exe = self_path(); | ||
| 935 | return exe.empty() ? ChildRun{} : run_process(exe.c_str(), arguments, limit_ms, fault); | ||
| 936 | } | ||
| 937 | |||
| 938 | // The last "DIFF ..." line a child printed. | ||
| 939 | bool parse_diff_report(const std::string& output, ChildResult& r) { | ||
| 940 | const std::size_t at = output.rfind("DIFF "); | ||
| 941 | return at != std::string::npos && | ||
| 942 | sscanf_s(output.c_str() + at, "DIFF %d %lx %lx %lu", &r.ran, &r.hr, &r.removed, &r.validation) == 4; | ||
| 943 | } | ||
| 944 | |||
| 945 | ChildResult run_child(const char* kind, int index, bool through_proxy) { | ||
| 946 | ChildResult r; | ||
| 947 | wchar_t arguments[64]; | ||
| 948 | std::swprintf(arguments, std::size(arguments), L"--diff %hs %d %hs", kind, index, | ||
| 949 | through_proxy ? "proxy" : "real"); | ||
| 950 | const ChildRun run = run_self(arguments, kChildLimitMs); | ||
| 951 | const char* side = through_proxy ? "proxy" : "real"; | ||
| 952 | if (run.end == ChildEnd::timed_out) | ||
| 953 | std::fprintf(stderr, "%s case %d (%s): the child did not finish within %lu s and was ended\n", kind, index, | ||
| 954 | side, kChildLimitMs / 1000); | ||
| 955 | if (run.end != ChildEnd::exited) return r; | ||
| 956 | r.ok = parse_diff_report(run.tail, r); | ||
| 957 | if (!r.ok) | ||
| 958 | std::fprintf(stderr, "%s case %d (%s): the child exited with 0x%08lx without a report\n", kind, index, side, | ||
| 959 | run.exit_code); | ||
| 960 | return r; | ||
| 961 | } | ||
| 962 | |||
| 963 | constexpr DWORD kCrashCode = 0xE04C4E44; // the self-test child's own exception code, raised and not handled | ||
| 964 | |||
| 965 | // Self-test child modes of the runner, no device: print `bytes` bytes, then exit with a report (--child-flood), | ||
| 966 | // never return (--child-hang), or end on an unhandled exception (--child-crash). | ||
| 967 | int run_runner_child(const char* mode, unsigned long bytes) { | ||
| 968 | const std::string chunk(4096, 'x'); | ||
| 969 | for (unsigned long left = bytes; left;) { | ||
| 970 | const unsigned long n = left < chunk.size() ? left : static_cast<unsigned long>(chunk.size()); | ||
| 971 | if (std::fwrite(chunk.data(), 1, n, stdout) != n) return 3; | ||
| 972 | left -= n; | ||
| 973 | } | ||
| 974 | if (std::strcmp(mode, "--child-hang") == 0) Sleep(INFINITE); | ||
| 975 | if (std::strcmp(mode, "--child-crash") == 0) { | ||
| 976 | SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); // no error dialog to wait on | ||
| 977 | RaiseException(kCrashCode, EXCEPTION_NONCONTINUABLE, 0, nullptr); | ||
| 978 | } | ||
| 979 | std::printf("\nDIFF 1 0x00000000 0x00000000 0\n"); | ||
| 980 | return 0; | ||
| 981 | } | ||
| 982 | |||
| 983 | // The runner itself, on every machine (it needs no device): | ||
| 984 | // - a child that exits normally is read and its report found; | ||
| 985 | // - one printing 8 MiB (128 times a 64 KiB pipe buffer) finishes, all of it is counted, and only the tail is kept; | ||
| 986 | // - hung children, silent or after printing 256 KiB, are ended at the limit and their output still read; | ||
| 987 | // - a child that crashes, or cannot be started, is reported, not taken for a report; | ||
| 988 | // - an injected pipe or reader-thread failure is reported, and the child of the latter is ended, not waited on; | ||
| 989 | // - no handle is left behind by any of it. | ||
| 990 | int check_child_runner() { | ||
| 991 | const char* label = "child runner"; | ||
| 992 | ChildResult report; | ||
| 993 | ChildRun run = run_self(L"--child-flood 0", 30000); | ||
| 994 | EXPECT(run.end == ChildEnd::exited && run.exit_code == 0 && parse_diff_report(run.tail, report)); | ||
| 995 | EXPECT(report.ran == 1 && report.hr == 0 && report.removed == 0 && report.validation == 0); | ||
| 996 | // The first start and the first failed start make Windows load what it keeps for them (a failed CreateProcessW | ||
| 997 | // leaves 42 handles the first time on Windows 11 26200, none on every later one): both run once before the handle | ||
| 998 | // count is taken, and every case after it must leave the count as it was. | ||
| 999 | std::fprintf(stderr, "child runner: the next four errors are expected\n"); | ||
| 1000 | const std::wstring missing = self_path() + L".missing.exe"; | ||
| 1001 | run = run_process(missing.c_str(), L"", 5000); | ||
| 1002 | EXPECT(run.end == ChildEnd::failed && run.exit_code == ~0ul && run.printed == 0); | ||
| 1003 | DWORD handles_before = 0, handles_after = 0; | ||
| 1004 | EXPECT(GetProcessHandleCount(GetCurrentProcess(), &handles_before)); | ||
| 1005 | |||
| 1006 | constexpr unsigned long long kFlood = 8ull << 20, kBeforeHang = 256ull << 10; | ||
| 1007 | run = run_self(L"--child-flood 8388608", 30000); | ||
| 1008 | EXPECT(run.end == ChildEnd::exited && run.exit_code == 0); | ||
| 1009 | EXPECT(run.printed > kFlood && run.tail.size() <= kChildOutputKeep && parse_diff_report(run.tail, report)); | ||
| 1010 | |||
| 1011 | ULONGLONG start = GetTickCount64(); | ||
| 1012 | run = run_self(L"--child-hang 262144", 2000); | ||
| 1013 | ULONGLONG took = GetTickCount64() - start; | ||
| 1014 | EXPECT(run.end == ChildEnd::timed_out && run.exit_code == 1 && run.printed == kBeforeHang); | ||
| 1015 | EXPECT(took >= 1900 && took < 15000); | ||
| 1016 | |||
| 1017 | start = GetTickCount64(); | ||
| 1018 | run = run_self(L"--child-hang 0", 1000); | ||
| 1019 | took = GetTickCount64() - start; | ||
| 1020 | EXPECT(run.end == ChildEnd::timed_out && run.exit_code == 1 && run.printed == 0 && run.tail.empty()); | ||
| 1021 | EXPECT(took >= 900 && took < 15000); | ||
| 1022 | |||
| 1023 | run = run_self(L"--child-crash 100", 30000); | ||
| 1024 | EXPECT(run.end == ChildEnd::exited && run.exit_code != 0 && run.printed == 100); | ||
| 1025 | EXPECT(!parse_diff_report(run.tail, report)); | ||
| 1026 | const DWORD crash_code = run.exit_code; | ||
| 1027 | |||
| 1028 | run = run_process(missing.c_str(), L"", 5000); | ||
| 1029 | EXPECT(run.end == ChildEnd::failed && run.exit_code == ~0ul && run.printed == 0); | ||
| 1030 | run = run_self(L"--child-flood 16", 5000, RunnerFault::pipe); | ||
| 1031 | EXPECT(run.end == ChildEnd::failed && run.exit_code == ~0ul); | ||
| 1032 | start = GetTickCount64(); | ||
| 1033 | run = run_self(L"--child-hang 0", kChildLimitMs, RunnerFault::thread); | ||
| 1034 | took = GetTickCount64() - start; | ||
| 1035 | EXPECT(run.end == ChildEnd::failed && run.exit_code == 1 && took < 15000); | ||
| 1036 | |||
| 1037 | EXPECT(GetProcessHandleCount(GetCurrentProcess(), &handles_after)); | ||
| 1038 | if (handles_after != handles_before) | ||
| 1039 | std::fprintf(stderr, "%s: %lu handles before, %lu after\n", label, handles_before, handles_after); | ||
| 1040 | EXPECT(handles_after == handles_before); | ||
| 1041 | std::printf("%s: normal exit, 8 MiB of output (tail kept), hung children (silent and after 256 KiB), a crash " | ||
| 1042 | "(0x%08lx), a missing executable and injected pipe and thread failures behave, no handle left\n", | ||
| 1043 | label, crash_code); | ||
| 1044 | return 0; | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | // Each case, in a child process for the real object and in another for the proxy: the same HRESULT, the device | ||
| 1048 | // removed (or not) alike, and no validation errors. A case the real runtime faults on is reported, not compared. | ||
| 1049 | int check_differential(const Backend& b) { | ||
| 1050 | const char* label = b.label; | ||
| 1051 | int compared = 0, faulting = 0; | ||
| 1052 | for (int i = 0; i < static_cast<int>(std::size(kDiffCases)); ++i) { | ||
| 1053 | const char* name = kDiffCases[i].name; | ||
| 1054 | const ChildResult want = run_child(b.kind, i, false); | ||
| 1055 | if (!want.ok) { | ||
| 1056 | std::fprintf(stderr, "%s: %s: the real-object child did not report\n", label, name); | ||
| 1057 | return 1; | ||
| 1058 | } | ||
| 1059 | if (!want.ran) { | ||
| 1060 | std::printf("%s: %s: the real object faults, not compared\n", label, name); | ||
| 1061 | ++faulting; | ||
| 1062 | continue; | ||
| 1063 | } | ||
| 1064 | const ChildResult got = run_child(b.kind, i, true); | ||
| 1065 | if (!got.ok || !got.ran || got.hr != want.hr || got.removed != want.removed || got.validation || | ||
| 1066 | want.validation) { | ||
| 1067 | std::fprintf(stderr, | ||
| 1068 | "%s: %s: proxy %s 0x%08lx (removed 0x%08lx, %lu validation errors), real 0x%08lx (removed " | ||
| 1069 | "0x%08lx, %lu validation errors)\n", | ||
| 1070 | label, name, !got.ok ? "did not report" : got.ran ? "returned" : "faulted", got.hr, | ||
| 1071 | got.removed, got.validation, want.hr, want.removed, want.validation); | ||
| 1072 | return 1; | ||
| 1073 | } | ||
| 1074 | ++compared; | ||
| 1075 | } | ||
| 1076 | std::printf("%s: %d error and edge cases give the real object's HRESULT and removal state through the proxy, " | ||
| 1077 | "each in its own process (%d not comparable)\n", label, compared, faulting); | ||
| 1078 | return 0; | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | // ---- 4. Identity, references and release orders ---- | ||
| 1082 | |||
| 1083 | int check_identity(const Backend& b) { | ||
| 1084 | const char* label = b.label; | ||
| 1085 | ID3D12Device* device = b.device; | ||
| 1086 | const unsigned long device_refs = xs::device_proxy_ref_count(device); | ||
| 1087 | { | ||
| 1088 | Ref<ID3D12Fence> fence; | ||
| 1089 | const HRESULT made = device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)); | ||
| 1090 | if (FAILED(made)) | ||
| 1091 | std::fprintf(stderr, "%s: CreateFence 0x%08lx, removed reason 0x%08lx\n", label, | ||
| 1092 | static_cast<unsigned long>(made), | ||
| 1093 | static_cast<unsigned long>(device->GetDeviceRemovedReason())); | ||
| 1094 | EXPECT(SUCCEEDED(made)); | ||
| 1095 | const bool has1 = xs::fence_proxy_slot_count(fence.p) == 12; | ||
| 1096 | // Every modelled interface is the same pointer, and QueryInterface is reflexive, symmetric, transitive. | ||
| 1097 | Ref<IUnknown> unknown; | ||
| 1098 | Ref<ID3D12Object> object; | ||
| 1099 | Ref<ID3D12DeviceChild> child; | ||
| 1100 | Ref<ID3D12Pageable> pageable; | ||
| 1101 | Ref<ID3D12Fence> again; | ||
| 1102 | EXPECT(SUCCEEDED(fence->QueryInterface(IID_PPV_ARGS(&unknown.p)))); | ||
| 1103 | EXPECT(SUCCEEDED(unknown->QueryInterface(IID_PPV_ARGS(&object.p)))); | ||
| 1104 | EXPECT(SUCCEEDED(object->QueryInterface(IID_PPV_ARGS(&child.p)))); | ||
| 1105 | EXPECT(SUCCEEDED(child->QueryInterface(IID_PPV_ARGS(&pageable.p)))); | ||
| 1106 | EXPECT(SUCCEEDED(pageable->QueryInterface(IID_PPV_ARGS(&again.p)))); | ||
| 1107 | void* const p = fence.p; | ||
| 1108 | EXPECT(unknown.p == p && object.p == p && child.p == p && pageable.p == p && again.p == p); | ||
| 1109 | Ref<ID3D12Fence1> fence1; | ||
| 1110 | const HRESULT hr1 = again->QueryInterface(IID_PPV_ARGS(&fence1.p)); | ||
| 1111 | EXPECT(has1 ? SUCCEEDED(hr1) && static_cast<void*>(fence1.p) == p : hr1 == E_NOINTERFACE); | ||
| 1112 | EXPECT(xs::command_object_proxy_ref_count(p) == (has1 ? 7u : 6u)); | ||
| 1113 | // Interfaces the proxy does not model are refused, also those the real object has. | ||
| 1114 | Ref<IUnknown> other; | ||
| 1115 | EXPECT(fence->QueryInterface(__uuidof(ID3D12CommandQueue), reinterpret_cast<void**>(&other.p)) == | ||
| 1116 | E_NOINTERFACE && !other.p); | ||
| 1117 | EXPECT(fence->QueryInterface(__uuidof(ID3D12Device), reinterpret_cast<void**>(&other.p)) == E_NOINTERFACE); | ||
| 1118 | // AddRef through one interface, Release through another. | ||
| 1119 | EXPECT(child->AddRef() == (has1 ? 8u : 7u)); | ||
| 1120 | EXPECT(pageable->Release() == (has1 ? 7u : 6u)); | ||
| 1121 | // Release in an order unlike the acquisition. | ||
| 1122 | child.reset(); | ||
| 1123 | unknown.reset(); | ||
| 1124 | fence1.reset(); | ||
| 1125 | object.reset(); | ||
| 1126 | again.reset(); | ||
| 1127 | pageable.reset(); | ||
| 1128 | EXPECT(xs::command_object_proxy_ref_count(p) == 1); | ||
| 1129 | } | ||
| 1130 | { | ||
| 1131 | // The queue the same way, and its device: GetDevice and QueryInterface of the device give one object. | ||
| 1132 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 1133 | Ref<IUnknown> queue; | ||
| 1134 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&desc, __uuidof(IUnknown), reinterpret_cast<void**>(&queue.p)))); | ||
| 1135 | EXPECT(xs::is_queue_proxy(queue.p)); | ||
| 1136 | Ref<ID3D12CommandQueue> typed; | ||
| 1137 | EXPECT(SUCCEEDED(queue->QueryInterface(IID_PPV_ARGS(&typed.p))) && static_cast<void*>(typed.p) == queue.p); | ||
| 1138 | Ref<ID3D12Device> owner; | ||
| 1139 | Ref<ID3D12Device1> owner1; | ||
| 1140 | EXPECT(SUCCEEDED(typed->GetDevice(IID_PPV_ARGS(&owner.p)))); | ||
| 1141 | EXPECT(SUCCEEDED(typed->GetDevice(IID_PPV_ARGS(&owner1.p)))); | ||
| 1142 | EXPECT(owner.p == device && static_cast<void*>(owner1.p) == static_cast<void*>(device)); | ||
| 1143 | Ref<ID3D12Fence> not_a_device; | ||
| 1144 | EXPECT(typed->GetDevice(IID_PPV_ARGS(¬_a_device.p)) == E_NOINTERFACE && !not_a_device.p); | ||
| 1145 | |||
| 1146 | // A null IID: the PC runtime dereferences it (no answer to compare with); the proxies refuse it with | ||
| 1147 | // E_POINTER and leave the output null, on every slot that takes one. | ||
| 1148 | const D3D12_COMMAND_QUEUE_DESC desc0{}; | ||
| 1149 | void* out = reinterpret_cast<void*>(1); | ||
| 1150 | EXPECT(as_hr(raw_call(device, 8, reinterpret_cast<std::uint64_t>(&desc0), 0, | ||
| 1151 | reinterpret_cast<std::uint64_t>(&out))) == E_POINTER); | ||
| 1152 | EXPECT(as_hr(raw_call(device, 36, 0, 0, 0, reinterpret_cast<std::uint64_t>(&out))) == E_POINTER); | ||
| 1153 | EXPECT(as_hr(raw_call(device, 32, 0, 0, reinterpret_cast<std::uint64_t>(&out))) == E_POINTER); | ||
| 1154 | if (xs::device_proxy_slot_count(device) > 75) | ||
| 1155 | EXPECT(as_hr(raw_call(device, 75, reinterpret_cast<std::uint64_t>(&desc0), 0, 0, | ||
| 1156 | reinterpret_cast<std::uint64_t>(&out))) == E_POINTER); | ||
| 1157 | out = reinterpret_cast<void*>(1); | ||
| 1158 | EXPECT(as_hr(raw_call(typed.p, 0, 0, reinterpret_cast<std::uint64_t>(&out))) == E_POINTER && !out); | ||
| 1159 | EXPECT(as_hr(raw_call(typed.p, 7, 0, reinterpret_cast<std::uint64_t>(&out))) == E_POINTER); | ||
| 1160 | EXPECT(xs::command_object_proxy_ref_count(typed.p) == 2); // queue + typed: nothing leaked or added | ||
| 1161 | } | ||
| 1162 | EXPECT(xs::device_proxy_ref_count(device) == device_refs); | ||
| 1163 | std::printf("%s: QueryInterface identity, references across interfaces and release orders hold\n", label); | ||
| 1164 | return 0; | ||
| 1165 | } | ||
| 1166 | |||
| 1167 | // ---- 5. Shared fences ---- | ||
| 1168 | |||
| 1169 | int check_shared(const Backend& b) { | ||
| 1170 | const char* label = b.label; | ||
| 1171 | Ref<ID3D12Fence> fence; | ||
| 1172 | const HRESULT created = b.device->CreateFence(0, D3D12_FENCE_FLAG_SHARED, IID_PPV_ARGS(&fence.p)); | ||
| 1173 | { | ||
| 1174 | Ref<ID3D12Fence> probe; | ||
| 1175 | EXPECT(created == b.real->CreateFence(0, D3D12_FENCE_FLAG_SHARED, IID_PPV_ARGS(&probe.p))); | ||
| 1176 | } | ||
| 1177 | if (FAILED(created)) { | ||
| 1178 | std::printf("%s: no shared fences (0x%08lx, as on the real device), not checked\n", label, | ||
| 1179 | static_cast<unsigned long>(created)); | ||
| 1180 | return 0; | ||
| 1181 | } | ||
| 1182 | HANDLE handle = nullptr; | ||
| 1183 | g_shared_created = true; | ||
| 1184 | const HRESULT shared = b.device->CreateSharedHandle(fence.p, nullptr, GENERIC_ALL, nullptr, &handle); | ||
| 1185 | Ref<ID3D12Fence> raw; | ||
| 1186 | HANDLE raw_handle = nullptr; | ||
| 1187 | EXPECT(SUCCEEDED(b.real->CreateFence(0, D3D12_FENCE_FLAG_SHARED, IID_PPV_ARGS(&raw.p)))); | ||
| 1188 | const HRESULT raw_shared = b.real->CreateSharedHandle(raw.p, nullptr, GENERIC_ALL, nullptr, &raw_handle); | ||
| 1189 | if (raw_handle) CloseHandle(raw_handle); | ||
| 1190 | EXPECT(shared == raw_shared); // the proxy unwrapped the fence for the real device | ||
| 1191 | if (FAILED(shared)) { | ||
| 1192 | std::printf("%s: shared fences not supported (0x%08lx), OpenSharedHandle not checked\n", label, | ||
| 1193 | static_cast<unsigned long>(shared)); | ||
| 1194 | return 0; | ||
| 1195 | } | ||
| 1196 | Ref<ID3D12Fence> opened; | ||
| 1197 | Ref<IUnknown> opened_unknown; | ||
| 1198 | EXPECT(SUCCEEDED(b.device->OpenSharedHandle(handle, IID_PPV_ARGS(&opened.p)))); | ||
| 1199 | EXPECT(SUCCEEDED(b.device->OpenSharedHandle(handle, __uuidof(IUnknown), | ||
| 1200 | reinterpret_cast<void**>(&opened_unknown.p)))); | ||
| 1201 | CloseHandle(handle); | ||
| 1202 | EXPECT(xs::is_fence_proxy(opened.p) && xs::is_fence_proxy(opened_unknown.p)); // also when asked as IUnknown | ||
| 1203 | EXPECT(xs::command_object_proxy_ref_count(opened.p) == 1); | ||
| 1204 | EXPECT(xs::command_object_proxy_ref_count(opened_unknown.p) == 1); | ||
| 1205 | EXPECT(xs::device_slot_hits(32) >= 2); | ||
| 1206 | EXPECT(SUCCEEDED(fence->Signal(4))); | ||
| 1207 | EXPECT(opened->GetCompletedValue() == 4); // one fence, seen through both proxies | ||
| 1208 | g_shared_checked = true; | ||
| 1209 | |||
| 1210 | // Not a fence: a shared buffer goes through CreateSharedHandle (31) as it is and comes back from | ||
| 1211 | // OpenSharedHandle (32) as the real resource, not a proxy. | ||
| 1212 | D3D12_HEAP_PROPERTIES heap{}; | ||
| 1213 | heap.Type = D3D12_HEAP_TYPE_DEFAULT; | ||
| 1214 | D3D12_RESOURCE_DESC buffer{}; | ||
| 1215 | buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; | ||
| 1216 | buffer.Width = 65536; | ||
| 1217 | buffer.Height = buffer.DepthOrArraySize = buffer.MipLevels = 1; | ||
| 1218 | buffer.SampleDesc.Count = 1; | ||
| 1219 | buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; | ||
| 1220 | Ref<ID3D12Resource> resource; | ||
| 1221 | if (SUCCEEDED(b.device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_SHARED, &buffer, | ||
| 1222 | D3D12_RESOURCE_STATE_COMMON, nullptr, | ||
| 1223 | IID_PPV_ARGS(&resource.p)))) { | ||
| 1224 | HANDLE resource_handle = nullptr; | ||
| 1225 | EXPECT(SUCCEEDED(b.device->CreateSharedHandle(resource.p, nullptr, GENERIC_ALL, nullptr, &resource_handle))); | ||
| 1226 | Ref<ID3D12Resource> reopened; | ||
| 1227 | const HRESULT reopen = b.device->OpenSharedHandle(resource_handle, IID_PPV_ARGS(&reopened.p)); | ||
| 1228 | CloseHandle(resource_handle); | ||
| 1229 | EXPECT(SUCCEEDED(reopen) && reopened.p && !xs::is_fence_proxy(reopened.p) && !xs::is_queue_proxy(reopened.p)); | ||
| 1230 | EXPECT(reopened->GetDesc().Width == buffer.Width); | ||
| 1231 | // An invalid (null) handle: the real runtime's own answer, and no object. Not the closed handle above: its | ||
| 1232 | // value may already name another object of this process. | ||
| 1233 | Ref<ID3D12Fence> none, none_real; | ||
| 1234 | none.p = reinterpret_cast<ID3D12Fence*>(1); | ||
| 1235 | #pragma warning(suppress : 6387) // the null handle is the invalid input under test | ||
| 1236 | const HRESULT invalid = b.device->OpenSharedHandle(nullptr, IID_PPV_ARGS(&none.p)); | ||
| 1237 | const bool cleared = !none.p; | ||
| 1238 | none.p = nullptr; | ||
| 1239 | #pragma warning(suppress : 6387) // the same input on the real device | ||
| 1240 | const HRESULT invalid_real = b.real->OpenSharedHandle(nullptr, IID_PPV_ARGS(&none_real.p)); | ||
| 1241 | EXPECT(FAILED(invalid) && cleared && invalid == invalid_real && !none_real.p); | ||
| 1242 | } else { | ||
| 1243 | std::printf("%s: no shared committed buffer, the non-fence OpenSharedHandle not checked\n", label); | ||
| 1244 | } | ||
| 1245 | std::printf("%s: CreateSharedHandle unwraps the fence and OpenSharedHandle wraps it; a shared buffer and an " | ||
| 1246 | "invalid handle pass through unchanged\n", label); | ||
| 1247 | return 0; | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | // ---- 8. A console-built client, end to end ---- | ||
| 1251 | // | ||
| 1252 | // The way a library compiled against the console device calls it: raw vtable slots, the measured Xbox shapes where | ||
| 1253 | // they differ (device slot 14 hands out a heap proxy whose slot 9 returns the handle in RAX; slot 76 creates a | ||
| 1254 | // resource from an Xbox description with the layout word 0x1), and the queue and fence slots for its GPU work. The | ||
| 1255 | // shipped package has no queue or fence client (docs/d3d12_command_objects.md §3.5), so this is the synthetic stand-in | ||
| 1256 | // for what bvh_builder_test and fsr_dispatch_test cannot show: real copies executed through the queue proxy, finished | ||
| 1257 | // through the fence proxy and read back byte for byte, and heap, queue and fence proxies in the residency calls. | ||
| 1258 | |||
| 1259 | std::uint64_t raw(void* object, unsigned slot, std::uint64_t a1 = 0, std::uint64_t a2 = 0, std::uint64_t a3 = 0, | ||
| 1260 | std::uint64_t a4 = 0, std::uint64_t a5 = 0, std::uint64_t a6 = 0, std::uint64_t a7 = 0) { | ||
| 1261 | void** vtable = *reinterpret_cast<void***>(object); | ||
| 1262 | return reinterpret_cast<SlotFn>(vtable[slot])(object, a1, a2, a3, a4, a5, a6, a7, 0, 0, 0, 0, 0, 0, 0); | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | template<class T> | ||
| 1266 | std::uint64_t word(T* pointer) { return reinterpret_cast<std::uint64_t>(pointer); } | ||
| 1267 | |||
| 1268 | int check_xbox_client(const Backend& b) { | ||
| 1269 | const char* label = b.label; | ||
| 1270 | void* device = b.device; | ||
| 1271 | constexpr UINT64 kBytes = 256; | ||
| 1272 | |||
| 1273 | // Device slot 76: buffers from an Xbox description (layout word 0x1, row-major buffer). | ||
| 1274 | const auto create_buffer = [&](D3D12_HEAP_TYPE type, D3D12_RESOURCE_STATES state, ID3D12Resource** out) { | ||
| 1275 | D3D12_HEAP_PROPERTIES heap{}; | ||
| 1276 | heap.Type = type; | ||
| 1277 | D3D12_RESOURCE_DESC desc{}; | ||
| 1278 | desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; | ||
| 1279 | desc.Width = kBytes; | ||
| 1280 | desc.Height = desc.DepthOrArraySize = desc.MipLevels = 1; | ||
| 1281 | desc.SampleDesc.Count = 1; | ||
| 1282 | desc.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x1); | ||
| 1283 | const IID iid = __uuidof(ID3D12Resource); | ||
| 1284 | return as_hr(raw(device, 76, word(&heap), D3D12_HEAP_FLAG_NONE, word(&desc), state, 0, word(&iid), word(out))); | ||
| 1285 | }; | ||
| 1286 | Ref<ID3D12Resource> upload, gpu, readback; | ||
| 1287 | EXPECT(SUCCEEDED(create_buffer(D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, &upload.p))); | ||
| 1288 | EXPECT(SUCCEEDED(create_buffer(D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COPY_DEST, &gpu.p))); | ||
| 1289 | EXPECT(SUCCEEDED(create_buffer(D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST, &readback.p))); | ||
| 1290 | { | ||
| 1291 | std::uint8_t* bytes = nullptr; | ||
| 1292 | EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast<void**>(&bytes)))); | ||
| 1293 | for (UINT64 i = 0; i < kBytes; ++i) bytes[i] = static_cast<std::uint8_t>(i * 7 + 3); | ||
| 1294 | upload->Unmap(0, nullptr); | ||
| 1295 | } | ||
| 1296 | |||
| 1297 | // Device slot 14 and heap slot 9 in the Xbox form (one argument, handle in RAX). | ||
| 1298 | D3D12_DESCRIPTOR_HEAP_DESC heap_desc{}; | ||
| 1299 | heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; | ||
| 1300 | heap_desc.NumDescriptors = 8; | ||
| 1301 | const IID iid_heap = __uuidof(ID3D12DescriptorHeap); | ||
| 1302 | Ref<ID3D12DescriptorHeap> heap; | ||
| 1303 | EXPECT(SUCCEEDED(as_hr(raw(device, 14, word(&heap_desc), word(&iid_heap), word(&heap.p))))); | ||
| 1304 | EXPECT(xs::is_heap_proxy(heap.p)); | ||
| 1305 | EXPECT(raw(heap.p, 9) == xs::heap_proxy_real_heap(heap.p)->GetCPUDescriptorHandleForHeapStart().ptr); | ||
| 1306 | |||
| 1307 | // Device slots 8 and 36. | ||
| 1308 | const D3D12_COMMAND_QUEUE_DESC queue_desc{}; | ||
| 1309 | const IID iid_queue = __uuidof(ID3D12CommandQueue), iid_fence = __uuidof(ID3D12Fence); | ||
| 1310 | Ref<ID3D12CommandQueue> queue; | ||
| 1311 | Ref<ID3D12Fence> fence, second; | ||
| 1312 | EXPECT(SUCCEEDED(as_hr(raw(device, 8, word(&queue_desc), word(&iid_queue), word(&queue.p))))); | ||
| 1313 | EXPECT(SUCCEEDED(as_hr(raw(device, 36, 0, D3D12_FENCE_FLAG_NONE, word(&iid_fence), word(&fence.p))))); | ||
| 1314 | EXPECT(SUCCEEDED(as_hr(raw(device, 36, 0, D3D12_FENCE_FLAG_NONE, word(&iid_fence), word(&second.p))))); | ||
| 1315 | EXPECT(xs::is_queue_proxy(queue.p) && xs::is_fence_proxy(fence.p) && xs::is_fence_proxy(second.p)); | ||
| 1316 | |||
| 1317 | // Residency with heap, queue and fence proxies among the objects (device slots 34, 35, 46). | ||
| 1318 | ID3D12Pageable* pageables[] = {heap.p, gpu.p, fence.p, queue.p}; | ||
| 1319 | EXPECT(SUCCEEDED(as_hr(raw(device, 34, 4, word(pageables))))); | ||
| 1320 | EXPECT(SUCCEEDED(as_hr(raw(device, 35, 4, word(pageables))))); | ||
| 1321 | EXPECT(SUCCEEDED(as_hr(raw(device, 34, 4, word(pageables))))); | ||
| 1322 | ID3D12Pageable* resources[] = {gpu.p}; | ||
| 1323 | const D3D12_RESIDENCY_PRIORITY priority[] = {D3D12_RESIDENCY_PRIORITY_HIGH}; | ||
| 1324 | EXPECT(SUCCEEDED(as_hr(raw(device, 46, 1, word(resources), word(priority))))); | ||
| 1325 | |||
| 1326 | // The copies: upload -> GPU buffer -> readback, executed through queue slot 10 and finished through slot 14 | ||
| 1327 | // and fence slot 9. | ||
| 1328 | Ref<ID3D12CommandAllocator> allocator; | ||
| 1329 | Ref<ID3D12GraphicsCommandList> list; | ||
| 1330 | EXPECT(SUCCEEDED(b.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); | ||
| 1331 | EXPECT(SUCCEEDED(b.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, | ||
| 1332 | IID_PPV_ARGS(&list.p)))); | ||
| 1333 | list->CopyBufferRegion(gpu.p, 0, upload.p, 0, kBytes); | ||
| 1334 | D3D12_RESOURCE_BARRIER barrier{}; | ||
| 1335 | barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; | ||
| 1336 | barrier.Transition.pResource = gpu.p; | ||
| 1337 | barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; | ||
| 1338 | barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST; | ||
| 1339 | barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; | ||
| 1340 | list->ResourceBarrier(1, &barrier); | ||
| 1341 | list->CopyBufferRegion(readback.p, 0, gpu.p, 0, kBytes); | ||
| 1342 | EXPECT(SUCCEEDED(list->Close())); | ||
| 1343 | ID3D12CommandList* lists[] = {list.p}; | ||
| 1344 | raw(queue.p, 10, 1, word(lists)); | ||
| 1345 | EXPECT(SUCCEEDED(as_hr(raw(queue.p, 14, word(fence.p), 1)))); | ||
| 1346 | Event done; | ||
| 1347 | EXPECT(SUCCEEDED(as_hr(raw(fence.p, 9, 1, word(done.h)))) && done.set_within(5000)); | ||
| 1348 | EXPECT(raw(fence.p, 8) >= 1); | ||
| 1349 | { | ||
| 1350 | std::uint8_t* bytes = nullptr; | ||
| 1351 | const D3D12_RANGE read_range{0, kBytes}; | ||
| 1352 | EXPECT(SUCCEEDED(readback->Map(0, &read_range, reinterpret_cast<void**>(&bytes)))); | ||
| 1353 | bool same = true; | ||
| 1354 | for (UINT64 i = 0; i < kBytes; ++i) same = same && bytes[i] == static_cast<std::uint8_t>(i * 7 + 3); | ||
| 1355 | const D3D12_RANGE nothing{0, 0}; | ||
| 1356 | readback->Unmap(0, ¬hing); | ||
| 1357 | EXPECT(same); // the GPU really ran the list the queue proxy submitted | ||
| 1358 | } | ||
| 1359 | |||
| 1360 | // Device slot 50: EnqueueMakeResident with the objects and a fence proxy to signal. | ||
| 1361 | EXPECT(SUCCEEDED(as_hr(raw(device, 50, D3D12_RESIDENCY_FLAG_NONE, 4, word(pageables), word(fence.p), 2)))); | ||
| 1362 | EXPECT(SUCCEEDED(as_hr(raw(fence.p, 9, 2, word(done.h)))) && done.set_within(5000)); | ||
| 1363 | |||
| 1364 | // Queue slot 15 holds the queue until the CPU (fence slot 10) signals; device slot 45 then sees both fences. | ||
| 1365 | EXPECT(SUCCEEDED(as_hr(raw(queue.p, 15, word(fence.p), 3)))); | ||
| 1366 | EXPECT(SUCCEEDED(as_hr(raw(queue.p, 14, word(second.p), 1)))); | ||
| 1367 | Sleep(50); | ||
| 1368 | EXPECT(raw(second.p, 8) == 0); | ||
| 1369 | EXPECT(SUCCEEDED(as_hr(raw(fence.p, 10, 3)))); | ||
| 1370 | ID3D12Fence* both[] = {fence.p, second.p}; | ||
| 1371 | const UINT64 values[] = {3, 1}; | ||
| 1372 | EXPECT(SUCCEEDED(as_hr(raw(device, 45, word(both), word(values), 2, D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL, | ||
| 1373 | word(done.h)))) && | ||
| 1374 | done.set_within(5000)); | ||
| 1375 | EXPECT(raw(second.p, 8) == 1); | ||
| 1376 | |||
| 1377 | // Release the way such a client does: through slot 2, the queue and fences before the resources. | ||
| 1378 | EXPECT(raw(second.p, 2) == 0); | ||
| 1379 | second.p = nullptr; | ||
| 1380 | EXPECT(raw(fence.p, 2) == 0); | ||
| 1381 | fence.p = nullptr; | ||
| 1382 | EXPECT(raw(queue.p, 2) == 0); | ||
| 1383 | queue.p = nullptr; | ||
| 1384 | std::printf("%s: console-style client: Xbox heap/resource slots, copies through the queue proxy read back " | ||
| 1385 | "intact, residency with heap, queue and fence proxies\n", label); | ||
| 1386 | return 0; | ||
| 1387 | } | ||
| 1388 | |||
| 1389 | // ---- 9. Coverage gate ---- | ||
| 1390 | // | ||
| 1391 | // Every changed device slot, every queue slot and every fence slot of the tables must have been called in this | ||
| 1392 | // process by the checks above (the differential cases run in child processes and do not count here), and the guard | ||
| 1393 | // region must have caught the overruns check_guard provoked. | ||
| 1394 | int check_coverage(const Backend& b) { | ||
| 1395 | const char* label = b.label; | ||
| 1396 | int missing = 0; | ||
| 1397 | const auto need = [&](const char* what, unsigned slot, std::uint64_t hits) { | ||
| 1398 | if (hits) return; | ||
| 1399 | std::fprintf(stderr, "%s: %s slot %u never called\n", label, what, slot); | ||
| 1400 | ++missing; | ||
| 1401 | }; | ||
| 1402 | // Slots an earlier check skips on purpose on this backend (and says so) are not asked for: 75 without | ||
| 1403 | // ID3D12Device9, queue 8/9 without reserved resources, 31 without shared fences, 32 when they cannot be opened. | ||
| 1404 | const bool device9 = xs::device_proxy_slot_count(b.device) > 75; | ||
| 1405 | for (unsigned slot : {8u, 34u, 35u, 36u, 45u, 46u, 50u}) need("device", slot, xs::device_slot_hits(slot)); | ||
| 1406 | if (device9) need("device", 75, xs::device_slot_hits(75)); | ||
| 1407 | if (g_shared_created) need("device", 31, xs::device_slot_hits(31)); | ||
| 1408 | if (g_shared_checked) need("device", 32, xs::device_slot_hits(32)); | ||
| 1409 | for (unsigned slot = 0; slot < xs::queue_proxy_table_size(); ++slot) { | ||
| 1410 | if ((slot == 8 || slot == 9) && !g_reserved_checked) continue; | ||
| 1411 | need("queue", slot, xs::queue_slot_hits(slot)); | ||
| 1412 | } | ||
| 1413 | for (unsigned slot = 0; slot < xs::fence_proxy_table_size(); ++slot) need("fence", slot, xs::fence_slot_hits(slot)); | ||
| 1414 | EXPECT(missing == 0); | ||
| 1415 | EXPECT(xs::queue_table_overruns() >= 3 && xs::fence_table_overruns() >= 3); | ||
| 1416 | std::printf("%s: coverage: device slots 8/34/35/36/45/46/50%s%s%s, %s %u queue and all %u fence slots, both " | ||
| 1417 | "guard regions\n", label, device9 ? "/75" : "", g_shared_created ? "/31" : "", | ||
| 1418 | g_shared_checked ? "/32" : "", g_reserved_checked ? "all" : "all but 8/9 of the", | ||
| 1419 | xs::queue_proxy_table_size(), xs::fence_proxy_table_size()); | ||
| 1420 | return 0; | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | // ---- 6. Eight threads at once ---- | ||
| 1424 | |||
| 1425 | int check_threads(const Backend& b) { | ||
| 1426 | const char* label = b.label; | ||
| 1427 | ID3D12Device* device = b.device; | ||
| 1428 | const unsigned long device_refs = xs::device_proxy_ref_count(device); | ||
| 1429 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 1430 | Ref<ID3D12CommandQueue> shared_queue; | ||
| 1431 | Ref<ID3D12Fence> shared_fence; | ||
| 1432 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&shared_queue.p)))); | ||
| 1433 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&shared_fence.p)))); | ||
| 1434 | constexpr int kThreads = 8, kRounds = 300, kSignals = 20; | ||
| 1435 | std::atomic<int> failures{0}; | ||
| 1436 | std::vector<std::thread> threads; | ||
| 1437 | for (int t = 0; t < kThreads; ++t) { | ||
| 1438 | threads.emplace_back([&, t] { | ||
| 1439 | const auto fail = [&](int line) { | ||
| 1440 | std::fprintf(stderr, "%s thread %d line %d\n", label, t, line); | ||
| 1441 | failures.fetch_add(1); | ||
| 1442 | }; | ||
| 1443 | for (int i = 0; i < kRounds; ++i) { | ||
| 1444 | shared_queue->AddRef(); | ||
| 1445 | ID3D12Fence* f = nullptr; | ||
| 1446 | if (FAILED(shared_fence->QueryInterface(IID_PPV_ARGS(&f))) || f != shared_fence.p) fail(__LINE__); | ||
| 1447 | if (f) f->Release(); | ||
| 1448 | ID3D12Device* owner = nullptr; | ||
| 1449 | if (FAILED(shared_queue->GetDevice(IID_PPV_ARGS(&owner))) || owner != device) fail(__LINE__); | ||
| 1450 | if (owner) owner->Release(); | ||
| 1451 | (void)shared_fence->GetCompletedValue(); | ||
| 1452 | ID3D12Fence* mine = nullptr; // device slot 36 from every thread | ||
| 1453 | if (FAILED(device->CreateFence(i, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mine))) || | ||
| 1454 | !xs::is_fence_proxy(mine) || mine->GetCompletedValue() != static_cast<UINT64>(i)) | ||
| 1455 | fail(__LINE__); | ||
| 1456 | if (mine) mine->Release(); | ||
| 1457 | shared_queue->Release(); | ||
| 1458 | } | ||
| 1459 | // GPU work from every thread: its own queue and fence, signalled and waited on. | ||
| 1460 | // The shared queue signals a fence of its own per thread: were it to signal `fence`, it could land after | ||
| 1461 | // the thread's queue had moved `fence` to v + 1 and set it back to v, which the PC runtime allows (the | ||
| 1462 | // Vulkan layer drops such a signal), and the wait for v + 1 would never end. | ||
| 1463 | ID3D12CommandQueue* queue = nullptr; | ||
| 1464 | ID3D12Fence* fence = nullptr; | ||
| 1465 | ID3D12Fence* mark = nullptr; | ||
| 1466 | HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 1467 | if (!event || FAILED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue))) || | ||
| 1468 | FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence))) || | ||
| 1469 | FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&mark)))) | ||
| 1470 | fail(__LINE__); | ||
| 1471 | for (UINT64 v = 1; event && queue && fence && mark && v <= kSignals; ++v) { | ||
| 1472 | if (FAILED(queue->Signal(fence, v)) || FAILED(fence->SetEventOnCompletion(v, event)) || | ||
| 1473 | WaitForSingleObject(event, 5000) != WAIT_OBJECT_0) | ||
| 1474 | fail(__LINE__); | ||
| 1475 | if (FAILED(shared_queue->Signal(mark, v))) fail(__LINE__); // one queue, many threads | ||
| 1476 | } | ||
| 1477 | // The shared queue's signals land in order: the last one means all of this thread's have. | ||
| 1478 | if (event && mark && (FAILED(mark->SetEventOnCompletion(kSignals, event)) || | ||
| 1479 | WaitForSingleObject(event, 5000) != WAIT_OBJECT_0)) | ||
| 1480 | fail(__LINE__); | ||
| 1481 | if (event) CloseHandle(event); | ||
| 1482 | if (mark) mark->Release(); | ||
| 1483 | if (fence) fence->Release(); | ||
| 1484 | if (queue) queue->Release(); | ||
| 1485 | }); | ||
| 1486 | } | ||
| 1487 | for (auto& thread : threads) thread.join(); | ||
| 1488 | EXPECT(failures.load() == 0); | ||
| 1489 | // The shared queue's last Signals must also have landed. | ||
| 1490 | Ref<ID3D12Fence> tail; | ||
| 1491 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&tail.p)))); | ||
| 1492 | EXPECT(SUCCEEDED(shared_queue->Signal(tail.p, 1))); | ||
| 1493 | if (wait_for(label, tail.p, 1)) return 1; | ||
| 1494 | tail.reset(); | ||
| 1495 | EXPECT(xs::command_object_proxy_ref_count(shared_queue.p) == 1); | ||
| 1496 | EXPECT(xs::command_object_proxy_ref_count(shared_fence.p) == 1); | ||
| 1497 | shared_queue.reset(); | ||
| 1498 | shared_fence.reset(); | ||
| 1499 | EXPECT(xs::device_proxy_ref_count(device) == device_refs); | ||
| 1500 | std::printf("%s: %d threads x %d rounds of AddRef/QueryInterface/GetDevice/CreateFence/Release, " | ||
| 1501 | "%d GPU signals each, consistent\n", label, kThreads, kRounds, kSignals); | ||
| 1502 | return 0; | ||
| 1503 | } | ||
| 1504 | |||
| 1505 | // ---- 7. Repeated creation and release orders, without leaks ---- | ||
| 1506 | |||
| 1507 | // One round on a fresh device proxy over `real`, which the caller keeps one reference to; the proxy takes another. | ||
| 1508 | // `children_last` releases the device proxy before its queues and fences. | ||
| 1509 | int one_round(const char* label, ID3D12Device* real, bool vulkan, bool children_last) { | ||
| 1510 | real->AddRef(); | ||
| 1511 | ID3D12Device* device = nullptr; | ||
| 1512 | EXPECT(SUCCEEDED(xs::create_device_proxy(real, __uuidof(ID3D12Device), reinterpret_cast<void**>(&device)))); | ||
| 1513 | if (vulkan) rt::d3d12::set_device_outer(real, device); | ||
| 1514 | D3D12_COMMAND_QUEUE_DESC desc{}; | ||
| 1515 | ID3D12CommandQueue* queues[3] = {}; | ||
| 1516 | ID3D12Fence* fences[3] = {}; | ||
| 1517 | for (int i = 0; i < 3; ++i) { | ||
| 1518 | EXPECT(SUCCEEDED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queues[i])))); | ||
| 1519 | EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fences[i])))); | ||
| 1520 | EXPECT(SUCCEEDED(queues[i]->Signal(fences[i], 1))); | ||
| 1521 | } | ||
| 1522 | for (int i = 0; i < 3; ++i) | ||
| 1523 | if (wait_for(label, fences[i], 1)) return 1; | ||
| 1524 | if (children_last) { | ||
| 1525 | // Release the title's device reference first: the children keep the proxy alive and still work. | ||
| 1526 | device->Release(); | ||
| 1527 | EXPECT(SUCCEEDED(queues[0]->Signal(fences[1], 2))); | ||
| 1528 | if (wait_for(label, fences[1], 2)) return 1; | ||
| 1529 | ID3D12Device* owner = nullptr; | ||
| 1530 | EXPECT(SUCCEEDED(fences[2]->GetDevice(IID_PPV_ARGS(&owner))) && owner == device); | ||
| 1531 | owner->Release(); | ||
| 1532 | for (int i = 2; i >= 0; --i) { | ||
| 1533 | fences[i]->Release(); | ||
| 1534 | queues[i]->Release(); | ||
| 1535 | } | ||
| 1536 | } else { | ||
| 1537 | for (int i = 0; i < 3; ++i) { | ||
| 1538 | queues[i]->Release(); | ||
| 1539 | fences[i]->Release(); | ||
| 1540 | } | ||
| 1541 | EXPECT(xs::device_proxy_ref_count(device) == 1); | ||
| 1542 | device->Release(); | ||
| 1543 | } | ||
| 1544 | return 0; | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | int check_repeated(const Backend& b) { | ||
| 1548 | const char* label = b.label; | ||
| 1549 | constexpr int kRounds = 12; | ||
| 1550 | b.real->AddRef(); | ||
| 1551 | const ULONG refs_before = b.real->Release(); | ||
| 1552 | int failed = 0; | ||
| 1553 | for (int round = 0; round < kRounds && !failed; ++round) | ||
| 1554 | failed = one_round(label, b.real, b.vulkan, round % 2 == 1); | ||
| 1555 | // The layer's device names its outer device for the children it creates; give it back the backend's proxy, as | ||
| 1556 | // the round's proxy is gone. | ||
| 1557 | if (b.vulkan) rt::d3d12::set_device_outer(b.real, b.device); | ||
| 1558 | if (failed) return 1; | ||
| 1559 | // Every round's proxy, queue and fence is gone: the real device has the references it had before. | ||
| 1560 | b.real->AddRef(); | ||
| 1561 | EXPECT(b.real->Release() == refs_before); | ||
| 1562 | std::printf("%s: %d rounds of device proxy, 3 queues and 3 fences in both release orders, no leak\n", label, | ||
| 1563 | kRounds); | ||
| 1564 | return 0; | ||
| 1565 | } | ||
| 1566 | |||
| 1567 | int run_all(const Backend& b) { | ||
| 1568 | g_shared_checked = g_shared_created = g_reserved_checked = false; | ||
| 1569 | return check_slots(b) || check_guard(b) || check_differential(b) || check_identity(b) || check_shared(b) || | ||
| 1570 | check_threads(b) || check_repeated(b) || check_xbox_client(b) || check_coverage(b); | ||
| 1571 | } | ||
| 1572 | |||
| 1573 | // Host PC runtime on `adapter`. 77 when no device can be made on it. | ||
| 1574 | int run_host(IDXGIAdapter1* adapter, const char* label, const char* kind) { | ||
| 1575 | ID3D12Device* real = nullptr; | ||
| 1576 | if (FAILED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&real)))) { | ||
| 1577 | std::printf("%s: no D3D12 device, skipped\n", label); | ||
| 1578 | return 77; | ||
| 1579 | } | ||
| 1580 | real->AddRef(); // the backend's own reference; the proxy takes the other | ||
| 1581 | Ref<ID3D12Device> device; | ||
| 1582 | if (FAILED(xs::create_device_proxy(real, __uuidof(ID3D12Device), reinterpret_cast<void**>(&device.p)))) { | ||
| 1583 | real->Release(); | ||
| 1584 | real->Release(); | ||
| 1585 | std::fprintf(stderr, "%s: device proxy creation failed\n", label); | ||
| 1586 | return 1; | ||
| 1587 | } | ||
| 1588 | const int result = run_all(Backend{label, device.p, real, false, kind}); | ||
| 1589 | device.reset(); | ||
| 1590 | if (result) return 1; | ||
| 1591 | if (real->Release() != 0) { // nothing the proxies made kept the PC device alive | ||
| 1592 | std::fprintf(stderr, "%s: the PC device outlives its proxies\n", label); | ||
| 1593 | return 1; | ||
| 1594 | } | ||
| 1595 | return 0; | ||
| 1596 | } | ||
| 1597 | |||
| 1598 | } // namespace | ||
| 1599 | |||
| 1600 | // A failure ends the process with a message on stderr, never in a dialog a test run would wait on: abort() (an | ||
| 1601 | // exception that escapes, for one), the debug CRT's reports, and Windows' fault and critical-error boxes. | ||
| 1602 | static void no_dialogs() { | ||
| 1603 | _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); | ||
| 1604 | #ifdef _DEBUG // the report functions exist only in the debug CRT (empty macros otherwise) | ||
| 1605 | for (const int type : {_CRT_WARN, _CRT_ERROR, _CRT_ASSERT}) { | ||
| 1606 | _CrtSetReportMode(type, _CRTDBG_MODE_FILE); | ||
| 1607 | _CrtSetReportFile(type, _CRTDBG_FILE_STDERR); | ||
| 1608 | } | ||
| 1609 | #endif | ||
| 1610 | SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | int main(int argc, char** argv) { | ||
| 1614 | no_dialogs(); | ||
| 1615 | std::setvbuf(stdout, nullptr, _IONBF, 0); | ||
| 1616 | if (argc == 5 && std::strcmp(argv[1], "--diff") == 0) | ||
| 1617 | return run_diff_child(argv[2], std::atoi(argv[3]), std::strcmp(argv[4], "proxy") == 0); | ||
| 1618 | if (argc == 3 && (std::strcmp(argv[1], "--child-flood") == 0 || std::strcmp(argv[1], "--child-hang") == 0 || | ||
| 1619 | std::strcmp(argv[1], "--child-crash") == 0)) | ||
| 1620 | return run_runner_child(argv[1], std::strtoul(argv[2], nullptr, 10)); | ||
| 1621 | // --trace-d3d12, as gta6rt takes it: every proxy call is recorded on the trace channel as well. | ||
| 1622 | if (argc == 2 && std::strcmp(argv[1], "--trace-d3d12") == 0) rt::d3d12_trace::set_enabled(true); | ||
| 1623 | else if (argc != 1) return 2; | ||
| 1624 | // The differential check depends on the child runner, which needs no device: checked first, on every machine. | ||
| 1625 | if (check_child_runner()) return 1; | ||
| 1626 | int ran = 0; | ||
| 1627 | |||
| 1628 | // Host PC runtime: WARP, then the first hardware adapter. Under Wine the host d3d12.dll is Wine's own, not the | ||
| 1629 | // PC runtime the proxy is written against (as d3d12_copy_compare.h's host_is_wine), so only Vulkan runs there. | ||
| 1630 | const bool wine = GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "wine_get_version") != nullptr; | ||
| 1631 | Ref<IDXGIFactory4> factory; | ||
| 1632 | if (wine) std::printf("host D3D12: under Wine, skipped\n"); | ||
| 1633 | if (!wine && SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p)))) { | ||
| 1634 | Ref<IDXGIAdapter1> warp; | ||
| 1635 | if (SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p)))) { | ||
| 1636 | const int r = run_host(warp.p, "host WARP", "warp"); | ||
| 1637 | if (r == 1) return 1; | ||
| 1638 | ran += r == 0; | ||
| 1639 | } | ||
| 1640 | for (UINT i = 0;; ++i) { | ||
| 1641 | Ref<IDXGIAdapter1> adapter; | ||
| 1642 | if (factory->EnumAdapters1(i, &adapter.p) == DXGI_ERROR_NOT_FOUND) break; | ||
| 1643 | DXGI_ADAPTER_DESC1 desc{}; | ||
| 1644 | if (FAILED(adapter->GetDesc1(&desc)) || (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) continue; | ||
| 1645 | char name[128] = {}; | ||
| 1646 | WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, name, sizeof(name) - 1, nullptr, nullptr); | ||
| 1647 | char host_label[160]; | ||
| 1648 | std::snprintf(host_label, sizeof(host_label), "host %s", name); | ||
| 1649 | const int r = run_host(adapter.p, host_label, "hw"); | ||
| 1650 | if (r == 1) return 1; | ||
| 1651 | ran += r == 0; | ||
| 1652 | break; | ||
| 1653 | } | ||
| 1654 | } | ||
| 1655 | |||
| 1656 | // The Vulkan-backed device, the way D3D12XboxCreateDevice hands it out with LEONIDA_D3D12=vulkan. | ||
| 1657 | vktest::Device d; | ||
| 1658 | if (d.create()) { | ||
| 1659 | const char* label = "Vulkan"; | ||
| 1660 | rt::d3d12::VulkanDeviceInfo info{}; | ||
| 1661 | info.get_instance_proc = d.instance_proc; | ||
| 1662 | info.instance = d.instance; | ||
| 1663 | info.physical = d.physical; | ||
| 1664 | info.device = d.device; | ||
| 1665 | info.queue = d.queue; | ||
| 1666 | info.queue_family = d.family; | ||
| 1667 | ID3D12Device* inner = nullptr; | ||
| 1668 | EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner)))); | ||
| 1669 | inner->AddRef(); // the backend's own reference; the proxy takes the other | ||
| 1670 | ID3D12Device* device = nullptr; | ||
| 1671 | EXPECT(SUCCEEDED(xs::create_device_proxy(inner, __uuidof(ID3D12Device), reinterpret_cast<void**>(&device)))); | ||
| 1672 | rt::d3d12::set_device_outer(inner, device); | ||
| 1673 | const int result = run_all(Backend{label, device, inner, true, "vulkan"}); | ||
| 1674 | device->Release(); | ||
| 1675 | if (result) return 1; | ||
| 1676 | EXPECT(inner->Release() == 0); // nothing the proxies made kept the layer's device alive | ||
| 1677 | ++ran; | ||
| 1678 | if (d.validation_errors()) { | ||
| 1679 | std::fprintf(stderr, "%u validation errors\n", d.validation_errors()); | ||
| 1680 | return 1; | ||
| 1681 | } | ||
| 1682 | } else { | ||
| 1683 | std::printf("Vulkan: no device, skipped\n"); | ||
| 1684 | } | ||
| 1685 | |||
| 1686 | if (!ran) { | ||
| 1687 | std::printf("SKIP queue_proxy_test: neither a host D3D12 device nor a Vulkan device\n"); | ||
| 1688 | return 77; | ||
| 1689 | } | ||
| 1690 | std::printf("queue_proxy_test: %d backend(s) passed\n", ran); | ||
| 1691 | return 0; | ||
| 1692 | } | ||