You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I run two primary threads in my application; one handles world update at 60hz while another renders frames at however fast it's able. The world update thread keeps two versions of the graphics state that the graphics thread interpolates. Is there support for this mode of operation now? If not, would it be simple to add? (Interpolation isn't important for the GUI side of things. I'd be fine having double-buffered DrawData.)
Hello,
It’s not supported yet but there is a CloneOutput function you can use (a little slower) to duplicate an ImDrawList.
For this and other features I would like to support double buffered ImDrawList per window, which would lead to double buffered ImDrawData.
I think a simple "double buffering" feature in imgui wouldn't cover every use case. ideally I would like to see some kind of configurable n-buffering or improvement of CloneOutput.
e.g. my engine (inspired a bit by "Parallelizing the Naughty Dog Engine Using Fibers") is multi threaded with N-world-frame-buffering (usually N = 10 in my case but configurable).
The renderer is double buffered and can skip world frames in order to catch up and reduce latency.
I have managed to make imgui mostly work in this context by storing the command lists to each world frame. When the renderer skips a world frame it also skips an imgui frame so I still have a few minor issues like invalid framerate value in imgui but nothing problematic.
In case it can help anyone, in order to save the data of each imgui frame i used both CloneOutput and this bit of code:
void set_imgui_data(ImDrawData* dat) {
if (m_imgui_data.CmdLists != nullptr) {
for (int i = 0; i < m_imgui_data.CmdListsCount; i++) {
delete m_imgui_data.CmdLists[i];
}
delete m_imgui_data.CmdLists;
m_imgui_data.CmdLists = nullptr;
}
m_imgui_data.Clear();
m_imgui_data = *dat;
m_imgui_data.CmdLists = new ImDrawList*[dat->CmdListsCount];
for (int i = 0; i < dat->CmdListsCount; i++) {
m_imgui_data.CmdLists[i] = dat->CmdLists[i]->CloneOutput();
}
}
Since i was on the docking branch, I have updated more often and currently CloneOutput crashes 100% when called, so I wanted to share my fix, hopefully it can be upstreamed so i don't have to refix it with every update.
The current code is:
Here's a suggested more optimal way to handle double buffering of ImDrawData.
Maybe people have been using CloneOutput() but it's not ideally efficient.
This implementation is a little more complex but allow avoiding a full copy of vertex data.
I wrote this on a whim and haven't tested it thoroughly. Would be interested in feedback.
I would consider making ImDrawDataSnapshot public API eventually, but I'm not sure I am fully grasp how multi-frames-in-flight custom backends would work with multi-viewports, and where would be the responsibility there.
Code
// Usage:// static ImDrawDataSnapshot snapshot; // Important: make persistent accross frames to reuse buffers.// snapshot.SnapUsingSwap(ImGui::GetDrawData(), ImGui::GetTime());// [...]// ImGui_ImplDX11_RenderDrawData(&snapshot.DrawData);// FIXME: Could store an ID in ImDrawList to make this easier for user.
#include"imgui_internal.h"// ImPool<>, ImHashDatastructImDrawDataSnapshotEntry
{
ImDrawList* SrcCopy = NULL; // Drawlist owned by main context
ImDrawList* OurCopy = NULL; // Our copydouble LastUsedTime = 0.0;
};
structImDrawDataSnapshot
{
// Members
ImDrawData DrawData;
ImPool<ImDrawDataSnapshotEntry> Cache;
float MemoryCompactTimer = 20.0f; // Discard unused data after 20 seconds// Functions~ImDrawDataSnapshot() { Clear(); }
voidClear();
voidSnapUsingSwap(ImDrawData* src, double current_time); // Efficient snapshot by swapping data, meaning "src_list" is unusable.//void SnapUsingCopy(ImDrawData* src, double current_time); // Deep-copy snapshop// Internals
ImGuiID GetDrawListID(ImDrawList* src_list) { returnImHashData(&src_list, sizeof(src_list)); } // Hash pointer
ImDrawDataSnapshotEntry* GetOrAddEntry(ImDrawList* src_list) { return Cache.GetOrAddByKey(GetDrawListID(src_list)); }
};
voidImDrawDataSnapshot::Clear()
{
for (int n = 0; n < Cache.GetMapSize(); n++)
if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
IM_DELETE(entry->OurCopy);
Cache.Clear();
DrawData.Clear();
}
voidImDrawDataSnapshot::SnapUsingSwap(ImDrawData* src, double current_time)
{
ImDrawData* dst = &DrawData;
IM_ASSERT(src != dst && src->Valid);
// Copy all fields except CmdLists[]
ImVector<ImDrawList*> backup_draw_list;
backup_draw_list.swap(src->CmdLists);
IM_ASSERT(src->CmdLists.Data == NULL);
*dst = *src;
backup_draw_list.swap(src->CmdLists);
// Swap and mark as usedfor (ImDrawList* src_list : src->CmdLists)
{
ImDrawDataSnapshotEntry* entry = GetOrAddEntry(src_list);
if (entry->OurCopy == NULL)
{
entry->SrcCopy = src_list;
entry->OurCopy = IM_NEW(ImDrawList)(src_list->_Data);
}
IM_ASSERT(entry->SrcCopy == src_list);
entry->SrcCopy->CmdBuffer.swap(entry->OurCopy->CmdBuffer); // Cheap swap
entry->SrcCopy->IdxBuffer.swap(entry->OurCopy->IdxBuffer);
entry->SrcCopy->VtxBuffer.swap(entry->OurCopy->VtxBuffer);
entry->SrcCopy->CmdBuffer.reserve(entry->OurCopy->CmdBuffer.Capacity); // Preserve bigger size to avoid reallocs for two consecutive frames
entry->SrcCopy->IdxBuffer.reserve(entry->OurCopy->IdxBuffer.Capacity);
entry->SrcCopy->VtxBuffer.reserve(entry->OurCopy->VtxBuffer.Capacity);
entry->LastUsedTime = current_time;
dst->CmdLists.push_back(entry->OurCopy);
}
// Cleanup unused dataconstdouble gc_threshold = current_time - MemoryCompactTimer;
for (int n = 0; n < Cache.GetMapSize(); n++)
if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
{
if (entry->LastUsedTime > gc_threshold)
continue;
IM_DELETE(entry->OurCopy);
Cache.Remove(GetDrawListID(entry->SrcCopy), entry);
}
};
Test bed, press 1 to capture current output, hold 2 to display it:
Here's a suggested more optimal way to handle double buffering of ImDrawData. Maybe people have been using CloneOutput() but it's not ideally efficient. This implementation is a little more complex but allow avoiding a full copy of vertex data.
Isn't sizeof supposed to be called on the structure instead of the pointer?
return ImHashData(&src_list, sizeof(*src_list));
I guess that's why that line has a comment "hash pointer", to make clear that it is indeed the pointer that is to be hashed. That also makes sense since you want an ID for the draw list, that shouldn't change if the contents of the draw list change. The pointer stays the same.
Btw, dangerous bug in your one-liner. You would be hashing the pointer and unspecified memory after where the pointer is stored, not the structure itself, but with the size of the structure. Could be an access violation, could be hashing the stack, could be hashing anything.
Hello, I tried integrating this setup in my plugin today but unsure how this serve as a double buffered setup :?
From what I can tell ImDrawDataSnapshot::SnapUsingSwap could touch the same DrawData and ImDrawList structures accessed by the rendering work? So by the time render thread gets a chance to issue draw commands, the draw data gets modified by the ImDrawDataSnapshot::SnapUsingSwap command called on the game thread (during next update) :?
I've had some success including frame index when calculating ImDrawList hash. This is to avoid consecutive frames from touching the same draw lists.
returnImHashData(&src_list, sizeof(src_list), (frame_index&0x1)); //0x1 for double buffered
Also shouldn't we copy the DrawData instead of using pointer in render callback? Just clarifying this as the example you provided is from imgui_examples which doesn't use multi threaded rendering.
// copy snapshot DrawData for passing to render thread laterImDrawDataDrawData_RT=snapshot.DrawData// [Render thread] Render laterImGui_ImplDX11_RenderDrawData(&DrawData_RT);
From what I can tell ImDrawDataSnapshot::SnapUsingSwap could touch the same DrawData and ImDrawList structures accessed by the rendering work? So by the time render thread gets a chance to issue draw commands, the draw data gets modified by the ImDrawDataSnapshot::SnapUsingSwap command called on the game thread (during next update) :?
For multi-threading you would indeed need to alternate between two ImDrawDataSnapshot (or more if you have multiple frames in flight). I guess the example at the top of imgui_threaded_rendering.h may be misleading that it doesn't reflect this. ImDrawDataSnapshot may also be useful in cases where threads are not involved, but it eventually got added into a file called imgui_threaded_rendering.h the example should reflect that better.
Meaning you don't copy the ImDrawData. Part of the point of ImDrawDataSnapshot design is that it should stabilize to zero allocations quickly after each growth/change.
Does that makes more sense, or am I missing something?
from imgui_examples which doesn't use multi threaded rendering.
I do not have a multi-threaded example, it would be good that I have a patch for one(preferably the win32+dx11 one). I believe #8597 aimed to offer one but I had some issues with it (I don't remember what haven't re-read that thread now)
Activity
ocornut commentedon Jun 5, 2018
gviot commentedon Jun 17, 2019
Hey there,
I wanted to share a few things:
I think a simple "double buffering" feature in imgui wouldn't cover every use case. ideally I would like to see some kind of configurable n-buffering or improvement of CloneOutput.
e.g. my engine (inspired a bit by "Parallelizing the Naughty Dog Engine Using Fibers") is multi threaded with N-world-frame-buffering (usually N = 10 in my case but configurable).
The renderer is double buffered and can skip world frames in order to catch up and reduce latency.
I have managed to make imgui mostly work in this context by storing the command lists to each world frame. When the renderer skips a world frame it also skips an imgui frame so I still have a few minor issues like invalid framerate value in imgui but nothing problematic.
In case it can help anyone, in order to save the data of each imgui frame i used both CloneOutput and this bit of code:
The current code is:
The NULL argument causes a crash when the constructor calls Clear so I changed it to
ImDrawList: Fixed CloneOutput() helper crashing. Also removed unneces…
ocornut commentedon Jun 17, 2019
@gviot For now I have pushed the fix for the issue described in (3).
ImDrawData: changed CmdLists from raw array to ImVector<> owned by Im…
Metrics: Fixed "Drawlists" section and per-viewport equivalent appear…
ocornut commentedon Feb 5, 2024
EDIT 2025: CODE MOVED TO https://github.com/ocornut/imgui_club/tree/main/imgui_threaded_rendering <-- please use this instead of the coed on this post.
Here's a suggested more optimal way to handle double buffering of ImDrawData.
Maybe people have been using
CloneOutput()but it's not ideally efficient.This implementation is a little more complex but allow avoiding a full copy of vertex data.
I wrote this on a whim and haven't tested it thoroughly. Would be interested in feedback.
I would consider making
ImDrawDataSnapshotpublic API eventually, but I'm not sure I am fully grasp how multi-frames-in-flight custom backends would work with multi-viewports, and where would be the responsibility there.Code
Test bed, press 1 to capture current output, hold 2 to display it:
pozemka commentedon Jun 25, 2024
Isn't sizeof supposed to be called on the structure instead of the pointer?
GamingMinds-DanielC commentedon Jun 25, 2024
I guess that's why that line has a comment "hash pointer", to make clear that it is indeed the pointer that is to be hashed. That also makes sense since you want an ID for the draw list, that shouldn't change if the contents of the draw list change. The pointer stays the same.
Btw, dangerous bug in your one-liner. You would be hashing the pointer and unspecified memory after where the pointer is stored, not the structure itself, but with the size of the structure. Could be an access violation, could be hashing the stack, could be hashing anything.
imgui_threaded_rendering: Added v0.10
ocornut commentedon Apr 30, 2025
I'm pushing this code here
https://github.com/ocornut/imgui_club/tree/main/imgui_threaded_rendering
DrawList: fixed CloneOutput() taking a copy of the ImDrawListSharedDa…
DrawList: fixed CloneOutput() taking a copy of the ImDrawListSharedDa…
amuTBKT commentedon Oct 17, 2025
Hello, I tried integrating this setup in my plugin today but unsure how this serve as a double buffered setup :?
From what I can tell
ImDrawDataSnapshot::SnapUsingSwapcould touch the sameDrawDataandImDrawListstructures accessed by the rendering work? So by the time render thread gets a chance to issue draw commands, the draw data gets modified by theImDrawDataSnapshot::SnapUsingSwapcommand called on the game thread (during next update) :?Regards!
amuTBKT commentedon Oct 18, 2025
I've had some success including frame index when calculating ImDrawList hash. This is to avoid consecutive frames from touching the same draw lists.
Also shouldn't we copy the DrawData instead of using pointer in render callback? Just clarifying this as the example you provided is from imgui_examples which doesn't use multi threaded rendering.
ocornut commentedon Oct 29, 2025
For multi-threading you would indeed need to alternate between two
ImDrawDataSnapshot(or more if you have multiple frames in flight). I guess the example at the top ofimgui_threaded_rendering.hmay be misleading that it doesn't reflect this.ImDrawDataSnapshotmay also be useful in cases where threads are not involved, but it eventually got added into a file calledimgui_threaded_rendering.hthe example should reflect that better.e.g.
ImDrawDataSnapshot* snapshot_w = &snapshots[frame_index & 1];Meaning you don't copy the ImDrawData. Part of the point of
ImDrawDataSnapshotdesign is that it should stabilize to zero allocations quickly after each growth/change.Does that makes more sense, or am I missing something?
I do not have a multi-threaded example, it would be good that I have a patch for one(preferably the win32+dx11 one). I believe #8597 aimed to offer one but I had some issues with it (I don't remember what haven't re-read that thread now)
amuTBKT commentedon Oct 29, 2025
That makes sense, thanks for the clarification.
The usage comment in the file made it look like
ImDrawDataSnapshothandles buffering internally.imgui_threaded_rendering: Amend comments.