Jan Rüegg
Jan Rüegg

Reputation: 10045

Memory coherency with compute shaders and imageStore

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:

Are they correct?

Upvotes: 3

Views: 1338

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473447

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.

Upvotes: 4

Related Questions