diff --git a/runtime/d3d12/src/commands/compute_bindings.rs b/runtime/d3d12/src/commands/compute_bindings.rs index 2678e5c9..d30376bc 100644 --- a/runtime/d3d12/src/commands/compute_bindings.rs +++ b/runtime/d3d12/src/commands/compute_bindings.rs @@ -529,7 +529,7 @@ impl Bindings { list.SetComputeRootShaderResourceView(slot, va); } if let Some(bvh) = &root.bvh { - match self.console_scenes(guest, root, arena) { + match self.console_scenes(guest, root, arena, resources) { Some((nodes, offsets)) => { list.SetComputeRootShaderResourceView(root.bvh_slot(), nodes); if let Some(offsets) = offsets { @@ -658,8 +658,21 @@ impl Bindings { guest: &super::List, root: &SceneRoot, arena: Option<&Arc>, + resources: &crate::device::MediaResources, ) -> Option<(u64, Option)> { use crate::device::console_heap::{self, EMPTY_SCENE, SCENES}; + // The list holds the buffer it binds by address: a larger buffer may + // replace it before the list has run, and the smaller one is given + // back after a while. Without room to hold it the walk keeps its + // empty buffer. + let hold = |buffer: &ID3D12Resource| { + let mut held = resources.lock().unwrap_or_else(|e| e.into_inner()); + let room = held.try_reserve(1).is_ok(); + if room { + held.push(ID3D12Pageable::from(buffer.clone())); + } + room + }; let declares_offsets = root.bvh.as_ref()?.offsets(); if !console_heap::enabled() { return None; @@ -679,7 +692,7 @@ impl Bindings { if !declares_offsets { // The walk reaches its nodes from what another pass left it: an // address in the buffer the scenes are in. - let nodes = heap.buffer(); + let nodes = heap.buffer_held().filter(|(buffer, _)| hold(buffer)); crate::diagnostics::log_once(&root.program.first_console, || { format!( "console walk {}: it declares no scene offsets and is given {}", @@ -687,7 +700,7 @@ impl Bindings { if nodes.is_some() { "the buffer alone" } else { "nothing yet: it keeps its empty buffer" } ) }); - return nodes.map(|nodes| (nodes, None)); + return nodes.map(|(_, nodes)| (nodes, None)); } let mut offsets = [EMPTY_SCENE; SCENES]; let mut found = Vec::with_capacity(root.console.len()); @@ -700,7 +713,7 @@ impl Bindings { } found.push((*source, address, at)); } - let bound = heap.bind(device, offsets); + let bound = heap.bind(device, offsets).filter(|(buffer, ..)| hold(buffer)); crate::diagnostics::log_once(&root.program.first_console, || { let mut text = format!("console walk {}: {} scenes", root.program.source_hash, found.len()); for (index, (source, address, at)) in found.iter().enumerate() { diff --git a/runtime/d3d12/src/commands/recording.rs b/runtime/d3d12/src/commands/recording.rs index 9e3e1632..31ff713d 100644 --- a/runtime/d3d12/src/commands/recording.rs +++ b/runtime/d3d12/src/commands/recording.rs @@ -618,10 +618,9 @@ impl Recording { } let result = self.inline_scenes(list).map(|(vas, geometry)| { let Some(root) = self.bindings.scene_root() else { return }; - let console = root - .bvh - .as_ref() - .and_then(|_| self.bindings.console_scenes(list, root, self.descriptor_arena.as_ref())); + let console = root.bvh.as_ref().and_then(|_| { + self.bindings.console_scenes(list, root, self.descriptor_arena.as_ref(), &self.resources) + }); // SAFETY: the active list is open, as `dispatch_scene` requires. unsafe { self.bindings.dispatch_scene(&self.active.native, root, &vas, geometry, console, dispatch) }; crate::diagnostics::log_once(&root.program.first_dispatch, || { diff --git a/runtime/d3d12/src/device/console_heap.rs b/runtime/d3d12/src/device/console_heap.rs index bad548f8..e57ac577 100644 --- a/runtime/d3d12/src/device/console_heap.rs +++ b/runtime/d3d12/src/device/console_heap.rs @@ -821,10 +821,11 @@ impl ConsoleHeap { Some((space.buffer.clone(), space.address, space.address + block)) } - /// The buffer's GPU address, for a walk that reads no scene offsets. None - /// while nothing was placed, when the buffer the walk already has serves. - pub(crate) fn buffer(&self) -> Option { - self.lock().space.as_ref().map(|space| space.address) + /// The buffer and its address, taken together: a list that binds the + /// address holds the buffer, since a larger one may replace it before + /// that list has run. + pub(crate) fn buffer_held(&self) -> Option<(ID3D12Resource, u64)> { + self.lock().space.as_ref().map(|space| (space.buffer.clone(), space.address)) } /// Structures named, scene addresses named, and bytes in places that are