Advertisement Jump to content
Sign in to follow this  
Ed Welch

is glEnableVertexAttribArray redundant?

This topic is 1728 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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!