OpenGL Shading Language: Difference between revisions
mNo edit summary |
m sorted language references alphabetically in source text |
||
Line 126: | Line 126: | ||
[[es:GLSL]] |
[[es:GLSL]] |
||
[[fr:GLSL]] |
[[fr:GLSL]] |
||
⚫ | |||
⚫ | |||
[[it:GLSL]] |
[[it:GLSL]] |
||
⚫ | |||
[[ja:GLSL]] |
[[ja:GLSL]] |
||
⚫ | |||
⚫ | |||
⚫ | |||
[[pl:OpenGL Shading Language]] |
[[pl:OpenGL Shading Language]] |
||
[[ru:OpenGL Shading Language]] |
[[ru:OpenGL Shading Language]] |
Revision as of 19:37, 9 September 2010
GLSL (OpenGL Shading Language), also known as GLslang, is a high level shading language based on the C programming language. It was created by the OpenGL ARB to give developers more direct control of the graphics pipeline without having to use assembly language or hardware-specific languages. Current specification for GLSL is version 4.10.[1]
Background
With the recent 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 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 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.
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, if/else, for, do-while, break, continue, etc.
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 texture2D().
Compilation and Execution
GLSL shaders are not stand-alone applications; they require an application that utilizes the OpenGL API. The OpenGL API implementation is available on many different platforms (e.g., GNU/Linux, Mac OS, 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, except for geometry shaders, this became included in the core OpenGL as of OpenGL Version 3.2. These OpenGL APIs are found in the extensions:
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[2].
#version 140
uniform Transformation {
mat4 projection_matrix;
mat4 modelview_matrix;
};
in vec3 vertex;
void main() {
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 1.0);
}
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() {
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() {
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.
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
See also
References
- Rost, Randi J. OpenGL Shading Language. 1st ed. Pearson Education, Inc, 2004. ISBN 0-321-19789-5
- Kessenich, John, & Baldwin, David, & Rost, Randi. The OpenGL Shading Language. Version 1.10.59. 3Dlabs, Inc. Ltd.
- Notes
External links
- GLSL Language Specification, Version 4.00.8
- GLSL Language Specification, Version 3.30.6
- GLSL Language Specification, Version 1.50
- GLSL Language Specification, Version 1.40
- GLSL Language Specification, Version 1.30
- GLSL Language Specification, Version 1.20
- OpenGL Fragment Shader Specification
- OpenGL Vertex Shader Specification
- OpenGL Shader Objects Specification
- The Official OpenGL Web Site