title: Trouble with ImageTextures in OpenGL 4.2
body: <p>I'm trying to write a simple test program to demonstrate the functionality of GLSL 4.2's <code>ImageLoad</code> function. So far I have:</p>
  
  <ul>
  <li>A simple Shader that loads the bottom-left texel from an <code>image2D</code> and displays it.</li>
  <li>Some simple vertex code to draw a fullscreen squad</li>
  <li>Other scaffolding code to create a context, compile shaders, etc.</li>
  </ul>
  
  <p>Here's my frag code (vertex is a simple passthrough shader):</p>
  
  <pre class="lang-c prettyprint-override"><code>#version 420
  
  #include &lt;gtest/SmallImage.h&gt; //NOTE: I have a preprocessor that takes care of includes
  
  layout(binding = SMALL_IMAGE_ONE_BINDING, rgba8) uniform image2D image_one;
  
  smooth in vec2 texture_coords;
  out vec4 out_color;
  
  void main(void)
  {
    vec4 loaded = imageLoad(image_one, ivec2(0,0));
    vec4;
    out_color = vec4(loaded.r,
                 loaded.g,
                     gl_FragCoord.y/1200.0,
                     1);
  }
  </code></pre>
  
  <p>Pretty simple, no read room for mishap. The blue channel being proportionate to pixel coordinates lets me know that the quad is being properly rendered even if the <code>imageLoad</code> call fails.</p>
  
  <p>Here is the C++ loop I'm using to render to the screen. It binds a texture, writes a single yellow texel, then renders a fullscreen quad. There are two variants: when <code>MAKE_ONCE</code> is defined, the texture is generated once, before the loop. If it is not defined, a new texture is generated on each iteration of the loop:</p>
  
  <pre class="lang-c prettyprint-override"><code>MyTestFun(void)
  {
    glm::vec4 yellow = glm::vec4(1.0, 1.0, 0.0, 1.0); 
  
  #ifdef MAKE_ONCE
    renderer-&gt;InitFrame();  //sets current OpenGL context 
    GLuint name = 0;
    glGenTextures(1, &amp;name);   
    renderer-&gt;EndFrame();  
  #endif
  
    while(1)
    {     
      renderer-&gt;InitFrame();
  
  #ifndef MAKE_ONCE
      GLuint name =0;
      glGenTextures(1, &amp;name);   
  #endif
  
      glBindTexture(GL_TEXTURE_2D, name);
  
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_FLOAT,
                   glm::value_ptr(yellow));
  
      glBindTexture(GL_TEXTURE_2D, 0);
  
      glBindImageTexture(SMALL_IMAGE_ONE_UNIT, name, 0, false, 0, GL_READ_ONLY,
                         GL_RGBA8);
  
  
      renderer-&gt;FullscreenQuad(); //shades using the frag shader shown above
  
      //bind the default texture to the image unit, hopefully freeing ours for editing
      glBindImageTexture(SMALL_IMAGE_ONE_UNIT, 0, 0, false, 0, GL_READ_WRITE,
                         GL_RGBA8);
  
      renderer-&gt;EndFrame();
    }
  }
  </code></pre>
  
  <p>When <code>MAKE_ONCE</code> is defined, the first frame shows a vertical gradient from white to yellow, but subsequent frames show a vertical gradient from blue to black, indicating that the <code>IMAGE_LOAD</code> call is returning 0.0.</p>
  
  <p>When <code>MAKE_ONCE</code> is not defined, e.g. the texture is remade on every frame, the program behaves as expected, showing the same white&lt;->yellow gradient on every frame.</p>
  
  <p>What might be causing this behavior? The OpenGL 4.2 specification says that an <code>imageLoad</code> call will return black (all zeros) if the image is in an invalid state (p255-266), but none of the validity conditions seem to apply here.</p>