When it is safe to delete VBOs?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

When it is safe to delete VBOs?

nyholku
I must be dense and having a bad google day but I can't find definite answer to the question When can it is safe to call glDeleteBuffers?

As far as I understand the GPU processes the stuff in the VBO asynchronously so it might still be drawing stuff from the buffer when I call delete buffers? Do I have to guard against this?

So basically is this safe:


                                gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buffers[0]);
                                gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
                                gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
                                gl.glDrawArrays(GL.GL_LINE_STRIP, 0, n );
                                gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
                                gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0);
// safe to call glDeleteBuffers now ????
                                gl.glDeleteBuffers(buffers.length, buffers, 0);


Obviously I would not be calling delete there, inside my display method, every time
but draw stuff but point is that can I delete the buffer as soon *I* don't need it although
the GPU might still be using it?

I would have thought google would have found the answer in seconds but alas no...

br Kusti

Reply | Threaded
Open this post in threaded view
|

Re: When it is safe to delete VBOs?

gouessej
Administrator
Hi

At first, glDeleteBuffers frees the OpenGL identifier or marks it as available and triggers the destruction of the data store on the GPU. It doesn't destroy the storage on the CPU (for example if you create your own direct NIO buffer).

Secondly, rather call glDeleteBuffers from the same thread that performs the rendering by queuing a GLRunnable, see GLAutoDrawable.invoke. Don't delete a buffer if it might still be in use.

Thirdly, call it when you're sure you don't need it anymore. Rather use a buffer to contain the id instead of an array because a direct NIO buffer will be probably created under the hood in your case anyway.
Julien Gouesse | Personal blog | Website