In theory two Bad Things can happen:
- If the size is different the buffer will need to be reallocated.
- If the buffer is currently in use for drawing the pipeline must stall until it is no longer in use before the update can complete.
Either of these will cause you performance problems. Now, some drivers may get clever and decide to not reallocate if the new size is smaller than the old, but that's internal driver behaviour and shouldn't be relied on. Moving on.
Counter-intuitively, (1) may actually be significantly less of a performance problem than (2) is. The reason why is that with (1), if you've created your buffer with the proper usage flags, then the driver can make some intelligent decisions around how it allocates and releases memory. So it can decide to not release memory immediately but keep it hanging around for a few frames in case you need it again shortly, meaning that with (1) you reach a state where the driver is just handing you back a block of memory that you'd previously used a few frames ago each time you update, and no reallocations are actually happening at all (using the same size and usage flags for your glBufferData calls each time can clue the driver in on this being the behaviour you want).
In practice however you can rely on this behaviour fairly confidently (Doom 3 did it and you can bet that GPU vendors optimized around it's usage patterns). Regarding the usage flags, bear in mind here that they're just hints to the driver, and the driver is not actually obliged to honour them at all - AMD/ATI drivers (at least in the past) generally would completely ignore them and optimize the buffer around how your program actually used it instead. I'm not entirely certain if that's good behaviour or not...
(2) is where things can get nasty and cause you serious trouble. The way glBufferSubData is specified, it must not
the data copy has completed. Because of GPU latency and GPU/CPU asynchronous operation, that means that you may incur a pipeline stall of (typically) up to 3 frames worth of time. See http://www.opengl.or...fferSubData.xml
and especially note:
If any rendering in the pipeline makes reference to data in the buffer object being updated by glBufferSubData, especially from the specific region being updated, that rendering must drain from the pipeline before the data store can be updated.
The trick here is to call glBufferData with a NULL pointer, but with the same flags and size as when the buffer was first created - that will cause the driver to give you a new block of memory (hopefully using the pattern I described above where this memory is just something you'd previously used but the driver decided to keep for a while in case you need it again) but allow drawing to continue uninterrupted from the memory it was previously using. Then you can safely use glBufferSubData to update that without incurring any risk of stalling the pipeline.
With GL ES2 you're more or less stuck with this model; with ES3 (and GL3.x +, or older versions with the ARB_map_buffer_range extension) you've got a few more options that allow for creation of a proper streaming buffer pattern, similar to the tried-and-trusted D3D discard/no-overwrite pattern (which has been widely in use for well over a decade and is known to work well with dynamic buffer data).
Edited by mhagain, 02 December 2012 - 07:18 PM.