diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index 59c5e2f9f6..e43a94e857 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -343,7 +343,7 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) { let key = queue as usize; if !s.fences.contains_key(&key) { let mut dev: Option = None; - let Some(f) = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| d.CreateFence::(0, D3D12_FENCE_FLAG_NONE).ok()) else { return }; + let Some(f) = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| super::real::create_raw_fence(&d).ok()) else { return }; s.fences.insert(key, (f, 0)); } let (fence, value) = { diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 6ee560428b..b6827e1032 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2161,7 +2161,7 @@ unsafe fn ensure_cell_fence(va: u64, dev: &ID3D12Device, dev_raw: usize) -> Opti } } } - let created: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).ok()?; + let created: ID3D12Fence = real::create_raw_fence(&dev).ok()?; let mut g = SYNC_GRAPH.lock().unwrap(); let cell = g.get_or_insert_with(SyncGraph::default).cells.entry(va).or_default(); if cell.non_monotonic { @@ -2925,7 +2925,7 @@ pub(super) unsafe fn copy_scratch_submitted(queue: *mut c_void, lists: &[*mut c_ if let std::collections::hash_map::Entry::Vacant(entry) = cache.fences.entry(key) { let mut dev: Option = None; q.GetDevice(&mut dev).ok()?; - entry.insert((dev?.CreateFence::(0, D3D12_FENCE_FLAG_NONE).ok()?, 0)); + entry.insert((real::create_raw_fence(&dev?).ok()?, 0)); } let (fence, value) = cache.fences.get_mut(&key)?; let next = *value + 1; @@ -3352,7 +3352,10 @@ pub(super) unsafe fn record_deferred(wrapper: *mut c_void) { None => super::alloc_pool::title_reset(own).unwrap_or_else(|| real::reset_pc_allocator(own)), }; if hr >= 0 { - hr = super::alloc_pool::reset_list(seg as usize, own as usize, ty.0, |pc_alloc| pc:: i32>(seg, PC_RESET)(seg, pc_alloc, 0)); + hr = super::alloc_pool::reset_list(seg as usize, own as usize, ty.0, |pc_alloc| { + let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void); + pc:: i32>(seg, PC_RESET)(seg, pc_alloc, 0) + }); } real::preserve_vtable(seg, ext); if hr < 0 { @@ -3486,6 +3489,8 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ let ext_spare = *(spare as *const usize); let reset_hr = super::alloc_pool::reset_list(spare as usize, alloc, ty.0, |pc_alloc| { on = pc_alloc; + let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void); + let _pso_guard = real::UnadoptGuard::new(init_pso as *mut c_void); pc:: i32>(spare, PC_RESET)(spare, pc_alloc, init_pso) }); real::preserve_vtable(spare, ext_spare); @@ -3643,7 +3648,10 @@ unsafe fn deferred_segment(wrapper: *mut c_void, list: *mut c_void, ty: D3D12_CO let emptied = |p: &*mut c_void| { let p = *p; let ext = *(p as *const usize); - let hr = super::alloc_pool::reset_list(p as usize, own, ty.0, |pc_alloc| pc:: i32>(p, PC_RESET)(p, pc_alloc, 0)); + let hr = super::alloc_pool::reset_list(p as usize, own, ty.0, |pc_alloc| { + let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void); + pc:: i32>(p, PC_RESET)(p, pc_alloc, 0) + }); real::preserve_vtable(p, ext); hr >= 0 }; @@ -4129,7 +4137,10 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { if let Some((_, ref prior, _)) = kickoff_split { if !prior.is_empty() { execute_with_waits_on(q_raw, prior, |chunk| { - exec(q_raw, chunk.len() as u32, chunk.as_ptr()); + { + let _list_guards: Vec<_> = chunk.iter().filter_map(|&l| real::UnadoptGuard::new(l)).collect(); + exec(q_raw, chunk.len() as u32, chunk.as_ptr()); + } super::gpu_times::executed(q_raw, chunk); copy_scratch_submitted(q_raw, chunk); super::alloc_pool::executed(q_raw, chunk); @@ -4150,7 +4161,10 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { } let one = [this]; - exec(q_raw, 1, one.as_ptr()); + { + let _list_guard = real::UnadoptGuard::new(this); + exec(q_raw, 1, one.as_ptr()); + } super::gpu_times::executed(q_raw, &one); copy_scratch_submitted(q_raw, &one); super::alloc_pool::executed(q_raw, &one); @@ -4209,6 +4223,8 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let ext_closed = *(target as *const usize); let init_pso = super::pso_async::resolve_for_bind(bound_pso); let reset_hr = super::alloc_pool::reset_list(target as usize, alloc, list_type(target).0, |pc_alloc| { + let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void); + let _pso_guard = real::UnadoptGuard::new(init_pso as *mut c_void); pc:: i32>(target, PC_RESET)(target, pc_alloc, init_pso) }); real::preserve_vtable(target, ext_closed); @@ -5064,6 +5080,8 @@ pub(super) unsafe fn reset_pc(this: *mut c_void, alloc: usize, pso: usize) -> i3 let mut on = alloc; let hr = super::alloc_pool::reset_list(this as usize, alloc, ty, |pc_alloc| { on = pc_alloc; + let _alloc_guard = real::UnadoptGuard::new(pc_alloc as *mut c_void); + let _pso_guard = real::UnadoptGuard::new(resolved as *mut c_void); pc:: i32>(this, PC_RESET)(this, pc_alloc, resolved) }); real::preserve_vtable(this, ext); diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 9859adb05a..1ffe667960 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -517,7 +517,7 @@ unsafe fn open_uma(device: &ID3D12Device, allow_driver_remap: bool) { pub fn stats() -> D3d12Stats { let classes = CLASSES - .lock() + .read() .map(|g| { g.iter() .map(|c| ClassStat { interface: c.iface.name.to_string(), public_methods: c.public, internal_slots: c.scanned - c.public }) @@ -793,16 +793,75 @@ struct Class { scanned: usize, } -static CLASSES: Mutex> = Mutex::new(Vec::new()); -static EXTS: Mutex>> = Mutex::new(None); +static CLASSES: std::sync::RwLock> = std::sync::RwLock::new(Vec::new()); +static EXTS: std::sync::RwLock>> = std::sync::RwLock::new(None); +const EXT_FAST_CAP: usize = 64; +static EXT_FAST: [AtomicUsize; EXT_FAST_CAP] = [const { AtomicUsize::new(0) }; EXT_FAST_CAP]; +static EXT_FAST_N: AtomicUsize = AtomicUsize::new(0); +static NATIVE_FAST: [AtomicUsize; EXT_FAST_CAP] = [const { AtomicUsize::new(0) }; EXT_FAST_CAP]; +static NATIVE_FAST_N: AtomicUsize = AtomicUsize::new(0); +static NATIVE_FAST_LOCK: Mutex<()> = Mutex::new(()); + +fn register_ext(ext: usize) { + let mut g = EXTS.write().unwrap(); + if g.get_or_insert_with(HashSet::new).insert(ext) { + let n = EXT_FAST_N.load(Ordering::Relaxed); + if n < EXT_FAST_CAP { + EXT_FAST[n].store(ext, Ordering::Relaxed); + EXT_FAST_N.store(n + 1, Ordering::Release); + } + } +} + +fn is_ext_fast(v: usize) -> bool { + let n = EXT_FAST_N.load(Ordering::Acquire); + for slot in &EXT_FAST[..n.min(EXT_FAST_CAP)] { + if slot.load(Ordering::Relaxed) == v { + return true; + } + } + n >= EXT_FAST_CAP && is_ext(v) +} + +fn register_native(orig: usize) { + if is_known_native(orig) { + return; + } + let _g = NATIVE_FAST_LOCK.lock().unwrap(); + let n = NATIVE_FAST_N.load(Ordering::Relaxed); + for slot in &NATIVE_FAST[..n.min(EXT_FAST_CAP)] { + if slot.load(Ordering::Relaxed) == orig { + return; + } + } + if n < EXT_FAST_CAP { + NATIVE_FAST[n].store(orig, Ordering::Relaxed); + NATIVE_FAST_N.store(n + 1, Ordering::Release); + } +} + +fn is_known_native(v: usize) -> bool { + let n = NATIVE_FAST_N.load(Ordering::Acquire); + for slot in &NATIVE_FAST[..n.min(EXT_FAST_CAP)] { + if slot.load(Ordering::Relaxed) == v { + return true; + } + } + false +} unsafe fn vt(obj: *mut c_void) -> *const usize { - *(obj as *const *const usize) + (*(obj as *const AtomicUsize)).load(Ordering::Acquire) as *const usize } /// Original function of `slot` for an adopted object. pub unsafe fn orig_fn(this: *mut c_void, slot: usize) -> usize { let ext = vt(this); + if crate::util::under_wine() && is_known_native(ext as usize) { + // Another thread's UnadoptGuard temporarily restored the native vtable on this + // shared object (e.g. a pipeline state); `ext` is already the original table. + return *ext.add(slot); + } let orig = *ext.sub(1) as *const usize; *orig.add(slot) } @@ -852,11 +911,11 @@ pub(super) unsafe fn this_first(a: *mut c_void, b: *mut c_void) -> bool { } fn is_ext(v: usize) -> bool { - EXTS.lock().map(|g| g.as_ref().map(|s| s.contains(&v)).unwrap_or(false)).unwrap_or(false) + EXTS.read().map(|g| g.as_ref().map(|s| s.contains(&v)).unwrap_or(false)).unwrap_or(false) } fn kind_of(ext: usize) -> Option { - CLASSES.lock().unwrap().iter().find(|c| c.ext == ext).map(|c| c.kind) + CLASSES.read().unwrap().iter().find(|c| c.ext == ext).map(|c| c.kind) } /// If a native D3D12 method (such as `Close` or `Reset` on Agility SDK 1.620+, or copy-list @@ -885,6 +944,101 @@ pub(super) unsafe fn readopt(obj: *mut c_void, kind: Kind) { *(obj as *mut usize) = class_for(obj, orig, kind); } +/// Active `UnadoptGuard` counts per object pointer so concurrent guards on a shared object +/// (such as a pipeline state, heap, or resource) keep the native vtable installed until the +/// last guard drops, while pinning a COM reference (`AddRef` before unadopt, hooked `Release` +/// after restoring the extended vtable) so a concurrent `Release` through the native vtable +/// cannot drop the refcount to zero and bypass XCL's final-release cleanup (`pso_release`, +/// `heap_release`, `res_release`, `cl_release`, `alloc_release`). +static UNADOPTED: Mutex>> = Mutex::new(None); + +/// Temporarily restores an adopted object's native vtable for a call into `d3d12core.dll` +/// (vkd3d-proton validates `iface->lpVtbl` on command allocators and command lists). +pub(crate) struct UnadoptGuard { + obj: *mut c_void, +} + +impl UnadoptGuard { + pub(crate) unsafe fn new(obj: *mut c_void) -> Option { + if !crate::util::under_wine() || obj.is_null() || (obj as usize) & 7 != 0 || !crate::gpu::shader::readable(obj as usize, 8) { + return None; + } + let mut g = UNADOPTED.lock().unwrap(); + let map = g.get_or_insert_with(HashMap::new); + if let Some((_, count)) = map.get_mut(&(obj as usize)) { + *count += 1; + return Some(Self { obj }); + } + let cur = (*(obj as *const AtomicUsize)).load(Ordering::Acquire); + if !is_ext_fast(cur) { + return None; + } + let orig = *(cur as *const usize).sub(1); + // Pin +1 COM reference via native AddRef before exposing `orig`, so any concurrent + // Release observing the native vtable sees refcount >= 2 and cannot finalize the object. + let add_ref: RefFn = std::mem::transmute(*(orig as *const usize).add(1)); + add_ref(obj); + register_native(orig); + (*(obj as *const AtomicUsize)).store(orig, Ordering::Release); + map.insert(obj as usize, (cur, 1)); + Some(Self { obj }) + } + + #[cfg(test)] + unsafe fn enter(obj: *mut c_void) -> Self { + let mut g = UNADOPTED.lock().unwrap(); + let map = g.get_or_insert_with(HashMap::new); + if let Some((_, count)) = map.get_mut(&(obj as usize)) { + *count += 1; + return Self { obj }; + } + let ext = (*(obj as *const AtomicUsize)).load(Ordering::Acquire); + let orig = *(ext as *const usize).sub(1); + let add_ref: RefFn = std::mem::transmute(*(orig as *const usize).add(1)); + add_ref(obj); + register_native(orig); + (*(obj as *const AtomicUsize)).store(orig, Ordering::Release); + map.insert(obj as usize, (ext, 1)); + Self { obj } + } +} + +impl Drop for UnadoptGuard { + fn drop(&mut self) { + let release_ext = { + let mut g = UNADOPTED.lock().unwrap(); + if let Some(map) = g.as_mut() { + if let Some((ext, count)) = map.get_mut(&(self.obj as usize)) { + *count -= 1; + if *count == 0 { + let ext = *ext; + map.remove(&(self.obj as usize)); + unsafe { + (*(self.obj as *const AtomicUsize)).store(ext, Ordering::Release); + } + Some(ext) + } else { + None + } + } else { + None + } + } else { + None + } + }; + // Release our pinned reference outside `UNADOPTED` via XCL's hooked Release slot so if + // another thread dropped the last title reference while the guard was active, the final + // 1 -> 0 transition always runs XCL's cleanup hook. + if let Some(ext) = release_ext { + unsafe { + let rel: RefFn = std::mem::transmute(*(ext as *const usize).add(2)); + rel(self.obj); + } + } + } +} + /// Give a genuine D3D12 object the extended vtable for `kind`. pub unsafe fn adopt(obj: *mut c_void, kind: Kind) { if obj.is_null() { @@ -943,7 +1097,7 @@ unsafe fn release_through_its_own_vtable(p: *mut c_void) -> u32 { } unsafe fn class_for(obj: *mut c_void, orig: usize, kind: Kind) -> usize { - if let Some(c) = CLASSES.lock().unwrap().iter().find(|c| c.orig == orig && c.kind == kind) { + if let Some(c) = CLASSES.read().unwrap().iter().find(|c| c.orig == orig && c.kind == kind) { return c.ext; } let ot = orig as *const usize; @@ -1060,14 +1214,14 @@ unsafe fn class_for(obj: *mut c_void, orig: usize, kind: Kind) -> usize { } let leaked: &'static [usize] = Box::leak(v.into_boxed_slice()); let ext = leaked.as_ptr() as usize + 8; - EXTS.lock().unwrap().get_or_insert_with(HashSet::new).insert(ext); + register_ext(ext); crate::xlog_info!( "D3D12.X: {} – {public} PC methods, {} internal slots under the guard, +{EXTRA_SLOTS} stubs ({kind:?}, PC vtable {})", iface.name, scanned - public, crate::modules::describe_address(orig) ); - CLASSES.lock().unwrap().push(Class { orig, kind, ext, iface, public, scanned }); + CLASSES.write().unwrap().push(Class { orig, kind, ext, iface, public, scanned }); ext } @@ -4168,7 +4322,14 @@ fn overrides(kind: Kind) -> Vec<(&'static str, usize)> { ]), Kind::CommandQueue => { let [update, copy] = tile_mapping_entries(); - v.extend_from_slice(&[("Release", q_release as usize), ("ExecuteCommandLists", execute_entry()), ("UpdateTileMappings", update), ("CopyTileMappings", copy)]) + v.extend_from_slice(&[ + ("Release", q_release as usize), + ("ExecuteCommandLists", execute_entry()), + ("UpdateTileMappings", update), + ("CopyTileMappings", copy), + ("Signal", q_signal_pc as usize), + ("Wait", q_wait_pc as usize), + ]) } Kind::CommandAllocator => v.extend_from_slice(&[("Release", alloc_release as usize), ("Reset", alloc_reset as usize)]), Kind::Resource => v.extend_from_slice(&[ @@ -4196,7 +4357,7 @@ fn overrides(kind: Kind) -> Vec<(&'static str, usize)> { /// Slot of `method` in the class of an adopted object. unsafe fn slot_of(this: *mut c_void, method: &str) -> usize { let ext = vt(this) as usize; - let g = CLASSES.lock().unwrap(); + let g = CLASSES.read().unwrap(); let c = g.iter().find(|c| c.ext == ext).expect("adopted class"); layout::slot(c.iface, method).expect("method in layout") } @@ -4259,7 +4420,11 @@ unsafe extern "system" fn cl_reset(this: *mut c_void, allocator: *mut c_void, in let f: unsafe extern "system" fn(*mut c_void, *mut c_void, *mut c_void) -> HRESULT = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12GraphicsCommandList", "Reset"))); let resolved = super::pso_async::resolve_for_bind(initial_state as usize) as *mut c_void; - let hr = f(this, allocator, resolved); + let hr = { + let _alloc_guard = UnadoptGuard::new(allocator); + let _pso_guard = UnadoptGuard::new(resolved); + f(this, allocator, resolved) + }; preserve_vtable(this, ext); if hr >= 0 && !super::title_textures::is_own_work() { super::list_x::on_pc_reset(this); @@ -4442,7 +4607,12 @@ fn is_own_object(kind: Kind) -> bool { creator!(dev_create_command_queue, "CreateCommandQueue", Kind::CommandQueue, [a1]); creator!(dev_create_command_queue1, "CreateCommandQueue1", Kind::CommandQueue, [a1, a2]); creator!(dev_create_command_allocator, "CreateCommandAllocator", Kind::CommandAllocator, [a1]); -creator!(dev_create_command_list, "CreateCommandList", Kind::CommandList, [a1, a2, a3, a4]); +creator!(dev_create_command_list_inner, "CreateCommandList", Kind::CommandList, [a1, a2, a3, a4]); +pub(super) unsafe extern "system" fn dev_create_command_list(this: *mut c_void, a1: usize, a2: usize, a3: usize, a4: usize, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + let _alloc_guard = UnadoptGuard::new(a3 as *mut c_void); + let _pso_guard = UnadoptGuard::new(a4 as *mut c_void); + dev_create_command_list_inner(this, a1, a2, a3, a4, riid, ppv) +} creator!(dev_create_command_list1, "CreateCommandList1", Kind::CommandList, [a1, a2, a3]); /// Create a raw adopted PC `ID3D12GraphicsCommandList` without wrapping it in `title_list` @@ -4464,6 +4634,8 @@ pub(super) unsafe fn create_raw_pc_command_list( type F = unsafe extern "system" fn(*mut c_void, u32, D3D12_COMMAND_LIST_TYPE, usize, usize, *const GUID, *mut *mut c_void) -> HRESULT; let slot = layout::slot(layout::find("ID3D12Device")?, "CreateCommandList")?; let f: F = std::mem::transmute(*vt(dev).add(slot)); + let _alloc_guard = UnadoptGuard::new(alloc as *mut c_void); + let _pso_guard = UnadoptGuard::new(init_pso as *mut c_void); let hr = f(dev, 0, ty, alloc, init_pso, &iid, &mut out); if hr >= 0 && !out.is_null() { adopt(out, Kind::CommandList); @@ -4476,7 +4648,11 @@ creator!(dev_create_descriptor_heap, "CreateDescriptorHeap", Kind::DescriptorHea creator!(dev_create_committed_resource, "CreateCommittedResource", Kind::Resource, [a1, a2, a3, a4, a5]); creator!(dev_create_committed_resource1, "CreateCommittedResource1", Kind::Resource, [a1, a2, a3, a4, a5, a6]); creator!(dev_create_committed_resource2, "CreateCommittedResource2", Kind::Resource, [a1, a2, a3, a4, a5, a6]); -creator!(dev_create_placed_resource, "CreatePlacedResource", Kind::Resource, [a1, a2, a3, a4, a5]); +creator!(dev_create_placed_resource_inner, "CreatePlacedResource", Kind::Resource, [a1, a2, a3, a4, a5]); +pub(super) unsafe extern "system" fn dev_create_placed_resource(this: *mut c_void, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + let _heap_guard = UnadoptGuard::new(a1 as *mut c_void); + dev_create_placed_resource_inner(this, a1, a2, a3, a4, a5, riid, ppv) +} /// D3D12_RESOURCE_STATES the PC knows (as `list_x::PC_STATES`) pub(super) const PC_RESOURCE_STATES: u32 = 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80 | 0x100 | 0x200 | 0x400 | 0x800 | 0x1000 | 0x2000 | 0xF_0000 | 0x20_0000 | 0x40_0000 | 0x80_0000 | 0x100_0000; @@ -4639,7 +4815,11 @@ unsafe fn pc_allocation_size(device: *mut c_void, d: &[u8; 0x38]) -> u64 { f(device, &mut r, 0, 1, &safe); r[0] } -creator!(dev_create_placed_resource1, "CreatePlacedResource1", Kind::Resource, [a1, a2, a3, a4, a5]); +creator!(dev_create_placed_resource1_inner, "CreatePlacedResource1", Kind::Resource, [a1, a2, a3, a4, a5]); +unsafe extern "system" fn dev_create_placed_resource1(this: *mut c_void, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + let _heap_guard = UnadoptGuard::new(a1 as *mut c_void); + dev_create_placed_resource1_inner(this, a1, a2, a3, a4, a5, riid, ppv) +} creator!(pc_create_reserved_resource, "CreateReservedResource", Kind::Resource, [a1, a2, a3]); creator!(pc_create_reserved_resource1, "CreateReservedResource1", Kind::Resource, [a1, a2, a3, a4]); @@ -4866,6 +5046,146 @@ unsafe extern "system" fn mem_heap_release(this: *mut c_void) -> u32 { } creator!(dev_create_fence_pc, "CreateFence", Kind::Fence, [a1, a2]); +/// Create an un-proxied `ID3D12Fence` for XCL's own internal queue tracking through the device's +/// original vtable slot, so under Wine/vkd3d-proton it can be signalled on raw internal queues +/// (`sparse::queue()`) and passed to native APIs without hitting `FenceProxy` vtable mismatches +/// (run55 wedge + run54 crash: rb fences adopted through the wrapped device never satisfied +/// `rb_queue.Wait`). Local port of the tree-C2 helper; the a9308c48 sparse.rs fix calls it. +pub(super) unsafe fn create_raw_fence(dev: &ID3D12Device) -> windows::core::Result { + let raw = dev.as_raw(); + if is_ext(vt(raw) as usize) { + type F = unsafe extern "system" fn(*mut c_void, u64, D3D12_FENCE_FLAGS, *const GUID, *mut *mut c_void) -> HRESULT; + let f: F = std::mem::transmute(orig_fn(raw, slot_of(raw, "CreateFence"))); + let mut out: *mut c_void = std::ptr::null_mut(); + let hr = f(raw, 0, D3D12_FENCE_FLAG_NONE, (&ID3D12Fence::IID as *const windows::core::GUID).cast(), &mut out); + if hr < 0 { + return Err(windows::core::Error::from_hresult(windows::core::HRESULT(hr))); + } + return Ok(ID3D12Fence::from_raw(out)); + } + dev.CreateFence(0, D3D12_FENCE_FLAG_NONE) +} + +/// Thin COM proxy around `ID3D12Fence1` used under Wine/vkd3d-proton so the underlying +/// fence's native `lpVtbl` stays intact (`vkd3d-proton` compares `fence->lpVtbl` against +/// `&d3d12_fence_vtbl` in `CommandQueue::Signal` / `Wait` and on its queue thread). +#[repr(C)] +struct FenceProxy { + vt: usize, + pc: *mut c_void, +} + +fn fence_proxy_vtable() -> usize { + static V: std::sync::OnceLock = std::sync::OnceLock::new(); + *V.get_or_init(|| { + const SLOTS: usize = 32; + let mut v = vec![0usize; SLOTS]; + for slot in 0..12 { + // mov rcx, [rcx + 8]; mov rax, [rcx]; jmp [rax + slot * 8] + let mut c = vec![0x48u8, 0x8B, 0x49, 0x08, 0x48, 0x8B, 0x01, 0xFF, 0xA0]; + c.extend_from_slice(&((slot * 8) as u32).to_le_bytes()); + let p = crate::stubs::alloc_code(c.len()); + unsafe { std::ptr::copy_nonoverlapping(c.as_ptr(), p, c.len()) }; + v[slot] = p as usize; + } + v[0] = fence_proxy_qi as usize; + v[2] = fence_proxy_release as usize; + v[10] = fence_proxy_signal as usize; + v[12] = super::fence_x::completed_value_address as usize; + for slot in 13..SLOTS { + let (addr, _) = stubs::make_stub_info(DLL, &format!("ID3D12Fence1::#{slot} (not on PC)"), 0); + v[slot] = addr; + } + Box::leak(v.into_boxed_slice()).as_ptr() as usize + }) +} + +unsafe extern "system" fn fence_proxy_qi(this: *mut c_void, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + let pc = (*(this as *const FenceProxy)).pc; + let f: QiFn = std::mem::transmute(*vt(pc)); + let hr = f(pc, riid, ppv); + if hr >= 0 && !ppv.is_null() && *ppv == pc { + *ppv = this; + } else if hr == E_NOINTERFACE && !riid.is_null() { + let s = layout::guid_string(&*riid); + if UNKNOWN_IIDS.lock().unwrap().insert(s.clone()) { + crate::xlog_warn!("D3D12.X: QueryInterface({s}) – interface unknown on the PC (console-only?)"); + } + } + hr +} + +unsafe extern "system" fn fence_proxy_release(this: *mut c_void) -> u32 { + let pc = (*(this as *const FenceProxy)).pc; + let f: RefFn = std::mem::transmute(*vt(pc).add(2)); + let n = f(pc); + if n == 0 { + drop(Box::from_raw(this as *mut FenceProxy)); + } + n +} + +unsafe extern "system" fn fence_proxy_signal(this: *mut c_void, value: u64) -> HRESULT { + let pc = (*(this as *const FenceProxy)).pc; + let f: unsafe extern "system" fn(*mut c_void, u64) -> HRESULT = std::mem::transmute(*vt(pc).add(10)); + let hr = f(pc, value); + super::fence_x::note_cpu_signal(this as usize); + hr +} + +unsafe fn wrap_fence_proxy(pc: *mut c_void) -> *mut c_void { + Box::into_raw(Box::new(FenceProxy { vt: fence_proxy_vtable(), pc })) as *mut c_void +} + +unsafe fn unwrap_fence(p: *mut c_void) -> *mut c_void { + if crate::util::under_wine() { unwrap_fence_proxy(p) } else { p } +} + +unsafe fn unwrap_fence_proxy(p: *mut c_void) -> *mut c_void { + if !p.is_null() && crate::gpu::shader::readable(p as usize, 16) && *(p as *const usize) == fence_proxy_vtable() { + (*(p as *const FenceProxy)).pc + } else { + p + } +} + + +/// Create an un-adopted `ID3D12CommandAllocator` (the device's original slot, as +/// `create_raw_fence`) for XCL's own lists submitted straight to native queues (`sparse` upload +/// slots and readback jobs): vkd3d-proton under Wine does not know an adopted allocator or list. +pub(super) unsafe fn create_raw_command_allocator(dev: &ID3D12Device, ty: D3D12_COMMAND_LIST_TYPE) -> windows::core::Result { + let raw = dev.as_raw(); + if is_ext(vt(raw) as usize) { + type F = unsafe extern "system" fn(*mut c_void, D3D12_COMMAND_LIST_TYPE, *const GUID, *mut *mut c_void) -> HRESULT; + let f: F = std::mem::transmute(orig_fn(raw, slot_of(raw, "CreateCommandAllocator"))); + let mut out: *mut c_void = std::ptr::null_mut(); + let hr = f(raw, ty, (&ID3D12CommandAllocator::IID as *const windows::core::GUID).cast(), &mut out); + if hr < 0 { + return Err(windows::core::Error::from_hresult(windows::core::HRESULT(hr))); + } + return Ok(ID3D12CommandAllocator::from_raw(out)); + } + dev.CreateCommandAllocator(ty) +} + +/// Create an un-adopted `ID3D12GraphicsCommandList` on `alloc` (from +/// `create_raw_command_allocator`) through the device's original slot, with no initial pipeline. +pub(super) unsafe fn create_raw_command_list(dev: &ID3D12Device, ty: D3D12_COMMAND_LIST_TYPE, alloc: &ID3D12CommandAllocator) -> windows::core::Result { + let raw = dev.as_raw(); + if is_ext(vt(raw) as usize) { + type F = unsafe extern "system" fn(*mut c_void, u32, D3D12_COMMAND_LIST_TYPE, *mut c_void, *mut c_void, *const GUID, *mut *mut c_void) -> HRESULT; + let f: F = std::mem::transmute(orig_fn(raw, slot_of(raw, "CreateCommandList"))); + let _alloc_guard = UnadoptGuard::new(alloc.as_raw()); + let mut out: *mut c_void = std::ptr::null_mut(); + let hr = f(raw, 0, ty, alloc.as_raw(), std::ptr::null_mut(), (&ID3D12GraphicsCommandList::IID as *const windows::core::GUID).cast(), &mut out); + if hr < 0 { + return Err(windows::core::Error::from_hresult(windows::core::HRESULT(hr))); + } + return Ok(ID3D12GraphicsCommandList::from_raw(out)); + } + dev.CreateCommandList(0, ty, alloc, None) +} + /// fences created through the device (AddRef'd: they are released at the end only) /// (a read-write lock: `is_fence` reads it on every queue call, and `end_title` reads it with /// the title's threads suspended – a reader that holds it then blocks no other reader) @@ -4873,7 +5193,21 @@ static FENCES: std::sync::RwLock> = std::sync::RwLock::new(Vec::new() /// CreateFence: as on the PC; the fence is remembered for `end_title` unsafe extern "system" fn dev_create_fence(this: *mut c_void, value: usize, flags: usize, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { - let hr = dev_create_fence_pc(this, value, flags, riid, ppv); + let hr = if crate::util::under_wine() { + type F = unsafe extern "system" fn(*mut c_void, usize, usize, *const GUID, *mut *mut c_void) -> HRESULT; + let f: F = std::mem::transmute(orig_fn(this, slot_of(this, "CreateFence"))); + let mut hr = f(this, value, flags, riid, ppv); + if hr == E_NOINTERFACE { + let iid = pc_iid(Kind::Fence); + hr = f(this, value, flags, &iid, ppv); + } + if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { + *ppv = wrap_fence_proxy(*ppv); + } + hr + } else { + dev_create_fence_pc(this, value, flags, riid, ppv) + }; if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { let add: RefFn = std::mem::transmute(*vt(*ppv).add(1)); add(*ppv); @@ -10305,7 +10639,10 @@ unsafe extern "system" fn dev_create_rtv(this: *mut c_void, res: *mut c_void, de copy = ID3D12Resource::from_raw_borrowed(&res).map(|r| r.GetDesc()).and_then(|rd| typeless_srgb_rtv(&rd)); } let desc = copy.as_ref().map_or(desc, |c| c as *const _ as *const c_void); - f(this, res, desc, handle); + { + let _res_guard = UnadoptGuard::new(res); + f(this, res, desc, handle); + } super::present::note_rtv(handle, res); if super::capture::enabled() { super::capture::note_view(handle, res as usize, false, (!desc.is_null()).then(|| (desc as *const [u32; 6]).read_unaligned())); @@ -10406,7 +10743,10 @@ unsafe extern "system" fn cl_set_pipeline_state(this: *mut c_void, pso: *mut c_v type F = unsafe extern "system" fn(*mut c_void, *mut c_void); let f: F = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12GraphicsCommandList", "SetPipelineState"))); let bound = super::pso_async::resolve(pso as usize); - f(this, bound as *mut c_void); + { + let _pso_guard = UnadoptGuard::new(bound as *mut c_void); + f(this, bound as *mut c_void); + } super::dred::mark_pipeline(this, pso as usize, bound); super::list_x::on_set_pipeline_state(this, pso as usize); } @@ -10422,10 +10762,13 @@ unsafe extern "system" fn cl_set_marker(this: *mut c_void, metadata: u32, data: unsafe extern "system" fn cl_clear_state(this: *mut c_void, pso: *mut c_void) { type F = unsafe extern "system" fn(*mut c_void, *mut c_void); let f: F = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12GraphicsCommandList", "ClearState"))); - let bound = super::pso_async::resolve_for_bind(pso as usize); - f(this, bound as *mut c_void); + let resolved = super::pso_async::resolve_for_bind(pso as usize); + { + let _pso_guard = UnadoptGuard::new(resolved as *mut c_void); + f(this, resolved as *mut c_void); + } if !pso.is_null() { - super::dred::mark_pipeline(this, pso as usize, bound); + super::dred::mark_pipeline(this, pso as usize, resolved); } super::list_x::on_clear_state(this, pso as usize); super::bind::clear_state(this, pso as usize); @@ -10553,8 +10896,14 @@ unsafe extern "system" fn alloc_release(this: *mut c_void) -> u32 { pub(super) unsafe fn reset_pc_allocator(raw: *mut c_void) -> HRESULT { type F = unsafe extern "system" fn(*mut c_void) -> HRESULT; let f: F = std::mem::transmute(orig_fn(raw, pc_slot!("ID3D12CommandAllocator", "Reset"))); - let hr = f(raw); - if hr >= 0 { dispatch_chain::allocator_reset(raw as usize); } + let hr = { + let _alloc_guard = UnadoptGuard::new(raw); + f(raw) + }; + if hr >= 0 { + dispatch_chain::allocator_reset(raw as usize); + super::console_list::allocator_reset(raw); + } hr } @@ -10884,6 +11233,18 @@ unsafe fn q_copy_page_mappings(count: u32, batches: usize, pool: usize, pool_pag S_OK } +/// command queue #14 Signal (PC callers, e.g. XCL's own drain/present/sparse helpers) +unsafe extern "system" fn q_signal_pc(this: *mut c_void, fence: *mut c_void, value: u64) -> HRESULT { + let f: unsafe extern "system" fn(*mut c_void, *mut c_void, u64) -> HRESULT = std::mem::transmute(orig_fn(this, slot_of(this, "Signal"))); + f(this, unwrap_fence(fence), value) +} + +/// command queue #15 Wait (PC callers) +unsafe extern "system" fn q_wait_pc(this: *mut c_void, fence: *mut c_void, value: u64) -> HRESULT { + let f: unsafe extern "system" fn(*mut c_void, *mut c_void, u64) -> HRESULT = std::mem::transmute(orig_fn(this, slot_of(this, "Wait"))); + f(this, unwrap_fence(fence), value) +} + /// command queue #23 SignalX(fence, value, flush flags): Signal with the console's cache flush /// options – the PC's Signal makes the queue's writes visible itself. (A stub that signalled /// nothing left Fortnite's render fences pending: its render thread waited, textures were @@ -10899,8 +11260,8 @@ pub(super) unsafe fn queue_signal_now(queue: *mut c_void, fence: usize, value: u super::list_x::break_cycles_before_title_signal(queue as usize, fence, value); // (work that waits for it may use memory committed since it was submitted) super::sparse::send_pending(); - let f: unsafe extern "system" fn(*mut c_void, usize, u64) -> HRESULT = std::mem::transmute(orig_fn(queue, PC_SIGNAL)); - let hr = f(queue, fence, value); + let f: unsafe extern "system" fn(*mut c_void, *mut c_void, u64) -> HRESULT = std::mem::transmute(orig_fn(queue, PC_SIGNAL)); + let hr = f(queue, unwrap_fence(fence as *mut c_void), value); if hr >= 0 { super::fence_x::note_queue_signal(fence, value); super::list_x::note_title_queue_signal(queue as usize, fence, value); @@ -10911,8 +11272,8 @@ pub(super) unsafe fn queue_signal_now(queue: *mut c_void, fence: usize, value: u /// A Wait of `queue` for `fence` (one of the title's) to reach `value`, with what XCL notes. pub(super) unsafe fn queue_wait_now(queue: *mut c_void, fence: usize, value: u64) -> HRESULT { const PC_WAIT: usize = 15; - let f: unsafe extern "system" fn(*mut c_void, usize, u64) -> HRESULT = std::mem::transmute(orig_fn(queue, PC_WAIT)); - let hr = f(queue, fence, value); + let f: unsafe extern "system" fn(*mut c_void, *mut c_void, u64) -> HRESULT = std::mem::transmute(orig_fn(queue, PC_WAIT)); + let hr = f(queue, unwrap_fence(fence as *mut c_void), value); if hr >= 0 { super::fence_x::note_queue_wait(fence, value); super::list_x::note_title_queue_wait(queue as usize, fence, value); @@ -11019,8 +11380,18 @@ unsafe extern "system" fn fence_signal(this: *mut c_void, value: u64) -> HRESULT } unsafe extern "system" fn q_execute_command_lists(this: *mut c_void, n: u32, lists: *const *mut c_void) { - if n == 0 || lists.is_null() { - pc_execute_command_lists(this)(this, n, lists); + // Internal XCL submits (`sparse` uploads/readbacks, `title_textures` uploads, tile zeroing) + // run inside `own_work()` — sometimes while holding internal locks (`sparse::STATE`, + // `vram::PAGES`). Forwarding them straight to PC `ExecuteCommandLists` skips + // `present::note_execute` -> `late_release::drain_known` -> `vram::recycle_ready`, preventing + // re-entrant lock self-deadlocks and keeping internal queues out of `present::QUEUES`. + if n == 0 || lists.is_null() || super::title_textures::is_own_work() { + { + // (Wine/vkd3d-proton) lists carry the extended vtable: native for this call, or + // vkd3d-proton does not know them (read at 0x2a80 in d3d12core, 2026-11-26) + let _list_guards: Vec<_> = if lists.is_null() { Vec::new() } else { (0..n as usize).filter_map(|i| UnadoptGuard::new(*lists.add(i))).collect() }; + pc_execute_command_lists(this)(this, n, lists); + } stop_if_device_removed(); return; } @@ -11110,6 +11481,7 @@ pub(super) unsafe fn execute_mapped(this: *mut c_void, mapped: &[*mut c_void], t let t = std::time::Instant::now(); { let _in_driver = super::watch::in_driver(super::watch::Call::Execute); + let _list_guards: Vec<_> = chunk.iter().filter_map(|&l| UnadoptGuard::new(l)).collect(); f(this, chunk.len() as u32, chunk.as_ptr()); } super::gpu_times::executed(this, chunk); @@ -11192,7 +11564,7 @@ pub fn drain_gpu() { let qr = q as *mut c_void; let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&qr) else { continue }; let submit = || { - let f: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE) + let f: ID3D12Fence = create_raw_fence(dev) .map_err(|e| fence::api_error(dev, "CreateFence", e))?; q.Signal(&f, 1).map_err(|e| fence::api_error(dev, "Signal", e))?; Ok(f) @@ -11631,7 +12003,7 @@ mod this_first_tests { let pc = [0usize; 32]; let ext = [pc.as_ptr() as usize, 0]; let vtbl = ext.as_ptr() as usize + 8; - EXTS.lock().unwrap().get_or_insert_with(HashSet::new).insert(vtbl); + EXTS.write().unwrap().get_or_insert_with(HashSet::new).insert(vtbl); let plain = 0x1234_5678usize; let p = &plain as *const usize as *mut c_void; // an object off the stack (as a real device is), and a stack buffer holding a stale copy @@ -11659,7 +12031,7 @@ mod this_first_tests { assert!(this_first(b, s) && this_first(s, b)); drop(Box::from_raw(obj as *mut [usize; 1])); } - if let Some(g) = EXTS.lock().unwrap().as_mut() { + if let Some(g) = EXTS.write().unwrap().as_mut() { g.remove(&vtbl); } } @@ -12808,13 +13180,13 @@ mod mesh_stream_tests { } impl Drop for Cleanup { fn drop(&mut self) { - CLASSES.lock().unwrap().retain(|c| c.ext != self.ext); + CLASSES.write().unwrap().retain(|c| c.ext != self.ext); if let Some(m) = ABI_RS_SAMPLERS.lock().unwrap().as_mut() { m.remove(&self.key); } } } - CLASSES.lock().unwrap().push(Class { + CLASSES.write().unwrap().push(Class { orig: pc.as_ptr() as usize, kind: Kind::Device, ext: ext_vtbl, diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index e42486ac89..51955c5ac4 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -1376,15 +1376,15 @@ unsafe fn new_state(dev: &ID3D12Device, windows: Vec<(usize, ID3D12Resource)>) - .ok(); let mut slots = Vec::with_capacity(UPLOAD_SLOTS); for _ in 0..UPLOAD_SLOTS { - let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT)?; - let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None)?; + let alloc: ID3D12CommandAllocator = super::real::create_raw_command_allocator(dev, D3D12_COMMAND_LIST_TYPE_DIRECT)?; + let list: ID3D12GraphicsCommandList = super::real::create_raw_command_list(dev, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc)?; list.Close()?; let _ = list.SetName(windows::core::w!("xcl sparse upload list")); let _ = alloc.SetName(windows::core::w!("xcl sparse upload allocator")); slots.push(UploadSlot { alloc, list, fence_val: 0 }); } - let rb_alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT)?; - let rb_list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &rb_alloc, None)?; + let rb_alloc: ID3D12CommandAllocator = super::real::create_raw_command_allocator(dev, D3D12_COMMAND_LIST_TYPE_DIRECT)?; + let rb_list: ID3D12GraphicsCommandList = super::real::create_raw_command_list(dev, D3D12_COMMAND_LIST_TYPE_DIRECT, &rb_alloc)?; rb_list.Close()?; let _ = rb_list.SetName(windows::core::w!("xcl sparse readback list")); let _ = rb_alloc.SetName(windows::core::w!("xcl sparse readback allocator")); @@ -2799,8 +2799,8 @@ unsafe fn take_or_alloc_rb_job(dev: &ID3D12Device, readbacks: &SharedReadbacks, } } let cap = need.max(64 << 10).next_power_of_two().min(READBACK_STAGING); - let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).ok()?; - let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, ty, &alloc, None).ok()?; + let alloc: ID3D12CommandAllocator = super::real::create_raw_command_allocator(dev, ty).ok()?; + let list: ID3D12GraphicsCommandList = super::real::create_raw_command_list(dev, ty, &alloc).ok()?; list.Close().ok()?; let _ = list.SetName(windows::core::w!("xcl sparse readback list")); let mut d = buffer_desc(cap as u64); diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index b0e09c4f1c..ec1d30e8de 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -2093,7 +2093,7 @@ unsafe fn retire_heaps_and_sweep(dev: &ID3D12Device, dev_key: usize, newly_recla if newly_reclaimed.is_empty() && !map.contains_key(&dev_key) { return; } let rq = map.entry(dev_key).or_insert_with(RetireQueue::new); let before = rq.batches.len(); - let immediate = rq.retire_with(newly_reclaimed, unmap_mark, &queues, || dev.CreateFence::(0, D3D12_FENCE_FLAG_NONE).ok(), |q, f, v| q.Signal(f, v).is_ok()); + let immediate = rq.retire_with(newly_reclaimed, unmap_mark, &queues, || super::real::create_raw_fence(dev).ok(), |q, f, v| q.Signal(f, v).is_ok()); // (`fence_completed`: XCL's own fences, without the title's hook) let (done, pruned) = rq.sweep_with(&live_keys, |f| super::real::fence_completed(f)); RETIRING_BATCHES.fetch_add(rq.batches.len().wrapping_sub(before), std::sync::atomic::Ordering::AcqRel); @@ -2119,7 +2119,7 @@ unsafe fn signal_device_queues( let rq = map.entry(dev_key).or_insert_with(RetireQueue::new); rq.signal_queues_with( &queues, - || dev.CreateFence::(0, D3D12_FENCE_FLAG_NONE).ok(), + || super::real::create_raw_fence(dev).ok(), |q, f, v| q.Signal(f, v).is_ok(), ) }