From e07d03bf0fc1bcbfac3d369d2f7eeed5eba6afd7 Mon Sep 17 00:00:00 2001 From: MacieGx Date: Thu, 26 Nov 2026 06:01:16 +0100 Subject: [PATCH] D3D12.X: a 3-D texture XG cannot size is taken at its texels' bytes xg::Texture::volume refuses 3-D render targets (flags 1 / 2: E_NOTIMPL), so CreatePlacedResourceX had no console size for them and vram gave such a texture every tile of the PC texture as a 64 KiB page at its address. The newer retail title of #505 places thirteen 33x33x33 RGBA16F colour LUTs (flags 0x5) 512 KiB apart. The PC texture has 18 tiles, so each LUT shared 10 pages with the next one. A LUT's bake overwrote its neighbour's rows and the tone map sampled the wrong LUT (black -> cyan): the picture was teal. On our lab branch the same change turned the picture into the game's colours. The fallback is the bytes of the texels of every level (287,496 bytes, 5 pages of its own, for that LUT) and not a console layout. What the console's library does with a 3-D render target is not known: the xg oracle covers 3-D flags 0 and 4 only, and no console library was at hand. The 2-D render target rule (64KB_R_X, thin for 3-D) gives 2.06 MiB, which cannot be right for textures packed 512 KiB apart and keeps the overlap. 4KB_S thick (500 KiB) fits but is a guess. Teaching XG would also change what XGCreateTextureComputer and GetResourceAllocationInfo tell titles. No layout is smaller than its texels, so the pages taken are always the texture's own. Part of #505. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/device_x.rs | 65 ++++++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/vram.rs | 2 +- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 57c30e0ffe..eb446c1629 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -1081,6 +1081,38 @@ fn note_alias(va: usize, x: &[u8; 0x38]) { } } +/// The size of a texture (`x`: the console's desc) in title memory: XG's, or for a 3-D texture +/// XG does not lay out (render targets: a retail title's 33x33x33 RGBA16F colour LUTs, 512 KiB +/// apart) the bytes of its texels, which no console layout of them is smaller than. Without a +/// size, `vram` gave such a texture every tile of the PC texture as a page at its address (18 for +/// that LUT), so neighbouring LUTs shared 10 pages and overwrote each other's rows. +fn console_bytes(x: &[u8; 0x38]) -> Option { + crate::hle::xg::Texture::new(x).ok().map(|t| t.size()).filter(|&s| s != u64::MAX && s != 0).or_else(|| volume_texel_bytes(x)) +} + +/// The bytes of the texels of every level of a 3-D texture (`x`: the console's desc); None for +/// other dimensions and formats `xg::element` does not know. +fn volume_texel_bytes(x: &[u8; 0x38]) -> Option { + let u = |o: usize| u32::from_le_bytes(x[o..o + 4].try_into().unwrap()); + let w16 = |o: usize| u16::from_le_bytes(x[o..o + 2].try_into().unwrap()) as u64; + if u(0) != D3D12_RESOURCE_DIMENSION_TEXTURE3D.0 as u32 { + return None; + } + let (width, height, depth, mips) = (u64::from_le_bytes(x[0x10..0x18].try_into().unwrap()), u(0x18) as u64, w16(0x1C), w16(0x1E)); + if width == 0 || height == 0 || depth == 0 { + return None; + } + let (e, side) = crate::hle::xg::element(u(0x20))?; + let mips = if mips == 0 { 64 - width.max(height).max(depth).leading_zeros() as u64 } else { mips }; + let mut bytes = 0u64; + for i in 0..mips.min(16) { + let level = |n: u64| (n >> i).max(1); + let texels = level(width).div_ceil(side as u64).checked_mul(level(height).div_ceil(side as u64))?.checked_mul(level(depth))?; + bytes = bytes.checked_add(texels.checked_mul(1 << e)?)?; + } + Some(bytes) +} + /// device #44 CreatePlacedResourceX(address, desc, initial state, clear value, riid, resource): /// a resource in title memory. A buffer at a 64 KiB-aligned address in unified memory is that /// memory (placed in its UMA heap); a texture gets a PC texture kept in step with the memory @@ -1123,7 +1155,7 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, let mut placed_hr = None; if !buffer { let pc = std::ptr::read_unaligned(d.as_ptr() as *const D3D12_RESOURCE_DESC); - let console_bytes = crate::hle::xg::Texture::new(&x).ok().map(|t| t.size()).filter(|&s| s != u64::MAX && s != 0); + let console_bytes = console_bytes(&x); if super::vram::gpu_owned(flags) { if let Some(hr) = super::vram::place(this, va as usize, &pc, state, clear, console_bytes, riid, ppv) { if hr >= 0 && !(*ppv).is_null() { @@ -1297,6 +1329,37 @@ mod tests { assert_eq!(pc_initial_state(0, 0x10_0004), 0x4); } + /// A retail title's 33x33x33 RGBA16F colour LUT targets (render target + UAV), 512 KiB apart: + /// XG does not lay out 3-D render targets, so each is taken at its texels' bytes, and of the + /// PC texture's 18 tiles no page of one is a page of the next. A sampled 3-D texture keeps + /// XG's size. + #[test] + fn volume_targets_xg_does_not_lay_out_keep_to_their_texels() { + let volume = |flags: u32, mips: u16| { + let mut x = [0u8; 0x38]; + x[0..4].copy_from_slice(&4u32.to_le_bytes()); // texture 3D + x[0x10..0x18].copy_from_slice(&33u64.to_le_bytes()); + x[0x18..0x1C].copy_from_slice(&33u32.to_le_bytes()); + x[0x1C..0x1E].copy_from_slice(&33u16.to_le_bytes()); + x[0x1E..0x20].copy_from_slice(&mips.to_le_bytes()); + x[0x20..0x24].copy_from_slice(&10u32.to_le_bytes()); // R16G16B16A16_FLOAT + x[0x24..0x28].copy_from_slice(&1u32.to_le_bytes()); + x[0x30..0x34].copy_from_slice(&flags.to_le_bytes()); + x + }; + let lut = volume(0x5, 1); + assert!(crate::hle::xg::Texture::new(&lut).is_err()); + assert_eq!(console_bytes(&lut), Some(33 * 33 * 33 * 8)); + let (a, b) = (0x15_1c8a_0000usize, 0x15_1c92_0000usize); + let (first_a, n_a) = super::super::vram::owned_pages(a, console_bytes(&lut), 18).unwrap(); + let (first_b, _) = super::super::vram::owned_pages(b, console_bytes(&lut), 18).unwrap(); + assert!(first_a + n_a * 0x1_0000 <= first_b, "{first_a:#x} + {n_a} pages reach {first_b:#x}"); + // every level: 33^3, 16^3, ... 1 texels + assert_eq!(console_bytes(&volume(0x5, 0)), Some((33 * 33 * 33 + 16 * 16 * 16 + 8 * 8 * 8 + 4 * 4 * 4 + 2 * 2 * 2 + 1) * 8)); + let sampled = volume(0, 1); + assert_eq!(console_bytes(&sampled), Some(crate::hle::xg::Texture::new(&sampled).unwrap().size())); + } + /// GPU: a 256x256 D16 depth buffer the console creates for unordered access too, in /// UNORDERED_ACCESS, is placed on the PC as its description and initial state say; the state /// with only the console's bits dropped is refused (the depth buffer has no UAV flag there). diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index ea49ce6b89..796d367185 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -888,7 +888,7 @@ fn heap_ranges_trimmed(heap: u32, tiles: &[Tile]) -> Vec<(bool, u32, u32)> { /// 64 KiB page boundaries `P` with `va <= P < va + bytes`, capped at `total` tiles: /// `(first_page, count)`. Because every `P` lies strictly inside `[va, va + bytes)`, two /// non-overlapping ranges never share a page here even when `va` is only 4 KiB aligned. -fn owned_pages(va: usize, console_bytes: Option, total: usize) -> Option<(usize, usize)> { +pub(super) fn owned_pages(va: usize, console_bytes: Option, total: usize) -> Option<(usize, usize)> { match console_bytes { Some(b) => { let bytes = usize::try_from(b).ok()?;