In this lesson, you will learn how to drastically reduce your final package size and memory footprint by surgically removing unused shader permutations during the build process.
The Problem: Shader Explosion
Unreal Engine generates thousands of shader permutations to handle different combinations of features (e.g., whether an object has a Normal map, uses Translucency, supports distance fields, or utilizes specific light types). By default, the engine compiles variants for every possible combination. If your project doesn't use certain features—like specific blending modes or light types—those compiled permutations are essentially "dead weight" taking up precious megabytes in your binary and memory during runtime.
Strategic Stripping
To optimize this, we use the ShaderInclude and ShaderPermutation filtering systems within the project’s configuration files, specifically focusing on the DefaultEngine.ini. By defining which global features are disabled, we tell the shader compiler to skip the entire branch of permutation logic before the shader is ever built.
For instance, if you are targeting a mobile platform that strictly avoids expensive features like RayTracing or VolumetricFog, you can explicitly strip them out:
[ShaderCompiler]
; Strip unused permutation branches globally for the build
r.ShaderDevelopmentMode=0
r.AllowMeshShader=0
r.AllowRayTracing=0
Beyond the ini files, you can use the IShaderPipelineCache interface in C++ or the GetShaderPermutation() override in your custom material shaders to return false in the ShouldCompilePermutation function. This prevents the compiler from generating shaders that your project logic would never trigger anyway:
bool FMyCustomShader::ShouldCompilePermutation(const FShaderPermutationParameters& Parameters)
{
// Strip this permutation if we aren't using the feature on this specific platform
if (Parameters.Platform == SP_PCD3D_SM5 && !bEnableSpecificFeature)
{
return false;
}
return true;
}
Why it Matters
When you strip permutations, you aren't just saving disk space; you are reducing the time it takes for the Driver/RHI to initialize, lowering the memory pressure during level loading, and preventing the GPU from needing to swap these variants in and out of the shader cache. It is one of the most effective ways to tighten your build footprint before shipping.
Key Takeaway: Build-time shader stripping allows you to prune thousands of unused shader variants, directly reducing your game's binary size and improving initial load performance by ensuring only relevant code paths are compiled for your target platforms.