spirv_backend: remove Vec3Std and use coremem_types::Vec3 everywhere

This commit is contained in:
2022-07-18 13:51:11 -07:00
parent d7d8be62d1
commit 19f00c9076
6 changed files with 80 additions and 94 deletions

View File

@@ -8,7 +8,7 @@
extern crate spirv_std;
pub use spirv_std::glam::{UVec3, Vec3};
pub use spirv_std::glam;
#[cfg(not(target_arch = "spirv"))]
use spirv_std::macros::spirv;
@@ -17,26 +17,27 @@ pub mod sim;
pub mod support;
pub use sim::{SerializedSimMeta, SerializedStepE, SerializedStepH};
pub use support::{Optional, UnsizedArray, Vec3Std};
pub use support::{Optional, UnsizedArray};
use mat::{IsoConductorOr, Ferroxcube3R1MH, FullyGenericMaterial, Material};
use coremem_types::vecu::Vec3u;
use coremem_types::vec::Vec3;
type Iso3R1 = IsoConductorOr<Ferroxcube3R1MH>;
fn glam_vec_to_internal(v: UVec3) -> Vec3u {
fn glam_vec_to_internal(v: glam::UVec3) -> Vec3u {
Vec3u::new(v.x, v.y, v.z)
}
fn step_h<M: Material>(
id: Vec3u,
meta: &SerializedSimMeta,
stimulus_h: &UnsizedArray<Vec3Std>,
stimulus_h: &UnsizedArray<Vec3<f32>>,
material: &UnsizedArray<M>,
e: &UnsizedArray<Vec3Std>,
h: &mut UnsizedArray<Vec3Std>,
m: &mut UnsizedArray<Vec3Std>,
e: &UnsizedArray<Vec3<f32>>,
h: &mut UnsizedArray<Vec3<f32>>,
m: &mut UnsizedArray<Vec3<f32>>,
) {
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);
@@ -48,10 +49,10 @@ fn step_h<M: Material>(
fn step_e<M: Material>(
id: Vec3u,
meta: &SerializedSimMeta,
stimulus_e: &UnsizedArray<Vec3Std>,
stimulus_e: &UnsizedArray<Vec3<f32>>,
material: &UnsizedArray<M>,
e: &mut UnsizedArray<Vec3Std>,
h: &UnsizedArray<Vec3Std>,
e: &mut UnsizedArray<Vec3<f32>>,
h: &UnsizedArray<Vec3<f32>>,
) {
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);
@@ -86,31 +87,31 @@ macro_rules! steps {
// LocalSize/numthreads
#[spirv(compute(threads(4, 4, 4)))]
pub fn $step_h(
#[spirv(global_invocation_id)] id: UVec3,
#[spirv(global_invocation_id)] id: glam::UVec3,
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] meta: &SerializedSimMeta,
// XXX: delete this input?
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] _unused_stimulus_e: &UnsizedArray<Vec3Std>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 2)] stimulus_h: &UnsizedArray<Vec3Std>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] _unused_stimulus_e: &UnsizedArray<Vec3<f32>>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 2)] stimulus_h: &UnsizedArray<Vec3<f32>>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 3)] material: &UnsizedArray<$mat>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 4)] e: &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 = 4)] e: &UnsizedArray<Vec3<f32>>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 5)] h: &mut UnsizedArray<Vec3<f32>>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 6)] m: &mut UnsizedArray<Vec3<f32>>,
) {
step_h(glam_vec_to_internal(id), meta, stimulus_h, material, e, h, m)
}
#[spirv(compute(threads(4, 4, 4)))]
pub fn $step_e(
#[spirv(global_invocation_id)] id: UVec3,
#[spirv(global_invocation_id)] id: glam::UVec3,
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] meta: &SerializedSimMeta,
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] stimulus_e: &UnsizedArray<Vec3Std>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] stimulus_e: &UnsizedArray<Vec3<f32>>,
// XXX: delete this input?
#[spirv(storage_buffer, descriptor_set = 0, binding = 2)] _unused_stimulus_h: &UnsizedArray<Vec3Std>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 2)] _unused_stimulus_h: &UnsizedArray<Vec3<f32>>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 3)] material: &UnsizedArray<$mat>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 4)] e: &mut UnsizedArray<Vec3Std>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 5)] h: &UnsizedArray<Vec3Std>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 4)] e: &mut UnsizedArray<Vec3<f32>>,
#[spirv(storage_buffer, descriptor_set = 0, binding = 5)] h: &UnsizedArray<Vec3<f32>>,
// 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<Vec3<f32>>,
) {
step_e(glam_vec_to_internal(id), meta, stimulus_e, material, e, h)
}

View File

@@ -1,11 +1,12 @@
use crate::support::{Optional, Vec3Std};
use crate::support::Optional;
use coremem_types::vec::Vec3;
pub trait Material: Sized {
fn conductivity(&self) -> Vec3Std {
fn conductivity(&self) -> Vec3<f32> {
Default::default()
}
/// returns the new M vector for this material
fn move_b_vec(&self, m: Vec3Std, _target_b: Vec3Std) -> Vec3Std {
fn move_b_vec(&self, m: Vec3<f32>, _target_b: Vec3<f32>) -> Vec3<f32> {
// XXX could return either 0, or `m`. they should be the same, but one might be more
// optimizable than the other (untested).
m
@@ -14,18 +15,18 @@ pub trait Material: Sized {
#[derive(Copy, Clone, Default, PartialEq)]
pub struct Conductor<T>(pub T);
pub type AnisomorphicConductor = Conductor<Vec3Std>;
pub type AnisomorphicConductor = Conductor<Vec3<f32>>;
pub type IsomorphicConductor = Conductor<f32>;
impl Material for AnisomorphicConductor {
fn conductivity(&self) -> Vec3Std {
fn conductivity(&self) -> Vec3<f32> {
self.0
}
}
impl Material for IsomorphicConductor {
fn conductivity(&self) -> Vec3Std {
Vec3Std::uniform(self.0)
fn conductivity(&self) -> Vec3<f32> {
Vec3::uniform(self.0)
}
}
@@ -79,8 +80,8 @@ impl MBPgram {
}
impl Material for MBPgram {
fn move_b_vec(&self, m: Vec3Std, target_b: Vec3Std) -> Vec3Std {
Vec3Std::new(
fn move_b_vec(&self, m: Vec3<f32>, target_b: Vec3<f32>) -> Vec3<f32> {
Vec3::new(
self.move_b(m.x(), target_b.x()),
self.move_b(m.y(), target_b.y()),
self.move_b(m.z(), target_b.z()),
@@ -167,8 +168,8 @@ impl MHPgram {
}
impl Material for MHPgram {
fn move_b_vec(&self, m: Vec3Std, target_b: Vec3Std) -> Vec3Std {
Vec3Std::new(
fn move_b_vec(&self, m: Vec3<f32>, target_b: Vec3<f32>) -> Vec3<f32> {
Vec3::new(
self.move_b(m.x(), target_b.x()),
self.move_b(m.y(), target_b.y()),
self.move_b(m.z(), target_b.z()),
@@ -178,16 +179,16 @@ impl Material for MHPgram {
#[derive(Copy, Clone, Default, PartialEq)]
pub struct FullyGenericMaterial {
pub conductivity: Vec3Std,
pub conductivity: Vec3<f32>,
pub m_b_curve: Optional<MBPgram>,
pub m_h_curve: Optional<MHPgram>,
}
impl Material for FullyGenericMaterial {
fn conductivity(&self) -> Vec3Std {
fn conductivity(&self) -> Vec3<f32> {
self.conductivity
}
fn move_b_vec(&self, m: Vec3Std, target_b: Vec3Std) -> Vec3Std {
fn move_b_vec(&self, m: Vec3<f32>, target_b: Vec3<f32>) -> Vec3<f32> {
if self.m_b_curve.is_some() {
self.m_b_curve.unwrap().move_b_vec(m, target_b)
} else if self.m_h_curve.is_some() {
@@ -210,7 +211,7 @@ impl Into<MHPgram> for Ferroxcube3R1MH {
}
impl Material for Ferroxcube3R1MH {
fn move_b_vec(&self, m: Vec3Std, target_b: Vec3Std) -> Vec3Std {
fn move_b_vec(&self, m: Vec3<f32>, target_b: Vec3<f32>) -> Vec3<f32> {
let curve: MHPgram = (*self).into();
curve.move_b_vec(m, target_b)
}
@@ -241,10 +242,10 @@ impl<M: Default> IsoConductorOr<M> {
}
impl<M: Material + Copy> Material for IsoConductorOr<M> {
fn conductivity(&self) -> Vec3Std {
Vec3Std::uniform(self.value.max(0.0))
fn conductivity(&self) -> Vec3<f32> {
Vec3::uniform(self.value.max(0.0))
}
fn move_b_vec(&self, m: Vec3Std, target_b: Vec3Std) -> Vec3Std {
fn move_b_vec(&self, m: Vec3<f32>, target_b: Vec3<f32>) -> Vec3<f32> {
if self.value < 0.0 {
let mat = self.mat; //< XXX hack for ZST
mat.move_b_vec(m, target_b)

View File

@@ -1,8 +1,9 @@
// use spirv_std::RuntimeArray;
use crate::mat::Material;
use crate::support::{
Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray, Vec3Std
Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray
};
use coremem_types::vec::Vec3;
use coremem_types::vecu::Vec3u;
#[derive(Copy, Clone)]
@@ -17,21 +18,21 @@ pub struct SerializedSimMeta {
/// Whatever data we received from the host in their call to step_h
pub struct SerializedStepH<'a, M> {
meta: &'a SerializedSimMeta,
stimulus_h: &'a UnsizedArray<Vec3Std>,
stimulus_h: &'a UnsizedArray<Vec3<f32>>,
material: &'a UnsizedArray<M>,
e: &'a UnsizedArray<Vec3Std>,
h: &'a mut UnsizedArray<Vec3Std>,
m: &'a mut UnsizedArray<Vec3Std>,
e: &'a UnsizedArray<Vec3<f32>>,
h: &'a mut UnsizedArray<Vec3<f32>>,
m: &'a mut UnsizedArray<Vec3<f32>>,
}
impl<'a, M> SerializedStepH<'a, M> {
pub fn new(
meta: &'a SerializedSimMeta,
stimulus_h: &'a UnsizedArray<Vec3Std>,
stimulus_h: &'a UnsizedArray<Vec3<f32>>,
material: &'a UnsizedArray<M>,
e: &'a UnsizedArray<Vec3Std>,
h: &'a mut UnsizedArray<Vec3Std>,
m: &'a mut UnsizedArray<Vec3Std>,
e: &'a UnsizedArray<Vec3<f32>>,
h: &'a mut UnsizedArray<Vec3<f32>>,
m: &'a mut UnsizedArray<Vec3<f32>>,
) -> Self {
Self {
meta, stimulus_h, material, e, h, m
@@ -72,19 +73,19 @@ impl<'a, M> SerializedStepH<'a, M> {
/// Whatever data we received from the host in their call to step_e
pub struct SerializedStepE<'a, M> {
meta: &'a SerializedSimMeta,
stimulus_e: &'a UnsizedArray<Vec3Std>,
stimulus_e: &'a UnsizedArray<Vec3<f32>>,
material: &'a UnsizedArray<M>,
e: &'a mut UnsizedArray<Vec3Std>,
h: &'a UnsizedArray<Vec3Std>,
e: &'a mut UnsizedArray<Vec3<f32>>,
h: &'a UnsizedArray<Vec3<f32>>,
}
impl<'a, M> SerializedStepE<'a, M> {
pub fn new(
meta: &'a SerializedSimMeta,
stimulus_e: &'a UnsizedArray<Vec3Std>,
stimulus_e: &'a UnsizedArray<Vec3<f32>>,
material: &'a UnsizedArray<M>,
e: &'a mut UnsizedArray<Vec3Std>,
h: &'a UnsizedArray<Vec3Std>,
e: &'a mut UnsizedArray<Vec3<f32>>,
h: &'a UnsizedArray<Vec3<f32>>,
) -> Self {
Self {
meta, stimulus_e, material, e, h
@@ -140,10 +141,10 @@ impl<'a, M> SerializedStepE<'a, M> {
/// This is used in step_e when looking at the H field deltas.
#[derive(Copy, Clone)]
struct VolumeSampleNeg {
mid: Vec3Std,
xm1: Optional<Vec3Std>,
ym1: Optional<Vec3Std>,
zm1: Optional<Vec3Std>,
mid: Vec3<f32>,
xm1: Optional<Vec3<f32>>,
ym1: Optional<Vec3<f32>>,
zm1: Optional<Vec3<f32>>,
}
impl VolumeSampleNeg {
@@ -197,10 +198,10 @@ impl VolumeSampleNeg {
/// This is used in step_h when looking at the E field deltas.
#[derive(Copy, Clone)]
struct VolumeSamplePos {
mid: Vec3Std,
xp1: Optional<Vec3Std>,
yp1: Optional<Vec3Std>,
zp1: Optional<Vec3Std>
mid: Vec3<f32>,
xp1: Optional<Vec3<f32>>,
yp1: Optional<Vec3<f32>>,
zp1: Optional<Vec3<f32>>
}
impl VolumeSamplePos {
@@ -258,8 +259,8 @@ struct FieldDeltas {
}
impl FieldDeltas {
fn nabla(self) -> Vec3Std {
Vec3Std::new(
fn nabla(self) -> Vec3<f32> {
Vec3::new(
self.dfz_dy - self.dfy_dz,
self.dfx_dz - self.dfz_dx,
self.dfy_dx - self.dfx_dy,
@@ -270,12 +271,12 @@ impl FieldDeltas {
pub struct StepEContext<'a, M> {
inv_feature_size: f32,
time_step: f32,
stim_e: Vec3Std,
stim_e: Vec3<f32>,
mat: ArrayHandle<'a, M>,
/// Input field sampled near this location
in_h: VolumeSampleNeg,
/// Handle to the output field at one specific index.
out_e: ArrayHandleMut<'a, Vec3Std>,
out_e: ArrayHandleMut<'a, Vec3<f32>>,
}
impl<'a, M: Material> StepEContext<'a, M> {
@@ -291,7 +292,7 @@ impl<'a, M: Material> StepEContext<'a, M> {
let sigma = self.mat.get_ref().conductivity();
let e_prev = self.out_e.get();
let delta_e = (nabla_h - e_prev.elem_mul(sigma)).elem_div(
sigma*self.time_step + Vec3Std::uniform(TWICE_EPS0)
sigma*self.time_step + Vec3::uniform(TWICE_EPS0)
)*(2.0*self.time_step);
// println!("spirv-step_e delta_e: {:?}", delta_e);
self.out_e.write(e_prev + delta_e + self.stim_e);
@@ -302,13 +303,13 @@ impl<'a, M: Material> StepEContext<'a, M> {
pub struct StepHContext<'a, M> {
inv_feature_size: f32,
time_step: f32,
stim_h: Vec3Std,
stim_h: Vec3<f32>,
mat: ArrayHandle<'a, M>,
/// Input field sampled near this location
in_e: VolumeSamplePos,
/// Handle to the output field at one specific index.
out_h: ArrayHandleMut<'a, Vec3Std>,
out_m: ArrayHandleMut<'a, Vec3Std>,
out_h: ArrayHandleMut<'a, Vec3<f32>>,
out_m: ArrayHandleMut<'a, Vec3<f32>>,
}
impl<'a, M: Material> StepHContext<'a, M> {

View File

@@ -1,11 +1,5 @@
use coremem_types::vec;
use coremem_types::vecu::Vec3u;
#[derive(Clone, Copy, Default, PartialEq)]
pub struct XYZStd<T>(T, T, T);
pub type Vec3Std = vec::Vec3<f32>;
/// This is a spirv-compatible option type.
/// The native rust Option type produces invalid spirv due to its enum nature; this custom option
/// type creates code which will actually compile.