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

Closed
Kal0s-design wants to merge 2 commits from upstream-pr/2703 into main
Kal0s-design commented 2026-11-30 09:01:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T09:01:29Z.


Claimed in the coordination issue as: D3D12-WC-REGIONS-ON-RELEASE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7273508390)

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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5562167367 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2703). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-30T09:01:29Z. --- Claimed in the coordination issue as: D3D12-WC-REGIONS-ON-RELEASE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7273508390) 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)
Kal0s-design commented 2026-11-30 12:14:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T12:14:05Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7278145830 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2703#issuecomment-7278145830). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-30T12:14:05Z. --- 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
KelboLive commented 2026-11-30 15:26:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T15:26:29Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7283000584 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2703#issuecomment-7283000584). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T15:26:29Z. --- 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).
Kal0s-design commented 2026-11-30 15:42:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T15:42:33Z.


@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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7283447064 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2703#issuecomment-7283447064). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-30T15:42:33Z. --- @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 commented 2026-12-01 03:09:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-01T03:09:48Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7299394282 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2703#issuecomment-7299394282). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-01T03:09:48Z. --- 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

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
ProjectLeonida/mandy!2703
No description provided.