The '''OpenGL Shading Language''' (GLSL) is the principle [[shading languages]] for OpenGL. While there are several shading languages available for use in OpenGL, GLSL is the only one that is a part of the OpenGL core.

The '''OpenGL Shading Language''' (GLSL) is the principle [[shading languages]] for OpenGL. While there are several shading languages available for use in OpenGL, GLSL is the only one that is a part of the OpenGL core.

−

GLSL is a C-style language. The language has undergone a number of version changes, and it shares the deprecation model of OpenGL. The current version of GLSL is 1.50.

+

GLSL is a C-style language. The language has undergone a number of version changes, and it shares the deprecation model of OpenGL. The current version of GLSL is 4.30.

== Compilation model ==

== Compilation model ==

Line 28:

Line 26:

GLSL is a lot like C/C++ in many ways. It supports most of the familiar structural components (for-loops, if-statements, etc). But it has some important language differences.

GLSL is a lot like C/C++ in many ways. It supports most of the familiar structural components (for-loops, if-statements, etc). But it has some important language differences.

−

== Variable types ==

+

=== Standard library ===

+

+

The OpenGL Shading Language defines a number of '''standard functions'''. Some standard functions are specific to certain shader stages, while most are available in any stage. There is reference documentation for these functions available [http://www.opengl.org/sdk/docs/manglsl/ here].

+

+

=== Variable types ===

{{main|GLSL Types}}

{{main|GLSL Types}}

Line 34:

Line 36:

=== Type qualifiers ===

=== Type qualifiers ===

−

{{main|GLSL Type Qualifiers}

+

{{main|GLSL Type Qualifiers}}

−

GLSL's uses a large number of qualifiers to specify where the values that various variables store come from.

+

GLSL's uses a large number of qualifiers to specify where the values that various variables contain come from. Qualifiers also modify how those variables can be used.

+

+

=== Interface blocks ===

+

{{main|GLSL Interface Block}}

+

+

Certain variable definitions can be grouped into interface blocks. These can be used to make communication between different shader stages easier, or to allow storage for variables to come from a buffer object.

+

+

=== Predefined variables ===

+

{{main|GLSL Predefined Variables}}

+

+

The different shader stages have a number of predefined variables for them. These are provided by the system for various system-specific use.

== Using GLSL shaders ==

== Using GLSL shaders ==

Line 46:

Line 58:

==== Attributes and draw buffers ====

==== Attributes and draw buffers ====

−

+

For the stages at the start and end of the pipeline (vertex and fragment, respectively), the initial input values and final output values do not come from or go to shader stages. The input values to a vertex shader come from [[Vertex Attribute|vertex data]] [[Vertex Specification|specified]] in a [[Vertex Array Objects|vertex array object]], pulled from [[Vertex Buffer Objects|vertex buffer objects]] during [[Vertex Rendering]]. The output values of a fragment shader are piped to particular buffers for the currently bound framebuffer; either the [[Default Framebuffer|default framebuffer]] or a [[Framebuffer Objects|framebuffer object]].

−

For the stages at the start and end of the pipeline (vertex and fragment, respectively), the initial input values and final output values do not come from or go to shader stages. The input values to a vertex shader come from [[Vertex Attributes|vertex data]] [[Vertex Specification|specified]] in a [[Vertex Array Objects|vertex array object]], pulled from [[Vertex Buffer Objects|vertex buffer objects]]. The output values of a fragment shader are piped to particular buffers for the currently bound framebuffer; either the [[Default Framebuffer|default framebuffer]] or a [[Framebuffer Objects|framebuffer object]].

Because of this, there is a mapping layer for the program's inputs and outputs. The vertex shader's input names are mapped to attribute indices, while the fragment shader's output names are mapped to draw buffer indices. This mapping can be created before the program is linked. If it is not, or if the mapping does not cover all of the inputs and outputs, then the linker will automatically define what indices are mapped to which unmapped input or output names. This auto-generated mapping can be queried by the user after the program is linked.

Because of this, there is a mapping layer for the program's inputs and outputs. The vertex shader's input names are mapped to attribute indices, while the fragment shader's output names are mapped to draw buffer indices. This mapping can be created before the program is linked. If it is not, or if the mapping does not cover all of the inputs and outputs, then the linker will automatically define what indices are mapped to which unmapped input or output names. This auto-generated mapping can be queried by the user after the program is linked.

Line 53:

Line 64:

=== Setting uniforms ===

=== Setting uniforms ===

+

{{main|GLSL Uniform#Uniform management}}

+

Uniforms in GLSL a shader variables that are set from user code, but only are allowed to change between different {{code|glDraw*}} calls. Uniforms can be queried and set by the code external to a particular shader. Uniforms can be arranged into blocks, and the data storage for these blocks can come from buffer objects.

+

==== Setting samplers ====

+

{{main|GLSL Sampler#Binding textures to samplers}}

−

=== Setting samplers ===

+

Samplers are special types which must be defined as uniforms. They represent bound textures in the OpenGL context. They are set like integer, 1D uniform values.

=== Error Checking ===

=== Error Checking ===

Line 63:

Line 78:

Note that the process of loading and compiling shaders hasn't changed much over the different GL versions.

Note that the process of loading and compiling shaders hasn't changed much over the different GL versions.

−

<pre>

+

<source lang="cpp">

−

char *vertexsource;

+

−

char *fragmentsource;

+

char *vertexsource;

−

int isCompiled_VS, isCompiled_FS;

+

char *fragmentsource;

−

int IsLinked;

+

int isCompiled_VS, isCompiled_FS;

−

int maxLength;

+

int IsLinked;

−

char *vertexInfoLog;

+

int maxLength;

−

char *fragmentInfoLog;

+

char *vertexInfoLog;

−

char *shaderProgramInfoLog;

+

char *fragmentInfoLog;

+

char *shaderProgramInfoLog;

−

/* Read our shaders into the appropriate buffers */

+

/* Read our shaders into the appropriate buffers */

−

/* You need to code your own version of ReadTextFile() */

+

/* You need to code your own version of ReadTextFile() */

−

/* Also, the file extension that you give to your vertex and fragment shader doesn't matter.

+

/* Also, the file extension that you give to your vertex and fragment shader doesn't matter.

−

/* Don't forget, OpenGL doesn't care about your files. */

+

/* Don't forget, OpenGL doesn't care about your files. */

−

/* Let's assuming that ReadTextFile allocates a char array and fills it with text */

+

/* Let's assuming that ReadTextFile allocates a char array and fills it with text */

−

vertexsource = ReadTextFile("myfile.vert");

+

vertexsource = ReadTextFile("myfile.vert");

−

fragmentsource = ReadTextFile("myfile.frag");

+

fragmentsource = ReadTextFile("myfile.frag");

−

/* Create an empty vertex shader handle */

+

/* Create an empty vertex shader handle */

−

vertexshader = glCreateShader(GL_VERTEX_SHADER);

+

vertexshader = glCreateShader(GL_VERTEX_SHADER);

−

/* Send the vertex shader source code to GL */

+

/* Send the vertex shader source code to GL */

−

/* Note that the source code is NULL character terminated. */

+

/* Note that the source code is NULL character terminated. */

−

/* GL will automatically detect that therefore the length info can be 0 in this case (the last parameter) */

+

/* GL will automatically detect that therefore the length info can be 0 in this case (the last parameter) */

Revision as of 22:18, 27 September 2012

The OpenGL Shading Language (GLSL) is the principle shading languages for OpenGL. While there are several shading languages available for use in OpenGL, GLSL is the only one that is a part of the OpenGL core.

GLSL is a C-style language. The language has undergone a number of version changes, and it shares the deprecation model of OpenGL. The current version of GLSL is 4.30.

Compilation model

GLSL is quite unique among shading languages due to its compilation model. It's compilation model is more like the standard C paradigm. Compilation is overseen by a number of object types. Note that these do not follow the standard OpenGL Objects paradigm.

Terminology

According to GLSL's standard terminology, a shader is just a compiled set of strings for a particular programmable stage; it does not even need to have the complete code for that stage. A program is a fully linked program that covers multiple programmable stages.

For the sake of clarity, we will adjust this slightly. When the term shader is used, it will be synonymous with the GLSL concept of program. To refer to a GLSL shader, the term shader object will be used.

Language

GLSL is a lot like C/C++ in many ways. It supports most of the familiar structural components (for-loops, if-statements, etc). But it has some important language differences.

Standard library

The OpenGL Shading Language defines a number of standard functions. Some standard functions are specific to certain shader stages, while most are available in any stage. There is reference documentation for these functions available here.

Interface blocks

Certain variable definitions can be grouped into interface blocks. These can be used to make communication between different shader stages easier, or to allow storage for variables to come from a buffer object.

Because of this, there is a mapping layer for the program's inputs and outputs. The vertex shader's input names are mapped to attribute indices, while the fragment shader's output names are mapped to draw buffer indices. This mapping can be created before the program is linked. If it is not, or if the mapping does not cover all of the inputs and outputs, then the linker will automatically define what indices are mapped to which unmapped input or output names. This auto-generated mapping can be queried by the user after the program is linked.

Setting uniforms

Uniforms in GLSL a shader variables that are set from user code, but only are allowed to change between different glDraw*​ calls. Uniforms can be queried and set by the code external to a particular shader. Uniforms can be arranged into blocks, and the data storage for these blocks can come from buffer objects.

Setting samplers

Samplers are special types which must be defined as uniforms. They represent bound textures in the OpenGL context. They are set like integer, 1D uniform values.

Error Checking

This short piece of code shows the process of loading a vertex and fragment shader from 2 text files. Then it compiles them and also checks for errors. The idea here is to encourage newcomers to GLSL to always check for errors. It is in C++ but that doesn't matter.

Note that the process of loading and compiling shaders hasn't changed much over the different GL versions.

char*vertexsource;char*fragmentsource;intisCompiled_VS,isCompiled_FS;intIsLinked;intmaxLength;char*vertexInfoLog;char*fragmentInfoLog;char*shaderProgramInfoLog;/* Read our shaders into the appropriate buffers *//* You need to code your own version of ReadTextFile() *//* Also, the file extension that you give to your vertex and fragment shader doesn't matter./* Don't forget, OpenGL doesn't care about your files. *//* Let's assuming that ReadTextFile allocates a char array and fills it with text */vertexsource=ReadTextFile("myfile.vert");fragmentsource=ReadTextFile("myfile.frag");/* Create an empty vertex shader handle */vertexshader=glCreateShader(GL_VERTEX_SHADER);/* Send the vertex shader source code to GL *//* Note that the source code is NULL character terminated. *//* GL will automatically detect that therefore the length info can be 0 in this case (the last parameter) */glShaderSource(vertexshader,1,(constGLchar**)&vertexsource,0);/* Compile the vertex shader */glCompileShader(vertexshader);glGetShaderiv(vertexshader,GL_COMPILE_STATUS,&IsCompiled_VS);if(IsCompiled_VS==FALSE){glGetShaderiv(vertexshader,GL_INFO_LOG_LENGTH,&maxLength);/* The maxLength includes the NULL character */vertexInfoLog=newchar[maxLength];glGetShaderInfoLog(vertexshader,maxLength,&maxLength,vertexInfoLog);/* Handle the error in an appropriate way such as displaying a message or writing to a log file. *//* In this simple program, we'll just leave */delete[]vertexInfoLog;return;}/* Create an empty fragment shader handle */fragmentshader=glCreateShader(GL_FRAGMENT_SHADER);/* Send the fragment shader source code to GL *//* Note that the source code is NULL character terminated. *//* GL will automatically detect that therefore the length info can be 0 in this case (the last parameter) */glShaderSource(fragmentshader,1,(constGLchar**)&fragmentsource,0);/* Compile the fragment shader */glCompileShader(fragmentshader);glGetShaderiv(fragmentshader,GL_COMPILE_STATUS,&IsCompiled_FS);if(IsCompiled_FS==FALSE){glGetShaderiv(fragmentshader,GL_INFO_LOG_LENGTH,&maxLength);/* The maxLength includes the NULL character */fragmentInfoLog=newchar[maxLength];glGetShaderInfoLog(fragmentshader,maxLength,&maxLength,fragmentInfoLog);/* Handle the error in an appropriate way such as displaying a message or writing to a log file. *//* In this simple program, we'll just leave */delete[]fragmentInfoLog;return;}/* If we reached this point it means the vertex and fragment shaders compiled and are syntax error free. *//* We must link them together to make a GL shader program *//* GL shader programs are monolithic. It is a single piece made of 1 vertex shader and 1 fragment shader. *//* Assign our program handle a "name" */shaderprogram=glCreateProgram();/* Attach our shaders to our program */glAttachShader(shaderprogram,vertexshader);glAttachShader(shaderprogram,fragmentshader);/* Link our program *//* At this stage, the vertex and fragment programs are inspected, optimized and a binary code is generated for the shader. *//* The binary code is uploaded to the GPU, if there is no error. */glLinkProgram(shaderprogram);/* Again, we must check and make sure that it linked. If it fails, it would mean either there is a mismatch between the vertex *//* and fragment shaders. It might be that you have surpassed your GPU's abilities. Perhaps too many ALU operations or *//* too many texel fetch instructions or too many interpolators or dynamic loops. */glGetProgramiv(shaderprogram,GL_LINK_STATUS,(int*)&IsLinked);if(IsLinked==FALSE){/* Noticed that glGetProgramiv is used to get the length for a shader program, not glGetShaderiv. */glGetProgramiv(shaderprogram,GL_INFO_LOG_LENGTH,&maxLength);/* The maxLength includes the NULL character */shaderProgramInfoLog=newchar[maxLength];/* Notice that glGetProgramInfoLog, not glGetShaderInfoLog. */glGetProgramInfoLog(shaderprogram,maxLength,&maxLength,shaderProgramInfoLog);/* Handle the error in an appropriate way such as displaying a message or writing to a log file. *//* In this simple program, we'll just leave */free(shaderProgramInfoLog);return;}/* Deallocating these */delete[]vertexsource;delete[]fragmentsource;/* In your rendering code, you just need to call glUseProgram, call the various glUniform** to update your uniforms *//* and then render. *//* Load the shader into the rendering pipeline */glUseProgram(shaderprogram);/* When the user shuts down your program, you should deallocate all your GL resources. *//* Unbind your shader. */glUseProgram(0);/* Let's detach */glDetachShader(shaderprogram,vertexshader);glDetachShader(shaderprogram,fragmentshader);/* Delete the shaders */glDeleteShader(vertexshader);glDeleteShader(fragmentshader);/* Delete the shader object */glDeleteProgram(shaderprogram);