From cc8eb8ff7d31e165bbf270f8b9db3075046e74df Mon Sep 17 00:00:00 2001 From: openhands Date: Tue, 24 Nov 2026 20:27:53 +0000 Subject: [PATCH] GPU: a buffer view of no elements is made null instead of removing the device Port of Vaximous's a52f612 on fix/zero-element-buffer-views (issue #912); it applies as-is. D3D12 treats a buffer SRV or UAV with NumElements 0 as an invalid call and removes the device (DXGI_ERROR_INVALID_CALL), with no debug layer needed. buffer_srv and buffer_uav passed such a title view straight to the host. On the console a view of no elements reads and writes nothing, as the null view the slot now gets does. The new test makes placed buffer views of one and of no elements on the host device: without the change the device is removed with 0x887A0001. Co-authored-by: Vaximous Co-authored-by: openhands --- crates/victory-gpu/src/descriptor/host.rs | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 582f2347ad..61341a0765 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -506,6 +506,12 @@ fn buffer_srv( let element = element_size(&desc); // SAFETY: a buffer view's union member. let buffer = unsafe { &mut desc.Anonymous.Buffer }; + // D3D12 refuses a buffer view of no elements (it removes the device); + // the console reads nothing through one, as a null view does. + if buffer.NumElements == 0 { + once!("buffer view of no elements made null"); + return None; + } let offset = shadow.offset(address); if element == 0 || offset % element != 0 { once!( @@ -552,6 +558,11 @@ fn buffer_uav( } else { crate::resource::element(desc.Format.0 as u32).map_or(0, |(bytes, _)| bytes) }; + // As in `buffer_srv`: the console writes nothing through such a view. + if buffer.NumElements == 0 { + once!("unordered buffer view of no elements made null"); + return None; + } let offset = shadow.offset(address); if element == 0 || offset % element != 0 { once!(offset, element, "unordered buffer view not on an element of its host copy"); @@ -958,6 +969,49 @@ mod tests { assert!(unsafe { host.device.GetDeviceRemovedReason() }.is_ok()); } + /// A buffer view of no elements, which the host refuses (removing the + /// device), makes a null slot; one of an element is made as usual. + #[test] + fn buffer_views_of_no_elements_are_null() { + let Some(host) = host() else { + return; // no host GPU + }; + let Some(heap) = HostHeap::new(false, 4) else { + return; + }; + // A title buffer the view's memory lies in (its copy is made on the + // host; title memory is not read). + let (address, len) = (0x7e00_0000_0000_u64, 0x1000_u64); + crate::shadow::register(usize::MAX - 1, address, len); + let mut resource = [0u32; 14]; + resource[0] = 1; // a buffer + resource[4] = len as u32; + let placed = |unordered: bool, elements: u32| { + let mut desc = [0u32; 16]; + desc[1] = 1; // buffer dimension + if unordered { + // NumElements, StructureByteStride + (desc[4], desc[5]) = (elements, 16); + } else { + desc[2] = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + (desc[6], desc[7]) = (elements, 16); + } + (desc[10], desc[11]) = (address as u32, (address >> 32) as u32); + View::Placed(unordered, resource, desc) + }; + let kept = |index: u32| heap.kept.lock().unwrap()[index as usize].is_some(); + for unordered in [false, true] { + heap.make_at(0, &placed(unordered, 1)); + assert!(kept(0), "a view of an element (unordered: {unordered})"); + heap.make_at(1, &placed(unordered, 0)); + assert!(!kept(1), "a view of no elements (unordered: {unordered})"); + // SAFETY: a plain device query. + let removed = unsafe { host.device.GetDeviceRemovedReason() }; + assert!(removed.is_ok(), "device removed: {removed:?} (unordered: {unordered})"); + } + crate::shadow::unregister(usize::MAX - 1, address); + } + /// Dynamic slots stay taken until given back, and runs do not wrap. #[test] fn dynamic_slots_are_held_until_released() {