Transform feed-back: watch out for an output array variable catch!

Transform feed-back is one of the features in OpenGL 3.x that are very handy if you need to perform any calculations which are of iterative nature and are more interested in performance, rather than in flexibility that OpenCL offers. Watch out though, as the latter solution might prove to be more reliable – as it turns out.

Consider the following GLSL snippet:

#version 330 core

out result
    vec3 result[16];
} Out;

void main()
    for (int n = 0; n < 16; ++n)
        result[n] = n;

Trivial so far. Now, what I would expect to see after using this shader for a transform feed-back operation configured to capture result varying would be to have a certain region of target buffer object filled with sequence of numbers.

WRONG! (..most of the time)

At least with my favourite NVIDIA driver, that is 🙂 The latest version (314.21) do either of the three things:

  • Buffer object is filled with QNBs; (seems to happen 45% of the time)
  • Buffer object is filled with zeroes; (45% again)
  • Buffer object is filled with expected values (remainder of the attempts)

In case you’re curious – no, no errors are reported in the process.

Let’s take a quick look at OpenGL 3.3 Core Profile specification. Here’s a nice excerpt from p. 98:

When an  individual point, line, or triangle primitive  reaches
the  transform feedback stage  while transform feedback
is active, the values of the specified varying variables
of the vertex are appended to the buffer objects bound
to the transform feedback binding points. 

The  attributes  of  the  first  vertex  received  after
BeginTransformFeedback are written at the starting offsets 
of the bound buffer objects set by BindBufferRange, and
subsequent vertex attributes are appended to the buffer


When writing varying variables that are arrays, individual
array elements are written in order. 

For multi-component varying variables or varying array
elements, the individual components are written in order.

Well then, seems like theory is to practice as practice is to theory. The only solution I could come up with is to revert to le good ol’ gl_VertexID friend. At least it works 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *