HLSL pragma directives reference
Reference for HLSL pragma directives in Unity.
阅读时间6 分钟最后更新于 1 个月前
Standard pragma directives
Unity supports all directives that are part of standard HLSL, as long as these directives are in regular include files. For more information on these directives, see the HLSL documentation: pragma Directive.
#pragmaShader stages
If you are writing a regular graphics shader, use these directives to tell the compiler which functions to use for different shader stages. The and directives are required, but other stages are optional.
#pragma vertex#pragma fragmentStatement | Function |
|---|---|
| Compile the function with the given name as the vertex shader. Replace <name> with the function name. This directive is required in regular graphics shaders. |
| Compile the function with the given name as the fragment shader. Replace <name> with the function name. This directive is required in regular graphics shaders. |
| Compile the function with the given name as the geometry shader. Replace <name> with the function name. This option automatically turns on Note: Metal does not support geometry shaders. |
| Compile the function with the given name as the DirectX 11 hull shader. Replace <name> with the function name. This automatically adds |
| Compile the function with the given name as the DirectX 11 domain shader. Replace <name> with the function name. This option automatically turns on |
Shader variants and keywords
Use these directives to tell the shader compiler how to handle shader variants and keywords. For more information, see Declaring and using shader keywords in HLSL.
Directive | Description |
|---|---|
| Declares a collection of keywords. The compiler includes all of the keywords in the build. You can use suffixes such as For more information and a list of supported suffixes, see Declaring and using shader keywords in HLSL. |
| Declares a collection of keywords. The compiler excludes unused keywords from the build. You can use suffixes such as For more information and a list of supported suffixes, see Declaring and using shader keywords in HLSL. |
| Built-in Render Pipeline only: Add keywords for graphics tiers when compiling for a given graphics API. For more information, see Graphics tiers. |
| Strip specified keywords. |
GPU requirements and shader model support
Use these directives to tell the compiler that your shader requires specific GPU features.
Statement | Function |
|---|---|
| The minimum shader model that this shader program is compatible with. Replace <value> with a valid value. For a list of valid values, see Shader compilation: Targeting shader models and GPU features in HLSL. |
| The minimum GPU features that this shader is compatible with. Replace <value> with a valid value, or multiple valid values separated by a space. For a list of valid values, see Shader compilation: Targeting shader models and GPU features in HLSL. |
Graphics APIs
Use these directives to tell Unity to include or exclude code for a given graphics API.
Statement | Function |
|---|---|
| Compile this shader program only for given graphics APIs. Replace <values> with a space-delimited list of valid values. For more information and a list of valid values, refer to Targeting graphics APIs and platforms in HLSL. For example, |
| Do not compile this shader program for given graphics APIs. Replace <value> with a space-delimited list of valid values. For more information and a list of valid values, refer to Targeting graphics APIs and platforms in HLSL. |
Other pragma directives
Statement | Function |
|---|---|
| Enable precise IEEE 754 rules involving NaN handling. This currently only affects the Metal platform. |
| Force synchronous compilation. This affects the Unity Editor only. For more information, see Asynchronous Shader compilation. |
| Emit |
| Generates shader debug symbols for supported graphics APIs, and disables optimizations for all graphics APIs. Use this for debugging shader code in an external tool. Unity generates debug symbols for Vulkan, DirectX 11 and 12, and supported console platforms. Warning: Using this pragma results in an increased file size, reduced shader performance and can cause non-deterministic compilation in shader binaries and AssetBundles. When you have finished debugging your shaders and you are ready to make a final build of your application, remove this line from your shader source code and recompile the shaders. |
| Embed disassembled HLSLcc bytecode into a translated shader. |
| Enable GPU instancing in this shader, with given options. For more information, see GPU instancing |
| Enables Unity compiling the shader into DXBC using the Microsoft FXC compiler. Overrides the compiler you select in the Shader Compiler Backend Selection section of the build profile settings. To enable DXC compilation, use |
| Put this directive in a file to ensure that the compiler includes the file only once in a shader program. Note: Unity only supports this directive when the Caching Shader Preprocessor is enabled. |
| Provides a hint to platform-specific shader compilers at compile time about which GraphicsFormat may be used for each render target. This is required on platforms that must have specified fragment shader export formats at compile time. Platforms may compile multiple shader variants to support all listed format combinations, allowing the correct variant to be selected at runtime. An example of this format: |
| Forces optimizations off for given graphics APIs. Replace <values> with a space-delimited list of valid values. For a list of valid values, see Targeting graphics APIs and platforms in HLSL |
| Enables Unity compiling the shader into DXIL using the Microsoft DXC compiler. Overrides the compiler you select in the Shader Compiler Backend Selection section of the build profile settings. To disable DXC compilation, use |
Surface Shaders
If you are writing a Surface Shader in the Built-In Render Pipeline, use this directive to tell the compiler which function to use as the surface function, and pass data to that function.
Statement | Function |
|---|---|
| Compile the function with the given name as the surface shader, so that it works with the given lighting model. For more information, see Surface Shaders. |