From 7305728f5b9530839dc0b10bc6ed72a366a18d33 Mon Sep 17 00:00:00 2001 From: bobcatchris <83428450+bobcatchris@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:02:49 -0600 Subject: [PATCH] leonida-gpu: reuse thread-local before snapshot buffer in texback::write (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, Vec)` (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) --- d3d12/crates/leonida-gpu/src/texback.rs | 67 ++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/texback.rs b/d3d12/crates/leonida-gpu/src/texback.rs index 92ee5afc44..c578e23c82 100644 --- a/d3d12/crates/leonida-gpu/src/texback.rs +++ b/d3d12/crates/leonida-gpu/src/texback.rs @@ -429,8 +429,10 @@ fn write(landing: &Landing) { use crate::land::{count, spent, stat}; static COPIED: AtomicU64 = AtomicU64::new(0); thread_local! { - /// The texture as it is tiled, kept from one to the next ([`light`]). - static TILED: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; + /// The texture as it is tiled and its pre-tile snapshot (for `copy` + /// page diffing), kept from one landing to the next ([`light`]). + static SCRATCH: std::cell::RefCell<(Vec, Vec)> = + const { std::cell::RefCell::new((Vec::new(), Vec::new())) }; } /// Memory kept for the next up to this size. const KEPT: usize = 32 << 20; @@ -446,9 +448,6 @@ fn write(landing: &Landing) { let (at, end) = (landing.ticket.at, landing.ticket.end); let size = end - at; let copy = landing.ticket.copy; - // What title memory held, for a copy's write: only the pages that change - // go back ([`changed_pages`]). - let mut before = Vec::new(); // The texture as it will be, in `tiled` (`size` bytes); whether it is. let tile = |tiled: &mut [u8], before: &mut Vec| { // Keep title bytes the subresources do not cover. @@ -491,17 +490,17 @@ fn write(landing: &Landing) { put }; let bytes = if light { - let written = TILED.with_borrow_mut(|kept| { + let written = SCRATCH.with_borrow_mut(|(kept, before)| { if kept.len() < size { kept.resize(size, 0); } - let written = if tile(&mut kept[..size], &mut before) { + let written = if tile(&mut kept[..size], before) { // The lock for the write alone: who lets the allocation go // or maps other memory there waits for a copy, not for the // tiling, and what it made invalid meanwhile is not written. let _tickets = TICKETS.lock().unwrap_or_else(|e| e.into_inner()); if landing.ticket.valid.load(Ordering::Acquire) { - Some(put(&kept[..size], &before)) + Some(put(&kept[..size], before)) } else { stale(); None @@ -512,6 +511,9 @@ fn write(landing: &Landing) { if kept.capacity() > KEPT { *kept = Vec::new(); } + if before.capacity() > KEPT { + *before = Vec::new(); + } written }); let Some(bytes) = written else { @@ -520,6 +522,7 @@ fn write(landing: &Landing) { bytes } else { let mut tiled = vec![0u8; size]; + let mut before = Vec::new(); if !tile(&mut tiled, &mut before) { return; } @@ -770,4 +773,52 @@ mod tests { assert_eq!(now(), second, "a read-back was written into an allocation that went"); drop(title); } + + #[test] + fn copy_readbacks_write_changed_pages_across_reused_scratch() { + use leonida_formats::texture::{Desc, Dimension, Layout, Swizzle}; + const OWNER: usize = 0x7e57_bac1; + let layout = Layout::new(&Desc { + dimension: Dimension::Tex2d, + width: 64, + height: 32, + depth: 1, + mips: 1, + element_bytes: 4, + block: 1, + samples: 1, + swizzle: Swizzle::LINEAR, + }) + .unwrap(); + let size = layout.size as usize; // 8192 bytes (2 pages) + let mut title = vec![0x11u8; size]; + let at = title.as_mut_ptr() as usize; + let (pitch, rows) = (256usize, 32u32); + let make_landing = |data: Vec| Landing { + footprints: vec![D3D12_PLACED_SUBRESOURCE_FOOTPRINT { + Offset: 0, + Footprint: D3D12_SUBRESOURCE_FOOTPRINT { Width: 64, Height: 32, Depth: 1, RowPitch: pitch as u32, ..Default::default() }, + }], + rows: vec![rows], + mips: 1, + layout: layout.clone(), + ticket: track(OWNER, at, at + size, true), + data, + subs: u64::MAX, + }; + // First copy modifies only page 1; page 0 stays 0x11. + let mut d1 = vec![0x11u8; size]; + d1[4096..4128].fill(0x22); + write(&make_landing(d1.clone())); + // SAFETY: `title` stays live for the duration of the test. + let snap1 = unsafe { std::slice::from_raw_parts(at as *const u8, size) }.to_vec(); + assert_eq!(snap1, d1); + // Second copy (reusing thread-local `before`) modifies only page 0; page 1 stays as snap1. + let mut d2 = d1; + d2[0..32].fill(0x33); + write(&make_landing(d2.clone())); + let snap2 = unsafe { std::slice::from_raw_parts(at as *const u8, size) }.to_vec(); + assert_eq!(snap2, d2); + drop(title); + } }