OpenGL Shading Language
OpenGL Shading Language (abbreviated: GLSL or GLslang), is a high-level shading language based on the syntax of the C programming language. It was created by the OpenGL ARB (OpenGL Architecture Review Board) to give developers more direct control of the graphics pipeline without having to use ARB assembly language or hardware-specific languages.
Background
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 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:
- Cross-platform compatibility on multiple operating systems, including GNU/Linux, Mac OS X and Windows.
- 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:
GLSL Version | OpenGL Version | Date | Shader Preprocessor |
---|---|---|---|
1.10.59 [1] | 2.0 | April 2004 | #version 110 |
1.20.8[2] | 2.1 | September 2006 | #version 120 |
1.30.10[3] | 3.0 | August 2008 | #version 130 |
1.40.08[4] | 3.1 | March 2009 | #version 140 |
1.50.11[5] | 3.2 | August 2009 | #version 150 |
3.30.6[6] | 3.3 | February 2010 | #version 330 |
4.00.9[7] | 4.0 | March 2010 | #version 400 |
4.10.6[8] | 4.1 | July 2010 | #version 410 |
4.20.11[9] | 4.2 | August 2011 | #version 420 |
4.30.8[10] | 4.3 | August 2012 | #version 430 |
4.40[11] | 4.4 | July 2013 | #version 440 |
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++, with the exception of pointers. Bitwise operators were added in version 1.30.
Functions and control structures
Similar to the C programming language, GLSL supports loops and branching, including: if-else, for, do-while, break, continue, etc. Recursion is forbidden, however.
User-defined functions are supported, and a wide variety of commonly used functions are provided built-in 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 math library of the C programming language, such as exp() and abs(), while others are specific to graphics programming, such as smoothstep() and texture().
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 (e.g., GNU/Linux, Mac OS X, Windows). There are language bindings for C, C++, C#, 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 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.
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 and tessellation shaders in OpenGL 4.0. These OpenGL APIs are found in the extensions:
- ARB vertex shader
- ARB fragment shader
- ARB shader objects
- ARB geometry shader 4
- ARB tessellation shader
A sample trivial GLSL vertex shader
This transforms the input vertex the same way the fixed-function pipeline would.
void main(void) { gl_Position = ftransform(); }
Note that ftransform() is no longer available since GLSL 1.40 and GLSL ES 1.0. Instead, the programmer has to manage the projection and modelview matrices explicitly in order to comply with the new OpenGL 3.1 standard.
#version 140 uniform Transformation { mat4 projection_matrix; mat4 modelview_matrix; }; in vec3 vertex; void main(void) { gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 1.0); }
A sample trivial GLSL Tessellation shader
This is a simple pass-through Tessellation Control Shader for the position.
#version 400 layout(vertices=3) out; void main(void) { gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; gl_TessLevelOuter[0] = 1.0; gl_TessLevelOuter[1] = 1.0; gl_TessLevelOuter[2] = 1.0; gl_TessLevelInner[0] = 1.0; gl_TessLevelInner[1] = 1.0; }
This is a simple pass-through Tessellation Evaluation Shader for the position.
#version 400 layout(triangles,equal_spacing) in; void main(void) { vec4 p0 = gl_in[0].gl_Position; vec4 p1 = gl_in[1].gl_Position; vec4 p2 = gl_in[2].gl_Position; vec3 p = gl_TessCoord.xyz; gl_Position = p0*p.x + p1*p.y + p2*p.z; }
A sample trivial GLSL geometry shader
This is a simple pass-through shader for the color and position.
#version 120 #extension GL_EXT_geometry_shader4 : enable void main(void) { for (int i = 0; i < gl_VerticesIn; ++i) { gl_FrontColor = gl_FrontColorIn[i]; gl_Position = gl_PositionIn[i]; EmitVertex(); } }
Since OpenGL 3.2 with GLSL 1.50 geometry shaders were adopted into core functionality which means there is no need to use extensions. However, the syntax is a bit different. This is a simple version 1.50 pass-through shader for vertex positions (of triangle primitives):
#version 150 layout(triangles) in; layout(triangle_strip, max_vertices = 3) out; void main(void) { for (int i = 0; i < gl_in.length(); ++i) { gl_Position = gl_in[i].gl_Position; EmitVertex(); } EndPrimitive(); }
A sample trivial GLSL fragment shader
This produces a red fragment.
#version 120 void main(void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }
In GLSL 1.30 and later you can do
glBindFragDataLocation(Program, 0, "MyFragColor");
where:
- Program - your shader program's handle;
- 0 - color buffer number, associated with the variable; if you are not using multiple render targets, you must write zero;
- "MyFragColor" - name of the output variable in the shader program, which is associated with the given buffer.
#version 150 out vec4 MyFragColor; void main(void) { MyFragColor = vec4(1.0, 0.0, 0.0, 1.0); }
See also
- 3D computer graphics
- Khronos Group
- WebGL, an OpenGL-ES dialect for web browsers, which uses GLSL for shaders
Other shading languages
- ARB assembly language, a low-level shading language
- Cg, a high-level shading language for programming vertex and pixel shaders
References
- Notes
- ↑ "GLSL Language Specification, Version 1.10.59".
- ↑ "GLSL Language Specification, Version 1.20.8".
- ↑ "GLSL Language Specification, Version 1.30.10".
- ↑ "GLSL Language Specification, Version 1.40.08".
- ↑ "GLSL Language Specification, Version 1.50.11".
- ↑ "GLSL Language Specification, Version 3.30.6".
- ↑ "GLSL Language Specification, Version 4.00.9".
- ↑ "GLSL Language Specification, Version 4.10.6".
- ↑ "GLSL Language Specification, Version 4.20.11".
- ↑ "GLSL Language Specification, Version 4.30.8".
- ↑ "GLSL Language Specification, Version 4.40".
Books
- Rost, Randi J.: OpenGL Shading Language (3rd Edition), Addison-Wesley, July 30, 2009, ISBN 978-0-321-63763-5
- Kessenich, John, & Baldwin, David, & Rost, Randi. The OpenGL Shading Language. Version 1.10.59. 3Dlabs, Inc. Ltd.
External links
- OpenGL Fragment Shader Specification
- OpenGL Vertex Shader Specification
- OpenGL Shader Objects Specification
- The official OpenGL website
IDE
- RenderMonkey
- OpenGL Shader Designer
- Mac OpenGL Shader Builder User Guide
- Shader Maker (GPL)
- Polydraw