title: Reading fragment shader outputs into application memory
body: <p>I'm trying to use OpenGL for visibility testing for complex geometries.  What I want to do is simple: assign each primitive an integer ID, and then count the number of pixels with that ID.  That allows me to calculate the relative visible area of each primitive.  (Ultimately, this will be expanded to some minor finite-element calculations on the visible area.)</p>
  
  <p>My problem is this.  I'm trying to read the output of a fragment shader into my application memory: specifically, the primitive ID output.  I'm using QT 4.7.4 and its OpenGL wrapper classes. When I bind and enable a buffer (a "PixelPack" buffer), and attempt to read from the OpenGL buffer into the memory, it reports a successful read.  But the values stored in the array are not what I expect- they're all 0, even though for testing purposes I've set the ID to 1 for all primitives.</p>
  
  <p>Here's my fragment shader: </p>
  
  <pre><code>#version 130
  
  in vec4 Color;
  flat in uint VertId;
  
  out vec4 FragColor;
  out uint FragId;
  
  void main()
  {
     FragColor = Color;
  
     // Changed to simpler version for debugging.
     //   FragId = VertId;
     FragId = uint( 1 );
  }
  </code></pre>
  
  <p>And here's my application code, with some irrelevant parts stripped off (test harness hookups, etc.):</p>
  
  <pre><code>#include &lt;QtOpenGL/QGLShader&gt;
  #include &lt;QtOpenGL/QGLShaderProgram&gt;
  #include &lt;QtOpenGL/QGLBuffer&gt;
  
  using namespace std;
  
  string loadSource( string file );
  
  int
  testSelfShadow::
  shader( ostream&amp; error )
  {
     bool fail = false;
  
     // Create the OpenGL context.
     int argc = 0;
     char* argv;
     QApplication* app = new QApplication( argc, &amp;argv );
     QGLWidget* widget = new QGLWidget();
     widget-&gt;makeCurrent();
  
     // Create the shader program.
     QGLShaderProgram* prog = new QGLShaderProgram();
     bool success = false;
     success = prog-&gt;addShaderFromSourceCode( QGLShader::Vertex,
                                              loadSource( "vertex.glsl" ).c_str() );     
     if ( ! success )
     {
        ErrorOStream msg;
        msg &lt;&lt; "Error trying to load vertex shader into a shader program.\n"
            &lt;&lt; prog-&gt;log().toStdString();
        throw ERRORLOG( msg.str() );
     }
     success = prog-&gt;addShaderFromSourceCode( QGLShader::Fragment,
                                              loadSource( "fragment.glsl" ).c_str() );   
     if ( ! success )
     {
        ErrorOStream msg;
        msg &lt;&lt; "Error trying to load fragment shader into a shader program.\n"
            &lt;&lt; prog-&gt;log().toStdString();
        throw ERRORLOG( msg.str() );
     }
     success = prog-&gt;link();
  
     if ( ! success )
     {
        ErrorOStream msg;
        msg &lt;&lt; "Error trying to link shaders into a shader program.\n"
            &lt;&lt; prog-&gt;log().toStdString();
        throw ERRORLOG( msg.str() );
     }
  
     prog-&gt;bind();
  
     // Create the buffer for vertex position.
     QGLBuffer* vBuf = new QGLBuffer( QGLBuffer::VertexBuffer );
     vBuf-&gt;create();
     vBuf-&gt;setUsagePattern( QGLBuffer::DynamicDraw );
     vBuf-&gt;bind();
  
     GLfloat vertices[] = {
        -1.0f, -1.0f, 0.0f, 1.0f,
        -1.0f, 0.0f, 0.0f, 1.0f,
        1.0f, 0.0f, 0.0f, 1.0f,
        1.0f, -1.0f, 0.0f, 1.0f,
        -1.0f, 0.0f, 0.1f, 1.0f,
        -1.0f, 1.0f, 0.1f, 1.0f,
        1.0f, 1.0f, 0.1f, 1.0f,
        1.0f, 0.0f, 0.1f, 1.0f };
  
     vBuf-&gt;allocate( vertices, sizeof( vertices ) );
  
     prog-&gt;setAttributeBuffer( "Vertex", GL_FLOAT, 0, 4, 0 );
     prog-&gt;enableAttributeArray( "Vertex" );
  
     // Create the buffer for Grayscale brightness value.
     // Not important for final program, just for debugging during
     // development.
     QGLBuffer* bBuf = new QGLBuffer( QGLBuffer::VertexBuffer );
     bBuf-&gt;create();
     bBuf-&gt;bind();
  
     GLfloat brightness[] = {
        1.0, 0.9, 0.8, 0.7,
        0.5, 0.4, 0.3, 0.2 
   };
  
     bBuf-&gt;allocate( brightness, sizeof( brightness ) );
  
     prog-&gt;setAttributeBuffer( "Brightness", GL_FLOAT, 0, 1, 0 );
     prog-&gt;enableAttributeArray( "Brightness" );
  
     // Create the buffer for polygon ID.
     QGLBuffer* idBuf = new QGLBuffer( QGLBuffer::VertexBuffer );
     idBuf-&gt;create();
     idBuf-&gt;bind();
     GLuint polyId[] = {
        1, 1, 1, 1, 
        2, 2, 2, 2
     };
  
     idBuf-&gt;allocate( polyId, sizeof( polyId ) );
     prog-&gt;setAttributeBuffer( "PolyId", GL_UNSIGNED_INT, 0, 1, 0 );
     prog-&gt;enableAttributeArray( "PolyId" );
  
     // Create the index buffer.  Not trying to do any optimization
     // here yet.
     QGLBuffer* iBuf = new QGLBuffer( QGLBuffer::IndexBuffer );
     iBuf-&gt;create();
     iBuf-&gt;bind();
     GLuint indices[] = {
        0, 1, 2, 3, 4, 5, 6, 7
     };
     iBuf-&gt;setUsagePattern( QGLBuffer::StaticDraw );
     iBuf-&gt;allocate( indices, sizeof( indices ) );
  
     // Create the buffer for reading back polygon id per fragment.
     QGLBuffer* fBuf = new QGLBuffer( QGLBuffer::PixelPackBuffer );
     fBuf-&gt;create();
     fBuf-&gt;setUsagePattern( QGLBuffer::DynamicRead );
     fBuf-&gt;bind();
     fBuf-&gt;allocate( 640 * 480 * sizeof( GLuint ) );
  
     prog-&gt;setAttributeBuffer( "FragId", GL_UNSIGNED_INT, 0, 1, 0 );
     prog-&gt;enableAttributeArray( "FragId" );
  
     GLuint* fBufData = new GLuint[ 640 * 480 * sizeof( GLuint ) ];
  
     glDrawElements( GL_QUADS, 8, GL_UNSIGNED_INT, 0 );
     widget-&gt;show();
     widget-&gt;updateGL();
  
     // Trying this two different ways; neither way works.
     bool readSuccess = fBuf-&gt;read( 0, fBufData, 640 * 480 * sizeof( GLuint ) );
     GLuint* fBufMap = 
        static_cast&lt; GLuint* &gt;( fBuf-&gt;map( QGLBuffer::ReadOnly ) );
  
     cout &lt;&lt; "\n"
          &lt;&lt; "Read Successful: " &lt;&lt; readSuccess &lt;&lt; endl;
     cout &lt;&lt; "Buffer map location and sample data: " 
          &lt;&lt; fBufMap &lt;&lt; ":" &lt;&lt; fBufMap[640] &lt;&lt; endl;
     cout &lt;&lt; "Read data pointer: " &lt;&lt; fBufData &lt;&lt; endl;
     cout &lt;&lt; "Sample fragment ID: " &lt;&lt; fBufData[ 640 * 480 / 2 ] &lt;&lt; endl;
  
     app-&gt;exec();
  
     return fail;
  }
  </code></pre>
  
  <p>Here are sample outputs for a program run: </p>
  
  <pre><code>Read Successful: true
  Buffer map location and sample data: 0x5a5d9000:0
  Read data pointer: 0x59e48008
  Sample fragment ID: 0
  </code></pre>
  
  <p>That's not what I would expect.  I would expect all fragment IDs to be 1, because I explicitly set FragId = uint( 1 ) in the fragment shader.  Am I setting up my reads wrong?  Am I doing something wrong in my binding of buffers, or enabling the names?</p>
  
  <p>I would prefer to use QT code if possible, for reasons beyond the scope of this question.</p>