CPU profiling for Unity

Originally posted:
Last updated:

It can often be difficult to know where to start when trying to optimize a Unity® project.

Our Unity Performance Guide is here to help with a step-by-step look at the profiling and optimization process which can help you get the best performance out of your Unity game.

Profiling

Your first step in any optimization journey is profiling. Understanding how your game is currently performing allows you to identify where you need to make changes to boost performance.

Before profiling

When profiling the GPU, you need to decrease the overhead on the CPU-side as much as possible. This can be achieved in a number of steps.

  1. Firstly, you can turn off VSync on platforms other than Mobile or VR by setting vSyncCount to 0. vSyncCount can be changed in your script, QualitySettings.vSyncCount = 0. Alternatively, you can update vSyncCount in the Project Settings panel.

    turn off vsync through project-settings

  2. Next, disable log by adding this -nolog into your input arguments

  3. Finally, instead of muting the Audio Source, you should disable the Audio Source component or disable the GameObject.

This should all reduce the load on the CPU and get you ready for profiling.

The next step is making sure you’ve got the right tools for the job.

Tools

AMD provides a large collection of tools to support application development on AMD Radeon GPUs.

The most useful one for our purposes is Radeon GPU Profiler. In addition, the built-in Unity profiler also provides a useful source of data for your investigation.

Radeon GPU Profiler (RGP)

Radeon™ GPU Profiler (RGP) is a profiling tool for Radeon GPUs. It provides rich information about the GPU which allows the developer to gain a greater insight into their application and have more ideas about where to optimize. You can find more information about RGP on the RGP product page. Note, RGP only supports DirectX® 12 and Vulkan.

Using RGP

You can create a capture of your game using RGP in a few simple steps.

  1. Run RadeonDeveloperPanel.exe and connect to the local Radeon Developer Service.
  2. Launch your game.
  3. When your game has started running successfully, RGP will switch to the APPLICATIONS tab automatically. rdp-applications-tab
  4. Capture a profile by either pressing the “Capture profile” button, or by using the hotkey Ctrl+Alt+C.
  5. After RGP has succesfully created a capture, the profiling data will be shown in the current tab.
Analysing captured data

Now that you’ve created a capture in RGP, it’s time to analyze the profiling data for clues.

  1. First, check if the game is GPU or CPU-bound. You can determine this directly from the OVERVIEW tab in RGP. Note, a development build could result in the game being reported as being CPU-bound due to the higher overhead on CPU-side incurred by the development build. rgp-overview-cpu-bound
  2. Next, check the EVENTS tab. From this tab, you can see the wavefronts occupancy, cache hit rate, and the events timeline. Low occupancy means low GPU utilization. There are many reasons for a shader having low wavefront occupancy, including high cache hit missing rate and redundant barriers. You can find cache hit information in the cache counters section. rgp-events

Unity built-in profiler

In addition to RGP, you can also use Unity’s built-in profiler to gather more data.

Using the built-in profiler

The Unity profiler can be activated using the following steps.

  1. Open profiler from Window->Analysis->Profiler. open-built-in-profiler
  2. Play the scene as usual, but make sure the status of record profiling has been turned on. built-in-profiler-record-profiling
  3. On the top left, there are some profiler modules that you could choose depending on your requirements. built-in-profiler-profiler-modules-choose
  4. You can choose the frame you would like to analyse by dragging the frame indicator line to the frame you’re interested in. built-in-profiler-frame-choose
Analysing captured data

As with RGP, you now need to investigate your profiling data to determine how your game performance is limited.

  • From the CPU and GPU profiler modules, you can see an overview about time cost over one specified frame. built-in-profiler-capatured-data-cpu-gpu-usage
  • At the bottom, you can switch between different views with respect to different threads. built-in-profiler-capatured-data-view-switch

Below is a screenshot of the Timeline view.

built-in-profiler-capatured-data-timeline-view

Optimization Tips

By using the profiling steps above, you should be able to determine why the performance of your game is limited, so the next step is determining what actions to take to improve performance.

For the GPU-bound case, you may want to try FSR 1 or 2 integration for Unity URP.

FSR 1

Since it’s already been adopted by Unity, you should be able to use it simply by selecting FSR1 as the Upscaling Filter on the inspector panel of your Universal Render Pipeline Asset.

FSR1-URP-enable

FSR 2

Our FSR 2 patch for Unity URP is now available on GitHub.

Check out our Integrating AMD FidelityFX™ Super Resolution 2 (FSR 2) into Unity URP blog post to get up and running quickly.

Note that our current version of the patch supports DirectX® 11 only.

Other tips

In addition, you can try some of these quick fixes.

  • Disable unnecessary lights, or set lights count limit for object in URP. URP asset panel urp-lights-limit
  • Use the lowest shadow resolution that can meet the demand. URP asset panel urp-lights-main-shadow-resolution
  • Volumetric Fog/Cloud FX and reflection probes can be very costy. Use the lowest quality setting as much as you can. HDRP settings panel hdrp-volumetric-fog-settings
  • Use compression format or lower resolution for textures.
  • Make use of static/dynamic batching in Unity

Remember to profile your game again after making any changes to ensure performance is improved.

Related news and technical articles

Related videos