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);
genpfault
  • 47,669
  • 9
  • 68
  • 119
Benoît Dubreuil
  • 564
  • 7
  • 22

1 Answers1

10

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.

Alexis Wilke
  • 15,168
  • 8
  • 60
  • 116
Reto Koradi
  • 49,246
  • 7
  • 76
  • 116
  • Excellent answer and thanks! You wouldn't happen to have a reference for this information would you? – Nicholas Miller Sep 09 '16 at 20:40
  • @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. – Reto Koradi Sep 10 '16 at 02:21
  • 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. – Nicholas Miller Sep 10 '16 at 02:35
  • 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`. – Nicholas Miller Sep 12 '16 at 06:28