Predefined shader preprocessor macros

When compiling shader programs, Unity defines several preprocessor macros.

Target platform

Additionally, SHADER_TARGET_GLSL is defined when the target shading language is GLSL (always true when SHADER_API_GLES is defined; and can be true for SHADER_API_OPENGL when #pragma glsl is used).

SHADER_API_MOBILE is defined for SHADER_API_GLES when compiling for "mobile" platform (iOS/Android); and not defined when compiling for "desktop" (NativeClient).

Platform difference helpers

Direct use of these platform macros is discouraged, since it's not very future proof. For example, if you're writing a shader that checks for D3D9, then maybe in the future the check should be extended to include D3D11. Instead, Unity defines several helper macros (in HLSLSupport.cginc) to help with that.

Shadow mapping macros

Declaring and sampling shadow maps can be very different depending on the platform, so Unity has several macros to help with that:

Constant buffer macros

Direct3D 11 groups all shader variables into "constant buffers". Most of Unity's built-in variables are already grouped, but for variables in your own shaders it might be more optimal to put them into separate constant buffers depending on expected frequency of updates.

Use CBUFFER_START(name) and CBUFFER_END macros for that:

    CBUFFER_START(MyRarelyUpdatedVariables)
    float4 _SomeGlobalValue;
    CBUFFER_END

Surface shader pass indicators

When Surface Shaders are compiled, they end up generating a lot of code for various passes to do lighting. When compiling each pass, one of the following macros is defined:

Page last updated: 2013-03-04