Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How does Unity determin whether shader snipt with shader_feature keyword should be stripped or not?

Discussion in 'Shaders' started by EminemJ, Mar 27, 2023.

  1. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    42
    I am currently using the OnProcessShader method to do shader stripping, everything works ok in Unity2021.3.13, However, when I upgrade my Unity to 2021.3.20, the stripper is not working anymore, lots of shader feature is gone, so I step into the method"
    OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data)
    ", it turns out that the ShaderCompilerData list is totally wrong, there are only a few keywords been kept alive. And that leads to the result that most of the materials used in my project are not correctly rendered in the bundle version.
    My question is How Unity treat these shader_feature keywords,how does it know whether the shader snipts using these keywords should be kept or strpped?
    What should I do to make Unity correctly handle these shader_features with Unity 2021.3.20?
     
  2. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    829
    It depends on the materials that use it. Unity looks through all scenes in the build (and any Resources folders), looks at all referenced Material objects, and then checks what shader features they use. However this means...

    - If you create a material at runtime, the build system will know nothing about that.
    - If you modify material properties at runtime in a script, the build system won't know about that. It'll strip variants that aren't part of the static/build-time material.
     
  3. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    42
    The materials using those shaders are included in asset bundles(not the same bundle with shaders), in this way can't Unity get the correct dependencies?
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,749
    Normally, you'd put the materials using the shader and the shader in the same bundle. I'd ask in the asset bundle subforum whether materials put in an asset bundle should automatically handle the dependencies (shaders) or not.
    Please be careful with this, though - a shader in an asset bundle is isolated, so if you have two asset bundles with the same shader and load both, you'll get the shader loaded twice.
     
  5. EminemJ

    EminemJ

    Joined:
    Mar 10, 2020
    Posts:
    42
    Thanks for your reply, I've tried to build a shader variant collection with the corresponding shader in the same bundle, and it works fine now
     
    aleksandrk likes this.