简体   繁体   中英

glGenBuffers - freeing memory?

When I use glGenBuffers to create a VBO with a pointer to it, I (think) am allocating memory. Subsequent calls to glGenBuffers should return different pointes, but what about the memory previously allocated? In the Nehe examples I didn't see a "free memory" call... and I read that openGL is kind of a "state machine", does this mean that if my initializeBuffers() function is called several times I don't need to free anything and just use the glGenBuffers "as it is" ?

For any glGenBuffers call you do, you have to do a corresponding glDeleteBuffers call.

Note that the pointer you pass to glGenBuffers is not allocated/deallocated. In fact, it could even be these calls do not actually allocate anything in cpu memory, but you have no guarantee of that either.

What you pass to glGenBuffers is an already-allocated array (or single value) that will store the "name" you reference a gpu-resource by, the workflow looks like this:

GLuint buffer; //Note: statically allocated
glGenBuffers(1, &buffer); // Allocate a GPU-resource - only updates value in buffer
//Work a bit with buffer
glDeleteBuffers(1, &buffer); //Deallocate the gpu-resource

The reason buffer is passed as a pointer is because you can create multiple buffers, eg like this:

GLuint buffer[3]; //Note: again statically allocated
glGenBuffers(3, &buffer); // Allocate a GPU-resource - update values in buffer
//Work a bit with buffer
glDeleteBuffers(3, &buffer); //Deallocate the gpu-resource

Of course, buffer could very well be dynamically allocated by you, eg:

std::vector<GLuint> buffer(3); //std::vector uses dynamic allocation
glGenBuffers(3, &buffer[0]); // Allocate a GPU-resource - update values in buffer
//Work a bit with buffer
glDeleteBuffers(3, &buffer[0]); //Deallocate the gpu-resource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM