Skip to content

Commit 3b653b3

Browse files
committed
Fix NaturalLog backend variance, add Fract trait
1 parent 92ff272 commit 3b653b3

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

src/fract.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Shader `fract()`
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+
/// Returns the fractional part of self.
10+
///
11+
/// Equivalent of the `fract()` function.
12+
pub trait Fract {
13+
fn fract(self) -> Self;
14+
}
15+
16+
impl Fract for f32 {
17+
fn fract(self) -> Self {
18+
#[cfg(feature = "glam")]
19+
{
20+
f32::fract(self)
21+
}
22+
23+
#[cfg(feature = "spirv-std")]
24+
{
25+
spirv_std::num_traits::Float::fract(self)
26+
}
27+
}
28+
}
29+
30+
impl Fract for Vec2 {
31+
fn fract(self) -> Self {
32+
Vec2::new(Fract::fract(self.x), Fract::fract(self.y))
33+
}
34+
}
35+
36+
impl Fract for Vec3 {
37+
fn fract(self) -> Self {
38+
Vec3::new(
39+
Fract::fract(self.x),
40+
Fract::fract(self.y),
41+
Fract::fract(self.z),
42+
)
43+
}
44+
}
45+
46+
impl Fract for Vec4 {
47+
fn fract(self) -> Self {
48+
Vec4::new(
49+
Fract::fract(self.x),
50+
Fract::fract(self.y),
51+
Fract::fract(self.z),
52+
Fract::fract(self.w),
53+
)
54+
}
55+
}
56+

src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ pub use spirv_std::glam;
1717
compile_error!("Either the glam or spirv-std feature must be enabled.");
1818

1919
mod abs;
20-
mod asin;
2120
mod acos;
21+
mod asin;
2222
mod atan2;
2323
mod clamp;
2424
mod cos;
2525
mod dot;
2626
mod exp2;
27+
mod fract;
2728
mod length;
2829
mod log2;
2930
mod mix;
@@ -48,6 +49,7 @@ pub use clamp::*;
4849
pub use cos::*;
4950
pub use dot::*;
5051
pub use exp2::*;
52+
pub use fract::*;
5153
pub use length::*;
5254
pub use log2::*;
5355
pub use mix::*;
@@ -67,10 +69,6 @@ pub use tan::*;
6769

6870
use glam::Vec3;
6971

70-
#[cfg(feature = "spirv-std")]
71-
#[allow(unused_imports)]
72-
use spirv_std::num_traits::Float;
73-
7472
/// Convert from HSV to RGB.
7573
///
7674
/// From `bevy_pbr/src/render/utils.wgsl`

src/natural_log.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ use spirv_std::num_traits::Float;
1212
pub trait NaturalLog {
1313
const BASE: f32 = 2.718281828459;
1414

15-
fn natural_log(&self) -> Self;
15+
fn natural_log(self) -> Self;
1616
}
1717

1818
impl NaturalLog for f32 {
19-
fn natural_log(&self) -> Self {
20-
self.log(Self::BASE)
19+
fn natural_log(self) -> Self {
20+
#[cfg(feature = "glam")]
21+
{
22+
f32::log(self, Self::BASE)
23+
}
24+
25+
#[cfg(feature = "spirv-std")]
26+
{
27+
spirv_std::num_traits::Float::log(self, Self::BASE)
28+
}
2129
}
2230
}
2331

2432
impl NaturalLog for Vec2 {
25-
fn natural_log(&self) -> Self {
33+
fn natural_log(self) -> Self {
2634
Vec2::new(
2735
NaturalLog::natural_log(self.x),
2836
NaturalLog::natural_log(self.y),
@@ -31,7 +39,7 @@ impl NaturalLog for Vec2 {
3139
}
3240

3341
impl NaturalLog for Vec3 {
34-
fn natural_log(&self) -> Self {
42+
fn natural_log(self) -> Self {
3543
Vec3::new(
3644
NaturalLog::natural_log(self.x),
3745
NaturalLog::natural_log(self.y),
@@ -41,7 +49,7 @@ impl NaturalLog for Vec3 {
4149
}
4250

4351
impl NaturalLog for Vec4 {
44-
fn natural_log(&self) -> Self {
52+
fn natural_log(self) -> Self {
4553
Vec4::new(
4654
NaturalLog::natural_log(self.x),
4755
NaturalLog::natural_log(self.y),

0 commit comments

Comments
 (0)