Compute Shaders Order Execution

Yes, you do a read to CPU by doing that, and it is delayed by one frame because it is using a promise (the promise is resolved after we finished processing the frame on Babylon side).

You should create a storage buffer with the VERTEX flag, so that you can use it as a vertex buffer.

See how it is done in the Boids compute shader example:

Creation of the buffers:

this.particleBuffers = [
    new BABYLON.StorageBuffer(engine, initialParticleData.byteLength, BABYLON.Constants.BUFFER_CREATIONFLAG_VERTEX | BABYLON.Constants.BUFFER_CREATIONFLAG_WRITE),
    new BABYLON.StorageBuffer(engine, initialParticleData.byteLength, BABYLON.Constants.BUFFER_CREATIONFLAG_VERTEX | BABYLON.Constants.BUFFER_CREATIONFLAG_WRITE),
];

(there are two buffers, because we do a “ping-pong” in this example)

Create the vertex buffers by using the storage buffers created above:

this.vertexBuffers = [
    [
        new BABYLON.VertexBuffer(engine, this.particleBuffers[0].getBuffer(), "a_particlePos", false, false, 4, true, 0, 2),
        new BABYLON.VertexBuffer(engine, this.particleBuffers[0].getBuffer(), "a_particleVel", false, false, 4, true, 2, 2)
    ],
    [
        new BABYLON.VertexBuffer(engine, this.particleBuffers[1].getBuffer(), "a_particlePos", false, false, 4, true, 0, 2),
        new BABYLON.VertexBuffer(engine, this.particleBuffers[1].getBuffer(), "a_particleVel", false, false, 4, true, 2, 2)
    ]
];

Set the vertex buffers to the mesh:

this.mesh.setVerticesBuffer(this.vertexBuffers[this.t][0], false);
this.mesh.setVerticesBuffer(this.vertexBuffers[this.t][1], false);
2 Likes