Skip to content

Commit 9b3ce49

Browse files
committed
Columns: Lower overhead on column switches and switching to background channel (some stress tests in debug builds went 3->2 ms). (#125)
This change benefits Columns but was primarily made with Tables in mind.
1 parent 84862ec commit 9b3ce49

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

docs/CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Other Changes:
4646
flag was also set, and _OpenOnArrow is frequently set along with _OpenOnDoubleClick).
4747
- TreeNode: Fixed bug where dragging a payload over a TreeNode() with either _OpenOnDoubleClick
4848
or _OpenOnArrow would open the node. (#143)
49+
- Columns: Lower overhead on column switches and switching to background channel (some of our stress
50+
tests in debug builds went 3->2 ms). Benefits Columns but was primarily made with Tables in mind!
4951
- Style: Added style.TabMinWidthForUnselectedCloseButton settings.
5052
Set to 0.0f (default) to always make a close button appear on hover (same as Chrome, VS).
5153
Set to FLT_MAX to only display a close button when selected (merely hovering is not enough).

imgui.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -4167,7 +4167,12 @@ static void SetupDrawData(ImVector<ImDrawList*>* draw_lists, ImDrawData* draw_da
41674167
}
41684168
}
41694169

4170-
// When using this function it is sane to ensure that float are perfectly rounded to integer values, to that e.g. (int)(max.x-min.x) in user's render produce correct result.
4170+
// Push a clipping rectangle for both ImGui logic (hit-testing etc.) and low-level ImDrawList rendering.
4171+
// - When using this function it is sane to ensure that float are perfectly rounded to integer values,
4172+
// so that e.g. (int)(max.x-min.x) in user's render produce correct result.
4173+
// - If the code here changes, may need to update code of functions like NextColumn() and PushColumnClipRect():
4174+
// some frequently called functions which to modify both channels and clipping simultaneously tend to use a more
4175+
// specialized code path to added extraneous updates of the underlying ImDrawCmd.
41714176
void ImGui::PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect)
41724177
{
41734178
ImGuiWindow* window = GetCurrentWindow();

imgui_internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ struct IMGUI_API ImRect
435435
void ClipWithFull(const ImRect& r) { Min = ImClamp(Min, r.Min, r.Max); Max = ImClamp(Max, r.Min, r.Max); } // Full version, ensure both points are fully clipped.
436436
void Floor() { Min.x = IM_FLOOR(Min.x); Min.y = IM_FLOOR(Min.y); Max.x = IM_FLOOR(Max.x); Max.y = IM_FLOOR(Max.y); }
437437
bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; }
438+
ImVec4 ToVec4() const { return ImVec4(Min.x, Min.y, Max.x, Max.y); }
438439
};
439440

440441
// Helper: ImBitArray

imgui_widgets.cpp

+25-4
Original file line numberDiff line numberDiff line change
@@ -7549,6 +7549,11 @@ void ImGui::PushColumnsBackground()
75497549
ImGuiColumns* columns = window->DC.CurrentColumns;
75507550
if (columns->Count == 1)
75517551
return;
7552+
7553+
// Set cmd header ahead to avoid SetCurrentChannel+PushClipRect doing an unnecessary AddDrawCmd/Pop
7554+
//if (window->DrawList->Flags & ImDrawListFlags_Debug) IMGUI_DEBUG_LOG("PushColumnsBackground()\n");
7555+
window->DrawList->_CmdHeader.ClipRect = columns->HostClipRect.ToVec4();
7556+
75527557
columns->Splitter.SetCurrentChannel(window->DrawList, 0);
75537558
int cmd_size = window->DrawList->CmdBuffer.Size;
75547559
PushClipRect(columns->HostClipRect.Min, columns->HostClipRect.Max, false);
@@ -7562,6 +7567,12 @@ void ImGui::PopColumnsBackground()
75627567
ImGuiColumns* columns = window->DC.CurrentColumns;
75637568
if (columns->Count == 1)
75647569
return;
7570+
7571+
// Set cmd header ahead to avoid SetCurrentChannel+PushClipRect doing an unnecessary AddDrawCmd/Pop
7572+
//if (window->DrawList->Flags & ImDrawListFlags_Debug) IMGUI_DEBUG_LOG("PopColumnsBackground()\n");
7573+
ImVec4 pop_clip_rect = window->DrawList->_ClipRectStack.Data[window->DrawList->_ClipRectStack.Size - 2];
7574+
window->DrawList->_CmdHeader.ClipRect = pop_clip_rect;
7575+
75657576
columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1);
75667577
PopClipRect();
75677578
}
@@ -7684,11 +7695,22 @@ void ImGui::NextColumn()
76847695
return;
76857696
}
76867697
PopItemWidth();
7687-
PopClipRect();
7698+
7699+
// Next column
7700+
if (++columns->Current == columns->Count)
7701+
columns->Current = 0;
7702+
7703+
// As a small optimization, to avoid doing PopClipRect() + SetCurrentChannel() + PushClipRect()
7704+
// (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them),
7705+
// We use a shortcut: we override ClipRect in window and drawlist's CmdHeader + SetCurrentChannel().
7706+
ImGuiColumnData* column = &columns->Columns[columns->Current];
7707+
window->ClipRect = column->ClipRect;
7708+
window->DrawList->_CmdHeader.ClipRect = column->ClipRect.ToVec4();
7709+
//PopClipRect();
76887710

76897711
const float column_padding = g.Style.ItemSpacing.x;
76907712
columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y);
7691-
if (++columns->Current < columns->Count)
7713+
if (columns->Current > 0)
76927714
{
76937715
// Columns 1+ ignore IndentX (by canceling it out)
76947716
// FIXME-COLUMNS: Unnecessary, could be locked?
@@ -7701,15 +7723,14 @@ void ImGui::NextColumn()
77017723
// Column 0 honor IndentX
77027724
window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f);
77037725
columns->Splitter.SetCurrentChannel(window->DrawList, 1);
7704-
columns->Current = 0;
77057726
columns->LineMinY = columns->LineMaxY;
77067727
}
77077728
window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
77087729
window->DC.CursorPos.y = columns->LineMinY;
77097730
window->DC.CurrLineSize = ImVec2(0.0f, 0.0f);
77107731
window->DC.CurrLineTextBaseOffset = 0.0f;
77117732

7712-
PushColumnClipRect(columns->Current); // FIXME-COLUMNS: Could it be an overwrite?
7733+
//PushColumnClipRect(columns->Current);
77137734

77147735
// FIXME-COLUMNS: Share code with BeginColumns() - move code on columns setup.
77157736
float offset_0 = GetColumnOffset(columns->Current);

0 commit comments

Comments
 (0)