use crate::backend::RawQueueGroup;
use crate::queue::capability::{Capability, Compute, Graphics, Transfer};
use crate::queue::{CommandQueue, QueueType};
use crate::Backend;
use std::any::Any;
use std::fmt::Debug;
pub trait QueueFamily: Debug + Any + Send + Sync {
fn queue_type(&self) -> QueueType;
fn max_queues(&self) -> usize;
fn supports_graphics(&self) -> bool {
Graphics::supported_by(self.queue_type())
}
fn supports_compute(&self) -> bool {
Compute::supported_by(self.queue_type())
}
fn supports_transfer(&self) -> bool {
Transfer::supported_by(self.queue_type())
}
fn id(&self) -> QueueFamilyId;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct QueueFamilyId(pub usize);
pub struct QueueGroup<B: Backend, C> {
family: QueueFamilyId,
pub queues: Vec<CommandQueue<B, C>>,
}
impl<B: Backend, C> QueueGroup<B, C> {
pub fn family(&self) -> QueueFamilyId {
self.family
}
}
impl<B: Backend, C: Capability> QueueGroup<B, C> {
fn new(raw: RawQueueGroup<B>) -> Self {
assert!(C::supported_by(raw.family.queue_type()));
QueueGroup {
family: raw.family.id(),
queues: raw
.queues
.into_iter()
.map(|q| unsafe { CommandQueue::new(q) })
.collect(),
}
}
}
pub struct Queues<B: Backend>(pub(crate) Vec<RawQueueGroup<B>>);
impl<B: Backend> Queues<B> {
pub fn take<C: Capability>(&mut self, id: QueueFamilyId) -> Option<QueueGroup<B, C>> {
self.0
.iter()
.position(|raw| raw.family.id() == id)
.map(|index| QueueGroup::new(self.0.swap_remove(index)))
}
pub fn take_raw(&mut self, id: QueueFamilyId) -> Option<Vec<B::CommandQueue>> {
self.0
.iter()
.position(|raw| raw.family.id() == id)
.map(|index| self.0.swap_remove(index).queues)
}
}