I use an atomic counter in a compute shader with an atomic_uint
bound to a dynamic GL_ATOMIC_COUNTER_BUFFER
(in a similar way to this opengl-atomic-counter tutorial lighthouse3d).
I'm using the atomic counter in a particle system to check a condition has been reached for all particles; I expect to see counter==numParticles
when all of the particles are in the correct place.
I map the buffer each frame and check if the atomic counter has counted all of the particles:
GLuint *ptr = (GLuint *) glMapBuffer( GL_ATOMIC_COUNTER_BUFFER, GL_READ_ONLY );
GLuint particleCount = ptr[ 0 ];
glUnmapBuffer( GL_ATOMIC_COUNTER_BUFFER );
if( particleCount == numParticles() ){ // do stuff }
On a single GPU host the code works fine and particleCount
always reaches numParticles()
but on a multi gpu host the particleCount
never reaches numParticles()
.
I can visually check that the condition has been reached and the test should be true however particleCount is changing each frame going up and down but never reaching numParticles().
I have tried an opengl memory barrier on the GL_ATOMIC_COUNTER_BARRIER_BIT
before I unmap particleCount
:
glMemoryBarrier(GL_ATOMIC_COUNTER_BARRIER_BIT);
GLuint *ptr = (GLuint *) glMapBuffer( GL_ATOMIC_COUNTER_BUFFER, GL_READ_ONLY );
GLuint particleCount = ptr[ 0 ];
glUnmapBuffer( GL_ATOMIC_COUNTER_BUFFER );
if( particleCount == m_particleSystem->numParticles() )
{ // do stuff }
and I've tried a glsl barrier before incrementing the counter in the compute shader:
memoryBarrierAtomicCounter();
atomicCounterIncrement( particleCount );
but the atomic counter doesn't seem to synchronise across devices.
What is the correct way to synchronise so that the atomic counter works with multiple devices?