GLSL tips

This page describes basic GLSL types and operators that can be used in your shader to facilitate the creation of your shaders. The full GLSL documentation can be found here.

Basic types

float f; // scalar variable
int i;   // integer variable
vec2 v2; // 2D float vector
vec3 v3; // 3D float vector
vec3 v4; // 4D float vector
mat2 m2; // 2D float (column major) matrix
mat3 m3; // 3D float (column major) matrix
mat4 m4; // 4D float (column major) matrix

Constructors

float f1 = 1.0;            // f1 = 1.0
float f2 = f1 + 1.0;       // f2 = 2.0

int i1 = int(f1);          // i1 = 1
float f3 = float(i1+1);    // f3 = 2.0

vec2 v1 = vec2(2.0,3.0);   // v1 = (2.0,3.0)
vec2 v2 = vec2(f1,f2);     // v2 = (1.0,2.0)
vec2 v3 = v1;              // v3 = (2.0,3.0)
vec2 v4 = vec2(v1.x,v2.y); // v4 = (2.0,2.0)

vec3 v5 = vec3(4.0,5.0,6.0); // v5 = (4.0,5.0,6.0)
vec3 v6 = vec3(v1,f1);       // v6 = (2.0,3.0,1.0)
vec3 v7 = vec3(10.0);        // v7 = (10.0,10.0,10.0)
vec3 v8 = vec3(f1,v1.yx);    // v8 = (1.0,3.0,2.0)
vec3 v9 = v1.xxy;            // v9 = (2.0,2.0,3.0)
vec3 v10 = v1.yyy;           // v10 = (3.0,3.0,3.0)

vec4 v11 = vec4(1.0);        // v11 = (1.0,1.0,1.0,1.0)
vec4 v12 = vec4(v5,f1);      // v12 = (4.0,5.0,6.0,1.0)
vec4 v13 = vec4(v1,v2);      // v13 = (2.0,3.0,1.0,2.0)
vec4 v14 = v12.xyzw;         // v14 = (4.0,5.0,6.0,1.0)
vec4 v15 = v12.wzyx;         // v14 = (1.0,6.0,5.0,4.0)
vec4 v16 = v12.xxww;         // v15 = (4.0,4.0,1.0,1.0)

mat2 m1 = mat2(1.0);             // m1 = 2D identity matrix
mat2 m2 = mat2(1.0,2.0,3.0,4.0); // m2 = (1.0,3.0,
                                          2.0,4.0)
mat2 m3 = mat2(v1,v2);           // m3 = (2.0,1.0,
                                          3.0,2.0)
mat2 m4 = mat2(m3[0],m3[1]);     // m4 = (2.0,1.0,
                                          3.0,2.0)
mat2 m5 = mat2(m3[1],m3[0]);     // m5 = (1.0,2.0,
                                          2.0,3.0)

// same principle for mat3 and mat4 matrices
mat3 m6 = mat3(v5,v6,v7);        // m6 = (4.0,2.0,10.0,
                                          5.0,3.0,10.0,
                                          6.0,1.0,10.0)
mat4 m7 = mat4(v11,v12,v13,v14); // m7 = (1.0,4.0,2.0,4.0,
                                          1.0,5.0,3.0,5.0,
                                          1.0,6.0,1.0,6.0,
                                          1.0,1.0,2.0,1.0)

Basic functions

// geometric functions
float = length(vecX);              // length of the input vector, where X = 2, 3, or 4
vecX = normalize(vecX);            // normalize the input vector
float = distance(vecX p1,vecX p2); // return length(p2-p1)
float = dot(vecX,vecX);            // dot product
vec3 = cross(vec3,vec3);           // cross product

// trigonometric functions
float = sin(float);
float = cos(float);
float = tan(float);
float = asin(float);
float = acos(float);
float = atan(float);

// exponential functions
float = pow(float x,float y); // return x^y
float = exp(float x);         // return e^x
float = exp2(float x);        // return 2^x
float = log(float x);         // return y such as x=e^y
float = log2(float x);        // return y such as x=2^y
float = sqrt(float x);        // return the square root of x

// trigo and exp functions also work with vectors. For instance:
vec2 = pow(vec2 x,vec2 y);
vec3 = sin(vec3);

// reflection of an incident vector i, according to a normal n
vecX r = reflect(vecX i,vecX n);

// refraction of an incident vector i, according to a normal n and an indice of refraction eta
vecX = refract(vecX i,vecX n, float eta);

// common functions (work with any input type)
float = min(float,float);     // min value between the 2 inputs
float = max(float,float);     // max value between the 2 inputs
float = abs(float);           // absolute value
float = sign(float);          // return 1 (positive), -1 (negative) or 0
float = floor(float x);       // nearest integer less than or equal to x
float = fract(float x);       // return x-floor(x)
float = mod(float x,float y); // modulo: return x-y*floor(x/y)

// clamping and mixing variables (work with any input)
float = clamp(float x,float minV,float maxV); // return min(max(x,minV),maxV)
float = mix(float x,float y,float a);         // linear interpolation: (1-a)*x + a*y
float = step(float e,float x);                // return 0 if x<e, return 1 otherwise
float = smoothstep(float e1,float e2,float x);
// return a S-shaped value between 0 and 1. 0 if x<e1. 1 if x>e2. Hermite interpolation otherwise

// matrix operations
matX = matX * matX;     // multiply 2 matrices
vecX = matX * vecX;     // multiply a matrix and a vector
matX = transpose(matX); // compute the transpose of the input matrix
matX = inverse(matX);   // compute the inverse of the input matrix

// texture access
vec2 = textureSize(sampler T,0);  // width and height of the texture T
vec4 = texture(sampler T,vec2 c); // return the color of the texture T at coord c (in [0,1])