# BABYLON.WebGPUEngine and OffscreenCanvas: Roadmap for Support?

**URL:** <https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312>\
**Category:** Questions\
**Tags:** webgpu, offscreencanvas\
**Created:** [January 17, 2024, 9:49pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312 "2024-01-17T21:49:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![webgrid](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/webgrid/32/24940_2.png) [@webgrid](https://forum.babylonjs.com/u/webgrid)\
**Post date:** [January 17, 2024, 9:49pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/1 "2024-01-17T21:49:02Z")

</div>

Hi again,

As my game project progresses with effects, animations, and many meshes, I need to work more closely with the metal 🙂

I’ve been examining Babylon.WebGPUEngine and noticed its constructor currently accepts (canvas: HTMLCanvasElement, options?: WebGPUEngineOptions).

Is there a way to use an OffscreenCanvas with the WebGPUEngine currently? Also, are there any plans to implement this feature?

Thank you again for all your excellent work you are doing 🥳

WebGPU in a Worker example: [https://webgpu.github.io/webgpu-samples/samples/worker](https://webgpu.github.io/webgpu-samples/samples/worker)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 17, 2024, 10:04pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/2 "2024-01-17T22:04:37Z")

</div>

cc @Evgeni_Popov, I guess it should be pretty similar to adding the correct typings.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 18, 2024, 10:36am UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/3 "2024-01-18T10:36:34Z")

</div>

This PR adds the support for `OffscreenCanvas` in the `WebGPUEngine` constructor:

> <https://github.com/BabylonJS/Babylon.js/pull/14714>
>
> See https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadm…ap-for-support/47312

---

<div class="post-metadata">

**Author:** ![webgrid](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/webgrid/32/24940_2.png) [@webgrid](https://forum.babylonjs.com/u/webgrid)\
**Post date:** [January 18, 2024, 12:20pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/4 "2024-01-18T12:20:52Z")

</div>

Thank you 😊

---

<div class="post-metadata">

**Author:** ![webgrid](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/webgrid/32/24940_2.png) [@webgrid](https://forum.babylonjs.com/u/webgrid)\
**Post date:** [January 31, 2024, 12:58pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/5 "2024-01-31T12:58:44Z")

</div>

WebGPU won’t display anything on the offscreencanvas, and it works fine for WebGL2. Do we have any Babylon.WebGPUEngine with OffscreenCanvas ? I would love to have a look at that 🙂

Here is the setup for Babylon.Engine:

```auto
if (WEBGPU_ENABLED) {
    const engine = new BABYLON.WebGPUEngine(this.offscreenCanvas);
    void engine.initAsync().then(() => {
        this.engine = engine;

        this.loadScene();
    });
}
else {
    this.engine = new BABYLON.Engine(this.offscreenCanvas, true);

    this.loadScene();
}

```

The configuration for the scene:

`BABYLON.Scene(this.engine, { useGeometryUniqueIdsMap: true, useClonedMeshMap: true, useMaterialMeshMap: true });`

and a `BABYLON.ArcRotateCamera` and a `BABYLON.HemisphericLight`

Update: I noticed `scene.clearColor = BABYLON.Color4.FromHexString(`#AAAAAA`);` has no effect for WebGPUEngine and works as expected for WebGL

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 31, 2024, 4:10pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/6 "2024-01-31T16:10:31Z")

</div>

It does work for me in this PG:

> **[Babylon.js Playground](https://playground.babylonjs.com/#D73ZT5%233)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

After 1s, the offscreen canvas will be converted to a data url, and this url will be used as the source for an IMG element displayed at the top/right of the screen.

`scene.clearColor = BABYLON.Color4.FromHexString("#AAAAAA");` does work (see PG).

---

<div class="post-metadata">

**Author:** ![webgrid](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/webgrid/32/24940_2.png) [@webgrid](https://forum.babylonjs.com/u/webgrid)\
**Post date:** [January 31, 2024, 4:59pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/7 "2024-01-31T16:59:54Z")

</div>

Thanks for the confirmation and I will look into the issue and report back when I found the issue on my end 🙂

---

<div class="post-metadata">

**Author:** ![webgrid](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/webgrid/32/24940_2.png) [@webgrid](https://forum.babylonjs.com/u/webgrid)\
**Post date:** [January 31, 2024, 8:26pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/8 "2024-01-31T20:26:28Z")

</div>

I found the issue, and it seems that WebGPU requires to use:

```auto
engine.runRenderLoop(() => { scene.render(); }); //Works for WebGPU/WebGL

```

while WebGL can use a render outside the runRenderLoop:

```auto
engine.stopRenderLoop();
setInterval(() => { scene.render(); }, 100); //works only for WebGL

```

I have made an demo about it here: [BABYLON.JS web worker](https://www.mudgate.com/misc/demo.htm)

Is this behavior by design ?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 31, 2024, 8:47pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/9 "2024-01-31T20:47:40Z")

</div>

In WebGPU, everything should happen between `engine.beginFrame()` / `engine.endFrame()` (it happens that WebGL doesn’t do anything critical in these functions, that’s why you can omit them - but it’s still better to call them). When using a render loop, it is done for you. Else, you should do these calls yourself:

```javascript
engine.beginFrame();
scene.render();
engine.endFrame();

```

---

<div class="post-metadata">

**Author:** ![webgrid](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/webgrid/32/24940_2.png) [@webgrid](https://forum.babylonjs.com/u/webgrid)\
**Post date:** [January 31, 2024, 9:24pm UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/10 "2024-01-31T21:24:11Z")

</div>

Superb! That fixed it 🙂

Thank you

---

<div class="post-metadata">

**Author:** ![webgrid](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/webgrid/32/24940_2.png) [@webgrid](https://forum.babylonjs.com/u/webgrid)\
**Post date:** [February 29, 2024, 7:27am UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/11 "2024-02-29T07:27:43Z")

</div>

I’ve added WebGPU support now and this is my notes:

When I added support for WebGPU, application to loading my Babylon engine into a worker and the canvas as an OffscreenCanvas, I encountered a few issues that did not occur with WebGL:

- Since my canvas may resize, I had to use Babylon.WebGPUEngine.resize(true); otherwise, it would not display and would print numerous warning messages in the console.

- For my custom render method, I had to add beginFrame, render, endFrame.

- I can’t set Babylon.Scene.autoClearDepthAndStencil to false in WebGPU mode

- WebGPU shaders strictly require that attributes be declared, for example: ‘Vertex attribute slot 1 used in ([ShaderModule], [EntryPoint “main”]) is not present in the VertexState.’

WebGL remains my default renderer as WebGPU does not significantly improve performance in compatibility mode. Some of my games run in non-compatibility mode, where I see a performance gain, but it is not yet ready for production release.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [February 29, 2024, 9:33am UTC](https://forum.babylonjs.com/t/babylon-webgpuengine-and-offscreencanvas-roadmap-for-support/47312/12 "2024-02-29T09:33:43Z")

</div>

For 1/ and 2/, it’s more like it should also be done in WebGL, but that for some reasons it still works without doing it (for 2/, it works because WebGL does nothing substantial in being/end frame).

For 3/, I’m not sure why it would not work… If you have a repro, we can have a look.

For 4/ it’s expected, WebGPU is more strict than WebGL in this regard.

I’m happy to know that you can get some performance gain when using the non-compatibility mode in WebGPU!
