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
use crate::scalar::{Scalar, ScalarType};
#[cfg(not(target_arch = "spirv"))]
use core::marker::PhantomData;
use core::ops::Index;
#[cfg(target_arch = "spirv")]
use core::{arch::asm, mem::MaybeUninit};
#[cfg(target_arch = "spirv")]
use spirv_std::arch::IndexUnchecked;

/** Unsafe Index trait.

Like [Index], performs checked indexing, but the caller must ensure that there is no aliasing of a mutable reference.
*/
pub trait UnsafeIndex<Idx> {
    /// The returned type after indexing.
    type Output;
    /// Immutably indexes with `index`.
    /// # Safety
    /// The caller must ensure that the returned reference is not aliased by a mutable borrow, ie by a call to `.unsafe_index_mut()` with the same index.
    unsafe fn unsafe_index(&self, index: Idx) -> &Self::Output;
    /// Mutably indexes with `index`.
    /// # Safety
    /// The caller must ensure that the returned reference is not aliased by another borrow, ie by a call to `.unsafe_index()` or `.unsafe_index_mut()` with the same index.
    #[allow(clippy::mut_from_ref)]
    unsafe fn unsafe_index_mut(&self, index: Idx) -> &mut Self::Output;
}

#[cfg(target_arch = "spirv")]
trait IndexUncheckedMutExt<T> {
    unsafe fn index_unchecked_mut_ext(&self, index: usize) -> &mut T;
}

#[cfg(target_arch = "spirv")]
impl<T, const N: usize> IndexUncheckedMutExt<T> for [T; N] {
    #[inline]
    unsafe fn index_unchecked_mut_ext(&self, index: usize) -> &mut T {
        let mut output = MaybeUninit::uninit();
        unsafe {
            asm!(
                "%val_ptr = OpInBoundsAccessChain _ {array_ptr} {index}",
                "OpStore {output} %val_ptr",
                array_ptr = in(reg) self,
                index = in(reg) index,
                output = in(reg) output.as_mut_ptr(),
            );
            output.assume_init()
        }
    }
}

mod sealed {
    pub trait Sealed {}
}
use sealed::Sealed;

/// Base trait for [`BufferBase`] representation.
#[allow(clippy::len_without_is_empty)]
pub trait DataBase: Sealed {
    /// The numerical type of the buffer.
    type Elem: Scalar;
    #[doc(hidden)]
    fn len(&self) -> usize;
}

/// Marker trait for immutable access.
///
/// See [`Slice`].
pub trait Data: DataBase + Index<usize, Output = Self::Elem> {}
/// Marker trait for unsafe access.
///
/// See [`UnsafeSlice`].
pub trait UnsafeData: DataBase + UnsafeIndex<usize, Output = Self::Elem> {}

/// [`Slice`] representation.
#[derive(Clone, Copy)]
pub struct SliceRepr<'a, T> {
    #[cfg(not(target_arch = "spirv"))]
    inner: &'a [T],
    #[cfg(target_arch = "spirv")]
    inner: &'a [T; 1],
    #[cfg(target_arch = "spirv")]
    offset: usize,
    #[cfg(target_arch = "spirv")]
    len: usize,
}

impl<T> Sealed for SliceRepr<'_, T> {}

impl<T: Scalar> DataBase for SliceRepr<'_, T> {
    type Elem = T;
    #[cfg(not(target_arch = "spirv"))]
    #[inline]
    fn len(&self) -> usize {
        self.inner.len()
    }
    #[cfg(target_arch = "spirv")]
    #[inline]
    fn len(&self) -> usize {
        self.len
    }
}

impl<T: Scalar> Index<usize> for SliceRepr<'_, T> {
    type Output = T;
    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        #[cfg(target_arch = "spirv")]
        if index < self.len {
            unsafe { self.inner.index_unchecked(self.offset + index) }
        } else {
            let len = self.len;
            panic!("index out of bounds: the len is {index} but the index is {len}")
        }
        #[cfg(not(target_arch = "spirv"))]
        self.inner.index(index)
    }
}

impl<T: Scalar> Data for SliceRepr<'_, T> {}

/// [`UnsafeSlice`] representation.
#[derive(Clone, Copy)]
pub struct UnsafeSliceRepr<'a, T> {
    #[cfg(not(target_arch = "spirv"))]
    ptr: *mut T,
    #[cfg(target_arch = "spirv")]
    #[allow(unused)]
    inner: &'a [T; 1],
    #[cfg(target_arch = "spirv")]
    #[allow(unused)]
    offset: usize,
    len: usize,
    #[cfg(not(target_arch = "spirv"))]
    _m: PhantomData<&'a ()>,
}

impl<T> Sealed for UnsafeSliceRepr<'_, T> {}

impl<T: Scalar> DataBase for UnsafeSliceRepr<'_, T> {
    type Elem = T;
    #[inline]
    fn len(&self) -> usize {
        self.len
    }
}

impl<T: Scalar> UnsafeIndex<usize> for UnsafeSliceRepr<'_, T> {
    type Output = T;
    #[inline]
    unsafe fn unsafe_index(&self, index: usize) -> &Self::Output {
        if index < self.len {
            #[cfg(target_arch = "spirv")]
            unsafe {
                self.inner.index_unchecked(self.offset + index)
            }
            #[cfg(not(target_arch = "spirv"))]
            unsafe {
                &*self.ptr.add(index)
            }
        } else {
            let len = self.len;
            panic!("index out of bounds: the len is {index} but the index is {len}")
        }
    }
    #[inline]
    unsafe fn unsafe_index_mut(&self, index: usize) -> &mut Self::Output {
        if index < self.len {
            #[cfg(target_arch = "spirv")]
            unsafe {
                self.inner.index_unchecked_mut_ext(self.offset + index)
            }
            #[cfg(not(target_arch = "spirv"))]
            unsafe {
                &mut *self.ptr.add(index)
            }
        } else {
            let len = self.len();
            panic!("index out of bounds: the len is {index} but the index is {len}")
        }
    }
}

impl<T: Scalar> UnsafeData for UnsafeSliceRepr<'_, T> {}

unsafe impl<T: Send> Send for UnsafeSliceRepr<'_, T> {}
unsafe impl<T: Sync> Sync for UnsafeSliceRepr<'_, T> {}

/// A buffer.
///
/// [`Slice`] implements [`Index`] and [`UnsafeSlice`] implements [`UnsafeIndex`].
#[derive(Clone, Copy)]
pub struct BufferBase<S> {
    data: S,
}

/// [`Slice`] implements [`Index`].
///
/// See [`BufferBase`].
pub type Slice<'a, T> = BufferBase<SliceRepr<'a, T>>;
/// [`UnsafeSlice`] implements [`UnsafeIndex`].
///
/// See [`BufferBase`].
pub type UnsafeSlice<'a, T> = BufferBase<UnsafeSliceRepr<'a, T>>;

impl<S: DataBase> BufferBase<S> {
    /// The length of the buffer.
    #[inline]
    pub fn len(&self) -> usize {
        self.data.len()
    }
    /// Whether the buffer is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    #[doc(hidden)]
    #[deprecated(since = "0.0.4", note = "use S::Elem::SCALAR_TYPE")]
    #[inline]
    pub fn scalar_type(&self) -> ScalarType {
        S::Elem::SCALAR_TYPE
    }
}

impl<S: Data> Index<usize> for BufferBase<S> {
    type Output = S::Elem;
    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        self.data.index(index)
    }
}

impl<S: UnsafeData> UnsafeIndex<usize> for BufferBase<S> {
    type Output = S::Elem;
    /// # Safety
    /// The caller must ensure that the returned reference is not aliased by a mutable borrow, ie by a call to `.unsafe_index_mut()` with the same index.
    #[inline]
    unsafe fn unsafe_index(&self, index: usize) -> &Self::Output {
        unsafe { self.data.unsafe_index(index) }
    }
    /// # Safety
    /// The caller must ensure that the returned reference is not aliased by another borrow, ie by a call to `.unsafe_index()` or `.unsafe_index_mut()` with the same index.
    #[inline]
    unsafe fn unsafe_index_mut(&self, index: usize) -> &mut Self::Output {
        unsafe { self.data.unsafe_index_mut(index) }
    }
}

impl<'a, T: Scalar> Slice<'a, T> {
    // For kernel macro.
    #[doc(hidden)]
    #[cfg(target_arch = "spirv")]
    #[inline]
    pub unsafe fn from_raw_parts(inner: &'a [T; 1], offset: usize, len: usize) -> Self {
        let data = SliceRepr { inner, offset, len };
        Self { data }
    }
    /// A pointer to the buffer's data.
    #[cfg(not(target_arch = "spirv"))]
    #[inline]
    pub fn as_ptr(&self) -> *const T {
        self.data.inner.as_ptr()
    }
}

impl<'a, T: Scalar> UnsafeSlice<'a, T> {
    // For kernel macro.
    #[doc(hidden)]
    #[cfg(target_arch = "spirv")]
    #[inline]
    pub unsafe fn from_unsafe_raw_parts(inner: &'a [T; 1], offset: usize, len: usize) -> Self {
        let data = UnsafeSliceRepr {
            inner: &*inner,
            offset,
            len,
        };
        Self { data }
    }
    /// A mutable pointer to the buffer's data.
    #[cfg(not(target_arch = "spirv"))]
    #[inline]
    pub fn as_mut_ptr(&self) -> *mut T {
        self.data.ptr
    }
}

#[cfg(not(target_arch = "spirv"))]
impl<'a, T: Scalar> From<&'a [T]> for Slice<'a, T> {
    #[inline]
    fn from(slice: &'a [T]) -> Self {
        let data = SliceRepr { inner: slice };
        Self { data }
    }
}

#[cfg(not(target_arch = "spirv"))]
impl<'a, T: Scalar> From<Slice<'a, T>> for &'a [T] {
    #[inline]
    fn from(slice: Slice<'a, T>) -> &'a [T] {
        slice.data.inner
    }
}

#[cfg(not(target_arch = "spirv"))]
impl<'a, T: Scalar> From<&'a mut [T]> for UnsafeSlice<'a, T> {
    #[inline]
    fn from(slice: &'a mut [T]) -> Self {
        let data = UnsafeSliceRepr {
            ptr: slice.as_mut_ptr(),
            len: slice.len(),
            _m: PhantomData,
        };
        Self { data }
    }
}