Search Unity

How to have a shader with multi_compile_local supporting older version of Unity?

Discussion in 'Shaders' started by stephero, May 22, 2020.

  1. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Hi guys,
    I wanted to take advantage of the "new" shader local keywords introduced in Unity 2019.1.0 by using multi_compile_local instead of multi_compile.
    However I need my shader to be compatible with older versions of Unity which don't support this feature. So I told myself that's easy, I just have to do that:
    Code (CSharp):
    1. CGPROGRAM
    2. #pragma vertex vert
    3. #pragma fragment frag
    4. #pragma multi_compile_fog
    5.  
    6. #if UNITY_VERSION >= 201910
    7.     #pragma multi_compile_local __ FOO_ON
    8. #else
    9.     #pragma multi_compile __ FOO_ON
    10. #endif
    11.  
    12. #include "UnityCG.cginc"
    13.  
    14. struct appdata
    15. {
    16.     float4 vertex : POSITION;
    17.     float2 uv : TEXCOORD0;
    18. };
    While this works, it generates this very annoying warning message (tried in Unity 2019.3.14)
    Screenshot_77.png

    So why Unity tries to evaluate code which is supposed to be disabled by the preprocessor, and how can I support that without any warning?

    Thanks
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,025
    Hi!
    Unless Caching Shader Preprocessor is enabled (2020.1+ only), #pragma directives in the .shader file are not affected by the preprocessor (the reason is that they are parsed way before preprocessing).
    I just checked the code in 2018.4, it will just ignore the multi_compile_local directive, so you're good on that side :)
     
  3. stephero

    stephero

    Joined:
    Feb 8, 2016
    Posts:
    123
    Thanks for the answer.
    Indeed, it looks like the #pragma multi_compile_local are ignored in Unity 2018.4.21 without generating an error, but I can't do that since I would just looe my shader variants then...
    I think the only option is to stick with multi_compile for now...
    Thanks
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,025
    Well, you can keep both lines in there as you did with this snippet you posted initially, if you can live with this warning for some time.
     
    stephero likes this.