GLSL
From Wikipedia, the free encyclopedia
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.
Contents |
[edit] 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 Macintosh, 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.
[edit] Details
[edit] Data types
The OpenGL Shading Language Specification defines 22 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 be either true or 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
[edit] 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 bitwise operators and pointers.
[edit] 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 C programming language such as exp() and abs() while others are specific to graphics programming such as smoothstep() and texture2D().
[edit] Compilation and Execution
GLSL shaders are not stand-alone applications; they require an application that utilizes the OpenGL API. C, C++, C#, Delphi 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.
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. These OpenGL APIs are found in the extensions:
[edit] A sample trivial GLSL Vertex Shader
void main(void) { gl_Position = ftransform(); }
[edit] A sample trivial GLSL Fragment Shader
void main(void) { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }
[edit] 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. http://developer.3dlabs.com/documents/index.htm
[edit] See also
[edit] External links
- GLSL Reference Sheet
- GLSL Language Specification
- OpenGL Fragment Shader Specification
- OpenGL Vertex Shader Specification
- OpenGL Program Specification
- The Official OpenGL Web Site
- GLSL Resources and Documentation
- Tutorials and Examples from Lighthouse3D
- Tutorials and Examples from NeHe Productions
- A GLSL Development Environment
- RenderMonkey Shader Development Environment
- Geist3D graphics engine including GLSL editor