You can use #pragma
directives to indicate that a shaderA program that runs on the GPU. More info
See in Glossary requires certain GPU features. At runtime, Unity uses this information to determine whether a shader program is compatible with the current hardware.
You can specify individual GPU features with the #pragma require
directive, or specify a shader model with the #pragma target
directive. A shader model is a shorthand for a group of GPU features; internally, it is the same as a #pragma require
directive with the same list of features.
It is important to correctly describe the GPU features that your shader requires. If your shader uses features that are not included in the list of requirements, this can result in either compile time errors, or in devices failing to support shaders at runtime.
By default, Unity compiles shaders with #pragma require derivatives
, which corresponds to #pragma target 2.5
.
If your shader defines certain shader stages, Unity automatically adds items to the list of requirements.
#pragma geometry
), Unity automatically adds geometry
to the list of requirements.#pragma hull
or #pragma domain
), Unity automatically adds tessellation
to the list of requirements.If the list of requirements (or the equivalent target value) does not already include these values, Unity displays a warning message when it compiles the shader, to indicate that it has added these requirements. To avoid seeing this warning message, explicitly add the requirements or use an appropriate target value in your code.
To specify required features, use the #pragma require
directive, followed by a list of space-delimited values. For example:
#pragma require integers mrt8
You can also use the #pragma require
directive followed by a colon and a list of space-delimited shader keywords. This means that the requirement applies only to variants that are used when any of the given keywords are enabled.
For example:
#pragma require integers mrt8 : EXAMPLE_KEYWORD OTHER_EXAMPLE_KEYWORD
You can use multiple #pragma require
lines. In this example, the shader requires integers
in all cases, and mrt8
if EXAMPLE_KEYWORD is enabled.
#pragma require integers
#pragma require integers mrt8 : EXAMPLE_KEYWORD
To specify a shader model, use #pragma target
directive. For example:
#pragma target 4.0
You can also use the #pragma target
directive followed by a list of space-delimited shader keywords. This means that the requirement applies only to variants that are used when any of the given keywords are enabled.
For example:
#pragma target 4.0 EXAMPLE_KEYWORD OTHER_EXAMPLE_KEYWORD
Note: The syntax for specifying keywords for #pragma require
and #pragma target
is slightly different. When you specify keywords for #pragma require
, you use a colon. When you specify keywords for #pragma target
, you do not use a colon.
Note: If you use the following shader keywords, Unity compiles shaders using the DXC compiler. DXC support in Unity is experimental, not supported on all platforms, and not ready for production use.
If you use the DirectX12 (DX12), Vulkan or Metal graphics APIs, you can use a shader keyword to target the following GPU features:
Use the following syntax:
#pragma multi_compile _ <keyword>
You don’t need to add a pragma require
directive.
Unity then does the following:
You can use an #if
statement to make parts of your shader code conditional on whether the GPU supports the feature.
multi-compile keyword | GPU feature | Keyword for conditional shader code |
---|---|---|
UNITY_DEVICE_SUPPORTS_NATIVE_16BIT |
Supports 16-bit data types. If you use this keyword, the layout of shader buffers might change, because data types such as half and min16float convert to 16-bit. |
UNITY_DEVICE_SUPPORTS_NATIVE_16BIT |
UNITY_DEVICE_SUPPORTS_WAVE_ANY |
Supports wave operations of any size. Use this keyword only if you use wave operations where the size of the waves doesn’t matter. | UNITY_HW_SUPPORTS_WAVE |
UNITY_DEVICE_SUPPORTS_WAVE_8 |
Supports wave operations with a wave size of 8. | UNITY_HW_SUPPORTS_WAVE |
UNITY_DEVICE_SUPPORTS_WAVE_16 |
Supports wave operations with a wave size of 16. | UNITY_HW_SUPPORTS_WAVE |
UNITY_DEVICE_SUPPORTS_WAVE_32 |
Supports wave operations with a wave size of 32. | UNITY_HW_SUPPORTS_WAVE |
UNITY_DEVICE_SUPPORTS_WAVE_64 |
Supports wave operations with a wave size of 64. | UNITY_HW_SUPPORTS_WAVE |
UNITY_DEVICE_SUPPORTS_WAVE_128 |
Supports wave operations with a wave size of 128. | UNITY_HW_SUPPORTS_WAVE |
If you use a keyword that targets a specific wave size, Unity sets a UNITY_HW_WAVE_SIZE
define to the same wave size so you can use it in shader code.
Refer to Declaring and using shader keywords in HLSL for more information.
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.