aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/queue_negotiator.rs
blob: 4ad68239d5daa52a7e7111480435af449071008d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::{error::EnvironmentError, types::*};
use anyhow::Result;
use hal::queue::family::QueueFamilyId;
use std::sync::{Arc, RwLock};

pub struct QueueNegotiator {
    family_id: QueueFamilyId,
    already_allocated: Vec<Arc<RwLock<QueueT>>>,
    next_share: usize,
}

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)?;

        Ok(QueueNegotiator {
            family_id: family.id(),
            already_allocated: Vec::with_capacity(family.max_queues()),
            next_share: 0,
        })
    }

    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(&mut self, groups: &mut Vec<QueueGroup>) -> Option<Arc<RwLock<QueueT>>> {
        match groups
            .iter()
            .position(|x| x.queues.len() > 0 && x.family == self.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());

                Some(queue)
            }
            None => {
                if self.already_allocated.len() == 0 {
                    return None;
                }

                let queue = self.already_allocated[self.next_share].clone();
                self.next_share = (self.next_share + 1) % self.already_allocated.len();

                Some(queue)
            }
        }
    }
}