How to share WebGPU index buffer between meshes

Hey there!

I am working on procedural terrain generation to make a system that loads and destroys terrain chunks on the fly.

As all chunks have the same triangle topology, I thought sharing the index buffer between all the chunks would be a nice optimization to reduce memory consumption.

In this PG, 2 terrain chunks share the same index buffer:

However, when I dispose one of the chunks (press the “d” key), it crashes WebGPU. I believe disposing the chunk could be disposing the shared index buffer, hence the remaining chunk is in for a bad time ^^

So my question is: how can I dispose my chunks without disposing the shared index buffer in order to keep using it for other chunks?

Mesh.setIndexBuffer doesn’t manage the reference counting of the buffer passed as the first parameter. If you reuse the same buffer multiple times, you should increase the reference counter manually (see line 64):

1 Like

That works for me :ok_hand: Thanks a lot for your help again ^^