phaserjs/phaser · error · Error

Invalid buffer type for ELEMENT_ARRAY_BUFFER

Error message

Invalid buffer type for ELEMENT_ARRAY_BUFFER

What it means

Thrown by WebGLGlobalWrapper.updateBindingsElementArrayBuffer when the buffer bound as ELEMENT_ARRAY_BUFFER has a bufferType other than gl.ELEMENT_ARRAY_BUFFER. It is the index-buffer counterpart of the ARRAY_BUFFER check, preventing a vertex buffer from being bound where indices are expected.

Source

Thrown at src/renderer/webgl/wrappers/WebGLGlobalWrapper.js:246

    /**
     * Updates the index array buffer state.
     *
     * @method Phaser.Renderer.WebGL.Wrappers.WebGLGlobalWrapper#updateBindingsElementArrayBuffer
     * @since 4.0.0
     * @param {Phaser.Types.Renderer.WebGL.WebGLGlobalParameters} state - The state to set.
     * @param {boolean} [force=false] - If `true`, the state will be set regardless of the current state.
     */
    updateBindingsElementArrayBuffer: function (state, force)
    {
        var elementArrayBuffer = state.bindings.elementArrayBuffer;
        var gl = this.renderer.gl;
        if (
            elementArrayBuffer !== null &&
            elementArrayBuffer.bufferType !== gl.ELEMENT_ARRAY_BUFFER
        )
        {
            throw new Error('Invalid buffer type for ELEMENT_ARRAY_BUFFER');
        }

        var different = elementArrayBuffer !== this.state.bindings.elementArrayBuffer;

        if (different)
        {
            this.state.bindings.elementArrayBuffer = elementArrayBuffer;
        }
        if (different || force)
        {
            var webGLBuffer = elementArrayBuffer ? elementArrayBuffer.webGLBuffer : null;
            gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, webGLBuffer);
        }
    },

    /**
     * Updates the framebuffer state.
     *

View on GitHub (pinned to 41be1e462b)

Solutions

  1. Use renderer.createIndexBuffer (bufferType = ELEMENT_ARRAY_BUFFER) for index data.
  2. Check bufferType before binding: if (buf.bufferType === gl.ELEMENT_ARRAY_BUFFER).
  3. Re-audit the geometry setup to ensure vertex and index buffers are created with the correct helpers.
  4. If sharing data, allocate two correctly-typed buffers instead of reusing one.

Example fix

// before
const ibo = renderer.createVertexBuffer(indices);
glWrapper.updateBindingsElementArrayBuffer({ bindings: { elementArrayBuffer: ibo } }); // throws

// after
const ibo = renderer.createIndexBuffer(indices);
glWrapper.updateBindingsElementArrayBuffer({ bindings: { elementArrayBuffer: ibo } });
Defensive patterns

Strategy: type-guard

Validate before calling

function bindElementBuffer(glWrapper, gl, buf) {
  if (buf && buf.bufferType !== gl.ELEMENT_ARRAY_BUFFER) {
    throw new TypeError('Expected ELEMENT_ARRAY_BUFFER-typed wrapper');
  }
  glWrapper.updateBindingsElementArrayBuffer({ bindings: { elementArrayBuffer: buf } });
}

Type guard

function isElementArrayBufferWrapper(buf, gl) { return buf == null || buf.bufferType === gl.ELEMENT_ARRAY_BUFFER; }

Prevention

When it happens

Trigger: Passing a vertex-typed WebGLBufferWrapper into the elementArrayBuffer slot of a state update; creating a buffer with createVertexBuffer and binding it as an index buffer.

Common situations: Custom geometry code that hands a vertex buffer to the element binding; copy-paste errors when wiring up buffer bindings; confusion between createVertexBuffer and createIndexBuffer.

Related errors


AI-assisted analysis of phaserjs/phaser@41be1e462b (2026-08-13). Data as JSON: /api/errors/b686a7624b999d0b. Report an issue: GitHub.