GLSL

From Wikipedia, the free encyclopedia

Jump to: navigation, search

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 1.40.[1]

Contents

[edit] 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 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] 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 math library of 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. 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();
}

Note that ftransform() is no longer available since GLSL 1.40. 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() {
    gl_Position=projection_matrix*modelview_matrix*vec4(vertex,1.0);
}

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

  1. ^ "GLSL Language Specification, Version 1.40". http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.40.05.pdf. 
Personal tools