I have code in my web game like this:
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const glContextAttributes = { alpha: true, antialias: false };
let gl = state.canvas.getContext('webgl2', glContextAttributes);
if (!gl) {
console.log('WebGL2 not supported, falling back to WebGL1.');
gl = state.canvas.getContext('webgl', glContextAttributes);
}
if (!gl) {
throw new Error('WebGL not supported in this browser.');
}
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
document.body.appendChild(canvas);
const shader = gl.createShader(gl.VERTEX_SHADER);
if (!shader) {
debugInfo.glError = gl.getError();
debugInfo.contextLost = gl.isContextLost();
throw new Error(`Failed to create ${gl.VERTEX_SHADER} shader. ${JSON.stringify(debugInfo)}`);
}
This works fine on my machine, and it works for most of my users, but I’m getting a trickle of emails from iOS Safari users that this exception is being thrown.
Failed to create 35633 shader. {“glError”:37442,”contextLost”:true}
For the users this is happening to, it happens 100% of the time. I have no idea how to help them.
How did the WebGL context get lost that fast? I just created the canvas. Not even a single turn of the event loop has elapsed. Is there anything I can do to prevent this (or even just mitigate it)?