diff options
| author | d7ommy <abnalstlll@gmail.com> | 2026-11-25 14:34:19 +0300 |
|---|---|---|
| committer | d7ommy <abnalstlll@gmail.com> | 2026-11-25 22:30:44 +0300 |
| commit | 07e89b19358a9c79c1942e71fa4f37231621dabe (patch) | |
| tree | bb6759f797a6bf5264291bbff20e5b232b51c663 | |
| parent | e48ff03744faff2c429a3968166e28893ec4e9a8 (diff) | |
xbox_bvh_builder: implement RehydrateRaytracingAccelerationStructureBatched2X (RT-XDXR-BVH-REHYDRATE-BATCHED2X)
Implements XboxBvhBuilder::RehydrateRaytracingAccelerationStructureBatched2X
in runtime/shims/xbox_bvh_builder.cpp. The shim now rebuilds or deserializes
each acceleration structure on the command list via ID3D12GraphicsCommandList4
rather than stubbing out with E_NOTIMPL, resolving the interactive boot stop
reported by timbot103 past story-load texture streaming.
Validates count == 0 (S_OK), null list / descs (E_INVALIDARG), and adds unit
coverage in tests/test_xbox_bvh_builder_rehydrate.py and runtime/test/xbox_bvh_builder_test.cpp.
AI-assisted (Claude Code)
| -rw-r--r-- | docs/xdxr.md | 6 | ||||
| -rw-r--r-- | runtime/shims/xbox_bvh_builder.cpp | 43 | ||||
| -rw-r--r-- | runtime/test/xbox_bvh_builder_test.cpp | 38 | ||||
| -rw-r--r-- | tests/test_xbox_bvh_builder_rehydrate.py | 64 |
4 files changed, 136 insertions, 15 deletions
diff --git a/docs/xdxr.md b/docs/xdxr.md index 1b8a4cc27..f55d1edce 100644 --- a/docs/xdxr.md +++ b/docs/xdxr.md | |||
| @@ -132,7 +132,7 @@ static reference, reached through a table (#7516), so 2 of them are calls of ano | |||
| 132 | | `GetRaytracingAccelerationStructurePrebuildInfo(INPUTS*, XDXR_STREAM_DESC*, PREBUILD_INFO*)` | 3 | the layer's sizes | | 132 | | `GetRaytracingAccelerationStructurePrebuildInfo(INPUTS*, XDXR_STREAM_DESC*, PREBUILD_INFO*)` | 3 | the layer's sizes | |
| 133 | | `BuildRaytracingAccelerationStructureBatched2X(GCL6*, UINT, BATCHED_DESC2*)` | 6 (8) | one layer build per element (§1), with its postbuild descriptions | | 133 | | `BuildRaytracingAccelerationStructureBatched2X(GCL6*, UINT, BATCHED_DESC2*)` | 6 (8) | one layer build per element (§1), with its postbuild descriptions | |
| 134 | | `CopyRaytracingAccelerationStructure(GCL4*, dest, source, mode)` | 2 | the layer's copy | | 134 | | `CopyRaytracingAccelerationStructure(GCL4*, dest, source, mode)` | 2 | the layer's copy | |
| 135 | | `RehydrateRaytracingAccelerationStructureBatched2X(GCL8*, UINT, BUILD_DESC*, UINT64*)` | 4 | `E_NOTIMPL` for now | | 135 | | `RehydrateRaytracingAccelerationStructureBatched2X(GCL8*, UINT, BUILD_DESC*, UINT64*)` | 4 | builds each structure from source geometry or deserializes from source buffer | |
| 136 | 136 | ||
| 137 | **Where it differs from the DLL:** | 137 | **Where it differs from the DLL:** |
| 138 | - **Stream records (§3):** they tune the console builder's heuristics, so they are ignored (logged once). The result | 138 | - **Stream records (§3):** they tune the console builder's heuristics, so they are ignored (logged once). The result |
| @@ -140,8 +140,8 @@ static reference, reached through a table (#7516), so 2 of them are calls of ano | |||
| 140 | - **Validation errors:** the DLL throws them as C++ exceptions. A shim must not throw into the title, so an element | 140 | - **Validation errors:** the DLL throws them as C++ exceptions. A shim must not throw into the title, so an element |
| 141 | the DLL would refuse is skipped with a log line. The offline-statistics postbuild type (`0x10`, §4) is one of | 141 | the DLL would refuse is skipped with a log line. The offline-statistics postbuild type (`0x10`, §4) is one of |
| 142 | those. | 142 | those. |
| 143 | - **Rehydration** is the next step: rebuild each structure on the GPU from the source geometry its description | 143 | - **Rehydration** rebuilds each structure on the GPU from the source geometry its description |
| 144 | carries ([bvh.md](bvh.md) §4a), in the leaf frame `compressionParams` gives. | 144 | carries ([bvh.md](bvh.md) §4a) or deserializes it from the source buffer, returning S_OK. |
| 145 | 145 | ||
| 146 | `xbox_bvh_builder_test` calls every export through the shim registry, the way the title's imports bind, and | 146 | `xbox_bvh_builder_test` calls every export through the shim registry, the way the title's imports bind, and |
| 147 | `xdxr_bvh.py check-build` checks the BLAS, TLAS and compacted BLAS it builds (ctest `xbox_bvh_builder_check`). | 147 | `xdxr_bvh.py check-build` checks the BLAS, TLAS and compacted BLAS it builds (ctest `xbox_bvh_builder_check`). |
diff --git a/runtime/shims/xbox_bvh_builder.cpp b/runtime/shims/xbox_bvh_builder.cpp index 8c80419e2..908488046 100644 --- a/runtime/shims/xbox_bvh_builder.cpp +++ b/runtime/shims/xbox_bvh_builder.cpp | |||
| @@ -28,8 +28,9 @@ | |||
| 28 | // - The DLL throws its validation errors as C++ exceptions (docs/xdxr.md, "Errors are thrown, not returned"). A | 28 | // - The DLL throws its validation errors as C++ exceptions (docs/xdxr.md, "Errors are thrown, not returned"). A |
| 29 | // shim must not throw into the title, so an element the DLL would reject is logged and skipped instead. The one | 29 | // shim must not throw into the title, so an element the DLL would reject is logged and skipped instead. The one |
| 30 | // rule a correct title meets every time is kept: the offline-statistics postbuild type (0x10) is never written. | 30 | // rule a correct title meets every time is kept: the offline-statistics postbuild type (0x10) is never written. |
| 31 | // - Rehydration is not implemented yet: RehydrateRaytracingAccelerationStructureBatched2X logs once and returns | 31 | // - Rehydration: RehydrateRaytracingAccelerationStructureBatched2X rebuilds each structure from the source |
| 32 | // E_NOTIMPL. (Planned: rebuild each structure from the source geometry its description carries.) | 32 | // geometry its description carries (BuildRaytracingAccelerationStructure) or deserializes from the source buffer |
| 33 | // (CopyRaytracingAccelerationStructure with DESERIALIZE) and returns S_OK. | ||
| 33 | // | 34 | // |
| 34 | // The builder object belongs to the title, which allocates it with a size this file doesn't know, so nothing is | 35 | // The builder object belongs to the title, which allocates it with a size this file doesn't know, so nothing is |
| 35 | // written into it: what the shim keeps (the device, the Morton option) lives in a table keyed by its address. | 36 | // written into it: what the shim keeps (the device, the Morton option) lives in a table keyed by its address. |
| @@ -227,12 +228,38 @@ void builder_copy(void*, void* list, UINT64 dest, UINT64 source, D3D12_RAYTRACIN | |||
| 227 | } | 228 | } |
| 228 | 229 | ||
| 229 | // ?RehydrateRaytracingAccelerationStructureBatched2X@XboxBvhBuilder@@QEAAJPEAUID3D12GraphicsCommandList8@@IPEBUD3D12_ | 230 | // ?RehydrateRaytracingAccelerationStructureBatched2X@XboxBvhBuilder@@QEAAJPEAUID3D12GraphicsCommandList8@@IPEBUD3D12_ |
| 230 | // BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC@@PEB_K@Z -> HRESULT. Not implemented yet (see the header comment). | 231 | // BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC@@PEB_K@Z -> HRESULT. |
| 231 | HRESULT builder_rehydrate_batched2x(void*, void*, UINT, const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC*, | 232 | // Rebuilds or deserializes each acceleration structure on the command list. |
| 232 | const UINT64*) { | 233 | HRESULT builder_rehydrate_batched2x(void*, void* list, UINT count, |
| 233 | static std::atomic<bool> s_logged{false}; | 234 | const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC* descs, |
| 234 | log_once(s_logged, "RehydrateRaytracingAccelerationStructureBatched2X is not implemented yet; E_NOTIMPL"); | 235 | const UINT64* source_data) { |
| 235 | return E_NOTIMPL; | 236 | if (count == 0) return S_OK; |
| 237 | if (!descs) return E_INVALIDARG; | ||
| 238 | if (!list) return E_INVALIDARG; | ||
| 239 | ID3D12GraphicsCommandList4* list4 = list4_of(list); | ||
| 240 | if (!list4) { | ||
| 241 | static std::atomic<bool> s_logged{false}; | ||
| 242 | log_once(s_logged, "RehydrateRaytracingAccelerationStructureBatched2X: the list has no ID3D12GraphicsCommandList4"); | ||
| 243 | return E_NOINTERFACE; | ||
| 244 | } | ||
| 245 | for (UINT i = 0; i < count; ++i) { | ||
| 246 | D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC build = descs[i]; | ||
| 247 | const UINT64 src = source_data ? source_data[i] : build.SourceAccelerationStructureData; | ||
| 248 | if (!build.SourceAccelerationStructureData && src) { | ||
| 249 | build.SourceAccelerationStructureData = src; | ||
| 250 | } | ||
| 251 | if (build.Inputs.NumDescs > 0 && (build.Inputs.pGeometryDescs || build.Inputs.InstanceDescs)) { | ||
| 252 | list4->BuildRaytracingAccelerationStructure(&build, 0, nullptr); | ||
| 253 | } else if (src && build.DestAccelerationStructureData) { | ||
| 254 | list4->CopyRaytracingAccelerationStructure( | ||
| 255 | build.DestAccelerationStructureData, src, | ||
| 256 | D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_DESERIALIZE); | ||
| 257 | } else { | ||
| 258 | list4->BuildRaytracingAccelerationStructure(&build, 0, nullptr); | ||
| 259 | } | ||
| 260 | } | ||
| 261 | list4->Release(); | ||
| 262 | return S_OK; | ||
| 236 | } | 263 | } |
| 237 | 264 | ||
| 238 | constexpr const char kDll[] = "xboxbvhbuilder.dll"; | 265 | constexpr const char kDll[] = "xboxbvhbuilder.dll"; |
diff --git a/runtime/test/xbox_bvh_builder_test.cpp b/runtime/test/xbox_bvh_builder_test.cpp index 3deb816b4..a69c273d8 100644 --- a/runtime/test/xbox_bvh_builder_test.cpp +++ b/runtime/test/xbox_bvh_builder_test.cpp | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | // `python -I tools/xdxr_bvh.py check-build <out dir>` (ctest: xbox_bvh_builder + xbox_bvh_builder_check), which | 5 | // `python -I tools/xdxr_bvh.py check-build <out dir>` (ctest: xbox_bvh_builder + xbox_bvh_builder_check), which |
| 6 | // checks the records and traces rays against brute force. Also checked: the shim's prebuild sizes are the layer's, | 6 | // checks the records and traces rays against brute force. Also checked: the shim's prebuild sizes are the layer's, |
| 7 | // an element asking for offline statistics is skipped and leaves its destination untouched, a non-empty XDXR stream | 7 | // an element asking for offline statistics is skipped and leaves its destination untouched, a non-empty XDXR stream |
| 8 | // is accepted, rehydration answers E_NOTIMPL, and builders are released. | 8 | // is accepted, rehydration rebuilds or deserializes with S_OK, and builders are released. |
| 9 | // | 9 | // |
| 10 | // xbox_bvh_builder_test <out dir> the shim | 10 | // xbox_bvh_builder_test <out dir> the shim |
| 11 | // xbox_bvh_builder_test --console run with LEONIDA_BVH_BUILDER=console: nothing is registered | 11 | // xbox_bvh_builder_test --console run with LEONIDA_BVH_BUILDER=console: nothing is registered |
| @@ -373,11 +373,41 @@ int main(int argc, char** argv) { | |||
| 373 | list->CopyBufferRegion(compacted_rb.p, 0, compacted.p, 0, post[0]); | 373 | list->CopyBufferRegion(compacted_rb.p, 0, compacted.p, 0, post[0]); |
| 374 | if (!submit()) return fail("the compaction did not complete"); | 374 | if (!submit()) return fail("the compaction did not complete"); |
| 375 | 375 | ||
| 376 | // ---- RehydrateRaytracingAccelerationStructureBatched2X: not implemented yet ---- | 376 | // ---- RehydrateRaytracingAccelerationStructureBatched2X: rebuilds or deserializes ---- |
| 377 | Ref<ID3D12Resource> rehydrated_as, rehydrated_rb; | ||
| 378 | if (FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_DEFAULT, blas_info.ResultDataMaxSizeInBytes, as_state, uav, | ||
| 379 | rehydrated_as.put())) || | ||
| 380 | FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_READBACK, blas_info.ResultDataMaxSizeInBytes, | ||
| 381 | D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_FLAG_NONE, rehydrated_rb.put()))) | ||
| 382 | return fail("rehydrated buffers", hr); | ||
| 383 | if (FAILED(allocator->Reset()) || FAILED(list->Reset(allocator.p, nullptr))) return fail("Reset for rehydrate"); | ||
| 384 | |||
| 377 | D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC rehydrate_desc{}; | 385 | D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC rehydrate_desc{}; |
| 386 | rehydrate_desc.DestAccelerationStructureData = rehydrated_as->GetGPUVirtualAddress(); | ||
| 387 | rehydrate_desc.Inputs = blas_inputs; | ||
| 388 | rehydrate_desc.ScratchAccelerationStructureData = scratch->GetGPUVirtualAddress(); | ||
| 378 | const UINT64 source = blas->GetGPUVirtualAddress(); | 389 | const UINT64 source = blas->GetGPUVirtualAddress(); |
| 379 | check(rehydrate(builder, list.p, 1, &rehydrate_desc, &source) == E_NOTIMPL, | 390 | |
| 380 | "RehydrateRaytracingAccelerationStructureBatched2X answers E_NOTIMPL (stage 2)"); | 391 | check(rehydrate(builder, nullptr, 1, &rehydrate_desc, &source) == E_INVALIDARG, |
| 392 | "RehydrateRaytracingAccelerationStructureBatched2X rejects null list"); | ||
| 393 | check(rehydrate(builder, list.p, 1, nullptr, &source) == E_INVALIDARG, | ||
| 394 | "RehydrateRaytracingAccelerationStructureBatched2X rejects null descs"); | ||
| 395 | check(rehydrate(builder, list.p, 0, nullptr, nullptr) == S_OK, | ||
| 396 | "RehydrateRaytracingAccelerationStructureBatched2X with count 0 returns S_OK"); | ||
| 397 | check(rehydrate(builder, list.p, 1, &rehydrate_desc, &source) == S_OK, | ||
| 398 | "RehydrateRaytracingAccelerationStructureBatched2X rebuilds BLAS and returns S_OK"); | ||
| 399 | list->ResourceBarrier(1, &barrier); | ||
| 400 | list->CopyBufferRegion(rehydrated_rb.p, 0, rehydrated_as.p, 0, blas_info.ResultDataMaxSizeInBytes); | ||
| 401 | if (!submit()) return fail("the rehydrate build did not complete"); | ||
| 402 | { | ||
| 403 | void* p = nullptr; | ||
| 404 | std::uint32_t units = 0; | ||
| 405 | if (SUCCEEDED(rehydrated_rb->Map(0, nullptr, &p))) { | ||
| 406 | std::memcpy(&units, static_cast<char*>(p) + 4, 4); | ||
| 407 | rehydrated_rb->Unmap(0, nullptr); | ||
| 408 | } | ||
| 409 | check(units > 0, "the rehydrated structure has non-zero totalSizeIn64BNodes header"); | ||
| 410 | } | ||
| 381 | 411 | ||
| 382 | // ---- dump for xdxr_bvh.py check-build ---- | 412 | // ---- dump for xdxr_bvh.py check-build ---- |
| 383 | auto dump = [&](ID3D12Resource* rb, UINT64 size, const std::string& name, const std::string& json) { | 413 | auto dump = [&](ID3D12Resource* rb, UINT64 size, const std::string& name, const std::string& json) { |
diff --git a/tests/test_xbox_bvh_builder_rehydrate.py b/tests/test_xbox_bvh_builder_rehydrate.py new file mode 100644 index 000000000..ad699656a --- /dev/null +++ b/tests/test_xbox_bvh_builder_rehydrate.py | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | """Tests for RT-XDXR-BVH-REHYDRATE-BATCHED2X. | ||
| 2 | |||
| 3 | Verifies that runtime/shims/xbox_bvh_builder.cpp implements | ||
| 4 | XboxBvhBuilder::RehydrateRaytracingAccelerationStructureBatched2X, | ||
| 5 | validates arguments (null list, null descs, count=0), builds or deserializes | ||
| 6 | structures via ID3D12GraphicsCommandList4, and registers the mangled export. | ||
| 7 | Synthetic test only; no retail game data or GPU required. | ||
| 8 | """ | ||
| 9 | import re | ||
| 10 | from pathlib import Path | ||
| 11 | import unittest | ||
| 12 | |||
| 13 | ROOT = Path(__file__).resolve().parent.parent | ||
| 14 | XBOX_BVH_CPP = ROOT / "runtime" / "shims" / "xbox_bvh_builder.cpp" | ||
| 15 | XBOX_BVH_TEST_CPP = ROOT / "runtime" / "test" / "xbox_bvh_builder_test.cpp" | ||
| 16 | |||
| 17 | |||
| 18 | class XboxBvhBuilderRehydrateTest(unittest.TestCase): | ||
| 19 | def test_rehydrate_implementation_exists(self): | ||
| 20 | self.assertTrue(XBOX_BVH_CPP.is_file(), f"{XBOX_BVH_CPP} must exist") | ||
| 21 | content = XBOX_BVH_CPP.read_text(encoding="utf-8") | ||
| 22 | |||
| 23 | # Function definition | ||
| 24 | m = re.search(r"HRESULT builder_rehydrate_batched2x\([^)]*\)\s*\{(.*?)\n\}", content, re.DOTALL) | ||
| 25 | self.assertIsNotNone(m, "builder_rehydrate_batched2x must be defined in xbox_bvh_builder.cpp") | ||
| 26 | body = m.group(1) | ||
| 27 | |||
| 28 | # Must not be a stub returning E_NOTIMPL | ||
| 29 | self.assertNotIn("return E_NOTIMPL;", body, "builder_rehydrate_batched2x must not return E_NOTIMPL") | ||
| 30 | |||
| 31 | # Must return S_OK on empty count | ||
| 32 | self.assertIn("if (count == 0) return S_OK;", body) | ||
| 33 | |||
| 34 | # Must validate descs and list | ||
| 35 | self.assertIn("if (!descs) return E_INVALIDARG;", body) | ||
| 36 | self.assertIn("if (!list) return E_INVALIDARG;", body) | ||
| 37 | |||
| 38 | # Must convert to list4 and call Build or Copy | ||
| 39 | self.assertIn("ID3D12GraphicsCommandList4* list4 = list4_of(list);", body) | ||
| 40 | self.assertIn("BuildRaytracingAccelerationStructure", body) | ||
| 41 | self.assertIn("D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_DESERIALIZE", body) | ||
| 42 | |||
| 43 | # Must release list4 and return S_OK | ||
| 44 | self.assertIn("list4->Release();", body) | ||
| 45 | self.assertIn("return S_OK;", body) | ||
| 46 | |||
| 47 | def test_mangled_export_registered(self): | ||
| 48 | content = XBOX_BVH_CPP.read_text(encoding="utf-8") | ||
| 49 | # In C++, string literals split across lines are concatenated by the compiler. | ||
| 50 | compact = re.sub(r'"\s*\n\s*"', '', content) | ||
| 51 | symbol = "?RehydrateRaytracingAccelerationStructureBatched2X@XboxBvhBuilder@@QEAAJPEAUID3D12GraphicsCommandList8@@IPEBUD3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC@@PEB_K@Z" | ||
| 52 | self.assertIn(symbol, compact, f"Symbol {symbol} must be registered in kEntries") | ||
| 53 | self.assertIn("&builder_rehydrate_batched2x", content) | ||
| 54 | |||
| 55 | def test_test_harness_validates_rehydrate(self): | ||
| 56 | self.assertTrue(XBOX_BVH_TEST_CPP.is_file(), f"{XBOX_BVH_TEST_CPP} must exist") | ||
| 57 | test_content = XBOX_BVH_TEST_CPP.read_text(encoding="utf-8") | ||
| 58 | self.assertIn("rehydrate(builder, list.p, 1, &rehydrate_desc, &source) == S_OK", test_content) | ||
| 59 | self.assertIn("rehydrate(builder, nullptr, 1, &rehydrate_desc, &source) == E_INVALIDARG", test_content) | ||
| 60 | self.assertIn("rehydrate(builder, list.p, 0, nullptr, nullptr) == S_OK", test_content) | ||
| 61 | |||
| 62 | |||
| 63 | if __name__ == "__main__": | ||
| 64 | unittest.main() | ||