diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 4ea6374885..aca7c6b7a6 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3106,13 +3106,27 @@ unsafe fn check_feature_support_pc(f: CheckFeatureSupportFn, this: *mut c_void, hr2 } +thread_local! { + /// A `creator!` method is inside the runtime's own call: what it creates through the device + /// (D3D12Core's DRED breadcrumb buffers, made with the debug layer on) is the runtime's, not + /// the title's + static IN_CREATOR: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + /// Device creation methods ending in (REFIID riid, void** ppv). macro_rules! creator { ($name:ident, $method:literal, $kind:expr, [$($a:ident),*]) => { pub(super) unsafe extern "system" fn $name(this: *mut c_void, $($a: usize,)* riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { type F = unsafe extern "system" fn(*mut c_void, $(as_usize!($a),)* *const GUID, *mut *mut c_void) -> HRESULT; let f: F = std::mem::transmute(orig_fn(this, slot_of(this, $method))); + // the runtime calls the device's public methods from inside another one (a command + // list's DRED breadcrumb buffer, made through CreatePlacedResource): that object + // is still being made – asking it for interfaces to adopt it crashes D3D12Core + if IN_CREATOR.with(|c| c.replace(true)) { + return f(this, $($a,)* riid, ppv); + } let mut hr = f(this, $($a,)* riid, ppv); + IN_CREATOR.with(|c| c.set(false)); if hr == E_NOINTERFACE { let iid = pc_iid($kind); hr = f(this, $($a,)* &iid, ppv); diff --git a/crates/xcl/tests/e2e.rs b/crates/xcl/tests/e2e.rs index 6c079431c9..027638eee7 100644 --- a/crates/xcl/tests/e2e.rs +++ b/crates/xcl/tests/e2e.rs @@ -270,11 +270,22 @@ fn test_title_runs_and_game_folder_is_untouched() { /// (Graphics Tools) and the system XGameRuntime, as the run above does; skipped without them. #[test] fn test_title_draws_with_a_root_signature_under_the_debug_layer() { + title_under_the_debug_layer(false); +} + +/// The same run with `XCL_DRED=1` too (#2298): D3D12Core makes a command list's breadcrumb +/// buffer through the device's public CreatePlacedResource, which XCL took for the title's. +#[test] +fn test_title_runs_under_the_debug_layer_with_dred() { + title_under_the_debug_layer(true); +} + +fn title_under_the_debug_layer(dred: bool) { let sys = PathBuf::from(std::env::var_os("SystemRoot").unwrap_or_else(|| r"C:\Windows".into())).join("System32"); for dll in ["d3d12SDKLayers.dll", "xgameruntime.dll"] { if !sys.join(dll).is_file() { use std::io::Write; - let _ = writeln!(std::io::stderr(), "SKIPPED test_title_draws_with_a_root_signature_under_the_debug_layer: no {dll}"); + let _ = writeln!(std::io::stderr(), "SKIPPED the title under the debug layer (dred: {dred}): no {dll}"); return; } } @@ -287,7 +298,14 @@ fn test_title_draws_with_a_root_signature_under_the_debug_layer() { for f in ["xcl_testtitle.exe", "testgame_mw.dll"] { std::fs::copy(bin_dir.join(f), game.join(f)).unwrap_or_else(|e| panic!("{f}: {e} – build first: cargo build --workspace")); } - let out = Command::new(&xcl).arg("run").arg(&game).arg("--").arg("--hello").env("XCL_DATA_ROOT", &data).env("XCL_D3D12_DEBUG", "1").output().expect("spawn xcl"); + let mut cmd = Command::new(&xcl); + cmd.arg("run").arg(&game).arg("--").arg("--hello").env("XCL_DATA_ROOT", &data).env("XCL_D3D12_DEBUG", "1"); + if dred { + cmd.env("XCL_DRED", "1"); + } else { + cmd.env_remove("XCL_DRED"); + } + let out = cmd.output().expect("spawn xcl"); let stdout = String::from_utf8_lossy(&out.stdout); let stderr = String::from_utf8_lossy(&out.stderr); assert!(out.status.success(), "exit code {:?}\n--- stdout ---\n{stdout}\n--- stderr ---\n{stderr}", out.status.code());