SFML community forums

Help => Window => Topic started by: espectra on October 06, 2015, 04:18:30 pm

Title: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: espectra on October 06, 2015, 04:18:30 pm
I'm going to mark this as solved since I already have a workaround. Although at the bottom of this post, I have a possible feature request. But first, here's the problem I encountered...

This is on Windows Vista with SFML-2.3.1.

There seems to be lag or "hiccups" in a smooth framerate when calling the window's pollEvent() without a Joystick/controller plugged in. I especially noticed this since I limit the framerate in my prototype game to 60fps - every second or so, there was a glitch or hiccup and by recording and printing out the frame periods I was able to verify this with most of the times being around 15 to 17 milliseconds but every so often a 30 or 40 ms period showing up.

Just through trial and error I noticed that this DOESN'T happen when I have an Xbox360 clone USB controller plugged in. Another way to "fix" the glitches was to remove the pollEvent() loop.

Anyway, after searching the forum a bit and consulting with a twitter pal, I found this thread:

http://en.sfml-dev.org/forums/index.php?topic=6079.0 (http://en.sfml-dev.org/forums/index.php?topic=6079.0)

That's obviously an old thread, but I guess some of it still applies.

TEST PROGRAM

//
// Simple example to demonstrate lag/hiccups in SFML when framerate is set to 60fps
// and window.pollEvent() is called.
//
// On my machine (Windows Vista 64bit) compiling with mingw  4.8.1 & SFML 2.3.1
// the lag or "hiccups" go away when I plug in a controller/joystick. Unplugging the
// controller again, the "hiccups" stay gone.
//
// Also commenting out the pollEvent() call "fixes" the problem. So maybe it's an
// interaction with a framerate limited to 60fps and polling for events when no
// controller is plugged in. Could just be my machine, too.
//   --> Core2 Duo T6500 CPU at 2.1 GHz
//       with Intel Graphics (but decent-ish "Entertainment" Notebook PC GPU)
//       surely I should be able to draw one quad at 60fps without lag.
//       Seems more like a bug somewhere with polling for controller Events???
//
// The hiccups can be seen in the flickering of the quad (that represents the frametime).
// This rectangle should be about 2*16 or 2*17 pixels long, where the 16 or 17 represent
// milliseconds between frames in the test program. However, the drawing is occasionally
// missed to to a lag introduced that takes the frametime up to around 30ms.
//
// Frame times can be verified by tapping the Return/Enter key to dump the last 100 frame
// times and whereas most are ~16ms, the occasional ~30ms time can be seen in the dump.
//

#include <SFML/Graphics.hpp>

#include <iostream>

int main()
{
  int Window_Width;
  int Window_Height;

  Window_Width = sf::VideoMode::getDesktopMode().width;
  Window_Height = sf::VideoMode::getDesktopMode().height;

  // Create a fullscreen window
  sf::RenderWindow renderWindow(sf::VideoMode(Window_Width, Window_Height),"FrameRate Test", sf::Style::None);

  renderWindow.setVerticalSyncEnabled(true);
  renderWindow.setFramerateLimit(60);

  // clock to measure time
  sf::Clock clock;
  sf::Int32 lastTime = 0;
  sf::Int32 thisTime = 0;
  sf::Int32 deltaTime;

  // check updates in ms
  int numChecks = 0;
  int i = 0;
  sf::Int32 check[100];

  // prime the timer/clock
  lastTime = clock.getElapsedTime().asMilliseconds();

  // draw a frame time quad
  sf::VertexArray ftquad(sf::Quads, 4);
  ftquad[0].position = sf::Vector2f(100,50);
  ftquad[1].position = sf::Vector2f(200,50);
  ftquad[2].position = sf::Vector2f(200,100);
  ftquad[3].position = sf::Vector2f(100,100);

  ftquad[0].color = sf::Color::Red;
  ftquad[1].color = sf::Color::Red;
  ftquad[2].color = sf::Color::Red;
  ftquad[3].color = sf::Color::Red;

  bool frametimes_dumped = false;

  // ********************************************************************************************************************
  // Start the game loop
  // ********************************************************************************************************************
  while (renderWindow.isOpen())
  {
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
          renderWindow.close();
    }

    // commenting out the pollEvent() call "fixes" the problem with lag/hiccups
    ///*
    sf::Event event;
    while (renderWindow.pollEvent(event)) {
        // DO NOTHING
    }
    //*/

    // -----------------------------------------------------------------
    // UPDATING
    // -----------------------------------------------------------------
    thisTime = clock.getElapsedTime().asMilliseconds();
    deltaTime = thisTime - lastTime;

    // record "frame time" from last frame to now
    check[numChecks] = deltaTime;
    numChecks++;
    if (numChecks >= 100) numChecks = 0;

    lastTime = thisTime;

    // draw a rectangle with length of twice the frame time
    ftquad[0].position = sf::Vector2f(100, 50);
    ftquad[1].position = sf::Vector2f(100 + deltaTime*2, 50);
    ftquad[2].position = sf::Vector2f(100 + deltaTime*2, 100);
    ftquad[3].position = sf::Vector2f(100, 100);

    // dump past 100 frame times when Return key is hit
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return) && !frametimes_dumped) {
        frametimes_dumped = true;
        for (i=numChecks; i < 100; i++) // dump older recorded frame times
                std::cout << "check" << std::dec << i << " : " << check[i] << "ms\n" ;
        for (i=0; i < numChecks; i++) // dump most recent recorded frame times
                std::cout << "check" << std::dec << i << " : " << check[i] << "ms\n" ;
    }

    // -----------------------------------------------------------------
    // DRAWING
    // -----------------------------------------------------------------
    renderWindow.clear(sf::Color::Black);
    renderWindow.draw(ftquad);
    renderWindow.display();

  }

  return EXIT_SUCCESS;
}

 

On my PC at least (and perhaps others), if you run the program without a controller plugged in, you'll see a red bar that shows the frame period, but occasionally it'll miss a frame and will just draw black. Commenting out the pollEvent() call/loop or plugging in a Controller alleviates the problem and it draws every frame.

It's possible you won't see this on a higher powered CPU, but I'm guessing it'll show up even there if you limit your framerate to 60fps and add the sort of complexity & number of entities you're drawing in an actual game.

WORKAROUND

I hacked together a somewhat similar workaround as the one mentioned in the older forum thread, but in this case, by adding a bool member to Win32/JoystickImpl.hpp

 
  // hacky new methods
  bool isfixConnected();
  void setfixConnected(bool flag);

  // hacky new private member
  bool m_fix_connected;

 

and Win32/JoystickImpl.cpp

  bool JoystickImpl::isfixConnected() {return m_fix_connected;}
  void JoystickImpl::setfixConnected(bool flag) {m_fix_connected = flag;}
 

and then changing Window/JoystickManager.cpp JoystickManager::update() to this

////////////////////////////////////////////////////////////
void JoystickManager::update()
{
    for (int i = 0; i < Joystick::Count; ++i)
    {
        Item& item = m_joysticks[i];

        if (item.state.connected)
        {
            // Get the current state of the joystick
            item.state = item.joystick.update();

            // Check if it's still connected
            if (!item.state.connected)
            {
                item.joystick.close();
                item.capabilities   = JoystickCaps();
                item.state          = JoystickState();
                item.identification = Joystick::Identification();
            }
        }
        else
        {
            // Check if the joystick was connected since last update
            // Hack - fix glitch when joystick is not connected
          if (!item.joystick.isfixConnected()) {
            // HACK - have it only check once...
            item.joystick.setfixConnected(true);

            if (JoystickImpl::isConnected(i))
            {
                if (item.joystick.open(i))
                {
                    item.capabilities   = item.joystick.getCapabilities();
                    item.state          = item.joystick.update();
                    item.identification = item.joystick.getIdentification();
                }
            }
          }
        }
    }
}

 

So it will only check once (when the game launches) whether a controller is plugged in. This fixes the "hiccups" in the frame rate.

FEATURE REQUEST?

I guess it might be a useful addition to add a flag to the Window & Joystick implementations to workaround this, so that the default is to check every time JoystickManager::update() is called, but allow the game dev to set a flag after creating the window so that it only checks once when the game starts.

That is until a definite solution is found.

Just putting this out there in case someone else runs into the same problem.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 07, 2015, 10:52:33 am
I think it's probably because SFML cause a bunch of registry access operations periodically. All these operations is related to the joystick. So it may cause high lags...
There is need for option to disable joystick update. For example I don't use joysticks at all, but it affects my app, I see for about hundreds of registry operations per second with sysinternals Process Monitor

Also I have suggestion to replace setframelimit(60) with SetFrameLimit(0). On my system it allows to decrease CPU load for about 4000% ( from 50% load to 1% load on 4 core processor). Also it makes more smooth and stable frame rate
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: Nexus on October 07, 2015, 01:56:18 pm
Also I have suggestion to replace setframelimit(60) with SetFrameLimit(0). On my system it allows to decrease CPU load for about 4000% ( from 50% load to 1% load on 4 core processor).
setFramerateLimit(0) disables the frame rate limit, meaning that the game loop is cycled as frequently as possible.

Are you sure it reduces the processor load? Unless you have VSync or some other kind of waiting/sleeping enabled, that doesn't make sense.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 07, 2015, 05:42:09 pm
Also I have suggestion to replace setframelimit(60) with SetFrameLimit(0). On my system it allows to decrease CPU load for about 4000% ( from 50% load to 1% load on 4 core processor).
setFramerateLimit(0) disables the frame rate limit, meaning that the game loop is cycled as frequently as possible.

Are you sure it reduces the processor load? Unless you have VSync or some other kind of waiting/sleeping enabled, that doesn't make sense.

Yes, of cause I'm using VBlank sync to limit frame smoothly. It's the single way which allows to limit render rate with smooth timeline (with no lags). So, when I want to measure renderer performance I disable vblanksync in game settings. When I need maximum smooth and to eliminate screen tearing effect I enable vblanksync.
It's easy, high quality way with minimum cpu load and it used in all commercial games.

I have no idea what is doing SetFrameLimit, probably it was designed to skip some frames to make frame rate slower. But if you skipping frames, it means that you will trap in problems with smoothness. You will get jerky movements, you can reduce that effect if you are using time based movements and video resampling filter (which interpolates colors with complicated and heavy filtering detects object motions on the image, etc) which is too heavy task for todays videocard shader processing, because it requires a lot of calculations and to using complicated algorithms. But anyway you cannot eliminate such effect. And your video will be always much worse than with no frame limiting.

If you don't need for smoothness movements in your app (if you are using static image with no movements) you can use frame limiting, but I have no idea for what :)
Because you will get better results with vblank sync even for static image.

And yes, I have extremely high CPU load with using SetFrameLimit. It even affects entire system, I get high lags in other applications. For example, my renderer for the game with no player entities consumes for about 0,5% of CPU time. If I set SetFrameLimit(60), cpu load jumps to 50-60% (two core of 4 was totaly burned to execute some useless idle loop).

I can say more you cannot eliminate jerky movement effect which appears when you skipping frames even if you will use pixel color interpolation between near frames and other filtering algorithms. It's physically impossible. For better filtering result, your display should have refresh rate at least 100 times faster than your target value. It means that if you want to get good quality (NOT high) with SetFrameLimit(60), your display should works with frequency 6000 frames per second. And your videocard should be able to apply complicated filtering for each frame. It just impossible with today's technology.

The best approach to visualize such effects is to use slow text movement (one pixel per frame), also it affects fast color changing (each frame has new color of pixel). With skipping frame your image will looks very different. You will find that the text is not moving smoothly, its speed will always jump. And you will be surprised that the colors also will be so different from vblank sync synchronization ;)
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: Nexus on October 07, 2015, 07:17:09 pm
Yes, of cause I'm using VBlank sync to limit frame smoothly.
Okay, you should have mentioned that. VSync and a framerate limit are not intended to work together, so don't be surprised about high CPU loads. It's either or.

I have no idea what is doing SetFrameLimit, probably it was designed to skip some frames to make frame rate slower.
Instead of guessing wildly, you could read the documentation (http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Window.php#af4322d315baf93405bf0d5087ad5e784) for example.

It doesn't skip frames. With that premise being wrong, your conclusion is invalid, too ;)

setFramerateLimit() isn't meant to be the perfect choice for every application, its inaccuracy is mentioned in the documentation as well. But it's simple to set up and useful in cases where vertical synchronization can't be used.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: Jesper Juhl on October 07, 2015, 07:23:52 pm
Remember that even with vsync (when it's even available, it isn't always) you are not guaranteed a completely stable framerate. You always have to take into account that time-per-frame can vary and decouple your game logic updates from your rendering. And once you do that, setting a framerate limit is usually just as fine as using vsync (unless you have tearing problems specifically).
Just don't mix the two.

This classic article also seems to be relevant, so I'll link it: http://gafferongames.com/game-physics/fix-your-timestep/
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: Jesper Juhl on October 07, 2015, 07:31:09 pm
I think it's probably because SFML cause a bunch of registry access operations periodically. All these operations is related to the joystick. So it may cause high lags...
Don't just think/guess that it is so. Measure. Use a profiler.
Personally I doubt these registry accesses take any significant time at all, but do test that hypotheses.

There is need for option to disable joystick update.
I don't think you've provided sufficient evidence to back up that claim. How much time does your profiler tell you is spent doing those updates? Is is a significant amount of the programs total CPU time?
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 07, 2015, 10:03:43 pm
Remember that even with vsync (when it's even available, it isn't always) you are not guaranteed a completely stable framerate. You always have to take into account that time-per-frame can vary and decouple your game logic updates from your rendering. And once you do that, setting a framerate limit is usually just as fine as using vsync (unless you have tearing problems specifically).
Just don't mix the two.

This classic article also seems to be relevant, so I'll link it: http://gafferongames.com/game-physics/fix-your-timestep/

I don't need to implement it because all physics and movements in my game is time based and not depends on frame rate. It works with the same speed for 1250 fps and for 75 fps  ;)
But vsync looks definitely better because it absolutely smooth.

I tested and it seems that you are right, with disabled vblank sync SetFrameLimit works at least not so bad, with no high cpu load. It's not so stable like vblank sync, but it works not so bad.

Here is my results for LCD display with physical refresh rate 75 Hz:
Full graph width = last 200 frames
Avg = moving average
Time measurement error = ±0,00000037 [sec]  (timer frequency 2742939 [Hz])

SetFrameLimit(75);
SetVerticalSyncEnabled(false);
(http://savepic.su/6221852.png)


SetFrameLimit(0);
SetVerticalSyncEnabled(true);
(http://savepic.su/6214684.png)
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 07, 2015, 10:26:51 pm
There is need for option to disable joystick update.
I don't think you've provided sufficient evidence to back up that claim. How much time does your profiler tell you is spent doing those updates? Is is a significant amount of the programs total CPU time?

You can just open process monitor and look what really happens, here is evidence (just 10-20 sec of running SFML app):
(http://savepic.su/6193183.png)
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: Nexus on October 07, 2015, 10:42:02 pm
I don't need to implement it because all physics and movements in my game is time based and not depends on frame rate.  It works with the same speed for 1250 fps and for 75 fps  ;)
Does that mean you run the physic updates with different delta-times?

If so, this will easily add non-deterministic behavior to your physics. For example, if the computer lags for a blink of an eye, objects may tunnel through others instead of colliding.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 07, 2015, 10:52:35 pm
I don't need to implement it because all physics and movements in my game is time based and not depends on frame rate.  It works with the same speed for 1250 fps and for 75 fps  ;)
Does that mean you run the physic updates with different delta-times?

If so, this will easily add non-deterministic behavior to your physics. For example, if the computer lags for a blink of an eye, objects may tunnel through others instead of colliding.

No, renderer uses just interpolated values from the target value which is calculated with fixed time step, so if there will be lag in update, all entities just will smoothly stop with a little of inertia added by filter (0,5-1 sec) :)

The lag value on the graph is related to network lag. I taken screenshot when it was disconnected, so it shows all zeros :)
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: espectra on October 07, 2015, 11:02:33 pm
Remember that even with vsync (when it's even available, it isn't always) you are not guaranteed a completely stable framerate. You always have to take into account that time-per-frame can vary and decouple your game logic updates from your rendering. And once you do that, setting a framerate limit is usually just as fine as using vsync (unless you have tearing problems specifically).
Just don't mix the two.


Hmm, this is interesting because I'm mixing the two. But you're right - the tutorial (http://www.sfml-dev.org/tutorials/2.0/window-window.php#controlling-the-framerate) says not to mix them. It's just that for my PC, mixing them doesn't make things worse. It makes them better.  :P

The problem is that on my notebook PC, using setVerticalSyncEnabled(true) doesn't limit the framerate to the refresh rate (60Hz).

Not sure if it's the correct place to check and set this, but in the "Intel Graphics and Media Control Panel" there IS an option under 3D relating to Vertical Sync. It's currently set to "Application Settings" which I assume means SFML would enable/disable it according to my setting with setVerticalSyncEnabled().

The other option is "On", but changing to that and clicking Apply still doesn't limit the framerate in my game.

That's why I'm currently using both setVerticalSyncEnabled(true) to prevent tearing AND setFramerateLimit(60) to keep from hogging the CPU.

Of course my PC may be quite different from what most of you guys are using, but a medium powered notebook PC is pretty useful in game dev since many of your players/customers will only have a medium powered PC.

Anyway, is this a known issue/situation where some notebook PCs don't allow vsync (at least with SFML)?

EDIT: seems some notebook OpenGL drivers on Windows might be ignoring the vsync flag...

http://en.sfml-dev.org/forums/index.php?topic=10429.msg71870#msg71870 (http://en.sfml-dev.org/forums/index.php?topic=10429.msg71870#msg71870)

EDIT2: a couple of other links about forcing vsync to be enabled on Windows

https://www.opengl.org/discussion_boards/showthread.php/146791-How-to-enable-force-VSYNC (https://www.opengl.org/discussion_boards/showthread.php/146791-How-to-enable-force-VSYNC)

http://stackoverflow.com/questions/589064/how-to-enable-vertical-sync-in-opengl (http://stackoverflow.com/questions/589064/how-to-enable-vertical-sync-in-opengl)

I'll have to see if I can figure that all out.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 11, 2015, 02:25:18 am
The problem is that on my notebook PC, using setVerticalSyncEnabled(true) doesn't limit the framerate to the refresh rate (60Hz).

It seems that it's because there is an issue with SFML. I have the same issue on Windows 10 machine.
With SFML setting VerticalBlankSync(true) just doesn't works on this machine.
But I can run my DirectX application on the same machine and it can use vblank sync with no problem (and I see that it really works on the timing realtime graph)...
But when I trying to enable it from SFML application, vblank sync just has no effect (it runs with 1000 fps).

May be it's related to specific driver or OpenGL...
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: espectra on October 11, 2015, 03:13:26 am
The problem is that on my notebook PC, using setVerticalSyncEnabled(true) doesn't limit the framerate to the refresh rate (60Hz).

It seems that it's because there is an issue with SFML. I have the same issue on Windows 10 machine.
With SFML setting VerticalBlankSync(true) just doesn't works on this machine.
But I can run my DirectX application on the same machine and it can use vblank sync with no problem (and I see that it really works on the timing realtime graph)...
But when I trying to enable it from SFML application, vblank sync just has no effect (it runs with 1000 fps).

May be it's related to specific driver or OpenGL...

I did an experiment yesterday with Slick2D/LWJGL which uses OpenGL and it had the same problem, so it may be an OpenGL driver problem, either just with my driver or possibly also the OpenGL drivers for other GPUs.

FWIW my notebook has a built-in Intel graphics accelerator, "Mobile Intel 4 Series Express" and the driver is the latest version but since it's an old PC (2009), the latest driver update is from 2013 and it only supports up to OpenGL 2.1.

For now in my prototype game I'm just using setFramerateLimit(). There's no tearing on the notebook, but I plan to check it on a friend's PC with an Nvidia GPU to see if I need to do something to prevent tearing.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 11, 2015, 04:11:31 pm
May be it's related with OpenGL version, here is my observations:

OpenGL 4.5: vblank sync works OK
OpenGL 2.1: vblank sync didn't works
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: Nexus on October 11, 2015, 04:51:22 pm
How did you switch between OpenGL versions? Different context settings in the application, with the same driver? In any case, this problem doesn't seem to be related to SFML...

And I don't know why you keep saying "vblank sync", it's "VSync" which means "vertical synchronization".
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 11, 2015, 05:02:32 pm
How did you switch between OpenGL versions? Different context settings in the application, with the same driver? In any case, this problem doesn't seem to be related to SFML...

And I don't know why you keep saying "vblank sync", it's "VSync" which means "vertical synchronization".

I'm checked it in virtual machine which uses OpenGL 2.1.
By the way, it seems that vblank sync has it's own specific in OpenGL.
Here is some info:
http://stackoverflow.com/questions/589064/how-to-enable-vertical-sync-in-opengl
https://www.opengl.org/wiki/Swap_Interval

So, it seems that vblank sync depends on OpenGL implementation.
I'm not sure how it's cross platform support is implemented in SFML.
Does SFML support vblank sync at all on Linux?

Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 11, 2015, 05:11:32 pm
And I don't know why you keep saying "vblank sync", it's "VSync" which means "vertical synchronization".

Because it's name taken from low level implementation. "vblank" means vertical blank - it's the flag from the videocard which means that the vertical blank signal is currently active. When this flag is active, it's the time after end scanning of previous frame and before begin scanning of the next frame. So, it's the time to change video memory with no tearing effect.
Actually vertical synchronization means waiting for vblank flag=1 (active).

vblank sync is just more clear term which describes what happens exactly - synchronizing with vblank flag from the videocard.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: espectra on October 12, 2015, 04:40:50 pm
May be it's related with OpenGL version, here is my observations:

OpenGL 4.5: vblank sync works OK
OpenGL 2.1: vblank sync didn't works

It's quite strange - the OpenGL driver on my PC only supports 2.1, but apparently for Windows, OpenGL uses a Windows-specific extension to set vsync: the wglSwapIntervalEXT extension. My driver DOES support that and indeed SFML gets the extension and uses it, by setting the swap interval to 1 when setVerticalSyncEnabled() is set to true.

I'm not yet sure if it's actually enabling vsync and preventing tearing, but it definitely seems to NOT be limiting the frame rate to the refresh rate of 60Hz. Same problem with LWJGL, so I doubt it's simply an SFML problem.

I don't know what's going on and googling info is pretty frustrating and not turning up anything useful.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 12, 2015, 05:44:27 pm
Here is some more details: http://www.d-silence.com/feature.php?id=255

Quote
"There is no Vsync in OpenGL as a command. Most apps use the GLFlush command, sometimes followed by a GLFinish command. The GLFlush command basically says "Ok, what ever commands you have in your buffer, send 'em to the rendering device now, and get it working." It doesn't care where the raster is in the drawing sync, it just goes out and does it. The GLFinish command will then make the app wait until the rendering device has completed all the commands it has been sent up til then. This gives you the fastest feedback, fairly obviously. Now, depending on whether you are double buffering your video displays (ie rendering to the back one while the front one is being displayed) you might want to use a swapbuffers command. This means that you can afford to slap out commands to the rendering device when ever you feel like it, since it's always going to be rendering to an unseen buffer. The SwapBuffers command does what it says, it swaps the buffers between the front and the back. When it actually does this, ie at Vsync or just randomly whenever it can depends on the card you are using. Sometimes you can set the 'wait for Vsync' in the properties dialog for your card, sometimes it has to be set via registry options. It's messy and highly card dependant. Obviously working in a window you don't get any kind of Vsyncing going on."

So, it seems like OpenGL issue, there is no direct way to control vblank sync in OpenGL. It depends on driver implementation...

If you need it, you can use Direct3D for vblank sync and sfml/opengl for rendering. But it's really ugly solution which mixes Direct3D and OpenGL :)

When you use SetVerticalSync in SFML it actually uses first available function from the following list:
- glXSwapIntervalEXT
- glXSwapIntervalMESA
- glXSwapIntervalSGI

If all these functions is not available it just make warning and ignore it.

Probably it will be better to rename SetVerticalSync to SetSwapInterval to avoid confusing. There is no way to control vblank sync with OpenGL.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: espectra on October 12, 2015, 06:20:21 pm
Probably it will be better to rename SetVerticalSync to SetSwapInterval to avoid confusing. There is no way to control vblank sync with OpenGL.

But you tested it on OpenGL 4.1 and reported that it worked, didn't you?

May be it's related with OpenGL version, here is my observations:

OpenGL 4.5: vblank sync works OK
OpenGL 2.1: vblank sync didn't works

This whole thing with vsync & OpenGL is very confusing.  :o
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: Jesper Juhl on October 12, 2015, 08:59:07 pm
So, it seems like OpenGL issue, there is no direct way to control vblank sync in OpenGL. It depends on driver implementation...

If you need it, you can use Direct3D for vblank sync and sfml/opengl for rendering. But it's really ugly solution which mixes Direct3D and OpenGL :)
Asking for "vsync" is a request to the driver. There is no guarantee it will (or can) be honored.
You need to ensure that a) your logic updates are decoupled from your rendering. b) your rendering is independent of your framerate. c) realize that you can't get "vsync" everywhere (does it even make sense for modern lcd panels rhat don't actually have a vertical retrace like old CRT monitors?).
Vsync makes sense if you have a tearing problem. Don't think of it as a "fix framerate limit" thing. If you need to fix (somewhat) your framerate, you need to implement sleeps that make you render at roughly the framerate you want (and handle the inacuracies). Don't rely on vsync for that.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: espectra on October 12, 2015, 09:53:03 pm
Asking for "vsync" is a request to the driver. There is no guarantee it will (or can) be honored.
You need to ensure that a) your logic updates are decoupled from your rendering. b) your rendering is independent of your framerate. c) realize that you can't get "vsync" everywhere (does it even make sense for modern lcd panels rhat don't actually have a vertical retrace like old CRT monitors?).
Vsync makes sense if you have a tearing problem. Don't think of it as a "fix framerate limit" thing. If you need to fix (somewhat) your framerate, you need to implement sleeps that make you render at roughly the framerate you want (and handle the inacuracies). Don't rely on vsync for that.

This is good info! Thanks.

About the "modern lcd" not having vsync, that's an interesting topic. Aren't most displays actually LED displays these days?

Anyway, regardless of whether vsync works on my notebook's built-in display, I've never really seen tearing on it.

However, when testing on a friend's desktop PC with external LED display, I've seen some obvious tearing happening with the same test program.

Since I kind of enjoy understanding the technical stuff like this, I'd like to learn what's going on between testing the program on my notebook and on a desktop. For instance, the Intel media control panel on my notebook has a Vertical Sync option which can be set to "Application Settings" or "On". I don't know if that applies to DirectX only or also OpenGL, since it basically hides some of that underlying detail from the user (older versions of the Intel Graphics drivers seemed to show OpenGL settings explicitly...)

I wonder if we could come up with a simple test case that includes the features you mentioned (decoupled logic and rendering, framerate independent rendering) to show how the setting of vsync (wglSwapIntervalEXT) affects everything?
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 12, 2015, 11:16:50 pm

But you tested it on OpenGL 4.1 and reported that it worked, didn't you?


it works because driver doing it with glXSwapIntervalEXT(1), but if you call it it doesn't means that you will have vblank sync, it depends on driver desire.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 12, 2015, 11:20:24 pm
Asking for "vsync" is a request to the driver. There is no guarantee it will (or can) be honored.

There is no way to ask OpenGL driver for vblank sync. When you call SetVerticalSync(true) it actually enable SwapInterval. Vblank may be used with SwapInterval and may not be used. It not depends on support vblank sync in videocard driver. It depends on OpenGL driver.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 12, 2015, 11:25:39 pm
Vsync makes sense if you have a tearing problem. Don't think of it as a "fix framerate limit" thing.

No. Vsync allows to get equal render intervals with minimum delay between render frame and swap frame. In that way it improves smoothness of movements.
Actually you can eliminate tearing effect with using buffered rendering, but if you want to make smooth movements with equals intervals it will be too hard without vblank sync.
SFML SetVerticalSync eliminates tearing effect, but it actually doesn't deal with vblank sync.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 12, 2015, 11:33:11 pm
does it even make sense for modern lcd panels rhat don't actually have a vertical retrace like old CRT monitors?

Yes, it has real sense for all modern LCD with no exception, because all they receive video signal in the same way as it works with CRT. They may use digital signals instead of analog and short vblank but it still exists  ;)
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: zsbzsb on October 12, 2015, 11:36:14 pm
...You don't need 4 posts in the matter of 15 minutes. You can post multiple replies in a single post.
Title: Re: [SOLVED] window.pollEvent() lagging when Joystick not plugged in
Post by: mkalex777 on October 13, 2015, 03:04:59 am
...You don't need 4 posts in the matter of 15 minutes. You can post multiple replies in a single post.

Sorry, other forums automatically merge posts with short interval, so I get used to it :)