From 4bae769050e888f79f513b9bcb8bc471f5fb18ca Mon Sep 17 00:00:00 2001 From: 4DBug <115124145+4DBug@users.noreply.github.com> Date: Wed, 21 Oct 2026 02:38:26 -0500 Subject: [PATCH] Library: PlotEx() without per-point temporaries (scalar tp0/tp1 and ImLerp, two scratch corners only read by AddLine()/AddRectFilled(), scalar frame/inner/total rects) and @native; RenderColorRectWithAlphaCheckerboard() with scratch cell corners and @native; @native ShadeVertsLinearColorGradientKeepAlpha() and _PathArcToN() (color picker hue wheel) plots scene headless --roblox (interleaved A/B): lib lowq ~0.35 -> ~0.27 ms/frame; colorpicker lib lowq ~0.43 -> ~0.38 ms/frame. Draw data unchanged (STRESS2HASH of all scenes, draw hashes). --- imgui_draw.lua | 9 ++++++++- imgui_widgets.lua | 32 ++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/imgui_draw.lua b/imgui_draw.lua index 31ba9f9..1a9f871 100644 --- a/imgui_draw.lua +++ b/imgui_draw.lua @@ -349,6 +349,7 @@ end --- @param gradient_p1 ImVec2 --- @param col0 ImU32 --- @param col1 ImU32 +@native function ImGui.ShadeVertsLinearColorGradientKeepAlpha(draw_list, vert_start_idx, vert_end_idx, gradient_p0, gradient_p1, col0, col1) local gradient_extent = gradient_p1 - gradient_p0 local gradient_inv_length2 = 1.0 / ImLengthSqr(gradient_extent) @@ -7241,6 +7242,7 @@ end --- @param a_min float --- @param a_max float --- @param num_segments int +@native function MT.ImDrawList:_PathArcToN(center, radius, a_min, a_max, num_segments) if not (radius >= 0.25 * self._FringeScale) then -- [Port] !IM_HAS_ROUNDING(radius) self._Path:push_back(center) @@ -7758,6 +7760,8 @@ function ImGui.CalcRoundingFlagsForRectInRect(r_in, r_outer, threshold) return flags end +local Checkerboard_cell_min, Checkerboard_cell_max = ImVec2(), ImVec2() + -- Helper for ColorPicker4() -- NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that. -- Spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding altogether. @@ -7770,6 +7774,7 @@ end --- @param grid_off ImVec2 --- @param rounding? float --- @param flags? ImDrawFlags +@native function ImGui.RenderColorRectWithAlphaCheckerboard(draw_list, p_min, p_max, col, alpha, grid_step, grid_off, rounding, flags) if rounding == nil then rounding = 0.0 end if flags == nil then flags = 0 end @@ -7820,7 +7825,9 @@ function ImGui.RenderColorRectWithAlphaCheckerboard(draw_list, p_min, p_max, col end local col_bg if dual_layer then col_bg = col_bg2 elseif (yi + xi) % 2 == 1 then col_bg = col_bg2 else col_bg = col_bg1 end - draw_list:AddRectFilled(ImVec2(x1, y1), ImVec2(x2, y2), col_bg, rounding, cell_flags) + local cell_min, cell_max = Checkerboard_cell_min, Checkerboard_cell_max -- [Luau] scratch: AddRectFilled() only reads them + cell_min.x = x1; cell_min.y = y1; cell_max.x = x2; cell_max.y = y2 + draw_list:AddRectFilled(cell_min, cell_max, col_bg, rounding, cell_flags) end x = x + x_step xi = xi + 1 diff --git a/imgui_widgets.lua b/imgui_widgets.lua index b251b56..bd056ac 100644 --- a/imgui_widgets.lua +++ b/imgui_widgets.lua @@ -10120,6 +10120,8 @@ end -- Consider using ImPlot (https://github.com/epezent/implot) which is much better! ------------------------------------------------------------------------------- +local PlotEx_pos0, PlotEx_pos1 = ImVec2(), ImVec2() -- [Luau] scratch line/bar corners + --- @param plot_type ImGuiPlotType --- @param label string --- @param values_getter fun(data?: table, idx: int): float @@ -10131,6 +10133,7 @@ end --- @param scale_max float --- @param size_arg ImVec2 --- @return int +@native function ImGui.PlotEx(plot_type, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, size_arg) local g = GImGui local window = ImGui.GetCurrentWindow() @@ -10145,9 +10148,10 @@ function ImGui.PlotEx(plot_type, label, values_getter, data, values_count, value local label_size = ImGui.CalcTextSize(label, label_end, false) local frame_size = ImGui.CalcItemSize(size_arg, ImGui.CalcItemWidth(), label_size.y + style.FramePadding.y * 2.0) - local frame_bb = ImRect(window.DC.CursorPos, window.DC.CursorPos + frame_size) - local inner_bb = ImRect(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding) - local total_bb = ImRect(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0 and style.ItemInnerSpacing.x + label_size.x or 0.0, 0)) + local cursor, pad = window.DC.CursorPos, style.FramePadding -- [Luau] scalar forms: no temporary ImVec2s + local frame_bb = ImRect(cursor.x, cursor.y, cursor.x + frame_size.x, cursor.y + frame_size.y) + local inner_bb = ImRect(frame_bb.Min.x + pad.x, frame_bb.Min.y + pad.y, frame_bb.Max.x - pad.x, frame_bb.Max.y - pad.y) + local total_bb = ImRect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Max.x + (label_size.x > 0.0 and style.ItemInnerSpacing.x + label_size.x or 0.0), frame_bb.Max.y + 0) ImGui.ItemSize(total_bb, style.FramePadding.y) if not ImGui.ItemAdd(total_bb, id, frame_bb, ImGuiItemFlags.NoNav) then return -1 @@ -10203,39 +10207,43 @@ function ImGui.PlotEx(plot_type, label, values_getter, data, values_count, value local v0 = values_getter(data, (0 + values_offset) % values_count + 1) local t0 = 0.0 - local tp0 = ImVec2(t0, 1.0 - ImSaturate((v0 - scale_min) * inv_scale)) -- Point in the normalized space of our target rectangle + local tp0_x, tp0_y = t0, 1.0 - ImSaturate((v0 - scale_min) * inv_scale) -- Point in the normalized space of our target rectangle -- [Luau] scalar tp0/tp1 local histogram_zero_line_t = (scale_min * scale_max < 0.0) and (1 + scale_min * inv_scale) or (scale_min < 0.0 and 0.0 or 1.0) -- Where does the zero line stands local col_base = ImGui.GetColorU32((plot_type == ImGuiPlotType.Lines) and ImGuiCol.PlotLines or ImGuiCol.PlotHistogram) local col_hovered = ImGui.GetColorU32((plot_type == ImGuiPlotType.Lines) and ImGuiCol.PlotLinesHovered or ImGuiCol.PlotHistogramHovered) + local inner_min_x, inner_min_y, inner_max_x, inner_max_y = inner_bb.Min.x, inner_bb.Min.y, inner_bb.Max.x, inner_bb.Max.y + local draw_list = window.DrawList for _ = 0, res_w - 1 do local t1 = t0 + t_step local v1_idx = math.floor(t0 * item_count + 0.5) + 1 IM_ASSERT(v1_idx >= 1 and v1_idx <= values_count) local v1 = values_getter(data, (v1_idx - 1 + values_offset + 1) % values_count + 1) - local tp1 = ImVec2(t1, 1.0 - ImSaturate((v1 - scale_min) * inv_scale)) + local tp1_x, tp1_y = t1, 1.0 - ImSaturate((v1 - scale_min) * inv_scale) -- NB: Draw calls are merged together by the DrawList system. Still, we should render our batch are lower level to save a bit of CPU. - local pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0) - local pos1 + -- [Luau] ImLerp() on scalars into scratch vectors, only read by AddLine()/AddRectFilled() + local pos0, pos1 = PlotEx_pos0, PlotEx_pos1 + pos0.x = inner_min_x + (inner_max_x - inner_min_x) * tp0_x; pos0.y = inner_min_y + (inner_max_y - inner_min_y) * tp0_y + pos1.x = inner_min_x + (inner_max_x - inner_min_x) * tp1_x if plot_type == ImGuiPlotType.Lines then - pos1 = ImLerp(inner_bb.Min, inner_bb.Max, tp1) + pos1.y = inner_min_y + (inner_max_y - inner_min_y) * tp1_y else - pos1 = ImLerp(inner_bb.Min, inner_bb.Max, ImVec2(tp1.x, histogram_zero_line_t)) + pos1.y = inner_min_y + (inner_max_y - inner_min_y) * histogram_zero_line_t end if plot_type == ImGuiPlotType.Lines then - window.DrawList:AddLine(pos0, pos1, idx_hovered == v1_idx and col_hovered or col_base) + draw_list:AddLine(pos0, pos1, idx_hovered == v1_idx and col_hovered or col_base) elseif plot_type == ImGuiPlotType.Histogram then if pos1.x >= pos0.x + 2.0 then pos1.x = pos1.x - 1.0 end - window.DrawList:AddRectFilled(pos0, pos1, idx_hovered == v1_idx and col_hovered or col_base) + draw_list:AddRectFilled(pos0, pos1, idx_hovered == v1_idx and col_hovered or col_base) end t0 = t1 - tp0 = tp1 + tp0_x, tp0_y = tp1_x, tp1_y end end