Skip to content
Draft
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ xcl inspect D:\Game\game.exe # the game's imports and what XCL has
xcl run D:\Game [-- game arguments] # run
xcl disasm <shader.bin> [--hex] # RDNA2 disassembler (console containers too)
xcl shaders D:\data\shaders.rpf --top 50 # pack statistics; --recomp / --compile N
xcl shaders D:\data\shaders.rpf --precompile --only cs --pipelines # build compute pipelines and save driver blobs for the next run
xcl shaders D:\data\shaders.rpf --precompile --only cs --pipelines # build compute pipelines and save driver blobs for the next run (except by default on AMD)
xcl shaders D:\data\shaders.rpf --precompile --only cs --pipelines --used-only # only the compute pipelines an earlier run of the title used
xcl shaders D:\data\shaders.rpf --export-hlsl out --record-hash HEX # exact compute HLSL for investigation
xcl shaders D:\data\shaders.rpf --measure-pso HEX --repeat 3 # cold driver PSO timing (cache disabled)
Expand Down
54 changes: 50 additions & 4 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result<String, String
let factory: IDXGIFactory6 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).map_err(|e| format!("DXGI: {e}"))?;
let mut device: Option<ID3D12Device> = None;
let mut name = String::new();
let mut vendor = 0x1414; // WARP is Microsoft's adapter
let sel = crate::gpu::adapter::Selector::from_env_or(&cfg.d3d12_adapter);
if sel != crate::gpu::adapter::Selector::Warp {
let mut list = crate::gpu::adapter::candidates(&factory, &sel);
Expand All @@ -536,6 +537,7 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result<String, String
for (adapter, desc) in list {
if let Ok(dev) = crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0) {
device = Some(dev);
vendor = desc.VendorId;
name = format!("{} ({} MiB VRAM)", crate::gpu::adapter::name(&desc), desc.DedicatedVideoMemory >> 20);
break;
}
Expand All @@ -551,6 +553,7 @@ pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result<String, String
d
}
};
driver_cache::note_vendor(vendor);
if debug {
// the debug layer's messages into XCL's log (they otherwise go to a debugger only)
if let Ok(q) = device.cast::<ID3D12InfoQueue1>() {
Expand Down Expand Up @@ -4562,10 +4565,11 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
/// 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.
/// and the program compiled as before). Off by default on AMD; `XCL_PSO_DISK_CACHE=1` forces
/// it on, and `XCL_PSO_DISK_CACHE=0` turns it off everywhere.
pub(crate) mod driver_cache {
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

/// builds shorter than this are not worth a file
const KEEP: std::time::Duration = std::time::Duration::from_millis(250);
Expand All @@ -4586,6 +4590,22 @@ pub(crate) mod driver_cache {
(HITS.load(Ordering::Relaxed), STALE.load(Ordering::Relaxed), STORED.load(Ordering::Relaxed))
}

static REFERENCE_BLOBS: AtomicBool = AtomicBool::new(false);

/// AMD's cached-PSO blobs name entries in the driver's own cache instead of containing
/// self-sufficient code. They are not safe to carry across runs unless explicitly enabled.
pub fn note_vendor(vendor: u32) {
let references = reference_blob_vendor(vendor);
let was = REFERENCE_BLOBS.fetch_or(references, Ordering::Relaxed);
if references && !was && !enabled() {
crate::xlog_info!("D3D12.X: driver-blob disk cache off on AMD (XCL_PSO_DISK_CACHE=1: force on)");
}
}

fn reference_blob_vendor(vendor: u32) -> bool {
vendor == 0x1002
}

/// The file of the pipeline made from `dxil` with `flags`.
pub fn name(dxil: &[u8], flags: i32) -> String {
name_for(dxil, flags, false)
Expand All @@ -4603,9 +4623,21 @@ pub(crate) mod driver_cache {
format!("gfx-{hash:016x}-{bytes:x}.pso")
}

fn env_setting() -> Option<bool> {
static ON: std::sync::OnceLock<Option<bool>> = std::sync::OnceLock::new();
*ON.get_or_init(|| configured(std::env::var("XCL_PSO_DISK_CACHE").ok().as_deref()))
}

fn configured(value: Option<&str>) -> Option<bool> {
value.map(|v| v.trim() != "0")
}

fn enabled_with(setting: Option<bool>, reference_blobs: bool) -> bool {
setting.unwrap_or(!reference_blobs)
}

pub(crate) fn enabled() -> bool {
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"))
enabled_with(env_setting(), REFERENCE_BLOBS.load(Ordering::Relaxed))
}

pub fn path(dxil: &[u8], flags: i32) -> Option<PathBuf> {
Expand Down Expand Up @@ -4725,6 +4757,20 @@ pub(crate) mod driver_cache {
assert!(name_for(&[0u8; 0x1234], 2, true).ends_with("-1234-2.chain.pso"));
}

#[test]
fn disk_blobs_default_off_only_for_amd_and_can_be_overridden() {
assert!(reference_blob_vendor(0x1002));
assert!(!reference_blob_vendor(0x10de)); // NVIDIA
assert!(!reference_blob_vendor(0x8086)); // Intel
assert!(!reference_blob_vendor(0x1414)); // WARP

assert!(!enabled_with(None, true));
assert!(enabled_with(None, false));
assert!(enabled_with(configured(Some("1")), true));
assert!(!enabled_with(configured(Some(" 0 ")), false));
assert_eq!(configured(None), None);
}

#[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()));
Expand Down
9 changes: 5 additions & 4 deletions crates/xcl/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1567,13 +1567,14 @@ impl Pipelines {
unsafe {
let factory: IDXGIFactory6 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0)).map_err(|e| e.to_string())?;
let sel = crate::gpu::adapter::Selector::from_env_or(adapter);
let dev = if sel == crate::gpu::adapter::Selector::Warp {
let (dev, vendor) = if sel == crate::gpu::adapter::Selector::Warp {
let warp: IDXGIAdapter = factory.EnumWarpAdapter().map_err(|e| e.to_string())?;
crate::gpu::agility::create_device(&warp, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?
(crate::gpu::agility::create_device(&warp, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?, 0)
} else {
let (adapter, _) = crate::gpu::adapter::candidates(&factory, &sel).into_iter().next().ok_or_else(|| crate::gpu::adapter::no_match(&factory, &sel))?;
crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?
let (adapter, desc) = crate::gpu::adapter::candidates(&factory, &sel).into_iter().next().ok_or_else(|| crate::gpu::adapter::no_match(&factory, &sel))?;
(crate::gpu::agility::create_device(&adapter, D3D_FEATURE_LEVEL_12_0).map_err(|e| e.to_string())?, desc.VendorId)
};
crate::hle::d3d12x::real::driver_cache::note_vendor(vendor);
let most = pipeline_threads();
let max_dxil = pipeline_cap_kb(std::env::var("XCL_PIPELINE_MAX_KB").ok().as_deref()).saturating_mul(1024);
Ok(Pipelines {
Expand Down
Loading