title: GLSL memoryBarrier()
body: <p>OpenGL red book version 8 (GL 4.3) example 11.19 places a <code>imageLoad()</code> in a while loop, keep polling until at least one fragment of the previous primitive has updated this value. This book says</p>
  
  <blockquote>
    <p>Example 11.19 shows a very simple use case for memory barriers. It allows
    some level of ordering between fragments to be ensured. At the top of
    <code>functionUsingBarriers()</code>, a simple loop is used to wait for the contents
    of a memory location to reach our current primitive ID. <strong>Because we know
    that no two fragments from the same primitive can land on the same
    pixel, we know that when we’re executing the code in the body of the
    function, at least one fragment from the previous primitive has been
    processed</strong>. We then go about modifying the contents of memory at our
    fragment’s location using nonatomic operations. We signal to other shader
    invocations that we are done by writing to the shared memory location
    originally polled at the top of the function.</p>
    
    <p>To ensure that our modified image contents are written back to memory
    before other shader invocations start into the body of the function, we use
    a call to memoryBarrier between updates of the color image and the
    primitive counter to enforce ordering.</p>
  </blockquote>
  
  <p>However, GL spec 4.3 says</p>
  
  <blockquote>
    <p>having one invocation poll memory written by another invocation assumes that the other invocation has been launched and can complete its writes</p>
  </blockquote>
  
  <p>So how can we assure that fragment invocations of the previous primitive have been launched and completed its writes?</p>
  
  <p>Post the src code</p>
  
  <pre><code>#version 420 core
  
  layout (rgba32f} uniform coherent image2D my_image;
  
  // Declaration of function
  void functionUsingBarriers(coherent uimageBuffer i)
  
  {
  
      uint val;
  
      // This loop essentially waits until at least one fragment from
      // an earlier primitive (that is, one with gl_PrimitiveID - 1)
      // has reached the end of this function point. Note that this is
      // not a robust loop as not every primitive will generate
      // fragments.
      do
      {
          val = imageLoad(i, 0).x;
      } while (val != gl_PrimitiveID);
  
      // At this point, we can load data from another global image
      vec4 frag = imageLoad(my_image, gl_FragCoord.xy);
  
      // Operate on it...
      frag *= 0.1234;
      frag = pow(frag, 2.2);
  
      // Write it back to memory
      imageStore(my_image, gl_FragCoord.xy, frag);
  
      // Now, we’re about to signal that we’re done with processing
      // the pixel. We need to ensure that all stores thus far have
      // been posted to memory. So, we insert a memory barrier.
      memoryBarrier();
  
      // Now we write back into the original "primitive count" memory
      // to signal that we have reached this point. The stores
      // resulting from processing "my_image" will have reached memory
      // before this store is committed due to the barrier.
      imageStore(i, 0, gl_PrimitiveID + 1);
  
      // Now issue another barrier to ensure that the results of the
      // image store are committed to memory before this shader
      // invocation ends.
      memoryBarrier();
  }
  </code></pre>