diff --git a/HANDOFF.md b/HANDOFF.md index d651ffa..e40ee1d 100644 --- a/HANDOFF.md +++ b/HANDOFF.md @@ -536,6 +536,25 @@ Nic otwartego. Niewyjaśnione: 6 pakietów na podblok przy różnicy w tablicy 12·960 – TOC 0xFC to CELT FB 20 ms **stereo**, więc tablica liczy chyba próbki × kanały. +## Tekstury kafelkowane z pamięci konsoli (sesja 5, 2026-11-21) +- **Łańcuch mipów GFX10** (`tiling::Chain`, `tiling::image_chain(&desc::Image, cfg)`; addrlib + Gfx10Lib::ComputeSurfaceInfoMacroTiled/MicroTiled): na slice najpierw blok mip tail (poziomy ≤ pół bloku szerokości, + najwyżej log2(blok) − 4 poziomów; bloki 64 KB / 4 KB), potem poziomy od najmniejszego do mip 0, slice'y co + `slice_bytes`. Poziom w tail: origin w bloku z jego offsetu (bity nieparzyste → x, parzyste → y, ×blok 256 B). + `detile_level` / `tile_level`. Rozmiary poziomów = piksele >> i zaokrąglone w górę do elementów (jak D3D12). +- **`gpu/texture.rs`**: `placement(T#)` (adres wyrównany do bloku, bajty, łańcuch, slice'y), `subresources(T#, pamięć)` + (kolejność D3D12: mip + slice × mips), `uma_bytes(T#)`, `create(dev, queue, T#, pamięć)` → tekstura PC (stan SRV), + `upload(...)` – ponowne wgranie, gdy tytuł przepisze pamięć. `CONSOLE_PIPES` – konfiguracja pipe/packer konsoli dla + trybów _X, nadal nieznana (archiwa używają trybów bez XOR). +- **Zweryfikowane na TTPF** (`XCL_SAMPLE_TTPF`, test `retail_ttpf_textures_detile_with_their_mip_chains`): model daje + dokładnie miejsce, które zajmuje 908 z 912 tekstur (odstają 2 cube'y w trybie 2, 1 wolumen, ostatnia ucięta). Liczba + mipów = `dword0 >> 28` (0 → 1), slice'y = `(dword1 >> 10) & 0x7FF` + 1. Mipy tekstur R8/RGBA8/RGBA16 ≈ poprzedni + poziom pomniejszony (6/6); 15 poziomów w mip tail: błąd 0,66/255, w złym miejscu bloku 150/255. +- Test GPU `gpu_console_textures_upload_every_mip_and_slice`: kafelkowana pamięć → tekstura PC → odczyt = dane, bit w + bit, każdy mip i slice (tablica RGBA8 4 KB, cube BC7 64 KB). +- **Do zrobienia z exe:** wiązanie z API, którym tytuł tworzy tekstury w swojej pamięci (CreatePlacedResourceX / sterta + pod adresem), i moment ponownego wgrania (zapis CPU / kopia GPU); tekstury liniowe, 3D (thick), MSAA. + ## Samplery statyczne (sesja 5, 2026-11-21) - **Luka:** tłumacz brał indeks samplera PC zawsze z S# słowo 3 [11:0], a S# zbudowany ze stałych w kodzie (statyczne samplery root signature konsoli) ma tam co innego → zły sampler / odczyt poza stertą. W PS_PedTextured_Eight_Fire 66 @@ -677,8 +696,8 @@ Bez exe zostało niewiele – reszta potrzebuje prawdziwego wywołania gry. 2. Wiązanie przy rysowaniu: argumenty root gry → SGPR w `io`, `mem` → bufor UMA; przechwytywanie list poleceń dla GS i dispatchy Compute; `XboxBvhBuilder` (lub `BuildRaytracingAccelerationStructure`) → `gpu::bvh::build/build_top` (geometria z buforów GPU w UMA; Rehydrate = gotowe BVH z `*_bvh4.rpf` – format do sprawdzenia). -3. Tekstury: konfiguracja pipe/packer konsoli dla trybów _X (`tiling::PipeConfig`), untiling przy tworzeniu zasobów - z pamięci konsoli, układ łańcucha mipów GFX10. +3. Tekstury: wiązanie `gpu::texture` z API tytułu (tekstury w jego pamięci, ponowne wgranie po zapisie), konfiguracja + pipe/packer konsoli dla trybów _X (`texture::CONSOLE_PIPES`), tekstury liniowe / 3D. 4. Audio: Opus (MFT wyżej) dla głosów XAudio2 0x704F lub API konsoli – zależnie od tego, co pokaże exe; XMA2 niski priorytet. 5. Mniejsze: 749 programów wave64 z ryzykiem w fali 32 (dopiero jeśli się objawią), `image_sample_o` z offsetem z VGPR, mniejszy HLSL, kolejne API XGameRuntime (XGameUI, XNetworking…), GameInput → systemowe GameInput.dll, diff --git a/crates/xcl/src/gpu/mod.rs b/crates/xcl/src/gpu/mod.rs index 9e90925..a83d41f 100644 --- a/crates/xcl/src/gpu/mod.rs +++ b/crates/xcl/src/gpu/mod.rs @@ -10,6 +10,7 @@ pub mod recomp; mod rdna2_spec; mod rdna2_xbox; pub mod shader; +pub mod texture; pub mod tiling; mod tiling_tables; pub mod uma; diff --git a/crates/xcl/src/gpu/texture.rs b/crates/xcl/src/gpu/texture.rs new file mode 100644 index 0000000..d5c2055 --- /dev/null +++ b/crates/xcl/src/gpu/texture.rs @@ -0,0 +1,539 @@ +//! Console textures from console memory → PC textures. On the console a texture is the +//! memory a T# points at: the title writes it already tiled (GFX10 swizzle, mip chain and +//! slices as addrlib lays them out – its archives store textures that way) and samples it +//! directly. A PC texture has an opaque layout, so every mip level of every slice is detiled +//! (`tiling::Chain`) into a D3D12 subresource and copied in. +//! +//! What is not here yet: linear (SW_MODE 0) and 3D (thick) textures, MSAA, the console's pipe +//! configuration for the _X modes (`CONSOLE_PIPES`), and the binding to the console API that +//! creates textures in title memory (and tells when the title rewrote one) – that comes with +//! the game executable. + +use super::desc::{self, Image, ImageType}; +use super::tiling::{self, Chain, PipeConfig}; +use windows::Win32::Graphics::Direct3D12::*; + +/// Pipes / packers of the console's GB_ADDR_CONFIG – only the XOR (_X) modes depend on it. +/// Not known yet (to be found on an _X texture from the game); the non-XOR modes the +/// archives use do not need it. +pub const CONSOLE_PIPES: PipeConfig = PipeConfig { pipes_log2: 0, pkrs_log2: 0 }; + +/// Linear data of one subresource: `rows` rows of `row_bytes` (elements: pixels or 4×4 blocks). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Subresource { + pub rows: usize, + pub row_bytes: usize, + pub data: Vec, +} + +/// Where and how a T#'s texture lies in console memory. +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: Chain, + pub slices: usize, +} + +pub fn placement(t: &Image, cfg: PipeConfig) -> Result { + if matches!(t.ty, ImageType::D3 | ImageType::D2Msaa | ImageType::D2MsaaArray) { + return Err(format!("tekstura {:?} – jeszcze nie (tylko 1D/2D, tablice, cube)", t.ty)); + } + if t.swizzle_mode == 0 { + return Err("tekstura liniowa (SW_MODE 0) – jeszcze nie".into()); + } + let chain = tiling::image_chain(t, cfg).ok_or_else(|| format!("SW_MODE {} / format {} bez wzorca kafelkowania", t.swizzle_mode, t.format))?; + let slices = match t.ty { + ImageType::D1Array | ImageType::D2Array | ImageType::Cube => t.depth.max(1) as usize, + _ => 1, + }; + let start = t.address & !(chain.layout.block_bytes as u64 - 1); + Ok(Placement { start, bytes: chain.slice_bytes * slices, chain, slices }) +} + +/// The subresources of `t` in D3D12 order (mip + slice × mips) from `mem`, its bytes from +/// `Placement::start` on. +pub fn subresources(t: &Image, mem: &[u8], cfg: PipeConfig) -> Result, String> { + let p = placement(t, cfg)?; + if mem.len() < p.bytes { + return Err(format!("{} B pamięci, a tekstura zajmuje {} B", mem.len(), p.bytes)); + } + let e = 1usize << p.chain.layout.elem_log2; + let mut out = Vec::with_capacity(p.slices * p.chain.levels.len()); + for s in 0..p.slices { + let slice = &mem[s * p.chain.slice_bytes..(s + 1) * p.chain.slice_bytes]; + for (i, l) in p.chain.levels.iter().enumerate() { + out.push(Subresource { rows: l.height, row_bytes: l.width * e, data: p.chain.detile_level(slice, i) }); + } + } + Ok(out) +} + +/// The console memory a T#'s texture occupies, when it lies in the unified memory (UMA). +pub fn uma_bytes(t: &Image, cfg: PipeConfig) -> Result<&'static [u8], String> { + let p = placement(t, cfg)?; + let u = super::uma::get().ok_or("brak UMA")?; + let (a, n) = (p.start as usize, p.bytes); + if !u.contains(a) || !u.contains(a + n - 1) { + return Err(format!("tekstura {a:#x}+{n:#x} poza UMA")); + } + // SAFETY: inside the UMA reservation, which lives for the whole process + Ok(unsafe { std::slice::from_raw_parts(a as *const u8, n) }) +} + +fn err(x: windows::core::Error) -> String { + format!("{x}") +} + +/// A PC texture (default heap, left in the shader-resource states) with the console +/// texture's contents: `mem` from `Placement::start` on. +pub unsafe fn create(dev: &ID3D12Device, queue: &ID3D12CommandQueue, t: &Image, mem: &[u8], cfg: PipeConfig) -> Result { + let subs = subresources(t, mem, cfg)?; + let (rd, _) = desc::image_d3d12(t).ok_or_else(|| format!("format {} bez odpowiednika DXGI", t.format))?; + let mut r: Option = None; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &rd, D3D12_RESOURCE_STATE_COPY_DEST, None, &mut r) + .map_err(|x| format!("CreateCommittedResource: {x}"))?; + let r = r.ok_or("CreateCommittedResource bez zasobu")?; + upload(dev, queue, &r, &subs, D3D12_RESOURCE_STATE_COPY_DEST, SHADER_READ)?; + Ok(r) +} + +/// The states `create` leaves a texture in. +pub const SHADER_READ: D3D12_RESOURCE_STATES = D3D12_RESOURCE_STATES(D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE.0 | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE.0); + +/// Copy `subs` (all subresources, D3D12 order) into `res` on `queue` and wait; `res` is in +/// `before` and ends in `after` (e.g. again after the title rewrote the memory). +pub unsafe fn upload(dev: &ID3D12Device, queue: &ID3D12CommandQueue, res: &ID3D12Resource, subs: &[Subresource], before: D3D12_RESOURCE_STATES, after: D3D12_RESOURCE_STATES) -> Result<(), String> { + let rd = res.GetDesc(); + let n = subs.len(); + let mut fps = vec![D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); n]; + let mut rows = vec![0u32; n]; + let mut total = 0u64; + dev.GetCopyableFootprints(&rd, 0, n as u32, 0, Some(fps.as_mut_ptr()), Some(rows.as_mut_ptr()), None, Some(&mut total)); + let up = buffer(dev, total)?; + let mut p = std::ptr::null_mut(); + up.Map(0, None, Some(&mut p)).map_err(err)?; + for ((s, fp), &nrows) in subs.iter().zip(&fps).zip(&rows) { + let pitch = fp.Footprint.RowPitch as usize; + let row = s.row_bytes.min(pitch); + for y in 0..(nrows as usize).min(s.rows) { + std::ptr::copy_nonoverlapping(s.data[y * s.row_bytes..].as_ptr(), (p as *mut u8).add(fp.Offset as usize + y * pitch), row); + } + } + up.Unmap(0, None); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).map_err(err)?; + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).map_err(err)?; + if before != D3D12_RESOURCE_STATE_COPY_DEST { + list.ResourceBarrier(&[transition(res, before, D3D12_RESOURCE_STATE_COPY_DEST)]); + } + for (k, fp) in fps.iter().enumerate() { + let dst = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(res), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: k as u32 }, + }; + let src = D3D12_TEXTURE_COPY_LOCATION { pResource: std::mem::transmute_copy(&up), Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: *fp } }; + list.CopyTextureRegion(&dst, 0, 0, 0, &src, None); + } + if after != D3D12_RESOURCE_STATE_COPY_DEST { + list.ResourceBarrier(&[transition(res, D3D12_RESOURCE_STATE_COPY_DEST, after)]); + } + list.Close().map_err(err)?; + queue.ExecuteCommandLists(&[Some(windows::core::Interface::cast(&list).map_err(err)?)]); + wait(dev, queue) +} + +/// Wait until `queue` has run everything submitted so far. +pub unsafe fn wait(dev: &ID3D12Device, queue: &ID3D12CommandQueue) -> Result<(), String> { + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).map_err(err)?; + queue.Signal(&fence, 1).map_err(err)?; + let ev = windows::Win32::System::Threading::CreateEventW(None, false, false, None).map_err(err)?; + fence.SetEventOnCompletion(1, ev).map_err(err)?; + windows::Win32::System::Threading::WaitForSingleObject(ev, 10_000); + let _ = windows::Win32::Foundation::CloseHandle(ev); + if fence.GetCompletedValue() < 1 { + return Err(format!("GPU nie skończył kopiowania tekstury w 10 s ({:?})", dev.GetDeviceRemovedReason())); + } + Ok(()) +} + +unsafe fn buffer(dev: &ID3D12Device, size: u64) -> Result { + let rd = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, + Width: size.max(256), + Height: 1, + DepthOrArraySize: 1, + MipLevels: 1, + SampleDesc: windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + ..Default::default() + }; + let mut r: Option = None; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_UPLOAD, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &rd, D3D12_RESOURCE_STATE_GENERIC_READ, None, &mut r) + .map_err(|x| format!("bufor wysyłania tekstury: {x}"))?; + r.ok_or_else(|| "bufor wysyłania tekstury".into()) +} + +fn transition(r: &ID3D12Resource, before: D3D12_RESOURCE_STATES, after: D3D12_RESOURCE_STATES) -> D3D12_RESOURCE_BARRIER { + D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: std::mem::ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: unsafe { std::mem::transmute_copy(r) }, + Subresource: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + StateBefore: before, + StateAfter: after, + }), + }, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashMap; + use windows::Win32::Graphics::Dxgi::Common::*; + + fn image(format: u32, ty: ImageType, w: u32, h: u32, slices: u32, mips: u32, mode: u32, address: u64) -> Image { + Image { + address, + format, + width: w, + height: h, + depth: slices, + ty, + base_level: 0, + last_level: mips - 1, + max_mip: mips - 1, + base_array: 0, + dst_sel: [4, 5, 6, 7], + swizzle_mode: mode, + min_lod: 0.0, + } + } + + /// Random linear subresources for `t`, and the console memory they are, tiled. + fn console_texture(t: &Image) -> (Vec, Vec) { + let p = placement(t, CONSOLE_PIPES).unwrap(); + let e = 1usize << p.chain.layout.elem_log2; + let mut mem = vec![0u8; p.bytes]; + let mut subs = Vec::new(); + let mut n = 0x1234_5678u32; + for s in 0..p.slices { + let slice = &mut mem[s * p.chain.slice_bytes..(s + 1) * p.chain.slice_bytes]; + for (i, l) in p.chain.levels.iter().enumerate() { + let data: Vec = (0..l.width * l.height * e) + .map(|_| { + n ^= n << 13; + n ^= n >> 17; + n ^= n << 5; + n as u8 + }) + .collect(); + p.chain.tile_level(slice, i, &data); + subs.push(Subresource { rows: l.height, row_bytes: l.width * e, data }); + } + } + (subs, mem) + } + + #[test] + fn subresources_of_a_mipped_array_from_console_memory() { + let rgba8 = desc::gfx10_format(DXGI_FORMAT_R8G8B8A8_UNORM).unwrap(); + let t = image(rgba8, ImageType::D2Array, 96, 40, 3, 6, 5, 0x10_0000 | 0x300); + let (want, mem) = console_texture(&t); + let p = placement(&t, CONSOLE_PIPES).unwrap(); + assert_eq!((p.start, p.slices, want.len()), (0x10_0000, 3, 18)); + assert_eq!(subresources(&t, &mem, CONSOLE_PIPES).unwrap(), want); + assert!(subresources(&t, &mem[..mem.len() - 1], CONSOLE_PIPES).is_err()); + } + + fn device() -> Option<(ID3D12Device, ID3D12CommandQueue)> { + unsafe { + let mut dev: Option = None; + windows::Win32::Graphics::Direct3D12::D3D12CreateDevice(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0, &mut dev).ok()?; + let dev = dev?; + let queue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).ok()?; + Some((dev, queue)) + } + } + + /// Every subresource of a PC texture, read back (rows of `row_bytes`). + unsafe fn read_back(dev: &ID3D12Device, queue: &ID3D12CommandQueue, res: &ID3D12Resource, subs: &[Subresource]) -> Vec { + let rd = res.GetDesc(); + let n = subs.len(); + let mut fps = vec![D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); n]; + let mut total = 0u64; + dev.GetCopyableFootprints(&rd, 0, n as u32, 0, Some(fps.as_mut_ptr()), None, None, Some(&mut total)); + let mut rb: Option = None; + let bd = D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, Width: total, Height: 1, DepthOrArraySize: 1, MipLevels: 1, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, ..Default::default() }; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &bd, D3D12_RESOURCE_STATE_COPY_DEST, None, &mut rb).unwrap(); + let rb = rb.unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); + list.ResourceBarrier(&[transition(res, SHADER_READ, D3D12_RESOURCE_STATE_COPY_SOURCE)]); + for (k, fp) in fps.iter().enumerate() { + let src = D3D12_TEXTURE_COPY_LOCATION { pResource: std::mem::transmute_copy(res), Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: k as u32 } }; + let dst = D3D12_TEXTURE_COPY_LOCATION { pResource: std::mem::transmute_copy(&rb), Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: *fp } }; + list.CopyTextureRegion(&dst, 0, 0, 0, &src, None); + } + list.ResourceBarrier(&[transition(res, D3D12_RESOURCE_STATE_COPY_SOURCE, SHADER_READ)]); + list.Close().unwrap(); + queue.ExecuteCommandLists(&[Some(windows::core::Interface::cast(&list).unwrap())]); + wait(dev, queue).unwrap(); + let mut p = std::ptr::null_mut(); + rb.Map(0, None, Some(&mut p)).unwrap(); + let out = subs + .iter() + .zip(&fps) + .map(|(s, fp)| { + let mut data = Vec::with_capacity(s.rows * s.row_bytes); + for y in 0..s.rows { + data.extend_from_slice(std::slice::from_raw_parts((p as *const u8).add(fp.Offset as usize + y * fp.Footprint.RowPitch as usize), s.row_bytes)); + } + Subresource { rows: s.rows, row_bytes: s.row_bytes, data } + }) + .collect(); + rb.Unmap(0, None); + out + } + + /// Tiled console memory → PC texture → read back = the linear data it was tiled from, for + /// every mip and slice: an RGBA8 array with 4 KB tiles, a BC7 cube with 64 KB tiles. + #[test] + fn gpu_console_textures_upload_every_mip_and_slice() { + let Some((dev, queue)) = device() else { + println!("brak urządzenia D3D12 – pomijam"); + return; + }; + let rgba8 = desc::gfx10_format(DXGI_FORMAT_R8G8B8A8_UNORM).unwrap(); + let bc7 = desc::gfx10_format(DXGI_FORMAT_BC7_UNORM).unwrap(); + for t in [image(rgba8, ImageType::D2Array, 200, 72, 3, 8, 5, 0x40_0000), image(bc7, ImageType::Cube, 256, 256, 6, 9, 10, 0x80_0000)] { + let (want, mem) = console_texture(&t); + unsafe { + let res = create(&dev, &queue, &t, &mem, CONSOLE_PIPES).unwrap(); + let got = read_back(&dev, &queue, &res, &want); + for (k, (g, w)) in got.iter().zip(&want).enumerate() { + assert!(g == w, "format {} subzasób {k} ({} wierszy × {} B) różni się", t.format, w.rows, w.row_bytes); + } + } + } + } + + /// Mean |level `k` - level `k - 1` scaled down 2×| per channel (0..255 scale) of a texture + /// with `ch` channels of `cb` bytes (unorm); None when the sizes do not halve. + fn mip_error(big: &Subresource, small: &Subresource, ch: usize, cb: usize) -> Option { + let e = ch * cb; + let (bw, bh, sw, sh) = (big.row_bytes / e, big.rows, small.row_bytes / e, small.rows); + if sw * 2 != bw || sh * 2 != bh { + return None; + } + let get = |d: &[u8], w: usize, x: usize, y: usize, c: usize| -> u32 { + let o = (y * w + x) * e + c * cb; + if cb == 1 { + d[o] as u32 + } else { + u16::from_le_bytes([d[o], d[o + 1]]) as u32 >> 8 + } + }; + let mut sum = 0u64; + for y in 0..sh { + for x in 0..sw { + for c in 0..ch { + let avg = (get(&big.data, bw, 2 * x, 2 * y, c) + get(&big.data, bw, 2 * x + 1, 2 * y, c) + get(&big.data, bw, 2 * x, 2 * y + 1, c) + get(&big.data, bw, 2 * x + 1, 2 * y + 1, c) + 2) / 4; + sum += (avg as i32 - get(&small.data, sw, x, y, c) as i32).unsigned_abs() as u64; + } + } + } + Some(sum as f64 / (sw * sh * ch) as f64) + } + + /// The retail TTPF archive (`XCL_SAMPLE_TTPF`): (1) the mip chain model gives the space + /// every texture takes; (2) on its uncompressed unorm textures each detiled mip level is + /// close to the level above it scaled down 2× (mip tail levels included), while the same + /// tail levels read from a wrong place in the tail block (origin 0, 0) are not. + #[test] + fn retail_ttpf_textures_detile_with_their_mip_chains() { + let Ok(path) = std::env::var("XCL_SAMPLE_TTPF") else { + println!("XCL_SAMPLE_TTPF nie ustawione – pomijam"); + return; + }; + let a = Ttpf::open(&path); + let (mut fit, mut sized, mut unreadable) = (0, 0, 0); + let rgba8 = desc::gfx10_format(DXGI_FORMAT_R8G8B8A8_UNORM).unwrap(); + let (mut checked, mut close, mut worst) = (0, 0, Vec::new()); + let (mut tail_levels, mut tail_err, mut wrong_err) = (0, 0.0f64, 0.0f64); + for t in &a.textures { + let Some((e, side, _)) = ttpf_element(t.dxgi) else { continue }; + let gfx10 = match (e, side) { + (0, 1) => 1, + (1, 1) => 7, + (2, 1) => rgba8, + (3, 1) => 62, + (3, 4) => 169, + _ => 171, + }; + let ty = if t.slices > 1 { ImageType::D2Array } else { ImageType::D2 }; + let img = image(gfx10, ty, t.w, t.h, t.slices, t.mips, t.mode, 0); + let p = placement(&img, CONSOLE_PIPES).unwrap(); + sized += 1; + if p.bytes == t.end - t.start || p.bytes.div_ceil(4096) * 4096 == t.end - t.start { + fit += 1; + } + // unorm channels: R8 / A8, RGBA8 / BGRA8, RGBA16 + let (ch, cb) = match t.dxgi { + 61 | 65 => (1, 1), + 27..=29 | 87 | 90 | 91 => (4, 1), + 11 => (4, 2), + _ => continue, + }; + if t.mips < 2 || t.slices != 1 { + continue; + } + let Some(mem) = a.read(t.start, t.start + p.bytes) else { + unreadable += 1; + continue; + }; + let subs = subresources(&img, &mem, CONSOLE_PIPES).unwrap(); + let mut bad = 0.0f64; + for k in 1..subs.len() { + 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 l = p.chain.levels[k]; + if l.origin != (0, 0) && l.width >= 2 && l.height >= 2 { + let mut wrong = p.chain.clone(); + wrong.levels[k].origin = (0, 0); + let ws = Subresource { rows: l.height, row_bytes: subs[k].row_bytes, data: wrong.detile_level(&mem, k) }; + tail_levels += 1; + tail_err += err; + wrong_err += mip_error(&subs[k - 1], &ws, ch, cb).unwrap(); + } + } + checked += 1; + if bad < 8.0 { + close += 1; + } else if worst.len() < 8 { + worst.push((t.index, t.w, t.h, t.mips, t.mode, bad)); + } + } + let n = tail_levels.max(1) as f64; + println!("TTPF: rozmiar z modelu łańcucha mipów pasuje dla {fit} z {sized} tekstur"); + println!("TTPF: tekstury z mipami (R8/RGBA8/RGBA16): {close} z {checked} – każdy mip bliski poprzedniego pomniejszonego (< 8/255), {unreadable} w nieczytelnych blokach; odstające: {worst:?}"); + println!("TTPF: {tail_levels} poziomów w mip tail – błąd {:.2}/255, przy złym miejscu w bloku {:.2}/255", tail_err / n, wrong_err / n); + assert!(fit * 100 >= sized * 99, "model łańcucha mipów: {fit} / {sized}"); + assert!(checked >= 5 && close * 100 >= checked * 90, "mipy: {close} / {checked}"); + assert!(tail_levels >= 5 && wrong_err > 3.0 * tail_err, "mip tail: {tail_err} vs {wrong_err}"); + } + + /// (element log2, pixels per side, uncompressed) of the DXGI formats the archive uses + fn ttpf_element(dxgi: u32) -> Option<(u32, u32, bool)> { + Some(match dxgi { + 70..=72 | 79..=81 => (3, 4, false), + 73..=78 | 82..=84 | 94..=99 => (4, 4, false), + 10 | 11 => (3, 1, true), + 27..=29 | 87 | 90 | 91 => (2, 1, true), + 54 => (1, 1, true), + 61 | 65 => (0, 1, true), + _ => return None, + }) + } + + struct TtpfTexture { + index: usize, + w: u32, + h: u32, + dxgi: u32, + mode: u32, + mips: u32, + slices: u32, + start: usize, + end: usize, + } + + /// Just enough of a .ttpf archive (see tools/ttpf-viewer/src/archive.rs): its zlib blocks + /// of one unpacked stream and the texture table. + struct Ttpf { + d: Vec, + /// (file offset, position in the stream), by position + blocks: Vec<(usize, usize)>, + textures: Vec, + cache: std::cell::RefCell>>>>, + } + + impl Ttpf { + fn open(path: &str) -> Ttpf { + let d = std::fs::read(path).unwrap(); + let u32_at = |o: usize| u32::from_le_bytes(d[o..o + 4].try_into().unwrap()); + assert_eq!(&d[..4], b"RDS5"); + let total = u32_at(8) as usize | (u32_at(0xc) as usize & 0xff) << 32; + let at = (u32_at(0x10) as usize) << 8; + let n = (u32_at(4) as usize).min((at - 0x18) / 8); + let mut blocks: Vec<(usize, usize)> = (0..n) + .filter_map(|i| { + let v = u64::from_le_bytes(d[0x18 + 8 * i..0x20 + 8 * i].try_into().unwrap()); + let pos = ((v >> 24) & 0xff_ffff) | ((v >> 48) & 0xf) << 24; + (v != 0).then_some((((v & 0xff_ffff) << 8) as usize, (pos << 6) as usize)) + }) + .collect(); + blocks.sort_by_key(|b| b.1); + let count = u32_at(at + 8) as usize; + let mut textures: Vec = (0..count) + .map(|i| { + let o = at + 16 + 16 * i; + let (a, b, c) = (u32_at(o), u32_at(o + 4), u32_at(o + 8)); + TtpfTexture { + index: i, + w: (a & 0x3fff) + 1, + h: ((a >> 14) & 0x3fff) + 1, + dxgi: (b & 0x3ff) >> 3, + // tile mode bits: 0 = 64 KB tiles (SW_64KB_D), else 4 KB (SW_4KB_S) + mode: if (b >> 24) & 3 == 0 { 10 } else { 5 }, + mips: (a >> 28).max(1), + slices: ((b >> 10) & 0x7ff) + 1, + start: (c & 0x1f_ffff) as usize * 4096 + 0x800, + end: total, + } + }) + .collect(); + let mut by_start: Vec = (0..count).collect(); + by_start.sort_by_key(|&i| textures[i].start); + for k in 0..count.saturating_sub(1) { + textures[by_start[k]].end = textures[by_start[k + 1]].start; + } + Ttpf { d, blocks, textures, cache: Default::default() } + } + + /// bytes [a, b) of the unpacked stream (None: in a block that is not zlib) + fn read(&self, a: usize, b: usize) -> Option> { + let mut out = Vec::with_capacity(b - a); + let mut pos = a; + while pos < b { + let i = self.blocks.partition_point(|bl| bl.1 <= pos).checked_sub(1)?; + let data = self.block(i)?; + let from = pos - self.blocks[i].1; + let to = (b - self.blocks[i].1).min(data.len()); + if to <= from { + return None; + } + out.extend_from_slice(&data[from..to]); + pos += to - from; + } + Some(out) + } + + fn block(&self, i: usize) -> Option>> { + self.cache + .borrow_mut() + .entry(i) + .or_insert_with(|| { + let off = self.blocks[i].0; + let next = self.blocks.iter().map(|b| b.0).filter(|&o| o > off).min().unwrap_or(self.d.len()); + let src = self.d.get(off..next)?; + (src.first() == Some(&0x78)).then(|| miniz_oxide::inflate::decompress_to_vec_zlib(src).ok().map(std::rc::Rc::new)).flatten() + }) + .clone() + } + } +} diff --git a/crates/xcl/src/gpu/tiling.rs b/crates/xcl/src/gpu/tiling.rs index f3b44d7..09f8fc4 100644 --- a/crates/xcl/src/gpu/tiling.rs +++ b/crates/xcl/src/gpu/tiling.rs @@ -162,6 +162,119 @@ pub fn image_layout(t: &super::desc::Image, cfg: PipeConfig) -> Option<(Layout, Some((l, t.width.div_ceil(side) as usize, t.height.div_ceil(side) as usize)) } +/// One mip level of a tiled 2D surface, in elements. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct MipLevel { + pub width: usize, + pub height: usize, + /// byte offset of the level's first block from the start of its slice + pub offset: usize, + /// width in elements the level's blocks are laid out for (the tail block's width in the tail) + pub pitch: usize, + /// where the level lies inside the mip tail block (levels in the tail), else (0, 0) + pub origin: (usize, usize), +} + +/// The mip chain of a tiled 2D surface (addrlib Gfx10Lib::ComputeSurfaceInfoMacroTiled / +/// MicroTiled). Per slice: first the mip tail – one block shared by the smallest levels, those +/// at most half a block wide (64 KB / 4 KB blocks only) – then the other levels from the +/// smallest to mip 0; slices follow each other `slice_bytes` apart. On the retail TTPF archive +/// the space between textures is exactly this size for 908 of its 912 textures. +#[derive(Clone, Debug)] +pub struct Chain { + pub layout: Layout, + pub levels: Vec, + pub slice_bytes: usize, +} + +impl Chain { + /// `width` × `height` pixels, `side` pixels per element side (4 for BCn), `mips` levels; + /// a level is the pixel size halved, rounded up to whole elements (as D3D12 sizes it). + pub fn new(layout: Layout, width: u32, height: u32, side: u32, mips: u32) -> Chain { + let mips = mips.max(1) as usize; + let (bw, bh) = (layout.block_w, layout.block_h); + let blk = layout.block_bytes.trailing_zeros() as usize; + let size = |i: usize| (((width >> i).max(1)).div_ceil(side) as usize, ((height >> i).max(1)).div_ceil(side) as usize); + // at most this many levels in the tail (GetMaxNumMipsInTail, 2D) + let max_tail = blk.saturating_sub(4); + let first_tail = if mips > 1 && blk > 8 { + (0..mips) + .find(|&i| { + let (w, h) = size(i); + w <= bw / 2 && h <= bh && mips - i <= max_tail + }) + .unwrap_or(mips) + } else { + mips + }; + let mut levels = vec![MipLevel::default(); mips]; + let mut offset = if first_tail < mips { layout.block_bytes } else { 0 }; + for i in (0..first_tail).rev() { + let (w, h) = size(i); + levels[i] = MipLevel { width: w, height: h, offset, pitch: w.div_ceil(bw) * bw, origin: (0, 0) }; + offset += layout.size(w, h); + } + // a level in the tail: its offset there (256-byte units, largest level first) gives its + // origin, x from the odd and y from the even address bits above 256 B + let e = layout.elem_log2 as usize; + let (w256, h256) = (1usize << [4, 4, 3, 3, 2][e], 1usize << [4, 3, 3, 2, 2][e]); + for i in first_tail..mips { + let m = max_tail - 1 - (i - first_tail); + let at = if m > 6 { 16usize << m } else { m << 8 }; + let (mut mx, mut my) = (0, 0); + for b in 0..6 { + mx |= ((at >> (9 + 2 * b)) & 1) << b; + my |= ((at >> (8 + 2 * b)) & 1) << b; + } + let (w, h) = size(i); + levels[i] = MipLevel { width: w, height: h, offset: 0, pitch: bw, origin: (mx * w256, my * h256) }; + } + Chain { layout, levels, slice_bytes: offset } + } + + fn element_at(&self, l: &MipLevel, x: usize, y: usize) -> usize { + l.offset + self.layout.offset(x + l.origin.0, y + l.origin.1, l.pitch) + } + + /// level `i` of one slice (`src` = the slice's bytes) → linear rows of elements; bytes + /// past the end of `src` read as 0 + pub fn detile_level(&self, src: &[u8], i: usize) -> Vec { + let l = self.levels[i]; + let e = 1usize << self.layout.elem_log2; + let mut out = vec![0u8; l.width * l.height * e]; + for y in 0..l.height { + for x in 0..l.width { + let o = self.element_at(&l, x, y); + if let Some(s) = src.get(o..o + e) { + out[(y * l.width + x) * e..][..e].copy_from_slice(s); + } + } + } + out + } + + /// linear rows of level `i` → its place in one slice (`dst`, `slice_bytes` long) + pub fn tile_level(&self, dst: &mut [u8], i: usize, linear: &[u8]) { + let l = self.levels[i]; + let e = 1usize << self.layout.elem_log2; + for y in 0..l.height { + for x in 0..l.width { + let o = self.element_at(&l, x, y); + dst[o..o + e].copy_from_slice(&linear[(y * l.width + x) * e..][..e]); + } + } + } +} + +/// The mip chain of a T#'s image (all `max_mip + 1` levels of the resource, base swizzle in +/// `base_xor`); the data starts at `address & !(block_bytes - 1)`. +pub fn image_chain(t: &super::desc::Image, cfg: PipeConfig) -> Option { + let (e, side) = element(t.format)?; + let mut l = Layout::new(t.swizzle_mode, e, cfg)?; + l.base_xor = (t.address as usize) & (l.block_bytes - 1); + Some(Chain::new(l, t.width, t.height, side, t.max_mip + 1)) +} + #[cfg(test)] mod tests { use super::*; @@ -198,6 +311,50 @@ mod tests { } } + /// Sizes of retail textures (TTPF): the space each takes in the archive. + #[test] + fn mip_chain_sizes_match_retail_textures() { + let cfg = PipeConfig { pipes_log2: 0, pkrs_log2: 0 }; + // (mode, element log2, side, width, height, mips, bytes) + for (mode, e, side, w, h, mips, bytes) in [ + (5, 4, 4, 128, 1024, 5, 192512), // BC7, 4 KB tiles + (5, 2, 1, 128, 128, 7, 90112), // BGRA8 + (5, 4, 4, 512, 64, 9, 65536), + (5, 3, 4, 64, 512, 9, 61440), // BC1 + (10, 4, 4, 2048, 2048, 1, 4194304), + (10, 4, 4, 512, 512, 8, 393216), // one slice of a 32-slice array + (5, 4, 4, 16, 16, 5, 4096), // all in the tail + ] { + let c = Chain::new(Layout::new(mode, e, cfg).unwrap(), w, h, side, mips); + assert_eq!(c.slice_bytes, bytes, "{w}x{h} e{e} mode {mode} mips {mips}"); + } + } + + /// Every level of a chain (with a mip tail) gets its own elements: tiling all levels with + /// distinct values and detiling them gives each back unchanged. + #[test] + fn mip_chain_levels_do_not_overlap() { + for (mode, e, w, h, mips) in [(5u32, 2u32, 128u32, 96u32, 8u32), (10, 4, 64, 64, 7), (10, 0, 512, 128, 10), (1, 2, 40, 24, 6)] { + let c = Chain::new(Layout::new(mode, e, PipeConfig { pipes_log2: 0, pkrs_log2: 0 }).unwrap(), w, h, 1, mips); + let mut slice = vec![0u8; c.slice_bytes]; + let mut want = Vec::new(); + let mut n = 0u32; + for (i, l) in c.levels.iter().enumerate() { + let lin: Vec = (0..l.width * l.height << e) + .map(|_| { + n = n.wrapping_mul(1_103_515_245).wrapping_add(12345); + (n >> 16) as u8 | 1 + }) + .collect(); + c.tile_level(&mut slice, i, &lin); + want.push(lin); + } + for (i, lin) in want.iter().enumerate() { + assert_eq!(&c.detile_level(&slice, i), lin, "mode {mode} e{e}: level {i} overwritten"); + } + } + } + /// SW_256B_S with bytes: a 16×16 block stored row by row (addrlib's first nibble row) #[test] fn sw_256b_s_bytes_are_row_major() {