Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the effects of unbinding OpenGL buffers?

Following along with the tutorials here to get an introduction to OpenGL 3.3, I understand that vertex and index buffers need to be bound with glBindBuffer() in order to issue commands to them. There is a mention that it is possible to unbind buffers by passing a handle of 0 to glBindBuffer(), which seems like a good idea to prevent accidentally using an incorrect buffer when you are done using it. Is there any reason to not always unbind vertex and index buffers after issuing setup or draw calls?

like image 928
masrtis Avatar asked Mar 20 '23 09:03

masrtis


1 Answers

Due to OpenGL's architecture as a state machine, such topics are often raised - and there is no definitive answer. The buffer bindings do influence the operations of various other GL commands, depending on the binding target.

In some cases, object 0 represenets some "no buffer/default object" case. For example, with GL_PIXEL_UNPACK_BUFFER, using 0 will allow you to transfer pixel data directly from client memory to the GL - so if you want to do that, you absolutely need to unbind any bound PBO at some point. If you do it as early as possible or as late as possible is up to you as the programmer, and depends heavily on the architecture of your software. The general rule should be to avoid unnecessary state changes, and that includs buffer (un)bind operations. But following this route often leads to situations where some state "leaks" out of the scope it really was meant to - and such stuff can be annoying to debug.

In other cases, 0 is not a valid state to do anything. For example, modern GL requires you to use VBOs. There is not really a need to ever have 0 bound as GL_ARRAY_BUFFER, as the only use case for that - specifying some attrib pointer to client memory - is gone. So unbinding a VBO is always a waste of time - the next time you set an attrib pointer, you have to bind some VBO anyways, and if you don't set an attrib pointer, that binding target is completely irrelevant.

like image 159
derhass Avatar answered Apr 25 '23 06:04

derhass