Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: FPS Drop  (Read 3844 times)

0 Members and 1 Guest are viewing this topic.

Macecraft

  • Guest
FPS Drop
« on: June 23, 2017, 02:31:12 am »
Hello,

so I seem to be experiencing a rather dramatic FPS drop based on the period of time my game has been running for.

(Useful Info):

Using: SFML 2.4.2 with C++ in Visual Studio 2015.
Graphics Card: nVidia 960M

I've stripped my game loop down to just doing a basic

        while (m_rWindow.isOpen())
        {
                Event evnt; //Make an sfml event
                while (m_rWindow.pollEvent(evnt))//Get all the events that have occurred
                {
                        if (evnt.type == Event::Closed)
                        {
                                m_rWindow.close();
                        }
                        if (evnt.type == Event::LostFocus)
                        {
                                hasFocus = false;
                        }
                        if (evnt.type == Event::GainedFocus)
                        {
                                hasFocus = true;
                        }
                        if (evnt.type == Event::KeyReleased)
                        {
                                if (evnt.key.code == Keyboard::Escape)
                                {
                                        m_rWindow.close();
                                }
                        }
                }
                ++frames;
                thisTime = elapsed.getElapsedTime();
                setFPSText(lastTime, thisTime, frames);
                       
                m_rWindow.clear();
                m_rWindow.draw(m_fpsText);
                m_rWindow.display();
}

 

All the loop does now is draw the fps to the screen and display this value. I disabled all rendering and updating of the game to find the cause of my present problem. Through some testing I managed to find:

When the game is initially run on my PC with just this loop (no VSync enabled, and no frame rate limit function call) I achieve 1.7 ms/frame (568 fps). But after a period of maybe 2-3 minutes I can end up at a value of 10 ms/frame (97 fps). This drastic change when I do have the fps capped to 60 and my game running takes me from an average of 16 ms/frame to 32 ms/frame.

There only a black screen with an FPS counter at the top right of the game window. I've profiled the executable a few times and timed the m_renderWindow.clear() to .display() and had variations from 11ms to 110ms when rendering my own game, but then had 6ms - 90ms when only rendering text (with a 60 fps cap on).

(Sorry for the information overload!)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: FPS Drop
« Reply #1 on: June 23, 2017, 06:25:41 am »
Sounds more like an accumulation of calculating the FPS wrong. Can you show your FPS calculation?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Macecraft

  • Guest
Re: FPS Drop
« Reply #2 on: June 23, 2017, 02:45:20 pm »
Yeah sure, here's my fps calculation:


void Game::setFPSText(Time& lastTime, const Time& currentTime, Int32& frames)
{
        const float TIME_GAP = 0.1f;
        if ((currentTime - lastTime).asSeconds() >= TIME_GAP)
        {
                float msPerFrame = 1000.0f / float(frames);

                m_fpsText.setString(std::to_string(msPerFrame) + " ms/frame\n " + "FPS: " +
                                                      std::to_string((Int32)(1.0f / (msPerFrame / 1000.0f))));
                frames = 0;
                lastTime += Time(seconds(1.0));
        }
}

 

The thing is the game does begin to run slower, I can see things jittering on the screen
« Last Edit: June 23, 2017, 02:49:26 pm by Macecraft »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: FPS Drop
« Reply #3 on: June 23, 2017, 03:53:08 pm »
lastTime += Time(seconds(1.0));
Should be
lastTime = currentTime;
Because the difference will never be exactly 1.0.
Laurent Gomila - SFML developer

Macecraft

  • Guest
Re: FPS Drop
« Reply #4 on: June 23, 2017, 04:33:01 pm »
Using that I still get the same FPS drop during runtime. I'm going to try just running a window not initialising my own game and just run a renderwindow with some text displaying ms/frame and FPS

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: FPS Drop
« Reply #5 on: June 23, 2017, 05:20:48 pm »
Btw, you can always just determine a simple FPS by restarting a clock and taking the inverse.

auto dt = clock.restart();
auto fps = 1.f / dt.asSeconds();

So you can at least use this to verify your implementation.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Macecraft

  • Guest
Re: FPS Drop
« Reply #6 on: June 23, 2017, 05:27:33 pm »
Btw, you can always just determine a simple FPS by restarting a clock and taking the inverse.

auto dt = clock.restart();
auto fps = 1.f / dt.asSeconds();

So you can at least use this to verify your implementation.
Thanks for the help, I'll put that in

Sent from my SM-G920F using Tapatalk


Macecraft

  • Guest
Re: FPS Drop
« Reply #7 on: June 24, 2017, 09:19:43 pm »
So I'm still not sure, but I verified the issue only occurs when I initialise my game


I used VS' profiler to profile the game in release mode, and that erratic CPU usage section (that's highlighted in yellow) is what is the filter for the results. I do see huge spikes when I time around my clear() & display () blocks. I find spikes of 90ms+, but it's only rendering around 20 sprites, most of which share a texture. There's nothing new being added. Below is the data from the profiler:

(click to show/hide)
(click to show/hide)
(click to show/hide)

-- Update --

So I've profiled down and during the games execution inside a loop which draws all game objects (shown below) seems to spike. The object count remains constant through execution (I checked for this initially).
for (const auto& gameObj : m_allGameObjects)
{
        if (!gameObj->isEnabled())
        {
                continue;
        }
        m_rWindow->draw(*gameObj);
}
 
When getting the game to output a value every time this loops execution is above 20ms I get log outputs such as:

(click to show/hide)
« Last Edit: June 24, 2017, 10:28:12 pm by Macecraft »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: FPS Drop
« Reply #8 on: June 24, 2017, 10:23:31 pm »
Not sure how went from wrongful FPS calculation to FPS spikes, but there has been an issue with the joystick bit, which was fixed in SFML master.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Macecraft

  • Guest
Re: FPS Drop
« Reply #9 on: June 24, 2017, 10:26:27 pm »
Not sure how went from wrongful FPS calculation to FPS spikes, but there has been an issue with the joystick bit, which was fixed in SFML master.

My initial post was about this FPS drop, and it is still the main issue that I'm experiencing. Would I need to compile the master branch myself to see if this fixes my issue?

Macecraft

  • Guest
Re: FPS Drop
« Reply #10 on: June 29, 2017, 03:43:58 am »
So whilst debugging I found the element that seems to hit the performance of the game, and it points to renderwindow's .clear(), but only after a period of time has elapsed do I get this issue.