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

Author Topic: Stutter Occurs In Application  (Read 3363 times)

0 Members and 1 Guest are viewing this topic.

NoNick2

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Stutter Occurs In Application
« on: March 13, 2021, 07:53:48 am »
Hello SFML community,

I've had an annoying bug in my program since I began it months ago.  So, my program randomly "stutters", below is a video demonstrating what I mean:

http://streamable.com/9ajq63

Here is my source code, that validates the issue:

#include <SFML/Graphics.hpp>

// This is for multi-graphics cards in a laptop, bug happens with or without this
#ifdef _WIN32
extern "C" {
__declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
__declspec(
                dllexport) unsigned int AmdPowerXpressRequestHighPerformance = 0x1;
}
#endif

int main () {
        sf::ContextSettings settings;
        settings.antialiasingLevel = 0;

        sf::RenderWindow renderWindow(
                        { 2560, 1440 },
                        "FunTitleForForum",
                        sf::Style::Default,
                        settings
        );
        renderWindow.setVerticalSyncEnabled( true );

        sf::Texture textureTEMP;
        textureTEMP.loadFromFile("../Source/TextureManager/TileMap/atlas_48x.png" );

        sf::Sprite spriteTEMP { textureTEMP };

        sf::View gameView;
        gameView.setSize( renderWindow.getDefaultView().getSize());

        renderWindow.setView( gameView );

        sf::Event event {};
        while ( renderWindow.isOpen()) {

                while ( renderWindow.pollEvent( event )) {
                        if ( event.type == sf::Event::Closed ) {
                                renderWindow.close();
                        }
                }
                if ( sf::Keyboard::isKeyPressed( sf::Keyboard::D )) {
                        gameView.move( 10, 0 );
                } else if ( sf::Keyboard::isKeyPressed( sf::Keyboard::A )) {
                        gameView.move( -10, 0 );
                }

                renderWindow.clear();
                renderWindow.setView( gameView );
                renderWindow.draw( spriteTEMP );
                renderWindow.display();

        }
        return 0;
}

 

Here is what I have tried (not in any order):
    Set all textures to smooth
    Implement timestep manually
    Use kairos timestep library
    Making sure compiler version and sfml version match
    Rebuilding sfml
    Statically linking sfml instead of dynamically linking

One note is that I have a 144hz screen output, and if you need any more additional info please let me know!

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Stutter Occurs In Application
« Reply #1 on: March 13, 2021, 12:49:20 pm »
Do you have an nvidia gpu? If so disabling 'threaded optimization' in the nvidia control panel can help.

NoNick2

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Stutter Occurs In Application
« Reply #2 on: March 13, 2021, 08:21:42 pm »
Yeah, that is something else I have tried but forgot to mention, on or off doesn't seem to make a difference, it is left as off for now though.

NoNick2

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Stutter Occurs In Application
« Reply #3 on: March 17, 2021, 08:34:35 pm »
Sorry for the bump, but does anyone else have an idea on how to solve this issue by chance? 

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Stutter Occurs In Application
« Reply #4 on: March 18, 2021, 10:54:37 am »
To narrow down the cause you could try some code known to work, such as the source of the SFML game development book: https://github.com/SFML/SFML-Game-Development-Book or, if you're desperate ( ;) ), one of my projects: https://github.com/fallahn/xygine . There are of course others in the SFML projects sub-forum which you can check out.

If these run smoothly for you (although experience tells me you will need nvidia's threaded optimisation disabled) then the root cause is likely your code, and you can use the example source as a launching point for creating your own project.

If they don't then there's possibly a problem with your driver/hardware/OS combination. It may or may not also help to enable/disable v-sync. If you have access to another computer then try running your own and the sample code on there, to see if there's a difference.

antoine2509

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Stutter Occurs In Application
« Reply #5 on: April 22, 2022, 04:08:11 pm »
Hello everyone,
I'm struggling exactly with the same issue, and for very basic lines of code. This draws a rectangle with a rectilinear uniform trajectory (analytical position based on the current frametime).

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1600, 900), "SFML works!");
    sf::Clock globalClock;
    sf::RectangleShape rectangle({100, 100});

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }

        rectangle.setPosition(globalClock.getElapsedTime().asSeconds()*sf::Vector2f(600,300));
        window.clear();
        window.draw(rectangle);
        window.display();
    }

    return 0;
}
 

I won't post any video or gif but it's exactly like what you see in these links :
https://en.sfml-dev.org/forums/index.php?topic=16449.0
http://streamable.com/9ajq63

Enabling/disabling vertical sync or setting a frame limit doesn't solve the problem.

My config is :
OS Name : Ubuntu 20.04.4 LTS
OS Type : 64 bit
GNOME Version : 3.36.8
Windowing System : X11
Processor : Intel® Core™ i9-9880H CPU @ 2.30GHz × 16
Gaphics : NVIDIA Corporation TU117GLM [Quadro T2000 Mobile / Max-Q]

I've read on different posts that it could be due to threaded optimization of Nvidia, but it seems it is off by default on Linux. At least there is no direct access to this option for all applications in NVIDIA X SERVER Settings, but if I try to set an application profile for my game I can see there is a boolean GLTHREADEDOPTIMIZATIONS and its value is false by default...

I really don't know what to do and any help would be useful :)
Thank you!

« Last Edit: April 22, 2022, 08:03:59 pm by antoine2509 »

antoine2509

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Stutter Occurs In Application
« Reply #6 on: April 22, 2022, 06:03:13 pm »
(Re)hello everyone! :)

After 3 days of trying everything and right after having decided to post a first comment here, I finally got a slight amelioration...

I decided to go to Ubuntu settings > About > Software Updates > Additional Drivers

I had the last recommended driver for my graphic card :
Using NVIDIA driver metapackage from nvidia-driver-470 (proprietary, tested)
I changed it to :
Using NVIDIA driver metapackage from nvidia-driver-510 (proprietary)

After rerunning my SFML app, I noticed that the stuttering was still there, but way less frequent. Before it was happening once or twice a second, now it's about once every 5 or 6 seconds.

I continued to investigate and noticed in the NVIDIA X SERVER settings (equivalent of control panel but on Linux I suppose), that my driver modification had changed the option from NVIDIA (Performance mode) to NVIDIA On-Demand in the PRIME Profiles tab.

So I tried to revert my driver changes, came back to the initial and recommended one (470) and switched from NVIDIA Performance mode to NVIDIA On-Demand in the NVIDIA X SERVER Settings. And this has the same result, the stuttering is 10 times less frequent than before, but still there...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Stutter Occurs In Application
« Reply #7 on: April 24, 2022, 09:50:44 pm »
If you want smooth movements, you'll need to use a Fixed Time Step including the interpolation step.

If you directly bind the movement to the previous frame time, you can easily get into trouble, as the frame time can slightly fluctuate due to all sorts of things, which then can easily be determined as stutter by the human eye/brain.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

antoine2509

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Stutter Occurs In Application
« Reply #8 on: April 27, 2022, 04:48:15 pm »
Actually my initial problem comes from a game where I used a fixed time step for the physics update, and render each frame using the current time got from the globalClock and doing a linear interpolation between the previous physics state and the current one.

When reproducing the stutter problem with the little example I gave in this post, I tried to limit it to 60 fps while my computer is able to run it at 1000+ fps. When I print the fps in the terminal, they are perfectly stable, between 59.995 and 60.005, and there are no jumps in time at all. So I guess the problem can't come from there, or maybe I miss something here that you could explain?

However, today I tried to look at this bug again, and it seems to have disappeared. I can't reproduce it even with NVIDIA performance mode, that's really strange...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Stutter Occurs In Application
« Reply #9 on: April 28, 2022, 08:28:06 am »
I guess binary1248 gave in the other thread which you linked some explanation.

If you can capture it on video, you could step through it frame by frame and spot what exactly is going on.
The human eye/brain recognizes a single delayed frame as stutter.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything