|
| 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