From ad9c154d7734be7d78d8ea00a2cbe455f9246077 Mon Sep 17 00:00:00 2001 From: b3IKK <278540256+b3IKK@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:57:16 +0000 Subject: [PATCH] D3D12.X: force MipLevels=1 and skip mip arithmetic for MSAA descriptors in image_d3d12 valid_image_shape returns early for D2Msaa and D2MsaaArray without validating max_mip or last_level (which encodes log2(samples) for MSAA). image_d3d12 then unconditionally computed `t.max_mip + 1` and `t.last_level.saturating_sub(t.base_level) + 1`, overflowing on u32::MAX and setting MipLevels > 1 for 1x MSAA textures (last_level == 0, samples == 1). Set mips = 1 and levels = 1 directly whenever the image type is D2Msaa or D2MsaaArray, with a unit test. Assisted-by: OpenCode (Gemini 4 Neon) --- crates/xcl/src/gpu/desc.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/gpu/desc.rs b/crates/xcl/src/gpu/desc.rs index 32b8477a78..123095dbba 100644 --- a/crates/xcl/src/gpu/desc.rs +++ b/crates/xcl/src/gpu/desc.rs @@ -216,7 +216,8 @@ pub fn image_d3d12(t: &Image) -> Option<(D3D12_RESOURCE_DESC, D3D12_SHADER_RESOU return None; } let (format, order) = dxgi_format(t.format)?; - let mips = t.max_mip + 1; + let msaa = matches!(t.ty, ImageType::D2Msaa | ImageType::D2MsaaArray); + let mips = if msaa { 1 } else { t.max_mip + 1 }; let (dim, depth) = match t.ty { ImageType::D1 => (D3D12_RESOURCE_DIMENSION_TEXTURE1D, 1), ImageType::D1Array => (D3D12_RESOURCE_DIMENSION_TEXTURE1D, t.depth), @@ -224,18 +225,18 @@ pub fn image_d3d12(t: &Image) -> Option<(D3D12_RESOURCE_DESC, D3D12_SHADER_RESOU ImageType::D2 | ImageType::D2Msaa => (D3D12_RESOURCE_DIMENSION_TEXTURE2D, 1), ImageType::D2Array | ImageType::D2MsaaArray | ImageType::Cube => (D3D12_RESOURCE_DIMENSION_TEXTURE2D, t.depth), }; - let samples = if matches!(t.ty, ImageType::D2Msaa | ImageType::D2MsaaArray) { 1u32 << t.last_level.min(4) } else { 1 }; + let samples = if msaa { 1u32 << t.last_level.min(4) } else { 1 }; let res = D3D12_RESOURCE_DESC { Dimension: dim, Width: t.width as u64, Height: if dim == D3D12_RESOURCE_DIMENSION_TEXTURE1D { 1 } else { t.height }, DepthOrArraySize: depth as u16, - MipLevels: if samples > 1 { 1 } else { mips as u16 }, + MipLevels: mips as u16, Format: format, SampleDesc: DXGI_SAMPLE_DESC { Count: samples, Quality: 0 }, ..Default::default() }; - let levels = t.last_level.saturating_sub(t.base_level) + 1; + let levels = if msaa { 1 } else { t.last_level.saturating_sub(t.base_level) + 1 }; let slices = depth.saturating_sub(t.base_array).max(1); let mut v = D3D12_SHADER_RESOURCE_VIEW_DESC { Format: format, Shader4ComponentMapping: component_mapping(t.dst_sel, order), ..Default::default() }; let lod = t.min_lod; @@ -581,6 +582,24 @@ mod tests { } } + #[test] + fn msaa_resources_always_have_one_mip_and_never_overflow() { + let mut t = image(&tsharp()).unwrap(); + t.ty = ImageType::D2Msaa; + t.width = 4; + t.height = 4; + t.last_level = 0; // 1x MSAA (samples == 1) + t.max_mip = 15; + let (res, _) = image_d3d12(&t).unwrap(); + assert_eq!(res.MipLevels, 1, "D2Msaa resource must have MipLevels == 1 even when samples == 1"); + + t.ty = ImageType::D2MsaaArray; + t.max_mip = u32::MAX; + t.last_level = u32::MAX; + let (res2, _) = image_d3d12(&t).unwrap(); + assert_eq!(res2.MipLevels, 1); + } + #[test] fn decodes_ssharp() { // clamp/wrap/border, aniso 8, compare LESS, bias -1.5, mag/min linear, mip linear, lod 0..13