Get an 20% OFF using the RELEASE code on your book purchase. For a limited time.

Introduction to the shader programming language

3.2.8. Cg / HLSL Pragmas

This post is also available in…

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.

Follow us to stay informed about all the latest news, updates, and more.

Join the group to share your experiences with other developers.

Subscribe to our channel and keep learning game dev!

jettelly-logo

Jettelly Team

We are a team of indie developers with more than 9 years of experience in video games. As an independent studio, we have developed Nom Noms in which we published with Hyperbeard in 2019. We are currently developing The Unity Shader Bible.

Follow us on our social networks.