{"record":{"id":"5d37c646312ecc78","repo":"BabylonJS/Babylon.js","slug":"unable-to-create-index-buffer","errorCode":null,"errorMessage":"Unable to create index buffer","messagePattern":"Unable to create index buffer","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/dev/core/src/Engines/thinEngine.pure.ts","lineNumber":1368,"sourceCode":"\r\n    protected _resetIndexBufferBinding(): void {\r\n        this.bindIndexBuffer(null);\r\n        this._cachedIndexBuffer = null;\r\n    }\r\n\r\n    /**\r\n     * Creates a new index buffer\r\n     * @param indices defines the content of the index buffer\r\n     * @param updatable defines if the index buffer must be updatable\r\n     * @param _label defines the label of the buffer (for debug purpose)\r\n     * @returns a new webGL buffer\r\n     */\r\n    public createIndexBuffer(indices: IndicesArray, updatable?: boolean, _label?: string): DataBuffer {\r\n        const vbo = this._gl.createBuffer();\r\n        const dataBuffer = new WebGLDataBuffer(vbo);\r\n\r\n        if (!vbo) {\r\n            throw new Error(\"Unable to create index buffer\");\r\n        }\r\n\r\n        this.bindIndexBuffer(dataBuffer);\r\n\r\n        const data = this._normalizeIndexData(indices);\r\n        this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, data, updatable ? this._gl.DYNAMIC_DRAW : this._gl.STATIC_DRAW);\r\n        this._resetIndexBufferBinding();\r\n        dataBuffer.references = 1;\r\n        dataBuffer.is32Bits = data.BYTES_PER_ELEMENT === 4;\r\n        return dataBuffer;\r\n    }\r\n\r\n    protected _normalizeIndexData(indices: IndicesArray): Uint16Array | Uint32Array {\r\n        const bytesPerElement = (indices as Exclude<IndicesArray, number[]>).BYTES_PER_ELEMENT;\r\n        if (bytesPerElement === 2) {\r\n            return indices as Uint16Array;\r\n        }\r\n\r","sourceCodeStart":1350,"sourceCodeEnd":1386,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/core/src/Engines/thinEngine.pure.ts#L1350-L1386","documentation":"Thrown by createIndexBuffer when gl.createBuffer() returns null for an index buffer allocation — the driver failed to create the GL buffer object. Like error 108, it signals GPU resource exhaustion or a lost context, specific to the ELEMENT_ARRAY_BUFFER used for index data.","triggerScenarios":"gl.createBuffer() returning null while creating index buffers: GPU memory exhausted, context lost, driver limits hit — typically on scenes with very large index arrays (32-bit indices for high-poly meshes).","commonSituations":"High-poly meshes requiring 32-bit index buffers on low-VRAM devices; repeatedly creating index buffers for dynamic geometry without disposing the old ones; context loss on mobile during heavy scenes.","solutions":["Check gl.isContextLost() and handle context restoration if lost","Dispose unused meshes/index buffers to free GPU memory before retrying","Use 16-bit indices where vertex count < 65536 to shrink buffer size","Reuse existing DataBuffers (updateVertexBuffer patterns) instead of allocating new ones per frame"],"exampleFix":"// before\nmesh.setIndices(newIndices); // creates a new 32-bit index buffer each frame\n// after\nif (maxIndex < 65536) mesh.setIndices(newIndices, true); // updatable 16-bit buffer reused\nelse { oldBuffer.dispose(); mesh.setIndices(newIndices); }","handlingStrategy":"validation","validationCode":"function canAllocateBuffer(gl: WebGLRenderingContext): boolean {\n  return !gl.isContextLost() && gl.getError() === gl.NO_ERROR;\n}","typeGuard":"null","tryCatchPattern":"try {\n  const ib = engine.createIndexBuffer(indices);\n} catch (e) {\n  if ((e as Error).message === 'Unable to create index buffer') {\n    freeGpuMemory();\n    ib = engine.createIndexBuffer(indices); // retry once\n  } else throw e;\n}","preventionTips":["Use 16-bit indices when vertex count allows (< 65536)","Reuse and update existing index buffers instead of recreating them per frame","Dispose old geometry before uploading replacements","Handle context-lost events; lost contexts make createBuffer return null"],"tags":["webgl","gpu-memory","buffer","index-buffer"],"backgroundTag":"gpu-out-of-memory","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}