hwrt: compact the BLASes built without update on the host (DARNER_HW_RT_COMPACT, off by default) #452

Open
Layne wants to merge 1 commit from Layne/Darner:pr/hwrt-blas-compact into Main
First-time contributor

Not run in the game on this branch. The numbers below come from a one-hour session on a fork of this tree that carried the same compaction together with other local changes (see "How it was tested"). The switch is off by default, and nothing changes while it is unset.

Contributor role (optional)

  • Fluxer ID: Layne#3304

What it changes

  • The problem on main: read from the code, and measured on a fork. Every BLAS keeps the buffer of its largest possible size (ResultDataMaxSizeInBytes), and the title's own compaction (its copy, mode 1) only makes a second console address point at the same host buffer (execute_copy). So no structure ever gets smaller. On an RTX 4070 Ti (12 GB), the compacted size the card reports for the BLASes built without update is about half of what they hold (numbers below).

With DARNER_HW_RT_COMPACT=1:

  • A BLAS the title builds with neither ALLOW_UPDATE nor PERFORM_UPDATE (static and rehydrated geometry, mostly) is built with ALLOW_COMPACTION. Unless its buffer is a single 64 KiB unit (it could not shrink), the list that builds it asks the card for its compacted size with EmitRaytracingAccelerationStructurePostbuildInfo. The size is copied to a readback buffer and read when that list is reset after its last execution, the same way as the TLAS marks (Feedback).
  • A BLAS that would save at least one whole 64 KiB unit waits 120 frames. Then, at most 16 of them (8 MB of source buffers) a frame, in blas_work ahead of that list's builds, it is compacted, but only if all of these still hold under the lookup table's write lock: the table still points at its buffer, nothing has been built into that buffer since (a per-structure generation counter, bumped in emit_blas_wave), and nothing but the table holds it. A smaller buffer is made, CopyRaytracingAccelerationStructure(..., COPY_MODE_COMPACT) is recorded, and every console address that pointed at the old buffer points at the new one.
  • Top levels get the new buffer only once the copying list is submitted (shown, the existing path that already keeps TLASes from naming structures whose build was not submitted). A TLAS built before keeps the old buffer through BlasRefs until it is built again. The old buffer is released past the fences when its last holder goes, and its heap units go with it.
  • An address that the title builds again over a compacted buffer (which never fits a full build) is not compacted again (counted as addresses built again after compaction).
  • Each structure keeps the console addresses that were given to it (HostAs::keys, written under the table's write lock). That way the compaction does not walk the whole lookup table under its write lock: it checks only the addresses that may name the structure. An address missing from that list makes the holder count larger than expected, so that structure is skipped rather than compacted.
  • One line every five seconds: hardware RT BLAS compaction (...), giving the candidates, the sizes read, how many were compacted (MB before and after, and 64 KB units given back), and why the others were dropped.

Only structures that are never refitted and never serve as a refit's source are compacted. So no refit ever reads a buffer whose copy is recorded but not yet submitted. Unset, nothing of this runs: no extra flag, no postbuild query, no work per frame.

Relation to other pull requests

  • #318 (segregate BLAS heaps by size class, prefer fullest-fit, release empty heaps): complementary, and the two do not touch the same functions. This one frees units inside the heaps (and committed buffers). On main, heaps are kept for the whole run, so freed units only go to later BLASes, which means fewer new heaps but no heap given back. With #318, heaps that the compaction empties can be released. The new buffers come from create_blas_buffer, whose signature #318 keeps. Expect at most a trivial rebase in the tests module.
  • #268 (BLAS TDR bursts, ArenaSlot UAF, in-flight HostAs reuse): not needed. This change checks the generation counter rather than relying on lists holding Arc<HostAs>. If #268 lands, lists hold the structures they build until reset, and the "held by nothing but the table" check simply gets stricter.
  • #446 (a refit reads only a source whose build has been submitted, DARNER_HW_RT_REFIT_BUILT): compatible. The compaction never reads a source whose build is not finished. It copies only after the compacted size of that very build has been read back, which happens when the building list is reset after its last execution, and only if the generation counter shows no build into the buffer since. Compacted copies are never refit sources (no ALLOW_UPDATE). Expect a textual conflict: both add a field to HostAs and initialise it in the same constructors. On rebase, the compacted HostAs should get built: true (or be set at the copying list's submission; it is never a refit source either way).
  • #445 (refittable BLASes take committed buffers, DARNER_HW_RT_REFIT_COMMITTED): compatible. Compaction only takes BLASes built without update, which stay in the shared heaps under #445 too. Textual conflict: #445 adds a refittable argument to create_blas_buffer, and this PR calls it once more for the compacted buffer. On rebase, pass false there.
  • Follow-up in a separate pull request, stacked on this one: "BLAS heaps emptied then given back" (DARNER_HW_RT_HEAP_EVACUATE, branch pr/hwrt-heap-evacuate).

How it was tested

  • Tested revision: 15498a839, on top of main 47d21cd3b.

  • Commands and results:

    • cargo check --release -p darner-gpu: finished, no warnings
    • cargo test --release -p darner-gpu --lib hwrt: 31 passed, 0 failed (6 of them new)
    • New unit tests: which builds are candidates (compact_candidate), when a compaction is worth it (a whole 64 KiB unit saved), that a buffer of one unit or less is never asked its compacted size, the per-frame share (count and bytes, at least one), and the wait/retry queue (120-frame wait, once a frame, retries, give up after 8 tries).
  • Changes a default: no (off unless DARNER_HW_RT_COMPACT=1).

  • How the switch was checked: in a one-hour session on an RTX 4070 Ti 12 GB, Series X profile, hardware RT on (DARNER_HW_RT=1), 30 fps cap (33.4 ms/frame median at the end of the run, 0 frames over 50 ms in the last interval). This was on a fork that also carried other memory changes, including a BLAS heap packing and release that main does not have. The compaction line at the end of the hour:

    • candidates (sizes asked) 200,187; sizes read 200,187; compacted 26,541
    • 10,499 MB of buffers before, 5,422 MB after (a sum over the hour as structures came and went, not what was live at one time); 5,338 MB of 64 KB units given back
    • saving under one unit 146,396 (the fork asked every candidate; this branch no longer asks for buffers of one unit or less)
    • dropped before their turn: gone 25,490, built again in place 1,760, held too long 0, size unusable 0, no buffer 0
    • addresses built again after compaction 5,284 (1,056 not compacted again)
    • No device loss and no TDR during the hour. One earlier session on the same fork crashed at about 40 minutes. The cause was a use-after-free of host textures in the resource-release thread, unrelated to this change, and it was fixed separately.

    No A/B (off and on, same route) has been run on this branch yet:

    A (off) B (on)
    card ledger accel, MB not measured on this branch not measured on this branch
  • Gates run on that revision: none of the three full gates. Only the narrower checks: the two above and cargo clippy -p darner-gpu --all-targets --locked -- -D warnings (clean).

  • Gameplay: RTX 4070 Ti 12 GB, Series X profile, 1440p, DLSS Quality, frame generation x2, hardware RT, frame interval 33.3 ms (fork session above). The log has no device-removed line.

  • Hardware dependence: compacted sizes are the driver's. The saving may differ on AMD or Intel cards, and on other NVIDIA drivers.

  • Coverage gaps: the GPU path (ask_compacted_sizes, compact_work) has no unit test, since it needs a device. It has not been run with the D3D12 debug layer or GPU-based validation on this branch.

Risks

  • Builds that get ALLOW_COMPACTION added may build slightly slower, and the driver may report a different ResultDataMaxSizeInBytes for them. The flag is added only with the switch on.
  • Up to 8 MB of BLAS copies a frame on the card, plus one small postbuild query and readback per wave that has candidates.
  • CPU per frame with the switch on: one look at up to 64 waiting entries under the lookup table's write lock (per structure, not per table entry), and one pass over shown when the copying list is submitted.
  • A list that records copies but is never submitted would leave the lookup table pointing at a buffer that was never written. Top levels would not use it (they read shown, which is swapped only on submission), and it is never refitted or compacted again. The next full build at that address replaces it. The only cost is memory until then.

How to test

  1. Build as usual, then run with DARNER_HW_RT=1 DARNER_HW_RT_COMPACT=1.
  2. In the log, look for the hardware RT BLAS compaction line every five seconds: compacted should grow after the first 120 frames of a scene with static geometry.
  3. A/B: play the same route with the switch unset and then set. Compare the card ledger kinds line (accel), the hardware RT BLAS buffers line (MB in use) and the frame times. Watch for device loss, TDRs, or missing or flickering geometry in ray-traced shadows and reflections.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only). Run on a fork with this change, not on this branch; see above.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #318 (BLAS heaps: size classes, fullest-fit, empty heaps released), #268 (BLAS TDR bursts, ArenaSlot UAF, in-flight HostAs reuse), #445 (refittable BLASes in committed buffers), #446 (refits only from submitted sources)
  • Replaces or overlaps: none found

🤖 Generated with Claude Code

**Not run in the game on this branch.** The numbers below come from a one-hour session on a fork of this tree that carried the same compaction together with other local changes (see "How it was tested"). The switch is off by default, and nothing changes while it is unset. ## Contributor role (optional) - Fluxer ID: Layne#3304 ## What it changes - The problem on main: read from the code, and measured on a fork. Every BLAS keeps the buffer of its largest possible size (`ResultDataMaxSizeInBytes`), and the title's own compaction (its copy, mode 1) only makes a second console address point at the same host buffer (`execute_copy`). So no structure ever gets smaller. On an RTX 4070 Ti (12 GB), the compacted size the card reports for the BLASes built without update is about half of what they hold (numbers below). With `DARNER_HW_RT_COMPACT=1`: - A BLAS the title builds with neither `ALLOW_UPDATE` nor `PERFORM_UPDATE` (static and rehydrated geometry, mostly) is built with `ALLOW_COMPACTION`. Unless its buffer is a single 64 KiB unit (it could not shrink), the list that builds it asks the card for its compacted size with `EmitRaytracingAccelerationStructurePostbuildInfo`. The size is copied to a readback buffer and read when that list is reset after its last execution, the same way as the TLAS marks (`Feedback`). - A BLAS that would save at least one whole 64 KiB unit waits 120 frames. Then, at most 16 of them (8 MB of source buffers) a frame, in `blas_work` ahead of that list's builds, it is compacted, but only if all of these still hold under the lookup table's write lock: the table still points at its buffer, nothing has been built into that buffer since (a per-structure `generation` counter, bumped in `emit_blas_wave`), and nothing but the table holds it. A smaller buffer is made, `CopyRaytracingAccelerationStructure(..., COPY_MODE_COMPACT)` is recorded, and every console address that pointed at the old buffer points at the new one. - Top levels get the new buffer only once the copying list is submitted (`shown`, the existing path that already keeps TLASes from naming structures whose build was not submitted). A TLAS built before keeps the old buffer through `BlasRefs` until it is built again. The old buffer is released past the fences when its last holder goes, and its heap units go with it. - An address that the title builds again over a compacted buffer (which never fits a full build) is not compacted again (counted as `addresses built again after compaction`). - Each structure keeps the console addresses that were given to it (`HostAs::keys`, written under the table's write lock). That way the compaction does not walk the whole lookup table under its write lock: it checks only the addresses that may name the structure. An address missing from that list makes the holder count larger than expected, so that structure is skipped rather than compacted. - One line every five seconds: `hardware RT BLAS compaction (...)`, giving the candidates, the sizes read, how many were compacted (MB before and after, and 64 KB units given back), and why the others were dropped. Only structures that are never refitted and never serve as a refit's source are compacted. So no refit ever reads a buffer whose copy is recorded but not yet submitted. Unset, nothing of this runs: no extra flag, no postbuild query, no work per frame. ### Relation to other pull requests - **#318** (segregate BLAS heaps by size class, prefer fullest-fit, release empty heaps): complementary, and the two do not touch the same functions. This one frees units inside the heaps (and committed buffers). On `main`, heaps are kept for the whole run, so freed units only go to later BLASes, which means fewer new heaps but no heap given back. With #318, heaps that the compaction empties can be released. The new buffers come from `create_blas_buffer`, whose signature #318 keeps. Expect at most a trivial rebase in the `tests` module. - **#268** (BLAS TDR bursts, ArenaSlot UAF, in-flight HostAs reuse): not needed. This change checks the `generation` counter rather than relying on lists holding `Arc<HostAs>`. If #268 lands, lists hold the structures they build until reset, and the "held by nothing but the table" check simply gets stricter. - **#446** (a refit reads only a source whose build has been submitted, `DARNER_HW_RT_REFIT_BUILT`): compatible. The compaction never reads a source whose build is not finished. It copies only after the compacted size of that very build has been read back, which happens when the building list is reset after its last execution, and only if the `generation` counter shows no build into the buffer since. Compacted copies are never refit sources (no `ALLOW_UPDATE`). Expect a textual conflict: both add a field to `HostAs` and initialise it in the same constructors. On rebase, the compacted `HostAs` should get `built: true` (or be set at the copying list's submission; it is never a refit source either way). - **#445** (refittable BLASes take committed buffers, `DARNER_HW_RT_REFIT_COMMITTED`): compatible. Compaction only takes BLASes built without update, which stay in the shared heaps under #445 too. Textual conflict: #445 adds a `refittable` argument to `create_blas_buffer`, and this PR calls it once more for the compacted buffer. On rebase, pass `false` there. - Follow-up in a separate pull request, stacked on this one: "BLAS heaps emptied then given back" (`DARNER_HW_RT_HEAP_EVACUATE`, branch `pr/hwrt-heap-evacuate`). ## How it was tested - Tested revision: 15498a839, on top of `main` 47d21cd3b. - Commands and results: - `cargo check --release -p darner-gpu`: finished, no warnings - `cargo test --release -p darner-gpu --lib hwrt`: 31 passed, 0 failed (6 of them new) - New unit tests: which builds are candidates (`compact_candidate`), when a compaction is worth it (a whole 64 KiB unit saved), that a buffer of one unit or less is never asked its compacted size, the per-frame share (count and bytes, at least one), and the wait/retry queue (120-frame wait, once a frame, retries, give up after 8 tries). - Changes a default: no (off unless `DARNER_HW_RT_COMPACT=1`). - How the switch was checked: in a one-hour session on an RTX 4070 Ti 12 GB, Series X profile, hardware RT on (`DARNER_HW_RT=1`), 30 fps cap (33.4 ms/frame median at the end of the run, 0 frames over 50 ms in the last interval). This was on a fork that also carried other memory changes, including a BLAS heap packing and release that `main` does not have. The compaction line at the end of the hour: - candidates (sizes asked) 200,187; sizes read 200,187; compacted 26,541 - 10,499 MB of buffers before, 5,422 MB after (a sum over the hour as structures came and went, not what was live at one time); 5,338 MB of 64 KB units given back - saving under one unit 146,396 (the fork asked every candidate; this branch no longer asks for buffers of one unit or less) - dropped before their turn: gone 25,490, built again in place 1,760, held too long 0, size unusable 0, no buffer 0 - addresses built again after compaction 5,284 (1,056 not compacted again) - No device loss and no TDR during the hour. One earlier session on the same fork crashed at about 40 minutes. The cause was a use-after-free of host textures in the resource-release thread, unrelated to this change, and it was fixed separately. No A/B (off and on, same route) has been run on this branch yet: | | A (off) | B (on) | |---|---|---| | card ledger `accel`, MB | not measured on this branch | not measured on this branch | - Gates run on that revision: none of the three full gates. Only the narrower checks: the two above and `cargo clippy -p darner-gpu --all-targets --locked -- -D warnings` (clean). - Gameplay: RTX 4070 Ti 12 GB, Series X profile, 1440p, DLSS Quality, frame generation x2, hardware RT, frame interval 33.3 ms (fork session above). The log has no device-removed line. - Hardware dependence: compacted sizes are the driver's. The saving may differ on AMD or Intel cards, and on other NVIDIA drivers. - Coverage gaps: the GPU path (`ask_compacted_sizes`, `compact_work`) has no unit test, since it needs a device. It has not been run with the D3D12 debug layer or GPU-based validation on this branch. ### Risks - Builds that get `ALLOW_COMPACTION` added may build slightly slower, and the driver may report a different `ResultDataMaxSizeInBytes` for them. The flag is added only with the switch on. - Up to 8 MB of BLAS copies a frame on the card, plus one small postbuild query and readback per wave that has candidates. - CPU per frame with the switch on: one look at up to 64 waiting entries under the lookup table's write lock (per structure, not per table entry), and one pass over `shown` when the copying list is submitted. - A list that records copies but is never submitted would leave the lookup table pointing at a buffer that was never written. Top levels would not use it (they read `shown`, which is swapped only on submission), and it is never refitted or compacted again. The next full build at that address replaces it. The only cost is memory until then. ## How to test 1. Build as usual, then run with `DARNER_HW_RT=1 DARNER_HW_RT_COMPACT=1`. 2. In the log, look for the `hardware RT BLAS compaction` line every five seconds: `compacted` should grow after the first 120 frames of a scene with static geometry. 3. A/B: play the same route with the switch unset and then set. Compare the `card ledger kinds` line (`accel`), the `hardware RT BLAS buffers` line (MB in use) and the frame times. Watch for device loss, TDRs, or missing or flickering geometry in ray-traced shadows and reflections. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (docs, tests only). *Run on a fork with this change, not on this branch; see above.* - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [ ] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #318 (BLAS heaps: size classes, fullest-fit, empty heaps released), #268 (BLAS TDR bursts, ArenaSlot UAF, in-flight HostAs reuse), #445 (refittable BLASes in committed buffers), #446 (refits only from submitted sources) - Replaces or overlaps: none found 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A BLAS keeps the buffer of its largest possible size; the title's own
compaction only aliases a second console address to the same host buffer.
With DARNER_HW_RT_COMPACT=1, BLASes built with neither ALLOW_UPDATE nor
PERFORM_UPDATE get ALLOW_COMPACTION, their compacted size is read back once
the building list is reset, and after 120 unchanged frames those that save a
whole 64 KiB unit are copied (COPY_MODE_COMPACT) into a buffer of that size,
at most 16 (8 MB) a frame. Every console address naming the old buffer names
the new one (per-structure address list, no table walk); top levels get it
from the copying list's submission (shown); the old buffer retires past the
fences with its last holder. A build in place is caught by a per-structure
generation counter, read after the holder count behind an Acquire fence.
Never refitted structures only, so no refit reads an unsubmitted copy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TrustNo1 changed title from [pr-feature][memory][RAYTRACING] hwrt: compact the BLASes built without update on the host (DARNER_HW_RT_COMPACT, off by default) to hwrt: compact the BLASes built without update on the host (DARNER_HW_RT_COMPACT, off by default) 2026-12-06 03:28:51 +00:00
Owner

Test needed — commit 7d95441b; what to check:

  1. Build as usual, then run with DARNER_HW_RT=1 DARNER_HW_RT_COMPACT=1.
  2. In the log, look for the hardware RT BLAS compaction line every five seconds: compacted should grow after the first 120 frames of a scene with static geometry.
  3. A/B: play the same route with the switch unset and then set. Compare the card ledger kinds line (accel), the hardware RT BLAS buffers line (MB in use) and the frame times. Watch for device loss, TDRs, or missing or flickering geometry in ray-traced shadows and reflections.

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `7d95441b`; what to check: 1. Build as usual, then run with `DARNER_HW_RT=1 DARNER_HW_RT_COMPACT=1`. 2. In the log, look for the `hardware RT BLAS compaction` line every five seconds: `compacted` should grow after the first 120 frames of a scene with static geometry. 3. A/B: play the same route with the switch unset and then set. Compare the `card ledger kinds` line (`accel`), the `hardware RT BLAS buffers` line (MB in use) and the frame times. Watch for device loss, TDRs, or missing or flickering geometry in ray-traced shadows and reflections. Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
This pull request has changes conflicting with the target branch.
  • crates/darner-gpu/src/hwrt.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'pr/hwrt-blas-compact:Layne-pr/hwrt-blas-compact'
git switch 'Layne-pr/hwrt-blas-compact'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'Main'
git merge --no-ff 'Layne-pr/hwrt-blas-compact'
git switch 'Layne-pr/hwrt-blas-compact'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'Layne-pr/hwrt-blas-compact'
git switch 'Layne-pr/hwrt-blas-compact'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'Layne-pr/hwrt-blas-compact'
git switch 'Main'
git merge --squash 'Layne-pr/hwrt-blas-compact'
git switch 'Main'
git merge --ff-only 'Layne-pr/hwrt-blas-compact'
git switch 'Main'
git merge 'Layne-pr/hwrt-blas-compact'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!452
No description provided.