diff options
| author | rasptum <348594691+rasptum@users.noreply.github.com> | 2026-11-21 21:56:53 -0500 |
|---|---|---|
| committer | rasptum <348594691+rasptum@users.noreply.github.com> | 2026-11-21 21:56:53 -0500 |
| commit | 4fa0fb7dee0f7e9d78e719e2b05f3636e56fc773 (patch) | |
| tree | 69b1667f11d28fce3e1c6555d56ff40993372cd0 /runtime/test | |
| parent | 67204bf8c0471335d9046890288b33e8d5e2875b (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/test')
| -rw-r--r-- | runtime/test/d3d12_resource_desc_test.cpp | 201 |
1 files changed, 201 insertions, 0 deletions
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 | |||
| 9 | namespace { | ||
| 10 | template<class T> | ||
| 11 | struct Ref { | ||
| 12 | T* p = nullptr; | ||
| 13 | ~Ref() { if (p) p->Release(); } | ||
| 14 | T* operator->() const { return p; } | ||
| 15 | }; | ||
| 16 | |||
| 17 | struct 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 | |||
| 25 | D3D12_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 | |||
| 37 | D3D12_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 | |||
| 53 | HRESULT 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 | |||
| 60 | bool 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 | |||
| 67 | void 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 | |||
| 75 | struct 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). | ||
| 81 | Totals 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 | |||
| 111 | int 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 | } | ||