glsl specification 4.00.9.clean

Upload: mushakk

Post on 03-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    1/157

    The OpenGLShading Language

    Language Version: 4.00

    Document Revision: 9

    24-Jul-2010

    Editor: John Kessenich, Intel

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

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    2/157

    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 4.00.9.Clean

    3/157

    Table of Contents

    1 Introduction.................................................................................................................................1

    1.1 Acknowledgents................................................................................................................!1.! "hanges ............................................................................................................................... !

    1.!.1 "hanges #ro Revision $............................................................................................. !1.!.! %uar& o# "hanges #ro Version '.'(..................................................................... '

    1.' )verview.............................................................................................................................. *1.* Error +andling......................................................................................................................*1. -&ograhical "onventions................................................................................................. *1./ Derecation.......................................................................................................................... *

    ! )verview o# )en0 %hading....................................................................................................!.1 Verte2 3rocessor.................................................................................................................. !.! -essellation "ontrol 3rocessor.............................................................................................

    !.' -essellation Evaluation 3rocessor......................................................................................../!.* 0eoetr& 3rocessor............................................................................................................. /!. 4ragent 3rocessor.............................................................................................................. /

    ' Basics.......................................................................................................................................... 5'.1 "haracter %et........................................................................................................................ 5'.! %ource %trings...................................................................................................................... 5'.' 3rerocessor......................................................................................................................... $'.* "oents.......................................................................................................................... 1''. -okens................................................................................................................................ 1''./ Ke&words............................................................................................................................1''.5 Identi#iers........................................................................................................................... 1

    '.$ De#initions..........................................................................................................................1/'.$.1 %tatic 6se....................................................................................................................1/'.$.! 6ni#or and 7on86ni#or "ontrol 4low.................................................................. 1/'.$.' D&naicall& 6ni#or E2ressions.............................................................................15

    * Varia9les and -&es..................................................................................................................1$*.1 Basic -&es........................................................................................................................ 1$

    *.1.1 Void............................................................................................................................ !1*.1.! Booleans..................................................................................................................... !1*.1.' Integers....................................................................................................................... !!*.1.* 4loats.......................................................................................................................... !**.1. Vectors........................................................................................................................!

    *.1./ atrices...................................................................................................................... !*.1.5 %alers..................................................................................................................... !*.1.$ %tructures....................................................................................................................!/*.1.; Arra&s......................................................................................................................... !5*.1.1( Ilicit "onversions................................................................................................ !;

    *.! %coing...............................................................................................................................'(

    3

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    4/157

    *.' %torage

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    5/157

    /.1.1 4unction "alling "onventions....................................................................................5//.1.! %u9routines.................................................................................................................5$

    /.! %election.............................................................................................................................5;

    /.' Iteration.............................................................................................................................. $(/.* Jus..................................................................................................................................$1

    5 Built8in Varia9les......................................................................................................................$!5.1 Built8In anguage Varia9les.............................................................................................. $!

    5.1.1 "oati9ilit& 3ro#ile Built8In anguage Varia9les....................................................$$5.! "oati9ilit& 3ro#ile Verte2 %hader Built8In Inuts......................................................... ;(5.' Built8In "onstants.............................................................................................................. ;(

    5.'.1 "oati9ilit& 3ro#ile Built8In "onstants....................................................................;15.* Built8In 6ni#or %tate....................................................................................................... ;1

    5.*.1 "oati9ilit& 3ro#ile %tate......................................................................................... ;!$ Built8in 4unctions..................................................................................................................... ;/

    $.1 Angle and -rigonoetr& 4unctions....................................................................................;5$.! E2onential 4unctions........................................................................................................;;$.' "oon 4unctions.......................................................................................................... 1(($.* 4loating83oint 3ack and 6nack 4unctions..................................................................... 1(/$. 0eoetric 4unctions........................................................................................................ 1($$./ atri2 4unctions.............................................................................................................. 11($.5 Vector Relational 4unctions.............................................................................................11!$.$ Integer 4unctions..............................................................................................................11*$.; -e2ture 4unctions.............................................................................................................11/

    $.;.1 -e2ture

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    6/157

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    7/157

    1 Introduction

    -his docuent seci#ies onl& version *.(( o# the )en0 %hading anguage. It re=uires >>VER%I)7>>

    to su9stitute *((, and re=uires #versionto accet onl& *((. I# #version is declared with a saller

    nu9er, the language acceted is a revious version o# the shading language, which will 9e suorted

    deending on the version and t&e o# conte2t in the )en0 A3I. %ee the )en0 0rahics %&ste

    %eci#ication, Version *.(, #or details on what language versions are suorted.

    All )en0 0rahics %&ste %eci#ication re#erences in this seci#ication are to version *.(.

    1

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    8/157

    1 Introduction

    1.1 Acknowledgments

    -his seci#ication is 9ased on the work o# those who contri9uted to ast versions o# the )en0

    anguage %eci#ication, the )en0 E% !.( anguage %eci#ication, and the #ollowing contri9utors tothis version:

    3at Brown, 7vidia

    Je## Bolt?, 7vidia

    3ierre Boudier, AD

    Eric Bouaour, AD

    urat Balci, AD

    Ignacio "astano, 7vidia

    Ale2 "hal#in, AD

    3iers Daniell, 7vidia

    "hris Dodd, 7vidia

    Evan +art, 7vidia

    Ben@ ichak, AleEric erness, 7vidia

    7ick +aeel, AD

    Brent Insko, Intel

    Jon eech

    Bill icea8Kane, AD

    Barthold ichten9elt, 7vidia

    Bruce err&, AR

    Daniel Koch, -ransgaing

    aurice Ri99le,

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    9/157

    1 Introduction

    1.2.2 Summar! of Changes from "ersion #.#$

    7ote: 7o #eatures were reoved or derecated 9etween versions '.'( and *.((.

    -he #ollowing #eatures are added:

    -essellation, which includes two new rograa9le stages, tessellation control stage and tessellation

    evaluation stage. Includes barrierFG9uilt8in #or s&nchroni?ation.

    3ol&orhic #unctions: Run8tie selection o# what #unction gets called, through the new ke&word

    subroutine.

    /*9it #loating oint nu9ers with the new t&e ke&word double. Built8in #unctions e2tended #or

    dou9les, and new #unction atching rules are added to 9oth allow ilicit conversions when calling a

    #unction and reserve ost e2isting #unction atching once dou9les are included.

    ore ilicit conversions

    float to double, and siilarl& #or all #loating8oint vector and atri2 t&es

    int to uint, and siilarl& #or all integer vector t&esint to double, and siilarl& #or all vectors o# integers and dou9les.

    unint to double, and siilarl& #or all vectors o# integers and dou9les.

    "u9e a arra& te2tures and te2ture #unctions tetureFG, tetureSi!eFG, teture"odFG, and

    tetureradFG.

    %aler arra&s can take a varia9le inde2 now, as long as itHs value is uni#or #or all uses.

    3er8sale shading. Including sale inut ask gl$Sample%ask&n'( and er8sale

    interolation, with e2licit interolation 9uilt8ins interpolateAtCentroidFG, interpolateAtSampleFG,

    and interpolateAtOffsetFG.

    7ew precise =uali#ier to disallow otii?ations that re8order oerations or treat di##erent instances o#

    the sae oerator with di##erent recision.

    Add a #used ultil& and add 9uilt8in, fmaFG, in relation to the new precise =uali#ier. FBecause a 9

    c will re=uire two oerations under new rules #or precise.G

    Added new 9uilt8in #loating8oint #unctions

    frepFGand ldepFG

    pack)norm*1+FG,pack)norm-FG,packSnorm-FG, andpackouble*/*FG

    unpack)norm*1+FG,unpack)norm-FG,unpackSnorm-FG, andunpackouble*/*FG

    Add new 9uilt8in integer #unctions

    uaddCarr0FG andusuborrowFG

    umul2tendedFG andimul2tendedFG

    bitfield2tractFG andbitfield&nsertFG

    bitfield3everseFG

    bitCountFG,find"SFG, andfind%SFG

    7ew 9uilt8in to =uer& )D, teture4uer0"odFG.

    7ew overloaded #unction atching algorith, handling selection #ro an& valid ultile choices.

    3

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    10/157

    1 Introduction

    -e2ture gather #unctions that return #our te2els with a single call.

    tetureatherFG

    tetureatherOffsetFG

    tetureatherOffsetsFG

    0eoetr& shading

    Add streas out #ro geoetr& shader. )utut can 9e directed to streas through

    2mitStream5erteFG and 2ndStream6rimitiveFG.

    1.# Overvie%

    -his docuent descri9es The Oen!L "ha#ing Language$ version 4.00.

    Indeendent coilation units written in this language are calledsha#ers. Arogramis a colete set o#

    shaders that are coiled and linked together. -he ai o# this docuent is to thoroughl& seci#& the

    rograing language. -he )en0 0rahics %&ste %eci#ication will seci#& the )en0 entr&

    oints used to aniulate and counicate with rogras and shaders.

    1.& 'rror (andling

    "oilers, in general, accet rogras that are ill8#ored, due to the iossi9ilit& o# detecting all ill8

    #ored rogras. 3orta9ilit& is onl& ensured #or well8#ored rogras, which this seci#ication

    descri9es. "oilers are encouraged to detect ill8#ored rogras and issue diagnostic essages, 9ut are

    not re=uired to do so #or all cases. "oilers are re=uired to return essages regarding le2icall&,

    graaticall&, or seanticall& incorrect shaders.

    1.) T!pographical Conventions

    Italic, 9old, and #ont choices have 9een used in this seci#ication riaril& to irove reada9ilit&. "ode

    #ragents use a #i2ed width #ont. Identi#iers e9edded in te2t are italici?ed. Ke&words e9edded in te2t

    are 9old. )erators are called 9& their nae, #ollowed 9& their s&9ol in 9old in arentheses. -he

    clari#&ing graar #ragents in the te2t use 9old #or literals and italics #or non8terinals. -he o##icial

    graar in section ; %hading anguage 0raar uses all caitals #or terinals and lower case #or

    non8terinals.

    1.* +eprecation

    3revious versions o# the )en0 %hading anguage derecated soe #eatures. -hese are clearl& called

    out in this seci#ication as derecated. -he& are still resent in this version o# the language, 9ut are

    targeted #or otential reoval in a #uture version o# the shading language. -he )en0 A3I has a

    #orward coati9ilit& ode that will disallow use o# derecated #eatures. I# coiling in a ode where

    use o# derecated #eatures is disallowed, their use causes coile tie errors. %ee the )en0 0rahics

    %&ste %eci#ication #or details on what causes derecated language #eatures to 9e acceted or to return

    an error.

    4

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    11/157

    2 Overvie% of OpenGL Shading

    -he )en0 %hading anguage is actuall& several closel& related languages. -hese languages are used

    to create shaders #or each o# the rograa9le rocessors contained in the )en0 rocessing ieline.

    "urrentl&, these rocessors are the verte2, tessellation control, tessellation evaluation, geoetr&, and

    #ragent rocessors.

    6nless otherwise noted in this aer, a language #eature alies to all languages, and coon usage will

    re#er to these languages as a single language. -he seci#ic languages will 9e re#erred to 9& the nae o#

    the rocessor the& target: verte2, tessellation control, tessellation evaluation, geoetr&, or #ragent.

    ost )en0 state is not tracked or ade availa9le to shaders. -&icall&, user8de#ined varia9les will 9e

    used #or counicating 9etween di##erent stages o# the )en0 ieline. +owever, a sall aount o#state is still tracked and autoaticall& ade availa9le to shaders, and there are a #ew 9uilt8in varia9les #or

    inter#aces 9etween di##erent stages o# the )en0 ieline.

    2.1 "erte, -rocessor

    -he verte% rocessoris a rograa9le unit that oerates on incoing vertices and their associated data.

    "oilation units written in the )en0 %hading anguage to run on this rocessor are called verte%

    sha#ers. hen a colete set o# verte2 shaders are coiled and linked, the& result in a verte% sha#er

    e%ecuta&lethat runs on the verte2 rocessor.

    -he verte2 rocessor oerates on one verte2 at a tie. It does not relace grahics oerations that re=uire

    knowledge o# several vertices at a tie.

    2.2 Tessellation Control -rocessor

    -he tessellation control rocessor is a rograa9le unit that oerates on a atch o# incoing vertices

    and their associated data, eitting a new outut atch. "oilation units written in the )en0 %hading

    anguage to run on this rocessor are called tessellation control shaders. hen a colete set o#

    tessellation control shaders are coiled and linked, the& result in a tessellation control shader e2ecuta9le

    that runs on the tessellation control rocessor.

    -he tessellation control shader is invoked #or each verte2 o# the outut atch. Each invocation can read

    the attri9utes o# an& verte2 in the inut or outut atches, 9ut can onl& write er8verte2 attri9utes #or the

    corresonding outut atch verte2. -he shader invocations collectivel& roduce a set o# er8atch

    attri9utes #or the outut atch. A#ter all tessellation control shader invocations have coleted, the outut

    vertices and er8atch attri9utes are asse9led to #or a atch to 9e used 9& su9se=uent ieline stages.

    5

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    12/157

    2 Overview of OpenGL Shading

    -essellation control shader invocation run ostl& indeendentl&, with unde#ined relative e2ecution order.

    +owever, the 9uilt8in #unction 9arrierFG can 9e used to control e2ecution order 9& s&nchroni?ing

    invocations, e##ectivel& dividing tessellation control shader e2ecution into a set o# hases. -essellation

    control shaders will get unde#ined results i# one invocation reads a er8verte2 or er8atch attri9utewritten 9& another invocation at an& oint during the sae hase, or i# two invocations attet to write

    di##erent values to the sae er8atch outut in a single hase.

    2.# Tessellation 'valuation -rocessor

    -he tessellation evaluation rocessor is a rograa9le unit that evaluates the osition and other

    attri9utes o# a verte2 generated 9& the tessellation riitive generator, using a atch o# incoing vertices

    and their associated data. "oilation units written in the )en0 %hading anguage to run on this

    rocessor are called tessellation evaluation shaders. hen a colete set o# tessellation evaluation

    shaders are coiled and linked, the& result in a tessellation evaluation shader e2ecuta9le that runs on the

    tessellation evaluation rocessor.

    Each invocation o# the tessellation evaluation e2ecuta9le coutes the osition and attri9utes o# a singleverte2 generated 9& the tessellation riitive generator. -he e2ecuta9le can read the attri9utes o# an&

    verte2 in the inut atch, lus the tessellation coordinate, which is the relative location o# the verte2 in the

    riitive 9eing tessellated. -he e2ecuta9le writes the osition and other attri9utes o# the verte2.

    2.& Geometr! -rocessor

    -hegeometr' rocessor is a rograa9le unit that oerates on data #or incoing vertices #or a riitive

    asse9led a#ter verte2 rocessing and oututs a se=uence o# vertices #oring outut riitives.

    "oilation units written in the )en0 %hading anguage to run on this rocessor are calledgeometr'

    sha#ers. hen a colete set o# geoetr& shaders are coiled and linked, the& result in ageometr'

    sha#er e%ecuta&le that runs on the geoetr& rocessor.

    A single invocation o# the geoetr& shader e2ecuta9le on the geoetr& rocessor will oerate on a

    declared inut riitive with a #i2ed nu9er o# vertices. -his single invocation can eit a varia9le

    nu9er o# vertices that are asse9led into riitives o# a declared outut riitive t&e and assed to

    su9se=uent ieline stages.

    2.) ragment -rocessor

    -he(ragment rocessoris a rograa9le unit that oerates on #ragent values and their associated

    data. "oilation units written in the )en0 %hading anguage to run on this rocessor are called

    (ragment sha#ers. hen a colete set o# #ragent shaders are coiled and linked, the& result in a

    (ragment sha#er e%ecuta&lethat runs on the #ragent rocessor.

    A #ragent shader cannot change a #ragentHs F%,'G osition. Access to neigh9oring #ragents is not

    allowed. -he values couted 9& the #ragent shader are ultiatel& used to udate #rae9u##er eor&

    or te2ture eor&, deending on the current )en0 state and the )en0 coand that caused the

    #ragents to 9e generated.

    6

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    13/157

    # /asics

    #.1 Character Set

    -he source character set used #or the )en0 shading languages is a su9set o# A%"II. It includes the

    #ollowing characters:

    -he letters a7!, A78, and the underscore F >G.

    -he nu9ers97:.

    -he s&9ols eriod F.G, lus F;G, dash F7G, slash FG, angled 9rackets F?and

    @G, s=uare 9rackets F 'and ( G, arentheses F and BG, 9races F and DG, caret FEG, vertical 9ar F FG,

    aersand FGG, tilde FHG, e=uals FIG, e2claation oint FJG, colon FKG, seicolon FLG, coa F,G, and=uestion ark FMG.

    -he nu9er sign F#G #or rerocessor use.

    hite sace: the sace character, hori?ontal ta9, vertical ta9, #or #eed, carriage8return, and line8

    #eed.

    ines are relevant #or coiler diagnostic essages and the rerocessor. -he& are terinated 9&

    carriage8return or line8#eed. I# 9oth are used together, it will count as onl& a single line terination. 4or

    the reainder o# this docuent, an& o# these co9inations is sil& re#erred to as a new8line. -here is no

    line continuation character.

    In general, the languages 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 end8o#8#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 ultile lines, searated 9& new8

    lines. 7o new8lines need 9e resent in a stringL a single line can 9e #ored #ro ultile strings. 7o

    new8lines or other characters are inserted 9& the ileentation when it concatenates the strings to #or a

    single shader. ultile shaders can 9e linked together to #or a single rogra.

    Diagnostic essages returned #ro coiling a shader ust identi#& 9oth the line nu9er within a string

    and which source string the essage alies to. %ource strings are counted se=uentiall& with the #irst

    string 9eing string (. ine nu9ers are one ore than the nu9er o# new8lines that have 9een rocessed.

    7

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    14/157

    3 Basics

    #.# -reprocessor

    -here is a rerocessor that rocesses the source strings as art o# the coilation rocess.

    -he colete list o# rerocessor directives is as #ollows.

    #

    #define

    #undef

    #if

    #ifdef

    #ifndef

    #else

    #elif

    #endif

    #error

    #pragma

    #extension

    #version

    #line

    -he #ollowing oerators are also availa9le

    defined

    ##

    Each nu9er sign F#G can 9e receded in its line onl& 9& saces or hori?ontal ta9s. It a& also 9e

    #ollowed 9& saces and hori?ontal ta9s, receding the directive. Each directive is terinated 9& a new8

    line. 3rerocessing does not change the nu9er or relative location o# new8lines in a source string.

    -he nu9er sign F#G on a line 9& itsel# is ignored. An& directive not listed a9ove will cause a diagnostic

    essage and ake the ileentation treat the shader as ill8#ored.

    #defineand #undef#unctionalit& are de#ined as is standard #or " rerocessors #or acro de#initions

    9oth with and without acro araeters.

    -he #ollowing rede#ined acros are availa9le

    __LINE__

    __FILE__

    __VERSION__

    ))L*+,))will su9stitute a decial integer constant that is one ore than the nu9er o# receding new8lines in the current source string.

    ))*L,))will su9stitute a decial integer constant that sa&s which source string nu9er is currentl&

    9eing rocessed.

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    15/157

    3 Basics

    ))V,R"*O+))will su9stitute a decial integer re#lecting the version nu9er o# the )en0 shading

    language. -he version o# the shading language descri9ed in this docuent will have))V,R"*O+))

    su9stitute the decial integer *((.

    All acro naes containing two consecutive underscores F$$ G are reserved #or #uture use as rede#ined

    acro naes. All acro naes re#i2ed with 0> F0 #ollowed 9& a single underscoreG are also

    reserved.

    #if, #ifdef, #ifndef, #else, #elif, and#endifare de#ined to oerate as is standard #or " rerocessors.

    E2ressions #ollowing #ifand #elifare #urther restricted to e2ressions oerating on literal integer

    constants, lus identi#iers consued 9& the definedoerator. It is an error to use #if or #elif on

    e2ressions containing unde#ined acro naes, other than as arguents to the defined oerator.

    "haracter constants are not suorted. -he oerators availa9le are as #ollows.

    -recedence Operator class Operators 0ssociativit!

    1 FhighestG arenthetical grouing F G 7A

    ! unar& de#ined 8 M N

    Right to e#t

    ' ultilicative O P e#t to Right

    * additive 8 e#t to Right

    9it8wise shi#t QQ e#t to Right

    / relational Q QS S e#t to Right

    5 e=ualit& SS NS e#t to Right

    $ 9it8wise and T e#t to Right

    ; 9it8wise e2clusive or U e#t to Right

    1( 9it8wise inclusive or e#t to Right

    11 logical and TT e#t to Right

    1! FlowestG logical inclusive or e#t to Right

    -he definedoerator can 9e used in either o# the #ollowing wa&s:

    defined identifier

    defined( identifier )

    -wo tokens in a acro can 9e concatenated into one token using the token asting F##Goerator, as is

    standard #or " rerocessors. -he result ust 9e a valid single token, which will then 9e su9@ect to

    acro e2ansion. -hat is, acro e2ansion haens onl& a#ter token asting. -here are no other nu9ersign 9ased oerators Fe.g., no # or#NG, nor is there a si!eofoerator.

    -he seantics o# al&ing oerators to integer literals in the rerocessor atch those standard in the

    " rerocessor, not those in the )en0 %hading anguage.

    !

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    16/157

    3 Basics

    3rerocessor e2ressions will 9e evaluated according to the 9ehavior o# the host rocessor, not the

    rocessor targeted 9& the shader.

    #error will cause the ileentation to ut a diagnostic essage into the shader o9@ects in#oration log

    Fsection /.1.1! %hader and 3rogra

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    17/157

    3 Basics

    I# the otionalro(ile arguent is rovided, it ust 9e the nae o# an )en0 ro#ile. "urrentl&, there

    are two choices:

    %ore

    %ompati$ilit&

    I# noro(ile arguent is rovided, the de#ault is core. 6nless otherwise seci#ied, this seci#ication is

    docuenting the core ro#ile, and ever&thing seci#ied #or the core ro#ile is also availa9le in the

    coati9ilit& ro#ile. 4eatures seci#ied as 9elonging seci#icall& to the coati9ilit& ro#ile are not

    availa9le in the core ro#ile.

    -here is a 9uilt8in acro de#inition #or each ro#ile the ileentation suorts. All ileentations

    rovide the #ollowing acro:

    #define !L_%ore_profile '

    Ileentations roviding the coati9ilit& ro#ile rovide the #ollowing acro:

    #define !L_%ompati$ilit&_profile '

    -he #versiondirective ust occur in a shader 9e#ore an&thing else, e2cet #or coents and white sace.

    11

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    18/157

    3 Basics

    B& de#ault, coilers o# this language ust issue coile tie s&ntactic, graatical, and seantic

    errors #or shaders that do not con#or to this seci#ication. An& e2tended 9ehavior ust #irst 9e ena9led.

    Directives to control the 9ehavior o# the coiler with resect to e2tensions are declared with the

    #etensiondirective

    #extension extension_name:behavior

    #extension all :behavior

    where e%tension)nameis the nae o# an e2tension. E2tension naes are not docuented in this

    seci#ication. -he token alleans the 9ehavior alies to all e2tensions suorted 9& the coiler. -he

    &ehavior can 9e one o# the #ollowing

    behavior 'ffect

    reuire Behave as seci#ied 9& the e2tension e%tension)name.

    0ive an error on the #etension i# the e2tension e%tension)name is notsuorted, or i# allis seci#ied.

    enable Behave as seci#ied 9& the e2tension e%tension)name.

    arn on the #etensioni# the e2tension e%tension)nameis not suorted.

    0ive an error on the #etension i# allis seci#ied.

    %arn Behave as seci#ied 9& the e2tension e%tension)name, e2cet issue warningson an& detecta9le use o# that e2tension, unless such use is suorted 9& otherena9led or re=uired e2tensions.

    I# all is seci#ied, then warn on all detecta9le uses o# an& e2tension used.

    arn on the #etensioni# the e2tension e%tension)nameis not suorted.

    disable Behave Fincluding issuing errors and warningsG as i# the e2tensione%tension)nameis not art o# the language de#inition.

    I# all is seci#ied, then 9ehavior ust revert 9ack to that o# the non8e2tendedcore version o# the language 9eing coiled to.

    arn on the #etension i# the e2tension e%tension)nameis not suorted.

    -he etensiondirective is a sile, low8level echanis to set the 9ehavior #or each e2tension. It does

    not de#ine olicies such as which co9inations are aroriate, those ust 9e de#ined elsewhere. )rder

    o# directives atters in setting the 9ehavior #or each e2tension: Directives that occur later override those

    seen earlier. -he allvariant sets the 9ehavior #or all e2tensions, overriding all reviousl& issued

    etension directives, 9ut onl& #or the &ehaviorswarnand disable.

    12

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    19/157

    3 Basics

    -he initial state o# the coiler is as i# the directive

    #extension all ( disa$le

    was issued, telling the coiler that all error and warning reorting ust 9e done according to thisseci#ication, ignoring an& e2tensions.

    Each e2tension can de#ine its allowed granularit& o# scoe. I# nothing is said, the granularit& is a shader

    Fthat is, a single coilation unitG, and the e2tension directives ust occur 9e#ore an& non8rerocessor

    tokens. I# necessar&, the linker can en#orce granularities larger than a single coilation unit, in which

    case each involved shader will have to contain the necessar& e2tension directive.

    acro e2ansion is not done on lines containing #etensionand #versiondirectives.

    #lineust have, a#ter acro su9stitution, one o# the #ollowing #ors:

    #line line

    #line line source-string-number

    where line andsource-string-num&er are constant integer e2ressions. A#ter rocessing this directiveFincluding its new8lineG, the ileentation will 9ehave as i# it is coiling at line nu9er lineand

    source string nu9ersource-string-num&er. %u9se=uent source strings will 9e nu9ered se=uentiall&,

    until another #linedirective overrides that nu9ering.

    #.& Comments

    "oents are deliited 9& O and O, or 9& OO and a new8line. -he 9egin coent deliiters FO or OOG are

    not recogni?ed as coent deliiters inside o# a coent, hence coents cannot 9e nested. I# a

    coent resides entirel& within a single line, it is treated s&ntacticall& as a single sace. 7ew8lines are

    not eliinated 9& coents.

    #.) Toens-he language is a se=uence o# tokens. A token can 9e

    toen:

    e'/or#

    i#enti(ier

    integer-constant

    (loating-constant

    oerator

    L D

    #.* 3e!%ords

    -he #ollowing are the ke&words in the language, and cannot 9e used #or an& other urose than that

    de#ined 9& this docuent:

    attribute const uniform var0ing

    la0out

    13

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    20/157

    3 Basics

    centroid flat smooth noperspective

    patch sample

    break continue do for while switch case defaultif else

    subroutine

    in out inout

    float double int void bool true false

    invariant

    discard return

    mat* mat/ mat dmat* dmat/ dmat

    mat** mat*/ mat* dmat** dmat*/ dmat*

    mat/* mat// mat/ dmat/* dmat// dmat/ mat* mat/ mat dmat* dmat/ dmat

    vec* vec/ vec ivec* ivec/ ivec bvec* bvec/ bvec dvec* dvec/ dvec

    uint uvec* uvec/ uvec

    lowp mediump highp precision

    sampler1 sampler* sampler/ samplerCube

    sampler1Shadow sampler*Shadow samplerCubeShadow

    sampler1Arra0 sampler*Arra0

    sampler1Arra0Shadow sampler*Arra0Shadow

    isampler1 isampler* isampler/ isamplerCube

    isampler1Arra0 isampler*Arra0

    usampler1 usampler* usampler/ usamplerCube

    usampler1Arra0 usampler*Arra0

    sampler*3ect sampler*3ectShadow isampler*3ect usampler*3ect

    sampleruffer isampleruffer usampleruffer

    sampler*%S isampler*%S usampler*%S

    sampler*%SArra0 isampler*%SArra0 usampler*%SArra0

    samplerCubeArra0 samplerCubeArra0Shadow isamplerCubeArra0 usamplerCubeArra0

    struct

    14

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    21/157

    3 Basics

    -he #ollowing are the ke&words reserved #or #uture use. 6sing the will result in an error:

    common partition active

    asm class union enum t0pedef template this packed

    goto

    inline noinline volatile public static etern eternal interface

    long short half fied unsigned superp

    input output

    hvec* hvec/ hvec fvec* fvec/ fvec

    sampler/3ect

    filter

    image1 image* image/ imageCube

    iimage1 iimage* iimage/ iimageCube

    uimage1 uimage* uimage/ uimageCube

    image1Arra0 image*Arra0

    iimage1Arra0 iimage*Arra0 uimage1Arra0 uimage*Arra0

    image1Shadow image*Shadow

    image1Arra0Shadow image*Arra0Shadow

    imageuffer iimageuffer uimageuffer

    si!eof cast

    namespace using

    row$maPor

    In addition, all identi#iers containing two consecutive underscores F$$G are reserved as ossi9le #uture

    ke&words.

    #.4 Identifiers

    Identi#iers are used #or varia9le naes, #unction naes, structure naes, and #ield selectors F#ield

    selectors select coonents o# vectors and atrices siilar to structure #ields, as discussed in section .

    Vector "oonents and section ./ atri2 "oonents G. Identi#iers have the #or

    i#enti(ier

    non#igiti#enti(ier non#igit

    i#enti(ier #igit

    15

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    22/157

    3 Basics

    non#igit: one o#

    $ a b c d e f g h i P k l m n o p Q r s t u v w 0 !

    A C 2 R & T U " % V O 6 4 3 S ) 5 W X Y 8

    #igit: one o#

    9 1 * / Z + [ - :

    Identi#iers starting with gl> are reserved #or use 9& )en0, and a& not 9e declared in a shader as

    either a varia9le or a #unction. +owever, as noted in the seci#ication, there are soe cases where

    reviousl& declared varia9les can 9e redeclared to change or add soe roert&, and redeclared Wgl>W

    naes are allowed to 9e redeclared in a shader onl& #or these seci#ic uroses. ore generall&, it is an

    error to redeclare a varia9le, including those starting gl>.

    #. +efinitions

    %oe language rules descri9ed 9elow deend on the #ollowing de#initions.

    #..1 Static 5se

    A shader contains astatic useo# Forstatic assignmenttoG a varia9le%i#, a#ter rerocessing, the shader

    contains a stateent that would read For writeG%, whether or not run8tie #low o# control will cause that

    stateent to 9e e2ecuted.

    #..2 5niform and 6on75niform Control lo%

    hen e2ecuting stateents in a #ragent shader, control #low starts as uni(orm control (lo/L all #ragents

    enter the sae control ath into mainFG. "ontrol #low 9ecoes non-uni(orm when di##erent #ragents

    take di##erent aths through control8#low stateents Fselection, iteration, and @usG. "ontrol #low

    su9se=uentl& returns to 9eing uni#or a#ter such divergent su98stateents or skied code coletes,until the ne2t tie di##erent control aths are taken.

    4or e2ale:

    main)

    float a * +++,-- t.is is uniform flo/ %ontrol

    if a 0 $) -- t.is expression is true for some fragments1 not all

    ++++, -- non2uniform flo/ %ontrol

    3 else

    ++++, -- non2uniform flo/ %ontrol

    3

    ++++, -- uniform flo/ %ontrol again

    3

    )ther e2ales o# non8uni#or #low control can occur within switch stateents and a#ter conditional

    9reaks, continues, earl& returns, and a#ter #ragent discards, when the condition is true #or soe

    #ragents 9ut not others. oo iterations that onl& soe #ragents e2ecute are also non8uni#or #low

    control.

    16

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    23/157

    3 Basics

    -his is siilarl& de#ined #or other shader stages, 9ased on the er8instance data ites the& rocess.

    #..# +!namicall! 5niform ',pressions

    A #ragent8shader e2ression is #'namicall' uni(orm i# all #ragents evaluating it get the sae resulting

    value. hen loos are involved, this re#ers to the e2ressionHs value #or the sae loo iteration. hen

    #unctions are involved, this re#ers to calls #ro the sae call oint.

    -his is siilarl& de#ined #or other shader stages, 9ased on the er8instance data the& rocess.

    7ote that constant e2ressions are triviall& d&naicall& uni#or. It #ollows that t&ical loo counters

    9ased on these are also d&naicall& uni#or.

    17

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    24/157

    & "ariables and T!pes

    All varia9les and #unctions ust 9e declared 9e#ore 9eing used. Varia9le and #unction naes are

    identi#iers.

    -here are no de#ault t&es. All varia9le and #unction declarations ust have a declared t&e, and

    otionall& =uali#iers. A varia9le is declared 9& seci#&ing its t&e #ollowed 9& one or ore naes

    searated 9& coas. In an& cases, a varia9le can 9e initiali?ed as art o# its declaration 9& using the

    assignent oerator FIG. -he graar near the end o# this docuent rovides a #ull re#erence #or the

    s&nta2 o# declaring varia9les.

    6ser8de#ined t&es a& 9e de#ined using structto aggregate a list o# e2isting t&es into a single nae.

    -he )en0 %hading anguage is t&e sa#e. -here are no ilicit conversions 9etween t&es, with thee2cetion that an integer value a& aear where a #loating8oint t&e is e2ected, and 9e converted to a

    #loating8oint value. E2actl& how and when this can occur is descri9ed in section *.1.1(Ilicit

    "onversions and as re#erenced 9& other sections in this seci#ication.

    &.1 /asic T!pes

    -he )en0 %hading anguage suorts the #ollowing 9asic data t&es, groued as #ollows.

    -ransarent t&es

    T!pe 8eaning

    void #or #unctions that do not return a value

    bool a conditional t&e, taking on values o# true or #alse

    int a signed integer

    uint an unsigned integer

    float a single #loating8oint scalar

    double a single dou9le8recision #loating oint scalar

    vec* a two8coonent #loating8oint vector

    vec/ a three8coonent #loating8oint vector

    vec a #our8coonent #loating8oint vector

    dvec* a two8coonent dou9le recision #loating8oint vector

    dvec/ a three8coonent dou9le recision #loating8oint vector

    dvec a #our8coonent dou9le recision #loating8oint vector

    bvec* a two8coonent Boolean vector

    bvec/ a three8coonent Boolean vector

    1

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    25/157

    4 #aria$%es and &'pes

    T!pe 8eaning

    bvec a #our8coonent Boolean vector

    ivec* a two8coonent signed integer vectorivec/ a three8coonent signed integer vector

    ivec a #our8coonent signed integer vector

    uvec* a two8coonent unsigned integer vector

    uvec/ a three8coonent unsigned integer vector

    uvec a #our8coonent unsigned integer vector

    mat* a !X! #loating8oint atri2

    mat/ a 'X' #loating8oint atri2

    mat a *X* #loating8oint atri2

    mat** sae as a mat*mat*/ a #loating8oint atri2 with ! coluns and ' rows

    mat* a #loating8oint atri2 with ! coluns and * rows

    mat/* a #loating8oint atri2 with ' coluns and ! rows

    mat// sae as a mat/

    mat/ a #loating8oint atri2 with ' coluns and * rows

    mat* a #loating8oint atri2 with * coluns and ! rows

    mat/ a #loating8oint atri2 with * coluns and ' rows

    mat sae as a mat

    dmat* a !X! dou9le8recision #loating8oint atri2dmat/ a 'X' dou9le8recision #loating8oint atri2

    dmat a *X* dou9le8recision #loating8oint atri2

    dmat** sae as a dmat*

    dmat*/ a dou9le8recision #loating8oint atri2 with ! coluns and ' rows

    dmat* a dou9le8recision #loating8oint atri2 with ! coluns and * rows

    dmat/* a dou9le8recision #loating8oint atri2 with ' coluns and ! rows

    dmat// sae as a dmat/

    dmat/ a dou9le8recision #loating8oint atri2 with ' coluns and * rows

    dmat* a dou9le8recision #loating8oint atri2 with * coluns and ! rowsdmat/ a dou9le8recision #loating8oint atri2 with * coluns and ' rows

    dmat sae as a dmat

    1!

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    26/157

    4 #aria$%es and &'pes

    4loating 3oint %aler -&es Foa=ueG

    T!pe 8eaning

    sampler1 a handle #or accessing a 1D te2ture

    sampler* a handle #or accessing a !D te2ture

    sampler/ a handle #or accessing a 'D te2ture

    samplerCube a handle #or accessing a cu9e aed te2ture

    sampler*3ect a handle #or accessing a rectangular te2ture

    sampler1Shadow a handle #or accessing a 1D deth te2ture with coarison

    sampler*Shadow a handle #or accessing a !D deth te2ture with coarison

    sampler*3ectShadow a handle #or accessing a rectangular te2ture with coarison

    sampler1Arra0 a handle #or accessing a 1D arra& te2ture

    sampler*Arra0 a handle #or accessing a !D arra& te2turesampler1Arra0Shadow a handle #or accessing a 1D arra& deth te2ture with coarison

    sampler*Arra0Shadow a handle #or accessing a !D arra& deth te2ture with coarison

    sampleruffer a handle #or accessing a 9u##er te2ture

    sampler*%S a handle #or accessing a !D ulti8sale te2ture

    sampler*%SArra0 a handle #or accessing a !D ulti8sale arra& te2ture

    samplerCubeShadow a handle #or accessing a cu9e a deth te2ture with coarison

    samplerCubeArra0 a handle #or accessing a cu9e a arra& te2ture

    samplerCubeArra0Shadow a handle #or accessing a cu9e a arra& deth te2ture withcoarison

    %igned Integer %aler -&es Foa=ueG

    T!pe 8eaning

    isampler1 a handle #or accessing an integer 1D te2ture

    isampler* a handle #or accessing an integer !D te2ture

    isampler/ a handle #or accessing an integer 'D te2ture

    isamplerCube a handle #or accessing an integer cu9e aed te2ture

    isampler*3ect a handle #or accessing an integer !D rectangular te2ture

    isampler1Arra0 a handle #or accessing an integer 1D arra& te2ture

    isampler*Arra0 a handle #or accessing an integer !D arra& te2ture

    isampleruffer a handle #or accessing an integer 9u##er te2ture

    isampler*%S a handle #or accessing an integer !D ulti8sale te2ture

    isampler*%SArra0 a handle #or accessing an integer !D ulti8sale arra& te2ture

    2"

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    27/157

    4 #aria$%es and &'pes

    T!pe 8eaning

    isamplerCubeArra0 a handle #or accessing an integer cu9e a arra& te2ture

    6nsigned Integer %aler -&es Foa=ueG

    T!pe 8eaning

    usampler1 a handle #or accessing an unsigned integer 1D te2ture

    usampler* a handle #or accessing an unsigned integer !D te2ture

    usampler/ a handle #or accessing an unsigned integer 'D te2ture

    usamplerCube a handle #or accessing an unsigned integer cu9e aed te2ture

    usampler*3ect a handle #or accessing an unsigned integer rectangular te2ture

    usampler1Arra0 a handle #or accessing an unsigned integer 1D arra& te2ture

    usampler*Arra0 a handle #or accessing an unsigned integer !D arra& te2ture

    usampleruffer a handle #or accessing an unsigned integer 9u##er te2ture

    usampler*%S a handle #or accessing an unsigned integer !D ulti8sale te2ture

    usampler*%SArra0 a handle #or accessing an unsigned integer !D ulti8sale te2turearra&

    usamplerCubeArra0 a handle #or accessing an unsigned integer cu9e a arra& te2ture

    In addition, a shader can aggregate these using arra&s and structures to 9uild ore cole2 t&es.

    -here are no ointer t&es.

    &.1.1 "oid4unctions that do not return a value ust 9e declared as void. -here is no de#ault #unction return t&e.

    -he ke&word voidcannot 9e used in an& other declarations Fe2cet #or et& #oral or actual araeter

    listsG.

    &.1.2 /ooleans

    -o ake conditional e2ecution o# code easier to e2ress, the t&e boolis suorted. -here is no

    e2ectation that hardware directl& suorts varia9les 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 trueand falsecan 9e used as literal

    Boolean constants. Booleans are declared and otionall& initiali?ed as in the #ollow e2ale:

    $ool su%%ess, -- de%lare 4su%%ess5 to $e a 6oolean

    $ool done * false, -- de%lare and initiali"e 4done5

    -he right side o# the assignent oerator F I G ust 9e an e2ression whose t&e is bool.

    E2ressions used #or conditional @us Fif, for, MK, while, do7whileG ust evaluate to the t&e bool.

    21

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    28/157

    4 #aria$%es and &'pes

    &.1.# Integers

    %igned and unsigned integer varia9les are #ull& suorted. In this docuent, the ter integer is eant to

    generall& include 9oth signed and unsigned integers. 6nsigned integers have e2actl& '! 9its o# recision.

    %igned integers use '! 9its, including a sign 9it, in twoHs coleent #or. )erations resulting in

    over#low or under#low will not cause an& e2cetion, nor will the& saturate, rather the& will wra to &ield

    the low8order '! 9its o# the result.

    Integers are declared and otionall& initiali?ed with integer e2ressions, as in the #ollowing e2ale:

    int i1 7 * 89, -- default integer literal t&pe is int

    uint : * ;u, -- 4u5 esta$lis.es t.e t&pe as uint

    22

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    29/157

    4 #aria$%es and &'pes

    iteral integer constants can 9e e2ressed in decial F9ase 1(G, octal F9ase $G, or he2adecial F9ase 1/G

    as #ollows.

    integer-constant :

    #ecimal-constant integer-su((i%ot

    octal-constant integer-su((i%ot

    he%a#ecimal-constant integer-su((i%ot

    integer-su((i%: one o#

    u )

    #ecimal-constant :

    nonero-#igit

    #ecimal-constant #igit

    octal-constant :

    9

    octal-constant octal-#igit

    he%a#ecimal-constant :

    (2he%a#ecimal-#igit

    (Yhe%a#ecimal-#igit

    he%a#ecimal-constant he%a#ecimal-#igit

    #igit :

    9

    nonero-#igit

    nonero-#igit : one o#

    1 * / Z + [ - :

    octal-#igitK one o#

    9 1 * / Z + [

    he%a#ecimal-#igitKone o#

    9 1 * / Z + [ - :

    a b c d e f

    A C 2 R

    7o white sace is allowed 9etween the digits o# an integer constant, including a#ter the leading 9or a#ter

    the leading 9or 9Xo# a constant, or 9e#ore the su##i2uor). hen the su##i2 u or ) is resent, the

    literal has t&e uint,otherwise the t&e is int. A leading unar& inus sign F8G is interreted as an

    arithetic unar& negation, not as art o# the constant.

    It is an error to rovide a literal integer whose agnitude is too large to store in a varia9le o# atchingsigned or unsigned t&e.

    23

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    30/157

    4 #aria$%es and &'pes

    &.1.& loats

    %ingle8recision and dou9le8recision #loating oint varia9les are availa9le #or use in a variet& o# scalar

    calculations. 4loating8oint varia9les are de#ined as in the #ollowing e2ale:

    float a1 $ * '+

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    31/157

    4 #aria$%es and &'pes

    &.1.) "ectors

    -he )en0 %hading anguage includes data t&es #or generic !8, '8, and *8coonent vectors o#

    #loating8oint values, integers, or Booleans. 4loating8oint vector varia9les can 9e used to store colors,

    norals, ositions, te2ture coordinates, te2ture looku results and the like. Boolean vectors can 9e used

    #or coonent8wise coarisons o# nueric vectors. %oe e2ales o# vector declaration are:

    ve%9 tex%oord'1 tex%oord9,

    ve%; position,

    ve%8 m&R!6>,

    ive%9 textureLoo:up,

    $ve%; less,

    Initiali?ation o# vectors can 9e done with constructors, which are discussed shortl&.

    &.1.* 8atrices

    -he )en0 %hading anguage has 9uilt8in t&es #or !X!, !X', !X*, 'X!, 'X', 'X*, *X!, *X', and *X*atrices o# #loating8oint nu9ers. atri2 t&es 9eginning with WatW have single8recision coonents

    while atri2 t&es 9eginning with WdatW have dou9le8recision coonents. -he #irst nu9er in the

    t&e is the nu9er o# coluns, the second is the nu9er o# rows. I# there is onl& one nu9er, the atri2

    is s=uare. E2ale atri2 declarations:

    mat9 mat9,

    mat; opt?atrix,

    mat8 vie/1 pro7e%tion,

    mat8x8 vie/, -- an alternate /a& of de%laring a mat8

    mat;x9 m, -- a matrix /it. ; %olumns and 9 ro/s

    dmat8 .ig.@re%ision?V@,

    dmat9x8 dm,

    Initiali?ation o# atri2 values is done with constructors Fdescri9ed in section .* "onstructors G incolun8a@or order.

    &.1.4 Samplers

    %aler t&es Fe.g., sampler*G are e##ectivel& oa=ue handles to te2tures and their #ilters. -he& are

    used with the 9uilt8in te2ture #unctions Fdescri9ed in section $.5 -e2ture ooku 4unctions G to seci#&

    which te2ture to access and how it is to 9e #iltered. -he& can onl& 9e declared as #unction araeters or

    uniformvaria9les Fsee section *.'. 6ni#or G. E2cet #or arra& inde2ing, structure #ield selection, and

    arentheses, salers are not allowed to 9e oerands in e2ressions. %alers aggregated into arra&s

    within a shader Fusing s=uare 9rackets ' (G can onl& 9e inde2ed with a d&naicall& uni#or integral

    e2ression, otherwise results are unde#ined. %alers cannot 9e treated as l8valuesL hence cannot 9e used

    as out or inout #unction araeters, nor can the& 9e assigned into. As uni#ors, the& are initiali?ed onl&

    with the )en0 A3IL the& cannot 9e declared with an initiali?er in a shader. As #unction araeters,onl& salers a& 9e assed to salers o# atching t&e. -his ena9les consistenc& checking 9etween

    shader te2ture accesses and )en0 te2ture state 9e#ore a shader is run.

    25

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    32/157

    4 #aria$%es and &'pes

    &.1. Structures

    6ser8de#ined t&es can 9e created 9& aggregating other alread& de#ined t&es into a structure using the

    structke&word. 4or e2ale,

    stru%t lig.t

    float intensit&,

    ve%; position,

    3 lig.tVar,

    In this e2ale, light9ecoes the nae o# the new t&e, and lightVar9ecoes a varia9le o# t&e light.

    -o declare varia9les o# the new t&e, use its nae Fwithout the ke&word structG.

    lig.t lig.tVar9,

    ore #orall&, structures are declared as #ollows. +owever, the colete correct graar is as given in

    section ; %hading anguage 0raar .

    struct-#e(inition :uali(ier

    ot structname

    otmem&er-list D#eclarators

    ot

    mem&er-list :

    mem&er-#eclaration

    mem&er-#eclaration mem&er-list

    mem&er-#eclaration :

    &asic-t'e #eclarators

    where name9ecoes the user8de#ined t&e, and can 9e used to declare varia9les to 9e o# this new t&e.

    -he nameshares the sae nae sace as other varia9les, t&es, and #unctions. All reviousl& visi9le

    varia9les, t&es, constructors, or #unctions with that nae are hidden. -he otional uali(ieronl& alies

    to an& #eclarators, and is not art o# the t&e 9eing de#ined #or name.

    %tructures ust have at least one e9er declaration. e9er declarators a& contain recision

    =uali#iers, 9ut a& not contain an& other =uali#iers. Bit #ields are not suorted. e9er t&es ust 9e

    alread& de#ined Fthere are no #orward re#erencesG. e9er declarations cannot contain initiali?ers.

    e9er declarators can contain arra&s. %uch arra&s ust have a si?e seci#ied, and the si?e ust 9e an

    integral constant e2ression thatHs greater than ?ero Fsee section *.'.' "onstant E2ressionsG. Each

    level o# structure has its own nae sace #or naes given in e9er declaratorsL such naes need onl&

    9e uni=ue within that nae sace.

    26

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    33/157

    4 #aria$%es and &'pes

    Anon&ous structures are not suorted. E9edded structure de#initions are not suorted.

    stru%t S float f, 3,

    stru%t T

    S, -- Error( anon&mous stru%tures disallo/ed

    stru%t +++ 3, -- Error( em$edded stru%tures disallo/ed

    S s, -- O:a&( nested stru%tures /it. name are allo/ed

    3,

    %tructures can 9e initiali?ed at declaration tie using constructors, as discussed in section .*.' %tructure

    "onstructors .

    &.1.9 0rra!s

    Varia9les o# the sae t&e can 9e aggregated into arra&s 9& declaring a nae #ollowed 9& 9rackets F ' (G

    enclosing an otional si?e. hen an arra& si?e is seci#ied in a declaration, it ust 9e an integral constant

    e2ression Fsee section *.'.' "onstant E2ressions G greater than ?ero. I# an arra& is inde2ed with ane2ression that is not an integral constant e2ression, or i# an arra& is assed as an arguent to a #unction,

    then its si?e ust 9e declared 9e#ore an& such use. It is legal to declare an arra& without a si?e and then

    later re8declare the sae nae as an arra& o# the sae t&e and seci#& a si?e. It is illegal to declare an

    arra& with a si?e, and then later Fin the sae shaderG inde2 the sae arra& with an integral constant

    e2ression greater than or e=ual to the declared si?e. It is also illegal to inde2 an arra& with a negative

    constant e2ression. Arra&s declared as #oral araeters in a #unction declaration ust seci#& a si?e.

    6nde#ined 9ehavior results #ro inde2ing an arra& with a non8constant e2ression thats greater than or

    e=ual to the arra&s si?e or less than (. )nl& one8diensional arra&s a& 9e declared. All 9asic t&es and

    structures can 9e #ored into arra&s. %oe e2ales are:

    float freAuen%iesB;C,

    uniform ve%8 lig.t@ositionB8C,

    lig.t lig.tsBC,%onst int numLig.ts * 9,

    lig.t lig.tsBnumLig.tsC,

    An arra& t&e can 9e #ored 9& seci#&ing a t&e #ollowed 9& s=uare 9rackets FZ [G and including a si?e:

    floatB

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    34/157

    4 #aria$%es and &'pes

    and as an alternate wa& o# declaring a varia9le or #unction araeter.

    floatB

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    35/157

    4 #aria$%es and &'pes

    &.1.1$ Implicit Conversions

    In soe situations, an e2ression and its t&e will 9e ilicitl& converted to a di##erent t&e. -he

    #ollowing ta9le shows all allowed ilicit conversions:

    T!pe of e,pression Can be implicitl! converted to

    int uint

    int

    uint

    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/

    2!

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    36/157

    4 #aria$%es and &'pes

    -here are no ilicit arra& or structure conversions. 4or e2ale, an arra& o# intcannot 9e ilicitl&

    converted to an arra& o# float.

    hen an ilicit conversion is done, it is not a re8interretation o# the e2ressionHs 9it attern, 9ut a

    conversion o# its value to an e=uivalent value in the new t&e. 4or e2ale, the integer value 8Zwill 9e

    converted to the #loating8oint value 8Z.9. Integer values having ore 9its o# recision than a #loating

    oint antissa will lose recision when converted to float.

    hen er#oring ilicit conversion #or 9inar& oerators, there a& 9e ultile data t&es to which the

    two oerands can 9e converted. 4or e2ale, when adding an int value to a uint value, 9oth values can

    9e ilicitl& converted to uint, float, and double. In such cases, a #loating8oint t&e is chosen i# either

    oerand has a #loating8oint t&e. )therwise, an unsigned integer t&e is chosen i# either oerand has an

    unsigned integer t&e. )therwise, a signed integer t&e is chosen. I# oerands can 9e ilicitl& converted

    to ultile data t&es deriving #ro the sae 9ase data t&e, the t&e with the sallest coonent si?e is

    used.

    -he conversions in the ta9le a9ove are done onl& as indicated 9& other sections o# this seci#ication.

    &.2 Scoping

    -he scoe o# a varia9le is deterined 9& where it is declared. I# it is declared outside all #unction

    de#initions, it has glo9al scoe, which starts #ro where it is declared and ersists to the end o# the shader

    it is declared in. I# it is declared in a whiletest or a for stateent, then it is scoed to the end o# the

    #ollowing su98stateent. )therwise, i# it is declared as a stateent within a coound stateent, it is

    scoed to the end o# that coound stateent. I# it is declared as a araeter in a #unction de#inition, it is

    scoed until the end o# that #unction de#inition. A #unction 9od& has a scoe nested inside the #unctions

    de#inition. -he ifstateents e2ression does not allow new varia9les to 9e declared, hence does not

    #or a new scoe.

    3"

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    37/157

    4 #aria$%es and &'pes

    ithin a declaration, the scoe o# a nae starts iediatel& a#ter the initiali?er i# resent or iediatel&

    a#ter the nae 9eing declared i# not. %everal e2ales:

    int x * ',

    int x * 91 & * x, -- & is initiali"ed to 9

    3

    stru%t S

    int x,

    3,

    S S * S=), -- DSD is onl& visi$le as a stru%t and %onstru%tor

    S, -- DSD is no/ visi$le as a varia$le

    3

    int x * x, -- Error if x .as not $een previousl& defined+

    All varia9le naes, structure t&e naes, and #unction naes in a given scoe share the sae nae sace.

    4unction naes can 9e redeclared in the sae scoe, with the sae or di##erent araeters, without error.

    An ilicitl& si?ed arra& can 9e re8declared in the sae scoe as an arra& o# the sae 9ase t&e.

    )therwise, within one coilation unit, a declared nae cannot 9e redeclared in the sae scoeL doing so

    results in a redeclaration error. I# a nested scoe redeclares a nae used in an outer scoe, it hides all

    e2isting uses o# that nae. -here is no wa& to access the hidden nae or ake it unhidden, without

    e2iting the scoe that hid it.

    -he 9uilt8in #unctions are scoed in a scoe outside the glo9al scoe users declare glo9al varia9les in.

    -hat is, a shaderHs glo9al scoe, availa9le #or user8de#ined #unctions and glo9al varia9les, is nested inside

    the scoe containing the 9uilt8in #unctions. hen a #unction nae is redeclared in a nested scoe, it hidesall #unctions declared with that nae in the outer scoe. 4unction declarations Frotot&esG cannot occur

    inside o# #unctionsL the& ust 9e at glo9al scoe, or #or the 9uilt8in #unctions, outside the glo9al scoe.

    %hared glo9als are glo9al varia9les declared with the sae nae in indeendentl& coiled units

    FshadersG within the sae language Fe.g., verte2G that are linked together when aking a single rogra.

    F0lo9als #oring the inter#ace 9etween two di##erent shader languages are discussed in other sections.G

    %hared glo9als share the sae nae sace, and ust 9e declared with the sae t&e. -he& will share the

    sae storage. %hared glo9al arra&s ust have the sae 9ase t&e and the sae e2licit si?e. An arra&

    ilicitl& si?ed in one shader can 9e e2licitl& si?ed 9& another shader. I# no shader has an e2licit si?e

    #or the arra&, the largest ilicit si?e is used. %calars ust have e2actl& the sae t&e nae and t&e

    de#inition. %tructures ust have the sae nae, se=uence o# t&e naes, and t&e de#initions, and #ield

    naes to 9e considered the sae t&e. -his rule alies recursivel& #or nested or e9edded t&es. All

    initiali?ers #or a shared glo9al ust have the sae value, or a link error will result.

    31

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    38/157

    4 #aria$%es and &'pes

    &.# Storage :ualifiers

    Varia9le declarations a& have one storage =uali#ier seci#ied in #ront o# the t&e. -hese are suari?ed

    as

    :ualifier 8eaning

    Q none: de#ault local readOwrite eor&, or an inut araeter to a #unction

    const a coile8tie constant, or a #unction araeter that is read8onl&

    incentroid in

    sample in

    linkage into a shader #ro a revious stage, varia9le is coied inlinkage with centroid 9ased interolationinut linkage with er8sale interolation

    outcentroid outsample out

    linkage out o# a shader to a su9se=uent stage, varia9le is coied outlinkage with centroid 9ased interolationoutut linkage with er8sale interolation

    attribute derecatedLlinkage 9etween a verte2 shader and )en0 #or er8verte2data

    uniform value does not change across the riitive 9eing rocessed, uni#ors#or the linkage 9etween a shader, )en0, and the alication

    var0ingcentroid var0ing

    derecatedL linkage 9etween a verte2 shader and a #ragent shader #orinterolated data

    patch in tessellation evaluation shader inut #or er8atch attri9utes

    patch out tessellation control shader outut #or er8atch attri9utes

    )ututs #ro shader FoutG and inuts to a shader FinG can 9e #urther =uali#ied with one o# these

    interolation =uali#iers

    :ualifier 8eaning

    smooth ersective correct interolation

    flat no interolation

    noperspective linear interolation

    -hese interolation =uali#iers a& onl& recede the =uali#iers in, centroid in, sample in, out, centroid

    out,or sample out in a declaration. -he& do not al& to the derecated storage =uali#iers var0ing or

    centroid var0ing. -he& also do not al& to inuts into a verte2 shader or oututs #ro a #ragent

    shader.

    ocal varia9les can onl& use the const storage =uali#ier.

    4unction araeters can use const,in, andout =uali#iers, 9ut asarameter uali(iers. 3araeter

    =uali#iers are discussed in section /.1.1 4unction "alling "onventions.

    4unction return t&es and structure #ields do not use storage =uali#iers.

    32

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    39/157

    4 #aria$%es and &'pes

    Data t&es #or counication #ro one run o# a shader e2ecuta9le to its ne2t run Fto counicate

    9etween #ragents or 9etween verticesG do not e2ist. -his would revent arallel e2ecution o# the sae

    shader e2ecuta9le on ultile vertices or #ragents.

    Initiali?ers a& onl& 9e used in declarations o# glo9als with no storage =uali#ier, with a const=uali#ier or

    with a uniform=uali#ier. 0lo9al varia9les without storage =uali#iers that are not initiali?ed in their

    declaration or 9& the alication will not 9e initiali?ed 9& )en0, 9ut rather will enter main3with

    unde#ined values.

    &.#.1 +efault Storage :ualifier

    I# no =uali#ier is resent on a glo9al varia9le, then the varia9le has no linkage to the alication or shaders

    running on other ieline stages. 4or either glo9al or local un=uali#ied varia9les, the declaration will

    aear to allocate eor& associated with the rocessor it targets. -his varia9le will rovide readOwrite

    access to this allocated eor&.

    &.#.2 Constant :ualifier7aed coile8tie constants can 9e declared using the const=uali#ier. An& varia9les =uali#ied as

    constant are read8onl& varia9les #or that shader. Declaring varia9les as constant allows ore descritive

    shaders than using hard8wired nuerical constants. -he const=uali#ier can 9e used with an& o# the 9asic

    data t&es. It is an error to write to a constvaria9le outside o# its declaration, so the& ust 9e initiali?ed

    when declared. 4or e2ale,

    %onst ve%; ">xis * ve%; =+=1 =+=1 '+=),

    %tructure #ields a& not 9e =uali#ied with const. %tructure varia9les can 9e declared as const, and

    initiali?ed with a structure constructor.

    Initiali?ers #or const declarations ust 9e constant e2ressions, as de#ined in section *.'.' "onstant

    E2ressions.

    &.#.# Constant ',pressions

    A constant e%ressionis one o#

    a literal value Fe.g., Zor trueG

    a glo9al or local varia9le =uali#ied as constFi.e., not including #unction araetersG

    an e2ression #ored 9& an oerator on oerands that are all constant e2ressions, including getting an

    eleent or length o# a constant arra&, or a #ield o# a constant structure, or coonents o# a constant

    vector.

    a constructor whose arguents are all constant e2ressions

    a 9uilt8in #unction call whose arguents are all constant e2ressions, with the e2cetion o# the te2turelooku #unctions and the noise #unctions. -he 9uilt8in #unctions dRd, dRd0, and fwidthust return

    ( when evaluated inside an initiali?er with an arguent that is a constant e2ression.

    4unction calls to user8de#ined #unctions Fnon89uilt8in #unctionsG cannot 9e used to #or constant

    e2ressions.

    33

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    40/157

    4 #aria$%es and &'pes

    An integral constant e%ressionis a constant e2ression that evaluates to a scalar signed or unsigned

    integer.

    "onstant e2ressions will 9e evaluated in an invariant wa& so as to create the sae value in ultile

    shaders when the sae constant e2ressions aear in those shaders. %ee section *./.1-he Invariant

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    41/157

    4 #aria$%es and &'pes

    -essellation control, evaluation, and geoetr& shader inut varia9les get the er8verte2 values written out

    9& outut varia9les o# the sae naes in the revious active shader stage. 4or these inuts, centroid in

    and interolation =uali#iers are allowed, 9ut have no e##ect. %ince tessellation control, tessellation

    evaluation, and geoetr& shaders oerate on a set o# vertices, each inut var&ing varia9le For inut 9lock,see inter#ace 9locks 9elowG needs to 9e declared as an arra&. 4or e2ale,

    in float fooBC, -- geometr& s.ader input for vertex 4out float foo5

    Each eleent o# such an arra& corresonds to one verte2 o# the riitive 9eing rocessed. Each arra& can

    otionall& have a si?e declared. -he arra& si?e will 9e set 9&, For i# rovided ust 9e consistent withG the

    inut la0outdeclarationFsG esta9lishing the t&e o# inut riitive, as descri9ed later in section *.'.$.1

    Inut a&out

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    42/157

    4 #aria$%es and &'pes

    4ragent inuts are declared as in the #ollowing e2ales:

    in ve%; normal,

    %entroid in ve%9 Texoord,

    invariant %entroid in ve%8 olor,

    noperspe%tive in float temperature,

    flat in ve%; m&olor,

    noperspe%tive %entroid in ve%9 m&Texoord,

    -he #ragent shader inuts #or an inter#ace with the last active shader in the verte2 rocessing ieline.

    4or this inter#ace, the last active shader stage outut varia9les and #ragent shader inut varia9les o# the

    sae nae ust atch in t&e and =uali#ication Fother than out atching to inG.

    &.#.) 5niform

    -he uniform =uali#ier is used to declare glo9al varia9les whose values are the sae across the entire

    riitive 9eing rocessed. All uniformvaria9les are read8onl& and are initiali?ed e2ternall& either at link

    tie or through the A3I. -he link tie initial value is either the value o# the varia9leHs initiali?er, i#resent, or ( i# no initiali?er is resent. %aler t&es cannot have initiali?ers.

    E2ale declarations are:

    uniform ve%8 lig.t@osition,

    uniform ve%; %olor * ve%;=+1 =+1 =+9), -- value assigned at lin: time

    -he uniform=uali#ier can 9e used with an& o# the 9asic data t&es, or when declaring a varia9le whose

    t&e is a structure, or an arra& o# an& o# these.

    -here is an ileentation deendent liit on the aount o# storage #or uni#ors that can 9e used #or

    each t&e o# shader and i# this is e2ceeded it will cause a coile8tie or link8tie error. 6ni#or

    varia9les that are declared 9ut not used do not count against this liit. -he nu9er o# user8de#ined

    uni#or varia9les and the nu9er o# 9uilt8in uni#or varia9les that are used within a shader are addedtogether to deterine whether availa9le uni#or storage has 9een e2ceeded.

    I# ultile shaders are linked together, then the& will share a single glo9al uni#or nae sace, including

    within a language as well as across languages. +ence, the t&es and initiali?ers o# uni#or varia9les 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 varia9le, while another shader

    does not, 9ut all rovided initiali?ers ust 9e e=ual.

    &.#.* Outputs

    %hader outut varia9les are declared with a storage =uali#ier using the ke&word out. -he& #or the outut

    inter#ace 9etween the declaring shader and the su9se=uent stages o# the )en0 ieline. )utut

    varia9les ust 9e declared at glo9al scoe. During shader e2ecution the& will 9ehave as noralun=uali#ied glo9al varia9les. -heir values are coied out to the su9se=uent ieline stage on shader e2it.

    )nl& outut varia9les that are read 9& the su9se=uent ieline stage need to 9e writtenL it is allowed to

    have suer#luous declarations o# outut varia9les.

    36

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    43/157

    4 #aria$%es and &'pes

    -here is notan inoutstorage =uali#ier at glo9al scoe #or declaring a single varia9le nae as 9oth inut

    and outut to a shader. )utut varia9les ust 9e declared with di##erent naes than inut varia9les.

    +owever, nesting an inut or outut inside an inter#ace 9lock with an instance nae allows the sae

    naes with one re#erenced through a 9lock instance nae.

    Verte2, tessellation evaluation, and geoetr& outut varia9les outut er8verte2 data and are declared

    using the out, centroid out, or sample out storage =uali#iers, or the derecated var0ingstorage =uali#ier.

    It is an error to use patch out in a verte2, tessellation evaluation, or geoetr& shader. )utut varia9les

    can onl& 9e #loating8oint scalars, #loating8oint vectors, atrices, signed or unsigned integers or integer

    vectors, or arra&s or structures o# an& these.

    Individual verte2, tessellation evaluation, and geoetr& oututs are declared as in the #ollowing e2ales:

    out ve%; normal,

    %entroid out ve%9 Texoord,

    invariant %entroid out ve%8 olor,

    noperspe%tive out float temperature, -- var&ing is depre%ated

    flat out ve%; m&olor,noperspe%tive %entroid out ve%9 m&Texoord,

    sample out ve%8 perSampleolor,

    -hese can also aear in inter#ace 9locks, as descri9ed in section *.'.5 Inter#ace Blocks. Inter#ace

    9locks allow siler addition o# arra&s to the inter#ace #ro verte2 to geoetr& shader. -he& also allow a

    #ragent shader to have the sae inut inter#ace as a geoetr& shader #or a given verte2 shader.

    -essellation control shader outut varia9les are a& 9e used to outut er8verte2 and er8atch data. 3er8

    verte2 outut varia9les are arra&ed Fsee arra'e# under *.'.* InutsG and declared using outor centroid

    out storage =uali#iers. 3er8atch outut varia9les are declared using the patch outstorage =uali#ier. 3er8

    verte2 and er8atch outut varia9les can onl& 9e #loating8oint scalars, #loating8oint vectors, atrices,

    signed or unsigned integers or integer vectors, or arra&s or structures o# an& these. %ince tessellation

    control shaders roduce an arra&ed riitive corising ultile vertices, each er8verte2 outut varia9le

    For outut 9lock, see inter#ace 9locks 9elowG needs to 9e declared as an arra&. 4or e2ale,

    out float fooBC, -- feeds next stage input 4in float fooBC5

    Each eleent o# such an arra& corresonds to one verte2 o# the riitive 9eing roduced. Each arra& can

    otionall& have a si?e declared. -he arra& si?e will 9e set 9& For i# rovided ust 9e consistent withG the

    outut la&out declarationFsG esta9lishing the nu9er o# vertices in the outut atch, as descri9ed later in

    section *.'.$.! )utut a&out

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    44/157

    4 #aria$%es and &'pes

    -he order o# e2ecution o# a tessellation control shader invocation relative to the other invocations #or the

    sae inut atch is unde#ined unless the 9uilt8in #unction barrierFGis used. -his rovides soe control

    over relative e2ecution order. hen a shader invocation calls barrierFG, its e2ecution auses until all

    other invocations have reached the sae oint o# e2ecution. )utut varia9le assignents er#ored 9&an& invocation e2ecuted rior to calling barrierFG will 9e visi9le to an& other invocation a#ter the call to

    barrierFG returns.

    Because tessellation control shader invocations e2ecute in unde#ined order 9etween 9arriers, the values o#

    er8verte2 or er8atch outut varia9les will soeties 9e unde#ined. "onsider the 9eginning and end o#

    shader e2ecution and each call to barrierFG as s&nchroni?ation oints. -he value o# an outut varia9le

    will 9e unde#ined in an& o# the three #ollowing cases:

    1. At the 9eginning o# e2ecution.

    !. At each s&nchroni?ation oint, unless

    the value was well8de#ined a#ter the revious s&nchroni?ation oint and was not written 9& an&

    invocation since, or the value was written 9& e2actl& one shader invocation since the revious s&nchroni?ation

    oint, or the value was written 9& ultile shader invocations since the revious s&nchroni?ation oint,

    and the last write er#ored 9& all such invocations wrote the sae value.

    '. hen read 9& a shader invocation, i#

    the value was unde#ined at the revious s&nchroni?ation oint and has not 9een writen 9& the

    sae shader invocation since, or the outut varia9le is written to 9& an& other shader invocation 9etween the revious and ne2t

    s&nchroni?ation oints, even i# that assignent occurs in code #ollowing the read.

    4ragent oututs outut er8#ragent data and are declared using the out storage =uali#ier. It is an error

    to use centroid out,sample out, or patch out in a #ragent shader. 4ragent oututs can onl& 9e float,

    #loating8oint vectors, signed or unsigned integers or integer vectors, or arra&s o# an& these. atrices andstructures cannot 9e outut. 4ragent oututs are declared as in the #ollowing e2ales:

    out ve%8 Fragmentolor,

    out uint Luminosit&,

    &.#.4 Interface /locs

    Inut, outut, and uni#or varia9le declarations can 9e groued into naed inter#ace 9locks to rovide

    coarser granularit& 9acking than is achieva9le with individual declarations. -he& can have an otional

    instance nae, used in the shader to re#erence their e9ers. An outut 9lock o# one rograa9le

    stage is 9acked 9& a corresonding inut 9lock in the su9se=uent rograa9le stage. A uni#or 9lock is

    9acked 9& the alication with a 9u##er o9@ect. It is illegal to have an inut 9lock in a verte2 shader or an

    outut 9lock in a #ragent shaderL these uses are reserved #or #uture use.

    An inter#ace 9lock is started 9& an in, out, or uniform ke&word, #ollowed 9& a 9lock nae, #ollowed 9&

    an oen curl& 9race F G as #ollows:

    inter(ace-&loc :

    la'out-uali(ieropt inter(ace-uali(ier &loc-name mem&er-list Dinstance-nameoptL

    3

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    45/157

    4 #aria$%es and &'pes

    la'out-uali(ier :

    la0out la'out-uali(ier-i#-listB

    inter(ace-uali(ier :

    in

    out

    uniform

    la'out-uali(ier-i#-list

    coa searated list o# la'out-uali(ier-i#

    mem&er-list :

    mem&er-#eclaration

    mem&er-#eclaration mem&er-list

    mem&er-#eclaration :

    la'out-uali(ieropt uali(iersopt t'e #eclarators L

    instance-name :i#enti(ier

    i#enti(ier 5 6

    i#enti(ier 5 integral-constant-e%ression 6

    Each o# the a9ove eleents is discussed 9elow, with the e2cetion o# la&out =uali#iers F la'out-uali(ier,

    which are de#ined in the ne2t section.

    4irst, an e2ale,

    uniform Transform

    mat8 ?odelVie/?atrix,

    mat8 ?odelVie/@ro7e%tion?atrix,

    uniform mat; Normal?atrix, -- allo/ed restatement of Aualifier

    float eformation,3,

    -he a9ove esta9lishes a uni#or 9lock naed -rans#or with #our uni#ors groued inside it.

    -&es and declarators are the sae as #or other inut, outut, and uni#or varia9le declarations outside

    9locks, with these e2cetions:

    initiali?ers are not allowed

    saler t&es are not allowed

    structure de#initions cannot 9e nested inside a 9lock

    )therwise, 9uilt8in 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 9lock.

    3!

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    46/157

    4 #aria$%es and &'pes

    I# no otional =uali#ier is used in a e9er8declaration, the =uali#ication o# the varia9le is @ust in, out, or

    uniformas deterined 9& inter(ace-uali(ier. I# otional =uali#iers are used, the& can include

    interolation and storage =uali#iers and the& ust declare an inut, outut, or uni#or varia9le consistent

    with the inter#ace =uali#ier o# the 9lock: Inut varia9les, outut varia9les, and uni#or varia9les can onl&9e in in9locks, out9locks, and uniform9locks, resectivel&. Reeating the in, out, or uniform

    inter#ace =uali#ier #or a e9erHs storage =uali#ier is otional. Declarations using the derecated

    attribute and var0ing=uali#iers are not allowed. 4or e2ale,

    in ?aterial

    smoot. in ve%8 olor', -- legal1 input inside in $lo%:

    smoot. ve%8 olor9, -- legal1 DinD in.erited from Din ?aterialD

    ve%9 Texoord, -- legal1 Texoord is an input

    uniform float >tten, -- illegal1 mismat%.ed interfa%es

    var&ing ve%9 Texoord9,--illegal1 depre%ated :e&/ords donDt get ne/ uses

    3,

    4or this section, de#ine an inter(ace to 9e one o# these

    All the uni#ors o# a rogra. -his sans all coilation units linked together within one rogra.

    -he 9oundar& 9etween ad@acent rograa9le ieline stages: -his sans all the oututs in all

    coilation units o# the #irst stage and all the inuts in all coilation units o# the second stage.

    -he 9lock nae F&loc-nameGis used to atch inter#aces: an outut 9lock o# one ieline stage will 9e

    atched to an inut 9lock with the sae nae in the su9se=uent ieline stage. 4or uni#or 9locks, the

    alication uses the 9lock nae to identi#& the 9lock. Block naes have no other use within a shader

    9e&ond inter#ace atchingL it is an error to use a 9lock nae at glo9al scoe #or an&thing other than as a

    9lock nae Fe.g., use o# a 9lock nae #or a glo9al varia9le nae or #unction nae is currentl& reservedG.

    atched 9lock naes within an inter#ace Fas de#ined a9oveG ust atch in ters o# having the sae

    nu9er o# declarations with the sae se=uence o# t&es and the sae se=uence o# e9er naes, as well

    as having the sae e9er8wise la&out =uali#ication Fsee ne2t sectionG. 4urtherore, i# a atching 9lock

    is declared as an arra&, then the arra& si?es ust also atch For #ollow arra& atching rules #or the

    inter#ace 9etween a verte2 and a geoetr& shaderG. An& isatch will generate a link error. A 9lock

    nae is allowed to have di##erent de#initions in di##erent inter#aces within the sae shader, allowing, #or

    e2ale, an inut 9lock and outut 9lock to have the sae nae.

    4"

  • 8/12/2019 GLSL Specification 4.00.9.Clean

    47/157

    4 #aria$%es and &'pes

    I# an instance nae Finstance-nameGis not used, the naes declared inside the 9lock are scoed at the

    glo9al level and accessed as i# the& were declared outside the 9lock. I# an instance nae Finstance-nameG

    is used, then it uts all the e9ers inside a scoe within its own nae sace, accessed with the #ield

    selector F . G oerator Fanalogousl& to structuresG. 4or e2ale,

    in Lig.t

    ve%8 Lig.t@os,

    ve%; Lig.tolor,

    3,

    in oloredTexture

    ve%8 olor,

    ve%9 Texoord,

    3 ?aterial, -- instan%e name

    ve%; olor, -- different olor t.an ?aterial+olor

    ve%8 Lig.t@os, -- illegal1 alread& defined

    +++

    +++ * Lig.t@os, -- a%%essing Lig.t@os

    +++ * ?aterial+olor, -- a%%essing olor in oloredTexture $lo%:

    )utside the shading language Fi.e., in the A3IG, e9ers are siilarl& identi#ied e2cet the 9lock nae is

    alwa&s used in lace o# the instance nae FA3I accesses are to inter#aces, not to shadersG. I# there is no

    instance nae, then the A3I does not use the 9lock nae to access a e9er, @ust the e9er nae.

    out Vertex

    ve%8 @osition, -- >@I transform-feed$a%: /ill use 4Vertex+@osition5

    ve%9 Texture,

    3 oords, -- s.ader /ill use 4oords+@osition5

    out Vertex9

    ve%8 olor, -- >@I /ill use 4olor5

    3,

    4or 9locks declared as arra&s, the arra& inde2 ust also 9e included when accessing e9ers, as in this

    e2ale

    uniform Transform -- >@I uses 4TransformB9C5 to refer to instan%e 9

    mat8 ?odelVie/?atrix,

    mat8 ?odelVie/@ro7e%tion?atrix,

    float eformation,

    3 transformsB8C,

    +++

    +++ * transformsB9C+?odelVie/?atrix, -- s.ader a%%ess of instan%e 9

    -- >@I uses 4Transform+?odelVie/?atrix5 to Auer& an offset or ot.er Auer&

    4or uni#or 9locks declared as an arra&, each individual arra& eleent corresonds to a searate 9u##ero9@ect 9acking one instance o# the