Predefined Shader preprocessor macros 预定义的着色器预处理宏
Unity defines several preprocessor macros when compiling Shader programs.
在编译着色程序时,Unity定义了几个预处理器宏。
Target platform
- 目标平台
Macro:
| Target platform:
|
---|---|
SHADER_API_D3D9 | Direct3D 9 |
SHADER_API_D3D11 | Direct3D 11 |
SHADER_API_GLCORE | Desktop OpenGL “core” (GL 3/4) |
SHADER_API_GLES | OpenGL ES 2.0 |
SHADER_API_GLES3 | OpenGL ES 3.0/3.1 |
SHADER_API_METAL | iOS/Mac Metal |
SHADER_API_VULKAN | Vulkan |
SHADER_API_D3D11_9X | Direct3D 11 “feature level 9.x” target for Windows Store and Windows Phone |
SHADER_API_PS4 | PlayStation 4. SHADER_API_PSSL is also defined. |
SHADER_API_XBOXONE | Xbox One |
SHADER_API_PSP2 | PlayStation Vita |
SHADER_API_WIIU | Nintendo Wii U |
SHADER_API_MOBILE
is defined for all general mobile platforms (GLES, GLES3, METAL, PSP2).
为所有通用移动平台(GLES、GLES3、金属、PSP2)定义了shaderapimobile。
Additionally, SHADER_TARGET_GLSL
is defined when the target shading language is GLSL (always true for OpenGL/GLES platforms).
另外,当目标着色语言是GLSL时(对于面向对象的/gles平台来说总是正确的),定义着色目标tglsl。
Shader target model 着色器目标模型
SHADER_TARGET
is defined to a numeric value that matches the Shader target compilation model (that is, 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:
shadertarget被定义为与着色器目标编译模型匹配的数值(也就是匹配目标指令指令)。例如,当编译成Shader模型3.0时,shadertarget是30。您可以在着色器代码中使用它来进行条件检查。例如:
#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
Unity version
- 版本
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 only passes on versions 5.0.0 or later.
unityversion包含Unity版本的数值。例如,unityversion是501,用于统一5.0.1。如果您需要编写使用不同内置着色器功能的着色器,那么可以将其用于版本比较。例如,如果unityversion=500预处理器检查只通过版本5.0.0或更高版本。
Shader stage being compiled 着色器阶段被编译
Preprocessor
- 预处理器
SHADER_STAGE_VERTEX
,
SHADER_STAGE_FRAGMENT
,
SHADER_STAGE_DOMAIN
,
SHADER_STAGE_HULL
,
SHADER_STAGE_GEOMETRY
,
SHADER_STAGE_COMPUTE
are defined when compiling each Shader stage. Typically they are useful when sharing Shader code between pixel Shaders and compute Shaders, to handle cases where some things have to be done slightly differently.
- 宏指令
SHADER_STAGE_FRAGMENT
,
SHADER_STAGE_DOMAIN
,
SHADER_STAGE_HULL
,
SHADER_STAGE_GEOMETRY
,
SHADER_STAGE_COMPUTE
在编译每个着色器阶段时都被罚款。通常,当在像素着色器和计算着色器之间共享着色的代码时,它们是很有用的,以处理一些需要稍微改变一些事情的情况。
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 (inHLSLSupport.cginc
):
直接使用这些平台宏是不鼓励的,因为它们并不总是对代码的未来有所帮助。例如,如果您正在编写一个用于检查D3D9的着色器,那么您可能希望确保在将来,该检查被扩展为包含D3D11。相反,Unity定义了几个助手宏(inHLSLSupport.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 在条件语句之前加上这个,告诉编译器,这个应该被编译成一个实际的分支。在HLSL平台上扩展到分支。. |
UNITY_FLATTEN | Add this before conditional statements to tell the compiler that this should be flattened to avoid an actual branch instruction. Expands to 在条件语句之前添加这个语句,告诉编译器应该将其夷为平,以避免实际的分支指令。在HLSL平台上扩展到平坦。. |
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) 在没有使用lightmap的RGBM压缩的平台上定义(移动平台)。. |
UNITY_NO_DXT5nm | Defined on platforms that do not use DXT5nm normal-map compression (mobile platforms) 在不使用DXT5nm标准映射压缩(移动平台)的平台上定义。. |
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) 在“framebuffer颜色取回”功能的平台上定义(通常是iOS平台——OpenGL ES 2.0、3.0和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) 在点光阴影映射使用RGBA纹理和编码深度(其他平台使用单通道浮点纹理)的平台上定义。. |
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’ 定义光衰减纹理的哪个通道包含数据;在每个像素的照明代码中使用。定义为“r”或“a”。. |
UNITY_HALF_TEXEL_OFFSET | Defined on platforms that need a half-texel offset adjustment in mapping texels to pixels (e.g. Direct3D 9) 在平台上定义,需要在将纹理映射到像素(例如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 the “top” of the Texture. Direct3D-like platforms use value of 1; OpenGL-like platforms use value of 0 总是定义为1或0的值。1的值在材质V坐标为0的平台上,在纹理的“顶部”。类似于direct3d的平台使用价值1;类似于类似于平台的平台使用价值为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 给定一个4个分量的向量,它返回一个纹理坐标,适合于投影的纹理读取。在大多数平台上,它直接返回给定的值。. |
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 定义为接近剪切面的值。类似于direct3d的平台使用0.0,而类似于类似于平台的平台使用-1.0。. |
UNITY_VPOS_TYPE | Defines the data type required for pixel position input (VPOS): 定义像素位置输入所需的数据类型(VPOS):在D3D9上的浮动,在其他地方的浮动。. |
UNITY_CAN_COMPILE_TESSELLATION | Defined when the Shader compiler “understands” the tessellation Shader HLSL syntax (currently only D3D11) 定义当着色器编译器“理解”了镶嵌着色的材质(目前只有D3D11)。.
|
UNITY_INITIALIZE_OUTPUT(type,name) | Initializes the variable name of given type to zero 初始化给定类型的变量名为0。. |
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 documentation on Shading Languages for more details. Use this if you run into very specific Shader syntax handling differences between the compilers, and want to write different code for each compiler 表明哪个着色编译器被用来编译着色器——分别是:微软的HLSL,HLSL到GLSL转换器,以及NVIDIA的Cg。请参阅有关着色语言的文档,了解更多细节。如果您在编译器中遇到非常特殊的着色处理差异,并且希望为每个编译器编写不同的代码,那么就使用这个方法。. |
-
UNITY_REVERSED_Z
- defined on plaftorms using reverse Z buffer. Stored Z values are in the range 1..0 instead of 0..1. - UNITY_REVERSED_Z在plaftorms上使用反向Z缓冲区定义。存储的Z值在范围1中。0 0 . . 1。
Shadow mapping macros 阴影映射宏
Declaring and sampling shadow maps can be very different depending on the platform. Unity has several macros to help with this:
根据平台的不同,声明和采样影子映射可能会非常不同。Unity有几个宏来帮助这个:
Macro:
| Use:使用 |
---|---|
UNITY_DECLARE_SHADOWMAP(tex) | Declares a shadowmap Texture variable with name “tex” 用名称“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 在给定的“uv”坐标下,阴影贴图纹理“tex”(XY组件是纹理位置,Z组件是深度来比较)。在0中返回一个浮动值。1范围。. |
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 类似于上面,但投射阴影图是读的。“uv”是一个漂浮的,所有其他的部件都被分开。做查找的w。. |
NOTE: Not all graphics cards support shadowmaps. Use SystemInfo.SupportsRenderTextureFormat to check for support.
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.
所有群组11 Direct3D Shader变量纳入“constant缓冲池”。协调统一的built-in变量,但已经是grouped for your own变量在Shaders it more to be可能optimal put them on可供缓冲constant单独考虑预期的《frequency更新。
Use CBUFFER_START(name)
and CBUFFER_END
macros for that:
使用cbufferstart(name)和cbufferend宏:
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 GameObjects, 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 (Samplers define Texture filtering and wrapping modes).
通常你会在着色器代码中使用texture2D来声明纹理和采样对。但是在某些平台上(比如DX11),纹理和采样器是独立的游戏对象,并且最大可能的采样计数是非常有限的。Unity有一些宏可以在没有采样器的情况下声明纹理,并使用来自另一个纹理的采样器来采样纹理。如果你最终进入了采样限制,你就知道你的一些纹理实际上可以共享一个采样器(Samplers定义了纹理过滤和包装模式)。
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 a Sampler from another Texture (samplername) 从纹理(名称)中取样,使用来自另一个纹理的采样器(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 从纹理数组中获得一个浮动的UV;坐标的z分量是数组元素索引。. |
UNITY_SAMPLE_TEX2DARRAY_LOD(name,uv,lod) | Sample from a Texture array with an explicit mipmap level 从纹理数组中获得一个显式的mipmap级别。. |
Surface Shader pass indicators 表面材质通过指标
When Surface Shaders are compiled, they generate 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). |