El Sombreador integrado incluye archivos
Built-in shader helper functions

Predefined shader preprocessor macros

Unity defines several preprocessor macros when compiling shader programs.

Target platform

Macro: Target platform:
* SHADER_API_D3D9 - Direct3D 9 Direct3D 9
* SHADER_API_D3D9 - Direct3D 9 Direct3D 11
SHADER_API_GLCORE * SHADER_API_OPENGL - desktop OpenGL
SHADER_API_OPENGL * SHADER_API_OPENGL - desktop OpenGL
* SHADER_API_GLES - OpenGL ES 2.0 OpenGL ES 2.0
* SHADER_API_GLES - OpenGL ES 2.0 OpenGL ES 3.0/3.1
* SHADER_API_METAL - iOS Metal iOS/Mac Metal
* SHADER_API_D3D9 - Direct3D 9 Direct3D 11 “feature level 9.x” target for Windows Store and Windows Phone
* SHADER_API_PS4 - PlayStation 4 PlayStation 4
* SHADER_API_XBOXONE - Xbox One Xbox One
* SHADER_API_PS3 - PlayStation 3 PlayStation 3
* SHADER_API_XBOX360 - Xbox 360 Xbox 360
* SHADER_API_PSP2 - PlayStation Vita PlayStation Vita
* SHADER_API_GLES - OpenGL ES 2.0 Nintendo WiiU

SHADER_API_MOBILE is defined for all general mobile platforms (GLES, GLES3, METAL, PSP2).

Additionally, SHADER_TARGET_GLSL is defined when the target shading language is GLSL (always true for OpenGL/GLES platforms).

Shader target model

SHADER_TARGET is defined to a numeric value that matches the shader target compilation model (i.e. matching #pragma target directive). For example, SHADER_TARGET is 30 when compiling into shader model 3.0. You can use it in shader code to do conditional checks. For example:

#if SHADER_TARGET < 30
    // less than shader model 3.0:
    // very limited shader capabilities, do some approximation
#else
    // decent capabilities, do a better thing
#endif

Versión de Unity

UNITY_VERSION contains the numeric value of the Unity version. For example, UNITY_VERSION is 501 for Unity 5.0.1. This can be used for version comparisons if you need to write shaders that use different built-in shader functionality. For example, a #if UNITY_VERSION >= 500 preprocessor check will only pass on versions 5.0.0 or later.

Platform difference helpers

Direct use of these platform macros is discouraged, as they don’t always contribute to the future-proofing of your code. For example, if you’re writing a shader that checks for D3D9, you may want to ensure that, in the future, the check is extended to include D3D11. Instead, Unity defines several helper macros (in HLSLSupport.cginc):

Macro: Use:
UNITY_BRANCH Add this before conditional statements to tell the compiler that this should be compiled into an actual branch. Expands to [branch] when on HLSL platforms.
UNITY_FLATTEN Add this before conditional statements to tell the compiler that this should be flattened to avoid an actual branch instruction. Expands to [flatten] when on HLSL platforms.
UNITY_NO_SCREENSPACE_SHADOWS Defined on platforms that do not use cascaded screenspace shadowmaps (mobile platforms).
UNITY_NO_LINEAR_COLORSPACE Defined on platforms that do not support Linear color space (mobile platforms).
UNITY_NO_RGBM Defined on platforms where RGBM compression for lightmaps is not used (mobile platforms).
UNITY_NO_DXT5nm Defined on platforms that do not use DXT5nm normal-map compression (mobile platforms).
UNITY_FRAMEBUFFER_FETCH_AVAILABLE Defined on platforms where “framebuffer color fetch” functionality can be available (generally iOS platforms - OpenGL ES 2.0, 3.0 and Metal).
UNITY_USE_RGBA_FOR_POINT_SHADOWS Defined on platforms where point light shadowmaps use RGBA textures with encoded depth (other platforms use single-channel floating point textures).
UNITY_ATTEN_CHANNEL Defines which channel of light attenuation texture contains the data; used in per-pixel lighting code. Defined to either ‘r’ or ‘a’.
UNITY_HALF_TEXEL_OFFSET Defined on platforms that need a half-texel offset adjustment in mapping texels to pixels (e.g. Direct3D 9).
UNITY_UV_STARTS_AT_TOP Always defined with value of 1 or 0. A value of 1 is on platforms where texture V coordinate is 0 at “top of the texture”. Direct3D-like platforms use value of 1; OpenGL-like platforms use value of 0.
UNITY_MIGHT_NOT_HAVE_DEPTH_TEXTURE Defined if a platform might emulate shadow maps or depth textures by manually rendering depth into a texture.
UNITY_PROJ_COORD(a) Given a 4-component vector, this returns a texture coordinate suitable for projected texture reads. On most platforms this returns the given value directly.
UNITY_NEAR_CLIP_VALUE Defined to the value of near clipping plane. Direct3D-like platforms use 0.0 while OpenGL-like platforms use –1.0.
UNITY_VPOS_TYPE Defines the data type required for pixel position input (VPOS): float2 on D3D9, float4 elsewhere.
UNITY_CAN_COMPILE_TESSELLATION Defined when the shader compiler “understands” the tessellation shader HLSL syntax (currently only D3D11).
UNITY_INITIALIZE_OUTPUT(type,name) Initializes the variable name of given type to zero.
UNITY_COMPILER_HLSL, UNITY_COMPILER_HLSL2GLSL, UNITY_COMPILER_CG Indicates which shader compiler is being used to compile shaders. Respectively, Microsoft’s HLSL, HLSL to GLSL translator, and NVIDIA’s Cg. See shading language page for more details. Use this if you run into very specific corner case shader syntax handling differences between the compilers, and want to write different code for each compiler.

Shadow mapping macros

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

Macro: Use:
UNITY_DECLARE_SHADOWMAP(tex) Declares a shadowmap texture variable with name “tex”.
UNITY_SAMPLE_SHADOW(tex,uv) Samples shadowmap texture “tex” at given “uv” coordinate (XY components are texture location, Z component is depth to compare with). Returns single float value with the shadow term in 0..1 range.
UNITY_SAMPLE_SHADOW_PROJ(tex,uv) Similar to above, but does a projective shadowmap read. “uv” is a float4, all other components are divided by .w for doing the lookup.

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

Texture / Sampler declaration macros

Usually you would use texture2D in shader code to declare a texture and sampler pair. However on some platforms (such as DX11), textures and samplers are separate objects, and maximum possible sampler count is quite limited. Unity has some macros to declare textures without samplers, and to sample a texture using a sampler from another texture. Use this if you end up running into sampler limits, and you know that several of your textures can in fact share a sampler (sampler defines texture filtering and wrapping modes).

Macro: Use:
UNITY_DECLARE_TEX2D(name) Declares a texture and sampler pair.
UNITY_DECLARE_TEX2D_NOSAMPLER(name) Declares a texture without a sampler.
UNITY_DECLARE_TEX2DARRAY(name) Declares a texture array sampler variable.
UNITY_SAMPLE_TEX2D(name,uv) Sample from a texture and sampler pair, using given texture coordinate.
UNITY_SAMPLE_TEX2D_SAMPLER( name,samplername,uv) Sample from texture (name), using sampler from another texture (samplername).
UNITY_SAMPLE_TEX2DARRAY(name,uv) Sample from a texture array with a float3 UV; the z component of the coordinate is array element index.
UNITY_SAMPLE_TEX2DARRAY_LOD(name,uv,lod) Sample from a texture array with an explicit mipmap level.

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:

Macro: Use:
UNITY_PASS_FORWARDBASE Forward rendering base pass (main directional light, lightmaps, SH).
UNITY_PASS_FORWARDADD Forward rendering additive pass (one light per pass).
UNITY_PASS_DEFERRED Deferred shading pass (renders g buffer).
UNITY_PASS_SHADOWCASTER Shadow caster and depth texture rendering pass.
UNITY_PASS_PREPASSBASE Legacy deferred lighting base pass (renders normals and specular exponent).
UNITY_PASS_PREPASSFINAL Legacy deferred lighting final pass (applies lighting and textures).

Véase también

El Sombreador integrado incluye archivos
Built-in shader helper functions