37
jonas echterhoff | [email protected] Kim Steen Riber | [email protected] Performance Optimization Tips and Tricks for Unity Tuesday, August 21, 2012

Performance Unite

  • Upload
    sannin3

  • View
    232

  • Download
    0

Embed Size (px)

DESCRIPTION

perf

Citation preview

Page 1: Performance Unite

jonas echterhoff | [email protected] Steen Riber | [email protected]

Performance Optimization Tips and Tricks for Unity

Tuesday, August 21, 2012

Page 2: Performance Unite

Performance Optimization Tips and Tricks for Unity

• Find out what your Bottlenecks are.

• Find out how to deal with them.

• Generally covers all Platforms Unity supports. Will try to point out platform specific considerations where applicable.

Tuesday, August 21, 2012

Page 3: Performance Unite

Profiler Demo

Tuesday, August 21, 2012

Page 4: Performance Unite

Optimizing your Scripts

Tuesday, August 21, 2012

Page 5: Performance Unite

Optimizing your Scripts

• Get details on interesting code by bracing it with:

Tuesday, August 21, 2012

Page 6: Performance Unite

Optimizing your Scripts

• Optimizing your algorithms is programming 101 and not covered here :)

• If scripts continuously use too much time, do you need to run them all the time?

• Maybe you can cull script execution for visible objects? Use OnBecameVisible, OnBecameInvisible callbacks to disable your script.

Tuesday, August 21, 2012

Page 7: Performance Unite

Optimizing your Scripts

• Maybe you don’t need to update every frame? Use Coroutines instead:

Tuesday, August 21, 2012

Page 8: Performance Unite

Optimizing your Scripts

• UnityEngine APIs may have unneeded overhead. Consider caching results instead of calling UnityEngine methods each frame.

• This also applies to some C# getter properties. For instance, Transform.position will iterate the transform hierarchy to calculate global position.

Tuesday, August 21, 2012

Page 9: Performance Unite

Optimizing Memory usage

Tuesday, August 21, 2012

Page 10: Performance Unite

Mono Memory

• Script objects

• Wrappers for

• Game objects

• Assets

• Components

Garbage collected

Unity Internal Memory

• Asset data• Textures, meshes,

animation etc.

• Game objects

• Internals• Rendering, particles,

physics etc

Optimizing Memory usage

Tuesday, August 21, 2012

Page 11: Performance Unite

Optimizing Memory usage• Mono allocates heap blocks for allocations.

• When available heap size runs out, new blocks are added

• Fragmentation can cause new heap blocks, even though memory is not exhausted

• Garbage Collector cleans up when• Allocation does not fit in available memory

• System.GC.Collect()

Tuesday, August 21, 2012

Page 12: Performance Unite

• Avoid allocations to get rid of Garbage Collection.

• But it’s all managed code! We have no control over allocations!

• Not quite right: Using C# does allow some control over memory allocations

Optimizing Memory usage

Tuesday, August 21, 2012

Page 13: Performance Unite

• Use structs instead of classes for local data!

Optimizing Memory usage

Tuesday, August 21, 2012

Page 14: Performance Unite

Optimizing Memory usage

Tuesday, August 21, 2012

Page 15: Performance Unite

• You can use lists of preallocated, reusable class instances to implement your own memory management scheme.

• Examples of object pooling can be found in Astro Dude on the Asset Store

Optimizing Memory usage

Tuesday, August 21, 2012

Page 16: Performance Unite

• Instantiation can be slow

• Prefetch objects you are going to use.

• For recurring objects (Bullets, Explosions) it may make sense to keep a List of inactive GameObjects around and reuse them instead of Instantiating and Destroying them!

Optimizing Memory usage

Tuesday, August 21, 2012

Page 17: Performance Unite

• Avoid using Unity’s built-in GUI system in-game, as it is not very GC friendly.

• Non-streaming games can usually get by without doing any allocations while playing!

• Use GetRuntimeMemorySize(Object), GetMonoHeapSize and GetMonoUsedSize to keep track of memory usage

Optimizing Memory usage

Tuesday, August 21, 2012

Page 18: Performance Unite

Optimizing Physics

Tuesday, August 21, 2012

Page 19: Performance Unite

Optimizing Physics

• Prefer primitive colliders over mesh colliders.

• Tweak Time.fixedDeltaTime (in Project settings -> Time) to be as high as you can get away with. If your game is slow moving, you probably need less fixed updates then games with fast action.

Tuesday, August 21, 2012

Page 20: Performance Unite

Optimizing Physics

• The most common performance pitfall using physics:

• Never ever move a static collider (i.e. a collider without a Rigidbody)!

• Shows up in Profiler as “Static Collider.Move” but actual processing is in Physics.Simulate!

Tuesday, August 21, 2012

Page 21: Performance Unite

Optimizing Graphics

Tuesday, August 21, 2012

Page 22: Performance Unite

Optimizing Graphics

• How fast you can render is bound by GPU and CPU performance.

• GPU performance is mostly limited by the amount of pixels rendered (fill rate) and by memory bandwidth.

• CPU performance is sometimes limited by the amount of draw calls processed.

Tuesday, August 21, 2012

Page 23: Performance Unite

Optimizing Graphics

• Use the GPU Profiler, to find out how much time is spend rendering each draw call in your scene.

• Any draw call you can get rid off completely saves rendering time on both GPU and CPU, so first let’s see if there are any draw calls we can do without.

• After that, let’s see what we can do to make your objects render faster.

Tuesday, August 21, 2012

Page 24: Performance Unite

Optimizing Graphics

• Improve performance by rendering fewer objects:

• By reducing scene complexity.

• By culling more aggressively (occlusion culling, frustum culling).

• By using LOD.

Tuesday, August 21, 2012

Page 25: Performance Unite

Optimizing Graphics

• Improve performance by reducing shader passes:

• Choose a scene setup and shaders with fewer passes (reflections, shadows, pixel lights in forward rendering all add passes).

Tuesday, August 21, 2012

Page 26: Performance Unite

Optimizing Graphics

• Improve performance by reducing shader passes:

• Forward rendered pixel lights are expensive. Use light mapping instead of realtime lights where ever possible.

• Adjust pixel light count in quality settings.

Tuesday, August 21, 2012

Page 27: Performance Unite

Optimizing Graphics

• Now let’s see what we can do to make objects render faster, on both GPU and CPU.

Tuesday, August 21, 2012

Page 28: Performance Unite

Optimizing Graphics (CPU)

• CPU performance is sometimes limited by the amount of draw calls to process.

• Rule of thumb: don’t use more then a few hundred draw calls per frame on mobiles or a few thousand on desktops for current hardware.

Tuesday, August 21, 2012

Page 29: Performance Unite

Optimizing Graphics (CPU)

• You can use fewer draw calls by combining nearby objects into single meshes, so they are drawn together.

• Manually in your source assets.

• Automatically using Unity’s draw call batching.

Tuesday, August 21, 2012

Page 30: Performance Unite

Optimizing Graphics (CPU)

• Use fewer different materials to allow for better batching of meshes.

• Use Atlas textures where possible.

Tuesday, August 21, 2012

Page 31: Performance Unite

Optimizing Graphics (CPU)

• If you are not bound by draw calls, then batching is actually worse for performance, as it makes culling less efficient and makes more objects affected by lights!

Tuesday, August 21, 2012

Page 32: Performance Unite

Optimizing Graphics (GPU)

• GPU performance is often limited by fillrate or memory bandwidth.

• Checking if you are fillrate-bound is easy: does the game run faster if you decrease the display resolution? If yes, you are limited by fillrate.

• Likewise, if reducing the Texture Quality in Quality Settings makes the game run faster, you are probably limited by memory bandwidth.

Tuesday, August 21, 2012

Page 33: Performance Unite

Optimizing Graphics (GPU)

• If you are fillrate bound, try reducing shader complexity:

• Mobile GPUs: avoid alpha-testing shaders, prefer alpha-blended versions.

• Use simple, optimized shader code (such as the “Mobile” shaders coming with Unity).

Tuesday, August 21, 2012

Page 34: Performance Unite

Optimizing Graphics (GPU)• Optimizing your shader code:

• Avoid expensive math functions in shader code (pow, exp, log, cos, sin, tan, etc). Consider using pre-calculated lookup textures instead.

• Mobile: pick lowest possible number precision format (float, half, fixed in Cg) for best performance.

Tuesday, August 21, 2012

Page 35: Performance Unite

Optimizing Graphics (GPU)

• If you are memory bandwidth bound, decrease required texture memory:

• Use texture compression or 16 bit textures.

• Reduce texture size (check “Mipmaps” render mode in scene view).

• Use mipmaps for any texture used in 3D scene.

Tuesday, August 21, 2012

Page 36: Performance Unite

Optimizing Graphics (GPU)

• GPU performance can also be bound by geometric complexity, i.e. the amount of vertices the GPU needs to process.

• Don’t use more triangles then necessary.

• Keep number of UV seams and hard edges (vertices with multiple normals) as low as possible.

• Use simpler vertex shaders.

Tuesday, August 21, 2012

Page 37: Performance Unite

Q & A

Tuesday, August 21, 2012