Optimize Shader Performance with Mali Offline Compiler

Updated on Mar 21,2024

Optimize Shader Performance with Mali Offline Compiler

Table of Contents

  1. Introduction
  2. Running Mali Offline Compiler
  3. Fragment Shader Program
  4. Analyzing Shaders on Mali GPUs
  5. Reducing Work Register Usage
  6. Reducing Variable Precision
  7. Optimizing Arithmetic Operations
  8. Cycle Cost Breakdown
  9. Shader Language Features
  10. Exporting Reports
  11. Developer Resources

Introduction

In this article, we will explore how to use the Mali Offline Compiler to analyze the performance of fragment and vertex shader programs on Mali GPUs. The Mali Offline Compiler is a powerful tool that allows you to generate reports on the expected performance of shaders without the need to connect to a device. This is especially useful for predicting performance on devices that you don't have access to. We will walk through the process of running the compiler from the command-line and interpreting the generated reports.

Running Mali Offline Compiler

To run Mali Offline Compiler, you need to execute it from the command-line. The command is simple and requires you to specify the Mali Offline Compiler command, your shader program, and the GPU that you want to analyze the shader against. For the purpose of this demonstration, we will use the Mali-G72, which is a popular Bifrost GPU found in many mass-market devices.

Fragment Shader Program

The fragment shader program we will be analyzing is responsible for the dissolve effect that occurs in our Game when enemy soldiers die. This effect adds visual appeal to the game and needs to be optimized for better performance. Let's proceed with analyzing this shader program using the Mali Offline Compiler.

Analyzing Shaders on Mali GPUs

The Mali Offline Compiler provides valuable information about the shader and the configuration of the run. The generated report includes details about the GPU, its architecture, the Mali driver version, and the type of shader being analyzed. One important aspect to consider is the number of work registers being used by the shader. By reducing work register usage, we can increase the number of Threads that can be executed simultaneously, thus keeping the GPU busy.

Reducing Work Register Usage

To optimize the shader, we can try reducing the precision from highp (32-bit) to mediump (16-bit). This allows the GPU to store twice as many variables per register, resulting in improved performance. Additionally, we should check if any variables are being spilled to stack memory. Stack spilling is expensive for a GPU to process, so reducing register pressure by reducing variable precision or simplifying the shader program can greatly benefit performance.

Reducing Variable Precision

In the generated report, we can see the percentage of arithmetic operations performed at 16-bit precision or lower. Ideally, we want a higher number here as 16-bit precision (mediump) is faster than 32-bit precision (highp). If the majority of calculations in the shader are done at highp precision, we can consider reducing the precision to mediump, which will result in improved efficiency, reduced energy consumption, and higher performance.

Optimizing Arithmetic Operations

The report also provides a breakdown of the cycle cost for the major functional units in the Mali shader core, including the arithmetic unit, load/store unit, varying unit, and texture unit. By analyzing the cost of each unit, we can identify the one with the highest cycle cost and optimize it. This optimization can be achieved by reducing the number of mathematical operations or adjusting the precision of the calculations.

Cycle Cost Breakdown

For Valhall-based GPUs like the Mali-G78, the arithmetic cost is further broken down by FMA, CVT, and SFU pipelines. These GPUs implement Parallel processing engines with their own set of arithmetic pipelines. By normalizing the data based on the number of engines in the design, we can obtain an overall cost for the targeted shader core. This information helps us optimize the shader for specific GPUs and ensure maximum performance.

Shader Language Features

The shader properties section in the report provides information about the shader's use of language features that can impact performance. For example, if the shader contains uniform computation, it is advisable to move this computation out of the shader code and onto the CPU to be executed at draw time. Additionally, the report indicates if the shader can modify the fragment coverage mask, which affects the efficiency of early ZS testing and fragment Scheduling. Minimizing the use of discard statements and alpha-to-coverage can optimize fragment shaders.

Exporting Reports

Once the analysis is complete, you have the option to export the generated report to a file. This allows for easy sharing and documentation of the shader's performance characteristics. If you are working in a continuous integration environment, you can also export reports as machine-readable JSON files. These files can be used to build your own dashboard to monitor shader performance over time, providing valuable insights into performance trends and optimizations.

Developer Resources

To further enhance your understanding and skills in designing performant content for mobile devices, there are developer resources available. Arm Mobile Studio tools offer a comprehensive suite of tools and resources to aid developers in optimizing their graphics applications. By leveraging these resources, you can unlock the full potential of Mali GPUs and create captivating mobile experiences.

Highlights

  • Use the Mali Offline Compiler to analyze and optimize the performance of fragment and vertex shader programs on Mali GPUs.
  • Reduce work register usage and variable precision to improve performance and increase the number of threads executed simultaneously.
  • Optimize arithmetic operations based on the cycle cost breakdown of functional units in the Mali shader core.
  • Take advantage of the shader language features to minimize discard statements and alpha-to-coverage usage.
  • Export reports for documentation and analysis, and utilize machine-readable JSON files for continuous integration environments.
  • Explore developer resources and Arm Mobile Studio tools to further optimize graphics applications for mobile devices.

FAQ

Q: Can I run Mali Offline Compiler on any Mali GPU? A: Yes, you can run Mali Offline Compiler on any Mali GPU, allowing you to predict the expected performance even on devices you don't have access to.

Q: How can reducing variable precision improve shader performance? A: Reducing variable precision from highp (32-bit) to mediump (16-bit) enables the GPU to store twice as many variables per register, leading to improved performance and reduced energy consumption.

Q: Are there any resources available to learn more about optimizing shader performance? A: Yes, Arm Mobile Studio tools provide a range of developer resources to help you design more performant content for mobile. These resources can greatly enhance your understanding and skills in shader optimization.

Q: Can I export Mali Offline Compiler reports as machine-readable JSON files? A: Yes, you can export reports as machine-readable JSON files, which are useful for building your own dashboard to monitor shader performance over time. JSON schema definitions and sample reports are provided in the installation directory of the compiler.

Q: How can I further optimize graphics applications for mobile devices? A: By leveraging the developer resources and tools offered by Arm Mobile Studio, you can further optimize your graphics applications, unlocking the full potential of Mali GPUs and creating captivating mobile experiences.

Resources:

Most people like