With advances 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 by writing shaders in ARB assembly language – a complex and unintuitive task. 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.4, GLSL was formally included into the OpenGL 2.0 core in 2004 by the OpenGL ARB. It was the first major revision to OpenGL since the creation of OpenGL 1.0 in 1992. Some benefits of using GLSL are:
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.
Versions
GLSL versions have evolved alongside specific versions of the OpenGL API. It is only with OpenGL versions 3.3 and above that the GLSL and OpenGL major and minor version numbers match. These versions for GLSL and OpenGL are related in the following table:
OpenGL ES and WebGL use OpenGL ES Shading Language. The two languages are related but not directly compatible. They can be interconverted through SPIR-Cross.
Similar to the C programming language, GLSL supports loops and branching, for instance: if-else, for, switch, etc. Recursion is forbidden and checked for during compilation. User-defined functions are supported and built-in functions are provided. The graphics card manufacturer may optimize built-in functions at the hardware level. Many of these functions are similar to those in the math library of the C programming language while others are specific to graphics programming. Most of the built-in functions and operators, can operate both on scalars and vectors, for one or both operands. Common built-in functions that are provided and are commonly used for graphics purposes are:,,,,,,,,,, and vector and. Other functions like,,, etc, are provided but they can also all operate on vector quantities, i.e.. GLSL supports function overloading, so there might be multiple function definitions with the same name, having different number of parameters or parameter types. Each of them can have own independent return type.
Preprocessor
GLSL defines a subset of the C preprocessor, combined with its own special directives for specifying versions and OpenGL extensions. The parts removed from CPP are those relating to file names such as and. The extension implements ability to use in source code, allowing easier sharing of code and definitions between many shaders without extra manual pre-processing. Similar extension and exist for using GLSL with Vulkan, and are supported in reference SPIR-V compiler.
Compilation and execution
GLSL shaders are not stand-alone applications; they require an application that utilizes the OpenGL API, which is available on many different platforms. There are language bindings for C, C++, C#, JavaScript, Delphi, Java and many more. 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 from within an application, or read-in as text files, but must be sent to the driver in the form of a string. The set of APIs used to compile, link, and pass parameters to GLSL programs are specified in three OpenGL extensions, and became part of core OpenGL as of OpenGL Version 2.0. The API was expanded with geometry shaders in OpenGL 3.2, tessellation shaders in OpenGL 4.0 and compute shaders in OpenGL 4.3. These OpenGL APIs are found in the extensions:
GLSL shaders can also be used with Vulkan, and are a common way of using shaders in Vulkan. GLSL shaders are precompiled before use, or at runtime, into a binary bytecode format called SPIR-V, usually using offline compiler.
Other shading languages
ARB assembly language, a low-level shading language