Search Unity

Stipping unused Universal render pipeline shaders and textures on iOS build

Discussion in 'Universal Render Pipeline' started by lostminds, Dec 3, 2019.

  1. lostminds

    lostminds

    Joined:
    Jan 17, 2019
    Posts:
    43
    I'm building an iOS app using the Universal Render Pipeline and I'm trying to get the file size as small as possible. When taking a look at the build report I noticed that Universal Render Pipeline seems to include a bunch of resources I don't use in my project, but that are still included in the build package.

    Primarily it's a big UberPost shader 1.7 mb, Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/UberPost.shader
    Along with a bunch of 256k textures in Packages/com.unity.render-pipelines.universal/Textures/

    These all seem to be related to post processing, that I don't use. Is there any way make sure these unused (?) resources aren't included in the build?

    While it's not such a problem for the file size I also notice that a lot of resources are included from Packages/com.unity.render-pipelines.core/Runtime/Debugging/ (various UI widgets, prefabs and images) even though this isn't supposed to be a debugging build. Shouldn't these be stripped as well when you're building for release on iOS?
     
  2. lostminds

    lostminds

    Joined:
    Jan 17, 2019
    Posts:
    43
    I should add that this is in 2019.3.0f1 with the latest render pipeline package
     
  3. FardinHaque

    FardinHaque

    Joined:
    Apr 13, 2015
    Posts:
    3
    Facing the same issue. My UberPost.shader size is 4.6mb even though i am not using any post processing. Is there any way to exclude this shader from the build?
     
    charl3ss likes this.
  4. lostminds

    lostminds

    Joined:
    Jan 17, 2019
    Posts:
    43
    Still the same in 2019.3.0f3 for me, though I just get a 1.7MB UberPost.shader. For some reason they closed the bug I reported on this back in October 2019, but it doesn't seem to have been fixed so I've contacted them and asked for the bug to be re-opened.
     
  5. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,352
    I'm still on 7.1.7 due to some incompatibility with some other assets I'm using, but my Android builds take FOREVER because it spends a ton of time compiling the Standard shader and other shaders meant for the built-in pipeline which I'm not even using anymore. Do I take it that 7.1.8 is supposed to solve this?
     
  6. lostminds

    lostminds

    Joined:
    Jan 17, 2019
    Posts:
    43
    I haven't seen anything about URP v7.1.8 solving this anywhere (I'm not sure Unity even considers this a bug any more), and as far as I can tell the problem is still the same on 2019.3.0f6 and URP v 7.1.8
     
  7. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,352
    According to the docs here:
    https://docs.unity3d.com/Packages/c...es.universal@7.1/manual/shader-stripping.html

    It should at least perform some kind of stripping according to the settings in the URP asset. I would think, at minimum, built-in shaders for the built-in pipeline should fall under that stripping, given that they can't even work with the URP. Yet my builds spend TONS of time compiling all the built-in pipeline shaders.
     
  8. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    116
    Same here.

    Shader is:
    4.6 mb 1.1% Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/UberPost.shader

    Textures are:
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Thin02.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Thin01.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Medium06.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Medium05.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Medium04.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Medium03.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Medium02.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Medium01.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Large02.png
    256.1 kb 0.1% Packages/com.unity.render-pipelines.universal/Textures/FilmGrain/Large01.png
     
  9. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    116
    Small workaround for UberPost.shader, create new file called StripShaders.cs to Assets/Editor folder and copy paste following code to it
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEditor;
    3. using UnityEditor.Build;
    4. using UnityEditor.Rendering;
    5. using UnityEngine;
    6. using UnityEngine.Rendering;
    7.  
    8. // Simple example of stripping of a debug build configuration
    9. class ShaderDebugBuildProcessor : IPreprocessShaders
    10. {
    11.     public ShaderDebugBuildProcessor()
    12.     {
    13.  
    14.     }
    15.  
    16.     // Multiple callback may be implemented.
    17.     // The first one executed is the one where callbackOrder is returning the smallest number.
    18.     public int callbackOrder { get { return 0; } }
    19.  
    20.     public void OnProcessShader(
    21.         Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> shaderCompilerData)
    22.     {
    23.         //UnityEngine.Debug.Log($"Shader {shader.name}");
    24.         if (shader.name.Contains("UberPost"))
    25.         {
    26.             //UnityEngine.Debug.Log($"- Shader cleared");
    27.             shaderCompilerData.Clear();
    28.         }
    29.     }
    30. }
    UberPost.shader will still be included in the build, but its size will be around 0.3 kb
     
  10. Moritz5thPlanet

    Moritz5thPlanet

    Joined:
    Feb 5, 2019
    Posts:
    40
    I use a solution based on this hacky github repo:
    https://github.com/SixWays/UnityShaderStripper/wiki

    I use the Whitelist feature of the ShaderStripperVariantCollection. Works like a charm (after realizing tha there are TWO activation checkboxes, one in the stripper and one in each settings asset). You need to add some excludes manually, such as "Sprites/Diffuse" if you want a splash screen that's not pink.

    It has cut down build times by over 20 minutes.
     
    SaturnCO and FardinHaque like this.
  11. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    116
    Another strip script, this time for textures in FilmGrain folder. So create script file called RemoveURPTextures.cs to Assets/Editor folder and copy paste following code to it
    Code (CSharp):
    1. using System.IO;
    2. using UnityEditor;
    3. using UnityEditor.Build;
    4. using UnityEditor.Build.Reporting;
    5. using UnityEngine;
    6.  
    7. public class RemoveURPTextures : IPreprocessBuildWithReport
    8. {
    9.     public int callbackOrder { get { return 0; } }
    10.     public void OnPreprocessBuild(BuildReport report)
    11.     {
    12.         Debug.Log("MyCustomBuildProcessor.OnPreprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);
    13.  
    14.         string packagesPath = Application.dataPath.Replace("/Assets", "") + "/Library/PackageCache";
    15.         Debug.Log($"packagesPath: {packagesPath}");
    16.         string[] directories = Directory.GetDirectories(packagesPath);
    17.  
    18.         foreach (string directory in directories)
    19.         {
    20.             Debug.Log($"directory: {directory}");
    21.             if (directory.Contains("com.unity.render-pipelines.universal"))
    22.             {
    23.                 Debug.Log($"Modifying entries in: {directory}");
    24.                 string texturesPath = Path.Combine(directory, "Textures/FilmGrain");
    25.                 string[] files = Directory.GetFiles(texturesPath, "*.meta");
    26.                 foreach (string filename in files)
    27.                 {
    28.                     Debug.Log($"Modifying entry: {filename}");
    29.                     string[] lines = File.ReadAllLines(filename);
    30.  
    31.                     for (int i = 0; i < lines.Length; i++)
    32.                     {
    33.                         lines[i] = lines[i].Replace("maxTextureSize: 2048", "maxTextureSize: 32");
    34.                     }
    35.  
    36.                     File.WriteAllLines(filename, lines);
    37.                 }
    38.             }
    39.         }
    40.         //throw new BuildFailedException("aa");
    41.     }
    42. }
    Filmgrain textures will still be in package, but their sizes will be 1.1 kb. This script reduces APK size with 1.2 MB in our project.
     
    SaturnCO, FardinHaque and wblaschko like this.
unityunity