Since I thought this was suppose to allocate the buffer, if you call it twice, does the old one get deleted, or is there a memory leak?
I'm trying to decide to the best option for frequently changing the size of the buffer as necessary. Is the best option to use glBufferData? I think so, as long as there is no leak.
glBufferData (...)
will create a new data store, and any command queued up in the pipeline that used the old data store is implicitly protected by GL. That is, until all commands that used the VBO's old data are finished, GL is not allowed to delete the old data store. This is an important concept, as it can be useful for a technique called "buffer orphaning", where you re-allocate a VBO when you send it new data in order to reduce GL's synchronization overhead when updating data in a VBO that is currently being used for drawing.
Nothing is going to leak, and in some special circumstances (e.g. buffer streaming), re-allocating the data store for a VBO that is in-use can be more efficient than updating it. The ability to map and invalidate buffer ranges in newer versions of GL has largely mitigated this optimization strategy, but it is still something to think about.
The OGL documentation says: "glBufferData creates and initializes a new data store. Any pre-existing data-store bound to the target buffer will be deleted." See: http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml
So there will be no memory leaks when invoking glBufferData multiple times. Of course zero298 was right and you have to call glDeleteBuffer to finally free the memory allocated for a target.
Call glDeleteBuffers
glDeleteBuffers API
However, I really wouldn't recommend frequently changing the size of a buffer. Instead if you must change data in a buffer, just allocate a large buffer, pack it with the data you need and note what you can dereference in the buffer safely and only use those pieces of the buffer.
Constantly allocating and deleting an OpenGL buffer is slow. I'd imagine it's a lot slower than just rebuffering your data since you have to have the round trip from GPU to CPU to give you a new pointer to the buffer on the GPU and then you have to send the data back to the GPU and THEN you have to send another round-trip call to delete the buffer. So that 3 trips!
I mean, whenever you allocate arrays in C++, do you constantly new
and delete
the array every time you fill it or do you recycle it?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With