SFML community forums

Help => Graphics => Topic started by: Carlo Meroni on January 06, 2014, 07:55:18 pm

Title: OpenGL application lag
Post by: Carlo Meroni on January 06, 2014, 07:55:18 pm
Hi,

I have a problem with my OpenGL application because i get some lag.
I set the VSync to true and the lag is reduced a lot but it is still present.
I set the limit of the frames to 60 and i get 16-17 millisecond elapsed time every frame.
The CPU usage is low  (5-10%) and the RAM usage is about 20 Mb.
I tested the application on 3 different computers (All macintosh) and the problem persist.

Specs of computers:

Mackbook pro 13"
OS : Maverick
Graphic Card : GeeForce 320M

Mackbook air 11"
OS : Maverick
Graphic Card : Intel HD 5000

Hackintosh
OS : MAverick
Graphic Card : AMD Radeon HD 6500;

Title: Re: OpenGL application lag
Post by: BaneTrapper on January 06, 2014, 08:27:34 pm
Provide with minimal example code.

60 fps = 16.66... sec per frame so that and everything else seams good.
I do not see the issue you getting "lag" with openGL application, are you using network?
Title: Re: OpenGL application lag
Post by: Carlo Meroni on January 06, 2014, 08:46:49 pm


Here a minimal code with a rotating quad that is a little bit laggy:

    sf::RenderWindow window(sf::VideoMode(800, 600), "Virtual Shock Testing",sf::Style::Default,settings);
   
    glEnable( GL_BLEND );
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   
    window.setVerticalSyncEnabled(true);
    window.setFramerateLimit(60);
   
    //--- OPENGL INITIALIZE ---
    //Perspective
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho (0, 800, 600,0, 0, 1);
    glViewport(0,0,800,600);
    glMatrixMode (GL_MODELVIEW);
   
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
   
    sf::Time time;
    sf::Clock clock;
   
    SpriteBatch spritebatch;
    Sprite test;
   
    test.SetColor(Color(1,0,0,1));
    test.SetTexture(0);
    test.SetTextureSource(Rectf(0,0,1,1));
    float rotation = 0;
   
    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            // Escape pressed : exit
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
        }
       
        rotation+=0.0001*time.asMicroseconds()/1000.00;
       
        test.SetPosition(Rect(200,200,400,400),Vector2i(400,400),rotation);
       
        window.clear();
       
        spritebatch.Begin();
        spritebatch.Draw(test);
        spritebatch.End();
       
        window.display();
       
        time = clock.restart();
    }
 

I'm not using network.
Title: Re: OpenGL application lag
Post by: BaneTrapper on January 06, 2014, 09:00:35 pm
The code is not compilable without spriteBatch class.
Looking deeper i see sf::Sprite has a line using
"SetColor"
The current version of sfml is using
"sf::Sprite::setColor"
if you are using older version of sfml be sure to note that.
Title: Re: OpenGL application lag
Post by: wintertime on January 06, 2014, 09:58:16 pm
    window.setVerticalSyncEnabled(true);
    window.setFramerateLimit(60);
 
Using both is most likely wrong. Using two independent timers for waiting will cause jitter from lost frames.
Title: Re: OpenGL application lag
Post by: eXpl0it3r on January 06, 2014, 10:35:49 pm
Looking deeper i see sf::Sprite has a line using
"SetColor"
The current version of sfml is using
"sf::Sprite::setColor"
if you are using older version of sfml be sure to note that.
You looked wrong, it's the sprite batch class. He's using SFML 2.x. ;)
Also 60fps = 0.0166.. seconds = 16.66.. milliseconds

As wintertime said and as written in the tutorials don't mix the two options.

At best would be an recording of the issue, however the question is, if it's still visible on a recording.
Title: Re: OpenGL application lag
Post by: Carlo Meroni on January 09, 2014, 05:44:24 pm
This video shows my issue:
https://www.youtube.com/watch?v=Kvgz_hQCkZ4

It's not mine but happen exactly the same thing, whit VSYNC and on 3 different MAC computers as I wrote above.

In the code I posted you can not see the Spritebatch class and the Sprite class...
I simply put every Sprite in a Sprite array and i draw everything with spritebatch.end() with Vertex array.

Sorry if I made any mistakes in writing, I'm Italian.
Title: Re: OpenGL application lag
Post by: eXpl0it3r on January 09, 2014, 05:50:52 pm
This video shows my issue:
http://www.youtube.com/watch?v=Kvgz_hQCkZ4
Well, now I'm confused. The video shows an issue where you can see some "tiles" getting pulled apart (i.e. the line gets broken) and from the comment it was an issue with the Nvidia driver.
Do you get that as well and call it "lag" or how does it show your issue? ???
Title: Re: OpenGL application lag
Post by: Carlo Meroni on January 09, 2014, 06:03:37 pm
It happens something like that, all the objects that are moving, (like rotating or simply when moving the view using glulookat() ) they move like the speed is irregular, like there is a delay or something like that.
In the video the movement of the view is not smooth.


Title: Re: OpenGL application lag
Post by: eXpl0it3r on January 09, 2014, 06:16:39 pm
It happens something like that, all the objects that are moving, (like rotating or simply when moving the view using glulookat() ) they move like the speed is irregular, like there is a delay or something like that.
So if there was a line moving, it would get "cut" in segments?

In the video the movement of the view is not smooth.
His view doesn't move smoothly, because he's moving it with events. Notice the one small movement, then the pause and then the repeating movement + he might not have used the smartest movement code. Hard to say + I'm too lazy to dig out the old topic. I just know, that it worked flawless on my system:
http://www.youtube.com/watch?v=n5GipBgpgx4
Title: Re: OpenGL application lag
Post by: Carlo Meroni on January 09, 2014, 07:16:00 pm
I made a video:
Watch carefully at the logo that rotates and the view when is moving.

https://www.youtube.com/watch?v=Yn-kHExa5Ws
Title: Re: OpenGL application lag
Post by: AlexAUT on January 09, 2014, 07:49:54 pm
3Things I would check:



AlexAUT
Title: Re: OpenGL application lag
Post by: BaneTrapper on January 09, 2014, 08:06:01 pm
What is your os?
I can send you application for you to test i believe i had 32bit one somewhere.
Title: Re: OpenGL application lag
Post by: Carlo Meroni on January 09, 2014, 08:26:44 pm
I have a costant 60 FPS. The elapsed time for each frame look like that (in microsecond) :

17680
17675
17282
17679
16684
16999
17678
17390
.....

I checked and there are no rounding issues.
For the drivers settings... OS X Mavericks does not allow users to touch the graphics card driver.
I tried to use glflush () and glfinish () after every draw but there is no difference.

Maybe i'm drawing in a wrong way?

Title: Re: OpenGL application lag
Post by: eXpl0it3r on January 10, 2014, 09:31:08 am
If you have a consistent FPS rate, it's really odd that you get micro stuttering.

Could you check how it performance in fullscreen mode?
And what happens if you move the view only on integer basis (1, 2, 3, 4 instead of 1.1, 1.2, 1.3,...)?

Btw. looks awesome! :)
Title: Re: OpenGL application lag
Post by: Robbis_1 on January 10, 2014, 12:42:54 pm
I have the same issue and moving the view with integers made it slightly better, but it's still there. I'm moving my view by using sf::Keyboard::isKeyPressed every game loop.

Running it in fullscreen caused extreme screen tearing, so I switched the frame limit to vsync instead. There seems to be no sign of "screen lag" in fullscreen with vsync, even if you move the view with floats.
Title: Re: OpenGL application lag
Post by: eXpl0it3r on January 10, 2014, 01:18:01 pm
Running it in fullscreen caused extreme screen tearing, so I switched the frame limit to vsync instead. There seems to be no sign of "screen lag" in fullscreen with vsync, even if you move the view with floats.
This should be expected to some sort, since when you're in window mode, you'll need to share the screen space on the GPU with other applications, while in full screen mode, nobody is going to interfere with you, but with a modern card this shouldn't be such a problem.

Btw. do you notice such micro stuttering as well with other games in window mode that don't use SFML?
Title: Re: OpenGL application lag
Post by: Robbis_1 on January 10, 2014, 01:54:17 pm
I've never noticed this on anything else and I'm usually running games in windowed no border mode.
Title: Re: OpenGL application lag
Post by: Carlo Meroni on January 10, 2014, 08:54:25 pm
Hey guys! I have resolved the issue. SFMl was managing the frames 2 times because i was calling window.setVerticalSyncEnabled(true) at the beginning of the programme and in the event 'lost focus' i was calling window.setFrameratelimit(30).

I have a question.. Whith VSYNC the framerate is set to 60? I can change it?
Title: Re: OpenGL application lag
Post by: AlexAUT on January 10, 2014, 09:44:35 pm
I have a question.. Whith VSYNC the framerate is set to 60? I can change it?

You could buy a 120hz monitor  :D. Because vertical sync does basically sync your "gpu" refreshrate with the monitor refreshrate to avoid screen tearing and other effects. So if you have a 60hz monitor (like 99% of the displays in europe) you get 60fps in your game.


AlexAUT
Title: Re: OpenGL application lag
Post by: BaneTrapper on January 13, 2014, 07:14:04 pm
I have a question.. Whith VSYNC the framerate is set to 60? I can change it?
You can change it if your setup supports different frame rates.