From eec9fb429b14bcccca96d4bf76798c9c51e7a304 Mon Sep 17 00:00:00 2001 From: ivall Date: Sat, 21 Nov 2026 17:51:38 +0100 Subject: [PATCH] PR #6 follow-up: linear textures keep addrlib's layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #6 laid linear levels out from mip 0 and 3D textures level by level; addrlib's GFX10 linear layout (Gfx10Lib::HwlComputeSurfaceInfoLinear) has the levels from the smallest one and a whole chain per depth slice – what tiling::Layout (mode 0) and Chain already do. Its separate linear chain and the tests asserting that layout go; its T# validation, 1D height, span and subresource checks stay. --- crates/xcl/src/gpu/texture.rs | 282 ++++------------------------------ 1 file changed, 27 insertions(+), 255 deletions(-) diff --git a/crates/xcl/src/gpu/texture.rs b/crates/xcl/src/gpu/texture.rs index 390a100..6a8ea68 100644 --- a/crates/xcl/src/gpu/texture.rs +++ b/crates/xcl/src/gpu/texture.rs @@ -34,45 +34,11 @@ pub struct Placement { /// first byte (the T# address rounded down to a block; the rest is the base swizzle) pub start: u64, pub bytes: usize, - pub chain: Option, - linear: Option, - /// array slices (cube faces), or the storage layers of a tiled 3D texture. + pub chain: Chain, + /// array slices (cube faces), or the storage layers of a 3D texture. pub slices: usize, } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -struct LinearLevel { offset: usize, width: usize, height: usize, depth: usize, pitch_bytes: usize } - -#[derive(Clone, Debug, PartialEq, Eq)] -struct LinearChain { elem_bytes: usize, slice_bytes: usize, levels: Vec } - -fn align_256(n: usize) -> Option { n.checked_add(255).map(|x| x & !255) } - -fn linear_element(format: u32) -> Option<(usize, u32)> { - tiling::element(format) - .map(|(log2, side)| (1usize << log2, side)) - .or_else(|| (72..=74).contains(&format).then_some((12, 1))) -} - -fn linear_chain(t: &Image) -> Result { - let (elem_bytes, side) = linear_element(t.format).ok_or_else(|| format!("format {} without a linear element layout", t.format))?; - let mut offset = 0usize; - let mut levels = Vec::with_capacity((t.max_mip + 1) as usize); - for mip in 0..=t.max_mip { - let width = (t.width >> mip).max(1).div_ceil(side) as usize; - let height = (t.height >> mip).max(1).div_ceil(side) as usize; - let depth = if t.ty == ImageType::D3 { (t.depth >> mip).max(1) as usize } else { 1 }; - let row_bytes = width.checked_mul(elem_bytes).ok_or("linear texture row size overflow")?; - let pitch_bytes = align_256(row_bytes).ok_or("linear texture row pitch overflow")?; - offset = align_256(offset).ok_or("linear texture mip offset overflow")?; - levels.push(LinearLevel { offset, width, height, depth, pitch_bytes }); - let plane_bytes = pitch_bytes.checked_mul(height).ok_or("linear texture plane size overflow")?; - offset = offset.checked_add(plane_bytes.checked_mul(depth).ok_or("linear texture level size overflow")?).ok_or("linear texture chain size overflow")?; - } - let slice_bytes = align_256(offset).ok_or("linear texture slice size overflow")?; - Ok(LinearChain { elem_bytes, slice_bytes, levels }) -} - pub fn placement(t: &Image, cfg: PipeConfig) -> Result { if !desc::valid_image_shape(t) { return Err(format!("invalid {:?} texture dimensions, mip range or array view", t.ty)); @@ -88,15 +54,7 @@ pub fn placement(t: &Image, cfg: PipeConfig) -> Result { // The T# height field is ignored for 1D resources, just as image_d3d12 ignores it. surface.height = 1; } - if t.swizzle_mode == 0 { - let slices = match t.ty { - ImageType::D1Array | ImageType::D2Array | ImageType::Cube => t.depth.max(1) as usize, - _ => 1, - }; - let linear = linear_chain(&surface)?; - let bytes = linear.slice_bytes.checked_mul(slices).ok_or("linear texture size overflow")?; - return Ok(Placement { start: t.address, bytes, chain: None, linear: Some(linear), slices }); - } + // linear (SW_MODE 0) included: addrlib's 256-byte rows, levels from the smallest let chain = tiling::image_chain(&surface, cfg).ok_or_else(|| format!("SW_MODE {} / format {} without a tiling pattern", t.swizzle_mode, t.format))?; let slices = match t.ty { ImageType::D1Array | ImageType::D2Array | ImageType::Cube => t.depth.max(1) as usize, @@ -105,7 +63,7 @@ pub fn placement(t: &Image, cfg: PipeConfig) -> Result { }; let start = t.address & !(chain.layout.block_bytes as u64 - 1); let bytes = chain.slice_bytes.checked_mul(slices).ok_or("tiled texture size overflow")?; - Ok(Placement { start, bytes, chain: Some(chain), linear: None, slices }) + Ok(Placement { start, bytes, chain, slices }) } /// The subresources of `t` in D3D12 order (mip + slice × mips) from `mem`, its bytes from @@ -115,45 +73,26 @@ pub fn subresources(t: &Image, mem: &[u8], cfg: PipeConfig) -> Result>(), vec![(2, 2, 1, 256), (1, 1, 1, 256), (1, 1, 1, 256)]); - assert_eq!(linear.slice_bytes, 1024); - } - - #[test] - fn linear_96_bit_texture_extracts_and_uploads_twelve_byte_elements() { - let rgb32 = desc::gfx10_format(DXGI_FORMAT_R32G32B32_FLOAT).unwrap(); - let t = image(rgb32, ImageType::D2, 21, 2, 1, 2, 0, 0x34_0000); - let p = placement(&t, CONSOLE_PIPES).unwrap(); - let linear = p.linear.as_ref().unwrap(); - assert_eq!(linear.elem_bytes, 12); - assert_eq!(linear.levels, vec![ - LinearLevel { offset: 0, width: 21, height: 2, depth: 1, pitch_bytes: 256 }, - LinearLevel { offset: 512, width: 10, height: 1, depth: 1, pitch_bytes: 256 }, - ]); - assert_eq!(p.bytes, 768); - let mut mem = vec![0xEE; p.bytes]; - let mut want = Vec::new(); - for (mip, level) in linear.levels.iter().enumerate() { - let row_bytes = level.width * 12; - let mut data = Vec::new(); - for y in 0..level.height { - let value = (mip * 16 + y) as u8; - let start = level.offset + y * level.pitch_bytes; - mem[start..start + row_bytes].fill(value); - data.extend(std::iter::repeat(value).take(row_bytes)); - } - want.push(Subresource { depth: 1, rows: level.height, row_bytes, data }); - } - assert_eq!(subresources(&t, &mem, CONSOLE_PIPES).unwrap(), want); - let Some((dev, queue)) = device() else { return }; - unsafe { - let res = create(&dev, &queue, &t, &mem, CONSOLE_PIPES).unwrap(); - assert_eq!(read_back(&dev, &queue, &res, &want), want); - } - } - #[test] fn one_dimensional_arrays_ignore_tsharp_height_for_linear_and_tiled_layouts() { let rgba8 = desc::gfx10_format(DXGI_FORMAT_R8G8B8A8_UNORM).unwrap(); @@ -556,25 +401,7 @@ mod tests { let mut normalized = t; normalized.height = 1; assert_eq!(placement(&t, CONSOLE_PIPES).unwrap().bytes, placement(&normalized, CONSOLE_PIPES).unwrap().bytes); - let (want, mem) = if mode == 1 { - console_texture(&t) - } else { - let p = placement(&t, CONSOLE_PIPES).unwrap(); - let linear = p.linear.as_ref().unwrap(); - assert!(linear.levels.iter().all(|l| l.height == 1)); - let mut mem = vec![0xEE; p.bytes]; - let mut want = Vec::new(); - for slice in 0..p.slices { - for (mip, level) in linear.levels.iter().enumerate() { - let row_bytes = level.width * linear.elem_bytes; - let value = (slice * 16 + mip) as u8; - let start = slice * linear.slice_bytes + level.offset; - mem[start..start + row_bytes].fill(value); - want.push(Subresource { depth: 1, rows: 1, row_bytes, data: vec![value; row_bytes] }); - } - } - (want, mem) - }; + let (want, mem) = console_texture(&t); assert!(want.iter().all(|s| s.rows == 1)); assert_eq!(subresources(&t, &mem, CONSOLE_PIPES).unwrap(), want); if let Some((dev, queue)) = &gpu { @@ -593,27 +420,8 @@ mod tests { for mode in [0, 1] { let t = image(rgba8, ImageType::D2, 17, 3, 9, 2, mode, 0x37_0000); assert_eq!(desc::image_d3d12(&t).unwrap().0.DepthOrArraySize, 1); - let (want, mem) = if mode == 1 { - console_texture(&t) - } else { - let p = placement(&t, CONSOLE_PIPES).unwrap(); - assert_eq!(p.slices, 1); - let linear = p.linear.as_ref().unwrap(); - let mut mem = vec![0xEE; p.bytes]; - let mut want = Vec::new(); - for (mip, level) in linear.levels.iter().enumerate() { - let row_bytes = level.width * linear.elem_bytes; - let mut data = Vec::new(); - for y in 0..level.height { - let value = (mip * 8 + y) as u8; - let start = level.offset + y * level.pitch_bytes; - mem[start..start + row_bytes].fill(value); - data.extend(std::iter::repeat(value).take(row_bytes)); - } - want.push(Subresource { depth: 1, rows: level.height, row_bytes, data }); - } - (want, mem) - }; + assert_eq!(placement(&t, CONSOLE_PIPES).unwrap().slices, 1); + let (want, mem) = console_texture(&t); assert_eq!(subresources(&t, &mem, CONSOLE_PIPES).unwrap(), want); if let Some((dev, queue)) = &gpu { unsafe { @@ -661,42 +469,6 @@ mod tests { assert!(desc::image_d3d12(&t).is_none()); } - #[test] - fn linear_3d_mips_extract_and_upload_every_depth_plane() { - let r8 = desc::gfx10_format(DXGI_FORMAT_R8_UNORM).unwrap(); - let t = image(r8, ImageType::D3, 5, 3, 4, 3, 0, 0x38_0000); - let p = placement(&t, CONSOLE_PIPES).unwrap(); - let linear = p.linear.as_ref().unwrap(); - assert_eq!(linear.levels, vec![ - LinearLevel { offset: 0, width: 5, height: 3, depth: 4, pitch_bytes: 256 }, - LinearLevel { offset: 3072, width: 2, height: 1, depth: 2, pitch_bytes: 256 }, - LinearLevel { offset: 3584, width: 1, height: 1, depth: 1, pitch_bytes: 256 }, - ]); - assert_eq!(p.bytes, 3840); - let mut mem = vec![0xEE; p.bytes]; - let mut want = Vec::new(); - for (mip, level) in linear.levels.iter().enumerate() { - let row_bytes = level.width * linear.elem_bytes; - let plane_bytes = level.pitch_bytes * level.height; - let mut data = Vec::new(); - for z in 0..level.depth { - for y in 0..level.height { - let value = (mip * 40 + z * 8 + y) as u8; - let start = level.offset + z * plane_bytes + y * level.pitch_bytes; - mem[start..start + row_bytes].fill(value); - data.extend(std::iter::repeat(value).take(row_bytes)); - } - } - want.push(Subresource { depth: level.depth, rows: level.height, row_bytes, data }); - } - assert_eq!(subresources(&t, &mem, CONSOLE_PIPES).unwrap(), want); - let Some((dev, queue)) = device() else { return }; - unsafe { - let res = create(&dev, &queue, &t, &mem, CONSOLE_PIPES).unwrap(); - assert_eq!(read_back(&dev, &queue, &res, &want), want); - } - } - fn device() -> Option<(ID3D12Device, ID3D12CommandQueue)> { unsafe { let mut dev: Option = None; @@ -881,7 +653,7 @@ mod tests { let Some(err) = mip_error(&subs[k - 1], &subs[k], ch, cb) else { continue }; bad = bad.max(err); // control: a tail level read from the tail block's corner - let chain = p.chain.as_ref().unwrap(); + let chain = &p.chain; let l = chain.levels[k]; if l.origin != (0, 0) && l.width >= 2 && l.height >= 2 { let mut wrong = chain.clone();