From 29771ce1657c18e9e8d49a2e1128b76d1a16d4f8 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 10 Nov 2020 14:32:05 -0700 Subject: [PATCH 1/2] add trunc and fract to MathExt --- crates/spirv-std/src/math_ext.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/spirv-std/src/math_ext.rs b/crates/spirv-std/src/math_ext.rs index f196cd086c..c5b5495d9d 100644 --- a/crates/spirv-std/src/math_ext.rs +++ b/crates/spirv-std/src/math_ext.rs @@ -11,6 +11,8 @@ pub trait MathExt { fn ceil(self) -> Self; fn exp(self) -> Self; fn saturate(self) -> Self; + fn trunc(self) -> Self; + fn fract(self) -> Self; fn signum(self) -> Self; fn copysign(self, sign: Self) -> Self; @@ -71,6 +73,14 @@ impl MathExt for f32 { self.max(0.0).min(1.0) } + fn trunc(self) -> f32 { + unsafe { core::intrinsics::truncf32(self) } + } + + fn fract(self) -> f32 { + self - self.trunc() + } + fn signum(self) -> f32 { if self.is_nan() { Self::NAN From 5ed98c4cf581e251cc58245707550ede52d5889d Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 10 Nov 2020 15:13:31 -0700 Subject: [PATCH 2/2] add step, step_select, and mix to MathExt --- crates/spirv-std/src/math_ext.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/crates/spirv-std/src/math_ext.rs b/crates/spirv-std/src/math_ext.rs index c5b5495d9d..a8b296e0d2 100644 --- a/crates/spirv-std/src/math_ext.rs +++ b/crates/spirv-std/src/math_ext.rs @@ -17,6 +17,14 @@ pub trait MathExt { fn signum(self) -> Self; fn copysign(self, sign: Self) -> Self; + /// Returns `0.0` if `self < edge` and 1.0 otherwise. + fn step(self, edge: Self) -> Self; + /// Selects between `less` and `greater_or_equal` based on the result of `self < edge` + fn step_select(self, edge: Self, less: Self, greater_or_equal: Self) -> Self; + /// Performs a linear interpolation between `self` and `other` using `a` to weight between them. + /// The return value is computed as `self * (1−a) + other * a`. + fn mix(self, other: Self, a: Self) -> Self; + // TODO: implement but need new intrinsic in core? //fn tan(self) -> Self; } @@ -92,4 +100,24 @@ impl MathExt for f32 { fn copysign(self, sign: f32) -> f32 { unsafe { core::intrinsics::copysignf32(self, sign) } } + + fn step(self, edge: f32) -> f32 { + if self < edge { + 0.0 + } else { + 1.0 + } + } + + fn step_select(self, edge: f32, less: f32, greater_or_equal: f32) -> f32 { + if self < edge { + less + } else { + greater_or_equal + } + } + + fn mix(self, other: f32, a: f32) -> f32 { + self - (self + other) * a + } }