OpenGL Vertex Array/Buffer Objects
Asked Answered
P

2

5

Question 1

Do vertex buffer objects created under a certain VAO deleted once that VAO is deleted?

An example:

glGenBuffers(1, &bufferObject);
glGenVertexArrays(1, &VAO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, bufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(someVertices), someVertices, 
             GL_STATIC_DRAW);
glEnableVertexAttribArray(positionAttrib);
glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, NULL);

When later calling glDeleteVertexArrays(1, &VAO);, will bufferObject be deleted as well?

The reason I'm asking is that I saw a few examples over the web that didn't delete those buffer objects.

Question 2

What is the maximum amount of memory that I can allocate for buffer objects? It must be system dependent of course, but I can't seem find an estimation for it. What happens when video RAM isn't big enough? How would I know?

Perigynous answered 12/11, 2012 at 10:52 Comment(0)
G
8

Answer 1

It depends. If you called glDeleteBuffers before, it will be deleted when you delete the VAO. If not, it won't. The VAO holds a reference to the VBO, and so does your application after calling glGenBuffers. Both references need to be released before the VBO is deleted.

Answer 2

There's (afaik) no way to query the maximum amount you can allocate. However, according to the reference documentation, glBufferData will emit GL_OUT_OF_MEMORY if a buffer can not be allocated.

Glasser answered 12/11, 2012 at 11:3 Comment(2)
Do VBOs and VAOs need to be bound prior to deleting them?Offal
@StevenLu No, it does not need to be bound, but it can be. Quote from the spec page: If a buffer object that is currently bound is deleted, the binding reverts to 0 (opengl.org/sdk/docs/man/html/glDeleteBuffers.xhtml)Corruption
B
15

1: Buffer objects are not "created under" VAOs. Buffer object state is not part of VAO state. VAOs can reference buffer objects, but that association is only made by calling glVertexAttribPointer (or other *Pointer calls). Simply binding a buffer to GL_ARRAY_BUFFER does not put it in the VAO. You can bind buffers to that target without a VAO being bound. This is legal code:

glGenBuffers(1, &bufferObject);
glBindBuffer(GL_ARRAY_BUFFER, bufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(someVertices), someVertices, 
             GL_STATIC_DRAW);  //Creates the buffer storage.

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glEnableVertexAttribArray(positionAttrib);
glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, NULL); //Uses whatever is bound to GL_ARRAY_BUFFER

Note that this is not true of GL_ELEMENT_ARRAY_BUFFER. That binding is directly part of VAO state. So you need a VAO bound before you can bind to that target.

However, binding to a target is not creation. You can bind a buffer to GL_TRANSFORM_FEEDBACK_BUFFER, create it's storage with glBufferData, and then bind it later for use with GL_ARRAY_BUFFER. Or as a GL_ELEMENT_ARRAY_BUFFER.

As to the main thrust of your question, no. The destruction of a VAO will not destroy the buffer objects that it references.

2: There is no standard OpenGL function to detect the amount of available resources. If you attempt to create storage and the implementation is out of resources, you will get a GL_OUT_OF_MEMORY​ error.

Brachycephalic answered 12/11, 2012 at 11:4 Comment(0)
G
8

Answer 1

It depends. If you called glDeleteBuffers before, it will be deleted when you delete the VAO. If not, it won't. The VAO holds a reference to the VBO, and so does your application after calling glGenBuffers. Both references need to be released before the VBO is deleted.

Answer 2

There's (afaik) no way to query the maximum amount you can allocate. However, according to the reference documentation, glBufferData will emit GL_OUT_OF_MEMORY if a buffer can not be allocated.

Glasser answered 12/11, 2012 at 11:3 Comment(2)
Do VBOs and VAOs need to be bound prior to deleting them?Offal
@StevenLu No, it does not need to be bound, but it can be. Quote from the spec page: If a buffer object that is currently bound is deleted, the binding reverts to 0 (opengl.org/sdk/docs/man/html/glDeleteBuffers.xhtml)Corruption

© 2022 - 2024 — McMap. All rights reserved.