OpenGL Shading Language: Difference between revisions
m (→Matrices) |
JeffKhronos (talk | contribs) |
||
(45 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{shader float}} | ||
The '''OpenGL Shading Language''' (GLSL) is the principal shading language for OpenGL. While, thanks to [[OpenGL Extension]]s, there are several shading languages available for use in OpenGL, GLSL (and [[SPIR-V|SPIR-V]]) are supported directly by OpenGL without extensions. | |||
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 {{current glsl version}}. | |||
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 | |||
== Compilation model == | == Compilation model == | ||
{{main|Shader Compilation}} | |||
GLSL is quite unique among shading languages due to its 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 [[Shader Compilation#Shader and program objects|a number of object types]]. Note that these do not follow the standard [[OpenGL Objects]] paradigm. | ||
Shader | |||
=== Terminology === | === Terminology === | ||
Line 31: | Line 19: | ||
== Language == | == Language == | ||
{{main|Core Language (GLSL)}} | |||
GLSL is a C | 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 [http://www.opengl.org/sdk/docs/manglsl/ here]. | |||
=== Variable types === | |||
{{main|Data Type (GLSL)}} | |||
C has a number of basic types. GLSL uses some of these, but adds many more. | |||
=== Type qualifiers === | |||
{{main|Type Qualifier (GLSL)}} | |||
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|Interface Block (GLSL)}} | |||
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|Built-in Variable (GLSL)}} | |||
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 == | |||
=== Building shaders === | |||
==== 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 | |||
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. | ||
=== Setting uniforms === | |||
{{main|GLSL Uniform#Uniform management}} | |||
Uniforms in GLSL are 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}} | |||
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 piece of code shows the process of loading a vertex and fragment shaders. 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. | |||
{{:Example/GLSL Full Compile Linking}} | |||
== See also == | == See also == | ||
* GLSL | * GLSL | ||
** [[Sampler (GLSL)]] | |||
** [[Uniform (GLSL)]] | |||
** [[Uniform Buffer Object]] | |||
** [[GLSL : common mistakes]] | ** [[GLSL : common mistakes]] | ||
** [[GLSL : nVidia specific features]] | ** [[GLSL : nVidia specific features]] | ||
** [[Hardware_specifics:_NVidia#GLSL_shader_conformance]] | ** [[Hardware_specifics:_NVidia#GLSL_shader_conformance]] | ||
** [[History of OpenGL]] | ** [[History of OpenGL]] | ||
==External links== | ==External links== | ||
Line 196: | Line 92: | ||
*[http://www.opengl.org/documentation/red_book/ OpenGL Red Book] | *[http://www.opengl.org/documentation/red_book/ OpenGL Red Book] | ||
*[http://www.opengl.org/documentation/blue_book/ OpenGL Blue Book] | *[http://www.opengl.org/documentation/blue_book/ OpenGL Blue Book] | ||
*[http://www.ati.com/developer/rendermonkey/ RenderMonkey Shader Development Environment] | *[http://www.ati.com/developer/rendermonkey/ RenderMonkey Shader Development Environment] | ||
[[Category:OpenGL Shading Language]] | [[Category:OpenGL Shading Language]] | ||
[[Category:Shading Languages]] | [[Category:Shading Languages]] |
Latest revision as of 22:31, 1 February 2021
The OpenGL Shading Language (GLSL) is the principal shading language for OpenGL. While, thanks to OpenGL Extensions, there are several shading languages available for use in OpenGL, GLSL (and SPIR-V) are supported directly by OpenGL without extensions.
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.60.
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
Because of GLSL's unique compilation model, GLSL uses unique 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.
Variable types
C has a number of basic types. GLSL uses some of these, but adds many more.
Type qualifiers
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
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
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
Building shaders
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 data specified in a vertex array object, pulled from 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 or a 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.
Setting uniforms
Uniforms in GLSL are 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 piece of code shows the process of loading a vertex and fragment shaders. 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.
Full compile/link of a Vertex and Fragment Shader.
// Read our shaders into the appropriate buffers
std::string vertexSource = // Get source code for vertex shader.
std::string fragmentSource = // Get source code for fragment shader.
// Create an empty vertex shader handle
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
// Send the vertex shader source code to GL
// Note that std::string's .c_str is NULL character terminated.
const GLchar *source = (const GLchar *)vertexSource.c_str();
glShaderSource(vertexShader, 1, &source, 0);
// Compile the vertex shader
glCompileShader(vertexShader);
GLint isCompiled = 0;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &infoLog[0]);
// We don't need the shader anymore.
glDeleteShader(vertexShader);
// Use the infoLog as you see fit.
// In this simple program, we'll just leave
return;
}
// Create an empty fragment shader handle
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
// Send the fragment shader source code to GL
// Note that std::string's .c_str is NULL character terminated.
source = (const GLchar *)fragmentSource.c_str();
glShaderSource(fragmentShader, 1, &source, 0);
// Compile the fragment shader
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, &infoLog[0]);
// We don't need the shader anymore.
glDeleteShader(fragmentShader);
// Either of them. Don't leak shaders.
glDeleteShader(vertexShader);
// Use the infoLog as you see fit.
// In this simple program, we'll just leave
return;
}
// Vertex and fragment shaders are successfully compiled.
// Now time to link them together into a program.
// Get a program object.
GLuint program = glCreateProgram();
// Attach our shaders to our program
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
// Link our program
glLinkProgram(program);
// Note the different functions here: glGetProgram* instead of glGetShader*.
GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
// We don't need the program anymore.
glDeleteProgram(program);
// Don't leak shaders either.
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// Use the infoLog as you see fit.
// In this simple program, we'll just leave
return;
}
// Always detach shaders after a successful link.
glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader);
See also
- GLSL