From 02348ffe702a9f889b212c8723fc35c4b70f744a Mon Sep 17 00:00:00 2001 From: hophaver <86059649+hophaver@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:22:41 +0200 Subject: [PATCH 1/2] GPU: drop draws whose fresh tables cannot be made; 2^18 dynamic slots When HostHeap::dynamic ran out, State::tables skipped the fresh table and the stage's user data fell back to the title's table offset. Constant buffer tables are always made fresh and never mirrored, so the shader read whatever host descriptor sat at those slots as a constant buffer; on AMD a texture or null descriptor read as a buffer gives a wild base address and the device is removed (#59: faults at 0x7000037xxxxx, 0x8000037xxxxx, 0x33c4000, a different pipeline every run, about 60 s in). bind now reports that a table could not be made, and the draw, mesh dispatch, dispatch or indirect execution is dropped. The dynamic range grows from 2^16 to 2^18 slots, which the world did not exhaust on an RX 9070 XT. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/descriptor/host.rs | 4 ++- crates/victory-gpu/src/draw.rs | 39 +++++++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 43dba24..7315840 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -62,7 +62,9 @@ const PORTABLE_VIEWS: u32 = 1_000_000; /// Slots past the title's in a view heap, for tables a draw needs made /// afresh (constant buffers copied per draw). A list holds the slots it /// took until it is reset (its submissions have run) or released. -const DYNAMIC: u32 = 1 << 16; +/// 2^16 ran out seconds into the world (every world run on an RX 9070 XT, +/// each a device removal shortly after; #59). +const DYNAMIC: u32 = 1 << 18; impl HostHeap { pub(crate) fn new(sampler: bool, count: u32) -> Option { diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index b6e043b..3cf7121 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -77,6 +77,8 @@ pub(crate) struct State { /// The dynamic slots the list holds: title heap, first, count; given /// back when the list is reset or released. dynamic: Vec<(usize, u32, u32)>, + /// A fresh table of the stage being bound could not be made. + table_failed: bool, } impl Drop for State { @@ -344,7 +346,9 @@ impl State { once!(mesh, "a draw or mesh dispatch with another kind of pipeline"); return false; } - self.bind(list, p, false); + if !self.bind(list, p, false) { + return false; + } let l = &list.list; // Vertex buffers the input layout reads. let mut views = [D3D12_VERTEX_BUFFER_VIEW::default(); 32]; @@ -403,7 +407,9 @@ impl State { /// What draws and dispatches share: heaps, our root signature, the /// pipeline, and per stage the user data, root views and the /// descriptors its tables hold. - fn bind(&mut self, list: &HostList, p: &HostPipeline, compute: bool) { + /// False if the draw or dispatch must not run: a table it reads could + /// not be made. + fn bind(&mut self, list: &HostList, p: &HostPipeline, compute: bool) -> bool { let l = &list.list; // Heaps first: a root signature with directly indexed heaps must be // set after them. @@ -446,7 +452,19 @@ impl State { } for stage in &p.stages { let t = stage.translated(); + self.table_failed = false; let made = self.tables(list, &t.used, compute); + if self.table_failed { + static DROPPED_TABLES: AtomicU64 = AtomicU64::new(0); + let n = DROPPED_TABLES.fetch_add(1, Ordering::Relaxed) + 1; + if n.is_power_of_two() { + tracing::warn!( + dropped = n, + "a draw or dispatch dropped: no dynamic slots for its tables" + ); + } + return false; + } if let Some((param, dwords)) = stage.user_data { let mut values = vec![0u32; dwords as usize]; for u in &t.host.user_data { @@ -489,6 +507,7 @@ impl State { } } } + true } /// Runs a dispatch on the host. @@ -513,7 +532,10 @@ impl State { once!("a dispatch with a graphics pipeline"); return; } - self.bind(list, p, true); + if !self.bind(list, p, true) { + DROPPED.fetch_add(1, Ordering::Relaxed); + return; + } #[cfg(feature = "diagnostics")] list.note_op(|| self.op_text(format!("dispatch {x}x{y}x{z}"), true)); // SAFETY: an open list with everything set. @@ -696,6 +718,12 @@ impl State { continue; } let Some(first) = host.dynamic(n) else { + // Without its fresh table the shader would read the title's + // slots, which for constant buffer tables hold no constant + // view: a texture or null descriptor read as a buffer gives + // a wild address on AMD and the device is removed (#59). + // The draw or dispatch is dropped instead. + self.table_failed = true; continue; }; self.fresh_tables.insert(key, first); @@ -988,10 +1016,7 @@ impl State { DRAW | INDEXED => self.topology != RECTANGLES && self.prepare(list, kind == INDEXED), MESH => self.prepare_for(list, false, true), DISPATCH => match crate::pipeline::host_pipeline(self.pipeline as This) { - Some(p) if p.compute => { - self.bind(list, p, true); - true - } + Some(p) if p.compute => self.bind(list, p, true), _ => false, }, _ => false, From cbc9ece91863599413411e3d045db7d5b76d5899 Mon Sep 17 00:00:00 2001 From: hophaver <86059649+hophaver@users.noreply.github.com> Date: Tue, 24 Nov 2026 05:55:13 +0200 Subject: [PATCH 2/2] GPU: the larger dynamic range only where the heap still fits A host that refuses the title's slots plus 2^18 dynamic ones (NVIDIA's 1,000,000-view cap) now gets 2^16 dynamic slots, as before, instead of losing mirrored title slots to the larger range: at the cap, 2^18 left 737,856 title slots against 934,464, and titles were seen to index slots up to 917,140. The cap itself is unchanged. Hosts that take the larger heap (RX 9070 XT) keep 2^18. --- crates/victory-gpu/src/descriptor/host.rs | 61 +++++++++++++++-------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 7315840..672901a 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -17,8 +17,11 @@ pub(crate) struct HostHeap { pub(crate) heap: ID3D12DescriptorHeap, cpu: usize, step: u32, - /// Slots mirroring the title's; view heaps have [`DYNAMIC`] more. + /// Slots mirroring the title's; view heaps have `dynamic` more. count: u32, + /// Dynamic slots past `count` (none in sampler heaps): [`DYNAMIC`] + /// where the host takes them, else [`MIN_DYNAMIC`]. + dynamic: u32, sampler: bool, /// The record (view index and generation) each slot was last made from. made: Mutex>, @@ -66,6 +69,10 @@ const PORTABLE_VIEWS: u32 = 1_000_000; /// each a device removal shortly after; #59). const DYNAMIC: u32 = 1 << 18; +/// The dynamic slots of a heap whose host would not take [`DYNAMIC`] more +/// (a capped heap, NVIDIA): the title's slots keep their room first. +const MIN_DYNAMIC: u32 = 1 << 16; + impl HostHeap { pub(crate) fn new(sampler: bool, count: u32) -> Option { let host = host()?; @@ -86,18 +93,31 @@ impl HostHeap { }) } }; - let extra = if sampler { 0 } else { DYNAMIC }; - let (heap, count) = match make(count + extra) { - Ok(heap) => (heap, count), - Err(e) if !sampler && count + extra > PORTABLE_VIEWS => { - // shortcut: descriptors past the host's limit are left out; - // a title that reaches them needs remapping. - tracing::warn!(error = %e, count, "host view heap capped at {PORTABLE_VIEWS}"); - (make(PORTABLE_VIEWS).ok()?, PORTABLE_VIEWS - extra) + // The larger dynamic range only where the host takes it on top of + // every title slot; else the smaller one, and the cap after that. + let (heap, count, extra) = if sampler { + match make(count) { + Ok(heap) => (heap, count, 0), + Err(e) => { + tracing::warn!(error = %e, count, sampler, "no host descriptor heap"); + return None; + } } - Err(e) => { - tracing::warn!(error = %e, count, sampler, "no host descriptor heap"); - return None; + } else if let Ok(heap) = make(count + DYNAMIC) { + (heap, count, DYNAMIC) + } else { + match make(count + MIN_DYNAMIC) { + Ok(heap) => (heap, count, MIN_DYNAMIC), + Err(e) if count + MIN_DYNAMIC > PORTABLE_VIEWS => { + // shortcut: descriptors past the host's limit are left + // out; a title that reaches them needs remapping. + tracing::warn!(error = %e, count, "host view heap capped at {PORTABLE_VIEWS}"); + (make(PORTABLE_VIEWS).ok()?, PORTABLE_VIEWS - MIN_DYNAMIC, MIN_DYNAMIC) + } + Err(e) => { + tracing::warn!(error = %e, count, sampler, "no host descriptor heap"); + return None; + } } }; // SAFETY: a live heap. @@ -121,10 +141,11 @@ impl HostHeap { cpu, step, count, + dynamic: extra, sampler, made: Mutex::new(vec![0; count as usize]), kept: Mutex::new(vec![None; (count + extra) as usize]), - next: Mutex::new((0, vec![false; DYNAMIC as usize])), + next: Mutex::new((0, vec![false; extra as usize])), }; // A slot never written is undefined: shaders reading one (a slot // the title wrote in a way we do not know, or a table entry no @@ -203,14 +224,14 @@ impl HostHeap { /// `n` consecutive free dynamic slots (view heaps only), taken until /// [`HostHeap::release`]: the first's index. pub(crate) fn dynamic(&self, n: u32) -> Option { - if self.sampler || n == 0 || n > DYNAMIC { + if self.sampler || n == 0 || n > self.dynamic { return None; } let mut guard = self.next.lock().unwrap_or_else(|e| e.into_inner()); let (next, taken) = &mut *guard; let n = n as usize; // Search from `next` on, then from the start. - let starts = (*next as usize..DYNAMIC as usize).chain(0..*next as usize); + let starts = (*next as usize..self.dynamic as usize).chain(0..*next as usize); let mut run = 0; let mut found = None; let mut last = usize::MAX; @@ -232,7 +253,7 @@ impl HostHeap { return None; }; taken[first..first + n].fill(true); - *next = ((first + n) % DYNAMIC as usize) as u32; + *next = ((first + n) % self.dynamic as usize) as u32; Some(self.count + first as u32) } @@ -251,7 +272,7 @@ impl HostHeap { return; }; let mut guard = self.next.lock().unwrap_or_else(|e| e.into_inner()); - let end = (at + n).min(DYNAMIC) as usize; + let end = (at + n).min(self.dynamic) as usize; if let Some(slots) = guard.1.get_mut(at as usize..end) { slots.fill(false); } @@ -864,13 +885,13 @@ mod tests { let Some(heap) = HostHeap::new(false, 4) else { return; // no host GPU }; - let a = heap.dynamic(DYNAMIC - 100).unwrap(); + let a = heap.dynamic(heap.dynamic - 100).unwrap(); let b = heap.dynamic(60).unwrap(); - assert_eq!((a, b), (4, 4 + DYNAMIC - 100)); + assert_eq!((a, b), (4, 4 + heap.dynamic - 100)); // 40 left at the end: 50 do not fit until something is given back. assert_eq!(heap.dynamic(50), None); heap.release(a, 50); assert_eq!(heap.dynamic(50), Some(4)); - assert_eq!(heap.dynamic(40), Some(4 + DYNAMIC - 40)); + assert_eq!(heap.dynamic(40), Some(4 + heap.dynamic - 40)); } }