# Wait for vSync without swapping buffers with SDL

I am trying to optimize my SDL rendering loop for an immediate mode GUI application that doesn't need to be redrawn every frame. I have vSync turned on and would like to skip buffer clearing / drawing altogether if the GUI hasn't changed.

The problem is, swapping buffers without redrawing causes flickering because of double / triple buffering. On the other hand, also skipping the buffer swap causes the program to run full throttle because the waiting is effectively done in SDL_GL_SwapWindow.

Ideally I would need some kind of function that waits for vSync without swapping buffers, but I haven't seen one in the SDL documentation. I've tried the following solutions but none are satisfying:

• Drawing VBOs every frame even if they haven't been updated. That will reduce load on the CPU but not on the GPU, so not ideal.
• Keeping track of which backbuffers are up to date, and only redraw them if they are dirty. This worked in my testing but requires to make some assumptions (the number of backbuffers, and hoping the driver didn't replace them with fresh ones). So not reliable enough.
• Manually waiting with a small delay. Probably the most reliable, but still not ideal.

VSync is done fully by the GPU. Available only through the use of GAPI to be only used internally by driver's GAPI implementation. There is no regular way you can get access to the VSync signal manually.

Manual framerate restriction is not same thing as VSync, however, because it will cause tearing, while VSync is synchronized with refresh rate of the monitor/GPU.

What I suggest you to do instead is to use framebuffer to draw GUI once when needed (is it really that expensive to update GUI every frame?) and then drawing static image from the framebuffer, when backbuffer has to be redrawn. (For messages like SDL_WINDOWEVENT_EXPOSED or SDL_WINDOWEVENT_RESIZED)

• Thanks, using a framebuffer is probably the cleanest approch. I hoped there was some way of accessing the vsync "signal" one way or another. Updating the GUI isn't so expensive, but I'm developing for an embedded system, so every optimization is welcome.
– Toun
Jul 8 '19 at 12:06

You can use SDL_GL_SetSwapInterval(0).

Parameters: