View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Particles Engine and Slow FPS

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.

42 replies to this topic

#1Medo3337  Members

Posted 12 January 2013 - 06:58 PM

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.

#2mhagain  Members

Posted 12 January 2013 - 08:25 PM

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.

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.

#3Nyssa  GDNet+

Posted 13 January 2013 - 03:01 AM

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.

#4Medo3337  Members

Posted 13 January 2013 - 03:28 AM

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.

#5Nyssa  GDNet+

Posted 13 January 2013 - 03:38 AM

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.

#6Medo3337  Members

Posted 13 January 2013 - 03:52 AM

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

#7Hodgman  Moderators

Posted 13 January 2013 - 03:53 AM

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

#8Nyssa  GDNet+

Posted 13 January 2013 - 04:09 AM

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

Posted 13 January 2013 - 04:20 AM

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.

#10Medo3337  Members

Posted 13 January 2013 - 07:29 PM

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.

#11mhagain  Members

Posted 13 January 2013 - 08:22 PM

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.

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.

#12Medo3337  Members

Posted 13 January 2013 - 11:36 PM

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.

#13L. Spiro  Members

Posted 13 January 2013 - 11:51 PM

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.
You have to help us help you.

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, 13 January 2013 - 11:52 PM.

#14Medo3337  Members

Posted 14 January 2013 - 01:12 AM

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

#15L. Spiro  Members

Posted 14 January 2013 - 01:31 AM

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, 14 January 2013 - 01:32 AM.

#16Medo3337  Members

Posted 14 January 2013 - 05:38 AM

I'm not using shaders, I'm updating each particle position D3DXVECTOR3 particles[i]->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.

#17L. Spiro  Members

Posted 14 January 2013 - 06:05 AM

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

#18Medo3337  Members

Posted 14 January 2013 - 11:06 PM

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.

#19L. Spiro  Members

Posted 14 January 2013 - 11:37 PM

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, 14 January 2013 - 11:44 PM.

#20Medo3337  Members

Posted 15 January 2013 - 04:50 AM

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

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.