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 ad30f2cb1f
commit b558934d7b
13 files changed
+727 -177

No files matched your search

+70 -1
View File
@@ -83,6 +83,9 @@ pub(crate) struct Shadow {
card: Option<Card>,
/// A window of title memory outside buffers, not a title buffer's copy.
window: bool,
/// Of a copy with two sides: only the dwords the title changed go up,
/// as with [`send_changed`] ([`by_address`]).
changes_only: bool,
/// With `crate::inplace`: not a copy at all. `buffer` is placed in a
/// heap opened on the title memory itself, so shaders read and write
/// the title's own bytes and nothing is copied either way. The heap,
@@ -2631,6 +2634,7 @@ impl Shadow {
},
card: None,
window: false,
changes_only: false,
place: None,
}
}
@@ -3515,7 +3519,7 @@ impl Shadow {
/// A page of title memory, as read.
static TITLE: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) };
}
let Some(card) = self.card.as_ref().filter(|_| send_changed()) else {
let Some(card) = self.card.as_ref().filter(|_| self.changes_only || send_changed()) else {
return self.copy(from, to);
};
let whole = !known && card.before.lock().unwrap_or_else(|e| e.into_inner()).is_none();
@@ -4554,6 +4558,71 @@ fn longest_tail(windows: impl Iterator<Item = (u64, u64)>, address: u64, end: u6
.min()
}
/// Whether `[address, address + len)` lies in one title buffer.
pub(crate) fn in_buffer(address: u64, len: u64) -> Option<(u64, u64)> {
let end = address.checked_add(len)?;
let map = buffers_read();
let found = map.range(..=(address, usize::MAX)).rev().take(64).find(|(_, e)| e.end >= end);
found.map(|(k, e)| (k.0, e.end - k.0))
}
/// A copy of the title memory `[start, start + len)` outside buffers for the
/// programs that find memory by address ([`crate::memory`]), on the card: the
/// structures rays are traced through, read with every step of every ray.
///
/// What shaders write there by address is not brought back to title memory
/// (nothing else reads a structure; the first whole run of the builder wrote
/// garbage over the title's objects), so the copy is the only place that has
/// it. The CPU's side holds the title's bytes as they were last taken, and
/// only dwords the title changed since go up: a page the title writes a part
/// of keeps what shaders wrote in the rest.
pub(crate) fn by_address(start: u64, len: u64) -> Option<Arc<Shadow>> {
let host = host()?;
if !cards() || !host.card_ready() {
return None;
}
// At most so much on the card's own memory (2 GiB unless
// `VICTORY_MEMORY_CARD_MB` says otherwise), and while it has room; past
// that the side shaders see is in the PC's memory.
static MOST: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
static USED: AtomicU64 = AtomicU64::new(0);
let most = *MOST.get_or_init(|| {
let mb = std::env::var("VICTORY_MEMORY_CARD_MB").ok().and_then(|v| v.trim().parse::<u64>().ok());
mb.unwrap_or(2048) << 20
});
let room = host.video_memory().is_none_or(|[budget, used, ..]| used + len < budget / 100 * 97);
let (staging, data) = host.shared_buffer(len)?;
let on_card = if room && USED.fetch_add(len, Ordering::Relaxed) + len <= most {
host.uav_buffer(len)
} else {
None
};
let buffer = match on_card {
Some(buffer) => buffer,
None => host.shared_buffer(len)?.0,
};
crate::host::name(&staging, || format!("title memory {start:#x}+{len:#x} by address, the CPU's side"));
crate::host::name(&buffer, || format!("title memory {start:#x}+{len:#x} by address"));
// (Counted down with the windows' on the card when it goes.)
WINDOW_CARD_BYTES.fetch_add(len, Ordering::Relaxed);
// SAFETY: a live buffer.
let gpu = unsafe { buffer.GetGPUVirtualAddress() };
let mut shadow = Shadow::new(buffer, data, gpu, start, len);
shadow.window = true;
shadow.changes_only = true;
shadow.card = Some(Card {
staging,
before: Mutex::default(),
dirty: Mutex::default(),
sent: Mutex::default(),
touched: Default::default(),
size: len,
});
let shadow = Arc::new(shadow);
CARDED.lock().unwrap_or_else(|e| e.into_inner()).push(Arc::downgrade(&shadow));
Some(shadow)
}
/// Copies of title memory outside buffers, by (start, length).
static LOOSE: std::sync::RwLock<BTreeMap<(u64, u64), Arc<Shadow>>> = std::sync::RwLock::new(BTreeMap::new());