OpenGL Shading Language
|This article is missing information. Further details can be found on the talk page.|
|Core in version||4.6|
|Core since version||2.0|
|ARB extension||GL_ARB_shader_objects, GL_ARB_vertex_shader, GL_ARB_fragment_shader, GL_ARB_shading_language_100|
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 quite unique among shading languages due to its compilation model. Most shading languages use a simple, one-stage model: you give it a string representing a shader for one of the shader stages, and it gives you a shader object that you can bind to the context and render with.
GLSL uses a more complicated model based on the compilation of C programs. In C, a compilation unit, typically a single source file that may include several external header files, is compiled into an object file. A number of object files are then linked into a single program.
GLSL operates in a similar fashion. A set of source files, given as a series of strings, are compiled into a shader object. This is the analog of an object file. Unlike typical shading languages, there is no requirement that this shader object represent a full shader stage. The only limitation is that it can only compile code that is appropriate for a single, specific shader stage.
Shader objects are useless in that form, just as object files from compiled C programs are useless directly. Shader objects must be linked into a single program object. Unlike most shader languages, the program objects contain all shader stages at once. Only one program can be bound for rendering at any one time, and it is illegal for a program object to omit certain stages entirely.
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.
GLSL is a C-style language, so it covers most of the features you would expect with such a language. Control structures (for-loops, if-else statements, etc) exist in GLSL, including the
switch statement. This section will not cover the entire language in detail; the GLSL specification can handle that. This section will hit the highlights of the important differences between GLSL and C.
C has a number of basic types. What C does not have the concept of is basic vector-types: a basic type that intrinsically store more than one value. The OpenGL Shading Language does define vector types.
The basic non-vector types are:
- bool: conditional type, values may be either true or false
- int: a signed integer
- uint: an unsigned integer
- float: a floating point number
Each of these types, including booleans, can have 2, 3, and 4-component vector equivalents. The n digit below can be 2, 3, or 4:
- bvecn: a vector of booleans
- ivecn: a vector of signed integers
- uvecn: a vector of unsigned integers
- vecn: a vector of floating-point numbers
Vector values can have the same math operators applied to them that scalar values do. These all perform the component-wise operations on each component. However, in order for these operators to work on vectors, the two vectors must have the same number of components.
You can access vectors using the following syntax:
vec4 someVec; someVec.x + someVec.y;
This is called swizzling. You can use x, y, z, or w, referring to the first, second, third, and fourth components, respectively.
The reason it has that name swizzling is because the following syntax is entirely valid:
vec2 someVec; vec4 otherVec = someVec.xyxx; vec3 thirdVec = otherVec.zyy;
You can use any combination of up to 4 of the letters to create a vector (of the same basic type) of that length. So
otherVec.zyy is a vec3, which is how we can initialize a vec3 value with it. Any combination of up to 4 letters is acceptable, so long as the source vector actually has those components. Attempting to access the 'w' component of a vec3 for example is a compile-time error.
Additionally, there are 3 sets of swizzle masks. You can use xyzw, rgba (for colors), or stpq (for texture coordinates). These three sets have no actual difference; they're just syntactic sugar.
In addition to vectors, there are also matrix types. All matrix types are floating-point. Matrix types are as follows, where n and m can be the numbers 2, 3, or 4:
- matnxm: A matrix with n columns and m rows. OpenGL uses column-major matrices, which is standard for mathematics users. Example: mat3x4.
- matn: A symmetric matrix with n columns and rows. This type is equivalent to matnxn.
Swizzling does not work with matrices. You can instead access a matrix's fields with array syntax:
mat3 theMatrix; theMatrix = vec3(3.0, 3.0, 3.0); //Sets the second column to all 3.0s theMatrix = 16.0; //Sets the first entry of the third column to 16.0.
Structs and arrays
All types have constructor syntax that allows you to create values of that type. Constructors use this general syntax:
type(value, value, ...);
The type is the type you wish to create, be it scalar, vector, matrix, struct, or array.
Constructors for scalar types are special. They can
Texture access is not as simple as reading a value from a memory address. Filtering and other processes are applied to textures, and how texture coordinates are interpreted can be part of the texture access operation. For these reason, texture access is somewhat complicated.
It starts with samplers, a special type that GLSL defines. Each sampler represents a texture that is attached to the program. Samplers have a type that defines what kind of texture can be attached to them. The following samplers types are available:
- sampler1DShadow: For doing shadow texture accesses. Depth formats are not required.
- sampler1DArray: For array textures.
- samplerBuffer: For buffer textures
- sampler2DMS: For multisample textures.
These samplers are for textures with floating-point image formats. For textures with integral image formats, you can preceed the sampler type with "i" for signed integers or "u" for unsigned integers. However, there are no integral "shadow" samplers.
The user of shaders is able to attach constant values to the linked program. These kinds of values are called "uniforms". Uniforms are globally defined variables that contain data that is constant through an invocation of a shader. Uniforms can be changed for different uses of the same program however.
To define a uniform, you simply put the keyword, "uniform" in front of a globally-scoped variable definition.
uniform vec3 lightDirection;
Any type of variable, structs and arrays included, can be a uniform. Indeed, sampler types can only be declared as uniforms (or as function parameters).
There is an implementation-defined maximum limit to the number of uniforms available. This limit can be different for different stages, and the limit can depend on the usage of uniform blocks (see below).
Uniforms are shared across stages within a program. This is useful, as it allows you to bind similar information in any stage with a single command. It is a linker error to use the same uniform name in two stages with different types.
Uniform blocks and buffers
It is often the case that a user wants to set multiple uniforms at once. Or that a user wants to have global state that modifies the uniforms of multiple programs at once.
A set of uniforms can be defined in a block; this is a specific named collection of uniforms with a specific format. Uniforms in a block are not set like normal uniform values; the storage for them comes from a buffer object with a specific format.
Stage inputs and outputs
Each stage in the shader pipeline can define a number of input values and a number of output values. There is an implementation-defined maximum number of inputs and outputs for each stage.
In a very few cases, there are also certain built-in outputs or inputs that can be used. These are generated or required by fixed-functionality processes. However, most inputs and outputs are user-defined.
GLSL creates linkage between outputs from one stage and inputs to the next stage very simply. Because the entire program consists of all stages at once, the linkage is made via matching the variable names. It is illegal for the types of the two ; this will cause a linker error, similar to how defining the same non-static function in two different C object files is an error.
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. 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.
Using GLSL shaders
- Cg shading language
- Shading languages (general)