WebGPU: after a device loss the scene stays black (meshes keep their old index/vertex buffers), follow-up to #18962

Hi @Evgeni_Popov and all! I hope you don’t mind all these bug posts. We’ve been profiling our game very stringently over the last few weeks, and we found a few. Thank you so much for Babylon.js, we love it!

This one is a small follow-up to #18962 ( Feat: Gaussian Point Splatting (WebGPU) by raymondyfei · Pull Request #18962 · BabylonJS/Babylon.js · GitHub ). Besides Gaussian Point Splatting, that PR made the WebGPU context-loss restore wait for the new GPU device before rebuilding. Thank you for that change, it was the first half of the problem we had seen. On 9.32.0 the restore now gets much further, but when the device is lost, the scene still doesn’t come back: the canvas stays black, even though onContextRestoredObservable fires.

In short: after a device loss, the WebGPU engine never recreates the meshes’ index and vertex buffers. Shaders, uniform buffers and textures are rebuilt on the new device, but each mesh still draws with buffers that belong to the old one. WebGPU rejects that, so every frame fails with a validation error and nothing is drawn. On WebGL, Engine._rebuildBuffers takes care of this by calling scene._rebuildGeometries(). But WebGPUEngine stopped extending Engine in 7.2.3, and its own _rebuildBuffers override never makes that call.

The fix is a few lines in WebGPUEngine._rebuildBuffers. Details, numbers and a suggested patch are below.

Repro (Playground, WebGPU engine): https://playground.babylonjs.com/?webgpu#SO3MD6#1

Choose WebGPU in the engine menu (or add ?webgpu to the URL), then press Lose device, which calls engine._device.destroy(). The panel is plain HTML on top of the canvas, so you can still read it while the canvas is black. It shows whether the restored event fired, how many WebGPU validation errors there have been since the loss (counted with uncapturederror listeners on every device), and two pixels read back with engine.readPixels. Apply fix patches _rebuildBuffers as suggested below. With the fix on, press Lose device again and the scene comes back (it also comes back after a second loss).

Measured (Chrome 156, Intel Iris Xe on Linux; Playground-like page, 4 s after engine._device.destroy()):

9.31.0 9.32.0 9.32.0 with the fix
onContextRestoredObservable fires yes yes yes
Canvas black black scene back
WebGPU validation errors 0 ~470 in 4 s (2 per frame) 0
Mesh index buffer recreated no no yes
What stops it the restore runs before the new device exists; the render loop then dies on TypeError: Cannot read properties of undefined (reading '0') each frame binds the old device’s index buffer nothing

The first error on 9.32.0, then one per frame for each draw:

[Buffer "BabylonWebGPUDevice0_DataBufferUniqueId=11-Geometry_<id>_IndexBuffer_CopyDst_Index_Storage_size27744"]
is associated with [Device "BabylonWebGPUDevice0"], and cannot be used with [Device "BabylonWebGPUDevice1"].
 - While encoding [RenderPassEncoder "[74|1] - MainRenderPass"].SetIndexBuffer(...)
[Invalid CommandBuffer from CommandEncoder "[74|0] - RenderEncoder"] is invalid due to a previous error.

After the restore, every geometry’s _indexBuffer and vertex buffers still have the uniqueIds they had before the loss. The fix alone doesn’t help on 9.31.0, which still rebuilds before the new device exists, so #18962’s change and this one are both needed.

(Screenshot attached: 9.32.0 as shipped, and with the fix.)

Even smaller repro (console, on any WebGPU Playground with a mesh):

const geometry = scene.meshes.find((m) => m.getTotalIndices() > 0).geometry;
const oldIndexBuffer = geometry.getIndexBuffer();
engine.onContextRestoredObservable.addOnce(() => {
    console.log("index buffer rebuilt on the new device:", geometry.getIndexBuffer() !== oldIndexBuffer); // false on 9.32.0
});
engine._device.destroy();

Where (master at c9e77a1189, unchanged since 9.32.0):

Engine._rebuildBuffers (Engines/engine.pure.ts lines 543-556) rebuilds the geometries, then calls super:

protected override _rebuildBuffers(): void {
    // Index / Vertex
    for (const scene of this.scenes) {
        scene.resetCachedMaterial();
        scene._rebuildGeometries();
    }
    for (const scene of this._virtualScenes) {
        scene.resetCachedMaterial();
        scene._rebuildGeometries();
    }
    super._rebuildBuffers();
}

WebGPUEngine extends ThinWebGPUEngine → AbstractEngine, so its override (Engines/webgpuEngine.pure.ts lines 1276-1285) reaches AbstractEngine._rebuildBuffers (abstractEngine.pure.ts lines 1919-1924), which only rebuilds uniform buffers:

protected override _rebuildBuffers(): void {
    super._rebuildBuffers();   // AbstractEngine: uniform buffers only

    for (const storageBuffer of this._storageBuffers) {
        // The buffer can already be rebuilt by the call to _rebuildGeometries(), which recreates the storage buffers for the ComputeShaderParticleSystem
        if ((storageBuffer.getBuffer() as WebGPUDataBuffer).engineId !== this.uniqueId) {
            storageBuffer._rebuild();
        }
    }
}

The comment shows _rebuildGeometries() was expected to have run. Up to 7.2.2 WebGPUEngine extended Engine, so it did; from 7.2.3 on it doesn’t, and nothing else calls it on the WebGPU path. (_rebuildGraphicsResources, abstractEngine.pure.ts lines 700-723, calls _rebuildBuffers once the new device is ready, since #18962.) The restoration unit test from #18962 uses a NullEngine, which extends Engine, so it couldn’t catch this.

Suggested fix: do the same geometry pass in WebGPUEngine._rebuildBuffers, before the storage-buffer loop:

protected override _rebuildBuffers(): void {
    // Index / Vertex (as in Engine._rebuildBuffers, which WebGPUEngine no longer inherits)
    for (const scene of this.scenes) {
        scene.resetCachedMaterial();
        scene._rebuildGeometries();
    }
    for (const scene of this._virtualScenes) {
        scene.resetCachedMaterial();
        scene._rebuildGeometries();
    }

    super._rebuildBuffers();

    for (const storageBuffer of this._storageBuffers) {
        // unchanged
    }
}

scene._rebuildGeometries() also rebuilds instance buffers (mesh._rebuild()), the post-process manager, scene components, particle systems and sprite managers, so the fix covers those too. Another option is to move the geometry loop into AbstractEngine._rebuildBuffers and drop Engine’s override, as AbstractEngine._rebuildTextures already does for textures. The repro’s “Apply fix” button does the first version, and with it the scene comes back with 0 validation errors.

Our workaround in the meantime: we don’t rely on the restore. On a WebGPU device loss, our game stops its render loop and asks the player to reload.

Happy to open a PR with this fix and a unit test (WebGPUEngine.prototype._rebuildBuffers called on a stub engine with a stub scene, checking that _rebuildGeometries runs) if that helps.

Thanks for the detailed report and repro! I’ve reproduced the issue on 9.32.0 and confirmed your diagnosis: the restored event fires, but the geometries retain their old index and vertex buffers, which WebGPU rejects because they belong to the previous device.

Your proposed fix works in the supplied Playground. With the geometry rebuild added, all four geometries receive new index and vertex buffers, rendering returns, and there are no validation errors. A subsequent device loss also restores correctly.

This is a longstanding omission from when WebGPUEngine stopped inheriting from Engine, rather than a regression introduced by #18962. That PR fixed the asynchronous initialization ordering; the missing geometry rebuild is the remaining issue.

A PR would be welcome! Adding the regular and virtual scene rebuild loops to WebGPUEngine._rebuildBuffers, before super._rebuildBuffers(), looks like the most focused fix. Please include a regression test exercising the WebGPU override directly—the existing restoration tests check initialization ordering but don’t exercise actual geometry rebuilding.

Thanks again for tracking this down!

Thanks so much for reproducing it and confirming, @Evgeni_Popov, and for the clarification about #18962! The PR is up: Rebuild scene geometries when the WebGPU engine restores its buffers by christabone · Pull Request #19055 · BabylonJS/Babylon.js · GitHub

It adds the regular and virtual scene rebuild loops to WebGPUEngine._rebuildBuffers, before super._rebuildBuffers(), and a regression test that calls the WebGPU override directly on a stub engine. It checks that resetCachedMaterial and _rebuildGeometries run for both scenes before the uniform buffers, and that the storage-buffer loop still skips a buffer the geometry rebuild already recreated. The test fails without the fix.