SlideShare una empresa de Scribd logo
1 de 93
Tales from the
Optimization Trenches
Ignacio Liverotti
Unity Technologies
About me and what I do here at Unity
3
— Joined Unity as a Software Engineer in 2015
— Became a Developer Relations Engineer in 2018
— I visit our Enterprise customers and help them resolve
technical issues affecting their projects
Project Reviews
4
— Multi-day engagement:
— We travel to our customers’ offices
— Review their projects
— Identify problems
— Some of them are resolved onsite
— We investigate and recommend
solutions for the rest
Project Reviews
5
— Types of problems:
– Runtime performance
– Build/patch size
– Load times
– Workflow issues
– Build times
Today’s plan
6
— Introduction to optimization and profiling in Unity
— CPU optimization
— GPU optimization
— Memory footprint optimization
— (Optimization) rules to live by
Introduction to
optimization and
profiling
What is optimization?
8
— Modifying a project so that an aspect of it is more efficient or
uses fewer resources (CPU, GPU, etc)
Why do we want to optimize?
9
— To pass the certification requirements imposed by the various distribution
platforms
— To reduce battery consumption
— To deploy our project to a wider range of target devices
— To streamline our production process
Optimization involves a lot more than
rewriting code!
What else does it involve?
11
— Reviewing assets (texture size, format, poly count, audio files sample freq, etc.)
— Reviewing the Project Settings
— Reviewing the assets’ settings
— Simplifying our solutions
The first step in our (optimization) journey: profiling!
12
— Using tools to gather actual data on how resources are being used
— This data will drive our optimization efforts
— We don’t want to optimize based on “guesses”
— We want the tools to let us know where the problems are
A note about optimization ‘tips’ and ‘advice’
13
— Don’t apply optimization advice blindly
– Certain pieces of advice are always applicable
– But good advice applied in the wrong situation can make things worse
– A technique that worked in a certain project and platform might not work for you
CPU optimization
What is the goal of CPU optimization?
15
— To reduce the stress on the CPU
– Because the CPU is the actual performance bottleneck
– Or to free the CPU so that we can do more
How do we achieve that?
16
— By using more efficient combinations of algorithms and data structures
— By aiming for nearly zero per-frame allocations
– The GC algorithm can be quite CPU intensive!
Tools of the trade: CPU
17
— Unity Profiler
— Unity Profile Analyzer
– Don’t miss the next talk!
— Xcode Instruments
— Intel VTune Amplifier
— Consoles have their own
proprietary tools
Tools of the trade: CPU
18
Tools of the trade: CPU
19
Example 1: Per-frame memory allocations
20
— Scenario: Management game for mobile platforms that ‘feels’ slow
during gameplay
— We take a capture using the Unity Profiler and uncheck all items, except
for ‘GarbageCollector’:
The Garbage Collector is running once every
three frames
Example 1: Per-frame memory allocations
22
— Our hypothesis: Our code is allocating memory on a per-frame basis
— Let’s select a random frame in the Unity Profiler:
Example 1: Per-frame memory allocations
23
— What about 10 frames later?
400 KB of allocations per frame
@60 FPS
~=
24 MB of allocations per second
Example 1: Per-frame memory allocations
25
— Let’s dig into the ‘GC Alloc’ column:
Example 1: Per-frame memory allocations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Update()
{
ProcessScore();
ProcessHitPoints();
// More game logic methods.
}
private void ProcessScore()
{
Debug.Log("GameLogic.ProcessScore(). Score: " + Score.ToString("00000"));
// Score processing logic.
}
private void ProcessHitPoints()
{
Debug.Log("GameLogic.ProcessHitPoints(). HitPoints: " + HitPoints.ToString("00000"));
// Hit points processing logic.
}
26
Example 1: Per-frame memory allocations
27
— From
https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
using System.Diagnostics;
public static class Logging
{
[Conditional ("ENABLE_LOG")]
static public void Log (object message)
{
UnityEngine.Debug.Log (message);
}
}
1
2
3
4
5
6
7
8
9
10
Example 1: Per-frame memory allocations
28
— If we want to see the log messages, we need to add ENABLE_LOG to the
list of defined symbols:
Example 1: Per-frame memory allocations
29
— Let’s remove ENABLE_LOG and reprofile:
Example 1: Per-frame memory allocations
30
— Zero per-frame allocations
— No GC spikes! 
Takeaway: use the Unity Profiler to
understand where your managed allocations
are coming from and fix them
Example 2: GC spikes in a fast-paced game
32
— Scenario: Mobile racing game where the frame rate needs to
be steady
— Common approach: let the GC do its work
– The problem it causes: When the GC kicks in, program execution
actually stops
– Also, the larger the managed heap, the longer it takes for the
GC algorithm to complete
Example 2: GC spikes in a fast-paced game
33
— GC capture:
Example 2: GC spikes in a fast-paced game
34
— What we recommend:
– Unload all resources when transitioning from the menu to the ‘racing’ scene
– Allocate a pool of objects
– Optimize the frame time as much as possible
– Enable the incremental garbage collector
Example 2: GC spikes in a fast-paced game
35
— The incremental GC was introduced in the early 2019 development cycle
— Instead of causing a single, long interruption, it splits the work across multiple
slices
Example 2: GC spikes in a fast-paced game
36
— Incremental GC capture:
Example 2: GC spikes in a fast-paced game
37
— Enable it via the Player Settings menu:
Takeaway: use the incremental GC and
remember to optimize the frame time as
much as possible so that we can give it
room to do its job
GPU optimization
What are the goals of GPU optimization?
40
— Reduce the stress on the GPU so that we can render our
scene at the target frame rate
— Free up the GPU for performing other tasks (including
offloading work from the CPU via compute shaders)
How do we achieve that?
41
— Minimizing the number of unnecessary rendering operations
— Reducing the amount of data sent to the GPU
— Minimizing the number of state changes (‘set pass’ calls)
— Optimizing our most expensive shaders
Tools of the trade: GPU
42
— Unity Frame Debugger
— RenderDoc
— NVidia NSight
— XCode Frame Capture
— Intel GPA
— Consoles have their own
proprietary tools
Tools of the trade: GPU
43
Example 3: Strategy game for mobile
44
— Scenario: A customer working on strategy game for
iOS/Android were experiencing framerate issues
— We profiled it using Xcode Frame Capture and saw a warning
message saying that we were sending too much geometry to
the GPU
Example 3: Strategy game for mobile
45
Example 3: Strategy game for mobile
46
Example 3: Strategy game for mobile
47
— Both draw calls have the same geometry as input:
— But the output of one of them is taking significantly more
screen real state in the final frame than the other one!
Example 3: Strategy game for mobile
48
Example 3: Strategy game for mobile
49
Do we need that much geometry for the
model in the background?
We probably don’t.
Example 3: Strategy game for mobile
51
— Our advice to the team: create LODs for the assets
Example 3: Strategy game for mobile
52
53
34.4K
With LODs
~38%
Tris reduction
55.1K
Without LODs
Takeaway: by understanding our
requirements and the tools and techniques
at our disposal, we’ve achieved a nearly 40%
reduction without observable differences in
the final output
Example 4: Sprite rendering
55
— Scenario: A customer working on a top down tile-based
strategy game for PC experienced very low frame rates when
deploying to mobile and WebGL
Example 4: Sprite rendering
56
— We profiled the game using the Unity Profiler and saw high frame times
— Most of that time was spent on rendering
— The CPU was too busy creating and sending rendering commands to
the GPU
Example 4: Sprite rendering
57
— The Frame Debugger revealed one draw call per tile (several hundred draw
calls in the real project!)
Example 4: Sprite rendering
58
Example 4: Sprite rendering
59
Example 4: Sprite rendering
60
— Let’s look at the SpriteRenderer components:
– They all share the same material!
Let’s look at the code…
Example 4: Sprite rendering
1
2
3
4
5
6
7
8
9
10
11
12
using UnityEngine;
public class Tile : MonoBehaviour
{
private Material _spriteRendererMaterial;
void PreprocessMethod()
{
var spriteRenderer = GetComponent<SpriteRenderer>();
_spriteRendererMaterial = spriteRenderer.material;
}
}
62
This statement creates a
copy of the first material
from this SpriteRenderer,
assigns it to the
SpriteRenderer and returns
it.
Example 4: Sprite rendering
1
2
3
4
5
6
7
8
9
10
11
12
using UnityEngine;
public class Tile : MonoBehaviour
{
private Material _spriteRendererMaterial;
void PreprocessMethod()
{
var spriteRenderer = GetComponent<SpriteRenderer>();
_spriteRendererMaterial = spriteRenderer.sharedMaterial;
}
}
63
Example 4: Sprite rendering
64
— All tiles are now drawn in the same batch
— The game was successfully deployed to mobile and WebGL 
— And the performance of the standalone version improved as well!
Takeaway: identifying the problem and
understanding the underlying issue allowed us to
trim several hundred draw calls per frame
Memory footprint
optimization
— Fit on devices that don’t have a large amount of memory
— Improve loading times
— Being able to add more content
— Avoid hard-crashes due to out of memory situations
— Improving overall performance by shuffling around less data
during runtime
What are the goals of memory footprint
reduction?
67
Tools of the trade:
memory footprint
— Unity Memory Profiler
— Xcode Instruments Allocations
— Xcode Instruments VM Tracker
— Consoles have their own
proprietary tools
68
Tools of the trade: memory footprint
69
Tools of the trade: memory footprint
70
Example 5: Built-in shaders duplication
71
— A customer project has a large number of materials that use
Unity’s Standard shader:
Example 5: Built-in shaders duplication
72
— Each Material is stored in its own AssetBundle:
Example 5: Built-in shaders duplication
73
— A memory snapshot of the project reveals that there are
multiple instances of the Standard shader in memory:
Example 5: Built-in shaders duplication
74
— This happens because the Standard shader is one of Unity’s
built-in shaders
— As such, it cannot be explicitly included in an AssetBundle
— And it will be implicitly included in every AssetBundle that
has a material with a reference to it
Example 5: Built-in shaders duplication
75
— What we recommend instead:
– Download a copy of the built-in shaders
– Make a copy of the Standard Shader, rename it (e.g., ‘Unite 2019
Standard’) and add it to its own AssetBundle
– Fix the materials so that they use the new renamed shader
– Rebuild the AssetBundles
Example 5: Built-in shaders duplication
76
— After rebuilding and taking a new memory snapshot:
– We now have a single instance of our custom ‘Unite 2019 Standard’
shader 
Takeaway: by using the right tools and
understanding the internals of the engine, we
were able to eliminate duplicates in memory
78
— Scenario: A customer is porting a desktop game to mobile
platforms and it keeps crashing on low-end devices due to its
high memory footprint
Example 6: Unable to run the game on mobile
Example 6: Unable to run the game on mobile
79
— A memory snapshot of the project reveals this:
80
Example 6: Unable to run the game on mobile
81
— Let’s look at the settings for these assets:
Example 6: Unable to run the game on mobile
82
— ‘Decompress on load’ option description from the Unity manual:
– Audio files will be decompressed as soon as they are loaded
– This option should be used for smaller compressed sounds to avoid the
performance overhead of decompressing on the fly
– Be aware that decompressing Vorbis-encoded sounds on load will use about
ten times more memory than keeping them compressed, so don’t use this
option for large files
Example 6: Unable to run the game on mobile
Do we have other options?
We do! There’s a ‘Streaming’ option
84
— Description from the Unity manual:
– Decode audio on the fly
– Uses a minimal amount of memory to buffer compressed data
– The data is incrementally read from the disk and decoded on the fly
Example 6: Unable to run the game on mobile
85
— Let’s change the load type to ‘Streaming’:
Example 6: Unable to run the game on mobile
86
— And take another memory snapshot:
Example 6: Unable to run the game on mobile
87
0.5MB
Set to ‘Streaming’
~99%
Memory footprint reduction
53MB
Set to ‘Decompress on load’
Takeaway: understanding our requirements
and using the correct settings allowed us to
reduce the audio memory footprint by ~99%
89
— These problems can be avoided!
— Let’s not catch them via the Memory Profiler
— Instead, let’s use an AssetPostprocessor and create rules:
– Background music assets should be set to streaming
– SFX should be set to decompress on load
– Etc
Example 6: Unable to run the game on mobile
Bonus tip: Snapshot diffs
90
(Optimization) rules to
live by
(Optimization) rules to live by
92
— Don’t assume where the bottlenecks are, always profile first
— Profile on the target device
— Profile early, profile often
— Don’t apply several fixes simultaneously, tackle one problem at the time
— Apply the ‘optimization triad’:
– Optimize your assets
– Update fewer things
– Draw less stuff 
Thank you

Más contenido relacionado

La actualidad más candente

김동건, 할머니가 들려주신 마비노기 개발 전설, NDC2019
김동건, 할머니가 들려주신 마비노기 개발 전설, NDC2019김동건, 할머니가 들려주신 마비노기 개발 전설, NDC2019
김동건, 할머니가 들려주신 마비노기 개발 전설, NDC2019
devCAT Studio, NEXON
 
Lighting Shading by John Hable
Lighting Shading by John HableLighting Shading by John Hable
Lighting Shading by John Hable
Naughty Dog
 

La actualidad más candente (20)

4K Checkerboard in Battlefield 1 and Mass Effect Andromeda
4K Checkerboard in Battlefield 1 and Mass Effect Andromeda4K Checkerboard in Battlefield 1 and Mass Effect Andromeda
4K Checkerboard in Battlefield 1 and Mass Effect Andromeda
 
Triangle Visibility buffer
Triangle Visibility bufferTriangle Visibility buffer
Triangle Visibility buffer
 
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
 
【Unite Tokyo 2019】Unityプログレッシブライトマッパー2019
【Unite Tokyo 2019】Unityプログレッシブライトマッパー2019【Unite Tokyo 2019】Unityプログレッシブライトマッパー2019
【Unite Tokyo 2019】Unityプログレッシブライトマッパー2019
 
Killzone Shadow Fall: Creating Art Tools For A New Generation Of Games
Killzone Shadow Fall: Creating Art Tools For A New Generation Of GamesKillzone Shadow Fall: Creating Art Tools For A New Generation Of Games
Killzone Shadow Fall: Creating Art Tools For A New Generation Of Games
 
More Performance! Five Rendering Ideas From Battlefield 3 and Need For Speed:...
More Performance! Five Rendering Ideas From Battlefield 3 and Need For Speed:...More Performance! Five Rendering Ideas From Battlefield 3 and Need For Speed:...
More Performance! Five Rendering Ideas From Battlefield 3 and Need For Speed:...
 
Stochastic Screen-Space Reflections
Stochastic Screen-Space ReflectionsStochastic Screen-Space Reflections
Stochastic Screen-Space Reflections
 
Physically Based Sky, Atmosphere and Cloud Rendering in Frostbite
Physically Based Sky, Atmosphere and Cloud Rendering in FrostbitePhysically Based Sky, Atmosphere and Cloud Rendering in Frostbite
Physically Based Sky, Atmosphere and Cloud Rendering in Frostbite
 
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
 
Physically Based and Unified Volumetric Rendering in Frostbite
Physically Based and Unified Volumetric Rendering in FrostbitePhysically Based and Unified Volumetric Rendering in Frostbite
Physically Based and Unified Volumetric Rendering in Frostbite
 
The Rendering Technology of 'Lords of the Fallen' (Game Connection Europe 2014)
The Rendering Technology of 'Lords of the Fallen' (Game Connection Europe 2014)The Rendering Technology of 'Lords of the Fallen' (Game Connection Europe 2014)
The Rendering Technology of 'Lords of the Fallen' (Game Connection Europe 2014)
 
Intro to unreal with framework and vr
Intro to unreal with framework and vrIntro to unreal with framework and vr
Intro to unreal with framework and vr
 
Killzone Shadow Fall Demo Postmortem
Killzone Shadow Fall Demo PostmortemKillzone Shadow Fall Demo Postmortem
Killzone Shadow Fall Demo Postmortem
 
김동건, 할머니가 들려주신 마비노기 개발 전설, NDC2019
김동건, 할머니가 들려주신 마비노기 개발 전설, NDC2019김동건, 할머니가 들려주신 마비노기 개발 전설, NDC2019
김동건, 할머니가 들려주신 마비노기 개발 전설, NDC2019
 
Secrets of CryENGINE 3 Graphics Technology
Secrets of CryENGINE 3 Graphics TechnologySecrets of CryENGINE 3 Graphics Technology
Secrets of CryENGINE 3 Graphics Technology
 
Lighting Shading by John Hable
Lighting Shading by John HableLighting Shading by John Hable
Lighting Shading by John Hable
 
LOD and Culling Systems That Scale - Unite LA
LOD and Culling Systems That Scale  - Unite LALOD and Culling Systems That Scale  - Unite LA
LOD and Culling Systems That Scale - Unite LA
 
Holy smoke! Faster Particle Rendering using Direct Compute by Gareth Thomas
Holy smoke! Faster Particle Rendering using Direct Compute by Gareth ThomasHoly smoke! Faster Particle Rendering using Direct Compute by Gareth Thomas
Holy smoke! Faster Particle Rendering using Direct Compute by Gareth Thomas
 
Lighting the City of Glass
Lighting the City of GlassLighting the City of Glass
Lighting the City of Glass
 
Rendering AAA-Quality Characters of Project A1
Rendering AAA-Quality Characters of Project A1Rendering AAA-Quality Characters of Project A1
Rendering AAA-Quality Characters of Project A1
 

Similar a Tales from the Optimization Trenches - Unite Copenhagen 2019

Running Dicom Visualization On The Cell (Ps3) Rsna Poster Presentation
Running Dicom Visualization On The Cell (Ps3) Rsna Poster PresentationRunning Dicom Visualization On The Cell (Ps3) Rsna Poster Presentation
Running Dicom Visualization On The Cell (Ps3) Rsna Poster Presentation
broekemaa
 
(Manual) auto cad 2000 visual lisp tutorial (autocad)
(Manual) auto cad 2000 visual lisp tutorial (autocad)(Manual) auto cad 2000 visual lisp tutorial (autocad)
(Manual) auto cad 2000 visual lisp tutorial (autocad)
Ketut Swandana
 

Similar a Tales from the Optimization Trenches - Unite Copenhagen 2019 (20)

Debug, Analyze and Optimize Games with Intel Tools
Debug, Analyze and Optimize Games with Intel Tools Debug, Analyze and Optimize Games with Intel Tools
Debug, Analyze and Optimize Games with Intel Tools
 
Debug, Analyze and Optimize Games with Intel Tools - Matteo Valoriani - Codem...
Debug, Analyze and Optimize Games with Intel Tools - Matteo Valoriani - Codem...Debug, Analyze and Optimize Games with Intel Tools - Matteo Valoriani - Codem...
Debug, Analyze and Optimize Games with Intel Tools - Matteo Valoriani - Codem...
 
Debug, Analyze and Optimize Games with Intel Tools - Matteo Valoriani - Codem...
Debug, Analyze and Optimize Games with Intel Tools - Matteo Valoriani - Codem...Debug, Analyze and Optimize Games with Intel Tools - Matteo Valoriani - Codem...
Debug, Analyze and Optimize Games with Intel Tools - Matteo Valoriani - Codem...
 
Getting Space Pirate Trainer* to Perform on Intel® Graphics
Getting Space Pirate Trainer* to Perform on Intel® GraphicsGetting Space Pirate Trainer* to Perform on Intel® Graphics
Getting Space Pirate Trainer* to Perform on Intel® Graphics
 
Gpudigital lab for english partners
Gpudigital lab for english partnersGpudigital lab for english partners
Gpudigital lab for english partners
 
Gpu digital lab english version
Gpu digital lab english versionGpu digital lab english version
Gpu digital lab english version
 
Performance_Programming
Performance_ProgrammingPerformance_Programming
Performance_Programming
 
Nexmark with beam
Nexmark with beamNexmark with beam
Nexmark with beam
 
Running Dicom Visualization On The Cell (Ps3) Rsna Poster Presentation
Running Dicom Visualization On The Cell (Ps3) Rsna Poster PresentationRunning Dicom Visualization On The Cell (Ps3) Rsna Poster Presentation
Running Dicom Visualization On The Cell (Ps3) Rsna Poster Presentation
 
(Manual) auto cad 2000 visual lisp tutorial (autocad)
(Manual) auto cad 2000 visual lisp tutorial (autocad)(Manual) auto cad 2000 visual lisp tutorial (autocad)
(Manual) auto cad 2000 visual lisp tutorial (autocad)
 
byteLAKE's CFD Suite (AI-accelerated CFD) (2024-02)
byteLAKE's CFD Suite (AI-accelerated CFD) (2024-02)byteLAKE's CFD Suite (AI-accelerated CFD) (2024-02)
byteLAKE's CFD Suite (AI-accelerated CFD) (2024-02)
 
It Doesn't Have to Be Hard: How to Fix Your Performance Woes
It Doesn't Have to Be Hard: How to Fix Your Performance WoesIt Doesn't Have to Be Hard: How to Fix Your Performance Woes
It Doesn't Have to Be Hard: How to Fix Your Performance Woes
 
Renesas DevCon 2010: Starting a QT Application with Minimal Boot
Renesas DevCon 2010: Starting a QT Application with Minimal BootRenesas DevCon 2010: Starting a QT Application with Minimal Boot
Renesas DevCon 2010: Starting a QT Application with Minimal Boot
 
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...
Webinar: Começando seus trabalhos com Machine Learning utilizando ferramentas...
 
Optimizing unity games (Google IO 2014)
Optimizing unity games (Google IO 2014)Optimizing unity games (Google IO 2014)
Optimizing unity games (Google IO 2014)
 
Optimizing HDRP with NVIDIA Nsight Graphics – Unite Copenhagen 2019
Optimizing HDRP with NVIDIA Nsight Graphics – Unite Copenhagen 2019Optimizing HDRP with NVIDIA Nsight Graphics – Unite Copenhagen 2019
Optimizing HDRP with NVIDIA Nsight Graphics – Unite Copenhagen 2019
 
[Unite Seoul 2019] Mali GPU Architecture and Mobile Studio
[Unite Seoul 2019] Mali GPU Architecture and Mobile Studio [Unite Seoul 2019] Mali GPU Architecture and Mobile Studio
[Unite Seoul 2019] Mali GPU Architecture and Mobile Studio
 
Forts and Fights Scaling Performance on Unreal Engine*
Forts and Fights Scaling Performance on Unreal Engine*Forts and Fights Scaling Performance on Unreal Engine*
Forts and Fights Scaling Performance on Unreal Engine*
 
Monte Carlo on GPUs
Monte Carlo on GPUsMonte Carlo on GPUs
Monte Carlo on GPUs
 
[Gstar 2013] Unity Security
[Gstar 2013] Unity Security[Gstar 2013] Unity Security
[Gstar 2013] Unity Security
 

Más de Unity Technologies

Más de Unity Technologies (20)

Build Immersive Worlds in Virtual Reality
Build Immersive Worlds  in Virtual RealityBuild Immersive Worlds  in Virtual Reality
Build Immersive Worlds in Virtual Reality
 
Augmenting reality: Bring digital objects into the real world
Augmenting reality: Bring digital objects into the real worldAugmenting reality: Bring digital objects into the real world
Augmenting reality: Bring digital objects into the real world
 
Let’s get real: An introduction to AR, VR, MR, XR and more
Let’s get real: An introduction to AR, VR, MR, XR and moreLet’s get real: An introduction to AR, VR, MR, XR and more
Let’s get real: An introduction to AR, VR, MR, XR and more
 
Using synthetic data for computer vision model training
Using synthetic data for computer vision model trainingUsing synthetic data for computer vision model training
Using synthetic data for computer vision model training
 
The Tipping Point: How Virtual Experiences Are Transforming Global Industries
The Tipping Point: How Virtual Experiences Are Transforming Global IndustriesThe Tipping Point: How Virtual Experiences Are Transforming Global Industries
The Tipping Point: How Virtual Experiences Are Transforming Global Industries
 
Unity Roadmap 2020: Live games
Unity Roadmap 2020: Live games Unity Roadmap 2020: Live games
Unity Roadmap 2020: Live games
 
Unity Roadmap 2020: Core Engine & Creator Tools
Unity Roadmap 2020: Core Engine & Creator ToolsUnity Roadmap 2020: Core Engine & Creator Tools
Unity Roadmap 2020: Core Engine & Creator Tools
 
How ABB shapes the future of industry with Microsoft HoloLens and Unity - Uni...
How ABB shapes the future of industry with Microsoft HoloLens and Unity - Uni...How ABB shapes the future of industry with Microsoft HoloLens and Unity - Uni...
How ABB shapes the future of industry with Microsoft HoloLens and Unity - Uni...
 
Unity XR platform has a new architecture – Unite Copenhagen 2019
Unity XR platform has a new architecture – Unite Copenhagen 2019Unity XR platform has a new architecture – Unite Copenhagen 2019
Unity XR platform has a new architecture – Unite Copenhagen 2019
 
Turn Revit Models into real-time 3D experiences
Turn Revit Models into real-time 3D experiencesTurn Revit Models into real-time 3D experiences
Turn Revit Models into real-time 3D experiences
 
How Daimler uses mobile mixed realities for training and sales - Unite Copenh...
How Daimler uses mobile mixed realities for training and sales - Unite Copenh...How Daimler uses mobile mixed realities for training and sales - Unite Copenh...
How Daimler uses mobile mixed realities for training and sales - Unite Copenh...
 
How Volvo embraced real-time 3D and shook up the auto industry- Unite Copenha...
How Volvo embraced real-time 3D and shook up the auto industry- Unite Copenha...How Volvo embraced real-time 3D and shook up the auto industry- Unite Copenha...
How Volvo embraced real-time 3D and shook up the auto industry- Unite Copenha...
 
QA your code: The new Unity Test Framework – Unite Copenhagen 2019
QA your code: The new Unity Test Framework – Unite Copenhagen 2019QA your code: The new Unity Test Framework – Unite Copenhagen 2019
QA your code: The new Unity Test Framework – Unite Copenhagen 2019
 
Engineering.com webinar: Real-time 3D and digital twins: The power of a virtu...
Engineering.com webinar: Real-time 3D and digital twins: The power of a virtu...Engineering.com webinar: Real-time 3D and digital twins: The power of a virtu...
Engineering.com webinar: Real-time 3D and digital twins: The power of a virtu...
 
Supplying scalable VR training applications with Innoactive - Unite Copenhage...
Supplying scalable VR training applications with Innoactive - Unite Copenhage...Supplying scalable VR training applications with Innoactive - Unite Copenhage...
Supplying scalable VR training applications with Innoactive - Unite Copenhage...
 
XR and real-time 3D in automotive digital marketing strategies | Visionaries ...
XR and real-time 3D in automotive digital marketing strategies | Visionaries ...XR and real-time 3D in automotive digital marketing strategies | Visionaries ...
XR and real-time 3D in automotive digital marketing strategies | Visionaries ...
 
Real-time CG animation in Unity: unpacking the Sherman project - Unite Copenh...
Real-time CG animation in Unity: unpacking the Sherman project - Unite Copenh...Real-time CG animation in Unity: unpacking the Sherman project - Unite Copenh...
Real-time CG animation in Unity: unpacking the Sherman project - Unite Copenh...
 
Creating next-gen VR and MR experiences using Varjo VR-1 and XR-1 - Unite Cop...
Creating next-gen VR and MR experiences using Varjo VR-1 and XR-1 - Unite Cop...Creating next-gen VR and MR experiences using Varjo VR-1 and XR-1 - Unite Cop...
Creating next-gen VR and MR experiences using Varjo VR-1 and XR-1 - Unite Cop...
 
What's ahead for film and animation with Unity 2020 - Unite Copenhagen 2019
What's ahead for film and animation with Unity 2020 - Unite Copenhagen 2019What's ahead for film and animation with Unity 2020 - Unite Copenhagen 2019
What's ahead for film and animation with Unity 2020 - Unite Copenhagen 2019
 
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Tales from the Optimization Trenches - Unite Copenhagen 2019

  • 1.
  • 2. Tales from the Optimization Trenches Ignacio Liverotti Unity Technologies
  • 3. About me and what I do here at Unity 3 — Joined Unity as a Software Engineer in 2015 — Became a Developer Relations Engineer in 2018 — I visit our Enterprise customers and help them resolve technical issues affecting their projects
  • 4. Project Reviews 4 — Multi-day engagement: — We travel to our customers’ offices — Review their projects — Identify problems — Some of them are resolved onsite — We investigate and recommend solutions for the rest
  • 5. Project Reviews 5 — Types of problems: – Runtime performance – Build/patch size – Load times – Workflow issues – Build times
  • 6. Today’s plan 6 — Introduction to optimization and profiling in Unity — CPU optimization — GPU optimization — Memory footprint optimization — (Optimization) rules to live by
  • 8. What is optimization? 8 — Modifying a project so that an aspect of it is more efficient or uses fewer resources (CPU, GPU, etc)
  • 9. Why do we want to optimize? 9 — To pass the certification requirements imposed by the various distribution platforms — To reduce battery consumption — To deploy our project to a wider range of target devices — To streamline our production process
  • 10. Optimization involves a lot more than rewriting code!
  • 11. What else does it involve? 11 — Reviewing assets (texture size, format, poly count, audio files sample freq, etc.) — Reviewing the Project Settings — Reviewing the assets’ settings — Simplifying our solutions
  • 12. The first step in our (optimization) journey: profiling! 12 — Using tools to gather actual data on how resources are being used — This data will drive our optimization efforts — We don’t want to optimize based on “guesses” — We want the tools to let us know where the problems are
  • 13. A note about optimization ‘tips’ and ‘advice’ 13 — Don’t apply optimization advice blindly – Certain pieces of advice are always applicable – But good advice applied in the wrong situation can make things worse – A technique that worked in a certain project and platform might not work for you
  • 15. What is the goal of CPU optimization? 15 — To reduce the stress on the CPU – Because the CPU is the actual performance bottleneck – Or to free the CPU so that we can do more
  • 16. How do we achieve that? 16 — By using more efficient combinations of algorithms and data structures — By aiming for nearly zero per-frame allocations – The GC algorithm can be quite CPU intensive!
  • 17. Tools of the trade: CPU 17 — Unity Profiler — Unity Profile Analyzer – Don’t miss the next talk! — Xcode Instruments — Intel VTune Amplifier — Consoles have their own proprietary tools
  • 18. Tools of the trade: CPU 18
  • 19. Tools of the trade: CPU 19
  • 20. Example 1: Per-frame memory allocations 20 — Scenario: Management game for mobile platforms that ‘feels’ slow during gameplay — We take a capture using the Unity Profiler and uncheck all items, except for ‘GarbageCollector’:
  • 21. The Garbage Collector is running once every three frames
  • 22. Example 1: Per-frame memory allocations 22 — Our hypothesis: Our code is allocating memory on a per-frame basis — Let’s select a random frame in the Unity Profiler:
  • 23. Example 1: Per-frame memory allocations 23 — What about 10 frames later?
  • 24. 400 KB of allocations per frame @60 FPS ~= 24 MB of allocations per second
  • 25. Example 1: Per-frame memory allocations 25 — Let’s dig into the ‘GC Alloc’ column:
  • 26. Example 1: Per-frame memory allocations 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 void Update() { ProcessScore(); ProcessHitPoints(); // More game logic methods. } private void ProcessScore() { Debug.Log("GameLogic.ProcessScore(). Score: " + Score.ToString("00000")); // Score processing logic. } private void ProcessHitPoints() { Debug.Log("GameLogic.ProcessHitPoints(). HitPoints: " + HitPoints.ToString("00000")); // Hit points processing logic. } 26
  • 27. Example 1: Per-frame memory allocations 27 — From https://docs.unity3d.com/Manual/PlatformDependentCompilation.html using System.Diagnostics; public static class Logging { [Conditional ("ENABLE_LOG")] static public void Log (object message) { UnityEngine.Debug.Log (message); } } 1 2 3 4 5 6 7 8 9 10
  • 28. Example 1: Per-frame memory allocations 28 — If we want to see the log messages, we need to add ENABLE_LOG to the list of defined symbols:
  • 29. Example 1: Per-frame memory allocations 29 — Let’s remove ENABLE_LOG and reprofile:
  • 30. Example 1: Per-frame memory allocations 30 — Zero per-frame allocations — No GC spikes! 
  • 31. Takeaway: use the Unity Profiler to understand where your managed allocations are coming from and fix them
  • 32. Example 2: GC spikes in a fast-paced game 32 — Scenario: Mobile racing game where the frame rate needs to be steady — Common approach: let the GC do its work – The problem it causes: When the GC kicks in, program execution actually stops – Also, the larger the managed heap, the longer it takes for the GC algorithm to complete
  • 33. Example 2: GC spikes in a fast-paced game 33 — GC capture:
  • 34. Example 2: GC spikes in a fast-paced game 34 — What we recommend: – Unload all resources when transitioning from the menu to the ‘racing’ scene – Allocate a pool of objects – Optimize the frame time as much as possible – Enable the incremental garbage collector
  • 35. Example 2: GC spikes in a fast-paced game 35 — The incremental GC was introduced in the early 2019 development cycle — Instead of causing a single, long interruption, it splits the work across multiple slices
  • 36. Example 2: GC spikes in a fast-paced game 36 — Incremental GC capture:
  • 37. Example 2: GC spikes in a fast-paced game 37 — Enable it via the Player Settings menu:
  • 38. Takeaway: use the incremental GC and remember to optimize the frame time as much as possible so that we can give it room to do its job
  • 40. What are the goals of GPU optimization? 40 — Reduce the stress on the GPU so that we can render our scene at the target frame rate — Free up the GPU for performing other tasks (including offloading work from the CPU via compute shaders)
  • 41. How do we achieve that? 41 — Minimizing the number of unnecessary rendering operations — Reducing the amount of data sent to the GPU — Minimizing the number of state changes (‘set pass’ calls) — Optimizing our most expensive shaders
  • 42. Tools of the trade: GPU 42 — Unity Frame Debugger — RenderDoc — NVidia NSight — XCode Frame Capture — Intel GPA — Consoles have their own proprietary tools
  • 43. Tools of the trade: GPU 43
  • 44. Example 3: Strategy game for mobile 44 — Scenario: A customer working on strategy game for iOS/Android were experiencing framerate issues — We profiled it using Xcode Frame Capture and saw a warning message saying that we were sending too much geometry to the GPU
  • 45. Example 3: Strategy game for mobile 45
  • 46. Example 3: Strategy game for mobile 46
  • 47. Example 3: Strategy game for mobile 47 — Both draw calls have the same geometry as input:
  • 48. — But the output of one of them is taking significantly more screen real state in the final frame than the other one! Example 3: Strategy game for mobile 48
  • 49. Example 3: Strategy game for mobile 49
  • 50. Do we need that much geometry for the model in the background? We probably don’t.
  • 51. Example 3: Strategy game for mobile 51 — Our advice to the team: create LODs for the assets
  • 52. Example 3: Strategy game for mobile 52
  • 54. Takeaway: by understanding our requirements and the tools and techniques at our disposal, we’ve achieved a nearly 40% reduction without observable differences in the final output
  • 55. Example 4: Sprite rendering 55 — Scenario: A customer working on a top down tile-based strategy game for PC experienced very low frame rates when deploying to mobile and WebGL
  • 56. Example 4: Sprite rendering 56 — We profiled the game using the Unity Profiler and saw high frame times — Most of that time was spent on rendering — The CPU was too busy creating and sending rendering commands to the GPU
  • 57. Example 4: Sprite rendering 57 — The Frame Debugger revealed one draw call per tile (several hundred draw calls in the real project!)
  • 58. Example 4: Sprite rendering 58
  • 59. Example 4: Sprite rendering 59
  • 60. Example 4: Sprite rendering 60 — Let’s look at the SpriteRenderer components: – They all share the same material!
  • 61. Let’s look at the code…
  • 62. Example 4: Sprite rendering 1 2 3 4 5 6 7 8 9 10 11 12 using UnityEngine; public class Tile : MonoBehaviour { private Material _spriteRendererMaterial; void PreprocessMethod() { var spriteRenderer = GetComponent<SpriteRenderer>(); _spriteRendererMaterial = spriteRenderer.material; } } 62 This statement creates a copy of the first material from this SpriteRenderer, assigns it to the SpriteRenderer and returns it.
  • 63. Example 4: Sprite rendering 1 2 3 4 5 6 7 8 9 10 11 12 using UnityEngine; public class Tile : MonoBehaviour { private Material _spriteRendererMaterial; void PreprocessMethod() { var spriteRenderer = GetComponent<SpriteRenderer>(); _spriteRendererMaterial = spriteRenderer.sharedMaterial; } } 63
  • 64. Example 4: Sprite rendering 64 — All tiles are now drawn in the same batch — The game was successfully deployed to mobile and WebGL  — And the performance of the standalone version improved as well!
  • 65. Takeaway: identifying the problem and understanding the underlying issue allowed us to trim several hundred draw calls per frame
  • 67. — Fit on devices that don’t have a large amount of memory — Improve loading times — Being able to add more content — Avoid hard-crashes due to out of memory situations — Improving overall performance by shuffling around less data during runtime What are the goals of memory footprint reduction? 67
  • 68. Tools of the trade: memory footprint — Unity Memory Profiler — Xcode Instruments Allocations — Xcode Instruments VM Tracker — Consoles have their own proprietary tools 68
  • 69. Tools of the trade: memory footprint 69
  • 70. Tools of the trade: memory footprint 70
  • 71. Example 5: Built-in shaders duplication 71 — A customer project has a large number of materials that use Unity’s Standard shader:
  • 72. Example 5: Built-in shaders duplication 72 — Each Material is stored in its own AssetBundle:
  • 73. Example 5: Built-in shaders duplication 73 — A memory snapshot of the project reveals that there are multiple instances of the Standard shader in memory:
  • 74. Example 5: Built-in shaders duplication 74 — This happens because the Standard shader is one of Unity’s built-in shaders — As such, it cannot be explicitly included in an AssetBundle — And it will be implicitly included in every AssetBundle that has a material with a reference to it
  • 75. Example 5: Built-in shaders duplication 75 — What we recommend instead: – Download a copy of the built-in shaders – Make a copy of the Standard Shader, rename it (e.g., ‘Unite 2019 Standard’) and add it to its own AssetBundle – Fix the materials so that they use the new renamed shader – Rebuild the AssetBundles
  • 76. Example 5: Built-in shaders duplication 76 — After rebuilding and taking a new memory snapshot: – We now have a single instance of our custom ‘Unite 2019 Standard’ shader 
  • 77. Takeaway: by using the right tools and understanding the internals of the engine, we were able to eliminate duplicates in memory
  • 78. 78 — Scenario: A customer is porting a desktop game to mobile platforms and it keeps crashing on low-end devices due to its high memory footprint Example 6: Unable to run the game on mobile
  • 79. Example 6: Unable to run the game on mobile 79 — A memory snapshot of the project reveals this:
  • 80. 80 Example 6: Unable to run the game on mobile
  • 81. 81 — Let’s look at the settings for these assets: Example 6: Unable to run the game on mobile
  • 82. 82 — ‘Decompress on load’ option description from the Unity manual: – Audio files will be decompressed as soon as they are loaded – This option should be used for smaller compressed sounds to avoid the performance overhead of decompressing on the fly – Be aware that decompressing Vorbis-encoded sounds on load will use about ten times more memory than keeping them compressed, so don’t use this option for large files Example 6: Unable to run the game on mobile
  • 83. Do we have other options? We do! There’s a ‘Streaming’ option
  • 84. 84 — Description from the Unity manual: – Decode audio on the fly – Uses a minimal amount of memory to buffer compressed data – The data is incrementally read from the disk and decoded on the fly Example 6: Unable to run the game on mobile
  • 85. 85 — Let’s change the load type to ‘Streaming’: Example 6: Unable to run the game on mobile
  • 86. 86 — And take another memory snapshot: Example 6: Unable to run the game on mobile
  • 87. 87 0.5MB Set to ‘Streaming’ ~99% Memory footprint reduction 53MB Set to ‘Decompress on load’
  • 88. Takeaway: understanding our requirements and using the correct settings allowed us to reduce the audio memory footprint by ~99%
  • 89. 89 — These problems can be avoided! — Let’s not catch them via the Memory Profiler — Instead, let’s use an AssetPostprocessor and create rules: – Background music assets should be set to streaming – SFX should be set to decompress on load – Etc Example 6: Unable to run the game on mobile
  • 92. (Optimization) rules to live by 92 — Don’t assume where the bottlenecks are, always profile first — Profile on the target device — Profile early, profile often — Don’t apply several fixes simultaneously, tackle one problem at the time — Apply the ‘optimization triad’: – Optimize your assets – Update fewer things – Draw less stuff 