• Unity
  • Compiling URP Shader Variants Takes Hours

Hi! I'm having issues when I Build out from Unity to either Windows or Mac standalone players, namely that the compiling of 165,888 Shader Variants takes hours on my PC, while consuming 100% of my CPU (an i5 with 4 cores at 3.50GHz). It takes a very, very long time:

Once it has built those shader variants once it doesn't need to do it again every single Build, it does cache them, but it repeats the process whenever I upgrade Unity, which is now something that I'm very loath to do when my computer has to become unusable for a long time.

My real problem is that I've tried to do the same Build on the same project with my 2015 Macbook Air but I've never waited long enough for it to finish (six+ hours) and perhaps it might take actual days on that under-powered machine. Back when I was using the old-school Unity renderer with its own Spine shaders I could build my entire game out in six minutes on that same Macbook.

My assumption, and I might be wrong here, is that Unity is taking the Spine shader and compiling every single combination of the Spine URP shader's options that the game might theoretically need. Is that just what Unity does with any shader that has the sort of Swiss Army Knife abilities that a shader like Spine's URP ones do? If so, what are my options here? A customised URP shader that has had an axe taken to it, removing all the options and keywords and whatnot that I just don't need, to speed up the compilation to make it possible on my Macbook Air?

Ideally I'd rather not go down that route because then it's harder to upgrade and get the latest spine shaders. Is there some Unity setting that I'm missing that culls the variant number? An addition to the Spine URP shaders that could help compile time?

Related Discussions
...

There seems to be something wrong with your project. When building for the PC, Mac and Linux Standalone target, building with the URP 3D Shaders example scene from the Spine URP shaders package as the included level (using the Universal RP/Spine/Sprite shader as well), it takes 1 minute to compile on my PC. This applies to both Unity 2019.4 and 2020.2 (I did not yet test other Unity versions). Build time also does not go up when building with the setting Project Settings - Graphics - Shader Stripping - Instancing Variants changed from the default Strip Unused to Keep All.

Did you try deleting the content of the Library directory, or at least reimporting the project?
If this does not help, could you please test building a clean new test project where you only add the Spine URP UPM package and select the URP 3D Shaders example scene as included level?
If the problem still occurs, you please provide more info about your project setup, such as the Unity version used, versions of the Unity Universal RP UPM package, screenshots of the Graphics Settings, Player Settings, etc.

Oh dear, I deleted the Library folder and then 'reimported all' and now all my shaders are pink in the editor.

Now, whenever I try and interact with a Spine game object my editor hangs for a few seconds:

and I can't click anything because then I'm back into that state, and so I have to shut Unity and open it again.

The six hour build for compiling shader variants was definitely present (for me) in Unity 2020.1.9f1 and again when I upgraded to 2020.2.7f1 yesterday, but after reimporting I seem to be in an even more broken state.

Clearly, there's something very wrong with my Unity project and how Spine is interacting with it, and I'm going to have to look into recovering this.


Okay, I've rolled back the state of my project to 2020.1.9f1, and escaped whatever totally broken state I found myself in in 2020.2.

Still though, after a deletion of (now safe) deletion of /Library/ and after a reimport, I'm still experiencing those long, long Build times. Harald, when you compile a URP project + URP Spine Shaders, are there not hundreds of thousands of variants that need to be compiled the first time around for you? Or do you have those hundreds of thousands of variants needing compilation, but it completes them very quickly? Just trying to determine whether there shouldn't be this build work being done, or whether it does get done, just usually much faster.

Now I'll try and replicate this in the clean new test project you described, see if I can reproduce the issue there.


I've tried creating the test project by:

  • Select URP from Unity Hub's create project list, using Unity 2020.1.9f1.

  • Add the latest versions of Spine Unity and the URP shaders package to the new project.

  • Change to the 'URP 3D Shaders' scene, and add it to the 'Scenes In Build' list.

  • Build...

... and it still takes hours? Despite the Unity upgrade difficulties I've mentioned in this thread, this makes me think it's not anything specific to my game project, but rather just the length of time it takes to compile all these shader variants on my hardware. I could send you a copy of my project but I genuinely just followed those four steps above and wasn't hard to replicate.


Okay, so rather than being a Spine Issue, or something on my end, this is... a Unity problem. Hooraaaay...

There's a huge thread over here: https://forum.unity.com/threads/compiling-shader-variants-taking-ages.527724/

And a second one over here: https://forum.unity.com/threads/shader-graph-takes-unbearable-long-time-to-compile.724247/#post-4847420

And probably some other threads as well. I've tried a few suggestions that have worked for other people, and I have brought it down from 6 hours to about 20 minutes, an improvement. But that's going to be an hour or two on my old Mac hardware, I suspect. Something to try tomorrow.

Harald, anything in those threads that stick out to you? It's the Spine shaders that this Unity issue is tripping up on, because there's hundreds of thousands of variants, or so Unity thinks.

8달 후

Oh dear, sorry to hear that you've gone through even more problems!

When performing the following steps, I still got only 1024 shader variants for the URP/Spine/Sprite shader when building:

  1. creating a new URP project in Unity 2020.2.1f1

  2. adding the spine-unity unitypackage

  3. adding the Spine URP UPM package,

  4. removing the existing default scene from the included scenes and adding the URP 3D Shaders scene.
    I did not modify project settings in any way, just left them at the default.

In general, the Sprite shader has a lot of keywords, so theoretically when shader stripping is disabled, it could sum up to the 150.000 variants that you are seeing, since you need "only" 17 binary feature-enabled / feature-disabled keywords to already have 217 = 131.072 variants. If Unity really includes every variant in

#pragma shader_feature _ _FIXED_NORMALS_VIEWSPACE _FIXED_NORMALS_VIEWSPACE_BACKFACE _FIXED_NORMALS_MODELSPACE _FIXED_NORMALS_MODELSPACE_BACKFACE _FIXED_NORMALS_WORLDSPACE

or

#pragma shader_feature _ _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON _ADDITIVEBLEND _ADDITIVEBLEND_SOFT _MULTIPLYBLEND _MULTIPLYBLEND_X2

each of these has 6 or 7 branches alone.

I will have a look if I can reduce the number of keywords a little bit, as some are actually duplicates that never occur independently, like
_MAIN_LIGHT_SHADOWS vs MAIN_LIGHT_CALCULATE_SHADOWS which are old vs new pipeline keywords for the same feature. Similar reduction could perhaps be performed for REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR and _MAIN_LIGHT_SHADOWS_CASCADE. This could at least reduce the number of variants by 4.


We have just committed a fix for the high number of multi_compile shader variants and thus high compile time to the 4.0 branch. Sorry that it took us so long to get to implement it!

A new spine-unity 4.0 URP shaders UPM package is available for download.
Spine Unity Download

Issue ticket URL for later reference:
https://github.com/EsotericSoftware/spine-runtimes/issues/1974

4일 후

Thanks Harald! Yes, this is still a pain point for me.

I'm still on Spine-Unity 3.8, do I have to upgrade to 4.0 to use the 4.0 URP Shaders, or are they compatible with the 3.8 runtime?

4달 후

Hi again Harald, I finally got around to upgrading my project to Spine 4.0, can confirm there's significant improvements in compile times for the shader variants, which is very welcome. Thanks!

Thanks for the feedback, very glad to hear! :nerd: