# Uniform Buffer Object (UBO) and WebGPU

**URL:** <https://forum.babylonjs.com/t/uniform-buffer-object-ubo-and-webgpu/47210>\
**Category:** Questions\
**Created:** [January 14, 2024, 8:41pm UTC](https://forum.babylonjs.com/t/uniform-buffer-object-ubo-and-webgpu/47210 "2024-01-14T20:41:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![axeljaeger](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/axeljaeger/32/21445_2.png) [@axeljaeger](https://forum.babylonjs.com/u/axeljaeger)\
**Post date:** [January 14, 2024, 8:41pm UTC](https://forum.babylonjs.com/t/uniform-buffer-object-ubo-and-webgpu/47210/1 "2024-01-14T20:41:18Z")

</div>

Hello,  
I am currently porting a scientific application from WebGL to WebGPU:  
A simulation of an ultrasound phased array, for the ones interested:

- App: [Air coupled ultrasound array](https://axeljaeger.github.io/phased/)
- Source: [GitHub - axeljaeger/phased: Air coupled ultrasound array](https://github.com/axeljaeger/phased)

After finding out that I can continue using GLSL as a shading language, I try to touch these later, after get most of the stuff running.  
I am currently struggling getting a Uniform Buffer up and running. Because I am working for a while on this, I do already have a playground that more or less isolates the problem:  
(from [UniformBuffer.addUniform: Arguments unclear](https://forum.babylonjs.com/t/uniformbuffer-adduniform-arguments-unclear/29429))  
[https://playground.babylonjs.com/#1OH09K#1096](https://playground.babylonjs.com/#1OH09K#1096)

It shows a cube that is rendered using a custom material that consumes a Uniform Buffer. When switching this playground to WebGPU, I get the following error:

```auto
glslang.js:34 ERROR: 0:20: 'binding' : uniform/buffer blocks require layout(binding=X) 

```

I tried modifying the playground to:

- Add a `binding=1` to the layout qualifier of the UBO declaration in the shader.
- Add `shaderMaterial.getEffect().bindUniformBlock('myBuffer', 1)`

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

Looks like this is not enough. I get other cryptic error messages.

Does anyone know how to fix it and make the playground work or is there a playground with a working example of a UBO working with WebGPU?

Bonus question: Is a UBO still state of the art? I see that they are disabled by default on Mac and I remember from the desktop days that there is “Shader Storage Buffers”. Any comments on this?

Best regards and thanks to everyone,  
Axel

---

<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 15, 2024, 10:38am UTC](https://forum.babylonjs.com/t/uniform-buffer-object-ubo-and-webgpu/47210/2 "2024-01-15T10:38:57Z")

</div>

We only support a subset of the syntax used to declare an ubo in the shader when converting from WebGL to WebGPU.

See how I fixed your PG:

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

If you want more freedom, you can write the shader directly in [WGSL](https://doc.babylonjs.com/setup/support/webGPU/webGPUWGSL) (but this will only work with WebGPU, of course!).

> [@axeljaeger](#):
>
> Bonus question: Is a UBO still state of the art? I see that they are disabled by default on Mac and I remember from the desktop days that there is “Shader Storage Buffers”. Any comments on this?

Yes, ubo are still the fastest way to pass data to the shader, but they are limited to a few kilobytes of data (around 64k). If you want to pass more data, you will have to use a texture or a storage buffer (available only in WebGPU). It’s disabled on Mac because of a Safari bug (I’m not sure it’s disabled for all versions, though)…

---

<div class="post-metadata">

**Author:** ![axeljaeger](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/axeljaeger/32/21445_2.png) [@axeljaeger](https://forum.babylonjs.com/u/axeljaeger)\
**Post date:** [January 15, 2024, 6:49pm UTC](https://forum.babylonjs.com/t/uniform-buffer-object-ubo-and-webgpu/47210/3 "2024-01-15T18:49:39Z")

</div>

Thank you. So I see two big differences:

1. You made the shader declaration a bit simpler, removing the layout at all and removed the struct.
2. You set the buffer on every bind of the shader. Is this necessary? or is it just a bridge solution?

So my strategy will be: After now having all building (AFAIK), I will start porting my shaders to WGSL and then see 🙂.

---

<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 15, 2024, 6:59pm UTC](https://forum.babylonjs.com/t/uniform-buffer-object-ubo-and-webgpu/47210/4 "2024-01-15T18:59:49Z")

</div>

> [@axeljaeger](#):
>
> 1. You set the buffer on every bind of the shader. Is this necessary? or is it just a bridge solution?

In fact, that’s what you did (`shader.onBind` is called each time a mesh is drawn), but I removed it. I simply do `shaderMaterial.setUniformBuffer('myBuffer', buffer);` once to set the ubo.

---

<div class="post-metadata">

**Author:** ![axeljaeger](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/axeljaeger/32/21445_2.png) [@axeljaeger](https://forum.babylonjs.com/u/axeljaeger)\
**Post date:** [January 15, 2024, 8:22pm UTC](https://forum.babylonjs.com/t/uniform-buffer-object-ubo-and-webgpu/47210/5 "2024-01-15T20:22:16Z")

</div>

Right, I read the diff in the wrong direction, sorry for that.
