GLSL
The OpenGL Shading Language (GLSL) is a C-style language used for writing shaders. For example below simple shader that draw vertecies with white color.
#version 300
precision mediump float;
in vec4 vCoord;
out vec4 vColor;
uniform mat4 matrix;
void main() {
gl_Position = matrix * vCoord;
vColor = vec4(1.0, 1.0, 1.0, 1.0);
}
version
You can specify version of GLSL in version directive in first line of shader.
#version 330 core
If version is not supported shader compiler will print error message like version '330' is not supported.
OpenGL Version | GLSL Version | Shader Preprocessor |
---|---|---|
2.0 | 1.10 | #version 110 |
2.1 | 1.20 | #version 120 |
3.0 | 1.30 | #version 130 |
3.1 | 1.40 | #version 140 |
3.2 | 1.50 | #version 150 |
3.3 | 3.30 | #version 330 |
3.3 | 3.30 core | #version 330 core |
4.1 | 4.10 | #version 410 |
For all versions of OpenGL 3.3 and above, the corresponding GLSL version matches the OpenGL version. So GL 4.1 uses GLSL 4.10.
There are also versions for embeded system, for example GLSL 3.00 ES.
#version 300 es
migrating from GLSL 100 to GLSL 300 (es)
1. Specify precision of float. Usually followed after version. Possible values are highp, mediump and lowp.
#version 300 es
precision mediump float;
// ...
2. attribute -> in
// was
// attribute vec4 vPosition;
// attribute vec2 vTexcoord;
// attribute vec3 vNormal;
// now
in vec4 vPosition;
in vec2 vTexcoord;
in vec3 vNormal;
3. varying -> in/out
// was
// varying vec2 vTexcoord;
// varying vec3 vNormal;
// now in vertex shader
out vec2 vTexcoord;
out vec3 vNormal;
// now in fragment shader
in vec2 vTexcoord;
in vec3 vNormal;
4. Most built-in variables like gl_FragColor are removed. You must declare own out/in or uniformvariables. Don't declare variables with prefix gl_.
// was
// gl_FragColor = vec4(1, 0, 0, 1);
// now
out vec4 fragColor;
void main() {
fragColor = vec4(1, 0, 0, 1); // red
}
// no longer exists
// gl_ModelViewMatrix, gl_ProjectionMatrix, gl_Light*,
// gl_Material*, gl_Clip*.
5. textureXX -> texture
// was
// uniform sampler2D u_some2DTexture;
// uniform samplerCube u_someCubeTexture;
// ...
// vec4 color1 = texture2D(u_some2DTexture, ...);
// vec4 color2 = textureCube(u_someCubeTexture, ...);
// now
uniform sampler2D u_some2DTexture;
uniform samplerCube u_someCubeTexture;
...
vec4 color1 = texture(u_some2DTexture, ...);
vec4 color2 = texture(u_someCubeTexture, ...);
qualifiers
GLSL has qualifiers that specify how variables will be used in shader.
qualifier | description |
---|---|
attribute | Specifies input data from previous stage for every vertex. Can be set via the OpenGL API. (deprecated from v3 and removed from v4.2) |
varying | Allows to pass data from the vertex shader to the fragment shader. So such variables are writable in the vertex shader and read-only in the fragment shader. (deprecated from v3 and removed from v4.2) |
uniform | Specifies variables that are not changed at least for the duration of a draw call. They are unknown at the time of compilation and can be set via the OpenGL API. |
in | Specifies input data from previous stage. Available since v1.30. |
out | Specifies data that will be passed to the next stage of the pipeline. Available since v1.30. |
layout | allows to specify some property of variable. For example, layout(location=2) in vec3 inPosition; means that inPosition variable has location 2. Available since v3. |
types
scalar types
- bool - boolen type with true and false values
- int - signed 32-bit integer
- uint - unsigned 32-bit integer
- float - single-precision floating point number
- double - double-precision floating point number
vectors
GLSL provides vector types for each scalar type with size 2,3 and 4:
- bvecn - vector of booleans
- ivecn - vector of signed integers
- uvecn - vector of unsigned integers
- vecn - vector of floats
- dvecn - vector of doubles
You can use x, y, z, or w, referring to the first, second, third, and fourth components.
vec4 someVec;
someVec.x + someVec.y;
You can use swizzling
vec2 someVec;
vec4 otherVec = someVec.xyxx;
vec3 thirdVec = otherVec.zyy;
vec4 someVec1;
// Reverses the order.
someVec1.wzyx = vec4(1.0, 2.0, 3.0, 4.0);
// Sets the 3rd component of someVec to 3.0 and the 1st component to 5.0
someVec1.zx = vec2(3.0, 5.0);
matrices
All matrix types are floating-point, either single-precision or double-precision. Matrix types are as follows, where n and m can be the numbers 2, 3, or 4:
- matnxm - matrix with n columns and m rows.
- matn - matrix with n columns and n rows. Shorthand for matnxn.
OpenGL uses column-major matrices, which is standard for mathematics users.
mat3 theMatrix;
theMatrix[1] = vec3(3.0, 3.0, 3.0); // Sets the second column to all 3.0s
theMatrix[2][0] = 16.0; // Sets the first entry of the third column to 16.0.
theMatrix[1].yzx = vec3(3.0, 1.0, 2.0);
others
There are other types like structures, arrays and GL types to support textures and images.
built-in variables
There are some built-in variables in shaders. For example
- gl_Position in vertex shaders represents output position of the current vertex.
- gl_FragColor in old fragment shaders represents output color of vertex.
You can read more on official page.