Skip to content
Closed
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
193 changes: 170 additions & 23 deletions crates/victory-gpu/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ struct Before {
spans: Vec<(usize, usize)>,
}

/// A title page's bytes in the copy before the submission ran: `[lo, hi)`
/// of `bytes` (offsets in the page; a page's buffer is a page long).
/// A title page's snapshot before the submission ran. The page-local
/// bounds `[lo, hi)` can contain gaps outside the declared write spans.
struct Snap {
lo: usize,
hi: usize,
Expand Down Expand Up @@ -179,6 +179,7 @@ impl Shadow {
if from >= to || before.spans.iter().any(|&(a, b)| a <= from && to <= b) {
return;
}
let new = outside(from, to, before.spans.clone());
let (mut a, mut b) = (from, to);
before.spans.retain(|&(x, y)| {
let meets = x <= b && a <= y;
Expand All @@ -188,25 +189,22 @@ impl Shadow {
!meets
});
before.spans.push((a, b));
let mut at = from;
while at < to {
let (page, lo) = ((start + at) / PAGE, (start + at) % PAGE);
let hi = lo + (PAGE - lo).min(to - at);
let snap = before.pages.entry(page).or_insert_with(|| Snap {
lo,
hi: lo,
bytes: page_buffer(),
});
if lo < snap.lo {
snap.bytes[lo..snap.lo].copy_from_slice(host(page, lo, snap.lo));
snap.lo = lo;
}
if hi > snap.hi {
let from = snap.hi.max(snap.lo);
snap.bytes[from..hi].copy_from_slice(host(page, from, hi));
snap.hi = hi;
for (mut at, to) in new {
while at < to {
let (page, lo) = ((start + at) / PAGE, (start + at) % PAGE);
let hi = lo + (PAGE - lo).min(to - at);
let snap = before.pages.entry(page).or_insert_with(|| Snap {
lo,
hi,
bytes: page_buffer(),
});
// Save only newly declared bytes. A gap within a page may be
// refreshed before it is declared for writing.
snap.bytes[lo..hi].copy_from_slice(host(page, lo, hi));
snap.lo = snap.lo.min(lo);
snap.hi = snap.hi.max(hi);
at += hi - lo;
}
at += hi - lo;
}
}

Expand All @@ -233,7 +231,7 @@ impl Shadow {
let n = (PAGE - lo).min(to - at);
let Some(snap) = snapshot
.pages
.get(&page)
.get_mut(&page)
.filter(|s| s.lo <= lo && lo + n <= s.hi)
else {
at += n;
Expand All @@ -242,18 +240,21 @@ impl Shadow {
// SAFETY: inside the mapping; only the submitting thread
// writes it.
let host = unsafe { std::slice::from_raw_parts_mut(self.data.add(at), n) };
let old = &snap.bytes[lo..lo + n];
let old = &mut snap.bytes[lo..lo + n];
let title = &mut scratch[..n];
if host != old && crate::host::read_into((start + at) as *const u8, title) {
// Title memory with the GPU's bytes put in; the copy
// takes the title's bytes in turn.
let mut changed = false;
for ((t, h), o) in title.iter_mut().zip(host.iter_mut()).zip(old) {
for ((t, h), o) in title.iter_mut().zip(host.iter_mut()).zip(old.iter_mut()) {
if *h != *o && *t != *h {
*t = *h;
changed = true;
}
*h = *t;
// A later overlapping range must not see merged
// title bytes as fresh GPU writes.
*o = *t;
}
if changed {
crate::watch::writing(start + at, n);
Expand Down Expand Up @@ -398,6 +399,22 @@ impl Shadow {
}
}

/// The portions of a span not covered by earlier declarations.
fn outside(from: usize, to: usize, mut ranges: Vec<(usize, usize)>) -> Vec<(usize, usize)> {
ranges.sort_unstable();
let (mut at, mut parts) = (from, Vec::new());
for (a, e) in ranges {
if at < to && a > at {
parts.push((at, a.min(to)));
}
at = at.max(e);
}
if at < to {
parts.push((at, to));
}
parts
}

struct Entry {
end: u64,
shadow: Option<Arc<Shadow>>,
Expand Down Expand Up @@ -619,3 +636,133 @@ mod tests {
drop(memory);
}
}

#[cfg(test)]
mod snapshot_preservation_controls {
use super::*;

/// Three pages of title memory on page bounds (plain heap memory, so
/// not write-watched: refreshes copy whole ranges) and their host
/// copy; `None` without a host GPU. Returns the memory's owner, the
/// address of the first page and the copy.
fn title() -> Option<(Vec<u8>, usize, Shadow)> {
let memory = vec![0u8; 4 * PAGE];
let base = (memory.as_ptr() as usize).next_multiple_of(PAGE);
let (buffer, data) = host()?.shared_buffer(3 * PAGE as u64)?;
// SAFETY: a live buffer.
let gpu = unsafe { buffer.GetGPUVirtualAddress() };
let shadow = Shadow::new(buffer, data, gpu, base as u64, 3 * PAGE as u64);
Some((memory, base, shadow))
}

/// A write of one byte: the title's into its memory, or a shader's
/// into the copy.
fn poke(at: usize, value: u8) {
// SAFETY: the tests pass addresses inside the memory or the copy.
unsafe { (at as *mut u8).write_volatile(value) }
}

fn peek(at: usize) -> u8 {
// SAFETY: as in `poke`.
unsafe { (at as *const u8).read_volatile() }
}

/// A range declared after ranges on both sides of it, once the snapshot
/// already spans it: what its refresh brought into the copy is not a
/// shader's write, and does not go back over what the title wrote since.
#[test]
fn a_range_between_earlier_ones_keeps_the_titles_newer_bytes() {
let Some((_memory, base, s)) = title() else {
eprintln!("no GPU; Shadow snapshot control skipped");
return;
};
let (a, b, c) = (base, base + PAGE, base + 2 * PAGE);
let copy = |at: usize| s.data as usize + (at - base);
// An earlier submission read the middle page; the title has written
// it since.
s.update(b, PAGE);
poke(b + 5, 1);
poke(b + 6, 3);
// This submission's shaders write the outer pages, then the middle
// one: each range refreshed, then declared.
for at in [a, c, b] {
s.update(at, PAGE);
s.writing(at, PAGE);
}
assert_eq!(peek(copy(b + 5)), 1, "the refresh");
// While the GPU runs: a shader writes two bytes, the title one.
poke(copy(a + 7), 9);
poke(copy(b + 9), 7);
poke(b + 5, 2);
for at in [a, c, b] {
s.copy_back(at, PAGE);
}
assert_eq!(peek(b + 5), 2, "the title's newer byte");
assert_eq!(peek(b + 6), 3, "the title's byte from before");
// What the shader wrote does go back, in either kind of range.
assert_eq!((peek(a + 7), peek(b + 9)), (9, 7));
eprintln!("gap-range Shadow snapshot readback completed");
assert!(s.before.lock().unwrap().pages.is_empty(), "the snapshot is done");
}

#[test]
fn a_gap_inside_one_page_keeps_the_titles_newer_bytes() {
let Some((_memory, base, s)) = title() else {
eprintln!("no GPU; Shadow snapshot control skipped");
return;
};
let (a, b, c) = (base, base + 256, base + 512);
let copy = |at: usize| s.data as usize + (at - base);
// An earlier submission read the middle page; the title has written
// it since.
s.update(b, 32);
poke(b + 5, 1);
poke(b + 6, 3);
// This submission's shaders write the outer pages, then the middle
// one: each range refreshed, then declared.
for at in [a, c, b] {
s.update(at, 32);
s.writing(at, 32);
}
assert_eq!(peek(copy(b + 5)), 1, "the refresh");
// While the GPU runs: a shader writes two bytes, the title one.
poke(copy(a + 7), 9);
poke(copy(b + 9), 7);
poke(b + 5, 2);
for at in [a, c, b] {
s.copy_back(at, 32);
}
assert_eq!(peek(b + 5), 2, "the title's newer byte");
assert_eq!(peek(b + 6), 3, "the title's byte from before");
// What the shader wrote does go back, in either kind of range.
assert_eq!((peek(a + 7), peek(b + 9)), (9, 7));
eprintln!("intra-page gap Shadow snapshot readback completed");
assert!(s.before.lock().unwrap().pages.is_empty(), "the snapshot is done");
}

/// Two declared ranges that overlap: the first one's copy-back gives
/// the copy the title's bytes, which the second must not take for a
/// shader's and put back over what the title wrote in between.
#[test]
fn overlapping_ranges_keep_the_titles_newer_bytes() {
let Some((_memory, base, s)) = title() else {
eprintln!("no GPU; Shadow snapshot control skipped");
return;
};
let copy = |at: usize| s.data as usize + (at - base);
s.update(base, 2 * PAGE);
s.writing(base, PAGE);
s.writing(base, 2 * PAGE);
// While the GPU runs: a shader's byte and the title's, in one page.
poke(copy(base + 3), 9);
poke(base + 8, 1);
s.copy_back(base, PAGE);
assert_eq!((peek(base + 3), peek(base + 8)), (9, 1));
// The title writes its byte again before the second range's turn.
poke(base + 8, 2);
s.copy_back(base, 2 * PAGE);
assert_eq!(peek(base + 8), 2, "the title's newer byte");
assert_eq!(peek(base + 3), 9, "the shader's byte");
eprintln!("overlapping-range Shadow snapshot readback completed");
}
}