Upload
mushakk
View
232
Download
1
Embed Size (px)
Citation preview
8/12/2019 GLSL Specification 3.30.6.Clean
1/123
The OpenGLShading Language
Language Version: 3.30
Document Revision: 6
11-Mar-2010
Editor: John Kessenich, Intel
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
8/12/2019 GLSL Specification 3.30.6.Clean
2/123
Copyright (c) 2008-2010 The Khronos Group Inc. All Rights Reserve.
This speci!ic"tion is protecte #y copyright l"$s "n cont"ins %"teri"l propriet"ry to the Khronos Group&Inc. It or "ny co%ponents %"y not #e reprouce& repu#lishe& istri#ute& tr"ns%itte& ispl"yero"c"st or other$ise e'ploite in "ny %"nner $ithout the e'press prior $ritten per%ission o! KhronosGroup. ou %"y use this speci!ic"tion !or i%ple%enting the !unction"lity therein& $ithout "ltering orre%oving "ny tr"e%"r& copyright or other notice !ro% the speci!ic"tion& #ut the receipt or possession o!this speci!ic"tion oes not convey "ny rights to reprouce& isclose& or istri#ute its contents& or to%"nu!"cture& use& or sell "nything th"t it %"y escri#e& in $hole or in p"rt.
Khronos Group gr"nts e'press per%ission to "ny current *ro%oter& Contri#utor or Aopter %e%#er o!Khronos to copy "n reistri#ute +,/II/ versions o! this speci!ic"tion in "ny !"shion& provie th"t, CARG is %"e !or the speci!ic"tion "n the l"test "v"il"#le up"te o! the speci!ic"tion !or "nyversion o! the A*I is use $henever possi#le. 3uch istri#ute speci!ic"tion %"y #e re-!or%"tte A34,G A3 the contents o! the speci!ic"tion "re not ch"nge in "ny $"y. The speci!ic"tion %"y #eincorpor"te into " prouct th"t is sol "s long "s such prouct inclues signi!ic"nt inepenent $orevelope #y the seller. A lin to the current version o! this speci!ic"tion on the Khronos Group $e#-siteshoul #e inclue $henever possi#le $ith speci!ic"tion istri#utions.
Khronos Group %"es no& "n e'pressly iscl"i%s "ny& represent"tions or $"rr"nties& e'press ori%plie& reg"ring this speci!ic"tion& incluing& $ithout li%it"tion& "ny i%plie $"rr"nties o! %erch"nt"#ilityor !itness !or " p"rticul"r purpose or non-in!ringe%ent o! "ny intellectu"l property. Khronos Group %"es
no& "n e'pressly iscl"i%s "ny& $"rr"nties& e'press or i%plie& reg"ring the correctness& "ccur"cy&co%pleteness& ti%eliness& "n reli"#ility o! the speci!ic"tion. +ner no circu%st"nces $ill the KhronosGroup& or "ny o! its *ro%oters& Contri#utors or e%#ers or their respective p"rtners& o!!icers& irectors&e%ployees& "gents or represent"tives #e li"#le !or "ny "%"ges& $hether irect& inirect& speci"l orconse5uenti"l "%"ges !or lost revenues& lost pro!its& or other$ise& "rising !ro% or in connection $iththese %"teri"ls.
Khronos& penK/& penKG3& pen6G& penA7& pen34 3 "n pen "re tr"e%"rs o!the Khronos Group Inc. C44A/A is " tr"e%"r o! 3ony Co%puter ntert"in%ent Inc. use #yper%ission #y Khronos. penG4 "n pen4 "re registere tr"e%"rs "n the penG4 3 logo is "tr"e%"r o! 3ilicon Gr"phics Inc. use #y per%ission #y Khronos. All other prouct n"%es& tr"e%"rs&"n9or co%p"ny n"%es "re use solely !or ienti!ic"tion "n #elong to their respective o$ners.
2
8/12/2019 GLSL Specification 3.30.6.Clean
3/123
Table of Contents
1 Introduction.................................................................................................................................1
1.1 Acknowledgents................................................................................................................11.! "hanges ............................................................................................................................... 1
1.!.1 #uar$ o% "hanges %ro Version 1.&'..................................................................... 11.( )verview.............................................................................................................................. !1.* Error +andling......................................................................................................................!1.& $-ogra-hical "onventions................................................................................................. !1. De-recation.......................................................................................................................... !
! )verview o% )-en/0 #hading....................................................................................................(!.1 Verte 2rocessor.................................................................................................................. (!.! /eoetr$ 2rocessor............................................................................................................. (!.( 3ragent 2rocessor.............................................................................................................. (
( Basics.......................................................................................................................................... &(.1 "haracter #et........................................................................................................................ &(.! #ource #trings...................................................................................................................... &(.( 2re-rocessor......................................................................................................................... (.* "oents.......................................................................................................................... 11(.& okens................................................................................................................................ 1!(. Ke$words............................................................................................................................1!(.4 Identi%iers........................................................................................................................... 1*(.5 #tatic 6se........................................................................................................................... 1*
* Varia7les and $-es..................................................................................................................1&*.1 Basic $-es........................................................................................................................ 1&
*.1.1 Void............................................................................................................................ 15*.1.! Booleans..................................................................................................................... 15*.1.( Integers....................................................................................................................... 15*.1.* 3loats.......................................................................................................................... !'*.1.& Vectors........................................................................................................................!1*.1. 8atrices...................................................................................................................... !1*.1.4 #a-lers..................................................................................................................... !1*.1.5 #tructures....................................................................................................................!!*.1.9 Arra$s......................................................................................................................... !(*.1.1' I-licit "onversions................................................................................................ !&
*.! #co-ing...............................................................................................................................!&
*.( #torage uali%iers...............................................................................................................!4*.(.1 De%ault #torage uali%ier............................................................................................!5*.(.! "onstant uali%ier...................................................................................................... !5*.(.( "onstant E-ressions................................................................................................. !5*.(.* In-uts.......................................................................................................................... !9*.(.& 6ni%or...................................................................................................................... ('
3
8/12/2019 GLSL Specification 3.30.6.Clean
4/123
*.(. )ut-uts....................................................................................................................... (1*.(.4 Inter%ace Blocks..........................................................................................................(!*.(.5 0a$out uali%iers........................................................................................................(&
*.(.5.1 In-ut 0a$out uali%iers.......................................................................................(&*.(.5.! )ut-ut 0a$out uali%iers.................................................................................... (5*.(.5.( 6ni%or Block 0a$out uali%iers.......................................................................(9
*.(.9 Inter-olation................................................................................................................*1*.(.9.1 Redeclaring Built;in Inter-olation Varia7les in the "o-ati7ilit$ 2ro%ile.........*!
*.* 2araeter uali%iers...........................................................................................................*!*.& 2recision and 2recision uali%iers..................................................................................... *(
*.&.1 Range and 2recision................................................................................................... *(*.&.! 2recision uali%iers.................................................................................................... *(*.&.( De%ault 2recision uali%iers....................................................................................... *(*.&.* Availa7le 2recision uali%iers....................................................................................**
*. Variance and the Invariant uali%ier.................................................................................. ***..1 he Invariant uali%ier............................................................................................... *&*..! Invariance o% "onstant E-ressions........................................................................... *
*.4 )rder o% uali%ication........................................................................................................ *& )-erators and E-ressions........................................................................................................*4
&.1 )-erators............................................................................................................................ *4&.! Arra$ )-erations............................................................................................................... *5&.( 3unction "alls.................................................................................................................... *5&.* "onstructors....................................................................................................................... *5
&.*.1 "onversion and #calar "onstructors.......................................................................... *5&.*.! Vector and 8atri "onstructors................................................................................. *9
&.*.( #tructure "onstructors................................................................................................&1&.*.* Arra$ "onstructors..................................................................................................... &!
&.& Vector "o-onents............................................................................................................&!&. 8atri "o-onents............................................................................................................&*&.4 #tructure and Arra$ )-erations..........................................................................................&*&.5 Assignents....................................................................................................................... &&&.9 E-ressions........................................................................................................................ &&.1' Vector and 8atri )-erations..........................................................................................&9
#tateents and #tructure...........................................................................................................1.1 3unction De%initions...........................................................................................................!
.1.1 3unction "alling "onventions....................................................................................(
.! #election.............................................................................................................................&.( Iteration.............................................................................................................................. .* Ju-s..................................................................................................................................4
4 Built;in Varia7les......................................................................................................................54.1 Verte and /eoetr$ #hader #-ecial Varia7les................................................................ 5
4.1.1 "o-ati7ilit$ 2ro%ile Verte and /eoetr$ #hader #-ecial Varia7les..................... 4'
4
8/12/2019 GLSL Specification 3.30.6.Clean
5/123
8/12/2019 GLSL Specification 3.30.6.Clean
6/123
8/12/2019 GLSL Specification 3.30.6.Clean
7/123
1 Introduction
his docuent s-eci%ies onl$ version (.(' o% the )-en/0 #hading 0anguage. It re=uires >>VER#I)>
to su7stitute ((', and re=uires #versionto acce-t onl$ (('. I% #version is declared with a saller
nu7er, the language acce-ted is a -revious version o% the shading language, which will 7e su--orted
de-ending on the version and t$-e o% contet in the )-en/0 A2I. #ee the )-en/0 /ra-hics #$ste
#-eci%ication, Version (.(, %or details on what language versions are su--orted.
All )-en/0 /ra-hics #$ste #-eci%ication re%erences in this s-eci%ication are to version (.(.
1.1 Acknowledgments
his s-eci%ication is 7ased on the work o% those who contri7uted to -ast versions o% the )-en/00anguage #-eci%ication, the )-en/0 E# !.' 0anguage #-eci%ication, and the %ollowing contri7utors to
this version:
2at Brown,
8/12/2019 GLSL Specification 3.30.6.Clean
8/123
1 Introduction
0a$out =uali%iers can declare the location o% %ragent shader out-uts.
Add ARB>shader>7it>encoding etension:
3loating;-oint varia7les within a shader are encoded according to IEEE 4&*.
Add 7uilt;in %unctions converting %loating;-oint values to and %ro signed or unsigned
integers re-resenting their encoding.
"hange #line7ehavior: he nu7er -rovided is the nu7er o% the net line o% code, not the
current line. his akes it atch " seantics.
"lari%$ that the second co-onent o%Pis unused %or 1Dshadow looku-s.
1." O#er#ie$
his docuent descri7es The Oen!L "ha#ing Language$ version 3.30.
Inde-endent co-ilation units written in this language are calledsha#ers. Arogramis a co-lete set o%
shaders that are co-iled and linked together. 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.
1.% &rror 'andling
"o-ilers, in general, acce-t -rogras that are ill;%ored, due to the i-ossi7ilit$ o% detecting all ill;
%ored -rogras. 2orta7ilit$ is onl$ ensured %or well;%ored -rogras, which this s-eci%ication
descri7es. "o-ilers are encouraged to detect ill;%ored -rogras and issue diagnostic essages, 7ut are
not re=uired to do so %or all cases. "o-ilers are re=uired to return essages regarding leicall$,
graaticall$, or seanticall$ incorrect shaders.
1. Typographical Con#entionsItalic, 7old, and %ont choices have 7een used in this s-eci%ication -riaril$ to i-rove reada7ilit$. "ode
%ragents use a %ied width %ont. Identi%iers e7edded in tet are italici?ed. Ke$words e7edded in tet
are 7old. )-erators are called 7$ their nae, %ollowed 7$ their s$7ol in 7old in -arentheses. he
clari%$ing graar %ragents in the tet use 7old %or literals and italics %or non;terinals. he o%%icial
graar in section 9 #hading 0anguage /raarC uses all ca-itals %or terinals and lower case %or
non;terinals.
1.( )eprecation
2revious versions o% the )-en/0 #hading 0anguage de-recated soe %eatures. hese are clearl$ called
out in this s-eci%ication as de-recatedC. he$ are still -resent in this version o% the language, 7ut are
targeted %or -otential reoval in a %uture version o% the shading language. he )-en/0 A2I has a
%orward co-ati7ilit$ 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 7e acce-ted or to return
an error.
2
8/12/2019 GLSL Specification 3.30.6.Clean
9/123
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 -rograa7le -rocessors contained in the )-en/0 -rocessing -i-eline.
"urrentl$, these -rocessors are the verte, geoetr$, and %ragent -rocessors.
6nless 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 7e re%erred to 7$ the nae o%
the -rocessor the$ target: verte, geoetr$, or %ragent.
8ost )-en/0 state is not tracked or ade availa7le to shaders. $-icall$, user;de%ined varia7les will 7e
used %or counicating 7etween di%%erent stages o% the )-en/0 -i-eline. +owever, a sall aount o%
state is still tracked and autoaticall$ ade availa7le to shaders, and there are a %ew 7uilt;in varia7les %orinter%aces 7etween di%%erent stages o% the )-en/0 -i-eline.
2.1 Verte* +rocessor
he verte% rocessoris a -rograa7le 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%
sha#ers. hen a co-lete set o% verte shaders are co-iled and linked, the$ result in a verte% sha#er
e%ecuta&'ethat runs on the verte -rocessor.
he verte -rocessor o-erates on one verte at a tie. It does not re-lace gra-hics o-erations that re=uire
knowledge o% several vertices at a tie.
2.2 Geometry +rocessorhegeometr( rocessor is a -rograa7le unit that o-erates on data %or incoing vertices %or a -riitive
asse7led a%ter verte -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 calledgeometr(
sha#ers. hen a co-lete set o% geoetr$ shaders are co-iled and linked, the$ result in ageometr(
sha#er e%ecuta&'e that runs on the geoetr$ -rocessor.
A single invocation o% the geoetr$ shader eecuta7le on the geoetr$ -rocessor will o-erate on a
declared in-ut -riitive with a %ied nu7er o% vertices. his single invocation can eit a varia7le
nu7er o% vertices that are asse7led into -riitives o% a declared out-ut -riitive t$-e and -assed to
su7se=uent -i-eline stages.
2." ,ragment +rocessorhe)ragment rocessoris a -rograa7le 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 sha#ers. hen a co-lete set o% %ragent shaders are co-iled and linked, the$ result in a
)ragment sha#er e%ecuta&'ethat runs on the %ragent -rocessor.
3
8/12/2019 GLSL Specification 3.30.6.Clean
10/123
2 Overview of OpenGL Shading
A %ragent shader cannot change a %ragents F%,(G -osition. Access to neigh7oring %ragents is not
allowed. he values co-uted 7$ the %ragent shader are ultiatel$ used to u-date %rae7u%%er eor$
or teture eor$, de-ending on the current )-en/0 state and the )-en/0 coand that caused the
%ragents to 7e generated.
4
8/12/2019 GLSL Specification 3.30.6.Clean
11/123
" -asics
".1 Character Set
he source character set used %or the )-en/0 shading languages is a su7set o% A#"II. It includes the
%ollowing characters:
he letters a-z, A-Z, and the underscore F >G.
he nu7ers0-9.
he s$7ols -eriod F.G, -lus F+G, dash F-G, slash F/G, asterisk FG, -ercent F!G, angled 7rackets F"and
G, s=uare 7rackets F $and % G, -arentheses F &and 'G, 7races F (and )G, caret F*G, vertical 7ar F G,
a-ersand FG, tilde FG, e=uals FG, eclaation -oint FG, colon FG, seicolon FG, coa F,G, and=uestion ark F2G.
he nu7er sign F#G %or -re-rocessor use.
hite s-ace: the s-ace character, hori?ontal ta7, vertical ta7, %or %eed, carriage;return, and line;
%eed.
0ines are relevant %or co-iler diagnostic essages and the -re-rocessor. he$ are terinated 7$
carriage;return or line;%eed. I% 7oth are used together, it will count as onl$ a single line terination. 3or
the reainder o% this docuent, an$ o% these co7inations is si-l$ re%erred to as a new;line. here is no
line continuation character.
In general, the languageHs use o% this character set is case sensitive.
here are no character or string data t$-es, so no =uoting characters are included.
here is no end;o%;%ile character.
".2 Source Strings
he source %or a single shader is an arra$ o% strings o% characters %ro the character set. A single shader
is ade %ro the concatenation o% these strings. Each string can contain ulti-le lines, se-arated 7$ new;
lines.
8/12/2019 GLSL Specification 3.30.6.Clean
12/123
3 Basics
"." +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
#pragma
#extension
#version
#line
he %ollowing o-erators are also availa7le
defined
##
Each nu7er sign F#G can 7e -receded in its line onl$ 7$ s-aces or hori?ontal ta7s. It a$ also 7e
%ollowed 7$ s-aces and hori?ontal ta7s, -receding the directive. Each directive is terinated 7$ a new;
line. 2re-rocessing does not change the nu7er or relative location o% new;lines in a source string.
he nu7er sign F#G on a line 7$ itsel% is ignored. An$ directive not listed a7ove will cause a diagnostic
essage and ake the i-leentation treat the shader as ill;%ored.
#de3ineand #4nde3%unctionalit$ are de%ined as is standard %or " -re-rocessors %or acro de%initions
7oth with and without acro -araeters.
he %ollowing -rede%ined acros are availa7le
__LINE__
__FILE__
__VERSION__
**L+,**will su7stitute a decial integer constant that is one ore than the nu7er o% -receding new;lines in the current source string.
**+L**will su7stitute a decial integer constant that sa$s which source string nu7er is currentl$
7eing -rocessed.
6
8/12/2019 GLSL Specification 3.30.6.Clean
13/123
3 Basics
**VR"+O,**will su7stitute a decial integer re%lecting the version nu7er o% the )-en/0 shading
language. he version o% the shading language descri7ed in this docuent will have**VR"+O,**
su7stitute the decial integer (('.
All acro naes containing two consecutive underscores F55 G are reserved %or %uture use as -rede%ined
acro naes. All acro naes -re%ied with /0>C F/0C %ollowed 7$ a single underscoreG are also
reserved.
#i3, #i3de3, #i3nde3, #else, #eli3, and#endi3are de%ined to o-erate as is standard %or " -re-rocessors.
E-ressions %ollowing #i3and #eli3are %urther restricted to e-ressions o-erating on literal integer
constants, -lus identi%iers consued 7$ the de3inedo-erator. It is an error to use #i3 or #eli3 on
e-ressions containing unde%ined acro naes, other than as arguents to the de3ined o-erator.
"haracter constants are not su--orted. he o-erators availa7le are as %ollows.
+recedence Operator class Operators ssociati#ity
1 FhighestG -arenthetical grou-ing F G
8/12/2019 GLSL Specification 3.30.6.Clean
14/123
3 Basics
2re-rocessor e-ressions will 7e evaluated according to the 7ehavior o% the host -rocessor, not the
-rocessor targeted 7$ the shader.
#error will cause the i-leentation to -ut a diagnostic essage into the shader o7TectHs in%oration log
Fsee section .1.11 #hader and 2rogra ueriesC in the )-en/0 /ra-hics #$ste #-eci%ication %or how
to access a shader o7TectHs in%oration logG. he essage will 7e the tokens %ollowing the #error
directive, u- to the %irst new;line. he i-leentation ust then consider the shader to 7e ill;%ored.
#7ragmaallows i-leentation de-endent co-iler control. okens %ollowing #7ragmaare not su7Tect
to -re-rocessor acro e-ansion. I% an i-leentation does not recogni?e the tokens %ollowing
#7ragma, then it will ignore that -raga. he %ollowing -ragas are de%ined as -art o% the language.
#pragma ST!L
he 8D:;-raga is used to reserve -ragas %or use 7$ %uture revisions o% this language.
8/12/2019 GLSL Specification 3.30.6.Clean
15/123
3 Basics
I% the o-tionalro)i'e arguent is -rovided, it ust 7e the nae o% an )-en/0 -ro%ile. "urrentl$, there
are two choices:
%ore
%ompati$ilit&
I% noro)i'e arguent is -rovided, the de%ault is core. 6nless 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 availa7le in the
co-ati7ilit$ -ro%ile. 3eatures s-eci%ied as 7elonging s-eci%icall$ to the co-ati7ilit$ -ro%ile are not
availa7le in the core -ro%ile.
here is a 7uilt;in acro de%inition %or each -ro%ile the i-leentation su--orts. All i-leentations
-rovide the %ollowing acro:
#define !L_%ore_profile '
I-leentations -roviding the co-ati7ilit$ -ro%ile -rovide the %ollowing acro:
#define !L_%ompati$ilit&_profile '
he #versiondirective ust occur in a shader 7e%ore an$thing else, ece-t %or coents and white s-ace.
!
8/12/2019 GLSL Specification 3.30.6.Clean
16/123
3 Basics
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$ etended 7ehavior ust %irst 7e ena7led.
Directives to control the 7ehavior o% the co-iler with res-ect to etensions are declared with the
#e=tensiondirective
#extension extension_name:behavior
#extension all :behavior
where e%tension*nameis the nae o% an etension. Etension naes are not docuented in this
s-eci%ication. he token alleans the 7ehavior a--lies to all etensions su--orted 7$ the co-iler. he
&ehavior can 7e one o% the %ollowing
behavior &ffect
re/uire Behave as s-eci%ied 7$ the etension e%tension*name.
/ive an error on the #e=tension i% the etension e%tension*name is notsu--orted, or i% allis s-eci%ied.
enable Behave as s-eci%ied 7$ the etension e%tension*name.
arn on the #e=tensioni% the etension e%tension*nameis not su--orted.
/ive an error on the #e=tension i% allis s-eci%ied.
$arn Behave as s-eci%ied 7$ the etension e%tension*name, ece-t issue warningson an$ detecta7le use o% that etension, unless such use is su--orted 7$ otherena7led or re=uired etensions.
I% all is s-eci%ied, then warn on all detecta7le uses o% an$ etension used.
arn on the #e=tensioni% the etension e%tension*nameis not su--orted.
disable Behave Fincluding issuing errors and warningsG as i% the etensione%tension*nameis not -art o% the language de%inition.
I% all is s-eci%ied, then 7ehavior ust revert 7ack to that o% the non;etendedcore version o% the language 7eing co-iled to.
arn on the #e=tension i% the etension e%tension*nameis not su--orted.
he e=tensiondirective is a si-le, low;level echanis to set the 7ehavior %or each etension. It does
not de%ine -olicies such as which co7inations are a--ro-riate, those ust 7e de%ined elsewhere. )rder
o% directives atters in setting the 7ehavior %or each etension: Directives that occur later override those
seen earlier. he allvariant sets the 7ehavior %or all etensions, overriding all -reviousl$ issued
e=tension directives, 7ut onl$ %or the &ehaviorswarnand disa>le.
1"
8/12/2019 GLSL Specification 3.30.6.Clean
17/123
3 Basics
he initial state o% the co-iler is as i% the directive
#extension all ( disa$le
was issued, telling the co-iler that all error and warning re-orting ust 7e done according to thiss-eci%ication, ignoring an$ etensions.
Each etension can de%ine its allowed granularit$ o% sco-e. I% nothing is said, the granularit$ is a shader
Fthat is, a single co-ilation unitG, and the etension directives ust occur 7e%ore an$ non;-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$ etension directive.
8acro e-ansion is not done on lines containing #e=tensionand #versiondirectives.
#lineust have, a%ter acro su7stitution, one o% the %ollowing %ors:
#line line
#line line source-string-number
where 'ine andsource-string-num&er are constant integer e-ressions. A%ter -rocessing this directiveFincluding its new;lineG, the i-leentation will 7ehave as i% it is co-iling at line nu7er 'ineand
source string nu7ersource-string-num&er. #u7se=uent source strings will 7e nu7ered se=uentiall$,
until another #linedirective overrides that nu7ering.
".% Comments
"oents are deliited 7$ @L and L@, or 7$ @@ and a new;line. he 7egin coent deliiters F@L or @@G are
not recogni?ed as coent deliiters inside o% a coent, hence coents cannot 7e nested. I% a
coent resides entirel$ within a single line, it is treated s$ntacticall$ as a single s-ace.
8/12/2019 GLSL Specification 3.30.6.Clean
18/123
3 Basics
". To0ens
he language is a se=uence o% tokens. A token can 7e
to/en:
/e(or#
i#enti)ier
integer-constant
)'oating-constant
oerator
( )
".( ey$ords
he %ollowing are the ke$words in the language, and cannot 7e used %or an$ other -ur-ose than that
de%ined 7$ this docuent:
attri>4te const 4ni3orm var?ing
la?o4t
centroid 3lat smoot@ no7ers7ective
>reak contin4e do 3or w@ile switc@ case de3a4lt
i3 else
in o4t ino4t
3loat int void >ool tr4e 3alse
invariant
discard ret4rn
mat mat< matB
mat= mat=< mat=B
matvecB
4int 4vec 4vec< 4vecB
low7 medi4m7 @ig@7 7recision
sam7ler1D sam7lerD sam7lere
sam7ler1D8@adow sam7lerD8@adow sam7lerC4>e8@adow sam7ler1DArra? sam7lerDArra?
sam7ler1DArra?8@adow sam7lerDArra?8@adow
isam7ler1D isam7lerD isam7lere
isam7ler1DArra? isam7lerDArra?
12
8/12/2019 GLSL Specification 3.30.6.Clean
19/123
3 Basics
4sam7ler1D 4sam7lerD 4sam7lere
4sam7ler1DArra? 4sam7lerDArra?
sam7lerDect sam7lerDect8@adow isam7lerDect 4sam7lerDect sam7lerE433er isam7lerE433er 4sam7lerE433er
sam7lerDF8 isam7lerDF8 4sam7lerDF8
sam7lerDF8Arra? isam7lerDF8Arra? 4sam7lerDF8Arra?
str4ct
he %ollowing are the ke$words reserved %or %uture use. 6sing the will result in an error:
common 7artition active
asm
class 4nion en4m t?7ede3 tem7late t@is 7acked
goto
inline noinline volatile 74>lic static e=tern e=ternal inter3ace
long s@ort do4>le @al3 3i=ed 4nsigned s47er7
in74t o4t74t
@vec @vec< @vecB dvec dvec< dvecB 3vec 3vec< 3vecB
sam7ler
8/12/2019 GLSL Specification 3.30.6.Clean
20/123
3 Basics
". Identifiers
Identi%iers are used %or varia7le naes, %unction naes, structure naes, and %ield selectors F%ield
selectors select co-onents o% vectors and atrices siilar to structure %ields, as discussed in section &.&Vector "o-onentsC and section &. 8atri "o-onentsC G. Identi%iers have the %or
i#enti)ier
non#igit
i#enti)ier non#igit
i#enti)ier #igit
non#igit: one o%
5 a > c d e 3 g @ i G k l m n o 7 H r s t 4 v w = ? z
A E C D I J : K L M N ; F O P Q R 8 S T U V W Z
#igit: one o%
0 1 < B X Y [ 9
Identi%iers starting with gl>C are reserved %or use 7$ )-en/0, and a$ not 7e declared in a shader as
either a varia7le or a %unction. +owever, as noted in the s-eci%ication, there are soe cases where
-reviousl$ declared varia7les can 7e redeclared to change or add soe -ro-ert$, and -redeclared Ugl>U
naes are allowed to 7e redeclared in a shader onl$ %or these s-eci%ic -ur-oses. 8ore generall$, it is an
error to redeclare a varia7le, including those starting gl>C.
".3 Static 4se
#oe language rules descri7ed 7elow de-end on whether soething isstatica''( written or used.
A shader contains astatic useo% Forstatic assignmenttoG a varia7le%i%, a%ter -re-rocessing, the shader
contains a stateent that would read For writeG%, whether or not run;tie %low o% control will cause that
stateent to 7e eecuted.
14
8/12/2019 GLSL Specification 3.30.6.Clean
21/123
% Variables and Types
All varia7les and %unctions ust 7e declared 7e%ore 7eing used. Varia7le and %unction naes are
identi%iers.
here are no de%ault t$-es. All varia7le and %unction declarations ust have a declared t$-e, and
o-tionall$ =uali%iers. A varia7le is declared 7$ s-eci%$ing its t$-e %ollowed 7$ one or ore naes
se-arated 7$ coas. In an$ cases, a varia7le can 7e initiali?ed as -art o% its declaration 7$ using the
assignent o-erator FG. he graar near the end o% this docuent -rovides a %ull re%erence %or the
s$nta o% declaring varia7les.
6ser;de%ined t$-es a$ 7e de%ined using str4ctto aggregate a list o% eisting t$-es into a single nae.
he )-en/0 #hading 0anguage is t$-e sa%e. here are no i-licit conversions 7etween t$-es, with theece-tion that an integer value a$ a--ear where a %loating;-oint t$-e is e-ected, and 7e converted to a
%loating;-oint value. Eactl$ how and when this can occur is descri7ed in section *.1.1'I-licit
"onversionsC and as re%erenced 7$ other sections in this s-eci%ication.
%.1 -asic Types
he )-en/0 #hading 0anguage su--orts the %ollowing 7asic data t$-es, grou-ed as %ollows.
rans-arent t$-es
Type 5eaning
void %or %unctions that do not return a value
>ool a conditional t$-e, taking on values o% true or %alse
int a signed integer
4int an unsigned integer
3loat a single %loating;-oint scalar
vec a two;co-onent %loating;-oint vector
vec< a three;co-onent %loating;-oint vector
vecB a %our;co-onent %loating;-oint vector
>vec a two;co-onent Boolean vector
>vec< a three;co-onent Boolean vector
>vecB a %our;co-onent Boolean vector
ivec a two;co-onent signed integer vector
ivec< a three;co-onent signed integer vector
ivecB a %our;co-onent signed integer vector
15
8/12/2019 GLSL Specification 3.30.6.Clean
22/123
4 #aria$%es and &'pes
Type 5eaning
4vec a two;co-onent unsigned integer vector
4vec< a three;co-onent unsigned integer vector4vecB a %our;co-onent unsigned integer vector
mat a !! %loating;-oint atri
mat< a (( %loating;-oint atri
matB a ** %loating;-oint atri
mat= sae as a mat
mat=< a %loating;-oint atri with ! coluns and ( rows
mat=B a %loating;-oint atri with ! coluns and * rows
mat
8/12/2019 GLSL Specification 3.30.6.Clean
23/123
4 #aria$%es and &'pes
Type 5eaning
sam7lerDF8Arra? a handle %or accessing a !D ulti;sa-le arra$ teture
#igned Integer #a-ler $-es Fo-a=ueG
Type 5eaning
isam7ler1D a handle %or accessing an integer 1D teture
isam7lerD a handle %or accessing an integer !D teture
isam7lere a handle %or accessing an integer cu7e a--ed teture
isam7lerDect a handle %or accessing an integer !D rectangular teture
isam7ler1DArra? a handle %or accessing an integer 1D arra$ teture
isam7lerDArra? a handle %or accessing an integer !D arra$ teture
isam7lerE433er a handle %or accessing an integer 7u%%er teture
isam7lerDF8 a handle %or accessing an integer !D ulti;sa-le teture
isam7lerDF8Arra? a handle %or accessing an integer !D ulti;sa-le arra$ teture
6nsigned Integer #a-ler $-es Fo-a=ueG
Type 5eaning
4sam7ler1D a handle %or accessing an unsigned integer 1D teture
4sam7lerD a handle %or accessing an unsigned integer !D teture
4sam7lere a handle %or accessing an unsigned integer cu7e a--ed teture
4sam7lerDect a handle %or accessing an unsigned integer rectangular teture
4sam7ler1DArra? a handle %or accessing an unsigned integer 1D arra$ teture
4sam7lerDArra? a handle %or accessing an unsigned integer !D arra$ teture
4sam7lerE433er a handle %or accessing an unsigned integer 7u%%er teture
4sam7lerDF8 a handle %or accessing an unsigned integer !D ulti;sa-le teture
4sam7lerDF8Arra? a handle %or accessing an unsigned integer !D ulti;sa-le teturearra$
In addition, a shader can aggregate these using arra$s and structures to 7uild ore co-le t$-es.
here are no -ointer t$-es.
17
8/12/2019 GLSL Specification 3.30.6.Clean
24/123
4 #aria$%es and &'pes
%.1.1 Void
3unctions that do not return a value ust 7e declared as void. here is no de%ault %unction return t$-e.
he ke$word voidcannot 7e used in an$ other declarations Fece-t %or e-t$ %oral or actual -araeter
listsG.
%.1.2 -ooleans
o ake conditional eecution o% code easier to e-ress, the t$-e >oolis su--orted. here is no
e-ectation that hardware directl$ su--orts varia7les 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 tr4eand 3alsecan 7e used as literal
Boolean constants. Booleans are declared and o-tionall$ initiali?ed as in the %ollow ea-le:
$ool su%%ess ** de%lare +su%%ess, to $e a -oolean
$ool done . false ** de%lare and initiali"e +done,
he right side o% the assignent o-erator F G ust 7e an e-ression whose t$-e is >ool.
E-ressions used %or conditional Tu-s Fi3, 3or, 2, w@ile, do-w@ileG ust evaluate to the t$-e >ool.
%.1." Integers
#igned and unsigned integer varia7les are %ull$ su--orted. In this docuent, the ter integer is eant to
generall$ include 7oth signed and unsigned integers. 6nsigned integers have eactl$ (! 7its o% -recision.
#igned integers use (! 7its, including a sign 7it, in twos co-leent %or. )-erations resulting in
over%low or under%low will not cause an$ ece-tion, nor will the$ saturate, rather the$ will wra-C to $ield
the low;order (! 7its o% the result.
Integers are declared and o-tionall$ initiali?ed with integer e-ressions, as in the %ollowing ea-le:
int i/ 0 . 12 ** default integer literal t&pe is int
uint 3 . 4u ** +u, esta$lis5es t5e t&pe as uint
1
8/12/2019 GLSL Specification 3.30.6.Clean
25/123
4 #aria$%es and &'pes
0iteral integer constants can 7e e-ressed in decial F7ase 1'G, octal F7ase 5G, or headecial F7ase 1G
as %ollows.
integer-constant :
#ecima'-constant integer-su))i%ot
octa'-constant integer-su))i%ot
he%a#ecima'-constant integer-su))i%ot
integer-su))i%: one o%
4 S
#ecima'-constant :
nonero-#igit
#ecima'-constant #igit
octa'-constant :
0
octa'-constant octa'-#igit
he%a#ecima'-constant :
'he%a#ecima'-#igit
'Whe%a#ecima'-#igit
he%a#ecima'-constant he%a#ecima'-#igit
#igit :
0
nonero-#igit
nonero-#igit : one o%
1 < B X Y [ 9
octa'-#igit one o%
0 1 < B X Y
he%a#ecima'-#igitone o%
0 1 < B X Y [ 9
a > c d e 3
A E C D I J
8/12/2019 GLSL Specification 3.30.6.Clean
26/123
4 #aria$%es and &'pes
%.1.% ,loats
3loats are availa7le %or use in a variet$ o% scalar calculations. 3loating;-oint varia7les are de%ined as in the
%ollowing ea-le:
float a/ $ . '67
As an in-ut value to one o% the -rocessing units, a %loating;-oint varia7le is e-ected to atch the IEEE
4&* single -recision %loating;-oint de%inition %or -recision and d$naic range. 3loating;-oint varia7les
within a shader are also encoded according to the IEEE 4&* s-eci%ication %or single;-recision %loating;
-oint values. +owever, it is not re=uired that the -recision o% internal -rocessing atch the IEEE 4&*
%loating;-oint s-eci%ication %or %loating;-oint o-erations, 7ut the guidelines %or -recision esta7lished 7$
the )-en/0 /ra-hics #$ste #-eci%ication ust 7e et. #iilarl$, treatent o% conditions such as divide
7$ ' a$ lead to an uns-eci%ied result, 7ut in no case should such a condition lead to the interru-tion or
terination o% -rocessing. /enerall$, there are no signaling
8/12/2019 GLSL Specification 3.30.6.Clean
27/123
4 #aria$%es and &'pes
%.1. Vectors
he )-en/0 #hading 0anguage includes data t$-es %or generic !;, (;, and *;co-onent vectors o%
%loating;-oint values, integers, or Booleans. 3loating;-oint vector varia7les can 7e used to store colors,
norals, -ositions, teture coordinates, teture looku- results and the like. Boolean vectors can 7e used
%or co-onent;wise co-arisons o% nueric vectors. #oe ea-les o% vector declaration are:
ve%2 tex%oord'/ tex%oord2
ve%4 position
ve%1 m&R!-8
ive%2 textureLoo3up
$ve%4 less
Initiali?ation o% vectors can 7e done with constructors, which are discussed shortl$.
%.1.( 5atrices
he )-en/0 #hading 0anguage has 7uilt;in t$-es %or !!, !(, !*, (!, ((, (*, *!, *(, and **atrices o% %loating;-oint nu7ers. he %irst nu7er in the t$-e is the nu7er o% coluns, the second is
the nu7er o% rows. Ea-le atri declarations:
mat2 mat2
mat4 opt9atrix
mat1 vie:/ pro0e%tion
mat1x1 vie: ** an alternate :a& of de%laring a mat1
mat4x2 m ** a matrix :it5 4 %olumns and 2 ro:s
Initiali?ation o% atri values is done with constructors Fdescri7ed in section &.* "onstructorsC G in
colun;aTor order.
%.1. Samplers
#a-ler t$-es Fe.g., sam7lerDG are e%%ectivel$ o-a=ue handles to tetures and their %ilters. he$ are
used with the 7uilt;in teture %unctions Fdescri7ed in section 5.4 eture 0ooku- 3unctionsC G to s-eci%$
which teture to access and how it is to 7e %iltered. he$ can onl$ 7e declared as %unction -araeters or
4ni3ormvaria7les Fsee section *.(.& 6ni%orC G. Ece-t %or arra$ indeing, structure %ield selection, and
-arentheses, sa-lers are not allowed to 7e o-erands in e-ressions. #a-lers aggregated into arra$s
within a shader Fusing s=uare 7rackets $ %G can onl$ 7e indeed with integral constant e-ressions Fsee
section *.(.( "onstant E-ressionsCG. #a-lers cannot 7e treated as l;values hence cannot 7e used as
o4t or ino4t %unction -araeters, nor can the$ 7e assigned into. As uni%ors, the$ are initiali?ed onl$
with the )-en/0 A2I the$ cannot 7e declared with an initiali?er in a shader. As %unction -araeters,
onl$ sa-lers a$ 7e -assed to sa-lers o% atching t$-e. his ena7les consistenc$ checking 7etween
shader teture accesses and )-en/0 teture state 7e%ore a shader is run.
21
8/12/2019 GLSL Specification 3.30.6.Clean
28/123
4 #aria$%es and &'pes
%.1.3 Structures
6ser;de%ined t$-es can 7e created 7$ aggregating other alread$ de%ined t$-es into a structure using the
str4ctke$word. 3or ea-le,
stru%t lig5t ;
float intensit&
ve%4 position
< lig5tVar
In this ea-le, 'ight7ecoes the nae o% the new t$-e, and 'ightVar7ecoes a varia7le o% t$-e 'ight.
o declare varia7les o% the new t$-e, use its nae Fwithout the ke$word str4ctG.
lig5t lig5tVar2
8ore %orall$, structures are declared as %ollows. +owever, the co-lete correct graar is as given in
section 9 #hading 0anguage /raarC .
struct-#e)inition :ua'i)ier
ot str4ctname
ot(mem&er-'ist )#ec'arators
ot
mem&er-'ist :
mem&er-#ec'aration
mem&er-#ec'aration mem&er-'ist
mem&er-#ec'aration :
&asic-t(e #ec'arators
where name7ecoes the user;de%ined t$-e, and can 7e used to declare varia7les to 7e o% this new t$-e.
he nameshares the sae nae s-ace as other varia7les, t$-es, and %unctions. All -reviousl$ visi7le
varia7les, t$-es, constructors, or %unctions with that nae are hidden. he o-tional ua'i)ieronl$ a--lies
to an$ #ec'arators, and is not -art o% the t$-e 7eing de%ined %or name.
#tructures ust have at least one e7er declaration. 8e7er declarators a$ contain -recision
=uali%iers, 7ut a$ not contain an$ other =uali%iers. Bit %ields are not su--orted. 8e7er t$-es ust 7e
alread$ de%ined Fthere are no %orward re%erencesG. 8e7er declarations cannot contain initiali?ers.
8e7er declarators can contain arra$s. #uch arra$s ust have a si?e s-eci%ied, and the si?e ust 7e an
integral constant e-ression thats greater than ?ero Fsee section *.(.( "onstant E-ressionsCG. Each
level o% structure has its own nae s-ace %or naes given in e7er declarators such naes need onl$
7e uni=ue within that nae s-ace.
22
8/12/2019 GLSL Specification 3.30.6.Clean
29/123
4 #aria$%es and &'pes
Anon$ous structures are not su--orted. E7edded structure de%initions are not su--orted.
stru%t S ; float f
8/12/2019 GLSL Specification 3.30.6.Clean
30/123
4 #aria$%es and &'pes
and as an alternate wa$ o% declaring a varia7le or %unction -araeter.
float>7? a
It is an error to declare arra$s o% arra$s:
float a>7?>4? ** illegal
float>7? a>4? ** illegal
Arra$s can have initiali?ers %ored %ro arra$ constructors:
float a>7? . float>7?461/ 162/ 76A/ 762/ '6')
float a>7? . float>?461/ 162/ 76A/ 762/ '6') ** same t5ing
6nsi?ed arra$s can 7e e-licitl$ si?ed 7$ an initiali?er at declaration tie:
float a>7?
666
float $>? . a ** $ is expli%itl& si"e 7
float $>7? . a ** means t5e same t5ing
+owever, i-licitl$ si?ed arra$s cannot 7e assigned to.
8/12/2019 GLSL Specification 3.30.6.Clean
31/123
4 #aria$%es and &'pes
%.1.1! Implicit Con#ersions
In soe situations, an e-ression and its t$-e will 7e i-licitl$ converted to a di%%erent t$-e. he
%ollowing ta7le shows all allowed i-licit conversions:
Type of e*pression Can be implicitly con#erted to
int
4int
3loat
ivec4vec
vec
ivec4te de-recatedlinkage 7etween a verte shader and )-en/0 %or -er;vertedata
4ni3orm value does not change across the -riitive 7eing -rocessed, uni%ors%or the linkage 7etween a shader, )-en/0, and the a--lication
var?ingcentroid var?ing
de-recated linkage 7etween a verte shader and a %ragent shader %orinter-olated data
)ut-uts %ro shader Fo4tG and in-uts to a shader FinG can 7e %urther =uali%ied with one o% these
inter-olation =uali%iers
7ualifier 5eaning
smoot@ -ers-ective correct inter-olation
3lat no inter-olation
no7ers7ective linear inter-olation
hese inter-olation =uali%iers a$ onl$ -recede the =uali%iers in, centroid in, o4t, or centroid o4t in a
declaration. he$ do not a--l$ to the de-recated storage =uali%iers var?ing or centroid var?ing. he$
also do not a--l$ to in-uts into a verte shader or out-uts %ro a %ragent shader.
0ocal varia7les can onl$ use the const storage =uali%ier.
3unction -araeters can use const,in, ando4t =uali%iers, 7ut asarameter ua'i)iers. 2araeter
=uali%iers are discussed in section .1.1 3unction "alling "onventionsC.
3unction return t$-es and structure %ields do not use storage =uali%iers.
Data t$-es %or counication %ro one run o% a shader eecuta7le to its net run Fto counicate
7etween %ragents or 7etween verticesG do not eist. his would -revent -arallel eecution o% the sae
shader eecuta7le on ulti-le vertices or %ragents.
27
8/12/2019 GLSL Specification 3.30.6.Clean
34/123
8/12/2019 GLSL Specification 3.30.6.Clean
35/123
4 #aria$%es and &'pes
"onstant e-ressions will 7e evaluated in an invariant wa$ so as to create the sae value in ulti-le
shaders when the sae constant e-ressions a--ear in those shaders. #ee section *..1he Invariant
uali%ierC %or ore details on how to create invariant e-ressions.
%.".% Inputs
#hader in-ut varia7les are declared with the in storage =uali%ier or the centroid instorage =uali%ier. he$
%or the in-ut inter%ace 7etween -revious stages o% the )-en/0 -i-eline and the declaring shader. In-ut
varia7les ust 7e declared at glo7al sco-e. Values %ro the -revious -i-eline stage are co-ied into in-ut
varia7les at the 7eginning o% shader eecution. Varia7les declared as in or centroid in a$ not 7e written
to during shader eecution. )nl$ the in-ut varia7les that are actuall$ read need to 7e written 7$ the
-revious stage it is allowed to have su-er%luous declarations o% in-ut varia7les.
#ee section 4 Built;in Varia7lesC %or a list o% the 7uilt;in in-ut naes.
Verte shader in-ut varia7les For attri7utesG receive -er;verte data. he$ are declared in a verte shader
with the in =uali%ier or the de-recated attri>4te=uali%ier. It is an error to use centroid in or inter-olation
=uali%iers in a verte shader in-ut. he values co-ied in are esta7lished 7$ the )-en/0 A2I or throughthe use o% the la$out identi%ier 'ocation. It is an error to use attri>4tein a non;verte shader. Verte
shader in-uts can onl$ 7e 3loat, %loating;-oint vectors, atrices, signed and unsigned integers and integer
vectors. Verte shader in-uts can also %or arra$s o% these t$-es, 7ut not structures.
Ea-le declarations in a verte shader:
in ve%1 position
in ve%4 normal
in ve%2 texCoord>1?
It is e-ected that gra-hics hardware will have a sall nu7er o% %ied vector locations %or -assing verte
in-uts. here%ore, the )-en/0 #hading language de%ines each non;atri in-ut varia7le as taking u- one
such vector location. here is an i-leentation de-endent liit on the nu7er o% locations that can 7e
used, and i% this is eceeded it will cause a link error. FDeclared in-ut varia7les that are not staticall$ useddo not count against this liit.G A scalar in-ut counts the sae aount against this liit as a vecB, so
a--lications a$ want to consider -acking grou-s o% %our unrelated %loat in-uts together into a vector to
7etter utili?e the ca-a7ilities o% the underl$ing hardware. A atri in-ut will use u- ulti-le locations.
he nu7er o% locations used will e=ual the nu7er o% coluns in the atri.
/eoetr$ shader in-ut varia7les get the -er;verte values written out 7$ verte shader out-ut varia7les o%
the sae naes. #ince a geoetr$ shader o-erates on a set o% vertices, each in-ut var$ing varia7le For
in-ut 7lock, see inter%ace 7locks 7elowG needs to 7e declared as an arra$. 3or ea-le,
in float foo>? ** geometr& s5ader input for vertex +out float foo,
Each eleent o% such an arra$ corres-onds to one verte o% the -riitive 7eing -rocessed. Each arra$ can
o-tionall$ have a si?e declared. he arra$ si?e will 7e set 7$, For i% -rovided ust 7e consistent withG the
in-ut la?o4tdeclarationFsG esta7lishing the t$-e o% in-ut -riitive, as descri7ed later in section *.(.5.1
In-ut 0a$out uali%iersC.
2!
8/12/2019 GLSL Specification 3.30.6.Clean
36/123
4 #aria$%es and &'pes
3or the inter%ace 7etween a verte shader and a geoetr$ shader, verte shader out-ut varia7les and
geoetr$ shader in-ut varia7les o% the sae nae ust atch in t$-e and =uali%ication, ece-t that the
verte shader nae cannot 7e declared as an arra$ while the geoetr$ shader nae ust 7e declared as an
arra$. )therwise, a link error will occur.
I% the out-ut o% a verte shader is itsel% an arra$ to 7e consued 7$ a geoetr$ shader, then it ust a--ear
in an out-ut 7lock Fsee inter%ace 7locks 7elowG in the verte shader and in an in-ut 7lock in the geoetr$
shader with a 7lock instance nae declared as an arra$. his is re=uired %or arra$s out-ut %ro a verte
shader 7ecause two;diensional arra$s are not su--orted.
3ragent shader in-uts get -er;%ragent values, t$-icall$ inter-olated %ro a -revious stages out-uts.
he$ are declared in %ragent shaders with the in storage =uali%ier, the centroid in storage =uali%ier, or
the de-recated var?ingand centroid var?ing storage =uali%iers. 3ragent in-uts can onl$ 7e signed and
unsigned integers and integer vectors, 3loat, %loating;-oint vectors, atrices, or arra$s or structures o%
these. 3ragent shader in-uts that are signed or unsigned integers or integer vectors ust 7e =uali%ied
with the inter-olation =uali%ier 3lat.
3ragent in-uts are declared as in the %ollowing ea-les:
in ve%4 normal
%entroid in ve%2 TexCoord
invariant %entroid in ve%1 Color
noperspe%tive in float temperature
flat in ve%4 m&Color
noperspe%tive %entroid in ve%2 m&TexCoord
I% a geoetr$ shader is not -resent in a -rogra, 7ut a verte and %ragent shader are -resent, then the
out-ut o% the verte shader and the in-ut o% the %ragent shader %or an inter%ace. 3or this inter%ace,
verte shader out-ut varia7les and %ragent shader in-ut varia7les o% the sae nae ust atch in t$-e
and =uali%ication Fother than o4t atching to inG.
%.". 4niform
he 4ni3orm =uali%ier is used to declare glo7al varia7les whose values are the sae across the entire
-riitive 7eing -rocessed. All 4ni3ormvaria7les are read;onl$ and are initiali?ed eternall$ either at link
tie or through the A2I. he link tie initial value is either the value o% the varia7les initiali?er, i%
-resent, or ' i% no initiali?er is -resent. #a-ler t$-es cannot have initiali?ers.
Ea-le declarations are:
uniform ve%1 lig5t@osition
uniform ve%4 %olor . ve%4A6D/ A6D/ A62) ** value assigned at lin3 time
he 4ni3orm=uali%ier can 7e used with an$ o% the 7asic data t$-es, or when declaring a varia7le 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 7e used %or
each t$-e o% shader and i% this is eceeded it will cause a co-ile;tie or link;tie error. 6ni%or
varia7les that are declared 7ut not used do not count against this liit. he nu7er o% user;de%ined
uni%or varia7les and the nu7er o% 7uilt;in uni%or varia7les that are used within a shader are added
together to deterine whether availa7le uni%or storage has 7een eceeded.
3"
8/12/2019 GLSL Specification 3.30.6.Clean
37/123
4 #aria$%es and &'pes
I% ulti-le shaders are linked together, then the$ will share a single glo7al uni%or nae s-ace, including
within a language as well as across languages. +ence, the t$-es and initiali?ers o% uni%or varia7les 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 varia7le, while another shader
does not, 7ut all -rovided initiali?ers ust 7e e=ual.
%.".( Outputs
#hader out-ut varia7les are declared with the o4t or centroid o4t storage =uali%iers. he$ %or the
out-ut inter%ace 7etween the declaring shader and the su7se=uent stages o% the )-en/0 -i-eline. )ut-ut
varia7les ust 7e declared at glo7al sco-e. During shader eecution the$ will 7ehave as noral
un=uali%ied glo7al varia7les. heir values are co-ied out to the su7se=uent -i-eline stage on shader eit.
)nl$ out-ut varia7les that are read 7$ the su7se=uent -i-eline stage need to 7e written it is allowed to
have su-er%luous declarations o% out-ut varia7les.
here is notan ino4tstorage =uali%ier at glo7al sco-e %or declaring a single varia7le nae as 7oth in-ut
and out-ut to a shader. )ut-ut varia7les ust 7e declared with di%%erent naes than in-ut varia7les.+owever, nesting an in-ut or out-ut inside an inter%ace 7lock with an instance nae allows the sae
naes with one re%erenced through a 7lock instance nae.
Verte and geoetr$ out-ut varia7les out-ut -er;verte data and are declared using the o4tstorage
=uali%ier, the centroid o4tstorage =uali%ier, or the de-recated var?ingstorage =uali%ier. he$ can onl$ 7e
3loat, %loating;-oint vectors, atrices, signed or unsigned integers or integer vectors, or arra$s or
structures o% an$ these.
Individual verte and geoetr$ out-uts are declared as in the %ollowing ea-les:
out ve%4 normal
%entroid out ve%2 TexCoord
invariant %entroid out ve%1 Color
noperspe%tive out float temperature ** var&ing is depre%atedflat out ve%4 m&Color
noperspe%tive %entroid out ve%2 m&TexCoord
hese can also a--ear in inter%ace 7locks, as descri7ed in the section *.(.4 Inter%ace BlocksC. Inter%ace
7locks allow si-ler addition o% arra$s to the inter%ace %ro verte to geoetr$ shader. he$ also allow a
%ragent shader to have the sae in-ut inter%ace as a geoetr$ shader %or a given verte shader.
3ragent out-uts out-ut -er;%ragent data and are declared using the o4t storage =uali%ier. It is an error
to use centroid o4t in a %ragent shader. 3ragent out-uts can onl$ 7e 3loat, %loating;-oint vectors,
signed or unsigned integers or integer vectors, or arra$s o% an$ these. 8atrices and structures cannot 7e
out-ut. 3ragent out-uts are declared as in the %ollowing ea-les:
out ve%1 FragmentColor
out uint Luminosit&
31
8/12/2019 GLSL Specification 3.30.6.Clean
38/123
4 #aria$%es and &'pes
%.". Interface -loc0s
In-ut, out-ut, and uni%or varia7le declarations can 7e grou-ed into naed inter%ace 7locks to -rovide
coarser granularit$ 7acking than is achieva7le with individual declarations. he$ can have an o-tional
instance nae, used in the shader to re%erence their e7ers. An out-ut 7lock o% one -rograa7le
stage is 7acked 7$ a corres-onding in-ut 7lock in the su7se=uent -rograa7le stage. A uni%or 7lock is
7acked 7$ the a--lication with a 7u%%er o7Tect. It is illegal to have an in-ut 7lock in a verte shader or an
out-ut 7lock in a %ragent shader these uses are reserved %or %uture use.
An inter%ace 7lock is started 7$ an in, o4t, or 4ni3orm ke$word, %ollowed 7$ a 7lock nae, %ollowed 7$
an o-en curl$ 7race F ( G as %ollows:
inter)ace-&'oc/ :
'a(out-ua'i)iero7t inter)ace-ua'i)ier &'oc/-name (mem&er-'ist )instance-nameo7t
'a(out-ua'i)ier :
la?o4t & 'a(out-ua'i)ier-i#-'ist'
inter)ace-ua'i)ier :in
o4t
4ni3orm
'a(out-ua'i)ier-i#-'ist
coa se-arated list o% 'a(out-ua'i)ier-i#
mem&er-'ist :
mem&er-#ec'aration
mem&er-#ec'aration mem&er-'ist
mem&er-#ec'aration :
'a(out-ua'i)iero7t ua'i)ierso7t t(e #ec'arators
instance-name :
i#enti)ier
i#enti)ier 7
i#enti)ier integra'-constant-e%ression 7
Each o% the a7ove eleents is discussed 7elow, with the ece-tion o% la$out =uali%iers F 'a(out-ua'i)ier5,
which are de%ined in the net section.
3irst, an ea-le,
uniform Transform ;
mat1 9odelVie:9atrix
mat1 9odelVie:@ro0e%tion9atrix
uniform mat4 Normal9atrix ** allo:ed restatement of =ualifier
float eformation
8/12/2019 GLSL Specification 3.30.6.Clean
39/123
4 #aria$%es and &'pes
$-es and declarators are the sae as %or other in-ut, out-ut, and uni%or varia7le declarations outside
7locks, with these ece-tions:
initiali?ers are not allowed
sa-ler t$-es are not allowed
structure de%initions cannot 7e nested inside a 7lock
)therwise, 7uilt;in 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 7lock.
I% no o-tional =uali%ier is used in a e7er;declaration, the =uali%ication o% the varia7le is Tust in, o4t, or
4ni3ormas deterined 7$ inter)ace-ua'i)ier. I% o-tional =uali%iers are used, the$ can include
inter-olation and storage =uali%iers and the$ ust declare an in-ut, out-ut, or uni%or varia7le consistent
with the inter%ace =uali%ier o% the 7lock: In-ut varia7les, out-ut varia7les, and uni%or varia7les can onl$
7e in in7locks, o4t7locks, and 4ni3orm7locks, res-ectivel$. Re-eating the in, o4t, or 4ni3orm
inter%ace =uali%ier %or a e7ers storage =uali%ier is o-tional. Declarations using the de-recated
attri>4te and var?ing=uali%iers are not allowed. 3or ea-le,
in 9aterial ;
smoot5 in ve%1 Color' ** legal/ input inside in $lo%3
smoot5 ve%1 Color2 ** legal/ BinB in5erited from Bin 9aterialB
ve%2 TexCoord ** legal/ TexCoord is an input
uniform float 8tten ** illegal/ mismat%5ed interfa%es
var&ing ve%2 TexCoord2**illegal/ depre%ated 3e&:ords donBt get ne: uses
8/12/2019 GLSL Specification 3.30.6.Clean
40/123
4 #aria$%es and &'pes
I% an instance nae Finstance-nameGis not used, the naes declared inside the 7lock are sco-ed at the
glo7al level and accessed as i% the$ were declared outside the 7lock. I% an instance nae Finstance-nameG
is used, then it -uts all the e7ers inside a sco-e within its own nae s-ace, accessed with the %ield
selector F . G o-erator Fanalogousl$ to structuresG. 3or ea-le,
in Lig5t ;
ve%1 Lig5t@os
ve%4 Lig5tColor
1?
666
666 . transforms>2?69odelVie:9atrix ** s5ader a%%ess of instan%e 2
** 8@I uses +Transform69odelVie:9atrix, to =uer& an offset or ot5er =uer&
3or uni%or 7locks declared as an arra$, each individual arra$ eleent corres-onds to a se-arate 7u%%ero7Tect 7acking one instance o% the 7lock. As the arra$ si?e indicates the nu7er o% 7u%%er o7Tects needed,
uni%or 7lock arra$ declarations ust s-eci%$ an arra$ si?e. All indees used to inde a uni%or 7lock
arra$ ust 7e integral constant e-ressions.
34
8/12/2019 GLSL Specification 3.30.6.Clean
41/123
4 #aria$%es and &'pes
hen using )-en/0 A2I entr$ -oints to identi%$ the nae o% an individual 7lock in an arra$ o% 7locks,
the nae string ust include an arra$ inde Fe.g., Trans)orm27G. hen using )-en/0 A2I entr$ -oints
to re%er to o%%sets or other characteristics o% a 7lock e7er, an arra$ inde ust not 7e s-eci%ied Fe.g.,
Trans)orm.Mo#e'VieMatri%G.
/eoetr$ shader in-ut 7locks ust 7e 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 7lock arra$s ust s-eci%$ an arra$ si?e.
here is an i-leentation de-endent liit on the nu7er o% uni%or 7locks that can 7e used -er stage.
I% this liit is eceeded, it will cause a link error.
%.".3 Layout 7ualifiers
0a$out =uali%iers can a--ear in several %ors o% declaration. he$ can a--ear as -art o% an inter%ace
7lock de%inition or 7lock e7er, as shown in the graar in the -revious section. he$ can also a--ear
with Tust an inter%ace =uali%ier to esta7lish la$outs o% other declarations ade with that inter%ace =uali%ier:
'a(out-ua'i)ierinter)ace-ua'i)ier
)r, the$ can a--ear with an individual varia7le declared with an inter%ace =uali%ier:
'a(out-ua'i)ierinter)ace-ua'i)ier #ec'aration
Declarations o% la$outs can onl$ 7e ade at glo7al sco-e, and onl$ where indicated in the %ollowing
su7sections their details are s-eci%ic to what the inter%ace =uali%ier is, and are discussed individuall$.
As shown in the -revious section, 'a(out-ua'i)ier e-ands to
'a(out-ua'i)ier :
la?o4t & 'a(out-ua'i)ier-i#-'ist'
he tokens in an$ 'a(out-ua'i)ier-i#-'istare identi%iers, not ke$words. /enerall$, the$ can 7e listed in
an$ order. )rder;de-endent eanings eist onl$ i% e-licitl$ called out 7elow. #iilarl$, these identi%iers
are not case sensitive, unless e-licitl$ noted otherwise.
%.".3.1 Input Layout 7ualifiers
Verte shaders allow in-ut la$out =uali%iers on in-ut varia7le declarations. he la$out =uali%ier identi%ier
%or verte shader in-uts is:
'a(out-ua'i)ier-i#
location integer-constant
)nl$ one arguent is acce-ted. 3or ea-le,
la&outlo%ation . 4) in ve%1 normal
will esta7lish that the verte shader in-ut norma' is co-ied in %ro vector location nu7er (.
I% the declared in-ut is an arra$, it will 7e assigned consecutive locations starting with the location
s-eci%ied. 3or ea-le,
la&outlo%ation . ) in ve%1 %olors>4?
will esta7lish that the verte shader in-ut co'orsis co-ied in %ro vector location nu7ers , 4, and 5.
35
8/12/2019 GLSL Specification 3.30.6.Clean
42/123
4 #aria$%es and &'pes
I% an in-ut varia7le with no location assigned in the shader tet has a location s-eci%ied through the
)-en/0 A2I, the A2I;assigned location will 7e used. )therwise, such varia7les will 7e assigned a
location 7$ the linker. #ee section !.11.* Verte Attri7utesC o% the )-en/0 /ra-hics #$ste
#-eci%ication %or ore details. A link error will occur i% an in-ut varia7le is declared in ulti-le verteshaders with con%licting locations.
/eoetr$ shaders allow in-ut la$out =uali%iers onl$ on the inter%ace =uali%ier in, not on an in-ut 7lock,
7lock e7er, or varia7le. he la$out =uali%ier identi%iers %or geoetr$ shader in-uts are
'a(out-ua'i)ier-i#
7oints
lines
lines5adGacenc?
triangles
triangles5adGacenc?
)nl$ one arguent is acce-ted. 3or ea-le,
la&outtriangles) in
will esta7lish that all in-uts to the geoetr$ shader are triangles.
At least one geoetr$ shader Fco-ilation unitG in a -rogra ust declare an in-ut 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 la$out.
All geoetr$ shader in-ut unsi?ed arra$ declarations will 7e si?ed 7$ an earlier in-ut la$out =uali%ier,
when -resent, as -er the %ollowing ta7le.
;a?o4t 8ize o3 Ln74t Arra?s
-oints 1
lines !
lines>adTacenc$ *
triangles (
triangles>adTacenc$
he intrinsicall$ declared in-ut arra$g'*in7will also 7e si?ed 7$ an$ in-ut la$out declaration. +ence,
the e-ression
gl_in6lengt5)
will return the value %ro the ta7le a7ove.
3or in-uts declared without an arra$ si?e, including intrinsicall$ declared in-uts Fi.e.,g'*inG, a la$out ust
7e declared 7e%ore an$ use o% the ethod 'ength45or other arra$ use re=uiring its si?e 7e known.
36
8/12/2019 GLSL Specification 3.30.6.Clean
43/123
8/12/2019 GLSL Specification 3.30.6.Clean
44/123
4 #aria$%es and &'pes
Redeclaringg'*rag8oor# with origin5477er5le3t and@or 7i=el5center5integer=uali%iers onl$ a%%ects
g'*rag8oor#.% and g'*rag8oor#.(. It has no a%%ect on rasteri?ation, trans%oration, or an$ other -art
o% the )-en/0 -i-eline or language %eatures.
%.".3.2 Output Layout 7ualifiers
Verte shaders cannot have out-ut la$out =uali%iers.
3ragent shaders allow out-ut la$out =uali%iers onl$ on the inter%ace =uali%ier o4t. he la$out =uali%ier
identi%ier %or %ragent shader out-uts is:
'a(out-ua'i)ier-i#
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 7e s-eci%ied.
I% inde= is not s-eci%ied, the value ' is used. 3or ea-le,
la&outlo%ation . 4) out ve%1 %olor
will esta7lish that the %ragent shader out-ut co'or is co-ied out to %ragent color ( as the %irst Finde
?eroG in-ut to the 7lend e=uation. And,
la&outlo%ation . 4/ index . ') out ve%1 fa%tor
will esta7lish that the %ragent shader out-ut)actoris co-ied out to %ragent color ( as the second Finde
oneG in-ut to the 7lend e=uation.
I% the naed %ragent shader out-ut is an arra$, it will 7e assigned consecutive locations starting with the
location s-eci%ied. 3or ea-le,
la&outlo%ation . 2) out ve%1 %olors>4?
will esta7lish that co'orsis co-ied out to vector location nu7ers !, (, and *.
I% an out-ut varia7le with no location or inde assigned in the shader tet has a location s-eci%ied through
the )-en/0 A2I, the A2I;assigned location will 7e used. )therwise, such varia7les will 7e assigned a
location 7$ the linker. All such assignents will have a color inde o% ?ero. #ee section (.9.! #hader
EecutionC o% the )-en/0 /ra-hics #$ste #-eci%ication %or ore details. A link error will occur i% an
in-ut varia7le is declared in ulti-le verte shaders with con%licting location or inde values.
/eoetr$ shaders can have out-ut la$out =uali%iers onl$ on the inter%ace =uali%ier o4t, not on an out-ut
7lock or varia7le declaration.
he la$out =uali%ier identi%iers %or geoetr$ shader out-uts are
'a(out-ua'i)ier-i#
7ointsline5stri7
triangle5stri7
ma=5verticesinteger-constant
3
8/12/2019 GLSL Specification 3.30.6.Clean
45/123
4 #aria$%es and &'pes
)ne declaration can declare either a -riitive t$-e F7oints,line5stri7, ortriangle5stri7G, or
ma=5vertices, or 7oth. 6se ma=5vertices to declare the aiu nu7er o% vertices this shader will
ever eit in a single eecution. 3or ea-le,
la&outtriangle_strip/ max_verti%es . A) out ** order does not matter
la&outmax_verti%es . A) out ** rede%laration o3a&
la&outtriangle_strip) out ** rede%laration o3a&
la&outpoints) out ** error/ %ontradi%ts triangle_strip
la&outmax_verti%es . 4A) out ** error/ %ontradi%ts A
these will esta7lish that all out-uts %ro the geoetr$ shader are triangles and at ost ' vertices will 7e
eitted 7$ the shader. It is an error %or the aiu nu7er o% vertices to 7e greater than
gl5Fa=:eometr?P4t74tTertices.
All geoetr$ shader out-ut la$out declarations in a -rogra ust declare the sae la$out and sae value
%or ma=5vertices. I% geoetr$ shaders are in a -rogra, there ust 7e at least one geoetr$ out-ut
la$out declaration soewhere in the -rogra, 7ut not all geoetr$ shaders Fco-ilation unitsG are
re=uired to declare it.
%.".3." 4niform -loc0 Layout 7ualifiers
0a$out =uali%iers can 7e used %or uni%or 7locks, 7ut not %or non;7lock uni%or declarations. he la$out
=uali%ier identi%iers %or uni%or 7locks are
'a(out-ua'i)ier-i#
s@ared
7acked
std1B0
row5maGor
col4mn5maGor
8/12/2019 GLSL Specification 3.30.6.Clean
46/123
4 #aria$%es and &'pes
6ni%or 7locks can 7e declared with o-tional la$out =uali%iers, and so can their individual e7er
declarations. #uch 7lock la$out =uali%ication is sco-ed onl$ to the content o% the 7lock. As with glo7al
la$out declarations, 7lock la$out =uali%ication %irst inherits %ro the current de%ault =uali%ication and then
overrides it. #iilarl$, individual e7er la$out =uali%ication is sco-ed Tust to the e7er declaration,and inherits %ro and overrides the 7locks =uali%ication.
heshare# =uali%ier overrides onl$ thest#190 andac/e# =uali%iers other =uali%iers are inherited. he
co-iler@linker will ensure that ulti-le -rogras and -rograa7le stages containing this de%inition
will share the sae eor$ la$out %or this 7lock, as long as the$ also atched in their ro*maor and@or
co'umn*maor =uali%ications. his allows use o% the sae 7u%%er to 7ack the sae 7lock de%inition across
di%%erent -rogras.
heac/e# =uali%ier overrides onl$st#190 andshare# other =uali%iers are inherited. henac/e# is
used, no sharea7le la$out is guaranteed. he co-iler and linker can o-tii?e eor$ use 7ased on what
varia7les activel$ get used and on other criteria. )%%sets ust 7e =ueried, as there is no other wa$ o%
guaranteeing where Fand whichG varia7les reside within the 7lock. Atte-ts to share a -acked uni%or
7lock across -rogras or stages will generall$ %ail. +owever, i-leentations a$ aid a--lication
anageent o% -acked 7locks 7$ using canonical la$outs %or -acked 7locks.
hest#190 =uali%ier overrides onl$ theac/e# andshare# =uali%iers other =uali%iers are inherited. he
la$out is e-licitl$ deterined 7$ this, as descri7ed in section !.11.& 6ni%or Varia7lesC under
#tandard 6ni%or Block 0a$outC o% the )-en/0 /ra-hics #$ste #-eci%ication. +ence, as inshare#
a7ove, the resulting la$out is sharea7le across -rogras.
0a$out =uali%iers on e7er declarations cannot use theshare#,ac/e#,orst#190 =uali%iers. hese can
onl$ 7e used at glo7al sco-e or on a 7lock declaration.
he ro*maor =uali%ier overrides onl$ the co'umn*maor=uali%ier other =uali%iers are inherited. It onl$
a%%ects the la$out o% atrices. Eleents within a atri row will 7e contiguous in eor$.
he co'umn*maor =uali%ier overrides onl$ the ro*maor=uali%ier other =uali%iers are inherited. It onl$
a%%ects the la$out o% atrices. Eleents within a atri colun will 7e contiguous in eor$.
hen ulti-le arguents are listed in a la?o4t declaration, the a%%ect will 7e 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.
4"
8/12/2019 GLSL Specification 3.30.6.Clean
47/123
4 #aria$%es and &'pes
3or ea-le
la&outro:_ma0or/ %olumn_ma0or)
results in the =uali%ication 7eing co'umn*maor. )ther ea-les:
la&outs5ared/ ro:_ma0or) uniform ** default is no: s5ared and ro:_ma0or
la&outstd'1A) uniform Transform ; ** la&out of t5is $lo%3 is std'1A
mat1 9' ** ro:_ma0or
la&out%olumn_ma0or) mat1 92 ** %olumn ma0or
mat4 N' ** ro:_ma0or
8/12/2019 GLSL Specification 3.30.6.Clean
48/123
8/12/2019 GLSL Specification 3.30.6.Clean
49/123
8/12/2019 GLSL Specification 3.30.6.Clean
50/123
8/12/2019 GLSL Specification 3.30.6.Clean
51/123
4 #aria$%es and &'pes
%.(.1 The In#ariant 7ualifier
o ensure that a -articular out-ut varia7le is invariant, it is necessar$ to use the invariant=uali%ier. It can
either 7e used to =uali%$ a -reviousl$ declared varia7le as 7eing invariant
invariant gl_@osition ** ma3e existing gl_@osition $e invariant
out ve%4 Color
invariant Color ** ma3e existing Color $e invariant
or as -art o% a declaration when a varia7le is declared
invariant %entroid out ve%4 Color
he invariant =uali%ier ust a--ear 7e%ore an$ inter-olation =uali%iers or storage =uali%iers when
co7ined with a declaration. )nl$ varia7les out-ut %ro a shader Fincluding those that are then in-ut to a
su7se=uent shaderG can 7e candidates %or invariance. his includes user;de%ined out-ut varia7les and the
7uilt;in out-ut varia7les. 3or varia7les leaving one shader and coing into another shader, the invariantke$word has to 7e used in 7oth shaders, or a link error will result.
In-ut or out-ut instance naes on 7locks are not used when redeclaring 7uilt;in varia7les.
he invariantke$word can 7e %ollowed 7$ a coa se-arated list o% -reviousl$ declared identi%iers. All
uses o% invariantust 7e at the glo7al sco-e, and 7e%ore an$ use o% the varia7les 7eing declared as
invariant.
o guarantee invariance o% a -articular out-ut varia7le across two -rogras, the %ollowing ust also 7e
true:
he out-ut varia7le is declared as invariant in 7oth -rogras.
he sae values ust 7e in-ut to all shader in-ut varia7les consued 7$ e-ressions and %low control
contri7uting to the value assigned to the out-ut varia7le.
he teture %orats, teel values, and teture %iltering are set the sae wa$ %or an$ teture %unction
calls contri7uting to the value o% the out-ut varia7le.
All in-ut values are all o-erated on in the sae wa$. All o-erations in the consuing e-ressions and
an$ interediate e-ressions ust 7e the sae, with the sae order o% o-erands and sae
associativit$, to give the sae order o% evaluation. Interediate varia7les and %unctions ust 7e
declared as the sae t$-e with the sae e-licit or i-licit -recision =uali%iers. An$ control %low
a%%ecting the out-ut value ust 7e the sae, and an$ e-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 varia7le reside in a single
co-ilation unit.
Essentiall$, all the data %low and control %low leading to an invariant out-ut ust atch.
Initiall$, 7$ de%ault, all out-ut varia7les are allowed to 7e variant. o %orce all out-ut varia7les to 7e
invariant, use the -raga
45
8/12/2019 GLSL Specification 3.30.6.Clean
52/123
4 #aria$%es and &'pes
#pragma ST!L invariantall)
7e%ore all declarations in a shader. I% this -raga is used a%ter the declaration o% an$ varia7les or
%unctions, then the set o% out-uts that 7ehave as invariant is unde%ined. It is an error to use this -raga ina %ragent shader.
/enerall$, invariance is ensured at the cost o% %lei7ilit$ in o-tii?ation, so -er%orance can 7e degraded
7$ use o% invariance. +ence, use o% this -raga is intended as a de7ug aid, to avoid individuall$ declaring
all out-ut varia7les as invariant.
%.(.2 In#ariance of Constant &*pressions
Invariance ust 7e guaranteed %or constant e-ressions. A -articular constant e-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
e-ression a--earing two shaders o% the sae language or shaders o% two di%%erent languages.
"onstant e-ressions ust evaluate to the sae result when o-erated on as alread$ descri7ed a7ove %or
invariant varia7les.
%. Order of 7ualification
hen ulti-le =uali%ications are -resent, the$ ust %ollow a strict order. his order is as %ollows.
invariant-ua'i)ier intero'ation-ua'i)ier storage-ua'i)ier recision-ua'i)ier
storage-ua'i)ier arameter-ua'i)ier recision-ua'i)ier
46
8/12/2019 GLSL Specification 3.30.6.Clean
53/123
Operators and &*pressions
.1 Operators
he )-en/0 #hading 0anguage has the %ollowing o-erators.
+recedence Operator Class Operators ssociati#ity
1 FhighestG -arenthetical grou-ing & '
8/12/2019 GLSL Specification 3.30.6.Clean
54/123
5 Operators and ()pressions
.2 rray Operations
hese are now descri7ed in section &.4 #tructure and Arra$ )-erationsC.
." ,unction Calls
I% a %unction returns a value, then a call to that %unction a$ 7e used as an e-ression, whose t$-e will 7e
the t$-e that was used to declare or de%ine the %unction.
3unction de%initions and calling conventions are discussed in section .1 3unction De%initionsC .
.% Constructors
"onstructors use the %unction call s$nta, where the %unction nae is a t$-e, and the call akes an o7Tect
o% that t$-e. "onstructors are used the sae wa$ in 7oth initiali?ers and e-ressions. F#ee section 9
#hading 0anguage /raarC %or details.G he -araeters are used to initiali?e the constructed value.
"onstructors can 7e used to re=uest a data t$-e conversion to change %ro one scalar t$-e to another
scalar t$-e, or to 7uild 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 7uilt;in %unctions with -redeterined -rotot$-es. 3or arra$s and
structures, there ust 7e eactl$ one arguent in the constructor %or each eleent or %ield. 3or the other
t$-es, the arguents ust -rovide a su%%icient nu7er o% co-onents to -er%or the initiali?ation, and it
is an error to include so an$ arguents that the$ cannot all 7e used. Detailed rules %ollow. he
-rotot$-es actuall$ listed 7elow are erel$ a su7set o% ea-les.
.%.1 Con#ersion and Scalar Constructors
"onverting 7etween scalar t$-es is done as the %ollowing -rotot$-es indicate:
int$ool) ** %onverts a -oolean value to an int
intfloat) ** %onverts a float value to an int
float$ool) ** %onverts a -oolean value to a float
floatint) ** %onverts a signed integer value to a float
$oolfloat) ** %onverts a float value to a -oolean
$oolint) ** %onverts a signed integer value to a -oolean
uint$ool) ** %onverts a -oolean value to an unsigned integer
uintfloat) ** %onverts a float value to an unsigned integer
uintint) ** %onverts a signed integer value to an unsigned integer
intuint) ** %onverts an unsigned integer to a signed integer
$ooluint) ** %onverts an unsigned integer value to a -oolean value
floatuint) ** %onverts an unsigned integer value to a float value
hen constructors are used to convert a 3loatto an int or 4int, the %ractional -art o% the %loating;-oint
value is dro--ed. It is unde%ined to convert a negative %loating -oint value to an 4int.
hen a constructor is used to convert an int, 4int, or a 3loatto a >ool, ' and '.' are converted to 3alse,
and non;?ero values are converted to tr4e. hen a constructor is used to convert a >oolto an int, 4int,
or 3loat, 3alseis converted to ' or '.', and tr4eis converted to 1 or 1.'.
4
8/12/2019 GLSL Specification 3.30.6.Clean
55/123
5 Operators and ()pressions
he constructor int&4int'-reserves the 7it -attern in the arguent, which will change the arguents
value i% its sign 7it is set. he constructor 4int&int'-reserves the 7it -attern in the arguent, which will
change its value i% it is negative.
Identit$ constructors, like 3loatF3loatG are also legal, 7ut o% little use.
#calar constructors with non;scalar -araeters can 7e used to take the %irst eleent %ro a non;scalar.
3or ea-le, the constructor 3loatFvec
8/12/2019 GLSL Specification 3.30.6.Clean
56/123
5 Operators and ()pressions
#oe use%ul vector constructors are as %ollows:
ve%4float) ** initiali"es ea%5 %omponent of t5e ve%4 :it5 t5e float
ve%1ive%1) ** ma3es a ve%1 :it5 %omponent:ise %onversion
ve%1mat2) ** t5e ve%1 is %olumn A follo:ed $& %olumn '
ve%2float/ float) ** initiali"es a ve%2 :it5 2 floats
ive%4int/ int/ int) ** initiali"es an ive%4 :it5 4 ints
$ve%1int/ int/ float/ float) ** uses 1 -oolean %onversions
ve%2ve%4) ** drops t5e t5ird %omponent of a ve%4
ve%4ve%1) ** drops t5e fourt5 %omponent of a ve%1
ve%4ve%2/ float) ** ve%46x . ve%26x/ ve%46& . ve%26&/ ve%46" . float
ve%4float/ ve%2) ** ve%46x . float/ ve%46& . ve%26x/ ve%46" . ve%26&
ve%1ve%4/ float)
ve%1float/ ve%4)
ve%1ve%2/ ve%2)
#oe ea-les o% these are:
ve%1 %olor . ve%1A6A/ '6A/ A6A/ '6A)
ve%1 rg$a . ve%1'6A) ** sets ea%5 %omponent to '6A
ve%4 rg$ . ve%4%olor) ** drop t5e 1t5 %omponent
o initiali?e the diagonal o% a atri with all other eleents set to ?ero:
mat2float)
mat4float)
mat1float)
hat is, resu'ti77is set to the %loat arguent %or all i < and set to ' %or all i:.
5"
8/12/2019 GLSL Specification 3.30.6.Clean
57/123
5 Operators and ()pressions
o initiali?e a atri 7$ s-eci%$ing vectors or scalars, the co-onents are assigned to the atri eleents
in colun;aTor order.
mat2ve%2/ ve%2) ** one %olumn per argument
mat4ve%4/ ve%4/ ve%4) ** one %olumn per argument
mat1ve%1/ ve%1/ ve%1/ ve%1) ** one %olumn per argument
mat4x2ve%2/ ve%2/ ve%2) ** one %olumn per argument
mat2float/ float/ ** first %olumn
float/ float) ** se%ond %olumn
mat4float/ float/ float/ ** first %olumn
float/ float/ float/ ** se%ond %olumn
float/ float/ float) ** t5ird %olumn
mat1float/ float/ float/ float/ ** first %olumn
float/ float/ float/ float/ ** se%ond %olumn
float/ float/ float/ float/ ** t5ird %olumn float/ float/ float/ float) ** fourt5 %olumn
mat2x4ve%2/ float/ ** first %olumn
ve%2/ float) ** se%ond %olumn
A wide range o% other -ossi7ilities eist, to construct a atri %ro vectors and scalars, as long as enough
co-onents are -resent to initiali?e the atri. o construct a atri %ro a atri:
mat4x4mat1x1) ** ta3es t5e upperleft 4x4 of t5e mat1x1
mat2x4mat1x2) ** ta3es t5e upperleft 2x2 of t5e mat1x1/ last ro: is A/A
mat1x1mat4x4) ** puts t5e mat4x4 in t5e upperleft/ sets t5e lo:er rig5t
** %omponent to '/ and t5e rest to A
.%." Structure Constructors
)nce a structure is de%ined, and its t$-e is given a nae, a constructor is availa7le with the sae nae to
construct instances o% that structure. 3or ea-le:
stru%t lig5t ;
float intensit&
ve%4 position
8/12/2019 GLSL Specification 3.30.6.Clean
58/123
5 Operators and ()pressions
.%.% rray Constructors
Arra$ t$-es can also 7e used as constructor naes, which can then 7e used in e-ressions or initiali?ers.
3or ea-le,
%onst float %>4? . float>4?76A/ D62/ '6')
%onst float d>4? . float>?76A/ D62/ '6')
float g
666
float a>7? . float>7?g/ '/ g/ 264/ g)
float $>4?
$ . float>4?g/ g H '6A/ g H 26A)
here ust 7e eactl$ the sae nu7er o% ar