A write through one view of a page is seen at its other views; page writes counted; levels on demand (off); warm-up; card memory numbers
Memory sync: - The guard over every view of a physical page (code by myokex, victory-amd c6dc4d8, after pull request #115 by Spexivus, its correction by joroyt and a part of VIctory #1122 by tightfall): the title shows one page at several addresses and a copy was guarded at one only. 0.8 to 1.0 million writes through another view in a five-minute run on the test machine. VICTORY_VIEW_GUARD=0 turns it off. - Kernel reads into title memory are read beside and copied in (Joshua Odom, 10ea38e). - Writes of the storage layer into physical pages are counted where the title has the pages mapped and can open the guards there (VICTORY_PAGE_WRITES, off: counted only). Failed page-destination reads are retried. Card memory: - Levels on demand, off unless VICTORY_LEVELS_ON_DEMAND=1: a texture the title fills takes card memory for the levels the title holds; held levels get memory only while the card has room and are asked for again. Its test runs on the software adapter; not run on a card yet. - Refused texture slots are held back so a pass reaches the slots behind them; room can be kept for card copies of title buffers (off). Tools: translate --warm all; the shader cache is keyed on the translator's source; counters for copy-back races, page writes and the copies the texture sweep never lets go; a walls trace. Game runs of these changes on an RTX 4070: five five-minute runs without a device loss or a failed request. This merged tree was built; its test suites were run on the tree the changes come from (gpu 129, hle 97). Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
1 parent
310c6eb6c1
commit
8dd30ab472
25 files changed
+4218
-240
No files matched your search
@@ -162,6 +162,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());
|
||||
|
||||
@@ -212,8 +246,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;
|
||||
}
|
||||
@@ -598,6 +635,155 @@ fn outside(from: usize, to: usize, mut skip: Vec<(usize, usize)>) -> Vec<(usize,
|
||||
out
|
||||
}
|
||||
|
||||
/// The runs of 4-byte words of title memory (cut on its addresses;
|
||||
/// `read` and `merged` are of the memory at `address`) that hold a byte in
|
||||
/// which `merged` differs from `read`, as (offset, bytes) in order to
|
||||
/// `put`. A shader stores words, so these are what the card wrote; every
|
||||
/// other byte of `merged` is the title's as it was read.
|
||||
fn word_runs(address: usize, read: &[u8], merged: &[u8], mut put: impl FnMut(usize, usize)) {
|
||||
let n = read.len().min(merged.len());
|
||||
let mut i = 0;
|
||||
while i < n {
|
||||
// Equal bytes are passed over, 32 at a time while they last.
|
||||
match first_difference(&read[i..n], &merged[i..n]) {
|
||||
Some(d) => i += d,
|
||||
None => break,
|
||||
}
|
||||
// From the word that holds this byte (or the stretch's first
|
||||
// byte) to the end of the last word in a row that holds one.
|
||||
let first = i - ((address + i) & 3).min(i);
|
||||
let mut end = first;
|
||||
while end < n {
|
||||
let word = (((address + end) | 3) + 1 - address).min(n);
|
||||
if read[end..word] == merged[end..word] {
|
||||
break;
|
||||
}
|
||||
end = word;
|
||||
}
|
||||
put(first, end - first);
|
||||
i = end;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the merges that write a stretch of title memory back
|
||||
/// ([`Shadow::relay_merge`], [`Shadow::card_merge`],
|
||||
/// [`Shadow::card_merge_relay`]) look, right before that write, for title
|
||||
/// bytes it is about to put back to older values (`VICTORY_MERGE_RACE=1`;
|
||||
/// off by default). Logging only: [`merge_race`].
|
||||
fn merge_race_on() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_MERGE_RACE").is_some_and(|v| v == "1"))
|
||||
}
|
||||
|
||||
/// [`merge_race`]'s sums. Of frame `frame`, and in `total` since the
|
||||
/// start: stretches written back, their bytes, title bytes put back to
|
||||
/// older values, bytes the title and the card both changed, stretches with
|
||||
/// either, the runs a write of the card's words alone makes
|
||||
/// ([`word_runs`]) and their bytes. `said`: the frame's first stretches
|
||||
/// with either. `quiet`: frames since the last line.
|
||||
struct MergeRace {
|
||||
frame: u64,
|
||||
now: [u64; 7],
|
||||
said: Vec<String>,
|
||||
total: [u64; 7],
|
||||
quiet: u64,
|
||||
}
|
||||
|
||||
static MERGE_RACE: Mutex<MergeRace> =
|
||||
Mutex::new(MergeRace { frame: 0, now: [0; 7], said: Vec::new(), total: [0; 7], quiet: 0 });
|
||||
|
||||
/// With [`merge_race_on`], right before a merge writes `merged` over title
|
||||
/// memory at `address` (a stretch of the copy of the title buffer `copy`:
|
||||
/// start and bytes): title memory is read once more and compared with
|
||||
/// `read`, what the merge read of it before. A byte that changed in
|
||||
/// between and that the write-back stores with its value as first read
|
||||
/// (the card did not change it) is a title byte put back to an older
|
||||
/// value; one the write-back stores with the card's byte is one both
|
||||
/// changed. Only bytes the write-back stores are looked at. Summed per
|
||||
/// frame; a line for every frame that has any, and one every 100 frames
|
||||
/// whatever it holds, so that "none" is said, not assumed.
|
||||
///
|
||||
/// A lower bound: a title write that lands after this read (while the
|
||||
/// write-back itself runs) is not seen, nor one that wrote a byte and then
|
||||
/// its old value again. Costs one more read and two compares of every
|
||||
/// stretch written back, on the thread that merges.
|
||||
fn merge_race(read: Option<&[u8]>, copy: (u64, u64), address: usize, merged: &[u8], which: &'static str) {
|
||||
if !merge_race_on() {
|
||||
return;
|
||||
}
|
||||
let Some(read) = read else {
|
||||
return;
|
||||
};
|
||||
let n = read.len().min(merged.len());
|
||||
let (read, merged) = (&read[..n], &merged[..n]);
|
||||
let mut runs: Vec<(usize, usize)> = Vec::new();
|
||||
word_runs(address, read, merged, |off, len| runs.push((off, len)));
|
||||
let whole = [(0, n)];
|
||||
let looked: &[(usize, usize)] = &whole;
|
||||
let mut again = vec![0u8; n];
|
||||
let (mut put_back, mut both, mut first) = (0u64, 0u64, None);
|
||||
if crate::host::read_into(address as *const u8, &mut again) {
|
||||
for &(off, len) in looked {
|
||||
for i in off..off + len {
|
||||
if again[i] == read[i] || again[i] == merged[i] {
|
||||
continue;
|
||||
}
|
||||
if merged[i] == read[i] {
|
||||
put_back += 1;
|
||||
first.get_or_insert(i);
|
||||
} else {
|
||||
both += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let frame = crate::frame::current();
|
||||
let mut race = MERGE_RACE.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if race.frame != frame {
|
||||
race.quiet += 1;
|
||||
if race.now[4] > 0 || race.quiet >= 100 {
|
||||
tracing::info!(
|
||||
frame = race.frame,
|
||||
stretches = race.now[0],
|
||||
stretch_bytes = race.now[1],
|
||||
put_back = race.now[2],
|
||||
both_changed = race.now[3],
|
||||
stretches_hit = race.now[4],
|
||||
word_runs = race.now[5],
|
||||
word_run_bytes = race.now[6],
|
||||
stretches_since_start = race.total[0],
|
||||
stretch_bytes_since_start = race.total[1],
|
||||
put_back_since_start = race.total[2],
|
||||
both_changed_since_start = race.total[3],
|
||||
stretches_hit_since_start = race.total[4],
|
||||
word_runs_since_start = race.total[5],
|
||||
word_run_bytes_since_start = race.total[6],
|
||||
first = ?race.said,
|
||||
"merge race: title bytes that changed between a merge's read of title memory and its write-back of what shaders wrote, in this frame (VICTORY_MERGE_RACE=1)"
|
||||
);
|
||||
race.quiet = 0;
|
||||
}
|
||||
race.frame = frame;
|
||||
race.now = [0; 7];
|
||||
race.said.clear();
|
||||
}
|
||||
let hit = u64::from(put_back + both > 0);
|
||||
let run_bytes: u64 = runs.iter().map(|r| r.1 as u64).sum();
|
||||
let add = [1, n as u64, put_back, both, hit, runs.len() as u64, run_bytes];
|
||||
for (i, v) in add.iter().enumerate() {
|
||||
race.now[i] += v;
|
||||
race.total[i] += v;
|
||||
}
|
||||
if hit > 0 && race.said.len() < 8 {
|
||||
race.said.push(format!(
|
||||
"{which}: copy {:#x}+{:#x} stretch {address:#x}+{n:#x}: {put_back} put back (first at {:#x}), {both} both changed",
|
||||
copy.0,
|
||||
copy.1,
|
||||
address + first.unwrap_or(0)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod relay_tests {
|
||||
use super::{Flights, changed_runs, merge_piece, outside};
|
||||
@@ -857,51 +1043,54 @@ fn give_back(v: Vec<u8>) {
|
||||
|
||||
const NOT_WATCHED: u8 = 2;
|
||||
|
||||
/// 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);
|
||||
@@ -912,6 +1101,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.
|
||||
@@ -1325,6 +1527,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];
|
||||
@@ -1363,7 +1639,93 @@ fn changed_runs(now: &[u8], was: &[u8]) -> Vec<(usize, usize)> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod merge_tests {
|
||||
use super::changed_runs;
|
||||
use super::{PIECE, back_piece, changed_runs};
|
||||
|
||||
/// 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");
|
||||
}
|
||||
|
||||
/// The runs hold exactly the bytes that differ: putting them into the
|
||||
/// old bytes gives the new ones, and no run holds an equal byte.
|
||||
@@ -1504,6 +1866,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];
|
||||
@@ -1927,44 +2334,27 @@ 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;
|
||||
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 {
|
||||
crate::watch::writing(start + at, n);
|
||||
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
|
||||
written += n;
|
||||
let at = at + first;
|
||||
crate::watch::writing(start + at, n);
|
||||
// Only the GPU's bytes (upstream #2009): the rest of the
|
||||
// stretch is the title's as read, and a title write landing
|
||||
// there since stays.
|
||||
for &(off, len) in &runs {
|
||||
crate::host::write_mapped((start + at + off) as *mut u8, title[off..].as_ptr(), len);
|
||||
written += len;
|
||||
}
|
||||
}
|
||||
// Our write shows as a title write at the copy's next refresh,
|
||||
@@ -2048,10 +2438,14 @@ impl Shadow {
|
||||
if !crate::host::read_into((start + at) as *const u8, title) {
|
||||
return 0;
|
||||
}
|
||||
// The stretch as read: what the card did not change is the
|
||||
// title's, and may have been written again since.
|
||||
let as_read = merge_race_on().then(|| title.clone());
|
||||
if !merge_piece(now, held, title) {
|
||||
return 0;
|
||||
}
|
||||
crate::watch::writing(start + at, n);
|
||||
merge_race(as_read.as_deref(), (self.start, self.len), start + at, &title[..], "pc relay");
|
||||
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
|
||||
n
|
||||
}
|
||||
@@ -2221,9 +2615,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,
|
||||
})
|
||||
}
|
||||
@@ -2278,11 +2670,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));
|
||||
@@ -2435,7 +2823,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;
|
||||
@@ -2662,6 +3050,9 @@ 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.
|
||||
let as_read = merge_race_on().then(|| title.clone());
|
||||
// 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;
|
||||
@@ -2674,6 +3065,7 @@ impl Shadow {
|
||||
}
|
||||
if differs != 0 {
|
||||
crate::watch::writing(start + at, n);
|
||||
merge_race(as_read.as_deref(), (self.start, self.len), start + at, &title[..], "card");
|
||||
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
|
||||
written = n;
|
||||
}
|
||||
@@ -2738,6 +3130,9 @@ impl Shadow {
|
||||
title.clear();
|
||||
title.resize(n, 0);
|
||||
let mapped = 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.
|
||||
let as_read = (mapped && merge_race_on()).then(|| title.clone());
|
||||
let mut changed = false;
|
||||
for (a, b) in outside(at, at + n, skip) {
|
||||
let (a, b) = (a - at, b - at);
|
||||
@@ -2749,6 +3144,7 @@ impl Shadow {
|
||||
return 0;
|
||||
}
|
||||
crate::watch::writing(start + at, n);
|
||||
merge_race(as_read.as_deref(), (self.start, self.len), start + at, &title[..], "card relay");
|
||||
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
|
||||
n
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user