|
@@ -28,6 +28,8 @@ pub(crate) struct Pending {
|
|
|
shaders: Vec<Arc<Shader>>,
|
|
shaders: Vec<Arc<Shader>>,
|
|
|
pub(crate) graphics: Option<Box<GraphicsState>>,
|
|
pub(crate) graphics: Option<Box<GraphicsState>>,
|
|
|
queue_state: AtomicU8,
|
|
queue_state: AtomicU8,
|
|
|
|
|
+ /// Taken by a worker from [`Queue::slow`].
|
|
|
|
|
+ was_slow: AtomicBool,
|
|
|
made: OnceLock<Option<HostPipeline>>,
|
|
made: OnceLock<Option<HostPipeline>>,
|
|
|
/// What identifies it across runs (shader containers and state), and
|
|
/// What identifies it across runs (shader containers and state), and
|
|
|
/// whether a draw or dispatch has requested it yet.
|
|
/// whether a draw or dispatch has requested it yet.
|
|
@@ -53,6 +55,7 @@ impl Pending {
|
|
|
shaders,
|
|
shaders,
|
|
|
graphics,
|
|
graphics,
|
|
|
queue_state: AtomicU8::new(NOT_QUEUED),
|
|
queue_state: AtomicU8::new(NOT_QUEUED),
|
|
|
|
|
+ was_slow: AtomicBool::new(false),
|
|
|
made: OnceLock::new(),
|
|
made: OnceLock::new(),
|
|
|
key: key.finalize().to_hex()[..32].to_owned(),
|
|
key: key.finalize().to_hex()[..32].to_owned(),
|
|
|
used: AtomicBool::new(false),
|
|
used: AtomicBool::new(false),
|
|
@@ -64,8 +67,12 @@ impl Pending {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn make(&self) {
|
|
fn make(&self) {
|
|
|
- self.made
|
|
|
|
|
- .get_or_init(|| HostPipeline::new(&self.shaders, self.graphics.as_deref()));
|
|
|
|
|
|
|
+ self.made.get_or_init(|| {
|
|
|
|
|
+ let began = Instant::now();
|
|
|
|
|
+ let made = HostPipeline::new(&self.shaders, self.graphics.as_deref());
|
|
|
|
|
+ super::library::note_time(&self.key, began.elapsed().as_millis() as u64);
|
|
|
|
|
+ made
|
|
|
|
|
+ });
|
|
|
let (lock, done) = &DONE;
|
|
let (lock, done) = &DONE;
|
|
|
let _held = lock.lock().unwrap_or_else(|e| e.into_inner());
|
|
let _held = lock.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
done.notify_all();
|
|
done.notify_all();
|
|
@@ -228,8 +235,31 @@ const EARLY: u8 = 1;
|
|
|
const REQUESTED: u8 = 2;
|
|
const REQUESTED: u8 = 2;
|
|
|
const MAKING: u8 = 3;
|
|
const MAKING: u8 = 3;
|
|
|
|
|
|
|
|
|
|
+/// 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
|
|
|
|
|
+/// change of the translator or the driver) the early list begins with
|
|
|
|
|
+/// programs that take tens of seconds each: all 16 workers were in them
|
|
|
|
|
+/// through the whole of the title's loading, 97 pipelines made in 20 s, and
|
|
|
|
|
+/// the first minute and a half of the world ran at 0.5 to 1.6 s a frame,
|
|
|
|
|
+/// dropping up to 2,900 draws a frame (run shaders-1).
|
|
|
|
|
+/// `VICTORY_PIPELINE_SLOW=0`: in the order the title creates them, as before.
|
|
|
|
|
+fn slow_limit() -> usize {
|
|
|
|
|
+ static LIMIT: OnceLock<usize> = OnceLock::new();
|
|
|
|
|
+ *LIMIT.get_or_init(|| {
|
|
|
|
|
+ if std::env::var_os("VICTORY_PIPELINE_SLOW").is_some_and(|v| v == "0") {
|
|
|
|
|
+ usize::MAX
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (worker_count(None, std::thread::available_parallelism().map_or(2, |n| n.get())) / 4).max(1)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
struct Queue {
|
|
struct Queue {
|
|
|
early: VecDeque<Arc<Pending>>,
|
|
early: VecDeque<Arc<Pending>>,
|
|
|
|
|
+ /// Those of the early ones known to take long, and how many of them are
|
|
|
|
|
+ /// being made ([`slow_limit`]).
|
|
|
|
|
+ slow: VecDeque<Arc<Pending>>,
|
|
|
|
|
+ slow_now: usize,
|
|
|
requested: VecDeque<Arc<Pending>>,
|
|
requested: VecDeque<Arc<Pending>>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -237,6 +267,8 @@ impl Queue {
|
|
|
const fn new() -> Self {
|
|
const fn new() -> Self {
|
|
|
Self {
|
|
Self {
|
|
|
early: VecDeque::new(),
|
|
early: VecDeque::new(),
|
|
|
|
|
+ slow: VecDeque::new(),
|
|
|
|
|
+ slow_now: 0,
|
|
|
requested: VecDeque::new(),
|
|
requested: VecDeque::new(),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -244,6 +276,10 @@ impl Queue {
|
|
|
/// Queues an unrequested pipeline or promotes one queued at creation.
|
|
/// Queues an unrequested pipeline or promotes one queued at creation.
|
|
|
/// Returns whether it is new to the queue, or `None` if nothing changed.
|
|
/// Returns whether it is new to the queue, or `None` if nothing changed.
|
|
|
fn push(&mut self, p: &Arc<Pending>, priority: u8) -> Option<bool> {
|
|
fn push(&mut self, p: &Arc<Pending>, priority: u8) -> Option<bool> {
|
|
|
|
|
+ self.push_as(p, priority, slow_limit() != usize::MAX && super::library::slow(&p.key))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn push_as(&mut self, p: &Arc<Pending>, priority: u8, slow: bool) -> Option<bool> {
|
|
|
let prev = p.queue_state.load(Ordering::Relaxed);
|
|
let prev = p.queue_state.load(Ordering::Relaxed);
|
|
|
if prev >= priority {
|
|
if prev >= priority {
|
|
|
return None;
|
|
return None;
|
|
@@ -251,18 +287,28 @@ impl Queue {
|
|
|
p.queue_state.store(priority, Ordering::Relaxed);
|
|
p.queue_state.store(priority, Ordering::Relaxed);
|
|
|
if priority == REQUESTED {
|
|
if priority == REQUESTED {
|
|
|
self.requested.push_back(p.clone());
|
|
self.requested.push_back(p.clone());
|
|
|
|
|
+ } else if slow {
|
|
|
|
|
+ self.slow.push_back(p.clone());
|
|
|
} else {
|
|
} else {
|
|
|
self.early.push_back(p.clone());
|
|
self.early.push_back(p.clone());
|
|
|
}
|
|
}
|
|
|
Some(prev == NOT_QUEUED)
|
|
Some(prev == NOT_QUEUED)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fn pop(&mut self) -> Option<Arc<Pending>> {
|
|
|
|
|
- let p = self.requested.pop_front().or_else(|| {
|
|
|
|
|
- // An early entry can have been promoted and claimed already.
|
|
|
|
|
- std::iter::from_fn(|| self.early.pop_front())
|
|
|
|
|
- .find(|p| p.queue_state.load(Ordering::Relaxed) == EARLY)
|
|
|
|
|
- })?;
|
|
|
|
|
|
|
+ fn pop(&mut self, slow_limit: usize) -> Option<Arc<Pending>> {
|
|
|
|
|
+ // An early entry can have been promoted and claimed already.
|
|
|
|
|
+ let unclaimed = |list: &mut VecDeque<Arc<Pending>>| {
|
|
|
|
|
+ std::iter::from_fn(|| list.pop_front()).find(|p| p.queue_state.load(Ordering::Relaxed) == EARLY)
|
|
|
|
|
+ };
|
|
|
|
|
+ let mut p = self.requested.pop_front().or_else(|| unclaimed(&mut self.early));
|
|
|
|
|
+ if p.is_none() && self.slow_now < slow_limit {
|
|
|
|
|
+ p = unclaimed(&mut self.slow);
|
|
|
|
|
+ if let Some(p) = &p {
|
|
|
|
|
+ p.was_slow.store(true, Ordering::Relaxed);
|
|
|
|
|
+ self.slow_now += 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ let p = p?;
|
|
|
p.queue_state.store(MAKING, Ordering::Relaxed);
|
|
p.queue_state.store(MAKING, Ordering::Relaxed);
|
|
|
Some(p)
|
|
Some(p)
|
|
|
}
|
|
}
|
|
@@ -295,7 +341,7 @@ fn start(p: &Arc<Pending>, priority: u8) {
|
|
|
fn next() -> Arc<Pending> {
|
|
fn next() -> Arc<Pending> {
|
|
|
let mut queue = QUEUE.lock().unwrap_or_else(|e| e.into_inner());
|
|
let mut queue = QUEUE.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
loop {
|
|
loop {
|
|
|
- if let Some(p) = queue.pop() {
|
|
|
|
|
|
|
+ if let Some(p) = queue.pop(slow_limit()) {
|
|
|
return p;
|
|
return p;
|
|
|
}
|
|
}
|
|
|
queue = AVAILABLE.wait(queue).unwrap_or_else(|e| e.into_inner());
|
|
queue = AVAILABLE.wait(queue).unwrap_or_else(|e| e.into_inner());
|
|
@@ -339,7 +385,13 @@ fn workers() -> bool {
|
|
|
let _ = SetThreadPriority(GetCurrentThread(), priority);
|
|
let _ = SetThreadPriority(GetCurrentThread(), priority);
|
|
|
}
|
|
}
|
|
|
loop {
|
|
loop {
|
|
|
- next().make();
|
|
|
|
|
|
|
+ let p = next();
|
|
|
|
|
+ p.make();
|
|
|
|
|
+ if p.was_slow.load(Ordering::Relaxed) {
|
|
|
|
|
+ let mut queue = QUEUE.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
+ queue.slow_now -= 1;
|
|
|
|
|
+ AVAILABLE.notify_one();
|
|
|
|
|
+ }
|
|
|
if WAITING.fetch_sub(1, Ordering::Relaxed) == 1 {
|
|
if WAITING.fetch_sub(1, Ordering::Relaxed) == 1 {
|
|
|
drained();
|
|
drained();
|
|
|
} else {
|
|
} else {
|
|
@@ -429,10 +481,33 @@ mod tests {
|
|
|
assert_eq!(queue.push(&promoted, REQUESTED), Some(false));
|
|
assert_eq!(queue.push(&promoted, REQUESTED), Some(false));
|
|
|
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
|
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
|
|
|
|
|
|
|
- assert!(Arc::ptr_eq(&queue.pop().unwrap(), &requested));
|
|
|
|
|
- assert!(Arc::ptr_eq(&queue.pop().unwrap(), &promoted));
|
|
|
|
|
|
|
+ assert!(Arc::ptr_eq(&queue.pop(1).unwrap(), &requested));
|
|
|
|
|
+ assert!(Arc::ptr_eq(&queue.pop(1).unwrap(), &promoted));
|
|
|
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
|
assert_eq!(queue.push(&promoted, REQUESTED), None);
|
|
|
- assert!(Arc::ptr_eq(&queue.pop().unwrap(), &earlier));
|
|
|
|
|
- assert!(queue.pop().is_none());
|
|
|
|
|
|
|
+ assert!(Arc::ptr_eq(&queue.pop(1).unwrap(), &earlier));
|
|
|
|
|
+ assert!(queue.pop(1).is_none());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn pipelines_known_to_take_long_leave_workers_for_the_others() {
|
|
|
|
|
+ let new = || Arc::new(Pending::new(Vec::new(), None));
|
|
|
|
|
+ let (long, longer, asked, quick) = (new(), new(), new(), new());
|
|
|
|
|
+ let mut queue = Queue::new();
|
|
|
|
|
+ for p in [&long, &longer, &asked] {
|
|
|
|
|
+ assert_eq!(queue.push_as(p, EARLY, true), Some(true));
|
|
|
|
|
+ }
|
|
|
|
|
+ assert_eq!(queue.push_as(&quick, EARLY, false), Some(true));
|
|
|
|
|
+ // The quick one first, then one long one at a time; one a draw asks
|
|
|
|
|
+ // for waits for nobody.
|
|
|
|
|
+ assert!(Arc::ptr_eq(&queue.pop(1).unwrap(), &quick));
|
|
|
|
|
+ assert!(Arc::ptr_eq(&queue.pop(1).unwrap(), &long));
|
|
|
|
|
+ assert!(queue.pop(1).is_none());
|
|
|
|
|
+ assert_eq!(queue.push_as(&asked, REQUESTED, true), Some(false));
|
|
|
|
|
+ assert!(Arc::ptr_eq(&queue.pop(1).unwrap(), &asked));
|
|
|
|
|
+ assert!(!asked.was_slow.load(Ordering::Relaxed));
|
|
|
|
|
+ assert!(queue.pop(1).is_none());
|
|
|
|
|
+ queue.slow_now -= 1;
|
|
|
|
|
+ assert!(Arc::ptr_eq(&queue.pop(1).unwrap(), &longer));
|
|
|
|
|
+ assert!(queue.pop(1).is_none());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|