Search Unity

Pragma and Conditional Shader Compilation

Discussion in 'Shaders' started by sebsmax, Nov 19, 2017.

  1. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
    Hello,
    I'm trying to use a Conditional Shader Compilation with two different declarations.
    But unity won't compile it!
    Any Idea?

    (It look like #pragma is compiled before the condition statement)

    Code (CSharp):
    1.  
    2. #if SHADER_API_METAL
    3.             #pragma surface seasonSurface Standard addshadow
    4. #else
    5.             #pragma surface seasonSurface StandardTranslucent addshadow vertex:vert
    6. #endif
    7.  
    https://docs.unity3d.com/Manual/SL-BuiltinMacros.html
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    As far as I'm aware, you cannot put #pragmas inside of conditional compilations like that, as nice as that would be for setting up various material options. You have to use your conditionals to enable whatever keywords those pragmas usually enable.
     
  3. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
    Thanks, this is what I was scared off.
    Well too bad
     
  4. jjxtra

    jjxtra

    Joined:
    Aug 30, 2013
    Posts:
    1,464
    And the copy and pasting begins, seriously why doesn't this work?
     
  5. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,461
    It would actually be really nice if the whole shader could be run through the preprocessor before being compiled, so you could apply conditional defines and macros to the property structures and such as well. But, I imagine it’s not quite that simple as everything outside the cg blocks is engine framework, and ecerything inside is what’s passed to the compiler. So think of those pragmas not as shader code, but rather instructions to the game engine about your settings.
     
  6. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    You don't need to copy/paste. Use CGINC files to include repeated chunks of code in different passes, you can put conditionals in the CGINC as well. You can also put a CGINCLUDE block above your Subshader and include that stuff in all passes of that Subshader, wrapping both in a Category {}
     
  7. GuardHei

    GuardHei

    Joined:
    Feb 10, 2018
    Posts:
    89
    sorry, i am confused. i just want to know whether we can put #pragma in cginc files? from my test case it does not work because you always write "#ifndef xxx_include" "#define xxx_include" at the beginning of a include file, which prevents the file from using #pragma right?
     
  8. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    Indeed, you can't use #pragma in a cginclude. You can use defines though.
     
    GuardHei likes this.