upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorrasptum <348594691+rasptum@users.noreply.github.com>2026-11-21 21:56:53 -0500
committerrasptum <348594691+rasptum@users.noreply.github.com>2026-11-21 21:56:53 -0500
commit4fa0fb7dee0f7e9d78e719e2b05f3636e56fc773 (patch)
tree69b1667f11d28fce3e1c6555d56ff40993372cd0 /runtime
parent67204bf8c0471335d9046890288b33e8d5e2875b (diff)
Runtime: ID3D12Resource::GetDesc fills in a defaulted Alignment, as the host does
A resource created with Alignment 0 read back Alignment 0 from the Vulkan-backed device. The PC runtime and WARP report the placement alignment instead: 64 KiB, or 4 MiB for a multisampled texture, and keep one the description asked for. d3d12_resource_desc_test compares every GetDesc field of 26 committed and reserved descriptions with the default adapter and WARP. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/CMakeLists.txt6
-rw-r--r--runtime/README.md13
-rw-r--r--runtime/shims/d3d12_vulkan_device.cpp11
-rw-r--r--runtime/test/d3d12_resource_desc_test.cpp201
4 files changed, 230 insertions, 1 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index adfc452aa..d4c6d71e8 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -564,6 +564,12 @@ if(TARGET Vulkan::Headers)
564 target_compile_definitions(d3d12_typeless_rtv_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) 564 target_compile_definitions(d3d12_typeless_rtv_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
565 target_compile_options(d3d12_typeless_rtv_test PRIVATE /W4 /permissive- /utf-8) 565 target_compile_options(d3d12_typeless_rtv_test PRIVATE /W4 /permissive- /utf-8)
566 566
567 # ID3D12Resource::GetDesc against the host runtime and WARP; run explicitly on a Vulkan GPU.
568 add_executable(d3d12_resource_desc_test test/d3d12_resource_desc_test.cpp)
569 target_link_libraries(d3d12_resource_desc_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
570 target_compile_definitions(d3d12_resource_desc_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
571 target_compile_options(d3d12_resource_desc_test PRIVATE /W4 /permissive- /utf-8)
572
567 # The loader can create this device instead of the host's (D3D12XboxCreateDevice, LEONIDA_D3D12=vulkan). 573 # The loader can create this device instead of the host's (D3D12XboxCreateDevice, LEONIDA_D3D12=vulkan).
568 if(TARGET gta6rt) 574 if(TARGET gta6rt)
569 target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_com) 575 target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_com)
diff --git a/runtime/README.md b/runtime/README.md
index b8cb19ec1..3b5589740 100644
--- a/runtime/README.md
+++ b/runtime/README.md
@@ -218,3 +218,16 @@ $env:VK_VALIDATION_VALIDATE_SYNC = '1'
218``` 218```
219 219
220RGBA8 typeless RTV clears support explicit UNORM/SRGB/UINT views of 2D mip slices and contiguous array-layer ranges, with sample count 1. Other format families, typed-resource casts, MSAA and 3D views are not expanded. 220RGBA8 typeless RTV clears support explicit UNORM/SRGB/UINT views of 2D mip slices and contiguous array-layer ranges, with sample count 1. Other format families, typed-resource casts, MSAA and 3D views are not expanded.
221
222### Resource descriptions
223
224`d3d12_resource_desc_test` creates the same committed and reserved resources on the Vulkan-backed device and on
225Windows D3D12 (the default adapter and WARP, whichever exist) and compares every field of `ID3D12Resource::GetDesc`:
226the `Alignment` and `MipLevels` a description left at 0 must read back as the host fills them in. A description the
227host refuses is skipped, and one only the layer refuses (1D textures today) is listed and not compared. It needs a
228Vulkan GPU and runs explicitly.
229
230```powershell
231$env:LEONIDA_VK_VALIDATION = '1'
232.\runtime\build\RelWithDebInfo\d3d12_resource_desc_test.exe
233```
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp
index 9599bbae0..5a2b53c9e 100644
--- a/runtime/shims/d3d12_vulkan_device.cpp
+++ b/runtime/shims/d3d12_vulkan_device.cpp
@@ -621,7 +621,16 @@ public:
621 return S_OK; 621 return S_OK;
622 } 622 }
623 623
624 D3D12_RESOURCE_DESC STDMETHODCALLTYPE GetDesc() override { return desc_; } 624 // D3D12 reports the placement alignment of a description that left it 0: 64 KiB, or 4 MiB for a multisampled
625 // texture. One the description asked for is kept. Measured on the PC runtime and WARP (d3d12_resource_desc_test).
626 D3D12_RESOURCE_DESC STDMETHODCALLTYPE GetDesc() override {
627 D3D12_RESOURCE_DESC desc = desc_;
628 if (!desc.Alignment) {
629 desc.Alignment = desc.SampleDesc.Count > 1 ? D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT
630 : D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
631 }
632 return desc;
633 }
625 634
626 // A reserved resource is a committed one underneath (docs/d3d12_vulkan.md section 4.2); it reports the layout it 635 // A reserved resource is a committed one underneath (docs/d3d12_vulkan.md section 4.2); it reports the layout it
627 // was created with and has no heap of its own. 636 // was created with and has no heap of its own.
diff --git a/runtime/test/d3d12_resource_desc_test.cpp b/runtime/test/d3d12_resource_desc_test.cpp
new file mode 100644
index 000000000..741801f43
--- /dev/null
+++ b/runtime/test/d3d12_resource_desc_test.cpp
@@ -0,0 +1,201 @@
1// Compare ID3D12Resource::GetDesc on the Vulkan-backed device with Windows D3D12 (the default adapter and WARP):
2// what a description that leaves fields at their defaults (Alignment 0, MipLevels 0) reads back as.
3// No game data or shaders. Run explicitly (needs Vulkan hardware); LEONIDA_VK_VALIDATION=1 checks Vulkan usage.
4#include "vk_test_device.h"
5#include "../shims/d3d12_com.h"
6#include <dxgi1_4.h>
7#include <vector>
8
9namespace {
10template<class T>
11struct Ref {
12 T* p = nullptr;
13 ~Ref() { if (p) p->Release(); }
14 T* operator->() const { return p; }
15};
16
17struct Case {
18 const char* name;
19 D3D12_RESOURCE_DESC desc;
20 D3D12_HEAP_TYPE heap; // of a committed resource
21 D3D12_RESOURCE_STATES state;
22 bool reserved;
23};
24
25D3D12_RESOURCE_DESC buffer(UINT64 bytes, UINT64 alignment = 0, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE) {
26 D3D12_RESOURCE_DESC desc{};
27 desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
28 desc.Alignment = alignment;
29 desc.Width = bytes;
30 desc.Height = desc.SampleDesc.Count = 1;
31 desc.DepthOrArraySize = desc.MipLevels = 1;
32 desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
33 desc.Flags = flags;
34 return desc;
35}
36
37D3D12_RESOURCE_DESC texture(D3D12_RESOURCE_DIMENSION dimension, UINT64 width, UINT height, UINT16 depth_or_layers,
38 UINT16 mips, DXGI_FORMAT format, UINT samples = 1,
39 D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE, UINT64 alignment = 0) {
40 D3D12_RESOURCE_DESC desc{};
41 desc.Dimension = dimension;
42 desc.Alignment = alignment;
43 desc.Width = width;
44 desc.Height = height;
45 desc.DepthOrArraySize = depth_or_layers;
46 desc.MipLevels = mips;
47 desc.Format = format;
48 desc.SampleDesc.Count = samples;
49 desc.Flags = flags;
50 return desc;
51}
52
53HRESULT create(ID3D12Device* device, const Case& c, ID3D12Resource** out) {
54 if (c.reserved) return device->CreateReservedResource(&c.desc, c.state, nullptr, IID_PPV_ARGS(out));
55 D3D12_HEAP_PROPERTIES heap{};
56 heap.Type = c.heap;
57 return device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &c.desc, c.state, nullptr, IID_PPV_ARGS(out));
58}
59
60bool same(const D3D12_RESOURCE_DESC& a, const D3D12_RESOURCE_DESC& b) {
61 return a.Dimension == b.Dimension && a.Alignment == b.Alignment && a.Width == b.Width && a.Height == b.Height &&
62 a.DepthOrArraySize == b.DepthOrArraySize && a.MipLevels == b.MipLevels && a.Format == b.Format &&
63 a.SampleDesc.Count == b.SampleDesc.Count && a.SampleDesc.Quality == b.SampleDesc.Quality &&
64 a.Layout == b.Layout && a.Flags == b.Flags;
65}
66
67void print(const char* who, const D3D12_RESOURCE_DESC& d) {
68 std::fprintf(stderr,
69 " %-6s dimension %d alignment %llu %llux%ux%u mips %u format %d samples %u/%u layout %d flags 0x%x\n",
70 who, int(d.Dimension), static_cast<unsigned long long>(d.Alignment),
71 static_cast<unsigned long long>(d.Width), d.Height, unsigned(d.DepthOrArraySize), unsigned(d.MipLevels),
72 int(d.Format), d.SampleDesc.Count, d.SampleDesc.Quality, int(d.Layout), unsigned(d.Flags));
73}
74
75struct Totals {
76 unsigned matched = 0, mismatched = 0, host_refused = 0, ours_refused = 0;
77};
78
79// Every case the reference creates must read back the same from the layer. A case the reference refuses says
80// nothing about GetDesc and is skipped; one only the layer refuses is counted (dimensions it does not create yet).
81Totals compare(ID3D12Device* ours, ID3D12Device* host, const char* host_name, const std::vector<Case>& cases) {
82 Totals totals;
83 for (const Case& c : cases) {
84 Ref<ID3D12Resource> expected, actual;
85 if (FAILED(create(host, c, &expected.p)) || !expected.p) {
86 ++totals.host_refused;
87 continue;
88 }
89 const HRESULT hr = create(ours, c, &actual.p);
90 if (FAILED(hr) || !actual.p) {
91 std::fprintf(stderr, "%s: not created by the layer (0x%08lx), not compared with %s\n", c.name,
92 static_cast<unsigned long>(hr), host_name);
93 ++totals.ours_refused;
94 continue;
95 }
96 const D3D12_RESOURCE_DESC a = actual->GetDesc(), e = expected->GetDesc();
97 if (same(a, e)) {
98 ++totals.matched;
99 continue;
100 }
101 ++totals.mismatched;
102 std::fprintf(stderr, "%s: GetDesc differs from %s\n", c.name, host_name);
103 print("asked", c.desc);
104 print("ours", a);
105 print("host", e);
106 }
107 return totals;
108}
109} // namespace
110
111int main() {
112 vktest::Device vk;
113 if (!vk.create()) return 1;
114 unsigned matched = 0, mismatched = 0, references = 0;
115 {
116 Ref<ID3D12Device> ours;
117 rt::d3d12::VulkanDeviceInfo info{};
118 info.get_instance_proc = vk.instance_proc;
119 info.instance = vk.instance;
120 info.physical = vk.physical;
121 info.device = vk.device;
122 info.queue = vk.queue;
123 info.queue_family = vk.family;
124 if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1;
125
126 constexpr auto t1 = D3D12_RESOURCE_DIMENSION_TEXTURE1D, t2 = D3D12_RESOURCE_DIMENSION_TEXTURE2D,
127 t3 = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
128 constexpr auto rgba8 = DXGI_FORMAT_R8G8B8A8_UNORM;
129 constexpr auto none = D3D12_RESOURCE_FLAG_NONE, target = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET,
130 depth = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, uav = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
131 constexpr auto common = D3D12_RESOURCE_STATE_COMMON, render = D3D12_RESOURCE_STATE_RENDER_TARGET;
132 constexpr auto default_heap = D3D12_HEAP_TYPE_DEFAULT;
133 constexpr UINT64 k4 = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT, k64 = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT,
134 m4 = D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT;
135 D3D12_RESOURCE_DESC tiled = texture(t2, 1024, 1024, 1, 0, rgba8);
136 tiled.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
137 const std::vector<Case> cases = {
138 // Buffers: Alignment 0 reads back as 64 KiB on every heap type.
139 {"buffer, default heap", buffer(100), default_heap, common, false},
140 {"buffer, upload heap", buffer(100), D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ, false},
141 {"buffer, readback heap", buffer(100), D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST, false},
142 {"buffer, 64 KiB asked", buffer(100, k64), default_heap, common, false},
143 {"buffer, unordered access", buffer(70000, 0, uav), default_heap, common, false},
144 // Single-sampled textures: 64 KiB unless the description asks for 4 KiB; MipLevels 0 is the full chain.
145 {"2D 64x64", texture(t2, 64, 64, 1, 1, rgba8), default_heap, common, false},
146 {"2D 64x64, all mips", texture(t2, 64, 64, 1, 0, rgba8), default_heap, common, false},
147 {"2D 100x60, all mips", texture(t2, 100, 60, 1, 0, rgba8), default_heap, common, false},
148 {"2D 64x64, 4 KiB asked", texture(t2, 64, 64, 1, 1, rgba8, 1, none, k4), default_heap, common, false},
149 {"2D 64x64, 64 KiB asked", texture(t2, 64, 64, 1, 1, rgba8, 1, none, k64), default_heap, common, false},
150 {"2D 1024x1024, all mips", texture(t2, 1024, 1024, 1, 0, rgba8), default_heap, common, false},
151 {"2D array of 6, all mips", texture(t2, 256, 256, 6, 0, rgba8), default_heap, common, false},
152 {"2D typeless", texture(t2, 64, 64, 1, 1, DXGI_FORMAT_R8G8B8A8_TYPELESS), default_heap, common, false},
153 {"2D render target", texture(t2, 256, 256, 1, 1, rgba8, 1, target), default_heap, render, false},
154 {"2D render target, all mips", texture(t2, 256, 256, 1, 0, rgba8, 1, target), default_heap, render, false},
155 {"2D depth", texture(t2, 256, 256, 1, 1, DXGI_FORMAT_D32_FLOAT, 1, depth), default_heap,
156 D3D12_RESOURCE_STATE_DEPTH_WRITE, false},
157 {"2D unordered access", texture(t2, 256, 256, 1, 1, rgba8, 1, uav), default_heap, common, false},
158 {"3D 32x32x32, all mips", texture(t3, 32, 32, 32, 0, rgba8), default_heap, common, false},
159 {"3D 64x16x4, all mips", texture(t3, 64, 16, 4, 0, rgba8), default_heap, common, false},
160 {"1D 128, all mips", texture(t1, 128, 1, 1, 0, rgba8), default_heap, common, false},
161 // Multisampled textures: 4 MiB unless the description asks for 64 KiB.
162 {"2D 4x render target", texture(t2, 256, 256, 1, 1, rgba8, 4, target), default_heap, render, false},
163 {"2D 4x render target, 64 KiB asked", texture(t2, 64, 64, 1, 1, rgba8, 4, target, k64), default_heap,
164 render, false},
165 {"2D 4x render target, 4 MiB asked", texture(t2, 64, 64, 1, 1, rgba8, 4, target, m4), default_heap, render,
166 false},
167 {"2D 4x depth", texture(t2, 256, 256, 1, 1, DXGI_FORMAT_D32_FLOAT, 4, depth), default_heap,
168 D3D12_RESOURCE_STATE_DEPTH_WRITE, false},
169 // Reserved resources report their own layout and the same alignment.
170 {"reserved 2D 1024x1024, all mips", tiled, default_heap, common, true},
171 {"reserved buffer", buffer(1 << 20), default_heap, common, true},
172 };
173
174 Ref<IDXGIFactory4> factory;
175 if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p)))) {
176 std::fprintf(stderr, "no DXGI factory\n");
177 return 1;
178 }
179 for (const bool warp : {false, true}) {
180 Ref<IDXGIAdapter> adapter;
181 Ref<ID3D12Device> host;
182 if (warp && FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter.p)))) continue;
183 if (FAILED(D3D12CreateDevice(adapter.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) continue;
184 const char* name = warp ? "WARP" : "the default adapter";
185 const Totals totals = compare(ours.p, host.p, name, cases);
186 std::printf("resource desc against %s: %u of %zu descriptions match, %u differ, %u refused by the host, "
187 "%u not created by the layer\n",
188 name, totals.matched, cases.size(), totals.mismatched, totals.host_refused, totals.ours_refused);
189 matched += totals.matched;
190 mismatched += totals.mismatched;
191 ++references;
192 }
193 }
194 if (!references) {
195 std::fprintf(stderr, "no D3D12 reference device (default adapter or WARP)\n");
196 return 1;
197 }
198 if (mismatched || !matched) return 1;
199 if (vk.validation_errors()) return 1;
200 return 0;
201}