StorageBuffer from existing DataBuffer

I’m trying to pass an existing DataBuffer to a compute shader.

Which means calling ComputeShader.setStorageBuffer(name: string, buffer: StorageBuffer): void; However, the StorageBuffer API always creates a new storage buffer. Would it be possible to add another constructor or a static function along the lines of .fromBuffer(buffer: DataBuffer)?

More specifically, I’m trying to do indirect drawing as in Dynamic size of instance count . The accepted answer there actually has a workaround for this, which just works due to the implementation of Buffer coincidentally being close enough to StorageBuffer
https://playground.babylonjs.com/?webgpu#SLP3YY#9

Welcome aboard!

It’s a nice idea, here’s the PR:

However, I have chosen to allow DataBuffer as a parameter to setStorageBuffer. In the same way, I can also allow it for setUniformBuffer and kill two birds with one stone.

3 Likes

Sweet, that looks like an excellent solution. Thank you very much!