Share rendering context between window / pbuffer

I’d like to share a single OpenGL rendering context between two different drawing surface (a window and a pbuffer). Is there a way to do so under Windows?

I’ve tried with the following code, but after a wglMakeCurrent(hDC_pbuffer, hRC_window), I got a ERROR_INVALID_PIXEL_FORMAT from GetLastError.


// Pixel format for the window surface
PIXELFORMATDESCRIPTOR pfd = {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW |
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        24,
        0, 0, 0, 0, 0, 0,
        8,
        0,
        0,
        0, 0, 0, 0,
        16,
        8,
        0,
        PFD_MAIN_PLANE,
        0,
        0, 0, 0
};

// Pixel format for the pbuffer surface
{   
    WGL_DRAW_TO_PBUFFER_ARB, 1,
    WGL_COLOR_BITS_ARB, colorBits,
    WGL_ALPHA_BITS_ARB, colorBits == 32 ? 8 : 0,
    WGL_DEPTH_BITS_ARB, depthBits,
    WGL_STENCIL_BITS_ARB, stencilBits
};

Any kind of help (and code snippets) will be really appreciated. Thanks in advance!

Matteo.

The pixel format of the two surfaces need to be the same for them to use the same rendering context. Contexts are tied to a specific pixel format and can’t be used on a different one.

You cannot use the same rendering context with two different device contexts. You can create two rendering contexts that share objects, however.

However, you really should avoid using pbuffers if you can get away without them.

You cannot use the same rendering context with two different device contexts.

Yes, we can :slight_smile:
As Malcom stated, the only requirement is that these DC’s have the same pixelformats (quoting MSDN:)

The hdc parameter must refer to a drawing surface supported by OpenGL. It need not be the same hdc that was passed to wglCreateContext when hglrc was created, but it must be on the same device and have the same pixel format.

In PBuffer days I even witnessed that some vendors had a rather relaxed definition of “identical”, especially when using the wglMakeCurrentReadARB() API. For instance, I was able to bind a float-PBuffer to a context created for a 24-bit Window. But since this is nowhere-land and FBOs are much better anyway, I don’t recommend it.

A valid scenario even today is an application with multiple windows. Set them all to the same pixelformat and you can use one rendercontext to render into all those windows. Additionally, multiple PBuffers using the same pixelformat may also be used with the same context.