Skip to content

d3d12: write the structures a title builds for the ray walks - #623

Merged
brcly merged 1 commit into
brcly:mainfrom
3kynox:pr-console-built
Dec 1, 2026
Merged

brcly merged 1 commit into
brcly:mainfrom
3kynox:pr-console-built

Conversation

@3kynox

@3kynox 3kynox commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On main, now that #536, #586 and #596 are merged. Part of #382: the row "dynamic bottom-level structures".

A title builds most of its bottom-level structures while it runs. Main leaves their instances out of the scenes (#596). This writes them, behind the same TUXBOX_CONSOLE_STRUCTURES=1.

  • The writer (console_structure.rs, box_tree.rs). A structure in the console's form from the triangles a build names: one triangle to a leaf, 128-byte box nodes, the parent table. The tree is its own; a walk only needs boxes that hold what is below them. The tests walk it with a CPU port of the traversal (console_walk.rs) and compare with brute force.
  • Which builds (TUXBOX_CONSOLE_BUILT). By default only those in packed positions, whose geometry is final in title memory when the build is called. A build in float positions is mostly a mesh the GPU writes after the call, so it is left out. late writes those too, at the call and once more when the GPU is past that frame; that only helps on a host whose GPU works in the title's own memory (section-import=aliased). 0 and all are for comparing pictures.
  • Bounding the buffer. Nothing tells the provider that the title dropped a structure. A structure named inside the bytes a later one is sure to cover, or whose own bytes reach a later one's address, is now given up.

What it does not do: build from what the GPU wrote on a host that substitutes the import, and pair triangles in a leaf as the console's builder does.

Tested

  • The checks relevant to this change passed
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below
  • Where the table calls for AMD and NVIDIA, they passed on both. Otherwise, say which vendor still needs to run them: AMD has not run it.

No Lanes scenario: Lanes builds no console-format structure and has no walk that reads one. The writer, the choice of builds and the overlap rule are covered by unit tests.

GPU, driver and backend: RTX 4070 Ti SUPER 16 GB, 620.73.04, GE-Proton11-14, vkd3d-proton.

The Evidence: line tuxbox.py check prints last, one per card (it names the commit it tested; run it on this pull request's head):

Evidence: TuxBox check of ba24f252a976, no uncommitted changes, no GPU checks: 12 passed, 1 failed, 0 not run

The one failure is test_stage_baseline.GuestWatch, which fails on this machine on main too.

Commands run and their results:

cargo fmt --all --check                      clean
cargo clippy ... -p tuxbox-d3d12 -- -D warnings   clean (Rust 1.98.1)
tuxbox-d3d12 unit tests under Wine           596 passed, 3 failed, 8 ignored
                                             (the 3 are scene_publication, which need a hardware device)
python -m unittest tests.test_repository_hygiene   OK
cargo miri test ... -- console_heap console::tests console_structure box_tree   pass

In ProjectScarlett, one run each.

  • On 717a74c4, before this rebase: with packed positions only the main scene holds 765 to 779 of about 2,700 instances; with late, 2,550 to 2,590 of about 3,250 (of 8,192 builds in float positions read again, 5,777 were written at the second reading and 946 could not be read).
  • On 5523fc01 with this change and late, before the last rebase: 349 s of the world, then the title's own access violation, which the run without the switch ends on too (d3d12: lazy publication gives back a page's protection from deferral time, undoing the title's later VirtualProtect (Lanes texture-publication crashes) #625). The main scene holds 2,109 of 2,818 instances at the end; 4,096 structures written, 53 left out, 1,001 given up under later structures; of 2,048 builds read again, 128 were written at the second reading.

About the buffer in late: in that run it doubled up to its 1,024 MiB limit, with 594 MiB held when the title stopped. That is what the structures in it take, not places left behind: 4,096 built structures were written and 1,001 given up under later ones, beside about 900 rehydrated ones, so some 4,000 structures at about 150 KiB each, in a form that spends about 110 bytes a triangle (a 64-byte leaf, its share of the 128-byte box nodes and its parent entry). A scene that needs more than TUXBOX_CONSOLE_HEAP_MB loses the structures that find no room, and the log says so.

Still open: the structures written at the second reading have not been checked against a reference; the tests check the writer itself against brute force. There is no visible effect to show: see #586 for why. Under Miri the two tests that walk large structures use small ones.

Checks

CI runs the code checks. No evidence JSON changes.

@3kynox
3kynox force-pushed the pr-console-built branch 3 times, most recently from 11327bc to 170f04b Compare December 1, 2026 18:33
@3kynox
3kynox marked this pull request as ready for review December 1, 2026 18:33
Copilot AI balanced review requested due to automatic review settings December 1, 2026 18:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical overlap-invalidation and late-mode queue defects can retain overwritten structures, publish geometry to the wrong device, and let obsolete rereads replace or evict newer builds.

4 open issues
What changed in this PR

Extends the opt-in console ray-walk support to structures built from geometry at runtime, advancing #382 beyond the static structures handled by earlier PRs.

Changes:

  • Adds a console-format structure writer and four-way bounding-box tree, with CPU traversal tests against brute-force triangle intersections.
  • Adds build-selection modes, including delayed rereads for GPU-written float positions.
  • Tracks overlapping title-memory ranges to retire overwritten structures.
File Description
runtime/​d3d12/​src/​tests/​device/​console_heap.rs Tests overlap retirement and extent bookkeeping.
runtime/​d3d12/​src/​tests/​commands/​console.rs Tests build modes, position selection, and digests.
runtime/​d3d12/​src/​tests/​commands/​console_walk.rs Adds CPU traversal helpers for structure validation.
runtime/​d3d12/​src/​tests/​commands/​console_structure.rs Tests structure layout, geometry handling, and ray hits.
runtime/​d3d12/​src/​tests/​commands/​box_tree.rs Tests tree coverage, bounds, and determinism.
runtime/​d3d12/​src/​device/​console_heap.rs Adds overlap bookkeeping and exposes frame progress.
runtime/​d3d12/​src/​commands/​build_raytracing/​title_geometry.rs Adds triangle counting and transformed-corner access.
runtime/​d3d12/​src/​commands/​build_raytracing/​console.rs Wires runtime builds, delayed rereads, and diagnostics.
runtime/​d3d12/​src/​commands/​build_raytracing/​console_structure.rs Encodes triangles into console-format structures.
runtime/​d3d12/​src/​commands/​build_raytracing/​box_tree.rs Builds four-way bounding-box trees.
runtime/​d3d12/​src/​commands/​build_raytracing.rs Registers the tree and structure-writer modules.
docs/​ENVIRONMENT.md Documents TUXBOX_CONSOLE_BUILT modes and limitations.

Review effort: Balanced

Comment on lines +267 to +268
static LATER: std::sync::Mutex<std::collections::BTreeMap<u64, Later>> =
std::sync::Mutex::new(std::collections::BTreeMap::new());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ba24f252: the pending readings now belong to the device's buffer (Reread, kept in console_heap.rs under the buffer's lock), so a build on one device cannot take another's. Test: a_reading_waits_in_its_own_buffer_only_and_only_for_the_last_build_at_its_address.

Comment on lines +302 to +305
// Written at the call and given up since: the title has built over it.
if look.digest != 0 && heap.placed(destination).is_none() {
continue;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ba24f252: a claim now makes stale every reading pending for a build at an address inside the claimed bytes, whether that build placed a structure or not (Book::claim, Book::stale). Test: a_reading_pending_at_an_address_a_later_structure_covers_is_dropped.

Comment on lines +302 to +305
// Written at the call and given up since: the title has built over it.
if look.digest != 0 && heap.placed(destination).is_none() {
continue;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ba24f252: each address counts the builds made at it (Book::begun), a reading carries the number of the build it is for, and ConsoleHeap::structure_if_current checks that number and places the structure in one step under the buffer's lock. A build recorded meanwhile either comes first and makes the reading stale, or comes after and replaces what it placed. Test: a_reading_taken_before_the_title_builds_there_again_is_no_longer_current.

Comment on lines +637 to +639
if placed.is_some() {
inner.book.claim(addresses, extent, now);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ba24f252: the claim is applied whether or not the new structure finds room (Book::built_over). Test: a_structure_that_finds_no_place_still_gives_up_what_the_title_built_over.

@3kynox
3kynox force-pushed the pr-console-built branch 2 times, most recently from a22aa1a to 32c9c3f Compare December 1, 2026 21:57
A build in packed positions is written in the console's form from the triangles it names, at the call. One in float positions is left out by default: the GPU mostly writes those after the call. TUXBOX_CONSOLE_BUILT=late writes them too and reads them again once the GPU is past that frame, for a host whose GPU works in the title's own memory. A structure the title builds over is given up, which is what bounds the buffer.
@brcly
brcly merged commit 53782ee into brcly:main Dec 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants