Version: 2020.1
GLSL 셰이더 프로그램
셰이더 컴파일 타겟 레벨

Unity에서 사용하는 셰이딩 언어

In Unity, you write shader programs using the HLSL programming language.

Unity originally used the Cg language, hence the name of some of Unity’s shader keywords (CGPROGRAM) and file extensions (.cginc). Unity no longer uses Cg, but these keywords and file extensions are still supported. Note that all shader program code must be valid HLSL, even if it uses the Cg-related keywords and file extensions.

The only difference between using HLSLPROGRAM and CGPROGRAM is in the files that Unity automatically includes when it compiles the shader program. This is for backwards compatibility reasons. For more information, see Built-in shader include files.

HLSL syntax

The HLSL language itself has two syntaxes: a “legacy” DX9-style syntax, and a more modern DX10+ style syntax.

The difference is mostly in how texture sampling functions work:

  • The legacy syntax uses sampler2D, tex2D() and similar functions. This syntax works on all platforms.
  • The DX10+ syntax uses Texture2D, SamplerState and .Sample() functions. Some forms of this syntax do not work on OpenGL platforms, due to how textures and samplers are not different objects in OpenGL.

In Unity, you can avoid problems with HLSL syntax platform support by using predefined macros to declare and sample textures. Unity expands these macros to the most appropriate syntax, depending on the platform that the shader is being compiled for.

셰이더 컴파일러

다음과 같이 플랫폼마다 서로 다른 셰이더 컴파일러를 셰이더 프로그램 컴파일에 사용합니다.

  • Windows and Microsoft platforms (DX11, DX12 and Xbox One) all use Microsoft’s HLSL compiler (currently FXC / D3DCompiler_47).
  • OpenGL (Core & ES), Metal and Vulkan use Microsoft’s HLSL followed by bytecode translation into GLSL, Metal or SPIR-V, using HLSLcc.
  • 기타 콘솔 플랫폼은 해당되는 컴파일러를 사용합니다(예: PS4에서는 PSSL).
  • 표면 셰이더는 코드 생성 분석 단계에 HLSL 및 MojoShader를 사용합니다.

사전 정의된 셰이더 매크로를 사용하면 한 컴파일러에서만 지원되는 HLSL 구문을 사용하거나 컴파일러 버그를 피하기 위해 Unity가 사용 중인 컴파일러를 식별할 수 있습니다.

The Caching Shader Preprocessor (Experimental)

Shader compilation involves several steps. One of the first steps is preprocessing the shader source. By default, Unity uses the platform compiler’s preprocessor to perform this step; however, you can choose to override this and use Unity’s Caching Shader Preprocessor to perform preprocesing. The Caching Shader Preprocessor is up to 25% faster than the default preprocessors used by platform compilers.

The Caching Shader Preprocessor caches intermediate preprocessing data to speed up shader import and compilation time. The Editor reuses this cached data, and only needs to parse include files when their contents change. This makes compiling multiple variants of the same shader more efficient. Enabling the Caching Shader Preprocessor has the most noticeable effect when shaders within a project use a large set of common include files.

Note that the Caching Shader Preprocessor is experimental; it is still in active development. You can provide feedback on this experimental feature in the Unity forum.

You can enable or disable the Caching Shader Preprocessor using the Caching Shader Preprocessor (Experimental) checkbox in the Shader Compilation section of the Editor settings window. You can also enable or disable this feature in a C# script, using the EditorSettings.cachingShaderPreprocessor API.

관련 항목


  • [Caching Shader Preprocessor] added in 2020.1 NewIn20201
GLSL 셰이더 프로그램
셰이더 컴파일 타겟 레벨