upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/d3d12_method_variants_test.cpp
diff options
context:
space:
mode:
authorkotorvr <332716892+kotorvr@users.noreply.github.com>2026-11-22 00:02:56 +0000
committerkotorvr <332716892+kotorvr@users.noreply.github.com>2026-11-22 01:03:35 +0000
commit382daba42f79b30ddbc4e6d8197464f36f290360 (patch)
treea3f865e28f77bfcb2eae322d3fe907ffff71366d /runtime/test/d3d12_method_variants_test.cpp
parent22590c7bbcb4f35bdc902fc4db0e9c4f556f9c39 (diff)
runtime: the 1/2/3 method variants on the Vulkan device (D3D-28, part 1)
CreateCommittedResource1/2/3, CreateCommandQueue1 and GetCopyableFootprints1 as thin wrappers over the existing methods. D3D12_RESOURCE_DESC1 drops to D3D12_RESOURCE_DESC; the initial state or barrier layout is unused, because new textures start UNDEFINED and move on first use. The newer arguments are answered as the PC runtime answers them (measured on an RTX 4090, driver 621.14): - a protected resource session: E_INVALIDARG (no session support reported); - a sampler-feedback mip region: E_INVALIDARG on textures, ignored on buffers; GetCopyableFootprints1 returns its failure answer for such a texture; - a buffer's initial layout other than UNDEFINED: E_INVALIDARG; - a castable count with a null list: no list; - a castable list: E_INVALIDARG, since it needs RelaxedFormatCastingSupported, which the layer does not report (D3D12_OPTIONS12 is unanswered); - CreateCommandQueue1's creator ID: ignored. d3d12_method_variants_test compares each variant with its base method, runs the edge cases and, when the host has a D3D12 device, compares each answer with the PC runtime live. Thanks to ezwinxdlmao for the probe notes in #156. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/d3d12_method_variants_test.cpp')
-rw-r--r--runtime/test/d3d12_method_variants_test.cpp447
1 files changed, 447 insertions, 0 deletions
diff --git a/runtime/test/d3d12_method_variants_test.cpp b/runtime/test/d3d12_method_variants_test.cpp
new file mode 100644
index 000000000..7bfba2a07
--- /dev/null
+++ b/runtime/test/d3d12_method_variants_test.cpp
@@ -0,0 +1,447 @@
1// The 1/2/3 method variants of the Vulkan-backed ID3D12Device (D3D-28), each compared with the method it wraps:
2// CreateCommittedResource1/2/3 with CreateCommittedResource, CreateCommandQueue1 with CreateCommandQueue and
3// GetCopyableFootprints1 with GetCopyableFootprints. The device is called directly, not through the Xbox-ABI proxy,
4// because the proxy's slot 76 is the Xbox texture method, not CreateCommittedResource3 (docs/d3d12_vulkan.md
5// section 7). The newer arguments' edge cases are checked against the PC runtime's answers, live when the host has
6// a D3D12 device. A texture from CreateCommittedResource3 goes through an upload, a copy and a readback on a queue
7// from CreateCommandQueue1. No shaders or game data. Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation; any validation error fails.
8#include "vk_test_device.h"
9#include "../shims/d3d12_com.h"
10
11#include <cstdint>
12#include <cstring>
13#include <vector>
14
15#define EXPECT(condition) do { \
16 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
17} while (0)
18
19namespace {
20
21template<class T>
22struct Ref {
23 T* p = nullptr;
24 ~Ref() { reset(); }
25 void reset() {
26 if (p) p->Release();
27 p = nullptr;
28 }
29 T* operator->() const { return p; }
30};
31
32D3D12_RESOURCE_DESC texture_desc(UINT width, UINT height, DXGI_FORMAT format, D3D12_RESOURCE_FLAGS flags,
33 UINT16 mips = 1, UINT16 layers = 1) {
34 D3D12_RESOURCE_DESC desc{};
35 desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
36 desc.Width = width;
37 desc.Height = height;
38 desc.DepthOrArraySize = layers;
39 desc.MipLevels = mips;
40 desc.Format = format;
41 desc.SampleDesc.Count = 1;
42 desc.Flags = flags;
43 return desc;
44}
45
46D3D12_RESOURCE_DESC buffer_desc(UINT64 bytes) {
47 D3D12_RESOURCE_DESC desc{};
48 desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
49 desc.Width = bytes;
50 desc.Height = 1;
51 desc.DepthOrArraySize = 1;
52 desc.MipLevels = 1;
53 desc.SampleDesc.Count = 1;
54 desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
55 return desc;
56}
57
58D3D12_RESOURCE_DESC volume_desc(UINT16 mips) {
59 auto desc = texture_desc(8, 6, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, mips, 16);
60 desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
61 return desc;
62}
63
64D3D12_RESOURCE_DESC1 desc1(const D3D12_RESOURCE_DESC& desc) {
65 D3D12_RESOURCE_DESC1 out{};
66 out.Dimension = desc.Dimension;
67 out.Alignment = desc.Alignment;
68 out.Width = desc.Width;
69 out.Height = desc.Height;
70 out.DepthOrArraySize = desc.DepthOrArraySize;
71 out.MipLevels = desc.MipLevels;
72 out.Format = desc.Format;
73 out.SampleDesc = desc.SampleDesc;
74 out.Layout = desc.Layout;
75 out.Flags = desc.Flags;
76 return out;
77}
78
79bool same_desc(const D3D12_RESOURCE_DESC& a, const D3D12_RESOURCE_DESC& b) {
80 return a.Dimension == b.Dimension && a.Alignment == b.Alignment && a.Width == b.Width && a.Height == b.Height &&
81 a.DepthOrArraySize == b.DepthOrArraySize && a.MipLevels == b.MipLevels && a.Format == b.Format &&
82 a.SampleDesc.Count == b.SampleDesc.Count && a.SampleDesc.Quality == b.SampleDesc.Quality &&
83 a.Layout == b.Layout && a.Flags == b.Flags;
84}
85
86// Never dereferenced: the device refuses any session before looking at it.
87ID3D12ProtectedResourceSession* fake_session() {
88 static int dummy;
89 return reinterpret_cast<ID3D12ProtectedResourceSession*>(&dummy);
90}
91
92struct Case {
93 const char* name;
94 D3D12_HEAP_TYPE heap;
95 D3D12_RESOURCE_DESC desc;
96};
97
98// A spread of what CreateCommittedResource accepts and refuses, so each variant is compared on both.
99std::vector<Case> cases() {
100 auto ms = texture_desc(8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
101 ms.SampleDesc.Count = 4;
102 auto ms_mips = ms;
103 ms_mips.MipLevels = 2;
104 return {
105 {"upload buffer", D3D12_HEAP_TYPE_UPLOAD, buffer_desc(4096)},
106 {"readback buffer", D3D12_HEAP_TYPE_READBACK, buffer_desc(300)},
107 {"default buffer", D3D12_HEAP_TYPE_DEFAULT, buffer_desc(65536)},
108 {"render target", D3D12_HEAP_TYPE_DEFAULT,
109 texture_desc(16, 8, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)},
110 {"depth-stencil", D3D12_HEAP_TYPE_DEFAULT,
111 texture_desc(8, 8, DXGI_FORMAT_D32_FLOAT_S8X24_UINT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)},
112 {"full mip chain", D3D12_HEAP_TYPE_DEFAULT,
113 texture_desc(16, 16, DXGI_FORMAT_R16G16B16A16_FLOAT, D3D12_RESOURCE_FLAG_NONE, 0, 2)},
114 {"BC7 array", D3D12_HEAP_TYPE_DEFAULT, texture_desc(8, 8, DXGI_FORMAT_BC7_UNORM, D3D12_RESOURCE_FLAG_NONE, 1, 3)},
115 {"volume", D3D12_HEAP_TYPE_DEFAULT, volume_desc(0)},
116 {"multisampled", D3D12_HEAP_TYPE_DEFAULT, ms},
117 // Refused by the base method; each variant must refuse them the same way.
118 {"texture on upload heap", D3D12_HEAP_TYPE_UPLOAD,
119 texture_desc(4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE)},
120 {"multisampled with mips", D3D12_HEAP_TYPE_DEFAULT, ms_mips},
121 {"unknown format", D3D12_HEAP_TYPE_DEFAULT,
122 texture_desc(4, 4, DXGI_FORMAT_R1_UNORM, D3D12_RESOURCE_FLAG_NONE)},
123 };
124}
125
126enum class Via { base, v1, v2, v3 };
127
128HRESULT create(ID3D12Device11* device, Via via, const D3D12_HEAP_PROPERTIES& heap, const D3D12_RESOURCE_DESC& desc,
129 void** out, ID3D12ProtectedResourceSession* session = nullptr) {
130 const D3D12_RESOURCE_DESC1 d1 = desc1(desc);
131 const IID& iid = __uuidof(ID3D12Resource);
132 switch (via) {
133 case Via::base:
134 return device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON,
135 nullptr, iid, out);
136 case Via::v1:
137 return device->CreateCommittedResource1(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON,
138 nullptr, session, iid, out);
139 case Via::v2:
140 return device->CreateCommittedResource2(&heap, D3D12_HEAP_FLAG_NONE, &d1, D3D12_RESOURCE_STATE_COMMON,
141 nullptr, session, iid, out);
142 case Via::v3:
143 return device->CreateCommittedResource3(&heap, D3D12_HEAP_FLAG_NONE, &d1,
144 desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER
145 ? D3D12_BARRIER_LAYOUT_UNDEFINED
146 : D3D12_BARRIER_LAYOUT_COMMON,
147 nullptr, session, 0, nullptr, iid, out);
148 }
149 return E_FAIL;
150}
151
152// Each variant against CreateCommittedResource on every case: the same result, and on success the same
153// description, heap properties, image-or-buffer and Map answer. Returns the number of differences.
154int compare_creation(ID3D12Device11* device) {
155 int differences = 0;
156 for (const Case& c : cases()) {
157 const D3D12_HEAP_PROPERTIES heap{c.heap};
158 Ref<ID3D12Resource> base;
159 const HRESULT base_hr = create(device, Via::base, heap, c.desc, reinterpret_cast<void**>(&base.p));
160 const HRESULT base_check = create(device, Via::base, heap, c.desc, nullptr); // null output: a check
161 for (const Via via : {Via::v1, Via::v2, Via::v3}) {
162 Ref<ID3D12Resource> variant;
163 const HRESULT hr = create(device, via, heap, c.desc, reinterpret_cast<void**>(&variant.p));
164 const HRESULT check = create(device, via, heap, c.desc, nullptr);
165 const auto differ = [&](const char* what) {
166 if (differences++ < 24)
167 std::fprintf(stderr, "%s, CreateCommittedResource%d: %s differs\n", c.name, int(via), what);
168 };
169 if (hr != base_hr) differ("result");
170 if (check != base_check) differ("null-output result");
171 if (!base.p || !variant.p) {
172 if (base.p || variant.p) differ("output");
173 continue;
174 }
175 if (!same_desc(base->GetDesc(), variant->GetDesc())) differ("description");
176 D3D12_HEAP_PROPERTIES a{}, b{};
177 D3D12_HEAP_FLAGS fa{}, fb{};
178 base->GetHeapProperties(&a, &fa);
179 variant->GetHeapProperties(&b, &fb);
180 if (a.Type != b.Type || fa != fb) differ("heap properties");
181 if (!rt::d3d12::vulkan_image(base.p) != !rt::d3d12::vulkan_image(variant.p)) differ("image");
182 void* ma = nullptr;
183 void* mb = nullptr;
184 if ((base->Map(0, nullptr, &ma) == S_OK) != (variant->Map(0, nullptr, &mb) == S_OK)) differ("Map");
185 }
186 }
187 return differences;
188}
189
190bool same_footprints(ID3D12Device11* device, const D3D12_RESOURCE_DESC* desc, UINT first, UINT count, UINT64 base) {
191 std::vector<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> a(count), b(count);
192 std::vector<UINT> rows_a(count), rows_b(count);
193 std::vector<UINT64> size_a(count), size_b(count);
194 UINT64 total_a = 0, total_b = 1;
195 device->GetCopyableFootprints(desc, first, count, base, a.data(), rows_a.data(), size_a.data(), &total_a);
196 const D3D12_RESOURCE_DESC1 d1 = desc ? desc1(*desc) : D3D12_RESOURCE_DESC1{};
197 device->GetCopyableFootprints1(desc ? &d1 : nullptr, first, count, base, b.data(), rows_b.data(), size_b.data(),
198 &total_b);
199 for (UINT i = 0; i < count; ++i) { // field by field: the struct has padding
200 const D3D12_SUBRESOURCE_FOOTPRINT &x = a[i].Footprint, &y = b[i].Footprint;
201 if (a[i].Offset != b[i].Offset || x.Format != y.Format || x.Width != y.Width || x.Height != y.Height ||
202 x.Depth != y.Depth || x.RowPitch != y.RowPitch)
203 return false;
204 }
205 return total_a == total_b && rows_a == rows_b && size_a == size_b;
206}
207
208struct Edge {
209 const char* name;
210 HRESULT got, want; // `want` is the PC runtime's answer, measured on an RTX 4090 (driver 621.14)
211};
212
213// The edge cases of the newer arguments, run on any ID3D12Device10: ours, or the host's PC runtime to compare
214// with. A GetCopyableFootprints1 answer is S_FALSE when every output is all ones (the failure answer), S_OK when
215// the footprint is the ordinary one, E_FAIL otherwise.
216std::vector<Edge> edge_answers(ID3D12Device10* device) {
217 const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}, upload_heap{D3D12_HEAP_TYPE_UPLOAD};
218 D3D12_RESOURCE_DESC1 texture = desc1(texture_desc(8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
219 D3D12_RESOURCE_DESC1 buffer = desc1(buffer_desc(300));
220 D3D12_RESOURCE_DESC1 texture_region = texture, buffer_region = buffer;
221 texture_region.SamplerFeedbackMipRegion = buffer_region.SamplerFeedbackMipRegion = {4, 4, 1};
222 const auto v2 = [&](const D3D12_HEAP_PROPERTIES& heap, const D3D12_RESOURCE_DESC1& desc) {
223 Ref<ID3D12Resource> r;
224 return device->CreateCommittedResource2(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON,
225 nullptr, nullptr, IID_PPV_ARGS(&r.p));
226 };
227 const auto v3 = [&](const D3D12_HEAP_PROPERTIES& heap, const D3D12_RESOURCE_DESC1& desc, D3D12_BARRIER_LAYOUT layout,
228 UINT32 castable_count = 0, bool output = true) {
229 Ref<ID3D12Resource> r;
230 return device->CreateCommittedResource3(&heap, D3D12_HEAP_FLAG_NONE, &desc, layout, nullptr, nullptr,
231 castable_count, nullptr, __uuidof(ID3D12Resource),
232 output ? reinterpret_cast<void**>(&r.p) : nullptr);
233 };
234 const auto footprint = [&](const D3D12_RESOURCE_DESC1& desc) {
235 D3D12_PLACED_SUBRESOURCE_FOOTPRINT layout{};
236 UINT rows = 0;
237 UINT64 row_size = 0, total = 0;
238 device->GetCopyableFootprints1(&desc, 0, 1, 0, &layout, &rows, &row_size, &total);
239 if (layout.Offset == ~0ull && rows == ~0u && row_size == ~0ull && total == ~0ull) return S_FALSE;
240 return layout.Offset == 0 && rows != ~0u && total != ~0ull ? S_OK : E_FAIL;
241 };
242 const GUID creator{0x1d2c3b4a, 0x5f6e, 0x7a8b, {0x9c, 0x0d, 0x1e, 0x2f, 0x3a, 0x4b, 0x5c, 0x6d}};
243 void* queue = nullptr;
244 const HRESULT null_queue = device->CreateCommandQueue1(nullptr, creator, __uuidof(ID3D12CommandQueue), &queue);
245 if (queue) static_cast<IUnknown*>(queue)->Release();
246 return {
247 {"CreateCommittedResource2, texture with a mip region", v2(default_heap, texture_region), E_INVALIDARG},
248 {"CreateCommittedResource2, buffer with a mip region", v2(upload_heap, buffer_region), S_OK},
249 {"CreateCommittedResource3, texture with a mip region",
250 v3(default_heap, texture_region, D3D12_BARRIER_LAYOUT_COMMON), E_INVALIDARG},
251 {"CreateCommittedResource3, buffer with a mip region",
252 v3(upload_heap, buffer_region, D3D12_BARRIER_LAYOUT_UNDEFINED), S_OK},
253 {"CreateCommittedResource3, buffer in layout COMMON", v3(upload_heap, buffer, D3D12_BARRIER_LAYOUT_COMMON),
254 E_INVALIDARG},
255 {"CreateCommittedResource3, texture in layout UNDEFINED",
256 v3(default_heap, texture, D3D12_BARRIER_LAYOUT_UNDEFINED), S_OK},
257 {"CreateCommittedResource3, castable count with a null list",
258 v3(default_heap, texture, D3D12_BARRIER_LAYOUT_COMMON, 2), S_OK},
259 {"CreateCommittedResource3, null output",
260 v3(default_heap, texture, D3D12_BARRIER_LAYOUT_COMMON, 0, false), S_FALSE},
261 {"CreateCommandQueue1, null description", null_queue, E_INVALIDARG},
262 {"GetCopyableFootprints1, texture with a mip region", footprint(texture_region), S_FALSE},
263 {"GetCopyableFootprints1, buffer with a mip region", footprint(buffer_region), S_OK},
264 };
265}
266
267} // namespace
268
269int main() {
270 vktest::Device d;
271 EXPECT(d.create());
272 {
273 rt::d3d12::VulkanDeviceInfo info{};
274 info.get_instance_proc = d.instance_proc;
275 info.instance = d.instance;
276 info.physical = d.physical;
277 info.device = d.device;
278 info.queue = d.queue;
279 info.queue_family = d.family;
280 Ref<ID3D12Device11> device;
281 EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device.p))));
282
283 // CreateCommittedResource1/2/3 against CreateCommittedResource.
284 const int differences = compare_creation(device.p);
285 EXPECT(differences == 0);
286
287 // A protected session is refused by every variant (the layer reports no session support); nothing is
288 // handed out.
289 const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}, upload_heap{D3D12_HEAP_TYPE_UPLOAD},
290 readback_heap{D3D12_HEAP_TYPE_READBACK};
291 for (const Via via : {Via::v1, Via::v2, Via::v3}) {
292 void* out = reinterpret_cast<void*>(1);
293 EXPECT(create(device.p, via, upload_heap, buffer_desc(256), &out, fake_session()) == E_INVALIDARG);
294 EXPECT(out == nullptr);
295 }
296
297 // The newer arguments' edge cases, each with the answer the PC runtime gives (and, when the host has a
298 // D3D12 device, compared with it live).
299 const std::vector<Edge> ours = edge_answers(device.p);
300 for (const Edge& e : ours) {
301 if (e.got == e.want) continue;
302 std::fprintf(stderr, "%s: got %08lx, want %08lx\n", e.name, e.got, e.want);
303 return 1;
304 }
305 {
306 const HMODULE d3d12 = LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
307 const auto create_host = d3d12 ? reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(
308 reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")))
309 : nullptr;
310 Ref<ID3D12Device10> host;
311 if (create_host && SUCCEEDED(create_host(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) {
312 const std::vector<Edge> theirs = edge_answers(host.p);
313 for (std::size_t i = 0; i < ours.size(); ++i) {
314 if (ours[i].got == theirs[i].got) continue;
315 std::fprintf(stderr, "%s: ours %08lx, host runtime %08lx\n", ours[i].name, ours[i].got,
316 theirs[i].got);
317 return 1;
318 }
319 } else {
320 std::printf("no host D3D12 device: edge cases checked against their recorded answers only\n");
321 }
322 }
323
324 // Castable formats: a list is refused, since the layer reports no relaxed format casting (D3D12_OPTIONS12).
325 // Not compared with the host, whose answer depends on its GPU's RelaxedFormatCastingSupported.
326 {
327 const D3D12_RESOURCE_DESC1 texture =
328 desc1(texture_desc(4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
329 const DXGI_FORMAT own[] = {DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB};
330 Ref<ID3D12Resource> r;
331 EXPECT(device->CreateCommittedResource3(&default_heap, D3D12_HEAP_FLAG_NONE, &texture,
332 D3D12_BARRIER_LAYOUT_COMMON, nullptr, nullptr, 2, own,
333 IID_PPV_ARGS(&r.p)) == E_INVALIDARG && !r.p);
334 }
335
336 // A texture from CreateCommittedResource3 (a castable count with a null list, which is no list) is
337 // uploaded, copied back and compared on a queue from CreateCommandQueue1.
338 Ref<ID3D12CommandQueue> queue, queue1;
339 D3D12_COMMAND_QUEUE_DESC queue_desc{};
340 queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
341 queue_desc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_HIGH;
342 EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
343 // CreateCommandQueue1 against CreateCommandQueue: the same description, a working queue, the same refusal
344 // of a missing description.
345 const GUID creator{0x1d2c3b4a, 0x5f6e, 0x7a8b, {0x9c, 0x0d, 0x1e, 0x2f, 0x3a, 0x4b, 0x5c, 0x6d}};
346 EXPECT(SUCCEEDED(device->CreateCommandQueue1(&queue_desc, creator, IID_PPV_ARGS(&queue1.p))));
347 {
348 const D3D12_COMMAND_QUEUE_DESC a = queue->GetDesc(), b = queue1->GetDesc();
349 EXPECT(a.Type == b.Type && a.Priority == b.Priority && a.Flags == b.Flags && a.NodeMask == b.NodeMask);
350 void* out = nullptr;
351 EXPECT(device->CreateCommandQueue(nullptr, __uuidof(ID3D12CommandQueue), &out) ==
352 device->CreateCommandQueue1(nullptr, creator, __uuidof(ID3D12CommandQueue), &out));
353 }
354
355 constexpr UINT size = 8;
356 const D3D12_RESOURCE_DESC1 castable_desc =
357 desc1(texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET));
358 Ref<ID3D12Resource> texture, upload, readback;
359 EXPECT(SUCCEEDED(device->CreateCommittedResource3(&default_heap, D3D12_HEAP_FLAG_NONE, &castable_desc,
360 D3D12_BARRIER_LAYOUT_COPY_DEST, nullptr, nullptr, 3, nullptr,
361 IID_PPV_ARGS(&texture.p))));
362 EXPECT(rt::d3d12::vulkan_image(texture.p));
363 const D3D12_RESOURCE_DESC1 bytes = desc1(buffer_desc(4096));
364 EXPECT(SUCCEEDED(device->CreateCommittedResource3(&upload_heap, D3D12_HEAP_FLAG_NONE, &bytes,
365 D3D12_BARRIER_LAYOUT_UNDEFINED, nullptr, nullptr, 0, nullptr,
366 IID_PPV_ARGS(&upload.p))));
367 EXPECT(SUCCEEDED(device->CreateCommittedResource2(&readback_heap, D3D12_HEAP_FLAG_NONE, &bytes,
368 D3D12_RESOURCE_STATE_COPY_DEST, nullptr, nullptr,
369 IID_PPV_ARGS(&readback.p))));
370 D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed{};
371 device->GetCopyableFootprints1(&castable_desc, 0, 1, 0, &placed, nullptr, nullptr, nullptr);
372 std::uint8_t* up = nullptr;
373 std::uint8_t* down = nullptr;
374 EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast<void**>(&up))));
375 EXPECT(SUCCEEDED(readback->Map(0, nullptr, reinterpret_cast<void**>(&down))));
376 for (UINT y = 0; y < size; ++y)
377 for (UINT x = 0; x < size * 4; ++x) up[placed.Offset + y * placed.Footprint.RowPitch + x] = std::uint8_t(y * 32 + x);
378 std::memset(down, 0xcd, 4096);
379
380 Ref<ID3D12CommandAllocator> allocator;
381 Ref<ID3D12GraphicsCommandList> list;
382 Ref<ID3D12Fence> fence;
383 EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
384 EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
385 IID_PPV_ARGS(&list.p))));
386 EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
387 D3D12_TEXTURE_COPY_LOCATION from_buffer{}, to_texture{}, from_texture{}, to_buffer{};
388 from_buffer.pResource = upload.p;
389 from_buffer.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
390 from_buffer.PlacedFootprint = placed;
391 to_texture.pResource = texture.p;
392 to_texture.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
393 from_texture = to_texture;
394 to_buffer = from_buffer;
395 to_buffer.pResource = readback.p;
396 list->CopyTextureRegion(&to_texture, 0, 0, 0, &from_buffer, nullptr);
397 D3D12_RESOURCE_BARRIER barrier{};
398 barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
399 barrier.Transition.pResource = texture.p;
400 barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
401 barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
402 barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
403 list->ResourceBarrier(1, &barrier);
404 list->CopyTextureRegion(&to_buffer, 0, 0, 0, &from_texture, nullptr);
405 EXPECT(list->Close() == S_OK);
406 ID3D12CommandList* lists[] = {list.p};
407 queue1->ExecuteCommandLists(1, lists); // on the queue CreateCommandQueue1 made
408 EXPECT(rt::d3d12::last_execute_result(queue1.p) == leonida::ReplayResult::success);
409 EXPECT(SUCCEEDED(queue1->Signal(fence.p, 1)));
410 EXPECT(SUCCEEDED(fence->SetEventOnCompletion(1, nullptr)) && fence->GetCompletedValue() == 1);
411 unsigned wrong = 0;
412 for (UINT y = 0; y < size; ++y) {
413 const std::size_t row = std::size_t(placed.Offset + y * placed.Footprint.RowPitch);
414 wrong += std::memcmp(down + row, up + row, size * 4) != 0;
415 }
416 EXPECT(wrong == 0);
417 upload->Unmap(0, nullptr);
418 readback->Unmap(0, nullptr);
419
420 // GetCopyableFootprints1 against GetCopyableFootprints, byte for byte, including the failure answer.
421 struct Footprints {
422 D3D12_RESOURCE_DESC desc;
423 UINT first, count;
424 };
425 const Footprints footprints[] = {
426 {buffer_desc(300), 0, 1},
427 {buffer_desc(300), 0, 2}, // refused: a buffer has one subresource
428 {volume_desc(5), 0, 5},
429 {texture_desc(8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 4, 2), 0, 8},
430 {texture_desc(10, 6, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE, 4), 1, 3},
431 {texture_desc(8, 8, DXGI_FORMAT_D24_UNORM_S8_UINT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 2, 2), 0, 8},
432 {texture_desc(300, 5, DXGI_FORMAT_D32_FLOAT_S8X24_UINT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL), 0, 2},
433 {texture_desc(4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), 0, 2}, // refused: too many
434 };
435 for (const Footprints& f : footprints)
436 for (const UINT64 base : {UINT64(0), UINT64(1024)})
437 EXPECT(same_footprints(device.p, &f.desc, f.first, f.count, base));
438 EXPECT(same_footprints(device.p, nullptr, 0, 1, 0));
439 }
440 if (d.validation_errors()) {
441 std::fprintf(stderr, "%u validation errors\n", d.validation_errors());
442 return 1;
443 }
444 std::printf("D3D12 method variants: CreateCommittedResource1/2/3, CreateCommandQueue1 and GetCopyableFootprints1 "
445 "matched their base methods and the PC runtime's edge-case answers; a texture round-tripped\n");
446 return 0;
447}