diff --git a/README.md b/README.md index 4d1932d2a..6e461d8ac 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,8 @@ next to `xcl.exe`, directly next to `xcl.exe`, in `%LOCALAPPDATA%\XCL\D3D12\`, o folder has DXC 1.7.2308 or 1.8.2502, depending on the SDK's revision). DXC 1.7's DXIL was 2–2.7 times the size of 1.9's for translated compute programs and took an NVIDIA driver 1.4–2.2 times as long to build pipelines from ([#156](https://github.com/ivall/xcl/issues/156)). `xcl run` prints the DXC it loaded, and - `report.json` has it under `dxc`. + `report.json` has it under `dxc`. `XCL_DXC_DIR` is a Windows host path, including when the SDK + is on a drive such as `D:` that the title uses for game files. - **GPU:** developed on AMD RDNA4. The console's geometry and NGG programs run as a compute pass plus a mesh pass, so a GPU without mesh shaders draws none of them. On GPUs with 32-lane waves only (NVIDIA) console wave64 compute, geometry and mesh programs run as two 32-lane halves that exchange diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index d53751d1f..71fd2380d 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -292,7 +292,7 @@ pub fn location() -> Result<&'static Path, String> { } fn load() -> Result<&'static (PathBuf, usize), String> { - LIB.get_or_init(|| { + LIB.get_or_init(|| crate::vfs_hooks::with_host_io(|| { use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryW}; for dir in candidates() { let dxc = dir.join("dxcompiler.dll"); @@ -323,7 +323,7 @@ fn load() -> Result<&'static (PathBuf, usize), String> { } } Err("dxcompiler.dll not found (set XCL_DXC_DIR or install the Windows SDK)".into()) - }) + })) .as_ref() .map_err(|e| e.clone()) } @@ -516,7 +516,7 @@ pub(super) fn toolchain_identity() -> &'static str { h ^= byte as u64; h = h.wrapping_mul(0x100_0000_01b3); } - let Ok(metadata) = std::fs::metadata(dir.join(name)) else { + let Ok(metadata) = crate::vfs_hooks::with_host_io(|| std::fs::metadata(dir.join(name))) else { h ^= 0xff; h = h.wrapping_mul(0x100_0000_01b3); continue; diff --git a/crates/xcl/src/vfs_hooks.rs b/crates/xcl/src/vfs_hooks.rs index 7cf09a9c3..df87a505f 100644 --- a/crates/xcl/src/vfs_hooks.rs +++ b/crates/xcl/src/vfs_hooks.rs @@ -45,6 +45,12 @@ impl Drop for Guard { } } +/// Keep XCL's own file accesses on the host even when a console mount shadows the path. +pub(crate) fn with_host_io(f: impl FnOnce() -> T) -> T { + let _guard = Guard::enter(); + f() +} + macro_rules! orig { ($slot:ident as $ty:ty) => { std::mem::transmute::($slot.load(Ordering::Acquire)) diff --git a/crates/xcl/tests/e2e.rs b/crates/xcl/tests/e2e.rs index 57301087f..31c720357 100644 --- a/crates/xcl/tests/e2e.rs +++ b/crates/xcl/tests/e2e.rs @@ -84,6 +84,57 @@ fn launcher_uses_configured_exe_in_an_ambiguous_folder() { assert_eq!(exe.file_name().unwrap(), "game.exe"); } +#[test] +fn title_loads_host_dxc_under_a_console_mount() { + use xcl::gpu::recomp::dxc; + + // The file hooks and DXC loader are process-wide. Both children use the same SDK and cache. + const CHILD: &str = "XCL_TEST_HOST_DXC"; + let Ok(mode) = std::env::var(CHILD) else { + let Ok(dir) = dxc::location() else { + xcl::gpu::recomp::runner::skip_test("no DXC"); + return; + }; + let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); + let root = std::env::temp_dir().join(format!("xcl-host-dxc-{}-{nonce}", std::process::id())); + let sdk = root.join("sdk"); + std::fs::create_dir_all(&sdk).unwrap(); + std::fs::copy(dir.join("dxcompiler.dll"), sdk.join("dxcompiler.dll")).unwrap(); + if dir.join("dxil.dll").is_file() { + std::fs::copy(dir.join("dxil.dll"), sdk.join("dxil.dll")).unwrap(); + } + for mode in ["mounted", "native"] { + let output = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "title_loads_host_dxc_under_a_console_mount", "--nocapture"]) + .env(CHILD, mode) + .env("XCL_DXC_DIR", &sdk) + .output().unwrap(); + if !output.status.success() { + std::fs::remove_dir_all(&root).unwrap(); + panic!("{mode} DXC child failed:\n{}", String::from_utf8_lossy(&output.stderr)); + } + } + std::fs::remove_dir_all(root).unwrap(); + return; + }; + + let sdk = PathBuf::from(std::env::var_os("XCL_DXC_DIR").unwrap()); + let root = sdk.parent().unwrap(); + let dll = sdk.join("dxcompiler.dll"); + if mode == "mounted" { + let game = root.join("empty-title"); + let mounts = vec![(sdk.display().to_string(), game.display().to_string())]; + xcl::vfs::init(xcl::vfs::Vfs::new(&game, &root.join("title-data"), mounts)); + xcl::vfs_hooks::install().unwrap(); + assert!(!dll.is_file(), "the title should see the empty mount"); + } + assert_eq!(dxc::location().unwrap(), sdk); + let (dxil, hit) = dxc::compile_cached_in(&root.join("cache"), "float4 main() : SV_Target { return 1; }", "main", "ps_6_0").unwrap(); + assert!(dxil.starts_with(b"DXBC")); + assert_eq!(hit, mode == "native", "both processes should fingerprint the same SDK"); + assert_eq!(dll.is_file(), mode == "native", "the mount should still apply outside the compiler"); +} + #[test] fn test_title_runs_and_game_folder_is_untouched() { let xcl = PathBuf::from(env!("CARGO_BIN_EXE_xcl"));