-
Notifications
You must be signed in to change notification settings - Fork 244
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
Add step
, step_select
, mix
to spirv_std::MathExt
#223
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,20 @@ 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; | ||
|
||
/// 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; | ||
} | ||
|
@@ -71,6 +81,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 | ||
|
@@ -82,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd prefer this to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I originally agreed with that, but one thing that one of your comments brought up actually is that I feel like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little torn on this one, but I think if we consider inverse lerp, I think it's crystal clear that the value parameter should go first, because in 99% of cases, you are remapping a value into a 0-1 range
and looking back at lerp, we're usually blending between two states, and having those states next to each other to signify the "input range" makes a lot of sense
And with the "remapping to a range" perspective, this one too is remapping a t value into another range. The one thing that makes me uncomfortable with this one though is that t is usually a float, but it can return a vector (or vice versa) which feels a little spooky to me for some reason~ But in the context of value remapping it kinda makes sense. Just like you could have a remap function which would read pretty naturally too:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or be really spicy with |
||
self - (self + other) * a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also wait, is this correct? wolframalpha says this is equivalent to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pretty sure this one is incorrect yeah! usually I see either of these two: I think I recall that the first one is more numerically stable as t approaches 1? in case that matters here, not sure if it does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes you are correct, i didn't distribute the negative x) |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably be an intrinsic that compiles to the spir-v step instruction, but whatever, who cares right now. I agree with the
x.step(edge)
order (and Freya does too ✨).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afaik
step
is almost certainly compiled down to anif
like this anyway so ✨