diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
commit | c92ddd37d04ebc7fb5582edd399f607c542e5501 (patch) | |
tree | bea339649563f27a140e5c81f533325cf5580a20 /stockton-render/src/draw/queue_negotiator.rs | |
parent | 36fdf081022aae4b21758fe23422ead88c9ca9d6 (diff) |
fix(draw): better queue negotiator
Diffstat (limited to 'stockton-render/src/draw/queue_negotiator.rs')
-rw-r--r-- | stockton-render/src/draw/queue_negotiator.rs | 118 |
1 files changed, 85 insertions, 33 deletions
diff --git a/stockton-render/src/draw/queue_negotiator.rs b/stockton-render/src/draw/queue_negotiator.rs index 4ad6823..4f00c9e 100644 --- a/stockton-render/src/draw/queue_negotiator.rs +++ b/stockton-render/src/draw/queue_negotiator.rs @@ -1,65 +1,117 @@ use crate::{error::EnvironmentError, types::*}; -use anyhow::Result; +use anyhow::{Result, Error}; use hal::queue::family::QueueFamilyId; use std::sync::{Arc, RwLock}; +use std::collections::HashMap; +use std::any::TypeId; pub struct QueueNegotiator { - family_id: QueueFamilyId, - already_allocated: Vec<Arc<RwLock<QueueT>>>, - next_share: usize, + family_ids: HashMap<TypeId, QueueFamilyId>, + already_allocated: HashMap<TypeId, (Vec<Arc<RwLock<QueueT>>>, usize)>, +} + +pub trait QueueFamilySelector: 'static { + fn is_suitable(&self, family: &QueueFamilyT) -> bool; + + fn get_type_id_self(&self) -> TypeId{ + TypeId::of::<Self>() + } + + fn get_type_id() -> TypeId where Self: Sized { + TypeId::of::<Self>() + } } impl QueueNegotiator { - pub fn find<F: FnMut(&&QueueFamilyT) -> bool>(adapter: &Adapter, filter: F) -> Result<Self> { - let family = adapter - .queue_families - .iter() - .find(filter) - .ok_or(EnvironmentError::NoSuitableFamilies)?; + pub fn find(adapter: &Adapter, stacks: &[&dyn QueueFamilySelector]) -> Result<Self> { + let mut families = HashMap::new(); + for filter in stacks { + let candidates: Vec<&QueueFamilyT> = adapter + .queue_families + .iter() + .filter(|x| filter.is_suitable(*x)) + .collect(); + + if candidates.len() == 0 { + return Err(Error::new(EnvironmentError::NoSuitableFamilies)); + } + + // Prefer using unique families + let family = match candidates.iter().filter(|x| !families.values().any(|y| *y == x.id())).next() { + Some(x) => *x, + None => candidates[0] + }; + + families.insert(filter.get_type_id_self(), family.id()); + } Ok(QueueNegotiator { - family_id: family.id(), - already_allocated: Vec::with_capacity(family.max_queues()), - next_share: 0, + family_ids: families, + already_allocated: HashMap::new(), }) } - pub fn family<'a>(&self, adapter: &'a Adapter) -> &'a QueueFamilyT { - adapter - .queue_families - .iter() - .find(|x| x.id() == self.family_id) - .unwrap() - } - - pub fn family_id(&self) -> QueueFamilyId { - self.family_id - } + pub fn get_queue<T: QueueFamilySelector>(&mut self, groups: &mut Vec<QueueGroup>) -> Option<Arc<RwLock<QueueT>>> { + let tid = T::get_type_id(); + let family_id = self.family_ids.get(&tid)?; - pub fn get_queue(&mut self, groups: &mut Vec<QueueGroup>) -> Option<Arc<RwLock<QueueT>>> { match groups .iter() - .position(|x| x.queues.len() > 0 && x.family == self.family_id) + .position(|x| x.queues.len() > 0 && x.family == *family_id) { Some(idx) => { // At least one remaining queue let queue = groups[idx].queues.pop().unwrap(); let queue = Arc::new(RwLock::new(queue)); - self.already_allocated.push(queue.clone()); + self.add_to_allocated::<T>(queue.clone()); Some(queue) } - None => { - if self.already_allocated.len() == 0 { - return None; + None => match self.already_allocated.get_mut(&tid) { + Some((queues, next_share)) => { + let queue = (&queues[*next_share]).clone(); + + *next_share += 1; + + Some(queue) } + None => None + } + } + } - let queue = self.already_allocated[self.next_share].clone(); - self.next_share = (self.next_share + 1) % self.already_allocated.len(); + pub fn family<T: QueueFamilySelector>(&self) -> Option<QueueFamilyId> { + self.family_ids.get(&TypeId::of::<T>()).cloned() + } - Some(queue) + fn add_to_allocated<T: QueueFamilySelector>(&mut self, queue: Arc<RwLock<QueueT>>) { + let tid = T::get_type_id(); + match self.already_allocated.get_mut(&tid) { + None => { + self.already_allocated.insert(tid, (vec![queue], 0)); + }, + Some(x) => { + x.0.push(queue); } } } + + pub fn family_spec<'a, T: QueueFamilySelector>(&self, queue_families: &'a Vec<QueueFamilyT>, count: usize) -> Option<(&'a QueueFamilyT, Vec<f32>)> { + let qf_id = self.family::<T>()?; + + let qf = queue_families.iter().find(|x| x.id() == qf_id)?; + let v = vec![1.0; count]; + + Some((qf, v)) + } +} + +pub struct DrawQueue { + pub surface: SurfaceT +} +impl QueueFamilySelector for DrawQueue { + fn is_suitable(&self, family: &QueueFamilyT) -> bool { + self.surface.supports_queue_family(family) && family.queue_type().supports_graphics() + } } |