Skip to content

Double-buffered ImDrawData #1860

Open
Open

Description

@mpvsv

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

Activity

  1. ocornut commented on Jun 5, 2018

    @ocornut
    Owner
  2. gviot commented on Jun 17, 2019

    @gviot

    Hey there,

    I wanted to share a few things:

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

    2. 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();
    	}
    }
    
    1. 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:
    ImDrawList* ImDrawList::CloneOutput() const
    {
        ImDrawList* dst = IM_NEW(ImDrawList(NULL));
        ...
    }
    

    The NULL argument causes a crash when the constructor calls Clear so I changed it to

    ImDrawList* ImDrawList::CloneOutput() const
    {
        ImDrawList* dst = IM_NEW(ImDrawList(_Data));
        ...
    }
    
  3. ocornut commented on Jun 17, 2019

    @ocornut
    Owner

    @gviot For now I have pushed the fix for the issue described in (3).

  4. ocornut commented on Feb 5, 2024

    @ocornut
    Owner

    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 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<>, ImHashData
    
    struct ImDrawDataSnapshotEntry
    {
        ImDrawList*     SrcCopy = NULL;     // Drawlist owned by main context
        ImDrawList*     OurCopy = NULL;     // Our copy
        double          LastUsedTime = 0.0;
    };
    
    struct ImDrawDataSnapshot
    {
        // Members
        ImDrawData                      DrawData;
        ImPool<ImDrawDataSnapshotEntry> Cache;
        float                           MemoryCompactTimer = 20.0f; // Discard unused data after 20 seconds
    
        // Functions
        ~ImDrawDataSnapshot()           { Clear(); }
        void                            Clear();
        void                            SnapUsingSwap(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) { return ImHashData(&src_list, sizeof(src_list)); }     // Hash pointer
        ImDrawDataSnapshotEntry*        GetOrAddEntry(ImDrawList* src_list) { return Cache.GetOrAddByKey(GetDrawListID(src_list)); }
    };
    
    void ImDrawDataSnapshot::Clear()
    {
        for (int n = 0; n < Cache.GetMapSize(); n++)
            if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
                IM_DELETE(entry->OurCopy);
        Cache.Clear();
        DrawData.Clear();
    }
    
    void ImDrawDataSnapshot::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 used
        for (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 data
        const double 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:

            static ImDrawData snapshot;
            ImDrawData* draw_data_to_render = ImGui::GetDrawData();
    
            if (ImGui::IsKeyPressed(ImGuiKey_1))
            {
                snapshot.SnapUsingSwap(ImGui::GetDrawData(), ImGui::GetTime());
                draw_data_to_render = &snapshot.DrawData;
            }
            else if (ImGui::IsKeyDown(ImGuiKey_2))
            {
                draw_data_to_render = &snapshot.DrawData;
            }
    
            ImGui_ImplDX11_RenderDrawData(draw_data_to_render);
  5. pozemka commented on Jun 25, 2024

    @pozemka

    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.

        // Internals
        ImGuiID                         GetDrawListID(ImDrawList* src_list) { return ImHashData(&src_list, sizeof(src_list)); }     // Hash pointer

    Isn't sizeof supposed to be called on the structure instead of the pointer?

    return ImHashData(&src_list, sizeof(*src_list));
  6. GamingMinds-DanielC commented on Jun 25, 2024

    @GamingMinds-DanielC
    Contributor

    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.

  7. added a commit that references this issue on Apr 30, 2025
  8. ocornut commented on Apr 30, 2025

    @ocornut
    Owner
  9. amuTBKT commented on Oct 17, 2025

    @amuTBKT

    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) :?

    Regards!

  10. amuTBKT commented on Oct 18, 2025

    @amuTBKT

    I've had some success including frame index when calculating ImDrawList hash. This is to avoid consecutive frames from touching the same draw lists.

    return ImHashData(&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 later
    ImDrawData DrawData_RT = snapshot.DrawData
    
    // [Render thread] Render later
    ImGui_ImplDX11_RenderDrawData(&DrawData_RT); 
  11. ocornut commented on Oct 29, 2025

    @ocornut
    Owner

    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.

    e.g.

    ImDrawDataSnapshot* snapshot_w = &snapshots[frame_index & 1];

    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)

  12. amuTBKT commented on Oct 29, 2025

    @amuTBKT

    That makes sense, thanks for the clarification.
    The usage comment in the file made it look like ImDrawDataSnapshot handles buffering internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址