diff --git a/crates/spirv_backend/src/lib.rs b/crates/spirv_backend/src/lib.rs index 830233e..f9ddf6e 100644 --- a/crates/spirv_backend/src/lib.rs +++ b/crates/spirv_backend/src/lib.rs @@ -8,8 +8,7 @@ extern crate spirv_std; -pub use glam::{UVec3, Vec3}; -use spirv_std::glam; +pub use spirv_std::glam::{UVec3, Vec3}; #[cfg(not(target_arch = "spirv"))] use spirv_std::macros::spirv; @@ -24,8 +23,12 @@ use mat::{IsoConductorOr, Ferroxcube3R1MH, FullyGenericMaterial, Material}; type Iso3R1 = IsoConductorOr; +fn glam_vec_to_internal(v: UVec3) -> UVec3Std { + UVec3Std::new(v.x, v.y, v.z) +} + fn step_h( - id: UVec3, + id: UVec3Std, meta: &SerializedSimMeta, stimulus_h: &UnsizedArray, material: &UnsizedArray, @@ -33,7 +36,7 @@ fn step_h( h: &mut UnsizedArray, m: &mut UnsizedArray, ) { - if id.x < meta.dim.x() && id.y < meta.dim.y() && id.z < meta.dim.z() { + if id.x() < meta.dim.x() && id.y() < meta.dim.y() && id.z() < meta.dim.z() { let sim_state = SerializedStepH::new(meta, stimulus_h, material, e, h, m); let update_state = sim_state.index(id); update_state.step_h(); @@ -41,14 +44,14 @@ fn step_h( } fn step_e( - id: UVec3, + id: UVec3Std, meta: &SerializedSimMeta, stimulus_e: &UnsizedArray, material: &UnsizedArray, e: &mut UnsizedArray, h: &UnsizedArray, ) { - if id.x < meta.dim.x() && id.y < meta.dim.y() && id.z < meta.dim.z() { + if id.x() < meta.dim.x() && id.y() < meta.dim.y() && id.z() < meta.dim.z() { let sim_state = SerializedStepE::new(meta, stimulus_e, material, e, h); let update_state = sim_state.index(id); @@ -91,7 +94,7 @@ macro_rules! steps { #[spirv(storage_buffer, descriptor_set = 0, binding = 5)] h: &mut UnsizedArray, #[spirv(storage_buffer, descriptor_set = 0, binding = 6)] m: &mut UnsizedArray, ) { - step_h(id, meta, stimulus_h, material, e, h, m) + step_h(glam_vec_to_internal(id), meta, stimulus_h, material, e, h, m) } #[spirv(compute(threads(4, 4, 4)))] @@ -107,7 +110,7 @@ macro_rules! steps { // XXX: can/should this m input be deleted? #[spirv(storage_buffer, descriptor_set = 0, binding = 6)] _unused_m: &UnsizedArray, ) { - step_e(id, meta, stimulus_e, material, e, h) + step_e(glam_vec_to_internal(id), meta, stimulus_e, material, e, h) } }; } diff --git a/crates/spirv_backend/src/sim.rs b/crates/spirv_backend/src/sim.rs index 23d893b..31cd666 100644 --- a/crates/spirv_backend/src/sim.rs +++ b/crates/spirv_backend/src/sim.rs @@ -1,25 +1,17 @@ // use spirv_std::RuntimeArray; -use spirv_std::glam::UVec3; use crate::mat::Material; use crate::support::{ - Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray, UVec3Std, Vec3Std, IntoGlam as _ + Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray, UVec3Std, Vec3Std }; #[derive(Copy, Clone)] pub struct SerializedSimMeta { - // XXX this HAS to be the tuple version instead of the glam dim, else: - // OpLoad Pointer '138[%138]' is not a logical pointer. pub dim: UVec3Std, pub inv_feature_size: f32, pub time_step: f32, pub feature_size: f32, } -impl SerializedSimMeta { - fn dim(&self) -> UVec3 { - self.dim.into_glam() - } -} /// Whatever data we received from the host in their call to step_h pub struct SerializedStepH<'a, M> { @@ -45,8 +37,8 @@ impl<'a, M> SerializedStepH<'a, M> { } } - pub fn index(self, idx: UVec3) -> StepHContext<'a, M> { - let dim = self.meta.dim(); + pub fn index(self, idx: UVec3Std) -> StepHContext<'a, M> { + let dim = self.meta.dim; let stim_h_matrix = Array3::new(self.stimulus_h, dim); let mat_matrix = Array3::new(self.material, dim); let e = Array3::new(self.e, dim); @@ -55,9 +47,9 @@ impl<'a, M> SerializedStepH<'a, M> { let in_e = VolumeSamplePos { mid: e.get(idx).unwrap(), - xp1: e.get(idx + UVec3::X), - yp1: e.get(idx + UVec3::Y), - zp1: e.get(idx + UVec3::Z), + xp1: e.get(idx + UVec3Std::unit_x()), + yp1: e.get(idx + UVec3Std::unit_y()), + zp1: e.get(idx + UVec3Std::unit_z()), }; let out_h = h.into_mut_handle(idx); let out_m = m.into_mut_handle(idx); @@ -98,27 +90,27 @@ impl<'a, M> SerializedStepE<'a, M> { } } - pub fn index(self, idx: UVec3) -> StepEContext<'a, M> { - let dim = self.meta.dim(); + pub fn index(self, idx: UVec3Std) -> StepEContext<'a, M> { + let dim = self.meta.dim; let stim_e_matrix = Array3::new(self.stimulus_e, dim); let mat_matrix = Array3::new(self.material, dim); let e = Array3Mut::new(self.e, dim); let h = Array3::new(self.h, dim); - let xm1 = if idx.x == 0 { + let xm1 = if idx.x() == 0 { Optional::none() } else { - h.get(idx - UVec3::X) + h.get(idx - UVec3Std::unit_x()) }; - let ym1 = if idx.y == 0 { + let ym1 = if idx.y() == 0 { Optional::none() } else { - h.get(idx - UVec3::Y) + h.get(idx - UVec3Std::unit_y()) }; - let zm1 = if idx.z == 0 { + let zm1 = if idx.z() == 0 { Optional::none() } else { - h.get(idx - UVec3::Z) + h.get(idx - UVec3Std::unit_z()) }; let in_h = VolumeSampleNeg { diff --git a/crates/spirv_backend/src/support.rs b/crates/spirv_backend/src/support.rs index c46cf97..4b90551 100644 --- a/crates/spirv_backend/src/support.rs +++ b/crates/spirv_backend/src/support.rs @@ -1,4 +1,3 @@ -use spirv_std::glam::{self, UVec3}; use coremem_types::vec; use coremem_types::vecu; @@ -13,25 +12,6 @@ pub struct XYZStd(T, T, T); pub type Vec3Std = vec::Vec3; pub type UVec3Std = vecu::Vec3u; -pub trait IntoGlam { - type Output; - fn into_glam(self) -> Self::Output; -} - -impl IntoGlam for Vec3Std { - type Output = glam::Vec3; - fn into_glam(self) -> Self::Output { - Self::Output::new(self.x(), self.y(), self.z()) - } -} - -impl IntoGlam for UVec3Std { - type Output = glam::UVec3; - fn into_glam(self) -> Self::Output { - Self::Output::new(self.x(), self.y(), self.z()) - } -} - // const NULL_VEC3STD: *const Vec3Std = core::ptr::null(); // const NULL_VEC3STD_MUT: *mut Vec3Std = core::ptr::null_mut(); // impl Nullable for Vec3Std { @@ -297,15 +277,15 @@ impl<'a, T: Copy> ArrayHandleMut<'a, T> { #[derive(Clone, Copy)] pub struct Array3<'a, T> { data: &'a UnsizedArray, - dim: UVec3, + dim: UVec3Std, } -fn index(loc: UVec3, dim: UVec3) -> usize { - ((loc.z*dim.y + loc.y)*dim.x + loc.x) as usize +fn index(loc: UVec3Std, dim: UVec3Std) -> usize { + ((loc.z()*dim.y() + loc.y())*dim.x() + loc.x()) as usize } -fn checked_index(idx: UVec3, dim: UVec3) -> Optional { - if idx.x < dim.x && idx.y < dim.y && idx.z < dim.z { +fn checked_index(idx: UVec3Std, dim: UVec3Std) -> Optional { + if idx.x() < dim.x() && idx.y() < dim.y() && idx.z() < dim.z() { let flat_idx = index(idx, dim); Optional::some(flat_idx) } else { @@ -314,18 +294,18 @@ fn checked_index(idx: UVec3, dim: UVec3) -> Optional { } impl<'a, T> Array3<'a, T> { - pub fn new(data: &'a UnsizedArray, dim: UVec3) -> Self { + pub fn new(data: &'a UnsizedArray, dim: UVec3Std) -> Self { Self { data, dim, } } - pub fn index(self, idx: UVec3) -> Optional { + pub fn index(self, idx: UVec3Std) -> Optional { checked_index(idx, self.dim) } - pub fn into_handle(self, idx: UVec3) -> ArrayHandle<'a, T> { + pub fn into_handle(self, idx: UVec3Std) -> ArrayHandle<'a, T> { let idx = checked_index(idx, self.dim).unwrap(); unsafe { self.data.get_handle(idx) @@ -334,7 +314,7 @@ impl<'a, T> Array3<'a, T> { } impl<'a, T: Copy + Default> Array3<'a, T> { - pub fn get(&self, idx: UVec3) -> Optional { + pub fn get(&self, idx: UVec3Std) -> Optional { let idx = self.index(idx); if idx.is_some() { Optional::some(unsafe { @@ -347,7 +327,7 @@ impl<'a, T: Copy + Default> Array3<'a, T> { } // impl<'a, T> Array3<'a, T> { -// pub fn get_ref(&self, idx: UVec3) -> OptionalRef<'a, T> { +// pub fn get_ref(&self, idx: UVec3Std) -> OptionalRef<'a, T> { // OptionalRef::some(unsafe { // self.data.index(0) // }) @@ -364,7 +344,7 @@ impl<'a, T: Copy + Default> Array3<'a, T> { // } // impl<'a> Array3<'a, Vec3Std> { -// pub fn get(&self, idx: UVec3) -> Vec3Std { +// pub fn get(&self, idx: UVec3Std) -> Vec3Std { // let flat_idx = self.index(idx); // (self.data[0].0, 0.0, 0.0) // // let idx = self.index(idx); @@ -380,11 +360,11 @@ impl<'a, T: Copy + Default> Array3<'a, T> { /// 3d dynamically-sized array backed by a mutably borrowed buffer. pub struct Array3Mut<'a, T> { data: &'a mut UnsizedArray, - dim: UVec3, + dim: UVec3Std, } impl<'a, T> Array3Mut<'a, T> { - pub fn new(data: &'a mut UnsizedArray, dim: UVec3) -> Self { + pub fn new(data: &'a mut UnsizedArray, dim: UVec3Std) -> Self { Self { data, dim, @@ -395,8 +375,8 @@ impl<'a, T> Array3Mut<'a, T> { // Array3::new(self.data, self.dim) // } - pub fn index(&self, idx: UVec3) -> Optional { - if idx.x < self.dim.x && idx.y < self.dim.y && idx.z < self.dim.z { + pub fn index(&self, idx: UVec3Std) -> Optional { + if idx.x() < self.dim.x() && idx.y() < self.dim.y() && idx.z() < self.dim.z() { let flat_idx = index(idx, self.dim); Optional::some(flat_idx) } else { @@ -406,19 +386,19 @@ impl<'a, T> Array3Mut<'a, T> { } impl<'a, T: Copy> Array3Mut<'a, T> { - pub fn into_mut_handle(self, idx: UVec3) -> ArrayHandleMut<'a, T> { + pub fn into_mut_handle(self, idx: UVec3Std) -> ArrayHandleMut<'a, T> { let idx = self.index(idx).unwrap(); unsafe { self.data.get_handle_mut(idx) } } - // fn index(&self, idx: UVec3) -> Optional { + // fn index(&self, idx: UVec3Std) -> Optional { // self.as_array3().index(idx) // } - // // pub fn get(&self, idx: UVec3) -> Option { + // // pub fn get(&self, idx: UVec3Std) -> Option { // // self.get_ref(idx).cloned() // // } - // pub fn get_ref<'b>(&'b self, idx: UVec3) -> OptionalRef<'b, T> { + // pub fn get_ref<'b>(&'b self, idx: UVec3Std) -> OptionalRef<'b, T> { // let idx = self.index(idx); // if idx.is_some() { // OptionalRef::some(&self.data[idx.unwrap()]) @@ -426,15 +406,15 @@ impl<'a, T: Copy> Array3Mut<'a, T> { // OptionalRef::none() // } // } - // pub fn get_mut(&mut self, idx: UVec3) -> Option { + // pub fn get_mut(&mut self, idx: UVec3Std) -> Option { // self.get_ref_mut(idx).cloned() // } - // pub fn get_ref_mut<'b>(&'b mut self, idx: UVec3) -> Option<&'b mut T> { + // pub fn get_ref_mut<'b>(&'b mut self, idx: UVec3Std) -> Option<&'b mut T> { // self.data.get_mut(index(idx, self.dim)) // } // /// Like `get_ref_mut`, but imposes fewer lifetime requirements by consuming `self`. - // pub fn into_ref_mut(self, idx: UVec3) -> Option<&'a mut T> { + // pub fn into_ref_mut(self, idx: UVec3Std) -> Option<&'a mut T> { // self.data.get_mut(index(idx, self.dim)) // } } @@ -445,16 +425,16 @@ mod test { use super::*; #[test] fn test_index() { - let dim = UVec3::new(2, 3, 7); - assert_eq!(index(UVec3::new(0, 0, 0), dim), 0); - assert_eq!(index(UVec3::new(1, 0, 0), dim), 1); - assert_eq!(index(UVec3::new(0, 1, 0), dim), 2); - assert_eq!(index(UVec3::new(1, 1, 0), dim), 3); - assert_eq!(index(UVec3::new(0, 2, 0), dim), 4); - assert_eq!(index(UVec3::new(0, 0, 1), dim), 6); - assert_eq!(index(UVec3::new(1, 0, 1), dim), 7); - assert_eq!(index(UVec3::new(0, 1, 1), dim), 8); - assert_eq!(index(UVec3::new(1, 2, 1), dim), 11); - assert_eq!(index(UVec3::new(1, 2, 2), dim), 17); + let dim = UVec3Std::new(2, 3, 7); + assert_eq!(index(UVec3Std::new(0, 0, 0), dim), 0); + assert_eq!(index(UVec3Std::new(1, 0, 0), dim), 1); + assert_eq!(index(UVec3Std::new(0, 1, 0), dim), 2); + assert_eq!(index(UVec3Std::new(1, 1, 0), dim), 3); + assert_eq!(index(UVec3Std::new(0, 2, 0), dim), 4); + assert_eq!(index(UVec3Std::new(0, 0, 1), dim), 6); + assert_eq!(index(UVec3Std::new(1, 0, 1), dim), 7); + assert_eq!(index(UVec3Std::new(0, 1, 1), dim), 8); + assert_eq!(index(UVec3Std::new(1, 2, 1), dim), 11); + assert_eq!(index(UVec3Std::new(1, 2, 2), dim), 17); } } diff --git a/crates/types/src/vecu.rs b/crates/types/src/vecu.rs index 4d7748a..d2c80c9 100644 --- a/crates/types/src/vecu.rs +++ b/crates/types/src/vecu.rs @@ -34,6 +34,15 @@ impl Vec3u { pub fn z(&self) -> u32 { self.z } + pub fn unit_x() -> Self { + Self::new(1, 0, 0) + } + pub fn unit_y() -> Self { + Self::new(0, 1, 0) + } + pub fn unit_z() -> Self { + Self::new(0, 0, 1) + } /// Convert for use with ndarray indexing pub fn row_major_idx(&self) -> [usize; 3] { [self.z as _, self.y as _, self.x as _]