Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial SPIRV no_std implementation #85

Merged
merged 20 commits into from Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -38,10 +38,12 @@ transform-types = []
libm = ["num-traits/libm"]

[dependencies]
bytemuck = { version = "1.4", optional = true, default-features = false }
mint = { version = "0.5", optional = true, default-features = false }
# enabled by the libm feature, required when building no_std
num-traits = { version = "0.2.14", optional = true, default-features = false }

[target.'cfg(not(target_arch = "spirv"))'.dependencies]
bytemuck = { version = "1.4", optional = true, default-features = false }
rand = { version = "0.7", optional = true, default-features = false }
serde = { version = "1.0", optional = true, features = ["derive"] }

Expand Down
3 changes: 2 additions & 1 deletion build.rs
Expand Up @@ -6,7 +6,8 @@ fn main() {
panic!("The minimum supported version of Rust for `glam` is 1.36.0");
}

let force_scalar_math = env::var("CARGO_FEATURE_SCALAR_MATH").is_ok();
let force_scalar_math = env::var("CARGO_FEATURE_SCALAR_MATH").is_ok()
|| env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "spirv";
bitshifter marked this conversation as resolved.
Show resolved Hide resolved

let target_feature_sse2 = match env::var("CARGO_CFG_TARGET_FEATURE") {
Ok(cfg) => cfg.split(',').find(|&f| f == "sse2").is_some(),
Expand Down
14 changes: 8 additions & 6 deletions src/f32/mat2.rs
Expand Up @@ -4,10 +4,9 @@ use crate::swizzles::*;
use core::arch::x86::*;
#[cfg(all(vec4_sse2, target_arch = "x86_64",))]
use core::arch::x86_64::*;
use core::{
fmt,
ops::{Add, Deref, DerefMut, Mul, Sub},
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::{Add, Deref, DerefMut, Mul, Sub};

#[cfg(feature = "std")]
use std::iter::{Product, Sum};
Expand All @@ -23,15 +22,17 @@ pub fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {

/// A 2x2 column major matrix.
#[cfg(doc)]
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[derive(Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
#[repr(C)]
pub struct Mat2 {
pub x_axis: Vec2,
pub y_axis: Vec2,
}

#[cfg(not(doc))]
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[derive(Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
#[repr(C)]
pub struct Mat2(pub(crate) Vec4);

Expand All @@ -42,6 +43,7 @@ impl Default for Mat2 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat2 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
Expand Down
11 changes: 6 additions & 5 deletions src/f32/mat3.rs
@@ -1,8 +1,7 @@
use super::{scalar_sin_cos, Quat, Vec2, Vec3, Vec3A, Vec3ASwizzles};
use core::{
fmt,
ops::{Add, Mul, Sub},
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::{Add, Mul, Sub};

#[cfg(feature = "std")]
use std::iter::{Product, Sum};
Expand Down Expand Up @@ -44,7 +43,8 @@ fn quat_to_axes(rotation: Quat) -> (Vec3, Vec3, Vec3) {
}

/// A 3x3 column major matrix.
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[derive(Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
#[repr(C)]
pub struct Mat3 {
pub x_axis: Vec3,
Expand All @@ -59,6 +59,7 @@ impl Default for Mat3 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat3 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
Expand Down
11 changes: 6 additions & 5 deletions src/f32/mat4.rs
Expand Up @@ -6,10 +6,9 @@ use super::{scalar_sin_cos, Mat3, Quat, Vec3, Vec3A, Vec3ASwizzles, Vec4, Vec4Sw
use core::arch::x86::*;
#[cfg(all(vec4_sse2, target_arch = "x86_64"))]
use core::arch::x86_64::*;
use core::{
fmt,
ops::{Add, Mul, Sub},
};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::{Add, Mul, Sub};

#[cfg(feature = "std")]
use std::iter::{Product, Sum};
Expand Down Expand Up @@ -59,7 +58,8 @@ fn quat_to_axes(rotation: Quat) -> (Vec4, Vec4, Vec4) {
/// A 4x4 column major matrix.
///
/// This type is 16 byte aligned.
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[derive(Clone, Copy, PartialEq, PartialOrd)]
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
#[repr(C)]
pub struct Mat4 {
pub x_axis: Vec4,
Expand All @@ -75,6 +75,7 @@ impl Default for Mat4 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat4 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
Expand Down
5 changes: 4 additions & 1 deletion src/f32/quat.rs
Expand Up @@ -6,9 +6,10 @@ use super::{scalar_acos, scalar_sin_cos, Mat3, Mat4, Vec3, Vec3A, Vec4, Vec4Swiz
use core::arch::x86::*;
#[cfg(all(vec4_sse2, target_arch = "x86_64",))]
use core::arch::x86_64::*;
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::{
cmp::Ordering,
fmt,
ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub},
};

Expand Down Expand Up @@ -532,6 +533,7 @@ impl Quat {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Quat {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let a = self.as_ref();
Expand All @@ -544,6 +546,7 @@ impl fmt::Debug for Quat {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Quat {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let (x, y, z, w) = self.0.into();
Expand Down
9 changes: 7 additions & 2 deletions src/f32/vec2.rs
Expand Up @@ -2,7 +2,9 @@
use num_traits::Float;

use crate::f32::{Vec2Mask, Vec3};
use core::{f32, fmt, ops::*};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::{f32, ops::*};

#[cfg(feature = "std")]
use std::iter::{Product, Sum};
Expand All @@ -14,7 +16,8 @@ const Y_AXIS: Vec2 = const_vec2!([0.0, 1.0]);

/// A 2-dimensional vector.
#[derive(Clone, Copy, PartialEq, PartialOrd, Default)]
#[repr(C)]
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
pub struct Vec2 {
pub x: f32,
pub y: f32,
Expand Down Expand Up @@ -403,6 +406,7 @@ impl Vec2 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec2 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}, {}]", self.x, self.y)
Expand Down Expand Up @@ -570,6 +574,7 @@ impl AsMut<[f32; 2]> for Vec2 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Vec2 {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple("Vec2")
Expand Down
9 changes: 7 additions & 2 deletions src/f32/vec2_mask.rs
@@ -1,11 +1,14 @@
use super::Vec2;
use core::{fmt, ops::*};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;

/// A 2-dimensional vector mask.
///
/// This type is typically created by comparison methods on `Vec2`.
#[derive(Clone, Copy, Default, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[repr(C)]
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
pub struct Vec2Mask(u32, u32);

impl Vec2Mask {
Expand Down Expand Up @@ -97,12 +100,14 @@ impl Not for Vec2Mask {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Vec2Mask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Vec2Mask({:#x}, {:#x})", self.0, self.1)
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec2Mask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}]", self.0 != 0, self.1 != 0)
Expand Down
6 changes: 5 additions & 1 deletion src/f32/vec3.rs
Expand Up @@ -2,7 +2,9 @@
use num_traits::Float;

use super::{Vec2, Vec3A, Vec3Mask, Vec4};
use core::{fmt, ops::*};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;

#[cfg(feature = "std")]
use std::iter::{Product, Sum};
Expand Down Expand Up @@ -472,6 +474,7 @@ impl AsMut<[f32; 3]> for Vec3 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Vec3 {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_tuple("Vec3")
Expand All @@ -482,6 +485,7 @@ impl fmt::Debug for Vec3 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec3 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
Expand Down
6 changes: 5 additions & 1 deletion src/f32/vec3_mask.rs
@@ -1,5 +1,7 @@
use super::Vec3;
use core::{fmt, ops::*};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;

/// A 3-dimensional vector mask.
#[derive(Clone, Copy, Default, PartialEq, Eq, Ord, PartialOrd, Hash)]
Expand Down Expand Up @@ -103,12 +105,14 @@ impl Not for Vec3Mask {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Vec3Mask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Vec3Mask({:#x}, {:#x}, {:#x})", self.0, self.1, self.2)
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec3Mask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let arr = self.as_ref();
Expand Down
10 changes: 8 additions & 2 deletions src/f32/vec3a.rs
Expand Up @@ -2,7 +2,9 @@
use num_traits::Float;

use super::{Vec2, Vec3, Vec3AMask, Vec4};
use core::{fmt, ops::*};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;

#[cfg(all(vec3a_sse2, target_arch = "x86"))]
use core::arch::x86::*;
Expand Down Expand Up @@ -32,7 +34,9 @@ const Z_AXIS: Vec3A = const_vec3a!([0.0, 0.0, 1.0]);
/// It is possible to convert between `Vec3` and `Vec3A` types using `From` trait implementations.
#[cfg(doc)]
#[derive(Clone, Copy, PartialEq, PartialOrd, Default)]
#[repr(align(16), C)]
#[repr(align(16))]
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
pub struct Vec3A {
pub x: f32,
pub y: f32,
Expand Down Expand Up @@ -739,6 +743,7 @@ impl AsMut<[f32; 3]> for Vec3A {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Vec3A {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let a = self.as_ref();
Expand All @@ -750,6 +755,7 @@ impl fmt::Debug for Vec3A {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec3A {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[cfg(vec3a_sse2)]
Expand Down
6 changes: 5 additions & 1 deletion src/f32/vec3a_mask.rs
@@ -1,7 +1,9 @@
use super::Vec3A;
#[cfg(vec3a_f32)]
use super::Vec3Mask;
use core::{fmt, ops::*};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;

#[cfg(all(vec3a_sse2, target_arch = "x86"))]
use core::arch::x86::*;
Expand Down Expand Up @@ -251,6 +253,7 @@ impl Not for Vec3AMask {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Vec3AMask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(vec3a_sse2)]
Expand All @@ -272,6 +275,7 @@ impl fmt::Debug for Vec3AMask {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec3AMask {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let arr = self.as_ref();
Expand Down
9 changes: 7 additions & 2 deletions src/f32/vec4.rs
Expand Up @@ -2,7 +2,9 @@
use num_traits::Float;

use super::{Vec2, Vec3, Vec3A, Vec4Mask};
use core::{fmt, ops::*};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;

#[cfg(all(vec4_sse2, target_arch = "x86"))]
use core::arch::x86::*;
Expand Down Expand Up @@ -39,7 +41,8 @@ pub struct Vec4(pub(crate) __m128);
#[derive(Clone, Copy, PartialEq, PartialOrd, Default)]
// if compiling with simd enabled assume alignment needs to match the simd type
#[cfg_attr(any(vec4_sse2, vec4_f32_align16), repr(align(16)))]
#[repr(C)]
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
pub struct Vec4 {
pub x: f32,
pub y: f32,
Expand Down Expand Up @@ -788,6 +791,7 @@ impl AsMut<[f32; 4]> for Vec4 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Vec4 {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let a = self.as_ref();
Expand All @@ -800,6 +804,7 @@ impl fmt::Debug for Vec4 {
}
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Vec4 {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let a = self.as_ref();
Expand Down