Sign in to follow this  
Medo Mex

Particles Engine and Slow FPS

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

        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;
            locktype = D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK;
        }

        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

        // advance the pointer
        this->CurrItem += numitems;
    }

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

private:
    IDirect3DDevice9 *Device;
    IDirect3DVertexBuffer9 *Buffer;

    int CurrItem;
    int MaxItems;
    int ItemSize;
};
Edited by mhagain

Share this post


Link to post
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.

 

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)

Share this post


Link to post
Share on other sites

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

 

@Adam_42:

 

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

That’s super.  Now show me where you delete the emitter buffers.

You called “EMITTER *emitter = new EMITTER(d3ddev);”, now show the corresponding call to “delete”.

 

You have probably solved this problem, but based on what you know about new and delete I don’t think this battle is over.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

In EMITTER destructor:

 

EMITTER::~EMITTER()
{
    for (int i = 0; i < maxVBuffer; i++)
    {
         // Release vertex buffer
         vbuffer[i]->Release(); // or delete v_buffer[i]
    }
}
I iterating through the emitters and check who is not alive anymore and then I remove it using:
EMITTER *emitter = *iter;
emitterList.erase(iter);
delete emitter;

But I think this has nothing to do with flashing smoke environment, I mean the flashing happened when I switched from (draw per particle) to (draw per emitter) and it only happen with the smoke environment particles, other types of particles works well.

 

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