3

I'm having a bit of performance issue in my iOS/Android game where several VBO's have to be updated every once in a while. After profiling my game it turns out that glDeleteBuffers() takes up to 7ms per VBO update. This of course results in a hiccup when frames normally take only 4 ms to render.

Here's the part where I update my VBO:

Chunk* chunk;
pthread_join(constructionThread, (void**)&chunk);
building = false;

if (vboID)
{
    //takes 7 milliseconds
    glDeleteBuffers(1, &vboID); 
    vboID = 0;
}
if (offset)
{
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);

    //takes about 1-2 milliseconds, which is acceptable
    glBufferData(GL_ARRAY_BUFFER, offset * 4, constructionBuffer, GL_STATIC_DRAW);
}

where offset is an instance variable is basically the size of the new VBO, which is quite variable. vboID speaks for itself, I guess ;)

2
  • Why delete them if you're just going to recreate them?
    – harold
    May 26, 2012 at 19:03
  • 1
    because they were going to be different size anyway. I thought of them as regular arrays.
    – Brammie
    May 26, 2012 at 19:16

1 Answer 1

6

glGenBuffers and glDeleteBuffers are designed to only be run on initialization and cleanup, respectively. Calling them during runtime is bad.

glBufferData replaces the current buffer data with a new set of data, which automatically changes the size of the buffer. You can safely remove the whole glGenBuffers/glDeleteBuffers thing and move it into initialization and cleanup.

Additionally, you are creating the buffer as a static buffer. This is telling OpenGL that you will almost never change it so it stores it in a way that's quicker to access on the GPU but slower to access from the rest of the system. Try changing GL_STATIC_DRAW to GL_DYNAMIC_DRAW or GL_STREAM_DRAW. More on this here: http://www.opengl.org/wiki/Buffer_Objects#Buffer_Object_Usage

4
  • GL_DYNAMIC_DRAW is used when the buffer content are generated on GPU, and used for drawing: this doesn't seem the case of the question.
    – Luca
    May 26, 2012 at 19:17
  • He mentioned that glBufferData took 1-2ms, changing the usage hint can speed it up quite a bit. And the COPY hint is used for GPU-generated data that the user never directly reads from. May 26, 2012 at 19:29
  • 3
    @Luca: The STATIC/DYNAMIC/STREAM is about the frequency of changes, not where the content comes from. May 26, 2012 at 19:47
  • @Nicolas: you're right, as usual. I got a lapsus now, but luckily my personal abstraction layer implemented some year ago is correct... Not too bad.
    – Luca
    May 27, 2012 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.