Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper.
blob is left to the runtime instead of being compiled in the pass — those compiles take tens
of minutes and gigabytes and stopped the pass on the biggest programs. `XCL_PIPELINE_MAX_KB`
sets the cap in KiB (0 turns it off).
- **Nonzero compute PSO flags (`compute-pso-flags.txt`)**: offline shader-only inputs (`shaders.rpf`
/ `shaders/*.dxbc`) do not include runtime `D3D12_COMPUTE_PIPELINE_STATE_DESC.Flags`, so a cold
`--precompile --pipelines` pass before any run builds only `Flags = 0`. When a title creates a
translated compute pipeline with driver-accepted nonzero flags, XCL records the `(record, flags)`
recipe in `compute-pso-flags.txt` beside the DXIL cache so later `--precompile --pipelines` runs
and startup preparation prebuild those flag variants (both whole and split pieces) under their
matching cache keys.
- **Without a command line** (xcl.exe opened from Explorer) XCL shows a small menu: *Choose a
game…* (pick its exe or `MicrosoftGame.config`) or *Run again* (the last one). On an error the
window stays open until Enter.
Expand Down
144 changes: 144 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6199,6 +6199,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const
PSO_USER_SGPRS.insert(*ppv as usize, (i.user_sgprs, i.threads));
}
note_translated_compute(0, ppv);
driver_cache::record_compute_flags(record, d.Flags.0);
PSO_SHADERS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, (record, 0));
if super::gpu_profile::on() || super::gpu_times::enabled() {
super::gpu_profile::note_compute(record, cs, dxil.len(), wave32_only(this));
Expand Down Expand Up @@ -6233,6 +6234,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const
note_translation_error(format!("the driver rejected the compute PSO with the translated CS ({hr:#x})"));
return Some(Err(()));
}
driver_cache::record_compute_flags(record, d.Flags.0);
TRANSLATED_PSO.fetch_add(1, Ordering::Relaxed);
note_pso_rs(*ppv, rs);
super::dred::label(*ppv as usize, &[("cs", Some(cs))]);
Expand Down Expand Up @@ -6621,6 +6623,89 @@ pub(crate) mod driver_cache {
enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(graphics_name(hash, bytes)))
}

/// Sidecar recipe file beside the DXIL cache recording nonzero `D3D12_PIPELINE_STATE_FLAGS`
/// that the driver accepted for each console compute shader record (`<record:016x> <flags:x>`).
/// Offline shader-only inputs carry no runtime PSO flags, so `xcl shaders --precompile --pipelines`
/// and startup preparation read this file to prebuild observed nonzero-flag variants too.
pub fn compute_flags_path() -> Option<PathBuf> {
enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("compute-pso-flags.txt"))
}

/// Parse `<record_hex> <flags_hex>` lines into sorted, deduplicated nonzero flags per record.
pub fn parse_compute_flags(text: &str) -> std::collections::HashMap<u64, Vec<i32>> {
let mut by_record: std::collections::HashMap<u64, std::collections::BTreeSet<u32>> = std::collections::HashMap::new();
for line in text.lines() {
let mut parts = line.split_whitespace();
let (Some(rec), Some(fl), None) = (parts.next(), parts.next(), parts.next()) else { continue };
let (Ok(record), Ok(flags)) = (u64::from_str_radix(rec, 16), u32::from_str_radix(fl, 16)) else { continue };
if flags != 0 {
by_record.entry(record).or_default().insert(flags);
}
}
by_record
.into_iter()
.map(|(record, flags)| (record, flags.into_iter().map(|f| f as i32).collect()))
.collect()
}

/// Load previously observed nonzero compute PSO flags from `compute-pso-flags.txt`.
pub fn load_compute_flags() -> std::collections::HashMap<u64, Vec<i32>> {
let Some(path) = compute_flags_path() else { return std::collections::HashMap::new() };
load_compute_flags_from(&path)
}

fn load_compute_flags_from(path: &Path) -> std::collections::HashMap<u64, Vec<i32>> {
std::fs::read_to_string(path).map(|s| parse_compute_flags(&s)).unwrap_or_default()
}

/// Record a driver-accepted nonzero `D3D12_PIPELINE_STATE_FLAGS` value for console compute
/// `record` (FNV-64 of the console shader container) so later offline precompiles warm it too.
pub fn record_compute_flags(record: u64, flags: i32) {
if flags == 0 {
return;
}
if cfg!(test) && std::env::var_os("XCL_DXIL_CACHE").is_none() {
return;
}
let Some(path) = compute_flags_path() else { return };
record_compute_flags_in(&path, record, flags);
}

fn record_compute_flags_in(path: &Path, record: u64, flags: i32) {
use std::collections::{HashMap, HashSet};
use std::io::Write;
use std::sync::Mutex;
if flags == 0 {
return;
}
static SEEN: Mutex<Option<HashMap<PathBuf, HashSet<(u64, i32)>>>> = Mutex::new(None);
let mut guard = SEEN.lock().unwrap();
let map = guard.get_or_insert_with(HashMap::new);
if let Some(seen) = map.get(path) {
if seen.contains(&(record, flags)) {
return;
}
} else {
let initial: HashSet<_> = load_compute_flags_from(path)
.into_iter()
.flat_map(|(r, fs)| fs.into_iter().map(move |f| (r, f)))
.collect();
let already = initial.contains(&(record, flags));
map.insert(path.to_path_buf(), initial);
if already {
return;
}
}
if let Some(dir) = path.parent() {
let _ = std::fs::create_dir_all(dir);
}
if let Ok(mut file) = std::fs::OpenOptions::new().create(true).append(true).open(path) {
if writeln!(file, "{record:016x} {:x}", flags as u32).is_ok() {
map.entry(path.to_path_buf()).or_default().insert((record, flags));
}
}
}

/// The layout of `graphics_file_hash`'s input: a change names every graphics file anew.
const GRAPHICS_FILE_SCHEMA: u32 = 2;

Expand Down Expand Up @@ -7200,6 +7285,65 @@ pub(crate) mod driver_cache {
assert_eq!(load_bytes(&p).as_deref(), Some(b"ours".as_slice()));
assert_eq!(std::fs::read(&occupied).unwrap(), b"not ours");
}

#[test]
fn compute_flags_recipe_records_dedups_and_parses_nonzero_flags() {
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let dir = std::env::temp_dir().join(format!("xcl-compute-flags-recipe-{}-{nonce}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let _directory = Directory(dir.clone());
let path = dir.join("compute-pso-flags.txt");

// Zero flags are never recorded; nonzero flags are written once and deduplicated.
record_compute_flags_in(&path, 0x0123_4567_89ab_cdef, 0);
assert!(!path.exists(), "flags=0 must not create a recipe file");
record_compute_flags_in(&path, 0x0123_4567_89ab_cdef, 8);
record_compute_flags_in(&path, 0x0123_4567_89ab_cdef, 4);
record_compute_flags_in(&path, 0x0123_4567_89ab_cdef, 8);
record_compute_flags_in(&path, 0xfedc_ba98_7654_3210, 4);

let raw = std::fs::read_to_string(&path).unwrap();
assert_eq!(raw.lines().count(), 3, "duplicate entries must not be appended:\n{raw}");
let parsed = load_compute_flags_from(&path);
assert_eq!(parsed.get(&0x0123_4567_89ab_cdef), Some(&vec![4, 8]));
assert_eq!(parsed.get(&0xfedc_ba98_7654_3210), Some(&vec![4]));

// Malformed lines and zero flags in hand-edited files are ignored safely.
let mixed = parse_compute_flags("\n# comment\n0123456789abcdef 0\nbadline\n0123456789abcdef 8\n0123456789abcdef 4\n0123456789abcdef 4 extra\n");
assert_eq!(mixed.get(&0x0123_4567_89ab_cdef), Some(&vec![4, 8]));
}

#[test]
fn compute_flags_recipe_retries_after_transient_sharing_violation() {
use std::os::windows::fs::OpenOptionsExt;
const FILE_SHARE_READ: u32 = 1;

let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let dir = std::env::temp_dir().join(format!("xcl-compute-flags-retry-{}-{nonce}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let _directory = Directory(dir.clone());
let path = dir.join("compute-pso-flags.txt");
let record = 0x1122_3344_5566_7788;

record_compute_flags_in(&path, record, 4);

// Hold the recipe open with FILE_SHARE_READ only so append fails with a sharing violation.
let reader = std::fs::OpenOptions::new()
.read(true)
.share_mode(FILE_SHARE_READ)
.open(&path)
.expect("open recipe with read-only sharing");
record_compute_flags_in(&path, record, 8);
assert_eq!(load_compute_flags_from(&path).get(&record), Some(&vec![4]));
drop(reader);

// A subsequent observation of flag 8 in the same process must retry and persist once.
record_compute_flags_in(&path, record, 8);
record_compute_flags_in(&path, record, 8);
let raw = std::fs::read_to_string(&path).unwrap();
assert_eq!(raw.lines().count(), 2, "retried flag must be persisted once:\n{raw}");
assert_eq!(load_compute_flags_from(&path).get(&record), Some(&vec![4, 8]));
}
}
}

Expand Down
Loading
Loading