From bfafdf77a42a39bff689a5e7e9a92c2d085799c5 Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:20:10 +0100 Subject: [PATCH] D3D12.X: add depth+UAV color twins and format-aliased placed texture copies --- crates/xcl/src/hle/d3d12x/bind.rs | 21 +- crates/xcl/src/hle/d3d12x/depth_uav.rs | 2749 +++++++++++++++++++++ crates/xcl/src/hle/d3d12x/device_x.rs | 45 +- crates/xcl/src/hle/d3d12x/format_alias.rs | 1282 ++++++++++ crates/xcl/src/hle/d3d12x/late_release.rs | 9 +- crates/xcl/src/hle/d3d12x/list_x.rs | 211 +- crates/xcl/src/hle/d3d12x/mod.rs | 2 + crates/xcl/src/hle/d3d12x/real.rs | 118 +- 8 files changed, 4347 insertions(+), 90 deletions(-) create mode 100644 crates/xcl/src/hle/d3d12x/depth_uav.rs create mode 100644 crates/xcl/src/hle/d3d12x/format_alias.rs diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index fad934addc..8535395630 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -28,6 +28,8 @@ struct State { /// the title's graphics root signature (a PC object), bound before a draw with an /// untranslated pipeline – created with it – since SetGraphicsRootSignature binds nothing rs_gfx: usize, + /// the title's compute root signature (a PC object), preserved for internal-pass restoration + rs_cs: usize, pso: usize, allocator: usize, /// unified memory of this recording (io, argument tables) @@ -82,6 +84,7 @@ pub(super) fn reset(list: *mut c_void, allocator: usize, initial_pso: usize) { s.layout_gfx = None; s.args_gfx.clear(); s.rs_gfx = 0; + s.rs_cs = 0; (old, std::mem::take(&mut s.uma)) }); console_list::hold_until_reset(old, blocks); @@ -97,6 +100,7 @@ pub(super) fn clear_state(list: *mut c_void, pso: usize) { s.layout_gfx = None; s.args_gfx.clear(); s.rs_gfx = 0; + s.rs_cs = 0; }); } @@ -104,8 +108,8 @@ pub(super) fn clear_state(list: *mut c_void, pso: usize) { /// deferred mid-list split (`to` records into `allocator` with its own fresh UMA arena while /// `from` retains its previously allocated blocks until `allocator` resets). pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: usize) { - let (layout, args, layout_gfx, args_gfx, rs_gfx, pso) = with(from, |s| { - (s.layout.clone(), s.args.clone(), s.layout_gfx.clone(), s.args_gfx.clone(), s.rs_gfx, s.pso) + let (layout, args, layout_gfx, args_gfx, rs_gfx, rs_cs, pso) = with(from, |s| { + (s.layout.clone(), s.args.clone(), s.layout_gfx.clone(), s.args_gfx.clone(), s.rs_gfx, s.rs_cs, s.pso) }); let (old, blocks) = with(to, |s| { let old = std::mem::replace(&mut s.allocator, allocator); @@ -115,6 +119,7 @@ pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: u s.layout_gfx = layout_gfx; s.args_gfx = args_gfx; s.rs_gfx = rs_gfx; + s.rs_cs = rs_cs; s.pso = pso; (old, std::mem::take(&mut s.uma)) }); @@ -811,6 +816,7 @@ pub(super) unsafe extern "system" fn set_compute_root_signature(this: *mut c_voi with(this, |s| { s.args = vec![0u8; layout.as_ref().map_or(0, |l| l.size as usize)]; s.layout = layout; + s.rs_cs = rs as usize; }); } @@ -1056,6 +1062,17 @@ pub(super) fn tracked_pso(list: *mut c_void) -> usize { .map_or(0, |s| s.pso) } +/// Tracked console `(graphics_root_signature, compute_root_signature)` on `list` for restoring +/// PC root signatures after an internal graphics/compute sync pass. +pub(super) fn tracked_root_sigs(list: *mut c_void) -> (usize, usize) { + STATES + .lock() + .unwrap() + .as_ref() + .and_then(|m| m.get(&(list as usize))) + .map_or((0, 0), |s| (s.rs_gfx, s.rs_cs)) +} + #[cfg(test)] mod tests { use super::super::{device_x, list_x, pso_async, real, title_list}; diff --git a/crates/xcl/src/hle/d3d12x/depth_uav.rs b/crates/xcl/src/hle/d3d12x/depth_uav.rs new file mode 100644 index 0000000000..2fdba89b40 --- /dev/null +++ b/crates/xcl/src/hle/d3d12x/depth_uav.rs @@ -0,0 +1,2749 @@ +//! Depth-stencil + unordered-access color twins (`ALLOW_DEPTH_STENCIL | ALLOW_UNORDERED_ACCESS`). +//! +//! Console titles bind depth-stencil buffers as UAVs in compute passes (for example, writing +//! tiled material classifications into depth and bitmasks into stencil, then testing them with +//! `DEPTH_READ` / `EQUAL` draws). PC D3D12 rejects `D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS` +//! on depth-stencil textures, so `device_x::pc_resource_desc` strips bit 2 (`ALLOW_UNORDERED_ACCESS`). +//! Without a writable host twin, every UAV of the depth-stencil texture becomes a null view and +//! compute writes vanish. +//! +//! This module pairs each single-sample depth+UAV texture with lazily allocated `ALLOW_UNORDERED_ACCESS` +//! color twin texture(s) (`R32_TYPELESS` / `R16_TYPELESS` for the depth plane and `R8_TYPELESS` for +//! the stencil plane on two-plane formats). Outside synchronization ops, every twin stays in +//! `D3D12_RESOURCE_STATE_UNORDERED_ACCESS` (valid on both DIRECT and COMPUTE queues). On DIRECT +//! command list transitions out of / into `UNORDERED_ACCESS` (or before `OMSetRenderTargets` binds +//! the DSV), dirty planes synchronize lazily: +//! * Single-plane `R32_TYPELESS` / `R16_TYPELESS`: direct `CopyResource`. +//! * Two-plane `R32G8X24_TYPELESS` depth (plane 0) and any two-plane stencil (plane 1): +//! per-subresource buffer-through `CopyTextureRegion` (exact bit-for-bit transfer). +//! * Two-plane `R24G8_TYPELESS` depth (plane 0, e.g. console `D16_UNORM_S8_UINT` / `D24_UNORM_S8_UINT`): +//! fullscreen depth-write pass (`SV_Depth`, stencil writes disabled) for twin -> depth and a +//! compute copy (`R24_UNORM_X8_TYPELESS` -> `R16_UNORM`/`R32_FLOAT`) for depth -> twin, +//! restoring all list graphics/descriptor bindings afterward. +//! +//! Set `XCL_DEPTH_UAV_TWIN=0` to disable color twins. + +use std::collections::HashMap; +use std::ffi::c_void; +use std::mem::ManuallyDrop; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Mutex, OnceLock}; + +use windows::core::{Interface, PCSTR}; +use windows::Win32::Foundation::RECT; +use windows::Win32::Graphics::Direct3D::Fxc::D3DCompile; +use windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; +use windows::Win32::Graphics::Direct3D12::*; +use windows::Win32::Graphics::Dxgi::Common::*; + +use super::list_x; +use super::real; + +const STATE_COMMON: u32 = 0; +const STATE_UAV: u32 = 0x8; +const STATE_DEPTH_WRITE: u32 = 0x10; +const STATE_DEPTH_READ: u32 = 0x20; +const STATE_COPY_DEST: u32 = 0x400; +const STATE_COPY_SOURCE: u32 = 0x800; +const STATE_SRV_ALL: u32 = 0x40 | 0x80; + +const FLAG_DEPTH_STENCIL: u32 = 0x2; +const FLAG_UAV: u32 = 0x4; + +/// How a depth resource's plane 0 synchronizes with its `depth_twin`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum DepthSyncKind { + /// Single-plane `R32_TYPELESS` or `R16_TYPELESS`: `CopyResource` directly. + DirectCopy, + /// Two-plane `R32G8X24_TYPELESS` plane 0 (`R32_TYPELESS`): buffer-through `CopyTextureRegion`. + BufferThrough, + /// Two-plane `R24G8_TYPELESS` plane 0: fullscreen depth-write pass (twin -> depth) and CS copy (depth -> twin). + FullscreenPass { twin_srv_fmt: i32, twin_uav_fmt: i32 }, +} + +/// Twin allocation & sync plan derived from a console resource format. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) struct TwinPlan { + pub depth_twin_fmt: i32, + pub stencil_twin_fmt: Option, + pub depth_sync: DepthSyncKind, + pub two_plane: bool, +} + +/// Choose the color twin format(s) and sync strategy for a console depth-stencil format. +pub(super) fn plan_for_console_format(console_fmt: u32) -> Option { + match console_fmt { + // D32_FLOAT / R32_TYPELESS + 39 | 40 => Some(TwinPlan { + depth_twin_fmt: DXGI_FORMAT_R32_TYPELESS.0, + stencil_twin_fmt: None, + depth_sync: DepthSyncKind::DirectCopy, + two_plane: false, + }), + // D16_UNORM / R16_TYPELESS + 53 | 55 => Some(TwinPlan { + depth_twin_fmt: DXGI_FORMAT_R16_TYPELESS.0, + stencil_twin_fmt: None, + depth_sync: DepthSyncKind::DirectCopy, + two_plane: false, + }), + // D32_FLOAT_S8X24_UINT / R32G8X24_TYPELESS + 19 | 20 => Some(TwinPlan { + depth_twin_fmt: DXGI_FORMAT_R32_TYPELESS.0, + stencil_twin_fmt: Some(DXGI_FORMAT_R8_TYPELESS.0), + depth_sync: DepthSyncKind::BufferThrough, + two_plane: true, + }), + // Console D16_UNORM_S8_UINT family -> PC R24G8_TYPELESS + 118..=120 => Some(TwinPlan { + depth_twin_fmt: DXGI_FORMAT_R16_TYPELESS.0, + stencil_twin_fmt: Some(DXGI_FORMAT_R8_TYPELESS.0), + depth_sync: DepthSyncKind::FullscreenPass { + twin_srv_fmt: DXGI_FORMAT_R16_UNORM.0, + twin_uav_fmt: DXGI_FORMAT_R16_UNORM.0, + }, + two_plane: true, + }), + // D24_UNORM_S8_UINT / R24G8_TYPELESS + 44 | 45 => Some(TwinPlan { + depth_twin_fmt: DXGI_FORMAT_R32_TYPELESS.0, + stencil_twin_fmt: Some(DXGI_FORMAT_R8_TYPELESS.0), + depth_sync: DepthSyncKind::FullscreenPass { + twin_srv_fmt: DXGI_FORMAT_R32_FLOAT.0, + twin_uav_fmt: DXGI_FORMAT_R32_FLOAT.0, + }, + two_plane: true, + }), + _ => None, + } +} + +/// Which plane(s) a view or barrier targets. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum PlaneMask { + Depth, + Stencil, + Both, +} + +impl PlaneMask { + fn includes_depth(self) -> bool { + matches!(self, PlaneMask::Depth | PlaneMask::Both) + } + + fn includes_stencil(self) -> bool { + matches!(self, PlaneMask::Stencil | PlaneMask::Both) + } +} + +/// Determine whether a UAV description targets the stencil plane of a two-plane depth resource. +pub(super) fn is_stencil_uav_format(fmt: u32, plane_slice: u32) -> bool { + plane_slice == 1 || matches!(fmt, 22 | 47 | 60..=64 | 120) +} + +/// Map a requested console/PC UAV format to a legal typed format on the chosen color twin. +pub(super) fn twin_uav_format(twin_typeless_fmt: i32, requested_fmt: u32) -> i32 { + match twin_typeless_fmt { + // R8_TYPELESS stencil twin + 60 => match requested_fmt { + 61 => DXGI_FORMAT_R8_UNORM.0, + 63 => DXGI_FORMAT_R8_SNORM.0, + 64 => DXGI_FORMAT_R8_SINT.0, + _ => DXGI_FORMAT_R8_UINT.0, + }, + // R16_TYPELESS depth twin + 53 => match requested_fmt { + 54 => DXGI_FORMAT_R16_FLOAT.0, + 57 => DXGI_FORMAT_R16_UINT.0, + 59 => DXGI_FORMAT_R16_SINT.0, + _ => DXGI_FORMAT_R16_UNORM.0, + }, + // R32_TYPELESS depth twin + 39 => match requested_fmt { + 42 => DXGI_FORMAT_R32_UINT.0, + 43 => DXGI_FORMAT_R32_SINT.0, + _ => DXGI_FORMAT_R32_FLOAT.0, + }, + other => other, + } +} + +/// Classify which plane(s) a D3D12 subresource index belongs to. +pub(super) fn plane_for_subresource(sub: u32, mips: u32, array_size: u32, two_plane: bool) -> PlaneMask { + if !two_plane { + return PlaneMask::Depth; + } + if sub == D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES { + return PlaneMask::Both; + } + let per_plane = mips.max(1).saturating_mul(array_size.max(1)); + if sub >= per_plane { + PlaneMask::Stencil + } else { + PlaneMask::Depth + } +} + +struct Entry { + device: usize, + plan: TwinPlan, + width: u64, + height: u32, + array_size: u16, + mip_levels: u16, + depth_twin: Option, + stencil_twin: Option, + /// Last known PC resource state per subresource for plane 0 (depth) and plane 1 (stencil). + pc_states_depth: Vec, + pc_states_stencil: Vec, + /// True when the color twin may contain newer UAV writes than the depth-stencil resource. + depth_twin_dirty: bool, + stencil_twin_dirty: bool, + /// True when the depth-stencil resource may contain newer DSV/clear writes than the twin. + depth_plane_dirty: bool, + stencil_plane_dirty: bool, + /// True per subresource if a split barrier (`BEGIN_ONLY`) involving `UNORDERED_ACCESS` was suppressed. + split_uav_depth: Vec, + split_uav_stencil: Vec, + /// Lazy DSV/SRV/UAV descriptor heaps for `FullscreenPass` sync on `R24G8_TYPELESS`. + pass_dsv_heap: Option, + pass_srv_uav_heap: Option, +} + +// D3D12 resources and descriptor heaps are free-threaded. +unsafe impl Send for Entry {} + +impl Entry { + fn new(device: usize, plan: TwinPlan, width: u64, height: u32, array_size: u16, mip_levels: u16, initial_pc_state: u32, started_as_uav: bool) -> Self { + let array_size = array_size.max(1); + let mip_levels = mip_levels.max(1); + let per_plane = (mip_levels as usize).saturating_mul(array_size as usize).max(1); + let stencil_len = if plan.two_plane { per_plane } else { 0 }; + Self { + device, + plan, + width, + height, + array_size, + mip_levels, + depth_twin: None, + stencil_twin: None, + pc_states_depth: vec![initial_pc_state; per_plane], + pc_states_stencil: vec![initial_pc_state; stencil_len], + depth_twin_dirty: started_as_uav, + stencil_twin_dirty: started_as_uav && plan.two_plane, + depth_plane_dirty: !started_as_uav && (initial_pc_state & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0), + stencil_plane_dirty: !started_as_uav && plan.two_plane && (initial_pc_state & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0), + split_uav_depth: vec![false; per_plane], + split_uav_stencil: vec![false; stencil_len], + pass_dsv_heap: None, + pass_srv_uav_heap: None, + } + } + + fn subresources_per_plane(&self) -> u32 { + (self.mip_levels as u32).saturating_mul(self.array_size as u32).max(1) + } + + fn local_subs_for(&self, subresource: u32, plane: u32) -> std::ops::Range { + let per_plane = self.subresources_per_plane(); + if subresource == D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES { + return 0..per_plane as usize; + } + let base = plane * per_plane; + if subresource >= base && subresource < base + per_plane { + let idx = (subresource - base) as usize; + idx..idx + 1 + } else { + 0..0 + } + } +} + +/// Action queued by `filter_barrier` to run on the command list. +/// Each `(local_sub, before_state, after_state)` entry names the exact plane-local subresource +/// index and its tracked PC `StateBefore` / `StateAfter` so partial subresource barriers and +/// per-plane states never corrupt sibling subresources or planes. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) enum SyncOp { + TwinToDepth { + res: usize, + depth: Vec<(u32, u32, u32)>, + stencil: Vec<(u32, u32, u32)>, + }, + DepthToTwin { + res: usize, + depth: Vec<(u32, u32, u32)>, + stencil: Vec<(u32, u32, u32)>, + }, + TransitionOnly { + res: usize, + depth: Vec<(u32, u32, u32)>, + stencil: Vec<(u32, u32, u32)>, + }, +} + +static ENTRIES: Mutex>> = Mutex::new(None); +static COUNT: AtomicUsize = AtomicUsize::new(0); +/// DSV CPU handle -> `(resource raw pointer, D3D12_DSV_FLAGS)`. +static DSV_TO_RES: Mutex>> = Mutex::new(None); + +/// Whether Depth+UAV color twins are enabled (`XCL_DEPTH_UAV_TWIN=0` turns them off). +pub fn enabled() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| !std::env::var("XCL_DEPTH_UAV_TWIN").is_ok_and(|v| v.trim() == "0")) +} + +pub fn any() -> bool { + COUNT.load(Ordering::Relaxed) != 0 +} + +/// Register a newly created console depth+UAV texture (`flags & 6 == 6`, single-sample, 2D). +pub(super) fn register_if_depth_uav(device: *mut c_void, res: *mut c_void, console_desc: &[u8; 0x38], initial_pc_state: u32, console_initial_state: u32) { + if !enabled() || device.is_null() || res.is_null() { + return; + } + let dim = u32::from_le_bytes(console_desc[0..4].try_into().unwrap()); + let flags = u32::from_le_bytes(console_desc[0x30..0x34].try_into().unwrap()); + let samples = u32::from_le_bytes(console_desc[0x24..0x28].try_into().unwrap()); + if dim != D3D12_RESOURCE_DIMENSION_TEXTURE2D.0 as u32 || (flags & (FLAG_DEPTH_STENCIL | FLAG_UAV)) != (FLAG_DEPTH_STENCIL | FLAG_UAV) || samples > 1 { + return; + } + let console_fmt = u32::from_le_bytes(console_desc[0x20..0x24].try_into().unwrap()); + let Some(plan) = plan_for_console_format(console_fmt) else { + return; + }; + let width = u64::from_le_bytes(console_desc[0x10..0x18].try_into().unwrap()); + let height = u32::from_le_bytes(console_desc[0x18..0x1C].try_into().unwrap()); + let array_size = u16::from_le_bytes(console_desc[0x1C..0x1E].try_into().unwrap()).max(1); + let mip_levels = u16::from_le_bytes(console_desc[0x1E..0x20].try_into().unwrap()).max(1); + let started_as_uav = console_initial_state & STATE_UAV != 0; + + let mut g = ENTRIES.lock().unwrap(); + let map = g.get_or_insert_with(HashMap::new); + let prev = map.insert( + res as usize, + Entry::new(device as usize, plan, width, height, array_size, mip_levels, initial_pc_state, started_as_uav), + ); + if prev.is_none() { + COUNT.fetch_add(1, Ordering::Relaxed); + } + static LOGGED: AtomicU64 = AtomicU64::new(0); + let n = LOGGED.fetch_add(1, Ordering::Relaxed); + if n < 8 || n.is_power_of_two() { + crate::xlog_info!( + "D3D12.X: tracked depth+UAV texture {res:p} ({width}x{height}x{array_size}, {mip_levels} mips, console fmt {console_fmt}, two_plane {}) for color-twin UAV sync ({} tracked)", + plan.two_plane, + map.len() + ); + } +} + +/// Remove tracking when a resource is released for good (twins go through `res_release` -> `late_release`). +pub(super) fn forget(res: usize) { + if !any() { + return; + } + let removed = { + let mut g = ENTRIES.lock().unwrap(); + let r = g.as_mut().and_then(|m| m.remove(&res)); + if r.is_some() { + COUNT.fetch_sub(1, Ordering::Relaxed); + } + r + }; + if let Some(mut e) = removed { + if let Some(m) = DSV_TO_RES.lock().unwrap().as_mut() { + m.retain(|_, &mut (v, _)| v != res); + } + // Defer release of internal unadopted pass descriptor heaps until in-flight GPU work completes. + unsafe { + if let Some(h) = e.pass_dsv_heap.take() { + super::late_release::hold(h.into_raw() as usize); + } + if let Some(h) = e.pass_srv_uav_heap.take() { + super::late_release::hold(h.into_raw() as usize); + } + } + drop(e); + } +} + +/// Record DSV handle -> `(resource, dsv_flags)` for `OMSetRenderTargets` and `ClearDepthStencilView` hooks. +pub(super) fn note_dsv(dsv_cpu: usize, res: usize, dsv_flags: u32) { + if !any() || dsv_cpu == 0 { + return; + } + let tracked = ENTRIES.lock().unwrap().as_ref().is_some_and(|m| m.contains_key(&res)); + let mut g = DSV_TO_RES.lock().unwrap(); + if tracked { + g.get_or_insert_with(HashMap::new).insert(dsv_cpu, (res, dsv_flags)); + } else if let Some(m) = g.as_mut() { + m.remove(&dsv_cpu); + } +} + +unsafe fn create_twin_resource(device: *mut c_void, width: u64, height: u32, array_size: u16, mip_levels: u16, format: i32) -> Option { + let heap = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }; + let desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Alignment: 0, + Width: width, + Height: height, + DepthOrArraySize: array_size, + MipLevels: mip_levels, + Format: DXGI_FORMAT(format), + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN, + Flags: D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + }; + let mut out: *mut c_void = std::ptr::null_mut(); + let hr = real::dev_create_committed_resource( + device, + &heap as *const _ as usize, + 0, + &desc as *const _ as usize, + STATE_UAV as usize, + 0, + &ID3D12Resource::IID as *const _ as *const windows_sys::core::GUID, + &mut out, + ); + if hr < 0 || out.is_null() { + static N: AtomicU64 = AtomicU64::new(0); + if N.fetch_add(1, Ordering::Relaxed) < 8 { + crate::xlog_warn!("D3D12.X: depth+UAV twin allocation ({width}x{height}x{array_size}, fmt {format}) failed ({hr:#x})"); + } + return None; + } + Some(ID3D12Resource::from_raw(out)) +} + +/// Route a `CreateUnorderedAccessView` on a tracked depth+UAV resource to its depth or stencil +/// color twin, returning `(twin_raw_ptr, patched_uav_desc)` when handled. +pub(super) unsafe fn resolve_uav( + dev: *mut c_void, + res: *mut c_void, + orig_desc: Option<&D3D12_UNORDERED_ACCESS_VIEW_DESC>, +) -> Option<(*mut c_void, D3D12_UNORDERED_ACCESS_VIEW_DESC)> { + if !any() || res.is_null() { + return None; + } + let key = res as usize; + let plane_slice = orig_desc.map_or(0, |d| match d.ViewDimension { + D3D12_UAV_DIMENSION_TEXTURE2D => d.Anonymous.Texture2D.PlaneSlice, + D3D12_UAV_DIMENSION_TEXTURE2DARRAY => d.Anonymous.Texture2DArray.PlaneSlice, + _ => 0, + }); + let req_fmt = orig_desc.map_or(0, |d| d.Format.0 as u32); + + // Inspect plan and check whether the twin already exists before allocating outside the lock. + let (need_alloc, is_stencil, twin_fmt, width, height, array_size, mip_levels, device, existing_raw) = { + let g = ENTRIES.lock().unwrap(); + let e = g.as_ref()?.get(&key)?; + let stencil = e.plan.two_plane && is_stencil_uav_format(req_fmt, plane_slice); + let twin_fmt = if stencil { e.plan.stencil_twin_fmt? } else { e.plan.depth_twin_fmt }; + let existing = if stencil { + e.stencil_twin.as_ref().map(|r| r.as_raw()) + } else { + e.depth_twin.as_ref().map(|r| r.as_raw()) + }; + (existing.is_none(), stencil, twin_fmt, e.width, e.height, e.array_size, e.mip_levels, if dev.is_null() { e.device as *mut c_void } else { dev }, existing) + }; + + let twin_raw = if let Some(raw) = existing_raw { + raw + } else if need_alloc { + let created = create_twin_resource(device, width, height, array_size, mip_levels, twin_fmt)?; + let mut g = ENTRIES.lock().unwrap(); + let Some(e) = g.as_mut().and_then(|m| m.get_mut(&key)) else { + return None; + }; + let slot = if is_stencil { &mut e.stencil_twin } else { &mut e.depth_twin }; + if slot.is_none() { + *slot = Some(created); + } + slot.as_ref()?.as_raw() + } else { + return None; + }; + + let patched_fmt = twin_uav_format(twin_fmt, req_fmt); + let mut patched = match orig_desc { + Some(d) => *d, + None => { + let mut d = D3D12_UNORDERED_ACCESS_VIEW_DESC::default(); + if array_size > 1 { + d.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; + d.Anonymous.Texture2DArray = D3D12_TEX2D_ARRAY_UAV { + MipSlice: 0, + FirstArraySlice: 0, + ArraySize: array_size as u32, + PlaneSlice: 0, + }; + } else { + d.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; + d.Anonymous.Texture2D = D3D12_TEX2D_UAV { MipSlice: 0, PlaneSlice: 0 }; + } + d + } + }; + patched.Format = DXGI_FORMAT(patched_fmt); + match patched.ViewDimension { + D3D12_UAV_DIMENSION_TEXTURE2D => patched.Anonymous.Texture2D.PlaneSlice = 0, + D3D12_UAV_DIMENSION_TEXTURE2DARRAY => patched.Anonymous.Texture2DArray.PlaneSlice = 0, + _ => {} + } + + static ROUTED: AtomicU64 = AtomicU64::new(0); + let n = ROUTED.fetch_add(1, Ordering::Relaxed); + if n < 8 || n.is_power_of_two() { + crate::xlog_info!( + "D3D12.X: routed {} UAV of depth+UAV texture {res:p} (req fmt {req_fmt} -> twin fmt {patched_fmt}) to color twin {twin_raw:p} ({} routed)", + if is_stencil { "stencil" } else { "depth" }, + n + 1 + ); + } + Some((twin_raw, patched)) +} + +/// Pure state-machine transition decision for a single barrier on a tracked depth+UAV texture. +/// Returns `(pass_through_barrier, needs_global_uav_barrier, optional_sync_op)`. +fn decide_transition( + e: &mut Entry, + subresource: u32, + before: u32, + after: u32, + flags: u32, + is_direct: bool, +) -> (bool, bool, Option) { + const BARRIER_BEGIN_ONLY: u32 = 1; + const BARRIER_END_ONLY: u32 = 2; + + let planes = plane_for_subresource(subresource, e.mip_levels as u32, e.array_size as u32, e.plan.two_plane); + let d_range = if planes.includes_depth() { e.local_subs_for(subresource, 0) } else { 0..0 }; + let s_range = if e.plan.two_plane && planes.includes_stencil() { e.local_subs_for(subresource, 1) } else { 0..0 }; + let touches_uav = (before | after) & STATE_UAV != 0; + + // Non-DIRECT lists (COMPUTE / COPY / BUNDLE) cannot transition a depth-stencil resource + // to/from DEPTH_* or UNORDERED_ACCESS; suppress invalid barriers and defer sync to DIRECT. + if !is_direct { + if touches_uav { + if after & STATE_UAV != 0 { + if !d_range.is_empty() && e.depth_twin.is_some() { + e.depth_twin_dirty = true; + } + if !s_range.is_empty() && e.stencil_twin.is_some() { + e.stencil_twin_dirty = true; + } + } + return (false, true, None); + } + return (false, false, None); + } + + // Handle split barriers touching UNORDERED_ACCESS: suppress BEGIN_ONLY and perform the + // complete atomic transition + sync at END_ONLY. + if flags == BARRIER_BEGIN_ONLY { + if touches_uav { + for s in d_range { + e.split_uav_depth[s] = true; + } + for s in s_range { + e.split_uav_stencil[s] = true; + } + return (false, false, None); + } + return (true, false, None); + } + if flags == BARRIER_END_ONLY { + let was_split_uav = d_range.clone().any(|s| e.split_uav_depth[s]) || s_range.clone().any(|s| e.split_uav_stencil[s]); + if !touches_uav && !was_split_uav { + for s in d_range.clone() { + e.pc_states_depth[s] = after; + } + if !d_range.is_empty() && after & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0 { + e.depth_plane_dirty = true; + } + for s in s_range.clone() { + e.pc_states_stencil[s] = after; + } + if !s_range.is_empty() && after & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0 { + e.stencil_plane_dirty = true; + } + return (true, false, None); + } + for s in d_range.clone() { + e.split_uav_depth[s] = false; + } + for s in s_range.clone() { + e.split_uav_stencil[s] = false; + } + } + + let collect_subs = |states: &mut [u32], range: std::ops::Range, target: u32| -> Vec<(u32, u32, u32)> { + let mut v = Vec::with_capacity(range.len()); + for s in range { + let prev = states[s]; + states[s] = target; + v.push((s as u32, prev, target)); + } + v + }; + + let leaving_uav = before & STATE_UAV != 0 && after & STATE_UAV == 0; + let entering_uav = before & STATE_UAV == 0 && after & STATE_UAV != 0; + + if leaving_uav { + let need_d = !d_range.is_empty() && e.depth_twin.is_some(); + let need_s = !s_range.is_empty() && e.stencil_twin.is_some(); + let depth = collect_subs(&mut e.pc_states_depth, d_range.clone(), after); + let stencil = collect_subs(&mut e.pc_states_stencil, s_range.clone(), after); + if !d_range.is_empty() { + e.depth_twin_dirty = false; + if after & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0 { + e.depth_plane_dirty = true; + } + } + if !s_range.is_empty() { + e.stencil_twin_dirty = false; + if after & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0 { + e.stencil_plane_dirty = true; + } + } + let op = if need_d || need_s { + Some(SyncOp::TwinToDepth { res: 0, depth, stencil }) + } else { + Some(SyncOp::TransitionOnly { res: 0, depth, stencil }) + }; + return (false, true, op); + } + + if entering_uav { + // Transition depth resource from each subresource's tracked PC state (`prev`) to + // `DEPTH_WRITE` as its resting state while UAV is active. + let need_d = !d_range.is_empty() && e.depth_twin.is_some() && (e.depth_plane_dirty || before & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0); + let need_s = !s_range.is_empty() && e.stencil_twin.is_some() && (e.stencil_plane_dirty || before & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0); + let depth = collect_subs(&mut e.pc_states_depth, d_range.clone(), STATE_DEPTH_WRITE); + let stencil = collect_subs(&mut e.pc_states_stencil, s_range.clone(), STATE_DEPTH_WRITE); + if !d_range.is_empty() { + e.depth_twin_dirty = e.depth_twin.is_some(); + e.depth_plane_dirty = false; + } + if !s_range.is_empty() { + e.stencil_twin_dirty = e.stencil_twin.is_some(); + e.stencil_plane_dirty = false; + } + let op = if need_d || need_s { + Some(SyncOp::DepthToTwin { res: 0, depth, stencil }) + } else { + Some(SyncOp::TransitionOnly { res: 0, depth, stencil }) + }; + return (false, true, op); + } + + // Neither before nor after is UNORDERED_ACCESS. If a twin became dirty (e.g. on an async + // compute list), flush it into the depth resource now as part of this transition. + let flush_d = !d_range.is_empty() && e.depth_twin_dirty && e.depth_twin.is_some(); + let flush_s = !s_range.is_empty() && e.stencil_twin_dirty && e.stencil_twin.is_some(); + let depth = collect_subs(&mut e.pc_states_depth, d_range.clone(), after); + let stencil = collect_subs(&mut e.pc_states_stencil, s_range.clone(), after); + if !d_range.is_empty() { + if flush_d { + e.depth_twin_dirty = false; + } + if after & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0 { + e.depth_plane_dirty = true; + } + } + if !s_range.is_empty() { + if flush_s { + e.stencil_twin_dirty = false; + } + if after & (STATE_DEPTH_WRITE | STATE_COPY_DEST) != 0 { + e.stencil_plane_dirty = true; + } + } + if flush_d || flush_s { + let op = SyncOp::TwinToDepth { res: 0, depth, stencil }; + return (false, true, Some(op)); + } + + (true, false, None) +} + +/// Inspect a PC transition barrier `[ty, flags, res_lo, res_hi, sub, before, after, _]` inside +/// `list_x::resource_barrier_x`. Returns `(pass_through, emit_global_uav_barrier)` and pushes +/// any required sync operation onto `ops`. +pub(super) fn filter_transition( + b: &[u32; 8], + is_direct: bool, + ops: &mut Vec, +) -> (bool, bool) { + if !any() { + return (true, false); + } + let res = (b[2] as u64 | (b[3] as u64) << 32) as usize; + let mut g = ENTRIES.lock().unwrap(); + let Some(e) = g.as_mut().and_then(|m| m.get_mut(&res)) else { + return (true, false); + }; + let (pass, uav, op) = decide_transition(e, b[4], b[5], b[6], b[1], is_direct); + if let Some(mut op) = op { + match &mut op { + SyncOp::TwinToDepth { res: r, .. } | SyncOp::DepthToTwin { res: r, .. } | SyncOp::TransitionOnly { res: r, .. } => *r = res, + } + ops.push(op); + } + (pass, uav) +} + +/// Whether a `D3D12_BARRIER_LAYOUT` is an unordered-access layout (generic or queue-specific). +fn is_uav_layout(layout: D3D12_BARRIER_LAYOUT) -> bool { + matches!( + layout, + D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS + | D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_UNORDERED_ACCESS + | D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_UNORDERED_ACCESS + ) +} + +/// Whether a `D3D12_BARRIER_LAYOUT` is a depth-write or copy-destination layout. +fn is_depth_write_or_copy_dest_layout(layout: D3D12_BARRIER_LAYOUT) -> bool { + matches!( + layout, + D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE + | D3D12_BARRIER_LAYOUT_COPY_DEST + | D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COPY_DEST + | D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COPY_DEST + ) +} + +/// Map a `D3D12_BARRIER_LAYOUT` + `D3D12_BARRIER_ACCESS` pair to a valid PC `D3D12_RESOURCE_STATES` +/// value for a depth-stencil texture (never returning `STATE_UAV` or buffer-only state bits). +pub(super) fn enhanced_layout_to_pc_state(layout: D3D12_BARRIER_LAYOUT, access: D3D12_BARRIER_ACCESS) -> u32 { + let a = access.0 as u32; + let access_state = || -> u32 { + if a & (D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE.0 as u32) != 0 { + STATE_DEPTH_WRITE + } else if a & (D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ.0 as u32) != 0 { + STATE_DEPTH_READ + } else if a & (D3D12_BARRIER_ACCESS_SHADER_RESOURCE.0 as u32) != 0 { + STATE_SRV_ALL + } else if a & (D3D12_BARRIER_ACCESS_COPY_DEST.0 as u32) != 0 { + STATE_COPY_DEST + } else if a & (D3D12_BARRIER_ACCESS_COPY_SOURCE.0 as u32) != 0 { + STATE_COPY_SOURCE + } else { + STATE_COMMON + } + }; + match layout { + D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE => STATE_DEPTH_WRITE, + D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ => STATE_DEPTH_READ, + D3D12_BARRIER_LAYOUT_SHADER_RESOURCE + | D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_SHADER_RESOURCE + | D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_SHADER_RESOURCE => STATE_SRV_ALL, + D3D12_BARRIER_LAYOUT_COPY_SOURCE + | D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COPY_SOURCE + | D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COPY_SOURCE => STATE_COPY_SOURCE, + D3D12_BARRIER_LAYOUT_COPY_DEST + | D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COPY_DEST + | D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COPY_DEST => STATE_COPY_DEST, + D3D12_BARRIER_LAYOUT_GENERIC_READ + | D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_GENERIC_READ + | D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_GENERIC_READ => { + if a & (D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ.0 as u32) != 0 { + STATE_DEPTH_READ + } else { + STATE_SRV_ALL | STATE_COPY_SOURCE + } + } + D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS + | D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_UNORDERED_ACCESS + | D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_UNORDERED_ACCESS => STATE_DEPTH_WRITE, + _ => access_state(), + } +} + +/// Resolve an enhanced barrier's `D3D12_BARRIER_SUBRESOURCE_RANGE` into plane-local subresource +/// indices `(depth_subs, stencil_subs)`. +fn resolve_enhanced_subresources(e: &Entry, range: &D3D12_BARRIER_SUBRESOURCE_RANGE) -> (Vec, Vec) { + let mips = e.mip_levels as u32; + let slices = e.array_size as u32; + let two_plane = e.plan.two_plane; + if range.NumMipLevels == 0 { + let sub = range.IndexOrFirstMipLevel; + let planes = plane_for_subresource(sub, mips, slices, two_plane); + let d = if planes.includes_depth() { e.local_subs_for(sub, 0).map(|s| s as u32).collect() } else { Vec::new() }; + let s = if two_plane && planes.includes_stencil() { e.local_subs_for(sub, 1).map(|s| s as u32).collect() } else { Vec::new() }; + return (d, s); + } + let first_mip = range.IndexOrFirstMipLevel.min(mips); + let end_mip = range.IndexOrFirstMipLevel.saturating_add(range.NumMipLevels).min(mips); + let first_arr = range.FirstArraySlice.min(slices); + let num_arr = if range.NumArraySlices == 0 { 1 } else { range.NumArraySlices }; + let end_arr = range.FirstArraySlice.saturating_add(num_arr).min(slices); + let plane_count = if two_plane { 2u32 } else { 1u32 }; + let first_plane = range.FirstPlane.min(plane_count); + let num_planes = if range.NumPlanes == 0 { 1 } else { range.NumPlanes }; + let end_plane = range.FirstPlane.saturating_add(num_planes).min(plane_count); + + let mut depth = Vec::new(); + let mut stencil = Vec::new(); + for p in first_plane..end_plane { + let dst = if p == 0 { &mut depth } else { &mut stencil }; + for a in first_arr..end_arr { + for m in first_mip..end_mip { + dst.push(a * mips + m); + } + } + } + (depth, stencil) +} + +/// Pure state-machine transition decision for an enhanced `D3D12_TEXTURE_BARRIER` (`#146`) on a +/// tracked depth+UAV texture. +#[allow(clippy::too_many_arguments)] +fn decide_enhanced_texture_barrier( + e: &mut Entry, + access_before: D3D12_BARRIER_ACCESS, + access_after: D3D12_BARRIER_ACCESS, + layout_before: D3D12_BARRIER_LAYOUT, + layout_after: D3D12_BARRIER_LAYOUT, + subresources: &D3D12_BARRIER_SUBRESOURCE_RANGE, + flags: D3D12_TEXTURE_BARRIER_FLAGS, + is_direct: bool, +) -> Option { + let (d_subs, s_subs) = resolve_enhanced_subresources(e, subresources); + if d_subs.is_empty() && s_subs.is_empty() { + return None; + } + let ab = access_before.0 as u32; + let aa = access_after.0 as u32; + let uav_bit = D3D12_BARRIER_ACCESS_UNORDERED_ACCESS.0 as u32; + let write_bits = (D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE.0 as u32) | (D3D12_BARRIER_ACCESS_COPY_DEST.0 as u32); + let discard_before = (flags.0 as u32) & (D3D12_TEXTURE_BARRIER_FLAG_DISCARD.0 as u32) != 0 || layout_before == D3D12_BARRIER_LAYOUT_UNDEFINED; + + let common_before = ab == 0 && matches!(layout_before, D3D12_BARRIER_LAYOUT_COMMON | D3D12_BARRIER_LAYOUT_DIRECT_QUEUE_COMMON | D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_COMMON); + let uav_before = (ab & uav_bit) != 0 || is_uav_layout(layout_before); + let uav_after = (aa & uav_bit) != 0 || is_uav_layout(layout_after); + let write_before = (ab & write_bits) != 0 || is_depth_write_or_copy_dest_layout(layout_before); + let write_after = (aa & write_bits) != 0 || is_depth_write_or_copy_dest_layout(layout_after); + let after_pc = enhanced_layout_to_pc_state(layout_after, access_after); + + if !is_direct { + if uav_before || uav_after || common_before { + if !d_subs.is_empty() && e.depth_twin.is_some() && !e.depth_plane_dirty { + e.depth_twin_dirty = true; + } + if !s_subs.is_empty() && e.stencil_twin.is_some() && !e.stencil_plane_dirty { + e.stencil_twin_dirty = true; + } + } + return None; + } + + let collect_subs = |states: &mut [u32], subs: &[u32], target: u32| -> Vec<(u32, u32, u32)> { + let mut v = Vec::with_capacity(subs.len()); + for &s in subs { + let idx = s as usize; + if idx < states.len() { + let prev = states[idx]; + states[idx] = target; + v.push((s, prev, target)); + } + } + v + }; + + if uav_before && !uav_after { + let need_d = !discard_before && !d_subs.is_empty() && e.depth_twin.is_some(); + let need_s = !discard_before && !s_subs.is_empty() && e.stencil_twin.is_some(); + let depth = collect_subs(&mut e.pc_states_depth, &d_subs, after_pc); + let stencil = collect_subs(&mut e.pc_states_stencil, &s_subs, after_pc); + if !d_subs.is_empty() { + e.depth_twin_dirty = false; + if write_after { + e.depth_plane_dirty = true; + } + } + if !s_subs.is_empty() { + e.stencil_twin_dirty = false; + if write_after { + e.stencil_plane_dirty = true; + } + } + return if need_d || need_s { + Some(SyncOp::TwinToDepth { res: 0, depth, stencil }) + } else if depth.iter().any(|&(_, b, a)| b != a) || stencil.iter().any(|&(_, b, a)| b != a) { + Some(SyncOp::TransitionOnly { res: 0, depth, stencil }) + } else { + None + }; + } + + if !uav_before && uav_after { + let need_d = !discard_before && !d_subs.is_empty() && e.depth_twin.is_some() && (e.depth_plane_dirty || write_before); + let need_s = !discard_before && !s_subs.is_empty() && e.stencil_twin.is_some() && (e.stencil_plane_dirty || write_before); + let depth = collect_subs(&mut e.pc_states_depth, &d_subs, STATE_DEPTH_WRITE); + let stencil = collect_subs(&mut e.pc_states_stencil, &s_subs, STATE_DEPTH_WRITE); + if !d_subs.is_empty() { + e.depth_twin_dirty = e.depth_twin.is_some(); + e.depth_plane_dirty = false; + } + if !s_subs.is_empty() { + e.stencil_twin_dirty = e.stencil_twin.is_some(); + e.stencil_plane_dirty = false; + } + return if need_d || need_s { + Some(SyncOp::DepthToTwin { res: 0, depth, stencil }) + } else if depth.iter().any(|&(_, b, a)| b != a) || stencil.iter().any(|&(_, b, a)| b != a) { + Some(SyncOp::TransitionOnly { res: 0, depth, stencil }) + } else { + None + }; + } + + if uav_before && uav_after { + if !d_subs.is_empty() && e.depth_twin.is_some() { + e.depth_twin_dirty = true; + } + if !s_subs.is_empty() && e.stencil_twin.is_some() { + e.stencil_twin_dirty = true; + } + return None; + } + + // Neither before nor after explicitly names UAV. Flush dirty twins (including compute-only + // twins transitioning out of COMMON access) unless discarded, and advance tracked PC state. + let flush_d = !discard_before && !d_subs.is_empty() && e.depth_twin.is_some() && (e.depth_twin_dirty || (common_before && !e.depth_plane_dirty)); + let flush_s = !discard_before && !s_subs.is_empty() && e.stencil_twin.is_some() && (e.stencil_twin_dirty || (common_before && !e.stencil_plane_dirty)); + let depth = collect_subs(&mut e.pc_states_depth, &d_subs, after_pc); + let stencil = collect_subs(&mut e.pc_states_stencil, &s_subs, after_pc); + if !d_subs.is_empty() { + if flush_d || discard_before { + e.depth_twin_dirty = false; + } + if write_after { + e.depth_plane_dirty = true; + } else if discard_before { + e.depth_plane_dirty = false; + } + } + if !s_subs.is_empty() { + if flush_s || discard_before { + e.stencil_twin_dirty = false; + } + if write_after { + e.stencil_plane_dirty = true; + } else if discard_before { + e.stencil_plane_dirty = false; + } + } + if flush_d || flush_s { + return Some(SyncOp::TwinToDepth { res: 0, depth, stencil }); + } + if depth.iter().any(|&(_, b, a)| b != a) || stencil.iter().any(|&(_, b, a)| b != a) { + return Some(SyncOp::TransitionOnly { res: 0, depth, stencil }); + } + None +} + +/// Inspect `ID3D12GraphicsCommandList7::Barrier` (`#146`) barrier groups for tracked depth+UAV +/// textures. Queues any immediate `SyncOp`s on `ops` and returns `true` if a global (or unparsed) +/// UAV barrier was present so the caller should also call `on_global_uav_barrier()`. +pub(super) unsafe fn filter_enhanced_barriers( + n: u32, + groups: *const c_void, + is_direct: bool, + ops: &mut Vec, +) -> bool { + if !any() { + return false; + } + if n == 0 || groups.is_null() { + return true; + } + let group_count = (n as usize).min(256); + let group_bytes = group_count.saturating_mul(std::mem::size_of::()); + if !crate::gpu::shader::readable(groups as usize, group_bytes) { + return true; + } + let slice = std::slice::from_raw_parts(groups as *const D3D12_BARRIER_GROUP, group_count); + let mut had_global_uav = false; + for grp in slice { + let count = (grp.NumBarriers as usize).min(4096); + match grp.Type { + D3D12_BARRIER_TYPE_GLOBAL => { + if count == 0 { + continue; + } + let ptr = grp.Anonymous.pGlobalBarriers; + let bytes = count.saturating_mul(std::mem::size_of::()); + if ptr.is_null() || !crate::gpu::shader::readable(ptr as usize, bytes) { + had_global_uav = true; + continue; + } + let gbs = std::slice::from_raw_parts(ptr, count); + for gb in gbs { + let ab = gb.AccessBefore.0 as u32; + if ab == 0 || ab & (D3D12_BARRIER_ACCESS_UNORDERED_ACCESS.0 as u32) != 0 { + had_global_uav = true; + break; + } + } + } + D3D12_BARRIER_TYPE_TEXTURE => { + if count == 0 { + continue; + } + let ptr = grp.Anonymous.pTextureBarriers; + let bytes = count.saturating_mul(std::mem::size_of::()); + if ptr.is_null() || !crate::gpu::shader::readable(ptr as usize, bytes) { + had_global_uav = true; + continue; + } + let tbs = std::slice::from_raw_parts(ptr, count); + let mut g = ENTRIES.lock().unwrap(); + let Some(map) = g.as_mut() else { + continue; + }; + for tb in tbs { + let res: usize = std::mem::transmute_copy(&tb.pResource); + if res == 0 { + continue; + } + let Some(e) = map.get_mut(&res) else { + continue; + }; + if let Some(mut op) = decide_enhanced_texture_barrier( + e, + tb.AccessBefore, + tb.AccessAfter, + tb.LayoutBefore, + tb.LayoutAfter, + &tb.Subresources, + tb.Flags, + is_direct, + ) { + match &mut op { + SyncOp::TwinToDepth { res: r, .. } | SyncOp::DepthToTwin { res: r, .. } | SyncOp::TransitionOnly { res: r, .. } => *r = res, + } + ops.push(op); + } + } + } + D3D12_BARRIER_TYPE_BUFFER => {} + _ => { + had_global_uav = true; + } + } + } + had_global_uav +} + +/// Apply a global UAV barrier to a single entry: mark materialized twins dirty only when their +/// corresponding depth/stencil plane is not currently graphics-dirty (avoiding clobbering +/// graphics-rendered depth targets on unrelated global UAV barriers). +fn mark_global_uav_dirty(e: &mut Entry) { + if e.depth_twin.is_some() && !e.depth_plane_dirty { + e.depth_twin_dirty = true; + } + if e.stencil_twin.is_some() && !e.stencil_plane_dirty { + e.stencil_twin_dirty = true; + } +} + +/// Mark materialized twins dirty on a global UAV barrier (`#146` global barrier or `#26` wildcard +/// `pResource == NULL` UAV barrier) for tracked depth+UAV resources whose depth/stencil plane is +/// not currently graphics-dirty, so compute-written depth+UAV targets flush on subsequent DSV bind. +pub(super) fn on_global_uav_barrier() { + if !any() { + return; + } + let mut g = ENTRIES.lock().unwrap(); + let Some(map) = g.as_mut() else { + return; + }; + for e in map.values_mut() { + mark_global_uav_dirty(e); + } +} + +/// Route a resource-specific UAV barrier targeting a tracked Depth+UAV resource to both its +/// materialized depth and stencil color twins, and mark those twins dirty so subsequent DSV/SRV +/// transitions flush their latest UAV writes back into the depth-stencil resource. +pub(super) fn on_uav_barrier(res: usize) -> [usize; 2] { + if !any() || res == 0 { + return [0, 0]; + } + let mut g = ENTRIES.lock().unwrap(); + let Some(e) = g.as_mut().and_then(|m| m.get_mut(&res)) else { + return [0, 0]; + }; + let d = match &e.depth_twin { + Some(t) => { + e.depth_twin_dirty = true; + t.as_raw() as usize + } + None => 0, + }; + let s = match &e.stencil_twin { + Some(t) => { + e.stencil_twin_dirty = true; + t.as_raw() as usize + } + None => 0, + }; + [d, s] +} + +/// Before `OMSetRenderTargets` binds `dsv_cpu`, flush any dirty color twin into the depth resource. +pub(super) unsafe fn before_bind_dsv(list: *mut c_void, dsv_cpu: usize) { + if !any() || dsv_cpu == 0 { + return; + } + if list_x::tracked_list_type(list).is_some_and(|t| t != D3D12_COMMAND_LIST_TYPE_DIRECT) { + return; + } + let Some((res, dsv_flags)) = DSV_TO_RES.lock().unwrap().as_ref().and_then(|m| m.get(&dsv_cpu).copied()) else { + return; + }; + let d_target_from_common = if dsv_flags & (D3D12_DSV_FLAG_READ_ONLY_DEPTH.0 as u32) != 0 { + STATE_DEPTH_READ + } else { + STATE_DEPTH_WRITE + }; + let s_target_from_common = if dsv_flags & (D3D12_DSV_FLAG_READ_ONLY_STENCIL.0 as u32) != 0 { + STATE_DEPTH_READ + } else { + STATE_DEPTH_WRITE + }; + let op = { + let mut g = ENTRIES.lock().unwrap(); + let Some(e) = g.as_mut().and_then(|m| m.get_mut(&res)) else { + return; + }; + let d = e.depth_twin_dirty && e.depth_twin.is_some(); + let s = e.stencil_twin_dirty && e.stencil_twin.is_some(); + if !d && !s { + return; + } + if d { + e.depth_twin_dirty = false; + } + if s { + e.stencil_twin_dirty = false; + } + let depth = if d { + e.pc_states_depth + .iter_mut() + .enumerate() + .map(|(sub, st)| { + let before = *st; + let after = if before == STATE_COMMON { d_target_from_common } else { before }; + *st = after; + (sub as u32, before, after) + }) + .collect() + } else { + Vec::new() + }; + let stencil = if s { + e.pc_states_stencil + .iter_mut() + .enumerate() + .map(|(sub, st)| { + let before = *st; + let after = if before == STATE_COMMON { s_target_from_common } else { before }; + *st = after; + (sub as u32, before, after) + }) + .collect() + } else { + Vec::new() + }; + SyncOp::TwinToDepth { res, depth, stencil } + }; + execute_syncs(list, &[op]); +} + +/// Record a `ClearDepthStencilView` on a tracked depth+UAV resource so cleared planes are marked +/// depth-dirty (and no longer wait for a stale twin flush). +pub(super) fn on_clear_dsv(dsv_cpu: usize, clear_flags: u32) { + if !any() || dsv_cpu == 0 { + return; + } + let Some((res, _)) = DSV_TO_RES.lock().unwrap().as_ref().and_then(|m| m.get(&dsv_cpu).copied()) else { + return; + }; + let mut g = ENTRIES.lock().unwrap(); + if let Some(e) = g.as_mut().and_then(|m| m.get_mut(&res)) { + if clear_flags & 1 != 0 { + e.depth_twin_dirty = false; + e.depth_plane_dirty = true; + } + if clear_flags & 2 != 0 && e.plan.two_plane { + e.stencil_twin_dirty = false; + e.stencil_plane_dirty = true; + } + } +} + +/// RAII guard for `D3D12_TEXTURE_COPY_LOCATION` so `ManuallyDrop>` is +/// always dropped cleanly without leaking COM references. +struct OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION); + +impl Drop for OwnedCopyLoc { + fn drop(&mut self) { + unsafe { ManuallyDrop::drop(&mut self.0.pResource) }; + } +} + +/// Transition specific plane-local subresources on `list` from their tracked `before` state to +/// `target_override` (if `Some`) or their tracked `after` state (if `None`). +unsafe fn transition_plane_subs( + list: *mut c_void, + res: usize, + two_plane: bool, + per_plane: u32, + plane: u32, + subs: &[(u32, u32, u32)], + target_override: Option, +) { + if subs.is_empty() { + return; + } + if !two_plane && subs.len() == per_plane as usize { + let first_b = subs[0].1; + let first_a = target_override.unwrap_or(subs[0].2); + if subs.iter().all(|&(_, b, a)| b == first_b && target_override.unwrap_or(a) == first_a) { + if first_b != first_a { + list_x::transition(list, res, D3D12_RESOURCE_STATES(first_b as i32), D3D12_RESOURCE_STATES(first_a as i32)); + } + return; + } + } + let base = plane * per_plane; + let mut barriers = Vec::with_capacity(subs.len()); + for &(s, before, after) in subs { + let dst = target_override.unwrap_or(after); + if before == dst { + continue; + } + barriers.push(D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: std::mem::transmute_copy(&res), + Subresource: base + s, + StateBefore: D3D12_RESOURCE_STATES(before as i32), + StateAfter: D3D12_RESOURCE_STATES(dst as i32), + }), + }, + }); + } + if !barriers.is_empty() { + list_x::pc_resource_barriers(list, &barriers); + } +} + +/// Transition specific plane-local subresources from a common intermediate `from_state` back to +/// each subresource's tracked `after` state. +unsafe fn transition_plane_from( + list: *mut c_void, + res: usize, + two_plane: bool, + per_plane: u32, + plane: u32, + from_state: u32, + subs: &[(u32, u32, u32)], +) { + if subs.is_empty() { + return; + } + if !two_plane && subs.len() == per_plane as usize { + let first_a = subs[0].2; + if subs.iter().all(|&(_, _, a)| a == first_a) { + if from_state != first_a { + list_x::transition(list, res, D3D12_RESOURCE_STATES(from_state as i32), D3D12_RESOURCE_STATES(first_a as i32)); + } + return; + } + } + let base = plane * per_plane; + let mut barriers = Vec::with_capacity(subs.len()); + for &(s, _, after) in subs { + if from_state == after { + continue; + } + barriers.push(D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: std::mem::transmute_copy(&res), + Subresource: base + s, + StateBefore: D3D12_RESOURCE_STATES(from_state as i32), + StateAfter: D3D12_RESOURCE_STATES(after as i32), + }), + }, + }); + } + if !barriers.is_empty() { + list_x::pc_resource_barriers(list, &barriers); + } +} + +/// Copy specific subresources (`subs` local indices) directly between two format-compatible +/// single-plane resources via `CopyTextureRegion` (or `CopyResource` if all subresources match). +unsafe fn copy_direct_subs( + list: *mut c_void, + dst_res: &ID3D12Resource, + src_res: &ID3D12Resource, + per_plane: u32, + subs: &[(u32, u32, u32)], +) { + if subs.len() == per_plane as usize { + list_x::pc_copy_resource(list, dst_res.as_raw() as usize, src_res.as_raw() as usize); + return; + } + for &(s, _, _) in subs { + let dst_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(dst_res.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: s }, + }); + let src_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(src_res.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: s }, + }); + list_x::pc_copy_texture_region(list, &dst_loc.0, 0, 0, 0, &src_loc.0, std::ptr::null()); + } +} + +/// Copy specific subresources (`subs`) on one plane (`plane`: 0 or 1) between `depth_res` and +/// `twin_res` via a staging buffer using `CopyTextureRegion`. +unsafe fn copy_plane_through_buffer( + list: *mut c_void, + dev: &ID3D12Device, + dst_res: &ID3D12Resource, + dst_plane: u32, + src_res: &ID3D12Resource, + src_plane: u32, + per_plane: u32, + subs: &[(u32, u32, u32)], +) -> bool { + let dst_desc = dst_res.GetDesc(); + let src_desc = src_res.GetDesc(); + for &(s, _, _) in subs { + let src_sub = src_plane * per_plane + s; + let dst_sub = dst_plane * per_plane + s; + let mut src_fp = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); + let mut dst_fp = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); + let mut src_rows = 0u32; + let mut dst_rows = 0u32; + let mut src_total = 0u64; + let mut dst_total = 0u64; + dev.GetCopyableFootprints(&src_desc, src_sub, 1, 0, Some(&mut src_fp), Some(&mut src_rows), None, Some(&mut src_total)); + dev.GetCopyableFootprints(&dst_desc, dst_sub, 1, 0, Some(&mut dst_fp), Some(&mut dst_rows), None, Some(&mut dst_total)); + if src_total == 0 + || src_total == u64::MAX + || dst_total == 0 + || dst_total == u64::MAX + || src_rows != dst_rows + || src_fp.Footprint.Width != dst_fp.Footprint.Width + || src_fp.Footprint.Height != dst_fp.Footprint.Height + { + return false; + } + // Plane 1 of a two-plane depth-stencil texture (e.g. R24G8_TYPELESS stencil) can report a + // larger row pitch (e.g. 512 B) than a standalone R8_TYPELESS twin (256 B). D3D12 allows + // increasing Footprint.RowPitch to any 256-byte multiple >= the reported minimum, so + // unifying both footprints to `max(src_pitch, dst_pitch)` keeps every row aligned identically. + let row_pitch = src_fp.Footprint.RowPitch.max(dst_fp.Footprint.RowPitch); + src_fp.Footprint.RowPitch = row_pitch; + dst_fp.Footprint.RowPitch = row_pitch; + let needed = (row_pitch as u64).saturating_mul(src_rows as u64).max(src_total).max(dst_total); + let Some(scratch_raw) = list_x::acquire_scratch_for_copy(list, needed) else { + return false; + }; + let scratch_ptr = scratch_raw as *mut c_void; + let Some(scratch) = ID3D12Resource::from_raw_borrowed(&scratch_ptr) else { + return false; + }; + list_x::transition(list, scratch_raw, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST); + { + let src_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(src_res.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: src_sub }, + }); + let buf_dst_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(scratch.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: src_fp }, + }); + list_x::pc_copy_texture_region(list, &buf_dst_loc.0, 0, 0, 0, &src_loc.0, std::ptr::null()); + } + + list_x::transition(list, scratch_raw, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE); + { + let buf_src_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(scratch.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: dst_fp }, + }); + let dst_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(dst_res.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: dst_sub }, + }); + list_x::pc_copy_texture_region(list, &dst_loc.0, 0, 0, 0, &buf_src_loc.0, std::ptr::null()); + } + + list_x::transition(list, scratch_raw, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_COMMON); + } + true +} + +// --------------------------------------------------------------------------------------- +// Internal fullscreen depth-write & compute depth-read pipelines for R24G8_TYPELESS +// (console D16_UNORM_S8_UINT / D24_UNORM_S8_UINT plane 0) +// --------------------------------------------------------------------------------------- + +struct SyncPipelines { + gfx_rs: ID3D12RootSignature, + gfx_pso: ID3D12PipelineState, + cs_rs: ID3D12RootSignature, + cs_pso: ID3D12PipelineState, +} + +unsafe impl Send for SyncPipelines {} + +static PIPELINES: Mutex>>> = Mutex::new(None); + +fn compile_sm5(src: &str, target: &str) -> Option> { + unsafe { + let mut blob = None; + let t = format!("{target}\0"); + let r = D3DCompile( + src.as_ptr() as *const c_void, + src.len(), + PCSTR::null(), + None, + None, + PCSTR(b"main\0".as_ptr()), + PCSTR(t.as_ptr()), + 0, + 0, + &mut blob, + None, + ); + match (r, blob) { + (Ok(()), Some(b)) => Some(std::slice::from_raw_parts(b.GetBufferPointer() as *const u8, b.GetBufferSize()).to_vec()), + _ => None, + } + } +} + +unsafe fn build_sync_pipelines(dev: &ID3D12Device) -> Option { + let vs = compile_sm5( + "float4 main(uint id : SV_VertexID) : SV_Position {\n\ + float2 p = float2((id == 2) ? 3.0 : -1.0, (id == 1) ? 3.0 : -1.0);\n\ + return float4(p, 0.0, 1.0);\n\ + }\n", + "vs_5_0", + )?; + let ps = compile_sm5( + "Texture2DArray src : register(t0);\n\ + cbuffer C : register(b0) { uint slice; uint mip; };\n\ + float main(float4 pos : SV_Position) : SV_Depth {\n\ + return src.Load(int4(int2(pos.xy), int(slice), int(mip)));\n\ + }\n", + "ps_5_0", + )?; + let cs = compile_sm5( + "Texture2DArray src : register(t0);\n\ + RWTexture2DArray dst : register(u0);\n\ + cbuffer C : register(b0) { uint w; uint h; uint slices; uint mip; };\n\ + [numthreads(8, 8, 1)]\n\ + void main(uint3 id : SV_DispatchThreadID) {\n\ + if (id.x < w && id.y < h && id.z < slices) {\n\ + dst[id] = src.Load(int4(int3(id), int(mip)));\n\ + }\n\ + }\n", + "cs_5_0", + )?; + + // Graphics RS: [0] 2 root constants (b0), [1] 1 SRV table (t0) + let srv_range = D3D12_DESCRIPTOR_RANGE { + RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_SRV, + NumDescriptors: 1, + BaseShaderRegister: 0, + RegisterSpace: 0, + OffsetInDescriptorsFromTableStart: 0, + }; + let gfx_params = [ + D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL, + Anonymous: D3D12_ROOT_PARAMETER_0 { + Constants: D3D12_ROOT_CONSTANTS { ShaderRegister: 0, RegisterSpace: 0, Num32BitValues: 2 }, + }, + }, + D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, + ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL, + Anonymous: D3D12_ROOT_PARAMETER_0 { + DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE { NumDescriptorRanges: 1, pDescriptorRanges: &srv_range }, + }, + }, + ]; + let gfx_rs_desc = D3D12_ROOT_SIGNATURE_DESC { + NumParameters: gfx_params.len() as u32, + pParameters: gfx_params.as_ptr(), + ..Default::default() + }; + let mut blob = None; + D3D12SerializeRootSignature(&gfx_rs_desc, D3D_ROOT_SIGNATURE_VERSION_1, &mut blob, None).ok()?; + let blob = blob?; + let dev_raw = dev.as_raw(); + type CreateRsFn = unsafe extern "system" fn(*mut c_void, u32, *const c_void, usize, *const windows_sys::core::GUID, *mut *mut c_void) -> i32; + let create_rs: CreateRsFn = std::mem::transmute(real::orig_fn(dev_raw, 16)); + let mut gfx_rs_raw = std::ptr::null_mut(); + if create_rs(dev_raw, 0, blob.GetBufferPointer(), blob.GetBufferSize(), &ID3D12RootSignature::IID as *const _ as *const _, &mut gfx_rs_raw) < 0 || gfx_rs_raw.is_null() { + return None; + } + let gfx_rs = ID3D12RootSignature::from_raw(gfx_rs_raw); + + // Compute RS: [0] 4 root constants (b0), [1] 1 SRV table (t0), [2] 1 UAV table (u0) + let uav_range = D3D12_DESCRIPTOR_RANGE { + RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_UAV, + NumDescriptors: 1, + BaseShaderRegister: 0, + RegisterSpace: 0, + OffsetInDescriptorsFromTableStart: 0, + }; + let cs_params = [ + D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + Anonymous: D3D12_ROOT_PARAMETER_0 { + Constants: D3D12_ROOT_CONSTANTS { ShaderRegister: 0, RegisterSpace: 0, Num32BitValues: 4 }, + }, + }, + D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + Anonymous: D3D12_ROOT_PARAMETER_0 { + DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE { NumDescriptorRanges: 1, pDescriptorRanges: &srv_range }, + }, + }, + D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + Anonymous: D3D12_ROOT_PARAMETER_0 { + DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE { NumDescriptorRanges: 1, pDescriptorRanges: &uav_range }, + }, + }, + ]; + let cs_rs_desc = D3D12_ROOT_SIGNATURE_DESC { + NumParameters: cs_params.len() as u32, + pParameters: cs_params.as_ptr(), + ..Default::default() + }; + let mut cs_blob = None; + D3D12SerializeRootSignature(&cs_rs_desc, D3D_ROOT_SIGNATURE_VERSION_1, &mut cs_blob, None).ok()?; + let cs_blob = cs_blob?; + let mut cs_rs_raw = std::ptr::null_mut(); + if create_rs(dev_raw, 0, cs_blob.GetBufferPointer(), cs_blob.GetBufferSize(), &ID3D12RootSignature::IID as *const _ as *const _, &mut cs_rs_raw) < 0 || cs_rs_raw.is_null() { + return None; + } + let cs_rs = ID3D12RootSignature::from_raw(cs_rs_raw); + + // Graphics PSO: write SV_Depth unconditionally, stencil disabled so plane 1 stays intact. + let mut gfx_desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { + pRootSignature: ManuallyDrop::new(Some(gfx_rs.clone())), + VS: D3D12_SHADER_BYTECODE { pShaderBytecode: vs.as_ptr() as *const c_void, BytecodeLength: vs.len() }, + PS: D3D12_SHADER_BYTECODE { pShaderBytecode: ps.as_ptr() as *const c_void, BytecodeLength: ps.len() }, + SampleMask: u32::MAX, + RasterizerState: D3D12_RASTERIZER_DESC { + FillMode: D3D12_FILL_MODE_SOLID, + CullMode: D3D12_CULL_MODE_NONE, + DepthClipEnable: false.into(), + ..Default::default() + }, + DepthStencilState: D3D12_DEPTH_STENCIL_DESC { + DepthEnable: true.into(), + DepthWriteMask: D3D12_DEPTH_WRITE_MASK_ALL, + DepthFunc: D3D12_COMPARISON_FUNC_ALWAYS, + StencilEnable: false.into(), + ..Default::default() + }, + PrimitiveTopologyType: D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, + NumRenderTargets: 0, + DSVFormat: DXGI_FORMAT_D24_UNORM_S8_UINT, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + ..Default::default() + }; + type CreateGfxPsoFn = unsafe extern "system" fn(*mut c_void, *const D3D12_GRAPHICS_PIPELINE_STATE_DESC, *const windows_sys::core::GUID, *mut *mut c_void) -> i32; + let create_gfx: CreateGfxPsoFn = std::mem::transmute(real::orig_fn(dev_raw, 10)); + let mut gfx_pso_raw = std::ptr::null_mut(); + let hr_gfx = create_gfx(dev_raw, &gfx_desc, &ID3D12PipelineState::IID as *const _ as *const _, &mut gfx_pso_raw); + ManuallyDrop::drop(&mut gfx_desc.pRootSignature); + if hr_gfx < 0 || gfx_pso_raw.is_null() { + return None; + } + let gfx_pso = ID3D12PipelineState::from_raw(gfx_pso_raw); + + let mut cs_desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: ManuallyDrop::new(Some(cs_rs.clone())), + CS: D3D12_SHADER_BYTECODE { pShaderBytecode: cs.as_ptr() as *const c_void, BytecodeLength: cs.len() }, + ..Default::default() + }; + type CreateCsPsoFn = unsafe extern "system" fn(*mut c_void, *const D3D12_COMPUTE_PIPELINE_STATE_DESC, *const windows_sys::core::GUID, *mut *mut c_void) -> i32; + let create_cs: CreateCsPsoFn = std::mem::transmute(real::orig_fn(dev_raw, 11)); + let mut cs_pso_raw = std::ptr::null_mut(); + let hr_cs = create_cs(dev_raw, &cs_desc, &ID3D12PipelineState::IID as *const _ as *const _, &mut cs_pso_raw); + ManuallyDrop::drop(&mut cs_desc.pRootSignature); + if hr_cs < 0 || cs_pso_raw.is_null() { + return None; + } + let cs_pso = ID3D12PipelineState::from_raw(cs_pso_raw); + + Some(SyncPipelines { gfx_rs, gfx_pso, cs_rs, cs_pso }) +} + +/// Ensure per-resource internal unadopted PC DSV + SRV/UAV descriptor heaps exist for `R24G8_TYPELESS` sync. +unsafe fn ensure_pass_heaps( + dev: &ID3D12Device, + depth_res: &ID3D12Resource, + twin_res: &ID3D12Resource, + e: &mut Entry, + twin_srv_fmt: i32, + twin_uav_fmt: i32, +) -> Option<(ID3D12DescriptorHeap, ID3D12DescriptorHeap)> { + let dev_raw = dev.as_raw(); + type CreateHeapFn = unsafe extern "system" fn(*mut c_void, *const D3D12_DESCRIPTOR_HEAP_DESC, *const windows_sys::core::GUID, *mut *mut c_void) -> i32; + type IncSizeFn = unsafe extern "system" fn(*mut c_void, D3D12_DESCRIPTOR_HEAP_TYPE) -> u32; + type CreateDsvFn = unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_DEPTH_STENCIL_VIEW_DESC, usize); + type CreateSrvFn = unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_SHADER_RESOURCE_VIEW_DESC, usize); + type CreateUavFn = unsafe extern "system" fn(*mut c_void, *mut c_void, *mut c_void, *const D3D12_UNORDERED_ACCESS_VIEW_DESC, usize); + let create_heap: CreateHeapFn = std::mem::transmute(real::orig_fn(dev_raw, 14)); + let inc_size: IncSizeFn = std::mem::transmute(real::orig_fn(dev_raw, 15)); + let create_srv: CreateSrvFn = std::mem::transmute(real::orig_fn(dev_raw, 18)); + let create_uav: CreateUavFn = std::mem::transmute(real::orig_fn(dev_raw, 19)); + let create_dsv: CreateDsvFn = std::mem::transmute(real::orig_fn(dev_raw, 21)); + + let mips = e.mip_levels as u32; + let slices = e.array_size as u32; + if e.pass_dsv_heap.is_none() { + let desc = D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_DSV, + NumDescriptors: mips * slices, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_NONE, + NodeMask: 0, + }; + let mut raw = std::ptr::null_mut(); + if create_heap(dev_raw, &desc, &ID3D12DescriptorHeap::IID as *const _ as *const _, &mut raw) < 0 || raw.is_null() { + return None; + } + let dsv_heap = ID3D12DescriptorHeap::from_raw(raw); + let inc = inc_size(dev_raw, D3D12_DESCRIPTOR_HEAP_TYPE_DSV) as usize; + let base = dsv_heap.GetCPUDescriptorHandleForHeapStart().ptr; + for m in 0..mips { + for a in 0..slices { + let dsv = D3D12_DEPTH_STENCIL_VIEW_DESC { + Format: DXGI_FORMAT_D24_UNORM_S8_UINT, + ViewDimension: D3D12_DSV_DIMENSION_TEXTURE2DARRAY, + Flags: D3D12_DSV_FLAG_NONE, + Anonymous: D3D12_DEPTH_STENCIL_VIEW_DESC_0 { + Texture2DArray: D3D12_TEX2D_ARRAY_DSV { MipSlice: m, FirstArraySlice: a, ArraySize: 1 }, + }, + }; + let idx = (m * slices + a) as usize; + create_dsv(dev_raw, depth_res.as_raw(), &dsv, base + idx * inc); + } + } + e.pass_dsv_heap = Some(dsv_heap); + } + if e.pass_srv_uav_heap.is_none() { + // Slot 0: twin SRV, Slot 1: depth plane 0 SRV (R24_UNORM_X8_TYPELESS), Slots 2..2+mips: twin UAV per mip + let total = 2 + mips; + let desc = D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + NumDescriptors: total, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, + NodeMask: 0, + }; + let mut raw = std::ptr::null_mut(); + if create_heap(dev_raw, &desc, &ID3D12DescriptorHeap::IID as *const _ as *const _, &mut raw) < 0 || raw.is_null() { + return None; + } + let heap = ID3D12DescriptorHeap::from_raw(raw); + let inc = inc_size(dev_raw, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize; + let base = heap.GetCPUDescriptorHandleForHeapStart().ptr; + let twin_srv = D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: DXGI_FORMAT(twin_srv_fmt), + ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2DARRAY, + Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + Texture2DArray: D3D12_TEX2D_ARRAY_SRV { MostDetailedMip: 0, MipLevels: mips, FirstArraySlice: 0, ArraySize: slices, PlaneSlice: 0, ResourceMinLODClamp: 0.0 }, + }, + }; + create_srv(dev_raw, twin_res.as_raw(), &twin_srv, base); + let depth_srv = D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: DXGI_FORMAT_R24_UNORM_X8_TYPELESS, + ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2DARRAY, + Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + Texture2DArray: D3D12_TEX2D_ARRAY_SRV { MostDetailedMip: 0, MipLevels: mips, FirstArraySlice: 0, ArraySize: slices, PlaneSlice: 0, ResourceMinLODClamp: 0.0 }, + }, + }; + create_srv(dev_raw, depth_res.as_raw(), &depth_srv, base + inc); + for m in 0..mips { + let uav = D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: DXGI_FORMAT(twin_uav_fmt), + ViewDimension: D3D12_UAV_DIMENSION_TEXTURE2DARRAY, + Anonymous: D3D12_UNORDERED_ACCESS_VIEW_DESC_0 { + Texture2DArray: D3D12_TEX2D_ARRAY_UAV { MipSlice: m, FirstArraySlice: 0, ArraySize: slices, PlaneSlice: 0 }, + }, + }; + create_uav(dev_raw, twin_res.as_raw(), std::ptr::null_mut(), &uav, base + (2 + m as usize) * inc); + } + e.pass_srv_uav_heap = Some(heap); + } + Some((e.pass_dsv_heap.clone()?, e.pass_srv_uav_heap.clone()?)) +} + +unsafe fn get_pipelines(dev: &ID3D12Device) -> Option<(ID3D12RootSignature, ID3D12PipelineState, ID3D12RootSignature, ID3D12PipelineState)> { + let key = dev.as_raw() as usize; + let mut g = PIPELINES.lock().unwrap(); + let map = g.get_or_insert_with(HashMap::new); + let entry = map.entry(key).or_insert_with(|| build_sync_pipelines(dev)); + let p = entry.as_ref()?; + Some((p.gfx_rs.clone(), p.gfx_pso.clone(), p.cs_rs.clone(), p.cs_pso.clone())) +} + +/// Execute queued depth+UAV synchronization ops on a `DIRECT` command list. +pub(super) unsafe fn execute_syncs(list: *mut c_void, ops: &[SyncOp]) { + if ops.is_empty() || list.is_null() { + return; + } + let Some(dev) = list_x::device_of_list(list) else { + return; + }; + let dev_raw = dev.as_raw(); + type IncSizeFn = unsafe extern "system" fn(*mut c_void, D3D12_DESCRIPTOR_HEAP_TYPE) -> u32; + let inc_size: IncSizeFn = std::mem::transmute(real::orig_fn(dev_raw, 15)); + let mut clobbered_gfx = false; + let mut clobbered_cs = false; + + for op in ops { + match op { + SyncOp::TransitionOnly { res, depth, stencil } => { + let res = *res; + let Some((two_plane, per_plane)) = ({ + let g = ENTRIES.lock().unwrap(); + g.as_ref().and_then(|m| m.get(&res)).map(|e| (e.plan.two_plane, e.subresources_per_plane())) + }) else { + continue; + }; + if !depth.is_empty() { + transition_plane_subs(list, res, two_plane, per_plane, 0, depth, None); + } + if two_plane && !stencil.is_empty() { + transition_plane_subs(list, res, two_plane, per_plane, 1, stencil, None); + } + } + SyncOp::TwinToDepth { res, depth, stencil } => { + let res = *res; + let res_ptr = res as *mut c_void; + let Some(depth_res) = ID3D12Resource::from_raw_borrowed(&res_ptr) else { + continue; + }; + let snapshot = { + let g = ENTRIES.lock().unwrap(); + g.as_ref().and_then(|m| m.get(&res)).map(|e| { + ( + e.plan, + e.width, + e.height, + e.array_size, + e.mip_levels, + e.subresources_per_plane(), + e.depth_twin.clone(), + e.stencil_twin.clone(), + ) + }) + }; + let Some((plan, width, height, array_size, mip_levels, per_plane, depth_twin, stencil_twin)) = snapshot else { + continue; + }; + + if !depth.is_empty() { + if let Some(twin) = depth_twin { + match plan.depth_sync { + DepthSyncKind::DirectCopy => { + let twin_raw = twin.as_raw() as usize; + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); + transition_plane_subs(list, res, false, per_plane, 0, depth, Some(STATE_COPY_DEST)); + copy_direct_subs(list, depth_res, &twin, per_plane, depth); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + transition_plane_from(list, res, false, per_plane, 0, STATE_COPY_DEST, depth); + } + DepthSyncKind::BufferThrough => { + let twin_raw = twin.as_raw() as usize; + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); + transition_plane_subs(list, res, true, per_plane, 0, depth, Some(STATE_COPY_DEST)); + let _ = copy_plane_through_buffer(list, &dev, depth_res, 0, &twin, 0, per_plane, depth); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + transition_plane_from(list, res, true, per_plane, 0, STATE_COPY_DEST, depth); + } + DepthSyncKind::FullscreenPass { twin_srv_fmt, twin_uav_fmt } => { + let heaps = { + let mut g = ENTRIES.lock().unwrap(); + g.as_mut().and_then(|m| m.get_mut(&res)).and_then(|e| ensure_pass_heaps(&dev, depth_res, &twin, e, twin_srv_fmt, twin_uav_fmt)) + }; + if let (Some((gfx_rs, gfx_pso, _, _)), Some((dsv_heap, srv_heap))) = (get_pipelines(&dev), heaps) { + let twin_raw = twin.as_raw() as usize; + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATES(STATE_SRV_ALL as i32)); + transition_plane_subs(list, res, true, per_plane, 0, depth, Some(STATE_DEPTH_WRITE)); + let srv_raw = srv_heap.as_raw() as usize; + let set_heaps: unsafe extern "system" fn(*mut c_void, u32, *const usize) = std::mem::transmute(real::orig_fn(list, 28)); + let set_gfx_rs: unsafe extern "system" fn(*mut c_void, *mut c_void) = std::mem::transmute(real::orig_fn(list, 30)); + let set_pso: unsafe extern "system" fn(*mut c_void, *mut c_void) = std::mem::transmute(real::orig_fn(list, 25)); + let set_topo: unsafe extern "system" fn(*mut c_void, u32) = std::mem::transmute(real::orig_fn(list, 20)); + let set_gfx_table: unsafe extern "system" fn(*mut c_void, u32, u64) = std::mem::transmute(real::orig_fn(list, 32)); + let set_vp: unsafe extern "system" fn(*mut c_void, u32, *const D3D12_VIEWPORT) = std::mem::transmute(real::orig_fn(list, 21)); + let set_sc: unsafe extern "system" fn(*mut c_void, u32, *const RECT) = std::mem::transmute(real::orig_fn(list, 22)); + let set_rt: unsafe extern "system" fn(*mut c_void, u32, *const usize, i32, *const usize) = std::mem::transmute(real::orig_fn(list, 46)); + let set_gfx_consts: unsafe extern "system" fn(*mut c_void, u32, u32, *const u32, u32) = std::mem::transmute(real::orig_fn(list, 36)); + let draw: unsafe extern "system" fn(*mut c_void, u32, u32, u32, u32) = std::mem::transmute(real::orig_fn(list, 12)); + + set_heaps(list, 1, &srv_raw); + set_gfx_rs(list, gfx_rs.as_raw()); + set_pso(list, gfx_pso.as_raw()); + set_topo(list, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST.0 as u32); + set_gfx_table(list, 1, srv_heap.GetGPUDescriptorHandleForHeapStart().ptr); + let dsv_base = dsv_heap.GetCPUDescriptorHandleForHeapStart().ptr; + let dsv_inc = inc_size(dev_raw, D3D12_DESCRIPTOR_HEAP_TYPE_DSV) as usize; + let mips_u32 = mip_levels as u32; + for &(local_sub, _, _) in depth { + let m = local_sub % mips_u32; + let a = local_sub / mips_u32; + let mw = ((width as u32) >> m).max(1); + let mh = (height >> m).max(1); + let vp = D3D12_VIEWPORT { TopLeftX: 0.0, TopLeftY: 0.0, Width: mw as f32, Height: mh as f32, MinDepth: 0.0, MaxDepth: 1.0 }; + let sc = RECT { left: 0, top: 0, right: mw as i32, bottom: mh as i32 }; + set_vp(list, 1, &vp); + set_sc(list, 1, &sc); + let dsv = dsv_base + ((m * array_size as u32 + a) as usize) * dsv_inc; + set_rt(list, 0, std::ptr::null(), 0, &dsv); + let c = [a, m]; + set_gfx_consts(list, 0, 2, c.as_ptr(), 0); + draw(list, 3, 1, 0, 0); + } + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATES(STATE_SRV_ALL as i32), D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + transition_plane_from(list, res, true, per_plane, 0, STATE_DEPTH_WRITE, depth); + clobbered_gfx = true; + } else { + transition_plane_subs(list, res, true, per_plane, 0, depth, None); + } + } + } + } else { + transition_plane_subs(list, res, plan.two_plane, per_plane, 0, depth, None); + } + } + + if plan.two_plane && !stencil.is_empty() { + if let Some(twin) = stencil_twin { + let twin_raw = twin.as_raw() as usize; + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); + transition_plane_subs(list, res, true, per_plane, 1, stencil, Some(STATE_COPY_DEST)); + let _ = copy_plane_through_buffer(list, &dev, depth_res, 1, &twin, 0, per_plane, stencil); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + transition_plane_from(list, res, true, per_plane, 1, STATE_COPY_DEST, stencil); + } else { + transition_plane_subs(list, res, true, per_plane, 1, stencil, None); + } + } + } + SyncOp::DepthToTwin { res, depth, stencil } => { + let res = *res; + let res_ptr = res as *mut c_void; + let Some(depth_res) = ID3D12Resource::from_raw_borrowed(&res_ptr) else { + continue; + }; + let snapshot = { + let g = ENTRIES.lock().unwrap(); + g.as_ref().and_then(|m| m.get(&res)).map(|e| { + ( + e.plan, + e.width, + e.height, + e.array_size, + e.mip_levels, + e.subresources_per_plane(), + e.depth_twin.clone(), + e.stencil_twin.clone(), + ) + }) + }; + let Some((plan, width, height, array_size, mip_levels, per_plane, depth_twin, stencil_twin)) = snapshot else { + continue; + }; + + if !depth.is_empty() { + if let Some(twin) = depth_twin { + match plan.depth_sync { + DepthSyncKind::DirectCopy => { + let twin_raw = twin.as_raw() as usize; + transition_plane_subs(list, res, false, per_plane, 0, depth, Some(STATE_COPY_SOURCE)); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST); + copy_direct_subs(list, &twin, depth_res, per_plane, depth); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + transition_plane_from(list, res, false, per_plane, 0, STATE_COPY_SOURCE, depth); + } + DepthSyncKind::BufferThrough => { + let twin_raw = twin.as_raw() as usize; + transition_plane_subs(list, res, true, per_plane, 0, depth, Some(STATE_COPY_SOURCE)); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST); + let _ = copy_plane_through_buffer(list, &dev, &twin, 0, depth_res, 0, per_plane, depth); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + transition_plane_from(list, res, true, per_plane, 0, STATE_COPY_SOURCE, depth); + } + DepthSyncKind::FullscreenPass { twin_srv_fmt, twin_uav_fmt } => { + let heaps = { + let mut g = ENTRIES.lock().unwrap(); + g.as_mut().and_then(|m| m.get_mut(&res)).and_then(|e| ensure_pass_heaps(&dev, depth_res, &twin, e, twin_srv_fmt, twin_uav_fmt)) + }; + if let (Some((_, _, cs_rs, cs_pso)), Some((_, srv_heap))) = (get_pipelines(&dev), heaps) { + transition_plane_subs(list, res, true, per_plane, 0, depth, Some(STATE_SRV_ALL)); + let srv_raw = srv_heap.as_raw() as usize; + let set_heaps: unsafe extern "system" fn(*mut c_void, u32, *const usize) = std::mem::transmute(real::orig_fn(list, 28)); + let set_cs_rs: unsafe extern "system" fn(*mut c_void, *mut c_void) = std::mem::transmute(real::orig_fn(list, 29)); + let set_pso: unsafe extern "system" fn(*mut c_void, *mut c_void) = std::mem::transmute(real::orig_fn(list, 25)); + let set_cs_table: unsafe extern "system" fn(*mut c_void, u32, u64) = std::mem::transmute(real::orig_fn(list, 31)); + let set_cs_consts: unsafe extern "system" fn(*mut c_void, u32, u32, *const u32, u32) = std::mem::transmute(real::orig_fn(list, 35)); + let dispatch: unsafe extern "system" fn(*mut c_void, u32, u32, u32) = std::mem::transmute(real::orig_fn(list, 14)); + + set_heaps(list, 1, &srv_raw); + set_cs_rs(list, cs_rs.as_raw()); + set_pso(list, cs_pso.as_raw()); + let gpu_base = srv_heap.GetGPUDescriptorHandleForHeapStart().ptr; + let inc = inc_size(dev_raw, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as u64; + set_cs_table(list, 1, gpu_base + inc); + let mips_u32 = mip_levels as u32; + let mut dispatched_mips = vec![false; mips_u32 as usize]; + for &(local_sub, _, _) in depth { + let m = local_sub % mips_u32; + if dispatched_mips[m as usize] { + continue; + } + dispatched_mips[m as usize] = true; + let mw = ((width as u32) >> m).max(1); + let mh = (height >> m).max(1); + let c = [mw, mh, array_size as u32, m]; + set_cs_consts(list, 0, 4, c.as_ptr(), 0); + set_cs_table(list, 2, gpu_base + (2 + m as u64) * inc); + dispatch(list, mw.div_ceil(8), mh.div_ceil(8), array_size as u32); + } + // Order internal compute UAV writes to `twin` before subsequent title dispatches. + let twin_raw = twin.as_raw() as usize; + let uav_barrier = D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_UAV, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + UAV: ManuallyDrop::new(D3D12_RESOURCE_UAV_BARRIER { + pResource: std::mem::transmute_copy(&twin_raw), + }), + }, + }; + list_x::pc_resource_barriers(list, &[uav_barrier]); + transition_plane_from(list, res, true, per_plane, 0, STATE_SRV_ALL, depth); + clobbered_cs = true; + } else { + transition_plane_subs(list, res, true, per_plane, 0, depth, None); + } + } + } + } else { + transition_plane_subs(list, res, plan.two_plane, per_plane, 0, depth, None); + } + } + + if plan.two_plane && !stencil.is_empty() { + if let Some(twin) = stencil_twin { + let twin_raw = twin.as_raw() as usize; + transition_plane_subs(list, res, true, per_plane, 1, stencil, Some(STATE_COPY_SOURCE)); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST); + let _ = copy_plane_through_buffer(list, &dev, &twin, 0, depth_res, 1, per_plane, stencil); + list_x::transition(list, twin_raw, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + transition_plane_from(list, res, true, per_plane, 1, STATE_COPY_SOURCE, stencil); + } else { + transition_plane_subs(list, res, true, per_plane, 1, stencil, None); + } + } + } + } + } + + if clobbered_gfx || clobbered_cs { + list_x::restore_after_internal_pass(list, clobbered_gfx, clobbered_cs); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn twin_plans_cover_console_depth_formats() { + let d32 = plan_for_console_format(40).expect("D32_FLOAT"); + assert_eq!((d32.depth_twin_fmt, d32.stencil_twin_fmt, d32.depth_sync, d32.two_plane), (39, None, DepthSyncKind::DirectCopy, false)); + + let d16 = plan_for_console_format(55).expect("D16_UNORM"); + assert_eq!((d16.depth_twin_fmt, d16.stencil_twin_fmt, d16.depth_sync, d16.two_plane), (53, None, DepthSyncKind::DirectCopy, false)); + + let d32s8 = plan_for_console_format(20).expect("D32_FLOAT_S8X24_UINT"); + assert_eq!((d32s8.depth_twin_fmt, d32s8.stencil_twin_fmt, d32s8.depth_sync, d32s8.two_plane), (39, Some(60), DepthSyncKind::BufferThrough, true)); + + let d16s8 = plan_for_console_format(118).expect("D16_UNORM_S8_UINT"); + assert_eq!(d16s8.depth_twin_fmt, 53); + assert_eq!(d16s8.stencil_twin_fmt, Some(60)); + assert!(d16s8.two_plane); + assert_eq!(d16s8.depth_sync, DepthSyncKind::FullscreenPass { twin_srv_fmt: 56, twin_uav_fmt: 56 }); + + let d24s8 = plan_for_console_format(45).expect("D24_UNORM_S8_UINT"); + assert_eq!(d24s8.depth_twin_fmt, 39); + assert_eq!(d24s8.stencil_twin_fmt, Some(60)); + assert_eq!(d24s8.depth_sync, DepthSyncKind::FullscreenPass { twin_srv_fmt: 41, twin_uav_fmt: 41 }); + + assert!(plan_for_console_format(28).is_none(), "color format has no depth twin plan"); + } + + #[test] + fn uav_format_and_plane_classification() { + // Stencil plane detection via format or explicit PlaneSlice == 1 + assert!(is_stencil_uav_format(62, 0)); // R8_UINT + assert!(is_stencil_uav_format(120, 0)); // console X16_TYPELESS_G8_UINT + assert!(is_stencil_uav_format(47, 0)); // X24_TYPELESS_G8_UINT + assert!(is_stencil_uav_format(22, 0)); // X32_TYPELESS_G8X24_UINT + assert!(is_stencil_uav_format(0, 1)); // explicit PlaneSlice 1 + assert!(!is_stencil_uav_format(56, 0)); // R16_UNORM depth + assert!(!is_stencil_uav_format(41, 0)); // R32_FLOAT depth + + // Legal typed UAV formats on each typeless twin + assert_eq!(twin_uav_format(60, 120), 62); + assert_eq!(twin_uav_format(60, 61), 61); + assert_eq!(twin_uav_format(53, 118), 56); + assert_eq!(twin_uav_format(53, 57), 57); + assert_eq!(twin_uav_format(39, 40), 41); + assert_eq!(twin_uav_format(39, 42), 42); + + // Subresource plane split (2 mips * 3 slices = 6 subresources per plane) + assert_eq!(plane_for_subresource(D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, 2, 3, true), PlaneMask::Both); + assert_eq!(plane_for_subresource(0, 2, 3, true), PlaneMask::Depth); + assert_eq!(plane_for_subresource(5, 2, 3, true), PlaneMask::Depth); + assert_eq!(plane_for_subresource(6, 2, 3, true), PlaneMask::Stencil); + assert_eq!(plane_for_subresource(11, 2, 3, true), PlaneMask::Stencil); + assert_eq!(plane_for_subresource(D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, 2, 3, false), PlaneMask::Depth); + } + + #[test] + fn state_machine_handles_split_and_async_compute_transitions() { + let plan = plan_for_console_format(118).unwrap(); + let mut e = Entry::new(0x1, plan, 1920, 1080, 1, 1, STATE_DEPTH_WRITE, true); + + // Without twins allocated yet, leaving UAV becomes a plain TransitionOnly (DEPTH_WRITE -> DEPTH_READ) + let (pass, uav, op) = decide_transition(&mut e, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, STATE_UAV, 0x20, 0, true); + assert!(!pass && uav); + assert_eq!( + op, + Some(SyncOp::TransitionOnly { + res: 0, + depth: vec![(0, STATE_DEPTH_WRITE, 0x20)], + stencil: vec![(0, STATE_DEPTH_WRITE, 0x20)], + }) + ); + assert_eq!((e.pc_states_depth[0], e.pc_states_stencil[0]), (0x20, 0x20)); + + // Split barrier BEGIN_ONLY touching UAV is suppressed, and END_ONLY performs the atomic transition + let (pass, uav, op) = decide_transition(&mut e, 1, 0x20, STATE_UAV, 1, true); + assert!(!pass && !uav && op.is_none()); + assert!(e.split_uav_stencil[0] && !e.split_uav_depth[0]); + + let (pass, uav, op) = decide_transition(&mut e, 1, 0x20, STATE_UAV, 2, true); + assert!(!pass && uav); + assert!(!e.split_uav_stencil[0]); + assert_eq!( + op, + Some(SyncOp::TransitionOnly { + res: 0, + depth: vec![], + stencil: vec![(0, 0x20, STATE_DEPTH_WRITE)], + }) + ); + // Plane 0 stayed in DEPTH_READ while plane 1 moved to DEPTH_WRITE + assert_eq!((e.pc_states_depth[0], e.pc_states_stencil[0]), (0x20, STATE_DEPTH_WRITE)); + + // Multi-mip per-subresource tracking: transitioning only mip 1 must leave mip 0 untouched + // and entering UAV must use each subresource's tracked PC state as StateBefore (not title `before`). + let mut multi = Entry::new(0x1, plan, 512, 512, 1, 2, STATE_DEPTH_WRITE, false); + let (pass, _, op) = decide_transition(&mut multi, 1, STATE_DEPTH_WRITE, 0x20, 0, true); + assert!(pass && op.is_none()); + assert_eq!(multi.pc_states_depth, vec![STATE_DEPTH_WRITE, 0x20]); + let (pass, uav, op) = decide_transition(&mut multi, 1, STATE_COMMON, STATE_UAV, 0, true); + assert!(!pass && uav); + assert_eq!( + op, + Some(SyncOp::TransitionOnly { + res: 0, + depth: vec![(1, 0x20, STATE_DEPTH_WRITE)], + stencil: vec![], + }), + "single-subresource UAV entry transitions only mip 1 from its tracked PC state 0x20" + ); + assert_eq!(multi.pc_states_depth, vec![STATE_DEPTH_WRITE, STATE_DEPTH_WRITE]); + + // On a COMPUTE list, UAV transitions are suppressed with a global UAV barrier (no invalid DEPTH state) + let (pass, uav, op) = decide_transition(&mut e, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, STATE_UAV, STATE_COMMON, 0, false); + assert!(!pass && uav && op.is_none()); + } + + #[test] + fn uav_barrier_returns_zero_for_untracked_resources() { + assert_eq!(on_uav_barrier(0), [0, 0]); + assert_eq!(on_uav_barrier(0xDEAD_BEE0), [0, 0]); + } + + #[test] + fn enhanced_barriers_handle_texture_subresources_uav_and_discard() { + // Layout + access -> valid PC resource state (never STATE_UAV or buffer-only bits) + assert_eq!( + enhanced_layout_to_pc_state(D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ, D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ), + STATE_DEPTH_READ + ); + assert_eq!( + enhanced_layout_to_pc_state(D3D12_BARRIER_LAYOUT_COMMON, D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ), + STATE_DEPTH_READ + ); + assert_eq!( + enhanced_layout_to_pc_state(D3D12_BARRIER_LAYOUT_COMMON, D3D12_BARRIER_ACCESS_SHADER_RESOURCE), + STATE_SRV_ALL + ); + assert_eq!( + enhanced_layout_to_pc_state(D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS, D3D12_BARRIER_ACCESS_UNORDERED_ACCESS), + STATE_DEPTH_WRITE + ); + assert_eq!( + enhanced_layout_to_pc_state(D3D12_BARRIER_LAYOUT_COMPUTE_QUEUE_UNORDERED_ACCESS, D3D12_BARRIER_ACCESS_UNORDERED_ACCESS), + STATE_DEPTH_WRITE + ); + + let plan = plan_for_console_format(44).unwrap(); + let mut e = Entry::new(0x1, plan, 2560, 1440, 1, 2, STATE_COMMON, false); + let all = D3D12_BARRIER_SUBRESOURCE_RANGE { + IndexOrFirstMipLevel: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + NumMipLevels: 0, + FirstArraySlice: 0, + NumArraySlices: 0, + FirstPlane: 0, + NumPlanes: 0, + }; + assert_eq!(resolve_enhanced_subresources(&e, &all), (vec![0, 1], vec![0, 1])); + + // Plane-specific range: stencil plane only, mip 1 only + let stencil_mip1 = D3D12_BARRIER_SUBRESOURCE_RANGE { + IndexOrFirstMipLevel: 1, + NumMipLevels: 1, + FirstArraySlice: 0, + NumArraySlices: 1, + FirstPlane: 1, + NumPlanes: 1, + }; + assert_eq!(resolve_enhanced_subresources(&e, &stencil_mip1), (vec![], vec![1])); + + // Without twins materialized yet, leaving UAV produces TransitionOnly -> DEPTH_READ + let op = decide_enhanced_texture_barrier( + &mut e, + D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, + D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ, + D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS, + D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ, + &all, + D3D12_TEXTURE_BARRIER_FLAG_NONE, + true, + ); + assert_eq!( + op, + Some(SyncOp::TransitionOnly { + res: 0, + depth: vec![(0, STATE_COMMON, STATE_DEPTH_READ), (1, STATE_COMMON, STATE_DEPTH_READ)], + stencil: vec![(0, STATE_COMMON, STATE_DEPTH_READ), (1, STATE_COMMON, STATE_DEPTH_READ)], + }) + ); + assert_eq!(e.pc_states_depth, vec![STATE_DEPTH_READ, STATE_DEPTH_READ]); + assert_eq!(e.pc_states_stencil, vec![STATE_DEPTH_READ, STATE_DEPTH_READ]); + + // DISCARD entering DEPTH_STENCIL_WRITE marks plane dirty and transitions DEPTH_READ -> DEPTH_WRITE + let op = decide_enhanced_texture_barrier( + &mut e, + D3D12_BARRIER_ACCESS_NO_ACCESS, + D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE, + D3D12_BARRIER_LAYOUT_UNDEFINED, + D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE, + &all, + D3D12_TEXTURE_BARRIER_FLAG_DISCARD, + true, + ); + assert_eq!( + op, + Some(SyncOp::TransitionOnly { + res: 0, + depth: vec![(0, STATE_DEPTH_READ, STATE_DEPTH_WRITE), (1, STATE_DEPTH_READ, STATE_DEPTH_WRITE)], + stencil: vec![(0, STATE_DEPTH_READ, STATE_DEPTH_WRITE), (1, STATE_DEPTH_READ, STATE_DEPTH_WRITE)], + }) + ); + assert!(e.depth_plane_dirty && e.stencil_plane_dirty); + + // Non-DIRECT list never returns a SyncOp + let op = decide_enhanced_texture_barrier( + &mut e, + D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, + D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ, + D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS, + D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ, + &all, + D3D12_TEXTURE_BARRIER_FLAG_NONE, + false, + ); + assert!(op.is_none()); + } + + /// GPU: verify `D24_UNORM_S8_UINT` (console fmt 44) and `D16_UNORM_S8_UINT` (console fmt 118) + /// compute UAV writes (`permutation / 65535.0` depth + per-pixel stencil) synchronize cleanly + /// via BOTH enhanced texture barriers (`#146` `D3D12_TEXTURE_BARRIER`) AND global UAV barriers + /// + `before_bind_dsv`, passing exact `DepthFunc = EQUAL` draws while preserving stencil. + #[test] + fn gpu_d24s8_and_d16s8_enhanced_barrier_twin_sync_passes_equal_depth_test() { + unsafe { + let dev: ID3D12Device = { + use windows::Win32::Graphics::Dxgi::*; + if std::env::var_os("XCL_D3D12_DEBUG").is_some() { + let _ = crate::gpu::agility::enable_debug_layer(); + } + let sel = crate::gpu::adapter::Selector::from_env_or("auto"); + let adapter: Option = if sel == crate::gpu::adapter::Selector::Warp { + let Ok(factory) = CreateDXGIFactory2::(DXGI_CREATE_FACTORY_FLAGS(0)) else { + return crate::gpu::recomp::runner::skip_test("no DXGI factory"); + }; + let Ok(warp) = factory.EnumWarpAdapter::() else { + return crate::gpu::recomp::runner::skip_test("no WARP adapter"); + }; + Some(warp.cast().unwrap()) + } else { + None + }; + let Ok(d) = crate::gpu::agility::create_device(adapter.as_ref(), windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + d + }; + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).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(); + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let mut fence_val = 0u64; + + let wait_gpu = |q: &ID3D12CommandQueue, fence: &ID3D12Fence, val: &mut u64| { + *val += 1; + q.Signal(fence, *val).unwrap(); + while fence.GetCompletedValue() < *val { + std::thread::yield_now(); + } + }; + + // Compute shader writing 4 quadrant permutations (1, 17, 42, 120) / 65535.0 to depth twin + // and (0x11, 0x22, 0x5A, 0xA5) to stencil twin; c0 overrides quadrant 0 permutation. + let cs_write = compile_sm5( + "RWTexture2D dstD : register(u0);\n\ + RWTexture2D dstS : register(u1);\n\ + cbuffer C : register(b0) { uint q0_perm; };\n\ + [numthreads(8, 8, 1)]\n\ + void main(uint3 id : SV_DispatchThreadID) {\n\ + uint q = (id.x >= 8 ? 1u : 0u) + (id.y >= 8 ? 2u : 0u);\n\ + uint perm = (q == 0u) ? q0_perm : ((q == 1u) ? 17u : ((q == 2u) ? 42u : 120u));\n\ + uint st = (q == 0u) ? 0x11u : ((q == 1u) ? 0x22u : ((q == 2u) ? 0x5Au : 0xA5u));\n\ + dstD[id.xy] = float(perm) / 65535.0;\n\ + dstS[id.xy] = st;\n\ + }\n", + "cs_5_0", + ) + .expect("cs_write"); + let uav_range = D3D12_DESCRIPTOR_RANGE { + RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_UAV, + NumDescriptors: 2, + BaseShaderRegister: 0, + RegisterSpace: 0, + OffsetInDescriptorsFromTableStart: 0, + }; + let cs_params = [ + D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + Anonymous: D3D12_ROOT_PARAMETER_0 { + Constants: D3D12_ROOT_CONSTANTS { ShaderRegister: 0, RegisterSpace: 0, Num32BitValues: 1 }, + }, + }, + D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + Anonymous: D3D12_ROOT_PARAMETER_0 { + DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE { NumDescriptorRanges: 1, pDescriptorRanges: &uav_range }, + }, + }, + ]; + let mut rs_blob = None; + D3D12SerializeRootSignature( + &D3D12_ROOT_SIGNATURE_DESC { NumParameters: 2, pParameters: cs_params.as_ptr(), ..Default::default() }, + D3D_ROOT_SIGNATURE_VERSION_1, + &mut rs_blob, + None, + ) + .unwrap(); + let rs_blob = rs_blob.unwrap(); + let cs_rs: ID3D12RootSignature = dev.CreateRootSignature(0, std::slice::from_raw_parts(rs_blob.GetBufferPointer() as *const u8, rs_blob.GetBufferSize())).unwrap(); + let mut cs_pso_desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: ManuallyDrop::new(Some(cs_rs.clone())), + CS: D3D12_SHADER_BYTECODE { pShaderBytecode: cs_write.as_ptr() as _, BytecodeLength: cs_write.len() }, + ..Default::default() + }; + let cs_pso: ID3D12PipelineState = dev.CreateComputePipelineState(&cs_pso_desc).unwrap(); + ManuallyDrop::drop(&mut cs_pso_desc.pRootSignature); + + // Graphics EQUAL test shader: VS outputs z = target_perm / 65535.0, PS writes solid 0xFF red. + let eq_vs = compile_sm5( + "cbuffer C : register(b0) { uint target_perm; };\n\ + float4 main(uint id : SV_VertexID) : SV_Position {\n\ + float2 p = float2((id == 2) ? 3.0 : -1.0, (id == 1) ? 3.0 : -1.0);\n\ + return float4(p, float(target_perm) / 65535.0, 1.0);\n\ + }\n", + "vs_5_0", + ) + .expect("eq_vs"); + let eq_ps = compile_sm5("float4 main() : SV_Target { return float4(1.0, 0.0, 0.0, 1.0); }\n", "ps_5_0").expect("eq_ps"); + let eq_param = D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + ShaderVisibility: D3D12_SHADER_VISIBILITY_VERTEX, + Anonymous: D3D12_ROOT_PARAMETER_0 { + Constants: D3D12_ROOT_CONSTANTS { ShaderRegister: 0, RegisterSpace: 0, Num32BitValues: 1 }, + }, + }; + let mut eq_rs_blob = None; + D3D12SerializeRootSignature( + &D3D12_ROOT_SIGNATURE_DESC { NumParameters: 1, pParameters: &eq_param, ..Default::default() }, + D3D_ROOT_SIGNATURE_VERSION_1, + &mut eq_rs_blob, + None, + ) + .unwrap(); + let eq_rs_blob = eq_rs_blob.unwrap(); + let eq_rs: ID3D12RootSignature = dev.CreateRootSignature(0, std::slice::from_raw_parts(eq_rs_blob.GetBufferPointer() as *const u8, eq_rs_blob.GetBufferSize())).unwrap(); + let mut eq_pso_desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { + pRootSignature: ManuallyDrop::new(Some(eq_rs.clone())), + VS: D3D12_SHADER_BYTECODE { pShaderBytecode: eq_vs.as_ptr() as _, BytecodeLength: eq_vs.len() }, + PS: D3D12_SHADER_BYTECODE { pShaderBytecode: eq_ps.as_ptr() as _, BytecodeLength: eq_ps.len() }, + SampleMask: u32::MAX, + BlendState: D3D12_BLEND_DESC { + RenderTarget: [ + D3D12_RENDER_TARGET_BLEND_DESC { RenderTargetWriteMask: 0xF, ..Default::default() }, + Default::default(), Default::default(), Default::default(), + Default::default(), Default::default(), Default::default(), Default::default(), + ], + ..Default::default() + }, + RasterizerState: D3D12_RASTERIZER_DESC { FillMode: D3D12_FILL_MODE_SOLID, CullMode: D3D12_CULL_MODE_NONE, DepthClipEnable: true.into(), ..Default::default() }, + DepthStencilState: D3D12_DEPTH_STENCIL_DESC { + DepthEnable: true.into(), + DepthWriteMask: D3D12_DEPTH_WRITE_MASK_ZERO, + DepthFunc: D3D12_COMPARISON_FUNC_EQUAL, + StencilEnable: false.into(), + ..Default::default() + }, + PrimitiveTopologyType: D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, + NumRenderTargets: 1, + RTVFormats: [DXGI_FORMAT_R8G8B8A8_UNORM, Default::default(), Default::default(), Default::default(), Default::default(), Default::default(), Default::default(), Default::default()], + DSVFormat: DXGI_FORMAT_D24_UNORM_S8_UINT, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + ..Default::default() + }; + let eq_pso: ID3D12PipelineState = dev.CreateGraphicsPipelineState(&eq_pso_desc).unwrap(); + ManuallyDrop::drop(&mut eq_pso_desc.pRootSignature); + + // Helper descriptors & heaps + let rt_desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 16, + Height: 16, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT_R8G8B8A8_UNORM, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Flags: D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, + ..Default::default() + }; + let mut rt: Option = None; + dev.CreateCommittedResource( + &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + D3D12_HEAP_FLAG_NONE, + &rt_desc, + D3D12_RESOURCE_STATE_RENDER_TARGET, + None, + &mut rt, + ) + .unwrap(); + let rt = rt.unwrap(); + + let rtv_heap: ID3D12DescriptorHeap = dev.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { Type: D3D12_DESCRIPTOR_HEAP_TYPE_RTV, NumDescriptors: 1, ..Default::default() }).unwrap(); + let rtv_cpu = rtv_heap.GetCPUDescriptorHandleForHeapStart(); + dev.CreateRenderTargetView(&rt, None, rtv_cpu); + + let dsv_heap: ID3D12DescriptorHeap = dev.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { Type: D3D12_DESCRIPTOR_HEAP_TYPE_DSV, NumDescriptors: 1, ..Default::default() }).unwrap(); + let dsv_cpu = dsv_heap.GetCPUDescriptorHandleForHeapStart(); + + let uav_heap: ID3D12DescriptorHeap = dev + .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + NumDescriptors: 2, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, + ..Default::default() + }) + .unwrap(); + let uav_cpu_base = uav_heap.GetCPUDescriptorHandleForHeapStart().ptr; + let uav_gpu_base = uav_heap.GetGPUDescriptorHandleForHeapStart().ptr; + let uav_inc = dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize; + + let rb_desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, + Width: 16 * 1024, + Height: 1, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT_UNKNOWN, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + ..Default::default() + }; + let mut rb_rt: Option = None; + let mut rb_st: Option = None; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &rb_desc, D3D12_RESOURCE_STATE_COPY_DEST, None, &mut rb_rt).unwrap(); + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &rb_desc, D3D12_RESOURCE_STATE_COPY_DEST, None, &mut rb_st).unwrap(); + let (rb_rt, rb_st) = (rb_rt.unwrap(), rb_st.unwrap()); + + let pc_desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 16, + Height: 16, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT_R24G8_TYPELESS, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Flags: D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, + ..Default::default() + }; + let mut rt_fp = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); + dev.GetCopyableFootprints(&rt_desc, 0, 1, 0, Some(&mut rt_fp), None, None, None); + let mut st_fp = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); + dev.GetCopyableFootprints(&pc_desc, 1, 1, 0, Some(&mut st_fp), None, None, None); + + // Create both depth-stencil targets BEFORE adopting `dev` so test heaps/resources stay + // pure unadopted PC COM objects (no console vtable ABI or global UMA side effects). + let mut depth_targets: Vec<(u32, ID3D12Resource)> = Vec::new(); + for console_fmt in [44u32, 118u32] { + let mut depth_res: Option = None; + dev.CreateCommittedResource( + &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + D3D12_HEAP_FLAG_NONE, + &pc_desc, + D3D12_RESOURCE_STATE_COMMON, + None, + &mut depth_res, + ) + .unwrap(); + depth_targets.push((console_fmt, depth_res.unwrap())); + } + + // Adopt dev + list so `depth_uav` internal helpers use `real::orig_fn` cleanly. + real::adopt(dev.as_raw(), real::Kind::Device); + real::adopt(list.as_raw(), real::Kind::CommandList); + list_x::on_create(list.as_raw(), alloc.as_raw() as usize, 0, D3D12_COMMAND_LIST_TYPE_DIRECT); + + type CreateUavOrig = unsafe extern "system" fn(*mut c_void, *mut c_void, *mut c_void, *const D3D12_UNORDERED_ACCESS_VIEW_DESC, usize); + type CreateDsvOrig = unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_DEPTH_STENCIL_VIEW_DESC, usize); + let create_uav_orig: CreateUavOrig = std::mem::transmute(real::orig_fn(dev.as_raw(), 19)); + let create_dsv_orig: CreateDsvOrig = std::mem::transmute(real::orig_fn(dev.as_raw(), 21)); + + for (console_fmt, depth_res) in depth_targets { + let mut console_desc = [0u8; 0x38]; + console_desc[0..4].copy_from_slice(&3u32.to_le_bytes()); // Texture2D + console_desc[0x10..0x18].copy_from_slice(&16u64.to_le_bytes()); + console_desc[0x18..0x1C].copy_from_slice(&16u32.to_le_bytes()); + console_desc[0x1C..0x1E].copy_from_slice(&1u16.to_le_bytes()); + console_desc[0x1E..0x20].copy_from_slice(&1u16.to_le_bytes()); + console_desc[0x20..0x24].copy_from_slice(&console_fmt.to_le_bytes()); + console_desc[0x24..0x28].copy_from_slice(&1u32.to_le_bytes()); + console_desc[0x30..0x34].copy_from_slice(&6u32.to_le_bytes()); // DEPTH_STENCIL | UAV + + register_if_depth_uav(dev.as_raw(), depth_res.as_raw(), &console_desc, STATE_COMMON, STATE_COMMON); + + let dsv_desc = D3D12_DEPTH_STENCIL_VIEW_DESC { + Format: DXGI_FORMAT_D24_UNORM_S8_UINT, + ViewDimension: D3D12_DSV_DIMENSION_TEXTURE2D, + Flags: D3D12_DSV_FLAG_READ_ONLY_DEPTH | D3D12_DSV_FLAG_READ_ONLY_STENCIL, + ..Default::default() + }; + create_dsv_orig(dev.as_raw(), depth_res.as_raw(), &dsv_desc, dsv_cpu.ptr); + note_dsv(dsv_cpu.ptr, depth_res.as_raw() as usize, dsv_desc.Flags.0 as u32); + + let depth_uav_req = D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: DXGI_FORMAT_R24_UNORM_X8_TYPELESS, + ViewDimension: D3D12_UAV_DIMENSION_TEXTURE2D, + ..Default::default() + }; + let stencil_uav_req = D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: DXGI_FORMAT_X24_TYPELESS_G8_UINT, + ViewDimension: D3D12_UAV_DIMENSION_TEXTURE2D, + ..Default::default() + }; + let (d_twin_raw, d_uav_desc) = resolve_uav(dev.as_raw(), depth_res.as_raw(), Some(&depth_uav_req)).expect("depth twin"); + let (s_twin_raw, s_uav_desc) = resolve_uav(dev.as_raw(), depth_res.as_raw(), Some(&stencil_uav_req)).expect("stencil twin"); + create_uav_orig(dev.as_raw(), d_twin_raw, std::ptr::null_mut(), &d_uav_desc, uav_cpu_base); + create_uav_orig(dev.as_raw(), s_twin_raw, std::ptr::null_mut(), &s_uav_desc, uav_cpu_base + uav_inc); + + // --- Phase 1: Compute writes twins -> Enhanced #146 Texture Barrier (UAV -> DEPTH_READ) -> EQUAL draw (perm 42) --- + let set_heaps_orig: unsafe extern "system" fn(*mut c_void, u32, *const usize) = std::mem::transmute(real::orig_fn(list.as_raw(), 28)); + let set_cs_rs: unsafe extern "system" fn(*mut c_void, *mut c_void) = std::mem::transmute(real::orig_fn(list.as_raw(), 29)); + let set_gfx_rs: unsafe extern "system" fn(*mut c_void, *mut c_void) = std::mem::transmute(real::orig_fn(list.as_raw(), 30)); + let set_cs_table: unsafe extern "system" fn(*mut c_void, u32, u64) = std::mem::transmute(real::orig_fn(list.as_raw(), 31)); + let set_cs_const: unsafe extern "system" fn(*mut c_void, u32, u32, u32) = std::mem::transmute(real::orig_fn(list.as_raw(), 33)); + let set_gfx_const: unsafe extern "system" fn(*mut c_void, u32, u32, u32) = std::mem::transmute(real::orig_fn(list.as_raw(), 34)); + let set_pso: unsafe extern "system" fn(*mut c_void, *mut c_void) = std::mem::transmute(real::orig_fn(list.as_raw(), 25)); + let dispatch: unsafe extern "system" fn(*mut c_void, u32, u32, u32) = std::mem::transmute(real::orig_fn(list.as_raw(), 14)); + let draw: unsafe extern "system" fn(*mut c_void, u32, u32, u32, u32) = std::mem::transmute(real::orig_fn(list.as_raw(), 12)); + let set_topo: unsafe extern "system" fn(*mut c_void, u32) = std::mem::transmute(real::orig_fn(list.as_raw(), 20)); + let set_vp: unsafe extern "system" fn(*mut c_void, u32, *const D3D12_VIEWPORT) = std::mem::transmute(real::orig_fn(list.as_raw(), 21)); + let set_sc: unsafe extern "system" fn(*mut c_void, u32, *const RECT) = std::mem::transmute(real::orig_fn(list.as_raw(), 22)); + let set_rt: unsafe extern "system" fn(*mut c_void, u32, *const usize, i32, *const usize) = std::mem::transmute(real::orig_fn(list.as_raw(), 46)); + let clear_rt: unsafe extern "system" fn(*mut c_void, usize, *const [f32; 4], u32, *const RECT) = std::mem::transmute(real::orig_fn(list.as_raw(), 48)); + + let uav_heap_raw = uav_heap.as_raw() as usize; + set_heaps_orig(list.as_raw(), 1, &uav_heap_raw); + set_cs_rs(list.as_raw(), cs_rs.as_raw()); + set_pso(list.as_raw(), cs_pso.as_raw()); + set_cs_const(list.as_raw(), 0, 1, 0); // q0_perm = 1 + set_cs_table(list.as_raw(), 1, uav_gpu_base); + dispatch(list.as_raw(), 2, 2, 1); + + // Enhanced #146 Texture Barrier: UNORDERED_ACCESS -> DEPTH_STENCIL_READ + let tb = D3D12_TEXTURE_BARRIER { + SyncBefore: D3D12_BARRIER_SYNC_COMPUTE_SHADING, + SyncAfter: D3D12_BARRIER_SYNC_DEPTH_STENCIL, + AccessBefore: D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, + AccessAfter: D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ, + LayoutBefore: D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS, + LayoutAfter: D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ, + pResource: ManuallyDrop::new(Some(depth_res.clone())), + Subresources: D3D12_BARRIER_SUBRESOURCE_RANGE { + IndexOrFirstMipLevel: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + NumMipLevels: 0, + FirstArraySlice: 0, + NumArraySlices: 0, + FirstPlane: 0, + NumPlanes: 0, + }, + Flags: D3D12_TEXTURE_BARRIER_FLAG_NONE, + }; + let grp = D3D12_BARRIER_GROUP { + Type: D3D12_BARRIER_TYPE_TEXTURE, + NumBarriers: 1, + Anonymous: D3D12_BARRIER_GROUP_0 { pTextureBarriers: &tb }, + }; + let mut ops = Vec::new(); + let had_global = filter_enhanced_barriers(1, &grp as *const _ as *const c_void, true, &mut ops); + ManuallyDrop::drop(&mut { tb }.pResource); + assert!(!had_global); + assert_eq!(ops.len(), 1, "enhanced texture barrier must queue TwinToDepth"); + execute_syncs(list.as_raw(), &ops); + + // Draw with DepthFunc = EQUAL at z = 42 / 65535.0 + let black = [0.0f32; 4]; + clear_rt(list.as_raw(), rtv_cpu.ptr, &black, 0, std::ptr::null()); + let vp = D3D12_VIEWPORT { TopLeftX: 0.0, TopLeftY: 0.0, Width: 16.0, Height: 16.0, MinDepth: 0.0, MaxDepth: 1.0 }; + let sc = RECT { left: 0, top: 0, right: 16, bottom: 16 }; + set_vp(list.as_raw(), 1, &vp); + set_sc(list.as_raw(), 1, &sc); + set_rt(list.as_raw(), 1, &rtv_cpu.ptr, 0, &dsv_cpu.ptr); + set_gfx_rs(list.as_raw(), eq_rs.as_raw()); + set_pso(list.as_raw(), eq_pso.as_raw()); + set_topo(list.as_raw(), D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST.0 as u32); + set_gfx_const(list.as_raw(), 0, 42, 0); + draw(list.as_raw(), 3, 1, 0, 0); + + // Copy RT + stencil plane (subresource 1) to readback + list_x::transition(list.as_raw(), rt.as_raw() as usize, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); + let dst_rt = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { pResource: ManuallyDrop::new(Some(rb_rt.clone())), Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: rt_fp } }); + let src_rt = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { pResource: ManuallyDrop::new(Some(rt.clone())), Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 } }); + list_x::pc_copy_texture_region(list.as_raw(), &dst_rt.0, 0, 0, 0, &src_rt.0, std::ptr::null()); + list_x::transition(list.as_raw(), rt.as_raw() as usize, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET); + + // Copy stencil plane (subresource 1) from DEPTH_READ -> COPY_SOURCE -> DEPTH_READ + let st_to_src = D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: std::mem::transmute_copy(&depth_res), + Subresource: 1, + StateBefore: D3D12_RESOURCE_STATE_DEPTH_READ, + StateAfter: D3D12_RESOURCE_STATE_COPY_SOURCE, + }), + }, + }; + list_x::pc_resource_barriers(list.as_raw(), &[st_to_src]); + let dst_st = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { pResource: ManuallyDrop::new(Some(rb_st.clone())), Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: st_fp } }); + let src_st = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { pResource: ManuallyDrop::new(Some(depth_res.clone())), Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 1 } }); + list_x::pc_copy_texture_region(list.as_raw(), &dst_st.0, 0, 0, 0, &src_st.0, std::ptr::null()); + let st_to_read = D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: std::mem::transmute_copy(&depth_res), + Subresource: 1, + StateBefore: D3D12_RESOURCE_STATE_COPY_SOURCE, + StateAfter: D3D12_RESOURCE_STATE_DEPTH_READ, + }), + }, + }; + list_x::pc_resource_barriers(list.as_raw(), &[st_to_read]); + + let close_orig: unsafe extern "system" fn(*mut c_void) -> i32 = std::mem::transmute(real::orig_fn(list.as_raw(), 9)); + let hr_close = close_orig(list.as_raw()); + if hr_close != 0 { + if let Ok(iq) = dev.cast::() { + let n = iq.GetNumStoredMessages(); + for i in 0..n { + let mut sz = 0usize; + let _ = iq.GetMessage(i, None, &mut sz); + if sz > 0 { + let mut buf = vec![0u8; sz]; + if iq.GetMessage(i, Some(buf.as_mut_ptr() as *mut D3D12_MESSAGE), &mut sz).is_ok() { + let msg = &*(buf.as_ptr() as *const D3D12_MESSAGE); + let desc = std::ffi::CStr::from_ptr(msg.pDescription as *const i8).to_string_lossy(); + eprintln!("D3D12 INFO[{i}]: id={} sev={} {desc}", msg.ID.0, msg.Severity.0); + } + } + } + } + } + assert_eq!(hr_close, 0); + q.ExecuteCommandLists(&[Some(list.cast().unwrap())]); + wait_gpu(&q, &fence, &mut fence_val); + + // Verify Phase 1 RT: ONLY quadrant 2 (x < 8, y >= 8, perm 42) passed EQUAL! + let mut mapped = std::ptr::null_mut(); + rb_rt.Map(0, None, Some(&mut mapped)).unwrap(); + let rt_bytes = std::slice::from_raw_parts(mapped as *const u8, 16 * rt_fp.Footprint.RowPitch as usize); + for (qx, qy, expect_r) in [(2usize, 2usize, 0u8), (10, 2, 0), (2, 10, 255), (10, 10, 0)] { + let r = rt_bytes[qy * rt_fp.Footprint.RowPitch as usize + qx * 4]; + assert_eq!(r, expect_r, "fmt {console_fmt} Phase 1 EQUAL(42) at ({qx},{qy})"); + } + rb_rt.Unmap(0, None); + + // Verify stencil survived FullscreenPass intact + let mut st_mapped = std::ptr::null_mut(); + rb_st.Map(0, None, Some(&mut st_mapped)).unwrap(); + let st_bytes = std::slice::from_raw_parts(st_mapped as *const u8, 16 * st_fp.Footprint.RowPitch as usize); + for (qx, qy, expect_s) in [(2usize, 2usize, 0x11u8), (10, 2, 0x22), (2, 10, 0x5A), (10, 10, 0xA5)] { + let s = st_bytes[qy * st_fp.Footprint.RowPitch as usize + qx]; + assert_eq!(s, expect_s, "fmt {console_fmt} stencil at ({qx},{qy})"); + } + rb_st.Unmap(0, None); + + // --- Phase 2: Global UAV barrier (#146 GLOBAL) + before_bind_dsv --- + alloc.Reset().unwrap(); + let reset_orig: unsafe extern "system" fn(*mut c_void, *mut c_void, *mut c_void) -> i32 = std::mem::transmute(real::orig_fn(list.as_raw(), 10)); + let reset_adopted = |list: &ID3D12GraphicsCommandList, alloc: &ID3D12CommandAllocator| { + // The PC runtime's Reset restores its own vtable (as `list_x::reset_pc` handles). + let ext = *(list.as_raw() as *const usize); + assert_eq!(reset_orig(list.as_raw(), alloc.as_raw(), std::ptr::null_mut()), 0); + real::preserve_vtable(list.as_raw(), ext); + }; + reset_adopted(&list, &alloc); + + set_heaps_orig(list.as_raw(), 1, &uav_heap_raw); + set_cs_rs(list.as_raw(), cs_rs.as_raw()); + set_pso(list.as_raw(), cs_pso.as_raw()); + set_cs_const(list.as_raw(), 0, 77, 0); // q0_perm = 77 + set_cs_table(list.as_raw(), 1, uav_gpu_base); + dispatch(list.as_raw(), 2, 2, 1); + + let gb = D3D12_GLOBAL_BARRIER { + SyncBefore: D3D12_BARRIER_SYNC_COMPUTE_SHADING, + SyncAfter: D3D12_BARRIER_SYNC_ALL, + AccessBefore: D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, + AccessAfter: D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ, + }; + let ggrp = D3D12_BARRIER_GROUP { + Type: D3D12_BARRIER_TYPE_GLOBAL, + NumBarriers: 1, + Anonymous: D3D12_BARRIER_GROUP_0 { pGlobalBarriers: &gb }, + }; + let mut ops2 = Vec::new(); + assert!(filter_enhanced_barriers(1, &ggrp as *const _ as *const c_void, true, &mut ops2)); + assert!(ops2.is_empty()); + on_global_uav_barrier(); + before_bind_dsv(list.as_raw(), dsv_cpu.ptr); + + clear_rt(list.as_raw(), rtv_cpu.ptr, &black, 0, std::ptr::null()); + set_vp(list.as_raw(), 1, &vp); + set_sc(list.as_raw(), 1, &sc); + set_rt(list.as_raw(), 1, &rtv_cpu.ptr, 0, &dsv_cpu.ptr); + set_gfx_rs(list.as_raw(), eq_rs.as_raw()); + set_pso(list.as_raw(), eq_pso.as_raw()); + set_topo(list.as_raw(), D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST.0 as u32); + set_gfx_const(list.as_raw(), 0, 77, 0); + draw(list.as_raw(), 3, 1, 0, 0); + + list_x::transition(list.as_raw(), rt.as_raw() as usize, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); + list_x::pc_copy_texture_region(list.as_raw(), &dst_rt.0, 0, 0, 0, &src_rt.0, std::ptr::null()); + list_x::transition(list.as_raw(), rt.as_raw() as usize, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET); + assert_eq!(close_orig(list.as_raw()), 0); + q.ExecuteCommandLists(&[Some(list.cast().unwrap())]); + wait_gpu(&q, &fence, &mut fence_val); + + let mut mapped2 = std::ptr::null_mut(); + rb_rt.Map(0, None, Some(&mut mapped2)).unwrap(); + let rt_bytes2 = std::slice::from_raw_parts(mapped2 as *const u8, 16 * rt_fp.Footprint.RowPitch as usize); + for (qx, qy, expect_r) in [(2usize, 2usize, 255u8), (10, 2, 0), (2, 10, 0), (10, 10, 0)] { + let r = rt_bytes2[qy * rt_fp.Footprint.RowPitch as usize + qx * 4]; + assert_eq!(r, expect_r, "fmt {console_fmt} Phase 2 global+before_bind_dsv EQUAL(77) at ({qx},{qy})"); + } + rb_rt.Unmap(0, None); + + forget(depth_res.as_raw() as usize); + alloc.Reset().unwrap(); + reset_adopted(&list, &alloc); + } + if let Some(m) = PIPELINES.lock().unwrap().as_mut() { + m.remove(&(dev.as_raw() as usize)); + } + } + } +} diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 03d7360edc..c148d1d2cc 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -878,11 +878,11 @@ pub fn pc_shader_view_format(resource: u32, view: u32) -> u32 { let view = pc_view_format(view); match (resource, view) { // R32G8X24_TYPELESS: depth 21, stencil 22 - (19..=22, 0 | 19 | 20 | 21 | 39 | 40 | 41) => 21, - (19..=22, 22 | 61 | 62) => 22, + (19..=22, 0 | 19 | 20 | 21 | 39 | 40 | 41 | 42 | 43) => 21, + (19..=22, 22 | 60..=64) => 22, // R24G8_TYPELESS: depth 46, stencil 47 - (44..=47, 0 | 41 | 44 | 45 | 46 | 53 | 54 | 55 | 56) => 46, - (44..=47, 47 | 61 | 62) => 47, + (44..=47, 0 | 41 | 42 | 44 | 45 | 46 | 53 | 54 | 55 | 56 | 57 | 59) => 46, + (44..=47, 47 | 60..=64) => 47, // R32_TYPELESS, R16_TYPELESS (39 | 40, 0 | 39 | 40) => 41, (53 | 55, 0 | 53 | 55) => 56, @@ -1099,6 +1099,7 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, note_alias(va as usize, &x); // console-only state bits (Unicorn Overlord's 4K display target: 0x100000) are not the // PC's: the PC refused the resource (E_INVALIDARG) and the title threw + let console_state = state; let state = pc_initial_state(u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()), state); let mut clear_buf = [0u8; 24]; let pc_format = u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()); @@ -1121,8 +1122,10 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, // sampled textures: on the same shared 64 KiB pages when tiled resources allow, else committed let flags = u32::from_le_bytes(d[0x30..0x34].try_into().unwrap()); let mut placed_hr = None; + let mut pc_tex_desc: Option = None; if !buffer { let pc = std::ptr::read_unaligned(d.as_ptr() as *const D3D12_RESOURCE_DESC); + pc_tex_desc = Some(pc); let console_bytes = crate::hle::xg::Texture::new(&x).ok().map(|t| t.size()).filter(|&s| s != u64::MAX && s != 0); if super::vram::gpu_owned(flags) { if let Some(hr) = super::vram::place(this, va as usize, &pc, state, clear, console_bytes, riid, ppv) { @@ -1131,6 +1134,8 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, super::title_textures::retire_overlapping(va as usize, bytes as usize); } real::note_title_address(*ppv as usize, va as usize); + super::depth_uav::register_if_depth_uav(this, *ppv, &x, state, console_state); + super::format_alias::register_title_texture(this as usize, *ppv as usize, va as usize, &pc, flags, state); } return hr; } @@ -1146,21 +1151,27 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, } }; if hr >= 0 && !buffer && !(*ppv).is_null() { + super::depth_uav::register_if_depth_uav(this, *ppv, &x, state, console_state); + let aliased_to_gpu = pc_tex_desc.as_ref().is_some_and(|pc| { + super::format_alias::register_title_texture(this as usize, *ppv as usize, va as usize, pc, flags, state) + }); static M: AtomicU64 = AtomicU64::new(0); - match super::title_textures::register(this, *ppv, va as usize, &x, D3D12_RESOURCE_STATES(state as i32)) { - Ok(()) => { - real::note_title_address(*ppv as usize, va as usize); - return hr; - } - Err(e) if once(&M, 4) => { - let u = |o: usize| u32::from_le_bytes(x[o..o + 4].try_into().unwrap()); - crate::xlog_warn!( - "D3D12.X: CreatePlacedResourceX texture at {va:#x} (dimension {} {}x{}x{}, {} mips, format {}, {} samples, layout {:#x}, flags {:#x}): not kept in step with the memory ({e})", - u(0), u64::from_le_bytes(x[0x10..0x18].try_into().unwrap()), u(0x18), u16::from_le_bytes(x[0x1C..0x1E].try_into().unwrap()), - u16::from_le_bytes(x[0x1E..0x20].try_into().unwrap()), u(0x20), u(0x24), u(0x2C), u(0x30) - ) + if !aliased_to_gpu { + match super::title_textures::register(this, *ppv, va as usize, &x, D3D12_RESOURCE_STATES(state as i32)) { + Ok(()) => { + real::note_title_address(*ppv as usize, va as usize); + return hr; + } + Err(e) if once(&M, 4) => { + let u = |o: usize| u32::from_le_bytes(x[o..o + 4].try_into().unwrap()); + crate::xlog_warn!( + "D3D12.X: CreatePlacedResourceX texture at {va:#x} (dimension {} {}x{}x{}, {} mips, format {}, {} samples, layout {:#x}, flags {:#x}): not kept in step with the memory ({e})", + u(0), u64::from_le_bytes(x[0x10..0x18].try_into().unwrap()), u(0x18), u16::from_le_bytes(x[0x1C..0x1E].try_into().unwrap()), + u16::from_le_bytes(x[0x1E..0x20].try_into().unwrap()), u(0x20), u(0x24), u(0x2C), u(0x30) + ) + } + Err(_) => {} } - Err(_) => {} } } if hr >= 0 && !(*ppv).is_null() { diff --git a/crates/xcl/src/hle/d3d12x/format_alias.rs b/crates/xcl/src/hle/d3d12x/format_alias.rs new file mode 100644 index 0000000000..81b5aabc1e --- /dev/null +++ b/crates/xcl/src/hle/d3d12x/format_alias.rs @@ -0,0 +1,1282 @@ +//! Placed UAV/SRV texture views (`Create{ShaderResource,UnorderedAccess}ViewAtX`) and +//! format-aliased GPU copies across incompatible DXGI format families. +//! +//! Console titles write packed lighting data (sky, light probes, volumetric fog) through a placed +//! UAV texture view in an integer format (such as `R32_UINT`) and sample the same title address +//! through a placed SRV view in an incompatible format of the same texel width (such as +//! `R9G9B9E5_SHAREDEXP`). On PC D3D12: +//! * `CreateUnorderedAccessViewAtX` often passes a resource desc without `ALLOW_UNORDERED_ACCESS` +//! (or with `R9G9B9E5_SHAREDEXP` / `SRGB`), which caused XCL to produce a null UAV view or +//! promote `R9G9B9E5` to `R11G11B10_FLOAT` (breaking `R32_UINT` views and RGB9E5 decoding). +//! * `CreateShaderResourceViewAtX` with `ALLOW_RENDER_TARGET | ALLOW_UNORDERED_ACCESS` bits still +//! set caused `R9G9B9E5_SHAREDEXP` to be promoted to `R11G11B10_FLOAT` and could register a +//! GPU-written texture in `title_textures` (overwriting GPU data with zeros from CPU memory). +//! * Direct `CopyResource` / `CopyTextureRegion` between different DXGI format families is invalid +//! on PC D3D12; texels of equal byte width must be transferred through an intermediate buffer +//! (`CopyTextureRegion` across all mips and array/depth slices). +//! +//! Set `XCL_FORMAT_ALIAS=0` to disable format-alias syncing. + +use std::collections::{HashMap, HashSet}; +use std::ffi::c_void; +use std::mem::ManuallyDrop; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Mutex, OnceLock}; + +use windows::core::Interface; +use windows::Win32::Graphics::Direct3D12::*; + +use super::list_x; +use super::real; + +const STATE_COMMON: u32 = 0; +const STATE_RENDER_TARGET: u32 = 0x4; +const STATE_UAV: u32 = 0x8; +const STATE_SRV_NON_PIXEL: u32 = 0x40; +const STATE_SRV_ALL: u32 = 0x40 | 0x80; + +const FLAG_RENDER_TARGET: u32 = 0x1; +const FLAG_DEPTH_STENCIL: u32 = 0x2; +const FLAG_UAV: u32 = 0x4; + +/// Geometry & format summary of a texture for alias / buffer-through copy compatibility checks. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) struct TexShape { + pub dimension: i32, + pub width: u64, + pub height: u32, + pub depth_or_array: u16, + pub mip_levels: u16, + pub format: u32, + pub samples: u32, +} + +impl TexShape { + pub(super) fn from_pc_desc(d: &D3D12_RESOURCE_DESC) -> Self { + Self { + dimension: d.Dimension.0, + width: d.Width, + height: d.Height, + depth_or_array: d.DepthOrArraySize.max(1), + mip_levels: d.MipLevels.max(1), + format: d.Format.0 as u32, + samples: d.SampleDesc.Count.max(1), + } + } + + pub(super) fn subresource_count(&self) -> u32 { + let slices = if self.dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D.0 { + 1 + } else { + self.depth_or_array as u32 + }; + (self.mip_levels as u32).saturating_mul(slices).max(1) + } +} + +/// `(bytes_per_element, is_4x4_block_compressed)` for common DXGI formats. +pub(super) fn format_element_bytes(fmt: u32) -> Option<(u32, bool)> { + match fmt { + // 128-bit uncompressed (16 bytes/texel) + 1..=4 => Some((16, false)), + // 96-bit uncompressed (12 bytes/texel) + 5..=8 => Some((12, false)), + // 64-bit uncompressed (8 bytes/texel): R16G16B16A16_*, R32G32_* + 9..=18 => Some((8, false)), + // 32-bit uncompressed (4 bytes/texel): R10G10B10A2_*, R11G11B10_FLOAT, R8G8B8A8_*, R16G16_*, R32_*, R9G9B9E5_SHAREDEXP, B8G8R8A8/X8_* + 23..=43 | 67 | 87..=93 => Some((4, false)), + // 16-bit uncompressed (2 bytes/texel): R8G8_*, R16_*, B5G6R5, B5G5R5A1, B4G4R4A4 + 48..=59 | 85 | 86 | 115 => Some((2, false)), + // 8-bit uncompressed (1 byte/texel): R8_*, A8_UNORM + 60..=65 => Some((1, false)), + // 4x4 block-compressed, 8 bytes/block: BC1, BC4 + 70..=72 | 79..=81 => Some((8, true)), + // 4x4 block-compressed, 16 bytes/block: BC2, BC3, BC5, BC6H, BC7 + 73..=78 | 82..=84 | 94..=99 => Some((16, true)), + _ => None, + } +} + +/// Canonical typeless family representative for a DXGI format. +pub(super) fn typeless_family(fmt: u32) -> u32 { + match fmt { + 1..=4 => 1, // R32G32B32A32_TYPELESS + 5..=8 => 5, // R32G32B32_TYPELESS + 9..=14 => 9, // R16G16B16A16_TYPELESS + 15..=18 => 15, // R32G32_TYPELESS + 19..=22 => 19, // R32G8X24_TYPELESS + 23..=25 => 23, // R10G10B10A2_TYPELESS + 27..=32 => 27, // R8G8B8A8_TYPELESS + 33..=38 => 33, // R16G16_TYPELESS + 39..=43 => 39, // R32_TYPELESS + 44..=47 => 44, // R24G8_TYPELESS + 48..=52 => 48, // R8G8_TYPELESS + 53..=59 => 53, // R16_TYPELESS + 60..=64 => 60, // R8_TYPELESS + 70..=72 => 70, // BC1_TYPELESS + 73..=75 => 73, // BC2_TYPELESS + 76..=78 => 76, // BC3_TYPELESS + 79..=81 => 79, // BC4_TYPELESS + 82..=84 => 82, // BC5_TYPELESS + 87 | 90 | 91 => 90, // B8G8R8A8_TYPELESS + 88 | 92 | 93 => 92, // B8G8R8X8_TYPELESS + 94..=96 => 94, // BC6H_TYPELESS + 97..=99 => 97, // BC7_TYPELESS + other => other, + } +} + +/// Whether `dst_fmt` and `src_fmt` belong to different DXGI typeless families (so direct +/// `CopyResource` is illegal on PC D3D12 and a buffer-through `CopyTextureRegion` is needed). +pub(super) fn needs_buffer_through(dst_fmt: u32, src_fmt: u32) -> bool { + dst_fmt != 0 && src_fmt != 0 && typeless_family(dst_fmt) != typeless_family(src_fmt) +} + +/// Whether two textures can exchange texels via an intermediate buffer (`CopyTextureRegion`). +pub(super) fn can_copy_through_buffer(dst: &TexShape, src: &TexShape) -> bool { + if dst.dimension == D3D12_RESOURCE_DIMENSION_BUFFER.0 + || src.dimension == D3D12_RESOURCE_DIMENSION_BUFFER.0 + || dst.dimension != src.dimension + || dst.samples > 1 + || src.samples > 1 + || dst.mip_levels != src.mip_levels + || dst.depth_or_array != src.depth_or_array + { + return false; + } + let (Some((db, dbc)), Some((sb, sbc))) = (format_element_bytes(dst.format), format_element_bytes(src.format)) else { + return false; + }; + if db != sb { + return false; + } + match (dbc, sbc) { + (false, false) | (true, true) => dst.width == src.width && dst.height == src.height, + (true, false) => src.width == dst.width.div_ceil(4) && src.height == dst.height.div_ceil(4), + (false, true) => dst.width == src.width.div_ceil(4) && dst.height == src.height.div_ceil(4), + } +} + +/// Choose a UAV-capable PC resource format that accepts `view_fmt` (or `rdesc_fmt` if `view_fmt == 0`). +pub(super) fn uav_resource_format(rdesc_fmt: u32, view_fmt: u32) -> u32 { + let target = if view_fmt != 0 { view_fmt } else { rdesc_fmt }; + // R9G9B9E5_SHAREDEXP cannot have ALLOW_UNORDERED_ACCESS on PC; use R32_TYPELESS when the + // view wants 32-bit integer/float UAV access, otherwise let device_x handle it. + if rdesc_fmt == 67 && view_fmt != 0 && view_fmt != 67 { + return typeless_family(view_fmt); + } + // sRGB formats cannot have ALLOW_UNORDERED_ACCESS on PC; use their typeless family. + if matches!(target, 29 | 91 | 93) { + return typeless_family(target); + } + // If the view requests a different family than the resource desc (e.g. R32_UINT view over + // an R9G9B9E5 or R11G11B10 resource), make the UAV texture typeless in the view's family. + if view_fmt != 0 && typeless_family(rdesc_fmt) != typeless_family(view_fmt) { + return typeless_family(view_fmt); + } + // Use typeless when available so both typed UAV and SRV views match cleanly. + let fam = typeless_family(target); + match fam { + 1 | 9 | 15 | 23 | 27 | 33 | 39 | 48 | 53 | 60 | 90 => fam, + _ => target, + } +} + +/// Sanitize a console resource desc for `CreateUnorderedAccessViewAtX`: guarantee +/// `ALLOW_UNORDERED_ACCESS` and a UAV-compatible format so PC D3D12 never drops it to a null view. +pub(super) fn prepare_placed_uav_desc(rdesc: &[u8; 0x38], view_fmt: u32) -> [u8; 0x38] { + let mut x = *rdesc; + x[0x34..].fill(0); + let rdesc_fmt = u32::from_le_bytes(x[0x20..0x24].try_into().unwrap()); + let mut flags = u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()); + let is_depth = flags & FLAG_DEPTH_STENCIL != 0 || matches!(rdesc_fmt, 19 | 20 | 40 | 44 | 45 | 55 | 118..=120); + if !is_depth { + // Strip RTV bit on placed UAV textures so integer/typeless UAV formats are never rejected + // by render-target format restrictions. + flags = (flags & !FLAG_RENDER_TARGET) | FLAG_UAV; + let fmt = uav_resource_format(rdesc_fmt, view_fmt); + x[0x20..0x24].copy_from_slice(&fmt.to_le_bytes()); + } else { + flags |= FLAG_UAV; + } + x[0x30..0x34].copy_from_slice(&flags.to_le_bytes()); + x +} + +/// Sanitize a console resource desc for `CreateShaderResourceViewAtX`: strip `ALLOW_RENDER_TARGET` +/// and `ALLOW_UNORDERED_ACCESS` so `R9G9B9E5_SHAREDEXP` stays format 67 and placed SRVs never +/// retire or overwrite GPU-owned target tracking at `va`. +pub(super) fn prepare_placed_srv_desc(rdesc: &[u8; 0x38], view_fmt: u32) -> [u8; 0x38] { + let mut x = *rdesc; + x[0x34..].fill(0); + let rdesc_fmt = u32::from_le_bytes(x[0x20..0x24].try_into().unwrap()); + let mut flags = u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()); + flags &= !(FLAG_RENDER_TARGET | FLAG_UAV); + x[0x30..0x34].copy_from_slice(&flags.to_le_bytes()); + if view_fmt != 0 && typeless_family(rdesc_fmt) != typeless_family(view_fmt) && format_element_bytes(view_fmt).is_some() { + // Keep R9G9B9E5_SHAREDEXP (67) and other single-member formats verbatim; use typeless + // only when it belongs to a multi-format typeless family. + let fmt = match typeless_family(view_fmt) { + f if f == view_fmt => view_fmt, + f if matches!(f, 1 | 9 | 15 | 23 | 27 | 33 | 39 | 48 | 53 | 60 | 70 | 73 | 76 | 79 | 82 | 90 | 92 | 94 | 97) => f, + _ => view_fmt, + }; + x[0x20..0x24].copy_from_slice(&fmt.to_le_bytes()); + } + x +} + +/// Ownership of a resource participating in format-alias tracking. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +enum Owner { + /// Internal texture created by `dev_create_uav_at_x` (resting state: `UNORDERED_ACCESS`). + PlacedUav, + /// Internal texture created by `dev_create_srv_at_x` (resting state outside open list: `COMMON`). + PlacedSrv, + /// Title-created texture from `CreatePlacedResourceX` (tracked PC state). + TitlePlaced, +} + +struct TrackedTex { + device: usize, + va: usize, + shape: TexShape, + owner: Owner, + /// Per-subresource PC states (only tracked for `Owner::TitlePlaced`). + pc_states: Vec, + is_writer: bool, +} + +impl TrackedTex { + fn new(device: usize, va: usize, shape: TexShape, owner: Owner, initial_state: u32, is_writer: bool) -> Self { + let n = if owner == Owner::TitlePlaced { shape.subresource_count().max(1) as usize } else { 0 }; + Self { device, va, shape, owner, pc_states: vec![initial_state; n], is_writer } + } + + fn uniform_state(&self) -> Option { + let &first = self.pc_states.first()?; + self.pc_states.iter().all(|&s| s == first).then_some(first) + } + + fn update_state(&mut self, subresource: u32, after: u32) { + if subresource == D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES { + self.pc_states.fill(after); + } else if let Some(slot) = self.pc_states.get_mut(subresource as usize) { + *slot = after; + } + } +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +struct AliasPair { + writer: usize, + reader: usize, + device: usize, + buffer_through: bool, + writer_owner: Owner, + reader_owner: Owner, +} + +struct AliasTable { + /// Cached internal placed-UAV textures: `(va, prepared_desc)` -> owned `ID3D12Resource`. + placed_uavs: HashMap<(u64, [u8; 0x38]), ID3D12Resource>, + /// Cached internal placed-SRV textures: `(va, prepared_desc)` -> owned `ID3D12Resource`. + placed_srvs: HashMap<(u64, [u8; 0x38]), ID3D12Resource>, + /// All tracked textures by raw resource pointer. + by_res: HashMap, + /// Title VA -> tracked resource pointers at that exact VA. + by_va: HashMap>, + /// Active `(writer -> reader)` sync pairs. + pairs: Vec, + /// Pairs whose writer is an internal `PlacedUav` (must be synced on UAV barriers). + placed_uav_pairs: usize, +} + +unsafe impl Send for AliasTable {} + +impl AliasTable { + fn new() -> Self { + Self { + placed_uavs: HashMap::new(), + placed_srvs: HashMap::new(), + by_res: HashMap::new(), + by_va: HashMap::new(), + pairs: Vec::new(), + placed_uav_pairs: 0, + } + } + + fn has_writer_at(&self, va: usize, shape: &TexShape) -> bool { + self.by_va.get(&va).is_some_and(|list| { + list.iter().any(|r| { + self.by_res.get(r).is_some_and(|t| t.is_writer && can_copy_through_buffer(shape, &t.shape)) + }) + }) + } + + /// Register `res` at `va` and link any compatible `(writer -> reader)` pairs at the same VA. + /// Returns any existing reader resources that should be marked `gpu_written` in `title_textures`. + fn insert(&mut self, res: usize, tex: TrackedTex) -> Vec { + self.remove(res); + let mut mark_gpu_written = Vec::new(); + let mut new_pairs = Vec::new(); + if let Some(peers) = self.by_va.get(&tex.va) { + for &peer in peers { + let Some(p) = self.by_res.get(&peer) else { + continue; + }; + if p.device != tex.device { + continue; + } + // Link writer -> reader when one is a PlacedUav (always needs sync to its SRV alias) + // OR when their formats are in different typeless families (format-aliased GPU textures). + if tex.is_writer && !p.is_writer && can_copy_through_buffer(&p.shape, &tex.shape) { + let bt = needs_buffer_through(p.shape.format, tex.shape.format); + if tex.owner == Owner::PlacedUav || bt { + new_pairs.push(AliasPair { + writer: res, + reader: peer, + device: tex.device, + buffer_through: bt, + writer_owner: tex.owner, + reader_owner: p.owner, + }); + mark_gpu_written.push(peer); + } + } else if !tex.is_writer && p.is_writer && can_copy_through_buffer(&tex.shape, &p.shape) { + let bt = needs_buffer_through(tex.shape.format, p.shape.format); + if p.owner == Owner::PlacedUav || bt { + new_pairs.push(AliasPair { + writer: peer, + reader: res, + device: tex.device, + buffer_through: bt, + writer_owner: p.owner, + reader_owner: tex.owner, + }); + mark_gpu_written.push(res); + } + } + } + } + for pair in new_pairs { + self.add_pair(pair); + } + self.by_va.entry(tex.va).or_default().push(res); + if tex.owner == Owner::TitlePlaced { + TRACKED_TITLE_TEX.fetch_add(1, Ordering::Relaxed); + } + self.by_res.insert(res, tex); + mark_gpu_written + } + + /// Evict cached internal `PlacedUav`/`PlacedSrv` resources at `va` whose shape no longer + /// matches `keep_shape` (or all internal resources at `va` when `keep_shape` is `None` and + /// no `TitlePlaced` peers remain at `va`), returning the evicted `ID3D12Resource`s to be + /// deferred-released outside the lock. + fn evict_stale_internal_at(&mut self, va: usize, keep_shape: Option<&TexShape>) -> Vec { + let mut evicted_ptrs = Vec::new(); + if let Some(list) = self.by_va.get(&va) { + let has_title = list.iter().any(|r| self.by_res.get(r).is_some_and(|t| t.owner == Owner::TitlePlaced)); + if keep_shape.is_none() && has_title { + return Vec::new(); + } + for &r in list { + let Some(t) = self.by_res.get(&r) else { continue }; + if t.owner == Owner::TitlePlaced { + continue; + } + let stale = match keep_shape { + Some(s) => !can_copy_through_buffer(s, &t.shape), + None => true, + }; + if stale { + evicted_ptrs.push(r); + } + } + } + let mut dropped = Vec::new(); + for r in evicted_ptrs { + self.remove(r); + let uav_keys: Vec<_> = self.placed_uavs.iter().filter(|(_, v)| v.as_raw() as usize == r).map(|(k, _)| *k).collect(); + for k in uav_keys { + if let Some(res) = self.placed_uavs.remove(&k) { + dropped.push(res); + } + } + let srv_keys: Vec<_> = self.placed_srvs.iter().filter(|(_, v)| v.as_raw() as usize == r).map(|(k, _)| *k).collect(); + for k in srv_keys { + if let Some(res) = self.placed_srvs.remove(&k) { + dropped.push(res); + } + } + } + dropped + } + + fn add_pair(&mut self, pair: AliasPair) { + if self.pairs.iter().any(|p| p.writer == pair.writer && p.reader == pair.reader) { + return; + } + if pair.writer_owner == Owner::PlacedUav { + self.placed_uav_pairs += 1; + ACTIVE_PLACED_UAV_PAIRS.store(self.placed_uav_pairs, Ordering::Relaxed); + } + ACTIVE_PAIRS.store(self.pairs.len() + 1, Ordering::Relaxed); + self.pairs.push(pair); + } + + fn remove(&mut self, res: usize) -> Option { + if let Some(old) = self.by_res.remove(&res) { + if old.owner == Owner::TitlePlaced { + TRACKED_TITLE_TEX.fetch_sub(1, Ordering::Relaxed); + } + if let Some(list) = self.by_va.get_mut(&old.va) { + list.retain(|&r| r != res); + if list.is_empty() { + self.by_va.remove(&old.va); + } + } + let before = self.pairs.len(); + self.pairs.retain(|p| p.writer != res && p.reader != res); + if self.pairs.len() != before { + self.placed_uav_pairs = self.pairs.iter().filter(|p| p.writer_owner == Owner::PlacedUav).count(); + ACTIVE_PLACED_UAV_PAIRS.store(self.placed_uav_pairs, Ordering::Relaxed); + ACTIVE_PAIRS.store(self.pairs.len(), Ordering::Relaxed); + } + return Some(old.va); + } + None + } +} + +static TABLE: Mutex> = Mutex::new(None); +static TRACKED_TITLE_TEX: AtomicUsize = AtomicUsize::new(0); +static ACTIVE_PAIRS: AtomicUsize = AtomicUsize::new(0); +static ACTIVE_PLACED_UAV_PAIRS: AtomicUsize = AtomicUsize::new(0); + +/// Defer releasing internal resources evicted from `placed_uavs` / `placed_srvs` until in-flight +/// GPU work has completed. +unsafe fn defer_drop_resources(evicted: Vec) { + for r in evicted { + super::late_release::hold(r.into_raw() as usize); + } +} + +/// D3D12 resource states legal in a `ResourceBarrier` on a `COMPUTE` command list for textures +/// (`COMMON`, `UNORDERED_ACCESS`, `NON_PIXEL_SHADER_RESOURCE`, `COPY_DEST`, `COPY_SOURCE`). +const COMPUTE_VALID_STATES: u32 = 0x8 | 0x40 | 0x400 | 0x800; + +fn state_valid_on_list(ty: D3D12_COMMAND_LIST_TYPE, st: u32) -> bool { + ty == D3D12_COMMAND_LIST_TYPE_DIRECT || (st & !COMPUTE_VALID_STATES == 0) +} + +/// Per-open-list state for `PlacedSrv` resources transitioned into read state on that list +/// (returned to `COMMON` before `Close()`), plus last synced `bind::activity()`. +#[derive(Default)] +struct ListSyncState { + /// `PlacedSrv` resource -> current state on this open list (`SRV_ALL` or `SRV_NON_PIXEL`). + srv_states: HashMap, + last_activity: (u64, u64), + list_type: Option, +} + +static LIST_STATES: Mutex>> = Mutex::new(None); + +pub fn enabled() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| !std::env::var("XCL_FORMAT_ALIAS").is_ok_and(|v| v.trim() == "0")) +} + +unsafe fn create_internal_texture(dev: *mut c_void, pc_desc: &[u8; 0x38], initial_state: u32) -> Option { + let heap = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }; + let mut out: *mut c_void = std::ptr::null_mut(); + let hr = real::dev_create_committed_resource( + dev, + &heap as *const _ as usize, + 0, + pc_desc.as_ptr() as usize, + initial_state as usize, + 0, + &ID3D12Resource::IID as *const _ as *const windows_sys::core::GUID, + &mut out, + ); + if hr < 0 || out.is_null() { + return None; + } + Some(ID3D12Resource::from_raw(out)) +} + +/// Obtain or create the host UAV texture backing `CreateUnorderedAccessViewAtX` at `va`. +pub(super) unsafe fn placed_uav_resource(dev: *mut c_void, rdesc: *const u8, va: u64, view_fmt: u32) -> *mut c_void { + if rdesc.is_null() || va == 0 || !crate::gpu::shader::readable(rdesc as usize, 0x38) { + return std::ptr::null_mut(); + } + let raw_x: [u8; 0x38] = (rdesc as *const [u8; 0x38]).read_unaligned(); + let prepared = prepare_placed_uav_desc(&raw_x, view_fmt); + let key = (va, prepared); + + { + let mut g = TABLE.lock().unwrap(); + let t = g.get_or_insert_with(AliasTable::new); + if let Some(res) = t.placed_uavs.get(&key) { + return res.as_raw(); + } + } + + let pc = super::device_x::pc_resource_desc(&prepared); + let flags = u32::from_le_bytes(prepared[0x30..0x34].try_into().unwrap()); + let is_depth_uav = flags & (FLAG_DEPTH_STENCIL | FLAG_UAV) == (FLAG_DEPTH_STENCIL | FLAG_UAV); + let init_state = if is_depth_uav { + super::device_x::pc_initial_state(flags, STATE_UAV) + } else { + STATE_UAV + }; + let Some(res) = create_internal_texture(dev, &pc, init_state) else { + static N: AtomicU64 = AtomicU64::new(0); + if N.fetch_add(1, Ordering::Relaxed) < 8 { + crate::xlog_warn!("D3D12.X: placed UAV texture at {va:#x} (view fmt {view_fmt}) could not be created"); + } + return std::ptr::null_mut(); + }; + let raw = res.as_raw(); + if is_depth_uav { + super::depth_uav::register_if_depth_uav(dev, raw, &prepared, init_state, STATE_UAV); + } + let pc_desc = res.GetDesc(); + let shape = TexShape::from_pc_desc(&pc_desc); + let mark = { + let mut g = TABLE.lock().unwrap(); + let t = g.get_or_insert_with(AliasTable::new); + let entry = t.placed_uavs.entry(key).or_insert(res); + let raw_in = entry.as_raw(); + if raw_in == raw && !is_depth_uav && enabled() { + t.insert( + raw as usize, + TrackedTex::new(dev as usize, va as usize, shape, Owner::PlacedUav, STATE_UAV, true), + ) + } else { + Vec::new() + } + }; + for reader in mark { + super::title_textures::note_gpu_write(reader); + } + static LOGGED: AtomicU64 = AtomicU64::new(0); + let n = LOGGED.fetch_add(1, Ordering::Relaxed); + if n < 8 || n.is_power_of_two() { + crate::xlog_info!( + "D3D12.X: placed UAV texture at {va:#x} ({}x{}x{}, {} mips, pc fmt {}, view fmt {view_fmt}) -> {raw:p} ({} created)", + pc_desc.Width, + pc_desc.Height, + pc_desc.DepthOrArraySize, + pc_desc.MipLevels, + pc_desc.Format.0, + n + 1 + ); + } + raw +} + +/// Obtain or create the host SRV texture backing `CreateShaderResourceViewAtX` at `va`. +/// If a GPU writer already occupies `va` with a compatible shape, creates a dedicated GPU-synced +/// SRV texture (skipping `title_textures` so CPU zeros never overwrite GPU writes); otherwise +/// delegates to `create_placed_resource_x` and registers it as a reader in case a placed UAV +/// appears at `va` later. +pub(super) unsafe fn placed_srv_resource(dev: *mut c_void, rdesc: *const u8, va: u64, view_fmt: u32) -> *mut c_void { + if rdesc.is_null() || va == 0 || !crate::gpu::shader::readable(rdesc as usize, 0x38) { + return std::ptr::null_mut(); + } + let raw_x: [u8; 0x38] = (rdesc as *const [u8; 0x38]).read_unaligned(); + let prepared = prepare_placed_srv_desc(&raw_x, view_fmt); + let key = (va, prepared); + let pc = super::device_x::pc_resource_desc(&prepared); + let pc_desc = std::ptr::read_unaligned(pc.as_ptr() as *const D3D12_RESOURCE_DESC); + let shape = TexShape::from_pc_desc(&pc_desc); + + let has_writer = { + let mut g = TABLE.lock().unwrap(); + let t = g.get_or_insert_with(AliasTable::new); + if let Some(res) = t.placed_srvs.get(&key) { + return res.as_raw(); + } + enabled() && t.has_writer_at(va as usize, &shape) + }; + + let res = if has_writer { + // Dedicated GPU-synced SRV texture in COMMON state (not registered in title_textures). + match create_internal_texture(dev, &pc, STATE_COMMON) { + Some(r) => r, + None => return std::ptr::null_mut(), + } + } else { + let mut r = std::ptr::null_mut(); + let hr = super::device_x::create_placed_resource_x( + dev, + va, + prepared.as_ptr(), + 0, + std::ptr::null(), + &ID3D12Resource::IID as *const _ as *const windows_sys::core::GUID, + &mut r, + ); + if hr < 0 || r.is_null() { + static N: AtomicU64 = AtomicU64::new(0); + if N.fetch_add(1, Ordering::Relaxed) < 8 { + crate::xlog_warn!("D3D12.X: placed SRV texture at {va:#x}: resource creation failed ({hr:#x})"); + } + return std::ptr::null_mut(); + } + ID3D12Resource::from_raw(r) + }; + + let raw = res.as_raw(); + let owner = if has_writer { Owner::PlacedSrv } else { Owner::TitlePlaced }; + let mark = { + let mut g = TABLE.lock().unwrap(); + let t = g.get_or_insert_with(AliasTable::new); + let entry = t.placed_srvs.entry(key).or_insert(res); + let raw_in = entry.as_raw(); + if raw_in == raw && enabled() { + t.insert( + raw as usize, + TrackedTex::new(dev as usize, va as usize, shape, owner, STATE_COMMON, false), + ) + } else { + Vec::new() + } + }; + for reader in mark { + super::title_textures::note_gpu_write(reader); + } + raw +} + +/// Register a title-created placed texture from `device_x::create_placed_resource_x`. +/// Returns `true` if the texture is already aliased to a GPU writer at `va` (so callers should +/// call `title_textures::note_gpu_write` and avoid CPU uploads over GPU-written texels). +pub(super) fn register_title_texture(dev: usize, res: usize, va: usize, pc_desc: &D3D12_RESOURCE_DESC, flags: u32, initial_state: u32) -> bool { + if !enabled() || res == 0 || va == 0 || pc_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER { + return false; + } + let shape = TexShape::from_pc_desc(pc_desc); + if format_element_bytes(shape.format).is_none() || shape.samples > 1 { + return false; + } + let is_writer = flags & (FLAG_RENDER_TARGET | FLAG_UAV) != 0; + let (mark, evicted) = { + let mut g = TABLE.lock().unwrap(); + let t = g.get_or_insert_with(AliasTable::new); + let evicted = t.evict_stale_internal_at(va, Some(&shape)); + let mark = t.insert( + res, + TrackedTex::new(dev, va, shape, Owner::TitlePlaced, initial_state, is_writer), + ); + (mark, evicted) + }; + if !evicted.is_empty() { + unsafe { defer_drop_resources(evicted) }; + } + let mut self_gpu_written = false; + for r in mark { + if r == res { + self_gpu_written = true; + } + super::title_textures::note_gpu_write(r); + } + self_gpu_written +} + +/// Remove a released title resource from format-alias tracking, evicting any orphaned internal +/// `PlacedUav`/`PlacedSrv` textures at the same VA via deferred GPU release. +pub(super) fn forget(res: usize) { + let evicted = { + let mut g = TABLE.lock().unwrap(); + if let Some(t) = g.as_mut() { + let mut dropped = Vec::new(); + let va = t.remove(res); + let uav_keys: Vec<_> = t.placed_uavs.iter().filter(|(_, v)| v.as_raw() as usize == res).map(|(k, _)| *k).collect(); + for k in uav_keys { + if let Some(r) = t.placed_uavs.remove(&k) { + dropped.push(r); + } + } + let srv_keys: Vec<_> = t.placed_srvs.iter().filter(|(_, v)| v.as_raw() as usize == res).map(|(k, _)| *k).collect(); + for k in srv_keys { + if let Some(r) = t.placed_srvs.remove(&k) { + dropped.push(r); + } + } + if let Some(va) = va { + dropped.extend(t.evict_stale_internal_at(va, None)); + } + dropped + } else { + Vec::new() + } + }; + if !evicted.is_empty() { + unsafe { defer_drop_resources(evicted) }; + } + if let Some(m) = LIST_STATES.lock().unwrap().as_mut() { + for s in m.values_mut() { + s.srv_states.remove(&res); + } + } +} + +/// Track state transitions of title-owned textures (even before an alias peer is created at the +/// same VA) and collect any writers leaving `UNORDERED_ACCESS` / `RENDER_TARGET` whose +/// format-aliased readers need a copy. Ignores split-barrier `BEGIN_ONLY` flags. +pub(super) fn note_title_transition(res: usize, subresource: u32, before: u32, after: u32, flags: u32, writers_to_sync: &mut Vec<(usize, u32)>) { + const BARRIER_BEGIN_ONLY: u32 = 1; + if TRACKED_TITLE_TEX.load(Ordering::Relaxed) == 0 || flags == BARRIER_BEGIN_ONLY { + return; + } + let mut g = TABLE.lock().unwrap(); + let Some(t) = g.as_mut() else { + return; + }; + let Some(tex) = t.by_res.get_mut(&res) else { + return; + }; + if tex.owner != Owner::TitlePlaced { + return; + } + tex.update_state(subresource, after); + let uniform = tex.uniform_state(); + let is_writer = tex.is_writer; + let leaving_write = (before & (STATE_UAV | STATE_RENDER_TARGET) != 0) && (after & (STATE_UAV | STATE_RENDER_TARGET) == 0); + if is_writer && leaving_write && ACTIVE_PAIRS.load(Ordering::Relaxed) != 0 && t.pairs.iter().any(|p| p.writer == res) { + if let Some(st) = uniform { + writers_to_sync.push((res, st)); + } + } +} + +/// Copy all subresources of `src` into `dst` through an intermediate buffer (`CopyTextureRegion`), +/// preserving raw texel bits across incompatible DXGI format families (e.g. `R32_UINT` -> +/// `R9G9B9E5_SHAREDEXP` or `BCn` <-> `R32G32B32A32_UINT`). +/// Callers must ensure `src` is in `COPY_SOURCE` and `dst` is in `COPY_DEST`. +pub(super) unsafe fn copy_all_subresources_through_buffer( + list: *mut c_void, + dev: &ID3D12Device, + dst: &ID3D12Resource, + src: &ID3D12Resource, +) -> bool { + let dst_desc = dst.GetDesc(); + let src_desc = src.GetDesc(); + let dst_shape = TexShape::from_pc_desc(&dst_desc); + let src_shape = TexShape::from_pc_desc(&src_desc); + if !can_copy_through_buffer(&dst_shape, &src_shape) { + return false; + } + let count = dst_shape.subresource_count().min(src_shape.subresource_count()); + for sub in 0..count { + if !copy_one_subresource_through_buffer(list, dev, dst, &dst_desc, sub, src, &src_desc, sub) { + return false; + } + } + true +} + +struct OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION); + +impl Drop for OwnedCopyLoc { + fn drop(&mut self) { + unsafe { ManuallyDrop::drop(&mut self.0.pResource) }; + } +} + +/// Verify two single-subresource footprints have identical element byte widths and compatible +/// block/texel dimensions (preventing accidental aliasing of incompatible formats that merely +/// share a 256-byte-aligned `RowPitch`). +fn subresource_footprints_compatible(dst_fmt: u32, dst_fp: &D3D12_SUBRESOURCE_FOOTPRINT, src_fmt: u32, src_fp: &D3D12_SUBRESOURCE_FOOTPRINT) -> bool { + let (Some((db, dbc)), Some((sb, sbc))) = (format_element_bytes(dst_fmt), format_element_bytes(src_fmt)) else { + return false; + }; + if db != sb || dst_fp.Depth != src_fp.Depth { + return false; + } + let eff = |bc: bool, w: u32, h: u32| -> (u32, u32) { + if bc { (w.div_ceil(4), h.div_ceil(4)) } else { (w, h) } + }; + eff(dbc, dst_fp.Width, dst_fp.Height) == eff(sbc, src_fp.Width, src_fp.Height) +} + +unsafe fn copy_one_subresource_through_buffer( + list: *mut c_void, + dev: &ID3D12Device, + dst: &ID3D12Resource, + dst_desc: &D3D12_RESOURCE_DESC, + dst_sub: u32, + src: &ID3D12Resource, + src_desc: &D3D12_RESOURCE_DESC, + src_sub: u32, +) -> bool { + let mut src_fp = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); + let mut dst_fp = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); + let mut src_rows = 0u32; + let mut dst_rows = 0u32; + let mut src_total = 0u64; + let mut dst_total = 0u64; + dev.GetCopyableFootprints(src_desc, src_sub, 1, 0, Some(&mut src_fp), Some(&mut src_rows), None, Some(&mut src_total)); + dev.GetCopyableFootprints(dst_desc, dst_sub, 1, 0, Some(&mut dst_fp), Some(&mut dst_rows), None, Some(&mut dst_total)); + if src_total == 0 + || src_total == u64::MAX + || dst_total == 0 + || dst_total == u64::MAX + || src_rows != dst_rows + || !subresource_footprints_compatible(dst_desc.Format.0 as u32, &dst_fp.Footprint, src_desc.Format.0 as u32, &src_fp.Footprint) + { + return false; + } + let row_pitch = src_fp.Footprint.RowPitch.max(dst_fp.Footprint.RowPitch); + src_fp.Footprint.RowPitch = row_pitch; + dst_fp.Footprint.RowPitch = row_pitch; + let slices = (src_fp.Footprint.Depth as u64).max(1); + let needed = (row_pitch as u64).saturating_mul(src_rows as u64).saturating_mul(slices).max(src_total).max(dst_total); + let Some(scratch_raw) = list_x::acquire_scratch_for_copy(list, needed) else { + return false; + }; + let scratch_ptr = scratch_raw as *mut c_void; + let Some(scratch) = ID3D12Resource::from_raw_borrowed(&scratch_ptr) else { + return false; + }; + + list_x::transition(list, scratch_raw, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST); + { + let src_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(src.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: src_sub }, + }); + let buf_dst_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(scratch.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: src_fp }, + }); + list_x::pc_copy_texture_region(list, &buf_dst_loc.0, 0, 0, 0, &src_loc.0, std::ptr::null()); + } + + list_x::transition(list, scratch_raw, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE); + { + let buf_src_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(scratch.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: dst_fp }, + }); + let dst_loc = OwnedCopyLoc(D3D12_TEXTURE_COPY_LOCATION { + pResource: ManuallyDrop::new(Some(dst.clone())), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: dst_sub }, + }); + list_x::pc_copy_texture_region(list, &dst_loc.0, 0, 0, 0, &buf_src_loc.0, std::ptr::null()); + } + + list_x::transition(list, scratch_raw, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_COMMON); + true +} + +unsafe fn sync_pair(list: *mut c_void, dev: &ID3D12Device, ty: D3D12_COMMAND_LIST_TYPE, pair: AliasPair, writer_state: u32) { + let w_ptr = pair.writer as *mut c_void; + let r_ptr = pair.reader as *mut c_void; + let (Some(writer_res), Some(reader_res)) = (ID3D12Resource::from_raw_borrowed(&w_ptr), ID3D12Resource::from_raw_borrowed(&r_ptr)) else { + return; + }; + let target_srv = if ty == D3D12_COMMAND_LIST_TYPE_DIRECT { STATE_SRV_ALL } else { STATE_SRV_NON_PIXEL }; + + // Determine reader's incoming state on this list. + let (reader_before, reader_after) = match pair.reader_owner { + Owner::PlacedSrv => { + let cur = LIST_STATES + .lock() + .unwrap() + .as_ref() + .and_then(|m| m.get(&(list as usize))) + .and_then(|s| s.srv_states.get(&pair.reader).copied()) + .unwrap_or(STATE_COMMON); + (cur, target_srv) + } + Owner::TitlePlaced => { + let Some(st) = TABLE + .lock() + .unwrap() + .as_ref() + .and_then(|t| t.by_res.get(&pair.reader)) + .and_then(TrackedTex::uniform_state) + else { + return; + }; + // On a compute list, never emit transitions referencing DIRECT-only states + // (`RENDER_TARGET`, `INDEX_BUFFER`, `VERTEX_AND_CONSTANT_BUFFER`, `DEPTH_*`, `PIXEL_SHADER_RESOURCE`). + if !state_valid_on_list(ty, st) { + return; + } + (st, st) + } + Owner::PlacedUav => return, + }; + + if writer_state != D3D12_RESOURCE_STATE_COPY_SOURCE.0 as u32 { + list_x::transition(list, pair.writer, D3D12_RESOURCE_STATES(writer_state as i32), D3D12_RESOURCE_STATE_COPY_SOURCE); + } + if reader_before != D3D12_RESOURCE_STATE_COPY_DEST.0 as u32 { + list_x::transition(list, pair.reader, D3D12_RESOURCE_STATES(reader_before as i32), D3D12_RESOURCE_STATE_COPY_DEST); + } + + let ok = if pair.buffer_through { + copy_all_subresources_through_buffer(list, dev, reader_res, writer_res) + } else { + list_x::pc_copy_resource(list, pair.reader, pair.writer); + true + }; + + if writer_state != D3D12_RESOURCE_STATE_COPY_SOURCE.0 as u32 { + list_x::transition(list, pair.writer, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATES(writer_state as i32)); + } + if reader_after != D3D12_RESOURCE_STATE_COPY_DEST.0 as u32 { + list_x::transition(list, pair.reader, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATES(reader_after as i32)); + } + + if pair.reader_owner == Owner::PlacedSrv { + let mut g = LIST_STATES.lock().unwrap(); + let ls = g.get_or_insert_with(HashMap::new).entry(list as usize).or_default(); + ls.list_type = Some(ty); + ls.srv_states.insert(pair.reader, reader_after); + } + + if ok { + static SYNCED: AtomicU64 = AtomicU64::new(0); + let n = SYNCED.fetch_add(1, Ordering::Relaxed); + if n < 8 || n.is_power_of_two() { + crate::xlog_info!( + "D3D12.X: synced format-aliased texture {:#x} -> {:#x} ({}, {} syncs so far)", + pair.writer, + pair.reader, + if pair.buffer_through { "buffer-through" } else { "direct copy" }, + n + 1 + ); + } + } +} + +/// Sync active placed-UAV -> format-aliased SRV pairs after a UAV barrier on `list`. +/// When `specific_targets` is `Some(targets)`, only sync pairs whose `writer` is in `targets`; +/// when `None` (global UAV barrier), deduplicate against `bind::activity()`. +pub(super) unsafe fn on_uav_barrier(list: *mut c_void, specific_targets: Option<&[usize]>) { + if ACTIVE_PLACED_UAV_PAIRS.load(Ordering::Relaxed) == 0 || list.is_null() { + return; + } + let Some(ty) = list_x::tracked_list_type(list) else { + return; + }; + if ty != D3D12_COMMAND_LIST_TYPE_DIRECT && ty != D3D12_COMMAND_LIST_TYPE_COMPUTE { + return; + } + // Skip duplicate back-to-back global UAV barriers when no draw or dispatch executed in between. + if specific_targets.is_none() { + let act = super::bind::activity(); + let mut g = LIST_STATES.lock().unwrap(); + let ls = g.get_or_insert_with(HashMap::new).entry(list as usize).or_default(); + if ls.last_activity == act && act != (0, 0) { + return; + } + ls.last_activity = act; + ls.list_type = Some(ty); + } + let Some(dev) = list_x::device_of_list(list) else { + return; + }; + let dev_raw = dev.as_raw() as usize; + let pairs: Vec = { + let g = TABLE.lock().unwrap(); + match g.as_ref() { + Some(t) => t + .pairs + .iter() + .copied() + .filter(|p| { + p.writer_owner == Owner::PlacedUav + && p.device == dev_raw + && specific_targets.is_none_or(|targets| targets.contains(&p.writer)) + }) + .collect(), + None => return, + } + }; + for pair in pairs { + sync_pair(list, &dev, ty, pair, STATE_UAV); + } +} + +/// Sync format-aliased readers for title-owned writers that just transitioned out of write state. +pub(super) unsafe fn sync_title_writers(list: *mut c_void, writers: &[(usize, u32)]) { + if writers.is_empty() || ACTIVE_PAIRS.load(Ordering::Relaxed) == 0 || list.is_null() { + return; + } + let Some(ty) = list_x::tracked_list_type(list) else { + return; + }; + if ty != D3D12_COMMAND_LIST_TYPE_DIRECT && ty != D3D12_COMMAND_LIST_TYPE_COMPUTE { + return; + } + let Some(dev) = list_x::device_of_list(list) else { + return; + }; + let dev_raw = dev.as_raw() as usize; + let mut seen = HashSet::new(); + for &(writer, state) in writers { + if !seen.insert(writer) { + continue; + } + let pairs: Vec = { + let g = TABLE.lock().unwrap(); + match g.as_ref() { + Some(t) => t.pairs.iter().copied().filter(|p| p.writer == writer && p.device == dev_raw).collect(), + None => continue, + } + }; + for pair in pairs { + sync_pair(list, &dev, ty, pair, state); + } + } +} + +/// Return any `PlacedSrv` resources transitioned into read state on `list` back to `COMMON` +/// before `Close()`, keeping cross-queue state invariants clean. +pub(super) unsafe fn before_close(list: *mut c_void) { + if list.is_null() { + return; + } + let pending: Vec<(usize, u32)> = { + let mut g = LIST_STATES.lock().unwrap(); + match g.as_mut().and_then(|m| m.get_mut(&(list as usize))) { + Some(ls) if !ls.srv_states.is_empty() => ls.srv_states.drain().collect(), + _ => return, + } + }; + for (res, state) in pending { + if state != STATE_COMMON { + list_x::transition(list, res, D3D12_RESOURCE_STATES(state as i32), D3D12_RESOURCE_STATE_COMMON); + } + } +} + +/// Clear per-list sync state on `Reset` / `Create` / `Release`. +pub(super) fn list_reset(list: usize) { + if let Some(m) = LIST_STATES.lock().unwrap().as_mut() { + m.remove(&list); + } +} + +/// Intercept `CopyResource(dst, src)` between incompatible DXGI format families of equal texel +/// width (e.g. `R32_UINT` <-> `R9G9B9E5_SHAREDEXP`) and run it through an intermediate buffer. +/// Returns `true` if handled. +pub(super) unsafe fn try_copy_resource(list: *mut c_void, dst: usize, src: usize) -> bool { + if !enabled() || dst == 0 || src == 0 || dst == src { + return false; + } + let (dp, sp) = (dst as *mut c_void, src as *mut c_void); + let (Some(d), Some(s)) = (ID3D12Resource::from_raw_borrowed(&dp), ID3D12Resource::from_raw_borrowed(&sp)) else { + return false; + }; + let (dd, sd) = (d.GetDesc(), s.GetDesc()); + if dd.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER || sd.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER { + return false; + } + if !needs_buffer_through(dd.Format.0 as u32, sd.Format.0 as u32) { + return false; + } + let Some(dev) = list_x::device_of_list(list) else { + return false; + }; + let ok = copy_all_subresources_through_buffer(list, &dev, d, s); + if ok { + static N: AtomicU64 = AtomicU64::new(0); + let k = N.fetch_add(1, Ordering::Relaxed); + if k < 8 || k.is_power_of_two() { + crate::xlog_info!( + "D3D12.X: format-aliased CopyResource fmt {} -> fmt {} ({}x{}) copied via intermediate buffer ({} so far)", + sd.Format.0, + dd.Format.0, + dd.Width, + dd.Height, + k + 1 + ); + } + } + ok +} + +/// Intercept full-subresource texture-to-texture `CopyTextureRegion` between incompatible DXGI +/// format families of equal texel width and run it through an intermediate buffer. +/// Returns `true` if handled. +pub(super) unsafe fn try_copy_texture_region( + list: *mut c_void, + dst: &D3D12_TEXTURE_COPY_LOCATION, + x: u32, + y: u32, + z: u32, + src: &D3D12_TEXTURE_COPY_LOCATION, + bx: *const D3D12_BOX, +) -> bool { + if !enabled() + || dst.Type != D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX + || src.Type != D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX + || x != 0 + || y != 0 + || z != 0 + || !bx.is_null() + { + return false; + } + let (Some(d), Some(s)) = (dst.pResource.as_ref(), src.pResource.as_ref()) else { + return false; + }; + let (dd, sd) = (d.GetDesc(), s.GetDesc()); + if dd.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER || sd.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER { + return false; + } + if !needs_buffer_through(dd.Format.0 as u32, sd.Format.0 as u32) { + return false; + } + let Some(dev) = list_x::device_of_list(list) else { + return false; + }; + copy_one_subresource_through_buffer( + list, + &dev, + d, + &dd, + dst.Anonymous.SubresourceIndex, + s, + &sd, + src.Anonymous.SubresourceIndex, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn shape(fmt: u32, w: u64, h: u32, d: u16, mips: u16) -> TexShape { + TexShape { + dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D.0, + width: w, + height: h, + depth_or_array: d, + mip_levels: mips, + format: fmt, + samples: 1, + } + } + + #[test] + fn r32_uint_and_r9g9b9e5_alias_needs_buffer_through() { + assert!(needs_buffer_through(67, 42)); // R9G9B9E5_SHAREDEXP vs R32_UINT + assert!(needs_buffer_through(67, 39)); // R9G9B9E5_SHAREDEXP vs R32_TYPELESS + assert!(!needs_buffer_through(42, 39)); // R32_UINT vs R32_TYPELESS (same family) + assert!(!needs_buffer_through(28, 27)); // R8G8B8A8_UNORM vs R8G8B8A8_TYPELESS + + let s_uav = shape(39, 128, 128, 6, 4); + let s_srv = shape(67, 128, 128, 6, 4); + assert!(can_copy_through_buffer(&s_srv, &s_uav)); + + // 3D volumetric fog froxel texture: R32_UINT UAV -> R9G9B9E5_SHAREDEXP SRV + let fog_uav = TexShape { dimension: D3D12_RESOURCE_DIMENSION_TEXTURE3D.0, ..shape(42, 160, 90, 64, 1) }; + let fog_srv = TexShape { dimension: D3D12_RESOURCE_DIMENSION_TEXTURE3D.0, ..shape(67, 160, 90, 64, 1) }; + assert!(can_copy_through_buffer(&fog_srv, &fog_uav)); + assert_eq!(fog_srv.subresource_count(), 1); + + // BC7 (16 B per 4x4 block) <-> R32G32B32A32_UINT (16 B per texel, 1/4 width & height) + let bc7 = shape(98, 256, 128, 1, 1); + let u32x4 = shape(3, 64, 32, 1, 1); + assert!(can_copy_through_buffer(&bc7, &u32x4)); + assert!(can_copy_through_buffer(&u32x4, &bc7)); + + // Mismatched dimensions or bpp rejected + assert!(!can_copy_through_buffer(&shape(67, 64, 64, 1, 1), &shape(42, 128, 128, 1, 1))); + assert!(!can_copy_through_buffer(&shape(10, 128, 128, 1, 1), &shape(42, 128, 128, 1, 1))); + } + + #[test] + fn placed_uav_and_srv_descs_are_sanitized() { + let mut x = [0u8; 0x38]; + x[0..4].copy_from_slice(&3u32.to_le_bytes()); // Texture2D + x[0x20..0x24].copy_from_slice(&67u32.to_le_bytes()); // R9G9B9E5_SHAREDEXP + x[0x30..0x34].copy_from_slice(&0x30_0005u32.to_le_bytes()); // RTV | UAV + console bits + + // Placed UAV with R32_UINT view over R9G9B9E5 desc -> R32_TYPELESS + ALLOW_UNORDERED_ACCESS (no RTV) + let uav = prepare_placed_uav_desc(&x, 42); + let uav_fmt = u32::from_le_bytes(uav[0x20..0x24].try_into().unwrap()); + let uav_flags = u32::from_le_bytes(uav[0x30..0x34].try_into().unwrap()); + assert_eq!(uav_fmt, 39, "R32_UINT UAV over R9G9B9E5 must become R32_TYPELESS, not R11G11B10_FLOAT"); + assert!(uav_flags & FLAG_UAV != 0 && uav_flags & FLAG_RENDER_TARGET == 0); + + // Placed SRV with R9G9B9E5 view -> strips RTV|UAV so pc_resource_desc keeps format 67! + let srv = prepare_placed_srv_desc(&x, 67); + let srv_pc = super::super::device_x::pc_resource_desc(&srv); + let srv_fmt = u32::from_le_bytes(srv_pc[0x20..0x24].try_into().unwrap()); + let srv_flags = u32::from_le_bytes(srv_pc[0x30..0x34].try_into().unwrap()); + assert_eq!(srv_fmt, 67, "placed SRV must stay R9G9B9E5_SHAREDEXP"); + assert_eq!(srv_flags & (FLAG_RENDER_TARGET | FLAG_UAV), 0); + } + + #[test] + fn alias_table_links_in_both_creation_orders() { + let mut t = AliasTable::new(); + let va = 0x2_0000_0000usize; + let uav_shape = shape(39, 64, 64, 1, 1); + let srv_shape = shape(67, 64, 64, 1, 1); + + // Order 1: SRV created first, placed UAV created second -> links and marks SRV gpu_written + let m1 = t.insert(0x100, TrackedTex::new(1, va, srv_shape, Owner::TitlePlaced, 0, false)); + assert!(m1.is_empty()); + assert!(t.pairs.is_empty()); + + let m2 = t.insert(0x200, TrackedTex::new(1, va, uav_shape, Owner::PlacedUav, STATE_UAV, true)); + assert_eq!(m2, vec![0x100]); + assert_eq!(t.pairs.len(), 1); + assert!(t.pairs[0].buffer_through); + assert_eq!(t.placed_uav_pairs, 1); + + // Order 2: second SRV created after UAV -> links immediately and marks itself gpu_written + let m3 = t.insert(0x300, TrackedTex::new(1, va, srv_shape, Owner::PlacedSrv, 0, false)); + assert_eq!(m3, vec![0x300]); + assert_eq!(t.pairs.len(), 2); + + // Removing the reader cleans up its pair + t.remove(0x100); + assert_eq!(t.pairs.len(), 1); + assert_eq!(t.pairs[0].reader, 0x300); + + // Removing the writer cleans up remaining pairs + t.remove(0x200); + assert!(t.pairs.is_empty()); + assert_eq!(t.placed_uav_pairs, 0); + + // Compute-list state validation rejects RENDER_TARGET (0x4), INDEX_BUFFER (0x2), VCB (0x1), + // DEPTH_WRITE (0x10), PIXEL_SHADER_RESOURCE (0x80), while allowing COMMON/UAV/NON_PIXEL_SRV/COPY. + assert!(state_valid_on_list(D3D12_COMMAND_LIST_TYPE_COMPUTE, STATE_COMMON)); + assert!(state_valid_on_list(D3D12_COMMAND_LIST_TYPE_COMPUTE, STATE_UAV)); + assert!(state_valid_on_list(D3D12_COMMAND_LIST_TYPE_COMPUTE, STATE_SRV_NON_PIXEL)); + assert!(!state_valid_on_list(D3D12_COMMAND_LIST_TYPE_COMPUTE, STATE_RENDER_TARGET)); + assert!(!state_valid_on_list(D3D12_COMMAND_LIST_TYPE_COMPUTE, 0x1)); + assert!(!state_valid_on_list(D3D12_COMMAND_LIST_TYPE_COMPUTE, 0x2)); + assert!(!state_valid_on_list(D3D12_COMMAND_LIST_TYPE_COMPUTE, STATE_SRV_ALL)); + assert!(state_valid_on_list(D3D12_COMMAND_LIST_TYPE_DIRECT, STATE_RENDER_TARGET)); + + // Subresource footprint compatibility rejects mismatched element byte widths even with equal RowPitch + let fp = |w, h, d| D3D12_SUBRESOURCE_FOOTPRINT { + Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(0), + Width: w, + Height: h, + Depth: d, + RowPitch: 256, + }; + assert!(subresource_footprints_compatible(67, &fp(32, 32, 1), 42, &fp(32, 32, 1))); + assert!(!subresource_footprints_compatible(10, &fp(32, 32, 1), 42, &fp(32, 32, 1)), "8 Bpp vs 4 Bpp must be rejected"); + assert!(subresource_footprints_compatible(98, &fp(64, 64, 1), 3, &fp(16, 16, 1)), "BC7 64x64 == R32G32B32A32 16x16"); + } +} diff --git a/crates/xcl/src/hle/d3d12x/late_release.rs b/crates/xcl/src/hle/d3d12x/late_release.rs index 18ddc684e7..07e8abb939 100644 --- a/crates/xcl/src/hle/d3d12x/late_release.rs +++ b/crates/xcl/src/hle/d3d12x/late_release.rs @@ -143,8 +143,13 @@ pub unsafe fn drain_known(seal_now: bool) { }; // (outside the lock: a release may end other objects, whose releases come back here) for res in gone { - let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(super::real::orig_fn(res as *mut c_void, 2)); - f(res as *mut c_void); + let ptr = res as *mut c_void; + let f: unsafe extern "system" fn(*mut c_void) -> u32 = if super::real::is_adopted(ptr) { + std::mem::transmute(super::real::orig_fn(ptr, 2)) + } else { + std::mem::transmute(*(*(ptr as *const *const usize)).add(2)) + }; + f(ptr); } } diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index e6e6bf6a29..9335016e45 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -124,6 +124,84 @@ pub(super) unsafe fn transition(list: *mut c_void, res: usize, before: D3D12_RES pc::(list, PC_RESOURCE_BARRIER)(list, 1, &b); } +pub(super) unsafe fn pc_resource_barriers(list: *mut c_void, barriers: &[D3D12_RESOURCE_BARRIER]) { + if !barriers.is_empty() { + pc::(list, PC_RESOURCE_BARRIER)(list, barriers.len() as u32, barriers.as_ptr()); + } +} + +pub(super) unsafe fn pc_copy_resource(list: *mut c_void, dst: usize, src: usize) { + pc::(list, PC_COPY_RESOURCE)(list, dst, src); +} + +pub(super) unsafe fn pc_copy_texture_region( + list: *mut c_void, + dst: *const D3D12_TEXTURE_COPY_LOCATION, + x: u32, + y: u32, + z: u32, + src: *const D3D12_TEXTURE_COPY_LOCATION, + bx: *const D3D12_BOX, +) { + pc::(list, PC_COPY_TEXTURE_REGION)( + list, dst, x, y, z, src, bx, + ); +} + +pub(super) unsafe fn device_of_list(list: *mut c_void) -> Option { + device_of(list) +} + +/// Restore title bindings, PC root signatures, bound PSO, and dynamic/fallback depth-bias state +/// after an internal graphics/compute sync pass on `list`. +pub(super) unsafe fn restore_after_internal_pass(list: *mut c_void, clobbered_gfx: bool, clobbered_cs: bool) { + let (bindings, bound_pso, ty, depth_bias, native_bias, had_fallback) = { + let mut g = LISTS.lock().unwrap(); + match g.as_mut().and_then(|m| m.get_mut(&(list as usize))) { + Some(s) => { + let had_fallback = s.using_fallback_pso; + if had_fallback { + s.using_fallback_pso = false; + s.pso_dirty = true; + } + ( + s.bindings.clone(), + s.bound_pso, + s.list_type.unwrap_or(D3D12_COMMAND_LIST_TYPE_DIRECT), + s.depth_bias, + s.native_bias, + had_fallback, + ) + } + None => return, + } + }; + restore_bindings(list, &bindings, ty); + let (rs_gfx, rs_cs) = super::bind::tracked_root_sigs(list); + if clobbered_gfx && rs_gfx != 0 { + pc::(list, 30)(list, rs_gfx); + } + if clobbered_cs && rs_cs != 0 { + pc::(list, 29)(list, rs_cs); + } + if bound_pso != 0 { + let resolved = super::pso_async::resolve_for_bind(bound_pso); + pc::(list, PC_SET_PIPELINE_STATE)(list, resolved); + if ty != D3D12_COMMAND_LIST_TYPE_BUNDLE { + if let Some((true, static_bias)) = device_x::base_depth_bias_info(bound_pso) { + let target = depth_bias.unwrap_or(static_bias); + if target != DepthBias::ZERO || native_bias != DepthBias::ZERO { + let _ = try_native_depth_bias(list, target); + } + } else if depth_bias.is_some() && !had_fallback { + if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(list as usize))) { + s.pso_dirty = true; + } + } + } + } +} + /// `op` with the window buffer `res` as a copy destination (back to COMMON after it) unsafe fn as_copy_dest(list: *mut c_void, res: usize, op: impl FnOnce()) { transition(list, res, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST); @@ -820,6 +898,7 @@ pub unsafe fn on_create(list: *mut c_void, alloc: usize, pso: usize, ty: D3D12_C }; drop(old); super::title_textures::list_reset(key); + super::format_alias::list_reset(key); // Dynamic-depth-bias PSOs ignore baked RasterizerState depth bias; seed the list with the // initial PSO's static bias if non-zero (CreateCommandList resets RSSetDepthBias to 0). if ty != D3D12_COMMAND_LIST_TYPE_BUNDLE && pso != 0 { @@ -838,6 +917,7 @@ pub(super) fn on_release(list: usize) { super::alloc_pool::forget_list(list); release_copy_scratch(list); super::title_textures::list_reset(list); + super::format_alias::list_reset(list); let old = LISTS.lock().unwrap().as_mut().and_then(|m| m.remove(&list)); if let Some(old) = old { if !old.pending_waits.is_empty() { @@ -2162,12 +2242,17 @@ const MAX_COPY_SCRATCH: u64 = 64 << 20; struct CopyScratch { buffer: ID3D12Resource, bytes: u64, + extra: Vec<(ID3D12Resource, u64)>, recorded: bool, untracked_submission: bool, marks: HashMap, } impl CopyScratch { + fn total_bytes(&self) -> u64 { + self.bytes + self.extra.iter().map(|(_, b)| *b).sum::() + } + fn idle(&mut self) -> bool { self.marks.retain(|_, (fence, value)| unsafe { fence.GetCompletedValue() < *value }); !self.untracked_submission && self.marks.is_empty() @@ -2189,7 +2274,7 @@ impl CopyScratchCache { if !scratch.idle() { return true; } - self.bytes -= scratch.bytes; + self.bytes -= scratch.total_bytes(); false }); } @@ -2209,12 +2294,12 @@ impl CopyScratchCache { if !scratch.recorded && !scratch.idle() { return None; } - if scratch.recorded || scratch.bytes >= bytes || bytes > MAX_COPY_SCRATCH - self.bytes + scratch.bytes { + if scratch.recorded || scratch.bytes >= bytes || bytes > MAX_COPY_SCRATCH - self.bytes + scratch.total_bytes() { scratch.recorded = true; return Some((scratch.buffer.as_raw() as usize, scratch.bytes)); } // Drop the old allocation before growing so the peak stays within the budget. - self.bytes -= self.active.remove(&key).unwrap().bytes; + self.bytes -= self.active.remove(&key).unwrap().total_bytes(); } if bytes > MAX_COPY_SCRATCH - self.bytes { return None; @@ -2222,11 +2307,33 @@ impl CopyScratchCache { let buffer = make_copy_scratch(list, bytes)?; let raw = buffer.as_raw() as usize; self.active.insert(key, CopyScratch { - buffer, bytes, recorded: true, untracked_submission: false, marks: HashMap::new(), + buffer, bytes, extra: Vec::new(), recorded: true, untracked_submission: false, marks: HashMap::new(), }); self.bytes += bytes; Some((raw, bytes)) } + + /// Acquire a scratch buffer on `list` guaranteed to hold at least `bytes` bytes (allocating + /// an extra GPU-fenced buffer on `list` if an earlier smaller scratch was already recorded). + unsafe fn acquire_exact(&mut self, list: *mut c_void, bytes: u64) -> Option { + let (raw, cap) = self.acquire(list, bytes)?; + if cap >= bytes { + return Some(raw); + } + let key = list as usize; + let scratch = self.active.get_mut(&key)?; + if let Some((buf, _)) = scratch.extra.iter().find(|(_, b)| *b >= bytes) { + return Some(buf.as_raw() as usize); + } + if bytes > MAX_COPY_SCRATCH.saturating_sub(self.bytes) { + return None; + } + let buffer = make_copy_scratch(list, bytes)?; + let raw = buffer.as_raw() as usize; + scratch.extra.push((buffer, bytes)); + self.bytes += bytes; + Some(raw) + } } static COPY_SCRATCH: LazyLock> = LazyLock::new(|| Mutex::new(CopyScratchCache::default())); @@ -2260,6 +2367,18 @@ unsafe fn copy_scratch(list: *mut c_void, n: u64) -> Option<(usize, u64)> { COPY_SCRATCH.lock().unwrap().acquire(list, bytes) } +/// Acquire a per-list DEFAULT-heap scratch buffer in `COMMON` state large enough to hold `n` bytes. +pub(super) unsafe fn acquire_scratch_for_copy(list: *mut c_void, n: u64) -> Option { + if n == 0 || n > MAX_COPY_SCRATCH || list_type(list) == D3D12_COMMAND_LIST_TYPE_BUNDLE { + return None; + } + let bytes = n.next_power_of_two().max(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT as u64); + if bytes > MAX_COPY_SCRATCH { + return COPY_SCRATCH.lock().unwrap().acquire_exact(list, n.next_multiple_of(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT as u64)); + } + COPY_SCRATCH.lock().unwrap().acquire_exact(list, bytes) +} + fn reset_copy_scratch(list: *mut c_void) { real::dispatch_chain::list_reset(list as usize); if let Some(scratch) = COPY_SCRATCH.lock().unwrap().active.get_mut(&(list as usize)) { @@ -2896,9 +3015,11 @@ pub(super) unsafe extern "system" fn dispatch_x(this: *mut c_void, x: u32, y: u3 super::bind::dispatch(this, x, y, z); } -/// command list #146 console compute/UAV barrier (used by `XboxBvhBuilder.dll` between BVH -/// build passes): emits a global UAV barrier so UMA writes become visible to the next pass -pub(super) unsafe extern "system" fn uav_barrier_x(this: *mut c_void, _n: u32, _barriers: *const c_void) { +/// command list #146 console enhanced/UAV barrier (`ID3D12GraphicsCommandList7::Barrier`, also +/// used by `XboxBvhBuilder.dll` between BVH build passes): emits a global PC UAV barrier so UMA +/// writes become visible, parses any readable `D3D12_BARRIER_GROUP` texture barriers targeting +/// tracked depth+UAV resources, and notifies `depth_uav` / `format_alias` of global UAV barriers. +pub(super) unsafe extern "system" fn uav_barrier_x(this: *mut c_void, n: u32, barriers: *const c_void) { let b = D3D12_RESOURCE_BARRIER { Type: D3D12_RESOURCE_BARRIER_TYPE_UAV, Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, @@ -2907,6 +3028,18 @@ pub(super) unsafe extern "system" fn uav_barrier_x(this: *mut c_void, _n: u32, _ }, }; pc::(this, PC_RESOURCE_BARRIER)(this, 1, &b); + if super::depth_uav::any() { + let is_direct = list_type(this) == D3D12_COMMAND_LIST_TYPE_DIRECT; + let mut depth_syncs = Vec::new(); + let had_global_uav = super::depth_uav::filter_enhanced_barriers(n, barriers, is_direct, &mut depth_syncs); + if !depth_syncs.is_empty() { + super::depth_uav::execute_syncs(this, &depth_syncs); + } + if had_global_uav { + super::depth_uav::on_global_uav_barrier(); + } + } + super::format_alias::on_uav_barrier(this, None); } /// command list #84 CopyResourceX(destination, source, flags); in place (the console's @@ -2963,7 +3096,11 @@ unsafe fn predication_buffer(list: *mut c_void) -> Option { /// Before `Close()` on a command list: return `pred_buf` to `COMMON` if it is currently in /// `D3D12_RESOURCE_STATE_PREDICATION`. pub(super) unsafe fn before_close(list: *mut c_void) { - if !PREDICATION_USED.load(Ordering::Acquire) || list.is_null() { + if list.is_null() { + return; + } + super::format_alias::before_close(list); + if !PREDICATION_USED.load(Ordering::Acquire) { return; } let pb = { @@ -2986,6 +3123,7 @@ pub(super) unsafe fn before_close(list: *mut c_void) { /// Clear active predication state when `ID3D12GraphicsCommandList::Reset` resets a recording. pub(super) fn on_pc_reset(list: *mut c_void) { reset_copy_scratch(list); + super::format_alias::list_reset(list as usize); if !PREDICATION_USED.load(Ordering::Acquire) || list.is_null() { return; } @@ -3896,6 +4034,7 @@ pub(super) unsafe fn reset_pc(this: *mut c_void, alloc: usize, pso: usize) -> i3 } reset_copy_scratch(this); + super::format_alias::list_reset(this as usize); let is_bundle = { let mut g = LISTS.lock().unwrap(); let s = g.get_or_insert_with(HashMap::new).entry(this as usize).or_default(); @@ -3945,7 +4084,11 @@ pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u3 } let mut out: Vec<[u32; 8]> = Vec::with_capacity(n as usize + 1); // a UAV barrier for all resources is in `out` / a transition from or to UNORDERED_ACCESS - let (mut any_uav, mut uav_transition) = (false, false); + let (mut any_uav, mut global_uav_barrier, mut uav_transition) = (false, false, false); + let mut uav_targets: Vec = Vec::new(); + let is_direct = tracked_list_type(this).is_none_or(|t| t == D3D12_COMMAND_LIST_TYPE_DIRECT); + let mut depth_syncs: Vec = Vec::new(); + let mut alias_writers: Vec<(usize, u32)> = Vec::new(); for k in 0..n as usize { let mut b = barriers.add(k).read_unaligned(); let (ty, flags) = (b[0], b[1]); @@ -3975,6 +4118,19 @@ pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u3 } } if ty == 2 { + // Route resource-specific UAV barriers on tracked Depth+UAV textures to both their + // materialized depth and stencil color twins before emitting the wildcard UMA UAV barrier. + let res = (b[2] as u64 | (b[3] as u64) << 32) as usize; + if res != 0 { + uav_targets.push(res); + for twin in super::depth_uav::on_uav_barrier(res) { + if twin != 0 { + out.push([2, 0, twin as u32, (twin as u64 >> 32) as u32, 0, 0, 0, 0]); + } + } + } else { + global_uav_barrier = true; + } // UAV (resource +8): translated shaders reach title memory through XCL's memory // window buffers – other resources over the same memory – which a UAV barrier for // the title's resource alone does not order (AMD drivers wait and flush for any UAV @@ -3993,18 +4149,30 @@ pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u3 } b[5] &= PC_STATES; b[6] &= PC_STATES; + let res = (b[2] as u64 | (b[3] as u64) << 32) as usize; // VERTEX_AND_CONSTANT_BUFFER / INDEX_BUFFER are buffer states on the PC; the console // takes them for textures too (Minecraft moves a texture to a generic-read // combination) – the PC list then fails to close - if (b[5] | b[6]) & BUFFER_ONLY_STATES != 0 && !is_buffer((b[2] as u64 | (b[3] as u64) << 32) as usize) { + if (b[5] | b[6]) & BUFFER_ONLY_STATES != 0 && !is_buffer(res) { b[5] &= !BUFFER_ONLY_STATES; b[6] &= !BUFFER_ONLY_STATES; } if b[5] == b[6] { continue; } + // Depth+UAV textures: rewrite invalid UNORDERED_ACCESS transitions on depth resources + // and lazily synchronize their writable color twins. + let (pass, need_uav) = super::depth_uav::filter_transition(&b, is_direct, &mut depth_syncs); + uav_transition |= need_uav; + if !pass { + continue; + } + super::format_alias::note_title_transition(res, b[4], b[5], b[6], flags, &mut alias_writers); // UAV writes before (or after) the transition: the same holds for the window buffers - uav_transition |= (b[5] | b[6]) & UNORDERED_ACCESS != 0; + if (b[5] | b[6]) & UNORDERED_ACCESS != 0 { + uav_transition = true; + uav_targets.push(res); + } } out.push(b); } @@ -4020,6 +4188,19 @@ pub(super) unsafe extern "system" fn resource_barrier_x(this: *mut c_void, n: u3 if !out.is_empty() { pc::(this, PC_RESOURCE_BARRIER)(this, out.len() as u32, out.as_ptr()); } + if !depth_syncs.is_empty() { + super::depth_uav::execute_syncs(this, &depth_syncs); + } + if !alias_writers.is_empty() { + super::format_alias::sync_title_writers(this, &alias_writers); + } + if any_uav || uav_transition { + if global_uav_barrier { + super::depth_uav::on_global_uav_barrier(); + } + let targets = if global_uav_barrier || uav_targets.is_empty() { None } else { Some(uav_targets.as_slice()) }; + super::format_alias::on_uav_barrier(this, targets); + } } /// D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER | INDEX_BUFFER: valid for buffers only @@ -4179,7 +4360,9 @@ pub(super) unsafe extern "system" fn copy_texture_region_x(this: *mut c_void, ds } } let (Some(d), Some(s)) = (pc_location(dst), pc_location(src)) else { return }; - pc::(this, PC_COPY_TEXTURE_REGION)(this, &d, x, y, z, &s, bx); + if !super::format_alias::try_copy_texture_region(this, &d, x, y, z, &s, bx) { + pc::(this, PC_COPY_TEXTURE_REGION)(this, &d, x, y, z, &s, bx); + } std::mem::forget((d, s)); // borrowed resource pointers } @@ -4205,6 +4388,7 @@ pub(super) unsafe extern "system" fn clear_depth_stencil_x(this: *mut c_void, ds ); } } + super::depth_uav::on_clear_dsv(dsv, flags); pc::(this, PC_CLEAR_DEPTH_STENCIL_VIEW)(this, dsv, flags, depth, stencil, n, rects) } @@ -4313,6 +4497,9 @@ pub(super) unsafe extern "system" fn copy_resource_x(this: *mut c_void, dst: usi } let (dd, sd) = (real::buffer_delta(dst), real::buffer_delta(src)); if dd == 0 && sd == 0 { + if super::format_alias::try_copy_resource(this, dst, src) { + return; + } return pc::(this, PC_COPY_RESOURCE)(this, dst, src); } let n = real::console_buffer(dst).or(real::console_buffer(src)).map_or(0, |b| b.1 as u64); diff --git a/crates/xcl/src/hle/d3d12x/mod.rs b/crates/xcl/src/hle/d3d12x/mod.rs index 8886624632..bfebb84554 100644 --- a/crates/xcl/src/hle/d3d12x/mod.rs +++ b/crates/xcl/src/hle/d3d12x/mod.rs @@ -13,11 +13,13 @@ mod alloc_pool; pub mod bind; pub mod console_heap; pub mod console_list; +mod depth_uav; pub mod device_x; pub mod dxgi_device; mod cpu_writes; pub mod dred; mod fence_x; +mod format_alias; pub mod frame; pub mod guard; mod late_release; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 01e3e1015e..a14aecf8a8 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1692,6 +1692,8 @@ unsafe extern "system" fn res_release(this: *mut c_void) -> u32 { super::sparse::forget_buffer(this as usize); super::vram::forget(this as usize); super::title_textures::forget(this as usize); + super::depth_uav::forget(this as usize); + super::format_alias::forget(this as usize); let gone = UMA_BUFFERS.lock().unwrap().as_mut().and_then(|m| m.remove(&(this as usize))); if let (Some((p, _, true)), Some(u)) = (gone, crate::gpu::uma::get()) { if u.is_sparse(p) { @@ -2142,7 +2144,9 @@ unsafe extern "system" fn dev_create_dsv_x(this: *mut c_void, res: *mut c_void, if copy.is_none() { copy = ID3D12Resource::from_raw_borrowed(&res).and_then(|r| default_dsv(&r.GetDesc())); } - f(this, res, copy.as_ref().map_or(desc, |c| c as *const _), cpu) + let dsv_flags = copy.as_ref().map_or(0, |c| c.Flags.0 as u32); + f(this, res, copy.as_ref().map_or(desc, |c| c as *const _), cpu); + super::depth_uav::note_dsv(cpu as usize, res as usize, dsv_flags); } /// The view a DSV without a description has, for a depth buffer `pc_resource_desc` made @@ -2341,33 +2345,6 @@ fn cube_as_cube_array(v: &D3D12_SHADER_RESOURCE_VIEW_DESC) -> Option *mut c_void { - type Key = (u64, [u8; 0x38]); - static MADE: Mutex>> = Mutex::new(None); - if desc.is_null() || va == 0 || !crate::gpu::shader::readable(desc as usize, 0x38) { - return std::ptr::null_mut(); - } - // the PC part of the desc; the flags (@0x30) are its last dword, padding follows - let mut x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned(); - x[0x34..].fill(0); - if let Some(&r) = MADE.lock().unwrap().get_or_insert_with(HashMap::new).get(&(va, x)) { - return r as *mut c_void; - } - let mut r = std::ptr::null_mut(); - let hr = super::device_x::create_placed_resource_x(this, va, x.as_ptr(), 0, std::ptr::null(), &ID3D12Resource::IID as *const _ as *const GUID, &mut r); - if hr < 0 || r.is_null() { - static N: AtomicU64 = AtomicU64::new(0); - if N.fetch_add(1, Ordering::Relaxed) < 8 { - crate::xlog_warn!("D3D12.X: view at {va:#x}: the resource cannot be made ({hr:#x}); desc {x:02x?}"); - } - r = std::ptr::null_mut(); - } - MADE.lock().unwrap().get_or_insert_with(HashMap::new).insert((va, x), r as usize); - r -} - /// device #70 (a retail game): a shader resource view of memory, without a resource object: /// (resource desc, { the PC's SRV desc; GPU address @0x28; … }, destination). unsafe extern "system" fn dev_create_srv_at_x(this: *mut c_void, rdesc: *const u8, view: *const u8, cpu: u64) { @@ -2383,7 +2360,7 @@ unsafe extern "system" fn dev_create_srv_at_x(this: *mut c_void, rdesc: *const u let words = buffer_words_at(va, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw); return super::console_heap::write(&slot, &words); } - dev_create_srv_x(this, resource_at(this, rdesc, va), &v, cpu); + dev_create_srv_x(this, super::format_alias::placed_srv_resource(this, rdesc, va, v.Format.0 as u32), &v, cpu); } /// device #71: the same for an unordered access view ({ the PC's UAV desc; GPU address @0x28; @@ -2401,7 +2378,7 @@ unsafe extern "system" fn dev_create_uav_at_x(this: *mut c_void, rdesc: *const u let words = buffer_words_at(va, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw); return super::console_heap::write(&slot, &words); } - dev_create_uav_x(this, resource_at(this, rdesc, va), std::ptr::null_mut(), &v, cpu); + dev_create_uav_x(this, super::format_alias::placed_uav_resource(this, rdesc, va, v.Format.0 as u32), std::ptr::null_mut(), &v, cpu); } /// V# of `n` elements from element `first` of the buffer at `va`. @@ -2605,21 +2582,30 @@ fn uav_companion_srv( } unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, counter: *mut c_void, desc: *const D3D12_UNORDERED_ACCESS_VIEW_DESC, cpu: u64) { + let orig_desc = desc.as_ref().copied(); + // Route depth+UAV textures (`ALLOW_DEPTH_STENCIL | ALLOW_UNORDERED_ACCESS`) to their + // writable color twin so compute writes land on a real UAV instead of a null view. + let depth_twin = super::depth_uav::resolve_uav(this, res, orig_desc.as_ref()); // the view's format as the PC's for the resource (B5G5R5A1 of a target made R8G8B8A8, // `device_x::pc_resource_desc`: an invalid call, the device removed) - let mut copy = desc.as_ref().copied(); - if let Some(c) = copy.as_mut() { - if let Some(r) = ID3D12Resource::from_raw_borrowed(&res) { - let rf = r.GetDesc().Format.0 as u32; - c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_shader_view_format(rf, c.Format.0 as u32) as i32); - } else { - c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_view_format(c.Format.0 as u32) as i32); - if c.Format == windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_UNKNOWN && view_dim_to_image_type(c.ViewDimension.0).is_some() { - c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_FLOAT; + let mut copy = orig_desc; + if depth_twin.is_none() { + if let Some(c) = copy.as_mut() { + if let Some(r) = ID3D12Resource::from_raw_borrowed(&res) { + let rf = r.GetDesc().Format.0 as u32; + c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_shader_view_format(rf, c.Format.0 as u32) as i32); + } else { + c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_view_format(c.Format.0 as u32) as i32); + if c.Format == windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_UNKNOWN && view_dim_to_image_type(c.ViewDimension.0).is_some() { + c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_FLOAT; + } } } } - let desc = copy.as_ref().map_or(desc, |c| c as *const _); + let (uav_res, uav_desc) = match &depth_twin { + Some((twin_res, patched)) => (*twin_res, patched as *const D3D12_UNORDERED_ACCESS_VIEW_DESC), + None => (res, copy.as_ref().map_or(desc, |c| c as *const _)), + }; super::present::note_use(res as usize, super::present::USE_UAV); let f: unsafe extern "system" fn(*mut c_void, *mut c_void, *mut c_void, *const D3D12_UNORDERED_ACCESS_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 19)); let rd_opt = ID3D12Resource::from_raw_borrowed(&res).map(|r| r.GetDesc()); @@ -2630,15 +2616,15 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, "frame trace: UAV of render target {res:p} {:?} at {cpu:#x}: console slot {:?}, view {:?}", rd_opt.map(|d| (d.Width, d.Height, d.Format.0)), s.as_ref().map(|s| (s.index, s.pc_uav, s.at)), - desc.as_ref().map(|v| (v.Format.0, v.ViewDimension.0)) + uav_desc.as_ref().map(|v| (v.Format.0, v.ViewDimension.0)) ); } } - // a resource without ALLOW_UNORDERED_ACCESS (a console depth buffer that also had it: the PC - // refuses the pair, `device_x::pc_resource_desc`): a null view – a UAV of it is an invalid - // call, and the device is removed (0x887A0001) - let no_uav = rd_opt.is_some_and(|d| d.Flags.0 & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.0 == 0); - let null_desc = null_uav_desc(desc.as_ref(), rd_opt.as_ref()); + // a resource without ALLOW_UNORDERED_ACCESS (a console depth buffer without a twin or + // another non-UAV resource): a null view – a UAV of it is an invalid call (0x887A0001) + let uav_rd_opt = if uav_res == res { rd_opt } else { ID3D12Resource::from_raw_borrowed(&uav_res).map(|r| r.GetDesc()) }; + let no_uav = uav_rd_opt.is_some_and(|d| d.Flags.0 & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.0 == 0); + let null_desc = null_uav_desc(uav_desc.as_ref(), uav_rd_opt.as_ref()); let pc_uav = |at: u64| { if no_uav { static N: AtomicU64 = AtomicU64::new(0); @@ -2647,18 +2633,18 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, } f(this, std::ptr::null_mut(), std::ptr::null_mut(), &null_desc, at) } else { - f(this, res, counter, desc, at) + f(this, uav_res, counter, uav_desc, at) } }; let Some(slot) = super::console_heap::slot(cpu) else { return pc_uav(cpu) }; pc_uav(slot.pc_uav); super::console_heap::publish(&slot, true); super::console_heap::note_resource(&slot, res as usize); - let Some(r) = ID3D12Resource::from_raw_borrowed(&res) else { + let Some(r) = ID3D12Resource::from_raw_borrowed(&uav_res) else { // A null image UAV was already created and published at `slot.index + 1`. Create a // matching null companion SRV at `slot.index` and write a valid T# referencing // `slot.index` so shaders never fall back to `ResourceDescriptorHeap[0]` / `[1]`. - if let Some(v) = desc.as_ref() { + if let Some(v) = uav_desc.as_ref() { if let Some(ty) = view_dim_to_image_type(v.ViewDimension.0) { if let Some(srv) = uav_companion_srv(None, Some(v), true) { let g: unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_SHADER_RESOURCE_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 18)); @@ -2671,14 +2657,14 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, } return super::console_heap::write(&slot, &[0; 8]); }; - let rd = rd_opt.unwrap(); - if let Some(v) = desc.as_ref().filter(|v| v.ViewDimension == D3D12_UAV_DIMENSION_BUFFER) { + let rd = uav_rd_opt.unwrap(); + if let Some(v) = uav_desc.as_ref().filter(|v| v.ViewDimension == D3D12_UAV_DIMENSION_BUFFER) { let b = &v.Anonymous.Buffer; let raw = b.Flags.0 & D3D12_BUFFER_UAV_FLAG_RAW.0 != 0; return super::console_heap::write(&slot, &buffer_view(res as usize, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw)); } // UAV dimensions: 1 buffer, 2 tex1d, 3 tex1d array, 4 tex2d, 5 tex2d array, 8 tex3d - let (format, dim, array, mip) = match desc.as_ref() { + let (format, dim, array, mip) = match uav_desc.as_ref() { Some(v) => { let a = &v.Anonymous; let (array, mip) = match v.ViewDimension.0 { @@ -2699,9 +2685,10 @@ unsafe extern "system" fn dev_create_uav_x(this: *mut c_void, res: *mut c_void, }; // RDNA images have no SRV / UAV distinction: console shaders may read any image UAV slot // (`image_load`, `image_sample_*`, `image_get_resinfo` all index `ResourceDescriptorHeap[slot.index]`). + // Use `uav_res` / `uav_desc` so Depth+UAV routed slots pair their SRV against the color twin. let deny_srv = rd.Flags.0 & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE.0 != 0; - if let Some(srv) = uav_companion_srv(Some(&rd), desc.as_ref(), deny_srv) { - let srv_res = if deny_srv { std::ptr::null_mut() } else { res }; + if let Some(srv) = uav_companion_srv(Some(&rd), uav_desc.as_ref(), deny_srv) { + let srv_res = if deny_srv { std::ptr::null_mut() } else { uav_res }; let g: unsafe extern "system" fn(*mut c_void, *mut c_void, *const D3D12_SHADER_RESOURCE_VIEW_DESC, u64) = std::mem::transmute(orig_fn(this, 18)); g(this, srv_res, &srv, slot.pc_srv); super::console_heap::publish(&slot, false); @@ -2803,8 +2790,8 @@ unsafe extern "system" fn dev_create_committed_resource_x(this: *mut c_void, pro // not placed in unified memory (e.g. its coherent window kept for readback heaps): a PC // resource of the PC's description – the console's flag bits (0x1000000, 0x300000 …) and // alignments made the PC refuse Fortnite's 3264x1836 scene targets (E_INVALIDARG, a crash) - let pc_full = (console_desc != 0 && crate::gpu::shader::readable(console_desc, 0x38)) - .then(|| super::device_x::pc_resource_desc_for_device(this, &(console_desc as *const [u8; 0x38]).read_unaligned())); + let console_raw = (console_desc != 0 && crate::gpu::shader::readable(console_desc, 0x38)).then(|| (console_desc as *const [u8; 0x38]).read_unaligned()); + let pc_full = console_raw.as_ref().map(|raw| super::device_x::pc_resource_desc_for_device(this, raw)); let desc = pc_full.as_ref().map_or(desc, |d| d.as_ptr() as usize); let mut clear_buf = [0u8; 24]; let clear = match pc_full.as_ref() { @@ -2819,6 +2806,11 @@ unsafe extern "system" fn dev_create_committed_resource_x(this: *mut c_void, pro super::device_x::pc_initial_state_for_desc(orig_flags, pc_flags, state as u32) }); let hr = dev_create_committed_resource(this, props, flags, desc, st as usize, clear, riid, ppv); + if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { + if let Some(ref raw_x) = console_raw { + super::depth_uav::register_if_depth_uav(this, *ppv, raw_x, st, state as u32); + } + } if hr < 0 { let pw = |o: usize| if props == 0 { 0 } else { *((props + o) as *const u32) }; crate::xlog_warn!( @@ -3264,7 +3256,11 @@ pub(super) unsafe extern "system" fn dev_create_placed_resource_x(this: *mut c_v u(0), u64::from_le_bytes(x[0x10..0x18].try_into().unwrap()), u(0x18), u(0x20), u(0x2C), u(0x30) ); } - hr3.unwrap_or(hr2) + let out_hr = hr3.unwrap_or(hr2); + if out_hr >= 0 && is_texture && !ppv.is_null() && !(*ppv).is_null() { + super::depth_uav::register_if_depth_uav(this, *ppv, &x, st, state as u32); + } + out_hr } /// A texture the title placed in a heap by the console's size (`get_resource_allocation_info_x` @@ -3293,12 +3289,17 @@ unsafe fn own_memory_if_larger_on_the_pc(this: *mut c_void, heap: usize, offset: let st = super::device_x::pc_initial_state(u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()), state as u32); let mut clear_buf = [0u8; 24]; let clear = super::device_x::pc_clear_for_desc(clear as *const c_void, u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()), &mut clear_buf) as usize; + let mut effective_st = st; let mut hr = dev_create_committed_resource(this, &h.Properties as *const _ as usize, 0, d.as_ptr() as usize, st as usize, clear, riid, ppv); if hr < 0 && st != 0 { // as in the past-the-end fallback: a state the PC refuses for this desc (e.g. UAV // without the UAV flag) – COMMON rather than falling back to the overlapping placement + effective_st = 0; hr = dev_create_committed_resource(this, &h.Properties as *const _ as usize, 0, d.as_ptr() as usize, 0, clear, riid, ppv); } + if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { + super::depth_uav::register_if_depth_uav(this, *ppv, &x, effective_st, state as u32); + } static N: AtomicU64 = AtomicU64::new(0); if N.fetch_add(1, Ordering::Relaxed) < 8 { let u = |o: usize| u32::from_le_bytes(x[o..o + 4].try_into().unwrap()); @@ -6830,6 +6831,9 @@ unsafe extern "system" fn cl_clear_rtv(this: *mut c_void, rtv: usize, color: *co } unsafe extern "system" fn cl_om_set_render_targets(this: *mut c_void, n: u32, rtvs: *const usize, single: i32, dsv: *const usize) { + if !dsv.is_null() { + super::depth_uav::before_bind_dsv(this, *dsv); + } type F = unsafe extern "system" fn(*mut c_void, u32, *const usize, i32, *const usize); let f: F = std::mem::transmute(orig_fn(this, slot_of(this, "OMSetRenderTargets"))); f(this, n, rtvs, single, dsv);