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

Author Topic: FPS Peaks with VSync-enabled (59Hz refresh rate)  (Read 5372 times)

0 Members and 1 Guest are viewing this topic.

hiram

  • Newbie
  • *
  • Posts: 7
    • View Profile
FPS Peaks with VSync-enabled (59Hz refresh rate)
« on: August 29, 2014, 01:12:46 pm »
Hello,

I'm observing the FPS peaks in a program with V-Sync Enabled on a monitor with 59Hz refresh rate. Framerate limit is disabled, as expected. FYI, it's on a Mac OS X 10.10 (haven't tested the same code in other OSes), I'm using latest SFML (master) from Git (compiled as Release).

As far as I know, V-Sync tends to keep framerate (FPS) similar to the monitor refresh rate. The magic happens on GPU. However, I'm observing these peaks and it's causing sprites movements to"lag", once they are dependent on deltaTime (time per frame) and a big variation will affect the movement.

I've attached a chart with my measurements. The peaks happens intermitently. They are not caused by something I do on the main loop.

Is that a normal behavior with V-Sync enabled?

Note: I was expecting the FPS to be nearly constant. Also, I know I could use setFramerateLimit, but I'd rather prefer to trust on the underlying hardware mechanisms to achive the desired FPS.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #1 on: August 29, 2014, 01:20:51 pm »
SFML really can't do much if the GPU feels like letting the FPS go through the roof  :P

As for your sprites being affected by too large of delta time, you should also implement a fixed time step to ensure consistent movement/physics across machines.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

hiram

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #2 on: August 29, 2014, 01:26:14 pm »
Yeah, I understand. But do you observe this behavior in your OS & GPU? It might be something related to the driver, but I don't see those peaks happening on other games with V-Sync enabled. I have no idea.

hiram

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #3 on: August 29, 2014, 01:41:58 pm »
BTW, a great tutorial on fixed time step.

http://gafferongames.com/game-physics/fix-your-timestep/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #4 on: August 29, 2014, 08:00:56 pm »
http://gafferongames.com/game-physics/fix-your-timestep/
This is almost always the link that I would recommend for timestep information.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

hiram

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #5 on: August 30, 2014, 10:22:44 pm »
totally agreed, Hapax.

Anyway, I'm new to game dev, but one of the main reasons to use fixed timestep is because of that FPS variation that everyone have to face, right?

As I am still learning, I don't usually know what is my fault and what is just how things works :-)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #6 on: August 30, 2014, 10:47:06 pm »
one of the main reasons to use fixed timestep is because of that FPS variation that everyone have to face, right?

Very roughly speaking, yes.  But it's worth explaining a bit more.

You can deal with FPS variation simply by giving your game loop a timer, and every frame you check how much time has passed (the so-called "deltaTime" value), and you pass that information to every component of your game logic.  At this point FPS variation is no longer a direct issue.

The indirect issue comes from the fact that your physics engine has to deal with deltaTimes anywhere from one millisecond to ten seconds.  The problem is that computing one thousand "physics steps" for one millisecond is not necessarily the same thing as computing a single "physics step" for one second.  If an enemy projectile passes through you in a fraction of a second, then they're completely different, and your game is broken.  This is the problem that a fixed timestep solves, by letting the deltaTime pile up and then feeding it to the physics engine (and AI and other stuff) in fixed-size chunks.  Note that rendering and possibly some cosmetic animations are still done once every frame; it's only game logic that needs to pretend time is discrete to work properly.

Quote
Is that a normal behavior with V-Sync enabled?

It can be.  Everything time-related is going to have some inherent imprecision, but vsync is different from simply sleeping for 1/xth of a second (which is what setFramerateLimit does).  Specifically, vsync is designed to prevent screen tearing by forcing your program to pause until the monitor is ready for a new frame.  Even if the monitor refreshes 60 times a second, waiting for the monitor to be ready is different from simply waiting 1/60th of a second.  Your program and the monitor are actually being kept in sync, which sometimes means your program has to wait longer than usual.  There's a reason most games let the user configure this stuff; the tradeoff between possible screen tearing and possible input lag is not trivial.

If you're interested in more detail, look up double buffering and triple buffering.



Regarding your chart and lag problem...I'm not really sure what to say, since an FPS spike shouldn't mean your program lags.  If anything vsync means your program should stop drawing frames until the monitor is ready for a new one, not draw thousands of extra frames for the monitor to ignore.  Admittedly, an FPS of several thousand is usually misleading, since that just means one frame got drawn much faster than usual, not that you actually drew 10000 frames in one second.  It's probably more meaningful to look at milliseconds per frame than frames per second.  Plus, you didn't tell us how you're measuring this, so for all we know maybe the counter you're using gets easily confused by vsync.

How does it look when you try setFramerateLimit(59) instead of vsync?
« Last Edit: August 30, 2014, 11:00:20 pm by Ixrec »

hiram

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #7 on: August 31, 2014, 11:20:26 am »
Hi Ixrec,

First of all, thanks for dedicating your precious time writing a so complete answer.

I'm measuring FPS this way:

sf::ContextSettings settings;
settings.antialiasingLevel = 16;
sf::RenderWindow window(sf::VideoMode(1280, 720), "Test SFML", sf::Style::Default, settings);
window.setKeyRepeatEnabled(false);
window.setMouseCursorVisible(false);
window.setVerticalSyncEnabled(true);
// window.setFramerateLimit(59);

Game game;
sf::Clock clock;
float dt;
game.init();
while (window.isOpen()) {
    dt = clock.restart().asSeconds();
    window.clear();
    game.update(dt);
    game.render(window);
    window.display();
}

About setting Framerate limit to 59, it works perfectly. FPS keeps nearly constant (small variation). The problem with the V-Sync is that the deltaTime just goes from 0.0016 to 0.00001 (random values), and when it comes back to 0.0016 the animation is affected, once it depends on deltaTime.

Am I measuring time correctly?

I'm on iMac. Its hardware is very good. The GPU is AMD Radeon HD 6750M 512 MB. That's why I'm surprised to see the framerate spikes happening on rendering frames with just a green sf::Circle and a sf::Sprite moving.

hiram

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #8 on: August 31, 2014, 11:22:09 am »
FYI: I calculate the FPS by 1 / dt, once I want to know how many "dt" fits in one second.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #9 on: August 31, 2014, 11:59:01 am »
Am I measuring time correctly?
Yes, assuming the values of dt are what you output to generate your graphs.

Quote
I'm on iMac. Its hardware is very good. The GPU is AMD Radeon HD 6750M 512 MB. That's why I'm surprised to see the framerate spikes happening on rendering frames with just a green sf::Circle and a sf::Sprite moving.

I'm inclined to simply assume that this is the inherent downside of vsync making itself abnormally obvious (namely, that sometimes your program just has to stop and wait for the monitor in order to stay in sync with it).  So it may be that enabling vsync just isn't a very good idea on your particular machine.  A quick google turns up threads like http://us.battle.net/wow/en/forum/topic/2549108227 and http://forums.steampowered.com/forums/showthread.php?t=1268239 which sound vaguely similar.  Though don't take this to mean "Macs suck at vsync."  Issues like this might depend on any number of details about your individual machine, so it's very likely that other Macs behave differently (unless that second thread is right about certain games only supporting triple buffering on Windows...but it's probably not that simple).  If your system supports triple buffering, I would try toggling that too since it should also have a noticeable effect on this behavior.

tl;dr This is why most games let the user turn vsync on or off.  You just can't know ahead of time if it'll be a net positive for everyone.
« Last Edit: August 31, 2014, 12:03:22 pm by Ixrec »

hiram

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: FPS Peaks with VSync-enabled (59Hz refresh rate)
« Reply #10 on: August 31, 2014, 12:06:11 pm »
Perfect explanation, Ixrec. Thank you so much! Learnt a lot with you.