Other Posts

Rendering Workflow of Slate

The rendering of Slate happens in FSlateApplication::PrivateDrawWindows. It first iterates every visible window to collect nessary rendering information used for rendering. Then consumes these information by invoking corresponding platform’s graphics API (D3D, OpenGL, etc.) to do the rendering job.

Collecting

A window (SWindow) in UE4’s terminology is the top-level widget of game, usually one game has only one window, every widget we created at runtime is just a child of it.

The collecting is rooted from window, then follows its widget hierarchy in a deep-first order to collect each widget’s rendering information. This is done in each widget’s OnPaint function, inside this funcion, a leaf widget simply collects itself’s rendering information, a compound widget will also call each child’s OnPaint function. Rendering order of parent and children is thus determined by parent’s OnPaint function.

The rendering information is represented by class FSlateDrawElement, we can basicly think it as texture (brush) and text information.

Rendering

After collected every window’s rendering informaion, Slate will iterate each window’s rendering infomation, create batches from it and then invoke corresponding platform’s graphics API to draw these batches. This is all done in FSlateRenderer::DrawWindows.

The rendering process on Windows + DirectX is:

  1. Create batches by calling FSlateElementBatcher::AddElements and FSlateBatchData::CreateRenderBatches.
  2. Merge all vertics and indics of window to a large buffer correspondingly by calling FSlateD3D11RenderingPolicy::UpdateVertexAndIndexBuffers.
  3. Draw batches by calling ID3D11DeviceContext:::RSSetViewports, ID3D11DeviceContext::OMSetRenderTargets, FSlateD3D11RenderingPolicy::DrawElements and D3DSwapChain::Present.
  4. Reset batches to ready for next window by calling FSlateElementBatcher::ResetBatches.