Skip to content

Commit a019894

Browse files
committed
Add Log2 trait
1 parent fa36a1e commit a019894

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod natural_log;
2323
pub mod mix;
2424
pub mod pow;
2525
pub mod exp2;
26+
pub mod log2;
2627

2728
pub mod prelude;
2829

src/log2.rs

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

0 commit comments

Comments
 (0)