building a fast user interface

82
COPYRIGHT 2015 @ UNITY TECHNOLOGIES BUILDING A FAST USER INTERFACE Tim Cooper Rune Johansen

Upload: phamnguyet

Post on 08-Jan-2017

230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

BUILDING A FAST USER INTERFACE• Tim Cooper• Rune Johansen

Page 2: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

THINGS YOU WILL KNOW• Author high performance UI’s• Use the new Unity Frame debugger• Understand what ‘batching’ is and why you want it• The performance cost of a CanvasRenderer• How to write optimised custom UI components

Page 3: BUILDING A FAST USER INTERFACE

COPYRIGHT 2014 @ UNITY TECHNOLOGIESCOPYRIGHT 2015 @ UNITY TECHNOLOGIES

WHO ARE WE?

Page 4: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

TIM COOPER

Page 5: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

RUNE JOHANSEN

Page 6: BUILDING A FAST USER INTERFACE

COPYRIGHT 2014 @ UNITY TECHNOLOGIESCOPYRIGHT 2015 @ UNITY TECHNOLOGIES

BATCHING

Page 7: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

WHAT IS BATCHING?• Decreases draw calls

• Combine input elements into one output element• Reduces the load on the render thread

Page 8: BUILDING A FAST USER INTERFACE

No Batching

Page 9: BUILDING A FAST USER INTERFACE

Batching

Page 10: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

HOW DOES BATCHING GET CALCULATED?• Based on a number of internal rules

• Material changes• Element overlaps• Shared material properties

• You need to configure your project to take advantage of this

• You need to understand your content ;)

Page 11: BUILDING A FAST USER INTERFACE

**BEFORE WE BEGIN**

Page 12: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

UNITY 5 FRAME DEBUGGER• Step through individual draw calls• Easiest way to see what is getting batched

• Works in editor!

• How do I use it?

Page 13: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

DEMO

Page 14: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

BACK TO BATCHING• Batch calculations happen for each canvas

• This includes nested canvases!

• What are the rules?

Page 15: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

RULE #1A• If elements use the same material and texture

• They can batch together

Page 16: BUILDING A FAST USER INTERFACE
Page 17: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

RULE #1B• If elements use the same material and texture and

overlap

• They can still batch!

Page 18: BUILDING A FAST USER INTERFACE
Page 19: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

RULE #2• Batches will be broken when a material / texture

change is required

Page 20: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

RULE #2A• Simple case:

• Material / Texture change

• Elements can not batch• Separate draw calls are required

Page 21: BUILDING A FAST USER INTERFACE
Page 22: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

RULE #2B• Less simple case

• Overlapping elements

• Elements need to be rendered in order• If there are different materials / textures between

two non overlapping elements…• Batching will break

Page 23: BUILDING A FAST USER INTERFACE
Page 24: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

BATCH SORTING• Internal to the UI System

• Uses rect overlaps to calculate this.• Rearranges draw order

• Least material changes• Least texture changes

• Keeps output appearing the same

Page 25: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

SPECIAL RULE (5.2)• Only elements that are coplanar with the Canvas will

sort for batching• If the UI is in perspective mode, or elements are

rotated things can render incorrectly.

Page 26: BUILDING A FAST USER INTERFACE

Modified internal code to show issue

Page 27: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

SPECIAL RULE (5.2)• If we ‘break’ the sorting

• The issue will be fixed

• Depending on content this may or may not cost more draw calls.

Page 28: BUILDING A FAST USER INTERFACE
Page 29: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

SPECIAL RULE (5.2)• What makes an element break coplanerness?

• Elements having a different z-value from the canvas• Elements rotated out of canvas alignment• Input mesh with vertices where the z value is not

aligned with the canvas

Page 30: BUILDING A FAST USER INTERFACE
Page 31: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

RULE SUMMARY• Changing materials breaks a batch• Changing textures breaks a batch• Elements offset from the canvas breaks a batch

Page 32: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

APPLYING THE RULES• Minimise material changes in your UI

• Use materials for effects, or special things• Use a single material for most of the UI

• 5.2• Text and normal UI elements can use the same

material

Page 33: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

APPLYING THE RULES• Minimise texture changes in your UI

• Use sprite atlases.

• Note - Text is not put in a sprite atlas• Can lead to increased draw calls.

Page 34: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

APPLYING THE RULES• Keep elements in coplanar with the Canvas

• You can use nested canvas• It’s okay to move some elements off the canvas

• Buttons that pop out ect• Make it the exception

Page 35: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

RENDERING

•Fast Batches•Slow Batches

Page 36: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

SLOW BATCHES• Whenever a material is changed

• The shader must be changed• Slow process• Internal rendering state needs to be rebuilt• “SetPass” in the stats window

• Minimise material changes

Page 37: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

FAST BATCHES• Whenever a texture is changed

• The material can stay the same• Internal rendering state stays the same

• The shader uniforms are updated• Just another draw call

Page 38: BUILDING A FAST USER INTERFACE
Page 39: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

IN PRACTICE• Example

Page 40: BUILDING A FAST USER INTERFACE

COPYRIGHT 2014 @ UNITY TECHNOLOGIESCOPYRIGHT 2015 @ UNITY TECHNOLOGIES

CANVAS RENDERER

Page 41: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

BUILDING BLOCKS• All renderable UI elements have a CanvasRenderer• The UI components configure a CanvasRenderer

• Texture• Mesh• Colour

• What happens when you change a CanvasRenderer?• What happens if you reparent or reorder CanvasRenders?

Page 42: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

MODIFICATION• Causes Rebatching!

• Need to recalculate internal state• Could change batching / other elements• Not slow, but also not fast

• Do it as little as possible!

Page 43: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

REORDERING• Rearrange hierarchy / Change sibling order

• We need to recalculate the depths of all elements• Manually iterate all CanvasRenders on a Canvas

• Then do a Rebatch

• Avoid this whenever you can!

Page 44: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

PIXEL PERFECT• Snaps RectTransform edges to vertex boundaries• Happens whenever a Rect is moved

• Including children• Means that vertices need to be regenerated

• SLOW!

Page 45: BUILDING A FAST USER INTERFACE
Page 46: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

PIXEL PERFECT• For speed

• Disable pixel perfect on Canvases where elements move

• You can disable at the start of movement, reenable after

• Pixel perfect only works on Screen Space canvas!

Page 47: BUILDING A FAST USER INTERFACE

COPYRIGHT 2014 @ UNITY TECHNOLOGIESCOPYRIGHT 2015 @ UNITY TECHNOLOGIES

OPTIMIZING CUSTOM UI COMPONENTS

Page 48: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

CUSTOM UI ELEMENTSYou can make your own UI components• Custom graphics (controls Canvas Renderer)• Custom layout (controls Rect Transforms)

Optimization means:• Layout and graphics should only be rebuilt when

needed

Page 49: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

public interface ICanvasElement{void Rebuild(CanvasUpdate executing);…

}

• Custom UI elements can be Canvas Elements• Implement ICanvasElement

CANVAS ELEMENT

Page 50: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

• CanvasUpdate• Prelayout• Layout• PostLayout• PreRender• LatePreRender

public interface ICanvasElement{void Rebuild(CanvasUpdate executing);…

}

Page 51: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

• PreLayout• Update data needed for layout calculations

• Layout• Used by auto-layout system

• PostLayout• Update data dependent on layout

• PreRender• Update vertices and materials

• LatePreRender• Update vertices dependent on generated text character data

REBUILD CALLBACKS

Page 52: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

LayoutCanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild

• PreLayout• Layout• PostLayout

GraphicCanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild

• PreRender• LatePreRender

TRIGGERING REBUILDS

Page 53: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

• Rebuilds are delayed, not immediate• Multiple things may trigger the same rebuild

• but it only happens once

TRIGGERING REBUILDS

Page 54: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Canvas.willRenderCanvases >delegate invoked (5.2+)

The CanvasUpdater is invoked as part of willRenderCanvases

Each rebuild can be triggered by multiple things in the same frame, but the callback only happens once.

Page 55: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

• We want rebuilds to happen when needed• Otherwise we get buggy behavior

• We only want the rebuilds when needed• Otherwise performance suffers

WHAT SHOULD TRIGGER WHICH REBUILDS?

Page 56: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

PROPERTY CHANGES IN YOUR COMPONENTIf a property change may require changes to vertices or materials (anything on CanvasRenderer)• RegisterCanvasElementForGraphicRebuild

Examples• Color change• Sprite change• Essentially anything that affects appearance

Page 57: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

PROPERTY CHANGES IN YOUR COMPONENTIf a property change may require changes to position or size (anything on RectTransform)

• Change RectTransform immediately (simple things - slider)

• LayoutRebuilder.MarkLayoutForRebuild (auto-layout)

• CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuildRegisterCanvasElementForLayoutRebuild is only rarely used

Normally higher level LayoutRebuilder is used instead.

Page 58: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

CHANGES TO RECTTRANSFORM OR HIERARCHYOutside changes may require a rebuild

• OnRectTransformDimensionsChange

• OnTransformParentChanged

• OnTransformChildrenChanged (for layout groups)

• OnCanvasGroupChanged

Page 59: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

SHORTCUTSInherit from existing built-in classes that do much of the work

• Graphic• Selectable• LayoutGroup

Page 60: BUILDING A FAST USER INTERFACE

COPYRIGHT 2014 @ UNITY TECHNOLOGIESCOPYRIGHT 2015 @ UNITY TECHNOLOGIES

OPTIMIZING CUSTOM LAYOUT COMPONENTS

Page 61: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

AUTO-LAYOUTAuto-Layout system has• Layout Elements (Image, Text, LayoutElement)• Layout Controllers

• Layout Groups (HorizontalLayoutGroup)• Layout Self Controllers (ContentSizeFitter)

Page 62: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

LAYOUT ELEMENTSLayout Elements provide layout input:

• Minimum size• Preferred size• Flexible size

Page 63: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

LAYOUT ELEMENTSpublic interface ILayoutElement{

void CalculateLayoutInputHorizontal();void CalculateLayoutInputVertical();

float minWidth { get; }float preferredWidth { get; }float flexibleWidth { get; } float minHeight { get; }float preferredHeight { get; }float flexibleHeight { get; }

int layoutPriority { get; }}

Page 64: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

LAYOUT CONTROLLERSLayout Controllers use the layout input and control

• RectTransform sizes• RectTransform positions

Page 65: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

LAYOUT CONTROLLERSpublic interface ILayoutController{

void SetLayoutHorizontal();void SetLayoutVertical();

}

Page 66: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

AUTO-LAYOUT CALLBACK ORDERLayout input data can depend on children• Calculate children first, parents afterwards!

Layout control of RectTransform can affect children• Calculate parents first, children afterwards!

This avoids calculations becoming obsolete because of child or parent changes in the same frame

• No need to re-calculate

Page 67: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 68: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 69: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 70: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 71: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 72: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 73: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 74: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 75: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 76: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 77: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 78: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 79: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 80: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 81: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

Page 82: BUILDING A FAST USER INTERFACE

COPYRIGHT 2015 @ UNITY TECHNOLOGIES

LAYOUT ELEMENT OPTIMIZATIONOptimize the layout calculations by storing the calculated layout input for when it’s used

• CalculateLayoutInputHorizontal• Calculate and store minimum, preferred, flexible

• minWidth, preferredWidth, flexibleWidth properties• Simply return stored result