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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use super::{PendingTransition, ResourceState, Unit};
use crate::{
id::{BufferId, Valid},
resource::BufferUse,
};
//TODO: store `hal::buffer::State` here to avoid extra conversions
pub(crate) type BufferState = Unit<BufferUse>;
impl PendingTransition<BufferState> {
fn collapse(self) -> Result<BufferUse, Self> {
if self.usage.start.is_empty()
|| self.usage.start == self.usage.end
|| !BufferUse::WRITE_ALL.intersects(self.usage.start | self.usage.end)
{
Ok(self.usage.start | self.usage.end)
} else {
Err(self)
}
}
}
impl Default for BufferState {
fn default() -> Self {
Self {
first: None,
last: BufferUse::empty(),
}
}
}
impl BufferState {
pub fn with_usage(usage: BufferUse) -> Self {
Unit::new(usage)
}
}
impl ResourceState for BufferState {
type Id = BufferId;
type Selector = ();
type Usage = BufferUse;
fn query(&self, _selector: Self::Selector) -> Option<Self::Usage> {
Some(self.last)
}
fn change(
&mut self,
id: Valid<Self::Id>,
_selector: Self::Selector,
usage: Self::Usage,
output: Option<&mut Vec<PendingTransition<Self>>>,
) -> Result<(), PendingTransition<Self>> {
let old = self.last;
if old != usage || !BufferUse::ORDERED.contains(usage) {
let pending = PendingTransition {
id,
selector: (),
usage: old..usage,
};
*self = match output {
None => {
assert_eq!(
self.first, None,
"extending a state that is already a transition"
);
Unit::new(pending.collapse()?)
}
Some(transitions) => {
transitions.push(pending);
Unit {
first: self.first.or(Some(old)),
last: usage,
}
}
};
}
Ok(())
}
fn prepend(
&mut self,
id: Valid<Self::Id>,
_selector: Self::Selector,
usage: Self::Usage,
) -> Result<(), PendingTransition<Self>> {
match self.first {
Some(old) if old != usage => Err(PendingTransition {
id,
selector: (),
usage: old..usage,
}),
_ => {
self.first = Some(usage);
Ok(())
}
}
}
fn merge(
&mut self,
id: Valid<Self::Id>,
other: &Self,
output: Option<&mut Vec<PendingTransition<Self>>>,
) -> Result<(), PendingTransition<Self>> {
let old = self.last;
let new = other.port();
if old == new && BufferUse::ORDERED.contains(new) {
if output.is_some() && self.first.is_none() {
self.first = Some(old);
}
} else {
let pending = PendingTransition {
id,
selector: (),
usage: old..new,
};
*self = match output {
None => {
assert_eq!(
self.first, None,
"extending a state that is already a transition"
);
Unit::new(pending.collapse()?)
}
Some(transitions) => {
transitions.push(pending);
Unit {
first: self.first.or(Some(old)),
last: other.last,
}
}
};
}
Ok(())
}
fn optimize(&mut self) {}
}
#[cfg(test)]
mod test {
use super::*;
use crate::id::Id;
#[test]
fn change_extend() {
let mut bs = Unit {
first: None,
last: BufferUse::INDEX,
};
let id = Id::dummy();
assert_eq!(
bs.change(id, (), BufferUse::STORAGE_STORE, None),
Err(PendingTransition {
id,
selector: (),
usage: BufferUse::INDEX..BufferUse::STORAGE_STORE,
}),
);
bs.change(id, (), BufferUse::VERTEX, None).unwrap();
bs.change(id, (), BufferUse::INDEX, None).unwrap();
assert_eq!(bs, Unit::new(BufferUse::VERTEX | BufferUse::INDEX));
}
#[test]
fn change_replace() {
let mut bs = Unit {
first: None,
last: BufferUse::STORAGE_STORE,
};
let id = Id::dummy();
let mut list = Vec::new();
bs.change(id, (), BufferUse::VERTEX, Some(&mut list))
.unwrap();
assert_eq!(
&list,
&[PendingTransition {
id,
selector: (),
usage: BufferUse::STORAGE_STORE..BufferUse::VERTEX,
}],
);
assert_eq!(
bs,
Unit {
first: Some(BufferUse::STORAGE_STORE),
last: BufferUse::VERTEX,
}
);
list.clear();
bs.change(id, (), BufferUse::STORAGE_STORE, Some(&mut list))
.unwrap();
assert_eq!(
&list,
&[PendingTransition {
id,
selector: (),
usage: BufferUse::VERTEX..BufferUse::STORAGE_STORE,
}],
);
assert_eq!(
bs,
Unit {
first: Some(BufferUse::STORAGE_STORE),
last: BufferUse::STORAGE_STORE,
}
);
}
#[test]
fn prepend() {
let mut bs = Unit {
first: None,
last: BufferUse::VERTEX,
};
let id = Id::dummy();
bs.prepend(id, (), BufferUse::INDEX).unwrap();
bs.prepend(id, (), BufferUse::INDEX).unwrap();
assert_eq!(
bs.prepend(id, (), BufferUse::STORAGE_LOAD),
Err(PendingTransition {
id,
selector: (),
usage: BufferUse::INDEX..BufferUse::STORAGE_LOAD,
})
);
assert_eq!(
bs,
Unit {
first: Some(BufferUse::INDEX),
last: BufferUse::VERTEX,
}
);
}
}
|