2

我将 GLFW 和 ImGui 用于涉及打开多个窗口的项目。到目前为止,我已经进行了设置,因此每次必须打开一个新窗口时,我都会生成一个线程来创建它自己的 GLFW 窗口和 OpenGL 上下文。线程函数看起来像这样:

window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
// Check for creation error...
glfwMakeContextCurrent(window);

ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();   // Is this supposed to be done per-thread?
// Calling specific impl-specific ImGui setup methods for GLFW & OpenGL3...
// Set up OpenGL stuff ...

while (!glfwWindowShouldClose(window))
{
    // Some heavy-duty processing happens here...

    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    // ImGui code is here...

    // Rendering some stuff in the window here...

    // Render ImGui last...
    ImGui::Render();
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

    glfwSwapBuffers(window);
}

// Calling impl-specific ImGui shutdown here...
glfwDestroyWindow(window);

我知道 GLFW 要求您从主线程(调用的那个glfwInit())轮询事件,所以我的主线程上有一个循环来执行此操作:

while (!appMustExit)
{
    glfwWaitEvents();
}
// appMustExit is set from another thread that waits for console input

所以我遇到的问题是我的 ImGui 控件不响应任何类型的输入,并且glfwWindowShouldClose()如果我单击关闭按钮,则永远不会返回 true。似乎输入状态仅在调用的线程上可用glfwPollEvents(),这让我相信你不能在仍然使用单独的线程进行渲染的同时结合 ImGui 和 GLFW!

如何解决此问题以允许 ImGui 和这些窗口响应 GLFW 事件?

我之前的尝试使用单个线程来迭代每个窗口并更新/渲染它,但我希望使用线程来帮助应用程序在打开许多窗口时更好地扩展。

更新:我想澄清一下,这个应用程序涉及实时处理复杂的机器视觉,并且 ImGui 代码部分与控制和响应这个机器视觉代码高度集成。因此,我希望能够在与此处理相同的线程上调用 ImGui 函数,这也意味着该线程必须能够响应 glfw 输入。

4

2 回答 2

3

我能够找到一种方法将 Dear ImGui 更改为(希望)线程安全的库,并自由使用说明thead_local符。

imconfig.h我必须创建一个新的线程本地 ImGuiContext 指针:

struct ImGuiContext;
extern thread_local ImGuiContext* threadCTX;
#define GImGui threadCTX

imgui_impl_glfw.cpp我必须将所有本地/静态变量更改为thread_local版本:

thread_local GLFWwindow*    g_Window = NULL;    // Per-Thread Main window
thread_local GlfwClientApi  g_ClientApi = GlfwClientApi_Unknown;
thread_local double         g_Time = 0.0;
thread_local bool           g_MouseJustPressed[5] = { false, false, false, false, false };
thread_local GLFWcursor*    g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 };

// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
static thread_local GLFWmousebuttonfun   g_PrevUserCallbackMousebutton = NULL;
static thread_local GLFWscrollfun        g_PrevUserCallbackScroll = NULL;
static thread_local GLFWkeyfun           g_PrevUserCallbackKey = NULL;
static thread_local GLFWcharfun          g_PrevUserCallbackChar = NULL;

同样,imgui_impl_opengl3.h我对 OpenGL 对象句柄也做了同样的事情:

static thread_local char         g_GlslVersionString[32] = "";
static thread_local GLuint       g_FontTexture = 0;
static thread_local GLuint       g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0;
static thread_local int          g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0;                                // Uniforms location
static thread_local int          g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_AttribLocationVtxColor = 0; // Vertex attributes location
static thread_local unsigned int g_VboHandle = 0, g_ElementsHandle = 0;

通过这些少量更改,我现在能够创建 GLFW 窗口和 OpenGL 上下文,初始化 Dear ImGui,并调用glfwPollEvents每个线程,而不会相互影响。本质上,每个创建 GLFW 窗口的线程都可以像“主”线程一样使用。

这个解决方案可能有一些挫折,但它似乎适用于我的用例,每个窗口在自己的线程中运行其事件循环,有自己的 OpenGL 和 ImGui 上下文,并且窗口不相互交互或共享资源。

于 2019-08-12T16:50:57.373 回答
-1

为什么你首先要创建多个线程?您可以完美地创建多个 GLFW 窗口和多个 Dear ImGui 上下文,并在同一个线程中管理所有内容。从不同的线程工作只会让一切变得更加难以处理。

在 Dear ImGui 的特定情况下,您可以使用“停靠”分支中的多视口功能,它本机支持在主视口之外提取任何亲爱的 imgui 窗口并为您创建/管理 GLFW 窗口。这一切都由一个亲爱的 imgui 上下文处理,因此您可以例如从一个窗口拖放到另一个窗口。

于 2019-08-02T20:07:03.700 回答