Search code examples
c#openglsharpgl

Diagnosis of error 1008 when calling wglsharelists for two OpenGL contexts


I'm having troubles diagnosing the cause of error 1008 when calling wglsharelists to share the space of two OpenGL contexts (created using sharpgl). The call returns false and the last error code retrieved is 1008.

I've made the two OpenGL contexts identical in construction and made sure that neither are current when calling the share lists function. I've cut it down to the absolute simplest form and can't get past this error. I also noticed that if I try to access either of the render context pointers before trying to link them the error code changes to 3221684311. Code is below:

static void Main(string[] args)
{

    OpenGL gl1 = new OpenGL();
    gl1.Create(SharpGL.Version.OpenGLVersion.OpenGL3_1, RenderContextType.FBO, 1, 1, 16, null);

    OpenGL gl2 = new OpenGL();
    gl2.Create(SharpGL.Version.OpenGLVersion.OpenGL3_1, RenderContextType.FBO, 1, 1, 16, null);

    IntPtr rc1 = gl1.RenderContextProvider.RenderContextHandle;
    IntPtr rc2 = gl2.RenderContextProvider.RenderContextHandle;

    //These lines when uncommented change the return error to 3221684311
    //Debug.WriteLine("Render Context 1: " + rc1.ToString());
    //Debug.WriteLine("Render Context 2: " + rc1.ToString());

    Debug.WriteLine(Win32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero));

    Debug.WriteLine(Win32.wglShareLists(rc1, rc2));
    Debug.WriteLine(GetLastError());
}

[DllImport("Kernel32.dll", SetLastError = true)]
private extern static UInt32 GetLastError();

Solution

  • It turns out the problem is in the render context creation from SharpGL. The moment you create any buffers in a context it can't be set to share with another existing context. When choosing the FBO context type, SharpGL performs some creations of frame buffers when the Create method is called. By setting the render context type to HiddenWindow the sharing worked.

    This is by no means a solution (I imagine I'm going to have to write my own FBO creation code in place of what SharpGL does and swap the custom render context out with the device context) but it is ultimately the cause of my error.