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

+4 -2
View File
@@ -251,7 +251,6 @@ impl<'a> Lower<'a> {
/// computed, `offset` fixed; none of the node's bytes past 127), read
/// four at a time.
fn bvh_node_words(&mut self, at: NodeAt, base: Option<V>, offset: u32, count: u32) -> Vec<V> {
let c = self.cur;
let (undef, align) = (self.s.m.undef(self.s.t.i32), self.s.m.i32(4));
// The second half of a 128-byte node may lie in the next 64 KiB
// page, which may be in another copy: its own look, made once.
@@ -267,10 +266,11 @@ impl<'a> Lower<'a> {
let (low, high) = self.address_plus(low, high, 64);
// Every lane in this block runs the instruction.
let (handle, byte) = self.bvh_memory(low, high, None);
let k64 = self.s.m.i32(64);
let (k64, c) = (self.s.m.i32(64), self.cur);
(handle, self.s.m.bin(c, Bin::Sub, byte, k64))
}),
};
let c = self.cur;
if let Some(b) = base {
byte = self.s.m.bin(c, Bin::Add, byte, b);
}
@@ -330,6 +330,8 @@ impl<'a> Lower<'a> {
let k16 = self.s.m.i32(16);
let words = self.bvh_node_words(at, None, 16, 24);
let floats: Vec<V> = words.iter().map(|&w| self.as_float(w)).collect();
// (The look for the second half goes on in another block.)
let wide_block = self.cur;
self.s.m.terminate(wide_block, Term::Br(join));
// 16-bit floats: 24 halves in 12 dwords from byte 16, the low half
// of a dword first.
+103 -18
View File
@@ -259,6 +259,9 @@ struct Lower<'a> {
/// they are globally coherent; a load in a loop that only waits would
/// be made once otherwise.
coherent: HashSet<(usize, u32)>,
/// In the instruction being lowered: the host copy its root view's bytes
/// are in ([`Lower::root_memory`]).
root_copy: Option<V>,
mask_phis: HashMap<Value, V>,
pending_masks: Vec<(V, usize, usize)>,
data_phis: Vec<(V, usize, usize)>,
@@ -1572,6 +1575,7 @@ impl<'a> Lower<'a> {
read_phis: HashSet::new(),
stale: HashSet::new(),
coherent: HashSet::new(),
root_copy: None,
ordered: if x.native.stage == HwStage::Ps {
ordered_section(p)
} else {
@@ -1735,6 +1739,7 @@ impl<'a> Lower<'a> {
let Fields::Flat { dst, .. } = &inst.fields else {
unreachable!()
};
self.root_copy = None;
let name = inst.name();
if self.scalar.get(&(b, i)).is_some_and(|a| a.param == MEMORY) {
return self.memory_flat(inst, b, i);
@@ -1778,8 +1783,15 @@ impl<'a> Lower<'a> {
if let Some(bias) = self.root_bias(a.param) {
off = self.s.m.bin(c, Bin::Add, off, bias);
}
Some(self.handle((b, i), &src, false, (11, 0))?)
match self.root_memory(&src, off, guard) {
Some((h, byte)) => {
off = byte;
Some(h)
}
None => Some(self.handle((b, i), &src, false, (11, 0))?),
}
};
let c = self.cur;
let dword = |l: &mut Self, byte: V| -> Result<V, Why> {
match h {
None => l.code_dword(byte),
@@ -2999,7 +3011,8 @@ impl<'a> Lower<'a> {
/// `SLOT_MEMORY`): the handle of the raw view of the host copy holding
/// it and the byte in that view. A page without a copy reads zeros
/// (and drops writes) through the null view at the first slot, and its
/// number is left for the host, for lanes that run (`guard`).
/// number is left for the host, for lanes that run (`guard`). Goes on in
/// another block.
fn memory(&mut self, lo: V, hi: V, guard: Option<V>) -> (V, V) {
use super::shader::{MEMORY_FAULTS, MEMORY_PAGES, SLOT_FAULTS, SLOT_MEMORY, USER_MEMORY_BASE};
let c = self.cur;
@@ -3031,21 +3044,23 @@ impl<'a> Lower<'a> {
);
let (copy, start) = (self.s.m.extract(c, entry, 0), self.s.m.extract(c, entry, 1));
let (copy, start) = (self.as_int(copy), self.as_int(start));
// A page without a copy: its number, at a place by the number, or
// at the sink for lanes that do not run and pages that have one.
// A page without a copy: its number, at a place by the number. In a
// branch: the notes lie in the PC's memory, and a store there with
// every node of every ray would be most of what a trace does.
let zero = k(self, 0);
let mut missing = self.s.m.cmp(c, pred::EQ, copy, zero);
if let Some(g) = guard {
missing = self.s.m.bin(c, Bin::And, missing, g);
}
let places = k(self, MEMORY_FAULTS - 1);
let faults = self.s.root_uav_handle(SLOT_FAULTS);
let noting = self.begin_guarded(Some(missing));
let c = self.cur;
let place = self.s.m.bin(c, Bin::URem, page, places);
let one = k(self, 1);
let place = self.s.m.bin(c, Bin::Add, place, one);
let k2 = k(self, 2);
let place = self.s.m.bin(c, Bin::Shl, place, k2);
let place = self.s.m.select(c, missing, place, zero);
let faults = self.s.root_uav_handle(SLOT_FAULTS);
let void = self.s.t.void;
let mask = self.s.m.i8(1);
self.s.op(
@@ -3056,15 +3071,63 @@ impl<'a> Lower<'a> {
140,
&[faults, place, iu, page, iu, iu, iu, mask, align],
);
self.end_guarded(noting, &[]);
let c = self.cur;
let base = self.host_word(RootType::Other(USER_MEMORY_BASE), 0, 0);
let slot = self.s.m.bin(c, Bin::Add, base, copy);
let h = self.s.heap_handle(c, slot, false, true, (11 | 0x1000, 0));
// The builder's threads read what others of the dispatch wrote (a
// refit climbs from children to parents): globally coherent.
let coherent = if crate::spill::is_spilled(self.x.native) { 0x4000 } else { 0 };
let h = self.s.heap_handle(c, slot, false, true, (11 | 0x1000 | coherent, 0));
let k64k = k(self, 0xFFFF);
let within = self.s.m.bin(c, Bin::And, lo, k64k);
let byte = self.s.m.bin(c, Bin::Add, start, within);
(h, byte)
}
/// The title address in the root view `src` is built around, in a program
/// with a spilled user-data table (`shader.rs` `USER_ROOT_ADDRESS`).
fn root_address(&mut self, src: &Source) -> Option<(V, V)> {
use super::shader::USER_ROOT_ADDRESS;
let Source::Built { address, .. } = src else {
return None;
};
let rp = *self.x.native.layout.get(address.param)?;
if rp.root != RootType::Descriptor || !crate::spill::is_spilled(self.x.native) {
return None;
}
let used = self.x.native.used.iter().find(|u| u.user_data == rp.user_data)?;
if !matches!(used.kind, ResourceType::Srv | ResourceType::Uav) {
return None;
}
let lo = self.host_word(RootType::Other(USER_ROOT_ADDRESS), rp.slot, 0);
let hi = self.host_word(RootType::Other(USER_ROOT_ADDRESS + 1), rp.slot, 0);
Some((lo, hi))
}
/// For an access through such a root view: the raw view of the host copy
/// and the byte in it of the view's byte `off` (signed: an offset below
/// the address stays below it). [`Lower::handle`] gives the same view for
/// the rest of the instruction. None for any other source. Goes on in
/// another block.
/// shortcut: an access of several dwords is taken not to cross the end
/// of a host copy (the builder's records lie on their size).
fn root_memory(&mut self, src: &Source, off: V, guard: Option<V>) -> Option<(V, V)> {
let (lo, hi) = self.root_address(src)?;
let c = self.cur;
let i32t = self.s.t.i32;
let sum = self.s.m.bin(c, Bin::Add, lo, off);
let wrapped = self.s.m.cmp(c, pred::IULT, sum, lo);
let carry = self.s.m.cast(c, Cast::ZExt, wrapped, i32t);
let k31 = self.s.m.i32(31);
let sign = self.s.m.bin(c, Bin::AShr, off, k31);
let hi = self.s.m.bin(c, Bin::Add, hi, carry);
let hi = self.s.m.bin(c, Bin::Add, hi, sign);
let (h, byte) = self.memory(sum, hi, guard);
self.root_copy = Some(h);
Some((h, byte))
}
/// `global_load_*`, `global_store_*` and `global_atomic_*` on title
/// memory by address (`resource::MEMORY`): the address is the VGPR
/// pair, each lane its own.
@@ -3094,7 +3157,6 @@ impl<'a> Lower<'a> {
let (lo, hi) = (word(self, 0)?, word(self, 1)?);
let (lo, hi) = self.address_plus(lo, hi, i32::from(*offset));
let guard = self.guard(b, i)?;
let c = self.cur;
let (i32t, res, void) = (self.s.t.i32, self.s.t.res_ret, self.s.t.void);
let iu = self.s.m.undef(i32t);
let align = self.s.m.i32(4);
@@ -3112,6 +3174,7 @@ impl<'a> Lower<'a> {
return Err("a global load result");
};
let (h, byte) = self.memory(lo, hi, guard);
let c = self.cur;
let mask = self.s.m.i8((1u8 << n) - 1);
let r = self.s.op(
c,
@@ -3160,6 +3223,7 @@ impl<'a> Lower<'a> {
None => self.s.m.i32(0),
};
let (h, byte) = self.memory(lo, hi, guard);
let c = self.cur;
// DXIL's atomic operations: add 0, and 1, or 2, xor 3, signed
// minimum 4 and maximum 5, unsigned 6 and 7, exchange 8.
let simple = match op {
@@ -3294,6 +3358,7 @@ impl<'a> Lower<'a> {
let n = (count - first).min(4);
let (lo, hi) = self.address_plus(lo, hi, offset + 4 * i32::from(first));
let (h, byte) = self.memory(lo, hi, None);
let c = self.cur;
let mask = self.s.m.i8((1u8 << n) - 1);
let r = self.s.op(
c,
@@ -5830,6 +5895,7 @@ impl<'a> Lower<'a> {
else {
unreachable!()
};
self.root_copy = None;
let name = inst.name();
if let Some(a) = self.scalar.get(&site).copied().filter(|a| a.param == CODE) {
return self.code_smem(inst, a);
@@ -5938,7 +6004,14 @@ impl<'a> Lower<'a> {
return Ok(());
}
if matches!(kind, Some(u) if u.kind == ResourceType::Srv) {
let h = self.handle(site, src, false, (11, 0))?;
let h = match self.root_memory(src, addr, None) {
Some((h, byte)) => {
addr = byte;
h
}
None => self.handle(site, src, false, (11, 0))?,
};
let c = self.cur;
for k in 0..count {
let off = self.s.m.i32(4 * k);
let a = self.s.m.bin(c, Bin::Add, addr, off);
@@ -6645,13 +6718,14 @@ impl<'a> Lower<'a> {
/// For a root view the host binds as a raw view that may begin before
/// the title's address (`shader.rs` `USER_VIEW_BIAS`): the bytes to add
/// to offsets through it. Every root UAV, and the root SRVs of programs
/// with a spilled user-data table (the others keep the views they
/// to offsets through it. Every root UAV (root SRVs keep the views they
/// have).
fn root_bias(&mut self, param: usize) -> Option<V> {
use super::shader::USER_VIEW_BIAS;
let rp = *self.x.native.layout.get(param)?;
if rp.root != RootType::Descriptor {
// (Title memory by address where the user-data table is spilled:
// [`Lower::root_memory`].)
if rp.root != RootType::Descriptor || crate::spill::is_spilled(self.x.native) {
return None;
}
let kind = self
@@ -6661,12 +6735,7 @@ impl<'a> Lower<'a> {
.iter()
.find(|u| u.user_data == rp.user_data)
.map(|u| u.kind);
let biased = match kind {
Some(ResourceType::Uav) => true,
Some(ResourceType::Srv) => crate::spill::is_spilled(self.x.native),
_ => false,
};
biased.then(|| self.host_word(RootType::Other(USER_VIEW_BIAS), rp.slot, 0))
(kind == Some(ResourceType::Uav)).then(|| self.host_word(RootType::Other(USER_VIEW_BIAS), rp.slot, 0))
}
/// The used-resource entry a descriptor loaded from a table falls in.
@@ -6738,6 +6807,7 @@ impl<'a> Lower<'a> {
Ok(self.s.sampler_handle(self.cur, n))
}
Source::Constant(_) => Err("a constant resource descriptor"),
Source::Built { .. } if self.root_copy.is_some() => self.root_copy.ok_or("no copy"),
Source::Built { address, .. } => {
let rp = *self
.x
@@ -11001,6 +11071,7 @@ impl<'a> Lower<'a> {
}
fn buffer(&mut self, inst: &Inst, b: usize, i: usize) -> Result<(), Why> {
self.root_copy = None;
let Fields::Buffer {
data,
addr,
@@ -11163,6 +11234,20 @@ impl<'a> Lower<'a> {
}
return Ok(());
}
if self.root_address(src).is_some() {
// Title memory by address: the byte in the host copy from here on.
if let Some(element) = index {
if used.is_none_or(|u| u.stride != 1) {
return Err("an indexed access through a root view by address");
}
off = self.s.m.bin(c, Bin::Add, element, off);
index = None;
}
if let Some((_, byte)) = self.root_memory(src, off, guard) {
off = byte;
}
}
let c = self.cur;
let uav = store || atomic.is_some() || used.is_some_and(|u| u.kind == ResourceType::Uav);
let mut u = if uav { 0x1000 } else { 0 };
if uav && !typed && self.entry(src).is_some_and(|e| self.coherent.contains(&e)) {
+1 -1
View File
@@ -118,7 +118,7 @@ pub use lower::{host_settings, set_host_max_wave};
pub use rect::rect_list_gs;
pub use shader::{
MEMORY_FAULTS, MEMORY_PAGES, ROOT_CBV_SPACE, SAMPLER_SPACE, SLOT_FAULTS, SLOT_MEMORY, SLOT_STRIDES,
SLOT_STRIDES_COUNT, USER_DATA_SPACE, USER_MEMORY_BASE, USER_VIEW_ADDRESS, USER_VIEW_BIAS,
SLOT_STRIDES_COUNT, USER_DATA_SPACE, USER_MEMORY_BASE, USER_ROOT_ADDRESS, USER_VIEW_ADDRESS, USER_VIEW_BIAS,
set_view_heap_limit, view_heap_limit,
};
+9 -2
View File
@@ -66,9 +66,16 @@ pub const USER_VIEW_ADDRESS: u32 = 0x101;
/// begins on 16 bytes of its host buffer; the console's root views begin
/// anywhere (the structure builder's counters sit at addresses ending in
/// 4 and c). The shader adds the dword to every byte offset through that
/// view. Asked for every root UAV, and for the root SRVs of programs with
/// a spilled user-data table.
/// view. Asked for every root UAV.
pub const USER_VIEW_BIAS: u32 = 0x103;
/// `RootType::Other` code of a user-data dword the host computes: the low
/// 32 bits of the title address in the root view of root parameter `slot`;
/// [`USER_ROOT_ADDRESS`] + 1: its bits from 32 up. Programs with a spilled
/// user-data table (the ray-tracing structure builder's) reach what their
/// root views name as title memory by address ([`SLOT_MEMORY`]): a root view
/// has no size, and the host's copy of what it names may end in the middle of
/// it (a structure of megabytes crosses the 4 MiB windows).
pub const USER_ROOT_ADDRESS: u32 = 0x104;
/// `ShaderFlags` bits (metadata) and their `SFI0` feature bits.
pub(crate) mod flags {