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,6 +1,5 @@
use coremem_types::step::{StepEContext, StepHContext, VolumeSampleNeg, VolumeSamplePos};
use crate::support::{Array3, Array3Mut, UnsizedArray};
use coremem_types::compound::Optional;
use coremem_types::mat::Material;
use coremem_types::real::Real;
use coremem_types::step::SimMeta;
@@ -76,12 +75,7 @@ impl<'a, R: Real, M> SerializedStepH<'a, R, M> {
let h = Array3::new(self.h, dim);
let m = Array3::new(self.m, dim);
let in_e = VolumeSamplePos {
mid: e.get(idx).unwrap(),
xp1: e.get(idx + Vec3u::unit_x()),
yp1: e.get(idx + Vec3u::unit_y()),
zp1: e.get(idx + Vec3u::unit_z()),
};
let in_e = VolumeSamplePos::from_indexable(e, dim, idx);
let in_h = h.get(idx).unwrap();
let in_m = m.get(idx).unwrap();
@@ -140,29 +134,8 @@ impl<'a, R: Real, M> SerializedStepE<'a, R, M> {
let e = Array3::new(self.e, dim);
let h = Array3::new(self.h, dim);
let xm1 = if idx.x() == 0 {
Optional::none()
} else {
h.get(idx - Vec3u::unit_x())
};
let ym1 = if idx.y() == 0 {
Optional::none()
} else {
h.get(idx - Vec3u::unit_y())
};
let zm1 = if idx.z() == 0 {
Optional::none()
} else {
h.get(idx - Vec3u::unit_z())
};
let in_h = VolumeSampleNeg {
mid: h.get(idx).unwrap(),
xm1,
ym1,
zm1,
};
let in_e = e.get(idx).unwrap();
let in_h = VolumeSampleNeg::from_indexable(h, idx);
let mat = mat_matrix.into_ref(idx);

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>,