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

Introduction to the shader programming language

3.3.3. Cg / HLSL fragment shader stage

This post is also available in…

Our next and last function in the Pass corresponds to the fragment shader stage that appears in our shader with the name “frag”. The reason we can tell that frag is the function of the fragment shader stage is because it has been declared as such in the #pragma fragment.

#pragma fragment frag
fixed4 frag (v2f i) : SV_Target
    // fragment shader functions here 

The word “fragment” refers to a pixel on the screen; to an individual fragment or to a group that together cover an object area. This means that the fragment shaderstage will process every pixel on the computer screen concerning the object we are viewing. 

We are going to analyze the structure of the frag function. It starts with the data type “fixed4” which means that we will have to return a vector of four dimensions.

On the other hand, let’s remember that this shader is currently written in Cg, so it will only compile in Built-in RP. If we want our shader to compile both in Universal RP and High Definition RP we will have to change this data type for “half4 or float4”, otherwise our program could generate an error.

// Cg language 
fixed4 frag (v2f i) : SV_Target {} ...

// HLSL language 
half4 frag (v2f i) : SV_Target {} ...

After the data type, the name continues with “frag” and as an argument, it has a “v2f” type variable called “i”. 

Unlike the vertex shader stage, this function has an output called “SV_Target”, which allows us to render our scene in an intermediate buffer (render target) instead of sending the data to the Frame Buffer. In previous versions of Direct3D (version 9 and lower), the color output in the fragment shader appeared with the COLOR semantic. However, in modern GPUs (version 10 onwards), this semantics is updated by SV_Target, which means “system value target”. It can apply additional effects to the image before projecting them on the computer screen. 

Inside the fragment shader stage we can find a fixed4 vector type called “col” which is the same as the tex2D function, where, as an argument, it receives the _MainTex texture and UV coordinate input. Basically what this operation does is store a texture within the col vector.

The reason why this vector has four dimensions is due to two conditions: The first because the frag function is a four-dimensional vector, so we will have to return a vector with an equal quantity of dimensions, and the second is because within the col vector we are going to store the colors of the texture in its RGBA channels, therefore, if the texture we use has an alpha channel (transparency), then it will be reflected in the object.

fixed4 frag (v2f i) : SV_Target
    // save the texture inside col 
    fixed4 col = tex2D(_MainTex, i.uv); 
    // return the colour of the texture 
    return col;    

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 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.