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

+125
View File
@@ -364,12 +364,25 @@ struct Slice {
index: usize,
}
impl Slice {
/// The key in `Physical::shown` of the page `k` pages after this one.
fn key(&self, k: usize) -> (usize, usize) {
(Arc::as_ptr(&self.section) as usize, self.index + k)
}
}
#[derive(Default)]
struct Physical {
next_page: usize,
pages: HashMap<usize, Slice>,
/// Mapped views by address: their length and what they show.
views: BTreeMap<usize, (usize, Slice)>,
/// Where each page shows: by its section and its index there, the
/// address of that page in every view that shows it now ([`shown_at`];
/// as upstream's c75fdba keeps it). The title maps pages where the
/// card reads them and has them filled afterwards by DirectStorage,
/// which writes through views of our own.
shown: HashMap<(usize, usize), Vec<usize>>,
}
static PHYSICAL: LazyLock<Mutex<Physical>> = LazyLock::new(|| {
@@ -596,6 +609,58 @@ pub(crate) fn page_window(pages: &[usize], offset: usize, len: usize) -> Option<
})
}
/// The title addresses that show bytes `[offset, offset + len)` of `pages`
/// now, byte for byte, as runs (address, bytes) in address order: a write
/// to the pages through a view of our own ([`page_window`],
/// [`write_physical`]) lands there too, and nothing that watches those
/// addresses sees it (the GPU side's guards fault only on a write at the
/// guarded address itself). Empty if no view shows them. Byte for byte,
/// not by whole pages: the title packs several small textures into one
/// page and fills each with a request of its own.
pub(crate) fn shown_at(pages: &[usize], offset: usize, len: usize) -> Vec<(usize, usize)> {
let Some(end) = offset.checked_add(len) else {
return Vec::new();
};
let first = offset / PAGE;
let Some(touched) = pages.get(first..end.div_ceil(PAGE)) else {
return Vec::new();
};
let mut pieces: Vec<(usize, usize)> = Vec::new();
{
let physical = physical();
for (i, page) in touched.iter().enumerate() {
let Some(addresses) = physical.pages.get(page).and_then(|slice| physical.shown.get(&slice.key(0))) else {
continue;
};
// The bytes of this page that the write covers.
let lo = if i == 0 { offset % PAGE } else { 0 };
let hi = (end - (first + i) * PAGE).min(PAGE);
pieces.extend(addresses.iter().map(|&at| (at + lo, hi - lo)));
}
}
pieces.sort_unstable();
let mut runs: Vec<(usize, usize)> = Vec::new();
for (at, bytes) in pieces {
match runs.last_mut() {
Some((start, n)) if *start + *n == at => *n += bytes,
_ => runs.push((at, bytes)),
}
}
runs
}
/// Bytes `[offset, offset + len)` of `pages` have been written through a
/// view of our own: the GPU side is told at every title address that
/// shows them (`victory_gpu::title_memory_written`; what it does with it
/// is `VICTORY_PAGE_WRITES`' to say, and it counts either way). Call when
/// the bytes are there, and without the page table's lock ([`shown_at`]
/// takes and leaves it).
pub(crate) fn pages_written(pages: &[usize], offset: usize, len: usize) {
for (at, bytes) in shown_at(pages, offset, len) {
victory_gpu::title_memory_written(at, bytes, true);
}
}
/// Maps `n` pages at `address`, in place of what is mapped there; null
/// `pages` unmaps. Returns `address`, or null if a page or the host fails.
pub extern "system" fn XMemMapPhysicalPages(
@@ -746,6 +811,9 @@ fn map(physical: &mut Physical, base: usize, len: usize, slice: Slice) -> bool {
victory_gpu::title_memory_remapped(base, len);
// What the range shows now, for the GPU side's in-place census.
victory_gpu::title_memory_view(base, len, slice.section.0 as usize, (slice.index * PAGE) as u64);
for k in 0..len.div_ceil(PAGE) {
physical.shown.entry(slice.key(k)).or_default().push(base + k * PAGE);
}
physical.views.insert(base, (len, slice));
true
}
@@ -824,6 +892,15 @@ fn unmap(physical: &mut Physical, base: usize, len: usize) {
let Some((size, slice)) = physical.views.remove(&at) else {
continue;
};
for k in 0..size.div_ceil(PAGE) {
let key = slice.key(k);
if let Some(addresses) = physical.shown.get_mut(&key) {
addresses.retain(|&a| a != at + k * PAGE);
if addresses.is_empty() {
physical.shown.remove(&key);
}
}
}
victory_gpu::title_memory_remapped(at, size);
// SAFETY: a view we mapped; it becomes a placeholder again.
unsafe {
@@ -1306,6 +1383,54 @@ mod tests {
assert_eq!(XMemFreePhysicalPages(1, b.as_ptr()), 1);
}
/// Pages say where the title has them mapped, byte for byte (after
/// fork PR #571's test; its runs are whole pages).
#[test]
fn pages_say_where_they_are_mapped() {
let pages = allocate(3);
let reserve = |n: usize| {
let at = XMemVirtualAlloc(std::ptr::null_mut(), n * PAGE, MEM_RESERVE | PAGES_64K, 0, PAGE_READWRITE);
assert!(!at.is_null());
at
};
// `b` keeps a page unmapped at either end, so that no run at `b`
// ever touches one at `a`, wherever the host puts the two.
let (a, b) = (reserve(3), reserve(4));
assert!(shown_at(&pages, 0, 3 * PAGE).is_empty(), "mapped nowhere");
// All three at `a`; the last two at `b`, from its second page on.
assert_eq!(XMemMapPhysicalPages(a, 3, pages.as_ptr()), a);
let second = b.wrapping_byte_add(PAGE);
assert_eq!(XMemMapPhysicalPages(second, 2, pages[1..].as_ptr()), second);
let (a, second) = (a as usize, second as usize);
let sorted = |mut runs: Vec<(usize, usize)>| {
runs.sort_unstable();
runs
};
assert_eq!(shown_at(&pages, 0, PAGE), vec![(a, PAGE)]);
// A request for part of a page names those bytes alone.
assert_eq!(shown_at(&pages, 0x4000, 0x3000), vec![(a + 0x4000, 0x3000)]);
// Across two pages, shown at two addresses each: one run at each.
assert_eq!(
shown_at(&pages, PAGE + 1, PAGE),
sorted(vec![(a + PAGE + 1, PAGE), (second + 1, PAGE)])
);
// A page unmapped in the middle of a view: the rest still shows.
let middle = (a + PAGE) as *mut c_void;
assert_eq!(XMemMapPhysicalPages(middle, 1, std::ptr::null()), middle);
assert_eq!(
shown_at(&pages, 0, 3 * PAGE),
sorted(vec![(a, PAGE), (a + 2 * PAGE, PAGE), (second, 2 * PAGE)])
);
// Too few pages for the bytes: nothing (the write fails too).
assert!(shown_at(&pages, 0, 3 * PAGE + 1).is_empty());
for (at, n) in [(a, 3), (second, 2)] {
let at = at as *mut c_void;
assert_eq!(XMemMapPhysicalPages(at, n, std::ptr::null()), at);
}
assert!(shown_at(&pages, 0, 3 * PAGE).is_empty());
assert_eq!(XMemFreePhysicalPages(3, pages.as_ptr()), 1);
}
#[test]
fn page_windows_cover_one_run_in_place() {
let (a, b) = (allocate(3), allocate(1));