Optimize Unity Game Performance


Objective

Learn how to optimize the performance of a demo game using the Adaptive Performance package from within Unity. The Adaptive Performance package provides you with tools, including relevant information from the mobile device. The tools allow you to edit different features in the game with useful scalers that give you the ability to improve the overall game performance.

Overview

Galaxy GameSDK delivers an interface between game application and device which helps developers optimize their games. Integrating Unity Adaptive Performance with Galaxy GameSDK allows Unity developers to use this feature on Unity Editor within Unity Package Manager and also customize their game contents by C# scripting. Game performance and quality settings can be adjusted in real-time by identifying device performance status and heat trends. Moreover, using a set of simple UI controls, you can scale the quality of the game to match the device platform.

Samsung Galaxy S10 / Note10 devices and onwards support Adaptive Performance.

Thermal Throttling

Mobile devices don’t have an active cooling system, so temperature continues to rise and it will limit the CPU / GPU clock to control heat and consequently degrading the performance.

The ideal goal is to make performance stable with low temperature. Adaptive Performance, primarily for performance and quality balance management, can help achieve this.

Thermal Warning

The warning system implemented in GameSDK can trigger the throttling system to adjust the CPU/GPU clock.

Adjust Quality

It also provides you with the option to adjust the quality of the game to maintain stable performance at different warning levels.

Dynamic Quality Setting

You can scale the quality of your game in real-time to meet the desired performance. This includes changes in network, animation, physics, level of detail, and visual effects.

Scalers

With the latest addition to the Adaptive Performance tool v2.0, available in the 2020 version of Unity, here’s a list of features that can be managed within the Adaptive Performance scalers in the project settings:

  • Frame Rate

  • Resolution

  • Batching

  • Level of Detail (LOD)

  • Look up Texture (LUT)

  • Multisample Anti-aliasing (MSAA)

  • Shadow Cascade

  • Shadow Distance

  • Shadow Map Resolution

  • Shadow Quality

  • Sorting

  • Transparency

  • View Distance

There are other available features in the Adaptive Performance settings menu that can be edited similarly with different scale ranges.

Variable Refresh Rate

Another latest addition provided in v2.0 is the Variable Refresh Rate (VRR). This will increase frame rate for devices that support refresh rates above 60.

Simulator

The newest and useful feature that allows you to facilitate mobile development would be the device simulator. This allows developers to display the game as it would be presented on a device.

MegaCity Demo Game

You will use a demo game, named MegaCity, and optimize its performance using Adaptive Performance tools.

MegaCity contains the following:

  • 8 million triangles
  • 6 million entities
  • 2000 cars
  • 100 audio source (heavy for mobile in general case)
  • Unity 2019.1
  • Visual Studio 2017
  • Android SDK + NDK 16b
  • Profiling: GPU Watch

GPUWatch

GPUWatch is a profiling tool for observing GPU activity in your app.

The following GPU related information are measured by the GPUWatch:

  1. FPS counters

    • Current

    • Average

  2. CPU / GPU load

    • CPU load

    • GPU load

    • GPU frequency

  3. Frame info

    • Render pass stats

    • Vertex / Fragment shader load

Set up your environment

You will need the following:

  • Run Unity Hub and add MegaCity project

Sample Code

Here is a sample code for you to start coding in this Code Lab. Download it and start your learning experience!

Adaptive Performance Sample Code
(1.93 KB)

Enable GPUWatch

How to enable GPUWatch:


Test out the base sample app

The base sample is written with classic simple approach:

  • Check CPU / GPU bottleneck
  • Trigger when thermal status is changed
  • Adjust CPU / GPU clock level
  • Adjust quality setting by setting LOD
  • Adjust target FPS

All of these can be controlled in real-time.

Install Adapative Performance package

Go to Editor > Windows > Package Manager > Install Adaptive Performance Package.

Modify C# script

Go to Editor > Window > Package Manager > Import Adaptive Performance Package.

Double click AdaptivePerformanceController.cs script file to edit.

Modify script as intended.

Build and launch APK

Select File > Build Settings.

Check that the device is connected and check the Development Build / Scripts Only Build option.

Then, click Patch and the installed APK on your device will be updated.

Measure performance

To check current application performance, check the FPS and CPU / GPU load counters.

As you can see, the performance is not very good. The next step is to check current status where you will try to:

  • Check performance status to know if there is any bottleneck

  • IAdaptivePerformance.PerformanceStatus.PerformanceMetrics.PerformanceBottleneck

    • CPU: Frame rate is limited by CPU processing

    • GPU: Frame rate is limited by GPU processing

    • TargetFrameRate: Can’t reach target frame rate

      • Frame rate is limited by Application.targetFrameRate. In this case, the application should consider lowering performance requirements.

Check thermal status

Register event handler to trigger thermal status and set handler into:

IThermalStatus.ThermalStatus.ThermalEvent

Check ThermalMetrics information in event handler:

  • WarningLevel

  • TemperatureLevel (0.0 ~ 1.0)

    Current normalized temperature level in the range of [0, 1]. A value of 0 means standard operation temperature and the device is not in a throttling state. A value of 1 means that the maximum temperature of the device is reached and the device is going into or is already in throttling state.

  • TemperatureTrend (-1.0 ~ 1.0)

    Current normalized temperature trend in the range of [-1, 1]. A value of 1 describes a rapid increase in temperature. A value of 0 describes a constant temperature. A value of -1 describes a rapid decrease in temperature. Please note that it takes at least 10s until the temperature trend may reflect any changes.

Adjust target frame rate

Change target frame rate when thermal status is changed. For example:

Change the quality setting

Change the quality setting by adjusting LOD in real-time.

Adjust CPU/GPU clock

Change CPU / GPU clock level by IDevicePerformanceControl.

IDevicePerformanceControl

  • AutomaticPerformanceControl

    When set to true (default) CpuLevel and GpuLevel are set automatically by Adaptive Performance.

  • MaxCpuPerformanceLevel

    The maximum valid CPU performance level you use with CpuLevel. The minimum value returned is MinCpuPerformanceLevel. This value does not change after startup is complete.

  • MaxGpuPerformanceLevel

    The maximum valid GPU performance level you use with GpuLevel. The minimum value returned is MinGpuPerformanceLevel. This value does not change after startup is complete.

  • CpuLevel

    The requested CPU performance level. Higher levels typically allow CPU cores to run at higher clock speeds. The consequence is that thermal warnings and throttling may happen sooner when the device cannot sustain high clock speeds. Changes are applied once per frame. It is recommended to set the CpuLevel as low as possible to save power. The valid value range is [MinCpuPerformanceLevel, MaxCpuPerformanceLevel].

  • GpuLevel

    The requested GPU performance level. Higher levels typically allow the GPU to run at higher clock speeds. The consequence is that thermal warnings and throttling may happen sooner when the device cannot sustain high clock speeds. Changes are applied once per frame. It is recommended to set the GpuLevel as low as possible to save power. The valid value range is [MinGpuPerformanceLevel, MaxGpuPerformanceLevel].

Summary

Check the current status by:

  • ThermalMetrics.WarningLevel (NoWarning/ ThrottlingImminent/ Throtting)
  • PerformanceBottleneck (CPU / GPU / TargetFrameRate)

Control the performance by adjusting:

  • LOD bias
  • CPU / GPU clock level
  • Target frame rate
  • If possible, GFX load (triangles, memory, draw calls..)

Performance results

Frame Rate now displays a stable FPS with Adaptive Performance.

You're done!

Congratulations! You have successfully achieved the goal of this Code Lab. Now, you can improve the frame rate using Adaptive Performance by yourself! If you're having trouble, you may download this file:

Adaptive Performance Complete Code
(2.25 KB)

To learn more about Adaptive Performance, GPUWatch, and Galaxy GameDev, visit: www.developer.samsung.com/galaxy-gamedev