0

I am creating an application using OpenGL (4.2). By default, the rendering is done on a panel in a window, it works without issues, but the panel can be docked/undocked, which causes the underlying window handle to change, therefore I am recreating the context.

There are no errors (glGetError = GL_NO_ERROR), the context exists (wglGetCurrentContext returns nonzero).

  // This snippet happens in response to window handle changed.
  fDC := GetDC(Handle); // Always recreate DC

  if not fIsGlInitialized then
      fRC := CreateRenderingContextVersion(fDC, ...); // Only one RC

  wglMakeCurrent(fDC, fRC); // Edit: Fails with ERROR_INVALID_PIXEL_FORMAT 
  fIsGlInitialized := True;
  InitOpenGL(); // Reload OpenGL function pointers just in case

However, after the handle changes, there is nothing rendered (the panel just has default gray color).

What is the correct way of recreating the context, assuming I want to keep my OpenGL objects (such as VAOs)?

Tried recreating the rendering context: Causes Invalid Operation on glBindVertexArray with VAO created in the old context.

Tried using wglShareLists: Same error although I am not sure I used it correctly, I have seen people complaining it's legacy and some drivers are buggy, also I don't need multiple living contexts.

Tried creating new rendering context, using wglCopyContext(oldRC, fRC, GL_ALL_ATTRIB_BITS) and wglDeleteContext(oldRC), causes the same error.

4
  • What does wglMakeCurrent return? About context sharing: Please check which objects can be shared; VAOs are not sharable. Commented Aug 12 at 8:45
  • @BDL wglMakeCurrent returns True at first, but False after docking with GetLastError ERROR_INVALID_PIXEL_FORMAT. If VAOs can't be transferred, what is the proper way of handling this, recreate all buffers from scratch in the new context? Commented Aug 12 at 9:19
  • I'm not sure if/how it is possible to transfer the context to a different HWND. In typical sharing situations (rendering same data to multiple context), you'd have to create a new VAO. Buffers (created with glGenBuffers) are shared and do not have to be copied. Note, that the VAO itself does not store any data, it only stores which buffers are bound and to which attachment points they are bound. The data itself still resides in the buffer objects. Commented Aug 12 at 10:54
  • @BDL Thanks for the response, I have resolved this, seems like it is possible to transfer the context, but the pixel format needs to be set again on the new DC, which was the issue. Commented Aug 12 at 11:51

1 Answer 1

1

It is sufficient to call GetDC and use wglMakeCurrent with the new DC handle and old RC, however, it might be necessary to set your pixel format on the new DC.

As @BDL suggested, check the return value of wglMakeCurrent to avoid such bugs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.