Transform Feedback lost order of triangle mesh

470 Views Asked by At

I'm trying to compute some of my vertices in a vertex (or geometry) shader. When I draw finally the vertices they lost the order I gave to the vertex array object (with GL_ELEMENT_ARRAY_BUFFER). The function glDrawElements doesn't work in this situation, instead I have to use glDrawArrays. Am I wrong?

How can I save the order of the triangles index for the Transform Feedback?

1

There are 1 best solutions below

1
On

You are not wrong. Vertices are written to the transform feedback buffer in the order they are processed by the vertex (or geometry) shader. I don't see an easy way around it.

This means that if you want to draw the vertices in the captured buffer, you will use glDrawArrays(). Or in GL 4.0 and later, you can also use glDrawTransformFeedback().

If you were drawing indexed, and need the vertices back in their original order, you will have to reshuffle them yourself. If your index buffer was filled with n indices from array idxA, your input buffer with vertices from array inVertexA, and the vertices from the captured buffer are in array capturedVertexA, the transformed vertices in the original order could be written to outVertexA as:

for (int k = 0; k < n; ++k) {
    outVertexA[idxA[k]] = capturedVertexA[k];
}

This will work as long as the drawing was with primitive type GL_TRIANGLES. If you were drawing with something like GL_TRIANGLE_STRIP, the index logic will be slightly more complex because transform feedback produces separate triangles.

I guess you could let the GPU reshuffle the vertices by running the output through transform feedback again, and using an index array that is the reverse mapping of the original index array.

It looks kind of awkward in any case. You may want to think through your use case again, and make sure that you really need the vertices back in the original order.

Disclaimer: I have never used transform feedback, and have done only very minimal work implementing/fixing it in drivers. All of the above is my understanding from reading the spec.