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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
mod family;
mod queue;
mod submission;
use std::collections::HashMap;
use std::ops::{Index, IndexMut};
pub use self::{
family::Family,
queue::{Queue, QueueId},
submission::{Submission, SubmissionId},
};
#[derive(Clone, Debug)]
pub struct Schedule<S> {
map: HashMap<gfx_hal::queue::QueueFamilyId, Family<S>>,
ordered: Vec<SubmissionId>,
}
impl<S> Schedule<S> {
pub fn new() -> Self {
Schedule {
map: HashMap::default(),
ordered: Vec::new(),
}
}
pub fn total(&self) -> usize {
self.ordered.len()
}
pub fn ordered(&self) -> impl Iterator<Item = &Submission<S>> {
let ref map = self.map;
self.ordered
.iter()
.map(move |&sid| map[&sid.family()].submission(sid).unwrap())
}
pub fn family_count(&self) -> usize {
self.map.len()
}
pub fn queue_count(&self) -> usize {
self.map.iter().map(|x| x.1.queue_count()).sum()
}
pub fn iter(&self) -> impl Iterator<Item = &Family<S>> {
self.map.values()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Family<S>> {
self.map.values_mut()
}
pub fn family(&self, fid: gfx_hal::queue::QueueFamilyId) -> Option<&Family<S>> {
self.map.get(&fid)
}
pub fn family_mut(&mut self, fid: gfx_hal::queue::QueueFamilyId) -> Option<&mut Family<S>> {
self.map.get_mut(&fid)
}
pub fn queue(&self, qid: QueueId) -> Option<&Queue<S>> {
self.family(qid.family())
.and_then(|family| family.queue(qid))
}
pub fn queue_mut(&mut self, qid: QueueId) -> Option<&mut Queue<S>> {
self.family_mut(qid.family())
.and_then(|family| family.queue_mut(qid))
}
pub fn submission(&self, sid: SubmissionId) -> Option<&Submission<S>> {
self.queue(sid.queue())
.and_then(|queue| queue.submission(sid))
}
pub fn submission_mut(&mut self, sid: SubmissionId) -> Option<&mut Submission<S>> {
self.queue_mut(sid.queue())
.and_then(|queue| queue.submission_mut(sid))
}
pub fn ensure_family(&mut self, fid: gfx_hal::queue::QueueFamilyId) -> &mut Family<S> {
self.map.entry(fid).or_insert_with(|| Family::new(fid))
}
pub fn ensure_queue(&mut self, qid: QueueId) -> &mut Queue<S> {
self.ensure_family(qid.family()).ensure_queue(qid)
}
pub(crate) fn set_queue(&mut self, queue: Queue<S>) {
let qid = queue.id();
*self.ensure_queue(qid) = queue;
}
pub fn build_order(&mut self) {
let mut oredered: Vec<SubmissionId> = Vec::new();
{
let submissions = self
.iter()
.flat_map(|family| family.iter().flat_map(|queue| queue.iter()));
for submission in submissions {
if submission.submit_order() == !0 {
continue;
}
let len = oredered.len();
if len <= submission.submit_order() {
oredered.extend((len..submission.submit_order() + 1).map(|_| submission.id()));
} else {
oredered[submission.submit_order()] = submission.id();
}
}
}
log::debug!("Schedule order: {:#?}", oredered);
self.ordered = oredered;
}
}
impl<S> Index<QueueId> for Schedule<S> {
type Output = Queue<S>;
fn index(&self, qid: QueueId) -> &Queue<S> {
self.queue(qid).unwrap()
}
}
impl<S> IndexMut<QueueId> for Schedule<S> {
fn index_mut(&mut self, qid: QueueId) -> &mut Queue<S> {
self.queue_mut(qid).unwrap()
}
}
impl<S> Index<SubmissionId> for Schedule<S> {
type Output = Submission<S>;
fn index(&self, sid: SubmissionId) -> &Submission<S> {
self.submission(sid).unwrap()
}
}
impl<S> IndexMut<SubmissionId> for Schedule<S> {
fn index_mut(&mut self, sid: SubmissionId) -> &mut Submission<S> {
self.submission_mut(sid).unwrap()
}
}