diff options
| author | romainveurrier <romain.veurrier@gmail.com> | 2026-11-22 14:04:34 +0100 |
|---|---|---|
| committer | romainveurrier <romain.veurrier@gmail.com> | 2026-11-22 14:04:34 +0100 |
| commit | 1c58d6cfbc63ef3eb41d332c8aa443bb65ac4b4d (patch) | |
| tree | ad1aafa19ed74e0b550af8081fd7024b32596c83 /runtime/test | |
| parent | 51a028115446da42565fa75d4657e82c214afc2d (diff) | |
D3D12: BuildRaytracingAccelerationStructure writes XDXR BVH4 records
The game's translated RT shaders read the console's XDXR records, which a
VkAccelerationStructureKHR is not (docs/d3d12_vulkan.md 4.8). The standalone
Vulkan device now builds them with src/xdxr_build.h, unless LEONIDA_RT_AS=khr
keeps #743's KHR builds (the proposal in the maintainer decisions of #1548).
- VulkanDeviceInfo::xdxr_acceleration_structures; the standalone device sets
it with bufferDeviceAddress and then leaves the KHR extensions off, so
acceleration-structure buffers keep their console VAs.
- VulkanContext owns an XdxrBuilder; replay checks the inputs while the list
is prepared (R32G32B32/R32G32 vertices, none/R16/R32 indices, addresses,
counts) and records the build with heap_base added to every VA and as the
bias for instance BLAS addresses.
- GetRaytracingAccelerationStructurePrebuildInfo answers from XdxrLayout,
exactly; an update rebuilds, so it needs the same scratch.
- The geometry's vertex format is mapped per DXGI format (an unknown one is
refused instead of read as R32G32B32), and its D3D12 flags reach the build.
- test/d3d12_xdxr_rt_test.cpp: a two-geometry BLAS (a soup; an R16-indexed
grid with stride 16 and a transform) and a TLAS of three instances through
the D3D12 API; ctest d3d12_xdxr_rt + d3d12_xdxr_rt_check.
- tools/xdxr_bvh.py: check-build compares TLAS hits by (geometry, primitive),
and skips (77) when the build step wrote no cases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test')
| -rw-r--r-- | runtime/test/d3d12_xdxr_rt_test.cpp | 306 |
1 files changed, 306 insertions, 0 deletions
diff --git a/runtime/test/d3d12_xdxr_rt_test.cpp b/runtime/test/d3d12_xdxr_rt_test.cpp new file mode 100644 index 000000000..9499dbe0d --- /dev/null +++ b/runtime/test/d3d12_xdxr_rt_test.cpp | |||
| @@ -0,0 +1,306 @@ | |||
| 1 | // BuildRaytracingAccelerationStructure on the standalone Vulkan device writes XDXR BVH4 records (docs/d3d12_vulkan.md | ||
| 2 | // 4.8, src/xdxr_build.h), the layout the game's translated traversal shaders read. A BLAS of two geometries (an | ||
| 3 | // unindexed triangle soup, and an R16-indexed grid with a 16-byte stride and a 3x4 transform) and a TLAS of three | ||
| 4 | // instances are built through the D3D12 API, sized by GetRaytracingAccelerationStructurePrebuildInfo, copied to | ||
| 5 | // readback buffers and written to <out dir> as <case>.bin + <case>.json at their console GPU virtual addresses. | ||
| 6 | // `python -I tools/xdxr_bvh.py check-build <out dir>` validates them and traces rays against brute force (ctest: | ||
| 7 | // d3d12_xdxr_rt + d3d12_xdxr_rt_check). Exit 77 without a Vulkan device, or with LEONIDA_RT_AS=khr. No game files. | ||
| 8 | #include "../shims/d3d12_com.h" | ||
| 9 | |||
| 10 | #include <cmath> | ||
| 11 | #include <cstdint> | ||
| 12 | #include <cstdio> | ||
| 13 | #include <cstring> | ||
| 14 | #include <filesystem> | ||
| 15 | #include <fstream> | ||
| 16 | #include <random> | ||
| 17 | #include <string> | ||
| 18 | #include <vector> | ||
| 19 | |||
| 20 | namespace { | ||
| 21 | |||
| 22 | template<class T> struct Ref { | ||
| 23 | T* p = nullptr; | ||
| 24 | ~Ref() { if (p) p->Release(); } | ||
| 25 | T* operator->() const { return p; } | ||
| 26 | T** put() { return &p; } | ||
| 27 | }; | ||
| 28 | |||
| 29 | int fail(const char* what, HRESULT hr = S_OK) { | ||
| 30 | std::fprintf(stderr, "FAIL: %s (hr 0x%08lx)\n", what, static_cast<unsigned long>(hr)); | ||
| 31 | return 1; | ||
| 32 | } | ||
| 33 | |||
| 34 | HRESULT buffer(ID3D12Device* device, D3D12_HEAP_TYPE heap, UINT64 size, D3D12_RESOURCE_STATES state, | ||
| 35 | D3D12_RESOURCE_FLAGS flags, ID3D12Resource** out) { | ||
| 36 | D3D12_HEAP_PROPERTIES props{}; | ||
| 37 | props.Type = heap; | ||
| 38 | D3D12_RESOURCE_DESC desc{}; | ||
| 39 | desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; | ||
| 40 | desc.Width = size ? size : 1; | ||
| 41 | desc.Height = 1; | ||
| 42 | desc.DepthOrArraySize = 1; | ||
| 43 | desc.MipLevels = 1; | ||
| 44 | desc.SampleDesc.Count = 1; | ||
| 45 | desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; | ||
| 46 | desc.Flags = flags; | ||
| 47 | return device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr, IID_PPV_ARGS(out)); | ||
| 48 | } | ||
| 49 | |||
| 50 | std::string num(double x) { | ||
| 51 | char s[64]; | ||
| 52 | std::snprintf(s, sizeof s, "%.9g", x); | ||
| 53 | return s; | ||
| 54 | } | ||
| 55 | |||
| 56 | bool write_file(const std::string& path, const void* data, std::size_t bytes) { | ||
| 57 | std::ofstream f(path, std::ios::binary); | ||
| 58 | f.write(static_cast<const char*>(data), std::streamsize(bytes)); | ||
| 59 | return bool(f); | ||
| 60 | } | ||
| 61 | |||
| 62 | } // namespace | ||
| 63 | |||
| 64 | int main(int argc, char** argv) { | ||
| 65 | if (argc < 2) { | ||
| 66 | std::fprintf(stderr, "usage: d3d12_xdxr_rt_test <out dir>\n"); | ||
| 67 | return 2; | ||
| 68 | } | ||
| 69 | const std::string dir = argv[1]; | ||
| 70 | std::error_code ec; | ||
| 71 | for (const auto& entry : std::filesystem::directory_iterator(dir, ec)) { | ||
| 72 | const auto ext = entry.path().extension(); | ||
| 73 | if (ext == ".bin" || ext == ".json") std::filesystem::remove(entry.path(), ec); | ||
| 74 | } | ||
| 75 | |||
| 76 | // LEONIDA_RT_AS=khr builds #743's VkAccelerationStructureKHR instead, whose bytes are the driver's own. | ||
| 77 | char rt_as[8] = {}; | ||
| 78 | std::size_t rt_as_length = 0; | ||
| 79 | if (getenv_s(&rt_as_length, rt_as, "LEONIDA_RT_AS") == 0 && rt_as_length > 1 && std::strcmp(rt_as, "khr") == 0) { | ||
| 80 | std::puts("SKIP: LEONIDA_RT_AS=khr"); | ||
| 81 | return 77; | ||
| 82 | } | ||
| 83 | Ref<ID3D12Device5> device; | ||
| 84 | if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(device.put())))) { | ||
| 85 | std::puts("SKIP: no Vulkan D3D12 device"); | ||
| 86 | return 77; | ||
| 87 | } | ||
| 88 | |||
| 89 | // ---- geometry: a triangle soup, and an R16-indexed grid (stride 16) with a transform ---- | ||
| 90 | std::mt19937 rng(11); | ||
| 91 | std::uniform_real_distribution<float> spread(-4.0f, 4.0f), jitter(-0.6f, 0.6f); | ||
| 92 | const UINT soup_tris = 300; | ||
| 93 | std::vector<float> soup; | ||
| 94 | for (UINT t = 0; t < soup_tris; ++t) { | ||
| 95 | const float c[3] = {spread(rng), spread(rng), spread(rng)}; | ||
| 96 | for (int k = 0; k < 9; ++k) soup.push_back(c[k % 3] + jitter(rng)); | ||
| 97 | } | ||
| 98 | const UINT n = 8; | ||
| 99 | std::vector<float> grid; | ||
| 100 | for (UINT j = 0; j <= n; ++j) | ||
| 101 | for (UINT i = 0; i <= n; ++i) { | ||
| 102 | grid.push_back(float(i) - n * 0.5f); | ||
| 103 | grid.push_back(float(j) - n * 0.5f); | ||
| 104 | grid.push_back(0.25f * float((i * 7 + j * 3) % 5)); | ||
| 105 | grid.push_back(9.0f); // padding: the stride is 16 | ||
| 106 | } | ||
| 107 | std::vector<std::uint16_t> indices; | ||
| 108 | for (UINT j = 0; j < n; ++j) | ||
| 109 | for (UINT i = 0; i < n; ++i) { | ||
| 110 | const std::uint16_t a = std::uint16_t(j * (n + 1) + i); | ||
| 111 | const std::uint16_t q[6] = {a, std::uint16_t(a + 1), std::uint16_t(a + n + 2), a, std::uint16_t(a + n + 2), | ||
| 112 | std::uint16_t(a + n + 1)}; | ||
| 113 | indices.insert(indices.end(), q, q + 6); | ||
| 114 | } | ||
| 115 | const float transform[12] = {0, 2, 0, 1, 0, 0, 1, -2, 2, 0, 0, 3}; // exact in float | ||
| 116 | |||
| 117 | const UINT64 soup_bytes = soup.size() * 4, grid_bytes = grid.size() * 4, index_bytes = indices.size() * 2; | ||
| 118 | const UINT64 upload_size = 256 + soup_bytes + grid_bytes + index_bytes + 64 + 3 * 64; | ||
| 119 | Ref<ID3D12Resource> upload; | ||
| 120 | HRESULT hr = buffer(device.p, D3D12_HEAP_TYPE_UPLOAD, upload_size, D3D12_RESOURCE_STATE_GENERIC_READ, | ||
| 121 | D3D12_RESOURCE_FLAG_NONE, upload.put()); | ||
| 122 | if (FAILED(hr)) return fail("upload buffer", hr); | ||
| 123 | char* mapped = nullptr; | ||
| 124 | if (FAILED(upload->Map(0, nullptr, reinterpret_cast<void**>(&mapped)))) return fail("Map upload"); | ||
| 125 | UINT64 at = 0; | ||
| 126 | auto place = [&](const void* data, UINT64 bytes, UINT64 align) { | ||
| 127 | at = (at + align - 1) / align * align; | ||
| 128 | std::memcpy(mapped + at, data, std::size_t(bytes)); | ||
| 129 | const UINT64 va = upload->GetGPUVirtualAddress() + at; | ||
| 130 | at += bytes; | ||
| 131 | return va; | ||
| 132 | }; | ||
| 133 | const UINT64 soup_va = place(soup.data(), soup_bytes, 16); | ||
| 134 | const UINT64 grid_va = place(grid.data(), grid_bytes, 16); | ||
| 135 | const UINT64 index_va = place(indices.data(), index_bytes, 16); | ||
| 136 | const UINT64 transform_va = place(transform, sizeof transform, 16); | ||
| 137 | |||
| 138 | D3D12_RAYTRACING_GEOMETRY_DESC geometries[2]{}; | ||
| 139 | geometries[0].Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES; | ||
| 140 | geometries[0].Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE; | ||
| 141 | geometries[0].Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT; | ||
| 142 | geometries[0].Triangles.VertexCount = soup_tris * 3; | ||
| 143 | geometries[0].Triangles.VertexBuffer = {soup_va, 12}; | ||
| 144 | geometries[1].Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES; | ||
| 145 | geometries[1].Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE; | ||
| 146 | geometries[1].Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT; | ||
| 147 | geometries[1].Triangles.VertexCount = (n + 1) * (n + 1); | ||
| 148 | geometries[1].Triangles.VertexBuffer = {grid_va, 16}; | ||
| 149 | geometries[1].Triangles.IndexFormat = DXGI_FORMAT_R16_UINT; | ||
| 150 | geometries[1].Triangles.IndexCount = UINT(indices.size()); | ||
| 151 | geometries[1].Triangles.IndexBuffer = index_va; | ||
| 152 | geometries[1].Triangles.Transform3x4 = transform_va; | ||
| 153 | |||
| 154 | D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS blas_inputs{}; | ||
| 155 | blas_inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; | ||
| 156 | blas_inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY; | ||
| 157 | blas_inputs.NumDescs = 2; | ||
| 158 | blas_inputs.pGeometryDescs = geometries; | ||
| 159 | D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO blas_info{}; | ||
| 160 | device->GetRaytracingAccelerationStructurePrebuildInfo(&blas_inputs, &blas_info); | ||
| 161 | if (!blas_info.ResultDataMaxSizeInBytes) { | ||
| 162 | std::puts("SKIP: the device does not build XDXR acceleration structures (no bufferDeviceAddress)"); | ||
| 163 | return 77; | ||
| 164 | } | ||
| 165 | |||
| 166 | D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS tlas_inputs{}; | ||
| 167 | tlas_inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; | ||
| 168 | tlas_inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY; | ||
| 169 | tlas_inputs.NumDescs = 3; | ||
| 170 | D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO tlas_info{}; | ||
| 171 | device->GetRaytracingAccelerationStructurePrebuildInfo(&tlas_inputs, &tlas_info); | ||
| 172 | if (!tlas_info.ResultDataMaxSizeInBytes) return fail("TLAS prebuild info is empty"); | ||
| 173 | |||
| 174 | const D3D12_RESOURCE_FLAGS uav = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; | ||
| 175 | const D3D12_RESOURCE_STATES as_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE; | ||
| 176 | Ref<ID3D12Resource> blas, tlas, scratch, blas_rb, tlas_rb; | ||
| 177 | const UINT64 scratch_size = blas_info.ScratchDataSizeInBytes > tlas_info.ScratchDataSizeInBytes | ||
| 178 | ? blas_info.ScratchDataSizeInBytes : tlas_info.ScratchDataSizeInBytes; | ||
| 179 | if (FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_DEFAULT, blas_info.ResultDataMaxSizeInBytes, as_state, uav, blas.put())) || | ||
| 180 | FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_DEFAULT, tlas_info.ResultDataMaxSizeInBytes, as_state, uav, tlas.put())) || | ||
| 181 | FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_DEFAULT, scratch_size, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, uav, | ||
| 182 | scratch.put())) || | ||
| 183 | FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_READBACK, blas_info.ResultDataMaxSizeInBytes, | ||
| 184 | D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_FLAG_NONE, blas_rb.put())) || | ||
| 185 | FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_READBACK, tlas_info.ResultDataMaxSizeInBytes, | ||
| 186 | D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_FLAG_NONE, tlas_rb.put()))) | ||
| 187 | return fail("acceleration-structure buffers", hr); | ||
| 188 | if (blas->GetGPUVirtualAddress() % 256 || tlas->GetGPUVirtualAddress() % 256) | ||
| 189 | return fail("acceleration structures must be 256-byte aligned"); | ||
| 190 | |||
| 191 | // ---- instances: identity, rotated + translated, and masked out ---- | ||
| 192 | const float c = std::cos(0.7f), s = std::sin(0.7f); | ||
| 193 | const float xforms[3][12] = {{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}, | ||
| 194 | {c, -s, 0, 9, s, c, 0, -1, 0, 0, 1, 0.5f}, | ||
| 195 | {2, 0, 0, -9, 0, 0.5f, 0, 2, 0, 0, 1.5f, 1}}; | ||
| 196 | const UINT masks[3] = {0xff, 0x01, 0x00}; | ||
| 197 | D3D12_RAYTRACING_INSTANCE_DESC instances[3]{}; | ||
| 198 | for (int i = 0; i < 3; ++i) { | ||
| 199 | std::memcpy(instances[i].Transform, xforms[i], sizeof xforms[i]); | ||
| 200 | instances[i].InstanceID = 20 + i; | ||
| 201 | instances[i].InstanceMask = masks[i]; | ||
| 202 | instances[i].AccelerationStructure = blas->GetGPUVirtualAddress(); | ||
| 203 | } | ||
| 204 | const UINT64 instances_va = place(instances, sizeof instances, 16); | ||
| 205 | upload->Unmap(0, nullptr); | ||
| 206 | tlas_inputs.InstanceDescs = instances_va; | ||
| 207 | |||
| 208 | // ---- record: BLAS, barrier, TLAS, barrier, copies to readback ---- | ||
| 209 | D3D12_COMMAND_QUEUE_DESC qd{}; | ||
| 210 | qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; | ||
| 211 | Ref<ID3D12CommandQueue> queue; | ||
| 212 | Ref<ID3D12CommandAllocator> allocator; | ||
| 213 | Ref<ID3D12GraphicsCommandList4> list; | ||
| 214 | Ref<ID3D12Fence> fence; | ||
| 215 | if (FAILED(device->CreateCommandQueue(&qd, IID_PPV_ARGS(queue.put()))) || | ||
| 216 | FAILED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(allocator.put()))) || | ||
| 217 | FAILED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, | ||
| 218 | IID_PPV_ARGS(list.put()))) || | ||
| 219 | FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(fence.put())))) | ||
| 220 | return fail("queue, allocator, list or fence"); | ||
| 221 | D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC build{}; | ||
| 222 | build.Inputs = blas_inputs; | ||
| 223 | build.DestAccelerationStructureData = blas->GetGPUVirtualAddress(); | ||
| 224 | build.ScratchAccelerationStructureData = scratch->GetGPUVirtualAddress(); | ||
| 225 | list->BuildRaytracingAccelerationStructure(&build, 0, nullptr); | ||
| 226 | D3D12_RESOURCE_BARRIER barrier{}; | ||
| 227 | barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; | ||
| 228 | barrier.UAV.pResource = nullptr; // every UAV: the BLAS and the scratch | ||
| 229 | list->ResourceBarrier(1, &barrier); | ||
| 230 | build.Inputs = tlas_inputs; | ||
| 231 | build.DestAccelerationStructureData = tlas->GetGPUVirtualAddress(); | ||
| 232 | list->BuildRaytracingAccelerationStructure(&build, 0, nullptr); | ||
| 233 | list->ResourceBarrier(1, &barrier); | ||
| 234 | // The layer keeps buffers in one memory, so a copy out of an acceleration structure reads its bytes as they are | ||
| 235 | // (D3D12 itself only allows CopyRaytracingAccelerationStructure; this is a test of the layer's records). | ||
| 236 | list->CopyBufferRegion(blas_rb.p, 0, blas.p, 0, blas_info.ResultDataMaxSizeInBytes); | ||
| 237 | list->CopyBufferRegion(tlas_rb.p, 0, tlas.p, 0, tlas_info.ResultDataMaxSizeInBytes); | ||
| 238 | if (FAILED(hr = list->Close())) return fail("Close", hr); | ||
| 239 | ID3D12CommandList* lists[] = {list.p}; | ||
| 240 | queue->ExecuteCommandLists(1, lists); | ||
| 241 | if (FAILED(queue->Signal(fence.p, 1))) return fail("Signal"); | ||
| 242 | HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 243 | const bool done = event && SUCCEEDED(fence->SetEventOnCompletion(1, event)) && | ||
| 244 | WaitForSingleObject(event, 10000) == WAIT_OBJECT_0; | ||
| 245 | if (event) CloseHandle(event); | ||
| 246 | if (!done) return fail("the builds did not complete"); | ||
| 247 | if (FAILED(device->GetDeviceRemovedReason())) return fail("device removed", device->GetDeviceRemovedReason()); | ||
| 248 | |||
| 249 | // ---- dump ---- | ||
| 250 | auto dump = [&](ID3D12Resource* rb, UINT64 size, const std::string& name, const std::string& json) { | ||
| 251 | void* p = nullptr; | ||
| 252 | if (FAILED(rb->Map(0, nullptr, &p))) return false; | ||
| 253 | std::uint32_t units = 0; | ||
| 254 | std::memcpy(&units, static_cast<char*>(p) + 4, 4); // header totalSizeIn64BNodes | ||
| 255 | const UINT64 used = UINT64(units) * 64; | ||
| 256 | const bool ok = used && used <= size && write_file(dir + "/" + name + ".bin", p, std::size_t(used)) && | ||
| 257 | write_file(dir + "/" + name + ".json", json.data(), json.size()); | ||
| 258 | rb->Unmap(0, nullptr); | ||
| 259 | return ok; | ||
| 260 | }; | ||
| 261 | std::string tris = "["; | ||
| 262 | bool first = true; | ||
| 263 | auto add_tri = [&](const float v[3][3], int geometry, UINT primitive) { | ||
| 264 | tris += first ? "\n [" : ",\n ["; | ||
| 265 | first = false; | ||
| 266 | for (int k = 0; k < 3; ++k) | ||
| 267 | tris += (k ? ", [" : "[") + num(v[k][0]) + ", " + num(v[k][1]) + ", " + num(v[k][2]) + "]"; | ||
| 268 | tris += ", " + std::to_string(geometry) + ", " + std::to_string(primitive) + "]"; | ||
| 269 | }; | ||
| 270 | for (UINT t = 0; t < soup_tris; ++t) { | ||
| 271 | float v[3][3]; | ||
| 272 | for (int k = 0; k < 9; ++k) v[k / 3][k % 3] = soup[9 * t + k]; | ||
| 273 | add_tri(v, 0, t); | ||
| 274 | } | ||
| 275 | for (UINT t = 0; t < indices.size() / 3; ++t) { | ||
| 276 | float v[3][3]; | ||
| 277 | for (int k = 0; k < 3; ++k) { | ||
| 278 | const float* p = &grid[4 * indices[3 * t + k]]; | ||
| 279 | for (int r = 0; r < 3; ++r) | ||
| 280 | v[k][r] = transform[4 * r] * p[0] + transform[4 * r + 1] * p[1] + transform[4 * r + 2] * p[2] + | ||
| 281 | transform[4 * r + 3]; | ||
| 282 | } | ||
| 283 | add_tri(v, 1, t); | ||
| 284 | } | ||
| 285 | tris += "]"; | ||
| 286 | if (!dump(blas_rb.p, blas_info.ResultDataMaxSizeInBytes, "d3d12_blas", | ||
| 287 | "{\"kind\": \"blas\", \"address\": " + std::to_string(blas->GetGPUVirtualAddress()) + | ||
| 288 | ", \"triangles\": " + tris + "}\n")) | ||
| 289 | return fail("BLAS readback"); | ||
| 290 | std::string inst = "["; | ||
| 291 | for (int i = 0; i < 3; ++i) { | ||
| 292 | inst += i ? ", {\"transform\": [" : "{\"transform\": ["; | ||
| 293 | for (int k = 0; k < 12; ++k) inst += (k ? ", " : "") + num(xforms[i][k]); | ||
| 294 | inst += "], \"mask\": " + std::to_string(masks[i]) + ", \"blas\": 0}"; | ||
| 295 | } | ||
| 296 | inst += "]"; | ||
| 297 | if (!dump(tlas_rb.p, tlas_info.ResultDataMaxSizeInBytes, "d3d12_tlas", | ||
| 298 | "{\"kind\": \"tlas\", \"address\": " + std::to_string(tlas->GetGPUVirtualAddress()) + | ||
| 299 | ", \"bias\": 0, \"blas\": [\"d3d12_blas\"], \"instances\": " + inst + "}\n")) | ||
| 300 | return fail("TLAS readback"); | ||
| 301 | std::printf("d3d12_xdxr_rt: BLAS %llu bytes (prebuild %llu), TLAS (prebuild %llu), written to %s\n", | ||
| 302 | static_cast<unsigned long long>(blas_info.ResultDataMaxSizeInBytes), | ||
| 303 | static_cast<unsigned long long>(blas_info.ResultDataMaxSizeInBytes), | ||
| 304 | static_cast<unsigned long long>(tlas_info.ResultDataMaxSizeInBytes), dir.c_str()); | ||
| 305 | return 0; | ||
| 306 | } | ||