When did editor shader compilation start taking forever and how to speed it up?

I used to love working on shaders because unless you had a ton of includes, compilation was lightning fast. Recently noticed that any shader tweek now takes forever to compile and unity grinds to a halt while doing so.

What happened and is there any way to speed this up?

If this is during building, often unticking “optimize mesh data” (in player settings) can improve times by up to 95%. Enterprise project we had went from 2 hours build time to 5 minutes with that alone :wink: (As in if its shader compilation in the build that is taking ages, which usually is the main thing taking up time during building)

If in editor without building, I have no idea but I have not noticed this on any versions I am using (including the beta)

I’m talking about editing shaders and the internal editor compilation step when you save the shader. You haven’t noticed any slowdown compared to a few years ago??

1 Like

So for example. I open a shader file. Paste in a super simple example from the manual:

Shader "Custom/TextureCoordinates/ChessOpt" {
   SubShader {
       Pass {
           CGPROGRAM
           #pragma vertex vert_img
           #pragma fragment frag

           #include "UnityCG.cginc"

           fixed4 frag(v2f_img i) : SV_Target {
               bool p = fmod(i.uv.x*8.0,2.0) < 1.0;
               bool q = fmod(i.uv.y*8.0,2.0) > 1.0;
              
               return fixed4(fixed3((p && q) || !(p || q)),1.0);
           }
           ENDCG
       }
   }
}

It takes 39 seconds for unity to compile this! What happened? It used to be like a half second!

No not noticing that on my side, I suggest you file a bug report!

1 Like

What Unity version?

This is 2019.4.23f1 - maybe I should update?

Is this really not a thing for others?

Try deleting \library and re-importing the project. That seemed to solve it for me last time I ran into it.

1 Like

I’ll try that tonight, thanks for the tip.

If your Library/ShaderCache.db file is “fairly huge” then you might be affected by this bug https://issuetracker.unity3d.com/issues/shader-error-db-grows-on-each-build – workaround is to either delete Library/ShaderCache.db once/if it grows large, or for Unity 2019 versions, to update to 2019.4.29 where it’s fixed (release notes).

7 Likes