One context, several windows

I have an application with multiple windows and after some heavy struggling with sharing stuff between contexts (wglShareLists) I stumbled across this on msdn:

A current rendering context has an associated device context. That device context need not be the same device context as that used when the rendering context was created, but it must reference the same device and have the same pixel format.

I tried it, and it seems to work perfectly. I simply create one RC for my first DC and then I use the same RC for all the DC:s which I create later. I no longer have to share stuff with wglShareLists, because everything now belongs to the same RC.

Are there any pifalls here which I have missed? I guess there might be, since I’ve never heard of anyone doing like this before…

I’ve done that before, works fine.

The only issue i had was a stupid error when i first set things up.

I was catching WM_MOUSEMOVE in each window and trying to read the depth buffer. Of course the depth buffer i was reading was that of the last window rendered with the shared RC, which wasn’t always the window that was responding to a WM_MOUSEMOVE message.

Just had to make the DC of the WM_MOUSEMOVE window current in WM_MOUSEMOVE.


a side question: is there any performance advantage/penalty when using only one context? i heard that switching contexts is expensive, and you do not have to switch contexts at all, just switch window handles (?)

At the time i did a quick perf check and found that calling wglMakeCurrent(DC, RC) was much faster when only the DC was being changed (RC was equal to wglGetCurrentContext) than when the RC was being changed (as well).

I don’t recall the numbers, but it was fast enough that i didn’t consider it a bottleneck and went ahead and implimented the one RC to many DC model.

It makes sense, the RC contains all the opengl state, the DC only the pixel buffers. I wouldn’t be suprised if a call to wglMakeCurrent, that only changes the DC, only needs to change a pointer to the DC in the RC.

Oh, i also saved the matrix states with the DC (window object) durring rendering so that they would be available in WM_MOUSEMOVE. There may be other (DC specific) state you want to save as well, my needs were pretty simple.

Switch window handles ?
What do you mean ?

window handle -> dc, drawing context.
i would be very interested in the figures, though!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.