d3d12-gpu: writes kept for a one-shot bake are compacted, not dropped #3048
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3048"
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 Kal0s-design on 2026-12-02T05:07:02Z.
Claimed in the coordination issue as: D3D12-GPU-BAKE-WRITES-KEEP-COVERAGE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7285079681)
What and why
texture_slots_page_writtenind3d12/crates/leonida-gpu/src/descriptor/host.rskeeps the physical-page writes it does not make due in the heap'sbake_page_written, for the next one-shot decoration/ped bake. Past 16,384 ranges it dropped the lowest of them (drainafterjoin's sort), so textures written there kept their old bytes through the bake. DirectStorage can finish many batches between two bakes.The change: the ranges are made fewer with the existing
compact(neighbours joined, every changed byte still covered), as the pending texture changes already are, in a smallretain_bake_writes. Rebased on #2852 (writes kept per heap).This ports the bake-writes part of TaynanGT's fix from the sibling VIctory tree (TaynanGT/Vice-Tay
af7a9ed, GPL-3.0), credited as co-author.Tested
descriptor::host::tests::bake_writes_keep_every_change_across_batches: three batches of 32,768 disjoint ranges each; after every batch at most 16,384 are kept and every changed range is still covered by one. Withmain's drop in place it fails (batch 0: 16,384 ranges no kept range covers); with the change it passes.cargo test -p leonida-gpu --lib: 669 passed, 0 failed, 59 ignored.cargo clippy -p leonida-gpu --lib --tests: no warning in the changed lines.GPUs tested: n/a (CPU unit tests)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T07:18:04Z.
Independent verification on Windows 10 Pro 22H2 (build 19045), NVIDIA GeForce RTX 5070 12 GB (driver
621.40), MSVC toolchain (1.98.1). Head2ab0b4b9dmerged cleanly ontomain(f37c4a87d).python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/descriptor/host.rs: PASS.cargo test -p leonida-gpu --locked --lib bake_writes_keep_every_change_across_batches: 1 passed, 0 failed (6.12 s).cargo test -p leonida-gpu --locked --lib -- --skip depth_bounds_pipeline_loads_and_filters_draws: 668 passed, 0 failed, 59 ignored.cargo clippy -p leonida-gpu --locked --lib --tests: clean (0 new warnings).retain_bake_writes(descriptor/host.rs:1431-1439) passes2 * BAKE_MOSTtocompact(which loops whileranges.len() > most / 2, reducing strictly to<= BAKE_MOSTwhile preserving 100% byte coverage across batches).Verdict: PASS (unit tests & source review; title not run).
AI-assisted: yes (Antigravity).
Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-02T07:56:36Z.
VERIFY-3048 — Linux, Fedora 45, Rust 1.98.1, cross-compiled for x86_64-pc-windows-gnu, Wine 11.21, no GPU device, no game files
Verdict: PASS. The change removes a silent data loss: past a limit the kept write-ranges were dropped, and the textures written in the dropped ranges kept their old bytes through the next one-shot bake. The fix keeps the same limit and the same bound on the number of ranges, and makes the list fewer (neighbouring ranges merged) instead of shorter, so nothing that was written is forgotten. The new test states exactly that property and passes with the pull request. No regression: the base's 727 tests all still pass with the pull request (0 regressions); the only three failures are the
watchtests that fail on the base as well.head
2ab0b4b9d4b24543dced7351a385b5b4327defb4("d3d12-gpu: writes kept for a one-shot bake are compacted, not dropped"), baseorigin/main=0ae5a2664b9cd2fd30abfb1f6096f1c07b030f39, merge base8422def759cda12c89f918438eeb8d53dd5972a6(4 commits behindmain). One file, +46/−10:d3d12/crates/leonida-gpu/src/descriptor/host.rs.1. What it changes
HostHeapkeeps a list of the title-memory ranges that were written behind the guards' back (DirectStorage completions) inbake_page_written: Mutex<Vec<(u64, u64)>>, so that the slots holding those bytes can be woken up right before a one-shot ped/decoration bake draw:texture_slots_page_written(host.rs:3111) takes the frame's ranges and, when the bake is not due yet, keeps them for the bake.texture_slots_bake_written(host.rs:3132) takes the kept ranges, joins them and finds the placed-texture slots that hold bytes of them; those slots are marked changed, so their textures are re-uploaded before the bake draw.The old way of keeping them (in
texture_slots_page_written) was:joinsorts the list by address, sodrain(..drop_n)removes the lowest-addressed ranges — the writes that happened to sit at the bottom of the address space — and those writes are then invisible totexture_slots_bake_written: the slot that holds them is not marked changed, and its texture keeps its old bytes through the bake. DirectStorage can finish many batches between two bakes, so the list crosses 16 384 ranges easily (the file's own comment elsewhere says several threads map and write "some 5,000 ranges a frame").The new code introduces the named limit and one function:
compactalready existed onmain(host.rs:1414) and already had tests (host.rs:7448-7459): it halves the list by merging each pair of neighbours into one range that spans both, until the list is short enough. The limit is unchanged (1 << 14) and so is the bound on the number of ranges (kept.len() <= BAKE_MOST); what changes is that past the limit the list is made smaller (wider ranges) instead of shorter (missing ranges). The trade-off — "a bake looks at a little more memory than changed", so some slots that did not change are marked due — is stated in the new doc comment and is the price of not losing a write.2. The new test checks the property that matters
descriptor::host::tests::bake_writes_keep_every_change_across_batchesstates the property in the terms the caller depends on: every range that was written is still covered by some range the bake will look at, and the list stays within its bound. It feeds three batches of2 * BAKE_MOST= 32 768 non-overlapping ranges (each two bytes long, eight bytes apart, sojoincannot merge them), and after each batch asserts:kept.len() <= BAKE_MOST— the bound the old code also had; andlost == 0, wherelostcounts the changed ranges(s, e)for which no kept range(ks, ke)satisfiesks <= s && e <= ke— i.e. no change is forgotten.The test is pure logic over
Vec<(u64, u64)>: no GPU device, no game files, no title are needed, and it really runs here (it is present in the green binary and is the one test the base does not have).The red side cannot be built, so I checked the property against the old code directly. The new test calls
retain_bake_writesandBAKE_MOST, which the pull request introduces; the red side (merge base + the PR's test hunks) therefore does not compile — the tool reportsThat is the expected shape of a red side for a pull request that introduces the function its test exercises (the same thing happens for a test of a new API), and it means the red/green comparison for this pull request is not "the same test, failing then passing" but "the base's tests still pass with the pull request" (section 3). To establish that the new test would fail on the base, I wrote the old expression and the new one side by side in a scratch program (
/tmp/opencode/bake_old_vs_new.rs, both functions copied verbatim from the two revisions) and ran the same three batches through both:So on the base the very first batch already loses half of the writes (16 384 of 32 768 changed ranges are no longer covered by any kept range) and three batches lose 81 920 of 98 304; with the pull request nothing is lost. The new test's two assertions are exactly these two numbers ("no kept range covers" / "kept <= BAKE_MOST"), so the test does fail on the base and passes with the pull request.
3. What the tests do here
I cross-compiled the crate for
x86_64-pc-windows-gnuand ran the test binaries under Wine withpython3 tools/wine_red_green.py 3048 --crate leonida-gpu(run from/home/shangli/oc-workspace/gta6). The red side does not compile, so the tool falls back to its "merge base vs PR head" mode and reports:8422def7+ the PR's test hunksBAKE_MOST,retain_bake_writesnot found)8422def72ab0b4b9(PR head)The tool's verdict: "no regression against the base" — 0 regressions, 3 failing on both sides, 665 passing on both sides, and 1 test present only on the green side (
descriptor::host::tests::bake_writes_keep_every_change_across_batches, the new one).4. Any failure I saw and why it is not this PR
watchtests fail on both sides:watch::many_askers_tests::holes_are_known_until_the_runtime_tells,watch::many_askers_tests::no_mapping_is_lost_between_askers,watch::tests::what_is_left_alone_is_no_known_hole. They are inwatch.rs, which this pull request does not touch, and they fail identically with the pull request reverted (measured 6/6 failures per side on the base of PR #3046, which has the same merge base). They are environment failures of the guard-page/exception machinery ofwatchunder Wine without a real GPU.texture_slots_bake_writtentakes the kept ranges andjoins them, then looks for the slots that hold bytes of them. With the merged (wider) ranges the bake now marks more slots due than strictly changed — the trade-off the doc comment names. I could not measure how many extra slots that is on a real run (no GPU, no title).&mut self.bake_page_written.lock().unwrap_or_else(|e| e.into_inner())), which reads a little densely but is the same expression the old code used, and the lock is released at the end of the statement.5. What I did not check
Vec, notHostHeap.retain_bake_writesis called directly, so the test does not show thattexture_slots_page_writtenkeeps the ranges and thattexture_slots_bake_writtenthen uses them. The link between the two is the existing code and I only read it.2 * BAKE_MOSTargument tocompact:compact(ranges, most)stops whenranges.len() <= most / 2, so passing2 * BAKE_MOSTstops atBAKE_MOST. That is the only reason the two constants differ by a factor of two, and it is not obvious at the call site; I checked it by readingcompact(host.rs:1414-1420) and by the test's ownkept.len() <= BAKE_MOSTassertion, but I did not add a test forcompactwithmost = 2 * BAKE_MOSTitself.cargo test --target x86_64-pc-windows-gnuunder Wine 11.21 on Fedora 45; the change is pure logic and does not depend on the platform, but no native Windows run was made.slots_holdingcovers more memory than before; not measured.leonida-gpuwas, because the change is confined to it.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:16:13Z.
RUN (in game, grouped): this head
2ab0b4b9dwas merged withgit merge --no-ff, together with nine other rendering pull requests, ontomainf6dd2242e(no conflicts), and the d3d12 layer was rebuilt from that tree (diag profile, with DLSS). RTX 4060 8 GB, driver 610.88, Windows 11;scripts/play-d3d12.ps1 -SeriesS -Resolution 1080 -FrameGen 2, low-memory profile, keyboard-driven entry into the world and a walking route.Heads in this run: #2854
81a6e94a9, #293668215ad0b, #2939561bbc19c, #2997ef429f923, #3043915d4d5aa, #3045cb30a595a, #30460f19a0819, #3047c33e5b48e, #30482ab0b4b9d, #3051856615ce7.AI-assisted (Claude Code, Claude Opus 5.5)
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:22:02Z.
Merging. Head
2ab0b4b9dmerges cleanly withmainbf7886746and does not force a rebase of #2566. Checked on head2ab0b4b9d: all 6 CI checks green; third-party red/green by imShangLi on this head; an in-game run of at least 381 s with this head merged onto mainf6dd2242e(grouped with nine other rendering pull requests, comment above) showed no device loss, no unhandled exception and no hang; item claimed in the coordination issue; author has earlier merged pull requests; conflict-free merge against main. Merged by MoonEyes.AI-assisted (Claude Code).