GLFW & ImGui:从主线程以外的线程创建 ImGui 控件

Rom*_*men 3 c++ multithreading glfw imgui

我正在将 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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

我该如何修复此问题以允许 ImGui 和这些窗口响应 GLFW 事件?

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

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

Rom*_*men 5

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

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

struct ImGuiContext;
extern thread_local ImGuiContext* threadCTX;
#define GImGui threadCTX
Run Code Online (Sandbox Code Playgroud)

我必须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;
Run Code Online (Sandbox Code Playgroud)

同样,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;
Run Code Online (Sandbox Code Playgroud)

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

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