diff --git a/README.md b/README.md index 6c97b6ad33..7ea1f1bc42 100644 --- a/README.md +++ b/README.md @@ -268,7 +268,8 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder `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 target gets its own piece, up to four times the target. Larger components and indirect - dispatches use the whole pipeline. + dispatches use the whole pipeline. If a larger scratch buffer cannot be allocated, the + dispatch runs in smaller slabs using the recording's existing buffer when it fits at least one thread group. - Huge pipelines (translated DXIL of `XCL_HUGE_PSO_BYTES` or more, default 262144) built on the background `xcl-pso*` workers take turns in the driver's compiler: `XCL_HUGE_PSO_CONCURRENCY` at once (default 1, maximum 64). Pipelines of 4 MiB or more use a separate one-wide lane so diff --git a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs index 6fa76b4cbf..d4e373a66c 100644 --- a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs +++ b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs @@ -171,15 +171,24 @@ fn scratch_bytes(needed: u64, group: u64, budget: u64, had: Option, live: u /// chain before it on the list used it. unsafe fn scratch_for(dev: &ID3D12Device, list: usize, allocator: usize, needed: u64, group: u64) -> Option<(Arc, bool)> { let had = SHARED.lock().unwrap().get(&list).filter(|s| s.0 == allocator).and_then(|s| s.1.upgrade()); - let (bytes, make) = scratch_bytes(needed, group, scratch_budget(), had.as_ref().map(|s| s.bytes), LIVE.load(Ordering::Relaxed), scratch_total()); - if !make { - return had.map(|s| (s, true)); + let plan = scratch_bytes(needed, group, scratch_budget(), had.as_ref().map(|s| s.bytes), LIVE.load(Ordering::Relaxed), scratch_total()); + select_scratch(plan, had.map(|s| (s.bytes, s)), group, |bytes| { + let s = Arc::new(Scratch { buffer: scratch_buffer(dev, bytes)?, bytes }); + let live = LIVE.fetch_add(bytes, Ordering::Relaxed) + bytes; + PEAK.fetch_max(live, Ordering::Relaxed); + SHARED.lock().unwrap().insert(list, (allocator, Arc::downgrade(&s))); + Some(s) + }) +} + +fn select_scratch( + (bytes, make): (u64, bool), had: Option<(u64, T)>, group: u64, allocate: impl FnOnce(u64) -> Option, +) -> Option<(T, bool)> { + if make { + if let Some(s) = allocate(bytes) { return Some((s, false)); } } - let s = Arc::new(Scratch { buffer: scratch_buffer(dev, bytes)?, bytes }); - let live = LIVE.fetch_add(bytes, Ordering::Relaxed) + bytes; - PEAK.fetch_max(live, Ordering::Relaxed); - SHARED.lock().unwrap().insert(list, (allocator, Arc::downgrade(&s))); - Some((s, false)) + // A smaller buffer can still run the chain in slabs. Reuse needs a UAV barrier. + had.filter(|(capacity, _)| *capacity >= group).map(|(_, s)| (s, true)) } pub(in crate::hle::d3d12x) fn allocator_reset(allocator: usize) { @@ -235,7 +244,7 @@ pub(in crate::hle::d3d12x) unsafe fn dispatch(list: &ID3D12GraphicsCommandList, #[cfg(test)] mod tests { - use super::{scratch_bytes, LEAST}; + use super::{scratch_bytes, select_scratch, LEAST}; const MB: u64 = 1 << 20; @@ -266,6 +275,28 @@ mod tests { } } + #[test] + fn failed_scratch_growth_reuses_a_buffer_that_fits_a_group() { + let group = 64 << 10; + let select = |had: Option<(u64, &'static str)>| { + let plan = scratch_bytes(8 * MB, group, 256 * MB, had.map(|(bytes, _)| bytes), 2 * MB, 384 * MB); + let mut attempted = false; + let result = select_scratch(plan, had, group, |bytes| { + assert_eq!(bytes, 8 * MB); + attempted = true; + None + }); + assert!(attempted); + result + }; + assert_eq!(select(Some((2 * MB, "old"))), Some(("old", true))); + assert_eq!(select(Some((group, "old"))), Some(("old", true))); + assert_eq!(select(Some((group - 1, "old"))), None); + assert_eq!(select(None), None); + assert_eq!(select_scratch((8 * MB, true), Some((2 * MB, "old")), group, |_| Some("new")), Some(("new", false))); + assert_eq!(select_scratch((2 * MB, false), Some((2 * MB, "old")), group, |_| panic!("unneeded allocation")), Some(("old", true))); + } + #[test] fn a_chains_scratch_is_bounded_by_what_all_recordings_hold() { let group = 187 << 10;