ClearBufferData + shader_buffer_store

Hi,

I do not understand the behavior of ClearBufferData in my OpenGL 4.3 application. Basically, it seems that clearing a buffer with ClearBufferData then writing into it in shaders causes the buffer to always be empty after the shaders execute.

Specifically, my application does the following:

  1. ClearBufferData of myBuffer
  2. Make myBuffer resident on the GPU and set it as a uniform in shaders
  3. a. Enable shaders
  4. b. Draw stuff with shaders (each fragment shader writes into myBuffer)
  5. c. Disable shaders
  6. Make myBuffer non resident

After #4 myBuffer contains all 0’s. However, if I do not clear myBuffer (remove #1) then its contents is written by the shaders as I would expect. I’ve tried putting a Finish() call between the clear and draw, but it has no effect. What is going on here? I’d appreciate any explanation.

Thanks!

First, if all you’re doing is writing to the buffer, there’s no point in clearing it beforehand; you’ll just overwrite the cleared data.

Second, I don’t see the part where you put a glMemoryBarrierEXT(GL_SHADER_GLOBAL_ACCESS_BARRIER_BIT_NV) between the clear and the part where you write to it. Without that barrier, OpenGL will not synchronize memory accesses. Also, you need a barrier between when you write the data in your shader and when you read it to ensure that it becomes visible.