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
/*!

This module handles the fences of a buffer.

*/
use smallvec::SmallVec;
use std::cell::RefCell;
use std::ops::Range;

use context::CommandContext;
use sync::{self, LinearSyncFence};

/// Contains a list of fences.
pub struct Fences {
    fences: RefCell<SmallVec<[(Range<usize>, LinearSyncFence); 16]>>,
}

impl Fences {
    /// Initialization.
    pub fn new() -> Fences {
        Fences {
            fences: RefCell::new(SmallVec::new()),
        }
    }

    /// Creates an `Inserter` that allows inserting a fence in the list for the given range.
    #[inline]
    pub fn inserter(&self, range: Range<usize>) -> Inserter {
        Inserter {
            fences: self,
            range: range,
        }
    }

    /// Waits until the given range is accessible.
    pub fn wait(&self, ctxt: &mut CommandContext, range: Range<usize>) {
        let mut existing_fences = self.fences.borrow_mut();
        let mut new_fences = SmallVec::new();

        for existing in existing_fences.drain() {
            if (existing.0.start >= range.start && existing.0.start < range.end) ||
               (existing.0.end > range.start && existing.0.end < range.end)
            {
                unsafe { sync::wait_linear_sync_fence_and_drop(existing.1, ctxt) };
            } else {
                new_fences.push(existing);
            }
        }

        *existing_fences = new_fences;
    }

    /// Cleans up all fences in the container. Must be called or you'll get a panic.
    pub fn clean(&mut self, ctxt: &mut CommandContext) {
        let mut fences = self.fences.borrow_mut();
        for (_, sync) in fences.drain() {
            unsafe { sync::destroy_linear_sync_fence(ctxt, sync) };
        }
    }
}

/// Allows inserting a fence in the list.
pub struct Inserter<'a> {
    fences: &'a Fences,
    range: Range<usize>,
}

impl<'a> Inserter<'a> {
    /// Inserts a new fence.
    pub fn insert(self, ctxt: &mut CommandContext) {
        let mut new_fences = SmallVec::new();

        let mut written = false;

        let mut existing_fences = self.fences.fences.borrow_mut();
        for existing in existing_fences.drain() {
            if existing.0.start < self.range.start && existing.0.end <= self.range.start {
                new_fences.push(existing);

            } else if existing.0.start < self.range.start && existing.0.end >= self.range.end {
                // we are stuck here, because we can't duplicate a fence
                // so instead we just extend the new fence to the existing one
                let new_fence = unsafe { sync::new_linear_sync_fence(ctxt).unwrap() };
                new_fences.push((existing.0.start .. self.range.start, existing.1));
                new_fences.push((self.range.start .. existing.0.end, new_fence));
                written = true;

            } else if existing.0.start < self.range.start && existing.0.end >= self.range.start {
                new_fences.push((existing.0.start .. self.range.start, existing.1));
                if !written {
                    let new_fence = unsafe { sync::new_linear_sync_fence(ctxt).unwrap() };
                    new_fences.push((self.range.clone(), new_fence));
                    written = true;
                }

            } else if existing.0.start >= self.range.start && existing.0.end <= self.range.end {
                unsafe { sync::destroy_linear_sync_fence(ctxt, existing.1) };
                if !written {
                    let new_fence = unsafe { sync::new_linear_sync_fence(ctxt).unwrap() };
                    new_fences.push((self.range.clone(), new_fence));
                    written = true;
                }

            } else if existing.0.start >= self.range.end {
                if !written {
                    let new_fence = unsafe { sync::new_linear_sync_fence(ctxt).unwrap() };
                    new_fences.push((self.range.clone(), new_fence));
                    written = true;
                }

                new_fences.push(existing);

            } else {
                if !written {
                    let new_fence = unsafe { sync::new_linear_sync_fence(ctxt).unwrap() };
                    new_fences.push((self.range.clone(), new_fence));
                    written = true;
                }

                new_fences.push((self.range.end .. existing.0.end, existing.1));
            }
        }

        if !written {
            let new_fence = unsafe { sync::new_linear_sync_fence(ctxt).unwrap() };
            new_fences.push((self.range, new_fence));
        }

        *existing_fences = new_fences;
    }
}