Vous êtes sur la page 1sur 180

The OpenGL

Shading Language
Language Version: 4.20
Document Revision: 11
12-Dec-2011
Editor: John Kessenich
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
Copyright (c) 2008-2011 The Khronos Group Inc. All Rights Reserve.
This speci!ic"tion is protecte #y copyright l"$s "n cont"ins %"teri"l propriet"ry to the Khronos Group&
Inc. It or "ny co%ponents %"y not #e reprouce& repu#lishe& istri#ute& tr"ns%itte& ispl"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%oving "ny tr"e%"r)& copyright or other notice !ro% the speci!ic"tion& #ut the receipt or possession o!
this speci!ic"tion oes not convey "ny rights to reprouce& isclose& or istri#ute its contents& or to
%"nu!"cture& use& or sell "nything th"t it %"y escri#e& in $hole or in p"rt.
Khronos Group gr"nts e'press per%ission to "ny current *ro%oter& Contri#utor or Aopter %e%#er o!
Khronos to copy "n reistri#ute +,-./I0I1/ versions o! this speci!ic"tion in "ny !"shion& provie th"t
,. C2ARG1 is %"e !or the speci!ic"tion "n the l"test "v"il"#le up"te o! the speci!ic"tion !or "ny
version o! the A*I is use $henever possi#le. 3uch istri#ute speci!ic"tion %"y #e re-!or%"tte A3
4.,G A3 the contents o! the speci!ic"tion "re not ch"nge in "ny $"y. The speci!ic"tion %"y #e
incorpor"te into " prouct th"t is sol "s long "s such prouct inclues signi!ic"nt inepenent $or)
evelope #y the seller. A lin) to the current version o! this speci!ic"tion on the Khronos Group $e#-site
shoul #e inclue $henever possi#le $ith speci!ic"tion istri#utions.
Khronos Group %")es no& "n e'pressly iscl"i%s "ny& represent"tions or $"rr"nties& e'press or
i%plie& reg"ring this speci!ic"tion& incluing& $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"ring the correctness& "ccur"cy&
co%pleteness& ti%eliness& "n reli"#ility o! the speci!ic"tion. +ner no circu%st"nces $ill the Khronos
Group& or "ny o! its *ro%oters& Contri#utors or -e%#ers or their respective p"rtners& o!!icers& irectors&
e%ployees& "gents or represent"tives #e li"#le !or "ny "%"ges& $hether irect& inirect& speci"l or
conse5uenti"l "%"ges !or lost revenues& lost pro!its& or other$ise& "rising !ro% or in connection $ith
these %"teri"ls.
Khronos& .penK./1& .penK.G3& .pen6G& .pen-A7& .pen34 13 "n .pen80 "re tr"e%"r)s o!
the Khronos Group Inc. C.44A/A is " tr"e%"r) o! 3ony Co%puter 1ntert"in%ent Inc. use #y
per%ission #y Khronos. .penG4 "n .pen-4 "re registere tr"e%"r)s "n the .penG4 13 logo is "
tr"e%"r) o! 3ilicon Gr"phics Inc. use #y per%ission #y Khronos. All other prouct n"%es& tr"e%"r)s&
"n9or co%p"ny n"%es "re use solely !or ienti!ic"tion "n #elong to their respective o$ners.
2
Table of Contents
1 Introduction.................................................................................................................................1
1.1 Acknowledgents................................................................................................................!
1.! "hanges................................................................................................................................!
1.!.1 #uar$ o% "hanges %ro Version &.1'.....................................................................!
1.( )verview..............................................................................................................................&
1.& Error *andling......................................................................................................................+
1.+ ,$-ogra-hical "onventions.................................................................................................+
1.. De-recation..........................................................................................................................+
! )verview o% )-en/0 #hading.....................................................................................................
!.1 Verte1 2rocessor...................................................................................................................
!.! ,essellation "ontrol 2rocessor..............................................................................................
!.( ,essellation Evaluation 2rocessor........................................................................................3
!.& /eoetr$ 2rocessor.............................................................................................................3
!.+ 4ragent 2rocessor..............................................................................................................3
( Basics..........................................................................................................................................5
(.1 "haracter #et........................................................................................................................5
(.! #ource #trings......................................................................................................................5
(.( 2re-rocessor.........................................................................................................................6
(.& "oents..........................................................................................................................1&
(.+ ,okens................................................................................................................................1+
(.. Ke$words............................................................................................................................1+
(.3 Identi%iers...........................................................................................................................13
(.5 De%initions..........................................................................................................................13
(.5.1 #tatic 7se....................................................................................................................15
(.5.! 7ni%or and 8on97ni%or "ontrol 4low..................................................................15
(.5.( D$naicall$ 7ni%or E1-ressions.............................................................................15
& Varia:les and ,$-es..................................................................................................................16
&.1 Basic ,$-es........................................................................................................................16
&.1.1 Void............................................................................................................................!(
&.1.! Booleans.....................................................................................................................!(
&.1.( Integers.......................................................................................................................!(
&.1.& 4loats..........................................................................................................................!+
&.1.+ Vectors........................................................................................................................!.
&.1.. ;atrices......................................................................................................................!.
&.1.3 )-a<ue ,$-es.............................................................................................................!3
&.1.3.1 #a-lers.............................................................................................................!3
&.1.3.! Iages.................................................................................................................!3
&.1.3.( Atoic "ounters.................................................................................................!5
&.1.5 #tructures....................................................................................................................!5
&.1.6 Arra$s.........................................................................................................................!6
3
&.1.1' I-licit "onversions................................................................................................(1
&.1.11 Initiali=ers.................................................................................................................(!
&.! #co-ing...............................................................................................................................(&
&.( #torage >uali%iers...............................................................................................................(.
&.(.1 De%ault #torage >uali%ier............................................................................................(3
&.(.! "onstant >uali%ier......................................................................................................(3
&.(.( "onstant E1-ressions.................................................................................................(3
&.(.& In-ut Varia:les...........................................................................................................(5
&.(.+ 7ni%or......................................................................................................................&'
&.(.. )ut-ut Varia:les.........................................................................................................&1
&.(.3 Inter%ace Blocks..........................................................................................................&(
&.& 0a$out >uali%iers................................................................................................................&.
&.&.1 In-ut 0a$out >uali%iers...............................................................................................&.
&.&.1.1 ,essellation Evaluation In-uts............................................................................&5
&.&.1.! /eoetr$ #hader In-uts......................................................................................&6
&.&.1.( 4ragent #hader In-uts......................................................................................+1
&.&.! )ut-ut 0a$out >uali%iers............................................................................................+!
&.&.!.1 ,essellation "ontrol )ut-uts..............................................................................+(
&.&.!.! /eoetr$ )ut-uts...............................................................................................+&
&.&.!.( 4ragent )ut-uts...............................................................................................+.
&.&.( 7ni%or Block 0a$out >uali%iers...............................................................................+3
&.&.& )-a<ue97ni%or 0a$out >uali%iers............................................................................+6
&.&.&.1 Atoic "ounter 0a$out >uali%iers......................................................................'
&.&.&.! 4orat 0a$out >uali%iers.....................................................................................1
&.+ Inter-olation >uali%iers.......................................................................................................(
&.+.1 Redeclaring Built9in Inter-olation Varia:les in the "o-ati:ilit$ 2ro%ile..................&
&.. 2araeter >uali%iers............................................................................................................+
&.3 2recision and 2recision >uali%iers......................................................................................+
&.3.1 Range and 2recision....................................................................................................+
&.3.! 2recision >uali%iers......................................................................................................
&.3.( De%ault 2recision >uali%iers........................................................................................3
&.3.& Availa:le 2recision >uali%iers.....................................................................................5
&.5 Variance and the Invariant >uali%ier...................................................................................5
&.5.1 ,he Invariant >uali%ier................................................................................................5
&.5.! Invariance o% "onstant E1-ressions............................................................................6
&.6 ,he 2recise >uali%ier...........................................................................................................6
&.1' ;eor$ >uali%iers...........................................................................................................31
&.11 )rder o% >uali%ication......................................................................................................3(
+ )-erators and E1-ressions........................................................................................................3&
+.1 )-erators............................................................................................................................3&
+.! Arra$ )-erations...............................................................................................................3+
+.( 4unction "alls....................................................................................................................3+
4
+.& "onstructors.......................................................................................................................3+
+.&.1 "onversion and #calar "onstructors..........................................................................3+
+.&.! Vector and ;atri1 "onstructors.................................................................................3.
+.&.( #tructure "onstructors................................................................................................35
+.&.& Arra$ "onstructors.....................................................................................................36
+.+ Vector and #calar "o-onents and 0ength.......................................................................36
+.. ;atri1 "o-onents............................................................................................................51
+.3 #tructure and Arra$ )-erations..........................................................................................51
+.5 Assignents.......................................................................................................................5!
+.6 E1-ressions........................................................................................................................5(
+.1' Vector and ;atri1 )-erations..........................................................................................5.
. #tateents and #tructure...........................................................................................................55
..1 4unction De%initions...........................................................................................................56
..1.1 4unction "alling "onventions....................................................................................61
..1.! #u:routines.................................................................................................................6(
..! #election.............................................................................................................................6&
..( Iteration..............................................................................................................................6+
..& Ju-s..................................................................................................................................6.
3 Built9in Varia:les......................................................................................................................63
3.1 Built9In 0anguage Varia:les..............................................................................................63
3.1.1 "o-ati:ilit$ 2ro%ile Built9In 0anguage Varia:les..................................................1'&
3.! "o-ati:ilit$ 2ro%ile Verte1 #hader Built9In In-uts.......................................................1'3
3.( Built9In "onstants............................................................................................................1'3
3.(.1 "o-ati:ilit$ 2ro%ile Built9In "onstants..................................................................1'6
3.& Built9In 7ni%or #tate.....................................................................................................11'
3.&.1 "o-ati:ilit$ 2ro%ile #tate.......................................................................................11'
5 Built9in 4unctions...................................................................................................................11&
5.1 Angle and ,rigonoetr$ 4unctions..................................................................................11+
5.! E1-onential 4unctions......................................................................................................113
5.( "oon 4unctions..........................................................................................................115
5.& 4loating92oint 2ack and 7n-ack 4unctions.....................................................................1!&
5.+ /eoetric 4unctions........................................................................................................1!.
5.. ;atri1 4unctions..............................................................................................................1!5
5.3 Vector Relational 4unctions.............................................................................................1('
5.5 Integer 4unctions..............................................................................................................1(!
5.6 ,e1ture 4unctions.............................................................................................................1(&
5.6.1 ,e1ture >uer$ 4unctions..........................................................................................1(+
5.6.! ,e1el 0ooku- 4unctions...........................................................................................1(3
5.6.( ,e1ture /ather Instructions......................................................................................1&&
5.6.& "o-ati:ilit$ 2ro%ile ,e1ture 4unctions..................................................................1&3
5.1' Atoic9"ounter 4unctions.............................................................................................1&6
5.11 Iage 4unctions.............................................................................................................1&6
5
5.1! 4ragent 2rocessing 4unctions......................................................................................1+!
5.1!.1 Derivative 4unctions..............................................................................................1+!
5.1!.! Inter-olation 4unctions...........................................................................................1+&
5.1( 8oise 4unctions..............................................................................................................1++
5.1& /eoetr$ #hader 4unctions...........................................................................................1+.
5.1+ #hader Invocation "ontrol 4unctions.............................................................................1+5
5.1. #hader ;eor$ "ontrol 4unction..................................................................................1+5
6 #hading 0anguage /raar %or "ore 2ro%ile........................................................................1.'
6
1 Introduction
,his docuent s-eci%ies onl$ version &.!' o% the )-en/0 #hading 0anguage. It re<uires ??VER#I)8??
to su:stitute &!', and re<uires #version to acce-t onl$ &!'. I% #version is declared with a saller
nu:er, the language acce-ted is a -revious version o% the shading language, which will :e su--orted
de-ending on the version and t$-e o% conte1t in the )-en/0 A2I. #ee the )-en/0 /ra-hics #$ste
#-eci%ication, Version &.!, %or details on what language versions are su--orted.
2revious versions o% the )-en/0 #hading 0anguage, as well as the )-en/0 E# #hading 0anguage, are
not strict su:sets o% the version s-eci%ied here, -articularl$ with res-ect to -recision, nae9hiding rules,
and treatent o% inter%ace varia:les. #ee the s-eci%ication corres-onding to a -articular language version
%or details s-eci%ic to that version o% the language.
All )-en/0 /ra-hics #$ste #-eci%ication re%erences in this s-eci%ication are to version &.!.
1
1 Introduction
1.1 Acknowledgments
,his s-eci%ication is :ased on the work o% those who contri:uted to -ast versions o% the )-en/0
0anguage #-eci%ication, the )-en/0 E# !.' 0anguage #-eci%ication, and the %ollowing contri:utors to
this version:
2at Brown, 8VIDIA
Je%% Bol=, 8VIDIA
4rank "hen
2ierre Boudier, A;D
2iers Daniell, 8VIDIA
"hris Dodd, 8VIDIA
8ick *aeel, 8VIDIA
Jason /reen, ,rans/aing
Brent Insko, Intel
Jon 0eech
Bill 0icea9Kane, A;D
Daniel Koch, ,rans/aing
Barthold 0ichten:elt, 8VIDIA
Bruce ;err$, AR;
Ro:ert )hannessian
Acorn 2oole$, 8VIDIA
Kevin Rogovin
Ian Roanick, Intel
/reg Roth, 8vidia
/raha #ellers, A;D
Dave #hreiner, AR;
Jere$ #andel, A--le
Ro:ert #i-son, >ualco
Eric @erness, 8VIDIA
;ark Aoung, A;D
1.2 Changes
1.2.1 Summary of Changes from Version .1!
8ote: 8o %eatures were de-recated :etween versions &.1' and &.!'.
;ove these -reviousl$ de-recated %eatures to :e onl$ in the co-ati:ilit$ -ro%ile:
,he ke$word attribute %or verte1 shader in-uts. B7se in instead.C
,he ke$word varying %or in-uts and out-uts. B7se in and out instead.C
,he original te1turing :uilt9in %unctions. B7se the new %ors instead.C
,he :uilt9in varia:les gl_FragColor and gl_FragData. B7se out instead.C
Built9in constants related to these.
2
1 Introduction
"hange %ro A#"II to 7,495 %or the language character set and also allow an$ characters inside
coents Be1ce-t the :$te value 'C, including DED.
Add line9continuation using DED, as in "FF.
E# convergence
Add ta:le showing G,reatent o% ;isatched In-utH)ut-ut Varia:lesI in section &.(.&.
8a8s are not re<uired to :e generated, dividing a non9=ero :$ =ero akes in%, see !nd
-aragra-h &.3.1 %or %ull odi%ication.
"lari%$ that .1$=w1$.1$ is illegal, is it te-oraril$ akes a Gvec.I.
"lari%$ that return stateents onl$ acce-t values Bno return o% a void %unctionC.
Add iage t$-es B/0?ARB?shader?iage?load?storeC
(( new t$-es, all with GimageI in their nae, corres-ond to the non9shadow te1ture t$-es
addition o% eor$ <uali%iers: coherent, volatile, restrict, readonly, and writeonly
can readHwriteHodi%$ iages %ro a shader, through new :uilt9in %unctions
<uali%iers can act inde-endentl$ on the o-a<ue shader varia:le and the :acking iage, so
e1tra <uali%iers can :e used to se-aratel$ <uali%$ these
Varia:les declared in if and else stateents are sco-ed onl$ to the end o% those stateents. 8ote,
this is not :ackward co-ati:le, it a$ de-end on Jversion.
Allow i-licit conversions o% return values to the declared t$-e o% the %unction.
,he const ke$word can :e used to declare varia:les within a %unction :od$ with initiali=er
e1-ressions that are not constant e1-ressions.
>uali%iers on varia:le declarations no longer have to %ollow a strict order. ,he layout <uali%ier
can :e used ulti-le ties, and ulti-le -araeter <uali%iers can :e used.
2araeter <uali%iers can include -recision and eor$ <uali%iers.
Add a new atomic_uint t$-e to su--ort atoic counters. Also, add :uilt9in %unctions %or
ani-ulating atoic counters.
atomicCounterIncrement, atomicCounterDecrement, and atomicCounter
Add la$out <uali%ier identi%iers binding and oset to :ind units to sa-ler and iage varia:le
declarations, atoic counters, and uni%or :locks.
Add :uilt9in %unctions to -ackHun-ack 1. :it %loating9-oint nu:ers
BARB?shading?language?-ack!%C.
ack!alf"#1$ and unack!alf"#1$
ack%norm"#1$ and unack%norm"#1$
Add gl_&ragDeth la$out <uali%iers to counicate what kind o% changes will :e ade to
gl_&ragDeth B/0?A;D?conservative de-thC.
3
1 Introduction
Add "9st$le curl$ :race initiali=er lists s$nta1 %or initiali=ers. 4ull initiali=ation o% aggregates is
re<uired when these are used.
Allow .lengthBC to :e a--lied to vectors and atrices, returning the nu:er o% co-onents or
coluns.
"lari%$ that .lengthBC returns an int t$-e and can :e used as a constant integer e1-ression.
Allow swi==le o-erations on scalars.
Even the -otential %or recursion through su:routine uni%ors is an error.
2ositive signed decial literals, as well as octal and he1adecial, can set all (! :its. ,his
includes setting the sign :it to create a negative value.
;ake /0#0 consistent with the A2I regarding user cli--ing, :$ no longer re%erring to
gl_!osition when gl_Cli"Verte# is not written. Rather, user cli--ing :ecoes unde%ined.
;inor consistenc$ %i1es, corrections
"onsistentl$ state structures have e:ers not %ields. ,he -eriod B . C is still called the %ield
selector, %or all its ulti-le uses.
Reove coent that there is no counication :etween instantiations o% a shader.
"lari%ied that a coa se<uence9o-erator e1-ression cannot :e a constant e1-ression. E.g.,
GB!,(CI is not allowed, seanticall$, as a valid constant e1-ression (, even though it is an
e1-ression that will evaluate to (.
7se vec" instead o% vec' %or coordinate in te1ture/atherKBsa-ler!DRect,...C.
"lari%$ that te#ture(ather)ffsetBC can take non9constants %or the o%%sets.
4i1 t$-o to allow integer cu:e9arra$ te1tures in the :uilt9in %unction te1ture#i=eBC.
"orrectl$ restrict inde1es o% uni%or :locks to :eing d$naicall$9uni%or integral e1-ressions.
,his correction also a--lies to earlier releases B&.'' and &.1'C, which inadvertentl$ allowed non9
uni%or inde1es.
7-date the graar in the last section.
1." O#er#ie$
,his docuent descri:es $%e &"en'L (%ading Language) version &.!'.
Inde-endent co-ilation units written in this language are called s%aders. A "rogram is a set o% shaders
that are co-iled and linked together, co-letel$ creating one or ore o% the -rograa:le stages o% the
)-en/0 -i-eline. All the shaders %or a single -rograa:le stage ust :e within the sae -rogra. A
co-lete set o% -rograa:le stages can :e -ut into a single -rogra or the stages can :e -artitioned
across ulti-le -rogras. ,he ai o% this docuent is to thoroughl$ s-eci%$ the -rograing language.
,he )-en/0 /ra-hics #$ste #-eci%ication will s-eci%$ the )-en/0 entr$ -oints used to ani-ulate and
counicate with -rogras and shaders.
4
1 Introduction
1. %rror &andling
"o-ilers, in general, acce-t -rogras that are ill9%ored, due to the i-ossi:ilit$ o% detecting all ill9
%ored -rogras. 2orta:ilit$ is onl$ ensured %or well9%ored -rogras, which this s-eci%ication
descri:es. "o-ilers are encouraged to detect ill9%ored -rogras and issue diagnostic essages, :ut are
not re<uired to do so %or all cases. "o-ilers are re<uired to return essages regarding le1icall$,
graaticall$, or seanticall$ incorrect shaders.
1.' Typographical Con#entions
Italic, :old, and %ont choices have :een used in this s-eci%ication -riaril$ to i-rove reada:ilit$. "ode
%ragents use a %i1ed width %ont. Identi%iers e:edded in te1t are italici=ed. Ke$words e:edded in te1t
are :old. )-erators are called :$ their nae, %ollowed :$ their s$:ol in :old in -arentheses. ,he
clari%$ing graar %ragents in the te1t use :old %or literals and italics %or non9terinals. ,he o%%icial
graar in section 6 G#hading 0anguage /raarI uses all ca-itals %or terinals and lower case %or
non9terinals.
1.( )eprecation
8ote that while this section is true o% /0#0 in general, version &.! has no de-recated %eatures in it.
2reviousl$ de-recated %eatures have :een oved to the co-ati:ilit$ -ro%ile and no new %eatures have
:een de-recated.
2revious versions o% the )-en/0 #hading 0anguage de-recated soe %eatures. ,hese are clearl$ called
out in this s-eci%ication as Gde-recatedI. ,he$ are still -resent in this version o% the language, :ut are
targeted %or -otential reoval in a %uture version o% the shading language. ,he )-en/0 A2I has a
%orward co-ati:ilit$ ode that will disallow use o% de-recated %eatures. I% co-iling in a ode where
use o% de-recated %eatures is disallowed, their use causes co-ile tie errors. #ee the )-en/0 /ra-hics
#$ste #-eci%ication %or details on what causes de-recated language %eatures to :e acce-ted or to return
an error.
5
2 O#er#ie$ of OpenGL Shading
,he )-en/0 #hading 0anguage is actuall$ several closel$ related languages. ,hese languages are used
to create shaders %or each o% the -rograa:le -rocessors contained in the )-en/0 -rocessing -i-eline.
"urrentl$, these -rocessors are the verte1, tessellation control, tessellation evaluation, geoetr$, and
%ragent -rocessors.
7nless otherwise noted in this -a-er, a language %eature a--lies to all languages, and coon usage will
re%er to these languages as a single language. ,he s-eci%ic languages will :e re%erred to :$ the nae o%
the -rocessor the$ target: verte1, tessellation control, tessellation evaluation, geoetr$, or %ragent.
;ost )-en/0 state is not tracked or ade availa:le to shaders. ,$-icall$, user9de%ined varia:les will :e
used %or counicating :etween di%%erent stages o% the )-en/0 -i-eline. *owever, a sall aount o%
state is still tracked and autoaticall$ ade availa:le to shaders, and there are a %ew :uilt9in varia:les %or
inter%aces :etween di%%erent stages o% the )-en/0 -i-eline.
2.1 Verte* +rocessor
,he verte# "rocessor is a -rograa:le unit that o-erates on incoing vertices and their associated data.
"o-ilation units written in the )-en/0 #hading 0anguage to run on this -rocessor are called verte#
s%aders. @hen a co-lete set o% verte1 shaders are co-iled and linked, the$ result in a verte# s%ader
e#ecutable that runs on the verte1 -rocessor.
,he verte1 -rocessor o-erates on one verte1 at a tie. It does not re-lace gra-hics o-erations that re<uire
knowledge o% several vertices at a tie.
2.2 Tessellation Control +rocessor
,he tessellation control -rocessor is a -rograa:le unit that o-erates on a -atch o% incoing vertices
and their associated data, eitting a new out-ut -atch. "o-ilation units written in the )-en/0 #hading
0anguage to run on this -rocessor are called tessellation control shaders. @hen a co-lete set o%
tessellation control shaders are co-iled and linked, the$ result in a tessellation control shader e1ecuta:le
that runs on the tessellation control -rocessor.
,he tessellation control shader is invoked %or each verte1 o% the out-ut -atch. Each invocation can read
the attri:utes o% an$ verte1 in the in-ut or out-ut -atches, :ut can onl$ write -er9verte1 attri:utes %or the
corres-onding out-ut -atch verte1. ,he shader invocations collectivel$ -roduce a set o% -er9-atch
attri:utes %or the out-ut -atch. A%ter all tessellation control shader invocations have co-leted, the out-ut
vertices and -er9-atch attri:utes are asse:led to %or a -atch to :e used :$ su:se<uent -i-eline stages.
,essellation control shader invocations run ostl$ inde-endentl$, with unde%ined relative e1ecution order.
*owever, the :uilt9in %unction :arrierBC can :e used to control e1ecution order :$ s$nchroni=ing
invocations, e%%ectivel$ dividing tessellation control shader e1ecution into a set o% -hases. ,essellation
control shaders will get unde%ined results i% one invocation reads a -er9verte1 or -er9-atch attri:ute
6
2 Overview of OpenGL Shading
written :$ another invocation at an$ -oint during the sae -hase, or i% two invocations atte-t to write
di%%erent values to the sae -er9-atch out-ut in a single -hase.
2." Tessellation %#aluation +rocessor
,he tessellation evaluation -rocessor is a -rograa:le unit that evaluates the -osition and other
attri:utes o% a verte1 generated :$ the tessellation -riitive generator, using a -atch o% incoing vertices
and their associated data. "o-ilation units written in the )-en/0 #hading 0anguage to run on this
-rocessor are called tessellation evaluation shaders. @hen a co-lete set o% tessellation evaluation
shaders are co-iled and linked, the$ result in a tessellation evaluation shader e1ecuta:le that runs on the
tessellation evaluation -rocessor.
Each invocation o% the tessellation evaluation e1ecuta:le co-utes the -osition and attri:utes o% a single
verte1 generated :$ the tessellation -riitive generator. ,he e1ecuta:le can read the attri:utes o% an$
verte1 in the in-ut -atch, -lus the tessellation coordinate, which is the relative location o% the verte1 in the
-riitive :eing tessellated. ,he e1ecuta:le writes the -osition and other attri:utes o% the verte1.
2. Geometry +rocessor
,he geometr* "rocessor is a -rograa:le unit that o-erates on data %or incoing vertices %or a -riitive
asse:led a%ter verte1 -rocessing and out-uts a se<uence o% vertices %oring out-ut -riitives.
"o-ilation units written in the )-en/0 #hading 0anguage to run on this -rocessor are called geometr*
s%aders. @hen a co-lete set o% geoetr$ shaders are co-iled and linked, the$ result in a geometr*
s%ader e#ecutable that runs on the geoetr$ -rocessor.
A single invocation o% the geoetr$ shader e1ecuta:le on the geoetr$ -rocessor will o-erate on a
declared in-ut -riitive with a %i1ed nu:er o% vertices. ,his single invocation can eit a varia:le
nu:er o% vertices that are asse:led into -riitives o% a declared out-ut -riitive t$-e and -assed to
su:se<uent -i-eline stages.
2.' ,ragment +rocessor
,he ragment "rocessor is a -rograa:le unit that o-erates on %ragent values and their associated
data. "o-ilation units written in the )-en/0 #hading 0anguage to run on this -rocessor are called
ragment s%aders. @hen a co-lete set o% %ragent shaders are co-iled and linked, the$ result in a
ragment s%ader e#ecutable that runs on the %ragent -rocessor.
A %ragent shader cannot change a %ragentDs B#, *C -osition. Access to neigh:oring %ragents is not
allowed. ,he values co-uted :$ the %ragent shader are ultiatel$ used to u-date %rae:u%%er eor$
or te1ture eor$, de-ending on the current )-en/0 state and the )-en/0 coand that caused the
%ragents to :e generated.
7
" -asics
".1 Character Set
,he source character set used %or the )-en/0 shading languages, outside o% coents, is a su:set o%
7,495. It includes the %ollowing characters:
,he letters a*+, A*,- and the underscore B ? C.
,he nu:ers .*/.
,he s$:ols -eriod B.C, -lus B0C, dash B*C, slash B1C, asterisk B2C, -ercent B3C, angled :rackets B4 and
5C, s<uare :rackets B 6 and 7 C, -arentheses B 8 and 9 C, :races B : and ; C, caret B<C, vertical :ar B = C,
a-ersand B>C, tilde B?C, e<uals B@C, e1claation -oint BAC, colon BBC, seicolon BCC, coa B-C, and
<uestion ark BDC.
,he nu:er sign B#C %or -re-rocessor use.
,he :ackslash B E C as the line9continuation character when used as the last character o% a line, Lust
:e%ore a new line.
@hite s-ace: the s-ace character, hori=ontal ta:, vertical ta:, %or %eed, carriage9return, and line9
%eed.
An error will :e given i% an$ other character is used outside a coent.
,here are no trigra-hs. ,here are no esca-e se<uences or other uses o% the :ackslash :e$ond use as the
line9continuation character.
0ines are relevant %or co-iler diagnostic essages and the -re-rocessor. ,he$ are terinated :$
carriage9return or line9%eed. I% :oth are used together, it will count as onl$ a single line terination. 4or
the reainder o% this docuent, an$ o% these co:inations is si-l$ re%erred to as a new line.
In general, the languageMs use o% this character set is case sensitive.
,here are no character or string data t$-es, so no <uoting characters are included.
,here is no end9o%9%ile character.
".2 Source Strings
,he source %or a single shader is an arra$ o% strings o% characters %ro the character set. A single shader
is ade %ro the concatenation o% these strings. Each string can contain ulti-le lines, se-arated :$ new
lines. 8o new lines need :e -resent in a stringN a single line can :e %ored %ro ulti-le strings. 8o new
lines or other characters are inserted :$ the i-leentation when it concatenates the strings to %or a
single shader. ;ulti-le shaders can :e linked together to %or a single -rogra.
Diagnostic essages returned %ro co-iling a shader ust identi%$ :oth the line nu:er within a string
and which source string the essage a--lies to. #ource strings are counted se<uentiall$ with the %irst
8
3 Baic
string :eing string '. 0ine nu:ers are one ore than the nu:er o% new lines that have :een -rocessed,
including counting the new lines that will :e reoved :$ the line9continuation character B E C.
0ines se-arated :$ the line9continuation character -receding a new line are concatenated together :e%ore
either coent -rocessing or -re-rocessing. 8o white s-ace is su:stituted %or the line9continuation
character. ,hat is, a single token could :e %ored :$ the concatenation :$ taking the characters at the end
o% one line concatenating the with the characters at the :eginning o% the ne1t line.
float f\
oo;
// forms a single line equivalent to float foo;
// (assuming '\' is the last character before the new line and oo are
// the first two characters of the next line)
"." +reprocessor
,here is a -re-rocessor that -rocesses the source strings as -art o% the co-ilation -rocess.
,he co-lete list o% -re-rocessor directives is as %ollows.
#
#define
#undef
#if
#ifdef
#ifndef
#else
#elif
#endif
#error
#ragma
#extension
#version
#line
,he %ollowing o-erators are also availa:le
defined
##
Each nu:er sign B#C can :e -receded in its line onl$ :$ s-aces or hori=ontal ta:s. It a$ also :e
%ollowed :$ s-aces and hori=ontal ta:s, -receding the directive. Each directive is terinated :$ a new
line. 2re-rocessing does not change the nu:er or relative location o% new lines in a source string.
2re-rocessing takes -laces a%ter new lines have :een reoved :$ the line9continuation character.
,he nu:er sign B#C on a line :$ itsel% is ignored. An$ directive not listed a:ove will cause a diagnostic
essage and ake the i-leentation treat the shader as ill9%ored.
!
3 Baic
#define and #undef %unctionalit$ are de%ined as is standard %or "FF -re-rocessors %or acro de%initions
:oth with and without acro -araeters.
,he %ollowing -rede%ined acros are availa:le
!!"#$%!!
!!&#"%!!
!!'%()#*$!!
__L+,-__ will su:stitute a decial integer constant that is one ore than the nu:er o% -receding new
lines in the current source string.
__F+L-__ will su:stitute a decial integer constant that sa$s which source string nu:er is currentl$
:eing -rocessed.
__V-R(+&,__ will su:stitute a decial integer re%lecting the version nu:er o% the )-en/0 shading
language. ,he version o% the shading language descri:ed in this docuent will have __V-R(+&,__
su:stitute the decial integer &!'.
All acro naes containing two consecutive underscores B __ C are reserved %or %uture use as -rede%ined
acro naes. All acro naes -re%i1ed with G/0?I BG/0I %ollowed :$ a single underscoreC are also
reserved.
#if- #ifdef- #ifndef- #else- #elif- and #endif are de%ined to o-erate as is standard %or "FF -re-rocessors.
E1-ressions %ollowing #if and #elif are %urther restricted to e1-ressions o-erating on literal integer
constants, -lus identi%iers consued :$ the defined o-erator. It is an error to use #if or #elif on
e1-ressions containing unde%ined acro naes, other than as arguents to the defined o-erator.
"haracter constants are not su--orted. ,he o-erators availa:le are as %ollows.
+recedence Operator class Operators .ssociati#ity
1 BhighestC -arenthetical grou-ing B C 8A
! unar$ de%ined
F 9 O P
Right to 0e%t
( ulti-licative K H Q 0e%t to Right
& additive F 9 0e%t to Right
+ :it9wise shi%t RR SS 0e%t to Right
. relational R S RT ST 0e%t to Right
3 e<ualit$ TT PT 0e%t to Right
5 :it9wise and U 0e%t to Right
6 :it9wise e1clusive or V 0e%t to Right
1' :it9wise inclusive or W 0e%t to Right
11 logical and UU 0e%t to Right
1! BlowestC logical inclusive or W W 0e%t to Right
1"
3 Baic
,he defined o-erator can :e used in either o% the %ollowing wa$s:
defined identifier
defined ( identifier )
,wo tokens in a acro can :e concatenated into one token using the token -asting B##C o-erator, as is
standard %or "FF -re-rocessors. ,he result ust :e a valid single token, which will then :e su:Lect to
acro e1-ansion. ,hat is, acro e1-ansion ha--ens onl$ a%ter token -asting. ,here are no other nu:er
sign :ased o-erators Be.g., no # or #FC, nor is there a si+eof o-erator.
,he seantics o% a--l$ing o-erators to integer literals in the -re-rocessor atch those standard in the "F
F -re-rocessor, not those in the )-en/0 #hading 0anguage.
2re-rocessor e1-ressions will :e evaluated according to the :ehavior o% the host -rocessor, not the
-rocessor targeted :$ the shader.
#error will cause the i-leentation to -ut a diagnostic essage into the shader o:LectMs in%oration log
Bsection ..1.1! G#hader and 2rogra >ueriesI in the )-en/0 /ra-hics #$ste #-eci%ication %or how to
access a shader o:LectMs in%oration logC. ,he essage will :e the tokens %ollowing the #error directive,
u- to the %irst new line. ,he i-leentation ust then consider the shader to :e ill9%ored.
#ragma allows i-leentation de-endent co-iler control. ,okens %ollowing #ragma are not su:Lect
to -re-rocessor acro e1-ansion. I% an i-leentation does not recogni=e the tokens %ollowing
#ragma, then it will ignore that -raga. ,he %ollowing -ragas are de%ined as -art o% the language.
#ragma )+,-"
,he %GD(H -raga is used to reserve -ragas %or use :$ %uture revisions o% this language. 8o
i-leentation a$ use a -raga whose %irst token is %GD(H.
#ragma otimi.e(on)
#ragma otimi.e(off)
can :e used to turn o%% o-tii=ations as an aid in develo-ing and de:ugging shaders. It can onl$ :e used
outside %unction de%initions. B$ de%ault, o-tii=ation is turned on %or all shaders. ,he de:ug -raga
#ragma debug(on)
#ragma debug(off)
can :e used to ena:le co-iling and annotating a shader with de:ug in%oration, so that it can :e used
with a de:ugger. It can onl$ :e used outside %unction de%initions. B$ de%ault, de:ug is turned o%%.
#haders should declare the version o% the language the$ are written to. ,he language version a shader is
written to is s-eci%ied :$
#version number profile
opt
where number ust :e a version o% the language, %ollowing the sae convention as __V-R(+&,__ a:ove.
,he directive G#version I".I is re<uired in an$ shader that uses version &.!' o% the language. An$
number re-resenting a version o% the language a co-iler does not su--ort will cause an error to :e
generated. Version 1.1' o% the language does not re<uire shaders to include this directive, and shaders that
do not include a #version directive will :e treated as targeting version 1.1'. #haders that s-eci%$
#version 1'' will :e treated as targeting version 1.'' o% the )-en/0 E# #hading 0anguage.
11
3 Baic
#haders declaring version 1.&', 1.+', (.(', &.', or &.1 o% the shading language can :e linked with shaders
declaring version &.!' in the sae -rogra. #haders targeting earlier versions B1.(' or earlierC o% the
shading language cannot :e linked with version &.!' shaders.
I% the o-tional "roile arguent is -rovided, it ust :e the nae o% an )-en/0 -ro%ile. "urrentl$, there
are two choices:
core
comatibilit/
I% no "roile arguent is -rovided, the de%ault is core. 7nless otherwise s-eci%ied, this s-eci%ication is
docuenting the core -ro%ile, and ever$thing s-eci%ied %or the core -ro%ile is also availa:le in the
co-ati:ilit$ -ro%ile. 4eatures s-eci%ied as :elonging s-eci%icall$ to the co-ati:ilit$ -ro%ile are not
availa:le in the core -ro%ile.
,here is a :uilt9in acro de%inition %or each -ro%ile the i-leentation su--orts. All i-leentations
-rovide the %ollowing acro:
#define -"!core!rofile 0
I-leentations -roviding the co-ati:ilit$ -ro%ile -rovide the %ollowing acro:
#define -"!comatibilit/!rofile 0
,he #version directive ust occur in a shader :e%ore an$thing else, e1ce-t %or coents and white s-ace.
12
3 Baic
B$ de%ault, co-ilers o% this language ust issue co-ile tie s$ntactic, graatical, and seantic
errors %or shaders that do not con%or to this s-eci%ication. An$ e1tended :ehavior ust %irst :e ena:led.
Directives to control the :ehavior o% the co-iler with res-ect to e1tensions are declared with the
#e#tension directive
#extension extension_name : behavior
#extension all : behavior
where e#tension_name is the nae o% an e1tension. E1tension naes are not docuented in this
s-eci%ication. ,he token all eans the :ehavior a--lies to all e1tensions su--orted :$ the co-iler. ,he
be%avior can :e one o% the %ollowing
behavior %ffect
re/uire Behave as s-eci%ied :$ the e1tension e#tension_name.
/ive an error on the #e#tension i% the e1tension e#tension_name is not
su--orted, or i% all is s-eci%ied.
enable Behave as s-eci%ied :$ the e1tension e#tension_name.
@arn on the #e#tension i% the e1tension e#tension_name is not su--orted.
/ive an error on the #e#tension i% all is s-eci%ied.
$arn Behave as s-eci%ied :$ the e1tension e#tension_name, e1ce-t issue warnings
on an$ detecta:le use o% that e1tension, unless such use is su--orted :$ other
ena:led or re<uired e1tensions.
I% all is s-eci%ied, then warn on all detecta:le uses o% an$ e1tension used.
@arn on the #e#tension i% the e1tension e#tension_name is not su--orted.
disable Behave Bincluding issuing errors and warningsC as i% the e1tension
e#tension_name is not -art o% the language de%inition.
I% all is s-eci%ied, then :ehavior ust revert :ack to that o% the non9e1tended
core version o% the language :eing co-iled to.
@arn on the #e#tension i% the e1tension e#tension_name is not su--orted.
,he e#tension directive is a si-le, low9level echanis to set the :ehavior %or each e1tension. It does
not de%ine -olicies such as which co:inations are a--ro-riate, those ust :e de%ined elsewhere. )rder
o% directives atters in setting the :ehavior %or each e1tension: Directives that occur later override those
seen earlier. ,he all variant sets the :ehavior %or all e1tensions, overriding all -reviousl$ issued
e#tension directives, :ut onl$ %or the be%aviors warn and disable.
13
3 Baic
,he initial state o% the co-iler is as i% the directive
#extension all 1 disable
was issued, telling the co-iler that all error and warning re-orting ust :e done according to this
s-eci%ication, ignoring an$ e1tensions.
Each e1tension can de%ine its allowed granularit$ o% sco-e. I% nothing is said, the granularit$ is a shader
Bthat is, a single co-ilation unitC, and the e1tension directives ust occur :e%ore an$ non9-re-rocessor
tokens. I% necessar$, the linker can en%orce granularities larger than a single co-ilation unit, in which
case each involved shader will have to contain the necessar$ e1tension directive.
;acro e1-ansion is not done on lines containing #e#tension and #version directives.
#line ust have, a%ter acro su:stitution, one o% the %ollowing %ors:
#line line
#line line source-string-number
where line and source-string-number are constant integer e1-ressions. A%ter -rocessing this directive
Bincluding its new lineC, the i-leentation will :ehave as i% it is co-iling at line nu:er line and source
string nu:er source-string-number. #u:se<uent source strings will :e nu:ered se<uentiall$, until
another #line directive overrides that nu:ering.
". Comments
"oents are deliited :$ HK and KH, or :$ HH and a new line. ,he :egin coent deliiters BHK or HHC are
not recogni=ed as coent deliiters inside o% a coent, hence coents cannot :e nested. I% a
coent resides entirel$ within a single line, it is treated s$ntacticall$ as a single s-ace. 8ew lines are
not eliinated :$ coents.
Inside coents, an$ :$te values a$ :e used, e1ce-t a :$te whose value is '. 8o errors will :e given
%or the content o% coents and no validation on the content o% coents need :e done.
Reoval o% new lines :$ the line9continuation character B E C logicall$ occurs :e%ore coents are
-rocessed. ,hat is, a single9line coent ending in the line9continuation character B E C includes the ne1t
line in the coent.
// a single2line comment containing the next line \
a 3 b; // this is still in the first comment
14
3 Baic
".' To0ens
,he language is a se<uence o% tokens. A token can :e
to.en:
.e*/ord
identiier
integer-constant
loating-constant
o"erator
C : ;
".( 1ey$ords
,he %ollowing are the ke$words in the language, and cannot :e used %or an$ other -ur-ose than that
de%ined :$ this docuent:
attribute const uniform varying
coherent volatile restrict readonly writeonly
atomic_uint
layout
centroid flat smooth noersective
atch samle
break continue do for while switch case default
if else
subroutine
in out inout
float double int void bool true false
invariant
discard return
mat" mat' matI dmat" dmat' dmatI
mat"#" mat"#' mat"#I dmat"#" dmat"#' dmat"#I
mat'#" mat'#' mat'#I dmat'#" dmat'#' dmat'#I
matI#" matI#' matI#I dmatI#" dmatI#' dmatI#I
vec" vec' vecI ivec" ivec' ivecI bvec" bvec' bvecI dvec" dvec' dvecI
uint uvec" uvec' uvecI
15
3 Baic
low medium high recision
samler1D samler"D samler'D samlerCube
samler1D%hadow samler"D%hadow samlerCube%hadow
samler1DArray samler"DArray
samler1DArray%hadow samler"DArray%hadow
isamler1D isamler"D isamler'D isamlerCube
isamler1DArray isamler"DArray
usamler1D usamler"D usamler'D usamlerCube
usamler1DArray usamler"DArray
samler"DJect samler"DJect%hadow isamler"DJect usamler"DJect
samlerKuffer isamlerKuffer usamlerKuffer
samler"DL% isamler"DL% usamler"DL%
samler"DL%Array isamler"DL%Array usamler"DL%Array
samlerCubeArray samlerCubeArray%hadow isamlerCubeArray usamlerCubeArray
image1D iimage1D uimage1D
image"D iimage"D uimage"D
image'D iimage'D uimage'D
image"DJect iimage"DJect uimage"DJect
imageCube iimageCube uimageCube
imageKuffer iimageKuffer uimageKuffer
image1DArray iimage1DArray uimage1DArray
image"DArray iimage"DArray uimage"DArray
imageCubeArray iimageCubeArray uimageCubeArray
image"DL% iimage"DL% uimage"DL%
image"DL%Array iimage"DL%Array uimage"DL%Array
struct
,he %ollowing are the ke$words reserved %or %uture use. 7sing the will result in an error:
common artition active
asm
class union enum tyedef temlate this acked
resource
16
3 Baic
goto
inline noinline ublic static e#tern e#ternal interface
long short half fi#ed unsigned suer
inut outut
hvec" hvec' hvecI fvec" fvec' fvecI
samler'DJect
filter
si+eof cast
namesace using
row_maMor
In addition, all identi%iers containing two consecutive underscores B__C are reserved as -ossi:le %uture
ke$words.
".2 Identifiers
Identi%iers are used %or varia:le naes, %unction naes, structure naes, and %ield selectors B%ield
selectors select co-onents o% vectors and atrices siilar to structure e:ers, as discussed in section
+.+ GVector "o-onentsI and section +.. G;atri1 "o-onentsI C. Identi%iers have the %or
identiier
nondigit
identiier nondigit
identiier digit
nondigit: one o%
_ a b c d e f g h i M k l m n o N r s t u v w # y +
A K C D O & ( ! I P Q H L R ) S T J % G U V W X Y ,
digit: one o%
. 1 " ' I Z $ [ \ /
Identi%iers starting with Ggl?I are reserved %or use :$ )-en/0, and a$ not :e declared in a shader as
either a varia:le or a %unction. *owever, as noted in the s-eci%ication, there are soe cases where
-reviousl$ declared varia:les can :e redeclared, and -redeclared Xgl?X naes are allowed to :e
redeclared in a shader onl$ %or these s-eci%ic -ur-oses. ;ore generall$, it is an error to redeclare a
varia:le, including those starting Ggl?I.
".3 )efinitions
#oe language rules descri:ed :elow de-end on the %ollowing de%initions.
17
3 Baic
".3.1 Static 4se
A shader contains a static use o% Bor static assignment toC a varia:le # i%, a%ter -re-rocessing, the shader
contains a stateent that would read Bor writeC #, whether or not run9tie %low o% control will cause that
stateent to :e e1ecuted.
".3.2 4niform and 5on64niform Control ,lo$
@hen e1ecuting stateents in a %ragent shader, control %low starts as uniorm control lo/N all %ragents
enter the sae control -ath into mainBC. "ontrol %low :ecoes non-uniorm when di%%erent %ragents
take di%%erent -aths through control9%low stateents Bselection, iteration, and Lu-sC. "ontrol %low
su:se<uentl$ returns to :eing uni%or a%ter such divergent su:9stateents or ski--ed code co-letes,
until the ne1t tie di%%erent control -aths are taken.
4or e1a-le:
main()
4
float a 3 555;// this is uniform flow control
if (a 6 b) 4 // this exression is true for some fragments7 not all
5555; // non2uniform flow control
8 else 4
5555; // non2uniform flow control
8
5555; // uniform flow control again
8
)ther e1a-les o% non9uni%or %low control can occur within switch stateents and a%ter conditional
:reaks, continues, earl$ returns, and a%ter %ragent discards, when the condition is true %or soe
%ragents :ut not others. 0oo- iterations that onl$ soe %ragents e1ecute are also non9uni%or %low
control.
,his is siilarl$ de%ined %or other shader stages, :ased on the -er9instance data ites the$ -rocess.
".3." )ynamically 4niform %*pressions
A %ragent9shader e1-ression is d*namicall* uniorm i% all %ragents evaluating it get the sae resulting
value. @hen loo-s are involved, this re%ers to the e1-ressionDs value %or the sae loo- iteration. @hen
%unctions are involved, this re%ers to calls %ro the sae call -oint.
,his is siilarl$ de%ined %or other shader stages, :ased on the -er9instance data the$ -rocess.
8ote that constant e1-ressions are triviall$ d$naicall$ uni%or. It %ollows that t$-ical loo- counters
:ased on these are also d$naicall$ uni%or.
18
Variables and Types
All varia:les and %unctions ust :e declared :e%ore :eing used. Varia:le and %unction naes are
identi%iers.
,here are no de%ault t$-es. All varia:le and %unction declarations ust have a declared t$-e, and
o-tionall$ <uali%iers. A varia:le is declared :$ s-eci%$ing its t$-e %ollowed :$ one or ore naes
se-arated :$ coas. In an$ cases, a varia:le can :e initiali=ed as -art o% its declaration :$ using the
assignent o-erator B@C.
7ser9de%ined t$-es a$ :e de%ined using struct to aggregate a list o% e1isting t$-es into a single nae.
,he )-en/0 #hading 0anguage is t$-e sa%e. ,here are soe i-licit conversions :etween t$-es.
E1actl$ how and when this can occur is descri:ed in section &.1.1' GI-licit "onversionsI and as
re%erenced :$ other sections in this s-eci%ication.
.1 -asic Types
,he )-en/0 #hading 0anguage su--orts the %ollowing :asic data t$-es, grou-ed as %ollows.
,rans-arent t$-es
Type 7eaning
void %or %unctions that do not return a value
bool a conditional t$-e, taking on values o% true or %alse
int a signed integer
uint an unsigned integer
float a single %loating9-oint scalar
double a single dou:le9-recision %loating -oint scalar
vec" a two9co-onent %loating9-oint vector
vec' a three9co-onent %loating9-oint vector
vecI a %our9co-onent %loating9-oint vector
dvec" a two9co-onent dou:le9-recision %loating9-oint vector
dvec' a three9co-onent dou:le9-recision %loating9-oint vector
dvecI a %our9co-onent dou:le9-recision %loating9-oint vector
bvec" a two9co-onent Boolean vector
bvec' a three9co-onent Boolean vector
bvecI a %our9co-onent Boolean vector
ivec" a two9co-onent signed integer vector
1!
4 #aria$%e and &'pe
Type 7eaning
ivec' a three9co-onent signed integer vector
ivecI a %our9co-onent signed integer vector
uvec" a two9co-onent unsigned integer vector
uvec' a three9co-onent unsigned integer vector
uvecI a %our9co-onent unsigned integer vector
mat" a !Y! %loating9-oint atri1
mat' a (Y( %loating9-oint atri1
matI a &Y& %loating9-oint atri1
mat"#" sae as a mat"
mat"#' a %loating9-oint atri1 with ! coluns and ( rows
mat"#I a %loating9-oint atri1 with ! coluns and & rows
mat'#" a %loating9-oint atri1 with ( coluns and ! rows
mat'#' sae as a mat'
mat'#I a %loating9-oint atri1 with ( coluns and & rows
matI#" a %loating9-oint atri1 with & coluns and ! rows
matI#' a %loating9-oint atri1 with & coluns and ( rows
matI#I sae as a matI
dmat" a !Y! dou:le9-recision %loating9-oint atri1
dmat' a (Y( dou:le9-recision %loating9-oint atri1
dmatI a &Y& dou:le9-recision %loating9-oint atri1
dmat"#" sae as a dmat"
dmat"#' a dou:le9-recision %loating9-oint atri1 with ! coluns and ( rows
dmat"#I a dou:le9-recision %loating9-oint atri1 with ! coluns and & rows
dmat'#" a dou:le9-recision %loating9-oint atri1 with ( coluns and ! rows
dmat'#' sae as a dmat'
dmat'#I a dou:le9-recision %loating9-oint atri1 with ( coluns and & rows
dmatI#" a dou:le9-recision %loating9-oint atri1 with & coluns and ! rows
dmatI#' a dou:le9-recision %loating9-oint atri1 with & coluns and ( rows
dmatI#I sae as a dmatI
2"
4 #aria$%e and &'pe
4loating 2oint )-a<ue ,$-es
Type 7eaning
samler1D
image1D
a handle %or accessing a 1D te1ture
samler"D
image"D
a handle %or accessing a !D te1ture
samler'D
image'D
a handle %or accessing a (D te1ture
samlerCube
imageCube
a handle %or accessing a cu:e a--ed te1ture
samler"DJect
image"DJect
a handle %or accessing a rectangular te1ture
samler1DArray
image1DArray
a handle %or accessing a 1D arra$ te1ture
samler"DArray
image"DArray
a handle %or accessing a !D arra$ te1ture
samlerKuffer
imageKuffer
a handle %or accessing a :u%%er te1ture
samler"DL%
image"DL%
a handle %or accessing a !D ulti9sa-le te1ture
samler"DL%Array
image"DL%Array
a handle %or accessing a !D ulti9sa-le arra$ te1ture
samlerCubeArray
imageCubeArray
a handle %or accessing a cu:e a- arra$ te1ture
samler1D%hadow a handle %or accessing a 1D de-th te1ture with co-arison
samler"D%hadow a handle %or accessing a !D de-th te1ture with co-arison
samler"DJect%hadow a handle %or accessing a rectangular te1ture with co-arison
samler1DArray%hadow a handle %or accessing a 1D arra$ de-th te1ture with co-arison
samler"DArray%hadow a handle %or accessing a !D arra$ de-th te1ture with co-arison
samlerCube%hadow a handle %or accessing a cu:e a- de-th te1ture with co-arison
samlerCubeArray%hadow a handle %or accessing a cu:e a- arra$ de-th te1ture with
co-arison
#igned Integer )-a<ue ,$-es
Type 7eaning
isamler1D
iimage1D
a handle %or accessing an integer 1D te1ture
21
4 #aria$%e and &'pe
Type 7eaning
isamler"D
iimage"D
a handle %or accessing an integer !D te1ture
isamler'D
iimage'D
a handle %or accessing an integer (D te1ture
isamlerCube
iimageCube
a handle %or accessing an integer cu:e a--ed te1ture
isamler"DJect
iimage"DJect
a handle %or accessing an integer !D rectangular te1ture
isamler1DArray
iimage1DArray
a handle %or accessing an integer 1D arra$ te1ture
isamler"DArray
iimage"DArray
a handle %or accessing an integer !D arra$ te1ture
isamlerKuffer
iimageKuffer
a handle %or accessing an integer :u%%er te1ture
isamler"DL%
iimage"DL%
a handle %or accessing an integer !D ulti9sa-le te1ture
isamler"DL%Array
iimage"DL%Array
a handle %or accessing an integer !D ulti9sa-le arra$ te1ture
isamlerCubeArray
iimageCubeArray
a handle %or accessing an integer cu:e a- arra$ te1ture
7nsigned Integer )-a<ue ,$-es
Type 7eaning
atomic_uint a handle %or accessing an unsigned integer atoic counter
usamler1D
uimage1D
a handle %or accessing an unsigned integer 1D te1ture
usamler"D
uimage"D
a handle %or accessing an unsigned integer !D te1ture
usamler'D
uimage'D
a handle %or accessing an unsigned integer (D te1ture
usamlerCube
uimageCube
a handle %or accessing an unsigned integer cu:e a--ed te1ture
usamler"DJect
uimage"DJect
a handle %or accessing an unsigned integer rectangular te1ture
usamler1DArray
uimage1DArray
a handle %or accessing an unsigned integer 1D arra$ te1ture
usamler"DArray
uimage"DArray
a handle %or accessing an unsigned integer !D arra$ te1ture
22
4 #aria$%e and &'pe
Type 7eaning
usamlerKuffer
uimageKuffer
a handle %or accessing an unsigned integer :u%%er te1ture
usamler"DL%
uimage"DL%
a handle %or accessing an unsigned integer !D ulti9sa-le te1ture
usamler"DL%Array
uimage"DL%Array
a handle %or accessing an unsigned integer !D ulti9sa-le te1ture
arra$
usamlerCubeArray
uimageCubeArray
a handle %or accessing an unsigned integer cu:e a- arra$ te1ture
In addition, a shader can aggregate these :asic t$-es using arra$s and structures to :uild ore co-le1
t$-es.
,here are no -ointer t$-es.
.1.1 Void
4unctions that do not return a value ust :e declared as void. ,here is no de%ault %unction return t$-e.
,he ke$word void cannot :e used in an$ other declarations Be1ce-t %or e-t$ %oral or actual -araeter
listsC.
.1.2 -ooleans
,o ake conditional e1ecution o% code easier to e1-ress, the t$-e bool is su--orted. ,here is no
e1-ectation that hardware directl$ su--orts varia:les o% this t$-e. It is a genuine Boolean t$-e, holding
onl$ one o% two values eaning either true or %alse. ,wo ke$words true and false can :e used as literal
Boolean constants. Booleans are declared and o-tionall$ initiali=ed as in the %ollow e1a-le:
bool success; // declare success to be a 9oolean
bool done 3 false; // declare and initiali.e done
,he right side o% the assignent o-erator B @ C ust :e an e1-ression whose t$-e is bool.
E1-ressions used %or conditional Lu-s Bif- for- DB- while- do*whileC ust evaluate to the t$-e bool.
.1." Integers
#igned and unsigned integer varia:les are %ull$ su--orted. In this docuent, the ter integer is eant to
generall$ include :oth signed and unsigned integers. 7nsigned integers have e1actl$ (! :its o% -recision.
#igned integers use (! :its, including a sign :it, in twoDs co-leent %or. )-erations resulting in
over%low or under%low will not cause an$ e1ce-tion, nor will the$ saturate, rather the$ will Gwra-I to $ield
the low9order (! :its o% the result.
Integers are declared and o-tionall$ initiali=ed with integer e1-ressions, as in the %ollowing e1a-le:
int i7 : 3 ;<; // default integer literal t/e is int
uint = 3 >u; // u establishes the t/e as uint
23
4 #aria$%e and &'pe
0iteral integer constants can :e e1-ressed in decial B:ase 1'C, octal B:ase 5C, or he1adecial B:ase 1.C
as %ollows.
integer-constant :
decimal-constant integer-sui#o"t
octal-constant integer-sui#o"t
%e#adecimal-constant integer-sui#o"t
integer-sui#: one o%
u U
decimal-constant :
non0ero-digit
decimal-constant digit
octal-constant :
.
octal-constant octal-digit
%e#adecimal-constant :
'1 %e#adecimal-digit
'Z %e#adecimal-digit
%e#adecimal-constant %e#adecimal-digit
digit :
.
non0ero-digit
non0ero-digit : one o%
1 " ' I Z $ [ \ /
octal-digit B one o%
. 1 " ' I Z $ [
%e#adecimal-digit B one o%
. 1 " ' I Z $ [ \ /
a b c d e f
A K C D O &
8o white s-ace is allowed :etween the digits o% an integer constant, including a%ter the leading . or a%ter
the leading .# or .X o% a constant, or :e%ore the su%%i1 u or U. @hen tokeni=ing, the a1ial token
atching the a:ove will :e recogni=ed :e%ore a new token is started. @hen the su%%i1 u or U is -resent,
the literal has t$-e uint, otherwise the t$-e is int. A leading unar$ inus sign B9C is inter-reted as an
arithetic unar$ negation, not as -art o% the constant. *ence, literals theselves are alwa$s e1-ressed
with non9negative s$nta1, though the$ could result in a negative value.
It is an error to -rovide a literal integer whose :it -attern cannot %it in (! :its. ,he :it -attern o% the literal
is alwa$s used unodi%ied. #o a signed literal whose :it -attern includes a set sign :it creates a negative
value. 4or e1a-le,
24
4 #aria$%e and &'pe
int a 3 ?xffffffff; // >< bits7 a gets the value 20
int b 3 ?xffffffff@; // %((*(1 can't convert uint to int
uint c 3 ?xffffffff; // >< bits7 c gets the value ?x&&&&&&&&
uint d 3 ?xffffffff@; // >< bits7 d gets the value ?x&&&&&&&&
int e 3 20; // the literal is 07 then negation is erformed7
// and the resulting non2literal ><2bit signed
// bit attern of ?x&&&&&&&& is assigned7 giving e
// the value of 205
uint f 3 20u; // the literal is 0u7 then negation is erformed7
// and the resulting non2literal ><2bit unsigned
// bit attern of ?x&&&&&&&& is assigned7 giving f
// the value of ?x&&&&&&&&5
int g 3 >?????????; // a signed decimal literal ta=ing >< bits7
// setting the sign bit7 g gets 20<A;ABC<AB
int h 3 ?xD???????; // o=a/7 ><2bit signed hexadecimal
int i 3 E?????????; // %((*(1 needs more than >< bits
int : 3 ?x&&&&&&&&&; // %((*(1 needs more that >< bits
int = 3 ?xF???????; // = gets 2<0;C;F>B;F 33 ?xF???????
int l 3 <0;C;F>B;F; // l gets 2<0;C;F>B;F (the literal set the sign bit)
Des-ite all these e1a-les initiali=ing varia:les, literals are recogni=ed and given values and t$-es
inde-endentl$ o% their conte1t.
.1. ,loats
#ingle9-recision and dou:le9-recision %loating -oint varia:les are availa:le %or use in a variet$ o% scalar
calculations. 4loating9-oint varia:les are de%ined as in the %ollowing e1a-le:
float a7 b 3 05E;
double c7 d 3 <5?"&;
As an in-ut value to one o% the -rocessing units, a single9-recision or dou:le9-recision %loating9-oint
varia:le is e1-ected to atch the corres-onding IEEE 3+& %loating9-oint de%inition %or -recision and
d$naic range. 4loating9-oint varia:les within a shader are also encoded according to the IEEE 3+&
s-eci%ication %or single9-recision %loating9-oint values Blogicall$, not necessaril$ -h$sicall$C. @hile
encodings are logicall$ IEEE 3+&, o-erations Baddition, ulti-lication, etc.C are not necessaril$ -er%ored
as re<uired :$ IEEE 3+&. #ee section &.3.1 GRange and 2recisionI %or ore details on -recision and
usage o% 8a8s B8ot a 8u:erC and In%s B-ositive or negative in%initiesC.
25
4 #aria$%e and &'pe
4loating9-oint constants are de%ined as %ollows.
loating-constant :
ractional-constant e#"onent-"art
o"t
loating-sui#o"t

digit-se1uence e#"onent-"art loating-sui#o"t
ractional-constant :
digit-se1uence . digit-se1uence
digit-se1uence .
. digit-se1uence
e#"onent-"art :
e sign
o"t
digit-se1uence
E sign
o"t
digit-se1uence
sign : one o%
0 ]
digit-se1uence :
digit
digit-se1uence digit
loating-sui#: one o%
f & lf H&
A decial -oint B . C is not needed i% the e1-onent -art is -resent. 8o white s-ace a$ a--ear an$where
within a %loating9-oint constant, including :e%ore a su%%i1. @hen tokeni=ing, the a1ial token atching
the a:ove will :e recogni=ed :e%ore a new token is started. @hen the su%%i1 Xl%X or X04X is -resent, the
literal has t$-e double. )therwise, the literal has t$-e float. A leading unar$ inus sign B*C is inter-reted
as a unar$ o-erator and is not -art o% the %loating9-oint constant
.1.' Vectors
,he )-en/0 #hading 0anguage includes data t$-es %or generic !9, (9, and &9co-onent vectors o%
%loating9-oint values, integers, or Booleans. 4loating9-oint vector varia:les can :e used to store colors,
norals, -ositions, te1ture coordinates, te1ture looku- results and the like. Boolean vectors can :e used
%or co-onent9wise co-arisons o% nueric vectors. #oe e1a-les o% vector declaration are:
vec< texcoord07 texcoord<;
vec> osition;
vec; m/(-9D;
ivec< texture"oo=u;
bvec> less;
Initiali=ation o% vectors can :e done with constructors, which are discussed shortl$.
.1.( 7atrices
,he )-en/0 #hading 0anguage has :uilt9in t$-es %or !Y!, !Y(, !Y&, (Y!, (Y(, (Y&, &Y!, &Y(, and &Y&
atrices o% %loating9-oint nu:ers. ;atri1 t$-es :eginning with XatX have single9-recision co-onents
26
4 #aria$%e and &'pe
while atri1 t$-es :eginning with XdatX have dou:le9-recision co-onents. ,he %irst nu:er in the
t$-e is the nu:er o% coluns, the second is the nu:er o% rows. I% there is onl$ one nu:er, the atri1
is s<uare. E1a-le atri1 declarations:
mat< mat<,;
mat> otGatrix;
mat; view7 ro:ection;
mat;x; view; // an alternate wa/ of declaring a mat;
mat>x< m; // a matrix with > columns and < rows
dmat; highHrecisionG'H;
dmat<x; dm;
Initiali=ation o% atri1 values is done with constructors Bdescri:ed in section +.& G"onstructorsI C in
colun9aLor order.
.1.2 Opa/ue Types
,he o-a<ue t$-es declare varia:les that are e%%ectivel$ o-a<ue handles to other o:Lects. ,hese o:Lects are
accessed through :uilt9in %unctions, not through direct reading or writing o% the declared varia:le. ,he$
can onl$ :e declared as %unction -araeters or uniform*<uali%ied varia:les. E1ce-t %or arra$ inde1ing,
structure e:er selection, and -arentheses, o-a<ue varia:les are not allowed to :e o-erands in
e1-ressions.
)-a<ue varia:les cannot :e treated as l9valuesN hence cannot :e used as out or inout %unction -araeters,
nor can the$ :e assigned into. *owever, the$ can :e -assed as in -araeters with atching t$-e and
eor$ <uali%iers. ,he$ are initiali=ed onl$ through the )-en/0 A2IN the$ cannot :e declared with an
initiali=er in a shader.
Because a single o-a<ue t$-e declaration e%%ectivel$ declares two o:Lects, the o-a<ue handle itsel% and the
o:Lect it is a handle to, there is roo %or :oth a storage <uali%ier and a eor$ <uali%ier. ,he storage
<uali%ier will <uali%$ the o-a<ue handle, while the eor$ <uali%ier will <uali%$ the o:Lect it as a handle
to.
.1.2.1 Samplers
#a-ler t$-es Be.g., samler"DC are o-a<ue t$-es, declared and :ehaving as descri:ed a:ove %or o-a<ue
t$-es. ,he$ do not use eor$ <uali%iers. @hen aggregated into arra$s within a shader, sa-lers can
onl$ :e inde1ed with a d$naicall$ uni%or integral e1-ression, otherwise results are unde%ined.
#a-ler varia:les are handles to one9, two9, and three9 diensional te1tures, cu:e a-s, de-th te1tures
BshadowingC, etc., as enuerated in the :asic t$-es ta:les. ,here are distinct sa-ler t$-es %or each
te1ture target, and %or each o% %loat, integer, and unsigned integer data t$-es. ,e1ture accesses are done
through :uilt9in te1ture %unctions Bdescri:ed in section 5.6 G,e1ture 4unctionsI I C and sa-lers are used
to s-eci%$ which te1ture to access and how it is to :e %iltered.
.1.2.2 Images
Iage t$-es are o-a<ue t$-es, declared and :ehaving as descri:ed a:ove %or o-a<ue t$-es. ,he$ can :e
%urther <uali%ied with eor$ <uali%iers. @hen aggregated into arra$s within a shader, iages can onl$
:e inde1ed with a d$naicall$ uni%or integral e1-ression, otherwise results are unde%ined.
27
4 #aria$%e and &'pe
Iage varia:les are handles to one9, two9, or three9diensional iages corres-onding to all or a -ortion
o% a single level o% a te1ture iage :ound to an iage unit. ,here are distinct iage t$-es %or each te1ture
target, and %or each o% %loat, integer, and unsigned integer data t$-es. Iage accesses should use an iage
t$-e that atches the target o% the te1ture whose level is :ound to the iage unit, or %or non9la$ered
:indings o% (D or arra$ iages should use the iage t$-e that atches the diensionalit$ o% the la$er o%
the iage Bi.e., a la$er o% (D, !DArra$, "u:e, or "u:eArra$ should use image"D, a la$er o% 1DArra$
should use image1D, and a la$er o% !D;#Arra$ should use image"DL%C. I% the iage target t$-e does
not atch the :ound iage in this anner, i% the data t$-e does not atch the :ound iage, or i% the
%orat la$out <uali%ier does not atch the iage unit %orat as descri:ed in #ection (.6.!' o% the
)-en/0 #-eci%ication, the results o% iage accesses are unde%ined :ut cannot include -rogra
terination.
Iage varia:les are used in the iage load, store, and atoic %unctions descri:ed in #ection 5.11 XIage
4unctionsX to s-eci%$ an iage to access.
.1.2." .tomic Counters
Atoic counter t$-es Batomic_uintC are o-a<ue handles to counters, declared and :ehaving as descri:ed
a:ove %or o-a<ue t$-es. ,he varia:les the$ declare s-eci%$ which counter to access when using the :uilt9
in atoic counter %unctions as descri:ed in section 5.1' GAtoic "ounter 4unctionsI. ,he$ are :ound to
:u%%ers as descri:ed in section &.&.&.1 GAtoic "ounter 0a$out >uali%iersI. @hen aggregated into arra$s
within a shader, atoic counters can onl$ :e inde1ed with a d$naicall$ uni%or integral e1-ression,
otherwise results are unde%ined.
.1.3 Structures
7ser9de%ined t$-es can :e created :$ aggregating other alread$ de%ined t$-es into a structure using the
struct ke$word. 4or e1a-le,
struct light 4
float intensit/;
vec> osition;
8 light'ar;
In this e1a-le, lig%t :ecoes the nae o% the new t$-e, and lig%tVar :ecoes a varia:le o% t$-e lig%t.
,o declare varia:les o% the new t$-e, use its nae Bwithout the ke$word structC.
light light'ar<;
;ore %orall$, structures are declared as %ollows. *owever, the co-lete correct graar is as given in
section 6 G#hading 0anguage /raarI .
struct-deinition :
1ualiier
o"t
struct name
o"t
: member-list ; declarators
o"t
2
member-list :
member-declaration2
member-declaration member-list2
member-declaration :
28
4 #aria$%e and &'pe
basic-t*"e declarators2
where name :ecoes the user9de%ined t$-e, and can :e used to declare varia:les to :e o% this new t$-e.
,he name shares the sae nae s-ace as other varia:les, t$-es, and %unctions. All -reviousl$ visi:le
varia:les, t$-es, constructors, or %unctions with that nae are hidden. ,he o-tional 1ualiier onl$ a--lies
to an$ declarators, and is not -art o% the t$-e :eing de%ined %or name.
#tructures ust have at least one e:er declaration. ;e:er declarators a$ contain -recision
<uali%iers, :ut a$ not contain an$ other <uali%iers. Bit %ields are not su--orted. ;e:er t$-es ust :e
alread$ de%ined Bthere are no %orward re%erencesC. ;e:er declarations cannot contain initiali=ers.
;e:er declarators can contain arra$s. #uch arra$s ust have a si=e s-eci%ied, and the si=e ust :e an
integral constant e1-ression thatDs greater than =ero Bsee section &.(.( G"onstant E1-ressionsIC. Each
level o% structure has its own nae s-ace %or naes given in e:er declaratorsN such naes need onl$
:e uni<ue within that nae s-ace.
Anon$ous structures are not su--orted. E:edded structure de%initions are not su--orted.
struct ) 4 float f; 8;
struct + 4
); // %rror1 anon/mous structures disallowed
struct 4 555 8; // %rror1 embedded structures disallowed
) s; // *=a/1 nested structures with name are allowed
8;
#tructures can :e initiali=ed at declaration tie using constructors, as discussed in section +.&.( G#tructure
"onstructorsI .
An$ restrictions on the usage o% a t$-e or <uali%ier also a--l$ to an$ structure that contains a e:er o%
that t$-e or <uali%ier. ,his also a--lies to structure e:ers that are structures, recursivel$.
.1.8 .rrays
Varia:les o% the sae t$-e can :e aggregated into arra$s :$ declaring a nae %ollowed :$ :rackets B 6 7 C
enclosing an o-tional si=e. @hen an arra$ si=e is s-eci%ied in a declaration, it ust :e an integral constant
e1-ression Bsee section &.(.( G"onstant E1-ressionsI C greater than =ero. I% an arra$ is inde1ed with an
e1-ression that is not an integral constant e1-ression, or i% an arra$ is -assed as an arguent to a %unction,
then its si=e ust :e declared :e%ore an$ such use. It is legal to declare an arra$ without a si=e and then
later re9declare the sae nae as an arra$ o% the sae t$-e and s-eci%$ a si=e. It is illegal to declare an
arra$ with a si=e, and then later Bin the sae shaderC inde1 the sae arra$ with an integral constant
e1-ression greater than or e<ual to the declared si=e. It is also illegal to inde1 an arra$ with a negative
constant e1-ression. Arra$s declared as %oral -araeters in a %unction declaration ust s-eci%$ a si=e.
7nde%ined :ehavior results %ro inde1ing an arra$ with a non9constant e1-ression thatMs greater than or
e<ual to the arra$Ms si=e or less than '. )nl$ one9diensional arra$s a$ :e declared. All :asic t$-es and
structures can :e %ored into arra$s. #oe e1a-les are:
2!
4 #aria$%e and &'pe
float frequenciesI>J;
uniform vec; lightHositionI;J;
light lightsIJ;
const int num"ights 3 <;
light lightsInum"ightsJ;
An arra$ t$-e can :e %ored :$ s-eci%$ing a t$-e %ollowed :$ s<uare :rackets B[ \C and including a si=e:
floatIEJ
,his t$-e can :e used an$where an$ other t$-e can :e used, including as the return value %ro a %unction
floatIEJ foo() 4 8
as a constructor o% an arra$
floatIEJ(>5;7 ;5<7 E5?7 E5<7 050)
as an unnaed -araeter
void foo(floatIEJ)
and as an alternate wa$ o% declaring a varia:le or %unction -araeter.
floatIEJ a;
It is an error to declare arra$s o% arra$s:
float aIEJI>J; // illegal
floatIEJ aI>J; // illegal
Arra$s can have initiali=ers %ored %ro arra$ constructors:
float aIEJ 3 floatIEJ(>5;7 ;5<7 E5?7 E5<7 050);
float aIEJ 3 floatIJ(>5;7 ;5<7 E5?7 E5<7 050); // same thing
7nsi=ed arra$s can :e e1-licitl$ si=ed :$ an initiali=er at declaration tie:
float aIEJ;
555
float bIJ 3 a; // b is exlicitl/ si.e E
float bIEJ 3 a; // means the same thing
float bIJ 3 floatIJ(07<7>7;7E); // also exlicitl/ si.es to E
*owever, i-licitl$ si=ed arra$s cannot :e assigned to. 8ote, this is a rare case that initiali=ers and
assignents a--ear to have di%%erent seantics.
Arra$s know the nu:er o% eleents the$ contain. ,his can :e o:tained :$ using the length ethod:
a5length(); // returns E for the above declarations
,his returns a t$-e int. ,he length ethod cannot :e called on an arra$ that has not :een e1-licitl$ si=ed.
3"
4 #aria$%e and &'pe
.1.1! Implicit Con#ersions
In soe situations, an e1-ression and its t$-e will :e i-licitl$ converted to a di%%erent t$-e. ,he
%ollowing ta:le shows all allowed i-licit conversions:
Type of e*pression Can be implicitly con#erted to
int uint
int
uint
float
int
uint
float
double
ivec" uvec"
ivec' uvec'
ivecI uvecI
ivec"
uvec"
vec"
ivec'
uvec'
vec'
ivecI
uvecI
vecI
ivec"
uvec"
vec"
dvec"
ivec'
uvec'
vec'
dvec'
ivecI
uvecI
vecI
dvecI
mat" dmat"
mat' dmat'
matI dmatI
mat"#' dmat"#'
mat"#I dmat"#I
mat'#" dmat'#"
mat'#I dmat'#I
matI#" dmatI#"
matI#' dmatI#'
31
4 #aria$%e and &'pe
,here are no i-licit arra$ or structure conversions. 4or e1a-le, an arra$ o% int cannot :e i-licitl$
converted to an arra$ o% float.
@hen an i-licit conversion is done, it is not a re9inter-retation o% the e1-ressionDs :it -attern, :ut a
conversion o% its value to an e<uivalent value in the new t$-e. 4or e1a-le, the integer value 9Z will :e
converted to the %loating9-oint value 9Z... Integer values having ore :its o% -recision than a %loating
-oint antissa will lose -recision when converted to float.
@hen -er%oring i-licit conversion %or :inar$ o-erators, there a$ :e ulti-le data t$-es to which the
two o-erands can :e converted. 4or e1a-le, when adding an int value to a uint value, :oth values can
:e i-licitl$ converted to uint, float, and double. In such cases, a %loating9-oint t$-e is chosen i% either
o-erand has a %loating9-oint t$-e. )therwise, an unsigned integer t$-e is chosen i% either o-erand has an
unsigned integer t$-e. )therwise, a signed integer t$-e is chosen. I% o-erands can :e i-licitl$ converted
to ulti-le data t$-es deriving %ro the sae :ase data t$-e, the t$-e with the sallest co-onent si=e is
used.
,he conversions in the ta:le a:ove are done onl$ as indicated :$ other sections o% this s-eci%ication.
.1.11 Initiali9ers
At declaration, an initial value %or an aggregate varia:le a$ :e -rovided, s-eci%ied as an e<uals BTC
%ollowed :$ an initiali=er. ,he initiali=er is either an assignment-e#"ression or a list o% initiali=ers
enclosed in curl$ :races. ,he graar %or the initiali=er is:
initiali0er :
assignment-e#"ression
: initiali0er-list ;
: initiali0er-list , ;
initiali0er-list :
initiali0er
initiali0er-list - initiali0er
,he assignment-e#"ression is a noral e1-ression e1ce-t that a coa B - C outside -arentheses is
inter-reted as the end o% the initiali=er, not as the se<uence o-erator. As e1-lained in ore detail :elow,
this allows creation o% nested initiali=ers: ,he aggregate and its initiali=er ust e1actl$ atch in ters o%
nesting, nu:er o% co-onentsHeleentsHe:ers -resent at each level, and t$-es o%
co-onentsHeleentsHe:ers.
An assignment-e#"ression in an initiali=er ust :e either the sae t$-e as the o:Lect it initiali=es or :e a
t$-e that can :e converted to the o:LectDs t$-e according to section &.1.1' XI-licit "onversionsX. #ince
these include constructors, an aggregate can :e initiali=ed :$ either a constructor or an initiali=er listN an
eleent 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 :eing declared ust :e a
vector, a atri1, an arra$, or a structure.
int i 3 4 0 8; // illegal7 i is not an aggregate
32
4 #aria$%e and &'pe
A list o% initiali=ers enclosed in a atching set o% curl$ :races is a--lied to one aggregate. ,his a$ :e
the varia:le :eing declared or an aggregate contained in the varia:le :eing declared. Individual
initiali=ers %ro the initiali=er list are a--lied to the eleentsHe:ers o% the aggregate, in order.
I% the aggregate has a vector t$-e, initiali=ers %ro the list are a--lied to the co-onents o% the vector, in
order, starting with co-onent '. ,he nu:er o% initiali=ers ust atch the nu:er o% co-onents.
I% the aggregate has a atri1 t$-e, initiali=ers %ro the list ust :e vector initiali=ers and are a--lied to
the coluns o% the atri1, in order, starting with colun '. ,he nu:er o% initiali=ers ust atch the
nu:er o% coluns.
I% the aggregate has a structure t$-e, initiali=ers %ro the list are a--lied to the e:ers o% the structure,
in the order declared in the structure, starting with the %irst e:er. ,he nu:er o% initiali=ers ust
atch the nu:er o% e:ers.
A--l$ing these rules, the %ollowing atri1 declarations are e<uivalent:
mat<x< a 3 mat<( vec<( 05?7 ?5? )7 vec<( ?5?7 05? ) );
mat<x< b 3 4 vec<( 05?7 ?5? )7 vec<( ?5?7 05? ) 8;
mat<x< c 3 4 4 05?7 ?5? 87 4 ?5?7 05? 8 8;
All o% the %ollowing declarations are illegal.
float aI<J 3 4 >5;7 ;5<7 E5? 8; // illegal
vec< b 3 4 05?7 <5?7 >5? 8; // illegal
mat>x> c 3 4 vec>(?5?)7 vec>(05?)7 vec>(<5?)7 vec>(>5?) 8; // illegal
mat<x< d 3 4 05?7 ?5?7 ?5?7 05? 8; // illegal7 can't flatten nesting
struct 4
float a;
int b;
8 e 3 4 05<7 <7 > 8; // illegal
In all cases, the innerost initiali=er Bi.e., not a list o% initiali=ers enclosed in curl$ :racesC a--lied to an
o:Lect ust have the sae t$-e as the o:Lect :eing initiali=ed or :e a t$-e that can :e converted to the
o:LectDs t$-e according to section &.1.1' XI-licit "onversionsX. In the latter case, an i-licit conversion
will :e done on the initiali=er :e%ore the assignent is done.
struct 4
float a;
int b;
8 e 3 4 05<7 < 8; // legal7 all t/es match
33
4 #aria$%e and &'pe
struct 4
float a;
int b;
8 e 3 4 07 > 8; // legal7 first initiali.er is converted
All o% the %ollowing declarations are illegal.
int a 3 true; // illegal
vec; bI<J 3 4 vec;(?5?)7 05? 8; // illegal
mat;x< c 3 4 vec>(?5?)7 vec>(05?) 8; // illegal
struct )0 4
vec; a;
vec; b;
8;
struct 4
float s;
float t;
8 dIJ 3 4 )0(vec;(?5?)7 vec;(050)) 8; // illegal
I% an initiali=er Bo% either %orC is -rovided %or an unsi=ed arra$, the si=e o% the arra$ is deterined :$ the
nu:er o% to-9level Bnon9nestedC initiali=ers within the initiali=er. All o% the %ollowing declarations create
arra$s e1-licitl$ si=ed with %ive eleents:
float aIJ 3 floatIJ(>5;7 ;5<7 E5?7 E5<7 050);
float bIJ 3 4 >5;7 ;5<7 E5?7 E5<7 050 8;
float cIJ 3 a; // c is exlicitl/ si.e E
float dIEJ 3 b; // means the same thing
It is an error to have too %ew or too an$ initiali=ers in an initiali=er list %or the aggregate :eing
initiali=ed. ,hat is, all eleents o% an arra$, all e:ers o% a structure, all coluns o% a atri1, and all
co-onents o% a vector ust have e1actl$ one initiali=er e1-ression -resent, with no unconsued
initiali=ers.
.2 Scoping
,he sco-e o% a varia:le is deterined :$ where it is declared. I% it is declared outside all %unction
de%initions, it has glo:al sco-e, which starts %ro where it is declared and -ersists to the end o% the shader
it is declared in. I% it is declared in a while test or a for stateent, then it is sco-ed to the end o% the
%ollowing su:9stateent. I% it is declared in an if or else stateent, it is sco-ed to the end o% that
stateent. B#ee section ..! G#electionI and section ..( GIterationI %or the location o% stateents and su:9
stateents.C )therwise, i% it is declared as a stateent within a co-ound stateent, it is sco-ed to the
end o% that co-ound stateent. I% it is declared as a -araeter in a %unction de%inition, it is sco-ed until
the end o% that %unction de%inition. A %unction :od$ has a sco-e nested inside the %unctionMs de%inition.
,he if stateentMs e1-ression does not allow new varia:les to :e declared, hence does not %or a new
sco-e.
34
4 #aria$%e and &'pe
@ithin a declaration, the sco-e o% a nae starts iediatel$ a%ter the initiali=er i% -resent or iediatel$
a%ter the nae :eing declared i% not. #everal e1a-les:
int x 3 0;
4
int x 3 <7 / 3 x; // / is initiali.ed to <
8
struct )
4
int x;
8;
4
) ) 3 )(?); // ')' is onl/ visible as a struct and constructor
); // ')' is now visible as a variable
8
int x 3 x; // %rror if x has not been reviousl/ defined5
All varia:le naes, structure t$-e naes, and %unction naes in a given sco-e share the sae nae s-ace.
4unction naes can :e redeclared in the sae sco-e, with the sae or di%%erent -araeters, without error.
An i-licitl$ si=ed arra$ can :e re9declared in the sae sco-e as an arra$ o% the sae :ase t$-e.
)therwise, within one co-ilation unit, a declared nae cannot :e redeclared in the sae sco-eN doing so
results in a redeclaration error. I% a nested sco-e redeclares a nae used in an outer sco-e, it hides all
e1isting uses o% that nae. ,here is no wa$ to access the hidden nae or ake it unhidden, without
e1iting the sco-e that hid it.
,he :uilt9in %unctions are sco-ed in a sco-e outside the glo:al sco-e users declare glo:al varia:les in.
,hat is, a shaderDs glo:al sco-e, availa:le %or user9de%ined %unctions and glo:al varia:les, is nested inside
the sco-e containing the :uilt9in %unctions. @hen a %unction nae is redeclared in a nested sco-e, it hides
all %unctions declared with that nae in the outer sco-e. 4unction declarations B-rotot$-esC cannot occur
inside o% %unctionsN the$ ust :e at glo:al sco-e, or %or the :uilt9in %unctions, outside the glo:al sco-e.
#hared glo:als are glo:al varia:les declared with the sae nae in inde-endentl$ co-iled units
BshadersC within the sae language Be.g., verte1C that are linked together when aking a single -rogra.
B/lo:als %oring the inter%ace :etween two di%%erent shader languages are discussed in other sections.C
#hared glo:als share the sae nae s-ace, and ust :e declared with the sae t$-e. ,he$ will share the
sae storage. #hared glo:al arra$s ust have the sae :ase t$-e and the sae e1-licit si=e. An arra$
i-licitl$ si=ed in one shader can :e e1-licitl$ si=ed :$ another shader. I% no shader has an e1-licit si=e
%or the arra$, the largest i-licit si=e is used. #calars ust have e1actl$ the sae t$-e nae and t$-e
de%inition. #tructures ust have the sae nae, se<uence o% t$-e naes, and t$-e de%initions, and
e:er naes to :e considered the sae t$-e. ,his rule a--lies recursivel$ %or nested or e:edded
t$-es. I% a shared glo:al has ulti-le initiali=ers, the initiali=ers ust all :e constant e1-ressions, and
the$ ust all have the sae value. )therwise, a link error will result. BA shared glo:al having onl$ one
initiali=er does not re<uire that initiali=er to :e a constant e1-ression.C
35
4 #aria$%e and &'pe
." Storage :ualifiers
Varia:le declarations a$ have at ost one storage <uali%ier s-eci%ied in %ront o% the t$-e. ,hese are
suari=ed as
Storage :ualifier 7eaning
R none: de%ault S local readHwrite eor$, or an in-ut -araeter to a %unction
const a varia:le whose value cannot :e changed
in linkage into a shader %ro a -revious stage, varia:le is co-ied in
out linkage out o% a shader to a su:se<uent stage, varia:le is co-ied out
attribute co-ati:ilit$ -ro%ile onl$ and verte1 language onl$N sae as in when in a
verte1 shader
uniform value does not change across the -riitive :eing -rocessed, uni%ors
%or the linkage :etween a shader, )-en/0, and the a--lication
varying co-ati:ilit$ -ro%ile onl$ and verte1 and %ragent languages onl$N sae
as out when in a verte1 shader and sae as in when in a %ragent shader
#oe in-ut and out-ut <uali%ied varia:les can :e <uali%ied with at ost one additional au1iliar$ storage
<uali%ier:
.u*iliary Storage
:ualifier
7eaning
centroid centroid9:ased inter-olation
samle -er9sa-le inter-olation
atch -er9tessellation9-atch attri:utes
8ot all co:inations o% <uali%ication are allowed. @hich varia:le t$-es can have which <uali%iers are
s-eci%icall$ de%ined in u-coing sections.
36
4 #aria$%e and &'pe
0ocal varia:les can onl$ use the const storage <uali%ier Bor use no storage <uali%ierC.
4unction -araeters can use const, in, and out <uali%iers, :ut as "arameter 1ualiiers. 2araeter
<uali%iers are discussed in section ..1.1 G4unction "alling "onventionsI.
4unction return t$-es and structure e:ers do not use storage <uali%iers.
Initiali=ers in glo:al declarations a$ onl$ :e used in declarations o% glo:al varia:les with no storage
<uali%ier, with a const <uali%ier or with a uniform <uali%ier. /lo:al varia:les without storage <uali%iers
that are not initiali=ed in their declaration or :$ the a--lication will not :e initiali=ed :$ )-en/0, :ut
rather will enter main34 with unde%ined values.
@hen co-aring an out-ut %ro one shader stage to an in-ut o% a su:se<uent shader stage, the in-ut and
out-ut donDt atch i% their au1iliar$ <uali%iers Bor lack thereo%C are not the sae.
.".1 )efault Storage :ualifier
I% no <uali%ier is -resent on a glo:al varia:le, then the varia:le has no linkage to the a--lication or shaders
running on other -i-eline stages. 4or either glo:al or local un<uali%ied varia:les, the declaration will
a--ear to allocate eor$ associated with the -rocessor it targets. ,his varia:le will -rovide readHwrite
access to this allocated eor$.
.".2 Constant :ualifier
8aed co-ile9tie constants or read9onl$ varia:les can :e declared using the const <uali%ier. ,he const
<uali%ier can :e used with an$ o% the non9void trans-arent :asic data t$-es, as well as with structures and
arra$s o% these. It is an error to write to a const varia:le outside o% its declaration, so the$ ust :e
initiali=ed when declared. 4or e1a-le,
const vec> .Dxis 3 vec> (?5?7 ?5?7 05?);
const float ceiling 3 a K b; // a and b not necessaril/ 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 glo:al sco-e ust :e constant e1-ressions, as de%ined in section &.(.(
G"onstant E1-ressions.I
."." Constant %*pressions
A constant e#"ression is one o%
a literal value Be.g., Z or trueC
a varia:le declared with the const <uali%ier and an initiali=er, where the initiali=er is a constant
e1-ression
an e1-ression %ored :$ an o-erator on o-erands that are all constant e1-ressions, including getting an
eleent o% a constant arra$, or a e:er o% a constant structure, or co-onents o% a constant vector.
37
4 #aria$%e and &'pe
*owever, the lowest -recedence o-erators o% the se<uence o-erator B - C and the assignent o-erators
B @, 0@, ...9 are not included in the o-erators that can create a constant e1-ression.
valid use o% the lengthBC ethod on a si=ed o:Lect, whether or not the o:Lect itsel% is constant
a constructor whose arguents are all constant e1-ressions
a :uilt9in %unction call whose arguents are all constant e1-ressions, with the e1ce-tion o% the te1ture
looku- %unctions and the noise %unctions. ,he :uilt9in %unctions d&d#, d&dy, and fwidth ust return
' when evaluated inside an initiali=er with an arguent that is a constant e1-ression.
4unction calls to user9de%ined %unctions Bnon9:uilt9in %unctionsC cannot :e used to %or constant
e1-ressions.
An integral constant e#"ression is a constant e1-ression that evaluates to a scalar signed or unsigned
integer.
"onstant e1-ressions will :e evaluated in an invariant wa$ so as to create the sae value in ulti-le
shaders when the sae constant e1-ressions a--ear in those shaders. #ee section &.5.1 G,he Invariant
>uali%ierI %or ore details on how to create invariant e1-ressions.
.". Input Variables
#hader in-ut varia:les are declared with the storage <uali%ier in. ,he$ %or the in-ut inter%ace :etween
-revious stages o% the )-en/0 -i-eline and the declaring shader. In-ut varia:les ust :e declared at
glo:al sco-e. Values %ro the -revious -i-eline stage are co-ied into in-ut varia:les at the :eginning o%
shader e1ecution. Varia:les declared as in-uts cannot :e written to during shader e1ecution.
)nl$ the in-ut varia:les that are staticall$ read need to :e written :$ the -revious stageN it is allowed to
have su-er%luous declarations o% in-ut varia:les. ,his is shown in the %ollowing ta:le.
,reatent o% ;isatched In-ut
Varia:les
"onsuing #hader Bin-ut varia:lesC
8o Declaration Declared :ut no
#tatic 7se
Declared and
#tatic 7se
/enerating
#hader
Bout-ut
varia:lesC
8o Declaration Allowed Allowed Error
Declared :ut no
#tatic 7se
Allowed Allowed
Allowed
Bvalues are unde%inedC
Declared and
#tatic 7se
Allowed Allowed
Allowed
Bvalues are -otentiall$
unde%inedC
Errors are :ased on static use onl$. "o-ilation a$ generate a warning, :ut not an error, %or an$
d$naic use the co-iler can deduce that ight cause consu-tion o% unde%ined values.
#ee section 3 GBuilt9in Varia:lesI %or a list o% the :uilt9in in-ut naes.
Verte1 shader in-ut varia:les Bor attri:utesC receive -er9verte1 data. ,he$ are declared in a verte1 shader
with the in <uali%ier. It is an error to use an$ au1iliar$ or inter-olation <uali%iers on a verte1 shader in-ut.
,he values co-ied in are esta:lished :$ the )-en/0 A2I or through the use o% the la$out identi%ier
38
4 #aria$%e and &'pe
location. Verte1 shader in-uts can :e single9 or dou:le9-recision %loating9-oint scalars, vectors, and
atrices, or signed9 and unsigned9integer scalars and vectors. Verte1 shader in-uts can also %or arra$s
o% these t$-es, :ut not structures.
E1a-le declarations in a verte1 shader:
in vec; osition;
in vec> normal;
in vec< texLoordI;J;
It is e1-ected that gra-hics hardware will have a sall nu:er o% %i1ed vector locations %or -assing verte1
in-uts. ,here%ore, the )-en/0 #hading language de%ines each non9atri1 in-ut varia:le as taking u- one
such vector location. ,here is an i-leentation de-endent liit on the nu:er o% locations that can :e
used, and i% this is e1ceeded it will cause a link error. BDeclared in-ut varia:les that are not staticall$ used
do not count against this liit.C A scalar in-ut counts the sae aount against this liit as a vecI, so
a--lications a$ want to consider -acking grou-s o% %our unrelated %loat in-uts together into a vector to
:etter utili=e the ca-a:ilities o% the underl$ing hardware. A atri1 in-ut will use u- ulti-le locations.
,he nu:er o% locations used will e<ual the nu:er o% coluns in the atri1.
,essellation control, evaluation, and geoetr$ shader in-ut varia:les get the -er9verte1 values written out
:$ out-ut varia:les o% the sae naes in the -revious active shader stage. 4or these in-uts, centroid and
inter-olation <uali%iers are allowed, :ut have no e%%ect. #ince tessellation control, tessellation evaluation,
and geoetr$ shaders o-erate on a set o% vertices, each in-ut varia:le Bor in-ut :lock, see inter%ace :locks
:elowC needs to :e declared as an arra$. 4or e1a-le,
in float fooIJ; // geometr/ shader inut for vertex out float foo
Each eleent o% such an arra$ corres-onds to one verte1 o% the -riitive :eing -rocessed. Each arra$ can
o-tionall$ have a si=e declared. ,he arra$ si=e will :e set :$, Bor i% -rovided ust :e consistent withC the
in-ut layout declarationBsC esta:lishing the t$-e o% in-ut -riitive, as descri:ed later in section &.&.1
GIn-ut 0a$out >uali%iersI.
#oe in-uts and out-uts are arra*ed, eaning that %or an inter%ace :etween two shader stages either the
in-ut or out-ut declaration re<uires an e1tra level o% arra$ inde1ing %or the declarations to atch. 4or
e1a-le, with the inter%ace :etween a verte1 shader and a geoetr$ shader, verte1 shader out-ut varia:les
and geoetr$ shader in-ut varia:les o% the sae nae ust atch in t$-e and <uali%ication, e1ce-t that
the verte1 shader nae cannot :e declared as an arra$ while the geoetr$ shader nae ust :e declared
as an arra$, to allow %or verte1 inde1ing. It is a link error i% a non9arra$ed in-ut is not declared with the
sae t$-e, <uali%ication, and arra$ diensionalit$ as the atching out-ut. It is an error i% an arra$ed in-ut
is not declared as an arra$ o% the sae t$-e and <uali%ication as the corres-onding Bnon9arra$C out-ut.
#$etricall$, it is an error i% an arra$ed out-ut is not declared as an arra$ o% the sae t$-e and
<uali%ication as the corres-onding Bnon9arra$C in-ut.
I% the out-ut corres-onding to an arra$ed in-ut is itsel% an arra$, it ust a--ear in an out-ut :lock Bsee
inter%ace :locks :elowC in the out-utting shader and in an in-ut :lock in the in-utting shader with a :lock
instance nae declared as an arra$. ,his is re<uired :ecause two9diensional arra$s are not su--orted.
Additionall$, tessellation evaluation shaders su--ort -er9-atch in-ut varia:les declared with the atch and
in <uali%iers. 2er9-atch in-ut varia:les are %illed with the values o% -er9-atch out-ut varia:les written :$
the tessellation control shader. 2er9-atch in-uts a$ :e declared as one9diensional arra$s, :ut are not
inde1ed :$ verte1 nu:er. A--l$ing the atch <uali%ier to in-uts can onl$ :e done in tessellation
3!
4 #aria$%e and &'pe
evaluation shaders. As with other in-ut varia:les, -er9-atch in-uts ust :e declared using the sae t$-e
and <uali%ication as -er9-atch out-uts %ro the -revious Btessellation controlC shader stage.
4ragent shader in-uts get -er9%ragent values, t$-icall$ inter-olated %ro a -revious stageDs out-uts.
,he$ are declared in %ragent shaders with the in storage <uali%ier. ,he au1iliar$ storage <uali%iers
centroid and samle can also :e a--lied, as well as the inter-olation <uali%iers flat, noersective, and
smooth. It is an error to use atch in a %ragent shader. 4ragent in-uts can onl$ :e signed and
unsigned integers and integer vectors, %loating -oint scalars, %loating9-oint vectors, atrices, or arra$s or
structures o% these. 4ragent shader in-uts that are signed or unsigned integers, integer vectors, or an$
dou:le9-recision %loating9-oint t$-e ust :e <uali%ied with the inter-olation <uali%ier flat.
4ragent in-uts are declared as in the %ollowing e1a-les:
in vec> normal;
centroid in vec< +exLoord;
invariant centroid in vec; Lolor;
noersective in float temerature;
flat in vec> m/Lolor;
noersective centroid in vec< m/+exLoord;
,he %ragent shader in-uts %or an inter%ace with the last active shader in the verte1 -rocessing -i-eline.
4or this inter%ace, the last active shader stage out-ut varia:les and %ragent shader in-ut varia:les o% the
sae nae ust atch in t$-e and <uali%ication Bother than out atching to inC.
@hen an inter%ace :etween shader stages is %ored using shaders %ro two se-arate -rogra o:Lects, it is
not -ossi:le to detect isatches :etween in-uts and out-uts when the -rogras are linked. @hen there
are isatches :etween in-uts and out-uts on such inter%aces, the values -assed across the inter%ace will
:e -artiall$ or co-letel$ unde%ined. #haders can ensure atches across such inter%aces either :$ using
in-ut and out-ut la$out <uali%iers Bsections &.&.1 GIn-ut 0a$out >uali%iersI and &.&.! G)ut-ut 0a$out
>uali%iersIC or :$ using identical in-ut and out-ut declarations o% :locks or varia:les. "o-lete rules %or
inter%ace atching :etween -rogras are %ound in the X#hader Inter%ace ;atchingX -ortion o% section
!.1&.22) o% the )-en/0 /ra-hics #$ste #-eci%ication.
.".' 4niform
,he uniform <uali%ier is used to declare glo:al varia:les whose values are the sae across the entire
-riitive :eing -rocessed. All uniform varia:les are read9onl$ and are initiali=ed e1ternall$ either at link
tie or through the A2I. ,he link tie initial value is either the value o% the varia:leDs initiali=er, i%
-resent, or ' i% no initiali=er is -resent. )-a<ue t$-es cannot have initiali=ers.
E1a-le declarations are:
uniform vec; lightHosition;
uniform vec> color 3 vec>(?5C7 ?5C7 ?5<); // value assigned at lin= time
,he uniform <uali%ier can :e used with an$ o% the :asic data t$-es, or when declaring a varia:le whose
t$-e is a structure, or an arra$ o% an$ o% these.
,here is an i-leentation de-endent liit on the aount o% storage %or uni%ors that can :e used %or
each t$-e o% shader and i% this is e1ceeded it will cause a co-ile9tie or link9tie error. 7ni%or
varia:les that are declared :ut not used do not count against this liit. ,he nu:er o% user9de%ined
4"
4 #aria$%e and &'pe
uni%or varia:les and the nu:er o% :uilt9in uni%or varia:les that are used within a shader are added
together to deterine whether availa:le uni%or storage has :een e1ceeded.
I% ulti-le shaders are linked together, then the$ will share a single glo:al uni%or nae s-ace, including
within a language as well as across languages. *ence, the t$-es and initiali=ers o% uni%or varia:les with
the sae nae ust atch across all shaders that are linked into a single -rogra.
It is legal %or soe shaders to -rovide an initiali=er %or a -articular uni%or varia:le, while another shader
does not, :ut all -rovided initiali=ers ust :e e<ual.
.".( Output Variables
#hader out-ut varia:les are declared with a storage <uali%ier using the ke$word out. ,he$ %or the out-ut
inter%ace :etween the declaring shader and the su:se<uent stages o% the )-en/0 -i-eline. )ut-ut
varia:les ust :e declared at glo:al sco-e. During shader e1ecution the$ will :ehave as noral
un<uali%ied glo:al varia:les. ,heir values are co-ied out to the su:se<uent -i-eline stage on shader e1it.
)nl$ out-ut varia:les that are read :$ the su:se<uent -i-eline stage need to :e writtenN it is allowed to
have su-er%luous declarations o% out-ut varia:les.
,here is not an inout storage <uali%ier at glo:al sco-e %or declaring a single varia:le nae as :oth in-ut
and out-ut to a shader. A varia:le also cannot :e declared with :oth the in and the out <uali%iers. )ut-ut
varia:les ust :e declared with di%%erent naes than in-ut varia:les. *owever, nesting an in-ut or out-ut
inside an inter%ace :lock with an instance nae allows the sae naes with one re%erenced through a
:lock instance nae.
Verte1, tessellation evaluation, and geoetr$ out-ut varia:les out-ut -er9verte1 data and are declared
using the out storage <uali%ier. A--l$ing atch to an out-ut can onl$ :e done in a tessellation control
shader. )ut-ut varia:les can onl$ :e %loating9-oint scalars, %loating9-oint vectors, atrices, signed or
unsigned integers or integer vectors, or arra$s or structures o% an$ these.
Individual verte1, tessellation evaluation, and geoetr$ out-uts are declared as in the %ollowing e1a-les:
out vec> normal;
centroid out vec< +exLoord;
invariant centroid out vec; Lolor;
noersective out float temerature;
flat out vec> m/Lolor;
noersective centroid out vec< m/+exLoord;
samle out vec; er)amleLolor;
,hese can also a--ear in inter%ace :locks, as descri:ed in section &.(.5 GInter%ace BlocksI. Inter%ace
:locks allow si-ler addition o% arra$s to the inter%ace %ro verte1 to geoetr$ shader. ,he$ also allow a
%ragent shader to have the sae in-ut inter%ace as a geoetr$ shader %or a given verte1 shader.
,essellation control shader out-ut varia:les are a$ :e used to out-ut -er9verte1 and -er9-atch data. 2er9
verte1 out-ut varia:les are arra$ed Bsee arra*ed under &.(.& In-utsC and declared using the out <uali%ier
without the atch <uali%ier. 2er9-atch out-ut varia:les are declared using the atch and out <uali%iers.
2er9verte1 and -er9-atch out-ut varia:les can onl$ :e %loating9-oint scalars, %loating9-oint vectors,
atrices, signed or unsigned integers or integer vectors, or arra$s or structures o% an$ these. #ince
tessellation control shaders -roduce an arra$ed -riitive co-rising ulti-le vertices, each -er9verte1
41
4 #aria$%e and &'pe
out-ut varia:le Bor out-ut :lock, see inter%ace :locks :elowC needs to :e declared as an arra$. 4or
e1a-le,
out float fooIJ; // feeds next stage inut in float fooIJ
Each eleent o% such an arra$ corres-onds to one verte1 o% the -riitive :eing -roduced. Each arra$ can
o-tionall$ have a si=e declared. ,he arra$ si=e will :e set :$ Bor i% -rovided ust :e consistent withC the
out-ut la$out declarationBsC esta:lishing the nu:er o% vertices in the out-ut -atch, as descri:ed later in
section &.&.!.1 G,essellation "ontrol )ut-utsI.
As descri:ed under the section &.(.& GIn-ut Varia:lesI a:ove, i% a -er9verte1 out-ut o% the tessellation
control shader is itsel% an arra$ with ulti-le values -er verte1, it ust a--ear in an out-ut :lock Bsee
inter%ace :locks :elowC in the tessellation control shader with a :lock instance nae declared as an arra$.
Each tessellation control shader invocation has a corres-onding out-ut -atch verte1, and a$ assign
values to -er9verte1 out-uts onl$ i% the$ :elong to that corres-onding verte1. I% a -er9verte1 out-ut
varia:le is used as an l9value, it is an error i% the e1-ression indicating the verte1 inde1 is not the identi%ier
gl_+nvocation+D.
,he order o% e1ecution o% a tessellation control shader invocation relative to the other invocations %or the
sae in-ut -atch is unde%ined unless the :uilt9in %unction barrierBC is used. ,his -rovides soe control
over relative e1ecution order. @hen a shader invocation calls barrierBC, its e1ecution -auses until all
other invocations have reached the sae -oint o% e1ecution. )ut-ut varia:le assignents -er%ored :$
an$ invocation e1ecuted -rior to calling barrierBC will :e visi:le to an$ other invocation a%ter the call to
barrierBC returns.
Because tessellation control shader invocations e1ecute in unde%ined order :etween :arriers, the values o%
-er9verte1 or -er9-atch out-ut varia:les will soeties :e unde%ined. "onsider the :eginning and end o%
shader e1ecution and each call to barrierBC as s$nchroni=ation -oints. ,he value o% an out-ut varia:le
will :e unde%ined in an$ o% the three %ollowing cases:
1. At the :eginning o% e1ecution.
!. At each s$nchroni=ation -oint, unless
the value was well9de%ined a%ter the -revious s$nchroni=ation -oint and was not written :$ an$
invocation since, or
the value was written :$ e1actl$ one shader invocation since the -revious s$nchroni=ation
-oint, or
the value was written :$ ulti-le shader invocations since the -revious s$nchroni=ation -oint,
and the last write -er%ored :$ all such invocations wrote the sae value.
(. @hen read :$ a shader invocation, i%
the value was unde%ined at the -revious s$nchroni=ation -oint and has not :een writen :$ the
sae shader invocation since, or
the out-ut varia:le is written to :$ an$ other shader invocation :etween the -revious and ne1t
s$nchroni=ation -oints, even i% that assignent occurs in code %ollowing the read.
4ragent out-uts out-ut -er9%ragent data and are declared using the out storage <uali%ier. It is an error
to use au1iliar$ storage <uali%iers or inter-olation <uali%iers on an out-ut in a %ragent shader. 4ragent
out-uts can onl$ :e float, %loating9-oint vectors, signed or unsigned integers or integer vectors, or arra$s
42
4 #aria$%e and &'pe
o% an$ these. ;atrices and structures cannot :e out-ut. 4ragent out-uts are declared as in the %ollowing
e1a-les:
out vec; &ragmentLolor;
out uint "uminosit/;
.".2 Interface -loc0s
In-ut, out-ut, and uni%or varia:le declarations can :e grou-ed into naed inter%ace :locks to -rovide
coarser granularit$ :acking than is achieva:le with individual declarations. ,he$ can have an o-tional
instance nae, used in the shader to re%erence their e:ers. An out-ut :lock o% one -rograa:le
stage is :acked :$ a corres-onding in-ut :lock in the su:se<uent -rograa:le stage. A uni%or :lock is
:acked :$ the a--lication with a :u%%er o:Lect. It is illegal to have an in-ut :lock in a verte1 shader or an
out-ut :lock in a %ragent shaderN these uses are reserved %or %uture use.
An inter%ace :lock is started :$ an in, out, or uniform ke$word, %ollowed :$ a :lock nae, %ollowed :$
an o-en curl$ :race B : C as %ollows:
interace-bloc. :
la*out-1ualiierot interace-1ualiier bloc.-name : member-list ; instance-nameot C
interace-1ualiier :
in
out
uniform
member-list :
member-declaration
member-declaration member-list
member-declaration :
la*out-1ualiierot 1ualiiersot t*"e declarators C
instance-name :
identiier
identiier 5 6
identiier 5 integral-constant-e#"ression 6
Each o% the a:ove eleents is discussed :elow, with the e1ce-tion o% la$out <uali%iers Bla*out-1ualiier4,
which are de%ined in the ne1t section.
4irst, an e1a-le,
uniform +ransform 4
mat; Godel'iewGatrix;
mat; Godel'iewHro:ectionGatrix;
uniform mat> $ormalGatrix; // allowed restatement of qualifier
float ,eformation;
8;
,he a:ove esta:lishes a uni%or :lock naed G,rans%orI with %our uni%ors grou-ed inside it.
43
4 #aria$%e and &'pe
,$-es and declarators are the sae as %or other in-ut, out-ut, and uni%or varia:le declarations outside
:locks, with these e1ce-tions:
initiali=ers are not allowed
o-a<ue t$-es are not allowed
structure de%initions cannot :e nested inside a :lock
)therwise, :uilt9in t$-es, -reviousl$ declared structures, and arra$s o% these are allowed as the t$-e o% a
declarator in the sae anner the$ are allowed outside a :lock.
I% no o-tional <uali%ier is used in a e:er9declaration, the <uali%ication o% the varia:le is Lust in, out, or
uniform as deterined :$ interace-1ualiier. I% o-tional <uali%iers are used, the$ can include
inter-olation <uali%iers, au1iliar$ storage <uali%iers, and storage <uali%iers and the$ ust declare an in-ut,
out-ut, or uni%or varia:le consistent with the inter%ace <uali%ier o% the :lock: In-ut varia:les, out-ut
varia:les, and uni%or varia:les can onl$ :e in in :locks, out :locks, and uniform :locks, res-ectivel$.
Re-eating the in, out, or uniform inter%ace <uali%ier %or a e:erDs storage <uali%ier is o-tional. 4or
e1a-le,
in Gaterial 4
smooth in vec; Lolor0; // legal7 inut inside in bloc=
smooth vec; Lolor<; // legal7 'in' inherited from 'in Gaterial'
vec< +exLoord; // legal7 +exLoord is an inut
uniform float Dtten; // illegal7 mismatched storage qualifier
8;
4or this section, de%ine an interace to :e one o% these
All the uni%ors o% a -rogra. ,his s-ans all co-ilation units linked together within one -rogra.
,he :oundar$ :etween adLacent -rograa:le -i-eline stages: ,his s-ans all the out-uts in all
co-ilation units o% the %irst stage and all the in-uts in all co-ilation units o% the second stage.
,he :lock nae Bbloc.-nameC is used to atch inter%aces: an out-ut :lock o% one -i-eline stage will :e
atched to an in-ut :lock with the sae nae in the su:se<uent -i-eline stage. 4or uni%or :locks, the
a--lication uses the :lock nae to identi%$ the :lock. Block naes have no other use within a shader
:e$ond inter%ace atchingN it is an error to use a :lock nae at glo:al sco-e %or an$thing other than as a
:lock nae Be.g., use o% a :lock nae %or a glo:al varia:le nae or %unction nae is currentl$ reservedC.
;atched :lock naes within an inter%ace Bas de%ined a:oveC ust atch in ters o% having the sae
nu:er o% declarations with the sae se<uence o% t$-es and the sae se<uence o% e:er naes, as well
as having the sae e:er9wise la$out <uali%ication Bsee ne1t sectionC. 4urtherore, i% a atching :lock
is declared as an arra$, then the arra$ si=es ust also atch Bor %ollow arra$ atching rules %or the
inter%ace :etween a verte1 and a geoetr$ shaderC. An$ isatch will generate a link error. A :lock
nae is allowed to have di%%erent de%initions in di%%erent inter%aces within the sae shader, allowing, %or
e1a-le, an in-ut :lock and out-ut :lock to have the sae nae.
44
4 #aria$%e and &'pe
I% an instance nae Binstance-nameC is not used, the naes declared inside the :lock are sco-ed at the
glo:al level and accessed as i% the$ were declared outside the :lock. I% an instance nae Binstance-nameC
is used, then it -uts all the e:ers inside a sco-e within its own nae s-ace, accessed with the %ield
selector B . C o-erator Banalogousl$ to structuresC. 4or e1a-le,
in "ight 4
vec; "ightHos;
vec> "ightLolor;
8;
in Lolored+exture 4
vec; Lolor;
vec< +exLoord;
8 Gaterial; // instance name
vec> Lolor; // different Lolor than Gaterial5Lolor
vec; "ightHos; // illegal7 alread/ defined
555
555 3 "ightHos; // accessing "ightHos
555 3 Gaterial5Lolor; // accessing Lolor in Lolored+exture bloc=
)utside the shading language Bi.e., in the A2IC, e:ers are siilarl$ identi%ied e1ce-t the :lock nae is
alwa$s used in -lace o% the instance nae BA2I accesses are to inter%aces, not to shadersC. I% there is no
instance nae, then the A2I does not use the :lock nae to access a e:er, Lust the e:er nae.
out 'ertex 4
vec; Hosition; // DH# transform/feedbac= will use 'ertex5Hosition
vec< +exture;
8 Loords; // shader will use Loords5Hosition
out 'ertex< 4
vec; Lolor; // DH# will use Lolor
8;
4or :locks declared as arra$s, the arra$ inde1 ust also :e included when accessing e:ers, as in this
e1a-le
uniform +ransform 4 // DH# uses +ransformI<J to refer to instance <
mat; Godel'iewGatrix;
mat; Godel'iewHro:ectionGatrix;
float ,eformation;
8 transformsI;J;
555
555 3 transformsI<J5Godel'iewGatrix; // shader access of instance <
// DH# uses +ransform5Godel'iewGatrix to quer/ an offset or other quer/
4or uni%or :locks declared as an arra$, each individual arra$ eleent corres-onds to a se-arate :u%%er9
o:Lect :ind range, :acking one instance o% the :lock. As the arra$ si=e indicates the nu:er o% :u%%er
o:Lects needed, uni%or :lock arra$ declarations ust s-eci%$ an arra$ si=e. A uni%or :lock arra$ can
onl$ :e inde1ed with a d$naicall$ uni%or integral e1-ression, otherwise results are unde%ined.
@hen using )-en/0 A2I entr$ -oints to identi%$ the nae o% an individual :lock in an arra$ o% :locks,
the nae string ust include an arra$ inde1 Be.g., $ransorm526C. @hen using )-en/0 A2I entr$ -oints
45
4 #aria$%e and &'pe
to re%er to o%%sets or other characteristics o% a :lock e:er, an arra$ inde1 ust not :e s-eci%ied Be.g.,
$ransorm.7odelVie/7atri#C.
/eoetr$ shader in-ut :locks ust :e declared as arra$s and %ollow the arra$ declaration and linking
rules %or all geoetr$ shader in-uts. All other in-ut and out-ut :lock arra$s ust s-eci%$ an arra$ si=e.
,here is an i-leentation de-endent liit on the nu:er o% uni%or :locks that can :e used -er stage.
I% this liit is e1ceeded, it will cause a link error.
. Layout :ualifiers
0a$out <uali%iers can a--ear in several %ors o% declaration. ,he$ can a--ear as -art o% an inter%ace
:lock de%inition or :lock e:er, as shown in the graar in the -revious section. ,he$ can also a--ear
with Lust an inter%ace <uali%ier Ba storage <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-1ualiier interace-1ualiier C
)r, the$ can a--ear with an individual varia:le declared with an inter%ace <uali%ier:
la*out-1ualiier interace-1ualiier declaration C
Declarations o% la$outs can onl$ :e ade at glo:al sco-e, and onl$ where indicated in the %ollowing
su:sectionsN their details are s-eci%ic to what the inter%ace <uali%ier is, and are discussed individuall$.
,he la*out-1ualiier e1-ands to
la*out-1ualiier :
layout 8 la*out-1ualiier-id-list 9
la*out-1ualiier-id-list :
la*out-1ualiier-id
la*out-1ualiier-id - la*out-1ualiier-id-list
la*out-1ualiier-id
la*out-1ualiier-name
la*out-1ualiier-name T la*out-1ualiier-value
,he tokens used %or la*out-1ualiier-name are identi%iers, not ke$words. /enerall$, the$ can :e listed in
an$ order. )rder9de-endent eanings e1ist onl$ i% e1-licitl$ called out :elow. #iilarl$, these identi%iers
are not case sensitive, unless e1-licitl$ noted otherwise.
;ore than one la$out <uali%ier a$ a--ear in a single declaration. I% the sae la*out-1ualiier-name
occurs in ulti-le la$out <uali%iers %or the sae declaration, the last one overrides the %orer ones.
..1 Input Layout :ualifiers
#oe in-ut la$out <uali%iers a--l$ to all shader languages and soe a--l$ onl$ to s-eci%ic languages.
,he latter are discussed in se-arate sections :elow.
All shaders allow in-ut la$out <uali%iers on in-ut varia:le declarations. ,he location la$out <uali%ier
identi%ier %or in-uts is:
46
4 #aria$%e and &'pe
la*out-1ualiier-id
location @ integer-constant
)nl$ one arguent is acce-ted. 4or e1a-le,
la/out(location 3 >) in vec; normal;
will esta:lish that the shader in-ut normal is assigned to vector location nu:er (. 4or verte1 shader
in-uts, the location s-eci%ies the nu:er o% the generic verte1 attri:ute %ro which in-ut values are taken.
4or in-uts o% all other shader t$-es, the location s-eci%ies a vector nu:er that can :e used to atch
against out-uts %ro a -revious shader stage, even i% that shader is in a di%%erent -rogra o:Lect.
I% a verte1 shader in-ut is an$ scalar or vector t$-e, it will consue a single location. I% a non9verte1
shader in-ut is a scalar or vector t$-e other than dvec' or dvecI, it will consue a single location, while
t$-es dvec' or dvecI will consue two consecutive locations. In-uts o% t$-e double and dvec" will
consue onl$ a single location, in all stages.
I% the declared in-ut is an arra$ o% si=e n and each eleent takes m locations, it will :e assigned m K n
consecutive locations starting with the location s-eci%ied. 4or e1a-le,
la/out(location 3 B) in vec; colorsI>J;
will esta:lish that the shader in-ut colors is assigned to vector location nu:ers ., 3, and 5.
I% the declared in-ut is an n 1 m single9 or dou:le9-recision atri1, it will :e assigned ulti-le locations
starting with the location s-eci%ied. ,he nu:er o% locations assigned %or each atri1 will :e the sae as
%or an n9eleent arra$ o% m9co-onent vectors. 4or e1a-le,
la/out(location 3 A) in mat; transformsI<J;
will esta:lish that shader in-ut transorms is assigned to vector locations 691., with transorms506 :eing
assigned to locations 691! and transorms516 :eing assigned to locations 1(91..
I% the declared in-ut is a structure, its e:ers will :e assigned consecutive locations in the order o%
declaration, with the %irst e:er assigned the location s-eci%ied %or the structure. ,he nu:er o%
locations consued :$ a structure e:er is deterined :$ a--l$ing the rules a:ove recursivel$ as
though the structure e:er were declared as an in-ut varia:le o% the sae t$-e. 4or e1a-le,
la/out(location 3 >) struct ) 4
vec> a;
mat< b;
vec; cI<J;
8 s;
will assign location ( to s.a, locations & and + to the two colun vectors o% s.b, and locations . and 3 to
s.c.
0ocation la$out <uali%iers a$ :e used on in-ut varia:les declared as structures, :ut not on individual
e:ers. 0ocation la$out <uali%iers a$ not :e used on in-ut :locks or in-ut :lock e:ers.
47
4 #aria$%e and &'pe
,he nu:er o% in-ut locations availa:le to a shader is liited. 4or verte1 shaders, the liit is the
advertised nu:er o% verte1 attri:utes. 4or all other shaders, the liit is i-leentation9de-endent and
ust :e no less than one %ourth o% the advertised a1iu in-ut co-onent count. A -rogra will %ail to
link i% an$ attached shader uses a location greater than or e<ual to the nu:er o% su--orted locations,
unless device9de-endent o-tii=ations are a:le to ake the -rogra %it within availa:le hardware
resources.
A -rogra will %ail to link i% an$ two non9verte1 shader in-ut varia:les are assigned to the sae location.
4or verte1 shaders, ulti-le in-ut varia:les a$ :e assigned to the sae location using either la$out
<uali%iers or via the )-en/0 A2I. *owever, such aliasing is intended onl$ to su--ort verte1 shaders
where each e1ecution -ath accesses at ost one in-ut -er each location. I-leentations are -eritted,
:ut not re<uired, to generate link errors i% the$ detect that ever$ -ath through the verte1 shader e1ecuta:le
accesses ulti-le in-uts assigned to an$ single location. 4or all shader t$-es, a -rogra will %ail to link i%
e1-licit location assignents leave the linker una:le to %ind s-ace %or other varia:les without e1-licit
assignents.
4or the -ur-oses o% deterining i% a non9verte1 in-ut atches an out-ut %ro a -revious shader stage, the
location la$out <uali%ier Bi% an$C ust atch.
I% a verte1 shader in-ut varia:le with no location assigned in the shader te1t has a location s-eci%ied
through the )-en/0 A2I, the A2I9assigned location will :e used. )therwise, such varia:les will :e
assigned a location :$ the linker. #ee section !.11.( GVerte1 Attri:utesI o% the )-en/0 /ra-hics #$ste
#-eci%ication %or ore details. A link error will occur i% an in-ut varia:le is declared in ulti-le shaders
o% the sae language with con%licting locations.
..1.1 Tessellation %#aluation Inputs
Additional in-ut la$out <uali%ier identi%iers allowed %or tessellation evaluation shaders are:
la*out-1ualiier-id
triangles
Nuads
isolines
eNual_sacing
fractional_even_sacing
fractional_odd_sacing
cw
ccw
oint_mode
)ne su:set o% these identi%iers, "rimitive mode, is used to s-eci%$ a tessellation -riitive ode to :e used
:$ the tessellation -riitive generator. ,o s-eci%$ a -riitive ode, the identi%ier ust :e one o%
triangles, Nuads, or isolines, which s-eci%$ that the tessellation -riitive generator should su:divide a
triangle into saller triangles, a <uad into triangles, or a <uad into a collection o% lines, res-ectivel$.
A second su:set o% these identi%iers, verte# s"acing, is used to s-eci%$ the s-acing used :$ the tessellation
-riitive generator when su:dividing an edge. ,o s-eci%$ verte1 s-acing, the identi%ier ust :e one o%
the %ollowing.
eNual_sacing signi%$ing that edges should :e divided into a collection o% e<ual9si=ed segents.
48
4 #aria$%e and &'pe
fractional_even_sacing signi%$ing that edges should :e divided into an even nu:er o% e<ual9
length segents -lus two additional shorter X%ractionalX segents.
fractional_odd_sacing signi%$ing that edges should :e divided into an odd nu:er o% e<ual9
length segents -lus two additional shorter X%ractionalX segents.
A third su:set o% these identi%iers, ordering, s-eci%ies whether the tessellation -riitive generator
-roduces triangles in clockwise or counter9clockwise order, according to the coordinate s$ste de-icted
in the )-en/0 s-eci%ication. ,he ordering identi%iers cw and ccw indicate clockwise and counter9
clockwise triangles, res-ectivel$. I% the tessellation -riitive generator does not -roduce triangles,
ordering is ignored.
4inall$, "oint mode) is s-eci%ied with the identi%ier oint_mode indicating the tessellation -riitive
generator should -roduce a -oint %or each uni<ue verte1 in the su:divided -riitive, rather than
generating lines or triangles.
An$ or all o% these identi%iers a$ :e s-eci%ied one or ore ties in a single in-ut la$out declaration. I%
-riitive ode, verte1 s-acing, or ordering is declared ore than once in the tessellation evaluation
shaders o% a -rogra, all such declarations ust use the sae identi%ier.
At least one tessellation evaluation shader Bco-ilation unitC in a -rogra ust declare a -riitive ode
in its in-ut la$out. Declaring verte1 s-acing, ordering, or -oint ode identi%iers is o-tional. It is not
re<uired that all tessellation evaluation shaders in a -rogra declare a -riitive ode. I% s-acing or
verte1 ordering declarations are oitted, the tessellation -riitive generator will use e<ual s-acing or
counter9clockwise verte1 ordering, res-ectivel$. I% a -oint ode declaration is oitted, the tessellation
-riitive generator will -roduce lines or triangles according to the -riitive ode.
..1.2 Geometry Shader Inputs
Additional la$out <uali%ier identi%iers %or geoetr$ shader in-uts include "rimitive identi%iers and an
invocation count identi%ier:
la*out-1ualiier-id
oints
lines
lines_adMacency
triangles
triangles_adMacency
invocations @ integer-constant
,he identi%iers oints, lines, lines_adMacency, triangles, and triangles_adMacency are used to s-eci%$ the
t$-e o% in-ut "rimitive acce-ted :$ the geoetr$ shader, and onl$ one o% these is acce-ted. At least one
geoetr$ shader Bco-ilation unitC in a -rogra ust declare this in-ut -riitive la$out, and all geoetr$
shader in-ut la$out declarations in a -rogra ust declare the sae la$out. It is not re<uired that all
geoetr$ shaders in a -rogra declare an in-ut -riitive la$out.
,he identi%ier invocations is used to s-eci%$ the nu:er o% ties the geoetr$ shader e1ecuta:le is
invoked %or each in-ut -riitive received. Invocation count declarations are o-tional. I% no invocation
count is declared in an$ geoetr$ shader in a -rogra, the geoetr$ shader will :e run once %or each
in-ut -riitive. I% an invocation count is declared, all such declarations ust s-eci%$ the sae count. I% a
4!
4 #aria$%e and &'pe
shader s-eci%ies an invocation count greater than the i-leentation9de-endent a1iu, it will %ail to
co-ile.
4or e1a-le,
la/out(triangles7 invocations 3 B) in;
will esta:lish that all in-uts to the geoetr$ shader are triangles and that the geoetr$ shader e1ecuta:le
is run si1 ties %or each triangle -rocessed.
All geoetr$ shader in-ut unsi=ed arra$ declarations will :e si=ed :$ an earlier in-ut -riitive la$out
<uali%ier, when -resent, as -er the %ollowing ta:le.
Hayout %i+e of Inut Arrays
-oints 1
lines !
lines?adLacenc$ &
triangles (
triangles?adLacenc$ .
,he intrinsicall$ declared in-ut arra$ gl_in56 will also :e si=ed :$ an$ in-ut -riitive9la$out declaration.
*ence, the e1-ression
gl!in5length()
will return the value %ro the ta:le a:ove.
4or in-uts declared without an arra$ si=e, including intrinsicall$ declared in-uts Bi.e., gl_inC, a la$out ust
:e declared :e%ore an$ use o% the ethod length or other an$ arra$ use that re<uires the arra$ si=e to :e
known.
It is a co-ile9tie error i% a la$out declarationDs arra$ si=e B%ro ta:le a:oveC does not atch all the
e1-licit arra$ si=es s-eci%ied in declarations o% an in-ut varia:les in the sae shader. ,he %ollowing
includes e1a-les o% co-ile tie errors:
5"
4 #aria$%e and &'pe
// code sequence within one shader555
in vec; Lolor0IJ; // legal7 si.e still un=nown
555Lolor05length()555// illegal7 length() un=nown
in vec; Lolor<I<J; // legal7 si.e is <
555Lolor05length()555// illegal7 Lolor0 still has no si.e
in vec; Lolor>I>J; // illegal7 inut si.es are inconsistent
la/out(lines) in; // legal for Lolor<7 inut si.e is <7 matching Lolor<
in vec; Lolor;I>J; // illegal7 contradicts la/out of lines
555Lolor05length()555// legal7 length() is <7 Lolor0 si.ed b/ la/out()
la/out(lines) in; // legal7 matches other la/out() declaration
la/out(triangles) in;// illegal7 does not match earlier la/out() declaration
It is a link9tie error i% not all -rovided si=es Bsi=ed in-ut arra$s and la$out si=eC atch across all
geoetr$ shaders in a -rogra.
..1." ,ragment Shader Inputs
Additional %ragent la$out <uali%ier identi%iers include the %ollowing %or gl_FragCoord
la*out-1ualiier-id
origin_uer_left
i#el_center_integer
B$ de%ault, gl_FragCoord assues a lower9le%t origin %or window coordinates and assues -i1el centers
are located at hal%9-i1el coordinates. 4or e1a-le, the B#) *C location B'.+, '.+C is returned %or the lower9
le%t9ost -i1el in a window. ,he origin can :e changed :$ redeclaring gl_FragCoord with the
origin_uer_left identi%ier, oving the origin o% gl_FragCoord to the u--er le%t o% the window, with *
increasing in value toward the :otto o% the window. ,he values returned can also :e shi%ted :$ hal% a
-i1el in :oth # and * :$ i#el_center_integer so it a--ears the -i1els are centered at whole nu:er -i1el
o%%sets. ,his oves the B#, *C value returned :$ gl_FragCoord o% B'.+, '.+C :$ de%ault, to B'.', '.'C with
i#el_center_integer.
Redeclarations are done as %ollows
in vec; gl!&ragLoord; // redeclaration that changes nothing is allowed
// Dll the following are allowed redeclaration that change behavior
la/out(origin!uer!left) in vec; gl!&ragLoord;
la/out(ixel!center!integer) in vec; gl!&ragLoord;
la/out(origin!uer!left7 ixel!center!integer) in vec; gl!&ragLoord;
I% gl_FragCoord is redeclared in an$ %ragent shader in a -rogra, it ust :e redeclared in all the
%ragent shaders in that -rogra that have a static use gl_FragCoord. All redeclarations o%
gl_FragCoord in all %ragent shaders in a single -rogra ust have the sae set o% <uali%iers. @ithin
an$ shader, the %irst redeclarations o% gl_FragCoord ust a--ear :e%ore an$ use o% gl_FragCoord. ,he
:uilt9in gl_FragCoord is onl$ -redeclared in %ragent shaders, so redeclaring it in an$ other shader
language will :e illegal.
Redeclaring gl_FragCoord with origin_uer_left andHor i#el_center_integer <uali%iers onl$ a%%ects
gl_FragCoord.# and gl_FragCoord.*. It has no a%%ect on rasteri=ation, trans%oration, or an$ other -art
o% the )-en/0 -i-eline or language %eatures.
51
4 #aria$%e and &'pe
4ragent shaders also allow the %ollowing la$out <uali%ier on in onl$ Bnot with varia:le declarationsC
la*out-1ualiier-id
early_fragment_tests
to re<uest that %ragent tests :e -er%ored :e%ore %ragent shader e1ecution, as descri:ed in section (.1'
o% the )-en/0 #-eci%ication.
4or e1a-le,
la/out(earl/!fragment!tests) in;
#-eci%$ing this will ake -er9%ragent tests :e -er%ored :e%ore %ragent shader e1ecution. I% this is not
declared, -er9%ragent tests will :e -er%ored a%ter %ragent shader e1ecution.
..2 Output Layout :ualifiers
#oe out-ut la$out <uali%iers a--l$ to all shader languages and soe a--l$ onl$ to s-eci%ic languages.
,he latter are discussed in se-arate sections :elow.
All shaders allow location out-ut la$out <uali%iers on out-ut varia:le declarations. ,he location la$out
<uali%ier identi%ier %or out-uts is:
la*out-1ualiier-id
location @ integer-constant
4ragent shaders allow an additional inde# out-ut la$out <uali%iers:
la*out-1ualiier-id
location @ integer-constant
inde# @ integer-constant
Each o% these <uali%iers a$ a--ear at ost once. I% inde# is s-eci%ied, location ust also :e s-eci%ied.
I% inde# is not s-eci%ied, the value ' is used. 4or e1a-le, in a %ragent shader,
la/out(location 3 >) out vec; color;
will esta:lish that the %ragent shader out-ut color is assigned to %ragent color ( as the %irst Binde1 =eroC
in-ut to the :lend e<uation. And,
la/out(location 3 >7 index 3 0) out vec; factor;
will esta:lish that the %ragent shader out-ut actor is assigned to %ragent color ( as the second Binde1
oneC in-ut to the :lend e<uation.
4or %ragent9shader out-uts, the location and inde1 s-eci%$ the color out-ut nu:er and inde1 receiving
the values o% the out-ut. 4or out-uts o% all other shader stages, the location s-eci%ies a vector nu:er that
can :e used to atch against in-uts in a su:se<uent shader stage, even i% that shader is in a di%%erent
-rogra o:Lect.
I% a declared out-ut is a scalar or vector t$-e other than dvec' or dvecI, it will consue a single location.
)ut-uts o% t$-e dvec' or dvecI will consue two consecutive locations. )ut-uts o% t$-e double and
dvec" will consue onl$ a single location, in all stages.
52
4 #aria$%e and &'pe
I% the declared out-ut is an arra$, it will :e assigned consecutive locations starting with the location
s-eci%ied. 4or e1a-le,
la/out(location 3 <) out vec; colorsI>J;
will esta:lish that colors is assigned to vector location nu:ers !, (, and &.
I% the declared out-ut is an n 1 m single9 or dou:le9-recision atri1, it will :e assigned ulti-le locations
starting with the location s-eci%ied. ,he nu:er o% locations assigned will :e the sae as %or an n9
eleent arra$ o% m9co-onent vectors.
I% the declared out-ut is a structure, its e:ers will :e assigned consecutive locations in the order o%
declaration, with the %irst e:er assigned the location s-eci%ied %or the structure. ,he nu:er o%
locations consued :$ a structure e:er is deterined :$ a--l$ing the rules a:ove recursivel$ as
though the structure e:er were declared as an out-ut varia:le o% the sae t$-e.
0ocation la$out <uali%iers a$ :e used on out-ut varia:les declared as structures, :ut not on individual
e:ers. 0ocation la$out <uali%iers a$ not :e used on out-ut :locks or out-ut :lock e:ers.
,he nu:er o% out-ut locations availa:le to a shader is liited. 4or %ragent shaders, the liit is the
advertised nu:er o% draw :u%%ers. 4or all other shaders, the liit is i-leentation9de-endent and ust
:e no less than one %ourth o% the advertised a1iu out-ut co-onent count. A -rogra will %ail to
link i% an$ attached shader uses a location greater than or e<ual to the nu:er o% su--orted locations,
unless device9de-endent o-tii=ations are a:le to ake the -rogra %it within availa:le hardware
resources.
A -rogra will %ail to link i% an$ two %ragent shader out-ut varia:les are assigned to the sae location
and inde1, or i% an$ two out-ut varia:les %ro the sae non9%ragent shader stage are assigned to the
sae location. 4or %ragent shader out-uts, locations can :e assigned using either a la$out <uali%ier or
via the )-en/0 A2I. 4or all shader t$-es, a -rogra will %ail to link i% e1-licit location assignents
leave the linker una:le to %ind s-ace %or other varia:les without e1-licit assignents.
I% an out-ut varia:le with no location or inde1 assigned in the shader te1t has a location s-eci%ied through
the )-en/0 A2I, the A2I9assigned location will :e used. )therwise, such varia:les will :e assigned a
location :$ the linker. All such assignents will have a color inde1 o% =ero. #ee section (.6.! G#hader
E1ecutionI o% the )-en/0 /ra-hics #$ste #-eci%ication %or ore details. A link error will occur i% an
out-ut varia:le is declared in ulti-le shaders o% the sae language with con%licting location or inde1
values.
4or the -ur-oses o% deterining i% a non9%ragent out-ut atches an in-ut %ro a su:se<uent shader
stage, the location la$out <uali%ier Bi% an$C ust atch.
..2.1 Tessellation Control Outputs
,essellation control shaders allow out-ut la$out <uali%iers onl$ on the inter%ace <uali%ier out, not on an
out-ut :lock, :lock e:er, or varia:le declaration. ,he out-ut la$out <uali%ier identi%iers allowed %or
tessellation control shaders include the verte19count la$out <uali%ier:
la*out-1ualiier-id
vertices @ integer-constant
53
4 #aria$%e and &'pe
,he identi%ier vertices s-eci%ies the nu:er o% vertices in the out-ut -atch -roduced :$ the tessellation
control shader, which also s-eci%ies the nu:er o% ties the tessellation control shader is invoked. It is an
error %or the out-ut verte1 count to :e less than or e<ual to =ero, or greater than the i-leentation9
de-endent a1iu -atch si=e.
,he intrinsicall$ declared tessellation control out-ut arra$ gl_out67 will also :e si=ed :$ an$ out-ut la$out
declaration. *ence, the e1-ression
gl!out5length()
will return the out-ut -atch verte1 count s-eci%ied in a -revious out-ut la$out <uali%ier. 4or out-uts
declared without an arra$ si=e, including intrinsicall$ declared out-uts Bi.e., gl_outC, a la$out ust :e
ust :e declared :e%ore an$ use o% the ethod lengthBC or other arra$ use re<uires its si=e :e known.
It is a co-ile9tie error i% the out-ut -atch verte1 count s-eci%ied in an out-ut la$out <uali%ier does not
atch the arra$ si=e s-eci%ied in an$ out-ut varia:le declaration in the sae shader.
All tessellation control shader la$out declarations in a -rogra ust s-eci%$ the sae out-ut -atch verte1
count. ,here ust :e at least one la$out <uali%ier s-eci%$ing an out-ut -atch verte1 count in an$ -rogra
containing tessellation control shadersN however, such a declaration is not re<uired in all tessellation
control shaders.
..2.2 Geometry Outputs
/eoetr$ shaders can have three additional t$-es o% out-ut la$out identi%iers: an out-ut "rimitive t*"e, a
a1iu out-ut verte# count, and -er9out-ut stream nu:ers. ,he -riitive t$-e and verte1 count
identi%iers are allowed onl$ on the inter%ace <uali%ier out, not on an out-ut :lock, :lock e:er, or
varia:le declaration. ,he strea identi%ier is allowed on the inter%ace <uali%ier out, on out-ut :locks, and
on varia:le declarations.
,he la$out <uali%ier identi%iers %or geoetr$ shader out-uts are
la*out-1ualiier-id
oints
line_stri
triangle_stri
ma#_vertices @ integer-constant
stream @ integer-constant
,he -riitive t$-e identi%iers oints, line_stri, and triangle_stri are used to s-eci%$ the t$-e o% out-ut
-riitive -roduced :$ the geoetr$ shader, and onl$ one o% these is acce-ted. At least one geoetr$
shader Bco-ilation unitC in a -rogra ust declare an out-ut -riitive t$-e, and all geoetr$ shader
out-ut -riitive t$-e declarations in a -rogra ust declare the sae -riitive t$-e. It is not re<uired
that all geoetr$ shaders in a -rogra declare an out-ut -riitive t$-e.
,he verte1 count identi%ier ma#_vertices is used to s-eci%$ the a1iu nu:er o% vertices the shader
will ever eit in a single invocation. At least one geoetr$ shader Bco-ilation unitC in a -rogra ust
declare a a1iu out-ut verte1 count, and all geoetr$ shader out-ut verte1 count declarations in a
-rogra ust declare the sae count. It is not re<uired that all geoetr$ shaders in a -rogra declare a
count.
54
4 #aria$%e and &'pe
In this e1a-le,
la/out(triangle!stri7 max!vertices 3 B?) out; // order does not matter
la/out(max!vertices 3 B?) out; // redeclaration o=a/
la/out(triangle!stri) out; // redeclaration o=a/
la/out(oints) out; // error7 contradicts triangle!stri
la/out(max!vertices 3 >?) out; // error7 contradicts B?
all out-uts %ro the geoetr$ shader are triangles and at ost .' vertices will :e eitted :$ the shader. It
is an error %or the a1iu nu:er o% vertices to :e greater than gl_La#(eometry)ututVertices.
,he identi%ier stream is used to s-eci%$ that a geoetr$ shader out-ut varia:le or :lock is associated with
a -articular verte1 strea Bnu:ered :eginning with =eroC. A de%ault strea nu:er a$ :e declared at
glo:al sco-e :$ <uali%$ing inter%ace <uali%ier out as in this e1a-le:
la/out(stream 3 0) out;
,he strea nu:er s-eci%ied in such a declaration re-laces an$ -revious de%ault and a--lies to all
su:se<uent :lock and varia:le declarations until a new de%ault is esta:lished. ,he initial de%ault strea
nu:er is =ero.
Each out-ut :lock or non9:lock out-ut varia:le is associated with a verte1 strea. I% the :lock or varia:le
is declared with the strea identi%ier, it is associated with the s-eci%ied streaN otherwise, it is associated
with the current de%ault strea. A :lock e:er a$ :e declared with a strea identi%ier, :ut the
s-eci%ied strea ust atch the strea associated with the containing :lock. )ne e1a-le:
la/out(stream30) out; // default is now stream 0
out vec; var0; // var0 gets default stream (0)
la/out(stream3<) out 9loc=0 4 // M9loc=0M belongs to stream <
la/out(stream3<) vec; var<; // redundant bloc= member stream decl
la/out(stream3>) vec< var>; // #""%-D" (must match bloc= stream)
vec> var;; // belongs to stream <
8;
la/out(stream3?) out; // default is now stream ?
out vec; varE; // varE gets default stream (?)
out 9loc=< 4 // M9loc=<M gets default stream (?)
vec; varB;
8;
la/out(stream3>) out vec; varC; // varC belongs to stream >
Each verte1 eitted :$ the geoetr$ shader is assigned to a s-eci%ic strea, and the attri:utes o% the
eitted verte1 are taken %ro the set o% out-ut :locks and varia:les assigned to the targeted strea. A%ter
each verte1 is eitted, the values o% all out-ut varia:les :ecoe unde%ined. Additionall$, the out-ut
varia:les associated with each verte1 strea a$ share storage. @riting to an out-ut varia:le associated
with one strea a$ overwrite out-ut varia:les associated with an$ other strea. @hen eitting each
verte1, a geoetr$ shader should write to all out-uts associated with the strea to which the verte1 will
:e eitted and to no out-uts associated with an$ other strea.
I% a geoetr$ shader out-ut :lock or varia:le is declared ore than once, all such declarations ust
associate the varia:le with the sae verte1 strea. I% an$ strea declaration s-eci%ies a non9e1istent
strea nu:er, the shader will %ail to co-ile.
55
4 #aria$%e and &'pe
Built9in geoetr$ shader out-uts are alwa$s associated with verte1 strea =ero.
All geoetr$ shader out-ut la$out declarations in a -rogra ust declare the sae la$out and sae value
%or ma#_vertices. I% geoetr$ shaders are in a -rogra, there ust :e at least one geoetr$ out-ut
la$out declaration soewhere in that -rogra, :ut not all geoetr$ shaders Bco-ilation unitsC are
re<uired to declare it.
..2." ,ragment Outputs
,he :uilt9in %ragent shader varia:le gl_&ragDeth a$ :e redeclared using one o% the %ollowing la$out
<uali%iers.
la*out-1ualiier-id
deth_any
deth_greater
deth_less
deth_unchanged
4or e1a-le:
la/out (deth!greater) out float gl!&rag,eth;
,he la$out <uali%ier %or gl_&ragDeth constrains intentions o% the %inal value o% gl_&ragDeth written
:$ an$ shader invocation. /0 i-leentations are allowed to -er%or o-tii=ations assuing that the
de-th test %ails Bor -assesC %or a given %ragent i% all values o% gl_&ragDeth consistent with the la$out
<uali%ier would %ail Bor -assC. I% the %inal value o% gl_&ragDeth is inconsistent with its la$out <uali%ier,
the result o% the de-th test %or the corres-onding %ragent is unde%ined. *owever, no error will :e
generated in this case. I% the de-th test -asses and de-th writes are ena:led, the value written to the de-th
:u%%er is alwa$s the value o% gl_&ragDeth, whether or not it is consistent with the la$out <uali%ier.
B$ de%ault, gl_&ragDeth is <uali%ied as de"t%_an*. @hen the la$out <uali%ier %or gl_&ragDeth is
de"t%_an*, the shader co-iler will note an$ assignent to gl_&ragDeth odi%$ing it in an unknown
wa$, and de-th testing will alwa$s :e -er%ored a%ter the shader has e1ecuted. @hen the la$out <uali%ier
is de"t%_greater, the /0 can assue that the %inal value o% gl_&ragDeth is greater than or e<ual to the
%ragentDs inter-olated de-th value, as given :$ the 0 co-onent o% gl_FragCoord. @hen the la$out
<uali%ier is de"t%_less, the /0 can assue that an$ odi%ication o% gl_&ragDeth will onl$ decrease its
value. @hen the la$out <uali%ier is de"t%_unc%anged, the shader co-iler will honor an$ odi%ication to
gl_&ragDeth, :ut the rest o% the /0 can assue that gl_&ragDeth is not assigned a new value.
56
4 #aria$%e and &'pe
Redeclarations o% gl_&ragDeth are -er%ored as %ollows:
// redeclaration that changes nothing is allowed
out float gl!&rag,eth;
// assume it ma/ be modified in an/ wa/
la/out (deth!an/) out float gl!&rag,eth;
// assume it ma/ be modified such that its value will onl/ increase
la/out (deth!greater) out float gl!&rag,eth;
// assume it ma/ be modified such that its value will onl/ decrease
la/out (deth!less) out float gl!&rag,eth;
// assume it will not be modified
la/out (deth!unchanged) out float gl!&rag,eth;
I% gl_&ragDeth is redeclared in an$ %ragent shader in a -rogra, it ust :e redeclared in all %ragent
shaders in that -rogra that have static assignents to gl_&ragDeth. All redeclarations o%
gl_&ragDeth in all %ragent shaders in a single -rogra ust have the sae set o% <uali%iers. @ithin
an$ shader, the %irst redeclarations o% gl_&ragDeth ust a--ear :e%ore an$ use o% gl_&ragDeth. ,he
:uilt9in gl_&ragDeth is onl$ -redeclared in %ragent shaders, so redeclaring it in an$ other shader
language will :e illegal.
.." 4niform -loc0 Layout :ualifiers
0a$out <uali%iers can :e used %or uni%or :locks, :ut not %or non9:lock uni%or declarations. ,he la$out
<uali%ier identi%iers %or uni%or :locks are
la*out-1ualiier-id
shared
acked
std1I.
row_maMor
column_maMor
binding @ integer-constant
8one o% these have an$ seantic a%%ect at all on the usage o% the varia:les :eing declaredN the$ onl$
descri:e how data is laid out in eor$. 4or e1a-le, atri1 seantics are alwa$s colun9:ased, as
descri:ed in the rest o% this s-eci%ication, no atter what la$out <uali%iers are :eing used.
7ni%or :lock la$out <uali%iers can :e declared %or glo:al sco-e, on a single uni%or :lock, or on a single
:lock e:er declaration.
57
4 #aria$%e and &'pe
De%ault la$outs are esta:lished Be1ce-t %or bindingC at glo:al sco-e %or uni%or :locks as
la/out(la*out-1ualiier-id-list) uniform;
@hen this is done, the -revious de%ault <uali%ication is %irst inherited and then overridden as -er the
override rules listed :elow %or each <uali%ier listed in the declaration. ,he result :ecoes the new de%ault
<uali%ication sco-ed to su:se<uent uni%or :lock de%initions.
,he initial state o% co-ilation is as i% the %ollowing were declared:
la/out(shared7 column!ma:or) uniform;
E1-licitl$ declaring this in a shader will return de%aults :ack to their initial state.
7ni%or :locks can :e declared with o-tional la$out <uali%iers, and so can their individual e:er
declarations. #uch :lock la$out <uali%ication is sco-ed onl$ to the content o% the :lock. As with glo:al
la$out declarations, :lock la$out <uali%ication %irst inherits %ro the current de%ault <uali%ication and then
overrides it. #iilarl$, individual e:er la$out <uali%ication is sco-ed Lust to the e:er declaration,
and inherits %ro and overrides the :lockDs <uali%ication.
,he s%ared <uali%ier overrides onl$ the std140 and "ac.ed <uali%iersN other <uali%iers are inherited. ,he
co-ilerHlinker will ensure that ulti-le -rogras and -rograa:le stages containing this de%inition
will share the sae eor$ la$out %or this :lock, as long as the$ also atched in their ro/_ma8or andHor
column_ma8or <uali%ications. ,his allows use o% the sae :u%%er to :ack the sae :lock de%inition across
di%%erent -rogras.
,he "ac.ed <uali%ier overrides onl$ std140 and s%aredN other <uali%iers are inherited. @hen "ac.ed is
used, no sharea:le la$out is guaranteed. ,he co-iler and linker can o-tii=e eor$ use :ased on what
varia:les activel$ get used and on other criteria. )%%sets ust :e <ueried, as there is no other wa$ o%
guaranteeing where Band whichC varia:les reside within the :lock. Atte-ts to share a -acked uni%or
:lock across -rogras or stages will generall$ %ail. *owever, i-leentations a$ aid a--lication
anageent o% -acked :locks :$ using canonical la$outs %or -acked :locks.
,he std140 <uali%ier overrides onl$ the "ac.ed and s%ared <uali%iersN other <uali%iers are inherited. ,he
la$out is e1-licitl$ deterined :$ this, as descri:ed in section !.11.& G7ni%or Varia:lesI under #tandard
7ni%or Block 0a$out o% the )-en/0 /ra-hics #$ste #-eci%ication. *ence, as in s%ared a:ove, the
resulting la$out is sharea:le across -rogras.
0a$out <uali%iers on e:er declarations cannot use the s%ared, "ac.ed, or std140 <uali%iers. ,hese can
onl$ :e used at glo:al sco-e or on a :lock declaration.
,he ro/_ma8or <uali%ier overrides onl$ the column_ma8or <uali%ierN other <uali%iers are inherited. It onl$
a%%ects the la$out o% atrices. Eleents within a atri1 row will :e contiguous in eor$.
,he column_ma8or <uali%ier overrides onl$ the ro/_ma8or <uali%ierN other <uali%iers are inherited. It onl$
a%%ects the la$out o% atrices. Eleents within a atri1 colun will :e contiguous in eor$.
,he binding identi%ier s-eci%ies the uni%or :u%%er :inding -oint corres-onding to the uni%or :lock,
which will :e used to o:tain the values o% the e:er varia:les o% the :lock. It is an error to s-eci%$ the
binding identi%ier %or the glo:al sco-e or %or :lock e:er declarations. An$ uni%or :lock declared
without a binding identi%ier is initiall$ assigned to :lock :inding -oint =ero. A%ter a -rogra is linked, the
58
4 #aria$%e and &'pe
:inding -oints used %or uni%or :locks declared with or without a binding identi%ier can :e u-dated :$ the
)-en/0 A2I.
I% the binding identi%ier is used with a uni%or :lock instanced as an arra$ then the %irst eleent o% the
arra$ takes the s-eci%ied :lock :inding and each su:se<uent eleent takes the ne1t consecutive uni%or
:lock :inding -oint.
I% the :inding -oint %or an$ uni%or :lock instance is less than =ero, or greater than or e<ual to the
i-leentation9de-endent a1iu nu:er o% uni%or :u%%er :indings, a co-ilation error will occur.
@hen the binding identi%ier is used with a uni%or :lock instanced as an arra$ o% si=e ,, all eleents o%
the arra$ %ro binding through binding 9 , : 1 ust :e within this range.
@hen ulti-le arguents are listed in a layout declaration, the a%%ect will :e the sae as i% the$ were
declared one at a tie, in order %ro le%t to right, each in turn inheriting %ro and overriding the result
%ro the -revious <uali%ication.
4or e1a-le
la/out(row!ma:or7 column!ma:or)
results in the <uali%ication :eing column_ma8or. )ther e1a-les:
la/out(shared7 row!ma:or) uniform; // default is now shared and row!ma:or
la/out(std0;?) uniform +ransform 4 // la/out of this bloc= is std0;?
mat; G0; // row!ma:or
la/out(column!ma:or) mat; G<; // column ma:or
mat> $0; // row!ma:or
8;
uniform +< 4 // la/out of this bloc= is shared
555
8;
la/out(column!ma:or) uniform +> 4 // shared and column!ma:or
mat; G>; // column!ma:or
la/out(row!ma:or) mat; m;; // row ma:or
mat> $<; // column!ma:or
8;
.. Opa/ue64niform Layout :ualifiers
7ni%or la$out <uali%iers can :e used to :ind o-a<ue uni%or varia:les to s-eci%ic :u%%ers or units.
,e1ture iage units can :e :ound to sa-lers, iage units can :e :ound to iages, and atoic counters
can :e :ound to :u%%ers.
Details %or s-eci%ic to iage %orats and atoic counter :indings are given in the su:sections :elow.
Iage and sa-ler t$-es :oth take the uni%or la$out <uali%ier identi%ier %or :inding:
la*out-1ualiier-id
binding @ integer-constant
5!
4 #aria$%e and &'pe
,he identi%ier binding s-eci%ies which unit will :e :ound. An$ uni%or sa-ler or iage varia:le
declared without a binding <uali%ier is initiall$ :ound to unit =ero. A%ter a -rogra is linked, the unit
re%erenced :$ a sa-ler or iage uni%or varia:le declared with or without a :inding identi%ier can :e
u-dated :$ the )-en/0 A2I.
I% the binding identi%ier is used with an arra$, the %irst eleent o% the arra$ takes the s-eci%ied unit and
each su:se<uent eleent takes the ne1t consecutive unit.
I% the binding is less than =ero, or greater than or e<ual to the i-leentation9de-endent a1iu
su--orted nu:er o% units, a co-ilation error will occur. @hen the binding identi%ier is used with an
arra$ o% si=e ,, all eleents o% the arra$ %ro binding through binding 9 , - 1 ust :e within this range.
A link error will result i% two co-ilation units in a -rogra s-eci%$ di%%erent integer-constant :indings
%or the sae o-a<ue9uni%or nae. *owever, it is not an error to s-eci%$ a :inding on soe :ut not all
declarations %or the sae nae, as shown in the e1a-les :elow.
// in one comilation unit555
la/out(binding3>) uniform samler<, s; // s bound to unit >
// in another comilation unit555
uniform samler<, s; // o=a/7 s still bound at >
// in another comilation unit555
la/out(binding3;) uniform samler<, s; // %((*(1 contradictor/ bindings
...1 .tomic Counter Layout :ualifiers
,he atoic counter <uali%iers are
la*out-1ualiier-id
binding @ integer-constant
offset @ integer-constant
4or e1a-le,
la/out (binding 3 <7 offset 3 ;) uniform atomic!uint a;
will esta:lish that the o-a<ue handle to the atoic counter a will :e :ound to atoic counter :u%%er
:inding -oint ! at an o%%set o% & :asic achine units into that :u%%er. ,he de%ault oset %or :inding -oint !
will :e -ost increented :$ & Bthe si=e o% an atoic counterC.
A su:se<uent atoic counter declaration will inherit the -revious B-ost increentedC o%%set. 4or e1a-le,
a su:se<uent declaration o%
la/out (binding 3 <) uniform atomic!uint bar;
will esta:lish that the atoic counter bar has a :inding to :u%%er :inding -oint ! at an o%%set o% 5 :asic
achine units into that :u%%er. ,he o%%set %or :inding -oint ! will again :e -ost9increented :$ & Bthe si=e
o% an atoic counterC.
@hen ulti-le varia:les are listed in a la$out declaration, the e%%ect will :e the sae as i% the$ were
declared one at a tie, in order %ro le%t to right.
6"
4 #aria$%e and &'pe
Binding -oints are not inherited, onl$ o%%sets. Each :inding -oint tracks its own current de%ault oset %or
inheritance o% su:se<uent varia:les using the sae binding. ,he initial state o% co-ilation is that all
binding -oints have an oset o% '. ,he oset can :e set -er :inding -oint at glo:al sco-e Bwithout
declaring a varia:leC. 4or e1a-le,
la/out (binding 3 <7 offset 3 ;) uniform atomic!uint;
Esta:lishes that the ne1t atomic_uint declaration %or :inding -oint ! will inherit oset & B:ut does not
esta:lish a de%ault bindingC:
la/out (binding 3 <) uniform atomic!uint bar; // offset is ;
la/out (offset 3 F) uniform atomic!uint bar; // error7 no default binding
Atoic counters a$ share the sae :inding -oint, :ut i% a :inding is shared, their o%%sets ust :e either
e1-licitl$ or i-licitl$ B%ro inheritanceC uni<ue and non overla--ing.
E1a-le valid uni%or declarations, assuing to- o% shader:
la/out (binding3>7 offset3;) uniform atomic!uint a; // offset 3 ;
la/out (binding3<) uniform atomic!uint b; // offset 3 ?
la/out (binding3>) uniform atomic!uint c; // offset 3 F
la/out (binding3<) uniform atomic!uint d; // offset 3 ;
E1a-le o% an invalid uni%or declaration:
la/out (offset3;) N // error7 must include binding
la/out (binding307 offset3?) N a; // o=a/
la/out (binding3<7 offset3?) N b; // o=a/
la/out (binding307 offset3?) N c; // error7 offsets must not be shared
// between a and c
la/out (binding307 offset3<) N d; // error7 overlas offset ? of a
It is a co-ile9tie error to :ind an atoic counter with a :inding value greater than or e<ual to
gl_7a#;tomicCounter<indings.
...2 ,ormat Layout :ualifiers
4orat la$out <uali%iers can :e used on iage varia:le declarations Bthose declared with a :asic t$-e
having GimageI in its ke$wordC. ,he %orat la$out <uali%ier identi%iers %or iage varia:le declarations
are
la*out-1ualiier-id
loat-image-ormat-1ualiier
int-image-ormat-1ualiier
uint-image-ormat-1ualiier
binding = integer-constant
loat-image-ormat-1ualiier
rgba'"f
rgba1$f
rg'"f
rg1$f
61
4 #aria$%e and &'pe
r11f_g11f_b1.f
r'"f
r1$f
rgba1$
rgb1._a"
rgba\
rg1$
rg\
r1$
r\
rgba1$_snorm
rgba\_snorm
rg1$_snorm
rg\_snorm
r1$_snorm
r\_snorm
int-image-ormat-1ualiier
rgba'"i
rgba1$i
rgba\i
rg'"i
rg1$i
rg\i
r'"i
r1$i
r\i
uint-image-ormat-1ualiier
rgba'"ui
rgba1$ui
rgb1._a"ui
rgba\ui
rg'"ui
rg1$ui
rg\ui
r'"ui
r1$ui
r\ui
A %orat la$out <uali%ier s-eci%ies the iage %orat associated with a declared iage varia:le. )nl$ one
%orat <uali%ier a$ :e s-eci%ied %or an$ iage varia:le declaration. 4or iage varia:les with %loating9
-oint co-onent t$-es Bke$words starting with GimageIC, signed integer co-onent t$-es Bke$words
starting with GiimageIC, or unsigned integer co-onent t$-es Bke$words starting with GuimageIC, the
%orat <uali%ier used ust atch the loat-image-ormat-1ualiier, int-image-ormat-1ualiier, or uint-
image-ormat-1ualiier graar rules, res-ectivel$. It is an error to declare an iage varia:le where the
%orat <uali%ier does not atch the iage varia:le t$-e.
62
4 #aria$%e and &'pe
An$ iage varia:le used %or iage loads or atoic o-erations ust s-eci%$ a %orat la$out <uali%ierN it is
an error to -ass an iage uni%or varia:le or %unction -araeter declared without a %orat la$out
<uali%ier to an iage load or atoic %unction.
,he binding identi%ier was descri:ed in section &.&.& G7ni%or 0a$out >uali%iersI.
7ni%ors not <uali%ied with writeonly ust have a %orat la$out <uali%ier. 8ote that an iage varia:le
-assed to a %unction %or read access cannot :e declared as writeonly and hence ust have :een declared
with a %orat la$out <uali%ier.
.' Interpolation :ualifiers
In-uts and out-uts that could :e inter-olated can :e %urther <uali%ied :$ at ost one o% the %ollowing
inter-olation <uali%iers:
:ualifier 7eaning
smooth -ers-ective correct inter-olation
flat no inter-olation
noersective linear inter-olation
,he -resence o% and t$-e o% inter-olation is controlled :$ the a:ove inter-olation <uali%iers as well as the
au1iliar$ storage <uali%iers centroid and samle. ,he au1iliar$ storage <uali%ier atch is not used %or
inter-olationN it is an error to use inter-olation <uali%iers with atch.
A varia:le <uali%ied as flat will not :e inter-olated. Instead, it will have the sae value %or ever$
%ragent within a triangle. ,his value will coe %ro a single -rovoking verte1, as descri:ed :$ the
)-en/0 /ra-hics #$ste #-eci%ication. A varia:le a$ :e <uali%ied as flat can also :e <uali%ied as
centroid or samle, which will ean the sae thing as <uali%$ing it onl$ as flat.
A varia:le <uali%ied as smooth will :e inter-olated in a -ers-ective9correct anner over the -riitive
:eing rendered. Inter-olation in a -ers-ective correct anner is s-eci%ied in e<uation (.. in the )-en/0
/ra-hics #$ste #-eci%ication, section (.+ G0ine #egentsI.
A varia:le <uali%ied as noersective ust :e inter-olated linearl$ in screen s-ace, as descri:ed in
e<uation (.3 in the )-en/0 /ra-hics #$ste #-eci%ication, section (.+ G0ine #egentsI.
@hen ulti9sa-le rasteri=ation is disa:led, or %or %ragent shader in-ut varia:les <uali%ied with neither
centroid nor samle, the value o% the assigned varia:le a$ :e inter-olated an$where within the -i1el
and a single value a$ :e assigned to each sa-le within the -i1el, to the e1tent -eritted :$ the
)-en/0 /ra-hics #$ste #-eci%ication.
@hen ultisa-le rasteri=ation is ena:led, centroid and samle a$ :e used to control the location and
%re<uenc$ o% the sa-ling o% the <uali%ied %ragent shader in-ut. I% a %ragent shader in-ut is <uali%ied
with centroid, a single value a$ :e assigned to that varia:le %or all sa-les in the -i1el, :ut that value
ust :e inter-olated to a location that lies in :oth the -i1el and in the -riitive :eing rendered, including
an$ o% the -i1elDs sa-les covered :$ the -riitive. Because the location at which the varia:le is
inter-olated a$ :e di%%erent in neigh:oring -i1els, and derivatives a$ :e co-uted :$ co-uting
di%%erences :etween neigh:oring -i1els, derivatives o% centroid9sa-led in-uts a$ :e less accurate than
those %or non9centroid inter-olated varia:les. I% a %ragent shader in-ut is <uali%ied with samle, a
63
4 #aria$%e and &'pe
se-arate value ust :e assigned to that varia:le %or each covered sa-le in the -i1el, and that value ust
:e sa-led at the location o% the individual sa-le.
,he t$-e and -resence o% inter-olation <uali%iers o% varia:les with the sae nae declared in all linked
shaders %or the sae cross9stage inter%ace ust atch, otherwise the link coand will %ail.
@hen co-aring an out-ut %ro one stage to an in-ut o% a su:se<uent stage, the in-ut and out-ut donDt
atch i% their inter-olation <uali%iers Bor lack thereo%C are not the sae.
.'.1 ;edeclaring -uilt6in Interpolation Variables in the Compatibility +rofile
,he %ollowing -redeclared varia:les can :e redeclared with an inter-olation <uali%ier when using the
co-ati:ilit$ -ro%ile:
Verte1, tessellation control, tessellation evaluation, and geoetr$ languages:
gl!&rontLolor
gl!9ac=Lolor
gl!&ront)econdar/Lolor
gl!9ac=)econdar/Lolor
4ragent language:
gl!Lolor
gl!)econdar/Lolor
4or e1a-le,
in vec; gl!Lolor; // redeclared b/ the fragment language
flat in vec; gl!Lolor; // redeclared b/ user to be flat
flat in vec; gl!&rontLolor; // inut to geometr/ shader7 no gl!inIJ
flat out vec; gl!&rontLolor; // outut from geometr/ shader
Ideall$, these are redeclared as -art o% the redeclaration o% an inter%ace :lock, as descri:ed in section 3.1.1
G"o-ati:ilit$ 2ro%ile Built9In 0anguage Varia:lesI. *owever, %or the a:ove -ur-ose, the$ can :e
redeclared as individual varia:les at glo:al sco-e, outside an inter%ace :lock. A co-ilation error will
result i% a shader has :oth an inter%ace :lock redeclaration and a se-arate redeclaration o% a e:er o%
that inter%ace :lock outside the inter%ace :lock redeclaration.
I% gl_Color is redeclared with an inter-olation <uali%ier, then gl_FrontColor and gl_<ac.Color Bi% the$
are written toC ust also :e redeclared with the sae inter-olation <uali%ier, and vice versa. I%
gl_(econdar*Color is redeclared with an inter-olation <uali%ier, then gl_Front(econdar*Color and
gl_<ac.(econdar*Color Bi% the$ are written toC ust also :e redeclared with the sae inter-olation
<uali%ier, and vice versa. ,his <uali%ier atching on -redeclared varia:les is onl$ re<uired %or varia:les
that are staticall$ used within the shaders in a -rogra.
64
4 #aria$%e and &'pe
.( +arameter :ualifiers
In addition to -recision <uali%iers and eor$ <uali%iers, -araeters can have these -araeter <uali%iers.
:ualifier 7eaning
R none: de%ault S sae is in
const %or %unction -araeters that cannot :e written to
in %or %unction -araeters -assed into a %unction
out %or %unction -araeters -assed :ack out o% a %unction, :ut not initiali=ed
%or use when -assed in
inout %or %unction -araeters -assed :oth into and out o% a %unction
2araeter <uali%iers are discussed in ore detail in section ..1.1 G4unction "alling "onventionsI.
.2 +recision and +recision :ualifiers
2recision <uali%iers are added %or code -orta:ilit$ with )-en/0 E#, not %or %unctionalit$. ,he$ have the
sae s$nta1 as in )-en/0 E#, as descri:ed :elow, :ut the$ have no seantic eaning, which includes no
e%%ect on the -recision used to store or o-erate on varia:les.
I% an e1tension adds in the sae seantics and %unctionalit$ in the )-en/0 E# !.' s-eci%ication %or
-recision <uali%iers, then the e1tension is allowed to reuse the ke$words :elow %or that -ur-ose.
4or the -ur-oses o% deterining i% an out-ut %ro one shader stage atches an in-ut o% the ne1t stage, the
-recision <uali%ier need not atch.
.2.1 ;ange and +recision
,he -recision o% stored single9 and dou:le9-recision %loating9-oint varia:les is de%ined :$ the IEEE 3+&
standard %or (!9:it and .&9:it %loating9-oint nu:ers. ,his includes su--ort %or 8a8s B8ot a 8u:erC
and In%s B-ositive or negative in%initiesC.
,he %ollowing rules a--l$ to :oth single and dou:le9-recision o-erations: In%inities and =eros are
generated as dictated :$ IEEE, :ut su:Lect to the -recisions allowed in the %ollowing ta:le and su:Lect to
allowing -ositive and negative =eros to :e interchanged. *owever, dividing a non9=ero :$ ' results in the
a--ro-riatel$ signed IEEE In%: I% :oth -ositive and negative =eros are i-leented, the correctl$ signed
In% will :e generated, otherwise -ositive In% is generated. An$ denorali=ed value in-ut into a shader or
-otentiall$ generated :$ an$ o-eration in a shader can :e %lushed to '. ,he rounding ode cannot :e set
and is unde%ined. 8a8s are not re<uired to :e generated. #u--ort %or signaling 8a8s is not re<uired and
e1ce-tions are never raised. )-erations and :uilt9in %unctions that o-erate on a 8a8 are not re<uired to
return a 8a8 as the result.
2recisions are e1-ressed in ters o% a1iu relative error in units o% 702 Bunits in the last -laceC,
unless otherwise noted.
65
4 #aria$%e and &'pe
4or single -recision o-erations, -recisions are re<uired as %ollows:
)eration Srecision
a F b, a ] b, a K b "orrectl$ rounded.
R, RT, TT, S, ST "orrect result.
a H b, 1.' H b !.+ 702 %or b in the range [!
91!.
, !
1!.
\.
a K b F c "orrectl$ rounded single o-eration or se<uence o%
two correctl$ rounded o-erations.
fmaBC Inherited %ro a K b F c.
owB#, *C Inherited %ro e#" B# = log" B*CC.
e# B#C, e#" B#C B( F ! K W#WC 702.
log BC, log" BC ( 702 outside the range ['.+, !.'\.
A:solute error R !
9!1
inside the range ['.+, !.'\.
sNrt BC Inherited %ro 1.' H inversesNrtBC.
inversesNrt BC ! 702.
i-licit and e1-licit
conversions :etween t$-es
"orrectl$ rounded.
Built9in %unctions de%ined in the s-eci%ication with an e<uation :uilt %ro the a:ove o-erations inherit the
a:ove errors. ,hese include, %or e1a-le, the geoetric %unctions, the coon %unctions, and an$ o%
the atri1 %unctions. Built9in %unctions not listed a:ove and not de%ined as e<uations o% the a:ove have
unde%ined -recision. ,hese include, %or e1a-le, the trigonoetric %unctions and deterinant.
,he -recision o% dou:le9-recision o-erations is at least that o% single -recision.
.2.2 +recision :ualifiers
An$ %loating -oint or an$ integer declaration can have the t$-e -receded :$ one o% these -recision
<uali%iers:
:ualifier 7eaning
high 8one.
medium 8one.
low 8one.
66
4 #aria$%e and &'pe
4or e1a-le:
low float color;
out medium vec< H;
low ivec< foo(low mat>);
high mat; m;
0iteral constants do not have -recision <uali%iers. 8either do Boolean varia:les. 8either do %loating -oint
constructors nor integer constructors when none o% the constructor arguents have -recision <uali%iers.
2recision <uali%iers, as with other <uali%iers, do not e%%ect the :asic t$-e o% the varia:le. In -articular,
there are no constructors %or -recision conversionsN constructors onl$ convert t$-es. #iilarl$, -recision
<uali%iers, as with other <uali%iers, do not contri:ute to %unction overloading :ased on -araeter t$-es. As
discussed in the ne1t cha-ter, %unction in-ut and out-ut is done through co-ies, and there%ore <uali%iers do
not have to atch.
.2." )efault +recision :ualifiers
,he -recision stateent
recision recision2qualifier t/e;
can :e used to esta:lish a de%ault -recision <uali%ier. ,he tye %ield can :e either int or float, and the
"recision-1ualiier can :e low, medium, or high. An$ other t$-es or <uali%iers will result in an error.
I% t*"e is float, the directive a--lies to non9-recision9<uali%ied %loating -oint t$-e Bscalar, vector, and
atri1C declarations. I% t*"e is int, the directive a--lies to all non9-recision9<uali%ied integer t$-e Bscalar,
vector, signed, and unsignedC declarations. ,his includes glo:al varia:le declarations, %unction return
declarations, %unction -araeter declarations, and local varia:le declarations.
8on9-recision <uali%ied declarations will use the -recision <uali%ier s-eci%ied in the ost recent recision
stateent that is still in sco-e. ,he recision stateent has the sae sco-ing rules as varia:le
declarations. I% it is declared inside a co-ound stateent, its e%%ect sto-s at the end o% the innerost
stateent it was declared in. 2recision stateents in nested sco-es override -recision stateents in outer
sco-es. ;ulti-le -recision stateents %or the sae :asic t$-e can a--ear inside the sae sco-e, with later
stateents overriding earlier stateents within that sco-e.
,he verte1, tessellation, and geoetr$ languages have the %ollowing -redeclared glo:all$ sco-ed de%ault
-recision stateents:
recision high float;
recision high int;
,he %ragent language has the %ollowing -redeclared glo:all$ sco-ed de%ault -recision stateents:
recision medium int;
recision high float;
67
4 #aria$%e and &'pe
.2. .#ailable +recision :ualifiers
,he :uilt9in acro /0?4RA/;E8,?2RE"I#I)8?*I/* is de%ined to 1:
#define -"!&(D-G%$+!H(%L#)#*$!O#-O 0
,his acro is availa:le in the verte1, tessellation, geoetr$, and %ragent languages.
.3 Variance and the In#ariant :ualifier
In this section, variance re%ers to the -ossi:ilit$ o% getting di%%erent values %ro the sae e1-ression in
di%%erent -rogras. 4or e1a-le, sa$ two verte1 shaders, in di%%erent -rogras, each set gl_!osition with
the sae e1-ression in :oth shaders, and the in-ut values into that e1-ression are the sae when :oth
shaders run. It is -ossi:le, due to inde-endent co-ilation o% the two shaders, that the values assigned to
gl_!osition are not e1actl$ the sae when the two shaders run. In this e1a-le, this can cause -ro:les
with alignent o% geoetr$ in a ulti9-ass algorith.
In general, such variance :etween shaders is allowed. @hen such variance does not e1ist %or a -articular
out-ut varia:le, that varia:le is said to :e invariant.
.3.1 The In#ariant :ualifier
,o ensure that a -articular out-ut varia:le is invariant, it is necessar$ to use the invariant <uali%ier. It can
either :e used to <uali%$ a -reviousl$ declared varia:le as :eing invariant
invariant gl!Hosition; // ma=e existing gl!Hosition be invariant
out vec> Lolor;
invariant Lolor; // ma=e existing Lolor be invariant
or as -art o% a declaration when a varia:le is declared
invariant centroid out vec> Lolor;
)nl$ varia:les out-ut %ro a shader Bincluding those that are then in-ut to a su:se<uent shaderC can :e
candidates %or invariance. ,his includes user9de%ined out-ut varia:les and the :uilt9in out-ut varia:les.
As onl$ out-uts need :e declared with invariant, an out-ut %ro one shader stage will still atch an in-ut
o% a su:se<uent stage without the in-ut :eing declared as invariant.
In-ut or out-ut instance naes on :locks are not used when redeclaring :uilt9in varia:les.
,he invariant ke$word can :e %ollowed :$ a coa se-arated list o% -reviousl$ declared identi%iers. All
uses o% invariant ust :e at the glo:al sco-e, and :e%ore an$ use o% the varia:les :eing declared as
invariant.
,o guarantee invariance o% a -articular out-ut varia:le across two -rogras, the %ollowing ust also :e
true:
,he out-ut varia:le is declared as invariant in :oth -rogras.
,he sae values ust :e in-ut to all shader in-ut varia:les consued :$ e1-ressions and %low control
contri:uting to the value assigned to the out-ut varia:le.
68
4 #aria$%e and &'pe
,he te1ture %orats, te1el values, and te1ture %iltering are set the sae wa$ %or an$ te1ture %unction
calls contri:uting to the value o% the out-ut varia:le.
All in-ut values are all o-erated on in the sae wa$. All o-erations in the consuing e1-ressions and
an$ interediate e1-ressions ust :e the sae, with the sae order o% o-erands and sae
associativit$, to give the sae order o% evaluation. Interediate varia:les and %unctions ust :e
declared as the sae t$-e with the sae e1-licit or i-licit -recision <uali%iers. An$ control %low
a%%ecting the out-ut value ust :e the sae, and an$ e1-ressions consued to deterine this control
%low ust also %ollow these invariance rules.
All the data %low and control %low leading to setting the invariant out-ut varia:le reside in a single
co-ilation unit.
Essentiall$, all the data %low and control %low leading to an invariant out-ut ust atch.
Initiall$, :$ de%ault, all out-ut varia:les are allowed to :e variant. ,o %orce all out-ut varia:les to :e
invariant, use the -raga
#ragma )+,-" invariant(all)
:e%ore all declarations in a shader. I% this -raga is used a%ter the declaration o% an$ varia:les or
%unctions, then the set o% out-uts that :ehave as invariant is unde%ined. It is an error to use this -raga in
a %ragent shader.
/enerall$, invariance is ensured at the cost o% %le1i:ilit$ in o-tii=ation, so -er%orance can :e degraded
:$ use o% invariance. *ence, use o% this -raga is intended as a de:ug aid, to avoid individuall$ declaring
all out-ut varia:les as invariant.
.3.2 In#ariance of Constant %*pressions
Invariance ust :e guaranteed %or constant e1-ressions. A -articular constant e1-ression ust evaluate to
the sae result i% it a--ears again in the sae shader or a di%%erent shader. ,his includes the sae
e1-ression a--earing two shaders o% the sae language or shaders o% two di%%erent languages.
"onstant e1-ressions ust evaluate to the sae result when o-erated on as alread$ descri:ed a:ove %or
invariant varia:les.
.8 The +recise :ualifier
#oe algoriths re<uire %loating9-oint co-utations to e1actl$ %ollow the order o% o-erations s-eci%ied in
the source code and to treat all o-erations consistentl$, even i% the i-leentation su--orts o-tii=ations
that could -roduce nearl$ e<uivalent results with higher -er%orance. 4or e1a-le, an$ /0
i-leentations su--ort a Xulti-l$9addX instruction that can co-ute a %loating9-oint e1-ression such as
result 3 (a P b) K (c P d);
in two o-erations instead o% three o-erationsN one ulti-l$ and one ulti-l$9add instead o% two ulti-lies
and one add. ,he result o% a %loating9-oint ulti-l$9add a$ not alwa$s :e identical to %irst doing a
ulti-l$ $ielding a %loating9-oint result and then doing a %loating9-oint add. *ence, in this e1a-le, the
two ulti-l$ o-erations would not :e treated consistentl$N the two ulti-lies could e%%ectivel$ a--ear to
have di%%ering -recisions. @ithout an$ <uali%iers, i-leentations are -eritted to -er%or such
6!
4 #aria$%e and &'pe
o-tii=ations that e%%ectivel$ odi%$ the order or nu:er o% o-erations used to evaluate an e1-ression,
even i% those o-tii=ations a$ -roduce slightl$ di%%erent results relative to uno-tii=ed code.
,he <uali%ier recise will ensure that o-erations contri:uting to a varia:leDs value are done in their stated
order and are done with o-erator consistenc$. )rder is deterined :$ o-erator -recedence and
-arenthesis, as descri:ed in section +.1 G)-eratorsI. )-erator consistenc$ eans %or each -articular
o-erator, %or e1a-le the ulti-l$ o-erator B 2 C, its o-eration is alwa$s co-uted with the sae
-recision. 8ote this e%%ectivel$ -revents %using ulti-le o-erations into a single o-eration.
4or e1a-le,
recise out vec; osition;
declares that o-erations used to -roduce the value o% "osition ust :e -er%ored in e1actl$ the order
s-eci%ied in the source code and with all o-erators :eing treated consistentl$. As with the invariant
<uali%ier Bsection &.5.1C, the -recise <uali%ier a$ :e used to <uali%$ a :uilt9in or -reviousl$ declared user9
de%ined varia:le as :eing -recise:
out vec> Lolor;
recise Lolor; // ma=e existing Lolor be recise
,his <uali%ier will a%%ect the evaluation o% an r9value in a -articular %unction i% and onl$ i% the result is
eventuall$ consued in the sae %unction :$ an l9value <uali%ied as recise. An$ other e1-ressions
within a %unction are not a%%ected, including return values and out-ut -araeters not declared as recise
:ut that are eventuall$ consued outside the %unction :$ an varia:le <uali%ied as recise.
7"
4 #aria$%e and &'pe
#oe e1a-les o% the use o% recise:
in vec; a7 b7 c7 d;
recise out vec; v;
float func(float e7 float f7 float g7 float h)
4
return (ePf) K (gPh); // no constraint on order or
// oerator consistenc/
8
float func<(float e7 float f7 float g7 float h)
4
recise float result 3 (ePf) K (gPh); // ensures same recision for
// the two multilies
return result;
8
float func>(float i7 float :7 recise out float =)
4
= 3 i P i K :; // recise7 due to 6=Q declaration
8
void main()
4
vec; r 3 vec>(a P b); // recise7 used to comute v5x/.
vec; s 3 vec>(c P d); // recise7 used to comute v5x/.
v5x/. 3 r K s; // recise
v5w 3 (a5w P b5w) K (c5w P d5w); // recise
v5x 3 func(a5x7 b5x7 c5x7 d5x); // values comuted in func()
// are $*+ recise
v5x 3 func<(a5x7 b5x7 c5x7 d5x); // reciseR
func>(a5x P b5x7 c5x P d5x7 v5x); // reciseR
8
4or the -ur-oses o% deterining i% an out-ut %ro one shader stage atches an in-ut o% the ne1t stage, the
recise <uali%ier need not atch :etween the in-ut and the out-ut.
.1! 7emory :ualifiers
)nl$ varia:les declared as iage t$-es Bthe :asic o-a<ue t$-es with GimageI in their ke$wordC can :e
<uali%ied with a eor$ <uali%ier.
71
4 #aria$%e and &'pe
Varia:les declared as iage t$-es can <uali%ied with one or ore o% the %ollowing eor$ <uali%iers:
:ualifier 7eaning
coherent eor$ varia:le where reads and writes are coherent with reads and
writes %ro other shader invocations
volatile eor$ varia:le whose underl$ing value a$ :e changed at an$ -oint
during shader e1ecution :$ soe source other than the current shader
invocation
restrict eor$ varia:le where use o% that varia:le is the onl$ wa$ to read
and write the underl$ing eor$ in the relevant shader stage
readonly eor$ varia:le that can :e used to read the underl$ing eor$, :ut
cannot :e used to write the underl$ing eor$
writeonly eor$ varia:le that can :e used to write the underl$ing eor$, :ut
cannot :e used to read the underl$ing eor$
;eor$ accesses to iage varia:les declared using the coherent <uali%ier are -er%ored coherentl$ with
siilar accesses %ro other shader invocations. In -articular, when reading a varia:le declared as
coherent, the values returned will re%lect the results o% -reviousl$ co-leted writes -er%ored :$ other
shader invocations. @hen writing a varia:le declared as coherent, the values written will :e re%lected in
su:se<uent coherent reads -er%ored :$ other shader invocations. As descri:ed in the #ection !.11.1( o%
the )-en/0 #-eci%ication, shader eor$ reads and writes co-lete in a largel$ unde%ined order. ,he
:uilt9in %unction memoryKarrierBC can :e used i% needed to guarantee the co-letion and relative
ordering o% eor$ accesses -er%ored :$ a single shader invocation.
@hen accessing eor$ using varia:les not declared as coherent, the eor$ accessed :$ a shader a$
:e cached :$ the i-leentation to service %uture accesses to the sae address. ;eor$ stores a$ :e
cached in such a wa$ that the values written a$ not :e visi:le to other shader invocations accessing the
sae eor$. ,he i-leentation a$ cache the values %etched :$ eor$ reads and return the sae
values to an$ shader invocation accessing the sae eor$, even i% the underl$ing eor$ has :een
odi%ied since the %irst eor$ read. @hile varia:les not declared as coherent a$ not :e use%ul %or
counicating :etween shader invocations, using non9coherent accesses a$ result in higher
-er%orance.
;eor$ accesses to iage varia:les declared using the volatile <uali%ier ust treat the underl$ing
eor$ as though it could :e read or written at an$ -oint during shader e1ecution :$ soe source other
than the e1ecuting shader invocation. @hen a volatile varia:le is read, its value ust :e re9%etched %ro
the underl$ing eor$, even i% the shader invocation -er%oring the read had -reviousl$ %etched its value
%ro the sae eor$. @hen a volatile varia:le is written, its value ust :e written to the underl$ing
eor$, even i% the co-iler can conclusivel$ deterine that its value will :e overwritten :$ a
su:se<uent write. #ince the e1ternal source reading or writing a volatile varia:le a$ :e another shader
invocation, varia:les declared as volatile are autoaticall$ treated as coherent.
;eor$ accesses to iage varia:les declared using the restrict <uali%ier a$ :e co-iled assuing that
the varia:le used to -er%or the eor$ access is the onl$ wa$ to access the underl$ing eor$ using
the shader stage in <uestion. ,his allows the co-iler to coalesce or reorder loads and stores using
restrict9<uali%ied iage varia:les in wa$s that wouldnDt :e -eritted %or iage varia:les not so <uali%ied,
72
4 #aria$%e and &'pe
:ecause the co-iler can assue that the underl$ing iage wonDt :e read or written :$ other code.
A--lications are res-onsi:le %or ensuring that iage eor$ re%erenced :$ varia:les <uali%ied with
restrict will not :e re%erenced using other varia:les in the sae sco-eN otherwise, accesses to restrict9
<uali%ied varia:les will have unde%ined results.
;eor$ accesses to iage varia:les declared using the readonly <uali%ier a$ onl$ read the underl$ing
eor$, which is treated as read9onl$ eor$ and cannot :e written to. It is an error to -ass an iage
varia:le <uali%ied with readonly to image%toreBC or other :uilt9in %unctions that odi%$ iage eor$.
;eor$ accesses to iage varia:les declared using the writeonly <uali%ier a$ onl$ write the underl$ing
eor$N the underl$ing eor$ cannot :e read. It is an error to -ass an iage varia:le <uali%ied with
writeonly to imageHoadBC or other :uilt9in %unctions that read iage eor$.
,he values o% iage varia:les <uali%ied with coherent, volatile, restrict, readonly, or writeonly a$ not
:e -assed to %unctions whose %oral -araeters lack such <uali%iers. B#ee section ..1 G4unction
De%initionsI %or ore detail on %unction calling.C It is legal to have additional <uali%iers on a %oral
-araeter, :ut not to have %ewer.
vec; funcD(restrict image<, a) 4 555 8
vec; func9(image<, a) 4 555 8
la/out(rgba><f) uniform image<, img0;
la/out(rgba><f) coherent uniform image<, img<;
funcD(img0); // *S7 adding MrestrictM is allowed
func9(img<); // illegal7 striing McoherentM is not
0a$out <uali%iers cannot :e used on %oral %unction -araeters, :ut the$ are not included in -araeter
atching.
8ote that the use o% const in an iage varia:le declaration is <uali%$ing the const9ness o% varia:le :eing
declared, not the iage it re%ers to: ,he <uali%ier readonly <uali%ies the iage eor$ Bas accessed
through that varia:leC while const <uali%iers the varia:le itsel%.
.11 Order of :ualification
@hen ulti-le <uali%iers are -resent in a declaration, the$ a$ a--ear in an$ order, :ut the$ ust all
a--ear :e%ore the t$-e. ,he layout <uali%ier is the onl$ <uali%ier that can a--ear ore than once. 4urther,
a declaration can have at ost one storage <uali%ier, at ost one au1iliar$ storage <uali%ier, and at ost
one inter-olation <uali%ier. ;ulti-le eor$ <uali%iers can :e used. An$ violation o% these rules will
cause a co-ile9tie error.
73
' Operators and %*pressions
'.1 Operators
,he )-en/0 #hading 0anguage has the %ollowing o-erators.
+recedence Operator Class Operators .ssociati#ity
1 BhighestC -arenthetical grou-ing 8 9 8A
!
arra$ su:scri-t
%unction call and constructor structure
%ield or ethod selector, swi==le
-ost %i1 increent and decreent
6 7
8 9
.
00 **
0e%t to Right
(
-re%i1 increent and decreent
unar$
00 **
0 * ? A
Right to 0e%t
& ulti-licative 2 1 3 0e%t to Right
+ additive 0 * 0e%t to Right
. :it9wise shi%t 44 55 0e%t to Right
3 relational 4 5 4@ 5@ 0e%t to Right
5 e<ualit$ @@ A@ 0e%t to Right
6 :it9wise and > 0e%t to Right
1' :it9wise e1clusive or < 0e%t to Right
11 :it9wise inclusive or = 0e%t to Right
1! logical and >> 0e%t to Right
1( logical e1clusive or << 0e%t to Right
1& logical inclusive or = = 0e%t to Right
1+ selection D B Right to 0e%t
1.
Assignent
arithetic assignents
@
0@ *@
2@ 1@
3@ 44@ 55@
>@ <@ =@
Right to 0e%t
13 BlowestC se<uence - 0e%t to Right
,here is no address9o% o-erator nor a dere%erence o-erator. ,here is no t$-ecast o-eratorN constructors
are used instead.
74
5 Operator and ()preion
'.2 .rray Operations
,hese are now descri:ed in section +.3 G#tructure and Arra$ )-erationsI.
'." ,unction Calls
I% a %unction returns a value, then a call to that %unction a$ :e used as an e1-ression, whose t$-e will :e
the t$-e that was used to declare or de%ine the %unction.
4unction de%initions and calling conventions are discussed in section ..1 G4unction De%initionsI .
'. Constructors
"onstructors use the %unction call s$nta1, where the %unction nae is a t$-e, and the call akes an o:Lect
o% that t$-e. "onstructors are used the sae wa$ in :oth initiali=ers and e1-ressions. B#ee section 6
G#hading 0anguage /raarI %or details.C ,he -araeters are used to initiali=e the constructed value.
"onstructors can :e used to re<uest a data t$-e conversion to change %ro one scalar t$-e to another
scalar t$-e, or to :uild larger t$-es out o% saller t$-es, or to reduce a larger t$-e to a saller t$-e.
In general, constructors are not :uilt9in %unctions with -redeterined -rotot$-es. 4or arra$s and
structures, there ust :e e1actl$ one arguent in the constructor %or each eleent or e:er. 4or the
other t$-es, the arguents ust -rovide a su%%icient nu:er o% co-onents to -er%or the initiali=ation,
and it is an error to include so an$ arguents that the$ cannot all :e used. Detailed rules %ollow. ,he
-rotot$-es actuall$ listed :elow are erel$ a su:set o% e1a-les.
'..1 Con#ersion and Scalar Constructors
"onverting :etween scalar t$-es is done as the %ollowing -rotot$-es indicate:
int(uint) // converts an unsigned integer to a signed integer
int(bool) // converts a 9oolean 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 9oolean 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 9oolean
bool(uint) // converts an unsigned integer value to a 9oolean value
bool(float) // converts a float value to a 9oolean
bool(double) // converts a double value to a 9oolean
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 9oolean 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 9oolean value to a double
double(float)// converts a float value to a double
75
5 Operator and ()preion
@hen constructors are used to convert an$ %loating9-oint t$-e to an integer t$-e, the %ractional -art o% the
%loating9-oint value is dro--ed. It is unde%ined to convert a negative %loating -oint value to an uint.
@hen a constructor is used to convert an$ integer or %loating9-oint t$-e 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$
integer or %loating9-oint t$-e, false is converted to ' or '.', and true is converted to 1 or 1.'.
,he constructor intBuintC -reserves the :it -attern in the arguent, which will change the arguentDs
value i% its sign :it is set. ,he constructor uintBintC -reserves the :it -attern in the arguent, which will
change its value i% it is negative.
Identit$ constructors, like floatBfloatC are also legal, :ut o% little use.
#calar constructors with non9scalar -araeters can :e used to take the %irst eleent %ro a non9scalar.
4or e1a-le, the constructor floatBvec'C will select the %irst co-onent o% the vec' -araeter.
'..2 Vector and 7atri* Constructors
"onstructors can :e used to create vectors or atrices %ro a set o% scalars, vectors, or atrices. ,his
includes the a:ilit$ to shorten vectors.
I% there is a single scalar -araeter to a vector constructor, it is used to initiali=e all co-onents o% the
constructed vector to that scalarMs value. I% there is a single scalar -araeter to a atri1 constructor, it is
used to initiali=e all the co-onents on the atri1Ms diagonal, with the reaining co-onents initiali=ed
to '.'.
I% a vector is constructed %ro ulti-le scalars, one or ore vectors, or one or ore atrices, or a i1ture
o% these, the vectorDs co-onents will :e constructed in order %ro the co-onents o% the arguents. ,he
arguents will :e consued le%t to right, and each arguent will have all its co-onents consued, in
order, :e%ore an$ co-onents %ro the ne1t arguent are consued. #iilarl$ %or constructing a atri1
%ro ulti-le scalars or vectors, or a i1ture o% these. ;atri1 co-onents will :e constructed and
consued in colun aLor order. In these cases, there ust :e enough co-onents -rovided in the
arguents to -rovide an initiali=er %or ever$ co-onent in the constructed value. It is an error to -rovide
e1tra arguents :e$ond this last used arguent.
I% a atri1 is constructed %ro a atri1, then each co-onent Bcolun i) row 8C in the result that has a
corres-onding co-onent Bcolun i) row 8C in the arguent will :e initiali=ed %ro there. All other
co-onents will :e initiali=ed to the identit$ atri1. I% a atri1 arguent is given to a atri1 constructor,
it is an error to have an$ other arguents.
I% the :asic t$-e Bbool- int- float, or doubleC o% a -araeter to a constructor does not atch the :asic t$-e
o% the o:Lect :eing constructed, the scalar construction rules Ba:oveC are used to convert the -araeters.
76
5 Operator and ()preion
#oe use%ul vector constructors are as %ollows:
vec>(float) // initiali.es each comonent of the vec> with the float
vec;(ivec;) // ma=es a vec; with comonent2wise conversion
vec;(mat<) // the vec; is column ? followed b/ column 0
vec<(float7 float) // initiali.es a vec< with < floats
ivec>(int7 int7 int) // initiali.es an ivec> with > ints
bvec;(int7 int7 float7 float) // uses ; 9oolean conversions
vec<(vec>) // dros the third comonent of a vec>
vec>(vec;) // dros the fourth comonent of a vec;
vec>(vec<7 float) // vec>5x 3 vec<5x7 vec>5/ 3 vec<5/7 vec>5. 3 float
vec>(float7 vec<) // vec>5x 3 float7 vec>5/ 3 vec<5x7 vec>5. 3 vec<5/
vec;(vec>7 float)
vec;(float7 vec>)
vec;(vec<7 vec<)
#oe e1a-les o% these are:
vec; color 3 vec;(?5?7 05?7 ?5?7 05?);
vec; rgba 3 vec;(05?); // sets each comonent to 05?
vec> rgb 3 vec>(color); // dro the ;th comonent
,o initiali=e the diagonal o% a atri1 with all other eleents set to =ero:
mat<(float)
mat>(float)
mat;(float)
,hat is, result5i6586 is set to the %loat arguent %or all i > 8 and set to ' %or all i 8.
77
5 Operator and ()preion
,o initiali=e a atri1 :$ s-eci%$ing vectors or scalars, the co-onents are assigned to the atri1 eleents
in colun9aLor order.
mat<(vec<7 vec<); // one column er argument
mat>(vec>7 vec>7 vec>); // one column er argument
mat;(vec;7 vec;7 vec;7 vec;); // one column er argument
mat>x<(vec<7 vec<7 vec<); // one column er argument
dmat<(dvec<7 dvec<);
dmat>(dvec>7 dvec>7 dvec>);
dmat;(dvec;7 dvec;7 dvec;7 dvec;);
mat<(float7 float7 // first column
float7 float); // second column
mat>(float7 float7 float7 // first column
float7 float7 float7 // second column
float7 float7 float); // third column
mat;(float7 float7 float7 float7 // first column
float7 float7 float7 float7 // second column
float7 float7 float7 float7 // third column
float7 float7 float7 float); // fourth column
mat<x>(vec<7 float7 // first column
vec<7 float); // second column
dmat<x;(dvec>7 double7 // first column
double7 dvec>) // second column
A wide range o% other -ossi:ilities e1ist, to construct a atri1 %ro vectors and scalars, as long as enough
co-onents are -resent to initiali=e the atri1. ,o construct a atri1 %ro a atri1:
mat>x>(mat;x;); // ta=es the uer2left >x> of the mat;x;
mat<x>(mat;x<); // ta=es the uer2left <x< of the mat;x;7 last row is ?7?
mat;x;(mat>x>); // uts the mat>x> in the uer2left7 sets the lower right
// comonent to 07 and the rest to ?
'.." Structure Constructors
)nce a structure is de%ined, and its t$-e is given a nae, a constructor is availa:le with the sae nae to
construct instances o% that structure. 4or e1a-le:
struct light 4
float intensit/;
vec> osition;
8;
light light'ar 3 light(>5?7 vec>(05?7 <5?7 >5?));
78
5 Operator and ()preion
,he arguents to the constructor will :e used to set the structureDs e:ers, in order, using one arguent
-er e:er. Each arguent ust :e the sae t$-e as the e:er it sets, or :e a t$-e that can :e
converted to the e:erDs t$-e according to section &.1.1' GI-licit "onversions.I
#tructure constructors can :e used as initiali=ers or in e1-ressions.
'.. .rray Constructors
Arra$ t$-es can also :e used as constructor naes, which can then :e used in e1-ressions or initiali=ers.
4or e1a-le,
const float cI>J 3 floatI>J(E5?7 C5<7 050);
const float dI>J 3 floatIJ(E5?7 C5<7 050);
float g;
555
float aIEJ 3 floatIEJ(g7 07 g7 <5>7 g);
float bI>J;
b 3 floatI>J(g7 g K 05?7 g K <5?);
,here ust :e e1actl$ the sae nu:er o% arguents as the si=e o% the arra$ :eing constructed. I% no si=e
is -resent in the constructor, then the arra$ is e1-licitl$ si=ed to the nu:er o% arguents -rovided. ,he
arguents are assigned in order, starting at eleent ', to the eleents o% the constructed arra$. Each
arguent ust :e the sae t$-e as the eleent t$-e o% the arra$, or :e a t$-e that can :e converted to the
eleent t$-e o% the arra$ according to section &.1.1' GI-licit "onversions.I
'.' Vector and Scalar Components and Length
,he naes o% the co-onents o% a vector or scalar are denoted :$ a single letter. As a notational
convenience, several letters are associated with each co-onent :ased on coon usage o% -osition,
color or te1ture coordinate vectors. ,he individual co-onents can :e selected :$ %ollowing the varia:le
nae with -eriod B . C and then the co-onent nae.
,he co-onent naes su--orted are:
?#) *) 0) /@ 7se%ul when accessing vectors that re-resent -oints or norals
?r) g) b) a@ 7se%ul when accessing vectors that re-resent colors
?s) t) ") 1@ 7se%ul when accessing vectors that re-resent te1ture coordinates
,he co-onent naes #) r) and s are, %or e1a-le, s$non$s %or the sae B%irstC co-onent in a vector.
,he$ are also the naes o% the onl$ co-onent in a scalar.
8ote that the third co-onent o% the te1ture coordinate set, r in )-en/0, has :een renaed " so as to
avoid the con%usion with r B%or redC in a color.
Accessing co-onents :e$ond those declared %or the t$-e is an error so, %or e1a-le:
7!
5 Operator and ()preion
vec< os;
float height;
os5x // is legal
os5. // is illegal
height5x // is legal
height5/ // is illegal
,he co-onent selection s$nta1 allows ulti-le co-onents to :e selected :$ a--ending their naes
B%ro the sae nae setC a%ter the -eriod B . C.
vec; v;;
v;5rgba; // is a vec; and the same as :ust using v;7
v;5rgb; // is a vec>7
v;5b; // is a float7
v;5x/; // is a vec<7
v;5xgba; // is illegal 2 the comonent names do not come from
// the same set5
,he order o% the co-onents can :e di%%erent to swi==le the, or re-licated:
vec; os 3 vec;(05?7 <5?7 >5?7 ;5?);
vec; swi.3 os5w./x; // swi. 3 (;5?7 >5?7 <5?7 05?)
vec; du 3 os5xx//; // du 3 (05?7 05?7 <5?7 <5?)
float f 3 05<;
vec; du 3 f5xxxx; // du 3 (05<7 05<7 05<7 05<)
,his notation is ore concise than the constructor s$nta1. ,o %or an r9value, it can :e a--lied to an$
e1-ression that results in a vector or scalar r9value. An$ resulting vector o% an$ o-eration ust :e a valid
vector in the languageN hence the %ollowing is illegal:
vec; f;
vec; g 3 os5x/.wx/5x/.w; // illegal; os5x/.wx/ is non2existent vecB
,he co-onent grou- notation can occur on the le%t hand side o% an e1-ression.
vec; os 3 vec;(05?7 <5?7 >5?7 ;5?);
os5xw 3 vec<(E5?7 B5?); // os 3 (E5?7 <5?7 >5?7 B5?)
os5wx 3 vec<(C5?7 F5?); // os 3 (F5?7 <5?7 >5?7 C5?)
os5xx 3 vec<(>5?7 ;5?); // illegal 2 'x' used twice
os5x/ 3 vec>(05?7 <5?7 >5?); // illegal 2 mismatch between vec< and vec>
,o %or an l9value, swi==ling ust :e a--lied to an l9value o% vector or scalar t$-e, contain no du-licate
co-onents, and it results in an l9value o% scalar or vector t$-e, de-ending on nu:er o% co-onents
s-eci%ied.
Arra$ su:scri-ting s$nta1 can also :e a--lied to vectors B:ut not to scalarsC to -rovide nueric inde1ing.
#o in
vec; os;
"os526 re%ers to the third eleent o% -os and is e<uivalent to "os.0. ,his allows varia:le inde1ing into a
vector, as well as a generic wa$ o% accessing co-onents. An$ integer e1-ression can :e used as the
8"
5 Operator and ()preion
su:scri-t. ,he %irst co-onent is at inde1 =ero. Reading %ro or writing to a vector using a constant
integral e1-ression with a value that is negative or greater than or e<ual to the si=e o% the vector is illegal.
@hen inde1ing with non9constant e1-ressions, :ehavior is unde%ined i% the inde1 is negative, or greater
than or e<ual to the si=e o% the vector.
,he length ethod a$ :e a--lied to vectors B:ut not scalarsC. ,he result is the nu:er o% co-onents in
the vector. 4or e1a-le,
vec> v;
const int " 3 v5length();
sets the constant L to (. ,he t$-e returned :$ .lengthBC on a vector is int.
'.( 7atri* Components
,he co-onents o% a atri1 can :e accessed using arra$ su:scri-ting s$nta1. A--l$ing a single su:scri-t
to a atri1 treats the atri1 as an arra$ o% colun vectors, and selects a single colun, whose t$-e is a
vector o% the sae si=e as the atri1. ,he le%tost colun is colun '. A second su:scri-t would then
o-erate on the resulting vector, as de%ined earlier %or vectors. *ence, two su:scri-ts select a colun and
then a row.
mat; m;
mI0J 3 vec;(<5?); // sets the second column to all <5?
mI?JI?J 3 05?; // sets the uer left element to 05?
mI<JI>J 3 <5?; // sets the ;th element of the third column to <5?
Behavior is unde%ined when accessing a co-onent outside the :ounds o% a atri1 with a non9constant
e1-ression. It is an error to access a atri1 with a constant e1-ression that is outside the :ounds o% the
atri1.
,he length ethod a$ :e a--lied to atrices. ,he result is the nu:er o% coluns o% the atri1. 4or
e1a-le,
mat>x; v;
const int " 3 v5length();
sets the constant L to (. ,he t$-e returned :$ .lengthBC on a atri1 is int.
'.2 Structure and .rray Operations
,he e:ers o% a structure and the length ethod o% an arra$ are selected using the -eriod B . C.
In total, onl$ the %ollowing o-erators are allowed to o-erate on arra$s and structures as whole entities:
%ield selector .
e<ualit$ @@ A@
assignent @
inde1ing Barra$s onl$C [ \
81
5 Operator and ()preion
,he e<ualit$ o-erators and assignent o-erator are onl$ allowed i% the two o-erands are sae si=e and
t$-e. #tructure t$-es ust :e o% the sae declared structure. Both arra$ o-erands ust :e e1-licitl$
si=ed. @hen using the e<ualit$ o-erators, two structures are e<ual i% and onl$ i% all the e:ers are
co-onent9wise e<ual, and two arra$s are e<ual i% and onl$ i% all the eleents are eleent9wise e<ual.
Arra$ eleents are accessed using the arra$ su:scri-t o-erator B 6 7 C. An e1a-le o% accessing an arra$
eleent is
diffuseLolor K3 light#ntensit/I>J P $dot";
Arra$ indices start at =ero. Arra$ eleents are accessed using an e1-ression whose t$-e is int or uint.
Behavior is unde%ined i% a shader su:scri-ts an arra$ with an inde1 less than ' or greater than or e<ual to
the si=e the arra$ was declared with.
Arra$s can also :e accessed with the ethod o-erator B . C and the length ethod to <uer$ the si=e o% the
arra$:
light#ntensit/5length() // return the si.e of the arra/
'.3 .ssignments
Assignents o% values to varia:le naes are done with the assignent o-erator B @ C:
lvalue2exression 3 rvalue2exression
,he lvalue-e#"ression evaluates to an l9value. ,he assignent o-erator stores the value o% rvalue-
e#"ression into the l9value and returns an r9value with the t$-e and -recision o% lvalue-e#"ression. ,he
lvalue-e#"ression and rvalue-e#"ression ust have the sae t$-e, or the e1-ression ust have a t$-e in
the ta:le in section &.1.1' GI-licit "onversionsI that converts to the t$-e o% lvalue-e#"ression, in which
case an i-licit conversion will :e done on the rvalue-e#"ression :e%ore the assignent is done. An$
other desired t$-e9conversions ust :e s-eci%ied e1-licitl$ via a constructor. 09values ust :e writa:le.
Varia:les that are :uilt9in t$-es, entire structures or arra$s, structure e:ers, l9values with the %ield
selector B . C a--lied to select co-onents or swi==les without re-eated %ields, l9values within -arentheses,
and l9values dere%erenced with the arra$ su:scri-t o-erator B 6 7 C are all l9values. )ther :inar$ or unar$
e1-ressions, %unction naes, swi==les with re-eated %ields, and constants cannot :e l9values. ,he ternar$
o-erator BDBC is also not allowed as an l9value.
E1-ressions on the le%t o% an assignent are evaluated :e%ore e1-ressions on the right o% the assignent.
,he other assignent o-erators are
add into B0@C
su:tract %ro B*@C
ulti-l$ into B2@C
divide into B1@C
odulus into B3@C
le%t shi%t :$ B44@C
82
5 Operator and ()preion
right shi%t :$ B55@C
and into B>@C
inclusive9or into B=@C
e1clusive9or into B<@C
where the general e1-ression
lvalue op3 exression
is e<uivalent to
lvalue 3 lvalue op exression
where o" is as descri:ed :elow, and the l9value and e1-ression ust satis%$ the seantic re<uireents o%
:oth o" and e<uals B@C.
Reading a varia:le :e%ore writing Bor initiali=ingC it is legal, however the value is unde%ined.
'.8 %*pressions
E1-ressions in the shading language are :uilt %ro the %ollowing:
"onstants o% t$-e bool- all integer t$-es, all %loating9-oint t$-es, all vector t$-es, and all atri1 t$-es.
"onstructors o% all t$-es.
Varia:le naes o% all t$-es.
An arra$ nae with the length ethod a--lied.
#u:scri-ted arra$ naes.
4unction calls that return values.
"o-onent %ield selectors and arra$ su:scri-t results.
2arenthesi=ed e1-ression. An$ e1-ression can :e -arenthesi=ed. 2arentheses can :e used to grou-
o-erations. )-erations within -arentheses are done :e%ore o-erations across -arentheses.
,he arithetic :inar$ o-erators add B0C, su:tract B*C, ulti-l$ B2C, and divide B1C o-erate on integer and
%loating9-oint scalars, vectors, and atrices. I% the %undaental t$-es in the o-erands do not atch,
then the conversions %ro section &.1.1' GI-licit "onversionsI are a--lied to create atching t$-es.
All arithetic :inar$ o-erators result in the sae %undaental t$-e Bsigned integer, unsigned integer,
single9-recision %loating -oint, or dou:le9-recision %loating -ointC as the o-erands the$ o-erate on,
a%ter o-erand t$-e conversion. A%ter conversion, the %ollowing cases are valid
,he two o-erands are scalars. In this case the o-eration is a--lied, resulting in a scalar.
)ne o-erand is a scalar, and the other is a vector or atri1. In this case, the scalar o-eration is
a--lied inde-endentl$ to each co-onent o% the vector or atri1, resulting in the sae si=e vector
or atri1.
83
5 Operator and ()preion
,he two o-erands are vectors o% the sae si=e. In this case, the o-eration is done co-onent9wise
resulting in the sae si=e vector.
,he o-erator is add B0C, su:tract B*C, or divide B1C, and the o-erands are atrices with the sae
nu:er o% rows and the sae nu:er o% coluns. In this case, the o-eration is done co-onent9
wise resulting in the sae si=e atri1.
,he o-erator is ulti-l$ B2C, where :oth o-erands are atrices or one o-erand is a vector and the
other a atri1. A right vector o-erand is treated as a colun vector and a le%t vector o-erand as a
row vector. In all these cases, it is re<uired that the nu:er o% coluns o% the le%t o-erand is e<ual
to the nu:er o% rows o% the right o-erand. ,hen, the ulti-l$ B2C o-eration does a linear
alge:raic ulti-l$, $ielding an o:Lect that has the sae nu:er o% rows as the le%t o-erand and the
sae nu:er o% coluns as the right o-erand. #ection +.1' GVector and ;atri1 )-erationsI
e1-lains in ore detail how vectors and atrices are o-erated on.
All other cases are illegal.
Dividing :$ =ero does not cause an e1ce-tion :ut does result in an uns-eci%ied value. 7se the :uilt9in
%unctions dot, cross, matri#ComLult, and outerSroduct, to get, res-ectivel$, vector dot -roduct,
vector cross -roduct, atri1 co-onent9wise ulti-lication, and the atri1 -roduct o% a colun
vector ties a row vector.
,he o-erator odulus B3C o-erates on signed or unsigned integer scalars or integer vectors. I% the
%undaental t$-es in the o-erands do not atch, then the conversions %ro section &.1.1' GI-licit
"onversionsI are a--lied to create atching t$-es. ,he o-erands cannot :e vectors o% di%%ering si=e.
I% one o-erand is a scalar and the other vector, then the scalar is a--lied co-onent9wise to the vector,
resulting in the sae t$-e as the vector. I% :oth are vectors o% the sae si=e, the result is co-uted
co-onent9wise. ,he resulting value is unde%ined %or an$ co-onent co-uted with a second
o-erand that is =ero, while results %or other co-onents with non9=ero second o-erands reain
de%ined. I% :oth o-erands are non9negative, then the reainder is non9negative. Results are unde%ined
i% one or :oth o-erands are negative. ,he o-erator odulus B3C is not de%ined %or an$ other data
t$-es Bnon9integer t$-esC.
,he arithetic unar$ o-erators negate B9C, -ost9 and -re9increent and decreent B** and 00C o-erate
on integer or %loating9-oint values Bincluding vectors and atricesC. All unar$ o-erators work
co-onent9wise on their o-erands. ,hese result with the sae t$-e the$ o-erated on. 4or -ost9 and
-re9increent and decreent, the e1-ression ust :e one that could :e assigned to Ban l9valueC. 2re9
increent and -re9decreent add or su:tract 1 or 1.' to the contents o% the e1-ression the$ o-erate on,
and the value o% the -re9increent or -re9decreent e1-ression is the resulting value o% that
odi%ication. 2ost9increent and -ost9decreent e1-ressions add or su:tract 1 or 1.' to the contents
o% the e1-ression the$ o-erate on, :ut the resulting e1-ression has the e1-ressionMs value :e%ore the
-ost9increent or -ost9decreent was e1ecuted.
,he relational o-erators greater than B5C, less than B4C, greater than or e<ual B5@C, and less than or
e<ual B4@C o-erate onl$ on scalar integer and scalar %loating9-oint e1-ressions. ,he result is scalar
Boolean. Either the o-erandsM t$-es ust atch, or the conversions %ro section &.1.1' GI-licit
"onversionsI will :e a--lied to o:tain atching t$-es. ,o do co-onent9wise relational co-arisons
on vectors, use the :uilt9in %unctions lessGhan- lessGhanONual- greaterGhan- and
greaterGhanONual.
84
5 Operator and ()preion
,he e<ualit$ o-erators eNual B@@C, and not e<ual BA@C o-erate on all t$-es. ,he$ result in a scalar
Boolean. I% the o-erand t$-es do not atch, then there ust :e a conversion %ro section &.1.1'
GI-licit "onversionsI a--lied to one o-erand that can ake the atch, in which case this
conversion is done. 4or vectors, atrices, structures, and arra$s, all co-onents, e:ers, or
eleents o% one o-erand ust e<ual the corres-onding co-onents, e:ers, or eleents in the other
o-erand %or the o-erands to :e considered e<ual. ,o get a vector o% co-onent9wise e<ualit$ results
%or vectors, use the :uilt9in %unctions eNual and notONual.
,he logical :inar$ o-erators and B>>C, or B = = C, and e1clusive or B<<C o-erate onl$ on two Boolean
e1-ressions and result in a Boolean e1-ression. And B>>C will onl$ evaluate the right hand o-erand
i% the le%t hand o-erand evaluated to true. )r B = = C will onl$ evaluate the right hand o-erand i% the le%t
hand o-erand evaluated to false. E1clusive or B<<C will alwa$s evaluate :oth o-erands.
,he logical unar$ o-erator not BAC. It o-erates onl$ on a Boolean e1-ression and results in a Boolean
e1-ression. ,o o-erate on a vector, use the :uilt9in %unction not.
,he se<uence B - C o-erator that o-erates on e1-ressions :$ returning the t$-e and value o% the right9
ost e1-ression in a coa se-arated list o% e1-ressions. All e1-ressions are evaluated, in order,
%ro le%t to right.
,he ternar$ selection o-erator BDBC. It o-erates on three e1-ressions Be#"1 D e#"2 B e#"AC. ,his
o-erator evaluates the %irst e1-ression, which ust result in a scalar Boolean. I% the result is true, it
selects to evaluate the second e1-ression, otherwise it selects to evaluate the third e1-ression. )nl$
one o% the second and third e1-ressions is evaluated. ,he second and third e1-ressions can :e an$
t$-e, as long their t$-es atch, or there is a conversion in section &.1.1' GI-licit "onversionsI that
can :e a--lied to one o% the e1-ressions to ake their t$-es atch. ,his resulting atching t$-e is the
t$-e o% the entire e1-ression.
,he oneDs co-leent o-erator B?C. ,he o-erand ust :e o% t$-e signed or unsigned integer or integer
vector, and the result is the oneDs co-leent o% its o-erandN each :it o% each co-onent is
co-leented, including an$ sign :its.
,he shi%t o-erators B44C and B55C. 4or :oth o-erators, the o-erands ust :e signed or unsigned
integers or integer vectors. )ne o-erand can :e signed while the other is unsigned. In all cases, the
resulting t$-e will :e the sae t$-e as the le%t o-erand. I% the %irst o-erand is a scalar, the second
o-erand has to :e a scalar as well. I% the %irst o-erand is a vector, the second o-erand ust :e a scalar
or a vector, and the result is co-uted co-onent9wise. ,he result is unde%ined i% the right o-erand is
negative, or greater than or e<ual to the nu:er o% :its in the le%t e1-ressionDs :ase t$-e. ,he value o%
E1 RR E! is E1 Binter-reted as a :it -atternC le%t9shi%ted :$ E! :its. ,he value o% E1 SS E! is E1 right9
shi%ted :$ E! :it -ositions. I% E1 is a signed integer, the right9shi%t will e1tend the sign :it. I% E1 is an
unsigned integer, the right9shi%t will =ero9e1tend.
,he :itwise o-erators and B>C, e1clusive9or B<C, and inclusive9or B=C. ,he o-erands ust :e o% t$-e
signed or unsigned integers or integer vectors. ,he o-erands cannot :e vectors o% di%%ering si=e. I% one
o-erand is a scalar and the other a vector, the scalar is a--lied co-onent9wise to the vector, resulting
in the sae t$-e as the vector. ,he %undaental t$-es o% the o-erands Bsigned or unsignedC ust
atch, and will :e the resulting %undaental t$-e. 4or and B>C, the result is the :itwise9and %unction
o% the o-erands. 4or e1clusive9or B<C, the result is the :itwise e1clusive9or %unction o% the o-erands.
4or inclusive9or B=C, the result is the :itwise inclusive9or %unction o% the o-erands.
4or a co-lete s-eci%ication o% the s$nta1 o% e1-ressions, see section 6 G#hading 0anguage /raar.I
85
5 Operator and ()preion
'.1! Vector and 7atri* Operations
@ith a %ew e1ce-tions, o-erations are co-onent9wise. 7suall$, when an o-erator o-erates on a vector or
atri1, it is o-erating inde-endentl$ on each co-onent o% the vector or atri1, in a co-onent9wise
%ashion. 4or e1a-le,
vec> v7 u;
float f;
v 3 u K f;
will :e e<uivalent to
v5x 3 u5x K f;
v5/ 3 u5/ K f;
v5. 3 u5. K f;
And
vec> v7 u7 w;
w 3 v K u;
will :e e<uivalent to
w5x 3 v5x K u5x;
w5/ 3 v5/ K u5/;
w5. 3 v5. K u5.;
and likewise %or ost o-erators and all integer and %loating -oint vector and atri1 t$-es. ,he e1ce-tions
are atri1 ulti-lied :$ vector, vector ulti-lied :$ atri1, and atri1 ulti-lied :$ atri1. ,hese do
not o-erate co-onent9wise, :ut rather -er%or the correct linear alge:raic ulti-l$.
vec> v7 u;
mat> m;
u 3 v P m;
is e<uivalent to
u5x 3 dot(v7 mI?J); // mI?J is the left column of m
u5/ 3 dot(v7 mI0J); // dot(a7b) is the inner (dot) roduct of a and b
u5. 3 dot(v7 mI<J);
And
u 3 m P v;
is e<uivalent to
u5x 3 mI?J5x P v5x K mI0J5x P v5/ K mI<J5x P v5.;
u5/ 3 mI?J5/ P v5x K mI0J5/ P v5/ K mI<J5/ P v5.;
u5. 3 mI?J5. P v5x K mI0J5. P v5/ K mI<J5. P v5.;
86
5 Operator and ()preion
And
mat> m7 n7 r;
r 3 m P n;
is e<uivalent to
rI?J5x 3 mI?J5x P nI?J5x K mI0J5x P nI?J5/ K mI<J5x P nI?J5.;
rI0J5x 3 mI?J5x P nI0J5x K mI0J5x P nI0J5/ K mI<J5x P nI0J5.;
rI<J5x 3 mI?J5x P nI<J5x K mI0J5x P nI<J5/ K mI<J5x P nI<J5.;
rI?J5/ 3 mI?J5/ P nI?J5x K mI0J5/ P nI?J5/ K mI<J5/ P nI?J5.;
rI0J5/ 3 mI?J5/ P nI0J5x K mI0J5/ P nI0J5/ K mI<J5/ P nI0J5.;
rI<J5/ 3 mI?J5/ P nI<J5x K mI0J5/ P nI<J5/ K mI<J5/ P nI<J5.;
rI?J5. 3 mI?J5. P nI?J5x K mI0J5. P nI?J5/ K mI<J5. P nI?J5.;
rI0J5. 3 mI?J5. P nI0J5x K mI0J5. P nI0J5/ K mI<J5. P nI0J5.;
rI<J5. 3 mI?J5. P nI<J5x K mI0J5. P nI<J5/ K mI<J5. P nI<J5.;
and siilarl$ %or other si=es o% vectors and atrices.
87
( Statements and Structure
,he %undaental :uilding :locks o% the )-en/0 #hading 0anguage are:
stateents and declarations
%unction de%initions
selection Bif*else and switch*case*defaultC
iteration Bfor- while- and do*whileC
Lu-s Bdiscard- return- break- and continueC
,he overall structure o% a shader is as %ollows
translation-unit:
global-declaration
translation-unit global-declaration
global-declaration:
unction-deinition
declaration
,hat is, a shader is a se<uence o% declarations and %unction :odies. 4unction :odies are de%ined as
unction-deinition:
unction-"rotot*"e ? statement-list @
statement-list:
statement
statement-list statement
statement:
com"ound-statement
sim"le-statement
"url$ :races are used to grou- se<uences o% stateents into co-ound stateents.
com"ound-statement:
? statement-list @
sim"le-statement:
declaration-statement
e#"ression-statement
selection-statement
88
6 State*ent and Structure
iteration-statement
8um"-statement
#i-le declaration, e1-ression, and Lu- stateents end in a sei9colon.
,his a:ove is slightl$ si-li%ied, and the co-lete graar s-eci%ied in section 6 G#hading 0anguage
/raarI should :e used as the de%initive s-eci%ication.
Declarations and e1-ressions have alread$ :een discussed.
(.1 ,unction )efinitions
As indicated :$ the graar a:ove, a valid shader is a se<uence o% glo:al declarations and %unction
de%initions. A %unction is declared as the %ollowing e1a-le shows:
// rotot/e
return+/e function$ame (t/e? arg?7 t/e0 arg07 5557 t/en argn);
and a %unction is de%ined like
// definition
return+/e function$ame (t/e? arg?7 t/e0 arg07 5557 t/en argn)
4
// do some comutation
return return'alue;
8
where return$*"e ust :e -resent and include a t$-e. I% the t$-e o% returnValue does not atch
return$*"e) there ust :e an i-licit conversion in section &.1.1' GI-licit "onversionsI that converts
the t$-e o% returnValue to return$*"e) or a co-ile error will result.
Each o% the t*"e, ust include a t$-e and can o-tionall$ include -araeter <uali%iers. ,he %oral
arguent naes Bargs a:oveC in the declarations are o-tional %or :oth the declaration and de%inition
%ors.
A %unction is called :$ using its nae %ollowed :$ a list o% arguents in -arentheses.
Arra$s are allowed as arguents and as the return t$-e. In :oth cases, the arra$ ust :e e1-licitl$ si=ed.
An arra$ is -assed or returned :$ using Lust its nae, without :rackets, and the si=e o% the arra$ ust
atch the si=e s-eci%ied in the %unctionDs declaration.
#tructures are also allowed as arguent t$-es. ,he return t$-e can also :e structure.
#ee section 6 G#hading 0anguage /raarI %or the de%initive re%erence on the s$nta1 to declare and
de%ine %unctions.
All %unctions ust :e either declared with a -rotot$-e or de%ined with a :od$ :e%ore the$ are called. 4or
e1a-le:
float m/func (float f7 // f is an inut arameter
out float g); // g is an outut arameter
8!
6 State*ent and Structure
4unctions that return no value ust :e declared as void. A void %unction can onl$ use return without a
return arguent, even i% the return arguent has void t$-e. Return stateents onl$ acce-t values:
void %unc1BC ^ _
void %unc!BC ^ return %unc1BCN _ HH illegal return stateent
)nl$ a -recision <uali%ier is allowed on the return t$-e o% a %unction. 4oral -araeters can have
-araeter, -recision, and eor$ <uali%iers, :ut no other <uali%iers.
4unctions that acce-t no in-ut arguents need not use void in the arguent list :ecause -rotot$-es Bor
de%initionsC are re<uired and there%ore there is no a:iguit$ when an e-t$ arguent list XB CX is declared.
,he idio GBvoidCI as a -araeter list is -rovided %or convenience.
4unction naes can :e overloaded. ,he sae %unction nae can :e used %or ulti-le %unctions, as long
as the -araeter t$-es di%%er. I% a %unction nae is declared twice with the sae -araeter t$-es, then the
return t$-es and all <uali%iers ust also atch, and it is the sae %unction :eing declared. 4or e1a-le,
vec; f(in vec; x7 out vec; /); // (D)
vec; f(in vec; x7 out uvec; /); // (9) o=a/7 different argument t/e
vec; f(in ivec; x7 out uvec; /); // (L) o=a/7 different argument t/e
int f(in vec; x7 out ivec; /); // error7 onl/ return t/e differs
vec; f(in vec; x7 in vec; /); // error7 onl/ qualifier differs
vec; f(const in vec; x7 out vec; /); // error7 onl/ qualifier differs
@hen %unction calls are resolved, an e1act t$-e atch %or all the arguents is sought. I% an e1act atch is
%ound, all other %unctions are ignored, and the e1act atch is used. I% no e1act atch is %ound, then the
i-licit conversions in section &.1.1' GI-licit "onversionsI will :e a--lied to %ind a atch.
;isatched t$-es on in-ut -araeters Bin or inout or de%ault9 ust have a conversion %ro the calling
arguent t$-e to the %oral -araeter t$-e. ;isatched t$-es on out-ut -araeters Bout or inoutC ust
have a conversion %ro the %oral -araeter t$-e to the calling arguent t$-e.
I% i-licit conversions can :e used to %ind ore than one atching %unction, a single :est9atching
%unction is sought. ,o deterine a :est atch, the conversions :etween calling arguent and %oral
-araeter t$-es are co-ared %or each %unction arguent and -air o% atching %unctions. A%ter these
co-arisons are -er%ored, each -air o% atching %unctions are co-ared. A %unction declaration ; is
considered a :etter atch than %unction declaration < i%
%or at least one %unction arguent, the conversion %or that arguent in ; is :etter than the
corres-onding conversion in <N and
there is no %unction arguent %or which the conversion in < is :etter than the corres-onding
conversion in ;.
I% a single %unction declaration is considered a :etter atch than ever$ other atching %unction
declaration, it will :e used. )therwise, a seantic error %or an a:iguous overloaded %unction call occurs
and the shader will %ail to co-ile.
,o deterine whether the conversion %or a single arguent in one atch is :etter than that %or another
atch, the %ollowing rules are a--lied, in order:
1. An e1act atch is :etter than a atch involving an$ i-licit conversion.
!"
6 State*ent and Structure
!. A atch involving an i-licit conversion %ro float to double is :etter than a atch involving
an$ other i-licit conversion.
(. A atch involving an i-licit conversion %ro either int or uint to float is :etter than a atch
involving an i-licit conversion %ro either int or uint to double.
I% none o% the rules a:ove a--l$ to a -articular -air o% conversions, neither conversion is considered :etter
than the other.
4or the e1a-le %unction -rotot$-es BAC, BBC, and B"C a:ove, the %ollowing e1a-les show how the rules
a--l$ to di%%erent sets o% calling arguent t$-es:
f(vec;7 vec;); // exact match of vec; f(in vec; x7 out vec; /)
f(vec;7 uvec;); // exact match of vec; f(in vec; x7 out ivec; /)
f(vec;7 ivec;); // matched to vec; f(in vec; x7 out vec; /)
// (L) not relevant7 can't convert vec; to
// ivec;5 (D) better than (9) for <nd
// argument (rule <)7 same on first argument5
f(ivec;7 vec;); // $*+ matched5 Dll three match b/ imlicit
// conversion5 (L) is better than (D) and (9)
// on the first argument5 (D) is better than
// (9) and (L)5
7ser9de%ined %unctions can have ulti-le 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 -rotot$-e is visi:leC :e%ore a call
to it, then the linker will onl$ atte-t to resolve that call within the set o% shaders that are linked with it.
,he %unction main is used as the entr$ -oint to a shader e1ecuta:le. A shader need not contain a %unction
naed main, :ut one shader in a set o% shaders linked together to %or a single shader e1ecuta:le ust.
,his %unction takes no arguents, returns no value, and ust :e declared as t$-e voidB
void main()
4
555
8
,he %unction main can contain uses o% return. #ee section ..& GJu-sI %or ore details.
It is an error to declare or de%ine a %unction main with an$ other -araeters or return t$-e.
(.1.1 ,unction Calling Con#entions
4unctions are called :$ value9return. ,his eans in-ut arguents are co-ied into the %unction at call tie,
and out-ut arguents are co-ied :ack to the caller :e%ore %unction e1it. Because the %unction works with
local co-ies o% -araeters, there are no issues regarding aliasing o% varia:les within a %unction. ,o
control what -araeters are co-ied in andHor out through a %unction de%inition or declaration:
,he ke$word in is used as a <uali%ier to denote a -araeter is to :e co-ied in, :ut not co-ied out.
,he ke$word out is used as a <uali%ier to denote a -araeter is to :e co-ied out, :ut not co-ied in.
,his should :e used whenever -ossi:le to avoid unnecessaril$ co-$ing -araeters in.
!1
6 State*ent and Structure
,he ke$word inout is used as a <uali%ier to denote the -araeter is to :e :oth co-ied in and co-ied
out. It eans the sae thing as s-eci%$ing :oth in and out.
A %unction -araeter declared with no such <uali%ier eans the sae thing as s-eci%$ing in.
All arguents are evaluated at call tie, e1actl$ once, in order, %ro le%t to right. Evaluation o% an in
-araeter results in a value that is co-ied to the %oral -araeter. Evaluation o% an out -araeter results
in an l9value that is used to co-$ out a value when the %unction returns. Evaluation o% an inout -araeter
results in :oth a value and an l9valueN the value is co-ied to the %oral -araeter at call tie and the l9
value is used to co-$ out a value when the %unction returns.
,he order in which out-ut -araeters are co-ied :ack to the caller is unde%ined.
I% the %unction atching descri:ed in the -revious section re<uired arguent t$-e conversions, these
conversions are a--lied at co-$9in and co-$9out ties.
In a %unction, writing to an in-ut9onl$ -araeter is allowed. )nl$ the %unctionMs co-$ is odi%ied. ,his
can :e -revented :$ declaring a -araeter with the const <uali%ier.
@hen calling a %unction, e1-ressions that do not evaluate to l9values cannot :e -assed to -araeters
declared as out or inout.
unction-"rotot*"e :
"recision-1ualiier t*"e unction-name3"arameter-1ualiiers "recision-1ualiier t*"e name
arra*-s"eciier) ... 4
t*"e :
an$ :asic t$-e, arra$ t$-e, structure nae, or structure deinition
"arameter-1ualiiers :
e-t$
list o% "arameter-1ualiier
"arameter-1ualiier :
const
in
out
inout
recise
memor* 1ualiier
"recision 1ualiier
name :
e-t$
identi%ier
arra*-s"eciier :
e-t$
6 integral-constant-e#"ression 7
!2
6 State*ent and Structure
,he const <uali%ier cannot :e used with out or inout. ,he a:ove is used %or %unction declarations Bi.e.,
-rotot$-esC and %or %unction de%initions. *ence, %unction de%initions can have unnaed arguents.
Recursion is not allowed, not even staticall$. #tatic recursion is -resent i% the static %unction9call gra-h o%
a -rogra contains c$cles. ,his includes all -otential %unction calls through varia:les declared as
subroutine uniform Bdescri:ed :elowC. It is an error i% a single co-ilation unit BshaderC contains either
static recursion or the -otential %or recursion through su:routine varia:les.
(.1.2 Subroutines
#u:routines -rovide a echanis allowing shaders to :e co-iled in a anner where the target o% one or
ore %unction calls can :e changed at run9tie without re<uiring an$ shader reco-ilation. 4or e1a-le,
a single shader a$ :e co-iled with su--ort %or ulti-le illuination algoriths to handle di%%erent
kinds o% lights or sur%ace aterials. An a--lication using such a shader a$ switch illuination
algoriths :$ changing the value o% its su:routine uni%ors. ,o use su:routines, a su:routine t$-e is
declared, one or ore %unctions are associated with that su:routine t$-e, and a su:routine varia:le o% that
t$-e is declared. ,he %unction currentl$ assigned to the varia:le %unction is then called :$ using %unction
calling s$nta1 re-lacing a %unction nae with the nae o% the su:routine varia:le. #u:routine varia:les
are uni%ors, and are assigned to s-eci%ic %unctions onl$ through coands BUniform%ubroutinesuivC in
the )-en/0 A2I.
#u:routine t$-es are declared using a stateent siilar to a %unction declaration, with the subroutine
ke$word, as %ollows:
subroutine return+/e subroutine+/e$ame(t/e? arg?7 t/e0 arg07
5557 t/en argn);
As with %unction declarations, the %oral arguent naes Bargs a:oveC are o-tional. 4unctions are
associated with su:routine t$-es o% atching declarations :$ de%ining the %unction with the subroutine
ke$word and a list o% su:routine t$-es the %unction atches:
subroutine(subroutine+/e$ame?7 5557 subroutine+/e$ame$)
return+/e function$ame(t/e? arg?7 t/e0 arg07 5557 t/en argn)
4 555 8 // function bod/
It is an error i% arguents and return t$-e donDt atch :etween the %unction and each associated su:routine
t$-e.
4unctions declared with subroutine ust include a :od$. An overloaded %unction cannot :e declared
with subroutineN a -rogra will %ail to co-ile or link i% an$ shader or stage contains two or ore
%unctions with the sae nae i% the nae is associated with a su:routine t$-e.
#u:routine t$-e varia:les are re<uired to :e subroutine uniorms, and are declared with a s-eci%ic
su:routine t$-e in a su:routine uni%or varia:le declaration:
subroutine uniform subroutine+/e$ame subroutine'ar$ame;
#u:routine uni%or varia:les are called the sae wa$ %unctions are called. @hen a su:routine varia:le
Bor an eleent o% a su:routine varia:le arra$C is associated with a -articular %unction, all %unction calls
through that varia:le will call that -articular %unction.
!3
6 State*ent and Structure
7nlike other uni%or varia:les, su:routine uni%or varia:les are sco-ed to the shader e1ecution stage the
varia:le is declared in.
#u:routine varia:les a$ :e declared as e1-licitl$9si=ed arra$s, which can :e d$naicall$ inde1ed at use.
(.2 Selection
"onditional control %low in the shading language is done :$ either if, if9else, or switch stateents:
selection-statement :
if B bool-e#"ression C statement
if B bool-e#"ression C statement else statement
switch B init-e#"ression C ^ s/itc%-statement-listo"t

_
@here s/itc%-statement-list is a list o% =ero or ore s/itc%-statement and other stateents de%ined :$ the
language, where s/itc%-statement adds soe %ors o% la:els. ,hat is
s/itc%-statement-list :
s/itc%-statement
s/itc%-statement-list s/itc%-statement
s/itc%-statement :
case constant-e#"ression B
default B
statement
I% an if*e1-ression evaluates to true, then the %irst statement is e1ecuted. I% it evaluates to false and there
is an else -art then the second statement is e1ecuted.
An$ e1-ression whose t$-e evaluates to a Boolean can :e used as the conditional e1-ression bool-
e#"ression. Vector t$-es are not acce-ted as the e1-ression to if.
"onditionals can :e nested.
,he t$-e o% init-e#"ression in a switch stateent ust :e a scalar integer. I% a case la:el has a constant-
e#"ression o% e<ual value, then e1ecution will continue a%ter that la:el. )therwise, i% there is a default
la:el, e1ecution will continue a%ter that la:el. )therwise, e1ecution ski-s the rest o% the switch stateent.
It is an error to have ore than one default or a re-licated constant-e#"ression. A break stateent not
nested in a loo- or other switch stateent Beither not nested or nested onl$ in if or if9else stateentsC will
also ski- the rest o% the switch stateent. 4all through la:els are allowed, :ut it is an error to have no
stateent :etween a la:el and the end o% the switch stateent. 8o stateents are allowed in a switch
stateent :e%ore the %irst case stateent.
8o case or default la:els can :e nested inside other %low control nested within their corres-onding
switch.
!4
6 State*ent and Structure
(." Iteration
4or, while, and do loo-s are allowed as %ollows:
for (init2exression; condition2exression; loo2exression)
sub2statement
while (condition2exression)
sub2statement
do
statement
while (condition2exression)
#ee section 6 G#hading 0anguage /raarI %or the de%initive s-eci%ication o% loo-s.
,he for loo- %irst evaluates the init-e#"ression, then the condition-e#"ression. I% the condition-
e#"ression evaluates to true, then the :od$ o% the loo- is e1ecuted. A%ter the :od$ is e1ecuted, a for loo-
will then evaluate the loo"-e#"ression, and then loo- :ack to evaluate the condition-e#"ression, re-eating
until the condition-e#"ression evaluates to %alse. ,he loo- is then e1ited, ski--ing its :od$ and ski--ing
its loo"-e#"ression. Varia:les odi%ied :$ the loo"-e#"ression aintain their value a%ter the loo- is
e1ited, -rovided the$ are still in sco-e. Varia:les declared in init-e#"ression or condition-e#"ression are
onl$ in sco-e until the end o% the su:9stateent o% the for loo-.
,he while loo- %irst evaluates the condition-e#"ression. I% true, then the :od$ is e1ecuted. ,his is then
re-eated, until the condition-e#"ression evaluates to %alse, e1iting the loo- and ski--ing its :od$.
Varia:les declared in the condition-e#"ression are onl$ in sco-e until the end o% the su:9stateent o% the
while loo-.
4or :oth for and while loo-s, the su:9stateent does not introduce a new sco-e %or varia:le naes, so the
%ollowing has a redeclaration error:
for (int i 3 ?; i 6 0?; iKK) 4
int i; // redeclaration error
8
,he do*while loo- %irst e1ecutes the :od$, then e1ecutes the condition-e#"ression. ,his is re-eated until
condition-e#"ression evaluates to %alse, and then the loo- is e1ited.
E1-ressions %or condition-e#"ression ust evaluate to a Boolean.
Both the condition-e#"ression and the init-e#"ression can declare and initiali=e a varia:le, e1ce-t in the
do*while loo-, which cannot declare a varia:le in its condition-e#"ression. ,he varia:leMs sco-e lasts
onl$ until the end o% the su:9stateent that %ors the :od$ o% the loo-.
0oo-s can :e nested.
8on9terinating loo-s are allowed. ,he conse<uences o% ver$ long or non9terinating loo-s are -lat%or
de-endent.
!5
6 State*ent and Structure
(. <umps
,hese are the Lu-s:
8um"_statement:
continueC
breakC
returnC
return e#"ressionC
discardC 11 in the %ragent shader language onl$
,here is no GgotoI nor other non9structured %low o% control.
,he continue Lu- is used onl$ in loo-s. It ski-s the reainder o% the :od$ o% the inner ost loo- o%
which it is inside. 4or while and do*while loo-s, this Lu- is to the ne1t evaluation o% the loo-
condition-e#"ression %ro which the loo- continues as -reviousl$ de%ined. 4or for loo-s, the Lu- is to
the loo"-e#"ression, %ollowed :$ the condition-e#"ression.
,he break Lu- can also :e used onl$ in loo-s and switch stateents. It is si-l$ an iediate e1it o%
the inner9ost loo- or switch stateents containing the break. 8o %urther e1ecution o% condition-
e#"ression) loo"-e#"ression, or s/itc%-statement is done.
,he discard ke$word is onl$ allowed within %ragent shaders. It can :e used within a %ragent shader to
a:andon the o-eration on the current %ragent. ,his ke$word causes the %ragent to :e discarded and no
u-dates to an$ :u%%ers will occur. "ontrol %low e1its the shader, and su:se<uent i-licit or e1-licit
derivatives are unde%ined when this e1it is non9uni%or. It would t$-icall$ :e used within a conditional
stateent, %or e1a-le:
if (intensit/ 6 ?5?)
discard;
A %ragent shader a$ test a %ragentMs al-ha value and discard the %ragent :ased on that test.
*owever, it should :e noted that coverage testing occurs a%ter the %ragent shader runs, and the coverage
test can change the al-ha value.
,he return Lu- causes iediate e1it o% the current %unction. I% it has e#"ression then that is the return
value %or the %unction.
,he %unction main can use return. ,his si-l$ causes main to e1it in the sae wa$ as when the end o%
the %unction had :een reached. It does not i-l$ a use o% discard in a %ragent shader. 7sing return in
ain :e%ore de%ining out-uts will have the sae :ehavior as reaching the end o% ain :e%ore de%ining
out-uts.
!6
2 -uilt6in Variables
2.1 -uilt6In Language Variables
#oe )-en/0 o-erations occur in %i1ed %unctionalit$ and need to -rovide values to or receive values
%ro shader e1ecuta:les. #haders counicate with %i1ed9%unction )-en/0 -i-eline stages, and
o-tionall$ with other shader e1ecuta:les, through the use o% :uilt9in in-ut and out-ut varia:les.
In the verte1 language, the :uilt9ins are intrinsicall$ declared as:
in int gl!'ertex#,;
in int gl!#nstance#,;
out gl!Her'ertex 4
vec; gl!Hosition;
float gl!Hoint)i.e;
float gl!Lli,istanceIJ;
8;
!7
7 Bui%t+in #aria$%e
In the geoetr$ language, the :uilt9in varia:les are intrinsicall$ declared as:
in gl!Her'ertex 4
vec; gl!Hosition;
float gl!Hoint)i.e;
float gl!Lli,istanceIJ;
8 gl!inIJ;
in int gl!Hrimitive#,#n;
in int gl!#nvocation#,;
out gl!Her'ertex 4
vec; gl!Hosition;
float gl!Hoint)i.e;
float gl!Lli,istanceIJ;
8;
out int gl!Hrimitive#,;
out int gl!"a/er;
out int gl!'iewort#ndex;
In the tessellation control language, :uilt9in varia:les are intrinsicall$ declared as:
in gl!Her'ertex 4
vec; gl!Hosition;
float gl!Hoint)i.e;
float gl!Lli,istanceIJ;
8 gl!inIgl!GaxHatch'erticesJ;
in int gl!Hatch'ertices#n;
in int gl!Hrimitive#,;
in int gl!#nvocation#,;
out gl!Her'ertex 4
vec; gl!Hosition;
float gl!Hoint)i.e;
float gl!Lli,istanceIJ;
8 gl!outIJ;
atch out float gl!+ess"evel*uterI;J;
atch out float gl!+ess"evel#nnerI<J;
In the tessellation evaluation language, :uilt9in varia:les are intrinsicall$ declared as:
in gl!Her'ertex 4
vec; gl!Hosition;
float gl!Hoint)i.e;
float gl!Lli,istanceIJ;
8 gl!inIgl!GaxHatch'erticesJ;
in int gl!Hatch'ertices#n;
!8
7 Bui%t+in #aria$%e
in int gl!Hrimitive#,;
in vec> gl!+essLoord;
atch in float gl!+ess"evel*uterI;J;
atch in float gl!+ess"evel#nnerI<J;
out gl!Her'ertex 4
vec; gl!Hosition;
float gl!Hoint)i.e;
float gl!Lli,istanceIJ;
8;
In the %ragent language, :uilt9in varia:les are intrinsicall$ declared as:
in vec; gl!&ragLoord;
in bool gl!&ront&acing;
in float gl!Lli,istanceIJ;
in vec< gl!HointLoord;
in int gl!Hrimitive#,;
in int gl!)amle#,;
in vec< gl!)amleHosition;
in int gl!)amleGas=#nIJ;
out float gl!&rag,eth;
out int gl!)amleGas=IJ;
Each o% the a:ove varia:les is discussed :elow.
,he varia:le gl_Verte#+D is a verte1 language in-ut varia:le that holds an integer inde1 %or the verte1, as
de%ined under G#hader In-utsI in section !.11.3 GVar$ing Varia:lesI in the )-en/0 /ra-hics #$ste
#-eci%ication. @hile the varia:le gl_Verte#+D is alwa$s -resent, its value is not alwa$s de%ined.
,he varia:le gl_+nstance+D is a verte1 language in-ut varia:le that holds the instance nu:er o% the
current -riitive in an instanced draw call Bsee G#hader In-utsI in section !.11.3 GVar$ing Varia:lesI in
the )-en/0 /ra-hics #$ste #-eci%icationC. I% the current -riitive does not coe %ro an instanced
draw call, the value o% gl_+nstance+D is =ero.
As an out-ut varia:le, gl_!osition is intended %or writing the hoogeneous verte1 -osition. It can :e
written at an$ tie during shader e1ecution. ,his value will :e used :$ -riitive asse:l$, cli--ing,
culling, and other %i1ed %unctionalit$ o-erations, i% -resent, that o-erate on -riitives a%ter verte1
-rocessing has occurred. Its value is unde%ined a%ter the verte1 -rocessing stage i% the verte1 shader
e1ecuta:le does not write gl_!osition, and it is unde%ined a%ter geoetr$ -rocessing i% the geoetr$
e1ecuta:le calls OmitVerte#BC without having written gl_!osition since the last OmitVerte#BC Bor hasnDt
written it at allC. As an in-ut varia:le, gl_!osition reads the out-ut written in the -revious shader stage to
gl_!osition.
As an out-ut varia:le, gl_!oint(i0e is intended %or a shader to write the si=e o% the -oint to :e rasteri=ed.
It is easured in -i1els. I% gl_!oint(i0e is not written to, its value is unde%ined in su:se<uent -i-e stages.
As an in-ut varia:le, gl_!oint(i0e reads the out-ut written in the -revious shader stage to gl_!oint(i0e .
!!
7 Bui%t+in #aria$%e
,he varia:le gl_Cli"Distance -rovides the %orward co-ati:le echanis %or controlling user cli--ing.
,he eleent gl_Cli"Distance5i6 s-eci%ies a cli- distance %or each -lane i. A distance o% ' eans the
verte1 is on the -lane, a -ositive distance eans the verte1 is inside the cli- -lane, and a negative distance
eans the -oint is outside the cli- -lane. ,he cli- distances will :e linearl$ inter-olated across the
-riitive and the -ortion o% the -riitive with inter-olated distances less than ' will :e cli--ed.
,he gl_Cli"Distance arra$ is -redeclared as unsi=ed and ust :e si=ed :$ the shader either redeclaring it
with a si=e or inde1ing it onl$ with integral constant e1-ressions. ,his needs to si=e the arra$ to include
all the cli- -lanes that are ena:led via the )-en/0 A2IN i% the si=e does not include all ena:led -lanes,
results are unde%ined. ,he si=e can :e at ost gl_7a#Cli"Distances. ,he nu:er o% var$ing co-onents
Bsee gl_7a#Var*ingCom"onents4 consued :$ gl_Cli"Distance will atch the si=e o% the arra$, no
atter how an$ -lanes are ena:led. ,he shader ust also set all values in gl_Cli"Distance that have
:een ena:led via the )-en/0 A2I, or results are unde%ined. Values written into gl_Cli"Distance %or
-lanes that are not ena:led have no e%%ect.
As an out-ut varia:le, gl_Cli"Distance -rovides the -lace %or the shader to write these distances. As an
in-ut in all :ut the %ragent language, it reads the values written in the -revious shader stage. In the
%ragent language, gl_Cli"Distance arra$ contains linearl$ inter-olated values %or the verte1 values
written :$ a shader to the gl_Cli"Distance verte1 out-ut varia:le. )nl$ eleents in this arra$ that have
cli--ing ena:led will have de%ined values.
,he out-ut varia:le gl_!rimitive+D is availa:le onl$ in the geoetr$ language and -rovides a single
integer that serves as a -riitive identi%ier. ,his is then availa:le to %ragent shaders as the %ragent
in-ut gl_!rimitive+D, which will select the written -riitive ID %ro the -rovoking verte1 in the -riitive
:eing shaded. I% a %ragent shader using gl_!rimitive+D is active and a geoetr$ shader is also active,
the geoetr$ shader ust write to gl_!rimitive+D or the %ragent shader in-ut gl_!rimitive+D is
unde%ined. #ee section !.1(.& G/eoetr$ #hader E1ecution EnvironentI Bunder G/eoetr$ #hader
)ut-utsIC and section (.6.! G#hader E1ecutionI Bunder G#hader In-utsIC o% the )-en/0 /ra-hics #$ste
#-eci%ication %or ore in%oration.
4or tessellation control and evaluation languages the in-ut varia:le gl_!rimitive+D is %illed with the
nu:er o% -riitives -rocessed :$ the shader since the current set o% rendering -riitives was started.
4or the %ragent language, it is %illed with the value written to the gl_!rimitive+D geoetr$ shader out-ut
i% a geoetr$ shader is -resent. )therwise, it is assigned in the sae anner as with tessellation control
and evaluation shaders.
,he geoetr$ language in-ut varia:le gl_!rimitive+D+n :ehaves identicall$ to the tessellation control and
evaluation language in-ut varia:le gl_!rimitive+D.
,he in-ut varia:le gl_+nvocation+D is availa:le onl$ in the tessellation control and geoetr$ languages.
In the tessellation control shader, it identi%ies the nu:er o% the out-ut -atch verte1 assigned to the
tessellation control shader invocation. In the geoetr$ shader, it identi%ies the invocation nu:er
assigned to the geoetr$ shader invocation. In :oth cases, gl_+nvocation+D is assigned integer values in
the range 50) ,-16, where , is the nu:er o% out-ut -atch vertices or geoetr$ shader invocations -er
-riitive.
,he out-ut varia:le gl_La*er is availa:le onl$ in the geoetr$ language, and is used to select a s-eci%ic
la$er Bor %ace and la$er o% a cu:e a-C o% a ulti9la$er %rae:u%%er attachent. ,he actual la$er used will
coe %ro one o% the vertices in the -riitive :eing shaded. @hich verte1 the la$er coes %ro is
1""
7 Bui%t+in #aria$%e
unde%ined, so it is :est to write the sae la$er value %or all vertices o% a -riitive. I% a shader staticall$
assigns a value to gl_La*er, la$ered rendering ode is ena:led. #ee section !.1(.& G/eoetr$ #hader
E1ecution EnvironentI Bunder G/eoetr$ #hader )ut-utsIC and section &.&.3 G0a$ered 4rae:u%%ersI
o% the )-en/0 /ra-hics #$ste #-eci%ication %or ore in%oration. I% a shader staticall$ assigns a value
to gl_La*er, and there is an e1ecution -ath through the shader that does not set gl_La*er, then the value o%
gl_La*er is unde%ined %or e1ecutions o% the shader that take that -ath.
,he out-ut varia:le gl_La*er takes on a s-ecial value when used with an arra$ o% cu:e a- te1tures.
Instead o% onl$ re%erring to the la$er, it is used to select a cu:e a- %ace and a la$er. #etting gl_La*er to
the value la*er=B9ace will render to %ace ace o% the cu:e de%ined in la$er la*er. ,he %ace values are
de%ined in ,a:le &.1! o% section &.&.3 G0a$ered 4rae:u%%ersI o% the )-en/0 /ra-hics #$ste
#-eci%ication, :ut re-eated :elow %or clarit$.
&ace Value Jesulting Garget
'
+%T+@(%!L@9%!GDH!H*)#+#'%!T
1
+%T+@(%!L@9%!GDH!$%-D+#'%!T
!
+%T+@(%!L@9%!GDH!H*)#+#'%!U
(
+%T+@(%!L@9%!GDH!$%-D+#'%!U
&
+%T+@(%!L@9%!GDH!H*)#+#'%!V
+
+%T+@(%!L@9%!GDH!$%-D+#'%!V
4or e1a-le, to render to the -ositive * cu:e a- %ace located in the +th la$er o% the cu:e a- arra$,
gl_La*er should :e set to C=B92.
,he out-ut varia:le gl_Vie/"ort+nde# is availa:le onl$ in the geoetr$ language and -rovides the inde1
o% the view-ort to which the ne1t -riitive eitted %ro the geoetr$ shader should :e drawn. 2riitives
generated :$ the geoetr$ shader will undergo view-ort trans%oration and scissor testing using the
view-ort trans%oration and scissor rectangle selected :$ the value o% gl_Vie/"ort+nde#. ,he view-ort
inde1 used will coe %ro one o% the vertices in the -riitive :eing shaded. *owever, which verte1 the
view-ort inde1 coes %ro is i-leentation9de-endent, so it is :est to use the sae view-ort inde1 %or
all vertices o% the -riitive. I% a geoetr$ shader does not assign a value to gl_Vie/"ort+nde#, view-ort
trans%or and scissor rectangle =ero will :e used. I% a geoetr$ shader staticall$ assigns a value to
gl_Vie/"ort+nde# and there is a -ath through the shader that does not assign a value to gl_Vie/"ort+nde#,
the value o% gl_Vie/"ort+nde# is unde%ined %or e1ecutions o% the shader that take that -ath. #ee section
!.1(, under X/eoetr$ #hader )ut-utsX o% the )-en/0 /ra-hics #$ste #-eci%ication B"ore 2ro%ileC %or
ore in%oration.
,he varia:le gl_!atc%Vertices+n is availa:le onl$ in the tessellation control and evaluation languages. It
is an integer s-eci%$ing the nu:er o% vertices in the in-ut -atch :eing -rocessed :$ the shader. A single
tessellation control or evaluation shader can read -atches o% di%%ering si=es, so the value o%
gl_!atc%Vertices+n a$ di%%er :etween -atches.
,he out-ut varia:les gl_$essLevel&uter56 and gl_$essLevel+nner56 are availa:le onl$ in the tessellation
control language. ,he values written to these varia:les are assigned to the corres-onding outer and inner
1"1
7 Bui%t+in #aria$%e
tessellation levels o% the out-ut -atch. ,he$ are used :$ the tessellation -riitive generator to control
-riitive tessellation and a$ :e read :$ tessellation evaluation shaders.
,he varia:le gl_$essCoord is availa:le onl$ in the tessellation evaluation language. It s-eci%ies a three9
co-onent 3u)v)/4 vector identi%$ing the -osition o% the verte1 :eing -rocessed :$ the shader relative to
the -riitive :eing tessellated.
,he in-ut varia:les gl_$essLevel&uter56 and gl_$essLevel+nner56 are availa:le onl$ in the tessellation
evaluation shader. I% a tessellation control shader is active, these varia:les are %illed with corres-onding
out-uts written :$ the tessellation control shader. )therwise, the$ are assigned with de%ault tessellation
levels s-eci%ied in section !.1! G,essellationI in the )-en/0 /ra-hics #$ste #-eci%ication.
4ragent shaders out-ut values to the )-en/0 -i-eline using declared out varia:les, the :uilt9in
varia:les gl_FragDe"t% and gl_(am"le7as., unless the discard stateent is e1ecuted.
,he %i1ed %unctionalit$ co-uted de-th %or a %ragent a$ :e o:tained :$ reading gl_FragCoord.0)
descri:ed :elow.
@riting to gl_FragDe"t% will esta:lish the de-th value %or the %ragent :eing -rocessed. I% de-th
:u%%ering is ena:led, and no shader writes gl_FragDe"t%, then the %i1ed %unction value %or de-th will :e
used as the %ragentMs de-th value. I% a shader staticall$ assigns a value to gl_FragDe"t%, and there is an
e1ecution -ath through the shader that does not set gl_FragDe"t%, then the value o% the %ragentMs de-th
a$ :e unde%ined %or e1ecutions o% the shader that take that -ath. ,hat is, i% the set o% linked %ragent
shaders staticall$ contain a write to gl_FragDe"t%, then it is res-onsi:le %or alwa$s writing it.
I% a shader e1ecutes the discard ke$word, the %ragent is discarded, and the values o% an$ user9de%ined
%ragent out-uts, gl_FragDe"t%, and gl_(am"le7as. :ecoe irrelevant.
,he varia:le gl_FragCoord is availa:le as an in-ut varia:le %ro within %ragent shaders and it holds the
window relative coordinates B#, *, 0, 1D/C values %or the %ragent. I% ulti9sa-ling, this value can :e %or
an$ location within the -i1el, or one o% the %ragent sa-les. ,he use o% centroid does not %urther
restrict this value to :e inside the current -riitive. ,his value is the result o% the %i1ed %unctionalit$ that
inter-olates -riitives a%ter verte1 -rocessing to generate %ragents. ,he 0 co-onent is the de-th value
that would :e used %or the %ragentMs de-th i% no shader contained an$ writes to gl_FragDe"t%. ,his is
use%ul %or invariance i% a shader conditionall$ co-utes gl_FragDe"t% :ut otherwise wants the %i1ed
%unctionalit$ %ragent de-th.
4ragent shaders have access to the in-ut :uilt9in varia:le gl_FrontFacing) whose value is true i% the
%ragent :elongs to a %ront9%acing -riitive. )ne use o% this is to eulate two9sided lighting :$ selecting
one o% two colors calculated :$ a verte1 or geoetr$ shader.
,he values in gl_!ointCoord are two9diensional coordinates indicating where within a -oint -riitive
the current %ragent is located, when -oint s-rites are ena:led. ,he$ range %ro '.' to 1.' across the
-oint. I% the current -riitive is not a -oint, or i% -oint s-rites are not ena:led, then the values read %ro
gl_!ointCoord are unde%ined.
4or :oth the in-ut arra$ gl_(am"le7as.+n56 and the out-ut arra$ gl_(am"le7as.56, :it < o% ask 7
Bgl_(am"le7as.+n576 or gl_(am"le7as.576C corres-onds to sa-le A2=79<. ,hese arra$s have
ceilBsH(!C eleents, where s is the a1iu nu:er o% color sa-les su--orted :$ the i-leentation.
1"2
7 Bui%t+in #aria$%e
,he in-ut varia:le gl_(am"le7as.+n indicates the set o% sa-les covered :$ the -riitive generating the
%ragent during ultisa-le rasteri=ation. It has a sa-le :it set i% and onl$ i% the sa-le is considered
covered %or this %ragent shader invocation.
,he out-ut arra$ gl_(am"le7as.56 sets the sa-le ask %or the %ragent :eing -rocessed. "overage %or
the current %ragent will :ecoe the logical A8D o% the coverage ask and the out-ut gl_(am"le7as..
,his arra$ ust :e si=ed in the %ragent shader either i-licitl$ or e1-licitl$ to :e the sae si=e descri:ed
a:ove. I% the %ragent shader staticall$ assigns a value to gl_(am"le7as., the sa-le ask will :e
unde%ined %or an$ arra$ eleents o% an$ %ragent shader invocations that %ail to assign a value. I% a
shader does not staticall$ assign a value to gl_(am"le7as., the sa-le ask has no e%%ect on the
-rocessing o% a %ragent.
,he in-ut varia:le gl_(am"le+D is %illed with the sa-le nu:er o% the sa-le currentl$ :eing -rocessed.
,his varia:le is in the range 0 to gl_,um(am"les-1, where gl_,um(am"les is the total nu:er o% sa-les
in the %rae:u%%er, or 1 i% rendering to a non9ultisa-le %rae:u%%er. An$ static use o% this varia:le in a
%ragent shader causes the entire shader to :e evaluated -er9sa-le.
,he in-ut varia:le gl_(am"le!osition contains the -osition o% the current sa-le within the ulti9sa-le
draw :u%%er. ,he # and * co-onents o% gl_(am"le!osition contain the su:9-i1el coordinate o% the current
sa-le and will have values in the range '.' to 1.'. An$ static use o% this varia:le in a %ragent shader
causes the entire shader to :e evaluated -er sa-le.
,he gl_!erVerte# :lock can :e redeclared in a shader to e1-licitl$ indicate what su:set o% the %i1ed
-i-eline inter%ace will :e used. ,his is necessar$ to esta:lish the inter%ace :etween ulti-le -rogras.
4or e1a-le:
out gl!Her'ertex 4
vec; gl!Hosition; // will use gl!Hosition
float gl!Hoint)i.e; // will use gl!Hoint)i.e
vec; t; // error7 onl/ gl!Her'ertex members allowed
8; // no other members of gl!Her'ertex will be used
,his esta:lishes the out-ut inter%ace the shader will use with the su:se<uent -i-eline stage. It ust :e a
su:set o% the :uilt9in e:ers o% gl_!erVerte#.
I% a :uilt9in inter%ace :lock is redeclared, it ust a--ear in the shader :e%ore an$ use o% an$ e:er
included in the :uilt9in declaration, or a co-ilation error will result. It is also a co-ilation error to
redeclare the :lock ore than once or to redeclare a :uilt9in :lock and then use a e:er %ro that :uilt9
in :lock that was not included in the redeclaration. Also, i% a :uilt9in inter%ace :lock is redeclared, no
e:er o% the :uilt9in declaration can :e redeclared outside the :lock redeclaration. I% ulti-le shaders
using e:ers o% a :uilt9in :lock :elonging to the sae inter%ace are linked together in the sae
-rogra, the$ ust all redeclare the :uilt9in :lock in the sae wa$, as descri:ed in section &.(.5
GInter%ace BlocksI %or inter%ace :lock atching, or a link error will result. It will also :e a link error i%
soe shaders in a -rogra redeclare a s-eci%ic :uilt9in inter%ace :lock while another shader in that
-rogra does not redeclare that inter%ace :lock $et still uses a e:er o% that inter%ace :lock. I% a :uilt9
in :lock inter%ace is %ored across shaders in di%%erent -rogras, the shaders ust all redeclare the :uilt9
in :lock in the sae wa$ Bas descri:ed %or a single -rograC, or the values -assed along the inter%ace are
unde%ined.
1"3
7 Bui%t+in #aria$%e
2.1.1 Compatibility +rofile -uilt6In Language Variables
@hen using the co-ati:ilit$ -ro%ile, the /0 can -rovide %i1ed %unctionalit$ :ehavior %or the verte1 and
%ragent -rograa:le -i-eline stages. 4or e1a-le, i1ing a %i1ed %unctionalit$ verte1 stage with a
-rograa:le %ragent stage.
,he %ollowing :uilt9in verte1, tessellation control, tessellation evaluation, and geoetr$ out-ut varia:les
are availa:le to s-eci%$ in-uts %or the su:se<uent -rograa:le shader stage or the %i1ed %unctionalit$
%ragent stage. A -articular one should :e written to i% an$ %unctionalit$ in a corres-onding %ragent
shader or %i1ed -i-eline uses it or state derived %ro it. )therwise, :ehavior is unde%ined. ,he %ollowing
e:ers are added to the out-ut gl_!erVerte# :lock in these languages:
out gl!Her'ertex 4 // art of the gl!Her'ertex bloc= described in C50
// in addition to other gl!Her'ertex members555
vec; gl!Lli'ertex;
vec; gl!&rontLolor;
vec; gl!9ac=Lolor;
vec; gl!&ront)econdar/Lolor;
vec; gl!9ac=)econdar/Lolor;
vec; gl!+exLoordIJ;
float gl!&og&ragLoord;
8;
,he out-ut varia:le gl_Cli"Verte# -rovides a -lace %or verte1 and geoetr$ shaders to write the
coordinate to :e used with the user cli--ing -lanes. @riting to gl_Cli"Distance is the -re%erred ethod
%or user cli--ing. It is an error %or the set o% shaders %oring a -rogra to staticall$ read or write :oth
gl_Cli"Verte# and gl_Cli"Distance. I% neither gl_Cli"Verte# nor gl_Cli"Distance is written, their values
are unde%ined and an$ cli--ing against user cli- -lanes is also unde%ined.
#iilarl$ to what was -reviousl$ descri:ed %or the core -ro%ile, the gl_!erVerte# :lock can :e redeclared
in a shader to e1-licitl$ include these additional e:ers. 4or e1a-le:
out gl!Her'ertex 4
vec; gl!Hosition; // will use gl!Hosition
vec; gl!&rontLolor; // will consume gl!color in the fragment shader
vec; gl!9ac=Lolor;
vec; gl!+exLoordI>J; // > elements of gl!+exLoord will be used
8; // no other asects of the fixed interface will be used
,he user ust ensure the cli- verte1 and user cli--ing -lanes are de%ined in the sae coordinate s-ace.
7ser cli- -lanes work -ro-erl$ onl$ under linear trans%or. It is unde%ined what ha--ens under non9
linear trans%or.
,he out-ut varia:les gl_FrontColor, glFront(econdar*Color, gl_<ac.Color, and gl<ac.(econdar*Color
assign -riar$ and secondar$ colors %or %ront and :ack %aces o% -riitives containing the verte1 :eing
-rocessed. ,he out-ut varia:le gl_$e#Coord assigns te1ture coordinates %or the verte1 :eing -rocessed.
4or gl_FogFragCoord, the value written will :e used as the GcI value in section (.11 G4ogI o% the
co-ati:ilit$ -ro%ile o% the )-en/0 /ra-hics #$ste #-eci%ication, :$ the %i1ed %unctionalit$ -i-eline.
4or e1a-le, i% the =9coordinate o% the %ragent in e$e s-ace is desired as GcI, then thatDs what the verte1
shader e1ecuta:le should write into gl_FogFragCoord.
1"4
7 Bui%t+in #aria$%e
As with all arra$s, indices used to su:scri-t gl_$e#Coord ust either :e an integral constant e1-ressions,
or this arra$ ust :e re9declared :$ the shader with a si=e. ,he si=e can :e at ost
gl_7a#$e#tureCoords. 7sing inde1es close to ' a$ aid the i-leentation in -reserving var$ing
resources. ,he redeclaration o% gl_$e#Coord can also :e done at glo:al sco-e as, %or e1a-le:
in vec; gl!+exLoordI>J;
out vec; gl!+exLoordI;J;
B,his treatent is a s-ecial case %or gl_$e#Coord56, not a general ethod %or redeclaring e:ers o%
:locks.C It is a co-ilation error to redeclare gl_$e#Coord56 at glo:al sco-e i% there is a redeclaration o%
the corres-onding :uilt9in :lockN onl$ one %or o% redeclaration is allowed within a shader Band hence
within a stage, as :lock redeclarations ust atch across all shaders using itC.
In the tessellation control, evaluation, and geoetr$ shaders, the out-uts o% the -revious stage descri:ed
a:ove are also availa:le in the in-ut gl_!erVerte# :lock in these languages.
in gl!Her'ertex 4 // art of the gl!Her'ertex bloc= described in C50
// in addition to other gl!Her'ertex members555
vec; gl!Lli'ertex;
vec; gl!&rontLolor;
vec; gl!9ac=Lolor;
vec; gl!&ront)econdar/Lolor;
vec; gl!9ac=)econdar/Lolor;
vec; gl!+exLoordIJ;
float gl!&og&ragLoord;
8 gl!inIJ;
,hese can :e redeclared to esta:lish an e1-licit -i-eline inter%ace, the sae wa$ as descri:ed a:ove %or
the out-ut :lock gl_!erVerte#, and the in-ut redeclaration ust atch the out-ut redeclaration o% the
-revious stage. *owever, when a :uilt9in inter%ace :lock with an instance nae is redeclared Be.g., gl_inC,
the instance nae ust :e included in the redeclaration. It is an error to not include the :uilt9in instance
nae or to change its nae. 4or e1a-le,
in gl!Her'ertex 4
vec; gl!Lli'ertex;
vec; gl!&rontLolor;
8 gl!inIJ; // must be resent and must be gl!inIJ
,reatent o% gl_$e#Coord56 redeclaration is also identical to that descri:ed %or the out-ut :lock
gl_$e#Coord56 redeclaration.
,he %ollowing %ragent in-ut :lock is also availa:le in a %ragent shader when using the co-ati:ilit$
-ro%ile:
1"5
7 Bui%t+in #aria$%e
in gl!Her&ragment 4
in float gl!&og&ragLoord;
in vec; gl!+exLoordIJ;
in vec; gl!Lolor;
in vec; gl!)econdar/Lolor;
8;
,he values in gl_Color and gl_(econdar*Color will :e derived autoaticall$ :$ the s$ste %ro
gl_FrontColor) gl_<ac.Color) gl_Front(econdar*Color) and gl_<ac.(econdar*Color :ased on which
%ace is visi:le in the -riitive -roducing the %ragent. I% %i1ed %unctionalit$ is used %or verte1 -rocessing,
then gl_FogFragCoord will either :e the =9coordinate o% the %ragent in e$e s-ace, or the inter-olation o%
the %og coordinate, as descri:ed in section (.11 G4ogI o% the co-ati:ilit$ -ro%ile o% the )-en/0
/ra-hics #$ste #-eci%ication. ,he gl_$e#Coord56 values are the inter-olated gl_$e#Coord56 values
%ro a verte1 shader or the te1ture coordinates o% an$ %i1ed -i-eline :ased verte1 %unctionalit$.
Indices to the %ragent shader gl_$e#Coord arra$ are as descri:ed a:ove in the verte1 shader te1t.
As descri:ed a:ove %or the in-ut and out-ut gl_!erVerte# :locks, the gl_!erFragment :lock can :e
redeclared to create an e1-licit inter%ace to another -rogra. @hen atching these inter%aces :etween
se-arate -rogras, e:ers in the gl_!erVerte# out-ut :lock ust :e declared i% and onl$ i% the
corres-onding %ragent9shader e:ers generated %ro the are -resent in the gl_!erFragment in-ut
:lock. ,hese atches are descri:ed in detail in section !.1& GVerte1 #hadersI o% the )-en/0 /ra-hics
#$ste #-eci%ication B"o-ati:ilit$ 2ro%ileC. I% the$ donDt atch within a -rogra, a link error will
result. I% the isatch is :etween two -rogras, values -assed :etween -rogras are unde%ined. 7nlike
with all other :lock atching, the order o% declaration within gl_!erFragment does not have to atch
across shaders and does not have to corres-ond with order o% declaration in a atching gl_!erVerte#
redeclaration.
,he %ollowing %ragent out-ut varia:les are availa:le in a %ragent shader when using the co-ati:ilit$
-ro%ile:
out vec; gl!&ragLolor;
out vec; gl!&rag,ataIgl!Gax,raw9uffersJ;
@riting to gl_FragColor s-eci%ies the %ragent color that will :e used :$ the su:se<uent %i1ed
%unctionalit$ -i-eline. I% su:se<uent %i1ed %unctionalit$ consues %ragent color and an e1ecution o% the
%ragent shader e1ecuta:le does not write a value to gl_FragColor then the %ragent color consued is
unde%ined.
,he varia:le gl_FragData is an arra$. @riting to gl_FragData5n6 s-eci%ies the %ragent data that will :e
used :$ the su:se<uent %i1ed %unctionalit$ -i-eline %or data n. I% su:se<uent %i1ed %unctionalit$ consues
%ragent data and an e1ecution o% a %ragent shader e1ecuta:le does not write a value to it, then the
%ragent data consued is unde%ined.
I% a shader staticall$ assigns a value to gl_FragColor, it a$ not assign a value to an$ eleent o%
gl_FragData. I% a shader staticall$ writes a value to an$ eleent o% gl_FragData, it a$ not assign a
value to gl_FragColor. ,hat is, a shader a$ assign values to either gl_FragColor or gl_FragData, :ut
not :oth. ;ulti-le shaders linked together ust also consistentl$ write Lust one o% these varia:les.
#iilarl$, i% user9declared out-ut varia:les are in use Bstaticall$ assigned toC, then the :uilt9in varia:les
1"6
7 Bui%t+in #aria$%e
gl_FragColor and gl_FragData a$ not :e assigned to. ,hese incorrect usages all generate co-ile tie
errors.
I% a shader e1ecutes the discard ke$word, the %ragent is discarded, and the values o% gl_FragDe"t% and
gl_FragColor :ecoe irrelevant.
2.2 Compatibility +rofile Verte* Shader -uilt6In Inputs
,he %ollowing -redeclared in-ut naes can :e used %ro within a verte1 shader to access the current
values o% )-en/0 state when using the co-ati:ilit$ -ro%ile.
in vec; gl!Lolor;
in vec; gl!)econdar/Lolor;
in vec> gl!$ormal;
in vec; gl!'ertex;
in vec; gl!Gulti+exLoord?;
in vec; gl!Gulti+exLoord0;
in vec; gl!Gulti+exLoord<;
in vec; gl!Gulti+exLoord>;
in vec; gl!Gulti+exLoord;;
in vec; gl!Gulti+exLoordE;
in vec; gl!Gulti+exLoordB;
in vec; gl!Gulti+exLoordC;
in float gl!&ogLoord;
2." -uilt6In Constants
,he %ollowing :uilt9in constants are -rovided to all shaders. ,he actual values used are i-leentation
de-endent, :ut ust :e at least the value shown.
//
// #mlementation2deendent constants5 +he examle values below
// are the minimum values allowed for these maximums5
//
const int gl!Gax'ertexDttribs 3 0B;
const int gl!Gax'ertex@niformLomonents 3 0?<;;
const int gl!Gax'ar/ingLomonents 3 B?;
const int gl!Gax'ertex*ututLomonents 3 B;;
const int gl!Gax-eometr/#nutLomonents 3 B;;
const int gl!Gax-eometr/*ututLomonents 3 0<F;
const int gl!Gax&ragment#nutLomonents 3 0<F;
const int gl!Gax'ertex+exture#mage@nits 3 0B;
const int gl!GaxLombined+exture#mage@nits 3 F?;
const int gl!Gax+exture#mage@nits 3 0B;
1"7
7 Bui%t+in #aria$%e
const int gl!Gax#mage@nits 3 F;
const int gl!GaxLombined#mage@nitsDnd&ragment*ututs 3 F;
const int gl!Gax#mage)amles 3 ?;
const int gl!Gax'ertex#mage@niforms 3 ?;
const int gl!Gax+essLontrol#mage@niforms 3 ?;
const int gl!Gax+ess%valuation#mage@niforms 3 ?;
const int gl!Gax-eometr/#mage@niforms 3 ?;
const int gl!Gax&ragment#mage@niforms 3 ?;
const int gl!GaxLombined#mage@niforms 3 ?;
const int gl!Gax&ragment@niformLomonents 3 0?<;;
const int gl!Gax,raw9uffers 3 F;
const int gl!GaxLli,istances 3 F;
const int gl!Gax-eometr/+exture#mage@nits 3 0B;
const int gl!Gax-eometr/*utut'ertices 3 <EB;
const int gl!Gax-eometr/+otal*ututLomonents 3 0?<;;
const int gl!Gax-eometr/@niformLomonents 3 0?<;;
const int gl!Gax-eometr/'ar/ingLomonents 3 B;;
1"8
7 Bui%t+in #aria$%e
const int gl!Gax+essLontrol#nutLomonents 3 0<F;
const int gl!Gax+essLontrol*ututLomonents 3 0<F;
const int gl!Gax+essLontrol+exture#mage@nits 3 0B;
const int gl!Gax+essLontrol@niformLomonents 3 0?<;;
const int gl!Gax+essLontrol+otal*ututLomonents 3 ;?AB;
const int gl!Gax+ess%valuation#nutLomonents 3 0<F;
const int gl!Gax+ess%valuation*ututLomonents 3 0<F;
const int gl!Gax+ess%valuation+exture#mage@nits 3 0B;
const int gl!Gax+ess%valuation@niformLomonents 3 0?<;;
const int gl!Gax+essHatchLomonents 3 0<?;
const int gl!GaxHatch'ertices 3 ><;
const int gl!Gax+ess-en"evel 3 B;;
const int gl!Gax'ieworts 3 0B;
const int gl!Gax'ertex@niform'ectors 3 <EB;
const int gl!Gax&ragment@niform'ectors 3 <EB;
const int gl!Gax'ar/ing'ectors 3 0E;
const int gl!Gax'ertexDtomicLounters 3 ?;
const int gl!Gax+essLontrolDtomicLounters 3 ?;
const int gl!Gax+ess%valuationDtomicLounters 3 ?;
const int gl!Gax-eometr/DtomicLounters 3 ?;
const int gl!Gax&ragmentDtomicLounters 3 F;
const int gl!GaxLombinedDtomicLounters 3 F;
const int gl!GaxDtomicLounter9indings 3 0;
const int gl!Gax'ertexDtomicLounter9uffers 3 ?;
const int gl!Gax+essLontrolDtomicLounter9uffers 3 ?;
const int gl!Gax+ess%valuationDtomicLounter9uffers 3 ?;
const int gl!Gax-eometr/DtomicLounter9uffers 3 ?;
const int gl!Gax&ragmentDtomicLounter9uffers 3 0;
const int gl!GaxLombinedDtomicLounter9uffers 3 0;
const int gl!GaxDtomicLounter9uffer)i.e 3 0B>F;;
const int gl!GinHrogram+exel*ffset 3 2F;
const int gl!GaxHrogram+exel*ffset 3 C;
,he constant gl_7a#Var*ingFloats is reoved in the core -ro%ile, use gl_7a#Var*ingCom"onents
instead.
2.".1 Compatibility +rofile -uilt6In Constants
const int gl!Gax+exture@nits 3 <;
const int gl!Gax+extureLoords 3 F;
const int gl!GaxLliHlanes 3 F;
const int gl!Gax'ar/ing&loats 3 B?;
1"!
7 Bui%t+in #aria$%e
2. -uilt6In 4niform State
As an aid to accessing )-en/0 -rocessing state, the %ollowing uni%or varia:les are :uilt into the
)-en/0 #hading 0anguage.
//
// ,eth range in window coordinates7
// section <50;50 Lontrolling the 'iewort in the
// *en-" -rahics )/stem )ecification5
//
struct gl!,eth(angeHarameters 4
float near; // n
float far; // f
float diff; // f 2 n
8;
uniform gl!,eth(angeHarameters gl!,eth(ange;
uniform int gl!$um)amles;
2..1 Compatibility +rofile State
,hese varia:les are -resent onl$ in the co-ati:ilit$ -ro%ile.
//
// comatibilit/ rofile onl/
//
uniform mat; gl!Godel'iewGatrix;
uniform mat; gl!Hro:ectionGatrix;
uniform mat; gl!Godel'iewHro:ectionGatrix;
uniform mat; gl!+extureGatrixIgl!Gax+extureLoordsJ;
//
// comatibilit/ rofile onl/
//
uniform mat> gl!$ormalGatrix; // transose of the inverse of the
// uer 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+extureLoordsJ;
uniform mat; gl!Godel'iewGatrix+ransose;
uniform mat; gl!Hro:ectionGatrix+ransose;
uniform mat; gl!Godel'iewHro:ectionGatrix+ransose;
uniform mat; gl!+extureGatrix+ransoseIgl!Gax+extureLoordsJ;
uniform mat; gl!Godel'iewGatrix#nverse+ransose;
uniform mat; gl!Hro:ectionGatrix#nverse+ransose;
11"
7 Bui%t+in #aria$%e
uniform mat; gl!Godel'iewHro:ectionGatrix#nverse+ransose;
uniform mat; gl!+extureGatrix#nverse+ransoseIgl!Gax+extureLoordsJ;
//
// comatibilit/ rofile onl/
//
uniform float gl!$ormal)cale;
//
// comatibilit/ rofile onl/
//
uniform vec; gl!LliHlaneIgl!GaxLliHlanesJ;
//
// comatibilit/ rofile onl/
//
struct gl!HointHarameters 4
float si.e;
float si.eGin;
float si.eGax;
float fade+hreshold)i.e;
float distanceLonstantDttenuation;
float distance"inearDttenuation;
float distanceWuadraticDttenuation;
8;

uniform gl!HointHarameters gl!Hoint;
//
// comatibilit/ rofile onl/
//
struct gl!GaterialHarameters 4
vec; emission; // %cm
vec; ambient; // Dcm
vec; diffuse; // ,cm
vec; secular; // )cm
float shininess; // )rm
8;
uniform gl!GaterialHarameters gl!&rontGaterial;
uniform gl!GaterialHarameters gl!9ac=Gaterial;
111
7 Bui%t+in #aria$%e
//
// comatibilit/ rofile onl/
//
struct gl!"ight)ourceHarameters 4
vec; ambient; // Dcli
vec; diffuse; // ,cli
vec; secular; // )cli
vec; osition; // Hli
vec; half'ector; // ,erived1 Oi
vec> sot,irection; // )dli
float sot%xonent; // )rli
float sotLutoff; // Lrli
// (range1 I?5?7A?5?J7 0F?5?)
float sotLosLutoff; // ,erived1 cos(Lrli)
// (range1 I05?7?5?J7205?)
float constantDttenuation; // S?
float linearDttenuation; // S0
float quadraticDttenuation;// S<
8;
uniform gl!"ight)ourceHarameters gl!"ight)ourceIgl!Gax"ightsJ;
struct gl!"ightGodelHarameters 4
vec; ambient; // Dcs
8;
uniform gl!"ightGodelHarameters gl!"ightGodel;
//
// comatibilit/ rofile onl/
//
// ,erived state from roducts of light and material5
//
struct gl!"ightGodelHroducts 4
vec; sceneLolor; // ,erived5 %cm K Dcm P Dcs
8;
uniform gl!"ightGodelHroducts gl!&ront"ightGodelHroduct;
uniform gl!"ightGodelHroducts gl!9ac="ightGodelHroduct;
struct gl!"ightHroducts 4
vec; ambient; // Dcm P Dcli
vec; diffuse; // ,cm P ,cli
vec; secular; // )cm P )cli
8;
uniform gl!"ightHroducts gl!&ront"ightHroductIgl!Gax"ightsJ;
uniform gl!"ightHroducts gl!9ac="ightHroductIgl!Gax"ightsJ;
112
7 Bui%t+in #aria$%e
//
// comatibilit/ rofile onl/
//
uniform vec; gl!+exture%nvLolorIgl!Gax+exture@nitsJ;
uniform vec; gl!%/eHlane)Igl!Gax+extureLoordsJ;
uniform vec; gl!%/eHlane+Igl!Gax+extureLoordsJ;
uniform vec; gl!%/eHlane(Igl!Gax+extureLoordsJ;
uniform vec; gl!%/eHlaneWIgl!Gax+extureLoordsJ;
uniform vec; gl!*b:ectHlane)Igl!Gax+extureLoordsJ;
uniform vec; gl!*b:ectHlane+Igl!Gax+extureLoordsJ;
uniform vec; gl!*b:ectHlane(Igl!Gax+extureLoordsJ;
uniform vec; gl!*b:ectHlaneWIgl!Gax+extureLoordsJ;
//
// comatibilit/ rofile onl/
//
struct gl!&ogHarameters 4
vec; color;
float densit/;
float start;
float end;
float scale; // ,erived1 05? / (end 2 start)
8;

uniform gl!&ogHarameters gl!&og;
113
3 -uilt6in ,unctions
,he )-en/0 #hading 0anguage de%ines an assortent o% :uilt9in convenience %unctions %or scalar and
vector o-erations. ;an$ o% these :uilt9in %unctions can :e used in ore than one t$-e o% shader, :ut soe
are intended to -rovide a direct a--ing to hardware and so are availa:le onl$ %or a s-eci%ic t$-e o%
shader.
,he :uilt9in %unctions :asicall$ %all into three categories:
,he$ e1-ose soe necessar$ hardware %unctionalit$ in a convenient wa$ such as accessing a te1ture
a-. ,here is no wa$ in the language %or these %unctions to :e eulated :$ a shader.
,he$ re-resent a trivial o-eration Bcla-, i1, etc.C that is ver$ si-le %or the user to write, :ut the$
are ver$ coon and a$ have direct hardware su--ort. It is a ver$ hard -ro:le %or the co-iler to
a- e1-ressions to co-le1 asse:ler instructions.
,he$ re-resent an o-eration gra-hics hardware is likel$ to accelerate at soe -oint. ,he trigonoetr$
%unctions %all into this categor$.
;an$ o% the %unctions are siilar to the sae naed ones in coon " li:raries, :ut the$ su--ort vector
in-ut as well as the ore traditional scalar in-ut.
A--lications should :e encouraged to use the :uilt9in %unctions rather than do the e<uivalent co-utations
in their own shader code since the :uilt9in %unctions are assued to :e o-tial Be.g., -erha-s su--orted
directl$ in hardwareC.
7ser code can re-lace :uilt9in %unctions with their own i% the$ choose, :$ si-l$ re9declaring and de%ining
the sae nae and arguent list. Because :uilt9in %unctions are in a ore outer sco-e than user :uilt9in
%unctions, doing this will hide all :uilt9in %unctions with the sae nae as the re9declared %unction.
@hen the :uilt9in %unctions are s-eci%ied :elow, where the in-ut arguents Band corres-onding out-utC
can :e float, vec", vec', or vecI, gen$*"e is used as the arguent. @here the in-ut arguents Band
corres-onding out-utC can :e int, ivec", ivec', or ivecI, gen+$*"e is used as the arguent. @here the
in-ut arguents Band corres-onding out-utC can :e uint, uvec", uvec', or uvecI, genE$*"e is used as the
arguent. @here the in-ut arguents Bor corres-onding out-utC can :e bool, bvec", bvec', or bvecI,
gen<$*"e is used as the arguent. @here the in-ut arguents Band corres-onding out-utC can :e double,
dvec", dvec', dvecI, genD$*"e is used as the arguent. 4or an$ s-eci%ic use o% a %unction, the actual
t$-es su:stituted %or gen$*"e, gen+$*"e, genE$*"e) or gen<$*"e have to have the sae nu:er o%
co-onents %or all arguents and %or the return t$-e. #iilarl$, mat is used %or an$ atri1 :asic t$-e
with single9-recision co-onents and dmat is used %or an$ atri1 :asic t$-e with dou:le9-recision
co-onents.
114
8 Bui%t+in ,unction
3.1 .ngle and Trigonometry ,unctions
4unction -araeters s-eci%ied as angle are assued 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.
,hese all o-erate co-onent9wise. ,he descri-tion is -er co-onent.
Synta* )escription
gen,$-e radians Bgen,$-e degreesC
"onverts degrees to radians, i.e.,

15'
degrees
gen,$-e degrees Bgen,$-e radiansC
"onverts radians to degrees, i.e.,
15'

radians
gen,$-e sin Bgen,$-e angleC ,he standard trigonoetric sine %unction.
gen,$-e cos Bgen,$-e angleC ,he standard trigonoetric cosine %unction.
gen,$-e tan Bgen,$-e angleC ,he standard trigonoetric tangent.
gen,$-e asin Bgen,$-e #C Arc sine. Returns an angle whose sine is #. ,he range
o% values returned :$ this %unction is
[

!
)

!
]
Results are unde%ined i% #1.
gen,$-e acos Bgen,$-e #C Arc cosine. Returns an angle whose cosine is #. ,he
range o% values returned :$ this %unction is [', \.
Results are unde%ined i% #1.
gen,$-e atan Bgen,$-e *, gen,$-e #C Arc tangent. Returns an angle whose tangent is *D#. ,he
signs o% # and * are used to deterine what <uadrant the
angle is in. ,he range o% values returned :$ this
%unction is [) ] . Results are unde%ined i% # and
* are :oth '.
gen,$-e atan Bgen,$-e *_over_#C Arc tangent. Returns an angle whose tangent is
*_over_#. ,he range o% values returned :$ this %unction
is
[

!
)

!
]
.
115
8 Bui%t+in ,unction
Synta* )escription
gen,$-e sinh Bgen,$-e #C Returns the h$-er:olic sine %unction
e
#
e
#
!
gen,$-e cosh Bgen,$-e #C Returns the h$-er:olic cosine %unction
e
#
e
#
!
gen,$-e tanh Bgen,$-e #C Returns the h$-er:olic tangent %unction
sinh #
cosh #
gen,$-e asinh Bgen,$-e #C Arc h$-er:olic sineN returns the inverse o% sinh.
gen,$-e acosh Bgen,$-e #C Arc h$-er:olic cosineN returns the non9negative inverse
o% cosh. Results are unde%ined i% # R 1.
gen,$-e atanh Bgen,$-e #C Arc h$-er:olic tangentN returns the inverse o% tanh.
Results are unde%ined i% #1.
116
8 Bui%t+in ,unction
3.2 %*ponential ,unctions
,hese all o-erate co-onent9wise. ,he descri-tion is -er co-onent.
Synta* )escription
gen,$-e ow Bgen,$-e #, gen,$-e *C Returns # raised to the * -ower, i.e., #
*
Results are unde%ined i% # F 0.
Results are unde%ined i% # > 0 and * F> 0.
gen,$-e e# Bgen,$-e #C Returns the natural e1-onentiation o% #, i.e., e
#
.
gen,$-e log Bgen,$-e #C Returns the natural logarith o% #) i.e., returns the value
* which satis%ies the e<uation # T e
*
.
Results are unde%ined i% # F> 0.
gen,$-e e#" Bgen,$-e #C Returns ! raised to the # -ower, i.e., !
#
gen,$-e log" Bgen,$-e #C Returns the :ase ! logarith o% #) i.e., returns the value
* which satis%ies the e<uation #=!
*
Results are unde%ined i% # F> 0.
gen,$-e sNrt Bgen,$-e #C
genD,$-e sNrt BgenD,$-e #C
Returns
# .
Results are unde%ined i% # F 0.
gen,$-e inversesNrt Bgen,$-e #C
genD,$-e inversesNrt BgenD,$-e #C
Returns
1
#
.
Results are unde%ined i% # F> 0.
117
8 Bui%t+in ,unction
3." Common ,unctions
,hese all o-erate co-onent9wise. ,he descri-tion is -er co-onent.
Synta* )escription
gen,$-e abs Bgen,$-e #C
genI,$-e abs BgenI,$-e #C
genD,$-e abs BgenD,$-e #C
Returns # i% # ST ', otherwise it returns ]#.
gen,$-e sign Bgen,$-e #C
genI,$-e sign BgenI,$-e #C
genD,$-e sign BgenD,$-e #C
Returns 1.' i% # S ', '.' i% # T ', or ]1.' i% # R '.
gen,$-e floor Bgen,$-e #C
genD,$-e floor BgenD,$-e #C
Returns a value e<ual to the nearest integer that is less
than or e<ual to #.
gen,$-e trunc Bgen,$-e #C
genD,$-e trunc BgenD,$-e #C
Returns a value e<ual to the nearest integer to # whose
a:solute value is not larger than the a:solute value o% #.
gen,$-e round Bgen,$-e #C
genD,$-e round BgenD,$-e #C
Returns a value e<ual to the nearest integer to #. ,he
%raction '.+ will round in a direction chosen :$ the
i-leentation, -resua:l$ the direction that is %astest.
,his includes the -ossi:ilit$ that roundB#C returns the
sae value as roundOvenB#C %or all values o% #.
gen,$-e roundOven Bgen,$-e #C
genD,$-e roundOven BgenD,$-e #C
Returns a value e<ual to the nearest integer to #. A
%ractional -art o% '.+ will round toward the nearest even
integer. BBoth (.+ and &.+ %or 1 will return &.'.C
gen,$-e ceil Bgen,$-e #C
genD,$-e ceil BgenD,$-e #C
Returns a value e<ual to the nearest integer that is
greater than or e<ual to #.
gen,$-e fract Bgen,$-e #C
genD,$-e fract BgenD,$-e #C
Returns # ] floor B#C.
gen,$-e mod Bgen,$-e #, %loat *C
gen,$-e mod Bgen,$-e #, gen,$-e *C
genD,$-e mod BgenD,$-e #, dou:le *C
genD,$-e mod BgenD,$-e #, genD,$-e *C
;odulus. Returns # ] * floor B#H*C.
118
8 Bui%t+in ,unction
Synta* )escription
gen,$-e modf Bgen,$-e #, out gen,$-e iC
genD,$-e modf BgenD,$-e #,
out genD,$-e iC
Returns the %ractional -art o% # and sets i to the integer
-art Bas a whole nu:er %loating -oint valueC. Both the
return value and the out-ut -araeter will have the sae
sign as #.
gen,$-e min Bgen,$-e #, gen,$-e *C
gen,$-e min Bgen,$-e #, %loat *C
genD,$-e min BgenD,$-e #, genD,$-e *C
genD,$-e min BgenD,$-e #, dou:le *C
genI,$-e min BgenI,$-e #, genI,$-e *C
genI,$-e min BgenI,$-e #, int *C
gen7,$-e min Bgen7,$-e #, gen7,$-e *C
gen7,$-e min Bgen7,$-e #, uint *C
Returns * i% * R #, otherwise it returns #.
gen,$-e ma# Bgen,$-e #, gen,$-e *C
gen,$-e ma# Bgen,$-e #, %loat *C
genD,$-e ma# BgenD,$-e #, genD,$-e *C
genD,$-e ma# BgenD,$-e #, dou:le *C
genI,$-e ma# BgenI,$-e #, genI,$-e *C
genI,$-e ma# BgenI,$-e #, int *C
gen7,$-e ma# Bgen7,$-e #, gen7,$-e *C
gen7,$-e ma# Bgen7,$-e #, uint *C
Returns * i% # R *, otherwise it returns #.
11!
8 Bui%t+in ,unction
Synta* )escription
gen,$-e clam Bgen,$-e #,
gen,$-e minVal,
gen,$-e ma#ValC
gen,$-e clam Bgen,$-e #,
%loat minVal,
%loat ma#ValC
genD,$-e clam BgenD,$-e #,
genD,$-e minVal,
genD,$-e ma#ValC
genD,$-e clam BgenD,$-e #,
dou:le minVal,
dou:le ma#ValC
genI,$-e clam BgenI,$-e #,
genI,$-e minVal,
genI,$-e ma#ValC
genI,$-e clam BgenI,$-e #,
int minVal,
int ma#ValC
gen7,$-e clam Bgen7,$-e #,
gen7,$-e minVal,
gen7,$-e ma#ValC
gen7,$-e clam Bgen7,$-e #,
uint minVal,
uint ma#ValC
Returns min Bma# B#, minValC, ma#ValC.
Results are unde%ined i% minVal S ma#Val.
gen,$-e mi# Bgen,$-e #,
gen,$-e *,
gen,$-e aC
gen,$-e mi# Bgen,$-e #,
gen,$-e *,
%loat aC
genD,$-e mi# BgenD,$-e #,
genD,$-e *,
genD,$-e aC
genD,$-e mi# BgenD,$-e #,
genD,$-e *,
dou:le aC
Returns the linear :lend o% # and *) i.e.,
#1a*a
12"
8 Bui%t+in ,unction
Synta* )escription
gen,$-e mi# Bgen,$-e #,
gen,$-e $,
genB,$-e aC
genD,$-e mi# BgenD,$-e #,
genD,$-e $,
genB,$-e aC
#elects which vector each returned co-onent coes
%ro. 4or a co-onent o% a that is false, the
corres-onding co-onent o% # is returned. 4or a
co-onent o% a that is true, the corres-onding
co-onent o% * is returned. "o-onents o% # and * that
are not selected are allowed to :e invalid %loating -oint
values and will have no e%%ect on the results. ,hus, this
-rovides di%%erent %unctionalit$ than, %or e1a-le,
gen,$-e mi#Bgen,$-e #, gen,$-e *, gen,$-eBaCC
where a is a Boolean vector.
gen,$-e ste Bgen,$-e edge, gen,$-e #C
gen,$-e ste B%loat edge, gen,$-e #C
genD,$-e ste BgenD,$-e edge,
genD,$-e #C
genD,$-e ste Bdou:le edge, genD,$-e #C
Returns '.' i% # R edge, otherwise it returns 1.'.
gen,$-e smoothste Bgen,$-e edge0,
gen,$-e edge1,
gen,$-e #C
gen,$-e smoothste B%loat edge0,
%loat edge1,
gen,$-e #C
genD,$-e smoothste BgenD,$-e edge0,
genD,$-e edge1,
genD,$-e #C
genD,$-e smoothste Bdou:le edge0,
dou:le edge1,
genD,$-e #C
Returns '.' i% # RT edge0 and 1.' i% # ST edge1 and
-er%ors sooth *erite inter-olation :etween ' and 1
when edge0 R # R edge1. ,his is use%ul in cases where
$ou would want a threshold %unction with a sooth
transition. ,his is e<uivalent to:
gen,$-e tN
t T cla- BB1 ] edge'C H Bedge1 ] edge'C, ', 1CN
return t K t K B( ] ! K tCN
BAnd siilarl$ %or dou:les.C
Results are undeined i edge0 G> edge1.
genB,$-e isnan Bgen,$-e #C
genB,$-e isnan BgenD,$-e #C
Returns true i% # holds a 8a8. Returns false otherwise.
Alwa$s returns false i% 8a8s are not i-leented.
genB,$-e isinf Bgen,$-e #C
genB,$-e isinf BgenD,$-e #C
Returns true i% # holds a -ositive in%init$ or negative
in%init$. Returns false otherwise.
genI,$-e floatKitsGoInt Bgen,$-e valueC
gen7,$-e floatKitsGoUint Bgen,$-e valueC
Returns a signed or unsigned integer value re-resenting
the encoding o% a %loating9-oint value. ,he %loating9
-oint valueDs :it9level re-resentation is -reserved.
121
8 Bui%t+in ,unction
Synta* )escription
gen,$-e intKitsGo&loat BgenI,$-e valueC
gen,$-e uintKitsGo&loat Bgen7,$-e valueC
Returns a %loating9-oint value corres-onding to a signed
or unsigned integer encoding o% a %loating9-oint value.
I% a 8a8 is -assed in, it will not signal, and the resulting
%loating -oint value is uns-eci%ied. I% an In% is -assed in,
the resulting %loating9-oint value is the corres-onding
In%.
gen,$-e fma Bgen,$-e a, gen,$-e b,
gen,$-e cC
genD,$-e fma BgenD,$-e a, genD,$-e b,
genD,$-e cC
"o-utes and returns a=b 9 c.
In uses where the return value is eventuall$ consued :$
a varia:le declared as recise:
fmaBC is considered a single o-eration, whereas the
e1-ression GaKb F cI consued :$ a varia:le
declared recise is considered two o-erations.
,he -recision o% fmaBC can di%%er %ro the -recision
o% the the e1-ression GaKb F cI.
fmaBC will :e co-uted with the sae -recision as
an$ other fmaBC consued :$ a recise varia:le,
giving invariant results %or the sae in-ut values o%
a, b, and c.
)therwise, in the a:sence o% recise consu-tion, there
are no s-ecial constraints on the nu:er o% o-erations or
di%%erence in -recision :etween fmaBC and the e1-ression
GaKb F cI.
gen,$-e fre# Bgen,$-e #,
out genI,$-e e#"C
genD,$-e fre# BgenD,$-e #,
out genI,$-e e#"C
#-lits # into a %loating9-oint signi%icand in the range
['.+, 1.'C and an integral e1-onent o% two, such that:
#=signiicand!
e#"onent
,he signi%icand is returned :$ the %unction and the
e1-onent is returned in the -araeter e#". 4or a
%loating9-oint value o% =ero, the signi%icant and e1-onent
are :oth =ero. 4or a %loating9-oint value that is an
in%init$ or is not a nu:er, the results are unde%ined.
gen,$-e lde# Bgen,$-e #,
in genI,$-e e#"C
genD,$-e lde# BgenD,$-e #,
in genI,$-e e#"C
Builds a %loating9-oint nu:er %ro # and the
corres-onding integral e1-onent o% two in e#", returning:
signiicand!
e#"onent
I% this -roduct is too large to :e re-resented in the
%loating9-oint t$-e, the result is unde%ined.
122
8 Bui%t+in ,unction
123
8 Bui%t+in ,unction
3. ,loating6+oint +ac0 and 4npac0 ,unctions
,hese %unctions do not o-erate co-onent9wise, rather as descri:ed in each case.
Synta* )escription
uint ackUnorm"#1$ Bvec! vC
uint ack%norm"#1$ Bvec! vC
uint ackUnormI#\ Bvec& vC
uint ack%normI#\ Bvec& vC
4irst, converts each co-onent o% the norali=ed
%loating9-oint value v into 59 or 1.9:it integer values.
,hen, the results are -acked into the returned (!9:it
unsigned integer.
,he conversion %or co-onent c o% v to %i1ed -oint is
done as %ollows:
ackUnorm"#1$: roundBcla-Bc, ', F1C K .++(+.'C
ack%norm"#1$B roundBcla-Bc, 91, F1C K (!3.3.'C
ackUnormI#\: roundBcla-Bc, ', F1C K !++.'C
ack%normI#\: roundBcla-Bc, 91, F1C K 1!3.'C
,he %irst co-onent o% the vector will :e written to the
least signi%icant :its o% the out-utN the last co-onent
will :e written to the ost signi%icant :its.
vec! unackUnorm"#1$ Buint "C
vec! unack%norm"#1$ Buint "C
vec& unackUnormI#\ Buint "C
vec& unack%normI#\ Buint "C
4irst, un-acks a single (!9:it unsigned integer " into a
-air o% 1.9:it unsigned integers, %our 59:it unsigned
integers, or %our 59:it signed integers. ,hen, each
co-onent is converted to a norali=ed %loating9-oint
value to generate the returned two9 or %our9co-onent
vector.
,he conversion %or un-acked %i1ed9-oint value to
%loating -oint is done as %ollows:
unackUnorm"#1$B H .++(+.'
unack%norm"#1$B cla-B H (!3.3.', 91, F1C
unackUnormI#\B H !++.'
unack%normI#\B cla-B H 1!3.', 91, F1C
,he %irst co-onent o% the returned vector will :e
e1tracted %ro the least signi%icant :its o% the in-utN the
last co-onent will :e e1tracted %ro the ost
signi%icant :its.
124
8 Bui%t+in ,unction
Synta* )escription
dou:le ackDouble"#'" Buvec! vC Returns a dou:le9-recision value o:tained :$ -acking
the co-onents o% v into a .&9:it value. I% an IEEE 3+&
In% or 8a8 is created, it will not signal, and the resulting
%loating -oint value is uns-eci%ied. )therwise, the :it9
level re-resentation o% v is -reserved. ,he %irst vector
co-onent s-eci%ies the (! least signi%icant :itsN the
second co-onent s-eci%ies the (! ost signi%icant :its.
uvec! unackDouble"#'" Bdou:le vC Returns a two9co-onent unsigned integer vector
re-resentation o% v. ,he :it9level re-resentation o% v is
-reserved. ,he %irst co-onent o% the vector contains
the (! least signi%icant :its o% the dou:leN the second
co-onent consists the (! ost signi%icant :its.
uint ack!alf"#1$ Bvec! vC Returns an unsigned integer o:tained :$ converting the
co-onents o% a two9co-onent %loating9-oint vector to
the 1.9:it %loating9-oint re-resentation %ound in the
)-en/0 #-eci%ication, and then -acking these two 1.9
:it integers into a (!9:it unsigned integer.
,he %irst vector co-onent s-eci%ies the 1. least9
signi%icant :its o% the resultN the second co-onent
s-eci%ies the 1. ost9signi%icant :its.
vec! unack!alf"#1$ Buint vC Returns a two9co-onent %loating9-oint vector with
co-onents o:tained :$ un-acking a (!9:it unsigned
integer into a -air o% 1.9:it values, inter-reting those
values as 1.9:it %loating9-oint nu:ers according to the
)-en/0 #-eci%ication, and converting the to (!9:it
%loating9-oint values.
,he %irst co-onent o% the vector is o:tained %ro the
1. least9signi%icant :its o% vN the second co-onent is
o:tained %ro the 1. ost9signi%icant :its o% v.
125
8 Bui%t+in ,unction
3.' Geometric ,unctions
,hese o-erate on vectors as vectors, not co-onent9wise.
Synta* )escription
%loat length Bgen,$-e #C
dou:le length BgenD,$-e #C
Returns the length o% vector #, i.e.,
#[ ']
!
#[ 1]
!
...
%loat distance Bgen,$-e "0, gen,$-e "1C
dou:le distance BgenD,$-e "0,
genD,$-e "1C
Returns the distance :etween "0 and "1, i.e.,
length B"0 : "1C
%loat dot Bgen,$-e #, gen,$-e *C
dou:le dot BgenD,$-e #, genD,$-e *C
Returns the dot -roduct o% # and *, i.e.,
#[ ']* [']# [1]*[1]...
vec( cross Bvec( #, vec( *C
dvec( cross Bdvec( #, dvec( *C
Returns the cross -roduct o% 1 and $, i.e.,
[
# [1]*[ !]* [1]# [!]
#[ !]*[ ']* [ !]#[ ']
#[']* [1]* [']# [1]
]
gen,$-e normali+e Bgen,$-e #C
genD,$-e normali+e BgenD,$-e #C
Returns a vector in the sae direction as # :ut with a
length o% 1.
co-ati:ilit$ -ro%ile onl$
vec& ftransform BC
Availa:le onl$ when using the co-ati:ilit$ -ro%ile. 4or
core )-en/0, use invariant.
4or verte1 shaders onl$. ,his %unction will ensure that
the incoing verte1 value will :e trans%ored in a wa$
that -roduces e1actl$ the sae result as would :e
-roduced :$ )-en/0Ms %i1ed %unctionalit$ trans%or. It
is intended to :e used to co-ute gl_!osition, e.g.,
gl?2osition T ftransformBC
,his %unction should :e used, %or e1a-le, when an
a--lication is rendering the sae geoetr$ in se-arate
-asses, and one -ass uses the %i1ed %unctionalit$ -ath to
render and another -ass uses -rograa:le shaders.
126
8 Bui%t+in ,unction
Synta* )escription
gen,$-e faceforward Bgen,$-e ,,
gen,$-e +,
gen,$-e ,reC
genD,$-e faceforward BgenD,$-e ,,
genD,$-e +,
genD,$-e ,reC
I% dotB,re, +C R ' return ,) otherwise return ],.
gen,$-e reflect Bgen,$-e +, gen,$-e ,C
genD,$-e reflect BgenD,$-e +,
genD,$-e ,C
4or the incident vector + and sur%ace orientation ,,
returns the re%lection direction:
+ ] ! dotB,, +C ,
, ust alread$ :e norali=ed in order to achieve the
desired result.
gen,$-e refract Bgen,$-e +, gen,$-e ,,
%loat etaC
genD,$-e refract BgenD,$-e +,
genD,$-e ,,
%loat etaC
4or the incident vector + and sur%ace noral ,, and the
ratio o% indices o% re%raction eta) return the re%raction
vector. ,he result is co-uted :$
k T 1.' 9 eta K eta K B1.' 9 dotB,, +C K dotB,, +CC
i% Bk R '.'C
return gen,$-eB'.'C HH or genD,$-eB'.'C
else
return eta K + 9 Beta K dotB,, +C F sNrtBkCC K ,
,he in-ut -araeters %or the incident vector + and the
sur%ace noral , ust alread$ :e norali=ed to get the
desired results.
127
8 Bui%t+in ,unction
3.( 7atri* ,unctions
4or each o% the %ollowing :uilt9in atri1 %unctions, there is :oth a single9-recision %loating -oint version,
where all arguents and return values are single -recision, and a dou:le9-recision %loating version, where
all arguents and return values are dou:le -recision. )nl$ the single9-recision %loating -oint version is
shown.
Synta* )escription
at matri#ComLult Bat #, at *C ;ulti-l$ atri1 # :$ atri1 * co-onent9wise, i.e.,
result[i\[L\ is the scalar -roduct o% #[i\[L\ and *[i\[L\.
8ote: to get linear alge:raic atri1 ulti-lication, use
the ulti-l$ o-erator B2C.
at! outerSroduct Bvec! c, vec! rC
at( outerSroduct Bvec( c, vec( rC
at& outerSroduct Bvec& c, vec& rC
at!1( outerSroduct Bvec( c, vec! rC
at(1! outerSroduct Bvec! c, vec( rC
at!1& outerSroduct Bvec& c, vec! rC
at&1! outerSroduct Bvec! c, vec& rC
at(1& outerSroduct Bvec& c, vec( rC
at&1( outerSroduct Bvec( c, vec& rC
,reats the %irst -araeter c as a colun vector Batri1
with one colunC and the second -araeter r as a row
vector Batri1 with one rowC and does a linear alge:raic
atri1 ulti-l$ c K r, $ielding a atri1 whose nu:er o%
rows is the nu:er o% co-onents in c and whose
nu:er o% coluns is the nu:er o% co-onents in r.
at! transose Bat! mC
at( transose Bat( mC
at& transose Bat& mC
at!1( transose Bat(1! mC
at(1! transose Bat!1( mC
at!1& transose Bat&1! mC
at&1! transose Bat!1& mC
at(1& transose Bat&1( mC
at&1( transose Bat(1& mC
Returns a atri1 that is the trans-ose o% m. ,he in-ut
atri1 m is not odi%ied.
%loat determinant Bat! mC
%loat determinant Bat( mC
%loat determinant Bat& mC
Returns the deterinant o% m.
128
8 Bui%t+in ,unction
Synta* )escription
at! inverse Bat! mC
at( inverse Bat( mC
at& inverse Bat& mC
Returns a atri1 that is the inverse o% m. ,he in-ut
atri1 m is not odi%ied. ,he values in the returned
atri1 are unde%ined i% m is singular or -oorl$9
conditioned Bnearl$ singularC.
12!
8 Bui%t+in ,unction
3.2 Vector ;elational ,unctions
Relational and e<ualit$ o-erators B4- 4@- 5- 5@- @@- A@C are de%ined to o-erate on scalars and -roduce
scalar Boolean results. 4or vector results, use the %ollowing :uilt9in %unctions. Below, the %ollowing
-laceholders are used %or the listed s-eci%ic t$-es:
Slaceholder %ecific Gyes 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 in-ut and return vectors %or an$ -articular call ust atch.
Synta* )escription
:vec lessGhan Bvec 1, vec $C
:vec lessGhan Bivec 1, ivec $C
:vec lessGhan Buvec 1, uvec $C
Returns the co-onent9wise co-are o% # R *.
:vec lessGhanONual Bvec 1, vec $C
:vec lessGhanONual Bivec 1, ivec $C
:vec lessGhanONual Buvec 1, uvec $C
Returns the co-onent9wise co-are o% # RT *.
:vec greaterGhan Bvec 1, vec $C
:vec greaterGhan Bivec 1, ivec $C
:vec greaterGhan Buvec 1, uvec $C
Returns the co-onent9wise co-are o% # S *.
:vec greaterGhanONual Bvec 1, vec $C
:vec greaterGhanONual Bivec 1, ivec $C
:vec greaterGhanONual Buvec 1, uvec $C
Returns the co-onent9wise co-are o% # ST *.
:vec eNual Bvec 1, vec $C
:vec eNual Bivec 1, ivec $C
:vec eNual Buvec 1, uvec $C
:vec eNual B:vec 1, :vec $C
:vec notONual Bvec 1, vec $C
:vec notONual Bivec 1, ivec $C
:vec notONual Buvec 1, uvec $C
:vec notONual B:vec 1, :vec $C
Returns the co-onent9wise co-are o% # TT *.
Returns the co-onent9wise co-are o% # PT *.
:ool any B:vec 1C Returns true i% an$ co-onent o% # is true.
13"
8 Bui%t+in ,unction
Synta* )escription
:ool all B:vec 1C Returns true onl$ i% all co-onents o% # are true.
:vec not B:vec 1C Returns the co-onent9wise logical co-leent o% #.
131
8 Bui%t+in ,unction
3.3 Integer ,unctions
,hese all o-erate co-onent9wise. ,he descri-tion is -er co-onent. ,he notation [a, b\ eans the set
o% :its %ro :it9nu:er a through :it9nu:er b, inclusive. ,he lowest9order :it is :it '. GBit nu:erI
will alwa$s re%er to counting u- %ro the lowest9order :it as :it '.
Synta* )escription
gen7,$-e uaddCarry Bgen7,$-e #,
gen7,$-e *,
out gen7,$-e carr*C
Adds (!9:it unsigned integer # and *, returning the su
odulo !
(!
. ,he value carr* is set to ' i% the su was
less than !
(!
, or to 1 otherwise.
gen7,$-e usubKorrow Bgen7,$-e #,
gen7,$-e *,
out gen7,$-e borro/C
#u:tracts the (!9:it unsigned integer * %ro #, returning
the di%%erence i% non9negative, or !
(!
-lus the di%%erence
otherwise. ,he value borro/ is set to ' i% # G> *, or to
1 otherwise.
void umulO#tended Bgen7,$-e #,
gen7,$-e *,
out gen7,$-e msb,
out gen7,$-e lsbC
void imulO#tended BgenI,$-e #,
genI,$-e *,
out genI,$-e msb,
out genI,$-e lsbC
;ulti-lies (!9:it integers # and *, -roducing a .&9:it
result. ,he (! least9signi%icant :its are returned in lsb.
,he (! ost9signi%icant :its are returned in msb.
genI,$-e bitfieldO#tract BgenI,$-e value,
int oset, int bitsC
gen7,$-e bitfieldO#tract Bgen7,$-e value,
int oset, int bitsC
E1tracts :its [oset, oset F bits - 1\ %ro value,
returning the in the least signi%icant :its o% the result.
4or unsigned data t$-es, the ost signi%icant :its o% the
result will :e set to =ero. 4or signed data t$-es, the
ost signi%icant :its will :e set to the value o% :it oset
F bits : 1.
I% bits is =ero, the result will :e =ero. ,he result will :e
unde%ined i% oset or bits is negative, or i% the su o%
oset and bits is greater than the nu:er o% :its used
to store the o-erand.
132
8 Bui%t+in ,unction
Synta* )escription
genI,$-e bitfieldInsert BgenI,$-e base,
genI,$-e insert,
int oset, int bitsC
gen7,$-e bitfieldInsert Bgen7,$-e base,
gen7,$-e insert,
int oset, int bitsC
Returns the insertion the bits least9signi%icant :its o%
insert into base.
,he result will have :its [oset, oset F bits - 1\ taken
%ro :its [', bits : 1\ o% insert, and all other :its taken
directl$ %ro the corres-onding :its o% base. I% bits is
=ero, the result will si-l$ :e base. ,he result will :e
unde%ined i% oset or bits is negative, or i% the su o%
oset and bits is greater than the nu:er o% :its used to
store the o-erand.
genI,$-e bitfieldJeverse BgenI,$-e valueC
gen7,$-e bitfieldJeverse Bgen7,$-e valueC
Returns the reversal o% the :its o% value. ,he :it
nu:ered n o% the result will :e taken %ro :it Bbits - 1C
- n o% value, where bits is the total nu:er o% :its used
to re-resent value.
genI,$-e bitCount BgenI,$-e valueC
genI,$-e bitCount Bgen7,$-e valueC
Returns the nu:er o% :its set to 1 in the :inar$
re-resentation o% value.
genI,$-e findH%K BgenI,$-e valueC
genI,$-e findH%K Bgen7,$-e valueC
Returns the :it nu:er o% the least signi%icant :it set to
1 in the :inar$ re-resentation o% value. I% value is =ero,
91will :e returned.
genI,$-e findL%K BgenI,$-e valueC
genI,$-e findL%K Bgen7,$-e valueC
Returns the :it nu:er o% the ost signi%icant :it in the
:inar$ re-resentation o% value.
4or -ositive integers, the result will :e the :it nu:er o%
the ost signi%icant :it set to 1. 4or negative integers,
the result will :e the :it nu:er o% the ost signi%icant
:it set to '. 4or a value o% =ero or negative one, 91 will
:e returned.
133
8 Bui%t+in ,unction
3.8 Te*ture ,unctions
,e1ture looku- %unctions are availa:le in all shading stages. *owever, autoatic level o% detail is
co-uted onl$ %or %ragent shaders. )ther shaders o-erate as though the :ase level o% detail were
co-uted as =ero. ,he %unctions in the ta:le :elow -rovide access to te1tures through sa-lers, as set u-
through the )-en/0 A2I. ,e1ture -ro-erties such as si=e, -i1el %orat, nu:er o% diensions, %iltering
ethod, nu:er o% i-9a- levels, de-th co-arison, and so on are also de%ined :$ )-en/0 A2I calls.
#uch -ro-erties are taken into account as the te1ture is accessed via the :uilt9in %unctions de%ined :elow.
,e1ture data can :e stored :$ the /0 as %loating -oint, unsigned norali=ed integer, unsigned integer or
signed integer data. ,his is deterined :$ the t$-e o% the internal %orat o% the te1ture. ,e1ture looku-s
on unsigned norali=ed integer and %loating -oint data return %loating -oint values in the range [', 1\.
,e1ture looku- %unctions are -rovided that can return their result as %loating -oint, unsigned integer or
signed integer, de-ending on the sa-ler t$-e -assed to the looku- %unction. "are ust :e taken to use
the right sa-ler t$-e %or te1ture access. ,he %ollowing ta:le lists the su--orted co:inations o% sa-ler
t$-es and te1ture internal %orats. Blank entries are unsu--orted. Doing a te1ture looku- will return
unde%ined values %or unsu--orted co:inations.
Internal ,e1ture 4orat
4loating 2oint
#a-ler ,$-es
#igned Integer
#a-ler ,$-es
7nsigned Integer
#a-ler ,$-es
4loating -oint #u--orted
8orali=ed Integer #u--orted
#igned Integer #u--orted
7nsigned Integer #u--orted
I% an integer sa-ler t$-e is used, the result o% a te1ture looku- is an ivecI. I% an unsigned integer sa-ler
t$-e is used, the result o% a te1ture looku- is a uvecI. I% a %loating -oint sa-ler t$-e is used, the result o%
a te1ture looku- is a vecI, where each co-onent is in the range [', 1\.
In the -rotot$-es :elow, the GgI in the return t$-e Ggvec4I is used as a -laceholder %or nothing, GiI, or GuI
aking a return t$-e o% vecI, ivecI, or uvecI. In these cases, the sa-ler arguent t$-e also starts with
GgI, indicating the sae su:stitution done on the return t$-eN it is either a %loating -oint, signed integer, or
unsigned integer sa-ler, atching the :asic t$-e o% the return t$-e, as descri:ed a:ove.
4or shadow %ors Bthe sa-ler -araeter is a shadow9t$-eC, a de-th co-arison looku- on the de-th
te1ture :ound to sam"ler is done as descri:ed in section (.5.1. G,e1ture "o-arison ;odesI o% the
)-en/0 /ra-hics #$ste #-eci%ication. #ee the ta:le :elow %or which co-onent s-eci%ies Dre. ,he
te1ture :ound to sam"ler ust :e a de-th te1ture, or results are unde%ined. I% a non9shadow te1ture call is
ade to a sa-ler that re-resents a de-th te1ture with de-th co-arisons turned on, then results are
unde%ined. I% a shadow te1ture call is ade to a sa-ler that re-resents a de-th te1ture with de-th
co-arisons turned o%%, then results are unde%ined. I% a shadow te1ture call is ade to a sa-ler that does
not re-resent a de-th te1ture, then results are unde%ined.
134
8 Bui%t+in ,unction
In all %unctions :elow, the bias -araeter is o-tional %or %ragent shaders. ,he bias -araeter is not
acce-ted in an$ other shaders. 4or a %ragent shader, i% bias is -resent, it is added to the i-licit level o%
detail -rior to -er%oring the te1ture access o-eration. 8o bias or lod -araeters %or rectangular te1tures,
ulti9sa-le te1tures, or te1ture :u%%ers are su--orted :ecause i-9a-s are not allowed %or these t$-es
o% te1tures.
,he i-licit level o% detail is selected as %ollows: 4or a te1ture that is not i-9a--ed, the te1ture is used
directl$. I% it is i-9a--ed and running in a %ragent shader, the 0)D co-uted :$ the i-leentation
is used to do the te1ture looku-. I% it is i-9a--ed and running on the verte1 shader, then the :ase
te1ture is used.
#oe te1ture %unctions Bnon9GHodI and non9G(radI versionsC a$ re<uire i-licit derivatives. I-licit
derivatives are unde%ined within non9uni%or control %low and %or non9%ragent9shader te1ture %etches.
4or Cube %ors, the direction o% ! is used to select which %ace to do a !9diensional te1ture looku- in, as
descri:ed in section (.5.1' G"u:e ;a- ,e1ture #electionI in the )-en/0 /ra-hics #$ste #-eci%ication.
4or Array %ors, the arra$ la$er used will :e
ma# ',min d 1, loorla*er'.+
where d is the de-th o% the te1ture arra$ and la*er coes %ro the co-onent indicated in the ta:les
:elow.
3.8.1 Te*ture :uery ,unctions
,he te#ture%i+e %unctions <uer$ the diensions o% a s-eci%ic te1ture level %or a sa-ler.
,he te#tureTueryHod %unctions are availa:le onl$ in a %ragent shader. ,he$ take the co-onents o% !
and co-ute the level o% detail in%oration that the te1ture -i-e would use to access that te1ture through a
noral te1ture looku-. ,he level o% detail HI Be<uation (.15 in the )-en/0 /ra-hics #$ste
#-eci%icationC is o:tained a%ter an$ 0)D :ias, :ut -rior to cla-ing to [,EZ,7RE?;I8?0)D,
,EZ,7RE?;AZ?0)D\. ,he i-a- arra$BsC that would :e accessed are also co-uted. I% a single
level o% detail would :e accessed, the level9o%9detail nu:er relative to the :ase level is returned. I%
ulti-le levels o% detail would :e accessed, a %loating9-oint nu:er :etween the two levels is returned,
with the %ractional -art e<ual to the %ractional -art o% the co-uted and cla-ed level o% detail. ,he
algorith used is given :$ the %ollowing -seudo9code:
135
8 Bui%t+in ,unction
float LomuteDccessed"od(float comuted"od)
4
// Llam the comuted "*, according to the texture "*, clams5
if (comuted"od 6 +%T+@(%!G#$!"*,) comuted"od 3 +%T+@(%!G#$!"*,;
if (comuted"od Q +%T+@(%!GDT!"*,) comuted"od 3 +%T+@(%!GDT!"*,;
// Llam the comuted "*, to the range of accessible levels5
if (comuted"od 6 ?5?)
comuted"od 3 ?5?;
if (comuted"od Q (float)
maxDccessible"evel) comuted"od 3 (float) maxDccessible"evel;
// (eturn a value according to the min filter5
if (+%T+@(%!G#$!&#"+%( is "#$%D( or $%D(%)+) 4
return ?5?;
8 else if (+%T+@(%!G#$!&#"+%( is $%D(%)+!G#HGDH!$%D(%)+
or "#$%D(!G#HGDH!$%D(%)+) 4
return ceil(comuted"od K ?5E) 2 05?;
8 else 4
return comuted"od;
8
8
,he value ma#;ccessibleLevel is the level nu:er o% the sallest accessi:le level o% the i-a- arra$
Bthe value 1 in section (.5.6 G,e1ture ;ini%icationI o% the )-en/0 /ra-hics #$ste #-eci%icationC inus
the :ase level.
Synta* )escription
int te#ture%i+e Bgsa-ler1D sam"ler, int lodC
ivec! te#ture%i+e Bgsa-ler!D sam"ler, int lodC
ivec( te#ture%i+e Bgsa-ler(D sam"ler, int lodC
ivec! te#ture%i+e Bgsa-ler"u:e sam"ler, int lodC
int te#ture%i+e Bsa-ler1D#hadow sam"ler, int lodC
ivec! te#ture%i+e Bsa-ler!D#hadow sam"ler, int lodC
ivec! te#ture%i+e Bsa-ler"u:e#hadow sam"ler, int lodC
ivec( te#ture%i+e Bgsa-ler"u:eArra$ sam"ler, int lodC
ivec( te#ture%i+e Bsa-ler"u:eArra$#hadow sam"ler, int lodC
ivec! te#ture%i+e Bgsa-ler!DRect sam"lerC
ivec! te#ture%i+e Bsa-ler!DRect#hadow sam"lerC
ivec! te#ture%i+e Bgsa-ler1DArra$ sam"ler, int lodC
ivec( te#ture%i+e Bgsa-ler!DArra$ sam"ler, int lodC
ivec! te#ture%i+e Bsa-ler1DArra$#hadow sam"ler, int lodC
ivec( te#ture%i+e Bsa-ler!DArra$#hadow sam"ler, int lodC
int te#ture%i+e Bgsa-lerBu%%er sam"lerC
ivec! te#ture%i+e Bgsa-ler!D;# sam"lerC
ivec( te#ture%i+e Bgsa-ler!D;#Arra$ sam"lerC
Returns the diensions o% level
lod Bi% -resentC %or the te1ture
:ound to sam"ler, as descri:ed
in section !.11.3 G#hader
E1ecutionI o% the )-en/0
/ra-hics #$ste #-eci%ication,
under G,e1ture #i=e >uer$I.
,he co-onents in the return
value are %illed in, in order, with
the width, height, de-th o% the
te1ture.
4or the arra$ %ors, the last
co-onent o% the return value is
the nu:er o% la$ers in the
te1ture arra$.
136
8 Bui%t+in ,unction
Synta* )escription
vec! te#tureTueryHodBgsa-ler1D sam"ler, %loat !C
vec! te#tureTueryHodBgsa-ler!D sam"ler, vec! !C
vec! te#tureTueryHodBgsa-ler(D sam"ler, vec( !C
vec! te#tureTueryHodBgsa-ler"u:e sam"ler, vec( !C
vec! te#tureTueryHodBgsa-ler1DArra$ sam"ler, %loat !C
vec! te#tureTueryHodBgsa-ler!DArra$ sam"ler, vec! !C
vec! te#tureTueryHodBgsa-ler"u:eArra$ sam"ler, vec( !C
vec! te#tureTueryHodBsa-ler1D#hadow sam"ler, %loat !C
vec! te#tureTueryHodBsa-ler!D#hadow sam"ler, vec! !C
vec! te#tureTueryHodBsa-ler"u:e#hadow sam"ler, vec( !C
vec! te#tureTueryHodBsa-ler1DArra$#hadow sam"ler, %loat !C
vec! te#tureTueryHodBsa-ler!DArra$#hadow sam"ler, vec! !C
vec! te#tureTueryHodBsa-ler"u:eArra$#hadow sam"ler, vec( !C
Returns the i-a- arra$BsC
that would :e accessed in the #
co-onent o% the return value.
Returns the co-uted level o%
detail relative to the :ase level
in the * co-onent o% the return
value.
I% called on an inco-lete
te1ture, the results are
unde%ined.
3.8.2 Te*el Loo0up ,unctions
Synta* )escription
gvec& te#ture Bgsa-ler1D sam"ler) %loat ! [, %loat bias\ C
gvec& te#ture Bgsa-ler!D sam"ler, vec! ! [, %loat bias\ C
gvec& te#ture Bgsa-ler(D sam"ler, vec( ! [, %loat bias\ C
gvec& te#ture Bgsa-ler"u:e sam"ler, vec( ! [, %loat bias\ C
%loat te#ture Bsa-ler1D#hadow sam"ler) vec( ! [, %loat bias\ C
%loat te#ture Bsa-ler!D#hadow sam"ler) vec( ! [, %loat bias\ C
%loat te#ture Bsa-ler"u:e#hadow sam"ler) vec& ! [, %loat bias\ C
gvec& te#ture Bgsa-ler1DArra$ sam"ler) vec! ! [, %loat bias\ C
gvec& te#ture Bgsa-ler!DArra$ sam"ler, vec( ! [, %loat bias\ C
gvec& te#ture Bgsa-ler"u:eArra$ sam"ler, vec& ! [, %loat bias\ C
%loat te#ture Bsa-ler1DArra$#hadow sam"ler) vec( !
[, %loat bias\ C
%loat te#ture Bsa-ler!DArra$#hadow sam"ler) vec& !C
gvec& te#ture Bgsa-ler!DRect sam"ler, vec! !C
%loat te#ture Bsa-ler!DRect#hadow sam"ler, vec( !C
%loat te#ture Bgsa-ler"u:eArra$#hadow sam"ler, vec& !,
%loat com"areC
7se the te1ture coordinate ! to
do a te1ture looku- in the
te1ture currentl$ :ound to
sam"ler.
4or shadow %ors: @hen
com"are is -resent, it is used as
Dre and the arra$ la$er coes
%ro !./. @hen com"are is not
-resent, the last co-onent o%
! is used as Dre and the arra$
la$er coes %ro the second to
last co-onent o% !. B,he
second co-onent o% ! is
unused %or 1D shadow looku-s.C
4or non9shadow %ors: the arra$
la$er coes %ro the last
co-onent o% !.
137
8 Bui%t+in ,unction
Synta* )escription
gvec& te#tureSroM Bgsa-ler1D sam"ler, vec! ! [, %loat bias\ C
gvec& te#tureSroM Bgsa-ler1D sam"ler, vec& ! [, %loat bias\ C
gvec& te#tureSroM Bgsa-ler!D sam"ler, vec( ! [, %loat bias\ C
gvec& te#tureSroM Bgsa-ler!D sam"ler, vec& ! [, %loat bias\ C
gvec& te#tureSroM Bgsa-ler(D sam"ler, vec& ! [, %loat bias\ C
%loat te#tureSroM Bsa-ler1D#hadow sam"ler) vec& !
[, %loat bias\ C
%loat te#tureSroM Bsa-ler!D#hadow sam"ler) vec& !
[, %loat bias\ C
gvec& te#tureSroM Bgsa-ler!DRect sam"ler, vec( !C
gvec& te#tureSroM Bgsa-ler!DRect sam"ler, vec& !C
%loat te#tureSroM Bsa-ler!DRect#hadow sam"ler, vec& !C
Do a te1ture looku- with
-roLection. ,he te1ture
coordinates consued %ro !,
not including the last co-onent
o% !, are divided :$ the last
co-onent o% !. ,he resulting
(
rd
co-onent o% ! in the
shadow %ors is used as Dre.
A%ter these values are co-uted,
te1ture looku- -roceeds as in
te#ture.
gvec& te#tureHod Bgsa-ler1D sam"ler, %loat !, %loat lodC
gvec& te#tureHod Bgsa-ler!D sam"ler, vec! !, %loat lodC
gvec& te#tureHod Bgsa-ler(D sam"ler, vec( !, %loat lodC
gvec& te#tureHod Bgsa-ler"u:e sam"ler, vec( !, %loat lodC
%loat te#tureHod Bsa-ler1D#hadow sam"ler) vec( !, %loat lodC
%loat te#tureHod Bsa-ler!D#hadow sam"ler) vec( !, %loat lodC
gvec& te#tureHod Bgsa-ler1DArra$ sam"ler, vec! !, %loat lodC
gvec& te#tureHod Bgsa-ler!DArra$ sam"ler, vec( !, %loat lodC
%loat te#tureHod Bsa-ler1DArra$#hadow sam"ler) vec( !,
%loat lodC
gvec& te#tureHod Bgsa-ler"u:eArra$ sam"ler, vec& !, %loat lodC
Do a te1ture looku- as in
te#ture :ut with e1-licit 0)DN
lod s-eci%ies Hbase and sets the
-artial derivatives as %ollows.
B#ee section (.5.11 G,e1ture
;ini%icationI and e<uation (.13
in the )-en/0 /ra-hics #$ste
#-eci%ication.C
u
#
= '
v
#
= '
/
#
= '
u
*
= '
v
*
= '
/
*
= '
138
8 Bui%t+in ,unction
Synta* )escription
gvec& te#ture)ffset Bgsa-ler1D sam"ler) %loat !)
int oset [, %loat bias\ C
gvec& te#ture)ffset Bgsa-ler!D sam"ler, vec! !,
ivec! oset [, %loat bias\ C
gvec& te#ture)ffset Bgsa-ler(D sam"ler, vec( !,
ivec( oset [, %loat bias\ C
gvec& te#ture)ffset Bgsa-ler!DRect sam"ler, vec! !,
ivec! oset C
%loat te#ture)ffset Bsa-ler!DRect#hadow sam"ler, vec( !,
ivec! oset C
%loat te#ture)ffset Bsa-ler1D#hadow sam"ler) vec( !,
int oset [, %loat bias\ C
%loat te#ture)ffset Bsa-ler!D#hadow sam"ler) vec( !,
ivec! oset [, %loat bias\ C
gvec& te#ture)ffset Bgsa-ler1DArra$ sam"ler) vec! !)
int oset [, %loat bias\ C
gvec& te#ture)ffset Bgsa-ler!DArra$ sam"ler, vec( !,
ivec! oset [, %loat bias\ C
%loat te#ture)ffset Bsa-ler1DArra$#hadow sam"ler) vec( !,
int oset [, %loat bias\ C
Do a te1ture looku- as in
te#ture :ut with oset added to
the Bu,v,/C te1el coordinates
:e%ore looking u- each te1el.
,he o%%set value ust :e a
constant e1-ression. A liited
range o% o%%set values are
su--ortedN the iniu and
a1iu o%%set values are
i-leentation9de-endent and
given :$
gl?;in2rogra,e1el)%%set and
gl?;a12rogra,e1el)%%set,
res-ectivel$.
8ote that oset does not a--l$
to the la$er coordinate %or
te1ture arra$s. ,his is e1-lained
in detail in section (.5.11
G,e1ture ;ini%icationI o% the
)-en/0 /ra-hics #$ste
#-eci%ication, where oset is

u
)
v
)
/
. 8ote that te1el
o%%sets are also not su--orted
%or cu:e a-s.
gvec& te#el&etch Bgsa-ler1D sam"ler, int !, int lodC
gvec& te#el&etch Bgsa-ler!D sam"ler, ivec! !, int lodC
gvec& te#el&etch Bgsa-ler(D sam"ler, ivec( !, int lodC
gvec& te#el&etch Bgsa-ler!DRect sam"ler, ivec! !C
gvec& te#el&etch Bgsa-ler1DArra$ sam"ler, ivec! !, int lodC
gvec& te#el&etch Bgsa-ler!DArra$ sam"ler, ivec( !, int lodC
gvec& te#el&etch Bgsa-lerBu%%er sam"ler, int !C
gvec& te#el&etch Bgsa-ler!D;# sam"ler, ivec! !, int sam"leC
gvec& te#el&etch Bgsa-ler!D;#Arra$ sam"ler, ivec( !,
int sam"leC
7se integer te1ture coordinate !
to looku- a single te1el %ro
sam"ler. ,he arra$ la$er coes
%ro the last co-onent o% ! %or
the arra$ %ors. ,he level9o%9
detail lod Bi% -resentC is as
descri:ed in sections !.11.5
G#hader E1ecutionI under ,e1el
4etches and (.5 G,e1turingI o%
the )-en/0 /ra-hics #$ste
#-eci%ication.
13!
8 Bui%t+in ,unction
Synta* )escription
gvec& te#el&etch)ffset Bgsa-ler1D sam"ler, int !, int lod,
int osetC
gvec& te#el&etch)ffset Bgsa-ler!D sam"ler, ivec! !, int lod,
ivec! osetC
gvec& te#el&etch)ffset Bgsa-ler(D sam"ler, ivec( !, int lod,
ivec( osetC
gvec& te#el&etch)ffset Bgsa-ler!DRect sam"ler, ivec! !,
ivec! osetC
gvec& te#el&etch)ffset Bgsa-ler1DArra$ sam"ler, ivec! !, int lod,
int osetC
gvec& te#el&etch)ffset Bgsa-ler!DArra$ sam"ler, ivec( !, int lod,
ivec! osetC
4etch a single te1el as in
te#el&etch o%%set :$ oset as
descri:ed in te#ture)ffset.
gvec& te#tureSroM)ffset Bgsa-ler1D sam"ler) vec! !)
int oset [, %loat bias\ C
gvec& te#tureSroM)ffset Bgsa-ler1D sam"ler) vec& !)
int oset [, %loat bias\ C
gvec& te#tureSroM)ffset Bgsa-ler!D sam"ler, vec( !,
ivec! oset [, %loat bias\ C
gvec& te#tureSroM)ffset Bgsa-ler!D sam"ler, vec& !,
ivec! oset [, %loat bias\ C
gvec& te#tureSroM)ffset Bgsa-ler(D sam"ler, vec& !,
ivec( oset [, %loat bias\ C
gvec& te#tureSroM)ffset Bgsa-ler!DRect sam"ler, vec( !,
ivec! oset C
gvec& te#tureSroM)ffset Bgsa-ler!DRect sam"ler, vec& !,
ivec! oset C
%loat te#tureSroM)ffset Bsa-ler!DRect#hadow sam"ler, vec& !,
ivec! oset C
%loat te#tureSroM)ffset Bsa-ler1D#hadow sam"ler) vec& !,
int oset [, %loat bias\ C
%loat te#tureSroM)ffset Bsa-ler!D#hadow sam"ler) vec& !,
ivec! oset [, %loat bias\ C
Do a -roLective te1ture looku-
as descri:ed in te#tureSroM
o%%set :$ oset as descri:ed in
te#ture)ffset.
14"
8 Bui%t+in ,unction
Synta* )escription
gvec& te#tureHod)ffset Bgsa-ler1D sam"ler) %loat !,
%loat lod, int osetC
gvec& te#tureHod)ffset Bgsa-ler!D sam"ler, vec! !,
%loat lod, ivec! osetC
gvec& te#tureHod)ffset Bgsa-ler(D sam"ler, vec( !,
%loat lod, ivec( osetC
%loat te#tureHod)ffset Bsa-ler1D#hadow sam"ler) vec( !,
%loat lod, int osetC
%loat te#tureHod)ffset Bsa-ler!D#hadow sam"ler) vec( !,
%loat lod, ivec! osetC
gvec& te#tureHod)ffset Bgsa-ler1DArra$ sam"ler) vec! !,
%loat lod, int osetC
gvec& te#tureHod)ffset Bgsa-ler!DArra$ sam"ler, vec( !,
%loat lod, ivec! osetC
%loat te#tureHod)ffset Bsa-ler1DArra$#hadow sam"ler) vec( !,
%loat lod, int osetC
Do an o%%set te1ture looku- with
e1-licit 0)D. #ee te#tureHod
and te#ture)ffset.
gvec& te#tureSroMHod Bgsa-ler1D sam"ler, vec! !, %loat lodC
gvec& te#tureSroMHod Bgsa-ler1D sam"ler, vec& !, %loat lodC
gvec& te#tureSroMHod Bgsa-ler!D sam"ler, vec( !, %loat lodC
gvec& te#tureSroMHod Bgsa-ler!D sam"ler, vec& !, %loat lodC
gvec& te#tureSroMHod Bgsa-ler(D sam"ler, vec& !, %loat lodC
%loat te#tureSroMHod Bsa-ler1D#hadow sam"ler) vec& !, %loat lodC
%loat te#tureSroMHod Bsa-ler!D#hadow sam"ler) vec& !) %loat lodC
Do a -roLective te1ture looku-
with e1-licit 0)D. #ee
te#tureSroM and te#tureHod.
gvec& te#tureSroMHod)ffset Bgsa-ler1D sam"ler) vec! !,
%loat lod, int osetC
gvec& te#tureSroMHod)ffset Bgsa-ler1D sam"ler) vec& !,
%loat lod, int osetC
gvec& te#tureSroMHod)ffset Bgsa-ler!D sam"ler, vec( !,
%loat lod, ivec! osetC
gvec& te#tureSroMHod)ffset Bgsa-ler!D sam"ler, vec& !,
%loat lod, ivec! osetC
gvec& te#tureSroMHod)ffset Bgsa-ler(D sam"ler, vec& !,
%loat lod, ivec( osetC
%loat te#tureSroMHod)ffset Bsa-ler1D#hadow sam"ler) vec& !,
%loat lod, int osetC
%loat te#tureSroMHod)ffset Bsa-ler!D#hadow sam"ler) vec& !,
%loat lod, ivec! osetC
Do an o%%set -roLective te1ture
looku- with e1-licit 0)D. #ee
te#tureSroM, te#tureHod, and
te#ture)ffset.
141
8 Bui%t+in ,unction
Synta* )escription
gvec& te#ture(rad Bgsa-ler1D sam"ler) %loat !,
%loat d!d#, %loat d!d*C
gvec& te#ture(rad Bgsa-ler!D sam"ler) vec! !,
vec! d!d#, vec! d!d*C
gvec& te#ture(rad Bgsa-ler(D sam"ler) vec( !,
vec( d!d#, vec( d!d*C
gvec& te#ture(rad Bgsa-ler"u:e sam"ler, vec( !,
vec( d!d#, vec( d!d*C
gvec& te#ture(rad Bgsa-ler!DRect sam"ler, vec! !,
vec! d!d#, vec! d!d*C
%loat te#ture(rad Bsa-ler!DRect#hadow sam"ler, vec( !,
vec! d!d#, vec! d!d*C
%loat te#ture(rad Bsa-ler1D#hadow sam"ler, vec( !,
%loat d!d#, %loat d!d*C
%loat te#ture(rad Bsa-ler!D#hadow sam"ler, vec( !,
vec! d!d#, vec! d!d*C
%loat te#ture(rad Bsa-ler"u:e#hadow sam"ler, vec& !,
vec( d!d#, vec( d!d*C
gvec& te#ture(rad Bgsa-ler1DArra$ sam"ler) vec! !,
%loat d!d#, %loat d!d*C
gvec& te#ture(rad Bgsa-ler!DArra$ sam"ler) vec( !,
vec! d!d#, vec! d!d*C
%loat te#ture(rad Bsa-ler1DArra$#hadow sam"ler, vec( !,
%loat d!d#, %loat d!d*C
%loat te#ture(rad Bsa-ler!DArra$#hadow sam"ler, vec& !,
vec! d!d#, vec! d!d*C
gvec& te#ture(rad Bgsa-ler"u:eArra$ sam"ler, vec& !,
vec( d!d#, vec( d!d*C
Do a te1ture looku- as in
te#ture :ut with e1-licit
gradients. ,he -artial
derivatives o% ! are with res-ect
to window 1 and window $. #et
s
#
=
{
!
#
%or a 1D te1ture
!.s
#
otherwise
s
*
=
{
!
*
%or a 1D te1ture
!.s
*
otherwise
t
#
=
{
'.' %or a 1D te1ture
!.t
#
otherwise
t
*
=
{
'.' %or a 1D te1ture
!.t
*
otherwise
r
#
=
{
'.' %or 1D or !D
!."
#
cu:e, other
r
*
=
{
'.' %or 1D or !D
!."
*
cu:e, other
4or the cu:e version, the -artial
derivatives o% ! are assued to
:e in the coordinate s$ste used
:e%ore te1ture coordinates are
-roLected onto the a--ro-riate
cu:e %ace.
142
8 Bui%t+in ,unction
Synta* )escription
gvec& te#ture(rad)ffset Bgsa-ler1D sam"ler) %loat !)
%loat d!d#, %loat d!d*) int osetC
gvec& te#ture(rad)ffset Bgsa-ler!D sam"ler, vec! !,
vec! d!d#, vec! d!d*) ivec! osetC
gvec& te#ture(rad)ffset Bgsa-ler(D sam"ler, vec( !,
vec( d!d#, vec( d!d*) ivec( osetC
gvec& te#ture(rad)ffset Bgsa-ler!DRect sam"ler, vec! !,
vec! d!d#, vec! d!d*) ivec! osetC
%loat te#ture(rad)ffset Bsa-ler!DRect#hadow sam"ler, vec( !,
vec! d!d#, vec! d!d*) ivec! osetC
%loat te#ture(rad)ffset Bsa-ler1D#hadow sam"ler) vec( !,
%loat d!d#, %loat d!d*) int oset C
%loat te#ture(rad)ffset Bsa-ler!D#hadow sam"ler) vec( !,
vec! d!d#, vec! d!d*) ivec! osetC
gvec& te#ture(rad)ffset Bgsa-ler1DArra$ sam"ler) vec! !)
%loat d!d#, %loat d!d*) int osetC
gvec& te#ture(rad)ffset Bgsa-ler!DArra$ sam"ler, vec( !,
vec! d!d#, vec! d!d*) ivec! osetC
%loat te#ture(rad)ffset Bsa-ler1DArra$#hadow sam"ler) vec(
!,
%loat d!d#, %loat d!d*) int osetC
%loat te#ture(rad)ffset Bsa-ler!DArra$#hadow sam"ler) vec&
!,
vec! d!d#, vec! d!d*) ivec! osetC
Do a te1ture looku- with :oth
e1-licit gradient and o%%set, as
descri:ed in te#ture(rad and
te#ture)ffset.
gvec& te#tureSroM(rad Bgsa-ler1D sam"ler) vec! !,
%loat d!d#, %loat d!d*C
gvec& te#tureSroM(rad Bgsa-ler1D sam"ler) vec& !,
%loat d!d#, %loat d!d*C
gvec& te#tureSroM(rad Bgsa-ler!D sam"ler) vec( !,
vec! d!d#, vec! d!d*C
gvec& te#tureSroM(rad Bgsa-ler!D sam"ler) vec& !,
vec! d!d#, vec! d!d*C
gvec& te#tureSroM(rad Bgsa-ler(D sam"ler) vec& !,
vec( d!d#, vec( d!d*C
gvec& te#tureSroM(rad Bgsa-ler!DRect sam"ler) vec( !,
vec! d!d#, vec! d!d*C
gvec& te#tureSroM(rad Bgsa-ler!DRect sam"ler) vec& !,
vec! d!d#, vec! d!d*C
%loat te#tureSroM(rad Bsa-ler!DRect#hadow sam"ler) vec& !,
vec! d!d#, vec! d!d*C
%loat te#tureSroM(rad Bsa-ler1D#hadow sam"ler, vec& !,
%loat d!d#, %loat d!d*C
%loat te#tureSroM(rad Bsa-ler!D#hadow sam"ler, vec& !,
vec! d!d#, vec! d!d*C
Do a te1ture looku- :oth
-roLectivel$, as descri:ed in
te#tureSroM, and with e1-licit
gradient as descri:ed in
te#ture(rad. ,he -artial
derivatives d!d# and d!d* are
assued to :e alread$ -roLected.
143
8 Bui%t+in ,unction
Synta* )escription
gvec& te#tureSroM(rad)ffset Bgsa-ler1D sam"ler) vec! !,
%loat d!d#, %loat d!d*, int osetC
gvec& te#tureSroM(rad)ffset Bgsa-ler1D sam"ler) vec& !,
%loat d!d#, %loat d!d*, int osetC
gvec& te#tureSroM(rad)ffset Bgsa-ler!D sam"ler) vec( !,
vec! d!d#, vec! d!d*, ivec! osetC
gvec& te#tureSroM(rad)ffset Bgsa-ler!D sam"ler) vec& !,
vec! d!d#, vec! d!d*, ivec! osetC
gvec& te#tureSroM(rad)ffset Bgsa-ler!DRect sam"ler, vec( !,
vec! d!d#, vec! d!d*) ivec! osetC
gvec& te#tureSroM(rad)ffset Bgsa-ler!DRect sam"ler, vec& !,
vec! d!d#, vec! d!d*) ivec! osetC
%loat te#tureSroM(rad)ffset Bsa-ler!DRect#hadow sam"ler,
vec& !,
vec! d!d#, vec! d!d*) ivec! osetC
gvec& te#tureSroM(rad)ffset Bgsa-ler(D sam"ler) vec& !,
vec( d!d#, vec( d!d*, ivec( osetC
%loat te#tureSroM(rad)ffset Bsa-ler1D#hadow sam"ler, vec& !,
%loat d!d#, %loat d!d*, int osetC
%loat te#tureSroM(rad)ffset Bsa-ler!D#hadow sam"ler, vec& !,
vec! d!d#, vec! d!d*, ivec! osetC
Do a te1ture looku- -roLectivel$
and with e1-licit gradient as
descri:ed in te#tureSroM(rad,
as well as with o%%set, as
descri:ed in te#ture)ffset.
3.8." Te*ture Gather Instructions
,he te1ture gather %unctions take co-onents o% a single %loating9-oint vector o-erand as a te1ture
coordinate, deterine a set o% %our te1els to sa-le %ro the :ase level o% detail o% the s-eci%ied te1ture
iage, and return one co-onent %ro each te1el in a %our9co-onent result vector.
@hen -er%oring a te1ture gather o-eration, the ini%ication and agni%ication %ilters are ignored, and
the rules %or 0I8EAR %iltering in the )-en/0 #-eci%ication are a--lied to the :ase level o% the te1ture
iage to identi%$ the %our te1els i081) i181) i180) and i080. ,he te1els are then converted to te1ture source
colors BR
s
, '
s
, <
s
, ;
s
C according to ,a:le (.!', %ollowed :$ a--lication o% the te1ture swi==le as descri:ed
in section (.6.! G#hader E1ecutionI o% the )-en/0 /ra-hics #$ste #-eci%ication. A %our9co-onent
vector is asse:led :$ taking the selected co-onent %ro each o% the -ost9swi==led te1ture source colors
in the order Bi081) i181) i180) i080C.
4or te1ture gather %unctions using a shadow sa-ler t$-e, each o% the %our te1el looku-s -er%ors a de-th
co-arison against the de-th re%erence value -assed in BreJC, and returns the result o% that co-arison in
the a--ro-riate co-onent o% the result vector.
As with other te1ture looku- %unctions, the results o% a te1ture gather are unde%ined %or shadow sa-lers i%
the te1ture re%erenced is not a de-th te1ture or has de-th co-arisons disa:ledN or %or non9shadow
sa-lers i% the te1ture re%erenced is a de-th te1ture with de-th co-arisons ena:led.
144
8 Bui%t+in ,unction
Synta* )escription
gvec& te#ture(atherBgsa-ler!D sam"ler, vec! !
[, int com"\C
gvec& te#ture(atherBgsa-ler!DArra$ sam"ler,
vec( ! [, int com"\C
gvec& te#ture(atherBgsa-ler"u:e sam"ler,
vec( ! [, int com"\C
gvec& te#ture(atherBgsa-ler"u:eArra$ sam"ler,
vec& ![, int com"\C
gvec& te#ture(atherBgsa-ler!DRect sam"ler,
vec! ![, int com"\C
vec& te#ture(atherBsa-ler!D#hadow sam"ler,
vec! !, %loat reJC
vec& te#ture(atherBsa-ler!DArra$#hadow sam"ler,
vec( !, %loat reJC
vec& te#ture(atherBsa-ler"u:e#hadow sam"ler,
vec( !, %loat reJC
vec& te#ture(atherB
sa-ler"u:eArra$#hadow sam"ler,
vec& !, %loat reJCN
vec& te#ture(atherBsa-ler!DRect#hadow sam"ler,
vec! !, %loat reJCN
Returns the value
vec&B#a-le?i'?L1B!, :aseC.com",
#a-le?i1?L1B!, :aseC.com",
#a-le?i1?L'B!, :aseC.com",
#a-le?i'?L'B!, :aseC.com"C
I% s-eci%ied, the value o% com" ust :e a
constant integer e1-ression with a value o% ',
1, !, or (, identi%$ing the #, *, 0, or / -ost9
swi==led co-onent o% the %our9co-onent
vector looku- result %or each te1el,
res-ectivel$. I% com" is not s-eci%ied, it is
treated as ', selecting the # co-onent o%
each te1el to generate the result.
gvec& te#ture(ather)ffsetBgsa-ler!D sam"ler,
vec! !, ivec! oset
[, int com"\C
gvec& te#ture(ather)ffsetBgsa-ler!DArra$ sam"ler,
vec( !, ivec! oset
[, int com"\C
gvec& te#ture(ather)ffsetBgsa-ler!DRect sam"ler)
vec! !, ivec! oset
[, int com"\C
vec& te#ture(ather)ffsetB
sa-ler!D#hadow sam"ler,
vec! !, %loat reJ, ivec! osetC
vec& te#ture(ather)ffsetB
sa-ler!DArra$#hadow sam"ler,
vec( !, %loat reJ, ivec! osetC
vec& te#ture(ather)ffsetB
sa-ler!DRect#hadow sam"ler,
vec! !, %loat reJ, ivec! osetC
2er%or a te1ture gather o-eration as in
te#ture(ather :$ oset as descri:ed in
te#ture)ffset e1ce-t that the oset can :e
varia:le Bnon constantC and the
i-leentation9de-endent iniu and
a1iu o%%set values are given :$
;I8?2R)/RA;?,EZ,7RE?/A,*ER?)44#E,
and
;AZ?2R)/RA;?,EZ,7RE?/A,*ER?)44#E,,
res-ectivel$.
145
8 Bui%t+in ,unction
Synta* )escription
gvec& te#ture(ather)ffsetsB
gsa-ler!D sam"ler,
vec! !, ivec! osets[&\
[, int com"\C
gvec& te#ture(ather)ffsetsB
gsa-ler!DArra$ sam"ler,
vec( !, ivec! osets[&\
[, int com"\C
gvec& te#ture(ather)ffsetsB
gsa-ler!DRect sam"ler)
vec( !, ivec! osets[&\
[, int com"\C
vec& te#ture(ather)ffsetsB
sa-ler!D#hadow sam"ler,
vec! !, %loat reJ, ivec! osets[&\C
vec& te#ture(ather)ffsetsB
sa-ler!DArra$#hadow sam"ler,
vec( !, %loat reJ, ivec! osets[&\C
vec& te#ture(ather)ffsetsB
sa-ler!DRect#hadow sam"ler,
vec! !, %loat reJ, ivec! osets[&\C
)-erate identicall$ to te#ture(ather)ffset
e1ce-t that osets is used to deterine the
location o% the %our te1els to sa-le. Each
o% the %our te1els is o:tained :$ a--l$ing the
corres-onding o%%set in osets as a Bu) vC
coordinate o%%set to !, identi%$ing the %our9
te1el 0I8EAR %oot-rint, and then selecting
the te1el i080 o% that %oot-rint. ,he s-eci%ied
values in osets ust :e set with constant
integral e1-ressions.
146
8 Bui%t+in ,unction
3.8. Compatibility +rofile Te*ture ,unctions
,he %ollowing te1ture %unctions are onl$ in the co-ati:ilit$ -ro%ile.
Synta* =deprecated> )escription =deprecated>
vec& te#ture1D Bsa-ler1D sam"ler)
%loat coord [, %loat bias\ C
vec& te#ture1DSroM Bsa-ler1D sam"ler,
vec! coord [, %loat bias\ C
vec& te#ture1DSroM Bsa-ler1D sam"ler,
vec& coord [, %loat bias\ C
vec& te#ture1DHod Bsa-ler1D sam"ler,
%loat coord, %loat lodC
vec& te#ture1DSroMHod Bsa-ler1D sam"ler,
vec! coord, %loat lodC
vec& te#ture1DSroMHod Bsa-ler1D sam"ler,
vec& coord, %loat lodC
#ee corres-onding signature a:ove without
G1DI in the nae.
vec& te#ture"D Bsa-ler!D sam"ler,
vec! coord [, %loat bias\ C
vec& te#ture"DSroM Bsa-ler!D sam"ler,
vec( coord [, %loat bias\ C
vec& te#ture"DSroM Bsa-ler!D sam"ler,
vec& coord [, %loat bias\ C
vec& te#ture"DHod Bsa-ler!D sam"ler,
vec! coord, %loat lodC
vec& te#ture"DSroMHod Bsa-ler!D sam"ler,
vec( coord, %loat lodC
vec& te#ture"DSroMHod Bsa-ler!D sam"ler,
vec& coord, %loat lodC
#ee corres-onding signature a:ove without
G!DI in the nae.
vec& te#ture'D Bsa-ler(D sam"ler,
vec( coord [, %loat bias\ C
vec& te#ture'DSroM Bsa-ler(D sam"ler,
vec& coord [, %loat bias\ C
vec& te#ture'DHod Bsa-ler(D sam"ler,
vec( coord, %loat lodC
vec& te#ture'DSroMHod Bsa-ler(D sam"ler,
vec& coord, %loat lodC
#ee corres-onding signature a:ove without
G(DI in the nae.
7se the te1ture coordinate coord to do a
te1ture looku- in the (D te1ture currentl$
:ound to sam"ler. 4or the -roLective
BGSroMIC versions, the te1ture coordinate is
divided :$ coord.1.
vec& te#tureCube Bsa-ler"u:e sam"ler,
vec( coord [, %loat bias\ C
vec& te#tureCubeHod Bsa-ler"u:e sam"ler,
vec( coord, %loat lodC
#ee corres-onding signature a:ove without
G"u:eI in the nae.
147
8 Bui%t+in ,unction
Synta* =deprecated> )escription =deprecated>
vec& shadow1D Bsa-ler1D#hadow sam"ler)
vec( coord [, %loat bias\ C
vec& shadow"D Bsa-ler!D#hadow sam"ler)
vec( coord [, %loat bias\ C
vec& shadow1DSroM Bsa-ler1D#hadow sam"ler)
vec& coord [, %loat bias\ C
vec& shadow"DSroM Bsa-ler!D#hadow sam"ler)
vec& coord [, %loat bias\ C
vec& shadow1DHod Bsa-ler1D#hadow sam"ler)
vec( coord, %loat lodC
vec& shadow"DHod Bsa-ler!D#hadow sam"ler)
vec( coord, %loat lodC
vec& shadow1DSroMHodBsa-ler1D#hadow sam"ler)
vec& coord, %loat lodC
vec& shadow"DSroMHodBsa-ler!D#hadow sam"ler)
vec& coord) %loat lodC
#ae %unctionalit$ as the Gte#ture^ :ased
naes a:ove with the sae signature.
148
8 Bui%t+in ,unction
3.1! .tomic6Counter ,unctions
,he atoic9counter o-erations in this section o-erate atoicall$ with res-ect to each other. ,he$ are
atoic %or an$ single counter, eaning an$ o% these o-erations on a s-eci%ic counter in one shader
instantiation will :e indivisi:le :$ an$ o% these o-erations on the sae counter %ro another shader
instantiation. ,here is no guarantee that these o-erations are atoic with res-ect to other %ors o% access
to the counter or that the$ are seriali=ed when a--lied to se-arate counters. #uch cases would re<uire
additional use o% %ences, :arriers, or other %ors a s$nchroni=ation, i% atoicit$ or seriali=ation is desired.
,he value returned :$ an atoic9counter %unction is the value o% an atoic counter, which a$ :e
returned and increented in an atoic o-eration, or
decreented and returned in an atoic o-eration, or
si-l$ returned.
,he underl$ing counter is a (!9:it unsigned integer. Increents and decreents at the liit o% the range
will wra- to [', !
(!
91\.
Synta* )escription
uint atomicCounterIncrement Batoic?uint cC Atoicall$
1. increents the counter %or c, and
!. returns its value -rior to the increent
o-eration.
,hese two ste-s are done atoicall$ with res-ect the
atoic counter %unctions in this ta:le.
uint atomicCounterDecrement Batoic?uint cC Atoicall$
1. decreents the counter %or c, and
!. returns the value resulting %ro the
decreent o-eration.
,hese two ste-s are done atoicall$ with res-ect to
the atoic counter %unctions in this ta:le.
uint atomicCounter Batoic?uint cC Returns the counter value %or c.
3.11 Image ,unctions
Varia:les using one o% the iage :asic t$-es a$ :e used :$ the :uilt9in shader iage eor$ %unctions
de%ined in this section to read and write individual te1els o% a te1ture. Each iage varia:le re%erences an
iage unit, which has a te1ture iage attached.
@hen iage eor$ %unctions :elow access eor$, an individual te1el in the iage is identi%ied using
an BiC, Bi) 8C, or Bi) 8) .C coordinate corres-onding to the values o% !. 4or image"DL% and
image"DL%Array varia:les Band the corres-onding intHunsigned int t$-esC corres-onding to ulti9
14!
8 Bui%t+in ,unction
sa-le te1tures, each te1el a$ have ulti-le sa-les and an individual sa-le is identi%ied using the
integer sam"le -araeter. ,he coordinates and sa-le nu:er are used to select an individual te1el in
the anner descri:ed in section (.6.Z o% the )-en/0 s-eci%ication.
0oads and stores su--ort %loat, integer, and unsigned integer t$-es. ,he data t$-es :elow starting
GgimageI serve as -laceholders eaning t$-es starting either GimageI, GiimageI, or GuimageI in the sae
wa$ as gvec or gsam"ler in earlier sections.
,he +7;'-_!;R;7( in the -rotot$-es :elow is a -laceholder re-resenting (( se-arate %unctions, each
%or a di%%erent t$-e o% iage varia:le. ,he +7;'-_!;R;7( -laceholder is re-laced :$ one o% the
%ollowing -araeter lists:
giage1D image, int !
giage!D image, ivec! !
giage(D image, ivec( !
giage!DRect image, ivec! !
giage"u:e image, ivec( !
giageBu%%er image, int !
giage1DArra$ image, ivec! !
giage!DArra$ image, ivec( !
giage"u:eArra$ image, ivec( !
giage!D;# image, ivec! !, int sam"le
giage!D;#Arra$ image, ivec( !, int sam"le
where each o% the line re-resents one o% three di%%erent iage varia:le t$-es, and image, !, and sam"le
s-eci%$ the individual te1el to o-erate on. ,he ethod %or identi%$ing the individual te1el o-erated on
%ro image, !, and sam"le, and the ethod %or reading and writing the te1el are s-eci%ied in section (.6.Z
o% the )-en/0 s-eci%ication re% .
,he atoic %unctions -er%or atoic o-erations on individual te1els or sa-les o% an iage varia:le.
Atoic eor$ o-erations read a value %ro the selected te1el, co-ute a new value using one o% the
o-erations descri:ed :elow, write the new value to the selected te1el, and return the original value read.
,he contents o% the te1el :eing u-dated :$ the atoic o-eration are guaranteed not to :e odi%ied :$ an$
other iage store or atoic %unction :etween the tie the original value is read and the tie the new
value is written.
Atoic eor$ o-erations are su--orted on onl$ a su:set o% all iage varia:le t$-esN image ust :e
either:
a signed integer iage varia:le Bt$-e starts GiimageIC and a %orat <uali%ier o% rA2i, used with a
data arguent o% t$-e int) or
an unsigned iage varia:le Bt$-e starts GuimageIC and a %orat <uali%ier o% rA2ui, used with a
data arguent o% t$-e uint.
15"
8 Bui%t+in ,unction
Synta* )escription
gvec& imageHoad Breadonl$ +7;'-_!;R;7(C 0oads the te1el at the coordinate ! %ro the iage
unit image Bin +7;'-_!;R;7(C. 4or ulti9sa-le
loads, the sa-le nu:er is given :$ sam"le. @hen
image, !, sam"le identi%$ a valid te1el, the :its used
to re-resent the selected te1el in eor$ are
converted to a vecI, ivecI, or uvecI in the anner
descri:ed in section (.6.!' o% the )-en/0
#-eci%ication and returned.
void image%tore Bwriteonl$ +7;'-_!;R;7(,
gvec& dataC
#tores data into the te1el at the coordinate ! %ro
the iage s-eci%ied :$ image. 4or ulti9sa-le
stores, the sa-le nu:er is given :$ sam"le. @hen
image, !, and sam"le identi%$ a valid te1el, the :its
used to re-resent data are converted to the %orat o%
the iage unit in the anner descri:ed in section
(.6.!' o% the )-en/0 #-eci%ication and stored to the
s-eci%ied te1el.
uint imageAtomicAdd B+7;'-_!;R;7(,
uint dataC
int imageAtomicAdd B+7;'-_!;R;7(,
int dataC
"o-utes a new value :$ adding the value o% data
to the contents o% the selected te1el.
uint imageAtomicLin B+7;'-_!;R;7(,
uint dataC
int imageAtomicLin B+7;'-_!;R;7(,
int dataC
"o-utes a new value :$ taking the iniu o% the
value o% data and the contents o% the selected te1el.
uint imageAtomicLa# B+7;'-_!;R;7(,
uint dataC
int imageAtomicLa# B+7;'-_!;R;7(,
int dataC
"o-utes a new value :$ taking the a1iu o% the
value data and the contents o% the selected te1el.
uint imageAtomicAnd B+7;'-_!;R;7(,
uint dataC
int imageAtomicAnd B+7;'-_!;R;7(,
int dataC
"o-utes a new value :$ -er%oring a :it9wise and
o% the value o% data and the contents o% the selected
te1el.
uint imageAtomic)r B+7;'-_!;R;7(,
uint dataC
int imageAtomic)r B+7;'-_!;R;7(,
int dataC
"o-utes a new value :$ -er%oring a :it9wise or
o% the value o% data and the contents o% the selected
te1el.
151
8 Bui%t+in ,unction
Synta* )escription
uint imageAtomicXor B+7;'-_!;R;7(,
uint dataC
int imageAtomicXor B+7;'-_!;R;7(,
int dataC
"o-utes a new value :$ -er%oring a :it9wise
e1clusive or o% the value o% data and the contents o%
the selected te1el.
uint imageAtomicO#change B+7;'-_!;R;7(,
uint dataC
int imageAtomicO#change B+7;'-_!;R;7(,
int dataC
"o-utes a new value :$ si-l$ co-$ing the value
o% data.
uint imageAtomicCom%wa
B+7;'-_!;R;7(,
uint com"are,
uint dataC
int imageAtomicCom%wa
B+7;'-_!;R;7(,
int com"are,
int dataC
"o-ares the value o% com"are and the contents o%
the selected te1el. I% the values are e<ual, the new
value is given :$ dataN otherwise, it is taken %ro the
original value loaded %ro the te1el.
3.12 ,ragment +rocessing ,unctions
4ragent -rocessing %unctions are onl$ availa:le in %ragent shaders.
3.12.1 )eri#ati#e ,unctions
Derivatives a$ :e co-utationall$ e1-ensive andHor nuericall$ unsta:le. ,here%ore, an )-en/0
i-leentation a$ a--ro1iate the true derivatives :$ using a %ast :ut not entirel$ accurate derivative
co-utation. Derivatives are unde%ined within non9uni%or control %low.
,he e1-ected :ehavior o% a derivative is s-eci%ied using %orwardH:ackward di%%erencing.
4orward di%%erencing:
F #d#F # ~dFd# #d# 1a
dFd# # ~
F #d#F #
d#
1:
Backward di%%erencing:
F #d#F # ~dFd# #d# !a
dFd# # ~
F #F #d#
d#
!:
152
8 Bui%t+in ,unction
@ith single9sa-le rasteri=ation, d# RT 1.' in e<uations 1: and !:. 4or ulti9sa-le rasteri=ation, d# R
!.' in e<uations 1: and !:.
d&dy is a--ro1iated siilarl$, with * re-lacing #.
A /0 i-leentation a$ use the a:ove or other ethods to -er%or the calculation, su:Lect to the
%ollowing conditions:
1. ,he ethod a$ use -iecewise linear a--ro1iations. #uch linear a--ro1iations i-l$ that higher
order derivatives, d&d#Bd&d#B#CC and a:ove, are unde%ined.
!. ,he ethod a$ assue that the %unction evaluated is continuous. ,here%ore derivatives within non9
uni%or control %low are unde%ined.
(. ,he ethod a$ di%%er -er %ragent, su:Lect to the constraint that the ethod a$ var$ :$ window
coordinates, not screen coordinates. ,he invariance re<uireent descri:ed in section (.! GInvarianceI
o% the )-en/0 /ra-hics #$ste #-eci%ication, is rela1ed %or derivative calculations, :ecause the
ethod a$ :e a %unction o% %ragent location.
)ther -ro-erties that are desira:le, :ut not re<uired, are:
&. 4unctions should :e evaluated within the interior o% a -riitive Binter-olated, not e1tra-olatedC.
+. 4unctions %or d&d# should :e evaluated while holding $ constant. 4unctions %or d&dy should :e
evaluated while holding 1 constant. *owever, i1ed higher order derivatives, like d&d#Bd&dyB*CC
and d&dyBd&d#B#CC are unde%ined.
.. Derivatives o% constant arguents should :e '.
In soe i-leentations, var$ing degrees o% derivative accurac$ a$ :e o:tained :$ -roviding /0 hints
Bsection +.& G*intsI o% the )-en/0 /ra-hics #$ste #-eci%icationC, allowing a user to ake an iage
<ualit$ versus s-eed trade o%%.
Synta* )escription
gen,$-e d&d# Bgen,$-e "C Returns the derivative in 1 using local di%%erencing %or
the in-ut arguent ".
gen,$-e d&dy Bgen,$-e "C Returns the derivative in $ using local di%%erencing %or
the in-ut arguent ".
,hese two %unctions are coonl$ used to estiate the
%ilter width used to anti9alias -rocedural te1tures. @e
are assuing that the e1-ression is :eing evaluated in
-arallel on a #I;D arra$ so that at an$ given -oint in
tie the value o% the %unction is known at the grid -oints
re-resented :$ the #I;D arra$. 0ocal di%%erencing
:etween #I;D arra$ eleents can there%ore :e used to
derive d4d1, d4d$, etc.
153
8 Bui%t+in ,unction
Synta* )escription
gen,$-e fwidth Bgen,$-e "C Returns the su o% the a:solute derivative in 1 and $
using local di%%erencing %or the in-ut arguent ", i.e.,
abs Bd&d# B"CC F abs Bd&dy B"CCN
3.12.2 Interpolation ,unctions
Built9in inter-olation %unctions are availa:le to co-ute an inter-olated value o% a %ragent shader in-ut
varia:le at a shader9s-eci%ied B#, *C location. A se-arate 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 -roduce the
de%ault value o% the in-ut.
4or all o% the inter-olation %unctions, inter"olant ust :e an in-ut varia:le or an eleent o% an in-ut
varia:le declared as an arra$. "o-onent selection o-erators Be.g., .#*C a$ not :e used when s-eci%$ing
inter"olant. I% inter"olant is declared with a flat or centroid <uali%ier, the <uali%ier will have no e%%ect on
the inter-olated value. I% inter"olant is declared with the noersective <uali%ier, the inter-olated value
will :e co-uted without -ers-ective correction.
Synta* )escription
%loat interolateAtCentroid B%loat inter"olantC
vec! interolateAtCentroid Bvec! inter"olantC
vec( interolateAtCentroid Bvec( inter"olantC
vec& interolateAtCentroid Bvec& inter"olantC
Return the value o% the in-ut inter"olant sa-led at a
location inside the :oth the -i1el and the -riitive
:eing -rocessed. ,he value o:tained would :e the
sae value assigned to the in-ut varia:le i% declared
with the centroid <uali%ier.
%loat interolateAt%amle B%loat inter"olant,
int sam"leC
vec! interolateAt%amle Bvec! inter"olant,
int sam"leC
vec( interolateAt%amle Bvec( inter"olant,
int sam"leC
vec& interolateAt%amle Bvec& inter"olant,
int sam"leC
Return the value o% the in-ut inter"olant varia:le at
the location o% sa-le nu:er sam"le. I%
ultisa-le :u%%ers are not availa:le, the in-ut
varia:le will :e evaluated at the center o% the -i1el.
I% sa-le sam"le does not e1ist, the -osition used to
inter-olate the in-ut varia:le is unde%ined.
%loat interolateAt)ffset B%loat inter"olant,
vec! o%%setC
vec! interolateAt)ffset Bvec! inter"olant,
vec! o%%setC
vec( interolateAt)ffset Bvec( inter"olant,
vec! o%%setC
vec& interolateAt)ffset Bvec& inter"olant,
vec! o%%setC
Return the value o% the in-ut inter"olant varia:le
sa-led at an o%%set %ro the center o% the -i1el
s-eci%ied :$ oset. ,he two %loating9-oint
co-onents o% oset, give the o%%set in -i1els in the #
and * directions, res-ectivel$. An o%%set o% B', 'C
identi%ies the center o% the -i1el. ,he range and
granularit$ o% o%%sets su--orted :$ this %unction is
i-leentation9de-endent.
154
8 Bui%t+in ,unction
3.1" 5oise ,unctions
8oise %unctions are availa:le to %ragent, geoetr$, and verte1 shaders. ,he$ are stochastic %unctions that
can :e used to increase visual co-le1it$. Values returned :$ the %ollowing noise %unctions give the
a--earance o% randoness, :ut are not trul$ rando. ,he noise %unctions :elow are de%ined to have the
%ollowing characteristics:
,he return valueBsC are alwa$s in the range [91.',1.'\, and cover at least the range [9'.., '..\, with a
/aussian9like distri:ution.
,he return valueBsC have an overall average o% '.'
,he$ are re-eata:le, in that a -articular in-ut value will alwa$s -roduce the sae return value
,he$ are statisticall$ invariant under rotation Bi.e., no atter how the doain is rotated, it has the sae
statistical characterC
,he$ have a statistical invariance under translation Bi.e., no atter how the doain is translated, it has
the sae statistical characterC
,he$ t$-icall$ give di%%erent results under translation.
,he s-atial %re<uenc$ is narrowl$ concentrated, centered soewhere :etween '.+ to 1.'.
,he$ are "
1
continuous ever$where Bi.e., the %irst derivative is continuousC
Synta* )escription
%loat noise1 Bgen,$-e #C Returns a 1D noise value :ased on the in-ut value #.
vec! noise" Bgen,$-e #C Returns a !D noise value :ased on the in-ut value #.
vec( noise' Bgen,$-e #C Returns a (D noise value :ased on the in-ut value #.
vec& noiseI Bgen,$-e #C Returns a &D noise value :ased on the in-ut value #.
155
8 Bui%t+in ,unction
3.1 Geometry Shader ,unctions
,hese %unctions are onl$ availa:le in geoetr$ shaders. ,he$ are descri:ed in ore de-th %ollowing the
ta:le.
Synta* )escription
void Omit%treamVerte# Bint streamC Eit the current values o% out-ut varia:les to the current
out-ut -riitive on strea stream. ,he arguent to stream
ust :e a constant integral e1-ression. )n return %ro this
call, the values o% all out-ut varia:les are unde%ined.
"an onl$ :e used i% ulti-le out-ut streas are su--orted.
void Ond%treamSrimitive Bint streamC "o-letes the current out-ut -riitive on strea stream and
starts a new one. ,he arguent to stream ust :e a constant
integral e1-ression. 8o verte1 is eitted.
"an onl$ :e used i% ulti-le out-ut streas are su--orted.
void OmitVerte# BC Eit the current values o% out-ut varia:les to the current
out-ut -riitive. )n return %ro this call, the values o%
out-ut varia:les are unde%ined.
@hen ulti-le out-ut streas are su--orted, this is
e<uivalent to calling Omit%treamVerte#B'C.
void OndSrimitive BC "o-letes the current out-ut -riitive and starts a new one.
8o verte1 is eitted.
@hen ulti-le out-ut streas are su--orted, this is
e<uivalent to calling Ond%treamSrimitiveB'C.
,he %unction Omit%treamVerte#BC s-eci%ies that a verte1 is co-leted. A verte1 is added to the current
out-ut -riitive in verte1 strea stream using the current values o% all out-ut varia:les associated with
stream. ,hese include gl_!oint(i0e, gl_Cli"Distance, gl_La*er, gl_!osition, gl_!rimitive+D and
gl_Vie/"ort+nde#. ,he values o% all out-ut varia:les %or all out-ut streas are unde%ined a%ter a call to
Omit%treamVerte#BC. I% a geoetr$ shader invocation has eitted ore vertices than -eritted :$ the
out-ut la$out <uali%ier ma#_vertices, the results o% calling Omit%treamVerte#BC are unde%ined.
,he %unction Ond%treamSrimitiveBC s-eci%ies that the current out-ut -riitive %or verte1 strea stream is
co-leted and a new out-ut -riitive Bo% the sae t$-eC will started :$ an$ su:se<uent
Omit%treamVerte#BC. ,his %unction does not eit a verte1. I% the out-ut la$out is declared to :e
G-ointsI, calling Ond%treamSrimitiveBC is o-tional.
A geoetr$ shader starts with an out-ut -riitive containing no vertices %or each strea. @hen a
geoetr$ shader terinates, the current out-ut -riitive %or each strea is autoaticall$ co-leted. It is
not necessar$ to call Ond%treamSrimitiveBC i% the geoetr$ shader writes onl$ a single -riitive.
156
8 Bui%t+in ,unction
;ulti-le out-ut streas are su--orted onl$ i% the out-ut -riitive t$-e is declared to :e "oints. A
-rogra will %ail to link i% it contains a geoetr$ shader calling Omit%treamVerte#BC or
Ond%treamSrimitiveBC i% its in-ut -riitive t$-e is not "oints.
157
8 Bui%t+in ,unction
3.1' Shader In#ocation Control ,unctions
,he shader invocation control %unction is availa:le onl$ in tessellation control shaders. It is used to
control the relative e1ecution order o% ulti-le shader invocations used to -rocess a -atch, which are
otherwise e1ecuted with an unde%ined relative order.
Synta* )escription
void barrier BC 4or an$ given static instance o% barrierBC, all tessellation control shader
invocations %or a single in-ut -atch ust enter it :e%ore an$ will :e
allowed to continue :e$ond it.
,he %unction barrierBC -rovides a -artiall$ de%ined order o% e1ecution :etween shader invocations. ,his
ensures that values written :$ one invocation -rior to a given static instance o% barrierBC can :e sa%el$
read :$ other invocations a%ter their call to the sae static instance barrierBC. Because invocations a$
e1ecute in unde%ined order :etween these :arrier calls, the values o% a -er9verte1 or -er9-atch out-ut
varia:le will :e unde%ined in a nu:er o% cases enuerated in section &.(.3 G)ut-ut Varia:lesI.
,he barrierBC %unction a$ onl$ :e -laced 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 stateent in the
%unction mainBC.
3.1( Shader 7emory Control ,unction
#haders o% all t$-es a$ read and write the contents o% te1tures and :u%%er o:Lects using iage varia:les.
@hile the order o% reads and writes within a single shader invocation is well9de%ined, the relative order o%
reads and writes to a single shared eor$ address %ro ulti-le se-arate shader invocations is largel$
unde%ined. ,he order o% eor$ accesses -er%ored :$ one shader invocation, as o:served :$ other
shader invocations, is also largel$ unde%ined :ut can :e controlled through eor$ control %unctions.
Synta* )escription
void memoryKarrier BC "ontrol the ordering o% eor$ transactions issued :$ a single shader
invocation.
,he :uilt9in memoryKarrierBC waits on the co-letion o% all eor$ accesses resulting %ro the use o%
iage varia:les or atoic counters B:$ the sae shader invocation calling memoryKarrier89C and then
returns with no other e%%ect. @hen this %unction returns, the results o% an$ eor$ stores -er%ored using
coherent varia:les -er%ored -rior to the call will :e visi:le to an$ %uture coherent eor$ access to the
sae addresses %ro other shader invocations. In -articular, the values written this wa$ in one shader
stage are guaranteed to :e visi:le to coherent eor$ accesses -er%ored :$ shader invocations in
su:se<uent stages when those invocations were triggered :$ the e1ecution o% the original shader
invocation Be.g., %ragent shader invocations %or a -riitive resulting %ro a -articular geoetr$ shader
invocationC.
158
8 Bui%t+in ,unction
15!
! Shading Language Gra**ar for -ore .rofi%e
8 Shading Language Grammar for Core
+rofile
,he graar is %ed %ro the out-ut o% le1ical anal$sis. ,he tokens returned %ro le1ical anal$sis are
L*$)+ 9**" &"*D+ ,*@9"% #$+ @#$+
9(%DS L*$+#$@% ,* %")% &*( #& ,#)LD(, (%+@($ )X#+LO LD)% ,%&D@"+ )@9(*@+#$%
9'%L< 9'%L> 9'%L; #'%L< #'%L> #'%L; @'%L< @'%L> @'%L; '%L< '%L> '%L;
GD+< GD+> GD+; L%$+(*#, #$ *@+ #$*@+ @$#&*(G HD+LO )DGH"%
,'%L< ,'%L> ,'%L; ,GD+< ,GD+> ,GD+;
$*H%()H%L+#'% &"D+ )G**+O "DU*@+ HD+LO )DGH"%
GD+<T< GD+<T> GD+<T;
GD+>T< GD+>T> GD+>T;
GD+;T< GD+;T> GD+;T;
,GD+<T< ,GD+<T> ,GD+<T;
,GD+>T< ,GD+>T> ,GD+>T;
,GD+;T< ,GD+;T> ,GD+;T;
)DGH"%(0, )DGH"%(<, )DGH"%(>, )DGH"%(L@9% )DGH"%(0,)OD,*X )DGH"%(<,)OD,*X
)DGH"%(L@9%)OD,*X )DGH"%(0,D((DU )DGH"%(<,D((DU )DGH"%(0,D((DU)OD,*X
)DGH"%(<,D((DU)OD,*X #)DGH"%(0, #)DGH"%(<, #)DGH"%(>, #)DGH"%(L@9%
#)DGH"%(0,D((DU #)DGH"%(<,D((DU @)DGH"%(0, @)DGH"%(<, @)DGH"%(>,
@)DGH"%(L@9% @)DGH"%(0,D((DU @)DGH"%(<,D((DU
)DGH"%(<,(%L+ )DGH"%(<,(%L+)OD,*X #)DGH"%(<,(%L+ @)DGH"%(<,(%L+
)DGH"%(9@&&%( #)DGH"%(9@&&%( @)DGH"%(9@&&%(
)DGH"%(L@9%D((DU )DGH"%(L@9%D((DU)OD,*X
#)DGH"%(L@9%D((DU @)DGH"%(L@9%D((DU
)DGH"%(<,G) #)DGH"%(<,G) @)DGH"%(<,G)
)DGH"%(<,G)Drra/ #)DGH"%(<,G)Drra/ @)DGH"%(<,G)Drra/
#GD-%0, ##GD-%0, @#GD-%0, #GD-%<, ##GD-%<,
@#GD-%<, #GD-%>, ##GD-%>, @#GD-%>,
#GD-%<,(%L+ ##GD-%<,(%L+ @#GD-%<,(%L+
#GD-%L@9% ##GD-%L@9% @#GD-%L@9%
#GD-%9@&&%( ##GD-%9@&&%( @#GD-%9@&&%(
#GD-%0,D((DU ##GD-%0,D((DU @#GD-%0,D((DU
#GD-%<,D((DU ##GD-%<,D((DU @#GD-%<,D((DU
#GD-%L@9%D((DU ##GD-%L@9%D((DU @#GD-%L@9%D((DU
#GD-%<,G) ##GD-%<,G) @#GD-%<,G)
#GD-%<,G)D((DU ##GD-%<,G)D((DU @#GD-%<,G)D((DU
16"
! Shading Language Gra**ar for -ore .rofi%e
)+(@L+ '*#, XO#"%
#,%$+#&#%( +UH%!$DG% &"*D+L*$)+D$+ #$+L*$)+D$+ @#$+L*$)+D$+ 9**"L*$)+D$+
&#%",!)%"%L+#*$
"%&+!*H (#-O+!*H
#$L!*H ,%L!*H "%!*H -%!*H %W!*H $%!*H
D$,!*H *(!*H T*(!*H G@"!D))#-$ ,#'!D))#-$ D,,!D))#-$
G*,!D))#-$ "%&+!D))#-$ (#-O+!D))#-$ D$,!D))#-$ T*(!D))#-$ *(!D))#-$
)@9!D))#-$
"%&+!HD(%$ (#-O+!HD(%$ "%&+!9(DLS%+ (#-O+!9(DLS%+ "%&+!9(DL% (#-O+!9(DL% ,*+
L*GGD L*"*$ %W@D" )%G#L*"*$ 9D$- ,D)O +#",% H"@) )+D( )"D)O H%(L%$+
"%&+!D$-"% (#-O+!D$-"% '%(+#LD"!9D( LD(%+ DGH%()D$, W@%)+#*$
#$'D(#D$+ H(%L#)%
O#-O!H(%L#)#*$ G%,#@G!H(%L#)#*$ "*X!H(%L#)#*$ H(%L#)#*$
,he %ollowing descri:es the graar %or the )-en/0 #hading 0anguage in ters o% the a:ove tokens.
variable_identiier:
+D-,$+F+-R
"rimar*_e#"ression:
variable_identiier
+,$C&,($;,$
E+,$C&,($;,$
FL&;$C&,($;,$
<&&LC&,($;,$
D&E<L-C&,($;,$
L-F$_!;R-, e#"ression R+'K$_!;R-,
"osti#_e#"ression:
"rimar*_e#"ression
"osti#_e#"ression L-F$_<R;CL-$ integer_e#"ression R+'K$_<R;CL-$
unction_call
"osti#_e#"ression D&$ F+-LD_(-L-C$+&,
"osti#_e#"ression +,C_&!
"osti#_e#"ression D-C_&!
integer_e#"ression:
e#"ression
161
! Shading Language Gra**ar for -ore .rofi%e
unction_call:
unction_call_or_met%od
unction_call_or_met%od:
unction_call_generic

unction_call_generic:
unction_call_%eader_/it%_"arameters R+'K$_!;R-,
unction_call_%eader_no_"arameters R+'K$_!;R-,
unction_call_%eader_no_"arameters:
unction_call_%eader V&+D
unction_call_%eader
unction_call_%eader_/it%_"arameters:
unction_call_%eader assignment_e#"ression
unction_call_%eader_/it%_"arameters C&77; assignment_e#"ression
unction_call_%eader:
unction_identiier L-F$_!;R-,
DD 'rammar ,ote: Constructors loo. li.e unctions) but le#ical anal*sis recogni0ed most o t%em as
DD .e*/ords. $%e* are no/ recogni0ed t%roug% Mt*"e_s"eciierN.
DD 7et%ods 3.lengt%4) subroutine arra* calls) and identiiers are recogni0ed t%roug% "osti#_e#"ression.
unction_identiier:
t*"e_s"eciier
"osti#_e#"ression
unar*_e#"ression:
"osti#_e#"ression
+,C_&! unar*_e#"ression
D-C_&! unar*_e#"ression
unar*_o"erator unar*_e#"ression
DD 'rammar ,ote: ,o traditional st*le t*"e casts.
unar*_o"erator:
!LE(
162
! Shading Language Gra**ar for -ore .rofi%e
D;(K
<;,'
$+LD-
DD 'rammar ,ote: ,o I=I or IOI unar* o"s. !ointers are not su""orted.
multi"licative_e#"ression:
unar*_e#"ression
multi"licative_e#"ression ($;R unar*_e#"ression
multi"licative_e#"ression (L;(K unar*_e#"ression
multi"licative_e#"ression !-RC-,$ unar*_e#"ression
additive_e#"ression:
multi"licative_e#"ression
additive_e#"ression !LE( multi"licative_e#"ression
additive_e#"ression D;(K multi"licative_e#"ression
s%it_e#"ression:
additive_e#"ression
s%it_e#"ression L-F$_&! additive_e#"ression
s%it_e#"ression R+'K$_&! additive_e#"ression
relational_e#"ression:
s%it_e#"ression
relational_e#"ression L-F$_;,'L- s%it_e#"ression
relational_e#"ression R+'K$_;,'L- s%it_e#"ression
relational_e#"ression L-_&! s%it_e#"ression
relational_e#"ression '-_&! s%it_e#"ression
e1ualit*_e#"ression:
relational_e#"ression
e1ualit*_e#"ression -P_&! relational_e#"ression
e1ualit*_e#"ression ,-_&! relational_e#"ression
and_e#"ression:
e1ualit*_e#"ression
and_e#"ression ;7!-R(;,D e1ualit*_e#"ression
e#clusive_or_e#"ression:
and_e#"ression
163
! Shading Language Gra**ar for -ore .rofi%e
e#clusive_or_e#"ression C;R-$ and_e#"ression
inclusive_or_e#"ression:
e#clusive_or_e#"ression
inclusive_or_e#"ression V-R$+C;L_<;R e#clusive_or_e#"ression
logical_and_e#"ression:
inclusive_or_e#"ression
logical_and_e#"ression ;,D_&! inclusive_or_e#"ression
logical_#or_e#"ression:
logical_and_e#"ression
logical_#or_e#"ression Q&R_&! logical_and_e#"ression
logical_or_e#"ression:
logical_#or_e#"ression
logical_or_e#"ression &R_&! logical_#or_e#"ression
conditional_e#"ression:
logical_or_e#"ression
logical_or_e#"ression PE-($+&, e#"ression C&L&, assignment_e#"ression
assignment_e#"ression:
conditional_e#"ression
unar*_e#"ression assignment_o"erator assignment_e#"ression
assignment_o"erator:
-PE;L
7EL_;((+',
D+V_;((+',
7&D_;((+',
;DD_;((+',
(E<_;((+',
L-F$_;((+',
R+'K$_;((+',
;,D_;((+',
Q&R_;((+',
&R_;((+',
e#"ression:
164
! Shading Language Gra**ar for -ore .rofi%e
assignment_e#"ression
e#"ression C&77; assignment_e#"ression
constant_e#"ression:
conditional_e#"ression
declaration:
unction_"rotot*"e (-7+C&L&,
init_declarator_list (-7+C&L&,
!R-C+(+&, "recision_1ualiier t*"e_s"eciier (-7+C&L&,
t*"e_1ualiier +D-,$+F+-R L-F$_<R;C- struct_declaration_list R+'K$_<R;C- (-7+C&L&,
t*"e_1ualiier +D-,$+F+-R L-F$_<R;C- struct_declaration_list R+'K$_<R;C-
+D-,$+F+-R (-7+C&L&,
t*"e_1ualiier +D-,$+F+-R L-F$_<R;C- struct_declaration_list R+'K$_<R;C-
+D-,$+F+-R L-F$_<R;CL-$ R+'K$_<R;CL-$ (-7+C&L&,
t*"e_1ualiier +D-,$+F+-R L-F$_<R;C- struct_declaration_list R+'K$_<R;C-
+D-,$+F+-R L-F$_<R;CL-$ constant_e#"ression R+'K$_<R;CL-$ (-7+C&L&,
t*"e_1ualiier (-7+C&L&,
t*"e_1ualiier +D-,$+F+-R (-7+C&L&,
t*"e_1ualiier +D-,$+F+-R identiier_list (-7+C&L&,
identiier_list:
C&77; +D-,$+F+-R
identiier_list C&77; +D-,$+F+-R
unction_"rotot*"e:
unction_declarator R+'K$_!;R-,
unction_declarator:
unction_%eader
unction_%eader_/it%_"arameters
unction_%eader_/it%_"arameters:
unction_%eader "arameter_declaration
unction_%eader_/it%_"arameters C&77; "arameter_declaration
unction_%eader:
ull*_s"eciied_t*"e +D-,$+F+-R L-F$_!;R-,
"arameter_declarator:
165
! Shading Language Gra**ar for -ore .rofi%e
t*"e_s"eciier +D-,$+F+-R
t*"e_s"eciier +D-,$+F+-R L-F$_<R;CL-$ constant_e#"ression R+'K$_<R;CL-$
"arameter_declaration:
t*"e_1ualiier "arameter_declarator
"arameter_declarator
t*"e_1ualiier "arameter_t*"e_s"eciier
"arameter_t*"e_s"eciier
"arameter_t*"e_s"eciier:
t*"e_s"eciier
init_declarator_list:
single_declaration
init_declarator_list C&77; +D-,$+F+-R
init_declarator_list C&77; +D-,$+F+-R L-F$_<R;CL-$ R+'K$_<R;CL-$
init_declarator_list C&77; +D-,$+F+-R L-F$_<R;CL-$ constant_e#"ression
R+'K$_<R;CL-$
init_declarator_list C&77; +D-,$+F+-R L-F$_<R;CL-$
R+'K$_<R;CL-$ -PE;L initiali0er
init_declarator_list C&77; +D-,$+F+-R L-F$_<R;CL-$ constant_e#"ression
R+'K$_<R;CL-$ -PE;L initiali0er
init_declarator_list C&77; +D-,$+F+-R -PE;L initiali0er
single_declaration:
ull*_s"eciied_t*"e
ull*_s"eciied_t*"e +D-,$+F+-R
ull*_s"eciied_t*"e +D-,$+F+-R L-F$_<R;CL-$ R+'K$_<R;CL-$
ull*_s"eciied_t*"e +D-,$+F+-R L-F$_<R;CL-$ constant_e#"ression R+'K$_<R;CL-$
ull*_s"eciied_t*"e +D-,$+F+-R L-F$_<R;CL-$ R+'K$_<R;CL-$ -PE;L initiali0er
ull*_s"eciied_t*"e +D-,$+F+-R L-F$_<R;CL-$ constant_e#"ression
R+'K$_<R;CL-$ -PE;L initiali0er
ull*_s"eciied_t*"e +D-,$+F+-R -PE;L initiali0er
+,V;R+;,$ +D-,$+F+-R
DD 'rammar ,ote: ,o IenumI) or It*"edeI.
ull*_s"eciied_t*"e:
t*"e_s"eciier
t*"e_1ualiier t*"e_s"eciier
166
! Shading Language Gra**ar for -ore .rofi%e
invariant_1ualiier:
+,V;R+;,$
inter"olation_1ualiier:
(7&&$K
FL;$
,&!-R(!-C$+V-
la*out_1ualiier:
L;R&E$ L-F$_!;R-, la*out_1ualiier_id_list R+'K$_!;R-,
la*out_1ualiier_id_list:
la*out_1ualiier_id
la*out_1ualiier_id_list C&77; la*out_1ualiier_id
la*out_1ualiier_id:
+D-,$+F+-R
+D-,$+F+-R -PE;L +,$C&,($;,$
"recise_1ualiier:
!R-C+(-
t*"e_1ualiier:
single_t*"e_1ualiier
t*"e_1ualiier single_t*"e_1ualiier
single_t*"e_1ualiier:
storage_1ualiier
la*out_1ualiier
"recision_1ualiier
inter"olation_1ualiier
invariant_1ualiier
"recise_1ualiier
storage_1ualiier:
C&,($
+,&E$
+,
167
! Shading Language Gra**ar for -ore .rofi%e
&E$
C-,$R&+D
!;$CK
(;7!L-
E,+F&R7
(E<R&E$+,-
(E<R&E$+,- L-F$_!;R-, +D-,$+F+-R t*"e_name_list R+'K$_!;R-,
t*"e_name_list:
$R!-_,;7-
t*"e_name_list C&77; $R!-_,;7-
t*"e_s"eciier:
t*"e_s"eciier_nonarra*
t*"e_s"eciier_nonarra* L-F$_<R;CL-$ R+'K$_<R;CL-$
t*"e_s"eciier_nonarra* L-F$_<R;CL-$ constant_e#"ression R+'K$_<R;CL-$
t*"e_s"eciier_nonarra*:
V&+D
FL&;$
D&E<L-
+,$
E+,$
<&&L
V-C2
V-CA
V-C4
DV-C2
DV-CA
DV-C4
<V-C2
<V-CA
<V-C4
+V-C2
+V-CA
+V-C4
EV-C2
EV-CA
168
! Shading Language Gra**ar for -ore .rofi%e
EV-C4
7;$2
7;$A
7;$4
7;$2Q2
7;$2QA
7;$2Q4
7;$AQ2
7;$AQA
7;$AQ4
7;$4Q2
7;$4QA
7;$4Q4
D7;$2
D7;$A
D7;$4
D7;$2Q2
D7;$2QA
D7;$2Q4
D7;$AQ2
D7;$AQA
D7;$AQ4
D7;$4Q2
D7;$4QA
D7;$4Q4
(;7!L-R1D
(;7!L-R2D
(;7!L-RAD
(;7!L-RCE<-
(;7!L-R1D(K;D&S
(;7!L-R2D(K;D&S
(;7!L-RCE<-(K;D&S
(;7!L-R1D;RR;R
(;7!L-R2D;RR;R
(;7!L-R1D;RR;R(K;D&S
(;7!L-R2D;RR;R(K;D&S
(;7!L-RCE<-;RR;R
16!
! Shading Language Gra**ar for -ore .rofi%e
(;7!L-RCE<-;RR;R(K;D&S
+(;7!L-R1D
+(;7!L-R2D
+(;7!L-RAD
+(;7!L-RCE<-
+(;7!L-R1D;RR;R
+(;7!L-R2D;RR;R
+(;7!L-RCE<-;RR;R
E(;7!L-R1D
E(;7!L-R2D
E(;7!L-RAD
E(;7!L-RCE<-
E(;7!L-R1D;RR;R
E(;7!L-R2D;RR;R
E(;7!L-RCE<-;RR;R
(;7!L-R2DR-C$
(;7!L-R2DR-C$(K;D&S
+(;7!L-R2DR-C$
E(;7!L-R2DR-C$
(;7!L-R<EFF-R
+(;7!L-R<EFF-R
E(;7!L-R<EFF-R
(;7!L-R2D7(
+(;7!L-R2D7(
E(;7!L-R2D7(
(;7!L-R2D7(;RR;R
+(;7!L-R2D7(;RR;R
E(;7!L-R2D7(;RR;R
+7;'-1D
++7;'-1D
E+7;'-1D
+7;'-2D
++7;'-2D
E+7;'-2D
+7;'-AD
++7;'-AD
E+7;'-AD
17"
! Shading Language Gra**ar for -ore .rofi%e
+7;'-2DR-C$
++7;'-2DR-C$
E+7;'-2DR-C$
+7;'-CE<-
++7;'-CE<-
E+7;'-CE<-
+7;'-<EFF-R
++7;'-<EFF-R
E+7;'-<EFF-R
+7;'-1D;RR;R
++7;'-1D;RR;R
E+7;'-1D;RR;R
+7;'-2D;RR;R
++7;'-2D;RR;R
E+7;'-2D;RR;R
+7;'-CE<-;RR;R
++7;'-CE<-;RR;R
E+7;'-CE<-;RR;R
+7;'-2D7(
++7;'-2D7(
E+7;'-2D7(
+7;'-2D7(;RR;R
++7;'-2D7(;RR;R
E+7;'-2D7(;RR;R
struct_s"eciier
$R!-_,;7-
"recision_1ualiier:
K+'K_!R-C+(+&,
7-D+E7_!R-C+(+&,
L&S_!R-C+(+&,
struct_s"eciier:
($REC$ +D-,$+F+-R L-F$_<R;C- struct_declaration_list R+'K$_<R;C-
($REC$ L-F$_<R;C- struct_declaration_list R+'K$_<R;C-
struct_declaration_list:
struct_declaration
struct_declaration_list struct_declaration
171
! Shading Language Gra**ar for -ore .rofi%e
struct_declaration:
t*"e_s"eciier struct_declarator_list (-7+C&L&,
t*"e_1ualiier t*"e_s"eciier struct_declarator_list (-7+C&L&,
struct_declarator_list:
struct_declarator
struct_declarator_list C&77; struct_declarator
struct_declarator:
+D-,$+F+-R
+D-,$+F+-R L-F$_<R;CL-$ R+'K$_<R;CL-$
+D-,$+F+-R L-F$_<R;CL-$ constant_e#"ression R+'K$_<R;CL-$
initiali0er:
assignment_e#"ression
declaration_statement:
declaration
statement:
com"ound_statement
sim"le_statement
DD 'rammar ,ote: labeled statements or (S+$CK onl*2 IgotoI is not su""orted.
sim"le_statement:
declaration_statement
e#"ression_statement
selection_statement
s/itc%_statement
case_label
iteration_statement
8um"_statement
com"ound_statement:
L-F$_<R;C- R+'K$_<R;C-
L-F$_<R;C- statement_list R+'K$_<R;C-
statement_no_ne/_sco"e:
com"ound_statement_no_ne/_sco"e
172
! Shading Language Gra**ar for -ore .rofi%e
sim"le_statement
com"ound_statement_no_ne/_sco"e:
L-F$_<R;C- R+'K$_<R;C-
L-F$_<R;C- statement_list R+'K$_<R;C-
statement_list:
statement
statement_list statement
e#"ression_statement:
(-7+C&L&,
e#"ression (-7+C&L&,
selection_statement:
+F L-F$_!;R-, e#"ression R+'K$_!;R-, selection_rest_statement
selection_rest_statement:
statement -L(- statement
statement
condition:
e#"ression
ull*_s"eciied_t*"e +D-,$+F+-R -PE;L initiali0er
s/itc%_statement:
(S+$CK L-F$_!;R-, e#"ression R+'K$_!;R-, L-F$_<R;C- s/itc%_statement_list
R+'K$_<R;C-
s/itc%_statement_list:
D= not%ing =D
statement_list
case_label:
C;(- e#"ression C&L&,
D-F;EL$ C&L&,
iteration_statement:
SK+L- L-F$_!;R-, condition R+'K$_!;R-, statement_no_ne/_sco"e
D& statement SK+L- L-F$_!;R-, e#"ression R+'K$_!;R-, (-7+C&L&,
F&R L-F$_!;R-, or_init_statement or_rest_statement R+'K$_!;R-,
statement_no_ne/_sco"e
173
! Shading Language Gra**ar for -ore .rofi%e
or_init_statement:
e#"ression_statement
declaration_statement
conditiono"t:
condition
D= em"t* =D
or_rest_statement:
conditiono"t (-7+C&L&,
conditiono"t (-7+C&L&, e#"ression
8um"_statement:
C&,$+,E- (-7+C&L&,
<R-;L (-7+C&L&,
R-$ER, (-7+C&L&,
R-$ER, e#"ression (-7+C&L&,
D+(C;RD (-7+C&L&, DD Fragment s%ader onl*.
DD 'rammar ,ote: ,o IgotoI. 'otos are not su""orted.
translation_unit:
e#ternal_declaration
translation_unit e#ternal_declaration
e#ternal_declaration:
unction_deinition
declaration
unction_deinition:
unction_"rotot*"e com"ound_statement_no_ne/_sco"e
174