Skip to content

Commit da32fb9

Browse files
committed
imgui_threaded_rendering: Added v0.10
Extracted from ocornut/imgui#1860
1 parent b89dbdd commit da32fb9

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Officially maintained small extensions for Dear ImGui:
44
- [Memory Editor](#imgui_memory_editor)
55
- [Multi-Context Compositor](#imgui_multicontext_compositor)
6+
- [Threaded Rendering](#imgui_threaded_rendering)
67

78
<BR>You also can find many useful third-party snippets here: https://github.com/ocornut/imgui/wiki/Useful-Extensions
89

@@ -32,3 +33,9 @@ When using and displaying multiple contexts simultaneously (e.g. Update vs Rende
3233
- Allow drag and drop between contexts
3334

3435
![multi_context_compositor.gif](https://github.com/user-attachments/assets/220a9469-db15-419a-8f29-3e0bf7025c84)
36+
37+
# imgui_threaded_rendering
38+
39+
https://github.com/ocornut/imgui_club/tree/main/imgui_threaded_rendering
40+
41+
Helper to take a snapshot of ImDrawData in order to render it later.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Helper for staged/multi-threaded rendering v0.10, for Dear ImGui
2+
// Get latest version at http://www.github.com/ocornut/imgui_club
3+
// Licensed under The MIT License (MIT)
4+
5+
// Based on a discussion at https://github.com/ocornut/imgui/issues/1860#issuecomment-1927630727
6+
7+
// CHANGELOG:
8+
// - v0.10: (2025/04/30): initial version. Not well tested.
9+
10+
// Usage:
11+
/*
12+
// Storage. Keep persistent as we reuse buffers across frames.
13+
static ImDrawDataSnapshot snapshot;
14+
15+
// [Update thread] Take a snapshot of the ImDrawData
16+
snapshot.SnapUsingSwap(ImGui::GetDrawData(), ImGui::GetTime());
17+
18+
// [Render thread] Render later
19+
ImGui_ImplDX11_RenderDrawData(&snapshot.DrawData);
20+
*/
21+
22+
// FIXME: Could store an ID in ImDrawList to make this easier for user.
23+
#include "imgui_internal.h" // ImPool<>, ImHashData
24+
25+
//-----------------------------------------------------------------------------
26+
// ImDrawDataSnapshot - HEADERS
27+
//-----------------------------------------------------------------------------
28+
29+
struct ImDrawDataSnapshotEntry
30+
{
31+
ImDrawList* SrcCopy = NULL; // Drawlist owned by main context
32+
ImDrawList* OurCopy = NULL; // Our copy
33+
double LastUsedTime = 0.0;
34+
};
35+
36+
struct ImDrawDataSnapshot
37+
{
38+
// Members
39+
ImDrawData DrawData;
40+
ImPool<ImDrawDataSnapshotEntry> Cache;
41+
float MemoryCompactTimer = 20.0f; // Discard unused data after 20 seconds
42+
43+
// Functions
44+
~ImDrawDataSnapshot() { Clear(); }
45+
void Clear();
46+
void SnapUsingSwap(ImDrawData* src, double current_time); // Efficient snapshot by swapping data, meaning "src" is unusable.
47+
//void SnapUsingCopy(ImDrawData* src, double current_time); // Deep-copy snapshop. Probably not needed.
48+
49+
// Internals
50+
ImGuiID GetDrawListID(ImDrawList* src_list) { return ImHashData(&src_list, sizeof(src_list)); } // Hash pointer
51+
ImDrawDataSnapshotEntry* GetOrAddEntry(ImDrawList* src_list) { return Cache.GetOrAddByKey(GetDrawListID(src_list)); }
52+
};
53+
54+
//-----------------------------------------------------------------------------
55+
// ImDrawDataSnapshot - IMPLEMENTATION
56+
//-----------------------------------------------------------------------------
57+
58+
void ImDrawDataSnapshot::Clear()
59+
{
60+
for (int n = 0; n < Cache.GetMapSize(); n++)
61+
if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
62+
IM_DELETE(entry->OurCopy);
63+
Cache.Clear();
64+
DrawData.Clear();
65+
}
66+
67+
void ImDrawDataSnapshot::SnapUsingSwap(ImDrawData* src, double current_time)
68+
{
69+
ImDrawData* dst = &DrawData;
70+
IM_ASSERT(src != dst && src->Valid);
71+
72+
// Copy all fields except CmdLists[]
73+
ImVector<ImDrawList*> backup_draw_list;
74+
backup_draw_list.swap(src->CmdLists);
75+
IM_ASSERT(src->CmdLists.Data == NULL);
76+
*dst = *src;
77+
backup_draw_list.swap(src->CmdLists);
78+
79+
// Swap and mark as used
80+
for (ImDrawList* src_list : src->CmdLists)
81+
{
82+
ImDrawDataSnapshotEntry* entry = GetOrAddEntry(src_list);
83+
if (entry->OurCopy == NULL)
84+
{
85+
entry->SrcCopy = src_list;
86+
entry->OurCopy = IM_NEW(ImDrawList)(src_list->_Data);
87+
}
88+
IM_ASSERT(entry->SrcCopy == src_list);
89+
entry->SrcCopy->CmdBuffer.swap(entry->OurCopy->CmdBuffer); // Cheap swap
90+
entry->SrcCopy->IdxBuffer.swap(entry->OurCopy->IdxBuffer);
91+
entry->SrcCopy->VtxBuffer.swap(entry->OurCopy->VtxBuffer);
92+
entry->SrcCopy->CmdBuffer.reserve(entry->OurCopy->CmdBuffer.Capacity); // Preserve bigger size to avoid reallocs for two consecutive frames
93+
entry->SrcCopy->IdxBuffer.reserve(entry->OurCopy->IdxBuffer.Capacity);
94+
entry->SrcCopy->VtxBuffer.reserve(entry->OurCopy->VtxBuffer.Capacity);
95+
entry->LastUsedTime = current_time;
96+
dst->CmdLists.push_back(entry->OurCopy);
97+
}
98+
99+
// Cleanup unused data
100+
const double gc_threshold = current_time - MemoryCompactTimer;
101+
for (int n = 0; n < Cache.GetMapSize(); n++)
102+
if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
103+
{
104+
if (entry->LastUsedTime > gc_threshold)
105+
continue;
106+
IM_DELETE(entry->OurCopy);
107+
Cache.Remove(GetDrawListID(entry->SrcCopy), entry);
108+
}
109+
};
110+
111+
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)
⌂ 换网址