LWJGL3 共享上下文抛出 "GLFW_VERSION_UNAVAILABLE"

LWJGL3 shared context throws "GLFW_VERSION_UNAVAILABLE"

我尝试对我的应用程序进行多线程处理,但是当我尝试从我的主 window 共享上下文时,程序崩溃了。

[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)

这是主要的创建代码 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();

以及我第二个线程中的代码:

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

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

我做错了什么? 版本:稳定的 LWJGL 3.1.3 快照构建 1

这里至少有一个问题是您从不同线程调用 glfwCreateWindow,而

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

您必须基于 ConcurrentLinkedQueue 或其他任何方式实现线程之间的消息传递,以便仅从除主线程之外的每个线程请求 window 创建,而主线程必须侦听这些请求并执行使用此函数调用的实际创建

如果 Grief 的回答不起作用,您还必须确保仅当资源共享 window 和已与此共享共享的 windows 时才调用 glfwCreateWindow,不得在任何线程中处于当前状态。

来自 glfwCreateWindow javadoc:

Windows: The context to share resources with may not be current on any other thread. [ref]

您可以通过在主线程(或您正在创建主线程 window 的线程)中调用 GLFW.glfwMakeContextCurrent(0) 然后创建 window 并在第二个线程。