Search Unity

ShaderVariantCollection best practises?

Discussion in 'Shaders' started by PsyDev, Feb 8, 2017.

  1. PsyDev

    PsyDev

    Joined:
    Sep 30, 2015
    Posts:
    31
    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: https://docs.unity3d.com/Manual/OptimizingShaderLoadTime.html 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.
     
    McDev02 likes this.
  2. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    7,210
    Nice find I hit a major performance problem that seems to be caused by this!
     
  3. indieDoroid

    indieDoroid

    Joined:
    Jan 25, 2016
    Posts:
    239

    Yep I have the same questions as you.

    I found this. Seems to let us load \ unload shader variants on demand. So would be useful when certain levels need certain shaders.
    https://docs.unity3d.com/ScriptReference/ShaderVariantCollection.Add.html

    Just gotta figure out how to use it now lol.
     
  4. andysaia

    andysaia

    Joined:
    Nov 2, 2015
    Posts:
    10
    Anybody come up with a good workflow for this? Manually creating the shaderVariantCollection has already been the source of a lot of bugs and confusion on our team. To make it worse it looks like the variants won't even get built to asset bundles unless they are bundled next to a shaderVariantCollection.
     
unityunity