Shader Variables Data Types

Ahmed Schrute
Shader Coding in Unity from a to z
4 min readDec 6, 2019
CG Programs uses different variable types than the regular C# variables

Why we use different variable types when writing shaders?

Shader code is for every pixel(or vertex), therefor efficient variable types are crucial for running time and memory. e.g.(an iPhone X have around 2.7 million pixels).

Also, when we are writing code in Unity Shader Lab, we are only writing enough code for the shader to compile.

When we are writing code in Unity Shader Lab, we are only writing enough code for the shader to compile.
Click on Show generated code to see the whole code of the shader

Basic Data Types:

float

Size: (32 Bits) Highest Precision

Usage examples: World Positions, Texture coordinates

half

Size: (16 Bits) Half float

Usage examples: short vectors, directions, and dynamic colors

fixed

Size: (11 bits) Lowest float precision

Usage examples: regular colors and simple color operations

int

Size(32 Bits) the regular C# int

Usage examples: counters and array indices

Texture Data Types:

sampler2D

Usage examples: regular 2D textures

Textures are represented by sampler2D(data type)

samplerCUBE

Usage examples: for Cube maps

Cube Maps are represented by samplerCUBE(data type)

sampler2D and samplerCUBE have a half value (for low precision) and float value (for high precision).

Packed Arrays

for all the basic data types we can create a packed array

we can create a packed array for any of basic data type
we can create a packed array for any of basic data type

The syntax of declaring a packed array is as the following:

Name of DataType LengthOfTheArray NameOfTheVariable = (VALUES);

fixed4 color= (0,0,1,1);

In regular coding, Access to values in an array is by using two square brackets that enclose the index> arr[1]=2

In Shader coding, Access to values in an array is by using(r,g,b,a) or (x,y,z,w)

fixed4 color= (0,0,1,1);

color.rgb >> (0,0,1)

Also, you can transfer values between variables:

fixed3 color2= color.rgb;

we are assigning the first three values of color into color2(which the capacity of three)

You can also swizzle values between two variables (Swizzling & Masking)

fixed3 color3= color.gbr

color3.rgb >> (0,1,0) > Green Color

A packed array can be initialized using a single value (Smearing)

fixed4 color4=1;

color4.rgb >>(1,1,1,1)

Packed Matrices

For data structures bigger than arrays there are packed matrices

Packed Matrices
Packed Matrices are used to store transformations, rotations, and scales
Packed Matrices are used to store transformations, rotations, and scales
Packed Matrices are used to store transformations, rotations, and scales, Source: The Amazing King

Packed matrices can be created for any basic type following this syntax:

NameOfDataType NumberOfRows x NumberOfColumns NameOfVariable;

float4x4 matrix;

Accessing values & Assigning values to the matrix follow this syntax:

m ._ RowNumberColumnNumber

Element at first row and first column

matrix._m00=1

Element at 2nd row and third column

matrix._m12=2

We start counting from zero & Row Number is before Column number

Packed Matrices support Chaining (storing matrix data in a packed array)

float4 pos= matrix._m00_m01_m02_m03;

You can store the whole matrix row also in the packed array

float4 pos=matrix[0];

Notes:

rgba structure can represent color using the Additive color model, however, it can be used for accessing values from arrays that represent data other than colors such as positions.

Additive Color Model

In Unity Editor Color Picker the default is RGB (0-255), you can change it to follow the shader RGB (0,1)

Unity Editor color picker (RGB 0,1)
Setting Unity Editor color picker to (RGB 0,1)
RGBA Color Model, the a is for transparency, Source: Irfan Gani Purwanda

Hope you find this tutorial useful, I am currently looking for a Unity Dev job (Remote or in the Bay Area). If you know someone who can might be looking for a Unity Developer, my email is ah1053@stanford.edu

--

--