-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multithreading. ImGui lags behind #6597
Comments
ImGui doesn't lag behind in the way you think. Frames don't get queued up for rendering. If you loop through the main thread twice before getting a lock in the render thread, a frame gets skipped. If you loop through the render thread twice before getting a lock in the main thread, a frame is rendered twice. ImGui just doesn't handle multithreading at all, proper synchronization would be your responsibility. You could do that f.e. with two semaphores. The main thread acquires a semaphore M with an initial count of 1, does its thing and releases a semaphore R with an initial count of 0. The render thread acquires R and once it is done with the render data releases M. You can accomplish the same with condition variables or waitable events, that's entirely up to you. But all of that is basically just serialized processing split into two threads. |
Thanks for the clarification. I thought one mutex is enough, since at any give point in time ImGui is either being rendered to the screen on RT, or being updated on MT (by ImGui calls) |
I believe none of this discussion is related to what Dear ImGui is doing or not doing. We provide you with ImDrawData. Whereas you render it immediately or later, and whether your graphic engine use a queue or any other mechanism and not really within the scope of Dear ImGui. Every example we provide calls a backend's RenderDrawData for every time I'm closing as this doesn't have to do with Dear ImGui, but feel free to discuss this here if needed. |
Note that as the buffers returned by While we provide a I'm going to work on that. |
Ok, thanks for the help! |
…DrawData itself. Faclitate user-manipulation of the array (#6406, #4879, #1878) + deep swap. (#6597, #6475, #6167, #5776, #5109, #4763, #3515, #1860) + Metrics: avoid misleadingly iterating all layers of DrawDataBuilder as everything is flattened into Layers[0] at this point. # Conflicts: # imgui.cpp # imgui_internal.h
I pushed some changes (c649aca + 1a9ddd2 + dbeeeae) outlined here which should make it easier to perform a deep-swap (instead of a deep-copy) of a ImDrawData is desired for multi-threaded rendering: |
@IceLuna how you implement multithread in imgui. i met many problems when i do this. if so,please show me your code. |
@1623275623 if you only needed to use ImDrawList from another thread, check out this solution |
@1623275623 Basically, you need one mutex that will prevent running ImGui code concurrently (as shown on the attachment).
I'm not sure if it'll work if graphics API doesn't support multithreading. @sakiodre I don't need something like that just to render ImGui in RT. At least if Vulkan is used :) |
I have posted a |
Version/Branch of Dear ImGui:
Version: 1.89.4 WIP
Branch: docking
My Issue/Question:
Hi! I have a render thread that does all the rendering and my problem is that ImGui UI lags behind.
These frame numbers are just static local variables that increment each time it prints. So as you can see
begin frame
was called 726 times, andRender draw data
- 724 times. I think that might be the problem.If I wait for the render thread to finish before calling
BeginFrame
, everything seems fineSo, does the ImGui render previous frames that weren't rendered yet? If so, is there a way to make it render UI using the data from the most recent frame (the data that was collected during
ImGui Begin Frame #726
)?I couldn't find any information about that and digging through the ImGui code didn't help yet.
Thanks!
Screenshots/Video
Here's the code that each thread executes for ImGui
The text was updated successfully, but these errors were encountered: