I’m implementing a particle system, where the particles are initialized during each frame with CPU commands:
protected void initParticles()
{
int dataSize = (4 + 3 + 1 + 1) * Float.SIZE / 8;
int buffSize = particleCount * dataSize;
int newParticles = 10;
for (int i = 0; i < particleCount; i++)
{
if (newParticles > 0 && dataBuff.get(i * 9 + 7) == 0)
{
dataBuff.put(i * 9 + 7, 1f); //update lifespan
newParticles--;
}
}
dataBuff.rewind();
gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vboBuff.get(0));
gl.glBufferSubData(GL4.GL_ARRAY_BUFFER, 0, buffSize, dataBuff);
}
Setup buffers at the start:
dataBuff = GLBuffers.newDirectFloatBuffer(buffSize);
for (int i = 0; i < particleCount; i++)
{
dataBuff.put(new float[] {((float) Math.random() - 0.5f) * 20f,
10, //((float) Math.random() * 25) + 25f
((float) Math.random() - 0.5f) * 20f,
0.02f}); //position and size
dataBuff.put(new float[] {0, -16, 0}); //velocity
dataBuff.put(0f); //lifespan
dataBuff.put(0f); //age
}
gl.glBufferData(...);
And then the compute shader is supposed to receive the updated data. This is the rendering code:
initParticles();
gl.glUseProgram(computeProgram);
gl.glBindBufferBase(GL4.GL_SHADER_STORAGE_BUFFER, 0, vboBuff.get(0));
gl.glDispatchCompute(1, 1, 1);
gl.glUseProgram(viewProgram);
setUniforms();
gl.glActiveTexture(GL4.GL_TEXTURE0);
texture.bind(gl);
gl.glBindVertexArray(vaoBuff.get(0));
gl.glDrawArraysInstanced(GL4.GL_TRIANGLE_STRIP, 0, 4, particleCount);
In my example, the compute shader is bound to a SSBO, which is also used as a VBO for rendering program to read from it. The ‘initParticles()’ will update that buffer in each frame. There should be 10 new particles generated in each frame, yet the result comes out to be that the command initParticles() doesn’t wait the compute shader to finish first, it just keep executing and as a result all particles are generated. (The shader code should be correct after testing.) Any ideas about that?