GLSL

GLSL

OpenGL Shading Language (abbreviated: GLSL or GLslang), is a high-level shading language based on the syntax of 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

Background

With 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 X 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.

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

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. 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, 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, which is available on many different platforms (e.g., GNU/Linux, Mac OS X, 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, 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.[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.

#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.
#version 150
out vec4 MyFragColor;
void main(void)
{
    MyFragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

See also

Other shading languages

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

IDE

Examples


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • GLSL — es el acrónimo de OpenGL Shading Language (Lenguaje de Sombreado de OpenGL), también conocido como GLslang, una tecnología parte del API estandar OpenGL, que permite especificar segmentos de programas gráficos que serán ejecutados sobre el GPU.… …   Wikipedia Español

  • GLSL — Dieser Artikel oder Abschnitt bedarf einer Überarbeitung. Näheres ist auf der Diskussionsseite angegeben. Hilf mit, ihn zu verbessern, und entferne anschließend diese Markierung. Die OpenGL Shading Language (kurz: GLSL oder glSlang) ist eine… …   Deutsch Wikipedia

  • GLSL — L’OpenGL Shading Language (GLSL) est un langage de programmation de shaders. Ces derniers permettent un contrôle avancé du pipeline de la carte graphique. Le GLSL a été développé par l’OpenGL Architecture Review Board afin de faciliter la… …   Wikipédia en Français

  • GLSL — …   Википедия

  • OpenGL Shading Language — Эту статью следует викифицировать. Пожалуйста, оформите её согласно правилам оформления статей …   Википедия

  • OpenGL — Original author(s) Silicon Graphics Developer(s) Khronos Group Stable release 4.2 …   Wikipedia

  • OpenGL — Desarrollador Khronos Group www.opengl.org Información general Diseñador Silicon Graphics …   Wikipedia Español

  • OpenGL Shading Language — Erscheinungsjahr: 2002 Entwickler: Khronos Group Aktuelle Version: 4.10  (8. August 2011) Einflüsse: C …   Deutsch Wikipedia

  • Comparison of Nvidia graphics processing units — For information on AMD (previously ATI) graphics processing units, see Comparison of AMD graphics processing units. This page contains general information about Nvidia s GPUs and videocards based on official Nvidia specifications. Contents 1… …   Wikipedia

  • OpenGL — Тип API Раз …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”