{"record":{"id":"3dcfc7804f76ded9","repo":"BabylonJS/Babylon.js","slug":"something-went-wrong-while-creating-a-gl-type-s","errorCode":null,"errorMessage":"Something went wrong while creating a gl ${type} shader object. gl error=${error}, gl isContextLost=${gl.isContextLost()}, _contextWasLost=${_contextWasLost}","messagePattern":"Something went wrong while creating a gl (.+?) shader object\\. gl error=(.+?), gl isContextLost=(.+?), _contextWasLost=(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/dev/core/src/Engines/thinEngine.functions.ts","lineNumber":338,"sourceCode":"\r\n    onReady();\r\n}\r\n\r\nfunction CompileShader(source: string, type: string, defines: Nullable<string>, shaderVersion: string, gl: WebGLContext, _contextWasLost?: boolean): WebGLShader {\r\n    return CompileRawShader(_ConcatenateShader(source, defines, shaderVersion), type, gl, _contextWasLost);\r\n}\r\n\r\nfunction CompileRawShader(source: string, type: string, gl: WebGLContext, _contextWasLost?: boolean): WebGLShader {\r\n    const shader = gl.createShader(type === \"vertex\" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);\r\n\r\n    if (!shader) {\r\n        let error: GLenum = gl.NO_ERROR;\r\n        let tempError: GLenum;\r\n        while ((tempError = gl.getError()) !== gl.NO_ERROR) {\r\n            error = tempError;\r\n        }\r\n\r\n        throw new Error(\r\n            `Something went wrong while creating a gl ${type} shader object. gl error=${error}, gl isContextLost=${gl.isContextLost()}, _contextWasLost=${_contextWasLost}`\r\n        );\r\n    }\r\n\r\n    gl.shaderSource(shader, source);\r\n    gl.compileShader(shader);\r\n\r\n    return shader;\r\n}\r\n\r\n/**\r\n * @internal\r\n */\r\nexport function _setProgram(program: Nullable<WebGLProgram>, gl: WebGLContext): void {\r\n    gl.useProgram(program);\r\n}\r\n\r\n/**\r","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/core/src/Engines/thinEngine.functions.ts#L320-L356","documentation":"Thrown by CompileRawShader when gl.createShader(type) returned null/failed — the engine drains gl.getError() and reports the last GL error code, whether the context is lost, and the engine's internal _contextWasLost flag. This means the GL driver refused to create the shader object itself (not a GLSL compile failure).","triggerScenarios":"gl.createShader returns null: WebGL context lost (GPU reset, driver crash), GL error state like OUT_OF_MEMORY, or calling shader compilation after the context was disposed.","commonSituations":"Laptop GPUs resetting on driver crash or overheat; mobile browsers losing context on backgrounding; too many shaders/buffers exhausting GPU memory; creating an engine during page teardown.","solutions":["Check the reported isContextLost value; if true, listen for the engine's onContextLost/ContextLostObservable and re-create the engine","Free GPU resources (dispose unused materials/textures) and retry when the error is OUT_OF_MEMORY","Reload the scene/engine after handling onContextLost — a lost WebGL context is usually unrecoverable in-place","Verify the engine was not disposed before shader compilation"],"exampleFix":"// before\nengine.createEffect(...); // throws if context was lost\n// after\nengine.onContextLostObservable.add(() => recreateEngine());\nif (!engine._gl.isContextLost()) { engine.createEffect(...); }","handlingStrategy":"retry","validationCode":"function canCompile(engine: { _gl: WebGLRenderingContext }): boolean {\n  return !!engine._gl && !engine._gl.isContextLost() && engine._gl.getError() === engine._gl.NO_ERROR;\n}","typeGuard":"function isContextLostError(e: unknown): boolean {\n  return e instanceof Error && e.message.includes('isContextLost=true');\n}","tryCatchPattern":"try {\n  engine.createEffect(...);\n} catch (e) {\n  if (isContextLostError(e)) {\n    engine.onContextLostObservable.addOnce(() => setTimeout(rebuildScene, 100));\n  } else throw e;\n}","preventionTips":["Register engine.onContextLostObservable and onContextRestoredObservable handlers at startup","Cap scene GPU memory; dispose unused resources","Avoid heavy GPU load that triggers driver resets (TDR) on Windows","Do not compile shaders during page teardown or after engine.dispose()"],"tags":["webgl","context-lost","gpu"],"backgroundTag":"webgl-context-lost","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}