Ok to call SwapBuffers in separate rendering thread?

Hi,

I’ve just spent hours trying to confirm if it’s ok or not to call SwapBuffers in a separate rendering thread under Windows/WGL.

My scenario:

Main Thread:
Create UI and GL window with an OpenGL context etc.
Create new render thread
Run UI event loop

Render Thread:
Make OpenGL context current in this thread
Loop:
Render scene using OpenGL
SwapBuffers(HDC)

My understanding is that, in general, GDI device handles (HDC) are not thread safe and must only be accessed from the thread that created the window. But on the other hand, calling SwapBuffers with the device handle as an argument in a separate OpenGL rendering thread without any particular synchronization precautions seems to be a common thing to do, including by popular OpenGL support libraries such as GLFW.

Is anyone able to definitively confirm that calling SwapBuffers on a device handle in a separate OpenGL Rendering thread is ok? (In general, or under Windows WGL specifically).

Thanks, Lars

I went ahead and called ::SwapBuffers((HDC)surface) in the rendering thread. This seemed to work just fine on most of the Windows machines we have tested on…

Last week though, we discovered an issue on a different machine, where the main UI (wxWidgets) becomes very sluggish when rendering and swapping from a separate theread.

Would love if anyone could provide pointer on how to properly do OpenGL rendering in a separate thread under Windows.

opengl is not threadsafe, and even nvidia’s threaded optimization feature is known to bump into situations where it harms performance.