Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" ?

like image 813
Johnny Pauling Avatar asked Jul 20 '12 14:07

Johnny Pauling


1 Answers

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, e.g. 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, e.g.:

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
like image 88
KillianDS Avatar answered Sep 30 '22 07:09

KillianDS