Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


is glEnableVertexAttribArray redundant?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Ed Welch   Members   -  Reputation: 489

Like
0Likes
Like

Posted 22 April 2014 - 05:48 PM

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.



Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 8281

Like
5Likes
Like

Posted 22 April 2014 - 07:05 PM

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.


Edited by mhagain, 22 April 2014 - 07:09 PM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 Ed Welch   Members   -  Reputation: 489

Like
0Likes
Like

Posted 23 April 2014 - 09:07 AM

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.



#4 mhagain   Crossbones+   -  Reputation: 8281

Like
3Likes
Like

Posted 23 April 2014 - 09:47 AM

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?


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 Ed Welch   Members   -  Reputation: 489

Like
0Likes
Like

Posted 23 April 2014 - 10:33 AM

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



#6 mhagain   Crossbones+   -  Reputation: 8281

Like
0Likes
Like

Posted 23 April 2014 - 10:42 AM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#7 Ed Welch   Members   -  Reputation: 489

Like
0Likes
Like

Posted 23 April 2014 - 10:49 AM

 


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?



#8 mhagain   Crossbones+   -  Reputation: 8281

Like
0Likes
Like

Posted 23 April 2014 - 10:59 AM

 

 


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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#9 Ed Welch   Members   -  Reputation: 489

Like
0Likes
Like

Posted 23 April 2014 - 11:05 AM

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



#10 clb   Members   -  Reputation: 1792

Like
0Likes
Like

Posted 23 April 2014 - 04:20 PM

 

 

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.


Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#11 mhagain   Crossbones+   -  Reputation: 8281

Like
0Likes
Like

Posted 23 April 2014 - 04:52 PM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS