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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use rfw_backend::*;
use rfw_math::*;
use std::cell::UnsafeCell;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;

use crate::utils::Transform;

bitflags::bitflags! {
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[repr(transparent)]
    pub struct InstanceFlags2D: u32 {
        const TRANSFORMED = 1;
    }
}

#[derive(Debug, Clone)]
pub struct InstanceList2D {
    pub(crate) list: Arc<UnsafeCell<List2D>>,
}

/// Although sharing instances amongst multiple threads without any mitigations against data races
/// is unsafe, the performance benefits of not doing any mitigation is too great to neglect this
/// opportunity (especially with many instances).
unsafe impl Send for InstanceList2D {}
unsafe impl Sync for InstanceList2D {}

impl From<List2D> for InstanceList2D {
    fn from(l: List2D) -> Self {
        Self {
            list: Arc::new(UnsafeCell::new(l)),
        }
    }
}

impl From<InstanceList2D> for List2D {
    fn from(l: InstanceList2D) -> Self {
        l.clone_inner()
    }
}

impl<'a> From<&'a InstanceList2D> for InstancesData2D<'a> {
    fn from(list: &'a InstanceList2D) -> Self {
        Self {
            matrices: list.matrices(),
        }
    }
}

impl<'a> From<&'a mut InstanceList2D> for InstancesData2D<'a> {
    fn from(list: &'a mut InstanceList2D) -> Self {
        Self {
            matrices: list.matrices(),
        }
    }
}

impl Default for InstanceList2D {
    fn default() -> Self {
        Self {
            list: Arc::new(UnsafeCell::new(List2D::default())),
        }
    }
}

impl InstanceList2D {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn len(&self) -> usize {
        unsafe { (*self.list.get()).ptr.load(Ordering::SeqCst) }
    }

    pub fn is_empty(&self) -> bool {
        (unsafe { (*self.list.get()).ptr.load(Ordering::SeqCst) }) == 0
    }

    pub fn allocate(&mut self) -> InstanceHandle2D {
        let list = unsafe { self.list.get().as_mut().unwrap() };
        if let Some(id) = list.free_slots.pop() {
            return InstanceHandle2D {
                index: id,
                ptr: self.list.clone(),
            };
        }

        let id = list.ptr.load(Ordering::Acquire);
        if (id + 1) >= list.matrices.len() {
            self.resize((id + 1) * 4);
        }
        list.ptr.store(id + 1, Ordering::Release);
        list.flags[id] = InstanceFlags2D::all();
        list.matrices[id] = Mat4::IDENTITY;

        InstanceHandle2D {
            index: id,
            ptr: self.list.clone(),
        }
    }

    pub fn make_invalid(&mut self, handle: InstanceHandle2D) {
        let list = unsafe { self.list.get().as_mut().unwrap() };
        list.matrices[handle.index] = Mat4::ZERO;
        list.flags[handle.index] = InstanceFlags2D::all();
        list.free_slots.push(handle.index);
        list.removed.push(handle.index);
    }

    pub fn resize(&mut self, new_size: usize) {
        let list = unsafe { self.list.get().as_mut().unwrap() };
        list.matrices.resize(new_size, Mat4::ZERO);
        list.flags.resize(new_size, InstanceFlags2D::empty());
    }

    pub fn get(&self, index: usize) -> Option<InstanceHandle2D> {
        let list = unsafe { self.list.get().as_mut().unwrap() };
        if list.matrices.get(index).is_some() {
            Some(InstanceHandle2D {
                index,
                ptr: self.list.clone(),
            })
        } else {
            None
        }
    }

    pub fn matrices(&self) -> &[Mat4] {
        let list = self.list.get();
        unsafe { &(*list).matrices[0..(*list).len()] }
    }

    pub fn flags(&self) -> &[InstanceFlags2D] {
        let list = self.list.get();
        unsafe { &(*list).flags[0..(*list).len()] }
    }

    pub fn set_all_flags(&mut self, flag: InstanceFlags2D) {
        let list = self.list.get();
        let flags = unsafe { &mut (*list).flags[0..(*list).len()] };
        flags.iter_mut().for_each(|f| {
            (*f) |= flag;
        });
    }

    pub fn clone_inner(&self) -> List2D {
        unsafe { self.list.get().as_ref().unwrap() }.clone()
    }

    pub fn iter(&self) -> InstanceIterator2D {
        InstanceIterator2D {
            list: self.list.clone(),
            current: 0,
            ptr: unsafe { (*self.list.get()).len() },
        }
    }

    pub fn any_changed(&self) -> bool {
        for flag in self.flags() {
            if !flag.is_empty() {
                return true;
            }
        }

        false
    }

    pub fn reset_changed(&mut self) {
        let list = unsafe { (*self.list.get()).flags.as_mut_slice() };
        for v in list.iter_mut() {
            *v = InstanceFlags2D::empty();
        }
    }

    pub fn take_removed(&mut self) -> Vec<usize> {
        let list = unsafe { self.list.get().as_mut().unwrap() };
        let mut vec = Vec::new();
        std::mem::swap(&mut vec, &mut list.removed);
        vec
    }
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Default)]
pub struct List2D {
    matrices: Vec<Mat4>,
    flags: Vec<InstanceFlags2D>,

    ptr: AtomicUsize,
    free_slots: Vec<usize>,
    removed: Vec<usize>,
}

impl Clone for List2D {
    fn clone(&self) -> Self {
        let ptr = AtomicUsize::new(self.ptr.load(Ordering::Acquire));
        let this = Self {
            matrices: self.matrices.clone(),
            flags: self.flags.clone(),

            ptr,
            free_slots: self.free_slots.clone(),
            removed: self.removed.clone(),
        };

        self.ptr.load(Ordering::Acquire);
        this
    }
}

impl List2D {
    pub fn len(&self) -> usize {
        self.ptr.load(Ordering::Acquire)
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

#[derive(Debug)]
pub struct InstanceIterator2D {
    list: Arc<UnsafeCell<List2D>>,
    current: usize,
    ptr: usize,
}

impl Clone for InstanceIterator2D {
    fn clone(&self) -> Self {
        Self {
            list: self.list.clone(),
            current: 0,
            ptr: unsafe { (*self.list.get()).ptr.load(Ordering::Acquire) },
        }
    }
}

impl Iterator for InstanceIterator2D {
    type Item = InstanceHandle2D;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current < self.ptr {
            let index = self.current;
            self.current += 1;
            return Some(InstanceHandle2D {
                index,
                ptr: self.list.clone(),
            });
        }

        None
    }
}

#[derive(Debug)]
pub struct InstanceHandle2D {
    index: usize,
    ptr: Arc<UnsafeCell<List2D>>,
}

unsafe impl Send for InstanceHandle2D {}
unsafe impl Sync for InstanceHandle2D {}

impl InstanceHandle2D {
    #[inline]
    pub fn set_matrix(&mut self, matrix: Mat4) {
        let list = unsafe { self.ptr.get().as_mut().unwrap() };
        list.matrices[self.index] = matrix;
        list.flags[self.index] |= InstanceFlags2D::TRANSFORMED;
    }

    #[inline]
    pub fn get_transform(&mut self) -> Transform<Self> {
        let (scale, rotation, translation) = self.get_matrix().to_scale_rotation_translation();

        Transform {
            translation,
            rotation,
            scale,
            handle: self,
            changed: false,
        }
    }

    #[inline]
    pub fn get_matrix(&self) -> Mat4 {
        unsafe { (*self.ptr.get()).matrices[self.index] }
    }

    #[inline]
    pub fn get_flags(&self) -> InstanceFlags2D {
        unsafe { (*self.ptr.get()).flags[self.index] }
    }

    #[inline]
    pub fn get_id(&self) -> usize {
        self.index
    }

    #[inline]
    pub fn transformed(&self) -> bool {
        unsafe { (*self.ptr.get()).flags[self.index].contains(InstanceFlags2D::TRANSFORMED) }
    }

    #[inline]
    pub fn make_invalid(self) {
        let list = unsafe { self.ptr.get().as_mut().unwrap() };
        list.matrices[self.index] = Mat4::ZERO;
        list.flags[self.index] = InstanceFlags2D::all();
        list.free_slots.push(self.index);
        list.removed.push(self.index);
    }

    /// # Safety
    ///
    /// There should only be a single instance of a handle at a time.
    /// Using these handles makes updating instances fast but leaves safety up to the user.
    pub unsafe fn clone_handle(&self) -> Self {
        Self {
            index: self.index,
            ptr: self.ptr.clone(),
        }
    }
}