Search Unity

Long shader import times and timeouts when too many variants are used

Discussion in 'Shaders' started by skatebee, Nov 12, 2020.

  1. skatebee

    skatebee

    Joined:
    Jan 6, 2020
    Posts:
    4
    Hi there

    I'm trying to make an ubershader utilising keywords and directives such as #pragma shader_feature_local. From what I've read, there is a limit of 64 local keywords for a shader. However in practice when using only around 10 keywords, the compilation time balloons to many minutes every time my shader compiles in the editor after saving changes to the shader file. By the time I start pushing 15 keywords the shader compiler seems to timeout after a long while and the editor fails to import the shader asset entirely. In these cases the error in the console says: Shader Compiler Socket Exception: Terminating shader compiler process.

    I even get these results in an empty Unity project. I'm just wondering whether this is abnormal behaviour. I've seen mention of people using around 30 or up to 64 local keywords in a shader but I can't seem to come close. Are these times and timeouts expected? Or does anyone have any clues as to what is going on?
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Hi!
    Can you please report a bug?
    We haven't seen this behaviour, so it would be great to figure out, what exactly is going on with your shader.
    Thank you!
     
  3. skatebee

    skatebee

    Joined:
    Jan 6, 2020
    Posts:
    4
    I reported this as a bug and was told this was expected behaviour. Here was the full response:

    Regarding the import time, unfortunately, this is expected behavior, since 15 keywords mean that during import, about 32 thousand shader variants have to be analyzed and that's why the import time grows exponentially with keyword count for surface shaders, but currently that’s how the system works right now.​

    No mention of the timeout error but I suppose that's expected behaviour too. When submitting the bug report I noticed the compiler gives up after exactly 10 minutes. Makes sense, but I'm just surprised I haven't seen anyone else mention that they've into this problem.
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Ah, indeed, surface shaders indeed do a lot of stuff under the hood. Sorry, I thought you had this behaviour with regular shaders.

    Yes, the timeout is there, but it can be controlled via an environment variable (UNITY_SHADER_COMPILER_TASK_TIMEOUT_MINUTES).
     
    dave_g_69 and skatebee like this.