d3d12: release the scratch pages past a stored texture snapshot #324

Merged
proof-by-vibes merged 2 commits from upstream-pr/324 into main 2026-11-27 19:35:32 +00:00
proof-by-vibes commented 2026-11-27 18:58:32 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by proof-by-vibes on 2026-11-27T18:58:32Z.


Summary

Upload compares every placed texture's mips through one scratch buffer, Transfer::scratch. snapshot_mip reserves the mip's size, which never shrinks the buffer, and writes the mip into it. After a large mip, the buffer keeps that capacity with its pages touched. When a mip has changed, the buffer moves into Contents::previous as its snapshot, and the snapshot it replaces becomes the next scratch. Snapshots live as long as their texture, so small mips end up holding the resident pages of the largest mip their buffer ever compared.

Snapshots are now stored through Contents::store. When at least 64 KiB of whole pages lie past the snapshot's bytes, it resets them with VirtualAlloc(MEM_RESET). Nothing is copied: the Vec still owns the memory and can grow back into it, and only the contents past its length, which it never reads, are discarded.

shrink_to_fit doesn't help here. Wine shrinks a large heap block in place, and its pages stay resident. A standalone probe under GE-Proton11-14 filled 64 scratch buffers of 16 MiB each and kept 4 KiB of each, reading the process's RssAnon from Linux:

Kept as RssAnon growth
The buffer as it is +1,024 MiB
truncate + shrink_to_fit +1,024 MiB (capacity 4 KiB, same 16 MiB region still committed)
MEM_RESET on the pages past the length +1 MiB

Measured in ProjectScarlett

ProjectScarlett 1.0.2.1 on an RX 7900 XTX. The runs were made before #258 was merged, with #258 at its earlier head d50a51c on an older main. #258 backs guest memory with MEM_PRIVATE, which is what makes the effect show in RssAnon. In the upload burst as the world streams in, the RssAnon the burst left behind was:

Build Left behind by the burst
Without this change (3 runs) +6.35, +6.5 and +6.8 GiB
With this change (1 run) +2.35 GiB

The burst still peaks briefly, at about the same height as before, because comparing still fills the scratch buffers. The pages are released when the snapshots are stored, within one 5 s sample. A local trace of snapshot capacity, not included here, had measured 11.6 GiB held with 6.8 GiB of it past the snapshots' lengths in a run without this change.

Not yet repeated: the game runs on the current main, which now includes #258.

Tested

Run the checks relevant to this change.

  • 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: NVIDIA still needs to run them.

No Lanes scenario: the snapshots are the provider's own host memory, which a title can't observe. Unit tests in src/tests/device/placement/contents.rs cover the change instead. a_stored_snapshot_releases_the_scratch_pages_past_its_bytes checks with QueryWorkingSetEx that a page past the snapshot is no longer resident, and it fails without the change. a_released_snapshot_can_grow_back checks that a reused buffer keeps its bytes and refills the released pages. store_returns_the_replaced_snapshot_for_reuse checks the scratch handover.

GPU, driver and backend: AMD Radeon RX 7900 XTX (1002:744c, RADV NAVI31), Mesa 26.1.8, Linux 6.18.57, GE-Proton11-14 (proton backend)

Commands run and their results. The full check ran on 07db0df, before main (with #258) was merged into this branch. The unit tests ran again on the merged head ba78020.

$ python tuxbox.py check --adapter 1002:744c --proton GE-Proton11-14 --prefix PREFIX --windows-dxc DXC/bin/x64
  (inside steam-run on NixOS, so the isolated controls can start Proton's Wine)
  passed   Build the shader compiler
  passed   Python tests
  passed   Shader compiler tests
  passed   DXBC tests
  passed   XG layout tests
  passed   D3D12 Windows typecheck
  passed   Build
  passed   Lanes and fixtures on the GPU        98/98
  passed   D3D12 regression controls            20/20, outputs unchanged
  passed   Shader translator on the GPU         149/149
All checks passed.

$ tuxbox-d3d12 lib tests under GE-Proton11-14, filter device::placement   (on 07db0df)
test result: ok. 12 passed; 0 failed
  (a_stored_snapshot_releases_the_scratch_pages_past_its_bytes fails without the change:
   "the scratch pages past the snapshot's bytes stay resident")

$ tuxbox-d3d12 lib tests under GE-Proton11-14, filters device::placement and device::heap::backing   (on ba78020)
test result: ok. 28 passed; 0 failed

Checks

CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, and a Windows typecheck of tuxbox-d3d12.

No evidence JSON changes.

<!-- greyfinch-sync:brcly/TuxBox:issue:5531718426 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/324). Originally posted by [proof-by-vibes](https://github.com/proof-by-vibes) on 2026-11-27T18:58:32Z. --- ## Summary Upload compares every placed texture's mips through one scratch buffer, `Transfer::scratch`. `snapshot_mip` reserves the mip's size, which never shrinks the buffer, and writes the mip into it. After a large mip, the buffer keeps that capacity with its pages touched. When a mip has changed, the buffer moves into `Contents::previous` as its snapshot, and the snapshot it replaces becomes the next scratch. Snapshots live as long as their texture, so small mips end up holding the resident pages of the largest mip their buffer ever compared. Snapshots are now stored through `Contents::store`. When at least 64 KiB of whole pages lie past the snapshot's bytes, it resets them with `VirtualAlloc(MEM_RESET)`. Nothing is copied: the `Vec` still owns the memory and can grow back into it, and only the contents past its length, which it never reads, are discarded. `shrink_to_fit` doesn't help here. Wine shrinks a large heap block in place, and its pages stay resident. A standalone probe under GE-Proton11-14 filled 64 scratch buffers of 16 MiB each and kept 4 KiB of each, reading the process's `RssAnon` from Linux: | Kept as | `RssAnon` growth | | --- | --- | | The buffer as it is | +1,024 MiB | | `truncate` + `shrink_to_fit` | +1,024 MiB (capacity 4 KiB, same 16 MiB region still committed) | | `MEM_RESET` on the pages past the length | +1 MiB | ### Measured in ProjectScarlett ProjectScarlett 1.0.2.1 on an RX 7900 XTX. The runs were made before #258 was merged, with #258 at its earlier head `d50a51c` on an older `main`. #258 backs guest memory with `MEM_PRIVATE`, which is what makes the effect show in `RssAnon`. In the upload burst as the world streams in, the `RssAnon` the burst left behind was: | Build | Left behind by the burst | | --- | --- | | Without this change (3 runs) | +6.35, +6.5 and +6.8 GiB | | With this change (1 run) | +2.35 GiB | The burst still peaks briefly, at about the same height as before, because comparing still fills the scratch buffers. The pages are released when the snapshots are stored, within one 5 s sample. A local trace of snapshot capacity, not included here, had measured 11.6 GiB held with 6.8 GiB of it past the snapshots' lengths in a run without this change. Not yet repeated: the game runs on the current `main`, which now includes #258. ## Tested Run the [checks relevant to this change](../CONTRIBUTING.md#test-what-your-change-touches). - [x] 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: **NVIDIA still needs to run them.** **No Lanes scenario:** the snapshots are the provider's own host memory, which a title can't observe. Unit tests in `src/tests/device/placement/contents.rs` cover the change instead. `a_stored_snapshot_releases_the_scratch_pages_past_its_bytes` checks with `QueryWorkingSetEx` that a page past the snapshot is no longer resident, and it fails without the change. `a_released_snapshot_can_grow_back` checks that a reused buffer keeps its bytes and refills the released pages. `store_returns_the_replaced_snapshot_for_reuse` checks the scratch handover. GPU, driver and backend: AMD Radeon RX 7900 XTX (`1002:744c`, RADV NAVI31), Mesa 26.1.8, Linux 6.18.57, GE-Proton11-14 (`proton` backend) Commands run and their results. The full check ran on `07db0df`, before `main` (with #258) was merged into this branch. The unit tests ran again on the merged head `ba78020`. ```text $ python tuxbox.py check --adapter 1002:744c --proton GE-Proton11-14 --prefix PREFIX --windows-dxc DXC/bin/x64 (inside steam-run on NixOS, so the isolated controls can start Proton's Wine) passed Build the shader compiler passed Python tests passed Shader compiler tests passed DXBC tests passed XG layout tests passed D3D12 Windows typecheck passed Build passed Lanes and fixtures on the GPU 98/98 passed D3D12 regression controls 20/20, outputs unchanged passed Shader translator on the GPU 149/149 All checks passed. $ tuxbox-d3d12 lib tests under GE-Proton11-14, filter device::placement (on 07db0df) test result: ok. 12 passed; 0 failed (a_stored_snapshot_releases_the_scratch_pages_past_its_bytes fails without the change: "the scratch pages past the snapshot's bytes stay resident") $ tuxbox-d3d12 lib tests under GE-Proton11-14, filters device::placement and device::heap::backing (on ba78020) test result: ok. 28 passed; 0 failed ``` ## Checks CI runs the Python unit tests, the shader compiler, DXBC, and XG layout tests, and a Windows typecheck of `tuxbox-d3d12`. No evidence JSON changes.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
brcly/TuxBox!324
No description provided.