编写表面着色器或常规 着色器程序时,HLSL 源代码可以 编译到不同“着色器模型”中。 为了支持使用更现代的 GPI 功能,必须使用更高的着色器编译目标。
注意:使用更高的着色器编译目标可能会阻止着色器在较旧的 GPU 或平台上运行。
Indicate the compilation target by using the #pragma target
name directive or the more specific #pragma require
feature … directive. For example:
#pragma target 3.5
#pragma require integers 2darray instancing
默认情况下,Unity 将着色器编译为几乎支持的最低目标(“2.5”);处于 DirectX 着色器模型 2.0 和 3.0 之间。其他一些编译指令使着色器自动 编译成更高的目标:
#pragma geometry
) 将编译目标设置为 4.0
。#pragma hull
或 #pragma domain
)将编译目标设置为 4.6
。对于几何体、外壳或域着色器,未通过 #pragma
显式设置函数入口点的任何着色器都将降级内部着色器功能要求。这可使具有更广泛运行时和功能差异的非 DX11 目标与现有着色器内容更加兼容。
例如,Unity 在 Metal 图形上支持曲面细分着色器,但 Metal 不支持几何着色器。使用 #pragma target 5.0
仍有效,只要您不使用几何着色器。
以下是支持的着色器模型列表,其中包含大致增加的功能集(在某些情况下对于平台/GPU 的要求更高):
适用于 Unity 支持的所有平台。DX9 着色器模型 2.0。 有限数量的算术和纹理指令;8 个插值器;没有顶点纹理采样;片元着色器中没有衍生指令;没有显式的 LOD 纹理采样。
es3.0
目标所具有的一切功能。请注意,所有 OpenGL 类平台(包括移动平台)都被视为“支持着色器模型 3.0”。WP8/WinRT 平台(DX11 功能级别 9.x)被视为仅支持着色器模型 2.5。
List of supported feature names for the #pragma require
directive:
interpolators10
: At least 10 vertex-to-fragment interpolators (“varyings”) are available.interpolators15
: At least 15 vertex-to-fragment interpolators (“varyings”) are available.interpolators32
: At least 32 vertex-to-fragment interpolators (“varyings”) are available.mrt4
: Multiple Render Targets, at least 4.mrt8
: Multiple Render Targets, at least 8.derivatives
: Pixel shader derivative instructions (ddx/ddy).samplelod
: Explicit texture LOD sampling (tex2Dlod / SampleLevel).fragcoord
: Pixel location (XY on screen, ZW depth in clip space) input in pixel shader.integers
: Integers are an actual data type, including bit/shift operations.2darray
: 2D texture arrays (Texture2DArray).cubearray
: Cubemap arrays (CubemapArray).instancing
: SV_InstanceID input system value.geometry
: DX10 geometry shaders.compute
: Compute shaders, structured buffers, atomic operations.randomwrite
: “random write” (UAV) textures.tesshw
: GPU support for hardware tessellation, but not necessarily tessellation shader stages (e.g. Metal supports tessellation, but not via shader stages).tessellation
: Tessellation hull/domain shader stages.msaatex
: Ability to access multi-sampled textures (Texture2DMS in HLSL).sparsetex
: Sparse textures with residency info (“Tier2” support in D3D terms; CheckAccessFullyMapped HLSL function). Note that currently this is only implemented on DX11/12.framebufferfetch
: Framebuffer fetch – ability to read input pixel color in the pixel shader.The broad #pragma target
directives are shorthands for the requirements above, and they correspond to:
2.5
: derivatives3.0
: 2.5 + interpolators10 + samplelod + fragcoord3.5
: 3.0 + interpolators15 + mrt4 + integers + 2darray + instancing4.0
: 3.5 + geometry5.0
: 4.0 + compute + randomwrite + tesshw + tessellation4.5
: 3.5 + compute + randomwrite4.6
: 4.0 + cubearray + tesshw + tessellationNote that in Direct3D terms shader model 4.0 also implies “mrt8”; and shader model 5.0 implies “interpolators32” and “cubearray”. However, these are not guaranteed to be available on many mobile platforms. So for backwards compatibility with existing shaders, writing #pragma target 4.0 does not automatically require 8 MRTs support; and writing #pragma target 5.0 does not require 32 interpolators nor cubemap arrays.