Skip to content

Commit b625043

Browse files
authored
remove fast_sqrt in favor of sqrt (#19995)
# Objective - the fast inverse sqrt trick hasnt been useful on modern hardware for over a decade now ## Solution - just use sqrt, modern hardware has a dedicated instruction which will outperform approximations both in efficiency and accuracy ## Testing - ran `atmosphere`
1 parent 537adcc commit b625043

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

crates/bevy_render/src/maths.wgsl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,11 @@ fn project_onto(lhs: vec3<f32>, rhs: vec3<f32>) -> vec3<f32> {
104104
// are likely most useful when raymarching, for example, where complete numeric
105105
// accuracy can be sacrificed for greater sample count.
106106

107-
fn fast_sqrt(x: f32) -> f32 {
108-
let n = bitcast<f32>(0x1fbd1df5 + (bitcast<i32>(x) >> 1u));
109-
// One Newton's method iteration for better precision
110-
return 0.5 * (n + x / n);
111-
}
112-
113107
// Slightly less accurate than fast_acos_4, but much simpler.
114108
fn fast_acos(in_x: f32) -> f32 {
115109
let x = abs(in_x);
116110
var res = -0.156583 * x + HALF_PI;
117-
res *= fast_sqrt(1.0 - x);
111+
res *= sqrt(1.0 - x);
118112
return select(PI - res, res, in_x >= 0.0);
119113
}
120114

@@ -131,7 +125,7 @@ fn fast_acos_4(x: f32) -> f32 {
131125
s = -0.2121144 * x1 + 1.5707288;
132126
s = 0.0742610 * x2 + s;
133127
s = -0.0187293 * x3 + s;
134-
s = fast_sqrt(1.0 - x1) * s;
128+
s = sqrt(1.0 - x1) * s;
135129

136130
// acos function mirroring
137131
return select(PI - s, s, x >= 0.0);

0 commit comments

Comments
 (0)