Hello everyone,
I have been learning the ways of compute shaders recently, and I am now using them to generate an instance buffer like in this PG:
The compute shader creates an instance buffer on the GPU, then it gets copied to the CPU at the end of the compute dispatch, so that I can pass it to thinInstanceSetBuffer which sends it again to the GPU (that what I understand at least).
Copying all this data back and forth is not ideal and there is probably some trick to keep all the data on the GPU which would be a sizeable improvement. Does someone have an idea?
You canât use your own buffers with thin instances, so what you want to do is currently not possible.
You could try to hack it, by creating your own vertex buffer based on your storage buffer (you can pass storageBuffer.getBuffer() as the second parameter of the VertexBuffer constructor) and setting it to mesh.setVerticesBuffer("world", your_vertex_buffer). However, for thin instances, the matrices are passed as 4 vertex buffers, world0/world1/world2/world3 (worldX correspond to the row X of the matrix). So, you would need to change your compute shader to fill 4 storage buffers, and you would have to create 4 vertex buffers.
That looks fairly easy! I will also create a small PG for that one.
Just to clarify, is using the storage buffer incompatible with a physics engine? I assume that with the physics engine residing on the CPU, while the vertex data resides on the GPU, it may be impossible to account for collisions. I am currently weighing my options here, haha.
The issue is that updateIndices takes an IndicesArray (BABYLON | Babylon.js Documentation), and does not accept my storage buffer sadly. I also saw the boids compute shader uses a cpu buffer for the indices.
Is there a workaround to set the index buffer using the gpu storage?
I am working on the thin instance hack but I am struggling with setVerticesBuffer. I can indeed get the world0, world1, world2, world3 properly and I can create the associate buffers but how do I tell my grass blade to create thin instances out of it?
You must explicitely set the geometry._indexBuffer by hand, and create a submesh (itâs done for you when you call mesh.setIndices, but you canât do it in this case).
Also, you must create your storage buffers with the right flags: BUFFER_CREATIONFLAG_READWRITE + BUFFER_CREATIONFLAG_VERTEX or BUFFER_CREATIONFLAG_INDEX, depending on the buffer.
Lastly, you canât use material.wireframe = true, because it triggers an index buffer creation to properly render lines, which expects to be able to read from the existing index buffer.