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. electro_unity

    electro_unity

    Joined:
    Nov 29, 2015
    Posts:
    64
    I'm finishing my android project, it is very simple and the unique shaders I use are the TextMeshPro Mobile Diffuse and Projector - Light. This "Compiling shader variants" takes the 99% of the build time and it last for almost an hour. Any solution?
     
    tigerleapgorge likes this.
  2. Juho_Oravainen

    Juho_Oravainen

    Unity Technologies

    Joined:
    Jun 5, 2014
    Posts:
    42
    Hi!

    This sort of issues are often caused by people mistakenly adding some complex shader in the "Always Included Shaders" list in the graphics settings which forces all the variants of that shader to be included in the build. Please check your settings.

    However, none of the shaders mentioned above sound complex enough to cause ~hour compile time even with all the variants included. So if checking the graphics settings does not help then please file a bug with the project attached (or preferably a stripped down version still causing long build time) so we can reproduce the issue locally and dive into it.
     
  3. perziili

    perziili

    Joined:
    Apr 24, 2018
    Posts:
    6
    I need to "always include" couple of shaders so that my runtime glTF imports work. This causes tens of thousands shader variants to be compiled for every build. Any way to reduce that?
     
    tigerleapgorge likes this.
  4. Juho_Oravainen

    Juho_Oravainen

    Unity Technologies

    Joined:
    Jun 5, 2014
    Posts:
    42
    If you know in advance that only a subset of variants is needed in the build, you could remove the shader from the "always include" list and instead create a ShaderVariantCollection identifying those variants and include that in the build. This will force those variants to be included.

    However, if you really need to include _all_ variants of those shaders then there is no way around this. Though after the first build all the variants are fetched from the shader cache which should happen quite quickly.

    Note: There is one known bug causing abnormally long shader compilation times (even with warmed up cache) when "Optimize Mesh Data" is enabled in the player settings. You could try disabling that to see if it reduces the build time. A fix for this is in the works...
     
  5. sadeep

    sadeep

    Joined:
    Aug 31, 2018
    Posts:
    1
    this did the trick thanks
     
    tigerleapgorge likes this.
  6. Gerlich

    Gerlich

    Joined:
    Nov 3, 2015
    Posts:
    37
    Unticking "Optimize Mesh Data" reduced my building time (I have over 200 scenes...) from roughly 20 minutes to around 45 seconds.
    You sir, are a genius and I will gift you a beer, shall we once meet!
     
  7. sj631

    sj631

    Joined:
    Dec 23, 2016
    Posts:
    22
    Any Updates. I am using Unity 2019.4.21f1 with Universal Render Pipeline (URP). It is taking a long time on "Mesh Data Optimization - Resolving used channels". Time reduced on unchecking "Optimize Mesh Data".
     
  8. corb555

    corb555

    Joined:
    Apr 27, 2013
    Posts:
    13
    Why does Unity recompile all the shaders on every game build even if they haven't changed? My game build takes 4 hours on a 6 core Intel I7 and almost all the time is recompiling shaders.
     
    LordCafe likes this.
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    @corb555 it's not recompiling the shaders if they didn't change, it reads them from the disk cache.
     
    DavidZobrist likes this.
  10. corb555

    corb555

    Joined:
    Apr 27, 2013
    Posts:
    13
    What causes the Shaders to change? I build the game every 2 days and it always recompiles all the shaders and takes 4 hours?
     
  11. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Same here. I always wanted to submit a bug-report for the issue, but it's so unreliable to reproduce. But I observe this issue very very often.
    1. I make changes to a shader and create a build: Unity recompiles shaders that are in the same asset bundle (expected).
    2. Then I make script changes only and create another build: no shaders recompiled (expected).
    3. Then I close Unity, leave the office, come back the next day, make script changes only, create another build: suddenly Unity recompiles every single shader in the project.
    While this occurs very often, it does not occur always. There must be some condition that triggers the recompile that I don't understand.

    I use Addressables to store shaders in Asset Bundles, do you use Addressables and Asset Bundles too?
     
    kmowers, KarlKarl2000 and Hoglet like this.
  12. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    Been swapping back and forth between 2020 and 2021 .. too many version to name.. but this compile shader and other editor regressions are killing productivity for the past 3 days


    Screenshot 2021-04-28 232541.jpg
     
  13. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    But what about when working with Amplify or Shader Graph? Becomes problematic if shaders are being recompiled every time I make a single change to a shader. Not a great workflow if you want to tweak visuals in project. Compiling thousands of shader variants is the longest part of the build process - and my project only uses a handful of shaders throughout.
     
  14. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Only the shaders that changed are recompiled.
    If it uses a handful of variants, you could make it build only those variants that you know you need using the IPreprocessShaders interface.
     
  15. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    131
    I contest this. We have insanely large build times sometimes because all shaders are recompiled, even when only a single shader has changed. We run our builds on TeamCity and have a 5 hour time limit on build duration, and we frequently hit this because the shaders have all seemingly randomly decided to recompile. I can't see anything in the logs which would indicate why they are recompiling.

    We're on version 2020.3.2f1
     
  16. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Shaders may be recompiled in the following cases:
    • Unity updated to a newer version
    • Shader source changed
    • One of the included files changed
    • Build target changed
    • Normal map encoding changed
    • Lightmap encoding changed
    • Color space changed
    • Graphics tier settings changed
    There are some other platform-specific settings that may cause shaders to be recompiled as well.

    If you don't touch the settings and don't update to a newer Unity version, and all shaders are still recompiled, this is a bug that I'd love to have a repro for.
     
    kmowers likes this.
  17. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    131
    As far as I can tell, none of these things are happening, it just seems random. It is intermittent so a repro is gonna be difficult. I'll submit a bug though if I can repro it
     
    aleksandrk likes this.
  18. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Thank you! :)
     
  19. BlackclawsK

    BlackclawsK

    Joined:
    Jan 9, 2019
    Posts:
    100
    So I can confirm that this is indeed an issue. Even running two builds immediately after one another the second build still compiles shader variants. This uses up absurd amounts of time just to test a small change so its definitely an issue.

    If its relevant I am building iOS projects on a Linux machine which are then fully built on a mac.

    However the same happens when building for android twice in a row.

    Happens on Unity 2021.2.0b3.

    EDIT:

    To reproduce this I can simply create a new 3D Mobile game from the included template. That already shows the issue for me. The second build IS faster but it still spends a lot of time going through compiling shader variants.
     
    Last edited: Jul 21, 2021
    ANTEVETY likes this.
  20. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    @BlackclawsK can you please check the shader compilation stats in the Editor log? Does it say it compiles variants or it says it gets them from the cache?
    Look for "Local cache hits" in the log.
     
  21. BlackclawsK

    BlackclawsK

    Joined:
    Jan 9, 2019
    Posts:
    100
    Apparently it gets them from cache. However it still goes through all variants and takes 0.3 seconds for each variant. For some reason each variant shows up multiple times in each build. I'm not sure why that is or how to prevent that from happening. I've tried to attach an editor log but it won't let me as .log is apparently not a supported extension. The log is showing the extremely long time spent in compiling shader variants (275 instances) for what is essentially a very small project with a total build time of 4.5 minutes (most of it spent in compiling shader variants)

    As each variant invariably seems to take up at least 0.3 seconds (for some reason) this adds at least one and a half minutes of build time (actually much longer considering the time unity takes to start the process for each shader variant, looking at the progress bar I'd say about 60-70% of the time is taken up in this step while il2cpp takes only around 2 seconds to run). While this might not seem like a lot to the people here experiencing hours of build time, for me since I am iterating an AR application that has to be build and deployed to test changed on device this is a bigger issue.

    Even more annoying is the fact that my cpu/disk (nvme) and pretty much the entire system is idling and not even remotely operating at peak capacity. Unity doesn't even use a single core to its full extent.

    I'm guessing something is happening here which is delaying the whole operation for no apparent reason.

    This is definitely a regression. I've built the same project with 2020.3 and the whole build takes 36 seconds compared to 277 seconds in 2021.2b3

    Excerpts from the build log:

    2021.2b3:
    2020.3
    I've just tried it with 2021.2b4 and the issue persists.

    For good measure: I can also not reproduce the issue in 2021.1.15f1

    So this seems to be a regression in the 2021.2 line.
     
  22. BlackclawsK

    BlackclawsK

    Joined:
    Jan 9, 2019
    Posts:
    100
    I've submitted a bug report: https://fogbugz.unity3d.com/default.asp?1352585

    I've also noticed that asset importing in 2021.2 takes significantly longer. This is especially noticable when switching build targets from within the editor for the first time as it reimports from com.unity.visualscripting. It takes 0.6 seconds per asset in 2021.2 while it only takes 0.01 second per asset in 2021.1

    I've also submitted a bug report on that issue: https://fogbugz.unity3d.com/default.asp?1352593

    They _might_ be related.
     
    Last edited: Jul 23, 2021
  23. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Thank you for the reported bugs!
    It seems at least stripping became slower in 2021.2. We'll take a look :)

    Side note: please change the personal Fogbugz links to just IDs of the issues.
     
  24. BlackclawsK

    BlackclawsK

    Joined:
    Jan 9, 2019
    Posts:
    100
    If you tell me where I can upload the full build logs that showcase the difference in build time on the same system even better.

    How do I change the links to just the issue id and still allow viewing of the content? If I remove the string at the end then the issue is no longer viewable from just the link alone.
     
  25. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    When the issue is verified and classified as a bug, a public issue tracker link will appear.
    For now just the IDs without the links would be enough.
     
  26. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    I think you can just reply to the email that you got from the bug tracker and attach files there.
     
  27. akashdarshan99

    akashdarshan99

    Joined:
    Feb 26, 2020
    Posts:
    8
    @aleksandrk I hope this is a top priority because my 10gb project takes almost 15 hours to build everytime i add something to the URP renderer on a i7 9700F + ssd
     
  28. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    @akashdarshan99 how many variants does it compile vs fetch from the cache?
    Can you please post the timings from the log here as well?
     
  29. akashdarshan99

    akashdarshan99

    Joined:
    Feb 26, 2020
    Posts:
    8
    @aleksandrk next time I build, i'll definitely gather logs for it. But i'll explain the process of what I did exactly.
    I'm using a URP project with a deferred renderer and the Graphic settings used to show a count of approximately 100-200 tracked shader and variants after the first build ever after migrating my project to URP took 8 hours for the initial build.
    Subsequently while playing with forward renderer for overlay camera the build kept on going for 10+ hours. So i cancelled that and shutdown my pc at the eod and next day when i started building it took 5 more hours to complete.

    But once the project is built fully, when i dont change anything in the renderer/pipeline asset the build happens within 10 minutes or so.

    Now after adding decals to the renderer, again when i started building, it went on for more than 6 hours.
    The last count of variants in denominator i saw was 256000 for URP/Simple Lit and this was the exact number
    While building i came across this post and to my relief this was already recognized as a regression in 2021.2 and i cancelled it midway

    Hopefully this helps for now. Once i have the logs for it, the first thing i'll do is post it here and ping you :)

    Thanks for the immediate response. Cheers :)

    EDIT:

    My current graphic setting shows a number of 22 shaders, 32 variants which is very low compared to the last number i remember
     
    Last edited: Jul 23, 2021
  30. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    This has nothing to do with the amount of variants that go into the build :)

    If it needs to compile that many variants, I would expect it to take a really long time to compile everything anew.
     
  31. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    131
    This is the output of our most recent build where the Universal Render Pipeline/Lit shader seemingly randomly recompiled itself. We haven't made any shader changes in this build... the shader in question is a built-in URP shader anyways so we definitely haven't changed the source of that.
    Code (CSharp):
    1. [20:14:24] :         [Build] Compiling shader "Universal Render Pipeline/Lit" pass "ForwardLit" (fp)
    2. [20:14:25] :         [Build]     230400 variants, starting stripping...
    3. [20:14:25] :         [Build]     finished in 0.10 seconds. 25600 variants left
    4. [20:14:25] :         [Build]     starting compilation...
    5. [20:15:26] :         [Build]     [ 61s] 1181 / 25600 variants ready
    6. [20:16:26] :         [Build]     [121s] 2357 / 25600 variants ready
    7. [20:17:27] :         [Build]     [182s] 2828 / 25600 variants ready
    8. [20:18:28] :         [Build]     [243s] 3297 / 25600 variants ready
    9. [20:19:28] :         [Build]     [303s] 3788 / 25600 variants ready
    10. [20:20:29] :         [Build]     [364s] 4285 / 25600 variants ready
    11. [20:21:29] :         [Build]     [424s] 4782 / 25600 variants ready
    12. [20:22:30] :         [Build]     [485s] 5841 / 25600 variants ready
    13. [20:23:31] :         [Build]     [547s] 6896 / 25600 variants ready
    14. [20:24:33] :         [Build]     [608s] 7410 / 25600 variants ready
    15. [20:25:34] :         [Build]     [669s] 7924 / 25600 variants ready
    16. [20:26:34] :         [Build]     [729s] 8435 / 25600 variants ready
    17. [20:27:36] :         [Build]     [791s] 8950 / 25600 variants ready
    18. [20:28:35] :         [Build]     [851s] 9465 / 25600 variants ready
    19. [20:29:36] :         [Build]     [911s] 10014 / 25600 variants ready
    20. [20:30:37] :         [Build]     [972s] 10557 / 25600 variants ready
    21. [20:31:39] :         [Build]     [1034s] 11129 / 25600 variants ready
    22. [20:32:39] :         [Build]     [1094s] 11678 / 25600 variants ready
    23. [20:33:39] :         [Build]     [1154s] 12229 / 25600 variants ready
    24. [20:34:40] :         [Build]     [1215s] 12805 / 25600 variants ready
    25. [20:35:41] :         [Build]     [1275s] 14056 / 25600 variants ready
    26. [20:36:40] :         [Build]     [1336s] 17265 / 25600 variants ready
    27. [20:37:41] :         [Build]     [1396s] 18791 / 25600 variants ready
    28. [20:38:41] :         [Build]     [1456s] 20539 / 25600 variants ready
    29. [20:39:42] :         [Build]     [1516s] 23386 / 25600 variants ready
    30. [20:40:42] :         [Build]     [1577s] 25166 / 25600 variants ready
    31. [20:41:00] :         [Build]     finished in 1594.75 seconds. Local cache hits 9 (0.01s CPU time), remote cache hits 0 (0.00s CPU time), compiled 25591 variants (25511.99s CPU time)
    Now I notice that @aleksandrk in your list of things which can cause shaders to recompile, one of them is understandably changing platform. In the case of this build machine, the build before this one was for XboxOne and this build was for Win64, so a change of platform has happened. However this build machine has made many Win64 builds before, so aren't the compiled shaders cached?

    On another build machine, the exact same transition from an XboxOne to Win64 build happened, and that didn't cause the shader to recompile, it found all of its results in the cache:

    Code (CSharp):
    1. [20:09:03] :         [Build] Compiling shader "Universal Render Pipeline/Lit" pass "ForwardLit" (fp)
    2. [20:09:03] :         [Build]     162432 variants, starting stripping...
    3. [20:09:03] :         [Build]     finished in 0.07 seconds. 18048 variants left
    4. [20:09:03] :         [Build]     starting compilation...
    5. [20:09:39] :         [Build]     finished in 35.49 seconds. Local cache hits 18048 (567.48s CPU time), remote cache hits 0 (0.00s CPU time), compiled 0 variants (0.00s CPU time)
    I don't have a repro project so I realise that this is only so helpful. Anyways, the improved shader compile logging is greatly appreciated! That seens to have snuck in since 2020.3.2
     
  32. akashdarshan99

    akashdarshan99

    Joined:
    Feb 26, 2020
    Posts:
    8
    But still it doesn't make sense to me as why it would take a fresh build in Unity 2020.3 to compile in within 6 hours for the first time without any shader cache and the same project takes almost twice the time on the same machine in Unity 2021.2?

    as @hugeandy mentioned, the URP/Basic Lit shader is a shader built into the project. I dont see a reason as for why it should recompile every variant for just adding Decals to the renderer feature. I use only a total of 6 decal materials with all of them using a custom shader graph and not the URP/Basic Lit Shader :)

    When i was in 2020.3 the only time it had to recompile variants was whenever i upgraded the unity first time to 2020.3 but after that all my builds were using the shaders from cache.
     
  33. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    They should be if the shaders didn't change. I'd love to get a solid repro for a case where nothing changes, Library/ShaderCache is intact and it still compiles the variants and doesn't take them from the cache.

    They're in since 2020.3.4f1 :)

    I suppose that's due to a newer URP version. They added a lot of new features, so the amount of variants increased. AFAIK work is being done to reduce the number of variants as much as possible.
     
    Immu likes this.
  34. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    While not directly related to the issue, I thought I leave the following information here, because not everybody is aware of it:

    When you use CLI-based builds such as Jenkins, you can often significantly reduce build times when you change to a "one checkout per platform" strategy. Here is the conversation in case you're interested: https://forum.unity.com/threads/aut...riation-in-example-repo.1073480/#post-6950015
     
    aleksandrk likes this.
  35. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    For large projects the drawback is that this can eat up disk space pretty dramatically. The upside is that you can build multiple platforms in parallel if you have a beefy build machine (since most of Unity's build process is single threaded).
     
  36. DEEnvironment

    DEEnvironment

    Joined:
    Dec 30, 2018
    Posts:
    437

    hello sir
    is this also possible related to the FBX import setting Optimize Mesh about this same bug ?
    i ask as this setting seams to be also related to people getting pink in the preview but looks ok in scene when porting between versions
     
  37. akashdarshan99

    akashdarshan99

    Joined:
    Feb 26, 2020
    Posts:
    8
    @aleksandrk here's the timing of my results for approximately 1/4th of my build time with all renderer features such as SSAO and Decal removed. The timings have been captured after running the build for 1 hr previously and cancelling the build.

    My specs: i7 9700F 8cores, 32gb, SSD

    https://hastebin.com/ayixewawug.yaml

    My CPU usage remains high all the time (8 unity shader compiler processes) and its kinda unusable for anything other than browsing. But normally in unity 2020.3 it takes around an hour in total for the first build the of project without any renderer feature in the rp asset

    EDIT:

    Updated the link with a total of 5 hours discontinuous build time
     
    Last edited: Jul 24, 2021
  38. akashdarshan99

    akashdarshan99

    Joined:
    Feb 26, 2020
    Posts:
    8
    One more bad thing which I noticed is the shader stripping.
    Currently I'm using Strip All but despite that there are some custom shaders which I made, which are lying in project but I haven't used them in any scene and still they are being compiled.

    Btw:
    Been 11 hours and still my project hasnt finished building :)

    EDIT: 12hrs 18mins to build a 753mb build
     
    Last edited: Jul 25, 2021
  39. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    @akashdarshan99 well, it says it's compiling nearly 53K out of 122880 variants.

    finished in 13874.49 seconds. Local cache hits 52495 (22.50s CPU time), remote cache hits 0 (0.00s CPU time), compiled 52963 variants (110964.97s CPU time)

    I'll ask somebody from the URP team to comment on this, but, as I said, AFAIK they are in the process of reducing the amount of variants.
     
  40. akashdarshan99

    akashdarshan99

    Joined:
    Feb 26, 2020
    Posts:
    8
    yeah thats because unfortunately I'm not able to leave my desktop just for building on for whole 12 hours which it took to make the complete build because the system is completely unusable at the time the shader variants are being compiled.

    when i want to work on something else i usually cancel the build mid way, and then do my work. and later when i'm taking a break of something i put it back to build.

    in my case as long as im not changing/adding anything in the renderer/shader/material it gets everything from the cache which is a good thing. changing or adding or even removing anything from renderer asset causes rebuild of everything which is little disappointing. i wouldn't mind if it takes 2-3 hours because nothing in this world is perfect, its just the insanely huge time which it takes to build which is my main problem.

    for now im sticking with 2020.3 but im pointing out the few issues without which 2021 will be usuable for me.

    Thank once again for the speedy reply. Much appreciated.
    Also thank you for taking your time to investigate this issue with the URP team.
    Hope to see this fix sooner :)
     
  41. BlackclawsK

    BlackclawsK

    Joined:
    Jan 9, 2019
    Posts:
    100
    Just FYI to anyone reading this. The issue in 2021.2 is known and is on the list of things to fix, it unfortunately does not have a public issue tracker entry. Just in case anyone else stumbles across it.
     
  42. LarryTheBrave

    LarryTheBrave

    Joined:
    Nov 20, 2016
    Posts:
    24
    For now I think the thing to do that seems like it helped, is use Standard Shader model if you can. Universal Render Pipeline seems to cause all this Shader Variant compiling and when I switched to Standard on everything the build went MUCH faster! Maybe use Universal only on the Final build IF you really need that.
     
    sj631 likes this.
  43. ANTEVETY

    ANTEVETY

    Joined:
    Nov 20, 2012
    Posts:
    2
    Yes, the problem remains (2021.2b7) in URP and HDRP.
     
  44. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    upload_2021-9-9_17-46-3.png

    is this still true?! 2020.3
     
  45. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    sj631 likes this.
  46. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
  47. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    ahh sorry I misread, the fix is not out yet! I am on 2020.3!
     
    aleksandrk likes this.
  48. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    @aleksandrk did something happen? I have very slow building/compiling shader times again, even with mesh optimizations off.
     
  49. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    @sebas77 not that I know of... It's likely some other issue. Can you please check the Editor log when it compiles variants and check if the timings seem reasonable?
     
  50. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    the problem is not just that it's slow, but unity builds the same shader multiple times. My gut feeling is that addressable somehow messes up the shader compilation. Like shaders using among addressable are not recognised as the same shader.
     
    glenneroo and craftsmanbeck like this.