Repository navigation
d3d12: keep console-format structures for the ray walks in one buffer - #586
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Placement reuse can overwrite geometry still referenced by recorded commands or live scenes before all readers retire, and failed rebuilds leave stale address mappings accessible.
3 open issues
What changed in this PR
Adds shared-buffer storage and address bookkeeping for the console-format structures used by translated ray walks (#382). The module is not yet wired into builds or dispatches, so runtime behavior remains unchanged.
Changes:
- Adds 256-byte-aligned placements, address mappings, and reuse after a four-present delay.
- Adds 13 unit tests covering allocation, reuse, and naming behavior.
- Documents the configurable buffer capacity, defaulting to 1,024 MiB.
| File | Description |
|---|---|
runtime/d3d12/src/tests/device/console_heap.rs |
Tests placement allocation, reuse, limits, and address bookkeeping. |
runtime/d3d12/src/device/console_heap.rs |
Implements the upload buffer, placement allocator, and structure/scene mappings. |
runtime/d3d12/src/device.rs |
Registers the console heap module. |
docs/ENVIRONMENT.md |
Documents TUXBOX_CONSOLE_HEAP_MB and its current unwired status. |
Review effort: Balanced
| if now < since.saturating_add(REST) { | ||
| break; | ||
| } | ||
| self.resting.pop_front(); | ||
| self.release(at, size); |
There was a problem hiding this comment.
Agreed that four presents counted on the CPU say nothing of how far behind the GPU is. In 351fbb4 the places rest by a clock that follows the GPU. At each present it notes what the queues still have to run (queue_trace::pending_fences, which blas/lifetime.rs reads at retirement), and a place given up at present N is free only once every batch submitted by present N + 4 has run (a queue's mark moves after each batch, submit.rs). A GPU twenty frames behind keeps the place twenty frames longer (a_place_given_up_waits_for_a_gpu_that_is_frames_behind).
What this does not cover, and the module comment now says so: a list recorded before the place was given up and handed to its queue more than four presents later. Only the list itself could hold what it binds. That belongs with the wiring PR, where bind is called at a dispatch and the list is at hand; I would rather do it there than guess the list's side from here.
| if entry.1 == 0 { | ||
| let bytes = self.named.remove(&offset).map_or(0, |(bytes, _)| bytes); | ||
| self.places.give(offset, bytes, now); |
There was a problem hiding this comment.
Right. A scene now holds the place of every structure its instance records name: scene takes those places, and a structure's place is given up only when no address names it and no scene still seen holds it. It then rests from the same present as the scene's own old place, so the two cannot be written over in the wrong order. A scene that names a place given up since the caller read it is not placed at all. Tests: a_scene_holds_the_places_of_the_structures_it_names and a_structure_two_scenes_name_keeps_its_place_until_both_let_go.
| pub(crate) fn structure(&self, device: &ID3D12Device, addresses: &[u64], bytes: &[u8]) -> bool { | ||
| let Some(header) = bytes.get(..64).and_then(|header| <[u8; 64]>::try_from(header).ok()) else { return false }; | ||
| let mut inner = self.lock(); | ||
| let Some(offset) = inner.write(device, bytes) else { return false }; |
There was a problem hiding this comment.
Right, and the early return on a short header had the same hole. Book::structure now forgets every address it was given when the structure finds no place, so placed answers none for them, as the module comment promises (a_structure_that_finds_no_place_is_no_longer_seen_at_its_addresses).
|
Thanks. The reuse with a four-present rest, the smallest-fit choice, and the 18 planted errors each caught by a test all look right. I'm holding this together with #536 until the wiring PR is up (reasons there): nothing calls it yet, and the switched-on runs on #382 end in GPU losses. Two things to have answered by then:
|
|
Heads-up for |
|
Answers to the two questions, and where the runs stand. All on ProjectScarlett 1.0.2.1 offline, RTX 4070 Ti SUPER 16 GB, 620.73.04, GE-Proton11-14: 1. The slow growth. The 1,080 MiB came from a local build that also wrote the structures the title builds every frame, which none of these PRs do. That number should not stand next to this PR and I'll reword the description. With what the PRs do (rehydrated structures and the scenes), the buffer holds 84 to 126 MiB when the world starts (920 to 1,016 structures) and 140 to 223 MiB five to ten minutes later (about 1,700 structures). The bytes held per structure stay the same within a run (0.12 then 0.13 MiB in one, 0.09 then 0.08 in another) while some 500 scenes are written again, so the scenes do not add to it and the reuse does what it is for. What grows is the number of structures: a place is given back when the title rehydrates or builds at the same address again, and nothing tells the provider that the title has dropped a structure. So it does not level off while the opening streams the world in: 8 to 16 MiB a minute. Two ways to bound it, neither done: give up what a new structure overlaps in title memory, or give up a structure no scene has named for a while and expand it again when one does. I want to measure which structures stay unnamed before choosing. 2. The default size. It can grow on demand, because a scene's offsets are relative to the buffer: a larger buffer that begins with a copy of the smaller one reads the same to a walk. I have that written and not pushed: 64 MiB at first, twice the size when nothing fits, What the size costs on this card, measured: the title fills video memory by itself (the card sits at 15.3 of 16 GiB in a run without the switch, desktop included) and vkd3d-proton puts UPLOAD heaps in host-visible video memory. The 1,024 MiB buffer never fit: in five runs of five, "Memory allocation failed, falling back to system memory" is the line just before the buffer's own. A 320 MiB one was made without that line, and 208 allocations fell back after it; the 35-minute run without the switch had one in all. So reserved whole it lands in system memory here, and smaller it takes video memory from the title. I do not know which of the two this buffer should be on a card this full, and would like your view. The runs. With
So on this card the switch brings a loss that the two runs without it did not show in an hour, through something other than the walks' reads: what is left is the work on the CPU at each rehydration and scene build, and the buffer's allocation. I have not told those apart, and with losses anywhere between 2 and 19 minutes one more run says little. DrArroway, thank you for the capture. Loads on inactive lanes would fit the loss we had earlier with a walk's dispatch in flight, and a read 3 GiB past a root SRV lands on whatever else is mapped, which this buffer changes. #594 is in since; the next runs are on main with it, and I'll report here. Visible effect. None I can show. The opening is a cinematic that advances with the frames presented, one every five seconds here, so two runs do not show the same shot at the same moment. In the one static shot I can compare, the picture with two walks bound is the picture without. About 2,050 of the 3,250 instances of the main scene name structures the title builds while it runs, which these PRs do not write. Neither of your two conditions is met, then. The wiring PR is up as a draft, #596, on current main and stacked on these two, so the three can be read side by side. Everything above was run on |
|
A correction to what I wrote above about #389, and three runs on tonight's main. #389. I wrote that it does not separate anything here. That holds for how long a run lasts, not for where it ends. Without #389, both losses have only The 36-second run had On
So tonight's main does not hold on this card without the structures either, where |
|
Runs on main Main does not hold here with or without the switch, and the switch does not shorten it.
In all six, No dispatch is in flight at any of these six losses, only draws. Yesterday's 39-second loss on The walks for indirect light and reflections cannot be given the buffer yet. With every walk given it, the device is lost as soon as the walks are bound (one run, on Visible effect: none seen from the four distant-shadow walks in the yard scene of the intro. The runs without the switch die before the shots worth comparing, so the 1,201-second run alternates instead: a local switch gives the walks the buffer for 45 s, then their empty buffers for 45 s, and the log dates each change. In the yard shots I compared eight pairs of captures taken 30 to 60 s apart, one with and one without, at a quarter of their size and with the camera drifting. Seven show the same picture; in the eighth the yard's geometry is missing from one capture altogether. As a control I ran the same alternation with closed boxes around each scene in place of its structures, so that every ray hits (661 s of the world, 172 frames, the same end): the two pairs taken within one shot show the same picture too. On an older base ( The structures the title builds. Not in #596; this is the local commit, and where I am going next. Builds in packed positions are written from title memory at the call. In the 1,201-second run, builds in float positions are written at the call too, then read again once the GPU is past that frame: this host's profile says |
|
@3kynox one candidate for the Xid 13 in mesh-shader draws, not tested against your fault: on main the mesh and amplification stages of a mapped-root pipeline are translated without the index map, so under descriptor paging they read other descriptors. #617 (draft) fixes that; its Lanes scenario fails on main and passes with the change. If you try it on your card, the pipelines in your loss lines would be the ones to watch. |
|
@NickStallman thank you. Tried on this card: main
Without #617 the two runs with the switch off lost the device after 158 s and 13 s, so it looks like the fix for those here. One run, but it is the first on today's main to reach its limit on this card. With #617 the switch brings a loss the run without it no longer has, which this morning's runs could not show since both sides died. The indirect-light walks, to complete the paragraph above. Six runs of six with every walk given the buffer end about 15 s after the walks are bound: Xid 109, one command in flight, each time a hit-resolve program dispatched through |
|
@3kynox thanks for trying it. I have added your run to #617. For comparison: our builds have carried that change all evening, and we still see intermittent device losses on the first world scene change, 125-200 s in, on a 32 GB card: Xid 109 with mesh draws or compute (shadow rays, culling) in flight, and twice Xid 13. It is a different program each time, and runs with extra diagnostics on fail far more often than plain runs. We are still narrowing it down and will post when it is measured. |
|
Narrowing what in the switch brings the loss back with #617. Same setup as above, one run per value, time from the first ray dispatch of the world.
It is not the CPU work at each rehydration and scene build: the 1 MiB run does all of it (1,703 structures expanded, nearly all left out for lack of room) and reaches its limit. It is not the walks' reads either: the run with no walk given the buffer is lost too. It follows the size of the buffer. The 1,024 MiB one falls back to system memory when it is created, in every run; the 256 MiB one does not and lasts longer. Every loss has only mesh-shader draws in flight, and The buffer that starts at 64 MiB and doubles when nothing fits is merged locally on top of #596 (two more tests). I'll push it here once it has run in the game. |
|
Two runs with the buffer starting at 64 MiB and doubling when nothing fits (local, on top of #596 and #617), four distant-shadow walks given the buffer, same setup and same clock as above.
In both the buffer went 64, 128, 256 MiB, with 990 and 1,507 structures placed, none left out and no fall back to system memory. Neither fault is in memory that was never mapped, which every loss with the fixed-size buffers was; both are the freed-then-read kind, far from the buffer. So with #617 and the buffer starting small, the walks read the structures for 20.8 minutes of the world in one run of two, and the other ended on what looks like the intermittent loss @NickStallman describes. @brcly, is that enough for the first condition while that loss is open, or would you rather see it repeated once it is fixed? The second condition stands as written above: no picture yet from these four walks in the scenes these runs reach. The growing buffer is not pushed yet: one case is still open, a list that waits more than four presents between recording and execution could read a buffer already given back. It needs the wiring to make the list hold the buffer, which belongs in #596. |
|
@3kynox thanks for narrowing it down so carefully. The fixed-size runs show the losses follow the buffer's size, and the growing buffer removes the never-mapped reads, which is real progress. On your question: one run of two isn't enough for the first condition yet, so I'd keep #586 until the remaining loss is understood. What would settle it is the same build and settings with the switch off: if those runs lose the device at about the same rate with the same freed-then-read kind of fault, the loss isn't from the structures, and #586 can go in with the growing buffer. If the losses only come with the switch on, they're ours to find first. #536 is merged on its own (see there), so #586 can rebase onto main. Please put the growing buffer and the list holding its buffer into #586 and #596 as you describe, since a list that waits more than four presents reading a buffer already given back would be the same use-after-free class we're chasing in #603. The second condition (a picture from these walks) stays as it was. |
…ing fits The buffer was reserved whole, 1,024 MiB by default. It now starts at 64 MiB and is made again at twice the size when no place fits, up to TUXBOX_CONSOLE_HEAP_MB, which becomes the most it may be. A scene's offsets are relative to the buffer, so the larger buffer begins with a copy of the smaller one and a walk reads the same from either. The smaller one is kept for the lists that bound it, by the same rest as a place given up.
5b82343 to
d28d649
Compare
|
@brcly the switch-off control you asked for, now that #625 is fixed. Setup. One tree, the same settings on both sides, 25-minute limit, runs alternated. RTX 4070 Ti SUPER 16 GB, 620.73.04, Intel CPU. The tree is
Four of five end early with the switch off, three of five with it on. The freed-then-read fault comes twice on each side. The rejected submission also ended a run with the switch off at other settings, and #382 has it from builds without this code. The spread is wide, so five runs a side give no rate, but I see no loss that comes only with the switch. Correction. I wrote on this PR that the 95,048-group indirect dispatch is over the host's limit. The indirect-light walks. On this tree the hit-resolve programs are emitted with 11 or 12 loops instead of 27. With every walk given the buffer the device is still lost soon after the walks are bound: Xid 109, one command in flight, |
|
@brcly the three things you asked for before the console structures become a default. A visible effect: yes. In ProjectScarlett's first playable scene, the right side of the room (the drawings on the wall, the bunk) is lit by indirect light with the structures on and goes black with them off. All nine walks are served, the programs that shade their hits run, and nothing is skipped. Alternating every 45 s from a still camera, it switched 5 times out of 5 each way in one run and again in a second, shorter one ( What blocked it was in the translator: a ballot branched on the lane around its operand, and NVIDIA's compiled program never ended. The five-line change is #670. The list that holds its buffer: merged in #644. The off/on control: done for four walks, not met yet for all nine. The four-walk control is my previous comment here: ten runs, no loss that comes only with the switch, and that result stands. With nine walks and the shading programs running, three pairs on the same build: the device was lost in three runs of three with the switch on, and in one of three with it off. Most of those losses look like #603. One looks like ours: the fault address points to a walk reading outside the structures buffer, and I haven't found the cause. So the switch should stay off by default until I have, and I'll redo the control then. |



Summary
For #382, the second of the pieces agreed there: the buffer the console-format structures live in, and the bookkeeping of its places. It follows #536 (the expansion of the packed bottom-level structures) and is independent of it: the two can be reviewed and merged in either order.
A translated ray walk reads every scene from one raw buffer and takes each scene's place in it from a block of offsets. Today that buffer is the empty one
compute_scenemakes for each walk.device::console_heapis the real one, one per device:Review fixes, 351fbb4 (the three remarks of the automatic review):
queue_trace::pending_fences), and a place given up at present N is free once every batch submitted by present N + 4 has run;Not covered, and the module comment says so: a list recorded before a place is given up and handed to its queue more than four presents later. Only the list could hold what it binds, which belongs with the wiring.
Nothing calls it yet, so behaviour does not change. The file is marked
allow(dead_code)until the wiring PR, which removes the marker.Why the places are reused at all: a first version only ever took new places. ProjectScarlett builds its scenes again every frame, and in a local build that also wrote the structures the title builds while it runs (an experiment, not in these PRs) a 1,920 MiB buffer was full after about five minutes of the world; after that the walks read nothing new. With the reuse here, the same load held about 1,080 MiB after 16 minutes (one run, RTX 4070 Ti SUPER, before the rebase onto current main; the bookkeeping code is unchanged since). Both numbers are from that experiment, which writes hundreds of structures a frame. With what these PRs write (the rehydrated structures and the scenes), the buffer holds 84 to 126 MiB when the world starts and 140 to 223 MiB ten minutes later: it grows with the number of structures the title loads, not with the scenes written again every frame (the comment of 2026-11-30 has the runs).
TUXBOX_CONSOLE_HEAP_MB(1 to 4096, default 1024) sizes the buffer, and is indocs/ENVIRONMENT.md. A walk's offsets are 32 bits, so the buffer never passes 4 GiB.Tested
Run the checks relevant to this change.
mainon this machine (below)No Lanes scenario: nothing calls the buffer yet. The part that needs no device (the places, the clock they rest by, and what is named or held by what) has eighteen unit tests, among them a buffer written again every frame for 150 presents, ten times its size in all, that must not fill, and a place that waits for a GPU twenty frames behind. 32 errors planted one at a time (a place freed at once or never, free places not joined, the first fit taken instead of the smallest, a name counted twice, a scene that holds none of its structures or keeps holding them, a clock past a present that still waits and so on) each fail at least one test. The part that makes and maps the upload buffer has no test here; it ran in the game (below).
In the game, before the review fixes, with #536, the wiring and #566 on top: two 9-minute runs made the buffer (1,024 MiB), placed 256 and 485 structures by their last counts and 5 and 10 scenes in it (58 MiB held at most), and bound it to the walks. Both ended on a GPU loss a minute into the world, one in a walk given the buffer and one in mesh-shader draws; a run without the switch went 25 minutes. I can't tell yet whether the two are related; more runs come before the wiring PR does.
Before the rebase, from a local build that also has the wiring: a dump of the buffer taken in the world was walked on the CPU, 4,399,968 rays through the scenes it held. No ray failed to read what it followed, and none ended at another distance than in a scene written anew from the same records by #525's encoder: at that instant no place still in use had been written over.
GPU, driver and backend: none needed for the checks below. The game runs: 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, 351fbb4. The review fixes have not run in the game yet.
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.