javaswingopengllwjgl

LWJGL 3 sharing context between Swing and GLFW window


I'm creating an application in Java where there are two windows, a "game view" window, and a Swing window with debug options. Included in Swing window is another view of the game, where I can fly a debug camera around.

I am creating the "game window" with,

...
window = glfwCreateWindow(300, 300, "Game", NULL, NULL);
...

In the Swing part I am using https://github.com/LWJGLX/lwjgl3-awt for a canvas I can draw to. I am creating it with,

frame.add(canvas = new AWTGLCanvas(new GLData()) {
  public void initGL() {
    …
  }
  public void paintGL() {
    …
  }
});

How do I share the context between the two so I can use the same textures, VBOs, etc?

AWTGLCanvas offers a way to pass in another AWTGLCanvas to share their contexts via the GLData#shareContext field on the GLData instance that you pass in to the AWTGLCanvas constructor. GLData#shareContext needs another instance of AWTGLCanvas, which I can't get because the other window is made with GLFW.

I've tried passing in the context from my AWTGLCanvas to glfwCreateWindow as the share parameter, but it causes a crash in native code.

Upon digging into the source of AWTGLCanvas it uses WGL#wglShareLists to share the context. Calling that myself directly after creating the window in GLFW does not work, the method returns false, indicating an error according to the docs.

To reiterate my question, how do I share contexts between a "normal" LWJGL window, and in Swing.

I am open to using a different library for drawing on a Swing canvas if needed.


Solution

  • I figure out how to solve the problem. I call this right before rendering to the GLFW window.

    long hwnd = GLFWNativeWin32.glfwGetWin32Window(glfwWindowHandle);
    long hdc = User32.GetDC(hwnd);
    WGL.wglMakeCurrent(hdc, awtglCanvasContext);
    

    The GLFW window now uses the same context as the AWTGL Canvas.