PDA

View Full Version : Bind Buffer best practice?



Jeffg
03-17-2010, 07:02 PM
If your drawing an array every frame, should you add this after every draw?

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0);

Or is this something you do only at program exit?

Dark Photon
03-18-2010, 05:11 AM
If your drawing an array every frame, should you add this after every draw? ... Or is this something you do only at program exit?
Strictly speaking, neither is necessary.

Cheapest approach is to just update these bind points lazily as needed. That is, for example, never reset them to "0" as you are doing above until right before you need to draw a batch with client arrays.

...and if you never need to draw a batch with client arrays, you'll never do this at all.

Jeffg
03-20-2010, 05:54 AM
Cool.. ok similar question but with textures.

For each draw and for each texture I go through something like this..

enable texture
bind texture
disable texture

enable texture 2
bind texture 2
disable texture 2
So, is there any reason I need to enable and disable these textures in every draw. Can't I just enable them all at program startup and then just bind as needed? Wouldn't that be more efficient? I have about 10 textures.

Rosario Leonardi
03-20-2010, 07:58 AM
Jeffg it really depend on what you are doing and if you are using fixed or programmable pipeline.
With shaders you can use all available texture slot in a round robin style (you don't have to enable texture, just to bind and tell the shader witch slot to use).
With fixed pipeline you don't have to disable texture if the next draw call still use texture.
Leaving texture unit active can give some problem in big program, for example you draw something with a texture, then you want to draw a grid of lines. If you don't disable the texture unit your lines will be textured (even is you don't specify the UV).
One solution can be to write a render layer to manage those low level stuff.

V-man
03-20-2010, 08:11 AM
What I do is state tracking. I know what vbo is bound and what texture is bound to which unit and what client array is enabled.

Alfonse Reinheart
03-20-2010, 12:13 PM
With shaders you can use all available texture slot in a round robin style (you don't have to enable texture, just to bind and tell the shader witch slot to use).

I would advise something else. Just have global rules about what kinds of textures use which texture unit indices. All the diffuse textures go to texture unit 0, all detail maps go to texture unit 1, etc. Things like that. That way, you never have to change the program's state. You set the texture units into the program once, and never again.

Jeffg
03-20-2010, 07:50 PM
Thanks for all the great tips!