{"record":{"id":"f8c592fbe1a0c0d9","repo":"phaserjs/phaser","slug":"buffer-too-small-for-layout","errorCode":null,"errorMessage":"Buffer too small for layout","messagePattern":"Buffer too small for layout","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/renderer/webgl/wrappers/WebGLVertexBufferLayoutWrapper.js","lineNumber":61,"sourceCode":"        /**\n         * The completed attribute buffer layout, describing the stride,\n         * attributes, their types, sizes, byte counts, and byte offsets\n         * within the vertex buffer.\n         *\n         * @name Phaser.Renderer.WebGL.Wrappers.WebGLVertexBufferLayoutWrapper#layout\n         * @type {Phaser.Types.Renderer.WebGL.WebGLAttributeBufferLayout}\n         * @since 4.0.0\n         */\n        this.layout = layout;\n\n        // Fill in the layout with the stride\n        // and per-attribute bytes and offset.\n        this.completeLayout(layout);\n\n        var bufferSize = layout.stride * layout.count;\n        if (buffer && buffer.byteLength < bufferSize)\n        {\n            throw new Error('Buffer too small for layout');\n        }\n\n        /**\n         * The WebGLBufferWrapper holding the vertex data for this layout.\n         *\n         * @name Phaser.Renderer.WebGL.Wrappers.WebGLVertexBufferLayoutWrapper#buffer\n         * @type {Phaser.Renderer.WebGL.Wrappers.WebGLBufferWrapper}\n         * @since 4.0.0\n         */\n        this.buffer = buffer || renderer.createVertexBuffer(new ArrayBuffer(bufferSize), layout.usage);\n    },\n\n    /**\n     * Complete the layout of the provided attribute buffer layout.\n     * This will fill in the stride, byte counts, and offsets.\n     * In addition, it will convert any GLenums specified as strings\n     * to their numeric values.\n     * This mutates the layout.","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/renderer/webgl/wrappers/WebGLVertexBufferLayoutWrapper.js#L43-L79","documentation":"Thrown by WebGLVertexBufferLayoutWrapper when a buffer argument is supplied whose byteLength is smaller than the required bufferSize = layout.stride * layout.count. The wrapper refuses to under-allocate because binding a too-small buffer to the described attributes would read out of bounds at draw time.","triggerScenarios":"Constructing WebGLVertexBufferLayoutWrapper with an explicit buffer that was allocated for fewer vertices than layout.count, or a layout whose count/stride grew after the buffer was created; passing a shared/recycled buffer that is too small for the new layout.","commonSituations":"Pre-allocating a vertex buffer for N sprites then increasing layout.count; reuse of a buffer across layouts with different stride; off-by-one in count when sizing the ArrayBuffer; changing the attribute set (and thus stride) without re-sizing the buffer.","solutions":["Size the buffer to at least layout.stride * layout.count bytes.","If you want the wrapper to allocate for you, omit the buffer argument so it creates one of the correct size.","Recompute layout.count when the capacity changes and reallocate the buffer to match.","Audit stride math (sum of attribute sizes * components) to ensure stride is not inflated, forcing an oversized buffer."],"exampleFix":"// before\nconst buf = renderer.createVertexBuffer(new ArrayBuffer(1024)); // too small\nnew WebGLVertexBufferLayoutWrapper(renderer, { stride: 16, count: 100, attributes: [...] }, buf);\n\n// after\nconst buf = renderer.createVertexBuffer(new ArrayBuffer(16 * 100));\nnew WebGLVertexBufferLayoutWrapper(renderer, { stride: 16, count: 100, attributes: [...] }, buf);\n// or omit buffer to auto-allocate","handlingStrategy":"validation","validationCode":"function assertBufferBigEnough(buffer, stride, count) {\n  const need = stride * count;\n  if (buffer && buffer.byteLength < need) {\n    throw new RangeError(`Buffer ${buffer.byteLength} < required ${need}`);\n  }\n}","typeGuard":"function bufferFitsLayout(buffer, layout) { return !buffer || buffer.byteLength >= layout.stride * layout.count; }","tryCatchPattern":null,"preventionTips":["Let the wrapper allocate the buffer when in doubt (omit the buffer arg).","Recompute buffer size whenever layout.count or stride changes.","Centralize stride computation to avoid inflation."],"tags":["renderer","webgl","buffer","vertex-layout","phaser4"],"backgroundTag":null,"analyzedSha":"41be1e462bc600064e498cba370bfa8c5c055a22","analyzedAt":"2026-08-13T04:23:39.729Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}