d3d12: exclude write-combined sub-ranges from mixed copy-back (D3D12-WC-SUBRANGE-NO-BACK) #2795
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2795"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by hardly-relevant on 2026-11-30T21:36:50Z.
Claimed in the coordination issue as: D3D12-WC-SUBRANGE-NO-BACK (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7263187405)
Overlaps with: #2703, #2569 (same function
title_memory_freedind3d12/crates/leonida-gpu/src/watch.rs; neither is a dependency — whichever merges later needs a small rebase)What and why
Shadow::gpu_writescurrently only skips write-combined (WC) copy-back when an entire card copy sits inside a registered WC region (LEONIDA_WC_NO_BACK). For mixed reservations split withMEM_REPLACE_PLACEHOLDER— notably the 185 MiB 32-bit graphics heap split across0x1004/0x1404/0x8404/0x2404/0x2004parts — two gaps prevented skipping the0x2404WC sub-range:XMemVirtualAlloc'sMEM_REPLACE_PLACEHOLDERearly-return path registeredGPU_READ_ONLY(0x1000/0x8000) but did not register write-combined (asked & 0x400 != 0) sub-ranges (andwatch::title_memory_freedwithlen == 0onMEM_RELEASEdid not resolve the full allocation span to clear split sub-ranges).Shadow::gpu_writestreated a card copy spanning mixed classes as wholly non-WC rather than subtracting qualifying WC sub-ranges (>= LEONIDA_WC_MIN_BYTES) alongsideGPU_READ_ONLY.Changes:
d3d12/crates/leonida-hle/src/modules/xmem.rs: Registerasked & 0x400 != 0vialeonida_gpu::title_memory_write_combined(and guard both WC and GPU-RO registration with!base.is_null()) in theMEM_REPLACE_PLACEHOLDERpath; addplaceholder_replace_registers_write_combined_and_gpu_read_only.d3d12/crates/leonida-gpu/src/watch.rs: Resolve the allocation span once viaallocation_end(at, at)ontitle_memory_freed(at, 0)(whenat != 0and memory classes are registered), clearing split WC/GPU-RO sub-ranges onMEM_RELEASEand reusing the cached end forforget_views.d3d12/crates/leonida-gpu/src/lib.rs: Re-exporttitle_memory_is_write_combinedandtitle_memory_is_gpu_read_onlyunder#[cfg(windows)].d3d12/crates/leonida-gpu/src/shadow.rs:WRITE_COMBINEDandGPU_READ_ONLYtables (saturating_add), trackANY_WRITE_COMBINED, updateANY_*flags inside write locks on free, and exposehas_memory_classes().Shadow.wcto 4 states (WcKind: unknown / none / whole / partial) soShadow::gpu_writesskips extra work for non-WC and whole-WC copies and preserves standaloneLEONIDA_WC_NO_BACKbehavior.LEONIDA_WC_SUBRANGE_NO_BACK(default off;LEONIDA_AB-switchable; disabled by explicitLEONIDA_WC_NO_BACK=0unlessLEONIDA_ABoverridesLEONIDA_WC_NO_BACK).SmallRangesspilling toVeconly above 4 intervals) subtractingGPU_READ_ONLYfirst (GPU_RO_DECLARED, avoiding double-counting overlaps like0x1404/0x8404) and qualifying>= wc_minWC sub-ranges second (WC_LEFT, lock dropped beforeemit).shadow::write_combined_teststo 7 unit tests covering clipping/merging/minimum-size filtering, mixed GPU-RO + WC subtraction,WcKindclassification,SmallRangesspill, and switch policy.Kept opt-in (
LEONIDA_WC_SUBRANGE_NO_BACK=0by default) pending broader hardware/scene confirmation, since copy-back savings only translate into frame-time gains when relay landing is on the critical path.Tested
Unit tests, checks, and clippy re-run after rebasing onto
main(0b6e0e3bc):python -I scripts/check_no_game_data.pyon all 4 modified files: passcargo test --manifest-path d3d12/Cargo.toml --locked -p leonida-gpu --lib shadow::write_combined_tests: 7 passed, 0 failedcargo test --manifest-path d3d12/Cargo.toml --locked -p leonida-gpu --features diagnostics --lib shadow::write_combined_tests: 7 passed, 0 failedcargo test --manifest-path d3d12/Cargo.toml --locked -p leonida-gpu --lib -- --test-threads=4: 585 passed, 0 failed (52 ignored)cargo test --manifest-path d3d12/Cargo.toml --locked -p leonida-hle --lib: 222 passed, 0 failed (2 ignored)cargo check --manifest-path d3d12/Cargo.toml --locked -p leonida: passcargo clippy --manifest-path d3d12/Cargo.toml --locked -p leonida-hle -p leonida-gpu --features diagnostics --lib --tests: pass (0 warnings in modified lines)Live retail-package runs (Series X profile, 2560x1440, render scale
0.6667, title upscaler,LEONIDA_WC_NO_BACK=1,LEONIDA_RELAY=2,diagprofile withdiagnostics,dlss,mimalloc, run on the same patchset prior to rebasing onto0b6e0e3bc):LEONIDA_AB=4,LEONIDA_WC_SUBRANGE_NO_BACK,LEONIDA_FRAME_GEN=0, 8 min, 0 errors/TDRs):10 MiB 0x1004,1 MiB 0x1404,27 MiB 0x8404,47 MiB 0x2404,100 MiB 0x2004).n=9drift-cancelled triplets): mean-6.41 ± 4.80 ms(8/9), p90-13.59 ± 10.88 ms(8/9), jitter-7.28 ± 6.55 ms, landed new 4 KiB pages-17.4%(1,552 → 1,282), exact sawtooth on 12/12 consecutive phase flips.LEONIDA_AB=4,LEONIDA_WC_SUBRANGE_NO_BACK,LEONIDA_FRAME_GEN=0, 8 min, 56 stationary phases across 5 camera holds, 0 errors/TDRs):OFF n=77/ON n=78):WC_LEFT+1,648 MB/frame(24,225 → 25,873 MB,+6.8%),GPU_RO_DECLAREDunchanged (589.8 → 591.3 MB), landed pieces296.2 → 263.3(-11.1%), landed new 4 KiB pages1,373.7 → 1,122.1(-18.3%,5.37 → 4.38 MB/frame), compare passes439.4 → 432.9(-1.5%), relay landing11.62 → 10.57 ms(-9.0%).n=9draw-locked<=3%drift-cancelled triplets): median-1.06 ± 0.92 ms(57.96 → 57.00 ms, 6/9), mean-0.96 ± 1.11 ms(59.88 → 58.99 ms), p90-2.44 ± 2.52 ms(69.96 → 67.86 ms), submit worked-0.33 ± 0.12 ms(8/9), landed new 4 KiB pages-21.8%(1,679 → 1,313), relay landing-1.54 ms(12.80 → 11.26 ms), compare passes-13.0/frame.n=11draw-locked<=3%triplets): frame time flat (+0.30 ± 0.61 msmedian,+0.43 ± 0.89 msmean; submit worked-0.15 ± 0.14 ms) because the ~10–11 ms relay landing sits behind async submit at ~50 ms/frame.LEONIDA_WC_SUBRANGE_NO_BACK=1,LEONIDA_FRAME_GEN=2, noLEONIDA_AB): 0 errors/TDRs, 2x FG active (status=0,presented=2), reduced copy-back confirmed (290landed pieces /1,293new 4 KiB pages /5.05 MBat median10,188draws), no new visual regressions.GPUs tested: NVIDIA GeForce RTX 4080 Laptop GPU (12 GB VRAM), driver 621.40, Windows 11 (Intel Core Ultra 9 185H, 31.4 GB RAM)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a — D3D12 shadow/HLE change only)Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T21:42:05Z.
Verified by OreoFR on macOS 27.2.1, Apple M5, head
f85d1790a, main0b6e0e3bc. This is a type-check only:leonida-gpudoes not build natively on macOS (Windows-only modules, the same on main), so the Mac can't run your unit tests.MEASURED
git merge-treeof this head onto main0b6e0e3bcgives no conflicts.cargo check --target x86_64-pc-windows-msvc(no MSVC; theccbuild steps are replaced with stand-ins that only create their output files), main vs main + this PR, exit 0 on both sides. The sorted warning lists are byte-identical:-p leonida-gpu: 19 / 19-p leonida-gpu --features diagnostics: 11 / 11-p leonida-gpu --tests: 24 / 24 (this compiles the 7 newshadow::write_combined_tests)-p leonida-hle --tests: 19 / 19 (this compilesplaceholder_replace_registers_write_combined_and_gpu_read_only)diagnostics.READ IN CODE
wc_subrange_no_back()(shadow.rs:3053in this head) returnsfalseunlessLEONIDA_WC_SUBRANGE_NO_BACKis asked. An explicitLEONIDA_WC_NO_BACK=0turns it off, and theLEONIDA_ABbit forLEONIDA_WC_NO_BACKis only read underdiagnostics. With the switch unset,gpu_writeskeeps its previous behavior. The newWcKindclassification and thexmem.rs/watch.rsregistration and release changes still run, and the type-check covers them.ASSUMED
AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T23:46:39Z.
RUN - independent verification (Verified by OreoFR) - RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 Pro 10.0.26300.
b1f91c2f9; green = main + this PR atf85d1790a(git merge --no-ff, clean, merge commitad887098c). Head re-checked at the end: stillf85d1790a. The newer main398c085c4(it brings #2779 andLEONIDA_SEND_CHANGEDinLEONIDA_AB, touchingshadow.rsandwatch.rs) also merges cleanly (git merge-tree --write-tree, exit 0).LEONIDA_WC_SUBRANGE_NO_BACKis off unless set (crate::asked!). An explicitLEONIDA_WC_NO_BACK=0also turns it off. Some parts are not behind the switch:xmem.rsregistration of the write-combined parts of the split 32-bit heap;watch.rsrelease over the whole allocation span;d3d12/, one worktree and onetarget/per arm):cargo test --locked -p leonida-gpuandcargo test --locked -p leonida-hle.Red / green
"Main + only the new tests" does not compile: the tests import
plan_gpu_write_ranges,classify_write_combined,title_memory_is_write_combinedand others that main does not have. So red is the PR with one fix hunk reverted at a time.b1f91c2f9xmem.rsthat register the write-combined partwatch.rsback to mainfor_each_gpu_write_rangedisabled (&& false)placeholder_replace_registers_write_combined_and_gpu_read_onlyfails atxmem.rs:3256. No page is write-combined ([false, false, false, false], expected[false, true, true, false]).xmem.rs:3264with "WC cleared on page 1": with main'stitle_memory_freed(at, 0), aMEM_RELEASEforgets only the region that starts atat.mixed_copy_subtracts_gpu_ro_then_wc_subrangesandsmall_ranges_spills_past_four_intervalsfail withwc_left: 0, and the ranges are kept whole.watch::tests::freed_memory_leaves_no_notes, is a flake that is already on main. I rancargo test -p leonida-gpu --lib8 times on each arm:watch.rs:5163, its first assertion, before any free);leonida-gpuand 2 inleonida-hleare the same on every arm.In game. I built a diag
leonida.exeof main and of main + PR withbuild-nvidia.ps1(diagnostics,dlss,mimalloc).play-d3d12.ps1 -SeriesS -NoDlss -NoFrameGen -Log, withLEONIDA_WINDOWS_BAR_MB=0andLEONIDA_TEXTURE_BUDGET_MB=1024.LEONIDA_WC_NO_BACK=1. That is the valueplay-nvidia.ps1sets for every NVIDIA player.LEONIDA_WC_SUBRANGE_NO_BACK=1.WC_NO_BACKon fromplay-nvidia.ps1.What the copy-back numbers say
MEM_REPLACE_PLACEHOLDERin every run: 10 MiB0x1004, 1 MiB0x1404, 27 MiB0x8404, 47 MiB0x2404and 100 MiB0x2004. These are the sizes your description gives.WC_LEFTcounter as whole write-combined copies, and that counter changes with the scene far more than the gain you measured (+1,648 MB a frame): A1 73,656 vs A2 28,872 MB a frame, in the same arm. The B runs went the way you describe:LEONIDA_AB=...,LEONIDA_WC_SUBRANGE_NO_BACKrun, as in your description, would remove the scene difference. A counter of its own for the sub-range bytes would help too.Safe to merge into main? Yes, with one note on point 3.
git merge-tree --write-treegives no conflict onb1f91c2f9or on the newer398c085c4. #2703 and #2569 are not merged yet. Whichever of the three merges second needs the smallwatch.rsrebase the description announces.f85d1790a: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests.LEONIDA_WC_NO_BACK=1thatplay-nvidia.ps1sets, a copy of 1 MiB or more lying wholly inside the 47 MiB0x2404part would now be left on the card. In my B0 run no such copy happened (0 lines inside the heap), and the run was stable.MEM_RELEASEnow clears those regions over the whole allocation.check_no_game_data.pyon the 4 changed files: clean.cargo test -p leonida-gpu: 587/587. The only failure seen is thefreed_memory_leaves_no_notesflake, which main has too.cargo test -p leonida-hle: 222/222.shadow.rs,watch.rs,lib.rs(2 re-exports) andxmem.rs, all on the write-combined / GPU read-only copy-back path, with 7 + 1 unit tests.d3d12/docs/switches.mddoes not list the new switch yet; that is not required by CI.Verdict:
Not covered:
LEONIDA_ABA/B on this card.LEONIDA_GPU_READ_ONLY_NO_BACK=0, where the 27 MiB0x8404part is subtracted as write-combined.AI-assisted (Claude Code, oreofr-agent).
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T00:52:57Z.
IN-GAME RUN (gate, independent): #2795 head
f85d1790a, RTX 3090 24 GB, NVIDIA 621.40, Windows 11.Setup: leonida.exe built from this head (
d3d12/scripts/build-nvidia.ps1, diag profile, no DLSS files),scripts/play-d3d12.ps1 -SeriesS -NoDlss -NoFrameGen -Log, the same save restored, standing still once in the world, withLEONIDA_NO_WATCHDOG=1and a stall-mode timeline. Other jobs (cargo builds) ran on the PC at the same time, so the frame rates are not comparable with other runs.Gate: PASS. No device loss, no gap over 10 s between frames, ended at the time limit: 421 s, world reached at 72 s, so 349 s in the world.
One setting differs from the defaults, and why:
LEONIDA_REAPER=0. On this card, main's release thread falls behind in most long runs and its queue piles up memory, unrelated to this PR (a fix is in progress under claim D3D12-REAPER-BOUNDED-QUEUE). A first gate attempt on this head with default settings had no device loss but was cut by my own memory safety stop at 329 s (82 GB private, only 256 s in the world).LEONIDA_REAPER=0removes that pile-up, so this run tests the PR alone: private memory stayed under 29.6 GB, VRAM under 12.9 GB, mapped scratch under 0.62 GB.Median 8.5 fps (p5 5.7), not comparable (see above). Unit tests for this PR were done independently by another verifier on Windows; this comment covers only the in-game gate. Not checked: Series X, walking, picture.
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T01:28:00Z.
Re-verification on current
main(asked by OreoFR, maintainer: a verdict on an older main does not count). AI-assisted (Claude Code agentoreofr-agent). RTX 4070 Laptop 8 GB, NVIDIA 621.40, Core Ultra 7 155H, Windows 11.Head
f85d1790a(unchanged since my first verdict), merged into current maind920d850dwithgit merge --no-ffin a scratch worktree (clean;shadow.rsandwatch.rs, which main also changed since the PR's base0b6e0e3bc, auto-merged).Tests (
cargo test --locked, fromd3d12/):d920d850d-p leonida-gpu-p leonida-hleNo new failure. The one failure is already on main and not from this PR:
host::refresh_tests::only_what_differs_goes_up(host.rs:14735), 3/3 deterministic on plain main, passes withLEONIDA_TAKE_BLOCKS=0, so it comes fromd920d850dturning that switch on by default (likely a stale test, see the analysis relayed on the agents' bridge). GitHub CI does not run thed3d12/Rust tests, so main shows green. OreoFR decided in writing to ignore that one test until its author updates it; nothing else is covered by that.In-game run on the merged build (
diagnostics,mimalloc, no DLSS), fresh copy of our standing gate data,-SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, standing:Verdict: no new failure and no device loss on current main + this PR. The remaining blocker is a rule one, not this PR: main is red on the test above until it is fixed or waived; the waiver is written.
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T01:32:57Z.
Question before merging, from OreoFR (maintainer). AI-assisted (Claude Code agent
oreofr-agent).The description says
Depends on: #2703, #2569, but also that this PR does not build on either of them: they only touch the same function (title_memory_freedind3d12/crates/leonida-gpu/src/watch.rs). Our checks agree: this head merged into current maind920d850dwithout #2703 or #2569 builds, passes the crate tests and ran 330 s in the game with no device loss (see the comment above).Could you confirm it is not a real dependency, and if so change that line to something like
Overlaps with: #2703, #2569 (same function in watch.rs; whichever merges later needs a small rebase)? Our merge checklist readsDepends on:as "must be merged first".About the rebase: #2703 (Kal0s-design) and #2569 (adrianmjim) are by other authors, so each of them would rebase their own pull request onto whatever lands first; nothing to do on your side.
Imported from ubiquitarianism/leonida. Originally posted by hardly-relevant on 2026-12-01T01:42:08Z.
Confirmed — neither #2703 nor #2569 is a dependency of this PR:
#2569is unrelated memory-leak work that only touches the same function (watch::title_memory_freed) without changingwrite_combined_freed.#2703fixes the sameMEM_RELEASE(len == 0) span resolution inwatch::title_memory_freedthat this PR already includes (reusing the cachedallocation_endforforget_viewsand gating onhas_memory_classes()), alongside this PR'sxmem.rsplaceholder registration andshadow.rssub-range subtraction.Updated the description line from
Depends on:toOverlaps with:. Ready to merge on its own whenever you're ready!Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T02:19:03Z.
Merging. Head
f85d1790amerges cleanly with mainbf19719ab(merge commitd921489d1).Claim: D3D12-WC-SUBRANGE-NO-BACK, https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7263187405 (author hardly-relevant). The
Depends on:line was clarified by the author toOverlaps with: #2703, #2569(not dependencies; whichever merges later rebases).Red/green: by oreofr-agent (not the author), RTX 4070 Laptop, Windows 11 (red/green on the first verdict above). I ran on this head merged into the current main
bf19719ab:cargo test --locked -p leonida-gpu588 passed, 0 failed;-p leonida-hle242 passed, 0 failed.In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, 330 s in the world on this head merged into
d920d850d, 0 device loss (comment above);bf19719abonly adds a test arm on top, nothing that runs. Independent run by enlighted on an RTX 3090 (421 s, PASS).CI: green on the head (all 5 checks) and on main (CI, No Game Data; Runtime last green on
cd9fe51a9, not triggered since).AI-assisted: yes, Claude Code agent
oreofr-agent, merged on OreoFR's (maintainer) written instruction.