Jonathan
Jonathan

Reputation: 489

OpenGL VBO only being uploaded to GPU when rendered

My VBOs are only being sent to the GPU when they are used for the first time, this causes small freezes the first time an object/group of objects is drawn.

I tried loading the data this way:

glBufferData(GL_ARRAY_BUFFER, size, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, size, data);

and this way

glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);

But the result is the same.

If I then draw a triangle after glBufferData:

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, NULL);

then the problem is solved, but I find this solution rather hackish.

Is there a better solution?

(I have a bunch of small VBOs containing 256 vertices each)

Upvotes: 1

Views: 1219

Answers (2)

datenwolf
datenwolf

Reputation: 162164

Well, this is how Buffer Objects are supposed to work, namely adding somewhat asynchronous operation. The idea is, that you can upload a large bunch of Buffer Objects, and continue OpenGL operations afterwards, with the pipline stalling only, if data is accessed which upload has not been completed yet. glBufferData and glBufferSubData either make the pages of the pointer passed them CoW or make an interim copy, either way you can safely discard the data in your process after the call returned, the OpenGL client side will still have the data around for (the ongoing) upload process.

Calling glFinish() will block until the operations pipline has been completely finished (hence the name).

Upvotes: 1

Josh Rosen
Josh Rosen

Reputation: 1686

Try calling glFlush() after your glBufferData call.

Upvotes: 0

Related Questions