Skip to content
Merged
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
121 changes: 120 additions & 1 deletion d3d12/crates/leonida-gpu/src/pipeline/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,12 @@ fn wait(p: &Pending, frame: u64) {
if left.is_zero() {
return;
}
// Before the promotion: the queue's lock it may wait for is part of this
// frame's wait, not on top of it.
let started = Instant::now();
if await_first() {
promote(p);
}
let (lock, done) = &DONE;
let mut held = lock.lock().unwrap_or_else(|e| e.into_inner());
while p.made.get().is_none() {
Expand Down Expand Up @@ -567,10 +572,35 @@ fn wait(p: &Pending, frame: u64) {
);
}

/// Whether a pipeline a frame waits for goes before the others asked for
/// ([`AWAITED`]). A draw's pipeline went to the back of the queue: on the
/// first visit to the world about 1,700 were queued at once (562 of them took
/// over a second each), and with the launcher's 10 s budget every frame waited
/// its whole budget for one that a thousand others stood before, then dropped
/// its draws anyway: 0.35 fps for three and a half minutes (RTX 3080, 8
/// workers). On by default; `LEONIDA_PIPELINE_AWAIT_FIRST=0`: in the order
/// asked, as before.
fn await_first() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("LEONIDA_PIPELINE_AWAIT_FIRST").is_none_or(|v| v != "0"))
}

/// Moves `p`, asked for and not taken by a worker yet, to the front of its
/// list as a frame begins to wait for it. (A frame waiting again for one
/// already moved or being made does not take the lock.)
fn promote(p: &Pending) {
if p.queue_state.load(Ordering::Relaxed) == REQUESTED {
QUEUE.lock().unwrap_or_else(|e| e.into_inner()).promote(p);
}
}

const NOT_QUEUED: u8 = 0;
const EARLY: u8 = 1;
const REQUESTED: u8 = 2;
const MAKING: u8 = 3;
/// Requested, and a frame waits for it: at the front of its list, after
/// those awaited before it ([`Queue::promote`]).
const AWAITED: u8 = 3;
const MAKING: u8 = 4;

/// How many workers may make pipelines at once that are known to take long
/// and that no draw has asked for: a quarter of them. On a fresh cache (a
Expand Down Expand Up @@ -638,6 +668,36 @@ impl Queue {
Some(prev == NOT_QUEUED)
}

/// Moves `p` from its place among those asked for to the front, after
/// those awaited before it; whether it moved (not if a worker has it, it
/// is not asked for, or it was moved already). Behind the earlier ones,
/// not before them: with the shared budget their frames' waits have run
/// out, but taking them in turn keeps any from waiting forever. It does
/// not stop a make a worker has begun: the next free worker takes it.
fn promote(&mut self, p: &Pending) -> bool {
if p.queue_state.load(Ordering::Relaxed) != REQUESTED {
return false;
}
let list = if p.screen && screens_first() {
&mut self.screen
} else {
&mut self.requested
};
let Some(at) = list.iter().position(|q| std::ptr::eq(Arc::as_ptr(q), p)) else {
return false;
};
let Some(q) = list.remove(at) else {
return false;
};
q.queue_state.store(AWAITED, Ordering::Relaxed);
let front = list
.iter()
.position(|q| q.queue_state.load(Ordering::Relaxed) != AWAITED)
.unwrap_or(list.len());
list.insert(front, q);
true
}

fn pop(&mut self, screens_only: bool, slow_limit: usize) -> Option<Arc<Pending>> {
// An early entry can have been promoted and claimed already.
let unclaimed = |list: &mut VecDeque<Arc<Pending>>| {
Expand Down Expand Up @@ -930,4 +990,63 @@ mod tests {
assert!(queue.pop(true, 1).is_none());
assert!(Arc::ptr_eq(&queue.pop(false, 1).unwrap(), &world));
}

fn in_order(queue: &mut Queue, screens_only: bool, expected: &[&Arc<Pending>]) {
let made: Vec<_> = std::iter::from_fn(|| queue.pop(screens_only, 1)).collect();
assert_eq!(made.len(), expected.len());
for (i, (got, want)) in made.iter().zip(expected).enumerate() {
assert!(Arc::ptr_eq(got, want), "pipeline {i} out of order");
}
}

/// A pipeline a frame waits for is made next, before those asked for
/// earlier; those awaited keep their order among themselves.
#[test]
fn an_awaited_pipeline_is_made_before_those_asked_earlier() {
let new = || Arc::new(Pending::new(Vec::new(), None));
let (a, b, c, d, e) = (new(), new(), new(), new(), new());
let mut queue = Queue::new();
for p in [&a, &b, &c, &d] {
assert_eq!(queue.push_as(p, REQUESTED, false), Some(true));
}
// c, then a, are waited for: they go first, c before a, once each.
assert!(queue.promote(&c));
assert!(queue.promote(&a));
assert!(!queue.promote(&a));
// One a draw has not asked for, or that a worker has, stays where it is.
assert!(!queue.promote(&e));
assert!(Arc::ptr_eq(&queue.pop(false, 1).unwrap(), &c));
assert!(!queue.promote(&c));
in_order(&mut queue, false, &[&a, &b, &d]);
}

#[test]
fn an_awaited_early_pipeline_is_claimed_once() {
let new = || Arc::new(Pending::new(Vec::new(), None));
let (early, asked) = (new(), new());
let mut queue = Queue::new();
assert_eq!(queue.push_as(&early, EARLY, false), Some(true));
assert_eq!(queue.push_as(&asked, REQUESTED, false), Some(true));
assert_eq!(queue.push_as(&early, REQUESTED, false), Some(false));
assert!(queue.promote(&early));
// Already past asked for: a draw asking again changes nothing.
assert_eq!(queue.push_as(&early, REQUESTED, false), None);
// (The early list's stale entry is passed over.)
in_order(&mut queue, false, &[&early, &asked]);
}

#[test]
fn an_awaited_pass_over_the_screen_goes_first_among_them() {
let pass = || {
let mut p = Pending::new(Vec::new(), None);
p.screen = true;
Arc::new(p)
};
let (first, second) = (pass(), pass());
let mut queue = Queue::new();
assert_eq!(queue.push(&first, REQUESTED), Some(true));
assert_eq!(queue.push(&second, REQUESTED), Some(true));
assert!(queue.promote(&second));
in_order(&mut queue, true, &[&second, &first]);
}
}