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
5 changes: 4 additions & 1 deletion crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us
Ok(Some(whole)) => list.SetPipelineState(&whole),
Ok(None) => {}
Err(e) => {
crate::xlog_warn!("D3D12.X: ExecuteIndirect whole compute PSO: {e}");
static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
if N.fetch_add(1, Ordering::Relaxed) < 4 {
crate::xlog_warn!("D3D12.X: ExecuteIndirect whole compute PSO: {e}");
}
count(DISPATCHES_SKIPPED);
return;
}
Expand Down
86 changes: 74 additions & 12 deletions crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ struct PipelineBuilder {
}

impl PipelineBuilder {
unsafe fn pipeline(&self, dxil: &[u8], iid: &GUID) -> Option<ID3D12PipelineState> {
unsafe fn pipeline(&self, dxil: &[u8], iid: &GUID) -> Result<ID3D12PipelineState, HRESULT> {
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);
(hr >= 0 && !raw.is_null()).then(|| ID3D12PipelineState::from_raw(raw))
if hr >= 0 && !raw.is_null() { Ok(ID3D12PipelineState::from_raw(raw)) } else { Err(hr) }
}
}

Expand All @@ -53,21 +53,37 @@ struct Chain {
tail: Vec<ID3D12PipelineState>,
words: u32,
whole_code: Vec<u8>,
whole: OnceLock<ID3D12PipelineState>,
build_whole: Mutex<()>,
whole: OnceLock<Option<ID3D12PipelineState>>,
/// after a whole build that ran out of memory: when it may be tried, once more
retry_at: Mutex<Option<std::time::Instant>>,
}

impl Chain {
unsafe fn whole(&self) -> Result<ID3D12PipelineState, String> {
if let Some(p) = self.whole.get() { return Ok(p.clone()); }
let _guard = self.build_whole.lock().unwrap();
if let Some(p) = self.whole.get() { return Ok(p.clone()); }
let p = self.builder.pipeline(&self.whole_code, &pc_iid(Kind::PipelineState)).ok_or("whole compute pipeline")?;
let _ = self.whole.set(p.clone());
Ok(p)
if let Some(p) = self.whole.get() { return p.clone().ok_or_else(|| "whole compute pipeline".into()); }
let mut retry_at = self.retry_at.lock().unwrap();
if let Some(p) = self.whole.get() { return p.clone().ok_or_else(|| "whole compute pipeline".into()); }
if retry_at.is_some_and(|t| std::time::Instant::now() < t) { return Err("whole compute pipeline: out of memory".into()); }
match self.builder.pipeline(&self.whole_code, &pc_iid(Kind::PipelineState)) {
Ok(p) => {
let _ = self.whole.set(Some(p.clone()));
Ok(p)
}
Err(hr) if hr == crate::util::E_OUTOFMEMORY && retry_at.is_none() => {
*retry_at = Some(std::time::Instant::now() + WHOLE_RETRY);
Err("whole compute pipeline: out of memory".into())
}
Err(_) => {
let _ = self.whole.set(None);
Err("whole compute pipeline".into())
}
}
}
}

/// How long a split program's whole form waits after a build that ran out of memory, before its last try
const WHOLE_RETRY: std::time::Duration = std::time::Duration::from_secs(10);

static CHAINS: LazyLock<Mutex<HashMap<usize, Arc<Chain>>>> = LazyLock::new(Default::default);

pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUTE_PIPELINE_STATE_DESC,
Expand All @@ -91,7 +107,7 @@ pub(super) unsafe fn build(this: *mut c_void, create: PsoFn, desc: &D3D12_COMPUT
let mut pieces = built.into_iter().collect::<Option<Vec<_>>>()?.into_iter();
let first = pieces.next()?;
let chain = Arc::new(Chain { builder, tail: pieces.collect(), words: plan.dwords_per_group,
whole_code: whole.to_vec(), whole: OnceLock::new(), build_whole: Mutex::new(()) });
whole_code: whole.to_vec(), whole: OnceLock::new(), retry_at: Mutex::new(None) });
PROGRAMS.fetch_add(1, Ordering::Relaxed);
PIECES.fetch_add(plan.pieces.len() as u64, Ordering::Relaxed);
*result = first.into_raw();
Expand Down Expand Up @@ -235,7 +251,7 @@ pub(in crate::hle::d3d12x) unsafe fn dispatch(list: &ID3D12GraphicsCommandList,

#[cfg(test)]
mod tests {
use super::{scratch_bytes, LEAST};
use super::*;

const MB: u64 = 1 << 20;

Expand Down Expand Up @@ -284,4 +300,50 @@ mod tests {
assert_eq!(scratch_bytes(9 * MB, 9 * MB, 4 * MB, None, 400 * MB, 384 * MB), (9 * MB, true));
assert_eq!(scratch_bytes(9 * MB, 9 * MB, 4 * MB, Some(MB), 400 * MB, 384 * MB), (9 * MB, true));
}

#[test]
fn a_whole_form_that_fails_to_build_is_not_built_again() {
static CALLS: AtomicU64 = AtomicU64::new(0);
unsafe extern "system" fn refused(_: *mut c_void, _: *const c_void, _: *const GUID, _: *mut *mut c_void) -> HRESULT {
CALLS.fetch_add(1, Ordering::Relaxed);
crate::util::E_FAIL
}
unsafe {
let Ok(dev) = crate::gpu::agility::create_device(None, D3D_FEATURE_LEVEL_11_0) else {
return crate::gpu::recomp::runner::skip_test("no GPU");
};
let signature = pso::dispatch_chain_root_signature(&dev, &[]).unwrap();
let builder = Arc::new(PipelineBuilder { device: dev, create: refused, signature, flags: D3D12_PIPELINE_STATE_FLAG_NONE, node: 0, record: 0 });
let chain = Chain { builder, tail: Vec::new(), words: 4, whole_code: vec![0; 16], whole: OnceLock::new(), retry_at: Mutex::new(None) };
assert!(chain.whole().is_err());
assert!(chain.whole().is_err());
assert_eq!(CALLS.load(Ordering::Relaxed), 1);
}
}

#[test]
fn a_whole_form_that_ran_out_of_memory_is_tried_once_more_after_a_pause() {
static CALLS: AtomicU64 = AtomicU64::new(0);
unsafe extern "system" fn out_of_memory(_: *mut c_void, _: *const c_void, _: *const GUID, _: *mut *mut c_void) -> HRESULT {
CALLS.fetch_add(1, Ordering::Relaxed);
crate::util::E_OUTOFMEMORY
}
unsafe {
let Ok(dev) = crate::gpu::agility::create_device(None, D3D_FEATURE_LEVEL_11_0) else {
return crate::gpu::recomp::runner::skip_test("no GPU");
};
let signature = pso::dispatch_chain_root_signature(&dev, &[]).unwrap();
let builder = Arc::new(PipelineBuilder { device: dev, create: out_of_memory, signature, flags: D3D12_PIPELINE_STATE_FLAG_NONE, node: 0, record: 0 });
let chain = Chain { builder, tail: Vec::new(), words: 4, whole_code: vec![0; 16], whole: OnceLock::new(), retry_at: Mutex::new(None) };
assert!(chain.whole().is_err());
assert!(chain.whole().is_err());
assert_eq!(CALLS.load(Ordering::Relaxed), 1);
*chain.retry_at.lock().unwrap() = Some(std::time::Instant::now());
assert!(chain.whole().is_err());
assert_eq!(CALLS.load(Ordering::Relaxed), 2);
*chain.retry_at.lock().unwrap() = Some(std::time::Instant::now());
assert!(chain.whole().is_err());
assert_eq!(CALLS.load(Ordering::Relaxed), 2);
}
}
}
Loading