OpenGL Shading Language
GLSL or OpenGL Shading Language is a high level shading language based on the C programming language. It was created by the OpenGL ARB for programming graphics processing units directly, thus giving developers control of the graphics pipeline at the lowest level.
Background
With the recent advancements in graphics cards, new features have been added to allow for increased flexibility in the rendering pipeline at the vertex and fragment level. Programmability at this level is achieved with the use of fragment and vertex shaders.
Originally, this functionality was achieved through the use of shaders written in assembly language. Assembly language is non-intuitive and rather complex for developers to use. The OpenGL ARB created the OpenGL Shading Language to provide a more intuitive method for programming the graphics processing unit while maintaining the open standards advantage that has driven OpenGL throughout its history.
Originally introduced as an extension to OpenGL 1.5, the OpenGL ARB formally included GLSL into the OpenGL 2.0 core. OpenGL 2.0 is the first major revision to OpenGL since the creation of OpenGL 1.0 in 1992.
Some benefits of using GLSL are:
- Cross platform compatibility on multiple operating systems, including Windows and Linux.
- The ability to write shaders that can be used on any hardware vendor’s graphics card that supports the OpenGL Shading Language.
- Each hardware vendor includes the GLSL compiler in their driver, thus allowing each vendor to create code optimized for their particular graphics card’s architecture.
Details
Data types
The OpenGL Shading Language Specification defines twenty two basic data types, some are the same as used in the C programming language, while others are specific to graphics processing.
- void – used for functions that do not return a value
- bool – conditional type, values may either by true of false
- int – a signed integer
- float – a floating point number
- vec2 – a 2 component floating point vector
- vec3 – a 3 component floating point vector
- vec4 – a 4 component floating point vector
- bvec2 – a 2 component Boolean vector
- bvec3 – a 3 component Boolean vector
- bvec4 – a 4 component Boolean vector
- ivec2 – a 2 component vector of integers
- ivec3 – a 3 component vector of integers
- ivec4 – a 4 component vector of integers
- mat2 – a 2X2 matrix of floating point numbers
- mat3 – a 3X3 matrix of floating point numbers
- mat4 – a 4X4 matrix of floating point numbers
- sampler1D – a handle for accessing a texture with 1 dimension
- sampler2D – a handle for accessing a texture with 2 dimensions
- sampler3D – a handle for accessing a texture with 3 dimensions
- samplerCube – a handle for accessing cube mapped textures
- sampler1Dshadow – a handle for accessing a depth texture in one dimension
- sampler2Dshadow – a handle for accessing a depth texture in two dimensions
Operators
The OpenGL Shading Language provides many operators, familiar to those with a background in using the C programming language. This gives shader developers flexibility when writing shaders. GLSL contains the Operators in C and C Plus Plus, with the exception of bitwise operators and pointers.
Functions and control structures
Similar to the C programming language, GLSL supports loops and branching, including if, else, if/else, for, do-while, break, continue, etc.
User defined functions are supported, and a wide variety of commonly used built in functions are provided as well. This allows the graphics card manufacturer the ability to optimize these built in functions at the hardware level if they are inclined to do so. Many of these functions are similar to those found in the C programming language such as exp() and abs() while others are specific to graphics programming such as smoothstep() and texture2D().
Compilation and Execution
GLSL Shaders are not stand-alone applications; they require an application that utilizes the OpenGL API. C++, C and Java all support the OpenGL API and have support for the OpenGL Shading Language.
GLSL shaders themselves are simply a set of strings that are passed to the hardware vendor’s driver for compilation from within an application using the OpenGL API’s entry points. Shaders can be created on the fly from within an application or read in as text files, but must be sent to the driver in the form of a string.
A sample trivial GLSL Vertex Shader
void main(void) { gl_Position = ftransform(); }
A sample trivial GLSL Fragment Shader
void main(void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }
References
- Rost, Randi. OpenGL Shading Language. 1st ed. Pearson Education, Inc, 2004.
- Kessenich, John, & Baldwin, David, & Rost, Randi. The OpenGL Shading Language. Version 1.10.59. 3Dlabs, Inc. Ltd.