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

Introduction to the shader programming language

2.0.2. Introduction to the programming language

This post is also available in…

Before starting in the definition of code, we must take into consideration that in Unity there are three programming languages ​​associated with shader development, these are HLSL (High-Level Shader Language – Microsoft), Cg (C for Graphics – NVIDIA) which still compiles into the shader but is no longer used in current versions of the software, and ShaderLab (declarative language – Unity). 

We will start our adventure working with the Cg and ShaderLab languages in Built-in RP and later give way to the introduction of HLSL in Universal RP.

Cg is a high-level programming language designed to compile on most GPUs. It has been developed by NVIDIA in collaboration with Microsoft and uses a syntax very similar to HLSL. The reason why shaders work with the Cg language is that they can compile both HLSL and GLSL (OpenGL Shading Language), accelerating and optimizing the process of creating materials for video games. 

When we create a shader, our code compiles in a field called CGPROGRAM. Unity is currently working on providing further support and compatibility between Cg and HLSL, therefore it is very likely that shortly these blocks will be replaced by HLSLPROGRAM and ENDHLSL since HLSL is the official shader programming language in current versions of Unity (version 2019 onwards). 

All shaders in Unity (except Shader Graph and Compute) are written within a declarative language called ShaderLab. The syntax of this language allows you to display the properties of a shader in the Unity inspector. This is very interesting since we can manipulate the values ​​of variables and vectors in real-time, adjusting our shader to get the desired result.

In ShaderLab we can manually define several properties and commands, among which is the Fallback block, which is compatible with the different types of rendering pipelines that exist. 

Fallback is a fundamental code block in multiplatform games. It allows you to compile a different shader to one that has generated an error. If the shader breaks in its compilation process, Fallback returns a different shader, and so the graphics hardware can continue its work.

Section 2.4.2 looks at this property in detail.

SubShader is another block in ShaderLab which allows us to declare commands and generate passes. When written in Cg a shader can contain more than one SubShader or pass, however, in the case of HLSL, a shader can only contain one SubShader pass.

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.