is glEnableVertexAttribArray redundant?

Started by
9 comments, last by 21st Century Moose 10 years ago

I can't seem to understand the purpose glEnableVertexAttribArray. I know it enables glVertexAttribPointer, but I can't see any case where you would want to call glVertexAttribPointer with the attribute disabled. If you don't want to send the attribute, then you would just not call glVertexAttribPointer at all.

Advertisement

Yes and no.

Much of this is legacy from the old fixed pipeline (glEnable/DisableVertexAttribArray were modelled on glEnable/DisableClientState and first appeared in GL_ARB_vertex_program roundabout OpenGL 1.5: the older calls go back to 1.1) and dates to a time when you might be mixing vertex array drawing with immediate mode drawing. So you'd use vertex arrays for your complex meshes and maybe glBegin/glEnd for your 2D GUI.

VAOs didn't exist then so you'd need to be constantly enabling/disabling arrays, otherwise your current draw calls might pull in unwanted state from your previous draw calls. Obviously you wouldn't be using the same vertex format for everything (a mesh might have had position, normal, colour and 2 sets of texcoords; 2D GUI might just have position and a single set of texcoords) so a subsequent draw with a simpler format but more vertices might overrun the pointers used for a previous draw with a complex format but fewer vertices. In software T&L days that was death: your program would crash (if you were lucky: worst case is that it would continue running for a while and crash later on in some completely unrelated part of the code). Hardware T&L is more robust but back then you couldn't rely on everyone having it.

With VAOs - assuming you use them the way they're intended to be used - there's still a need for Enable but it could be argued that Disable is redundant. An attrib array must have a default state and that default state is disabled. If you try to draw from an enabled array but with no pointer set you'll crash too, so disabled is safer. glDisableVertexAttribArray is still handy to have around in case you ever want to modify a VAO at some time. And of course Enable must then exist if you ever want to go back again and modify it again. (None of that would exist if VAOs had been specified as immutable, but the ARB don't seem to have liked specifying immutable object types until quite recently. So in order for VAOs to be mutable both must exist.)

Unfortunately GL does carry a lot of legacy baggage like this around. Things that sometimes don't seem to make sense today can turn out to have a perfectly reasonable explanation when you wind the GL_VERSION back a few notches.

All of this goes away with GL_ARB_vertex_attrib_binding of course, which you should be using if possible.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Hi mhagain, thanks for the answer!

Right, so you're saying it's useful if you are re-using a VBO from the same shader?

Unfortunately, I'm using OpenGL ES so I can't use GL_ARB_vertex_attrib_binding.

Hi mhagain, thanks for the answer!

Right, so you're saying it's useful if you are re-using a VBO from the same shader?

Unfortunately, I'm using OpenGL ES so I can't use GL_ARB_vertex_attrib_binding.

So say you're drawing a mesh and it's shadow in two passes. You don't have VAOs.

The vertex format for the mesh is position, normal, texcoord.

The vertex format for the shadow is position only.

To save on data overhead in the second pass you have the mesh organized into 3 separate buffers: one for each attribute in the standard pass.

You set up to draw the mesh:

glBindBuffer (GL_ARRAY_BUFFER, vb_positions);

glEnableVertexAttribArray (0);

glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer (GL_ARRAY_BUFFER, vb_normals);

glEnableVertexAttribArray (1);

glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer (GL_ARRAY_BUFFER, vb_texcoords);

glEnableVertexAttribArray (2);

glVertexAttribPointer (2, 2, GL_FLOAT, GL_FALSE, 0, 0);

Then you set up the rest of your state and issue a draw call.

Now you come to draw the shadows. Because you've already got the buffer and array set up for positions, all you need to do is:

glDisableVertexAttribArray (2);

glDisableVertexAttribArray (1);

Then you set up the rest of your state and issue a draw call.

Now, think about what you need to do when drawing the next mesh?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

but if you are drawing a shadow you are using a different shader. I thought once you switched shaders everything is reset.

but if you are drawing a shadow you are using a different shader. I thought once you switched shaders everything is reset.

Nope, you can switch shaders and most states will stay the same.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Nope, you can switch shaders and most states will stay the same.

Right, I didn't know that. I presume switching the frame buffer doesn't effect it either?


Nope, you can switch shaders and most states will stay the same.

Right, I didn't know that. I presume switching the frame buffer doesn't effect it either?

States pretty much live in isolation unless it's documented otherwise.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Well, that means I had a potential bug in my program :-o. Thanks for your help anyways ;)

glDisableVertexAttribArray (2);

glDisableVertexAttribArray (1);

Then you set up the rest of your state and issue a draw call.

Now, think about what you need to do when drawing the next mesh?

The point here is that if the glEnable/DisableVertexAttribArray did not exist, one would not need to disable or enable in the first place. If the shadow shader doesn't use normals or texcoords, it could just ignore those attribute channels, and when the next shader is used with those channels, it would just pick them up in a no-op configure manner.

I've also pondered on the question "what if I'd just go and enable all attribute arrays always at engine startup, or in VAO case, right after creating a new VAO?" effectively making the default attribute state to be enabled, since unused attributes are always ignored anyways. There is that feature where you can disable an attribute array, and replace it with a single constant value for the whole attribute, but most consider that as a non-feature, so I would just ignore it. And the train of though always comes to this: what if there are some bad GL drivers out there that take a performance impact on setting up the unused vertex attribute data? So just to play safe, I've thought it to be better to just do the clean thing.

VAOs fortunately take this question away, since it moves the enables/disables to VAO creation time anyways, so it becomes an uninteresting question.

This topic is closed to new replies.

Advertisement