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
4 changes: 2 additions & 2 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us
if unbuilt_compute && super::list_x::defer(this, super::list_x::Deferred::Indirect(sig, max, args, args_off, count_buf, count_off)) {
return super::pso_async::need_by_submit(raw_pso);
}
let pso = super::pso_async::resolve_wait(raw_pso);
let (pso, bound_unbuilt) = super::pso_async::resolve_wait_bound(raw_pso);
let (gfx, compute) = (super::real::is_translated_graphics(pso), super::real::is_translated_compute(pso));
let (args_off, count_off) = (args_off + super::real::buffer_delta(args), count_off + super::real::buffer_delta(count_buf));
if !gfx && !compute {
Expand All @@ -303,7 +303,7 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us
// call with that pair is invalid (debug layer 953, "the currently set Root Signature
// doesn't match the currently set Pipeline State Object"; a retail title's world then
// hung the GPU, 0x887a0006). Skipped, like a draw or dispatch of such a pipeline.
if super::pso_async::is_pending(raw_pso) {
if bound_unbuilt {
count(DRAWS_SKIPPED);
static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
if N.fetch_add(1, Ordering::Relaxed) < 4 {
Expand Down
119 changes: 97 additions & 22 deletions crates/xcl/src/hle/d3d12x/pso_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,53 @@ mod used_tests {
assert_eq!(released(standins[1]), 0xC101, "the last stand-in releases the one owned reference");
}

#[test]
fn bound_state_follows_the_choice_from_a_shared_build() {
let standins = [0x7E57_C111usize, 0x7E57_C112];
let drivers = [0x7E57_D111usize, 0x7E57_D112];
let (started_tx, started_rx) = mpsc::channel();
let (finish_tx, finish_rx) = mpsc::channel::<()>();
submit_compute(standins[0], 0xE6, Box::new(move || {
started_tx.send(()).unwrap();
finish_rx.recv().unwrap();
0xC111
}));
submit_compute(standins[1], 0xE6, Box::new(|| panic!("duplicate shared build ran")));
for (&standin, &driver) in standins.iter().zip(&drivers) {
shared(standin, driver);
image_only(standin);
}

let before = standins.map(|standin| resolve_wait_bound_for(standin, None));
assert_eq!(before, [(drivers[0], true), (drivers[1], true)]);
started_rx.recv_timeout(std::time::Duration::from_secs(10)).expect("a worker did not start the build");
finish_tx.send(()).unwrap();
let start = std::time::Instant::now();
while is_pending(standins[0]) && start.elapsed() < std::time::Duration::from_secs(10) {
std::thread::sleep(std::time::Duration::from_millis(5));
}
assert!(!is_pending(standins[0]), "shared build did not finish");
for ((&standin, &driver), &(bound, _)) in standins.iter().zip(&drivers).zip(&before) {
let pending = entry(standin).unwrap();
assert!(pending.bound_unbuilt(standin, bound), "the earlier driver stand-in stays unbuilt");
assert_eq!(resolve_wait_bound_for(standin, None), (0xC111, false));
assert_ne!(driver, 0xC111);
}
assert_eq!(released(standins[0]), 0);
assert_eq!(released(standins[1]), 0xC111);
}

#[test]
fn a_failed_builds_permanent_stand_in_is_not_unbuilt() {
let standin = 0x7E57_C121usize;
let driver = 0x7E57_D121usize;
submit_lazy(standin, true, Box::new(|| 0), 0);
shared(standin, driver);
assert_eq!(resolve_wait_bound_for(standin, None), (driver, false));
assert_eq!(resolve_wait_bound_for(0x7E57_C122, None), (0x7E57_C122, false));
assert_eq!(released(standin), 0);
}

#[test]
fn no_budget_wait_runs_a_shared_build_on_a_wanted_worker() {
let standins = [0x7E57_C701usize, 0x7E57_C702];
Expand Down Expand Up @@ -1654,6 +1701,13 @@ impl Pending {
d => d,
}
}

/// `bound` was this pending pipeline's stand-in before its build finished successfully.
/// A failed build keeps its stand-in permanently, so that is a finished pipeline choice.
fn bound_unbuilt(&self, pso: usize, bound: usize) -> bool {
let done = self.build.done.lock().unwrap();
bound == self.stand(pso) && !(*done && self.build.real.load(Ordering::Acquire) == 0)
}
}

/// Stand-in `standin` is no driver object (`standin::shell`): binds get `driver` in its place.
Expand Down Expand Up @@ -1686,32 +1740,53 @@ pub fn resolve_wait(pso: usize) -> usize {
resolve_wait_for(pso, wait_budget())
}

/// The pipeline to bind and whether that exact choice was a stand-in for an unfinished build.
/// The answer stays true if a successful build ends after this bind chose its stand-in.
pub fn resolve_wait_bound(pso: usize) -> (usize, bool) {
resolve_wait_bound_for(pso, wait_budget())
}

/// `resolve_wait`, waiting at most `budget` (None: until the pipeline is built).
fn resolve_wait_for(pso: usize, budget: Option<std::time::Duration>) -> usize {
let Some(p) = entry(pso) else { return pso };
p.note_use();
if p.spared.load(Ordering::Acquire) {
return resolve_spared(&p, pso);
}
// (the title's thread is held from here until the pipeline is built or the budget is over)
let waiting = !*p.build.done.lock().unwrap();
let _held = waiting.then(Held::new);
let _stall = waiting.then(|| crate::stall::span(crate::stall::StallKind::PipelineWait)).flatten();
if let Some(budget) = budget {
return resolve_within(&p, pso, budget);
}
// Mark it wanted before an unsent build is queued: the worker and its DXC/driver admission
// see demand from their first instruction. An already queued build is promoted by `want`.
p.build.want();
schedule(&p.build, Priority::Foreground, false, None);
let mut done = p.build.done.lock().unwrap();
while !*done {
done = p.build.cv.wait(done).unwrap();
}
match p.build.real.load(Ordering::Acquire) {
0 => p.stand(pso),
r => r,
}
resolve_wait_pending(&p, pso, budget)
}

fn resolve_wait_bound_for(pso: usize, budget: Option<std::time::Duration>) -> (usize, bool) {
let Some(p) = entry(pso) else { return (pso, false) };
p.note_use();
let bound = resolve_wait_pending(&p, pso, budget);
(bound, p.bound_unbuilt(pso, bound))
}

fn resolve_wait_pending(p: &Arc<Pending>, pso: usize, budget: Option<std::time::Duration>) -> usize {
let bound = if p.spared.load(Ordering::Acquire) {
resolve_spared(p, pso)
} else {
// (the title's thread is held from here until the pipeline is built or the budget is over)
let waiting = !*p.build.done.lock().unwrap();
let _held = waiting.then(Held::new);
let _stall = waiting.then(|| crate::stall::span(crate::stall::StallKind::PipelineWait)).flatten();
if let Some(budget) = budget {
resolve_within(p, pso, budget)
} else {
// Mark it wanted before an unsent build is queued: the worker and its DXC/driver
// admission see demand from their first instruction. An already queued build is
// promoted by `want`.
p.build.want();
schedule(&p.build, Priority::Foreground, false, None);
let mut done = p.build.done.lock().unwrap();
while !*done {
done = p.build.cv.wait(done).unwrap();
}
match p.build.real.load(Ordering::Acquire) {
0 => p.stand(pso),
r => r,
}
}
};
bound
}

/// How long a bind waits for a pipeline still being built (`XCL_PSO_WAIT_MS`; unset or 0: until
Expand Down
Loading