-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Math for Curves and Interpolations (Lerp and Callmul-Rom) #1837
Conversation
Have you looked at the https://github.com/mockersf/bevy_extra/tree/master/bevy_easings Also, would your animations work obsolete Note that |
In the scope of #1429, this is much more general than There is commented code in this PR, should it be removed? About the Also, some of the doc comments don't sound completely right to my non-english native ear, could someone check? |
What comented code? I wan't to keep the Not sure what TODOs, but I want to keep them for later, otherwise they will be forgoten; Can you point out the comments that sound strange? Note also that |
I added a few comments on commented code and doc
There are 8 |
Added 2 more functions in the Curve api to move common functions from curve specific impl to the trait impl; |
/// | ||
/// Implementation borrowed from Piston under the MIT License: [https://github.com/PistonDevelopers/skeletal_animation] | ||
#[inline] | ||
pub fn fast_inv_sqrt(x: f32) -> f32 { |
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.
Alright, I did some more digging: Rust's native solution for this is faster than the approximate solution because inverse square root has a dedicated instruction these days. See: https://stackoverflow.com/a/59082210/3234149
IMO this function should be cut completely and its usages should be replaced with sqrt().recip()
.
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.
Good catch
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.
Do you know how the intrinsic rsqrt + a single Newton-Raphson iteration compares with the 'carmack' method?
In that same thread I found this: https://stackoverflow.com/questions/31555260, so I compiled a few possible methods here https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=ddc10be842400c95b81a0c5fba218da6;
If my memory is not failing me, in a old bench I found that sqrtss
as a very hot instruction and that made me use the carmak method, I will have to double check that ...
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.
Small note: While sqrt
and 1.0/x
are guaranteed under IEEE-754 2008, implementations at the hardware level may or may not be compliant. My particular game I'm working on requires animation to be in the networking simulation hotpath, and cross-platform determinism is mandatory, something that may not be uncommon to see across other networked games too. If we are going down the route of sqrt().recip()
, could we use libm::sqrtf
here under a feature flag? glam already has a feature flag for it, so we could pass the feature flag through for ease of use.
The 'carmack' method here does not have this issue and should be consistent across platforms.
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.
Yeah, I think we're going to need to do our own modern benchmarks here to get a firm answer on what's correct for this context.
The note on determinism is important; the approach James suggests seems very reasonable.
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.
I think in doing _mm_rsqrt_ss
with a fallback to quake3
method for non x86
or libm
/ no_std
but I'm not sure if the aligment requiriment will be a problem, expecially when called by the consumer code, on the nlerp side I can make things rigth I think ...
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.
@james7132 did you checked the lib impl for sqrtf here? It don't look like it's cross-plataform deterministic
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.
I submitted a revision for this function, by the looks of it the compiler can optimize the re-alignment instructions so it should not be a problem
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.
It doesn't look like it at a first glance and it's difficult to tell if it actually produces the same output as the intrinsic with sse
disabled. If it does, we may need to reevaluate this approach to achieving cross-platform FP determinism.
The approx_rsqrt
implementation here, however, we for a fact know does not produce the same bit-for-bit results between the two implementations given the error difference seen from the benchmarks.
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.
I suggest you to resample the curve animations, that way you can mitigate any differences between cross plataform hardware
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.
The examples/animations/smooth_curves.rs
needs an entry in the examples/README.md
@lassade, can you rebase this? @james7132 is this still useful for your animation plans? |
The curves are definitely still needed. However, I think it would be better to use the |
I can't :( |
Fair enough. I'll close this out; we should reuse (and credit) this code later when attempting to tackle this down the road. |
Required by #1429, adds curves and a
Lerp
trait used by the animation system in thebevy_math
crate;Notes
lerp
is by default clamped (but there is alerp_unclampled
)Missing bits, (non block)
Handle
andHandleUntyped
should implement lerp using a step functionCurveVariable
behaviourThe new
Color
definition make lerping and a color curve really complex and impractical (please join the discussion here), It's is some what related but doesn't block this issue;