2
votes

I want to use imageStore and imageLoad in multiple compute shaders.

This is mixed with "normal" rendering commands (glDraw) to the screen or to framebuffers, but these don't use imageStore or imageLoad (only texture or texelFetch).

I only have one OpenGL context and thread.

My assumptions are the following:

  • When doing an imageStore, I need to do glMemoryBarrier before doing an imageLoad in a later compute shader or texture or texelFetch in a later fragment shader.
  • I don't need to use coherent at all.
  • I don't need to use glSync at all.
  • I don't need to use glMemoryBarrier after writing to a texture using a framebuffer and then reading it with imageLoad
  • OpenGL will run the compute shaders and "normal" draw operations in the order they are requested. Assuming I use glMemoryBarrier between the calls, there are no "threading issues".

Are they correct?

1

1 Answers

3
votes

Each of those is correct, with the ''possible'' exception of one:

I don't need to use coherent at all.

You may need coherent, depending on what your compute shader is doing. If your compute shader writes to an image, then reads from data written by another invocation within the same dispatch, then you need coherent. So if you do an imageStore, issue a compute shader barrier() call, then do an imageLoad to read some other invocation's value, then you need the coherent qualifier.

coherent is about ensuring visibility within a rendering command (CS dispatches are considered "rendering commands" in OpenGL). If you don't need internal visibility, then you don't need coherent.


I want to elaborate on this, because it's a common source of confusion:

I don't need to use glMemoryBarrier after writing to a texture using a framebuffer and then reading it with imageLoad

This is absolutely correct. Memory barriers are about ensuring the visibility (and synchronization) of incoherent writes to memory. Rendering to the framebuffer is not an incoherent write. So you can use imageLoad on such data without needing explicit synchronization.

Assuming of course that you are not rendering to that framebuffer in the same operation you're imageLoading from it. That rule still applies.