Some questions about compute shader

Hello, I have some questions about compute shader. My application contains function, in which invokes 2 compute shaders. First fills by some value 2D texture (RGBA32F) or SSBO (in function invoked for 2D texture and SSBO), second performs computations and uses prepaired by first shader texture and SSBO (contains loops, if statements (at this moment can’t rewrite without them)). In another function I read values from SSBO and texture by glMapBuffer and glGetTextureSubImage. All worked, before I started calling these functions in for loop. App began crashing after delay with STATUS_STACK_BUFFER_OVERRUN(0xC0000409) at the first occurrence of glMapBuffer/glGetTextureSubImage. If comment buffer and texture access functions (such as glMapBuffer/glGetTextureSubImage/glUnmapBuffer) app working without problem. So, for continue work around, I want to know something else about compute shader.

  1. Have to I call something else before reading from SSBO/texture modified by compute shader? (now I call only glMemoryBarrier with appropriate bit and bind buffer/texture)
  2. Have to I call something else after reading from SSBO/texture? (now only glUnmapBuffer if reading from SSBO)
  3. How should I invoke compute shader in such situation? (maybe I do something wrong: use shader - bind uniforms - glDispatchCompute - glMemoryBarrier - unuse shader)

Yesterday I maybe found the source of problem - my second shader very complex and unoptimized, so it takes even seconds sometimes to complete work and Windows thinks, that application is not responding (window marked as Not responding before crash), so after a few seconds terminates it.

maybe a “glFinish();” at the end of the loop solves the problem
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glFinish.xhtml

if you want to read from a buffer / texture in which the previous invocation of the computeshader writes into, then yes, you have to call:

glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT);

https://www.khronos.org/opengl/wiki/GLAPI/glMemoryBarrier

yes, glMemoryBarrier(…) BEFORE you map the buffer, and when finished reading, immediately glUnmapBuffer(…) BEFORE you use the buffer again

thats correct, but you need to call glMemoryBarrier(…) if you want the buffers / textures to …

Thank you for your reply, john_connor, it helped me understand, that I correctly use compute shaders. glFinish() helped a little bit (app crashes now with higher values), so I think the main problem is shader’s complexity and I have already started refactoring.