From 901e24c46f8db0cd91306a1c8b5520b20a0889b5 Mon Sep 17 00:00:00 2001 From: Martin Valigursky Date: Tue, 19 Nov 2024 12:08:29 +0000 Subject: [PATCH] [Fix] Improve handling of lost device on WebGL --- src/platform/graphics/webgl/webgl-shader.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/platform/graphics/webgl/webgl-shader.js b/src/platform/graphics/webgl/webgl-shader.js index b37f0f4a86a..c184fa5c910 100644 --- a/src/platform/graphics/webgl/webgl-shader.js +++ b/src/platform/graphics/webgl/webgl-shader.js @@ -192,12 +192,17 @@ class WebglShader { * @param {string} src - The shader source code. * @param {boolean} isVertexShader - True if the shader is a vertex shader, false if it is a * fragment shader. - * @returns {WebGLShader} The compiled shader. + * @returns {WebGLShader|null} The compiled shader, or null if the device is lost. * @private */ _compileShaderSource(device, src, isVertexShader) { const gl = device.gl; + // if the device is lost, silently ignore + if (gl.isContextLost()) { + return null; + } + // device cache for current device, containing cache of compiled shaders const shaderDeviceCache = isVertexShader ? _vertexShaderCache : _fragmentShaderCache; const shaderCache = shaderDeviceCache.get(device, () => { @@ -218,11 +223,6 @@ class WebglShader { glShader = gl.createShader(isVertexShader ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER); - // if the device is lost, silently ignore - if (!glShader && gl.isContextLost()) { - return glShader; - } - gl.shaderSource(glShader, src); gl.compileShader(glShader);