devNotes 2-18-2017 Shader Variants

  • dsfhdhfhf fhdifhidhifhid6 c2a4z8rxcaaenit c49ivwrweaaacv2 c47r6fdwcaexdjd


    I just learned that some framerate spikes were being caused by shader variants being copied to the GPU only when they are used for the first time. The solution is to use Shader Variant Collections. The Unity docs are here: I think this thread is part of the history.

    In my case it was when calling SetActive() on a bunch of objects at the same time, I was getting a very long frame. Drilling down into the profiler, I saw that all the time in that long frame was spent in Shader.EditorLoadVariant. So after learning about Shader Variant Collections, I can do this to create a collection and preload it at boot, preventing the glitch runtime:

    • Run the game to the point where all the required shaders are being used.
    • Edit->Project Settings->Graphics
    • Scroll to the Shader preloading section at the bottom
    • Hit Clear <- I learned through trial and error you need to do this. I think Unity adds to the Currently Tracked count everytime it sees a shader. So when I was doing a test with a new blank project, imported Effects package, it was saying it was tracking 37 variants, even with nothing in the scene.
    • Save to asset
    • Add that asset to the Preloaded shaders array

    That all works fine. My questions are:

    • How is this managed in a production environment?
    • Does anyone automate the creation of the Shader Variant Collections? Or do you have to manually go into each scene or level, clear the tracked, then create a new asset? I can see that getting very tedious and be error prone as new shaders and materials are added to a scene.
    • What about subtle variations from one level to another? Rather than having a separate collection for each level/scene, they could be combined to create a collection that covers all cases.
    • Do shader variants ever get unloaded from GPU? So that when used again, do they get recompiled? It appears not from my testing.

    Please share if you have experience with this issue.

    EyecyArt likes this.
  • Arowx


    Nov 12, 2009

    Nice find I hit a major performance problem that seems to be caused by this!

Leave a Reply

Your email address will not be published. Required fields are marked *