D3D12.X: free buffer mirrors no draw used for 10 seconds #819

Open
Cattn wants to merge 1 commit from upstream-pr/825 into main
Cattn commented 2026-11-24 06:12:17 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Cattn on 2026-11-24T06:12:17Z.


A buffer view outside unified memory gets a mirror that each draw copies what it reads into. Mirrors are never freed, so a title that binds ring buffers at new addresses while it streams its world keeps making them. On a retail title they reached 925 MB at 74 s, on the way to the 2 GiB cap. That memory is both system commit and, through the sparse pools, video memory, and on a 32 GB / 12 GB machine it is part of what runs the process out of memory during world loading.

What is in it

  • crates/xcl/src/hle/d3d12x/list_x.rs: each mirror records when a lookup last used it. When a new mirror is made, at most once a second, mirrors unused for 10 s are taken out of the table and their sparse or coherent memory is freed. A range bound again later gets a new mirror, filled from the title's memory as before. A unit test covers the lookup marking a mirror used and the idle sweep.

Tested

  • NVIDIA GeForce RTX 4070 (driver 32.0.16.1742), AMD Ryzen 7 7800X3D, Windows 11 10.0.26200, rustc 1.93.1, debug tests and release smoke builds
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on b12bf42 (rebased on beaa245): 714 passed, 0 failed, 41 ignored
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)

Regression run

main:

xcl-smoke v1
commit: beaa245ca04e212f064efc7cafcdce104bc5c0a2
gpu: NVIDIA GeForce RTX 4070
title: 7f4b2c91
seconds: 60
exit: 0x0
frames shown: 1516 (PresentX 1516)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16272 MB

this PR:

xcl-smoke v1
commit: b12bf42d76951c08f35c4b34d11f3aa13a53504e
gpu: NVIDIA GeForce RTX 4070
title: 7f4b2c91
seconds: 60
exit: 0x0
frames shown: 1550 (PresentX 1551)
presents per 2 s (last 3): 60 49 44
device removals: 0
crashes: 0
peak commit: 18262 MB

Not tested

  • A full world-loading run with this change alone: the 60 s smoke run ends before the title streams its world, where mirrors pile up, so it shows no regression but not the saving. The 925 MB figure is from a longer run without this change.
  • AMD and Intel GPUs, wave64 hardware.
  • Command lists recorded once and executed again more than 10 s later with a mirrored buffer bound would read a freed mirror (zeros for a freed sparse range). I did not find such a case in the title I ran.

AI use

  • Claude Code with Claude Opus 5.5: found the growth in the logs, wrote the change, the test and this description. It also ran the tests and smoke runs on the hardware above. A person has not yet reviewed the diff line by line.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:5467107554 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/825). Originally posted by [Cattn](https://github.com/Cattn) on 2026-11-24T06:12:17Z. --- A buffer view outside unified memory gets a mirror that each draw copies what it reads into. Mirrors are never freed, so a title that binds ring buffers at new addresses while it streams its world keeps making them. On a retail title they reached 925 MB at 74 s, on the way to the 2 GiB cap. That memory is both system commit and, through the sparse pools, video memory, and on a 32 GB / 12 GB machine it is part of what runs the process out of memory during world loading. **What is in it** - `crates/xcl/src/hle/d3d12x/list_x.rs`: each mirror records when a lookup last used it. When a new mirror is made, at most once a second, mirrors unused for 10 s are taken out of the table and their sparse or coherent memory is freed. A range bound again later gets a new mirror, filled from the title's memory as before. A unit test covers the lookup marking a mirror used and the idle sweep. **Tested** - NVIDIA GeForce RTX 4070 (driver 32.0.16.1742), AMD Ryzen 7 7800X3D, Windows 11 10.0.26200, rustc 1.93.1, debug tests and release smoke builds - `cargo test --workspace --no-fail-fast -- --test-threads=4` on b12bf42 (rebased on beaa245): 714 passed, 0 failed, 41 ignored - SKIPPED: `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware` (no wave64) **Regression run** main: ``` xcl-smoke v1 commit: beaa245ca04e212f064efc7cafcdce104bc5c0a2 gpu: NVIDIA GeForce RTX 4070 title: 7f4b2c91 seconds: 60 exit: 0x0 frames shown: 1516 (PresentX 1516) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 16272 MB ``` this PR: ``` xcl-smoke v1 commit: b12bf42d76951c08f35c4b34d11f3aa13a53504e gpu: NVIDIA GeForce RTX 4070 title: 7f4b2c91 seconds: 60 exit: 0x0 frames shown: 1550 (PresentX 1551) presents per 2 s (last 3): 60 49 44 device removals: 0 crashes: 0 peak commit: 18262 MB ``` **Not tested** - A full world-loading run with this change alone: the 60 s smoke run ends before the title streams its world, where mirrors pile up, so it shows no regression but not the saving. The 925 MB figure is from a longer run without this change. - AMD and Intel GPUs, wave64 hardware. - Command lists recorded once and executed again more than 10 s later with a mirrored buffer bound would read a freed mirror (zeros for a freed sparse range). I did not find such a case in the title I ran. **AI use** - Claude Code with Claude Opus 5.5: found the growth in the logs, wrote the change, the test and this description. It also ran the tests and smoke runs on the hardware above. A person has not yet reviewed the diff line by line. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
dcastell1 commented 2026-11-24 07:06:52 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by dcastell1 on 2026-11-24T06:50:48Z.


Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, applied on main@beaa245:

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
test result: 738 passed; 0 failed; 41 ignored   (20 blocks, exit 0)

That is one more passing test than main on the same box (737), so the idle-mirror sweep (free buffer mirrors no draw used for 10 s) runs green here and nothing else in the workspace changes verdict. No game data, hardware adapter, no Wine.

AI-assisted (opencode): applied the patch to a built tree, ran the suite, restored the two files.

<!-- greyfinch-sync:ivall/xcl:comment:7124350540 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/825#issuecomment-7124350540). Originally posted by [dcastell1](https://github.com/dcastell1) on 2026-11-24T06:50:48Z. --- Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, applied on `main@beaa245`: ``` patch -p1 < this PR # clean, no fuzz cargo test --workspace -- --test-threads=4 test result: 738 passed; 0 failed; 41 ignored (20 blocks, exit 0) ``` That is one more passing test than `main` on the same box (737), so the idle-mirror sweep (free buffer mirrors no draw used for 10 s) runs green here and nothing else in the workspace changes verdict. No game data, hardware adapter, no Wine. AI-assisted (opencode): applied the patch to a built tree, ran the suite, restored the two files.
SlopTart commented 2026-11-24 08:03:45 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-24T07:17:54Z.


Linux / AMD RDNA3 datapoint.

Merged state: head b12bf42 onto main 1ec4c98, clean merge.

Full suite, cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 706 passed, 10 failed, 41 ignored. Failures are the known Wine-lane set from my #42 rows plus #718 trio members; nothing here fails because of your change. Unused buffer mirror reclaim tests green; no new failures.

Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

<!-- greyfinch-sync:ivall/xcl:comment:7124835706 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/825#issuecomment-7124835706). Originally posted by [SlopTart](https://github.com/SlopTart) on 2026-11-24T07:17:54Z. --- Linux / AMD RDNA3 datapoint. **Merged state:** head `b12bf42` onto main `1ec4c98`, clean merge. **Full suite, `cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4`:** **706 passed, 10 failed, 41 ignored**. Failures are the known Wine-lane set from my #42 rows plus #718 trio members; nothing here fails because of your change. Unused buffer mirror reclaim tests green; no new failures. **Not tested:** retail titles (none here), Windows, RDNA4. Counts only, no game data. **Machine:** RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross `x86_64-pc-windows-gnu`, `XCL_NETWORK=0`, release profile, `--test-threads=4`. Linux rows: #42 / #718. AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.
iggyvilla commented 2026-11-24 08:03:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by iggyvilla on 2026-11-24T07:34:41Z.


Review of b12bf42 (read only, nothing built or run by me): I think the 10 s idle rule can free a mirror that a recorded list still binds.

What the timer measures. A mirror is stamped only by find_mirror, which runs at record time: gpu_va -> mirror_of (list_x.rs:3066) when a buffer is bound, and sync_title_buffers -> mirrored (:3199-3200) at every draw. What the PC list holds is the mirror's GPU address (pc_va(p) at :3067), so the GPU reads the mirror when the list executes, not when it is recorded. "Not looked up for 10 s" therefore means "not recorded against for 10 s", not "no GPU work uses it".

Where it frees. mirror_of takes the idle entries out of the table and free_mirrors frees them straight away on the recording thread: u.free(p) for a coherent block, sparse::free(p) for a sparse one. There is no fence or allocator check. So if a list that bound a mirror is submitted (or is still executing, or is executed again, which D3D12 allows for a closed list) more than 10 s after its last draw was recorded, and any other thread creates a new mirror in between, the GPU reads a mirror that was freed. For a coherent block that can be memory the next alloc has already handed out and overwritten, so the draw reads another range's bytes rather than zeros; the PR text only describes the sparse case.

I have not shown that the title does this (the retail runs in the PR may never record that far ahead), so I'd call it a hypothesis about a window, but the code has no protection against it. Two ways to close it that the tree already has: hold the freed blocks until the allocator of the lists that could bind them resets (console_list::hold_until_reset / allocator_reset, the lifetime #838 relies on for its arena), or stamp each mirror with the submission it was last bound in and free only below completed work. A test that binds a mirror in a closed list, lets it go idle and checks it is not freed would pin it; the current test covers only find_mirror and take_idle_mirrors.

Smaller points:

  • The sweep only runs after a lookup miss in mirror_of (:3150 returns on a hit first), so once the title stops binding new ranges nothing sweeps and the table stays at its peak. A sweep from submit/present, or when MIRROR_BYTES crosses a threshold, would also reclaim after streaming stops.
  • free_mirrors returns at let Some(u) = uma::get() else { return } after take_idle_mirrors already removed the entries, so their bytes stay in MIRROR_BYTES. Only matters at shutdown.

AI use: read with Claude Code, Claude Sonnet 5.5, against the PR head; the findings are mine and I checked each line reference.

<!-- greyfinch-sync:ivall/xcl:comment:7125142076 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/825#issuecomment-7125142076). Originally posted by [iggyvilla](https://github.com/iggyvilla) on 2026-11-24T07:34:41Z. --- Review of `b12bf42` (read only, nothing built or run by me): I think the 10 s idle rule can free a mirror that a recorded list still binds. **What the timer measures.** A mirror is stamped only by `find_mirror`, which runs at record time: `gpu_va` -> `mirror_of` (`list_x.rs:3066`) when a buffer is bound, and `sync_title_buffers` -> `mirrored` (`:3199-3200`) at every draw. What the PC list holds is the mirror's GPU address (`pc_va(p)` at `:3067`), so the GPU reads the mirror when the list *executes*, not when it is recorded. "Not looked up for 10 s" therefore means "not recorded against for 10 s", not "no GPU work uses it". **Where it frees.** `mirror_of` takes the idle entries out of the table and `free_mirrors` frees them straight away on the recording thread: `u.free(p)` for a coherent block, `sparse::free(p)` for a sparse one. There is no fence or allocator check. So if a list that bound a mirror is submitted (or is still executing, or is executed again, which D3D12 allows for a closed list) more than 10 s after its last draw was recorded, and any other thread creates a new mirror in between, the GPU reads a mirror that was freed. For a coherent block that can be memory the next `alloc` has already handed out and overwritten, so the draw reads another range's bytes rather than zeros; the PR text only describes the sparse case. I have not shown that the title does this (the retail runs in the PR may never record that far ahead), so I'd call it a hypothesis about a window, but the code has no protection against it. Two ways to close it that the tree already has: hold the freed blocks until the allocator of the lists that could bind them resets (`console_list::hold_until_reset` / `allocator_reset`, the lifetime #838 relies on for its arena), or stamp each mirror with the submission it was last bound in and free only below completed work. A test that binds a mirror in a closed list, lets it go idle and checks it is not freed would pin it; the current test covers only `find_mirror` and `take_idle_mirrors`. Smaller points: - The sweep only runs after a lookup *miss* in `mirror_of` (`:3150` returns on a hit first), so once the title stops binding new ranges nothing sweeps and the table stays at its peak. A sweep from submit/present, or when `MIRROR_BYTES` crosses a threshold, would also reclaim after streaming stops. - `free_mirrors` returns at `let Some(u) = uma::get() else { return }` after `take_idle_mirrors` already removed the entries, so their bytes stay in `MIRROR_BYTES`. Only matters at shutdown. **AI use:** read with Claude Code, Claude Sonnet 5.5, against the PR head; the findings are mine and I checked each line reference.
DavidM1911 commented 2026-11-25 11:09:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T10:38:43Z.


Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

<!-- greyfinch-sync:ivall/xcl:comment:7159978688 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/825#issuecomment-7159978688). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T10:38:43Z. --- Verification on RTX 3060 Ti (Windows 11): **merge conflict with current `main`** — needs a rebase before it can be tested.
Foniass commented 2026-12-01 04:14:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T04:14:03Z.


Triage (#1607) – head b12bf42

No open or merged PR frees buffer mirrors; main still keeps them for the whole run (MIRRORS in list_x.rs).
Two concerns with mirror_of / take_idle_mirrors: a mirror counts as used only when a bind or a draw's sync looks it up, so a command list or bundle recorded once and executed again more than 10 s later still holds the freed mirror's address in its VBV/IBV, and nothing waits on a fence before uma.free / sparse::free. Also, main now caches mirrors per thread (MIRROR_LOOKUP snapshot keyed by MIRROR_GENERATION, documented as append-only). A rebase has to bump the generation on removal, or other threads keep returning the freed address. The branch conflicts with main.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7300518233 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/825#issuecomment-7300518233). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T04:14:03Z. --- **Triage (#1607)** – head b12bf42 No open or merged PR frees buffer mirrors; main still keeps them for the whole run (`MIRRORS` in list_x.rs). Two concerns with `mirror_of` / `take_idle_mirrors`: a mirror counts as used only when a bind or a draw's sync looks it up, so a command list or bundle recorded once and executed again more than 10 s later still holds the freed mirror's address in its VBV/IBV, and nothing waits on a fence before `uma.free` / `sparse::free`. Also, main now caches mirrors per thread (`MIRROR_LOOKUP` snapshot keyed by `MIRROR_GENERATION`, documented as append-only). A rebase has to bump the generation on removal, or other threads keep returning the freed address. The branch conflicts with main. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/825:upstream-pr/825'
git switch 'upstream-pr/825'

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 'upstream-pr/825'
git switch 'upstream-pr/825'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/825'
git switch 'upstream-pr/825'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/825'
git switch 'main'
git merge --squash 'upstream-pr/825'
git switch 'main'
git merge --ff-only 'upstream-pr/825'
git switch 'main'
git merge 'upstream-pr/825'
git push origin 'main'
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
xcl-dev/xcl!819
No description provided.