do i need to reserve space for vertex buffer?

Started by
5 comments, last by 21st Century Moose 11 years, 4 months ago
i am using opengles 2.0. but i think the answer will be same with openGL.
i am using VBO. every frame i bind the vertexBuffer and then use glBufferData to submit a vertexArray to GPU.the size of the vertexArray is different each frame. so i wonder if the space of the vertexBuffer reallocated every time?
i guess if the vertexArray i submit this time is bigger than last time, the space of vertexBuffer on GPU side need to reallocate, and it may inefficient. am i right? do i need to reserve space for the vertexBuffer to avoid reallocation?

i read on the opengles 2.0 reference and found the information that if we call glBufferData with * data set to NULL, it will perform space reservation, for example: glBufferData(GL_ARRAY_BUFFER, n_byte, NULL, usage);
Advertisement
Yes it would be reallocated every time. What you should do is allocate a buffer using the method you described that is "big enough" for anything you will ever put into it. On a side note it is a bad practice to call glBufferData every frame (its really slow). What you should do if the vertices are changing every frame is allocate you buffer as GL_DYNAMIC_DRAW and use glBufferSubData to upload only the vertices that have changed.
In theory two Bad Things can happen:

  1. If the size is different the buffer will need to be reallocated.
  2. 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 return before 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.[/quote]

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).

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

thank you guys , now i am more clear with this question smile.png
That's a great answer from mhagain. I'm in a similar situation where a lot of my rendering is one-shot, variable sized stuff, also OpenGLES and I'm trying to work out how best to switch it over to using VBOs.

This might be veering a little off topic, but I just can't understand why VBOs should be the correct way to do things in this situation. It seems to me that in terms of expressing your wishes to OpenGL, then just pointing OpenGL at a bit of memory and saying "render this" seems to make more sense than creating/locking/unlocking buffers with just the right sizes/hints/setting and crossing your fingers that the driver is implemented in a sensible way.

Is there a reason that doing it with VBOs makes it possible for drivers to use more efficient code paths?
Vbo are allocated on the gpu. So they are faster to access.
Vbo are allocated on the gpu. So they are faster to access.

Not necessarily; GL doesn't make any promises about where it will allocate a VBO and is perfectly free to allocate one in system memory depending on usage hints (which it is free to ignore), current resource constraints, etc.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement