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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder
- XCL saves an eligible driver blob when its pipeline takes at least 250 ms to build, even
after the bounded in-memory blob cache fills. This is off by default on AMD;
`XCL_PSO_DISK_CACHE=1` forces it on, and `XCL_PSO_DISK_CACHE=0` disables it elsewhere.
Compute blobs with different ordered static-sampler root signatures use separate entries.
- Direct compute dispatches can split large programs into pipelines built in parallel.
`XCL_DIRECT_SPLIT_INSTRUCTIONS` sets the target instructions per piece (default 12500,
zero disables splitting). Loops stay in one piece. A strongly connected component over the
Expand Down
75 changes: 61 additions & 14 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4673,7 +4673,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const
let record = crate::gpu::shader::fnv64(cs);
let warm = compiled.dispatch_chain.as_ref().and_then(|_| {
let rs = abi_rs_for(this, &compiled.samplers)?;
let result = compute_container(this, f, d, rs, &dxil, record, riid, ppv, ComputeBuild::CachedWhole);
let result = compute_container(this, f, d, rs, &dxil, &compiled.samplers, record, riid, ppv, ComputeBuild::CachedWhole);
(result.0 >= 0).then_some((rs, result))
});
if let Some(plan) = compiled.dispatch_chain.filter(|_| warm.is_none()) {
Expand All @@ -4693,7 +4693,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const
Some(hit) => hit,
None => {
let Some(rs) = abi_rs_for(this, &compiled.samplers) else { return Some(Err(())) };
(rs, compute_container(this, f, d, rs, &dxil, record, riid, ppv, ComputeBuild::Whole))
(rs, compute_container(this, f, d, rs, &dxil, &compiled.samplers, record, riid, ppv, ComputeBuild::Whole))
}
};
LAST_COMPUTE_RS.with(|c| c.set(rs as usize));
Expand All @@ -4708,7 +4708,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const
crate::xlog_warn!(
"D3D12.X: the driver refused the compute PSO of record {record:016x} ({} B DXIL, driver cache {}) with {hr:#x} after {} ms in the driver: its dispatches are skipped",
dxil.len(),
driver_cache::name(&dxil, d.Flags.0),
driver_cache::name_for_root(&dxil, d.Flags.0, false, &compiled.samplers),
in_driver.as_millis()
);
}
Expand All @@ -4722,7 +4722,7 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const
PSO_USER_SGPRS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, (i.user_sgprs, i.threads));
}
note_pso_dispatch_remap(*ppv, compiled.dispatch_remap);
remap_fallback::note(*ppv as usize, this, f, rs as usize, d.Flags, d.NodeMask, record, cs,
remap_fallback::note(*ppv as usize, this, f, rs as usize, &compiled.samplers, d.Flags, d.NodeMask, record, cs,
wave32_only(this), crate::gpu::recomp::pso::mem_windows(), compiled.dispatch_remap.is_some());
note_translated_compute(hr, ppv);
crate::xlog_info!(
Expand All @@ -4745,6 +4745,7 @@ pub(super) mod remap_fallback {
device: ID3D12Device,
create: PsoFn,
signature: ID3D12RootSignature,
samplers: Vec<crate::gpu::recomp::pso::StaticSampler>,
flags: D3D12_PIPELINE_STATE_FLAGS,
node: u32,
record: u64,
Expand All @@ -4761,13 +4762,15 @@ pub(super) mod remap_fallback {
}

pub(super) unsafe fn note(pso: usize, this: *mut c_void, create: PsoFn, rs: usize,
flags: D3D12_PIPELINE_STATE_FLAGS, node: u32, record: u64, cs: &[u8], split_wave64: bool, windows: u32, remapped: bool) {
samplers: &[crate::gpu::recomp::pso::StaticSampler], flags: D3D12_PIPELINE_STATE_FLAGS,
node: u32, record: u64, cs: &[u8], split_wave64: bool, windows: u32, remapped: bool) {
if remapped && pso != 0 {
let Some(device) = ID3D12Device::from_raw_borrowed(&this) else { return };
let raw_rs = rs as *mut c_void;
let Some(signature) = ID3D12RootSignature::from_raw_borrowed(&raw_rs) else { return };
let old = FALLBACKS.lock().unwrap().insert(pso, Arc::new(Fallback {
device: device.clone(), create, signature: signature.clone(), flags, node, record, cs: cs.to_vec(), split_wave64, windows,
device: device.clone(), create, signature: signature.clone(), samplers: samplers.to_vec(), flags, node, record,
cs: cs.to_vec(), split_wave64, windows,
whole: dispatch_chain::WholeBuild::new(),
}));
drop(old);
Expand All @@ -4791,7 +4794,8 @@ pub(super) mod remap_fallback {
let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { NodeMask: job_fb.node, Flags: job_fb.flags, ..Default::default() };
let mut raw = std::ptr::null_mut();
let (hr, _, _) = compute_container(job_fb.device.as_raw(), job_fb.create, &desc,
job_fb.signature.as_raw(), &dxil, job_fb.record, &pc_iid(Kind::PipelineState), &mut raw, ComputeBuild::Whole);
job_fb.signature.as_raw(), &dxil, &job_fb.samplers, job_fb.record,
&pc_iid(Kind::PipelineState), &mut raw, ComputeBuild::Whole);
job_status.store(hr, Ordering::Release);
if hr >= 0 { raw as usize } else { 0 }
}));
Expand All @@ -4817,7 +4821,8 @@ enum ComputeBuild {
}

unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPELINE_STATE_DESC, rs: *mut c_void,
dxil: &[u8], record: u64, riid: *const GUID, ppv: *mut *mut c_void, mode: ComputeBuild) -> (HRESULT, std::time::Duration, bool) {
dxil: &[u8], samplers: &[crate::gpu::recomp::pso::StaticSampler], record: u64, riid: *const GUID,
ppv: *mut *mut c_void, mode: ComputeBuild) -> (HRESULT, std::time::Duration, bool) {
let mut copy: D3D12_COMPUTE_PIPELINE_STATE_DESC = std::ptr::read(d);
copy.pRootSignature = std::mem::transmute::<*mut c_void, std::mem::ManuallyDrop<Option<ID3D12RootSignature>>>(rs);
copy.CS = bytecode(dxil);
Expand All @@ -4834,7 +4839,7 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
let key = compute_blob_key(dxil, rs as usize, copy.Flags.0);
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_for(dxil, copy.Flags.0, mode == ComputeBuild::Chain);
let disk = driver_cache::path_for_root(dxil, copy.Flags.0, mode == ComputeBuild::Chain, samplers);
let from_disk = cached.is_none();
let mut loaded = None;
if from_disk {
Expand Down Expand Up @@ -4900,6 +4905,7 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
/// 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 crate::gpu::recomp::pso::StaticSampler;
use std::io::Read;
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::AsRawHandle;
Expand Down Expand Up @@ -4944,15 +4950,31 @@ pub(crate) mod driver_cache {
}

/// The file of the pipeline made from `dxil` with `flags`.
#[cfg(test)]
pub fn name(dxil: &[u8], flags: i32) -> String {
name_for(dxil, flags, false)
}

/// Like [`name`], using `.chain.pso` for pipelines built with the dispatch chain's root
/// signature (`dispatch_chain`).
/// The legacy empty-sampler name, using `.chain.pso` for the dispatch-chain root signature.
#[cfg(test)]
pub fn name_for(dxil: &[u8], flags: i32, chained: bool) -> String {
name_for_root(dxil, flags, chained, &[])
}

/// The cache file for a pipeline whose fixed ABI root-signature layout has these ordered
/// static samplers. Empty samplers retain the existing name and cache compatibility.
pub fn name_for_root(dxil: &[u8], flags: i32, chained: bool, samplers: &[StaticSampler]) -> String {
let ext = if chained { "chain.pso" } else { "pso" };
format!("{:016x}-{:x}-{:x}.{ext}", crate::gpu::shader::fnv64(dxil), dxil.len(), flags)
let base = format!("{:016x}-{:x}-{:x}", crate::gpu::shader::fnv64(dxil), dxil.len(), flags);
if samplers.is_empty() {
return format!("{base}.{ext}");
}
let bytes: Vec<u8> = samplers
.iter()
.flat_map(|(words, compare)| words.iter().copied().chain([u32::from(*compare)]))
.flat_map(u32::to_le_bytes)
.collect();
format!("{base}-{:016x}-{:x}.{ext}", crate::gpu::shader::fnv64(&bytes), samplers.len())
}

/// The file of a translated graphics pipeline keyed by its full state + shader digest.
Expand All @@ -4977,14 +4999,20 @@ pub(crate) mod driver_cache {
enabled_with(env_setting(), REFERENCE_BLOBS.load(Ordering::Relaxed))
}

#[cfg(test)]
pub fn path(dxil: &[u8], flags: i32) -> Option<PathBuf> {
path_for(dxil, flags, false)
}

#[cfg(test)]
pub fn path_for(dxil: &[u8], flags: i32, chained: bool) -> Option<PathBuf> {
enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(name_for(dxil, flags, chained)))
}

pub fn path_for_root(dxil: &[u8], flags: i32, chained: bool, samplers: &[StaticSampler]) -> Option<PathBuf> {
enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(name_for_root(dxil, flags, chained, samplers)))
}

pub fn graphics_path(hash: u64, bytes: usize) -> Option<PathBuf> {
enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(graphics_name(hash, bytes)))
}
Expand Down Expand Up @@ -5321,6 +5349,22 @@ pub(crate) mod driver_cache {
assert!(name_for(&[0u8; 0x1234], 2, true).ends_with("-1234-2.chain.pso"));
}

#[test]
fn static_sampler_root_signatures_have_distinct_compatible_names() {
let wrap = ([0, 0, 0, 0], false);
let clamp_u = ([2, 0, 0, 0], false);
let comparison = (wrap.0, true);
let file = |chained, samplers: &[StaticSampler]| name_for_root(b"same dxil", 0, chained, samplers);

assert_eq!(file(false, &[]), name_for(b"same dxil", 0, false), "empty sampler cache compatibility");
assert_eq!(file(true, &[]), name_for(b"same dxil", 0, true), "empty chain cache compatibility");
assert_ne!(file(false, &[]), file(false, &[wrap]));
assert_ne!(file(false, &[wrap]), file(false, &[clamp_u]));
assert_ne!(file(false, &[wrap]), file(false, &[comparison]));
assert_ne!(file(false, &[wrap, clamp_u]), file(false, &[clamp_u, wrap]));
assert_ne!(file(false, &[wrap]), file(true, &[wrap]));
}

#[test]
fn disk_blobs_default_off_only_for_amd_and_can_be_overridden() {
assert!(reference_blob_vendor(0x1002));
Expand Down Expand Up @@ -7419,10 +7463,13 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
// Reuse the compiled driver blob of the compute pass across all `(MS/GS, PS)` pipelines that
// share `tp.cs` (in memory and on disk), matching `translated_compute_pso_once`.
let ckey = compute_blob_key(&tp.cs, rs as usize, 0);
let library_compute = crate::shaders::pipeline_library::load_compute(&driver_cache::name(&tp.cs, 0), &cdesc);
let library_compute = crate::shaders::pipeline_library::load_compute(
&driver_cache::name_for_root(&tp.cs, 0, false, &tp.samplers),
&cdesc,
);
let compute_from_library = library_compute.is_some();
let mut ccached = if compute_from_library { None } else { compute_blob(ckey) };
let cdisk = driver_cache::path(&tp.cs, 0);
let cdisk = driver_cache::path_for_root(&tp.cs, 0, false, &tp.samplers);
let cfrom_disk = !compute_from_library && ccached.is_none();
let mut cloaded = None;
if cfrom_disk {
Expand Down
5 changes: 3 additions & 2 deletions crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct PipelineBuilder {
device: ID3D12Device,
create: PsoFn,
signature: ID3D12RootSignature,
samplers: Vec<pso::StaticSampler>,
flags: D3D12_PIPELINE_STATE_FLAGS,
node: u32,
record: u64,
Expand All @@ -43,7 +44,7 @@ impl PipelineBuilder {
let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { NodeMask: self.node, Flags: self.flags, ..Default::default() };
let mut raw = std::ptr::null_mut();
let (hr, _, _) = compute_container(self.device.as_raw(), self.create, &desc, self.signature.as_raw(),
dxil, self.record, iid, &mut raw, ComputeBuild::Chain);
dxil, &self.samplers, self.record, iid, &mut raw, ComputeBuild::Chain);
if hr >= 0 && !raw.is_null() { Ok(ID3D12PipelineState::from_raw(raw)) } else { Err(hr) }
}
}
Expand Down Expand Up @@ -112,7 +113,7 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT
let dev = ID3D12Device::from_raw_borrowed(&this)?;
let signature = pso::dispatch_chain_root_signature(dev, samplers).ok()?;
let rs = signature.as_raw() as usize;
let builder = Arc::new(PipelineBuilder { device: dev.clone(), create, signature,
let builder = Arc::new(PipelineBuilder { device: dev.clone(), create, signature, samplers: samplers.to_vec(),
flags: desc.Flags, node: desc.NodeMask, record });
let jobs = (0..plan.pieces.len()).map(|i| {
let plan = plan.clone();
Expand Down
4 changes: 2 additions & 2 deletions crates/xcl/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1695,9 +1695,9 @@ impl Pipelines {
let path = path_override.or_else(|| {
#[cfg(test)]
if let Some(dir) = self.cache_dir.as_deref() {
return Some(dir.join(driver_cache::name_for(dxil, desc.Flags.0, chained)));
return Some(dir.join(driver_cache::name_for_root(dxil, desc.Flags.0, chained, samplers)));
}
driver_cache::path_for(dxil, desc.Flags.0, chained)
driver_cache::path_for_root(dxil, desc.Flags.0, chained, samplers)
});
let mut loaded = path.as_deref().and_then(driver_cache::load);
let mut cached = loaded.as_ref().map(driver_cache::LoadedBlob::shared);
Expand Down
Loading