Search Unity

Is there a way to avoid "Compiling shader variants" part of the build time?

Discussion in 'Shaders' started by electro_unity, Jan 9, 2019.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    check yourself, for example check for the compilation of the shader Grass Subsurface Lit. that log there was an error in a shader, the editor told me after 90 minutes of compilation. I have started now again and I am 50minutes in.

    I am sure something is not normal. I am going to send you the second log once the building is done.
     

    Attached Files:

    KAJed likes this.
  2. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    332
    @sebas77 can you rebuild with your SlowLoggerUnity hook disabled, they seem to be causing editor log entries to be written out of order to what we would expect to see normally, and possibly cutting off some of the build errors.

    As far as Addressables goes, built-in shaders are automatically de-duplicated because there is no way to manually assign these to addressable groups, but if you are using shaders from a package or your own shaders, those are not automatically de-duplicated. So one option to improve build times is to assign shaders being used to an Addressables group so they are not pulled in multiple times in a single build.
     
  3. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    testing your suggestion immediately because it's killing me. Just to confirm, are you saying to make all our shaders addressable or something else too?

    Doesn't the duplication detection fix take care of this by the way?
     
    Last edited: Oct 18, 2021
  4. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    332
    Addressables' duplication detection system does detect shader use and advice you to assign them to a bundle, and I believe it has a fix for me "button" that does just that, assigns them to a bundle. This is a manual analyze rule that you need to run and it doesn't run it for you automatically, so if you have, you should be fine.
     
  5. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    this is part of our workflow, so the shaders were already addressable. I will be back to you with more info asap.
     
  6. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    I just upgraded to URP after years of holding it off.

    Now I run into this :(
    long.jpg
    Any tips on how to reduce the amount of "fp"?
     
  7. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    KAJed likes this.
  8. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    @crdmrn this looks unrelated.
    Are you sure it's really recompiling other shaders? Can you check in the Editor log and see if that's really the case, or it's picking variants from the cache?
     
  9. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    100%, when it's using the cache it takes 1/100 of the time to compile.
     
  10. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Can you please submit a bug report in this case? I'd love to investigate this issue.
     
  11. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    What should I call the bug though?
    "Unity randomly triggers full shader recompile, no consistent way of repro that?" ^^"
    Also, platform specific cache is always deleted, going back to a platform that i previously compiled for triggers a full shader recompile for that platform :/

    EDIT: another bug, 2021.1.23 seems to be recompiling even shaders that are not present nor referenced in the scenes to build
     
    Last edited: Nov 3, 2021
  12. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    For shader compilation results? Definitely not.
    This is very different from
    May I ask you to send me an Editor log right after it does that next time?
     
  13. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    I am not sure which platforms you're trying it on, but we're at the point that we have a machine dedicated to PC builds and one to Switch builds in the office, because switching platforms was made impossible by continuous shader recompiling.
     
    KAJed likes this.
  14. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    OK, next question then :)
    Are you on Builtin or on SRP? Are you using surface shaders?
     
  15. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    URP, using Amplify.
     
  16. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    When you switch the build target, we reimport all the shaders. After that, if we have already compiled a given variant for the current build target, project settings and the given graphics API, it should be taken from the cache.
    Import doesn't involve compiling shaders, as long as it's not surface shaders.

    Now, I don't know, what exactly Amplify does. Perhaps they regenerate something at import time that makes the compiler think it's a different shader.
     
  17. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    Something is wrong in 2021. 2019 and 2020 builds where faster, few minutes to neverending compile times as in 2021:
    upload_2021-11-11_2-43-30.png
    Got this error in the shader after the build failed:
    upload_2021-11-11_2-47-45.png
     
    stonstad likes this.
  18. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    @tatoforever The compile times went up due to the increased number of variants (909K vertex shader variants in your screenshot).
    Your screenshot says the compiler timed out or crashed - can you please make a bug report and attach this particular shader?
    Thank you :)
     
  19. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    Here is the current amount of memory Unity is leaking while building:
    upload_2021-11-11_4-24-28.png
    Btw, this is the default URP Clearcoat Lit (only with a different name).
    PS: Will report the bug soon.
     
    Last edited: Nov 11, 2021
  20. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    It now stoped at this vartiant number and isn't moving, probably crashed:
    upload_2021-11-11_4-26-12.png
     
  21. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    They don't, I'm using version control and I can see if shaders change or not.
    That said, using Addressables makes it even more of a nightmare as full shaders rebuild are triggered continuously.
     
  22. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
  23. crdmrn

    crdmrn

    Joined:
    Dec 24, 2013
    Posts:
    152
    will be able to do this tomorrow, cause rebuilding shaders takes literally the whole night and there's no way to revert it.
    Or is it ok if I make the build fail, cause you can see what triggers the rebuild anyway?
     
    Last edited: Nov 11, 2021
  24. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    This is fine as well, I just want to see, what's causing the rebuild. Just make sure it starts compiling some shaders.
     
  25. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,368
    I PM you the editor log and the shader. The build failed after hours trying to compile the shader...
    And this is only one shader, i don't wanna see multiple shaders in a project.
    PS: In 2019 i never had this problem my builds where 1-2 minutes.
     
  26. LordCafe

    LordCafe

    Joined:
    Aug 26, 2015
    Posts:
    35
    Having same problem in 2022... just trying to build the Empty Project and took almost 2h... almost going back to unity 2017... URP/HDRP broke the engine so much that's almost unbereable..
     
  27. Playloom

    Playloom

    Joined:
    Jan 20, 2020
    Posts:
    2
    Hi...please I'm also having about 200 scenes in my project but the APK size is enormous...how did you go about this?
     
  28. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254

    I don't quite believe this. I do for hand written shaders, but it appears that shadergraph shaders compile thousands of variants each and every build.

    I made 3 windows D3D11 builds back-to back and each build did a full recompile with 99% of the time spent on the two shadergraph shaders I use. ('Optimize' was turned off for the 2nd and 3rd builds.)

    I do have a shaderassset created, that was saved after running the full app in the editor in it's entirety (approx 144 shader variants), but it doesn't seem to have much of an effect on build times.
     
  29. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Which version are you on? There used to be a bug where some SG shaders specifically would recompile even if nothing changed.
     
  30. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    We're currently on 2022.1.16f1, I'm hesitant to update again as the road from 2018LTS to 2020LTS was fraught with painful issues that kept forcing us to upgrade and rebuild systems well out of the LTS comfort zone. In fact the primary culprit graph is the result of having to re-build our previous Built-In pipeline shader. (URP + OpenXR + SPR is a hell of a combination with many delicate parts that frequently have bugged packages.)

    Of the two shader graphs that are the cause of 90% of our build time, the most variant expensive is the attached. It's built on top of URP Lit to add support for a Fresnel surface shell highlight in addition to tightly packing Metallic, Smoothness, and Emissive into the RGB channels of a single secondary texture. (2 Keywords, 6 branches on top of what LIT already uses)

    As our release platform is extremely controlled, our oldest Nvidia card uses shader model 6 so we don't need to worry about supporting variants/fallbacks below that let alone the other GPU manufacturers. Is there a simple way we can exclude shader models and features like fog from Lit without rewriting it by hand? I'm passable in HLSL at best and am not sure I can safely gut URP Lit for single pass instanced XR rendering with the fancy PBR lighting.

    Side note, I'm open to constructive feedback on the attached shader. I know there's a better way to achieve our goal, I'm just not experienced enough in tech-artistry to grasp it yet.
     

    Attached Files:

  31. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    I'd love to get a bug report if you have the same variants compiled over and over again.
     
  32. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    I can't send the application scene due to the nature of our training content, but will see if I can get a demo scene put together for uploading as a report. (life would be so much easier if we only made games)
     
    aleksandrk likes this.
  33. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    unitybuildtime.png

    Is there any known solution?
     
  34. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
  35. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Ok, will do. Thank you.
     
  36. Ebonicus

    Ebonicus

    Joined:
    Oct 31, 2016
    Posts:
    158
    stonstad likes this.
  37. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    Yes, try modifying this GitHub tool to fit your project and using it. It applies changes to all shaders.
    Though by default it doesn't strip hidden, legacy, unlit, particles. I recommend testing commenting out that line.
    https://gist.github.com/yasirkula/d8fa2fb5f22aefcc7a232f6feeb91db7

    I would add these to keywords if they aren't used in your project.
    Code (CSharp):
    1.         new ShaderKeyword( "VERTEXLIGHT_ON" ),
    2.         new ShaderKeyword( "LOD_FADE_CROSSFADE" ),
    Use this asset to remove variants from the build.
    https://assetstore.unity.com/packages/vfx/shaders/shader-control-74817

    It doesn't modify shader files, instead creates a scriptableObject in the Resources folder and uses
    IPreprocessShaders and
    OnProcessShader
    to remove variants on build time.

    Not just variants, you can also use it to remove a whole shader (including their variants) from a build.
    For example, I use Microsplat, and although its unused unity terrain was still getting in the build. I removed unity terrain from the build using it.

    But some Hidden/TerrainEngine should be included even if you use Microsplat, without them, Microsplat also doesn't work properly. I think these are related and there are more but I didn't test each of them;
    Hidden/TerrainEngine/Splatmap/Diffuse-Addpass
    Hidden/TerrainEngine/Splatmap/Diffuse-Base

    You can also use Shader Control to remove the LIGHTMAP_ON keyword from shaders that you never bake lightmaps with. Of course, you can modify shaders to remove that, but if you are using assets and don't want to modify them, or for Hidden internal shaders its a nice option.

    BetterLit also has its own shader stripper. If you are using BetterLit, use BetterLitVariantConfig and enable the toggles for options that you aren't using.

    In Project Settings/Graphics there is a ShaderStripping header and under that many options. Disable everything that you aren't using. For example Fog Modes and lightmap modes.

    After these changes, my build time was reduced from 7 minutes to 2 minutes. Build size reduced by 30mb. Memory use was reduced by 100mb.
     
    Last edited: Mar 20, 2023
  38. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,019
    Absolutely. We have tools to do that. We just don't know which variants are unused in a given project.
    For things that are supposed to be per object (as in, specified on the materials), we only compile the variants that are needed. For things that the project may want to switch at runtime, one can either configure URP or write their own variant stripping based on the knowledge of the actual project.