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.
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:
sampler2D
, tex2D()
and similar functions. This syntax works on all platforms.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.
다음과 같이 플랫폼마다 서로 다른 셰이더 컴파일러를 셰이더 프로그램 컴파일에 사용합니다.
사전 정의된 셰이더 매크로를 사용하면 한 컴파일러에서만 지원되는 HLSL 구문을 사용하거나 컴파일러 버그를 피하기 위해 Unity가 사용 중인 컴파일러를 식별할 수 있습니다.