From 2c2bce36504a83b36ceece2abb8826855e2f9892 Mon Sep 17 00:00:00 2001 From: nathan Date: Sat, 28 Nov 2026 15:41:11 +0100 Subject: [PATCH] D3D12.X: remap superseded targets bound through descriptor tables A target superseded by a newer overlapping placement (5d00b390) had its tiles unmapped (D3D12_TILE_RANGE_FLAG_NULL) and was only remapped by placements::note_use on ResourceBarrier or Copy* calls. Compute and graphics passes that bind a UAV/SRV texture through descriptor tables without a per-resource barrier (e.g. global UAV barriers) left the target unmapped, silently dropping UAV writes and returning zeros on reads. Also avoid superseding a live target when a newer placement at the exact same VA has the same texels (e.g. an R9G9B9E5 sampled alias over an R32_UINT UAV target whose owned page range differs by a trailing page). In bind_abi, scan descriptor table slots and call placements::note_use only when placements::any_superseded() is true (single relaxed atomic load on the fast path). Assisted-by: Antigravity (Gemini 4 Neon) --- crates/xcl/src/hle/d3d12x/bind.rs | 37 +++++++++++++++++ crates/xcl/src/hle/d3d12x/placements.rs | 55 ++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index b50b835091..829517eafd 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -1302,6 +1302,40 @@ pub(super) fn bound_pso(this: *mut c_void) -> usize { with(this, |s| s.pso) } +/// Remap any superseded targets named in the first 256 slots of each descriptor table in `layout` +/// / `args`. Only called when `placements::any_superseded()` is true. +unsafe fn note_superseded_in_tables(u: &crate::gpu::uma::Uma, layout: Option<&Layout>, args: &[u8]) { + let Some(layout) = layout else { return }; + const TABLE_SLOTS: usize = 256; + let mut table = [0u8; TABLE_SLOTS * 32]; + for p in &layout.params { + let Param::Table { at } = *p else { continue }; + let at = at as usize; + let Some(word) = args.get(at..at + 4) else { continue }; + let offset = u32::from_le_bytes(word.try_into().unwrap()) as usize; + if offset == 0 { + continue; + } + let addr = u.base() + offset; + if u.in_committed_window(addr, table.len()) { + std::ptr::copy_nonoverlapping(addr as *const u8, table.as_mut_ptr(), table.len()); + } else { + table.fill(0); + super::title_textures::read_committed_into(addr, &mut table); + } + for slot in table.chunks_exact(32) { + let index = u32::from_le_bytes(slot[..4].try_into().unwrap()); + if slot[15] >> 4 < 8 || index == 0 { + continue; + } + let res = super::console_heap::named_by_index(index); + if res != 0 && res != super::console_heap::GONE { + super::placements::note_use(res); + } + } + } +} + /// `XCL_DUMP_TARGETS`: the resources the first 16 slots of each descriptor table of `this`'s /// graphics arguments name, each once (as `trace_reads_of_watched` reads them). pub(super) fn table_resources(this: *mut c_void) -> Vec { @@ -5214,6 +5248,9 @@ unsafe fn bind_abi_with_root_resolved( let prepared = prepare_abi_state(this, u, gfx, user_sgprs)?; let PreparedAbiState { layout, args, inline, table_at, io_at, res_at, allocator, heaps } = prepared; want_root_views(layout.as_deref(), &args, super::view_want::Path::RootView); + if super::placements::any_superseded() { + note_superseded_in_tables(u, layout.as_deref(), &args); + } std::ptr::copy_nonoverlapping(args[inline..].as_ptr(), table_at as *mut u8, args.len() - inline); let io = prepared_abi_io(layout.as_deref(), &args, table_at, gfx, user_sgprs, u.base(), heaps, limits, groups, group_limit); let io_bytes = io.iter().flat_map(|word| word.to_le_bytes()).collect::>(); diff --git a/crates/xcl/src/hle/d3d12x/placements.rs b/crates/xcl/src/hle/d3d12x/placements.rs index 696e9dcfe5..5dda38c2d4 100644 --- a/crates/xcl/src/hle/d3d12x/placements.rs +++ b/crates/xcl/src/hle/d3d12x/placements.rs @@ -61,6 +61,16 @@ impl Desc { pub(super) fn of(d: &D3D12_RESOURCE_DESC) -> Desc { Desc { width: d.Width, height: d.Height, depth: d.DepthOrArraySize, mips: d.MipLevels, format: d.Format.0, flags: d.Flags.0 } } + + /// Whether a reserved texture of this description has the texels of one of `other`'s on the + /// same tiles: the same size and levels and a texel of the same size (e.g. R9G9B9E5 sampled + /// over an R32_UINT UAV target). + pub(super) fn same_texels(&self, other: &Desc) -> bool { + let plain = |f: i32| !matches!(f, 0 | 5..=8 | 66 | 68..=84 | 94..=99) && f < 100; + let texel = |f: i32| super::targets::texel_bytes(windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(f)); + (self.width, self.height, self.depth, self.mips) == (other.width, other.height, other.depth, other.mips) + && (self.format == other.format || (plain(self.format) && plain(other.format) && texel(self.format) == texel(other.format))) + } } impl std::fmt::Display for Desc { @@ -225,7 +235,8 @@ impl Registry { let mut out = Placed { seq: self.seq, remap, ..Default::default() }; for &(o, overlap) in &found { let Some(old) = self.live.get_mut(&o) else { continue }; - let retired = retire && supersedes(overlap, old.kind, old.backing, old.released, old.mapped); + let same_view = p.desc.format != 0 && p.va == old.va && p.desc.same_texels(&old.desc); + let retired = retire && !same_view && supersedes(overlap, old.kind, old.backing, old.released, old.mapped); if look { out.cases.push(Case { old: o, @@ -527,7 +538,12 @@ pub(super) unsafe fn unmap_if_superseded(res: &ID3D12Resource) { } } -/// The title uses `res` (a barrier, a copy): a superseded target goes back on the shared pages – +/// Whether any live target is currently superseded (`SUPERSEDED != 0`). +pub(super) fn any_superseded() -> bool { + SUPERSEDED.load(Ordering::Relaxed) != 0 +} + +/// The title uses `res` (a barrier, a copy, or a descriptor table): a superseded target goes back on the shared pages – /// on the console the title may use both over one memory when it knows they share it. Logged /// once for each pair (the first 64). The work recorded now waits for the mapping /// (`vram::before_execute` at its submit). @@ -778,4 +794,39 @@ mod tests { assert!(r.use_again(1).is_empty(), "released"); assert!(r.remove(3).unwrap().is_empty()); } + + /// A sampled alias at the same address with the same texels (e.g. R9G9B9E5 over an R32_UINT + /// UAV target) must not supersede the target even when their owned page ranges differ by a + /// trailing page. + #[test] + fn same_address_same_texels_alias_pair_does_not_supersede() { + let mut r = Registry::default(); + let at = 0x10_7720_7400usize; + let p0 = at.next_multiple_of(K64); + let d = |format: i32, flags: i32| Desc { width: 640, height: 360, depth: 1, mips: 1, format, flags }; + let out_uav = r.place(1, at, (p0, p0 + 14 * K64), Backing::Pages(1), Kind::Target, d(42, 4), Instant::now(), false, true); + assert!(out_uav.victims.is_empty()); + let out_srv = r.place(2, at, (p0, p0 + 15 * K64), Backing::Pages(1), Kind::Sampled, d(67, 0), Instant::now(), false, true); + assert!(out_srv.victims.is_empty(), "same-VA same-texels alias must not supersede its UAV target"); + assert!(!r.superseded(1)); + assert_eq!(r.unmapped, 0); + } + + /// A target superseded by a newer overlapping placement at another address is remapped when + /// used again (e.g. via descriptor-table binding). + #[test] + fn superseded_target_remaps_on_use_again() { + let mut r = Registry::default(); + let at = 0x10_b69b_c200usize; + let p0 = at.next_multiple_of(K64); + let d = |format: i32| Desc { width: 480, height: 272, depth: 1, mips: 1, format, flags: 4 }; + r.place(10, at, (p0, p0 + 7 * K64), Backing::Pages(1), Kind::Target, d(24), Instant::now(), false, true); + let out = r.place(11, at - 0x7_f800, (p0 - 7 * K64, p0 + K64), Backing::Pages(1), Kind::Target, d(42), Instant::now(), false, true); + assert_eq!(out.victims, vec![10]); + assert!(r.superseded(10)); + assert_eq!(r.unmapped, 1); + assert_eq!(r.use_again(10), vec![11]); + assert!(!r.superseded(10)); + assert_eq!(r.unmapped, 0); + } }