Sign in to follow this  
Medo Mex

Particle Engine, Creating alot of Rain/Snow

Recommended Posts

Medo Mex    891
I have created a particle engine, basically, it create ONE single vertex using CreateVertexBuffer() and rendering it as much as particles we need.

Using the class I assign the number of particles that I want to generate at once, I am using "for() { }" statement to generate that number each time like the following:

[CODE]
void RenderParticles(...)
{
// Some other codes goes here...

for(DWORD i = 0; i < maxNumOfParticles; i++)
CreateNewParticle();

}

[/CODE]
When I create a rain usually I set a high number for maxNumOfParticles number like 150-250, so I can have alot of rains rendering (if the weather is so rainy).

However, the rendering SLOW down when 'maxNumOfParticles' is high, making the game slow even on a modern graphic card. Edited by Medo3337

Share this post


Link to post
Share on other sites
jefferytitan    2523
It's hard to tell from this sample how you're using it. Are you instantiating objects every frame? Are you drawing each particle with a separate call instead of batching? Is each particle literally one raindrop? I would think that if you had a few different rain textures (each containing a few randomly scattered raindrops with alpha), players wouldn't notice that each raindrop wasn't an individual particle.

Share this post


Link to post
Share on other sites
Medo Mex    891
[size=4]Each time DirectX render, I create particles according to [color=#000000][left]maxNumOfParticles, here is what I use on each frame:[/left][/color][/size]
[CODE]
for(DWORD i = 0; i < maxNumOfParticles; i++)
CreateNewParticle();
[/CODE]

Share this post


Link to post
Share on other sites
jefferytitan    2523
Hmm, I don't think we're quite on the same page. Here are my questions:[list=1]
[*]Does one particle = one raindrop?
[*]How many textures are you using for your particles?
[*]What does CreateNewParticle do?
[list=1]
[*]Are you allocating new vertices or textures in CreateNewParticle?
[*]Is each particle rendered as a separate batch? One thread on here concerned a developer who was drawing a circle using approximately 100 line segments. With begin and end after each line segment the speed was terrible. As one batch it was very fast.
[*]Does CreateNewParticle just perform rendering of an existing particle, or create a particle?
[*]Where is the code for moving a particle?
[/list]
[/list]

Share this post


Link to post
Share on other sites
Medo Mex    891
1. Yes, one particle = one raindrop.
2. I am using only one texture for the rain, each particle (raindrop) set the same texture.
3.
1. Only ONE single vertex created at loading time, and used in rendering to create different raindrops.
3. [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]CreateNewParticle() ONLY create new particles and it does that only during rendering process.[/background][/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)] 4. The code for moving particles is inside RenderParticles() method[/background][/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]Basically, I ONLY create ONE single vertex for ALL the particles, this vertex is rendered as many as the particles with different matrix world so I can get the particles to move in different positions, I am using point sprite.[/background][/left][/size][/font][/color]

Share this post


Link to post
Share on other sites
mhagain    13430
The key question being asked here is how many draw calls do you have? Do you have one draw call that handles all of your particles, or do you have one draw call per-particle? The fact that you have a different matrix for each particle suggests the latter, in which case - yes - it's going to perform terribly. You really need to post the code you have for CreateNewParticle and RenderParticle here - and the full code, not just an excerpt. It's not enough to describe what they do as it's not possible to correctly analyse why you're running slow based on a mere description - hence all the questions.

Share this post


Link to post
Share on other sites
dAND3h    220
As pointed out above, it seems as though you are rendering and updating for each particle! Think about that and you will see why it will slow down. If you have 10,000 particles, each one has to create a new transformation matrix (10,000+ operations) and each one will render itself separately(10,000+ draw calls).

Most modern particle systems use the GPU for both rendering and calculating positions. And then some sort of particle collision system on the CPU. Using the GPU , you will then batch alot of particles together, in a DynamicVertexBuffer which will be sent to the graphics card at once! Turning 10,000 draw calls into maybe 2 or 3!
You should post both the CreateNewParticle and RenderParticle methods. Edited by dAND3h

Share this post


Link to post
Share on other sites
DJTN    207
I'm confused because in your other post (that I just repied to) you stated you were using Point Sprites. In this post your refering to vertices, implying quads. In any event, like the above posts, we'll need to see your render function at the very least. Performing a transformation and a draw call for each particle is highly inefficient.

Share this post


Link to post
Share on other sites
Hornsj3    195
So I'm reading this and wondering. Would the best way to do this be to have 1 particle and instance it with separate transformation matrices? Sorry if I just hijacked your thread.

Share this post


Link to post
Share on other sites
DJTN    207
[quote name='Hornsj3' timestamp='1339175324' post='4947418']
So I'm reading this and wondering. Would the best way to do this be to have 1 particle and instance it with separate transformation matrices? Sorry if I just hijacked your thread.
[/quote]

I'd use a list of particles, updating their positions in a dynamic vertex buffer, calculate the world matrix once and then draw with the D3DDevice->DrawIndexedPrimitive (triangle list) method. But depending on your target hardware and version of DX, you can do almost all of it on the GPU these days. Edited by DJTN

Share this post


Link to post
Share on other sites
mhagain    13430
I personally use one vertex (containing initial position, velocity, gravity factors, acceleration, time-since-spawn, etc) with forward/left/up values as shader constants, combined with instancing - everything is computed on the GPU from there and up to 16384 (arbitrary choice) particles are drawn per draw-call. There's no need whatsoever for any matrix changes and it runs faster than a slippery snot on a slope. :)

Again - show some more code, and how many draw calls?

Share this post


Link to post
Share on other sites
jefferytitan    2523
I think the other posters are on the same page as me. You may be making savings on the CPU by using a single vertex, but you're punishing the GPU by making many separate draw calls. It would probably run better if you had 200 vertices but pushed them through in a single batch.

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