Unity3d shader pre-defined shader preprocessing macros

Source: Internet
Author: User

When you compile a shader program, Unity defines several preprocessing macros.

Target platform

    • SHADER_API_OPENGL-Desktop OpenGL

    • SHADER_API_D3D9-Direct3D 9

    • SHADER_API_XBOX360-Xbox 360

    • SHADER_API_PS3-PlayStation 3

    • SHADER_API_D3D11-Desktop Direct3D 11

    • SHADER_API_GLES-OpenGL ES 2.0 (desktop or mobile), using the presence of shader_api_mobile to determine.

    • SHADER_API_FLASH-Flash Stage3D

    • SHADER_API_D3D11_9X-Direct3D 11 target for Windows RT

In addition, the target shading language is defined when it is GLSL SHADER_TARGET_GLSL ( SHADER_API_GLES It is always true when defined; it #pragma glsl can be true after it is used SHADER_API_OPENGL ).

When compiling for the move platform (ios/android), it is defined when it is SHADER_API_GLES SHADER_API_MOBILE compiled for the desktop (local client).

Platform Differences Help

Because it is not particularly forward-looking, it is not recommended to use macros directly from these platforms. For example, if you are writing a shader that checks for D3D, then perhaps this check should be extended to include d3d11 in the future. Instead, Unity defines some help macros (in HLSLSupport.cginc ) to help with this.

  • UNITY_ATTEN_CHANNEL-which channel of the light attenuation texture contains data and is used for pixel-wise illumination code. Defined as ' R ' or ' a '.

  • UNITY_HALF_TEXEL_OFFSET-defined on a platform that requires a semi-textured element offset adjustment when mapping texture elements to pixels (such as Direct3D 9).

  • UNITY_UV_STARTS_AT_TOP-Always defined with a value of 1 or 0, and a value of 1 on a platform where the texture V coordinate is zero on the top of the texture. Platforms like Direct3D use the value 1; OpenGL-like platforms use a value of 0.

  • UNITY_MIGHT_NOT_HAVE_DEPTH_TEXTURE-defined when a platform can mimic a shadow map or depth texture by manually rendering the depth to the texture.

  • UNITY_PROJ_COORD(a)-Given a 4-component vector, returns a texture coordinate suitable for projection texture reading. On most platforms, it returns the given value directly.

  • UNITY_NEAR_CLIP_VALUE-defined as the value of the near clipping plane, the platform similar to Direct3D uses 0.0, and the OpenGL-like platform uses-1.0.

  • UNITY_COMPILER_CG、UNITY_COMPILER_HLSL 或 UNITY_COMPILER_HLSL2GLSLDetermine which underlying shader compiler to use, and where subtle syntax differences force you to write different shader code.

  • UNITY_CAN_COMPILE_TESSELLATION-defined when the shader compiler "understands" the overlay shader HLSL syntax (currently d3d11 only).

  • UNITY_INITIALIZE_OUTPUT(类型,名称)-initializes the name variable of the given type to zero.

  • UNITY_COMPILER_HLSL、UNITY_COMPILER_HLSL2GLSL、UNITY_COMPILER_CG-Indicates which shader compiler is being used to compile the shader. There are Microsoft HLSL (for DX11, Xbox360, WinRT), HLSL to GLSL converters (for GLSL and desktop OpenGL #pragma ios/android), and NVIDIA Cg (for D3D 9 and non-GLSL desktop OpenGL targets). When you encounter very special situations such as shader syntax handling differences between compilers, and when you want to write different code for each compiler, use.

Shadow Map macro

On different platforms, the declaration and sampling of shadow maps can be very different. So Unity has several macros that can help with this:

    • UNITY_DECLARE_SHADOWMAP(tex)-Declare the shadow map texture variable with the name "Tex".

    • UNITY_SAMPLE_SHADOW(tex,uv)-Samples The shadow map texture "Tex" at the given "UV" coordinates (the XY component is the texture position and the Z component is the depth to compare). Returns a single floating-point value where the shadow term is within the 0..1 range.

    • UNITY_SAMPLE_SHADOW_PROJ(tex,uv)-a shadow map read that is similar to the above but is projected. "UV" is float4, and all other components are divided by. W for searching.

Constant buffer macros

Direct3D 11 sets all shader variables into the constant buffer. Most Unity built-in variables are already assembled, but for your own shader variables, it is better to put them in different constant buffers depending on the desired update frequency.

To do this, use CBUFFER_START(name) and CBUFFER_END macros:

    Cbuffer_start (myrarelyupdatedvariables)    float4 _someglobalvalue;    Cbuffer_end

Surface Shader Channel Flag

When you compile a surface shader, the shader ends up with a lot of code to produce illumination for different channels. When compiling each channel, define one of the following macros:

    • UNITY_PASS_FORWARDBASE-Forward rendering of the underlying channel (main direction light, light map, SH).

    • UNITY_PASS_FORWARDADD-Forward rendering additional channels (one light per channel).

    • UNITY_PASS_PREPASSBASE-Delay light base channel (render normals and highlight index).

    • UNITY_PASS_PREPASSFINAL-Delay Light final channel (using lighting and textures).

    • UNITY_PASS_SHADOWCASTER-Shadow casts the render channel.

    • UNITY_PASS_SHADOWCOLLECTOR-The Shadow "collect" channel of the Directional light shadow.

Unity3d shader pre-defined shader preprocessing macros

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.