rendy_chain/schedule/
mod.rs

1//!
2//! This module defines types for execution hierarchy.
3//!
4//! `Submission` is a piece of work that can be recorded into single primary command buffer.
5//! `Submission` contains references to links and semaphores required to wait/signal.
6//! `Queue` contains array of `Submission`'s. User is expected to submission corresponding command buffers in the order.
7//! `Queue`'s are grouped into `Family`. All queues from one `Family` has identical capabilities.
8//! `Schedule` is a set or `Family` instances.
9//!
10
11mod family;
12mod queue;
13mod submission;
14
15use std::collections::HashMap;
16use std::ops::{Index, IndexMut};
17
18pub use self::{
19    family::Family,
20    queue::{Queue, QueueId},
21    submission::{Submission, SubmissionId},
22};
23
24/// Whole passes schedule.
25#[derive(Clone, Debug)]
26pub struct Schedule<S> {
27    map: HashMap<rendy_core::hal::queue::QueueFamilyId, Family<S>>,
28    ordered: Vec<SubmissionId>,
29}
30
31impl<S> Schedule<S> {
32    /// Create new empty `Schedule`
33    pub fn new() -> Self {
34        Schedule {
35            map: HashMap::default(),
36            ordered: Vec::new(),
37        }
38    }
39
40    /// Get total number of submissions.
41    pub fn total(&self) -> usize {
42        self.ordered.len()
43    }
44
45    /// Iterate over submissions in ordered they must be submitted.
46    pub fn ordered(&self) -> impl Iterator<Item = &Submission<S>> {
47        let ref map = self.map;
48
49        self.ordered
50            .iter()
51            .map(move |&sid| map[&sid.family()].submission(sid).unwrap())
52    }
53
54    /// The number of families in this schedule.
55    pub fn family_count(&self) -> usize {
56        self.map.len()
57    }
58
59    /// The number of queues in this schedule.
60    pub fn queue_count(&self) -> usize {
61        self.map.iter().map(|x| x.1.queue_count()).sum()
62    }
63
64    /// Iterate over immutable references to families in this schedule.
65    pub fn iter(&self) -> impl Iterator<Item = &Family<S>> {
66        self.map.values()
67    }
68
69    /// Iterate over mutable references to families in this schedule
70    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Family<S>> {
71        self.map.values_mut()
72    }
73
74    /// Get reference to `Family` instance by the id.
75    pub fn family(&self, fid: rendy_core::hal::queue::QueueFamilyId) -> Option<&Family<S>> {
76        self.map.get(&fid)
77    }
78
79    /// Get mutable reference to `Family` instance by the id.
80    pub fn family_mut(
81        &mut self,
82        fid: rendy_core::hal::queue::QueueFamilyId,
83    ) -> Option<&mut Family<S>> {
84        self.map.get_mut(&fid)
85    }
86
87    /// Get reference to `Queue` instance by the id.
88    pub fn queue(&self, qid: QueueId) -> Option<&Queue<S>> {
89        self.family(qid.family())
90            .and_then(|family| family.queue(qid))
91    }
92
93    /// Get mutable reference to `Queue` instance by the id.
94    pub fn queue_mut(&mut self, qid: QueueId) -> Option<&mut Queue<S>> {
95        self.family_mut(qid.family())
96            .and_then(|family| family.queue_mut(qid))
97    }
98
99    /// Get reference to `Submission` instance by id.
100    pub fn submission(&self, sid: SubmissionId) -> Option<&Submission<S>> {
101        self.queue(sid.queue())
102            .and_then(|queue| queue.submission(sid))
103    }
104
105    /// Get reference to `Submission` instance by id.
106    pub fn submission_mut(&mut self, sid: SubmissionId) -> Option<&mut Submission<S>> {
107        self.queue_mut(sid.queue())
108            .and_then(|queue| queue.submission_mut(sid))
109    }
110
111    /// Get mutable reference to `Family` instance by the id.
112    /// This function will add empty `Family` if id is not present.
113    pub fn ensure_family(&mut self, fid: rendy_core::hal::queue::QueueFamilyId) -> &mut Family<S> {
114        self.map.entry(fid).or_insert_with(|| Family::new(fid))
115    }
116
117    /// Get mutable reference to `Queue` instance by the id.
118    /// This function will grow queues array if index is out of bounds.
119    pub fn ensure_queue(&mut self, qid: QueueId) -> &mut Queue<S> {
120        self.ensure_family(qid.family()).ensure_queue(qid)
121    }
122
123    /// Set queue to the schedule.
124    pub(crate) fn set_queue(&mut self, queue: Queue<S>) {
125        let qid = queue.id();
126        *self.ensure_queue(qid) = queue;
127    }
128
129    /// Make ordered.
130    pub fn build_order(&mut self) {
131        let mut oredered: Vec<SubmissionId> = Vec::new();
132
133        {
134            let submissions = self
135                .iter()
136                .flat_map(|family| family.iter().flat_map(|queue| queue.iter()));
137
138            for submission in submissions {
139                if submission.submit_order() == !0 {
140                    continue;
141                }
142                let len = oredered.len();
143                if len <= submission.submit_order() {
144                    oredered.extend((len..submission.submit_order() + 1).map(|_| submission.id()));
145                } else {
146                    oredered[submission.submit_order()] = submission.id();
147                }
148            }
149        }
150
151        log::debug!("Schedule order: {:#?}", oredered);
152        self.ordered = oredered;
153    }
154}
155
156impl<S> Index<QueueId> for Schedule<S> {
157    type Output = Queue<S>;
158
159    fn index(&self, qid: QueueId) -> &Queue<S> {
160        self.queue(qid).unwrap()
161    }
162}
163
164impl<S> IndexMut<QueueId> for Schedule<S> {
165    fn index_mut(&mut self, qid: QueueId) -> &mut Queue<S> {
166        self.queue_mut(qid).unwrap()
167    }
168}
169
170impl<S> Index<SubmissionId> for Schedule<S> {
171    type Output = Submission<S>;
172
173    fn index(&self, sid: SubmissionId) -> &Submission<S> {
174        self.submission(sid).unwrap()
175    }
176}
177
178impl<S> IndexMut<SubmissionId> for Schedule<S> {
179    fn index_mut(&mut self, sid: SubmissionId) -> &mut Submission<S> {
180        self.submission_mut(sid).unwrap()
181    }
182}