spirv_backend: strip out the glam stuff, except for the lib.rs interface

This commit is contained in:
2022-07-18 01:48:19 -07:00
parent d92089ae23
commit 4378f33eb9
4 changed files with 67 additions and 83 deletions

View File

@@ -8,8 +8,7 @@
extern crate spirv_std; extern crate spirv_std;
pub use glam::{UVec3, Vec3}; pub use spirv_std::glam::{UVec3, Vec3};
use spirv_std::glam;
#[cfg(not(target_arch = "spirv"))] #[cfg(not(target_arch = "spirv"))]
use spirv_std::macros::spirv; use spirv_std::macros::spirv;
@@ -24,8 +23,12 @@ use mat::{IsoConductorOr, Ferroxcube3R1MH, FullyGenericMaterial, Material};
type Iso3R1 = IsoConductorOr<Ferroxcube3R1MH>; type Iso3R1 = IsoConductorOr<Ferroxcube3R1MH>;
fn glam_vec_to_internal(v: UVec3) -> UVec3Std {
UVec3Std::new(v.x, v.y, v.z)
}
fn step_h<M: Material>( fn step_h<M: Material>(
id: UVec3, id: UVec3Std,
meta: &SerializedSimMeta, meta: &SerializedSimMeta,
stimulus_h: &UnsizedArray<Vec3Std>, stimulus_h: &UnsizedArray<Vec3Std>,
material: &UnsizedArray<M>, material: &UnsizedArray<M>,
@@ -33,7 +36,7 @@ fn step_h<M: Material>(
h: &mut UnsizedArray<Vec3Std>, h: &mut UnsizedArray<Vec3Std>,
m: &mut UnsizedArray<Vec3Std>, m: &mut UnsizedArray<Vec3Std>,
) { ) {
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 sim_state = SerializedStepH::new(meta, stimulus_h, material, e, h, m);
let update_state = sim_state.index(id); let update_state = sim_state.index(id);
update_state.step_h(); update_state.step_h();
@@ -41,14 +44,14 @@ fn step_h<M: Material>(
} }
fn step_e<M: Material>( fn step_e<M: Material>(
id: UVec3, id: UVec3Std,
meta: &SerializedSimMeta, meta: &SerializedSimMeta,
stimulus_e: &UnsizedArray<Vec3Std>, stimulus_e: &UnsizedArray<Vec3Std>,
material: &UnsizedArray<M>, material: &UnsizedArray<M>,
e: &mut UnsizedArray<Vec3Std>, e: &mut UnsizedArray<Vec3Std>,
h: &UnsizedArray<Vec3Std>, h: &UnsizedArray<Vec3Std>,
) { ) {
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 sim_state = SerializedStepE::new(meta, stimulus_e, material, e, h);
let update_state = sim_state.index(id); 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<Vec3Std>, #[spirv(storage_buffer, descriptor_set = 0, binding = 5)] h: &mut UnsizedArray<Vec3Std>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 6)] m: &mut UnsizedArray<Vec3Std>, #[spirv(storage_buffer, descriptor_set = 0, binding = 6)] m: &mut UnsizedArray<Vec3Std>,
) { ) {
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)))] #[spirv(compute(threads(4, 4, 4)))]
@@ -107,7 +110,7 @@ macro_rules! steps {
// XXX: can/should this m input be deleted? // XXX: can/should this m input be deleted?
#[spirv(storage_buffer, descriptor_set = 0, binding = 6)] _unused_m: &UnsizedArray<Vec3Std>, #[spirv(storage_buffer, descriptor_set = 0, binding = 6)] _unused_m: &UnsizedArray<Vec3Std>,
) { ) {
step_e(id, meta, stimulus_e, material, e, h) step_e(glam_vec_to_internal(id), meta, stimulus_e, material, e, h)
} }
}; };
} }

View File

@@ -1,25 +1,17 @@
// use spirv_std::RuntimeArray; // use spirv_std::RuntimeArray;
use spirv_std::glam::UVec3;
use crate::mat::Material; use crate::mat::Material;
use crate::support::{ use crate::support::{
Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray, UVec3Std, Vec3Std, IntoGlam as _ Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray, UVec3Std, Vec3Std
}; };
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct SerializedSimMeta { pub struct SerializedSimMeta {
// XXX this HAS to be the tuple version instead of the glam dim, else:
// OpLoad Pointer <id> '138[%138]' is not a logical pointer.
pub dim: UVec3Std, pub dim: UVec3Std,
pub inv_feature_size: f32, pub inv_feature_size: f32,
pub time_step: f32, pub time_step: f32,
pub feature_size: 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 /// Whatever data we received from the host in their call to step_h
pub struct SerializedStepH<'a, M> { pub struct SerializedStepH<'a, M> {
@@ -45,8 +37,8 @@ impl<'a, M> SerializedStepH<'a, M> {
} }
} }
pub fn index(self, idx: UVec3) -> StepHContext<'a, M> { pub fn index(self, idx: UVec3Std) -> StepHContext<'a, M> {
let dim = self.meta.dim(); let dim = self.meta.dim;
let stim_h_matrix = Array3::new(self.stimulus_h, dim); let stim_h_matrix = Array3::new(self.stimulus_h, dim);
let mat_matrix = Array3::new(self.material, dim); let mat_matrix = Array3::new(self.material, dim);
let e = Array3::new(self.e, dim); let e = Array3::new(self.e, dim);
@@ -55,9 +47,9 @@ impl<'a, M> SerializedStepH<'a, M> {
let in_e = VolumeSamplePos { let in_e = VolumeSamplePos {
mid: e.get(idx).unwrap(), mid: e.get(idx).unwrap(),
xp1: e.get(idx + UVec3::X), xp1: e.get(idx + UVec3Std::unit_x()),
yp1: e.get(idx + UVec3::Y), yp1: e.get(idx + UVec3Std::unit_y()),
zp1: e.get(idx + UVec3::Z), zp1: e.get(idx + UVec3Std::unit_z()),
}; };
let out_h = h.into_mut_handle(idx); let out_h = h.into_mut_handle(idx);
let out_m = m.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> { pub fn index(self, idx: UVec3Std) -> StepEContext<'a, M> {
let dim = self.meta.dim(); let dim = self.meta.dim;
let stim_e_matrix = Array3::new(self.stimulus_e, dim); let stim_e_matrix = Array3::new(self.stimulus_e, dim);
let mat_matrix = Array3::new(self.material, dim); let mat_matrix = Array3::new(self.material, dim);
let e = Array3Mut::new(self.e, dim); let e = Array3Mut::new(self.e, dim);
let h = Array3::new(self.h, dim); let h = Array3::new(self.h, dim);
let xm1 = if idx.x == 0 { let xm1 = if idx.x() == 0 {
Optional::none() Optional::none()
} else { } 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() Optional::none()
} else { } 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() Optional::none()
} else { } else {
h.get(idx - UVec3::Z) h.get(idx - UVec3Std::unit_z())
}; };
let in_h = VolumeSampleNeg { let in_h = VolumeSampleNeg {

View File

@@ -1,4 +1,3 @@
use spirv_std::glam::{self, UVec3};
use coremem_types::vec; use coremem_types::vec;
use coremem_types::vecu; use coremem_types::vecu;
@@ -13,25 +12,6 @@ pub struct XYZStd<T>(T, T, T);
pub type Vec3Std = vec::Vec3<f32>; pub type Vec3Std = vec::Vec3<f32>;
pub type UVec3Std = vecu::Vec3u; 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: *const Vec3Std = core::ptr::null();
// const NULL_VEC3STD_MUT: *mut Vec3Std = core::ptr::null_mut(); // const NULL_VEC3STD_MUT: *mut Vec3Std = core::ptr::null_mut();
// impl Nullable for Vec3Std { // impl Nullable for Vec3Std {
@@ -297,15 +277,15 @@ impl<'a, T: Copy> ArrayHandleMut<'a, T> {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Array3<'a, T> { pub struct Array3<'a, T> {
data: &'a UnsizedArray<T>, data: &'a UnsizedArray<T>,
dim: UVec3, dim: UVec3Std,
} }
fn index(loc: UVec3, dim: UVec3) -> usize { fn index(loc: UVec3Std, dim: UVec3Std) -> usize {
((loc.z*dim.y + loc.y)*dim.x + loc.x) as usize ((loc.z()*dim.y() + loc.y())*dim.x() + loc.x()) as usize
} }
fn checked_index(idx: UVec3, dim: UVec3) -> Optional<usize> { fn checked_index(idx: UVec3Std, dim: UVec3Std) -> Optional<usize> {
if idx.x < dim.x && idx.y < dim.y && idx.z < dim.z { if idx.x() < dim.x() && idx.y() < dim.y() && idx.z() < dim.z() {
let flat_idx = index(idx, dim); let flat_idx = index(idx, dim);
Optional::some(flat_idx) Optional::some(flat_idx)
} else { } else {
@@ -314,18 +294,18 @@ fn checked_index(idx: UVec3, dim: UVec3) -> Optional<usize> {
} }
impl<'a, T> Array3<'a, T> { impl<'a, T> Array3<'a, T> {
pub fn new(data: &'a UnsizedArray<T>, dim: UVec3) -> Self { pub fn new(data: &'a UnsizedArray<T>, dim: UVec3Std) -> Self {
Self { Self {
data, data,
dim, dim,
} }
} }
pub fn index(self, idx: UVec3) -> Optional<usize> { pub fn index(self, idx: UVec3Std) -> Optional<usize> {
checked_index(idx, self.dim) 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(); let idx = checked_index(idx, self.dim).unwrap();
unsafe { unsafe {
self.data.get_handle(idx) self.data.get_handle(idx)
@@ -334,7 +314,7 @@ impl<'a, T> Array3<'a, T> {
} }
impl<'a, T: Copy + Default> Array3<'a, T> { impl<'a, T: Copy + Default> Array3<'a, T> {
pub fn get(&self, idx: UVec3) -> Optional<T> { pub fn get(&self, idx: UVec3Std) -> Optional<T> {
let idx = self.index(idx); let idx = self.index(idx);
if idx.is_some() { if idx.is_some() {
Optional::some(unsafe { Optional::some(unsafe {
@@ -347,7 +327,7 @@ impl<'a, T: Copy + Default> Array3<'a, T> {
} }
// impl<'a, T> 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 { // OptionalRef::some(unsafe {
// self.data.index(0) // self.data.index(0)
// }) // })
@@ -364,7 +344,7 @@ impl<'a, T: Copy + Default> Array3<'a, T> {
// } // }
// impl<'a> Array3<'a, Vec3Std> { // 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); // let flat_idx = self.index(idx);
// (self.data[0].0, 0.0, 0.0) // (self.data[0].0, 0.0, 0.0)
// // let idx = self.index(idx); // // 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. /// 3d dynamically-sized array backed by a mutably borrowed buffer.
pub struct Array3Mut<'a, T> { pub struct Array3Mut<'a, T> {
data: &'a mut UnsizedArray<T>, data: &'a mut UnsizedArray<T>,
dim: UVec3, dim: UVec3Std,
} }
impl<'a, T> Array3Mut<'a, T> { impl<'a, T> Array3Mut<'a, T> {
pub fn new(data: &'a mut UnsizedArray<T>, dim: UVec3) -> Self { pub fn new(data: &'a mut UnsizedArray<T>, dim: UVec3Std) -> Self {
Self { Self {
data, data,
dim, dim,
@@ -395,8 +375,8 @@ impl<'a, T> Array3Mut<'a, T> {
// Array3::new(self.data, self.dim) // Array3::new(self.data, self.dim)
// } // }
pub fn index(&self, idx: UVec3) -> Optional<usize> { pub fn index(&self, idx: UVec3Std) -> Optional<usize> {
if idx.x < self.dim.x && idx.y < self.dim.y && idx.z < self.dim.z { if idx.x() < self.dim.x() && idx.y() < self.dim.y() && idx.z() < self.dim.z() {
let flat_idx = index(idx, self.dim); let flat_idx = index(idx, self.dim);
Optional::some(flat_idx) Optional::some(flat_idx)
} else { } else {
@@ -406,19 +386,19 @@ impl<'a, T> Array3Mut<'a, T> {
} }
impl<'a, T: Copy> 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(); let idx = self.index(idx).unwrap();
unsafe { unsafe {
self.data.get_handle_mut(idx) self.data.get_handle_mut(idx)
} }
} }
// fn index(&self, idx: UVec3) -> Optional<usize> { // fn index(&self, idx: UVec3Std) -> Optional<usize> {
// self.as_array3().index(idx) // self.as_array3().index(idx)
// } // }
// // pub fn get(&self, idx: UVec3) -> Option<T> { // // pub fn get(&self, idx: UVec3Std) -> Option<T> {
// // self.get_ref(idx).cloned() // // 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); // let idx = self.index(idx);
// if idx.is_some() { // if idx.is_some() {
// OptionalRef::some(&self.data[idx.unwrap()]) // OptionalRef::some(&self.data[idx.unwrap()])
@@ -426,15 +406,15 @@ impl<'a, T: Copy> Array3Mut<'a, T> {
// OptionalRef::none() // OptionalRef::none()
// } // }
// } // }
// pub fn get_mut(&mut self, idx: UVec3) -> Option<T> { // pub fn get_mut(&mut self, idx: UVec3Std) -> Option<T> {
// self.get_ref_mut(idx).cloned() // 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)) // self.data.get_mut(index(idx, self.dim))
// } // }
// /// Like `get_ref_mut`, but imposes fewer lifetime requirements by consuming `self`. // /// 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)) // self.data.get_mut(index(idx, self.dim))
// } // }
} }
@@ -445,16 +425,16 @@ mod test {
use super::*; use super::*;
#[test] #[test]
fn test_index() { fn test_index() {
let dim = UVec3::new(2, 3, 7); let dim = UVec3Std::new(2, 3, 7);
assert_eq!(index(UVec3::new(0, 0, 0), dim), 0); assert_eq!(index(UVec3Std::new(0, 0, 0), dim), 0);
assert_eq!(index(UVec3::new(1, 0, 0), dim), 1); assert_eq!(index(UVec3Std::new(1, 0, 0), dim), 1);
assert_eq!(index(UVec3::new(0, 1, 0), dim), 2); assert_eq!(index(UVec3Std::new(0, 1, 0), dim), 2);
assert_eq!(index(UVec3::new(1, 1, 0), dim), 3); assert_eq!(index(UVec3Std::new(1, 1, 0), dim), 3);
assert_eq!(index(UVec3::new(0, 2, 0), dim), 4); assert_eq!(index(UVec3Std::new(0, 2, 0), dim), 4);
assert_eq!(index(UVec3::new(0, 0, 1), dim), 6); assert_eq!(index(UVec3Std::new(0, 0, 1), dim), 6);
assert_eq!(index(UVec3::new(1, 0, 1), dim), 7); assert_eq!(index(UVec3Std::new(1, 0, 1), dim), 7);
assert_eq!(index(UVec3::new(0, 1, 1), dim), 8); assert_eq!(index(UVec3Std::new(0, 1, 1), dim), 8);
assert_eq!(index(UVec3::new(1, 2, 1), dim), 11); assert_eq!(index(UVec3Std::new(1, 2, 1), dim), 11);
assert_eq!(index(UVec3::new(1, 2, 2), dim), 17); assert_eq!(index(UVec3Std::new(1, 2, 2), dim), 17);
} }
} }

View File

@@ -34,6 +34,15 @@ impl Vec3u {
pub fn z(&self) -> u32 { pub fn z(&self) -> u32 {
self.z 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 /// Convert for use with ndarray indexing
pub fn row_major_idx(&self) -> [usize; 3] { pub fn row_major_idx(&self) -> [usize; 3] {
[self.z as _, self.y as _, self.x as _] [self.z as _, self.y as _, self.x as _]