OpenGL Shading Language: Difference between revisions

Content deleted Content added
added Infobox and unofficial logo
 
(33 intermediate revisions by 26 users not shown)
Line 1:
{{Short description|High-level shading language}}
{{distinguish|Open Shading Language}}
 
{{Infobox software
[[File:Linux kernel and OpenGL video games.svg|thumb|300px|[[Video games]] outsource rendering calculations to the [[Graphics processing unit|GPU]] over [[OpenGL]] in real-time. Shaders are written in '''OpenGL Shading Language''' and compiled. The compiled programs are executed on the GPU.]]
| name = GLSL
| title = GLSL
| logo = GLSL_Logo_(Unofficial).svg
| logo size = x64px
| logo caption = ''Unofficial logo''
| screenshot =
| caption =
| collapsible =
| author = [[OpenGL ARB]]
| developer = [[Khronos Group]]
| released = {{Start date and age|2002|07|24}} (as an extension)
| discontinued =
| latest_release_version = 4.60.8
| latest_release_date = {{Start date and age|2023|08|14}}<ref>{{cite web|url=https://registry.khronos.org/OpenGL/specs/gl/GLSLangSpec.4.60.html|title=The OpenGL® Shading Language, Version 4.60.8}}</ref>
| latest preview version =
| latest preview date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} -->
| programming language =
| operating_system =
| platform = [[Cross-platform]]
| size =
| genre = [[Programming Language]]
| license =
| website =
| standard =
| AsOf =
}}
 
[[File:Linux kernel and OpenGL video games.svg|thumb|300px|[[Video games]] outsource rendering calculations to the [[Graphics processing unit|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 programming language|high-level]] [[shading language]] with a syntax based on the [[C (programming language)|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.
 
Line 59 ⟶ 88:
| 3.00.6<ref>{{cite web|url=https://www.khronos.org/registry/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf|title=GLSL ES Language Specification, Version 3.00, revision 6}}</ref> || 3.0 || 2.0 || 3.30 || 29 January 2016 || #version 300 es
|-
|3.10.5<ref>{{Cite web|title=The OpenGL ES® Shading Language, version 3.10, revision 5|url=https://www.khronos.org/registry/OpenGL/specs/es/3.1/GLSL_ES_Specification_3.10.pdf}}</ref>
| colspan=6 |{{expand list|date=December 2019}}
|3.1
|
|GLSL ES 3.00
|29 January 2016
|#version 310 es
|-
|3.20.6<ref>{{Cite web|title=The OpenGL ES® Shading Language, Version 3.20.6|url=https://www.khronos.org/registry/OpenGL/specs/es/3.2/GLSL_ES_Specification_3.20.pdf}}</ref>
|3.2
|
|GLSL ES 3.10
|10 July 2019
|#version 320 es
|}
 
The two languages are related but not directly compatible. They can be interconverted through [[Standard Portable Intermediate Representation|SPIRSPIRV-Cross]].<ref>{{Citation|last=|first=|title=KhronosGroup/SPIRV-Cross|date=2019-09-06|url=https://github.com/KhronosGroup/SPIRV-Cross|volume=|pages=|publisher=The Khronos Group|access-date=2019-09-08}}</ref>
 
== Language ==
===Operators===
GLSL contains the same operators as the [[operators in C and C++]], with the exception of [[Pointer (computer programming)|pointer]]s. [[Bitwise operators]] were added in version 1.30.
 
===Functions and control structures===
Similar to the [[C (programming language)|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: {{code|mix}}, {{code|smoothstep}}, {{code|normalize}}, {{code|inversesqrt}}, {{code|clamp}}, {{code|length}}, {{code|distance}}, {{code|dot}}, {{code|cross}}, {{code|reflect}}, {{code|refract}} and vector {{code|min}} and {{code|max}}. Other functions like {{code|abs}}, {{code|sin}}, {{code|pow}}, etc, are provided but they can also all operate on vector quantities, i.e. {{code|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 {{code|#include}} and {{code|__FILE__}}.<ref>{{cite book |title=OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 4.3, Eighth Edition |url=https://www.oreilly.com/library/view/opengl-programming-guide/9780132748445/ch02lev2sec5.html |language=en |chapter=Shader Preprocessor}}</ref>
 
The {{code|GL_ARB_shading_language_include}} extension<ref>{{cite web |title=ARB_shading_language_include |url=https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shading_language_include.txt |website=Khronos.org |accessdateaccess-date=2020-05-31}}</ref> (implemented for example in Nvidia drivers<ref>{{cite web |title=NVIDIA driver 265.90 WHQL Quadro |url=https://forums.laptopvideo2go.com/topic/27639-nvidia-driver-26590-whql-quadro/?tab=comments#comment-134520 |archive-url=https://web.archive.org/web/20210308165926/https://forums.laptopvideo2go.com/topic/27639-nvidia-driver-26590-whql-quadro/?tab=comments#comment-134520 |url-status=dead |archive-date=March 8, 2021 |website=LaptopVideo2Go Forums }}</ref> on Windows and Linux, and all Mesa 20.0.0<ref>{{cite web |title=Mesa 20.0.0 Release Notes / 2020-02-19 |url=https://www.mesa3d.org/relnotes/20.0.0.html |website=www.mesa3d.org |accessdateaccess-date=2020-05-31 |archive-date=2020-05-12 |archive-url=https://web.archive.org/web/20200512220620/https://mesa3d.org/relnotes/20.0.0.html |url-status=dead }}</ref> drivers on Linux, FreeBSD and Android) implements ability to use {{code|#include}} in source code, allowing easier sharing of code and definitions between many shaders without extra manual pre-processing. Similar extension {{code|GL_GOOGLE_include_directive}} and {{code|GL_GOOGLE_cpp_style_line_directive}} exist for using GLSL with Vulkan, and are supported in reference SPIR-V compiler ({{code|glslang}} aka glslangValidator).<ref>{{cite web |title=#include directive support by antiagainst · Pull Request #46 · KhronosGroup/glslang |url=https://github.com/KhronosGroup/glslang/pull/46 |website=GitHub |accessdateaccess-date=2020-05-31 |language=en}}</ref><ref>{{cite web |title=Preprocessing line number handling by antiagainst · Pull Request #38 · KhronosGroup/glslang |url=https://github.com/KhronosGroup/glslang/pull/38 |website=GitHub |language=en}}</ref><ref>{{cite web |title=Extend the syntax of #line and __FILE__ to support filename strings by antiagainst · Pull Request #43 · KhronosGroup/glslang |url=https://github.com/KhronosGroup/glslang/pull/43 |website=GitHub |language=en}}</ref>
 
==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]], [[Microsoft Windows|Windows]]). There are language bindings for [[C (programming language)|C]], [[C++]], [[C sharp (programming language)|C#]], [[JavaScript]], [[Delphi (programming language)|Delphi]], [[Java (programming language)|Java]], and many more.
 
GLSL shaders themselves are simply a set of [[String (computer science)|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 [[Wiktionary:on the fly|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.
Line 90 ⟶ 131:
* ARB tessellation shader
* ARB compute shader
 
GLSL shaders can also be used with [[Vulkan (API)|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==
Line 102 ⟶ 145:
*[[ARB assembly language]], a low-level shading language
*[[Cg (programming language)|Cg]], a high-level shading language for programming vertex and pixel shaders
*[[High-levelLevel shaderShader languageLanguage|HLSL]], a high-level shading language for use with [[Direct3D]] and [[SPIR-V]]
*[[TGSI]], a low-level intermediate language introduced by [[Gallium3D]]
*[[AMDIL]], a low-level intermediate language used internally at AMD
*[[RenderMan Shading Language]]
*[[WebGPU Shading Language|WGSL]], a shading language with a Rust-like syntax for WebGPU originally based on [[SPIR-V]]
*[[Metal (API)|Metal Shading Language]], a shading language used with [[Apple Inc.|Apple]] platform's proprietary Metal [[Graphics API]]<ref>{{cite web|url=https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf]|title=Metal Shading Language Specification Version 3.2}}</ref>
*[[Open Shading Language]], a shading language developed by [[Sony Pictures Imageworks]]
 
== References ==
Line 115 ⟶ 161:
* {{cite book | first=Randi J. | last=Rost | author-link = Randi J. Rost | title=OpenGL Shading Language | edition=3rd |publisher=Addison-Wesley | date=30 July 2009 | isbn = 978-0-321-63763-5 }}
* {{cite book | first1=John | last1=Kessenich | first2=David | last2=Baldwin | first3=Randi | last3=Rost | title = The OpenGL Shading Language | version=Version 1.10.59 | publisher = 3Dlabs, Inc. Ltd. }}
* {{cite book | firstfirst1=Mike| lastlast1=Bailey | first2=Steve | last2=Cunningham | title = Graphics Shaders: Theory and Practice | edition=2nd | publisher=CRC Press | date=22 April 2009 | isbn = 978-1-56881-434-6 }}
 
==External links==
Line 130 ⟶ 176:
[[Category:OpenGL]]
[[Category:Shading languages]]
[[Category:Virtual reality]]