Search code examples
multithreadingopengllwjglglfw

LWJGL3 shared context throws "GLFW_VERSION_UNAVAILABLE"


I tried to multi-Thread my application, but when I try to share the context from my main window, the program crashes.

[LWJGL]
GLFW_VERSION_UNAVAILABLE
error
Description : WGL: Failed to create OpenGL context
Stacktrace  :   
org.lwjgl.glfw.GLFW.nglfwCreateWindow(GLFW.java:1361)   
org.lwjgl.glfw.GLFW.glfwCreateWindow(GLFW.java:1521)

Here is the creation code for the main window:

GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit()) {
    throw new IllegalStateException("Unable to initialize GLFW");
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
if(fullscreen){
    window = glfwCreateWindow(vidmode.width(), vidmode.height(), windowTitle, glfwGetPrimaryMonitor(), NULL);
    Var.windowSizeX=vidmode.width();
    Var.windowSizeY=vidmode.height();
}else{
    window = glfwCreateWindow(windowSizeX, windowSizeY, windowTitle, NULL, NULL);
    glfwSetWindowPos(window, (vidmode.width() - windowSizeX) / 2, (vidmode.height() - windowSizeY) / 2);
}

if (window == NULL) {
    throw new RuntimeException("Failed to create the GLFW window");
}

glfwMakeContextCurrent(window);
if (vSync) {
    glfwSwapInterval(1);
} else {
    glfwSwapInterval(0);
}
GL.createCapabilities();

And the code in my second thread:

GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);

offsiteWindow = GLFW.glfwCreateWindow(1, 1, "offsite", MemoryUtil.NULL, window);//errors
GLFW.glfwMakeContextCurrent(offsiteWindow);
GL.createCapabilities();

What am I doing wrong? Version: Stable LWJGL 3.1.3 Snapshot build 1


Solution

  • At least one problem here is that you are calling glfwCreateWindow from different threads, while

    This function must only be called from the main thread. [ref]

    You have to implement messaging between threads based on ConcurrentLinkedQueue or whatever to do only requests for window creation from every thread except the main one while the main thread must listen to these requests and do the actual creation with this function call