Jump to content

  • Log In with Google      Sign In   
  • Create Account

Proper use of VAO


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
17 replies to this topic

#1 B_old   Members   -  Reputation: 668

Like
0Likes
Like

Posted 22 August 2013 - 05:00 AM

I have some experience with D3D11 but am currently looking into OpenGL.

 

I noticed that in OpenGL both the vertex buffers and the vertex layout are encapsulated together in a vertex array object.

Suppose I have a properly set up VAO and now decide that I want to use different vertex buffers that contain data in the same layout as before. At first I though I have to create a new VAO for this.

 

But then I stumbled over this quote in a text about vertex specifiation:

 

 

Better yet, if you want to use the same format but move the buffer around, it only takes one function call; namely glBindVertexBuffer with a buffer binding of 0. [...] Note again that all of the above state is still VAO state. It is all encapsulated in vertex array objects.
regarding the usage of glVertexAttribBinding and glVertexAttribFormat. So I assume there muss be a better way.
 
My new theory on the proper usage of VAO is that I set them up with glVertexAttribFormat according to my vertex layout once and when I want switch buffers I bind the VAO but only change the glVertexAttribBinding.
Is this correct?


Sponsor:

#2 LorenzoGatti   Crossbones+   -  Reputation: 2762

Like
0Likes
Like

Posted 23 August 2013 - 01:54 AM

Phrases like "move the buffer around" and "switch buffers" and "proper usage" are vaguer than they seem, considering that vertex array objects are an entirely optional superstructure to organize buffers and other objects: you can put into a VAO the vertex drawing state that used to be global, with a few new VAO-aware functions.

 

The quoted wiki passage means only that you can bind any VBO to the same "binding point" of a VAO, and it will be interpreted according to the VAO's layout, specified once independently of what buffers are bound. You need to call glVertexAttribBinding when you bind different buffers only if they are laid out differently after all.

 

If you want to define a vertex and vertex buffer layout once and use it with different buffer content, you it's very likely that since they have the same layout your data sets can live together in the same VBOs in, letting you use indexes to draw one or the other without binding or memory tricks.

glVertexAttribBinding

Produci, consuma, crepa

#3 B_old   Members   -  Reputation: 668

Like
0Likes
Like

Posted 23 August 2013 - 06:54 AM

Phrases like "move the buffer around" and "switch buffers" and "proper usage" are vaguer than they seem, considering that vertex array objects are an entirely optional superstructure to organize buffers and other objects: you can put into a VAO the vertex drawing state that used to be global, with a few new VAO-aware functions.

Do you mean by optional, that I don't have to use VAOs at all?

 

If you want to define a vertex and vertex buffer layout once and use it with different buffer content, you it's very likely that since they have the same layout your data sets can live together in the same VBOs in, letting you use indexes to draw one or the other without binding or memory tricks.

I was thinking about a scenario where I calculate the lighting for a model and the shader expects normals, tangents etc.. Later I calculate the the shadow where only positions are expected. If the normals and tangents reside in a separate buffer there is no need to bind it in that case.

 

I was thinking along the lines of D3D's InputLayout.


Edited by B_old, 23 August 2013 - 07:20 AM.


#4 LorenzoGatti   Crossbones+   -  Reputation: 2762

Like
0Likes
Like

Posted 23 August 2013 - 10:20 AM

 

If you want to define a vertex and vertex buffer layout once and use it with different buffer content, you it's very likely that since they have the same layout your data sets can live together in the same VBOs in, letting you use indexes to draw one or the other without binding or memory tricks.

I was thinking about a scenario where I calculate the lighting for a model and the shader expects normals, tangents etc.. Later I calculate the the shadow where only positions are expected. If the normals and tangents reside in a separate buffer there is no need to bind it in that case.

You might set up two vertex array objects, one for the lighting pass state and one for the shadow pass state, and two vertex buffer objects, one containing positions and one containing "normals, tangents, etc."; then you could bind the positions buffer in both vertex array objects and the other buffer in the lighting pass VAO only.

When you bind the appropriate VAO, OpenGL automatically recalls what vertex buffers are in use, and their layout.


Produci, consuma, crepa

#5 l0calh05t   Members   -  Reputation: 816

Like
2Likes
Like

Posted 23 August 2013 - 10:45 AM


vertex array objects are an entirely optional superstructure to organize buffers

 

This is not true for core OpenGL. See for example http://www.opengl.org/wiki/GLAPI/glVertexAttribPointer which states "GL_INVALID_OPERATION​ is generated if no vertex array object is bound."



#6 B_old   Members   -  Reputation: 668

Like
0Likes
Like

Posted 23 August 2013 - 11:24 AM

You might set up two vertex array objects, one for the lighting pass state and one for the shadow pass state, and two vertex buffer objects, one containing positions and one containing "normals, tangents, etc."; then you could bind the positions buffer in both vertex array objects and the other buffer in the lighting pass VAO only.

When you bind the appropriate VAO, OpenGL automatically recalls what vertex buffers are in use, and their layout.

That sounds complicated.

I was hoping there is a method more akin to D3D. It is easier to keep track off many vertex buffers and a few input layouts than many vertex buffers and two times (or more) that many vertex array objects.



#7 mhagain   Crossbones+   -  Reputation: 8275

Like
1Likes
Like

Posted 23 August 2013 - 01:54 PM

 


vertex array objects are an entirely optional superstructure to organize buffers

 

This is not true for core OpenGL. See for example http://www.opengl.org/wiki/GLAPI/glVertexAttribPointer which states "GL_INVALID_OPERATION​ is generated if no vertex array object is bound."

 

 

At the same time, it remains semi-optional; a common tactic is to create and bind a single VAO after context creation, following which you can continue to code as before and pretend VAOs never existed.


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.


#8 l0calh05t   Members   -  Reputation: 816

Like
0Likes
Like

Posted 23 August 2013 - 02:02 PM

 

 


vertex array objects are an entirely optional superstructure to organize buffers

 

This is not true for core OpenGL. See for example http://www.opengl.org/wiki/GLAPI/glVertexAttribPointer which states "GL_INVALID_OPERATION​ is generated if no vertex array object is bound."

 

 

At the same time, it remains semi-optional; a common tactic is to create and bind a single VAO after context creation, following which you can continue to code as before and pretend VAOs never existed.

 

I wouldn't really recommend it, but it is possible, yes. Even so you do need at least one VAO, so "entirely optional" isn't entirely true.



#9 B_old   Members   -  Reputation: 668

Like
0Likes
Like

Posted 23 August 2013 - 03:06 PM

I wouldn't really recommend it, but it is possible, yes. Even so you do need at least one VAO, so "entirely optional" isn't entirely true.

Would you say, that the proper OpenGL style is to have two VAO per model for instance? One for the lighting/g-buffer passe(s) and another for shadow passes.

 

In that case I don't understand the benefit of the 4.3 glBindVertexBuffer(), because it now seems it should still happen in a initialization phase.



#10 mhagain   Crossbones+   -  Reputation: 8275

Like
1Likes
Like

Posted 23 August 2013 - 03:24 PM

 

 

 


vertex array objects are an entirely optional superstructure to organize buffers

 

This is not true for core OpenGL. See for example http://www.opengl.org/wiki/GLAPI/glVertexAttribPointer which states "GL_INVALID_OPERATION​ is generated if no vertex array object is bound."

 

 

At the same time, it remains semi-optional; a common tactic is to create and bind a single VAO after context creation, following which you can continue to code as before and pretend VAOs never existed.

 

I wouldn't really recommend it, but it is possible, yes. Even so you do need at least one VAO, so "entirely optional" isn't entirely true.

 

 

Well id do it, Valve do it, so it seems (at least on the surface) to be viable.  Despite that, I do find VAOs useful as a way to avoid having to track lots of state, but I'm still looking forward to GL_ARB_vertex_attrib_binding being more widely available.


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.


#11 l0calh05t   Members   -  Reputation: 816

Like
0Likes
Like

Posted 23 August 2013 - 03:35 PM

 

I wouldn't really recommend it, but it is possible, yes. Even so you do need at least one VAO, so "entirely optional" isn't entirely true.

Would you say, that the proper OpenGL style is to have two VAO per model for instance? One for the lighting/g-buffer passe(s) and another for shadow passes.

 

In that case I don't understand the benefit of the 4.3 glBindVertexBuffer(), because it now seems it should still happen in a initialization phase.

 

Why would you need two different VAOs for that? Just don't use the normal information in the shadow pass shaders. If you maintain consistent attribute locations between shaders, a single VAO per mesh should do, IMO.



#12 l0calh05t   Members   -  Reputation: 816

Like
0Likes
Like

Posted 23 August 2013 - 03:37 PM

 

I wouldn't really recommend it, but it is possible, yes. Even so you do need at least one VAO, so "entirely optional" isn't entirely true.

 

 

Well id do it, Valve do it, so it seems (at least on the surface) to be viable.  Despite that, I do find VAOs useful as a way to avoid having to track lots of state, but I'm still looking forward to GL_ARB_vertex_attrib_binding being more widely available.

 

It is obviously doable/viable but why track all that state yourself, when the API can do it for you (which may have the side effect of being more efficient)?


Edited by l0calh05t, 23 August 2013 - 03:37 PM.


#13 B_old   Members   -  Reputation: 668

Like
0Likes
Like

Posted 25 August 2013 - 01:04 PM

Why would you need two different VAOs for that? Just don't use the normal information in the shadow pass shaders. If you maintain consistent attribute locations between shaders, a single VAO per mesh should do, IMO.

But wouldn't you waste bandwidth that way?



#14 B_old   Members   -  Reputation: 668

Like
0Likes
Like

Posted 25 August 2013 - 02:25 PM

Well id do it, Valve do it, so it seems (at least on the surface) to be viable.  Despite that, I do find VAOs useful as a way to avoid having to track lots of state, but I'm still looking forward to GL_ARB_vertex_attrib_binding being more widely available.

Did you get this info from this presentation about porting the source engine to OpenGL? Because I did not quite understand the part about VAOs.



#15 l0calh05t   Members   -  Reputation: 816

Like
0Likes
Like

Posted 26 August 2013 - 02:10 AM

 

Why would you need two different VAOs for that? Just don't use the normal information in the shadow pass shaders. If you maintain consistent attribute locations between shaders, a single VAO per mesh should do, IMO.

But wouldn't you waste bandwidth that way?

 

Unlikely. As the driver knows which attributes are actually used by the shader and the shader has no reason to access that memory. Performance will not be optimal if your attributes are interleaved, but unbinding the interleaved attributes won't change that. In any case, if you need performance: profile.



#16 B_old   Members   -  Reputation: 668

Like
0Likes
Like

Posted 26 August 2013 - 05:44 AM

Unlikely. As the driver knows which attributes are actually used by the shader and the shader has no reason to access that memory. Performance will not be optimal if your attributes are interleaved, but unbinding the interleaved attributes won't change that. In any case, if you need performance: profile.

Even if the data is not accessed it still takes up bandwidth if the data is interleaved, right?

But if you only partly interleave the vertex data and partly keep it in separate streams you can potentially skip a stream thus saving bandwidth.



#17 mhagain   Crossbones+   -  Reputation: 8275

Like
1Likes
Like

Posted 26 August 2013 - 09:12 AM

 

Unlikely. As the driver knows which attributes are actually used by the shader and the shader has no reason to access that memory. Performance will not be optimal if your attributes are interleaved, but unbinding the interleaved attributes won't change that. In any case, if you need performance: profile.

Even if the data is not accessed it still takes up bandwidth if the data is interleaved, right?

But if you only partly interleave the vertex data and partly keep it in separate streams you can potentially skip a stream thus saving bandwidth.

 

 

But at the higher cost of drawing non-interleaved verts.

 

This is one that there is no "correct in every case" answer to.  One really needs to profile and find which option is best for one's own case, not forgetting to watch out for factors that you can't directly measure (i.e. bandwidth cost can be measured but the higher cost of drawing non-interleaved data is not immediately obvious).


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.


#18 B_old   Members   -  Reputation: 668

Like
0Likes
Like

Posted 27 August 2013 - 01:02 AM

But at the higher cost of drawing non-interleaved verts.

 

This is one that there is no "correct in every case" answer to.  One really needs to profile and find which option is best for one's own case, not forgetting to watch out for factors that you can't directly measure (i.e. bandwidth cost can be measured but the higher cost of drawing non-interleaved data is not immediately obvious).

Fair enough.

Could you please elaborate more on Valve's VAO usage? Are you referring to the presentation I linked?






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