unity optimization tips, tricks and tools
TRANSCRIPT
Unity Optimization: Tips, Tricks, and ToolsKelly Gawne, Games EngineerSeptember 2016
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice Intel Confidential 2
Agenda
• Intro• Optimization Thoughts• Profiling• Optimizations• Questions
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 3
Optimization Ideology
• Don’t.• Don’t yet.• Profile and identify your largest bottleneck.• Test, address, test again.• Repeat as desired.
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 4
Where is the bottleneck?
The Core Optimization Question:
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 5
CPU Limitations• Draw Call Preparation• Scripting• Physics • Memory• Etc.
GPU Limitations• Memory bandwidth• Resolution• Shader or Geometric
Complexity• Lighting• Etc.
The Core Optimization Question: CPU or GPU?
Profiling
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 7
A Profiling Toolbox
Unity Profiler
Unity Render Stats Overlay Intel GPA System Analyzer
Intel GPA Frame Analyzer
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 8
Unity Render Stats
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 9
Unity Profiler
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 10
Intel GPA System Analyzer
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 11
Intel GPA Frame Analyzer
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 12
Unity: Watch the profilerDecrease resolution. If framerate improves, GPU-bound
GPA:Use the experiments:• If turning on NULL HW
increases framerate, GPU-Bound
• If NULL HW doesn’t help but DISABLE DRAW CALLS does, driver-bound
• If neither help, CPU-bound
Where’s that bottleneck?
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 13
CPU:Check the Unity Profiler: What’s so expensive?Is it a constant cost?Are there hitches or frametime spikes?
GPU:Is there a killer draw call?Are lots of little calls eating up frametime?Is everything rendered essential?
Where next?
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 14
Profile, Optimize, Profile
Optimization
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 16
Scripting
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 17
Script Frustum Culling Co-routines
Help, I Can’t Stop Update()ing
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 18
Scripting Fades
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 19
Scripting with Memory in Mind
Garbage Collection (GC)
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 20
Object PoolingReuse objects in a pool instead of instantiating and destroying them.Great for: bullets, explosionsCatch 22:
Heap memory cost, could trigger GCSlower GCLong-lasting, large groups aren’t ideal
Garbage Collection Avoidance• Use structs instead of classes• Cache references to objects• Minimize runtime allocations
Scripting with Memory in Mind
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 21
Physics
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 22
Physics
• Use primitive colliders over mesh colliders
• Tweak fixed delta time• Don’t move static colliders• If it needs to move, add a rigidbody
and uncheck static
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 23
Model Geometry
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 24
Model Geometry
The number of vertices processed by the GPU is greater than the number of geometric vertices in the model.
Why? Vertices can be split for multiple normals/UV coordinates/etc
Two Good Modeling Rules:1. Don’t use more triangles than you need. 2. Reduce UV seams and hard edges when possible
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 25
Model Geometry: LOD
• Switch out mesh based on objects distance from camera
• Customizable camera distances• Dramatic reduction in memory requirements by
frame
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 26
Level of Detail (LOD)
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 27
Model Geometry: Occlusion Culling
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 28
Model Geometry: Occlusion Culling
Be sure to set occlusion areas, Unity defaults to using the whole scene
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 29
Batching
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 30
Batching
The number of vertices processed by the GPU is greater than the number of geometric vertices in the model.
Why? Vertices can be split for multiple normals/UV coordinates/etc
Two Good Modeling Rules:1. Don’t use more triangles than you need. 2. Reduce UV seams and hard edges when possible
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 31
Static BatchingUnity automatically performsDoes not affect culling (unlike manual)No performance gain unless material is shared texture atlasCons: Memory overhead
Dynamic BatchingFor small meshes (<900 vertex attributes)Incurs per-vertex CPU overhead, only a win if the CPU vertex transform work < extra draw call expense.
Batching
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 32
Textures and Shaders
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 33
Textures
• Compress when possible
• Generate Mipmaps• UNLESS 2D game, UI element• 33% more memory per
texture
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 34
Texture Atlasing
Combine multiple textures into oneTexture2D.PackTextures.
Why?•Reduce Material and Texture count•Reduce engine overhead on Material setup•Reduce engine overhead on Texture switch•Allow for more static batching
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 35
Shaders
Built-ins aren’t built for your gameOnly compute what you need• Reduce complex math operations (e.g. pow, exp, log, cos, sin, tan).
Consider a lookup texture if applicable• Avoid writing your own versions of operations (e.g. normalize, dot,
inversesqrt) as the built-in's ensure the driver can optimize as much as possible.
• Float v. half v. fixed is largely ignored on desktop GPUs (everything is float on modern GPUs). Worry about it for mobile if you need it
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 36
Lighting and Shadows
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 37
Pros:Uses per-pixel, per-vertex and SHReal-time shadows and per-pixel effects
Cons:Can lead to many draws to same pixel
Lighting Path Quick Notes: Forward
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 38
Pros:Lighting complexity unrelated to sceneTrade heavy lighting computation for memory
Lighting Path Quick Notes: Deferred
Cons:Semi-transparent not supported directlyNo anti-aliasingNo Receive Shadows flag support
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 39
Lightmapping
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 40
Lightmapping
Mark all static geometry.Place light probes to fill all dynamic object pathsBake
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 41
HDR
If you’re using HDR effect like Bloom and Flare using deferred rendering, check HDR to reduce draw calls.
Each camera has an HDR checkbox
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 42
ShadowsAvoid Realtime ShadowsSoft shadows have a greater GPU overheadUse cascaded shadow maps for directional lightsTweak your shadow settings:• Shadow Filtering: hard/soft• Shadow Resolution: Resolution of shadow map• Shadow Projection: Stable/Close fit• Shadow Cascades: 0, 2, 4• Shadow Distance: Max distance object’s shadow can project
Final Thoughts
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice 44
Final Thoughts
• Design with optimization in mind
• Know what you’re optimizing for• If there’s an issue, profile first.• Profile, optimize, profile.• Make fast, fantastic games.
Questions?Kelly GawneIntel Games [email protected]
45
software.intel.com/gpahttps://github.com/GameTechDev/UnityPerformanceSandbox
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice Intel Confidential
Legal Disclaimer & Optimization NoticeINFORMATION IN THIS DOCUMENT IS PROVIDED “AS IS”. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO THIS INFORMATION INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark and MobileMark, are measured using specific computer systems, components, software, operations and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products.
Copyright © 2014, Intel Corporation. All rights reserved. Intel, Pentium, Xeon, Xeon Phi, Core, VTune, Cilk, and the Intel logo are trademarks of Intel Corporation in the U.S. and other countries.
Optimization NoticeIntel’s compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.
Notice revision #2011080447
Backup
48
Copyright © 2015, Intel Corporation. All rights reserved. *Other names and brands may be claimed as the property of others.
Optimization Notice Intel Confidential 49
Intel® GPA Release Notes
https://software.intel.com/en-us/articles/intel-gpa-release-notes