From 443ad6381509f78a63bffadf049c151e39c8cedd Mon Sep 17 00:00:00 2001 From: BBareth <179921368+BBareth@users.noreply.github.com> Date: Mon, 30 Nov 2026 10:36:25 +0100 Subject: [PATCH] D3D12.X: download block-compressed texture mip tails D3D12 returns a 4x4 footprint for the 2x2 and 1x1 BC3 mips. Its single-footprint query also preserved the unaligned 0x555780 base on an RTX 5070 Ti. The planner rejected both results, and reverse layout also expected logical mip dimensions. Align each query base and use whole-block extents for the copy and reverse layout. The GPU regression publishes all 12 mips byte for byte on the RTX 5070 Ti. The title_textures suite reports 67 passed, 0 failed, 1 ignored and no SKIPPED lines. The base alignment applies to every format: an RGBA8 chain's small mips were refused the same way, and a second GPU test publishes all 8 mips of a 128x128 one. Assisted-by: Codex (GPT-6.2-Sol) Co-Authored-By: Claude Fable 5.2 --- README.md | 2 +- crates/xcl/src/hle/d3d12x/title_textures.rs | 126 ++++++++++++++++++-- 2 files changed, 116 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 483162f329..cd2c80e13f 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ A gamepad is recommended ([Controls](#controls)). The game folder is never modif | **Pipelines** | ✅ | PSOs from console shaders: VS+PS, CS, MS+PS, VS+GS+PS; root signature with the shaders' static samplers; non-blocking creation – a pipeline whose shaders are not compiled yet gets a stand-in at once and is built in the background | GPU tests drawing the game's own VS / PS pairs, also built in the background | | **D3D12.X compute** | ✅ | console device slots, console descriptor heaps (T# / V# / S#), buffers at their console addresses, compute command lists recorded in the console format and replayed (tables, barriers with console flag translation, clears, copies) | the game's BVH builder and FSR 4 record and run through XCL | | **D3D12.X graphics** | ✅ | device, resources and heaps (also placed in title memory), pipelines; the title's graphics command lists – draws, indirect draws, graphics root arguments, barriers, copies, clears – and command lists recorded by console code, replayed; `PresentX` and frame pacing into an XCL window | Minecraft Preview plays its worlds (millions of draws, none skipped); Fortnite and XDefiant draw their screens | -| **Textures** | ✅ | GFX10 tiling (all 14 2D swizzle modes, the 6 3D thick modes, linear), mip chains with mip tail, arrays, cubes and 3D textures → PC textures; supported GPU-written title subresources copy back before their writer queue's title fence becomes visible | 912 / 912 entries detiled in the tested retail archive; GPU upload bit exact in test fixtures | +| **Textures** | ✅ | GFX10 tiling (all 14 2D swizzle modes, the 6 3D thick modes, linear), mip chains with mip tail, arrays, cubes and 3D textures → PC textures; supported GPU-written title subresources, including block-compressed mip tails, copy back before their writer queue's title fence becomes visible | 912 / 912 entries detiled in the tested retail archive; GPU upload and BC3 download bit exact in test fixtures | | **Ray tracing** | ✅ | BLAS / TLAS built on the GPU by the game's own XboxBvhBuilder; RDNA2 BVH node format; BVH intersection instructions emulated in shaders, with misses for nodes outside mapped memory windows | GPU traversal = brute force reference; the game's builder output checked on the GPU | | **Upscaling** | ✅ | the game's AMD FSR 4 console DLL (`amd_fidelityfx_upscaler_xbox.dll`): context, the 29 pipelines in the console driver's own container (FSPX) translated, a frame's 20 passes recorded and run on the GPU | end-to-end with the real DLL: a 1280×720 → 1920×1080 frame on an RX 9070, the input's gradient in the output | diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index d809b1c2a2..74694749fa 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -3497,15 +3497,15 @@ pub(super) fn reverse_layout_subresource( (sub % mips, Some(sub / mips)) }; let level = &surface.chain.levels[mip]; - let width = (surface.width >> mip).max(1); - let height = (surface.height >> mip).max(1); + let width = (surface.width >> mip).max(1).checked_next_multiple_of(side).ok_or(ReverseLayoutError::Dimensions)?; + let height = (surface.height >> mip).max(1).checked_next_multiple_of(side).ok_or(ReverseLayoutError::Dimensions)?; let depth = if surface.depth > 0 { (surface.depth >> mip).max(1) } else { 1 }; if footprint.width != width || footprint.height != height || footprint.depth != depth { return Err(ReverseLayoutError::Dimensions); } let row_bytes = level.width.checked_mul(surface.bpe as usize).ok_or(ReverseLayoutError::Footprint)?; - if level.width != (width as usize).div_ceil(side as usize) - || level.height != (height as usize).div_ceil(side as usize) + if level.width != (width / side) as usize + || level.height != (height / side) as usize || level.depth != depth as usize { return Err(ReverseLayoutError::UnsupportedLayout); @@ -3565,7 +3565,7 @@ pub(super) struct DownloadSubresource { } /// Validate driver footprints against the immutable XG layout and explicit tracked states. -/// The driver may add only the required D3D row/placement alignment, never enlarge the work. +/// Footprint dimensions include whole format blocks. Row sizes and counts must match the retained layout. fn validate_download_layout( tex: &xg::Texture, desc: &D3D12_RESOURCE_DESC, @@ -3619,8 +3619,8 @@ fn validate_download_layout( }; if std::mem::replace(&mut seen[sub], true) { return Err("duplicate subresource".into()) } let level = &surface.chain.levels[mip]; - let width = (surface.width >> mip).max(1); - let height = (surface.height >> mip).max(1); + let width = (surface.width >> mip).max(1).checked_next_multiple_of(side).ok_or("footprint width overflow")?; + let height = (surface.height >> mip).max(1).checked_next_multiple_of(side).ok_or("footprint height overflow")?; let row_bytes = level.width.checked_mul(surface.bpe as usize).ok_or("row size overflow")?; let expected_pitch = row_bytes.checked_next_multiple_of(256).ok_or("row pitch overflow")?; let expected_offset = total.checked_next_multiple_of(512).ok_or("footprint offset overflow")?; @@ -4227,6 +4227,9 @@ pub(super) unsafe fn prepare_texture_download( queried.try_reserve_exact(selected.len()).map_err(|_| "cannot allocate driver footprints")?; let mut base = 0u64; for &(subresource, _) in selected { + // GetCopyableFootprints adds the supplied base to the first footprint's offset. + base = base.checked_next_multiple_of(u64::from(D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT)) + .ok_or("footprint offset overflow")?; let mut fp = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); let (mut rows, mut row_bytes, mut ignored_total) = (0u32, 0u64, 0u64); dev.GetCopyableFootprints(&desc, subresource, 1, base, Some(&mut fp), Some(&mut rows), Some(&mut row_bytes), Some(&mut ignored_total)); @@ -5887,7 +5890,8 @@ pub(super) mod tests { let mips = s.chain.levels.len(); let mip = subresource as usize % mips; let l = &s.chain.levels[mip]; - let (w, h) = ((s.width >> mip).max(1), (s.height >> mip).max(1)); + let w = (s.width >> mip).max(1).next_multiple_of(s.side); + let h = (s.height >> mip).max(1).next_multiple_of(s.side); let depth = if s.depth > 0 { (s.depth >> mip).max(1) } else { 1 }; let row_bytes = l.width * s.bpe as usize; let row_pitch = row_bytes.next_multiple_of(256); @@ -5929,8 +5933,8 @@ pub(super) mod tests { Offset: offset as u64, Footprint: D3D12_SUBRESOURCE_FOOTPRINT { Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(format as i32), - Width: (s.width >> mip).max(1), - Height: (s.height >> mip).max(1), + Width: (s.width >> mip).max(1).next_multiple_of(s.side), + Height: (s.height >> mip).max(1).next_multiple_of(s.side), Depth: if s.depth > 0 { (s.depth >> mip).max(1) } else { 1 }, RowPitch: pitch as u32, }, @@ -5942,6 +5946,106 @@ pub(super) mod tests { .collect() } + + #[test] + fn gpu_download_publishes_every_bc3_mip() { + use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_BC3_UNORM_SRGB; + gpu_download_publishes_every_mip(DXGI_FORMAT_BC3_UNORM_SRGB.0 as u32, 2048, 12, 16); + } + + /// The base alignment is not specific to block compression: an RGBA8 chain's small mips end + /// off a 512-byte boundary too. + #[test] + fn gpu_download_publishes_every_rgba8_mip() { + use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM; + gpu_download_publishes_every_mip(DXGI_FORMAT_R8G8B8A8_UNORM.0 as u32, 128, 8, 4); + } + + /// A pattern uploaded to all `mips` of a `side` x `side` texture (`unit` bytes per block or + /// texel) comes back through a download plan byte for byte. + fn gpu_download_publishes_every_mip(format: u32, side: u32, mips: u16, unit: usize) { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + + let mut dev: Option = None; + if unsafe { D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev) }.is_err() { + return crate::gpu::recomp::runner::skip_test("no GPU"); + } + let dev = dev.unwrap(); + let console = console_desc(3, u64::from(side), side, 1, mips, format, 0x105); + let tex = Arc::new(xg::Texture::new(&console).unwrap()); + let pc = super::super::device_x::pc_resource_desc(&console); + let desc: D3D12_RESOURCE_DESC = unsafe { std::ptr::read_unaligned(pc.as_ptr().cast()) }; + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + let state = D3D12_RESOURCE_STATE_COMMON; + let selected: Vec<_> = (0..u32::from(desc.MipLevels)).map(|sub| (sub, state)).collect(); + + unsafe { + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC::default()).unwrap(); + let mut source: Option = None; + dev.CreateCommittedResource( + &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + D3D12_HEAP_FLAG_NONE, &desc, state, None, &mut source, + ).unwrap(); + let source = source.unwrap(); + let plan = prepare_texture_download(&dev, &source, ty, tex.clone(), format, &selected).unwrap(); + + let mut expected = vec![0xCD; tex.size() as usize]; + let chain = &tex.texels().unwrap().chain; + for (mip, level) in chain.levels.iter().enumerate() { + let linear: Vec<_> = (0..level.width * level.height * unit) + .map(|i| (mip * 17 + i * 31 + i / unit) as u8) + .collect(); + chain.tile_level(&mut expected, mip, 0, &linear); + } + let item = texture_upload::Item { + res: source.clone(), src: expected.as_ptr() as usize, tex: tex.clone(), + states: vec![state], changed: None, known: None, pages: None, + }; + let (errors, upload, _) = own_work(|| texture_upload::submit(&dev, &queue, &[item])).unwrap(); + assert!(errors.is_empty() && upload.is_some(), "upload errors: {errors:?}"); + let submitted = plan.submit(&queue).unwrap(); + assert!(submitted.signal_error.is_none(), "{:?}", submitted.signal_error); + crate::gpu::fence::wait(&submitted.ticket.fence, 1, &dev, None, 5_000).unwrap(); + texture_upload::give_back(upload.unwrap()); + + let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let mut backing = vec![0xCD; expected.len()]; + let target = backing.as_mut_ptr() as usize; + let res = source.as_raw() as usize; + register(dev.as_raw(), source.as_raw(), target, &console, state).unwrap(); + let write = capture_gpu_write(res, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES).unwrap(); + let writer_queue = WriterQueueId { raw: queue.as_raw() as usize, generation: 0 }; + publish_gpu_writes(writer_queue, &[write]); + let writes = download_writes(res, &TEXTURES.lock().unwrap().by_res[&res], writer_queue); + let download = ReadyDownload { + res, generation: write.generation, target, format, writes, ticket: submitted.ticket, + }; + let published = publish_download(&download); + forget(res); + assert!(published.unwrap()); + let mismatch = backing.iter().zip(&expected).position(|(a, b)| a != b); + assert_eq!(mismatch, None, "first differing byte in the published texture"); + } + } + + #[test] + fn download_layout_accepts_full_bc_blocks_for_small_mips() { + let console = console_desc(3, 4, 4, 1, 3, 78, 0x105); + let tex = xg::Texture::new(&console).unwrap(); + let pc = super::super::device_x::pc_resource_desc(&console); + let desc: D3D12_RESOURCE_DESC = unsafe { std::ptr::read_unaligned(pc.as_ptr().cast()) }; + let selected = [(1, D3D12_RESOURCE_STATE_COPY_SOURCE), (2, D3D12_RESOURCE_STATE_COPY_SOURCE)]; + let mut driver = driver_download_layout(&tex, 78, &selected); + let (layout, bytes) = validate_download_layout(&tex, &desc, 78, &selected, &driver).unwrap(); + assert_eq!(bytes, 528); + assert_eq!(layout[1].footprint.offset, 512); + for sub in &layout { + assert_eq!((sub.footprint.width, sub.footprint.height, sub.footprint.size), (4, 4, 16)); + } + driver[1].0.Footprint.Width = 1; + assert!(validate_download_layout(&tex, &desc, 78, &selected, &driver).is_err()); + } + #[test] fn download_plans_use_writer_capable_queue_types() { assert!(download_queue_type_supported(D3D12_COMMAND_LIST_TYPE_DIRECT)); @@ -6031,7 +6135,7 @@ pub(super) mod tests { let blocks = xg::Texture::new(&console_desc(3, 10, 9, 1, 1, 71, 0x105)).unwrap(); let (fp, readback, linear) = readback_for(&blocks, 71, 0); - assert_eq!((fp.width, fp.height, fp.depth), (10, 9, 1)); + assert_eq!((fp.width, fp.height, fp.depth), (12, 12, 1)); assert_eq!(blocks.texels().unwrap().chain.levels[0].width, 3); let mut console = vec![0u8; blocks.size() as usize]; reverse_layout_subresource(&blocks, 71, 0, fp, &readback, &mut console).unwrap();