From 64d8d302fb379dd9d7fbe5475a32bd6d61004db5 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 10 Jun 2020 19:16:06 +0200 Subject: [PATCH] ImDrawList: Fixed VtxOffset change leading to unnecessary leading empty ImDrawCmd in certain cases. --- imgui.h | 1 + imgui_draw.cpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/imgui.h b/imgui.h index 3f25e81e..4de17899 100644 --- a/imgui.h +++ b/imgui.h @@ -2075,6 +2075,7 @@ struct ImDrawList IMGUI_API void _PopUnusedDrawCmd(); IMGUI_API void _OnChangedClipRect(); IMGUI_API void _OnChangedTextureID(); + IMGUI_API void _OnChangedVtxOffset(); }; // All draw data to render a Dear ImGui frame diff --git a/imgui_draw.cpp b/imgui_draw.cpp index 94f223ba..5b0a9855 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -514,6 +514,21 @@ void ImDrawList::_OnChangedTextureID() curr_cmd->TextureId = _CmdHeader.TextureId; } +void ImDrawList::_OnChangedVtxOffset() +{ + // We don't need to compare curr_cmd->VtxOffset != _CmdHeader.VtxOffset because we know it'll be different at the time we call this. + _VtxCurrentIdx = 0; + ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + IM_ASSERT(curr_cmd->VtxOffset != _CmdHeader.VtxOffset); + if (curr_cmd->ElemCount != 0) + { + AddDrawCmd(); + return; + } + IM_ASSERT(curr_cmd->UserCallback == NULL); + curr_cmd->VtxOffset = _CmdHeader.VtxOffset; +} + // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) void ImDrawList::PushClipRect(ImVec2 cr_min, ImVec2 cr_max, bool intersect_with_current_clip_rect) { @@ -570,8 +585,7 @@ void ImDrawList::PrimReserve(int idx_count, int vtx_count) if (sizeof(ImDrawIdx) == 2 && (_VtxCurrentIdx + vtx_count >= (1 << 16)) && (Flags & ImDrawListFlags_AllowVtxOffset)) { _CmdHeader.VtxOffset = VtxBuffer.Size; - _VtxCurrentIdx = 0; - AddDrawCmd(); + _OnChangedVtxOffset(); } ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1];