coremem_types: hide Serialize/Deserialize behind a serde feature flag

- this flag is disabled in spirv_backend.
- spirv_backend currently errors in core::fmt stuff. may try hiding fmt
  behind a feature_flag.
This commit is contained in:
2022-07-17 23:44:14 -07:00
parent 19849c2428
commit 97c2813c9d
11 changed files with 97 additions and 124 deletions

1
Cargo.lock generated
View File

@@ -2095,6 +2095,7 @@ source = "git+https://github.com/EmbarkStudios/rust-gpu#0866cf591a7fdbbd15bdb346
name = "spirv_backend" name = "spirv_backend"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"coremem_types",
"spirv-std", "spirv-std",
] ]

View File

@@ -47,7 +47,7 @@ spirv-std = { git = "https://github.com/EmbarkStudios/rust-gpu" }
spirv-std-macros = { git = "https://github.com/EmbarkStudios/rust-gpu" } spirv-std-macros = { git = "https://github.com/EmbarkStudios/rust-gpu" }
spirv_backend = { path = "../spirv_backend" } spirv_backend = { path = "../spirv_backend" }
spirv_backend_runner = { path = "../spirv_backend_runner" } spirv_backend_runner = { path = "../spirv_backend_runner" }
coremem_types = { path = "../types" } coremem_types = { path = "../types", features = ["serde"] }
[dev-dependencies] [dev-dependencies]

View File

@@ -229,7 +229,8 @@ impl Wrap {
fn map(&self, p: Meters) -> Meters { fn map(&self, p: Meters) -> Meters {
let p_rel = p - self.about; let p_rel = p - self.about;
let xy = p_rel.xy(); let xy = p_rel.xy();
let rev = xy.arg() / std::f32::consts::PI * self.y_max; let arg = xy.y().atan2(xy.x());
let rev = arg / std::f32::consts::PI * self.y_max;
Meters::new(xy.mag() + self.about.x(), rev, p.z()) Meters::new(xy.mag() + self.about.x(), rev, p.z())
} }
} }

View File

@@ -3,8 +3,9 @@ name = "spirv_backend"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies]
spirv-std = { git = "https://github.com/EmbarkStudios/rust-gpu", features = ["glam"] } # MIT or Apache 2.0
[lib] [lib]
crate-type = ["dylib", "lib"] crate-type = ["dylib", "lib"]
[dependencies]
spirv-std = { git = "https://github.com/EmbarkStudios/rust-gpu", features = ["glam"] } # MIT or Apache 2.0
coremem_types = { path = "../types" }

View File

@@ -1,7 +1,9 @@
// use spirv_std::RuntimeArray; // use spirv_std::RuntimeArray;
use spirv_std::glam::UVec3; use spirv_std::glam::UVec3;
use crate::mat::Material; use crate::mat::Material;
use crate::support::{Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray, UVec3Std, Vec3Std}; use crate::support::{
Array3, Array3Mut, ArrayHandle, ArrayHandleMut, Optional, UnsizedArray, UVec3Std, Vec3Std, IntoGlam as _
};
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct SerializedSimMeta { pub struct SerializedSimMeta {
@@ -15,7 +17,7 @@ pub struct SerializedSimMeta {
impl SerializedSimMeta { impl SerializedSimMeta {
fn dim(&self) -> UVec3 { fn dim(&self) -> UVec3 {
self.dim.into() self.dim.into_glam()
} }
} }

View File

@@ -1,4 +1,6 @@
use spirv_std::glam::{self, UVec3}; use spirv_std::glam::{self, UVec3};
use coremem_types::vec;
use coremem_types::vecu;
// pub trait Nullable { // pub trait Nullable {
// fn null() -> *const Self; // fn null() -> *const Self;
@@ -8,73 +10,25 @@ use spirv_std::glam::{self, UVec3};
#[derive(Clone, Copy, Default, PartialEq)] #[derive(Clone, Copy, Default, PartialEq)]
pub struct XYZStd<T>(T, T, T); pub struct XYZStd<T>(T, T, T);
pub type Vec3Std = XYZStd<f32>; pub type Vec3Std = vec::Vec3<f32>;
pub type UVec3Std = XYZStd<u32>; pub type UVec3Std = vecu::Vec3u;
impl<T> XYZStd<T> { pub trait IntoGlam {
pub fn new(x: T, y: T, z: T) -> Self { type Output;
Self(x, y, z) 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<T: Copy> XYZStd<T> { impl IntoGlam for UVec3Std {
pub fn uniform(u: T) -> Self { type Output = glam::UVec3;
Self(u, u, u) fn into_glam(self) -> Self::Output {
} Self::Output::new(self.x(), self.y(), self.z())
pub fn x(self) -> T {
self.0
}
pub fn y(self) -> T {
self.1
}
pub fn z(self) -> T {
self.2
}
}
impl<T: core::ops::Add<T, Output=T>> core::ops::Add<XYZStd<T>> for XYZStd<T> {
type Output = Self;
fn add(self, s: Self) -> Self {
Self::new(self.0 + s.0, self.1 + s.1, self.2 + s.2)
}
}
impl<T: core::ops::Sub<T, Output=T>> core::ops::Sub<XYZStd<T>> for XYZStd<T> {
type Output = Self;
fn sub(self, s: Self) -> Self {
Self::new(self.0 - s.0, self.1 - s.1, self.2 - s.2)
}
}
impl<T: Copy + core::ops::Mul<T, Output=T>> core::ops::Mul<T> for XYZStd<T> {
type Output = Self;
fn mul(self, s: T) -> Self {
Self::new(self.0 * s, self.1 * s, self.2 * s)
}
}
impl Into<glam::Vec3> for Vec3Std {
fn into(self) -> glam::Vec3 {
glam::Vec3::new(self.x(), self.y(), self.z())
}
}
impl Into<glam::UVec3> for UVec3Std {
fn into(self) -> glam::UVec3 {
glam::UVec3::new(self.x(), self.y(), self.z())
}
}
impl<T: core::ops::Mul<T, Output=T>> XYZStd<T> {
pub fn elem_mul(self, other: Self) -> Self {
Self::new(self.0 * other.0, self.1 * other.1, self.2 * other.2)
}
}
impl<T: core::ops::Div<T, Output=T>> XYZStd<T> {
pub fn elem_div(self, other: Self) -> Self {
Self::new(self.0 / other.0, self.1 / other.1, self.2 / other.2)
} }
} }

View File

@@ -4,5 +4,8 @@ version = "0.1.0"
authors = ["Colin <colin@uninsane.org>"] authors = ["Colin <colin@uninsane.org>"]
edition = "2021" edition = "2021"
[features]
serde = [ "dep:serde" ]
[dependencies] [dependencies]
serde = "1.0" # MIT or Apache 2.0 serde = { version = "1.0", optional = true } # MIT or Apache 2.0

View File

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

View File

@@ -1,9 +1,12 @@
use core::fmt; use core::fmt;
use core::intrinsics;
use core::ops::{ use core::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign, Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign,
}; };
use core::cmp::{Eq, Ord, Ordering, PartialOrd}; use core::cmp::{Eq, Ord, Ordering, PartialOrd};
use serde::{Deserialize, Serialize};
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
pub trait ToFloat { pub trait ToFloat {
// TODO: make these by-value // TODO: make these by-value
@@ -69,7 +72,6 @@ pub trait Real:
fn powf(self, p: Self) -> Self; fn powf(self, p: Self) -> Self;
fn sqrt(self) -> Self; fn sqrt(self) -> Self;
fn sin_cos(self) -> (Self, Self); fn sin_cos(self) -> (Self, Self);
fn atan2(self, x: Self) -> Self;
fn min_max_or_undefined(self, other: Self) -> (Self, Self) { fn min_max_or_undefined(self, other: Self) -> (Self, Self) {
match self.partial_cmp(&other) { match self.partial_cmp(&other) {
@@ -117,10 +119,10 @@ pub trait Real:
} }
fn pi() -> Self { fn pi() -> Self {
Self::from_f64(std::f64::consts::PI) Self::from_f64(core::f64::consts::PI)
} }
fn two_pi() -> Self { fn two_pi() -> Self {
Self::from_f64(std::f64::consts::PI * 2.0) Self::from_f64(core::f64::consts::PI * 2.0)
} }
/// Speed of light in a vacuum; m/s. /// Speed of light in a vacuum; m/s.
@@ -158,28 +160,28 @@ impl Real for f32 {
f32::is_finite(self) f32::is_finite(self)
} }
fn floor(self) -> Self { fn floor(self) -> Self {
f32::floor(self) unsafe { intrinsics::floorf32(self) }
} }
fn ceil(self) -> Self { fn ceil(self) -> Self {
f32::ceil(self) unsafe { intrinsics::ceilf32(self) }
} }
fn round(self) -> Self { fn round(self) -> Self {
f32::round(self) unsafe { intrinsics::roundf32(self) }
} }
fn exp(self) -> Self { fn exp(self) -> Self {
f32::exp(self) unsafe { intrinsics::expf32(self) }
}
fn powf(self, p: Self) -> Self {
f32::powf(self, p)
} }
fn sqrt(self) -> Self { fn sqrt(self) -> Self {
f32::sqrt(self) unsafe { intrinsics::sqrtf32(self) }
}
fn powf(self, p: Self) -> Self {
unsafe { intrinsics::powf32(self, p) }
} }
fn sin_cos(self) -> (Self, Self) { fn sin_cos(self) -> (Self, Self) {
f32::sin_cos(self) unsafe {(
} intrinsics::sinf32(self),
fn atan2(self, x: Self) -> Self { intrinsics::cosf32(self),
f32::atan2(self, x) )}
} }
} }
@@ -197,32 +199,33 @@ impl Real for f64 {
f64::is_finite(self) f64::is_finite(self)
} }
fn floor(self) -> Self { fn floor(self) -> Self {
f64::floor(self) unsafe { intrinsics::floorf64(self) }
} }
fn ceil(self) -> Self { fn ceil(self) -> Self {
f64::ceil(self) unsafe { intrinsics::ceilf64(self) }
} }
fn round(self) -> Self { fn round(self) -> Self {
f64::round(self) unsafe { intrinsics::roundf64(self) }
} }
fn exp(self) -> Self { fn exp(self) -> Self {
f64::exp(self) unsafe { intrinsics::expf64(self) }
}
fn powf(self, p: Self) -> Self {
f64::powf(self, p)
} }
fn sqrt(self) -> Self { fn sqrt(self) -> Self {
f64::sqrt(self) unsafe { intrinsics::sqrtf64(self) }
}
fn powf(self, p: Self) -> Self {
unsafe { intrinsics::powf64(self, p) }
} }
fn sin_cos(self) -> (Self, Self) { fn sin_cos(self) -> (Self, Self) {
f64::sin_cos(self) unsafe {(
} intrinsics::sinf64(self),
fn atan2(self, x: Self) -> Self { intrinsics::cosf64(self),
f64::atan2(self, x) )}
} }
} }
#[derive(Default, Copy, Clone, Debug, PartialEq, PartialOrd, Serialize, Deserialize)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Default, Copy, Clone, Debug, PartialEq, PartialOrd)]
pub struct Finite<T>(T); pub struct Finite<T>(T);
pub type R32 = Finite<f32>; pub type R32 = Finite<f32>;
@@ -349,9 +352,6 @@ impl<T: Real> Real for Finite<T> {
fn powf(self, p: Self) -> Self { fn powf(self, p: Self) -> Self {
Self::new(self.0.powf(p.0)) Self::new(self.0.powf(p.0))
} }
fn atan2(self, p: Self) -> Self {
Self::new(self.0.atan2(p.0))
}
fn sin_cos(self) -> (Self, Self) { fn sin_cos(self) -> (Self, Self) {
let (s, c) = self.0.sin_cos(); let (s, c) = self.0.sin_cos();
(Self::new(s), Self::new(c)) (Self::new(s), Self::new(c))

View File

@@ -1,13 +1,16 @@
use crate::real::{Real, ToFloat}; use crate::real::{Real, ToFloat};
use super::vecu::Vec3u; use crate::vecu::Vec3u;
use serde::{Serialize, Deserialize};
use core::convert::From; use core::convert::From;
use core::fmt; use core::fmt;
use core::iter::Sum; use core::iter::Sum;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub}; use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub};
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] #[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Vec2<R=f32> { pub struct Vec2<R=f32> {
pub x: R, pub x: R,
pub y: R, pub y: R,
@@ -128,10 +131,11 @@ impl<R: Real> Vec2<R> {
} }
} }
/// Returns the angle of this point, (-pi, pi] // /// Returns the angle of this point, (-pi, pi]
pub fn arg(&self) -> R { // pub fn arg(&self) -> R {
self.y.atan2(self.x) // // self.y.atan2(self.x)
} // self.y.to_f64().atan2(self.x.to_f64()).cast()
// }
// TODO test // TODO test
pub fn rotate(&self, angle: R) -> Self { pub fn rotate(&self, angle: R) -> Self {
@@ -145,7 +149,8 @@ impl<R: Real> Vec2<R> {
} }
} }
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd, Serialize, Deserialize)] #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)]
pub struct Vec3<R=f32> { pub struct Vec3<R=f32> {
pub(crate) x: R, pub(crate) x: R,
pub(crate) y: R, pub(crate) y: R,
@@ -450,24 +455,24 @@ impl<R: Real> Sum for Vec3<R> {
impl<R: Real> fmt::LowerExp for Vec3<R> { impl<R: Real> fmt::LowerExp for Vec3<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "(")?; fmt::Display::fmt("(", f)?;
fmt::LowerExp::fmt(&self.x(), f)?; fmt::LowerExp::fmt(&self.x(), f)?;
write!(f, ", ")?; fmt::Display::fmt(", ", f)?;
fmt::LowerExp::fmt(&self.y(), f)?; fmt::LowerExp::fmt(&self.y(), f)?;
write!(f, ", ")?; fmt::Display::fmt(", ", f)?;
fmt::LowerExp::fmt(&self.z(), f)?; fmt::LowerExp::fmt(&self.z(), f)?;
write!(f, ")") fmt::Display::fmt(")", f)
} }
} }
impl<R: Real> fmt::Display for Vec3<R> { impl<R: Real> fmt::Display for Vec3<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "(")?; fmt::Display::fmt("(", f)?;
fmt::Display::fmt(&self.x(), f)?; fmt::Display::fmt(&self.x(), f)?;
write!(f, ", ")?; fmt::Display::fmt(", ", f)?;
fmt::Display::fmt(&self.y(), f)?; fmt::Display::fmt(&self.y(), f)?;
write!(f, ", ")?; fmt::Display::fmt(", ", f)?;
fmt::Display::fmt(&self.z(), f)?; fmt::Display::fmt(&self.z(), f)?;
write!(f, ")") fmt::Display::fmt(")", f)
} }
} }

View File

@@ -1,11 +1,14 @@
use super::vec::Vec3; use crate::real::Real;
use super::real::Real; use crate::vec::Vec3;
use serde::{Serialize, Deserialize};
use core::fmt::{self, Display}; use core::fmt::{self, Display};
use core::ops::{Add, Div, Mul, Sub}; use core::ops::{Add, Div, Mul, Sub};
#[derive(Copy, Clone, Debug, Default, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)] #[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Copy, Clone, Debug, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Vec3u { pub struct Vec3u {
x: u32, x: u32,
y: u32, y: u32,
@@ -80,6 +83,6 @@ impl Mul<u32> for Vec3u {
impl Display for Vec3u { impl Display for Vec3u {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {}, {})", self.x, self.y, self.z) core::fmt::Debug::fmt(&(self.x, self.y, self.z), f)
} }
} }