ドキュメント

Unity Engine


Unity Engine


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
#pragma
directives
to indicate that a shader pass requires certain GPU features. At runtime, Unity compiles the shader only if the platform supports the features.
Use one of the following in the
HLSLPROGRAM
block of a shader:
  • #pragma require
    to specify individual GPU features that a shader requires, for example
    #pragma require integers mrt8
    .
  • #pragma target
    to specify a shader model that a shader requires, for example
    #pragma target 4.0
    . A shader model represents a specific group of GPU features.
For example:
SubShader{ Pass { HLSLPROGRAM // Require multiple GPU features #pragma require integers mrt8 ... ENDHLSL }}
You can add multiple
#pragma
statements. You can also add multiple GPU features or shader models to a single
#pragma
statement, using a space between each.
By default, Unity uses:
  • #pragma require geometry
    if your shader uses
    #pragma geometry
    to define a geometry stage.
  • #pragma require tessellation
    if your shader uses
    #pragma hull
    or
    #pragma domain
    to define a tessellation stage.
Unity displays a warning message when it adds
geometry
or
tessellation
. To avoid this warning, add the requirements explicitly or use an appropriate
#pragma target
value in your code.

Limit 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
    #pragma target
    , add the keywords of the shader variants, separated by spaces. For example
    #pragma target 4.0 EXAMPLE_KEYWORD
    .
  • In
    #pragma require
    , add a colon (
    :
    ), 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
EXAMPLE_KEYWORD
only if the target platform supports the GPU feature or shader model.
If you use two or more keywords in
#pragma target
or
#pragma require
, Unity compiles the shader variants that use any of the keywords.

DirectX12 (DX12), Vulkan and Metal graphics APIs features

注
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:
  • 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
pragma require
directive.
Unity 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
#if
statement to make parts of your shader code conditional on whether the GPU supports the feature.

Keywords

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.

Additional resources