GLSL
Encyclopedia
OpenGL Shading Language is a high-level shading language
Shading language
A shading language is a special programming language adapted to map on shader programming. Those kind of languages usually have special data types like color and normal...

 based on the syntax of the C programming language
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

. It was created by the OpenGL ARB to give developers more direct control of the graphics pipeline
Graphics pipeline
In 3D computer graphics, the terms graphics pipeline or rendering pipeline most commonly refers to the current state of the art method of rasterization-based rendering as supported by commodity graphics hardware. The graphics pipeline typically accepts some representation of a three-dimensional...

 without having to use assembly language
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...

 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
Vertex (computer graphics)
A vertex in computer graphics is a data structure that describes a point in 2D or 3D space. Display objects are composed of arrays of flat surfaces and vertices define the location and other attributes of the corners of the surfaces.-Application to object models:In computer graphics, objects are...

 and fragment
Fragment (computer graphics)
In computer graphics, a fragment is the data necessary to generate a single pixel's worth of a drawing primitive in the frame buffer.This data may include, but is not limited to:* raster position* depth...

 level. Programmability at this level is achieved with the use of fragment and vertex shader
Shader
In the field of computer graphics, a shader is a computer program that is used primarily to calculate rendering effects on graphics hardware with a high degree of flexibility...

s.

Originally, this functionality was achieved by writing shaders in assembly language
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...

 – 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
Graphics processing unit
A graphics processing unit or GPU is a specialized circuit designed to rapidly manipulate and alter memory in such a way so as to accelerate the building of images in a frame buffer intended for output to a display...

 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
OpenGL
OpenGL is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL...

 1.0 in 1992.

Some benefits of using GLSL are:
  • Cross-platform
    Cross-platform
    In computing, cross-platform, or multi-platform, is an attribute conferred to computer software or computing methods and concepts that are implemented and inter-operate on multiple computer platforms...

     compatibility on multiple operating systems, including GNU
    GNU
    GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

    /Linux
    Linux
    Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

    , Mac OS X
    Mac OS X
    Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

     and Windows
    Microsoft Windows
    Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

    .
  • 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 along side 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:

The most recent version of the GLSL language is 4.20.
GLSL Version OpenGL Version
1.10.59 2.0
1.20.8 2.1
1.30.10 3.0
1.40.08 3.1
1.50.11 3.2
3.30.6 3.3
4.00.9 4.0
4.10.6 4.1
4.20.6 4.2

Operators

The OpenGL Shading Language provides many operators familiar to those with a background in using the C programming language
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

. This gives shader developers flexibility when writing shaders. GLSL contains the operators in C and C++
Operators in C and C++
This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the fourth column "Included in C", dictates whether an operator is also present in C...

, with the exception of pointers. Bitwise operators were added in version 1.30.

Functions and control structures

Similar to the C programming language
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, GLSL supports loops and branching, including: 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
Smoothstep
Smoothstep is a scalar interpolation function commonly used in computer graphics and video game engines. The function interpolates smoothly between two input values based on a third one that should be between the first two. The returned value is clamped between 0 and 1.The slope of the smoothstep...

and texture2D.

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
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

/Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

, Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

, Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

). There are language bindings for C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

, C#, Delphi, Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 and many more.

GLSL shaders themselves are simply a set of strings
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....

 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
On the fly
-Colloquial usage:In colloquial use, on the fly means something created when needed. The phrase is used to mean:# something that was not planned ahead# changes that are made during the execution of same activity: ex tempore, impromptu.-Automotive usage:...

 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, which were included 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.
  1. 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
  1. 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.
  1. version 120

void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

In GLSL 1.30 and later you must do
glBindFragDataLocation(Program, 0, "MyFragColor");
where:
  • Program - your shader program's handle;
  • 0 - count of color buffers, if you are not using multiple render targets, you must write zero;
  • "MyFragColor" - name of your color buffer in shader program.


  1. version 150

out vec4 MyFragColor;
void main(void)
{
MyFragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

See also

  • 3D computer graphics
    3D computer graphics
    3D computer graphics are graphics that use a three-dimensional representation of geometric data that is stored in the computer for the purposes of performing calculations and rendering 2D images...

  • Khronos Group
    Khronos Group
    The Khronos Group is a not-for-profit member-funded industry consortium based in Beaverton, Oregon, focused on the creation of open standard, royalty-free APIs to enable the authoring and accelerated playback of dynamic media on a wide variety of platforms and devices...

  • WebGL
    WebGL
    WebGL is a software library that extends the capability of the JavaScript programming language to allow it to generate interactive 3D graphics within any compatible web browser...

    , an OpenGL-ES dialect for web browsers, which uses GLSL for shaders

Other shading languages

  • ARB (GPU assembly language)
    ARB (GPU assembly language)
    ARB - OpenGL Assembly Language is a low-level shading language. It was created by the OpenGL ARB to standardize GPU instructions controlling the hardware graphics pipeline.-History:...

    , a low-level shading language
  • Cg, a high-level shading language for programming vertex and pixel shaders
  • High Level Shader Language
    High Level Shader Language
    The High Level Shader Language or High Level Shading Language is a proprietary shading language developed by Microsoft for use with the Microsoft Direct3D API. It is analogous to the GLSL shading language used with the OpenGL standard...

    (HLSL)

External links


IDE

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK