spirv backend: simplify the adapt.rs indexing by using the constructors previously created

This commit is contained in:
2022-07-26 13:29:39 -07:00
parent 68d8cdde42
commit 6e4133db4d
2 changed files with 23 additions and 30 deletions

View File

@@ -1,3 +1,5 @@
use core::ops::Index;
use coremem_types::compound::Optional;
use coremem_types::vec::Vec3u;
@@ -12,6 +14,8 @@ pub const MAX_UNSIZED_ARRAY: usize = 0x1ff_ffff;
pub struct UnsizedArray<T>(pub [T; MAX_UNSIZED_ARRAY]);
impl<T: Copy> UnsizedArray<T> {
/// user is responsible for bounds-checking the index before calling this.
/// unsafe because this can trigger OOB reads if used incorrectly.
pub unsafe fn index(&self, index: usize) -> T {
// *self.0.index(index)
self.0[index]
@@ -25,13 +29,19 @@ impl<T: Copy> UnsizedArray<T> {
// index = in(reg) index,
//}
}
}
impl<T> UnsizedArray<T> {
/// user is responsible for bounds-checking the index before calling this.
/// unsafe because this can trigger OOB writes if used incorrectly.
pub unsafe fn write(&mut self, index: usize, value: T) {
self.0[index] = value;
}
}
impl<T> UnsizedArray<T> {
/// user is responsible for bounds-checking the index before calling this.
/// unsafe because this can trigger OOB access if used incorrectly.
pub unsafe fn index_ref(&self, index: usize) -> &T {
&self.0[index]
}
@@ -142,7 +152,7 @@ impl<'a, T> Array3<'a, T> {
impl<'a, T: Copy + Default> Array3<'a, T> {
pub fn get(&self, idx: Vec3u) -> Optional<T> {
let idx = self.index(idx);
let idx = checked_index(idx, self.dim);
if idx.is_some() {
Optional::some(unsafe {
self.data.index(idx.unwrap())
@@ -153,6 +163,16 @@ impl<'a, T: Copy + Default> Array3<'a, T> {
}
}
impl<'a, T> Index<Vec3u> for Array3<'a, T> {
type Output = T;
fn index(&self, idx: Vec3u) -> &Self::Output {
let idx = checked_index(idx, self.dim).unwrap();
unsafe {
self.data.index_ref(idx)
}
}
}
/// 3d dynamically-sized array backed by a mutably borrowed buffer.
pub struct Array3Mut<'a, T> {
data: &'a mut UnsizedArray<T>,