• FEATURED

View more

View more

View more

\$3

### 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

### #21mhagain  Members

Posted 15 January 2013 - 08:41 AM

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.

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.

### #22Medo3337  Members

Posted 15 January 2013 - 11:29 PM

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

Posted 17 January 2013 - 02:13 PM

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

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.

### #24Postie  Members

Posted 17 January 2013 - 05:27 PM

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

Currently working on an open world survival RPG - For info check out my Development blog:

### #25Medo3337  Members

Posted 18 January 2013 - 02:45 AM

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.

### #26mhagain  Members

Posted 18 January 2013 - 04:15 AM

Yep, I think we've found our culprit.  MSDN (and your DXSDK) have an article on the proper way to do this; see: http://msdn.microsoft.com/en-us/library/windows/desktop/bb147263%28v=vs.85%29.aspx#Using_Dynamic_Vertex_and_Index_Buffers

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.

### #27Medo3337  Members

Posted 18 January 2013 - 04:47 PM

I am now using CreateVertexBuffer() once and doing Lock() and Unlock() on the vertex buffer every frame, still have performance issue.

### #28L. Spiro  Members

Posted 18 January 2013 - 05:06 PM

Use 2 vertex buffers and switch between them each frame.

Make sure you implement it so that you can expand it to 3 vertex buffers in the future, or 4, or N.

In other words, make it an array and use a macro or enum to determine the size of the array.

L. Spiro

### #29Medo3337  Members

Posted 18 January 2013 - 05:52 PM

Created 2 vertex buffers:

#define maxVBuffer 2
for (int i = 0; i < maxVBuffer; i++)
{
device->CreateVertexBuffer(10000 * sizeof(PARTICLE_VERTEX),
D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY|D3DUSAGE_POINTS,
CustomFVF,
D3DPOOL_DEFAULT,
&vbuffer[i],
NULL);
}

And switched between them every frame, I even tried to use more than 2 buffers and switch between them every frame.

Still having the same performance problem.

### #30mhagain  Members

Posted 18 January 2013 - 06:05 PM

Here's one example of a way of doing this that I coded up (but didn't test/etc), fairly rough and ready but it should illustrate the way things are done. PLEASE don't just copy & paste this code; use it as a way of understanding how dynamic vertex buffers should be handled instead, read the link I posted earlier and see how what's done here matches with what's described there.

class CDynamicVertexBuffer
{
public:
CDynamicVertexBuffer (IDirect3DDevice9 *device, int maxitems, int itemsize) :
Buffer (NULL),
MaxItems (maxitems),
ItemSize (itemsize)
{
assert (device != NULL);
assert (maxitems > 0);
assert (itemsize > 0);

// AddRef the device because this needs to hold a reference to it
this->Device = device;

this->OnResetDevice ();
}

~CDynamicVertexBuffer (void)
{
this->OnLostDevice ();

if (this->Device)
{
this->Device->Release ();
this->Device = NULL;
}
}

void OnLostDevice (void)
{
if (this->Buffer)
{
this->Buffer->Release ();
this->Buffer = NULL;
}
}

void OnResetDevice (void)
{
assert (this->Buffer == NULL);
assert (this->Device != NULL);

HRESULT hr = E_FAIL;

hr = this->Device->CreateVertexBuffer (
this->ItemSize * this->MaxItems,
D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,
0,    // FVF is only used if we're calling ProcessVertices, which we're not, so we don't set it
D3DPOOL_DEFAULT,
&this->Buffer,
NULL
);

assert (SUCCEEDED (hr));

this->CurrItem = 0;
}

void AddItems (const void *itemdata, int numitems)
{
assert (itemdata != NULL);
assert (numitems > 0);
assert (this->Buffer != NULL);

// by default we're just appending to the buffer
DWORD locktype = D3DLOCK_NOOVERWRITE | D3DLOCK_NOSYSLOCK;
void *lockdata = NULL;
HRESULT hr = E_FAIL;

// check will these items overflow the buffer; if so flush it and rewind
if (this->CurrItem + numitems >= this->MaxItems)
{
this->Draw ();
this->CurrItem = 0;
}

hr = this->Buffer->Lock (
this->CurrItem * this->ItemSize,
numitems * this->ItemSize,
(void **) &lockdata,
locktype
);

assert (SUCCEEDED (hr));
assert (SUCCEEDED (lockdata != NULL));

memcpy (lockdata, itemdata, numitems * this->ItemSize);

hr = this->Buffer->Unlock ();
assert (SUCCEEDED (hr));

// you may wish to draw here or you may not; up to you

this->CurrItem += numitems;
}

void Draw (void)
{
// fill this in yourself
}

private:
IDirect3DDevice9 *Device;
IDirect3DVertexBuffer9 *Buffer;

int CurrItem;
int MaxItems;
int ItemSize;
};


Edited by mhagain, 18 January 2013 - 06:50 PM.

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.

Posted 18 January 2013 - 06:25 PM

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

What frame rate are you getting in each case? Drawing extra stuff is never going to be free.

How many particles get rendered? With how many draw calls? How many pixels does each particle cover on average?

How long is the CPU taking to update all those particles and copy them into the vertex buffer?

Is it any faster if you use a managed vertex buffer instead of a dynamic one? (normally this is a bad plan, but in some cases it is quicker)

### #32Medo3337  Members

Posted 18 January 2013 - 07:10 PM

@mhagain: The code is hard to read because it's all in one line, you should update the post.

What frame rate are you getting in each case? Drawing extra stuff is never going to be free.

I'm not creating alot of particles, I don't have performance problems with rain/snow particles, however I get that problem when I create smoke.

Even one single smoke emitter slow down rendering, normally I have 61 FPS, 1 smoke emitter = 59-61 FPS, more smoke emitters? LESS FPS

The weird thing is that I'm not creating alot of particles, just few and it's dramatically decreasing the number of frames per second.

How many particles get rendered? With how many draw calls? How many pixels does each particle cover on average?

Not alot, just few for smoke effect, one draw call per emitter, so If the player is shooting and got 10 hits, that will create 10 emitters for smoke.

Is it any faster if you use a managed vertex buffer instead of a dynamic one? (normally this is a bad plan, but in some cases it is quicker)

I tried changing D3DPOOL_DEFAULT to D3DPOOL_MANAGED, still having the same problem.

### #33mhagain  Members

Posted 18 January 2013 - 07:52 PM

I've updated the post; forum bugs bit me.

As a general description though - you create a dynamic buffer once only at start up time (and handle it appropriately during lost device scenarios); make it big enough for what you reasonably expect, a few MB will do.  Set a "first particle" counter to 0 for it, then each frame when particles come in, see if there's room in the buffer for them.  If so, lock it with no-overwrite, if not, flush anything that may be in it (you may not need that part), lock with discard, reset your "first particle" to 0.  memcpy in your particles and unlock.  Draw, add number of particles to "first particle" counter, repeat next frame.

This way your driver will automatically handle GPU memory allocations for you, and you don't need to worry about how many particles you have, whether the number changes every frame, etc.  You've one buffer and it's only created once, at a time that's not performance-critical, and everything runs fast.

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.

### #34phil_t  Members

Posted 18 January 2013 - 07:55 PM

Hmm... 0 smoke emitters you get 61 FPS, and 1 smoke emitter you get 59-61 FPS (essentially no different). And more smoke emitters you get... "less". That's pretty vague, it's still not clear you have a performance problem. How much less?

One easy test you can do: change your smoke emitter so that the smoke particles are extremly small (like just one pixel). Do you still experience similar slowdowns? If not, then your limited by fill-rate, and you need to either draw less particles or make them smaller.

### #35Medo3337  Members

Posted 18 January 2013 - 08:06 PM

Okay guys, I notice a good improvement now.

The particles are now not slowing down rendering from the beginning, but it does by time, I mean in the beginning it works perfectly, when I repeat the following many times I start notice slow rendering:

- Create smoke emitter

- Update smoke particles

- Remove smoke emitter

So the player can keep shooting and seeing smoke particles for like 30 seconds without problems, later it will start to slow down FPS.

### #36L. Spiro  Members

Posted 18 January 2013 - 08:28 PM

You have a memory/resource leak.

So the next step is to figure out what kind of leak it is.

Use the DirectX debug run-time to check for leaks related to DirectX.

L. Spiro

### #37Medo3337  Members

Posted 19 January 2013 - 12:30 AM

@L. Spiro:

I'm not sure how exactly I can detect where the memory leak happen.

Here is how I create the emitter:

vector<EMITTER*> emitterList;

// CREATE EMITTER
EMITTER *emitter = new EMITTER(d3ddev);
emitter->value1 = ...;
emitter->value2 = ...;
emitter->value3 = ...;
// etc...

emiterList.push_back(emitter);

// RELEASE EMITTER
EMITTER::~EMITTER()
{
particles.clear();
for (int i = 0; i < maxVBuffer; i++)
{
vbuffer[i]->Release();
}
}

Let me know if the above code doesn't look right.

### #38L. Spiro  Members

Posted 19 January 2013 - 02:47 AM

When do you actually delete the emitters?

Releasing them is a concept familiar to DirectX.  You are not DirectX.  You should just be deleting them.  If Release() has to be called before deleting them, call it within the destructor of the emitter.

L. Spiro

### #39Medo3337  Members

Posted 19 January 2013 - 03:06 AM

I delete the emitter when there is no more particles to render, I posted the emitter class destructor above.

vbuffer[i]->Release(); Should release the vertex buffer LPDIRECT3DVERTEXBUFFER9.

### #40Medo3337  Members

Posted 19 January 2013 - 03:18 AM

Finally, I found out that the slow rendering was from the billboards and not the particles!

The slow rendering problem is now resolved.

After upgrading the Particle Engine to make it one draw per emitter instead of one draw per particle I notice a slight problem.

When I create a smoke environment (Blizzard smoke), I feel like it's flashing, this problem didn't happen before (when I was drawing once per particle).

Any idea why the undesired particles flashing occurs?

BTW, before I used to set the texture alpha blending for the particles, now I'm setting the alpha blending using vertex color.

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.