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, useImDrawListSplitter 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
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-testingImDrawList::PushClipRect()affects rendering only- Always pair
PushClipRect()withPopClipRect()
Drawing Flags
UseImDrawFlags to customize shape rendering:
ImDrawFlags_NoneImDrawFlags_Closed- For AddPolyline(): connect last and first pointImDrawFlags_RoundCornersTopLeft- Rounded cornersImDrawFlags_RoundCornersTopRightImDrawFlags_RoundCornersBottomLeftImDrawFlags_RoundCornersBottomRightImDrawFlags_RoundCornersNone- Disable roundingImDrawFlags_RoundCornersTop- Top cornersImDrawFlags_RoundCornersBottom- Bottom cornersImDrawFlags_RoundCornersLeft- Left cornersImDrawFlags_RoundCornersRight- Right cornersImDrawFlags_RoundCornersAll- All corners (default when rounding > 0)
Complete Example: Custom Widget
Performance Tips
Reference
- ImDrawList API (line 3268)
- See
imgui_demo.cppunder “Examples->Custom Rendering” for more examples