Skip to content

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

Open
Kal0s-design wants to merge 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/page-writes-per-heap
Open

Kal0s-design wants to merge 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

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.

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.

5 participants