Upload
pablo-romero
View
214
Download
0
Embed Size (px)
Citation preview
8/12/2019 GLSLangSpec.4.30.6
1/196
The OpenGLShading Language
Language Version: 4.30Document Revision: 63-Aug-2012
Editor: John Kessenich, LunarG
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
8/12/2019 GLSLangSpec.4.30.6
2/196
Copyright (c) 2008-2012 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 +,/II/ versions o! this speci!ic"tion in "ny !"shion& provie th"t, CARG 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/& penKG3& pen6G& penA7& pen34 3 "n pen "re tr"e%"rs o!the Khronos Group Inc. C44A/A is " tr"e%"r o! 3ony Co%puter ntert"in%ent Inc. use #yper%ission #y Khronos. penG4 "n pen4 "re registere tr"e%"rs "n the penG4 3 logo is "tr"e%"r o! 3ilicon Gr"phics Inc. use #y per%ission #y Khronos. All other prouct n"%es& tr"e%"rs&"n9or co%p"ny n"%es "re use solely !or ienti!ic"tion "n #elong to their respective o$ners.
ii
8/12/2019 GLSLangSpec.4.30.6
3/196
Table of Contents1 Introduction.................................................................................................................................1
1.1 Acnowled!"ents................................................................................................................#1.# $han!es................................................................................................................................%1.#.1 &u""ar' o( $han!es (ro" Version ).#*.....................................................................%
1.% +verview..............................................................................................................................1.) Error -andlin!......................................................................................................................1. '/o!ra/hical $onventions.................................................................................................1.0 De/recation..........................................................................................................................
# +verview o( +/enGL &hadin!....................................................................................................0#.1 Verte 2rocessor..................................................................................................................0#.# essellation $ontrol 2rocessor.............................................................................................0#.% essellation Evaluation 2rocessor........................................................................................3#.) Geo"etr' 2rocessor.............................................................................................................3#. 4ra!"ent 2rocessor..............................................................................................................3#.0 $o"/ute 2rocessor...............................................................................................................3
% Basics..........................................................................................................................................5%.1 $haracter &et........................................................................................................................5%.# &ource &trin!s......................................................................................................................5%.% 2re/rocessor.......................................................................................................................1*%.) $o""ents..........................................................................................................................1%. oens................................................................................................................................10%.0 Ke'words............................................................................................................................10%.3 Identi(iers...........................................................................................................................16%.6 De(initions..........................................................................................................................16
%.6.1 &tatic 7se....................................................................................................................15%.6.# 7ni(or" and 8on97ni(or" $ontrol 4low..................................................................15%.6.% D'na"icall' 7ni(or" E/ressions.............................................................................15
) Variales and '/es..................................................................................................................#*).1 Basic '/es........................................................................................................................#*
).1.1 Void............................................................................................................................#)).1.# Booleans.....................................................................................................................#)).1.% Inte!ers.......................................................................................................................#)).1.) 4loatin!92oint Variales.............................................................................................#0).1. Vectors........................................................................................................................#3).1.0 ;atrices......................................................................................................................#3
).1.3 +/a
8/12/2019 GLSLangSpec.4.30.6
4/196
).1.3.% Ato"ic $ounters.................................................................................................#5).1.6 &tructures....................................................................................................................#5).1.5 Arra's.........................................................................................................................%*
).1.1* I"/licit $onversions................................................................................................%)).1.11 Initiali=ers.................................................................................................................%
).# &co/in!...............................................................................................................................%3).% &tora!e >uali(iers...............................................................................................................)*
).%.1 De(ault &tora!e >uali(ier............................................................................................)1).%.# $onstant >uali(ier......................................................................................................)1).%.% $onstant E/ressions.................................................................................................)1).%.) In/ut Variales...........................................................................................................)#).%. 7ni(or" Variales......................................................................................................)).%.0 +ut/ut Variales.........................................................................................................)).%.3 Bu((er Variales.........................................................................................................)3
).%.6 &hared Variales.........................................................................................................)6).%.5 Inter(ace Blocs..........................................................................................................)6
).) La'out >uali(iers................................................................................................................#).).1 In/ut La'out >uali(iers...............................................................................................%
).).1.1 essellation Evaluation In/uts............................................................................).).1.# Geo"etr' &hader In/uts......................................................................................0).).1.% 4ra!"ent &hader In/uts......................................................................................3).).1.) $o"/ute &hader In/uts.......................................................................................6
).).# +ut/ut La'out >uali(iers............................................................................................5).).#.1 essellation $ontrol +ut/uts..............................................................................01).).#.# Geo"etr' +ut/uts...............................................................................................01
).).#.% 4ra!"ent +ut/uts...............................................................................................0%).).% 7ni(or" Variale La'out >uali(iers...........................................................................0)).).) &uroutine 4unction La'out >uali(iers......................................................................0).). 7ni(or" and &hader &tora!e Bloc La'out >uali(iers...............................................00).).0 +/auali(iers....................................................................................3*
). Inter/olation >uali(iers......................................................................................................3#)..1 Redeclarin! Built9in Inter/olation Variales in the $o"/atiilit' 2ro(ile.................3%
).0 2ara"eter >uali(iers...........................................................................................................3)).3 2recision and 2recision >uali(iers.....................................................................................3)
).3.1 Ran!e and 2recision...................................................................................................3)).3.# 2recision >uali(iers....................................................................................................3).3.% De(ault 2recision >uali(iers.......................................................................................30
iv
8/12/2019 GLSLangSpec.4.30.6
5/196
).3.) Availale 2recision >uali(iers....................................................................................33).6 Variance and the Invariant >uali(ier..................................................................................33
).6.1 he Invariant >uali(ier...............................................................................................33
).6.# Invariance o( $onstant E/ressions...........................................................................36).5 he 2recise >uali(ier..........................................................................................................36).1* ;e"or' >uali(iers...........................................................................................................61).11 +rder o( >uali(ication......................................................................................................6)
+/erators and E/ressions........................................................................................................6.1 +/erators............................................................................................................................6.# Arra' +/erations...............................................................................................................60.% 4unction $alls....................................................................................................................60.) $onstructors.......................................................................................................................60
.).1 $onversion and &calar $onstructors..........................................................................60.).# Vector and ;atri $onstructors.................................................................................63
.).% &tructure $onstructors................................................................................................65.).) Arra' $onstructors.....................................................................................................5*
. Vector and &calar $o"/onents and Len!th.......................................................................5*.0 ;atri $o"/onents............................................................................................................5#.3 &tructure and Arra' +/erations..........................................................................................5#.6 Assi!n"ents.......................................................................................................................5%.5 E/ressions........................................................................................................................5).1* Vector and ;atri +/erations..........................................................................................53.11 +ut9o(9Bounds Accesses..................................................................................................56
0 &tate"ents and &tructure...........................................................................................................550.1 4unction De(initions.........................................................................................................1**
0.1.1 4unction $allin! $onventions..................................................................................1*%0.1.# &uroutines...............................................................................................................1*)
0.# &election...........................................................................................................................1*0.% Iteration............................................................................................................................1*00.) Ju"/s................................................................................................................................1*3
3 Built9in Variales....................................................................................................................1*53.1 Built9In Lan!ua!e Variales............................................................................................1*5
3.1.1 $o"/atiilit' 2ro(ile Built9In Lan!ua!e Variales..................................................1163.# $o"/atiilit' 2ro(ile Verte &hader Built9In In/uts.......................................................1#13.% Built9In $onstants............................................................................................................1##
3.%.1 $o"/atiilit' 2ro(ile Built9In $onstants..................................................................1#%
3.) Built9In 7ni(or" &tate.....................................................................................................1#)3.).1 $o"/atiilit' 2ro(ile &tate.......................................................................................1#)
6 Built9in 4unctions...................................................................................................................1#6
v
8/12/2019 GLSLangSpec.4.30.6
6/196
6.1 An!le and ri!ono"etr' 4unctions..................................................................................1#56.# E/onential 4unctions......................................................................................................1%16.% $o""on 4unctions..........................................................................................................1%#
6.) 4loatin!92oint 2ac and 7n/ac 4unctions.....................................................................1%36. Geo"etric 4unctions........................................................................................................1%56.0 ;atri 4unctions..............................................................................................................1)16.3 Vector Relational 4unctions.............................................................................................1)%6.6 Inte!er 4unctions..............................................................................................................1)6.5 eture 4unctions.............................................................................................................1)3
6.5.1 eture >uer' 4unctions..........................................................................................1)66.5.# eel Loou/ 4unctions...........................................................................................116.5.% eture Gather 4unctions.........................................................................................136.5.) $o"/atiilit' 2ro(ile eture 4unctions..................................................................10*
6.1* Ato"ic9$ounter 4unctions.............................................................................................10#
6.11 Ato"ic ;e"or' 4unctions............................................................................................10#6.1# I"a!e 4unctions.............................................................................................................10%6.1% 4ra!"ent 2rocessin! 4unctions......................................................................................100
6.1%.1 Derivative 4unctions..............................................................................................1006.1%.# Inter/olation 4unctions...........................................................................................106
6.1) 8oise 4unctions..............................................................................................................1056.1 Geo"etr' &hader 4unctions...........................................................................................13*6.10 &hader Invocation $ontrol 4unctions.............................................................................13#6.13 &hader ;e"or' $ontrol 4unctions................................................................................13%
5 &hadin! Lan!ua!e Gra""ar (or $ore 2ro(ile........................................................................13
vi
8/12/2019 GLSLangSpec.4.30.6
7/196
1 Introduction
his docu"ent s/eci(ies onl' version ).%* o( the +/enGL &hadin! Lan!ua!e. It re
8/12/2019 GLSLangSpec.4.30.6
8/196
1 Introduction
1.1 Acknowledgments
his s/eci(ication is ased on the wor o( those who contriuted to /ast versions o( the +/enGL
Lan!ua!e &/eci(ication, the +/enGL E& #.* Lan!ua!e &/eci(ication, and the (ollowin! contriutors tothis version:
2at Brown, 8VIDIA
Je(( Bol=, 8VIDIA
4ran $hen
2ierre Boudier, A;D
2iers Daniell, 8VIDIA
$hris Dodd, 8VIDIA
8ic -ae"el, 8VIDIA
Jason Green, ransGa"in!
Brent Inso, Intel
Jon Leech
Bill Licea9Kane, A;DDaniel Koch, ransGa"in!
Barthold Lichtenelt, 8VIDIA
Bruce ;err', AR;
Roert +hannessian
Acorn 2oole', 8VIDIA
$hristo/he Riccio, A;D
Kevin Ro!ovin
Ian Ro"anic, Intel
Gre! Roth, 8vidia
Graha" &ellers, A;D
Dave &hreiner, AR;
Jere"' &and"el, A//le
Roert &i"/son, >ualco""Eric @erness, 8VIDIA
;ar oun!, A;D
2
8/12/2019 GLSLangSpec.4.30.6
9/196
1 Introduction
1.2 Changes
1.2.1 Summary of Changes from Version .2!
8ote: 8o (eatures were de/recated etween versions ).#* and ).%*.
$han!es
Add shader stora!e bufferoects, as /er the ARB?shader?stora!e?u((er?oect etension.
his includes 1C allowin! the last "e"er o( a stora!e u((er loc to e an arra' that does not
now its si=e until render ti"e, and #C readwrite "e"or' shared with the a//lication and other
shader invocations. It also adds thestd430la'out
8/12/2019 GLSLangSpec.4.30.6
10/196
1 Introduction
4or la'out
8/12/2019 GLSLangSpec.4.30.6
11/196
1 Introduction
1." O#er#ie$
his docu"ent descries ,e *en/L ,ading Language version ).%*.
Inde/endent co"/ilation units written in this lan!ua!e are called s,aders. A*rogramis a set o( shadersthat are co"/iled and lined to!ether, co"/letel' creatin! one or "ore o( the /ro!ra""ale sta!es o( the
+/enGL /i/eline. All the shaders (or a sin!le /ro!ra""ale sta!e "ust e within the sa"e /ro!ra". A
co"/lete set o( /ro!ra""ale sta!es can e /ut into a sin!le /ro!ra" or the sta!es can e /artitioned
across "ulti/le /ro!ra"s. he ai" o( this docu"ent is to thorou!hl' s/eci(' the /ro!ra""in! lan!ua!e.
he +/enGL Gra/hics &'ste" &/eci(ication will s/eci(' the +/enGL entr' /oints used to "ani/ulate and
co""unicate with /ro!ra"s and shaders.
1. %rror &andling
$o"/ilers, in !eneral, acce/t /ro!ra"s that are ill9(or"ed, due to the i"/ossiilit' o( detectin! all ill9
(or"ed /ro!ra"s. 2ortailit' is onl' ensured (or well9(or"ed /ro!ra"s, which this s/eci(ication
descries. $o"/ilers are encoura!ed to detect ill9(or"ed /ro!ra"s and issue dia!nostic "essa!es, ut are
not re
8/12/2019 GLSLangSpec.4.30.6
12/196
2 O#er#ie$ of OpenGL Shading
he +/enGL &hadin! Lan!ua!e is actuall' several closel' related lan!ua!es. hese lan!ua!es are used
to create shaders (or each o( the /ro!ra""ale /rocessors contained in the +/enGL /rocessin! /i/eline.
$urrentl', these /rocessors are the verte, tessellation control, tessellation evaluation, !eo"etr',
(ra!"ent, and co"/ute /rocessors.
7nless otherwise noted in this /a/er, a lan!ua!e (eature a//lies to all lan!ua!es, and co""on usa!e will
re(er to these lan!ua!es as a sin!le lan!ua!e. he s/eci(ic lan!ua!es will e re(erred to ' the na"e o(
the /rocessor the' tar!et: verte, tessellation control, tessellation evaluation, !eo"etr', (ra!"ent, or
co"/ute.
;ost +/enGL state is not traced or "ade availale to shaders. '/icall', user9de(ined variales will eused (or co""unicatin! etween di((erent sta!es o( the +/enGL /i/eline. -owever, a s"all a"ount o(
state is still traced and auto"aticall' "ade availale to shaders, and there are a (ew uilt9in variales (or
inter(aces etween di((erent sta!es o( the +/enGL /i/eline.
2.1 Verte* +rocessor
he verte! *rocessoris a /ro!ra""ale unit that o/erates on inco"in! vertices and their associated data.
$o"/ilation units written in the +/enGL &hadin! Lan!ua!e to run on this /rocessor are called verte!
s,aders. @hen a set o( verte shaders are success(ull' co"/iled and lined, the' result in a verte! s,ader
e!ecuta'lethat runs on the verte /rocessor.
he verte /rocessor o/erates on one verte at a ti"e. It does not re/lace !ra/hics o/erations that re
8/12/2019 GLSLangSpec.4.30.6
13/196
2 Overview of OpenGL Shading
written ' another invocation at an' /oint durin! the sa"e /hase, or i( two invocations atte"/t to write
di((erent values to the sa"e /er9/atch out/ut in a sin!le /hase.
2." Tessellation %#aluation +rocessorhe tessellation evaluation /rocessor is a /ro!ra""ale unit that evaluates the /osition and other
attriutes o( a verte !enerated ' the tessellation /ri"itive !enerator, usin! a /atch o( inco"in! vertices
and their associated data. $o"/ilation units written in the +/enGL &hadin! Lan!ua!e to run on this
/rocessor are called tessellation evaluation shaders. @hen a set o( tessellation evaluation shaders are
success(ull' co"/iled and lined, the' result in a tessellation evaluation s,ader e!ecuta'le that runs on
the tessellation evaluation /rocessor.
Each invocation o( the tessellation evaluation eecutale co"/utes the /osition and attriutes o( a sin!le
verte !enerated ' the tessellation /ri"itive !enerator. he eecutale can read the attriutes o( an'
verte in the in/ut /atch, /lus the tessellation coordinate, which is the relative location o( the verte in the
/ri"itive ein! tessellated. he eecutale writes the /osition and other attriutes o( the verte.
2. Geometry +rocessor
hegeometr( *rocessor is a /ro!ra""ale unit that o/erates on data (or inco"in! vertices (or a /ri"itive
asse"led a(ter verte /rocessin! and out/uts a se
8/12/2019 GLSLangSpec.4.30.6
14/196
2 Overview of OpenGL Shading
A co"/ute shader has access to "an' o( the sa"e resources as (ra!"ent and other shader /rocessors,
includin! tetures, u((ers, i"a!e variales, and ato"ic counters. It does not have an' /rede(ined in/uts
nor an' (ied9(unction out/uts. It is not /art o( the !ra/hics /i/eline and its visile side e((ects are
throu!h chan!es to i"a!es, stora!e u((ers, and ato"ic counters.
A co"/ute shader o/erates on a !rou/ o( wor ite"s called a wor !rou/. A wor !rou/ is a collection
o( shader invocations that eecute the sa"e code, /otentiall' in /arallel. An invocation within a wor
!rou/ "a' share data with other "e"ers o( the sa"e wor !rou/ throu!h shared variales and issue
"e"or' and control arriers to s'nchroni=e with other "e"ers o( the sa"e wor !rou/.
8
8/12/2019 GLSLangSpec.4.30.6
15/196
" -asics
".1 Character Set
he source character set used (or the +/enGL shadin! lan!ua!es, outside o( co""ents, is a suset o(
7496. It includes the (ollowin! characters:
he letters a&z, A&'( and the underscore ?C.
he nu"ers)&*.
he s'"ols /eriod .C, /lus +C, dash &C, slash ,C, asteris -C, /ercent C, an!led racets /and
0C, s
8/12/2019 GLSLangSpec.4.30.6
16/196
3 a!ic!
strin! ein! strin! *. Line nu"ers are one "ore than the nu"er o( new lines that have een /rocessed,
includin! countin! the new lines that will e re"oved ' the line9continuation character @C.
Lines se/arated ' the line9continuation character /recedin! a new line are concatenated to!ether e(ore
either co""ent /rocessin! or /re/rocessin!. 8o white s/ace is sustituted (or the line9continuation
character. hat is, a sin!le toen could e (or"ed ' the concatenation ' tain! the characters at the end
o( one line concatenatin! the" with the characters at the e!innin! o( the net line.
float f\
oo;
// forms a single line equivalent to float foo;
// (assuming '\' is the last character before the new line and oo are
// the first two characters of the next line)
"." +reprocessor
here is a /re/rocessor that /rocesses the source strin!s as /art o( the co"/ilation /rocess.
he co"/lete list o( /re/rocessor directives is as (ollows.
define
undef
if
ifdef
ifndef
else
elif
endif
error!ragma
extension
version
line
he (ollowin! o/erators are also availale
defined
Each nu"er si!n #C can e /receded in its line onl' ' s/aces or hori=ontal tas. It "a' also e
(ollowed ' s/aces and hori=ontal tas, /recedin! the directive. Each directive is ter"inated ' a newline. 2re/rocessin! does not chan!e the nu"er or relative location o( new lines in a source strin!.
2re/rocessin! taes /laces a(ter new lines have een re"oved ' the line9continuation character.
he nu"er si!n #C on a line ' itsel( is i!nored. An' directive not listed aove will cause a dia!nostic
"essa!e and "ae the i"/le"entation treat the shader as ill9(or"ed.
1"
8/12/2019 GLSLangSpec.4.30.6
17/196
3 a!ic!
#defineand #undef(unctionalit' are de(ined as is standard (or $OO /re/rocessors (or "acro de(initions
oth with and without "acro /ara"eters.
he (ollowin! /rede(ined "acros are availale
""#$%&""
""$#&""
""&*$+%""
__L#__will sustitute a deci"al inte!er constant that is one "ore than the nu"er o( /recedin! new
lines in the current source strin!.
__"#L__will sustitute a deci"al inte!er constant that sa's which source strin! nu"er is currentl'
ein! /rocessed.
__VR#__will sustitute a deci"al inte!er re(lectin! the version nu"er o( the +/enGL shadin!
lan!ua!e. he version o( the shadin! lan!ua!e descried in this docu"ent will have__VR#__
sustitute the deci"al inte!er )%*.
All "acro na"es containin! two consecutive underscores "" C are reserved (or (uture use as /rede(ined
"acro na"es. All "acro na"es /re(ied with GL? GL (ollowed ' a sin!le underscoreC are also
reserved.
#if( #ifdef( #ifndef( #else( #elif( and#endifare de(ined to o/erate as is standard (or $OO /re/rocessors.
E/ressions (ollowin! #ifand #elifare (urther restricted to e/ressions o/eratin! on literal inte!er
constants, /lus identi(iers consu"ed ' the definedo/erator. $haracter constants are not su//orted.
he o/erators availale are as (ollows.
+recedence Operator class Operators ssociati#ity
1 hi!hestC /arenthetical !rou/in! C 8A
# unar' de(inedO 9 P Q
Ri!ht to Le(t
% "ulti/licative S Le(t to Ri!ht
) additive O 9 Le(t to Ri!ht
it9wise shi(t TT UU Le(t to Ri!ht
0 relational T U T U Le(t to Ri!ht
3 e
8/12/2019 GLSLangSpec.4.30.6
18/196
3 a!ic!
he definedo/erator can e used in either o( the (ollowin! wa's:
defined identifier
defined( identifier )
wo toens in a "acro can e concatenated into one toen usin! the toen /astin! ##Co/erator, as is
standard (or $OO /re/rocessors. he result "ust e a valid sin!le toen, which will then e suect to
"acro e/ansion. hat is, "acro e/ansion ha//ens onl' a(ter toen /astin!. here are no other nu"er
si!n ased o/erators e.!., no # or#C, nor is there a sizeofo/erator.
he se"antics o( a//l'in! o/erators to inte!er literals in the /re/rocessor "atch those standard in the $O
O /re/rocessor, not those in the +/enGL &hadin! Lan!ua!e.
2re/rocessor e/ressions will e evaluated accordin! to the ehavior o( the host /rocessor, not the
/rocessor tar!eted ' the shader.
#error will cause the i"/le"entation to /ut a co"/ile9ti"e dia!nostic "essa!e into the shader oectNs
in(or"ation lo! see section 3.1# &hader and 2ro!ra" >ueries in the +/enGL Gra/hics &'ste"
&/eci(ication (or how to access a shader oectNs in(or"ation lo!C. he "essa!e will e the toens(ollowin! the #errordirective, u/ to the (irst new line. he i"/le"entation "ust then consider the shader
to e ill9(or"ed.
#!ragmaallows i"/le"entation de/endent co"/iler control. oens (ollowin! #!ragmaare not suect
to /re/rocessor "acro e/ansion. I( an i"/le"entation does not reco!ni=e the toens (ollowin!
#!ragma, then it will i!nore that /ra!"a. he (ollowin! /ra!"as are de(ined as /art o( the lan!ua!e.
!ragma *,-.#
he SBCD/ra!"a is used to reserve /ra!"as (or use ' (uture revisions o( this lan!ua!e. 8o
i"/le"entation "a' use a /ra!"a whose (irst toen is SBCD.
!ragma o!timie(on)
!ragma o!timie(off)
can e used to turn o(( o/ti"i=ations as an aid in develo/in! and deu!!in! shaders. It can onl' e used
outside (unction de(initions. B' de(ault, o/ti"i=ation is turned on (or all shaders. he deu! /ra!"a
!ragma debug(on)
!ragma debug(off)
can e used to enale co"/ilin! and annotatin! a shader with deu! in(or"ation, so that it can e used
with a deu!!er. It can onl' e used outside (unction de(initions. B' de(ault, deu! is turned o((.
&haders should declare the version o( the lan!ua!e the' are written to. he lan!ua!e version a shader is
written to is s/eci(ied '
version number profileopt
where num'er"ust e a version o( the lan!ua!e, (ollowin! the sa"e convention as__VR#__aove.
he directive #version EF) is re
8/12/2019 GLSLangSpec.4.30.6
19/196
3 a!ic!
s/eci(' #version1** will e treated as tar!etin! version 1.** o( the +/enGL E& &hadin! Lan!ua!e.
&haders that s/eci(' #version%** will e treated as tar!etin! version %.** o( the +/enGL E& &hadin!
Lan!ua!e.
I( the o/tional*ro%ile ar!u"ent is /rovided, it "ust e the na"e o( an +/enGL /ro(ile. $urrentl', there
are three choices:
core
com!atibilit0
es
A*ro%ilear!u"ent can onl' e used with version 1* or !reater. I( no /ro(ile ar!u"ent is /rovided and
the version is 1* or !reater, the de(ault is core. I( version %** is s/eci(ied, the /ro(ile ar!u"ent is not
o/tional and "ust e es, or a co"/ile9ti"e error results. he Lan!ua!e &/eci(ication (or the es/ro(ile is
s/eci(ied in he +/enGL E& &hadin! Lan!ua!e s/eci(ication.
&haders (or the core or com!atibility/ro(iles that declare di((erent versions can e lined to!ether.
-owever, es/ro(ile shaders cannot e lined with non9es/ro(ile shaders or with es/ro(ile shaders o( adi((erent version, or a lin9ti"e error will result. @hen linin! shaders o( versions allowed ' these rules,
re"ainin! lin9ti"e errors will e !iven as /er the linin! rules in the GL&L version corres/ondin! to the
version o( the contet the shaders are lined under. &hader co"/ile9ti"e errors "ust still e !iven strictl'
ased on the version declared or de(aulted toC within each shader.
7nless otherwise s/eci(ied, this s/eci(ication is docu"entin! the core /ro(ile, and ever'thin! s/eci(ied (or
the core /ro(ile is also availale in the co"/atiilit' /ro(ile. 4eatures s/eci(ied as elon!in! s/eci(icall'
to the co"/atiilit' /ro(ile are not availale in the core /ro(ile.
here is a uilt9in "acro de(inition (or each /ro(ile the i"/le"entation su//orts. All i"/le"entations
/rovide the (ollowin! "acro:
define .#"core"!rofile 1
I"/le"entations /rovidin! the com!atibility/ro(ile /rovide the (ollowin! "acro:
define .#"com!atibilit0"!rofile 1
I"/le"entations /rovidin! the es/ro(ile /rovide the (ollowin! "acro:
define .#"es"!rofile 1
he #versiondirective "ust occur in a shader e(ore an'thin! else, ece/t (or co""ents and white s/ace.
13
8/12/2019 GLSLangSpec.4.30.6
20/196
3 a!ic!
B' de(ault, co"/ilers o( this lan!ua!e "ust issue co"/ile9ti"e leical and !ra""atical errors (or shaders
that do not con(or" to this s/eci(ication. An' etended ehavior "ust (irst e enaled. Directives to
control the ehavior o( the co"/iler with res/ect to etensions are declared with the #extensiondirective
extension extension_name:behavior
extension all :behavior
where e!tension_nameis the na"e o( an etension. Etension na"es are not docu"ented in this
s/eci(ication. he toen all"eans the ehavior a//lies to all etensions su//orted ' the co"/iler. he
'e,avior can e one o( the (ollowin!
behavior %ffect
re/uire Behave as s/eci(ied ' the etension e!tension_name.
Give a co"/ile9ti"e error on the #extension i( the etension e!tension_nameis not su//orted, or i( allis s/eci(ied.
enable Behave as s/eci(ied ' the etension e!tension_name.
@arn on the #extensioni( the etension e!tension_nameis not su//orted.
Give a co"/ile9ti"e error on the #extension i( allis s/eci(ied.
$arn Behave as s/eci(ied ' the etension e!tension_name, ece/t issue warnin!son an' detectale use o( that etension, unless such use is su//orted ' otherenaled or re
8/12/2019 GLSLangSpec.4.30.6
21/196
3 a!ic!
he initial state o( the co"/iler is as i( the directive
extension all 2 disable
was issued, tellin! the co"/iler that all error and warnin! re/ortin! "ust e done accordin! to thiss/eci(ication, i!norin! an' etensions.
Each etension can de(ine its allowed !ranularit' o( sco/e. I( nothin! is said, the !ranularit' is a shader
that is, a sin!le co"/ilation unitC, and the etension directives "ust occur e(ore an' non9/re/rocessor
toens. I( necessar', the liner can en(orce !ranularities lar!er than a sin!le co"/ilation unit, in which
case each involved shader will have to contain the necessar' etension directive.
;acro e/ansion is not done on lines containin! #extensionand #versiondirectives.
#line"ust have, a(ter "acro sustitution, one o( the (ollowin! (or"s:
line line
line line source-string-number
where line andsource-string-num'er are constant inte!er e/ressions. A(ter /rocessin! this directiveincludin! its new lineC, the i"/le"entation will ehave as i( it is co"/ilin! at line nu"er lineand source
strin! nu"ersource-string-num'er. &use
8/12/2019 GLSLangSpec.4.30.6
22/196
3 a!ic!
".' To0ens
he lan!ua!e is a se 5 6
".( ey$ords
he (ollowin! are the lan!ua!eMs e'words and a(ter /re/rocessin!C can onl' e used as descried in this
s/eci(ication, or a co"/ile9ti"e error results:
attribute const uniform varying buffer shared
coherent volatile restrict readonly writeonly
atomic"uint
layout
centroid flat smooth no!ers!ective
!atch sam!le
break continue do for while switch case default
if else
subroutine
in out inout
float double int void bool true false
invariant
discard return
matG matF matE dmatG dmatF dmatE
matGxG matGxF matGxE dmatGxG dmatGxF dmatGxE
matFxG matFxF matFxE dmatFxG dmatFxF dmatFxE
matExG matExF matExE dmatExG dmatExF dmatExE
vecG vecF vecE ivecG ivecF ivecE bvecG bvecF bvecE dvecG dvecF dvecE uint uvecG uvecF uvecE
16
8/12/2019 GLSLangSpec.4.30.6
23/196
3 a!ic!
low! medium! high! !recision
sam!ler1C sam!lerGC sam!lerFC sam!lerHube
sam!ler1CShadow sam!lerGCShadow sam!lerHubeShadow sam!ler1CArray sam!lerGCArray
sam!ler1CArrayShadow sam!lerGCArrayShadow
isam!ler1C isam!lerGC isam!lerFC isam!lerHube
isam!ler1CArray isam!lerGCArray
usam!ler1C usam!lerGC usam!lerFC usam!lerHube
usam!ler1CArray usam!lerGCArray
sam!lerGCIect sam!lerGCIectShadow isam!lerGCIect usam!lerGCIect
sam!lerJuffer isam!lerJuffer usam!lerJuffer
sam!lerGCKS isam!lerGCKS usam!lerGCKS sam!lerGCKSArray isam!lerGCKSArray usam!lerGCKSArray
sam!lerHubeArray sam!lerHubeArrayShadow isam!lerHubeArray usam!lerHubeArray
image1C iimage1C uimage1C
imageGC iimageGC uimageGC
imageFC iimageFC uimageFC
imageGCIect iimageGCIect uimageGCIect
imageHube iimageHube uimageHube
imageJuffer iimageJuffer uimageJuffer
image1CArray iimage1CArray uimage1CArray
imageGCArray iimageGCArray uimageGCArray
imageHubeArray iimageHubeArray uimageHubeArray
imageGCKS iimageGCKS uimageGCKS
imageGCKSArray iimageGCKSArray uimageGCKSArray
struct
he (ollowin! are the e'words reserved (or (uture use. 7sin! the" will result in a co"/ile9ti"e error:
common !artition active
asm
class union enum ty!edef tem!late this !acked
resource
17
8/12/2019 GLSLangSpec.4.30.6
24/196
3 a!ic!
goto
inline noinline !ublic static extern external interface
long short half fixed unsigned su!er! in!ut out!ut
hvecG hvecF hvecE fvecG fvecF fvecE
sam!lerFCIect
filter
sizeof cast
names!ace using
row"maLor
In addition, all identi(iers containin! two consecutive underscores ""C are reserved as /ossile (uture
e'words.
". Identifiers
Identi(iers are used (or variale na"es, (unction na"es, structure na"es, and (ield selectors (ield
selectors select co"/onents o( vectors and "atrices si"ilar to structure "e"ers, as discussed in section
. Vector and &calar $o"/onents and section .0 ;atri $o"/onents C. Identi(iers have the (or"
identi%ier
nondigit
identi%ier nondigit
identi%ier digit
nondigit: one o(
" a b c d e f g h i L k l m n o ! M r s t u v w x y z
A J H C $ N D O P R K % Q I S B T U V W X '
digit: one o(
) 1 G F E Y Z [ \ *
Identi(iers startin! with !l? are reserved (or use ' +/enGL, and "a' not e declared in a shader as
either a variale or a (unctionF this results in a co"/ile9ti"e error. -owever, as noted in the s/eci(ication,
there are so"e cases where /reviousl' declared variales can e redeclared, and /redeclared Z!l?Z na"es
are allowed to e redeclared in a shader onl' (or these s/eci(ic /ur/oses. ;ore !enerall', it is a co"/ile9
ti"e error to redeclare a variale, includin! those startin! !l?.
".3 )efinitions
&o"e lan!ua!e rules descried elow de/end on the (ollowin! de(initions.
18
8/12/2019 GLSLangSpec.4.30.6
25/196
3 a!ic!
".3.1 Static 4se
A shader contains astatic useo( orstatic assignmenttoC a variale!i(, a(ter /re/rocessin!, the shader
contains a state"ent that would read or writeC!, whether or not run9ti"e (low o( control will cause that
state"ent to e eecuted.
".3.2 4niform and 5on64niform Control ,lo$
@hen eecutin! state"ents in a (ra!"ent shader, control (low starts as uni%orm control %lo)F all (ra!"ents
enter the sa"e control /ath into mainC. $ontrol (low eco"es non-uni%orm when di((erent (ra!"ents
tae di((erent /aths throu!h control9(low state"ents selection, iteration, and u"/sC. $ontrol (low
suse
8/12/2019 GLSLangSpec.4.30.6
26/196
Variables and Types
All variales and (unctions "ust e declared e(ore ein! used. Variale and (unction na"es are
identi(iers.
here are no de(ault t'/es. All variale and (unction declarations "ust have a declared t'/e, and
o/tionall'
8/12/2019 GLSLangSpec.4.30.6
27/196
4 #aria$%e! and &'pe!
Type 7eaning
ivecF a three9co"/onent si!ned inte!er vector
ivecE a (our9co"/onent si!ned inte!er vectoruvecG a two9co"/onent unsi!ned inte!er vector
uvecF a three9co"/onent unsi!ned inte!er vector
uvecE a (our9co"/onent unsi!ned inte!er vector
matG a #[# sin!le9/recision (loatin!9/oint "atri
matF a %[% sin!le9/recision (loatin!9/oint "atri
matE a )[) sin!le9/recision (loatin!9/oint "atri
matGxG sa"e as a matG
matGxF a sin!le9/recision (loatin!9/oint "atri with # colu"ns and % rows
matGxE a sin!le9/recision (loatin!9/oint "atri with # colu"ns and ) rowsmatFxG a sin!le9/recision (loatin!9/oint "atri with % colu"ns and # rows
matFxF sa"e as a matF
matFxE a sin!le9/recision (loatin!9/oint "atri with % colu"ns and ) rows
matExG a sin!le9/recision (loatin!9/oint "atri with ) colu"ns and # rows
matExF a sin!le9/recision (loatin!9/oint "atri with ) colu"ns and % rows
matExE sa"e as a matE
dmatG a #[# doule9/recision (loatin!9/oint "atri
dmatF a %[% doule9/recision (loatin!9/oint "atri
dmatE a )[) doule9/recision (loatin!9/oint "atridmatGxG sa"e as a dmatG
dmatGxF a doule9/recision (loatin!9/oint "atri with # colu"ns and % rows
dmatGxE a doule9/recision (loatin!9/oint "atri with # colu"ns and ) rows
dmatFxG a doule9/recision (loatin!9/oint "atri with % colu"ns and # rows
dmatFxF sa"e as a dmatF
dmatFxE a doule9/recision (loatin!9/oint "atri with % colu"ns and ) rows
dmatExG a doule9/recision (loatin!9/oint "atri with ) colu"ns and # rows
dmatExF a doule9/recision (loatin!9/oint "atri with ) colu"ns and % rows
dmatExE sa"e as a dmatE
21
8/12/2019 GLSLangSpec.4.30.6
28/196
4 #aria$%e! and &'pe!
4loatin!92oint +/a
8/12/2019 GLSLangSpec.4.30.6
29/196
4 #aria$%e! and &'pe!
Type 7eaning
isam!lerGCiimageGC
a handle (or accessin! an inte!er #D teture
isam!lerFC
iimageFC
a handle (or accessin! an inte!er %D teture
isam!lerHubeiimageHube
a handle (or accessin! an inte!er cue "a//ed teture
isam!lerGCIectiimageGCIect
a handle (or accessin! an inte!er #D rectan!le teture
isam!ler1CArrayiimage1CArray
a handle (or accessin! an inte!er 1D arra' teture
isam!lerGCArray
iimageGCArray
a handle (or accessin! an inte!er #D arra' teture
isam!lerJufferiimageJuffer a handle (or accessin! an inte!er u((er teture
isam!lerGCKSiimageGCKS
a handle (or accessin! an inte!er #D "ulti9sa"/le teture
isam!lerGCKSArrayiimageGCKSArray
a handle (or accessin! an inte!er #D "ulti9sa"/le arra' teture
isam!lerHubeArray
iimageHubeArray
a handle (or accessin! an inte!er cue "a/ arra' teture
7nsi!ned Inte!er +/a
8/12/2019 GLSLangSpec.4.30.6
30/196
4 #aria$%e! and &'pe!
Type 7eaning
usam!lerJufferuimageJuffer
a handle (or accessin! an unsi!ned inte!er u((er teture
usam!lerGCKS
uimageGCKS
a handle (or accessin! an unsi!ned inte!er #D "ulti9sa"/le teture
usam!lerGCKSArrayuimageGCKSArray
a handle (or accessin! an unsi!ned inte!er #D "ulti9sa"/le teturearra'
usam!lerHubeArrayuimageHubeArray
a handle (or accessin! an unsi!ned inte!er cue "a/ arra' teture
In addition, a shader can a!!re!ate these asic t'/es usin! arra's and structures to uild "ore co"/le
t'/es.
here are no /ointer t'/es.
.1.1 Void
4unctions that do not return a value "ust e declared as void. here is no de(ault (unction return t'/e.
he e'word voidcannot e used in an' other declarations ece/t (or e"/t' (or"al or actual /ara"eter
listsC, or a co"/ile9ti"e error results.
.1.2 -ooleans
o "ae conditional eecution o( code easier to e/ress, the t'/e boolis su//orted. here is no
e/ectation that hardware directl' su//orts variales o( this t'/e. It is a !enuine Boolean t'/e, holdin!
onl' one o( two values "eanin! either true or (alse. wo e'words trueand falsecan e used as literal
Boolean constants. Booleans are declared and o/tionall' initiali=ed as in the (ollow ea"/le:
bool success; // declare success to be a :ooleanbool done 4 false; // declare and initialie done
he ri!ht side o( the assi!n"ent o/erator ; C "ust e an e/ression whose t'/e is bool.
E/ressions used (or conditional u"/s if( for( ?=( while( do&whileC "ust evaluate to the t'/e bool.
.1." Integers
&i!ned and unsi!ned inte!er variales are (ull' su//orted. In this docu"ent, the ter" integer is "eant to
!enerall' include oth si!ned and unsi!ned inte!ers. 7nsi!ned inte!ers have eactl' %# its o( /recision.
&i!ned inte!ers use %# its, includin! a si!n it, in twoMs co"/le"ent (or". +/erations resultin! in
over(low or under(low will not cause an' ece/tion, nor will the' saturate, rather the' will wra/ to 'ield
the low9order %# its o( the result.
Inte!ers are declared and o/tionall' initiali=ed with inte!er e/ressions, as in the (ollowin! ea"/le:
int i8 4 u; // u establishes the t0!e as uint
24
8/12/2019 GLSLangSpec.4.30.6
31/196
4 #aria$%e! and &'pe!
Literal inte!er constants can e e/ressed in deci"al ase 1*C, octal ase 6C, or headeci"al ase 10C
as (ollows.
integer-constant :
decimal-constant integer-su%%i!o*t
octal-constant integer-su%%i!o*t
,e!adecimal-constant integer-su%%i!o*t
integer-su%%i!: one o(
u T
decimal-constant :
non5ero-digit
decimal-constant digit
octal-constant :
)
octal-constant octal-digit
,e!adecimal-constant :
*,e!adecimal-digit
*\,e!adecimal-digit
,e!adecimal-constant ,e!adecimal-digit
digit :
)
non5ero-digit
non5ero-digit : one o(
1 G F E Y Z [ \ *
octal-digit= one o(
) 1 G F E Y Z [
,e!adecimal-digit=one o(
) 1 G F E Y Z [ \ *
a b c d e f
A J H C $ N
8o white s/ace is allowed etween the di!its o( an inte!er constant, includin! a(ter the leadin! )or a(ter
the leadin! )xor )Wo( a constant, or e(ore the su((iuorT. @hen toeni=in!, the "ai"al toen
"atchin! the aove will e reco!ni=ed e(ore a new toen is started. @hen the su((i u or T is /resent,
the literal has t'/e uint,otherwise the t'/e is int. A leadin! unar' "inus si!n 9C is inter/reted as an
arith"etic unar' ne!ation, not as /art o( the constant. -ence, literals the"selves are alwa's e/ressedwith non9ne!ative s'nta, thou!h the' could result in a ne!ative value.
It is a co"/ile9ti"e error to /rovide a literal inte!er whose it /attern cannot (it in %# its. he it /attern
o( the literal is alwa's used un"odi(ied. &o a si!ned literal whose it /attern includes a set si!n it
creates a ne!ative value. 4or ea"/le,
25
8/12/2019 GLSLangSpec.4.30.6
32/196
4 #aria$%e! and &'pe!
int a 4 ?xffffffff; // >= bits8 a gets the value 31
int b 4 ?xffffffff@; // &+2 can't convert uint to int
uint c 4 ?xffffffff; // >= bits8 c gets the value ?x
uint d 4 ?xffffffff@; // >= bits8 d gets the value ?xint e 4 31; // the literal is 18 then negation is !erformed8
// and the resulting non3literal >=3bit signed
// bit !attern of ?x is assigned8 giving e
// the value of 316
uint f 4 31u; // the literal is 1u8 then negation is !erformed8
// and the resulting non3literal >=3bit unsigned
// bit !attern of ?x is assigned8 giving f
// the value of ?x6
int g 4 >?????????; // a signed decimal literal taking >= bits8
// setting the sign bit8 g gets 31=A=3bit signed hexadecimal
int i 4 E?????????; // &+2 needs more than >= bits
int 4 ?x; // &+2 needs more that >= bits
int k 4 ?xF???????; // k gets 3=1
8/12/2019 GLSLangSpec.4.30.6
33/196
4 #aria$%e! and &'pe!
4loatin!9/oint constants are de(ined as (ollows.
%loating-constant :
%ractional-constant e!*onent-*arto*t
%loating-su%%i!o*t
digit-se+uence e!*onent-*art %loating-su%%i!o*t
%ractional-constant :
digit-se+uence.digit-se+uence
digit-se+uence.
.digit-se+uence
e!*onent-*art :
esigno*t
digit-se+uence
Esigno*t
digit-se+uence
sign : one o(
+ ]digit-se+uence :
digit
digit-se+uence digit
%loating-su%%i!: one o(
f N lf N
A deci"al /oint .C is not needed i( the e/onent /art is /resent. 8o white s/ace "a' a//ear an'where
within a (loatin!9/oint constant, includin! e(ore a su((i. @hen toeni=in!, the "ai"al toen "atchin!
the aove will e reco!ni=ed e(ore a new toen is started. @hen the su((i Zl(Z or ZL4Z is /resent, the
literal has t'/e double. +therwise, the literal has t'/e float. A leadin! unar' "inus si!n &C is inter/reted
as a unar' o/erator and is not /art o( the (loatin!9/oint constant
.1.' Vectors
he +/enGL &hadin! Lan!ua!e includes data t'/es (or !eneric #9, %9, and )9co"/onent vectors o(
(loatin!9/oint values, inte!ers, or Booleans. 4loatin!9/oint vector variales can e used to store colors,
nor"als, /ositions, teture coordinates, teture loou/ results and the lie. Boolean vectors can e used
(or co"/onent9wise co"/arisons o( nu"eric vectors. &o"e ea"/les o( vector declaration are:
vec= texcoord18 texcoord=;
vec> !osition;
vec< m0.:D;
ivec= texture#ooku!;
bvec> less;
Initiali=ation o( vectors can e done with constructors, which are discussed shortl'.
.1.( 7atrices
he +/enGL &hadin! Lan!ua!e has uilt9in t'/es (or #[#, #[%, #[), %[#, %[%, %[), )[#, )[%, and )[)
"atrices o( (loatin!9/oint nu"ers. ;atri t'/es e!innin! with Z"atZ have sin!le9/recision co"/onents
27
8/12/2019 GLSLangSpec.4.30.6
34/196
4 #aria$%e! and &'pe!
while "atri t'/es e!innin! with Zd"atZ have doule9/recision co"/onents. he (irst nu"er in the
t'/e is the nu"er o( colu"ns, the second is the nu"er o( rows. I( there is onl' one nu"er, the "atri
is s o!tGatrix;
mat< view8 !roection;
matx= m; // a matrix with > columns and = rows
dmat< highHrecisionGH;
dmat=x< dm;
Initiali=ation o( "atri values is done with constructors descried in section .) $onstructors C in
colu"n9"aor order.
.1. Opa/ue Types
he o/a
8/12/2019 GLSLangSpec.4.30.6
35/196
4 #aria$%e! and &'pe!
I"a!e variales are handles to one9, two9, or three9di"ensional i"a!es corres/ondin! to all or a /ortion
o( a sin!le level o( a teture i"a!e ound to an i"a!e unit. here are distinct i"a!e t'/es (or each teture
tar!et, and (or each o( (loat, inte!er, and unsi!ned inte!er data t'/es. I"a!e accesses should use an i"a!e
t'/e that "atches the tar!et o( the teture whose level is ound to the i"a!e unit, or (or non9la'eredindin!s o( %D or arra' i"a!es should use the i"a!e t'/e that "atches the di"ensionalit' o( the la'er o(
the i"a!e i.e., a la'er o( %D, #DArra', $ue, or $ueArra' should use imageGC, a la'er o( 1DArra'
should use image1C, and a la'er o( #D;&Arra' should use imageGCKSC. I( the i"a!e tar!et t'/e does
not "atch the ound i"a!e in this "anner, i( the data t'/e does not "atch the ound i"a!e, or i( the
(or"at la'out uali(iers. @hen a!!re!ated into arra's
within a shader, ato"ic counters can onl' e indeed with a d'na"icall' uni(or" inte!ral e/ression,
otherwise results are unde(ined.
.1.3 Structures
7ser9de(ined t'/es can e created ' a!!re!atin! other alread' de(ined t'/es into a structure usin! the
structe'word. 4or ea"/le,
struct light 5
float intensit0; vec> !osition;
9 lightar;
In this ea"/le, lig,teco"es the na"e o( the new t'/e, and lig,tVareco"es a variale o( t'/e lig,t.
o declare variales o( the new t'/e, use its na"e without the e'word structC.
light lightar=;
29
8/12/2019 GLSLangSpec.4.30.6
36/196
4 #aria$%e! and &'pe!
;ore (or"all', structures are declared as (ollows. -owever, the co"/lete correct !ra""ar is as !iven in
section 5 &hadin! Lan!ua!e Gra""ar .
struct-de%inition :
+uali%iero*t
structnameo*t
5mem'er-list 6declaratorso*t
mem'er-list :
mem'er-declaration
mem'er-declaration mem'er-list
mem'er-declaration :
'asic-t(*e declarators
where nameeco"es the user9de(ined t'/e, and can e used to declare variales to e o( this new t'/e.
he nameshares the sa"e na"e s/ace as other variales, t'/es, and (unctions. All /reviousl' visile
variales, t'/es, constructors, or (unctions with that na"e are hidden. he o/tional +uali%ieronl' a//lies
to an' declarators, and is not /art o( the t'/e ein! de(ined (or name.
&tructures "ust have at least one "e"er declaration. ;e"er declarators "a' contain /recision
8/12/2019 GLSLangSpec.4.30.6
37/196
4 #aria$%e! and &'pe!
co"/ile9ti"e errors. It is le!al to declare an arra' without a si=e and then later re9declare the sa"e na"e
as an arra' o( the sa"e t'/e and s/eci(' a si=e. It is a co"/ile9ti"e error to declare an arra' with a si=e,
and then later in the sa"e shaderC inde the sa"e arra' with an inte!ral constant e/ression !reater than
or eJ;uniform vec< lightHositionI
8/12/2019 GLSLangSpec.4.30.6
38/196
4 #aria$%e! and &'pe!
An arra' o( arra's can e declared as
vec< aI>JI=J; // sie3> arra0 of sie3= arra0 of vec 9; // illegal
In all cases, the inner"ost initiali=er i.e., not a list o( initiali=ers enclosed in curl' racesC a//lied to anoect "ust have the sa"e t'/e as the oect ein! initiali=ed or e a t'/e that can e converted to the
oectMs t'/e accordin! to section ).1.1* ZI"/licit $onversionsZ. In the latter case, an i"/licit conversion
will e done on the initiali=er e(ore the assi!n"ent is done.
struct 5
float a;
int b;
9 e 4 5 16=8 = 9; // legal8 all t0!es match
36
8/12/2019 GLSLangSpec.4.30.6
43/196
4 #aria$%e! and &'pe!
struct 5
float a;
int b;
9 e 4 5 18 > 9; // legal8 first initialier is converted
All o( the (ollowin! declarations result in a co"/ile9ti"e error.
int a 4 true; // illegal
vec< bI=J 4 5 vec(16?) 9; // illegal
struct *1 5
vec< a;
vec< b;
9;
struct 5
float s; float t;
9 dIJ 4 5 *1(vec
8/12/2019 GLSLangSpec.4.30.6
44/196
4 #aria$%e! and &'pe!
@ithin a declaration, the sco/e o( a na"e starts i""ediatel' a(ter the initiali=er i( /resent or i""ediatel'
a(ter the na"e ein! declared i( not. &everal ea"/les:
int x 4 1;
5
int x 4 =8 0 4 x; // 0 is initialied to =
9
struct *
5
int x;
9;
5
* * 4 *(?); // '*' is onl0 visible as a struct and constructor
*; // '*' is now visible as a variable
9
int x 4 x; // &rror if x has not been !reviousl0 defined6
// $f the !revious definition of x was in this
// same sco!e8 this causes a redeclaration error6
int f( /L nested sco!e begins here L/ int k)
5
int k 4 k K >; // redeclaration error of the name k
666
9
int f(int k)
5
5 int k 4 k K >; // =nd k is !arameter8 initialiing nested first k
int m 4 k // use of new k8 which is hiding the !arameter
9
9
4or oth for and while loo/s, the su9state"ent itsel( does not introduce a new sco/e (or variale na"es,
so the (ollowin! has a redeclaration co"/ile9ti"e error:
for ( /L nested sco!e begins here L/ int i 4 ?; i 7 1?; iKK) 5
int i; // redeclaration error
9
38
8/12/2019 GLSLangSpec.4.30.6
45/196
4 #aria$%e! and &'pe!
he od' o( a do&whileloo/ introduces a new sco/e lastin! onl' etween the do and while not includin!
the while test e/ressionC, whether or not the od' is si"/le or co"/ound:
int i 4 1C;
do
int i 4
8/12/2019 GLSLangSpec.4.30.6
46/196
4 #aria$%e! and &'pe!
." Storage :ualifiers
Variale declarations "a' have at "ost one stora!e
8/12/2019 GLSLangSpec.4.30.6
47/196
4 #aria$%e! and &'pe!
8ot all co"inations o(
8/12/2019 GLSLangSpec.4.30.6
48/196
4 #aria$%e! and &'pe!
-owever, the lowest /recedence o/erators o( the se
8/12/2019 GLSLangSpec.4.30.6
49/196
4 #aria$%e! and &'pe!
&ee section 3 Built9in Variales (or a list o( the uilt9in in/ut na"es.
Verte shader in/ut variales or attriutesC receive /er9verte data. he' are declared in a verte shader
with the in
8/12/2019 GLSLangSpec.4.30.6
50/196
4 #aria$%e! and &'pe!
the tessellation control shader. 2er9/atch in/uts "a' e declared as one9di"ensional arra's, ut are not
indeed ' verte nu"er. A//l'in! the !atch
8/12/2019 GLSLangSpec.4.30.6
51/196
4 #aria$%e! and &'pe!
.".' 4niform Variables
he uniform
8/12/2019 GLSLangSpec.4.30.6
52/196
4 #aria$%e! and &'pe!
Individual verte, tessellation evaluation, and !eo"etr' out/uts are declared as in the (ollowin! ea"/les:
out vec> normal;
centroid out vec= ,exMoord;
invariant centroid out vec< Molor;
no!ers!ective out float tem!erature;
flat out vec> m0Molor;
no!ers!ective centroid out vec= m0,exMoord;
sam!le out vec< !er*am!leMolor;
hese can also a//ear in inter(ace locs, as descried in section ).%.5 Inter(ace Blocs. Inter(ace
locs allow si"/ler addition o( arra's to the inter(ace (ro" verte to !eo"etr' shader. he' also allow a
(ra!"ent shader to have the sa"e in/ut inter(ace as a !eo"etr' shader (or a !iven verte shader.
essellation control shader out/ut variales are "a' e used to out/ut /er9verte and /er9/atch data. 2er9
verte out/ut variales are arra'ed see arra(ed under ).%.) In/utsC and declared usin! the out
8/12/2019 GLSLangSpec.4.30.6
53/196
4 #aria$%e! and &'pe!
invocation since, or the value was written ' eactl' one shader invocation since the /revious s'nchroni=ation
/oint, or
the value was written ' "ulti/le shader invocations since the /revious s'nchroni=ation /oint,and the last write /er(or"ed ' all such invocations wrote the sa"e value.
%. @hen read ' a shader invocation, i(
the value was unde(ined at the /revious s'nchroni=ation /oint and has not een writen ' thesa"e shader invocation since, or
the out/ut variale is written to ' an' other shader invocation etween the /revious and nets'nchroni=ation /oints, even i( that assi!n"ent occurs in code (ollowin! the read.
4ra!"ent out/uts out/ut /er9(ra!"ent data and are declared usin! the out stora!e
8/12/2019 GLSLangSpec.4.30.6
54/196
4 #aria$%e! and &'pe!
I( "ulti/le shaders are lined to!ether, then the' will share a sin!le !loal u((er variale na"e s/ace,
includin! within a lan!ua!e as well as across lan!ua!es. -ence, the t'/es o( u((er variales with the
sa"e na"e "ust "atch across all shaders that are lined into a sin!le /ro!ra".
.".3 Shared Variables
he shared
8/12/2019 GLSLangSpec.4.30.6
55/196
4 #aria$%e! and &'pe!
An inter(ace loc is started ' an in, out, uniform, or buffer e'word, (ollowed ' a loc na"e,
(ollowed ' an o/en curl' race 5 C as (ollows:
inter%ace-'loc :
la(out-+uali%iero!t inter%ace-+uali%ier 'loc-name 5mem'er-list 6instance-nameo!t>
inter%ace-+uali%ier :
in
out
uniform
buffer
mem'er-list :
mem'er-declaration
mem'er-declaration mem'er-list
mem'er-declaration :
la(out-+uali%iero!t +uali%ierso!t t(*e declarators >
instance-name :
identi%ier
identi%ier 9
identi%ier 9 integral-constant-e!*ression
Each o( the aove ele"ents is discussed elow, with the ece/tion o( la'out
8/12/2019 GLSLangSpec.4.30.6
56/196
4 #aria$%e! and &'pe!
out/ut, or uni(or" variale consistent with the inter(ace
8/12/2019 GLSLangSpec.4.30.6
57/196
4 #aria$%e! and &'pe!
I( an instance na"e instance-nameCis not used, the na"es declared inside the loc are sco/ed at the
!loal level and accessed as i( the' were declared outside the loc. I( an instance na"e instance-nameC
is used, then it /uts all the "e"ers inside a sco/e within its own na"e s/ace, accessed with the (ield
selector . C o/erator analo!ousl' to structuresC. 4or ea"/le,
in #ight 5
vec< #ightHos;
vec> #ightMolor;
9;
in Molored,exture 5
vec< Molor;
vec= ,exMoord;
9 Gaterial; // instance name
vec> Molor; // different Molor than Gaterial6Molor
vec< #ightHos; // illegal8 alread0 defined
666
666 4 #ightHos; // accessing #ightHos
666 4 Gaterial6Molor; // accessing Molor in Molored,exture block
+utside the shadin! lan!ua!e i.e., in the A2IC, "e"ers are si"ilarl' identi(ied ece/t the loc na"e is
alwa's used in /lace o( the instance na"e A2I accesses are to inter(aces, not to shadersC. I( there is no
instance na"e, then the A2I does not use the loc na"e to access a "e"er, ust the "e"er na"e.
out ertex 5
vec< Hosition; // DH$ transform/feedback will use ertex6Hosition
vec= ,exture;
9 Moords; // shader will use Moords6Hosition
out ertex= 5
vec< Molor; // DH$ will use Molor
9;
51
8/12/2019 GLSLangSpec.4.30.6
58/196
4 #aria$%e! and &'pe!
4or locs declared as arra's, the arra' inde "ust also e included when accessin! "e"ers, as in this
ea"/le
uniform ,ransform 5 // DH$ uses ,ransformI=J to refer to instance =
mat< GodeliewGatrix;
mat< GodeliewHroectionGatrix;
vec< aIJ; // arra0 will get im!licitl0 sied
float -eformation;
9 transformsIJ666 // if these are the onl0 two dereferences of 'a'8
666transformsI>J6aICJ666 // then 'a' must be sie F8 for all transformsIxJ
4or uni(or" or shader stora!e locs declared as an arra', each individual arra' ele"ent corres/onds to a
se/arate u((er9oect ind ran!e, acin! one instance o( the loc. As the arra' si=e indicates the
nu"er o( u((er oects needed, uni(or" and shader stora!e loc arra' declarations "ust s/eci(' an
arra' si=e. A uni(or" or shader stora!e loc arra' can onl' e indeed with a d'na"icall' uni(or"
inte!ral e/ression, otherwise results are unde(ined.
@hen usin! +/enGL A2I entr' /oints to identi(' the na"e o( an individual loc in an arra' o( locs,
the na"e strin! "ust include an arra' inde e.!., rans%orm92C. @hen usin! +/enGL A2I entr' /oints
to re(er to o((sets or other characteristics o( a loc "e"er, an arra' inde "ust not e s/eci(ied e.!.,
rans%orm.odelVie)atri! C.
Geo"etr' shader in/ut locs "ust e declared as arra's and (ollow the arra' declaration and linin!
rules (or all !eo"etr' shader in/uts. All other in/ut and out/ut loc arra's "ust s/eci(' an arra' si=e.
here are i"/le"entation de/endent li"its on the nu"er o( uni(or" locs and the nu"er o( shader
stora!e locs that can e used /er sta!e. I( either li"it is eceeded, it will cause a lin9ti"e error.
. Layout :ualifiers
La'out
8/12/2019 GLSLangSpec.4.30.6
59/196
4 #aria$%e! and &'pe!
he la(out-+uali%ier e/ands to
la(out-+uali%ier :
layout 3 la(out-+uali%ier-id-list4
la(out-+uali%ier-id-list :
la(out-+uali%ier-id
la(out-+uali%ier-id (la(out-+uali%ier-id-list
la(out-+uali%ier-id
la(out-+uali%ier-name
la(out-+uali%ier-name la(out-+uali%ier-value
shared
he toens used (or la(out-+uali%ier-name are identi(iers, not e'words, however, the shared e'word is
allowed as a la(out-+uali%ier-id. Generall', the' can e listed in an' order. +rder9de/endent "eanin!s
eist onl' i( e/licitl' called out elow. &i"ilarl', these identi(iers are not case sensitive, unless
e/licitl' noted otherwise.
;ore than one la'out
8/12/2019 GLSLangSpec.4.30.6
60/196
4 #aria$%e! and &'pe!
I( the declared in/ut is an n m sin!le9 or doule9/recision "atri, it will e assi!ned "ulti/le locations
startin! with the location s/eci(ied. he nu"er o( locations assi!ned (or each "atri will e the sa"e as
(or an n9ele"ent arra' o( m9co"/onent vectors. 4or ea"/le,
la0out(location 4 A) in mat< transformsI=J;
will estalish that shader in/ut trans%ormsis assi!ned to vector locations 5910, with trans%orms90ein!
assi!ned to locations 591# and trans%orms91ein! assi!ned to locations 1%910.
I( the declared in/ut is a structure, its "e"ers will e assi!ned consecutive locations in the order o(
declaration, with the (irst "e"er assi!ned the location s/eci(ied (or the structure. he nu"er o(
locations consu"ed ' a structure "e"er is deter"ined ' a//l'in! the rules aove recursivel' as
thou!h the structure "e"er were declared as an in/ut variale o( the sa"e t'/e. 4or ea"/le,
la0out(location 4 >) struct * 5
vec> a;
mat= b;
vec< cI=J;
9 s;
will assi!n location % tos.a, locations ) and to the two colu"n vectors o(s.', and locations 0 and 3 to
s.c.
Location la'out
8/12/2019 GLSLangSpec.4.30.6
61/196
4 #aria$%e! and &'pe!
..1.1 Tessellation %#aluation Inputs
Additional in/ut la'out
8/12/2019 GLSLangSpec.4.30.6
62/196
4 #aria$%e! and &'pe!
counter9clocwise verte orderin!, res/ectivel'. I( a /oint "ode declaration is o"itted, the tessellation
/ri"itive !enerator will /roduce lines or trian!les accordin! to the /ri"itive "ode.
..1.2 Geometry Shader InputsAdditional la'out
8/12/2019 GLSLangSpec.4.30.6
63/196
4 #aria$%e! and &'pe!
he intrinsicall' declared in/ut arra'gl_in9will also e si=ed ' an' in/ut /ri"itive9la'out declaration.
-ence, the e/ression
gl"in6length()
will return the value (ro" the tale aove.
4or in/uts declared without an arra' si=e, includin! intrinsicall' declared in/uts i.e., gl_inC, a la'out "ust
e declared e(ore an' use o( the "ethod lengthor other an' arra' use that reI>J; // illegal8 in!ut sies are inconsistentla0out(lines) in; // legal for Molor=8 in!ut sie is =8 matching Molor=
in vec< MolorJ; // illegal8 contradicts la0out of lines
la0out(lines) in; // legal8 matches other la0out() declaration
la0out(triangles) in;// illegal8 does not match earlier la0out() declaration
It is a lin9ti"e error i( not all /rovided si=es si=ed in/ut arra's and la'out si=eC "atch across all
!eo"etr' shaders in a /ro!ra".
..1." ,ragment Shader Inputs
Additional (ra!"ent la'out
8/12/2019 GLSLangSpec.4.30.6
64/196
4 #aria$%e! and &'pe!
Redeclarations are done as (ollows
in vec< gl"ragMoord; // redeclaration that changes nothing is allowed
// Dll the following are allowed redeclaration that change behavior
la0out(origin"u!!er"left) in vec< gl"ragMoord;
la0out(!ixel"center"integer) in vec< gl"ragMoord;
la0out(origin"u!!er"left8 !ixel"center"integer) in vec< gl"ragMoord;
I(gl_"rag&oord is redeclared in an' (ra!"ent shader in a /ro!ra", it "ust e redeclared in all the
(ra!"ent shaders in that /ro!ra" that have a static use gl_"rag&oord. All redeclarations o(
gl_"rag&oord in all (ra!"ent shaders in a sin!le /ro!ra" "ust have the sa"e set o(
8/12/2019 GLSLangSpec.4.30.6
65/196
4 #aria$%e! and &'pe!
4or ea"/le, the (ollowin! declaration in a co"/ute shader
la0out (local"sie"x 4 >=8 local"sie"0 4 >=) in;
is used to declare a two9di"ensional co"/ute shader with a local si=e o( %# \ %# ele"ents, which is
e
8/12/2019 GLSLangSpec.4.30.6
66/196
4 #aria$%e! and &'pe!
4or (ra!"ent9shader out/uts, the location and inde s/eci(' the color out/ut nu"er and inde receivin!
the values o( the out/ut. 4or out/uts o( all other shader sta!es, the location s/eci(ies a vector nu"er that
can e used to "atch a!ainst in/uts in a suseJ;
will estalish that colorsis assi!ned to vector location nu"ers #, %, and ).
I( the declared out/ut is an n m sin!le9 or doule9/recision "atri, it will e assi!ned "ulti/le locations
startin! with the location s/eci(ied. he nu"er o( locations assi!ned will e the sa"e as (or an n9
ele"ent arra' o( m9co"/onent vectors.
I( the declared out/ut is a structure, its "e"ers will e assi!ned consecutive locations in the order o(
declaration, with the (irst "e"er assi!ned the location s/eci(ied (or the structure. he nu"er o(
locations consu"ed ' a structure "e"er is deter"ined ' a//l'in! the rules aove recursivel' as
thou!h the structure "e"er were declared as an out/ut variale o( the sa"e t'/e.
Location la'out
8/12/2019 GLSLangSpec.4.30.6
67/196
4 #aria$%e! and &'pe!
4or the /ur/oses o( deter"inin! i( a non9(ra!"ent out/ut "atches an in/ut (ro" a suse
8/12/2019 GLSLangSpec.4.30.6
68/196
4 #aria$%e! and &'pe!
he /ri"itive t'/e identi(iers !oints, line"stri!, and triangle"stri!are used to s/eci(' the t'/e o( out/ut
/ri"itive /roduced ' the !eo"etr' shader, and onl' one o( these is acce/ted. At least one !eo"etr'
shader co"/ilation unitC in a /ro!ra" "ust declare an out/ut /ri"itive t'/e, and all !eo"etr' shader
out/ut /ri"itive t'/e declarations in a /ro!ra" "ust declare the sa"e /ri"itive t'/e. It is not re
8/12/2019 GLSLangSpec.4.30.6
69/196
4 #aria$%e! and &'pe!
Each out/ut loc or non9loc out/ut variale is associated with a verte strea". I( the loc or variale
is declared with the strea" identi(ier, it is associated with the s/eci(ied strea"F otherwise, it is associated
with the current de(ault strea". A loc "e"er "a' e declared with a strea" identi(ier, ut the
s/eci(ied strea" "ust "atch the strea" associated with the containin! loc. +ne ea"/le:
la0out(stream41) out; // default is now stream 1
out vec< var1; // var1 gets default stream (1)
la0out(stream4=) out :lock1 5 // N:lock1N belongs to stream =
la0out(stream4=) vec< var=; // redundant block member stream decl
la0out(stream4>) vec= var>; // $##&.D# (must match block stream)
vec> var) out vec< varC; // varC belongs to stream >
Each verte e"itted ' the !eo"etr' shader is assi!ned to a s/eci(ic strea", and the attriutes o( the
e"itted verte are taen (ro" the set o( out/ut locs and variales assi!ned to the tar!eted strea". A(ter
each verte is e"itted, the values o( all out/ut variales eco"e unde(ined. Additionall', the out/ut
variales associated with each verte strea" "a' share stora!e. @ritin! to an out/ut variale associated
with one strea" "a' overwrite out/ut variales associated with an' other strea". @hen e"ittin! each
verte, a !eo"etr' shader should write to all out/uts associated with the strea" to which the verte will
e e"itted and to no out/uts associated with an' other strea".
I( a !eo"etr' shader out/ut loc or variale is declared "ore than once, all such declarations "ust
associate the variale with the sa"e verte strea". I( an' strea" declaration s/eci(ies a non9eistent
strea" nu"er, the shader will (ail to co"/ile.
Built9in !eo"etr' shader out/uts are alwa's associated with verte strea" =ero.
All !eo"etr' shader out/ut la'out declarations in a /ro!ra" "ust declare the sa"e la'out and sa"e value
(or max"vertices. I( !eo"etr' shaders are in a /ro!ra", there "ust e at least one !eo"etr' out/ut
la'out declaration so"ewhere in that /ro!ra", ut not all !eo"etr' shaders co"/ilation unitsC are
re
8/12/2019 GLSLangSpec.4.30.6
70/196
4 #aria$%e! and &'pe!
4or ea"/le:
la0out (de!th"greater) out float gl"rag-e!th;
he la'out
8/12/2019 GLSLangSpec.4.30.6
71/196
4 #aria$%e! and &'pe!
la(out-+uali%ier-id
location ; integer-constant
he location identi(ier can e used with de(ault9loc uni(or" variales and suroutine uni(or"s. he
location s/eci(ies the location ' which the +/enGL A2I can re(erence the uni(or" and u/date its value.
Individual ele"ents o( a uni(or" arra' are assi!ned consecutive locations with the (irst ele"ent tain!
location location. 8o two de(ault9loc uni(or" variales in the /ro!ra" can have the sa"e location,
even i( the' are unused, otherwise a co"/ile9ti"e or lin9ti"e error will e !enerated. 8o two suroutine
uni(or" variales can have the sa"e location in the sa"e shader sta!e, otherwise a co"/ile9ti"e or lin9
ti"e error will e !enerated. Valid locations (or de(ault9loc uni(or" variale locations are in the ran!e
o( * to the i"/le"entation9de(ined "ai"u" nu"er o( uni(or" locations "inus one. Valid locations (or
suroutine uni(or"s are in the ran!e o( * to the i"/le"entation9de(ined /er9sta!e "ai"u" nu"er o(
suroutine uni(or" locations "inus one.
Locations can e assi!ned to de(ault9loc uni(or" arra's and structures. he (irst inner9"ost scalar,
vector or "atri "e"er or ele"ent taes the s/eci(ied locationand the co"/iler assi!ns the net inner9
"ost "e"er or ele"ent the net incre"ental location value. Each suse
8/12/2019 GLSLangSpec.4.30.6
72/196
4 #aria$%e! and &'pe!
..' 4niform and Shader Storage -loc0 Layout :ualifiers
La'out
8/12/2019 GLSLangSpec.4.30.6
73/196
4 #aria$%e! and &'pe!
co"e (ro" a declaration outside the loc de(initionC. his allows use o( the sa"e u((er to ac the sa"e
loc de(inition across di((erent /ro!ra"s.
he*aced
8/12/2019 GLSLangSpec.4.30.6
74/196
4 #aria$%e! and &'pe!
4or ea"/le
la0out(row"maor8 column"maor)
results in the
8/12/2019 GLSLangSpec.4.30.6
75/196
4 #aria$%e! and &'pe!
// in one com!ilation unit666
la0out(binding4>) uniform sam!ler=- s; // s bound to unit >
// in another com!ilation unit666uniform sam!ler=- s; // oka08 s still bound at >
// in another com!ilation unit666
la0out(binding4
8/12/2019 GLSLangSpec.4.30.6
76/196
4 #aria$%e! and &'pe!
Ea"/le valid uni(or" declarations, assu"in! to/ o( shader:
la0out (binding4>8 offset4) uniform atomic"uint c; // offset 4 F
la0out (binding4=) uniform atomic"uint d; // offset 4 (float i8 float 8 !recise out float k)
5
k 4 i L i K ; // !recise8 due to 7kQ declaration
9
void main()
5
vec> r 4 vec>(a L b); // !recise8 used to com!ute v6x0
vec> s 4 vec>(c L d); // !recise8 used to com!ute v6x0
v6x0 4 r K s; // !recise
v6w 4 (a6w L b6w) K (c6w L d6w); // !recise
v6x 4 func(a6x8 b6x8 c6x8 d6x); // values com!uted in func()
// are %+, !recise v6x 4 func=(a6x8 b6x8 c6x8 d6x); // !reciseR
func>(a6x L b6x8 c6x L d6x8 v6x); // !reciseR
9
4or the /ur/oses o( deter"inin! i( an out/ut (ro" one shader sta!e "atches an in/ut o( the net sta!e, the
!recise
8/12/2019 GLSLangSpec.4.30.6
88/196
4 #aria$%e! and &'pe!
Variales declared as i"a!e t'/es can
8/12/2019 GLSLangSpec.4.30.6
89/196
4 #aria$%e! and &'pe!
ecause the co"/iler can assu"e that the underl'in! i"a!e wonMt e read or written ' other code.
A//lications are res/onsile (or ensurin! that i"a!e "e"or' re(erenced ' variales
8/12/2019 GLSLangSpec.4.30.6
90/196
4 #aria$%e! and &'pe!
vec< funcD(restrict image=- a) 5 666 9
vec< func:(image=- a) 5 666 9
la0out(rgba>=f) uniform image=- img1;
la0out(rgba>=f) coherent uniform image=- img=;
funcD(img1); // +S8 adding NrestrictN is allowed
func:(img=); // illegal8 stri!!ing NcoherentN is not
La'out
8/12/2019 GLSLangSpec.4.30.6
91/196
' Operators and %*pressions
'.1 Operators
he +/enGL &hadin! Lan!ua!e has the (ollowin! o/erators.
+recedence Operator Class Operators ssociati#ity
1 hi!hestC /arenthetical !rou/in! 3 4 8A
#
arra' suscri/t(unction call and constructor structure(ield or "ethod selector, swi==le
/ost (i incre"ent and decre"ent
2
3 4.
++ &&
Le(t to Ri!ht
%/re(i incre"ent and decre"entunar'
++ &&+ & : v8 u;
float f;
v 4 u K f;
will e e v8 u8 w;
w 4 v K u;
will e e v8 u;mat> m;
u 4 v L m;
is e
8/12/2019 GLSLangSpec.4.30.6
104/196
5 Operator! and ()pre!!ion!
u6x 4 dot(v8 mI?J); // mI?J is the left column of m
u60 4 dot(v8 mI1J); // dot(a8b) is the inner (dot) !roduct of a and b
u6 4 dot(v8 mI=J);
And
u 4 m L v;
is e m8 n8 r;
r 4 m L n;
is e
8/12/2019 GLSLangSpec.4.30.6
105/196
( Statements and Structure
he (unda"ental uildin! locs o( the +/enGL &hadin! Lan!ua!e are:
state"ents and declarations
(unction de(initions
selection if&else and switch&case&defaultC
iterationfor( while( and do&whileC
u"/sdiscard( return( break( and continueC
he overall structure o( a shader is as (ollows
translation-unit:
glo'al-declaration
translation-unit glo'al-declaration
glo'al-declaration:
%unction-de%inition
declaration
hat is, a shader is a se
8/12/2019 GLSLangSpec.4.30.6
106/196
6 State*ent! and Structure
$url' races are used to !rou/ se
8/12/2019 GLSLangSpec.4.30.6
107/196
6 State*ent! and Structure
&ee section 5 &hadin! Lan!ua!e Gra""ar (or the de(initive re(erence on the s'nta to declare and
de(ine (unctions.
All (unctions "ust e either declared with a /rotot'/e or de(ined with a od' e(ore the' are called. 4or
ea"/le:
float m0func (float f8 // f is an in!ut !arameter
out float g); // g is an out!ut !arameter
4unctions that return no value "ust e declared as void. A void (unction can onl' use returnwithout a
return ar!u"ent, even i( the return ar!u"ent has void t'/e. Return state"ents onl' acce/t values:
void (unc1C _ `
void (unc#C _ return (unc1CF ` ille!al return state"ent
+nl' a /recision
8/12/2019 GLSLangSpec.4.30.6
108/196
6 State*ent! and Structure
there is no (unction ar!u"ent (or which the conversion in>is etter than the corres/ondin!
conversion inA.
I( a sin!le (unction declaration is considered a etter "atch than ever' other "atchin! (unction
declaration, it will e used. +therwise, a co"/ile9ti"e se"antic error (or an a"i!uous overloaded
(unction call occurs.
o deter"ine whether the conversion (or a sin!le ar!u"ent in one "atch is etter than that (or another
"atch, the (ollowin! rules are a//lied, in order:
1. An eact "atch is etter than a "atch involvin! an' i"/licit conversion.
#. A "atch involvin! an i"/licit conversion (ro" floatto doubleis etter than a "atch involvin!
an' other i"/licit conversion.
%. A "atch involvin! an i"/licit conversion (ro" either int or uint to float is etter than a "atch
involvin! an i"/licit conversion (ro" either int or uint to double.
I( none o( the rules aove a//l' to a /articular /air o( conversions, neither conversion is considered etter
than the other.
4or the ea"/le (unction /rotot'/es AC, BC, and $C aove, the (ollowin! ea"/les show how the rules
a//l' to di((erent sets o( callin! ar!u"ent t'/es:
f(vec
8/12/2019 GLSLangSpec.4.30.6
109/196
6 State*ent! and Structure
(.1.1 ,unction Calling Con#entions
4unctions are called ' value9return. his "eans in/ut ar!u"ents are co/ied into the (unction at call ti"e,
and out/ut ar!u"ents are co/ied ac to the caller e(ore (unction eit. Because the (unction wors with
local co/ies o( /ara"eters, there are no issues re!ardin! aliasin! o( variales within a (unction. o
control what /ara"eters are co/ied in andor out throu!h a (unction de(inition or declaration:
he e'word inis used as a
8/12/2019 GLSLangSpec.4.30.6
110/196
6 State*ent! and Structure
%unction-*rotot(*e :
*recision-+uali%ier t(*e %unction-name7*arameter-+uali%iers *recision-+uali%ier t(*e name
arra(-s*eci%ier ... 8
t(*e :
an' asic t'/e, arra' t'/e, structure na"e, or structure de%inition
*arameter-+uali%iers :
e"/t'
list o(*arameter-+uali%ier
*arameter-+uali%ier :
const
in
out
inout
!recise
memor( +uali%ier
*recision +uali%ier
name:
e"/t'
identi(ier
arra(-s*eci%ier :
e"/t'
integral-constant-e!*ression2
he const
8/12/2019 GLSLangSpec.4.30.6
111/196
6 State*ent! and Structure
are uni(or"s, and are assi!ned to s/eci(ic (unctions onl' throu!h co""ands TniformSubroutinesuivC in
the +/enGL A2I.
&uroutine t'/es are declared usin! a state"ent si"ilar to a (unction declaration, with the subroutine
e'word, as (ollows:
subroutine return,0!e subroutine,0!e%ame(t0!e? arg?8 t0!e1 arg18
6668 t0!en argn);
As with (unction declarations, the (or"al ar!u"ent na"es args aoveC are o/tional. 4unctions are
associated with suroutine t'/es o( "atchin! declarations ' de(inin! the (unction with the subroutine
e'word and a list o( suroutine t'/es the (unction "atches:
subroutine(subroutine,0!e%ame?8 6668 subroutine,0!e%ame%)
return,0!e function%ame(t0!e? arg?8 t0!e1 arg18 6668 t0!en argn)
5 666 9 // function bod0
It is a co"/ile9ti"e error i( ar!u"ents and return t'/e donMt "atch etween the (unction and eachassociated suroutine t'/e.
4unctions declared with subroutine "ust include a od'. An overloaded (unction cannot e declared
with subroutineF a /ro!ra" will (ail to co"/ile or lin i( an' shader or sta!e contains two or "ore
(unctions with the sa"e na"e i( the na"e is associated with a suroutine t'/e.
&uroutine t'/e variales are re
8/12/2019 GLSLangSpec.4.30.6
112/196
6 State*ent! and Structure
s)itc,-statement :
case constant-e!*ression =
default =
statement
I( an if&e/ression evaluates to true, then the (irststatementis eecuted. I( it evaluates to falseand there
is an else/art then the secondstatementis eecuted.
An' e/ression whose t'/e evaluates to a Boolean can e used as the conditional e/ression 'ool-
e!*ression. Vector t'/es are not acce/ted as the e/ression to if.
$onditionals can e nested.
he t'/e o( the init-e!*ressionvalue in a switch state"ent "ust e a scalar int or uint. he t'/e o( the
constant-e!*ressionvalue in a case lael