What are the effects of unbinding OpenGL buffers? - opengl

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?

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.

Related

How does openGL store VBO & 'GL_ARRAY_BUFFER' on GPU?

Though I've understood the state design of OpenGL enough to work with it, I've still struggled visualizing the inner workings on the CPU/GPU. Currently I'm stuck wondering how VBO's are copied onto the GPU using GL_ARRAY_BUFFER.
I previously understood the VBO's to be a 'state' of GL_ARRAY_BUFFER, this led me to 3 different options as to how that might work, shown below.
The top answer of the following thread explains: "The GL_ARRAY_BUFFER target for buffer objects represents the intent to use that buffer object for vertex attribute data"
What does the GL_ARRAY_BUFFER target mean in glBindBuffer?
which, as I understand it, means that GL_ARRAY_BUFFER is sort of a 'state' of a VBO. It could have a different state, but it does require to have a state to be able to be copied onto 'the' GPU buffer/ a smaller allocated buffer on the GPU? Or am I misunderstanding it? does 'binding' mean something different from setting a state? (this 4th option is not visualized below).
Are any of these 4 ideas accurate? or is it something completely different?
Apologies for the horrendous diagram. I hope it's clear enough though.
Any confirmation / correction would be greatly appreciated!
I also looked though this post
What exactly is a VBO in OpenGL?
which states
"Since you use GL_ARRAY_BUFFER here as the target parameter, this operation (glBufferData(...)) will affect the BO which is currently bound as GL_ARRAY_BUFFER.
After doing all that, the VBO now contains all the vertex data within.
Basically, yes."
this implies the VBO memory on the GPU is set through the GL_ARRAY_BUFFER, which is what I understood at first.
options 3 & 4 seem most logical to me. Could it be that VBO and GL_ARRAY_BUFFER are both states of eachother simultaneously, depending on the context?: which VBO is active / which target is active for the VBO
Frankly, it's not any of the options you listed. GL_ARRAY_BUFFER is a client-side binding point that's used to name the active buffer that you operate on with other commands. It's used purely in communications with the driver. Furthermore, by means of DSA (direct state access, OpenGL 4.5+) you can fully set up a VBO without ever binding it to anything.
This is probably a better diagram to describe the situation:
Note the GL_ARRAY_BUFFER binding point can be tracked either by the driver or the user-process libGL. It's really not essential.
glBufferData creates the buffer object and the data store of the buffer and optionally initializes the data. As long as glBufferData is not called there is no object and no reserved (GPU) data store. The buffer's size is immutable. glGenBuffers creates only the "name" of the buffer (the buffer ID).
Actually a buffer has no type. However, a buffer can be used for different things and to each target (like GL_ARRAY_BUFFER) a different buffer can be bound. GL_ARRAY_BUFFER is only used to identify the buffer in various API functions.

What exactly is a VBO in OpenGL?

I am trying to understand the theory behind OpenGL and I'm studying VBOs at the moment.
This is what I understand so far: when we declare a series of vertices, let's say 3 vertices that form a triangle primitive, we basically store those nowhere, they're simply declared in code.
But, if we want to store them somewhere we can use a VBO that stores the definition of those vertices. And, through the same VBO we send all that vertex info to the Vertex Shader (which is a bunch of code). Now, the VBO is located in the GPU, so we are basically storing all that info on the GPU's memory when we call the VBO. Then the Vertex Shader, which is part of the Pipeline Rendering process, "comes" to the GPU's memory, "looks" into the VBO and retrieves all that info. In other words, the VBO stores the vertex data (triangle vertices) and sends it to the Vertex Shader.
So, VBO -> send info to -> Vertex Shader.
Is this correct? I'm asking to make sure if this is the correct interpretation, as I find myself drawing triangles on screen and sometimes letters made up of many triangles with a bunch of code and functions that I basically learned by memory but don't really understand what they do.
To break it down:
// here I declare the VBO
unsigned int VBO;
// we have 1 VBO, so we generate an ID for it, and that ID is: GL_ARRAY_BUFFER
glGenBuffers(1, &VBO)
// GL_ARRAY_BUFFER is the VBO's ID, which we are going to bind to the VBO itself
glBindBuffer(GL_ARRAY_BUFFER, VBO)
// bunch of info in here that basically says: I want to take the vertex data (the
// triangle that I declared as a float) and send it to the VBO's ID, which is
// GL_ARRAY_BUFFER, then I want to specify the size of the vertex
// data, the vertex data itself and the 'static draw' thingy
glBufferData(...).
After doing all that, the VBO now contains all the vertex data within. So we tell the VBO, ok now send it to the Vertex Shader.
And that's the start of the Pipeline, jsut the beginning.
Is this correct? (I haven't read what VAOs do yet, before I get to that I'd like to know if the way I deconstruct VBOs in my mind is the right way, or else I'm confused)
I think you are mixing up lots of different things and have several confusions, so I'm try to work through most of them in the order you brought them up:
when we declare a series of vertices, let's say 3 vertices that form a triangle primitive, we basically store those nowhere, they're simply declared in code.
No. If you store data "nowhere", then you don't have it. Also you are mixing up declaration, definiton and initialization of variables here. For vertex data (like all other forms of data), there are two basic strategies:
You store the data somewhere, typically in a file. Specifying it directly in source code just means that it is stored in some binary file, potentially the executable itself (or some shared library used by it)
You procedurally generate the data through some mathematical formula or more general by some algortihm
Methods 1. and 2 can of course be mixed, and usually, method 2 will need some parameters (which itself need to be stored somewhere, so the parameters are just case 1 again).
And, through the same VBO we send all that vertex info to the Vertex Shader (which is a bunch of code). Now, the VBO is located in the GPU, so we are basically storing all that info on the GPU's memory when we call the VBO.
OpenGL is actually just a specification which is completely agnostic about the existence of a GPU and the existence of VRAM. And as such, OpenGL uses the concept of buffer objects (BOs) as some continuous block of memory of a certain size which is completely managed by the GL implementation. You as the user can ask the GL to create or destroy such BOs, specify their size, and have complete control of the contents - you can put an MP3 file into a BO if you like (not that there would be a good use case for this).
The GL implementation on the other hand controls where this memory is actually allocated, and GL implementations for GPUs
which actually have dedicated video memory have the option to store a BO directly in VRAM. The hints like GL_STATIC_DRAW are there to help the GL implementation decide where to best put such a buffer (but that hint system is somewhat flawed, and better alternatives exist in modern GL, but I'm not going into that here). GL_STATIC_DRAW means you intent to specify the contents once and use the may times as the source of a drawing option - so the data won't change often (and certainly not on a per-frame basis or even more often), and it might be a very good idea to store it in VRAM if such a thing exists.
Then the Vertex Shader, which is part of the Pipeline Rendering process, "comes" to the GPU's memory, "looks" into the VBO and retrieves all that info.
I think one could put it that way, although some GPUs have a dedicated "vertex fetch" hardware stage which actually reads the vertex data which is then fed to the vertex shaders. But that's not a really important point - the vertex shader needs to access each vertex' data, and that means the GPU will read that memory (VRAM or system memory or whatever) at some point before or during the execution of a vertex shader.
In other words, the VBO stores the vertex data (triangle vertices)
Yes. A buffer object which is used as source for the vertex shader's per-vertex inputs ("vertex attributes") is called a vertex buffer object ("VBO"), so that just follows directly from the definition of the term.
and sends it to the Vertex Shader.
I wouldn't put it that way. A BO is just a block of memory, it doesn't actively do anything. It is just a passive element: it is being written to or being read from. That's all.
// here I declare the VBO
unsigned int VBO;
No, you are declaring (and defining) a variable in the context of your programming language, and this variable is later used to hold the name of a buffer object. And in the GL, object names are just positive integers (so 0 is reserved for the GL as "no such object" or "default object", depending on the object type).
// we have 1 VBO, so we generate an ID for it, and that ID is: GL_ARRAY_BUFFER
glGenBuffers(1, &VBO)
No. glGenBuffers(n,ptr) just generates names for n new buffer objects, so it will generate n previously unused buffer names (and mark them as used) and returns them by writing them to the array pointed to byptr. So in this case, it just creates one new buffer object name and stores it in your VBO variable.
GL_ARRAY_BUFFER has nothing to do with this.
// GL_ARRAY_BUFFER is the VBO's ID, which we are going to bind to the VBO itself
glBindBuffer(GL_ARRAY_BUFFER, VBO)
No, GL_ARRAY_BUFFER is not the VBO's ID, the value of yourVBO variable is the VBO's ID (name!).
GL_ARRAY_BUFFER is the binding target. OpenGL buffer objects can be used for different purposes, and using them as the source for vertex data is just one of them, and GL_ARRAY_BUFFER refers to that use case.
Note that classic OpenGL uses the concept of binding for two purposes:
bind-to-use: Whenever you issue a GL call which depends on some GL objects, the objects you want to work with have to be currently bound to some (specific, depending on the use case) binding target (not only buffer objects, but also textures and others).
bind-to_modify: Whenever you as the user want to modify the state of some object, you have to bind it first to some binding target, and all the object state modify functions don't directly take the name of the GL object to work on as parameter, but the binding target, and will affect the object which is currently bound at that target. (Modern GL also has direct state access which allows you to modify objects without having to bind them first, but I'm also not going into details about that here).
Binding a buffer object to some of the buffer object binding targets means that you can use that object for the purpose defined by the target. But note that a buffer object doesn't change because it is bound to a target. You can bind a buffer object to different targets even at the same time. A GL buffer object doesn't have a type. Calling a buffer a "VBO" usually just means that you intent to use it as GL_ARRAY_BUFFER, but the GL doesn't care. It does care about what is buffer is bound as GL_ARRAY_BUFFER at the time of the glVertexAttribPointer() call.
// bunch of info in here that basically says: I want to take the vertex data (the
// triangle that I declared as a float) and send it to the VBO's ID, which is
// GL_ARRAY_BUFFER, then I want to specify the size of the vertex
// data, the vertex data itself and the 'static draw' thingy
glBufferData(...).
Well, glBufferData just creates the actual data storage for a GL buffer object (that is, the real memory), meaning you specify the size of the buffer (and the usage hint I mentioned earlier where you tell the GL how you intend to use the memory), and it optionally allows you to initialize the buffer by copying data from your application's memory into the buffer object. It doesn't care about the actual data, and the types you use).
Since you use GL_ARRAY_BUFFER here as the target parameter, this operation will affect the BO which is currently bound as GL_ARRAY_BUFFER.
After doing all that, the VBO now contains all the vertex data within.
Basically, yes.
So we tell the VBO, ok now send it to the Vertex Shader.
No. The GL uses Vertex Array Objects (VAOs) which store for each vertex shader input attribute where to find the data (in which buffer object, at which offset inside the buffer object) and how to interpret this data (by specifying the data types).
Later during the the draw call, the GL will fetch the data from the relevant locations within the buffer objects, as you specified it in the VAO. If this memory access is triggered by the vertex shader itself, or if there is a dedicated vertex fetch stage which reads the data before and forwards it to the vertex shader - or if there is a GPU at all - is totally implementation-specific, and none of your concern.
And that's the start of the Pipeline, just the beginning.
Well, depends on how you look at things. In a traditional rasterizer-based rendering pipline, the "vertex fetch" is more or less the first stage, and vertex buffer objects will just hold the memory where to fetch the vertex data from (and VAOs telling it which buffer objects to use, and which actual locations, and how to interpret them).
It all boils down to this: when you work in "normal" programs, all what you have is the CPU, caches, registers, main memory, etc.
However, when you work with computer graphics (and other fields), you want to use a GPU because it is faster for that particular task. The GPU is an independent computer on its own, with its own processor, pipeline and main even memory.
This means your program needs to somehow transfer all the data to the other computer and tell the other computer what to do with it. This is no easy task, so OpenGL simplifies things for you. Thus they give you an abstraction (VBO) that represents a buffer of vertices in the GPU, among many other abstractions for other tasks. Then they give you functions to create that resource (glGenBuffers), fill it with data (glBufferData), "bind it" to work with it (glBindBuffer), etc.
Remember, it is all a simplification for your benefit. In truth, the details of how everything is performed underneath is way more complex. Having abstractions like VBOs for vertices or IBOs for indexes makes it easier to work with them.

Reuse vertex attribute buffer as index buffer?

Can I use a VBO which I initialise like this:
GLuint bufferID;
glGenBuffers(1,&BufferID);
glBindBuffer(GL_ARRAY_BUFFER,bufferID);
glBufferData(GL_ARRAY_BUFFER,nBytes,indexData,GL_DYNAMIC_DRAW);
as an index buffer, like this:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bufferID);
/* ... set up vertex attributes, NOT using bufferID in the process ... */
glDrawElements(...);
I would like to use the buffer mostly as an attribute buffer and occasionally as an index buffer (but never at the same time).
There is nothing in the GL which prevents you from doing such things, your code above is legal GL. You can bind every buffer to every buffer binding target (you can even bind the same buffer to different targets at the same time, so it is even OK if attributes and index data come from the same buffer). However, the GL implementation might do some optimizations based on the observed behavior of the application, so you might end up with sub-optimal performance if you suddenly change the usage of an existing buffer object with such an approach, or use it for two things at once.
Update
The ARB_vertex_buffer_object extension spec, which introduced the concept of buffer objects to OpenGL, mentions this topic in the "Issues" section:
Should this extension include support for allowing vertex indices to be stored in buffer objects?
RESOLVED: YES. It is easily and cleanly added with just the
addition of a binding point for the index buffer object. Since
our approach of overloading pointers works for any pointer in GL,
no additional APIs need be defined, unlike in the various
*_element_array extensions.
Note that it is expected that implementations may have different
memory type requirements for efficient storage of indices and
vertices. For example, some systems may prefer indices in AGP
memory and vertices in video memory, or vice versa; or, on
systems where DMA of index data is not supported, index data must
be stored in (cacheable) system memory for acceptable
performance. As a result, applications are strongly urged to
put their models' vertex and index data in separate buffers, to
assist drivers in choosing the most efficient locations.
The reasoning that some implementations might prefer to keep index buffers in system RAM seems quite outdated, though.
While completely legal, it's sometimes discouraged to have attribute data and index data in the same buffer. I suspect that this is mostly based on a paragraph in the spec document (e.g. page 49 of the OpenGL 3.3 spec, at the end of the section "2.9.7 Array Indices in Buffer Objects"):
In some cases performance will be optimized by storing indices and array data in separate buffer objects, and by creating those buffer objects with the corresponding binding points.
While it seems plausible that it could be harmful to performance, I would be very interested to see benchmark results on actual platforms showing it. Attribute data and index data are used at the same time, and with the same access operations (CPU write, or blit from temporary storage, for filling the buffer with data, GPU read during rendering). So I can't think of a very good reason why they would need to be treated differently.
The only difference I can think of is that the index data is always read sequentially, while the attribute data is read out of order during indexed rendering. So it might be possible to apply different caching attributes for performance tuning the access in both cases.

How to check which Frame Buffer Object is currently bound in OpenGL?

I'm working with OpenGL Frame Buffer Objects. I have created a Frame Buffer Object with 2 color textures and a depth texture.
I'm using
glBindFramebuffer(GL_READ_FRAMEBUFFER, ID);
To bind my framebuffer, but on console i'm getting this warning
Redundant State change in glBindFramebuffer call, FBO 1 already bound
How can I check which of my framebuffers is already bound? I mean which OpenGL function allows me to check the ID of the already bound framebuffer so that I can prevent redundant binding.
Hold your horses... Yes, you can get the currently bound draw and read FBOs with:
GLint drawFboId = 0, readFboId = 0;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFboId);
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFboId);
and for backwards compatibility, GL_FRAMEBUFFER_BINDING is equivalent to GL_DRAW_FRAMEBUFFER_BINDING:
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &drawFboId);
But for the scenario you describe, you most likely do not want to use this. The diagnostic message tells you that you're making a redundant state change. But querying the current state to compare it with your new value is most likely much worse.
glGet*() calls can cause a certain level of synchronization, and be fairly harmful to performance. They should generally be avoided in performance critical parts of your code.
You have two options that are both likely to be better than what you were planning to do:
Ignore the diagnostic message. The driver will probably detect the redundant change, and avoid unnecessary work anyway. And it can do that much more efficiently than a solution that involves the app making glGet*() calls.
Keep track of the most recently bound FBO in your own code, so that you can filter out redundant changes without using any glGet*() calls.
In any case, what you had in mind would be like the proverbial "putting out fire with gasoline".
It's simply glGetIntegerv(GL_FRAMEBUFFER_BINDING, &result);

OpenGL - Vertex Array inside a VBO and Index & Texture Array outside?

Is it possible to place a set of Vertices into a VBO, but take the Index and Texture coord. Arrays from regular memory? If yes, which syntax to use?
Yes, it is possible to do this. But you shouldn't.
The reason to use buffer objects is to improve performance. Doing what you suggest simply reduces the performance you would have gained by properly using buffer objects.
Also, it's a driver path that most drivers don't see very often. Either people use buffer objects for vertex data, or they use client memory arrays. Since it's a road less traveled, you're more likely to encounter driver bugs.
The syntax is just the regular syntax. The gl*Pointer calls use buffer objects or not based on whether a buffer object is bound to GL_ARRAY_BUFFER at the time the gl*Pointer call is made. As such, you can bind a buffer to GL_ARRAY_BUFFER, make a gl*Pointer call with an offset, then bind 0 to GL_ARRAY_BUFFER and make a gl*Pointer call with an actual pointer.
Similarly, the glDraw*Elements* calls use a buffer object if a buffer is bound to GL_ELEMENT_ARRAY_BUFFER. So if you want to use client memory for these functions, bind 0 to that.