Sign in to follow this  
Christopher Harris

CreateVertexBuffer & Particles.

Recommended Posts

Hello, everyone. I'm curious as to how the best way to store particles is, or rather how to create a Vertex Buffer with an undefined/dynamic/changing amount of vertices. I don't want to ocupie more memmory than needed when storing my particles, but I also don't want the processor to slow down by recreating vertex buffers every time a particle is added/removed. Any suggestions, answers, or ideas are appreciated. Thanks!

Share this post


Link to post
Share on other sites
Flimflam    665
Well to be honest I dunno how well a vertex buffer with particles would work. You'd have to lock, edit, remove, etc particles every frame. If it was me, I'd probably either have a hard coded array of particle objects (struct or class) (ie, PARTICLE *ptc = new PARTICLE[500] etc), and run through it rendering with the DrawPrimitiveUP function if the particle is visible (hasn't died yet to be recreated at the base), or store them in an std::vector with a predefined size, and iterate through that, calling DrawPrimitiveUP.

I hope this helps :)

Share this post


Link to post
Share on other sites
DirectXFreak    166
I strongly disagree about the use of the DrawPrimitiveUP call, although I may be wrong. A more efficienct method of rendering particles, according to my education and experience, is to use a dynamic vertex buffer.

Here's what you can do: Create your vertex buffer with D3DUSAGE_DYNAMIC, D3DUSAGE_WRITEONLY, and possibly the D3DUSAGE_POINTS flags enabled. Then set the maximum vertex value to something reasonable, say 10% of your total particle amount (although this value can definately be tweeked).

Next, you store your particles in one big allocated array, which means don't use dynamic allocation.

At the rendering stage of your particle system, batch your particles. This means that you fill the vertex buffer (which holds a fixed portion to send to the graphics card), render it, fill it again, and repeat as necessary.

This is one efficient way to do it, but I'm sure there are others.
I hope you found this helpful =)

Share this post


Link to post
Share on other sites
JohnBolton    1372
Quote:
Original post by Christopher Harris
... I don't want to ocupie more memmory than needed when storing my particles, but I also don't want the processor to slow down by recreating vertex buffers ...


You can't have both. Either you have a big buffer that might be nearly empty on occasion, or recreate the buffer whenever the number of particles changes (by some significant amount). One compromise to have a smaller vertex buffer and fill it and draw it as many times as is needed to draw all the particles.

Share this post


Link to post
Share on other sites
MasterWorks    496
You want to draw all of your particles in ONE draw call. The advice about the dynamic vertex buffer is correct and it's the ONLY way you're going to get strong performance. You need to make a LARGE vertex buffer (dynamic) and keep using the empty parts of it each frame until it is full (after say 20 frames). Lock it with NOOVERWRITE flag to prevent the GPU stall; you're telling DirectX you're going to use a new part of the buffer only. When it's full lock with DISCARD and repeat. Do it right and you can get 100's of FPS even with fully dynamic particles that change every frame. Use an index buffer alongside your vertex buffer to make things even faster

The only drawback is the small amount of extra video memory since the buffer needs to be larger than necessary (otherwise you can't use the NOOVERWRITE flag, which makes it FAST.) But it's a VERY small price to pay, especially compared to other video resources; it uses very little memory. The reason you have to make this tradeoff is because it allows the GPU to keep rendering/using data from your previous frame as you construct the new one. No GPU stalls = fast.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this