Hello,
I have problem with memory usage when URP shaders are in Addressables and I would like to ask how should I strip shader variants correctly? Problem is not version specific. We see it in both 2021 LTS and 2022 LTS.
When my assets are in the bundles together with URP shaders Unity compiles all shader variants (20+K). Bundle has around 30 mb and shaders take even up to 700 mb of RAM memory each in runtime (SimpleLit + Lit together can take 1.4 GB).
However, when I remove addressables and everything is included in the build, Unity is compiling just a few hundred of variants for URP/Lit or URP/SimpleLit. I takes a few mb of RAM memory in runtime.
I have looked through the forum and documentation and found a few ways to help Unity strip shader variants, but they seem to don’t work for addressables. Below is the list of what I have tried:
-
Graphics Settings - changed shader stripping based on my scenes
-
URP Global Settings - all stripping options are enabled
-
Shader Variants Collection - added to the bundle instead of shaders itself
-
Custom stripping preprocess - by implementing IPreprocessShaders interface
Only custom stripping preprocess seems to have any impact. It decreased bundle size from 30 mb to 6 mb, however in runtime it uses the same amount of RAM memory (or maybe even a bit more).
I am not sure what should exactly this preprocess do. Based on the documentation, you propose to remove variants based on keywords: Unity - Scripting API: Build.IPreprocessShaders.OnProcessShader.
At first try I just started from disabling Keywords which in my opinion are not needed, but I didn’t remove variant from the list. Compilation was much faster, bundle size smaller, but unfortunately didn’t affect RAM memory usage. I think that still all variants were compiled, but much simplified. Then I tried to remove variants which have zero keywords. Unfortunately it still seemd to compile all variants (based on what progress bar is showing), RAM memory usage wasn’t affected again, but some shaders appeared to be missing in the build (pink objects).
Do you have any advice in which direction I should go further with this problem?