编写表面着色器或常规 着色器程序时,HLSL 源代码可以 编译到不同“着色器模型”中。 为了支持使用更现代的 GPI 功能,必须使用更高的着色器编译目标。
注意:使用更高的着色器编译目标可能会阻止着色器在较旧的 GPU 或平台上运行。
应使用 #pragma target
名称 指令或更具体的 #pragma require
功能 … 指令来指示编译目标。例如:
# 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。
#pragma require
指令支持的功能名称列表:
interpolators10
:至少有 10 个顶点到片元插值器(“变化”)可用。interpolators15
:至少有 15 个顶点到片元插值器(“变化”)可用。interpolators32
:至少有 32 个顶点到片元插值器(“变化”)可用。mrt4
:多个渲染目标,至少 4 个。mrt8
:多个渲染目标,至少 8 个。derivatives
:像素着色器衍生指令 (ddx/ddy)。samplelod
:显式纹理 LOD 采样 (tex2Dlod / SampleLevel)。fragcoord
:像素着色器中的像素位置(屏幕上的 XY,裁剪空间中的 ZW 深度)输入。integers
:整数是一种实际的数据类型,包括位/移位操作。2darray
:2D 纹理数组 (Texture2DArray)。cubearray
:立方体贴图数组 (CubemapArray)。instancing
:SV_InstanceID 输入系统值。geometry
:DX10 几何着色器。compute
:计算着色器、结构化缓冲区、原子操作。randomwrite
:“随机写入”(UAV) 纹理。tesshw
:GPU 支持硬件曲面细分,但不一定是曲面细分着色器阶段(例如,Metal 支持曲面细分,但不是通过着色器阶段)。tessellation
:曲面细分外壳/域着色器阶段。msaatex
:能够访问多重采样的纹理(HLSL 中的 Texture2DMS)。sparsetex
:包含驻留信息的稀疏纹理(D3D 术语中的“Tier2”支持;CheckAccessFullyMapped HLSL 函数)。请注意,目前仅在 DX11/12 上实现。framebufferfetch
:帧缓冲提取 - 能够在像素着色器中读取输入像素颜色。广泛的 #pragma target
指令是上述要求的缩写,它们对应于:
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 + tessellation请注意,在 Direct3D 术语中,Shader Model 4.0 也表示“mrt8”;而 Shader Model 5.0 表示“interpolators32”和“cubearray”。但是,这些并不能保证在大量移动平台上都可用。因此,为了向后兼容现有着色器,编写 #pragma target 4.0 不会自动要求 8 个 MRT 支持;而编写 #pragma target 5.0 不需要 32 个插值器和立方体贴图数组。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.