簡體   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發送的請求運行 opengl 線程渲染它然后將其保存到圖片(基本上我試圖為網站渲染 3d 虛擬化身)。 但我一直收到這個錯誤:

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

代碼(在公共領域許可):

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();                        
                }
            }
        }
    }

在訪問 web 頁面后拋出異常,但我很確定它在理論上應該可以正常運行,因為除了初始化游戲type: GameWindow object,參數取自查詢並運行它。 所以我想知道 glfw 背后的技術原因是什么,即使理論上它可以正常工作,也不讓我在不同的線程上運行它? 只要我不從不同的線程進行數據操作,它就會運行良好是否有繞過異常的方法,或者我可以通過自己編譯 OpenTK 並刪除異常並期望它運行良好來避免錯誤?

大多數 GLFW 函數,包括glfwCreateWindow必須從主線程調用。 它在主要GLFW 參考OpenTK 參考中准確地說明了這一點。

OpenGL 不是 GLFW。 GLFW 線程規則適用於 GLFW 函數,因此在調用 GLFW 函數時必須遵守它們。 如果您想在非主線程中運行 OpenGL 代碼,那很好。

重要的是, glfwMakeContextCurrent可以從任何線程調用。 這意味着您可以在主線程上創建 window,將其交給第二個線程,然后第二個線程可以使用該 window 的 OpenGL 上下文。 第二個線程也可以調用glfwSwapBuffers ,但其他各種 window 函數,如glfwPollEvents仍然必須從主線程調用。

如果您不確定 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