spirv: migrate Material trait to types, and parameterize by R

This commit is contained in:
2022-07-18 14:33:09 -07:00
parent 35251f5a7f
commit 7d2a3baadc
7 changed files with 38 additions and 34 deletions

View File

@@ -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;

View File

@@ -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<M: IntoFfi + PartialEq> MaterialSim for SpirvSim<M>
where
M::Ffi: Send + Sync + Material + Clone + IntoLib<Lib=M> + 'static
M::Ffi: Send + Sync + Material<f32> + Clone + IntoLib<Lib=M> + 'static
{
type Material = M;
@@ -94,7 +95,7 @@ where
impl<M: IntoFfi> SampleableSim for SpirvSim<M>
where
M::Ffi: Send + Sync + Material
M::Ffi: Send + Sync + Material<f32>
{
fn sample(&self, pos: Meters) -> Sample {
// TODO: smarter sampling than nearest neighbor?
@@ -129,7 +130,7 @@ where
impl<M: IntoFfi> GenericSim for SpirvSim<M>
where
M::Ffi: Send + Sync + Material + 'static
M::Ffi: Send + Sync + Material<f32> + 'static
{
fn step_multiple<S: AbstractStimulus>(&mut self, num_steps: u32, s: &S) {
self.step_spirv(num_steps, s);
@@ -210,7 +211,7 @@ impl<M: IntoFfi> SpirvSim<M> {
impl<M: IntoFfi> SpirvSim<M>
where
M::Ffi: Send + Sync + Material + 'static
M::Ffi: Send + Sync + Material<f32> + 'static
{
#[allow(unused)] // used for test
fn apply_stimulus(&mut self, stim: &dyn AbstractStimulus) {

View File

@@ -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<Ferroxcube3R1MH>;
@@ -29,7 +30,7 @@ fn glam_vec_to_internal(v: glam::UVec3) -> Vec3u {
Vec3u::new(v.x, v.y, v.z)
}
fn step_h<M: Material>(
fn step_h<M: Material<f32>>(
id: Vec3u,
meta: &SerializedSimMeta,
stimulus_h: &UnsizedArray<Vec3<f32>>,
@@ -45,7 +46,7 @@ fn step_h<M: Material>(
}
}
fn step_e<M: Material>(
fn step_e<M: Material<f32>>(
id: Vec3u,
meta: &SerializedSimMeta,
stimulus_e: &UnsizedArray<Vec3<f32>>,

View File

@@ -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<f32> {
Default::default()
}
/// returns the new M vector for this material
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
}
}
#[derive(Copy, Clone, Default, PartialEq)]
pub struct Conductor<T>(pub T);
pub type AnisomorphicConductor = Conductor<Vec3<f32>>;
pub type IsomorphicConductor = Conductor<f32>;
impl Material for AnisomorphicConductor {
impl Material<f32> for AnisomorphicConductor {
fn conductivity(&self) -> Vec3<f32> {
self.0
}
}
impl Material for IsomorphicConductor {
impl Material<f32> for IsomorphicConductor {
fn conductivity(&self) -> Vec3<f32> {
Vec3::uniform(self.0)
}
@@ -79,7 +68,7 @@ impl MBPgram {
}
}
impl Material for MBPgram {
impl Material<f32> for MBPgram {
fn move_b_vec(&self, m: Vec3<f32>, target_b: Vec3<f32>) -> Vec3<f32> {
Vec3::new(
self.move_b(m.x(), target_b.x()),
@@ -167,7 +156,7 @@ impl MHPgram {
}
}
impl Material for MHPgram {
impl Material<f32> for MHPgram {
fn move_b_vec(&self, m: Vec3<f32>, target_b: Vec3<f32>) -> Vec3<f32> {
Vec3::new(
self.move_b(m.x(), target_b.x()),
@@ -184,7 +173,7 @@ pub struct FullyGenericMaterial {
pub m_h_curve: Optional<MHPgram>,
}
impl Material for FullyGenericMaterial {
impl Material<f32> for FullyGenericMaterial {
fn conductivity(&self) -> Vec3<f32> {
self.conductivity
}
@@ -210,7 +199,7 @@ impl Into<MHPgram> for Ferroxcube3R1MH {
}
}
impl Material for Ferroxcube3R1MH {
impl Material<f32> for Ferroxcube3R1MH {
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,7 +230,7 @@ impl<M: Default> IsoConductorOr<M> {
}
}
impl<M: Material + Copy> Material for IsoConductorOr<M> {
impl<M: Material<f32> + Copy> Material<f32> for IsoConductorOr<M> {
fn conductivity(&self) -> Vec3<f32> {
Vec3::uniform(self.value.max(0.0))
}

View File

@@ -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<f32>>,
}
impl<'a, M: Material> StepEContext<'a, M> {
impl<'a, M: Material<f32>> 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<f32>>,
}
impl<'a, M: Material> StepHContext<'a, M> {
impl<'a, M: Material<f32>> StepHContext<'a, M> {
pub fn step_h(mut self) {
const MU0: f32 = 1.2566370621219e-06;
const MU0_INV: f32 = 795774.715025073;

View File

@@ -1,6 +1,7 @@
#![no_std]
#![feature(core_intrinsics)]
pub mod mat;
pub mod real;
pub mod vec;
mod vecu;

15
crates/types/src/mat.rs Normal file
View File

@@ -0,0 +1,15 @@
use crate::real::Real;
use crate::vec::Vec3;
pub trait Material<R: Real>: Sized {
fn conductivity(&self) -> Vec3<R> {
Default::default()
}
/// returns the new M vector for this material
fn move_b_vec(&self, m: Vec3<R>, _target_b: Vec3<R>) -> Vec3<R> {
// XXX could return either 0, or `m`. they should be the same, but one might be more
// optimizable than the other (untested).
m
}
}