diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 74e4bf550c..5f27d71df5 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -1344,8 +1344,129 @@ fn make_marked(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> (Option, O /// format of its family takes the texture's format unless it is castable. /// shortcut: such a view (sRGB of UNORM, say, on a texture the host made /// without castable formats) reads without its conversion. +/// Synthesizes a default shader resource view description for a typeless host +/// texture when the title omitted `pDesc`. Typed host textures stay `None` so +/// D3D12 fills in its own defaults. +fn default_srv_desc( + resource: crate::object::This, + texture: &ID3D12Resource, +) -> Option { + // SAFETY: a live resource. + let d = unsafe { texture.GetDesc() }; + if !crate::resource::is_typeless(d.Format.0 as u32) { + return None; + } + let mips = u32::from(d.MipLevels); + let slices = u32::from(d.DepthOrArraySize); + let mut desc = D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: DXGI_FORMAT(crate::resource::view_format(resource).unwrap_or(0) as i32), + Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + ..Default::default() + }; + match d.Dimension { + D3D12_RESOURCE_DIMENSION_TEXTURE1D if slices <= 1 => { + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE1D; + desc.Anonymous.Texture1D = D3D12_TEX1D_SRV { + MipLevels: mips, + ..Default::default() + }; + } + D3D12_RESOURCE_DIMENSION_TEXTURE1D => { + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE1DARRAY; + desc.Anonymous.Texture1DArray = D3D12_TEX1D_ARRAY_SRV { + MipLevels: mips, + ArraySize: slices, + ..Default::default() + }; + } + D3D12_RESOURCE_DIMENSION_TEXTURE2D if d.SampleDesc.Count > 1 && slices <= 1 => { + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DMS; + } + D3D12_RESOURCE_DIMENSION_TEXTURE2D if d.SampleDesc.Count > 1 => { + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY; + desc.Anonymous.Texture2DMSArray = D3D12_TEX2DMS_ARRAY_SRV { + ArraySize: slices, + ..Default::default() + }; + } + D3D12_RESOURCE_DIMENSION_TEXTURE2D if slices <= 1 => { + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + desc.Anonymous.Texture2D = D3D12_TEX2D_SRV { + MipLevels: mips, + ..Default::default() + }; + } + D3D12_RESOURCE_DIMENSION_TEXTURE2D => { + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; + desc.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_SRV { + MipLevels: mips, + ArraySize: slices, + ..Default::default() + }; + } + D3D12_RESOURCE_DIMENSION_TEXTURE3D => { + desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE3D; + desc.Anonymous.Texture3D = D3D12_TEX3D_SRV { + MipLevels: mips, + ..Default::default() + }; + } + _ => return None, + } + Some(desc) +} + +/// Synthesizes a default unordered access view description (mip 0) for a +/// typeless host texture when the title omitted `pDesc`. +fn default_uav_desc( + resource: crate::object::This, + texture: &ID3D12Resource, +) -> Option { + // SAFETY: a live resource. + let d = unsafe { texture.GetDesc() }; + if !crate::resource::is_typeless(d.Format.0 as u32) || d.SampleDesc.Count > 1 { + return None; + } + let slices = u32::from(d.DepthOrArraySize); + let mut desc = D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: DXGI_FORMAT(crate::resource::view_format(resource).unwrap_or(0) as i32), + ..Default::default() + }; + match d.Dimension { + D3D12_RESOURCE_DIMENSION_TEXTURE1D if slices <= 1 => { + desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE1D; + } + D3D12_RESOURCE_DIMENSION_TEXTURE1D => { + desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE1DARRAY; + desc.Anonymous.Texture1DArray = D3D12_TEX1D_ARRAY_UAV { + ArraySize: slices, + ..Default::default() + }; + } + D3D12_RESOURCE_DIMENSION_TEXTURE2D if slices <= 1 => { + desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; + } + D3D12_RESOURCE_DIMENSION_TEXTURE2D => { + desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; + desc.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_UAV { + ArraySize: slices, + ..Default::default() + }; + } + D3D12_RESOURCE_DIMENSION_TEXTURE3D => { + desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE3D; + desc.Anonymous.Texture3D = D3D12_TEX3D_UAV { + WSize: slices, + ..Default::default() + }; + } + _ => return None, + } + Some(desc) +} + fn fits(texture: &ID3D12Resource, castable: &[u32], mut format: Option<&mut DXGI_FORMAT>, unordered: bool) -> bool { - use crate::resource::{DEPTH_FORMATS, family}; + use crate::resource::{DEPTH_FORMATS, concrete_view_format, family, is_typeless}; // SAFETY: a live resource. let d = unsafe { texture.GetDesc() }; let allowed = if unordered { @@ -1363,8 +1484,8 @@ fn fits(texture: &ID3D12Resource, castable: &[u32], mut format: Option<&mut DXGI ); return false; } + let depth = d.Flags.0 & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL.0 != 0; // Every family has more than one format, its typeless one first. - let is_typeless = |f: u32| family(f) == f && family(f + 1) == f; let typeless = is_typeless(own); if let Some(format) = format.as_deref_mut() && unordered @@ -1375,29 +1496,45 @@ fn fits(texture: &ID3D12Resource, castable: &[u32], mut format: Option<&mut DXGI format.0 = linear as i32; } let view = format.as_ref().map_or(0, |f| f.0 as u32); - // A typed unordered view in a format the host cannot store to - // (B5G5R5A1_UNORM on NVIDIA) removes the device when made, in the - // texture's own format too: the slot stays null, its writes dropped. - // The format checked is the one the view is made in: a typed - // texture's own unless the requested one is castable (below). - let typed = if view == 0 || (!typeless && !castable.contains(&view)) { own } else { view }; - if unordered && !(view == 0 && typeless) && !typed_store(typed) { - once!(view = typed, own, "an unordered view in a format the host cannot store"); - return false; - } if view == 0 { - return !typeless && (!unordered || typed_store(own)); - } - if DEPTH_FORMATS.iter().any(|&(typed, _)| typed == view) || is_typeless(view) { - once!(unordered, view, own, "view the host does not allow"); - return false; + if !typeless { + return !unordered || typed_store(own); + } + let Some(format) = format else { + return false; + }; + let Some(concrete) = concrete_view_format(own, depth, unordered) else { + return false; + }; + if unordered && !typed_store(concrete) { + once!( + view = concrete, + own, + "an unordered view in a format the host cannot store" + ); + return false; + } + *format = DXGI_FORMAT(concrete as i32); + return true; } let cast = castable.contains(&view); if family(view) != family(own) && !cast { once!(view, own, "view format of another family"); return false; } - let effective = if typeless || cast { view } else { own }; + if is_typeless(view) { + once!(view, own, "a typeless view format"); + return false; + } + let resolved = if typeless { + let Some(concrete) = concrete_view_format(view, depth, unordered) else { + return false; + }; + concrete + } else { + view + }; + let effective = if typeless || cast { resolved } else { own }; if unordered && !typed_store(effective) { once!( view = effective, @@ -1406,11 +1543,12 @@ fn fits(texture: &ID3D12Resource, castable: &[u32], mut format: Option<&mut DXGI ); return false; } - if let Some(format) = format - && !typeless - && !cast - { - *format = d.Format; + if let Some(format) = format { + if !typeless && !cast { + *format = d.Format; + } else if typeless { + *format = DXGI_FORMAT(resolved as i32); + } } true } @@ -1495,7 +1633,10 @@ fn make(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option { if let Some(copy) = crate::resource::host_copy(resource) { copy.viewed.store(true, std::sync::atomic::Ordering::Relaxed); let texture = ©.texture; - let mut desc = desc.as_ref().map(srv_desc); + let mut desc = desc + .as_ref() + .map(srv_desc) + .or_else(|| default_srv_desc(resource, texture)); if desc.is_none() && !default_view(resource) { return None; } @@ -1543,7 +1684,10 @@ fn make(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option { return crate::twin::uav(resource, ©, desc.as_ref(), at) .map(|twin| Some((twin, None))); } - let mut desc = desc.as_ref().map(uav_desc); + let mut desc = desc + .as_ref() + .map(uav_desc) + .or_else(|| default_uav_desc(resource, texture)); if desc.is_none() && !default_view(resource) { return None; } diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index a9860b5bb7..b13338736e 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -278,6 +278,43 @@ pub(crate) fn family(format: u32) -> u32 { } } +/// Whether a format is the root typeless format of a multi-format family. +pub(crate) fn is_typeless(format: u32) -> bool { + format != 0 + && family(format) == format + && format.checked_add(1).is_some_and(|next| family(next) == format) +} + +/// A concrete view format for a typeless or depth-attachment format, aware of +/// whether the resource carries `ALLOW_DEPTH_STENCIL` (`depth`) and whether +/// the view is unordered access (`unordered`). Returns `None` when the format +/// has no legal D3D12 view of the requested kind (such as planar depth UAVs). +pub(crate) fn concrete_view_format(format: u32, depth: bool, unordered: bool) -> Option { + let resolved = match format { + 19..=21 => (!unordered).then_some(21)?, // R32_FLOAT_X8X24_TYPELESS (SRV only) + 22 => (!unordered).then_some(22)?, // X32_TYPELESS_G8X24_UINT (stencil SRV only) + 44..=46 => (!unordered).then_some(46)?, // R24_UNORM_X8_TYPELESS (SRV only) + 47 => (!unordered).then_some(47)?, // X24_TYPELESS_G8_UINT (stencil SRV only) + 39 => 41, // R32_TYPELESS -> R32_FLOAT + 40 => (depth && !unordered).then_some(41)?, // D32_FLOAT -> R32_FLOAT (depth SRV only) + 53 => { + if depth { + (!unordered).then_some(56)? // depth R16_TYPELESS -> R16_UNORM + } else { + 54 // color R16_TYPELESS -> R16_FLOAT + } + } + 55 => (depth && !unordered).then_some(56)?, // D16_UNORM -> R16_UNORM (depth SRV only) + 90 => 87, // B8G8R8A8_TYPELESS -> B8G8R8A8_UNORM + 88 | 92 => (!unordered).then_some(88)?, // B8G8R8X8 -> B8G8R8X8_UNORM (SRV only) + 70..=84 | 94..=99 if unordered => return None, // block-compressed formats have no UAV + 94 => 95, // BC6H_TYPELESS -> BC6H_UF16 + f if is_typeless(f) => f + 1, + f => f, + }; + Some(resolved) +} + /// sRGB formats and their linear twins. pub(crate) const SRGB: [(u32, u32); 3] = [(29, 28), (91, 87), (93, 88)]; @@ -3179,4 +3216,52 @@ mod tests { assert_eq!(slot::(heap, 0x10)(heap), 1); assert_eq!(slot::(placed, 0x10)(placed), 0); } + + #[test] + fn concrete_view_format_resolves_color_depth_uav_and_bounds() { + assert!(!is_typeless(0)); + assert!(!is_typeless(u32::MAX)); + assert!(is_typeless(27)); + assert!(is_typeless(53)); + + // Color typeless formats resolve to primary typed view format. + assert_eq!(concrete_view_format(27, false, false), Some(28)); + assert_eq!(concrete_view_format(27, false, true), Some(28)); + assert_eq!(concrete_view_format(90, false, false), Some(87)); + assert_eq!(concrete_view_format(92, false, false), Some(88)); + assert_eq!(concrete_view_format(92, false, true), None); + assert_eq!(concrete_view_format(9, false, false), Some(10)); + assert_eq!(concrete_view_format(94, false, false), Some(95)); + assert_eq!(concrete_view_format(94, false, true), None); + assert_eq!(concrete_view_format(97, false, false), Some(98)); + assert_eq!(concrete_view_format(97, false, true), None); + + // R16_TYPELESS (53) distinguishes depth (R16_UNORM 56) from color/UAV (R16_FLOAT 54). + assert_eq!(concrete_view_format(53, true, false), Some(56)); + assert_eq!(concrete_view_format(53, false, false), Some(54)); + assert_eq!(concrete_view_format(53, false, true), Some(54)); + assert_eq!(concrete_view_format(56, false, false), Some(56)); + + // Depth typeless and attachment formats resolve to valid SRVs and reject planar UAVs. + assert_eq!(concrete_view_format(19, true, false), Some(21)); + assert_eq!(concrete_view_format(20, true, false), Some(21)); + assert_eq!(concrete_view_format(19, true, true), None); + assert_eq!(concrete_view_format(21, false, true), None); + assert_eq!(concrete_view_format(44, true, false), Some(46)); + assert_eq!(concrete_view_format(45, true, false), Some(46)); + assert_eq!(concrete_view_format(44, true, true), None); + assert_eq!(concrete_view_format(46, false, true), None); + assert_eq!(concrete_view_format(39, true, false), Some(41)); + assert_eq!(concrete_view_format(40, true, false), Some(41)); + assert_eq!(concrete_view_format(40, false, false), None); + assert_eq!(concrete_view_format(40, false, true), None); + assert_eq!(concrete_view_format(55, true, false), Some(56)); + assert_eq!(concrete_view_format(55, false, false), None); + assert_eq!(concrete_view_format(55, false, true), None); + + // Typed formats stay untouched when legal. + assert_eq!(concrete_view_format(28, false, false), Some(28)); + assert_eq!(concrete_view_format(46, true, false), Some(46)); + assert_eq!(concrete_view_format(47, true, false), Some(47)); + } }