From f169911a9e5d53faebca1b5b56958bfc5c49a5b0 Mon Sep 17 00:00:00 2001 From: Tiago Leal <183942127+tiagonleal@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:14:55 +0000 Subject: [PATCH] D3D12.X: a skipped ExecuteIndirect inside an occlusion query leaves it visible A draw that XCL drops while an occlusion query is open marks the query so it resolves as visible (list_x::note_dropped_draw). A skipped ExecuteIndirect did not, so a title's occlusion query around skipped indirect draws resolved to 0 samples and the title could treat the objects as hidden. execute_indirect now calls note_dropped_draw for the skips that could have drawn: a pipeline still being built or unavailable, a two-pass mesh pipeline, a root-changing signature with a translated graphics pipeline, and a failed ABI bind of a graphics pipeline. A Dispatch signature makes no samples and is left out, as are the console compute skips. graphics_bound marks the two-pass skip too, and a draw with a stand-in whose build failed (pso_async::is_stand_in) like one still being built. Test execute_indirect_skipped_inside_an_occlusion_query_reports_visible (pending, Dispatch signature, two-pass, root-changing): before the fix it fails ("a pipeline still being built"); after it passes 5 of 5 alone. The failed-ABI path and graphics_bound have no test (they need a real list). --- crates/xcl/src/hle/d3d12x/bind.rs | 87 +++++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/list_x.rs | 10 +++ crates/xcl/src/hle/d3d12x/pso_async.rs | 5 ++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index b50b835091..3acfb8636a 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -1605,6 +1605,14 @@ fn indirect_signature(sig: usize) -> IndirectSignatureInfo { .and_then(|signatures| signatures.get(&sig).copied()).unwrap_or_default() } +/// An ExecuteIndirect that could have drawn is skipped: an occlusion query open on the list +/// reads as visible, as for a skipped draw (`graphics_bound`). A Dispatch signature makes no samples. +fn note_skipped_indirect(this: *mut c_void, signature: IndirectSignatureInfo) { + if !signature.dispatches() { + super::list_x::note_dropped_draw(this); + } +} + pub(super) fn note_dispatch_mesh_signature(sig: usize, companion_dispatch: usize, byte_stride: u32, mesh_offset: u32) { DISPATCH_MESH_SIGS .lock() @@ -3554,6 +3562,7 @@ unsafe fn execute_indirect_snapshot( } let Some((pso, bound_unbuilt)) = resolve_indirect_pipeline(this as usize, raw_pso, resolution) else { ei_work(this, dispatches, super::targets::SKIPPED); + note_skipped_indirect(this, signature); count_indirect_skip(dispatches); return; }; @@ -3576,6 +3585,7 @@ unsafe fn execute_indirect_snapshot( DrawPipeline::StandIn => { ei_work(this, dispatches, super::targets::STANDIN); count_indirect_skip(dispatches); + note_skipped_indirect(this, signature); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { crate::xlog_warn!("D3D12.X: ExecuteIndirect with a pipeline that is still being built skipped"); @@ -3592,6 +3602,7 @@ unsafe fn execute_indirect_snapshot( // signature does not have. let Some(msig) = dispatch_mesh_sig_of(sig) else { ei_work(this, dispatches, super::targets::SKIPPED); + note_skipped_indirect(this, signature); count(DRAWS_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -3703,6 +3714,7 @@ unsafe fn execute_indirect_snapshot( // see as two-pass.) if let Some(tp) = super::real::two_pass_of_exact(pso) { ei_work(this, dispatches, super::targets::SKIPPED); + note_skipped_indirect(this, signature); count(DRAWS_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -3746,6 +3758,9 @@ unsafe fn execute_indirect_snapshot( } if signature.root_changes() { ei_work(this, dispatches, super::targets::SKIPPED); + if gfx { + note_skipped_indirect(this, signature); + } static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { crate::xlog_warn!("D3D12.X: ExecuteIndirect that changes root arguments with a translated pipeline skipped"); @@ -3869,6 +3884,9 @@ unsafe fn execute_indirect_snapshot( } Err(e) => { ei_work(this, dispatches, super::targets::SKIPPED); + if gfx { + note_skipped_indirect(this, signature); + } count(if gfx { DRAWS_SKIPPED } else { DISPATCHES_SKIPPED }); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -4161,6 +4179,7 @@ unsafe fn graphics_bound(this: *mut c_void) -> Option<(ID3D12GraphicsCommandList // (Close → E_INVALIDARG). It draws only through DispatchMesh. if let Some(tp) = super::real::two_pass_of_exact(real) { super::targets::note_draw(this, super::targets::SKIPPED); + super::list_x::note_dropped_draw(this); count(DRAWS_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -4190,7 +4209,7 @@ unsafe fn graphics_bound(this: *mut c_void) -> Option<(ID3D12GraphicsCommandList } } // a pending stand-in draws nothing: an occlusion query around it resolves as visible - if super::pso_async::is_pending(pso) { + if super::pso_async::is_stand_in(pso) { super::list_x::note_dropped_draw(this); } // a PC pipeline (also a stand-in or fallback): the title's root signature @@ -5499,6 +5518,72 @@ mod tests { assert!(!pso_async::is_pending(building)); } + /// An ExecuteIndirect skipped inside an occlusion query leaves it visible, as a skipped draw + /// does (`note_dropped_draw`): the skipped call could have made samples, and a query that + /// reads 0 makes the title treat its objects as hidden. A Dispatch signature makes none. + #[test] + fn execute_indirect_skipped_inside_an_occlusion_query_reports_visible() { + use windows::Win32::Graphics::Direct3D12::D3D12_QUERY_TYPE_OCCLUSION; + unsafe extern "system" fn fake_query(_this: *mut c_void, _heap: usize, _ty: D3D12_QUERY_TYPE, _index: u32) {} + let mut orig_vt = [0usize; 64]; + orig_vt[52] = fake_query as usize; + orig_vt[53] = fake_query as usize; + orig_vt[59] = fake_execute_indirect as usize; + let mut ext_vt = vec![0usize; 65]; + ext_vt[0] = orig_vt.as_ptr() as usize; + ext_vt[1..].copy_from_slice(&orig_vt); + let fake = Box::new(FakeAdoptedList { + vt: unsafe { ext_vt.as_ptr().add(1) }, + refs: AtomicUsize::new(1), + dispatch_calls: AtomicUsize::new(0), + set_compute_rs_calls: AtomicUsize::new(0), + set_compute_uav_calls: AtomicUsize::new(0), + set_pso_calls: AtomicUsize::new(0), + last_pso: AtomicUsize::new(0), + execute_indirect_calls: AtomicUsize::new(0), + }); + let list = &*fake as *const FakeAdoptedList as *mut c_void; + let (sig, sig_dispatch, sig_root) = (0x7E57_7000usize, 0x7E57_7001usize, 0x7E57_7002usize); + note_indirect_signature(sig, IndirectSignatureInfo::new(false, false, None)); + note_indirect_signature(sig_dispatch, IndirectSignatureInfo::new(true, false, None)); + note_indirect_signature(sig_root, IndirectSignatureInfo::new(false, true, None)); + // the query of `heap` around one ExecuteIndirect: does it read as visible? + let visible = |heap: usize, pso: usize, sig: usize| unsafe { + reset(list, 0x7E57_7100, pso); + list_x::begin_query_x(list, heap, D3D12_QUERY_TYPE_OCCLUSION, 0); + execute_indirect(list, sig, 1, 0, 0, 0, 0); + list_x::end_query_x(list, heap, D3D12_QUERY_TYPE_OCCLUSION, 0); + list_x::forced_visible_for_test(heap, 0) + }; + // a pipeline still being built (its job waits for `release`, a worker does not finish it) + let (release, gate) = std::sync::mpsc::channel::<()>(); + let building = 0x7E57_7010usize; + pso_async::submit_lazy(building, false, Box::new(move || { let _ = gate.recv(); 0 }), 0); + pso_async::image_only(building); + assert!(pso_async::is_pending(building)); + let pending = visible(0x7E57_7201, building, sig); + let pending_dispatch = visible(0x7E57_7202, building, sig_dispatch); + let _ = release.send(()); + // a finished two-pass mesh pipeline + let two_pass = 0x7E57_7020usize; + real::register_two_pass_for_test(two_pass, "MS"); + let two = visible(0x7E57_7203, two_pass, sig); + // a translated graphics pipeline with a root-changing signature (the ABI bind itself needs a real list) + let translated = 0x7E57_7030usize; + real::register_translated_graphics_for_test(translated); + let root = visible(0x7E57_7204, translated, sig_root); + real::forget_pso_for_test(translated); + real::forget_pso_for_test(two_pass); + pso_async::released(building); + assert!(pending, "a pipeline still being built"); + assert!(!pending_dispatch, "a Dispatch signature makes no samples"); + assert!(two, "a two-pass pipeline"); + assert!(root, "a root-changing signature"); + forget(list); + list_x::forget_list_for_test(list); + assert_eq!(fake.execute_indirect_calls.load(Ordering::Relaxed), 0); + } + /// A dispatch writes an ExecuteIndirect's group count through another buffer over the same /// memory (as translated shaders write through the memory windows), a UAV barrier (what a /// console barrier becomes), then the ExecuteIndirect: it runs the count just written, not diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index b18c6e1c05..476369da4e 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -6879,6 +6879,16 @@ pub(super) unsafe extern "system" fn begin_query_x(this: *mut c_void, heap: usiz pc::(this, PC_BEGIN_QUERY)(this, heap, ty, index); } +#[cfg(test)] +pub(super) fn forced_visible_for_test(heap: usize, index: u32) -> bool { + VISIBLE.lock().unwrap().as_ref().is_some_and(|m| m.contains_key(&(heap, index))) +} + +#[cfg(test)] +pub(super) fn forget_list_for_test(list: *mut c_void) { + LISTS.lock().unwrap().as_mut().map(|m| m.remove(&(list as usize))); +} + /// command list #29 EndQuery(heap, type, index) (PC #53) pub(super) unsafe extern "system" fn end_query_x(this: *mut c_void, heap: usize, ty: D3D12_QUERY_TYPE, index: u32) { if is_occlusion(ty) { diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index df9fabd0da..6456527360 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -12419,6 +12419,11 @@ pub fn is_pending(pso: usize) -> bool { entry(pso).is_some_and(|p| !*p.build.done.lock().unwrap()) } +/// `pso` is a stand-in with no real pipeline: still being built, or its build failed (it draws nothing). +pub fn is_stand_in(pso: usize) -> bool { + entry(pso).is_some_and(|p| p.build.real.load(Ordering::Acquire) == 0) +} + /// `pso` is a stand-in whose real pipeline is being built or was built, and `resolved`, what a /// `resolve_wait` gave for it, is the stand-in (`pso` itself, or the driver pipeline a shared /// stand-in binds: `shared`): the bind has the stand-in, also when the build finished since (a