upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/d3d12_query_rules_test.cpp
diff options
context:
space:
mode:
authorquackottu <224479491+quackottu@users.noreply.github.com>2026-11-22 13:54:34 +0530
committerquackottu <224479491+quackottu@users.noreply.github.com>2026-11-22 13:54:34 +0530
commitbe0e227dbd3537107765cf5c1df5e12d0e827b65 (patch)
treebcdfd4bc8833c9d6724982ad59628cd5848374d0 /runtime/test/d3d12_query_rules_test.cpp
parent2e5c0f283c618fdb37b4e9241748fa1e91e1abda (diff)
D3D12: refuse query calls the list type can't hold, and CreateQueryHeap node masks above 1
Diffstat (limited to 'runtime/test/d3d12_query_rules_test.cpp')
-rw-r--r--runtime/test/d3d12_query_rules_test.cpp308
1 files changed, 308 insertions, 0 deletions
diff --git a/runtime/test/d3d12_query_rules_test.cpp b/runtime/test/d3d12_query_rules_test.cpp
new file mode 100644
index 000000000..e77672b2f
--- /dev/null
+++ b/runtime/test/d3d12_query_rules_test.cpp
@@ -0,0 +1,308 @@
1// Query heaps and query calls on the Vulkan-backed device against Windows D3D12 (the default adapter and WARP):
2// CreateQueryHeap results by type, count and node mask, Close() results of lists that use queries on each list type,
3// and timestamps resolved on direct, compute and copy queues (pipeline statistics heaps are left out: the test
4// device does not enable pipelineStatisticsQuery). No game data or shaders. Run explicitly (needs Vulkan
5// hardware); LEONIDA_VK_VALIDATION=1 checks Vulkan usage.
6#include "vk_test_device.h"
7#include "../shims/d3d12_com.h"
8#include <dxgi1_4.h>
9#include <cstdio>
10#include <cstring>
11#include <functional>
12#include <string>
13#include <vector>
14
15namespace {
16template<class T>
17struct Ref {
18 T* p = nullptr;
19 ~Ref() { if (p) p->Release(); }
20 T* operator->() const { return p; }
21 T** put() { if (p) { p->Release(); p = nullptr; } return &p; }
22};
23
24D3D12_RESOURCE_DESC buffer(UINT64 bytes) {
25 D3D12_RESOURCE_DESC desc{};
26 desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
27 desc.Width = bytes;
28 desc.Height = desc.SampleDesc.Count = 1;
29 desc.DepthOrArraySize = desc.MipLevels = 1;
30 desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
31 return desc;
32}
33
34struct Env {
35 ID3D12Device* device;
36 Ref<ID3D12QueryHeap> occlusion, timestamps, copy_timestamps;
37 Ref<ID3D12Resource> dst; // default buffer, COPY_DEST
38 Ref<ID3D12Resource> readback; // readback buffer
39};
40
41std::string heap_cases(ID3D12Device* device) {
42 std::string out;
43 const D3D12_QUERY_HEAP_TYPE types[] = {D3D12_QUERY_HEAP_TYPE_OCCLUSION, D3D12_QUERY_HEAP_TYPE_TIMESTAMP,
44 D3D12_QUERY_HEAP_TYPE_COPY_QUEUE_TIMESTAMP, D3D12_QUERY_HEAP_TYPE(99)};
45 for (const auto type : types)
46 for (const UINT count : {0u, 1u})
47 for (const UINT mask : {0u, 1u, 2u, 3u}) {
48 Ref<ID3D12QueryHeap> heap;
49 D3D12_QUERY_HEAP_DESC desc{type, count, mask};
50 const HRESULT hr = device->CreateQueryHeap(&desc, IID_PPV_ARGS(heap.put()));
51 char line[128];
52 std::snprintf(line, sizeof line, "heap type %d count %u mask %u: %s\n", int(type), count, mask,
53 SUCCEEDED(hr) ? "created" : "refused");
54 out += line;
55 }
56 return out;
57}
58
59// Records one list of `type`, closes it and returns Close's result.
60HRESULT close_result(Env& e, D3D12_COMMAND_LIST_TYPE type, const std::function<void(ID3D12GraphicsCommandList*)>& rec) {
61 Ref<ID3D12CommandAllocator> allocator;
62 Ref<ID3D12GraphicsCommandList> list;
63 if (FAILED(e.device->CreateCommandAllocator(type, IID_PPV_ARGS(&allocator.p)))) return 0x7fff0001;
64 if (FAILED(e.device->CreateCommandList(0, type, allocator.p, nullptr, IID_PPV_ARGS(&list.p)))) return 0x7fff0002;
65 rec(list.p);
66 return list->Close();
67}
68
69// Executes one list on a new queue of its type and waits; true when it ran.
70bool run(Env& e, D3D12_COMMAND_LIST_TYPE type, const std::function<void(ID3D12GraphicsCommandList*)>& rec,
71 UINT64* frequency = nullptr, int* result = nullptr) {
72 Ref<ID3D12CommandQueue> queue;
73 D3D12_COMMAND_QUEUE_DESC qd{type};
74 if (FAILED(e.device->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue.p)))) return false;
75 if (frequency && FAILED(queue->GetTimestampFrequency(frequency))) *frequency = 0;
76 Ref<ID3D12CommandAllocator> allocator;
77 Ref<ID3D12GraphicsCommandList> list;
78 if (FAILED(e.device->CreateCommandAllocator(type, IID_PPV_ARGS(&allocator.p)))) return false;
79 if (FAILED(e.device->CreateCommandList(0, type, allocator.p, nullptr, IID_PPV_ARGS(&list.p)))) return false;
80 rec(list.p);
81 if (FAILED(list->Close())) return false;
82 ID3D12CommandList* lists[] = {list.p};
83 queue->ExecuteCommandLists(1, lists);
84 if (result) *result = int(rt::d3d12::last_execute_result(queue.p));
85 Ref<ID3D12Fence> fence;
86 if (FAILED(e.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))) return false;
87 queue->Signal(fence.p, 1);
88 for (int i = 0; i < 5000 && fence->GetCompletedValue() < 1; ++i) Sleep(1);
89 return fence->GetCompletedValue() >= 1;
90}
91
92UINT64 read_u64(Env& e, UINT64 offset) {
93 void* data = nullptr;
94 D3D12_RANGE range{0, 4096};
95 if (FAILED(e.readback->Map(0, &range, &data))) return ~0ull;
96 UINT64 v;
97 std::memcpy(&v, static_cast<char*>(data) + offset, 8);
98 D3D12_RANGE none{0, 0};
99 e.readback->Unmap(0, &none);
100 return v;
101}
102
103void fill_readback(Env& e, unsigned char byte) {
104 void* data = nullptr;
105 D3D12_RANGE none{0, 0};
106 if (FAILED(e.readback->Map(0, &none, &data))) return;
107 std::memset(data, byte, 4096);
108 e.readback->Unmap(0, nullptr);
109}
110
111bool setup(Env& e) {
112 D3D12_QUERY_HEAP_DESC desc{D3D12_QUERY_HEAP_TYPE_OCCLUSION, 4, 0};
113 if (FAILED(e.device->CreateQueryHeap(&desc, IID_PPV_ARGS(&e.occlusion.p)))) return false;
114 desc.Type = D3D12_QUERY_HEAP_TYPE_TIMESTAMP;
115 if (FAILED(e.device->CreateQueryHeap(&desc, IID_PPV_ARGS(&e.timestamps.p)))) return false;
116 desc.Type = D3D12_QUERY_HEAP_TYPE_COPY_QUEUE_TIMESTAMP;
117 e.device->CreateQueryHeap(&desc, IID_PPV_ARGS(&e.copy_timestamps.p));
118 D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT};
119 const auto bd = buffer(4096);
120 if (FAILED(e.device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &bd, D3D12_RESOURCE_STATE_COPY_DEST,
121 nullptr, IID_PPV_ARGS(&e.dst.p))))
122 return false;
123 heap.Type = D3D12_HEAP_TYPE_READBACK;
124 return SUCCEEDED(e.device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &bd,
125 D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
126 IID_PPV_ARGS(&e.readback.p)));
127}
128
129std::string list_cases(Env& e) {
130 std::string out;
131 using L = ID3D12GraphicsCommandList*;
132 constexpr auto direct = D3D12_COMMAND_LIST_TYPE_DIRECT, compute = D3D12_COMMAND_LIST_TYPE_COMPUTE,
133 copy = D3D12_COMMAND_LIST_TYPE_COPY, bundle = D3D12_COMMAND_LIST_TYPE_BUNDLE;
134 auto* occ = e.occlusion.p;
135 auto* ts = e.timestamps.p;
136 auto* cts = e.copy_timestamps.p ? e.copy_timestamps.p : e.timestamps.p;
137 auto* dst = e.dst.p;
138 struct Case {
139 const char* name;
140 D3D12_COMMAND_LIST_TYPE type;
141 std::function<void(L)> rec;
142 };
143 const Case cases[] = {
144 {"direct timestamp", direct, [&](L l) { l->EndQuery(ts, D3D12_QUERY_TYPE_TIMESTAMP, 0); }},
145 {"direct occlusion begin/end", direct,
146 [&](L l) { l->BeginQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); l->EndQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); }},
147 {"direct resolve timestamp", direct,
148 [&](L l) { l->ResolveQueryData(ts, D3D12_QUERY_TYPE_TIMESTAMP, 0, 1, dst, 0); }},
149 {"direct end without begin", direct, [&](L l) { l->EndQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); }},
150 {"direct begin twice", direct, [&](L l) {
151 l->BeginQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); l->BeginQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0);
152 l->EndQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); }},
153 {"direct begin timestamp", direct, [&](L l) { l->BeginQuery(ts, D3D12_QUERY_TYPE_TIMESTAMP, 0); }},
154 {"direct begin never ended", direct, [&](L l) { l->BeginQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); }},
155 {"direct resolve offset 4", direct,
156 [&](L l) { l->ResolveQueryData(ts, D3D12_QUERY_TYPE_TIMESTAMP, 0, 1, dst, 4); }},
157 {"direct predication offset 4", direct, [&](L l) {
158 l->SetPredication(dst, 4, D3D12_PREDICATION_OP_EQUAL_ZERO); l->SetPredication(nullptr, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); }},
159 {"compute timestamp", compute, [&](L l) { l->EndQuery(ts, D3D12_QUERY_TYPE_TIMESTAMP, 0); }},
160 {"compute resolve timestamp", compute,
161 [&](L l) { l->ResolveQueryData(ts, D3D12_QUERY_TYPE_TIMESTAMP, 0, 1, dst, 0); }},
162 {"compute occlusion begin/end", compute,
163 [&](L l) { l->BeginQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); l->EndQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); }},
164 {"compute predication", compute, [&](L l) {
165 l->SetPredication(dst, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); l->SetPredication(nullptr, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); }},
166 {"copy timestamp (copy heap)", copy, [&](L l) { l->EndQuery(cts, D3D12_QUERY_TYPE_TIMESTAMP, 0); }},
167 {"copy resolve timestamp", copy,
168 [&](L l) { l->ResolveQueryData(cts, D3D12_QUERY_TYPE_TIMESTAMP, 0, 1, dst, 0); }},
169 {"copy occlusion begin/end", copy,
170 [&](L l) { l->BeginQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); l->EndQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); }},
171 {"copy predication", copy, [&](L l) {
172 l->SetPredication(dst, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); l->SetPredication(nullptr, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); }},
173 {"bundle occlusion begin/end", bundle,
174 [&](L l) { l->BeginQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); l->EndQuery(occ, D3D12_QUERY_TYPE_OCCLUSION, 0); }},
175 {"bundle timestamp", bundle, [&](L l) { l->EndQuery(ts, D3D12_QUERY_TYPE_TIMESTAMP, 0); }},
176 {"bundle resolve", bundle, [&](L l) { l->ResolveQueryData(ts, D3D12_QUERY_TYPE_TIMESTAMP, 0, 1, dst, 0); }},
177 {"compute binary begin/end", compute, [&](L l) {
178 l->BeginQuery(occ, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 0); l->EndQuery(occ, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 0); }},
179 {"compute resolve occlusion", compute,
180 [&](L l) { l->ResolveQueryData(occ, D3D12_QUERY_TYPE_OCCLUSION, 0, 1, dst, 0); }},
181 {"copy resolve occlusion", copy,
182 [&](L l) { l->ResolveQueryData(occ, D3D12_QUERY_TYPE_OCCLUSION, 0, 1, dst, 0); }},
183 {"bundle predication", bundle, [&](L l) { l->SetPredication(nullptr, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); }},
184 };
185 for (const auto& c : cases) {
186 char line[160];
187 // Success or failure only: the layer reports E_INVALIDARG where the host sometimes gives E_FAIL.
188 std::snprintf(line, sizeof line, "close %s: %s\n", c.name,
189 SUCCEEDED(close_result(e, c.type, c.rec)) ? "ok" : "fails");
190 out += line;
191 }
192 return out;
193}
194
195// Timestamps on each queue type: written, resolved, copied to readback; reports whether each value is plausible.
196std::string timestamp_runs(Env& e) {
197 std::string out;
198 const std::pair<const char*, D3D12_COMMAND_LIST_TYPE> types[] = {
199 {"direct", D3D12_COMMAND_LIST_TYPE_DIRECT}, {"compute", D3D12_COMMAND_LIST_TYPE_COMPUTE},
200 {"copy", D3D12_COMMAND_LIST_TYPE_COPY}};
201 for (const auto& [name, type] : types) {
202 ID3D12QueryHeap* heap = type == D3D12_COMMAND_LIST_TYPE_COPY ? e.copy_timestamps.p : e.timestamps.p;
203 char line[200];
204 if (!heap) {
205 std::snprintf(line, sizeof line, "timestamps on %s: no heap\n", name);
206 out += line;
207 continue;
208 }
209 fill_readback(e, 0xcd);
210 UINT64 frequency = 0;
211 const bool ran = run(e, type, [&](ID3D12GraphicsCommandList* l) {
212 l->EndQuery(heap, D3D12_QUERY_TYPE_TIMESTAMP, 0);
213 l->EndQuery(heap, D3D12_QUERY_TYPE_TIMESTAMP, 1);
214 l->ResolveQueryData(heap, D3D12_QUERY_TYPE_TIMESTAMP, 0, 2, e.readback.p, 0);
215 }, &frequency);
216 const UINT64 t0 = read_u64(e, 0), t1 = read_u64(e, 8);
217 const bool plausible = ran && t0 && t0 != 0xcdcdcdcdcdcdcdcdull && t1 >= t0 && frequency &&
218 double(t1 - t0) / double(frequency) < 1.0;
219 std::snprintf(line, sizeof line, "timestamps on %s: %s\n", name, plausible ? "plausible" : "wrong");
220 out += line;
221 }
222 return out;
223}
224
225std::string all_cases(ID3D12Device* device) {
226 Env e{device};
227 if (!setup(e)) return "setup failed\n";
228 return heap_cases(device) + list_cases(e) + timestamp_runs(e);
229}
230
231std::vector<std::string> lines(const std::string& s) {
232 std::vector<std::string> out;
233 size_t at = 0;
234 while (at < s.size()) {
235 const size_t nl = s.find('\n', at);
236 out.push_back(s.substr(at, nl - at));
237 at = nl + 1;
238 }
239 return out;
240}
241} // namespace
242
243int main() {
244 vktest::Device vk;
245 const auto device_status = vk.initialize();
246 if (device_status == vktest::DeviceStatus::unavailable) return 77;
247 if (device_status != vktest::DeviceStatus::ready) return 1;
248 std::string mine;
249 unsigned mismatched = 0;
250 {
251 Ref<ID3D12Device> ours;
252 rt::d3d12::VulkanDeviceInfo info{};
253 info.get_instance_proc = vk.instance_proc;
254 info.instance = vk.instance;
255 info.physical = vk.physical;
256 info.device = vk.device;
257 info.queue = vk.queue;
258 info.queue_family = vk.family;
259 if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1;
260 mine = all_cases(ours.p);
261 { // A timestamp heap of the wrong kind for the list: the host refuses it at Close (E_INVALIDARG); the layer
262 // checks heap types at execution, so it must refuse the list there.
263 Env e{ours.p};
264 if (setup(e)) {
265 const std::pair<const char*, std::pair<D3D12_COMMAND_LIST_TYPE, ID3D12QueryHeap*>> m[] = {
266 {"copy list, TIMESTAMP heap", {D3D12_COMMAND_LIST_TYPE_COPY, e.timestamps.p}},
267 {"direct list, COPY heap", {D3D12_COMMAND_LIST_TYPE_DIRECT, e.copy_timestamps.p}},
268 {"compute list, COPY heap", {D3D12_COMMAND_LIST_TYPE_COMPUTE, e.copy_timestamps.p}}};
269 for (const auto& [name, tc] : m) {
270 int result = -1;
271 auto* heap = tc.second;
272 const bool ran = run(e, tc.first, [&](ID3D12GraphicsCommandList* l) {
273 l->EndQuery(heap, D3D12_QUERY_TYPE_TIMESTAMP, 0);
274 l->ResolveQueryData(heap, D3D12_QUERY_TYPE_TIMESTAMP, 0, 1, e.readback.p, 0);
275 }, nullptr, &result);
276 if (result != int(leonida::ReplayResult::invalid_query)) {
277 ++mismatched;
278 std::printf("[ours] %s: executed (ran %d, result %d), host refuses it\n", name, ran, result);
279 }
280 }
281 }
282 }
283 }
284 Ref<IDXGIFactory4> factory;
285 if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p)))) return 1;
286 unsigned differ = 0;
287 for (const bool warp : {false, true}) {
288 Ref<IDXGIAdapter> adapter;
289 Ref<ID3D12Device> host;
290 if (warp && FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter.p)))) continue;
291 if (FAILED(D3D12CreateDevice(adapter.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) continue;
292 const auto a = lines(mine), b = lines(all_cases(host.p));
293 for (size_t i = 0; i < a.size() || i < b.size(); ++i) {
294 const std::string x = i < a.size() ? a[i] : "", y = i < b.size() ? b[i] : "";
295 if (x != y) {
296 ++differ;
297 std::printf("[%s] ours: %s | host: %s\n", warp ? "WARP" : "GPU", x.c_str(), y.c_str());
298 } else if (!warp) {
299 std::printf("same: %s\n", x.c_str());
300 }
301 }
302 }
303 if (vk.validation_errors()) {
304 std::printf("validation errors\n");
305 return 1;
306 }
307 return differ || mismatched ? 1 : 0;
308}