From a23118376d5fde4d8da3d9a31ff2198d0f1a389c Mon Sep 17 00:00:00 2001 From: "Manley.B" Date: Sat, 28 Nov 2026 19:38:55 +1100 Subject: [PATCH 1/2] Texture read-back: the ocean's height map in SW_64KB_R_X is tiled for each console (port of DoubleGhosty/cool_fox d9f3ea6f8) The title's CPU code reads the ocean's 256x256 R32_FLOAT height map, which its wave passes write, and texback copies it into title memory in the console's layout. That texture is in SW_64KB_R_X, a pipe and bank XOR mode the tiler had no addressing for, so the copy back could not be tiled and the CPU read what was in that memory before. The logs of #788, #853 and #859 each have "a texture copied back could not be tiled e=Unsupported"; the warning does not say for which texture. victory-formats: Layout::configure_render_xor addresses that one layout (a 256x256 plane of 4-byte elements, one mip, a 64 KiB-aligned allocation) with addrlib's GFX10_SW_64K_R_X_1xaa_RBPLUS equations for the console's pipes and packers: Series S 8 and 4, Series X 32 and 8. victory-gpu: title_texture sets it for an R32_FLOAT texture at a 64 KiB-aligned address, by the console the title runs as. A texture whose layout still cannot be addressed is left alone before any copy is recorded, with one warning that names its size, format and swizzle. From DoubleGhosty/cool_fox d9f3ea6f8 ("Support the aligned water height texture readback for boat missions"). The test's digests come from XCL's independent GFX10 tiler. --- crates/victory-formats/src/texture.rs | 122 ++++++++++++++++++++++++-- crates/victory-gpu/src/resource.rs | 43 ++++++++- 2 files changed, 155 insertions(+), 10 deletions(-) diff --git a/crates/victory-formats/src/texture.rs b/crates/victory-formats/src/texture.rs index 61aca7547..426cef1a0 100644 --- a/crates/victory-formats/src/texture.rs +++ b/crates/victory-formats/src/texture.rs @@ -179,6 +179,49 @@ pub struct Layout { /// Byte offsets inside a block for each coordinate, per axis. /// Empty for linear and unsupported modes. pattern: &'static SwizzlePattern, + render_xor: Option, +} + +/// GFX10.3 pipe and packer rows used by the two Series consoles: Series S +/// has 8 pipes and 4 packers, Series X 32 and 8. XCL (`gpu/texture.rs`) +/// found every texel of every mode matches the console's own layout +/// library with these two. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RenderPipeConfig { + Pipes8Packers4, + Pipes32Packers8, +} + +// AMD addrlib, gfx10SwizzlePattern.h, GFX10_SW_64K_R_X_1xaa_RBPLUS_PATINFO, +// 4 bytes/element. These are the contributions of X0..X7 and Y0..Y7 to +// the low 16 address bits. Z, sample, and coordinates above 255 are zero. +const fn render_axis(bits: [u16; 8]) -> [u16; 256] { + let mut offsets = [0; 256]; + let mut coord = 1usize; + while coord < 256 { + offsets[coord] = offsets[coord & (coord - 1)] ^ bits[coord.trailing_zeros() as usize]; + coord += 1; + } + offsets +} + +impl RenderPipeConfig { + fn offset(self, x: usize, y: usize) -> usize { + static S: [[u16; 256]; 2] = [ + render_axis([4, 8, 0x40, 0x800, 0x200, 0x2400, 0x4100, 0x8000]), + render_axis([0x10, 0x20, 0x80, 0x1000, 0x300, 0x400, 0x8100, 0x4000]), + ]; + static X: [[u16; 256]; 2] = [ + render_axis([4, 8, 0x40, 0x2000, 0x200, 0x800, 0x1400, 0x8100]), + render_axis([0x10, 0x20, 0x80, 0x4000, 0x300, 0x400, 0x8800, 0x100]), + ]; + let axes = match self { + Self::Pipes8Packers4 => &S, + Self::Pipes32Packers8 => &X, + }; + let block = (y / 128) * 2 + x / 128; + block * 65536 + usize::from(axes[0][x] ^ axes[1][y]) + } } type SwizzlePattern = [Vec; 3]; @@ -344,6 +387,7 @@ impl Layout { first_tail_mip: d.mips, mips, pattern: &EMPTY_PATTERN, + render_xor: None, } } @@ -526,18 +570,34 @@ impl Layout { first_tail_mip, mips, pattern, + render_xor: None, + } + } + + /// Enables addressing for a single 256×256 R32 plane in a block-aligned + /// allocation. The caller supplies the console's pipe configuration. + pub fn configure_render_xor(&mut self, config: RenderPipeConfig) { + let d = &self.desc; + if d.dimension == Dimension::Tex2d && d.swizzle == Swizzle::SW_64KB_R_X + && (d.width, d.height, d.depth, d.mips) == (256, 256, 1, 1) + && (d.element_bytes, d.block, d.samples) == (4, 1, 1) + && self.block == [128, 128, 1] && self.size == 256 * 256 * 4 + { + self.render_xor = Some(config); } } /// Whether [`Layout::offset`] and the copies work for this layout: - /// linear and the modes without pipe or bank XOR, single-sampled. + /// linear, the modes without pipe or bank XOR (single-sampled), and the + /// one XOR layout [`Layout::configure_render_xor`] takes. /// - /// shortcut: no addressing for the XOR modes (`_X`, `_T`) and MSAA; - /// they need the GPU's pipe and bank configuration (Series X and S - /// differ). Titles use them for render targets and depth, which the - /// host renders; add them when a title copies into or out of one. + /// shortcut: no addressing for the other XOR modes (`_X`, `_T`) and + /// MSAA; they need the GPU's pipe and bank configuration (Series X and + /// S differ). Titles use them for render targets and depth, which the + /// host renders; add them when a title copies into or out of one, as + /// the ocean's CPU code reads its height map. pub fn addressable(&self) -> bool { - self.desc.swizzle == Swizzle::LINEAR || !self.pattern[0].is_empty() + self.desc.swizzle == Swizzle::LINEAR || !self.pattern[0].is_empty() || self.render_xor.is_some() } /// The slice and the depth inside it for `z` (an array slice or a 3D @@ -567,6 +627,9 @@ impl Layout { Error::Invalid }); } + if let Some(config) = self.render_xor { + return Ok(config.offset(x as usize, y as usize) as u64); + } let (slice, z) = self.split(z); let base = u64::from(slice) * self.slice_size + m.offset; if self.desc.swizzle == Swizzle::LINEAR { @@ -710,6 +773,15 @@ impl Layout { if tiled_len < tiled_need as usize || linear_len < need || row_pitch < bytes { return Err(Error::TooSmall); } + if let Some(config) = self.render_xor { + for y in 0..256 { + // X0 and X1 select adjacent dwords in both equations. + for x in (0..256).step_by(4) { + copy_run::(src, dst, config.offset(x, y), y * row_pitch + 4 * x, 16); + } + } + return Ok(()); + } let linear = self.desc.swizzle == Swizzle::LINEAR; // The row and depth contribute one common base to every column, so // the same contiguous runs repeat on every row, including mip tails. @@ -937,6 +1009,44 @@ impl Texture { mod tests { use super::*; + #[test] + fn render_xor_water_matches_both_consoles_with_padded_rows() { + // Golden FNV-1a digests of every tiled byte from XCL 85e3826's + // independent GFX10 tiler. Each texel stores its own linear index. + let desc = Desc { dimension: Dimension::Tex2d, width: 256, height: 256, + depth: 1, mips: 1, element_bytes: 4, block: 1, samples: 1, + swizzle: Swizzle::SW_64KB_R_X }; + let pitch = 1040; + let mut linear = vec![0xfe; 255 * pitch + 1024]; + for y in 0..256usize { + for x in 0..256usize { + let at = y * pitch + 4 * x; + linear[at..at + 4].copy_from_slice(&((y * 256 + x) as u32).to_le_bytes()); + } + } + for (cfg, expected) in [(RenderPipeConfig::Pipes8Packers4, 0xc0e94c4e1b16af25), + (RenderPipeConfig::Pipes32Packers8, 0x49527ce701b6e325)] { + let mut layout = Layout::new(&desc).unwrap(); + assert!(!layout.addressable()); + layout.configure_render_xor(cfg); + let mut tiled = vec![0xbc; layout.size as usize]; + layout.tile(&mut tiled, 0, 0, &linear, pitch, 256 * pitch).unwrap(); + let digest = tiled.iter().fold(0xcbf29ce484222325u64, |h, &b| { + (h ^ u64::from(b)).wrapping_mul(0x100000001b3) + }); + assert_eq!(digest, expected); + for y in 0..256u32 { + for x in 0..256u32 { + let at = layout.offset(0, x, y, 0).unwrap() as usize; + assert_eq!(&tiled[at..at + 4], &(y * 256 + x).to_le_bytes()); + } + } + let mut round_trip = vec![0xfe; linear.len()]; + layout.detile(&tiled, 0, 0, &mut round_trip, pitch, 256 * pitch).unwrap(); + assert_eq!(round_trip, linear); + } + } + fn desc(w: u32, h: u32, mips: u32, bpe: u32, swizzle: Swizzle) -> Desc { Desc { dimension: Dimension::Tex2d, diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 597ede594..306b8a9d7 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -3533,7 +3533,7 @@ pub(crate) fn texture_kind(this: This) -> Option<(u64, u32, u32)> { /// Texture `this`'s console layout (one plane), where it lies in title /// memory and the bytes that layout takes, for `crate::texback`; `None` for -/// a buffer or a texture with planes. +/// buffers, multi-plane textures, and layouts we cannot address. #[cfg(windows)] pub(crate) fn title_texture(this: This) -> Option<(victory_formats::texture::Layout, usize, usize)> { if this.is_null() { @@ -3544,10 +3544,27 @@ pub(crate) fn title_texture(this: This) -> Option<(victory_formats::texture::Lay return None; } let t = texture_layout(&r.desc)?; - let [plane] = &t.planes[..] else { + let [mut plane]: [_; 1] = t.planes.try_into().ok()?; + let at = r.data() as usize; + const R32_FLOAT: u32 = 41; + if r.desc.format == R32_FLOAT && at.is_multiple_of(65536) { + use victory_formats::texture::RenderPipeConfig; + let pipes = match crate::console::current() { + crate::console::Console::SeriesS => RenderPipeConfig::Pipes8Packers4, + crate::console::Console::SeriesX => RenderPipeConfig::Pipes32Packers8, + }; + plane.layout.configure_render_xor(pipes); + } + if !plane.layout.addressable() { + // Recording and waiting for a GPU copy cannot produce a CPU-visible + // plane if the console's address equation is unavailable. + static ONCE: std::sync::Once = std::sync::Once::new(); + ONCE.call_once(|| tracing::warn!(width = r.desc.width, height = r.desc.height, + format = r.desc.format, swizzle = plane.layout.desc.swizzle.0, + "a texture of a kind copied back (VICTORY_TEXTURE_BACK) is left alone: its console layout is not addressed here")); return None; - }; - Some((plane.layout.clone(), r.data() as usize, t.size as usize)) + } + Some((plane.layout, at, t.size as usize)) } /// The bytes of title memory a texture with the resource description @@ -3987,6 +4004,24 @@ mod tests { } } + #[test] + #[cfg(windows)] + fn water_readback_requires_an_addressable_console_plane() { + for (swizzle, bias, supported) in [(0x105, 0, true), (0x11b, 0, true), + (0x11b, 256, false), (0x119, 0, false)] { + let desc = ResourceDesc { layout: swizzle, flags: 4, ..texture(256, 256, 41, 1) }; + let bytes = footprint(&desc).unwrap().size as usize; + let memory = vec![0u8; bytes + 65536 + bias]; + let at = (memory.as_ptr() as usize).next_multiple_of(65536) + bias; + let mut r = std::ptr::null(); + assert_eq!(create_at_address(std::ptr::null(), at as u64, &desc, 0, + std::ptr::null(), &PAGEABLE_IIDS[0], &mut r), object::S_OK); + let destination = title_texture(r).map(|(_, base, extent)| (base, extent)); + assert_eq!(destination, supported.then_some((at, bytes))); + assert_eq!(resource_release(r), 0); + } + } + fn heap(size: u64) -> This { let desc = HeapDesc { size, From 7f6e2aa47e729b09664367b760b7cff95a843c71 Mon Sep 17 00:00:00 2001 From: "Manley.B" Date: Sat, 28 Nov 2026 20:07:14 +1100 Subject: [PATCH 2/2] Texture read-back: the layouts still not addressed are left to #881 #881 skips the copy back of a texture whose layout the tiler cannot address, at the list, with a test on WARP. This change also returned no target for such a texture: the same skip twice, and #881 came first. So title_texture only sets the XOR addressing now, and its test checks what it gives: the layout addressed or not, at the right place and size. --- crates/victory-gpu/src/resource.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 306b8a9d7..a0bb30f9c 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -3533,7 +3533,10 @@ pub(crate) fn texture_kind(this: This) -> Option<(u64, u32, u32)> { /// Texture `this`'s console layout (one plane), where it lies in title /// memory and the bytes that layout takes, for `crate::texback`; `None` for -/// buffers, multi-plane textures, and layouts we cannot address. +/// a buffer or a texture with planes. An `R32_FLOAT` texture at a 64 KiB +/// boundary in `SW_64KB_R_X` (the ocean's height map) is addressed with the +/// console's pipes and packers +/// ([`victory_formats::texture::Layout::configure_render_xor`]). #[cfg(windows)] pub(crate) fn title_texture(this: This) -> Option<(victory_formats::texture::Layout, usize, usize)> { if this.is_null() { @@ -3555,15 +3558,6 @@ pub(crate) fn title_texture(this: This) -> Option<(victory_formats::texture::Lay }; plane.layout.configure_render_xor(pipes); } - if !plane.layout.addressable() { - // Recording and waiting for a GPU copy cannot produce a CPU-visible - // plane if the console's address equation is unavailable. - static ONCE: std::sync::Once = std::sync::Once::new(); - ONCE.call_once(|| tracing::warn!(width = r.desc.width, height = r.desc.height, - format = r.desc.format, swizzle = plane.layout.desc.swizzle.0, - "a texture of a kind copied back (VICTORY_TEXTURE_BACK) is left alone: its console layout is not addressed here")); - return None; - } Some((plane.layout, at, t.size as usize)) } @@ -4006,8 +4000,10 @@ mod tests { #[test] #[cfg(windows)] - fn water_readback_requires_an_addressable_console_plane() { - for (swizzle, bias, supported) in [(0x105, 0, true), (0x11b, 0, true), + fn the_ocean_height_map_layout_is_addressed_at_a_64_kib_boundary() { + // SW_4KB_S always; SW_64KB_R_X only at a 64 KiB boundary; SW_64KB_S_X + // not (no XOR mode but that one has addresses yet). + for (swizzle, bias, addressable) in [(0x105, 0, true), (0x11b, 0, true), (0x11b, 256, false), (0x119, 0, false)] { let desc = ResourceDesc { layout: swizzle, flags: 4, ..texture(256, 256, 41, 1) }; let bytes = footprint(&desc).unwrap().size as usize; @@ -4016,8 +4012,8 @@ mod tests { let mut r = std::ptr::null(); assert_eq!(create_at_address(std::ptr::null(), at as u64, &desc, 0, std::ptr::null(), &PAGEABLE_IIDS[0], &mut r), object::S_OK); - let destination = title_texture(r).map(|(_, base, extent)| (base, extent)); - assert_eq!(destination, supported.then_some((at, bytes))); + let destination = title_texture(r).map(|(layout, base, extent)| (layout.addressable(), base, extent)); + assert_eq!(destination, Some((addressable, at, bytes)), "layout {swizzle:#x}, {bias} bytes off"); assert_eq!(resource_release(r), 0); } }