Introduction
GLSL, the OpenGL Shading Language, is the Khronos Group’s C-like language for shaders, the small programs that run on the graphics card. It is used by OpenGL, OpenGL ES and WebGL, which compile it in the graphics driver while the program runs, and by Vulkan, which takes it compiled ahead of time to SPIR-V.
There is no single official extension. Files are named .glsl, or after
the shader stage: .vert for vertex shaders, .frag for
fragment shaders, and .geom, .comp, .tesc and
.tese for geometry, compute and tessellation shaders, the names the glslang
compiler recognises.
Structure
A GLSL file is plain text. It starts with a #version line naming the
language version, such as 450 for desktop OpenGL 4.5 or
300 es for OpenGL ES 3.0. Then come declarations of the shader’s inputs,
outputs and uniforms (values set by the program), often with layout
qualifiers giving their locations, then functions. Execution starts at
void main(). The language has built-in vector and matrix types such as
vec4 and mat4.
Each kind of shader has its own built-in variables, such as gl_Position,
which a vertex shader sets, and gl_FragCoord, which a fragment shader
reads. The preprocessor works like C’s, with #define and
#ifdef, and #extension switches on optional language features.
Core GLSL has no #include; it needs an extension such as
GL_GOOGLE_include_directive. Comments are // and
/* */.
#version 450
layout(location = 0) in vec3 colour;
layout(location = 0) out vec4 fragColour;
void main() {
fragColour = vec4(colour, 1.0);
}
Not described
Known, not yet covered
- Reference: this overview is the only page. The language itself has no reference pages: the types and the conversion rules, the storage, layout and precision qualifiers, the built-in variables (
gl_Position,gl_FragCoord) for each shader stage, the built-in functions (texture,mix,dot, atomics and barriers), the preprocessor and#extensionlist, and what changed in each version from 1.10 to 4.60. The OpenGL Shading Language specification describes them. - Reference: the dialects are named but not described: OpenGL ES (ESSL), the Vulkan flavour of GLSL (
GL_KHR_vulkan_glsl, descriptor sets, push constants), WebGL’s subset, and the Shadertoy wrapper. The SPIR-V a compiler produces has its own page. - Viewer: a
//comment that ends in a backslash is not continued onto the next line: in// text \the following line is coloured as code, although GLSL (like C) joins it to the comment. - Viewer: only the keywords, the vector, matrix and sampler type names and a list of built-in functions are coloured. The built-in variables (
gl_Position,gl_FragCoord), layout qualifier names (location,binding,std140), swizzles and thecoreandesin#version 450 coreare plain, a user-declared struct or block name is plain, and a macro in#defineis coloured as a function name. - Viewer: the file is not preprocessed or compiled:
#ifbranches are all coloured,#includeis not followed, and there is no list of errors. Only the first 1 MiB is read and coloured.
Links
Software
- glslang – the Khronos reference compiler and validator (Windows, macOS, Linux)
- Shaderc – Google’s tools, whose
glslccompiles GLSL to SPIR-V (Windows, macOS, Linux) - Vulkan SDK – LunarG’s SDK, which bundles both compilers (Windows, macOS, Linux)
- RenderDoc – graphics debugger that shows and edits the shaders of a running program (Windows, Linux)
- Shadertoy – site for writing and sharing fragment shaders in the browser (web)
Developers
- SPIRV-Cross – converts SPIR-V back to GLSL, HLSL and Metal source (C++)
- tree-sitter-glsl – GLSL grammar for the Tree-sitter parser used by editors
Reference
- OpenGL Registry – the Khronos Group’s registry, with the GLSL specifications
- The OpenGL Shading Language, version 4.60 – the language specification (PDF)