From e69981f6a8afb04fdc6746aabd5a8d66bc4837fa Mon Sep 17 00:00:00 2001 From: gdemontalivet Date: Thu, 26 Nov 2026 00:10:42 +0000 Subject: [PATCH] Bind explicit null root buffers to null descriptors in the active heap Keep the SRV and UAV in range even on hosts with capped heaps. Skip the address-zero shadow in title-memory lookup. Exercise zero reads and discarded writes using the existing GPU readback fixture. --- crates/victory-gpu/src/descriptor/host.rs | 30 ++++++++--- crates/victory-gpu/src/draw.rs | 64 +++++++++++++++++++---- 2 files changed, 76 insertions(+), 18 deletions(-) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index f8ff7c8c..f7dfc7a9 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -42,6 +42,8 @@ pub(crate) struct HostHeap { /// taken, those given back, and where they end. pairs: Mutex<(u32, Vec)>, pairs_end: u32, + /// Null raw SRV for explicitly bound root descriptors. Its UAV is one slot below. + null_root_srv: Option, /// View heaps: the view-stride table translated shaders read /// (`victory_shader::emit::VIEW_STRIDES`), a dword per title slot: the /// byte stride of the buffer view there, 0 for none. Mapped at the @@ -199,7 +201,7 @@ impl HostHeap { u64::from(step), sampler, )); - let h = HostHeap { + let mut h = HostHeap { heap, cpu, step, @@ -210,6 +212,7 @@ impl HostHeap { kept: Mutex::new(Paged::new((count + extra) as usize)), next: Mutex::new((0, vec![false; extra as usize])), pairs: Mutex::new((count + extra, Vec::new())), + null_root_srv: None, pairs_end: match victory_shader::emit::NULL_BUFFER_VIEW { null if !sampler && null > count + extra && null < total => null - 1, _ => total, @@ -234,16 +237,29 @@ impl HostHeap { // What shaders read instead of a slot without a buffer view where // they take one at a computed index (`victory_shader::emit`, "Buffer // views at computed heap indices"): a null raw SRV, and UAV below it. - let null = victory_shader::emit::NULL_BUFFER_VIEW; - if !sampler && null > count + extra && null < total { - h.raw_view_at(null, None, 0, 0, false); - h.raw_view_at(null - 1, None, 0, 0, true); - } else if !sampler { - tracing::warn!(count, total, "no room for the null buffer views: computed indices of slots without a buffer view read what is there"); + if !sampler { + let fixed = victory_shader::emit::NULL_BUFFER_VIEW; + let null = if fixed > count + extra && fixed < total { + Some(fixed) + } else { + tracing::warn!(count, total, "no room for the null buffer views: computed indices of slots without a buffer view read what is there"); + // Root descriptors receive a heap index at bind time and can + // use a pair inside a capped heap. Keep it for the heap's life. + h.pair().map(|first| first + 1) + }; + if let Some(srv) = null { + h.raw_view_at(srv, None, 0, 0, false); + h.raw_view_at(srv - 1, None, 0, 0, true); + h.null_root_srv = Some(srv); + } } Some(h) } + pub(crate) fn null_root_view(&self, uav: bool) -> Option { + self.null_root_srv.map(|srv| srv - u32::from(uav)) + } + /// Makes the first `total` slots null: one null slot in a CPU-side heap, /// doubled by copies to a block, the block copied over the heap. fn fill_null(&self, kind: D3D12_DESCRIPTOR_HEAP_TYPE, total: u32) -> windows::core::Result<()> { diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index e867da8f..f9659f77 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -720,6 +720,7 @@ impl State { && u.dword == 0 && let Some(Root::View(address)) = self.roots_ref(compute).get(usize::from(u.slot)) + && *address != 0 && let Some(shadow) = crate::shadow::find(*address, 1) { let uav = t.used.iter().any(|r| { @@ -737,6 +738,7 @@ impl State { None if u.dword == victory_shader::emit::ROOT_VIEW_OFFSET => { // Where the root address lies in the copy its view shows. let offset = match self.roots_ref(compute).get(usize::from(u.slot)) { + Some(&Root::View(0)) => Some(0), Some(&Root::View(address)) => crate::shadow::find(address, 1) .and_then(|s| u32::try_from(s.offset(address)).ok()), _ => None, @@ -891,6 +893,24 @@ impl State { not_run("a root view the title did not set"); return None; }; + let (key, host) = match self.heaps[0] { + 0 => (MEMORY_HEAP, crate::memory::heap()), + h => ( + std::ptr::from_ref(crate::descriptor::heap(h as This)) as usize, + crate::descriptor::heap(h as This).host(), + ), + }; + let Some(host) = host else { + not_run("a root view without a host view heap"); + return None; + }; + if address == 0 { + let index = host.null_root_view(uav); + if index.is_none() { + not_run("no null root buffer view in the host heap"); + } + return index.map(|slot| (slot, 0)); + } let Some(shadow) = crate::shadow::find(address, 1) else { not_run("a root view outside title buffers"); return None; @@ -906,17 +926,6 @@ impl State { let len = if shadow.marked() { shadow.left(address) } else { shadow.left(address).min(1 << 20) }; shadow.gpu_writes(list, address, len, true); } - let (key, host) = match self.heaps[0] { - 0 => (MEMORY_HEAP, crate::memory::heap()), - h => ( - std::ptr::from_ref(crate::descriptor::heap(h as This)) as usize, - crate::descriptor::heap(h as This).host(), - ), - }; - let Some(host) = host else { - not_run("a root view without a host view heap"); - return None; - }; let copy = std::sync::Arc::as_ptr(&shadow) as usize; if let Some(&index) = self.root_slots.get(&(key, copy, uav)) { return Some((index, offset)); @@ -2202,6 +2211,39 @@ uint ps() : SV_Target { return 1; } host.execute(&[&list]); assert_eq!(out.read_host(dst_bytes as u64, 16), want, "round {round}"); } + + // Rebind the same slots to a null SRV, then to a null UAV. The + // previous round left nonzero words in the destination. + for null_uav in [false, true] { + list.reset(); + let mut state = State::default(); + state.set_heaps(&[heap]); + assert!(state.root_view(&list, true, 0, false).is_none(), "unset root"); + state.set_view(true, 0, if null_uav { src + 0x18_0100 } else { 0 }); + state.set_view(true, 1, if null_uav { 0 } else { dst_bytes as u64 }); + let (input, at) = state.root_view(&list, true, 0, false).unwrap(); + let (output, out_at) = state.root_view(&list, true, 1, true).unwrap(); + assert_ne!(input, output); + assert_eq!(state.root_slots.len(), 1, "only the real buffer needs a dynamic view"); + assert_eq!(out_at, 0); + if !null_uav { + assert_eq!(at, 0); + } + let constants = [at, 0]; + // SAFETY: all descriptors are in the bound heap and held through execution. + unsafe { + list.list.SetDescriptorHeaps(&[Some(heap_host.heap.clone())]); + list.list.SetComputeRootSignature(&root); + list.list.SetComputeRootDescriptorTable(0, heap_host.gpu(input)); + list.list.SetComputeRootDescriptorTable(1, heap_host.gpu(output)); + list.list.SetComputeRoot32BitConstants(2, 2, constants.as_ptr().cast(), 0); + list.list.SetPipelineState(&pso); + list.list.Dispatch(1, 1, 1); + } + list.close(); + host.execute(&[&list]); + assert_eq!(out.read_host(dst_bytes as u64, 16), [0; 16]); + } } /// Title memory of `bytes` (64 KiB aligned) for textures at an address.