d3d12: forget write-combined and GPU read-only regions over a released allocation (D3D12-WC-REGIONS-ON-RELEASE) #2703

Closed
github-bot wants to merge 2 commits from github-bot/gh-2703 into main AGit
Owner

@⁠Kal0s-design on GitHub · 2026-11-30 09:01 UTC · original
Kal0s-design:fix/wc-regions-on-release → main · +72901 −8961 · 279 files · 264 commits · closed without merging

Claimed in the coordination issue as: D3D12-WC-REGIONS-ON-RELEASE (#2570)

Depends on: conflicts with #2569 at the top of title_memory_freed in watch.rs (#2569 already conflicts with main in other files). Whichever lands second needs a small rebase.

What and why

watch::title_memory_freed passes its own len to shadow::write_combined_freed. A release (XMemVirtualFree/VirtualFree with MEM_RELEASE) has len 0, and for 0 write_combined_freed drops only the regions whose start equals the allocation's base. A region recorded for something inside the allocation outlives the release:

  • a write-combined commit (0x400) inside a reservation that wasn't asked for write-combined;
  • a GPU read-only part. xmem.rs records these for the parts the title splits a placeholder into, and its 32-bit graphics heap comes that way.

After the release, a new allocation at those addresses counts as write-combined (LEONIDA_WC_NO_BACK, on in play-nvidia.ps1) or GPU read-only (LEONIDA_GPU_READ_ONLY_NO_BACK, on by default). Its shader writes are then left on the card and never come back to title memory, the one failure these switches must not cause.

The change: on a release, title_memory_freed forgets the regions over the allocation's own extent, allocation_end(at, at), which it already asks for the views just below while the memory is still there. A decommit keeps its length, and write_combined_freed itself is unchanged, so its existing tests keep their meaning.

I found the same gap while reviewing the write-combined port in TaynanGT/Vice-Tay#462 (the same code from trustno1-coder/VIctory-NVIDIA), where it was fixed the same way. Here it also covers the GPU read-only list, which write_combined_freed forgets too.

Tested

  • New test shadow::write_combined_tests::regions_inside_a_reservation_go_with_it. It reserves 4 MiB, commits a write-combined part and a GPU read-only part inside it, records both, releases through title_memory_freed(base, 0) and checks both regions are gone. On main (dcba958e5) it fails with a write-combined commit outlived its reservation; with the change it passes, along with the two existing tests in the module.
  • cargo test -p leonida-gpu --lib: 560 passed, 0 failed, 50 ignored. cargo test -p leonida-hle --lib xmem: 22 passed.
  • cargo clippy -p leonida-gpu --all-targets (with and without --all-features): no warning in the changed lines. The workspace-wide -D warnings run stops on main already, in leonida-lua and leonida-shader.
  • Windows only. Linux and macOS are not covered (the code under test is Windows-only).

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

**@⁠Kal0s-design** on GitHub · 2026-11-30 09:01 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2703) `Kal0s-design:fix/wc-regions-on-release` → `main` · +72901 −8961 · 279 files · 264 commits · closed without merging Claimed in the coordination issue as: D3D12-WC-REGIONS-ON-RELEASE (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/2570) Depends on: conflicts with #2569 at the top of `title_memory_freed` in `watch.rs` (#2569 already conflicts with `main` in other files). Whichever lands second needs a small rebase. ### What and why `watch::title_memory_freed` passes its own `len` to `shadow::write_combined_freed`. A release (`XMemVirtualFree`/`VirtualFree` with `MEM_RELEASE`) has `len` 0, and for 0 `write_combined_freed` drops only the regions whose start equals the allocation's base. A region recorded for something *inside* the allocation outlives the release: - a write-combined commit (`0x400`) inside a reservation that wasn't asked for write-combined; - a GPU read-only part. `xmem.rs` records these for the parts the title splits a placeholder into, and its 32-bit graphics heap comes that way. After the release, a new allocation at those addresses counts as write-combined (`LEONIDA_WC_NO_BACK`, on in `play-nvidia.ps1`) or GPU read-only (`LEONIDA_GPU_READ_ONLY_NO_BACK`, on by default). Its shader writes are then left on the card and never come back to title memory, the one failure these switches must not cause. The change: on a release, `title_memory_freed` forgets the regions over the allocation's own extent, `allocation_end(at, at)`, which it already asks for the views just below while the memory is still there. A decommit keeps its length, and `write_combined_freed` itself is unchanged, so its existing tests keep their meaning. I found the same gap while reviewing the write-combined port in TaynanGT/Vice-Tay#462 (the same code from trustno1-coder/VIctory-NVIDIA), where it was fixed the same way. Here it also covers the GPU read-only list, which `write_combined_freed` forgets too. ### Tested - New test `shadow::write_combined_tests::regions_inside_a_reservation_go_with_it`. It reserves 4 MiB, commits a write-combined part and a GPU read-only part inside it, records both, releases through `title_memory_freed(base, 0)` and checks both regions are gone. On `main` (dcba958e5) it fails with `a write-combined commit outlived its reservation`; with the change it passes, along with the two existing tests in the module. - `cargo test -p leonida-gpu --lib`: 560 passed, 0 failed, 50 ignored. `cargo test -p leonida-hle --lib xmem`: 22 passed. - `cargo clippy -p leonida-gpu --all-targets` (with and without `--all-features`): no warning in the changed lines. The workspace-wide `-D warnings` run stops on `main` already, in `leonida-lua` and `leonida-shader`. - Windows only. Linux and macOS are not covered (the code under test is Windows-only). GPUs tested: n/a (CPU unit tests) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
title_memory_freed told shadow::write_combined_freed the call's own length,
and a release has none: write_combined_freed then dropped only the regions
that start exactly at the allocation's base. A region recorded for a commit
inside a reservation, or for one of the parts the title splits a placeholder
into (its 32-bit graphics heap comes in such parts, recorded GPU read-only),
outlived the release. Memory a later allocation got at those addresses then
counted as write-combined (LEONIDA_WC_NO_BACK, on in play-nvidia.ps1) or GPU
read-only (LEONIDA_GPU_READ_ONLY_NO_BACK, on by default), and what shaders
wrote there was left on the card instead of coming back to title memory.

On a release the extent is now the allocation's own (allocation_end, asked
before the memory is let go, as the views below already do). A decommit
keeps its length. write_combined_freed itself is unchanged.

The new test reserves memory, commits a write-combined part and a GPU
read-only part inside it, releases the reservation through
title_memory_freed and checks that neither region is left. It fails on main
with "a write-combined commit outlived its reservation".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 09:06:53 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-11-30 12:14 UTC · original

Merged main (d367675c7, all CI green there) into the branch so the red semantic-tests (the f16 case that was red on main too) runs again: new head 46399564b (merge commit, no rebase or force-push). cargo test -p leonida-gpu --lib on the new head: 564 passed, 52 ignored, 0 failed.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-11-30 12:14 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2703#issuecomment-7278145830) Merged `main` (`d367675c7`, all CI green there) into the branch so the red `semantic-tests` (the f16 case that was red on `main` too) runs again: new head `46399564b` (merge commit, no rebase or force-push). `cargo test -p leonida-gpu --lib` on the new head: 564 passed, 52 ignored, 0 failed. AI-assisted: yes
Author
Owner

@⁠KelboLive on GitHub · 2026-11-30 15:26 UTC · original

Independent check on Windows (not the author), at 46399564b.

  • cargo test --locked --jobs 8 -p leonida-gpu --lib -- --test-threads=8: 564 passed, 0 failed, 52 ignored in 13.8 s.

The diff adds no unit test (source-only in shadow.rs/watch.rs), so this is regression/compile coverage; the write-combined/GPU-read-only region release itself needs a game run, which I did not do.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

**@⁠KelboLive** on GitHub · 2026-11-30 15:26 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2703#issuecomment-7283000584) Independent check on Windows (not the author), at `46399564b`. - `cargo test --locked --jobs 8 -p leonida-gpu --lib -- --test-threads=8`: **564 passed, 0 failed, 52 ignored** in 13.8 s. The diff adds no unit test (source-only in `shadow.rs`/`watch.rs`), so this is regression/compile coverage; the write-combined/GPU-read-only region release itself needs a game run, which I did not do. Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), Rust 1.98.1 pinned. AI-assisted: yes (opencode).
Author
Owner

@⁠Kal0s-design on GitHub · 2026-11-30 15:42 UTC · original

@⁠KelboLive thanks for the Windows run. One correction for whoever reads it: the diff does add a unit test, shadow::tests::regions_inside_a_reservation_go_with_it (in shadow.rs, +32 lines). It fails on main and passes here, so it's red/green rather than only regression coverage: cargo test -p leonida-gpu --lib -- regions_inside_a_reservation_go_with_it.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-11-30 15:42 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2703#issuecomment-7283447064) @⁠KelboLive thanks for the Windows run. One correction for whoever reads it: the diff does add a unit test, `shadow::tests::regions_inside_a_reservation_go_with_it` (in `shadow.rs`, +32 lines). It fails on `main` and passes here, so it's red/green rather than only regression coverage: `cargo test -p leonida-gpu --lib -- regions_inside_a_reservation_go_with_it`. AI-assisted: yes
github-bot 2026-12-01 02:26:07 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-01 03:09 UTC · original

Closing as superseded: main now does this through f85d1790a (#2795, D3D12-WC-SUBRANGE-NO-BACK, hardly-relevant). title_memory_freed with no length forgets the write-combined and GPU read-only regions over the whole allocation (resolve_end(...)), which is what this PR added. I checked it: this PR's test regions_inside_a_reservation_go_with_it, dropped unchanged into main 90c514437 (only .as_slice() added for the new SmallRanges), passes there. Thanks to the reviewers and verifiers.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-01 03:09 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2703#issuecomment-7299394282) Closing as superseded: `main` now does this through f85d1790a (#2795, D3D12-WC-SUBRANGE-NO-BACK, hardly-relevant). `title_memory_freed` with no length forgets the write-combined and GPU read-only regions over the whole allocation (`resolve_end(...)`), which is what this PR added. I checked it: this PR's test `regions_inside_a_reservation_go_with_it`, dropped unchanged into `main` `90c514437` (only `.as_slice()` added for the new `SmallRanges`), passes there. Thanks to the reviewers and verifiers. AI-assisted: yes

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/ubiquitarianism--leonida!2703
No description provided.