{"record":{"id":"aefa7bf6cc91662e","repo":"BabylonJS/Babylon.js","slug":"unable-to-create-instance-buffer","errorCode":null,"errorMessage":"Unable to create instance buffer","messagePattern":"Unable to create instance buffer","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/core/src/Engines/engine.pure.ts","lineNumber":977,"sourceCode":"                gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE);\r\n            }\r\n\r\n            this._bindTextureDirectly(this._gl.TEXTURE_2D, null);\r\n        }\r\n\r\n        texture._comparisonFunction = comparisonFunction;\r\n    }\r\n\r\n    /**\r\n     * Creates a webGL buffer to use with instantiation\r\n     * @param capacity defines the size of the buffer\r\n     * @returns the webGL buffer\r\n     */\r\n    public createInstancesBuffer(capacity: number): DataBuffer {\r\n        const buffer = this._gl.createBuffer();\r\n\r\n        if (!buffer) {\r\n            throw new Error(\"Unable to create instance buffer\");\r\n        }\r\n\r\n        const result = new WebGLDataBuffer(buffer);\r\n        result.capacity = capacity;\r\n\r\n        this.bindArrayBuffer(result);\r\n        this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);\r\n\r\n        result.references = 1;\r\n\r\n        return result;\r\n    }\r\n\r\n    /**\r\n     * Delete a webGL buffer used with instantiation\r\n     * @param buffer defines the webGL buffer to delete\r\n     */\r\n    public deleteInstancesBuffer(buffer: WebGLBuffer): void {\r","sourceCodeStart":959,"sourceCodeEnd":995,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/core/src/Engines/engine.pure.ts#L959-L995","documentation":"createInstancesBuffer() calls the underlying WebGL context's createBuffer(); when the GL driver fails to allocate a buffer (returns null) the engine throws this error. It almost always indicates GPU/driver resource exhaustion or a lost context, since instance buffers are ordinary GL buffer objects.","triggerScenarios":"engine.createInstancesBuffer(capacity) when gl.createBuffer() returns null — context is lost, or the driver has run out of buffer allocations / memory.","commonSituations":"Creating buffers after a webglcontextlost event, extremely large capacity requests exhausting GPU memory, embedded/mobile drivers with strict buffer limits, or too many live buffers never released.","solutions":["Check whether the WebGL context is lost (gl.isContextLost()) and re-initialize the engine before creating instance buffers.","Reduce the number/capacity of live GPU buffers and dispose unused ones (buffer.dispose()) before allocating.","Add a context-loss handler and recreate resources on restore.","Retry the allocation after freeing resources."],"exampleFix":"// before\nconst instBuf = engine.createInstancesBuffer(4 * floats);\n// after\nif (engine._gl.isContextLost()) {\n  engine.dispose();\n  engine = new Engine(canvas, true);\n}\nconst instBuf = engine.createInstancesBuffer(4 * floats);","handlingStrategy":"try-catch","validationCode":"if (engine._gl.isContextLost()) {\n  throw new Error(\"skip: context lost, recreate engine first\");\n}","typeGuard":"function canAllocateBuffers(engine) {\n  const gl = engine._gl;\n  return !!gl && !gl.isContextLost();\n}","tryCatchPattern":"try {\n  instanceBuffer = engine.createInstancesBuffer(capacity);\n} catch (e) {\n  if (/Unable to create instance buffer/.test(e.message)) {\n    disposeUnusedBuffers(engine);\n    instanceBuffer = engine.createInstancesBuffer(capacity);\n  }\n}","preventionTips":["Dispose buffers and instance buffers when meshes are removed.","Install webglcontextlost/restored handlers.","Cap the number of concurrent instanced meshes."],"tags":["webgl","gpu-memory","buffers","resource-exhaustion"],"backgroundTag":"gpu-buffer-allocation-failed","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}