View Full Version : gl_VertexId, gl_InstanceID, gl_PrimitiveID, but where is gl_IndexID

05-31-2013, 10:58 AM

Since I do much with TBOs, I wondered why there is no gl_IndexID.
For example, you could fill up a vertex buffer with a quad and index it 10000 times with a index buffer. But the index buffer just has the indices into the vertex buffer that brings in the gl_VertexID, which is 1000 times [0,4]. I can not deduce the particular quad id.

Sure, I could render the quads with a vertex buffer containing one quad by instancing 10000 times and use the gl_InstanceID, but that is inefficient.

That id is like: gl_VertexID = indexbuffer[gl_IndexID]

With a gl_IndexID, it could be calculated which instance I am in and index into the TBO. If the TBO has particle data, it could be indexed like this:

int particle_id = gl_IndexID / 6 ; // 6 because that quad has four vertices(two tris)

vec4 velocity = texelFetch(tbo, particle_id*2+0) ;
vec4 position = texelFetch(tbo, particle_id*2+1) ;

You could do it with gl_VertexID but that requires much vertices.

Do you agree or disagree? What do you think?

Alfonse Reinheart
05-31-2013, 12:26 PM
I can not deduce the particular quad id.

Nor are you supposed to.

The fundamental rule with vertex shaders is this: for every vertex which has the same inputs, you will get the same results. That is exactly why there is no "gl_IndexID": because this input would be different for every vertex. And therefore, the vertex shader would have to process that vertex every time, no matter what.

The entire concept of the post-T&L cache would break down, since no vertices can ever be reused. That's how the cache works: if the index fetched from the index buffer is the same as a previously processed one, then the vertex shader doesn't bother executing at all. What you want works against that entirely.

05-31-2013, 12:47 PM
It looks like you're rendering particle sprites; can you draw the quads with GL_POINT and GL_POINT_SPRITE instead?

It is also possible to use a geometry shader to index the TBOs with gl_PrimitiveIDIn ( /2 tris), though if you're thinking that instanced drawing is too slow, that solution will likely be as well.

05-31-2013, 02:11 PM
Rendering N instances with M indices shouldn't be any slower than rendering N*M indices. In fact the former could be faster, if not for anything else because there is no need to fetch N*M indices, but only M indices.

Doing point sprites, or rendering particles with instancing is going to be faster and it will allow you to use gl_VertexID (point sprites) or gl_InstanceID (instancing) to access your particle data.

For the rest, Alfonse's explanation why there is no gl_IndexID stands, i.e. post-transform vertex cache wouldn't be possible if there would be a gl_IndexID.