1
\$\begingroup\$

I have a question: if I have a VBO, and I have to change all the data inside it (because of resizing, ...), would it be more efficient to delete the old one and create a new one (and reallocate the memory for it), or change the data inside it using glBufferSubData?

\$\endgroup\$
1
\$\begingroup\$

According to OpenGL Insights there are a couple ways to efficiently stream data to a VBO. A simple but efficient method is to "orphan" the buffer (allocating a new piece of VRAM in the process) and refill the entire thing. This allows the current frame to use the old data while you can upload the new data.

// orphan the buffer so that we can write new data without waiting for it to be unused
// as soon as it's finished being used it will automatically be freed so we don't care about it anymore
glBufferData(GL_ARRAY_BUFFER, size, NULL, GL_STREAM_DRAW);

// fill the newly-allocated buffer with our new data
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STREAM_DRAW);

A more performant, but complex, solution is to disable synchronization. This will give you the most control over how and when you upload the data (and also let you do partial uploads), but it makes it possible to accidentally change a buffer that's being drawn from, causing graphical glitches.

The solution to this is to have several buffers (3 is a good number) that you switch between in a round-robin fashion, ensuring that you can instantly kick off a DMA (upload) without messing with a buffer that is being accessed. It's kind of like double buffering to prevent sending a scene to the monitor before it's completely built up. The article I linked goes more into depth with this technique.


Apologies for the C syntax, I don't use LWJGL but from what I've seen it's pretty much directly transferrable.

\$\endgroup\$
  • \$\begingroup\$ Thanks for your detailed answer - I now know what to do! C Syntax is not a problem, it's all the same... \$\endgroup\$ – Codeversum Jan 10 '15 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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