For the last week or two we’ve been getting reports of iOS issues, with the context being lost suddenly, and then being unable to recover. I was able to reproduce it a few times on ios 14.0, WebGL 1 and 2 on BabylonJS 4.1. I can’t say for sure it is related, but we didn’t get these reports previously, and I’ve certainly never noticed it myself.
I’ve seen after this error occurs, the context eventually restores (which I suspected after reviewing the ThinEngine source), but our scene is unusable because all of the textures etc. come from another context. Is there some way we’re supposed to be handling these gracefully and the loss of context is a standard occurrence now? Is there something we’re doing wrong that is causing this to happen, or is this just something in ios 14.0 and we should wait for ios 14.2 or perhaps a move to Babylon 4.2 when that releases?
I’ve managed to fix this issue, but updating here in case it is relevant to the Babylon internals or future devs dealing with the same issue.
The issue here is passing a uniform with the wrong type, or changing the type between updates, and then using that uniform value in the sin() function in GLSL code. For us, this was using Number.isInteger() to determine whether to call setInt or setFloat for some runtime/config driven uniform values. If you’ve used this in the sin function with multiple data types, the context will be lost when engine.resize() is called (likely in your window resize event handler).
TL;DR don’t try to infer the type at runtime, you need to keep track of this if you have any system for generically handling uniform values.
We’ve seen this as well for users who have upgraded to Safari 14.0. Right now we’re seeing the browser crash completely on Safari 14.0 on both iOS and desktop. The issue goes away (on some users) if we enable WebGL 2.0.
From the conversations you’re seeing here, do you think there’s a fix that is possible in Babylon, or do we just have to wait for Safari to update with a patch? We’re weighing whether or not to just disable 3D for these users.
Over on the ThreeJS issues page, they claim to have been able to reproduce the issue without creating the WebGL instance. Just resizing canvas enough times causes the crash. They’ve logged a bug with Apple but I’m not seeing much activity on that ticket.
I don’t - after fixing my uniform issue, I can rotate the canvas (via switching between portrait / landscape) as frequently as I want and the context is never lost.
Perhaps the root cause of my issue is different than yours, but we’re using a lot of different functionality - PBRCustomShader, ShaderMaterial, CustomProceduralTexture, with animated uniforms, etc. and I was able to get it stable (after pulling my hair out for a week and a half).