book-openShader Variant Tools

Overview

Shader Variant Tools is a unified toolchain for managing and trimming shader variants. It provides three primary components that form a complete workflow:

  • Variant Analyzer – analyzes collected variant data

  • Variant Strip Rule Editor – defines rules for variant removal

  • Variant Stripper – applies those rules to automatically eliminate unnecessary variants

Together, these tools streamline the variant-reduction process and help minimize the runtime burden caused by unused shader variants.

Workflow

  1. Use the Variant Analyzer to generate a variant report from the target shaders.

  2. Based on the report, refine removal rules in the Variant Strip Rule Editor.

  3. Re-analyze the shaders with the Variant Analyzer and iterate on the rules until the results meet your requirements.

  4. During shader compilation and packaging, the Variant Stripper automatically removes variants according to the finalized rules.

Tool Overview

Variant Analyzer

  • Analyzes shader variant data and evaluates stripping results.

  • Its reports help refine stripping rules and reduce overall variant counts.

  • A typical analysis report includes:

    • Total number of variants per shader

    • Number of variants successfully removed

    • Variants removed by each individual strip rule

    • Original vs. stripped counts for each keyword

    • … and other variant-level statistics

Variant Strip Rule Editor

  • Creates and edits Variant Strip Rules used by the Variant Stripper.

  • Supports logical expressions (AND / OR / NOT), for example:

    • A — removes any variant containing keyword A

    • A || B — removes any variant containing keyword A or B

    • !A && (B || C) — removes variants that lack A but contain B or C

Variant Stripper

  • Reduces the number of shader variants included in the final build.

  • Automatically removes unnecessary variants according to project-defined rules.

  • Provides the following benefits:

    • Shorter build times

    • Smaller package size

    • Reduced memory usage

    • Lower warm-up and compilation cost (e.g., Shader.Warmup)

Variant Analyzer Usage

You can use the Variant Analyzer menu to analyze the variant data of selected assets. The results help you create targeted strip rules to remove variants you do not need.

  1. Select the assets you want to analyze.

  2. Run: “GraphicsCat/Shader Tools/Shader Variant Tools/Variant Analyzer”

  1. After execution, an analysis report window will appear.

Global Keyword Variant Statsshows variant statistics for global keywords.

User Keyword Variant Statsdisplays variant statistics for shader-specific keywords.

Global Keyword Set Variant Statspresents statistics grouped by combinations of global keywords.

Local Keyword Set Variant Statspresents statistics grouped by combinations of shader local keywords.

You can use the variant reports to create stripping rules that reduce unnecessary variants. In principle, any variant that is not needed can be removed.

Variant Strip Rule Editor Usage

The Variant Strip Rule Editor is used to create and edit Variant Strip Rules, which Variant Stripper then uses to remove unwanted shader variants.

  1. Create a Variant Strip Rule Collection.

  1. Select the shader whose variants you want to strip.

  1. Select a target shader; here, we choose the URP Lit Shader.

  1. Add a stripping rule.

  1. Stripping expressions are edited using a token-based approach. Click the “[?]” button to change token content ([?] represents a single placeholder token).

  1. Select the content for the placeholder token, choosing from different keywords and logical operators.

  1. Add tokens as needed to construct the final stripping expression.

The expression !_MAIN_LIGHT_SHADOWS && (_MAIN_LIGHT_SHADOWS_CASCADE || _MAIN_LIGHT_SHADOWS_SCREEN) means that if main light shadows are disabled, the variants related to main light shadows will be stripped.

This is because Shadow Cascade and Screen Space Shadows are not used when main light shadows are absent.

  1. Click the Analyze button to evaluate the stripping effect. Once complete, an analysis report and log will appear.

The analysis report shows that 50% variants were stripped according to the rules, which indicates excellent results.

  1. During shader packaging, Variant Stripper automatically removes variants according to the defined rules and outputs a work log.

Note: After modifying variant stripping rules, the affected Shaders must be rebuilt for the new rules to take effect. If those Shaders have not changed since the last build, Unity’s incremental build system will reuse the previously built assets and will not rebuild the Shader variants.

Variant Stripper Usage

Variant Stripper is enabled by default and automatically strips variants based on the defined rules during shader builds, requiring no manual operation.

You can manually disable Variant Stripper via the menu to troubleshoot missing variants.

When missing variants occur in the game, there are generally two scenarios:

  1. The Shader Variant Collection used during the build lacks the required variants. In this case, collect the missing variants and rebuild the shaders.

  2. Variants were stripped by Variant Stripper. In this case, disable Variant Stripper to verify whether the missing variants were removed.

Note: After changing the toggle, the affected Shaders must be rebuilt for the change to take effect. If those Shaders have not been modified since the last build, Unity’s incremental build system will reuse the previously built assets and will not rebuild the Shader variants. As a result, turning the Variant Stripper on or off will not affect the build output.

During project builds, Variant Stripper logs variant stripping information, making it easy to review the results and troubleshoot missing variants.

For example, Variant Stripper removed 50% of the variants for the URP Lit Shader ForwardLit Pass, reducing the count from 168 to 84.

It also lists how many variants were removed by each stripping rule, and shows the original and remaining variant counts for each keyword. This information can be used to refine the stripping rules and further reduce the number of shader variants in the game.

In principle, any unused variants in the game can be removed. The more familiar you are with how variants are used in your project, the more effectively you can strip unnecessary variants.

Last updated