Skip to content
Dr Luke Thompson edited this page Dec 14, 2018 · 2 revisions

UnityShaderStripper

Modular utilities for shader stripping to drastically improve Unity build times on Unity 2018.3.

Launch Shader Stripper from Tools > Sigtrap > Shader Stripper. You'll be presented with something a little like this.

BORING.

To liven things up a bit, go into your project panel, then Right Click > Create > Sigtrap and select one of the thingies there. Then click Refresh Settings on the Shader Stripper panel and after a while it'll look more like this.

Each entry here is a ShaderStripper asset. When Unity starts a build, all shaders eligible for compilation are automagically passed into the ShaderStripper system. The shader strippers operate in order, from top to bottom, and can remove any number of shaders from the list. The output of each stage is the input for the next, so if the first removes a shader, the second will not receive it.

There are multiple types of built-in shader strippers, and it's possible to extend a base class to create your own. The two major ones to take note of are ShaderStripperSimple, and ShaderStripperVariantCollection.

ShaderStripperSimple

This can strip by shader name, pass type or keywords. Good for removing an entire family of shaders all the way down to a specific variant of a specific shader.

ShaderStripperVariantCollection

This is the one you're really here to see. This only allows Unity to compile the specific shaders and variants you specify. For something like LWRP, this can make shader compile times go from literally hours to perceptually instantaneous.

To create a ShaderVariantCollection, go to Project Settings > Graphics and press Save to asset... at the bottom. This dumps a file specifying all currently warmed-up shader variants. This asset can be dragged into a ShaderStripperVariantCollection to specify which shaders to allow.

HERE BE DRAGONS. This is easy to mess up. If a particular shader variant - particularly one using multi_compile keywords that are only activated from code - isn't in there, it'll be pink in your build.

You can manually edit a ShaderVariantCollection to add things, but let's be honest, it's painful. You have to make sure your ShaderVariantCollection(s) really do(es) represent all the shaders you need, which basically means running through as much of your game in-editor as possible to warm them all up.

The exclusion rules can help however.

  • By default, Hidden/ shaders are not stripped.
  • Built-in shaders are NEVER stripped. In this case, built-in means does not appear in AssetDatabase.
  • Checking Allow Vr Variants will not strip VR versions of collected non-VR variants, or non-VR versions of collected VR variants.
    • e.g. if your collection includes a shader with the keyword MY_KEYWORD and Unity tries to compile that shader with MY KEYWORD + STEREO_INSTANCED_ON, this will be allowed.
  • Checking Allow Instanced Variants works the same but with GPU instancing variants.
  • Ignore Shaders By Name allows you to ignore (i.e. exempt from stripping) specific shaders, shader families or anything whose name matches a simple pattern.
  • Ignore Pass Types works the same with pass types, if you couldn't guess.

DIY

Extend Sigtrap.Editors.ShaderStripper.ShaderStripperBase to create your own more complex rules. There are two main modes of operation.

You can override MatchShader, MatchPass and/or MatchVariant to tie into the basic behaviour of ShaderStripper. Override _checkShader, _checkPass and _checkVariant to tell ShaderStripper which of these methods to call. The results of the methods are combined as ANDs, and called in the order Shader => Pass => Variant.

E.g. if you override MatchShader and MatchVariant (and _checkShader and _checkVariant to return true), if MatchShader returns true, all variants will pass on to MatchVariant. If MatchVariant also returns true, that variant will be stripped.

The second method is to override StripCustom - this gives you complete control. To strip a variant, remove it from the variantData IList passed in. To remove an entire shader or pass, just clear the list.

If you use StripCustom, you must return true or ShaderStripper will think you've not done anything.

Remember to add a [CreateAssetMenu] attribute to create in the project panel. Then just press Refresh Settings to see it pop up in ShaderStripper.

There's plenty of other stuff in there, so poke around.

Clone this wiki locally