Skip to main content

Custom Rendering

This guide covers advanced custom rendering techniques including render channels, primitive allocation, and draw callbacks.

Render Channels

Channels allow you to split rendering into layers that can be drawn out of order and then merged back together. This is useful for:
  • Rendering background elements after foreground elements
  • Minimizing draw calls when switching between different clipping rectangles
  • Creating complex layered UIs

Basic Channel Usage

Using ImDrawListSplitter

For more complex scenarios, use ImDrawListSplitter directly. This allows you to stack splitters:
Prefer using your own persistent ImDrawListSplitter instance as you can stack them. Using ImDrawList::ChannelsXXXX() doesn’t support stacking.

Low-Level Primitive Allocation

For maximum performance, you can manually allocate vertex and index buffers:

Primitive Helper Functions

Draw Callbacks

Draw callbacks allow you to execute custom code during rendering, useful for:
  • Changing render state (shaders, blend modes, etc.)
  • Emitting custom GPU commands
  • Integrating with external rendering systems

Callback with Data Copy

Important callback considerations:
  • If userdata_size == 0: the userdata pointer is stored as-is
  • If userdata_size > 0: the data is copied into an internal buffer, and ImDrawCmd::UserCallbackData will point to that copy
  • All standard backends honor draw callbacks
  • Use ImDrawCallback_ResetRenderState to reset to default state

Texture Management

Switching Textures

Getting UV Coordinates for White Pixel

Polylines and Polygons

Polygon filling notes:
  • Only simple polygons are supported (no self-intersections, no holes)
  • Convex polygon fill is faster than concave
  • Concave polygon fill has O(N²) complexity but is provided for convenience

Advanced Clipping

Clipping behavior:
  • ImGui::PushClipRect() affects both rendering AND hit-testing
  • ImDrawList::PushClipRect() affects rendering only
  • Always pair PushClipRect() with PopClipRect()

Drawing Flags

Use ImDrawFlags to customize shape rendering:
Available flags:
  • ImDrawFlags_None
  • ImDrawFlags_Closed - For AddPolyline(): connect last and first point
  • ImDrawFlags_RoundCornersTopLeft - Rounded corners
  • ImDrawFlags_RoundCornersTopRight
  • ImDrawFlags_RoundCornersBottomLeft
  • ImDrawFlags_RoundCornersBottomRight
  • ImDrawFlags_RoundCornersNone - Disable rounding
  • ImDrawFlags_RoundCornersTop - Top corners
  • ImDrawFlags_RoundCornersBottom - Bottom corners
  • ImDrawFlags_RoundCornersLeft - Left corners
  • ImDrawFlags_RoundCornersRight - Right corners
  • ImDrawFlags_RoundCornersAll - All corners (default when rounding > 0)

Complete Example: Custom Widget

Performance Tips

Reference

  • ImDrawList API (line 3268)
  • See imgui_demo.cpp under “Examples->Custom Rendering” for more examples