GLSL Specification 4.20.11.Clean

download GLSL Specification 4.20.11.Clean

of 180

Transcript of GLSL Specification 4.20.11.Clean

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    1/180

    The OpenGL Shading Language

    Language Version: 4.20

    Document Revision: 11

    12-Dec-2011

    Editor: John Kessenich

    Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    2/180

    Copyright (c) 2008-2011 The Khronos Group Inc. All Rights Reserve .

    This speci!ic"tion is protecte #y copyright l"$s "n cont"ins %"teri"l propriet"ry to the Khronos Group&Inc. It or "ny co%ponents %"y not #e repro uce & repu#lishe & istri#ute & tr"ns%itte & ispl"ye ro" c"st or other$ise e'ploite in "ny %"nner $ithout the e'press prior $ritten per%ission o! 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 repro uce& isclose& or istri#ute its contents& or to%"nu!"cture& use& or sell "nything th"t it %"y escri#e& in $hole or in p"rt.

    Khronos Group gr"nts e'press per%ission to "ny current *ro%oter& Contri#utor or A opter %e%#er o!Khronos to copy "n re istri#ute +, /I I / versions o! this speci!ic"tion in "ny !"shion& provi e th"t, C ARG 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 " pro uct th"t is sol "s long "s such pro uct inclu es signi!ic"nt in epen ent $or

    evelope #y the seller. A lin to the current version o! this speci!ic"tion on the Khronos Group $e#-siteshoul #e inclu e $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"r ing this speci!ic"tion& inclu ing& $ithout li%it"tion& "ny i%plie $"rr"nties o! %erch"nt"#or !itness !or " p"rticul"r purpose or non-in!ringe%ent o! "ny intellectu"l property. Khronos Group %" es

    no& "n e'pressly iscl"i%s "ny& $"rr"nties& e'press or i%plie & reg"r ing the correctness& "ccur"cy&co%pleteness& ti%eliness& "n reli"#ility o! the speci!ic"tion. +n er no circu%st"nces $ill the 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& in irect& speci"l orconse5uenti"l "%"ges !or lost revenues& lost pro!its& or other$ise& "rising !ro% or in connection $iththese %"teri"ls.

    Khronos& penK / & penK G3& pen6G& pen A7& pen34 3 "n pen "re tr" e%"r s o!the Khronos Group Inc. C 44A/A is " tr" e%"r o! 3ony Co%puter ntert"in%ent Inc. use #yper%ission #y Khronos. penG4 "n pen 4 "re registere tr" e%"r s "n the penG4 3 logo is "tr" e%"r o! 3ilicon Gr"phics Inc. use #y per%ission #y Khronos. All other pro uct n"%es& tr" e%"r s&"n 9or co%p"ny n"%es "re use solely !or i enti!ic"tion "n #elong to their respective o$ners.

    2

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    3/180

    Table of Contents1 Introduction .................................................................................................................................1

    1.1 Acknowledg ents ................................................................................................................!1.! "hanges ................................................................................................................................!

    1.!.1 #u ar$ o% "hanges %ro Version &.1' .....................................................................!1.( )verview ..............................................................................................................................&1.& Error *andling ......................................................................................................................+1.+ $-ogra-hical "onventions .................................................................................................+1. De-recation ..........................................................................................................................+

    ! )verview o% )-en/0 #hading ....................................................................................................!.1 Verte 2rocessor ..................................................................................................................!.! essellation "ontrol 2rocessor .............................................................................................!.( essellation Evaluation 2rocessor ........................................................................................3

    !.& /eo etr$ 2rocessor .............................................................................................................3!.+ 4rag ent 2rocessor ..............................................................................................................3

    ( Basics ..........................................................................................................................................5(.1 "haracter #et ........................................................................................................................5(.! #ource #trings ......................................................................................................................5(.( 2re-rocessor .........................................................................................................................6(.& "o ents ..........................................................................................................................1&(.+ okens ................................................................................................................................1+(. Ke$words ............................................................................................................................1+(.3 Identi%iers...........................................................................................................................13(.5 De%initions..........................................................................................................................13

    (.5.1 #tatic 7se ....................................................................................................................15(.5.! 7ni%or and 8on97ni%or "ontrol 4low ..................................................................15(.5.( D$na icall$ 7ni%or E -ressions .............................................................................15

    & Varia les and $-es ..................................................................................................................16&.1 Basic $-es ........................................................................................................................16

    &.1.1 Void ............................................................................................................................!(&.1.! Booleans .....................................................................................................................!(&.1.( Integers .......................................................................................................................!(&.1.& 4loats ..........................................................................................................................!+&.1.+ Vectors ........................................................................................................................!&.1. ;atrices ......................................................................................................................!

    &.1.3 )-a

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    4/180

    &.1.1' I -licit "onversions ................................................................................................(1&.1.11 Initiali=ers .................................................................................................................(!

    &.! #co-ing ...............................................................................................................................(&&.( #torage >uali%iers ...............................................................................................................(

    &.(.1 De%ault #torage >uali%ier ............................................................................................(3&.(.! "onstant >uali%ier ......................................................................................................(3&.(.( "onstant E -ressions .................................................................................................(3&.(.& In-ut Varia les ...........................................................................................................(5&.(.+ 7ni%or ......................................................................................................................&'&.(. )ut-ut Varia les .........................................................................................................&1&.(.3 Inter%ace Blocks..........................................................................................................&(

    &.&0a$out >uali%iers ................................................................................................................&&.&.1 In-ut 0a$out >uali%iers ...............................................................................................&

    &.&.1.1 essellation Evaluation In-uts ............................................................................&5&.&.1.! /eo etr$ #hader In-uts ......................................................................................&6&.&.1.( 4rag ent #hader In-uts ......................................................................................+1

    &.&.! )ut-ut 0a$out >uali%iers ............................................................................................+!&.&.!.1 essellation "ontrol )ut-uts ..............................................................................+(&.&.!.! /eo etr$ )ut-uts ...............................................................................................+&&.&.!.( 4rag ent )ut-uts ...............................................................................................+

    &.&.( 7ni%or Block 0a$out >uali%iers ...............................................................................+3&.&.&)-auali%iers ............................................................................+6

    &.&.&.1Ato ic "ounter 0a$out >uali%iers ..................................................................... '&.&.&.! 4or at 0a$out >uali%iers .................................................................................... 1

    &.+ Inter-olation >uali%iers ...................................................................................................... (

    &.+.1 Redeclaring Built9in Inter-olation Varia les in the "o -ati ilit$ 2ro%ile ................. &&. 2ara eter >uali%iers ........................................................................................................... +&.3 2recision and 2recision >uali%iers ..................................................................................... +

    &.3.1 Range and 2recision ................................................................................................... +&.3.! 2recision >uali%iers ....................................................................................................&.3.( De%ault 2recision >uali%iers ....................................................................................... 3&.3.& Availa le 2recision >uali%iers .................................................................................... 5

    &.5 Variance and the Invariant >uali%ier .................................................................................. 5&.5.1 he Invariant >uali%ier ............................................................................................... 5&.5.! Invariance o% "onstant E -ressions ........................................................................... 6

    &.6 he 2recise >uali%ier .......................................................................................................... 6

    &.1' ;e or$ >uali%iers ...........................................................................................................31&.11 )rder o% >uali%ication ......................................................................................................3(+ )-erators and E -ressions ........................................................................................................3&

    +.1 )-erators ............................................................................................................................3&+.! Arra$ )-erations ...............................................................................................................3++.( 4unction "alls ....................................................................................................................3+

    4

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    5/180

    +.& "onstructors .......................................................................................................................3++.&.1 "onversion and #calar "onstructors ..........................................................................3++.&.! Vector and ;atri "onstructors .................................................................................3+.&.( #tructure "onstructors ................................................................................................35+.&.&Arra$ "onstructors .....................................................................................................36

    +.+ Vector and #calar "o -onents and 0ength .......................................................................36+. ;atri "o -onents ............................................................................................................51+.3 #tructure and Arra$ )-erations ..........................................................................................51+.5 Assign ents .......................................................................................................................5!+.6 E -ressions ........................................................................................................................5(+.1' Vector and ;atri )-erations ..........................................................................................5#tate ents and #tructure ...........................................................................................................55

    .1 4unction De%initions ...........................................................................................................56.1.1 4unction "alling "onventions ....................................................................................61.1.! #u routines .................................................................................................................6(

    .! #election .............................................................................................................................6&

    .( Iteration ..............................................................................................................................6+

    .& Ju -s ..................................................................................................................................63 Built9in Varia les ......................................................................................................................63

    3.1 Built9In 0anguage Varia les ..............................................................................................633.1.1 "o -ati ilit$ 2ro%ile Built9In 0anguage Varia les ..................................................1'&

    3.! "o -ati ilit$ 2ro%ile Verte #hader Built9In In-uts .......................................................1'33.( Built9In "onstants ............................................................................................................1'3

    3.(.1 "o -ati ilit$ 2ro%ile Built9In "onstants ..................................................................1'63.& Built9In 7ni%or #tate .....................................................................................................11'

    3.&.1 "o -ati ilit$ 2ro%ile #tate .......................................................................................11'5 Built9in 4unctions ...................................................................................................................11&

    5.1 Angle and rigono etr$ 4unctions ..................................................................................11+5.! E -onential 4unctions ......................................................................................................1135.( "o on 4unctions ..........................................................................................................1155.& 4loating92oint 2ack and 7n-ack 4unctions .....................................................................1!&5.+ /eo etric 4unctions ........................................................................................................1!5. ;atri 4unctions ..............................................................................................................1!55.3 Vector Relational 4unctions .............................................................................................1('5.5 Integer 4unctions ..............................................................................................................1(!5.6 e ture 4unctions .............................................................................................................1(&

    5.6.1 e ture >uer$ 4unctions ..........................................................................................1(+5.6.! e el 0ooku- 4unctions ...........................................................................................1(35.6.( e ture /ather Instructions ......................................................................................1&&5.6.& "o -ati ilit$ 2ro%ile e ture 4unctions ..................................................................1&3

    5.1' Ato ic9"ounter 4unctions .............................................................................................1&65.11 I age 4unctions .............................................................................................................1&6

    5

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    6/180

    5.1! 4rag ent 2rocessing 4unctions ......................................................................................1+!5.1!.1 Derivative 4unctions ..............................................................................................1+!5.1!.! Inter-olation 4unctions ...........................................................................................1+&

    5.1( 8oise 4unctions ..............................................................................................................1++5.1& /eo etr$ #hader 4unctions ...........................................................................................1+5.1+ #hader Invocation "ontrol 4unctions .............................................................................1+55.1 #hader ;e or$ "ontrol 4unction ..................................................................................1+5

    6 #hading 0anguage /ra ar %or "ore 2ro%ile ........................................................................1 '

    6

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    7/180

    1 Introduction

    his docu ent s-eci%ies onl$ version &.!' o% the )-en/0 #hading 0anguage. It re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    8/180

    1 Introduction

    1.1 Acknowledgmentshis s-eci%ication is ased on the work o% those who contri uted to -ast versions o% the )-en/0

    0anguage #-eci%ication, the )-en/0 E# !.' 0anguage #-eci%ication, and the %ollowing contri utors tothis version:

    2at Brown, 8VIDIAJe%% Bol=, 8VIDIA4rank "hen2ierre Boudier, A;D2iers Daniell, 8VIDIA"hris Dodd, 8VIDIA

    8ick *ae el, 8VIDIAJason /reen, rans/a ingBrent Insko, IntelJon 0eech

    Bill 0icea9Kane, A;DDaniel Koch, rans/a ingBarthold 0ichten elt, 8VIDIABruce ;err$, AR;Ro ert )hannessianAcorn 2oole$, 8VIDIAKevin RogovinIan Ro anick, Intel/reg Roth, 8vidia/raha #ellers, A;DDave #hreiner, AR;Jere $ #and el, A--leRo ert #i -son, >ualcoEric @erness, 8VIDIA;ark oung, A;D

    1.2 Changes

    1.2.1 Summary of Changes from Version .1!

    8ote: 8o %eatures were de-recated etween versions &.1' and &.!'.

    ;ove these -reviousl$ de-recated %eatures to e onl$ in the co -ati ilit$ -ro%ile:

    he ke$word attribute %or verte shader in-uts. 7se in instead.C

    he ke$word varying %or in-uts and out-uts. 7se in and out instead.C

    he original te turing uilt9in %unctions. 7se the new %or s instead.C

    he uilt9in varia les gl_FragColor and gl_FragData . 7se out instead.C

    Built9in constants related to these.

    2

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    9/180

    1 Introduction

    "hange %ro A#"II to 7 495 %or the language character set and also allow an$ characters insideco ents e ce-t the $te value 'C, including .

    Add line9continuation using , as in "FF.

    E# convergence

    Add ta le showing G reat ent o% ;is atched In-utH)ut-ut Varia les in section &.(.&.

    8a8s are not re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    10/180

    1 Introduction

    Add "9st$le curl$ race initiali=er lists s$nta %or initiali=ers. 4ull initiali=ation o% aggregates isre

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    11/180

    1 Introduction

    1. %rror &andling"o -ilers, in general, acce-t -rogra s that are ill9%or ed, due to the i -ossi ilit$ o% detecting all ill9

    %or ed -rogra s. 2orta ilit$ is onl$ ensured %or well9%or ed -rogra s, which this s-eci%icationdescri es. "o -ilers are encouraged to detect ill9%or ed -rogra s and issue diagnostic essages, ut arenot re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    12/180

    2 O#er#ie$ of OpenGL Shading

    he )-en/0 #hading 0anguage is actuall$ several closel$ related languages. hese languages are usedto create shaders %or each o% the -rogra a le -rocessors contained in the )-en/0 -rocessing -i-eline."urrentl$, these -rocessors are the verte , tessellation control, tessellation evaluation, geo etr$, and%rag ent -rocessors.

    7nless otherwise noted in this -a-er, a language %eature a--lies to all languages, and co on usage willre%er to these languages as a single language. he s-eci%ic languages will e re%erred to $ the na e o%the -rocessor the$ target: verte , tessellation control, tessellation evaluation, geo etr$, or %rag ent.

    ;ost )-en/0 state is not tracked or ade availa le to shaders. $-icall$, user9de%ined varia les will e

    used %or co unicating etween di%%erent stages o% the )-en/0 -i-eline. *owever, a s all a ount o%state is still tracked and auto aticall$ ade availa le to shaders, and there are a %ew uilt9in varia les %orinter%aces etween di%%erent stages o% the )-en/0 -i-eline.

    2.1 Verte* +rocessor he verte# "rocessor is a -rogra a le unit that o-erates on inco ing vertices and their associated data.

    "o -ilation units written in the )-en/0 #hading 0anguage to run on this -rocessor are called verte# s%aders. @hen a co -lete set o% verte shaders are co -iled and linked, the$ result in a verte# s%adere#ecutable that runs on the verte -rocessor.

    he verte -rocessor o-erates on one verte at a ti e. It does not re-lace gra-hics o-erations that re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    13/180

    2 Overview of OpenGL Shading

    written $ another invocation at an$ -oint during the sa e -hase, or i% two invocations atte -t to writedi%%erent values to the sa e -er9-atch out-ut in a single -hase.

    2." Tessellation %#aluation +rocessor he tessellation evaluation -rocessor is a -rogra a le unit that evaluates the -osition and other

    attri utes o% a verte generated $ the tessellation -ri itive generator, using a -atch o% inco ing verticesand their associated data. "o -ilation units written in the )-en/0 #hading 0anguage to run on this

    -rocessor are called tessellation evaluation shaders. @hen a co -lete set o% tessellation evaluationshaders are co -iled and linked, the$ result in a tessellation evaluation shader e ecuta le that runs on thetessellation evaluation -rocessor.

    Each invocation o% the tessellation evaluation e ecuta le co -utes the -osition and attri utes o% a singleverte generated $ the tessellation -ri itive generator. he e ecuta le can read the attri utes o% an$verte in the in-ut -atch, -lus the tessellation coordinate, which is the relative location o% the verte in the

    -ri itive eing tessellated. he e ecuta le writes the -osition and other attri utes o% the verte .

    2. Geometry +rocessor he geometr* "rocessor is a -rogra a le unit that o-erates on data %or inco ing vertices %or a -ri itive

    asse led a%ter verte -rocessing and out-uts a se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    14/180

    " -asics

    ".1 Character Sethe source character set used %or the )-en/0 shading languages, outside o% co ents, is a su set o%

    7 495. It includes the %ollowing characters:

    he letters a*+, A*,- and the underscore ? C.

    he nu ers */ .

    he s$ ols -eriod .C, -lus 0C, dash *C, slash C, asterisk 2C, -ercent 3 C, angled rackets 4 and5 C, s

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    15/180

    3 Ba ic

    string eing string '. 0ine nu ers are one ore than the nu er o% new lines that have een -rocessed,including counting the new lines that will e re oved $ the line9continuation character E C.

    0ines se-arated $ the line9continuation character -receding a new line are concatenated together e%oreeither co ent -rocessing or -re-rocessing. 8o white s-ace is su stituted %or the line9continuationcharacter. hat is, a single token could e %or ed $ the concatenation $ taking the characters at the endo% one line concatenating the with the characters at the eginning o% the ne t line.

    float f\oo;// forms a single line equivalent to float foo;// (assuming '\' is the last character before the new line and oo are// the first two characters of the next line)

    "." +reprocessor here is a -re-rocessor that -rocesses the source strings as -art o% the co -ilation -rocess.

    he co -lete list o% -re-rocessor directives is as %ollows.

    ##define#undef

    #if#ifdef#ifndef#else#elif#endif

    #error# ragma

    #extension#version

    #line

    he %ollowing o-erators are also availa le

    defined##

    Each nu er sign #C can e -receded in its line onl$ $ s-aces or hori=ontal ta s. It a$ also e

    %ollowed $ s-aces and hori=ontal ta s, -receding the directive. Each directive is ter inated $ a newline. 2re-rocessing does not change the nu er or relative location o% new lines in a source string.2re-rocessing takes -laces a%ter new lines have een re oved $ the line9continuation character.

    he nu er sign #C on a line $ itsel% is ignored. An$ directive not listed a ove will cause a diagnosticessage and ake the i -le entation treat the shader as ill9%or ed.

    !

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    16/180

    3 Ba ic

    #define and #undef %unctionalit$ are de%ined as is standard %or "FF -re-rocessors %or acro de%initions oth with and without acro -ara eters.

    he %ollowing -rede%ined acros are availa le

    !!" $%!!!!& "%!!!! % *$!!

    __L+, __ will su stitute a deci al integer constant that is one ore than the nu er o% -receding newlines in the current source string.

    __F+L __ will su stitute a deci al integer constant that sa$s which source string nu er is currentl$ eing -rocessed.

    __V R(+&,__ will su stitute a deci al integer re%lecting the version nu er o% the )-en/0 shadinglanguage. he version o% the shading language descri ed in this docu ent will have __V R(+&,__su stitute the deci al integer &!'.

    All acro na es containing two consecutive underscores __ C are reserved %or %uture use as -rede%inedacro na es. All acro na es -re%i ed with G/0? G/0 %ollowed $ a single underscoreC are also

    reserved.

    #if- #ifdef- #ifndef- #else- #elif- and #endif are de%ined to o-erate as is standard %or "FF -re-rocessors.E -ressions %ollowing #if and #elif are %urther restricted to e -ressions o-erating on literal integerconstants, -lus identi%iers consu ed $ the defined o-erator. It is an error to use #if or #elif one -ressions containing unde%ined acro na es, other than as argu ents to the defined o-erator."haracter constants are not su--orted. he o-erators availa le are as %ollows.

    +recedence Operator class Operators ssociati#ity

    1 highestC -arenthetical grou-ing C 8A! unar$ de%ined

    F 9 O PRight to 0e%t

    ( ulti-licative H Q 0e%t to Right

    & additive F 9 0e%t to Right

    + it9wise shi%t SS 0e%t to Right

    relational S T ST 0e%t to Right

    3 e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    17/180

    3 Ba ic

    he defined o-erator can e used in either o% the %ollowing wa$s:

    defined identifier defined ( identifier )

    wo tokens in a acro can e concatenated into one token using the token -asting ##C o-erator, as isstandard %or "FF -re-rocessors. he result ust e a valid single token, which will then e su Lect to

    acro e -ansion. hat is, acro e -ansion ha--ens onl$ a%ter token -asting. here are no other nu ersign ased o-erators e.g., no # or #F C, nor is there a si+eof o-erator.

    he se antics o% a--l$ing o-erators to integer literals in the -re-rocessor atch those standard in the "FF -re-rocessor, not those in the )-en/0 #hading 0anguage.

    2re-rocessor e -ressions will e evaluated according to the ehavior o% the host -rocessor, not the -rocessor targeted $ the shader.

    #error will cause the i -le entation to -ut a diagnostic essage into the shader o LectMs in%or ation logsection .1.1! G#hader and 2rogra >ueries in the )-en/0 /ra-hics #$ste #-eci%ication %or how to

    access a shader o LectMs in%or ation logC. he essage will e the tokens %ollowing the #error directive,u- to the %irst new line. he i -le entation ust then consider the shader to e ill9%or ed.

    # ragma allows i -le entation de-endent co -iler control. okens %ollowing # ragma are not su Lectto -re-rocessor acro e -ansion. I% an i -le entation does not recogni=e the tokens %ollowing# ragma , then it will ignore that -rag a. he %ollowing -rag as are de%ined as -art o% the language.

    # ragma +,-"

    he %GD(H -rag a is used to reserve -rag as %or use $ %uture revisions o% this language. 8oi -le entation a$ use a -rag a whose %irst token is %GD(H .

    # ragma o timi.e(on)# ragma o timi.e(off)

    can e used to turn o%% o-ti i=ations as an aid in develo-ing and de ugging shaders. It can onl$ e usedoutside %unction de%initions. B$ de%ault, o-ti i=ation is turned on %or all shaders. he de ug -rag a

    # ragma debug(on)# ragma debug(off)

    can e used to ena le co -iling and annotating a shader with de ug in%or ation, so that it can e usedwith a de ugger. It can onl$ e used outside %unction de%initions. B$ de%ault, de ug is turned o%%.

    #haders should declare the version o% the language the$ are written to. he language version a shader iswritten to is s-eci%ied $

    #version number profile opt

    where number ust e a version o% the language, %ollowing the sa e convention as __V R(+&,__ a ove.he directive G #version " is re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    18/180

    3 Ba ic

    #haders declaring version 1.&', 1.+', (.(', &.', or &.1 o% the shading language can e linked with shadersdeclaring version &.!' in the sa e -rogra . #haders targeting earlier versions 1.(' or earlierC o% theshading language cannot e linked with version &.!' shaders.

    I% the o-tional "ro ile argu ent is -rovided, it ust e the na e o% an )-en/0 -ro%ile. "urrentl$, thereare two choices:

    corecom atibilit

    I% no "ro ile argu ent is -rovided, the de%ault is core . 7nless otherwise s-eci%ied, this s-eci%ication isdocu enting the core -ro%ile, and ever$thing s-eci%ied %or the core -ro%ile is also availa le in theco -ati ilit$ -ro%ile. 4eatures s-eci%ied as elonging s-eci%icall$ to the co -ati ilit$ -ro%ile are notavaila le in the core -ro%ile.

    here is a uilt9in acro de%inition %or each -ro%ile the i -le entation su--orts. All i -le entations -rovide the %ollowing acro:

    #define -"!core! rofile 0

    I -le entations -roviding the co -ati ilit$ -ro%ile -rovide the %ollowing acro:

    #define -"!com atibilit ! rofile 0

    he #version directive ust occur in a shader e%ore an$thing else, e ce-t %or co ents and white s-ace.

    12

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    19/180

    3 Ba ic

    B$ de%ault, co -ilers o% this language ust issue co -ile ti e s$ntactic, gra atical, and se anticerrors %or shaders that do not con%or to this s-eci%ication. An$ e tended ehavior ust %irst e ena led.Directives to control the ehavior o% the co -iler with res-ect to e tensions are declared with the

    #e tension directive#extension extension_name : behavior #extension all : behavior

    where e#tension_name is the na e o% an e tension. E tension na es are not docu ented in thiss-eci%ication. he token all eans the ehavior a--lies to all e tensions su--orted $ the co -iler. hebe%avior can e one o% the %ollowing

    behavior %ffect

    re/uire Behave as s-eci%ied $ the e tension e#tension_name.

    /ive an error on the #e tension i% the e tension e#tension_name is notsu--orted, or i% all is s-eci%ied.

    enable Behave as s-eci%ied $ the e tension e#tension_name.

    @arn on the #e tension i% the e tension e#tension_name is not su--orted.

    /ive an error on the #e tension i%all is s-eci%ied.

    $arn Behave as s-eci%ied $ the e tension e#tension_name , e ce-t issue warningson an$ detecta le use o% that e tension, unless such use is su--orted $ otherena led or re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    20/180

    3 Ba ic

    he initial state o% the co -iler is as i% the directive

    #extension all 1 disable

    was issued, telling the co -iler that all error and warning re-orting ust e done according to thiss-eci%ication, ignoring an$ e tensions.

    Each e tension can de%ine its allowed granularit$ o% sco-e. I% nothing is said, the granularit$ is a shaderthat is, a single co -ilation unitC, and the e tension directives ust occur e%ore an$ non9-re-rocessor

    tokens. I% necessar$, the linker can en%orce granularities larger than a single co -ilation unit, in whichcase each involved shader will have to contain the necessar$ e tension directive.

    ;acro e -ansion is not done on lines containing #e tension and #version directives.

    #line ust have, a%ter acro su stitution, one o% the %ollowing %or s:

    #line line#line line source-string-number

    where line and source-string-number are constant integer e -ressions. A%ter -rocessing this directiveincluding its new lineC, the i -le entation will ehave as i% it is co -iling at line nu er line and source

    string nu er source-string-number . #u se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    21/180

    3 Ba ic

    ".' To0enshe language is a se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    22/180

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    23/180

    3 Ba ic

    goto

    inline noinline ublic static e tern e ternal interface

    long short half fi ed unsigned su er in ut out ut

    hvec" hvec' hvec fvec" fvec' fvec

    sam ler'DJect

    filter

    si+eof cast

    names ace using

    row_maMor

    In addition, all identi%iers containing two consecutive underscores __ C are reserved as -ossi le %utureke$words.

    ". IdentifiersIdenti%iers are used %or varia le na es, %unction na es, structure na es, and %ield selectors %ieldselectors select co -onents o% vectors and atrices si ilar to structure e ers, as discussed in section+.+ GVector "o -onents and section +. G;atri "o -onents C. Identi%iers have the %or

    identi ier nondigitidenti ier nondigitidenti ier digit

    nondigit: one o% _ a b c d e f g h i M k l m n o N r s t u v w y + A K C D O & ( ! I P Q H L R ) S T J % G U V W X Y ,

    digit : one o% 1 " ' Z $ [ \ /

    Identi%iers starting with Ggl? are reserved %or use $ )-en/0, and a$ not e declared in a shader aseither a varia le or a %unction. *owever, as noted in the s-eci%ication, there are so e cases where

    -reviousl$ declared varia les can e redeclared, and -redeclared Xgl?X na es are allowed to eredeclared in a shader onl$ %or these s-eci%ic -ur-oses. ;ore generall$, it is an error to redeclare avaria le, including those starting Ggl? .

    ".3 )efinitions#o e language rules descri ed elow de-end on the %ollowing de%initions.

    17

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    24/180

    3 Ba ic

    ".3.1 Static 4se

    A shader contains a static use o% or static assignment toC a varia le # i%, a%ter -re-rocessing, the shadercontains a state ent that would read or writeC #, whether or not run9ti e %low o% control will cause thatstate ent to e e ecuted.

    ".3.2 4niform and 5on64niform Control ,lo$

    @hen e ecuting state ents in a %rag ent shader, control %low starts as uni orm control lo/ N all %rag entsenter the sa e control -ath into main C. "ontrol %low eco es non-uni orm when di%%erent %rag entstake di%%erent -aths through control9%low state ents selection, iteration, and Lu -sC. "ontrol %lowsu se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    25/180

    Variables and Types

    All varia les and %unctions ust e declared e%ore eing used. Varia le and %unction na es areidenti%iers.

    here are no de%ault t$-es. All varia le and %unction declarations ust have a declared t$-e, ando-tionall$

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    26/180

    4 #aria$%e and &'pe

    Type 7eaning

    ivec' a three9co -onent signed integer vector

    ivec a %our9co -onent signed integer vector uvec" a two9co -onent unsigned integer vector

    uvec' a three9co -onent unsigned integer vector

    uvec a %our9co -onent unsigned integer vector

    mat" a !Y! %loating9-oint atri

    mat' a (Y( %loating9-oint atri

    mat a &Y& %loating9-oint atri

    mat" " sa e as a mat"

    mat" ' a %loating9-oint atri with ! colu ns and ( rows

    mat" a %loating9-oint atri with ! colu ns and & rowsmat' " a %loating9-oint atri with ( colu ns and ! rows

    mat' ' sa e as a mat'

    mat' a %loating9-oint atri with ( colu ns and & rows

    mat " a %loating9-oint atri with & colu ns and ! rows

    mat ' a %loating9-oint atri with & colu ns and ( rows

    mat sa e as a mat

    dmat" a !Y! dou le9-recision %loating9-oint atri

    dmat' a (Y( dou le9-recision %loating9-oint atri

    dmat a &Y& dou le9-recision %loating9-oint atridmat" " sa e as a dmat"

    dmat" ' a dou le9-recision %loating9-oint atri with ! colu ns and ( rows

    dmat" a dou le9-recision %loating9-oint atri with ! colu ns and & rows

    dmat' " a dou le9-recision %loating9-oint atri with ( colu ns and ! rows

    dmat' ' sa e as a dmat'

    dmat' a dou le9-recision %loating9-oint atri with ( colu ns and & rows

    dmat " a dou le9-recision %loating9-oint atri with & colu ns and ! rows

    dmat ' a dou le9-recision %loating9-oint atri with & colu ns and ( rows

    dmat sa e as a dmat

    2"

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    27/180

    4 #aria$%e and &'pe

    4loating 2oint )-a

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    28/180

    4 #aria$%e and &'pe

    Type 7eaning

    isam ler"Diimage"D

    a handle %or accessing an integer !D te ture

    isam ler'Diimage'D

    a handle %or accessing an integer (D te ture

    isam lerCubeiimageCube

    a handle %or accessing an integer cu e a--ed te ture

    isam ler"DJectiimage"DJect

    a handle %or accessing an integer !D rectangular te ture

    isam ler1DArrayiimage1DArray

    a handle %or accessing an integer 1D arra$ te ture

    isam ler"DArrayiimage"DArray

    a handle %or accessing an integer !D arra$ te ture

    isam lerKufferiimageKuffer a handle %or accessing an integer u%%er te ture

    isam ler"DL%iimage"DL%

    a handle %or accessing an integer !D ulti9sa -le te ture

    isam ler"DL%Arrayiimage"DL%Array

    a handle %or accessing an integer !D ulti9sa -le arra$ te ture

    isam lerCubeArrayiimageCubeArray

    a handle %or accessing an integer cu e a- arra$ te ture

    7nsigned Integer )-a

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    29/180

    4 #aria$%e and &'pe

    Type 7eaning

    usam lerKufferuimageKuffer

    a handle %or accessing an unsigned integer u%%er te ture

    usam ler"DL%uimage"DL%

    a handle %or accessing an unsigned integer !D ulti9sa -le te ture

    usam ler"DL%Arrayuimage"DL%Array

    a handle %or accessing an unsigned integer !D ulti9sa -le te turearra$

    usam lerCubeArrayuimageCubeArray

    a handle %or accessing an unsigned integer cu e a- arra$ te ture

    In addition, a shader can aggregate these asic t$-es using arra$s and structures to uild ore co -let$-es.

    here are no -ointer t$-es.

    .1.1 Void

    4unctions that do not return a value ust e declared as void . here is no de%ault %unction return t$-e.he ke$word void cannot e used in an$ other declarations e ce-t %or e -t$ %or al or actual -ara eter

    listsC.

    .1.2 -ooleans

    o ake conditional e ecution o% code easier to e -ress, the t$-e bool is su--orted. here is noe -ectation that hardware directl$ su--orts varia les o% this t$-e. It is a genuine Boolean t$-e, holdingonl$ one o% two values eaning either true or %alse. wo ke$words true and false can e used as literalBoolean constants. Booleans are declared and o-tionall$ initiali=ed as in the %ollow e a -le:

    bool success; // declare success to be a 9ooleanbool done 3 false; // declare and initiali.e done

    he right side o% the assign ent o-erator @C ust e an e -ression whose t$-e is bool .

    E -ressions used %or conditional Lu -s if- for- B- while- do*while C ust evaluate to the t$-e bool .

    .1." Integers

    #igned and unsigned integer varia les are %ull$ su--orted. In this docu ent, the ter integer is eant togenerall$ include oth signed and unsigned integers. 7nsigned integers have e actl$ (! its o% -recision.#igned integers use (! its, including a sign it, in two s co -le ent %or . )-erations resulting inover%low or under%low will not cause an$ e ce-tion, nor will the$ saturate, rather the$ will Gwra- to $ieldthe low9order (! its o% the result.

    Integers are declared and o-tionall$ initiali=ed with integer e -ressions, as in the %ollowing e a -le:

    int i7 : 3 u; // u establishes the t e as uint

    23

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    30/180

    4 #aria$%e and &'pe

    0iteral integer constants can e e -ressed in deci al ase 1'C, octal ase 5C, or he adeci al ase 1 Cas %ollows.

    integer-constant :decimal-constant integer-su i# o"t octal-constant integer-su i# o"t %e#adecimal-constant integer-su i# o"t

    integer-su i#: one o% u U

    decimal-constant :non ero-digit decimal-constant digit

    octal-constant :

    octal-constant octal-digit

    %e#adecimal-constant :' %e#adecimal-digit 'Z %e#adecimal-digit %e#adecimal-constant %e#adecimal-digit

    digit :

    non ero-digit

    non ero-digit : one o% 1 " ' Z $ [ \ /

    octal-digit Bone o% 1 " ' Z $ [

    %e#adecimal-digit B one o% 1 " ' Z $ [ \ /

    a b c d e f A K C D O &

    8o white s-ace is allowed etween the digits o% an integer constant, including a%ter the leading or a%terthe leading or X o% a constant, or e%ore the su%%i u or U . @hen tokeni=ing, the a i al token

    atching the a ove will e recogni=ed e%ore a new token is started. @hen the su%%i u or U is -resent,the literal has t$-e uint , otherwise the t$-e is int . A leading unar$ inus sign 9C is inter-reted as an

    arith etic unar$ negation, not as -art o% the constant. *ence, literals the selves are alwa$s e -ressedwith non9negative s$nta , though the$ could result in a negative value.

    It is an error to -rovide a literal integer whose it -attern cannot %it in (! its. he it -attern o% the literalis alwa$s used un odi%ied. #o a signed literal whose it -attern includes a set sign it creates a negativevalue. 4or e a -le,

    24

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    31/180

    4 #aria$%e and &'pe

    int a 3 ?xffffffff; // >< bits7 a gets the value 20int b 3 ?xffffffff@; // % * 1 can't convert uint to intuint c 3 ?xffffffff; // >< bits7 c gets the value ?x&&&&&&&&

    uint d 3 ?xffffffff@; // >< bits7 d gets the value ?x&&&&&&&&int e 3 20; // the literal is 07 then negation is erformed7 // and the resulting non2literal >?????????; // a signed decimal literal ta=ing >< bits7 // setting the sign bit7 g gets 20< bitsint = 3 ?xF???????; // = gets 2B F 33 ?xF???????int l 3 B F; // l gets 2B F (the literal set the sign bit)

    Des-ite all these e a -les initiali=ing varia les, literals are recogni=ed and given values and t$-esinde-endentl$ o% their conte t.

    .1. ,loats

    #ingle9-recision and dou le9-recision %loating -oint varia les are availa le %or use in a variet$ o% scalarcalculations. 4loating9-oint varia les are de%ined as in the %ollowing e a -le:

    float a7 b 3 05E;double c7 d 3

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    32/180

    4 #aria$%e and &'pe

    4loating9-oint constants are de%ined as %ollows.

    loating-constant : ractional-constant e#"onent-"art

    o"t loating-su i#o"t

    digit-se uence e#"onent-"art loating-su i# o"t

    ractional-constant :digit-se uence . digit-se uencedigit-se uence .

    . digit-se uence

    e#"onent-"art :e sign o"t digit-se uence

    E sign o"t digit-se uence

    sign : one o%

    0 ] digit-se uence :

    digit digit-se uence digit

    loating-su i#:one o% f & lf H&

    A deci al -oint . C is not needed i% the e -onent -art is -resent. 8o white s-ace a$ a--ear an$wherewithin a %loating9-oint constant, including e%ore a su%%i . @hen tokeni=ing, the a i al token atchingthe a ove will e recogni=ed e%ore a new token is started. @hen the su%%i Xl%X or X04X is -resent, theliteral has t$-e double . )therwise, the literal has t$-e float . A leading unar$ inus sign *C is inter-retedas a unar$ o-erator and is not -art o% the %loating9-oint constant

    .1.' Vectors

    he )-en/0 #hading 0anguage includes data t$-es %or generic !9, (9, and &9co -onent vectors o%%loating9-oint values, integers, or Booleans. 4loating9-oint vector varia les can e used to store colors,nor als, -ositions, te ture coordinates, te ture looku- results and the like. Boolean vectors can e used%or co -onent9wise co -arisons o% nu eric vectors. #o e e a -les o% vector declaration are:

    vec< texcoord07 texcoord osition;vec m -9D;ivec< texture"oo=u ;bvec> less;

    Initiali=ation o% vectors can e done with constructors, which are discussed shortl$.

    .1.( 7atrices

    he )-en/0 #hading 0anguage has uilt9in t$-es %or !Y!, !Y(, !Y&, (Y!, (Y(, (Y&, &Y!, &Y(, and &Y&atrices o% %loating9-oint nu ers. ;atri t$-es eginning with X atX have single9-recision co -onents

    26

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    33/180

    4 #aria$%e and &'pe

    while atri t$-es eginning with Xd atX have dou le9-recision co -onents. he %irst nu er in thet$-e is the nu er o% colu ns, the second is the nu er o% rows. I% there is onl$ one nu er, the atriis sx< m; // a matrix with > columns and < rowsdmat highHrecisionG H;dmat

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    34/180

    4 #aria$%e and &'pe

    I age varia les are handles to one9, two9, or three9di ensional i ages corres-onding to all or a -ortiono% a single level o% a te ture i age ound to an i age unit. here are distinct i age t$-es %or each te turetarget, and %or each o% %loat, integer, and unsigned integer data t$-es. I age accesses should use an i age

    t$-e that atches the target o% the te ture whose level is ound to the i age unit, or %or non9la$ered indings o% (D or arra$ i ages should use the i age t$-e that atches the di ensionalit$ o% the la$er o%the i age i.e., a la$er o% (D, !DArra$, "u e, or "u eArra$ should use image"D , a la$er o% 1DArra$should use image1D , and a la$er o% !D;#Arra$ should use image"DL% C. I% the i age target t$-e doesnot atch the ound i age in this anner, i% the data t$-e does not atch the ound i age, or i% the%or at la$out uali%iers . @hen aggregated into arra$swithin a shader, ato ic counters can onl$ e inde ed with a d$na icall$ uni%or integral e -ression,otherwise results are unde%ined.

    .1.3 Structures

    7ser9de%ined t$-es can e created $ aggregating other alread$ de%ined t$-es into a structure using thestruct ke$word. 4or e a -le,

    struct light 4

    float intensit ; vec> osition;8 light ar;

    In this e a -le, lig%t eco es the na e o% the new t$-e, and lig%tVar eco es a varia le o% t$-e lig%t .o declare varia les o% the new t$-e, use its na e without the ke$word struct C.

    light light ar

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    35/180

    4 #aria$%e and &'pe

    basic-t*"e declarators

    where name eco es the user9de%ined t$-e, and can e used to declare varia les to e o% this new t$-e.he name shares the sa e na e s-ace as other varia les, t$-es, and %unctions. All -reviousl$ visi le

    varia les, t$-es, constructors, or %unctions with that na e are hidden. he o-tional uali ier onl$ a--liesto an$ declarators , and is not -art o% the t$-e eing de%ined %or name .

    #tructures ust have at least one e er declaration. ;e er declarators a$ contain -recision

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    36/180

    4 #aria$%e and &'pe

    float frequenciesI>J;uniform vec lightHositionI J;light lightsIJ;

    const int num"ights 3

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    37/180

    4 #aria$%e and &'pe

    .1.1! Implicit Con#ersions

    In so e situations, an e -ression and its t$-e will e i -licitl$ converted to a di%%erent t$-e. he%ollowing ta le shows all allowed i -licit conversions:

    Type of e*pression Can be implicitly con#erted to

    int uint

    intuint

    float

    intuintfloat

    double

    ivec" uvec"

    ivec' uvec'

    ivec uvecivec"uvec"

    vec"

    ivec'uvec'

    vec'

    ivecuvec

    vec

    ivec"uvec"vec"

    dvec"

    ivec'uvec'vec'

    dvec'

    ivecuvecvec

    dvec

    mat" dmat"

    mat' dmat'

    mat dmat

    mat" ' dmat" '

    mat" dmat"

    mat' " dmat' "

    mat' dmat'

    mat " dmat "

    mat ' dmat '

    31

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    38/180

    4 #aria$%e and &'pe

    here are no i -licit arra$ or structure conversions. 4or e a -le, an arra$ o% int cannot e i -licitl$converted to an arra$ o% float .

    @hen an i -licit conversion is done, it is not a re9inter-retation o% the e -ression s it -attern, ut aconversion o% its value to an e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    39/180

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    40/180

    4 #aria$%e and &'pe

    struct 4 float a; int b;

    8 e 3 4 07 > 8; // legal7 first initiali.er is converted

    All o% the %ollowing declarations are illegal.

    int a 3 true; // illegalvec bI(?5?)7 vec>(05?) 8; // illegal

    struct 0 4 vec a; vec b;8;

    struct 4

    float s; float t;8 dIJ 3 4 0(vec (?5?)7 vec (050)) 8; // illegal

    I% an initiali=er o% either %or C is -rovided %or an unsi=ed arra$, the si=e o% the arra$ is deter ined $ thenu er o% to-9level non9nestedC initiali=ers within the initiali=er. All o% the %ollowing declarations createarra$s e -licitl$ si=ed with %ive ele ents:

    float aIJ 3 floatIJ(>5 7 5

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    41/180

    4 #aria$%e and &'pe

    @ithin a declaration, the sco-e o% a na e starts i ediatel$ a%ter the initiali=er i% -resent or i ediatel$a%ter the na e eing declared i% not. #everal e a -les:

    int x 3 0;4

    int x 3

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    42/180

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    43/180

    4 #aria$%e and &'pe

    0ocal varia les can onl$ use the const storage

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    44/180

    4 #aria$%e and &'pe

    *owever, the lowest -recedence o-erators o% the se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    45/180

    4 #aria$%e and &'pe

    location . Verte shader in-uts can e single9 or dou le9-recision %loating9-oint scalars, vectors, andatrices, or signed9 and unsigned9integer scalars and vectors. Verte shader in-uts can also %or arra$s

    o% these t$-es, ut not structures.

    E a -le declarations in a verte shader:

    in vec osition;in vec> normal;in vec< texLoordI J;

    It is e -ected that gra-hics hardware will have a s all nu er o% %i ed vector locations %or -assing vertein-uts. here%ore, the )-en/0 #hading language de%ines each non9 atri in-ut varia le as taking u- onesuch vector location. here is an i -le entation de-endent li it on the nu er o% locations that can eused, and i% this is e ceeded it will cause a link error. Declared in-ut varia les that are not staticall$ useddo not count against this li it.C A scalar in-ut counts the sa e a ount against this li it as a vec , soa--lications a$ want to consider -acking grou-s o% %our unrelated %loat in-uts together into a vector to

    etter utili=e the ca-a ilities o% the underl$ing hardware. A atri in-ut will use u- ulti-le locations.

    he nu er o% locations used will e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    46/180

    4 #aria$%e and &'pe

    evaluation shaders. As with other in-ut varia les, -er9-atch in-uts ust e declared using the sa e t$-eand (?5C7 ?5C7 ?5

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    47/180

    4 #aria$%e and &'pe

    uni%or varia les and the nu er o% uilt9in uni%or varia les that are used within a shader are addedtogether to deter ine whether availa le uni%or storage has een e ceeded.

    I% ulti-le shaders are linked together, then the$ will share a single glo al uni%or na e s-ace, includingwithin a language as well as across languages. *ence, the t$-es and initiali=ers o% uni%or varia les withthe sa e na e ust atch across all shaders that are linked into a single -rogra .

    It is legal %or so e shaders to -rovide an initiali=er %or a -articular uni%or varia le, while another shaderdoes not, ut all -rovided initiali=ers ust e e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    48/180

    4 #aria$%e and &'pe

    out-ut varia le or out-ut lock, see inter%ace locks elowC needs to e declared as an arra$. 4ore a -le,

    out float fooIJ; // feeds next stage in ut in float fooIJ

    Each ele ent o% such an arra$ corres-onds to one verte o% the -ri itive eing -roduced. Each arra$ cano-tionall$ have a si=e declared. he arra$ si=e will e set $ or i% -rovided ust e consistent withC theout-ut la$out declaration sC esta lishing the nu er o% vertices in the out-ut -atch, as descri ed later insection &.&.!.1 G essellation "ontrol )ut-uts .

    As descri ed under the section &.(.& GIn-ut Varia les a ove, i% a -er9verte out-ut o% the tessellationcontrol shader is itsel% an arra$ with ulti-le values -er verte , it ust a--ear in an out-ut lock seeinter%ace locks elowC in the tessellation control shader with a lock instance na e declared as an arra$.

    Each tessellation control shader invocation has a corres-onding out-ut -atch verte , and a$ assignvalues to -er9verte out-uts onl$ i% the$ elong to that corres-onding verte . I% a -er9verte out-utvaria le is used as an l9value, it is an error i% the e -ression indicating the verte inde is not the identi%ier

    gl_+nvocation+D .he order o% e ecution o% a tessellation control shader invocation relative to the other invocations %or the

    sa e in-ut -atch is unde%ined unless the uilt9in %unction barrier C is used. his -rovides so e controlover relative e ecution order. @hen a shader invocation calls barrier C, its e ecution -auses until allother invocations have reached the sa e -oint o% e ecution. )ut-ut varia le assign ents -er%or ed $an$ invocation e ecuted -rior to calling barrier C will e visi le to an$ other invocation a%ter the call tobarrier C returns.

    Because tessellation control shader invocations e ecute in unde%ined order etween arriers, the values o% -er9verte or -er9-atch out-ut varia les will so eti es e unde%ined. "onsider the eginning and end o%shader e ecution and each call to barrier C as s$nchroni=ation -oints. he value o% an out-ut varia lewill e unde%ined in an$ o% the three %ollowing cases:

    1. At the eginning o% e ecution.!. At each s$nchroni=ation -oint, unless

    the value was well9de%ined a%ter the -revious s$nchroni=ation -oint and was not written $ an$invocation since, or

    the value was written $ e actl$ one shader invocation since the -revious s$nchroni=ation -oint, or

    the value was written $ ulti-le shader invocations since the -revious s$nchroni=ation -oint,and the last write -er%or ed $ all such invocations wrote the sa e value.

    (. @hen read $ a shader invocation, i%

    the value was unde%ined at the -revious s$nchroni=ation -oint and has not een writen $ thesa e shader invocation since, or

    the out-ut varia le is written to $ an$ other shader invocation etween the -revious and ne t

    s$nchroni=ation -oints, even i% that assign ent occurs in code %ollowing the read.

    4rag ent out-uts out-ut -er9%rag ent data and are declared using the out storage

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    49/180

    4 #aria$%e and &'pe

    o% an$ these. ;atrices and structures cannot e out-ut. 4rag ent out-uts are declared as in the %ollowinge a -les:

    out vec &ragmentLolor;out uint "uminosit ;

    .". Interface -loc0s

    In-ut, out-ut, and uni%or varia le declarations can e grou-ed into na ed inter%ace locks to -rovidecoarser granularit$ acking than is achieva le with individual declarations. he$ can have an o-tionalinstance na e, used in the shader to re%erence their e ers. An out-ut lock o% one -rogra a lestage is acked $ a corres-onding in-ut lock in the su se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    50/180

    4 #aria$%e and &'pe

    $-es and declarators are the sa e as %or other in-ut, out-ut, and uni%or varia le declarations outside locks, with these e ce-tions:

    initiali=ers are not allowed

    o-a

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    51/180

    4 #aria$%e and &'pe

    I% an instance na e instance-name C is not used, the na es declared inside the lock are sco-ed at theglo al level and accessed as i% the$ were declared outside the lock. I% an instance na e instance-name Cis used, then it -uts all the e ers inside a sco-e within its own na e s-ace, accessed with the %ield

    selector . C o-erator analogousl$ to structuresC. 4or e a -le,in "ight 4 vec "ightHos; vec> "ightLolor;8;in Lolored+exture 4 vec Lolor; vec< +exLoord;8 Gaterial; // instance namevec> Lolor; // different Lolor than Gaterial5Lolorvec "ightHos; // illegal7 alread defined555555 3 "ightHos; // accessing "ightHos555 3 Gaterial5Lolor; // accessing Lolor in Lolored+exture bloc=

    )utside the shading language i.e., in the A2IC, e ers are si ilarl$ identi%ied e ce-t the lock na e isalwa$s used in -lace o% the instance na e A2I accesses are to inter%aces, not to shadersC. I% there is noinstance na e, then the A2I does not use the lock na e to access a e er, Lust the e er na e.

    out ertex 4 vec Hosition; // DH transform/feedbac= will use ertex5Hosition vec< +exture;8 Loords; // shader will use Loords5Hosition

    out ertex< 4 vec Lolor; // DH will use Lolor

    8;

    4or locks declared as arra$s, the arra$ inde ust also e included when accessing e ers, as in thise a -le

    uniform +ransform 4 // DH uses +ransformI

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    52/180

    4 #aria$%e and &'pe

    to re%er to o%%sets or other characteristics o% a lock e er, an arra$ inde ust not e s-eci%ied e.g.,$rans orm.7odelVie/7atri# C.

    /eo etr$ shader in-ut locks ust e declared as arra$s and %ollow the arra$ declaration and linkingrules %or all geo etr$ shader in-uts. All other in-ut and out-ut lock arra$s ust s-eci%$ an arra$ si=e.

    here is an i -le entation de-endent li it on the nu er o% uni%or locks that can e used -er stage.I% this li it is e ceeded, it will cause a link error.

    . Layout :ualifiers0a$out

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    53/180

    4 #aria$%e and &'pe

    la*out- uali ier-id location @ integer-constant

    )nl$ one argu ent is acce-ted. 4or e a -le,

    la out(location 3 >) in vec normal;

    will esta lish that the shader in-ut normal is assigned to vector location nu er (. 4or verte shaderin-uts, the location s-eci%ies the nu er o% the generic verte attri ute %ro which in-ut values are taken.4or in-uts o% all other shader t$-es, the location s-eci%ies a vector nu er that can e used to atchagainst out-uts %ro a -revious shader stage, even i% that shader is in a di%%erent -rogra o Lect.

    I% a verte shader in-ut is an$ scalar or vector t$-e, it will consu e a single location. I% a non9verteshader in-ut is a scalar or vector t$-e other than dvec' or dvec , it will consu e a single location, whilet$-es dvec' or dvec will consu e two consecutive locations. In-uts o% t$-e double and dvec" willconsu e onl$ a single location, in all stages.

    I% the declared in-ut is an arra$ o% si=e n and each ele ent takes m locations, it will e assigned m nconsecutive locations starting with the location s-eci%ied. 4or e a -le,

    la out(location 3 B) in vec colorsI>J;

    will esta lish that the shader in-ut colors is assigned to vector location nu ers , 3, and 5.

    I% the declared in-ut is an n m single9 or dou le9-recision atri , it will e assigned ulti-le locationsstarting with the location s-eci%ied. he nu er o% locations assigned %or each atri will e the sa e as%or ann9ele ent arra$ o% m9co -onent vectors. 4or e a -le,

    la out(location 3 A) in mat transformsI) struct 4 vec> a; mat< b; vec cI

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    54/180

    4 #aria$%e and &'pe

    he nu er o% in-ut locations availa le to a shader is li ited. 4or verte shaders, the li it is theadvertised nu er o% verte attri utes. 4or all other shaders, the li it is i -le entation9de-endent and

    ust e no less than one %ourth o% the advertised a i u in-ut co -onent count. A -rogra will %ail to

    link i% an$ attached shader uses a location greater than or e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    55/180

    4 #aria$%e and &'pe

    fractional_even_s acing signi%$ing that edges should e divided into an even nu er o% e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    56/180

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    57/180

    4 #aria$%e and &'pe

    // code sequence within one shader555in vec Lolor0IJ; // legal7 si.e still un=nown555Lolor05length()555// illegal7 length() un=nown

    in vec Lolor

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    58/180

    4 #aria$%e and &'pe

    4rag ent shaders also allow the %ollowing la$out ) out vec color;

    will esta lish that the %rag ent shader out-ut color is assigned to %rag ent color ( as the %irst inde =eroCin-ut to the lend e7 index 3 0) out vec factor;

    will esta lish that the %rag ent shader out-ut actor is assigned to %rag ent color ( as the second indeoneC in-ut to the lend e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    59/180

    4 #aria$%e and &'pe

    I% the declared out-ut is an arra$, it will e assigned consecutive locations starting with the locations-eci%ied. 4or e a -le,

    la out(location 3 J;

    will esta lish that colors is assigned to vector location nu ers !, (, and &.

    I% the declared out-ut is an n m single9 or dou le9-recision atri , it will e assigned ulti-le locationsstarting with the location s-eci%ied. he nu er o% locations assigned will e the sa e as %or an n9ele ent arra$ o% m9co -onent vectors.

    I% the declared out-ut is a structure, its e ers will e assigned consecutive locations in the order o%declaration, with the %irst e er assigned the location s-eci%ied %or the structure. he nu er o%locations consu ed $ a structure e er is deter ined $ a--l$ing the rules a ove recursivel$ asthough the structure e er were declared as an out-ut varia le o% the sa e t$-e.

    0ocation la$out

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    60/180

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    61/180

    4 #aria$%e and &'pe

    In this e a -le,

    la out(triangle!stri 7 max!vertices 3 B?) out; // order does not matterla out(max!vertices 3 B?) out; // redeclaration o=ala out(triangle!stri ) out; // redeclaration o=ala out( oints) out; // error7 contradicts triangle!strila out(max!vertices 3 >?) out; // error7 contradicts B?

    all out-uts %ro the geo etr$ shader are triangles and at ost ' vertices will e e itted $ the shader. Itis an error %or the a i u nu er o% vertices to e greater than gl_La (eometry)ut utVertices .

    he identi%ier stream is used to s-eci%$ that a geo etr$ shader out-ut varia le or lock is associated witha -articular verte strea nu ered eginning with =eroC. A de%ault strea nu er a$ e declared atglo al sco-e $

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    62/180

    4 #aria$%e and &'pe

    Built9in geo etr$ shader out-uts are alwa$s associated with verte strea =ero.

    All geo etr$ shader out-ut la$out declarations in a -rogra ust declare the sa e la$out and sa e value%orma _vertices . I% geo etr$ shaders are in a -rogra , there ust e at least one geo etr$ out-utla$out declaration so ewhere in that -rogra , ut not all geo etr$ shaders co -ilation unitsC arere

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    63/180

    4 #aria$%e and &'pe

    Redeclarations o% gl_&ragDe th are -er%or ed as %ollows:

    // redeclaration that changes nothing is allowedout float gl!&rag,e th;

    // assume it ma be modified in an wala out (de th!an ) out float gl!&rag,e th;

    // assume it ma be modified such that its value will onl increasela out (de th!greater) out float gl!&rag,e th;

    // assume it ma be modified such that its value will onl decreasela out (de th!less) out float gl!&rag,e th;

    // assume it will not be modifiedla out (de th!unchanged) out float gl!&rag,e th;

    I%gl_&ragDe th is redeclared in an$ %rag ent shader in a -rogra , it ust e redeclared in all %rag entshaders in that -rogra that have static assign ents to gl_&ragDe th . All redeclarations o%gl_&ragDe th in all %rag ent shaders in a single -rogra ust have the sa e set o%

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    64/180

    4 #aria$%e and &'pe

    De%ault la$outs are esta lished e ce-t %or binding C at glo al sco-e %or uni%or locks as

    la out( la*out- uali ier-id-list ) uniform;

    @hen this is done, the -revious de%ault

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    65/180

    4 #aria$%e and &'pe

    inding -oints used %or uni%or locks declared with or without a binding identi%ier can e u-dated $ the)-en/0 A2I.

    I% thebinding identi%ier is used with a uni%or lock instanced as an arra$ then the %irst ele ent o% thearra$ takes the s-eci%ied lock inding and each su se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    66/180

    4 #aria$%e and &'pe

    he identi%ier binding s-eci%ies which unit will e ound. An$ uni%or sa -ler or i age varia ledeclared without a binding

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    67/180

    4 #aria$%e and &'pe

    Binding -oints are not inherited, onl$ o%%sets. Each inding -oint tracks its own current de%ault o set%orinheritance o% su se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    68/180

    4 #aria$%e and &'pe

    r11f_g11f_b1 f r'"f r1$f

    rgba1$rgb1 _a"rgba\rg1$rg\r1$r\rgba1$_snormrgba\_snormrg1$_snormrg\_snormr1$_snormr\_snorm

    int-image- ormat- uali ier rgba'"irgba1$irgba\irg'"irg1$irg\ir'"ir1$ir\i

    uint-image- ormat- uali ier

    rgba'"uirgba1$uirgb1 _a"uirgba\uirg'"uirg1$uirg\uir'"uir1$uir\ui

    A %or at la$out

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    69/180

    4 #aria$%e and &'pe

    An$ i age varia le used %or i age loads or ato ic o-erations ust s-eci%$ a %or at la$out

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    70/180

    4 #aria$%e and &'pe

    se-arate value ust e assigned to that varia le %or each covered sa -le in the -i el, and that value ust e sa -led at the location o% the individual sa -le.

    he t$-e and -resence o% inter-olation

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    71/180

    4 #aria$%e and &'pe

    .( +arameter :ualifiersIn addition to -recision

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    72/180

    4 #aria$%e and &'pe

    4or single -recision o-erations, -recisions are re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    73/180

    4 #aria$%e and &'pe

    4or e a -le:

    low float color;out medium vec< H;low ivec< foo(low mat>);high mat m;

    0iteral constants do not have -recision

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    74/180

    4 #aria$%e and &'pe

    . . #ailable +recision :ualifiers

    he uilt9in acro /0?4RA/;E8 ?2RE"I#I)8?*I/* is de%ined to 1:

    #define -"!& D-G%$+!H %L *$!O -O 0

    his acro is availa le in the verte , tessellation, geo etr$, and %rag ent languages.

    .3 Variance and the In#ariant :ualifier In this section, variance re%ers to the -ossi ilit$ o% getting di%%erent values %ro the sa e e -ression indi%%erent -rogra s. 4or e a -le, sa$ two verte shaders, in di%%erent -rogra s, each set gl_!osition withthe sa e e -ression in oth shaders, and the in-ut values into that e -ression are the sa e when othshaders run. It is -ossi le, due to inde-endent co -ilation o% the two shaders, that the values assigned to

    gl_!osition are not e actl$ the sa e when the two shaders run. In this e a -le, this can cause -ro le swith align ent o% geo etr$ in a ulti9-ass algorith .

    In general, such variance etween shaders is allowed. @hen such variance does not e ist %or a -articularout-ut varia le, that varia le is said to e invariant.

    .3.1 The In#ariant :ualifier

    o ensure that a -articular out-ut varia le is invariant, it is necessar$ to use the invariant Lolor;

    )nl$ varia les out-ut %ro a shader including those that are then in-ut to a su se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    75/180

    4 #aria$%e and &'pe

    he te ture %or ats, te el values, and te ture %iltering are set the sa e wa$ %or an$ te ture %unctioncalls contri uting to the value o% the out-ut varia le.

    All in-ut values are all o-erated on in the sa e wa$. All o-erations in the consu ing e -ressions andan$ inter ediate e -ressions ust e the sa e, with the sa e order o% o-erands and sa eassociativit$, to give the sa e order o% evaluation. Inter ediate varia les and %unctions ust edeclared as the sa e t$-e with the sa e e -licit or i -licit -recision

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    76/180

    4 #aria$%e and &'pe

    o-ti i=ations that e%%ectivel$ odi%$ the order or nu er o% o-erations used to evaluate an e -ression,even i% those o-ti i=ations a$ -roduce slightl$ di%%erent results relative to uno-ti i=ed code.

    he

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    77/180

    4 #aria$%e and &'pe

    #o e e a -les o% the use o% recise :

    in vec a7 b7 c7 d;recise out vec v;

    float func(float e7 float f7 float g7 float h)4 return (ePf) K (gPh); // no constraint on order or

    // o erator consistenc8

    float func(float i7 float :7 recise out float =)4 = 3 i P i K :; // recise7 due to 6=Q declaration8

    void main()4 vec r 3 vec>(a P b); // recise7 used to com ute v5x . vec s 3 vec>(c P d); // recise7 used to com ute v5x . v5x . 3 r K s; // recise

    v5w 3 (a5w P b5w) K (c5w P d5w); // recise v5x 3 func(a5x7 b5x7 c5x7 d5x); // values com uted in func()

    // are $*+ recise v5x 3 func(a5x P b5x7 c5x P d5x7 v5x); // reciseR8

    4or the -ur-oses o% deter ining i% an out-ut %ro one shader stage atches an in-ut o% the ne t stage, therecise

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    78/180

    4 #aria$%e and &'pe

    Varia les declared as i age t$-es can

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    79/180

    4 #aria$%e and &'pe

    ecause the co -iler can assu e that the underl$ing i age won t e read or written $ other code.A--lications are res-onsi le %or ensuring that i age e or$ re%erenced $ varia les

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    80/180

    ' Operators and %*pressions

    '.1 Operatorshe )-en/0 #hading 0anguage has the %ollowing o-erators.

    +recedence Operator Class Operators ssociati#ity

    1 highestC -arenthetical grou-ing 8 9 8A

    !

    arra$ su scri-t%unction call and constructor structure%ield or ethod selector, swi==le

    -ost %i incre ent and decre ent

    6 78 9.

    00 **

    0e%t to Right

    ( -re%i incre ent and decre entunar$

    00 **0 * ?

    Right to 0e%t

    & ulti-licative 2 3 0e%t to Right

    + additive 0 * 0e%t to Right

    it9wise shi%t 44 55 0e%t to Right

    3 relational 4 5 4@ 5@ 0e%t to Right

    5 e 0e%t to Right

    1' it9wise e clusive or < 0e%t to Right

    11 it9wise inclusive or = 0e%t to Right

    1! logical and >> 0e%t to Right

    1( logical e clusive or @

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    81/180

    5 Operator and ()pre ion

    '.2 rray Operationshese are now descri ed in section +.3 G#tructure and Arra$ )-erations .

    '." ,unction CallsI% a %unction returns a value, then a call to that %unction a$ e used as an e -ression, whose t$-e will ethe t$-e that was used to declare or de%ine the %unction.

    4unction de%initions and calling conventions are discussed in section .1 G4unction De%initions .

    '. Constructors"onstructors use the %unction call s$nta , where the %unction na e is a t$-e, and the call akes an o Lecto% that t$-e. "onstructors are used the sa e wa$ in oth initiali=ers and e -ressions. #ee section 6G#hading 0anguage /ra ar %or details.C he -ara eters are used to initiali=e the constructed value."onstructors can e used to re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    82/180

    5 Operator and ()pre ion

    @hen constructors are used to convert an$ %loating9-oint t$-e to an integer t$-e, the %ractional -art o% the%loating9-oint value is dro--ed. It is unde%ined to convert a negative %loating -oint value to an uint .

    @hen a constructor is used to convert an$ integer or %loating9-oint t$-e to a bool , ' and '.' are convertedto false , and non9=ero values are converted to true . @hen a constructor is used to convert a bool to an$integer or %loating9-oint t$-e, false is converted to ' or '.', and true is converted to 1 or 1.'.

    he constructor int uint C -reserves the it -attern in the argu ent, which will change the argu ent svalue i% its sign it is set. he constructor uint int C -reserves the it -attern in the argu ent, which willchange its value i% it is negative.

    Identit$ constructors, like float float C are also legal, ut o% little use.

    #calar constructors with non9scalar -ara eters can e used to take the %irst ele ent %ro a non9scalar.4or e a -le, the constructor float vec' C will select the %irst co -onent o% the vec' -ara eter.

    '. .2 Vector and 7atri* Constructors

    "onstructors can e used to create vectors or atrices %ro a set o% scalars, vectors, or atrices. hisincludes the a ilit$ to shorten vectors.

    I% there is a single scalar -ara eter to a vector constructor, it is used to initiali=e all co -onents o% theconstructed vector to that scalarMs value. I% there is a single scalar -ara eter to a atri constructor, it isused to initiali=e all the co -onents on the atri Ms diagonal, with the re aining co -onents initiali=edto '.'.

    I% a vector is constructed %ro ulti-le scalars, one or ore vectors, or one or ore atrices, or a i tureo% these, the vector s co -onents will e constructed in order %ro the co -onents o% the argu ents. heargu ents will e consu ed le%t to right, and each argu ent will have all its co -onents consu ed, inorder, e%ore an$ co -onents %ro the ne t argu ent are consu ed. #i ilarl$ %or constructing a atri%ro ulti-le scalars or vectors, or a i ture o% these. ;atri co -onents will e constructed andconsu ed in colu n aLor order. In these cases, there ust e enough co -onents -rovided in theargu ents to -rovide an initiali=er %or ever$ co -onent in the constructed value. It is an error to -rovidee tra argu ents e$ond this last used argu ent.

    I% a atri is constructed %ro a atri , then each co -onent colu n i) row 8C in the result that has acorres-onding co -onent colu n i) row 8C in the argu ent will e initiali=ed %ro there. All otherco -onents will e initiali=ed to the identit$ atri . I% a atri argu ent is given to a atri constructor,it is an error to have an$ other argu ents.

    I% the asic t$-e bool- int- float , or double C o% a -ara eter to a constructor does not atch the asic t$-eo% the o Lect eing constructed, the scalar construction rules a oveC are used to convert the -ara eters.

    76

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    83/180

    5 Operator and ()pre ion

    #o e use%ul vector constructors are as %ollows:

    vec>(float) // initiali.es each com onent of the vec> with the floatvec (ivec ) // ma=es a vec with com onent2wise conversionvec (mat with > intsbvec (int7 int7 float7 float) // uses 9oolean conversions

    vec) // dro s the third com onent of a vec>vec>(vec ) // dro s the fourth com onent of a vec

    vec>(vec5x 3 vec5 3 vec5. 3 floatvec>(float7 vec5x 3 float7 vec>5 3 vec5. 3 vec7 float)vec (float7 vec>)

    vec (vec(color); // dro the th com onent

    o initiali=e the diagonal o% a atri with all other ele ents set to =ero:

    mat(float)mat (float)

    hat is, result5i6586 is set to the %loat argu ent %or all i > 8 and set to ' %or all i 8.

    77

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    84/180

    5 Operator and ()pre ion

    o initiali=e a atri $ s-eci%$ing vectors or scalars, the co -onents are assigned to the atri ele entsin colu n9 aLor order.

    mat7 vec>); // one column er argumentmat (vec 7 vec 7 vec 7 vec ); // one column er argumentmat>x of the mat xmat(mat x); // uts the mat>x> in the u er2left7 sets the lower right // com onent to 07 and the rest to ?

    '. ." Structure Constructors

    )nce a structure is de%ined, and its t$-e is given a na e, a constructor is availa le with the sa e na e toconstruct instances o% that structure. 4or e a -le:

    struct light 4 float intensit ; vec> osition;8;

    light light ar 3 light(>5?7 vec>(05?7 5?));

    78

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    85/180

    5 Operator and ()pre ion

    he argu ents to the constructor will e used to set the structure s e ers, in order, using one argu ent -er e er. Each argu ent ust e the sa e t$-e as the e er it sets, or e a t$-e that can econverted to the e er s t$-e according to section &.1.1' GI -licit "onversions .

    #tructure constructors can e used as initiali=ers or in e -ressions.

    '. . rray Constructors

    Arra$ t$-es can also e used as constructor na es, which can then e used in e -ressions or initiali=ers.4or e a -le,

    const float cI>J 3 floatI>J(E5?7 C5J 3 floatIJ(E5?7 C5J;

    b 3 floatI>J(g7 g K 05?7 g K

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    86/180

    5 Operator and ()pre ion

    vec< os;float height;

    os5x // is legal

    os5. // is illegalheight5x // is legalheight5 // is illegal

    he co -onent selection s$nta allows ulti-le co -onents to e selected $ a--ending their na es%ro the sa e na e setC a%ter the -eriod . C.

    vec v ;v 5rgba; // is a vec and the same as :ust using v 7v 5rgb; // is a vec>7v 5b; // is a float7v 5x ; // is a vec5?7

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    87/180

    5 Operator and ()pre ion

    su scri-t. he %irst co -onent is at inde =ero. Reading %ro or writing to a vector using a constantintegral e -ression with a value that is negative or greater than or e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    88/180

    5 Operator and ()pre ion

    he e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    89/180

    5 Operator and ()pre ion

    right shi%t $ 55@C

    and into >@C

    inclusive9or into =@C e clusive9or into

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    90/180

    5 Operator and ()pre ion

    he two o-erands are vectors o% the sa e si=e. In this case, the o-eration is done co -onent9wiseresulting in the sa e si=e vector.

    he o-erator is add 0 C, su tract *C, or divide C, and the o-erands are atrices with the sa enu er o% rows and the sa e nu er o% colu ns. In this case, the o-eration is done co -onent9wise resulting in the sa e si=e atri .

    he o-erator is ulti-l$ 2C, where oth o-erands are atrices or one o-erand is a vector and theother a atri . A right vector o-erand is treated as a colu n vector and a le%t vector o-erand as arow vector. In all these cases, it is re

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    91/180

    5 Operator and ()pre ion

    he e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    92/180

    5 Operator and ()pre ion

    '.1! Vector and 7atri* Operations@ith a %ew e ce-tions, o-erations are co -onent9wise. 7suall$, when an o-erator o-erates on a vector or

    atri , it is o-erating inde-endentl$ on each co -onent o% the vector or atri , in a co -onent9wise%ashion. 4or e a -le,

    vec> v7 u;float f;

    v 3 u K f;

    will e e v7 u7 w;w 3 v K u;

    will e e v7 u;mat> m;

    u 3 v P m;

    is e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    93/180

    5 Operator and ()pre ion

    And

    mat> m7 n7 r;

    r 3 m P n;

    is e

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    94/180

    ( Statements and Structure

    he %unda ental uilding locks o% the )-en/0 #hading 0anguage are:

    state ents and declarations

    %unction de%initions

    selection if*else and switch*case*default C

    iteration for- while- and do*while C

    Lu -s discard- return- break- and continue C

    he overall structure o% a shader is as %ollows

    translation-unit: global-declarationtranslation-unit global-declaration

    global-declaration: unction-de initiondeclaration

    hat is, a shader is a se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    95/180

    6 State*ent and Structure

    iteration-statement 8um"-statement

    #i -le declaration, e -ression, and Lu - state ents end in a se i9colon.

    his a ove is slightl$ si -li%ied, and the co -lete gra ar s-eci%ied in section 6 G#hading 0anguage/ra ar should e used as the de%initive s-eci%ication.

    Declarations and e -ressions have alread$ een discussed.

    (.1 ,unction )efinitionsAs indicated $ the gra ar a ove, a valid shader is a se

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    96/180

    6 State*ent and Structure

    4unctions that return no value ust e declared as void . A void %unction can onl$ use return without areturn argu ent, even i% the return argu ent has void t$-e. Return state ents onl$ acce-t values:

    void %unc1 C ^ _void %unc! C ^ return %unc1 CN _ HH illegal return state ent

    )nl$ a -recision

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    97/180

    6 State*ent and Structure

    !. A atch involving an i -licit conversion %ro float to double is etter than a atch involvingan$ other i -licit conversion.

    (. A atch involving an i -licit conversion %ro either int or uint to float is etter than a atchinvolving an i -licit conversion %ro either int or uint to double .

    I% none o% the rules a ove a--l$ to a -articular -air o% conversions, neither conversion is considered etterthan the other.

    4or the e a -le %unction -rotot$-es AC, BC, and "C a ove, the %ollowing e a -les show how the rulesa--l$ to di%%erent sets o% calling argu ent t$-es:

    f(vec 7 vec ); // exact match of vec f(in vec x7 out vec )f(vec 7 uvec ); // exact match of vec f(in vec x7 out ivec )f(vec 7 ivec ); // matched to vec f(in vec x7 out vec ) // (L) not relevant7 can't convert vec to

    // ivec 5 (D) better than (9) for

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    98/180

    6 State*ent and Structure

    he ke$word inout is used as a

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    99/180

    6 State*ent and Structure

    he const

  • 8/12/2019 GLSL Specification 4.20.11.Clean

    100/180

    6 State*ent and Structure

    7nlike other uni%or varia les, su routine uni%or varia les are sco-ed to the shader e ecution stage thevaria le is declared in.

    #u routine varia les a$ e declared as e -licitl$9si=ed arra$s, which can e d$na icall$ inde ed at use.

    (.2 Selection"onditional control %low in the shading language is done $ either if , if 9else , or switch state ents:

    selection-statement :if bool-e#"ression C statement if bool-e#"ression C statement else statement switch init-e#"ression C s/itc%-statement-list o"t _

    @here s/itc%-statement-list is a list o% =ero or ore s/itc%-statement and other state ents de%ined $ thelanguage, where s/itc%