# Particles Engine and Slow FPS

This topic is 1851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi!

I made point sprite based Particle Engine and sometimes I experience slow rendering when I create particles.

I'm trying to figure out why the particles are slowing down rendering.

##### Share on other sites

One draw call per particle?

You're really going to need to give some more info about how your particle engine is set up, even provide some of your code, because right now the best you'll get from anyone is a guess.  And the above is my guess, but without that additional info that's all I can do.

##### Share on other sites

Yep probably one draw call per particle. Batch your particles into a single draw call and that will speed things up. Another guess would be, depending on your code if your creating lots of particles in a single frame then that would cause that frame to be rather slow relative to frames that create no particles.

##### Share on other sites

Exactly, that's what I was expecting. one draw call per particle.

I have been trying to make all the particles draw in one single call, but have some problems.

How do I make each vertex have its own texture? Usually I use device->SetTexture() but this will set only one texture for all the particles which is a problem.

##### Share on other sites

So in that case, if different particles have different textures I'd group all the particles together that use the same texture then render each group, one draw call per group.

##### Share on other sites

@Nyssa: What if the same group use 2 textures?

##### Share on other sites

A common solution is to put multiple images into the same texture, then have each particle use only a portion of the entire texture.

##### Share on other sites
What if the same group use 2 textures?

Hodgmans solution would fix that up for you :)

Another solution would be to simply make the particles that use 2 textures another group. What I'm getting at is grouping all the particles that share the same states together. Hodgmans solution performance wise would be better though! .

##### Share on other sites

Yep, try to fit everything in one texture. You can even use 3D textures.

If you have order dependent transparency and multiple batches you can (will) run into problems, too.

##### Share on other sites

Currently, I'm trying to fill the vertex buffer and currently running into problems:

device->CreateVertexBuffer(particles.size() * sizeof(PARTICLE_VERTEX),
D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY|D3DUSAGE_POINTS,
CustomFVF,
D3DPOOL_DEFAULT,
&vbuffer,
NULL);
pVertices = new Particle_Vertex();
if (particles.size() > 0)
{
vbuffer->Lock(0, particles.size() * sizeof(Particle_Vertex), (void**)&pVertices, D3DLOCK_DISCARD);
for(int i = 0; i < particles.size(); ++i)
{
pVertices->pos = particles[i]->position;
pVertices++;
}
vbuffer->Unlock();

The particles are not rendering correctly, let me know how the above code should be fixed.

##### Share on other sites

You're really giving too little information to work with here.  There are some obvious things wrong with the above code, but without seeing, for example, what type pVertices is, without seeing what type particles is, without seeing the definitions of those types, without seeing some of your draw code - it's difficult to give you any genuinely helpful info.

##### Share on other sites

Here is the structure of pVertices:

struct Particle_Vertex {        D3DXVECTOR3 pos;        D3DXCOLOR color; };

"particles" is std::vector variable that contains information about each particle, I'm trying to fill the vertex buffer from std::vector particles.

##### Share on other sites

pVertices = new Particle_Vertex();

This is a memory leak. Remove this line of code. If you are doing this every frame, you will already see an improvement in your performance.

As for the rest, you do always have the option to actually describe what “incorrect” means. There are a million things it could be and the more information you provide the more easily we can guess where in the pipeline things are going wrong.

Did you actually create the vertex declaration? Correctly? Is the shader correct? Are the matrices passed to the shaders correct? Is your shader’s WORLD matrix set to identity? Did you try debugging with PIX?

L. Spiro Edited by L. Spiro

##### Share on other sites

Hi L. Spiro,

The problem is basically related to the vertex position, I'm setting each vertex position according to std::vector particles:

pVertices->pos = particles[i]->position;

But I notice invalid particles position and invalid movement as well.

I fill the vertex buffer from std::vector particles and then draw using the following code:

D3DXMATRIX worldMatrix;
D3DXMatrixTranslation(&worldMatrix, position.x, position.y, position.z);   // position is the emitter position
d3ddev->SetTransform(D3DTS_WORLD, &worldMatrix);
d3ddev->SetTexture(0, texture);
d3ddev->SetStreamSource(0, vbuffer, 0, sizeof(PARTICLE_VERTEX));
d3ddev->DrawPrimitive(D3DPT_POINTLIST, 0, particles.size());

##### Share on other sites
If the frame of reference inside the shader is in emitter-local coordinates, what is the frame of reference for particles[ i]->position?
Double-check that you are not filling world coordinates into the vertex buffer directly; if you are, the world matrix sent to the shader needs to be identity.
I don’t see where you ever set the vertex declaration.

What did PIX tell you?

L. Spiro Edited by L. Spiro

##### Share on other sites

I'm not using shaders, I'm updating each particle position D3DXVECTOR3 particles->position using for() { } statement

For the FVF:

#define CustomFVF (D3DFVF_XYZ | D3DFVF_TEX1)

device->SetFVF(CustomFVF);

Here is what I do:

First, I create the vertex buffer using device->CreateVertexBuffer() then I create array of Particle_Vertex and fill it with information from std::vector particles (position and color)

After that I render it using:

device->SetFVF(CustomFVF);
D3DXMATRIX worldMatrix;
D3DXMatrixTranslation(&worldMatrix, emitterPosition.x, emitterPosition.y, emitterPosition.z);
device->SetTransform(D3DTS_WORLD, &worldMatrix);
device->SetTexture(0, texture);
device->SetStreamSource(0, v_buffer, 0, sizeof(PARTICLE_VERTEX));
device->DrawPrimitive(D3DPT_POINTLIST, 0, particles.size());

Hope it's clear now.

##### Share on other sites

I guess you are updating the vertex declaration every frame. That answers 1 of my 3 points.
Let me clarify my previous reply.

#1: Don’t fill the vertex buffer with particles’ world coordinate if you are going to also be passing a non-identify world matrix to Direct3D. I just have an itching feeling that tells me you are storing raw world coordinates inside particles[ i]->position since that is how most of these kinds of implementations work.
#2: What did PIX tell you? I.e. I know you didn’t actually use PIX but I was trying to give you a chance to take it upon yourself and use it before your reply so that it would seem as though you used it on your own without having to be repeatedly told to do so.
PIX is our greatest source of knowledge in these kinds of situations and at this point no one will likely be able to help you until you have used it and posted some screenshots.

PIX is a tool that comes with the DirectX SDK.
Open it. Start a new project. Browse to your application. Hit the button that says it will take one screen capture when you hit F12, and start the “experiment”.
View your wrong particles and hit F12.
Close your game. PIX now allows you to step through every call you made to Direct3D. There are shortcuts to go directly to draw calls. Use them.
On the right side you have to click the Render tab. This will allow you to find the draw call that draws your particles.
In the list of commands, double-click the last vertex buffer you set (it will be blue and underlined). Check its contents. Are the positions all correct?

You can use this to verify a lot of things.
I suggest you spend a few hours with it before posting back here. You will typically be received better if you have shown a little effort to help yourself first. I don’t want to see you just checking the vertex buffers (or failing to find them because there are also many tutorials/resources online) and then just posting back with a quick, “OK I checked looks fine now what?”

Your mission, should you choose to just accept it, is to use this amazing new resource at your disposal to verify no fewer than 10 points and post back with a bullet list of no fewer than 5 things you checked/verified/thought suspicious.

L. Spiro

##### Share on other sites

The position problem was resolved by setting the world matrix to identity, it's now working perfectly, but still I see no difference.

The particles are slowing down rendering.

Do you still need information from PIX even after resolving the positioning issue?

Let me know what could be slowing down rendering, I'm now doing one draw call per emitter instead of one draw call per particle.

##### Share on other sites

Did you remove that memory leak I mentioned?

What do you means slowing down rendering?  All the time?  Over time getting unreasonably worse?  Over time getting reasonably worse (getting worse over time but in proportion to the number of particles being drawn)?

L. Spiro

Edited by L. Spiro

##### Share on other sites

Yes, I removed the line that was causing memory leak.

Not overtime getting worse, neither all the time its slowing rendering.. I'm getting slow rendering when I create some bullet impact smoke based on firing weapon (when I have 10 bullet hits for example I create 10 emitters and so on).

However, when I create rain/snow I don't have any problem.

Another problem is that when I create some sort of environmental smoke (like a blizzard smoke) I feel like it's flashing. (I didn't have this problem when I was doing one draw call per particle).

##### Share on other sites

When things start getting slow are the particles quite large and/or taking up a significant portion of the screen?  If so, then it's just simply overdraw and is expected behaviour.

##### Share on other sites

@mhagain: Not large at all, just a small smoke from bullet impact, when I draw some smoke on different bullet hits infront of the screen, I see well noticed slow rendering.

This means that whenever the player is shooting, the rendering will slow down due to smoke particles.

##### Share on other sites

[quote name='Medo3337' timestamp='1358314169' post='5022064']
This means that whenever the player is shooting, the rendering will slow down due to smoke particles.
[/quote]

Is the rendering slowing down only the moment the player shoots or is it slow as long as the particles are visible?

Possibly the way you allocate new particles is slow.

##### Share on other sites

Are you recreating the dynamic vertex buffer every frame? That's likely to cause slowness.

##### Share on other sites

I am doing the following every frame:

device->CreateVertexBuffer(particles.size() * sizeof(PARTICLE_VERTEX),
D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY|D3DUSAGE_POINTS,
CustomFVF,
D3DPOOL_DEFAULT,
&vbuffer,
NULL);

That could be slowing rendering, but I need to update the buffer length every frame since the number of particles will not stay the same.