BabylonJS/Babylon.js · critical

Unable to create dynamic uniform buffer

Error message

Unable to create dynamic uniform buffer

What it means

ThinEngine.createDynamicUniformBuffer calls WebGL's gl.createBuffer() to allocate a GPU buffer for dynamic uniform data. If the WebGL context returns null (buffer creation failed), the engine throws this error. This typically means the WebGL context is lost, exhausted, or the driver refused the allocation.

Source

Thrown at packages/dev/core/src/Engines/Extensions/engine.uniformBuffer.pure.ts:47

        this.bindUniformBuffer(result);

        if (elements instanceof Float32Array) {
            this._gl.bufferData(this._gl.UNIFORM_BUFFER, <Float32Array>elements, this._gl.STATIC_DRAW);
        } else {
            this._gl.bufferData(this._gl.UNIFORM_BUFFER, new Float32Array(elements), this._gl.STATIC_DRAW);
        }

        this.bindUniformBuffer(null);

        result.references = 1;
        return result;
    };

    ThinEngine.prototype.createDynamicUniformBuffer = function (elements: FloatArray, _label?: string): DataBuffer {
        const ubo = this._gl.createBuffer();

        if (!ubo) {
            throw new Error("Unable to create dynamic uniform buffer");
        }

        const result = new WebGLDataBuffer(ubo);
        this.bindUniformBuffer(result);

        if (elements instanceof Float32Array) {
            this._gl.bufferData(this._gl.UNIFORM_BUFFER, <Float32Array>elements, this._gl.DYNAMIC_DRAW);
        } else {
            this._gl.bufferData(this._gl.UNIFORM_BUFFER, new Float32Array(elements), this._gl.DYNAMIC_DRAW);
        }

        this.bindUniformBuffer(null);

        result.references = 1;
        return result;
    };

    ThinEngine.prototype.updateUniformBuffer = function (uniformBuffer: DataBuffer, elements: FloatArray, offset?: number, count?: number): void {

View on GitHub (pinned to 0592b347b8)

Solutions

  1. Listen for the engine's onContextLost / webglcontextlost event and re-create or restore the engine (engine.restoreDestroyedContext / reload) instead of continuing to allocate buffers
  2. Reduce GPU memory usage: fewer uniform buffers (merge uniforms, reduce skinned mesh count, lower instance counts), dispose unused buffers/textures with dispose()
  3. Check the context loss reason via the webglcontextlost event's statusMessage and verify the GPU isn't blacklisted (e.g. about:gpu in Chrome, Safari's WebGL vendor)
  4. Test with hardware acceleration enabled and updated GPU drivers; rule out software rendering fallbacks

Example fix

// before
const ubo = engine.createDynamicUniformBuffer(elements);
// after
engine.onContextLostObservable.add(() => {
  console.warn('WebGL context lost; pausing buffer creation');
});
if (!engine._gl.isContextLost()) {
  const ubo = engine.createDynamicUniformBuffer(elements);
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (engine._gl && !engine._gl.isContextLost()) {
  // safe to create uniform buffers
} else {
  await engineRestore(engine); // recreate engine / wait for context restore
}

Type guard

function canCreateGLBuffer(engine: ThinEngine): boolean {
  const gl = (engine as any)._gl as WebGL2RenderingContext | undefined;
  return !!gl && typeof gl.createBuffer === 'function' && !gl.isContextLost();
}

Try / catch

try {
  const ubo = engine.createDynamicUniformBuffer(elements);
} catch (e) {
  if (e.message === 'Unable to create dynamic uniform buffer') {
    // context lost or GPU memory exhausted: halt scene work, listen for
    // engine.onContextRestoredObservable, then rebuild
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling createDynamicUniformBuffer (directly or via UniformBuffer creation for dynamic uniforms, e.g. bones/morph targets) when gl.createBuffer() returns null — usually after a WebGL context loss (webglcontextlost), GPU memory exhaustion from too many buffers, or an invalid/lost rendering context.

Common situations: Scenes with huge numbers of skinned meshes or instances exhausting GPU buffer memory; browsers dropping WebGL contexts on tab memory pressure (mobile Safari/Chrome); hardware acceleration disabled or running on a software renderer (SwiftShader) with limits; a device that has just woken from sleep with a lost context.

Related errors


AI-assisted analysis of BabylonJS/Babylon.js@0592b347b8 (2026-08-30). Data as JSON: /api/errors/336940cd46a7b15d. Report an issue: GitHub.