Skip to content

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

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-gpu-texback-reuse-before
Dec 1, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
bobcatchris:feat/d3d12-gpu-texback-reuse-before

Conversation

@bobcatchris

Copy link
Copy Markdown
Contributor

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

lixonik commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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.

…ite (D3D12-GPU-TEXBACK-REUSE-BEFORE)

texback::write() already kept a thread-local TILED buffer (capped at 32 MiB)
to avoid per-landing heap allocations, but still allocated and freed a fresh
`let mut before = Vec::new()` on every LEONIDA_COPY_BACK read-back (~22/frame
in gameplay) to diff 4 KiB pages.

Pair `before` with `tiled` in a single thread-local SCRATCH `(Vec<u8>, Vec<u8>)`
(both capped at KEPT = 32 MiB) so copy read-backs reuse their snapshot buffer
without repeated multi-MB allocations/page faults.

AI-assisted: Antigravity (Gemini 4 Neon)
@bobcatchris
bobcatchris force-pushed the feat/d3d12-gpu-texback-reuse-before branch from e9e9e2b to 7305728 Compare November 30, 2026 11:21
@KelboLive

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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).

@MoonEyes
MoonEyes merged commit 3393cee into ProjectLeonida:main Dec 1, 2026
5 checks passed
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