# Frustum check in simd

**URL:** <https://forum.babylonjs.com/t/frustum-check-in-simd/55552>\
**Category:** Off topic\
**Tags:** performance, math, boundingbox, simd\
**Created:** [December 25, 2024, 2:46am UTC](https://forum.babylonjs.com/t/frustum-check-in-simd/55552 "2024-12-25T02:46:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [December 25, 2024, 2:46am UTC](https://forum.babylonjs.com/t/frustum-check-in-simd/55552/1 "2024-12-25T02:46:36Z")

</div>

For bounding boxed aligned to 4 floats (16 bytes)  
Depends on [cglm](https://github.com/recp/cglm)

> **AVX Impl**
>
> ```C
> CGLM_INLINE
> bool
> avx_aabb_frustum(vec4 min, vec4 max, vec4 planes[6]) {
> float dp;
> int i;
> glmm_128 minv, maxv, plane, zero, sign, tmp;
> minv = glmm_load(min);
> maxv = glmm_load(max);
> minv[3] = 1.f;
> maxv[3] = 1.f;
> zero = glmm_set1(0.f);
> 
> for (i = 0; i < 6; i++) {
> plane = glmm_load(planes[i]);
> sign = plane > zero;
> tmp = _mm_blendv_ps(minv, maxv, sign);
> dp = glmm_dot(plane, tmp);
> 
> if (dp < 0)
> return false;
> }
> 
> return true;
> }
> 
> ```

> **Wasm Impl**
>
> ```C
> bool
> aabb_frustum(vec4 min, vec4 max, vec4 planes[6]) {
> float dp;
> int i;
> glmm_128 minv, maxv, plane, zero, sign, tmp;
> minv = glmm_load(min);
> maxv = glmm_load(max);
> minv = wasm_f32x4_replace_lane(minv, 3, 1.0f);
> maxv = wasm_f32x4_replace_lane(maxv, 3, 1.0f);
> zero = glmm_set1(0.f);
> 
> for (i = 0; i < 6; i++) {
> plane = glmm_load(planes[i]);
> sign = wasm_f32x4_gt(plane, zero);
> tmp = wasm_v128_bitselect(maxv, minv, sign);
> dp = glmm_dot(plane, tmp);
> 
> if (dp < 0)
> return false;
> }
> 
> return true;
> }
> 
> ```

Wasm Impl with deps (loop unrolled):

> **[Compiler Explorer - C (WebAssembly clang (trunk))](https://godbolt.org/z/8bM1Po7o6)**
>
> \# define CGLM\_ALIGN(X) \_\_attribute((aligned(X)))
> \# define CGLM\_ALIGN\_IF(X) CGLM\_ALIGN(X)
> typedef CGLM\_ALIGN\_IF(16) float vec4\[4\];
> \#define glmm\_load(p) wasm\_v128\_load(p)
> \#define glmm\_store(p, a) wasm\_v128\_store(p, (a))
> 
> \#define glmm\_set1(x)...

> **The same algo in scalar js**
>
> [three.js/src/math/Frustum.js at 05dbc5d9f24d290a80173b218b7b8535015674df · mrdoob/three.js · GitHub](https://github.com/mrdoob/three.js/blob/05dbc5d9f24d290a80173b218b7b8535015674df/src/math/Frustum.js#L133-L157)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 25, 2024, 2:43pm UTC](https://forum.babylonjs.com/t/frustum-check-in-simd/55552/2 "2024-12-25T14:43:30Z")

</div>

To make it worthwhile, you’d have to calculate all the bounding boxes at once in a loop, as I think there are hidden costs to using/calling wasm code (?).

Also, I’m not sure we currently have a frustum check bottleneck, but that may depend on your scene.

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [December 26, 2024, 1:04am UTC](https://forum.babylonjs.com/t/frustum-check-in-simd/55552/3 "2024-12-26T01:04:19Z")

</div>

Yes, if calling wasm once for each mesh, the algo improvement might not cover the [cost](https://hacks.mozilla.org/2018/10/calls-between-javascript-and-webassembly-are-finally-fast-%f0%9f%8e%89/) of js-wasm calls, like context switching, and call conversions. But it would worth if doing in batches, like, call per 1k or 2k meshes.  
Also, since it only need min/max in world space, instead of all 8 corners, the transformation of bounding boxes can be simplified, like cglm’s impl:

> <https://github.com/recp/cglm/blob/054b2df0048e655b15bbf5621316c1baba20a66b/include/cglm/box.h#L25>

It’s strightforward to convert this impl to simd by replacing all vec3 to vec4 (and align pointers), so transformation and check can all be in simd, this should give more performance boost.

This can at least make `thinInstanceRefreshBoundingInfo` faster with a lot of thin instances.
