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

Introduction to the shader programming language

3.2.6 CGPROGRAM / ENDCG

This post is also available in…

All the sections we have reviewed above are written in the ShaderLab declarative language, our real challenge in the graphics programming language starts here with CGPROGRAM or HLSLPROGRAM declaration. 

By default, we can see that Unity has included the word CG in our Program. Regarding this, the official documentation (version 2021.2) says that:

“Unity originally used the Cg language, hence the name of some words and .cginc extensions, but the software no longer uses this language, however, the words are still included and the code is still compiling for compatibility reasons”.

As mentioned above, it is likely that in future versions our shader will appear with HLSLPROGRAM declaration instead of CGPROGRAM due to the fact that HLSL is currently the official graphics programming language (in fact Shader Graph is based on this language). 

Any way we can update our shader simply by replacing the word CGPROGRAM for HLSLPROGRAM and ENDCG for ENDHLSL, and then our program will compile both in Built-in RP as in Universal RP and High Definition RP. 

If we want to update the program to HLSL, we will have to change some settings and add some routes to our shader which will make this more complex to understand.

Pass    // Cg version
{
        CGPROGRAM 
        ENDCG
}

Pass    // HLSL version
{
        HLSLPROGRAM 
        ENDHLSL
}

All the necessary functions for our shader to compile will be written inside the CGPROGRAM and ENDCG field. Those functions outside this fragment will be taken by Unity as ShaderLab properties, and if they do not correspond to this language then our program will not compile. 

Shader “InspectorPath / shaderName”
{
    Properties {} ...
    SubShader
    {
        Tags { “RenderType”=”Opaque”} 
        Pass
        {
            CGPROGRAM 
            // all our functions written here
            ENDCG
        }
    }
}

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.