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 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, 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 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.

[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] Variables

Declaring and using variables in GLSL is similar to using variables in C.

There are four options for variable qualifiers:

  • const - A constant.
  • varying - Read-only in a fragment shader, readable/writable in the vertex shader. Used to communicate between the two shaders.
  • uniform - Global read-only variables available to vertex and fragment shaders which can't be changed inside glBegin() and glEnd() calls.
  • attribute - Global read-only variables only available to the vertex shader (and not the fragment shader). Passed from an OpenGL program, these can be changed on a per vertex level.

[edit] 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. 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. These OpenGL APIs are found in the extensions:

[edit] 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();
}

[edit] 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);
}

[edit] Tools

GLSL shaders need to be created and tested prior to injection into the application that will use them. To do so, several GLSL developer tools exist:

  • RenderMonkey - created by ATI, provides an interface to create, compile and debug GLSL shaders as well as DirectX shaders. It runs only on Microsoft Windows.
  • GLSLEditorSample - a cocoa application running only under Mac OS X. It allows shader creation and compilation, but no debugging is implemented. It is part of the Xcode package, versions 2.3 and above.
  • Quartz Composer - a visual programming environment running under Mac OS X. It allows shader creation, compilation and integration with other Quartz patches in its visual programming model. No debugging is implemented. It is part of the freely distributed Xcode package.
  • Lumina - a new GLSL development tool. It is platform independent and the interface uses Qt.
  • Blender - This GPL 3D modeling and animation package contains GLSL support in its game engine, as of version 2.41.
  • Shader Designer - This extensive, easy to use GLSL IDE has ceased production by TyphoonLabs. However, it can still be downloaded and used for free. Shader designer comes with example shaders and beginner tutorial documentation.
  • Demoniak3D - a tool that allows to quickly code and test your GLSL shaders. Demoniak3D uses a mixture of XML, LUA scripting and GLSL to build a real time 3d scenes.

[edit] See also

[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.

[edit] External links