Jump to content

  • Log In with Google      Sign In   
  • Create Account


do i need to reserve space for vertex buffer?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 wantnon_cn   Members   -  Reputation: 115

Like
0Likes
Like

Posted 02 December 2012 - 02:14 PM

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

Edited by wantnon_cn, 02 December 2012 - 02:31 PM.


Sponsor:

#2 ic0de   Members   -  Reputation: 804

Like
0Likes
Like

Posted 02 December 2012 - 05:09 PM

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.

you know you program too much when you start ending sentences with semicolons;


#3 mhagain   Crossbones+   -  Reputation: 7578

Like
2Likes
Like

Posted 02 December 2012 - 06:58 PM

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


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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 wantnon_cn   Members   -  Reputation: 115

Like
0Likes
Like

Posted 09 December 2012 - 07:32 AM

thank you guys , now i am more clear with this question Posted Image

Edited by wantnon_cn, 09 December 2012 - 07:32 AM.


#5 C0lumbo   Crossbones+   -  Reputation: 2155

Like
0Likes
Like

Posted 09 December 2012 - 02:23 PM

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?

#6 BornToCode   Members   -  Reputation: 912

Like
0Likes
Like

Posted 25 December 2012 - 03:43 PM

Vbo are allocated on the gpu. So they are faster to access.

#7 mhagain   Crossbones+   -  Reputation: 7578

Like
0Likes
Like

Posted 26 December 2012 - 03:15 AM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS