title: OpenGL: Shader storage buffer mapping/binding
body: <p>I'm currently working on a program which supports <strong>depth-independent</strong> (also known as <strong>order-independent</strong>) <strong>alpha blending</strong>. To do that, I implemented a <strong>per-pixel linked list</strong>, using a texture for the header (points for every pixel to the first entry in the linked list) and a texture buffer object for the linked list itself. While this works fine, I would like to <em>exchange the texture buffer object with a shader storage buffer</em> as an excercise.</p>
  
  <p>I think I almost got it, but it took me about a week to get to a point where I could actually use the shader storage buffer. My question are:</p>
  
  <ul>
  <li><p>Why I can't map the shader storage buffer?</p></li>
  <li><p>Why is it a problem to bind the shader storage buffer again?</p></li>
  </ul>
  
  <p>For debugging, I just display the contents of the shader storage buffer (which doesn't contain a linked list yet). I created the shader storage buffer in the following way:</p>
  
  <pre><code>glm::vec4* bufferData = new glm::vec4[windowOptions.width * windowOptions.height];
  glm::vec4* readBufferData = new glm::vec4[windowOptions.width * windowOptions.height];
  
  for(unsigned int y = 0; y &lt; windowOptions.height; ++y)
  {
      for(unsigned int x = 0; x &lt; windowOptions.width; ++x)
      {
          // Set the whole buffer to red
          bufferData[x + y * windowOptions.width] = glm::vec4(1,0,0,1);
      }
  }
  
  GLuint ssb;
  // Get a handle
  glGenBuffers(1, &amp;ssb);
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssb);
  // Create buffer
  glBufferData(GL_SHADER_STORAGE_BUFFER, windowOptions.width * windowOptions.height * sizeof(glm::vec4), bufferData, GL_DYNAMIC_COPY);
  // Now bind the buffer to the shader
  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssb);
  </code></pre>
  
  <p>In the shader, the shader storage buffer is defined as:</p>
  
  <pre><code>layout (std430, binding = 0) buffer BufferObject
  {
      vec4 points[];
  };
  </code></pre>
  
  <p>In the rendering loop, I do the following:</p>
  
  <pre><code>glUseProgram(defaultProgram);
  
  for(unsigned int y = 0; y &lt; windowOptions.height; ++y)
  {
      for(unsigned int x = 0; x &lt; windowOptions.width; ++x)
      {
          // Create a green/red color gradient
          bufferData[x + y * windowOptions.width] =
              glm::vec4((float)x / (float)windowOptions.width,
              (float)y / (float)windowOptions.height, 0.0f, 1.0f);
      }
  }
  
  glMemoryBarrier(GL_ALL_BARRIER_BITS);  // Don't know if this is necessary, just a precaution
  glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, windowOptions.width * windowOptions.height * sizeof(glm::vec4), bufferData);
  // Retrieving the buffer also works fine
  // glMemoryBarrier(GL_ALL_BARRIER_BITS);
  // glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, windowOptions.width * windowOptions.height * sizeof(glm::vec4), readBufferData);
  
  glMemoryBarrier(GL_ALL_BARRIER_BITS);  // Don't know if this is necessary, just a precaution
  
  // Draw a quad which fills the screen
  // ...
  </code></pre>
  
  <p>This code works, but when I replace <strong>glBufferSubData</strong> with the following code,</p>
  
  <pre><code>glm::vec4* p = (glm::vec4*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, windowOptions.width * windowOptions.height, GL_WRITE_ONLY);
  for(unsigned int x = 0; x &lt; windowOptions.width; ++x)
  {
      for(unsigned int y = 0; y &lt; windowOptions.height; ++y)
      {
          p[x + y * windowOptions.width] = glm::vec4(0,1,0,1);
      }
  }
  glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
  </code></pre>
  
  <p>the mapping fails, returning <strong>GL_INVALID_OPERATION</strong>. It seems like the shader storage buffer is still bound to something, so it can't be mapped. I read something about <strong>glGetProgramResourceIndex</strong> (<a href="http://www.opengl.org/wiki/GlGetProgramResourceIndex" rel="nofollow">http://www.opengl.org/wiki/GlGetProgramResourceIndex</a>) and <strong>glShaderStorageBlockBinding</strong> (<a href="http://www.opengl.org/wiki/GlShaderStorageBlockBinding" rel="nofollow">http://www.opengl.org/wiki/GlShaderStorageBlockBinding</a>), but I don't really get it.</p>
  
  <p>My second question is, why I can neither call</p>
  
  <pre><code>glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssb);
  </code></pre>
  
  <p>, nor</p>
  
  <pre><code>glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssb);
  </code></pre>
  
  <p>in the render loop after <strong>glBufferSubData</strong> and <strong>glMemoryBarrier</strong>. This code should not change a thing, since these calls are the same as during the creation of the shader storage buffer. If I can't bind different shader storage buffers, I can only use one. But I know that more than one shader storage buffer is supported, so I think I'm missing something else (like "releasing" the buffer).</p>