upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/xbox_bvh_builder.cpp
diff options
context:
space:
mode:
authorTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
committerTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
commitbeffaa9a476ca1b3dfd9cfa55e39eafbb1f5eec6 (patch)
tree32877805bf4eb46d9a89d5753db6b5a2b06dc845 /runtime/shims/xbox_bvh_builder.cpp
parentf29ca7bacae9507f83e8c56abdb604bd4efb4cf1 (diff)
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
Merge main into fix/null-descriptor-generated-dir
Diffstat (limited to 'runtime/shims/xbox_bvh_builder.cpp')
-rw-r--r--runtime/shims/xbox_bvh_builder.cpp188
1 files changed, 177 insertions, 11 deletions
diff --git a/runtime/shims/xbox_bvh_builder.cpp b/runtime/shims/xbox_bvh_builder.cpp
index 8c80419e2..ad6d6810a 100644
--- a/runtime/shims/xbox_bvh_builder.cpp
+++ b/runtime/shims/xbox_bvh_builder.cpp
@@ -28,8 +28,13 @@
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) (docs/bvh.md §4a). The leaf frame
33// compressionParams gives is not applied yet: the leaves are built from the geometry as the description has it.
34// The GPU cannot read offline dehydrated console streams directly (docs/bvh.md §4/§4a),
35// so source-only streams without geometry are refused with E_NOTIMPL. The title passes Scratch = 0, so the shim
36// supplies scratch: one buffer per call, sized via GetRaytracingAccelerationStructurePrebuildInfo, in which each
37// element has a region of its own (rehydrate_scratch).
33// 38//
34// The builder object belongs to the title, which allocates it with a size this file doesn't know, so nothing is 39// 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. 40// written into it: what the shim keeps (the device, the Morton option) lives in a table keyed by its address.
@@ -47,7 +52,11 @@
47#include <cstring> 52#include <cstring>
48#include <mutex> 53#include <mutex>
49#include <unordered_map> 54#include <unordered_map>
55#include <vector>
50 56
57#ifdef LEONIDA_VULKAN_DEVICE
58#include "d3d12_com.h" // rt::d3d12::hold_until_reset
59#endif
51#include "d3d12_xs_device.h" 60#include "d3d12_xs_device.h"
52#include "shims.h" 61#include "shims.h"
53#include "../src/xdxr_descs.h" 62#include "../src/xdxr_descs.h"
@@ -59,8 +68,18 @@ namespace xdxr = leonida::xdxr;
59struct BuilderState { 68struct BuilderState {
60 ID3D12Device5* device = nullptr; // a reference held for the builder's lifetime; null if it has no DXR methods 69 ID3D12Device5* device = nullptr; // a reference held for the builder's lifetime; null if it has no DXR methods
61 bool morton64 = false; 70 bool morton64 = false;
71 // Rehydration scratch made for a list that cannot hold it (not the layer's own, rehydrate_scratch): kept until the
72 // builder is destroyed, since the shim never learns when the GPU is done with it.
73 std::vector<ID3D12Resource*> kept;
62}; 74};
63 75
76void release_state(BuilderState& state) {
77 for (ID3D12Resource* scratch : state.kept) scratch->Release();
78 state.kept.clear();
79 if (state.device) state.device->Release();
80 state.device = nullptr;
81}
82
64std::mutex g_mutex; 83std::mutex g_mutex;
65std::unordered_map<const void*, BuilderState> g_builders; 84std::unordered_map<const void*, BuilderState> g_builders;
66 85
@@ -107,8 +126,8 @@ void* builder_construct(void* self, void* device, bool) {
107 } 126 }
108 std::lock_guard<std::mutex> lock(g_mutex); 127 std::lock_guard<std::mutex> lock(g_mutex);
109 BuilderState& state = g_builders[self]; 128 BuilderState& state = g_builders[self];
110 if (state.device) state.device->Release(); // the same storage constructed twice without a destructor 129 release_state(state); // the same storage constructed twice without a destructor
111 state = BuilderState{device5, false}; 130 state = BuilderState{device5, false, {}};
112 return self; 131 return self;
113} 132}
114 133
@@ -117,7 +136,7 @@ void builder_destruct(void* self) {
117 std::lock_guard<std::mutex> lock(g_mutex); 136 std::lock_guard<std::mutex> lock(g_mutex);
118 auto it = g_builders.find(self); 137 auto it = g_builders.find(self);
119 if (it == g_builders.end()) return; 138 if (it == g_builders.end()) return;
120 if (it->second.device) it->second.device->Release(); 139 release_state(it->second);
121 g_builders.erase(it); 140 g_builders.erase(it);
122} 141}
123 142
@@ -138,6 +157,63 @@ ID3D12Device5* builder_device(void* self) {
138 return it->second.device; 157 return it->second.device;
139} 158}
140 159
160// Scratch for one RehydrateRaytracingAccelerationStructureBatched2X call, where the title passes
161// ScratchAccelerationStructureData = 0 (measured on the retail title): a new buffer of `bytes`, in which each element
162// that needs scratch has a region of its own. It is never shared with another call: two calls on one list (the title
163// puts no barrier between them for scratch it does not know of) or lists recorded on several threads never write the
164// same bytes. The list holds the buffer until it is Reset or released (rt::d3d12::hold_until_reset), and the layer
165// destroys it after the submission that used it has retired; a list that cannot hold it leaves it to the builder,
166// which keeps it until its destruction. Returns the buffer's GPU address, 0 when it could not be made.
167UINT64 rehydrate_scratch(void* self, ID3D12Device5* device, ID3D12GraphicsCommandList4* list, UINT64 bytes) {
168 if (!device || !bytes) return 0;
169 D3D12_HEAP_PROPERTIES heap_props{};
170 heap_props.Type = D3D12_HEAP_TYPE_DEFAULT;
171 D3D12_RESOURCE_DESC res_desc{};
172 res_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
173 res_desc.Alignment = 0;
174 res_desc.Width = bytes;
175 res_desc.Height = 1;
176 res_desc.DepthOrArraySize = 1;
177 res_desc.MipLevels = 1;
178 res_desc.Format = DXGI_FORMAT_UNKNOWN;
179 res_desc.SampleDesc.Count = 1;
180 res_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
181 res_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
182
183 ID3D12Resource* res = nullptr;
184 HRESULT hr = device->CreateCommittedResource(
185 &heap_props, D3D12_HEAP_FLAG_NONE, &res_desc,
186 D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr,
187 IID_PPV_ARGS(&res));
188 if (FAILED(hr) || !res) return 0;
189 const UINT64 va = res->GetGPUVirtualAddress();
190 bool held = false;
191#ifdef LEONIDA_VULKAN_DEVICE
192 held = rt::d3d12::hold_until_reset(list, res);
193#else
194 (void)list;
195#endif
196 if (held) {
197 res->Release(); // the list's reference keeps it from here
198 return va;
199 }
200 static std::atomic<bool> s_logged{false};
201 log_once(s_logged, "RehydrateRaytracingAccelerationStructureBatched2X: the list cannot hold its scratch; the "
202 "builder keeps it until it is destroyed");
203 std::lock_guard<std::mutex> lock(g_mutex);
204 auto it = g_builders.find(self);
205 if (it == g_builders.end()) { // destroyed during the call: nothing is recorded with this scratch
206 res->Release();
207 return 0;
208 }
209 it->second.kept.push_back(res);
210 return va;
211}
212
213bool has_geometry(const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS& inputs) {
214 return inputs.NumDescs > 0 && (inputs.pGeometryDescs || inputs.InstanceDescs);
215}
216
141void note_stream(const xdxr::StreamDesc& stream) { 217void note_stream(const xdxr::StreamDesc& stream) {
142 if (stream.size_in_bytes == 0 || !stream.stream) return; 218 if (stream.size_in_bytes == 0 || !stream.stream) return;
143 static std::atomic<bool> s_logged{false}; 219 static std::atomic<bool> s_logged{false};
@@ -227,12 +303,102 @@ void builder_copy(void*, void* list, UINT64 dest, UINT64 source, D3D12_RAYTRACIN
227} 303}
228 304
229// ?RehydrateRaytracingAccelerationStructureBatched2X@XboxBvhBuilder@@QEAAJPEAUID3D12GraphicsCommandList8@@IPEBUD3D12_ 305// ?RehydrateRaytracingAccelerationStructureBatched2X@XboxBvhBuilder@@QEAAJPEAUID3D12GraphicsCommandList8@@IPEBUD3D12_
230// BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC@@PEB_K@Z -> HRESULT. Not implemented yet (see the header comment). 306// BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC@@PEB_K@Z -> HRESULT.
231HRESULT builder_rehydrate_batched2x(void*, void*, UINT, const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC*, 307// Rebuilds each acceleration structure from geometry on the command list.
232 const UINT64*) { 308HRESULT builder_rehydrate_batched2x(void* self, void* list, UINT count,
233 static std::atomic<bool> s_logged{false}; 309 const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC* descs,
234 log_once(s_logged, "RehydrateRaytracingAccelerationStructureBatched2X is not implemented yet; E_NOTIMPL"); 310 const UINT64* source_data) {
235 return E_NOTIMPL; 311 if (count == 0) return S_OK;
312 if (!descs) return E_INVALIDARG;
313 if (!list) return E_INVALIDARG;
314 ID3D12GraphicsCommandList4* list4 = list4_of(list);
315 if (!list4) {
316 static std::atomic<bool> s_logged{false};
317 log_once(s_logged, "RehydrateRaytracingAccelerationStructureBatched2X: the list has no ID3D12GraphicsCommandList4");
318 return E_NOINTERFACE;
319 }
320 ID3D12Device5* device = builder_device(self);
321 // First pass: the descriptions as they are recorded, and for each element that builds without scratch of the
322 // title's a region of its own in one scratch buffer for the batch, at D3D12's acceleration-structure alignment
323 // (256 bytes, which the layer checks scratch addresses against). The XDXR recorder leaves the dependency between
324 // builds to the caller's barrier (src/xdxr_build.h, dispatch()) and the title puts none between the elements of
325 // a batch, so elements must not share scratch bytes. The first source-only element ends the batch (below).
326 constexpr UINT64 kAlign = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT;
327 constexpr UINT64 kNoRegion = ~0ull;
328 std::vector<D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC> builds;
329 std::vector<UINT64> regions;
330 builds.reserve(count);
331 regions.reserve(count);
332 UINT64 scratch_bytes = 0;
333 bool source_only = false;
334 for (UINT i = 0; i < count; ++i) {
335 D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC build = descs[i];
336 const UINT64 src = source_data ? source_data[i] : build.SourceAccelerationStructureData;
337 // A rehydrate is a fresh build from geometry. The title sets SourceAccelerationStructureData to the
338 // dehydrated stream (measured on the retail title), and the layer reads that address for an update
339 // (d3d12_com.cpp, BuildRaytracingAccelerationStructure), so with PERFORM_UPDATE kept it would refit from
340 // the stream's bytes. Both are cleared.
341 if (build.Inputs.Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE) {
342 static std::atomic<bool> s_update_logged{false};
343 log_once(s_update_logged,
344 "RehydrateRaytracingAccelerationStructureBatched2X: an element asks for PERFORM_UPDATE; it is "
345 "built afresh from its geometry instead (the source is the dehydrated stream)");
346 build.Inputs.Flags &= ~D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE;
347 }
348 build.SourceAccelerationStructureData = 0;
349 UINT64 region = kNoRegion;
350 if (has_geometry(build.Inputs)) {
351 if (!build.ScratchAccelerationStructureData && device) {
352 D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO prebuild{};
353 device->GetRaytracingAccelerationStructurePrebuildInfo(&build.Inputs, &prebuild);
354 const UINT64 size = prebuild.ScratchDataSizeInBytes ? prebuild.ScratchDataSizeInBytes : 1;
355 region = scratch_bytes;
356 scratch_bytes += (size + kAlign - 1) / kAlign * kAlign;
357 }
358 } else if (src) {
359 source_only = true;
360 break;
361 }
362 builds.push_back(build);
363 regions.push_back(region);
364 }
365 const UINT64 scratch = scratch_bytes ? rehydrate_scratch(self, device, list4, scratch_bytes) : 0;
366
367 for (std::size_t i = 0; i < builds.size(); ++i) {
368 D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC& build = builds[i];
369 if (has_geometry(build.Inputs)) {
370 // The title passes ScratchAccelerationStructureData = 0 (measured on the retail title).
371 // Supply a scratch region to prevent d3d12_com.cpp from poisoning the command list.
372 if (regions[i] != kNoRegion && scratch) build.ScratchAccelerationStructureData = scratch + regions[i];
373 if (!build.ScratchAccelerationStructureData) {
374 static std::atomic<bool> s_scratch_logged{false};
375 log_once(s_scratch_logged,
376 "RehydrateRaytracingAccelerationStructureBatched2X: ScratchAccelerationStructureData is 0 "
377 "and could not be allocated; skipping build to avoid command list rejection");
378 continue;
379 }
380 list4->BuildRaytracingAccelerationStructure(&build, 0, nullptr);
381 } else {
382 static std::atomic<bool> s_empty_logged{false};
383 log_once(s_empty_logged,
384 "RehydrateRaytracingAccelerationStructureBatched2X: element has NumDescs == 0 with no geometry; "
385 "skipping empty structure");
386 }
387 }
388 if (source_only) {
389 // docs/bvh.md §4/§4a: the rehydrate source is a dehydrated console stream (element chunks,
390 // level table, box overrides), not a D3D12 serialized blob. On the XDXR path, a DESERIALIZE
391 // copy is replayed as a byte clone, so the destination would hold dehydrated bytes the GPU
392 // traversal shaders cannot read. Rehydration requires geometry reconstruction (docs/bvh.md §4a).
393 // Refusing source-only rehydration without geometry.
394 static std::atomic<bool> s_source_only_logged{false};
395 log_once(s_source_only_logged,
396 "RehydrateRaytracingAccelerationStructureBatched2X: source-only dehydrated stream without "
397 "geometry cannot be deserialized as a D3D12 BLAS (docs/bvh.md §4/§4a); refusing element");
398 }
399 if (device) device->Release();
400 list4->Release();
401 return source_only ? E_NOTIMPL : S_OK;
236} 402}
237 403
238constexpr const char kDll[] = "xboxbvhbuilder.dll"; 404constexpr const char kDll[] = "xboxbvhbuilder.dll";