diff --git a/runtime/xg-layout/src/element.rs b/runtime/xg-layout/src/element.rs index bda8853f..4bc8863f 100644 --- a/runtime/xg-layout/src/element.rs +++ b/runtime/xg-layout/src/element.rs @@ -2,6 +2,34 @@ use super::*; impl Plane { + /// Number of horizontally adjacent storage elements that are guaranteed + /// contiguous when X starts at a multiple of this span. Callers must + /// clamp the final span to the logical row width. Only for 2D + /// single-sample color data; unknown modes and packed tails use one. + pub fn horizontal_copy_span(&self, level: u32) -> u32 { + if level >= self.mip_count() || self.color_samples() != 1 { + return 1; + } + let mip = &self.mips[level as usize]; + if mip.swizzle == 0 { + return mip.width_elements.max(1); + } + if level >= self.unpacked_mips || !matches!(self.element_bytes, 1 | 2 | 4 | 8 | 16) { + return 1; + } + // The low X-coordinate bits in the address equations below + // occupy consecutive byte bits starting at log2(element_bytes). + // The 64 KiB bank XORs only affect higher bits. + let standard = 16 / self.element_bytes; + match (mip.swizzle, self.alignment, self.element_bytes) { + (1, 256, _) | (5, 4096, _) | (9 | 17, 65536, _) => standard, + (2, 256, 1) | (10 | 18, 65536, 1) => 8, + (2, 256, 16) | (10 | 18, 65536, 16) | (27, 65536, 16) => 2, + (2, 256, _) | (10 | 18 | 27, 65536, _) => standard, + _ => 1, + } + } + /// Native linear address-to-coordinate queries scan physical mip ranges /// first, then decode the slice in increasing level order. pub fn linear_coordinate(&self, offset: u64) -> Option<(u32, u64, u32, u32)> { diff --git a/runtime/xg-layout/src/tests/lib.rs b/runtime/xg-layout/src/tests/lib.rs index 3338afeb..94ab6602 100644 --- a/runtime/xg-layout/src/tests/lib.rs +++ b/runtime/xg-layout/src/tests/lib.rs @@ -360,3 +360,51 @@ fn single_sample_color_has_no_msaa_tile() { assert_eq!(p.color_samples(), 1); assert!(p.multisample_tile().is_none()); } + +#[test] +fn contiguous_spans_preserve_every_element_offset() { + assert_eq!(Plane::compute(&desc(61, 64, 64, 1, 1, TILE_4K)).unwrap().horizontal_copy_span(0), 16); + assert_eq!(Plane::compute(&desc(28, 64, 64, 1, 1, TILE_4K)).unwrap().horizontal_copy_span(0), 4); + let mut surfaces = planes(); + for &(format, _) in &FORMATS { + for layout in [257, 258, 265, 266, 273, 274] { + let d = desc(format, 301, 131, 2, 1, layout); + let result = if layout <= 258 { Plane::compute_microtile(&d) } + else { Plane::compute_sparse_64k(&d) }; + if let Ok(p) = result { surfaces.push((d, p)); } + } + let d = Desc { flags: 1, ..desc(format, 301, 131, 2, 4, 0) }; + if let Ok(p) = Plane::compute_64k_mips(&d) { surfaces.push((d, p)); } + } + let mut comparisons = 0u64; + let mut grouped = 0u64; + for (d, p) in surfaces { + for level in 0..p.mip_count() { + let (width, height, _, _) = p.mip_extent(level).unwrap(); + let span = p.horizontal_copy_span(level); + assert!(span > 0); + let eb = u64::from(p.element_bytes()); + for v4 in [false, true] { + for layer in [0, u32::from(d.slices) - 1] { + for y in 0..height.min(140) { + for x in (0..width.min(310)).step_by(span as usize) { + let count = span.min(width - x); + let start = p.element_offset_for_hardware(layer, level, x.into(), y, v4).unwrap(); + for i in 0..count { + let actual = p.element_offset_for_hardware(layer, level, (x + i).into(), y, v4); + assert_eq!(actual, Some(start + u64::from(i) * eb), + "format {} layout {} level {level} v4 {v4} layer {layer} x {x} y {y}", d.format, d.layout); + comparisons += 1; + } + if count > 1 { grouped += 1; } + assert!(start + u64::from(count) * eb <= p.size); + } + } + } + } + } + } + assert!(comparisons > 1_000_000); + assert!(grouped > 10_000); + println!("compared {comparisons} elements across {grouped} contiguous spans"); +} diff --git a/runtime/xg/src/copies.rs b/runtime/xg/src/copies.rs index af5aa47f..db16a229 100644 --- a/runtime/xg/src/copies.rs +++ b/runtime/xg/src/copies.rs @@ -123,7 +123,13 @@ unsafe fn transfer( if (height > 1 && row_pitch < row_bytes) || (depth > 1 && (slice_pitch as u64) < row_end) { return E_INVALIDARG; } - let copy_width = if row_linear { width } else { 1 }; + let copy_width = if row_linear { + width + } else if this.dimension == 3 && this.depth_stencil.is_none() && surface.color_samples() == 1 { + surface.horizontal_copy_span(mip) + } else { + 1 + }; for z in 0..depth { for y in 0..height { let row_start = z as usize * slice_pitch + y as usize * row_pitch; @@ -142,7 +148,7 @@ unsafe fn transfer( let Some(offset) = offset else { return E_INVALIDARG; }; - let count = copy_width as usize * bpe; + let count = copy_width.min(width - x) as usize * bpe; if offset + count as u64 > surface.size { return E_INVALIDARG; }