GLSL Specification 1.50.11
Transcript of GLSL Specification 1.50.11
-
8/12/2019 GLSL Specification 1.50.11
1/125
The OpenGLShading Language
Language Version: 1.50
Document Revision: 11
04-Dec-2009
John Kessenich
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
-
8/12/2019 GLSL Specification 1.50.11
2/125
Copyright (c) 2008-2009 The Khronos Group Inc. All Rights Reserve.
This speci!ic"tion is protecte #y copyright l"$s "n cont"ins %"teri"l propriet"ry to the Khronos Group&Inc. It or "ny co%ponents %"y not #e reprouce& repu#lishe& istri#ute& tr"ns%itte& ispl"yero"c"st or other$ise e'ploite in "ny %"nner $ithout the e'press prior $ritten per%ission o! KhronosGroup. ou %"y use this speci!ic"tion !or i%ple%enting the !unction"lity therein& $ithout "ltering orre%oving "ny tr"e%"r& copyright or other notice !ro% the speci!ic"tion& #ut the receipt or possession o!this speci!ic"tion oes not convey "ny rights to reprouce& isclose& or istri#ute its contents& or to%"nu!"cture& use& or sell "nything th"t it %"y escri#e& in $hole or in p"rt.
Khronos Group gr"nts e'press per%ission to "ny current *ro%oter& Contri#utor or Aopter %e%#er o!Khronos to copy "n reistri#ute +,/II1/ versions o! this speci!ic"tion in "ny !"shion& provie th"t, CARG1 is %"e !or the speci!ic"tion "n the l"test "v"il"#le up"te o! the speci!ic"tion !or "nyversion o! the A*I is use $henever possi#le. 3uch istri#ute speci!ic"tion %"y #e re-!or%"tte A34,G A3 the contents o! the speci!ic"tion "re not ch"nge in "ny $"y. The speci!ic"tion %"y #eincorpor"te into " prouct th"t is sol "s long "s such prouct inclues signi!ic"nt inepenent $orevelope #y the seller. A lin to the current version o! this speci!ic"tion on the Khronos Group $e#-siteshoul #e inclue $henever possi#le $ith speci!ic"tion istri#utions.
Khronos Group %"es no& "n e'pressly iscl"i%s "ny& represent"tions or $"rr"nties& e'press ori%plie& reg"ring this speci!ic"tion& incluing& $ithout li%it"tion& "ny i%plie $"rr"nties o! %erch"nt"#ilityor !itness !or " p"rticul"r purpose or non-in!ringe%ent o! "ny intellectu"l property. Khronos Group %"es
no& "n e'pressly iscl"i%s "ny& $"rr"nties& e'press or i%plie& reg"ring the correctness& "ccur"cy&co%pleteness& ti%eliness& "n reli"#ility o! the speci!ic"tion. +ner no circu%st"nces $ill the KhronosGroup& or "ny o! its *ro%oters& Contri#utors or e%#ers or their respective p"rtners& o!!icers& irectors&e%ployees& "gents or represent"tives #e li"#le !or "ny "%"ges& $hether irect& inirect& speci"l orconse5uenti"l "%"ges !or lost revenues& lost pro!its& or other$ise& "rising !ro% or in connection $iththese %"teri"ls.
Khronos& penK/1& penKG3& pen6G& penA7& pen34 13 "n pen "re tr"e%"rs o!the Khronos Group Inc. C44A/A is " tr"e%"r o! 3ony Co%puter 1ntert"in%ent Inc. use #yper%ission #y Khronos. penG4 "n pen4 "re registere tr"e%"rs "n the penG4 13 logo is "tr"e%"r o! 3ilicon Gr"phics Inc. use #y per%ission #y Khronos. All other prouct n"%es& tr"e%"rs&"nor co%p"ny n"%es "re use solely !or ienti!ic"tion "n #elong to their respective o$ners.
2
-
8/12/2019 GLSL Specification 1.50.11
3/125
-
8/12/2019 GLSL Specification 1.50.11
4/125
-
8/12/2019 GLSL Specification 1.50.11
5/125
3 Built;in Varia6les......................................................................................................................#93.1 Verte and /eometr( 'hader 'ecial Varia6les................................................................ #9
3.1.1 !omati6ilit( 2ro"ile Verte and /eometr( 'hader 'ecial Varia6les..................... 31
3. )ragment 'hader 'ecial Varia6les................................................................................... 33.% !omati6ilit( 2ro"ile Verte 'hader Built;In Inuts......................................................... 3&3.& Built;In !onstants.............................................................................................................. 3&
3.&.1 !omati6ilit( 2ro"ile Built;In !onstants....................................................................3$3.$ Built;In 5ni"orm 'tate....................................................................................................... 3$
3.$.1 !omati6ilit( 2ro"ile 'tate......................................................................................... 3$3.# !omati6ilit( 2ro"ile Verte and )ragment Inter"ace........................................................34
4 Built;in )unctions..................................................................................................................... 414.1 Angle and -rigonometr( )unctions....................................................................................44. +onential )unctions........................................................................................................4%4.% !ommon )unctions............................................................................................................ 4&
4.& /eometric )unctions.......................................................................................................... 434.$ 8atri )unctions................................................................................................................ 494.# Vector Relational )unctions...............................................................................................974.3 -eture 0ooku )unctions................................................................................................. 914.4 )ragment 2rocessing )unctions........................................................................................1714.9
-
8/12/2019 GLSL Specification 1.50.11
6/125
-
8/12/2019 GLSL Specification 1.50.11
7/125
-
8/12/2019 GLSL Specification 1.50.11
8/125
1 Introduction
1.2 Changes
1.2.1 Changes from revision 9 of version 1.
!lari"( that discard control "low eits the shader.
+lain how to redeclaregl_Texoor!"#.
Remove accidental inclusion o" textureGradOffset on samplerCubeSadow.
!lari"( it is generall( an error to redeclare varia6les, including 6uilt;ins.
1.2.2 Changes from revision ! of version 1.
2ut gl>8a-eture5nits and gl>8a-eture!oords into the comati6ilit( ro"ile. It was an
oversight to not have them there in the revious release.
!hange ARB>comati6ilit( etension to comati6ilit( ro"ile.
'mall clari"(ing comments o" eisting seci"ication.
Add new minimum maimums "or gl>8aVerte*utut!omonents,
gl>8a/eometr(Inut!omonents, gl>8a/eometr(*utut!omonents, and
gl>8a)ragmentInut!omonents, rather than rel(ing on gl>8aVar(ing!omonents. Also,
corrected gl>8aVar(ing!omonents to 6e #7 instead o" #&.
1.2." Changes from revision of version 1.
!omleted "ull udate to grammar section. -ested sec eamles against it:
add multi;samler samler t(es
add unsi@ed arra(s "or 6lock mem6ers
allow artial =uali"ication o" 6lock mem6ers
add assignments within the la(out =uali"ier id list
allow instances names on 6locks, no arra(, unsi@ed arra(, and arra(
Brought ARB>comati6ilit( u to "ull 1.$ "unctionalit(.
!orrected A2I trans"orm"eed6ack eamle in section &.%.3.
!lari"( that when redeclaring 6uilt;ins invariant, "lat, etc.C that 6lock instance names are not used
!lari"( that invariant can 6e used on outut varia6les that are then inut into a su6se=uent shader
stage and 6oth the inut and outut declarations o" that varia6le must 6e declared invariant or it is a
link error.
1.2.# Changes from revision # of version 1.
gl>2rimitiveID comes "rom the rovoking verte, while gl>0a(er comes "rom some verte.
Re=uire etension E/0>ARB>comati6ilit( : ena6leF to use ARB>comati6ilit( "eatures.
2
-
8/12/2019 GLSL Specification 1.50.11
9/125
1 Introduction
5dated grammar "or assignments in la(out =uali"iers and new teture ke(words.
0ots o" editorial changes.
1.2. Summar$ of %unctionalit$ differences from version 1.#
!reated ro"iles. -urned the comati6ilit( etension into the comati6ilit( ro"ile.
5date geometr( shader la(outs: the( must 6e declared, telling the s(stem the rimitive inut and
outut t(es and maimum num6er o" vertices.
Added geometr( shader constants.
!hangedgl_$ragoor! =uali"iers to use the la!out" mechanism instead.
8ade version 6e 1$7, 1.$7, etc.
Added geometr( shaders. -his includes targeting la(ers in )B* rendering.
Addedgl_%rimitive&D as an inut to "ragment shaders.
Added determinantC 6uilt;in.
Broaden arra( usage to include verte shader inuts verte inC.
Broaden structure usage to include "ragment and geometr( inuts and verte and geometr( oututs.
Broadened inter"ace 6locks "rom ?ust uni"orms to in and out inter"aces as well.
Addedgl_$ragoor!=uali"iers origin$upper$left, and pixel$center$integer to modi"( the values
returned 6(gl_$ragoor! and have no a""ect on an( other asect o" the ieline or languageC.
Removed the a6ilit( to have statements 6e"ore the "irst case in a switch.
Added suort "or multi;samle tetures through sampler%&'S andsampler%&'SArra! suort
in texel(etcC and textureSi)eC.
Derecated gl>8aVar(ing!omonentsG
1." Overvie&
-his document descri6es T'e ()en*L +'a!ing Language, version 1.50.
Indeendent comilation units written in this language are calleds'a!ers. A)rogramis a comlete set o"
shaders that are comiled and linked together. -he aim o" this document is to thoroughl( seci"( the
rogramming language. -he *en/0 /rahics '(stem 'eci"ication will seci"( the *en/0 entr(
oints used to maniulate and communicate with rograms and shaders.
1.# 'rror (andling
!omilers, in general, accet rograms that are ill;"ormed, due to the imossi6ilit( o" detecting all ill;
"ormed rograms. 2orta6ilit( is onl( ensured "or well;"ormed rograms, which this seci"ication
descri6es. !omilers are encouraged to detect ill;"ormed rograms and issue diagnostic messages, 6ut are
not re=uired to do so "or all cases. !omilers are re=uired to return messages regarding leicall(,
grammaticall(, or semanticall( incorrect shaders.
3
-
8/12/2019 GLSL Specification 1.50.11
10/125
1 Introduction
1. T$pographical Conventions
Italic, 6old, and "ont choices have 6een used in this seci"ication rimaril( to imrove reada6ilit(. !ode
"ragments use a "ied width "ont. Identi"iers em6edded in tet are italici@ed. Ke(words em6edded in tetare 6old. *erators are called 6( their name, "ollowed 6( their s(m6ol in 6old in arentheses. -he
clari"(ing grammar "ragments in the tet use 6old "or literals and italics "or non;terminals. -he o""icial
grammar in 'ection 9 E'hading 0anguage /rammarF uses all caitals "or terminals and lower case "or
non;terminals.
1.! )eprecation
2revious versions o" the *en/0 'hading 0anguage derecated some "eatures. -hese are clearl( called
out in this seci"ication as EderecatedF. -he( are still resent in this version o" the language, 6ut are
targeted "or otential removal in a "uture version o" the shading language. -he *en/0 A2I has a
"orward comati6ilit( mode that will disallow use o" derecated "eatures. I" comiling in a mode where
use o" derecated "eatures is disallowed, their use causes comile time errors. 'ee the *en/0 /rahics
'(stem 'eci"ication "or details on what causes derecated language "eatures to 6e acceted or to returnan error.
4
-
8/12/2019 GLSL Specification 1.50.11
11/125
2 Overvie& of OpenGL Shading
-he *en/0 'hading 0anguage is actuall( several closel( related languages. -hese languages are used
to create shaders "or each o" the rogramma6le rocessors contained in the *en/0 rocessing ieline.
!urrentl(, these rocessors are the verte, geometr(, and "ragment rocessors.
5nless otherwise noted in this aer, a language "eature alies to all languages, and common usage will
re"er to these languages as a single language. -he seci"ic languages will 6e re"erred to 6( the name o"
the rocessor the( target: verte, geometr(, or "ragment.
8ost *en/0 state is not tracked or made availa6le to shaders. -(icall(, user;de"ined varia6les will 6e
used "or communicating 6etween di""erent stages o" the *en/0 ieline. owever, a small amount o"
state is still tracked and automaticall( made availa6le to shaders, and there are a "ew 6uilt;in varia6les "orinter"aces 6etween di""erent stages o" the *en/0 ieline.
2.1 *erte+ ,rocessor
-he vertex )rocessoris a rogramma6le unit that oerates on incoming vertices and their associated data.
!omilation units written in the *en/0 'hading 0anguage to run on this rocessor are called vertex
s'a!ers. Hhen a comlete set o" verte shaders are comiled and linked, the( result in a vertex s'a!er
executalethat runs on the verte rocessor.
-he verte rocessor oerates on one verte at a time. It does not relace grahics oerations that re=uire
knowledge o" several vertices at a time.
2.2 Geometr$ ,rocessor-hegeometr )rocessor is a rogramma6le unit that oerates on data "or incoming vertices "or a rimitive
assem6led a"ter verte rocessing and oututs a se=uence o" vertices "orming outut rimitives.
!omilation units written in the *en/0 'hading 0anguage to run on this rocessor are calledgeometr
s'a!ers. Hhen a comlete set o" geometr( shaders are comiled and linked, the( result in ageometr
s'a!er executale that runs on the geometr( rocessor.
A single invocation o" the geometr( shader eecuta6le on the geometr( rocessor will oerate on a
declared inut rimitive with a "ied num6er o" vertices. -his single invocation can emit a varia6le
num6er o" vertices that are assem6led into rimitives o" a declared outut rimitive t(e and assed to
su6se=uent ieline stages.
2." %ragment ,rocessor-he/ragment )rocessoris a rogramma6le unit that oerates on "ragment values and their associated
data. !omilation units written in the *en/0 'hading 0anguage to run on this rocessor are called
/ragment s'a!ers. Hhen a comlete set o" "ragment shaders are comiled and linked, the( result in a
/ragment s'a!er executalethat runs on the "ragment rocessor.
5
-
8/12/2019 GLSL Specification 1.50.11
12/125
2 Overview of OpenGL Shading
A "ragment shader cannot change a "ragments x,C osition. Access to neigh6oring "ragments is not
allowed. -he values comuted 6( the "ragment shader are ultimatel( used to udate "rame6u""er memor(
or teture memor(, deending on the current *en/0 state and the *en/0 command that caused the
"ragments to 6e generated.
6
-
8/12/2019 GLSL Specification 1.50.11
13/125
" -asics
".1 Character Set
-he source character set used "or the *en/0 shading languages is a su6set o" A'!II. It includes the
"ollowing characters:
-he letters a*), A*+, and the underscore >C.
-he num6ers-*.
-he s(m6ols eriod .C, lus /C, dash *C, slash 0C, asterisk C, ercent 2C, angled 6rackets 3and
4C, s=uare 6rackets 5and 6 C, arentheses "and C, 6races 7and 8C, caret 9C, vertical 6ar :C,
amersand ;C, tilde C, colon ?C, semicolon @C, comma ,C, and=uestion mark C.
-he num6er sign #C "or rerocessor use.
Hhite sace: the sace character, hori@ontal ta6, vertical ta6, "orm "eed, carriage;return, and line;
"eed.
0ines are relevant "or comiler diagnostic messages and the rerocessor. -he( are terminated 6(
carriage;return or line;"eed. I" 6oth are used together, it will count as onl( a single line termination. )or
the remainder o" this document, an( o" these com6inations is siml( re"erred to as a new;line. -here is no
line continuation character.
In general, the languages use o" this character set is case sensitive.
-here are no character or string data t(es, so no =uoting characters are included.
-here is no end;o";"ile character.
".2 Source Strings
-he source "or a single shader is an arra( o" strings o" characters "rom the character set. A single shader
is made "rom the concatenation o" these strings. +ach string can contain multile lines, searated 6( new;
lines.
-
8/12/2019 GLSL Specification 1.50.11
14/125
3 Basics
"." ,reprocessor
-here is a rerocessor that rocesses the source strings as art o" the comilation rocess.
-he comlete list o" rerocessor directives is as "ollows.
#
#define
#undef
#if
#ifdef
#ifndef
#else
#elif
#endif
#error
#pragma
#extension
#version
#line
-he "ollowing oerators are also availa6le
defined
##
+ach num6er sign #C can 6e receded in its line onl( 6( saces or hori@ontal ta6s. It ma( also 6e
"ollowed 6( saces and hori@ontal ta6s, receding the directive. +ach directive is terminated 6( a new;
line. 2rerocessing does not change the num6er or relative location o" new;lines in a source string.
-he num6er sign #C on a line 6( itsel" is ignored. An( directive not listed a6ove will cause a diagnostic
message and make the imlementation treat the shader as ill;"ormed.
#defineand #undef"unctionalit( are de"ined as is standard "or ! rerocessors "or macro de"initions
6oth with and without macro arameters.
-he "ollowing rede"ined macros are availa6le
__LINE__
__FILE__
__VERSION__
__L&__will su6stitute a decimal integer constant that is one more than the num6er o" receding new;lines in the current source string.
__$&L__will su6stitute a decimal integer constant that sa(s which source string num6er is currentl(
6eing rocessed.
-
8/12/2019 GLSL Specification 1.50.11
15/125
3 Basics
__VR+&(__will su6stitute a decimal integer re"lecting the version num6er o" the *en/0 shading
language. -he version o" the shading language descri6ed in this document will have__VR+&(__
su6stitute the decimal integer 1$7.
All macro names containing two consecutive underscores $$ C are reserved "or "uture use as rede"ined
macro names. All macro names re"ied with E/0>F E/0F "ollowed 6( a single underscoreC are also
reserved.
#if, #ifdef, #ifndef, #else, #elif, and#endifare de"ined to oerate as is standard "or ! rerocessors.
+ressions "ollowing #ifand #elifare "urther restricted to eressions oerating on literal integer
constants, lus identi"iers consumed 6( the definedoerator. It is an error to use #if or #elif on
eressions containing unde"ined macro names, other than as arguments to the defined oerator.
!haracter constants are not suorted. -he oerators availa6le are as "ollows.
,recedence Operator class Operators ssociativit$
1 highestC arenthetical grouing C
-
8/12/2019 GLSL Specification 1.50.11
16/125
3 Basics
2rerocessor eressions will 6e evaluated according to the 6ehavior o" the host rocessor, not the
rocessor targeted 6( the shader.
#error will cause the imlementation to ut a diagnostic message into the shader o6?ects in"ormation log
see section #.1.17 'hader and 2rogram ueries in the *en/0 /rahics '(stem 'eci"ication "or how to
access a shader o6?ects in"ormation logC. -he message will 6e the tokens "ollowing the #errordirective,
u to the "irst new;line. -he imlementation must then consider the shader to 6e ill;"ormed.
#pragmaallows imlementation deendent comiler control. -okens "ollowing #pragmaare not su6?ect
to rerocessor macro eansion. I" an imlementation does not recogni@e the tokens "ollowing
#pragma, then it will ignore that ragma. -he "ollowing ragmas are de"ined as art o" the language.
#pragma ST!L
-he S&GDragma is used to reserve ragmas "or use 6( "uture revisions o" this language.
-
8/12/2019 GLSL Specification 1.50.11
17/125
3 Basics
I" the otional)ro/ile argument is rovided, it must 6e the name o" an *en/0 ro"ile. !urrentl(, there
are two choices:
%ore
%ompati$ilit&
I" no)ro/ile argument is rovided, the de"ault is core. 5nless otherwise seci"ied, this seci"ication is
documenting the core ro"ile, and ever(thing seci"ied "or the core ro"ile is also availa6le in the
comati6ilit( ro"ile. )eatures seci"ied as 6elonging seci"icall( to the comati6ilit( ro"ile are not
availa6le in the core ro"ile.
-here is a 6uilt;in macro de"inition "or each ro"ile the imlementation suorts. All imlementations
rovide the "ollowing macro:
#define !L_%ore_profile '
Imlementations roviding the comati6ilit( ro"ile rovide the "ollowing macro:
#define !L_%ompati$ilit&_profile '
-he #versiondirective must occur in a shader 6e"ore an(thing else, ecet "or comments and white sace.
11
-
8/12/2019 GLSL Specification 1.50.11
18/125
3 Basics
B( de"ault, comilers o" this language must issue comile time s(ntactic, grammatical, and semantic
errors "or shaders that do not con"orm to this seci"ication. An( etended 6ehavior must "irst 6e ena6led.
Directives to control the 6ehavior o" the comiler with resect to etensions are declared with the
#extensiondirective
#extension extension_name:behavior
#extension all :behavior
where extension_nameis the name o" an etension. +tension names are not documented in this
seci"ication. -he token allmeans the 6ehavior alies to all etensions suorted 6( the comiler. -he
e'avior can 6e one o" the "ollowing
behavior 'ffect
re/uire Behave as seci"ied 6( the etension extension_name.
/ive an error on the #extension i" the etension extension_name is notsuorted, or i" allis seci"ied.
enable Behave as seci"ied 6( the etension extension_name.
Harn on the #extensioni" the etension extension_nameis not suorted.
/ive an error on the #extension i" allis seci"ied.
&arn Behave as seci"ied 6( the etension extension_name, ecet issue warningson an( detecta6le use o" that etension, unless such use is suorted 6( otherena6led or re=uired etensions.
I" all is seci"ied, then warn on all detecta6le uses o" an( etension used.
Harn on the #extensioni" the etension extension_nameis not suorted.
disable Behave including issuing errors and warningsC as i" the etensionextension_nameis not art o" the language de"inition.
I" all is seci"ied, then 6ehavior must revert 6ack to that o" the non;etendedcore version o" the language 6eing comiled to.
Harn on the #extension i" the etension extension_nameis not suorted.
-he extensiondirective is a simle, low;level mechanism to set the 6ehavior "or each etension. It does
not de"ine olicies such as which com6inations are aroriate, those must 6e de"ined elsewhere. *rder
o" directives matters in setting the 6ehavior "or each etension: Directives that occur later override those
seen earlier. -he allvariant sets the 6ehavior "or all etensions, overriding all reviousl( issued
extension directives, 6ut onl( "or the e'aviorswarnand disable.
12
-
8/12/2019 GLSL Specification 1.50.11
19/125
3 Basics
-he initial state o" the comiler is as i" the directive
#extension all ( disa$le
was issued, telling the comiler that all error and warning reorting must 6e done according to thisseci"ication, ignoring an( etensions.
+ach etension can de"ine its allowed granularit( o" scoe. I" nothing is said, the granularit( is a shader
that is, a single comilation unitC, and the etension directives must occur 6e"ore an( non;rerocessor
tokens. I" necessar(, the linker can en"orce granularities larger than a single comilation unit, in which
case each involved shader will have to contain the necessar( etension directive.
8acro eansion is not done on lines containing #extensionand #versiondirectives.
#linemust have, a"ter macro su6stitution, one o" the "ollowing "orms:
#line line
#line line source-string-number
where line andsource-string-numer are constant integer eressions. A"ter rocessing this directiveincluding its new;lineC, the imlementation will 6ehave as i" it is comiling at line num6er line1and
source string num6ersource-string-numer. 'u6se=uent source strings will 6e num6ered se=uentiall(,
until another #linedirective overrides that num6ering.
".# Comments
!omments are delimited 6( N and N, or 6( and a new;line. -he 6egin comment delimiters N or C are
not recogni@ed as comment delimiters inside o" a comment, hence comments cannot 6e nested. I" a
comment resides entirel( within a single line, it is treated s(ntacticall( as a single sace.
-
8/12/2019 GLSL Specification 1.50.11
20/125
3 Basics
". To0ens
-he language is a se=uence o" tokens. A token can 6e
to3en:
3eor!
i!enti/ier
integer-constant
/loating-constant
o)erator
@ 7 8
".! e$&ords
-he "ollowing are the ke(words in the language, and cannot 6e used "or an( other urose than that
de"ined 6( this document:
attribute const uniform var!ing
la!out
centroid flat smoot noperspective
break continue do for wile switc case default
if else
in out inout
float int void bool true false
invariant
discard return
mat% matF mat
mat%x% mat%xF mat%x
matFx% matFxF matFx
matx% matxF matx
vec% vecF vec ivec% ivecF ivec bvec% bvecF bvec
uint uvec% uvecF uvec
lowp mediump igp precision
sampler1& sampler%& samplerF& samplerCube
sampler1&Sadow sampler%&Sadow samplerCubeSadow sampler1&Arra! sampler%&Arra!
sampler1&Arra!Sadow sampler%&Arra!Sadow
isampler1& isampler%& isamplerF& isamplerCube
isampler1&Arra! isampler%&Arra!
14
-
8/12/2019 GLSL Specification 1.50.11
21/125
3 Basics
usampler1& usampler%& usamplerF& usamplerCube
usampler1&Arra! usampler%&Arra!
sampler%&Hect sampler%&HectSadow isampler%&Hect usampler%&Hect samplerIuffer isamplerIuffer usamplerIuffer
sampler%&'S isampler%&'S usampler%&'S
sampler%&'SArra! isampler%&'SArra! usampler%&'SArra!
struct
-he "ollowing are the ke(words reserved "or "uture use. 5sing them will result in an error:
common partition active
asm
class union enum t!pedef template tis packed
goto
inline noinline volatile public static extern external interface
long sort double alf fixed unsigned superp
input output
vec% vecF vec dvec% dvecF dvec fvec% fvecF fvec
samplerF&Hect
filter
image1& image%& imageF& imageCube
iimage1& iimage%& iimageF& iimageCube
uimage1& uimage%& uimageF& uimageCube
image1&Arra! image%&Arra!
iimage1&Arra! iimage%&Arra! uimage1&Arra! uimage%&Arra!
image1&Sadow image%&Sadow
image1&Arra!Sadow image%&Arra!Sadow
imageIuffer iimageIuffer uimageIuffer
si)eof cast
namespace using
row$maJor
In addition, all identi"iers containing two consecutive underscores $$C are reserved as ossi6le "uture
ke(words.
15
-
8/12/2019 GLSL Specification 1.50.11
22/125
3 Basics
". Identifiers
Identi"iers are used "or varia6le names, "unction names, structure names, and "ield selectors "ield
selectors select comonents o" vectors and matrices similar to structure "ields, as discussed in 'ection $.$EVector !omonentsF and 'ection $.# E8atri !omonentsF C. Identi"iers have the "orm
i!enti/ier
non!igit
i!enti/ier non!igit
i!enti/ier !igit
non!igit: one o"
$ a b c d e f g i J k l m n o p K r s t u v w x ! )
A I C & L ( G M N P D ' Q O R H S T U V W X +
!igit: one o"
- 1 % F E Y Z [
Identi"iers starting with Egl>F are reserved "or use 6( *en/0, and ma( not 6e declared in a shader as
either a varia6le or a "unction. owever, as noted in the seci"ication, there are some cases where
reviousl( declared varia6les can 6e redeclared to change or add some roert(, and redeclared gl>
names are allowed to 6e redeclared in a shader.onl( "or these seci"ic uroses. 8ore generall(, it is an
error to redeclare a varia6le, including those starting Egl>F.
".3 Static 4se
'ome language rules descri6ed 6elow deend on whether something isstaticall written or used.
A shader contains astatic useo" orstatic assignmenttoC a varia6lexi", a"ter rerocessing, the shader
contains a statement that would read or writeCx, whether or not run;time "low o" control will cause that
statement to 6e eecuted.
16
-
8/12/2019 GLSL Specification 1.50.11
23/125
# *ariables and T$pes
All varia6les and "unctions must 6e declared 6e"ore 6eing used. Varia6le and "unction names are
identi"iers.
-here are no de"ault t(es. All varia6le and "unction declarations must have a declared t(e, and
otionall( =uali"iers. A varia6le is declared 6( seci"(ing its t(e "ollowed 6( one or more names
searated 6( commas. In man( cases, a varia6le can 6e initiali@ed as art o" its declaration 6( using the
assignment oerator =C. -he grammar near the end o" this document rovides a "ull re"erence "or the
s(nta o" declaring varia6les.
5ser;de"ined t(es ma( 6e de"ined using structto aggregate a list o" eisting t(es into a single name.
-he *en/0 'hading 0anguage is t(e sa"e. -here are no imlicit conversions 6etween t(es, with theecetion that an integer value ma( aear where a "loating;oint t(e is eected, and 6e converted to a
"loating;oint value. +actl( how and when this can occur is descri6ed in 'ection &.1.17EImlicit
!onversionsF and as re"erenced 6( other sections in this seci"ication.
#.1 -asic T$pes
-he *en/0 'hading 0anguage suorts the "ollowing 6asic data t(es, groued as "ollows.
-ransarent t(es
T$pe 5eaning
void "or "unctions that do not return a value
bool a conditional t(e, taking on values o" true or "alse
int a signed integer
uint an unsigned integer
float a single "loating;oint scalar
vec% a two;comonent "loating;oint vector
vecF a three;comonent "loating;oint vector
vec a "our;comonent "loating;oint vector
bvec% a two;comonent Boolean vector
bvecF a three;comonent Boolean vector
bvec a "our;comonent Boolean vector
ivec% a two;comonent signed integer vector
ivecF a three;comonent signed integer vector
ivec a "our;comonent signed integer vector
17
-
8/12/2019 GLSL Specification 1.50.11
24/125
4 #aria$%es and &'pes
T$pe 5eaning
uvec% a two;comonent unsigned integer vector
uvecF a three;comonent unsigned integer vectoruvec a "our;comonent unsigned integer vector
mat% a W "loating;oint matri
matF a %W% "loating;oint matri
mat a &W& "loating;oint matri
mat%x% same as a mat%
mat%xF a "loating;oint matri with columns and % rows
mat%x a "loating;oint matri with columns and & rows
matFx% a "loating;oint matri with % columns and rows
matFxF same as a matFmatFx a "loating;oint matri with % columns and & rows
matx% a "loating;oint matri with & columns and rows
matxF a "loating;oint matri with & columns and % rows
matx same as a mat
)loating 2oint 'amler -(es oa=ueC
T$pe 5eaning
sampler1& a handle "or accessing a 1D teture
sampler%& a handle "or accessing a D teture
samplerF& a handle "or accessing a %D teture
samplerCube a handle "or accessing a cu6e maed teture
sampler%&Hect a handle "or accessing a rectangular teture
sampler1&Sadow a handle "or accessing a 1D deth teture with comarison
sampler%&Sadow a handle "or accessing a D deth teture with comarison
sampler%&HectSadow a handle "or accessing a rectangular teture with comarison
sampler1&Arra! a handle "or accessing a 1D arra( teture
sampler%&Arra! a handle "or accessing a D arra( teture
sampler1&Arra!Sadow a handle "or accessing a 1D arra( deth teture with comarisonsampler%&Arra!Sadow a handle "or accessing a D arra( deth teture with comarison
samplerIuffer a handle "or accessing a 6u""er teture
sampler%&'S a handle "or accessing a D multi;samle teture
1
-
8/12/2019 GLSL Specification 1.50.11
25/125
4 #aria$%es and &'pes
T$pe 5eaning
sampler%&'SArra! a handle "or accessing a D multi;samle arra( teture
'igned Integer 'amler -(es oa=ueC
T$pe 5eaning
isampler1& a handle "or accessing an integer 1D teture
isampler%& a handle "or accessing an integer D teture
isamplerF& a handle "or accessing an integer %D teture
isamplerCube a handle "or accessing an integer cu6e maed teture
isampler%&Hect a handle "or accessing an integer D rectangular teture
isampler1&Arra! a handle "or accessing an integer 1D arra( teture
isampler%&Arra! a handle "or accessing an integer D arra( teture
isamplerIuffer a handle "or accessing an integer 6u""er teture
isampler%&'S a handle "or accessing an integer D multi;samle teture
isampler%&'SArra! a handle "or accessing an integer D multi;samle arra( teture
5nsigned Integer 'amler -(es oa=ueC
T$pe 5eaning
usampler1& a handle "or accessing an unsigned integer 1D teture
usampler%& a handle "or accessing an unsigned integer D teture
usamplerF& a handle "or accessing an unsigned integer %D teture
usamplerCube a handle "or accessing an unsigned integer cu6e maed teture
usampler%&Hect a handle "or accessing an unsigned integer rectangular teture
usampler1&Arra! a handle "or accessing an unsigned integer 1D arra( teture
usampler%&Arra! a handle "or accessing an unsigned integer D arra( teture
usamplerIuffer a handle "or accessing an unsigned integer 6u""er teture
usampler%&'S a handle "or accessing an unsigned integer D multi;samle teture
usampler%&'SArra! a handle "or accessing an unsigned integer D multi;samle teturearra(
In addition, a shader can aggregate these using arra(s and structures to 6uild more comle t(es.
-here are no ointer t(es.
1!
-
8/12/2019 GLSL Specification 1.50.11
26/125
4 #aria$%es and &'pes
#.1.1 *oid
)unctions that do not return a value must 6e declared as void. -here is no de"ault "unction return t(e.
-he ke(word voidcannot 6e used in an( other declarations ecet "or emt( "ormal or actual arameter
listsC.
#.1.2 -ooleans
-o make conditional eecution o" code easier to eress, the t(e boolis suorted. -here is no
eectation that hardware directl( suorts varia6les o" this t(e. It is a genuine Boolean t(e, holding
onl( one o" two values meaning either true or "alse. -wo ke(words trueand falsecan 6e used as literal
Boolean constants. Booleans are declared and otionall( initiali@ed as in the "ollow eamle:
$ool su%%ess ** de%lare +su%%ess, to $e a -oolean
$ool done . false ** de%lare and initiali"e +done,
-he right side o" the assignment oerator = C must 6e an eression whose t(e is bool.
+ressions used "or conditional ?ums if, for, ?, wile, do*wileC must evaluate to the t(e bool.
#.1." Integers
'igned and unsigned integer varia6les are "ull( suorted. In this document, the term integer is meant to
generall( include 6oth signed and unsigned integers. 5nsigned integers have eactl( % 6its o" recision.
'igned integers use % 6its, including a sign 6it, in twos comlement "orm. *erations resulting in
over"low or under"low will not cause an( ecetion, nor will the( saturate, rather the( will EwraF to (ield
the low;order % 6its o" the result.
Integers are declared and otionall( initiali@ed with integer eressions, as in the "ollowing eamle:
int i/ 0 . 12 ** default integer literal t&pe is int
uint 3 . 4u ** +u, esta$lis5es t5e t&pe as uint
2"
-
8/12/2019 GLSL Specification 1.50.11
27/125
4 #aria$%es and &'pes
0iteral integer constants can 6e eressed in decimal 6ase 17C, octal 6ase 4C, or headecimal 6ase 1#C
as "ollows.
integer-constant :
!ecimal-constant integer-su//ixo)t
octal-constant integer-su//ixo)t
'exa!ecimal-constant integer-su//ixo)t
integer-su//ix: one o"
u T
!ecimal-constant :
nonero-!igit
!ecimal-constant !igit
octal-constant :
-
octal-constant octal-!igit
'exa!ecimal-constant :
7'exa!ecimal-!igit
7X'exa!ecimal-!igit
'exa!ecimal-constant 'exa!ecimal-!igit
!igit :
-
nonero-!igit
nonero-!igit : one o"
1 % F E Y Z [
octal-!igit? one o"
- 1 % F E Y Z
'exa!ecimal-!igit?one o"
- 1 % F E Y Z [
a b c d e f
A I C & L (
-
8/12/2019 GLSL Specification 1.50.11
28/125
-
8/12/2019 GLSL Specification 1.50.11
29/125
4 #aria$%es and &'pes
#.1. *ectors
-he *en/0 'hading 0anguage includes data t(es "or generic ;, %;, and &;comonent vectors o"
"loating;oint values, integers, or Booleans. )loating;oint vector varia6les can 6e used to store colors,
normals, ositions, teture coordinates, teture looku results and the like. Boolean vectors can 6e used
"or comonent;wise comarisons o" numeric vectors. 'ome eamles o" vector declaration are:
ve%2 tex%oord'/ tex%oord2
ve%4 position
ve%1 m&R!-8
ive%2 textureLoo3up
$ve%4 less
Initiali@ation o" vectors can 6e done with constructors, which are discussed shortl(.
#.1.! 5atrices
-he *en/0 'hading 0anguage has 6uilt;in t(es "or W, W%, W&, %W, %W%, %W&, &W, &W%, and &W&matrices o" "loating;oint num6ers. -he "irst num6er in the t(e is the num6er o" columns, the second is
the num6er o" rows. +amle matri declarations:
mat2 mat2
mat4 opt9atrix
mat1 vie:/ pro0e%tion
mat1x1 vie: ** an alternate :a& of de%laring a mat1
mat4x2 m ** a matrix :it5 4 %olumns and 2 ro:s
Initiali@ation o" matri values is done with constructors descri6ed in 'ection $.& E!onstructorsF C in
column;ma?or order.
#.1. Samplers
'amler t(es e.g., sampler%&C are e""ectivel( oa=ue handles to tetures and their "ilters. -he( are
used with the 6uilt;in teture "unctions descri6ed in 'ection 4.3 E-eture 0ooku )unctionsF C to seci"(
which teture to access and how it is to 6e "iltered. -he( can onl( 6e declared as "unction arameters or
uniformvaria6les see 'ection &.%.$ E5ni"ormF C. +cet "or arra( indeing, structure "ield selection,
and arentheses, samlers are not allowed to 6e oerands in eressions. 'amlers aggregated into arra(s
within a shader using s=uare 6rackets 5 6C can onl( 6e indeed with integral constant eressions see
'ection &.%.% E!onstant +ressionsFC. 'amlers cannot 6e treated as l;valuesG hence cannot 6e used as
out or inout "unction arameters, nor can the( 6e assigned into. As uni"orms, the( are initiali@ed onl(
with the *en/0 A2IG the( cannot 6e declared with an initiali@er in a shader. As "unction arameters,
onl( samlers ma( 6e assed to samlers o" matching t(e. -his ena6les consistenc( checking 6etween
shader teture accesses and *en/0 teture state 6e"ore a shader is run.
23
-
8/12/2019 GLSL Specification 1.50.11
30/125
4 #aria$%es and &'pes
#.1.3 Structures
5ser;de"ined t(es can 6e created 6( aggregating other alread( de"ined t(es into a structure using the
structke(word. )or eamle,
stru%t lig5t ;
float intensit&
ve%4 position
< lig5tVar
In this eamle, lig't6ecomes the name o" the new t(e, and lig'tVar6ecomes a varia6le o" t(e lig't.
-o declare varia6les o" the new t(e, use its name without the ke(word structC.
lig5t lig5tVar2
8ore "ormall(, structures are declared as "ollows. owever, the comlete correct grammar is as given in
'ection 9 E'hading 0anguage /rammarF .
struct-!e/inition :6uali/ier
o)t structname
o)t7memer-list 8!eclarators
o)t7
memer-list :
memer-!eclaration7
memer-!eclaration memer-list7
memer-!eclaration :
asic-t)e !eclarators7
where name6ecomes the user;de"ined t(e, and can 6e used to declare varia6les to 6e o" this new t(e.
-he nameshares the same name sace as other varia6les, t(es, and "unctions. All reviousl( visi6le
varia6les, t(es, constructors, or "unctions with that name are hidden. -he otional 6uali/ieronl( alies
to an( !eclarators, and is not art o" the t(e 6eing de"ined "or name.
'tructures must have at least one mem6er declaration. 8em6er declarators ma( contain recision
=uali"iers, 6ut ma( not contain an( other =uali"iers. Bit "ields are not suorted. 8em6er t(es must 6e
alread( de"ined there are no "orward re"erencesC. 8em6er declarations cannot contain initiali@ers.
8em6er declarators can contain arra(s. 'uch arra(s must have a si@e seci"ied, and the si@e must 6e an
integral constant eression thats greater than @ero see 'ection &.%.% E!onstant +ressionsFC. +ach
level o" structure has its own name sace "or names given in mem6er declaratorsG such names need onl(
6e uni=ue within that name sace.
24
-
8/12/2019 GLSL Specification 1.50.11
31/125
4 #aria$%es and &'pes
Anon(mous structures are not suorted. +m6edded structure de"initions are not suorted.
stru%t S ; float f
-
8/12/2019 GLSL Specification 1.50.11
32/125
4 #aria$%es and &'pes
and as an alternate wa( o" declaring a varia6le or "unction arameter.
float>7? a
It is an error to declare arra(s o" arra(s:
float a>7?>4? ** illegal
float>7? a>4? ** illegal
Arra(s can have initiali@ers "ormed "rom arra( constructors:
float a>7? . float>7?461/ 162/ 76A/ 762/ '6')
float a>7? . float>?461/ 162/ 76A/ 762/ '6') ** same t5ing
5nsi@ed arra(s can 6e elicitl( si@ed 6( an initiali@er at declaration time:
float a>7?
666
float $>? . a ** $ is expli%itl& si"e 7
float $>7? . a ** means t5e same t5ing
owever, imlicitl( si@ed arra(s cannot 6e assigned to.
-
8/12/2019 GLSL Specification 1.50.11
33/125
4 #aria$%es and &'pes
#.1.16 Implicit Conversions
In some situations, an eression and its t(e will 6e imlicitl( converted to a di""erent t(e. -he
"ollowing ta6le shows all allowed imlicit conversions:
T$pe of e+pression Can be implicitl$ converted to
int
uint
float
ivec%uvec%
vec%
ivecFuvecF
vecF
ivecuvec
vec
-here are no imlicit arra( or structure conversions. )or eamle, an arra( o" intcannot 6e imlicitl(converted to an arra( o" float. -here are no imlicit conversions 6etween signed and unsigned integers.
Hhen an imlicit conversion is done, it is not a re;interretation o" the eressions 6it attern, 6ut a
conversion o" its value to an e=uivalent value in the new t(e. )or eamle, the integer value ;Ewill 6e
converted to the "loating;oint value ;E.-. Integer values having more 6its o" recision than a "loating
oint mantissa will lose recision when converted to float.
-he conversions in the ta6le a6ove are done onl( as indicated 6( other sections o" this seci"ication.
#.2 Scoping
-he scoe o" a varia6le is determined 6( where it is declared. I" it is declared outside all "unction
de"initions, it has glo6al scoe, which starts "rom where it is declared and ersists to the end o" the shader
it is declared in. I" it is declared in a wiletest or a for statement, then it is scoed to the end o" the
"ollowing su6;statement. *therwise, i" it is declared as a statement within a comound statement, it is
scoed to the end o" that comound statement. I" it is declared as a arameter in a "unction de"inition, it is
scoed until the end o" that "unction de"inition. A "unction 6od( has a scoe nested inside the "unctions
de"inition. -he ifstatements eression does not allow new varia6les to 6e declared, hence does not
"orm a new scoe.
27
-
8/12/2019 GLSL Specification 1.50.11
34/125
4 #aria$%es and &'pes
Hithin a declaration, the scoe o" a name starts immediatel( a"ter the initiali@er i" resent or immediatel(
a"ter the name 6eing declared i" not. 'everal eamles:
int x . '
;
int x . 2/ & . x ** & is initiali"ed to 2
? ** si"e un3no:n
666Color'6lengt5)666** illegal/ lengt5) un3no:n
in ve%1 Color2>2? ** si"e is 2666Color'6lengt5)666** illegal/ Color' still 5as no si"e
in ve%1 Color4>4? ** illegal/ input si"es are in%onsistent
la&outlines) in ** legal/ input si"e is 2/ mat%5ing
in ve%1 Color1>4? ** illegal/ %ontradi%ts la&out
666Color'6lengt5)666** legal/ lengt5) is 2/ Color' si"ed $& la&out)
la&outlines) in ** legal/ mat%5es ot5er la&out) de%laration
la&outtriangles) in** illegal/ does not mat%5 earlier la&out) de%laration
It is a link;time error i" not all rovided si@es si@ed inut arra(s and la(out si@eC match across all
geometr( shaders in the rogram.
)ragment shaders can have an inut la(out onl( "or redeclaring the 6uilt;in varia6legl_$ragoor! see
section 3.)ragment 'hader 'ecial Varia6lesC. -he la(out =uali"ier identi"iers "orgl_$ragoor!are
laout-6uali/ier-i!
origin$upper$left
pixel$center$integer
B( de"ault,gl_$ragoor! assumes a lower;le"t origin "or window coordinates and assumes iel centers
are located at hal";iel coordinates. )or eamle, the x, C location 7.$, 7.$C is returned "or the lower;
le"t;most iel in a window. -he origin can 6e changed 6( redeclaringgl_$ragoor! with the
origin$upper$left identi"ier, moving the origin o"gl_$ragoor! to the uer le"t o" the window, with
increasing in value toward the 6ottom o" the window. -he values returned can also 6e shi"ted 6( hal" a
iel in 6othx and6( pixel$center$integer so it aears the iels are centered at whole num6er iel
o""sets. -his moves the x,C valuereturned 6(gl_$ragoor! o" 7.$, 7.$C 6( de"ault, to 7.7, 7.7C with
pixel$center$integer. Redeclarations are done as "ollows
in ve%1 gl_FragCoord ** rede%laration t5at %5anges not5ing is allo:ed
** 8ll t5e follo:ing are allo:ed rede%laration t5at %5ange $e5avior
la&outorigin_upper_left) in ve%1 gl_FragCoord
la&outpixel_%enter_integer) in ve%1 gl_FragCoord
la&outorigin_upper_left/ pixel_%enter_integer) in ve%1 gl_FragCoord
I"gl_$ragoor! is redeclared in an( "ragment shader in a rogram, it must 6e redeclared in all the
"ragment shaders in that rogram that have a static use gl_$ragoor!. All redeclarations o"
gl_$ragoor! in all "ragment shaders in a single rogram must have the same set o" =uali"iers. Hithin
an( shader, the "irst redeclarations o"gl_$ragoor! must aear 6e"ore an( use o"gl_$ragoor!. -he
6uilt;ingl_$ragoor!is onl( redeclared in "ragment shaders, so redeclaring it in an( other shader
language will 6e illegal.
Redeclaringgl_$ragoor! with origin$upper$left andor pixel$center$integer=uali"iers onl( a""ects
gl_$ragoor!.x and gl_$ragoor!.. It has no a""ect on rasteri@ation, trans"ormation, or an( other art
o" the *en/0 ieline or language "eatures.
3!
-
8/12/2019 GLSL Specification 1.50.11
46/125
4 #aria$%es and &'pes
#.".3.2 Output La$out 7ualifiers
Verte and "ragment shaders cannot have outut la(out =uali"iers.
/eometr( shaders can have outut la(out =uali"iers onl( on the inter"ace =uali"ier out, not on an outut6lock or varia6le declaration.
-he la(out =uali"ier identi"iers "or geometr( shader oututs are
laout-6uali/ier-i!
points
line$strip
triangle$strip
max$vertices=integer-constant
*ne declaration can declare either a rimitive t(e points,line$strip, ortriangle$stripC, or
max$vertices, or 6oth. 5se max$vertices to declare the maimum num6er o" vertices this shader will
ever emit in a single eecution. )or eamle,
la&outtriangle_strip/ max_verti%es . A) out ** order does not matter
la&outmax_verti%es . A) out ** rede%laration o3a&
la&outtriangle_strip) out ** rede%laration o3a&
la&outpoints) out ** error/ %ontradi%ts triangle_strip
la&outmax_verti%es . 4A) out ** error/ %ontradi%ts A
these will esta6lish that all oututs "rom the geometr( shader are triangles and at most #7 vertices will 6e
emitted 6( the shader. It is an error "or the maimum num6er o" vertices to 6e greater than
gl$'axGeometr!OutputUertices.
All geometr( shader outut la(out declarations in a rogram must declare the same la(out and same value
"or max$vertices. -here must 6e at least one geometr( outut la(out declaration somewhere in a
rogram, 6ut not all geometr( shaders comilation unitsC are re=uired to declare it.
#.".3." 4niform -loc0 La$out 7ualifiers
0a(out =uali"iers can 6e used "or uni"orm 6locks, 6ut not "or non;6lock uni"orm declarations. -he la(out
=uali"ier identi"iers "or uni"orm 6locks are
laout-6uali/ier-i!
sared
packed
std1-
row$maJor
column$maJor
-
8/12/2019 GLSL Specification 1.50.11
47/125
4 #aria$%es and &'pes
De"ault la(outs are esta6lished at glo6al scoe "or uni"orm 6locks as
la&outlaout-6uali/ier-i!-list) uniform
Hhen this is done, the revious de"ault =uali"ication is "irst inherited and then overridden as er the
override rules listed 6elow "or each =uali"ier listed in the declaration. -he result 6ecomes the new de"ault
=uali"ication scoed to su6se=uent uni"orm 6lock de"initions.
-he initial state o" comilation is as i" the "ollowing were declared:
la&outs5ared/ %olumn_ma0or) uniform
+licitl( declaring this in a shader will return de"aults 6ack to their initial state.
5ni"orm 6locks can 6e declared with otional la(out =uali"iers, and so can their individual mem6er
declarations. 'uch 6lock la(out =uali"ication is scoed onl( to the content o" the 6lock. As with glo6al
la(out declarations, 6lock la(out =uali"ication "irst inherits "rom the current de"ault =uali"ication and then
overrides it. 'imilarl(, individual mem6er la(out =uali"ication is scoed ?ust to the mem6er declaration,
and inherits "rom and overrides the 6locks =uali"ication.
-hes'are! =uali"ier overrides onl( thest!140 and)ac3e! =uali"iersG other =uali"iers are inherited. -he
comilerlinker will ensure that multile rograms and rogramma6le stages containing this de"inition
will share the same memor( la(out "or this 6lock, as long as the( also matched in their ro_ma;or andor
column_ma;or =uali"ications. -his allows use o" the same 6u""er to 6ack the same 6lock de"inition across
di""erent rograms.
-he)ac3e! =uali"ier overrides onl(st!140 ands'are!G other =uali"iers are inherited. Hhen)ac3e! is
used, no sharea6le la(out is guaranteed. -he comiler and linker can otimi@e memor( use 6ased on what
varia6les activel( get used and on other criteria. *""sets must 6e =ueried, as there is no other wa( o"
guaranteeing where and whichC varia6les reside within the 6lock. Attemts to share a acked uni"orm
6lock across rograms or stages will generall( "ail. owever, imlementations ma( aid alication
management o" acked 6locks 6( using canonical la(outs "or acked 6locks.-hest!140 =uali"ier overrides onl( the)ac3e! ands'are! =uali"iersG other =uali"iers are inherited. -he
la(out is elicitl( determined 6( this, as descri6ed in section .11.& under 'tandard 5ni"orm Block
0a(out o" the *en/0 /rahics '(stem 'eci"ication. ence, as ins'are! a6ove, the resulting la(out is
sharea6le across rograms.
0a(out =uali"iers on mem6er declarations cannot use thes'are!,)ac3e!,orst!140 =uali"iers. -hese can
onl( 6e used at glo6al scoe or on a 6lock declaration.
-he ro_ma;or =uali"ier overrides onl( the column_ma;or=uali"ierG other =uali"iers are inherited. It onl(
a""ects the la(out o" matrices. +lements within a matri row will 6e contiguous in memor(.
-he column_ma;or =uali"ier overrides onl( the ro_ma;or=uali"ierG other =uali"iers are inherited. It onl(
a""ects the la(out o" matrices. +lements within a matri column will 6e contiguous in memor(.
Hhen multile arguments are listed in a la!out declaration, the a""ect will 6e the same as i" the( were
declared one at a time, in order "rom le"t to right, each in turn inheriting "rom and overriding the result
"rom the revious =uali"ication.
41
-
8/12/2019 GLSL Specification 1.50.11
48/125
4 #aria$%es and &'pes
)or eamle
la&outro:_ma0or/ %olumn_ma0or)
results in the =uali"ication 6eing column_ma;or. *ther eamles:
la&outs5ared/ ro:_ma0or) uniform ** default is no: s5ared and ro:_ma0or
la&outstd'1A) uniform Transform ; ** la&out of t5is $lo%3 is std'1A
mat1 9' ** ro:_ma0or
la&out%olumn_ma0or) mat1 92 ** %olumn ma0or
mat4 N' ** ro:_ma0or
-
8/12/2019 GLSL Specification 1.50.11
49/125
4 #aria$%es and &'pes
-he t(e and resence o" the interolation =uali"iers and storage =uali"iers and invariant =uali"iers o"
varia6les with the same name declared in all linked shaders must match, otherwise the link command will
"ail.
#.".9.1 8edeclaring -uiltin Interpolation *ariables in the Compatibilit$ ,rofile
-he "ollowing redeclared varia6les can 6e redeclared with an interolation =uali"ier when using the
comati6ilit( ro"ile:
Verte and geometr( languages:
gl_FrontColor
gl_-a%3Color
gl_FrontSe%ondar&Color
gl_-a%3Se%ondar&Color
)ragment language:
gl_Colorgl_Se%ondar&Color
)or eamle,
in ve%1 gl_Color ** prede%lared $& t5e fragment language
flat in ve%1 gl_Color ** rede%lared $& user to $e flat
flat in ve%1 gl_FrontColor ** input to geometr& s5ader/ no +gl_in>?,
flat out ve%1 gl_FrontColor ** output from geometr& s5ader
Inut or outut instance names on 6locks are not used when redeclaring 6uilt;in varia6les.
I"gl_olor is redeclared with an interolation =uali"ier, thengl_$rontolor andgl_
-
8/12/2019 GLSL Specification 1.50.11
50/125
4 #aria$%es and &'pes
2arameter =uali"iers are discussed in more detail in 'ection #.1.1 E)unction !alling !onventionsF.
#. ,recision and ,recision 7ualifiers
2recision =uali"iers are added "or code orta6ilit( with *en/0 +', not "or "unctionalit(. -he( have the
same s(nta as in *en/0 +', as descri6ed 6elow, 6ut the( have no semantic meaning, which includes no
e""ect on the recision used to store or oerate on varia6les.
I" an etension adds in the same semantics and "unctionalit( in the *en/0 +' .7 seci"ication "or
recision =uali"iers, then the etension is allowed to reuse the ke(words 6elow "or that urose.
#..1 8ange and ,recision
'ection num6er reserved "or "uture use.
#..2 ,recision 7ualifiersAn( "loating oint or an( integer declaration can have the t(e receded 6( one o" these recision
=uali"iers:
7ualifier 5eaning
igp
-
8/12/2019 GLSL Specification 1.50.11
51/125
4 #aria$%es and &'pes
pre%ision pre%ision=ualifier t&pe
can 6e used to esta6lish a de"ault recision =uali"ier. -he t!pe"ield can 6e eitherintor float, and the
)recision-6uali/ier can 6e lowp, mediump, or igp. An( other t(es or =uali"iers will result in an error.I" t)eis float, the directive alies to non;recision;=uali"ied "loating oint t(e scalar, vector, and
matriC declarations. I" t)eis int, the directive alies to all non;recision;=uali"ied integer t(e scalar,
vector, signed, and unsignedC declarations. -his includes glo6al varia6le declarations, "unction return
declarations, "unction arameter declarations, and local varia6le declarations.
-
8/12/2019 GLSL Specification 1.50.11
52/125
4 #aria$%es and &'pes
#.!.1 The Invariant 7ualifier
-o ensure that a articular outut varia6le is invariant, it is necessar( to use the invariant=uali"ier. It can
either 6e used to =uali"( a reviousl( declared varia6le as 6eing invariant
invariant gl_@osition ** ma3e existing gl_@osition $e invariant
out ve%4 Color
invariant Color ** ma3e existing Color $e invariant
or as art o" a declaration when a varia6le is declared
invariant %entroid out ve%4 Color
-he invariant =uali"ier must aear 6e"ore an( interolation =uali"iers or storage =uali"iers when
com6ined with a declaration. *nl( varia6les outut "rom a shader including those that are then inut to a
su6se=uent shaderC can 6e candidates "or invariance. -his includes user;de"ined outut varia6les and the
6uilt;in outut varia6les. )or varia6les leaving one shader and coming into another shader, the invariantke(word has to 6e used in 6oth shaders, or a link error will result.
Inut or outut instance names on 6locks are not used when redeclaring 6uilt;in varia6les.
-he invariantke(word can 6e "ollowed 6( a comma searated list o" reviousl( declared identi"iers. All
uses o" invariantmust 6e at the glo6al scoe, and 6e"ore an( use o" the varia6les 6eing declared as
invariant.
-o guarantee invariance o" a articular outut varia6le across two rograms, the "ollowing must also 6e
true:
-he outut varia6le is declared as invariant in 6oth rograms.
-he same values must 6e inut to all shader inut varia6les consumed 6( eressions and "low control
contri6uting to the value assigned to the outut varia6le.
-he teture "ormats, teel values, and teture "iltering are set the same wa( "or an( teture "unction
calls contri6uting to the value o" the outut varia6le.
All inut values are all oerated on in the same wa(. All oerations in the consuming eressions and
an( intermediate eressions must 6e the same, with the same order o" oerands and same
associativit(, to give the same order o" evaluation. Intermediate varia6les and "unctions must 6e
declared as the same t(e with the same elicit or imlicit recision =uali"iers. An( control "low
a""ecting the outut value must 6e the same, and an( eressions consumed to determine this control
"low must also "ollow these invariance rules.
All the data "low and control "low leading to setting the invariant outut varia6le reside in a single
comilation unit.
+ssentiall(, all the data "low and control "low leading to an invariant outut must match.
Initiall(, 6( de"ault, all outut varia6les are allowed to 6e variant. -o "orce all outut varia6les to 6e
invariant, use the ragma
46
-
8/12/2019 GLSL Specification 1.50.11
53/125
4 #aria$%es and &'pes
#pragma ST!L invariantall)
6e"ore all declarations in a shader. I" this ragma is used a"ter the declaration o" an( varia6les or
"unctions, then the set o" oututs that 6ehave as invariant is unde"ined. It is an error to use this ragma ina "ragment shader.
/enerall(, invariance is ensured at the cost o" "lei6ilit( in otimi@ation, so er"ormance can 6e degraded
6( use o" invariance. ence, use o" this ragma is intended as a de6ug aid, to avoid individuall( declaring
all outut varia6les as invariant.
#.!.2 Invariance of Constant '+pressions
Invariance must 6e guaranteed "or constant eressions. A articular constant eression must evaluate to
the same result i" it aears again in the same shader or a di""erent shader. -his includes the same
eression aearing two shaders o" the same language or shaders o" two di""erent languages.
!onstant eressions must evaluate to the same result when oerated on as alread( descri6ed a6ove "or
invariant varia6les.
#. Order of 7ualification
Hhen multile =uali"ications are resent, the( must "ollow a strict order. -his order is as "ollows.
invariant-6uali/ier inter)olation-6uali/ier storage-6uali/ier )recision-6uali/ier
storage-6uali/ier )arameter-6uali/ier )recision-6uali/ier
47
-
8/12/2019 GLSL Specification 1.50.11
54/125
Operators and '+pressions
.1 Operators
-he *en/0 'hading 0anguage has the "ollowing oerators.
,recedence Operator Class Operators ssociativit$
1 highestC arenthetical grouing "
Right to 0e"t
& multilicative 0 2 0e"t to Right
$ additive / * 0e"t to Right
# 6it;wise shi"t 33 44 0e"t to Right
3 relational 3 4 3= 4= 0e"t to Right
4 e=ualit( == >= 0e"t to Right
9 6it;wise and ; 0e"t to Right
17 6it;wise eclusive or 9 0e"t to Right
11 6it;wise inclusive or : 0e"t to Right
1 logical and ;; 0e"t to Right
1% logical eclusive or 99 0e"t to Right
1& logical inclusive or : : 0e"t to Right
1$ selection ? Right to 0e"t
1#
Assignmentarithmetic assignments
=
/= *== 0=
2= 33= 44=;= 9= :=
Right to 0e"t
13 lowestC se=uence , 0e"t to Right
-here is no address;o" oerator nor a dere"erence oerator. -here is no t(ecast oeratorG constructors
are used instead.
4
-
8/12/2019 GLSL Specification 1.50.11
55/125
5 Operators and ()pressions
.2 rra$ Operations
-hese are now descri6ed in 'ection $.3 E'tructure and Arra( *erationsF.
." %unction Calls
I" a "unction returns a value, then a call to that "unction ma( 6e used as an eression, whose t(e will 6e
the t(e that was used to declare or de"ine the "unction.
)unction de"initions and calling conventions are discussed in 'ection #.1 E)unction De"initionsF .
.# Constructors
!onstructors use the "unction call s(nta, where the "unction name is a t(e, and the call makes an o6?ect
o" that t(e. !onstructors are used the same wa( in 6oth initiali@ers and eressions. 'ee 'ection 9
E'hading 0anguage /rammarF "or details.C -he arameters are used to initiali@e the constructed value.
!onstructors can 6e used to re=uest a data t(e conversion to change "rom one scalar t(e to another
scalar t(e, or to 6uild larger t(es out o" smaller t(es, or to reduce a larger t(e to a smaller t(e.
In general, constructors are not 6uilt;in "unctions with redetermined rotot(es. )or arra(s and
structures, there must 6e eactl( one argument in the constructor "or each element or "ield. )or the other
t(es, the arguments must rovide a su""icient num6er o" comonents to er"orm the initiali@ation, and it
is an error to include so man( arguments that the( cannot all 6e used. Detailed rules "ollow. -he
rotot(es actuall( listed 6elow are merel( a su6set o" eamles.
.#.1 Conversion and Scalar Constructors
!onverting 6etween scalar t(es is done as the "ollowing rotot(es indicate:
int$ool) ** %onverts a -oolean value to an int
intfloat) ** %onverts a float value to an int
float$ool) ** %onverts a -oolean value to a float
floatint) ** %onverts a signed integer value to a float
$oolfloat) ** %onverts a float value to a -oolean
$oolint) ** %onverts a signed integer value to a -oolean
uint$ool) ** %onverts a -oolean value to an unsigned integer
uintfloat) ** %onverts a float value to an unsigned integer
uintint) ** %onverts a signed integer value to an unsigned integer
intuint) ** %onverts an unsigned integer to a signed integer
$ooluint) ** %onverts an unsigned integer value to a -oolean value
floatuint) ** %onverts an unsigned integer value to a float value
Hhen constructors are used to convert a floatto an int or uint, the "ractional art o" the "loating;oint
value is droed. It is unde"ined to convert a negative "loating oint value to an uint.
Hhen a constructor is used to convert an int, uint, or a floatto a bool, 7 and 7.7 are converted to false,
and non;@ero values are converted to true. Hhen a constructor is used to convert a boolto an int, uint,
or float, falseis converted to 7 or 7.7, and trueis converted to 1 or 1.7.
4!
-
8/12/2019 GLSL Specification 1.50.11
56/125
5 Operators and ()pressions
-he constructor int"uintreserves the 6it attern in the argument, which will change the arguments
value i" its sign 6it is set. -he constructor uint"intreserves the 6it attern in the argument, which will
change its value i" it is negative.
Identit( constructors, like floatfloatC are also legal, 6ut o" little use.
'calar constructors with non;scalar arameters can 6e used to take the "irst element "rom a non;scalar.
)or eamle, the constructor floatvecFC will select the "irst comonent o" the vecFarameter.
.#.2 *ector and 5atri+ Constructors
!onstructors can 6e used to create vectors or matrices "rom a set o" scalars, vectors, or matrices. -his
includes the a6ilit( to shorten vectors.
I" there is a single scalar arameter to a vector constructor, it is used to initiali@e all comonents o" the
constructed vector to that scalars value. I" there is a single scalar arameter to a matri constructor, it is
used to initiali@e all the comonents on the matris diagonal, with the remaining comonents initiali@ed
to 7.7.
I" a vector is constructed "rom multile scalars, one or more vectors, or one or more matrices, or a miture
o" these, the vectors comonents will 6e constructed in order "rom the comonents o" the arguments. -he
arguments will 6e consumed le"t to right, and each argument will have all its comonents consumed, in
order, 6e"ore an( comonents "rom the net argument are consumed. 'imilarl( "or constructing a matri
"rom multile scalars or vectors, or a miture o" these. 8atri comonents will 6e constructed and
consumed in column ma?or order. In these cases, there must 6e enough comonents rovided in the
arguments to rovide an initiali@er "or ever( comonent in the constructed value. It is an error to rovide
etra arguments 6e(ond this last used argument.
I" a matri is constructed "rom a matri, then each comonent column i,row;C in the result that has a
corresonding comonent column i,row;C in the argument will 6e initiali@ed "rom there. All other
comonents will 6e initiali@ed to the identit( matri. I" a matri argument is given to a matri constructor,
it is an error to have an( other arguments.
I" the 6asic t(e bool, int, or floatC o" a arameter to a constructor does not match the 6asic t(e o" the
o6?ect 6eing constructed, the scalar construction rules a6oveC are used to convert the arameters.
5"
-
8/12/2019 GLSL Specification 1.50.11
57/125
5 Operators and ()pressions
'ome use"ul vector constructors are as "ollows:
ve%4float) ** initiali"es ea%5 %omponent of t5e ve%4 :it5 t5e float
ve%1ive%1) ** ma3es a ve%1 :it5 %omponent:ise %onversion
ve%1mat2) ** t5e ve%1 is %olumn A follo:ed $& %olumn '
ve%2float/ float) ** initiali"es a ve%2 :it5 2 floats
ive%4int/ int/ int) ** initiali"es an ive%4 :it5 4 ints
$ve%1int/ int/ float/ float) ** uses 1 -oolean %onversions
ve%2ve%4) ** drops t5e t5ird %omponent of a ve%4
ve%4ve%1) ** drops t5e fourt5 %omponent of a ve%1
ve%4ve%2/ float) ** ve%46x . ve%26x/ ve%46& . ve%26&/ ve%46" . float
ve%4float/ ve%2) ** ve%46x . float/ ve%46& . ve%26x/ ve%46" . ve%26&
ve%1ve%4/ float)
ve%1float/ ve%4)
ve%1ve%2/ ve%2)
'ome eamles o" these are:
ve%1 %olor . ve%1A6A/ '6A/ A6A/ '6A)
ve%1 rg$a . ve%1'6A) ** sets ea%5 %omponent to '6A
ve%4 rg$ . ve%4%olor) ** drop t5e 1t5 %omponent
-o initiali@e the diagonal o" a matri with all other elements set to @ero:
mat2float)
mat4float)
mat1float)
-hat is, result"i#";#is set to the "loat argument "or all i = ; and set to 7 "or all i;.
51
-
8/12/2019 GLSL Specification 1.50.11
58/125
5 Operators and ()pressions
-o initiali@e a matri 6( seci"(ing vectors or scalars, the comonents are assigned to the matri elements
in column;ma?or order.
mat2ve%2/ ve%2) ** one %olumn per argument
mat4ve%4/ ve%4/ ve%4) ** one %olumn per argument
mat1ve%1/ ve%1/ ve%1/ ve%1) ** one %olumn per argument
mat4x2ve%2/ ve%2/ ve%2) ** one %olumn per argument
mat2float/ float/ ** first %olumn
float/ float) ** se%ond %olumn
mat4float/ float/ float/ ** first %olumn
float/ float/ float/ ** se%ond %olumn
float/ float/ float) ** t5ird %olumn
mat1float/ float/ float/ float/ ** first %olumn
float/ float/ float/ float/ ** se%ond %olumn
float/ float/ float/ float/ ** t5ird %olumn float/ float/ float/ float) ** fourt5 %olumn
mat2x4ve%2/ float/ ** first %olumn
ve%2/ float) ** se%ond %olumn
A wide range o" other ossi6ilities eist, to construct a matri "rom vectors and scalars, as long as enough
comonents are resent to initiali@e the matri. -o construct a matri "rom a matri:
mat4x4mat1x1) ** ta3es t5e upperleft 4x4 of t5e mat1x1
mat2x4mat1x2) ** ta3es t5e upperleft 2x2 of t5e mat1x1/ last ro: is A/A
mat1x1mat4x4) ** puts t5e mat4x4 in t5e upperleft/ sets t5e lo:er rig5t
** %omponent to '/ and t5e rest to A
.#." Structure Constructors
*nce a structure is de"ined, and its t(e is given a name, a constructor is availa6le with the same name to
construct instances o" that structure. )or eamle:
stru%t lig5t ;
float intensit&
ve%4 position
-
8/12/2019 GLSL Specification 1.50.11
59/125
5 Operators and ()pressions
.#.# rra$ Constructors
Arra( t(es can also 6e used as constructor names, which can then 6e used in eressions or initiali@ers.
)or eamle,
%onst float %>4? . float>4?76A/ D62/ '6')
%onst float d>4? . float>?76A/ D62/ '6')
float g
666
float a>7? . float>7?g/ '/ g/ 264/ g)
float $>4?
$ . float>4?g/ g H '6A/ g H 26A)
-here must 6e eactl( the same num6er o" arguments as the si@e o" the arra( 6eing constructed. I" no si@e
is resent in the constructor, then the arra( is elicitl( si@ed to the num6er o" arguments rovided. -hearguments are assigned in order, starting at element 7, to the elements o" the constructed arra(. +ach
argument must 6e the same t(e as the element t(e o" the arra(, or 6e a t(e that can 6e converted to the
element t(e o" the arra( according to 'ection &.1.17EImlicit !onversions.F
. *ector Components
-he names o" the comonents o" a vector are denoted 6( a single letter. As a notational convenience,
several letters are associated with each comonent 6ased on common usage o" osition, color or teture
coordinate vectors. -he individual comonents o" a vector can 6e selected 6( "ollowing the varia6le
name with eriod .C and then the comonent name.
-he comonent names suorted are:
>x, , , ? 5se"ul when accessing vectors that reresent oints or normals
>r, g, , a? 5se"ul when accessing vectors that reresent colors
>s, t, ), 6? 5se"ul when accessing vectors that reresent teture coordinates
-he comonent namesx, r, andsare, "or eamle, s(non(ms "or the same "irstC comonent in a vector.
-
8/12/2019 GLSL Specification 1.50.11
60/125
-
8/12/2019 GLSL Specification 1.50.11
61/125
5 Operators and ()pressions
.! 5atri+ Components
-he comonents o" a matri can 6e accessed using arra( su6scriting s(nta. Al(ing a single su6scrit
to a matri treats the matri as an arra( o" column vectors, and selects a single column, whose t(e is avector o" the same si@e as the matri. -he le"tmost column is column 7. A second su6scrit would then
oerate on the resulting vector, as de"ined earlier "or vectors. ence, two su6scrits select a column and
then a row.
mat1 m
m>'? . ve%126A) ** sets t5e se%ond %olumn to all 26A
m>A?>A? . '6A ** sets t5e upper left element to '6A
m>2?>4? . 26A ** sets t5e 1t5 element of t5e t5ird %olumn to 26A
Behavior is unde"ined when accessing a comonent outside the 6ounds o" a matri with a non;constant
eression. It is an error to access a matri with a constant eression that is outside the 6ounds o" the
matri.
. Structure and rra$ Operations
-he "ields o" a structure and the lengtmethod o" an arra( are selected using the eriod . C.
In total, onl( the "ollowing oerators are allowed to oerate on arra(s and structures as whole entities:
"ield or method selector .
e=ualit( == >=
assignment =
indeing arra(s onl(C Y Z
-he e=ualit( oerators and assignment oerator are onl( allowed i" the two oerands are same si@e andt(e. 'tructure t(es must 6e o" the same declared structure. Both arra( oerands must 6e elicitl(
si@ed. Hhen using the e=ualit( oerators, two structures are e=ual i" and onl( i" all the "ields are
comonent;wise e=ual, and two arra(s are e=ual i" and onl( i" all the elements are element;wise e=ual.
Arra( elements are accessed using the arra( su6scrit oerator 5 6C. An eamle o" accessing an arra(
element is
diffuseColor H. lig5tIntensit&>4? J NdotL
Arra( indices start at @ero. Arra( elements are accessed using an eression whose t(e is int or uint.
Behavior is unde"ined i" a shader su6scrits an arra( with an inde less than 7 or greater than or e=ual to
the si@e the arra( was declared with.
55
-
8/12/2019 GLSL Specification 1.50.11
62/125
5 Operators and ()pressions
Arra(s can also 6e accessed with the method oerator . C and the lengt method to =uer( the si@e o" the
arra(:
lig5tIntensit&6lengt5) ** return t5e si"e of t5e arra&
.3 ssignments
Assignments o" values to varia6le names are done with the assignment oerator = C:
lvalueexpression . rvalueexpression
-he lvalue-ex)ression evaluates to an l;value. -he assignment oerator stores the value o" rvalue-
ex)ressionintothel;value and returns an r;value with the t(e and recision o" lvalue-ex)ression. -he
lvalue-ex)ressionand rvalue-ex)ressionmust have the same t(e, or the eression must have a t(e in
the ta6le in 'ection &.1.17EImlicit !onversionsF that converts to the t(e o" lvalue-ex)ression, in which
case an imlicit conversion will 6e done on the rvalue-ex)ression6e"ore the assignment is done. An(
other desired t(e;conversions must 6e seci"ied elicitl( via a constructor. 0;values must 6e writa6le.Varia6les that are 6uilt;in t(es, entire structures or arra(s, structure "ields, l;values with the "ield selector
. C alied to select comonents or swi@@les without reeated "ields, l;values within arentheses, and l;
values dere"erenced with the arra( su6scrit oerator 5 6 C are all l;values. *ther 6inar( or unar(
eressions, "unction names, swi@@les with reeated "ields, and constants cannot 6e l;values. -he ternar(
oerator ?C is also not allowed as an l;value.
+ressions on the le"t o" an assignment are evaluated 6e"ore eressions on the right o" the assignment.
-he other assignment oerators are
add into /=C
su6tract "rom *=C
multil( into =C divide into 0=C
modulus into 2=C
le"t shi"t 6( 33=C
right shi"t 6( 44=C
and into ;=C
inclusive;or into :=C
eclusive;or into 9=C
56
-
8/12/2019 GLSL Specification 1.50.11
63/125
5 Operators and ()pressions
where the general eression
lvalue op. expression
is e=uivalent to
lvalue . lvalue opexpression
where o) is as descri6ed 6elow, and the l;value and eression must satis"( the semantic re=uirements o"
6oth o)and e=uals =C.
Reading a varia6le 6e"ore writing or initiali@ingC it is legal, however the value is unde"ined.
.9 '+pressions
+ressions in the shading language are 6uilt "rom the "ollowing:
!onstants o" t(e bool, int, uint, float, all vector t(es, and all matri t(es.
!onstructors o" all t(es.
Varia6le names o" all t(es.
An arra( name with the length method alied.
'u6scrited arra( names.
)unction calls that return values.
!omonent "ield selectors and arra( su6scrit results.
2arenthesi@ed eression. An( eression can 6e arenthesi@ed. 2arentheses can 6e used to grou
oerations. *erations within arentheses are done 6e"ore oerations across arentheses.
-he arithmetic 6inar( oerators add /C, su6tract *C, multil( C, and divide 0C oerate on integer and
"loating;oint scalars, vectors, and matrices. I" one oerand is "loating;oint 6ased and the other is
not, then the conversions "rom 'ection &.1.17EImlicit !onversionsF are alied to the non;"loating;
oint;6ased oerand. I" the oerands are integer t(es, the( must 6oth 6e signed or 6oth 6e unsigned.
All arithmetic 6inar( oerators result in the same "undamental t(e signed integer, unsigned integer,
or "loating;ointC as the oerands the( oerate on, a"ter oerand t(e conversion. A"ter conversion,
the "ollowing cases are valid
-he two oerands are scalars. In this case the oeration is alied, resulting in a scalar.
*ne oerand is a scalar, and the other is a vector or matri. In this case, the scalar oeration is
alied indeendentl( to each comonent o" the vector or matri, resulting in the same si@e vector
or matri.
-he two oerands are vectors o" the same si@e. In this case, the oeration is done comonent;wise
resulting in the same si@e vector.
-he oerator is add /C, su6tract *C, or divide 0C, and the oerands are matrices with the same
num6er o" rows and the same num6er o" columns. In this case, the oeration is done comonent;
wise resulting in the same si@e matri.
57
-
8/12/2019 GLSL Specification 1.50.11
64/125
-
8/12/2019 GLSL Specification 1.50.11
65/125
5 Operators and ()pressions
-he logical 6inar( oerators and ;;C, or : : C, and eclusive or 99C oerate onl( on two Boolean
eressions and result in a Boolean eression. And ;;C will onl( evaluate the right hand oerand
i" the le"t hand oerand evaluated to true. *r : :C will onl( evaluate the right hand oerand i" the le"t
hand oerand evaluated to false. +clusive or 99C will alwa(s evaluate 6oth oerands.
-he logical unar( oerator not >C. It oerates onl( on a Boolean eression and results in a Boolean
eression. -o oerate on a vector, use the 6uilt;in "unction not.
-he se=uence ,C oerator that oerates on eressions 6( returning the t(e and value o" the right;
most eression in a comma searated list o" eressions. All eressions are evaluated, in order,
"rom le"t to right.
-he ternar( selection oerator ?C. It oerates on three eressions ex)1ex)2?ex)@C. -his
oerator evaluates the "irst eression, which must result in a scalar Boolean. I" the result is true, it
selects to evaluate the second eression, otherwise it selects to evaluate the third eression. *nl(
one o" the second and third eressions is evaluated. -he second and third eressions can 6e an(
t(e, as long their t(es match, or there is a conversion in 'ection &.1.17EImlicit !onversionsF that
can 6e alied to one o" the eressions to make their t(es match. -his resulting matching t(e is thet(e o" the entire eression.
-he ones comlement oerator
-
8/12/2019 GLSL Specification 1.50.11
66/125
5 Operators and ()pressions
.16 *ector and 5atri+ Operations
Hith a "ew ecetions, oerations are comonent;wise. 5suall(, when an oerator oerates on a vector or
matri, it is oerating indeendentl( on each comonent o" the vector or matri, in a comonent;wise"ashion. )or eamle,
ve%4 v/ u
float f
v . u H f
will 6e e=uivalent to
v6x . u6x H f
v6& . u6& H f
v6" . u6" H f
And
ve%4 v/ u/ :
: . v H u
will 6e e=uivalent to
:6x . v6x H u6x
:6& . v6& H u6&
:6" . v6" H u6"
and likewise "or most oerators and all integer and "loating oint vector and matri t(es. -he ecetions
are matri multilied 6( vector, vector multilied 6( matri, and matri multilied 6( matri. -hese do
not oerate comonent;wise, 6ut rather er"orm the correct linear alge6raic multil(.
ve%4 v/ u
mat4 m
u . v J m
is e=uivalent to
u6x . dotv/ m>A?) ** m>A? is t5e left %olumn of m
u6& . dotv/ m>'?) ** dota/$) is t5e inner dot) produ%t of a and $
u6" . dotv/ m>2?)
And
u . m J v
is e=uivalent to
u6x . m>A?6x J v6x H m>'?6x J v6& H m>2?6x J v6"
u6& . m>A?6& J v6x H m>'?6& J v6& H m>2?6& J v6"
u6" . m>A?6" J v6x H m>'?6" J v6& H m>2?6" J v6"
6"
-
8/12/2019 GLSL Specification 1.50.11
67/125
5 Operators and ()pressions
And
mat4 m/ n/ r
r . m J n
is e=uivalent to
r>A?6x . m>A?6x J n>A?6x H m>'?6x J n>A?6& H m>2?6x J n>A?6"
r>'?6x . m>A?6x J n>'?6x H m>'?6x J n>'?6& H m>2?6x J n>'?6"
r>2?6x . m>A?6x J n>2?6x H m>'?6x J n>2?6& H m>2?6x J n>2?6"
r>A?6& . m>A?6& J n>A?6x H m>'?6& J n>A?6& H m>2?6& J n>A?6"
r>'?6& . m>A?6& J n>'?6x H m>'?6& J n>'?6& H m>2?6& J n>'?6"
r>2?6& . m>A?6& J n>2?6x H m>'?6& J n>2?6& H m>2?6& J n>2?6"
r>A?6" . m>A?6" J n>A?6x H m>'?6" J n>A?6& H m>2?6" J n>A?6"
r>'?6" . m>A?6" J n>'?6x H m>'?6" J n>'?6& H m>2?6" J n>'?6"
r>2?6" . m>A?6" J n>2?6x H m>'?6" J n>2?6& H m>2?6" J n>2?6"
and similarl( "or other si@es o" vectors and matrices.
61
-
8/12/2019 GLSL Specification 1.50.11
68/125
! Statements and Structure
-he "undamental 6uilding 6locks o" the *en/0 'hading 0anguage are:
statements and declarations
"unction de"initions
selection if*else and switc*case*default
iteration"for, wile, and do*wile
?ums"discard, return, break, and continueC
-he overall structure o" a shader is as "ollows
translation-unit:
gloal-!eclaration
translation-unit gloal-!eclaration
gloal-!eclaration:
/unction-!e/inition
!eclaration
-hat is, a shader is a se=uence o" declarations and "unction 6odies. )unction 6odies are de"ined as
/unction-!e/inition:
/unction-)rotot)e > statement-list ?
statement-list:
statement
statement-list statement
statement:
com)oun!-statement
sim)le-statement
!url( 6races are used to grou se=uences o" statements into comound statements.
com)oun!-statement:
> statement-list ?sim)le-statement:
!eclaration-statement
ex)ression-statement
selection-statement
62
-
8/12/2019 GLSL Specification 1.50.11
69/125
6 State*ents and Structure
iteration-statement
;um)-statement
'imle declaration, eression, and ?um statements end in a semi;colon.
-his a6ove is slightl( simli"ied, and the comlete grammar seci"ied in 'ection 9 E'hading 0anguage
/rammarF should 6e used as the de"initive seci"ication.
Declarations and eressions have alread( 6een discussed.
!.1 %unction )efinitions
As indicated 6( the grammar a6ove, a valid shader is a se=uence o" glo6al declarations and "unction
de"initions. A "unction is declared as the "ollowing eamle shows:
** protot&pe
returnT&pe fun%tionName t&peA argA/ t&pe' arg'/ 666/ t&pen argn)
and a "unction is de"ined like
** definition
returnT&pe fun%tionName t&peA argA/ t&pe' arg'/ 666/ t&pen argn)
;
** do some %omputation
return returnValue
gl_9axTextureCoords?
**
** %ompati$ilit& profile onl&
**
uniform float gl_NormalS%ale
**
** %ompati$ilit& profile onl&
**
uniform ve%1 gl_Clip@lane>gl_9axClip@lanes?
**
** %ompati$ilit& profile onl&
**
stru%t gl_@oint@arameters ;
float si"e
float si"e9in
float si"e9ax
float fadeT5res5oldSi"e
float distan%eConstant8ttenuation
float distan%eLinear8ttenuation
float distan%eMuadrati%8ttenuation
-
8/12/2019 GLSL Specification 1.50.11
83/125
7 Bui%t+in #aria$%es
**
** %ompati$ilit& profile onl&
**
stru%t gl_Lig5tSour%e@arameters ;
ve%1 am$ient ** 8%li
ve%1 diffuse ** %li
ve%1 spe%ular ** S%li
ve%1 position ** @pli
ve%1 5alfVe%tor ** erived( Gi
ve%4 spotire%tion ** Sdli
float spotExponent ** Srli
float spotCutoff ** Crli
** range( >A6A/A6A?/ 'A6A)
float spotCosCutoff ** erived( %osCrli)
** range( >'6A/A6A?/'6A)
float %onstant8ttenuation ** A
float linear8ttenuation ** '
float =uadrati%8ttenuation** 2
gl_9axLig5ts?
stru%t gl_Lig5t9odel@arameters ;
ve%1 am$ient ** 8%s
-
8/12/2019 GLSL Specification 1.50.11
84/125
7 Bui%t+in #aria$%es
**
** %ompati$ilit& profile onl&**
uniform ve%1 gl_TextureEnvColor>gl_9axTexturenits?
uniform ve%1 gl_E&e@laneS>gl_9axTextureCoords?
uniform ve%1 gl_E&e@laneT>gl_9axTextureCoords?
uniform ve%1 gl_E&e@laneR>gl_9axTextureCoords?
uniform ve%1 gl_E&e@laneM>gl_9axTextureCoords?
uniform ve%1 gl_O$0e%t@laneS>gl_9axTextureCoords?
uniform ve%1 gl_O$0e%t@laneT>gl_9axTextureCoords?
uniform ve%1 gl_O$0e%t@laneR>gl_9axTextureCoords?
uniform ve%1 gl_O$0e%t@laneM>gl_9axTextureCoords?
**
** %ompati$ilit& profile onl&
**
stru%t gl_Fog@arameters ;
ve%1 %olor
float densit&
float start
float end
float s%ale ** erived( '6A * end start)
-
8/12/2019 GLSL Specification 1.50.11
85/125
-
8/12/2019 GLSL Specification 1.50.11
86/125
7 Bui%t+in #aria$%es
in float gl_FogFragCoord
in ve%1 gl_TexCoord>?
in ve%1 gl_Color
in ve%1 gl_Se%ondar&Color
-he values ingl_olorandgl_+econ!arolorwill 6e derived automaticall( 6( the s(stem "rom
gl_$rontolor, gl_
-
8/12/2019 GLSL Specification 1.50.11
87/125
3 -uiltin %unctions
-he *en/0 'hading 0anguage de"ines an assortment o" 6uilt;in convenience "unctions "or