Version: 2020.3
HLSL in Unity
Shader compilation: targeting shader models and GPU features

Shader compilation: pragma directives

In HLSL, you can use preprocessor directives to tell the compiler how to compile a shader program. Pragma directives are a type of preprocessor directive. This page contains information about pragma directives that you can use in your shader source code.

For general information on preprocessor directives in HLSL, see the Microsoft documentation: Preprocessor directives (HLSL).

Using pragma directives

Put pragma directives inside an HLSL code block, in your shader source file.

By default, Unity does not support pragma directives in include files. If you enable the Caching Preprocessor in your Editor Settings, you can use the #include_with_pragmas preprocessor directive. This directive lets you put pragma directives in include files. This can be especially useful for enabling or disabling shader debug symbols for multiple files.

Supported pragma directives

Shader stages

Use these pragma directives to tell the compiler which parts of your shader code to compile as different shader stages.

An HLSL snippet must contain at least a vertex program and a fragment program, so #pragma vertex and #pragma fragment directives are required.

상태 기능
#pragma vertex name name 함수를 버텍스 셰이더로 컴파일합니다.
#pragma fragment name name 함수를 프래그먼트 셰이더로 컴파일합니다.
#pragma geometry name name 함수를 DX10 지오메트리 셰이더로 컴파일합니다. 이 옵션은 아래 표에 설명된 대로 #pragma target 4.0을 자동으로 활성화합니다.
#pragma hull name name 함수를 DX11 헐 셰이더로 컴파일합니다. 이 옵션은 아래 표에 설명된 대로 #pragma target 5.0을 자동으로 활성화합니다.
#pragma domain name name 함수를 DX11 도메인 셰이더로 컴파일합니다. 이 옵션은 아래 표에 설명된 대로 #pragma target 5.0을 자동으로 활성화합니다.

Shader variants and keywords

Use these pragma directives to tell the shader compiler how to handle shader variants and keywords.

상태 기능
#pragma multi_compile ... Create shader variants for a given keyword. Unused variants of multi_compile shaders are included in the game build. For more information, see Shader variants and keywords.
#pragma multi_compile_local ... This statement is similar to multi_compile, but enumerated keywords are local. For more information, see Shader variants and keywords.
#pragma shader_feature ... Create shader variants for a given keyword. Unused variants of shader_feature shaders are not included in the game build. For more information, see Shader variants and keywords.
#pragma shader_feature_local ... This statement is similar to shader_feature, but enumerated keywords are local. For more information, see Shader variants and keywords.

Shader model and GPU features

Use these pragma directives to tell the compiler that your shader targets a specific shader model, or requires specific GPU features.

상태 기능
#pragma target name Which shader model to compile this shader program for. For more information, see Shader compilation: targeting shader models and GPU features.
#pragma require feature ... Which GPU features the shader program needs. For more information, see Shader compilation: targeting shader models and GPU features.

Graphics APIs

Use these pragma directives to tell the compiler to compile your shader for specific graphics APIs.

상태 기능
#pragma only_renderers space separated names Compile this shader program only for given graphics APIs. For more information, see Shader compilation: targeting graphics APIs.
#pragma exclude_renderers space separated names Do not compile this shader program for given graphics APIs. For more information, see Shader compilation: targeting graphics APIs.

Other pragma directives

상태 기능
#pragma enable_d3d11_debug_symbols Generates shader debug symbols and/or disables optimizations. Use this for debugging shader code in an external tool.

For Vulkan, DirectX 11 and 12, and supported console platforms, Unity generates debug symbols and disables optimizations.

For Metal and OpenGL, you can already debug shaders by default. When you use this pragma directive, Unity disables optimizations.

Warning: Using this directive results in an increased file size and reduced shader performance. 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.
#pragma hardware_tier_variants renderer name Generate multiple shader hardware variants of each shader the system compiles for each hardware tier that can run the selected renderer.
This statement is only supported in the Built-in Render Pipeline.
#pragma hlslcc_bytecode_disassembly 디스어셈블된 HLSLcc 바이트코드를 변환된 셰이더에 포함합니다.
#pragma disable_fastmath 정확한 IEEE 754 규칙을 활성화하며, NaN 처리를 포함합니다. 현재에는 Metal 플랫폼에만 영향을 줍니다.
#pragma glsl_es2 GLSL 셰이더에서 설정하면 셰이더 타겟이 OpenGL ES 3인 경우에도 GLSL ES 1.0(OpenGL ES 2.0)을 생성합니다.
#pragma editor_sync_compilation Force synchronous compilation. This affects the Unity Editor only. For more information, see Asynchronous Shader compilation.
#pragma enable_cbuffer HLSLSupport의 CBUFFER_START(name)CBUFFER_END 매크로를 사용할 때 cbuffer(name)을 방출합니다. 이는 현재 플랫폼이 상수 버퍼를 지원하지 않더라도 마찬가지입니다.

Unused pragma directives

다음 컴파일 지시문은 아무 동작도 수행하지 않으며 안전하게 제거할 수 있습니다.

  • #pragma glsl
  • #pragma glsl_no_auto_normalization
  • #pragma profileoption
  • #pragma fragmentoption
HLSL in Unity
Shader compilation: targeting shader models and GPU features
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961