Structure builder: root views as memory by address, windows of its own on the card, upload memory below 2^37

The ray-tracing structure builder's 52 programs translate, validate and make
host pipelines on the 7900 XTX (its waves have 32-64 lanes). What kept it from
working, from a first run (light-1, VICTORY_BVH_QUARANTINE=1) and the code:
- a root view ends with the host copy (a 4 MiB window); structures of megabytes
  cross it. Programs with a spilled user-data table now reach what their root
  views name by address (USER_ROOT_ADDRESS), as upstream VIctory 0f637570 does.
- the list's upload memory (instance descriptions) came from the process heap,
  above 2^40, where shaders' addresses do not reach (upstream 0f637570, 6d021fe3).
- the 1,023 numbered copies of 4 MiB ran out in the first frame: pages come in
  windows of 256 KiB (4 MiB around what a dispatch names), 16,383 at most.
- what the builder stores came back to title memory; now only what it stores in
  small title buffers it names (sizes the title reads).
- the directory lay in the PC's memory and every access stored a fault note.
- a list without a view heap gets the title's; a dropped stage rests the builder
  for a frame, not for the run.
Off unless VICTORY_BVH_BUILDER=1, as before. VICTORY_UPLOAD_LOW=0,
VICTORY_LENT_HEAP=0, VICTORY_MEMORY_WINDOW_KB, VICTORY_MEMORY_CARD_MB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 03:11:24 -06:00
1 parent 0abcb9fd89
commit 87eb9f2449
13 files changed
+727 -177

No files matched your search

+262 -127
View File
@@ -4,15 +4,22 @@
//! structures; the structures themselves lie in plain title memory, in no
//! buffer object).
//!
//! The copies are the ones every other access uses ([`crate::shadow`]: a
//! title buffer's copy, or the 4 MiB window of memory outside buffers), so
//! a root view and a raw address reach the same host bytes. This module
//! keeps what a shader needs to find them:
//! A page in a title buffer that holds all of it is in that buffer's copy
//! ([`crate::shadow`]), which every other access uses. A page outside
//! buffers is in a window of this module's own, on the card
//! ([`crate::shadow::by_address`]): 4 MiB around what a dispatch names (the
//! title's pool of dynamic structures), 256 KiB around a page a shader came
//! to by a pointer (the streamed structures lie anywhere between other
//! data; `VICTORY_MEMORY_WINDOW_KB`).
//!
//! This module keeps what a shader needs to find the copies:
//!
//! - the **directory**: per 64 KiB page of title address space the number
//! of the copy that holds the page (0: none) and the page's byte offset
//! in it; one buffer for all lists, entries only ever go from empty to
//! filled;
//! in it; one for all lists, entries only ever go from empty to filled.
//! The CPU writes it in memory of its own and shaders read a copy of that
//! on the card, brought up to date as title memory is (over the bus, every
//! step of every ray waited for it);
//! - per view heap a run of slots with a raw unordered access view of each
//! numbered copy (slot 0 of the run: a null view, for pages without a
//! copy);
@@ -25,27 +32,26 @@
//! the read gives 0, the store is dropped, and the fault note only helps
//! the next frame. For the structure builder that is the same as a dropped
//! stage, so before a dispatch the host makes ready everything it can see
//! the dispatch name: the whole copy around each root view, around each
//! the dispatch name: the window around each root view, around each
//! pair of root constants that is a title address and around each root
//! constant that is one in 256-byte units (the builder passes structures
//! all three ways), and the 4 MiB after each (a structure may cross a
//! all three ways), and the one after each (a structure may cross a
//! window's end).
//! shortcut: what a stage reaches only through pointers it reads out of
//! memory (InitHeaders' table of structure addresses, LoadInstances'
//! instance records) is not made ready unless something else named it;
//! the fault count in the log says whether that happens. The whole answer
//! is the extent of the title's allocation around an address, which the
//! memory module of the layer knows and this one does not ask yet.
//! memory (the structures a top-level build's instances name) is there from
//! the second frame on; the fault count in the log says how often that
//! happens. The top level is built anew every frame.
//!
//! **What comes back to title memory**: what the builder stores in title
//! buffers of at most [`BACK`] bytes it names (the sizes the title reads to
//! make a structure smaller). Structures and scratch memory stay on the card.
//! Other programs that store by address declare the copies they name.
//!
//! shortcut: a copy that is numbered is kept for the rest of the run, and
//! there are at most [`COPIES`] of them (4 MiB windows mostly: 4 GiB at
//! most). Every list with such a shader brings all numbered copies up to
//! date. A dispatch that can store through raw addresses declares the
//! copies it names as written (the bytes are compared afterwards, as for
//! any unordered access view); what a stage stores through pointers it
//! reads out of memory is declared if a stage of the same list names the
//! copy, and otherwise stays in the host's copy until one does. A
//! dirty-page mask written by the shader would bound both.
//! there are at most [`COPIES`] of them.
//! shortcut: the windows a list does not name are brought up to date once a
//! frame, by the first list that reads by address: bytes the title writes
//! there later in the frame are seen a frame late.
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
@@ -58,29 +64,54 @@ use crate::host::{HostList, host};
use crate::shadow::Shadow;
/// Copies that can be numbered (the run of heap slots is one longer). A
/// listed frame of the opening cutscene names about 50 windows.
const COPIES: u32 = 1023;
/// A window of memory outside buffers (`shadow.rs`).
/// frame of the dorm names 1,100 pages outside the named windows.
const COPIES: u32 = (1 << 14) - 1;
/// A window around memory a dispatch names.
const CELL: u64 = 4 << 20;
/// A directory page.
const PAGE: u64 = 1 << 16;
/// The biggest title buffer whose builder results come back.
const BACK: u64 = 256 << 10;
/// What a draw or dispatch does with title memory by address.
#[derive(Clone, Copy, PartialEq)]
pub(crate) enum Use {
Reads,
Writes,
/// A stage of the structure builder; `back`: its results come back.
Builder { back: bool },
}
struct Memory {
/// Buffer, its mapping, its GPU address.
directory: (ID3D12Resource, *mut u32, u64),
/// The CPU's entries (number, offset), and the copy shaders read.
directory: (*mut u32, Arc<Shadow>),
faults: (ID3D12Resource, *mut u32, u64),
/// Copy `n` is `copies[n - 1]`.
copies: Vec<Arc<Shadow>>,
/// Copy `n` is `copies[n - 1]`; whether it is a window of ours.
copies: Vec<(Arc<Shadow>, bool)>,
/// Places of title buffers' copies among `copies`.
numbers: HashMap<usize, usize>,
/// Per host view heap (by address): the first slot of its run, and how
/// many copies have their view there.
heaps: HashMap<usize, (u32, u32)>,
/// The frame the faults were last read in.
frame: u64,
/// The frame our windows were last brought up to date in.
swept: u64,
/// Pages asked for this frame that are not the title's memory.
refused: std::collections::HashSet<u64>,
stats: Stats,
}
// SAFETY: the mappings are of buffers the struct owns; all access is under
/// Since the last report (diagnostics).
#[derive(Default)]
struct Stats {
from: Option<(std::time::Instant, u64)>,
uses: [u64; 3],
faults: u64,
back: u64,
}
// SAFETY: the mappings are of memory the struct owns; all access is under
// the mutex.
unsafe impl Send for Memory {}
@@ -94,54 +125,70 @@ pub(crate) struct Ready {
pub(crate) base: u32,
}
/// A window around a page a shader came to by a pointer.
fn far_window() -> u64 {
static BYTES: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
*BYTES.get_or_init(|| {
let kb = std::env::var("VICTORY_MEMORY_WINDOW_KB").ok().and_then(|v| v.trim().parse::<u64>().ok());
kb.map(|kb| kb << 10).filter(|b| b.is_power_of_two() && (PAGE..=CELL).contains(b)).unwrap_or(256 << 10)
})
}
impl Memory {
fn new() -> Option<Memory> {
let host = host()?;
let make = |dwords: u64, what: &str| {
let (buffer, data) = host.shared_buffer(dwords * 4)?;
crate::host::name(&buffer, || format!("title memory by address: {what}"));
// SAFETY: a fresh mapping of at least that many bytes.
unsafe { std::ptr::write_bytes(data, 0, (dwords * 4) as usize) };
// SAFETY: a live buffer.
let gpu = unsafe { buffer.GetGPUVirtualAddress() };
Some((buffer, data.cast::<u32>(), gpu))
};
let bytes = u64::from(MEMORY_PAGES) * 8;
// Zeroed, and watched for writes as the title's memory is.
let entries = crate::upload::reserve(bytes as usize)?;
let directory = crate::shadow::by_address(entries as u64, bytes)?;
crate::host::name(&directory.buffer, || "title memory by address: directory".into());
let (faults, data) = host.shared_buffer(u64::from(MEMORY_FAULTS) * 4)?;
crate::host::name(&faults, || "title memory by address: faults".into());
// SAFETY: a fresh mapping of at least that many bytes.
unsafe { std::ptr::write_bytes(data, 0, MEMORY_FAULTS as usize * 4) };
// SAFETY: a live buffer.
let gpu = unsafe { faults.GetGPUVirtualAddress() };
tracing::info!("title memory by address: a shader reads or writes through raw addresses");
Some(Memory {
directory: make(u64::from(MEMORY_PAGES) * 2, "directory")?,
faults: make(u64::from(MEMORY_FAULTS), "faults")?,
directory: (entries as *mut u32, directory),
faults: (faults, data.cast::<u32>(), gpu),
copies: Vec::new(),
numbers: HashMap::new(),
heaps: HashMap::new(),
frame: u64::MAX,
swept: u64::MAX,
refused: Default::default(),
stats: Stats::default(),
})
}
/// Gives the page of `address` its copy, if it has none and the memory
/// is the title's; the place of its copy in `copies`.
fn map(&mut self, address: u64) -> Option<usize> {
self.make(address);
let page = address / PAGE;
if page >= u64::from(MEMORY_PAGES) - 1 {
return None;
}
// SAFETY: inside the directory's mapping.
let number = unsafe { self.directory.1.add(page as usize * 2).read() };
(number as usize).checked_sub(1)
/// The directory's entry of `page`.
fn entry(&self, page: u64) -> *mut u32 {
debug_assert!(page < u64::from(MEMORY_PAGES));
// SAFETY: inside the directory.
unsafe { self.directory.0.add(page as usize * 2) }
}
fn make(&mut self, address: u64) {
/// Gives the page of `address` its copy, if it has none and the memory
/// is the title's (a window of `window` bytes outside buffers); the place
/// of its copy in `copies`.
fn map(&mut self, address: u64, window: u64) -> Option<usize> {
let page = address / PAGE;
// The last entry stays empty: addresses past the directory read it.
if page >= u64::from(MEMORY_PAGES) - 1 {
return;
return None;
}
// SAFETY: inside the directory's mapping.
let entry = unsafe { self.directory.1.add(page as usize * 2) };
// SAFETY: as above.
if unsafe { entry.read() } != 0 {
return;
// SAFETY: an entry of the directory.
let mut number = unsafe { self.entry(page).read() };
if number == 0 {
self.make(page, window);
// SAFETY: as above.
number = unsafe { self.entry(page).read() };
}
(number as usize).checked_sub(1)
}
fn make(&mut self, page: u64, window: u64) {
let start = page * PAGE;
// A shader's pointer, or a constant taken for an address, may be
// anything: only memory the title has.
@@ -152,60 +199,57 @@ impl Memory {
self.refused.insert(page);
return;
}
// The copy a root view at that address would get; a buffer that
// ends inside the page is passed over for what holds all of it.
let whole = |s: &Arc<Shadow>| s.offset_in(start).is_some_and(|_| s.left(start) >= PAGE);
let Some(shadow) = crate::shadow::find(start, 1)
.filter(whole)
.or_else(|| crate::shadow::find(start, PAGE).filter(whole))
else {
return;
// The copy of a title buffer that holds all of the page, the
// innermost first; else a window of our own.
let whole = |s: &Arc<Shadow>| s.offset_in(start).is_some() && s.left(start) >= PAGE;
let buffer = crate::shadow::in_buffer(start, PAGE).and_then(|_| crate::shadow::find(start, PAGE)).filter(whole);
let own = buffer.is_none();
let shadow = match buffer {
Some(s) => s,
None => {
if self.copies.len() >= COPIES as usize {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
tracing::warn!(COPIES, "title memory by address: no number left for another copy; its pages read zeros")
});
return;
}
let Some(s) = crate::shadow::by_address(start & !(window - 1), window) else {
return;
};
s
}
};
// SAFETY: a live buffer.
let flags = unsafe { shadow.buffer.GetDesc() }.Flags;
if flags.0 & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.0 == 0 {
return;
}
let n = match self.copies.iter().position(|c| Arc::ptr_eq(c, &shadow)) {
Some(n) => n,
let key = Arc::as_ptr(&shadow) as usize;
let n = match self.numbers.get(&key) {
Some(&n) => n,
None if self.copies.len() < COPIES as usize => {
self.copies.push(shadow.clone());
self.copies.push((shadow.clone(), own));
let n = self.copies.len() - 1;
self.numbers.insert(key, n);
// Every page the copy holds whole, not only the one asked
// for: a structure is more than 64 KiB.
// shortcut: a page of a title buffer inside this one that
// has a copy of its own is given this copy too.
// for: a structure is more than 64 KiB. (Not those of title
// buffers inside a window: they have copies of their own.)
let (first, len) = shadow.span();
let mut at = first.next_multiple_of(PAGE);
while at + PAGE <= first + len && at / PAGE < u64::from(MEMORY_PAGES) - 1 {
// SAFETY: inside the directory's mapping.
let other = unsafe { self.directory.1.add((at / PAGE) as usize * 2) };
// SAFETY: as above; the offset first, as below.
unsafe {
if other.read() == 0 {
other.add(1).write_volatile((at - first) as u32);
other.write_volatile(n as u32 + 1);
}
let entry = self.entry(at / PAGE);
// SAFETY: an entry of the directory.
if unsafe { entry.read() } == 0 && (!own || crate::shadow::in_buffer(at, PAGE).is_none()) {
fill(entry, n, at - first);
}
at += PAGE;
}
n
}
None => {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
tracing::warn!(COPIES, "title memory by address: no number left for another copy; its pages read zeros")
});
return;
}
None => return,
};
let offset = shadow.offset_in(start).unwrap_or(0);
// The offset first: a shader that sees the number sees it too.
// SAFETY: inside the directory's mapping.
unsafe {
entry.add(1).write_volatile(offset as u32);
entry.write_volatile(n as u32 + 1);
}
fill(self.entry(page), n, shadow.offset_in(start).unwrap_or(0));
}
/// The pages shaders found empty since the last frame.
@@ -223,48 +267,90 @@ impl Memory {
unsafe { at.write_volatile(0) };
}
}
if !pages.is_empty() {
// Each is an access that read 0 or was dropped.
static SAID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
if SAID.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 50 {
tracing::warn!(
pages = pages.len(),
first = format_args!("{:#x}", u64::from(pages[0]) * PAGE),
"title memory by address: shaders touched pages that had no copy (those accesses read 0 or were dropped); the copies are made now"
);
}
if pages.is_empty() {
return;
}
for page in pages {
self.map(u64::from(page) * PAGE);
let mut made = 0;
for &page in &pages {
made += usize::from(self.map(u64::from(page) * PAGE, far_window()).is_some());
}
self.stats.faults += pages.len() as u64;
// Each is an access that read 0 or was dropped.
static SAID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
if SAID.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 100 {
pages.sort_unstable();
let first: Vec<String> = pages.iter().take(8).map(|&p| format!("{:#x}", u64::from(p) * PAGE)).collect();
tracing::warn!(
frame = self.frame,
pages = pages.len(),
made,
copies = self.copies.len(),
?first,
"title memory by address: shaders touched pages that had no copy (those accesses read 0 or were dropped); the copies are made now, of those that are the title's"
);
}
}
/// What a dispatch names: the copy around `address` and the 4 MiB
/// after it; their places in `copies`.
/// What a dispatch names: the copy around `address` and the one after it;
/// their places in `copies`.
fn name(&mut self, address: u64, named: &mut Vec<usize>) {
for at in [address, (address & !(CELL - 1)) + CELL] {
if let Some(n) = self.map(at)
if let Some(n) = self.map(at, CELL)
&& !named.contains(&n)
{
named.push(n);
}
}
}
/// Every ten seconds or so, what was used (with the frame's line).
fn report(&mut self) {
let now = std::time::Instant::now();
let (since, frame) = *self.stats.from.get_or_insert((now, self.frame));
let seconds = now.duration_since(since).as_secs_f64();
if seconds < 10.0 {
return;
}
let frames = self.frame.wrapping_sub(frame).max(1) as f64;
let (mut ours, mut bytes) = (0, 0);
for (s, own) in &self.copies {
if *own {
ours += 1;
bytes += s.span().1;
}
}
let s = std::mem::take(&mut self.stats);
tracing::info!(
"title memory by address: {ours} windows ({} MB) and {} title buffers; per frame {:.0} dispatches that read, {:.0} that write, \
{:.0} of the structure builder, {:.1} pages that had no copy, {:.1} results brought back",
bytes >> 20,
self.copies.len() - ours,
s.uses[0] as f64 / frames,
s.uses[1] as f64 / frames,
s.uses[2] as f64 / frames,
s.faults as f64 / frames,
s.back as f64 / frames
);
self.stats.from = Some((now, self.frame));
}
}
/// The offset first: a shader that sees the number sees it too.
fn fill(entry: *mut u32, n: usize, offset: u64) {
// SAFETY: an entry of the directory, under the mutex.
unsafe {
entry.add(1).write_volatile(offset as u32);
entry.write_volatile(n as u32 + 1);
}
}
/// Before a draw whose shader reads title memory by address: what its
/// root arguments name gets its copies (`addresses`: its root views;
/// `constants`: its root constants, see the module), every numbered copy
/// has its view in `heap`'s run and is brought up to date when `list` is
/// submitted (and, with `writes`, the named ones copied back after). None if the host
/// cannot hold it; the draw is then dropped.
pub(crate) fn ready(
list: &HostList,
heap: &HostHeap,
addresses: &[u64],
constants: &[u32],
writes: bool,
) -> Option<Ready> {
/// submitted (what `use_` writes is copied back after, see the module).
/// None if the host cannot hold it; the draw is then dropped.
pub(crate) fn ready(list: &HostList, heap: &HostHeap, addresses: &[u64], constants: &[u32], use_: Use) -> Option<Ready> {
let mut guard = MEMORY.lock().unwrap_or_else(|e| e.into_inner());
if guard.is_none() {
*guard = Memory::new();
@@ -275,7 +361,13 @@ pub(crate) fn ready(
m.frame = frame;
m.refused.clear();
m.take_faults();
m.report();
}
m.stats.uses[match use_ {
Use::Reads => 0,
Use::Writes => 1,
Use::Builder { .. } => 2,
}] += 1;
let numbered = m.copies.len();
let mut named = Vec::new();
for &address in addresses {
@@ -305,7 +397,7 @@ pub(crate) fn ready(
(base, 0)
}
};
for (n, shadow) in m.copies.iter().enumerate().skip(made as usize) {
for (n, (shadow, _)) in m.copies.iter().enumerate().skip(made as usize) {
let (_, len) = shadow.span();
heap.raw_uav_at(base + 1 + n as u32, &shadow.buffer, 0, len);
}
@@ -315,25 +407,68 @@ pub(crate) fn ready(
// shortcut: a copy another list numbers meanwhile is brought up to date
// by that list's submission, which may run after this one's.
const READ: usize = usize::MAX - 0x6d;
const WRITE: usize = usize::MAX - 0x1_0000;
const NAMED: usize = usize::MAX - 0x1_0000;
const WRITE: usize = usize::MAX - 0x2_0000;
const RESULT: usize = usize::MAX / 2;
let first_read = list.first_use(READ);
for (n, shadow) in m.copies.iter().enumerate() {
if first_read {
let (start, len) = m.directory.1.span();
m.directory.1.refresh(list, start, len);
let sweep = m.swept != frame;
m.swept = frame;
for (shadow, own) in &m.copies {
if sweep || !own {
let (start, len) = shadow.span();
shadow.refresh(list, start, len);
}
}
list.keep(&m.faults.0);
} else if m.copies.len() > numbered {
let (start, len) = m.directory.1.span();
m.directory.1.refresh(list, start, len);
}
for (shadow, _) in &m.copies[numbered..] {
let (start, len) = shadow.span();
if first_read || n >= numbered {
shadow.refresh(list, start, len);
}
for &n in &named {
let (shadow, own) = &m.copies[n];
let (start, len) = shadow.span();
if *own && list.first_use(NAMED - n) {
shadow.refresh(list, start, len);
}
if writes && named.contains(&n) && list.first_use(WRITE - n) {
if use_ == Use::Writes && list.first_use(WRITE - n) {
#[cfg(feature = "diagnostics")]
crate::host::wide_write(start, len, 1);
shadow.gpu_writes(list, start, len);
}
}
if first_read {
list.keep(&m.directory.0);
list.keep(&m.faults.0);
if use_ == (Use::Builder { back: true }) {
for &address in addresses {
let Some((start, len)) = crate::shadow::in_buffer(address, 1).filter(|b| b.1 <= BACK) else {
continue;
};
if !list.first_use(RESULT + start as usize) {
continue;
}
// The copy or copies its pages are in (it need not fill one).
let mut at = start;
while at < start + len {
let end = (at / PAGE + 1) * PAGE;
let n = end.min(start + len) - at;
if let Some(k) = m.map(at, CELL) {
let shadow = &m.copies[k].0;
shadow.refresh(list, at, n);
shadow.gpu_writes(list, at, n);
m.stats.back += 1;
}
at = end;
}
}
}
let directory = &m.directory.1;
Some(Ready {
directory: m.directory.2,
directory: directory.gpu(directory.span().0),
faults: m.faults.2,
base,
})