Skip to content

Commit 3bac6eb

Browse files
committed
fastExp implementation for gsplat rendering (#7784)
1 parent f184d55 commit 3bac6eb

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/scene/shader-lib/glsl/chunks/gsplat/frag/gsplat.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ export default /* glsl */`
1919
#include "floatAsUintPS"
2020
#endif
2121
22+
// Fast approximate e^x based on https://nic.schraudolph.org/pubs/Schraudolph99.pdf
23+
const float EXP_A = 12102203.0; // ≈ 2^23 / ln(2)
24+
const int EXP_BC_RMS = 1064866808; // (127 << 23) - 60801 * 8
25+
float fastExp(float x) {
26+
int i = int(EXP_A * x) + EXP_BC_RMS;
27+
return intBitsToFloat(i);
28+
}
29+
2230
varying mediump vec2 gaussianUV;
2331
varying mediump vec4 gaussianColor;
2432
@@ -29,7 +37,7 @@ void main(void) {
2937
}
3038
3139
// evaluate alpha
32-
mediump float alpha = exp(-A * 4.0) * gaussianColor.a;
40+
mediump float alpha = fastExp(-A * 4.0) * gaussianColor.a;
3341
3442
#if defined(SHADOW_PASS) || defined(PICK_PASS) || defined(PREPASS_PASS)
3543
if (alpha < alphaClip) {

src/scene/shader-lib/wgsl/chunks/gsplat/frag/gsplat.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ export default /* wgsl */`
1919
#include "floatAsUintPS"
2020
#endif
2121
22+
// Fast approximate e^x based on https://nic.schraudolph.org/pubs/Schraudolph99.pdf
23+
const EXP_A: f32 = 12102203.0; // ≈ 2^23 / ln(2)
24+
const EXP_BC_RMS: i32 = 1064866808; // (127 << 23) - 60801 * 8
25+
fn fastExp(x: f32) -> f32 {
26+
var i: i32 = i32(EXP_A * x) + EXP_BC_RMS;
27+
return bitcast<f32>(i);
28+
}
29+
2230
varying gaussianUV: vec2f;
2331
varying gaussianColor: vec4f;
2432
@@ -33,7 +41,7 @@ fn fragmentMain(input: FragmentInput) -> FragmentOutput {
3341
}
3442
3543
// evaluate alpha
36-
var alpha: f32 = exp(-A * 4.0) * gaussianColor.a;
44+
var alpha: f32 = fastExp(-A * 4.0) * gaussianColor.a;
3745
3846
#if defined(SHADOW_PASS) || defined(PICK_PASS) || defined(PREPASS_PASS)
3947
if (alpha < uniform.alphaClip) {

0 commit comments

Comments
 (0)