mirror of
https://github.com/ivall/xcl.git
synced 2026-11-24 05:14:57 +01:00
Merge pull request #634 from theaustinayers/driver-pso-cache
D3D12.X: the driver's compile of a compute pipeline is kept on disk
This commit is contained in:
@@ -3370,7 +3370,13 @@ unsafe fn translated_compute_pso(this: *mut c_void, f: PsoFn, desc: *const c_voi
|
||||
// A program built again gets the driver's own result of its first build: a retail title
|
||||
// made 14 big compute pipelines twice while loading, 13 - 30 s each the second time.
|
||||
let key = compute_blob_key(&dxil, rs as usize, copy.Flags.0);
|
||||
let cached = compute_blob(key);
|
||||
let mut cached = compute_blob(key);
|
||||
// … and one built in an earlier run gets the blob kept on disk then (`driver_cache`)
|
||||
let disk = driver_cache::path(&dxil, copy.Flags.0);
|
||||
let from_disk = cached.is_none();
|
||||
if from_disk {
|
||||
cached = disk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new);
|
||||
}
|
||||
if let Some(b) = &cached {
|
||||
copy.CachedPSO = D3D12_CACHED_PIPELINE_STATE { pCachedBlob: b.as_ptr() as *const c_void, CachedBlobSizeInBytes: b.len() };
|
||||
}
|
||||
@@ -3385,12 +3391,20 @@ unsafe fn translated_compute_pso(this: *mut c_void, f: PsoFn, desc: *const c_voi
|
||||
if hr < 0 && cached.is_some() {
|
||||
// a blob the driver does not take (another root signature at the same address…): compiled
|
||||
forget_compute_blob(key);
|
||||
if from_disk {
|
||||
driver_cache::note(false, disk.as_deref());
|
||||
}
|
||||
copy.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default();
|
||||
hr = finish_pso(this, f, © as *const _ as *const c_void, riid, ppv);
|
||||
}
|
||||
let reused = hr >= 0 && cached.is_some() && copy.CachedPSO.CachedBlobSizeInBytes != 0;
|
||||
if hr >= 0 && !reused {
|
||||
keep_compute_blob(key, *ppv, dxil.len());
|
||||
if let (Some(p), Some(b)) = (disk.as_deref(), compute_blob(key)) {
|
||||
driver_cache::keep(p, &b, started.elapsed());
|
||||
}
|
||||
} else if reused && from_disk {
|
||||
driver_cache::note(true, None);
|
||||
}
|
||||
if hr < 0 {
|
||||
note_translation_error(format!("the driver rejected the compute PSO with the translated CS ({hr:#x})"));
|
||||
@@ -3411,6 +3425,118 @@ unsafe fn translated_compute_pso(this: *mut c_void, f: PsoFn, desc: *const c_voi
|
||||
Some(Ok(hr))
|
||||
}
|
||||
|
||||
/// The blobs of `COMPUTE_BLOBS`, kept on disk between runs.
|
||||
///
|
||||
/// `COMPUTE_BLOBS` saves the second build of a program in one run; the first build of every
|
||||
/// run still goes through the driver's compiler. On an NVIDIA GPU that is 5 – 27 s for each of
|
||||
/// a retail title's larger compute programs when its world loads: the title dispatches without
|
||||
/// them meanwhile and is ended before all are built, so the next run starts the same compiles
|
||||
/// again. A blob whose build took `KEEP` or longer is written under the hash of its DXIL and
|
||||
/// its flags (`<DXIL cache>\driver\`) and read when a later run has none in memory; the
|
||||
/// driver refuses the blob of another driver, adapter or root signature (the file is dropped
|
||||
/// and the program compiled as before). `XCL_PSO_DISK_CACHE=0`: off.
|
||||
mod driver_cache {
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
/// builds shorter than this are not worth a file
|
||||
const KEEP: std::time::Duration = std::time::Duration::from_millis(250);
|
||||
static HITS: AtomicU64 = AtomicU64::new(0);
|
||||
static STALE: AtomicU64 = AtomicU64::new(0);
|
||||
static STORED: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
/// The file of the pipeline made from `dxil` with `flags`.
|
||||
pub fn name(dxil: &[u8], flags: i32) -> String {
|
||||
format!("{:016x}-{:x}-{:x}.pso", crate::gpu::shader::fnv64(dxil), dxil.len(), flags)
|
||||
}
|
||||
|
||||
pub fn path(dxil: &[u8], flags: i32) -> Option<PathBuf> {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
ON.get_or_init(|| std::env::var("XCL_PSO_DISK_CACHE").map_or(true, |v| v.trim() != "0")).then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(name(dxil, flags)))
|
||||
}
|
||||
|
||||
pub fn load(p: &Path) -> Option<Vec<u8>> {
|
||||
std::fs::read(p).ok().filter(|b| !b.is_empty())
|
||||
}
|
||||
|
||||
/// A pipeline was made from a kept blob (`ok`), or the driver refused the blob: dropped.
|
||||
pub fn note(ok: bool, p: Option<&Path>) {
|
||||
if ok {
|
||||
let n = HITS.fetch_add(1, Ordering::Relaxed) + 1;
|
||||
if n.is_power_of_two() {
|
||||
crate::xlog_info!("D3D12.X: {n} compute pipelines made from the driver's blob of an earlier run (kept on disk)");
|
||||
}
|
||||
} else {
|
||||
if let Some(p) = p {
|
||||
let _ = std::fs::remove_file(p);
|
||||
}
|
||||
if STALE.fetch_add(1, Ordering::Relaxed) == 0 {
|
||||
crate::xlog_info!("D3D12.X: a driver blob kept on disk was refused (another driver or adapter?) – such pipelines are compiled again");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `bytes` to `p`, under another name first: a run that is cut short leaves no half file.
|
||||
pub fn write(p: &Path, bytes: &[u8]) -> bool {
|
||||
if bytes.is_empty() {
|
||||
return false;
|
||||
}
|
||||
if let Some(dir) = p.parent() {
|
||||
let _ = std::fs::create_dir_all(dir);
|
||||
}
|
||||
let tmp = p.with_extension(format!("tmp{}", std::process::id()));
|
||||
let ok = std::fs::write(&tmp, bytes).is_ok() && std::fs::rename(&tmp, p).is_ok();
|
||||
if !ok {
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
}
|
||||
ok
|
||||
}
|
||||
|
||||
/// Keep `blob`, of a build that took `took`, for the next run.
|
||||
pub fn keep(p: &Path, blob: &[u8], took: std::time::Duration) {
|
||||
if took >= KEEP && write(p, blob) {
|
||||
let n = STORED.fetch_add(1, Ordering::Relaxed) + 1;
|
||||
if n.is_power_of_two() {
|
||||
crate::xlog_info!("D3D12.X: {n} driver blobs kept on disk for the next run (this one: {} ms, {} KB)", took.as_millis(), blob.len() >> 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_file_is_named_after_its_dxil_and_flags() {
|
||||
assert_eq!(name(b"abc", 0), name(b"abc", 0));
|
||||
assert_ne!(name(b"abc", 0), name(b"abd", 0));
|
||||
assert_ne!(name(b"abc", 0), name(b"abc", 1));
|
||||
// (the length is in the name: two programs of one hash and different lengths differ)
|
||||
assert!(name(&[0u8; 0x1234], 2).ends_with("-1234-2.pso"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_kept_blob_is_read_back_and_a_refused_one_is_dropped() {
|
||||
let dir = std::env::temp_dir().join(format!("xcl-driver-cache-test-{}", std::process::id()));
|
||||
let p = dir.join("driver").join(name(b"program", 0));
|
||||
assert_eq!(load(&p), None, "nothing kept yet");
|
||||
assert!(!write(&p, &[]), "an empty blob is not kept");
|
||||
keep(&p, &[1, 2, 3], std::time::Duration::from_millis(10));
|
||||
assert_eq!(load(&p), None, "a build of milliseconds is not kept");
|
||||
keep(&p, &[1, 2, 3], std::time::Duration::from_secs(1));
|
||||
assert_eq!(load(&p).as_deref(), Some(&[1u8, 2, 3][..]));
|
||||
// no half file under the temporary name
|
||||
assert_eq!(std::fs::read_dir(p.parent().unwrap()).unwrap().count(), 1);
|
||||
// the driver took the blob: it stays; it refused it: gone, the program is compiled again
|
||||
note(true, Some(&p));
|
||||
assert!(p.exists());
|
||||
note(false, Some(&p));
|
||||
assert_eq!(load(&p), None);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The driver's compiled form of translated compute programs (`GetCachedBlob`),
|
||||
/// by (DXIL hash, root signature, flags), up to `COMPUTE_BLOBS_MAX` bytes in all.
|
||||
static COMPUTE_BLOBS: Mutex<Option<(HashMap<(u64, usize, i32), std::sync::Arc<Vec<u8>>>, usize)>> = Mutex::new(None);
|
||||
|
||||
Reference in New Issue
Block a user