Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions crates/spirv-std/src/math_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Copy link
Contributor

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 ✨).

Copy link
Member Author

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 an if like this anyway so ✨

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer this to be lerp, but that's mostly bikeshedding and I don't really care. Also, I think I'd prefer t.lerp(a, b) instead of a.lerp(b, t), although Freya's a lil sketchy here and thinks that both ways are valid interpretations, and almost wouldn't want an "object"-style method to avoid that ambiguity, haha. (t.lerp(a, b) makes sense in shaders, a.lerp(b, t) makes sense when animating objects)

Copy link
Member Author

Choose a reason for hiding this comment

The 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 lerp has a bit more of a stringent mathematical definition/interpretation that doesn't really allow something like fn lerp(self: Vec3, other: Vec3, a: Vec3) whereas with a more loosey-goosey definition of mix it can make sense (and is sometimes useful).

Copy link

@FreyaHolmer FreyaHolmer Nov 10, 2020

Choose a reason for hiding this comment

The 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

t = InvLerp( a, b, value )
t = value.InvLerp( a, b )

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

v = Lerp( a, b, t )
v = t.Lerp( a, b )

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:

vOut = vIn.Remap( iMin, iMax, oMin, oMax )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or be really spicy with t.mix(a..b) >:D (/s. mostly.)

self - (self + other) * a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also wait, is this correct? wolframalpha says this is equivalent to self * (1 - a) - other * a, which, I think lerp/mix is self * (1 - a) + other * a? I think this should be self + (other - self) * a? it is midnight and I am doubting myself though D:

Choose a reason for hiding this comment

The 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:
v = (1-t)a+bt
v = a+(b-a)t

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes you are correct, i didn't distribute the negative x)

}
}