# Shader Variant Analysis
Track and manage shader permutations in the Unity Editor: https://github.com/eldnach/shader-variant-analysis
* Trace the shader variants generated by your project at build time.
* Analyze the traces and identify Which shaders and keywords generate the most permutations.
* Identify effective ways to reduce shader build times.
The Unity Editor generates and compiles a large amount of shader permutations at build time, based on the amount of Shader Keywords declared and evaluated in shaders. This mechanism provides runtime control over shader functionality, while maintaing optimal shader execution performance.
Shader variant generation contributes to an expontential growth in project build times, depending on the amount of shader keywords and render pipeline features enabled:
It is advised to monitor the project's shader variant generation, and ensure no unnecessary variants are created at project build time. For more information on Unity's shader variant generation see https://blog.unity.com/engine-platform/2021-lts-improvements-to-shader-build-times-and-memory-usage
## Requirements
Supported in Unity 2021 LTS and later.
## Setup
1. Clone the package repository "https://github.com/eldnach/shader-variant-analysis"
2. In the Unity Editor, go to `Window > Package Manager`
3. In the top left of the Package Manager window, click on `+ > Add package from disk`
4. Add the package directory
Once installed, go to `Window > Analysis > Shader Variant Analysis`.
## Usage
Enable the "Trace Next Build" setting (`Shader Variant Analysis > Build Settings > Trace Next Build`), in order to record your project's shader generation at build time. The trace file will be saved in your project's Asset folder, using the specified name:
Initiate a project build as usual. Once the build is complete, load the trace (`Shader Variant Analysis > Import Settings > Load Traces`) and analyze the result. The `Tracked Shaders` panel will list all the shader included in the build, along with their respective variant counts.
You can expand the `Keywords` dropodown, in order to list all the keywords declared in a shader, and their contribution towards variant generation. Keyword tool tips reveal the render pipeline feature which enables each keyword.
In the above example, the "_ADDITIONAL_LIGHTS_SHADOWS" keyword is included in a large number of variants. This keyword is enabled via the "Additional Lights/Cast Shadows" settings in the Universal Render Pipeline Asset. Disabling graphics features in the project and render pipeline settings will allow the Unity Editor to filter the associated keywords and reduce shader build time.
The `Variants` dropdown reveals a detailed view on each variant generated by shader, along with the variant's subshader, pass, stage, platform and keyword set:
About Me
Realtime graphics enthusiant, currently working as a Technical Product Manager for the Unity Engine.
In this blog, I share some useful tools and personal projects to assist with Unity and realtime graphics development.