Skip to content

d3d12-gpu: physical-page writes reach every view heap's texture pass (D3D12-GPU-PAGE-WRITES-PER-HEAP) - #2852

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/page-writes-per-heap
Dec 1, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/page-writes-per-heap

Conversation

@Kal0s-design

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-GPU-PAGE-WRITES-PER-HEAP (#2570 (comment))

What and why

Physical-page writes behind the guards' back (page_written in d3d12/crates/leonida-gpu/src/descriptor/host.rs, mostly DirectStorage reads into physical pages) were kept in one list, PAGE_WRITTEN, and the ones retained for one-shot bakes in another, BAKE_PAGE_WRITTEN. The first view heap whose texture pass ran took each list whole. When the title binds more than one view heap, the placed textures of the others kept the bytes from before until something else made their slots due.

The remapped ranges already have a cursor per heap (RemapQueue, from fxceless/victory-fork 42be495). This gives the page writes the same treatment:

  • PAGE_WRITTEN becomes a list per view heap that holds placed textures, keyed by the heap's lease_id. A heap gets its list at its first placed texture, and Drop for HostHeap removes it.
  • page_written appends to every list, with the same joining and compact as before.
  • The bake list becomes a field of the heap (bake_page_written); texture_news looks at the heap's own lists.

This ports TaynanGT's fix from the sibling VIctory tree (TaynanGT/Vice-Tay bc44a9f, GPL-3.0), credited as co-author.

Tested

  • New test descriptor::host::tests::a_page_write_reaches_each_view_heap: two heaps with an uploaded placed texture at the same address, one page_written; after each heap's pass both slots are due (and with LEONIDA_PAGE_WRITES=guards, both bakes find them). On main (3bf2844a5) it fails: the second heap's slot never becomes due (left: [], right: [3]). With the change it passes, with the default LEONIDA_PAGE_WRITES and with guards.
  • cargo test -p leonida-gpu --lib: 600 passed, 0 failed, 55 ignored (main: 599).
  • cargo clippy -p leonida-gpu --lib --tests: no warning in the changed lines.
  • Windows only, unit tests on a hardware D3D12 device (the test makes real heaps); the title wasn't run.

GPUs tested: n/a (unit tests; no game run)

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

Writes behind the guards' back (DirectStorage reads into physical pages)
were kept in one list, and so were the ones retained for one-shot bakes.
The first view heap whose texture pass ran took the whole list, so when
the title binds more than one view heap the placed textures of the others
kept the bytes from before. The remapped ranges already have a cursor per
heap (RemapQueue); the page writes now get the same treatment: a list per
view heap that holds placed textures (made at its first placed texture,
taken out when the heap goes), and a bake list per heap.

After TaynanGT's fix in TaynanGT/Vice-Tay (bc44a9f).

Co-Authored-By: TaynanGT <157688638+TaynanGT@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fiestorra

Copy link
Copy Markdown
Contributor

Verified on Windows: the crate is green with it, and it adds one test - measured against main's own file.

main 7fbcd456e, cherry-picked (one commit, merge-base 3bf2844a5), cargo 1.98.1:

cargo test -p leonida-gpu --lib
  with this pull request:                 ok. 601 passed; 0 failed; 55 ignored
  with descriptor/host.rs from main:      ok. 600 passed; 0 failed; 55 ignored

So the change is tied to its test and nothing else in the crate moved. Existence control, not mutation: I did not break the view-heap walk to watch a specific test fail.

What the crate passing does say, and it is more than usual for a descriptor change: the 600 tests that were already there exercise the host descriptor paths hard, and 97 added lines through descriptor/host.rs did not disturb any of them. That file has now been touched twice today (#2766 gave the sampler heaps every slot the shaders can reach, this one the physical-page writes), and both times the suite stayed green - which is the argument for the suite being worth its 14 seconds.

What I could not test: what "physical-page writes reach every view heap" means for a title - that needs the game writing through a view it made before the page moved. The unit tests are what I ran.

AI-assisted: yes - DeepSeek v4.1 (flash), both arms on the machine above.

Written with an AI assistant.

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified by MoonEyes (another person than the author) on head c4e286fdf merged with main cd0c52fcf: I read the whole diff. cargo test -p leonida-gpu: the only failure seen, committed_and_placed_r32_uint_textures_cast_to_r9g9b9e5_under_legacy_barriers, fails in 2 of 5 parallel runs of plain main too. With heaps.values_mut() cut to the first heap, a_page_write_reaches_each_view_heap fails.

In-game run with the six others of a batch on main 21e6bbf4b: RTX 4060 8 GB, NVIDIA 610.88, Windows 11 26300, -SeriesS -FrameGen 2, about 860 s in the world, then a second one of about 440 s, 0 device lost, 0 panic; pictures of the same scene compared with main, same. Black surfaces in early cutscenes while the PC's memory is full are there on plain main too.

AI-assisted (Claude Code).

@merzleckeier

Copy link
Copy Markdown
Contributor

I tested c4e286f together with #2841 0729d56 on 0ff8a53, Win64 MSVC target under Wine 11.22 on Radeon Raphael (RADV 26.3.1) and RTX 4070 SUPER (620.73.04). The combined stock selection passed on both GPUs.

A separate late-view-heap lifecycle check uses real host view heaps and synthetic placed-texture descriptors. Two adjacent writes inside the same texture coalesced per heap. Draining the first heap kept the second heap’s pending range. Dropping that second heap removed its pending entry, and an uploaded view in a newly enrolled heap received only the next write. The isolated test passed on both GPUs with LEONIDA_PAGE_WRITES=slots (scheduled refresh) and with 0 (bake refresh).

Removing only the lease-entry deletion in Drop failed that same isolated test at the cleanup assertion. The dropped lease still held the coalesced range. MEASURED with synthetic descriptors. AI-assisted (Codex).

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The per-heap split is right, and the test kills the obvious regression: mutating heaps.values_mut() to .take(1) fails it with left: [] right: [3] at host.rs:7815, and the crate stays 600/0/55. The storage and consumption paths match the description (map at :1024-1026, append/join/compact at :1062-1078, own list at :2921-2927, per-heap bake at :315-319/:2702-2713, registration at the first placed view :3775-3777), and there is no lock inversion: PAGE_WRITTEN is never held while taking placed/textures.

Two gaps:

  1. The Drop cleanup has no coverage — deleting the remove(&self.lease_id) body leaves all 600 tests green, because the new test never drops a heap after a write. Either merzleckeier's lifecycle test belongs in this PR, or the cleanup is an untested leak fix; as it stands a future change can leak a dead heap's list forever.
  2. Registration is lazy: a heap's list is created only at its first spanned placed view, so a write arriving before that reaches no list at all. That is benign while the first upload reads title memory later, but the invariant is "from the first placed texture on", not "every write" — the doc comment should say so.

Worth stating in the body: every page write now takes the single global mutex and appends to every live heap, and each list can hold MOST = 1<<18 ranges (~4 MB), so retention and lock work now scale with the number of live heaps where the old single list did not. Also PAGE_WRITES[1] (the compaction stat) now increments per heap, which changes what that counter means.

Merge is clean against current main (383 behind, and main still has the global list, so this is not fixed elsewhere). #2908 overlaps this file but merges cleanly; #2834 conflicts only in d3d12/docs/switches.md.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

In-game RUN of head c4e286f, merged with #2857 (2451b47) onto main 1abef08 (tree 32b6b8c23). RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11, -SeriesS -NoDlss -NoFrameGen.

  • cargo test -p leonida-gpu --lib --locked: 647 passed, 0 failed.
  • Cold start to the world, then a 900 s warm walking run: 0 device lost, no panic; frame median 37.8 ms (26.5 fps), p90 38.8 ms.
  • Private memory flat after loading: 23.7 GB at +3 min, 24.0-24.2 GB from +5 min to +15 min (max 24.3 GB), so no growth over 900 s.
  • The fps is a little under my 330 s main runs (27.9-28.2) because the 900 s walk reaches heavier streets; the build ran from another folder than my main baseline, and the driver cache is per exe path, so it is not a like-for-like fps comparison.
    AI-assisted: Claude Code (oreofr-agent).

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging (added to the merge queue). Head c4e286f merges cleanly with main 913b88b; since my test main gained only #2973, #2858, #2882, #2788, #2876 and #2884, none of which touches this pull request's file. Claim: D3D12-GPU-PAGE-WRITES-PER-HEAP (#2570 (comment)). Red/green: MoonEyes (cutting heaps.values_mut() to the first heap fails a_page_write_reaches_each_view_heap), KelboLive review on this head (.take(1) mutation fails at host.rs:7815, crate 600/0/55), merzleckeier (with #2841 under Wine on two GPUs). I ran: #2857 and #2852 together on main 1abef08, leonida-gpu 647 passed / 0 failed, and a 900 s warm in-game run on an RTX 4070 Laptop (621.40): 0 device lost, private memory flat at 24.0-24.3 GB (details in my RUN comment above). CI: green on head and main; the queue runs the full CI before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.

@OreoFR
OreoFR added this pull request to the merge queue Dec 1, 2026
@EdenBendheim

Copy link
Copy Markdown
Contributor

Independent native Windows 11 + RTX 3090 verification (VERIFY-2852-RTX3090, claimed in #2570): PASS

  • Tested: PR head c4e286fdf merged cleanly onto main c4ca57615 (diagnostics,mimalloc, exe_sha16: 628a7632ac7e626b).
  • Hardware / OS: Windows 11, AMD Ryzen 9 5900X, 32 GiB RAM, NVIDIA GeForce RTX 3090 24 GiB (driver 610.88).
  • Clean-room check: python -I scripts/check_no_game_data.py passed on descriptor/host.rs.
  • Unit red/green (cargo test -p leonida-gpu --locked --lib -- a_page_write_reaches_each_view_heap --test-threads=1):
    • Baseline c4ca57615 with new test: 0 passed, 1 failed (second view heap received an empty slot list where [3] was expected).
    • Merged PR (default and --features diagnostics): 1 passed, 0 failed in both configurations.
  • Retail 300 s in-game gate (scripts/game-gate.ps1, Series X 1080–1440p, -NoFrameGen, gate_driver mode=roam, same free-roam template as main baseline):
    • Verdict: PASS (reasons=[], warnings=[], exit=clean, stop=target, cache_saved=true, GATE CONTROL fired with moved=1.67 m, max from_control_m=1.7 m, 0 GATE FAIL).
    • Metrics (world_s=300.6s, world+60s, 6,024 presents, 4,184 median draws): 25.36 FPS median, 21.00 FPS p5, 47.63 ms p95 frame time, 0.237 s longest present gap, 0 missed pipeline waits, 25,739 MB max private memory, 3,185 MB min commit left, 7,639 MB max VRAM (0 s over budget).
    • Note on texture rechecks: Logged cumulative changed-texture recheck count reached 3,001 (vs 2,001 on main roam-stress and #2896 roam; all three hit the 300-entry late-warning cap), so single-run roaming stability is confirmed cleanly without claiming recheck elimination from this single run (draw counts also differed: 4,184 vs 7,450).

AI-assisted: yes (Gemini 4 Neon orchestrator; Codex GPT-6.2 Astra build, unit tests, and serialized RTX 3090 gate execution).

Merged via the queue into ProjectLeonida:main with commit 2705632 Dec 1, 2026
5 checks passed
@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2852 — Linux, Fedora 45, Rust 1.98.1 (the pinned toolchain), Wine 11.21, no GPU device, no game files

Verdict: PASS on what I could check, and the red/green question could not be settled here. The change is a per-heap table of physical-page writes, and nothing that passed before fails now — but the new test cannot run on this machine's device, so I could not see it turn from red to green.

Head c4e286fd, base the current main, merge base 3bf2844a, 1 file, +97/−25: d3d12/crates/leonida-gpu/src/descriptor/host.rs.

1. What it changes

PAGE_WRITTEN was one global list of the address ranges that title memory wrote behind the guards' back (DirectStorage's reads into physical pages, at the addresses the title has them mapped). A texture pass took those ranges — std::mem::take — so with more than one view heap the first heap's pass took them and the other heaps' placed textures kept the bytes from before. The change gives each heap its own list, keyed by the heap's lease_id:

static PAGE_WRITTEN: Mutex<std::collections::BTreeMap<u64, Vec<(u64, u64)>>> =
    Mutex::new(std::collections::BTreeMap::new());

with the heap remembering whether it has an entry (page_writes_kept) and keeping its own bake_page_written list. This is the same shape the RemapQueue fix already had for the remapped ranges, and the change says so.

2. What the tests do here

leonida-gpu does not build on Linux (upstream: "The crates are Windows-only today … Linux follows #2577"), so I cross-compiled it for x86_64-pc-windows-gnu and ran the tests under Wine 11.21:

the merge base with only this pull request's tests 655 tests
the pull request's head 655 tests
tests that pass on the base and fail on the head 0
failing on both sides 3 — watch::many_askers_tests::holes_are_known_until_the_runtime_tells, watch::many_askers_tests::no_mapping_is_lost_between_askers and watch::tests::what_is_left_alone_is_no_known_hole

3. Why the new test could not be judged here

The new test is descriptor::host::tests::a_page_write_reaches_each_view_heap. It builds two view heaps, writes a page and checks that each heap's placed texture is woken. The red side (the merge base with only this pull request's tests) builds and the test passes there — which is not a result, because the test never gets that far on this machine. I put a probe in it:

PROBE: 两个 heap 开始建
PROBE: 提前返回(没有 host GPU / heap 建不起来)
test result: ok. 1 passed; 0 failed; ...

HostHeap::new(false, 8) gives None here (the same early return the other device-dependent tests have), so the test passes without asserting anything. The same is true of the head's copy. So the red→green that the repository's own verifier would look for cannot be shown on this machine: it needs a host GPU that makes a view heap.

4. What I did not check

  • The multi-heap behaviour itself. That the first heap's pass used to take the other heaps' ranges is the bug the change fixes; I read the code and the test, but the test could not run here.
  • Whether lease_id is unique per view heap for the whole run. The change keys the table by it; I did not trace where the ids come from.
  • Windows and MSVC. Everything above is Linux with Wine 11.21 (staging).
  • The corpus. The change is about how the title's placed textures see DirectStorage writes; that is a property of a run with the game.

AI-assisted: MiniMax M3.1 Flash.

1 similar comment
@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2852 — Linux, Fedora 45, Rust 1.98.1 (the pinned toolchain), Wine 11.21, no GPU device, no game files

Verdict: PASS on what I could check, and the red/green question could not be settled here. The change is a per-heap table of physical-page writes, and nothing that passed before fails now — but the new test cannot run on this machine's device, so I could not see it turn from red to green.

Head c4e286fd, base the current main, merge base 3bf2844a, 1 file, +97/−25: d3d12/crates/leonida-gpu/src/descriptor/host.rs.

1. What it changes

PAGE_WRITTEN was one global list of the address ranges that title memory wrote behind the guards' back (DirectStorage's reads into physical pages, at the addresses the title has them mapped). A texture pass took those ranges — std::mem::take — so with more than one view heap the first heap's pass took them and the other heaps' placed textures kept the bytes from before. The change gives each heap its own list, keyed by the heap's lease_id:

static PAGE_WRITTEN: Mutex<std::collections::BTreeMap<u64, Vec<(u64, u64)>>> =
    Mutex::new(std::collections::BTreeMap::new());

with the heap remembering whether it has an entry (page_writes_kept) and keeping its own bake_page_written list. This is the same shape the RemapQueue fix already had for the remapped ranges, and the change says so.

2. What the tests do here

leonida-gpu does not build on Linux (upstream: "The crates are Windows-only today … Linux follows #2577"), so I cross-compiled it for x86_64-pc-windows-gnu and ran the tests under Wine 11.21:

the merge base with only this pull request's tests 655 tests
the pull request's head 655 tests
tests that pass on the base and fail on the head 0
failing on both sides 3 — watch::many_askers_tests::holes_are_known_until_the_runtime_tells, watch::many_askers_tests::no_mapping_is_lost_between_askers and watch::tests::what_is_left_alone_is_no_known_hole

3. Why the new test could not be judged here

The new test is descriptor::host::tests::a_page_write_reaches_each_view_heap. It builds two view heaps, writes a page and checks that each heap's placed texture is woken. The red side (the merge base with only this pull request's tests) builds and the test passes there — which is not a result, because the test never gets that far on this machine. I put a probe in it:

PROBE: 两个 heap 开始建
PROBE: 提前返回(没有 host GPU / heap 建不起来)
test result: ok. 1 passed; 0 failed; ...

HostHeap::new(false, 8) gives None here (the same early return the other device-dependent tests have), so the test passes without asserting anything. The same is true of the head's copy. So the red→green that the repository's own verifier would look for cannot be shown on this machine: it needs a host GPU that makes a view heap.

4. What I did not check

  • The multi-heap behaviour itself. That the first heap's pass used to take the other heaps' ranges is the bug the change fixes; I read the code and the test, but the test could not run here.
  • Whether lease_id is unique per view heap for the whole run. The change keys the table by it; I did not trace where the ids come from.
  • Windows and MSVC. Everything above is Linux with Wine 11.21 (staging).
  • The corpus. The change is about how the title's placed textures see DirectStorage writes; that is a property of a run with the game.

AI-assisted: MiniMax M3.1 Flash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants