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:
fxcelessandClaude Fable 5.2 committed 2026-11-25 00:44:25 -05:00
1 parent 8d3ce0190f
commit 847a0af35e
25 files changed
+4218 -240

No files matched your search

+69 -6
View File
@@ -519,7 +519,10 @@ pub extern "C" fn _wfsopen(path: *const u16, mode: *const u16, share: i32) -> Fi
// fail there instead of faulting into its handler; the title's
// synchronous reads (title+0x313fc0) read straight into 4 KiB-aligned
// destinations and retry a failed `ReadFile` forever. So the destination
// is opened first.
// is opened first. A block guarded again before the kernel writes it (the
// guard over all the views of a page protects other views from other
// threads) fails that `ReadFile`, and the title's retry opens it again;
// `fread` has no such retry and reads beside instead (see there).
pub extern "system" fn ReadFile(
file: *mut c_void,
@@ -534,12 +537,38 @@ pub extern "system" fn ReadFile(
)
}
/// The host CRT reads large requests straight into the caller's buffer.
/// The host CRT reads large requests straight into the caller's buffer
/// through the kernel, whose write fails on a guarded block instead of
/// faulting into the guard. So the bytes are read beside, a chunk at a time
/// (a caller may ask for far more than the file holds), and copied in from
/// user mode, where a guarded block faults into the guard like the title's
/// own writes. Returns the items read, as the CRT does; the bytes of a
/// partial last item are copied too, as the CRT leaves them.
pub extern "C" fn fread(buffer: *mut u8, size: usize, count: usize, stream: File) -> usize {
victory_gpu::kernel_will_write(buffer as usize, size.saturating_mul(count));
host!("ucrtbase.dll", "fread": extern "C" fn(*mut u8, usize, usize, File) -> usize)(
buffer, size, count, stream,
)
let read = host!("ucrtbase.dll", "fread": extern "C" fn(*mut u8, usize, usize, File) -> usize);
let total = size.saturating_mul(count);
if buffer.is_null() || total == 0 {
// The CRT's own checks and answer (EINVAL, or nothing to read).
return read(buffer, size, count, stream);
}
const CHUNK: usize = 1 << 20;
let mut beside = vec![0u8; total.min(CHUNK)];
let mut done = 0;
while done < total {
let want = (total - done).min(CHUNK);
let n = read(beside.as_mut_ptr(), 1, want, stream);
if n > 0 {
victory_gpu::kernel_will_write(buffer as usize + done, n);
// SAFETY: the caller's buffer holds `size * count` bytes, and
// `done + n` is at most that.
unsafe { std::ptr::copy_nonoverlapping(beside.as_ptr(), buffer.add(done), n) };
done += n;
}
if n < want {
break;
}
}
done / size
}
pub extern "C" fn fopen(path: *const c_char, mode: *const c_char) -> File {
@@ -751,6 +780,40 @@ mod tests {
assert_eq!((&out, n), (b"0123456789", 10));
}
#[test]
fn fread_reads_beside_in_chunks() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("data.bin");
// Over two chunks, and not a whole number of 3-byte items.
let bytes: Vec<u8> = (0..(5 << 19) + 1).map(|i| (i * 7 + i / 251) as u8).collect();
std::fs::write(&path, &bytes).unwrap();
let fclose = host!("ucrtbase.dll", "fclose": extern "C" fn(File) -> i32);
let open = || {
let f = _wfopen(w(&path.to_string_lossy()).as_ptr(), w("rb").as_ptr());
assert!(!f.is_null());
f
};
// Asked for far more than the file holds: the whole file, in items.
let f = open();
let mut out = vec![0xaau8; bytes.len() + 64];
let items = fread(out.as_mut_ptr(), 3, out.len() / 3, f);
assert_eq!(items, bytes.len() / 3);
assert_eq!(&out[..bytes.len()], &bytes[..], "the partial last item's bytes too");
assert!(out[bytes.len()..].iter().all(|&b| b == 0xaa), "nothing past what was read");
fclose(f);
// Exact reads continue where the last one stopped.
let f = open();
let mut a = vec![0u8; 1000];
let mut b = vec![0u8; (1 << 20) + 5];
assert_eq!(fread(a.as_mut_ptr(), 1000, 1, f), 1);
assert_eq!(fread(b.as_mut_ptr(), 1, b.len(), f), b.len());
assert_eq!(a, bytes[..1000]);
assert_eq!(b, bytes[1000..1000 + b.len()]);
fclose(f);
}
fn text(v: &[u16]) -> String {
String::from_utf16_lossy(&v[..v.len() - 1])
}