diff --git a/crates/coremem/src/sim/spirv/bindings.rs b/crates/coremem/src/sim/spirv/bindings.rs index c9cabee..b1026d8 100644 --- a/crates/coremem/src/sim/spirv/bindings.rs +++ b/crates/coremem/src/sim/spirv/bindings.rs @@ -10,12 +10,9 @@ mod ffi { pub use spirv_backend::entry_points; pub use spirv_backend::sim::SerializedSimMeta; pub use spirv_backend::support::Optional; - pub use spirv_backend::mat::{Ferroxcube3R1MH, FullyGenericMaterial, IsoConductorOr, Material, MBPgram, MHPgram}; + pub use spirv_backend::mat::{Ferroxcube3R1MH, FullyGenericMaterial, IsoConductorOr, MBPgram, MHPgram}; } -// ffi Material trait uses only shared types: no conversion needed -pub use ffi::Material; - // conversion traits for types defined cross-lib pub trait IntoFfi { type Ffi; diff --git a/crates/coremem/src/sim/spirv/mod.rs b/crates/coremem/src/sim/spirv/mod.rs index 70126d5..0a771a6 100644 --- a/crates/coremem/src/sim/spirv/mod.rs +++ b/crates/coremem/src/sim/spirv/mod.rs @@ -12,9 +12,10 @@ use crate::geom::{Coord, Index, Meters, Vec3}; use crate::real::Real as _; use crate::sim::{CellStateWithM, GenericSim, MaterialSim, Sample, SampleableSim}; use crate::stim::AbstractStimulus; +use coremem_types::mat::Material; mod bindings; -pub use bindings::{entry_points, IntoFfi, IntoLib, IsoConductorOr, FullyGenericMaterial, Remote, SimMeta, Material}; +pub use bindings::{entry_points, IntoFfi, IntoLib, IsoConductorOr, FullyGenericMaterial, Remote, SimMeta}; /// Wrapper around an inner state object which offloads stepping onto a spirv backend (e.g. GPU). #[derive(Clone, Default, Serialize, Deserialize)] @@ -75,7 +76,7 @@ impl Default for WgpuData { impl MaterialSim for SpirvSim where - M::Ffi: Send + Sync + Material + Clone + IntoLib + 'static + M::Ffi: Send + Sync + Material + Clone + IntoLib + 'static { type Material = M; @@ -94,7 +95,7 @@ where impl SampleableSim for SpirvSim where - M::Ffi: Send + Sync + Material + M::Ffi: Send + Sync + Material { fn sample(&self, pos: Meters) -> Sample { // TODO: smarter sampling than nearest neighbor? @@ -129,7 +130,7 @@ where impl GenericSim for SpirvSim where - M::Ffi: Send + Sync + Material + 'static + M::Ffi: Send + Sync + Material + 'static { fn step_multiple(&mut self, num_steps: u32, s: &S) { self.step_spirv(num_steps, s); @@ -210,7 +211,7 @@ impl SpirvSim { impl SpirvSim where - M::Ffi: Send + Sync + Material + 'static + M::Ffi: Send + Sync + Material + 'static { #[allow(unused)] // used for test fn apply_stimulus(&mut self, stim: &dyn AbstractStimulus) { diff --git a/crates/spirv_backend/src/lib.rs b/crates/spirv_backend/src/lib.rs index 6e11d62..e5da4c3 100644 --- a/crates/spirv_backend/src/lib.rs +++ b/crates/spirv_backend/src/lib.rs @@ -19,8 +19,9 @@ pub mod support; pub use sim::{SerializedSimMeta, SerializedStepE, SerializedStepH}; pub use support::{Optional, UnsizedArray}; -use mat::{IsoConductorOr, Ferroxcube3R1MH, FullyGenericMaterial, Material}; +use mat::{IsoConductorOr, Ferroxcube3R1MH, FullyGenericMaterial}; +use coremem_types::mat::Material; use coremem_types::vec::{Vec3, Vec3u}; type Iso3R1 = IsoConductorOr; @@ -29,7 +30,7 @@ fn glam_vec_to_internal(v: glam::UVec3) -> Vec3u { Vec3u::new(v.x, v.y, v.z) } -fn step_h( +fn step_h>( id: Vec3u, meta: &SerializedSimMeta, stimulus_h: &UnsizedArray>, @@ -45,7 +46,7 @@ fn step_h( } } -fn step_e( +fn step_e>( id: Vec3u, meta: &SerializedSimMeta, stimulus_e: &UnsizedArray>, diff --git a/crates/spirv_backend/src/mat.rs b/crates/spirv_backend/src/mat.rs index f657d8f..11edebd 100644 --- a/crates/spirv_backend/src/mat.rs +++ b/crates/spirv_backend/src/mat.rs @@ -1,30 +1,19 @@ use crate::support::Optional; +use coremem_types::mat::Material; use coremem_types::vec::Vec3; -pub trait Material: Sized { - fn conductivity(&self) -> Vec3 { - Default::default() - } - /// returns the new M vector for this material - fn move_b_vec(&self, m: Vec3, _target_b: Vec3) -> Vec3 { - // XXX could return either 0, or `m`. they should be the same, but one might be more - // optimizable than the other (untested). - m - } -} - #[derive(Copy, Clone, Default, PartialEq)] pub struct Conductor(pub T); pub type AnisomorphicConductor = Conductor>; pub type IsomorphicConductor = Conductor; -impl Material for AnisomorphicConductor { +impl Material for AnisomorphicConductor { fn conductivity(&self) -> Vec3 { self.0 } } -impl Material for IsomorphicConductor { +impl Material for IsomorphicConductor { fn conductivity(&self) -> Vec3 { Vec3::uniform(self.0) } @@ -79,7 +68,7 @@ impl MBPgram { } } -impl Material for MBPgram { +impl Material for MBPgram { fn move_b_vec(&self, m: Vec3, target_b: Vec3) -> Vec3 { Vec3::new( self.move_b(m.x(), target_b.x()), @@ -167,7 +156,7 @@ impl MHPgram { } } -impl Material for MHPgram { +impl Material for MHPgram { fn move_b_vec(&self, m: Vec3, target_b: Vec3) -> Vec3 { Vec3::new( self.move_b(m.x(), target_b.x()), @@ -184,7 +173,7 @@ pub struct FullyGenericMaterial { pub m_h_curve: Optional, } -impl Material for FullyGenericMaterial { +impl Material for FullyGenericMaterial { fn conductivity(&self) -> Vec3 { self.conductivity } @@ -210,7 +199,7 @@ impl Into for Ferroxcube3R1MH { } } -impl Material for Ferroxcube3R1MH { +impl Material for Ferroxcube3R1MH { fn move_b_vec(&self, m: Vec3, target_b: Vec3) -> Vec3 { let curve: MHPgram = (*self).into(); curve.move_b_vec(m, target_b) @@ -241,7 +230,7 @@ impl IsoConductorOr { } } -impl Material for IsoConductorOr { +impl + Copy> Material for IsoConductorOr { fn conductivity(&self) -> Vec3 { Vec3::uniform(self.value.max(0.0)) } diff --git a/crates/spirv_backend/src/sim.rs b/crates/spirv_backend/src/sim.rs index 8e169e5..17c9a28 100644 --- a/crates/spirv_backend/src/sim.rs +++ b/crates/spirv_backend/src/sim.rs @@ -1,8 +1,8 @@ // use spirv_std::RuntimeArray; -use crate::mat::Material; use crate::support::{ Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray }; +use coremem_types::mat::Material; use coremem_types::vec::{Vec3, Vec3u}; #[derive(Copy, Clone)] @@ -278,7 +278,7 @@ pub struct StepEContext<'a, M> { out_e: ArrayHandleMut<'a, Vec3>, } -impl<'a, M: Material> StepEContext<'a, M> { +impl<'a, M: Material> StepEContext<'a, M> { pub fn step_e(mut self) { // const EPS0_INV: f32 = 112940906737.1361; const TWICE_EPS0: f32 = 1.7708375625626e-11; @@ -311,7 +311,7 @@ pub struct StepHContext<'a, M> { out_m: ArrayHandleMut<'a, Vec3>, } -impl<'a, M: Material> StepHContext<'a, M> { +impl<'a, M: Material> StepHContext<'a, M> { pub fn step_h(mut self) { const MU0: f32 = 1.2566370621219e-06; const MU0_INV: f32 = 795774.715025073; diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index ea01173..f2e1c48 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -1,6 +1,7 @@ #![no_std] #![feature(core_intrinsics)] +pub mod mat; pub mod real; pub mod vec; mod vecu; diff --git a/crates/types/src/mat.rs b/crates/types/src/mat.rs new file mode 100644 index 0000000..cbda4a0 --- /dev/null +++ b/crates/types/src/mat.rs @@ -0,0 +1,15 @@ +use crate::real::Real; +use crate::vec::Vec3; + +pub trait Material: Sized { + fn conductivity(&self) -> Vec3 { + Default::default() + } + /// returns the new M vector for this material + fn move_b_vec(&self, m: Vec3, _target_b: Vec3) -> Vec3 { + // XXX could return either 0, or `m`. they should be the same, but one might be more + // optimizable than the other (untested). + m + } +} +