Merge fork main 8dd30ab: writes through one view of a page are seen at its other views

The fork's guard over every view of a physical page is kept on our own block
bits, known holes, kernel writes and changing blocks; what both had, ours.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 00:38:57 -06:00
commit c45d109380
23 files changed
+4164 -221

No files matched your search

+305 -69
View File
@@ -220,6 +220,40 @@ fn card_limits() -> (u64, u64) {
/// Bytes alive of copies' card sides, and of their "before" twins.
static CARD_BYTES: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2];
/// Whether the texture queue leaves room on the card for the card copies
/// of title buffers that are not made yet
/// (`VICTORY_CARD_ROOM_FOR_BUFFERS=1`; off by default).
///
/// The copies are made in one burst when the world loads (run 189: 94 of
/// its 95 within two seconds, right before the first world frame), and
/// [`card_for`] makes one only while the card is under eight tenths of its
/// budget. That held as long as the textures picked by index filled the
/// card after the burst. Since the texture queue keeps packed neighbours
/// (52b8060) it fills the card while the world loads: 85% and more at the
/// burst in runs 190 to 192, so one copy of 95 was made, the CPU looked
/// for shader writes in 600 to 720 MB a frame where it had been 30, and
/// the frame went from about 240 to 370-470 ms.
///
/// With the switch, the texture queue's rule (`resource::room`) counts the
/// part of [`card_limits`]' second number that no copy has taken yet as
/// used, and a copy may be made up to nine tenths of the budget: the room
/// it takes was kept for it. After the burst what is kept is what the
/// burst left of the limit (87 MB of 1,024 in run 189).
fn card_room_kept_on() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_CARD_ROOM_FOR_BUFFERS").is_some_and(|v| v == "1"))
}
/// The bytes of the card's memory the texture queue leaves free for card
/// copies of title buffers not made yet ([`card_room_kept_on`]): 0 with
/// the switch off or without card copies.
pub(crate) fn card_room_kept() -> u64 {
if !card_room_kept_on() || !cards() {
return 0;
}
card_limits().1.saturating_sub(CARD_BYTES[0].load(Ordering::Relaxed))
}
/// The copies in the card's memory.
static CARDED: Mutex<Vec<std::sync::Weak<Shadow>>> = Mutex::new(Vec::new());
@@ -270,8 +304,11 @@ fn card_for(size: u64) -> Option<ID3D12Resource> {
// queue's own rule (`resource::room`: nine tenths of the budget) counts
// them, so the card's use stays where it was and the textures' share
// shrinks by the copies' bytes, at most `card_limits`' second number.
// (With `card_room_kept_on` the texture queue kept this copy's room
// free, and the copy may go up to the queue's own line.)
let tenths = if card_room_kept_on() { 9 } else { 8 };
if let Some([budget, used, ..]) = host.video_memory()
&& used + size > budget / 10 * 8
&& used + size > budget / 10 * tenths
{
return None;
}
@@ -942,51 +979,54 @@ const NOT_WATCHED: u8 = 2;
/// Watched, not by the kernel but by the guards, when last asked about.
const GUARDED: u8 = 3;
/// Where `a` and `b` (of one length) first differ, compared eight bytes at
/// a time.
// Fixed-size equality compiles to two SIMD comparisons on Windows x64.
// Only the first or last unequal chunk needs a byte-wise search.
const DIFF_CHUNK: usize = 32;
/// Where `a` and `b` first differ within their common length.
fn first_difference(a: &[u8], b: &[u8]) -> Option<usize> {
let whole = a.len().min(b.len()) / 8 * 8;
let from = a[..whole]
.as_chunks::<8>().0.iter()
.zip(b[..whole].chunks_exact(8))
.position(|(x, y)| x != y)
.map_or(whole, |word| word * 8);
a[from..].iter().zip(&b[from..]).position(|(x, y)| x != y).map(|i| from + i)
let len = a.len().min(b.len());
let (a_chunks, a_tail) = a[..len].as_chunks::<DIFF_CHUNK>();
let (b_chunks, b_tail) = b[..len].as_chunks::<DIFF_CHUNK>();
let (offset, a, b) = match a_chunks.iter().zip(b_chunks).position(|(x, y)| x != y) {
Some(i) => (i * DIFF_CHUNK, &a_chunks[i][..], &b_chunks[i][..]),
None => (len - a_tail.len(), a_tail, b_tail),
};
a.iter().zip(b).position(|(x, y)| x != y).map(|i| offset + i)
}
/// Where `a` and `b` (of one length) last differ.
/// Where `a` and `b` last differ within their common length.
fn last_difference(a: &[u8], b: &[u8]) -> Option<usize> {
let whole = a.len().min(b.len()) / 8 * 8;
if let Some(i) = a[whole..].iter().zip(&b[whole..]).rposition(|(x, y)| x != y) {
return Some(whole + i);
let len = a.len().min(b.len());
let (a_chunks, a_tail) = a[..len].as_chunks::<DIFF_CHUNK>();
let (b_chunks, b_tail) = b[..len].as_chunks::<DIFF_CHUNK>();
if let Some(i) = a_tail.iter().zip(b_tail).rposition(|(x, y)| x != y) {
return Some(len - a_tail.len() + i);
}
let word = a[..whole]
.as_chunks::<8>().0.iter()
.zip(b[..whole].chunks_exact(8))
.rposition(|(x, y)| x != y)?
* 8;
a[word..word + 8]
let chunk = a_chunks.iter().zip(b_chunks).rposition(|(x, y)| x != y)?;
a_chunks[chunk]
.iter()
.zip(&b[word..word + 8])
.zip(&b_chunks[chunk])
.rposition(|(x, y)| x != y)
.map(|i| word + i)
.map(|i| chunk * DIFF_CHUNK + i)
}
#[cfg(test)]
mod difference_tests {
use super::{first_difference, last_difference};
/// The word-wise searches agree with the byte-wise ones they replace.
/// The chunk searches agree with a byte-wise scan across chunk boundaries.
#[test]
fn differences_are_found_as_byte_by_byte() {
let base: Vec<u8> = (0..61u32).map(|i| (i * 7 + 3) as u8).collect();
let same = base.clone();
assert_eq!(first_difference(&base, &same), None);
assert_eq!(last_difference(&base, &same), None);
for len in [0usize, 1, 7, 8, 9, 16, 23, 61] {
let base: Vec<u8> = (0..98u32).map(|i| (i * 7 + 3) as u8).collect();
for len in [0, 1, 31, 32, 33, 63, 64, 65, 97] {
let a = &base[1..1 + len];
let same = a.to_vec();
assert_eq!(first_difference(&same, a), None);
assert_eq!(last_difference(&same, a), None);
for at in 0..len {
for other in at..len {
let (a, mut b) = (&base[..len], base[..len].to_vec());
let mut b = a.to_vec();
b[at] ^= 0x80;
b[other] ^= 0x01;
let first = a.iter().zip(&b).position(|(x, y)| x != y);
@@ -997,6 +1037,19 @@ mod difference_tests {
}
}
}
#[test]
fn differences_ignore_bytes_past_the_shorter_slice() {
let a = [0; 65];
let mut b = [0; 67];
b[65] = 1;
assert_eq!(first_difference(&a, &b), None);
assert_eq!(last_difference(&b, &a), None);
b[32] = 1;
b[64] = 1;
assert_eq!(first_difference(&b, &a), Some(32));
assert_eq!(last_difference(&a, &b), Some(64));
}
}
/// The unit shader writes are looked for and put back in.
@@ -1448,6 +1501,80 @@ mod wide_tests {
}
}
/// What [`Shadow::copy_back`] does with a piece that differs from the
/// snapshot: `host` is the copy's bytes there, `old` the snapshot's. From
/// the first byte that differs to the last, title memory is read into
/// `title` (scratch, sized to that stretch) by `read` (the stretch's offset
/// in the piece, where to), the GPU's bytes are put in and `host` takes the
/// result. The stretch's offset in the piece and its length if that changed
/// title memory; none if nothing differs or title memory is not there.
/// `runs` takes the runs of the stretch (offsets into `title`, lengths)
/// whose bytes are the GPU's and differ from the title's as read: only
/// those are to be written, so a title write that lands between the read
/// and the write elsewhere in the stretch stays (upstream VIctory #2009).
/// `mask` is scratch.
fn back_piece(
host: &mut [u8],
old: &[u8],
title: &mut Vec<u8>,
mask: &mut Vec<u8>,
runs: &mut Vec<(usize, usize)>,
read: impl FnOnce(usize, &mut [u8]) -> bool,
) -> Option<(usize, usize)> {
runs.clear();
// From the first byte that differs to the last: title memory is read
// and written there only (a shader that sets a few words of a piece
// cost a read, a walk and a write of all 64 KiB of it).
let first = first_difference(host, old)?;
let last = last_difference(host, old).unwrap_or(first);
let n = last + 1 - first;
let (host, old) = (&mut host[first..=last], &old[first..=last]);
title.clear();
title.resize(n, 0);
if !read(first, title) {
return None;
}
// Title memory with the GPU's bytes put in; the copy takes the title's
// bytes in turn.
// Without a branch, so the loop runs many bytes at a time: the GPU's
// byte where the copy's byte is not what it was before the submission
// and the title left its byte as it was, else the title's. (With the
// two searches above this was 44 ms a frame for 970 pieces.)
// Where the shader and the title both changed a byte while the GPU ran,
// the title's stays: the shader computed its byte from the copy's older
// data (one cleared a bit of a record the title had already replaced,
// and copying it back broke the title's object: an access violation
// through 0xffffffff00000000; PalmVIDev e0b70476, VIctory f39c6c2).
mask.clear();
mask.resize(n, 0);
let mut differs = 0u8;
for (((t, h), o), m) in title.iter_mut().zip(host.iter_mut()).zip(old).zip(mask.iter_mut()) {
let gpu = if (*h != *o) & (*t == *o) { 0xffu8 } else { 0 };
let merged = (*t & !gpu) | (*h & gpu);
*m = merged ^ *t;
differs |= *m;
*t = merged;
*h = merged;
}
if differs == 0 {
return None;
}
let mut i = 0;
while i < n {
while i < n && mask[i] == 0 {
i += 1;
}
let start = i;
while i < n && mask[i] != 0 {
i += 1;
}
if i > start {
runs.push((start, i - start));
}
}
Some((first, n))
}
/// Since the last frame-rate line, of [`Shadow::merge_under_writes`]: pages
/// compared, changed runs put in, and their bytes.
static MERGED: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3];
@@ -1668,7 +1795,94 @@ pub(crate) fn land_as_before(start: usize, now: usize, held: usize, n: usize, ti
#[cfg(test)]
mod merge_tests {
use super::{changed_runs, dword_runs, put_changed};
use super::{PIECE, back_piece, changed_runs, dword_runs, put_changed};
/// A piece put back takes the GPU's bytes where the copy changed and
/// the title's everywhere else, reads and writes title memory only from
/// the first change to the last, and its scratch holds that stretch and
/// no more: not the 64 KiB per range it once was.
#[test]
fn a_piece_put_back_reads_only_its_stretch() {
let old: Vec<u8> = (0..PIECE).map(|i| (i * 7 + i / 251) as u8).collect();
// The title wrote 7 and 40,000 since the snapshot; the GPU wrote 5,
// 9 and 40,000 (the GPU's byte wins where both wrote).
let mut title_memory = old.clone();
title_memory[7] ^= 0x11;
title_memory[40_000] ^= 0x22;
let mut host = old.clone();
for at in [5, 9] {
host[at] ^= 0x33;
}
let gpu = host.clone();
let mut scratch = Vec::new();
let (mut mask, mut runs) = (Vec::new(), Vec::new());
let mut asked = None;
let read = |first: usize, into: &mut [u8]| {
asked = Some((first, into.len()));
into.copy_from_slice(&title_memory[first..first + into.len()]);
true
};
assert_eq!(back_piece(&mut host, &old, &mut scratch, &mut mask, &mut runs, read), Some((5, 5)));
assert_eq!(asked, Some((5, 5)));
let mut wanted = title_memory[5..10].to_vec();
wanted[0] = gpu[5];
wanted[4] = gpu[9];
assert_eq!(scratch, wanted);
assert_eq!(host[5..10], wanted);
// Only the GPU's two bytes are written back (upstream #2009).
assert_eq!(runs, [(0, 1), (4, 1)]);
// The copy outside the stretch is left as it was.
assert!(host[..5] == gpu[..5] && host[10..] == gpu[10..]);
assert!(scratch.capacity() < PIECE / 2, "scratch of {} bytes for a stretch of 5", scratch.capacity());
// Nothing differs: title memory is not read.
let mut same = old.clone();
assert_eq!(back_piece(&mut same, &old, &mut scratch, &mut mask, &mut runs, |_, _| unreachable!()), None);
// Title memory not there: nothing to write, the copy unchanged.
let mut host = gpu.clone();
assert_eq!(back_piece(&mut host, &old, &mut scratch, &mut mask, &mut runs, |_, _| false), None);
assert!(host == gpu);
// The GPU wrote what the title holds already: nothing to write.
let mut title_memory = old.clone();
title_memory[5] = gpu[5];
title_memory[9] = gpu[9];
let mut host = gpu.clone();
let read = |first: usize, into: &mut [u8]| {
into.copy_from_slice(&title_memory[first..first + into.len()]);
true
};
assert_eq!(back_piece(&mut host, &old, &mut scratch, &mut mask, &mut runs, read), None);
// A whole piece changed: the scratch grows to it.
let mut host: Vec<u8> = old.iter().map(|b| !b).collect();
assert_eq!(back_piece(&mut host, &old, &mut scratch, &mut mask, &mut runs, |_, into| into.len() == PIECE), Some((0, PIECE)));
assert_eq!(scratch.len(), PIECE);
}
/// A byte the shader and the title both changed while the GPU ran keeps
/// the title's (the shader computed it from the copy's older data); a
/// byte only the shader changed goes back, and only such bytes are in
/// the runs to write (PalmVIDev e0b70476, upstream #2009).
#[test]
fn a_byte_both_changed_keeps_the_titles_and_only_gpu_bytes_are_written() {
let old = vec![1u8, 2, 3, 4, 5, 6];
let mut host = old.clone();
host[1] = 20; // the GPU alone
host[3] = 40; // both
host[4] = 50; // the GPU alone
let mut title_memory = old.clone();
title_memory[3] = 44;
title_memory[5] = 66; // the title alone
let (mut scratch, mut mask, mut runs) = (Vec::new(), Vec::new(), Vec::new());
let read = |first: usize, into: &mut [u8]| {
into.copy_from_slice(&title_memory[first..first + into.len()]);
true
};
assert_eq!(back_piece(&mut host, &old, &mut scratch, &mut mask, &mut runs, read), Some((1, 4)));
assert_eq!(scratch, [20, 3, 44, 50], "the title's byte where both changed");
assert_eq!(host[1..5], [20, 3, 44, 50], "the copy takes the result");
assert_eq!(runs, [(0, 1), (3, 1)], "the GPU's bytes only");
}
/// What shaders wrote reaches a page that is away only while the runtime
/// changes a mapping; a page that stays away loses it, and nothing else.
@@ -1889,6 +2103,51 @@ mod piece_tests {
}
}
/// The pages of `[a, e)` (title addresses, `a < e`) that a copy whose
/// first page is `base` must take from title memory before it holds the
/// title's bytes: those it never took (`copied` 0), whether or not the
/// title wrote them since tracking began, and those `written` (as
/// `crate::watch::writes` gives it, by page in order) shows written after
/// the copy took them. As [`Shadow::copy_stale`] decides it.
///
/// [`Shadow::stale_under_writes`] and [`Shadow::merge_under_writes`] looked
/// at `written` alone, so a page of a range shaders may be writing that the
/// copy never took, and the title had not written lately, was neither
/// merged nor a reason to settle; the refresh leaves such ranges alone, and
/// shaders read zeros there (fxceless/victory-fork#206: missing geometry
/// with `VICTORY_LATE_WAIT`/`VICTORY_RELAY`).
fn stale_pages_in(base: usize, copied: &[u64], a: usize, e: usize, written: &[(usize, u64)]) -> Vec<usize> {
(a / PAGE..e.div_ceil(PAGE))
.filter(|&page| page >= base)
.filter(|&page| match copied.get(page - base) {
None => false,
Some(0) => true,
Some(&at) => written
.binary_search_by_key(&page, |&(p, _)| p)
.is_ok_and(|i| written[i].1 > at),
})
.collect()
}
#[cfg(test)]
mod stale_tests {
use super::*;
#[test]
fn pages_never_taken_are_stale_written_or_not() {
let base = 0x100;
let copied = [5, 0, 5, 5];
let at = |page: usize| page * PAGE;
// Page 0x101 was never taken; page 0x102 was written after it was
// taken, page 0x103 before.
let written = [(0x102, 6), (0x103, 4)];
assert_eq!(stale_pages_in(base, &copied, at(0x100), at(0x104), &written), [0x101, 0x102]);
// Part of a page counts as the page; pages outside the copy do not.
assert_eq!(stale_pages_in(base, &copied, at(0x101) + 8, at(0x101) + 16, &[]), [0x101]);
assert!(stale_pages_in(base, &copied, at(0x104), at(0x106), &[(0x104, 9)]).is_empty());
}
}
/// Copies of windows outside buffers let go since the start, and their
/// bytes.
static LET_GO: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2];
@@ -2327,43 +2586,24 @@ impl Shadow {
for &(at, n) in &differing {
self.changed(at, at + n);
}
let mut title = vec![0u8; PIECE];
// Sized to each stretch read (`back_piece`): a zeroed 64 KiB per
// range, most of whose ranges differ nowhere, was most of the
// work for small ranges.
let (mut title, mut mask, mut runs) = (Vec::new(), Vec::new(), Vec::new());
for (at, n) in differing {
// 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 = &before[at - from..at - from + n];
// From the first byte that differs to the last: title
// memory is read and written there only (a shader that sets
// a few words of a piece cost a read, a walk and a write of
// all 64 KiB of it).
let Some(first) = first_difference(host, old) else {
let read = |first: usize, title: &mut [u8]| crate::host::read_into((start + at + first) as *const u8, title);
let Some((first, n)) = back_piece(host, old, &mut title, &mut mask, &mut runs, read) else {
continue;
};
let last = last_difference(host, old).unwrap_or(first);
let (at, n) = (at + first, last + 1 - first);
let (host, old) = (&mut host[first..=last], &old[first..=last]);
let title = &mut title[..n];
if !crate::host::read_into((start + at) as *const u8, title) {
continue;
}
// Title memory with the GPU's bytes put in; the copy takes
// the title's bytes in turn.
// Without a branch, so the loop runs many bytes at a time:
// the GPU's byte where the copy's byte is not what it was
// before the submission, else the title's. (With the two
// searches above this was 44 ms a frame for 970 pieces.)
let mut differs = 0u8;
let was = title.to_vec();
for ((t, h), o) in title.iter_mut().zip(host.iter_mut()).zip(old) {
let gpu = if *h != *o { 0xffu8 } else { 0 };
let merged = (*t & !gpu) | (*h & gpu);
differs |= merged ^ *t;
*t = merged;
*h = merged;
}
if differs != 0 {
written += put_changed(start + at, &was, title);
// Title memory as read, beside it with the card's bytes put
// in: only the dwords that differ are written.
let was: Vec<u8> = title.iter().zip(&mask).map(|(now, changed)| now ^ changed).collect();
if !runs.is_empty() {
written += put_changed(start + at + first, &was[..n], &title[..n]);
}
}
// Our write shows as a title write at the copy's next refresh,
@@ -2625,9 +2865,7 @@ impl Shadow {
let base = start / PAGE;
let copied = self.copied.lock().unwrap_or_else(|e| e.into_inner());
busy.into_iter().any(|(a, e)| match crate::watch::asked(a, e) {
Some((_, written)) => written.into_iter().any(|(page, write)| {
page >= base && copied.get(page - base).is_none_or(|&at| at == 0 || write > at)
}),
Some((_, written)) => !stale_pages_in(base, &copied, a, e, &written).is_empty(),
None => true,
})
}
@@ -2689,11 +2927,7 @@ impl Shadow {
};
let stale: Vec<usize> = {
let copied = self.copied.lock().unwrap_or_else(|e| e.into_inner());
written
.into_iter()
.filter(|&(page, write)| page >= base && copied.get(page - base).is_none_or(|&at| at == 0 || write > at))
.map(|(page, _)| page)
.collect()
stale_pages_in(base, &copied, a, e, &written)
};
for page in stale {
let (lo, hi) = ((page * PAGE).max(a), ((page + 1) * PAGE).min(e));
@@ -2888,7 +3122,7 @@ impl Shadow {
/// title's that arrived between the refresh and the look.
fn refresh_check(&self, from: usize, to: usize) {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if !*ON.get_or_init(|| std::env::var_os("VICTORY_REFRESH_CHECK").is_some_and(|v| v == "1") || crate::host::walls_trace()) {
if !*ON.get_or_init(|| std::env::var_os("VICTORY_REFRESH_CHECK").is_some_and(|v| v == "1")) {
return;
}
const SAMPLE: usize = 256;
@@ -3268,6 +3502,8 @@ impl Shadow {
title.resize(n, 0);
let mut written = 0;
if crate::host::read_into((start + at) as *const u8, title) {
// The stretch as read: what the card did not change is the
// title's, and may have been written again since.
// As in `copy_back`: the card's byte where it is not what it
// was before the submission, else the title's.
let mut differs = 0u8;