Você está na página 1de 199

The OpenGL

Shading Language
Language Version: 4.30
Document Revision: 8
7-Feb-2013
Editor: John Kessenich, LunarG
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
Copyright (c) 2008-2013 The Khronos Group Inc. All Rights Resere!.
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 repro!uce!' repu$lishe!' !istri$ute!' tr#ns&itte!' !ispl#ye!'
$ro#!c#st or other%ise e(ploite! in #ny &#nner %ithout the e(press prior %ritten per&ission o" Khronos
Group. )ou &#y use this speci"ic#tion "or i&ple&enting the "unction#lity therein' %ithout #ltering or
re&oing #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 coney #ny rights to repro!uce' !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 A!opter &e&$er o"
Khronos to copy #n! re!istri$ute ,-./0I1I20 ersions o" this speci"ic#tion in #ny "#shion' proi!e! th#t
-/ C3ARG2 is &#!e "or the speci"ic#tion #n! the l#test ##il#$le up!#te o" the speci"ic#tion "or #ny
ersion o" the A+I is use! %heneer possi$le. 4uch !istri$ute! speci"ic#tion &#y $e re-"or&#tte! A4
5/-G A4 the contents o" the speci"ic#tion #re not ch#nge! in #ny %#y. The speci"ic#tion &#y $e
incorpor#te! into # pro!uct th#t is sol! #s long #s such pro!uct inclu!es signi"ic#nt in!epen!ent %or*
!eelope! $y the seller. A lin* to the current ersion o" this speci"ic#tion on the Khronos Group %e$-site
shoul! $e inclu!e! %heneer 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 or
i&plie!' reg#r!ing this speci"ic#tion' inclu!ing' %ithout li&it#tion' #ny i&plie! %#rr#nties o" &erch#nt#$ility
or "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#r!ing the correctness' #ccur#cy'
co&pleteness' ti&eliness' #n! reli#$ility o" the speci"ic#tion. ,n!er no circu&st#nces %ill the Khronos
Group' or #ny o" its +ro&oters' Contri$utors or .e&$ers or their respectie p#rtners' o""icers' !irectors'
e&ployees' #gents or represent#ties $e li#$le "or #ny !#&#ges' %hether !irect' in!irect' speci#l or
conse6uenti#l !#&#ges "or lost reenues' lost pro"its' or other%ise' #rising "ro& or in connection %ith
these &#teri#ls.
Khronos' /penK/02' /penK/G4' /pen7G' /pen.A8' /pen45 24 #n! /pen91 #re tr#!e&#r*s o"
the Khronos Group Inc. C/55A0A is # tr#!e&#r* o" 4ony Co&puter 2ntert#in&ent Inc. use! $y
per&ission $y Khronos. /penG5 #n! /pen.5 #re registere! tr#!e&#r*s #n! the /penG5 24 logo is #
tr#!e&#r* o" 4ilicon Gr#phics Inc. use! $y per&ission $y Khronos. All other pro!uct n#&es' tr#!e&#r*s'
#n!:or co&p#ny n#&es #re use! solely "or i!enti"ic#tion #n! $elong to their respectie o%ners.
ii
Table of Contents
1 Introduction.................................................................................................................................1
1.1 Acnowled!"ents................................................................................................................#
1.# $han!es................................................................................................................................%
1.#.1 $han!es &ro" Revision ' o& Version (.%)...................................................................%
1.#.# $han!es &ro" Revision * o& Version (.%)...................................................................%
1.#.% +u""ar, o& $han!es &ro" Version (.#).....................................................................%
1.% -verview...............................................................................................................................
1.( Error /andlin!......................................................................................................................*
1.. 0,1o!ra1hical $onventions.................................................................................................*
1.* De1recation..........................................................................................................................*
# -verview o& -1enGL +hadin!....................................................................................................'
#.1 Verte2 3rocessor..................................................................................................................'
#.# 0essellation $ontrol 3rocessor.............................................................................................'
#.% 0essellation Evaluation 3rocessor........................................................................................4
#.( Geo"etr, 3rocessor.............................................................................................................4
#.. 5ra!"ent 3rocessor..............................................................................................................4
#.* $o"1ute 3rocessor...............................................................................................................4
% Basics........................................................................................................................................1)
%.1 $haracter +et......................................................................................................................1)
%.# +ource +trin!s....................................................................................................................1)
%.% 3re1rocessor.......................................................................................................................11
%.( $o""ents..........................................................................................................................1*
%.. 0oens................................................................................................................................1'
%.* Ke,words............................................................................................................................1'
%.' Identi&iers...........................................................................................................................16
%.4 De&initions..........................................................................................................................16
%.4.1 +tatic 7se....................................................................................................................#)
%.4.# 7ni&or" and 8on97ni&or" $ontrol 5low..................................................................#)
%.4.% D,na"icall, 7ni&or" E21ressions.............................................................................#)
( Varia:les and 0,1es..................................................................................................................#1
(.1 Basic 0,1es........................................................................................................................#1
(.1.1 Void............................................................................................................................#.
(.1.# Booleans.....................................................................................................................#.
(.1.% Inte!ers.......................................................................................................................#.
(.1.( 5loatin!93oint Varia:les.............................................................................................#'
(.1.. Vectors........................................................................................................................#4
(.1.* ;atrices......................................................................................................................#4
(.1.' -1a<ue 0,1es.............................................................................................................#6
iii
(.1.'.1 +a"1lers.............................................................................................................#6
(.1.'.# I"a!es.................................................................................................................%)
(.1.'.% Ato"ic $ounters.................................................................................................%)
(.1.4 +tructures....................................................................................................................%)
(.1.6 Arra,s.........................................................................................................................%1
(.1.1) I"1licit $onversions................................................................................................%.
(.1.11 Initiali=ers.................................................................................................................%*
(.# +co1in!...............................................................................................................................%4
(.% +tora!e >uali&iers...............................................................................................................(1
(.%.1 De&ault +tora!e >uali&ier............................................................................................(#
(.%.# $onstant >uali&ier......................................................................................................(#
(.%.% $onstant E21ressions.................................................................................................(#
(.%.( In1ut Varia:les...........................................................................................................(%
(.%.. 7ni&or" Varia:les......................................................................................................(*
(.%.* -ut1ut Varia:les.........................................................................................................(*
(.%.' Bu&&er Varia:les.........................................................................................................(4
(.%.4 +hared Varia:les.........................................................................................................(6
(.%.6 Inter&ace Blocs..........................................................................................................(6
(.( La,out >uali&iers.................................................................................................................%
(.(.1 In1ut La,out >uali&iers................................................................................................(
(.(.1.1 0essellation Evaluation In1uts.............................................................................*
(.(.1.# Geo"etr, +hader In1uts.......................................................................................'
(.(.1.% 5ra!"ent +hader In1uts.......................................................................................4
(.(.1.( $o"1ute +hader In1uts........................................................................................6
(.(.# -ut1ut La,out >uali&iers............................................................................................*)
(.(.#.1 0essellation $ontrol -ut1uts..............................................................................*#
(.(.#.# Geo"etr, -ut1uts...............................................................................................*#
(.(.#.% 5ra!"ent -ut1uts...............................................................................................*(
(.(.% 7ni&or" Varia:le La,out >uali&iers...........................................................................*.
(.(.( +u:routine 5unction La,out >uali&iers......................................................................**
(.(.. 7ni&or" and +hader +tora!e Bloc La,out >uali&iers...............................................*'
(.(.* -1a<ue97ni&or" La,out >uali&iers............................................................................*6
(.(.*.1 Ato"ic $ounter La,out >uali&iers.....................................................................')
(.(.*.# 5or"at La,out >uali&iers....................................................................................'1
(.. Inter1olation >uali&iers......................................................................................................'%
(...1 Redeclarin! Built9in Inter1olation Varia:les in the $o"1ati:ilit, 3ro&ile.................'(
(.* 3ara"eter >uali&iers...........................................................................................................'.
(.' 3recision and 3recision >uali&iers.....................................................................................'.
(.'.1 Ran!e and 3recision...................................................................................................'.
iv
(.'.# 3recision >uali&iers....................................................................................................'*
(.'.% De&ault 3recision >uali&iers.......................................................................................''
(.'.( Availa:le 3recision >uali&iers....................................................................................'4
(.4 Variance and the Invariant >uali&ier..................................................................................'4
(.4.1 0he Invariant >uali&ier...............................................................................................'4
(.4.# Invariance o& $onstant E21ressions...........................................................................'6
(.6 0he 3recise >uali&ier..........................................................................................................'6
(.1) ;e"or, >uali&iers...........................................................................................................4#
(.11 -rder o& >uali&ication......................................................................................................4.
. -1erators and E21ressions........................................................................................................4*
..1 -1erators............................................................................................................................4*
..# Arra, -1erations...............................................................................................................4'
..% 5unction $alls....................................................................................................................4'
..( $onstructors.......................................................................................................................4'
..(.1 $onversion and +calar $onstructors..........................................................................4'
..(.# Vector and ;atri2 $onstructors.................................................................................44
..(.% +tructure $onstructors................................................................................................6)
..(.( Arra, $onstructors.....................................................................................................61
... Vector and +calar $o"1onents and Len!th.......................................................................61
..* ;atri2 $o"1onents............................................................................................................6%
..' +tructure and Arra, -1erations..........................................................................................6%
..4 Assi!n"ents.......................................................................................................................6(
..6 E21ressions........................................................................................................................6.
..1) Vector and ;atri2 -1erations..........................................................................................64
..11 -ut9o&9Bounds Accesses..................................................................................................66
* +tate"ents and +tructure.........................................................................................................1))
*.1 5unction De&initions.........................................................................................................1)1
*.1.1 5unction $allin! $onventions..................................................................................1)(
*.1.# +u:routines...............................................................................................................1).
*.# +election...........................................................................................................................1)*
*.% Iteration............................................................................................................................1)'
*.( Ju"1s................................................................................................................................1)4
' Built9in Varia:les....................................................................................................................11)
'.1 Built9In Lan!ua!e Varia:les............................................................................................11)
'.1.1 $o"1ati:ilit, 3ro&ile Built9In Lan!ua!e Varia:les..................................................116
'.# $o"1ati:ilit, 3ro&ile Verte2 +hader Built9In In1uts.......................................................1##
'.% Built9In $onstants............................................................................................................1#%
'.%.1 $o"1ati:ilit, 3ro&ile Built9In $onstants..................................................................1#(
'.( Built9In 7ni&or" +tate.....................................................................................................1#.
v
'.(.1 $o"1ati:ilit, 3ro&ile +tate.......................................................................................1#.
4 Built9in 5unctions...................................................................................................................1#6
4.1 An!le and 0ri!ono"etr, 5unctions..................................................................................1%)
4.# E21onential 5unctions......................................................................................................1%#
4.% $o""on 5unctions..........................................................................................................1%%
4.( 5loatin!93oint 3ac and 7n1ac 5unctions.....................................................................1%4
4.. Geo"etric 5unctions........................................................................................................1()
4.* ;atri2 5unctions..............................................................................................................1(#
4.' Vector Relational 5unctions.............................................................................................1((
4.4 Inte!er 5unctions..............................................................................................................1(*
4.6 0e2ture 5unctions.............................................................................................................1(4
4.6.1 0e2ture >uer, 5unctions..........................................................................................1(6
4.6.# 0e2el Loou1 5unctions...........................................................................................1.#
4.6.% 0e2ture Gather 5unctions.........................................................................................1.4
4.6.( $o"1ati:ilit, 3ro&ile 0e2ture 5unctions..................................................................1*1
4.1) Ato"ic9$ounter 5unctions.............................................................................................1*%
4.11 Ato"ic ;e"or, 5unctions............................................................................................1*%
4.1# I"a!e 5unctions.............................................................................................................1*(
4.1% 5ra!"ent 3rocessin! 5unctions......................................................................................1*4
4.1%.1 Derivative 5unctions..............................................................................................1*4
4.1%.# Inter1olation 5unctions...........................................................................................1*6
4.1( 8oise 5unctions..............................................................................................................1')
4.1. Geo"etr, +hader 5unctions...........................................................................................1'#
4.1* +hader Invocation $ontrol 5unctions.............................................................................1'(
4.1' +hader ;e"or, $ontrol 5unctions................................................................................1'.
6 +hadin! Lan!ua!e Gra""ar &or $ore 3ro&ile........................................................................1''
1) 8or"ative Re&erences...........................................................................................................16%
vi
1 Introduction
0his docu"ent s1eci&ies onl, version (.%) o& the -1enGL +hadin! Lan!ua!e. It re<uires ??VER+I-8??
to su:stitute (%), and re<uires #version to acce1t onl, (%). I& #version is declared with a s"aller
nu":er, the lan!ua!e acce1ted is a 1revious version o& the shadin! lan!ua!e, which will :e su11orted
de1endin! on the version and t,1e o& conte2t in the -1enGL A3I. +ee the -1enGL Gra1hics +,ste"
+1eci&ication, Version (.%, &or details on what lan!ua!e versions are su11orted.
3revious versions o& the -1enGL +hadin! Lan!ua!e, as well as the -1enGL E+ +hadin! Lan!ua!e, are
not strict su:sets o& the version s1eci&ied here, 1articularl, with res1ect to 1recision, na"e9hidin! rules,
and treat"ent o& inter&ace varia:les. +ee the s1eci&ication corres1ondin! to a 1articular lan!ua!e version
&or details s1eci&ic to that version o& the lan!ua!e.
All -1enGL Gra1hics +,ste" +1eci&ication re&erences in this s1eci&ication are to version (.%.
1
1 Introduction
1.1 Acknowledgments
0his s1eci&ication is :ased on the wor o& those who contri:uted to 1ast versions o& the -1enGL
Lan!ua!e +1eci&ication, the -1enGL E+ #.) Lan!ua!e +1eci&ication, and the &ollowin! contri:utors to
this version:
3at Brown, 8VIDIA
Je&& Bol=, 8VIDIA
5ran $hen
3ierre Boudier, A;D
3iers Daniell, 8VIDIA
$hris Dodd, 8VIDIA
8ic /ae"el, 8VIDIA
Jason Green, 0ransGa"in!
Brent Inso, Intel
Jon Leech
Bill Licea9Kane, A;D
Daniel Koch, 0ransGa"in!
Barthold Lichten:elt, 8VIDIA
Bruce ;err,, AR;
Ro:ert -hannessian
Acorn 3oole,, 8VIDIA
$hristo1he Riccio, A;D
Kevin Ro!ovin
Ian Ro"anic, Intel
Gre! Roth, 8vidia
Graha" +ellers, A;D
Dave +hreiner, AR;
Jere", +and"el, A11le
Ro:ert +i"1son, >ualco""
Eric @erness, 8VIDIA
;ar Aoun!, A;D
2
1 Introduction
1.2 Changes
1.2.1 Changes from Revision of !ersion ".#$
Be e21licit that su:routines can :e called directl, Bwith static call without !oin! throu!h a
su:routine varia:leC.
$orrect the "inu"u" gl!a"#tomic$ounter%u&&er'i(e to :e %# instead o& 1*%4(.
Allow under&lowDover&low o& inte!er divisionD"ulti1lication to :e unde&ined rather than wra1.
$o"1ati:ilit,91ro&ile uni&or" state is not availa:le inside o& co"1ute shaders.
Re<uire "atched uni&or" :locs to also "atch in their 1resence or a:sence o& an instance na"e
B:ut not necessaril, the sa"e instance na"e, and not &or inDout :locs, Eust uni&or" :locsC.
8oise &unctions are availa:le in all shader sta!es.
Re"ove restriction that readonly and writeonly canFt :e used to!ether. It is valid to <uer, the
si=e o& a varia:le <uali&ied with :oth. Add the <uali&iers Greadonly writeonlyH to all the :uilt9in
imageSizeBC 1rotot,1es, allowin! <uer, o& i"a!e varia:les <uali&ied with either readonly,
writeonly, or :oth.
Add nor"ative re&erence Bsection 1)C &or the $II 1re1rocessor.
Re"ove the o1tional bias ar!u"ent &ro" the :uilt9in textureOffset Bsa"1ler#DArra,+hadow
sam)ler, vec( 3, vec# o&&setC 1rotot,1e.
Include e2istin! e,word precise in list o& e,words.
1.2.2 Changes from Revision % of !ersion ".#$
Added e2a"1les o& :u&&er :locs Bshader stora!e :locsC.
0he control &low :arrier :uilt9in &unction barrier! is allowed inside uni&or" &low control &or
co"1ute shaders.
It is an error i& an, two !eo"etr, shader out1ut varia:les are assi!ned the sa"e location and
strea", :ut i& the strea"s are di&&erent, the locations can :e the sa"e.
Arra,s declared without si=e, and never staticall, re&erenced, are !iven a si=e o& 1 &or an,
situations where the si=e is relevant Be.!., in an st*140 uni&or" :locC.
0he :uilt9in constant gl+or,-rou)'i(e is not availa:le in a co"1ute shader until a&ter a la,out
<uali&ier declaration that sets one or "ore o& the localsi(e". localsi(e/. and localsi(e(
la,out <uali&ier identi&iers.
E21licitl, allow e"1t, shaders as :ein! le!al, des1ite what the !ra""ar "i!ht i"1l,.
Be clear that i"a!e t,1es are the onl, o1a<ue t,1e tain! "e"or, <uali&iers.
1.2.# Summar& of Changes from !ersion ".2$
8ote: 8o &eatures were de1recated :etween versions (.#) and (.%).
$han!es
3
1 Introduction
Add shader stora!e buffer o:Eects, as 1er the ARB?shader?stora!e?:u&&er?o:Eect e2tension.
0his 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 readDwrite "e"or, shared with the a11lication and other
shader invocations. It also adds the st*430 la,out <uali&ier &or shader stora!e :locs.
Allow .lengt"BC on all arra,sJ returnin! a co"1ile9ti"e constant or not, de1endin! on how the
arra, is si=ed, as 1er the ARB?shader?stora!e?:u&&er?o:Eect e2tension.
Be clear that i"1licit arra, si=in! is onl, within a sta!e, not cross sta!e.
Arra, clari&ications: 1C All arra,s are inherentl, ho"o!eneous, e2ce1t &or arra,s o& the new
shader stora!e :u&&er o:Eects. #C Arra,s o& shader stora!e :u&&er o:Eects will :e dere&erenced
when the .lengt"BC "ethod is used on an unsi=ed arra, "e":er, so that "ust a have valid inde2.
%C Arra,s o& other o:Eects Buni&or" :locsC containin! i"1licitl, si=ed arra,s will have the sa"e
i"1licit si=e &or all ele"ents o& the arra,.
Arra,s o& arra,s are now su11orted, as 1er the GL?ARB?arra,s?o&?arra,s e2tension.
$o"1ute shaders are now su11orted, as 1er the GL?ARB?co"1ute?shader e2tension.
Added imageSizeBC :uilt9ins to <uer, the di"ensions o& an i"a!e.
De&ine ro:ust out9o&9:ounds access :ehavior when ena:led, as 1er the
GL?ARB?ro:ust?:u&&er?access?:ehavior e2tension.
All choice o& de1th or stencil te2turin!, &or a 1aced de1th9stencil te2ture, as 1er the
GL?ARB?stencil?te2turin! e2tension.
Allow e21licit locationsDinde2es to :e assi!ned to uni&or" varia:les and su:routines, as 1er the
GL?ARB?e21licit?uni&or"?location e2tension.
Acce1t E+ GL+L shader Kversion state"ents, which will re<uest E+ &unctionalit, &or E+ GL+L
versions 1)) and %)), as 1er the GL?ARB?E+%?co"1ati:ilit, e2tension.
$lari&ied a:out 6) errors as :ein! either co"1ile ti"e, lin ti"e, or either.
$lari&, and correct sco1in! rules to what would nor"all, :e e21ected and what was intended.
B5unction 1ara"eters and :od, nest inside !lo:al s1ace. Loo1 varia:les and :od, nest inside
loo1 sco1e.C
0here are no di!ra1hs Btri!ra1hs were alread, disallowedC.
Re"ove the $33 di&&erence that it is a co"1ile9ti"e error to use #if or #elif on e21ressions
containin! unde&ined "acro na"es. 0his reverts :ac to &ollowin! e21ected $33 :ehavior.
+et :oth gl!a"Fragment0mage1ni&orms and gl!a"$ombine*0mage1ni&orms to 4.
$lari&, textureSizeBC &or cu:e "a1 arra,s.
5or la,out <uali&iers,
"ae ne!ative out1ut locations a co"1ile9ti"e error, and
"ae inde2es outside the ran!e L),1M a co"1ile9ti"e error.
Allow "is"atches in inter1olation and au2iliar, <uali&ication across sta!es.
4
1 Introduction
Re"ove cross9version linin! restrictions.
Add texture#uery$evelsBC :uilt9ins to <uer, the nu":er o& "i1"a1 levels, as 1er the
GL?ARB?te2ture?<uer,?levels e2tension.
;ae glLa/er and glVie2)ort0n*e" also :e in1uts to the &ra!"ent shader, as 1er the
GL?ARB?&ra!"ent?la,er?view1ort e2tension.
Be e21licit that co"1ile9ti"e constant e21ressions evaluatin! on the host "a, !et a di&&erent
value than the sa"e e21ression on the tar!et. Also rein&orce that co"1ile9ti"e constant
e21ressions are evaluated as i& precise and invariant were used.
Add "ore e2a"1les and rules to :e "ore s1eci&ic a:out the re<uired :ehavior o& the precise
<uali&ier.
$lari&, &ra!"ent out1ut varia:les cannot :e dou:le 1recision.
$lari&, that G&loatin!91ointH !enerall, "eans :oth sin!le9 and dou:le9 1recision and :e "ore
consistent with that.
Be clear that early%fragment%tests is onl, needed in one &ra!"ent9sta!e co"1ilation unit.
Allow the new s"ared e,word to :e in la/out-3uali&ier-i*, allowin! :acward co"1ati:ilit,
with the s4are* identi&ier that was 1reviousl, used.
Added overlooed te2ture &unction
&loat textureOffset Bsa"1ler#DArra,+hadow sam)ler, vec( 3, vec# o&&set L, &loat biasM C.
Add "issin! t,1e in !ra""ar, A0-;I$?7I80, and "issin! <uali&iers $-/ERE80,
V-LA0ILE, RE+0RI$0, READ-8LA, and @RI0E-8LA.
Add "issin! initiali=er lists to !ra""ar.
5i2 all A3I re&erences to re&er to sections in the restructured A3I s1eci&ication.
0,1os
5i2ed t,1os in the !ra""ar. 1C Add "issin! D-7BLE$-8+0A80 toen, #C re"ove
e2traneous GI8VARIA80 IDE80I5IERH rule, %C re"ove e2traneous IDE80I5IER in the
G+7BR-70I8E LE50?3ARE8 IDE80I5IER t,1e?na"e?list RIG/0?3ARE8H rule, (C
re"ove du1licate G3A0$/H and G+A;3LEH, and .C correct lower case GArra,H to :e
GARRAAH.
5i2 &ndStream'rimitiveBC t,1o: Gin1utH N Gout1utH.
1.# Overvie'
0his docu"ent descri:es 54e 6)en-L '4a*ing Language. version (.%).
Inde1endent co"1ilation units written in this lan!ua!e are called s4a*ers. A )rogram is a set o& shaders
that are co"1iled and lined to!ether, co"1letel, creatin! one or "ore o& the 1ro!ra""a:le sta!es o& the
-1enGL 1i1eline. All the shaders &or a sin!le 1ro!ra""a:le sta!e "ust :e within the sa"e 1ro!ra". A
co"1lete set o& 1ro!ra""a:le sta!es can :e 1ut into a sin!le 1ro!ra" or the sta!es can :e 1artitioned
across "ulti1le 1ro!ra"s. 0he ai" o& this docu"ent is to thorou!hl, s1eci&, the 1ro!ra""in! lan!ua!e.
5
1 Introduction
0he -1enGL Gra1hics +,ste" +1eci&ication will s1eci&, the -1enGL entr, 1oints used to "ani1ulate and
co""unicate with 1ro!ra"s and shaders.
1." (rror )andling
$o"1ilers, in !eneral, acce1t 1ro!ra"s that are ill9&or"ed, due to the i"1ossi:ilit, o& detectin! all ill9
&or"ed 1ro!ra"s. 3orta:ilit, is onl, ensured &or well9&or"ed 1ro!ra"s, which this s1eci&ication
descri:es. $o"1ilers are encoura!ed to detect ill9&or"ed 1ro!ra"s and issue dia!nostic "essa!es, :ut are
not re<uired to do so &or all cases. $o"1ile9ti"e errors "ust :e returned &or le2icall, or !ra""aticall,
incorrect shaders. -ther errors are re1orted at co"1ile ti"e or lin ti"e as indicated. $ode that is GdeadH
"ust still :e error checed. 5or e2a"1le:
if (false) // changing false to true cannot uncover additional errors
statement; // statement must be error checked regardless
1.* T&pographical Conventions
Italic, :old, and &ont choices have :een used in this s1eci&ication 1ri"aril, to i"1rove reada:ilit,. $ode
&ra!"ents use a &i2ed width &ont. Identi&iers e":edded in te2t are italici=ed. Ke,words e":edded in te2t
are :old. -1erators are called :, their na"e, &ollowed :, their s,":ol in :old in 1arentheses. 0he
clari&,in! !ra""ar &ra!"ents in the te2t use :old &or literals and italics &or non9ter"inals. 0he o&&icial
!ra""ar in section 6 G+hadin! Lan!ua!e Gra""arH uses all ca1itals &or ter"inals and lower case &or
non9ter"inals.
1.% +eprecation
8ote that while this section is true o& GL+L in !eneral, version (.% has no de1recated &eatures in it.
3reviousl, de1recated &eatures have :een "oved to the co"1ati:ilit, 1ro&ile and no new &eatures have
:een de1recated.
3revious versions o& the -1enGL +hadin! Lan!ua!e de1recated so"e &eatures. 0hese are clearl, called
out in this s1eci&ication as Gde1recatedH. 0he, are still 1resent in this version o& the lan!ua!e, :ut are
tar!eted &or 1otential re"oval in a &uture version o& the shadin! lan!ua!e. 0he -1enGL A3I has a
&orward co"1ati:ilit, "ode that will disallow use o& de1recated &eatures. I& co"1ilin! in a "ode where
use o& de1recated &eatures is disallowed, their use causes co"1ile9ti"e or lin9ti"e errors. +ee the
-1enGL Gra1hics +,ste" +1eci&ication &or details on what causes de1recated lan!ua!e &eatures to :e
acce1ted or to return an error.
6
2 Overvie' of OpenGL Shading
0he -1enGL +hadin! Lan!ua!e is actuall, several closel, related lan!ua!es. 0hese lan!ua!es are used
to create shaders &or each o& the 1ro!ra""a:le 1rocessors contained in the -1enGL 1rocessin! 1i1eline.
$urrentl,, these 1rocessors are the verte2, tessellation control, tessellation evaluation, !eo"etr,,
&ra!"ent, and co"1ute 1rocessors.
7nless otherwise noted in this 1a1er, a lan!ua!e &eature a11lies 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. 0he s1eci&ic lan!ua!es will :e re&erred to :, the na"e o&
the 1rocessor the, tar!et: verte2, tessellation control, tessellation evaluation, !eo"etr,, &ra!"ent, or
co"1ute.
;ost -1enGL state is not traced or "ade availa:le to shaders. 0,1icall,, user9de&ined varia:les will :e
used &or co""unicatin! :etween di&&erent sta!es o& the -1enGL 1i1eline. /owever, a s"all a"ount o&
state is still traced and auto"aticall, "ade availa:le to shaders, and there are a &ew :uilt9in varia:les &or
inter&aces :etween di&&erent sta!es o& the -1enGL 1i1eline.
2.1 !erte, -rocessor
0he verte" )rocessor is a 1ro!ra""a:le unit that o1erates on inco"in! vertices and their associated data.
$o"1ilation units written in the -1enGL +hadin! Lan!ua!e to run on this 1rocessor are called verte"
s4a*ers. @hen a set o& verte2 shaders are success&ull, co"1iled and lined, the, result in a verte" s4a*er
e"ecutable that runs on the verte2 1rocessor.
0he verte2 1rocessor o1erates on one verte2 at a ti"e. It does not re1lace !ra1hics o1erations that re<uire
nowled!e o& several vertices at a ti"e.
2.2 Tessellation Control -rocessor
0he tessellation control 1rocessor is a 1ro!ra""a:le unit that o1erates on a 1atch o& inco"in! vertices
and their associated data, e"ittin! a new out1ut 1atch. $o"1ilation units written in the -1enGL +hadin!
Lan!ua!e to run on this 1rocessor are called tessellation control shaders. @hen a set o& tessellation
control shaders are success&ull, co"1iled and lined, the, result in a tessellation control s4a*er
e"ecutable that runs on the tessellation control 1rocessor.
0he tessellation control shader is invoed &or each verte2 o& the out1ut 1atch. Each invocation can read
the attri:utes o& an, verte2 in the in1ut or out1ut 1atches, :ut can onl, write 1er9verte2 attri:utes &or the
corres1ondin! out1ut 1atch verte2. 0he shader invocations collectivel, 1roduce a set o& 1er91atch
attri:utes &or the out1ut 1atch. A&ter all tessellation control shader invocations have co"1leted, the out1ut
vertices and 1er91atch attri:utes are asse":led to &or" a 1atch to :e used :, su:se<uent 1i1eline sta!es.
0essellation control shader invocations run "ostl, inde1endentl,, with unde&ined relative e2ecution order.
/owever, the :uilt9in &unction :arrierBC can :e used to control e2ecution order :, s,nchroni=in!
invocations, e&&ectivel, dividin! tessellation control shader e2ecution into a set o& 1hases. 0essellation
control shaders will !et unde&ined results i& one invocation reads a 1er9verte2 or 1er91atch attri:ute
7
2 Overview of OpenGL Shading
written :, another invocation at an, 1oint durin! the sa"e 1hase, or i& two invocations atte"1t to write
di&&erent values to the sa"e 1er91atch out1ut in a sin!le 1hase.
2.# Tessellation (valuation -rocessor
0he tessellation evaluation 1rocessor is a 1ro!ra""a:le unit that evaluates the 1osition and other
attri:utes o& a verte2 !enerated :, the tessellation 1ri"itive !enerator, usin! a 1atch o& inco"in! vertices
and their associated data. $o"1ilation units written in the -1enGL +hadin! Lan!ua!e to run on this
1rocessor are called tessellation evaluation shaders. @hen a set o& tessellation evaluation shaders are
success&ull, co"1iled and lined, the, result in a tessellation evaluation s4a*er e"ecutable that runs on
the tessellation evaluation 1rocessor.
Each invocation o& the tessellation evaluation e2ecuta:le co"1utes the 1osition and attri:utes o& a sin!le
verte2 !enerated :, the tessellation 1ri"itive !enerator. 0he e2ecuta:le can read the attri:utes o& an,
verte2 in the in1ut 1atch, 1lus the tessellation coordinate, which is the relative location o& the verte2 in the
1ri"itive :ein! tessellated. 0he e2ecuta:le writes the 1osition and other attri:utes o& the verte2.
2." Geometr& -rocessor
0he geometr/ )rocessor is a 1ro!ra""a:le unit that o1erates on data &or inco"in! vertices &or a 1ri"itive
asse":led a&ter verte2 1rocessin! and out1uts a se<uence o& vertices &or"in! out1ut 1ri"itives.
$o"1ilation units written in the -1enGL +hadin! Lan!ua!e to run on this 1rocessor are called geometr/
s4a*ers. @hen a set o& !eo"etr, shaders are success&ull, co"1iled and lined, the, result in a geometr/
s4a*er e"ecutable that runs on the !eo"etr, 1rocessor.
A sin!le invocation o& the !eo"etr, shader e2ecuta:le on the !eo"etr, 1rocessor will o1erate on a
declared in1ut 1ri"itive with a &i2ed nu":er o& vertices. 0his sin!le invocation can e"it a varia:le
nu":er o& vertices that are asse":led into 1ri"itives o& a declared out1ut 1ri"itive t,1e and 1assed to
su:se<uent 1i1eline sta!es.
2.* .ragment -rocessor
0he &ragment )rocessor is a 1ro!ra""a:le unit that o1erates on &ra!"ent values and their associated
data. $o"1ilation units written in the -1enGL +hadin! Lan!ua!e to run on this 1rocessor are called
&ragment s4a*ers. @hen a set o& &ra!"ent shaders are success&ull, co"1iled and lined, the, result in a
&ragment s4a*er e"ecutable that runs on the &ra!"ent 1rocessor.
A &ra!"ent shader cannot chan!e a &ra!"entFs B", /C 1osition. Access to nei!h:orin! &ra!"ents is not
allowed. 0he values co"1uted :, the &ra!"ent shader are ulti"atel, used to u1date &ra"e:u&&er "e"or,
or te2ture "e"or,, de1endin! on the current -1enGL state and the -1enGL co""and that caused the
&ra!"ents to :e !enerated.
2.% Compute -rocessor
0he com)ute )rocessor is a 1ro!ra""a:le unit that o1erates inde1endentl, &ro" the other shader
1rocessors. $o"1ilation units written in the -1enGL +hadin! Lan!ua!e to run on this 1rocessor are
called com)ute s4a*ers. @hen a set o& co"1ute shaders are success&ull, co"1iled and lined, the, result
in a com)ute s4a*er e"ecutable that runs on the co"1ute 1rocessor.
8
2 Overview of OpenGL Shading
A co"1ute shader has access to "an, o& the sa"e resources as &ra!"ent and other shader 1rocessors,
includin! te2tures, :u&&ers, i"a!e varia:les, and ato"ic counters. It does not have an, 1rede&ined in1uts
nor an, &i2ed9&unction out1uts. It is not 1art o& the !ra1hics 1i1eline and its visi:le side e&&ects are
throu!h chan!es to i"a!es, stora!e :u&&ers, and ato"ic counters.
A co"1ute shader o1erates on a !rou1 o& wor ite"s called a wor !rou1. A wor !rou1 is a collection
o& shader invocations that e2ecute the sa"e code, 1otentiall, in 1arallel. An invocation within a wor
!rou1 "a, share data with other "e":ers o& the sa"e wor !rou1 throu!h shared varia:les and issue
"e"or, and control :arriers to s,nchroni=e with other "e":ers o& the sa"e wor !rou1.
9
# /asics
#.1 Character Set
0he source character set used &or the -1enGL shadin! lan!ua!es, outside o& co""ents, is a su:set o&
70594. It includes the &ollowin! characters:
0he letters a(z, A()* and the underscore B ? C.
0he nu":ers +(,.
0he s,":ols 1eriod B.C, 1lus B-C, dash B(C, slash B.C, asteris B/C, 1ercent B0C, an!led :racets B1 and
2C, s<uare :racets B 3 and 4 C, 1arentheses B and ! C, :races B 5 and 6 C, caret B7C, vertical :ar B 8 C,
a"1ersand B9C, tilde B:C, e<uals B;C, e2cla"ation 1oint B<C, colon B=C, se"icolon B>C, co""a B*C, and
<uestion "ar B?C.
0he nu":er si!n B#C &or 1re1rocessor use.
0he :acslash B @ C as the line9continuation character when used as the last character o& a line, Eust
:e&ore a new line.
@hite s1ace: the s1ace character, hori=ontal ta:, vertical ta:, &or" &eed, carria!e9return, and line9
&eed.
A co"1ile9ti"e error will :e !iven i& an, other character is used outside a co""ent.
0here are no di!ra1hs or tri!ra1hs. 0here are no esca1e se<uences or other uses o& the :acslash :e,ond
use as the line9continuation character.
Lines are relevant &or co"1iler dia!nostic "essa!es and the 1re1rocessor. 0he, are ter"inated :,
carria!e9return or line9&eed. I& :oth are used to!ether, it will count as onl, a sin!le line ter"ination. 5or
the re"ainder o& this docu"ent, an, o& these co":inations is si"1l, re&erred to as a new line.
In !eneral, the lan!ua!eOs use o& this character set is case sensitive.
0here are no character or strin! data t,1es, so no <uotin! characters are included.
0here is no end9o&9&ile character.
#.2 Source Strings
0he source &or a sin!le shader is an arra, o& strin!s o& characters &ro" the character set. A sin!le shader
is "ade &ro" the concatenation o& these strin!s. Each strin! can contain "ulti1le lines, se1arated :, new
lines. 8o new lines need :e 1resent in a strin!J a sin!le line can :e &or"ed &ro" "ulti1le strin!s. 8o new
lines or other characters are inserted :, the i"1le"entation when it concatenates the strin!s to &or" a
sin!le shader. ;ulti1le shaders can :e lined to!ether to &or" a sin!le 1ro!ra".
Dia!nostic "essa!es returned &ro" co"1ilin! a shader "ust identi&, :oth the line nu":er within a strin!
and which source strin! the "essa!e a11lies to. +ource strin!s are counted se<uentiall, with the &irst
1
3 !a"ic"
strin! :ein! strin! ). Line nu":ers are one "ore than the nu":er o& new lines that have :een 1rocessed,
includin! countin! the new lines that will :e re"oved :, the line9continuation character B @ C.
Lines se1arated :, the line9continuation character 1recedin! a new line are concatenated to!ether :e&ore
either co""ent 1rocessin! or 1re1rocessin!. 8o white s1ace is su:stituted &or the line9continuation
character. 0hat 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 ne2t 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
0here is a 1re1rocessor that 1rocesses the source strin!s as 1art o& the co"1ilation 1rocess. E2ce1t as
noted :elow, it :ehaves as the $II standard 1re1rocessor Bsee section 1) G8or"ative Re&erencesHC.
0he co"1lete list o& 1re1rocessor directives is as &ollows.

define
undef
if
ifdef
ifndef
else
elif
endif
error
!ragma
extension
version
line
0he &ollowin! o1erators are also availa:le
defined

Each nu":er si!n B#C can :e 1receded in its line onl, :, s1aces or hori=ontal ta:s. It "a, also :e
&ollowed :, s1aces and hori=ontal ta:s, 1recedin! the directive. Each directive is ter"inated :, a new
line. 3re1rocessin! does not chan!e the nu":er or relative location o& new lines in a source strin!.
3re1rocessin! taes 1laces a&ter new lines have :een re"oved :, the line9continuation character.
0he nu":er si!n B#C on a line :, itsel& is i!nored. An, directive not listed a:ove will cause a dia!nostic
"essa!e and "ae the i"1le"entation treat the shader as ill9&or"ed.
11
3 !a"ic"
#define and #undef &unctionalit, are de&ined as is standard &or $II 1re1rocessors &or "acro de&initions
:oth with and without "acro 1ara"eters.
0he &ollowin! 1rede&ined "acros are availa:le
""#$%&""
""'$#&""
""(&)*$+%""
L078 will su:stitute a deci"al inte!er constant that is one "ore than the nu":er o& 1recedin! new
lines in the current source strin!.
F0L8 will su:stitute a deci"al inte!er constant that sa,s which source strin! nu":er is currentl,
:ein! 1rocessed.
V8R'067 will su:stitute a deci"al inte!er re&lectin! the version nu":er o& the -1enGL shadin!
lan!ua!e. 0he version o& the shadin! lan!ua!e descri:ed in this docu"ent will have V8R'067
su:stitute the deci"al inte!er (%).
All "acro na"es containin! two consecutive underscores B %% C are reserved &or &uture use as 1rede&ined
"acro na"es. All "acro na"es 1re&i2ed with GGL?H BGGLH &ollowed :, a sin!le underscoreC are also
reserved.
#if* #ifdef* #ifndef* #else* #elif* and #endif are de&ined to o1erate as is standard &or $II 1re1rocessors.
E21ressions &ollowin! #if and #elif are &urther restricted to e21ressions o1eratin! on literal inte!er
constants, 1lus identi&iers consu"ed :, the defined o1erator. $haracter constants are not su11orted.
0he o1erators availa:le are as &ollows.
-recedence Operator class Operators 0ssociativit&
1 Bhi!hestC 1arenthetical !rou1in! B C 8A
# unar, de&ined
I 9 P Q
Ri!ht to Le&t
% "ulti1licative R D S Le&t to Ri!ht
( additive I 9 Le&t to Ri!ht
. :it9wise shi&t TT UU Le&t to Ri!ht
* relational T U TV UV Le&t to Ri!ht
' e<ualit, VV QV Le&t to Ri!ht
4 :it9wise and W Le&t to Ri!ht
6 :it9wise e2clusive or X Le&t to Ri!ht
1) :it9wise inclusive or Y Le&t to Ri!ht
11 lo!ical and WW Le&t to Ri!ht
1# BlowestC lo!ical inclusive or Y Y Le&t to Ri!ht
12
3 !a"ic"
0he defined o1erator can :e used in either o& the &ollowin! wa,s:
defined identifier
defined ( identifier )
0wo toens in a "acro can :e concatenated into one toen usin! the toen 1astin! B##C o1erator, as is
standard &or $II 1re1rocessors. 0he result "ust :e a valid sin!le toen, which will then :e su:Eect to
"acro e21ansion. 0hat is, "acro e21ansion ha11ens onl, a&ter toen 1astin!. 0here are no other nu":er
si!n :ased o1erators Be.!., no # or #AC, nor is there a sizeof o1erator.
0he se"antics o& a11l,in! o1erators to inte!er literals in the 1re1rocessor "atch those standard in the $I
I 1re1rocessor, not those in the -1enGL +hadin! Lan!ua!e.
3re1rocessor e21ressions will :e evaluated accordin! to the :ehavior o& the host 1rocessor, not the
1rocessor tar!eted :, the shader.
#error will cause the i"1le"entation to 1ut a co"1ile9ti"e dia!nostic "essa!e into the shader o:EectOs
in&or"ation lo! Bsee section '.1# G+hader and 3ro!ra" >ueriesH in the -1enGL Gra1hics +,ste"
+1eci&ication &or how to access a shader o:EectOs in&or"ation lo!C. 0he "essa!e will :e the toens
&ollowin! the #error directive, u1 to the &irst new line. 0he i"1le"entation "ust then consider the shader
to :e ill9&or"ed.
#pragma allows i"1le"entation de1endent co"1iler control. 0oens &ollowin! #pragma are not su:Eect
to 1re1rocessor "acro e21ansion. I& an i"1le"entation does not reco!ni=e the toens &ollowin!
#pragma, then it will i!nore that 1ra!"a. 0he &ollowin! 1ra!"as are de&ined as 1art o& the lan!ua!e.
!ragma *,-.#
0he SBCD$ 1ra!"a is used to reserve 1ra!"as &or use :, &uture revisions o& this lan!ua!e. 8o
i"1le"entation "a, use a 1ra!"a whose &irst toen is SBCD$.
!ragma o!timi/e(on)
!ragma o!timi/e(off)
can :e used to turn o&& o1ti"i=ations as an aid in develo1in! and de:u!!in! shaders. It can onl, :e used
outside &unction de&initions. B, de&ault, o1ti"i=ation is turned on &or all shaders. 0he de:u! 1ra!"a
!ragma debug(on)
!ragma debug(off)
can :e used to ena:le co"1ilin! and annotatin! a shader with de:u! in&or"ation, so that it can :e used
with a de:u!!er. It can onl, :e used outside &unction de&initions. B, de&ault, de:u! is turned o&&.
+haders should declare the version o& the lan!ua!e the, are written to. 0he lan!ua!e version a shader is
written to is s1eci&ied :,
version number profile
opt
where number "ust :e a version o& the lan!ua!e, &ollowin! the sa"e convention as V8R'067 a:ove.
0he directive G#version EF+H is re<uired in an, shader that uses version (.%) o& the lan!ua!e. An,
number re1resentin! a version o& the lan!ua!e a co"1iler does not su11ort will cause a co"1ile9ti"e
error to :e !enerated. Version 1.1) o& the lan!ua!e does not re<uire shaders to include this directive, and
shaders that do not include a #version directive will :e treated as tar!etin! version 1.1). +haders that
13
3 !a"ic"
s1eci&, #version 1)) will :e treated as tar!etin! version 1.)) o& the -1enGL E+ +hadin! Lan!ua!e.
+haders that s1eci&, #version %)) will :e treated as tar!etin! version %.)) o& the -1enGL E+ +hadin!
Lan!ua!e.
I& the o1tional )ro&ile ar!u"ent is 1rovided, it "ust :e the na"e o& an -1enGL 1ro&ile. $urrentl,, there
are three choices:
core
com!atibilit0
es
A )ro&ile ar!u"ent can onl, :e used with version 1.) or !reater. I& no 1ro&ile ar!u"ent is 1rovided and
the version is 1.) or !reater, the de&ault is core. I& version %)) is s1eci&ied, the 1ro&ile ar!u"ent is not
o1tional and "ust :e es, or a co"1ile9ti"e error results. 0he Lan!ua!e +1eci&ication &or the es 1ro&ile is
s1eci&ied in 0he -1enGL E+ +hadin! Lan!ua!e s1eci&ication.
+haders &or the core or compatibility 1ro&iles that declare di&&erent versions can :e lined to!ether.
/owever, es 1ro&ile shaders cannot :e lined with non9es 1ro&ile shaders or with es 1ro&ile shaders o& a
di&&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 1er the linin! rules in the GL+L version corres1ondin! to the
version o& the conte2t the shaders are lined under. +hader co"1ile9ti"e errors "ust still :e !iven strictl,
:ased on the version declared Bor de&aulted toC within each shader.
7nless otherwise s1eci&ied, this s1eci&ication is docu"entin! the core 1ro&ile, and ever,thin! s1eci&ied &or
the core 1ro&ile is also availa:le in the co"1ati:ilit, 1ro&ile. 5eatures s1eci&ied as :elon!in! s1eci&icall,
to the co"1ati:ilit, 1ro&ile are not availa:le in the core 1ro&ile.
0here is a :uilt9in "acro de&inition &or each 1ro&ile the i"1le"entation su11orts. All i"1le"entations
1rovide the &ollowin! "acro:
define .#"core"!rofile 1
I"1le"entations 1rovidin! the compatibility 1ro&ile 1rovide the &ollowin! "acro:
define .#"com!atibilit0"!rofile 1
I"1le"entations 1rovidin! the es 1ro&ile 1rovide the &ollowin! "acro:
define .#"es"!rofile 1
0he #version directive "ust occur in a shader :e&ore an,thin! else, e2ce1t &or co""ents and white s1ace.
14
3 !a"ic"
B, de&ault, co"1ilers o& this lan!ua!e "ust issue co"1ile9ti"e le2ical and !ra""atical errors &or shaders
that do not con&or" to this s1eci&ication. An, e2tended :ehavior "ust &irst :e ena:led. Directives to
control the :ehavior o& the co"1iler with res1ect to e2tensions are declared with the #extension directive
extension extension_name : behavior
extension all : behavior
where e"tensionname is the na"e o& an e2tension. E2tension na"es are not docu"ented in this
s1eci&ication. 0he toen all "eans the :ehavior a11lies to all e2tensions su11orted :, the co"1iler. 0he
be4avior can :e one o& the &ollowin!
behavior (ffect
re1uire Behave as s1eci&ied :, the e2tension e"tensionname.
Give a co"1ile9ti"e error on the #extension i& the e2tension e"tensionname
is not su11orted, or i& all is s1eci&ied.
enable Behave as s1eci&ied :, the e2tension e"tensionname.
@arn on the #extension i& the e2tension e"tensionname is not su11orted.
Give a co"1ile9ti"e error on the #extension i& all is s1eci&ied.
'arn Behave as s1eci&ied :, the e2tension e"tensionname, e2ce1t issue warnin!s
on an, detecta:le use o& that e2tension, unless such use is su11orted :, other
ena:led or re<uired e2tensions.
I& all is s1eci&ied, then warn on all detecta:le uses o& an, e2tension used.
@arn on the #extension i& the e2tension e"tensionname is not su11orted.
disable Behave Bincludin! issuin! errors and warnin!sC as i& the e2tension
e"tensionname is not 1art o& the lan!ua!e de&inition.
I& all is s1eci&ied, then :ehavior "ust revert :ac to that o& the non9e2tended
core version o& the lan!ua!e :ein! co"1iled to.
@arn on the #extension i& the e2tension e"tensionname is not su11orted.
0he extension directive is a si"1le, low9level "echanis" to set the :ehavior &or each e2tension. It does
not de&ine 1olicies such as which co":inations are a11ro1riate, those "ust :e de&ined elsewhere. -rder
o& directives "atters in settin! the :ehavior &or each e2tension: Directives that occur later override those
seen earlier. 0he all variant sets the :ehavior &or all e2tensions, overridin! all 1reviousl, issued
extension directives, :ut onl, &or the be4aviors warn and disable.
15
3 !a"ic"
0he initial state o& the co"1iler is as i& the directive
extension all 2 disable
was issued, tellin! the co"1iler that all error and warnin! re1ortin! "ust :e done accordin! to this
s1eci&ication, i!norin! an, e2tensions.
Each e2tension can de&ine its allowed !ranularit, o& sco1e. I& nothin! is said, the !ranularit, is a shader
Bthat is, a sin!le co"1ilation unitC, and the e2tension directives "ust occur :e&ore an, non91re1rocessor
toens. I& necessar,, the liner can en&orce !ranularities lar!er than a sin!le co"1ilation unit, in which
case each involved shader will have to contain the necessar, e2tension directive.
;acro e21ansion is not done on lines containin! #extension and #version directives.
#line "ust have, a&ter "acro su:stitution, one o& the &ollowin! &or"s:
line line
line line source-string-number
where line and source-string-number are constant inte!er e21ressions. A&ter 1rocessin! this directive
Bincludin! its new lineC, the i"1le"entation will :ehave as i& it is co"1ilin! at line nu":er line and source
strin! nu":er source-string-number. +u:se<uent source strin!s will :e nu":ered se<uentiall,, until
another #line directive overrides that nu":erin!.
#." Comments
$o""ents are deli"ited :, DR and RD, or :, DD and a new line. 0he :e!in co""ent deli"iters BDR or DDC are
not reco!ni=ed as co""ent deli"iters inside o& a co""ent, hence co""ents cannot :e nested. I& a
co""ent resides entirel, within a sin!le line, it is treated s,ntacticall, as a sin!le s1ace. 8ew lines are
not eli"inated :, co""ents.
Inside co""ents, an, :,te values "a, :e used, e2ce1t a :,te whose value is ). 8o errors will :e !iven
&or the content o& co""ents and no validation on the content o& co""ents need :e done.
Re"oval o& new lines :, the line9continuation character B @ C lo!icall, occurs :e&ore co""ents are
1rocessed. 0hat is, a sin!le9line co""ent endin! in the line9continuation character B @ C includes the ne2t
line in the co""ent.
// a single3line comment containing the next line \
a 4 b; // this is still in the first comment
16
3 !a"ic"
#.* To2ens
0he lan!ua!e is a se<uence o& toens. A toen can :e
to,en:
,e/2or*
i*enti&ier
integer-constant
&loating-constant
o)erator
> 5 6
#.% 3e&'ords
0he &ollowin! are the lan!ua!eFs e,words and Ba&ter 1re1rocessin!C can onl, :e used as descri:ed in this
s1eci&ication, or a co"1ile9ti"e error results:
attribute const uniform varying buffer s"ared
co"erent volatile restrict readonly writeonly
atomic%uint
layout
centroid flat smoot" noperspective
patc" sample
break continue do for w"ile switc" case default
if else
subroutine
in out inout
float double int void bool true false
invariant precise
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
17
3 !a"ic"
lowp mediump "ig"p precision
sampler1C samplerGC samplerFC samplerHube
sampler1CS"adow samplerGCS"adow samplerHubeS"adow
sampler1CArray samplerGCArray
sampler1CArrayS"adow samplerGCArrayS"adow
isampler1C isamplerGC isamplerFC isamplerHube
isampler1CArray isamplerGCArray
usampler1C usamplerGC usamplerFC usamplerHube
usampler1CArray usamplerGCArray
samplerGCIect samplerGCIectS"adow isamplerGCIect usamplerGCIect
samplerJuffer isamplerJuffer usamplerJuffer
samplerGCKS isamplerGCKS usamplerGCKS
samplerGCKSArray isamplerGCKSArray usamplerGCKSArray
samplerHubeArray samplerHubeArrayS"adow isamplerHubeArray usamplerHubeArray
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
0he &ollowin! are the e,words reserved &or &uture use. 7sin! the" will result in a co"1ile9ti"e error:
common partition active
asm
class union enum typedef template t"is packed
resource
18
3 !a"ic"
goto
inline noinline public static extern external interface
long s"ort "alf fixed unsigned superp
input output
"vecG "vecF "vecE fvecG fvecF fvecE
samplerFCIect
filter
sizeof cast
namespace using
row%maLor
In addition, all identi&iers containin! two consecutive underscores B%%C are reserved as 1ossi:le &uture
e,words.
#. Identifiers
Identi&iers are used &or varia:le na"es, &unction na"es, structure na"es, and &ield selectors B&ield
selectors select co"1onents o& vectors and "atrices si"ilar to structure "e":ers, as discussed in section
... GVector and +calar $o"1onentsH and section ..* G;atri2 $o"1onentsH C. Identi&iers have the &or"
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 L k l m n o p M r s t u v w x y z
A J H C & N D O P Q R $ K S O ' # I S B T U V W X )
*igit: one o&
+ 1 G F E Y Z [ \ ,
Identi&iers startin! with G!l?H are reserved &or use :, -1enGL, and "a, not :e declared in a shader as
either a varia:le or a &unctionJ this results in a co"1ile9ti"e error. /owever, as noted in the s1eci&ication,
there are so"e cases where 1reviousl, declared varia:les can :e redeclared, and 1redeclared Z!l?Z na"es
are allowed to :e redeclared in a shader onl, &or these s1eci&ic 1ur1oses. ;ore !enerall,, it is a co"1ile9
ti"e error to redeclare a varia:le, includin! those startin! G!l?H.
#.4 +efinitions
+o"e lan!ua!e rules descri:ed :elow de1end on the &ollowin! de&initions.
19
3 !a"ic"
#.4.1 Static 5se
A shader contains a static use o& Bor static assignment toC a varia:le " i&, a&ter 1re1rocessin!, the shader
contains a state"ent that would read Bor writeC ", whether or not run9ti"e &low o& control will cause that
state"ent to :e e2ecuted.
#.4.2 5niform and 6on75niform Control .lo'
@hen e2ecutin! state"ents in a &ra!"ent shader, control &low starts as uni&orm control &lo2J all &ra!"ents
enter the sa"e control 1ath into mainBC. $ontrol &low :eco"es non-uni&orm when di&&erent &ra!"ents
tae di&&erent 1aths throu!h control9&low state"ents Bselection, iteration, and Eu"1sC. $ontrol &low
su:se<uentl, returns to :ein! uni&or" a&ter such diver!ent su:9state"ents or si11ed code co"1letes,
until the ne2t ti"e di&&erent control 1aths are taen.
5or e2a"1le:
main()
5
float a 4 666;// this is uniform flow control
if (a 7 b) 5 // this ex!ression is true for some fragments8 not all
6666; // non3uniform flow control
9 else 5
6666; // non3uniform flow control
9
6666; // uniform flow control again
9
-ther e2a"1les o& non9uni&or" &low control can occur within switch state"ents and a&ter conditional
:reas, continues, earl, returns, and a&ter &ra!"ent discards, when the condition is true &or so"e
&ra!"ents :ut not others. Loo1 iterations that onl, so"e &ra!"ents e2ecute are also non9uni&or" &low
control.
0his is si"ilarl, de&ined &or other shader sta!es, :ased on the 1er9instance data ite"s the, 1rocess.
#.4.# +&namicall& 5niform (,pressions
A &ra!"ent9shader e21ression is */namicall/ uni&orm i& all &ra!"ents evaluatin! it !et the sa"e resultin!
value. @hen loo1s are involved, this re&ers to the e21ressionFs value &or the sa"e loo1 iteration. @hen
&unctions are involved, this re&ers to calls &ro" the sa"e call 1oint.
0his is si"ilarl, de&ined &or other shader sta!es, :ased on the 1er9instance data the, 1rocess.
8ote that constant e21ressions are triviall, d,na"icall, uni&or". It &ollows that t,1ical loo1 counters
:ased on these are also d,na"icall, uni&or".
2
" !ariables and T&pes
All varia:les and &unctions "ust :e declared :e&ore :ein! used. Varia:le and &unction na"es are
identi&iers.
0here are no de&ault t,1es. All varia:le and &unction declarations "ust have a declared t,1e, and
o1tionall, <uali&iers. A varia:le is declared :, s1eci&,in! its t,1e &ollowed :, one or "ore na"es
se1arated :, co""as. In "an, cases, a varia:le can :e initiali=ed as 1art o& its declaration :, usin! the
assi!n"ent o1erator B;C.
7ser9de&ined t,1es "a, :e de&ined usin! struct to a!!re!ate a list o& e2istin! t,1es into a sin!le na"e.
0he -1enGL +hadin! Lan!ua!e is t,1e sa&e. 0here are so"e i"1licit conversions :etween t,1es.
E2actl, how and when this can occur is descri:ed in section (.1.1) GI"1licit $onversionsH and as
re&erenced :, other sections in this s1eci&ication.
".1 /asic T&pes
0he -1enGL +hadin! Lan!ua!e su11orts the &ollowin! :asic data t,1es, !rou1ed as &ollows.
0rans1arent t,1es
T&pe 8eaning
void &or &unctions that do not return a value
bool a conditional t,1e, tain! on values o& true or &alse
int a si!ned inte!er
uint an unsi!ned inte!er
float a sin!le91recision &loatin!91oint scalar
double a dou:le91recision &loatin!91oint scalar
vecG a two9co"1onent sin!le91recision &loatin!91oint vector
vecF a three9co"1onent sin!le91recision &loatin!91oint vector
vecE a &our9co"1onent sin!le91recision &loatin!91oint vector
dvecG a two9co"1onent dou:le91recision &loatin!91oint vector
dvecF a three9co"1onent dou:le91recision &loatin!91oint vector
dvecE a &our9co"1onent dou:le91recision &loatin!91oint vector
bvecG a two9co"1onent Boolean vector
bvecF a three9co"1onent Boolean vector
bvecE a &our9co"1onent Boolean vector
ivecG a two9co"1onent si!ned inte!er vector
21
4 #aria$%e" and &'pe"
T&pe 8eaning
ivecF a three9co"1onent si!ned inte!er vector
ivecE a &our9co"1onent si!ned inte!er vector
uvecG a two9co"1onent unsi!ned inte!er vector
uvecF a three9co"1onent unsi!ned inte!er vector
uvecE a &our9co"1onent unsi!ned inte!er vector
matG a #[# sin!le91recision &loatin!91oint "atri2
matF a %[% sin!le91recision &loatin!91oint "atri2
matE a ([( sin!le91recision &loatin!91oint "atri2
matGxG sa"e as a matG
matGxF a sin!le91recision &loatin!91oint "atri2 with # colu"ns and % rows
matGxE a sin!le91recision &loatin!91oint "atri2 with # colu"ns and ( rows
matFxG a sin!le91recision &loatin!91oint "atri2 with % colu"ns and # rows
matFxF sa"e as a matF
matFxE a sin!le91recision &loatin!91oint "atri2 with % colu"ns and ( rows
matExG a sin!le91recision &loatin!91oint "atri2 with ( colu"ns and # rows
matExF a sin!le91recision &loatin!91oint "atri2 with ( colu"ns and % rows
matExE sa"e as a matE
dmatG a #[# dou:le91recision &loatin!91oint "atri2
dmatF a %[% dou:le91recision &loatin!91oint "atri2
dmatE a ([( dou:le91recision &loatin!91oint "atri2
dmatGxG sa"e as a dmatG
dmatGxF a dou:le91recision &loatin!91oint "atri2 with # colu"ns and % rows
dmatGxE a dou:le91recision &loatin!91oint "atri2 with # colu"ns and ( rows
dmatFxG a dou:le91recision &loatin!91oint "atri2 with % colu"ns and # rows
dmatFxF sa"e as a dmatF
dmatFxE a dou:le91recision &loatin!91oint "atri2 with % colu"ns and ( rows
dmatExG a dou:le91recision &loatin!91oint "atri2 with ( colu"ns and # rows
dmatExF a dou:le91recision &loatin!91oint "atri2 with ( colu"ns and % rows
dmatExE sa"e as a dmatE
22
4 #aria$%e" and &'pe"
5loatin!93oint -1a<ue 0,1es
T&pe 8eaning
sampler1C
image1C
a handle &or accessin! a 1D te2ture
samplerGC
imageGC
a handle &or accessin! a #D te2ture
samplerFC
imageFC
a handle &or accessin! a %D te2ture
samplerHube
imageHube
a handle &or accessin! a cu:e "a11ed te2ture
samplerGCIect
imageGCIect
a handle &or accessin! a rectan!le te2ture
sampler1CArray
image1CArray
a handle &or accessin! a 1D arra, te2ture
samplerGCArray
imageGCArray
a handle &or accessin! a #D arra, te2ture
samplerJuffer
imageJuffer
a handle &or accessin! a :u&&er te2ture
samplerGCKS
imageGCKS
a handle &or accessin! a #D "ulti9sa"1le te2ture
samplerGCKSArray
imageGCKSArray
a handle &or accessin! a #D "ulti9sa"1le arra, te2ture
samplerHubeArray
imageHubeArray
a handle &or accessin! a cu:e "a1 arra, te2ture
sampler1CS"adow a handle &or accessin! a 1D de1th te2ture with co"1arison
samplerGCS"adow a handle &or accessin! a #D de1th te2ture with co"1arison
samplerGCIectS"adow a handle &or accessin! a rectan!le te2ture with co"1arison
sampler1CArrayS"adow a handle &or accessin! a 1D arra, de1th te2ture with co"1arison
samplerGCArrayS"adow a handle &or accessin! a #D arra, de1th te2ture with co"1arison
samplerHubeS"adow a handle &or accessin! a cu:e "a1 de1th te2ture with co"1arison
samplerHubeArrayS"adow a handle &or accessin! a cu:e "a1 arra, de1th te2ture with
co"1arison
+i!ned Inte!er -1a<ue 0,1es
T&pe 8eaning
isampler1C
iimage1C
a handle &or accessin! an inte!er 1D te2ture
23
4 #aria$%e" and &'pe"
T&pe 8eaning
isamplerGC
iimageGC
a handle &or accessin! an inte!er #D te2ture
isamplerFC
iimageFC
a handle &or accessin! an inte!er %D te2ture
isamplerHube
iimageHube
a handle &or accessin! an inte!er cu:e "a11ed te2ture
isamplerGCIect
iimageGCIect
a handle &or accessin! an inte!er #D rectan!le te2ture
isampler1CArray
iimage1CArray
a handle &or accessin! an inte!er 1D arra, te2ture
isamplerGCArray
iimageGCArray
a handle &or accessin! an inte!er #D arra, te2ture
isamplerJuffer
iimageJuffer
a handle &or accessin! an inte!er :u&&er te2ture
isamplerGCKS
iimageGCKS
a handle &or accessin! an inte!er #D "ulti9sa"1le te2ture
isamplerGCKSArray
iimageGCKSArray
a handle &or accessin! an inte!er #D "ulti9sa"1le arra, te2ture
isamplerHubeArray
iimageHubeArray
a handle &or accessin! an inte!er cu:e "a1 arra, te2ture
7nsi!ned Inte!er -1a<ue 0,1es
T&pe 8eaning
atomic%uint a handle &or accessin! an unsi!ned inte!er ato"ic counter
usampler1C
uimage1C
a handle &or accessin! an unsi!ned inte!er 1D te2ture
usamplerGC
uimageGC
a handle &or accessin! an unsi!ned inte!er #D te2ture
usamplerFC
uimageFC
a handle &or accessin! an unsi!ned inte!er %D te2ture
usamplerHube
uimageHube
a handle &or accessin! an unsi!ned inte!er cu:e "a11ed te2ture
usamplerGCIect
uimageGCIect
a handle &or accessin! an unsi!ned inte!er rectan!le te2ture
usampler1CArray
uimage1CArray
a handle &or accessin! an unsi!ned inte!er 1D arra, te2ture
usamplerGCArray
uimageGCArray
a handle &or accessin! an unsi!ned inte!er #D arra, te2ture
24
4 #aria$%e" and &'pe"
T&pe 8eaning
usamplerJuffer
uimageJuffer
a handle &or accessin! an unsi!ned inte!er :u&&er te2ture
usamplerGCKS
uimageGCKS
a handle &or accessin! an unsi!ned inte!er #D "ulti9sa"1le te2ture
usamplerGCKSArray
uimageGCKSArray
a handle &or accessin! an unsi!ned inte!er #D "ulti9sa"1le te2ture
arra,
usamplerHubeArray
uimageHubeArray
a handle &or accessin! an unsi!ned inte!er cu:e "a1 arra, te2ture
In addition, a shader can a!!re!ate these :asic t,1es usin! arra,s and structures to :uild "ore co"1le2
t,1es.
0here are no 1ointer t,1es.
".1.1 !oid
5unctions that do not return a value "ust :e declared as void. 0here is no de&ault &unction return t,1e.
0he e,word void cannot :e used in an, other declarations Be2ce1t &or e"1t, &or"al or actual 1ara"eter
listsC, or a co"1ile9ti"e error results.
".1.2 /ooleans
0o "ae conditional e2ecution o& code easier to e21ress, the t,1e bool is su11orted. 0here is no
e21ectation that hardware directl, su11orts varia:les o& this t,1e. It is a !enuine Boolean t,1e, holdin!
onl, one o& two values "eanin! either true or &alse. 0wo e,words true and false can :e used as literal
Boolean constants. Booleans are declared and o1tionall, initiali=ed as in the &ollow e2a"1le:
bool success; // declare success to be a :oolean
bool done 4 false; // declare and initiali/e done
0he ri!ht side o& the assi!n"ent o1erator B ; C "ust :e an e21ression whose t,1e is bool.
E21ressions used &or conditional Eu"1s Bif* for* ?=* w"ile* do(w"ileC "ust evaluate to the t,1e bool.
".1.# Integers
+i!ned and unsi!ned inte!er varia:les are &ull, su11orted. 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 e2actl, %# :its o& 1recision.
+i!ned inte!ers use %# :its, includin! a si!n :it, in twoFs co"1le"ent &or". Addition, su:traction, and
shi&t o1erations -1erations resultin! in over&low or under&low will not cause an, e2ce1tion, nor will the,
saturate, rather the, will Gwra1H to ,ield the low9order %# :its o& the result. Division and "ulti1lication
o1erations resultin! in over&low or under&low will not cause an, e2ce1tion :ut will result in an unde&ined
value.
Inte!ers are declared and o1tionall, initiali=ed with inte!er e21ressions, as in the &ollowin! e2a"1le:
int i8 ; 4 <=; // default integer literal t0!e is int
uint k 4 >u; // u establishes the t0!e as uint
25
4 #aria$%e" and &'pe"
Literal inte!er constants can :e e21ressed in deci"al B:ase 1)C, octal B:ase 4C, or he2adeci"al B:ase 1*C
as &ollows.
integer-constant :
*ecimal-constant integer-su&&i"o)t
octal-constant integer-su&&i"o)t
4e"a*ecimal-constant integer-su&&i"o)t
integer-su&&i": one o&
u T
*ecimal-constant :
non(ero-*igit
*ecimal-constant *igit
octal-constant :
+
octal-constant octal-*igit
4e"a*ecimal-constant :
)2 4e"a*ecimal-*igit
)\ 4e"a*ecimal-*igit
4e"a*ecimal-constant 4e"a*ecimal-*igit
*igit :
+
non(ero-*igit
non(ero-*igit : one o&
1 G F E Y Z [ \ ,
octal-*igit = one o&
+ 1 G F E Y Z [
4e"a*ecimal-*igit = one o&
+ 1 G F E Y Z [ \ ,
a b c d e f
A J H C & N
8o white s1ace is allowed :etween the di!its o& an inte!er constant, includin! a&ter the leadin! + or a&ter
the leadin! +x or +W o& a constant, or :e&ore the su&&i2 u or T. @hen toeni=in!, the "a2i"al toen
"atchin! the a:ove will :e reco!ni=ed :e&ore a new toen is started. @hen the su&&i2 u or T is 1resent,
the literal has t,1e uint, otherwise the t,1e is int. A leadin! unar, "inus si!n B9C is inter1reted as an
arith"etic unar, ne!ation, not as 1art o& the constant. /ence, literals the"selves are alwa,s e21ressed
with non9ne!ative s,nta2, thou!h the, could result in a ne!ative value.
It is a co"1ile9ti"e error to 1rovide a literal inte!er whose :it 1attern cannot &it in %# :its. 0he :it 1attern
o& the literal is alwa,s used un"odi&ied. +o a si!ned literal whose :it 1attern includes a set si!n :it
creates a ne!ative value. 5or e2a"1le,
26
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 ?x''''''''
int 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 ?x''''''''6
int g 4 >?????????; // a signed decimal literal taking >= bits8
// setting the sign bit8 g gets 31=A<ABC=AB
int h 4 ?xD???????; // oka08 >=3bit signed hexadecimal
int i 4 E?????????; // &))+)2 needs more than >= bits
int ; 4 ?x'''''''''; // &))+)2 needs more than >= bits
int k 4 ?xF???????; // k gets 3=1<C<F>B<F 44 ?xF???????
int l 4 =1<C<F>B<F; // l gets 3=1<C<F>B<F (the literal set the sign bit)
Des1ite all these e2a"1les initiali=in! varia:les, literals are reco!ni=ed and !iven values and t,1es
inde1endentl, o& their conte2t.
".1." .loating7-oint !ariables
+in!le91recision and dou:le91recision &loatin! 1oint varia:les are availa:le &or use in a variet, o& scalar
calculations. Generall,, the ter" &loating-)oint will re&er to :oth sin!le9 and dou:le91recision &loatin!
1oint. 5loatin!91oint varia:les are de&ined as in the &ollowin! e2a"1les:
float a8 b 4 16E; // single3!recision floating3!oint
double c8 d 4 =6?#'; // double3!recision floating3!oint
As an in1ut value to one o& the 1rocessin! units, a sin!le91recision or dou:le91recision &loatin!91oint
varia:le is e21ected to "atch the corres1ondin! IEEE '.( &loatin!91oint de&inition &or 1recision and
d,na"ic ran!e. 5loatin!91oint varia:les within a shader are also encoded accordin! to the IEEE '.(
s1eci&ication &or sin!le91recision &loatin!91oint values Blo!icall,, not necessaril, 1h,sicall,C. @hile
encodin!s are lo!icall, IEEE '.(, o1erations Baddition, "ulti1lication, etc.C are not necessaril, 1er&or"ed
as re<uired :, IEEE '.(. +ee section (.'.1 GRan!e and 3recisionH &or "ore details on 1recision and
usa!e o& 8a8s B8ot a 8u":erC and In&s B1ositive or ne!ative in&initiesC.
27
4 #aria$%e" and &'pe"
5loatin!91oint constants are de&ined as &ollows.
&loating-constant :
&ractional-constant e")onent-)art
o)t
&loating-su&&i"o)t

*igit-se3uence e")onent-)art &loating-su&&i"o)t
&ractional-constant :
*igit-se3uence . *igit-se3uence
*igit-se3uence .
. *igit-se3uence
e")onent-)art :
e sign
o)t
*igit-se3uence
E sign
o)t
*igit-se3uence
sign : one o&
- ]
*igit-se3uence :
*igit
*igit-se3uence *igit
&loating-su&&i": one o&
f N lf $N
A deci"al 1oint B . C is not needed i& the e21onent 1art is 1resent. 8o white s1ace "a, a11ear an,where
within a &loatin!91oint constant, includin! :e&ore a su&&i2. @hen toeni=in!, the "a2i"al toen "atchin!
the a:ove will :e reco!ni=ed :e&ore a new toen is started. @hen the su&&i2 Zl&Z or ZL5Z is 1resent, the
literal has t,1e double. -therwise, the literal has t,1e float. A leadin! unar, "inus si!n B(C is inter1reted
as a unar, o1erator and is not 1art o& the &loatin!91oint constant
".1.* !ectors
0he -1enGL +hadin! Lan!ua!e includes data t,1es &or !eneric #9, %9, and (9co"1onent vectors o&
&loatin!91oint values, inte!ers, or Booleans. 5loatin!91oint vector varia:les can :e used to store colors,
nor"als, 1ositions, te2ture coordinates, te2ture loou1 results and the lie. Boolean vectors can :e used
&or co"1onent9wise co"1arisons o& nu"eric vectors. +o"e e2a"1les 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.% 8atrices
0he -1enGL +hadin! Lan!ua!e has :uilt9in t,1es &or #[#, #[%, #[(, %[#, %[%, %[(, ([#, ([%, and ([(
"atrices o& &loatin!91oint nu":ers. ;atri2 t,1es :e!innin! with Z"atZ have sin!le91recision co"1onents
28
4 #aria$%e" and &'pe"
while "atri2 t,1es :e!innin! with Zd"atZ have dou:le91recision co"1onents. 0he &irst nu":er in the
t,1e is the nu":er o& colu"ns, the second is the nu":er o& rows. I& there is onl, one nu":er, the "atri2
is s<uare. E2a"1le "atri2 declarations:
mat= mat=-;
mat> o!tGatrix;
mat< view8 !ro;ection;
mat<x< view; // an alternate wa0 of declaring a mat<
mat>x= m; // a matrix with > columns and = rows
dmat< highHrecisionG(H;
dmat=x< dm;
Initiali=ation o& "atri2 values is done with constructors Bdescri:ed in section ..( G$onstructorsH C in
colu"n9"aEor order.
".1. Opa1ue T&pes
0he o1a<ue t,1es declare varia:les that are e&&ectivel, o1a<ue handles to other o:Eects. 0hese o:Eects are
accessed throu!h :uilt9in &unctions, not throu!h direct readin! or writin! o& the declared varia:le. 0he,
can onl, :e declared as &unction 1ara"eters or uniform(<uali&ied varia:les. 0he onl, o1a<ue t,1es that
tae "e"or, <uali&iers are the i"a!e t,1es. E2ce1t &or arra, inde2in!, structure "e":er selection, and
1arentheses, o1a<ue varia:les are not allowed to :e o1erands in e21ressionsJ such use results in a
co"1ile9ti"e error.
-1a<ue varia:les cannot :e treated as l9valuesJ hence cannot :e used as out or inout &unction 1ara"eters,
nor can the, :e assi!ned into. An, such use results in a co"1ile9ti"e error. /owever, the, can :e 1assed
as in 1ara"eters with "atchin! t,1e and "e"or, <uali&iers. 0he, are initiali=ed onl, throu!h the
-1enGL A3IJ the, cannot :e declared with an initiali=er in a shader.
Because a sin!le o1a<ue t,1e declaration e&&ectivel, declares two o:Eects, the o1a<ue handle itsel& and the
o:Eect it is a handle to, there is roo" &or :oth a stora!e <uali&ier and a "e"or, <uali&ier. 0he stora!e
<uali&ier will <uali&, the o1a<ue handle, while the "e"or, <uali&ier will <uali&, the o:Eect it as a handle
to.
".1..1 Samplers
+a"1ler t,1es Be.!., samplerGCC are o1a<ue t,1es, declared and :ehavin! as descri:ed a:ove &or o1a<ue
t,1es. @hen a!!re!ated into arra,s within a shader, sa"1lers can onl, :e inde2ed with a d,na"icall,
uni&or" inte!ral e21ression, otherwise results are unde&ined.
+a"1ler varia:les are handles to one9, two9, and three9 di"ensional te2tures, cu:e "a1s, de1th te2tures
Bshadowin!C, etc., as enu"erated in the :asic t,1es ta:les. 0here are distinct sa"1ler t,1es &or each
te2ture tar!et, and &or each o& &loat, inte!er, and unsi!ned inte!er data t,1es. 0e2ture accesses are done
throu!h :uilt9in te2ture &unctions Bdescri:ed in section 4.6 G0e2ture 5unctionsHC and sa"1lers are used to
s1eci&, which te2ture to access and how it is to :e &iltered.
29
4 #aria$%e" and &'pe"
".1..2 Images
I"a!e t,1es are o1a<ue t,1es, declared and :ehavin! as descri:ed a:ove &or o1a<ue t,1es. 0he, can :e
&urther <uali&ied with "e"or, <uali&iers. @hen a!!re!ated into arra,s within a shader, i"a!es can onl,
:e inde2ed with a d,na"icall, uni&or" inte!ral e21ression, otherwise results are unde&ined.
I"a!e varia:les are handles to one9, two9, or three9di"ensional i"a!es corres1ondin! to all or a 1ortion
o& a sin!le level o& a te2ture i"a!e :ound to an i"a!e unit. 0here are distinct i"a!e t,1es &or each te2ture
tar!et, and &or each o& &loat, inte!er, and unsi!ned inte!er data t,1es. I"a!e accesses should use an i"a!e
t,1e that "atches the tar!et o& the te2ture whose level is :ound to the i"a!e unit, or &or non9la,ered
:indin!s o& %D or arra, i"a!es should use the i"a!e t,1e that "atches the di"ensionalit, o& the la,er o&
the i"a!e Bi.e., a la,er o& %D, #DArra,, $u:e, or $u:eArra, 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,1e does
not "atch the :ound i"a!e in this "anner, i& the data t,1e does not "atch the :ound i"a!e, or i& the
&or"at la,out <uali&ier does not "atch the i"a!e unit &or"at as descri:ed in section 4.#. G0e2ture I"a!e
Loads and +toresH o& the -1enGL +1eci&ication, the results o& i"a!e accesses are unde&ined :ut cannot
include 1ro!ra" ter"ination.
I"a!e varia:les are used in the i"a!e load, store, and ato"ic &unctions descri:ed in section 4.1# ZI"a!e
5unctionsZ to s1eci&, an i"a!e to access.
".1..# 0tomic Counters
Ato"ic counter t,1es Batomic%uintC are o1a<ue handles to counters, declared and :ehavin! as descri:ed
a:ove &or o1a<ue t,1es. 0he varia:les the, declare s1eci&, which counter to access when usin! the :uilt9
in ato"ic counter &unctions as descri:ed in section 4.1) GAto"ic $ounter 5unctionsH. 0he, are :ound to
:u&&ers as descri:ed in section (.(.*.1 GAto"ic $ounter La,out >uali&iersH. @hen a!!re!ated into arra,s
within a shader, ato"ic counters can onl, :e inde2ed with a d,na"icall, uni&or" inte!ral e21ression,
otherwise results are unde&ined.
".1.4 Structures
7ser9de&ined t,1es can :e created :, a!!re!atin! other alread, de&ined t,1es into a structure usin! the
struct e,word. 5or e2a"1le,
struct light 5
float intensit0;
vec> !osition;
9 light(ar;
In this e2a"1le, lig4t :eco"es the na"e o& the new t,1e, and lig4tVar :eco"es a varia:le o& t,1e lig4t.
0o declare varia:les o& the new t,1e, use its na"e Bwithout the e,word structC.
light light(ar=;
3
4 #aria$%e" and &'pe"
;ore &or"all,, structures are declared as &ollows. /owever, the co"1lete correct !ra""ar is as !iven in
section 6 G+hadin! Lan!ua!e Gra""arH .
struct-*e&inition :
3uali&ier
o)t
struct name
o)t
5 member-list 6 *eclarators
o)t
9
member-list :
member-*eclaration9
member-*eclaration member-list9
member-*eclaration :
basic-t/)e *eclarators9
where name :eco"es the user9de&ined t,1e, and can :e used to declare varia:les to :e o& this new t,1e.
0he name shares the sa"e na"e s1ace as other varia:les, t,1es, and &unctions. All 1reviousl, visi:le
varia:les, t,1es, constructors, or &unctions with that na"e are hidden. 0he o1tional 3uali&ier onl, a11lies
to an, *eclarators, and is not 1art o& the t,1e :ein! de&ined &or name.
+tructures "ust have at least one "e":er declaration. ;e":er declarators "a, contain 1recision
<uali&iers, :ut use o& an, other <uali&ier results in a co"1ile9ti"e error. Bit &ields are not su11orted.
;e":er t,1es "ust :e alread, de&ined Bthere are no &orward re&erencesC. A co"1ile9ti"e error results i& a
"e":er declaration contains an initiali=er. ;e":er declarators can contain arra,s. +uch arra,s "ust
have a si=e s1eci&ied, and the si=e "ust :e an inte!ral constant e21ression thatFs !reater than =ero Bsee
section (.%.% G$onstant E21ressionsHC. Each level o& structure has its own na"e s1ace &or na"es !iven in
"e":er declaratorsJ such na"es need onl, :e uni<ue within that na"e s1ace.
Anon,"ous structures are not su11orted. E":edded structure de&initions are not su11orted. 0hese result
in co"1ile9ti"e errors.
struct * 5 float f; 9;
struct , 5
*; // &rror2 anon0mous structures disallowed
struct 5 666 9; // &rror2 embedded structures disallowed
* s; // +ka02 nested structures with name are allowed
9;
+tructures can :e initiali=ed at declaration ti"e usin! constructors, as discussed in section ..(.% G+tructure
$onstructorsH .
An, restrictions on the usa!e o& a t,1e or <uali&ier also a11l, to an, structure that contains a "e":er o&
that t,1e or <uali&ier. 0his also a11lies to structure "e":ers that are structures, recursivel,.
".1.9 0rra&s
Varia:les o& the sa"e t,1e can :e a!!re!ated into arra,s :, declarin! a na"e &ollowed :, :racets B 3 4 C
enclosin! an o1tional si=e. @hen an arra, si=e is s1eci&ied in a declaration, it "ust :e an inte!ral constant
e21ression Bsee section (.%.% G$onstant E21ressionsHC !reater than =ero. E2ce1t &or the last declared
"e":er o& a shader stora!e :loc Bsection (.%.6 GInter&ace BlocsHC, the si=e o& an arra, "ust :e declared
:e&ore it is inde2ed with an,thin! other than an inte!ral constant e21ression. 0he si=e o& an, arra, "ust
:e declared :e&ore 1assin! it as an ar!u"ent to a &unction. Violation o& an, o& these rules result in
31
4 #aria$%e" and &'pe"
co"1ile9ti"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,1e and s1eci&, a si=e. It is a co"1ile9ti"e error to declare an arra, with a si=e,
and then later Bin the sa"e shaderC inde2 the sa"e arra, with an inte!ral constant e21ression !reater than
or e<ual to the declared si=e. It is also a co"1ile9ti"e error to inde2 an arra, with a ne!ative constant
e21ression. Arra,s declared as &or"al 1ara"eters in a &unction declaration "ust s1eci&, a si=e.
7nde&ined :ehavior results &ro" inde2in! an arra, with a non9constant e21ression thatOs !reater than or
e<ual to the arra,Os si=e or less than ). Arra,s onl, have a sin!le di"ension Ba sin!le entr, within ZL MZC,
however, arra,s o& arra,s can :e declared. All t,1es B:asic t,1es, structures, arra,sC can :e &or"ed into
an arra,.
All arra,s are inherentl, ho"o!eneousJ "ade o& ele"ents all havin! the sa"e t,1e and si=e, with one
e2ce1tion. 0he e2ce1tion is a shader stora!e :loc havin! an unsi=ed arra, as its last "e":erJ an arra,
can :e &or"ed &ro" such a shader stora!e :loc, even i& the stora!e :locs have di&&erin! len!ths &or their
last "e":er.
+o"e e2a"1les are:
float frequenciesI>J;
uniform vec< lightHositionI<J;
light lightsIJ;
const int num#ights 4 =;
light lightsInum#ightsJ;
// a shader storage block8 introduced in section <6>6C buffer variables
buffer b 5
float uIJ; // an error8 unless u gets staticall0 si/ed b0 link time
vec< vIJ; // oka08 v will be si/ed d0namicall08 if not staticall0
9 nameI>J; // when the block is arra0ed8 all u will be the same si/e8
// but not necessaril0 all v8 if si/ed d0namicall0
An arra, t,1e can :e &or"ed :, s1eci&,in! a t,1e &ollowed :, s<uare :racets BL MC and includin! a si=e:
floatIEJ
0his t,1e can :e used an,where an, other t,1e can :e used, includin! as the return value &ro" a &unction
floatIEJ foo() 5 9
as a constructor o& an arra,
floatIEJ(>6<8 <6=8 E6?8 E6=8 161)
as an unna"ed 1ara"eter
void foo(floatIEJ)
and as an alternate wa, o& declarin! a varia:le or &unction 1ara"eter.
floatIEJ a;
Arra,s can have initiali=ers &or"ed &ro" arra, constructors:
32
4 #aria$%e" and &'pe"
float aIEJ 4 floatIEJ(>6<8 <6=8 E6?8 E6=8 161);
float aIEJ 4 floatIJ(>6<8 <6=8 E6?8 E6=8 161); // same thing
An arra, o& arra,s can :e declared as
vec< aI>JI=J; // si/e3> arra0 of si/e3= arra0 of vec<
which declares a one9di"ensional arra, o& si=e % o& one9di"ensional arra,s o& si=e # o& vec(s. 0hese
&ollowin! declarations do the sa"e thin!:
vec<I=J aI>J; // si/e3> arra0 of si/e3= arra0 of vec<
vec<I>JI=J a; // si/e3> arra0 of si/e3= arra0 of vec<
@hen in trans1arent "e"or, Blie in a uni&or" :locC, the la,out is that the inner9"ost Bri!ht9"ost in
declarationC di"ensions iterate &aster than outer di"ensions. 0hat is, &or the a:ove, the order in "e"or,
would :e:
Low address : aL)ML)M : aL)ML1M : aL1ML)M : aL1ML1M : aL#ML)M : aL#ML1M : /i!h address
0he t,1e o& a needed &or :oth constructors and na"eless 1ara"eters is Gvec(L%ML#MH:
vec< bI=J 4 vec<I=J(vec<(?6?)8 vec<(?61));
vec<I>JI=J a 4 vec<I>JI=J(b8 b8 b); // constructor
void foo(vec<I>JI=J); // !rotot0!e with unnamed !arameter
Alternativel,, the initiali=er9list s,nta2 can :e used to initiali=e an arra, o& arra,s:
vec< aI>JI=J 4 5 vec<I=J(vec<(?6?)8 vec<(16?))8
vec<I=J(vec<(?6?)8 vec<(16?))8
vec<I=J(vec<(?6?)8 vec<(16?)) 9;
7nsi=ed arra,s can :e e21licitl, si=ed :, an initiali=er at declaration ti"e:
float aIEJ;
666
float bIJ 4 a; // b is ex!licitl0 si/e E
float bIEJ 4 a; // means the same thing
float bIJ 4 floatIJ(18=8>8<8E); // also ex!licitl0 si/es to E
/owever, it is a co"1ile9ti"e error to assi!n to an i"1licitl, si=ed arra,. 8ote, this is a rare case that
initiali=ers and assi!n"ents a11ear to have di&&erent se"antics.
Arra,s now the nu":er o& ele"ents the, contain. 0his can :e o:tained :, usin! the lengt" "ethod:
float aIEJ;
a6length(); // returns E
0his returns a t,1e int. I& an arra, has :een e21licitl, si=ed, the value returned :, the len!th "ethod is a
constant e21ression. I& an arra, has not :een e21licitl, si=ed and is not the last declared "e":er o& a
shader stora!e :loc, the value returned :, the lengt" "ethod is not a constant e21ression and will :e
deter"ined when a 1ro!ra" is lined. I& an arra, has not :een e21licitl, si=ed and is the last declared
"e":er o& a shader stora!e :loc, the value returned will not :e a constant e21ression and will :e
33
4 #aria$%e" and &'pe"
deter"ined at run ti"e :ased on the si=e o& the :u&&er o:Eect 1rovidin! stora!e &or the :loc. 5or such
arra,s, the value returned :, the lengt" "ethod will :e unde&ined i& the arra, is contained in an arra, o&
shader stora!e :locs that is inde2ed with a non9constant e21ression less than =ero or !reater than or
e<ual to the nu":er o& :locs in the arra,.
0he lengt" "ethod cannot :e called on an arra, that has not ,et :een e21licitl, si=edJ this results in a
co"1ile9ti"e error.
0he lengt" "ethod wors e<uall, well &or arra,s o& arra,s:
vec< aI>JI=J;
a6length() // this is >
aIxJ6length() // this is =
@hen the lengt" "ethod returns a constant, the e21ression in :racets B" a:oveC will :e evaluated and
su:Eected to the rules re<uired &or arra, inde2es, :ut the arra, will not :e dere&erenced. 0hus, :ehavior is
well de&ined even i& the run9ti"e value o& the e21ression is out o& :ounds.
@hen the lengt" "ethod returns a run9ti"e value, the arra, will :e dere&erenced with the value ". I& " is
not a co"1ile9ti"e constant and is out o& ran!e, an unde&ined value results.

// for an arra0 b containing a member arra0 a2
bIKKxJ6a6length(); // b is never dereferenced8 but KKx is evaluated
// for an arra0 s of a shader storage ob;ect containing a member arra0 a2
sIxJ6a6length(); // s is dereferenced; x needs to be a valid index
5or unsi=ed arra,s, onl, the outer"ost di"ension can :e lacin! a si=e. A t,1e that includes an unnown
arra, si=e cannot :e &or"ed into an arra, until it !ets an e21licit si=e, e2ce1t &or shader stora!e :locs
where the onl, unsi=ed arra, "e":er is the last "e":er o& the :loc.
In a shader stora!e :loc, the last "e":er "a, :e declared without an e21licit si=e. In this case, the
e&&ective arra, si=e is in&erred at run9ti"e &ro" the si=e o& the data store :acin! the inter&ace :loc. +uch
unsi=ed arra,s "a, :e inde2ed with !eneral inte!er e21ressions. /owever, it is a co"1ile9ti"e error to
1ass the" as an ar!u"ent to a &unction or inde2 the" with a ne!ative constant e21ression.
34
4 #aria$%e" and &'pe"
".1.1$ Implicit Conversions
In so"e situations, an e21ression and its t,1e will :e i"1licitl, converted to a di&&erent t,1e. 0he
&ollowin! ta:le shows all allowed i"1licit conversions:
T&pe of e,pression Can be implicitl& converted to
int uint
int
uint
float
int
uint
float
double
ivecG uvecG
ivecF uvecF
ivecE uvecE
ivecG
uvecG
vecG
ivecF
uvecF
vecF
ivecE
uvecE
vecE
ivecG
uvecG
vecG
dvecG
ivecF
uvecF
vecF
dvecF
ivecE
uvecE
vecE
dvecE
matG dmatG
matF dmatF
matE dmatE
matGxF dmatGxF
matGxE dmatGxE
matFxG dmatFxG
matFxE dmatFxE
matExG dmatExG
matExF dmatExF
35
4 #aria$%e" and &'pe"
0here are no i"1licit arra, or structure conversions. 5or e2a"1le, an arra, o& int cannot :e i"1licitl,
converted to an arra, o& float.
@hen an i"1licit conversion is done, it is not a re9inter1retation o& the e21ressionFs :it 1attern, :ut a
conversion o& its value to an e<uivalent value in the new t,1e. 5or e2a"1le, the inte!er value 9Y will :e
converted to the &loatin!91oint value 9Y.+. Inte!er values havin! "ore :its o& 1recision than a sin!le9
1recision &loatin!91oint "antissa will lose 1recision when converted to float.
@hen 1er&or"in! i"1licit conversion &or :inar, o1erators, there "a, :e "ulti1le data t,1es to which the
two o1erands can :e converted. 5or e2a"1le, when addin! an int value to a uint value, :oth values can
:e i"1licitl, converted to uint, float, and double. In such cases, a &loatin!91oint t,1e is chosen i& either
o1erand has a &loatin!91oint t,1e. -therwise, an unsi!ned inte!er t,1e is chosen i& either o1erand has an
unsi!ned inte!er t,1e. -therwise, a si!ned inte!er t,1e is chosen. I& o1erands can :e i"1licitl, converted
to "ulti1le data t,1es derivin! &ro" the sa"e :ase data t,1e, the t,1e with the s"allest co"1onent si=e is
used.
0he conversions in the ta:le a:ove are done onl, as indicated :, other sections o& this s1eci&ication.
".1.11 Initiali:ers
At declaration, an initial value &or an a!!re!ate varia:le "a, :e 1rovided, s1eci&ied as an e<uals BVC
&ollowed :, an initiali=er. 0he initiali=er is either an assignment-e")ression or a list o& initiali=ers
enclosed in curl, :races. 0he !ra""ar &or the initiali=er is:
initiali(er :
assignment-e")ression
5 initiali(er-list 6
5 initiali(er-list , 6
initiali(er-list :
initiali(er
initiali(er-list * initiali(er
0he assignment-e")ression is a nor"al e21ression e2ce1t that a co""a B * C outside 1arentheses is
inter1reted as the end o& the initiali=er, not as the se<uence o1erator. As e21lained in "ore detail :elow,
this allows creation o& nested initiali=ers: 0he a!!re!ate and its initiali=er "ust e2actl, "atch in ter"s o&
nestin!, nu":er o& co"1onentsDele"entsD"e":ers 1resent at each level, and t,1es o&
co"1onentsDele"entsD"e":ers.
An assignment-e")ression in an initiali=er "ust :e either the sa"e t,1e as the o:Eect it initiali=es or :e a
t,1e that can :e converted to the o:EectFs t,1e accordin! to section (.1.1) ZI"1licit $onversionsZ. +ince
these include constructors, an a!!re!ate can :e initiali=ed :, either a constructor or an initiali=er listJ an
ele"ent in an initiali=er list can :e a constructor.
I& an initiali=er is a list o& initiali=ers enclosed in curl, :races, the varia:le :ein! declared "ust :e a
vector, a "atri2, an arra,, or a structure.
int i 4 5 1 9; // illegal8 i is not an aggregate
36
4 #aria$%e" and &'pe"
A list o& initiali=ers enclosed in a "atchin! set o& curl, :races is a11lied to one a!!re!ate. 0his "a, :e
the varia:le :ein! declared or an a!!re!ate contained in the varia:le :ein! declared. Individual
initiali=ers &ro" the initiali=er list are a11lied to the ele"entsD"e":ers o& the a!!re!ate, in order.
I& the a!!re!ate has a vector t,1e, initiali=ers &ro" the list are a11lied to the co"1onents o& the vector, in
order, startin! with co"1onent ). 0he nu":er o& initiali=ers "ust "atch the nu":er o& co"1onents.
I& the a!!re!ate has a "atri2 t,1e, initiali=ers &ro" the list "ust :e vector initiali=ers and are a11lied to
the colu"ns o& the "atri2, in order, startin! with colu"n ). 0he nu":er o& initiali=ers "ust "atch the
nu":er o& colu"ns.
I& the a!!re!ate has a structure t,1e, initiali=ers &ro" the list are a11lied to the "e":ers o& the structure,
in the order declared in the structure, startin! with the &irst "e":er. 0he nu":er o& initiali=ers "ust
"atch the nu":er o& "e":ers.
A11l,in! these rules, the &ollowin! "atri2 declarations are e<uivalent:
mat=x= a 4 mat=( vec=( 16?8 ?6? )8 vec=( ?6?8 16? ) );
mat=x= b 4 5 vec=( 16?8 ?6? )8 vec=( ?6?8 16? ) 9;
mat=x= c 4 5 5 16?8 ?6? 98 5 ?6?8 16? 9 9;
All o& the &ollowin! declarations result in a co"1ile9ti"e error.
float aI=J 4 5 >6<8 <6=8 E6? 9; // illegal
vec= b 4 5 16?8 =6?8 >6? 9; // illegal
mat>x> c 4 5 vec>(?6?)8 vec>(16?)8 vec>(=6?)8 vec>(>6?) 9; // illegal
mat=x= d 4 5 16?8 ?6?8 ?6?8 16? 9; // illegal8 can't flatten nesting
struct 5
float a;
int b;
9 e 4 5 16=8 =8 > 9; // illegal
In all cases, the inner"ost initiali=er Bi.e., not a list o& initiali=ers enclosed in curl, :racesC a11lied to an
o:Eect "ust have the sa"e t,1e as the o:Eect :ein! initiali=ed or :e a t,1e that can :e converted to the
o:EectFs t,1e accordin! to section (.1.1) ZI"1licit $onversionsZ. In the latter case, an i"1licit 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
37
4 #aria$%e" and &'pe"
struct 5
float a;
int b;
9 e 4 5 18 > 9; // legal8 first initiali/er is converted
All o& the &ollowin! declarations result in a co"1ile9ti"e error.
int a 4 true; // illegal
vec< bI=J 4 5 vec<(?6?)8 16? 9; // illegal
mat<x= c 4 5 vec>(?6?)8 vec>(16?) 9; // illegal
struct *1 5
vec< a;
vec< b;
9;
struct 5
float s;
float t;
9 dIJ 4 5 *1(vec<(?6?)8 vec<(161)) 9; // illegal
I& an initiali=er Bo& either &or"C is 1rovided &or an unsi=ed arra,, the si=e o& the arra, is deter"ined :, the
nu":er o& to19level Bnon9nestedC initiali=ers within the initiali=er. All o& the &ollowin! declarations create
arra,s e21licitl, si=ed with &ive ele"ents:
float aIJ 4 floatIJ(>6<8 <6=8 E6?8 E6=8 161);
float bIJ 4 5 >6<8 <6=8 E6?8 E6=8 161 9;
float cIJ 4 a; // c is ex!licitl0 si/e E
float dIEJ 4 b; // means the same thing
It is a co"1ile9ti"e error to have too &ew or too "an, initiali=ers in an initiali=er list &or the a!!re!ate
:ein! initiali=ed. 0hat is, all ele"ents o& an arra,, all "e":ers o& a structure, all colu"ns o& a "atri2, and
all co"1onents o& a vector "ust have e2actl, one initiali=er e21ression 1resent, with no unconsu"ed
initiali=ers.
".2 Scoping
0he sco1e o& a varia:le is deter"ined :, where it is declared. I& it is declared outside all &unction
de&initions, it has !lo:al sco1e, which starts &ro" where it is declared and 1ersists to the end o& the shader
it is declared in. I& it is declared in a w"ile test or a for state"ent, then it is sco1ed to the end o& the
&ollowin! su:9state"ent. I& it is declared in an if or else state"ent, it is sco1ed to the end o& that
state"ent. B+ee section *.# G+electionH and section *.% GIterationH &or the location o& state"ents and su:9
state"ents.C -therwise, i& it is declared as a state"ent within a co"1ound state"ent, it is sco1ed to the
end o& that co"1ound state"ent. I& it is declared as a 1ara"eter in a &unction de&inition, it is sco1ed until
the end o& that &unction de&inition. A &unctionFs 1ara"eter declarations and :od, to!ether &or" a sin!le
sco1e nested in the !lo:al sco1e. 0he if state"entOs e21ression does not allow new varia:les to :e
declared, hence does not &or" a new sco1e.
38
4 #aria$%e" and &'pe"
@ithin a declaration, the sco1e o& a na"e starts i""ediatel, a&ter the initiali=er i& 1resent or i""ediatel,
a&ter the na"e :ein! declared i& not. +everal e2a"1les:
int x 4 1;
5
int x 4 =8 0 4 x; // 0 is initiali/ed 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 initiali/ing nested first k
int m 4 k // use of new k8 which is hiding the !arameter
9
9
5or :oth for and w"ile loo1s, the su:9state"ent itsel& does not introduce a new sco1e &or varia:le na"es,
so the &ollowin! has a redeclaration co"1ile9ti"e error:
for ( /L nested sco!e begins here L/ int i 4 ?; i 7 1?; iKK) 5
int i; // redeclaration error
9
39
4 #aria$%e" and &'pe"
0he :od, o& a do(w"ile loo1 introduces a new sco1e lastin! onl, :etween the do and w"ile Bnot includin!
the while test e21ressionC, whether or not the :od, is si"1le or co"1ound:
int i 4 1C;
do
int i 4 <; // oka08 in nested sco!e
while (i 44 ?); // i is 1C8 sco!ed outside the do3while bod0
All varia:le na"es, structure t,1e na"es, and &unction na"es in a !iven sco1e share the sa"e na"e s1ace.
5unction na"es can :e redeclared in the sa"e sco1e, with the sa"e or di&&erent 1ara"eters, without error.
An i"1licitl, si=ed arra, can :e re9declared in the sa"e sco1e as an arra, o& the sa"e :ase t,1e.
-therwise, within one co"1ilation unit, a declared na"e cannot :e redeclared in the sa"e sco1eJ doin! so
results in a redeclaration co"1ile9ti"e error. I& a nested sco1e redeclares a na"e used in an outer sco1e,
it hides all e2istin! uses o& that na"e. 0here is no wa, to access the hidden na"e or "ae it unhidden,
without e2itin! the sco1e that hid it.
0he :uilt9in &unctions are sco1ed in a sco1e outside the !lo:al sco1e users declare !lo:al varia:les in.
0hat is, a shaderFs !lo:al sco1e, availa:le &or user9de&ined &unctions and !lo:al varia:les, is nested inside
the sco1e containin! the :uilt9in &unctions. @hen a &unction na"e is redeclared in a nested sco1e, it hides
all &unctions declared with that na"e in the outer sco1e. 5unction declarations B1rotot,1esC cannot occur
inside o& &unctionsJ the, "ust :e at !lo:al sco1e, or &or the :uilt9in &unctions, outside the !lo:al sco1e,
otherwise a co"1ile9ti"e error results.
+hared !lo:als are !lo:al varia:les declared with the sa"e na"e in inde1endentl, co"1iled units
BshadersC within the sa"e lan!ua!e Bi.e., sa"e sta!e, e.!., verte2C that are lined to!ether when "ain! a
sin!le 1ro!ra". BGlo:als &or"in! the inter&ace :etween two di&&erent shader lan!ua!es are discussed in
other sections.C +hared !lo:als share the sa"e na"e s1ace, and "ust :e declared with the sa"e t,1e.
0he, will share the sa"e stora!e.
+hared !lo:al arra,s "ust have the sa"e :ase t,1e and the sa"e e21licit si=e. An arra, i"1licitl, si=ed in
one shader can :e e21licitl, si=ed :, another shader in the sa"e sta!e. I& no shader in a sta!e has an
e21licit si=e &or the arra,, the lar!est i"1licit si=e Bone "ore than the lar!est inde2 usedC in that sta!e is
used. 0here is no cross9sta!e arra, si=in!. I& there is no static access to an i"1licitl, si=ed arra, within
the sta!e declarin! it, then the arra, is !iven a si=e o& 1, which is relevant when the arra, is declared
within an inter&ace :loc that is shared with other sta!es or the a11lication Bother unused arra,s "i!ht :e
eli"inated :, the o1ti"i=erC.
+hared !lo:al scalars "ust have e2actl, the sa"e t,1e na"e and t,1e de&inition. +tructures "ust have the
sa"e na"e, se<uence o& t,1e na"es, and t,1e de&initions, and "e":er na"es to :e considered the sa"e
t,1e. 0his rule a11lies recursivel, &or nested or e":edded t,1es. I& a shared !lo:al has "ulti1le
initiali=ers, the initiali=ers "ust all :e constant e21ressions, and the, "ust all have the sa"e value.
-therwise, a lin9ti"e error will result. BA shared !lo:al havin! onl, one initiali=er does not re<uire that
initiali=er to :e a constant e21ression.C
4
4 #aria$%e" and &'pe"
".# Storage ;ualifiers
Varia:le declarations "a, have at "ost one stora!e <uali&ier s1eci&ied in &ront o& the t,1e. 0hese are
su""ari=ed as
Storage ;ualifier 8eaning
T none: de&ault U local readDwrite "e"or,, or an in1ut 1ara"eter to a &unction
const a varia:le whose value cannot :e chan!ed
in lina!e into a shader &ro" a 1revious sta!e, varia:le is co1ied in
out lina!e out o& a shader to a su:se<uent sta!e, varia:le is co1ied out
attribute co"1ati:ilit, 1ro&ile onl, and verte2 lan!ua!e onl,J sa"e as in when in a
verte2 shader
uniform value does not chan!e across the 1ri"itive :ein! 1rocessed, uni&or"s
&or" the lina!e :etween a shader, -1enGL, and the a11lication
varying co"1ati:ilit, 1ro&ile onl, and verte2 and &ra!"ent lan!ua!es onl,J sa"e
as out when in a verte2 shader and sa"e as in when in a &ra!"ent shader
buffer value is stored in a :u&&er o:Eect, and can :e read or written :oth :,
shader invocations and the -1enGL A3I
s"ared co"1ute shader onl,J varia:le stora!e is shared across all wor ite"s in a
local wor !rou1
+o"e in1ut and out1ut <uali&ied varia:les can :e <uali&ied with at "ost one additional au2iliar, stora!e
<uali&ier:
0u,iliar& Storage
;ualifier
8eaning
centroid centroid9:ased inter1olation
sample 1er9sa"1le inter1olation
patc" 1er9tessellation91atch attri:utes
41
4 #aria$%e" and &'pe"
8ot all co":inations o& <uali&ication are allowed. @hich varia:le t,1es can have which <uali&iers are
s1eci&icall, de&ined in u1co"in! sections.
Local varia:les can onl, use the const stora!e <uali&ier Bor use no stora!e <uali&ierC.
5unction 1ara"eters can use const, in, and out <uali&iers, :ut as )arameter 3uali&iers. 3ara"eter
<uali&iers are discussed in section *.1.1 G5unction $allin! $onventionsH.
5unction return t,1es and structure "e":ers do not use stora!e <uali&iers.
Initiali=ers in !lo:al declarations "a, onl, :e used in declarations o& !lo:al varia:les with no stora!e
<uali&ier, with a const <uali&ier or with a uniform <uali&ier. Glo:al varia:les without stora!e <uali&iers
that are not initiali=ed in their declaration or :, the a11lication will not :e initiali=ed :, -1enGL, :ut
rather will enter main:; with unde&ined values.
@hen co"1arin! an out1ut &ro" one shader sta!e to an in1ut o& a su:se<uent shader sta!e, the in1ut and
out1ut donFt "atch i& their au2iliar, <uali&iers Bor lac thereo&C are not the sa"e.
".#.1 +efault Storage ;ualifier
I& no <uali&ier is 1resent on a !lo:al varia:le, then the varia:le has no lina!e to the a11lication or shaders
runnin! on other 1i1eline sta!es. 5or either !lo:al or local un<uali&ied varia:les, the declaration will
a11ear to allocate "e"or, associated with the 1rocessor it tar!ets. 0his varia:le will 1rovide readDwrite
access to this allocated "e"or,.
".#.2 Constant ;ualifier
8a"ed co"1ile9ti"e constants or read9onl, varia:les can :e declared usin! the const <uali&ier. 0he const
<uali&ier can :e used with an, o& the non9void trans1arent :asic data t,1es, as well as with structures and
arra,s o& these. It is a co"1ile9ti"e error to write to a const varia:le outside o& its declaration, so the,
"ust :e initiali=ed when declared. 5or e2a"1le,
const vec> /Dxis 4 vec> (?6?8 ?6?8 16?);
const float ceiling 4 a K b; // a and b not necessaril0 constants
+tructure "e":ers "a, not :e <uali&ied with const. +tructure varia:les can :e declared as const, and
initiali=ed with a structure constructor or initiali=er.
Initiali=ers &or const declarations at !lo:al sco1e "ust :e constant e21ressions, as de&ined in section (.%.%
G$onstant E21ressions.H
".#.# Constant (,pressions
A constant e")ression is one o&
a literal value Be.!., Y or trueC
a varia:le declared with the const <uali&ier and an initiali=er, where the initiali=er is a constant
e21ression
an e21ression &or"ed :, an o1erator on o1erands that are all constant e21ressions, includin! !ettin! an
ele"ent o& a constant arra,, or a "e":er o& a constant structure, or co"1onents o& a constant vector.
42
4 #aria$%e" and &'pe"
/owever, the lowest 1recedence o1erators o& the se<uence o1erator B * C and the assi!n"ent o1erators
B ;, -;, ...! are not included in the o1erators that can create a constant e21ression.
valid use o& the lengt"BC "ethod on an e21licitl, si=ed o:Eect, whether or not the o:Eect itsel& is
constant Bi"1licitl, si=ed or unsi=ed arra,s do not return a constant e21ressionC
a constructor whose ar!u"ents are all constant e21ressions
a :uilt9in &unction call whose ar!u"ents are all constant e21ressions, with the e2ce1tion o& the te2ture
loou1 &unctions and the noise &unctions. 0he :uilt9in &unctions dNdx, dNdy, and fwidt" "ust return
) when evaluated inside an initiali=er with an ar!u"ent that is a constant e21ression.
5unction calls to user9de&ined &unctions Bnon9:uilt9in &unctionsC cannot :e used to &or" constant
e21ressions.
An integral constant e")ression is a constant e21ression that evaluates to a scalar si!ned or unsi!ned
inte!er.
$onstant e21ressions will :e alwa,s :e evaluated in an invariant wa,, inde1endent o& use o& invariant
and precise <uali&ication, so as to create the sa"e value in "ulti1le shaders when the sa"e constant
e21ressions a11ear in those shaders. +ee section (.4.1 G0he Invariant >uali&ierH and section (.6 G0he
3recise >uali&ierH &or "ore details on how to create invariant e21ressions. $onstant e21ressions "a, :e
evaluated :, the co"1ilerFs host 1lat&or", and are there&ore not re<uired to co"1ute the sa"e value that
the sa"e e21ression would evaluate to on the shader e2ecution tar!et. /owever, the host "ust use the
sa"e or !reater 1recision than the tar!et would use.
".#." Input !ariables
+hader in1ut varia:les are declared with the stora!e <uali&ier in. 0he, &or" the in1ut inter&ace :etween
1revious sta!es o& the -1enGL 1i1eline and the declarin! shader. In1ut varia:les "ust :e declared at
!lo:al sco1e. Values &ro" the 1revious 1i1eline sta!e are co1ied into in1ut varia:les at the :e!innin! o&
shader e2ecution. It is a co"1ile9ti"e error to write to a varia:le declared as an in1ut.
-nl, the in1ut varia:les that are staticall, read need to :e written :, the 1revious sta!eJ it is allowed to
have su1er&luous declarations o& in1ut varia:les. 0his is shown in the &ollowin! ta:le.
0reat"ent o& ;is"atched In1ut
Varia:les
$onsu"in! +hader Bin1ut varia:lesC
8o Declaration Declared :ut no
+tatic 7se
Declared and
+tatic 7se
Generatin!
+hader
Bout1ut
varia:lesC
8o Declaration Allowed Allowed Lin90i"e Error
Declared :ut no
+tatic 7se
Allowed Allowed
Allowed
Bvalues are unde&inedC
Declared and
+tatic 7se
Allowed Allowed
Allowed
Bvalues are 1otentiall,
unde&inedC
Errors are :ased on static use onl,. $o"1ilation "a, !enerate a warnin!, :ut not an error, &or an,
d,na"ic use the co"1iler can deduce that "i!ht cause consu"1tion o& unde&ined values.
43
4 #aria$%e" and &'pe"
+ee section ' GBuilt9in Varia:lesH &or a list o& the :uilt9in in1ut na"es.
Verte2 shader in1ut varia:les Bor attri:utesC receive 1er9verte2 data. 0he, are declared in a verte2 shader
with the in <uali&ier. It is a co"1ile9ti"e error to use an, au2iliar, or inter1olation <uali&ier on a verte2
shader in1ut. 0he values co1ied in are esta:lished :, the -1enGL A3I or throu!h the use o& the la,out
identi&ier location. Verte2 shader in1uts can :e sin!le9 or dou:le91recision &loatin!91oint scalars, vectors,
and "atrices, or si!ned9 and unsi!ned9inte!er scalars and vectors. Verte2 shader in1uts can also &or"
arra,s o& these t,1es, :ut not structures.
E2a"1le declarations in a verte2 shader:
in vec< !osition;
in vec> normal;
in vec= texMoordI<J;
It is e21ected that !ra1hics hardware will have a s"all nu":er o& &i2ed vector locations &or 1assin! verte2
in1uts. 0here&ore, the -1enGL +hadin! lan!ua!e de&ines each non9"atri2 in1ut varia:le as tain! u1 one
such vector location. 0here is an i"1le"entation de1endent li"it on the nu":er o& locations that can :e
used, and i& this is e2ceeded it will cause a lin9ti"e error. BDeclared in1ut varia:les that are not staticall,
used do not count a!ainst this li"it.C A scalar in1ut counts the sa"e a"ount a!ainst this li"it as a vecE,
so a11lications "a, want to consider 1acin! !rou1s o& &our unrelated &loat in1uts to!ether into a vector
to :etter utili=e the ca1a:ilities o& the underl,in! hardware. A "atri2 in1ut will use u1 "ulti1le locations.
0he nu":er o& locations used will e<ual the nu":er o& colu"ns in the "atri2.
0essellation control, evaluation, and !eo"etr, shader in1ut varia:les !et the 1er9verte2 values written out
:, out1ut varia:les o& the sa"e na"es in the 1revious active shader sta!e. 5or these in1uts, centroid and
inter1olation <uali&iers are allowed, :ut have no e&&ect. +ince tessellation control, tessellation evaluation,
and !eo"etr, shaders o1erate on a set o& vertices, each in1ut varia:le Bor in1ut :loc, see inter&ace :locs
:elowC needs to :e declared as an arra,. 5or e2a"1le,
in float fooIJ; // geometr0 shader in!ut for vertex out float foo
Each ele"ent o& such an arra, corres1onds to one verte2 o& the 1ri"itive :ein! 1rocessed. Each arra, can
o1tionall, have a si=e declared. 0he arra, si=e will :e set :,, Bor i& 1rovided "ust :e consistent withC the
in1ut layout declarationBsC esta:lishin! the t,1e o& in1ut 1ri"itive, as descri:ed later in section (.(.1
GIn1ut La,out >uali&iersH.
+o"e in1uts and out1uts are arra/e*, "eanin! that &or an inter&ace :etween two shader sta!es either the
in1ut or out1ut declaration re<uires an e2tra level o& arra, inde2in! &or the declarations to "atch. 5or
e2a"1le, with the inter&ace :etween a verte2 shader and a !eo"etr, shader, verte2 shader out1ut varia:les
and !eo"etr, shader in1ut varia:les o& the sa"e na"e "ust have "atchin! t,1es, e2ce1t that the
!eo"etr, shader will have one "ore arra, di"ension than the verte2 shader, to allow &or verte2 inde2in!.
I& such an arra,ed inter&ace varia:le is not declared with the necessar, additional in1ut or out1ut arra,
di"ension, a lin9ti"e error will result.
5or non9arra,ed inter&aces B"eanin! arra, di"ensionall, sta,s the sa"e :etween sta!esC, it is a lin9ti"e
error i& the in1ut varia:le is not declared with the sa"e t,1e, includin! arra, di"ensionalit,, as the
"atchin! out1ut varia:le.
Additionall,, tessellation evaluation shaders su11ort 1er91atch in1ut varia:les declared with the patc" and
in <uali&iers. 3er91atch in1ut varia:les are &illed with the values o& 1er91atch out1ut varia:les written :,
44
4 #aria$%e" and &'pe"
the tessellation control shader. 3er91atch in1uts "a, :e declared as one9di"ensional arra,s, :ut are not
inde2ed :, verte2 nu":er. A11l,in! the patc" <uali&ier to in1uts can onl, :e done in tessellation
evaluation shaders. As with other in1ut varia:les, 1er91atch in1uts "ust :e declared usin! the sa"e t,1e
and <uali&ication as 1er91atch out1uts &ro" the 1revious Btessellation controlC shader sta!e.
5ra!"ent shader in1uts !et 1er9&ra!"ent values, t,1icall, inter1olated &ro" a 1revious sta!eFs out1uts.
0he, are declared in &ra!"ent shaders with the in stora!e <uali&ier. 0he au2iliar, stora!e <uali&iers
centroid and sample can also :e a11lied, as well as the inter1olation <uali&iers flat, noperspective, and
smoot". It is a co"1ile9ti"e error to use patc" in a &ra!"ent shader. 5ra!"ent in1uts can onl, :e
si!ned and unsi!ned inte!ers and inte!er vectors, &loatin!91oint scalars, &loatin!91oint vectors, "atrices,
or arra,s or structures o& these. 5ra!"ent shader in1uts that are si!ned or unsi!ned inte!ers, inte!er
vectors, or an, dou:le91recision &loatin!91oint t,1e "ust :e <uali&ied with the inter1olation <uali&ier flat.
5ra!"ent in1uts are declared as in the &ollowin! e2a"1les:
in vec> normal;
centroid in vec= ,exMoord;
invariant centroid in vec< Molor;
no!ers!ective in float tem!erature;
flat in vec> m0Molor;
no!ers!ective centroid in vec= m0,exMoord;
0he &ra!"ent shader in1uts &or" an inter&ace with the last active shader in the verte2 1rocessin! 1i1eline.
5or this inter&ace, the last active shader sta!e out1ut varia:les and &ra!"ent shader in1ut varia:les o& the
sa"e na"e "ust "atch in t,1e and <uali&ication, with a &ew e2ce1tions: 0he stora!e <uali&iers "ust, o&
course, di&&er Bone is in and one is outC. Also, inter1olation <uali&ication Be.!., flatC and au2iliar,
<uali&ication Be.!. centroidC "a, di&&er. 0hese "is"atches are allowed :etween an, 1air o& sta!es. @hen
inter1olation or au2iliar, <uali&iers do not "atch, those 1rovided in the &ra!"ent shader su1ersede those
1rovided in 1revious sta!es. I& an, such <uali&iers are co"1letel, "issin! in the &ra!"ent shaders, then
the de&ault is used, rather than an, <uali&iers that "a, have :een declared in 1revious sta!es. 0hat is,
what "atters is what is declared in the &ra!"ent shaders, not what is declared in shaders in 1revious
sta!es.
@hen an inter&ace :etween shader sta!es is &or"ed usin! shaders &ro" two se1arate 1ro!ra" o:Eects, it is
not 1ossi:le to detect "is"atches :etween in1uts and out1uts when the 1ro!ra"s are lined. @hen there
are "is"atches :etween in1uts and out1uts on such inter&aces, the values 1assed across the inter&ace will
:e 1artiall, or co"1letel, unde&ined. +haders can ensure "atches across such inter&aces either :, usin!
in1ut and out1ut la,out <uali&iers Bsections (.(.1 GIn1ut La,out >uali&iersH and (.(.# G-ut1ut La,out
>uali&iersHC or :, usin! identical in1ut and out1ut declarations o& :locs or varia:les. $o"1lete rules &or
inter&ace "atchin! :etween 1ro!ra"s are &ound in section '.(.1 G+hader Inter&ace ;atchin!H o& the
-1enGL Gra1hics +,ste" +1eci&ication.
$o"1ute shaders do not 1er"it user9de&ined in1ut varia:les and do not &or" a &or"al inter&ace with an,
other shader sta!e. +ee section '.1 GBuilt9In Varia:lesH &or a descri1tion o& :uilt9in co"1ute shader in1ut
varia:les. All other in1ut to a co"1ute shader is retrieved e21licitl, throu!h i"a!e loads, te2ture &etches,
loads &ro" uni&or"s or uni&or" :u&&ers, or other user su11lied code. Redeclaration o& :uilt9in in1ut
varia:les in co"1ute shaders is not 1er"itted.
45
4 #aria$%e" and &'pe"
".#.* 5niform !ariables
0he uniform <uali&ier is used to declare !lo:al varia:les whose values are the sa"e across the entire
1ri"itive :ein! 1rocessed. All uniform varia:les are read9onl, and are initiali=ed e2ternall, either at lin
ti"e or throu!h the A3I. 0he lin9ti"e initial value is either the value o& the varia:leFs initiali=er, i&
1resent, or ) i& no initiali=er is 1resent. -1a<ue t,1es cannot have initiali=ers, or a co"1ile9ti"e error
results.
E2a"1le declarations are:
uniform vec< lightHosition;
uniform vec> color 4 vec>(?6C8 ?6C8 ?6=); // value assigned at link time
0he uniform <uali&ier can :e used with an, o& the :asic data t,1es, or when declarin! a varia:le whose
t,1e is a structure, or an arra, o& an, o& these.
0here is an i"1le"entation de1endent li"it on the a"ount o& stora!e &or uni&or"s that can :e used &or
each t,1e o& shader and i& this is e2ceeded it will cause a co"1ile9ti"e or lin9ti"e error. 7ni&or"
varia:les that are declared :ut not used do not count a!ainst this li"it. 0he nu":er o& user9de&ined
uni&or" varia:les and the nu":er o& :uilt9in uni&or" varia:les that are used within a shader are added
to!ether to deter"ine whether availa:le uni&or" stora!e has :een e2ceeded.
I& "ulti1le shaders are lined to!ether, then the, will share a sin!le !lo:al uni&or" na"e s1ace, includin!
within a lan!ua!e as well as across lan!ua!es. /ence, the t,1es and initiali=ers o& uni&or" varia:les with
the sa"e na"e "ust "atch across all shaders that are lined into a sin!le 1ro!ra".
It is le!al &or so"e shaders to 1rovide an initiali=er &or a 1articular uni&or" varia:le, while another shader
does not, :ut all 1rovided initiali=ers "ust :e e<ual.
".#.% Output !ariables
+hader out1ut varia:les are declared with a stora!e <uali&ier usin! the e,word out. 0he, &or" the out1ut
inter&ace :etween the declarin! shader and the su:se<uent sta!es o& the -1enGL 1i1eline. -ut1ut
varia:les "ust :e declared at !lo:al sco1e. Durin! shader e2ecution the, will :ehave as nor"al
un<uali&ied !lo:al varia:les. 0heir values are co1ied out to the su:se<uent 1i1eline sta!e on shader e2it.
-nl, out1ut varia:les that are read :, the su:se<uent 1i1eline sta!e need to :e writtenJ it is allowed to
have su1er&luous declarations o& out1ut varia:les.
0here is not an inout stora!e <uali&ier at !lo:al sco1e &or declarin! a sin!le varia:le na"e as :oth in1ut
and out1ut to a shader. Also, a varia:le cannot :e declared with :oth the in and the out <uali&iers, this
will result in a co"1ile9ti"e or lin9ti"e error. -ut1ut varia:les "ust :e declared with di&&erent na"es
than in1ut varia:les. /owever, nestin! an in1ut or out1ut inside an inter&ace :loc with an instance na"e
allows the sa"e na"es with one re&erenced throu!h a :loc instance na"e.
Verte2, tessellation evaluation, and !eo"etr, out1ut varia:les out1ut 1er9verte2 data and are declared
usin! the out stora!e <uali&ier. A11l,in! patc" to an out1ut can onl, :e done in a tessellation control
shader. -ut1ut varia:les can onl, :e &loatin!91oint scalars, &loatin!91oint vectors, "atrices, si!ned or
unsi!ned inte!ers or inte!er vectors, or arra,s or structures o& an, these.
46
4 #aria$%e" and &'pe"
Individual verte2, tessellation evaluation, and !eo"etr, out1uts are declared as in the &ollowin! e2a"1les:
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;
0hese can also a11ear in inter&ace :locs, as descri:ed in section (.%.6 GInter&ace BlocsH. Inter&ace
:locs allow si"1ler addition o& arra,s to the inter&ace &ro" verte2 to !eo"etr, shader. 0he, also allow a
&ra!"ent shader to have the sa"e in1ut inter&ace as a !eo"etr, shader &or a !iven verte2 shader.
0essellation control shader out1ut varia:les are "a, :e used to out1ut 1er9verte2 and 1er91atch data. 3er9
verte2 out1ut varia:les are arra,ed Bsee arra/e* under (.%.( In1utsC and declared usin! the out <uali&ier
without the patc" <uali&ier. 3er91atch out1ut varia:les are declared usin! the patc" and out <uali&iers.
3er9verte2 and 1er91atch out1ut varia:les can onl, :e &loatin!91oint scalars, &loatin!91oint vectors,
"atrices, si!ned or unsi!ned inte!ers or inte!er vectors, or arra,s or structures o& an, these. +ince
tessellation control shaders 1roduce an arra,ed 1ri"itive co"1risin! "ulti1le vertices, each 1er9verte2
out1ut varia:le Bor out1ut :loc, see inter&ace :locs :elowC needs to :e declared as an arra,. 5or
e2a"1le,
out float fooIJ; // feeds next stage in!ut in float fooIJ
Each ele"ent o& such an arra, corres1onds to one verte2 o& the 1ri"itive :ein! 1roduced. Each arra, can
o1tionall, have a si=e declared. 0he arra, si=e will :e set :, Bor i& 1rovided "ust :e consistent withC the
out1ut la,out declarationBsC esta:lishin! the nu":er o& vertices in the out1ut 1atch, as descri:ed later in
section (.(.#.1 G0essellation $ontrol -ut1utsH.
Each tessellation control shader invocation has a corres1ondin! out1ut 1atch verte2, and "a, assi!n
values to 1er9verte2 out1uts onl, i& the, :elon! to that corres1ondin! verte2. I& a 1er9verte2 out1ut
varia:le is used as an l9value, it is a co"1ile9ti"e error i& the e21ression indicatin! the verte2 inde2 is not
the identi&ier gl0nvocation0D.
0he order o& e2ecution o& a tessellation control shader invocation relative to the other invocations &or the
sa"e in1ut 1atch is unde&ined unless the :uilt9in &unction barrierBC is used. 0his 1rovides so"e control
over relative e2ecution order. @hen a shader invocation calls barrierBC, its e2ecution 1auses until all
other invocations have reached the sa"e 1oint o& e2ecution. -ut1ut varia:le assi!n"ents 1er&or"ed :,
an, invocation e2ecuted 1rior to callin! barrierBC will :e visi:le to an, other invocation a&ter the call to
barrierBC returns.
Because tessellation control shader invocations e2ecute in unde&ined order :etween :arriers, the values o&
1er9verte2 or 1er91atch out1ut varia:les will so"eti"es :e unde&ined. $onsider the :e!innin! and end o&
shader e2ecution and each call to barrierBC as s,nchroni=ation 1oints. 0he value o& an out1ut varia:le
will :e unde&ined in an, o& the three &ollowin! cases:
1. At the :e!innin! o& e2ecution.
#. At each s,nchroni=ation 1oint, unless
the value was well9de&ined a&ter the 1revious s,nchroni=ation 1oint and was not written :, an,
47
4 #aria$%e" and &'pe"
invocation since, or
the value was written :, e2actl, one shader invocation since the 1revious s,nchroni=ation
1oint, or
the value was written :, "ulti1le shader invocations since the 1revious s,nchroni=ation 1oint,
and the last write 1er&or"ed :, all such invocations wrote the sa"e value.
%. @hen read :, a shader invocation, i&
the value was unde&ined at the 1revious s,nchroni=ation 1oint and has not :een writen :, the
sa"e shader invocation since, or
the out1ut varia:le is written to :, an, other shader invocation :etween the 1revious and ne2t
s,nchroni=ation 1oints, even i& that assi!n"ent occurs in code &ollowin! the read.
5ra!"ent out1uts out1ut 1er9&ra!"ent data and are declared usin! the out stora!e <uali&ier. It is a
co"1ile9ti"e error to use au2iliar, stora!e <uali&iers or inter1olation <uali&iers on an out1ut in a &ra!"ent
shader. 5ra!"ent out1uts can onl, :e float, sin!le91recision &loatin!91oint vectors, si!ned or unsi!ned
inte!ers or inte!er vectors, or arra,s o& an, these. It is a co"1ile9ti"e error to declare an, dou:le9
1recision t,1e, "atri2, or structure as an out1ut. 5ra!"ent out1uts are declared as in the &ollowin!
e2a"1les:
out vec< 'ragmentMolor;
out uint #uminosit0;
$o"1ute shaders have no :uilt9in out1ut varia:les, do not su11ort user9de&ined out1ut varia:les and do
not &or" a &or"al inter&ace with an, other shader sta!e. All out1uts &ro" a co"1ute shader tae the &or"
o& the side e&&ects such as i"a!e stores and o1erations on ato"ic counters.
".#. /uffer !ariables
0he buffer <uali&ier is used to declare !lo:al varia:les whose values are stored in the data store o& a
:u&&er o:Eect :ound throu!h the -1enGL A3I. Bu&&er varia:les can :e read and written with the
underl,in! stora!e shared a"on! all active shader invocations. Bu&&er varia:le "e"or, reads and writes
within a sin!le shader invocation are 1rocessed in order. /owever, the order o& reads and writes
1er&or"ed in one invocation relative to those 1er&or"ed :, another invocation is lar!el, unde&ined.
Bu&&er varia:les "a, :e <uali&ied with "e"or, <uali&iers a&&ectin! how the underl,in! "e"or, is
accessed, as descri:ed in section (.1) G;e"or, >uali&iersH.
0he buffer <uali&ier can :e used with an, o& the :asic data t,1es, or when declarin! a varia:le whose t,1e
is a structure, or an arra, o& an, o& these.
Bu&&er varia:les "a, onl, :e declared inside inter&ace :locs Bsection (.%.6 GInter&ace BlocsHC, which
are then re&erred to as shader stora!e :locs. It is a co"1ile9ti"e error to declare :u&&er varia:les at
!lo:al sco1e Boutside a :locC. Bu&&er varia:les cannot have initiali=ers.
48
4 #aria$%e" and &'pe"
// use buffer to create a buffer block (shader storage block)
buffer :uffer%ame 5 // externall0 visible name of buffer
int count; // t0!ed8 shared memor0666
666 // 666
vec< vIJ; // last element ma0 be an arra0 that is not si/ed
// until after link time (d0namicall0 si/ed)
9 %ame; // name of block within the shader
0here are i"1le"entation9de1endent li"its on the nu":er o& shader stora!e :locs used &or each t,1e o&
shader, the co":ined nu":er o& shader stora!e :locs used &or a 1ro!ra", and the a"ount o& stora!e
re<uired :, each individual shader stora!e :loc. I& an, o& these li"its are e2ceeded, it will cause a
co"1ile9ti"e or lin9ti"e error.
I& "ulti1le shaders are lined to!ether, then the, will share a sin!le !lo:al :u&&er varia:le na"e s1ace,
includin! within a lan!ua!e as well as across lan!ua!es. /ence, the t,1es o& :u&&er varia:les with the
sa"e na"e "ust "atch across all shaders that are lined into a sin!le 1ro!ra".
".#.4 Shared !ariables
0he s"ared <uali&ier is used to declare varia:les that have stora!e shared :etween all wor ite"s
co"1ute shader local wor !rou1. Varia:les declared as s"ared "a, onl, :e used in co"1ute shaders
Bsee section #.* G$o"1ute 3rocessorHC. +hared varia:les are i"1licitl, coherent. 0hat is, writes to shared
varia:les &ro" one shader invocation will eventuall, :e seen :, other invocations within the sa"e local
wor !rou1.
Varia:les declared as s"ared "a, not have initiali=ers and their contents are unde&ined at the :e!innin!
o& shader e2ecution. An, data written to shared varia:les will :e visi:le to other shaders e2ecutin! the
sa"e shader within the sa"e local wor !rou1. -rder o& e2ecution with res1ect to reads and writes to the
sa"e shared varia:le :, di&&erent invocations o& a shader is not de&ined. In order to achieve orderin! with
res1ect to reads and writes to shared varia:les, "e"or, :arriers "ust :e e"1lo,ed usin! the barrierBC
&unction Bsee section 4.1* G+hader Invocation $ontrol 5unctionsHC.
0here is a li"it to the total si=e o& all varia:les declared as shared in a sin!le 1ro!ra". 0his li"it,
e21ressed in units o& :asic "achine units "a, :e deter"ined :, usin! the -1enGL A3I to <uer, the value
o& ;A\?$-;370E?+/ARED?;E;-RA?+I]E.
".#.9 Interface /loc2s
In1ut, out1ut, uni&or", and :u&&er varia:le declarations can :e !rou1ed into na"ed inter&ace :locs to
1rovide coarser !ranularit, :acin! than is achieva:le with individual declarations. 0he, can have an
o1tional instance na"e, used in the shader to re&erence their "e":ers. An out1ut :loc o& one
1ro!ra""a:le sta!e is :aced :, a corres1ondin! in1ut :loc in the su:se<uent 1ro!ra""a:le sta!e. A
uni&or" :loc is :aced :, the a11lication with a :u&&er o:Eect. A :loc o& :u&&er varia:les, called a
shader stora!e :loc, is also :aced :, the a11lication with a :u&&er o:Eect. It is a co"1ile9ti"e error to
have an in1ut :loc in a verte2 shader or an out1ut :loc in a &ra!"ent shaderJ these uses are reserved &or
&uture use.
49
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 o1en curl, :race B 5 C as &ollows:
inter&ace-bloc, :
la/out-3uali&ieropt inter&ace-3uali&ier bloc,-name 5 member-list 6 instance-nameopt >
inter&ace-3uali&ier :
in
out
uniform
buffer
member-list :
member-*eclaration
member-*eclaration member-list
member-*eclaration :
la/out-3uali&ieropt 3uali&iersopt t/)e *eclarators >
instance-name :
i*enti&ier
i*enti&ier < =
i*enti&ier < integral-constant-e")ression =
Each o& the a:ove ele"ents is discussed :elow, with the e2ce1tion o& la,out <uali&iers Bla/out-3uali&ier;,
which are de&ined in the ne2t section.
5irst, an e2a"1le,
uniform ,ransform 5
mat< Godel(iewGatrix;
mat< Godel(iewHro;ectionGatrix;
uniform mat> %ormalGatrix; // allowed restatement of qualifier
float -eformation;
9;
0he a:ove esta:lishes a uni&or" :loc na"ed G0rans&or"H with &our uni&or"s !rou1ed inside it.
0,1es and declarators are the sa"e as &or other in1ut, out1ut, uni&or", and :u&&er varia:le declarations
outside :locs, with these e2ce1tions:
initiali=ers are not allowed
o1a<ue t,1es are not allowed
structure de&initions cannot :e nested inside a :loc
An, o& these would result in a co"1ile9ti"e error. -therwise, :uilt9in t,1es, 1reviousl, declared
structures, and arra,s o& these are allowed as the t,1e o& a declarator in the sa"e "anner the, are allowed
outside a :loc.
I& no o1tional <uali&ier is used in a "e":er9declaration, the <uali&ication o& the varia:le is Eust in, out,
uniform, or buffer as deter"ined :, inter&ace-3uali&ier. I& o1tional <uali&iers are used, the, can include
inter1olation <uali&iers, au2iliar, stora!e <uali&iers, and stora!e <uali&iers and the, "ust declare an in1ut,
5
4 #aria$%e" and &'pe"
out1ut, or uni&or" varia:le consistent with the inter&ace <uali&ier o& the :loc: In1ut varia:les, out1ut
varia:les, uni&or" varia:les, and buffer varia:les can onl, :e in in :locs, out :locs, uniform :locs,
and shader stora!e :locs, res1ectivel,. Re1eatin! the in, out, uniform, or buffer inter&ace <uali&ier &or
a "e":erFs stora!e <uali&ier is o1tional. 5or e2a"1le,
in Gaterial 5
smooth in vec< Molor1; // legal8 in!ut inside in block
smooth vec< Molor=; // legal8 'in' inherited from 'in Gaterial'
vec= ,exMoord; // legal8 ,exMoord is an in!ut
uniform float Dtten; // illegal8 mismatched storage qualifier
9;
5or this section, de&ine an inter&ace to :e one o& these
All the uni&or" varia:les o& a 1ro!ra". 0his s1ans all co"1ilation units lined to!ether within one
1ro!ra".
All the buffer varia:les o& a 1ro!ra".
0he :oundar, :etween adEacent 1ro!ra""a:le 1i1eline sta!es: 0his s1ans all the out1uts in all
co"1ilation units o& the &irst sta!e and all the in1uts in all co"1ilation units o& the second sta!e.
0he :loc na"e Bbloc,-nameC is used to "atch inter&aces: an out1ut :loc o& one 1i1eline sta!e will :e
"atched to an in1ut :loc with the sa"e na"e in the su:se<uent 1i1eline sta!e. 5or uni&or" :locs, the
a11lication uses the :loc na"e to identi&, the :loc. Bloc na"es have no other use within a shader
:e,ond inter&ace "atchin!J it is a co"1ile9ti"e error to use a :loc na"e at !lo:al sco1e &or an,thin!
other than as a :loc na"e Be.!., use o& a :loc na"e &or a !lo:al varia:le na"e or &unction na"e is
currentl, reservedC. ;atched :loc na"es within an inter&ace Bas de&ined a:oveC "ust "atch in ter"s o&
havin! the sa"e nu":er o& declarations with the sa"e se<uence o& t,1es and the sa"e se<uence o&
"e":er na"es, as well as havin! the sa"e "e":er9wise la,out <uali&ication Bsee ne2t sectionC. ;atched
uni&or" :loc na"es B:ut not in1ut or out1ut :loc na"esC "ust also either all :e lacin! an instance
na"e or all havin! an instance na"e, 1uttin! their "e":ers at the sa"e sco1in! level. @hen instance
na"es are 1resent on "atched :loc na"es, it is allowed &or the instance na"es to di&&erJ the, need not
"atch &or the :locs to "atch. 5urther"ore, i& a "atchin! :loc is declared as an arra,, then the arra,
si=es "ust also "atch Bor &ollow arra, "atchin! rules &or the inter&ace :etween a verte2 and a !eo"etr,
shaderC. An, "is"atch will !enerate a lin9ti"e error. A :loc na"e is allowed to have di&&erent
de&initions in di&&erent inter&aces within the sa"e shader, allowin!, &or e2a"1le, an in1ut :loc and out1ut
:loc to have the sa"e na"e.
51
4 #aria$%e" and &'pe"
I& an instance na"e Binstance-nameC is not used, the na"es declared inside the :loc are sco1ed at the
!lo:al level and accessed as i& the, were declared outside the :loc. I& an instance na"e Binstance-nameC
is used, then it 1uts all the "e":ers inside a sco1e within its own na"e s1ace, accessed with the &ield
selector B . C o1erator Banalo!ousl, to structuresC. 5or e2a"1le,
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 Bi.e., in the A3IC, "e":ers are si"ilarl, identi&ied e2ce1t the :loc na"e is
alwa,s used in 1lace o& the instance na"e BA3I accesses are to inter&aces, not to shadersC. I& there is no
instance na"e, then the A3I does not use the :loc na"e to access a "e":er, Eust 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;
52
4 #aria$%e" and &'pe"
5or :locs declared as arra,s, the arra, inde2 "ust also :e included when accessin! "e":ers, as in this
e2a"1le
uniform ,ransform 5 // DH$ uses ,ransformI=J to refer to instance =
mat< Godel(iewGatrix;
mat< Godel(iewHro;ectionGatrix;
vec< aIJ; // arra0 will get im!licitl0 si/ed
float -eformation;
9 transformsI<J;
666
666 4 transformsI=J6Godel(iewGatrix; // shader access of instance =
// DH$ uses ,ransform6Godel(iewGatrix to quer0 an offset or other quer0
transformsIxJ6a6length(); // same length for 'a' for all x
,ransformIxJ; // illegal8 must use 'transforms'
,ransform6a6length(); // illegal8 must use 'transforms'
666transformsI=J6aI>J666 // if these are the onl0 two dereferences of 'a'8
666transformsI>J6aICJ666 // then 'a' must be si/e F8 for all transformsIxJ
5or uni&or" or shader stora!e :locs declared as an arra,, each individual arra, ele"ent corres1onds to a
se1arate :u&&er9o:Eect :ind ran!e, :acin! one instance o& the :loc. As the arra, si=e indicates the
nu":er o& :u&&er o:Eects needed, uni&or" and shader stora!e :loc arra, declarations "ust s1eci&, an
arra, si=e. A uni&or" or shader stora!e :loc arra, can onl, :e inde2ed with a d,na"icall, uni&or"
inte!ral e21ression, otherwise results are unde&ined.
@hen usin! -1enGL A3I entr, 1oints to identi&, the na"e o& an individual :loc in an arra, o& :locs,
the na"e strin! "ust include an arra, inde2 Be.!., 5rans&orm<2=C. @hen usin! -1enGL A3I entr, 1oints
to re&er to o&&sets or other characteristics o& a :loc "e":er, an arra, inde2 "ust not :e s1eci&ied Be.!.,
5rans&orm.!o*elVie2!atri"C.
Geo"etr, shader in1ut :locs "ust :e declared as arra,s and &ollow the arra, declaration and linin!
rules &or all !eo"etr, shader in1uts. All other in1ut and out1ut :loc arra,s "ust s1eci&, an arra, si=e.
0here are i"1le"entation de1endent li"its on the nu":er o& uni&or" :locs and the nu":er o& shader
stora!e :locs that can :e used 1er sta!e. I& either li"it is e2ceeded, it will cause a lin9ti"e error.
"." La&out ;ualifiers
La,out <uali&iers can a11ear in several &or"s o& declaration. 0he, can a11ear as 1art o& an inter&ace
:loc de&inition or :loc "e":er, as shown in the !ra""ar in the 1revious section. 0he, can also a11ear
with Eust an inter&ace <uali&ier Ba stora!e <uali&ier that is in, out, or uniformC to esta:lish la,outs o& other
declarations "ade with that inter&ace <uali&ier:
la/out-3uali&ier inter&ace-3uali&ier >
-r, the, can a11ear with an individual varia:le declared with an inter&ace <uali&ier:
la/out-3uali&ier inter&ace-3uali&ier *eclaration >
Declarations o& la,outs can onl, :e "ade at !lo:al sco1e, and onl, where indicated in the &ollowin!
su:sectionsJ their details are s1eci&ic to what the inter&ace <uali&ier is, and are discussed individuall,.
53
4 #aria$%e" and &'pe"
0he la/out-3uali&ier e21ands to
la/out-3uali&ier :
layout la/out-3uali&ier-i*-list !
la/out-3uali&ier-i*-list :
la/out-3uali&ier-i*
la/out-3uali&ier-i* * la/out-3uali&ier-i*-list
la/out-3uali&ier-i*
la/out-3uali&ier-name
la/out-3uali&ier-name V la/out-3uali&ier-value
s"ared
0he toens used &or la/out-3uali&ier-name are identi&iers, not e,words, however, the s"ared e,word is
allowed as a la/out-3uali&ier-i*. Generall,, the, can :e listed in an, order. -rder9de1endent "eanin!s
e2ist onl, i& e21licitl, called out :elow. +i"ilarl,, these identi&iers are not case sensitive, unless
e21licitl, noted otherwise.
;ore than one la,out <uali&ier "a, a11ear in a sin!le declaration. I& the sa"e la/out-3uali&ier-name
occurs in "ulti1le la,out <uali&iers &or the sa"e declaration, the last one overrides the &or"er ones.
".".1 Input La&out ;ualifiers
+o"e in1ut la,out <uali&iers a11l, to all shader lan!ua!es and so"e a11l, onl, to s1eci&ic lan!ua!es.
0he latter are discussed in se1arate sections :elow.
All shaders, e2ce1t co"1ute shaders, allow in1ut la,out location <uali&iers on in1ut varia:le declarations.
0he location la,out <uali&ier identi&ier &or in1uts is:
la/out-3uali&ier-i*
location ; integer-constant
-nl, one ar!u"ent is acce1ted. 5or e2a"1le,
la0out(location 4 >) in vec< normal;
will esta:lish that the shader in1ut normal is assi!ned to vector location nu":er %. 5or verte2 shader
in1uts, the location s1eci&ies the nu":er o& the !eneric verte2 attri:ute &ro" which in1ut values are taen.
5or in1uts o& all other shader t,1es, the location s1eci&ies a vector nu":er that can :e used to "atch
a!ainst out1uts &ro" a 1revious shader sta!e, even i& that shader is in a di&&erent 1ro!ra" o:Eect.
I& a verte2 shader in1ut is an, scalar or vector t,1e, it will consu"e a sin!le location. I& a non9verte2
shader in1ut is a scalar or vector t,1e other than dvecF or dvecE, it will consu"e a sin!le location, while
t,1es dvecF or dvecE will consu"e two consecutive locations. In1uts o& t,1e double and dvecG will
consu"e onl, a sin!le location, in all sta!es.
I& the declared in1ut is an arra, o& si=e n and each ele"ent taes m locations, it will :e assi!ned m R n
consecutive locations startin! with the location s1eci&ied. 5or e2a"1le,
la0out(location 4 B) in vec< colorsI>J;
will esta:lish that the shader in1ut colors is assi!ned to vector location nu":ers *, ', and 4.
54
4 #aria$%e" and &'pe"
I& the declared in1ut is an n 2 m sin!le9 or dou:le91recision "atri2, it will :e assi!ned "ulti1le locations
startin! with the location s1eci&ied. 0he nu":er o& locations assi!ned &or each "atri2 will :e the sa"e as
&or an n9ele"ent arra, o& m9co"1onent vectors. 5or e2a"1le,
la0out(location 4 A) in mat< transformsI=J;
will esta:lish that shader in1ut trans&orms is assi!ned to vector locations 691*, with trans&orms<0= :ein!
assi!ned to locations 691# and trans&orms<1= :ein! assi!ned to locations 1%91*.
I& the declared in1ut 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 s1eci&ied &or the structure. 0he nu":er o&
locations consu"ed :, a structure "e":er is deter"ined :, a11l,in! the rules a:ove recursivel, as
thou!h the structure "e":er were declared as an in1ut varia:le o& the sa"e t,1e. 5or e2a"1le,
la0out(location 4 >) struct * 5
vec> a;
mat= b;
vec< cI=J;
9 s;
will assi!n location % to s.a, locations ( and . to the two colu"n vectors o& s.b, and locations * and ' to
s.c.
Location la,out <uali&iers "a, :e used on in1ut varia:les declared as structures, :ut not on individual
"e":ers. Location la,out <uali&iers "a, not :e used on in1ut :locs or in1ut :loc "e":ers. $o"1ile9
ti"e errors result i& these rules are not &ollowed.
0he nu":er o& in1ut locations availa:le to a shader is li"ited. 5or verte2 shaders, the li"it is the
advertised nu":er o& verte2 attri:utes. 5or all other shaders, the li"it is i"1le"entation9de1endent and
"ust :e no less than one &ourth o& the advertised "a2i"u" in1ut co"1onent count. A 1ro!ra" will &ail to
lin i& an, attached shader uses a location !reater than or e<ual to the nu":er o& su11orted locations,
unless device9de1endent o1ti"i=ations are a:le to "ae the 1ro!ra" &it within availa:le hardware
resources.
A 1ro!ra" will &ail to lin i& an, two non9verte2 shader in1ut varia:les are assi!ned to the sa"e location.
5or verte2 shaders, "ulti1le in1ut varia:les "a, :e assi!ned to the sa"e location usin! either la,out
<uali&iers or via the -1enGL A3I. /owever, such aliasin! is intended onl, to su11ort verte2 shaders
where each e2ecution 1ath accesses at "ost one in1ut 1er each location. I"1le"entations are 1er"itted,
:ut not re<uired, to !enerate lin9ti"e errors i& the, detect that ever, 1ath throu!h the verte2 shader
e2ecuta:le accesses "ulti1le in1uts assi!ned to an, sin!le location. 5or all shader t,1es, a 1ro!ra" will
&ail to lin i& e21licit location assi!n"ents leave the liner una:le to &ind s1ace &or other varia:les without
e21licit assi!n"ents.
5or the 1ur1oses o& deter"inin! i& a non9verte2 in1ut "atches an out1ut &ro" a 1revious shader sta!e, the
location la,out <uali&ier Bi& an,C "ust "atch.
I& a verte2 shader in1ut varia:le with no location assi!ned in the shader te2t has a location s1eci&ied
throu!h the -1enGL A3I, the A3I9assi!ned location will :e used. -therwise, such varia:les will :e
assi!ned a location :, the liner. +ee section 11.1.1 GVerte2 Attri:utesH o& the -1enGL Gra1hics +,ste"
+1eci&ication &or "ore details. A lin9ti"e error will occur i& an in1ut varia:le is declared in "ulti1le
shaders o& the sa"e lan!ua!e with con&lictin! locations.
55
4 #aria$%e" and &'pe"
".".1.1 Tessellation (valuation Inputs
Additional in1ut la,out <uali&ier identi&iers allowed &or tessellation evaluation shaders are:
la/out-3uali&ier-i*
triangles
Muads
isolines
eMual%spacing
fractional%even%spacing
fractional%odd%spacing
cw
ccw
point%mode
-ne su:set o& these identi&iers, )rimitive mo*e, is used to s1eci&, a tessellation 1ri"itive "ode to :e used
:, the tessellation 1ri"itive !enerator. 0o s1eci&, a 1ri"itive "ode, the identi&ier "ust :e one o&
triangles, Muads, or isolines, which s1eci&, that the tessellation 1ri"itive !enerator should su:divide a
trian!le into s"aller trian!les, a <uad into trian!les, or a <uad into a collection o& lines, res1ectivel,.
A second su:set o& these identi&iers, verte" s)acing, is used to s1eci&, the s1acin! used :, the tessellation
1ri"itive !enerator when su:dividin! an ed!e. 0o s1eci&, verte2 s1acin!, the identi&ier "ust :e one o&
the &ollowin!.
eMual%spacing si!ni&,in! that ed!es should :e divided into a collection o& e<ual9si=ed se!"ents.
fractional%even%spacing si!ni&,in! that ed!es should :e divided into an even nu":er o& e<ual9
len!th se!"ents 1lus two additional shorter Z&ractionalZ se!"ents.
fractional%odd%spacing si!ni&,in! that ed!es should :e divided into an odd nu":er o& e<ual9
len!th se!"ents 1lus two additional shorter Z&ractionalZ se!"ents.
A third su:set o& these identi&iers, or*ering, s1eci&ies whether the tessellation 1ri"itive !enerator
1roduces trian!les in clocwise or counter9clocwise order, accordin! to the coordinate s,ste" de1icted
in the -1enGL s1eci&ication. 0he orderin! identi&iers cw and ccw indicate clocwise and counter9
clocwise trian!les, res1ectivel,. I& the tessellation 1ri"itive !enerator does not 1roduce trian!les,
orderin! is i!nored.
5inall,, )oint mo*e. is s1eci&ied with the identi&ier point%mode indicatin! the tessellation 1ri"itive
!enerator should 1roduce a 1oint &or each uni<ue verte2 in the su:divided 1ri"itive, rather than
!eneratin! lines or trian!les.
An, or all o& these identi&iers "a, :e s1eci&ied one or "ore ti"es in a sin!le in1ut la,out declaration. I&
1ri"itive "ode, verte2 s1acin!, or orderin! is declared "ore than once in the tessellation evaluation
shaders o& a 1ro!ra", all such declarations "ust use the sa"e identi&ier.
At least one tessellation evaluation shader Bco"1ilation unitC in a 1ro!ra" "ust declare a 1ri"itive "ode
in its in1ut la,out. Declarin! verte2 s1acin!, orderin!, or 1oint "ode identi&iers is o1tional. It is not
re<uired that all tessellation evaluation shaders in a 1ro!ra" declare a 1ri"itive "ode. I& s1acin! or
verte2 orderin! declarations are o"itted, the tessellation 1ri"itive !enerator will use e<ual s1acin! or
56
4 #aria$%e" and &'pe"
counter9clocwise verte2 orderin!, res1ectivel,. I& a 1oint "ode declaration is o"itted, the tessellation
1ri"itive !enerator will 1roduce lines or trian!les accordin! to the 1ri"itive "ode.
".".1.2 Geometr& Shader Inputs
Additional la,out <uali&ier identi&iers &or !eo"etr, shader in1uts include )rimitive identi&iers and an
invocation count identi&ier:
la/out-3uali&ier-i*
points
lines
lines%adLacency
triangles
triangles%adLacency
invocations ; integer-constant
0he identi&iers points, lines, lines%adLacency, triangles, and triangles%adLacency are used to s1eci&, the
t,1e o& in1ut )rimitive acce1ted :, the !eo"etr, shader, and onl, one o& these is acce1ted. At least one
!eo"etr, shader Bco"1ilation unitC in a 1ro!ra" "ust declare this in1ut 1ri"itive la,out, and all !eo"etr,
shader in1ut la,out declarations in a 1ro!ra" "ust declare the sa"e la,out. It is not re<uired that all
!eo"etr, shaders in a 1ro!ra" declare an in1ut 1ri"itive la,out.
0he identi&ier invocations is used to s1eci&, the nu":er o& ti"es the !eo"etr, shader e2ecuta:le is
invoed &or each in1ut 1ri"itive received. Invocation count declarations are o1tional. I& no invocation
count is declared in an, !eo"etr, shader in a 1ro!ra", the !eo"etr, shader will :e run once &or each
in1ut 1ri"itive. I& an invocation count is declared, all such declarations "ust s1eci&, the sa"e count. I& a
shader s1eci&ies an invocation count !reater than the i"1le"entation9de1endent "a2i"u", it will &ail to
co"1ile.
5or e2a"1le,
la0out(triangles8 invocations 4 B) in;
will esta:lish that all in1uts to the !eo"etr, shader are trian!les and that the !eo"etr, shader e2ecuta:le
is run si2 ti"es &or each trian!le 1rocessed.
All !eo"etr, shader in1ut unsi=ed arra, declarations will :e si=ed :, an earlier in1ut 1ri"itive la,out
<uali&ier, when 1resent, as 1er the &ollowin! ta:le.
$ayout Size of Pnput Arrays
1oints 1
lines #
lines?adEacenc, (
trian!les %
trian!les?adEacenc, *
57
4 #aria$%e" and &'pe"
0he intrinsicall, declared in1ut arra, glin<= will also :e si=ed :, an, in1ut 1ri"itive9la,out declaration.
/ence, the e21ression
gl"in6length()
will return the value &ro" the ta:le a:ove.
5or in1uts declared without an arra, si=e, includin! intrinsicall, declared in1uts Bi.e., glinC, a la,out "ust
:e declared :e&ore an, use o& the "ethod lengt" or other an, arra, use that re<uires the arra, si=e to :e
nown.
It is a co"1ile9ti"e error i& a la,out declarationFs arra, si=e B&ro" ta:le a:oveC does not "atch all the
e21licit arra, si=es s1eci&ied in declarations o& an in1ut varia:les in the sa"e shader. 0he &ollowin!
includes e2a"1les o& co"1ile9ti"e errors:
// code sequence within one shader666
in vec< Molor1IJ; // legal8 si/e still unknown
in vec< Molor=I=J; // legal8 si/e is =
in vec< Molor>I>J; // illegal8 in!ut si/es are inconsistent
la0out(lines) in; // legal for Molor=8 in!ut si/e is =8 matching Molor=
in vec< Molor<I>J; // 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 1rovided si=es Bsi=ed in1ut arra,s and la,out si=eC "atch across all
!eo"etr, shaders in a 1ro!ra".
".".1.# .ragment Shader Inputs
Additional &ra!"ent la,out <uali&ier identi&iers include the &ollowin! &or glFrag$oor*
la/out-3uali&ier-i*
origin%upper%left
pixel%center%integer
B, de&ault, glFrag$oor* assu"es a lower9le&t ori!in &or window coordinates and assu"es 1i2el centers
are located at hal&91i2el coordinates. 5or e2a"1le, the B". /C location B).., )..C is returned &or the lower9
le&t9"ost 1i2el in a window. 0he ori!in can :e chan!ed :, redeclarin! glFrag$oor* with the
origin%upper%left identi&ier, "ovin! the ori!in o& glFrag$oor* to the u11er le&t o& the window, with /
increasin! in value toward the :otto" o& the window. 0he values returned can also :e shi&ted :, hal& a
1i2el in :oth " and / :, pixel%center%integer so it a11ears the 1i2els are centered at whole nu":er 1i2el
o&&sets. 0his "oves the B", /C value returned :, glFrag$oor* o& B).., )..C :, de&ault, to B).), ).)C with
pixel%center%integer.
58
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& glFrag$oor* is redeclared in an, &ra!"ent shader in a 1ro!ra", it "ust :e redeclared in all the
&ra!"ent shaders in that 1ro!ra" that have a static use glFrag$oor*. All redeclarations o&
glFrag$oor* in all &ra!"ent shaders in a sin!le 1ro!ra" "ust have the sa"e set o& <uali&iers. @ithin
an, shader, the &irst redeclarations o& glFrag$oor* "ust a11ear :e&ore an, use o& glFrag$oor*. 0he
:uilt9in glFrag$oor* is onl, 1redeclared in &ra!"ent shaders, so redeclarin! it in an, other shader
lan!ua!e results in a co"1ile9ti"e error.
Redeclarin! glFrag$oor* with origin%upper%left andDor pixel%center%integer <uali&iers onl, a&&ects
glFrag$oor*." and glFrag$oor*./. It has no a&&ect on rasteri=ation, trans&or"ation, or an, other 1art
o& the -1enGL 1i1eline or lan!ua!e &eatures.
5ra!"ent shaders also allow the &ollowin! la,out <uali&ier on in onl, Bnot with varia:le declarationsC
la/out-3uali&ier-i*
early%fragment%tests
to re<uest that &ra!"ent tests :e 1er&or"ed :e&ore &ra!"ent shader e2ecution, as descri:ed in section
1..#.( GEarl, 5ra!"ent 0estsH o& the -1enGL +1eci&ication.
5or e2a"1le,
la0out(earl0"fragment"tests) in;
+1eci&,in! this will "ae 1er9&ra!"ent tests :e 1er&or"ed :e&ore &ra!"ent shader e2ecution. I& this is not
declared, 1er9&ra!"ent tests will :e 1er&or"ed a&ter &ra!"ent shader e2ecution. -nl, one &ra!"ent shader
Bco"1ilation unitC need declare this, thou!h "ore than one can. I& at least one declares this, then it is
ena:led.
".".1." Compute Shader Inputs
0here are no la,out location <uali&iers &or co"1ute shader in1uts.
La,out <uali&ier identi&iers &or co"1ute shader in1uts are the wor9!rou1 si=e <uali&iers:
la/out-3uali&ier-i*
local%size%x V integer-constant
local%size%y V integer-constant
local%size%z V integer-constant
@here local%size%x, local%size%y, and local%size%z are used to de&ine the local si=e o& the ernel de&ined
:, the co"1ute shader in the &irst, second, and third di"ension, res1ectivel,. 0he de&ault si=e in each
di"ension is 1. I& a shader does not s1eci&, a si=e &or one o& the di"ensions, that di"ension will have a
si=e o& 1.
59
4 #aria$%e" and &'pe"
5or e2a"1le, the &ollowin! declaration in a co"1ute shader
la0out (local"si/e"x 4 >=8 local"si/e"0 4 >=) in;
is used to declare a two9di"ensional co"1ute shader with a local si=e o& %# \ %# ele"ents, which is
e<uivalent to a three9di"ensional co"1ute shader where the third di"ension has si=e one.
As another e2a"1le, the declaration
la0out (local"si/e"x 4 F) in;
e&&ectivel, s1eci&ies that a one9di"ensional co"1ute shader is :ein! co"1iled, and its si=e is 4 ele"ents.
I& the local si=e o& the shader in an, di"ension is !reater than the "a2i"u" si=e su11orted :, the
i"1le"entation &or that di"ension, a co"1ile9ti"e error results. Also, i& such a la,out <uali&ier is
declared "ore than once in the sa"e shader, all those declarations "ust set the sa"e set o& local wor9
!rou1 si=es and set the" to the sa"e valuesJ otherwise a co"1ile9ti"e error results. I& "ulti1le co"1ute
shaders attached to a sin!le 1ro!ra" o:Eect declare local wor9!rou1 si=e, the declarations "ust :e
identicalJ otherwise a lin9ti"e error results.
5urther"ore, i& a 1ro!ra" o:Eect contains an, co"1ute shaders, at least one "ust contain an in1ut la,out
<uali&ier s1eci&,in! the local wor si=es o& the 1ro!ra", or a lin9ti"e error will occur.
".".2 Output La&out ;ualifiers
+o"e out1ut la,out <uali&iers a11l, to all shader lan!ua!es and so"e a11l, onl, to s1eci&ic lan!ua!es.
0he latter are discussed in se1arate sections :elow.
All shaders, e2ce1t co"1ute shaders, allow location out1ut la,out <uali&iers on out1ut varia:le
declarations. 0he location la,out <uali&ier identi&ier &or out1uts is:
la/out-3uali&ier-i*
location ; integer-constant
5ra!"ent shaders allow an additional index out1ut la,out <uali&iers:
la/out-3uali&ier-i*
location ; integer-constant
index ; integer-constant
Each o& these <uali&iers "a, a11ear at "ost once. I& index is s1eci&ied, location "ust also :e s1eci&ied.
I& index is not s1eci&ied, the value ) is used. 5or e2a"1le, in a &ra!"ent shader,
la0out(location 4 >) out vec< color;
will esta:lish that the &ra!"ent shader out1ut color is assi!ned to &ra!"ent color % as the &irst Binde2 =eroC
in1ut to the :lend e<uation. And,
la0out(location 4 >8 index 4 1) out vec< factor;
will esta:lish that the &ra!"ent shader out1ut &actor is assi!ned to &ra!"ent color % as the second Binde2
oneC in1ut to the :lend e<uation.
6
4 #aria$%e" and &'pe"
5or &ra!"ent9shader out1uts, the location and inde2 s1eci&, the color out1ut nu":er and inde2 receivin!
the values o& the out1ut. 5or out1uts o& all other shader sta!es, the location s1eci&ies a vector nu":er that
can :e used to "atch a!ainst in1uts in a su:se<uent shader sta!e, even i& that shader is in a di&&erent
1ro!ra" o:Eect.
I& a declared out1ut is a scalar or vector t,1e other than dvecF or dvecE, it will consu"e a sin!le location.
-ut1uts o& t,1e dvecF or dvecE will consu"e two consecutive locations. -ut1uts o& t,1e double and
dvecG will consu"e onl, a sin!le location, in all sta!es.
I& the declared out1ut is an arra,, it will :e assi!ned consecutive locations startin! with the location
s1eci&ied. 5or e2a"1le,
la0out(location 4 =) out vec< colorsI>J;
will esta:lish that colors is assi!ned to vector location nu":ers #, %, and (.
I& the declared out1ut is an n 2 m sin!le9 or dou:le91recision "atri2, it will :e assi!ned "ulti1le locations
startin! with the location s1eci&ied. 0he nu":er o& locations assi!ned will :e the sa"e as &or an n9
ele"ent arra, o& m9co"1onent vectors.
I& the declared out1ut 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 s1eci&ied &or the structure. 0he nu":er o&
locations consu"ed :, a structure "e":er is deter"ined :, a11l,in! the rules a:ove recursivel, as
thou!h the structure "e":er were declared as an out1ut varia:le o& the sa"e t,1e.
Location la,out <uali&iers "a, :e used on out1ut varia:les declared as structures, :ut not on individual
"e":ers. Location la,out <uali&iers "a, not :e used on out1ut :locs or out1ut :loc "e":ers.
$o"1ile9ti"e errors result i& these rules are not &ollowed.
0he nu":er o& out1ut locations availa:le to a shader is li"ited. 5or &ra!"ent shaders, the li"it is the
advertised nu":er o& draw :u&&ers. 5or all other shaders, the li"it is i"1le"entation9de1endent and "ust
:e no less than one &ourth o& the advertised "a2i"u" out1ut co"1onent count. B$o"1ute shaders have
no out1uts.C A 1ro!ra" will &ail to lin i& an, attached shader uses a location !reater than or e<ual to the
nu":er o& su11orted locations, unless device9de1endent o1ti"i=ations are a:le to "ae the 1ro!ra" &it
within availa:le hardware resources. $o"1ile9ti"e errors "a, also :e !iven i& at co"1ile ti"e it is
nown the lin will &ail. A ne!ative out1ut location will result in a co"1ile9ti"e error. It is also a
co"1ile9ti"e error i& a &ra!"ent shader sets a la,out inde2 to less than ) or !reater than 1.
A 1ro!ra" will &ail to lin i& an, o& the &ollowin! occur:
an, two &ra!"ent shader out1ut varia:les are assi!ned to the sa"e location and inde2, or
an, two !eo"etr, shader out1ut varia:les are assi!ned the sa"e location and strea", or
i& an, two out1ut varia:les &ro" the sa"e verte2 or tessellation shader sta!e are assi!ned to the
sa"e location.
5or &ra!"ent shader out1uts, locations can :e assi!ned usin! either a la,out <uali&ier or via the -1enGL
A3I. 5or all shader t,1es, a 1ro!ra" will &ail to lin i& e21licit location assi!n"ents leave the liner
una:le to &ind s1ace &or other varia:les without e21licit assi!n"ents.
I& an out1ut varia:le with no location or inde2 assi!ned in the shader te2t has a location s1eci&ied throu!h
the -1enGL A3I, the A3I9assi!ned location will :e used. -therwise, such varia:les will :e assi!ned a
61
4 #aria$%e" and &'pe"
location :, the liner. All such assi!n"ents will have a color inde2 o& =ero. +ee section 1..# G+hader
E2ecutionH o& the -1enGL Gra1hics +,ste" +1eci&ication &or "ore details. A lin9ti"e error will occur i&
an out1ut varia:le is declared in "ulti1le shaders o& the sa"e lan!ua!e with con&lictin! location or inde2
values.
5or the 1ur1oses o& deter"inin! i& a non9&ra!"ent out1ut "atches an in1ut &ro" a su:se<uent shader
sta!e, the location la,out <uali&ier Bi& an,C "ust "atch.
".".2.1 Tessellation Control Outputs
0essellation control shaders allow out1ut la,out <uali&iers onl, on the inter&ace <uali&ier out, not on an
out1ut :loc, :loc "e":er, or varia:le declaration. 0he out1ut la,out <uali&ier identi&iers allowed &or
tessellation control shaders include the verte29count la,out <uali&ier:
la/out-3uali&ier-i*
vertices ; integer-constant
0he identi&ier vertices s1eci&ies the nu":er o& vertices in the out1ut 1atch 1roduced :, the tessellation
control shader, which also s1eci&ies the nu":er o& ti"es the tessellation control shader is invoed. It is a
co"1ile9 or lin9ti"e error &or the out1ut verte2 count to :e less than or e<ual to =ero, or !reater than the
i"1le"entation9de1endent "a2i"u" 1atch si=e.
0he intrinsicall, declared tessellation control out1ut arra, gl%out34 will also :e si=ed :, an, out1ut la,out
declaration. /ence, the e21ression
gl"out6length()
will return the out1ut 1atch verte2 count s1eci&ied in a 1revious out1ut la,out <uali&ier. 5or out1uts
declared without an arra, si=e, includin! intrinsicall, declared out1uts Bi.e., gl%outC, a la,out "ust :e
"ust :e declared :e&ore an, use o& the "ethod lengt"BC or other arra, use re<uires its si=e :e nown.
It is a co"1ile9ti"e error i& the out1ut 1atch verte2 count s1eci&ied in an out1ut la,out <uali&ier does not
"atch the arra, si=e s1eci&ied in an, out1ut varia:le declaration in the sa"e shader.
All tessellation control shader la,out declarations in a 1ro!ra" "ust s1eci&, the sa"e out1ut 1atch verte2
count. 0here "ust :e at least one la,out <uali&ier s1eci&,in! an out1ut 1atch verte2 count in an, 1ro!ra"
containin! tessellation control shadersJ however, such a declaration is not re<uired in all tessellation
control shaders.
".".2.2 Geometr& Outputs
Geo"etr, shaders can have three additional t,1es o& out1ut la,out identi&iers: an out1ut )rimitive t/)e, a
"a2i"u" out1ut verte" count, and 1er9out1ut stream nu":ers. 0he 1ri"itive t,1e and verte2 count
identi&iers are allowed onl, on the inter&ace <uali&ier out, not on an out1ut :loc, :loc "e":er, or
varia:le declaration. 0he strea" identi&ier is allowed on the inter&ace <uali&ier out, on out1ut :locs, and
on varia:le declarations.
0he la,out <uali&ier identi&iers &or !eo"etr, shader out1uts are
la/out-3uali&ier-i*
points
line%strip
62
4 #aria$%e" and &'pe"
triangle%strip
max%vertices ; integer-constant
stream ; integer-constant
0he 1ri"itive t,1e identi&iers points, line%strip, and triangle%strip are used to s1eci&, the t,1e o& out1ut
1ri"itive 1roduced :, the !eo"etr, shader, and onl, one o& these is acce1ted. At least one !eo"etr,
shader Bco"1ilation unitC in a 1ro!ra" "ust declare an out1ut 1ri"itive t,1e, and all !eo"etr, shader
out1ut 1ri"itive t,1e declarations in a 1ro!ra" "ust declare the sa"e 1ri"itive t,1e. It is not re<uired
that all !eo"etr, shaders in a 1ro!ra" declare an out1ut 1ri"itive t,1e.
0he verte2 count identi&ier max%vertices is used to s1eci&, the "a2i"u" nu":er o& vertices the shader
will ever e"it in a sin!le invocation. At least one !eo"etr, shader Bco"1ilation unitC in a 1ro!ra" "ust
declare a "a2i"u" out1ut verte2 count, and all !eo"etr, shader out1ut verte2 count declarations in a
1ro!ra" "ust declare the sa"e count. It is not re<uired that all !eo"etr, shaders in a 1ro!ra" declare a
count.
In this e2a"1le,
la0out(triangle"stri!8 max"vertices 4 B?) out; // order does not matter
la0out(max"vertices 4 B?) out; // redeclaration oka0
la0out(triangle"stri!) out; // redeclaration oka0
la0out(!oints) out; // error8 contradicts triangle"stri!
la0out(max"vertices 4 >?) out; // error8 contradicts B?
all out1uts &ro" the !eo"etr, shader are trian!les and at "ost *) vertices will :e e"itted :, the shader. It
is an error &or the "a2i"u" nu":er o& vertices to :e !reater than gl%KaxDeometryOutputUertices.
0he identi&ier stream is used to s1eci&, that a !eo"etr, shader out1ut varia:le or :loc is associated with
a 1articular verte2 strea" Bnu":ered :e!innin! with =eroC. A de&ault strea" nu":er "a, :e declared at
!lo:al sco1e :, <uali&,in! inter&ace <uali&ier out as in this e2a"1le:
la0out(stream 4 1) out;
0he strea" nu":er s1eci&ied in such a declaration re1laces an, 1revious de&ault and a11lies to all
su:se<uent :loc and varia:le declarations until a new de&ault is esta:lished. 0he initial de&ault strea"
nu":er is =ero.
63
4 #aria$%e" and &'pe"
Each out1ut :loc or non9:loc out1ut varia:le is associated with a verte2 strea". I& the :loc or varia:le
is declared with the strea" identi&ier, it is associated with the s1eci&ied strea"J otherwise, it is associated
with the current de&ault strea". A :loc "e":er "a, :e declared with a strea" identi&ier, :ut the
s1eci&ied strea" "ust "atch the strea" associated with the containin! :loc. -ne e2a"1le:
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<; // belongs to stream =
9;
la0out(stream4?) out; // default is now stream ?
out vec< varE; // varE gets default stream (?)
out :lock= 5 // N:lock=N gets default stream (?)
vec< varB;
9;
la0out(stream4>) out vec< varC; // varC belongs to stream >
Each verte2 e"itted :, the !eo"etr, shader is assi!ned to a s1eci&ic strea", and the attri:utes o& the
e"itted verte2 are taen &ro" the set o& out1ut :locs and varia:les assi!ned to the tar!eted strea". A&ter
each verte2 is e"itted, the values o& all out1ut varia:les :eco"e unde&ined. Additionall,, the out1ut
varia:les associated with each verte2 strea" "a, share stora!e. @ritin! to an out1ut varia:le associated
with one strea" "a, overwrite out1ut varia:les associated with an, other strea". @hen e"ittin! each
verte2, a !eo"etr, shader should write to all out1uts associated with the strea" to which the verte2 will
:e e"itted and to no out1uts associated with an, other strea".
I& a !eo"etr, shader out1ut :loc or varia:le is declared "ore than once, all such declarations "ust
associate the varia:le with the sa"e verte2 strea". I& an, strea" declaration s1eci&ies a non9e2istent
strea" nu":er, the shader will &ail to co"1ile.
Built9in !eo"etr, shader out1uts are alwa,s associated with verte2 strea" =ero.
All !eo"etr, shader out1ut la,out declarations in a 1ro!ra" "ust declare the sa"e la,out and sa"e value
&or max%vertices. I& !eo"etr, shaders are in a 1ro!ra", there "ust :e at least one !eo"etr, out1ut
la,out declaration so"ewhere in that 1ro!ra", :ut not all !eo"etr, shaders Bco"1ilation unitsC are
re<uired to declare it.
".".2.# .ragment Outputs
0he :uilt9in &ra!"ent shader varia:le gl%NragCept" "a, :e redeclared usin! one o& the &ollowin! la,out
<uali&iers.
la/out-3uali&ier-i*
dept"%any
dept"%greater
dept"%less
dept"%unc"anged
64
4 #aria$%e" and &'pe"
5or e2a"1le:
la0out (de!th"greater) out float gl"'rag-e!th;
0he la,out <uali&ier &or gl%NragCept" constrains intentions o& the &inal value o& gl%NragCept" written
:, an, shader invocation. GL i"1le"entations are allowed to 1er&or" o1ti"i=ations assu"in! that the
de1th test &ails Bor 1assesC &or a !iven &ra!"ent i& all values o& gl%NragCept" consistent with the la,out
<uali&ier would &ail Bor 1assC. I& the &inal value o& gl%NragCept" is inconsistent with its la,out <uali&ier,
the result o& the de1th test &or the corres1ondin! &ra!"ent is unde&ined. /owever, no error will :e
!enerated in this case. I& the de1th test 1asses and de1th writes are ena:led, the value written to the de1th
:u&&er is alwa,s the value o& gl%NragCept", whether or not it is consistent with the la,out <uali&ier.
B, de&ault, gl%NragCept" is <uali&ied as *e)t4an/. @hen the la,out <uali&ier &or gl%NragCept" is
*e)t4an/, the shader co"1iler will note an, assi!n"ent to gl%NragCept" "odi&,in! it in an unnown
wa,, and de1th testin! will alwa,s :e 1er&or"ed a&ter the shader has e2ecuted. @hen the la,out <uali&ier
is *e)t4greater, the GL can assu"e that the &inal value o& gl%NragCept" is !reater than or e<ual to the
&ra!"entFs inter1olated de1th value, as !iven :, the ( co"1onent o& glFrag$oor*. @hen the la,out
<uali&ier is *e)t4less, the GL can assu"e that an, "odi&ication o& gl%NragCept" will onl, decrease its
value. @hen the la,out <uali&ier is *e)t4unc4ange*, the shader co"1iler will honor an, "odi&ication to
gl%NragCept", :ut the rest o& the GL can assu"e that gl%NragCept" is not assi!ned a new value.
Redeclarations o& gl%NragCept" are 1er&or"ed as &ollows:
// redeclaration that changes nothing is allowed
out float gl"'rag-e!th;
// assume it ma0 be modified in an0 wa0
la0out (de!th"an0) out float gl"'rag-e!th;
// assume it ma0 be modified such that its value will onl0 increase
la0out (de!th"greater) out float gl"'rag-e!th;
// assume it ma0 be modified such that its value will onl0 decrease
la0out (de!th"less) out float gl"'rag-e!th;
// assume it will not be modified
la0out (de!th"unchanged) out float gl"'rag-e!th;
I& gl%NragCept" is redeclared in an, &ra!"ent shader in a 1ro!ra", it "ust :e redeclared in all &ra!"ent
shaders in that 1ro!ra" that have static assi!n"ents to gl%NragCept". All redeclarations o&
gl%NragCept" in all &ra!"ent shaders in a sin!le 1ro!ra" "ust have the sa"e set o& <uali&iers. @ithin
an, shader, the &irst redeclarations o& gl%NragCept" "ust a11ear :e&ore an, use o& gl%NragCept". 0he
:uilt9in gl%NragCept" is onl, 1redeclared in &ra!"ent shaders, so redeclarin! it in an, other shader
lan!ua!e results in a co"1ile9ti"e error.
".".# 5niform !ariable La&out ;ualifiers
La,out <uali&iers can :e used &or uni&or" varia:les and su:routine uni&or"s. 0he la,out <uali&ier
identi&iers &or uni&or" varia:les and su:routine uni&or"s are:
65
4 #aria$%e" and &'pe"
la/out-3uali&ier-i*
location ; integer-constant
0he location identi&ier can :e used with de&ault9:loc uni&or" varia:les and su:routine uni&or"s. 0he
location s1eci&ies the location :, which the -1enGL A3I can re&erence the uni&or" and u1date 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&ault9:loc uni&or" varia:les in the 1ro!ra" can have the sa"e location,
even i& the, are unused, otherwise a co"1ile9ti"e or lin9ti"e error will :e !enerated. 8o two su:routine
uni&or" varia:les can have the sa"e location in the sa"e shader sta!e, otherwise a co"1ile9ti"e or lin9
ti"e error will :e !enerated. Valid locations &or de&ault9:loc uni&or" varia:le locations are in the ran!e
o& ) to the i"1le"entation9de&ined "a2i"u" nu":er o& uni&or" locations "inus one. Valid locations &or
su:routine uni&or"s are in the ran!e o& ) to the i"1le"entation9de&ined 1er9sta!e "a2i"u" nu":er o&
su:routine uni&or" locations "inus one.
Locations can :e assi!ned to de&ault9:loc uni&or" arra,s and structures. 0he &irst inner9"ost scalar,
vector or "atri2 "e":er or ele"ent taes the s1eci&ied location and the co"1iler assi!ns the ne2t inner9
"ost "e":er or ele"ent the ne2t incre"ental location value. Each su:se<uent inner9"ost "e":er or
ele"ent !ets incre"ental locations &or the entire structure or arra,. 0his rule a11lies to nested structures
and arra,s and !ives each inner9"ost scalar, vector, or "atri2 t,1e a uni<ue location. 5or arra,s without
an e21licit si=e, the si=e is calculated :ased on its static usa!e. @hen the liner !enerates locations &or
uni&or"s without an e21licit location, it assu"es &or all uni&or"s with an e21licit location all their arra,
ele"ents and structure "e":ers are used and the liner will not !enerate a con&lictin! location, even i&
that ele"ent o& "e":er is dee"ed unused.
"."." Subroutine .unction La&out ;ualifiers
La,out <uali&iers can :e used &or su:routine &unctions. 0he la,out <uali&ier identi&iers &or su:routine
&unctions are:
la/out-3uali&ier-i*
index ; integer-constant
Each su:routine with an inde2 <uali&ier in the shader "ust :e !iven a uni<ue inde2, otherwise a co"1ile9
or lin9ti"e error will :e !enerated. 0he indices "ust :e in the ran!e o& ) to the i"1le"entation de&ined
"a2i"u" nu":er o& su:routines "inus one. It is reco""ended, :ut not re<uired, that the shader assi!ns
a ran!e o& ti!htl, 1aced in*e" values startin! &ro" =ero so that the -1enGL su:routine &unction
enu"eration A3I returns a non9e"1t, na"e &or all active indices.
66
4 #aria$%e" and &'pe"
".".* 5niform and Shader Storage /loc2 La&out ;ualifiers
La,out <uali&iers can :e used &or uni&or" and shader stora!e :locs, :ut not &or non9:loc uni&or"
declarations. 0he la,out <uali&ier identi&iers Band s"ared e,wordC &or uni&or" and shader stora!e :locs
are
la/out-3uali&ier-i*
s"ared
packed
std1E+
stdEF+
row%maLor
column%maLor
binding ; integer-constant
8one o& these have an, se"antic a&&ect at all on the usa!e o& the varia:les :ein! declaredJ the, onl,
descri:e how data is laid out in "e"or,. 5or e2a"1le, "atri2 se"antics are alwa,s colu"n9:ased, as
descri:ed in the rest o& this s1eci&ication, no "atter what la,out <uali&iers are :ein! used.
7ni&or" and shader stora!e :loc la,out <uali&iers can :e declared &or !lo:al sco1e, on a sin!le uni&or"
or shader stora!e :loc, or on a sin!le :loc "e":er declaration.
De&ault la,outs are esta:lished Be2ce1t &or bin*ingC at !lo:al sco1e &or uni&or" :locs as
la0out(la/out-3uali&ier-i*-list) uniform;
and &or shader stora!e :locs as
la0out(la/out-3uali&ier-i*-list) buffer;
@hen this is done, the 1revious de&ault <uali&ication is &irst inherited and then overridden as 1er the
override rules listed :elow &or each <uali&ier listed in the declaration. 0he result :eco"es the new de&ault
<uali&ication sco1ed to su:se<uent uni&or" or shader stora!e :loc de&initions.
0he initial state o& co"1ilation is as i& the &ollowin! were declared:
la0out(shared8 column"ma;or) uniform;
la0out(shared8 column"ma;or) buffer;
E21licitl, declarin! this in a shader will return de&aults :ac to their initial state.
7ni&or" and shader stora!e :locs can :e declared with o1tional la,out <uali&iers, and so can their
individual "e":er declarations. +uch :loc la,out <uali&ication is sco1ed onl, to the content o& the
:loc. As with !lo:al la,out declarations, :loc la,out <uali&ication &irst inherits &ro" the current de&ault
<uali&ication and then overrides it. +i"ilarl,, individual "e":er la,out <uali&ication is sco1ed Eust to the
"e":er declaration, and inherits &ro" and overrides the :locFs <uali&ication.
0he s"ared <uali&ier overrides onl, the st*140, st*430, and )ac,e* <uali&iersJ other <uali&iers are
inherited. 0he co"1ilerDliner will ensure that "ulti1le 1ro!ra"s and 1ro!ra""a:le sta!es containin!
this de&inition will share the sa"e "e"or, la,out &or this :loc, as lon! as all arra,s are declared with
e21licit si=es and all "atrices have "atchin! ro2ma>or andDor columnma>or <uali&ications Bwhich "a,
67
4 #aria$%e" and &'pe"
co"e &ro" a declaration outside the :loc de&initionC. 0his allows use o& the sa"e :u&&er to :ac the sa"e
:loc de&inition across di&&erent 1ro!ra"s.
0he )ac,e* <uali&ier overrides onl, st*140, st*430, and s"aredJ other <uali&iers are inherited. @hen
)ac,e* is used, no sharea:le la,out is !uaranteed. 0he co"1iler and liner can o1ti"i=e "e"or, use
:ased on what varia:les activel, !et used and on other criteria. -&&sets "ust :e <ueried, as there is no
other wa, o& !uaranteein! where Band whichC varia:les reside within the :loc. Atte"1ts to share a
1aced uni&or" or shader stora!e :loc across 1ro!ra"s or sta!es will !enerall, &ail. /owever,
i"1le"entations "a, aid a11lication "ana!e"ent o& 1aced :locs :, usin! canonical la,outs &or 1aced
:locs.
0he st*140 and st*430 <uali&iers override onl, the )ac,e*, s"ared, st*140, and st*430 <uali&iersJ other
<uali&iers are inherited. 0he st*430 <uali&ier is su11orted onl, &or shader stora!e :locsJ usin! st*430 on
a uni&or" :loc will result in a co"1ile9ti"e error. 0he la,out is e21licitl, deter"ined :, this, as
descri:ed in section '.*.# G7ni&or" BlocsZ under +tandard 7ni&or" Bloc La,out o& the -1enGL
Gra1hics +,ste" +1eci&ication. /ence, as in s"ared a:ove, the resultin! la,out is sharea:le across
1ro!ra"s.
La,out <uali&iers on "e":er declarations cannot use the s"ared, )ac,e*, st*140, or st*430 <uali&iers.
0hese can onl, :e used at !lo:al sco1e or on a :loc declaration, or a co"1ile9ti"e error results.
0he ro2ma>or <uali&ier overrides onl, the columnma>or <uali&ierJ other <uali&iers are inherited. It onl,
a&&ects the la,out o& "atrices. Ele"ents within a "atri2 row will :e conti!uous in "e"or,.
0he columnma>or <uali&ier overrides onl, the ro2ma>or <uali&ierJ other <uali&iers are inherited. It onl,
a&&ects the la,out o& "atrices. Ele"ents within a "atri2 colu"n will :e conti!uous in "e"or,.
0he bin*ing identi&ier s1eci&ies the uni&or" :u&&er :indin! 1oint corres1ondin! to the uni&or" or shader
stora!e :loc, which will :e used to o:tain the values o& the "e":er varia:les o& the :loc. It is a
co"1ile9ti"e error to s1eci&, the bin*ing identi&ier &or the !lo:al sco1e or &or :loc "e":er declarations.
An, uni&or" or shader stora!e :loc declared without a bin*ing identi&ier is initiall, assi!ned to :loc
:indin! 1oint =ero. A&ter a 1ro!ra" is lined, the :indin! 1oints used &or uni&or" and shader stora!e
:locs declared with or without a bin*ing identi&ier can :e u1dated :, the -1enGL A3I.
I& the bin*ing identi&ier is used with a uni&or" or shader stora!e :loc instanced as an arra,, the &irst
ele"ent o& the arra, taes the s1eci&ied :loc :indin! and each su:se<uent ele"ent taes the ne2t
consecutive uni&or" :loc :indin! 1oint.
I& the :indin! 1oint &or an, uni&or" or shader stora!e :loc instance is less than =ero, or !reater than or
e<ual to the i"1le"entation9de1endent "a2i"u" nu":er o& uni&or" :u&&er :indin!s, a co"1ile9ti"e
error will occur. @hen the bin*ing identi&ier is used with a uni&or" or shader stora!e :loc instanced as
an arra, o& si=e 7, all ele"ents o& the arra, &ro" bin*ing throu!h bin*ing ? 7 @ 1 "ust :e within this
ran!e.
@hen "ulti1le ar!u"ents are listed in a layout declaration, the a&&ect will :e the sa"e as i& the, were
declared one at a ti"e, in order &ro" le&t to ri!ht, each in turn inheritin! &ro" and overridin! the result
&ro" the 1revious <uali&ication.
68
4 #aria$%e" and &'pe"
5or e2a"1le
la0out(row"ma;or8 column"ma;or)
results in the <uali&ication :ein! columnma>or. -ther e2a"1les:
la0out(shared8 row"ma;or) uniform; // default is now shared and row"ma;or
la0out(std1<?) uniform ,ransform 5 // la0out of this block is std1<?
mat< G1; // row"ma;or
la0out(column"ma;or) mat< G=; // column ma;or
mat> %1; // row"ma;or
9;
uniform ,= 5 // la0out of this block is shared
666
9;
la0out(column"ma;or) uniform ,> 5 // shared and column"ma;or
mat< G>; // column"ma;or
la0out(row"ma;or) mat< m<; // row ma;or
mat> %=; // column"ma;or
9;
".".% Opa1ue75niform La&out ;ualifiers
7ni&or" la,out <uali&iers can :e used to :ind o1a<ue uni&or" varia:les to s1eci&ic :u&&ers or units.
0e2ture i"a!e units can :e :ound to sa"1lers, i"a!e units can :e :ound to i"a!es, and ato"ic counters
can :e :ound to :u&&ers.
Details &or s1eci&ic to i"a!e &or"ats and ato"ic counter :indin!s are !iven in the su:sections :elow.
I"a!e and sa"1ler t,1es :oth tae the uni&or" la,out <uali&ier identi&ier &or :indin!:
la/out-3uali&ier-i*
binding ; integer-constant
0he identi&ier bin*ing s1eci&ies which unit will :e :ound. An, uni&or" sa"1ler or i"a!e varia:le
declared without a bin*ing <uali&ier is initiall, :ound to unit =ero. A&ter a 1ro!ra" is lined, the unit
re&erenced :, a sa"1ler or i"a!e uni&or" varia:le declared with or without a :indin! identi&ier can :e
u1dated :, the -1enGL A3I.
I& the bin*ing identi&ier is used with an arra,, the &irst ele"ent o& the arra, taes the s1eci&ied unit and
each su:se<uent ele"ent taes the ne2t consecutive unit.
I& the bin*ing is less than =ero, or !reater than or e<ual to the i"1le"entation9de1endent "a2i"u"
su11orted nu":er o& units, a co"1ile9ti"e error will occur. @hen the bin*ing identi&ier is used with an
arra, o& si=e 7, all ele"ents o& the arra, &ro" bin*ing throu!h bin*ing ? 7 - 1 "ust :e within this ran!e.
A lin9ti"e error will result i& two co"1ilation units in a 1ro!ra" s1eci&, di&&erent integer-constant
:indin!s &or the sa"e o1a<ue9uni&or" na"e. /owever, it is not an error to s1eci&, a :indin! on so"e :ut
not all declarations &or the sa"e na"e, as shown in the e2a"1les :elow.
69
4 #aria$%e" and &'pe"
// in one com!ilation unit666
la0out(binding4>) uniform sam!ler=- s; // s bound to unit >
// in another com!ilation unit666
uniform sam!ler=- s; // oka08 s still bound at >
// in another com!ilation unit666
la0out(binding4<) uniform sam!ler=- s; // &))+)2 contradictor0 bindings
".".%.1 0tomic Counter La&out ;ualifiers
0he ato"ic counter <uali&iers are
la/out-3uali&ier-i*
binding ; integer-constant
offset ; integer-constant
5or e2a"1le,
la0out (binding 4 =8 offset 4 <) uniform atomic"uint a;
will esta:lish that the o1a<ue handle to the ato"ic counter a will :e :ound to ato"ic counter :u&&er
:indin! 1oint # at an o&&set o& ( :asic "achine units into that :u&&er. 0he de&ault o&&set &or :indin! 1oint #
will :e 1ost incre"ented :, ( Bthe si=e o& an ato"ic counterC.
A su:se<uent ato"ic counter declaration will inherit the 1revious B1ost incre"entedC o&&set. 5or e2a"1le,
a su:se<uent declaration o&
la0out (binding 4 =) uniform atomic"uint bar;
will esta:lish that the ato"ic counter bar has a :indin! to :u&&er :indin! 1oint # at an o&&set o& 4 :asic
"achine units into that :u&&er. 0he o&&set &or :indin! 1oint # will a!ain :e 1ost9incre"ented :, ( Bthe si=e
o& an ato"ic counterC.
@hen "ulti1le varia:les are listed in a la,out declaration, the e&&ect will :e the sa"e as i& the, were
declared one at a ti"e, in order &ro" le&t to ri!ht.
Bindin! 1oints are not inherited, onl, o&&sets. Each :indin! 1oint tracs its own current de&ault o&&set &or
inheritance o& su:se<uent varia:les usin! the sa"e bin*ing. 0he initial state o& co"1ilation is that all
bin*ing 1oints have an o&&set o& ). 0he o&&set can :e set 1er :indin! 1oint at !lo:al sco1e Bwithout
declarin! a varia:leC. 5or e2a"1le,
la0out (binding 4 =8 offset 4 <) uniform atomic"uint;
Esta:lishes that the ne2t atomic%uint declaration &or :indin! 1oint # will inherit o&&set ( B:ut does not
esta:lish a de&ault bin*ingC:
la0out (binding 4 =) uniform atomic"uint bar; // offset is <
la0out (offset 4 F) uniform atomic"uint bar; // error8 no default binding
Ato"ic counters "a, share the sa"e :indin! 1oint, :ut i& a :indin! is shared, their o&&sets "ust :e either
e21licitl, or i"1licitl, B&ro" inheritanceC uni<ue and non overla11in!.
7
4 #aria$%e" and &'pe"
E2a"1le valid uni&or" declarations, assu"in! to1 o& shader:
la0out (binding4>8 offset4<) uniform atomic"uint a; // offset 4 <
la0out (binding4=) uniform atomic"uint b; // offset 4 ?
la0out (binding4>) uniform atomic"uint c; // offset 4 F
la0out (binding4=) uniform atomic"uint d; // offset 4 <
E2a"1le o& an invalid uni&or" declaration:
la0out (offset4<) O // error8 must include binding
la0out (binding418 offset4?) O a; // oka0
la0out (binding4=8 offset4?) O b; // oka0
la0out (binding418 offset4?) O c; // error8 offsets must not be shared
// between a and c
la0out (binding418 offset4=) O d; // error8 overla!s offset ? of a
It is a co"1ile9ti"e error to :ind an ato"ic counter with a :indin! value !reater than or e<ual to
gl!a"#tomic$ounter%in*ings.
".".%.2 .ormat La&out ;ualifiers
5or"at la,out <uali&iers can :e used on i"a!e varia:le declarations Bthose declared with a :asic t,1e
havin! GimageH in its e,wordC. 0he &or"at la,out <uali&ier identi&iers &or i"a!e varia:le declarations
are
la/out-3uali&ier-i*
&loat-image-&ormat-3uali&ier
int-image-&ormat-3uali&ier
uint-image-&ormat-3uali&ier
binding = integer-constant
&loat-image-&ormat-3uali&ier
rgbaFGf
rgba1Zf
rgFGf
rg1Zf
r11f%g11f%b1+f
rFGf
r1Zf
rgba1Z
rgb1+%aG
rgba\
rg1Z
rg\
r1Z
r\
rgba1Z%snorm
rgba\%snorm
rg1Z%snorm
rg\%snorm
71
4 #aria$%e" and &'pe"
r1Z%snorm
r\%snorm
int-image-&ormat-3uali&ier
rgbaFGi
rgba1Zi
rgba\i
rgFGi
rg1Zi
rg\i
rFGi
r1Zi
r\i
uint-image-&ormat-3uali&ier
rgbaFGui
rgba1Zui
rgb1+%aGui
rgba\ui
rgFGui
rg1Zui
rg\ui
rFGui
r1Zui
r\ui
A &or"at la,out <uali&ier s1eci&ies the i"a!e &or"at associated with a declared i"a!e varia:le. -nl, one
&or"at <uali&ier "a, :e s1eci&ied &or an, i"a!e varia:le declaration. 5or i"a!e varia:les with &loatin!9
1oint co"1onent t,1es Be,words startin! with GimageHC, si!ned inte!er co"1onent t,1es Be,words
startin! with GiimageHC, or unsi!ned inte!er co"1onent t,1es Be,words startin! with GuimageHC, the
&or"at <uali&ier used "ust "atch the &loat-image-&ormat-3uali&ier, int-image-&ormat-3uali&ier, or uint-
image-&ormat-3uali&ier !ra""ar rules, res1ectivel,. It is a co"1ile9ti"e error to declare an i"a!e
varia:le where the &or"at <uali&ier does not "atch the i"a!e varia:le t,1e.
An, i"a!e varia:le used &or i"a!e loads or ato"ic o1erations "ust s1eci&, a &or"at la,out <uali&ierJ it is
a co"1ile9ti"e error to 1ass an i"a!e uni&or" varia:le or &unction 1ara"eter declared without a &or"at
la,out <uali&ier to an i"a!e load or ato"ic &unction.
0he bin*ing identi&ier was descri:ed in section (.(.. G7ni&or" and +hader +tora!e Bloc La,out
>uali&iersH.
7ni&or"s not <uali&ied with writeonly "ust have a &or"at la,out <uali&ier. 8ote that an i"a!e varia:le
1assed to a &unction &or read access cannot :e declared as writeonly and hence "ust have :een declared
with a &or"at la,out <uali&ier.
72
4 #aria$%e" and &'pe"
".* Interpolation ;ualifiers
In1uts and out1uts that could :e inter1olated can :e &urther <uali&ied :, at "ost one o& the &ollowin!
inter1olation <uali&iers:
;ualifier 8eaning
smoot" 1ers1ective correct inter1olation
flat no inter1olation
noperspective linear inter1olation
0he 1resence o& and t,1e o& inter1olation is controlled :, the a:ove inter1olation <uali&iers as well as the
au2iliar, stora!e <uali&iers centroid and sample. 0he au2iliar, stora!e <uali&ier patc" is not used &or
inter1olationJ it is a co"1ile9ti"e error to use inter1olation <uali&iers with patc".
A varia:le <uali&ied as flat will not :e inter1olated. Instead, it will have the sa"e value &or ever,
&ra!"ent within a trian!le. 0his value will co"e &ro" a sin!le 1rovoin! verte2, as descri:ed :, the
-1enGL Gra1hics +,ste" +1eci&ication. A varia:le "a, :e <uali&ied as flat can also :e <uali&ied as
centroid or sample, which will "ean the sa"e thin! as <uali&,in! it onl, as flat.
A varia:le <uali&ied as smoot" will :e inter1olated in a 1ers1ective9correct "anner over the 1ri"itive
:ein! rendered. Inter1olation in a 1ers1ective correct "anner is s1eci&ied in e<uation 1(.' in the -1enGL
Gra1hics +,ste" +1eci&ication, section 1(.. GLine +e!"entsH.
A varia:le <uali&ied as noperspective "ust :e inter1olated linearl, in screen s1ace, as descri:ed in
e<uation %.' in the -1enGL Gra1hics +,ste" +1eci&ication, section %.. GLine +e!"entsH.
@hen "ulti9sa"1le rasteri=ation is disa:led, or &or &ra!"ent shader in1ut varia:les <uali&ied with neither
centroid nor sample, the value o& the assi!ned varia:le "a, :e inter1olated an,where within the 1i2el
and a sin!le value "a, :e assi!ned to each sa"1le within the 1i2el, to the e2tent 1er"itted :, the
-1enGL Gra1hics +,ste" +1eci&ication.
@hen "ultisa"1le rasteri=ation is ena:led, centroid and sample "a, :e used to control the location and
&re<uenc, o& the sa"1lin! o& the <uali&ied &ra!"ent shader in1ut. I& a &ra!"ent shader in1ut is <uali&ied
with centroid, a sin!le value "a, :e assi!ned to that varia:le &or all sa"1les in the 1i2el, :ut that value
"ust :e inter1olated to a location that lies in :oth the 1i2el and in the 1ri"itive :ein! rendered, includin!
an, o& the 1i2elFs sa"1les covered :, the 1ri"itive. Because the location at which the varia:le is
inter1olated "a, :e di&&erent in nei!h:orin! 1i2els, and derivatives "a, :e co"1uted :, co"1utin!
di&&erences :etween nei!h:orin! 1i2els, derivatives o& centroid9sa"1led in1uts "a, :e less accurate than
those &or non9centroid inter1olated varia:les. I& a &ra!"ent shader in1ut is <uali&ied with sample, a
se1arate value "ust :e assi!ned to that varia:le &or each covered sa"1le in the 1i2el, and that value "ust
:e sa"1led at the location o& the individual sa"1le.
0he t,1e and 1resence o& inter1olation <uali&iers o& varia:les with the sa"e na"e declared in all lined
shaders &or the sa"e cross9sta!e inter&ace "ust "atch, otherwise the lin co""and will &ail.
@hen co"1arin! an out1ut &ro" one sta!e to an in1ut o& a su:se<uent sta!e, the in1ut and out1ut donFt
"atch i& their inter1olation <uali&iers Bor lac thereo&C are not the sa"e.
73
4 #aria$%e" and &'pe"
".*.1 Redeclaring /uilt7in Interpolation !ariables in the Compatibilit& -rofile
0he &ollowin! 1redeclared varia:les can :e redeclared with an inter1olation <uali&ier when usin! the
co"1ati:ilit, 1ro&ile:
Verte2, tessellation control, tessellation evaluation, and !eo"etr, lan!ua!es:
gl"'rontMolor
gl":ackMolor
gl"'ront*econdar0Molor
gl":ack*econdar0Molor
5ra!"ent lan!ua!e:
gl"Molor
gl"*econdar0Molor
5or e2a"1le,
in vec< gl"Molor; // !redeclared b0 the fragment language
flat in vec< gl"Molor; // redeclared b0 user to be flat
flat in vec< gl"'rontMolor; // in!ut to geometr0 shader8 no gl"inIJ
flat out vec< gl"'rontMolor; // out!ut from geometr0 shader
Ideall,, these are redeclared as 1art o& the redeclaration o& an inter&ace :loc, as descri:ed in section '.1.1
G$o"1ati:ilit, 3ro&ile Built9In Lan!ua!e Varia:lesH. /owever, &or the a:ove 1ur1ose, the, can :e
redeclared as individual varia:les at !lo:al sco1e, outside an inter&ace :loc. A co"1ile9ti"e error will
result i& a shader has :oth an inter&ace :loc redeclaration and a se1arate redeclaration o& a "e":er o&
that inter&ace :loc outside the inter&ace :loc redeclaration.
I& gl$olor is redeclared with an inter1olation <uali&ier, then glFront$olor and gl%ac,$olor Bi& the,
are written toC "ust also :e redeclared with the sa"e inter1olation <uali&ier, and vice versa. I&
gl'econ*ar/$olor is redeclared with an inter1olation <uali&ier, then glFront'econ*ar/$olor and
gl%ac,'econ*ar/$olor Bi& the, are written toC "ust also :e redeclared with the sa"e inter1olation
<uali&ier, and vice versa. 0his <uali&ier "atchin! on 1redeclared varia:les is onl, re<uired &or varia:les
that are staticall, used within the shaders in a 1ro!ra".
74
4 #aria$%e" and &'pe"
".% -arameter ;ualifiers
In addition to 1recision <uali&iers and "e"or, <uali&iers, 1ara"eters can have these 1ara"eter <uali&iers.
;ualifier 8eaning
T none: de&ault U sa"e is in
const &or &unction 1ara"eters that cannot :e written to
in &or &unction 1ara"eters 1assed into a &unction
out &or &unction 1ara"eters 1assed :ac out o& a &unction, :ut not initiali=ed
&or use when 1assed in
inout &or &unction 1ara"eters 1assed :oth into and out o& a &unction
3ara"eter <uali&iers are discussed in "ore detail in section *.1.1 G5unction $allin! $onventionsH.
". -recision and -recision ;ualifiers
3recision <uali&iers are added &or code 1orta:ilit, with -1enGL E+, not &or &unctionalit,. 0he, have the
sa"e s,nta2 as in -1enGL E+, as descri:ed :elow, :ut the, have no se"antic "eanin!, which includes no
e&&ect on the 1recision used to store or o1erate on varia:les.
I& an e2tension adds in the sa"e se"antics and &unctionalit, in the -1enGL E+ #.) s1eci&ication &or
1recision <uali&iers, then the e2tension is allowed to reuse the e,words :elow &or that 1ur1ose.
5or the 1ur1oses o& deter"inin! i& an out1ut &ro" one shader sta!e "atches an in1ut o& the ne2t sta!e, the
1recision <uali&ier need not "atch.
"..1 Range and -recision
0he 1recision o& stored sin!le9 and dou:le91recision &loatin!91oint varia:les is de&ined :, the IEEE '.(
standard &or %#9:it and *(9:it &loatin!91oint nu":ers. 0his includes su11ort &or 8a8s B8ot a 8u":erC
and In&s B1ositive or ne!ative in&initiesC.
0he &ollowin! rules a11l, to :oth sin!le and dou:le91recision o1erations: In&inities and =eros are
!enerated as dictated :, IEEE, :ut su:Eect to the 1recisions allowed in the &ollowin! ta:le and su:Eect to
allowin! 1ositive and ne!ative =eros to :e interchan!ed. /owever, dividin! a non9=ero :, ) results in the
a11ro1riatel, si!ned IEEE In&: I& :oth 1ositive and ne!ative =eros are i"1le"ented, the correctl, si!ned
In& will :e !enerated, otherwise 1ositive In& is !enerated. An, denor"ali=ed value in1ut into a shader or
1otentiall, !enerated :, an, o1eration in a shader can :e &lushed to ). 0he roundin! "ode cannot :e set
and is unde&ined. 8a8s are not re<uired to :e !enerated. +u11ort &or si!nalin! 8a8s is not re<uired and
e2ce1tions are never raised. -1erations and :uilt9in &unctions that o1erate on a 8a8 are not re<uired to
return a 8a8 as the result.
3recisions are e21ressed in ter"s o& "a2i"u" relative error in units o& 7L3 Bunits in the last 1laceC,
unless otherwise noted.
75
4 #aria$%e" and &'pe"
5or sin!le 1recision o1erations, 1recisions are re<uired as &ollows:
Operation 'recision
a I b, a ^ b, a R b $orrectl, rounded.
T, TV, VV, U, UV $orrect result.
a D b, 1.) D b #.. 7L3 &or b in the ran!e L#
91#*
, #
1#*
M.
a R b I c $orrectl, rounded sin!le o1eration or se<uence o&
two correctl, rounded o1erations.
fmaBC Inherited &ro" a R b I c.
powB", /C Inherited &ro" expG B" A logG B/CC.
exp B"C, expG B"C B% I # R Y"YC 7L3.
log BC, logG BC % 7L3 outside the ran!e L).., #.)M.
A:solute error T #
9#1
inside the ran!e L).., #.)M.
sMrt BC Inherited &ro" 1.) D inversesMrtBC.
inversesMrt BC # 7L3.
i"1licit and e21licit
conversions :etween t,1es
$orrectl, rounded.
Built9in &unctions de&ined in the s1eci&ication with an e<uation :uilt &ro" the a:ove o1erations inherit the
a:ove errors. 0hese include, &or e2a"1le, the !eo"etric &unctions, the co""on &unctions, and "an, o&
the "atri2 &unctions. Built9in &unctions not listed a:ove and not de&ined as e<uations o& the a:ove have
unde&ined 1recision. 0hese include, &or e2a"1le, the tri!ono"etric &unctions and deter"inant.
0he 1recision o& dou:le91recision o1erations is at least that o& sin!le 1recision.
"..2 -recision ;ualifiers
An, sin!le91recision &loatin!91oint declaration or an, inte!er declaration can have the t,1e 1receded :,
one o& these 1recision <uali&iers:
;ualifier 8eaning
"ig"p 8one.
mediump 8one.
lowp 8one.
76
4 #aria$%e" and &'pe"
5or e2a"1le:
low! float color;
out medium! vec= H;
low! ivec= foo(low! mat>);
high! mat< m;
Literal constants do not have 1recision <uali&iers. 8either do Boolean varia:les. 8either do &loatin!91oint
constructors nor inte!er constructors when none o& the constructor ar!u"ents have 1recision <uali&iers.
3recision <uali&iers, as with other <uali&iers, do not e&&ect the :asic t,1e o& the varia:le. In 1articular,
there are no constructors &or 1recision conversionsJ constructors onl, convert t,1es. +i"ilarl,, 1recision
<uali&iers, as with other <uali&iers, do not contri:ute to &unction overloadin! :ased on 1ara"eter t,1es. As
discussed in the ne2t cha1ter, &unction in1ut and out1ut is done throu!h co1ies, and there&ore <uali&iers do
not have to "atch.
"..# +efault -recision ;ualifiers
0he 1recision state"ent
!recision !recision3qualifier t0!e;
can :e used to esta:lish a de&ault 1recision <uali&ier. 0he type &ield can :e either int or float, and the
)recision-3uali&ier can :e lowp, mediump, or "ig"p. An, other t,1es or <uali&iers will result in a
co"1ile9ti"e error. I& t/)e is float, the directive a11lies to non91recision9<uali&ied sin!le91recision
&loatin!91oint t,1e Bscalar, vector, and "atri2C declarations. I& t/)e is int, the directive a11lies to all non9
1recision9<uali&ied inte!er t,1e Bscalar, vector, si!ned, and unsi!nedC declarations. 0his includes !lo:al
varia:le declarations, &unction return declarations, &unction 1ara"eter declarations, and local varia:le
declarations.
8on91recision <uali&ied declarations will use the 1recision <uali&ier s1eci&ied in the "ost recent precision
state"ent that is still in sco1e. 0he precision state"ent has the sa"e sco1in! rules as varia:le
declarations. I& it is declared inside a co"1ound state"ent, its e&&ect sto1s at the end o& the inner"ost
state"ent it was declared in. 3recision state"ents in nested sco1es override 1recision state"ents in outer
sco1es. ;ulti1le 1recision state"ents &or the sa"e :asic t,1e can a11ear inside the sa"e sco1e, with later
state"ents overridin! earlier state"ents within that sco1e.
0he verte2, tessellation, and !eo"etr, lan!ua!es have the &ollowin! 1redeclared !lo:all, sco1ed de&ault
1recision state"ents:
!recision high! float;
!recision high! int;
0he &ra!"ent lan!ua!e has the &ollowin! 1redeclared !lo:all, sco1ed de&ault 1recision state"ents:
!recision medium! int;
!recision high! float;
77
4 #aria$%e" and &'pe"
".." 0vailable -recision ;ualifiers
0he :uilt9in "acro GL?5RAG;E80?3RE$I+I-8?/IG/ is de&ined to 1:
define .#"')D.G&%,"H)&M$*$+%"P$.P 1
0his "acro is availa:le in the verte2, tessellation, !eo"etr,, and &ra!"ent lan!ua!es.
".4 !ariance and the Invariant ;ualifier
In this section, variance re&ers to the 1ossi:ilit, o& !ettin! di&&erent values &ro" the sa"e e21ression in
di&&erent 1ro!ra"s. 5or e2a"1le, sa, two verte2 shaders, in di&&erent 1ro!ra"s, each set glBosition with
the sa"e e21ression in :oth shaders, and the in1ut values into that e21ression are the sa"e when :oth
shaders run. It is 1ossi:le, due to inde1endent co"1ilation o& the two shaders, that the values assi!ned to
glBosition are not e2actl, the sa"e when the two shaders run. In this e2a"1le, this can cause 1ro:le"s
with ali!n"ent o& !eo"etr, in a "ulti91ass al!orith".
In !eneral, such variance :etween shaders is allowed. @hen such variance does not e2ist &or a 1articular
out1ut varia:le, that varia:le is said to :e invariant.
".4.1 The Invariant ;ualifier
0o ensure that a 1articular out1ut varia:le is invariant, it is necessar, to use the invariant <uali&ier. It can
either :e used to <uali&, a 1reviousl, declared varia:le as :ein! invariant
invariant gl"Hosition; // make existing gl"Hosition be invariant
out vec> Molor;
invariant Molor; // make existing Molor be invariant
or as 1art o& a declaration when a varia:le is declared
invariant centroid out vec> Molor;
-nl, varia:les out1ut &ro" a shader Bincludin! those that are then in1ut to a su:se<uent shaderC can :e
candidates &or invariance. 0his includes user9de&ined out1ut varia:les and the :uilt9in out1ut varia:les.
As onl, out1uts need :e declared with invariant, an out1ut &ro" one shader sta!e will still "atch an in1ut
o& a su:se<uent sta!e without the in1ut :ein! declared as invariant.
In1ut or out1ut instance na"es on :locs are not used when redeclarin! :uilt9in varia:les.
0he invariant e,word can :e &ollowed :, a co""a se1arated list o& 1reviousl, declared identi&iers. All
uses o& invariant "ust :e at the !lo:al sco1e, and :e&ore an, use o& the varia:les :ein! declared as
invariant.
0o !uarantee invariance o& a 1articular out1ut varia:le across two 1ro!ra"s, the &ollowin! "ust also :e
true:
0he out1ut varia:le is declared as invariant in :oth 1ro!ra"s.
0he sa"e values "ust :e in1ut to all shader in1ut varia:les consu"ed :, e21ressions and &low control
contri:utin! to the value assi!ned to the out1ut varia:le.
78
4 #aria$%e" and &'pe"
0he te2ture &or"ats, te2el values, and te2ture &ilterin! are set the sa"e wa, &or an, te2ture &unction
calls contri:utin! to the value o& the out1ut varia:le.
All in1ut values are all o1erated on in the sa"e wa,. All o1erations in the consu"in! e21ressions and
an, inter"ediate e21ressions "ust :e the sa"e, with the sa"e order o& o1erands and sa"e
associativit,, to !ive the sa"e order o& evaluation. Inter"ediate varia:les and &unctions "ust :e
declared as the sa"e t,1e with the sa"e e21licit or i"1licit 1recision <uali&iers. An, control &low
a&&ectin! the out1ut value "ust :e the sa"e, and an, e21ressions consu"ed to deter"ine this control
&low "ust also &ollow these invariance rules.
All the data &low and control &low leadin! to settin! the invariant out1ut varia:le reside in a sin!le
co"1ilation unit.
Essentiall,, all the data &low and control &low leadin! to an invariant out1ut "ust "atch.
Initiall,, :, de&ault, all out1ut varia:les are allowed to :e variant. 0o &orce all out1ut varia:les to :e
invariant, use the 1ra!"a
!ragma *,-.# invariant(all)
:e&ore all declarations in a shader. I& this 1ra!"a is used a&ter the declaration o& an, varia:les or
&unctions, then the set o& out1uts that :ehave as invariant is unde&ined. It is a co"1ile9ti"e error to use
this 1ra!"a in a &ra!"ent shader.
Generall,, invariance is ensured at the cost o& &le2i:ilit, in o1ti"i=ation, so 1er&or"ance can :e de!raded
:, use o& invariance. /ence, use o& this 1ra!"a is intended as a de:u! aid, to avoid individuall, declarin!
all out1ut varia:les as invariant.
".4.2 Invariance of Constant (,pressions
Invariance "ust :e !uaranteed &or constant e21ressions. A 1articular constant e21ression "ust evaluate to
the sa"e result i& it a11ears a!ain in the sa"e shader or a di&&erent shader. 0his includes the sa"e
e21ression a11earin! two shaders o& the sa"e lan!ua!e or shaders o& two di&&erent lan!ua!es.
$onstant e21ressions "ust evaluate to the sa"e result when o1erated on as alread, descri:ed a:ove &or
invariant varia:les, whether or not the invariant <uali&ier is used.
".9 The -recise ;ualifier
+o"e al!orith"s re<uire &loatin!91oint co"1utations to e2actl, &ollow the order o& o1erations s1eci&ied in
the source code and to treat all o1erations consistentl,, even i& the i"1le"entation su11orts o1ti"i=ations
that could 1roduce nearl, e<uivalent results with hi!her 1er&or"ance. 5or e2a"1le, "an, GL
i"1le"entations su11ort a Z"ulti1l,9addZ instruction that can co"1ute a &loatin!91oint e21ression such as
result 4 (a L b) K (c L d);
in two o1erations instead o& three o1erationsJ one "ulti1l, and one "ulti1l,9add instead o& two "ulti1lies
and one add. 0he result o& a &loatin!91oint "ulti1l,9add "i!ht not alwa,s :e identical to &irst doin! a
"ulti1l, ,ieldin! a &loatin!91oint result and then doin! a &loatin!91oint add. /ence, in this e2a"1le, the
two "ulti1l, o1erations would not :e treated consistentl,J the two "ulti1lies could e&&ectivel, a11ear to
have di&&erin! 1recisions.
79
4 #aria$%e" and &'pe"
0he e, co"1utation that needs to :e "ade consistent a11ears when tessellatin!, where inter"ediate
1oints &or su:division are s,nthesi=ed in di&&erent directions, ,et need to ,ield the sa"e result, as shown in
the dia!ra" :elow.
@ithout an, <uali&iers, i"1le"entations are 1er"itted to 1er&or" such o1ti"i=ations that e&&ectivel,
"odi&, the order or nu":er o& o1erations used to evaluate an e21ression, even i& those o1ti"i=ations "a,
1roduce sli!htl, di&&erent results relative to uno1ti"i=ed code.
0he <uali&ier precise will ensure that o1erations contri:utin! to a varia:leFs value are done in their stated
order and are done with o1erator consistenc,. -rder is deter"ined :, o1erator 1recedence and
1arenthesis, as descri:ed in section ..1 G-1eratorsH. -1erator consistenc, "eans &or each 1articular
o1erator, &or e2a"1le the "ulti1l, o1erator B / C, its o1eration is alwa,s co"1uted with the sa"e
1recision. +1eci&icall,, values co"1uted :, co"1iler9!enerated code "ust adhere to the &ollowin!
identities:
1. a I : V : I a
#. a R : V : R a
%. a R : I c R d V : R a I cR d V d R c I : R a V Tan, other "athe"aticall, valid co":inationU
@hile the &ollowin! are 1revented:
(. a I B: I cC is not allowed to :eco"e Ba I :C I c
.. a R B: R cC is not allowed to :eco"e a R B: R cC
*. a R : I c is not allowed to :eco"e a sin!le o1eration fmaBa, :, cC
@here a, b, c, and *, are scalars or vectors, not "atrices. B;atri2 "ulti1lication !enerall, does not
co""ute.C It is the shader writerFs res1onsi:ilit, to e21ress the co"1utation in ter"s o& these rules and
the co"1ilerFs res1onsi:ilit, to &ollow these rules. +ee the descri1tion o& gl5ess$oor* &or the rules the
tessellation sta!es are res1onsi:le &or &ollowin!, which in conEunction with the a:ove allow avoidin!
cracin! when su:dividin!.
8
-11osin! directions
o& ed!e walin!
&or su:division

+u:division 1oints
need to land on the
sa"e location to
1revent cracin!
$orner 1oints
start with sa"e values
$orner 1oints
start with sa"e values
4 #aria$%e" and &'pe"
5or e2a"1le,
!recise out vec< !osition;
declares that o1erations used to 1roduce the value o& )osition "ust :e 1er&or"ed in e2actl, the order
s1eci&ied in the source code and with all o1erators :ein! treated consistentl,. As with the invariant
<uali&ier Bsection (.4.1 G0he Invariant >uali&ierHC, the 1recise <uali&ier "a, :e used to <uali&, a :uilt9in or
1reviousl, declared user9de&ined varia:le as :ein! 1recise:
out vec> Molor;
!recise Molor; // make existing Molor be !recise
0his <uali&ier will a&&ect the evaluation o& an r9value in a 1articular &unction i& and onl, i& the result is
eventuall, consu"ed in the sa"e &unction :, an l9value <uali&ied as precise. An, other e21ressions
within a &unction are not a&&ected, includin! return values and out1ut 1ara"eters not declared as precise
:ut that are eventuall, consu"ed outside the &unction :, an varia:le <uali&ied as precise.
81
4 #aria$%e" and &'pe"
+o"e e2a"1les o& the use o& precise:
in vec< a8 b8 c8 d;
!recise out vec< v;
float func(float e8 float f8 float g8 float h)
5
return (eLf) K (gLh); // no constraint on order or
// o!erator consistenc0
9
float func=(float e8 float f8 float g8 float h)
5
!recise float result 4 (eLf) K (gLh); // ensures same !recision for
// the two multi!lies
return result;
9
float func>(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
5or the 1ur1oses o& deter"inin! i& an out1ut &ro" one shader sta!e "atches an in1ut o& the ne2t sta!e, the
precise <uali&ier need not "atch :etween the in1ut and the out1ut.
All constant e21ressions are evaluated as i& precise was 1resent, whether or not it is 1resent. /owever, as
descri:ed in section (.%.% G$onstant E21ressionsH, there is no re<uire"ent that a co"1ile9ti"e constant
e21ression evaluates to the sa"e value as a corres1ondin! non9constant e21ression.
".1$ 8emor& ;ualifiers
Varia:les declared as i"a!e t,1es Bthe :asic o1a<ue t,1es with GimageH in their e,wordC can :e &urther
<uali&ied with a "e"or, <uali&ier.
82
4 #aria$%e" and &'pe"
Varia:les declared as i"a!e t,1es can <uali&ied with one or "ore o& the &ollowin! "e"or, <uali&iers:
;ualifier 8eaning
co"erent "e"or, varia:le where reads and writes are coherent with reads and
writes &ro" other shader invocations
volatile "e"or, varia:le whose underl,in! value "a, :e chan!ed at an, 1oint
durin! shader e2ecution :, so"e source other than the current shader
invocation
restrict "e"or, varia:le where use o& that varia:le is the onl, wa, to read
and write the underl,in! "e"or, in the relevant shader sta!e
readonly "e"or, varia:le that can :e used to read the underl,in! "e"or,, :ut
cannot :e used to write the underl,in! "e"or,
writeonly "e"or, varia:le that can :e used to write the underl,in! "e"or,, :ut
cannot :e used to read the underl,in! "e"or,
;e"or, accesses to i"a!e varia:les declared usin! the co"erent <uali&ier are 1er&or"ed coherentl, with
si"ilar accesses &ro" other shader invocations. In 1articular, when readin! a varia:le declared as
co"erent, the values returned will re&lect the results o& 1reviousl, co"1leted writes 1er&or"ed :, other
shader invocations. @hen writin! a varia:le declared as co"erent, the values written will :e re&lected in
su:se<uent coherent reads 1er&or"ed :, other shader invocations. As descri:ed in section '.11 G+hader
;e"or, AccessH o& the -1enGL +1eci&ication, shader "e"or, reads and writes co"1lete in a lar!el,
unde&ined order. 0he :uilt9in &unction memoryJarrierBC can :e used i& needed to !uarantee the
co"1letion and relative orderin! o& "e"or, accesses 1er&or"ed :, a sin!le shader invocation.
@hen accessin! "e"or, usin! varia:les not declared as co"erent, the "e"or, accessed :, a shader "a,
:e cached :, the i"1le"entation to service &uture accesses to the sa"e address. ;e"or, stores "a, :e
cached in such a wa, that the values written "i!ht not :e visi:le to other shader invocations accessin! the
sa"e "e"or,. 0he i"1le"entation "a, cache the values &etched :, "e"or, reads and return the sa"e
values to an, shader invocation accessin! the sa"e "e"or,, even i& the underl,in! "e"or, has :een
"odi&ied since the &irst "e"or, read. @hile varia:les not declared as co"erent "i!ht not :e use&ul &or
co""unicatin! :etween shader invocations, usin! non9coherent accesses "a, result in hi!her
1er&or"ance.
;e"or, accesses to i"a!e varia:les declared usin! the volatile <uali&ier "ust treat the underl,in!
"e"or, as thou!h it could :e read or written at an, 1oint durin! shader e2ecution :, so"e source other
than the e2ecutin! shader invocation. @hen a volatile varia:le is read, its value "ust :e re9&etched &ro"
the underl,in! "e"or,, even i& the shader invocation 1er&or"in! the read had 1reviousl, &etched its value
&ro" the sa"e "e"or,. @hen a volatile varia:le is written, its value "ust :e written to the underl,in!
"e"or,, even i& the co"1iler can conclusivel, deter"ine that its value will :e overwritten :, a
su:se<uent write. +ince the e2ternal source readin! or writin! a volatile varia:le "a, :e another shader
invocation, varia:les declared as volatile are auto"aticall, treated as coherent.
;e"or, accesses to i"a!e varia:les declared usin! the restrict <uali&ier "a, :e co"1iled assu"in! that
the varia:le used to 1er&or" the "e"or, access is the onl, wa, to access the underl,in! "e"or, usin!
the shader sta!e in <uestion. 0his allows the co"1iler to coalesce or reorder loads and stores usin!
restrict9<uali&ied i"a!e varia:les in wa,s that wouldnFt :e 1er"itted &or i"a!e varia:les not so <uali&ied,
83
4 #aria$%e" and &'pe"
:ecause the co"1iler can assu"e that the underl,in! i"a!e wonFt :e read or written :, other code.
A11lications are res1onsi:le &or ensurin! that i"a!e "e"or, re&erenced :, varia:les <uali&ied with
restrict will not :e re&erenced usin! other varia:les in the sa"e sco1eJ otherwise, accesses to restrict9
<uali&ied varia:les will have unde&ined results.
;e"or, accesses to i"a!e varia:les declared usin! the readonly <uali&ier "a, onl, read the underl,in!
"e"or,, which is treated as read9onl, "e"or, and cannot :e written to. It is a co"1ile9ti"e error to 1ass
an i"a!e varia:le <uali&ied with readonly to imageStoreBC or other :uilt9in &unctions that "odi&, i"a!e
"e"or,.
;e"or, accesses to i"a!e varia:les declared usin! the writeonly <uali&ier "a, onl, write the underl,in!
"e"or,J the underl,in! "e"or, cannot :e read. It is a co"1ile9ti"e error to 1ass an i"a!e varia:le
<uali&ied with writeonly to image$oadBC or other :uilt9in &unctions that read i"a!e "e"or,. It is a
co"1ile9ti"e error to <uali&, a varia:le as :oth readonly and writeonly. A varia:le could :e <uali&ied as
:oth readonly and writeonly, disallowin! :oth read and write, :ut still :e 1assed to imageSizeBC to have
the si=e <ueried.
0he "e"or, <uali&iers co"erent, volatile, restrict, readonly, and writeonly "a, :e used in the
declaration o& :u&&er varia:les Bi.e., "e":ers o& shader stora!e :locsC. @hen a :u&&er varia:le is
declared with a "e"or, <uali&ier, the :ehavior s1eci&ied &or "e"or, accesses involvin! i"a!e varia:les
descri:ed a:ove a11lies identicall, to "e"or, accesses involvin! that :u&&er varia:le. It is a co"1ile9
ti"e error to assi!n to a :u&&er varia:le <uali&ied with readonly or to read &ro" a :u&&er varia:le <uali&ied
with writeonly.
Additionall,, "e"or, <uali&iers "a, also :e used in the declaration o& shader stora!e :locs. @hen a
:loc declaration is <uali&ied with a "e"or, <uali&ier, it is as i& all o& its "e":ers were declared with the
sa"e "e"or, <uali&ier. 5or e2a"1le, the :loc declaration
coherent buffer :lock 5
readonl0 vec< member1;
vec< member=;
9;
is e<uivalent to
buffer :lock 5
coherent readonl0 vec< member1;
coherent vec< member=;
9;
;e"or, <uali&iers are onl, su11orted in the declarations o& i"a!e varia:les, :u&&er varia:les, and shader
stora!e :locsJ it is an error to use such <uali&iers in an, other declarations.
0he values o& i"a!e varia:les <uali&ied with co"erent, volatile, restrict, readonly, or writeonly "a, not
:e 1assed to &unctions whose &or"al 1ara"eters lac such <uali&iers. B+ee section *.1 G5unction
De&initionsH &or "ore detail on &unction callin!.C It is le!al to have additional <uali&iers on a &or"al
1ara"eter, :ut not to have &ewer.
84
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 <uali&iers cannot :e used on &or"al &unction 1ara"eters, :ut the, are not included in 1ara"eter
"atchin!.
8ote that the use o& const in an i"a!e varia:le declaration is <uali&,in! the const9ness o& varia:le :ein!
declared, not the i"a!e it re&ers to: 0he <uali&ier readonly <uali&ies the i"a!e "e"or, Bas accessed
throu!h that varia:leC while const <uali&iers the varia:le itsel&.
".11 Order of ;ualification
@hen "ulti1le <uali&iers are 1resent in a declaration, the, "a, a11ear in an, order, :ut the, "ust all
a11ear :e&ore the t,1e. 0he layout <uali&ier is the onl, <uali&ier that can a11ear "ore than once. 5urther,
a declaration can have at "ost one stora!e <uali&ier, at "ost one au2iliar, stora!e <uali&ier, and at "ost
one inter1olation <uali&ier. ;ulti1le "e"or, <uali&iers can :e used. An, violation o& these rules will
cause a co"1ile9ti"e error.
85
* Operators and (,pressions
*.1 Operators
0he -1enGL +hadin! Lan!ua!e has the &ollowin! o1erators.
-recedence Operator Class Operators 0ssociativit&
1 Bhi!hestC 1arenthetical !rou1in! ! 8A
#
arra, su:scri1t
&unction call and constructor structure
&ield or "ethod selector, swi==le
1ost &i2 incre"ent and decre"ent
3 4
!
.
-- ((
Le&t to Ri!ht
%
1re&i2 incre"ent and decre"ent
unar,
-- ((
- ( : <
Ri!ht to Le&t
( "ulti1licative / . 0 Le&t to Ri!ht
. additive - ( Le&t to Ri!ht
* :it9wise shi&t 11 22 Le&t to Ri!ht
' relational 1 2 1; 2; Le&t to Ri!ht
4 e<ualit, ;; <; Le&t to Ri!ht
6 :it9wise and 9 Le&t to Ri!ht
1) :it9wise e2clusive or 7 Le&t to Ri!ht
11 :it9wise inclusive or 8 Le&t to Ri!ht
1# lo!ical and 99 Le&t to Ri!ht
1% lo!ical e2clusive or 77 Le&t to Ri!ht
1( lo!ical inclusive or 8 8 Le&t to Ri!ht
1. selection ? = Ri!ht to Le&t
1*
Assi!n"ent
arith"etic assi!n"ents
;
-; (;
/; .;
0; 11; 22;
9; 7; 8;
Ri!ht to Le&t
1' BlowestC se<uence * Le&t to Ri!ht
0here is no address9o& o1erator nor a dere&erence o1erator. 0here is no t,1ecast o1eratorJ constructors
are used instead.
86
5 Operator" and ()pre""ion"
*.2 0rra& Operations
0hese are now descri:ed in section ..' G+tructure and Arra, -1erationsH.
*.# .unction Calls
I& a &unction returns a value, then a call to that &unction "a, :e used as an e21ression, whose t,1e will :e
the t,1e that was used to declare or de&ine the &unction.
5unction de&initions and callin! conventions are discussed in section *.1 G5unction De&initionsH .
*." Constructors
$onstructors use the &unction call s,nta2, where the &unction na"e is a t,1e, and the call "aes an o:Eect
o& that t,1e. $onstructors are used the sa"e wa, in :oth initiali=ers and e21ressions. B+ee section 6
G+hadin! Lan!ua!e Gra""arH &or details.C 0he 1ara"eters are used to initiali=e the constructed value.
$onstructors can :e used to re<uest a data t,1e conversion to chan!e &ro" one scalar t,1e to another
scalar t,1e, or to :uild lar!er t,1es out o& s"aller t,1es, or to reduce a lar!er t,1e to a s"aller t,1e.
In !eneral, constructors are not :uilt9in &unctions with 1redeter"ined 1rotot,1es. 5or arra,s and
structures, there "ust :e e2actl, one ar!u"ent in the constructor &or each ele"ent or "e":er. 5or the
other t,1es, the ar!u"ents "ust 1rovide a su&&icient nu":er o& co"1onents to 1er&or" the initiali=ation,
and it is a co"1ile9ti"e error to include so "an, ar!u"ents that the, cannot all :e used. Detailed rules
&ollow. 0he 1rotot,1es actuall, listed :elow are "erel, a su:set o& e2a"1les.
*.".1 Conversion and Scalar Constructors
$onvertin! :etween scalar t,1es is done as the &ollowin! 1rotot,1es indicate:
int(uint) // converts an unsigned integer to a signed integer
int(bool) // converts a :oolean value to an int
int(float) // converts a float value to an int
int(double) // converts a double value to a signed integer
uint(int) // converts a signed integer value to an unsigned integer
uint(bool) // converts a :oolean value to an unsigned integer
uint(float) // converts a float value to an unsigned integer
uint(double) // converts a double value to an unsigned integer
bool(int) // converts a signed integer value to a :oolean
bool(uint) // converts an unsigned integer value to a :oolean value
bool(float) // converts a float value to a :oolean
bool(double) // converts a double value to a :oolean
float(int) // converts a signed integer value to a float
float(uint) // converts an unsigned integer value to a float value
float(bool) // converts a :oolean value to a float
float(double)// converts a double value to a float
double(int) // converts a signed integer value to a double
double(uint) // converts an unsigned integer value to a double
double(bool) // converts a :oolean value to a double
double(float)// converts a float value to a double
87
5 Operator" and ()pre""ion"
@hen constructors are used to convert an, &loatin!91oint t,1e to an inte!er t,1e, the &ractional 1art o& the
&loatin!91oint value is dro11ed. It is unde&ined to convert a ne!ative &loatin!91oint value to an uint.
@hen a constructor is used to convert an, inte!er or &loatin!91oint t,1e to a bool, ) and ).) are converted
to false, and non9=ero values are converted to true. @hen a constructor is used to convert a bool to an,
inte!er or &loatin!91oint t,1e, false is converted to ) or ).), and true is converted to 1 or 1.).
0he constructor intBuintC 1reserves the :it 1attern in the ar!u"ent, which will chan!e the ar!u"entFs
value i& its si!n :it is set. 0he constructor uintBintC 1reserves the :it 1attern in the ar!u"ent, which will
chan!e its value i& it is ne!ative.
Identit, constructors, lie floatBfloatC are also le!al, :ut o& little use.
+calar constructors with non9scalar 1ara"eters can :e used to tae the &irst ele"ent &ro" a non9scalar.
5or e2a"1le, the constructor floatBvecFC will select the &irst co"1onent o& the vecF 1ara"eter.
*.".2 !ector and 8atri, Constructors
$onstructors can :e used to create vectors or "atrices &ro" a set o& scalars, vectors, or "atrices. 0his
includes the a:ilit, to shorten vectors.
I& there is a sin!le scalar 1ara"eter to a vector constructor, it is used to initiali=e all co"1onents o& the
constructed vector to that scalarOs value. I& there is a sin!le scalar 1ara"eter to a "atri2 constructor, it is
used to initiali=e all the co"1onents on the "atri2Os dia!onal, with the re"ainin! co"1onents initiali=ed
to ).).
I& a vector is constructed &ro" "ulti1le scalars, one or "ore vectors, or one or "ore "atrices, or a "i2ture
o& these, the vectorFs co"1onents will :e constructed in order &ro" the co"1onents o& the ar!u"ents. 0he
ar!u"ents will :e consu"ed le&t to ri!ht, and each ar!u"ent will have all its co"1onents consu"ed, in
order, :e&ore an, co"1onents &ro" the ne2t ar!u"ent are consu"ed. +i"ilarl, &or constructin! a "atri2
&ro" "ulti1le scalars or vectors, or a "i2ture o& these. ;atri2 co"1onents will :e constructed and
consu"ed in colu"n "aEor order. In these cases, there "ust :e enou!h co"1onents 1rovided in the
ar!u"ents to 1rovide an initiali=er &or ever, co"1onent in the constructed value. It is a co"1ile9ti"e
error to 1rovide e2tra ar!u"ents :e,ond this last used ar!u"ent.
I& a "atri2 is constructed &ro" a "atri2, then each co"1onent Bcolu"n i. row >C in the result that has a
corres1ondin! co"1onent Bcolu"n i. row >C in the ar!u"ent will :e initiali=ed &ro" there. All other
co"1onents will :e initiali=ed to the identit, "atri2. I& a "atri2 ar!u"ent is !iven to a "atri2 constructor,
it is a co"1ile9ti"e error to have an, other ar!u"ents.
I& the :asic t,1e Bbool* int* float, or doubleC o& a 1ara"eter to a constructor does not "atch the :asic t,1e
o& the o:Eect :ein! constructed, the scalar construction rules Ba:oveC are used to convert the 1ara"eters.
88
5 Operator" and ()pre""ion"
+o"e use&ul vector constructors are as &ollows:
vec>(float) // initiali/es each com!onent of the vec> with the float
vec<(ivec<) // makes a vec< with com!onent3wise conversion
vec<(mat=) // the vec< is column ? followed b0 column 1
vec=(float8 float) // initiali/es a vec= with = floats
ivec>(int8 int8 int) // initiali/es an ivec> with > ints
bvec<(int8 int8 float8 float) // uses < :oolean conversions
vec=(vec>) // dro!s the third com!onent of a vec>
vec>(vec<) // dro!s the fourth com!onent of a vec<
vec>(vec=8 float) // vec>6x 4 vec=6x8 vec>60 4 vec=608 vec>6/ 4 float
vec>(float8 vec=) // vec>6x 4 float8 vec>60 4 vec=6x8 vec>6/ 4 vec=60
vec<(vec>8 float)
vec<(float8 vec>)
vec<(vec=8 vec=)
+o"e e2a"1les o& these are:
vec< color 4 vec<(?6?8 16?8 ?6?8 16?);
vec< rgba 4 vec<(16?); // sets each com!onent to 16?
vec> rgb 4 vec>(color); // dro! the <th com!onent
0o initiali=e the dia!onal o& a "atri2 with all other ele"ents set to =ero:
mat=(float)
mat>(float)
mat<(float)
0hat is, result<i=<>= is set to the &loat ar!u"ent &or all i C > and set to ) &or all i >.
89
5 Operator" and ()pre""ion"
0o initiali=e a "atri2 :, s1eci&,in! vectors or scalars, the co"1onents are assi!ned to the "atri2 ele"ents
in colu"n9"aEor order.
mat=(vec=8 vec=); // one column !er argument
mat>(vec>8 vec>8 vec>); // one column !er argument
mat<(vec<8 vec<8 vec<8 vec<); // one column !er argument
mat>x=(vec=8 vec=8 vec=); // one column !er argument
dmat=(dvec=8 dvec=);
dmat>(dvec>8 dvec>8 dvec>);
dmat<(dvec<8 dvec<8 dvec<8 dvec<);
mat=(float8 float8 // first column
float8 float); // second column
mat>(float8 float8 float8 // first column
float8 float8 float8 // second column
float8 float8 float); // third column
mat<(float8 float8 float8 float8 // first column
float8 float8 float8 float8 // second column
float8 float8 float8 float8 // third column
float8 float8 float8 float); // fourth column
mat=x>(vec=8 float8 // first column
vec=8 float); // second column
dmat=x<(dvec>8 double8 // first column
double8 dvec>) // second column
A wide ran!e o& other 1ossi:ilities e2ist, to construct a "atri2 &ro" vectors and scalars, as lon! as enou!h
co"1onents are 1resent to initiali=e the "atri2. 0o construct a "atri2 &ro" a "atri2:
mat>x>(mat<x<); // takes the u!!er3left >x> of the mat<x<
mat=x>(mat<x=); // takes the u!!er3left =x= of the mat<x<8 last row is ?8?
mat<x<(mat>x>); // !uts the mat>x> in the u!!er3left8 sets the lower right
// com!onent to 18 and the rest to ?
*.".# Structure Constructors
-nce a structure is de&ined, and its t,1e is !iven a na"e, a constructor is availa:le with the sa"e na"e to
construct instances o& that structure. 5or e2a"1le:
struct light 5
float intensit0;
vec> !osition;
9;
light light(ar 4 light(>6?8 vec>(16?8 =6?8 >6?));
9
5 Operator" and ()pre""ion"
0he ar!u"ents to the constructor will :e used to set the structureFs "e":ers, in order, usin! one ar!u"ent
1er "e":er. Each ar!u"ent "ust :e the sa"e t,1e as the "e":er it sets, or :e a t,1e that can :e
converted to the "e":erFs t,1e accordin! to section (.1.1) GI"1licit $onversions.H
+tructure constructors can :e used as initiali=ers or in e21ressions.
*."." 0rra& Constructors
Arra, t,1es can also :e used as constructor na"es, which can then :e used in e21ressions or initiali=ers.
5or e2a"1le,
const float cI>J 4 floatI>J(E6?8 C6=8 161);
const float dI>J 4 floatIJ(E6?8 C6=8 161);
float g;
666
float aIEJ 4 floatIEJ(g8 18 g8 =6>8 g);
float bI>J;
b 4 floatI>J(g8 g K 16?8 g K =6?);
0here "ust :e e2actl, the sa"e nu":er o& ar!u"ents as the si=e o& the arra, :ein! constructed. I& no si=e
is 1resent in the constructor, then the arra, is e21licitl, si=ed to the nu":er o& ar!u"ents 1rovided. 0he
ar!u"ents are assi!ned in order, startin! at ele"ent ), to the ele"ents o& the constructed arra,. Each
ar!u"ent "ust :e the sa"e t,1e as the ele"ent t,1e o& the arra,, or :e a t,1e that can :e converted to the
ele"ent t,1e o& the arra, accordin! to section (.1.1) GI"1licit $onversions.H
Arra,s o& arra,s are si"ilarl, constructed, :ut onl, the outer9"ost di"ension is o1tional:
vec< bI=J 4 666;
vec<I>JI=J(b8 b8 b); // constructor
vec<IJI=J(b8 b8 b); // constructor8 valid8 si/e deduced
vec<I>JIJ(b8 b8 b); // com!ile3time error8 invalid t0!e constructed
*.* !ector and Scalar Components and Length
0he na"es o& the co"1onents o& a vector or scalar are denoted :, a sin!le letter. As a notational
convenience, several letters are associated with each co"1onent :ased on co""on usa!e o& 1osition,
color or te2ture coordinate vectors. 0he individual co"1onents can :e selected :, &ollowin! the varia:le
na"e with 1eriod B . C and then the co"1onent na"e.
0he co"1onent na"es su11orted are:
D". /. (. 2E 7se&ul when accessin! vectors that re1resent 1oints or nor"als
Dr. g. b. aE 7se&ul when accessin! vectors that re1resent colors
Ds. t. ). 3E 7se&ul when accessin! vectors that re1resent te2ture coordinates
91
5 Operator" and ()pre""ion"
0he co"1onent na"es ". r. and s are, &or e2a"1le, s,non,"s &or the sa"e B&irstC co"1onent in a vector.
0he, are also the na"es o& the onl, co"1onent in a scalar.
8ote that the third co"1onent o& the te2ture coordinate set, r in -1enGL, has :een rena"ed ) so as to
avoid the con&usion with r B&or redC in a color.
Accessin! co"1onents :e,ond those declared &or the t,1e is a co"1ile9ti"e error so, &or e2a"1le:
vec= !os;
float height;
!os6x // is legal
!os6/ // is illegal
height6x // is legal
height60 // is illegal
0he co"1onent selection s,nta2 allows "ulti1le co"1onents to :e selected :, a11endin! their na"es
B&ro" the sa"e na"e setC a&ter the 1eriod B . C.
vec< v<;
v<6rgba; // is a vec< and the same as ;ust using v<8
v<6rgb; // is a vec>8
v<6b; // is a float8
v<6x0; // is a vec=8
v<6xgba; // is illegal 3 the com!onent names do not come from
// the same set6
0he order o& the co"1onents can :e di&&erent to swi==le the", or re1licated:
vec< !os 4 vec<(16?8 =6?8 >6?8 <6?);
vec< swi/4 !os6w/0x; // swi/ 4 (<6?8 >6?8 =6?8 16?)
vec< du! 4 !os6xx00; // du! 4 (16?8 16?8 =6?8 =6?)
float f 4 16=;
vec< du! 4 f6xxxx; // du! 4 (16=8 16=8 16=8 16=)
0his notation is "ore concise than the constructor s,nta2. 0o &or" an r9value, it can :e a11lied to an,
e21ression that results in a vector or scalar r9value. An, resultin! vector o& an, o1eration "ust :e a valid
vector in the lan!ua!eJ hence the &ollowin! results in a co"1ile9ti"e error:
vec< f;
vec< g 4 !os6x0/wx06x0/w; // illegal; !os6x0/wx0 is non3existent vecB
0he co"1onent !rou1 notation can occur on the le&t hand side o& an e21ression.
vec< !os 4 vec<(16?8 =6?8 >6?8 <6?);
!os6xw 4 vec=(E6?8 B6?); // !os 4 (E6?8 =6?8 >6?8 B6?)
!os6wx 4 vec=(C6?8 F6?); // !os 4 (F6?8 =6?8 >6?8 C6?)
!os6xx 4 vec=(>6?8 <6?); // illegal 3 'x' used twice
!os6x0 4 vec>(16?8 =6?8 >6?); // illegal 3 mismatch between vec= and vec>
0o &or" an l9value, swi==lin! "ust :e a11lied to an l9value o& vector or scalar t,1e, contain no du1licate
co"1onents, and it results in an l9value o& scalar or vector t,1e, de1endin! on nu":er o& co"1onents
s1eci&ied.
92
5 Operator" and ()pre""ion"
Arra, su:scri1tin! s,nta2 can also :e a11lied to vectors B:ut not to scalarsC to 1rovide nu"eric inde2in!.
+o in
vec< !os;
)os<2= re&ers to the third ele"ent o& 1os and is e<uivalent to )os.(. 0his allows varia:le inde2in! into a
vector, as well as a !eneric wa, o& accessin! co"1onents. An, inte!er e21ression can :e used as the
su:scri1t. 0he &irst co"1onent is at inde2 =ero. Readin! &ro" or writin! to a vector usin! a constant
inte!ral e21ression with a value that is ne!ative or !reater than or e<ual to the si=e o& the vector results in
a co"1ile9ti"e error. @hen inde2in! with non9constant e21ressions, :ehavior is unde&ined i& the inde2 is
ne!ative, or !reater than or e<ual to the si=e o& the vector.
0he lengt" "ethod "a, :e a11lied to vectors B:ut not scalarsC. 0he result is the nu":er o& co"1onents in
the vector. 5or e2a"1le,
vec> v;
const int # 4 v6length();
sets the constant L to %. 0he t,1e returned :, .lengt"BC on a vector is int, and the value returned is a
constant e21ression.
*.% 8atri, Components
0he co"1onents o& a "atri2 can :e accessed usin! arra, su:scri1tin! s,nta2. A11l,in! a sin!le su:scri1t
to a "atri2 treats the "atri2 as an arra, o& colu"n vectors, and selects a sin!le colu"n, whose t,1e is a
vector o& the sa"e si=e as the "atri2. 0he le&t"ost colu"n is colu"n ). A second su:scri1t would then
o1erate on the resultin! vector, as de&ined earlier &or vectors. /ence, two su:scri1ts select a colu"n and
then a row.
mat< m;
mI1J 4 vec<(=6?); // sets the second column to all =6?
mI?JI?J 4 16?; // sets the u!!er left element to 16?
mI=JI>J 4 =6?; // sets the <th element of the third column to =6?
Behavior is unde&ined when accessin! a co"1onent outside the :ounds o& a "atri2 with a non9constant
e21ression. It is a co"1ile9ti"e error to access a "atri2 with a constant e21ression that is outside the
:ounds o& the "atri2.
0he lengt" "ethod "a, :e a11lied to "atrices. 0he result is the nu":er o& colu"ns o& the "atri2. 5or
e2a"1le,
mat>x< v;
const int # 4 v6length();
sets the constant L to %. 0he t,1e returned :, .lengt"BC on a "atri2 is int, and the value returned is a
constant e21ression.
*. Structure and 0rra& Operations
0he "e":ers o& a structure and the lengt" "ethod o& an arra, are selected usin! the 1eriod B . C.
93
5 Operator" and ()pre""ion"
In total, onl, the &ollowin! o1erators are allowed to o1erate on arra,s and structures as whole entities:
&ield selector .
e<ualit, ;; <;
assi!n"ent ;
inde2in! Barra,s onl,C L M
0he e<ualit, o1erators and assi!n"ent o1erator are onl, allowed i& the two o1erands are sa"e si=e and
t,1e. +tructure t,1es "ust :e o& the sa"e declared structure. Both arra, o1erands "ust :e e21licitl,
si=ed. @hen usin! the e<ualit, o1erators, two structures are e<ual i& and onl, i& all the "e":ers are
co"1onent9wise e<ual, and two arra,s are e<ual i& and onl, i& all the ele"ents are ele"ent9wise e<ual.
Arra, ele"ents are accessed usin! the arra, su:scri1t o1erator B 3 4 C. An e2a"1le o& accessin! an arra,
ele"ent is
diffuseMolor K4 light$ntensit0I>J L %dot#;
Arra, indices start at =ero. Arra, ele"ents are accessed usin! an e21ression whose t,1e is int or uint.
Behavior is unde&ined i& a shader su:scri1ts an arra, with an inde2 less than ) or !reater than or e<ual to
the si=e the arra, was declared with.
Arra,s can also :e accessed with the "ethod o1erator B . C and the lengt" "ethod to <uer, the si=e o& the
arra,:
light$ntensit06length() // return the si/e of the arra0
*.4 0ssignments
Assi!n"ents o& values to varia:le na"es are done with the assi!n"ent o1erator B ; C:
lvalue3ex!ression 4 rvalue3ex!ression
0he lvalue-e")ression evaluates to an l9value. 0he assi!n"ent o1erator stores the value o& rvalue-
e")ression into the l9value and returns an r9value with the t,1e and 1recision o& lvalue-e")ression. 0he
lvalue-e")ression and rvalue-e")ression "ust have the sa"e t,1e, or the e21ression "ust have a t,1e in
the ta:le in section (.1.1) GI"1licit $onversionsH that converts to the t,1e o& lvalue-e")ression, in which
case an i"1licit conversion will :e done on the rvalue-e")ression :e&ore the assi!n"ent is done. An,
other desired t,1e9conversions "ust :e s1eci&ied e21licitl, via a constructor. L9values "ust :e writa:le.
Varia:les that are :uilt9in t,1es, entire structures or arra,s, structure "e":ers, l9values with the &ield
selector B . C a11lied to select co"1onents or swi==les without re1eated &ields, l9values within 1arentheses,
and l9values dere&erenced with the arra, su:scri1t o1erator B 3 4 C are all l9values. -ther :inar, or unar,
e21ressions, &unction na"es, swi==les with re1eated &ields, and constants cannot :e l9values. 0he ternar,
o1erator B?=C is also not allowed as an l9value. 7sin! an incorrect e21ression as an l9value results in a
co"1ile9ti"e error.
E21ressions on the le&t o& an assi!n"ent are evaluated :e&ore e21ressions on the ri!ht o& the assi!n"ent.
94
5 Operator" and ()pre""ion"
0he other assi!n"ent o1erators are
add into B-;C
su:tract &ro" B(;C
"ulti1l, into B/;C
divide into B.;C
"odulus into B0;C
le&t shi&t :, B11;C
ri!ht shi&t :, B22;C
and into B9;C
inclusive9or into B8;C
e2clusive9or into B7;C
where the !eneral e21ression
lvalue op4 ex!ression
is e<uivalent to
lvalue 4 lvalue op ex!ression
where o) is as descri:ed :elow, and the l9value and e21ression "ust satis&, the se"antic re<uire"ents o&
:oth o) and e<uals B;C.
Readin! a varia:le :e&ore writin! Bor initiali=in!C it is le!al, however the value is unde&ined.
*.9 (,pressions
E21ressions in the shadin! lan!ua!e are :uilt &ro" the &ollowin!:
$onstants o& t,1e bool* all inte!er t,1es, all &loatin!91oint t,1es, all vector t,1es, and all "atri2 t,1es.
$onstructors o& all t,1es.
Varia:le na"es o& all t,1es.
An arra,, vector, or "atri2 e21ression with the lengt" "ethod a11lied.
+u:scri1ted arra, na"es.
5unction calls that return values.
$o"1onent &ield selectors and arra, su:scri1t results.
3arenthesi=ed e21ression. An, e21ression can :e 1arenthesi=ed. 3arentheses can :e used to !rou1
o1erations. -1erations within 1arentheses are done :e&ore o1erations across 1arentheses.
95
5 Operator" and ()pre""ion"
0he arith"etic :inar, o1erators add B-C, su:tract B(C, "ulti1l, B/C, and divide B.C o1erate on inte!er and
&loatin!91oint scalars, vectors, and "atrices. I& the &unda"ental t,1es in the o1erands do not "atch,
then the conversions &ro" section (.1.1) GI"1licit $onversionsH are a11lied to create "atchin! t,1es.
All arith"etic :inar, o1erators result in the sa"e &unda"ental t,1e Bsi!ned inte!er, unsi!ned inte!er,
sin!le91recision &loatin! 1oint, or dou:le91recision &loatin! 1ointC as the o1erands the, o1erate on,
a&ter o1erand t,1e conversion. A&ter conversion, the &ollowin! cases are valid
0he two o1erands are scalars. In this case the o1eration is a11lied, resultin! in a scalar.
-ne o1erand is a scalar, and the other is a vector or "atri2. In this case, the scalar o1eration is
a11lied inde1endentl, to each co"1onent o& the vector or "atri2, resultin! in the sa"e si=e vector
or "atri2.
0he two o1erands are vectors o& the sa"e si=e. In this case, the o1eration is done co"1onent9wise
resultin! in the sa"e si=e vector.
0he o1erator is add B-C, su:tract B(C, or divide B.C, and the o1erands are "atrices with the sa"e
nu":er o& rows and the sa"e nu":er o& colu"ns. In this case, the o1eration is done co"1onent9
wise resultin! in the sa"e si=e "atri2.
0he o1erator is "ulti1l, B/C, where :oth o1erands are "atrices or one o1erand is a vector and the
other a "atri2. A ri!ht vector o1erand is treated as a colu"n vector and a le&t vector o1erand as a
row vector. In all these cases, it is re<uired that the nu":er o& colu"ns o& the le&t o1erand is e<ual
to the nu":er o& rows o& the ri!ht o1erand. 0hen, the "ulti1l, B/C o1eration does a linear
al!e:raic "ulti1l,, ,ieldin! an o:Eect that has the sa"e nu":er o& rows as the le&t o1erand and the
sa"e nu":er o& colu"ns as the ri!ht o1erand. +ection ..1) GVector and ;atri2 -1erationsH
e21lains in "ore detail how vectors and "atrices are o1erated on.
All other cases result in a co"1ile9ti"e error.
Dividin! :, =ero does not cause an e2ce1tion :ut does result in an uns1eci&ied value. 7se the :uilt9in
&unctions dot, cross, matrixHompKult, and outer'roduct, to !et, res1ectivel,, vector dot 1roduct,
vector cross 1roduct, "atri2 co"1onent9wise "ulti1lication, and the "atri2 1roduct o& a colu"n
vector ti"es a row vector.
0he o1erator "odulus B0C o1erates on si!ned or unsi!ned inte!er scalars or inte!er vectors. I& the
&unda"ental t,1es in the o1erands do not "atch, then the conversions &ro" section (.1.1) GI"1licit
$onversionsH are a11lied to create "atchin! t,1es. 0he o1erands cannot :e vectors o& di&&erin! si=eJ
this is a co"1ile ti"e error. I& one o1erand is a scalar and the other vector, then the scalar is a11lied
co"1onent9wise to the vector, resultin! in the sa"e t,1e as the vector. I& :oth are vectors o& the sa"e
si=e, the result is co"1uted co"1onent9wise. 0he resultin! value is unde&ined &or an, co"1onent
co"1uted with a second o1erand that is =ero, while results &or other co"1onents with non9=ero second
o1erands re"ain de&ined. I& :oth o1erands are non9ne!ative, then the re"ainder is non9ne!ative.
Results are unde&ined i& one or :oth o1erands are ne!ative. 0he o1erator "odulus B0C is not de&ined
&or an, other data t,1es Bnon9inte!er t,1esC.
0he arith"etic unar, o1erators ne!ate B9C, 1ost9 and 1re9incre"ent and decre"ent B(( and --C o1erate
on inte!er or &loatin!91oint values Bincludin! vectors and "atricesC. All unar, o1erators wor
co"1onent9wise on their o1erands. 0hese result with the sa"e t,1e the, o1erated on. 5or 1ost9 and
1re9incre"ent and decre"ent, the e21ression "ust :e one that could :e assi!ned to Ban l9valueC. 3re9
incre"ent and 1re9decre"ent add or su:tract 1 or 1.) to the contents o& the e21ression the, o1erate on,
96
5 Operator" and ()pre""ion"
and the value o& the 1re9incre"ent or 1re9decre"ent e21ression is the resultin! value o& that
"odi&ication. 3ost9incre"ent and 1ost9decre"ent e21ressions add or su:tract 1 or 1.) to the contents
o& the e21ression the, o1erate on, :ut the resultin! e21ression has the e21ressionOs value :e&ore the
1ost9incre"ent or 1ost9decre"ent was e2ecuted.
0he relational o1erators !reater than B2C, less than B1C, !reater than or e<ual B2;C, and less than or
e<ual B1;C o1erate onl, on scalar inte!er and scalar &loatin!91oint e21ressions. 0he result is scalar
Boolean. Either the o1erandsO t,1es "ust "atch, or the conversions &ro" section (.1.1) GI"1licit
$onversionsH will :e a11lied to o:tain "atchin! t,1es. 0o do co"1onent9wise relational co"1arisons
on vectors, use the :uilt9in &unctions lessB"an* lessB"an&Mual* greaterB"an* and
greaterB"an&Mual.
0he e<ualit, o1erators eMual B;;C, and not e<ual B<;C o1erate on all t,1es. 0he, result in a scalar
Boolean. I& the o1erand t,1es do not "atch, then there "ust :e a conversion &ro" section (.1.1)
GI"1licit $onversionsH a11lied to one o1erand that can "ae the" "atch, in which case this
conversion is done. 5or vectors, "atrices, structures, and arra,s, all co"1onents, "e":ers, or
ele"ents o& one o1erand "ust e<ual the corres1ondin! co"1onents, "e":ers, or ele"ents in the other
o1erand &or the o1erands to :e considered e<ual. 0o !et a vector o& co"1onent9wise e<ualit, results
&or vectors, use the :uilt9in &unctions eMual and not&Mual.
0he lo!ical :inar, o1erators and B99C, or B 8 8 C, and e2clusive or B77C o1erate onl, on two Boolean
e21ressions and result in a Boolean e21ression. And B99C will onl, evaluate the ri!ht hand o1erand
i& the le&t hand o1erand evaluated to true. -r B 8 8 C will onl, evaluate the ri!ht hand o1erand i& the le&t
hand o1erand evaluated to false. E2clusive or B77C will alwa,s evaluate :oth o1erands.
0he lo!ical unar, o1erator not B<C. It o1erates onl, on a Boolean e21ression and results in a Boolean
e21ression. 0o o1erate on a vector, use the :uilt9in &unction not.
0he se<uence B * C o1erator that o1erates on e21ressions :, returnin! the t,1e and value o& the ri!ht9
"ost e21ression in a co""a se1arated list o& e21ressions. All e21ressions are evaluated, in order,
&ro" le&t to ri!ht.
0he ternar, selection o1erator B?=C. It o1erates on three e21ressions Be")1 ? e")2 = e")3C. 0his
o1erator evaluates the &irst e21ression, which "ust result in a scalar Boolean. I& the result is true, it
selects to evaluate the second e21ression, otherwise it selects to evaluate the third e21ression. -nl,
one o& the second and third e21ressions is evaluated. 0he second and third e21ressions can :e an,
t,1e, as lon! their t,1es "atch, or there is a conversion in section (.1.1) GI"1licit $onversionsH that
can :e a11lied to one o& the e21ressions to "ae their t,1es "atch. 0his resultin! "atchin! t,1e is the
t,1e o& the entire e21ression.
0he oneFs co"1le"ent o1erator B:C. 0he o1erand "ust :e o& t,1e si!ned or unsi!ned inte!er or inte!er
vector, and the result is the oneFs co"1le"ent o& its o1erandJ each :it o& each co"1onent is
co"1le"ented, includin! an, si!n :its.
0he shi&t o1erators B11C and B22C. 5or :oth o1erators, the o1erands "ust :e si!ned or unsi!ned
inte!ers or inte!er vectors. -ne o1erand can :e si!ned while the other is unsi!ned. In all cases, the
resultin! t,1e will :e the sa"e t,1e as the le&t o1erand. I& the &irst o1erand is a scalar, the second
o1erand has to :e a scalar as well. I& the &irst o1erand is a vector, the second o1erand "ust :e a scalar
or a vector, and the result is co"1uted co"1onent9wise. 0he result is unde&ined i& the ri!ht o1erand is
ne!ative, or !reater than or e<ual to the nu":er o& :its in the le&t e21ressionFs :ase t,1e. 0he value o&
E1 TT E# is E1 Binter1reted as a :it 1atternC le&t9shi&ted :, E# :its. 0he value o& E1 UU E# is E1 ri!ht9
97
5 Operator" and ()pre""ion"
shi&ted :, E# :it 1ositions. I& E1 is a si!ned inte!er, the ri!ht9shi&t will e2tend the si!n :it. I& E1 is an
unsi!ned inte!er, the ri!ht9shi&t will =ero9e2tend.
0he :itwise o1erators and B9C, e2clusive9or B7C, and inclusive9or B8C. 0he o1erands "ust :e o& t,1e
si!ned or unsi!ned inte!ers or inte!er vectors. 0he o1erands cannot :e vectors o& di&&erin! si=eJ this is
a co"1ile9ti"e error. I& one o1erand is a scalar and the other a vector, the scalar is a11lied
co"1onent9wise to the vector, resultin! in the sa"e t,1e as the vector. 0he &unda"ental t,1es o& the
o1erands Bsi!ned or unsi!nedC "ust "atch, and will :e the resultin! &unda"ental t,1e. 5or and B9C,
the result is the :itwise9and &unction o& the o1erands. 5or e2clusive9or B7C, the result is the :itwise
e2clusive9or &unction o& the o1erands. 5or inclusive9or B8C, the result is the :itwise inclusive9or
&unction o& the o1erands.
5or a co"1lete s1eci&ication o& the s,nta2 o& e21ressions, see section 6 G+hadin! Lan!ua!e Gra""ar.H
*.1$ !ector and 8atri, Operations
@ith a &ew e2ce1tions, o1erations are co"1onent9wise. 7suall,, when an o1erator o1erates on a vector or
"atri2, it is o1eratin! inde1endentl, on each co"1onent o& the vector or "atri2, in a co"1onent9wise
&ashion. 5or e2a"1le,
vec> v8 u;
float f;
v 4 u K f;
will :e e<uivalent to
v6x 4 u6x K f;
v60 4 u60 K f;
v6/ 4 u6/ K f;
And
vec> v8 u8 w;
w 4 v K u;
will :e e<uivalent to
w6x 4 v6x K u6x;
w60 4 v60 K u60;
w6/ 4 v6/ K u6/;
and liewise &or "ost o1erators and all inte!er and &loatin!91oint vector and "atri2 t,1es. 0he e2ce1tions
are "atri2 "ulti1lied :, vector, vector "ulti1lied :, "atri2, and "atri2 "ulti1lied :, "atri2. 0hese do
not o1erate co"1onent9wise, :ut rather 1er&or" the correct linear al!e:raic "ulti1l,.
vec> v8 u;
mat> m;
u 4 v L m;
is e<uivalent to
98
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<uivalent to
u6x 4 mI?J6x L v6x K mI1J6x L v60 K mI=J6x L v6/;
u60 4 mI?J60 L v6x K mI1J60 L v60 K mI=J60 L v6/;
u6/ 4 mI?J6/ L v6x K mI1J6/ L v60 K mI=J6/ L v6/;
And
mat> m8 n8 r;
r 4 m L n;
is e<uivalent to
rI?J6x 4 mI?J6x L nI?J6x K mI1J6x L nI?J60 K mI=J6x L nI?J6/;
rI1J6x 4 mI?J6x L nI1J6x K mI1J6x L nI1J60 K mI=J6x L nI1J6/;
rI=J6x 4 mI?J6x L nI=J6x K mI1J6x L nI=J60 K mI=J6x L nI=J6/;
rI?J60 4 mI?J60 L nI?J6x K mI1J60 L nI?J60 K mI=J60 L nI?J6/;
rI1J60 4 mI?J60 L nI1J6x K mI1J60 L nI1J60 K mI=J60 L nI1J6/;
rI=J60 4 mI?J60 L nI=J6x K mI1J60 L nI=J60 K mI=J60 L nI=J6/;
rI?J6/ 4 mI?J6/ L nI?J6x K mI1J6/ L nI?J60 K mI=J6/ L nI?J6/;
rI1J6/ 4 mI?J6/ L nI1J6x K mI1J6/ L nI1J60 K mI=J6/ L nI1J6/;
rI=J6/ 4 mI?J6/ L nI=J6x K mI1J6/ L nI=J60 K mI=J6/ L nI=J6/;
and si"ilarl, &or other si=es o& vectors and "atrices.
*.11 Out7of7/ounds 0ccesses
In the su:sections descri:ed a:ove &or arra,, vector, "atri2 and structure accesses, an, out9o&9:ounds
access 1roduced unde&ined :ehavior. /owever, i& ro:ust :u&&er access is ena:led via the -1enGL A3I,
such accesses will :e :ound within the "e"or, e2tent o& the active 1ro!ra". It will not :e 1ossi:le to
access "e"or, &ro" other 1ro!ra"s, and accesses will not result in a:nor"al 1ro!ra" ter"ination. -ut9
o&9:ounds reads return unde&ined values, which include values &ro" other varia:les o& the active 1ro!ra"
or =ero. -ut9o&9:ounds writes "a, :e discarded or overwrite other varia:les o& the active 1ro!ra",
de1endin! on the value o& the co"1uted inde2 and how this relates to the e2tent o& the active 1ro!ra"Fs
"e"or,. A11lications that re<uire de&ined :ehavior &or out9o&9:ounds accesses should ran!e chec all
co"1uted indices :e&ore dere&erencin! an arra,.
99
% Statements and Structure
0he &unda"ental :uildin! :locs o& the -1enGL +hadin! Lan!ua!e are:
state"ents and declarations
&unction de&initions
selection Bif(else and switc"(case(defaultC
iteration Bfor* w"ile* and do(w"ileC
Eu"1s Bdiscard* return* break* and continueC
0he overall structure o& a shader is as &ollows
translation-unit:
global-*eclaration
translation-unit global-*eclaration
global-*eclaration:
&unction-*e&inition
*eclaration
0hat is, a shader is a se<uence o& declarations and &unction :odies. 5unction :odies are de&ined as
&unction-*e&inition:
&unction-)rotot/)e D statement-list E
statement-list:
statement
statement-list statement
statement:
com)oun*-statement
sim)le-statement
1
6 State*ent" and Structure
$url, :races are used to !rou1 se<uences o& state"ents into co"1ound state"ents.
com)oun*-statement:
D statement-list E
sim)le-statement:
*eclaration-statement
e")ression-statement
selection-statement
iteration-statement
>um)-statement
+i"1le declaration, e21ression, and Eu"1 state"ents end in a se"i9colon.
0his a:ove is sli!htl, si"1li&ied, and the co"1lete !ra""ar s1eci&ied in section 6 G+hadin! Lan!ua!e
Gra""arH should :e used as the de&initive s1eci&ication.
Declarations and e21ressions have alread, :een discussed.
%.1 .unction +efinitions
As indicated :, the !ra""ar a:ove, a valid shader is a se<uence o& !lo:al declarations and &unction
de&initions. A &unction is declared as the &ollowin! e2a"1le shows:
// !rotot0!e
return,0!e function%ame (t0!e? arg?8 t0!e1 arg18 6668 t0!en argn);
and a &unction is de&ined lie
// definition
return,0!e function%ame (t0!e? arg?8 t0!e1 arg18 6668 t0!en argn)
5
// do some com!utation
return return(alue;
9
where return5/)e "ust :e 1resent and include a t,1e. I& the t,1e o& returnValue does not "atch
return5/)e. there "ust :e an i"1licit conversion in section (.1.1) GI"1licit $onversionsH that converts
the t,1e o& returnValue to return5/)e. or a co"1ile9ti"e error will result.
Each o& the t/)e7 "ust include a t,1e and can o1tionall, include 1ara"eter <uali&iers. 0he &or"al
ar!u"ent na"es Bargs a:oveC in the declarations are o1tional &or :oth the declaration and de&inition
&or"s.
A &unction is called :, usin! its na"e &ollowed :, a list o& ar!u"ents in 1arentheses.
Arra,s are allowed as ar!u"ents and as the return t,1e. In :oth cases, the arra, "ust :e e21licitl, si=ed.
An arra, is 1assed or returned :, usin! Eust its na"e, without :racets, and the si=e o& the arra, "ust
"atch the si=e s1eci&ied in the &unctionFs declaration.
+tructures are also allowed as ar!u"ent t,1es. 0he return t,1e can also :e structure.
11
6 State*ent" and Structure
+ee section 6 G+hadin! Lan!ua!e Gra""arH &or the de&initive re&erence on the s,nta2 to declare and
de&ine &unctions.
All &unctions "ust :e either declared with a 1rotot,1e or de&ined with a :od, :e&ore the, are called. 5or
e2a"1le:
float m0func (float f8 // f is an in!ut !arameter
out float g); // g is an out!ut !arameter
5unctions that return no value "ust :e declared as void. A void &unction can onl, use return without a
return ar!u"ent, even i& the return ar!u"ent has void t,1e. Return state"ents onl, acce1t values:
void &unc1BC _ `
void &unc#BC _ return &unc1BCJ ` DD ille!al return state"ent
-nl, a 1recision <uali&ier is allowed on the return t,1e o& a &unction. 5or"al 1ara"eters can have
1ara"eter, 1recision, and "e"or, <uali&iers, :ut no other <uali&iers.
5unctions that acce1t no in1ut ar!u"ents need not use void in the ar!u"ent list :ecause 1rotot,1es Bor
de&initionsC are re<uired and there&ore there is no a":i!uit, when an e"1t, ar!u"ent list ZB CZ is declared.
0he idio" GBvoidCH as a 1ara"eter list is 1rovided &or convenience.
5unction na"es can :e overloaded. 0he sa"e &unction na"e can :e used &or "ulti1le &unctions, as lon!
as the 1ara"eter t,1es di&&er. I& a &unction na"e is declared twice with the sa"e 1ara"eter t,1es, then the
return t,1es and all <uali&iers "ust also "atch, and it is the sa"e &unction :ein! declared. 5or e2a"1le,
vec< f(in vec< x8 out vec< 0); // (D)
vec< f(in vec< x8 out uvec< 0); // (:) oka08 different argument t0!e
vec< f(in ivec< x8 out uvec< 0); // (M) oka08 different argument t0!e
int f(in vec< x8 out ivec< 0); // error8 onl0 return t0!e differs
vec< f(in vec< x8 in vec< 0); // error8 onl0 qualifier differs
vec< f(const in vec< x8 out vec< 0); // error8 onl0 qualifier differs
@hen &unction calls are resolved, an e2act t,1e "atch &or all the ar!u"ents is sou!ht. I& an e2act "atch is
&ound, all other &unctions are i!nored, and the e2act "atch is used. I& no e2act "atch is &ound, then the
i"1licit conversions in section (.1.1) GI"1licit $onversionsH will :e a11lied to &ind a "atch.
;is"atched t,1es on in1ut 1ara"eters Bin or inout or de&ault! "ust have a conversion &ro" the callin!
ar!u"ent t,1e to the &or"al 1ara"eter t,1e. ;is"atched t,1es on out1ut 1ara"eters Bout or inoutC "ust
have a conversion &ro" the &or"al 1ara"eter t,1e to the callin! ar!u"ent t,1e.
I& i"1licit conversions can :e used to &ind "ore than one "atchin! &unction, a sin!le :est9"atchin!
&unction is sou!ht. 0o deter"ine a :est "atch, the conversions :etween callin! ar!u"ent and &or"al
1ara"eter t,1es are co"1ared &or each &unction ar!u"ent and 1air o& "atchin! &unctions. A&ter these
co"1arisons are 1er&or"ed, each 1air o& "atchin! &unctions are co"1ared. A &unction declaration # is
considered a :etter "atch than &unction declaration % i&
&or at least one &unction ar!u"ent, the conversion &or that ar!u"ent in # is :etter than the
corres1ondin! conversion in %J and
12
6 State*ent" and Structure
there is no &unction ar!u"ent &or which the conversion in % is :etter than the corres1ondin!
conversion in #.
I& a sin!le &unction declaration is considered a :etter "atch than ever, other "atchin! &unction
declaration, it will :e used. -therwise, a co"1ile9ti"e se"antic error &or an a":i!uous overloaded
&unction call occurs.
0o 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 a11lied, in order:
1. An e2act "atch is :etter than a "atch involvin! an, i"1licit conversion.
#. A "atch involvin! an i"1licit conversion &ro" float to double is :etter than a "atch involvin!
an, other i"1licit conversion.
%. A "atch involvin! an i"1licit conversion &ro" either int or uint to float is :etter than a "atch
involvin! an i"1licit conversion &ro" either int or uint to double.
I& none o& the rules a:ove a11l, to a 1articular 1air o& conversions, neither conversion is considered :etter
than the other.
5or the e2a"1le &unction 1rotot,1es BAC, BBC, and B$C a:ove, the &ollowin! e2a"1les show how the rules
a11l, to di&&erent sets o& callin! ar!u"ent t,1es:
f(vec<8 vec<); // exact match of vec< f(in vec< x8 out vec< 0)
f(vec<8 uvec<); // exact match of vec< f(in vec< x8 out ivec< 0)
f(vec<8 ivec<); // matched to vec< f(in vec< x8 out vec< 0)
// (M) not relevant8 can't convert vec< to
// ivec<6 (D) better than (:) for =nd
// argument (rule =)8 same on first argument6
f(ivec<8 vec<); // %+, matched6 Dll three match b0 im!licit
// conversion6 (M) is better than (D) and (:)
// on the first argument6 (D) is better than
// (:) and (M)6
7ser9de&ined &unctions can have "ulti1le declarations, :ut onl, one de&inition. A shader can rede&ine
:uilt9in &unctions. I& a :uilt9in &unction is redeclared in a shader Bi.e., a 1rotot,1e is visi:leC :e&ore a call
to it, then the liner will onl, atte"1t to resolve that call within the set o& shaders that are lined with it.
0he &unction main is used as the entr, 1oint to a shader e2ecuta:le. A shader need not contain a &unction
na"ed main, :ut one shader in a set o& shaders lined to!ether to &or" a sin!le shader e2ecuta:le "ust, or
a lin9ti"e error results. 0his &unction taes no ar!u"ents, returns no value, and "ust :e declared as t,1e
void=
void main()
5
666
9
0he &unction main can contain uses o& return. +ee section *.( GJu"1sH &or "ore details.
It is a co"1ile9ti"e or lin9ti"e error to declare or de&ine a &unction main with an, other 1ara"eters or
return t,1e.
13
6 State*ent" and Structure
%.1.1 .unction Calling Conventions
5unctions are called :, value9return. 0his "eans in1ut ar!u"ents are co1ied into the &unction at call ti"e,
and out1ut ar!u"ents are co1ied :ac to the caller :e&ore &unction e2it. Because the &unction wors with
local co1ies o& 1ara"eters, there are no issues re!ardin! aliasin! o& varia:les within a &unction. 0o
control what 1ara"eters are co1ied in andDor out throu!h a &unction de&inition or declaration:
0he e,word in is used as a <uali&ier to denote a 1ara"eter is to :e co1ied in, :ut not co1ied out.
0he e,word out is used as a <uali&ier to denote a 1ara"eter is to :e co1ied out, :ut not co1ied in.
0his should :e used whenever 1ossi:le to avoid unnecessaril, co1,in! 1ara"eters in.
0he e,word inout is used as a <uali&ier to denote the 1ara"eter is to :e :oth co1ied in and co1ied
out. It "eans the sa"e thin! as s1eci&,in! :oth in and out.
A &unction 1ara"eter declared with no such <uali&ier "eans the sa"e thin! as s1eci&,in! in.
All ar!u"ents are evaluated at call ti"e, e2actl, once, in order, &ro" le&t to ri!ht. Evaluation o& an in
1ara"eter results in a value that is co1ied to the &or"al 1ara"eter. Evaluation o& an out 1ara"eter results
in an l9value that is used to co1, out a value when the &unction returns. Evaluation o& an inout 1ara"eter
results in :oth a value and an l9valueJ the value is co1ied to the &or"al 1ara"eter at call ti"e and the l9
value is used to co1, out a value when the &unction returns.
0he order in which out1ut 1ara"eters are co1ied :ac to the caller is unde&ined.
I& the &unction "atchin! descri:ed in the 1revious section re<uired ar!u"ent t,1e conversions, these
conversions are a11lied at co1,9in and co1,9out ti"es.
In a &unction, writin! to an in1ut9onl, 1ara"eter is allowed. -nl, the &unctionOs co1, is "odi&ied. 0his
can :e 1revented :, declarin! a 1ara"eter with the const <uali&ier.
@hen callin! a &unction, e21ressions that do not evaluate to l9values cannot :e 1assed to 1ara"eters
declared as out or inout, or a co"1ile9ti"e error results
14
6 State*ent" and Structure
&unction-)rotot/)e :
)recision-3uali&ier t/)e &unction-name:)arameter-3uali&iers )recision-3uali&ier t/)e name
arra/-s)eci&ier. ... ;
t/)e :
an, :asic t,1e, arra, t,1e, structure na"e, or structure *e&inition
)arameter-3uali&iers :
e"1t,
list o& )arameter-3uali&ier
)arameter-3uali&ier :
const
in
out
inout
precise
memor/ 3uali&ier
)recision 3uali&ier
name :
e"1t,
identi&ier
arra/-s)eci&ier :
e"1t,
3 integral-constant-e")ression 4
0he const <uali&ier cannot :e used with out or inout, or a co"1ile9ti"e error results. 0he a:ove is used
:oth &or &unction declarations Bi.e., 1rotot,1esC and &or &unction de&initions. /ence, &unction de&initions
can have unna"ed ar!u"ents.
Recursion is not allowed, not even staticall,. +tatic recursion is 1resent i& the static &unction9call !ra1h o&
a 1ro!ra" contains c,cles. 0his includes all 1otential &unction calls throu!h varia:les declared as
subroutine uniform Bdescri:ed :elowC. It is a co"1ile9ti"e or lin9ti"e error i& a sin!le co"1ilation
unit BshaderC contains either static recursion or the 1otential &or recursion throu!h su:routine varia:les.
%.1.2 Subroutines
+u:routines 1rovide a "echanis" allowin! shaders to :e co"1iled in a "anner where the tar!et o& one or
"ore &unction calls can :e chan!ed at run9ti"e without re<uirin! an, shader reco"1ilation. 5or e2a"1le,
a sin!le shader "a, :e co"1iled with su11ort &or "ulti1le illu"ination al!orith"s to handle di&&erent
inds o& li!hts or sur&ace "aterials. An a11lication usin! such a shader "a, switch illu"ination
al!orith"s :, chan!in! the value o& its su:routine uni&or"s. 0o use su:routines, a su:routine t,1e is
declared, one or "ore &unctions are associated with that su:routine t,1e, and a su:routine varia:le o& that
t,1e is declared. 0he &unction currentl, assi!ned to the varia:le &unction is then called :, usin! &unction
callin! s,nta2 re1lacin! a &unction na"e with the na"e o& the su:routine varia:le. +u:routine varia:les
15
6 State*ent" and Structure
are uni&or"s, and are assi!ned to s1eci&ic &unctions onl, throu!h co""ands BTniformSubroutinesuivC in
the -1enGL A3I.
+u:routine t,1es 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 Bargs a:oveC are o1tional. 5unctions are
associated with su:routine t,1es o& "atchin! declarations :, de&inin! the &unction with the subroutine
e,word and a list o& su:routine t,1es 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"1ile9ti"e error i& ar!u"ents and return t,1e donFt "atch :etween the &unction and each
associated su:routine t,1e.
5unctions declared with subroutine "ust include a :od,. An overloaded &unction cannot :e declared
with subroutineJ a 1ro!ra" will &ail to co"1ile 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 su:routine t,1e.
A &unction declared with subroutine can also :e called directl, with a static use o& &unction7ame, as is
done with non9su:routine &unction declarations and calls.
+u:routine t,1e varia:les are re<uired to :e subroutine uni&orms, and are declared with a s1eci&ic
su:routine t,1e in a su:routine uni&or" varia:le declaration:
subroutine uniform subroutine,0!e%ame subroutine(ar%ame;
+u:routine uni&or" varia:les are called the sa"e wa, &unctions are called. @hen a su:routine varia:le
Bor an ele"ent o& a su:routine varia:le arra,C is associated with a 1articular &unction, all &unction calls
throu!h that varia:le will call that 1articular &unction.
7nlie other uni&or" varia:les, su:routine uni&or" varia:les are sco1ed to the shader e2ecution sta!e the
varia:le is declared in.
+u:routine varia:les "a, :e declared as e21licitl,9si=ed arra,s, which can :e d,na"icall, inde2ed at use.
%.2 Selection
$onditional control &low in the shadin! lan!ua!e is done :, either if, if9else, or switc" state"ents:
selection-statement :
if B bool-e")ression C statement
if B bool-e")ression C statement else statement
switc" B init-e")ression C _ s2itc4-statement-listo)t

`
@here s2itc4-statement-list is a list o& =ero or "ore s2itc4-statement and other state"ents de&ined :, the
lan!ua!e, where s2itc4-statement adds so"e &or"s o& la:els. 0hat is
16
6 State*ent" and Structure
s2itc4-statement-list :
s2itc4-statement
s2itc4-statement-list s2itc4-statement
s2itc4-statement :
case constant-e")ression =
default =
statement
I& an if(e21ression evaluates to true, then the &irst statement is e2ecuted. I& it evaluates to false and there
is an else 1art then the second statement is e2ecuted.
An, e21ression whose t,1e evaluates to a Boolean can :e used as the conditional e21ression bool-
e")ression. Vector t,1es are not acce1ted as the e21ression to if.
$onditionals can :e nested.
0he t,1e o& the init-e")ression value in a switch state"ent "ust :e a scalar int or uint. 0he t,1e o& the
constant-e")ression value in a case la:el also "ust :e a scalar int or uint. @hen an, 1air o& these values
is tested &or Ze<ual valueZ and the t,1es do not "atch, an i"1licit conversion will :e done to convert the
int to a uint Bsee section (.1.1) GI"1licit $onversionsHC :e&ore the co"1are is done. I& a case la:el has a
constant-e")ression o& e<ual value to init-e")ression, e2ecution will continue a&ter that la:el. It is a
co"1ile9ti"e error to have two case la:el constant-e")ression o& e<ual value. -therwise, i& there is a
default la:el, e2ecution will continue a&ter that la:el. -therwise, e2ecution si1s the rest o& the switch
state"ent. It is a co"1ile9ti"e error to have "ore than one default. A break state"ent not nested in a
loo1 or other switch state"ent Beither not nested or nested onl, in if or if9else state"entsC will also si1
the rest o& the switch state"ent. 5all throu!h la:els are allowed, :ut it is a co"1ile9ti"e error to have no
state"ent :etween a la:el and the end o& the switc" state"ent. 8o state"ents are allowed in a switch
state"ent :e&ore the &irst case state"ent.
8o case or default la:els can :e nested inside other &low control nested within their corres1ondin!
switc".
%.# Iteration
5or, while, and do loo1s are allowed as &ollows:
for (init3ex!ression; condition3ex!ression; loo!3ex!ression)
sub3statement
while (condition3ex!ression)
sub3statement
do
statement
while (condition3ex!ression)
+ee section 6 G+hadin! Lan!ua!e Gra""arH &or the de&initive s1eci&ication o& loo1s.
0he for loo1 &irst evaluates the init-e")ression, then the con*ition-e")ression. I& the con*ition-
e")ression evaluates to true, then the :od, o& the loo1 is e2ecuted. A&ter the :od, is e2ecuted, a for loo1
17
6 State*ent" and Structure
will then evaluate the loo)-e")ression, and then loo1 :ac to evaluate the con*ition-e")ression, re1eatin!
until the con*ition-e")ression evaluates to &alse. 0he loo1 is then e2ited, si11in! its :od, and si11in!
its loo)-e")ression. Varia:les "odi&ied :, the loo)-e")ression "aintain their value a&ter the loo1 is
e2ited, 1rovided the, are still in sco1e. Varia:les declared in init-e")ression or con*ition-e")ression are
onl, in sco1e until the end o& the su:9state"ent o& the for loo1.
0he w"ile loo1 &irst evaluates the con*ition-e")ression. I& true, then the :od, is e2ecuted. 0his is then
re1eated, until the con*ition-e")ression evaluates to &alse, e2itin! the loo1 and si11in! its :od,.
Varia:les declared in the con*ition-e")ression are onl, in sco1e until the end o& the su:9state"ent o& the
while loo1.
0he do(w"ile loo1 &irst e2ecutes the :od,, then e2ecutes the con*ition-e")ression. 0his is re1eated until
con*ition-e")ression evaluates to &alse, and then the loo1 is e2ited.
E21ressions &or con*ition-e")ression "ust evaluate to a Boolean.
Both the con*ition-e")ression and the init-e")ression can declare and initiali=e a varia:le, e2ce1t in the
do(w"ile loo1, which cannot declare a varia:le in its con*ition-e")ression. 0he varia:leOs sco1e lasts
onl, until the end o& the su:9state"ent that &or"s the :od, o& the loo1.
Loo1s can :e nested.
8on9ter"inatin! loo1s are allowed. 0he conse<uences o& ver, lon! or non9ter"inatin! loo1s are 1lat&or"
de1endent.
%." <umps
0hese are the Eu"1s:
>um)statement:
continue>
break>
return>
return e")ression>
discard> .. in the &ra!"ent shader lan!ua!e onl,
0here is no G!otoH nor other non9structured &low o& control.
0he continue Eu"1 is used onl, in loo1s. It si1s the re"ainder o& the :od, o& the inner "ost loo1 o&
which it is inside. 5or w"ile and do(w"ile loo1s, this Eu"1 is to the ne2t evaluation o& the loo1
con*ition-e")ression &ro" which the loo1 continues as 1reviousl, de&ined. 5or for loo1s, the Eu"1 is to
the loo)-e")ression, &ollowed :, the con*ition-e")ression.
0he break Eu"1 can also :e used onl, in loo1s and switch state"ents. It is si"1l, an i""ediate e2it o&
the inner9"ost loo1 or switch state"ents containin! the break. 8o &urther e2ecution o& con*ition-
e")ression. loo)-e")ression, or s2itc4-statement is done.
0he discard e,word is onl, allowed within &ra!"ent shaders. It can :e used within a &ra!"ent shader to
a:andon the o1eration on the current &ra!"ent. 0his e,word causes the &ra!"ent to :e discarded and no
u1dates to an, :u&&ers will occur. $ontrol &low e2its the shader, and su:se<uent i"1licit or e21licit
18
6 State*ent" and Structure
derivatives are unde&ined when this e2it is non9uni&or". It would t,1icall, :e used within a conditional
state"ent, &or e2a"1le:
if (intensit0 7 ?6?)
discard;
A &ra!"ent shader "a, test a &ra!"entOs al1ha value and discard the &ra!"ent :ased on that test.
/owever, it should :e noted that covera!e testin! occurs a&ter the &ra!"ent shader runs, and the covera!e
test can chan!e the al1ha value.
0he return Eu"1 causes i""ediate e2it o& the current &unction. I& it has e")ression then that is the return
value &or the &unction.
0he &unction main can use return. 0his si"1l, causes main to e2it in the sa"e wa, as when the end o&
the &unction had :een reached. It does not i"1l, a use o& discard in a &ra!"ent shader. 7sin! return in
"ain :e&ore de&inin! out1uts will have the sa"e :ehavior as reachin! the end o& "ain :e&ore de&inin!
out1uts.
19
/uilt7in !ariables
.1 /uilt7In Language !ariables
+o"e -1enGL o1erations occur in &i2ed &unctionalit, and need to 1rovide values to or receive values
&ro" shader e2ecuta:les. +haders co""unicate with &i2ed9&unction -1enGL 1i1eline sta!es, and
o1tionall, with other shader e2ecuta:les, throu!h the use o& :uilt9in in1ut and out1ut varia:les.
In the co"1ute lan!ua!e, the :uilt9in varia:les are declared as &ollows:
// work grou! dimensions
in uvec> gl"%umTork.rou!s;
const uvec> gl"Tork.rou!*i/e;
// work grou! and invocation $-s
in uvec> gl"Tork.rou!$-;
in uvec> gl"#ocal$nvocation$-;
// derived variables
in uvec> gl".lobal$nvocation$-;
in uint gl"#ocal$nvocation$ndex;
In the verte2 lan!ua!e, the :uilt9ins are intrinsicall, declared as:
in int gl"(ertex$-;
in int gl"$nstance$-;
out gl"Her(ertex 5
vec< gl"Hosition;
float gl"Hoint*i/e;
float gl"Mli!-istanceIJ;
9;
11
7 !ui%t+in #aria$%e"
In the !eo"etr, lan!ua!e, the :uilt9in varia:les are intrinsicall, declared as:
in gl"Her(ertex 5
vec< gl"Hosition;
float gl"Hoint*i/e;
float gl"Mli!-istanceIJ;
9 gl"inIJ;
in int gl"Hrimitive$-$n;
in int gl"$nvocation$-;
out gl"Her(ertex 5
vec< gl"Hosition;
float gl"Hoint*i/e;
float gl"Mli!-istanceIJ;
9;
out int gl"Hrimitive$-;
out int gl"#a0er;
out int gl"(iew!ort$ndex;
111
7 !ui%t+in #aria$%e"
In the tessellation control lan!ua!e, :uilt9in varia:les are intrinsicall, declared as:
in gl"Her(ertex 5
vec< gl"Hosition;
float gl"Hoint*i/e;
float gl"Mli!-istanceIJ;
9 gl"inIgl"GaxHatch(erticesJ;
in int gl"Hatch(ertices$n;
in int gl"Hrimitive$-;
in int gl"$nvocation$-;
out gl"Her(ertex 5
vec< gl"Hosition;
float gl"Hoint*i/e;
float gl"Mli!-istanceIJ;
9 gl"outIJ;
!atch out float gl",ess#evel+uterI<J;
!atch out float gl",ess#evel$nnerI=J;
In the tessellation evaluation lan!ua!e, :uilt9in varia:les are intrinsicall, declared as:
in gl"Her(ertex 5
vec< gl"Hosition;
float gl"Hoint*i/e;
float gl"Mli!-istanceIJ;
9 gl"inIgl"GaxHatch(erticesJ;
in int gl"Hatch(ertices$n;
in int gl"Hrimitive$-;
in vec> gl",essMoord;
!atch in float gl",ess#evel+uterI<J;
!atch in float gl",ess#evel$nnerI=J;
out gl"Her(ertex 5
vec< gl"Hosition;
float gl"Hoint*i/e;
float gl"Mli!-istanceIJ;
9;
112
7 !ui%t+in #aria$%e"
In the &ra!"ent lan!ua!e, :uilt9in varia:les are intrinsicall, declared as:
in vec< gl"'ragMoord;
in bool gl"'ront'acing;
in float gl"Mli!-istanceIJ;
in vec= gl"HointMoord;
in int gl"Hrimitive$-;
in int gl"*am!le$-;
in vec= gl"*am!leHosition;
in int gl"*am!leGask$nIJ;
in int gl"#a0er;
in int gl"(iew!ort$ndex;
out float gl"'rag-e!th;
out int gl"*am!leGaskIJ;
Each o& the a:ove varia:les is discussed :elow.
0he :uilt9in varia:le gl7um+or,-rou)s is a co"1ute9shader in1ut varia:le containin! the total nu":er
o& !lo:al wor ite"s in each di"ension o& the wor !rou1 that will e2ecute the co"1ute shader. Its
content is e<ual to the values s1eci&ied in the numgrou)s", numgrou)s/, and numgrou)s(
1ara"eters 1assed to the Dis)atc4$om)ute A3I entr, 1oint.
0he :uilt9in constant gl+or,-rou)'i(e is a co"1ute9shader constant containin! the local wor9!rou1
si=e o& the shader. 0he si=e o& the wor !rou1 in the F, G, and H di"ensions is stored in the ", /, and (
co"1onents. 0he constants values in gl+or,-rou)'i(e will "atch those s1eci&ied in the re<uired
localsi(e", localsi(e/, and localsi(e( la,out <uali&iers &or the current shader. 0his is a constant so
that it can :e used to si=e arra,s o& "e"or, that can :e shared within the local wor !rou1. It is a
co"1ile9ti"e error to use gl+or,-rou)'i(e in a shader :e&ore that shader has declared the in1ut la,out
<uali&ier that sets the identi&iers localsi(e", localsi(e/, and localsi(e(. @hen a si=e is !iven &or
so"e o& these identi&iers, :ut not all, the corres1ondin! gl+or,-rou)'i(e will have a si=e o& 1.
0he :uilt9in varia:le gl+or,-rou)0D is a co"1ute9shader in1ut varia:le containin! the three9
di"ensional inde2 o& the !lo:al wor !rou1 that the current invocation is e2ecutin! in. 0he 1ossi:le
values ran!e across the 1ara"eters 1assed into Dis)atc4$om)ute, i.e., &ro" B), ), )C to
Bgl7um+or,-rou)s." 9 1, gl7um+or,-rou)s./ 9 1, gl7um+or,-rou)s.( 91C.
0he :uilt9in varia:le glLocal0nvocation0D is a co"1ute9shader in1ut varia:le containin! the t9
di"ensional inde2 o& the local wor !rou1 within the !lo:al wor !rou1 that the current invocation is
e2ecutin! in. 0he 1ossi:le values &or this varia:le ran!e across the local wor !rou1 si=e, i.e., B),),)C to
Bgl+or,-rou)'i(e." 9 1, gl+or,-rou)'i(e./ 9 1, gl+or,-rou)'i(e.( 9 1C.
0he :uilt9in varia:le gl-lobal0nvocation0D is a co"1ute shader in1ut varia:le containin! the !lo:al
inde2 o& the current wor ite". 0his value uni<uel, identi&ies this invocation &ro" all other invocations
across all local and !lo:al wor !rou1s initiated :, the current Dis)atc4$om)ute call. 0his is co"1uted
as:
gl".lobal$nvocation$- 4
gl"Tork.rou!$- L gl"Tork.rou!*i/e K gl"#ocal$nvocation$-;
113
7 !ui%t+in #aria$%e"
0he :uilt9in varia:le glLocal0nvocation0n*e" is a co"1ute shader in1ut varia:le that contains the one9
di"ensional re1resentation o& the glLocal0nvocation0D. 0his is use&ul &or uni<uel, identi&,in! a uni<ue
re!ion o& shared "e"or, within the local wor !rou1 &or this invocation to use. 0his is co"1uted as:
gl"#ocal$nvocation$ndex 4
gl"#ocal$nvocation$-6/ L gl"Tork.rou!*i/e6x L gl"Tork.rou!*i/e60 K
gl"#ocal$nvocation$-60 L gl"Tork.rou!*i/e6x K
gl"#ocal$nvocation$-6x;
0he varia:le glVerte"0D is a verte2 lan!ua!e in1ut varia:le that holds an inte!er inde2 &or the verte2, as
de&ined under G+hader In1utsH in section 11.1.%.6 G+hader In1utsH in the -1enGL Gra1hics +,ste"
+1eci&ication. @hile the varia:le glVerte"0D is alwa,s 1resent, its value is not alwa,s de&ined.
0he varia:le gl0nstance0D is a verte2 lan!ua!e in1ut varia:le that holds the instance nu":er o& the
current 1ri"itive in an instanced draw call Bsee G+hader In1utsH in section 11.1.%.6 G+hader In1utsH in the
-1enGL Gra1hics +,ste" +1eci&icationC. I& the current 1ri"itive does not co"e &ro" an instanced draw
call, the value o& gl0nstance0D is =ero.
As an out1ut varia:le, glBosition is intended &or writin! the ho"o!eneous verte2 1osition. It can :e
written at an, ti"e durin! shader e2ecution. 0his value will :e used :, 1ri"itive asse":l,, cli11in!,
cullin!, and other &i2ed &unctionalit, o1erations, i& 1resent, that o1erate on 1ri"itives a&ter verte2
1rocessin! has occurred. Its value is unde&ined a&ter the verte2 1rocessin! sta!e i& the verte2 shader
e2ecuta:le does not write glBosition, and it is unde&ined a&ter !eo"etr, 1rocessin! i& the !eo"etr,
e2ecuta:le calls &mitUertexBC without havin! written glBosition since the last &mitUertexBC Bor hasnFt
written it at allC. As an in1ut varia:le, glBosition reads the out1ut written in the 1revious shader sta!e to
glBosition.
As an out1ut varia:le, glBoint'i(e is intended &or a shader to write the si=e o& the 1oint to :e rasteri=ed.
It is "easured in 1i2els. I& glBoint'i(e is not written to, its value is unde&ined in su:se<uent 1i1e sta!es.
As an in1ut varia:le, glBoint'i(e reads the out1ut written in the 1revious shader sta!e to glBoint'i(e .
0he varia:le gl$li)Distance 1rovides the &orward co"1ati:le "echanis" &or controllin! user cli11in!.
0he ele"ent gl$li)Distance<i= s1eci&ies a cli1 distance &or each 1lane i. A distance o& ) "eans the
verte2 is on the 1lane, a 1ositive distance "eans the verte2 is inside the cli1 1lane, and a ne!ative distance
"eans the 1oint is outside the cli1 1lane. 0he cli1 distances will :e linearl, inter1olated across the
1ri"itive and the 1ortion o& the 1ri"itive with inter1olated distances less than ) will :e cli11ed.
0he gl$li)Distance arra, is 1redeclared as unsi=ed and "ust :e si=ed :, the shader either redeclarin! it
with a si=e or inde2in! it onl, with inte!ral constant e21ressions. 0his needs to si=e the arra, to include
all the cli1 1lanes that are ena:led via the -1enGL A3IJ i& the si=e does not include all ena:led 1lanes,
results are unde&ined. 0he si=e can :e at "ost gl!a"$li)Distances. 0he nu":er o& var,in! co"1onents
Bsee gl!a"Var/ing$om)onents; consu"ed :, gl$li)Distance will "atch the si=e o& the arra,, no
"atter how "an, 1lanes are ena:led. 0he shader "ust also set all values in gl$li)Distance that have
:een ena:led via the -1enGL A3I, or results are unde&ined. Values written into gl$li)Distance &or
1lanes that are not ena:led have no e&&ect.
As an out1ut varia:le, gl$li)Distance 1rovides the 1lace &or the shader to write these distances. As an
in1ut in all :ut the &ra!"ent lan!ua!e, it reads the values written in the 1revious shader sta!e. In the
&ra!"ent lan!ua!e, gl$li)Distance arra, contains linearl, inter1olated values &or the verte2 values
114
7 !ui%t+in #aria$%e"
written :, a shader to the gl$li)Distance verte2 out1ut varia:le. -nl, ele"ents in this arra, that have
cli11in! ena:led will have de&ined values.
0he out1ut varia:le glBrimitive0D is availa:le onl, in the !eo"etr, lan!ua!e and 1rovides a sin!le
inte!er that serves as a 1ri"itive identi&ier. 0his is then availa:le to &ra!"ent shaders as the &ra!"ent
in1ut glBrimitive0D, which will select the written 1ri"itive ID &ro" the 1rovoin! verte2 in the 1ri"itive
:ein! shaded. I& a &ra!"ent shader usin! glBrimitive0D is active and a !eo"etr, shader is also active,
the !eo"etr, shader "ust write to glBrimitive0D or the &ra!"ent shader in1ut glBrimitive0D is
unde&ined. +ee section 11.%.(.. ZGeo"etr, +hader -ut1utsZ o& the -1enGL Gra1hics +,ste"
+1eci&ication &or "ore in&or"ation.
5or tessellation control and evaluation lan!ua!es the in1ut varia:le glBrimitive0D is &illed with the
nu":er o& 1ri"itives 1rocessed :, the shader since the current set o& renderin! 1ri"itives was started.
5or the &ra!"ent lan!ua!e, it is &illed with the value written to the glBrimitive0D !eo"etr, shader out1ut
i& a !eo"etr, shader is 1resent. -therwise, it is assi!ned in the sa"e "anner as with tessellation control
and evaluation shaders.
0he !eo"etr, lan!ua!e in1ut varia:le glBrimitive0D0n :ehaves identicall, to the tessellation control and
evaluation lan!ua!e in1ut varia:le glBrimitive0D.
0he in1ut varia:le gl0nvocation0D is availa:le onl, in the tessellation control and !eo"etr, lan!ua!es.
In the tessellation control shader, it identi&ies the nu":er o& the out1ut 1atch verte2 assi!ned to the
tessellation control shader invocation. In the !eo"etr, shader, it identi&ies the invocation nu":er
assi!ned to the !eo"etr, shader invocation. In :oth cases, gl0nvocation0D is assi!ned inte!er values in
the ran!e <0. 7-1=, where 7 is the nu":er o& out1ut 1atch vertices or !eo"etr, shader invocations 1er
1ri"itive.
0he varia:le glLa/er is availa:le as an out1ut varia:le in the !eo"etr, lan!ua!e and an in1ut varia:le in
the &ra!"ent lan!ua!e. In the !eo"etr, lan!ua!e, it is used to select a s1eci&ic la,er Bor &ace and la,er o&
a cu:e "a1C o& a "ulti9la,er &ra"e:u&&er attach"ent. 0he actual la,er used will co"e &ro" one o& the
vertices in the 1ri"itive :ein! shaded. @hich verte2 the la,er co"es &ro" is unde&ined, so it is :est to
write the sa"e la,er value &or all vertices o& a 1ri"itive. I& a shader staticall, assi!ns a value to glLa/er,
la,ered renderin! "ode is ena:led. +ee section 11.%.(.. GGeo"etr, +hader -ut1utsHand section 6.(.6
GLa,ered 5ra"e:u&&ersH o& the -1enGL Gra1hics +,ste" +1eci&ication &or "ore in&or"ation. I& a shader
staticall, assi!ns a value to glLa/er, and there is an e2ecution 1ath throu!h the shader that does not set
glLa/er, then the value o& glLa/er is unde&ined &or e2ecutions o& the shader that tae that 1ath.
115
7 !ui%t+in #aria$%e"
0he out1ut varia:le glLa/er taes on a s1ecial value when used with an arra, o& cu:e "a1 te2tures.
Instead o& onl, re&errin! to the la,er, it is used to select a cu:e "a1 &ace and a la,er. +ettin! glLa/er to
the value la/erAI?&ace will render to &ace &ace o& the cu:e de&ined in la,er la/er. 0he &ace values are
de&ined in 0a:le 6.% o& section 6.(.6 GLa,ered 5ra"e:u&&ersH o& the -1enGL Gra1hics +,ste"
+1eci&ication, :ut re1eated :elow &or clarit,.
Nace Ualue Iesulting Barget
)
,&U,@)&"M@:&"GDH"H+*$,$(&"U
1
,&U,@)&"M@:&"GDH"%&.D,$(&"U
#
,&U,@)&"M@:&"GDH"H+*$,$(&"V
%
,&U,@)&"M@:&"GDH"%&.D,$(&"V
(
,&U,@)&"M@:&"GDH"H+*$,$(&"W
.
,&U,@)&"M@:&"GDH"%&.D,$(&"W
5or e2a"1le, to render to the 1ositive / cu:e "a1 &ace located in the .th la,er o& the cu:e "a1 arra,,
glLa/er should :e set to JAI?2.
0he in1ut varia:le glLa/er in the &ra!"ent lan!ua!e will have the sa"e value that was written to the
out1ut varia:le glLa/er in the !eo"etr, lan!ua!e. I& the !eo"etr, sta!e does not d,na"icall, assi!n a
value to glLa/er, the value o& glLa/er in the &ra!"ent sta!e will :e unde&ined. I& the !eo"etr, sta!e
"aes no static assi!n"ent to glLa/er, the in1ut glValue in the &ra!"ent sta!e will :e =ero. -therwise,
the &ra!"ent sta!e will read the sa"e value written :, the !eo"etr, sta!e, even i& that value is out o&
ran!e. I& a &ra!"ent shader contains a static access to glLa/er, it will count a!ainst the i"1le"entation
de&ined li"it &or the "a2i"u" nu":er o& in1uts to the &ra!"ent sta!e.
0he varia:le glVie2)ort0n*e" is availa:le as an out1ut varia:le in the !eo"etr, lan!ua!e and an in1ut
varia:le in the &ra!"ent lan!ua!e. In the !eo"etr, lan!ua!e, it 1rovides the inde2 o& the view1ort to
which the ne2t 1ri"itive e"itted &ro" the !eo"etr, shader should :e drawn. 3ri"itives !enerated :,
the !eo"etr, shader will under!o view1ort trans&or"ation and scissor testin! usin! the view1ort
trans&or"ation and scissor rectan!le selected :, the value o& glVie2)ort0n*e". 0he view1ort inde2 used
will co"e &ro" one o& the vertices in the 1ri"itive :ein! shaded. /owever, which verte2 the view1ort
inde2 co"es &ro" is i"1le"entation9de1endent, so it is :est to use the sa"e view1ort inde2 &or all
vertices o& the 1ri"itive. I& a !eo"etr, shader does not assi!n a value to glVie2)ort0n*e", view1ort
trans&or" and scissor rectan!le =ero will :e used. I& a !eo"etr, shader staticall, assi!ns a value to
glVie2)ort0n*e" and there is a 1ath throu!h the shader that does not assi!n a value to glVie2)ort0n*e",
the value o& glVie2)ort0n*e" is unde&ined &or e2ecutions o& the shader that tae that 1ath. +ee section
11.%.(.* GLa,er and View1ort +electionH o& the -1enGL Gra1hics +,ste" +1eci&ication B$ore 3ro&ileC &or
"ore in&or"ation.
0he in1ut varia:le glVie2)ort0n*e" in the &ra!"ent sta!e will have the sa"e value that was written to the
out1ut varia:le glVie2)ort0n*e" in the !eo"etr, sta!e. I& the !eo"etr, sta!e does not d,na"icall,
assi!n to glVie2)ort0n*e", the value o& glVie2)ort0n*e" in the &ra!"ent shader will :e unde&ined. I&
the !eo"etr, sta!e "aes no static assi!n"ent to glVie2)ort0n*e", the &ra!"ent sta!e will read =ero.
-therwise, the &ra!"ent sta!e will read the sa"e value written :, the !eo"etr, sta!e, even i& that value is
116
7 !ui%t+in #aria$%e"
out o& ran!e. I& a &ra!"ent shader contains a static access to glVie2)ort0n*e", it will count a!ainst the
i"1le"entation de&ined li"it &or the "a2i"u" nu":er o& in1uts to the &ra!"ent sta!e.
0he varia:le glBatc4Vertices0n is availa:le onl, in the tessellation control and evaluation lan!ua!es. It
is an inte!er s1eci&,in! the nu":er o& vertices in the in1ut 1atch :ein! 1rocessed :, the shader. A sin!le
tessellation control or evaluation shader can read 1atches o& di&&erin! si=es, so the value o&
glBatc4Vertices0n "a, di&&er :etween 1atches.
0he out1ut varia:les gl5essLevel6uter<= and gl5essLevel0nner<= are availa:le onl, in the tessellation
control lan!ua!e. 0he values written to these varia:les are assi!ned to the corres1ondin! outer and inner
tessellation levels o& the out1ut 1atch. 0he, are used :, the tessellation 1ri"itive !enerator to control
1ri"itive tessellation and "a, :e read :, tessellation evaluation shaders.
0he varia:le gl5ess$oor* is availa:le onl, in the tessellation evaluation lan!ua!e. It s1eci&ies a three9
co"1onent :u.v.2; vector identi&,in! the 1osition o& the verte2 :ein! 1rocessed :, the shader relative to
the 1ri"itive :ein! tessellated. Its values will o:e, the 1ro1erties
gl",essMoord6x 44 16? X (16? X gl",essMoord6x) // two o!erations !erformed
gl",essMoord60 44 16? X (16? X gl",essMoord60) // two o!erations !erformed
gl",essMoord6/ 44 16? X (16? X gl",essMoord6/) // two o!erations !erformed
to aid in re1licatin! su:division co"1utations.
0he in1ut varia:les gl5essLevel6uter<= and gl5essLevel0nner<= are availa:le onl, in the tessellation
evaluation shader. I& a tessellation control shader is active, these varia:les are &illed with corres1ondin!
out1uts written :, the tessellation control shader. -therwise, the, are assi!ned with de&ault tessellation
levels s1eci&ied in section 11.#.%.% G0essellation Evaluation +hader In1utsH in the -1enGL Gra1hics
+,ste" +1eci&ication.
5ra!"ent shaders out1ut values to the -1enGL 1i1eline usin! declared out varia:les, the :uilt9in
varia:les glFragDe)t4 and gl'am)le!as,, unless the discard state"ent is e2ecuted.
0he &i2ed &unctionalit, co"1uted de1th &or a &ra!"ent "a, :e o:tained :, readin! glFrag$oor*.(.
descri:ed :elow.
@ritin! to glFragDe)t4 will esta:lish the de1th value &or the &ra!"ent :ein! 1rocessed. I& de1th
:u&&erin! is ena:led, and no shader writes glFragDe)t4, then the &i2ed &unction value &or de1th will :e
used as the &ra!"entOs de1th value. I& a shader staticall, assi!ns a value to glFragDe)t4, and there is an
e2ecution 1ath throu!h the shader that does not set glFragDe)t4, then the value o& the &ra!"entOs de1th
"a, :e unde&ined &or e2ecutions o& the shader that tae that 1ath. 0hat is, i& the set o& lined &ra!"ent
shaders staticall, contain a write to glFragDe)t4, then it is res1onsi:le &or alwa,s writin! it.
I& a shader e2ecutes the discard e,word, the &ra!"ent is discarded, and the values o& an, user9de&ined
&ra!"ent out1uts, glFragDe)t4, and gl'am)le!as, :eco"e irrelevant.
0he varia:le glFrag$oor* is availa:le as an in1ut varia:le &ro" within &ra!"ent shaders and it holds the
window relative coordinates B", /, (, 1K2C values &or the &ra!"ent. I& "ulti9sa"1lin!, this value can :e &or
an, location within the 1i2el, or one o& the &ra!"ent sa"1les. 0he use o& centroid does not &urther
restrict this value to :e inside the current 1ri"itive. 0his value is the result o& the &i2ed &unctionalit, that
inter1olates 1ri"itives a&ter verte2 1rocessin! to !enerate &ra!"ents. 0he ( co"1onent is the de1th value
that would :e used &or the &ra!"entOs de1th i& no shader contained an, writes to glFragDe)t4. 0his is
117
7 !ui%t+in #aria$%e"
use&ul &or invariance i& a shader conditionall, co"1utes glFragDe)t4 :ut otherwise wants the &i2ed
&unctionalit, &ra!"ent de1th.
5ra!"ent shaders have access to the in1ut :uilt9in varia:le glFrontFacing. whose value is true i& the
&ra!"ent :elon!s to a &ront9&acin! 1ri"itive. -ne use o& this is to e"ulate two9sided li!htin! :, selectin!
one o& two colors calculated :, a verte2 or !eo"etr, shader.
0he values in glBoint$oor* are two9di"ensional coordinates indicatin! where within a 1oint 1ri"itive
the current &ra!"ent is located, when 1oint s1rites are ena:led. 0he, ran!e &ro" ).) to 1.) across the
1oint. I& the current 1ri"itive is not a 1oint, or i& 1oint s1rites are not ena:led, then the values read &ro"
glBoint$oor* are unde&ined.
5or :oth the in1ut arra, gl'am)le!as,0n<= and the out1ut arra, gl'am)le!as,<=, :it % o& "as !
Bgl'am)le!as,0n<!= or gl'am)le!as,<!=C corres1onds to sa"1le 32A!?%. 0hese arra,s have
ceilBsD%#C ele"ents, where s is the "a2i"u" nu":er o& color sa"1les su11orted :, the i"1le"entation.
0he in1ut varia:le gl'am)le!as,0n indicates the set o& sa"1les covered :, the 1ri"itive !eneratin! the
&ra!"ent durin! "ultisa"1le rasteri=ation. It has a sa"1le :it set i& and onl, i& the sa"1le is considered
covered &or this &ra!"ent shader invocation.
0he out1ut arra, gl'am)le!as,<= sets the sa"1le "as &or the &ra!"ent :ein! 1rocessed. $overa!e &or
the current &ra!"ent will :eco"e the lo!ical A8D o& the covera!e "as and the out1ut gl'am)le!as,.
0his arra, "ust :e si=ed in the &ra!"ent shader either i"1licitl, or e21licitl, to :e the sa"e si=e descri:ed
a:ove. I& the &ra!"ent shader staticall, assi!ns a value to gl'am)le!as,, the sa"1le "as will :e
unde&ined &or an, arra, ele"ents o& an, &ra!"ent shader invocations that &ail to assi!n a value. I& a
shader does not staticall, assi!n a value to gl'am)le!as,, the sa"1le "as has no e&&ect on the
1rocessin! o& a &ra!"ent.
0he in1ut varia:le gl'am)le0D is &illed with the sa"1le nu":er o& the sa"1le currentl, :ein! 1rocessed.
0his varia:le is in the ran!e 0 to gl7um'am)les-1, where gl7um'am)les is the total nu":er o& sa"1les
in the &ra"e:u&&er, or 1 i& renderin! to a non9"ultisa"1le &ra"e:u&&er. An, static use o& this varia:le in a
&ra!"ent shader causes the entire shader to :e evaluated 1er9sa"1le.
0he in1ut varia:le gl'am)leBosition contains the 1osition o& the current sa"1le within the "ulti9sa"1le
draw :u&&er. 0he " and / co"1onents o& gl'am)leBosition contain the su:91i2el coordinate o& the current
sa"1le and will have values in the ran!e ).) to 1.). An, static use o& this varia:le in a &ra!"ent shader
causes the entire shader to :e evaluated 1er sa"1le.
0he glBerVerte" :loc can :e redeclared in a shader to e21licitl, indicate what su:set o& the &i2ed
1i1eline inter&ace will :e used. 0his is necessar, to esta:lish the inter&ace :etween "ulti1le 1ro!ra"s.
5or e2a"1le:
out gl"Her(ertex 5
vec< gl"Hosition; // will use gl"Hosition
float gl"Hoint*i/e; // will use gl"Hoint*i/e
vec< t; // error8 onl0 gl"Her(ertex members allowed
9; // no other members of gl"Her(ertex will be used
0his esta:lishes the out1ut inter&ace the shader will use with the su:se<uent 1i1eline sta!e. It "ust :e a
su:set o& the :uilt9in "e":ers o& glBerVerte".
118
7 !ui%t+in #aria$%e"
I& a :uilt9in inter&ace :loc is redeclared, it "ust a11ear in the shader :e&ore an, use o& an, "e":er
included in the :uilt9in declaration, or a co"1ile9ti"e error will result. It is also a co"1ile9ti"e error to
redeclare the :loc "ore than once or to redeclare a :uilt9in :loc and then use a "e":er &ro" that :uilt9
in :loc that was not included in the redeclaration. Also, i& a :uilt9in inter&ace :loc is redeclared, no
"e":er o& the :uilt9in declaration can :e redeclared outside the :loc redeclaration. I& "ulti1le shaders
usin! "e":ers o& a :uilt9in :loc :elon!in! to the sa"e inter&ace are lined to!ether in the sa"e
1ro!ra", the, "ust all redeclare the :uilt9in :loc in the sa"e wa,, as descri:ed in section (.%.6
GInter&ace BlocsH &or inter&ace :loc "atchin!, or a lin9ti"e error will result. It will also :e a lin9ti"e
error i& so"e shaders in a 1ro!ra" redeclare a s1eci&ic :uilt9in inter&ace :loc while another shader in that
1ro!ra" does not redeclare that inter&ace :loc ,et still uses a "e":er o& that inter&ace :loc. I& a :uilt9
in :loc inter&ace is &or"ed across shaders in di&&erent 1ro!ra"s, the shaders "ust all redeclare the :uilt9
in :loc in the sa"e wa, Bas descri:ed &or a sin!le 1ro!ra"C, or the values 1assed alon! the inter&ace are
unde&ined.
.1.1 Compatibilit& -rofile /uilt7In Language !ariables
@hen usin! the co"1ati:ilit, 1ro&ile, the GL can 1rovide &i2ed &unctionalit, :ehavior &or the verte2 and
&ra!"ent 1ro!ra""a:le 1i1eline sta!es. 5or e2a"1le, "i2in! a &i2ed &unctionalit, verte2 sta!e with a
1ro!ra""a:le &ra!"ent sta!e.
0he &ollowin! :uilt9in verte2, tessellation control, tessellation evaluation, and !eo"etr, out1ut varia:les
are availa:le to s1eci&, in1uts &or the su:se<uent 1ro!ra""a:le shader sta!e or the &i2ed &unctionalit,
&ra!"ent sta!e. A 1articular one should :e written to i& an, &unctionalit, in a corres1ondin! &ra!"ent
shader or &i2ed 1i1eline uses it or state derived &ro" it. -therwise, :ehavior is unde&ined. 0he &ollowin!
"e":ers are added to the out1ut glBerVerte" :loc in these lan!ua!es:
out gl"Her(ertex 5 // !art of the gl"Her(ertex block described in C61
// in addition to other gl"Her(ertex members666
vec< gl"Mli!(ertex;
vec< gl"'rontMolor;
vec< gl":ackMolor;
vec< gl"'ront*econdar0Molor;
vec< gl":ack*econdar0Molor;
vec< gl",exMoordIJ;
float gl"'og'ragMoord;
9;
0he out1ut varia:le gl$li)Verte" 1rovides a 1lace &or verte2 and !eo"etr, shaders to write the
coordinate to :e used with the user cli11in! 1lanes. @ritin! to gl$li)Distance is the 1re&erred "ethod
&or user cli11in!. It is a co"1ile9ti"e or lin9ti"e error &or the set o& shaders &or"in! a 1ro!ra" to
staticall, read or write :oth gl$li)Verte" and gl$li)Distance. I& neither gl$li)Verte" nor
gl$li)Distance is written, their values are unde&ined and an, cli11in! a!ainst user cli1 1lanes is also
unde&ined.
+i"ilarl, to what was 1reviousl, descri:ed &or the core 1ro&ile, the glBerVerte" :loc can :e redeclared
in a shader to e21licitl, include these additional "e":ers. 5or e2a"1le:
119
7 !ui%t+in #aria$%e"
out gl"Her(ertex 5
vec< gl"Hosition; // will use gl"Hosition
vec< gl"'rontMolor; // will consume gl"color in the fragment shader
vec< gl":ackMolor;
vec< gl",exMoordI>J; // > elements of gl",exMoord will be used
9; // no other as!ects of the fixed interface will be used
0he user "ust ensure the cli1 verte2 and user cli11in! 1lanes are de&ined in the sa"e coordinate s1ace.
7ser cli1 1lanes wor 1ro1erl, onl, under linear trans&or". It is unde&ined what ha11ens under non9
linear trans&or".
0he out1ut varia:les glFront$olor, glFront'econ*ar/$olor, gl%ac,$olor, and gl%ac,'econ*ar/$olor
assi!n 1ri"ar, and secondar, colors &or &ront and :ac &aces o& 1ri"itives containin! the verte2 :ein!
1rocessed. 0he out1ut varia:le gl5e"$oor* assi!ns te2ture coordinates &or the verte2 :ein! 1rocessed.
5or glFogFrag$oor*, the value written will :e used as the GcH value in section 1*.( G5o!H o& the
co"1ati:ilit, 1ro&ile o& the -1enGL Gra1hics +,ste" +1eci&ication, :, the &i2ed &unctionalit, 1i1eline.
5or e2a"1le, i& the =9coordinate o& the &ra!"ent in e,e s1ace is desired as GcH, then thatFs what the verte2
shader e2ecuta:le should write into glFogFrag$oor*.
As with all arra,s, indices used to su:scri1t gl5e"$oor* "ust either :e an inte!ral constant e21ressions,
or this arra, "ust :e re9declared :, the shader with a si=e. 0he si=e can :e at "ost
gl!a"5e"ture$oor*s. 7sin! inde2es close to ) "a, aid the i"1le"entation in 1reservin! var,in!
resources. 0he redeclaration o& gl5e"$oor* can also :e done at !lo:al sco1e as, &or e2a"1le:
in vec< gl",exMoordI>J;
out vec< gl",exMoordI<J;
B0his treat"ent is a s1ecial case &or gl5e"$oor*<=, not a !eneral "ethod &or redeclarin! "e":ers o&
:locs.C It is a co"1ile9ti"e error to redeclare gl5e"$oor*<= at !lo:al sco1e i& there is a redeclaration
o& the corres1ondin! :uilt9in :locJ onl, one &or" o& redeclaration is allowed within a shader Band hence
within a sta!e, as :loc redeclarations "ust "atch across all shaders usin! itC.
In the tessellation control, evaluation, and !eo"etr, shaders, the out1uts o& the 1revious sta!e descri:ed
a:ove are also availa:le in the in1ut glBerVerte" :loc in these lan!ua!es.
in gl"Her(ertex 5 // !art of the gl"Her(ertex block described in C61
// in addition to other gl"Her(ertex members666
vec< gl"Mli!(ertex;
vec< gl"'rontMolor;
vec< gl":ackMolor;
vec< gl"'ront*econdar0Molor;
vec< gl":ack*econdar0Molor;
vec< gl",exMoordIJ;
float gl"'og'ragMoord;
9 gl"inIJ;
0hese can :e redeclared to esta:lish an e21licit 1i1eline inter&ace, the sa"e wa, as descri:ed a:ove &or
the out1ut :loc glBerVerte", and the in1ut redeclaration "ust "atch the out1ut redeclaration o& the
1revious sta!e. /owever, when a :uilt9in inter&ace :loc with an instance na"e is redeclared Be.!., glinC,
12
7 !ui%t+in #aria$%e"
the instance na"e "ust :e included in the redeclaration. It is a co"1ile9ti"e error to not include the :uilt9
in instance na"e or to chan!e its na"e. 5or e2a"1le,
in gl"Her(ertex 5
vec< gl"Mli!(ertex;
vec< gl"'rontMolor;
9 gl"inIJ; // must be !resent and must be gl"inIJ
0reat"ent o& gl5e"$oor*<= redeclaration is also identical to that descri:ed &or the out1ut :loc
gl5e"$oor*<= redeclaration.
0he &ollowin! &ra!"ent in1ut :loc is also availa:le in a &ra!"ent shader when usin! the co"1ati:ilit,
1ro&ile:
in gl"Her'ragment 5
in float gl"'og'ragMoord;
in vec< gl",exMoordIJ;
in vec< gl"Molor;
in vec< gl"*econdar0Molor;
9;
0he values in gl$olor and gl'econ*ar/$olor will :e derived auto"aticall, :, the s,ste" &ro"
glFront$olor. gl%ac,$olor. glFront'econ*ar/$olor. and gl%ac,'econ*ar/$olor :ased on which
&ace is visi:le in the 1ri"itive 1roducin! the &ra!"ent. I& &i2ed &unctionalit, is used &or verte2 1rocessin!,
then glFogFrag$oor* will either :e the =9coordinate o& the &ra!"ent in e,e s1ace, or the inter1olation o&
the &o! coordinate, as descri:ed in section 1*.( G5o!H o& the co"1ati:ilit, 1ro&ile o& the -1enGL
Gra1hics +,ste" +1eci&ication. 0he gl5e"$oor*<= values are the inter1olated gl5e"$oor*<= values
&ro" a verte2 shader or the te2ture coordinates o& an, &i2ed 1i1eline :ased verte2 &unctionalit,.
Indices to the &ra!"ent shader gl5e"$oor* arra, are as descri:ed a:ove in the verte2 shader te2t.
As descri:ed a:ove &or the in1ut and out1ut glBerVerte" :locs, the glBerFragment :loc can :e
redeclared to create an e21licit inter&ace to another 1ro!ra". @hen "atchin! these inter&aces :etween
se1arate 1ro!ra"s, "e":ers in the glBerVerte" out1ut :loc "ust :e declared i& and onl, i& the
corres1ondin! &ra!"ent9shader "e":ers !enerated &ro" the" are 1resent in the glBerFragment in1ut
:loc. 0hese "atches are descri:ed in detail in section '.(.1 G+hader Inter&ace ;atchin!H o& the -1enGL
Gra1hics +,ste" +1eci&ication. I& the, donFt "atch within a 1ro!ra", a lin9ti"e error will result. I& the
"is"atch is :etween two 1ro!ra"s, values 1assed :etween 1ro!ra"s are unde&ined. 7nlie with all other
:loc "atchin!, the order o& declaration within glBerFragment does not have to "atch across shaders
and does not have to corres1ond with order o& declaration in a "atchin! glBerVerte" redeclaration.
0he &ollowin! &ra!"ent out1ut varia:les are availa:le in a &ra!"ent shader when usin! the co"1ati:ilit,
1ro&ile:
out vec< gl"'ragMolor;
out vec< gl"'rag-ataIgl"Gax-raw:uffersJ;
@ritin! to glFrag$olor s1eci&ies the &ra!"ent color that will :e used :, the su:se<uent &i2ed
&unctionalit, 1i1eline. I& su:se<uent &i2ed &unctionalit, consu"es &ra!"ent color and an e2ecution o& the
&ra!"ent shader e2ecuta:le does not write a value to glFrag$olor then the &ra!"ent color consu"ed is
unde&ined.
121
7 !ui%t+in #aria$%e"
0he varia:le glFragData is an arra,. @ritin! to glFragData<n= s1eci&ies the &ra!"ent data that will :e
used :, the su:se<uent &i2ed &unctionalit, 1i1eline &or data n. I& su:se<uent &i2ed &unctionalit, consu"es
&ra!"ent data and an e2ecution o& a &ra!"ent shader e2ecuta:le does not write a value to it, then the
&ra!"ent data consu"ed is unde&ined.
I& a shader staticall, assi!ns a value to glFrag$olor, it "a, not assi!n a value to an, ele"ent o&
glFragData. I& a shader staticall, writes a value to an, ele"ent o& glFragData, it "a, not assi!n a
value to glFrag$olor. 0hat is, a shader "a, assi!n values to either glFrag$olor or glFragData, :ut
not :oth. ;ulti1le shaders lined to!ether "ust also consistentl, write Eust one o& these varia:les.
+i"ilarl,, i& user9declared out1ut varia:les are in use Bstaticall, assi!ned toC, then the :uilt9in varia:les
glFrag$olor and glFragData "a, not :e assi!ned to. 0hese incorrect usa!es all !enerate co"1ile9ti"e
or lin9ti"e errors.
I& a shader e2ecutes the discard e,word, the &ra!"ent is discarded, and the values o& glFragDe)t4 and
glFrag$olor :eco"e irrelevant.
.2 Compatibilit& -rofile !erte, Shader /uilt7In Inputs
0he &ollowin! 1redeclared in1ut na"es can :e used &ro" within a verte2 shader to access the current
values o& -1enGL state when usin! the co"1ati:ilit, 1ro&ile.
in vec< gl"Molor;
in vec< gl"*econdar0Molor;
in vec> gl"%ormal;
in vec< gl"(ertex;
in vec< gl"Gulti,exMoord?;
in vec< gl"Gulti,exMoord1;
in vec< gl"Gulti,exMoord=;
in vec< gl"Gulti,exMoord>;
in vec< gl"Gulti,exMoord<;
in vec< gl"Gulti,exMoordE;
in vec< gl"Gulti,exMoordB;
in vec< gl"Gulti,exMoordC;
in float gl"'ogMoord;
122
7 !ui%t+in #aria$%e"
.# /uilt7In Constants
0he &ollowin! :uilt9in constants are 1rovided to all shaders. 0he actual values used are i"1le"entation
de1endent, :ut "ust :e at least the value shown.
//
// $m!lementation3de!endent constants6 ,he exam!le values below
// are the minimum values allowed for these maximums6
//
const ivec> gl"GaxMom!uteTork.rou!Mount 4 5 BEE>E8 BEE>E8 BEE>E 9;
const ivec> gl"GaxMom!uteTork.rou!*i/e 4 5 1?=<8 1?=<8 B< 9;
const int gl"GaxMom!ute@niformMom!onents 4 1?=<;
const int gl"GaxMom!ute,exture$mage@nits 4 1B;
const int gl"GaxMom!ute$mage@niforms 4 F;
const int gl"GaxMom!uteDtomicMounters 4 F;
const int gl"GaxMom!uteDtomicMounter:uffers 4 1;
const int gl"Gax(ertexDttribs 4 1B;
const int gl"Gax(ertex@niformMom!onents 4 1?=<;
const int gl"Gax(ar0ingMom!onents 4 B?;
const int gl"Gax(ertex+ut!utMom!onents 4 B<;
const int gl"Gax.eometr0$n!utMom!onents 4 B<;
const int gl"Gax.eometr0+ut!utMom!onents 4 1=F;
const int gl"Gax'ragment$n!utMom!onents 4 1=F;
const int gl"Gax(ertex,exture$mage@nits 4 1B;
const int gl"GaxMombined,exture$mage@nits 4 F?;
const int gl"Gax,exture$mage@nits 4 1B;
const int gl"Gax$mage@nits 4 F;
const int gl"GaxMombined$mage@nitsDnd'ragment+ut!uts 4 F;
const int gl"Gax$mage*am!les 4 ?;
const int gl"Gax(ertex$mage@niforms 4 ?;
const int gl"Gax,essMontrol$mage@niforms 4 ?;
const int gl"Gax,ess&valuation$mage@niforms 4 ?;
const int gl"Gax.eometr0$mage@niforms 4 ?;
const int gl"Gax'ragment$mage@niforms 4 F;
const int gl"GaxMombined$mage@niforms 4 F;
const int gl"Gax'ragment@niformMom!onents 4 1?=<;
const int gl"Gax-raw:uffers 4 F;
const int gl"GaxMli!-istances 4 F;
const int gl"Gax.eometr0,exture$mage@nits 4 1B;
const int gl"Gax.eometr0+ut!ut(ertices 4 =EB;
const int gl"Gax.eometr0,otal+ut!utMom!onents 4 1?=<;
const int gl"Gax.eometr0@niformMom!onents 4 1?=<;
const int gl"Gax.eometr0(ar0ingMom!onents 4 B<;
123
7 !ui%t+in #aria$%e"
const int gl"Gax,essMontrol$n!utMom!onents 4 1=F;
const int gl"Gax,essMontrol+ut!utMom!onents 4 1=F;
const int gl"Gax,essMontrol,exture$mage@nits 4 1B;
const int gl"Gax,essMontrol@niformMom!onents 4 1?=<;
const int gl"Gax,essMontrol,otal+ut!utMom!onents 4 <?AB;
const int gl"Gax,ess&valuation$n!utMom!onents 4 1=F;
const int gl"Gax,ess&valuation+ut!utMom!onents 4 1=F;
const int gl"Gax,ess&valuation,exture$mage@nits 4 1B;
const int gl"Gax,ess&valuation@niformMom!onents 4 1?=<;
const int gl"Gax,essHatchMom!onents 4 1=?;
const int gl"GaxHatch(ertices 4 >=;
const int gl"Gax,ess.en#evel 4 B<;
const int gl"Gax(iew!orts 4 1B;
const int gl"Gax(ertex@niform(ectors 4 =EB;
const int gl"Gax'ragment@niform(ectors 4 =EB;
const int gl"Gax(ar0ing(ectors 4 1E;
const int gl"Gax(ertexDtomicMounters 4 ?;
const int gl"Gax,essMontrolDtomicMounters 4 ?;
const int gl"Gax,ess&valuationDtomicMounters 4 ?;
const int gl"Gax.eometr0DtomicMounters 4 ?;
const int gl"Gax'ragmentDtomicMounters 4 F;
const int gl"GaxMombinedDtomicMounters 4 F;
const int gl"GaxDtomicMounter:indings 4 1;
const int gl"Gax(ertexDtomicMounter:uffers 4 ?;
const int gl"Gax,essMontrolDtomicMounter:uffers 4 ?;
const int gl"Gax,ess&valuationDtomicMounter:uffers 4 ?;
const int gl"Gax.eometr0DtomicMounter:uffers 4 ?;
const int gl"Gax'ragmentDtomicMounter:uffers 4 1;
const int gl"GaxMombinedDtomicMounter:uffers 4 1;
const int gl"GaxDtomicMounter:uffer*i/e 4 >=1B>F<;
const int gl"GinHrogram,exel+ffset 4 3F;
const int gl"GaxHrogram,exel+ffset 4 C;
0he constant gl!a"Var/ingFloats is re"oved in the core 1ro&ile, use gl!a"Var/ing$om)onents
instead.
.#.1 Compatibilit& -rofile /uilt7In Constants
const int gl"Gax,exture@nits 4 =;
const int gl"Gax,extureMoords 4 F;
const int gl"GaxMli!Hlanes 4 F;
const int gl"Gax(ar0ing'loats 4 B?;
124
7 !ui%t+in #aria$%e"
." /uilt7In 5niform State
As an aid to accessin! -1enGL 1rocessin! state, the &ollowin! uni&or" varia:les are :uilt into the
-1enGL +hadin! Lan!ua!e.
//
// -e!th range in window coordinates8
// section 1>6B61 Montrolling the (iew!ort in the
// +!en.# .ra!hics *0stem *!ecification6
//
// %ote2 -e!th3range state is onl0 for view!ort ?6
//
struct gl"-e!th)angeHarameters 5
float near; // n
float far; // f
float diff; // f 3 n
9;
uniform gl"-e!th)angeHarameters gl"-e!th)ange;
uniform int gl"%um*am!les;
.".1 Compatibilit& -rofile State
0hese varia:les are 1resent onl, in the co"1ati:ilit, 1ro&ile. 0he, are not availa:le to co"1ute shaders,
:ut are availa:le to all other shaders.
//
// com!atibilit0 !rofile onl0
//
uniform mat< gl"Godel(iewGatrix;
uniform mat< gl"Hro;ectionGatrix;
uniform mat< gl"Godel(iewHro;ectionGatrix;
uniform mat< gl",extureGatrixIgl"Gax,extureMoordsJ;
//
// com!atibilit0 !rofile onl0
//
uniform mat> gl"%ormalGatrix; // trans!ose of the inverse of the
// u!!er leftmost >x> of gl"Godel(iewGatrix
uniform mat< gl"Godel(iewGatrix$nverse;
uniform mat< gl"Hro;ectionGatrix$nverse;
uniform mat< gl"Godel(iewHro;ectionGatrix$nverse;
uniform mat< gl",extureGatrix$nverseIgl"Gax,extureMoordsJ;
uniform mat< gl"Godel(iewGatrix,rans!ose;
uniform mat< gl"Hro;ectionGatrix,rans!ose;
uniform mat< gl"Godel(iewHro;ectionGatrix,rans!ose;
uniform mat< gl",extureGatrix,rans!oseIgl"Gax,extureMoordsJ;
125
7 !ui%t+in #aria$%e"
uniform mat< gl"Godel(iewGatrix$nverse,rans!ose;
uniform mat< gl"Hro;ectionGatrix$nverse,rans!ose;
uniform mat< gl"Godel(iewHro;ectionGatrix$nverse,rans!ose;
uniform mat< gl",extureGatrix$nverse,rans!oseIgl"Gax,extureMoordsJ;
//
// com!atibilit0 !rofile onl0
//
uniform float gl"%ormal*cale;
//
// com!atibilit0 !rofile onl0
//
uniform vec< gl"Mli!HlaneIgl"GaxMli!HlanesJ;
//
// com!atibilit0 !rofile onl0
//
struct gl"HointHarameters 5
float si/e;
float si/eGin;
float si/eGax;
float fade,hreshold*i/e;
float distanceMonstantDttenuation;
float distance#inearDttenuation;
float distanceYuadraticDttenuation;
9;

uniform gl"HointHarameters gl"Hoint;
//
// com!atibilit0 !rofile onl0
//
struct gl"GaterialHarameters 5
vec< emission; // &cm
vec< ambient; // Dcm
vec< diffuse; // -cm
vec< s!ecular; // *cm
float shininess; // *rm
9;
uniform gl"GaterialHarameters gl"'rontGaterial;
uniform gl"GaterialHarameters gl":ackGaterial;
126
7 !ui%t+in #aria$%e"
//
// com!atibilit0 !rofile onl0
//
struct gl"#ight*ourceHarameters 5
vec< ambient; // Dcli
vec< diffuse; // -cli
vec< s!ecular; // *cli
vec< !osition; // H!li
vec< half(ector; // -erived2 Pi
vec> s!ot-irection; // *dli
float s!ot&x!onent; // *rli
float s!otMutoff; // Mrli
// (range2 I?6?8A?6?J8 1F?6?)
float s!otMosMutoff; // -erived2 cos(Mrli)
// (range2 I16?8?6?J8316?)
float constantDttenuation; // S?
float linearDttenuation; // S1
float quadraticDttenuation;// S=
9;
uniform gl"#ight*ourceHarameters gl"#ight*ourceIgl"Gax#ightsJ;
struct gl"#ightGodelHarameters 5
vec< ambient; // Dcs
9;
uniform gl"#ightGodelHarameters gl"#ightGodel;
//
// com!atibilit0 !rofile onl0
//
// -erived state from !roducts of light and material6
//
struct gl"#ightGodelHroducts 5
vec< sceneMolor; // -erived6 &cm K Dcm L Dcs
9;
uniform gl"#ightGodelHroducts gl"'ront#ightGodelHroduct;
uniform gl"#ightGodelHroducts gl":ack#ightGodelHroduct;
struct gl"#ightHroducts 5
vec< ambient; // Dcm L Dcli
vec< diffuse; // -cm L -cli
vec< s!ecular; // *cm L *cli
9;
uniform gl"#ightHroducts gl"'ront#ightHroductIgl"Gax#ightsJ;
uniform gl"#ightHroducts gl":ack#ightHroductIgl"Gax#ightsJ;
127
7 !ui%t+in #aria$%e"
//
// com!atibilit0 !rofile onl0
//
uniform vec< gl",exture&nvMolorIgl"Gax,exture@nitsJ;
uniform vec< gl"&0eHlane*Igl"Gax,extureMoordsJ;
uniform vec< gl"&0eHlane,Igl"Gax,extureMoordsJ;
uniform vec< gl"&0eHlane)Igl"Gax,extureMoordsJ;
uniform vec< gl"&0eHlaneYIgl"Gax,extureMoordsJ;
uniform vec< gl"+b;ectHlane*Igl"Gax,extureMoordsJ;
uniform vec< gl"+b;ectHlane,Igl"Gax,extureMoordsJ;
uniform vec< gl"+b;ectHlane)Igl"Gax,extureMoordsJ;
uniform vec< gl"+b;ectHlaneYIgl"Gax,extureMoordsJ;
//
// com!atibilit0 !rofile onl0
//
struct gl"'ogHarameters 5
vec< color;
float densit0;
float start;
float end;
float scale; // -erived2 16? / (end 3 start)
9;

uniform gl"'ogHarameters gl"'og;
128
4 /uilt7in .unctions
0he -1enGL +hadin! Lan!ua!e de&ines an assort"ent o& :uilt9in convenience &unctions &or scalar and
vector o1erations. ;an, o& these :uilt9in &unctions can :e used in "ore than one t,1e o& shader, :ut so"e
are intended to 1rovide a direct "a11in! to hardware and so are availa:le onl, &or a s1eci&ic t,1e o&
shader.
0he :uilt9in &unctions :asicall, &all into three cate!ories:
0he, e21ose so"e necessar, hardware &unctionalit, in a convenient wa, such as accessin! a te2ture
"a1. 0here is no wa, in the lan!ua!e &or these &unctions to :e e"ulated :, a shader.
0he, re1resent a trivial o1eration Bcla"1, "i2, etc.C that is ver, si"1le &or the user to write, :ut the,
are ver, co""on and "a, have direct hardware su11ort. It is a ver, hard 1ro:le" &or the co"1iler to
"a1 e21ressions to co"1le2 asse":ler instructions.
0he, re1resent an o1eration !ra1hics hardware is liel, to accelerate at so"e 1oint. 0he tri!ono"etr,
&unctions &all into this cate!or,.
;an, o& the &unctions are si"ilar to the sa"e na"ed ones in co""on $ li:raries, :ut the, su11ort vector
in1ut as well as the "ore traditional scalar in1ut.
A11lications should :e encoura!ed to use the :uilt9in &unctions rather than do the e<uivalent co"1utations
in their own shader code since the :uilt9in &unctions are assu"ed to :e o1ti"al Be.!., 1erha1s su11orted
directl, in hardwareC.
7ser code can re1lace :uilt9in &unctions with their own i& the, choose, :, si"1l, re9declarin! and de&inin!
the sa"e na"e and ar!u"ent list. Because :uilt9in &unctions are in a "ore outer sco1e than user :uilt9in
&unctions, doin! this will hide all :uilt9in &unctions with the sa"e na"e as the re9declared &unction.
@hen the :uilt9in &unctions are s1eci&ied :elow, where the in1ut ar!u"ents Band corres1ondin! out1utC
can :e float, vecG, vecF, or vecE, gen5/)e is used as the ar!u"ent. @here the in1ut ar!u"ents Band
corres1ondin! out1utC can :e int, ivecG, ivecF, or ivecE, gen05/)e is used as the ar!u"ent. @here the
in1ut ar!u"ents Band corres1ondin! out1utC can :e uint, uvecG, uvecF, or uvecE, gen15/)e is used as the
ar!u"ent. @here the in1ut ar!u"ents Bor corres1ondin! out1utC can :e bool, bvecG, bvecF, or bvecE,
gen%5/)e is used as the ar!u"ent. @here the in1ut ar!u"ents Band corres1ondin! out1utC can :e double,
dvecG, dvecF, dvecE, genD5/)e is used as the ar!u"ent. 5or an, s1eci&ic use o& a &unction, the actual
t,1es su:stituted &or gen5/)e, gen05/)e, gen15/)e. or gen%5/)e have to have the sa"e nu":er o&
co"1onents &or all ar!u"ents and &or the return t,1e. +i"ilarl,, mat is used &or an, "atri2 :asic t,1e
with sin!le91recision co"1onents and *mat is used &or an, "atri2 :asic t,1e with dou:le91recision
co"1onents.
129
8 !ui%t+in ,unction"
4.1 0ngle and Trigonometr& .unctions
5unction 1ara"eters s1eci&ied as angle are assu"ed to :e in units o& radians. In no case will an, o& these
&unctions result in a divide :, =ero error. I& the divisor o& a ratio is ), then results will :e unde&ined.
0hese all o1erate co"1onent9wise. 0he descri1tion is 1er co"1onent.
S&nta, +escription
!en0,1e radians B!en0,1e *egreesC
$onverts *egrees to radians, i.e.,

14)
*egrees
!en0,1e degrees B!en0,1e ra*iansC
$onverts ra*ians to de!rees, i.e.,
14)

ra*ians
!en0,1e sin B!en0,1e angleC 0he standard tri!ono"etric sine &unction.
!en0,1e cos B!en0,1e angleC 0he standard tri!ono"etric cosine &unction.
!en0,1e tan B!en0,1e angleC 0he standard tri!ono"etric tan!ent.
!en0,1e asin B!en0,1e "C Arc sine. Returns an an!le whose sine is ". 0he ran!e
o& values returned :, this &unction is
[

#
.

#
]
Results are unde&ined i& "1.
!en0,1e acos B!en0,1e "C Arc cosine. Returns an an!le whose cosine is ". 0he
ran!e o& values returned :, this &unction is L), M.
Results are unde&ined i& "1.
!en0,1e atan B!en0,1e /, !en0,1e "C Arc tan!ent. Returns an an!le whose tan!ent is /K". 0he
si!ns o& " and / are used to deter"ine what <uadrant the
an!le is in. 0he ran!e o& values returned :, this
&unction is [. ] . Results are unde&ined i& " and
/ are :oth ).
!en0,1e atan B!en0,1e /over"C Arc tan!ent. Returns an an!le whose tan!ent is
/over". 0he ran!e o& values returned :, this &unction
is
[

#
.

#
]
.
13
8 !ui%t+in ,unction"
S&nta, +escription
!en0,1e sin" B!en0,1e "C Returns the h,1er:olic sine &unction
e
"
e
"
#
!en0,1e cos" B!en0,1e "C Returns the h,1er:olic cosine &unction
e
"
e
"
#
!en0,1e tan" B!en0,1e "C Returns the h,1er:olic tan!ent &unction
sinh "
cosh "
!en0,1e asin" B!en0,1e "C Arc h,1er:olic sineJ returns the inverse o& sin".
!en0,1e acos" B!en0,1e "C Arc h,1er:olic cosineJ returns the non9ne!ative inverse
o& cos". Results are unde&ined i& " T 1.
!en0,1e atan" B!en0,1e "C Arc h,1er:olic tan!entJ returns the inverse o& tan".
Results are unde&ined i& "1.
131
8 !ui%t+in ,unction"
4.2 (,ponential .unctions
0hese all o1erate co"1onent9wise. 0he descri1tion is 1er co"1onent.
S&nta, +escription
!en0,1e pow B!en0,1e ", !en0,1e /C Returns " raised to the / 1ower, i.e., "
/
Results are unde&ined i& " L 0.
Results are unde&ined i& " C 0 and / LC 0.
!en0,1e exp B!en0,1e "C Returns the natural e21onentiation o& ", i.e., e
"
.
!en0,1e log B!en0,1e "C Returns the natural lo!arith" o& ". i.e., returns the value
/ which satis&ies the e<uation " V e
/
.
Results are unde&ined i& " LC 0.
!en0,1e expG B!en0,1e "C Returns # raised to the " 1ower, i.e., #
"
!en0,1e logG B!en0,1e "C Returns the :ase # lo!arith" o& ". i.e., returns the value
/ which satis&ies the e<uation "=#
/
Results are unde&ined i& " LC 0.
!en0,1e sMrt B!en0,1e "C
!enD0,1e sMrt B!enD0,1e "C
Returns
" .
Results are unde&ined i& " L 0.
!en0,1e inversesMrt B!en0,1e "C
!enD0,1e inversesMrt B!enD0,1e "C
Returns
1
"
.
Results are unde&ined i& " LC 0.
132
8 !ui%t+in ,unction"
4.# Common .unctions
0hese all o1erate co"1onent9wise. 0he descri1tion is 1er co"1onent.
S&nta, +escription
!en0,1e abs B!en0,1e "C
!enI0,1e abs B!enI0,1e "C
!enD0,1e abs B!enD0,1e "C
Returns " i& " UV )J otherwise it returns ^".
!en0,1e sign B!en0,1e "C
!enI0,1e sign B!enI0,1e "C
!enD0,1e sign B!enD0,1e "C
Returns 1.) i& " U ), ).) i& " V ), or ^1.) i& " T ).
!en0,1e floor B!en0,1e "C
!enD0,1e floor B!enD0,1e "C
Returns a value e<ual to the nearest inte!er that is less
than or e<ual to ".
!en0,1e trunc B!en0,1e "C
!enD0,1e trunc B!enD0,1e "C
Returns a value e<ual to the nearest inte!er to " whose
a:solute value is not lar!er than the a:solute value o& ".
!en0,1e round B!en0,1e "C
!enD0,1e round B!enD0,1e "C
Returns a value e<ual to the nearest inte!er to ". 0he
&raction ).. will round in a direction chosen :, the
i"1le"entation, 1resu"a:l, the direction that is &astest.
0his includes the 1ossi:ilit, that roundB"C returns the
sa"e value as round&venB"C &or all values o& ".
!en0,1e round&ven B!en0,1e "C
!enD0,1e round&ven B!enD0,1e "C
Returns a value e<ual to the nearest inte!er to ". A
&ractional 1art o& ).. will round toward the nearest even
inte!er. BBoth %.. and (.. &or 2 will return (.).C
!en0,1e ceil B!en0,1e "C
!enD0,1e ceil B!enD0,1e "C
Returns a value e<ual to the nearest inte!er that is
!reater than or e<ual to ".
!en0,1e fract B!en0,1e "C
!enD0,1e fract B!enD0,1e "C
Returns " ^ floor B"C.
!en0,1e mod B!en0,1e ", &loat /C
!en0,1e mod B!en0,1e ", !en0,1e /C
!enD0,1e mod B!enD0,1e ", dou:le /C
!enD0,1e mod B!enD0,1e ", !enD0,1e /C
;odulus. Returns " ^ / floor B"D/C.
133
8 !ui%t+in ,unction"
S&nta, +escription
!en0,1e modf B!en0,1e ", out !en0,1e iC
!enD0,1e modf B!enD0,1e ",
out !enD0,1e iC
Returns the &ractional 1art o& " and sets i to the inte!er
1art Bas a whole nu":er &loatin!91oint valueC. Both the
return value and the out1ut 1ara"eter will have the sa"e
si!n as ".
!en0,1e min B!en0,1e ", !en0,1e /C
!en0,1e min B!en0,1e ", &loat /C
!enD0,1e min B!enD0,1e ", !enD0,1e /C
!enD0,1e min B!enD0,1e ", dou:le /C
!enI0,1e min B!enI0,1e ", !enI0,1e /C
!enI0,1e min B!enI0,1e ", int /C
!en70,1e min B!en70,1e ", !en70,1e /C
!en70,1e min B!en70,1e ", uint /C
Returns / i& / T "9 otherwise it returns ".
!en0,1e max B!en0,1e ", !en0,1e /C
!en0,1e max B!en0,1e ", &loat /C
!enD0,1e max B!enD0,1e ", !enD0,1e /C
!enD0,1e max B!enD0,1e ", dou:le /C
!enI0,1e max B!enI0,1e ", !enI0,1e /C
!enI0,1e max B!enI0,1e ", int /C
!en70,1e max B!en70,1e ", !en70,1e /C
!en70,1e max B!en70,1e ", uint /C
Returns / i& " T /9 otherwise it returns ".
134
8 !ui%t+in ,unction"
S&nta, +escription
!en0,1e clamp B!en0,1e ",
!en0,1e minVal,
!en0,1e ma"ValC
!en0,1e clamp B!en0,1e ",
&loat minVal,
&loat ma"ValC
!enD0,1e clamp B!enD0,1e ",
!enD0,1e minVal,
!enD0,1e ma"ValC
!enD0,1e clamp B!enD0,1e ",
dou:le minVal,
dou:le ma"ValC
!enI0,1e clamp B!enI0,1e ",
!enI0,1e minVal,
!enI0,1e ma"ValC
!enI0,1e clamp B!enI0,1e ",
int minVal,
int ma"ValC
!en70,1e clamp B!en70,1e ",
!en70,1e minVal,
!en70,1e ma"ValC
!en70,1e clamp B!en70,1e ",
uint minVal,
uint ma"ValC
Returns min Bmax B", minValC, ma"ValC.
Results are unde&ined i& minVal U ma"Val.
!en0,1e mix B!en0,1e ",
!en0,1e /,
!en0,1e aC
!en0,1e mix B!en0,1e ",
!en0,1e /,
&loat aC
!enD0,1e mix B!enD0,1e ",
!enD0,1e /,
!enD0,1e aC
!enD0,1e mix B!enD0,1e ",
!enD0,1e /,
dou:le aC
Returns the linear :lend o& " and /. i.e.,
"(1a)+/a
135
8 !ui%t+in ,unction"
S&nta, +escription
!en0,1e mix B!en0,1e ",
!en0,1e ,,
!enB0,1e aC
!enD0,1e mix B!enD0,1e ",
!enD0,1e ,,
!enB0,1e aC
+elects which vector each returned co"1onent co"es
&ro". 5or a co"1onent o& a that is false, the
corres1ondin! co"1onent o& " is returned. 5or a
co"1onent o& a that is true, the corres1ondin!
co"1onent o& / is returned. $o"1onents o& " and / that
are not selected are allowed to :e invalid &loatin!91oint
values and will have no e&&ect on the results. 0hus, this
1rovides di&&erent &unctionalit, than, &or e2a"1le,
!en0,1e mixB!en0,1e ", !en0,1e /, !en0,1eBaCC
where a is a Boolean vector.
!en0,1e step B!en0,1e e*ge, !en0,1e "C
!en0,1e step B&loat e*ge, !en0,1e "C
!enD0,1e step B!enD0,1e e*ge,
!enD0,1e "C
!enD0,1e step Bdou:le e*ge, !enD0,1e "C
Returns ).) i& " T e*ge9 otherwise it returns 1.).
!en0,1e smoot"step B!en0,1e e*ge0,
!en0,1e e*ge1,
!en0,1e "C
!en0,1e smoot"step B&loat e*ge0,
&loat e*ge1,
!en0,1e "C
!enD0,1e smoot"step B!enD0,1e e*ge0,
!enD0,1e e*ge1,
!enD0,1e "C
!enD0,1e smoot"step Bdou:le e*ge0,
dou:le e*ge1,
!enD0,1e "C
Returns ).) i& " TV e*ge0 and 1.) i& " UV e*ge1 and
1er&or"s s"ooth /er"ite inter1olation :etween ) and 1
when e*ge0 T " T e*ge1. 0his is use&ul in cases where
,ou would want a threshold &unction with a s"ooth
transition. 0his is e<uivalent to:
!en0,1e tJ
t V cla"1 BB2 ^ ed!e)C D Bed!e1 ^ ed!e)C, ), 1CJ
return t R t R B% ^ # R tCJ
BAnd si"ilarl, &or dou:les.C
Results are un*e&ine* i& e*ge0 MC e*ge1.
!enB0,1e isnan B!en0,1e "C
!enB0,1e isnan B!enD0,1e "C
Returns true i& " holds a 8a8. Returns false otherwise.
Alwa,s returns false i& 8a8s are not i"1le"ented.
!enB0,1e isinf B!en0,1e "C
!enB0,1e isinf B!enD0,1e "C
Returns true i& " holds a 1ositive in&init, or ne!ative
in&init,. Returns false otherwise.
!enI0,1e floatJitsBoPnt B!en0,1e valueC
!en70,1e floatJitsBoTint B!en0,1e valueC
Returns a si!ned or unsi!ned inte!er value re1resentin!
the encodin! o& a float. 0he float valueFs :it9level
re1resentation is 1reserved.
136
8 !ui%t+in ,unction"
S&nta, +escription
!en0,1e intJitsBoNloat B!enI0,1e valueC
!en0,1e uintJitsBoNloat B!en70,1e valueC
Returns a float value corres1ondin! to a si!ned or
unsi!ned inte!er encodin! o& a float. I& a 8a8 is 1assed
in, it will not si!nal, and the resultin! value is
uns1eci&ied. I& an In& is 1assed in, the resultin! value is
the corres1ondin! In&.
!en0,1e fma B!en0,1e a, !en0,1e b,
!en0,1e cC
!enD0,1e fma B!enD0,1e a, !enD0,1e b,
!enD0,1e cC
$o"1utes and returns aAb ? c.
In uses where the return value is eventuall, consu"ed :,
a varia:le declared as precise:
fmaBC is considered a sin!le o1eration, whereas the
e21ression GaRb I cH consu"ed :, a varia:le
declared precise is considered two o1erations.
0he 1recision o& fmaBC can di&&er &ro" the 1recision
o& the e21ression GaRb I cH.
fmaBC will :e co"1uted with the sa"e 1recision as
an, other fmaBC consu"ed :, a precise varia:le,
!ivin! invariant results &or the sa"e in1ut values o&
a, b, and c.
-therwise, in the a:sence o& precise consu"1tion, there
are no s1ecial constraints on the nu":er o& o1erations or
di&&erence in 1recision :etween fmaBC and the e21ression
GaRb I cH.
!en0,1e frexp B!en0,1e ",
out !enI0,1e e")C
!enD0,1e frexp B!enD0,1e ",
out !enI0,1e e")C
+1lits " into a &loatin!91oint si!ni&icand in the ran!e
L).., 1.)C and an inte!ral e21onent o& two, such that:
"=signi&ican*#
e")onent
0he si!ni&icand is returned :, the &unction and the
e21onent is returned in the 1ara"eter e"). 5or a
&loatin!91oint value o& =ero, the si!ni&icand and
e21onent are :oth =ero. 5or a &loatin!91oint value that is
an in&init, or is not a nu":er, the results are unde&ined.
!en0,1e ldexp B!en0,1e ",
in !enI0,1e e")C
!enD0,1e ldexp B!enD0,1e ",
in !enI0,1e e")C
Builds a &loatin!91oint nu":er &ro" " and the
corres1ondin! inte!ral e21onent o& two in e"), returnin!:
signi&ican*#
e")onent
I& this 1roduct is too lar!e to :e re1resented in the
&loatin!91oint t,1e, the result is unde&ined.
137
8 !ui%t+in ,unction"
4." .loating7-oint -ac2 and 5npac2 .unctions
0hese &unctions do not o1erate co"1onent9wise, rather, as descri:ed in each case.
S&nta, +escription
uint packTnormGx1Z Bvec# vC
uint packSnormGx1Z Bvec# vC
uint packTnormEx\ Bvec( vC
uint packSnormEx\ Bvec( vC
5irst, converts each co"1onent o& the nor"ali=ed
&loatin!91oint value v into 49 or 1*9:it inte!er values.
0hen, the results are 1aced into the returned %#9:it
unsi!ned inte!er.
0he conversion &or co"1onent c o& v to &i2ed 1oint is
done as &ollows:
packTnormGx1Z: roundBcla"1Bc, ), I1C R *..%..)C
packSnormGx1Z= roundBcla"1Bc, 91, I1C R %#'*'.)C
packTnormEx\: roundBcla"1Bc, ), I1C R #...)C
packSnormEx\: roundBcla"1Bc, 91, I1C R 1#'.)C
0he &irst co"1onent o& the vector will :e written to the
least si!ni&icant :its o& the out1utJ the last co"1onent
will :e written to the "ost si!ni&icant :its.
vec# unpackTnormGx1Z Buint )C
vec# unpackSnormGx1Z Buint )C
vec( unpackTnormEx\ Buint )C
vec( unpackSnormEx\ Buint )C
5irst, un1acs a sin!le %#9:it unsi!ned inte!er ) into a
1air o& 1*9:it unsi!ned inte!ers, &our 49:it unsi!ned
inte!ers, or &our 49:it si!ned inte!ers. 0hen, each
co"1onent is converted to a nor"ali=ed &loatin!91oint
value to !enerate the returned two9 or &our9co"1onent
vector.
0he conversion &or un1aced &i2ed91oint value & to
&loatin! 1oint is done as &ollows:
unpackTnormGx1Z= & D *..%..)
unpackSnormGx1Z= cla"1B& D %#'*'.), 91, I1C
unpackTnormEx\= & D #...)
unpackSnormEx\= cla"1B& D 1#'.), 91, I1C
0he &irst co"1onent o& the returned vector will :e
e2tracted &ro" the least si!ni&icant :its o& the in1utJ the
last co"1onent will :e e2tracted &ro" the "ost
si!ni&icant :its.
138
8 !ui%t+in ,unction"
S&nta, +escription
dou:le packCoubleGxFG Buvec# vC Returns a dou:le91recision value o:tained :, 1acin!
the co"1onents o& v into a *(9:it value. I& an IEEE '.(
In& or 8a8 is created, it will not si!nal, and the resultin!
&loatin!91oint value is uns1eci&ied. -therwise, the :it9
level re1resentation o& v is 1reserved. 0he &irst vector
co"1onent s1eci&ies the %# least si!ni&icant :itsJ the
second co"1onent s1eci&ies the %# "ost si!ni&icant :its.
uvec# unpackCoubleGxFG Bdou:le vC Returns a two9co"1onent unsi!ned inte!er vector
re1resentation o& v. 0he :it9level re1resentation o& v is
1reserved. 0he &irst co"1onent o& the vector contains
the %# least si!ni&icant :its o& the dou:leJ the second
co"1onent consists o& the %# "ost si!ni&icant :its.
uint packOalfGx1Z Bvec# vC Returns an unsi!ned inte!er o:tained :, convertin! the
co"1onents o& a two9co"1onent &loatin!91oint vector to
the 1*9:it &loatin!91oint re1resentation &ound in the
-1enGL +1eci&ication, and then 1acin! these two 1*9
:it inte!ers into a %#9:it unsi!ned inte!er.
0he &irst vector co"1onent s1eci&ies the 1* least9
si!ni&icant :its o& the resultJ the second co"1onent
s1eci&ies the 1* "ost9si!ni&icant :its.
vec# unpackOalfGx1Z Buint vC Returns a two9co"1onent &loatin!91oint vector with
co"1onents o:tained :, un1acin! a %#9:it unsi!ned
inte!er into a 1air o& 1*9:it values, inter1retin! those
values as 1*9:it &loatin!91oint nu":ers accordin! to the
-1enGL +1eci&ication, and convertin! the" to %#9:it
&loatin!91oint values.
0he &irst co"1onent o& the vector is o:tained &ro" the
1* least9si!ni&icant :its o& vJ the second co"1onent is
o:tained &ro" the 1* "ost9si!ni&icant :its o& v.
139
8 !ui%t+in ,unction"
4.* Geometric .unctions
0hese o1erate on vectors as vectors, not co"1onent9wise.
S&nta, +escription
&loat lengt" B!en0,1e "C
dou:le lengt" B!enD0,1e "C
Returns the len!th o& vector ", i.e.,
"[ )]
#
+"[1]
#
+...
&loat distance B!en0,1e )0, !en0,1e )1C
dou:le distance B!enD0,1e )0,
!enD0,1e )1C
Returns the distance :etween )0 and )1, i.e.,
lengt" B)0 @ )1C
&loat dot B!en0,1e ", !en0,1e /C
dou:le dot B!enD0,1e ", !enD0,1e /C
Returns the dot 1roduct o& " and /, i.e.,
"[ )]/ [)]+"[ 1]/ [1]+...
vec% cross Bvec% ", vec% /C
dvec% cross Bdvec% ", dvec% /C
Returns the cross 1roduct o& 2 and ,, i.e.,
[
"[ 1]/ [#]/[ 1]"[ #]
"[ #]/[)]/[ #]" [)]
" [)]/[1]/[ )]"[ 1]
]
!en0,1e normalize B!en0,1e "C
!enD0,1e normalize B!enD0,1e "C
Returns a vector in the sa"e direction as " :ut with a
len!th o& 1.
co"1ati:ilit, 1ro&ile onl,
vec( ftransform BC
Availa:le onl, when usin! the co"1ati:ilit, 1ro&ile. 5or
core -1enGL, use invariant.
5or verte2 shaders onl,. 0his &unction will ensure that
the inco"in! verte2 value will :e trans&or"ed in a wa,
that 1roduces e2actl, the sa"e result as would :e
1roduced :, -1enGLOs &i2ed &unctionalit, trans&or". It
is intended to :e used to co"1ute glBosition, e.!.,
!l?3osition V ftransformBC
0his &unction should :e used, &or e2a"1le, when an
a11lication is renderin! the sa"e !eo"etr, in se1arate
1asses, and one 1ass uses the &i2ed &unctionalit, 1ath to
render and another 1ass uses 1ro!ra""a:le shaders.
14
8 !ui%t+in ,unction"
S&nta, +escription
!en0,1e faceforward B!en0,1e 7,
!en0,1e 0,
!en0,1e 7re&C
!enD0,1e faceforward B!enD0,1e 7,
!enD0,1e 0,
!enD0,1e 7re&C
I& dotB7re&, 0C T ) return 7. otherwise return ^7.
!en0,1e reflect B!en0,1e 0, !en0,1e 7C
!enD0,1e reflect B!enD0,1e 0,
!enD0,1e 7C
5or the incident vector 0 and sur&ace orientation 7,
returns the re&lection direction:
0 ^ # dotB7, 0C 7
7 "ust alread, :e nor"ali=ed in order to achieve the
desired result.
!en0,1e refract B!en0,1e 0, !en0,1e 7,
&loat etaC
!enD0,1e refract B!enD0,1e 0,
!enD0,1e 7,
&loat etaC
5or the incident vector 0 and sur&ace nor"al 7, and the
ratio o& indices o& re&raction eta. return the re&raction
vector. 0he result is co"1uted :,
V 1.) 9 eta R eta R B1.) 9 dotB7, 0C R dotB7, 0CC
i& B T ).)C
return !en0,1eB).)C DD or !enD0,1eB).)C
else
return eta R 0 9 Beta R dotB7, 0C I sMrtBCC R 7
0he in1ut 1ara"eters &or the incident vector 0 and the
sur&ace nor"al 7 "ust alread, :e nor"ali=ed to !et the
desired results.
141
8 !ui%t+in ,unction"
4.% 8atri, .unctions
5or each o& the &ollowin! :uilt9in "atri2 &unctions, there is :oth a sin!le91recision &loatin!91oint version,
where all ar!u"ents and return values are sin!le 1recision, and a dou:le91recision &loatin!91oint version,
where all ar!u"ents and return values are dou:le 1recision. -nl, the sin!le91recision &loatin!91oint
version is shown.
S&nta, +escription
"at matrixHompKult B"at ", "at /C ;ulti1l, "atri2 " :, "atri2 / co"1onent9wise, i.e.,
resultLiMLEM is the scalar 1roduct o& "LiMLEM and /LiMLEM.
8ote: to !et linear al!e:raic "atri2 "ulti1lication, use
the "ulti1l, o1erator B/C.
"at# outer'roduct Bvec# c, vec# rC
"at% outer'roduct Bvec% c, vec% rC
"at( outer'roduct Bvec( c, vec( rC
"at#2% outer'roduct Bvec% c, vec# rC
"at%2# outer'roduct Bvec# c, vec% rC
"at#2( outer'roduct Bvec( c, vec# rC
"at(2# outer'roduct Bvec# c, vec( rC
"at%2( outer'roduct Bvec( c, vec% rC
"at(2% outer'roduct Bvec% c, vec( rC
0reats the &irst 1ara"eter c as a colu"n vector B"atri2
with one colu"nC and the second 1ara"eter r as a row
vector B"atri2 with one rowC and does a linear al!e:raic
"atri2 "ulti1l, c R r, ,ieldin! a "atri2 whose nu":er o&
rows is the nu":er o& co"1onents in c and whose
nu":er o& colu"ns is the nu":er o& co"1onents in r.
"at# transpose B"at# mC
"at% transpose B"at% mC
"at( transpose B"at( mC
"at#2% transpose B"at%2# mC
"at%2# transpose B"at#2% mC
"at#2( transpose B"at(2# mC
"at(2# transpose B"at#2( mC
"at%2( transpose B"at(2% mC
"at(2% transpose B"at%2( mC
Returns a "atri2 that is the trans1ose o& m. 0he in1ut
"atri2 m is not "odi&ied.
&loat determinant B"at# mC
&loat determinant B"at% mC
&loat determinant B"at( mC
Returns the deter"inant o& m.
142
8 !ui%t+in ,unction"
S&nta, +escription
"at# inverse B"at# mC
"at% inverse B"at% mC
"at( inverse B"at( mC
Returns a "atri2 that is the inverse o& m. 0he in1ut
"atri2 m is not "odi&ied. 0he values in the returned
"atri2 are unde&ined i& m is sin!ular or 1oorl,9
conditioned Bnearl, sin!ularC.
143
8 !ui%t+in ,unction"
4. !ector Relational .unctions
Relational and e<ualit, o1erators B1* 1;* 2* 2;* ;;* <;C are de&ined to o1erate on scalars and 1roduce
scalar Boolean results. 5or vector results, use the &ollowin! :uilt9in &unctions. Below, the &ollowin!
1laceholders are used &or the listed s1eci&ic t,1es:
'lace"older Specific Bypes Allowed
:vec :vec#, :vec%, :vec(
ivec ivec#, ivec%, ivec(
uvec uvec#, uvec%, uvec(
vec vec#, vec%, vec(, dvec#, dvec%, dvec(
In all cases, the si=es o& all the in1ut and return vectors &or an, 1articular call "ust "atch.
S&nta, +escription
:vec lessB"an Bvec 2, vec ,C
:vec lessB"an Bivec 2, ivec ,C
:vec lessB"an Buvec 2, uvec ,C
Returns the co"1onent9wise co"1are o& " T /.
:vec lessB"an&Mual Bvec 2, vec ,C
:vec lessB"an&Mual Bivec 2, ivec ,C
:vec lessB"an&Mual Buvec 2, uvec ,C
Returns the co"1onent9wise co"1are o& " TV /.
:vec greaterB"an Bvec 2, vec ,C
:vec greaterB"an Bivec 2, ivec ,C
:vec greaterB"an Buvec 2, uvec ,C
Returns the co"1onent9wise co"1are o& " U /.
:vec greaterB"an&Mual Bvec 2, vec ,C
:vec greaterB"an&Mual Bivec 2, ivec ,C
:vec greaterB"an&Mual Buvec 2, uvec ,C
Returns the co"1onent9wise co"1are o& " UV /.
:vec eMual Bvec 2, vec ,C
:vec eMual Bivec 2, ivec ,C
:vec eMual Buvec 2, uvec ,C
:vec eMual B:vec 2, :vec ,C
:vec not&Mual Bvec 2, vec ,C
:vec not&Mual Bivec 2, ivec ,C
:vec not&Mual Buvec 2, uvec ,C
:vec not&Mual B:vec 2, :vec ,C
Returns the co"1onent9wise co"1are o& " VV /.
Returns the co"1onent9wise co"1are o& " QV /.
:ool any B:vec 2C Returns true i& an, co"1onent o& " is true.
144
8 !ui%t+in ,unction"
S&nta, +escription
:ool all B:vec 2C Returns true onl, i& all co"1onents o& " are true.
:vec not B:vec 2C Returns the co"1onent9wise lo!ical co"1le"ent o& ".
145
8 !ui%t+in ,unction"
4.4 Integer .unctions
0hese all o1erate co"1onent9wise. 0he descri1tion is 1er co"1onent. 0he notation La, bM "eans the set
o& :its &ro" :it9nu":er a throu!h :it9nu":er b, inclusive. 0he lowest9order :it is :it ). GBit nu":erH
will alwa,s re&er to countin! u1 &ro" the lowest9order :it as :it ).
S&nta, +escription
!en70,1e uaddHarry B!en70,1e ",
!en70,1e /,
out !en70,1e carr/C
Adds %#9:it unsi!ned inte!er " and /, returnin! the su"
"odulo #
%#
. 0he value carr/ is set to ) i& the su" was
less than #
%#
, or to 1 otherwise.
!en70,1e usubJorrow B!en70,1e ",
!en70,1e /,
out !en70,1e borro2C
+u:tracts the %#9:it unsi!ned inte!er / &ro" ", returnin!
the di&&erence i& non9ne!ative, or #
%#
1lus the di&&erence
otherwise. 0he value borro2 is set to ) i& " MC /, or to
1 otherwise.
void umul&xtended B!en70,1e ",
!en70,1e /,
out !en70,1e msb,
out !en70,1e lsbC
void imul&xtended B!enI0,1e ",
!enI0,1e /,
out !enI0,1e msb,
out !enI0,1e lsbC
;ulti1lies %#9:it inte!ers " and /, 1roducin! a *(9:it
result. 0he %# least9si!ni&icant :its are returned in lsb.
0he %# "ost9si!ni&icant :its are returned in msb.
!enI0,1e bitfield&xtract B!enI0,1e value,
int o&&set, int bitsC
!en70,1e bitfield&xtract B!en70,1e value,
int o&&set, int bitsC
E2tracts :its Lo&&set, o&&set I bits - 1M &ro" value,
returnin! the" in the least si!ni&icant :its o& the result.
5or unsi!ned data t,1es, the "ost si!ni&icant :its o& the
result will :e set to =ero. 5or si!ned data t,1es, the
"ost si!ni&icant :its will :e set to the value o& :it o&&set
I bits @ 1.
I& bits is =ero, the result will :e =ero. 0he result will :e
unde&ined i& o&&set or bits is ne!ative, or i& the su" o&
o&&set and bits is !reater than the nu":er o& :its used
to store the o1erand.
146
8 !ui%t+in ,unction"
S&nta, +escription
!enI0,1e bitfieldPnsert B!enI0,1e base,
!enI0,1e insert,
int o&&set, int bitsC
!en70,1e bitfieldPnsert B!en70,1e base,
!en70,1e insert,
int o&&set, int bitsC
Returns the insertion o& the bits least9si!ni&icant :its o&
insert into base.
0he result will have :its Lo&&set, o&&set I bits - 1M taen
&ro" :its L), bits @ 1M o& insert, and all other :its taen
directl, &ro" the corres1ondin! :its o& base. I& bits is
=ero, the result will si"1l, :e base. 0he result will :e
unde&ined i& o&&set or bits is ne!ative, or i& the su" o&
o&&set and bits is !reater than the nu":er o& :its used to
store the o1erand.
!enI0,1e bitfieldIeverse B!enI0,1e valueC
!en70,1e bitfieldIeverse B!en70,1e valueC
Returns the reversal o& the :its o& value. 0he :it
nu":ered n o& the result will :e taen &ro" :it Bbits - 1C
- n o& value, where bits is the total nu":er o& :its used
to re1resent value.
!enI0,1e bitHount B!enI0,1e valueC
!enI0,1e bitHount B!en70,1e valueC
Returns the nu":er o& :its set to 1 in the :inar,
re1resentation o& value.
!enI0,1e find$SJ B!enI0,1e valueC
!enI0,1e find$SJ B!en70,1e valueC
Returns the :it nu":er o& the least si!ni&icant :it set to
1 in the :inar, re1resentation o& value. I& value is =ero,
91will :e returned.
!enI0,1e findKSJ B!enI0,1e valueC
!enI0,1e findKSJ B!en70,1e valueC
Returns the :it nu":er o& the "ost si!ni&icant :it in the
:inar, re1resentation o& value.
5or 1ositive inte!ers, the result will :e the :it nu":er o&
the "ost si!ni&icant :it set to 1. 5or ne!ative inte!ers,
the result will :e the :it nu":er o& the "ost si!ni&icant
:it set to ). 5or a value o& =ero or ne!ative one, 91 will
:e returned.
147
8 !ui%t+in ,unction"
4.9 Te,ture .unctions
0e2ture loou1 &unctions are availa:le in all shadin! sta!es. /owever, auto"atic level o& detail is
co"1uted onl, &or &ra!"ent shaders. -ther shaders o1erate as thou!h the :ase level o& detail were
co"1uted as =ero. 0he &unctions in the ta:le :elow 1rovide access to te2tures throu!h sa"1lers, as set u1
throu!h the -1enGL A3I. 0e2ture 1ro1erties such as si=e, 1i2el &or"at, nu":er o& di"ensions, &ilterin!
"ethod, nu":er o& "i1"a1 levels, de1th co"1arison, and so on are also de&ined :, -1enGL A3I calls.
+uch 1ro1erties are taen into account as the te2ture is accessed via the :uilt9in &unctions de&ined :elow.
0e2ture data can :e stored :, the GL as sin!le91recision &loatin! 1oint, unsi!ned nor"ali=ed inte!er,
unsi!ned inte!er or si!ned inte!er data. 0his is deter"ined :, the t,1e o& the internal &or"at o& the
te2ture. 0e2ture loou1s on unsi!ned nor"ali=ed inte!er and &loatin!91oint data return &loatin!91oint
values in the ran!e L), 1M.
0e2ture loou1 &unctions are 1rovided that can return their result as &loatin! 1oint, unsi!ned inte!er or
si!ned inte!er, de1endin! on the sa"1ler t,1e 1assed to the loou1 &unction. $are "ust :e taen to use
the ri!ht sa"1ler t,1e &or te2ture access. 0he &ollowin! ta:le lists the su11orted co":inations o& sa"1ler
t,1es and te2ture internal &or"ats. Blan entries are unsu11orted. Doin! a te2ture loou1 will return
unde&ined values &or unsu11orted co":inations.
Internal 0e2ture 5or"at
5loatin! 3oint
+a"1ler 0,1es
+i!ned Inte!er
+a"1ler 0,1es
7nsi!ned Inte!er
+a"1ler 0,1es
5loatin! 1oint +u11orted
8or"ali=ed Inte!er +u11orted
+i!ned Inte!er +u11orted
7nsi!ned Inte!er +u11orted
I& an inte!er sa"1ler t,1e is used, the result o& a te2ture loou1 is an ivecE. I& an unsi!ned inte!er sa"1ler
t,1e is used, the result o& a te2ture loou1 is a uvecE. I& a &loatin!91oint sa"1ler t,1e is used, the result o&
a te2ture loou1 is a vecE, where each co"1onent is in the ran!e L), 1M.
In the 1rotot,1es :elow, the GgH in the return t,1e Ggvec4H is used as a 1laceholder &or nothin!, GiH, or GuH
"ain! a return t,1e o& vecE, ivecE, or uvecE. In these cases, the sa"1ler ar!u"ent t,1e also starts with
GgH, indicatin! the sa"e su:stitution done on the return t,1eJ it is either a sin!le91recision &loatin! 1oint,
si!ned inte!er, or unsi!ned inte!er sa"1ler, "atchin! the :asic t,1e o& the return t,1e, as descri:ed
a:ove.
5or shadow &or"s Bthe sa"1ler 1ara"eter is a shadow9t,1eC, a de1th co"1arison loou1 on the de1th
te2ture :ound to sam)ler is done as descri:ed in section 4.## G0e2ture $o"1arison ;odesH o& the
-1enGL Gra1hics +,ste" +1eci&ication. +ee the ta:le :elow &or which co"1onent s1eci&ies Dre&. 0he
te2ture :ound to sam)ler "ust :e a de1th te2ture, or results are unde&ined. I& a non9shadow te2ture call is
"ade to a sa"1ler that re1resents a de1th te2ture with de1th co"1arisons turned on, then results are
unde&ined. I& a shadow te2ture call is "ade to a sa"1ler that re1resents a de1th te2ture with de1th
co"1arisons turned o&&, then results are unde&ined. I& a shadow te2ture call is "ade to a sa"1ler that does
not re1resent a de1th te2ture, then results are unde&ined.
148
8 !ui%t+in ,unction"
In all &unctions :elow, the bias 1ara"eter is o1tional &or &ra!"ent shaders. 0he bias 1ara"eter is not
acce1ted in an, other shader sta!e. 5or a &ra!"ent shader, i& bias is 1resent, it is added to the i"1licit
level o& detail 1rior to 1er&or"in! the te2ture access o1eration. 8o bias or lo* 1ara"eters &or rectan!le
te2tures, "ulti9sa"1le te2tures, or te2ture :u&&ers are su11orted :ecause "i1"a1s are not allowed &or
these t,1es o& te2tures.
0he i"1licit level o& detail is selected as &ollows: 5or a te2ture that is not "i1"a11ed, the te2ture is used
directl,. I& it is "i1"a11ed and runnin! in a &ra!"ent shader, the L-D co"1uted :, the i"1le"entation
is used to do the te2ture loou1. I& it is "i1"a11ed and runnin! on the verte2 shader, then the :ase
te2ture is used.
+o"e te2ture &unctions Bnon9G$odH and non9GDradH versionsC "a, re<uire i"1licit derivatives. I"1licit
derivatives are unde&ined within non9uni&or" control &low and &or non9&ra!"ent9shader te2ture &etches.
5or Hube &or"s, the direction o& B is used to select which &ace to do a #9di"ensional te2ture loou1 in, as
descri:ed in section 4.1% G$u:e ;a1 0e2ture +electionH in the -1enGL Gra1hics +,ste" +1eci&ication.
5or Array &or"s, the arra, la,er used will :e
ma"(),min(* 1, &loor(la/er+)..)))
where * is the de1th o& the te2ture arra, and la/er co"es &ro" the co"1onent indicated in the ta:les
:elow.
5or de1thDstencil te2tures, the sa"1ler t,1e should "atch the co"1onent :ein! accessed as set throu!h the
-1enGL A3I. @hen the de1thDstencil te2ture "ode is set to DE30/?$-;3-8E80, a &loatin!91oint
sa"1ler t,1e should :e used. @hen the de1thDstencil te2ture "ode is set to +0E8$IL?I8DE\, an
unsi!ned inte!er sa"1ler t,1e should :e used. Doin! a te2ture loou1 with an unsu11orted co":ination
will return unde&ined values.
4.9.1 Te,ture ;uer& .unctions
0he textureSize &unctions <uer, the di"ensions o& a s1eci&ic te2ture level &or a sa"1ler.
0he texture#uery$od &unctions are availa:le onl, in a &ra!"ent shader. 0he, tae the co"1onents o& B
and co"1ute the level o& detail in&or"ation that the te2ture 1i1e would use to access that te2ture throu!h a
nor"al te2ture loou1. 0he level o& detail NO Be<uation %.14 in the -1enGL Gra1hics +,ste"
+1eci&icationC is o:tained a&ter an, L-D :ias, :ut 1rior to cla"1in! to L0E\07RE?;I8?L-D,
0E\07RE?;A\?L-DM. 0he "i1"a1 arra,BsC that would :e accessed are also co"1uted. I& a sin!le
level o& detail would :e accessed, the level9o&9detail nu":er relative to the :ase level is returned. I&
"ulti1le levels o& detail would :e accessed, a &loatin!91oint nu":er :etween the two levels is returned,
with the &ractional 1art e<ual to the &ractional 1art o& the co"1uted and cla"1ed level o& detail.
149
8 !ui%t+in ,unction"
0he al!orith" used is !iven :, the &ollowin! 1seudo9code:
float Mom!uteDccessed#od(float com!uted#od)
5
// Mlam! the com!uted #+- according to the texture #+- clam!s6
if (com!uted#od 7 ,&U,@)&"G$%"#+-) com!uted#od 4 ,&U,@)&"G$%"#+-;
if (com!uted#od Q ,&U,@)&"GDU"#+-) com!uted#od 4 ,&U,@)&"GDU"#+-;
// Mlam! the com!uted #+- to the range of accessible levels6
if (com!uted#od 7 ?6?)
com!uted#od 4 ?6?;
if (com!uted#od Q (float)
maxDccessible#evel) com!uted#od 4 (float) maxDccessible#evel;
// )eturn a value according to the min filter6
if (,&U,@)&"G$%"'$#,&) is #$%&D) or %&D)&*,) 5
return ?6?;
9 else if (,&U,@)&"G$%"'$#,&) is %&D)&*,"G$HGDH"%&D)&*,
or #$%&D)"G$HGDH"%&D)&*,) 5
return ceil(com!uted#od K ?6E) 3 16?;
9 else 5
return com!uted#od;
9
9
0he value ma"#ccessibleLevel is the level nu":er o& the s"allest accessi:le level o& the "i1"a1 arra,
Bthe value 3 in section 4.1(.% G;i1"a11in!H o& the -1enGL Gra1hics +,ste" +1eci&icationC "inus the
:ase level.
S&nta, +escription
int textureSize B!sa"1ler1D sam)ler, int lo*C
ivec# textureSize B!sa"1ler#D sam)ler, int lo*C
ivec% textureSize B!sa"1ler%D sam)ler, int lo*C
ivec# textureSize B!sa"1ler$u:e sam)ler, int lo*C
int textureSize Bsa"1ler1D+hadow sam)ler, int lo*C
ivec# textureSize Bsa"1ler#D+hadow sam)ler, int lo*C
ivec# textureSize Bsa"1ler$u:e+hadow sam)ler, int lo*C
ivec% textureSize B!sa"1ler$u:eArra, sam)ler, int lo*C
ivec% textureSize Bsa"1ler$u:eArra,+hadow sam)ler, int lo*C
ivec# textureSize B!sa"1ler#DRect sam)lerC
ivec# textureSize Bsa"1ler#DRect+hadow sam)lerC
ivec# textureSize B!sa"1ler1DArra, sam)ler, int lo*C
ivec% textureSize B!sa"1ler#DArra, sam)ler, int lo*C
ivec# textureSize Bsa"1ler1DArra,+hadow sam)ler, int lo*C
ivec% textureSize Bsa"1ler#DArra,+hadow sam)ler, int lo*C
int textureSize B!sa"1lerBu&&er sam)lerC
ivec# textureSize B!sa"1ler#D;+ sam)lerC
ivec% textureSize B!sa"1ler#D;+Arra, sam)lerC
Returns the di"ensions o& level
lo* Bi& 1resentC &or the te2ture
:ound to sam)ler, as descri:ed
in section 11.1.%.( G0e2ture
>ueriesH o& the -1enGL
Gra1hics +,ste" +1eci&ication.
0he co"1onents in the return
value are &illed in, in order, with
the width, hei!ht, and de1th o&
the te2ture.
5or the arra, &or"s, the last
co"1onent o& the return value is
the nu":er o& la,ers in the
te2ture arra,, or the nu":er o&
cu:es in the te2ture cu:e "a1
arra,.
15
8 !ui%t+in ,unction"
S&nta, +escription
vec# texture#uery$odB!sa"1ler1D sam)ler, &loat BC
vec# texture#uery$odB!sa"1ler#D sam)ler, vec# BC
vec# texture#uery$odB!sa"1ler%D sam)ler, vec% BC
vec# texture#uery$odB!sa"1ler$u:e sam)ler, vec% BC
vec# texture#uery$odB!sa"1ler1DArra, sam)ler, &loat BC
vec# texture#uery$odB!sa"1ler#DArra, sam)ler, vec# BC
vec# texture#uery$odB!sa"1ler$u:eArra, sam)ler, vec% BC
vec# texture#uery$odBsa"1ler1D+hadow sam)ler, &loat BC
vec# texture#uery$odBsa"1ler#D+hadow sam)ler, vec# BC
vec# texture#uery$odBsa"1ler$u:e+hadow sam)ler, vec% BC
vec# texture#uery$odBsa"1ler1DArra,+hadow sam)ler, &loat BC
vec# texture#uery$odBsa"1ler#DArra,+hadow sam)ler, vec# BC
vec# texture#uery$odBsa"1ler$u:eArra,+hadow sam)ler, vec% BC
Returns the "i1"a1 arra,BsC
that would :e accessed in the "
co"1onent o& the return value.
Returns the co"1uted level o&
detail relative to the :ase level
in the / co"1onent o& the return
value.
I& called on an inco"1lete
te2ture, the results are
unde&ined.
int texture#uery$evelsB!sa"1ler1D sam)lerC
int texture#uery$evelsB!sa"1ler#D sam)lerC
int texture#uery$evelsB!sa"1ler%D sam)lerC
int texture#uery$evelsB!sa"1ler$u:e sam)lerC
int texture#uery$evelsB!sa"1ler1DArra, sam)lerC
int texture#uery$evelsB!sa"1ler#DArra, sam)lerC
int texture#uery$evelsB!sa"1ler$u:eArra, sam)lerC
int texture#uery$evelsB!sa"1ler1D+hadow sam)lerC
int texture#uery$evelsB!sa"1ler#D+hadow sam)lerC
int texture#uery$evelsB!sa"1ler$u:e+hadow sam)lerC
int texture#uery$evelsB!sa"1ler1DArra,+hadow sam)lerC
int texture#uery$evelsB!sa"1ler#DArra,+hadow sam)lerC
int texture#uery$evelsB!sa"1ler$u:eArra,+hadow sam)lerC
Returns the nu":er o& "i1"a1
levels accessi:le in the te2ture
associated with sam)ler, as
de&ined in the -1enGL
+1eci&ication.
0he value =ero will :e returned
i& no te2ture or an inco"1lete
te2ture is associated with
sam)ler.
Availa:le in all shader sta!es.
151
8 !ui%t+in ,unction"
4.9.2 Te,el Loo2up .unctions
S&nta, +escription
!vec( texture B!sa"1ler1D sam)ler. &loat B L, &loat biasM C
!vec( texture B!sa"1ler#D sam)ler, vec# B L, &loat biasM C
!vec( texture B!sa"1ler%D sam)ler, vec% B L, &loat biasM C
!vec( texture B!sa"1ler$u:e sam)ler, vec% B L, &loat biasM C
&loat texture Bsa"1ler1D+hadow sam)ler. vec% B L, &loat biasM C
&loat texture Bsa"1ler#D+hadow sam)ler. vec% B L, &loat biasM C
&loat texture Bsa"1ler$u:e+hadow sam)ler. vec( B L, &loat biasM C
!vec( texture B!sa"1ler1DArra, sam)ler. vec# B L, &loat biasM C
!vec( texture B!sa"1ler#DArra, sam)ler, vec% B L, &loat biasM C
!vec( texture B!sa"1ler$u:eArra, sam)ler, vec( B L, &loat biasM C
&loat texture Bsa"1ler1DArra,+hadow sam)ler. vec% B
L, &loat biasM C
&loat texture Bsa"1ler#DArra,+hadow sam)ler. vec( BC
!vec( texture B!sa"1ler#DRect sam)ler, vec# BC
&loat texture Bsa"1ler#DRect+hadow sam)ler, vec% BC
&loat texture B!sa"1ler$u:eArra,+hadow sam)ler, vec( B,
&loat com)areC
7se the te2ture coordinate B to
do a te2ture loou1 in the
te2ture currentl, :ound to
sam)ler.
5or shadow &or"s: @hen
com)are is 1resent, it is used as
Dre& and the arra, la,er co"es
&ro" B.2. @hen com)are is not
1resent, the last co"1onent o&
B is used as Dre& and the arra,
la,er co"es &ro" the second to
last co"1onent o& B. B0he
second co"1onent o& B is
unused &or 1C shadow loou1s.C
5or non9shadow &or"s: the arra,
la,er co"es &ro" the last
co"1onent o& B.
!vec( texture'roL B!sa"1ler1D sam)ler, vec# B L, &loat biasM C
!vec( texture'roL B!sa"1ler1D sam)ler, vec( B L, &loat biasM C
!vec( texture'roL B!sa"1ler#D sam)ler, vec% B L, &loat biasM C
!vec( texture'roL B!sa"1ler#D sam)ler, vec( B L, &loat biasM C
!vec( texture'roL B!sa"1ler%D sam)ler, vec( B L, &loat biasM C
&loat texture'roL Bsa"1ler1D+hadow sam)ler. vec( B
L, &loat biasM C
&loat texture'roL Bsa"1ler#D+hadow sam)ler. vec( B
L, &loat biasM C
!vec( texture'roL B!sa"1ler#DRect sam)ler, vec% BC
!vec( texture'roL B!sa"1ler#DRect sam)ler, vec( BC
&loat texture'roL Bsa"1ler#DRect+hadow sam)ler, vec( BC
Do a te2ture loou1 with
1roEection. 0he te2ture
coordinates consu"ed &ro" B,
not includin! the last co"1onent
o& B, are divided :, the last
co"1onent o& B. 0he resultin!
%
rd
co"1onent o& B in the
shadow &or"s is used as Dre&.
A&ter these values are co"1uted,
te2ture loou1 1roceeds as in
texture.
!vec( texture$od B!sa"1ler1D sam)ler, &loat B, &loat lo*C
!vec( texture$od B!sa"1ler#D sam)ler, vec# B, &loat lo*C
!vec( texture$od B!sa"1ler%D sam)ler, vec% B, &loat lo*C
!vec( texture$od B!sa"1ler$u:e sam)ler, vec% B, &loat lo*C
&loat texture$od Bsa"1ler1D+hadow sam)ler. vec% B, &loat lo*C
&loat texture$od Bsa"1ler#D+hadow sam)ler. vec% B, &loat lo*C
!vec( texture$od B!sa"1ler1DArra, sam)ler, vec# B, &loat lo*C
!vec( texture$od B!sa"1ler#DArra, sam)ler, vec% B, &loat lo*C
&loat texture$od Bsa"1ler1DArra,+hadow sam)ler. vec% B,
&loat lo*C
!vec( texture$od B!sa"1ler$u:eArra, sam)ler, vec( B, &loat lo*C
Do a te2ture loou1 as in
texture :ut with e21licit L-DJ
lo* s1eci&ies Nbase and sets the
1artial derivatives as &ollows.
B+ee section 4.1(G0e2ture
;ini&icationH and e<uations 4.(9
4.* in the -1enGL Gra1hics
+,ste" +1eci&ication.C
u
"
= )
v
"
= )
2
"
= )
u
/
= )
v
/
= )
2
/
= )
152
8 !ui%t+in ,unction"
S&nta, +escription
!vec( textureOffset B!sa"1ler1D sam)ler. &loat B.
int o&&set L, &loat biasM C
!vec( textureOffset B!sa"1ler#D sam)ler, vec# B,
ivec# o&&set L, &loat biasM C
!vec( textureOffset B!sa"1ler%D sam)ler, vec% B,
ivec% o&&set L, &loat biasM C
!vec( textureOffset B!sa"1ler#DRect sam)ler, vec# B,
ivec# o&&set C
&loat textureOffset Bsa"1ler#DRect+hadow sam)ler, vec% B,
ivec# o&&set C
&loat textureOffset Bsa"1ler1D+hadow sam)ler. vec% B,
int o&&set L, &loat biasM C
&loat textureOffset Bsa"1ler#D+hadow sam)ler. vec% B,
ivec# o&&set L, &loat biasM C
!vec( textureOffset B!sa"1ler1DArra, sam)ler. vec# B.
int o&&set L, &loat biasM C
!vec( textureOffset B!sa"1ler#DArra, sam)ler, vec% B,
ivec# o&&set L, &loat biasM C
&loat textureOffset Bsa"1ler1DArra,+hadow sam)ler. vec% B,
int o&&set L, &loat biasM C
&loat textureOffset Bsa"1ler#DArra,+hadow sam)ler, vec( 3,
vec# o&&set <. &loat bias= C
Do a te2ture loou1 as in
texture :ut with o&&set added to
the Bu,v,2C te2el coordinates
:e&ore looin! u1 each te2el.
0he o&&set value "ust :e a
constant e21ression. A li"ited
ran!e o& o&&set values are
su11ortedJ the "ini"u" and
"a2i"u" o&&set values are
i"1le"entation9de1endent and
!iven :,
!l?;in3ro!ra"0e2el-&&set and
!l?;a23ro!ra"0e2el-&&set,
res1ectivel,.
8ote that o&&set does not a11l,
to the la,er coordinate &or
te2ture arra,s. 0his is e21lained
in detail in section 4.1(.#
G$oordinate @ra11in! and
0e2el +electionH o& the -1enGL
Gra1hics +,ste" +1eci&ication,
where o&&set is (
u
.
v
.
2
).
8ote that te2el o&&sets are also
not su11orted &or cu:e "a1s.
!vec( texelNetc" B!sa"1ler1D sam)ler, int B, int lo*C
!vec( texelNetc" B!sa"1ler#D sam)ler, ivec# B, int lo*C
!vec( texelNetc" B!sa"1ler%D sam)ler, ivec% B, int lo*C
!vec( texelNetc" B!sa"1ler#DRect sam)ler, ivec# BC
!vec( texelNetc" B!sa"1ler1DArra, sam)ler, ivec# B, int lo*C
!vec( texelNetc" B!sa"1ler#DArra, sam)ler, ivec% B, int lo*C
!vec( texelNetc" B!sa"1lerBu&&er sam)ler, int BC
!vec( texelNetc" B!sa"1ler#D;+ sam)ler, ivec# B, int sam)leC
!vec( texelNetc" B!sa"1ler#D;+Arra, sam)ler, ivec% B,
int sam)leC
7se inte!er te2ture coordinate B
to loou1 a sin!le te2el &ro"
sam)ler. 0he arra, la,er co"es
&ro" the last co"1onent o& B &or
the arra, &or"s. 0he level9o&9
detail lo* Bi& 1resentC is as
descri:ed in sections 11.1.%.#
G0e2el 5etchesH and 4.1(.1
G+cale 5actor and Level o&
DetailH o& the -1enGL Gra1hics
+,ste" +1eci&ication.
153
8 !ui%t+in ,unction"
S&nta, +escription
!vec( texelNetc"Offset B!sa"1ler1D sam)ler, int B, int lo*,
int o&&setC
!vec( texelNetc"Offset B!sa"1ler#D sam)ler, ivec# B, int lo*,
ivec# o&&setC
!vec( texelNetc"Offset B!sa"1ler%D sam)ler, ivec% B, int lo*,
ivec% o&&setC
!vec( texelNetc"Offset B!sa"1ler#DRect sam)ler, ivec# B,
ivec# o&&setC
!vec( texelNetc"Offset B!sa"1ler1DArra, sam)ler, ivec# B, int lo*,
int o&&setC
!vec( texelNetc"Offset B!sa"1ler#DArra, sam)ler, ivec% B, int lo*,
ivec# o&&setC
5etch a sin!le te2el as in
texelNetc" o&&set :, o&&set as
descri:ed in textureOffset.
!vec( texture'roLOffset B!sa"1ler1D sam)ler. vec# B.
int o&&set L, &loat biasM C
!vec( texture'roLOffset B!sa"1ler1D sam)ler. vec( B.
int o&&set L, &loat biasM C
!vec( texture'roLOffset B!sa"1ler#D sam)ler, vec% B,
ivec# o&&set L, &loat biasM C
!vec( texture'roLOffset B!sa"1ler#D sam)ler, vec( B,
ivec# o&&set L, &loat biasM C
!vec( texture'roLOffset B!sa"1ler%D sam)ler, vec( B,
ivec% o&&set L, &loat biasM C
!vec( texture'roLOffset B!sa"1ler#DRect sam)ler, vec% B,
ivec# o&&set C
!vec( texture'roLOffset B!sa"1ler#DRect sam)ler, vec( B,
ivec# o&&set C
&loat texture'roLOffset Bsa"1ler#DRect+hadow sam)ler, vec( B,
ivec# o&&set C
&loat texture'roLOffset Bsa"1ler1D+hadow sam)ler. vec( B,
int o&&set L, &loat biasM C
&loat texture'roLOffset Bsa"1ler#D+hadow sam)ler. vec( B,
ivec# o&&set L, &loat biasM C
Do a 1roEective te2ture loou1
as descri:ed in texture'roL
o&&set :, o&&set as descri:ed in
textureOffset.
154
8 !ui%t+in ,unction"
S&nta, +escription
!vec( texture$odOffset B!sa"1ler1D sam)ler. &loat B,
&loat lo*, int o&&setC
!vec( texture$odOffset B!sa"1ler#D sam)ler, vec# B,
&loat lo*, ivec# o&&setC
!vec( texture$odOffset B!sa"1ler%D sam)ler, vec% B,
&loat lo*, ivec% o&&setC
&loat texture$odOffset Bsa"1ler1D+hadow sam)ler. vec% B,
&loat lo*, int o&&setC
&loat texture$odOffset Bsa"1ler#D+hadow sam)ler. vec% B,
&loat lo*, ivec# o&&setC
!vec( texture$odOffset B!sa"1ler1DArra, sam)ler. vec# B,
&loat lo*, int o&&setC
!vec( texture$odOffset B!sa"1ler#DArra, sam)ler, vec% B,
&loat lo*, ivec# o&&setC
&loat texture$odOffset Bsa"1ler1DArra,+hadow sam)ler. vec% B,
&loat lo*, int o&&setC
Do an o&&set te2ture loou1 with
e21licit L-D. +ee texture$od
and textureOffset.
!vec( texture'roL$od B!sa"1ler1D sam)ler, vec# B, &loat lo*C
!vec( texture'roL$od B!sa"1ler1D sam)ler, vec( B, &loat lo*C
!vec( texture'roL$od B!sa"1ler#D sam)ler, vec% B, &loat lo*C
!vec( texture'roL$od B!sa"1ler#D sam)ler, vec( B, &loat lo*C
!vec( texture'roL$od B!sa"1ler%D sam)ler, vec( B, &loat lo*C
&loat texture'roL$od Bsa"1ler1D+hadow sam)ler. vec( B, &loat lo*C
&loat texture'roL$od Bsa"1ler#D+hadow sam)ler. vec( B. &loat lo*C
Do a 1roEective te2ture loou1
with e21licit L-D. +ee
texture'roL and texture$od.
!vec( texture'roL$odOffset B!sa"1ler1D sam)ler. vec# B,
&loat lo*, int o&&setC
!vec( texture'roL$odOffset B!sa"1ler1D sam)ler. vec( B,
&loat lo*, int o&&setC
!vec( texture'roL$odOffset B!sa"1ler#D sam)ler, vec% B,
&loat lo*, ivec# o&&setC
!vec( texture'roL$odOffset B!sa"1ler#D sam)ler, vec( B,
&loat lo*, ivec# o&&setC
!vec( texture'roL$odOffset B!sa"1ler%D sam)ler, vec( B,
&loat lo*, ivec% o&&setC
&loat texture'roL$odOffset Bsa"1ler1D+hadow sam)ler. vec( B,
&loat lo*, int o&&setC
&loat texture'roL$odOffset Bsa"1ler#D+hadow sam)ler. vec( B,
&loat lo*, ivec# o&&setC
Do an o&&set 1roEective te2ture
loou1 with e21licit L-D. +ee
texture'roL, texture$od, and
textureOffset.
155
8 !ui%t+in ,unction"
S&nta, +escription
!vec( textureDrad B!sa"1ler1D sam)ler. &loat B,
&loat *B*", &loat *B*/C
!vec( textureDrad B!sa"1ler#D sam)ler. vec# B,
vec# *B*", vec# *B*/C
!vec( textureDrad B!sa"1ler%D sam)ler. vec% B,
vec% *B*", vec% *B*/C
!vec( textureDrad B!sa"1ler$u:e sam)ler, vec% B,
vec% *B*", vec% *B*/C
!vec( textureDrad B!sa"1ler#DRect sam)ler, vec# B,
vec# *B*", vec# *B*/C
&loat textureDrad Bsa"1ler#DRect+hadow sam)ler, vec% B,
vec# *B*", vec# *B*/C
&loat textureDrad Bsa"1ler1D+hadow sam)ler, vec% B,
&loat *B*", &loat *B*/C
&loat textureDrad Bsa"1ler#D+hadow sam)ler, vec% B,
vec# *B*", vec# *B*/C
&loat textureDrad Bsa"1ler$u:e+hadow sam)ler, vec( B,
vec% *B*", vec% *B*/C
!vec( textureDrad B!sa"1ler1DArra, sam)ler. vec# B,
&loat *B*", &loat *B*/C
!vec( textureDrad B!sa"1ler#DArra, sam)ler. vec% B,
vec# *B*", vec# *B*/C
&loat textureDrad Bsa"1ler1DArra,+hadow sam)ler, vec% B,
&loat *B*", &loat *B*/C
&loat textureDrad Bsa"1ler#DArra,+hadow sam)ler, vec( B,
vec# *B*", vec# *B*/C
!vec( textureDrad B!sa"1ler$u:eArra, sam)ler, vec( B,
vec% *B*", vec% *B*/C
Do a te2ture loou1 as in
texture :ut with e21licit
!radients. 0he 1artial
derivatives o& B are with res1ect
to window 2 and window ,. +et
s
"
=
{
B
"
&or a 1D te2ture
B.s
"
otherwise
s
/
=
{
B
/
&or a 1D te2ture
B.s
/
otherwise
t
"
=
{
).) &or a 1D te2ture
B.t
"
otherwise
t
/
=
{
).) &or a 1D te2ture
B.t
/
otherwise
r
"
=
{
).) &or 1D or #D
B.)
"
cu:e, other
r
/
=
{
).) &or 1D or #D
B.)
/
cu:e, other
5or the cu:e version, the 1artial
derivatives o& B are assu"ed to
:e in the coordinate s,ste" used
:e&ore te2ture coordinates are
1roEected onto the a11ro1riate
cu:e &ace.
156
8 !ui%t+in ,unction"
S&nta, +escription
!vec( textureDradOffset B!sa"1ler1D sam)ler. &loat B.
&loat *B*", &loat *B*/. int o&&setC
!vec( textureDradOffset B!sa"1ler#D sam)ler, vec# B,
vec# *B*", vec# *B*/. ivec# o&&setC
!vec( textureDradOffset B!sa"1ler%D sam)ler, vec% B,
vec% *B*", vec% *B*/. ivec% o&&setC
!vec( textureDradOffset B!sa"1ler#DRect sam)ler, vec# B,
vec# *B*", vec# *B*/. ivec# o&&setC
&loat textureDradOffset Bsa"1ler#DRect+hadow sam)ler, vec% B,
vec# *B*", vec# *B*/. ivec# o&&setC
&loat textureDradOffset Bsa"1ler1D+hadow sam)ler. vec% B,
&loat *B*", &loat *B*/. int o&&set C
&loat textureDradOffset Bsa"1ler#D+hadow sam)ler. vec% B,
vec# *B*", vec# *B*/. ivec# o&&setC
!vec( textureDradOffset B!sa"1ler1DArra, sam)ler. vec# B.
&loat *B*", &loat *B*/. int o&&setC
!vec( textureDradOffset B!sa"1ler#DArra, sam)ler, vec% B,
vec# *B*", vec# *B*/. ivec# o&&setC
&loat textureDradOffset Bsa"1ler1DArra,+hadow sam)ler. vec%
B,
&loat *B*", &loat *B*/. int o&&setC
&loat textureDradOffset Bsa"1ler#DArra,+hadow sam)ler. vec(
B,
vec# *B*", vec# *B*/. ivec# o&&setC
Do a te2ture loou1 with :oth
e21licit !radient and o&&set, as
descri:ed in textureDrad and
textureOffset.
!vec( texture'roLDrad B!sa"1ler1D sam)ler. vec# B,
&loat *B*", &loat *B*/C
!vec( texture'roLDrad B!sa"1ler1D sam)ler. vec( B,
&loat *B*", &loat *B*/C
!vec( texture'roLDrad B!sa"1ler#D sam)ler. vec% B,
vec# *B*", vec# *B*/C
!vec( texture'roLDrad B!sa"1ler#D sam)ler. vec( B,
vec# *B*", vec# *B*/C
!vec( texture'roLDrad B!sa"1ler%D sam)ler. vec( B,
vec% *B*", vec% *B*/C
!vec( texture'roLDrad B!sa"1ler#DRect sam)ler. vec% B,
vec# *B*", vec# *B*/C
!vec( texture'roLDrad B!sa"1ler#DRect sam)ler. vec( B,
vec# *B*", vec# *B*/C
&loat texture'roLDrad Bsa"1ler#DRect+hadow sam)ler. vec( B,
vec# *B*", vec# *B*/C
&loat texture'roLDrad Bsa"1ler1D+hadow sam)ler, vec( B,
&loat *B*", &loat *B*/C
&loat texture'roLDrad Bsa"1ler#D+hadow sam)ler, vec( B,
vec# *B*", vec# *B*/C
Do a te2ture loou1 :oth
1roEectivel,, as descri:ed in
texture'roL, and with e21licit
!radient as descri:ed in
textureDrad. 0he 1artial
derivatives *B*" and *B*/ are
assu"ed to :e alread, 1roEected.
157
8 !ui%t+in ,unction"
S&nta, +escription
!vec( texture'roLDradOffset B!sa"1ler1D sam)ler. vec# B,
&loat *B*", &loat *B*/, int o&&setC
!vec( texture'roLDradOffset B!sa"1ler1D sam)ler. vec( B,
&loat *B*", &loat *B*/, int o&&setC
!vec( texture'roLDradOffset B!sa"1ler#D sam)ler. vec% B,
vec# *B*", vec# *B*/, ivec# o&&setC
!vec( texture'roLDradOffset B!sa"1ler#D sam)ler. vec( B,
vec# *B*", vec# *B*/, ivec# o&&setC
!vec( texture'roLDradOffset B!sa"1ler#DRect sam)ler, vec% B,
vec# *B*", vec# *B*/. ivec# o&&setC
!vec( texture'roLDradOffset B!sa"1ler#DRect sam)ler, vec( B,
vec# *B*", vec# *B*/. ivec# o&&setC
&loat texture'roLDradOffset Bsa"1ler#DRect+hadow sam)ler,
vec( B,
vec# *B*", vec# *B*/. ivec# o&&setC
!vec( texture'roLDradOffset B!sa"1ler%D sam)ler. vec( B,
vec% *B*", vec% *B*/, ivec% o&&setC
&loat texture'roLDradOffset Bsa"1ler1D+hadow sam)ler, vec( B,
&loat *B*", &loat *B*/, int o&&setC
&loat texture'roLDradOffset Bsa"1ler#D+hadow sam)ler, vec( B,
vec# *B*", vec# *B*/, ivec# o&&setC
Do a te2ture loou1 1roEectivel,
and with e21licit !radient as
descri:ed in texture'roLDrad,
as well as with o&&set, as
descri:ed in textureOffset.
4.9.# Te,ture Gather .unctions
0he te2ture !ather &unctions tae co"1onents o& a sin!le &loatin!91oint vector o1erand as a te2ture
coordinate, deter"ine a set o& &our te2els to sa"1le &ro" the :ase level o& detail o& the s1eci&ied te2ture
i"a!e, and return one co"1onent &ro" each te2el in a &our9co"1onent result vector.
@hen 1er&or"in! a te2ture !ather o1eration, the "ini&ication and "a!ni&ication &ilters are i!nored, and
the rules &or LI8EAR &ilterin! in the -1enGL +1eci&ication are a11lied to the :ase level o& the te2ture
i"a!e to identi&, the &our te2els i0>1. i1>1. i1>0. and i0>0. 0he te2els are then converted to te2ture :ase colors
BR
s
, -
s
, %
s
, #
s
C accordin! to ta:le 1..1, &ollowed :, a11lication o& the te2ture swi==le as descri:ed in
section 1..#.1 G0e2ture AccessH o& the -1enGL Gra1hics +,ste" +1eci&ication. A &our9co"1onent
vector is asse":led :, tain! the selected co"1onent &ro" each o& the 1ost9swi==led te2ture source colors
in the order Bi0>1. i1>1. i1>0. i0>0C.
5or te2ture !ather &unctions usin! a shadow sa"1ler t,1e, each o& the &our te2el loou1s 1er&or" a de1th
co"1arison a!ainst the de1th re&erence value 1assed in Bre&HC, and returns the result o& that co"1arison in
the a11ro1riate co"1onent o& the result vector.
As with other te2ture loou1 &unctions, the results o& a te2ture !ather are unde&ined &or shadow sa"1lers i&
the te2ture re&erenced is not a de1th te2ture or has de1th co"1arisons disa:ledJ or &or non9shadow
sa"1lers i& the te2ture re&erenced is a de1th te2ture with de1th co"1arisons ena:led.
158
8 !ui%t+in ,unction"
S&nta, +escription
!vec( textureDat"erB!sa"1ler#D sam)ler, vec# B
L, int com)MC
!vec( textureDat"erB!sa"1ler#DArra, sam)ler,
vec% B L, int com)MC
!vec( textureDat"erB!sa"1ler$u:e sam)ler,
vec% B L, int com)MC
!vec( textureDat"erB!sa"1ler$u:eArra, sam)ler,
vec( BL, int com)MC
!vec( textureDat"erB!sa"1ler#DRect sam)ler,
vec# BL, int com)MC
vec( textureDat"erBsa"1ler#D+hadow sam)ler,
vec# B, &loat re&HC
vec( textureDat"erBsa"1ler#DArra,+hadow sam)ler,
vec% B, &loat re&HC
vec( textureDat"erBsa"1ler$u:e+hadow sam)ler,
vec% B, &loat re&HC
vec( textureDat"erB
sa"1ler$u:eArra,+hadow sam)ler,
vec( B, &loat re&HCJ
vec( textureDat"erBsa"1ler#DRect+hadow sam)ler,
vec# B, &loat re&HCJ
Returns the value
vec(B+a"1le?i)?E1BB, :aseC.com),
+a"1le?i1?E1BB, :aseC.com),
+a"1le?i1?E)BB, :aseC.com),
+a"1le?i)?E)BB, :aseC.com)C
I& s1eci&ied, the value o& com) "ust :e a
constant inte!er e21ression with a value o& ),
1, #, or %, identi&,in! the ", /, (, or 2 1ost9
swi==led co"1onent o& the &our9co"1onent
vector loou1 result &or each te2el,
res1ectivel,. I& com) is not s1eci&ied, it is
treated as ), selectin! the " co"1onent o&
each te2el to !enerate the result.
!vec( textureDat"erOffsetB!sa"1ler#D sam)ler,
vec# B, ivec# o&&set
L, int com)MC
!vec( textureDat"erOffsetB!sa"1ler#DArra, sam)ler,
vec% B, ivec# o&&set
L, int com)MC
!vec( textureDat"erOffsetB!sa"1ler#DRect sam)ler.
vec# B, ivec# o&&set
L, int com)MC
vec( textureDat"erOffsetB
sa"1ler#D+hadow sam)ler,
vec# B, &loat re&H, ivec# o&&setC
vec( textureDat"erOffsetB
sa"1ler#DArra,+hadow sam)ler,
vec% B, &loat re&H, ivec# o&&setC
vec( textureDat"erOffsetB
sa"1ler#DRect+hadow sam)ler,
vec# B, &loat re&H, ivec# o&&setC
3er&or" a te2ture !ather o1eration as in
textureDat"er :, o&&set as descri:ed in
textureOffset e2ce1t that the o&&set can :e
varia:le Bnon constantC and the
i"1le"entation9de1endent "ini"u" and
"a2i"u" o&&set values are !iven :,
;I8?3R-GRA;?0E\07RE?GA0/ER?-55+E0
and
;A\?3R-GRA;?0E\07RE?GA0/ER?-55+E0,
res1ectivel,.
159
8 !ui%t+in ,unction"
S&nta, +escription
!vec( textureDat"erOffsetsB
!sa"1ler#D sam)ler,
vec# B, ivec# o&&setsL(M
L, int com)MC
!vec( textureDat"erOffsetsB
!sa"1ler#DArra, sam)ler,
vec% B, ivec# o&&setsL(M
L, int com)MC
!vec( textureDat"erOffsetsB
!sa"1ler#DRect sam)ler.
vec% B, ivec# o&&setsL(M
L, int com)MC
vec( textureDat"erOffsetsB
sa"1ler#D+hadow sam)ler,
vec# B, &loat re&H, ivec# o&&setsL(MC
vec( textureDat"erOffsetsB
sa"1ler#DArra,+hadow sam)ler,
vec% B, &loat re&H, ivec# o&&setsL(MC
vec( textureDat"erOffsetsB
sa"1ler#DRect+hadow sam)ler,
vec# B, &loat re&H, ivec# o&&setsL(MC
-1erate identicall, to textureDat"erOffset
e2ce1t that o&&sets is used to deter"ine the
location o& the &our te2els to sa"1le. Each
o& the &our te2els is o:tained :, a11l,in! the
corres1ondin! o&&set in o&&sets as a Bu. vC
coordinate o&&set to B, identi&,in! the &our9
te2el LI8EAR &oot1rint, and then selectin!
the te2el i0>0 o& that &oot1rint. 0he s1eci&ied
values in o&&sets "ust :e set with constant
inte!ral e21ressions.
16
8 !ui%t+in ,unction"
4.9." Compatibilit& -rofile Te,ture .unctions
0he &ollowin! te2ture &unctions are onl, in the co"1ati:ilit, 1ro&ile.
S&nta, =deprecated> +escription =deprecated>
vec( texture1C Bsa"1ler1D sam)ler.
&loat coor* L, &loat biasM C
vec( texture1C'roL Bsa"1ler1D sam)ler,
vec# coor* L, &loat biasM C
vec( texture1C'roL Bsa"1ler1D sam)ler,
vec( coor* L, &loat biasM C
vec( texture1C$od Bsa"1ler1D sam)ler,
&loat coor*, &loat lo*C
vec( texture1C'roL$od Bsa"1ler1D sam)ler,
vec# coor*, &loat lo*C
vec( texture1C'roL$od Bsa"1ler1D sam)ler,
vec( coor*, &loat lo*C
+ee corres1ondin! si!nature a:ove without
G1DH in the na"e.
vec( textureGC Bsa"1ler#D sam)ler,
vec# coor* L, &loat biasM C
vec( textureGC'roL Bsa"1ler#D sam)ler,
vec% coor* L, &loat biasM C
vec( textureGC'roL Bsa"1ler#D sam)ler,
vec( coor* L, &loat biasM C
vec( textureGC$od Bsa"1ler#D sam)ler,
vec# coor*, &loat lo*C
vec( textureGC'roL$od Bsa"1ler#D sam)ler,
vec% coor*, &loat lo*C
vec( textureGC'roL$od Bsa"1ler#D sam)ler,
vec( coor*, &loat lo*C
+ee corres1ondin! si!nature a:ove without
G#DH in the na"e.
vec( textureFC Bsa"1ler%D sam)ler,
vec% coor* L, &loat biasM C
vec( textureFC'roL Bsa"1ler%D sam)ler,
vec( coor* L, &loat biasM C
vec( textureFC$od Bsa"1ler%D sam)ler,
vec% coor*, &loat lo*C
vec( textureFC'roL$od Bsa"1ler%D sam)ler,
vec( coor*, &loat lo*C
+ee corres1ondin! si!nature a:ove without
G%DH in the na"e.
7se the te2ture coordinate coor* to do a
te2ture loou1 in the %D te2ture currentl,
:ound to sam)ler. 5or the 1roEective
BG'roLHC versions, the te2ture coordinate is
divided :, coor*.3.
vec( textureHube Bsa"1ler$u:e sam)ler,
vec% coor* L, &loat biasM C
vec( textureHube$od Bsa"1ler$u:e sam)ler,
vec% coor*, &loat lo*C
+ee corres1ondin! si!nature a:ove without
G$u:eH in the na"e.
161
8 !ui%t+in ,unction"
S&nta, =deprecated> +escription =deprecated>
vec( s"adow1C Bsa"1ler1D+hadow sam)ler.
vec% coor* L, &loat biasM C
vec( s"adowGC Bsa"1ler#D+hadow sam)ler.
vec% coor* L, &loat biasM C
vec( s"adow1C'roL Bsa"1ler1D+hadow sam)ler.
vec( coor* L, &loat biasM C
vec( s"adowGC'roL Bsa"1ler#D+hadow sam)ler.
vec( coor* L, &loat biasM C
vec( s"adow1C$od Bsa"1ler1D+hadow sam)ler.
vec% coor*, &loat lo*C
vec( s"adowGC$od Bsa"1ler#D+hadow sam)ler.
vec% coor*, &loat lo*C
vec( s"adow1C'roL$odBsa"1ler1D+hadow sam)ler.
vec( coor*, &loat lo*C
vec( s"adowGC'roL$odBsa"1ler#D+hadow sam)ler.
vec( coor*. &loat lo*C
+a"e &unctionalit, as the Gtexture^ :ased
na"es a:ove with the sa"e si!nature.
162
8 !ui%t+in ,unction"
4.1$ 0tomic7Counter .unctions
0he ato"ic9counter o1erations in this section o1erate ato"icall, with res1ect to each other. 0he, are
ato"ic &or an, sin!le counter, "eanin! an, o& these o1erations on a s1eci&ic counter in one shader
instantiation will :e indivisi:le :, an, o& these o1erations on the sa"e counter &ro" another shader
instantiation. 0here is no !uarantee that these o1erations are ato"ic with res1ect to other &or"s o& access
to the counter or that the, are seriali=ed when a11lied to se1arate counters. +uch cases would re<uire
additional use o& &ences, :arriers, or other &or"s o& s,nchroni=ation, i& ato"icit, or seriali=ation is desired.
0he value returned :, an ato"ic9counter &unction is the value o& an ato"ic counter, which "a, :e
returned and incre"ented in an ato"ic o1eration, or
decre"ented and returned in an ato"ic o1eration, or
si"1l, returned.
0he underl,in! counter is a %#9:it unsi!ned inte!er. Incre"ents and decre"ents at the li"it o& the ran!e
will wra1 to L), #
%#
91M.
S&nta, +escription
uint atomicHounterPncrement Bato"ic?uint cC Ato"icall,
1. incre"ents the counter &or c, and
#. returns its value 1rior to the incre"ent
o1eration.
0hese two ste1s are done ato"icall, with res1ect to
the ato"ic counter &unctions in this ta:le.
uint atomicHounterCecrement Bato"ic?uint cC Ato"icall,
1. decre"ents the counter &or c, and
#. returns the value resultin! &ro" the
decre"ent o1eration.
0hese two ste1s are done ato"icall, with res1ect to
the ato"ic counter &unctions in this ta:le.
uint atomicHounter Bato"ic?uint cC Returns the counter value &or c.
4.11 0tomic 8emor& .unctions
Ato"ic "e"or, &unctions 1er&or" ato"ic o1erations on an individual si!ned or unsi!ned inte!er stored in
:u&&er9o:Eect or shared9varia:le stora!e. All o& the ato"ic "e"or, o1erations read a value &ro" "e"or,,
co"1ute a new value usin! one o& the o1erations descri:ed :elow, write the new value to "e"or,, and
return the ori!inal value read. 0he contents o& the "e"or, :ein! u1dated :, the ato"ic o1eration are
!uaranteed not to :e "odi&ied :, an, other assi!n"ent or ato"ic "e"or, &unction in an, shader
invocation :etween the ti"e the ori!inal value is read and the ti"e the new value is written.
163
8 !ui%t+in ,unction"
Ato"ic "e"or, &unctions are su11orted onl, &or a li"ited set o& varia:les. A shader will &ail to co"1ile
i& the value 1assed to the mem ar!u"ent o& an ato"ic "e"or, &unction does not corres1ond to a :u&&er or
shared varia:le. It is acce1ta:le to 1ass an ele"ent o& an arra, or a sin!le co"1onent o& a vector to the
mem ar!u"ent o& an ato"ic "e"or, &unction, as lon! as the underl,in! arra, or vector is a :u&&er or
shared varia:le.
S&nta, +escription
uint atomicAdd Binout uint mem, uint *ataC
int atomicAdd Binout int mem, int *ataC
$o"1utes a new value :, addin! the value o& *ata to
the contents mem.
uint atomicKin Binout uint mem, uint *ataC
int atomicKin Binout int mem, int *ataC
$o"1utes a new value :, tain! the "ini"u" o& the
value o& *ata and the contents o& mem.
uint atomicKax Binout uint mem, uint *ataC
int atomicKax Binout int mem, int *ataC
$o"1utes a new value :, tain! the "a2i"u" o& the
value o& *ata and the contents o& mem.
uint atomicAnd Binout uint mem, uint *ataC
int atomicAnd Binout int mem, int *ataC
$o"1utes a new value :, 1er&or"in! a :it9wise
A8D o& the value o& *ata and the contents o& mem.
uint atomicOr Binout uint mem, uint *ataC
int atomicOr Binout int mem, int *ataC
$o"1utes a new value :, 1er&or"in! a :it9wise -R
o& the value o& *ata and the contents o& mem.
uint atomicWor Binout uint mem, uint *ataC
int atomicWor Binout int mem, int *ataC
$o"1utes a new value :, 1er&or"in! a :it9wise
E\$L7+IVE -R o& the value o& *ata and the
contents o& mem.
uint atomic&xc"ange Binout uint mem, uint *ataC
int atomic&xc"ange Binout int mem, int *ataC
$o"1utes a new value :, si"1l, co1,in! the value
o& *ata.
uint atomicHompSwap Binout uint mem,
uint com)are, uint *ataC
int atomicHompSwap Binout int mem,
int com)are, int *ataC
$o"1ares the value o& com)are and the contents o&
mem. I& the values are e<ual, the new value is !iven
:, *ataJ otherwise, it is taen &ro" the ori!inal
contents o& mem.
4.12 Image .unctions
Varia:les usin! one o& the i"a!e :asic t,1es "a, :e used :, the :uilt9in shader i"a!e "e"or, &unctions
de&ined in this section to read and write individual te2els o& a te2ture. Each i"a!e varia:le re&erences an
i"a!e unit, which has a te2ture i"a!e attached.
@hen i"a!e "e"or, &unctions :elow access "e"or,, an individual te2el in the i"a!e is identi&ied usin!
an BiC, Bi. >C, or Bi. >. ,C coordinate corres1ondin! to the values o& B. 5or imageGCKS and
imageGCKSArray varia:les Band the corres1ondin! intDunsi!ned int t,1esC corres1ondin! to "ulti9
sa"1le te2tures, each te2el "a, have "ulti1le sa"1les and an individual sa"1le is identi&ied usin! the
164
8 !ui%t+in ,unction"
inte!er sam)le 1ara"eter. 0he coordinates and sa"1le nu":er are used to select an individual te2el in
the "anner descri:ed in section 4.#. G0e2ture I"a!e Loads and +toresH o& the -1enGL s1eci&ication.
Loads and stores su11ort &loat, inte!er, and unsi!ned inte!er t,1es. 0he data t,1es :elow startin!
GgimageH serve as 1laceholders "eanin! t,1es startin! either GimageH, GiimageH, or GuimageH in the sa"e
wa, as gvec or gsam)ler in earlier sections.
0he 0!#-8B#R#!' in the 1rotot,1es :elow is a 1laceholder re1resentin! %% se1arate &unctions, each
&or a di&&erent t,1e o& i"a!e varia:le. 0he 0!#-8B#R#!' 1laceholder is re1laced :, one o& the
&ollowin! 1ara"eter lists:
!i"a!e1D image, int B
!i"a!e#D image, ivec# B
!i"a!e%D image, ivec% B
!i"a!e#DRect image, ivec# B
!i"a!e$u:e image, ivec% B
!i"a!eBu&&er image, int B
!i"a!e1DArra, image, ivec# B
!i"a!e#DArra, image, ivec% B
!i"a!e$u:eArra, image, ivec% B
!i"a!e#D;+ image, ivec# B, int sam)le
!i"a!e#D;+Arra, image, ivec% B, int sam)le
where each o& the lines re1resents one o& three di&&erent i"a!e varia:le t,1es, and image, B, and sam)le
s1eci&, the individual te2el to o1erate on. 0he "ethod &or identi&,in! the individual te2el o1erated on
&ro" image, B, and sam)le, and the "ethod &or readin! and writin! the te2el are s1eci&ied in section 4.#.
G0e2ture I"a!e Loads and +toresH o& the -1enGL s1eci&ication.
0he ato"ic &unctions 1er&or" ato"ic o1erations on individual te2els or sa"1les o& an i"a!e varia:le.
Ato"ic "e"or, o1erations read a value &ro" the selected te2el, co"1ute a new value usin! one o& the
o1erations descri:ed :elow, write the new value to the selected te2el, and return the ori!inal value read.
0he contents o& the te2el :ein! u1dated :, the ato"ic o1eration are !uaranteed not to :e "odi&ied :, an,
other i"a!e store or ato"ic &unction :etween the ti"e the ori!inal value is read and the ti"e the new
value is written.
Ato"ic "e"or, o1erations are su11orted on onl, a su:set o& all i"a!e varia:le t,1esJ image "ust :e
either:
a si!ned inte!er i"a!e varia:le Bt,1e starts GiimageHC and a &or"at <uali&ier o& r32i, used with a
*ata ar!u"ent o& t,1e int. or
an unsi!ned i"a!e varia:le Bt,1e starts GuimageHC and a &or"at <uali&ier o& r32ui, used with a
*ata ar!u"ent o& t,1e uint.
165
8 !ui%t+in ,unction"
S&nta, +escription
int imageSize Breadonl, writeonl,
!i"a!e1D i"a!eC
ivec# imageSize Breadonl, writeonl,
!i"a!e#D i"a!eC
ivec% imageSize Breadonl, writeonl,
!i"a!e%D i"a!eC
ivec# imageSize Breadonl, writeonl,
!i"a!e$u:e i"a!eC
ivec% imageSize Breadonl, writeonl,
!i"a!e$u:eArra, i"a!eC
ivec# imageSize Breadonl, writeonl,
!i"a!eRect i"a!eC
ivec# imageSize Breadonl, writeonl,
!i"a!e1DArra, i"a!eC
ivec% imageSize Breadonl, writeonl,
!i"a!e#DArra, i"a!eC
int imageSize Breadonl, writeonl,
!i"a!eBu&&er i"a!eC
ivec# imageSize Breadonl, writeonl,
!i"a!e#D;+ i"a!eC
ivec% imageSize Breadonl, writeonl,
!i"a!e#D;+Arra, i"a!eC
Returns the di"ensions o& the i"a!e or i"a!es
:ound to image. 5or arra,ed i"a!es, the last
co"1onent o& the return value will hold the si=e o&
the arra,. $u:e i"a!es onl, return the di"ensions
o& one &ace, and the nu":er o& cu:es in the cu:e "a1
arra,, i& arra,ed.
8ote: 0he <uali&ication readonly writeonly acce1ts
a varia:le <uali&ied with readonly, writeonly, :oth,
or neither. It "eans the &or"al ar!u"ent will :e
used &or neither readin! nor writin! to the underl,in!
"e"or,.
!vec( image$oad Breadonl, 0!#-8B#R#!'C Loads the te2el at the coordinate B &ro" the i"a!e
unit image Bin 0!#-8B#R#!'C. 5or "ulti9sa"1le
loads, the sa"1le nu":er is !iven :, sam)le. @hen
image, B, sam)le identi&, a valid te2el, the :its used
to re1resent the selected te2el in "e"or, are
converted to a vecE, ivecE, or uvecE in the "anner
descri:ed in section 4.#. G0e2ture I"a!e Loads and
+toresH o& the -1enGL +1eci&ication and returned.
void imageStore Bwriteonl, 0!#-8B#R#!',
!vec( *ataC
+tores *ata into the te2el at the coordinate B &ro"
the i"a!e s1eci&ied :, image. 5or "ulti9sa"1le
stores, the sa"1le nu":er is !iven :, sam)le. @hen
image, B, and sam)le identi&, a valid te2el, the :its
used to re1resent *ata are converted to the &or"at o&
the i"a!e unit in the "anner descri:ed in section
4.#. G0e2ture I"a!e Loads and +toresH o& the
-1enGL +1eci&ication and stored to the s1eci&ied
te2el.
uint imageAtomicAdd B0!#-8B#R#!',
uint *ataC
int imageAtomicAdd B0!#-8B#R#!',
int *ataC
$o"1utes a new value :, addin! the value o& *ata
to the contents o& the selected te2el.
166
8 !ui%t+in ,unction"
S&nta, +escription
uint imageAtomicKin B0!#-8B#R#!',
uint *ataC
int imageAtomicKin B0!#-8B#R#!',
int *ataC
$o"1utes a new value :, tain! the "ini"u" o& the
value o& *ata and the contents o& the selected te2el.
uint imageAtomicKax B0!#-8B#R#!',
uint *ataC
int imageAtomicKax B0!#-8B#R#!',
int *ataC
$o"1utes a new value :, tain! the "a2i"u" o& the
value *ata and the contents o& the selected te2el.
uint imageAtomicAnd B0!#-8B#R#!',
uint *ataC
int imageAtomicAnd B0!#-8B#R#!',
int *ataC
$o"1utes a new value :, 1er&or"in! a :it9wise
A8D o& the value o& *ata and the contents o& the
selected te2el.
uint imageAtomicOr B0!#-8B#R#!',
uint *ataC
int imageAtomicOr B0!#-8B#R#!',
int *ataC
$o"1utes a new value :, 1er&or"in! a :it9wise -R
o& the value o& *ata and the contents o& the selected
te2el.
uint imageAtomicWor B0!#-8B#R#!',
uint *ataC
int imageAtomicWor B0!#-8B#R#!',
int *ataC
$o"1utes a new value :, 1er&or"in! a :it9wise
E\$L7+IVE -R o& the value o& *ata and the
contents o& the selected te2el.
uint imageAtomic&xc"ange B0!#-8B#R#!',
uint *ataC
int imageAtomic&xc"ange B0!#-8B#R#!',
int *ataC
$o"1utes a new value :, si"1l, co1,in! the value
o& *ata.
uint imageAtomicHompSwap
B0!#-8B#R#!',
uint com)are,
uint *ataC
int imageAtomicHompSwap
B0!#-8B#R#!',
int com)are,
int *ataC
$o"1ares the value o& com)are and the contents o&
the selected te2el. I& the values are e<ual, the new
value is !iven :, *ataJ otherwise, it is taen &ro" the
ori!inal value loaded &ro" the te2el.
167
8 !ui%t+in ,unction"
4.1# .ragment -rocessing .unctions
5ra!"ent 1rocessin! &unctions are onl, availa:le in &ra!"ent shaders.
4.1#.1 +erivative .unctions
Derivatives "a, :e co"1utationall, e21ensive andDor nu"ericall, unsta:le. 0here&ore, an -1enGL
i"1le"entation "a, a11ro2i"ate the true derivatives :, usin! a &ast :ut not entirel, accurate derivative
co"1utation. Derivatives are unde&ined within non9uni&or" control &low.
0he e21ected :ehavior o& a derivative is s1eci&ied usin! &orwardD:acward di&&erencin!.
5orward di&&erencin!:
F( "+*")F( ") *F*"( ")*" 1a
*F*"(")
F( "+*")F( ")
*"
1:
Bacward di&&erencin!:
F( "*")F( ") *F*"( ")*" #a
*F*"(")
F( ")F( "*")
*"
#:
@ith sin!le9sa"1le rasteri=ation, *" TV 1.) in e<uations 1: and #:. 5or "ulti9sa"1le rasteri=ation, *" T
#.) in e<uations 1: and #:.
dNdy is a11ro2i"ated si"ilarl,, with / re1lacin! ".
A GL i"1le"entation "a, use the a:ove or other "ethods to 1er&or" the calculation, su:Eect to the
&ollowin! conditions:
1. 0he "ethod "a, use 1iecewise linear a11ro2i"ations. +uch linear a11ro2i"ations i"1l, that hi!her
order derivatives, dNdxBdNdxB"CC and a:ove, are unde&ined.
#. 0he "ethod "a, assu"e that the &unction evaluated is continuous. 0here&ore derivatives within non9
uni&or" control &low are unde&ined.
%. 0he "ethod "a, di&&er 1er &ra!"ent, su:Eect to the constraint that the "ethod "a, var, :, window
coordinates, not screen coordinates. 0he invariance re<uire"ent descri:ed in section 1(.#
GInvarianceH o& the -1enGL Gra1hics +,ste" +1eci&ication, is rela2ed &or derivative calculations,
:ecause the "ethod "a, :e a &unction o& &ra!"ent location.
-ther 1ro1erties that are desira:le, :ut not re<uired, are:
(. 5unctions should :e evaluated within the interior o& a 1ri"itive Binter1olated, not e2tra1olatedC.
168
8 !ui%t+in ,unction"
.. 5unctions &or dNdx should :e evaluated while holdin! , constant. 5unctions &or dNdy should :e
evaluated while holdin! 2 constant. /owever, "i2ed hi!her order derivatives, lie dNdxBdNdyB/CC
and dNdyBdNdxB"CC are unde&ined.
*. Derivatives o& constant ar!u"ents should :e ).
In so"e i"1le"entations, var,in! de!rees o& derivative accurac, "a, :e o:tained :, 1rovidin! GL hints
Bsection #1.( G/intsH o& the -1enGL Gra1hics +,ste" +1eci&icationC, allowin! a user to "ae an i"a!e
<ualit, versus s1eed trade o&&.
S&nta, +escription
!en0,1e dNdx B!en0,1e )C Returns the derivative in 2 usin! local di&&erencin! &or
the in1ut ar!u"ent ).
!en0,1e dNdy B!en0,1e )C Returns the derivative in , usin! local di&&erencin! &or
the in1ut ar!u"ent ).
0hese two &unctions are co""onl, used to esti"ate the
&ilter width used to anti9alias 1rocedural te2tures. @e
are assu"in! that the e21ression is :ein! evaluated in
1arallel on a +I;D arra, so that at an, !iven 1oint in
ti"e the value o& the &unction is nown at the !rid 1oints
re1resented :, the +I;D arra,. Local di&&erencin!
:etween +I;D arra, ele"ents can there&ore :e used to
derive d5d2, d5d,, etc.
!en0,1e fwidt" B!en0,1e )C Returns the su" o& the a:solute derivative in 2 and ,
usin! local di&&erencin! &or the in1ut ar!u"ent ), i.e.,
abs BdNdx B)CC I abs BdNdy B)CCJ
4.1#.2 Interpolation .unctions
Built9in inter1olation &unctions are availa:le to co"1ute an inter1olated value o& a &ra!"ent shader in1ut
varia:le at a shader9s1eci&ied B", /C location. A se1arate B", /C location "a, :e used &or each invocation o&
the :uilt9in &unction, and those locations "a, di&&er &ro" the de&ault B", /C location used to 1roduce the
de&ault value o& the in1ut.
5or all o& the inter1olation &unctions, inter)olant "ust :e an in1ut varia:le or an ele"ent o& an in1ut
varia:le declared as an arra,. $o"1onent selection o1erators Be.!., ."/C "a, not :e used when s1eci&,in!
inter)olant. I& inter)olant is declared with a flat or centroid <uali&ier, the <uali&ier will have no e&&ect on
the inter1olated value. I& inter)olant is declared with the noperspective <uali&ier, the inter1olated value
will :e co"1uted without 1ers1ective correction.
169
8 !ui%t+in ,unction"
S&nta, +escription
&loat interpolateAtHentroid B&loat inter)olantC
vec# interpolateAtHentroid Bvec# inter)olantC
vec% interpolateAtHentroid Bvec% inter)olantC
vec( interpolateAtHentroid Bvec( inter)olantC
Returns the value o& the in1ut inter)olant sa"1led at
a location inside :oth the 1i2el and the 1ri"itive
:ein! 1rocessed. 0he value o:tained would :e the
sa"e value assi!ned to the in1ut varia:le i& declared
with the centroid <uali&ier.
&loat interpolateAtSample B&loat inter)olant,
int sam)leC
vec# interpolateAtSample Bvec# inter)olant,
int sam)leC
vec% interpolateAtSample Bvec% inter)olant,
int sam)leC
vec( interpolateAtSample Bvec( inter)olant,
int sam)leC
Returns the value o& the in1ut inter)olant varia:le at
the location o& sa"1le nu":er sam)le. I&
"ultisa"1le :u&&ers are not availa:le, the in1ut
varia:le will :e evaluated at the center o& the 1i2el.
I& sa"1le sam)le does not e2ist, the 1osition used to
inter1olate the in1ut varia:le is unde&ined.
&loat interpolateAtOffset B&loat inter)olant,
vec# o&&setC
vec# interpolateAtOffset Bvec# inter)olant,
vec# o&&setC
vec% interpolateAtOffset Bvec% inter)olant,
vec# o&&setC
vec( interpolateAtOffset Bvec( inter)olant,
vec# o&&setC
Returns the value o& the in1ut inter)olant varia:le
sa"1led at an o&&set &ro" the center o& the 1i2el
s1eci&ied :, o&&set. 0he two &loatin!91oint
co"1onents o& o&&set, !ive the o&&set in 1i2els in the "
and / directions, res1ectivel,. An o&&set o& B), )C
identi&ies the center o& the 1i2el. 0he ran!e and
!ranularit, o& o&&sets su11orted :, this &unction is
i"1le"entation9de1endent.
4.1" 6oise .unctions
8oise &unctions are availa:le &ra!"ent, !eo"etr,, and verte2 shadersto in all shader sta!es. 0he, are
stochastic &unctions that can :e used to increase visual co"1le2it,. Values returned :, the &ollowin! noise
&unctions !ive the a11earance o& rando"ness, :ut are not trul, rando". 0he noise &unctions :elow are
de&ined to have the &ollowin! characteristics:
0he return valueBsC are alwa,s in the ran!e L91.),1.)M, and cover at least the ran!e L9).*, ).*M, with a
Gaussian9lie distri:ution.
0he return valueBsC have an overall avera!e o& ).).
0he, are re1eata:le, in that a 1articular in1ut value will alwa,s 1roduce the sa"e return value.
0he, are statisticall, invariant under rotation Bi.e., no "atter how the do"ain is rotated, it has the sa"e
statistical characterC.
0he, have a statistical invariance under translation Bi.e., no "atter how the do"ain is translated, it has
the sa"e statistical characterC.
0he, t,1icall, !ive di&&erent results under translation.
17
8 !ui%t+in ,unction"
0he s1atial &re<uenc, is narrowl, concentrated, centered so"ewhere :etween ).. to 1.).
0he, are $
1
continuous ever,where Bi.e., the &irst derivative is continuousC.
S&nta, +escription
&loat noise1 B!en0,1e "C Returns a 1D noise value :ased on the in1ut value ".
vec# noiseG B!en0,1e "C Returns a #D noise value :ased on the in1ut value ".
vec% noiseF B!en0,1e "C Returns a %D noise value :ased on the in1ut value ".
vec( noiseE B!en0,1e "C Returns a (D noise value :ased on the in1ut value ".
171
8 !ui%t+in ,unction"
4.1* Geometr& Shader .unctions
0hese &unctions are onl, availa:le in !eo"etr, shaders. 0he, are descri:ed in "ore de1th &ollowin! the
ta:le.
S&nta, +escription
void &mitStreamUertex Bint streamC E"its the current values o& out1ut varia:les to the current
out1ut 1ri"itive on strea" stream. 0he ar!u"ent to stream
"ust :e a constant inte!ral e21ression. -n return &ro" this
call, the values o& all out1ut varia:les are unde&ined.
$an onl, :e used i& "ulti1le out1ut strea"s are su11orted.
void &ndStream'rimitive Bint streamC $o"1letes the current out1ut 1ri"itive on strea" stream and
starts a new one. 0he ar!u"ent to stream "ust :e a constant
inte!ral e21ression. 8o verte2 is e"itted.
$an onl, :e used i& "ulti1le out1ut strea"s are su11orted.
void &mitUertex BC E"its the current values o& out1ut varia:les to the current
out1ut 1ri"itive. -n return &ro" this call, the values o&
out1ut varia:les are unde&ined.
@hen "ulti1le out1ut strea"s are su11orted, this is
e<uivalent to callin! &mitStreamUertexB)C.
void &nd'rimitive BC $o"1letes the current out1ut 1ri"itive and starts a new one.
8o verte2 is e"itted.
@hen "ulti1le out1ut strea"s are su11orted, this is
e<uivalent to callin! &ndStream'rimitiveB)C.
0he &unction &mitStreamUertexBC s1eci&ies that a verte2 is co"1leted. A verte2 is added to the current
out1ut 1ri"itive in verte2 strea" stream usin! the current values o& all out1ut varia:les associated with
stream. 0hese include glBoint'i(e, gl$li)Distance, glLa/er, glBosition, glBrimitive0D and
glVie2)ort0n*e". 0he values o& all out1ut varia:les &or all out1ut strea"s are unde&ined a&ter a call to
&mitStreamUertexBC. I& a !eo"etr, shader invocation has e"itted "ore vertices than 1er"itted :, the
out1ut la,out <uali&ier max%vertices, the results o& callin! &mitStreamUertexBC are unde&ined.
0he &unction &ndStream'rimitiveBC s1eci&ies that the current out1ut 1ri"itive &or verte2 strea" stream is
co"1leted and a new out1ut 1ri"itive Bo& the sa"e t,1eC will :e started :, an, su:se<uent
&mitStreamUertexBC. 0his &unction does not e"it a verte2. I& the out1ut la,out is declared to :e
G1ointsH, callin! &ndStream'rimitiveBC is o1tional.
A !eo"etr, shader starts with an out1ut 1ri"itive containin! no vertices &or each strea". @hen a
!eo"etr, shader ter"inates, the current out1ut 1ri"itive &or each strea" is auto"aticall, co"1leted. It is
not necessar, to call &ndStream'rimitiveBC i& the !eo"etr, shader writes onl, a sin!le 1ri"itive.
172
8 !ui%t+in ,unction"
;ulti1le out1ut strea"s are su11orted onl, i& the out1ut 1ri"itive t,1e is declared to :e )oints. A
1ro!ra" will &ail to lin i& it contains a !eo"etr, shader callin! &mitStreamUertexBC or
&ndStream'rimitiveBC i& its out1ut 1ri"itive t,1e is not )oints.
173
8 !ui%t+in ,unction"
4.1% Shader Invocation Control .unctions
0he shader invocation control &unction is availa:le onl, in tessellation control shaders and co"1ute
shaders. It is used to control the relative e2ecution order o& "ulti1le shader invocations used to 1rocess a
1atch Bin the case o& tessellation control shadersC or a local wor !rou1 Bin the case o& co"1ute shadersC,
which are otherwise e2ecuted with an unde&ined relative order.
S&nta, +escription
void barrier BC 5or an, !iven static instance o& barrierBC, all tessellation control shader
invocations &or a sin!le in1ut 1atch "ust enter it :e&ore an, will :e
allowed to continue :e,ond it, or all invocations &or a sin!le wor !rou1
"ust enter it :e&ore an, will continue :e,ond it.
0he &unction barrierBC 1rovides a 1artiall, de&ined order o& e2ecution :etween shader invocations. 0his
ensures that values written :, one invocation 1rior to a !iven static instance o& barrierBC can :e sa&el,
read :, other invocations a&ter their call to the sa"e static instance barrierBC. Because invocations "a,
e2ecute in unde&ined order :etween these :arrier calls, the values o& a 1er9verte2 or 1er91atch out1ut
varia:le or shared varia:les &or co"1ute shaders will :e unde&ined in a nu":er o& cases enu"erated in
section (.%.* G-ut1ut Varia:lesH B&or tessellation control shadersC and section (.%.4 Z+hared Varia:lesZ
B&or co"1ute shadersC.
5or tessellation control shaders, the barrierBC &unction "a, onl, :e 1laced inside the &unction mainBC o&
the tessellation control shader and "a, not :e called within an, control &low. Barriers are also disallowed
a&ter a return state"ent in the &unction mainBC. An, such "is1laced :arriers result in a co"1ile9ti"e
error.
5or co"1ute shaders, the barrier! &unction "a, :e 1laced within &low control, :ut that &low control "ust
:e uni&or" &low control. 0hat is, all the controllin! e21ressions that lead to e2ecution o& the :arrier "ust
:e d,na"icall, uni&or" e21ressions. 0his ensures that i& an, shader invocation enters a conditional
state"ent, then all invocations will enter it. @hile co"1ilers are encoura!ed to !ive warnin!s i& the, can
detect this "i!ht not ha11en, co"1ilers cannot co"1letel, deter"ine this. /ence, it is the authorFs
res1onsi:ilit, to ensure barrier! onl, e2ists inside uni&or" &low control. -therwise, so"e shader
invocations will stall inde&initel,, waitin! &or a :arrier that is never reached :, other invocations.
174
8 !ui%t+in ,unction"
4.1 Shader 8emor& Control .unctions
+haders o& all t,1es "a, read and write the contents o& te2tures and :u&&er o:Eects usin! i"a!e varia:les.
@hile the order o& reads and writes within a sin!le shader invocation is well9de&ined, the relative order o&
reads and writes to a sin!le shared "e"or, address &ro" "ulti1le se1arate shader invocations is lar!el,
unde&ined. 0he order o& "e"or, accesses 1er&or"ed :, one shader invocation, as o:served :, other
shader invocations, is also lar!el, unde&ined :ut can :e controlled throu!h "e"or, control &unctions.
S&nta, +escription
void memoryJarrier BC $ontrol the orderin! o& "e"or, transactions issued :, a
sin!le shader invocation.
void memoryJarrierAtomicHounter BC $ontrol the orderin! o& accesses to ato"ic9counter varia:les
issued :, a sin!le shader invocation.
void memoryJarrierJuffer BC $ontrol the orderin! o& "e"or, transactions to :u&&er
varia:les issued within a sin!le shader invocation.
void memoryJarrierS"ared BC $ontrol the orderin! o& "e"or, transactions to shared
varia:les issued within a sin!le shader invocation.
-nl, availa:le in co"1ute shaders.
void memoryJarrierPmage BC $ontrol the orderin! o& "e"or, transactions to i"a!es
issued within a sin!le shader invocation.
void groupKemoryJarrier BC $ontrol the orderin! o& all "e"or, transactions issued within
a sin!le shader invocation, as viewed :, other invocations in
the sa"e wor !rou1.
-nl, availa:le in co"1ute shaders.
0he "e"or, :arrier :uilt9in &unctions can :e used to order reads and writes to varia:les stored in "e"or,
accessi:le to other shader invocations. @hen called, these &unctions will wait &or the co"1letion o& all
reads and writes 1reviousl, 1er&or"ed :, the caller that access selected varia:le t,1es, and then return
with no other e&&ect. 0he :uilt9in &unctions memoryJarrierAtomicHounterBC, memoryJarrierJufferBC,
memoryJarrierPmageBC, and memoryJarrierS"aredBC wait &or the co"1letion o& accesses to ato"ic
counter, :u&&er, i"a!e, and shared varia:les, res1ectivel,. 0he :uilt9in &unctions memoryJarrierBC and
groupKemoryJarrierBC wait &or the co"1letion o& accesses to all o& the a:ove varia:le t,1es. 0he
&unctions memoryJarrierS"aredBC and groupKemoryJarrierBC are availa:le onl, in co"1ute shadersJ
the other &unctions are availa:le in all shader t,1es.
175
8 !ui%t+in ,unction"
@hen these &unctions return, the results o& an, "e"or, stores 1er&or"ed usin! coherent varia:les
1er&or"ed 1rior to the call will :e visi:le to an, &uture coherent access to the sa"e "e"or, 1er&or"ed :,
an, other shader invocation. In 1articular, the values written this wa, in one shader sta!e are !uaranteed
to :e visi:le to coherent "e"or, accesses 1er&or"ed :, shader invocations in su:se<uent sta!es when
those invocations were tri!!ered :, the e2ecution o& the ori!inal shader invocation Be.!., &ra!"ent shader
invocations &or a 1ri"itive resultin! &ro" a 1articular !eo"etr, shader invocationC.
Additionall,, "e"or, :arrier &unctions order stores 1er&or"ed :, the callin! invocation, as o:served :,
other shader invocations. @ithout "e"or, :arriers, i& one shader invocation 1er&or"s two stores to
coherent varia:les, a second shader invocation "i!ht see the values written :, the second store 1rior to
seein! those written :, the &irst. /owever, i& the &irst shader invocation calls a "e"or, :arrier &unction
:etween the two stores, selected other shader invocations will never see the results o& the second store
:e&ore seein! those o& the &irst. @hen usin! the &unction groupKemoryJarrierBC, this orderin!
!uarantee a11lies onl, to other shader invocations in the sa"e co"1ute shader wor !rou1J all other
"e"or, :arrier &unctions 1rovide the !uarantee to all other shader invocations. 8o "e"or, :arrier is
re<uired to !uarantee the order o& "e"or, stores as o:served :, the invocation 1er&or"in! the storesJ an
invocation readin! &ro" a varia:le that it 1reviousl, wrote will alwa,s see the "ost recentl, written value
unless another shader invocation also wrote to the sa"e "e"or,.
176
9 Shading Language Gra**ar for -ore .rofi%e
9 Shading Language Grammar for Core
-rofile
0he !ra""ar is &ed &ro" the out1ut o& le2ical anal,sis. 0he toens returned &ro" le2ical anal,sis are
M+%*, :++# '#+D, -+@:#& $%, @$%,
:)&DS M+%,$%@& -+ &#*& '+) $' -$*MD)- )&,@)% *T$,MP MD*& -&'D@#, *@:)+@,$%&
:(&M= :(&M> :(&M< $(&M= $(&M> $(&M< @(&M= @(&M> @(&M< (&M= (&M> (&M<
GD,= GD,> GD,< M&%,)+$- $% +@, $%+@,
@%$'+)G HD,MP *DGH#& :@''&) *PD)&-
M+P&)&%, (+#D,$#& )&*,)$M, )&D-+%#V T)$,&+%#V
-(&M= -(&M> -(&M< -GD,= -GD,> -GD,<
%+H&)*H&M,$(& '#D, *G++,P #DV+@,
GD,=U= GD,=U> GD,=U<
GD,>U= GD,>U> GD,>U<
GD,<U= GD,<U> GD,<U<
-GD,=U= -GD,=U> -GD,=U<
-GD,>U= -GD,>U> -GD,>U<
-GD,<U= -GD,<U> -GD,<U<
D,+G$M"@$%,
*DGH#&)1- *DGH#&)=- *DGH#&)>- *DGH#&)M@:& *DGH#&)1-*PD-+T *DGH#&)=-*PD-+T
*DGH#&)M@:&*PD-+T *DGH#&)1-D))DV *DGH#&)=-D))DV *DGH#&)1-D))DV*PD-+T
*DGH#&)=-D))DV*PD-+T $*DGH#&)1- $*DGH#&)=- $*DGH#&)>- $*DGH#&)M@:&
$*DGH#&)1-D))DV $*DGH#&)=-D))DV @*DGH#&)1- @*DGH#&)=- @*DGH#&)>-
@*DGH#&)M@:& @*DGH#&)1-D))DV @*DGH#&)=-D))DV
*DGH#&)=-)&M, *DGH#&)=-)&M,*PD-+T $*DGH#&)=-)&M, @*DGH#&)=-)&M,
*DGH#&):@''&) $*DGH#&):@''&) @*DGH#&):@''&)
*DGH#&)M@:&D))DV *DGH#&)M@:&D))DV*PD-+T
$*DGH#&)M@:&D))DV @*DGH#&)M@:&D))DV
*DGH#&)=-G* $*DGH#&)=-G* @*DGH#&)=-G*
*DGH#&)=-G*D))DV $*DGH#&)=-G*D))DV @*DGH#&)=-G*D))DV
$GD.&1- $$GD.&1- @$GD.&1- $GD.&=- $$GD.&=-
@$GD.&=- $GD.&>- $$GD.&>- @$GD.&>-
$GD.&=-)&M, $$GD.&=-)&M, @$GD.&=-)&M,
$GD.&M@:& $$GD.&M@:& @$GD.&M@:&
$GD.&:@''&) $$GD.&:@''&) @$GD.&:@''&)
$GD.&1-D))DV $$GD.&1-D))DV @$GD.&1-D))DV
$GD.&=-D))DV $$GD.&=-D))DV @$GD.&=-D))DV
$GD.&M@:&D))DV $$GD.&M@:&D))DV @$GD.&M@:&D))DV
$GD.&=-G* $$GD.&=-G* @$GD.&=-G*
$GD.&=-G*D))DV $$GD.&=-G*D))DV @$GD.&=-G*D))DV
177
9 Shading Language Gra**ar for -ore .rofi%e
*,)@M, (+$- TP$#&
$-&%,$'$&) ,VH&"%DG&
'#+D,M+%*,D%, -+@:#&M+%*,D%, $%,M+%*,D%, @$%,M+%*,D%, :++#M+%*,D%,
'$&#-"*&#&M,$+%
#&',"+H )$.P,"+H
$%M"+H -&M"+H #&"+H .&"+H &Y"+H %&"+H
D%-"+H +)"+H U+)"+H G@#"D**$.% -$("D**$.% D--"D**$.%
G+-"D**$.% #&',"D**$.% )$.P,"D**$.% D%-"D**$.% U+)"D**$.% +)"D**$.%
*@:"D**$.%
#&',"HD)&% )$.P,"HD)&% #&',":)DMS&, )$.P,":)DMS&, #&',":)DM& )$.P,":)DM& -+,
M+GGD M+#+% &Y@D# *&G$M+#+% :D%. -D*P ,$#-& H#@* *,D) *#D*P H&)M&%,
#&',"D%.#& )$.P,"D%.#& (&),$MD#":D) MD)&, DGH&)*D%- Y@&*,$+%
$%(D)$D%, H)&M$*&
P$.P"H)&M$*$+% G&-$@G"H)&M$*$+% #+T"H)&M$*$+% H)&M$*$+%
0he &ollowin! descri:es the !ra""ar &or the -1enGL +hadin! Lan!ua!e in ter"s o& the a:ove toens.
0he startin! rule is translationunit. An e"1t, shader Bone havin! no toens to 1arse, a&ter 1re9
1rocessin!C is valid, resultin! in no co"1ile9ti"e errors, even thou!h the !ra""ar :elow does not have a
rule to acce1t an e"1t, toen strea".
variablei*enti&ier:
0D8750F08R
)rimar/e")ression:
variablei*enti&ier
075$67'5#75
1075$67'5#75
FL6#5$67'5#75
%66L$67'5#75
D61%L8$67'5#75
L8F5B#R87 e")ression R0-P5B#R87
)ost&i"e")ression:
)rimar/e")ression
)ost&i"e")ression L8F5%R#$Q85 integere")ression R0-P5%R#$Q85
&unctioncall
)ost&i"e")ression D65 F08LD'8L8$5067
)ost&i"e")ression 07$6B
)ost&i"e")ression D8$6B
178
9 Shading Language Gra**ar for -ore .rofi%e
integere")ression:
e")ression
&unctioncall:
&unctioncallormet4o*
&unctioncallormet4o*:
&unctioncallgeneric

&unctioncallgeneric:
&unctioncall4ea*er2it4)arameters R0-P5B#R87
&unctioncall4ea*erno)arameters R0-P5B#R87
&unctioncall4ea*erno)arameters:
&unctioncall4ea*er V60D
&unctioncall4ea*er
&unctioncall4ea*er2it4)arameters:
&unctioncall4ea*er assignmente")ression
&unctioncall4ea*er2it4)arameters $6!!# assignmente")ression
&unctioncall4ea*er:
&unctioni*enti&ier L8F5B#R87
KK -rammar 7ote: $onstructors loo, li,e &unctions. but le"ical anal/sis recogni(e* most o& t4em as
KK ,e/2or*s. 54e/ are no2 recogni(e* t4roug4 Rt/)es)eci&ierS.
KK !et4o*s :.lengt4;. subroutine arra/ calls. an* i*enti&iers are recogni(e* t4roug4 )ost&i"e")ression.
&unctioni*enti&ier:
t/)es)eci&ier
)ost&i"e")ression
unar/e")ression:
)ost&i"e")ression
07$6B unar/e")ression
D8$6B unar/e")ression
unar/o)erator unar/e")ression
KK -rammar 7ote: 7o tra*itional st/le t/)e casts.
179
9 Shading Language Gra**ar for -ore .rofi%e
unar/o)erator:
BL1'
D#'P
%#7-
50LD8
KK -rammar 7ote: 7o OAO or OTO unar/ o)s. Bointers are not su))orte*.
multi)licativee")ression:
unar/e")ression
multi)licativee")ression '5#R unar/e")ression
multi)licativee")ression 'L#'P unar/e")ression
multi)licativee")ression B8R$875 unar/e")ression
a**itivee")ression:
multi)licativee")ression
a**itivee")ression BL1' multi)licativee")ression
a**itivee")ression D#'P multi)licativee")ression
s4i&te")ression:
a**itivee")ression
s4i&te")ression L8F56B a**itivee")ression
s4i&te")ression R0-P56B a**itivee")ression
relationale")ression:
s4i&te")ression
relationale")ression L8F5#7-L8 s4i&te")ression
relationale")ression R0-P5#7-L8 s4i&te")ression
relationale")ression L86B s4i&te")ression
relationale")ression -86B s4i&te")ression
e3ualit/e")ression:
relationale")ression
e3ualit/e")ression 8U6B relationale")ression
e3ualit/e")ression 786B relationale")ression
an*e")ression:
e3ualit/e")ression
an*e")ression #!B8R'#7D e3ualit/e")ression
18
9 Shading Language Gra**ar for -ore .rofi%e
e"clusiveore")ression:
an*e")ression
e"clusiveore")ression $#R85 an*e")ression
inclusiveore")ression:
e"clusiveore")ression
inclusiveore")ression V8R50$#L%#R e"clusiveore")ression
logicalan*e")ression:
inclusiveore")ression
logicalan*e")ression #7D6B inclusiveore")ression
logical"ore")ression:
logicalan*e")ression
logical"ore")ression F6R6B logicalan*e")ression
logicalore")ression:
logical"ore")ression
logicalore")ression 6R6B logical"ore")ression
con*itionale")ression:
logicalore")ression
logicalore")ression U18'5067 e")ression $6L67 assignmente")ression
assignmente")ression:
con*itionale")ression
unar/e")ression assignmento)erator assignmente")ression
assignmento)erator:
8U1#L
!1L#''0-7
D0V#''0-7
!6D#''0-7
#DD#''0-7
'1%#''0-7
L8F5#''0-7
R0-P5#''0-7
#7D#''0-7
F6R#''0-7
181
9 Shading Language Gra**ar for -ore .rofi%e
6R#''0-7
e")ression:
assignmente")ression
e")ression $6!!# assignmente")ression
constante")ression:
con*itionale")ression
*eclaration:
&unction)rotot/)e '8!0$6L67
init*eclaratorlist '8!0$6L67
BR8$0'067 )recision3uali&ier t/)es)eci&ier '8!0$6L67
t/)e3uali&ier 0D8750F08R L8F5%R#$8 struct*eclarationlist R0-P5%R#$8 '8!0$6L67
t/)e3uali&ier 0D8750F08R L8F5%R#$8 struct*eclarationlist R0-P5%R#$8
0D8750F08R '8!0$6L67
t/)e3uali&ier 0D8750F08R L8F5%R#$8 struct*eclarationlist R0-P5%R#$8
0D8750F08R arra/s)eci&ier '8!0$6L67
t/)e3uali&ier '8!0$6L67
t/)e3uali&ier 0D8750F08R '8!0$6L67
t/)e3uali&ier 0D8750F08R i*enti&ierlist '8!0$6L67
i*enti&ierlist:
$6!!# 0D8750F08R
i*enti&ierlist $6!!# 0D8750F08R
&unction)rotot/)e:
&unction*eclarator R0-P5B#R87
&unction*eclarator:
&unction4ea*er
&unction4ea*er2it4)arameters
&unction4ea*er2it4)arameters:
&unction4ea*er )arameter*eclaration
&unction4ea*er2it4)arameters $6!!# )arameter*eclaration
&unction4ea*er:
&ull/s)eci&ie*t/)e 0D8750F08R L8F5B#R87
182
9 Shading Language Gra**ar for -ore .rofi%e
)arameter*eclarator:
t/)es)eci&ier 0D8750F08R
t/)es)eci&ier 0D8750F08R arra/s)eci&ier
)arameter*eclaration:
t/)e3uali&ier )arameter*eclarator
)arameter*eclarator
t/)e3uali&ier )arametert/)es)eci&ier
)arametert/)es)eci&ier
)arametert/)es)eci&ier:
t/)es)eci&ier
init*eclaratorlist:
single*eclaration
init*eclaratorlist $6!!# 0D8750F08R
init*eclaratorlist $6!!# 0D8750F08R arra/s)eci&ier
init*eclaratorlist $6!!# 0D8750F08R arra/s)eci&ier 8U1#L initiali(er
init*eclaratorlist $6!!# 0D8750F08R 8U1#L initiali(er
single*eclaration:
&ull/s)eci&ie*t/)e
&ull/s)eci&ie*t/)e 0D8750F08R
&ull/s)eci&ie*t/)e 0D8750F08R arra/s)eci&ier
&ull/s)eci&ie*t/)e 0D8750F08R arra/s)eci&ier 8U1#L initiali(er
&ull/s)eci&ie*t/)e 0D8750F08R 8U1#L initiali(er
KK -rammar 7ote: 7o OenumO. or Ot/)e*e&O.
&ull/s)eci&ie*t/)e:
t/)es)eci&ier
t/)e3uali&ier t/)es)eci&ier
invariant3uali&ier:
07V#R0#75
inter)olation3uali&ier:
183
9 Shading Language Gra**ar for -ore .rofi%e
'!665P
FL#5
76B8R'B8$50V8
la/out3uali&ier:
L#G615 L8F5B#R87 la/out3uali&ieri*list R0-P5B#R87
la/out3uali&ieri*list:
la/out3uali&ieri*
la/out3uali&ieri*list $6!!# la/out3uali&ieri*
la/out3uali&ieri*:
0D8750F08R
0D8750F08R 8U1#L 075$67'5#75
)recise3uali&ier:
BR8$0'8
t/)e3uali&ier:
singlet/)e3uali&ier
t/)e3uali&ier singlet/)e3uali&ier
singlet/)e3uali&ier:
storage3uali&ier
la/out3uali&ier
)recision3uali&ier
inter)olation3uali&ier
invariant3uali&ier
)recise3uali&ier
storage3uali&ier:
$67'5
07615
07
615
$875R60D
B#5$P
'#!BL8
170F6R!
184
9 Shading Language Gra**ar for -ore .rofi%e
%1FF8R
'P#R8D
$6P8R875
V6L#50L8
R8'5R0$5
R8#D67LG
+R05867LG
'1%R615078
'1%R615078 L8F5B#R87 t/)enamelist R0-P5B#R87
t/)enamelist:
5GB87#!8
t/)enamelist $6!!# 5GB87#!8
t/)es)eci&ier:
t/)es)eci&iernonarra/
t/)es)eci&iernonarra/ arra/s)eci&ier
arra/s)eci&ier:
L8F5%R#$Q85 R0-P5%R#$Q85
L8F5%R#$Q85 constante")ression R0-P5%R#$Q85
arra/s)eci&ier L8F5%R#$Q85 R0-P5%R#$Q85
arra/s)eci&ier L8F5%R#$Q85 constante")ression R0-P5%R#$Q85
t/)es)eci&iernonarra/:
V60D
FL6#5
D61%L8
075
1075
%66L
V8$2
V8$3
V8$4
DV8$2
DV8$3
DV8$4
185
9 Shading Language Gra**ar for -ore .rofi%e
%V8$2
%V8$3
%V8$4
0V8$2
0V8$3
0V8$4
1V8$2
1V8$3
1V8$4
!#52
!#53
!#54
!#52F2
!#52F3
!#52F4
!#53F2
!#53F3
!#53F4
!#54F2
!#54F3
!#54F4
D!#52
D!#53
D!#54
D!#52F2
D!#52F3
D!#52F4
D!#53F2
D!#53F3
D!#53F4
D!#54F2
D!#54F3
D!#54F4
#56!0$1075
'#!BL8R1D
'#!BL8R2D
'#!BL8R3D
186
9 Shading Language Gra**ar for -ore .rofi%e
'#!BL8R$1%8
'#!BL8R1D'P#D6+
'#!BL8R2D'P#D6+
'#!BL8R$1%8'P#D6+
'#!BL8R1D#RR#G
'#!BL8R2D#RR#G
'#!BL8R1D#RR#G'P#D6+
'#!BL8R2D#RR#G'P#D6+
'#!BL8R$1%8#RR#G
'#!BL8R$1%8#RR#G'P#D6+
0'#!BL8R1D
0'#!BL8R2D
0'#!BL8R3D
0'#!BL8R$1%8
0'#!BL8R1D#RR#G
0'#!BL8R2D#RR#G
0'#!BL8R$1%8#RR#G
1'#!BL8R1D
1'#!BL8R2D
1'#!BL8R3D
1'#!BL8R$1%8
1'#!BL8R1D#RR#G
1'#!BL8R2D#RR#G
1'#!BL8R$1%8#RR#G
'#!BL8R2DR8$5
'#!BL8R2DR8$5'P#D6+
0'#!BL8R2DR8$5
1'#!BL8R2DR8$5
'#!BL8R%1FF8R
0'#!BL8R%1FF8R
1'#!BL8R%1FF8R
'#!BL8R2D!'
0'#!BL8R2D!'
1'#!BL8R2D!'
'#!BL8R2D!'#RR#G
0'#!BL8R2D!'#RR#G
1'#!BL8R2D!'#RR#G
187
9 Shading Language Gra**ar for -ore .rofi%e
0!#-81D
00!#-81D
10!#-81D
0!#-82D
00!#-82D
10!#-82D
0!#-83D
00!#-83D
10!#-83D
0!#-82DR8$5
00!#-82DR8$5
10!#-82DR8$5
0!#-8$1%8
00!#-8$1%8
10!#-8$1%8
0!#-8%1FF8R
00!#-8%1FF8R
10!#-8%1FF8R
0!#-81D#RR#G
00!#-81D#RR#G
10!#-81D#RR#G
0!#-82D#RR#G
00!#-82D#RR#G
10!#-82D#RR#G
0!#-8$1%8#RR#G
00!#-8$1%8#RR#G
10!#-8$1%8#RR#G
0!#-82D!'
00!#-82D!'
10!#-82D!'
0!#-82D!'#RR#G
00!#-82D!'#RR#G
10!#-82D!'#RR#G
structs)eci&ier
5GB87#!8
)recision3uali&ier:
P0-PBR8$0'067
!8D01!BR8$0'067
188
9 Shading Language Gra**ar for -ore .rofi%e
L6+BR8$0'067
structs)eci&ier:
'5R1$5 0D8750F08R L8F5%R#$8 struct*eclarationlist R0-P5%R#$8
'5R1$5 L8F5%R#$8 struct*eclarationlist R0-P5%R#$8
struct*eclarationlist:
struct*eclaration
struct*eclarationlist struct*eclaration
struct*eclaration:
t/)es)eci&ier struct*eclaratorlist '8!0$6L67
t/)e3uali&ier t/)es)eci&ier struct*eclaratorlist '8!0$6L67
struct*eclaratorlist:
struct*eclarator
struct*eclaratorlist $6!!# struct*eclarator
struct*eclarator:
0D8750F08R
0D8750F08R arra/s)eci&ier
initiali(er:
assignmente")ression
L8F5%R#$8 initiali(erlist R0-P5%R#$8
L8F5%R#$8 initiali(erlist $6!!# R0-P5%R#$8
initiali(erlist:
initiali(er
initiali(erlist $6!!# initiali(er
*eclarationstatement:
*eclaration
statement:
com)oun*statement
sim)lestatement
KK -rammar 7ote: labele* statements &or '+05$P onl/9 OgotoO is not su))orte*.
189
9 Shading Language Gra**ar for -ore .rofi%e
sim)lestatement:
*eclarationstatement
e")ressionstatement
selectionstatement
s2itc4statement
caselabel
iterationstatement
>um)statement
com)oun*statement:
L8F5%R#$8 R0-P5%R#$8
L8F5%R#$8 statementlist R0-P5%R#$8
statementnone2sco)e:
com)oun*statementnone2sco)e
sim)lestatement
com)oun*statementnone2sco)e:
L8F5%R#$8 R0-P5%R#$8
L8F5%R#$8 statementlist R0-P5%R#$8
statementlist:
statement
statementlist statement
e")ressionstatement:
'8!0$6L67
e")ression '8!0$6L67
selectionstatement:
0F L8F5B#R87 e")ression R0-P5B#R87 selectionreststatement
selectionreststatement:
statement 8L'8 statement
statement
con*ition:
e")ression
&ull/s)eci&ie*t/)e 0D8750F08R 8U1#L initiali(er
19
9 Shading Language Gra**ar for -ore .rofi%e
s2itc4statement:
'+05$P L8F5B#R87 e")ression R0-P5B#R87 L8F5%R#$8 s2itc4statementlist
R0-P5%R#$8
s2itc4statementlist:
KA not4ing AK
statementlist
caselabel:
$#'8 e")ression $6L67
D8F#1L5 $6L67
iterationstatement:
+P0L8 L8F5B#R87 con*ition R0-P5B#R87 statementnone2sco)e
D6 statement +P0L8 L8F5B#R87 e")ression R0-P5B#R87 '8!0$6L67
F6R L8F5B#R87 &orinitstatement &orreststatement R0-P5B#R87
statementnone2sco)e
&orinitstatement:
e")ressionstatement
*eclarationstatement
con*itiono)t:
con*ition
KA em)t/ AK
&orreststatement:
con*itiono)t '8!0$6L67
con*itiono)t '8!0$6L67 e")ression
>um)statement:
$6750718 '8!0$6L67
%R8#Q '8!0$6L67
R851R7 '8!0$6L67
R851R7 e")ression '8!0$6L67
D0'$#RD '8!0$6L67 KK Fragment s4a*er onl/.
KK -rammar 7ote: 7o OgotoO. -otos are not su))orte*.
translationunit:
e"ternal*eclaration
translationunit e"ternal*eclaration
191
9 Shading Language Gra**ar for -ore .rofi%e
e"ternal*eclaration:
&unction*e&inition
*eclaration
&unction*e&inition:
&unction)rotot/)e com)oun*statementnone2sco)e
192
1 /or*ative 0eference"
1$ 6ormative References
1. International +tandard I+-DIE$ 1(44#:1664BEC. 3ro!ra""in! Lan!ua!es ^ $II.
Re&erenced &or 1re1rocessor onl,.
193

Você também pode gostar