Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions d3d12/crates/leonida-gpu/src/texback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> = 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<u8>, Vec<u8>)> =
const { std::cell::RefCell::new((Vec::new(), Vec::new())) };
}
/// Memory kept for the next up to this size.
const KEPT: usize = 32 << 20;
Expand All @@ -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<u8>| {
// Keep title bytes the subresources do not cover.
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<u8>| 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);
}
}