SDL2 not respecting VSync

Hi there,
I have an odd issue, which is probably something wrong with my code. Many users who have their VSync to default or “Off unless application specifies” are having FPS around 300 or so.

I’m passing vsync to SDL but maybe I’m missing something

A simplistic view of what I’m doing

Code:

SDL_Init(SDL_INIT_VIDEO);

SDL_GL_SetSwapInterval(1);
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

unsigned int flags = SDL_WINDOW_OPENGL;
if (fullscreen) {
flags |=SDL_WINDOW_FULLSCREEN;
}

        _window = SDL_CreateWindow(title.c_str(),
                                   SDL_WINDOWPOS_CENTERED,
                                   SDL_WINDOWPOS_CENTERED,
                                   width, height,
                                   flags);

_glcontext = SDL_GL_CreateContext(_window);

After this I do my own GL calls and on the Anim pump I do

Code:

//GL draw calls, etc

SDL_GL_SwapWindow(_window );

Help is much appreciated :)------------------------
@DJ_Link

www.david-amador.com

Just a wild guess, have you tried setting The GL attributes before calling SDL_Init()? I don’t know if this would work, but it should be fairly easy to try.

– Aggelos Kolaitis
– The SDL Utility Library (http://bitbucket.org/sdlu/sdlu)On May 3, 2014, at 2:42 PM, “^DJ_Link^” <david.djlink at gmail.com> wrote:

Hi there,
I have an odd issue, which is probably something wrong with my code. Many users who have their VSync to default or “Off unless application specifies” are having FPS around 300 or so.

I’m passing vsync to SDL but maybe I’m missing something

A simplistic view of what I’m doing

Code:

SDL_Init(SDL_INIT_VIDEO);

SDL_GL_SetSwapInterval(1);
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

unsigned int flags = SDL_WINDOW_OPENGL;
if (fullscreen) {
flags |=SDL_WINDOW_FULLSCREEN;
}

        _window = SDL_CreateWindow(title.c_str(),
                                   SDL_WINDOWPOS_CENTERED,
                                   SDL_WINDOWPOS_CENTERED,
                                   width, height,
                                   flags);

_glcontext = SDL_GL_CreateContext(_window);

After this I do my own GL calls and on the Anim pump I do

Code:

//GL draw calls, etc

SDL_GL_SwapWindow(_window );

Help is much appreciated

@DJ_Link

www.david-amador.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Just tried it, same effect :(------------------------
@DJ_Link

www.david-amador.com

Ah, found the problem.

SDL_GL_SetAttribute needs to be called before creating the window
and
SDL_GL_SetSwapInterval needs to be called after SDL_GL_CreateContext()

Code:

SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

unsigned int flags = SDL_WINDOW_OPENGL;
if (fullscreen) {
flags |=SDL_WINDOW_FULLSCREEN;
}

_window = SDL_CreateWindow(title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width, height,
flags);

_glcontext = SDL_GL_CreateContext(_window);
SDL_GL_SetSwapInterval(1);------------------------
@DJ_Link

www.david-amador.com

You already figured it out, but the reason for this is that
SDL_GL_SetSwapInterval() works on the current GL context (and can be
used to change that state on a GL context at any time), and
SDL_GL_SetAttribute() is used to set unchangeable state for the next
context you create.

(I think SDL 2.1 should get rid of SDL_GL_SetAttribute(), though, and
just make those variables part of SDL_GL_CreateContext().)

–ryan.On 05/03/2014 09:45 AM, ^DJ_Link^ wrote:

Ah, found the problem.

SDL_GL_SetAttribute needs to be called before creating the window
and
SDL_GL_SetSwapInterval needs to be called after SDL_GL_CreateContext()

Ryan C. Gordon wrote:

You already figured it out, but the reason for this is that
SDL_GL_SetSwapInterval() works on the current GL context (and can be
used to change that state on a GL context at any time), and
SDL_GL_SetAttribute() is used to set unchangeable state for the next
context you create.

(I think SDL 2.1 should get rid of SDL_GL_SetAttribute(), though, and
just make those variables part of SDL_GL_CreateContext().)

–ryan.

I think that would be a good change, this way we send all variables whenever the context is created, probably less included to user induced errors in the order (Although it makes perfect sense so SetAttribute to be used only after context is created)------------------------
@DJ_Link

www.david-amador.com

That would be nice, but it would only work if all backends for SDL_CreateWindow no longer rely on SDL GL attribute stuff (the Windows and Linux ones do, for example) - I don?t know enough about WGL and GLX to say whether that?s feasible or not.On May 3, 2014, at 4:09 PM, Ryan C. Gordon wrote:

(I think SDL 2.1 should get rid of SDL_GL_SetAttribute(), though, and just make those variables part of SDL_GL_CreateContext().)

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

For WGL, you can move the SetPixelFormat call from SDL_CreateWindow to
SDL_GL_CreateContext. This way you no longer need GL attributes to be set
before from SDL_CreateWindow.

For GLX, the window and GL context need to use the same visual, otherwise
some drivers will error out in glXMakeCurrent. However, it appears possible
to modify the window visual after the window is created, so this can also
be done in SDL_GL_CreateContext.

I am using this approach in OpenTK and it works on both platforms.

2014-05-03 21:56 GMT+02:00 Alex Szpakowski :> That would be nice, but it would only work if all backends for

SDL_CreateWindow no longer rely on SDL GL attribute stuff (the Windows and
Linux ones do, for example) - I don?t know enough about WGL and GLX to say
whether that?s feasible or not.

On May 3, 2014, at 4:09 PM, Ryan C. Gordon wrote:

(I think SDL 2.1 should get rid of SDL_GL_SetAttribute(), though, and
just make those variables part of SDL_GL_CreateContext().)

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org