   Vertex Buffer Question
  Can you create and use only a single vertex buffer to use throughout your engine? The books I have been reading create a new vertex buffer locally for every render of an object. Why not use one.... 1) can it be done? 2) what extra things must you manage other than locking and unlocking? 3) why use more than one vertex buffer?Edit: I just relized that you do need to define the size of the vertex buffer so if you used a single one then you would have to define a max size. I am still not sure if that is a good idea or not. Please continue to disscuss my question thanks.[edited by - drastick on September 27, 2003 12:23:47 AM]
  1) Of course it can be done. It is generally a bad idea to have a single vertex buffer per object, unless it is a very large object.2) You will have to keep track of the location of each object in the buffer, but it's not that big of a deal.3) If your vertex buffers get too large, there might not be a space in video memory large enough to hold them. DirectX will have to keep it in system memory and send it to the card each frame which is very slow. However, make it too small and you will be switching vertex buffers too often.[edit] It's nice to see a reverse of the question for once . Usually people ask why they shouldn't have a single buffer per object...   [edited by - Raloth on September 27, 2003 12:12:47 AM];  Ok so for 2) I want to keep track of basicly where I have filled the buffer to (the buffers offset) if it is filled with multiple objects...Wate a sec. something popped in my head.. Say I have a render call to an object...I can just fill the the buffer with that object draw the object then it is no longer needed in the buffer correct? I then empty the buffer and move to the next object. So why hold more than one object in the buffer at a time? ;  > I then empty the buffer and move to the next object. The only thing in D3D that really corresponds to the idea of emptying the buffer is D3DLOCK_DISCARD, and this only applies to VBs created with D3DUSAGE_DYNAMIC.If your geometry is not changing every frame, then you should put it in a static VB (without D3DUSAGE_DYNAMIC) once and reuse it.  It''s only if the vertex values change frequently that D3DUSAGE_DYNAMIC becomes a good thing.  Skinned characters, particles, and sprites are common examples where this is appropriate.Understand that when you call DrawPrimitive/DrawIndexedPrimitive/etc., the geometry has *not* yet been rendered when the call returns.  The GPU operates in parallel with the CPU and there are often tens of milliseconds between when you issue a rendering command and when it''s actually executed by the hardware.  This is why vertex buffers and index buffers and their lock semantics are important -- to make sure that the app doesn''t change vertices that haven''t been drawn yet.What this means is that if you were to try to lock a static (non-dynamic) vertex buffer for every object for every frame, the CPU would have to sit and wait in the Lock call for the GPU to finish rendering those vertices before the memory is freed up and the Lock call can return.  This would be very bad for performance.With dynamic vertices, you have the D3DLOCK_DISCARD flag which gives the driver permission to, in a surreptitious sort of way, give you a new fresh chunk of memory to write to, leaving the original vertices intact if they haven''t been rendered yet.  This is good for performance *if* used correctly (see also D3DLOCK_NOOVERWRITE) and *if* the vertices are actually dynamic (i.e., the positions, colors, UVs, etc. are changing every frame or close to it).  If the vertices aren''t changing, it''s better to avoid D3DUSAGE_DYNAMIC because then the vertices can be put in memory that is less CPU-friendly and more GPU-friendly.You can find more infomation in the docs under "Programming Tips/Performance Optimizations/Using Dynamic Vertex and Index Buffers".  NVIDIA and ATI also provide excellent optimization docs that go into more detail about static vs. dynamic vertices, why it''s important to fill static vertices once and then leave them alone, how to handle dynamic vertices properly, and in general how to get the most out of CPU and GPU parallelism.(And for some damn reason I can''t post using my GameDev account.)--Donavon Keithley ;  Thanks, that gives me a much better idea of what the vertex buffer is and what it is doing.  