Skip to content

Commit 0e739b1

Browse files
committed
Add Asin, Atan2, Dot, Length, Normalize, Round, Step traits
1 parent 57ae4f2 commit 0e739b1

File tree

9 files changed

+331
-4
lines changed

9 files changed

+331
-4
lines changed

src/asin.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! WGSL `asin()`
2+
3+
use crate::glam::{Vec2, Vec3, Vec4};
4+
5+
/// Equivalent of the WGSL `saturate()` function.
6+
///
7+
/// Clamps a value to the 0.0..=1.0 range
8+
pub trait Asin {
9+
fn asin(self) -> Self;
10+
}
11+
12+
impl Asin for f32 {
13+
fn asin(self) -> Self {
14+
#[cfg(feature = "glam")]
15+
{
16+
f32::asin(self)
17+
}
18+
19+
#[cfg(feature = "spirv-std")]
20+
{
21+
spirv_std::num_traits::Float::asin(self)
22+
}
23+
}
24+
}
25+
26+
impl Asin for Vec2 {
27+
fn asin(self) -> Self {
28+
Vec2::new(self.x.asin(), self.y.asin())
29+
}
30+
}
31+
32+
impl Asin for Vec3 {
33+
fn asin(self) -> Self {
34+
Vec3::new(self.x.asin(), self.y.asin(), self.z.asin())
35+
}
36+
}
37+
38+
impl Asin for Vec4 {
39+
fn asin(self) -> Self {
40+
Vec4::new(self.x.asin(), self.y.asin(), self.z.asin(), self.w.asin())
41+
}
42+
}
43+

src/atan2.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! WGSL `atan2()`
2+
3+
use crate::glam::{Vec2, Vec3, Vec4};
4+
5+
#[cfg(feature = "spirv-std")]
6+
#[allow(unused_imports)]
7+
use spirv_std::num_traits::Float;
8+
9+
/// Equivalent of the WGSL `atan2()` function.
10+
pub trait Atan2 {
11+
fn atan2(self, p: Self) -> Self;
12+
}
13+
14+
impl Atan2 for f32 {
15+
fn atan2(self, p: Self) -> Self {
16+
#[cfg(feature = "glam")]
17+
{
18+
f32::atan2(self, p)
19+
}
20+
21+
#[cfg(feature = "spirv-std")]
22+
{
23+
<f32 as spirv_std::num_traits::Float>::atan2(self, p)
24+
}
25+
}
26+
}
27+
28+
impl Atan2 for Vec2 {
29+
fn atan2(self, p: Self) -> Self {
30+
Vec2::new(Atan2::atan2(self.x, p.x), Atan2::atan2(self.y, p.y))
31+
}
32+
}
33+
34+
impl Atan2 for Vec3 {
35+
fn atan2(self, p: Self) -> Self {
36+
Vec3::new(
37+
Atan2::atan2(self.x, p.x),
38+
Atan2::atan2(self.y, p.y),
39+
Atan2::atan2(self.z, p.z),
40+
)
41+
}
42+
}
43+
44+
impl Atan2 for Vec4 {
45+
fn atan2(self, p: Self) -> Self {
46+
Vec4::new(
47+
Atan2::atan2(self.x, p.x),
48+
Atan2::atan2(self.y, p.y),
49+
Atan2::atan2(self.z, p.z),
50+
Atan2::atan2(self.w, p.w),
51+
)
52+
}
53+
}

src/dot.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! WGSL `dot()`
2+
3+
use crate::glam::{Vec2, Vec3, Vec4};
4+
5+
#[cfg(feature = "spirv-std")]
6+
#[allow(unused_imports)]
7+
use spirv_std::num_traits::Float;
8+
9+
/// Equivalent of the WGSL `dot()` function.
10+
///
11+
/// Returns a scalar from -1.0..1.0 denoting the similarity of two vectors.
12+
pub trait Dot {
13+
fn dot(self, rhs: Self) -> f32;
14+
}
15+
16+
impl Dot for f32 {
17+
fn dot(self, rhs: Self) -> f32 {
18+
self * rhs
19+
}
20+
}
21+
22+
impl Dot for Vec2 {
23+
fn dot(self, rhs: Self) -> f32 {
24+
Vec2::dot(self, rhs)
25+
}
26+
}
27+
28+
impl Dot for Vec3 {
29+
fn dot(self, rhs: Self) -> f32 {
30+
Vec3::dot(self, rhs)
31+
}
32+
}
33+
34+
impl Dot for Vec4 {
35+
fn dot(self, rhs: Self) -> f32 {
36+
Vec4::dot(self, rhs)
37+
}
38+
}
39+

src/length.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! WGSL `length()`
2+
3+
use crate::glam::{Vec2, Vec3, Vec4};
4+
5+
#[cfg(feature = "spirv-std")]
6+
#[allow(unused_imports)]
7+
use spirv_std::num_traits::Float;
8+
9+
/// Equivalent of the WGSL `length()` function.
10+
///
11+
/// Returns the length of self.
12+
pub trait Length {
13+
fn length(self) -> f32;
14+
}
15+
16+
impl Length for f32 {
17+
fn length(self) -> f32 {
18+
#[cfg(feature = "glam")]
19+
{
20+
f32::abs(self)
21+
}
22+
23+
#[cfg(feature = "spirv-std")]
24+
{
25+
spirv_std::num_traits::Float::abs(self)
26+
}
27+
}
28+
}
29+
30+
impl Length for Vec2 {
31+
fn length(self) -> f32 {
32+
Vec2::length(self)
33+
}
34+
}
35+
36+
impl Length for Vec3 {
37+
fn length(self) -> f32 {
38+
Vec3::length(self)
39+
}
40+
}
41+
42+
impl Length for Vec4 {
43+
fn length(self) -> f32 {
44+
Vec4::length(self)
45+
}
46+
}
47+

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,29 @@ pub use spirv_std::glam;
1616
#[cfg(all(not(feature = "glam"), not(feature = "spirv-std")))]
1717
compile_error!("Either the glam or spirv-std feature must be enabled.");
1818

19-
pub mod acos;
2019
pub mod abs;
20+
pub mod asin;
21+
pub mod acos;
22+
pub mod atan2;
2123
pub mod clamp;
2224
pub mod cos;
25+
pub mod dot;
2326
pub mod exp2;
27+
pub mod length;
2428
pub mod log2;
2529
pub mod mix;
2630
pub mod modulo;
2731
pub mod natural_log;
32+
pub mod normalize;
2833
pub mod pow;
2934
pub mod reflect;
35+
pub mod round;
3036
pub mod saturate;
3137
pub mod sign;
3238
pub mod sin;
3339
pub mod smooth_step;
3440
pub mod sqrt;
41+
pub mod step;
3542
pub mod tan;
3643

3744
pub mod prelude;

src/normalize.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! WGSL `normalize()`
2+
3+
use crate::{
4+
glam::{Vec2, Vec3, Vec4},
5+
sign::Sign,
6+
};
7+
8+
#[cfg(feature = "spirv-std")]
9+
#[allow(unused_imports)]
10+
use spirv_std::num_traits::Float;
11+
12+
/// Equivalent of the WGSL `normalize()` function.
13+
///
14+
/// Returns the provided vector quantity scaled such that its length is 1.
15+
pub trait Normalize {
16+
fn normalize(self) -> Self;
17+
}
18+
19+
impl Normalize for f32 {
20+
fn normalize(self) -> Self {
21+
self.sign()
22+
}
23+
}
24+
25+
impl Normalize for Vec2 {
26+
fn normalize(self) -> Self {
27+
Vec2::normalize(self)
28+
}
29+
}
30+
31+
impl Normalize for Vec3 {
32+
fn normalize(self) -> Self {
33+
Vec3::normalize(self)
34+
}
35+
}
36+
37+
impl Normalize for Vec4 {
38+
fn normalize(self) -> Self {
39+
Vec4::normalize(self)
40+
}
41+
}
42+

src/prelude.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub use crate::{
2-
abs::*, acos::*, clamp::*, cos::*, exp2::*, glam::*, log2::*, mix::*, modulo::*,
3-
natural_log::*, pow::*, reflect::*, saturate::*, sign::*, sin::*, smooth_step::*, sqrt::*,
4-
tan::*, *,
2+
abs::*, acos::*, asin::*, atan2::*, clamp::*, cos::*, dot::*, exp2::*, glam::*, length::*,
3+
log2::*, mix::*, modulo::*, natural_log::*, normalize::*, pow::*, reflect::*, round::*,
4+
saturate::*, sign::*, sin::*, smooth_step::*, sqrt::*, step::*, tan::*, *,
55
};

src/round.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! WGSL `round()`
2+
3+
use crate::glam::{Vec2, Vec3, Vec4};
4+
5+
#[cfg(feature = "spirv-std")]
6+
#[allow(unused_imports)]
7+
use spirv_std::num_traits::Float;
8+
9+
/// Equivalent of the WGSL `round()` function.
10+
///
11+
/// Returns self rounded to the nearest integer.
12+
pub trait Round {
13+
fn round(self) -> Self;
14+
}
15+
16+
impl Round for f32 {
17+
fn round(self) -> Self {
18+
#[cfg(feature = "glam")]
19+
{
20+
f32::round(self)
21+
}
22+
23+
#[cfg(feature = "spirv-std")]
24+
{
25+
spirv_std::num_traits::Float::round(self)
26+
}
27+
}
28+
}
29+
30+
impl Round for Vec2 {
31+
fn round(self) -> Self {
32+
Vec2::new(Round::round(self.x), Round::round(self.y))
33+
}
34+
}
35+
36+
impl Round for Vec3 {
37+
fn round(self) -> Self {
38+
Vec3::new(Round::round(self.x), Round::round(self.y), Round::round(self.z))
39+
}
40+
}
41+
42+
impl Round for Vec4 {
43+
fn round(self) -> Self {
44+
Vec4::new(
45+
Round::round(self.x),
46+
Round::round(self.y),
47+
Round::round(self.z),
48+
Round::round(self.w),
49+
)
50+
}
51+
}
52+

src/step.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! WGSL `step()`
2+
3+
use crate::glam::{Vec2, Vec3, Vec4};
4+
5+
/// Equivalent of the WGSL `step()` function.
6+
///
7+
/// Returns 1.0 if value is >= edge, 0.0 otherwise
8+
pub trait Step {
9+
fn step(self, edge: f32) -> Self;
10+
}
11+
12+
impl Step for f32 {
13+
fn step(self, edge: f32) -> Self {
14+
if edge >= self {
15+
1.0
16+
} else {
17+
0.0
18+
}
19+
}
20+
}
21+
22+
impl Step for Vec2 {
23+
fn step(self, edge: f32) -> Self {
24+
Vec2::new(self.x.step(edge), self.y.step(edge))
25+
}
26+
}
27+
28+
impl Step for Vec3 {
29+
fn step(self, edge: f32) -> Self {
30+
Vec3::new(self.x.step(edge), self.y.step(edge), self.z.step(edge))
31+
}
32+
}
33+
34+
impl Step for Vec4 {
35+
fn step(self, edge: f32) -> Self {
36+
Vec4::new(
37+
self.x.step(edge),
38+
self.y.step(edge),
39+
self.z.step(edge),
40+
self.w.step(edge),
41+
)
42+
}
43+
}
44+

0 commit comments

Comments
 (0)