Debian Codegraph @

third_party/rust/wgpu-core/src/indirect_validation/utils.rs

raw
1 use alloc::vec::Vec;
2
3 pub(crate) struct UniqueIndexScratch(bit_set::BitSet);
4
5 impl UniqueIndexScratch {
6 pub(crate) fn new() -> Self {
7 Self(bit_set::BitSet::new())
8 }
9 }
10
11 pub(crate) struct UniqueIndex<'a, I: Iterator<Item = usize>> {
12 inner: I,
13 scratch: &'a mut UniqueIndexScratch,
14 }
15
16 impl<'a, I: Iterator<Item = usize>> UniqueIndex<'a, I> {
17 fn new(inner: I, scratch: &'a mut UniqueIndexScratch) -> Self {
18 scratch.0.clear();
19 Self { inner, scratch }
20 }
21 }
22
23 impl<'a, I: Iterator<Item = usize>> Iterator for UniqueIndex<'a, I> {
24 type Item = usize;
25
26 fn next(&mut self) -> Option<Self::Item> {
27 self.inner.find(|&i| self.scratch.0.insert(i))
28 }
29 }
30
31 pub(crate) trait UniqueIndexExt: Iterator<Item = usize> {
32 fn unique<'a>(self, scratch: &'a mut UniqueIndexScratch) -> UniqueIndex<'a, Self>
33 where
34 Self: Sized,
35 {
36 UniqueIndex::new(self, scratch)
37 }
38 }
39
40 impl<T: Iterator<Item = usize>> UniqueIndexExt for T {}
41
42 type BufferBarrier<'b> = hal::BufferBarrier<'b, dyn hal::DynBuffer>;
43
44 pub(crate) struct BufferBarrierScratch<'b>(Vec<BufferBarrier<'b>>);
45
46 impl<'b> BufferBarrierScratch<'b> {
47 pub(crate) fn new() -> Self {
48 Self(Vec::new())
49 }
50 }
51
52 pub(crate) struct BufferBarriers<'a, 'b> {
53 scratch: &'a mut BufferBarrierScratch<'b>,
54 }
55
56 impl<'a, 'b> BufferBarriers<'a, 'b> {
57 pub(crate) fn new(scratch: &'a mut BufferBarrierScratch<'_>) -> Self {
58 // change lifetime of buffer reference, this is safe since `scratch` is empty,
59 // it was either just created or it has been cleared on `BufferBarriers::drop`
60 let scratch = unsafe {
61 core::mem::transmute::<&'a mut BufferBarrierScratch<'_>, &'a mut BufferBarrierScratch<'b>>(
62 scratch,
63 )
64 };
65 Self { scratch }
66 }
67
68 pub(crate) fn extend(self, iter: impl Iterator<Item = BufferBarrier<'b>>) -> Self {
69 self.scratch.0.extend(iter);
70 self
71 }
72
73 pub(crate) fn encode(self, encoder: &mut dyn hal::DynCommandEncoder) {
74 unsafe {
75 encoder.transition_buffers(&self.scratch.0);
76 }
77 }
78 }
79
80 impl<'a, 'b> Drop for BufferBarriers<'a, 'b> {
81 fn drop(&mut self) {
82 self.scratch.0.clear();
83 }
84 }