Set a shader to require GPU features
Use a pragma target or pragma require statement to set the minimum shader model or GPU feature for a shader.
読み終わるまでの所要時間 4 分最終更新 1ヶ月前
You can use directives to indicate that a shader pass requires certain GPU features. At runtime, Unity compiles the shader only if the platform supports the features.
#pragmaUse one of the following in the block of a shader:
HLSLPROGRAM- to specify individual GPU features that a shader requires, for example
#pragma require.#pragma require integers mrt8 - to specify a shader model that a shader requires, for example
#pragma target. A shader model represents a specific group of GPU features.#pragma target 4.0
For the full list of values, refer to HLSL pragma require command reference and HLSL pragma target command reference.
For example:
SubShader{ Pass { HLSLPROGRAM // Require multiple GPU features #pragma require integers mrt8 ... ENDHLSL }}
You can add multiple statements. You can also add multiple GPU features or shader models to a single statement, using a space between each.
#pragma#pragmaBy default, Unity uses:
- if your shader uses
#pragma require geometryto define a geometry stage.#pragma geometry - if your shader uses
#pragma require tessellationor#pragma hullto define a tessellation stage.#pragma domain
Unity displays a warning message when it adds or . To avoid this warning, add the requirements explicitly or use an appropriate value in your code.
geometrytessellation#pragma targetLimit the requirement to specific shader variants
To compile shader variants only when the runtime platform supports a GPU feature or shader model, use the following syntax:
- In , add the keywords of the shader variants, separated by spaces. For example
#pragma target.#pragma target 4.0 EXAMPLE_KEYWORD - In , add a colon (
#pragma require), followed by the keywords of the shader variants, separated by spaces. For example:.#pragma require mrt8 : EXAMPLE_KEYWORD
In these examples, at build time, Unity compiles shader variants that use the only if the target platform supports the GPU feature or shader model.
EXAMPLE_KEYWORDIf you use two or more keywords in or , Unity compiles the shader variants that use any of the keywords.
#pragma target#pragma requireDirectX12 (DX12), Vulkan and Metal graphics APIs features
If you use the DirectX12 (DX12), Vulkan or Metal graphics APIs, you can use a shader keyword to target the following GPU features:
- Support for 16-bit data types.
- Support for wave operations in compute shaders.
Use the following syntax:
#pragma multi_compile _ <keyword>
You don't need to add a directive.
pragma requireUnity then does the following:
- Uses a compatible shader compiler and turns on compiler flags related to the feature.
- Automatically compiles both a shader variant with the keyword on and a shader variant with the keyword off.
- At runtime, automatically checks if the hardware supports the feature and selects the correct variant.
You can use an statement to make parts of your shader code conditional on whether the GPU supports the feature.
#ifKeywords
multi-compile keyword | GPU feature | Keyword for conditional shader code |
|---|---|---|
| Supports 16-bit data types. If you use this keyword, the layout of shader buffers might change, because data types such as | |
| Supports wave operations of any size. Use this keyword only if you use wave operations where the size of the waves doesn't matter. | |
| Supports wave operations with a wave size of 8. | |
| Supports wave operations with a wave size of 16. | |
| Supports wave operations with a wave size of 32. | |
| Supports wave operations with a wave size of 64. | |
| Supports wave operations with a wave size of 128. | |
If you use a keyword that targets a specific wave size, Unity sets a define to the same wave size so you can use it in shader code.
UNITY_HW_WAVE_SIZERefer to Declaring and using shader keywords in HLSL for more information.