7

I've skimmed through the specs and OpenGL forum but couldn't really make sense of this:

Are the *BaseVertex version of the drawing commands supposed to add to the GLSL variable gl_VertexID? As it works, gl_VertexID contains the index taken from the the bound ELEMENT_ARRAY_BUFFER before basevertex is added to it.

So, my question is: Is this the correct behavior? I would assume that gl_VertexID should contain the index used to fetch the vertex.

genpfault
  • 47,669
  • 9
  • 68
  • 119
palle8x
  • 73
  • 5

2 Answers2

5

Yes this is the correct bahviour. The usage scenario of BaseVertex is, that you have to switch only this one value, instead of adjusting the buffer offsets into the vertex arrays with the gl*Pointer functions.

The idea is, that you can load the data from multiple meshes (model files) into a single VBO, without the need to adjust the indices.

datenwolf
  • 149,702
  • 12
  • 167
  • 273
  • Aye, makes sense. I guess something like "`gl_Element`" - which would contain the final computed index - would be a good complement to `gl_VertexID` in some future version of OpenGL/GLSL. Thank you. – palle8x Sep 17 '11 at 12:32
  • @palle8x I don't think so, as the index from the element array is more important, conceptually. And you can just add a uniform for the the baseVertex and add this to `gl_VertexID`, if you really need it. – Christian Rau Sep 17 '11 at 16:06
  • I agree, and what you describe is what I currently do in my code. But I still think there's a valid use case for something like "`gl_Element`", and (I believe) it wouldn't be too hard to implement in the driver. – palle8x Sep 17 '11 at 20:48
  • @palle8x: This would just add unnecessary complexity. Like Christian Rau already suggested you could easily add a uniform in which you pass the BaseVertex offset as well and do the math (a simple addition) in your shader. Since changing the BaseVertex happens between draw calls anyway, the overhead of changing a uniform is close to, if not even exactly 0 (commands are sent in batches consisting of a number of packets of fixed size. Changing a uniform and the Vertex Base may very well fit into one packet.) – datenwolf Sep 17 '11 at 21:41
  • True. It wouldn't enable you to do anything you couldn't do before. It would be purely a convenience feature. – palle8x Sep 18 '11 at 00:44
1

Your assumption is right. gl_VertexID should include BaseVertex offset.

opengl wiki about GLSL built-in vars:

Note: gl_VertexID​ will have the base vertex applied to it.

about glDrawElementsBaseVertex:

The gl_VertexID​ passed to the Vertex Shader will be the index after being offset by basevertex​, not the index fetched from the buffer.

Unfortunately, some old drivers didn't implement it correctly.

Ryhor Spivak
  • 198
  • 8