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

Author Topic: SFML.Net Windows 7 Stutter  (Read 6010 times)

0 Members and 3 Guests are viewing this topic.

schragnasher

  • Newbie
  • *
  • Posts: 6
    • View Profile
SFML.Net Windows 7 Stutter
« on: August 17, 2012, 03:08:43 am »
I really hope i can get help with this issue. It has plagued me for weeks now and its so fundamental to my program.

I have written a very simple program (below) in an attempt at using the Gaffer on Games timestep functionality to achieve smooth animation.

The problem i keep running into is a terrible stuttering effect, it jumps around all the time and is not smooth. Yet i have had people say everything is just fine on their PC. I have two PCs of similar hardware that this happens on.

I have tried using OpenTK and saw the same issue, SlimDX did not do this. I hoped SFML would be my savior.  If i set the exe to disable Desktop Composition, the animation is much more smooth than before.

using System;
using SFML;
using SFML.Graphics;
using SFML.Window;
using Tao.OpenGl;

namespace sample_opengl {

    static class Program {

        static RenderWindow Window;
        static float prevPos = 0.0f;
        static float nextPos = 0.0f;
        static float speed = -5.0f;

        static void Main() {
            Window = new RenderWindow(new VideoMode(800, 600), "SFML.Net OpenGL", Styles.Close);
            Window.UseVerticalSync(true);

            Window.Closed += new EventHandler(OnClosed);
            Window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluOrtho2D(-400, 400, -300, 300);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();

            float frameTime = 0.0f;
            float dt = 0.03f;
            float accumulator = 0.0f;
            float alpha = 0.0f;

            while (Window.IsOpened()) {
                Window.DispatchEvents();

                frameTime = Window.GetFrameTime();

                if (frameTime > 0.25f) {
                    frameTime = 0.25f;
                }

                accumulator += frameTime;

                while (accumulator >= dt) {
                    accumulator -= dt;
                    Update(dt);
                }

                alpha = accumulator / dt;
                Render(alpha);
            }
        }

        static void Update(float dt) {
            prevPos = nextPos;
            nextPos += speed;
            if (nextPos > 300 || nextPos < -400) { speed *= -1; }
        }

        static void Render(float alpha) {
            Window.Clear();
            Gl.glPushMatrix();
            Gl.glTranslatef(nextPos * alpha + prevPos * ( 1.0f - alpha ), 0.0f, 0.0f);
            Gl.glBegin(Gl.GL_QUADS);
            {
                Gl.glVertex2f(0, 0);
                Gl.glVertex2f(100, 0);
                Gl.glVertex2f(100, -100);
                Gl.glVertex2f(0, -100);
            }
            Gl.glEnd();
            Gl.glPopMatrix();
            Window.Display();
        }

        static void OnClosed(object sender, EventArgs e) {
            RenderWindow window = (RenderWindow)sender;
            window.Close();
        }

        static void OnKeyPressed(object sender, KeyEventArgs e) {
            RenderWindow window = (RenderWindow)sender;
            if (e.Code == KeyCode.Escape)
                window.Close();
        }
    }
}

I have been doing ALOT of searching on this issue and seen similar posts, all of which end simply by saying...disable Desktop Composition :p  That is not really an answer. I also came across discussion of a pixel format descriptor PFD_SUPPORT_COMPOSITION and wondered if this is missing in same way, i dunno im grasping at anything at this point. Can anyone help me out?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML.Net Windows 7 Stutter
« Reply #1 on: August 17, 2012, 07:47:09 am »
I'm not sure if this will solve anything but shouldn't it be nextPos += speed * dt? The movement should be depend on the time step. Have you ever tried not to use vsync but FramrateLimit? Vsync can somtimes create strange effects...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

schragnasher

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: SFML.Net Windows 7 Stutter
« Reply #2 on: August 17, 2012, 02:13:49 pm »
I'm not sure if this will solve anything but shouldn't it be nextPos += speed * dt? The movement should be depend on the time step. Have you ever tried not to use vsync but FramrateLimit? Vsync can somtimes create strange effects...

I should not need to do that as my dt is fixed and will always be the same, yet even when i throw out fixed time steps i get the same issue, jittery normally then smooth with composition off...

using System;
using SFML;
using SFML.Graphics;
using SFML.Window;
using Tao.OpenGl;

namespace sample_opengl {

    static class Program {

        static RenderWindow Window;
        static float pos = 0.0f;
        static float speed = -100.0f;

        static void Main() {
            Window = new RenderWindow(new VideoMode(800, 600, 32), "Test", Styles.Close, new WindowSettings(32, 0, 0));
            Window.UseVerticalSync(true);

            Window.Closed += new EventHandler(OnClosed);
            Window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluOrtho2D(-400, 400, -300, 300);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();

            while (Window.IsOpened()) {
                Window.DispatchEvents();
                Window.Clear();
                Update(Window.GetFrameTime());
                Render();
                Window.Display();
            }
        }

        static void Update(float dt) {
            pos += speed * dt;
            if (pos > 300 || pos < -400) { speed *= -1; }
        }

        static void Render() {
            Gl.glPushMatrix();
            Gl.glTranslatef(pos, 0, 0);
            Gl.glBegin(Gl.GL_QUADS);
            {
                Gl.glVertex2f(0, 0);
                Gl.glVertex2f(100, 0);
                Gl.glVertex2f(100, -100);
                Gl.glVertex2f(0, -100);
            }
            Gl.glEnd();
            Gl.glPopMatrix();
        }

        static void OnClosed(object sender, EventArgs e) {
            RenderWindow window = (RenderWindow)sender;
            window.Close();
        }

        static void OnKeyPressed(object sender, KeyEventArgs e) {
            RenderWindow window = (RenderWindow)sender;
            if (e.Code == KeyCode.Escape)
                window.Close();
        }
    }
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML.Net Windows 7 Stutter
« Reply #3 on: August 17, 2012, 04:07:49 pm »
What about disabling VSync and using FramerateLimit instead?

Also I've no idea what "Desktop Composition" is nor what this is good for...
And "stuttering", "jumping around", "not smooth" doesn't really help to get the idea. What does stutter? The sprite? The view? Your own OpenGL stuff?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

schragnasher

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: SFML.Net Windows 7 Stutter
« Reply #4 on: August 17, 2012, 04:56:41 pm »
What about disabling VSync and using FramerateLimit instead?

Also I've no idea what "Desktop Composition" is nor what this is good for...
And "stuttering", "jumping around", "not smooth" doesn't really help to get the idea. What does stutter? The sprite? The view? Your own OpenGL stuff?

Framerate limiting has no difference, and turning off vsync, while it looks a little better, it still looks like it is happening and is no real solution, as it can be force on by most graphics cards and create a bad experience.

What i mean by jumpy is that it almost looks like the render position of the square in this code is jumping ahead and then jumping back as it moves, hard to explain. Desktop Composition is basically using Aero, turning it off shuts off the advanced Aero features in windows 7.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: SFML.Net Windows 7 Stutter
« Reply #5 on: August 17, 2012, 05:04:57 pm »
Quote
I have been doing ALOT of searching on this issue and seen similar posts, all of which end simply by saying...disable Desktop Composition :p  That is not really an answer. I also came across discussion of a pixel format descriptor PFD_SUPPORT_COMPOSITION and wondered if this is missing in same way, i dunno im grasping at anything at this point. Can anyone help me out?
This probably has nothing to directly do with that. The only thing desktop compositing would influence is your framerate, if you have a really slow card since you have vsync on anyway.

Code: [Select]
Window.GetFrameTime();You should really be careful when doing this. Depending on the precision provided by SFML anything too small might end up returning an elapsed time of 0 sec if your framerate is too high and that is definitely something you don't want to pass to your movement handlers. I could not check up on what it used to return because that method got removed a long time ago. The current method would involve having a Clock which you would query the microseconds of. That would provide enough precision most of the time.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

schragnasher

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: SFML.Net Windows 7 Stutter
« Reply #6 on: August 17, 2012, 05:17:01 pm »
Quote
I have been doing ALOT of searching on this issue and seen similar posts, all of which end simply by saying...disable Desktop Composition :p  That is not really an answer. I also came across discussion of a pixel format descriptor PFD_SUPPORT_COMPOSITION and wondered if this is missing in same way, i dunno im grasping at anything at this point. Can anyone help me out?
This probably has nothing to directly do with that. The only thing desktop compositing would influence is your framerate, if you have a really slow card since you have vsync on anyway.

Code: [Select]
Window.GetFrameTime();You should really be careful when doing this. Depending on the precision provided by SFML anything too small might end up returning an elapsed time of 0 sec if your framerate is too high and that is definitely something you don't want to pass to your movement handlers. I could not check up on what it used to return because that method got removed a long time ago. The current method would involve having a Clock which you would query the microseconds of. That would provide enough precision most of the time.

Ill check the output, but really this is not just an SFML issue, when using OpenTK i used the c# stopwatch for timing and it spit out perfectly acceptable values yet still skipped with DC on and was smooth with DC off...

I have a GTX650 Ti and an i7 processor, so i don't think speed is any issue here. and neighter is framerate, as my Framerte in fraps shows a steady 60. Its almost like the bufferswap is jumping ahead or showing the wrong frame, its very odd.

schragnasher

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: SFML.Net Windows 7 Stutter
« Reply #7 on: August 18, 2012, 06:05:10 am »
So it seems i can eliminate the stutter just by drawing more squares, i drew the simple white square 1000 times each frame and the problem disappears and the animation is smooth. Any thoughts as a possible reason?

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: SFML.Net Windows 7 Stutter
« Reply #8 on: August 18, 2012, 06:52:06 am »
Ill check the output, but really this is not just an SFML issue, when using OpenTK i used the c# stopwatch for timing and it spit out perfectly acceptable values yet still skipped with DC on and was smooth with DC off...

I have a GTX650 Ti and an i7 processor, so i don't think speed is any issue here. and neighter is framerate, as my Framerte in fraps shows a steady 60. Its almost like the bufferswap is jumping ahead or showing the wrong frame, its very odd.
You misunderstood me. The first part of my post merely ruled out the fact that compositing is directly causing stuttering in your program. It is indirectly causing it by changing the load on your CPU/GPU even if just subtly.

So it seems i can eliminate the stutter just by drawing more squares, i drew the simple white square 1000 times each frame and the problem disappears and the animation is smooth. Any thoughts as a possible reason?
As you realize, you have a powerful system, and the main problem here is that your framerate is too high. Even if you limit the framerate superficially, the time your program spends doing anything is still very short before it goes to sleep. High framerates are more sensitive to jitter than low ones are. IIRC Fraps displays an averaged framerate which means it is unsuitable for detecting hiccups that occur. You can try outputing the value of GetFrameTime() as well as the "true" (as in time between beginning of your loop and before you call Window.Display() ) frametime every frame to the console and see if anything strange shows up.

Lastly always be sure to use the latest (a.k.a GitHub) version of the library/binding. The last version of SFML which still included GetFrameTime() was more than half a year ago and in that time many fixes in the bindings as well as the library code were made. It might just be that one of those changes addresses this issue.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).