D3D12-GPU-TEXBACK-REUSE-BEFORE: reuse thread-local before buffer in texback::write #2696
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2696"
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 bobcatchris on 2026-11-30T05:02:52Z.
Problem
In
d3d12/crates/leonida-gpu/src/texback.rs,write()already keeps a thread-localTILEDbuffer (capped atKEPT = 32 MiB) to avoid allocating and freeing a multi-MBVec<u8>on every texture read-back. However, line 451 still created a freshlet mut before = Vec::new()on every call, so everyLEONIDA_COPY_BACKread-back (~22/frame in gameplay) allocated, page-faulted, and freed a full texture-sized snapshot buffer just to runchanged_pages().Fix
TILED: RefCell<Vec<u8>>withSCRATCH: RefCell<(Vec<u8>, Vec<u8>)>holding both the tiled destination buffer and thebeforepre-tile snapshot buffer, capping both atKEPT = 32 MiB.copy_readbacks_write_changed_pages_across_reused_scratchverifying repeatedcopy = truelandings write only changed 4 KiB pages across reused scratch storage.Verification
10.0.26200), Ryzen 7 7800X3D, AMD Radeon RX 7900 XTX (gfx1100), pinnedrustc 1.98.1.cargo test -p leonida-gpu --lib texback: 8 passed, 0 failed.python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/texback.rs: PASS.Claimed in #2570 as
D3D12-GPU-TEXBACK-REUSE-BEFORE.AI-assisted: Antigravity (Gemini 4 Neon)
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-30T06:03:49Z.
Independent synthetic Windows check on
e9e9e2b0593bee4cf7731ac1a07c6eca896a3ee3merged locally into0ae217ceb136401793bf00f6fd7d9055eb0eabc7. Windows 10 22H2 build 19045, Ryzen 9 5950X, Rust 1.98.1 MSVC.cargo test --locked --offline --profile diag -p leonida-gpu --features diagnostics --lib texback::tests -- --test-threads=1.The eight checked-in tests passed. I also added a temporary synthetic fixture that landed 3, 1, 4, 2 and 1 pages in succession on the same worker. It changed small regions in selected first/interior/last pages, used two unchanged copies, and checked the production written-byte counter as well as final bytes and guard regions. It observed 4, 0, 8, 8 and 0 KiB written. All nine cases passed with
LEONIDA_TEXBACK_LIGHT=1and again in a fresh process with=0as the legacy-path control. This covers the shrinking and growing reused-scratch case, including no-op copies. I did not measure allocation cost or memory retention in a game run.AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T11:02:39Z.
Independent Wine allocation check. I merged
e9e9e2b05ontod367675c7(tree77aebe926) and built both revisions for x64 MSVC with Rust 1.98.1 anddiagnostics. On CachyOS/Wine 11.22 I used the same test-only exact-size counter in the diagnostics allocator on both sides. The counter saw one allocation and one free for a known control buffer before each test.At 4 MiB with
LEONIDA_TEXBACK_LIGHT=1, the first call left one scratch buffer live onmainand two with the PR. On each of the next two calls,mainmade and freed one 4 MiB allocation. The PR made and freed zero.At 32 MiB + 16 KiB, each of two consecutive calls produced two allocations and two frees on both revisions. Returning to 4 MiB allocated both buffers again.
With
LEONIDA_TEXBACK_LIGHT=0, every call allocated and freed both buffers on each revision. All 24 fixture calls also verified the destination, the untouched guard bytes and the 8 KiB written-byte counter delta. The checked-in texback tests passed in both modes (7 onmain, 8 on the PR).CPU-only synthetic readbacks. AI-assisted with OpenAI Codex.
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T15:43:38Z.
Independent check on Windows (not the author), at
7305728f5.cargo test --locked --jobs 8 -p leonida-gpu --lib -- --test-threads=8: 564 passed, 0 failed, 52 ignored in 17.0 s.texback.rsonly (no new unit test), so this is compile + regression coverage; the reuse-before-snapshot behaviour 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).
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T17:28:38Z.
Concurrent writeback and late ticket invalidation on the current head
MEASURED. I tested
7305728f5merged ontomainad25611b5(tree5929fbeb2). Rust 1.98.1 targeting Windows x64 via cargo-xwin,leonida-gpu --libwithdiagnostics, Wine 11.22 on CachyOS.Two workers call production
texback::writeon separate synthetic buffers. A test-only hook in the light path holds both workers after tiling and before the write lock. At one checkpoint the controller invalidates only the first ticket throughtexback::remappedbefore releasing the workers. The legacy control invalidates that ticket before enteringwrite.All 12 fresh-process runs agreed (three per tree and
LEONIDA_TEXBACK_LIGHTmode, 144 write calls total). Selected and unselected slices, unchanged copies, ordinary non-copy readbacks, and the subsequent copy after invalidation matched the per-texelLayout::offsetoracle and guard checks. With one stale ticket, only the valid worker committed and the production copy-byte counter increased by 8 KiB. The stale destination remained byte-identical to its initial state.The stock
texback::testssuite passed 7/7 onmainand 8/8 on the PR in both modes, includingcopy_readbacks_write_changed_pages_across_reused_scratchon the PR. Scope: synthetic CPU writeback and controlled ticket invalidation under Wine. AI-assisted: Codex (GPT-6.2 Sol).Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T08:33:38Z.
RUN - VERIFY-2696-RTX4060 - by MoonEyes (another person than the author) on head
7305728f5, with #2836, #2835, #2695, #2696, #2689, #2699 and #2705 merged together intomain9372a4d4f(clean).main9372a4d4fand thenmain+ the seven, one run each, same save: 0 device lost, 0 panics, 0 exceptions in both, about 490 s in the world each, alive at the time limit. Frame rate median 17.8 with the seven and 15.0 onmain, not a comparison: other work ran on the PC during themainrun.main9372a4d4f+ the seven (Windows): leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: twosource_hash_testsfailed on a stale build script of mine (a target folder shared with an older branch) and pass after a rebuild of it (3 of 3).Limits: one card, one run per arm, two arms only, the seven together. Values only, no game data.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T09:51:18Z.
Merging. Head
7305728f5merges cleanly withmain0259473b0and does not force a rebase of #2566. CI green on this head and on main. In-game run on the RTX 4060 posted above (this head, with the six others, about 490 s in the world, 0 device lost, pictures compared with main). Tests on main + the seven: leonida-hle, leonida-gpu, leonida-formats, leonida, leonida-shader, 0 failed. Verified on it by KelboLive (Windows) and merzleckeier (Wine). Merged by MoonEyes.AI-assisted (Claude Code).