GLSL shading language reference

The following GLSL language quick reference is not intended to replace the full OpenGL shading language specifications (which is waaaay to heavy for the scope of this page). It only lists the useful built-in variables and functions for a quick start in the world of GLSL programming. It is certainly not exhaustive.

OpenGL Shading Language have changed a lot in its 1.3 version (with OpenGL v3.0) with the suppression of its fixed pipeline functionalities. This page tries to handle both references:

Fortunately, shaders can be used in compatibility mode to access some older functionalities.

#version 150 compatibility

Official GLSL shader specifications

For a complete documentation about GLSL, please refer to the official OpenGL specification page:

GLSL vertex program

Before v1.3

Vertex program attribute inputs:

Vertex program attribute outputs:

Vertex program uniform state matrices:

Access to inverse and transpose matrices is possible through the addition of a suffix to the targeted matrix: 'Inverse' to get the inverse matrix; 'Transpose' to get the transposed matrix; 'InverseTranspose' to get the inverse transposed matrix.

Since v1.3

Since GLSL v1.3 the vertex shader inputs are user-defined variables. They must be defined using the 'layout' qualifier.

layout(location = 0) in vec4 vertex;

The location is the index in the attributes table. In REDsdk, the index of each vertex attribute is given by the RED_VSH_INPUT enumeration

The vertex shader outputs are quite the same as before:

In the newer versions of GLSL, built-in uniform matrices no longer exist. They must be transmitted to the shader via custom uniform variables. Defining the program to run in compatibility mode allows nevertheless to access them.

GLSL geometry program

The geometry shader is optional and is part of the specification since OpenGL 3.2 and GLSL 1.5.

As the geometry shader deals with mesh primitives, the input is a vertex data array called gl_in[]. The special variables are intrinsically declared as:

in gl_PerVertex
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];
} gl_in[];

The geometry program outputs the same informations:

Both the input and output primitive types must be declared in the shader using the 'layout' qualifier. These primitives are:

The output available primitives are:

Example of a point primitive input transformed to a triangle strip of 4 vertices output:

layout( points ) in;
layout( triangle_strip, max_vertices = 4 ) out;

Like vertex shader since GLSL v1.3, built-in uniform state matrices no longer exist. They have to be transmitted manually to the program or the shader must be run in compatibility mode.

GLSL pixel / fragment program

Before v1.3

Fragment shader program inputs:

Fragment shader program outputs:

The uniform variables like state matrices are accessible like in the vertex and geometry shaders.

Since v1.3

The fragment shader built-in inputs stay the same (Other variables exist. See the official reference guide):

Fragment shader program outputs:

The other fragment shader outputs are user-defined with the 'layout' qualifier since GLSL v1.3. Generally, the location 0 corresponds to the output RGBA color.

layout(location = 0) out vec4 color;

In the newer versions, like other shader programs, state matrices have to be transmitted manually via custom uniforms.