GLSLangSpec.4.40
Transcript of GLSLangSpec.4.40
-
8/12/2019 GLSLangSpec.4.40
1/207
The OpenGLShading Language
Language Version: 4.40Document Revision: 822-Jan-2014
Editor: John Kessenich, LunarG
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
-
8/12/2019 GLSLangSpec.4.40
2/207
Copyright (c) 2008-2014 The Khronos Group Inc. All Rights Resere!.
This speci"ic#tion is protecte! $y copyright l#%s #n! cont#ins teri#l propriet#ry to the Khronos Group'Inc. It or #ny co&ponents y not $e repro!uce!' repu$lishe!' !istri$ute!' tr#ns&itte!' !ispl#ye!'$ro#!c#st or other%ise eploite! in #ny nner %ithout the epress prior %ritten per&ission o" KhronosGroup. ou y use this speci"ic#tion "or i&ple&enting the "unction#lity therein' %ithout #ltering orre&oing #ny tr#!er*' copyright or other notice "ro& the speci"ic#tion' $ut the receipt or possession o"this speci"ic#tion !oes not coney #ny rights to repro!uce' !isclose' or !istri$ute its contents' or tonu"#cture' use' or sell #nything th#t it y !escri$e' in %hole or in p#rt.
Khronos Group gr#nts epress per&ission to #ny current +ro&oter' Contri$utor or A!opter &e&$er o"Khronos to copy #n! re!istri$ute ,/II ersions o" this speci"ic#tion in #ny "#shion' proi!e! th#t/ C3ARG is !e "or the speci"ic#tion #n! the l#test ##il#$le up!#te o" the speci"ic#tion "or #nyersion o" the A+I is use! %heneer possi$le. uch !istri$ute! speci"ic#tion y $e re-"ortte! A5/G A the contents o" the speci"ic#tion #re not ch#nge! in #ny %#y. The speci"ic#tion y $eincorpor#te! into # pro!uct th#t is sol! #s long #s such pro!uct inclu!es signi"ic#nt in!epen!ent %or*!eelope! $y the seller. A lin* to the current ersion o" this speci"ic#tion on the Khronos Group %e$-siteshoul! $e inclu!e! %heneer possi$le %ith speci"ic#tion !istri$utions.
Khronos Group *es no' #n! epressly !iscl#i&s #ny' represent#tions or %#rr#nties' epress ori&plie!' reg#r!ing this speci"ic#tion' inclu!ing' %ithout li&it#tion' #ny i&plie! %#rr#nties o" &erch#nt#$ilityor "itness "or # p#rticul#r purpose or non-in"ringe&ent o" #ny intellectu#l property. Khronos Group *es
no' #n! epressly !iscl#i&s #ny' %#rr#nties' epress or i&plie!' reg#r!ing the correctness' #ccur#cy'co&pleteness' ti&eliness' #n! reli#$ility o" the speci"ic#tion. ,n!er no circu&st#nces %ill the KhronosGroup' or #ny o" its +ro&oters' Contri$utors or e&$ers or their respectie p#rtners' o""icers' !irectors'e&ployees' #gents or represent#ties $e li#$le "or #ny !#ges' %hether !irect' in!irect' speci#l orconse6uenti#l !#ges "or lost reenues' lost pro"its' or other%ise' #rising "ro& or in connection %iththese teri#ls.
Khronos' /penK/' /penK/G' /pen7G' /penA' /pen5 #n! /pen9 #re tr#!er*s o"the Khronos Group Inc. C/55AA is # tr#!er* o" ony Co&puter ntert#in&ent Inc. use! $yper&ission $y Khronos. /penG5 #n! /pen5 #re registere! tr#!er*s #n! the /penG5 logo is #tr#!er* o" ilicon Gr#phics Inc. use! $y per&ission $y Khronos. All other pro!uct n#&es' tr#!er*s'#n!:or co&p#ny n#&es #re use! solely "or i!enti"ic#tion #n! $elong to their respectie o%ners.
ii
-
8/12/2019 GLSLangSpec.4.40
3/207
Table of Contents1 Introduction.................................................................................................................................1
1.1 Acnowled!"ents................................................................................................................#1.# $han!es................................................................................................................................%1.#.1 $han!es since revision & o' GL(L version ).)*..........................................................%1.#.# $han!es since revision + o' GL(L version ).)*..........................................................)1.#.% (u""ar o' $han!es 'ro" Revision - o' GL(L Version ).%*....................................
1.% /verview..............................................................................................................................&1.) Error 0andlin!......................................................................................................................&1. 2o!ra2hical $onventions.................................................................................................&1.+ De2recation..........................................................................................................................&
# /verview o' /2enGL (hadin!....................................................................................................-#.1 Verte3 4rocessor..................................................................................................................-#.# essellation $ontrol 4rocessor.............................................................................................-#.% essellation Evaluation 4rocessor......................................................................................1*#.) Geo"etr 4rocessor...........................................................................................................1*#. 5ra!"ent 4rocessor............................................................................................................1*#.+ $o"2ute 4rocessor.............................................................................................................1*
% Basics........................................................................................................................................1#%.1 $haracter (et and 4hases o' $o"2ilation..........................................................................1#%.# (ource (trin!s....................................................................................................................1%%.% 4re2rocessor.......................................................................................................................1)%.) $o""ents..........................................................................................................................1-%. oens................................................................................................................................#*%.+ Kewords............................................................................................................................#*
%.& Identi'iers...........................................................................................................................##%.6 De'initions..........................................................................................................................##
%.6.1 (tatic 7se....................................................................................................................#%%.6.# 7ni'or" and 8on97ni'or" $ontrol 5low..................................................................#%%.6.% Dna"icall 7ni'or" E32ressions.............................................................................#%
) Variales and 2es..................................................................................................................#)).1 Basic 2es........................................................................................................................#)
).1.1 Void............................................................................................................................#6).1.# Booleans.....................................................................................................................#6).1.% Inte!ers.......................................................................................................................#6).1.) 5loatin!94oint Variales.............................................................................................%*
).1. Vectors........................................................................................................................%1).1.+ ;atrices......................................................................................................................%1).1.& /2a
-
8/12/2019 GLSLangSpec.4.40
4/207
).1.&.1 (a"2lers.............................................................................................................%#).1.&.# I"a!es.................................................................................................................%%).1.&.% Ato"ic $ounters.................................................................................................%%
).1.6 (tructures....................................................................................................................%%).1.- Arras.........................................................................................................................%)).1.1* I"2licit $onversions................................................................................................%6).1.11 Initiali=ers.................................................................................................................%-
).# (co2in!...............................................................................................................................)1).% (tora!e >uali'iers...............................................................................................................))
).%.1 De'ault (tora!e >uali'ier............................................................................................)).%.# $onstant >uali'ier......................................................................................................)).%.% $onstant E32ressions.................................................................................................)).%.) In2ut Variales...........................................................................................................)+).%. 7ni'or" Variales......................................................................................................)-
).%.+ /ut2ut Variales.........................................................................................................)-).%.& Bu''er Variales.........................................................................................................#).%.6 (hared Variales.........................................................................................................#).%.- Inter'ace Blocs..........................................................................................................%
).) Laout >uali'iers................................................................................................................&).).1 In2ut Laout >uali'iers...............................................................................................-
).).1.1 essellation Evaluation In2uts............................................................................+#).).1.# Geo"etr (hader In2uts......................................................................................+)).).1.% 5ra!"ent (hader In2uts......................................................................................+).).1.) $o"2ute (hader In2uts.......................................................................................++
).).# /ut2ut Laout >uali'iers............................................................................................+&
).).#.1 rans'or" 5eedac Laout >uali'iers..............................................................+-).).#.# essellation $ontrol /ut2uts..............................................................................&1).).#.% Geo"etr /ut2uts...............................................................................................).).#.) 5ra!"ent /ut2uts...............................................................................................&)
).).% 7ni'or" Variale Laout >uali'iers...........................................................................&).).) (uroutine 5unction Laout >uali'iers......................................................................&+).). 7ni'or" and (hader (tora!e Bloc Laout >uali'iers...............................................&+).).+ /2auali'iers....................................................................................61
). Inter2olation >uali'iers......................................................................................................6%
)..1 Redeclarin! Built9in Inter2olation Variales in the $o"2atiilit 4ro'ile.................6)).+ 4ara"eter >uali'iers...........................................................................................................6).& 4recision and 4recision >uali'iers.....................................................................................6
iv
-
8/12/2019 GLSLangSpec.4.40
5/207
).&.1 Ran!e and 4recision...................................................................................................6).&.# 4recision >uali'iers....................................................................................................6+).&.% De'ault 4recision >uali'iers.......................................................................................6&
).&.) Availale 4recision >uali'iers....................................................................................66).6 Variance and the Invariant >uali'ier..................................................................................66
).6.1 he Invariant >uali'ier...............................................................................................66).6.# Invariance o' $onstant E32ressions...........................................................................6-
).- he 4recise >uali'ier..........................................................................................................6-).1* ;e"or >uali'iers...........................................................................................................-#).11 /rder and Re2etition o' >uali'ication..............................................................................-
/2erators and E32ressions........................................................................................................-+.1 /2erators............................................................................................................................-+.# Arra /2erations...............................................................................................................-&.% 5unction $alls....................................................................................................................-&
.) $onstructors.......................................................................................................................-&.).1 $onversion and (calar $onstructors..........................................................................-&.).# Vector and ;atri3 $onstructors.................................................................................-6.).% (tructure $onstructors..............................................................................................1**.).) Arra $onstructors...................................................................................................1*1
. Vector and (calar $o"2onents and Len!th.....................................................................1*1.+ ;atri3 $o"2onents..........................................................................................................1*%.& (tructure and Arra /2erations........................................................................................1*%.6 Assi!n"ents.....................................................................................................................1*).- E32ressions......................................................................................................................1*.1* Vector and ;atri3 /2erations........................................................................................1*6
.11 /ut9o'9Bounds Accesses................................................................................................1*-+ (tate"ents and (tructure.........................................................................................................11*
+.1 5unction De'initions.........................................................................................................111+.1.1 5unction $allin! $onventions..................................................................................11)+.1.# (uroutines...............................................................................................................11
+.# (election...........................................................................................................................11++.% Iteration............................................................................................................................11&+.) Ju"2s................................................................................................................................116
& Built9in Variales....................................................................................................................1#*&.1 Built9In Lan!ua!e Variales............................................................................................1#*
&.1.1 $o"2atiilit 4ro'ile Built9In Lan!ua!e Variales..................................................1#6
&.# $o"2atiilit 4ro'ile Verte3 (hader Built9In In2uts.......................................................1%1&.% Built9In $onstants............................................................................................................1%#
&.%.1 $o"2atiilit 4ro'ile Built9In $onstants..................................................................1%)
v
-
8/12/2019 GLSLangSpec.4.40
6/207
&.) Built9In 7ni'or" (tate.....................................................................................................1%)&.).1 $o"2atiilit 4ro'ile (tate.......................................................................................1%)
6 Built9in 5unctions...................................................................................................................1%6
6.1 An!le and ri!ono"etr 5unctions..................................................................................1%-6.# E32onential 5unctions......................................................................................................1)16.% $o""on 5unctions..........................................................................................................1)#6.) 5loatin!94oint 4ac and 7n2ac 5unctions.....................................................................1)&6. Geo"etric 5unctions........................................................................................................1)-6.+ ;atri3 5unctions..............................................................................................................116.& Vector Relational 5unctions.............................................................................................1%6.6 Inte!er 5unctions..............................................................................................................16.- e3ture 5unctions.............................................................................................................1&
6.-.1 e3ture >uer 5unctions..........................................................................................166.-.# e3el Loou2 5unctions...........................................................................................1+1
6.-.% e3ture Gather 5unctions.........................................................................................1+&6.-.) $o"2atiilit 4ro'ile e3ture 5unctions..................................................................1&*
6.1* Ato"ic9$ounter 5unctions.............................................................................................1.11 Ato"ic ;e"or 5unctions............................................................................................1.1# I"a!e 5unctions.............................................................................................................1&%6.1% 5ra!"ent 4rocessin! 5unctions......................................................................................1&&
6.1%.1 Derivative 5unctions..............................................................................................1&&6.1%.# Inter2olation 5unctions...........................................................................................1&6
6.1) 8oise 5unctions..............................................................................................................1&-6.1 Geo"etr (hader 5unctions...........................................................................................16*6.1+ (hader Invocation $ontrol 5unctions.............................................................................16#
6.1& (hader ;e"or $ontrol 5unctions................................................................................16%- (hadin! Lan!ua!e Gra""ar 'or $ore 4ro'ile........................................................................161* 8or"ative Re'erences...........................................................................................................#*1
vi
-
8/12/2019 GLSLangSpec.4.40
7/207
1 Introduction
his docu"ent s2eci'ies onl version ).)* o' the /2enGL (hadin! Lan!ua!e. It re
-
8/12/2019 GLSLangSpec.4.40
8/207
1 Introduction
1.1 Acknowledgments
his s2eci'ication is ased on the wor o' those who contriuted to 2ast versions o' the /2enGL
Lan!ua!e (2eci'ication, the /2enGL E( #.* Lan!ua!e (2eci'ication, and the 'ollowin! contriutors tothis version:
4at Brown, 8VIDIA
Je'' Bol=, 8VIDIA
5ran $hen
4ierre Boudier, A;D
4iers Daniell, 8VIDIA
$hris Dodd, 8VIDIA
8ic 0ae"el, 8VIDIA
Jason Green, ransGa"in!
Brent Inso, Intel
Jon Leech
Bill Licea9Kane, A;DDaniel Koch, ransGa"in!
Barthold Lichtenelt, 8VIDIA
Bruce ;err, AR;
Roert /hannessian
Acorn 4oole, 8VIDIA
$hristo2he Riccio, A;D
Kevin Ro!ovin
Ian Ro"anic, Intel
Gre! Roth, 8vidia
Graha" (ellers, A;D
Dave (hreiner, AR;
Jere" (and"el, A22le
Roert (i"2son, >ualco""Eric @erness, 8VIDIA
;ar oun!, A;D
2
-
8/12/2019 GLSLangSpec.4.40
9/207
1 Introduction
1.2 Changes
1.2.1 Changes since revision 7 of GLSL version 4.4
Bu! 1*))*: $lari' that a na"e collision etween "e"ers o' two anon"ous locs, or
etween a variale and a "e"er o' an anon"ous loc is an error.
Bu! 11**-: Re"oved packed'ro" the reserved word list.
Bu! 11#--: 5i3ed textureOffset 'or sampler2DArraySadow to tae a ivec2not a vec2C 'or
the o''set.
Bu! 11#*-: It is a co"2ile9ti"e error to use the sa"e loc na"e 'or "ore than one loc
declaration in the sa"e inter'ace within one shader, even i' the loc contents are identical.
Bu! 111**: (i"2li' state"ent o' what is written E"it(trea"Verte3C to ust sa all uilt9in
and user9de'ined out2ut variales.
Bu! 11*-+: !l?(a"2le;as can e si=ed to e no lar!er than the i"2le"entation9de2endent"a3i"u" sa"2le9"as.
Bu! 1*61#: ;issin! te3t: Added the 2hrase a 2air o' 1+9it si!ned inte!ersF when descriin!
unpackSnorm2x1!.
Bu! 1*6*): @hen a uni'or" laout location is used, it is not re
-
8/12/2019 GLSLangSpec.4.40
10/207
1 Introduction
Bu! 11%+#: @hen countin! locations consu"ed, clari' that the outer arra level 'or !eo"etr
shader in2uts, tessellation control shader in2uts and out2uts, and tessellation evaluation in2uts is
'irst re"oved e'ore countin!.
Bu! 1*&%&: (tate "ore clearl which t2es are ille!al 'or in2uts and out2uts.
Bu! 111&6: $orrect 'unction overloadin! e3a"2les, which were 'ro" a di''erent revision o' the
s2ec. than the current rules.
Bu! 1*-%: $lari' that within a declaration, i' inout is used, neither innor out"a e used,
and none o' these can e re2eated.
Bu! 11*#: ;ae t2e "atchin! across co"2ilation units in the sa"e 2ro!ra" a22l to all
declared variales not ust those staticall used, etc.C
Bu! 1*-)1: @hen accessin! the sa"epackedu''er across "ulti2le sta!es in the sa"e 2ro!ra",
it either wors or ou !et a lin error.
1.2.2 Changes since revision ! of GLSL version 4.4Deprecation
Bu! %6): 8oise is now
de'ined to return *, and
de2recated not re"ovedC.
"anges
Bu! 1*+#6: (uroutine arras now re
-
8/12/2019 GLSLangSpec.4.40
11/207
1 Introduction
Bu! 1*+6#: $lari' that uilt9in 'unctions with voidreturn or outar!u"ents are not included in
in the set o' constant e32ressions.
1.2." Su##ar$ of Changes fro# %evision & of GLSL 'ersion 4."Deprecations
he uilt9in noiseC 'unctions are de2recated. he are not re"oved, ut are de'ined to return
*.
"anges
Incor2orate the ARB?enhanced?laouts e3tension, which adds
co"2ile9ti"e constant e32ressions 'or laout
-
8/12/2019 GLSLangSpec.4.40
12/207
1 Introduction
Editorial chan!es around co"2ute shader !rou2 si=es 'or lan!ua!e consistenc within the s2ec.
and e3tensions.
Bu! 1*%#&: Editorial: (a character set is suset o' 7nicode, in 7596 encodin!.
Bu! 11#--: 5i3ed textureOffset 'or sampler2DArraySadow to tae a ivec2not a vec2C 'or
the o''set.
Bu! 1*))*: $lari' that a na"e collision etween "e"ers o' two anon"ous locs or a
variale and a "e"er o' an anon"ous loc is an error.
Bu! 1*+: $lari'ication that o2a
-
8/12/2019 GLSLangSpec.4.40
13/207
1 Introduction
Bu! 1*&%&: (tate "ore clearl which t2es are ille!al 'or in2uts and out2uts.
Bu! 111&6: $orrect 'unction overloadin! e3a"2les, which were 'ro" a di''erent revision o' the
s2ec. than the current rules.
Bu! 1*-%: $lari' that within a declaration, i' inout is used, neither innor out"a e used,
and none o' these can e re2eated.
1." Overvie(
his docu"ent descries (+e ,penL +ading Language/ version ).)*.
Inde2endent co"2ilation units written in this lan!ua!e are called s+aders. Aprogramis a set o' shaders
that are co"2iled and lined to!ether, co"2letel creatin! one or "ore o' the 2ro!ra""ale sta!es o' the
/2enGL 2i2eline. All the shaders 'or a sin!le 2ro!ra""ale sta!e "ust e within the sa"e 2ro!ra". A
co"2lete set o' 2ro!ra""ale sta!es can e 2ut into a sin!le 2ro!ra" or the sta!es can e 2artitioned
across "ulti2le 2ro!ra"s. he ai" o' this docu"ent is to thorou!hl s2eci' the 2ro!ra""in! lan!ua!e.
he /2enGL Gra2hics (ste" (2eci'ication will s2eci' the /2enGL entr 2oints used to "ani2ulate andco""unicate with 2ro!ra"s and shaders.
1.4 )rror *andling
$o"2ilers, in !eneral, acce2t 2ro!ra"s that are ill9'or"ed, due to the i"2ossiilit o' detectin! all ill9
'or"ed 2ro!ra"s. 4ortailit is onl ensured 'or well9'or"ed 2ro!ra"s, which this s2eci'ication
descries. $o"2ilers are encoura!ed to detect ill9'or"ed 2ro!ra"s and issue dia!nostic "essa!es, ut are
not re
-
8/12/2019 GLSLangSpec.4.40
14/207
1 Introduction
Gra2hics (ste" (2eci'ication 'or details on what causes de2recated lan!ua!e 'eatures to e acce2ted or
to return an error.
8
-
8/12/2019 GLSLangSpec.4.40
15/207
2 Overvie( of OpenGL Shading
he /2enGL (hadin! Lan!ua!e is actuall several closel related lan!ua!es. hese lan!ua!es are used
to create shaders 'or each o' the 2ro!ra""ale 2rocessors contained in the /2enGL 2rocessin! 2i2eline.
$urrentl, these 2rocessors are the verte3, tessellation control, tessellation evaluation, !eo"etr,
'ra!"ent, and co"2ute 2rocessors.
7nless otherwise noted in this 2a2er, a lan!ua!e 'eature a22lies to all lan!ua!es, and co""on usa!e will
re'er to these lan!ua!es as a sin!le lan!ua!e. he s2eci'ic lan!ua!es will e re'erred to the na"e o'
the 2rocessor the tar!et: verte3, tessellation control, tessellation evaluation, !eo"etr, 'ra!"ent, or
co"2ute.
;ost /2enGL state is not traced or "ade availale to shaders. 2icall, user9de'ined variales will eused 'or co""unicatin! etween di''erent sta!es o' the /2enGL 2i2eline. 0owever, a s"all a"ount o'
state is still traced and auto"aticall "ade availale to shaders, and there are a 'ew uilt9in variales 'or
inter'aces etween di''erent sta!es o' the /2enGL 2i2eline.
2.1 'erte- rocessor
he verte" processoris a 2ro!ra""ale unit that o2erates on inco"in! vertices and their associated data.
$o"2ilation units written in the /2enGL (hadin! Lan!ua!e to run on this 2rocessor are called verte"
s+aders. @hen a set o' verte3 shaders are success'ull co"2iled and lined, the result in a verte" s+ader
e"ecuta'lethat runs on the verte3 2rocessor.
he verte3 2rocessor o2erates on one verte3 at a ti"e. It does not re2lace !ra2hics o2erations that re
-
8/12/2019 GLSLangSpec.4.40
16/207
2 Overview of OpenGL Shading
written another invocation at an 2oint durin! the sa"e 2hase, or i' two invocations atte"2t to write
di''erent values to the sa"e 2er92atch out2ut in a sin!le 2hase.
2." Tessellation )valuation rocessorhe tessellation evaluation 2rocessor is a 2ro!ra""ale unit that evaluates the 2osition and other
attriutes o' a verte3 !enerated the tessellation 2ri"itive !enerator, usin! a 2atch o' inco"in! vertices
and their associated data. $o"2ilation units written in the /2enGL (hadin! Lan!ua!e to run on this
2rocessor are called tessellation evaluation shaders. @hen a set o' tessellation evaluation shaders are
success'ull co"2iled and lined, the result in a tessellation evaluation s+ader e"ecuta'le that runs on
the tessellation evaluation 2rocessor.
Each invocation o' the tessellation evaluation e3ecutale co"2utes the 2osition and attriutes o' a sin!le
verte3 !enerated the tessellation 2ri"itive !enerator. he e3ecutale can read the attriutes o' an
verte3 in the in2ut 2atch, 2lus the tessellation coordinate, which is the relative location o' the verte3 in the
2ri"itive ein! tessellated. he e3ecutale writes the 2osition and other attriutes o' the verte3.
2.4 Geo#etr$ rocessor
hegeometr processor is a 2ro!ra""ale unit that o2erates on data 'or inco"in! vertices 'or a 2ri"itive
asse"led a'ter verte3 2rocessin! and out2uts a se
-
8/12/2019 GLSLangSpec.4.40
17/207
2 Overview of OpenGL Shading
A co"2ute shader has access to "an o' the sa"e resources as 'ra!"ent and other shader 2rocessors,
includin! te3tures, u''ers, i"a!e variales, and ato"ic counters. It does not have an 2rede'ined in2uts
nor an 'i3ed9'unction out2uts. It is not 2art o' the !ra2hics 2i2eline and its visile side e''ects are
throu!h chan!es to i"a!es, stora!e u''ers, and ato"ic counters.
A co"2ute shader o2erates on a !rou2 o' wor ite"s called a wor !rou2. A wor !rou2 is a collection
o' shader invocations that e3ecute the sa"e code, 2otentiall in 2arallel. An invocation within a wor
!rou2 "a share data with other "e"ers o' the sa"e wor !rou2 throu!h shared variales and issue
"e"or and control arriers to snchroni=e with other "e"ers o' the sa"e wor !rou2.
11
-
8/12/2019 GLSLangSpec.4.40
18/207
" 0asics
".1 Character Set and hases of Co#pilation
he source character set used 'or the /2enGL shadin! lan!ua!es is 7nicode in the 7596 encodin!
sche"e. A'ter 2re2rocessin!, onl the 'ollowin! characters are allowed in the resultin! strea" o' GL(L
toens:
he letters a(), A(*+ and the underscore ?C.
he nu"ers,(-.
he s"ols 2eriod .C, 2lus C, dash (C, slash /C, asteris 0C, 2ercent C, an!led racets and
3C, s
-
8/12/2019 GLSLangSpec.4.40
19/207
3 !a"ic"
Details that 'ull de'ine source strin!s, co""ents, line nu"erin!, new line eli"ination, and
2re2rocessin! are all discussed in u2co"in! sections. (ections eond those descrie GL(L 2rocessin!.
".2 Source Stringshe source 'or a sin!le shader is an arra o' strin!s o' characters 'ro" the character set. A sin!le shader
is "ade 'ro" the concatenation o' these strin!s. Each strin! can contain "ulti2le lines, se2arated new
lines. 8o new lines need e 2resent in a strin! a sin!le line can e 'or"ed 'ro" "ulti2le strin!s. 8o new
lines or other characters are inserted the i"2le"entation when it concatenates the strin!s to 'or" a
sin!le shader. ;ulti2le shaders can e lined to!ether to 'or" a sin!le 2ro!ra".
Dia!nostic "essa!es returned 'ro" co"2ilin! a shader "ust identi' oth the line nu"er within a strin!
and which source strin! the "essa!e a22lies to. (ource strin!s are counted se
-
8/12/2019 GLSLangSpec.4.40
20/207
3 !a"ic"
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 2re2rocessor that 2rocesses the source strin!s as 2art o' the co"2ilation 2rocess. E3ce2t as
noted elow, it ehaves as the $MM standard 2re2rocessor see section 1* 8or"ative Re'erencesFC.
he co"2lete list o' 2re2rocessor directives is as 'ollows.
define
undef
ififdef
ifndef
else
elif
endif
error
!ragma
extension
version
line
he 'ollowin! o2erators are also availale
defined
Each nu"er si!n #C can e 2receded in its line onl s2aces or hori=ontal tas. It "a also e
'ollowed s2aces and hori=ontal tas, 2recedin! the directive. Each directive is ter"inated a new
line. 4re2rocessin! does not chan!e the nu"er or relative location o' new lines in a source strin!.
4re2rocessin! taes 2laces a'ter new lines have een re"oved the line9continuation character.
he nu"er si!n #C on a line itsel' is i!nored. An directive not listed aove will cause a dia!nostic
"essa!e and "ae the i"2le"entation treat the shader as ill9'or"ed.
#defineand #undef'unctionalit are de'ined as is standard 'or $MM 2re2rocessors 'or "acro de'initionsoth with and without "acro 2ara"eters.
he 'ollowin! 2rede'ined "acros are availale
14
-
8/12/2019 GLSLangSpec.4.40
21/207
3 !a"ic"
""#$%&""
""$#&""
""&*$+%""
L)will sustitute a deci"al inte!er constant that is one "ore than the nu"er o' 2recedin! new
lines in the current source strin!.
3)Lwill sustitute a deci"al inte!er constant that sas which source strin! nu"er is currentl
ein! 2rocessed.
VR),will sustitute a deci"al inte!er re'lectin! the version nu"er o' the /2enGL shadin!
lan!ua!e. he version o' the shadin! lan!ua!e descried in this docu"ent will haveVR),
sustitute the deci"al inte!er ))*.
All "acro na"es containin! two consecutive underscores $$ C are reserved 'or 'uture use as 2rede'ined
"acro na"es. All "acro na"es 2re'i3ed with GL?F GLF 'ollowed a sin!le underscoreC are also
reserved.
#if+ #ifdef+ #ifndef+ #else+ #elif+ and#endifare de'ined to o2erate as is standard 'or $MM 2re2rocessors.E32ressions 'ollowin! #ifand #elifare 'urther restricted to e32ressions o2eratin! on literal inte!er
constants, 2lus identi'iers consu"ed the definedo2erator. $haracter constants are not su22orted.
he o2erators availale are as 'ollows.
recedence Operator class Operators ssociativit$
1 hi!hestC 2arenthetical !rou2in! C 8A
# unar de'inedM 9 N O
Ri!ht to Le't
% "ulti2licative P Le't to Ri!ht
) additive M 9 Le't to Ri!ht
it9wise shi't QQ Le't to Ri!ht
+ relational Q QS S Le't to Ri!ht
& e
-
8/12/2019 GLSLangSpec.4.40
22/207
3 !a"ic"
he definedo2erator can e used in either o' the 'ollowin! was:
defined identifier
defined( identifier )
wo toens in a "acro can e concatenated into one toen usin! the toen 2astin! ##Co2erator, as is
standard 'or $MM 2re2rocessors. he result "ust e a valid sin!le toen, which will then e suect to
"acro e32ansion. hat is, "acro e32ansion ha22ens onl a'ter toen 2astin!. here are no other nu"er
si!n ased o2erators e.!., no # or#C, nor is there a si)eofo2erator.
he se"antics o' a22lin! o2erators to inte!er literals in the 2re2rocessor "atch those standard in the $M
M 2re2rocessor, not those in the /2enGL (hadin! Lan!ua!e.
4re2rocessor e32ressions will e evaluated accordin! to the ehavior o' the host 2rocessor, not the
2rocessor tar!eted the shader.
#error will cause the i"2le"entation to 2ut a co"2ile9ti"e dia!nostic "essa!e into the shader oects
in'or"ation lo! see section &.1# (hader and 4ro!ra" >ueriesF in the /2enGL Gra2hics (ste"
(2eci'ication 'or how to access a shader oects in'or"ation lo!C. he "essa!e will e the toens'ollowin! the #errordirective, u2 to the 'irst new line. he i"2le"entation "ust then consider the shader
to e ill9'or"ed.
#pragmaallows i"2le"entation de2endent co"2iler control. oens 'ollowin! #pragmaare not suect
to 2re2rocessor "acro e32ansion. I' an i"2le"entation does not reco!ni=e the toens 'ollowin!
#pragma, then it will i!nore that 2ra!"a. he 'ollowin! 2ra!"as are de'ined as 2art o' the lan!ua!e.
!ragma *,-.#
he S%DEF2ra!"a is used to reserve 2ra!"as 'or use 'uture revisions o' this lan!ua!e. 8o
i"2le"entation "a use a 2ra!"a whose 'irst toen is S%DEF.
!ragma o!timie(on)
!ragma o!timie(off)
can e used to turn o'' o2ti"i=ations as an aid in develo2in! and deu!!in! shaders. It can onl e used
outside 'unction de'initions. B de'ault, o2ti"i=ation is turned on 'or all shaders. he deu! 2ra!"a
!ragma debug(on)
!ragma debug(off)
can e used to enale co"2ilin! and annotatin! a shader with deu! in'or"ation, so that it can e used
with a deu!!er. It can onl e used outside 'unction de'initions. B de'ault, deu! is turned o''.
(haders should declare the version o' the lan!ua!e the are written to. he lan!ua!e version a shader is
written to is s2eci'ied
version number profileopt
where num'er"ust e a version o' the lan!ua!e, 'ollowin! the sa"e convention asVR),aove.
he directive #version GG,F is re
-
8/12/2019 GLSLangSpec.4.40
23/207
3 !a"ic"
s2eci' #version1** will e treated as tar!etin! version 1.** o' the /2enGL E( (hadin! Lan!ua!e.
(haders that s2eci' #version%** will e treated as tar!etin! version %.** o' the /2enGL E( (hadin!
Lan!ua!e.
I' the o2tionalpro&ile ar!u"ent is 2rovided, it "ust e the na"e o' an /2enGL 2ro'ile. $urrentl, there
are three choices:
core
com!atibilit0
es
Apro&ilear!u"ent can onl e used with version 1* or !reater. I' no 2ro'ile ar!u"ent is 2rovided and
the version is 1* or !reater, the de'ault is core. I' version %** is s2eci'ied, the 2ro'ile ar!u"ent is not
o2tional and "ust e es, or a co"2ile9ti"e error results. he Lan!ua!e (2eci'ication 'or the es2ro'ile is
s2eci'ied in he /2enGL E( (hadin! Lan!ua!e s2eci'ication.
(haders 'or the core or compatiility2ro'iles that declare di''erent versions can e lined to!ether.
0owever, es2ro'ile shaders cannot e lined with non9es2ro'ile shaders or with es2ro'ile shaders o' adi''erent version, or a lin9ti"e error will result. @hen linin! shaders o' versions allowed these rules,
re"ainin! lin9ti"e errors will e !iven as 2er the linin! rules in the GL(L version corres2ondin! to the
version o' the conte3t the shaders are lined under. (hader co"2ile9ti"e errors "ust still e !iven strictl
ased on the version declared or de'aulted toC within each shader.
7nless otherwise s2eci'ied, this s2eci'ication is docu"entin! the core 2ro'ile, and everthin! s2eci'ied 'or
the core 2ro'ile is also availale in the co"2atiilit 2ro'ile. 5eatures s2eci'ied as elon!in! s2eci'icall
to the co"2atiilit 2ro'ile are not availale in the core 2ro'ile.
here is a uilt9in "acro de'inition 'or each 2ro'ile the i"2le"entation su22orts. All i"2le"entations
2rovide the 'ollowin! "acro:
define .#"core"!rofile 1
I"2le"entations 2rovidin! the compatiility2ro'ile 2rovide the 'ollowin! "acro:
define .#"com!atibilit0"!rofile 1
I"2le"entations 2rovidin! the es2ro'ile 2rovide the 'ollowin! "acro:
define .#"es"!rofile 1
he #versiondirective "ust occur in a shader e'ore anthin! else, e3ce2t 'or co""ents and white s2ace.
17
-
8/12/2019 GLSLangSpec.4.40
24/207
3 !a"ic"
B de'ault, co"2ilers o' this lan!ua!e "ust issue co"2ile9ti"e le3ical and !ra""atical errors 'or shaders
that do not con'or" to this s2eci'ication. An e3tended ehavior "ust 'irst e enaled. Directives to
control the ehavior o' the co"2iler with res2ect to e3tensions are declared with the #extensiondirective
extension extension_name:behavior
extension all :behavior
where e"tensionnameis the na"e o' an e3tension. E3tension na"es are not docu"ented in this
s2eci'ication. he toen all"eans the ehavior a22lies to all e3tensions su22orted the co"2iler. he
'e+avior can e one o' the 'ollowin!
behavior )ffect
reuire Behave as s2eci'ied the e3tension e"tensionname.
Give a co"2ile9ti"e error on the #extension i' the e3tension e"tensionnameis not su22orted, or i' allis s2eci'ied.
enable Behave as s2eci'ied the e3tension e"tensionname.
@arn on the #extensioni' the e3tension e"tensionnameis not su22orted.
Give a co"2ile9ti"e error on the #extension i' allis s2eci'ied.
(arn Behave as s2eci'ied the e3tension e"tensionname, e3ce2t issue warnin!son an detectale use o' that e3tension, unless such use is su22orted otherenaled or re
-
8/12/2019 GLSLangSpec.4.40
25/207
3 !a"ic"
he initial state o' the co"2iler is as i' the directive
extension all 2 disable
was issued, tellin! the co"2iler that all error and warnin! re2ortin! "ust e done accordin! to thiss2eci'ication, i!norin! an e3tensions.
Each e3tension can de'ine its allowed !ranularit o' sco2e. I' nothin! is said, the !ranularit is a shader
that is, a sin!le co"2ilation unitC, and the e3tension directives "ust occur e'ore an non92re2rocessor
toens. I' necessar, the liner can en'orce !ranularities lar!er than a sin!le co"2ilation unit, in which
case each involved shader will have to contain the necessar e3tension directive.
;acro e32ansion is not done on lines containin! #extensionand #versiondirectives.
#line"ust have, a'ter "acro sustitution, one o' the 'ollowin! 'or"s:
line line
line line source-string-number
where line andsource-string-num'er are constant inte!er e32ressions. A'ter 2rocessin! this directiveincludin! its new lineC, the i"2le"entation will ehave as i' it is co"2ilin! at line nu"er lineand source
strin! nu"ersource-string-num'er. (use
-
8/12/2019 GLSLangSpec.4.40
26/207
3 !a"ic"
".+ To3ens
he lan!ua!e, a'ter 2re2rocessin!, is a se
-
8/12/2019 GLSLangSpec.4.40
27/207
3 !a"ic"
lowp mediump igp precision
sampler1D sampler2D samplerHD sampler"ue
sampler1DSadow sampler2DSadow sampler"ueSadow sampler1DArray sampler2DArray
sampler1DArraySadow sampler2DArraySadow
isampler1D isampler2D isamplerHD isampler"ue
isampler1DArray isampler2DArray
usampler1D usampler2D usamplerHD usampler"ue
usampler1DArray usampler2DArray
sampler2DIect sampler2DIectSadow isampler2DIect usampler2DIect
samplerJuffer isamplerJuffer usamplerJuffer
sampler2DKS isampler2DKS usampler2DKS sampler2DKSArray isampler2DKSArray usampler2DKSArray
sampler"ueArray sampler"ueArraySadow isampler"ueArray usampler"ueArray
image1D iimage1D uimage1D
image2D iimage2D uimage2D
imageHD iimageHD uimageHD
image2DIect iimage2DIect uimage2DIect
image"ue iimage"ue uimage"ue
imageJuffer iimageJuffer uimageJuffer
image1DArray iimage1DArray uimage1DArray
image2DArray iimage2DArray uimage2DArray
image"ueArray iimage"ueArray uimage"ueArray
image2DKS iimage2DKS uimage2DKS
image2DKSArray iimage2DKSArray uimage2DKSArray
struct
he 'ollowin! are the ewords reserved 'or 'uture use. 7sin! the" will result in a co"2ile9ti"e error:
common partition active
asm
class union enum typedef template tis
resource
21
-
8/12/2019 GLSLangSpec.4.40
28/207
3 !a"ic"
goto
inline noinline pulic static extern external interface
long sort alf fixed unsigned superp input output
vec2 vecH vecG fvec2 fvecH fvecG
samplerHDIect
filter
si)eof cast
namespace using
In addition, all identi'iers containin! two consecutive underscores $$C are reserved as 2ossile 'uture
ewords.
".7 Identifiers
Identi'iers are used 'or variale na"es, 'unction na"es, structure na"es, and 'ield selectors 'ield
selectors select co"2onents o' vectors and "atrices si"ilar to structure "e"ers, as discussed in section
. Vector and (calar $o"2onentsF and section .+ ;atri3 $o"2onentsF C. Identi'iers have the 'or"
identi&ier
nondigit
identi&ier nondigit
identi&ier digit
nondigit: one o'
$ a c d e f g i L k l m n o p M r s t u v w x y )
A J " D & N E P Q R F K O T U I S % V ' W X Y *
digit: one o'
, 1 2 H G Z ! [ \ -
Identi'iers startin! with !l?F are reserved 'or use /2enGL, and "a not e declared in a shader as
either a variale or a 'unction this results in a co"2ile9ti"e error. 0owever, as noted in the s2eci'ication,
there are so"e cases where 2reviousl declared variales can e redeclared, and 2redeclared W!l?W na"es
are allowed to e redeclared in a shader onl 'or these s2eci'ic 2ur2oses. ;ore !enerall, it is a co"2ile9
ti"e error to redeclare a variale, includin! those startin! !l?F.
".5 ,efinitions
(o"e lan!ua!e rules descried elow de2end on the 'ollowin! de'initions.
22
-
8/12/2019 GLSLangSpec.4.40
29/207
3 !a"ic"
".5.1 Static 6se
A shader contains astatic useo' orstatic assignmenttoC a variale"i', a'ter 2re2rocessin!, the shader
contains a state"ent that would read or writeC", whether or not run9ti"e 'low o' control will cause that
state"ent to e e3ecuted.
".5.2 6nifor# and on86nifor# Control /lo(
@hen e3ecutin! state"ents in a 'ra!"ent shader, control 'low starts as uni&orm control &lo all 'ra!"ents
enter the sa"e control 2ath into mainC. $ontrol 'low eco"es non-uni&orm when di''erent 'ra!"ents
tae di''erent 2aths throu!h control9'low state"ents selection, iteration, and u"2sC. $ontrol 'low
suse
-
8/12/2019 GLSLangSpec.4.40
30/207
-
8/12/2019 GLSLangSpec.4.40
31/207
4 #aria$%e" and &'pe"
T$pe 9eaning
ivecH a three9co"2onent si!ned inte!er vector
ivecG a 'our9co"2onent si!ned inte!er vectoruvec2 a two9co"2onent unsi!ned inte!er vector
uvecH a three9co"2onent unsi!ned inte!er vector
uvecG a 'our9co"2onent unsi!ned inte!er vector
mat2 a #X# sin!le92recision 'loatin!92oint "atri3
matH a %X% sin!le92recision 'loatin!92oint "atri3
matG a )X) sin!le92recision 'loatin!92oint "atri3
mat2x2 sa"e as a mat2
mat2xH a sin!le92recision 'loatin!92oint "atri3 with # colu"ns and % rows
mat2xG a sin!le92recision 'loatin!92oint "atri3 with # colu"ns and ) rowsmatHx2 a sin!le92recision 'loatin!92oint "atri3 with % colu"ns and # rows
matHxH sa"e as a matH
matHxG a sin!le92recision 'loatin!92oint "atri3 with % colu"ns and ) rows
matGx2 a sin!le92recision 'loatin!92oint "atri3 with ) colu"ns and # rows
matGxH a sin!le92recision 'loatin!92oint "atri3 with ) colu"ns and % rows
matGxG sa"e as a matG
dmat2 a #X# doule92recision 'loatin!92oint "atri3
dmatH a %X% doule92recision 'loatin!92oint "atri3
dmatG a )X) doule92recision 'loatin!92oint "atri3dmat2x2 sa"e as a dmat2
dmat2xH a doule92recision 'loatin!92oint "atri3 with # colu"ns and % rows
dmat2xG a doule92recision 'loatin!92oint "atri3 with # colu"ns and ) rows
dmatHx2 a doule92recision 'loatin!92oint "atri3 with % colu"ns and # rows
dmatHxH sa"e as a dmatH
dmatHxG a doule92recision 'loatin!92oint "atri3 with % colu"ns and ) rows
dmatGx2 a doule92recision 'loatin!92oint "atri3 with ) colu"ns and # rows
dmatGxH a doule92recision 'loatin!92oint "atri3 with ) colu"ns and % rows
dmatGxG sa"e as a dmatG
25
-
8/12/2019 GLSLangSpec.4.40
32/207
4 #aria$%e" and &'pe"
5loatin!94oint /2a
-
8/12/2019 GLSLangSpec.4.40
33/207
-
8/12/2019 GLSLangSpec.4.40
34/207
4 #aria$%e" and &'pe"
T$pe 9eaning
usamplerJufferuimageJuffer
a handle 'or accessin! an unsi!ned inte!er u''er te3ture
usampler2DKS
uimage2DKS
a handle 'or accessin! an unsi!ned inte!er #D "ulti9sa"2le te3ture
usampler2DKSArrayuimage2DKSArray
a handle 'or accessin! an unsi!ned inte!er #D "ulti9sa"2le te3turearra
usampler"ueArrayuimage"ueArray
a handle 'or accessin! an unsi!ned inte!er cue "a2 arra te3ture
In addition, a shader can a!!re!ate these asic t2es usin! arras and structures to uild "ore co"2le3
t2es.
here are no 2ointer t2es.
4.1.1 'oid
5unctions that do not return a value "ust e declared as void. here is no de'ault 'unction return t2e.
he eword voidcannot e used in an other declarations e3ce2t 'or e"2t 'or"al or actual 2ara"eter
listsC, or a co"2ile9ti"e error results.
4.1.2 0ooleans
o "ae conditional e3ecution o' code easier to e32ress, the t2e oolis su22orted. here is no
e32ectation that hardware directl su22orts variales o' this t2e. It is a !enuine Boolean t2e, holdin!
onl one o' two values "eanin! either true or 'alse. wo ewords trueand falsecan e used as literal
Boolean constants. Booleans are declared and o2tionall initiali=ed as in the 'ollow e3a"2le:
bool success; // declare success to be a :ooleanbool done 4 false; // declare and initialie done
he ri!ht side o' the assi!n"ent o2erator > C "ust e an e32ression whose t2e is ool.
E32ressions used 'or conditional u"2s if+ for+ B@+ wile+ do(wileC "ust evaluate to the t2e ool.
4.1." Integers
(i!ned and unsi!ned inte!er variales are 'ull su22orted. In this docu"ent, the ter" integer is "eant to
!enerall include oth si!ned and unsi!ned inte!ers. 7nsi!ned inte!ers have e3actl %# its o' 2recision.
(i!ned inte!ers use %# its, includin! a si!n it, in twoHs co"2le"ent 'or". Addition, sutraction, and
shi't o2erations resultin! in over'low or under'low will not cause an e3ce2tion, nor will the saturate,
rather the will wra2F to ield the low9order %# its o' the result. Division and "ulti2lication o2erations
resultin! in over'low or under'low will not cause an e3ce2tion ut will result in an unde'ined value.
Inte!ers are declared and o2tionall initiali=ed with inte!er e32ressions, as in the 'ollowin! e3a"2le:
int i8 4 u; // u establishes the t0!e as uint
28
-
8/12/2019 GLSLangSpec.4.40
35/207
4 #aria$%e" and &'pe"
Literal inte!er constants can e e32ressed in deci"al ase 1*C, octal ase 6C, or he3adeci"al ase 1+C
as 'ollows.
integer-constant :
decimal-constant integer-su&&i"opt
octal-constant integer-su&&i"opt
+e"adecimal-constant integer-su&&i"opt
integer-su&&i": one o'
u V
decimal-constant :
non5ero-digit
decimal-constant digit
octal-constant :
,
octal-constant octal-digit
+e"adecimal-constant :
*3+e"adecimal-digit
*Y+e"adecimal-digit
+e"adecimal-constant +e"adecimal-digit
digit :
,
non5ero-digit
non5ero-digit : one o'
1 2 H G Z ! [ \ -
octal-digit@ one o'
, 1 2 H G Z ! [
+e"adecimal-digit@one o'
, 1 2 H G Z ! [ \ -
a c d e f
A J " D & N
8o white s2ace is allowed etween the di!its o' an inte!er constant, includin! a'ter the leadin! ,or a'ter
the leadin! ,xor ,Xo' a constant, or e'ore the su''i3uorV. @hen toeni=in!, the "a3i"al toen
"atchin! the aove will e reco!ni=ed e'ore a new toen is started. @hen the su''i3 u or V is 2resent,
the literal has t2e uint,otherwise the t2e is int. A leadin! unar "inus si!n 9C is inter2reted as an
arith"etic unar ne!ation, not as 2art o' the constant. 0ence, literals the"selves are alwas e32ressedwith non9ne!ative snta3, thou!h the could result in a ne!ative value.
It is a co"2ile9ti"e error to 2rovide a literal inte!er whose it 2attern cannot 'it in %# its. he it 2attern
o' the literal is alwas used un"odi'ied. (o a si!ned literal whose it 2attern includes a set si!n it
creates a ne!ative value. 5or e3a"2le,
29
-
8/12/2019 GLSLangSpec.4.40
36/207
4 #aria$%e" and &'pe"
int a 4 ?xffffffff; // >= bits8 a gets the value 31
int b 4 ?xffffffff@; // &+2 can't convert uint to int
uint c 4 ?xffffffff; // >= bits8 c gets the value ?x
uint d 4 ?xffffffff@; // >= bits8 d gets the value ?xint e 4 31; // the literal is 18 then negation is !erformed8
// and the resulting non3literal >=3bit signed
// bit !attern of ?x is assigned8 giving e
// the value of 316
uint f 4 31u; // the literal is 1u8 then negation is !erformed8
// and the resulting non3literal >=3bit unsigned
// bit !attern of ?x is assigned8 giving f
// the value of ?x6
int g 4 >?????????; // a signed decimal literal taking >= bits8
// setting the sign bit8 g gets 31=A=3bit signed hexadecimal
int i 4 E?????????; // &+2 needs more than >= bits
int 4 ?x; // &+2 needs more than >= bits
int k 4 ?xF???????; // k gets 3=1
-
8/12/2019 GLSLangSpec.4.40
37/207
4 #aria$%e" and &'pe"
5loatin!92oint constants are de'ined as 'ollows.
&loating-constant :
&ractional-constant e"ponent-partopt
&loating-su&&i"opt
digit-se6uence e"ponent-part &loating-su&&i"opt
&ractional-constant :
digit-se6uence.digit-se6uence
digit-se6uence.
.digit-se6uence
e"ponent-part :
esignopt
digit-se6uence
Esignopt
digit-se6uence
sign : one o'
]digit-se6uence :
digit
digit-se6uence digit
&loating-su&&i": one o'
f N lf FN
A deci"al 2oint .C is not needed i' the e32onent 2art is 2resent. 8o white s2ace "a a22ear anwhere
within a 'loatin!92oint constant, includin! e'ore a su''i3. @hen toeni=in!, the "a3i"al toen "atchin!
the aove will e reco!ni=ed e'ore a new toen is started. @hen the su''i3 Wl'W or WL5W is 2resent, the
literal has t2e doule. /therwise, the literal has t2e float. A leadin! unar "inus si!n (C is inter2reted
as a unar o2erator and is not 2art o' the 'loatin!92oint constant.
4.1.+ 'ectors
he /2enGL (hadin! Lan!ua!e includes data t2es 'or !eneric #9, %9, and )9co"2onent vectors o'
'loatin!92oint values, inte!ers, or Booleans. 5loatin!92oint vector variales can e used to store colors,
nor"als, 2ositions, te3ture coordinates, te3ture loou2 results and the lie. Boolean vectors can e used
'or co"2onent9wise co"2arisons o' nu"eric vectors. (o"e e3a"2les o' vector declaration are:
vec= texcoord18 texcoord=;
vec> !osition;
vec< m0.:D;
ivec= texture#ooku!;
bvec> less;
Initiali=ation o' vectors can e done with constructors, which are discussed shortl.
4.1.! 9atrices
he /2enGL (hadin! Lan!ua!e has uilt9in t2es 'or #X#, #X%, #X), %X#, %X%, %X), )X#, )X%, and )X)
"atrices o' 'loatin!92oint nu"ers. ;atri3 t2es e!innin! with W"atW have sin!le92recision co"2onents
31
-
8/12/2019 GLSLangSpec.4.40
38/207
4 #aria$%e" and &'pe"
while "atri3 t2es e!innin! with Wd"atW have doule92recision co"2onents. he 'irst nu"er in the
t2e is the nu"er o' colu"ns, the second is the nu"er o' rows. I' there is onl one nu"er, the "atri3
is s o!tGatrix;
mat< view8 !roection;
matx= m; // a matrix with > columns and = rows
dmat< highHrecisionGH;
dmat=x< dm;
Initiali=ation o' "atri3 values is done with constructors descried in section .) $onstructorsF C in
colu"n9"aor order.
4.1.7 Opaue T$pes
he o2a
-
8/12/2019 GLSLangSpec.4.40
39/207
4 #aria$%e" and &'pe"
4.1.7.2 I#ages
I"a!e t2es are o2auali'iersF. @hen a!!re!ated into arras
within a shader, ato"ic counters can onl e inde3ed with a dna"icall uni'or" inte!ral e32ression,
otherwise results are unde'ined. ;e"ers o' structures cannot e declared as ato"ic counter t2es.
4.1.5 Structures
7ser9de'ined t2es can e created a!!re!atin! other alread de'ined t2es into a structure usin! the
structeword. 5or e3a"2le,
struct light 5
float intensit0;
vec> !osition;
9 lightar;
In this e3a"2le, lig+teco"es the na"e o' the new t2e, and lig+tVareco"es a variale o' t2e lig+t.
o declare variales o' the new t2e, use its na"e without the eword structC.
light lightar=;
33
-
8/12/2019 GLSLangSpec.4.40
40/207
4 #aria$%e" and &'pe"
;ore 'or"all, structures are declared as 'ollows. 0owever, the co"2lete correct !ra""ar is as !iven in
section - (hadin! Lan!ua!e Gra""arF .
struct-de&inition :
6uali&ieropt
structnameopt
8mem'er-list 9declaratorsopt
7
mem'er-list :
mem'er-declaration7
mem'er-declaration mem'er-list7
mem'er-declaration :
'asic-tpe declarators7
where nameeco"es the user9de'ined t2e, and can e used to declare variales to e o' this new t2e.
he nameshares the sa"e na"e s2ace as other variales, t2es, and 'unctions. All 2reviousl visile
variales, t2es, constructors, or 'unctions with that na"e are hidden. he o2tional 6uali&ieronl a22lies
to an declarators, and is not 2art o' the t2e ein! de'ined 'or name.
(tructures "ust have at least one "e"er declaration. ;e"er declarators "a contain 2recision
-
8/12/2019 GLSLangSpec.4.40
41/207
4 #aria$%e" and &'pe"
co"2ile9ti"e errors. It is le!al to declare an arra without a si=e and then later redeclare the sa"e na"e
as an arra o' the sa"e t2e and s2eci' a si=e. 0owever, unless noted otherwise, locs cannot e
redeclared an unsi=ed arra in a user9declared loc cannot e si=ed a loc redeclaration. It is a
co"2ile9ti"e error to declare an arra with a si=e, and then later in the sa"e shaderC inde3 the sa"e arrawith an inte!ral constant e32ression !reater than or eJ;
uniform vec< lightHositionIJ; // when the block is arra0ed8 all u will be the same sie8
// but not necessaril0 all v8 if sied d0namicall0
An arra t2e can e 'or"ed s2eci'in! a t2e 'ollowed s
-
8/12/2019 GLSLangSpec.4.40
42/207
4 #aria$%e" and &'pe"
float aIEJ 4 floatIEJ(>6JI=J 4 5 vec
-
8/12/2019 GLSLangSpec.4.40
43/207
4 #aria$%e" and &'pe"
deter"ined at run ti"e ased on the si=e o' the u''er oect 2rovidin! stora!e 'or the loc. 5or such
arras, the value returned the lengt "ethod will e unde'ined i' the arra is contained in an arra o'
shader stora!e locs that is inde3ed with a non9constant e32ression less than =ero or !reater than or
e
-
8/12/2019 GLSLangSpec.4.40
44/207
4 #aria$%e" and &'pe"
4.1.1 I#plicit Conversions
In so"e situations, an e32ression and its t2e will e i"2licitl converted to a di''erent t2e. he
'ollowin! tale shows all allowed i"2licit conversions:
T$pe of e-pression Can be i#plicitl$ converted to
int uint
int
uint
float
intuintfloat
doule
ivec2 uvec2
ivecH uvecH
ivecG uvecGivec2uvec2
vec2
ivecH
uvecH
vecH
ivecGuvecG
vecG
ivec2uvec2vec2
dvec2
ivecH
uvecHvecH
dvecH
ivecGuvecGvecG
dvecG
mat2 dmat2
matH dmatH
matG dmatG
mat2xH dmat2xH
mat2xG dmat2xG
matHx2 dmatHx2
matHxG dmatHxG
matGx2 dmatGx2
matGxH dmatGxH
38
-
8/12/2019 GLSLangSpec.4.40
45/207
4 #aria$%e" and &'pe"
here are no i"2licit arra or structure conversions. 5or e3a"2le, an arra o' intcannot e i"2licitl
converted to an arra o' float.
@hen an i"2licit conversion is done, it is not a re9inter2retation o' the e32ressionHs it 2attern, ut a
conversion o' its value to an e
-
8/12/2019 GLSLangSpec.4.40
46/207
4 #aria$%e" and &'pe"
A list o' initiali=ers enclosed in a "atchin! set o' curl races is a22lied to one a!!re!ate. his "a e
the variale ein! declared or an a!!re!ate contained in the variale ein! declared. Individual
initiali=ers 'ro" the initiali=er list are a22lied to the ele"ents"e"ers o' the a!!re!ate, in order.
I' the a!!re!ate has a vector t2e, initiali=ers 'ro" the list are a22lied to the co"2onents o' the vector, in
order, startin! with co"2onent *. he nu"er o' initiali=ers "ust "atch the nu"er o' co"2onents.
I' the a!!re!ate has a "atri3 t2e, initiali=ers 'ro" the list "ust e vector initiali=ers and are a22lied to
the colu"ns o' the "atri3, in order, startin! with colu"n *. he nu"er o' initiali=ers "ust "atch the
nu"er o' colu"ns.
I' the a!!re!ate has a structure t2e, initiali=ers 'ro" the list are a22lied to the "e"ers o' the structure,
in the order declared in the structure, startin! with the 'irst "e"er. he nu"er o' initiali=ers "ust
"atch the nu"er o' "e"ers.
A22lin! these rules, the 'ollowin! "atri3 declarations are e6x> c 4 5 vec>(?6?)8 vec>(16?)8 vec>(=6?)8 vec>(>6?) 9; // illegal
mat=x= d 4 5 16?8 ?6?8 ?6?8 16? 9; // illegal8 can't flatten nesting
struct 5
float a;
int b;
9 e 4 5 16=8 =8 > 9; // illegal
In all cases, the inner"ost initiali=er i.e., not a list o' initiali=ers enclosed in curl racesC a22lied to anoect "ust have the sa"e t2e as the oect ein! initiali=ed or e a t2e that can e converted to the
oectHs t2e accordin! to section ).1.1* WI"2licit $onversionsW. In the latter case, an i"2licit conversion
will e done on the initiali=er e'ore the assi!n"ent is done.
struct 5
float a;
int b;
9 e 4 5 16=8 = 9; // legal8 all t0!es match
4
-
8/12/2019 GLSLangSpec.4.40
47/207
4 #aria$%e" and &'pe"
struct 5
float a;
int b;
9 e 4 5 18 > 9; // legal8 first initialier is converted
All o' the 'ollowin! declarations result in a co"2ile9ti"e error.
int a 4 true; // illegal
vec< bI=J 4 5 vec(16?) 9; // illegal
struct *1 5
vec< a;
vec< b;
9;
struct 5
float s; float t;
9 dIJ 4 5 *1(vec
-
8/12/2019 GLSLangSpec.4.40
48/207
4 #aria$%e" and &'pe"
@ithin a declaration, the sco2e o' a na"e starts i""ediatel a'ter the initiali=er i' 2resent or i""ediatel
a'ter the na"e ein! declared i' not. (everal e3a"2les:
int x 4 1;
5
int x 4 =8 0 4 x; // 0 is initialied to =
9
struct *
5
int x;
9;
5
* * 4 *(?); // '*' is onl0 visible as a struct and constructor
*; // '*' is now visible as a variable
9
int x 4 x; // &rror if x has not been !reviousl0 defined6
// $f the !revious definition of x was in this
// same sco!e8 this causes a redeclaration error6
int f( /L nested sco!e begins here L/ int k)
5
int k 4 k K >; // redeclaration error of the name k
666
9
int f(int k)
5
5 int k 4 k K >; // =nd k is !arameter8 initialiing nested first k
int m 4 k // use of new k8 which is hiding the !arameter
9
9
5or oth for and wile loo2s, the su9state"ent itsel' does not introduce a new sco2e 'or variale na"es,
so the 'ollowin! has a redeclaration co"2ile9ti"e error:
for ( /L nested sco!e begins here L/ int i 4 ?; i 7 1?; iKK) 5
int i; // redeclaration error
9
42
-
8/12/2019 GLSLangSpec.4.40
49/207
4 #aria$%e" and &'pe"
he od o' a do(wileloo2 introduces a new sco2e lastin! onl etween the do and wile not includin!
the while test e32ressionC, whether or not the od is si"2le or co"2ound:
int i 4 1C;
do
int i 4
-
8/12/2019 GLSLangSpec.4.40
50/207
4 #aria$%e" and &'pe"
4." Storage ;ualifiers
Variale declarations "a have at "ost one stora!e
-
8/12/2019 GLSLangSpec.4.40
51/207
4 #aria$%e" and &'pe"
8ot all co"inations o'
-
8/12/2019 GLSLangSpec.4.40
52/207
4 #aria$%e" and &'pe"
0owever, the lowest 2recedence o2erators o' the se
-
8/12/2019 GLSLangSpec.4.40
53/207
4 #aria$%e" and &'pe"
$onsu"2tion errors are ased on static use onl. $o"2ilation "a !enerate a warnin!, ut not an error,
'or an dna"ic use the co"2iler can deduce that "i!ht cause consu"2tion o' unde'ined values.
(ee section & Built9in VarialesF 'or a list o' the uilt9in in2ut na"es.
Verte3 shader in2ut variales or attriutesC receive 2er9verte3 data. he are declared in a verte3 shader
with the in uali'iersF.
(o"e in2uts and out2uts are arraed, "eanin! that 'or an inter'ace etween two shader sta!es either the
in2ut or out2ut declaration re
-
8/12/2019 GLSLangSpec.4.40
54/207
4 #aria$%e" and &'pe"
out2uts, and tessellation evaluation in2uts all have an additional level o' arraness relative to other shader
in2uts and out2uts.
5or non9arraed inter'aces "eanin! arra di"ensionall stas the sa"e etween sta!esC, it is a lin9ti"e
error i' the in2ut variale is not declared with the sa"e t2e, includin! arra di"ensionalit, as the
"atchin! out2ut variale.
he lin9ti"e t2e9"atchin! rules a22l to all declared in2ut and out2ut variales, whether or not the are
used.
Additionall, tessellation evaluation shaders su22ort 2er92atch in2ut variales declared with the patc and
in m0Molor;
no!ers!ective centroid in vec= m0,exMoord;
he 'ra!"ent shader in2uts 'or" an inter'ace with the last active shader in the verte3 2rocessin! 2i2eline.
5or this inter'ace, the last active shader sta!e out2ut variales and 'ra!"ent shader in2ut variales o' the
sa"e na"e "ust "atch in t2e and
-
8/12/2019 GLSLangSpec.4.40
55/207
4 #aria$%e" and &'pe"
are "is"atches etween in2uts and out2uts on such inter'aces, the values 2assed across the inter'ace will
e 2artiall or co"2letel unde'ined. (haders can ensure "atches across such inter'aces either usin!
in2ut and out2ut laout uali'iersFC or usin! identical in2ut and out2ut declarations o' locs or variales. $o"2lete rules 'orinter'ace "atchin! etween 2ro!ra"s are 'ound in section &.).1 (hader Inter'ace ;atchin!F o' the
/2enGL Gra2hics (ste" (2eci'ication.
$o"2ute shaders do not 2er"it user9de'ined in2ut variales and do not 'or" a 'or"al inter'ace with an
other shader sta!e. (ee section &.1 Built9In VarialesF 'or a descri2tion o' uilt9in co"2ute shader in2ut
variales. All other in2ut to a co"2ute shader is retrieved e32licitl throu!h i"a!e loads, te3ture 'etches,
loads 'ro" uni'or"s or uni'or" u''ers, or other user su22lied code. Redeclaration o' uilt9in in2ut
variales in co"2ute shaders is not 2er"itted.
4.".+ 6nifor# 'ariables
he uniform
-
8/12/2019 GLSLangSpec.4.40
56/207
4 #aria$%e" and &'pe"
variales "ust e declared at !loal sco2e. Durin! shader e3ecution the will ehave as nor"al
un
-
8/12/2019 GLSLangSpec.4.40
57/207
4 #aria$%e" and &'pe"
variale is used as an l9value, it is a co"2ile9ti"e error i' the e32ression indicatin! the verte3 inde3 is not
the identi'iergl)nvocation)D.
he order o' e3ecution o' a tessellation control shader invocation relative to the other invocations 'or the
sa"e in2ut 2atch is unde'ined unless the uilt9in 'unction arrierCis used. his 2rovides so"e control
over relative e3ecution order. @hen a shader invocation calls arrierC, its e3ecution 2auses until all
other invocations have reached the sa"e 2oint o' e3ecution. /ut2ut variale assi!n"ents 2er'or"ed
an invocation e3ecuted 2rior to callin! arrierC will e visile to an other invocation a'ter the call to
arrierC returns.
Because tessellation control shader invocations e3ecute in unde'ined order etween arriers, the values o'
2er9verte3 or 2er92atch out2ut variales will so"eti"es e unde'ined. $onsider the e!innin! and end o'
shader e3ecution and each call to arrierC as snchroni=ation 2oints. he value o' an out2ut variale
will e unde'ined in an o' the three 'ollowin! cases:
1. At the e!innin! o' e3ecution.
#. At each snchroni=ation 2oint, unless
the value was well9de'ined a'ter the 2revious snchroni=ation 2oint and was not written aninvocation since, or
the value was written e3actl one shader invocation since the 2revious snchroni=ation2oint, or
the value was written "ulti2le shader invocations since the 2revious snchroni=ation 2oint,and the last write 2er'or"ed all such invocations wrote the sa"e value.
%. @hen read a shader invocation, i'
the value was unde'ined at the 2revious snchroni=ation 2oint and has not een writen thesa"e shader invocation since, or
the out2ut variale is written to an other shader invocation etween the 2revious and ne3tsnchroni=ation 2oints, even i' that assi!n"ent occurs in code 'ollowin! the read.
5ra!"ent out2uts out2ut 2er9'ra!"ent data and are declared usin! the out stora!e
-
8/12/2019 GLSLangSpec.4.40
58/207
4 #aria$%e" and &'pe"
4.".7 0uffer 'ariables
he ufferuali'iersF.
he uffer
-
8/12/2019 GLSLangSpec.4.40
59/207
4 #aria$%e" and &'pe"
here is a li"it to the total si=e o' all variales declared as shared in a sin!le 2ro!ra". his li"it,
e32ressed in units o' asic "achine units "a e deter"ined usin! the /2enGL A4I to
-
8/12/2019 GLSLangSpec.4.40
60/207
4 #aria$%e" and &'pe"
2es and declarators are the sa"e as 'or other in2ut, out2ut, uni'or", and u''er variale declarations
outside locs, with these e3ce2tions:
initiali=ers are not allowed
o2a
-
8/12/2019 GLSLangSpec.4.40
61/207
4 #aria$%e" and &'pe"
instance na"e, 2uttin! their "e"ers at the sa"e sco2in! level. @hen instance na"es are 2resent on
"atched loc na"es, it is allowed 'or the instance na"es to di''er the need not "atch 'or the locs to
"atch. 5urther"ore, i' a "atchin! loc is declared as an arra, then the arra si=es "ust also "atch or
'ollow arra "atchin! rules 'or the inter'ace etween a verte3 and a !eo"etr shaderC. An "is"atchwill !enerate a lin9ti"e error. A loc na"e is allowed to have di''erent de'initions in di''erent
inter'aces within the sa"e shader, allowin!, 'or e3a"2le, an in2ut loc and out2ut loc to have the
sa"e na"e.
I' an instance na"e instance-nameCis not used, the na"es declared inside the loc are sco2ed at the
!loal level and accessed as i' the were declared outside the loc. I' an instance na"e instance-nameC
is used, then it 2uts all the "e"ers inside a sco2e within its own na"e s2ace, accessed with the 'ield
selector . C o2erator analo!ousl to structuresC. 5or e3a"2le,
in #ight 5
vec< #ightHos;
vec> #ightMolor;
9;
in Molored,exture 5 vec< Molor;
vec= ,exMoord;
9 Gaterial; // instance name
vec> Molor; // different Molor than Gaterial6Molor
vec< #ightHos; // illegal8 alread0 defined
666
666 4 #ightHos; // accessing #ightHos
666 4 Gaterial6Molor; // accessing Molor in Molored,exture block
/utside the shadin! lan!ua!e i.e., in the A4IC, "e"ers are si"ilarl identi'ied e3ce2t the loc na"e is
alwas used in 2lace o' the instance na"e A4I accesses are to inter'aces, not to shadersC. I' there is no
instance na"e, then the A4I does not use the loc na"e to access a "e"er, ust the "e"er na"e.
@ithin an inter'ace, all declarations o' the sa"e !loal na"e "ust e 'or the sa"e oect and "ust "atch
in t2e and in whether the declare a variale or "e"er o' a loc with no instance na"e. he A4I also
needs this na"e to uni
-
8/12/2019 GLSLangSpec.4.40
62/207
4 #aria$%e" and &'pe"
out ertex 5
vec< Hosition; // DH$ transform/feedback will use ertex6Hosition
vec= ,exture;
9 Moords; // shader will use Moords6Hosition
out ertex= 5
vec< Molor; // DH$ will use Molor
float Molor=;
9;
// in same !rogram as ertex= above2
out ertex> 5
float $ntensit0;
vec< Molor; // &+8 name collision with Molor in ertex=
9;
float Molor=; // &+8 collides with Molor= in ertex=
5or locs declared as arras, the arra inde3 "ust also e included when accessin! "e"ers, as in this
e3a"2le
uniform ,ransform 5 // DH$ uses ,ransformI=J to refer to instance =
mat< GodeliewGatrix;
mat< GodeliewHroectionGatrix;
vec< aIJ; // arra0 will get im!licitl0 sied
float -eformation;
9 transformsIJ666 // if these are the onl0 two dereferences of 'a'8
666transformsI>J6aICJ666 // then 'a' must be sie F8 for all transformsIxJ
5or uni'or" or shader stora!e locs declared as an arra, each individual arra ele"ent corres2onds to a
se2arate u''er9oect ind ran!e, acin! one instance o' the loc. As the arra si=e indicates the
nu"er o' u''er oects needed, uni'or" and shader stora!e loc arra declarations "ust s2eci' an
arra si=e. A uni'or" or shader stora!e loc arra can onl e inde3ed with a dna"icall uni'or"
inte!ral e32ression, otherwise results are unde'ined.
@hen usin! /2enGL A4I entr 2oints to identi' the na"e o' an individual loc in an arra o' locs,
the na"e strin! "ust include an arra inde3 e.!., (rans&orm2;C. @hen usin! /2enGL A4I entr 2oints
to re'er to o''sets or other characteristics o' a loc "e"er, an arra inde3 "ust not e s2eci'ied e.!.,
(rans&orm.!odelVie!atri" C.
Geo"etr shader in2ut locs "ust e declared as arras and 'ollow the arra declaration and linin!
rules 'or all !eo"etr shader in2uts. All other in2ut and out2ut loc arras "ust s2eci' an arra si=e.
56
-
8/12/2019 GLSLangSpec.4.40
63/207
4 #aria$%e" and &'pe"
here are i"2le"entation de2endent li"its on the nu"er o' uni'or" locs and the nu"er o' shader
stora!e locs that can e used 2er sta!e. I' either li"it is e3ceeded, it will cause a lin9ti"e error.
4.4 La$out ;ualifiersLaout uali'iersF.
57
-
8/12/2019 GLSLangSpec.4.40
64/207
4 #aria$%e" and &'pe"
Fayout Uualifier Uualifier
Only
Pndividual
'ariale Jlock
Jlock
Kemer Allowed Pnterfaces
sared
packed
std1G,
stdGH,
Y Y
uniform/uffer
row$maLor
column$maLorY Y Y
inding > o2a Y
align > Y Y
location >
Y
uniformuffer and
suroutine variales
location > Y Y Y all inout, e3ce2t 'or
co"2utecomponent > Y Y
index > Y'ra!"ent out
and suroutine 'unctions
triangles
Muads
isolines
Y tessellation evaluation in
eMual$spacing
fractional$even$spacing
fractional$odd$spacing
Y tessellation evaluation in
cw
ccwY tessellation evaluation in
point$mode Y tessellation evaluation in
points Y !eo"etr inout
Zpoints [
lines
lines$adLacency
triangles
triangles$adLacency
Y !eo"etr in
invocations > Y !eo"etr in
origin$upper$left
pixel$center$integer
gl3rag#oord
onl 'ra!"ent in
early$fragment$tests Y
58
-
8/12/2019 GLSLangSpec.4.40
65/207
4 #aria$%e" and &'pe"
Fayout Uualifier Uualifier
Only
Pndividual
'ariale Jlock
Jlock
Kemer Allowed Pnterfaces
local$si)e$x >
local$si)e$y >
local$si)e$) >Y
co"2ute in
xf$uffer >
xf$stride >Y Y Y Y verte3, tessellation, and
!eo"etr outxf$offset > Y Y Y
vertices > Y tessellation control out
Zpoints [
line$strip
triangle$stripY
!eo"etr out
max$vertices > Y
stream > Y Y Y Y
dept$any
dept$greater
dept$less
dept$uncanged
gl3ragDept+
onl'ra!"ent out
4.4.1 Input La$out ;ualifiers
(o"e in2ut laout
-
8/12/2019 GLSLangSpec.4.40
66/207
4 #aria$%e" and &'pe"
attriute 'ro" which in2ut values are taen. 5or in2uts o' all other shader t2es, the location s2eci'ies a
vector nu"er that can e used to "atch a!ainst out2uts 'ro" a 2revious shader sta!e, even i' that shader
is in a di''erent 2ro!ra" oect.
he 'ollowin! lan!ua!e descries how "an locations are consu"ed a !iven t2e. 0owever, !eo"etr
shader in2uts, tessellation control shader in2uts and out2uts, and tessellation evaluation in2uts all have an
additional level o' arraness relative to other shader in2uts and out2uts. his outer arra level is re"oved
'ro" the t2e e'ore considerin! how "an locations the t2e consu"es.
I' a verte3 shader in2ut is an scalar or vector t2e, it will consu"e a sin!le location. I' a non9verte3
shader in2ut is a scalar or vector t2e other than dvecH or dvecG, it will consu"e a sin!le location, while
t2es dvecH or dvecG will consu"e two consecutive locations. In2uts o' t2e douleand dvec2 will
consu"e onl a sin!le location, in all sta!es.
I' the declared in2ut a'ter 2otentiall re"ovin! an outer arra level as ust descried aoveC is an arra o'
si=e n and each ele"ent taes m locations, it will e assi!ned m n consecutive locations startin! with the
location s2eci'ied. 5or e3a"2le,
la0out(location 4 B) in vec< colorsI>J;
will estalish that the shader in2ut colorsis assi!ned to vector location nu"ers +, &, and 6.
I' the declared in2ut is an n 3 m sin!le9 or doule92recision "atri3, it will e assi!ned "ulti2le locations
startin! with the location s2eci'ied. he nu"er o' locations assi!ned 'or each "atri3 will e the sa"e as
'or an n9ele"ent arra o' m9co"2onent vectors. 5or e3a"2le,
la0out(location 4 A) in mat< transformsI=J;
will estalish that shader in2ut trans&ormsis assi!ned to vector locations -91+, with trans&orms0;ein!
assi!ned to locations -91# and trans&orms1;ein! assi!ned to locations 1%91+.
I' the declared in2ut is a structure or loc, its "e"ers will e assi!ned consecutive locations in their
order o' declaration, with the 'irst "e"er assi!ned the location 2rovided in the laout
-
8/12/2019 GLSLangSpec.4.40
67/207
4 #aria$%e" and &'pe"
la0out(location 4 >) in struct * 5
vec> a; // gets location >
mat= b; // gets locations < and E
vec< cI=J; // gets locations B and C la0out (location 4 F) vec= D; // &+8 can't use on struct member
9 s;
la0out(location 4
vec< k; // &+8 location < alread0 used
9;
he nu"er o' in2ut locations availale to a shader is li"ited. 5or verte3 shaders, the li"it is the
advertised nu"er o' verte3 attriutes. 5or all other shaders, the li"it is i"2le"entation9de2endent and
"ust e no less than one 'ourth o' the advertised "a3i"u" in2ut co"2onent count. A 2ro!ra" will 'ail to
lin i' an attached shader uses a location !reater than or e
-
8/12/2019 GLSLangSpec.4.40
68/207
4 #aria$%e" and &'pe"
// a consumes com!onents = and > of location eIBJ;
// f consumes last com!onent of the same B slots
la0out(location 4 ?8 com!onent 4 >) in float fIBJ;
I' a22lin! this to an arra o' arras, all levels o' arraness are re"oved to !et to the ele"ents that are
assi!ned 2er location to the s2eci'ied co"2onent. hese non9arraed ele"ents will 'ill the locations in the
order s2eci'ied 'or arras o' arras in section ).1.- WArrasW.
It is a co"2ile9ti"e error to a22l the component
-
8/12/2019 GLSLangSpec.4.40
69/207
4 #aria$%e" and &'pe"
laout-6uali&ier-id
triangles
Muads
isolineseMual$spacing
fractional$even$spacing
fractional$odd$spacing
cw
ccw
point$mode
/ne suset o' these identi'iers,primitive mode, is used to s2eci' a tessellation 2ri"itive "ode to e used
the tessellation 2ri"itive !enerator. o s2eci' a 2ri"itive "ode, the identi'ier "ust e one o'
triangles, Muads, or isolines, which s2eci' that the tessellation 2ri"itive !enerator should sudivide a
trian!le into s"aller trian!les, a
-
8/12/2019 GLSLangSpec.4.40
70/207
4 #aria$%e" and &'pe"
4.4.1.2 Geo#etr$ Shader Inputs
Additional laout
-
8/12/2019 GLSLangSpec.4.40
71/207
4 #aria$%e" and &'pe"
he intrinsicall declared in2ut arraglin;will also e si=ed an in2ut 2ri"itive9laout declaration.
0ence, the e32ression
gl"in6length()
will return the value 'ro" the tale aove.
5or in2uts declared without an arra si=e, includin! intrinsicall declared in2uts i.e., glinC, a laout "ust
e declared e'ore an use o' the "ethod lengtor other an arra use that re
-
8/12/2019 GLSLangSpec.4.40
72/207
4 #aria$%e" and &'pe"
Redeclarations are done as 'ollows
in vec< gl"ragMoord; // redeclaration that changes nothing is allowed
// Dll the following are allowed redeclaration that change behavior
la0out(origin"u!!er"left) in vec< gl"ragMoord;
la0out(!ixel"center"integer) in vec< gl"ragMoord;
la0out(origin"u!!er"left8 !ixel"center"integer) in vec< gl"ragMoord;
I'gl3rag#oord is redeclared in an 'ra!"ent shader in a 2ro!ra", it "ust e redeclared in all the
'ra!"ent shaders in that 2ro!ra" that have a static use gl3rag#oord. All redeclarations o'
gl3rag#oord in all 'ra!"ent shaders in a sin!le 2ro!ra" "ust have the sa"e set o'
-
8/12/2019 GLSLangSpec.4.40
73/207
4 #aria$%e" and &'pe"
5or e3a"2le, the 'ollowin! declaration in a co"2ute shader
la0out (local"sie"x 4 >=8 local"sie"0 4 >=) in;
is used to declare a two9di"ensional co"2ute shader with a local si=e o' %# Y %# ele"ents, which is
e integer-constant-e"pression
he usa!e and rules 'or usin! the componentuali'iersW. Additionall, 'or 'ra!"ent
shader out2uts, i' two variales are 2laced within the sa"e location, the "ust have the sa"e underlin!
t2e 'loatin!92oint or inte!erC. 8o co"2onent aliasin! o' out2ut variales or "e"ers is allowed.
5ra!"ent shaders allow an additional index out2ut laout
-
8/12/2019 GLSLangSpec.4.40
74/207
4 #aria$%e" and &'pe"
la0out(location 4 >) out vec< color;
will estalish that the 'ra!"ent shader out2ut color is assi!ned to 'ra!"ent color % as the 'irst inde3 =eroC
in2ut to the lend e8 index 4 1) out vec< factor;
will estalish that the 'ra!"ent shader out2ut&actoris assi!ned to 'ra!"ent color % as the second inde3
oneC in2ut to the lend e
-
8/12/2019 GLSLangSpec.4.40
75/207
4 #aria$%e" and &'pe"
i' an two out2ut variales 'ro" the sa"e verte3 or tessellation shader sta!e are assi!ned to the
sa"e location.
5or 'ra!"ent shader out2uts, locations can e assi!ned usin! either a laout
-
8/12/2019 GLSLangSpec.4.40
76/207
4 #aria$%e" and &'pe"
la0out(xfb"buffer4=8 xfb"offset4?) out block 5 // block's buffer is =
la0out(xfb"buffer 4 =) vec< v; // oka08 matches the inherited =
la0out(xfb"buffer 4 >) vec< u; // &+8 mismatched buffer vec< w; // inherited
9;
la0out (xfb"offset41B) out vec< t; // initial default is buffer ?
la0out (xfb"buffer41) out; // new global default of 1
out block 5 // block has buffer 1
vec< x; // x has buffer 1 (not ca!tured)
la0out(xfb"buffer 4 1) vec< 0; // oka0 (not ca!tured)
la0out(xfb"buffer 4 ?) vec< ; // &+8 mismatched buffer
9;
la0out(xfb"offset4?) out vec< g; // g has buffer 1
la0out(xfb"buffer4=) out vec< h; // does not change global default
la0out(xfb"offset41B) out vec< ; // has buffer 1
8ote this "eans all "e"ers o' a loc that !o to a trans'or" 'eedac u''er will !o to the sa"e u''er.
It is a co"2ile9ti"e error to s2eci' an xf$ufferthat is !reater than the i"2le"entation9de2endent
constantgl!a"(rans&orm3eed'ack%u&&ers.
he xf$offset
-
8/12/2019 GLSLangSpec.4.40
77/207
4 #aria$%e" and &'pe"
"ulti2le o' ), or a co"2ile9ti"e or lin9ti"e error results. It is a co"2ile9ti"e or lin9ti"e error to have
an xf$offset that over'lows xf$stride, whether stated on declarations e'ore or a'ter the xf$stride, or
in di''erent co"2ilation units. @hile xf$stridecan e declared "ulti2le ti"es 'or the sa"e u''er, it is a
co"2ile9ti"e or lin9ti"e error to have di''erent values s2eci'ied 'or the stride 'or the sa"e u''er.
5or e3a"2le:
// buffer 1 has >=3b0te stride
la0out (xfb"buffer 4 18 xfb"stride 4 >=) out;
// same as !revious exam!le; order within la0out does not matter
la0out (xfb"stride 4 >=8 xfb"buffer 4 1) out;
// ever0thing in this block goes to buffer ?
la0out (xfb"buffer 4 ?8 xfb"stride 4 >=) out block1 5
la0out (xfb"offset 4 ?) vec< a; // a goes to b0te offset ? of buffer ?
la0out (xfb"offset 4 1B) vec< b; // b goes to offset 1B of buffer ?9;
la0out (xfb"buffer 4 >8 xfb"offset 4 1=) out block= 5
vec< v; // v will be written to b0te offsets 1= through =C of buffer
float u; // u will be written to offset =F
la0out(xfb"offset 4
-
8/12/2019 GLSLangSpec.4.40
78/207
4 #aria$%e" and &'pe"
declaration. he out2ut laout integer-constant-e"pression
he identi'ier verticess2eci'ies the nu"er o' vertices in the out2ut 2atch 2roduced the tessellation
control shader, which also s2eci'ies the nu"er o' ti"es the tessellation control shader is invoed. It is a
co"2ile9 or lin9ti"e error 'or the out2ut verte3 count to e less than or e integer-constant-e"pression
he 2ri"itive t2e identi'iers points, line$strip, and triangle$stripare used to s2eci' the t2e o' out2ut
2ri"itive 2roduced the !eo"etr shader, and onl one o' these is acce2ted. At least one !eo"etr
shader co"2ilation unitC in a 2ro!ra" "us