Merge pull request #205 from asdaasdsaddf/fix/null-uav-dimension

GPU: give null texture UAVs the title's view dimension
This commit is contained in:
Marvis authored and GitHub committed 2026-11-24 08:55:06 +01:00
commit 6af0faeb3e
1 file changed
+90 -11
+90 -11
View File
@@ -734,9 +734,13 @@ pub(crate) fn placed_texture(view: &View) -> Option<View> {
fn null_for(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) {
let Some(host) = host() else { return };
let is_buffer = |r: &usize| crate::resource::buffer_bytes(*r as crate::object::This).is_some();
let buffer = match view {
View::Unordered(r, _, desc) => is_buffer(r) || desc.is_some_and(|d| d[1] == 1),
View::Placed(true, resource, _) => resource[0] == 1,
// Unordered views: whether a buffer, and the title's view dimension.
let (buffer, dimension) = match view {
View::Unordered(r, _, desc) => (
is_buffer(r) || desc.is_some_and(|d| d[1] == 1),
desc.map(|d| d[1]),
),
View::Placed(true, resource, desc) => (resource[0] == 1, Some(desc[1])),
// Shader views of buffers read nothing through a null buffer view:
// a null 2D view where the shader expects a buffer is a mismatch
// the hardware does not survive in general (GPU-based validation
@@ -758,14 +762,14 @@ fn null_for(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) {
}
_ => return null_view(at),
};
let desc = D3D12_UNORDERED_ACCESS_VIEW_DESC {
Format: DXGI_FORMAT_R32_UINT,
ViewDimension: if buffer {
D3D12_UAV_DIMENSION_BUFFER
} else {
D3D12_UAV_DIMENSION_TEXTURE2D
},
..Default::default()
let desc = if buffer {
D3D12_UNORDERED_ACCESS_VIEW_DESC {
Format: DXGI_FORMAT_R32_UINT,
ViewDimension: D3D12_UAV_DIMENSION_BUFFER,
..Default::default()
}
} else {
null_uav_desc(dimension)
};
// SAFETY: a slot of our heap; a null resource is allowed with a desc.
unsafe {
@@ -778,6 +782,46 @@ fn null_for(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) {
};
}
/// A null texture UAV of the title's view dimension (1D, 1D or 2D arrays,
/// 3D), 2D if it names none: a 2D null view where the shader writes a 3D
/// texture is a mismatch GPU-based validation reports (the world's wind
/// field, CS_CopyWindFieldBeforeUpdateWithSmooth, 2026-11-24).
fn null_uav_desc(dimension: Option<u32>) -> D3D12_UNORDERED_ACCESS_VIEW_DESC {
let mut desc = D3D12_UNORDERED_ACCESS_VIEW_DESC {
Format: DXGI_FORMAT_R32_UINT,
ViewDimension: D3D12_UAV_DIMENSION_TEXTURE2D,
..Default::default()
};
match dimension.map(|d| D3D12_UAV_DIMENSION(d as i32)) {
Some(D3D12_UAV_DIMENSION_TEXTURE1D) => {
desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE1D;
}
Some(D3D12_UAV_DIMENSION_TEXTURE1DARRAY) => {
desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE1DARRAY;
desc.Anonymous.Texture1DArray = D3D12_TEX1D_ARRAY_UAV {
ArraySize: 1,
..Default::default()
};
}
Some(D3D12_UAV_DIMENSION_TEXTURE2DARRAY) => {
desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;
desc.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_UAV {
ArraySize: 1,
..Default::default()
};
}
Some(D3D12_UAV_DIMENSION_TEXTURE3D) => {
desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE3D;
desc.Anonymous.Texture3D = D3D12_TEX3D_UAV {
WSize: u32::MAX,
..Default::default()
};
}
_ => {}
}
desc
}
/// A buffer view that reads zeros (raw, as translated shaders mostly read
/// buffers).
fn null_buffer_view(at: D3D12_CPU_DESCRIPTOR_HANDLE) {
@@ -856,6 +900,41 @@ fn null_view(at: D3D12_CPU_DESCRIPTOR_HANDLE) {
mod tests {
use super::*;
/// A null texture UAV keeps the title's dimension (3D for the world's
/// wind field), and is 2D when the title names none.
#[test]
fn null_uavs_keep_the_titles_dimension() {
let d = null_uav_desc(Some(8));
assert_eq!(d.ViewDimension, D3D12_UAV_DIMENSION_TEXTURE3D);
// SAFETY: the member the dimension names.
assert_eq!(unsafe { d.Anonymous.Texture3D.WSize }, u32::MAX);
let d = null_uav_desc(Some(5));
assert_eq!(d.ViewDimension, D3D12_UAV_DIMENSION_TEXTURE2DARRAY);
// SAFETY: the member the dimension names.
assert_eq!(unsafe { d.Anonymous.Texture2DArray.ArraySize }, 1);
let dimension = |d| null_uav_desc(d).ViewDimension;
assert_eq!(dimension(Some(2)), D3D12_UAV_DIMENSION_TEXTURE1D);
assert_eq!(dimension(Some(4)), D3D12_UAV_DIMENSION_TEXTURE2D);
assert_eq!(dimension(None), D3D12_UAV_DIMENSION_TEXTURE2D);
// Multisampled UAVs need a cap; they stay 2D.
assert_eq!(dimension(Some(6)), D3D12_UAV_DIMENSION_TEXTURE2D);
}
/// A null 3D UAV made on the real device leaves it alive.
#[test]
fn null_3d_uav_is_accepted_by_the_device() {
let Some(host) = host() else {
return; // no host GPU
};
let Some(heap) = HostHeap::new(false, 4) else {
return;
};
let view = View::Unordered(0, 0, Some([28, 8, 0, 0, 0, 0, 0, 0, 0, 0]));
null_for(&view, heap.cpu(0));
// SAFETY: a plain device query.
assert!(unsafe { host.device.GetDeviceRemovedReason() }.is_ok());
}
/// Dynamic slots stay taken until given back, and runs do not wrap.
#[test]
fn dynamic_slots_are_held_until_released() {