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 shaderA small script that contains the mathematical calculations and algorithms for calculating the Color of each pixel rendered, based on the lighting input and the Material configuration. More info
See in Glossary 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.
Different platforms use different shader compilers for shader program compilation as follows:
You can use predefined shader macros to identify which compiler Unity is using, for instance to use HLSL syntax only supported by one compiler, or to work around a compiler bug.
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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.