Search Unity

Bug Build-Only per-frame GC caused by missing shaders

Discussion in 'Universal Render Pipeline' started by Baste, Nov 23, 2020.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,335
    I'm running a pass on cleaning up per-frame GC from our game, and there's one entry left, which comes from URP with 2D lights;

    upload_2020-11-23_16-35-41.png

    This is profiling a build, built with Deep Profiling enabled, with:
    Unity 2020.13.1f1
    URP 8.2.0

    What's happening with the first GC cost is pretty straight-forwards. RenderingUtils.errorMaterial looks like this:

    Code (csharp):
    1. static Material s_ErrorMaterial;
    2. static Material errorMaterial
    3. {
    4.     get
    5.     {
    6.         if (s_ErrorMaterial == null)
    7.         {
    8.             // TODO: When importing project, AssetPreviewUpdater::CreatePreviewForAsset will be called multiple times.
    9.             // This might be in a point that some resources required for the pipeline are not finished importing yet.
    10.             // Proper fix is to add a fence on asset import.
    11.             try
    12.             {
    13.                 s_ErrorMaterial = new Material(Shader.Find("Hidden/Universal Render Pipeline/FallbackError"));
    14.             }
    15.             catch { }
    16.         }
    17.  
    18.         return s_ErrorMaterial;
    19.     }
    20. }
    It seems like the shader "Hidden/Universal Render Pipeline/FallbackError" doesn't exist in builds, as if I run this code in a build:

    Code (csharp):
    1. var s = Shader.Find("Hidden/Universal Render Pipeline/FallbackError");
    2. Debug.Log(s);
    3. Debug.Log(s == null);
    The output is "null" and "true". The shader does get loaded when playing in the editor.

    The second GC cost, that's labeled the absurd "Object.__icall_wrapper_ves_icall_array_new_specific()" is probably just this line in Render2DLightingPass.Execute():

    Code (csharp):
    1. const int blendStylesCount = 4;
    2. bool[] hasBeenInitialized = new bool[blendStylesCount];
    So that should probably be cleaned up with a static array or by turning the pass unsafe and stackallocing the array instead.

    Should I report this as a bug, or is it something you're aware about and that will be fixed on it's own? I'd really love to not have to have GC spikes due to running the 2D renderer.
     
  2. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    Please report it as a bug or else it does not get fixed/resolved. I agree that it should be a bug.
    But I have sort of given up on waiting for the fix as I suspect the URP team might be pretty small and limited in resources as it is not a breaking issue.
    Because of this minor stupid issue, I implemented my own custom renderer to use my own custom DrawObjectPass that omits the resolving via errorMaterial. If something fails to render, I just ignore it. It might be a pain-in-the-ass to debug faulty shaders later on but still I avoid having the initial GC spike.
     
  3. Shaunyowns

    Shaunyowns

    Joined:
    Nov 4, 2019
    Posts:
    328
    Hey there! Reporting it as a bug will always help, so please feel free to do so!
     
  4. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    Perhaps the error material should have been created during the Renderer creation and not on demand.
     
  5. DrakesGames

    DrakesGames

    Joined:
    Aug 2, 2017
    Posts:
    8
    For anybody running into this problem still, I worked around this by forcing the Hidden/URP/FallbackError shader to be included in the build via the "Always Included Shaders" in ProjectSettings>>Graphics.

    • I can confirm that this removed the problem on my android build (I checked the profiler before and after the change)
    • I did not check the change in build size or anything else yet so be aware in case that is important to you
      • Though I will note that the build time wasn't materially impacted
    • Obviously this is not a substitute for the bug actually being fixed. @Shaunyowns i've seen a couple threads now mentioning this and that you say a bug is created but I cant find a link to it. Can you provide that / give an update please?

    upload_2020-12-17_13-43-51.png
     
    Fangh and AviarLabs like this.