In our shader, we can find at least three default pragmas. These are processor directives and are included in Cg or HLSL. Their function is to help our shader recognize and compile certain functions that could not otherwise be recognized as such.
The #pragma vertex vert allows the vertex shader stage called vert to be compiled to the GPU as vertex shader. This is essential since without this line of code the GPU will not be able to recognize the “vert” function as the vertex shader stage, therefore, we will not be able to use information from our objects and neither pass information to the fragment shader stage to be projected onto the screen.
If we look at the pass that is included in the USB_simple_color shader, we will find the following line of code related to the concept previously explained.
// allow me to compile the "vert" function as a vertex shader.
#pragma vertex vert
// use "vert" as vertex shader.
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
The #pragma fragment frag directive serves the same function as the pragma vertex, with the difference that it allows the fragment shader stage called “frag” to compile in the code as a fragment shader.
// allow me to compile the "frag" function as a fragment shader.
#pragma fragment frag
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
UNITY_APPLY_FOG(i.fogCoords, col);
return col;
}
Unlike previous directives, the #pragma multi_compile_fog has a double function. Firstly multi_compile refers to a shader variant that allows us to generate variants with different functionalities within our shader. Secondly, the word “_fog” enables the fog functionalities from the Lighting window in Unity, this means that, if we go to that tab, Environment / Other Setting, we can activate or deactivate our shader’s fog options.