D3D12-GPU-TEXBACK-REUSE-BEFORE: reuse thread-local before buffer in texback::write #2696

Merged
bobcatchris merged 1 commit from upstream-pr/2696 into main 2026-12-01 09:51:23 +00:00
bobcatchris commented 2026-11-30 05:02:52 +00:00 (Migrated from github.com)

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-local TILED buffer (capped at KEPT = 32 MiB) to avoid allocating and freeing a multi-MB Vec<u8> on every texture read-back. However, line 451 still created a fresh let mut before = Vec::new() on every call, so every LEONIDA_COPY_BACK read-back (~22/frame in gameplay) allocated, page-faulted, and freed a full texture-sized snapshot buffer just to run changed_pages().

Fix

  • Replace thread-local TILED: RefCell<Vec<u8>> with SCRATCH: RefCell<(Vec<u8>, Vec<u8>)> holding both the tiled destination buffer and the before pre-tile snapshot buffer, capping both at KEPT = 32 MiB.
  • Add copy_readbacks_write_changed_pages_across_reused_scratch verifying repeated copy = true landings write only changed 4 KiB pages across reused scratch storage.

Verification

  • Hardware/OS: Windows 11 Pro (10.0.26200), Ryzen 7 7800X3D, AMD Radeon RX 7900 XTX (gfx1100), pinned rustc 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)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5559321026 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2696). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T05:02:52Z. --- ### Problem In `d3d12/crates/leonida-gpu/src/texback.rs`, `write()` already keeps a thread-local `TILED` buffer (capped at `KEPT = 32 MiB`) to avoid allocating and freeing a multi-MB `Vec<u8>` on every texture read-back. However, line 451 still created a fresh `let mut before = Vec::new()` on every call, so every `LEONIDA_COPY_BACK` read-back (~22/frame in gameplay) allocated, page-faulted, and freed a full texture-sized snapshot buffer just to run `changed_pages()`. ### Fix - Replace thread-local `TILED: RefCell<Vec<u8>>` with `SCRATCH: RefCell<(Vec<u8>, Vec<u8>)>` holding both the tiled destination buffer and the `before` pre-tile snapshot buffer, capping both at `KEPT = 32 MiB`. - Add `copy_readbacks_write_changed_pages_across_reused_scratch` verifying repeated `copy = true` landings write only changed 4 KiB pages across reused scratch storage. ### Verification - Hardware/OS: Windows 11 Pro (`10.0.26200`), Ryzen 7 7800X3D, AMD Radeon RX 7900 XTX (`gfx1100`), pinned `rustc 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)
lixonik commented 2026-11-30 06:03:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-30T06:03:49Z.


Independent synthetic Windows check on e9e9e2b0593bee4cf7731ac1a07c6eca896a3ee3 merged locally into 0ae217ceb136401793bf00f6fd7d9055eb0eabc7. 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=1 and again in a fresh process with =0 as 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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7270266688 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2696#issuecomment-7270266688). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-30T06:03:49Z. --- Independent synthetic Windows check on `e9e9e2b0593bee4cf7731ac1a07c6eca896a3ee3` merged locally into `0ae217ceb136401793bf00f6fd7d9055eb0eabc7`. 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=1` and again in a fresh process with `=0` as 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.
merzleckeier commented 2026-11-30 11:02:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T11:02:39Z.


Independent Wine allocation check. I merged e9e9e2b05 onto d367675c7 (tree 77aebe926) and built both revisions for x64 MSVC with Rust 1.98.1 and diagnostics. 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 on main and two with the PR. On each of the next two calls, main made 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 on main, 8 on the PR).

CPU-only synthetic readbacks. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7276453326 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2696#issuecomment-7276453326). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T11:02:39Z. --- **Independent Wine allocation check.** I merged `e9e9e2b05` onto `d367675c7` (tree `77aebe926`) and built both revisions for x64 MSVC with Rust 1.98.1 and `diagnostics`. 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 on `main` and two with the PR. On each of the next two calls, `main` made 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 on `main`, 8 on the PR). CPU-only synthetic readbacks. AI-assisted with OpenAI Codex.
KelboLive commented 2026-11-30 15:43:38 +00:00 (Migrated from github.com)

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.rs only (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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7283476932 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2696#issuecomment-7283476932). Originally posted by [KelboLive](https://github.com/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.rs` only (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).
merzleckeier commented 2026-11-30 17:28:38 +00:00 (Migrated from github.com)

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 7305728f5 merged onto main ad25611b5 (tree 5929fbeb2). Rust 1.98.1 targeting Windows x64 via cargo-xwin, leonida-gpu --lib with diagnostics, Wine 11.22 on CachyOS.

Two workers call production texback::write on 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 through texback::remapped before releasing the workers. The legacy control invalidates that ticket before entering write.

All 12 fresh-process runs agreed (three per tree and LEONIDA_TEXBACK_LIGHT mode, 144 write calls total). Selected and unselected slices, unchanged copies, ordinary non-copy readbacks, and the subsequent copy after invalidation matched the per-texel Layout::offset oracle 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::tests suite passed 7/7 on main and 8/8 on the PR in both modes, including copy_readbacks_write_changed_pages_across_reused_scratch on the PR. Scope: synthetic CPU writeback and controlled ticket invalidation under Wine. AI-assisted: Codex (GPT-6.2 Sol).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7286359646 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2696#issuecomment-7286359646). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T17:28:38Z. --- ### Concurrent writeback and late ticket invalidation on the current head **MEASURED.** I tested `7305728f5` merged onto `main` `ad25611b5` (tree `5929fbeb2`). Rust 1.98.1 targeting Windows x64 via cargo-xwin, `leonida-gpu --lib` with `diagnostics`, Wine 11.22 on CachyOS. Two workers call production `texback::write` on 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 through `texback::remapped` before releasing the workers. The legacy control invalidates that ticket before entering `write`. All **12 fresh-process runs** agreed (three per tree and `LEONIDA_TEXBACK_LIGHT` mode, **144 write calls** total). Selected and unselected slices, unchanged copies, ordinary non-copy readbacks, and the subsequent copy after invalidation matched the per-texel `Layout::offset` oracle 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::tests` suite passed 7/7 on `main` and 8/8 on the PR in both modes, including `copy_readbacks_write_changed_pages_across_reused_scratch` on the PR. Scope: synthetic CPU writeback and controlled ticket invalidation under Wine. AI-assisted: Codex (GPT-6.2 Sol).
MoonEyes commented 2026-12-01 08:33:38 +00:00 (Migrated from github.com)

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 into main 9372a4d4f (clean).

  • Machine: RTX 4060 8 GB, Windows 11, diag profile, features diagnostics,mimalloc, Series S, BAR window off, texture budget 1024 MB.
  • In-game, no scripted action, 480 s asked, a session that loads into the world and stays: main 9372a4d4f and then main + 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 on main, not a comparison: other work ran on the PC during the main run.
  • Picture: screenshots every 60 s. The last four show the same scene in both runs (water, sky, the boat, the map) and look the same; the first four are cutscenes at different moments and were only looked at for broken textures (none seen). No screenshot is posted.
  • Tests on main 9372a4d4f + the seven (Windows): leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: two source_hash_tests failed 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7305479084 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2696#issuecomment-7305479084). Originally posted by [MoonEyes](https://github.com/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 into `main` `9372a4d4f` (clean). - **Machine:** RTX 4060 8 GB, Windows 11, diag profile, features diagnostics,mimalloc, Series S, BAR window off, texture budget 1024 MB. - **In-game, no scripted action, 480 s asked, a session that loads into the world and stays:** `main` `9372a4d4f` and then `main` + 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 on `main`, **not a comparison**: other work ran on the PC during the `main` run. - **Picture:** screenshots every 60 s. The last four show the same scene in both runs (water, sky, the boat, the map) and look the same; the first four are cutscenes at different moments and were only looked at for broken textures (none seen). No screenshot is posted. - **Tests on `main` `9372a4d4f` + the seven (Windows):** leonida-hle 243, leonida-gpu 602, leonida-formats 47, leonida 47, leonida-shader 403 of 405: two `source_hash_tests` failed 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).
MoonEyes commented 2026-12-01 09:51:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T09:51:18Z.


Merging. Head 7305728f5 merges cleanly with main 0259473b0 and 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7307302995 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2696#issuecomment-7307302995). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-01T09:51:18Z. --- Merging. Head `7305728f5` merges cleanly with `main` `0259473b0` and 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).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2696
No description provided.