# \`sphericalPolynomial\` really slow

**URL:** <https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820>\
**Category:** Bugs\
**Created:** [November 18, 2022, 7:56pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820 "2022-11-18T19:56:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alekop](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/alekop/32/4380_2.png) [@alekop](https://forum.babylonjs.com/u/alekop)\
**Post date:** [November 18, 2022, 7:56pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820/1 "2022-11-18T19:56:32Z")

</div>

`CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial` takes about 15s on Safari. For comparison, it takes about 3s on Chrome. Still very slow.

![Screenshot 2022-11-18 at 11.01.12 AM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/8/5853298bad7623ea5ce43a0dc439751541854a10.png)

What happens is:

1. A mesh (with a PBR material) enters the frustum.

2. Even though it’s completely occluded, the engine calls bindForSubMesh, which decides it `mustRebind`. **Shouldn’t the mesh be ignored?**

3. Then we have this:

4. We then check if there’s a reflection texture, and if so, get its `sphericalPolynomial` property. That’s what’s slow.

5. The worst part is… **the reason we computed the polynomials is to update `ubo`, which is disabled!**

6. It seems odd that **we’re reading a property (sphericalPolynomial) synchronously, while the actual computation is done in a Promise**. The property returns null until it’s computed, so the whole UBO update fails anyway.

7. Using a promise doesn’t help anyway, b/c it still runs on the main thread… It should be in a worker.

My apologies, if I’m jumping to conclusions. There’s a lot I don’t understand, so I could be totally wrong.

Anyway, I’m working around this by converting all PBR materials to standard. I don’t use PBRs, but that’s how GLTF materials are loaded.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 18, 2022, 8:19pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820/2 "2022-11-18T20:19:51Z")

</div>

@alekop this should be precomputed and cached if you use an env file instead of hdr for your environment texture.

Computing on the client is a waste of resources except in scenari of configurators like the sandbox where the user could dynamically chose any HDR files or with dynamic probes and so on,

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 18, 2022, 8:21pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820/3 "2022-11-18T20:21:04Z")

</div>

> [@alekop](#):
>
> Then we have this:
> 
> `if (!ubo.useUbo || !this.isFrozen || !ubo.isSync) {`  
> _useUbo=false, isFrozen=true, ubo.isSync=true._
> 
> We proceed because useUbo is false, but… **should we be checking for ubo.isSync, if useUbo is false**?

This one is really interesting @Evgeni_Popov could you double check ?

---

<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:** [November 18, 2022, 8:38pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820/4 "2022-11-18T20:38:29Z")

</div>

> if (!ubo.useUbo || !this.isFrozen || !ubo.isSync) {

If `ubo.useUbo` is `false`, the other expressions are not evaluated because `!ubo.useUbo` is `true` and all the expressions are ORed.

So, `ubo.isSync` is evaluated only if `ubo.useUbo=true`, which is the expected behavior. Note that `ubo.isSync` has no meaning if `ubo.useUbo=false`.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 18, 2022, 8:39pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820/5 "2022-11-18T20:39:56Z")

</div>

ahah Thanks a lot, sorry to disturb for this one, was in a meeting 😉

---

<div class="post-metadata">

**Author:** ![alekop](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/alekop/32/4380_2.png) [@alekop](https://forum.babylonjs.com/u/alekop)\
**Post date:** [November 18, 2022, 9:11pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820/6 "2022-11-18T21:11:26Z")

</div>

Makes sense. But if UBO is disabled, why do the whole polynomial computation? We use the result to update ubo.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 18, 2022, 9:25pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820/7 "2022-11-18T21:25:36Z")

</div>

we also update uniforms if ubos are not availables, we need the value somewhere 🙂 either in ubo or in uniforms

---

<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:** [November 18, 2022, 9:27pm UTC](https://forum.babylonjs.com/t/sphericalpolynomial-really-slow/35820/8 "2022-11-18T21:27:07Z")

</div>

The polynomial computation is not related to ubo.

When you see `ubo.updateXXX(...)`, the uniform buffer is updated if `ubo.useUbo==true` and it is the effect which is updated if `ubo.useUbo==false`. `ubo.updateXXX(...)` is not an empty operation when `ubo.useUbo==false`.
