ComputeGL 4.2
glMemoryBarrier
Makes writes done by shaders visible to the commands that come after.
Signature
void glMemoryBarrier(GLbitfield barriers);
How it works
Normal rendering is ordered automatically, but shaders that write to images or SSBOs bypass that tracking. Without a barrier, the next draw may read old data.
The flag describes HOW the data will be read NEXT, not how it was written. After a compute shader writes an SSBO that is then used as vertex data, the flag is GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT.
Parameters
barriersGLbitfield
How the written data will be consumed, combined with |.
Common values
- GL_SHADER_IMAGE_ACCESS_BARRIER_BIT
- Read again through imageLoad/imageStore.
- GL_TEXTURE_FETCH_BARRIER_BIT
- Sampled as a texture (texture()).
- GL_SHADER_STORAGE_BARRIER_BIT
- Read by shaders as an SSBO.
- GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT
- Used as vertex attributes.
- GL_BUFFER_UPDATE_BARRIER_BIT
- Read back with glGetBufferSubData or mapping.
- GL_ALL_BARRIER_BITS
- Everything — simple, but may be slower.
Example
example.cpp
1glDispatchCompute(groups, 1, 1);
2glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);
3glBindVertexArray(particleVAO);
4glDrawArrays(GL_POINTS, 0, particleCount);