<p>No it is not guaranteed, since the OpenGL specification allows that two Compute Shader run concurrently or even in different order.</p>

<p>You need to call <code>glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT)</code> before the second <code>glDispatchCompute</code> to ensure visibility of the writes from <code>program_one</code>.</p>

<hr>

<p><a href="https://www.opengl.org/wiki/Memory_Model">From the OpenGL.org wiki article on the memory model</a>:</p>

<blockquote>
  <p>[...] invocations between stages may be executed in any order. <strong>This includes invocations launched by different rendering commands.</strong> While it is perhaps unlikely that two vertex shaders from different rendering operations could be running at the same time, it is also possible, so OpenGL provides <strong>no guarantees</strong>.</p>
</blockquote>

<p><a href="https://www.opengl.org/wiki/Shader_Storage_Buffer_Object">From the Opengl.org wiki article on Shader Storage Buffer</a>:</p>

<blockquote>
  <p>SSBOs reads and writes use incoherent memory accesses, so they need the appropriate barriers, just as Image Load Store operations.</p>
</blockquote>

CC BY-SA 3.0
2015-09-01T20:07:25.123Z
2015-09-01T20:07:25.123Z
Wumpf
<p>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.</p>

<p>Consider I have two compute shaders compiled and linked as <code>program_one</code> and <code>program_two</code>. Suppose I also have a <code>GL_SHADER_STORAGE_BUFFER</code> that contains the data that is written to by <code>program_one</code> and read by <code>program_two</code>. Can I simply do the following:</p>

<pre><code>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);
</code></pre>

<p>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 <code>buffer</code>)? If not, how do I synchronize them?</p>

<opengl><compute-shader><gpgpu>
Synchronizing successive OpenGL Compute Shader invocations
Mokosha
# Synchronizing successive OpenGL Compute Shader invocations

(Asked by: username_0 on 2015-09-01)

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?

**Tags:** opengl, compute-shader, gpgpu


---

## 1 answers

### Answer by username_1 on 2015-09-01. Score: 15 | Accepted answer

No it is not guaranteed, since the OpenGL specification allows that two Compute Shader run concurrently or even in different order.

You need to call `glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT)` before the second `glDispatchCompute` to ensure visibility of the writes from `program_one`.

---

[From the OpenGL.org wiki article on the memory model](https://www.opengl.org/wiki/Memory_Model):

> [...] invocations between stages may be executed in any order. **This includes invocations launched by different rendering commands.** While it is perhaps unlikely that two vertex shaders from different rendering operations could be running at the same time, it is also possible, so OpenGL provides **no guarantees**.

[From the Opengl.org wiki article on Shader Storage Buffer](https://www.opengl.org/wiki/Shader_Storage_Buffer_Object):

> SSBOs reads and writes use incoherent memory accesses, so they need the appropriate barriers, just as Image Load Store operations.


---

