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:

  • Shaders before OpenGL 3.0.
  • Shaders since OpenGL 3.0.

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:

  • gl_Vertex: vertex position.
  • gl_Normal: vertex normal.
  • gl_Color: vertex color.
  • gl_SecondarayColor: vertex secondary color.
  • gl_MultiTexCoord0 to 7: vertex texture coordinates.
  • gl_FogColor: fog coordinates.

Vertex program attribute outputs:

  • gl_Position: vertex position.
  • gl_PointSize: vertex size for point primitives.
  • gl_ClipVertex: vertex clipping information.

Vertex program uniform state matrices:

  • gl_ModelViewMatrix: model view matrix.
  • gl_ProjectionMatrix: projection matrix.
  • gl_ModelViewProjectionMatrix: model view projection matrix.
  • gl_TextureMatrix[0]: view matrix (see RED::RenderCode::SetViewMatrix).
  • gl_TextureMatrix[1]: model matrix (see RED::RenderCode::SetModelMatrix).

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 HOOPS Luminate, the index of each vertex attribute is given by the RED_VSH_INPUT enumeration

  • location = 0: vertex position.
  • location = 1: user 0.
  • location = 2: vertex normal.
  • location = 3: vertex color.
  • location = 4 to 7: user 1 to 4.
  • location = 8 to 15: vertex texture coordinates.

The vertex shader outputs are quite the same as before:

  • gl_Position: vertex position.
  • gl_PointSize: vertex size for point primitives.
  • gl_ClipDistance[]: vertex clipping information.

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:

  • gl_Position.
  • gl_PointSize.
  • gl_ClipDistance[].

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

  • points: point primitives. 1 vertex per primitive.
  • lines: line primitives. 2 vertices per primitive.
  • triangles: triangle primitives. 3 vertices per primitive.

The output available primitives are:

  • points: point primitives.
  • line_strip: line strip primitive.
  • triangle_strip: triangle strip primitive.

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:

  • gl_FragCoord: windows relative coordinates.
  • gl_FrontFacing: indicates if the fragment belongs to a front-facing primitive.

Fragment shader program outputs:

  • gl_FragColor: output RGBA color.
  • gl_FragDepth: output fragment depth.
  • gl_FragData[]: output data for multiple buffer writing.

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):

  • gl_FragCoord: windows relative coordinates.
  • gl_FrontFacing: indicates if the fragment belongs to a front-facing primitive.

Fragment shader program outputs:

  • gl_FragDepth: output fragment depth.

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.