PDA

View Full Version : Some questions about compute shader



Van der Deken
03-13-2017, 09:13 AM
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)

Van der Deken
03-13-2017, 11:22 PM
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.

john_connor
03-13-2017, 11:45 PM
maybe a "glFinish();" at the end of the loop solves the problem
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glFinish.xhtml



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)

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


2) Have to I call something else after reading from SSBO/texture? (now only glUnmapBuffer if reading from SSBO)

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


3) How should I invoke compute shader in such situation? (maybe I do something wrong: use shader - bind uniforms - glDispatchCompute - glMemoryBarrier - unuse shader)

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

... reflect data written by shaders prior to the barrier.

Van der Deken
03-14-2017, 09:36 AM
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.