Popiel
Popiel

Reputation: 193

WinAPI - Synchronizing SwapBuffers

Is it possible to synchronize SwapBuffers in many threads? When I try to turn on vertical synchronization (wglSwapIntervalEXT), it stops all threads, until it doesn't do tick (ex. when I open 3 windows, every window has approximately 20 frames [60/3] ) Every window has separate thread, and of course every thread has his own SwapBuffer function.

Upvotes: 0

Views: 1055

Answers (2)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

Swap Control is per-window in WGL (when you set it, it is applied to the window that the current render context is tied to). The association between render context and window is tied to the device context (see wglMakeCurrent (...)). You probably only need VSYNC for 1 of your 3 windows if you are reliably hitting < ~5.6 ms frame time in each.

What you should consider here is having one of your contexts set the swap interval to 1 and the remaining 2 use 0. The context that is syncrhonized to VBLANK (swap interval = 1) will lead the other two threads. That is to say, have the other two threads call glFlush (...) and then busy wait until the first thread stops blocking for VSYNC before calling SwapBuffers (...). The reason for the glFlush (...) is so that the other two threads accomplish some useful rendering tasks while you wait for the first (synchronized) swap to finish.

It sounds funny - almost like a recipe for tearing - but given the nature of Windows Vista/7/8's compositing window manager, VSYNC does not actually prevent tearing anymore. The window manager itself does that by compositing asynchronously, it effectively performs triple buffering. What VSYNC will, however, allow you to do (when done correctly) is have all 3 of your windows update their contents each refresh (avoid late frames).

If you did not bother starting your series of buffer swaps at the beginning of VBLANK, you could run into a situation where the compositing window manager displays an old frame for 2 frames because you were swapping buffers in the middle of VBLANK. Granted, you can still run into this situation if you draw an excessively long frame, but it solves the case where a short frame was unfortunately swapped too close to the vertical retrace deadline to finish in time.

Upvotes: 2

Colonel Thirty Two
Colonel Thirty Two

Reputation: 26569

Why not just add a barrier before/after calling SwapBuffers?

Upvotes: 0

Related Questions