Turning VSync off in OpenGL

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Aug 09, 2003 at 21:23
typedef BOOL (APIENTRY *PFNWGLSWAPINTERVALFARPROC)( int );
PFNWGLSWAPINTERVALFARPROC wglSwapIntervalEXT = 0;

void setVSync(int interval=1)
{
  const char *extensions = glGetString( GL_EXTENSIONS );

  if( strstr( extensions, "WGL_EXT_swap_control" ) == 0 )
    return; // Error: WGL_EXT_swap_control extension not supported on your computer.\n");
  else
  {
    wglSwapIntervalEXT = (PFNWGLSWAPINTERVALFARPROC)wglGetProcAddress( "wglSwapIntervalEXT" );

    if( wglSwapIntervalEXT )
      wglSwapIntervalEXT(interval);
  }
}

The default ‘interval’ is 1, which means VSync is on. Setting it to 0 would turn it off.

14 Replies

Please log in or register to post a reply.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Aug 10, 2003 at 08:33

nice snippet-idea, and nice snippet!

0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Aug 12, 2003 at 21:07

It’s kinda only really half using OpenGL, though. It uses wgl extensions which are (obviously) only available using windows.

I don’t see why you’d really want to turn off VSync. Yes it limits frame rate, but once you get to 60, why go higher?

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Aug 12, 2003 at 21:29

for measurements.

i do that in my raytracer currently, to measure raw performance.

and, if you drop from 60fps, disabling vsync is good, as you else get 60-30-15fps, i can have everything between, too.. and there is no real difference between 50 and 60 fps.. but “jumps” from 30 to 60 to 30 to 60 to 30 to 60 are visible..

Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Aug 12, 2003 at 21:33

Well, during development, you would have to do optimizations and also analyize your code. Turning VSync off would tell you the real frames per second of your application.

0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Aug 13, 2003 at 17:57

I’ll need to find a linux way to do it!

F33eec98e0fc761467c315aad881c035
0
Jesse_M 101 Aug 22, 2003 at 01:36

Or you could just use your compilers profiler, because it’s going to give a hell of alot more information than the frame rate will. And in any case you shouldn’t worry about optimizing anything during developement anyway. That’s what alpha testing is for.

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Aug 22, 2003 at 05:32

its still a very useful feature.. very cheap to support, so what?

744d06ebf78d82e8667f1f7ad8392daf
0
SYS49152 101 Dec 29, 2004 at 10:19

hello guys…. i have some quick questions about the vsync story.
i dont know if i should start a new thread so sorry if this is the wrong place for this.

davepermen wrote:


and, if you drop from 60fps, disabling vsync is good, as you else get 60-30-15fps, i can have everything between, too.. and there is no real difference between 50 and 60 fps.. but “jumps” from 30 to 60 to 30 to 60 to 30 to 60 are visible..

i have this sometimes in my application. most of the time it runs 60fps but then sometimes 30fps appears. is this because the grafics-pipeline is not balance or something ? or has this to do with alphablending operations ?
in my project i have some alpha-blended objects. so ofcourse its the typical effect that appears with alpha-blendings sometimes ?

Die Kharrat wrote:


Well, during development, you would have to do optimizations and also analyize your code. Turning VSync off would tell you the real frames per second of your application.

i remember that i read on few boards that fps over 60fps doesnt really say something…..

so iam abit confused what is really true and what not…. :blush:

  • Andy
Fdbdc4176840d77fe6a8deca457595ab
0
dk 158 Dec 29, 2004 at 19:05

FPS is not the optimal measure of how efficient your coding is, but is just a general measure. An FPS of more than 60 is not important in terms of the human eye, where differences between 60 and 70 for example are not noticeable to the human eye.

However, during development, most developers turn vsync off so that they have a better measure of how certain things (adding a model, blending, etc.) affect the FPS.

However, the best way to measure efficiency and to optimize your code is to use a runtime profiler, which is a “tool” for timing bits of your code. You can either write your own or use a commercial one (there might be even free open source ones). With that tool, you’ll be able to measure for example what percentage of time a portion of a code took to execute, and the maximum and minimum time peaks it took, etc.

But for general purposes, FPS can work. Here’s an article that explains why FPS is not the best way to measure efficiency or optimize code.

744d06ebf78d82e8667f1f7ad8392daf
0
SYS49152 101 Dec 30, 2004 at 12:30

nice link. thank you.

  • Andy
4c9a0ef2e190977669bb175f835e7cdc
0
ahxian125 101 Sep 19, 2005 at 13:33

err…im a newb..where do u put these lines in? to turn off vsync

065f0635a4c94d685583c20132a4559d
0
Ed_Mack 101 Sep 19, 2005 at 16:09

Anywhere. Just make sure it runs once, and probably run that code once you have opened a window/about to.

C35e61a4dbfa975ca8cd4b3c98ff1229
0
Fragztor 101 Sep 22, 2007 at 14:07

i dont understand.. where is OpenGL?? ive only heard of it in CS and i dont know where to put the code in?? you said Anywhere and i did.. in my desktop and nothing happend? where shud i put it in?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Sep 22, 2007 at 16:12

Umm he meant anywhere *in your OpenGL program*, not anywhere in your file system =D

(Actually, should be in the initialization section of the program, not just anywhere.)