Skip to content
Merged
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
15 changes: 13 additions & 2 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2344,8 +2344,19 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us
match bind_abi(this, list, pso, gfx, None, 0) {
Ok(b) => {
let whole = if compute {
match super::real::dispatch_chain::whole(pso) {
Ok(w) => w,
match super::real::dispatch_chain::whole_ready(pso) {
Ok(super::real::dispatch_chain::Whole::Plain) => None,
Ok(super::real::dispatch_chain::Whole::Ready(w)) => Some(w),
Ok(super::real::dispatch_chain::Whole::Late) => {
static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
let n = N.fetch_add(1, Ordering::Relaxed) + 1;
if n <= 4 || n.is_power_of_two() {
crate::xlog_warn!("D3D12.X: ExecuteIndirect of a split compute program whose whole form is still being built skipped ({n} so far)");
}
ei_work(this, sig, super::targets::SKIPPED);
count(DISPATCHES_SKIPPED);
return;
}
Err(e) => {
crate::xlog_warn!("D3D12.X: ExecuteIndirect whole compute PSO: {e}");
ei_work(this, sig, super::targets::SKIPPED);
Expand Down
7 changes: 7 additions & 0 deletions crates/xcl/src/hle/d3d12x/pso_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2626,6 +2626,13 @@ fn gfx_wait_budget() -> Option<std::time::Duration> {
/// (`indirect_guard` zeroes only counts out of range). A retail title's thread spent 57 s in the
/// driver's compiler on one program no earlier run had used, and another of its threads waited
/// for it all that time: no frame for 57 s.
/// A compute pipeline's dispatches go on without it while it is built (`XCL_CS_PSO_WAIT_MS`):
/// what builds one on a title's thread hands it to a thread of its own instead
/// (`real::dispatch_chain::whole_ready`).
pub(super) fn cs_goes_on() -> bool {
cs_wait_budget().is_some()
}

fn cs_wait_budget() -> Option<std::time::Duration> {
static B: OnceLock<Option<std::time::Duration>> = OnceLock::new();
*B.get_or_init(|| budget_of(std::env::var("XCL_CS_PSO_WAIT_MS").ok().as_deref()))
Expand Down
50 changes: 49 additions & 1 deletion crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ struct Chain {
whole_code: Vec<u8>,
whole: OnceLock<ID3D12PipelineState>,
build_whole: Mutex<()>,
/// a thread of its own builds the whole form (`whole_ready`)
whole_sent: std::sync::atomic::AtomicBool,
}

/// A chain for the thread that builds its whole form (D3D12 objects are free-threaded).
struct ChainRef(Arc<Chain>);
unsafe impl Send for ChainRef {}

impl Chain {
unsafe fn whole(&self) -> Result<ID3D12PipelineState, String> {
if let Some(p) = self.whole.get() { return Ok(p.clone()); }
Expand Down Expand Up @@ -110,7 +116,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(), build_whole: Mutex::new(()), whole_sent: Default::default() });
PROGRAMS.fetch_add(1, Ordering::Relaxed);
PIECES.fetch_add(plan.pieces.len() as u64, Ordering::Relaxed);
*result = first.into_raw();
Expand All @@ -128,6 +134,48 @@ pub(in crate::hle::d3d12x) unsafe fn whole(pso: usize) -> Result<Option<ID3D12Pi
chain.map(|c| c.whole()).transpose()
}

/// The whole form of a split program for an indirect dispatch, which runs no chain (its group
/// counts are the GPU's).
pub(in crate::hle::d3d12x) enum Whole {
/// no split program: the pipeline as it is
Plain,
Ready(ID3D12PipelineState),
/// still being built on a thread of its own: this dispatch goes without it
Late,
}

/// `whole` where a compute pipeline's dispatches go on without it while it is built
/// (`pso_async::cs_goes_on`): the whole form, the largest program the driver compiles, is built
/// by a thread of its own, once, and until it is there its dispatches are left out as those of
/// any late compute pipeline. Built in the call, a retail title's render thread stood in the
/// driver's compiler for it (1.1 MB of DXIL) and every other thread that dispatched it stood
/// behind `build_whole`: no frame for over two minutes after its first world frame.
pub(in crate::hle::d3d12x) unsafe fn whole_ready(pso: usize) -> Result<Whole, String> {
let chain = CHAINS.lock().unwrap().get(&pso).cloned();
let Some(chain) = chain else { return Ok(Whole::Plain) };
if let Some(p) = chain.whole.get() {
return Ok(Whole::Ready(p.clone()));
}
if !super::super::pso_async::cs_goes_on() {
return chain.whole().map(Whole::Ready);
}
if !chain.whole_sent.swap(true, Ordering::AcqRel) {
let job = ChainRef(chain.clone());
// (named as the pipeline workers: a huge compile takes its turn at the gate)
let spawned = std::thread::Builder::new().name("xcl-pso-whole".into()).stack_size(64 << 20).spawn(move || {
let job = job;
if let Err(e) = unsafe { job.0.whole() } {
crate::xlog_warn!("D3D12.X: the whole form of a split compute program was not built ({e}): its indirect dispatches stay left out");
}
});
if spawned.is_err() {
chain.whole_sent.store(false, Ordering::Release);
return chain.whole().map(Whole::Ready);
}
}
Ok(Whole::Late)
}

struct Scratch {
buffer: ID3D12Resource,
bytes: u64,
Expand Down
Loading