skip to Main Content

About shader keywords

Shaders are extremmely optimized pieces of code that run in the GPU. Because they need to use the fewer number of instructions, shader keywords related to their features or options can be used to create compiled variations of the same code base. This way the code actually executed in the GPU is locally optimized with fewer register usage and less conditional jumps, resulting in faster execution.

For example, in a fog shader, an option could be to blend the fog using 2 colors instead of only one, creating an additional gradient effect. Since you may want to use a single color, a keyword could be used to generate two variants of the same fog code at compilation time, one that uses the second color and another one that just uses one color. This keyword could be named FOG_USE_GRADIENT and is speficied in the shader using a special compiler instruction called “#pragma multi_compile”. When the compiler detects a #pragma multi_compile line, it will generate several variations of the shader according to the keywords found.

Following the above example, in the shader file you could find:

#pragma multi_compile __ FOG_USE_GRADIENT

The underscore tells the compiler it must generate a shader variant without any keyword, and then a variant using the keyword FOG_USE_GRADIENT. This keyword can be detected later in the shader code using a compiler conditional that surrounds a piece of code. That piece of code will be include in that compilation if the keyword is enabled.

Many complex shaders, also called “uber shaders”, make use of lot of keywords so as much as possible code can be used in the same render pass. This approach creates faster shaders compared to chaining effects on sequential render passes.

But uber shaders usually adds lot of features (shader keywords) that many users won’t use, producing unnecesary variants hence bigger build size and compilation time. This is the case where Shader Control tool is very useful. By inspecting the shader keywords you can decide to remove the keywords from the shader file itself, avoiding compiling unused variants.

Note that modifying shaders incorrectly can render them useless or produce undesirable visual effects! Make sure you have a backup copy of your shaders or project before applying any automatic change. Also you may need to contact the asset author regarding some shader keywords to ensure you can safely remove them from the shader file.

Back To Top