Shader Stripping Improvements in URP
In recent years, URP has grown and introduced a lot of amazing features. This resulted in a huge amount of shader variants to support those features.
To ensure that these variants don’t add to build times, file size, and runtime memory usage, we have expanded URP’s shader stripping capabilities.
In 2021.2 we are introducing a few new features that will be covered in detail.
Stripping unused feature variants
Many features in URP require “enabled” and “disabled” variants of prebuilt shaders, depending on whether the feature is enabled or disabled. URP already performs some automatic stripping of these variants, but we have improved this from version 2021.2 onwards.
In all versions of URP, if a feature is disabled in all URP Assets included in your build, URP strips the “enabled” variants and compiles only the “disabled” variants.
In versions prior to 2021.2, if a feature is enabled in any URP Asset included in your build, URP compiles both “enabled” and “disabled” shader variants.
From 2021.2 onwards, you can enable Strip Unused Variants in the URP Global Settings. When this setting is enabled, if a feature is enabled in all URP Assets included in your build, URP strips the “disabled” variants and compiles only the “enabled” variants. If this setting is disabled, URP behaves as before.
Here is the list of features that this applies to:
- Light Layers
- Render Pass
- Reflection Probe Blending
- Reflection Probe Box Projection
- Forward+
- SSAO
- Decals
- Main Light Shadows *
- Additional Light Shadows *
- Additional Lights *
Keyword changes
In versions of URP prior to 2021.2, URP enables and disables the shader keywords for Main Light Shadow, Additional Light Shadow and Additional Light based on the configuration of the current scene. This means that URP can switch between the “enabled” and the “disabled” variants for different scenes.
From 2021.2, URP enables and disables these keywords based on settings in the URP Asset. This means that URP uses either the “enabled” or “disabled” shader variant as long as the current URP Asset is in use, rather than switching per-scene.
This change allows URP to strip unneeded variants when Strip Unused Variants is enabled.
We felt this was a less common case, and the overall performance change is quite small. Keep in mind that you can disable Strip Unused Variants to retain the previous functionality.
Stripping unused Quality Settings variants
You can assign different URP Assets to different Quality Settings levels. At build time, URP examines these URP Assets to determine which URP features are enabled or disabled in your build, and therefore which variants to compile or strip.
In versions of URP prior to 2021.2, URP examines every URP Asset that is assigned to a Quality Settings level, regardless of whether that Quality Settings level is enabled for the current build target. To avoid including unneeded variants in these versions of URP, you must ensure that unneeded URP Assets are not assigned to a Quality Settings level at build time.
From 2021.2, URP only examines URP Assets that are assigned to Quality Settings levels that are enabled for the current build target.
Stripping unused post-processing variants
In URP 2021.1, we added an option to strip all post-processing shader variants from your build. In URP 2021.2, we introduce the option to strip post-processing shader variants that are not used by any Volume Profiles in your project. For example, if you only use Bloom in your project, all other unneeded post processing variants and shaders will be stripped.
To enable this feature, go to URP Global Settings and enable Strip Post Processing. Note that this feature works by checking all Volume Profiles in the project - for now it does not check if it is actually used in a scene or not.
XR/VR Stripping
If you do not plan to use XR/VR in your project, you can disable XR and VR modules (How to disable a Module). This allows URP to strip XR and VR related shader variants. This functionality has not changed in 2021.2, but it is a useful tip.
“Small” Project
Testing small project with enabled features:
- SSAO
- Decals
- Reflection Probe Blending
- Reflection Probe Box Projection
- Bloom
Results from Windows Player D3D11 build:
--------------------- Before ------- After
Build Time ------ 2min 27s ---- 28s
Variant Count – 4407 --------- 944
Build Size ------- 70.2MB ----- 69.4MB
Template Project
Testing changes with URP Template project.
Results from Windows Player D3D11 build:
--------------------- Before ------- After
Build Time ------ 20min 14s — 7min 12s
Variant Count – 7964 ---------- 2277
Build Size ------- 82.4MB ------ 78.6MB
Boat Attack Project
Testing changes with URP Boat Attack Demo.
Results from Windows Player D3D11 build:
--------------------- Before ------- After
Build Time ------ 58min 57s — 39min 19s
Variant Count – 22890 -------- 11895
Build Size ------- 495MB ------ 492MB