Determine primitive id

Lets say I want to render a bunch of triangle strips (or any other primitive) using an index buffer with a restart index.

Is there anyway in the shader to determine which vertex number is currently being rendered from the perspective of when the primitive was started? For example I might want to know that I’m rendering the third vertex of a quad.

That’s an interesting question. If you were using straight glDrawArrays(), seems like a simple modulo of gl_VertexID would do. However in your case, sounds like you might want to use a geometry shader:


for(int i = 0; i < gl_VerticesIn; i++)
{
  ...
  EmitVertex();
}

This is for a sprite engine and I am using a geometry shader.

I want to provide backup method for older systems that don’t support geometry shaders. Ideally I’d like to just make the index buffer have 4 copies of the same index and then a restart index. Like so:
With geometry shader: GL_POINTS 3 7 5…
W/o geometry shader: GL_TRIANGLE_STRIP 3 3 3 3 R 7 7 7 7 R 5 5 5 5 R …

That way I don’t have to duplicate vertex data in my data vbo. It doesn’t look like there would be any possible way to accomplish this in the shader though. I probably just have suck it up and duplicate the vertex data and add an atribute to mark which vertex it is. Essentially duplicating the work of the geometry shader in the vertex shader.

I think you might be able to do it without adding a special attribute. Consider if your index list looks like this:


0,1,2,3, 4,5,6,7, 8,9,10,11, ...

That is, you just don’t share verts between triangles. Then gl_VertexID % 4 gives you which vert in the quad you are. This is (I think) effectively identical to just chucking the index list and using glDrawArrays to render it (again, gl_VertexID % 4 to get which vert).

Alternatively, in your legacy setup you’ll need texcoords (prob. (0,0), (0,1), (1,1), (1,0)). You can have that specified per vertex and then that’ll (effectively) tell you which vertex you are. Gives you which direction to spread your vert from the center of the sprite quad.

By chunking the index buffer and having some magical way to determine primitive id I would only need 1 vertex per sprite at a cost of 5x increase of size in the index buffer. In the vertex shader I could branch on the primitive id and expand the sprite vertex based on that to create a quad. The only reason I’d like to have a primitive id is for memory savings in vertices.

By using glDrawArrays I would have access to gl_VertexID % 4 but there is no way that I am aware of to tell glDrawArrays to render each vertex 4 times. I also don’t think you can do primitive restart with glDrawArrays. Having one glDraw call for every sprite would destroy performance. I think its possible to emulate primitive restart with instanced rendering, but thats a new feature which should not be relied on in a legacy rendering method.

I think I have to make 4 copies of each vertex, at which point its just as easy (and probably more efficient to avoid branching in the shader) to add the sprite offset to each vertex attribute and forget entirely about gl_VertexID.

What about an instanced draw call? Either draw_instanced or instanced_arrays.

That is, instance your sprite quad.

If you just need this to draw sprites on pre-geometry shader hardware, you should check if point sprites fit your needs. They have been required since OpenGL 2.0, so practically any GLSL capable hardware will have support for point sprites too.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.