OpenGL Shading Language

Last updated
Video games outsource rendering calculations to the GPU over OpenGL in real-time. Shaders are written in OpenGL Shading Language and compiled. The compiled programs are executed on the GPU. Linux kernel and OpenGL video games.svg
Video games outsource rendering calculations to the GPU over OpenGL in real-time. Shaders are written in OpenGL Shading Language and compiled. The compiled programs are executed on the GPU.

OpenGL Shading Language (GLSL) is a high-level shading language with a syntax based on the C programming language. It was created by the OpenGL ARB (OpenGL Architecture Review Board) to give developers more direct control of the graphics pipeline without having to use ARB assembly language or hardware-specific languages.

Contents

Background

With advances in graphics cards, new features have been added to allow for increased flexibility in the rendering pipeline at the vertex and fragment level. Programmability at this level is achieved with the use of fragment and vertex shaders.

Originally, this functionality was achieved by writing shaders in ARB assembly language – a complex and unintuitive task. The OpenGL ARB created the OpenGL Shading Language to provide a more intuitive method for programming the graphics processing unit while maintaining the open standards advantage that has driven OpenGL throughout its history.

Originally introduced as an extension to OpenGL 1.4, GLSL was formally included into the OpenGL 2.0 core in 2004 by the OpenGL ARB. It was the first major revision to OpenGL since the creation of OpenGL 1.0 in 1992.

Some benefits of using GLSL are:

Versions

GLSL versions have evolved alongside specific versions of the OpenGL API. It is only with OpenGL versions 3.3 and above that the GLSL and OpenGL major and minor version numbers match. These versions for GLSL and OpenGL are related in the following table:

GLSL VersionOpenGL VersionDateShader Preprocessor
1.10.59 [1] 2.030 April 2004#version 110
1.20.8 [2] 2.107 September 2006#version 120
1.30.10 [3] 3.022 November 2009#version 130
1.40.08 [4] 3.122 November 2009#version 140
1.50.11 [5] 3.204 December 2009#version 150
3.30.6 [6] 3.311 March 2010#version 330
4.00.9 [7] 4.024 July 2010#version 400
4.10.6 [8] 4.124 July 2010#version 410
4.20.11 [9] 4.212 December 2011#version 420
4.30.8 [10] 4.37 February 2013#version 430
4.40.9 [11] 4.416 June 2014#version 440
4.50.7 [12] 4.509 May 2017#version 450
4.60.5 [13] 4.614 June 2018#version 460

OpenGL ES and WebGL use OpenGL ES Shading Language (abbreviated: GLSL ES or ESSL).

GLSL ES versionOpenGL ES versionWebGL versionBased on GLSL versionDateShader Preprocessor
1.00.17 [14] 2.01.01.2012 May 2009#version 100
3.00.6 [15] 3.02.03.3029 January 2016#version 300 es
3.10.5 [16] 3.1GLSL ES 3.0029 January 2016#version 310 es
3.20.6 [17] 3.2GLSL ES 3.1010 July 2019#version 320 es

The two languages are related but not directly compatible. They can be interconverted through SPIRV-Cross. [18]

Language

Operators

GLSL contains the same operators as the operators in C and C++, with the exception of pointers. Bitwise operators were added in version 1.30.

Functions and control structures

Similar to the C programming language, GLSL supports loops and branching, for instance: if-else, for, switch, etc. Recursion is forbidden and checked for during compilation.

User-defined functions are supported and built-in functions are provided. The graphics card manufacturer may optimize built-in functions at the hardware level. Many of these functions are similar to those in the math library of the C programming language while others are specific to graphics programming. Most of the built-in functions and operators, can operate both on scalars and vectors (up to 4 elements), for one or both operands. Common built-in functions that are provided and are commonly used for graphics purposes are: mix, smoothstep, normalize, inversesqrt, clamp, length, distance, dot, cross, reflect, refract and vector min and max. Other functions like abs, sin, pow, etc, are provided but they can also all operate on vector quantities, i.e. pow(vec3(1.5, 2.0, 2.5), abs(vec3(0.1, -0.2, 0.3))). GLSL supports function overloading (for both built-in functions and operators, and user-defined functions), so there might be multiple function definitions with the same name, having different number of parameters or parameter types. Each of them can have own independent return type.

Preprocessor

GLSL defines a subset of the C preprocessor (CPP), combined with its own special directives for specifying versions and OpenGL extensions. The parts removed from CPP are those relating to file names such as #include and __FILE__. [19]

The GL_ARB_shading_language_include extension [20] (implemented for example in Nvidia drivers [21] on Windows and Linux, and all Mesa 20.0.0 [22] drivers on Linux, FreeBSD and Android) implements ability to use #include in source code, allowing easier sharing of code and definitions between many shaders without extra manual pre-processing. Similar extension GL_GOOGLE_include_directive and GL_GOOGLE_cpp_style_line_directive exist for using GLSL with Vulkan, and are supported in reference SPIR-V compiler (glslang aka glslangValidator). [23] [24] [25]

Compilation and execution

GLSL shaders are not stand-alone applications; they require an application that utilizes the OpenGL API, which is available on many different platforms (e.g., Linux, macOS, Windows). There are language bindings for C, C++, C#, JavaScript, Delphi, Java, and many more.

GLSL shaders themselves are simply a set of strings that are passed to the hardware vendor's driver for compilation from within an application using the OpenGL API's entry points. Shaders can be created on the fly from within an application, or read-in as text files, but must be sent to the driver in the form of a string.

The set of APIs used to compile, link, and pass parameters to GLSL programs are specified in three OpenGL extensions, and became part of core OpenGL as of OpenGL Version 2.0. The API was expanded with geometry shaders in OpenGL 3.2, tessellation shaders in OpenGL 4.0 and compute shaders in OpenGL 4.3. These OpenGL APIs are found in the extensions:

GLSL shaders can also be used with Vulkan, and are a common way of using shaders in Vulkan. GLSL shaders are precompiled before use, or at runtime, into a binary bytecode format called SPIR-V, usually using offline compiler.

See also

Other shading languages

Related Research Articles

<span class="mw-page-title-main">OpenGL</span> Cross-platform graphics API

OpenGL is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardware-accelerated rendering.

Direct3D is a graphics application programming interface (API) for Microsoft Windows. Part of DirectX, Direct3D is used to render three-dimensional graphics in applications where performance is important, such as games. Direct3D uses hardware acceleration if it is available on the graphics card, allowing for hardware acceleration of the entire 3D rendering pipeline or even only partial acceleration. Direct3D exposes the advanced graphics capabilities of 3D graphics hardware, including Z-buffering, W-buffering, stencil buffering, spatial anti-aliasing, alpha blending, color blending, mipmapping, texture blending, clipping, culling, atmospheric effects, perspective-correct texture mapping, programmable HLSL shaders and effects. Integration with other DirectX technologies enables Direct3D to deliver such features as video mapping, hardware 3D rendering in 2D overlay planes, and even sprites, providing the use of 2D and 3D graphics in interactive media ties.

<span class="mw-page-title-main">High-Level Shader Language</span> Shading language

The High-Level Shader Language or High-Level Shading Language (HLSL) is a proprietary shading language developed by Microsoft for the Direct3D 9 API to augment the shader assembly language, and went on to become the required shading language for the unified shader model of Direct3D 10 and higher.

Direct3D and OpenGL are both application programming interfaces (APIs) that can be used in applications to render 2D and 3D computer graphics. As of 2005, graphics processing units (GPUs) almost always implement one version of both of these APIs. Examples include: DirectX 9 and OpenGL 2 circa 2004; DirectX 10 and OpenGL 3 circa 2008; and most recently, DirectX 11 and OpenGL 4 circa 2011. GPUs that support more recent versions of the standards are backwards compatible with applications that use the older standards; for example, one can run older DirectX 9 games on a more recent DirectX 11-certified GPU.

The Khronos Group, Inc. is an open, non-profit, member-driven consortium of 170 organizations developing, publishing and maintaining royalty-free interoperability standards for 3D graphics, virtual reality, augmented reality, parallel computation, vision acceleration and machine learning. The open standards and associated conformance tests enable software applications and middleware to effectively harness authoring and accelerated playback of dynamic media across a wide variety of platforms and devices. The group is based in Beaverton, Oregon.

<span class="mw-page-title-main">Shader</span> Type of program in a graphical processing unit (GPU)

In computer graphics, a shader is a computer program that calculates the appropriate levels of light, darkness, and color during the rendering of a 3D scene—a process known as shading. Shaders have evolved to perform a variety of specialized functions in computer graphics special effects and video post-processing, as well as general-purpose computing on graphics processing units.

<span class="mw-page-title-main">OpenGL ES</span> Subset of the OpenGL API for embedded systems

OpenGL for Embedded Systems is a subset of the OpenGL computer graphics rendering application programming interface (API) for rendering 2D and 3D computer graphics such as those used by video games, typically hardware-accelerated using a graphics processing unit (GPU). It is designed for embedded systems like smartphones, tablet computers, video game consoles and PDAs. OpenGL ES is the "most widely deployed 3D graphics API in history".

Mesa, also called Mesa3D and The Mesa 3D Graphics Library, is an open source implementation of OpenGL, Vulkan, and other graphics API specifications. Mesa translates these specifications to vendor-specific graphics hardware drivers.

A shading language is a graphics programming language adapted to programming shader effects. Shading languages usually consist of special data types like "vector", "matrix", "color" and "normal".

Randi J. Rost is a computer graphics professional and frequent contributor to graphics standards. He was an early participant in the personal computer industry, creating a game called King Cribbage for the Apple II computer in 1981 and publishing numerous instructional and review articles in trade publications.

ARB assembly language is a low-level shading language, which can be characterized as an assembly language. It was created by the OpenGL Architecture Review Board (ARB) to standardize GPU instructions controlling the hardware graphics pipeline.

<span class="mw-page-title-main">OpenCL</span> Open standard for programming heterogenous computing systems, such as CPUs or GPUs

OpenCL is a framework for writing programs that execute across heterogeneous platforms consisting of central processing units (CPUs), graphics processing units (GPUs), digital signal processors (DSPs), field-programmable gate arrays (FPGAs) and other processors or hardware accelerators. OpenCL specifies programming languages for programming these devices and application programming interfaces (APIs) to control the platform and execute programs on the compute devices. OpenCL provides a standard interface for parallel computing using task- and data-based parallelism.

<span class="mw-page-title-main">WebGL</span> JavaScript bindings for OpenGL in web browsers

WebGL is a JavaScript API for rendering interactive 2D and 3D graphics within any compatible web browser without the use of plug-ins. WebGL is fully integrated with other web standards, allowing GPU-accelerated usage of physics, image processing, and effects in the HTML canvas. WebGL elements can be mixed with other HTML elements and composited with other parts of the page or page background.

ANGLE is an open source, cross-platform graphics engine abstraction layer developed by Google. ANGLE translates OpenGL ES 2/3 calls to DirectX 9, 11, OpenGL or Vulkan API calls. It's a portable version of OpenGL but with limitations of OpenGL ES standard.

<span class="mw-page-title-main">Metal (API)</span> iOS, macOS, and tvOS graphics rendering API

Metal is a low-level, low-overhead hardware-accelerated 3D graphic and compute shader API created by Apple, debuting in iOS 8. Metal combines functions similar to OpenGL and OpenCL in one API. It is intended to improve performance by offering low-level access to the GPU hardware for apps on iOS, iPadOS, macOS, and tvOS. It can be compared to low-level APIs on other platforms such as Vulkan and DirectX 12.

Vulkan is a low-level low-overhead, cross-platform API and open standard for 3D graphics and computing. It was intended to address the shortcomings of OpenGL, and allow developers more control over the GPU. It is designed to support a wide variety of GPUs, CPUs and operating systems, it is also designed to work with modern multi-core CPUs.

<span class="mw-page-title-main">Standard Portable Intermediate Representation</span>

Standard Portable Intermediate Representation (SPIR) is an intermediate language for parallel computing and graphics by Khronos Group. It is used in multiple execution environments, including the Vulkan graphics API and the OpenCL compute API, to represent a shader or kernel. It is also used as an interchange language for cross compilation.

In computing, a compute kernel is a routine compiled for high throughput accelerators, separate from but used by a main program. They are sometimes called compute shaders, sharing execution units with vertex shaders and pixel shaders on GPUs, but are not limited to execution on one class of device, or graphics APIs.

WebGPU is the working name for a potential web standard and JavaScript API for accelerated graphics and compute, aiming to provide "modern 3D graphics and computation capabilities". It is developed by the W3C GPU for the Web Community Group with engineers from Apple, Mozilla, Microsoft, Google, and others.

<span class="mw-page-title-main">Cg (programming language)</span> Shading language

Cg and High-Level Shader Language (HLSL) are two names given to a high-level shading language developed by Nvidia and Microsoft for programming shaders. Cg/HLSL is based on the C programming language and although they share the same core syntax, some features of C were modified and new data types were added to make Cg/HLSL more suitable for programming graphics processing units.

References

Citations
  1. "GLSL Language Specification, Version 1.10.59" (PDF).
  2. "GLSL Language Specification, Version 1.20.8" (PDF).
  3. "GLSL Language Specification, Version 1.30.10" (PDF).
  4. "GLSL Language Specification, Version 1.40.08" (PDF).
  5. "GLSL Language Specification, Version 1.50.11" (PDF).
  6. "GLSL Language Specification, Version 3.30.6" (PDF).
  7. "GLSL Language Specification, Version 4.00.9" (PDF).
  8. "GLSL Language Specification, Version 4.10.6" (PDF).
  9. "GLSL Language Specification, Version 4.20.11" (PDF).
  10. "GLSL Language Specification, Version 4.30.8" (PDF).
  11. "GLSL Language Specification, Version 4.40.9" (PDF).
  12. "GLSL Language Specification, Version 4.50.7" (PDF).
  13. "GLSL Language Specification, Version 4.60.5" (PDF).
  14. "GLSL ES Language Specification, Version 1.00, revision 17" (PDF).
  15. "GLSL ES Language Specification, Version 3.00, revision 6" (PDF).
  16. "The OpenGL ES® Shading Language, version 3.10, revision 5" (PDF).
  17. "The OpenGL ES® Shading Language, Version 3.20.6" (PDF).
  18. KhronosGroup/SPIRV-Cross, The Khronos Group, 2019-09-06, retrieved 2019-09-08
  19. "Shader Preprocessor". OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 4.3, Eighth Edition.
  20. "ARB_shading_language_include". Khronos.org. Retrieved 2020-05-31.
  21. "NVIDIA driver 265.90 WHQL Quadro". LaptopVideo2Go Forums.
  22. "Mesa 20.0.0 Release Notes / 2020-02-19". www.mesa3d.org. Retrieved 2020-05-31.
  23. "#include directive support by antiagainst · Pull Request #46 · KhronosGroup/glslang". GitHub. Retrieved 2020-05-31.
  24. "Preprocessing line number handling by antiagainst · Pull Request #38 · KhronosGroup/glslang". GitHub.
  25. "Extend the syntax of #line and __FILE__ to support filename strings by antiagainst · Pull Request #43 · KhronosGroup/glslang". GitHub.

Further reading

Books