d3d12: expand the packed bottom-level structures a title ships #536
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/536"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from brcly/TuxBox. Originally posted by 3kynox on 2026-11-29T21:03:56Z.
Summary
For #382, the static bottom-level structures. A title streams them in a packed form and asks the console's builder library to expand each in place; the walks then read the expanded records.
packed_structure::unpackdoes that expansion: it takes the packed bytes and the triangles the build names, and returns the structure the title's ray-walk shaders read.Nothing calls it yet, so behaviour does not change. The buffer the structures go into and the wiring to the builds and dispatches follow in two more PRs; this one is the expansion and its tests alone, so it can be reviewed by itself.
Rebased onto
8ece312fas a fresh commit: formatted, and clean under the lintsmainnow denies. Eight integer conversions were rewritten for that (a mask before each narrowing,cast_signed/cast_unsignedin the half-float rounding, onetry_fromon a masked offset); none changes a bit, and the comparison with the library below was run again on this code. The row this PR first added todocs/REFERENCES.mdis gone with the table (9acdc888); the module comment now names the revision of the public source instead.Where it comes from:
docs/bvh.mddescribes (sections 3.1 and 4a), already cited inrehydrate.rs. Its repository is offline today;ProjectLeonida/mandycarries the document;Where those are silent, what the library does was measured off the game: the title's own
XboxBvhBuilder.dllhas a rehydration entry that runs on the CPU and needs no device, so the same packed bytes and geometry can be given to it and tounpackunder Wine, and every byte compared. That harness loads the title's library and stays on my machine. What it showed, and the code follows:0x007f7f7fbrings a negative zero out positive.Review fix,
4631d10b. A child pointer and the root are now held to a box node of their own size at the place it begins: a 128-byte pointer to two 64-byte nodes, or a pointer into the middle of a 128-byte node, was expanded as if it named a node. The comparison below was run again on this commit: the 103 structures compared come out as before. The game has not run on this commit yet.Results, on this code
In the game, called from a local build that reads the geometry on the CPU at the rehydration call:
on
1474b05e, before the review fix, with the two PRs that follow and #566 on top: in a 9-minute run, the structures the title rehydrated were all expanded and placed, 485 by the last count, none refused. That run ended on a GPU loss a minute into the world, in mesh-shader draws; a run without the switch that turns all this on went 25 minutes. I can't tell yet whether the two are related; more runs come before the wiring PR does;before the rebase, with the code as it was but for the eight conversions:
Not handled:
When the library expands a structure in place, as the title asks, the level table stays at the end of the structure.
unpackleaves zeros there, as the library does when its destination is another buffer. No walk reads it.Tested
Run the checks relevant to this change.
mainon this machine (below)No Lanes scenario: the function is not called by the provider yet. It has twelve unit tests on packed structures built by hand in the test file (no title data). 77 errors planted in the expansion one at a time (a corner in the wrong slot, halves rounded inwards, the frame's move left out, a missing parent entry, a pointer of the other size accepted, products added one after the other and so on): 75 fail at least one test. The other two change nothing a caller could see: bounding the fourth corner of a one-triangle leaf (it is not a number, and a bound ignores those), and expanding a level's 64-byte nodes before its 128-byte ones (neither reads the other).
GPU, driver and backend: none needed for the checks below. The game runs above: GeForce RTX 4070 Ti SUPER, 620.73.04, GE-Proton11-14, Core i9-12900K, ProjectScarlett 1.0.2.1 offline.
The
Evidence:linetuxbox.py checkprints last, one per card (it names the commit it tested; run it on this pull request's head):Everything below was run on the head commit,
4631d10b.Commands run and their results:
Checks
CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, a Windows typecheck of every crate, and clippy.
Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T04:12:17Z.
Heads-up: main just landed a large cleanup (up to
e5d2e913), so this PR needs a rebase.rustfmt.toml).unwrap(), lossyascasts and undocumentedunsafeoutside tests.cargo fmt --check, rustdoc with warnings denied, and Miri on the providers' unit tests.The least painful way is to format your branch first, so the rebase only shows real changes:
Then run
python tuxbox.py check --no-gpu, which now includes the format step.If clippy flags something new:
Fromfor widening,TryFromfor narrowing, and a mask for bit fields.unwrap(): use?, orexpectwith the reason it can't fail.unsafeblock: it needs a// SAFETY:comment.CONTRIBUTING's "Code checks" section lists the commands. Thanks!
Imported from brcly/TuxBox. Originally posted by Copilot on 2026-11-30T10:44:27Z.
Code review location:
runtime/d3d12/src/commands/build_raytracing/packed_structure.rs; line 447This range check accepts malformed child pointers, such as a BOX32 pointer to two consecutive BOX16 records, and then reads half-float data as float bounds while preserving the invalid pointer in the output. The root check at lines 574–575 has the same issue: it accepts the wrong node kind or an address inside a wide record. Retain the level table's width-specific node ranges and validate both children and the root against actual record starts of the declared kind before accepting them.
Imported from brcly/TuxBox. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-30T10:44:27Z.
Review: COMMENTED
🟡 Changes recommended
A critical pointer-validation flaw allows child and root pointers with the wrong node kind or misaligned record address, causing malformed bounds reads and preserving invalid pointers in the output.
1 open issue
What changed in this PR
Adds CPU-side expansion of packed bottom-level ray-tracing structures to TuxBox’s D3D12 provider, preparing for the ray walks tracked in #382. The function is not yet called outside tests, so runtime behavior remains unchanged.
Changes:
runtime/d3d12/src/tests/commands/packed_structure.rsruntime/d3d12/src/commands/build_raytracing/packed_structure.rsruntime/d3d12/src/commands/build_raytracing.rsReview effort: Balanced
Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T11:45:59Z.
Thanks for the rebase and for the careful sourcing. The byte-for-byte comparison against the title's own rehydration entry is exactly the right standard, and so is keeping that harness off the repository.
As the description says, nothing calls this yet, and the same goes for #586. Our rule for this round is to merge when a change has an effect someone can see or measure. Your own runs on #382 also show that, with the console structures switched on, the walks currently end in GPU losses (three of three, Xid 13). So I'm holding #536 and #586 until the wiring PR is up. Then we'll review and merge the three together, once a run shows:
Splitting the work into three PRs is still useful: it keeps each one reviewable. They just land together.
Imported from brcly/TuxBox. Originally posted by 3kynox on 2026-11-30T12:25:40Z.
Code review location:
runtime/d3d12/src/commands/build_raytracing/packed_structure.rs; line 447Right, and against what the file says of itself: what does not follow the packing is refused. Fixed in
4631d10b.A child pointer and the root are now held to a box node of their own size, at the place it begins.
boxeskeeps the pairs of the level table it has checked, andLevels::beginsfinds the level a pointer falls in by a binary search over them, so nothing is allocated by a count the input gives. A structure whose box nodes are off the 64-byte steps a pointer counts in is refused by name.One new test, which fails on
1474b05e: a 128-byte pointer to two 64-byte nodes, a 64-byte pointer to a 128-byte node and to its second half, a 128-byte pointer from the middle of one, a pointer to a node of the pointing node's own level, and the same for the root.Run again on this commit:
check --no-gpu(12 steps passed, the Python test that fails onmainhere still fails), and the Wine suite (477 passed; the three failures are thescene_publicationtests that ask for a hardware device).The description is updated with these.
Imported from brcly/TuxBox. Originally posted by 3kynox on 2026-12-01T11:58:16Z.
@brcly a data point for this PR by itself. Ahmet-Dedeler ported this decoder into another runtime and ran ProjectScarlett with it on an M5 Max (his comment on #596): 1,739 structures (179 MiB) expanded from the title's data, none refused, and the game's own indirect light in the picture, read by the title's walks from the expanded trees. Here the same code expands up to about 1,000 structures a run, none refused.
So its effect has been seen, on another host, though not yet in TuxBox. If that is enough for this PR alone, it could land apart from #586 and #596, which still wait on your two conditions; the runs on main are in #586. If you would rather keep the three together, that is fine too.
Imported from brcly/TuxBox. Originally posted by brcly on 2026-12-01T15:21:47Z.
Merged on its own, thanks @3kynox. The decoder now has outside evidence (Ahmet-Dedeler's run with the title's indirect light, none of 1,739 structures refused), and on main it does nothing until something calls it, so it can go ahead of #586 and #596.