From 03d321d823ef03d70a2387c680340f0deb8b9d31 Mon Sep 17 00:00:00 2001 From: cansolele Date: Tue, 24 Nov 2026 23:38:33 +0300 Subject: [PATCH] Recompiler: defer cold direct DXIL during compute PSO cache probes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The direct compute path bypasses DXC and its cached-only check. Defer after a translated-cache miss so async creation can return a stand-in. Translate or fall back when the PSO build runs. On an RTX 5060 Ti with a warm driver cache, synthetic p95 CreateComputePipelineState fell from 2.02–2.12 ms to 0.40–0.41 ms. Immediate first bind rose from 9–13 us to 1.81–2.08 ms. Assisted-by: Codex (GPT-6.2 Sol) --- README.md | 2 +- crates/xcl/src/gpu/recomp/pso.rs | 5 + crates/xcl/tests/gpu_compute_e2e.rs | 227 ++++++++++++++++++++++++++-- 3 files changed, 223 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e35903a1c..00338963c 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ XCL works only on unpacked game files you already own. It does not download, dec | Feature | State | What it does | Verified by | |---|---|---|---| | **Shader recompiler** | ✅ | RDNA2 → HLSL → DXIL: VS, PS, CS, mesh (NGG), GS; atomics, LDS, scratch, images, samplers incl. static ones, wave ops, ray-tracing instructions; structured control flow; parallel DXC, disk cache | 26 698 / 26 698 programs of the retail pack; 0 DXC errors on random samples; per-instruction GPU tests | -| **Pipelines** | ✅ | PSOs from console shaders: VS+PS, CS, MS+PS, VS+GS+PS; root signature with the shaders' static samplers; non-blocking creation – a pipeline whose shaders are not compiled yet gets a stand-in at once and is built in the background | GPU tests drawing the game's own VS / PS pairs, also built in the background | +| **Pipelines** | ✅ | PSOs from console shaders: VS+PS, CS, MS+PS, VS+GS+PS; root signature with the shaders' static samplers; non-blocking creation – a pipeline whose shaders are not compiled yet gets a stand-in at once and is built in the background, including compute with `XCL_DIRECT_DXIL=1` | GPU tests drawing the game's own VS / PS pairs, also built in the background | | **D3D12.X compute** | ✅ | console device slots, console descriptor heaps (T# / V# / S#), buffers at their console addresses, compute command lists recorded in the console format and replayed (tables, barriers, clears, copies) | the game's BVH builder and FSR 4 record and run through XCL | | **D3D12.X graphics** | ✅ | device, resources and heaps (also placed in title memory), pipelines; the title's graphics command lists – draws, indirect draws, graphics root arguments, barriers, copies, clears – and command lists recorded by console code, replayed; `PresentX` and frame pacing into an XCL window | Minecraft Preview plays its worlds (millions of draws, none skipped); Fortnite and XDefiant draw their screens | | **Textures** | ✅ | GFX10 tiling (all 14 2D swizzle modes, the 6 3D thick modes, linear), mip chains with mip tail, arrays, cubes and 3D textures → PC textures | 909 / 912 retail textures exact (the 3 volume LUTs decode smooth); GPU upload bit exact | diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index 496543a53..7ace445e7 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -345,6 +345,11 @@ fn compile_console_shader(blob: &[u8], stage: Stage, sampler_base: u32, windows: // `XCL_DIRECT_DXIL=1`: compute shaders through the direct DXIL translator, the HLSL path // for what it cannot translate yet if stage == Stage::Compute && crate::gpu::dxil::kernel::enabled() { + // The PSO probe already checked the translation caches. Direct DXIL would otherwise + // compile here without reaching the DXC cache's cached-only check. + if dxc::defer() { + return Err("DXIL not compiled yet".into()); + } match direct_compute(blob, sampler_base, windows, split_wave64) { Ok(c) => { crate::gpu::dxil::coverage::record_translated("CS"); diff --git a/crates/xcl/tests/gpu_compute_e2e.rs b/crates/xcl/tests/gpu_compute_e2e.rs index 0d37e0b84..77c2d91dc 100644 --- a/crates/xcl/tests/gpu_compute_e2e.rs +++ b/crates/xcl/tests/gpu_compute_e2e.rs @@ -181,12 +181,11 @@ fn console_wave64_compute_through_the_pso_hook_dispatch_and_read_back() { mirror_program_through_the_hook(&dev); } -/// Hand the wave64 mirror program to the CreateComputePipelineState hook, dispatch the -/// pipeline it built and check every thread's output. -fn mirror_program_through_the_hook(dev: &ID3D12Device) { - // user SGPRs 2, TGID_X + TGID_Y, thread ids v0 + v1 - let blob = console_cs(&program(), 2 << 1 | 1 << 7 | 1 << 8 | 1 << 11, [16, 8, 1]); - assert!(xcl::gpu::xbox::is_xbox(&blob), "expected a console container"); +// User SGPRs 2, TGID_X + TGID_Y, thread ids v0 + v1. +const MIRROR_RSRC2: u32 = 2 << 1 | 1 << 7 | 1 << 8 | 1 << 11; +const MIRROR_THREADS: [u32; 3] = [16, 8, 1]; + +fn create_compute_pso(dev: &ID3D12Device, blob: &[u8]) -> ID3D12PipelineState { let game_rs: ID3D12RootSignature = unsafe { let mut ser = None; D3D12SerializeRootSignature(&D3D12_ROOT_SIGNATURE_DESC::default(), D3D_ROOT_SIGNATURE_VERSION_1, &mut ser, None).unwrap(); @@ -198,18 +197,31 @@ fn mirror_program_through_the_hook(dev: &ID3D12Device) { CS: D3D12_SHADER_BYTECODE { pShaderBytecode: blob.as_ptr() as _, BytecodeLength: blob.len() }, ..Default::default() }; - let before = real::stats(); let pso: Result = unsafe { dev.CreateComputePipelineState(&desc) }; unsafe { std::mem::ManuallyDrop::drop(&mut desc.pRootSignature) }; - let pso = pso.expect("CreateComputePipelineState through the backend hook"); + pso.expect("CreateComputePipelineState through the backend hook") +} + +/// Hand the wave64 mirror program to the CreateComputePipelineState hook, dispatch the +/// pipeline it built and check every thread's output. +fn mirror_program_through_the_hook(dev: &ID3D12Device) { + let blob = console_cs(&program(), MIRROR_RSRC2, MIRROR_THREADS); + assert!(xcl::gpu::xbox::is_xbox(&blob), "expected a console container"); + let before = real::stats(); + let pso = create_compute_pso(dev, &blob); // built in the background behind a stand-in when its shader was not compiled yet - let real_raw = xcl::hle::d3d12x::pso_async::resolve_wait(pso.as_raw() as usize) as *mut std::ffi::c_void; + let real_raw = pso_async::resolve_wait(pso.as_raw() as usize); let after = real::stats(); assert_eq!(after.translated_pipelines, before.translated_pipelines + 1, "PSO was not built from the translated shader: {:?}", after.translation_errors); + check_mirror_dispatch(dev, real_raw); +} + +fn check_mirror_dispatch(dev: &ID3D12Device, real_raw: usize) { assert!( - real::is_translated_compute(real_raw as usize), + real::is_translated_compute(real_raw), "resolved compute PSO must be registered in TRANSLATED_COMPUTE" ); + let real_raw = real_raw as *mut std::ffi::c_void; let pso = unsafe { ID3D12PipelineState::from_raw_borrowed(&real_raw) }.unwrap().clone(); let rs_raw = real::pso_root_signature(pso.as_raw() as usize).expect("root signature of the translated PSO") as *mut std::ffi::c_void; let rs = unsafe { ID3D12RootSignature::from_raw_borrowed(&rs_raw) }.unwrap().clone(); @@ -352,3 +364,198 @@ fn async_compute_pso_registers_real_pso_and_not_standin() { drop(pso); assert!(!real::is_translated_compute(real_raw), "forgetting released async compute PSO must clean up TRANSLATED_COMPUTE"); } + +fn mirror_fspx(code: &[u8]) -> Vec { + // ACCC/FSPX is recognized as console compute bytecode, but only HLSL translates it. + let body_len = 0x24 + code.len() as u32; + let mut part = b"FSPX".to_vec(); + for word in [1, 2, 0x4C + body_len, 0, 0x30, 0x1C, 0, 6, 0x4C, body_len, 0] { + le(&mut part, word); + } + part.resize(0x4C + 0x24, 0); + for (i, n) in MIRROR_THREADS.into_iter().enumerate() { + let offset = 0x3C + 2 * i; + part[offset..offset + 2].copy_from_slice(&(n as u16).to_le_bytes()); + } + part[0x48..0x4C].copy_from_slice(&MIRROR_RSRC2.to_le_bytes()); + part.extend_from_slice(code); + + let mut blob = b"DXBC".to_vec(); + blob.resize(20, 0); + for word in [ + 1, + 44 + part.len() as u32, + 1, + 36, + u32::from_le_bytes(*b"ACCC"), + part.len() as u32, + ] { + le(&mut blob, word); + } + blob.extend(part); + blob +} + +#[test] +fn direct_compute_pso_cached_only() { + use std::{ + fs, + path::PathBuf, + process::Command, + time::{SystemTime, UNIX_EPOCH}, + }; + use xcl::gpu::dxil::coverage::fallback_summary; + use xcl::gpu::recomp::{ + dxc, + pso::{console_to_dxil_in, Stage}, + }; + + const CASE: &str = "XCL_TEST_DIRECT_COMPUTE_CHILD"; + let Ok(case) = std::env::var(CASE) else { + // Backend selection and the disk cache namespace are process-wide. + let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + struct TempCache(PathBuf); + impl Drop for TempCache { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos(); + let cache = TempCache(std::env::temp_dir().join(format!("xcl-direct-probe-{}-{nonce}", std::process::id()))); + let exe = std::env::current_exe().unwrap(); + for case in ["probe", "cold", "disk hit", "no cache", "lazy", "sync"] { + let mut child = Command::new(&exe); + child + .args(["--exact", "direct_compute_pso_cached_only", "--nocapture"]) + .env(CASE, case) + .env("XCL_DIRECT_DXIL", "1") + .env("XCL_ASYNC_PSO", if case == "sync" { "0" } else { "1" }) + .env("XCL_LAZY_PSO", if case == "lazy" { "1" } else { "0" }) + .env("XCL_PSO_WAIT_MS", "0") + .env("XCL_HUGE_PSO_BYTES", "262144") + .env("XCL_HEAP_WINDOWS", "0") + .env("XCL_PSO_DISK_CACHE", "0") + .env("XCL_TRANSLATED_CACHE", if case == "no cache" { "0" } else { "1" }) + .env( + "XCL_DXIL_CACHE", + cache.0.join(if case == "disk hit" { "cold" } else { case }), + ); + for name in [ + "XCL_RECOMP_NO_STRUCTURED", + "XCL_RECOMP_WHY", + "XCL_X64_ALL_MASKS", + "XCL_WAVE64_SPIN", + ] { + child.env_remove(name); + } + if case == "no cache" { + child.env("XCL_RECOMP_NO_STRUCTURED", "1"); // also bypasses the RAM translation cache + } + let output = child.output().unwrap(); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + output.status.success(), + "{case} ({}): {}\n{stderr}", + output.status, + String::from_utf8_lossy(&output.stdout) + ); + if let Some(reason) = stderr + .lines() + .find_map(|line| line.strip_prefix("SKIPPED direct_compute_pso_cached_only: ")) + { + runner::skip_test(reason); + return; + } + } + return; + }; + + if dxc::location().is_err() { + runner::skip_test("no DXC"); + return; + } + assert!(xcl::gpu::dxil::kernel::enabled()); + let code = program(); + let blob = console_cs(&code, MIRROR_RSRC2, MIRROR_THREADS); + if case == "probe" { + let (result, missed) = dxc::cached_only(|| console_to_dxil_in(&blob, Stage::Compute, 0, true, 1)); + assert!( + result.is_err() && missed, + "cold direct translation ran in the cache probe: {}", + fallback_summary() + ); + assert!( + !fallback_summary().contains("CS direct"), + "the probe recorded a translation or fallback" + ); + return; + } + if xcl::hle::d3d12x::backend() != xcl::hle::d3d12x::Backend::Real { + runner::skip_test("no GPU"); + return; + } + if runner::wave64_programs_supported() != Ok(true) { + runner::skip_test("no wave64"); + return; + } + + let raw = real::device_raw(); + let dev = unsafe { ID3D12Device::from_raw_borrowed(&raw) }.unwrap(); + let alloc: ID3D12CommandAllocator = unsafe { dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE) }.unwrap(); + let list: ID3D12GraphicsCommandList = + unsafe { dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, &alloc, None) }.unwrap(); + let pso = create_compute_pso(dev, &blob); + let raw = pso.as_raw() as usize; + let inline = case == "sync" || case == "disk hit"; + assert_eq!( + real::is_translated_compute(raw), + inline, + "{case}: unexpected pipeline at CreateComputePipelineState return ({})", + fallback_summary() + ); + if case == "lazy" { + assert!(pso_async::is_pending(raw), "lazy PSO built before first use"); + } + // Use the title's first bind before the test resolves the stand-in itself. + unsafe { list.SetPipelineState(&pso) }; + assert!( + !pso_async::is_pending(raw), + "first bind returned before the build finished" + ); + let ready = pso_async::resolve(raw); + assert_eq!( + ready == raw, + inline, + "{case}: first bind did not resolve the expected PSO" + ); + unsafe { list.Close() }.unwrap(); + drop(list); + check_mirror_dispatch(dev, ready); + let summary = fallback_summary(); + if case == "disk hit" { + assert!(!summary.contains("CS direct"), "disk hit reran translation: {summary}"); + } else { + assert!(summary.contains("CS direct 1 / HLSL fallback 0"), "{summary}"); + } + + if case == "no cache" { + let cache = PathBuf::from(std::env::var_os("XCL_DXIL_CACHE").unwrap()); + assert!(!cache.join("translated").exists()); + } + if case == "cold" { + let warm = create_compute_pso(dev, &blob); + let warm_raw = warm.as_raw() as usize; + assert!( + real::is_translated_compute(warm_raw), + "RAM hit must return the translated PSO" + ); + assert_eq!(fallback_summary(), summary, "RAM hit reran translation"); + check_mirror_dispatch(dev, warm_raw); + + let fallback = create_compute_pso(dev, &mirror_fspx(&code)); + let raw = fallback.as_raw() as usize; + assert!(!real::is_translated_compute(raw), "HLSL fallback ran in the probe"); + check_mirror_dispatch(dev, pso_async::resolve_wait(raw)); + assert!(fallback_summary().contains("CS direct 1 / HLSL fallback 1")); + } +}