• Advertisement
Sign in to follow this  

is glEnableVertexAttribArray redundant?

This topic is 1369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 


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?

Share this post


Link to post
Share on other sites

 

 


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.

Share this post


Link to post
Share on other sites

 

 

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.

Share this post


Link to post
Share on other sites

If you just enable all attrib arrays, each one for which you don't make a glVertexAttribPointer call will take the default attrib pointer state.

 

The most important thing here is that the initial value for the pointer param is 0.

 

The second most important thing is that when you make a glVertexAttribPointer call, the driver takes a bunch of state and params, including the params to your glvertexAttribPointer call and the currently bound GL_ARRAY_BUFFER, and, saves them out as state for that attrib array.  So in other words, this and not anywhere else is the point at which your currently bound buffer takes effect.

 

If you've no GL_ARRAY_BUFFER bound your draw call will end up dereferencing a NULL pointer.

 

But the GL_ARRAY_BUFFER binding isn't established until you call glVertexAttribPointer so if you never call glVertexAttribPointer then you'll never have a GL_ARRAY_BUFFER binding for that attrib array.

 

So you'll always dereference a NULL pointer.

 

And you'll crash.

 

Go ahead, check out the OpenGL.org forums and you'll see a healthy enough supply of cries for help from people who get a crash inside a draw call and it turns out they've an array enabled but no glVertexAttribPointer set for it.

 

At this point one is just getting cutesy and trying to fight against the API rather than work with it and use it the way it was designed to be used.  That's not a clever thing to do.

 

(IIRC it's also illegal to have a GL_ARRAY_BUFFER of 0 when using VAOs too, so even if you have a really really robust driver - which you can't rely on your customers also having - best case is you'll get a bunch of GL errors).

 

Seriously - it's insane, don't do it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement