I have a couple of compute shaders that need to be executed in a certain order and whose outputs depend on previous inputs. Ideally, I'll never need to copy a buffer client-side and do all of my work on the GPU.
Consider I have two compute shaders compiled and linked as program_one and program_two. Suppose I also have a GL_SHADER_STORAGE_BUFFER that contains the data that is written to by program_one and read by program_two. Can I simply do the following:
glUseProgram(program_one);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, buffer);
glDispatchCompute(16, 16, 1);
glUseProgram(program_two);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, buffer);
glDispatchCompute(16, 16, 1);
Is it guaranteed that all invocations of the first compute shader will finish before any invocations of the second (to avoid data races between reading and writing buffer)? If not, how do I synchronize them?