Pass information to shader compilers in HLSL
Use the define, pragma, and defineforplatform_compiler directives to pass information to the shader compiler.
読み終わるまでの所要時間 3 分最終更新 11日前
To pass information to the shader compilers in High-Level Shader Language (HLSL), use one of the following directives:
- The standard HLSL directive.
#define - The standard HLSL directive, to pass standard HLSL or Unity-specific directives.
#pragma - The Unity directive.
#define_for_platform_compiler
Add a #define directive
Unity supports the standard HLSL directive. For more information, see # define directive in the Microsoft HLSL documentation. For example:
#defineShader "Custom/ExampleShader"{ SubShader { Pass { HLSLPROGRAM // Define a symbol called EXAMPLE_SYMBOL #define EXAMPLE_SYMBOL // Add your shader code here ENDHLSL } }}
Add a #pragma directive
Use a directive to do the following:
#pragma- Pass Unity-specific directives, for example to add shader keywords or target specific graphics APIs. For more information, refer to HLSL pragma directives reference.
- Pass standard HLSL directives. For more information, refer to # pragma directive in the Microsoft HLSL documentation.
Add a statement anywhere in your , but usually at the start. For example:
#pragmaHLSLPROGRAMShader "Custom/ExampleShader"{ SubShader { Pass { HLSLPROGRAM // Target shader model 3.0 #pragma target 3.0 // Compile this shader only for Direct3D 11 and OpenGL Core #pragma only_renderers d3d11 glcore // Add your shader code here ENDHLSL } }}
Limitations
Unity recognizes directives only in the following contexts:
#pragma- Unity-specific directives: In files, or files you include with the
.shaderdirective.#include_with_pragmas - Standard HLSL directives: In files you include with the directive.
#include
Unity ignores directives outside these contexts.
#pragmaIf you put a directive inside an statement, the condition must depend on one of the following:
#pragma#if- A custom symbol you define in a directive.
#define - The or
SHADER_API_MOBILEplatform macros for branching based on the target platform.SHADER_API_DESKTOP - The or
UNITY_NO_RGBMmacros for branching based on platform features.UNITY_FRAMEBUFFER_FETCH_AVAILABLE - The macro.
UNITY_USE_NATIVE_HDR - The macro.
UNITY_NO_CUBEMAP_ARRAY - The macro for branching based on Unity version.
UNITY_VERSION
Add a #define_for_platform_compiler directive
To pass information to the platform-specific shader compiler, for example the FXC compiler if you target a platform that uses DirectX, add a directive. For example:
#define_for_platform_compilerShader "Custom/ExampleShader"{ SubShader { Pass { HLSLPROGRAM // Define a symbol called EXAMPLE_SYMBOL for the platform-specific shader compiler #define_for_platform_compiler EXAMPLE_SYMBOL // Add your shader code here ENDHLSL } }}
The directive sends a directive to the platform-specific shader compiler. For example, sends a directive to the shader compiler.
#define#define_for_platform_compiler EXAMPLE_SYMBOL#define EXAMPLE_SYMBOLThe Unity preprocessor doesn't use symbols you define with . If you use the symbols inside conditional statements in your own shader code, the code won't run.
#define_for_platform_compiler