Texture-based thick lines: Minor tweaks and rename toward merging in master.
diff --git a/imgui.cpp b/imgui.cpp
index b9c300f..2129d00 100644
--- a/imgui.cpp
+++ b/imgui.cpp
@@ -935,7 +935,7 @@
DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows.
MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later.
AntiAliasedLines = true; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU.
- AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Requires back-end to render with bilinear filtering.
+ AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require back-end to render with bilinear filtering.
AntiAliasedFill = true; // Enable anti-aliased filled shapes (rounded rectangles, circles, etc.).
CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.
CircleSegmentMaxError = 1.60f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry.
@@ -3689,7 +3689,7 @@
g.DrawListSharedData.InitialFlags = ImDrawListFlags_None;
if (g.Style.AntiAliasedLines)
g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLines;
- if (g.Style.AntiAliasedLinesUseTex && !(g.Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoAntiAliasedLines))
+ if (g.Style.AntiAliasedLinesUseTex && !(g.Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoLines))
g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLinesUseTex;
if (g.Style.AntiAliasedFill)
g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedFill;
@@ -6161,7 +6161,7 @@
ImFontAtlas* atlas = g.Font->ContainerAtlas;
g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel;
- g.DrawListSharedData.TexUvAALines = atlas->TexUvAALines;
+ g.DrawListSharedData.TexUvLines = atlas->TexUvLines;
g.DrawListSharedData.Font = g.Font;
g.DrawListSharedData.FontSize = g.FontSize;
}
diff --git a/imgui.h b/imgui.h
index 5e86bae..5ccc54a 100644
--- a/imgui.h
+++ b/imgui.h
@@ -1439,7 +1439,7 @@
ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly!
float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later.
bool AntiAliasedLines; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU.
- bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Requires back-end to render with bilinear filtering.
+ bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Require back-end to render with bilinear filtering.
bool AntiAliasedFill; // Enable anti-aliased edges around filled shapes (rounded rectangles, circles, etc.). Disable if you are really tight on CPU/GPU.
float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.
float CircleSegmentMaxError; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry.
@@ -1897,9 +1897,9 @@
// Hold a series of drawing commands. The user provides a renderer for ImDrawData which essentially contains an array of ImDrawList.
//-----------------------------------------------------------------------------
-// The maximum line width to bake anti-aliased textures for. Build atlas with ImFontAtlasFlags_NoAALines to disable baking.
-#ifndef IM_DRAWLIST_TEX_AA_LINES_WIDTH_MAX
-#define IM_DRAWLIST_TEX_AA_LINES_WIDTH_MAX (63)
+// The maximum line width to bake anti-aliased textures for. Build atlas with ImFontAtlasFlags_NoLines to disable baking.
+#ifndef IM_DRAWLIST_TEX_LINES_WIDTH_MAX
+#define IM_DRAWLIST_TEX_LINES_WIDTH_MAX (63)
#endif
// ImDrawCallback: Draw callbacks for advanced uses [configurable type: override in imconfig.h]
@@ -1998,14 +1998,12 @@
ImDrawCornerFlags_All = 0xF // In your function calls you may use ~0 (= all bits sets) instead of ImDrawCornerFlags_All, as a convenience
};
-#define IMGUI_HAS_TEXLINES 1
-
enum ImDrawListFlags_
{
ImDrawListFlags_None = 0,
ImDrawListFlags_AntiAliasedLines = 1 << 0, // Enable anti-aliased lines/borders (*2 the number of triangles for 1.0f wide line or lines thin enough to be drawn using textures, otherwise *3 the number of triangles)
- ImDrawListFlags_AntiAliasedFill = 1 << 1, // Enable anti-aliased edge around filled shapes (rounded rectangles, circles).
- ImDrawListFlags_AntiAliasedLinesUseTex = 1 << 2, // Should anti-aliased lines be drawn using textures where possible?
+ ImDrawListFlags_AntiAliasedLinesUseTex = 1 << 1, // Enable anti-aliased lines/borders using textures when possible. Require back-end to render with bilinear filtering.
+ ImDrawListFlags_AntiAliasedFill = 1 << 2, // Enable anti-aliased edge around filled shapes (rounded rectangles, circles).
ImDrawListFlags_AllowVtxOffset = 1 << 3 // Can emit 'VtxOffset > 0' to allow large meshes. Set when 'ImGuiBackendFlags_RendererHasVtxOffset' is enabled.
};
@@ -2226,7 +2224,7 @@
ImFontAtlasFlags_None = 0,
ImFontAtlasFlags_NoPowerOfTwoHeight = 1 << 0, // Don't round the height to next power of two
ImFontAtlasFlags_NoMouseCursors = 1 << 1, // Don't build software mouse cursors into the atlas (save a little texture memory)
- ImFontAtlasFlags_NoAntiAliasedLines = 1 << 2 // Don't build anti-aliased line textures into the atlas (save a little texture memory). They will be rendered using polygons (a little bit more expensive)
+ ImFontAtlasFlags_NoLines = 1 << 2 // Don't build thick line textures into the atlas (save a little texture memory). The AntiAliasedLinesUseTex features uses them, otherwise they will be rendered using polygons (more expensive for CPU/GPU).
};
// Load and rasterize multiple TTF/OTF fonts into a same texture. The font atlas will build a single texture holding:
@@ -2327,11 +2325,11 @@
ImVector<ImFont*> Fonts; // Hold all the fonts returned by AddFont*. Fonts[0] is the default font upon calling ImGui::NewFrame(), use ImGui::PushFont()/PopFont() to change the current font.
ImVector<ImFontAtlasCustomRect> CustomRects; // Rectangles for packing custom texture data into the atlas.
ImVector<ImFontConfig> ConfigData; // Configuration data
- ImVec4 TexUvAALines[IM_DRAWLIST_TEX_AA_LINES_WIDTH_MAX + 1]; // UVs for anti-aliased line textures
+ ImVec4 TexUvLines[IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1]; // UVs for anti-aliased line textures
// [Internal] Packing data
int PackIdMouseCursors; // Custom texture rectangle ID for white pixel and mouse cursors
- int PackIdAALines; // Custom texture rectangle ID for anti-aliased lines
+ int PackIdLines; // Custom texture rectangle ID for anti-aliased lines
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
typedef ImFontAtlasCustomRect CustomRect; // OBSOLETED in 1.72+
diff --git a/imgui_demo.cpp b/imgui_demo.cpp
index 8b5d57d..d66bf01 100644
--- a/imgui_demo.cpp
+++ b/imgui_demo.cpp
@@ -3832,7 +3832,7 @@
ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines);
ImGui::SameLine(); HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well.");
ImGui::Checkbox("Anti-aliased lines use texture", &style.AntiAliasedLinesUseTex);
- ImGui::SameLine(); HelpMarker("Faster lines using texture data. Requires texture to use bilinear sampling (not nearest).");
+ ImGui::SameLine(); HelpMarker("Faster lines using texture data. Require back-end to render with bilinear filtering (not point/nearest filtering).");
ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill);
ImGui::PushItemWidth(100);
ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, 10.0f, "%.2f");
diff --git a/imgui_draw.cpp b/imgui_draw.cpp
index 15a6c6f..3150399 100644
--- a/imgui_draw.cpp
+++ b/imgui_draw.cpp
@@ -358,7 +358,7 @@
ArcFastVtx[i] = ImVec2(ImCos(a), ImSin(a));
}
memset(CircleSegmentCounts, 0, sizeof(CircleSegmentCounts)); // This will be set by SetCircleSegmentMaxError()
- TexUvAALines = NULL;
+ TexUvLines = NULL;
}
void ImDrawListSharedData::SetCircleSegmentMaxError(float max_error)
@@ -681,11 +681,16 @@
const int integer_thickness = (int)thickness;
const float fractional_thickness = thickness - integer_thickness;
- // Do we want to draw this line using a texture? (for now, only draw integer-width lines using textures to avoid issues with the way scaling occurs)
- const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex) && (integer_thickness < IM_DRAWLIST_TEX_AA_LINES_WIDTH_MAX) && (fractional_thickness >= -0.00001f) && (fractional_thickness <= 0.00001f);
+ // Do we want to draw this line using a texture?
+ // - For now, only draw integer-width lines using textures to avoid issues with the way scaling occurs, could be improved.
+ // - If AA_SIZE is not 1.0f we cannot use the texture path.
+ const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex)
+ && (integer_thickness < IM_DRAWLIST_TEX_LINES_WIDTH_MAX)
+ && (fractional_thickness >= -0.00001f) && (fractional_thickness <= 0.00001f)
+ && (AA_SIZE == 1.0f);
- // We should never hit this, because NewFrame() doesn't set ImDrawListFlags_AntiAliasedLinesUseTexData unless ImFontAtlasFlags_NoAALines is off
- IM_ASSERT_PARANOID((!use_texture) || (!(_Data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoAALines)));
+ // We should never hit this, because NewFrame() doesn't set ImDrawListFlags_AntiAliasedLinesUseTex unless ImFontAtlasFlags_NoLines is off
+ IM_ASSERT_PARANOID((!use_texture) || (!(_Data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoLines)));
const int idx_count = use_texture ? (count * 6) : (thick_line ? count * 18 : count * 12);
const int vtx_count = use_texture ? (points_count * 2) : (thick_line ? points_count * 4 : points_count * 3);
@@ -693,7 +698,7 @@
// Temporary buffer
// The first <points_count> items are normals at each line point, then after that there are either 2 or 4 temp points for each line point
- ImVec2* temp_normals = (ImVec2*)alloca(points_count * ((thick_line && !use_texture) ? 5 : 3) * sizeof(ImVec2)); //-V630
+ ImVec2* temp_normals = (ImVec2*)alloca(points_count * ((use_texture || !thick_line) ? 3 : 5) * sizeof(ImVec2)); //-V630
ImVec2* temp_points = temp_normals + points_count;
// Calculate normals (tangents) for each line segment
@@ -717,6 +722,7 @@
// The width of the geometry we need to draw - this is essentially <thickness> pixels for the line itself, plus one pixel for AA
// We don't use AA_SIZE here because the +1 is tied to the generated texture and so alternate values won't work without changes to that code
+ // If AA_SIZE != 1.0f we cannot use the use_texture path.
const float half_draw_size = use_texture ? ((thickness * 0.5f) + 1) : 1.0f;
// If line is not closed, the first and last points need to be generated differently as there are no normals to blend
@@ -775,10 +781,10 @@
if (use_texture)
{
// If we're using textures we only need to emit the left/right edge vertices
- ImVec4 tex_uvs = _Data->TexUvAALines[integer_thickness];
+ ImVec4 tex_uvs = _Data->TexUvLines[integer_thickness];
if (fractional_thickness != 0.0f)
{
- const ImVec4 tex_uvs_1 = _Data->TexUvAALines[integer_thickness + 1];
+ const ImVec4 tex_uvs_1 = _Data->TexUvLines[integer_thickness + 1];
tex_uvs.x = tex_uvs.x + (tex_uvs_1.x - tex_uvs.x) * fractional_thickness; // inlined ImLerp()
tex_uvs.y = tex_uvs.y + (tex_uvs_1.y - tex_uvs.y) * fractional_thickness;
tex_uvs.z = tex_uvs.z + (tex_uvs_1.z - tex_uvs.z) * fractional_thickness;
@@ -1719,7 +1725,7 @@
TexWidth = TexHeight = 0;
TexUvScale = ImVec2(0.0f, 0.0f);
TexUvWhitePixel = ImVec2(0.0f, 0.0f);
- PackIdMouseCursors = PackIdAALines = -1;
+ PackIdMouseCursors = PackIdLines = -1;
}
ImFontAtlas::~ImFontAtlas()
@@ -1747,7 +1753,7 @@
}
ConfigData.clear();
CustomRects.clear();
- PackIdMouseCursors = PackIdAALines = -1;
+ PackIdMouseCursors = PackIdLines = -1;
}
void ImFontAtlas::ClearTexData()
@@ -2387,27 +2393,15 @@
atlas->TexUvWhitePixel = ImVec2((r->X + 0.5f) * atlas->TexUvScale.x, (r->Y + 0.5f) * atlas->TexUvScale.y);
}
-static void ImFontAtlasBuildRegisterAALineCustomRects(ImFontAtlas* atlas)
+static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas)
{
- if (atlas->Flags & ImFontAtlasFlags_NoAntiAliasedLines)
+ if (atlas->Flags & ImFontAtlasFlags_NoLines)
return;
- // The +2 here is to give space for the end caps, whilst height +1 is to accommodate the fact we have a zero-width row
- atlas->PackIdAALines = atlas->AddCustomRectRegular(IM_DRAWLIST_TEX_AA_LINES_WIDTH_MAX + 2, IM_DRAWLIST_TEX_AA_LINES_WIDTH_MAX + 1);
-}
-
-static void ImFontAtlasBuildRenderAALinesTexData(ImFontAtlas* atlas)
-{
- IM_ASSERT(atlas->TexPixelsAlpha8 != NULL);
- if (atlas->Flags & ImFontAtlasFlags_NoAntiAliasedLines)
- return;
-
- ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(atlas->PackIdAALines);
- IM_ASSERT(r->IsPacked());
-
// This generates a triangular shape in the texture, with the various line widths stacked on top of each other to allow interpolation between them
- const int w = atlas->TexWidth;
- for (unsigned int n = 0; n < IM_DRAWLIST_TEX_AA_LINES_WIDTH_MAX + 1; n++) // +1 because of the zero-width row
+ ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(atlas->PackIdLines);
+ IM_ASSERT(r->IsPacked());
+ for (unsigned int n = 0; n < IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1; n++) // +1 because of the zero-width row
{
// Each line consists of at least two empty pixels at the ends, with a line of solid pixels in the middle
unsigned int y = n;
@@ -2415,30 +2409,18 @@
unsigned int pad_left = (r->Width - line_width) / 2;
unsigned int pad_right = r->Width - (pad_left + line_width);
- // Make sure we're inside the texture bounds before we start writing pixels
- IM_ASSERT_PARANOID(pad_left + line_width + pad_right == r.Width);
- IM_ASSERT_PARANOID(y < r.Height);
-
// Write each slice
- unsigned char* write_ptr = &atlas->TexPixelsAlpha8[r->X + ((r->Y + y) * w)];
- for (unsigned int x = 0; x < pad_left; x++)
- *(write_ptr++) = 0;
- for (unsigned int x = 0; x < line_width; x++)
- *(write_ptr++) = 0xFF;
- for (unsigned int x = 0; x < pad_right; x++)
- *(write_ptr++) = 0;
+ IM_ASSERT(pad_left + line_width + pad_right == r->Width && y < r->Height); // Make sure we're inside the texture bounds before we start writing pixels
+ unsigned char* write_ptr = &atlas->TexPixelsAlpha8[r->X + ((r->Y + y) * atlas->TexWidth)];
+ memset(write_ptr, 0x00, pad_left);
+ memset(write_ptr + pad_left, 0xFF, line_width);
+ memset(write_ptr + pad_left + line_width, 0x00, pad_right);
// Calculate UVs for this line
- ImFontAtlasCustomRect line_rect = *r;
- line_rect.X += (unsigned short)(pad_left - 1);
- line_rect.Y += (unsigned short)y;
- line_rect.Width = (unsigned short)(line_width + 2);
- line_rect.Height = 1;
-
- ImVec2 uv0, uv1;
- atlas->CalcCustomRectUV(&line_rect, &uv0, &uv1);
+ ImVec2 uv0 = ImVec2((float)(r->X + pad_left - 1), (float)(r->Y + y)) * atlas->TexUvScale;
+ ImVec2 uv1 = ImVec2((float)(r->X + pad_left + line_width + 1), (float)(r->Y + y + 1)) * atlas->TexUvScale;
float half_v = (uv0.y + uv1.y) * 0.5f; // Calculate a constant V in the middle of the row to avoid sampling artifacts
- atlas->TexUvAALines[n] = ImVec4(uv0.x, half_v, uv1.x, half_v);
+ atlas->TexUvLines[n] = ImVec4(uv0.x, half_v, uv1.x, half_v);
}
}
@@ -2454,15 +2436,22 @@
atlas->PackIdMouseCursors = atlas->AddCustomRectRegular(2, 2);
}
- ImFontAtlasBuildRegisterAALineCustomRects(atlas);
+ // Register texture region for thick lines
+ // The +2 here is to give space for the end caps, whilst height +1 is to accommodate the fact we have a zero-width row
+ if (atlas->PackIdLines < 0)
+ {
+ if (!(atlas->Flags & ImFontAtlasFlags_NoLines))
+ atlas->PackIdLines = atlas->AddCustomRectRegular(IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 2, IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1);
+ }
}
// This is called/shared by both the stb_truetype and the FreeType builder.
void ImFontAtlasBuildFinish(ImFontAtlas* atlas)
{
// Render into our custom data blocks
+ IM_ASSERT(atlas->TexPixelsAlpha8 != NULL);
ImFontAtlasBuildRenderDefaultTexData(atlas);
- ImFontAtlasBuildRenderAALinesTexData(atlas);
+ ImFontAtlasBuildRenderLinesTexData(atlas);
// Register custom rectangle glyphs
for (int i = 0; i < atlas->CustomRects.Size; i++)
diff --git a/imgui_internal.h b/imgui_internal.h
index 629ddc3..a6245d1 100644
--- a/imgui_internal.h
+++ b/imgui_internal.h
@@ -544,8 +544,7 @@
// [Internal] Lookup tables
ImVec2 ArcFastVtx[12 * IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER]; // FIXME: Bake rounded corners fill/borders in atlas
ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius (array index + 1) before we calculate it dynamically (to avoid calculation overhead)
-
- const ImVec4* TexUvAALines; // UV of anti-aliased lines in the atlas
+ const ImVec4* TexUvLines; // UV of anti-aliased lines in the atlas
ImDrawListSharedData();
void SetCircleSegmentMaxError(float max_error);