【unite 2017 tokyo】unity最適化講座...

57

Upload: unitytechnologiesjapan

Post on 21-Jan-2018

28.185 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
Page 2: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Ian DundoreLead Developer Relations Engineer, Unity

Page 3: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Optimizing UnitySaving CPU & Memory, because you can.

Page 4: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

What’ll we cover today?

• Primary focus will be CPU optimization. • Transform component • Animator • Physics

• Finish with some tips for saving memory.

Page 5: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Always, always profile!

Page 6: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Transforms! Not as simple as they look.

Page 7: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Transforms?

• Every GameObject has one. • When they change, they send out messages. • C++: “OnTransformChanged”

• When reparented, they send out MORE messages! • C#: “OnBeforeTransformParentChanged” • C#: “OnTransformParentChanged”

• Used by many internal Unity Components. • Physics, Renderers, UnityUI…

Page 8: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

OnTransformChanged?

• Sent every time a Transform changes.

• Three ways to cause these messages: • Changing position, rotation or scale in C# • Moved by Animators • Moved by Physics

• 1 change = 1 message!

Page 9: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Why is this expensive?

• Message is sent to all Components on Transform & all Children • Yes, ALL.

• Physics components will update the Physics scene. • Renderers will recalculate their bounding boxes. • Particle systems will update their bounding boxes.

Page 10: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

void Update() {

transform.position += new Vector3(1f, 1f, 1f); transform.rotation += Quaternion.Euler(45f, 45f, 45f);

}

Page 11: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

void Update() {

transform.position += new Vector3(1f, 1f, 1f); transform.rotation += Quaternion.Euler(45f, 45f, 45f);

} X

Page 12: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

void Update() {

transform.SetPositionAndRotation( transform.position + new Vector3(1f, 1f, 1f), transform.rotation + Quaternion.Euler(45f, 45f, 45f) );

}

Page 13: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Collect your transform updates!

• Many systems moving a Transform around?

• Add up all the changes, apply them once.

• If changing both position & rotation, use SetPositionAndRotation • New API, added in 5.6 • Eliminates duplicate messages

Page 14: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

What system has lots of moving Transforms?

Page 15: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
Page 16: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

100 Unity-chans!

• Over 15,000 Transforms!

• How will it perform?

Page 17: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Performance Test (Unity 5.6)

times in ms iPad Air 2 Macbook Pro, 2017

Unoptimized 33.35 6.06

Optimized 26.96 3.37

Timings are only from Animators

Page 18: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

“Optimized”?

Page 19: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

What does it do?

• Reorders animation data for better multithreading.

• Eliminates extra transforms in the model’s Transform hierarchy. • Need some, but not all? Add them to the “Extra Transforms” list!

• Allows Mesh Skinning to be multithreaded.

Page 20: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

“Optimized”?

Page 21: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Physics!

Page 22: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

What affects the cost of Physics?

• Two major operations that cost time:

• Physics simulation • Updating Rigidbodies

• Physics queries • Raycast, SphereCast, etc.

Page 23: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Scene complexity is important!

• All physics costs are affected by the complexity and density of a scene.

• The type of Colliders used has a big impact on cost. • Box colliders & sphere colliders are cheapest • Mesh colliders are very expensive

• Simple setup: Create some number of colliders, cast rays.

Page 24: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Cost of 1000 Raycasts

times in ms 10 Colliders 100 Colliders 1000 Colliders

Sphere 0.27 0.48 1.53

Box 0.34 0.42 1.67

Mesh 0.37 0.53 2.27

Objects created in a 25-meter sphere

Page 25: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Density is important!

times in ms 10 meters 25 meters 50 meters 100 meters

Sphere 2.13 1.28 1.08 0.78

1000 Raycasts through 1000 Spheres

Page 26: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Why is complexity important?

• Physics queries occur in three steps.

• 1: Gather list of potential collisions, based on world-space.

• 2: Cull list of potential collisions, based on layers.

• 3: Test each potential collision to find actual collisions.

Page 27: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Why is complexity important?

• Physics queries occur in three steps.

• 1: Gather list of potential collisions, based on world-space. • Done by PhysX (“broad phase”)

• 2: Cull list of potential collisions, based on layers. • Done by Unity

• 3: Test each potential collision to find actual collisions. • Done by PhysX (“midphase” & “narrow phase”) • Most expensive step

Page 28: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Reduce number of potential collisions!

• PhysX has an internal world-space partitioning system. • Divides world into smaller regions, which track contents.

• Longer rays may require querying more regions in the world. • Benefit of limiting ray length depends on scene density!

• Use maxDistance parameter of Raycast! • Limits the world-space involved in a query. • Physics.Raycast(myRay, 10f);

Page 29: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Control maxDistance!times in ms 10 meters 25 meters 50 meters 100 meters

1 meter 1.85 1.02 0.49 0.50

10 meters 2.10 1.19 0.91 0.53

100 meters 2.11 1.36 1.07 0.77

Infinite 2.13 1.28 1.08 0.78

1000 Raycasts through 1000 Spheres

Page 30: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Edit the physics layers!

• Consider making special layers for special types of entities in your game • Help the system cull unwanted results.

Page 31: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Trade accuracy for performance

• Reduce the physics time-step. • Running at 30FPS? Don’t run physics at 50FPS!

• Reduces accuracy of collisions, so test changes carefully. • Most games can accept timesteps of 0.04 or 0.08

Page 32: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

[RequireComponent(typeof(Rigidbody))] class MyMonoBehaviour : MonoBehaviour { void Update() {

transform.SetPositionAndRotation(…); } }

Page 33: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

[RequireComponent(typeof(Rigidbody))] class MyMonoBehaviour : MonoBehaviour { void Update() {

transform.SetPositionAndRotation(…); } } X

Page 34: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

[RequireComponent(typeof(Rigidbody))] class MyMonoBehaviour : MonoBehaviour { Rigidbody rb; void Awake() { rb = GetComponent<Rigidbody>(); } void Update() {

rb.MovePosition(…); rb.MoveRotation(…);

} }

Page 35: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Common errors: Rigidbody movement

• Do not move GameObjects with Rigidbodies via their Transform • Use Rigidbody.MovePosition, Rigidbody.MoveRotation

500 objects MovePosition & MoveRotation

Transform SetPositionAndRotation

FixedUpdate 0.45 0.90

Page 36: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Memory time!

Page 37: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

IL2CPP Platforms: Memory Profiler

• Available on Bitbucket • https://bitbucket.org/Unity-Technologies/memoryprofiler

• Requires IL2CPP to get accurate information. • Shows all UnityEngine.Objects and C# objects in memory. • Includes native allocations, such as the shadowmap

Page 38: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Examining a memory snapshot

Page 39: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Examining a memory snapshot

Page 40: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Examining a memory snapshot

?!

Page 41: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Examining a memory snapshot

Page 42: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

This is a shadowmap… do we need one?

Page 43: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Look at the HideFlags

Page 44: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

HideAndDontSave?

• Value in the HideFlags enum. • Includes 3 values: • HideInHierarchy • DontSave • DontUnloadUnusedAsset

• Mistake: Setting in-game assets to use this HideFlag. • Assets loaded with HideAndDontSave flag will never be unloaded!

Page 45: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Examining a memory snapshot

Page 46: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Examining a memory snapshot

Page 47: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Examining a memory snapshot

Page 48: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Check your assets!

• Common for artists to add assets in very high resolutions.

• Does Unity-chan’s hair really need three 1024x1024 textures? • Reduce size? Achieve the same effect some other way?

Page 49: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Beware of Asset Duplication

• Common problem when placing Assets into Asset Bundles

• Assets in Resources & an Asset Bundle will be included in both • Will be seen as separate Assets in Unity • Separate copies will be loaded into memory

• Also happens when Asset Dependencies are not declared properly

Page 50: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Asset Dependencies

Material A Material B

Texture (shared)Shader A Shader B

Page 51: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Asset Dependencies

Material A Material B

Texture (shared)Shader A Shader B

Page 52: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Asset Dependencies

Material A Material B

Texture (shared)Shader A Shader B

Page 53: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Asset Dependencies

Material A Material B

Texture (shared)Shader A Shader BTexture

(shared)

Page 54: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Asset Dependencies

Material A Material B

Texture (shared)Shader A Shader B

Page 55: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Runtime texture creation

• Creating textures procedurally, or downloading via the web? • Make sure your textures are non-readable! • Read-write textures use twice as much memory!

• Use UnityWebRequest — defaults to non-readable textures • Use WWW.textureNonReadable instead of WWW.texture

Page 56: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Marking textures non-readable

• Pass nonReadable as true when calling Texture2D.LoadImage • Texture2D.LoadImage(“/path/to/image”, true);

• Call Texture2D.Apply when updates are done • Texture2D.Apply(true, true); • Updates mipmaps & marks as non-readable

• Texture2D.Apply(false, true); • Does not update mipmaps, but does mark as non-readable

Page 57: 【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~

Thank you!