OpenGL glBindBuffer(0) outside vao?
Asked Answered
O

1

5

I currently do this to setup my vao:

glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

...

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBindVertexArray(0);

My question is: do I need to bind null buffers to prevent my vbo and ibo to change after I'm done with my vao or when bind a null vao it also unbinds current buffers? For example, I would do the following:

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Obstinate answered 20/8, 2014 at 19:18 Comment(0)
E
12

Generally you don't have to ever unbind your buffers explicitly. It shouldn't do any harm to keep them bound. They won't just spontaneously change. If other code is also using buffers, it needs to bind it's own buffers anyway before operating on them.

Unbinding the VAO is definitely a waste if you're using modern OpenGL (core profile). Every vertex setup and draw operation will have to bind a VAO anyway, so there's no need to unbind the previous VAO, and then just bind a different VAO shortly after.

But let's for a moment assume that you still want to unbind your buffers just to be more robust against possibly misbehaving code in your app, and you're willing to pay the performance penalty.

The answer is different for GL_ARRAY_BUFFER and GL_ELEMENT_ARRAY_BUFFER. The GL_ELEMENT_ARRAY_BUFFER binding is part of the VAO state. So if you unbind the VAO, that buffer will automatically be unbound as well.

The GL_ARRAY_BUFFER binding is not part of the VAO. In this case, you will have to explicit unbind the buffer.

Eddie answered 20/8, 2014 at 22:26 Comment(4)
Excellent answer and thanks! You wouldn't happen to have a reference for this information would you?Seibold
@NickMiller Which part specifically? I don't see anything in this answer that looks so questionable that it would need specific references. The basic behavior is in the spec documents. For example, which bindings are and are not part of the VAO state can be found in the state tables of the spec.Eddie
Didn't realize it was in the spec, at least I couldn't find it anyhow. I was concerned since the function description for glBindVertexArray makes no mention of unbinding GL_ELEMENT_ARRAY_BUFFER when binding to value of zero.Seibold
From the OpenGL 4.5 specification: "An INVALID_OPERATION error is generated by any commands which modify, draw from, or query vertex array state when no vertex array is bound. This occurs in the initial GL state [that is, when binding to the reserved value of 0], and may occur as a result of BindVertexArray or a side effect of DeleteVertexArrays.Seibold

© 2022 - 2024 — McMap. All rights reserved.