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 | `GetRaytracingAccelerationStructurePrebuildInfo(INPUTS*, XDXR_STREAM_DESC*, PREBUILD_INFO*)` | 3 | the layer's sizes | | `BuildRaytracingAccelerationStructureBatched2X(GCL6*, UINT, BATCHED_DESC2*)` | 6 (8) | one layer build per element (§1), with its postbuild descriptions | | `CopyRaytracingAccelerationStructure(GCL4*, dest, source, mode)` | 2 | the layer's copy | -| `RehydrateRaytracingAccelerationStructureBatched2X(GCL8*, UINT, BUILD_DESC*, UINT64*)` | 4 | `E_NOTIMPL` for now | +| `RehydrateRaytracingAccelerationStructureBatched2X(GCL8*, UINT, BUILD_DESC*, UINT64*)` | 4 | builds each structure from source geometry or deserializes from source buffer | **Where it differs from the DLL:** - **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 - **Validation errors:** the DLL throws them as C++ exceptions. A shim must not throw into the title, so an element the DLL would refuse is skipped with a log line. The offline-statistics postbuild type (`0x10`, §4) is one of those. -- **Rehydration** is the next step: rebuild each structure on the GPU from the source geometry its description - carries ([bvh.md](bvh.md) §4a), in the leaf frame `compressionParams` gives. +- **Rehydration** rebuilds each structure on the GPU from the source geometry its description + carries ([bvh.md](bvh.md) §4a) or deserializes it from the source buffer, returning S_OK. `xbox_bvh_builder_test` calls every export through the shim registry, the way the title's imports bind, and `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 @@ // - The DLL throws its validation errors as C++ exceptions (docs/xdxr.md, "Errors are thrown, not returned"). A // shim must not throw into the title, so an element the DLL would reject is logged and skipped instead. The one // rule a correct title meets every time is kept: the offline-statistics postbuild type (0x10) is never written. -// - Rehydration is not implemented yet: RehydrateRaytracingAccelerationStructureBatched2X logs once and returns -// E_NOTIMPL. (Planned: rebuild each structure from the source geometry its description carries.) +// - Rehydration: RehydrateRaytracingAccelerationStructureBatched2X rebuilds each structure from the source +// geometry its description carries (BuildRaytracingAccelerationStructure) or deserializes from the source buffer +// (CopyRaytracingAccelerationStructure with DESERIALIZE) and returns S_OK. // // The builder object belongs to the title, which allocates it with a size this file doesn't know, so nothing is // 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 } // ?RehydrateRaytracingAccelerationStructureBatched2X@XboxBvhBuilder@@QEAAJPEAUID3D12GraphicsCommandList8@@IPEBUD3D12_ -// BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC@@PEB_K@Z -> HRESULT. Not implemented yet (see the header comment). -HRESULT builder_rehydrate_batched2x(void*, void*, UINT, const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC*, - const UINT64*) { - static std::atomic s_logged{false}; - log_once(s_logged, "RehydrateRaytracingAccelerationStructureBatched2X is not implemented yet; E_NOTIMPL"); - return E_NOTIMPL; +// BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC@@PEB_K@Z -> HRESULT. +// Rebuilds or deserializes each acceleration structure on the command list. +HRESULT builder_rehydrate_batched2x(void*, void* list, UINT count, + const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC* descs, + const UINT64* source_data) { + if (count == 0) return S_OK; + if (!descs) return E_INVALIDARG; + if (!list) return E_INVALIDARG; + ID3D12GraphicsCommandList4* list4 = list4_of(list); + if (!list4) { + static std::atomic s_logged{false}; + log_once(s_logged, "RehydrateRaytracingAccelerationStructureBatched2X: the list has no ID3D12GraphicsCommandList4"); + return E_NOINTERFACE; + } + for (UINT i = 0; i < count; ++i) { + D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC build = descs[i]; + const UINT64 src = source_data ? source_data[i] : build.SourceAccelerationStructureData; + if (!build.SourceAccelerationStructureData && src) { + build.SourceAccelerationStructureData = src; + } + if (build.Inputs.NumDescs > 0 && (build.Inputs.pGeometryDescs || build.Inputs.InstanceDescs)) { + list4->BuildRaytracingAccelerationStructure(&build, 0, nullptr); + } else if (src && build.DestAccelerationStructureData) { + list4->CopyRaytracingAccelerationStructure( + build.DestAccelerationStructureData, src, + D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_DESERIALIZE); + } else { + list4->BuildRaytracingAccelerationStructure(&build, 0, nullptr); + } + } + list4->Release(); + return S_OK; } 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 @@ // `python -I tools/xdxr_bvh.py check-build ` (ctest: xbox_bvh_builder + xbox_bvh_builder_check), which // checks the records and traces rays against brute force. Also checked: the shim's prebuild sizes are the layer's, // an element asking for offline statistics is skipped and leaves its destination untouched, a non-empty XDXR stream -// is accepted, rehydration answers E_NOTIMPL, and builders are released. +// is accepted, rehydration rebuilds or deserializes with S_OK, and builders are released. // // xbox_bvh_builder_test the shim // xbox_bvh_builder_test --console run with LEONIDA_BVH_BUILDER=console: nothing is registered @@ -373,11 +373,41 @@ int main(int argc, char** argv) { list->CopyBufferRegion(compacted_rb.p, 0, compacted.p, 0, post[0]); if (!submit()) return fail("the compaction did not complete"); - // ---- RehydrateRaytracingAccelerationStructureBatched2X: not implemented yet ---- + // ---- RehydrateRaytracingAccelerationStructureBatched2X: rebuilds or deserializes ---- + Ref rehydrated_as, rehydrated_rb; + if (FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_DEFAULT, blas_info.ResultDataMaxSizeInBytes, as_state, uav, + rehydrated_as.put())) || + FAILED(hr = buffer(device.p, D3D12_HEAP_TYPE_READBACK, blas_info.ResultDataMaxSizeInBytes, + D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_FLAG_NONE, rehydrated_rb.put()))) + return fail("rehydrated buffers", hr); + if (FAILED(allocator->Reset()) || FAILED(list->Reset(allocator.p, nullptr))) return fail("Reset for rehydrate"); + D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC rehydrate_desc{}; + rehydrate_desc.DestAccelerationStructureData = rehydrated_as->GetGPUVirtualAddress(); + rehydrate_desc.Inputs = blas_inputs; + rehydrate_desc.ScratchAccelerationStructureData = scratch->GetGPUVirtualAddress(); const UINT64 source = blas->GetGPUVirtualAddress(); - check(rehydrate(builder, list.p, 1, &rehydrate_desc, &source) == E_NOTIMPL, - "RehydrateRaytracingAccelerationStructureBatched2X answers E_NOTIMPL (stage 2)"); + + check(rehydrate(builder, nullptr, 1, &rehydrate_desc, &source) == E_INVALIDARG, + "RehydrateRaytracingAccelerationStructureBatched2X rejects null list"); + check(rehydrate(builder, list.p, 1, nullptr, &source) == E_INVALIDARG, + "RehydrateRaytracingAccelerationStructureBatched2X rejects null descs"); + check(rehydrate(builder, list.p, 0, nullptr, nullptr) == S_OK, + "RehydrateRaytracingAccelerationStructureBatched2X with count 0 returns S_OK"); + check(rehydrate(builder, list.p, 1, &rehydrate_desc, &source) == S_OK, + "RehydrateRaytracingAccelerationStructureBatched2X rebuilds BLAS and returns S_OK"); + list->ResourceBarrier(1, &barrier); + list->CopyBufferRegion(rehydrated_rb.p, 0, rehydrated_as.p, 0, blas_info.ResultDataMaxSizeInBytes); + if (!submit()) return fail("the rehydrate build did not complete"); + { + void* p = nullptr; + std::uint32_t units = 0; + if (SUCCEEDED(rehydrated_rb->Map(0, nullptr, &p))) { + std::memcpy(&units, static_cast(p) + 4, 4); + rehydrated_rb->Unmap(0, nullptr); + } + check(units > 0, "the rehydrated structure has non-zero totalSizeIn64BNodes header"); + } // ---- dump for xdxr_bvh.py check-build ---- 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 @@ +"""Tests for RT-XDXR-BVH-REHYDRATE-BATCHED2X. + +Verifies that runtime/shims/xbox_bvh_builder.cpp implements +XboxBvhBuilder::RehydrateRaytracingAccelerationStructureBatched2X, +validates arguments (null list, null descs, count=0), builds or deserializes +structures via ID3D12GraphicsCommandList4, and registers the mangled export. +Synthetic test only; no retail game data or GPU required. +""" +import re +from pathlib import Path +import unittest + +ROOT = Path(__file__).resolve().parent.parent +XBOX_BVH_CPP = ROOT / "runtime" / "shims" / "xbox_bvh_builder.cpp" +XBOX_BVH_TEST_CPP = ROOT / "runtime" / "test" / "xbox_bvh_builder_test.cpp" + + +class XboxBvhBuilderRehydrateTest(unittest.TestCase): + def test_rehydrate_implementation_exists(self): + self.assertTrue(XBOX_BVH_CPP.is_file(), f"{XBOX_BVH_CPP} must exist") + content = XBOX_BVH_CPP.read_text(encoding="utf-8") + + # Function definition + m = re.search(r"HRESULT builder_rehydrate_batched2x\([^)]*\)\s*\{(.*?)\n\}", content, re.DOTALL) + self.assertIsNotNone(m, "builder_rehydrate_batched2x must be defined in xbox_bvh_builder.cpp") + body = m.group(1) + + # Must not be a stub returning E_NOTIMPL + self.assertNotIn("return E_NOTIMPL;", body, "builder_rehydrate_batched2x must not return E_NOTIMPL") + + # Must return S_OK on empty count + self.assertIn("if (count == 0) return S_OK;", body) + + # Must validate descs and list + self.assertIn("if (!descs) return E_INVALIDARG;", body) + self.assertIn("if (!list) return E_INVALIDARG;", body) + + # Must convert to list4 and call Build or Copy + self.assertIn("ID3D12GraphicsCommandList4* list4 = list4_of(list);", body) + self.assertIn("BuildRaytracingAccelerationStructure", body) + self.assertIn("D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_DESERIALIZE", body) + + # Must release list4 and return S_OK + self.assertIn("list4->Release();", body) + self.assertIn("return S_OK;", body) + + def test_mangled_export_registered(self): + content = XBOX_BVH_CPP.read_text(encoding="utf-8") + # In C++, string literals split across lines are concatenated by the compiler. + compact = re.sub(r'"\s*\n\s*"', '', content) + symbol = "?RehydrateRaytracingAccelerationStructureBatched2X@XboxBvhBuilder@@QEAAJPEAUID3D12GraphicsCommandList8@@IPEBUD3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC@@PEB_K@Z" + self.assertIn(symbol, compact, f"Symbol {symbol} must be registered in kEntries") + self.assertIn("&builder_rehydrate_batched2x", content) + + def test_test_harness_validates_rehydrate(self): + self.assertTrue(XBOX_BVH_TEST_CPP.is_file(), f"{XBOX_BVH_TEST_CPP} must exist") + test_content = XBOX_BVH_TEST_CPP.read_text(encoding="utf-8") + self.assertIn("rehydrate(builder, list.p, 1, &rehydrate_desc, &source) == S_OK", test_content) + self.assertIn("rehydrate(builder, nullptr, 1, &rehydrate_desc, &source) == E_INVALIDARG", test_content) + self.assertIn("rehydrate(builder, list.p, 0, nullptr, nullptr) == S_OK", test_content) + + +if __name__ == "__main__": + unittest.main()