简体   繁体   English

为什么不能在另一个线程上创建 glfw window?

[英]Why you can't create glfw window on another thread?

Yes I know opengl is not thread safe but what I want is run a new window with opengl in it on a different thread entirely without doing any sort of data manipulation over opengl and causing weird bugs what i want is have a web server that based on requests sent run a opengl thread render it then save it to a picture (basically im trying to render 3d virtual avatars for a website). Yes I know opengl is not thread safe but what I want is run a new window with opengl in it on a different thread entirely without doing any sort of data manipulation over opengl and causing weird bugs what i want is have a web server that based on发送的请求运行 opengl 线程渲染它然后将其保存到图片(基本上我试图为网站渲染 3d 虚拟化身)。 But I kept getting this error:但我一直收到这个错误:

OpenTK.Windowing.GraphicsLibraryFramework.GLFWException: GLFW can only be called from the main thread!

Code being (Licensed under public domain):代码(在公共领域许可):

public async Task RenderAvatar() {
    while(true) {
        HttpListenerContext td = await httpServer.GetContextAsync();
        HttpListenerRequest req = td.Request;
        HttpListenerResponse res = td.Response;
        if(req.HttpMethod == "GET") {
            if(req.Url.AbsolutePath.ToString() == "/renderAvatar.3d") {
                var parameters = HttpUtility.ParseQueryString(req.Url.Query);
                if(parameters.Count == 3) {
                    var pant = parameters[0].ToString();
                    var face = parameters[1].ToString();
                    var shirt = parameters[2].ToString();
                    Game game = new Game(new string[]{pant,face,shirt, null},new string[]{        "head",
                    "neck",
                    "torso",
                    "larm",
                    "rarm",
                    "lleg",
                    "rleg",null});
                    game.Run();                        
                }
            }
        }
    }

after accessing the web page exception throws but I'm pretty sure it should run fine in theory since I do not do any sort of access other than initializing game type: GameWindow object with parameters taken from the query and running it.在访问 web 页面后抛出异常,但我很确定它在理论上应该可以正常运行,因为除了初始化游戏type: GameWindow object,参数取自查询并运行它。 So I want to know what is the technical reason behind glfw don't letting me to run it on a different thread even if it will work fine in theory?所以我想知道 glfw 背后的技术原因是什么,即使理论上它可以正常工作,也不让我在不同的线程上运行它? If it will run fine as long as I don't do data manipulation from different thread Is theres a way to bypass the exception or can I avoid the error by Compiling OpenTK myself and removing the exception and expect it to run fine?只要我不从不同的线程进行数据操作,它就会运行良好是否有绕过异常的方法,或者我可以通过自己编译 OpenTK 并删除异常并期望它运行良好来避免错误?

Most GLFW functions, including glfwCreateWindow must by called from the main thread.大多数 GLFW 函数,包括glfwCreateWindow必须从主线程调用。 It says exactly that in the main GLFW reference and the OpenTK reference .它在主要GLFW 参考OpenTK 参考中准确地说明了这一点。

OpenGL is not GLFW. OpenGL 不是 GLFW。 The GLFW thread rules apply to GLFW functions, and so you must respect them while calling GLFW functions. GLFW 线程规则适用于 GLFW 函数,因此在调用 GLFW 函数时必须遵守它们。 If you want to run OpenGL code in a non main thread, that is all fine.如果您想在非主线程中运行 OpenGL 代码,那很好。

Importantly, glfwMakeContextCurrent may be called from any thread.重要的是, glfwMakeContextCurrent可以从任何线程调用。 This means that you can make a window on the main thread, hand it off to a second thread, and the second thread can then use the OpenGL context of that window.这意味着您可以在主线程上创建 window,将其交给第二个线程,然后第二个线程可以使用该 window 的 OpenGL 上下文。 The second thread can also call glfwSwapBuffers , but various other window functions like glfwPollEvents still must be called from the main thread.第二个线程也可以调用glfwSwapBuffers ,但其他各种 window 函数,如glfwPollEvents仍然必须从主线程调用。

If you are ever unsure about the thread safety of a GLFW function, check the reference and it will tell you: https://www.glfw.org/docs/3.3/modules.html如果您不确定 GLFW function 的线程安全性,请查看参考资料,它会告诉您: https://www.glfw.org/docs/3.3/modules.ZFC35FDC70D5FC69D23EZ8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM