Skip to content

Commit

Permalink
Columns: Lower overhead on column switches and switching to backgroun…
Browse files Browse the repository at this point in the history
…d channel (some stress tests in debug builds went 3->2 ms). (#125)

This change benefits Columns but was primarily made with Tables in mind.
  • Loading branch information
ocornut committed Jun 8, 2020
1 parent 84862ec commit 9b3ce49
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Other Changes:
flag was also set, and _OpenOnArrow is frequently set along with _OpenOnDoubleClick).
- TreeNode: Fixed bug where dragging a payload over a TreeNode() with either _OpenOnDoubleClick
or _OpenOnArrow would open the node. (#143)
- Columns: Lower overhead on column switches and switching to background channel (some of our stress
tests in debug builds went 3->2 ms). Benefits Columns but was primarily made with Tables in mind!
- Style: Added style.TabMinWidthForUnselectedCloseButton settings.
Set to 0.0f (default) to always make a close button appear on hover (same as Chrome, VS).
Set to FLT_MAX to only display a close button when selected (merely hovering is not enough).
Expand Down
7 changes: 6 additions & 1 deletion imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4167,7 +4167,12 @@ static void SetupDrawData(ImVector<ImDrawList*>* draw_lists, ImDrawData* draw_da
}
}

// 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.
// Push a clipping rectangle for both ImGui logic (hit-testing etc.) and low-level ImDrawList rendering.
// - When using this function it is sane to ensure that float are perfectly rounded to integer values,
// so that e.g. (int)(max.x-min.x) in user's render produce correct result.
// - If the code here changes, may need to update code of functions like NextColumn() and PushColumnClipRect():
// some frequently called functions which to modify both channels and clipping simultaneously tend to use a more
// specialized code path to added extraneous updates of the underlying ImDrawCmd.
void ImGui::PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect)
{
ImGuiWindow* window = GetCurrentWindow();
Expand Down
1 change: 1 addition & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ struct IMGUI_API ImRect
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.
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); }
bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; }
ImVec4 ToVec4() const { return ImVec4(Min.x, Min.y, Max.x, Max.y); }
};

// Helper: ImBitArray
Expand Down
29 changes: 25 additions & 4 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7549,6 +7549,11 @@ void ImGui::PushColumnsBackground()
ImGuiColumns* columns = window->DC.CurrentColumns;
if (columns->Count == 1)
return;

// Set cmd header ahead to avoid SetCurrentChannel+PushClipRect doing an unnecessary AddDrawCmd/Pop
//if (window->DrawList->Flags & ImDrawListFlags_Debug) IMGUI_DEBUG_LOG("PushColumnsBackground()\n");
window->DrawList->_CmdHeader.ClipRect = columns->HostClipRect.ToVec4();

columns->Splitter.SetCurrentChannel(window->DrawList, 0);
int cmd_size = window->DrawList->CmdBuffer.Size;
PushClipRect(columns->HostClipRect.Min, columns->HostClipRect.Max, false);
Expand All @@ -7562,6 +7567,12 @@ void ImGui::PopColumnsBackground()
ImGuiColumns* columns = window->DC.CurrentColumns;
if (columns->Count == 1)
return;

// Set cmd header ahead to avoid SetCurrentChannel+PushClipRect doing an unnecessary AddDrawCmd/Pop
//if (window->DrawList->Flags & ImDrawListFlags_Debug) IMGUI_DEBUG_LOG("PopColumnsBackground()\n");
ImVec4 pop_clip_rect = window->DrawList->_ClipRectStack.Data[window->DrawList->_ClipRectStack.Size - 2];
window->DrawList->_CmdHeader.ClipRect = pop_clip_rect;

columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1);
PopClipRect();
}
Expand Down Expand Up @@ -7684,11 +7695,22 @@ void ImGui::NextColumn()
return;
}
PopItemWidth();
PopClipRect();

// Next column
if (++columns->Current == columns->Count)
columns->Current = 0;

// As a small optimization, to avoid doing PopClipRect() + SetCurrentChannel() + PushClipRect()
// (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them),
// We use a shortcut: we override ClipRect in window and drawlist's CmdHeader + SetCurrentChannel().
ImGuiColumnData* column = &columns->Columns[columns->Current];
window->ClipRect = column->ClipRect;
window->DrawList->_CmdHeader.ClipRect = column->ClipRect.ToVec4();
//PopClipRect();

const float column_padding = g.Style.ItemSpacing.x;
columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y);
if (++columns->Current < columns->Count)
if (columns->Current > 0)
{
// Columns 1+ ignore IndentX (by canceling it out)
// FIXME-COLUMNS: Unnecessary, could be locked?
Expand All @@ -7701,15 +7723,14 @@ void ImGui::NextColumn()
// Column 0 honor IndentX
window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f);
columns->Splitter.SetCurrentChannel(window->DrawList, 1);
columns->Current = 0;
columns->LineMinY = columns->LineMaxY;
}
window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);
window->DC.CursorPos.y = columns->LineMinY;
window->DC.CurrLineSize = ImVec2(0.0f, 0.0f);
window->DC.CurrLineTextBaseOffset = 0.0f;

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

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

0 comments on commit 9b3ce49

Please sign in to comment.