• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Particles Engine and Slow FPS

42 posts in this topic

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

0

Share this post


Link to post
Share on other sites

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

0

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.

0

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
0

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)

0

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.

0

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.

0

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.

0

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.

0

Share this post


Link to post
Share on other sites

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

0

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.

0

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

0

Share this post


Link to post
Share on other sites

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.

0

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.

0

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

0

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.

 

0

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  
Followers 0