Вы находитесь на странице: 1из 155

The OpenGL ES

Shading Language
Language Version: 3.00
Document Revision: 3
11 July 2012
Editor: Robert J. Simpson, Qualcomm
OpenGL GLSL editor: John Kessenich, LunarG
GLSL version 1.1 uthors: John Kessenich, !ave "ald#in, Randi Rost
Copyright (c) 2008-2011 The Khronos Group Inc. All Rights Reserve.
This speci!ic"tion is protecte #y copyright l"$s "n cont"ins %"teri"l propriet"ry to the Khronos Group&
Inc. It or "ny co%ponents %"y not #e reprouce& repu#lishe& istri#ute& tr"ns%itte& ispl"ye&
#ro"c"st or other$ise e'ploite in "ny %"nner $ithout the e'press prior $ritten per%ission o! Khronos
Group. (ou %"y use this speci!ic"tion !or i%ple%enting the !unction"lity therein& $ithout "ltering or
re%oving "ny tr"e%"r)& copyright or other notice !ro% the speci!ic"tion& #ut the receipt or possession o!
this speci!ic"tion oes not convey "ny rights to reprouce& isclose& or istri#ute its contents& or to
%"nu!"cture& use& or sell "nything th"t it %"y escri#e& in $hole or in p"rt.
Khronos Group gr"nts e'press per%ission to "ny current *ro%oter& Contri#utor or Aopter %e%#er o!
Khronos to copy "n reistri#ute +,-./I0I1/ versions o! this speci!ic"tion in "ny !"shion& provie th"t
,. C2ARG1 is %"e !or the speci!ic"tion "n the l"test "v"il"#le up"te o! the speci!ic"tion !or "ny
version o! the A*I is use $henever possi#le. 3uch istri#ute speci!ic"tion %"y #e re-!or%"tte A3
4.,G A3 the contents o! the speci!ic"tion "re not ch"nge in "ny $"y. The speci!ic"tion %"y #e
incorpor"te into " prouct th"t is sol "s long "s such prouct inclues signi!ic"nt inepenent $or)
evelope #y the seller. A lin) to the current version o! this speci!ic"tion on the Khronos Group $e#-site
shoul #e inclue $henever possi#le $ith speci!ic"tion istri#utions.
Khronos Group %")es no& "n e'pressly iscl"i%s "ny& represent"tions or $"rr"nties& e'press or
i%plie& reg"ring this speci!ic"tion& incluing& $ithout li%it"tion& "ny i%plie $"rr"nties o! %erch"nt"#ility
or !itness !or " p"rticul"r purpose or non-in!ringe%ent o! "ny intellectu"l property. Khronos Group %")es
no& "n e'pressly iscl"i%s "ny& $"rr"nties& e'press or i%plie& reg"ring the correctness& "ccur"cy&
co%pleteness& ti%eliness& "n reli"#ility o! the speci!ic"tion. +ner no circu%st"nces $ill the Khronos
Group& or "ny o! its *ro%oters& Contri#utors or -e%#ers or their respective p"rtners& o!!icers& irectors&
e%ployees& "gents or represent"tives #e li"#le !or "ny "%"ges& $hether irect& inirect& speci"l or
conse5uenti"l "%"ges !or lost revenues& lost pro!its& or other$ise& "rising !ro% or in connection $ith
these %"teri"ls.
Khronos& .penK./1& .penK.G3& .pen6G& .pen-A7& .pen34 13 "n .pen80 "re tr"e%"r)s o!
the Khronos Group Inc. C.44A/A is " tr"e%"r) o! 3ony Co%puter 1ntert"in%ent Inc. use #y
per%ission #y Khronos. .penG4 "n .pen-4 "re registere tr"e%"r)s "n the .penG4 13 logo is "
tr"e%"r) o! 3ilicon Gr"phics Inc. use #y per%ission #y Khronos. All other prouct n"%es& tr"e%"r)s&
"n9or co%p"ny n"%es "re use solely !or ienti!ic"tion "n #elong to their respective o$ners.
2
Table of Contents
1 $ntroduction.................................................................................................................................1
1.1 %han&es................................................................................................................................1
1.1.1 %han&es 'rom GLSL ES (.) revision *........................................................................1
1.1.* %han&es 'rom GLSL ES (.) revision 1:.......................................................................*
1.1.( %han&es 'rom OpenGL GLSL (.(:...............................................................................*
1.* Overvie#..............................................................................................................................+
1.( Error ,andlin&......................................................................................................................+
1.+ -.po&raphical %onventions................................................................................................./
1./ %ompatibilit........................................................................................................................./
* Overvie# o' OpenGL ES Shadin&..............................................................................................0
*.1 1erte2 3rocessor..................................................................................................................0
*.* 4ra&ment 3rocessor..............................................................................................................0
( "asics..........................................................................................................................................5
(.1 %haracter Set........................................................................................................................5
(.* Source Strin&s......................................................................................................................5
(.( 1ersion !eclaration..............................................................................................................6
(.+ 3reprocessor.......................................................................................................................1)
(./ %omments..........................................................................................................................1+
(.7 -o8ens................................................................................................................................1/
(.0 Ke.#ords............................................................................................................................1/
(.5 $denti'iers...........................................................................................................................10
(.6 !e'initions..........................................................................................................................10
(.6.1 Static 9se....................................................................................................................10
(.6.* 9ni'orm and :on;9ni'orm %ontrol 4lo#..................................................................15
(.6.( !.namicall. 9ni'orm E2pressions.............................................................................15
(.1) Lo&ical 3hases o' %ompilation........................................................................................15
+ 1ariables and -.pes..................................................................................................................*)
+.1 "asic -.pes........................................................................................................................*)
+.1.1 1oid............................................................................................................................**
+.1.* "ooleans.....................................................................................................................**
+.1.( $nte&ers.......................................................................................................................**
+.1.+ 4loats..........................................................................................................................*+
+.1./ 1ectors........................................................................................................................*/
+.1.7 <atrices......................................................................................................................*7
+.1.0 Samplers.....................................................................................................................*7
+.1.5 Structures....................................................................................................................*7
+.1.6 rra.s.........................................................................................................................*5
+.* Scopin&...............................................................................................................................*6
+.*.1 !e'inition o' -erms....................................................................................................*6
+.*.* -.pes o' Scope...........................................................................................................*6
3
+.*.( Redeclarin& :ames.....................................................................................................(1
+.*.+ Global Scope..............................................................................................................((
+.*./ Shared Globals............................................................................................................((
+.( Stora&e Quali'iers...............................................................................................................((
+.(.1 !e'ault Stora&e Quali'ier............................................................................................(+
+.(.* %onstant Quali'ier......................................................................................................(+
+.(.( %onstant E2pressions.................................................................................................(/
+.(.+ $nput 1ariables...........................................................................................................(/
+.(./ 9ni'orm 1ariables......................................................................................................(7
+.(.7 Output 1ariables.........................................................................................................(0
+.(.0 $nter'ace "loc8s..........................................................................................................(5
+.(.5 La.out Quali'iers........................................................................................................+)
+.(.5.1 $nput La.out Quali'iers.......................................................................................+1
+.(.5.* Output La.out Quali'iers....................................................................................+1
+.(.5.( 9ni'orm "loc8 La.out Quali'iers.......................................................................+*
+.(.6 $nterpolation................................................................................................................++
+.(.1) Lin8in& o' 1erte2 Outputs and 4ra&ment $nputs......................................................+/
+.+ 3arameter Quali'iers...........................................................................................................+7
+./ 3recision and 3recision Quali'iers.....................................................................................+7
+./.1 Ran&e and 3recision...................................................................................................+7
+./.* %onversion bet#een precisions..................................................................................+5
+./.( 3recision Quali'iers....................................................................................................+6
+./.+ !e'ault 3recision Quali'iers......................................................................................./)
+.7 1ariance and the $nvariant Quali'ier................................................................................../1
+.7.1 -he $nvariant Quali'ier.............................................................................................../*
+.7.* $nvariance =ithin a Shader......................................................................................../(
+.7.( $nvariance o' %onstant E2pressions.........................................................................../+
+.7.+ $nvariance o' 9nde'ined 1alues................................................................................./+
+.0 Order o' Quali'ication......................................................................................................../+
/ Operators and E2pressions........................................................................................................//
/.1 Operators............................................................................................................................//
/.* rra. Operations.............................................................................................................../7
/.( 4unction %alls..................................................................................................................../7
/.+ %onstructors......................................................................................................................./7
/.+.1 %onversion and Scalar %onstructors........................................................................../7
/.+.* 1ector and <atri2 %onstructors................................................................................./0
/.+.( Structure %onstructors................................................................................................/6
/.+.+ rra. %onstructors.....................................................................................................7)
/./ 1ector %omponents............................................................................................................7)
/.7 <atri2 %omponents............................................................................................................7*
/.0 Structure and rra. Operations..........................................................................................7*
/.5 ssi&nments.......................................................................................................................7(
4
/.6 E2pressions........................................................................................................................7+
/.1) 1ector and <atri2 Operations..........................................................................................77
/.11 Evaluation o' e2pressions................................................................................................75
7 Statements and Structure...........................................................................................................76
7.1 4unction !e'initions...........................................................................................................0)
7.1.1 4unction %allin& %onventions....................................................................................0*
7.* Selection.............................................................................................................................0(
7.( $teration..............................................................................................................................0+
7.+ Jumps..................................................................................................................................0/
0 "uilt;in 1ariables......................................................................................................................00
0.1 1erte2 Shader Special 1ariables.......................................................................................00
0.* 4ra&ment Shader Special 1ariables...................................................................................05
0.( "uilt;$n %onstants..............................................................................................................05
0.+ "uilt;$n 9ni'orm State.......................................................................................................06
5 "uilt;in 4unctions.....................................................................................................................5)
5.1 n&le and -ri&onometr. 4unctions....................................................................................5*
5.* E2ponential 4unctions........................................................................................................5(
5.( %ommon 4unctions............................................................................................................5+
5.+ 4loatin&;3oint 3ac8 and 9npac8 4unctions.......................................................................55
5./ Geometric 4unctions..........................................................................................................56
5.7 <atri2 4unctions................................................................................................................61
5.0 1ector Relational 4unctions...............................................................................................6*
5.5 -e2ture Loo8up 4unctions.................................................................................................6(
5.6 4ra&ment 3rocessin& 4unctions..........................................................................................66
6 Shadin& Lan&ua&e Grammar...................................................................................................1)*
1) Errors.....................................................................................................................................11/
1).1 3reprocessor Errors........................................................................................................11/
1).* Le2er>3arser Errors.........................................................................................................11/
1).( Semantic Errors..............................................................................................................11/
1).+ Lin8er.............................................................................................................................110
11 %ountin& o' $nputs and Outputs............................................................................................116
1* $ssues.....................................................................................................................................1*(
1*.1 %ompatibilit. #ith OpenGL ES *.)...............................................................................1*(
1*.* %onver&ence #ith OpenGL............................................................................................1*(
1*.( :umeric 3recision..........................................................................................................1*(
1*.+ 4loatin& 3oint Representation and 4unctionalit............................................................1*+
1*./ 3recision Quali'iers........................................................................................................1*/
1*.7 4unction and 1ariable :ame Spaces..............................................................................1*5
1*.0 Local 4unction !eclarations and 4unction ,idin&........................................................1*6
1*.5 Overloadin& main?@........................................................................................................1*6
1*.6 Error Reportin&..............................................................................................................1*6
1*.1) Structure !eclarations..................................................................................................1*6
5
1*.11 Embedded Structure !e'initions..................................................................................1()
1*.1* Rede'inin& "uilt;in 4unctions......................................................................................1()
1*.1( Global Scope................................................................................................................1(1
1*.1+ %onstant E2pressions...................................................................................................1(1
1*.1/ 1ar.in& Lin8a&e...........................................................................................................1(1
1*.17 &lA3osition....................................................................................................................1(*
1*.10 3re;processor................................................................................................................1(*
1*.15 %haracter set.................................................................................................................1((
1*.16 Line %ontinuation.........................................................................................................1(+
1*.*) 3hases o' %ompilation..................................................................................................1(+
1*.*1 <a2imum :umber o' 1ar.in&s...................................................................................1(+
1*.** rra. !eclarations.......................................................................................................1(7
1*.*( $nvariance.....................................................................................................................1(7
1*.*+ $nvariance =ithin a shader...........................................................................................1(5
1*.*/ =hile;loop !eclarations..............................................................................................1(6
1*.*7 %ross Lin8in& "et#een Shaders...................................................................................1(6
1*.*0 1isibilit. o' !eclarations.............................................................................................1(6
1*.*5 Lan&ua&e 1ersion.........................................................................................................1+)
1*.*6 Samplers.......................................................................................................................1+)
1*.() !.namic $nde2in&........................................................................................................1+)
1*.(1 <a2imum :umber o' -e2ture 9nits............................................................................1+1
1*.(* On;tar&et Error Reportin&............................................................................................1+1
1*.(( Roundin& o' $nte&er !ivision.......................................................................................1+1
1*.(+ 9nde'ined Return 1alues.............................................................................................1+1
1*.(/ 3recisions o' Operations...............................................................................................1+*
1*.(7 %ompiler -rans'orms...................................................................................................1+(
1*.(0 E2pansion o' 4unction;li8e <acros in the 3reprocessor.............................................1+(
1*.(5 Should E2tension <acros be Globall. !e'inedB.........................................................1+(
1*.(6 <inimum ReCuirements...............................................................................................1++
1*.+) 3ac8in& 4unctions........................................................................................................1++
1*.+1 "oolean lo&ical vector operations ...............................................................................1++
1*.+* Ran&e %hec8in& o' literals...........................................................................................1++
1*.+( SeCuence operator and constant e2pressions...............................................................1+/
1*.++ 1ersion !irective.........................................................................................................1+/
1*.+/ 9se o' 9nsi&ned $nte&ers.............................................................................................1+7
1( c8no#led&ments.................................................................................................................1+0
1+ :ormative Re'erences...........................................................................................................1+6
6
1 Introduction
-his document speci'ies onl. version (.) o' the OpenGL ES Shadin& Lan&ua&e. $t reCuires
AA1ERS$O:AA to substitute ()), and reCuires #version to accept onl. ()) es. $' #version is declared
#ith a smaller number, the lan&ua&e accepted is a previous version o' the shadin& lan&ua&e, #hich #ill be
supported dependin& on the version and t.pe o' conte2t in the OpenGL ES 3$. See the OpenGL ES
Graphics S.stem Speci'ication, 1ersion (.), 'or details on #hat lan&ua&e versions are supported.
ll OpenGL ES Graphics S.stem Speci'ication re'erences in this speci'ication are to version (.)
1.1 Changes
-his speci'ication is derived 'rom OpenGL GLSL (.( revision 0.
1.1.1 Changes from GLSL ES 3.0 reision !
%lari'ied that the paramter 'or s#itch statements can be a si&ned or unsi&ned inte&er
%lari'ied that all inte&er verte2 shader outputs and 'ra&ment shader inputs must be Cuali'ied D'latD
$nvalid la.out Cuali'iers must &enerate an error
La.out Cuali'ier $!s are case sensitive
3recision o' pac8in& and unpac8in& 'unctions
3recision o' return t.pe o' te2tureSiEe?@
3recision reCuirements 'or built;in 'unctions
%onversion bet#een precisions
Removed de'ault precision 'or sampler t.pes introduced in GLSL ES (.)
&lA!epthRan&e members should be hi&hp
%lari'ied that the la.out Cuali'ier $! values can be si&ned or unsi&ned.
%lari'ied that use o' reserved 'eatures is an error
%lari'ied description o' samplin& o' proFected te2tures
1erte2 shader outputs #ith inte&er t.pe must be Cuali'ied as 'lat
%lari'ied that di&raphs and tri&raphs are disallo#ed
-he ma2imum len&th o' an identi'ier is 1)*+ characters
-he ma2imum len&th o' a macro name is 1)*+ characters
dded e2plicit statement that the precision o' a variable cannot be chan&ed
1
1 Introduction
Re&ions o' scope in loop statements, includin& corrections to &rammar
Ran&e o' lo#p inte&ers
%orrection to the countin& al&orithm e2ample 'or var.in&s
-he precision statement can be used to set the de'ault precision 'or sampler t.pes
-he de'ault precision 'or unsi&ned inte&ers cannot be set independentl. 'rom si&ned inte&ers
dded de'ault precision 'or built;in variables
%lari'ied that a macro #ith an empt. replacement list does not de'ault to D)D in a pre;processor
e2pression
$nputs cannot be declared invariant
Errors ma. be reported at compile time or lin8 time
%lari'ied that la.out Cuali'ier parameters ma. be either si&ned or unsi&ned inte&er constants
1.1.! Changes from GLSL ES 3.0 reision 1"
%lari'ied that mediump and lo#p inte&ers #rap on over'lo#
Ran&e chec8in& o' literal inte&ers
Rede'inition o' built;in macros not allo#ed
1ersion directive must be the 'irst line o' a shader
1.1.3 Changes from OpenGL GLSL 3.3"
Removed:
3ro'iles and deprecation
Geometr. shaders
<ultiple compilation units
Shared &lobals ?e2cept 'or uni'orms@
in and out bloc8s
verte2 arra. inputs ?attribute arra.s@
La.out Cuali'iers: inde2, ori&inAupperAle't and pi2elAcenterAinte&er
%33 to8en pastin&
9nsiEed arra.s.
$mplicit t.pe conversion.
Overloadin& built;in 'unctions
noperspective
<ulti;sample te2tures
2
1 Introduction
Rectan&ular te2tures
-e2ture bu''ers
1! te2tures
:oise
Outer scope 'or built;in 'unctions.
Redeclarin& built;in variables.
dded:
Line continuation and 9-4;5 in GLSL ES 1.)) #hen used #ith OpenGL ES (.)
rra. len&th operator returns an unsi&ned inte&er;constant. -he precision is determined usin&
the rules 'or literal inte&ers
%lari'ied that source code lines ma. be o' arbitrar. len&th
Line continuation
E2tended character set 'or comments
"uilt;in constants: &lA<in3ro&ram-e2elO''set, &lA<a23ro&ram-e2elO''set
,andlin& and reportin& o' errors
GLES macro
9se o' an unde'ined macro is an error
:umeric precision o' variables and operations
!e'ault precisions
!e'initions and behavior 'or precision Cuali'iers lo#p, mediump and hi&hp
$nvariance #ithin a shader
Rela2ation o' the order o' evaluation o' e2pressions
3ac8 and unpac8 built;in 'unctions
List o' errors
:ormative re'erences
E2tension macro names al#a.s de'ined i' the e2tension is available
%lari'ied that 'or the operators GG and HH, i' both operands are vectors, the. must have the same
siEe
GLSL ES 1.)) compatibilit.
1erte2 output, 'ra&ment input countin& al&orithm
3
1 Introduction
1.! Oerie#
-his document describes The OpenL !" "ha#ing Language$ version 3.00
$ndependent compilation units #ritten in this lan&ua&e are called sha#ers. program is a complete set o'
shaders that are compiled and lin8ed to&ether. -he aim o' this document is to thorou&hl. speci'. the
pro&rammin& lan&ua&e. -he OpenGL ES Graphics S.stem Speci'ication #ill speci'. the OpenGL ES
entr. points used to manipulate and communicate #ith pro&rams and shaders.
1.3 Error $andling
%ompilers, in &eneral, accept pro&rams that are ill;'ormed, due to the impossibilit. o' detectin& all ill;
'ormed pro&rams. 3ortabilit. is onl. ensured 'or #ell;'ormed pro&rams, #hich this speci'ication
describes. %ompilers are encoura&ed to detect ill;'ormed pro&rams and issue dia&nostic messa&es, but are
not reCuired to do so 'or all cases. -he compilation process is implementation;dependent but is &enerall.
split into a number o' sta&es, each o' #hich occurs at one o' the 'ollo#in& times:
call to gl%ompile"ha#er
call to glLin&'rogram
dra# call or a call to glVali#ate'rogram
-he implementation should report errors as earl. a possible but in an. case must satis'. the 'ollo#in&:
ll le2ical, &rammatical and semantic errors must have been detected 'ollo#in& a call to
glLin&'rogram
Errors due to mismatch bet#een the verte2 and 'ra&ment shader ?lin8 errors@ must have been
detected 'ollo#in& a call to &lLin83ro&ram
Errors due to e2ceedin& resource limits must have been detected 'ollo#in& an. dra# call or a call
to glVali#ate'rogram
call to glVali#ate'rogram must report all errors associated #ith a pro&ram obFect &iven the
current GL state.
=here the speci'ication uses the terms required, must>must not, does>does not, disallowed or not
supported, the compiler or lin8er is reCuired to detect and report an. violations. Similarl. #hen a
condition or situation is an error, it must be reported. 9se o' an. 'eature mar8ed as reserved is an error.
=here the speci'ication uses the terms should>should not or undefined behavior there is no such
reCuirement but compilers are encoura&ed to report possible violations.
distinction is made bet#een undefined behavior and an undefined value ?or result@. 9nde'ined
behavior includes s.stem instabilit. and>or termination o' the application. $t is e2pected that s.stems #ill
be desi&ned to handle these cases &race'ull. but speci'ication o' this is outside the scope o' OpenGL ES.
4
1 Introduction
$' a value or result is unde'ined, the s.stem ma. behave as i' the value or result had been assi&ned a
random value. 4or e2ample, an unde'ined &lA3osition ma. cause a trian&le to be dra#n #ith a random
siEe and position. -he value ma. not be consistent. 4or e2ample an unde'ined boolean value ma. cause
both sub;statements in an i';then;else statement to be e2ecuted ?see section +.7.+ $nvariance o' 9nde'ined
1alues@. -he implementation ma. also detect the &eneration and>or use o' unde'ined values and behave
accordin&l. ?'or e2ample causin& a trap@. 9nde'ined values must not b. themselves cause s.stem
instabilit.. ,o#ever unde'ined values ma. lead to other more serious conditions such as in'inite loops or
out o' bounds arra. accesses.
$mplementations ma. not in &eneral support 'unctionalit. be.ond the mandated parts o' the speci'ication
#ithout use o' the relevant e2tension. -he onl. e2ceptions are:
1. $' a 'eature is mar8ed as optional.
*. =here a ma2imum values is stated ?e.&. the ma2imum number o' verte2 outputs@, the
implementation ma. support a hi&her value than that speci'ied.
=here the implementation supports more than the mandated speci'ication, o'';tar&et compilers are
encoura&ed to issue #arnin&s i' these 'eatures are used.
-he compilation process is split bet#een the compiler and lin8er. -he allocation o' tas8s bet#een the
compiler and lin8er is implementation dependent. %onseCuentl. there are man. errors #hich ma. be
detected either at compiler or lin8 time, dependin& on the implementation.
1.% T&pographical Conentions
$talic, bold, and 'ont choices have been used in this speci'ication primaril. to improve readabilit.. %ode
'ra&ments use a 'i2ed #idth 'ont. $denti'iers embedded in te2t are italiciEed. Ke.#ords embedded in te2t
are bold. Operators are called b. their name, 'ollo#ed b. their s.mbol in bold in parentheses. -he
clari'.in& &rammar 'ra&ments in the te2t use bold 'or literals and italics 'or non;terminals. -he o''icial
&rammar in section 6 IShadin& Lan&ua&e GrammarJ uses all capitals 'or terminals and lo#er case 'or
non;terminals.
1.' Compatibilit&
-he OpenGL ES (.) 3$ is desi&ned to #or8 #ith both GLSL ES v1.)) and GLSL ES (.)). $n &eneral a
shader #ritten 'or OpenGL ES *.) should #or8 #ithout modi'ication in OpenGL ES (.).
=hen portin& applications 'rom OpenGL ES *.) to OpenGL ES (.), the 'ollo#in& points should be noted:
:ot all lan&ua&e constructs present in v1.)) o' the lan&ua&e are available in v(.)). E.&. attribute
and var.in& Cuali'iers. ,o#ever, the 'unctionalit. o' GLSL ES (.)) is a super;set o' GLSL ES
1.)).
Some 'eatures o' the OpenGL ES (.) 3$ reCuire lan&ua&e 'eatures that are present in GLSL ES
(.)) but not present in GLSL ES 1.)).
$t is an error to lin8 a verte2 shader and a 'ra&ment shader i' the. are #ritten in di''erent versions
o' the lan&ua&e.
-he OpenGL ES *.) 3$ does not support shaders #ritten in GLSL ES (.).
5
1 Introduction
9sin& GLSL ES 1.)) shaders #ithin OpenGL ES (.) ma. e2tend the resources available be.ond
the minima speci'ied in GLSL ES 1.). Shaders #hich ma8e use o' this #ill not necessaril. run
on an OpenGL ES *.) implementation:
Uniforms
-he number o' uni'orms speci'ied b. &lA<a21erte29ni'orm1ectors and returned b. the
correspondin& 3$ Cuer. is the same 'or GLSL ES versions 1.)) and (.)) #hen used as part o'
OpenGL ES (.).
Varyings, vertex outputs and fragment inputs
-hese are speci'ied di''erentl. in the t#o versions o' the lan&ua&e and ma. be di''erent. 4or
GLSL ES 1.)), the ma2imum number o' var.in&s is speci'ied b. &lA<a21ar.in&1ectors. 4or
GLSL ES (.)), the ma2imum number o' verte2 outputs and 'ra&ment inputs is independentl.
speci'ied b. &lA<a21erte2Output1ectors and &lA<a24ra&ment$nput1ectors.
$n GLSL ES 1.)), onl. var.in&s #hich are staticall. used in both the verte2 and 'ra&ment
shaders are counted. -his applies #hen GLSL ES 1.)) is used in OpenGL ES (.)
Multiple Render Targets
lthou&h &lA4ra&!ata is declared as an arra. in GLSL ES 1.)), multiple render tar&ets are not
supported in OpenGL ES *.) and are there'ore not available #hen usin& GLSL ES 1.)) in
OpenGL ES (.).
Support o' line continuation and support o' 9-4;5 characters #ithin comments is optional in
GLSL ES 1.)) #hen used #ith the OpenGL ES *.) 3$. ,o#ever, support is mandated 'or both
o' these #hen a GLSL ES 1.)) shader is used #ith the OpenGL ES (.) 3$.
6
! Oerie# of OpenGL ES Shading
-he OpenGL ES Shadin& Lan&ua&e is actuall. t#o closel. related lan&ua&es. -hese lan&ua&es are used
to create shaders 'or each o' the pro&rammable processors contained in the OpenGL ES processin&
pipeline. %urrentl., these processors are the verte2 and 'ra&ment processors.
9nless other#ise noted in this paper, a lan&ua&e 'eature applies to all lan&ua&es, and common usa&e #ill
re'er to these lan&ua&es as a sin&le lan&ua&e. -he speci'ic lan&ua&es #ill be re'erred to b. the name o'
the processor the. tar&et: verte2 or 'ra&ment.
<ost OpenGL ES state is not trac8ed or made available to shaders. -.picall., user;de'ined variables #ill
be used 'or communicatin& bet#een di''erent sta&es o' the OpenGL ES pipeline. ,o#ever, a small
amount o' state is still trac8ed and automaticall. made available to shaders, and there are a 'e# built;in
variables 'or inter'aces bet#een di''erent sta&es o' the OpenGL ES pipeline.
!.1 (erte) *rocessor
-he verte( processor is a pro&rammable unit that operates on incomin& vertices and their associated data.
%ompilation units #ritten in the OpenGL ES Shadin& Lan&ua&e to run on this processor are called verte(
sha#ers. =hen a complete set o' verte2 shaders are compiled and lin8ed, the. result in a verte( sha#er
e(ecuta)le that runs on the verte2 processor.
-he verte2 processor operates on one verte2 at a time. $t does not replace &raphics operations that reCuire
8no#led&e o' several vertices at a time.
!.! +ragment *rocessor
-he *ragment processor is a pro&rammable unit that operates on 'ra&ment values and their associated
data. %ompilation units #ritten in the OpenGL ES Shadin& Lan&ua&e to run on this processor are called
*ragment sha#ers. =hen a complete set o' 'ra&ment shaders are compiled and lin8ed, the. result in a
*ragment sha#er e(ecuta)le that runs on the 'ra&ment processor.
'ra&ment shader cannot chan&e a 'ra&mentDs ?(, y@ position. ccess to nei&hborin& 'ra&ments is not
allo#ed. -he values computed b. the 'ra&ment shader are ultimatel. used to update 'ramebu''er memor.
or te2ture memor., dependin& on the current OpenGL ES state and the OpenGL ES command that caused
the 'ra&ments to be &enerated.
7
3 ,asics
3.1 Character Set
-he source character set used 'or the OpenGL ES shadin& lan&ua&es is a subset o' 9-4;5. $t comprises
the 'ollo#in& characters:
-he letters a-z, A-Z, and the underscore ? A @
-he numbers !-".
-he s.mbols period ?@, plus ?#@, dash ?-@, slash ?$@, asteris8 ?%@, percent ?&@, an&led brac8ets ?' and
(@, sCuare brac8ets ? ) and * @, parentheses ? + and , @, braces ? - and . @, caret ?/@, vertical bar ? 0 @,
ampersand ?1@, tilde ?2@, eCuals ?3@, e2clamation point ?4@, colon ?5@, semicolon ?6@, comma ?,@, and
Cuestion mar8 ?7@.
-he number si&n ?#@ 'or preprocessor use.
"ac8slash ?DKD@, used to indicate line continuation #hen immediatel. precedin& a ne#;line.
=hite space: the space character, horiEontal tab, vertical tab, 'orm 'eed, carria&e;return, and line;
'eed.
-here are no di&raphs or tri&raphs. -here are no escape seCuences or other uses o' the bac8slash be.ond
use as the line;continuation character.
Lines are relevant 'or compiler dia&nostic messa&es and the preprocessor. -he. are terminated b.
carria&e;return or line;'eed. $' both are used to&ether, it #ill count as onl. a sin&le line termination. 4or
the remainder o' this document, an. o' these combinations is simpl. re'erred to as a ne#;line. Lines ma.
be o' arbitrar. len&th.
$n &eneral, the lan&ua&eLs use o' this character set is case sensitive.
-here are no character or strin& data t.pes, so no Cuotin& characters are included.
-here is no end;o';'ile character.
$nside comments, the character set is e2tended to allo# an. b.te values to be used but #ith the e2ception
that a b.te #ith the value Eero is al#a.s interpreted as the end o' the strin&. -he character encodin& is
assumed to be 9-4;5 but no chec8in& is per'ormed 'or invalid characters.
3.! Source Strings
-he source 'or a sin&le shader is an arra. o' strin&s o' characters 'rom the character set. sin&le shader
is made 'rom the concatenation o' these strin&s. Each strin& can contain multiple lines, separated b. ne#;
lines. :o ne#;lines need be present in a strin&M a sin&le line can be 'ormed 'rom multiple strin&s. :o
ne#;lines or other characters are inserted b. the implementation #hen it concatenates the strin&s to 'orm a
sin&le shader.
8
3 Basics
!ia&nostic messa&es returned 'rom compilin& a shader must identi'. both the line number #ithin a strin&
and #hich source strin& the messa&e applies to. Source strin&s are counted seCuentiall. #ith the 'irst
strin& bein& strin& ). Line numbers are one more than the number o' ne#;lines that have been processed.
3.3 (ersion -eclaration
Shaders must declare the version o' the lan&ua&e the. are #ritten to. -he version is speci'ied in the 'irst
line o' a shader b. a character strin&:
#version number es
#here num)er must be a version o' the lan&ua&e, 'ollo#in& the same convention as ++V!R",O-++ above.
-he directive I#version 8!! esJ is reCuired in an. shader that uses version (.)) o' the lan&ua&e. n.
num)er representin& a version o' the lan&ua&e a compiler does not support #ill cause an error to be
&enerated. 1ersion 1.)) o' the lan&ua&e does not reCuire shaders to include this directive, and shaders
that do not include a #version directive #ill be treated as tar&etin& version 1.)).
Shaders declarin& version (.)) o' the shadin& lan&ua&e cannot be lin8ed #ith shaders declarin& version
1.)).
-he #version directive must be present in the 'irst line o' a shader and must be 'ollo#ed b. a ne#line. $t
ma. contain optional #hite;space as speci'ied belo# but no other characters are allo#ed. -he directive is
onl. permitted in the 'irst line o' a shader.
version.#eclaration:
/hitespaceopt 3O9:! /hitespaceopt 1ERS$O: /hitespace num)er /hitespaceopt ES /hitespaceopt
-o8ens:
3O9:! N
1ERS$O: version
ES es
9
3 Basics
3.% *reprocessor
-here is a preprocessor that processes the source strin&s as part o' the compilation process.
-he complete list o' preprocessor directives is as 'ollo#s.
#
#define
#undef
#if
#ifdef
#ifndef
#else
#elif
#endif
#error
#pragma
#extension
#line
-he 'ollo#in& operator is also available
defined
:ote that the version directive is not considered to be a preprocessor directive and so is not listed here.
Each number si&n ?#@ can be preceded in its line onl. b. spaces or horiEontal tabs. $t ma. also be
'ollo#ed b. spaces and horiEontal tabs, precedin& the directive. Each directive is terminated b. a ne#;
line. 3reprocessin& does not chan&e the number or relative location o' ne#;lines in a source strin&.
-he number si&n ?#@ on a line b. itsel' is i&nored. n. directive not listed above #ill cause a dia&nostic
messa&e and ma8e the implementation treat the shader as ill;'ormed.
#define and #undef 'unctionalit. are de'ined as is standard 'or %OO preprocessors 'or macro de'initions
both #ith and #ithout macro parameters.
-he 'ollo#in& prede'ined macros are available
__LINE__
__FILE__
__VERSION__
GL_ES
++L,-!++ #ill substitute a decimal inte&er constant that is one more than the number o' precedin& ne#;
lines in the current source strin&.
++0,L!++ #ill substitute a decimal inte&er constant that sa.s #hich source strin& number is currentl.
bein& processed.
10
3 Basics
++V!R",O-++ #ill substitute a decimal inte&er re'lectin& the version number o' the OpenGL ES shadin&
lan&ua&e. -he version o' the shadin& lan&ua&e described in this document #ill have ++V!R",O-++
substitute the decimal inte&er ()).
L+!" #ill be de'ined and set to 1. -his is not true 'or the non;ES OpenGL Shadin& Lan&ua&e, so it can
be used to do a compile time test to determine i' a shader is runnin& on an ES s.stem.
ll macro names containin& t#o consecutive underscores ? 99 @ are reserved 'or 'uture use as prede'ined
macro names. ll macro names pre'i2ed #ith IGLAJ ?IGLJ 'ollo#ed b. a sin&le underscore@ are also
reserved.
$t is an error to unde'ine or to rede'ine a built;in ?pre;de'ined@ macro name.
-he ma2imum len&th o' a macro name is 1)*+ characters. $t is an error to declare a name #ith a len&th
&reater than this.
#if, #ifdef, #ifndef, #else, #elif, and #endif are de'ined to operate as 'or %OO e2cept 'or the 'ollo#in&:
E2pressions 'ollo#in& #if and #elif are restricted to e2pressions operatin& on literal inte&er
constants, plus identi'iers consumed b. the defined operator.
9nde'ined identi'iers not consumed b. the defined operator do not de'ault to D)D. 9se o' such
identi'iers causes an error.
%haracter constants are not supported.
s in %OO, a macro name de'ined #ith an empt. replacement list does not de'ault to D)D #hen used in a
pre;processor e2pression.
-he operators available are as 'ollo#s:
*recedence Operator class Operators .ssociatiit&
1 ?hi&hest@ parenthetical &roupin& ? @ :
* unar. de'ined
O ; P Q
Ri&ht to Le't
( multiplicative R > S Le't to Ri&ht
+ additive O ; Le't to Ri&ht
/ bit;#ise shi't GG HH Le't to Ri&ht
7 relational G H GT HT Le't to Ri&ht
0 eCualit. TT QT Le't to Ri&ht
5 bit;#ise and U Le't to Ri&ht
6 bit;#ise e2clusive or V Le't to Ri&ht
1) bit;#ise inclusive or W Le't to Ri&ht
11 lo&ical and UU Le't to Ri&ht
1* ?lo#est@ lo&ical inclusive or W W Le't to Ri&ht
11
3 Basics
-he defined operator can be used in either o' the 'ollo#in& #a.s:
defined identifier
defined ( identifier )
-here are no number si&n based operators ?e.&., no # or #:@, no NN operator, nor is there a sizeof
operator.
-he semantics o' appl.in& operators in the preprocessor match those standard in the %OO preprocessor
#ith the 'ollo#in& e2ceptions:
-he *
nd
operand in a lo&ical and ?DUUD@ operation is evaluated i' and onl. i' the 1
st
operand
evaluates to non;Eero.
-he *
nd
operand in a lo&ical or ?DWWD@ operation is evaluated i' and onl. i' the 1
st
operand evaluates
to Eero.
$' an operand is not evaluated, the presence o' unde'ined identi'iers in the operand #ill not cause an error.
3reprocessor e2pressions #ill be evaluated at compile time.
#error #ill cause the implementation to put a dia&nostic messa&e into the shader obFectLs in'ormation lo&
?see section 7.1.1* IShader and 3ro&ram QueriesJ in the OpenGL ES Graphics S.stem Speci'ication 'or
ho# to access a shader obFectLs in'ormation lo&@. -he messa&e #ill be the to8ens 'ollo#in& the #error
directive, up to the 'irst ne#;line. -he implementation must then consider the shader to be ill;'ormed.
#pragma allo#s implementation dependent compiler control. -o8ens 'ollo#in& #pragma are not subFect
to preprocessor macro e2pansion. $' an implementation does not reco&niEe the to8ens 'ollo#in&
#pragma, then it #ill i&nore that pra&ma. -he 'ollo#in& pra&mas are de'ined as part o' the lan&ua&e.
#pragma S!GL
-he ;T<=> pra&ma is used to reserve pra&mas 'or use b. this and 'uture revisions o' the lan&ua&e. :o
implementation ma. use a pra&ma #hose 'irst to8en is ;T<=>.
#pragma optimi"e#on)
#pragma optimi"e#off)
can be used to turn o'' optimiEations as an aid in developin& and debu&&in& shaders. $t can onl. be used
outside 'unction de'initions. ". de'ault, optimiEation is turned on 'or all shaders. -he debu& pra&ma
#pragma de$ug#on)
#pragma de$ug#off)
can be used to enable compilin& and annotatin& a shader #ith debu& in'ormation, so that it can be used
#ith a debu&&er. $t can onl. be used outside 'unction de'initions. ". de'ault, debu& is turned o''.
-he scope as #ell as the e''ect o' the optimiEe and debu& pra&mas is implementation;dependent e2cept
that their use must not &enerate an error.
12
3 Basics
". de'ault, compilers o' this lan&ua&e must issue compile time s.ntactic, &rammatical, and semantic
errors 'or shaders that do not con'orm to this speci'ication. n. e2tended behavior must 'irst be enabled.
!irectives to control the behavior o' the compiler #ith respect to e2tensions are declared #ith the
#extension directive
#extension extension_name : behavior
#extension all : behavior
#here e(tension+name is the name o' an e2tension. E2tension names are not documented in this
speci'ication. -he to8en all means the behavior applies to all e2tensions supported b. the compiler. -he
)ehavior can be one o' the 'ollo#in&:
behavior Effect
re/uire "ehave as speci'ied b. the e2tension e(tension+name.
Give an error on the #extension i' the e2tension e(tension+name is not
supported, or i' all is speci'ied.
enable "ehave as speci'ied b. the e2tension e(tension+name.
=arn on the #extension i' the e2tension e(tension+name is not supported.
Give an error on the #extension i' all is speci'ied.
#arn "ehave as speci'ied b. the e2tension e(tension+name, e2cept issue #arnin&s
on an. detectable use o' that e2tension, unless such use is supported b. other
enabled or reCuired e2tensions.
$' all is speci'ied, then #arn on all detectable uses o' an. e2tension used.
=arn on the #extension i' the e2tension e(tension+name is not supported.
disable "ehave ?includin& issuin& errors and #arnin&s@ as i' the e2tension
e(tension+name is not part o' the lan&ua&e de'inition.
$' all is speci'ied, then behavior must revert bac8 to that o' the non;e2tended
core version o' the lan&ua&e bein& compiled to.
=arn on the #extension i' the e2tension e(tension+name is not supported.
-he extension directive is a simple, lo#;level mechanism to set the behavior 'or each e2tension. $t does
not de'ine policies such as #hich combinations are appropriate, those must be de'ined else#here. Order
o' directives matters in settin& the behavior 'or each e2tension: !irectives that occur later override those
seen earlier. -he all variant sets the behavior 'or all e2tensions, overridin& all previousl. issued
extension directives, but onl. 'or the )ehaviors ?arn and disa@le.
13
3 Basics
-he initial state o' the compiler is as i' the directive
#extension all % disa$le
#as issued, tellin& the compiler that all error and #arnin& reportin& must be done accordin& to this
speci'ication, i&norin& an. e2tensions.
Each e2tension can de'ine its allo#ed &ranularit. o' scope. $' nothin& is said, the &ranularit. is a shader
?that is, a sin&le compilation unit@, and the e2tension directives must occur be'ore an. non;preprocessor
to8ens. $' necessar., the lin8er can en'orce &ranularities lar&er than a sin&le compilation unit, in #hich
case each involved shader #ill have to contain the necessar. e2tension directive.
<acro e2pansion is not done on lines containin& #extension and #version directives.
4or each e2tension there is an associated macro. -he macro is al#a.s de'ined in an implementation that
supports the e2tension. -his allo#s the 'ollo#in& construct to be used:
#ifdef OES_extension_name
#extension OES_extension_name % ena$le
&& 'ode t(at re)uires t(e extension
#else
&& alternative 'ode
#endif
#line must have, a'ter macro substitution, one o' the 'ollo#in& 'orms:
#line line
#line line source-string-number
#here line and source.string.num)er are constant inte&ral e2pressions. 'ter processin& this directive
?includin& its ne#;line@, the implementation #ill behave as i' it is compilin& at line number line and
source strin& number source.string.num)er. SubseCuent source strin&s #ill be numbered seCuentiall.,
until another #line directive overrides that numberin&.
$' durin& macro e2pansion a preprocessor directive is encountered, the results are unde'inedM the compiler
ma. or ma. not report an error in such cases.
3.' Comments
%omments are delimited b. >R and R>, or b. >> and a ne#;line. -he be&in comment delimiters ?>R or >>@ are
not reco&niEed as comment delimiters inside o' a comment, hence comments cannot be nested. $' a
comment resides entirel. #ithin a sin&le line, it is treated s.ntacticall. as a sin&le space. :e#;lines are
not eliminated b. comments.
14
3 Basics
3.0 To1ens
-he lan&ua&e is a seCuence o' to8ens. to8en can be
to&en:
&ey/or#
i#enti*ier
integer.constant
*loating.constant
operator
6 - .
3.2 3e&#ords
-he 'ollo#in& are the 8e.#ords in the lan&ua&e, and cannot be used 'or an. other purpose than that
de'ined b. this document:
Aonst uniform
layout
Aentroid flat smootB
@reaC Aontinue do for ?Bile s?itAB Aase default
if else
in out inout
float int void @ool true false
invariant
disAard return
matD mat8 matE
matDxD matDx8 matDxE
mat8xD mat8x8 mat8xE
matExD matEx8 matExE
veAD veA8 veAE iveAD iveA8 iveAE @veAD @veA8 @veAE
uint uveAD uveA8 uveAE
lo?p mediump BigBp preAision
samplerD< sampler8< samplerFu@e
samplerD<;Bado? samplerFu@e;Bado?
samplerD<Array
samplerD<Array;Bado?
isamplerD< isampler8< isamplerFu@e
isamplerD<Array
15
3 Basics
usamplerD< usampler8< usamplerFu@e
usamplerD<Array
struAt
-he 'ollo#in& are the 8e.#ords reserved 'or 'uture use. 9sin& them #ill result in an error:
attri@ute varying
AoBerent volatile restriAt readonly ?riteonly
resourAe atomiA9uint
noperspeAtive
patAB sample
su@routine
Aommon partition aAtive
asm
Alass union enum typedef template tBis paACed
goto
inline noinline volatile pu@liA statiA extern external interfaAe
long sBort dou@le Balf fixed unsigned superp
input output
BveAD BveA8 BveAE dveAD dveA8 dveAE fveAD fveA8 fveAE
sampler8<ReAt
filter
imageG< imageD< image8< imageFu@e
iimageG< iimageD< iimage8< iimageFu@e
uimageG< uimageD< uimage8< uimageFu@e
imageG<Array imageD<Array
iimageG<Array iimageD<Array uimageG<Array uimageD<Array
imageG<;Bado? imageD<;Bado?
imageG<Array;Bado? imageD<Array;Bado?
imageHuffer iimageHuffer uimageHuffer
samplerG< samplerG<;Bado? samplerG<Array samplerG<Array;Bado?
isamplerG< isamplerG<Array usamplerG< usamplerG<Array
samplerD<ReAt samplerD<ReAt;Bado? isamplerD<ReAt usamplerD<ReAt
samplerHuffer isamplerHuffer usamplerHuffer
16
3 Basics
samplerD<M; isamplerD<M; usamplerD<M;
samplerD<M;Array isamplerD<M;Array usamplerD<M;Array
sizeof Aast
namespaAe using

$n addition, all identi'iers containin& t#o consecutive underscores ?99@ are reserved as possible 'uture
8e.#ords.
3.4 Identifiers
$denti'iers are used 'or variable names, 'unction names, structure names, and 'ield selectors ?'ield
selectors select components o' vectors and matrices similar to structure 'ields, as discussed in section /./
I1ector %omponentsJ and section /.7 I<atri2 %omponentsJ @. $denti'iers have the 'orm
i#enti*ier
non#igit
i#enti*ier non#igit
i#enti*ier #igit
non#igit: one o'
9 a @ A d e f g B i I C l m n o p J r s t u v ? x y z
A H F < K L = M N O P > M Q R S T R ; T U V U V W Z
#igit: one o'
! G D 8 E X Y Z [ "
$denti'iers startin& #ith I&lAJ are reserved 'or use b. OpenGL ES, and ma. not be declared in a shader as
either a variable or a 'unction. $t is an error to redeclare a variable, includin& those startin& I&lAJ.
-he ma2imum len&th o' an identi'ier is 1)*+ characters. $t is an error to declare a variable #ith a len&th
&reater than this.
3.5 -efinitions
Some lan&ua&e rules described belo# depend on the 'ollo#in& de'initions.
3.5.1 Static 6se
shader contains a static use o' ?or static assignment to@ a variable ( i', a'ter preprocessin&, the shader
contains a statement that #ould read ?or #rite@ (, #hether or not run;time 'lo# o' control #ill cause that
statement to be e2ecuted.
17
3 Basics
3.5.! 6niform and 7on86niform Control +lo#
=hen e2ecutin& statements in a 'ra&ment shader, control 'lo# starts as uni*orm control *lo/M all 'ra&ments
enter the same control path into main?@. %ontrol 'lo# becomes non.uni*orm #hen di''erent 'ra&ments
ta8e di''erent paths throu&h control;'lo# statements ?selection, iteration, and Fumps@. %ontrol 'lo#
subseCuentl. returns to bein& uni'orm a'ter such diver&ent sub;statements or s8ipped code completes,
until the ne2t time di''erent control paths are ta8en.
4or e2ample:
main#)
*
float a + ,,,-&& t(is is uniform 'ontrol flo.
if #a / $) * && t(is expression is true for some fragments0 not all
,,,,- && non1uniform 'ontrol flo.
2 else *
,,,,- && non1uniform 'ontrol flo.
2
,,,,- && uniform 'ontrol flo. again
2
Other e2amples o' non;uni'orm control 'lo# can occur #ithin s#itch statements and a'ter conditional
brea8s, continues, earl. returns, and a'ter 'ra&ment discards, #hen the condition is true 'or some
'ra&ments but not others. Loop iterations that onl. some 'ra&ments e2ecute are also non;uni'orm control
'lo#.
-his is similarl. de'ined 'or other shader sta&es, based on the per;instance data items the. process.
3.5.3 -&namicall& 6niform E)pressions
'ra&ment;shader e2pression is #ynamically uni*orm i' all 'ra&ments evaluatin& it &et the same resultin&
value. =hen loops are involved, this re'ers to the e2pressionDs value 'or the same loop iteration. =hen
'unctions are involved, this re'ers to calls 'rom the same call point.
-his is similarl. de'ined 'or other shader sta&es, based on the per;instance data the. process.
:ote that constant e2pressions are triviall. d.namicall. uni'orm. $t 'ollo#s that t.pical loop counters
based on these are also d.namicall. uni'orm.
-he de'inition is not used in this version o' GLSL ES but ma. be re'erenced b. e2tensions.
3.10 Logical *hases of Compilation
-he compilation process is based on a subset o' the %OO standard ?see section 1+: :ormative Re'erences@.
-he compilation units 'or the verte2 and 'ra&ment processor are processed separatel. be'ore bein& lin8ed
to&ether in the 'inal sta&e o' compilation. -he lo&ical phases o' compilation are:
1. Source strin&s are concatenated.
*. -he source strin& is converted into a seCuence o' preprocessin& to8ens. -hese to8ens include
preprocessin& numbers, identi'iers and preprocessin& operations. %omments are each replaced
b. one space character. Line brea8s are retained.
18
3 Basics
(. -he preprocessor is run. !irectives are e2ecuted and macro e2pansion is per'ormed.
+. 3reprocessin& to8ens are converted into to8ens.
/. =hite space and line brea8s are discarded.
7. -he s.nta2 is anal.Eed accordin& to the GLSL ES &rammar.
0. -he result is chec8ed accordin& to the semantic rules o' the lan&ua&e.
5. -he verte2 and 'ra&ment shaders are lin8ed to&ether. n. verte2 outputs and correspondin&
'ra&ment inputs not used in both the verte2 and 'ra&ment shaders ma. be discarded.
6. -he binar. is &enerated.
19
% (ariables and T&pes
ll variables and 'unctions must be declared be'ore bein& used. 1ariable and 'unction names are
identi'iers.
-here are no de'ault t.pes. ll variable and 'unction declarations must have a declared t.pe, and
optionall. Cuali'iers. variable is declared b. speci'.in& its t.pe 'ollo#ed b. one or more names
separated b. commas. $n man. cases, a variable can be initialiEed as part o' its declaration b. usin& the
assi&nment operator ?3@. -he &rammar near the end o' this document provides a 'ull re'erence 'or the
s.nta2 o' declarin& variables.
9ser;de'ined t.pes ma. be de'ined usin& struAt to a&&re&ate a list o' e2istin& t.pes into a sin&le name.
-he OpenGL ES Shadin& Lan&ua&e is t.pe sa'e. -here are no implicit conversions bet#een t.pes.
%.1 ,asic T&pes
-he OpenGL ES Shadin& Lan&ua&e supports the 'ollo#in& basic data t.pes, &rouped as 'ollo#s.
-ransparent t.pes
T&pe 9eaning
void 'or 'unctions that do not return a value
@ool a conditional t.pe, ta8in& on values o' true or 'alse
int a si&ned inte&er
uint an unsi&ned inte&er
float a sin&le 'loatin&;point scalar
veAD a t#o;component 'loatin&;point vector
veA8 a three;component 'loatin&;point vector
veAE a 'our;component 'loatin&;point vector
@veAD a t#o;component "oolean vector
@veA8 a three;component "oolean vector
@veAE a 'our;component "oolean vector
iveAD a t#o;component si&ned inte&er vector
iveA8 a three;component si&ned inte&er vector
iveAE a 'our;component si&ned inte&er vector
uveAD a t#o;component unsi&ned inte&er vector
uveA8 a three;component unsi&ned inte&er vector
20
4 Variables and Types
T&pe 9eaning
uveAE a 'our;component unsi&ned inte&er vector
matD a *X* 'loatin&;point matri2
mat8 a (X( 'loatin&;point matri2
matE a +X+ 'loatin&;point matri2
matDxD same as a matD
matDx8 a 'loatin&;point matri2 #ith * columns and ( ro#s
matDxE a 'loatin&;point matri2 #ith * columns and + ro#s
mat8xD a 'loatin&;point matri2 #ith ( columns and * ro#s
mat8x8 same as a mat8
mat8xE a 'loatin&;point matri2 #ith ( columns and + ro#s
matExD a 'loatin&;point matri2 #ith + columns and * ro#s
matEx8 a 'loatin&;point matri2 #ith + columns and ( ro#s
matExE same as a matE
4loatin& 3oint Sampler -.pes ?opaCue@
T&pe 9eaning
samplerD< a handle 'or accessin& a *! te2ture
sampler8< a handle 'or accessin& a (! te2ture
samplerFu@e a handle 'or accessin& a cube mapped te2ture
samplerFu@e;Bado? a handle 'or accessin& a cube map depth te2ture #ith comparison
samplerD<;Bado? a handle 'or accessin& a *! depth te2ture #ith comparison
samplerD<Array a handle 'or accessin& a *! arra. te2ture
samplerD<Array;Bado? a handle 'or accessin& a *! arra. depth te2ture #ith comparison
Si&ned $nte&er Sampler -.pes ?opaCue@
T&pe 9eaning
isamplerD< a handle 'or accessin& an inte&er *! te2ture
isampler8< a handle 'or accessin& an inte&er (! te2ture
isamplerFu@e a handle 'or accessin& an inte&er cube mapped te2ture
isamplerD<Array a handle 'or accessin& an inte&er *! arra. te2ture
21
4 Variables and Types
9nsi&ned $nte&er Sampler -.pes ?opaCue@
T&pe 9eaning
usamplerD< a handle 'or accessin& an unsi&ned inte&er *! te2ture
usampler8< a handle 'or accessin& an unsi&ned inte&er (! te2ture
usamplerFu@e a handle 'or accessin& an unsi&ned inte&er cube mapped te2ture
usamplerD<Array a handle 'or accessin& an unsi&ned inte&er *! arra. te2ture
$n addition, a shader can a&&re&ate these usin& arra.s and structures to build more comple2 t.pes.
-here are no pointer t.pes.
%.1.1 (oid
4unctions that do not return a value must be declared as void. -here is no de'ault 'unction return t.pe.
-he 8e.#ord void cannot be used in an. other declarations ?e2cept 'or empt. 'ormal or actual parameter
lists@.
%.1.! ,ooleans
-o ma8e conditional e2ecution o' code easier to e2press, the t.pe @ool is supported. -here is no
e2pectation that hard#are directl. supports variables o' this t.pe. $t is a &enuine "oolean t.pe, holdin&
onl. one o' t#o values meanin& either true or 'alse. -#o 8e.#ords true and false can be used as literal
"oolean constants. "ooleans are declared and optionall. initialiEed as in the 'ollo# e2ample:
$ool su''ess- && de'lare 3su''ess4 to $e a 5oolean
$ool done + false- && de'lare and initiali"e 3done4
-he ri&ht side o' the assi&nment operator ? 3 @ must be an e2pression #hose t.pe is @ool.
E2pressions used 'or conditional Fumps ?if, for, 75, ?Bile, do-?Bile@ must evaluate to the t.pe @ool.
%.1.3 Integers
Si&ned and unsi&ned inte&er variables are 'ull. supported. $n this document, the term integer is meant to
&enerall. include both si&ned and unsi&ned inte&ers. ,i&hp unsi&ned inte&ers have e2actl. (* bits o'
precision. ,i&hp si&ned inte&ers use (* bits, includin& a si&n bit, in t#oDs complement 'orm. <ediump
and lo#p inte&ers have implementation;de'ined numbers o' bits. Operations resultin& in over'lo# or
under'lo# #ill not cause an. e2ception, nor #ill the. saturate, rather the. #ill I#rapJ to .ield the lo#;
order n bits o' the result #here n is the siEe in bits o' the inte&er. See section +./.1 IRan&e and 3recisionI
'or details.
$nte&ers are declared and optionall. initialiEed #ith inte&er e2pressions, as in the 'ollo#in& e2ample:
int i0 6 + 78- && default integer literal t9pe is int
uint : + ;u- && 3u4 esta$lis(es t(e t9pe as uint
22
4 Variables and Types
Literal inte&er constants can be e2pressed in decimal ?base 1)@, octal ?base 5@, or he2adecimal ?base 17@
as 'ollo#s.
integer.constant:
#ecimal.constant integer.su**i(opt
octal.constant integer.su**i(opt
he(a#ecimal.constant integer.su**i(opt
integer.su**i(: one o'
u U
#ecimal.constant:
non1ero.#igit
#ecimal.constant #igit
octal.constant:
!
octal.constant octal.#igit
he(a#ecimal.constant:
)2 he(a#ecimal.#igit
)Y he(a#ecimal.#igit
he(a#ecimal.constant he(a#ecimal.#igit
#igit:
!
non1ero.#igit
non1ero.#igit: one o'
G D 8 E X Y Z [ "
octal.#igit 5 one o'
! G D 8 E X Y Z
he(a#ecimal.#igit: one o'
! G D 8 E X Y Z [ "
a @ A d e f
A H F < K L
:o #hite space is allo#ed bet#een the di&its o' an inte&er constant, includin& a'ter the leadin& ! or a'ter
the leadin& !x or !V o' a constant, or be'ore the su''i2 u or U. =hen the su''i2 u or U is present, the
literal has t.pe uint, other#ise the t.pe is int. leadin& unar. minus si&n ?;@ is interpreted as an
arithmetic unar. ne&ation, not as part o' the constant.
$t is an error to provide a literal inte&er #hose value #ould be too lar&e to store in a hi&hp uint variable.
:ote that this onl. applies to literalsM no error chec8in& is per'ormed on the result o' a constant
e2pression.
23
4 Variables and Types
E2amples
< && signed integer0 value <
<u && unsigned integer0 value <
1< && unar9 minus applied to signed integer,
&& result is a signed integer0 value 1<
1<u && unar9 minus applies to unsigned integer
&& result is an unsigned integer0 value =xffffffff
=xffffffff && signed integer0 value 1<
=xffffffffu && unsigned integer0 value =xffffffff
=xffffffff= && error% values of signed integer is too large
%.1.% +loats
4loats are available 'or use in a variet. o' scalar calculations. 4loatin&;point variables are de'ined as in
the 'ollo#in& e2ample :
float a0 $ + <,>-
s an input value to one o' the processin& units, a 'loatin&;point variable is e2pected to match the $EEE
0/+ sin&le precision 'loatin&;point de'inition 'or precision and d.namic ran&e. ,i&hp 'loatin&;point
variables #ithin a shader are encoded accordin& to the $EEE 0/+ speci'ication 'or sin&le;precision
'loatin&;point values ?lo&icall., not necessaril. ph.sicall.@. =hile encodin&s are lo&icall. $EEE 0/+,
operations ?addition, multiplication, etc.@ are not necessaril. per'ormed as reCuired b. $EEE 0/+. See
section +./.1 IRan&e and 3recisionI 'or more details on precision and usa&e o' :a:s ?:ot a :umber@ and
$n's ?positive or ne&ative in'inities@.
24
4 Variables and Types
4loatin&;point constants are de'ined as 'ollo#s.
*loating.constant:
*ractional.constant e(ponent.part
opt
*loating.su**i(opt

#igit.se2uence e(ponent.part *loating.su**i(opt
*ractional.constant:
#igit.se2uence . #igit.se2uence
#igit.se2uence .
. #igit.se2uence
e(ponent.part:
e sign
opt
#igit.se2uence
E sign
opt
#igit.se2uence
sign: one o'
# \
#igit.se2uence:
#igit
#igit.se2uence #igit
*loating.su**i(: one o'
f L
decimal point ? @ is not needed i' the e2ponent part is present. :o #hite space ma. appear an.#here
#ithin a 'loatin&;point constant, includin& be'ore a su''i2. leadin& unar. minus si&n ?-@ is interpreted as
a unar. operator and is not part o' the 'loatin&;point constant.
-here is no limit on the number o' di&its in an. #igit.se2uence. $' the value o' the 'loatin& point number
is too lar&e ?small@ to be stored as a sin&le precision value, it is converted to positive ?ne&ative@ in'init..
value #ith a ma&nitude too small to be represented as a mantissa and e2ponent is converted to Eero.
$mplementations ma. also convert subnormal ?denormaliEed@ numbers to Eero.
%.1.' (ectors
-he OpenGL ES Shadin& Lan&ua&e includes data t.pes 'or &eneric *;, (;, and +;component vectors o'
'loatin&;point values, inte&ers, and "ooleans. 4loatin&;point vector variables can be used to store colors,
normals, positions, te2ture coordinates, te2ture loo8up results and the li8e. "oolean vectors can be used
'or component;#ise comparisons o' numeric vectors. Some e2amples o' vector declaration are:
ve'8 tex'oord<0 tex'oord8-
ve'; position-
ve'7 m9RG5?-
ive'8 textureLoo:up-
$ve'; less-
$nitialiEation o' vectors can be done #ith constructors, #hich are discussed shortl..
25
4 Variables and Types
%.1.0 9atrices
-he OpenGL ES Shadin& Lan&ua&e has built;in t.pes 'or *X*, *X(, *X+, (X*, (X(, (X+, +X*, +X(, and
+X+ matrices o' 'loatin&;point numbers. -he 'irst number in the t.pe is the number o' columns, the
second is the number o' ro#s. E2ample matri2 declarations:
mat8 mat8!-
mat; opt@atrix-
mat7 vie.0 pro6e'tion-
mat7x7 vie.- && an alternate .a9 of de'laring a mat7
mat;x8 m- && a matrix .it( ; 'olumns and 8 ro.s
$nitialiEation o' matri2 values is done #ith constructors ?described in section /.+ I%onstructorsJ @ in
column;maFor order.
matD is an alias 'or matDxD, not a distinct t.pe. Similarl. 'or mat8 and matE -he 'ollo#in& is le&al:
mat8 a-
mat8x8 $ + a-
%.1.2 Samplers
Sampler t.pes ?e.&., samplerD<@ are e''ectivel. opaCue handles to te2tures and their 'ilters. -he. are
used #ith the built;in te2ture 'unctions ?described in section 5.0 I-e2ture Loo8up 4unctionsJ @ to speci'.
#hich te2ture to access and ho# it is to be 'iltered. -he. can onl. be declared as 'unction parameters or
uniform variables ?see section +.(./ I9ni'ormJ @. E2cept 'or arra. inde2in&, structure 'ield selection, and
parentheses, samplers are not allo#ed to be operands in e2pressions. Samplers a&&re&ated into arra.s
#ithin a shader ?usin& sCuare brac8ets ) *@ can onl. be inde2ed #ith constant inte&ral e2pressions ?see
section +.(.( I%onstant E2pressionsJ@. Samplers cannot be treated as l;valuesM hence cannot be used as
out or inout 'unction parameters, nor can the. be assi&ned into. s uni'orms, the. are initialiEed onl.
#ith the OpenGL ES 3$M the. cannot be declared #ith an initialiEer in a shader. s 'unction parameters,
onl. samplers ma. be passed to samplers o' matchin& t.pe. -his enables consistenc. chec8in& bet#een
shader te2ture accesses and OpenGL ES te2ture state be'ore a shader is run.
%.1.4 Structures
9ser;de'ined t.pes can be created b. a&&re&atin& other alread. de'ined t.pes into a structure usin& the
struAt 8e.#ord. 4or e2ample,
stru't lig(t *
float intensit9-
ve'; position-
2 lig(tVar-
$n this e2ample, light becomes the name o' the ne# t.pe, and lightVar becomes a variable o' t.pe light.
-o declare variables o' the ne# t.pe, use its name ?#ithout the 8e.#ord struAt@.
lig(t lig(tVar8-
<ore 'ormall., structures are declared as 'ollo#s. ,o#ever, the complete correct &rammar is as &iven in
section 6 IShadin& Lan&ua&e GrammarJ .
26
4 Variables and Types
struct.#e*inition:
2uali*ier
opt
struAt name
opt
- mem)er.list . #eclarators
opt
3
mem)er.list:
mem)er.#eclaration3
mem)er.#eclaration mem)er.list3
mem)er.#eclaration:
)asic.type #eclarators3
#here name becomes the user;de'ined t.pe, and can be used to declare variables to be o' this ne# t.pe.
-he name shares the same name space as other variables, t.pes, and 'unctions. ll previousl. visible
variables, t.pes, constructors, or 'unctions #ith that name are hidden. -he optional 2uali*ier onl. applies
to an. #eclarators, and is not part o' the t.pe bein& de'ined 'or name.
Structures must have at least one member declaration. <ember declarators ma. contain precision
Cuali'iers, but ma. not contain an. other Cuali'iers. "it 'ields are not supported. <ember t.pes must be
alread. de'ined ?there are no 'or#ard re'erences@. <ember declarations cannot contain initialiEers.
<ember declarators can contain arra.s. Such arra.s must have a siEe speci'ied, and the siEe must be a
constant inte&ral e2pression thatDs &reater than Eero ?see section +.(.( I%onstant E2pressionsJ@. Each
level o' structure has its o#n name space 'or names &iven in member declaratorsM such names need onl.
be uniCue #ithin that name space.
non.mous structures are not supported. Embedded structure de'initions are not supported.
stru't S * float f- 2- && ?llo.ed% S is defined as a stru'ture,
stru't *
S- && Error% anon9mous stru'tures disallo.ed
stru't * ,,, 2- && Error% em$edded stru'tures disallo.ed
S s- && ?llo.ed% nested stru'ture .it( a name,
2-
Structures can be initialiEed at declaration time usin& constructors, as discussed in section /.+.( IStructure
%onstructorsJ .
n. restrictions on the usa&e o' a t.pe or Cuali'ier also appl. to a structure that contains that t.pe or
Cuali'ier. -his applies recursivel..
27
4 Variables and Types
%.1.5 .rra&s
1ariables o' the same t.pe can be a&&re&ated into arra.s b. declarin& a name 'ollo#ed b. brac8ets ? ) * @
enclosin& a siEe. -he arra. siEe must be a constant inte&ral e2pression ?see section +.(.( I%onstant
E2pressionsJ @ &reater than Eero. -he t.pe o' the siEe parameter can be a si&ned or unsi&ned inte&er and
the choice o' t.pe does not a''ect the t.pe o' the resultin& arra.. $t is ille&al to inde2 an arra. #ith a
constant inte&ral e2pression &reater than or eCual to the declared siEe. $t is also ille&al to inde2 an arra.
#ith a ne&ative constant e2pression. rra.s declared as 'ormal parameters in a 'unction declaration must
also speci'. a siEe. 9nde'ined behavior results 'rom inde2in& an arra. #ith a non;constant e2pression
thatLs &reater than or eCual to the arra.Ls siEe or less than ). Onl. one;dimensional arra.s ma. be
declared. ll basic t.pes and structures can be 'ormed into arra.s. Some e2amples are:
float fre)uen'iesA;B-
uniform ve'7 lig(tCositionA7uB-
'onst int numLig(ts + 8-
lig(t lig(tsAnumLig(tsB-
n arra. t.pe can be 'ormed b. speci'.in& a t.pe 'ollo#ed b. sCuare brac8ets ?Z [@ and includin& a siEe:
floatA>B
-his t.pe can be used an.#here an. other t.pe can be used, includin& as the return value 'rom a 'unction
floatA>B foo#) * 2
as a constructor o' an arra.
floatA>B#;,70 7,80 >,=0 >,80 <,<)
as an unnamed parameter
void foo#floatA>B)
and as an alternate #a. o' declarin& a variable or 'unction parameter.
floatA>B a-
n arra. t.pe can also be 'ormed #ithout speci'.in& a siEe i' the de'inition includes an initialiEer:
float xAB + floatA8B #<,=0 8,=)- && de'lares an arra9 of si"e 8
float 9AB + floatAB #<,=0 8,=0 ;,=)- && de'lares an arra9 of si"e ;
float aA>B-
float $AB + a-
:ote that the initialiEer itsel' does not need to be a constant e2pression but the len&th o' the initialiEer #ill
be a constant e2pression.
28
4 Variables and Types
$t is an error to declare arra.s o' arra.s:
float aA>BA;B- && illegal
floatA>B aA;B- && illegal
rra.s can have initialiEers 'ormed 'rom arra. constructors:
float aA>B + floatA>B#;,70 7,80 >,=0 >,80 <,<)-
float aA>B + floatAB#;,70 7,80 >,=0 >,80 <,<)- && same t(ing
n arra. declaration #hich leaves the siEe unspeci'ied is an error.
rra.s have a 'i2ed number o' elements. -his can be obtained b. usin& the len&th method:
a,lengt(#)- && returns > for t(e a$ove de'larations
-he return value is a constant si&ned inte&ral e2pression. -he precision is determined usin& the same
rules as 'or literal inte&ers.
%.! Scoping
-he scope o' a declaration determines #here the declaration is visible. GLSL ES uses a s.stem o'
staticall. nested scopes. -his allo#s names to be rede'ined #ithin a shader.
%.!.1 -efinition of Terms
-he term scope re'ers to a speci'ied re&ion o' the pro&ram #here names ma. be de'ined and are
&uaranteed to be visible. 4or e2ample, a compoun#+statement+/ith+scope ?D\D statement statement ... D]D@
de'ines a scope.
neste# scope is a scope de'ined #ithin an outer scope.
-he terms Dsame scope4 and Dcurrent scopeD are eCuivalent to the term DscopeD but used to emphasiEe that
nested scopes are e2cluded.
-he scope o* a #eclaration is the re&ion or re&ions o' the pro&ram #here that declaration is visible.
%.!.! T&pes of Scope
-he scope o' a variable is determined b. #here it is declared. $' it is declared outside all 'unction
de'initions, it has &lobal scope, #hich starts 'rom #here it is declared and persists to the end o' the shader
it is declared in. $' it is declared in a ?Bile test or a for statement, then it is scoped to the end o' the
'ollo#in& sub;statement ?speci'ied as statement.no.ne/.scope in the &rammar@. Other#ise, i' it is
declared as a statement #ithin a compound statement, it is scoped to the end o' that compound statement.
$' it is declared as a parameter in a 'unction de'inition, it is scoped until the end o' that 'unction de'inition.
'unctionDs parameter declarations and bod. to&ether 'orm a sin&le scope.
29
4 Variables and Types
int '? >R nested scope be&ins here R> int 8@
*
int : + : D ;- && rede'laration error of t(e name :
,,,
2
int f#int :)
*
*
int : + : D ;- && 8nd : is parameter0 initiali"ing nested first :
int m + : && use of ne. :0 .(i'( is (iding t(e parameter
2
2
4or both 'or and #hile loops, the sub;statement itsel' does not introduce a ne# scope 'or variable names,
so the 'ollo#in& has a redeclaration compile;time error:
for # &E nested s'ope $egins (ere E& int i + =- i / <=- iDD)
*
int i- && rede'laration error
2
-he bod. o' a do;#hile loop introduces a ne# scope lastin& onl. bet#een the do and #hile ?not includin&
the #hile test e2pression@, #hether or not the bod. is simple or compound:
int i + <F-
do
int i + 7- && o:a90 in nested s'ope
.(ile #i ++ =)- && i is <F0 s'oped outside t(e do1.(ile $od9
Representin& the i' construct as:
if i';e2pression tBen i';statement else else;statement,
a variable declared in the i';statement is scoped to the end o' the i';statement. variable declared in the
else;statement is scoped to the end o' the else;statement. -his applies both #hen these statements are
simple statements and #hen the. are compound statements. -he i';e2pression does not allo# ne#
variables to be declared, hence does not 'orm a ne# scope.
30
4 Variables and Types
=ithin a declaration, the scope o' a name starts immediatel. a'ter the initialiEer i' present or immediatel.
a'ter the name bein& declared i' not. Several e2amples:
int x + <-
*
int x + 80&E 8nd x visi$le (ere E& 9 + x- && 9 is initiali"ed to 8
int " + "- && error if " not previousl9 defined,
2
*
int x + x- && x is initiali"ed to G<G
2
structure name declaration is visible at the end o' the struct+speci*ier in #hich it #as declared:
stru't S
*
int x-
2-
*
S S + S#=)- && GSG is onl9 visi$le as a stru't and 'onstru'tor
S- && GSG is no. visi$le as a varia$le
2
int x + x- && Error if x (as not $een previousl9 defined,
%.!.3 :edeclaring 7ames
ll variable names, structure t.pe names, and 'unction names in a &iven scope share the same name space.
4unction names can be redeclared in the same scope, #ith the same or di''erent parameters, #ithout error.
Other#ise, #ithin a shader, a declared name cannot be redeclared in the same scopeM doin& so results in a
redeclaration error. $' a nested scope redeclares a name used in an outer scope, it hides all e2istin& uses o'
that name. -here is no #a. to access the hidden name or ma8e it unhidden, #ithout e2itin& the scope that
hid it.
:ames o' built;in 'unctions cannot be redeclared as 'unctions. -here'ore overloadin& or rede'inin& built;
in 'unctions is an error.
31
4 Variables and Types
#eclaration is considered to be a statement that adds a name or si&nature to the s.mbol table.
#e*inition is a statement that 'ull. de'ines that name or si&nature. E.&.
int f#)- && de'laration-
int f#) *return =-2 && de'laration and definition
int x- && de'laration and definition
int aA7B- && arra9 de'laration and definition
stru't S *int x-2- && stru'ture de'laration and definition
-he determination o' eCuivalence o' t#o declarations depends on the t.pe o' declaration. 4or 'unctions,
the #hole 'unction si&nature must be considered ?see section 7.1 4unction !e'initions@. 4or variables
?includin& arra.s@ and structures onl. the names must match.
=ithin each scope, a name ma. be declared either as a variable declaration or as 'unction declarations or
as a structure.
E2amples o' combinations that are allo#ed:
1.
void f#int) *,,,2
void f#float) *,,,2 && fun'tion overloading allo.ed
*.
void f#int)- && <
st
de'laration #allo.ed)
void f#int)- && repeated de'laration #allo.ed)
void f#int) *,,,2 && single definition #allo.ed)
E2amples o' combinations that are disallo#ed:
1.
void f#int) *,,,2
void f#int) *,,,2 && Error% repeated definition
*.
void f#int)-
stru't f *int x-2- && Error% t9pe GfG 'onfli'ts .it( fun'tion GfG
(.
stru't f *int x-2-
int f- && Error% 'onfli'ts .it( t(e t9pe GfG
+.
int aA;B-
int aA;B- && Error% repeated arra9 definition
/.
int x-
int x- && Error% repeated varia$le definition
32
4 Variables and Types
%.!.% Global Scope
-he built;in 'unctions are scoped in the &lobal scope users declare &lobal variables in. -hat is, a shaderDs
&lobal scope, available 'or user;de'ined 'unctions and &lobal variables, is the same as the scope containin&
the built;in 'unctions. 4unction declarations ?protot.pes@ cannot occur inside o' 'unctionsM the. must be at
&lobal scope.,,ence it is not possible to hide a name #ith a 'unction.
%.!.' Shared Globals
Shared &lobals are variables that can be accessed b. multiple compilation units. $n GLSL ES the onl.
shared &lobals are uni'orms. 1erte2 shader outputs are not considered to be shared &lobals since the.
must pass throu&h the rasteriEation sta&e be'ore the. are used as input b. the 'ra&ment shader.
Shared &lobals share the same name space, and must be declared #ith the same t.pe and precision. -he.
#ill share the same stora&e. Shared &lobal arra.s must have the same base t.pe and the same e2plicit siEe.
Scalars must have e2actl. the same precision, t.pe name and t.pe de'inition. Structures must have the
same name, seCuence o' t.pe names, and t.pe de'initions, and 'ield names to be considered the same t.pe.
-his rule applies recursivel. 'or nested or embedded t.pes.
%.3 Storage ;ualifiers
1ariable declarations ma. have one stora&e Cuali'ier speci'ied in 'ront o' the t.pe. -hese are summariEed
as
;ualifier 9eaning
G none: de'ault H local read>#rite memor., or an input parameter to a 'unction
Aonst a compile;time constant, or a 'unction parameter that is read;onl.
in
Aentroid in
lin8a&e into a shader 'rom a previous sta&e, variable is copied in
lin8a&e #ith centroid based interpolation
out
Aentroid out
lin8a&e out o' a shader to a subseCuent sta&e, variable is copied out
lin8a&e #ith centroid based interpolation
uniform value does not chan&e across the primitive bein& processed, uni'orms
'orm the lin8a&e bet#een a shader, OpenGL ES, and the application
Outputs 'rom shader ?out@ and inputs to a shader ?in@ can be 'urther Cuali'ied #ith one o' these
interpolation Cuali'iers
;ualifier 9eaning
smootB perspective correct interpolation
flat no interpolation
-hese interpolation Cuali'iers ma. onl. precede the Cuali'iers in, Aentroid in, out, or Aentroid out in a
declaration. -he. do not appl. to inputs into a verte2 shader or outputs 'rom a 'ra&ment shader.
Local variables can onl. use the Aonst stora&e Cuali'ier.
33
4 Variables and Types
4unction parameters can use Aonst, in, and out Cuali'iers, but as parameter 2uali*iers. 3arameter
Cuali'iers are discussed in section 7.1.1 I4unction %allin& %onventionsJ.
4unction return t.pes and structure 'ields do not use stora&e Cuali'iers.
!ata t.pes 'or communication 'rom one run o' a shader e2ecutable to its ne2t run ?to communicate
bet#een 'ra&ments or bet#een vertices@ do not e2ist. -his #ould prevent parallel e2ecution o' the same
shader e2ecutable on multiple vertices or 'ra&ments.
$nitialiEers ma. onl. be used in declarations o' &lobals #ith no stora&e Cuali'ier or #ith a Aonst Cuali'ier.
Such initialiEers must be a constant e2pression. Global variables #ithout stora&e Cuali'iers that are not
initialiEed in their declaration or b. the application #ill not be initialiEed b. OpenGL ES, but rather #ill
enter main56 #ith unde'ined values.
%.3.1 -efault Storage ;ualifier
$' no Cuali'ier is present on a &lobal variable, then the variable has no lin8a&e to the application or shaders
runnin& on other pipeline sta&es. 4or either &lobal or local unCuali'ied variables, the declaration #ill
appear to allocate memor. associated #ith the processor it tar&ets. -his variable #ill provide read>#rite
access to this allocated memor..
%.3.! Constant ;ualifier
:amed compile;time constants can be declared usin& the Aonst Cuali'ier. n. variables Cuali'ied as
constant are read;onl. variables 'or that shader. !eclarin& variables as constant allo#s more descriptive
shaders than usin& hard;#ired numerical constants. -he Aonst Cuali'ier can be used #ith an. o' the non;
void transparent basic data t.pes as #ell as structures and arra.s o' these. $t is an error to #rite to a Aonst
variable outside o' its declaration, so the. must be initialiEed #hen declared. 4or e2ample,
'onst ve'; "?xis + ve'; #=,=0 =,=0 <,=)-
Structure 'ields ma. not be Cuali'ied #ith Aonst. Structure variables can be declared as Aonst, and
initialiEed #ith a structure constructor.
$nitialiEers 'or const declarations must be constant e2pressions, as de'ined in section +.(.( I%onstant
E2pressions.J
34
4 Variables and Types
%.3.3 Constant E)pressions
constant e(pression is one o'
a literal value ?e.&., X or true@
a &lobal or local variable Cuali'ied as Aonst ?i.e., not includin& 'unction parameters@
an e2pression 'ormed b. an operator on operands that are all constant e2pressions, includin& &ettin& an
element o' a constant arra., or a 'ield o' a constant structure, or components o' a constant vector.
,o#ever, the seCuence operator ? , @ and the assi&nment operators ? T, OT, ...@ are not included in the
operators that can create a constant e2pression.
the len&th?@ method on an arra., #hether or not the obFect itsel' is constant.
a constructor #hose ar&uments are all constant e2pressions
a built;in 'unction call #hose ar&uments are all constant e2pressions, #ith the e2ception o' the te2ture
loo8up 'unctions. -he built;in 'unctions dLdx, dLdy, and f?idtB must return ) #hen evaluated inside
an initialiEer #ith an ar&ument that is a constant e2pression.
4unction calls to user;de'ined 'unctions ?non;built;in 'unctions@ cannot be used to 'orm constant
e2pressions.
Scalar, vector, matri2, arra. and structure variables are constant e2pressions i' Cuali'ied as const.
Sampler t.pes cannot be constant e2pressions.
constant integral e(pression is a constant e2pression that evaluates to a scalar si&ned or unsi&ned
inte&er.
%onstant e2pressions #ill be evaluated in an invariant #a. so as to create the same value in multiple
shaders #hen the same constant e2pressions appear in those shaders. See section +.7.1 I-he $nvariant
Quali'ierJ 'or more details on ho# to create invariant e2pressions.
%.3.% Input (ariables
Shader input variables are declared #ith the in stora&e Cuali'ier or the Aentroid in stora&e Cuali'ier. -he.
'orm the input inter'ace bet#een previous sta&es o' the OpenGL ES pipeline and the declarin& shader.
$nput variables must be declared at &lobal scope. 1alues 'rom the previous pipeline sta&e are copied into
input variables at the be&innin& o' shader e2ecution. 1ariables declared as in or Aentroid in ma. not be
#ritten to durin& shader e2ecution. Onl. the input variables that are actuall. read need to be #ritten b.
the previous sta&eM it is allo#ed to have super'luous declarations o' input variables.
See section 0 I"uilt;in 1ariablesJ 'or a list o' the built;in input names.
1erte2 shader input variables ?or attributes@ receive per;verte2 data. -he. are declared in a verte2 shader
#ith the in Cuali'ier . $t is an error to use Aentroid in or interpolation Cuali'iers in a verte2 shader input.
-he values copied in are established b. the OpenGL ES 3$ or throu&h the use o' the la.out identi'ier
location. 1erte2 shader inputs can onl. be float, 'loatin&;point vectors, matrices, si&ned and unsi&ned
inte&ers and inte&er vectors. 1erte2 shader inputs cannot be arra.s or structures.
35
4 Variables and Types
E2ample declarations in a verte2 shader:
in ve'7 position-
in ve'; normal-
$t is e2pected that &raphics hard#are #ill have a small number o' 'i2ed vector locations 'or passin& verte2
inputs. -here'ore, the OpenGL ES Shadin& lan&ua&e de'ines each non;matri2 input variable as ta8in& up
one such vector location. -here is an implementation dependent limit on the number o' locations that can
be used, and i' this is e2ceeded it #ill cause a lin8 error. ?!eclared input variables that are not staticall.
used do not count a&ainst this limit.@ scalar input counts the same amount a&ainst this limit as a veAE,
so applications ma. #ant to consider pac8in& &roups o' 'our unrelated 'loat inputs to&ether into a vector
to better utiliEe the capabilities o' the underl.in& hard#are. matri2 input #ill use up multiple locations.
-he number o' locations used #ill eCual the number o' columns in the matri2.
4ra&ment shader inputs &et per;'ra&ment values, t.picall. interpolated 'rom a previous sta&eDs outputs.
-he. are declared in 'ra&ment shaders #ith the in stora&e Cuali'ier or the Aentroid in stora&e Cuali'ier.
4ra&ment inputs can onl. be si&ned and unsi&ned inte&ers and inte&er vectors, float, 'loatin&;point
vectors, matrices, or arra.s or structures o' these. 4ra&ment shader inputs that are, or contain, si&ned or
unsi&ned inte&ers or inte&er vectors must be Cuali'ied #ith the interpolation Cuali'ier flat.
4ra&ment inputs are declared as in the 'ollo#in& e2amples:
in ve'; normal-
'entroid in ve'8 exHoord-
invariant 'entroid in ve'7 Holor-
flat in ve'; m9Holor-
-he output o' the verte2 shader and the input o' the 'ra&ment shader 'orm an inter'ace. 4or this inter'ace,
verte2 shader output variables and 'ra&ment shader input variables o' the same name must match in t.pe
and Cuali'ication ?other than out matchin& to in@.
%.3.' 6niform (ariables
-he uniform Cuali'ier is used to declare &lobal variables #hose values are the same across the entire
primitive bein& processed. ll uniform variables are read;onl.. -he. are initialiEed to ) at lin8 timeand
ma. be updated throu&h the 3$.
E2ample declarations are:
uniform ve'7 lig(tCosition-
-he uniform Cuali'ier can be used #ith an. o' the basic data t.pes, or #hen declarin& a variable #hose
t.pe is a structure, or an arra. o' an. o' these.
-here is an implementation dependent limit on the amount o' stora&e 'or uni'orms that can be used 'or
each t.pe o' shader and i' this is e2ceeded it #ill cause a compile;time or lin8;time error. 9ni'orm
variables that are declared but not used do not count a&ainst this limit. -he number o' user;de'ined
uni'orm variables and the number o' built;in uni'orm variables that are used #ithin a shader are added
to&ether to determine #hether available uni'orm stora&e has been e2ceeded.
36
4 Variables and Types
9ni'orms in the verte2 and 'ra&ment shaders share a sin&le &lobal name space. ,ence, the t.pes and
precisions o' uni'orm variables #ith the same name must match across shaders that are lin8ed into a
sin&le pro&ram.
%.3.0 Output (ariables
Shader output variables are declared #ith the out or Aentroid out stora&e Cuali'iers. -he. 'orm the
output inter'ace bet#een the declarin& shader and the subseCuent sta&es o' the OpenGL ES pipeline.
Output variables must be declared at &lobal scope. !urin& shader e2ecution the. #ill behave as normal
unCuali'ied &lobal variables. -heir values are copied out to the subseCuent pipeline sta&e on shader e2it.
Onl. output variables that are read b. the subseCuent pipeline sta&e need to be #rittenM it is allo#ed to
have super'luous declarations o' output variables.
-here is not an inout stora&e Cuali'ier at &lobal scope 'or declarin& a sin&le variable name as both input
and output to a shader. Output variables must be declared #ith di''erent names than input variables.
1erte2 output variables output per;verte2 data and are declared usin& the out stora&e Cuali'ier or the
Aentroid out stora&e Cuali'ier. -he. can onl. be float, 'loatin&;point vectors, matrices, si&ned or
unsi&ned inte&ers or inte&er vectors, or arra.s or structures o' an. these. 1erte2 shader outputs that are,
or contain, si&ned or unsi&ned inte&ers or inte&er vectors must be Cuali'ied #ith the interpolation Cuali'ier
flat.
$ndividual verte2 outputs are declared as in the 'ollo#in& e2amples:
out ve'; normal-
'entroid out ve'8 exHoord-
invariant 'entroid out ve'7 Holor-
flat out ve'; m9Holor-
4ra&ment outputs output per;'ra&ment data and are declared usin& the out stora&e Cuali'ier. $t is an error
to use Aentroid out in a 'ra&ment shader. 4ra&ment outputs can onl. be float, 'loatin&;point vectors,
si&ned or unsi&ned inte&ers or inte&er vectors, or arra.s o' an. these. Outputs declared as arra.s ma. onl.
be inde2ed b. a constant inte&ral e2pression. <atrices and structures cannot be output. 4ra&ment outputs
are declared as in the 'ollo#in& e2amples:
out ve'7 FragmentHolor-
out uint Luminosit9-
37
4 Variables and Types
%.3.2 Interface ,loc1s
9ni'orm variable declarations can be &rouped into named inter'ace bloc8s to provide coarser &ranularit.
bac8in& than is achievable #ith individual declarations. -he. can have an optional instance name, used in
the shader to re'erence their members. uni'orm bloc8 is bac8ed b. the application #ith a bu''er obFect.
GLSL ES (.) does not support inter'ace bloc8s 'or shader inputs or outputs.
n inter'ace bloc8 is started b. a uniform 8e.#ord, 'ollo#ed b. a bloc8 name, 'ollo#ed b. an open curl.
brace ? - @ as 'ollo#s:
inter*ace.)loc&:
layout.2uali*ieropt uniform )loc&.name - mem)er.list . instance.nameopt 6
layout.2uali*ier:
layout + layout.2uali*ier.i#.list ,
layout.2uali*ier.i#.list
comma separated list o' layout.2uali*ier.i#
mem)er.list:
mem)er.#eclaration
mem)er.#eclaration mem)er.list
mem)er.#eclaration:
layout.2uali*ieropt 2uali*iersopt type #eclarators 6
instance.name:
i#enti*ier
i#enti*ier 7 constant.integral.e(pression 8
Each o' the above elements is discussed belo#, #ith the e2ception o' la.out Cuali'iers ?layout.2uali*ier6,
#hich are de'ined in the ne2t section.
4irst, an e2ample,
uniform ransform *
mat7 @odelVie.@atrix-
mat7 @odelVie.Cro6e'tion@atrix-
uniform mat; Normal@atrix- && allo.ed restatement of )ualifier
float !eformation-
2-
-he above establishes a uni'orm bloc8 named I-rans'ormJ #ith 'our uni'orms &rouped inside it.
-.pes and declarators are the same as 'or other uni'orm variable declarations outside bloc8s, #ith these
e2ceptions:
sampler t.pes are not allo#ed
structure de'initions cannot be nested inside a bloc8
Other#ise, built;in t.pes, previousl. declared structures, and arra.s o' these are allo#ed as the t.pe o' a
declarator in the same manner the. are allo#ed outside a bloc8.
38
4 Variables and Types
Repeatin& the uniform inter'ace Cuali'ier 'or a memberDs stora&e Cuali'ier is optional. 4or e2ample,
uniform ransform
*
uniform mat7 model_vie.- && legal0 uniform inside a uniform $lo':,
mat7 pro6e'tion- && legal0 GuniformG in(erited from $lo':,
in $ool transform_flag- && illegal0 mem$er is not a uniform,
2
4or uni'orm bloc8s, the application uses the bloc8 name to identi'. the bloc8. "loc8 names have no other
use #ithin a shader be.ond inter'ace matchin&M it is an error to use a bloc8 name at &lobal scope 'or
an.thin& other than as a bloc8 name ?e.&., use o' a bloc8 name 'or a &lobal variable name or 'unction
name is currentl. reserved@.
<atched bloc8 names #ithin an inter'ace ?as de'ined above@ must match in terms o' havin& the same
number o' declarations #ith the same seCuence o' t.pes, precisions and the same seCuence o' member
names, as #ell as havin& the same member;#ise la.out Cuali'ication ?see ne2t section@. 4urthermore, i' a
matchin& bloc8 is declared as an arra., then the arra. siEes must also match.
$' an instance name ?instance.name@ is not used, the names declared inside the bloc8 are scoped at the
&lobal level and accessed as i' the. #ere declared outside the bloc8. $' an instance name ?instance.name@
is used, then it puts all the members inside a scope #ithin its o#n name space, accessed #ith the 'ield
selector ? @ operator ?analo&ousl. to structures@. 4or e2ample,
uniform ransform_<
*
mat7 modelvie.-
2
uniform ransform_8
*
mat7 pro6e'tion-
2 transform_8-
mat7 modelvie.- && illegal as modelvie. alread9 defined at t(is s'ope
mat7 pro6e'tion- && legal as pro6e'tion and transform_8,pro6e'tion are
&& distin't,
Outside the shadin& lan&ua&e ?i.e., in the 3$@, members are similarl. identi'ied e2cept the bloc8 name is
al#a.s used in place o' the instance name ?3$ accesses are to inter'aces, not to shaders@. $' there is no
instance name, then the 3$ does not use the bloc8 name to access a member, Fust the member name. 4or
e2ample:
39
4 Variables and Types
uniform ransform_<
*
mat7 modelvie.- && ?CI .ill use 3modelvie.4
2
uniform ransform_8
*
mat7 pro6e'tion- && ?CI .ill use 3ransform_8,pro6e'tion4
2 transform_8-
4or bloc8s declared as arra.s, the arra. inde2 must also be included #hen accessin& members, as in this
e2ample
uniform ransform * && ?CI uses 3ransformA8B4 to refer to instan'e 8
mat7 @odelVie.@atrix-
mat7 @odelVie.Cro6e'tion@atrix-
float !eformation-
2 transformsA7B-
,,,
,,, + transformsA8B,@odelVie.@atrix- && s(ader a''ess of instan'e 8
&& ?CI uses 3ransform,@odelVie.@atrix4 to )uer9 an offset or ot(er )uer9
4or uni'orm bloc8s declared as an arra., each individual arra. element corresponds to a separate bu''er
obFect bac8in& one instance o' the bloc8. s the arra. siEe indicates the number o' bu''er obFects needed,
uni'orm bloc8 arra. declarations must speci'. an arra. siEe. ll inde2es used to inde2 a uni'orm bloc8
arra. must be constant inte&ral e2pressions.
=hen usin& OpenGL ES 3$ entr. points to identi'. the name o' an individual bloc8 in an arra. o'
bloc8s, the name strin& must include an arra. inde2 ?e.&., Trans*orm728@. =hen usin& OpenGL ES 3$
entr. points to re'er to o''sets or other characteristics o' a bloc8 member, an arra. inde2 must not be
speci'ied ?e.&., Trans*orm.9o#elVie/9atri(@.
-here is an implementation dependent limit on the number o' uni'orm bloc8s that can be used per sta&e.
$' this limit is e2ceeded, it #ill cause a lin8 error.
%.3.4 La&out ;ualifiers
La.out Cuali'iers can appear in several 'orms o' declaration. -he. can appear as part o' an inter'ace
bloc8 de'inition or bloc8 member, as sho#n in the &rammar in the previous section. -he. can also appear
#ith Fust a uni'orm to establish la.outs o' other uni'orm declarations:
layout.2uali*ier uniform 6
Or, the. can appear #ith an individual variable declared #ith an inter'ace Cuali'ier:
layout.2uali*ier inter*ace.2uali*ier #eclaration 6
!eclarations o' la.outs can onl. be made at &lobal scope, and onl. #here indicated in the 'ollo#in&
subsectionsM their details are speci'ic to #hat the inter'ace Cuali'ier is, and are discussed individuall..
40
4 Variables and Types
$nter'ace Cuali'iers are a subset o' stora&e Cuali'iers:
inter*ace.2uali*ier:
in
out
uniform
s sho#n in the previous section, layout.2uali*ier e2pands to:
layout.2uali*ier :
layout + layout.2uali*ier.i#.list ,
-he to8ens in an. layout.2uali*ier.i#.list are identi'iers, not 8e.#ords. Generall., the. can be listed in
an. order. Order;dependent meanin&s e2ist onl. i' e2plicitl. called out belo#. s 'or other identi'iers,
the. are case sensitive.
%.3.4.1 Input La&out ;ualifiers
1erte2 shaders allo# input la.out Cuali'iers on input variable declarations. -he la.out Cuali'ier identi'ier
'or verte2 shader inputs is:
layout.2uali*ier.i#
loAation 3 integer.constant
Onl. one ar&ument is accepted. 4or e2ample,
la9out#lo'ation + ;) in ve'7 normal-
#ill establish that the verte2 shader input normal is copied in 'rom vector location number (.
$' an input variable #ith no location assi&ned in the shader te2t has a location speci'ied throu&h the
OpenGL ES 3$, the 3$;assi&ned location #ill be used. Other#ise, such variables #ill be assi&ned a
location b. the lin8er. See section *.11./ I1erte2 ttributesJ o' the OpenGL ES (.) Graphics S.stem
Speci'ication 'or more details.
4ra&ment shaders cannot have input la.out Cuali'iers.
%.3.4.! Output La&out ;ualifiers
1erte2 shaders cannot have output la.out Cuali'iers.
$n the 'ra&ment shader, a bindin& bet#een an output variable and a numbered dra# bu''er is established
b. the location la.out Cuali'ier in the output declaration. -he location o' each output corresponds to the
dra# bu''er the data is #ritten to. Locations are inte&ral values in the ran&e
Z), <YA!R=A"944ERS ^ 1[.
4ra&ment shaders allo# output la.out Cuali'iers onl. on the inter'ace Cuali'ier out. -he la.out Cuali'ier
identi'ier 'or 'ra&ment shader outputs is:
layout.2uali*ier.i#
loAation 3 integer.constant
41
4 Variables and Types
-he Cuali'ier ma. appear at most once #ithin a declaration. 4or e2ample,
la9out#lo'ation + ;) out ve'7 'olor-
#ill establish that the 'ra&ment shader output color is copied out to dra# bu''er (.
$' the named 'ra&ment shader output is an arra., it #ill be assi&ned consecutive locations startin& #ith the
location speci'ied. 4or e2ample,
la9out#lo'ation + 8) out ve'7 'olorsA;B-
#ill establish that colors is copied out to dra# bu''ers *, (, and +.
$' there is onl. a sin&le output, the location does not need to be speci'ied, in #hich case it de'aults to Eero.
-his applies 'or all output t.pes, includin& arra.s. 4or e2ample,
out ve'7 m9_FragHolor- && must $e t(e onl9 output de'laration
#ill establish that the 'ra&ment shader output my+0rag%olor is copied out to dra# bu''er ). Li8e#ise,
out ve'7 m9_Frag!ataA7B- && must $e t(e onl9 output de'laration
#ill establish that the 'ra&ment shader outputs my+0ragData708 to my+0ragData738 is copied out to
dra# bu''ers ) throu&h ( respectivel..
$' there is more than one output, the location must be speci'ied 'or all outputs. $t is an error i' an. o' the
'ollo#in& occur:
-he location o' an. output or element o' an arra. output, is &reater or eCual to the value o'
<YA!R=A"944ERS.
<ore than one output or element o' an arra. output is bound to the same location.
See section (.6.* IShader E2ecutionJ o' the OpenGL ES (.) Graphics S.stem Speci'ication 'or more
details.
%.3.4.3 6niform ,loc1 La&out ;ualifiers
La.out Cuali'iers can be used 'or uni'orm bloc8s, but not 'or non;bloc8 uni'orm declarations. -he la.out
Cuali'ier identi'iers 'or uni'orm bloc8s are:
layout.2uali*ier.i#
sBared
paACed
stdGE!
ro?9maIor
Aolumn9maIor
:one o' these have an. semantic a''ect at all on the usa&e o' the variables bein& declaredM the. onl.
describe ho# data is laid out in memor.. 4or e2ample, matri2 semantics are al#a.s column;based, as
described in the rest o' this speci'ication, no matter #hat la.out Cuali'iers are bein& used.
42
4 Variables and Types
9ni'orm bloc8 la.out Cuali'iers can be declared 'or &lobal scope, on a sin&le uni'orm bloc8, or on a sin&le
bloc8 member declaration.
!e'ault la.outs are established at &lobal scope 'or uni'orm bloc8s as
la9out#layout.2uali*ier.i#.list) uniform-
=hen this is done, the previous de'ault Cuali'ication is 'irst inherited and then overridden as per the
override rules listed belo# 'or each Cuali'ier listed in the declaration. -he result becomes the ne# de'ault
Cuali'ication scoped to subseCuent uni'orm bloc8 de'initions.
-he initial state o' compilation is as i' the 'ollo#in& #ere declared:
la9out#s(ared0 'olumn_ma6or) uniform-
E2plicitl. declarin& this in a shader #ill return de'aults bac8 to their initial state.
9ni'orm bloc8s can be declared #ith optional la.out Cuali'iers, and so can their individual member
declarations. Such bloc8 la.out Cuali'ication is scoped onl. to the content o' the bloc8. s #ith &lobal
la.out declarations, bloc8 la.out Cuali'ication 'irst inherits 'rom the current de'ault Cuali'ication and then
overrides it. Similarl., individual member la.out Cuali'ication is scoped Fust to the member declaration,
and inherits 'rom and overrides the bloc8Ds Cuali'ication.
-he share# Cuali'ier overrides onl. the st#1:0 and pac&e# Cuali'iersM other Cuali'iers are inherited. -he
compiler>lin8er #ill ensure that multiple pro&rams and pro&rammable sta&es containin& this de'inition
#ill share the same memor. la.out 'or this bloc8, as lon& as the. also matched in their ro/+ma;or and>or
column+ma;or Cuali'ications. -his allo#s use o' the same bu''er to bac8 the same bloc8 de'inition across
di''erent pro&rams.
-he pac&e# Cuali'ier overrides onl. st#1:0 and share#M other Cuali'iers are inherited. =hen pac&e# is
used, no shareable la.out is &uaranteed. -he compiler and lin8er can optimiEe memor. use based on #hat
variables activel. &et used and on other criteria. O''sets must be Cueried, as there is no other #a. o'
&uaranteein& #here ?and #hich@ variables reside #ithin the bloc8. ttempts to share a pac8ed uni'orm
bloc8 across pro&rams or sta&es #ill &enerall. 'ail. ,o#ever, implementations ma. aid application
mana&ement o' pac8ed bloc8s b. usin& canonical la.outs 'or pac8ed bloc8s.
-he st#1:0 Cuali'ier overrides onl. the pac&e# and share# Cuali'iersM other Cuali'iers are inherited. -he
la.out is e2plicitl. determined b. this, as described in section *.11./ I9ni'orm 1ariablesJ under
IStandard 9ni'orm "loc8 La.outJ o' the OpenGL ES Graphics S.stem Speci'ication. ,ence, as in
share# above, the resultin& la.out is shareable across pro&rams.
La.out Cuali'iers on member declarations cannot use the share#, pac&e#, or st#1:0 Cuali'iers. -hese can
onl. be used at &lobal scope or on a bloc8 declaration.
-he ro/+ma;or Cuali'ier overrides onl. the column+ma;or Cuali'ierM other Cuali'iers are inherited. $t onl.
a''ects the la.out o' matrices. Elements #ithin a matri2 ro# #ill be conti&uous in memor..
-he column+ma;or Cuali'ier overrides onl. the ro/+ma;or Cuali'ierM other Cuali'iers are inherited. $t onl.
a''ects the la.out o' matrices. Elements #ithin a matri2 column #ill be conti&uous in memor..
=hen multiple ar&uments are listed in a layout declaration, the e''ect #ill be the same as i' the. #ere
declared one at a time, in order 'rom le't to ri&ht, each in turn inheritin& 'rom and overridin& the result
'rom the previous Cuali'ication.
43
4 Variables and Types
La.out Cuali'iers are identi'iers, not 8e.#ords and the. have their o#n name space.
4or e2ample
la9out#ro._ma6or0 'olumn_ma6or)
results in the Cuali'ication bein& column+ma;or. Other e2amples:
la9out#s(ared0 ro._ma6or) uniform- && default is no. s(ared and ro._ma6or
la9out#std<7=) uniform ransform * && la9out of t(is $lo': is std<7=
mat7 @<- && ro._ma6or
la9out#'olumn_ma6or) mat7 @8- && 'olumn ma6or
mat; N<- && ro._ma6or
2-
uniform 8 * && la9out of t(is $lo': is s(ared
,,,
2-
la9out#'olumn_ma6or) uniform ; * && s(ared and 'olumn_ma6or
mat7 @;- && 'olumn_ma6or
la9out#ro._ma6or) mat7 m7- && ro. ma6or
mat; N8- && 'olumn_ma6or
2-
%.3.5 Interpolation
-he presence o' and t.pe o' interpolation is controlled b. the stora&e Cuali'iers Aentroid in and Aentroid
out, and b. the optional interpolation Cuali'iers smootB and flat. =hen no interpolation Cuali'ier is
present, smooth interpolation is used. $t is a compile;time error to use more than one interpolation
Cuali'ier.
variable Cuali'ied as flat #ill not be interpolated. $nstead, it #ill have the same value 'or ever.
'ra&ment #ithin a trian&le. -his value #ill come 'rom a sin&le provo8in& verte2, as described b. the
OpenGL ES Graphics S.stem Speci'ication. variable ma. be Cuali'ied as flat Aentroid, #hich #ill
mean the same thin& as Cuali'.in& it onl. as flat.
variable Cuali'ied as smootB #ill be interpolated in a perspective;correct manner over the primitive
bein& rendered. $nterpolation in a perspective correct manner is speci'ied in eCuations (.+ in the OpenGL
ES (.) Graphics S.stem Speci'ication, section (./ ILine Se&mentsJ.
-his para&raph onl. applies i' interpolation is bein& done: $' sin&le;samplin&, the value is interpolated to
the pi2elDs center, and the Aentroid Cuali'ier, i' present, is i&nored. $' multi;samplin& and the variable is
not Cuali'ied #ith Aentroid, then the value must be interpolated to the pi2elDs center, or an.#here #ithin
the pi2el, or to one o' the pi2elDs samples. $' multi;samplin& and the variable is Cuali'ied #ith Aentroid,
then the value must be interpolated to a point that lies in both the pi2el and in the primitive bein&
rendered, or to one o' the pi2elDs samples that 'alls #ithin the primitive. !ue to the less re&ular location o'
centroids, their derivatives ma. be less accurate than non;centroid interpolated variables.
44
4 Variables and Types
-he t.pe and presence o' the interpolation Cuali'iers and stora&e Cuali'iers and invariant Cuali'iers o'
variables #ith the same name declared in all lin8ed shaders must match, other#ise the lin8 command #ill
'ail.
%.3.10 Lin1ing of (erte) Outputs and +ragment Inputs
-he t.pe o' verte2 outputs and 'ra&ment input #ith the same name must match, other#ise the lin8
command #ill 'ail. -he precision does not need to match. Onl. those 'ra&ment inputs staticall. used ?i.e.
read@ in the 'ra&ment shader must be declared as outputs in the verte2 shaderM declarin& super'luous verte2
shader outputs is permissible.
-he 'ollo#in& table summariEes the rules 'or matchin& verte2 outputs #ith 'ra&ment inputs:
Lragment ;Bader Nnputs
:o re'erence !eclaresM
no static use
!eclares
and static use
Vertex
;Bader
Rutputs
:o re'erence llo#ed llo#ed error
!eclaresM
no static use
llo#ed llo#ed llo#ed
?values are
unde'ined@
!eclares
and static use
llo#ed llo#ed llo#ed
?values are
potentiall.
unde'ined@
-he term static use means that a'ter preprocessin& the shader includes at least one statement that accesses
the input or output, even i' that statement is never actuall. e2ecuted.
-he precision o' a verte2 output does not need to match the precision o' the correspondin& 'ra&ment input.
-he minimum precision at #hich verte2 outputs are interpolated is the minimum o' the verte2 output
precision and the 'ra&ment input precision, #ith the e2ception that 'or hi&hp, implementations do not have
to support 'ull ieee 0/+ precision. $n this case, the precision o' the interpolated value is de'ined b. a
ran&e and resolution as belo#:
-he precision o' values e2ported to a trans'orm 'eedbac8 bu''er is the precision o' the outputs o' the
verte2 shader.
45
4 Variables and Types
%.% *arameter ;ualifiers
3arameters can have these Cuali'iers.
;ualifier 9eaning
G none: de'ault H same is in
in 'or 'unction parameters passed into a 'unction
out 'or 'unction parameters passed bac8 out o' a 'unction, but not initialiEed
'or use #hen passed in
inout 'or 'unction parameters passed both into and out o' a 'unction
3arameter Cuali'iers are discussed in more detail in section 7.1.1 I4unction %allin& %onventionsJ.
%.' *recision and *recision ;ualifiers
%.'.1 :ange and *recision
-he precision o' hi&hp 'loatin&;point variables is de'ined b. the $EEE 0/+ standard 'or (*;bit 'loatin&;
point numbers. -his includes support 'or :a:s ?:ot a :umber@ and $n's ?positive or ne&ative in'inities@.
-he 'ollo#in& rules appl. to hi&hp operations: $n'inities and Eeros are &enerated as dictated b. $EEE,
but subFect to the precisions allo#ed in the 'ollo#in& table and subFect to allo#in& positive and ne&ative
Eeros to be interchan&ed. ,o#ever, dividin& a non;Eero b. ) results in the appropriatel. si&ned $EEE $n':
$' both positive and ne&ative Eeros are implemented, the correctl. si&ned $n' #ill be &enerated, other#ise
positive $n' is &enerated. n. subnormal ?denormaliEed@ value input into a shader or potentiall.
&enerated b. an. operation in a shader can be 'lushed to ). -he roundin& mode cannot be set and is
unde'ined. :a:s are not reCuired to be &enerated. Support 'or si&nalin& :a:s is not reCuired and
e2ceptions are never raised. Operations and built;in 'unctions that operate on a :a: are not reCuired to
return a :a: as the result. ,o#ever i' :a:s are &enerated, isnan?@ should return the correct value.
3recisions are e2pressed in terms o' ma2imum relative error in units o' 9L3 ?units in the last place@,
unless other#ise noted.
46
4 Variables and Types
4or sin&le precision operations, precisions are reCuired as 'ollo#s:
Rperation SreAision
a O ), a ^ ), a R ) %orrectl. rounded.
G, GT, TT, H, HT %orrect result.
a > ), 1.) > ) *./ 9L3 'or ) in the ran&e Z*
;1*7
, *
1*7
[.
a R ) O c %orrectl. rounded sin&le operation or seCuence o'
t#o correctl. rounded operations.
po??(, y@ $nherited 'rom expD ?( < logD ?y@@.
exp ?(@, expD ?(@ ?( O * R W(W@ 9L3.
log ?@, logD ?@ ( 9L3 outside the ran&e Z)./, *.)[.
bsolute error G *
;*1
inside the ran&e Z)./, *.)[.
sJrt ?@ $nherited 'rom 1.) > inversesJrt?@.
inversesJrt ?@ * 9L3.
e2plicit
conversions bet#een t.pes
%orrectl. rounded.
-he roundin& mode is not de'ined but must not a''ect the result b. more than 1 9L3.
"uilt;in 'unctions de'ined in the speci'ication #ith an eCuation built 'rom the above operations inherit the
above errors. -hese include, 'or e2ample, the &eometric 'unctions, the common 'unctions, and man. o'
the matri2 'unctions. "uilt;in 'unctions not listed above and not de'ined as eCuations o' the above have
unde'ined precision. -hese include, 'or e2ample, the tri&onometric 'unctions and determinant.
Stora&e reCuirements are declared throu&h use o' precision 2uali*iers. -he precision o' operations must
preserve the stora&e precisions o' the variables involved.
hi&hp 'loatin& point values are stored in $EEE 0/+ sin&le precision 'loatin& point 'ormat. <ediump and
lo#p 'loatin& point values have minimum ran&e and precision reCuirements as detailed belo# and have
ma2imum ran&e and precision as de'ined b. $EEE 0/+.
ll inte&er t.pes are assumed to be implemented as inte&ers and so ma. not be emulated b. 'loatin& point
values. ,i&hp si&ned inte&ers are represented as t#os;complement (*;bit si&ned inte&ers. ,i&hp
unsi&ned inte&ers are represented as unsi&ned (*;bit inte&ers. <ediump inte&ers ?si&ned and unsi&ned@
must be represented as an inte&er #ith bet#een 17 and (* bits inclusive. Lo#p inte&ers ?si&ned and
unsi&ned@ must be represented as an inte&er #ith bet#een 6 and (* bits inclusive.
47
4 Variables and Types
-he reCuired ran&es and precisions 'or precision Cuali'iers are:
;ualifier +loating
*oint :ange
+loating *oint
9agnitude
:ange
+loating *oint
*recision
Integer
:ange
Signed 6nsigned
BigBp s $EEE;0/+
(*
1*7
$ *
1*0
)
s $EEE;0/+
).),(*
1*7
$ *
1*0
)
s $EEE 0/+
relative:
*
*+
[*
(1
$ *
(1
1] [) $ *
(*
1]
mediump
+minimum
reJuirements,
(*
1+
$ *
1+
) (*
1+
$ *
1+
)
Relative:
*
1)
[*
1/
$ *
1/
1] [) $ *
17
1]
lo?p
+minimum
reJuirements,
(*, *) (*
5
$ *)
bsolute:
*
5
> *
6
si&ned>unsi&ned
[*
5
$ *
5
1] [) $ *
6
1]
$n addition, the ran&e and precision o' a mediump 'loatin& point value must be the same as or &reater than
the ran&e and precision o' a lo#p 'loatin& point value. -he ran&e and precision o' a hi&hp 'loatin& point
value must be the same as or &reater than the ran&e and precision o' a mediump 'loatin& point value.
-he ran&e o' a mediump inte&er value must be the same as or &reater than the ran&e o' a lo#p inte&er
value. -he ran&e o' a hi&hp inte&er value must be the same as or &reater than the ran&e o' a mediump
inte&er value.
=ithin the above speci'ication, an implementation is allo#ed to var. the representation o' numeric values,
both #ithin a shader and bet#een di''erent shaders. $' necessar., this variance can be controlled usin& the
invariance Cuali'ier.
-he actual ran&es and precisions provided b. an implementation can be Cueried throu&h the 3$. See the
OpenGL ES (.) speci'ication 'or details on ho# to do this.
%.'.! Conersion bet#een precisions
=ithin the same t.pe, conversion 'rom a lo#er to a hi&her precision must be e2act. =hen convertin&
'rom a hi&her precision to a lo#er precision, i' the value is representable b. the implementation o' the
tar&et precision, the conversion must also be e2act. $' the value is not representable, the behavior is
dependent on the t.pe:
4or si&ned and unsi&ned inte&ers, the value is truncatedM bits in positions not present in the tar&et
precision are set to Eero. ?3ositions start at Eero and the least si&ni'icant bit is considered to be
position Eero 'or this purpose.@
4or 'loatin& point values, the value should either clamp to O$:4 or ;$:4, or to the ma2imum or
minimum value that the implementation supports. =hile this behavior is implementation
dependent, it should be consistent 'or a &iven implementation.
48
4 Variables and Types
%.'.3 *recision ;ualifiers
n. 'loatin& point or an. inte&er declaration can have the t.pe preceded b. one o' these precision
Cuali'iers:
;ualifier 9eaning
BigBp -he variable satis'ies the minimum reCuirements 'or hi&hp described above.
,i&hp variables have the ma2imum ran&e and precision available but ma.
cause operations to run more slo#l. on some implementations.
mediump -he variable satis'ies the minimum reCuirements 'or mediump described
above. <ediump variables ma. t.picall. be used to store hi&h d.namic
ran&e colors and lo# precision &eometr..
lo?p -he variable satis'ies the minimum reCuirements 'or lo#p described above.
Lo#p variables ma. t.picall. be used to store 5;bit color values.
4or e2ample:
lo.p float 'olor-
out mediump ve'8 C-
lo.p ive'8 foo#lo.p mat;)-
(ig(p mat7 m-
Literal constants do not have precision Cuali'iers. :either do "oolean variables. :either do 'loatin&
point constructors nor inte&er constructors #hen none o' the constructor ar&uments have precision
Cuali'iers.
4or this para&raph, IoperationJ includes operators, built;in 'unctions, and constructors, and IoperandJ
includes 'unction ar&uments and constructor ar&uments. -he precision used to internall. evaluate an
operation, and the precision Cuali'ication subseCuentl. associated #ith an. resultin& intermediate values,
must be at least as hi&h as the hi&hest precision Cuali'ication o' the operands consumed b. the operation.
4or constant e2pressions and sub;e2pressions, #here the precision is not de'ined, the evaluation is
per'ormed at or above the hi&hest supported precision o' the tar&et ?either mediump or hi&hp@. -he
evaluation o' constant e2pressions must be invariant and #ill usuall. be per'ormed at compile time.
$n other cases #here operands do not have a precision Cuali'ier, the precision Cuali'ication #ill come 'rom
the other operands. $' no operands have a precision Cuali'ier, then the precision Cuali'ications o' the
operands o' the ne2t consumin& operation in the e2pression #ill be used. -his rule can be applied
recursivel. until a precision Cuali'ied operand is 'ound. $' necessar., it #ill also include the precision
Cuali'ication o' l;values 'or assi&nments, o' the declared variable 'or initialiEers, o' 'ormal parameters 'or
'unction call ar&uments, or o' 'unction return t.pes 'or 'unction return values. $' the precision cannot be
determined b. this method e.&. i' an entire e2pression is composed onl. o' operands #ith no precision
Cuali'ier, and the result is not assi&ned or passed as an ar&ument, then it is evaluated at the de'ault
precision o' the t.pe or &reater. =hen this occurs in the 'ra&ment shader, the de'ault precision must be
de'ined.
49
4 Variables and Types
4or e2ample, consider the statements.
uniform (ig(p float (<-
(ig(p float (8 + 8,; E 7,F- && operation and result are (ig(p pre'ision
mediump float m-
m + ;,F E (< E (8- && all operations are (ig(p pre'ision
(8 + m E (<- && operation is (ig(p pre'ision
m + (8 I (<- && operation is (ig(p pre'ision
(8 + m D m- && addition and result at mediump pre'ision
void f#(ig(p float p)-
f#;,;)- && ;,; .ill $e passed in at (ig(p pre'ision
3recision Cuali'iers, as #ith other Cuali'iers, do not a''ect the basic t.pe o' the variable. $n particular,
there are no constructors 'or precision conversionsM constructors onl. convert t.pes. Similarl., precision
Cuali'iers, as #ith other Cuali'iers, do not contribute to 'unction overloadin& based on parameter t.pes.
s discussed in the ne2t chapter, 'unction input and output is done throu&h copies, and there'ore Cuali'iers
do not have to match.
-he same uni'orm declared in di''erent shaders that are lin8ed to&ether must have the same precision
Cuali'ication.
-he precision o' a variable is determined #hen the variable is declared and cannot be subseCuentl.
chan&ed.
%.'.% -efault *recision ;ualifiers
-he precision statement
pre'ision pre'ision1)ualifier t9pe-
can be used to establish a de'ault precision Cuali'ier. -he type 'ield can be either int or float or an. o'
the sampler t.pes, and the precision.2uali*ier can be lo?p, mediump, or BigBp. n. other t.pes or
Cuali'iers #ill result in an error. $' type is float, the directive applies to non;precision;Cuali'ied 'loatin&
point t.pe ?scalar, vector, and matri2@ declarations. $' type is int, the directive applies to all non;
precision;Cuali'ied inte&er t.pe ?scalar, vector, si&ned, and unsi&ned@ declarations. -his includes &lobal
variable declarations, 'unction return declarations, 'unction parameter declarations, and local variable
declarations.
:on;precision Cuali'ied declarations #ill use the precision Cuali'ier speci'ied in the most recent preAision
statement that is still in scope. -he preAision statement has the same scopin& rules as variable
declarations. $' it is declared inside a compound statement, its e''ect stops at the end o' the innermost
statement it #as declared in. 3recision statements in nested scopes override precision statements in outer
scopes. <ultiple precision statements 'or the same basic t.pe can appear inside the same scope, #ith later
statements overridin& earlier statements #ithin that scope.
50
4 Variables and Types
-he verte2 lan&ua&e has the 'ollo#in& predeclared &loball. scoped de'ault precision statements:
pre'ision (ig(p float-
pre'ision (ig(p int-
pre'ision lo.p sampler8!-
pre'ision lo.p samplerHu$e-
-he 'ra&ment lan&ua&e has the 'ollo#in& predeclared &loball. scoped de'ault precision statements:
pre'ision mediump int-
pre'ision lo.p sampler8!-
pre'ision lo.p samplerHu$e-
-he 'ra&ment lan&ua&e has no de'ault precision Cuali'ier 'or 'loatin& point t.pes. ,ence 'or float, 'loatin&
point vector and matri2 variable declarations, either the declaration must include a precision Cuali'ier or
the de'ault 'loat precision must have been previousl. declared. Similarl., there is no de'ault precision
Cuali'ier 'or the 'ollo#in& sampler t.pes in either the verte2 or 'ra&ment lan&ua&e:
sampler;!-
samplerHu$e-
samplerHu$eS(ado.-
sampler8!S(ado.-
sampler8!?rra9-
sampler8!?rra9S(ado.-
isampler8!-
isampler;!-
isamplerHu$e-
isampler8!?rra9-
usampler8!-
usampler;!-
usamplerHu$e-
usampler8!?rra9-
%.0 (ariance and the Inariant ;ualifier
$n this section, variance re'ers to the possibilit. o' &ettin& di''erent values 'rom the same e2pression in
di''erent pro&rams. 4or e2ample, sa. t#o verte2 shaders, in di''erent pro&rams, each set gl+'osition #ith
the same e2pression in both shaders, and the input values into that e2pression are the same #hen both
shaders run. $t is possible, due to independent compilation o' the t#o shaders, that the values assi&ned to
gl+'osition are not e2actl. the same #hen the t#o shaders run. $n this e2ample, this can cause problems
#ith ali&nment o' &eometr. in a multi;pass al&orithm.
$n &eneral, such variance bet#een shaders is allo#ed. =hen such variance does not e2ist 'or a particular
output variable, that variable is said to be invariant.
51
4 Variables and Types
%.0.1 The Inariant ;ualifier
-o ensure that a particular output variable is invariant, it is necessar. to use the invariant Cuali'ier. $t can
either be used to Cuali'. a previousl. declared variable as bein& invariant
invariant gl_Cosition- && ma:e $uilt1in gl_Cosition $e invariant
out ve'; Holor-
invariant Holor- && ma:e existing Holor $e invariant
invariant Holor_8- && error% Holor_8 (as not $een de'lared
or as part o' a declaration #hen a variable is declared
invariant 'entroid out ve'; Holor-
-he invariant Cuali'ier must appear be'ore an. interpolation Cuali'iers or stora&e Cuali'iers #hen
combined #ith a declaration. Onl. variables output 'rom a shader can be candidates 'or invariance. -his
includes user;de'ined output variables and the built;in output variables. s onl. outputs can be declared
as invariant, an invariant output 'rom one shader sta&e #ill still match an input o' a subseCuent sta&e
#ithout the input bein& declared as invariant.
-he invariant 8e.#ord can be 'ollo#ed b. a comma separated list o' previousl. declared identi'iers. ll
uses o' invariant must be at the &lobal scope, and be'ore an. use o' the variables bein& declared as
invariant.
-o &uarantee invariance o' a particular output variable across t#o pro&rams, the 'ollo#in& must also be
true:
-he output variable is declared as invariant in both pro&rams.
-he same values must be input to all shader input variables consumed b. e2pressions and control 'lo#
contributin& to the value assi&ned to the output variable.
-he te2ture 'ormats, te2el values, and te2ture 'ilterin& are set the same #a. 'or an. te2ture 'unction
calls contributin& to the value o' the output variable.
ll input values are all operated on in the same #a.. ll operations in the consumin& e2pressions and
an. intermediate e2pressions must be the same, #ith the same order o' operands and same
associativit., to &ive the same order o' evaluation. $ntermediate variables and 'unctions must be
declared as the same t.pe #ith the same e2plicit or implicit precision Cuali'iers. n. control 'lo#
a''ectin& the output value must be the same, and an. e2pressions consumed to determine this control
'lo# must also 'ollo# these invariance rules.
ll the data 'lo# and control 'lo# leadin& to settin& the invariant output variable reside in a sin&le
compilation unit.
Essentiall., all the data 'lo# and control 'lo# leadin& to an invariant output must match.
52
4 Variables and Types
$nitiall., b. de'ault, all output variables are allo#ed to be variant. -o 'orce all output variables to be
invariant, use the pra&ma
#pragma S!GL invariant#all)
be'ore all declarations in a shader. $' this pra&ma is used a'ter the declaration o' an. variables or
'unctions, then the set o' outputs that behave as invariant is unde'ined. $t is an error to use this pra&ma in
a 'ra&ment shader.
Generall., invariance is ensured at the cost o' 'le2ibilit. in optimiEation, so per'ormance can be de&raded
b. use o' invariance. ,ence, use o' this pra&ma is intended as a debu& aid, to avoid individuall. declarin&
all output variables as invariant.
%.0.! Inariance <ithin a Shader
=hen a value is stored in a variable, it is usuall. assumed it #ill remain constant unless e2plicitl.
chan&ed. ,o#ever, durin& the process o' optimiEation, it is possible that the compiler ma. choose to
recompute a value rather than store it in a re&ister. Since the precision o' operations is not completel.
speci'ied ?e.&. a lo# precision operation ma. be done at medium or hi&h precision@, it #ould be possible
'or the recomputed value to be di''erent 'rom the ori&inal value.
1alues are allo#ed to be variant #ithin a shader. -o prevent this, the invariant Cuali'ier or invariant
pra&ma must be used.
=ithin a shader, there is no invariance 'or values &enerated b. di''erent non;constant e2pressions, even i'
those e2pressions are identical.
E2ample 1:
pre'ision mediump-
ve'7 'ol-
ve'8 a + ,,,
,,,
'ol + texture#tex0 a)- && a (as a value a1
,,,
'ol + texture#tex0 a)- && a (as a value a2 .(ere possi$l9 a1 J a2
-o en'orce invariance in this e2ample use:
#pragma S!GL invariant#all)
E2ample *:
ve'8 m + ,,,-
ve'8 n + ,,,-
ve'8 a + m D n-
ve'8 $ + m D n- && a and $ are not guaranteed to $e exa'tl9 e)ual
-here is no mechanism to en'orce invariance bet#een a and b.
53
4 Variables and Types
%.0.3 Inariance of Constant E)pressions
$nvariance must be &uaranteed 'or constant e2pressions. particular constant e2pression must evaluate to
the same result i' it appears a&ain in the same shader or a di''erent shader. -his includes the same
e2pression appearin& t#o shaders o' the same lan&ua&e or shaders o' t#o di''erent lan&ua&es.
%onstant e2pressions must evaluate to the same result #hen operated on as alread. described above 'or
invariant variables.
%.0.% Inariance of 6ndefined (alues
9nde'ined values are not invariant nor can the. be made invariant b. use o' the invariant Cuali'ier or
pra&ma. $n some implementations, unde'ined values ma. cause une2pected behavior i' the. are used in
control;'lo# e2pressions e.&. in the 'ollo#in& case, one, both or neither 'unctions ma. be e2ecuted and
this ma. not be consistent over multiple invocations o' the shader:
int x- && undefined value
if #x ++ <)
*
f#)- && Kndefined .(et(er f#) is exe'uted
2
if #x ++ 8)
*
g#)- && Kndefined .(et(er g#) is exe'uted,
2
:ote that an unde'ined value is a value that has not been speci'ied. value that has been speci'ied but
has a potentiall. lar&e error due to, 'or e2ample, lac8 o' precision in an e2pression, is not unde'ined and
so can be made invariant.
%.2 Order of ;ualification
=hen multiple Cuali'ications are present, the. must 'ollo# a strict order. -his order is as 'ollo#s.
invariant.2uali*ier interpolation.2uali*ier storage.2uali*ier precision.2uali*ier
storage.2uali*ier parameter.2uali*ier precision.2uali*ier
54
' Operators and E)pressions
'.1 Operators
-he OpenGL ES Shadin& Lan&ua&e has the 'ollo#in& operators.
*recedence Operator Class Operators .ssociatiit&
1 ?hi&hest@ parenthetical &roupin& + , :
*
arra. subscript
'unction call and constructor structure
'ield or method selector, s#iEEler
post 'i2 increment and decrement
) *
+ ,

## --
Le't to Ri&ht
(
pre'i2 increment and decrement
unar.
## --
# - 2 4
Ri&ht to Le't
+ multiplicative % $ & Le't to Ri&ht
/ additive # - Le't to Ri&ht
7 bit;#ise shi't '' (( Le't to Ri&ht
0 relational ' ( '3 (3 Le't to Ri&ht
5 eCualit. 33 43 Le't to Ri&ht
6 bit;#ise and 1 Le't to Ri&ht
1) bit;#ise e2clusive or / Le't to Ri&ht
11 bit;#ise inclusive or 0 Le't to Ri&ht
1* lo&ical and 11 Le't to Ri&ht
1( lo&ical e2clusive or // Le't to Ri&ht
1+ lo&ical inclusive or 0 0 Le't to Ri&ht
1/ selection 7 5 Ri&ht to Le't
17
ssi&nment
arithmetic assi&nments
3
#3 -3
%3 $3
&3 ''3 ((3
13 /3 03
Ri&ht to Le't
10 ?lo#est@ seCuence , Le't to Ri&ht
-here is no address;o' operator nor a dere'erence operator. -here is no t.pecast operatorM constructors
are used instead.
55
5 Operators and !pressions
'.! .rra& Operations
-hese are no# described in section /.0 IStructure and rra. OperationsJ.
'.3 +unction Calls
$' a 'unction returns a value, then a call to that 'unction ma. be used as an e2pression, #hose t.pe #ill be
the t.pe that #as used to declare or de'ine the 'unction.
4unction de'initions and callin& conventions are discussed in section 7.1 I4unction !e'initionsJ .
'.% Constructors
%onstructors use the 'unction call s.nta2, #here the 'unction name is a t.pe, and the call ma8es an obFect
o' that t.pe. %onstructors are used the same #a. in both initialiEers and e2pressions. ?See section 6
IShadin& Lan&ua&e GrammarJ 'or details.@ -he parameters are used to initialiEe the constructed value.
%onstructors can be used to reCuest a data t.pe conversion to chan&e 'rom one scalar t.pe to another
scalar t.pe, or to build lar&er t.pes out o' smaller t.pes, or to reduce a lar&er t.pe to a smaller t.pe.
$n &eneral, constructors are not built;in 'unctions #ith predetermined protot.pes. 4or arra.s and
structures, there must be e2actl. one ar&ument in the constructor 'or each element or 'ield. 4or the other
t.pes, the ar&uments must provide a su''icient number o' components to per'orm the initialiEation, and it
is an error to include so man. ar&uments that the. cannot all be used. !etailed rules 'ollo#. -he
protot.pes actuall. listed belo# are merel. a subset o' e2amples.
'.%.1 Conersion and Scalar Constructors
%onvertin& bet#een scalar t.pes is done as the 'ollo#in& protot.pes indicate:
int#$ool) && 'onverts a 5oolean value to an int
int#float) && 'onverts a float value to an int
float#$ool) && 'onverts a 5oolean value to a float
float#int) && 'onverts a signed integer value to a float
$ool#float) && 'onverts a float value to a 5oolean
$ool#int) && 'onverts a signed integer value to a 5oolean
uint#$ool) && 'onverts a 5oolean value to an unsigned integer
uint#float) && 'onverts a float value to an unsigned integer
uint#int) && 'onverts a signed integer value to an unsigned integer
int#uint) && 'onverts an unsigned integer to a signed integer
$ool#uint) && 'onverts an unsigned integer value to a 5oolean value
float#uint) && 'onverts an unsigned integer value to a float value
=hen constructors are used to convert a float to an int or uint, the 'ractional part o' the 'loatin&;point
value is dropped. $t is unde'ined to convert a ne&ative 'loatin& point value to an uint.
=hen a constructor is used to convert an int, uint, or a float to a @ool, ) and ).) are converted to false,
and non;Eero values are converted to true. =hen a constructor is used to convert a @ool to an int, uint,
or float, false is converted to ) or ).), and true is converted to 1 or 1.).
56
5 Operators and !pressions
-he constructor int+uint, preserves the bit pattern in the ar&ument, #hich #ill chan&e the ar&umentDs
value i' its si&n bit is set. -he constructor uint+int, preserves the bit pattern in the ar&ument, #hich #ill
chan&e its value i' it is ne&ative.
$dentit. constructors, li8e float?float@ are also le&al, but o' little use.
Scalar constructors #ith non;scalar parameters can be used to ta8e the 'irst element 'rom a non;scalar.
4or e2ample, the constructor float?veA8@ #ill select the 'irst component o' the veA8 parameter.
'.%.! (ector and 9atri) Constructors
%onstructors can be used to create vectors or matrices 'rom a set o' scalars, vectors, or matrices. -his
includes the abilit. to shorten vectors.
$' there is a sin&le scalar parameter to a vector constructor, it is used to initialiEe all components o' the
constructed vector to that scalarLs value. $' there is a sin&le scalar parameter to a matri2 constructor, it is
used to initialiEe all the components on the matri2Ls dia&onal, #ith the remainin& components initialiEed
to ).).
$' a vector is constructed 'rom multiple scalars, one or more vectors, or one or more matrices, or a mi2ture
o' these, the vectorDs components #ill be constructed in order 'rom the components o' the ar&uments. -he
ar&uments #ill be consumed le't to ri&ht, and each ar&ument #ill have all its components consumed, in
order, be'ore an. components 'rom the ne2t ar&ument are consumed. Similarl. 'or constructin& a matri2
'rom multiple scalars or vectors, or a mi2ture o' these. <atri2 components #ill be constructed and
consumed in column maFor order. $n these cases, there must be enou&h components provided in the
ar&uments to provide an initialiEer 'or ever. component in the constructed value. $t is an error to provide
e2tra ar&uments be.ond this last used ar&ument.
$' a matri2 is constructed 'rom a matri2, then each component ?column i$ ro# ;@ in the result that has a
correspondin& component ?column i$ ro# ;@ in the ar&ument #ill be initialiEed 'rom there. ll other
components #ill be initialiEed to the identit. matri2. $' a matri2 ar&ument is &iven to a matri2 constructor,
it is an error to have an. other ar&uments.
$' the basic t.pe ?@ool, int, or float@ o' a parameter to a constructor does not match the basic t.pe o' the
obFect bein& constructed, the scalar construction rules ?above@ are used to convert the parameters.
57
5 Operators and !pressions
Some use'ul vector constructors are as 'ollo#s:
ve';#float) && initiali"es ea'( 'omponent of t(e ve'; .it( t(e float
ve'7#ive'7) && ma:es a ve'7 .it( 'omponent1.ise 'onversion
ve'7#mat8) && t(e ve'7 is 'olumn = follo.ed $9 'olumn <
ve'8#float0 float) && initiali"es a ve'8 .it( 8 floats
ive';#int0 int0 int) && initiali"es an ive'; .it( ; ints
$ve'7#int0 int0 float0 float) && uses 7 5oolean 'onversions
ve'8#ve';) && drops t(e t(ird 'omponent of a ve';
ve';#ve'7) && drops t(e fourt( 'omponent of a ve'7
ve';#ve'80 float) && ve';,x + ve'8,x0 ve';,9 + ve'8,90 ve';," + float
ve';#float0 ve'8) && ve';,x + float0 ve';,9 + ve'8,x0 ve';," + ve'8,9
ve'7#ve';0 float)
ve'7#float0 ve';)
ve'7#ve'80 ve'8)
Some e2amples o' these are:
ve'7 'olor + ve'7#=,=0 <,=0 =,=0 <,=)-
ve'7 rg$a + ve'7#<,=)- && sets ea'( 'omponent to <,=
ve'; rg$ + ve';#'olor)- && drop t(e 7t( 'omponent
-o initialiEe the dia&onal o' a matri2 #ith all other elements set to Eero:
mat8#float)
mat;#float)
mat7#float)
-hat is, result7i87;8 is set to the 'loat ar&ument 'or all i = ; and set to ) 'or all i ;.
58
5 Operators and !pressions
-o initialiEe a matri2 b. speci'.in& vectors or scalars, the components are assi&ned to the matri2 elements
in column;maFor order.
mat8#ve'80 ve'8)- && one 'olumn per argument
mat;#ve';0 ve';0 ve';)- && one 'olumn per argument
mat7#ve'70 ve'70 ve'70 ve'7)- && one 'olumn per argument
mat;x8#ve'80 ve'80 ve'8)- && one 'olumn per argument
mat8#float0 float0 && first 'olumn
float0 float)- && se'ond 'olumn
mat;#float0 float0 float0 && first 'olumn
float0 float0 float0 && se'ond 'olumn
float0 float0 float)- && t(ird 'olumn
mat7#float0 float0 float0 float0 && first 'olumn
float0 float0 float0 float0 && se'ond 'olumn
float0 float0 float0 float0 && t(ird 'olumn
float0 float0 float0 float)- && fourt( 'olumn
mat8x;#ve'80 float0 && first 'olumn
ve'80 float)- && se'ond 'olumn
#ide ran&e o' other possibilities e2ist, to construct a matri2 'rom vectors and scalars, as lon& as enou&h
components are present to initialiEe the matri2. -o construct a matri2 'rom a matri2:
mat;x;#mat7x7)- && ta:es t(e upper1left ;x; of t(e mat7x7
mat8x;#mat7x8)- && ta:es t(e upper1left 8x8 of t(e mat7x70 last ro. is =0=
mat7x7#mat;x;)- && puts t(e mat;x; in t(e upper1left0 sets t(e lo.er rig(t
&& 'omponent to <0 and t(e rest to =
'.%.3 Structure Constructors
Once a structure is de'ined, and its t.pe is &iven a name, a constructor is available #ith the same name to
construct instances o' that structure. 4or e2ample:
stru't lig(t *
float intensit9-
ve'; position-
2-
lig(t lig(tVar + lig(t#;,=0 ve';#<,=0 8,=0 ;,=))-
-he ar&uments to the constructor #ill be used to set the structureDs 'ields, in order, usin& one ar&ument per
'ield. Each ar&ument must be the same t.pe as the 'ield it sets.
Structure constructors can be used as initialiEers or in e2pressions.
59
5 Operators and !pressions
'.%.% .rra& Constructors
rra. t.pes can also be used as constructor names, #hich can then be used in e2pressions or initialiEers.
4or e2ample,
'onst float 'A;B + floatA;B#>,=0 F,80 <,<)-
'onst float dA;B + floatAB#>,=0 F,80 <,<)-
float g-
,,,
float aA>B + floatA>B#g0 <0 g0 8,;0 g)-
float $A;B-
$ + floatA;B#g0 g D <,=0 g D 8,=)-
-here must be e2actl. the same number o' ar&uments as the siEe o' the arra. bein& constructed. -he
ar&uments are assi&ned in order, startin& at element ), to the elements o' the constructed arra.. Each
ar&ument must be the same t.pe as the element t.pe o' the arra..
'.' (ector Components
-he names o' the components o' a vector are denoted b. a sin&le letter. s a notational convenience,
several letters are associated #ith each component based on common usa&e o' position, color or te2ture
coordinate vectors. -he individual components o' a vector can be selected b. 'ollo#in& the variable
name #ith period ? @ and then the component name.
-he component names supported are:
>($ y$ 1$ /? 9se'ul #hen accessin& vectors that represent points or normals
>r$ g$ )$ a? 9se'ul #hen accessin& vectors that represent colors
>s$ t$ p$ 2? 9se'ul #hen accessin& vectors that represent te2ture coordinates
-he component names ($ r$ and s are, 'or e2ample, s.non.ms 'or the same ?'irst@ component in a vector.
:ote that the third component o' the te2ture coordinate set, r in OpenGL ES, has been renamed p so as to
avoid the con'usion #ith r ?'or red@ in a color.
ccessin& components be.ond those declared 'or the vector t.pe is an error so, 'or e2ample:
ve'8 pos-
pos,x && is legal
pos," && is illegal
60
5 Operators and !pressions
-he component selection s.nta2 allo#s multiple components to be selected b. appendin& their names
?'rom the same name set@ a'ter the period ? @.
ve'7 v7-
v7,rg$a- && is a ve'7 and t(e same as 6ust using v70
v7,rg$- && is a ve';0
v7,$- && is a float0
v7,x9- && is a ve'80
v7,xg$a- && is illegal 1 t(e 'omponent names do not 'ome from
&& t(e same set,
:o more than + components can be selected.
ve'7 v7-
v7,x9".- && is a ve'7
v7,x9".x9- && is illegal sin'e it (as L 'omponents
#v7,x9".x9),x9- && is illegal sin'e t(e intermediate value (as L 'omponents
ve'8 v8-
v8,x9x9- && is legal, It evaluates to a ve'7,
-he order o' the components can be di''erent to s#iEEle them, or replicated:
ve'7 pos + ve'7#<,=0 8,=0 ;,=0 7,=)-
ve'7 s.i"+ pos,."9x- && s.i" + #7,=0 ;,=0 8,=0 <,=)
ve'7 dup + pos,xx99- && dup + #<,=0 <,=0 8,=0 8,=)
-his notation is more concise than the constructor s.nta2. -o 'orm an r;value, it can be applied to an.
e2pression that results in a vector r;value.
-he component &roup notation can occur on the le't hand side o' an e2pression.
ve'7 pos + ve'7#<,=0 8,=0 ;,=0 7,=)-
pos,x. + ve'8#>,=0 L,=)- && pos + #>,=0 8,=0 ;,=0 L,=)
pos,.x + ve'8#F,=0 M,=)- && pos + #M,=0 8,=0 ;,=0 F,=)
pos,xx + ve'8#;,=0 7,=)- && illegal 1 GxG used t.i'e
pos,x9 + ve';#<,=0 8,=0 ;,=)- && illegal 1 mismat'( $et.een ve'8 and ve';
-o 'orm an l;value, s#iEElin& must be applied to an l;value o' vector t.pe, contain no duplicate
components, and it results in an l;value o' scalar or vector t.pe, dependin& on number o' components
speci'ied.
rra. subscriptin& s.nta2 can also be applied to vectors to provide numeric inde2in&. So in
ve'7 pos-
pos728 re'ers to the third element o' pos and is eCuivalent to pos.1. -his allo#s variable inde2in& into a
vector, as #ell as a &eneric #a. o' accessin& components. n. inte&er e2pression can be used as the
subscript. -he 'irst component is at inde2 Eero. Readin& 'rom or #ritin& to a vector usin& a constant
inte&ral e2pression #ith a value that is ne&ative or &reater than or eCual to the siEe o' the vector is ille&al.
=hen inde2in& #ith non;constant e2pressions, behavior is unde'ined i' the inde2 is ne&ative, or &reater
than or eCual to the siEe o' the vector.
61
5 Operators and !pressions
:ote that scalars are not considered to be sin&le;component vectors and there'ore the use o' component
selection operators on scalars is ille&al.
'.0 9atri) Components
-he components o' a matri2 can be accessed usin& arra. subscriptin& s.nta2. ppl.in& a sin&le subscript
to a matri2 treats the matri2 as an arra. o' column vectors, and selects a sin&le column, #hose t.pe is a
vector o' the same siEe as the ?column siEe o' the@ matri2. -he le'tmost column is column ). second
subscript #ould then operate on the resultin& vector, as de'ined earlier 'or vectors. ,ence, t#o subscripts
select a column and then a ro#.
mat7 m-
mA<B + ve'7#8,=)- && sets t(e se'ond 'olumn to all 8,=
mA=BA=B + <,=- && sets t(e upper left element to <,=
mA8BA;B + 8,=- && sets t(e 7t( element of t(e t(ird 'olumn to 8,=
"ehavior is unde'ined #hen accessin& a component outside the bounds o' a matri2 #ith a non;constant
e2pression. $t is an error to access a matri2 #ith a constant e2pression that is outside the bounds o' the
matri2.
'.2 Structure and .rra& Operations
-he 'ields o' a structure and the lengtB method o' an arra. are selected usin& the period ? @.
$n total, onl. the 'ollo#in& operators are allo#ed to operate on arra.s and structures as #hole entities:
'ield or method selector
eCualit. 33 43
assi&nment 3
inde2in& ?arra.s onl.@ Z [
-he eCualit. operators and assi&nment operator are onl. allo#ed i' the t#o operands are same siEe and
t.pe. Structure t.pes must be o' the same declared structure. =hen usin& the eCualit. operators, t#o
structures are eCual i' and onl. i' all the 'ields are component;#ise eCual, and t#o arra.s are eCual i' and
onl. i' all the elements are element;#ise eCual.
rra. elements are accessed usin& the arra. subscript operator ? ) * @. n e2ample o' accessin& an arra.
element is
diffuseHolor D+ lig(tIntensit9A;B E NdotL-
rra. indices start at Eero. rra. elements are accessed usin& an e2pression #hose t.pe is int or uint.
"ehavior is unde'ined i' a shader subscripts an arra. #ith an inde2 less than ) or &reater than or eCual to
the siEe the arra. #as declared #ith.
62
5 Operators and !pressions
rra.s can also be accessed #ith the method operator ? @ and the lengtB method to Cuer. the siEe o' the
arra.:
lig(tIntensit9,lengt(#) && return t(e si"e of t(e arra9
'.4 .ssignments
ssi&nments o' values to variable names are done #ith the assi&nment operator ? 3 @:
lvalue1expression + rvalue1expression
-he lvalue.e(pression evaluates to an l;value. -he assi&nment operator stores the value o' rvalue.
e(pression into the l;value and returns an r;value #ith the t.pe and precision o' lvalue.e(pression. -he
lvalue.e(pression and rvalue.e(pression must have the same t.pe. n. t.pe;conversions must be
speci'ied e2plicitl. via constructors. L;values must be #ritable. 1ariables that are built;in t.pes, entire
structures or arra.s, structure 'ields, l;values #ith the 'ield selector ? @ applied to select components or
s#iEEles #ithout repeated 'ields, l;values #ithin parentheses, and l;values dere'erenced #ith the arra.
subscript operator ? ) * @ are all l;values. Other binar. or unar. e2pressions, 'unction names, s#iEEles #ith
repeated 'ields, and constants cannot be l;values. -he ternar. operator ?75@ is also not allo#ed as an l;
value.
E2pressions on the le't o' an assi&nment are evaluated be'ore e2pressions on the ri&ht o' the assi&nment.
-he other assi&nment operators are
add into ?#3@
subtract 'rom ?-3@
multipl. into ?%3@
divide into ?$3@
modulus into ?&3@
le't shi't b. ?''3@
ri&ht shi't b. ?((3@
and into ?13@
inclusive;or into ?03@
e2clusive;or into ?/3@
#here the &eneral e2pression
lvalue op+ expression
is eCuivalent to
lvalue + lvalue op expression
#here op is as described belo#, and the l;value and e2pression must satis'. the semantic reCuirements o'
both op and eCuals ?3@.
63
5 Operators and !pressions
Readin& a variable be'ore #ritin& ?or initialiEin&@ it is le&al, ho#ever the value is unde'ined.
'.5 E)pressions
E2pressions in the shadin& lan&ua&e are built 'rom the 'ollo#in&:
%onstants o' t.pe @ool, int, uint, float, all vector t.pes, and all matri2 t.pes.
%onstructors o' all t.pes.
1ariable names o' all t.pes.
n arra. name #ith the len&th method applied.
Subscripted arra. names.
4unction calls that return values.
%omponent 'ield selectors and arra. subscript results.
3arenthesiEed e2pression. n. e2pression can be parenthesiEed. 3arentheses can be used to &roup
operations. Operations #ithin parentheses are done be'ore operations across parentheses.
-he arithmetic binar. operators add ?#@, subtract ?-@, multipl. ?%@, and divide ?$@ operate on inte&er and
'loatin&;point scalars, vectors, and matrices. . $' the operands are inte&er t.pes, the. must both be
si&ned or both be unsi&ned. ll arithmetic binar. operators result in the same 'undamental t.pe
?si&ned inte&er, unsi&ned inte&er, or 'loatin&;point@ as the operands the. operate on, a'ter operand t.pe
conversion. 'ter conversion, the 'ollo#in& cases are valid
-he t#o operands are scalars. $n this case the operation is applied, resultin& in a scalar.
One operand is a scalar, and the other is a vector or matri2. $n this case, the scalar operation is
applied independentl. to each component o' the vector or matri2, resultin& in the same siEe vector
or matri2.
-he t#o operands are vectors o' the same siEe. $n this case, the operation is done component;#ise
resultin& in the same siEe vector.
-he operator is add ?#@, subtract ?-@, or divide ?$@, and the operands are matrices #ith the same
number o' ro#s and the same number o' columns. $n this case, the operation is done component;
#ise resultin& in the same siEe matri2.
-he operator is multipl. ?%@, #here both operands are matrices or one operand is a vector and the
other a matri2. ri&ht vector operand is treated as a column vector and a le't vector operand as a
ro# vector. $n all these cases, it is reCuired that the number o' columns o' the le't operand is eCual
to the number o' ro#s o' the ri&ht operand. -hen, the multipl. ?%@ operation does a linear
al&ebraic multipl., .ieldin& an obFect that has the same number o' ro#s as the le't operand and the
same number o' columns as the ri&ht operand. Section /.1) I1ector and <atri2 OperationsJ
e2plains in more detail ho# vectors and matrices are operated on.
ll other cases are ille&al.
64
5 Operators and !pressions
!ividin& b. Eero does not cause an e2ception but does result in an unspeci'ied value. 9se the built;in
'unctions dot, Aross, matrixFompMult, and outerSroduAt, to &et, respectivel., vector dot product,
vector cross product, matri2 component;#ise multiplication, and the matri2 product o' a column
vector times a ro# vector.
-he operator modulus ?&@ operates on si&ned or unsi&ned inte&ers or inte&er vectors. -he operand
t.pes must both be si&ned or both be unsi&ned. -he operands cannot be vectors o' di''erin& siEe. $'
one operand is a scalar and the other vector, then the scalar is applied component;#ise to the vector,
resultin& in the same t.pe as the vector. $' both are vectors o' the same siEe, the result is computed
component;#ise. -he resultin& value is unde'ined 'or an. component computed #ith a second
operand that is Eero, #hile results 'or other components #ith non;Eero second operands remain
de'ined. $' both operands are non;ne&ative, then the remainder is non;ne&ative. Results are
unde'ined i' one or both operands are ne&ative. -he operator modulus ?&@ is not de'ined 'or an.
other data t.pes ?non;inte&er t.pes@.
-he arithmetic unar. operators ne&ate ?;@, post; and pre;increment and decrement ?-- and ##@ operate
on inte&er or 'loatin&;point values ?includin& vectors and matrices@. ll unar. operators #or8
component;#ise on their operands. -hese result #ith the same t.pe the. operated on. 4or post; and
pre;increment and decrement, the e2pression must be one that could be assi&ned to ?an l;value@. 3re;
increment and pre;decrement add or subtract 1 or 1.) to the contents o' the e2pression the. operate on,
and the value o' the pre;increment or pre;decrement e2pression is the resultin& value o' that
modi'ication. 3ost;increment and post;decrement e2pressions add or subtract 1 or 1.) to the contents
o' the e2pression the. operate on, but the resultin& e2pression has the e2pressionLs value be'ore the
post;increment or post;decrement #as e2ecuted.
-he relational operators &reater than ?(@, less than ?'@, &reater than or eCual ?(3@, and less than or
eCual ?'3@ operate onl. on scalar inte&er and scalar 'loatin&;point e2pressions. -he result is scalar
"oolean. -he t.pes o' the operands must match. -o do component;#ise relational comparisons on
vectors, use the built;in 'unctions lessTBan, lessTBanKJual, greaterTBan, and greaterTBanKJual
-he eCualit. operators eJual +33@, and not eCual ?43@ operate on all t.pes. -he. result in a scalar
"oolean. -he t.pes o' the operands must match. 4or vectors, matrices, structures, and arra.s, all
components, 'ields, or elements o' one operand must eCual the correspondin& components, 'ields, or
elements in the other operand 'or the operands to be considered eCual. -o &et a vector o' component;
#ise eCualit. results 'or vectors, use the built;in 'unctions eJual and notKJual.
-he lo&ical binar. operators and ?11@, or ? 0 0 @, and e2clusive or ?//@ operate onl. on t#o "oolean
e2pressions and result in a "oolean e2pression. nd ?11@ #ill onl. evaluate the ri&ht hand operand
i' the le't hand operand evaluated to true. Or ? 0 0 @ #ill onl. evaluate the ri&ht hand operand i' the le't
hand operand evaluated to false. E2clusive or ?//@ #ill al#a.s evaluate both operands.
-he lo&ical unar. operator not ?4@. $t operates onl. on a "oolean e2pression and results in a "oolean
e2pression. -o operate on a vector, use the built;in 'unction not.
-he seCuence ? , @ operator that operates on e2pressions b. returnin& the t.pe and value o' the ri&ht;
most e2pression in a comma separated list o' e2pressions. ll e2pressions are evaluated, in order,
'rom le't to ri&ht.
65
5 Operators and !pressions
-he ternar. selection operator ?75@. $t operates on three e2pressions ?e(p1 7 e(p2 5 e(p3@. -his
operator evaluates the 'irst e2pression, #hich must result in a scalar "oolean. $' the result is true, it
selects to evaluate the second e2pression, other#ise it selects to evaluate the third e2pression. Onl.
one o' the second and third e2pressions is evaluated. -he second and third e2pressions can be an.
t.pe, as lon& their t.pes match. -his resultin& matchin& t.pe is the t.pe o' the entire e2pression.
-he oneDs complement operator ?2@. -he operand must be o' t.pe si&ned or unsi&ned inte&er or inte&er
vector, and the result is the oneDs complement o' its operandM each bit o' each component is
complemented, includin& an. si&n bits.
-he shi't operators ?''@ and ?((@. 4or both operators, the operands must be si&ned or unsi&ned
inte&ers or inte&er vectors. One operand can be si&ned #hile the other is unsi&ned. $n all cases, the
resultin& t.pe #ill be the same t.pe as the le't operand. $' the 'irst operand is a scalar, the second
operand has to be a scalar as #ell. $' the 'irst operand is a vector, the second operand must be a scalar
or a vector #ith the same siEe as the 'irst operand, and the result is computed component;#ise. -he
result is unde'ined i' the ri&ht operand is ne&ative, or &reater than or eCual to the number o' bits in the
le't e2pressionDs base t.pe. -he value o' E1 GG E* is E1 ?interpreted as a bit pattern@ le't;shi'ted b.
E* bits. -he value o' E1 HH E* is E1 ri&ht;shi'ted b. E* bit positions. $' E1 is a si&ned inte&er, the
ri&ht;shi't #ill e2tend the si&n bit. $' E1 is an unsi&ned inte&er, the ri&ht;shi't #ill Eero;e2tend.
-he bit#ise operators and ?1@, e2clusive;or ?/@, and inclusive;or ?0@. -he operands must be o' t.pe
si&ned or unsi&ned inte&ers or inte&er vectors. -he operands cannot be vectors o' di''erin& siEe. $'
one operand is a scalar and the other a vector, the scalar is applied component;#ise to the vector,
resultin& in the same t.pe as the vector. -he 'undamental t.pes o' the operands ?si&ned or unsi&ned@
must match, and #ill be the resultin& 'undamental t.pe. 4or and ?1@, the result is the bit#ise;and
'unction o' the operands. 4or e2clusive;or ?/@, the result is the bit#ise e2clusive;or 'unction o' the
operands. 4or inclusive;or ?0@, the result is the bit#ise inclusive;or 'unction o' the operands.
4or a complete speci'ication o' the s.nta2 o' e2pressions, see section 6 IShadin& Lan&ua&e Grammar.J
'.10 (ector and 9atri) Operations
=ith a 'e# e2ceptions, operations are component;#ise. 9suall., #hen an operator operates on a vector or
matri2, it is operatin& independentl. on each component o' the vector or matri2, in a component;#ise
'ashion. 4or e2ample,
ve'; v0 u-
float f-
v + u D f-
#ill be eCuivalent to
v,x + u,x D f-
v,9 + u,9 D f-
v," + u," D f-
nd
ve'; v0 u0 .-
. + v D u-
66
5 Operators and !pressions
#ill be eCuivalent to
.,x + v,x D u,x-
.,9 + v,9 D u,9-
.," + v," D u,"-
and li8e#ise 'or most operators and all inte&er and 'loatin& point vector and matri2 t.pes. -he e2ceptions
are matri2 multiplied b. vector, vector multiplied b. matri2, and matri2 multiplied b. matri2. -hese do
not operate component;#ise, but rather per'orm the correct linear al&ebraic multipl..
ve'; v0 u-
mat; m-
u + v E m-
is eCuivalent to
u,x + dot#v0 mA=B)- && mA=B is t(e left 'olumn of m
u,9 + dot#v0 mA<B)- && dot#a0$) is t(e inner #dot) produ't of a and $
u," + dot#v0 mA8B)-
nd
u + m E v-
is eCuivalent to
u,x + mA=B,x E v,x D mA<B,x E v,9 D mA8B,x E v,"-
u,9 + mA=B,9 E v,x D mA<B,9 E v,9 D mA8B,9 E v,"-
u," + mA=B," E v,x D mA<B," E v,9 D mA8B," E v,"-
nd
mat; m0 n0 r-
r + m E n-
is eCuivalent to
rA=B,x + mA=B,x E nA=B,x D mA<B,x E nA=B,9 D mA8B,x E nA=B,"-
rA<B,x + mA=B,x E nA<B,x D mA<B,x E nA<B,9 D mA8B,x E nA<B,"-
rA8B,x + mA=B,x E nA8B,x D mA<B,x E nA8B,9 D mA8B,x E nA8B,"-
rA=B,9 + mA=B,9 E nA=B,x D mA<B,9 E nA=B,9 D mA8B,9 E nA=B,"-
rA<B,9 + mA=B,9 E nA<B,x D mA<B,9 E nA<B,9 D mA8B,9 E nA<B,"-
rA8B,9 + mA=B,9 E nA8B,x D mA<B,9 E nA8B,9 D mA8B,9 E nA8B,"-
rA=B," + mA=B," E nA=B,x D mA<B," E nA=B,9 D mA8B," E nA=B,"-
rA<B," + mA=B," E nA<B,x D mA<B," E nA<B,9 D mA8B," E nA<B,"-
rA8B," + mA=B," E nA8B,x D mA<B," E nA8B,9 D mA8B," E nA8B,"-
and similarl. 'or other siEes o' vectors and matrices.
67
5 Operators and !pressions
'.11 Ealuation of e)pressions
-he %OO standard reCuires that e2pressions must be evaluated in the order speci'ied b. the precedence o'
operations and ma. onl. be re&rouped i' the result is the same or #here the result is unde'ined. :o other
trans'orms ma. be applied that a''ect the result o' an operation. GLSL ES rela2es these reCuirements in
the 'ollo#in& #a.s:
ddition and multiplication are assumed to be associative.
<ultiplication ma. be replaced b. repeated addition
4loatin& point division ma. be replaced b. reciprocal and multiplication:
=ithin the constraints o' invariance ?#here applicable@, the precision used ma. var..
68
0 Statements and Structure
-he 'undamental buildin& bloc8s o' the OpenGL ES Shadin& Lan&ua&e are:
statements and declarations
'unction de'initions
selection ?if-else and s?itAB-Aase-default,
iteration +for, ?Bile, and do-?Bile,
Fumps +disAard, return, @reaC, and Aontinue@
-he overall structure o' a shader is as 'ollo#s
translation.unit:
glo)al.#eclaration
translation.unit glo)al.#eclaration
glo)al.#eclaration:
*unction.#e*inition
#eclaration
-hat is, a shader is a seCuence o' declarations and 'unction bodies. 4unction bodies are de'ined as
*unction.#e*inition:
*unction.prototype > statement.list ?
statement.list:
statement
statement.list statement
statement:
compoun#.statement
simple.statement
%url. braces are used to &roup seCuences o' statements into compound statements.
compoun#.statement:
> statement.list ?
simple.statement:
#eclaration.statement
e(pression.statement
selection.statement
69
6 "tate#ents and "tructure
iteration.statement
;ump.statement
Simple declaration, e2pression, and Fump statements end in a semi;colon.
-his above is sli&htl. simpli'ied, and the complete &rammar speci'ied in section 6 IShadin& Lan&ua&e
GrammarJ should be used as the de'initive speci'ication.
!eclarations and e2pressions have alread. been discussed.
0.1 +unction -efinitions
s indicated b. the &rammar above, a valid shader is a seCuence o' &lobal declarations and 'unction
de'initions. 'unction is declared as the 'ollo#in& e2ample sho#s:
&& protot9pe
return9pe fun'tionName #t9pe= arg=0 t9pe< arg<0 ,,,0 t9pen argn)-
and a 'unction is de'ined li8e
&& definition
return9pe fun'tionName #t9pe= arg=0 t9pe< arg<0 ,,,0 t9pen argn)
*
&& do some 'omputation
return returnValue-
2
#here returnType must be present and cannot be void.
or
void fun'tionName #t9pe= arg=0 t9pe< arg<0 ,,,0 t9pen argn)
*
&& do some 'omputation
return- && optional
2
. Each o' the type- must include a t.pe and can optionall. include a parameter Cuali'ier and>or Aonst.
'unction is called b. usin& its name 'ollo#ed b. a list o' ar&uments in parentheses.
rra.s are allo#ed as ar&uments and as the return t.pe. $n both cases, the arra. must be e2plicitl. siEed.
n arra. is passed or returned b. usin& Fust its name, #ithout brac8ets, and the siEe o' the arra. must
match the siEe speci'ied in the 'unctionDs declaration.
Structures are also allo#ed as ar&ument t.pes. -he return t.pe can also be a structure.
See section 6 IShadin& Lan&ua&e GrammarJ 'or the de'initive re'erence on the s.nta2 to declare and
de'ine 'unctions.
ll 'unctions must be either declared #ith a protot.pe or de'ined #ith a bod. be'ore the. are called. 4or
e2ample:
70
6 "tate#ents and "tructure
float m9fun' #float f0 && f is an input parameter
out float g)- && g is an output parameter
4unctions that return no value must be declared as void. void 'unction can onl. use return #ithout a
return ar&ument, even i' the return ar&ument has void t.pe. Return statements onl. accept values:
void fun'<#) * 2
void fun'8#) * return fun'<#)- 2 && illegal return statement
Onl. a precision Cuali'ier is allo#ed on the return t.pe o' a 'unction. 4ormal parameters can have
parameter and precision Cuali'iers, but no other Cuali'iers.
4unctions that accept no input ar&uments need not use void in the ar&ument list because protot.pes ?or
de'initions@ are reCuired and there'ore there is no ambi&uit. #hen an empt. ar&ument list _? @_ is declared.
-he idiom I?void@J as a parameter list is provided 'or convenience.
4unction names can be overloaded. -he same 'unction name can be used 'or multiple 'unctions, as lon&
as the parameter t.pes di''er. $' a 'unction name is declared t#ice #ith the same parameter t.pes, then the
return t.pes and all Cuali'iers must also match, and it is the same 'unction bein& declared. =hen 'unction
calls are resolved, an e2act t.pe match 'or all the ar&uments is reCuired.
4or e2ample,
ve'7 f#in ve'7 x0 out ve'7 9)-
ve'7 f#in ve'7 x0 out ive'7 9)- && allo.ed0 different argument t9pe
int f#in ve'7 x0 out ive'7 9)- && error0 onl9 return t9pe differs
ve'7 f#in ve'7 x0 in ive'7 9)- && error0 onl9 )ualifier differs
int f#'onst in ve'7 x0 out ive'7 9)- && error0 onl9 )ualifier differs
%allin& the 'irst t#o 'unctions above #ith the 'ollo#in& ar&ument t.pes .ields
f#ve'70 ve'7) && exa't mat'( of ve'7 f#in ve'7 x0 out ve'7 9)
f#ve'70 ive'7) && exa't mat'( of ve'7 f#in ve'7 x0 out ive'7 9)
f#ive'70 ve'7) && error0 no exa't mat'(,
f#ive'70 ive'7) && error0 no exa't mat'(,
9ser;de'ined 'unctions can have multiple declarations, but onl. one de'inition.
shader cannot rede'ine or overload built;in 'unctions.
-he 'unction main is used as the entr. point to a shader e2ecutable. "oth the verte2 and 'ra&ment shaders
must de'ine a 'unction named main. -his 'unction ta8es no ar&uments, returns no value, and must be
declared as t.pe void5
void main#)
*
,,,
2
-he 'unction main can contain uses o' return. See section 7.+ IJumpsJ 'or more details.
$t is an error to declare or de'ine a 'unction main #ith an. other parameters or return t.pe.
71
6 "tate#ents and "tructure
0.1.1 +unction Calling Conentions
4unctions are called b. value;return. -his means input ar&uments are copied into the 'unction at call time,
and output ar&uments are copied bac8 to the caller be'ore 'unction e2it. "ecause the 'unction #or8s #ith
local copies o' parameters, there are no issues re&ardin& aliasin& o' variables #ithin a 'unction. -o
control #hat parameters are copied in and>or out throu&h a 'unction de'inition or declaration:
-he 8e.#ord in is used as a Cuali'ier to denote a parameter is to be copied in, but not copied out.
-he 8e.#ord out is used as a Cuali'ier to denote a parameter is to be copied out, but not copied in.
-his should be used #henever possible to avoid unnecessaril. cop.in& parameters in.
-he 8e.#ord inout is used as a Cuali'ier to denote the parameter is to be both copied in and copied
out.
'unction parameter declared #ith no such Cuali'ier means the same thin& as speci'.in& in.
ll ar&uments are evaluated at call time, e2actl. once, in order, 'rom le't to ri&ht. Evaluation o' an in
parameter results in a value that is copied to the 'ormal parameter. Evaluation o' an out parameter results
in an l;value that is used to cop. out a value #hen the 'unction returns. Evaluation o' an inout parameter
results in both a value and an l;valueM the value is copied to the 'ormal parameter at call time and the l;
value is used to cop. out a value #hen the 'unction returns.
-he order in #hich output parameters are copied bac8 to the caller is unde'ined.
$n a 'unction, #ritin& to an input;onl. parameter is allo#ed. Onl. the 'unctionLs cop. is modi'ied. -his
can be prevented b. declarin& a parameter #ith the Aonst Cuali'ier.
=hen callin& a 'unction, e2pressions that do not evaluate to l;values cannot be passed to parameters
declared as out or inout.
:o Cuali'ier is allo#ed on the return t.pe o' a 'unction.
*unction.prototype:
precision.2uali*ier type *unction.name5const.2uali*ier parameter.2uali*ier precision.2uali*ier
type name array.speci*ier$ ... 6
type:
an. basic t.pe, arra. t.pe, structure name, or structure #e*inition
const.2uali*ier:
empt.
Aonst
parameter.2uali*ier:
empt.
in
out
inout
name:
empt.
72
6 "tate#ents and "tructure
identi'ier
array.speci*ier:
empt.
) constant.integral.e(pression *
,o#ever, the Aonst Cuali'ier cannot be used #ith out or inout. -he above is used 'or 'unction
declarations ?i.e., protot.pes@ and 'or 'unction de'initions. ,ence, 'unction de'initions can have unnamed
ar&uments.
Static and d.namic recursion is not allo#ed Static recursion is present i' the static 'unction call &raph o'
the pro&ram contains c.cles. !.namic recursion occurs i' at an. time control 'lo# has entered but not
e2ited a sin&le 'unction more than once.
0.! Selection
%onditional control 'lo# in the shadin& lan&ua&e is done b. either if, if;else, or s?itAB statements:
selection.statement:
if ? )ool.e(pression @ statement
if ? )ool.e(pression @ statement else statement
s?itAB ? init.e(pression @ \ s/itch.statement.listopt

]
=here s/itch.statement.list is a list o' Eero or more s/itch.statement and other statements de'ined b. the
lan&ua&e, #here s/itch.statement adds some 'orms o' labels. -hat is
s/itch.statement.list:
s/itch.statement
s/itch.statement.list s/itch.statement
s/itch.statement:
Aase constant.e(pression:
default 5
statement
$' an if-e2pression evaluates to true, then the 'irst statement is e2ecuted. $' it evaluates to false and there
is an else part then the second statement is e2ecuted.
n. e2pression #hose t.pe evaluates to a "oolean can be used as the conditional e2pression )ool.
e(pression. 1ector t.pes are not accepted as the e2pression to if.
%onditionals can be nested.
73
6 "tate#ents and "tructure
-he t.pe o' init.e(pression in a s#itch statement must be a scalar inte&er. $' a Aase label has a constant.
e(pression o' eCual value, then e2ecution #ill continue a'ter that label. Other#ise, i' there is a default
label, e2ecution #ill continue a'ter that label. Other#ise, e2ecution s8ips the rest o' the s#itch statement.
$t is an error to have more than one default or a replicated constant.e(pression. @reaC statement not
nested in a loop or other s#itch statement ?either not nested or nested onl. in if or if;else statements@ #ill
also s8ip the rest o' the s#itch statement. 4all throu&h labels are allo#ed, but it is an error to have no
statement bet#een a label and the end o' the s?itAB statement. :o statements are allo#ed in a s#itch
statement be'ore the 'irst Aase statement.
:o Aase or default labels can be nested inside other control 'lo# nested #ithin their correspondin&
s?itAB.
0.3 Iteration
4or, #hile, and do loops are allo#ed as 'ollo#s:
for #init1expression- 'ondition1expression- loop1expression)
su$1statement
.(ile #'ondition1expression)
su$1statement
do
statement
.(ile #'ondition1expression)
See section 6 IShadin& Lan&ua&e GrammarJ 'or the de'initive speci'ication o' loops.
-he for loop 'irst evaluates the init.e(pression, then the con#ition.e(pression. $' the con#ition.
e(pression evaluates to true, then the bod. o' the loop is e2ecuted. 'ter the bod. is e2ecuted, a for loop
#ill then evaluate the loop.e(pression, and then loop bac8 to evaluate the con#ition.e(pression, repeatin&
until the con#ition.e(pression evaluates to 'alse. -he loop is then e2ited, s8ippin& its bod. and s8ippin&
its loop.e(pression. 1ariables modi'ied b. the loop.e(pression maintain their value a'ter the loop is
e2ited, provided the. are still in scope. 1ariables declared in init.e(pression or con#ition.e(pression are
onl. in scope until the end o' the sub;statement o' the for loop.
-he ?Bile loop 'irst evaluates the con#ition.e(pression. $' true, then the bod. is e2ecuted. -his is then
repeated, until the con#ition.e(pression evaluates to 'alse, e2itin& the loop and s8ippin& its bod..
1ariables declared in the con#ition.e(pression are onl. in scope until the end o' the sub;statement o' the
#hile loop.
4or both for and ?Bile loops, the sub;statement does not introduce a ne# scope 'or variable names, so the
'ollo#in& has a redeclaration error:
for #int i + =- i / <=- iDD) *
int i- && rede'laration error
]
-he do-?Bile loop 'irst e2ecutes the bod., then e2ecutes the con#ition.e(pression. -his is repeated until
con#ition.e(pression evaluates to 'alse, and then the loop is e2ited.
74
6 "tate#ents and "tructure
E2pressions 'or con#ition.e(pression must evaluate to a "oolean.
"oth the con#ition.e(pression and the init.e(pression can declare and initialiEe a variable, e2cept in the
do-?Bile loop, #hich cannot declare a variable in its con#ition.e(pression. -he variableLs scope lasts
onl. until the end o' the sub;statement that 'orms the bod. o' the loop.
Loops can be nested.
:on;terminatin& loops are allo#ed. -he conseCuences o' ver. lon& or non;terminatin& loops are plat'orm
dependent.
0.% =umps
-hese are the Fumps:
;ump+statement:
Aontinue6
@reaC6
return6
return e(pression6
disAard6 $$ in the 'ra&ment shader lan&ua&e onl.
-here is no I&otoJ nor other non;structured 'lo# o' control.
-he Aontinue Fump is used onl. in loops. $t s8ips the remainder o' the bod. o' the innermost loop o'
#hich it is inside. 4or ?Bile and do-?Bile loops, this Fump is to the ne2t evaluation o' the loop
con#ition.e(pression 'rom #hich the loop continues as previousl. de'ined. 4or for loops, the Fump is to
the loop.e(pression, 'ollo#ed b. the con#ition.e(pression.
-he @reaC Fump can also be used onl. in loops and s#itch statements. $t is simpl. an immediate e2it o'
the inner;most loop or s#itch statements containin& the @reaC. :o 'urther e2ecution o' con#ition.
e(pression$ loop.e(pression, or s/itch.statement is done.
-he disAard 8e.#ord is onl. allo#ed #ithin 'ra&ment shaders. $t can be used #ithin a 'ra&ment shader to
abandon the operation on the current 'ra&ment. -his 8e.#ord causes the 'ra&ment to be discarded and no
updates to an. bu''ers #ill occur. %ontrol 'lo# e2its the shader, and subseCuent implicit or e2plicit
derivatives are unde'ined #hen this control 'lo# is non;uni'orm ?meanin& di''erent 'ra&ments #ithin the
primitive ta8e di''erent control paths@. $t #ould t.picall. be used #ithin a conditional statement, 'or
e2ample:
if #intensit9 / =,=)
dis'ard-
'ra&ment shader ma. test a 'ra&mentLs alpha value and discard the 'ra&ment based on that test.
,o#ever, it should be noted that covera&e testin& occurs a'ter the 'ra&ment shader runs, and the covera&e
test can chan&e the alpha value.
-he return Fump causes immediate e2it o' the current 'unction. $' it has e(pression then that is the return
value 'or the 'unction.
75
6 "tate#ents and "tructure
-he 'unction main can use return. -his simpl. causes main to e2it in the same #a. as #hen the end o'
the 'unction had been reached. $t does not impl. a use o' disAard in a 'ra&ment shader. 9sin& return in
main be'ore de'inin& outputs #ill have the same behavior as reachin& the end o' main be'ore de'inin&
outputs.
76
2 ,uilt8in (ariables
2.1 (erte) Shader Special (ariables
Some OpenGL ES operations occur in 'i2ed 'unctionalit. bet#een the verte2 processor and the 'ra&ment
processor. Shaders communicate #ith the 'i2ed 'unctionalit. o' OpenGL ES throu&h the use o' built;in
variables.
-he built;in verte2 shader variables 'or communicatin& #ith 'i2ed 'unctionalit. are intrinsicall. declared
as 'ollo#s in the verte2 lan&ua&e:
in (ig(p int gl_VertexI!-
in (ig(p int gl_Instan'eI!-
out (ig(p ve'7 gl_Cosition-
out (ig(p float gl_CointSi"e-
9nless other#ise noted else#here, these variables are onl. available in the verte2 lan&ua&e as declared
above.
-he variable gl+'osition is intended 'or #ritin& the homo&eneous verte2 position. $t can be #ritten at an.
time durin& shader e2ecution. -his value #ill be used b. primitive assembl., clippin&, cullin&, and other
'i2ed 'unctionalit. operations, i' present, that operate on primitives a'ter verte2 processin& has occurred.
$ts value is unde'ined a'ter the verte2 processin& sta&e i' the verte2 shader e2ecutable does not #rite
gl+'osition.
-he variable gl+'oint"i1e is intended 'or a shader to #rite the siEe o' the point to be rasteriEed. $t is
measured in pi2els. $' gl+'oint"i1e is not #ritten to, its value is unde'ined in subseCuent pipe sta&es.
-he variable gl+Verte(,D is a verte2 shader input variable that holds an inte&er inde2 'or the verte2, as
de'ined under IShader $nputsJ in section *.11.6 IShader E2ecutionJ in the OpenGL ES Graphics S.stem
Speci'ication. =hile the variable gl+Verte(,D is al#a.s present, its value is not al#a.s de'ined.
-he variable gl+,nstance,D is a verte2 shader input variable that holds the instance number o' the current
primitive in an instanced dra# call ?see IShader $nputsJ in section *.11.6 IShader E2ecutionJ in the
OpenGL ES (.) Graphics S.stem Speci'ication@. $' the current primitive does not come 'rom an instanced
dra# call, the value o' gl+,nstance,D is Eero.
77
7 Built$in Variables
2.! +ragment Shader Special (ariables
-he built;in special variables that are accessible 'rom a 'ra&ment shader are intrinsicall. declared as
'ollo#s:
in (ig(p ve'7 gl_FragHoord-
in $ool gl_FrontFa'ing-
out (ig(p float gl_Frag!ept(-
in mediump ve'8 gl_CointHoord-
E2cept as noted belo#, the. behave as other input and output variables.
-he output o' the 'ra&ment shader e2ecutable is processed b. the 'i2ed 'unction operations at the bac8 end
o' the OpenGL ES pipeline.
-he 'i2ed 'unctionalit. computed depth 'or a 'ra&ment ma. be obtained b. readin& gl+0rag%oor#.1$
described belo#.
=ritin& to gl+0ragDepth #ill establish the depth value 'or the 'ra&ment bein& processed. $' depth
bu''erin& is enabled, and no shader #rites gl+0ragDepth, then the 'i2ed 'unction value 'or depth #ill be
used as the 'ra&mentLs depth value. $' a shader staticall. assi&ns a value to gl+0ragDepth, and there is an
e2ecution path throu&h the shader that does not set gl+0ragDepth, then the value o' the 'ra&mentLs depth
ma. be unde'ined 'or e2ecutions o' the shader that ta8e that path. -hat is, i' the set o' lin8ed 'ra&ment
shaders staticall. contain a #rite to gl+0ragDepth, then it is responsible 'or al#a.s #ritin& it.
$' a shader e2ecutes the disAard 8e.#ord, the 'ra&ment is discarded, and the values o' an. user;de'ined
'ra&ment outputs, become irrelevant.
-he variable gl+0rag%oor# is available as an input variable 'rom #ithin 'ra&ment shaders and it holds the
#indo# relative coordinates ?(, y, 1, 1@/@ values 'or the 'ra&ment. $' multi;samplin&, this value can be 'or
an. location #ithin the pi2el, or one o' the 'ra&ment samples. -he use o' Aentroid in does not 'urther
restrict this value to be inside the current primitive. -his value is the result o' the 'i2ed 'unctionalit. that
interpolates primitives a'ter verte2 processin& to &enerate 'ra&ments. -he 1 component is the depth value
that #ould be used 'or the 'ra&mentLs depth i' no shader contained an. #rites to gl+0ragDepth. -his is
use'ul 'or invariance i' a shader conditionall. computes gl+0ragDepth but other#ise #ants the 'i2ed
'unctionalit. 'ra&ment depth.
4ra&ment shaders have access to the input built;in variable gl+0ront0acing$ #hose value is true i' the
'ra&ment belon&s to a 'ront;'acin& primitive. One use o' this is to emulate t#o;sided li&htin& b. selectin&
one o' t#o colors calculated b. a verte2 shader.
-he values in gl+'oint%oor# are t#o;dimensional coordinates indicatin& #here #ithin a point primitive
the current 'ra&ment is located, #hen point sprites are enabled. -he. ran&e 'rom ).) to 1.) across the
point. $' the current primitive is not a point, or i' point sprites are not enabled, then the values read 'rom
gl+'oint%oor# are unde'ined.
2.3 ,uilt8In Constants
-he 'ollo#in& built;in constants are provided to all shaders. -he actual values used are implementation
dependent, but must be at least the value sho#n.
&&
78
7 Built$in Variables
&& Implementation dependent 'onstants, (e example values $elo.
&& are t(e minimum values allo.ed for t(ese maximums,
&&
'onst mediump int gl_@axVertex?ttri$s + <L-
'onst mediump int gl_@axVertexKniformVe'tors + 8>L-
'onst mediump int gl_@axVertexOutputVe'tors + <L-
'onst mediump int gl_@axFragmentInputVe'tors + <>-
'onst mediump int gl_@axVertexextureImageKnits + <L-
'onst mediump int gl_@axHom$inedextureImageKnits + ;8-
'onst mediump int gl_@axextureImageKnits + <L-
'onst mediump int gl_@axFragmentKniformVe'tors + 887-
'onst mediump int gl_@ax!ra.5uffers + 7-
'onst mediump int gl_@inCrogramexelOffset + 1M-
'onst mediump int gl_@axCrogramexelOffset + F-
2.% ,uilt8In 6niform State
s an aid to accessin& OpenGL ES processin& state, the 'ollo#in& uni'orm variables are built into the
OpenGL ES Shadin& Lan&ua&e.
&&
&& !ept( range in .indo. 'oordinates0
&& se'tion 8,<;,< 3Hontrolling t(e Vie.port4 in t(e
&& OpenGL ES Grap(i's S9stem Spe'ifi'ation,
&&
stru't gl_!ept(RangeCarameters *
(ig(p float near- && n
(ig(p float far- && f
(ig(p float diff- && f 1 n
2-
uniform gl_!ept(RangeCarameters gl_!ept(Range-
79
4 ,uilt8in +unctions
-he OpenGL ES Shadin& Lan&ua&e de'ines an assortment o' built;in convenience 'unctions 'or scalar and
vector operations. <an. o' these built;in 'unctions can be used in more than one t.pe o' shader, but some
are intended to provide a direct mappin& to hard#are and so are available onl. 'or a speci'ic t.pe o'
shader.
-he built;in 'unctions basicall. 'all into three cate&ories:
-he. e2pose some necessar. hard#are 'unctionalit. in a convenient #a. such as accessin& a te2ture
map. -here is no #a. in the lan&ua&e 'or these 'unctions to be emulated b. a shader.
-he. represent a trivial operation ?clamp, mi2, etc.@ that is ver. simple 'or the user to #rite, but the.
are ver. common and ma. have direct hard#are support. $t is a ver. hard problem 'or the compiler to
map e2pressions to comple2 assembler instructions.
-he. represent an operation &raphics hard#are is li8el. to accelerate at some point. -he tri&onometr.
'unctions 'all into this cate&or..
<an. o' the 'unctions are similar to the same named ones in common % libraries, but the. support vector
input as #ell as the more traditional scalar input.
pplications should be encoura&ed to use the built;in 'unctions rather than do the eCuivalent computations
in their o#n shader code since the built;in 'unctions are assumed to be optimal ?e.&., perhaps supported
directl. in hard#are@.
=hen the built;in 'unctions are speci'ied belo#, #here the input ar&uments ?and correspondin& output@
can be float, veAD, veA8, or veAE, genType is used as the ar&ument. =here the input ar&uments ?and
correspondin& output@ can be int, iveAD, iveA8, or iveAE, gen,Type is used as the ar&ument. =here the
input ar&uments ?and correspondin& output@ can be uint, uveAD, uveA8, or uveAE, genAType is used as the
ar&ument. =here the input ar&uments ?or correspondin& output@ can be @ool, @veAD, @veA8, or @veAE,
genBType is used as the ar&ument. 4or an. speci'ic use o' a 'unction, the actual t.pes substituted 'or
genType, gen,Type, genAType$ or genBType have to have the same number o' components 'or all
ar&uments and 'or the return t.pe. Similarl. 'or mat$ #hich can be an. matri2 basic t.pe.
-he precision o' built;in 'unctions is dependent on the 'unction and ar&uments. -here are three
cate&ories:
Some 'unctions have prede'ined precisions. -he precision is speci'ied
e.&.
hi&hp vec* texture;ize ?&sampler*! sampler, int lo#@
4or the te2ture samplin& 'unctions, the precision o' the return t.pe matches the precision o' the
sampler t.pe.
80
8 Built$in %unctions
uniform lo.p sampler8! sampler-
(ig(p ve'8 'oord-
,,,
lo.p ve'7 'ol + texture #sampler0 'oord)- && texture#) returns lo.p
4or other built;in 'unctions, a call #ill return a precision Cuali'ication matchin& the hi&hest precision
Cuali'ication o' the callDs input ar&uments. See Section +./.* I3recision Quali'iersJ 'or more detail.
-he built;in 'unctions are assumed to be implemented accordin& to the eCuations speci'ied in the
'ollo#in& sections. -he precision at #hich the calculations are per'ormed 'ollo#s the &eneral rules 'or
precision o' operations as speci'ied in section +./.( I3recision Quali'iersI.
E2ample:
normali1e
(
(
y
1
)
=
1
(
*
+y
*
+1
*
(
(
y
1
)
$' the input vector is lo#p, the entire calculation is per'ormed at lo#p. 4or some inputs, this #ill cause
the calculation to over'lo#, even #hen the correct result is #ithin the ran&e o' lo#p.
81
8 Built$in %unctions
4.1 .ngle and Trigonometr& +unctions
4unction parameters speci'ied as angle are assumed to be in units o' radians. $n no case #ill an. o' these
'unctions result in a divide b. Eero error. $' the divisor o' a ratio is ), then results #ill be unde'ined.
-hese all operate component;#ise. -he description is per component.
S&nta) -escription
&en-.pe radians ?&en-.pe #egrees@
%onverts #egrees to radians, i.e.,

15)
#egrees
&en-.pe degrees ?&en-.pe ra#ians@
%onverts ra#ians to de&rees, i.e.,
15)

ra#ians
&en-.pe sin ?&en-.pe angle@ -he standard tri&onometric sine 'unction.
&en-.pe Aos ?&en-.pe angle@ -he standard tri&onometric cosine 'unction.
&en-.pe tan ?&en-.pe angle@ -he standard tri&onometric tan&ent.
&en-.pe asin ?&en-.pe (@ rc sine. Returns an an&le #hose sine is (. -he ran&e
o' values returned b. this 'unction is
[

*
$

*
]
Results are unde'ined i' (1.
&en-.pe aAos ?&en-.pe (@ rc cosine. Returns an an&le #hose cosine is (. -he
ran&e o' values returned b. this 'unction is Z), [.
Results are unde'ined i' (1.
&en-.pe atan ?&en-.pe y, &en-.pe (@ rc tan&ent. Returns an an&le #hose tan&ent is y@(. -he
si&ns o' ( and y are used to determine #hat Cuadrant the
an&le is in. -he ran&e o' values returned b. this
'unction is [$ ] . Results are unde'ined i' ( and
y are both ).
&en-.pe atan ?&en-.pe y+over+(@ rc tan&ent. Returns an an&le #hose tan&ent is
y+over+(. -he ran&e o' values returned b. this 'unction
is
[

*
$

*
]
.
82
8 Built$in %unctions
S&nta) -escription
&en-.pe sinB ?&en-.pe (@ Returns the h.perbolic sine 'unction
e
(
e
(
*
&en-.pe AosB ?&en-.pe (@ Returns the h.perbolic cosine 'unction
e
(
e
(
*
&en-.pe tanB ?&en-.pe (@ Returns the h.perbolic tan&ent 'unction
sinh (
cosh (
&en-.pe asinB ?&en-.pe (@ rc h.perbolic sineM returns the inverse o' sinB.
&en-.pe aAosB ?&en-.pe (@ rc h.perbolic cosineM returns the non;ne&ative inverse
o' AosB. Results are unde'ined i' ( G 1.
&en-.pe atanB ?&en-.pe (@ rc h.perbolic tan&entM returns the inverse o' tanB.
Results are unde'ined i' (1.
4.! E)ponential +unctions
-hese all operate component;#ise. -he description is per component.
S&nta) -escription
&en-.pe po? ?&en-.pe (, &en-.pe y@ Returns ( raised to the y po#er, i.e., (
y
Results are unde'ined i' ( C 0.
Results are unde'ined i' ( = 0 and y C= 0.
&en-.pe exp ?&en-.pe (@ Returns the natural e2ponentiation o' (, i.e., e
(
.
&en-.pe log ?&en-.pe (@ Returns the natural lo&arithm o' ($ i.e., returns the value
y #hich satis'ies the eCuation ( T e
y
.
Results are unde'ined i' ( C= 0.
&en-.pe expD ?&en-.pe (@ Returns * raised to the ( po#er, i.e., *
(
&en-.pe logD ?&en-.pe (@ Returns the base * lo&arithm o' ($ i.e., returns the value
y #hich satis'ies the eCuation (=*
y
Results are unde'ined i' ( C= 0.
83
8 Built$in %unctions
S&nta) -escription
&en-.pe sJrt ?&en-.pe (@ Returns
( .
Results are unde'ined i' ( C 0.
&en-.pe inversesJrt ?&en-.pe (@
Returns
1
(
.
Results are unde'ined i' ( C= 0.
4.3 Common +unctions
-hese all operate component;#ise. -he description is per component.
S&nta) -escription
&en-.pe a@s ?&en-.pe (@
&en$-.pe a@s ?&en$-.pe (@
Returns ( i' ( HT ), other#ise it returns ^(.
&en-.pe sign ?&en-.pe (@
&en$-.pe sign ?&en$-.pe (@
Returns 1.) i' ( H ), ).) i' ( T ), or ^1.) i' ( G ).
&en-.pe floor ?&en-.pe (@ Returns a value eCual to the nearest inte&er that is less
than or eCual to (.
&en-.pe trunA ?&en-.pe (@ Returns a value eCual to the nearest inte&er to ( #hose
absolute value is not lar&er than the absolute value o' (.
&en-.pe round ?&en-.pe (@ Returns a value eCual to the nearest inte&er to (. -he
'raction )./ #ill round in a direction chosen b. the
implementation, presumabl. the direction that is 'astest.
-his includes the possibilit. that round?(@ returns the
same value as roundKven?(@ 'or all values o' (.
&en-.pe roundKven ?&en-.pe (@ Returns a value eCual to the nearest inte&er to (.
'ractional part o' )./ #ill round to#ard the nearest even
inte&er. ?"oth (./ and +./ 'or 2 #ill return +.).@
&en-.pe Aeil ?&en-.pe (@ Returns a value eCual to the nearest inte&er that is
&reater than or eCual to (.
&en-.pe fraAt ?&en-.pe (@ Returns ( ^ floor ?(@.
84
8 Built$in %unctions
S&nta) -escription
&en-.pe mod ?&en-.pe (, 'loat y@
&en-.pe mod ?&en-.pe (, &en-.pe y@
<odulus. Returns ( ^ y floor ?(>y@.
&en-.pe modf ?&en-.pe (, out &en-.pe i@ Returns the 'ractional part o' ( and sets i to the inte&er
part ?as a #hole number 'loatin& point value@. "oth the
return value and the output parameter #ill have the same
si&n as (.
&en-.pe min ?&en-.pe (, &en-.pe y@
&en-.pe min ?&en-.pe (, 'loat y@
&en$-.pe min ?&en$-.pe (, &en$-.pe y@
&en$-.pe min ?&en$-.pe (, int y@
&en9-.pe min ?&en9-.pe (, &en9-.pe y@
&en9-.pe min ?&en9-.pe (, uint y@
Returns y i' y G (, other#ise it returns (.
&en-.pe max ?&en-.pe (, &en-.pe y@
&en-.pe max ?&en-.pe (, 'loat y@
&en$-.pe max ?&en$-.pe (, &en$-.pe y@
&en$-.pe max ?&en$-.pe (, int y@
&en9-.pe max ?&en9-.pe (, &en9-.pe y@
&en9-.pe max ?&en9-.pe (, uint y@
Returns y i' ( G y, other#ise it returns (.
&en-.pe Alamp ?&en-.pe (,
&en-.pe minVal,
&en-.pe ma(Val@
&en-.pe Alamp ?&en-.pe (,
'loat minVal,
'loat ma(Val@
&en$-.pe Alamp ?&en$-.pe (,
&en$-.pe minVal,
&en$-.pe ma(Val@
&en$-.pe Alamp ?&en$-.pe (,
int minVal,
int ma(Val@
&en9-.pe Alamp ?&en9-.pe (,
&en9-.pe minVal,
&en9-.pe ma(Val@
&en9-.pe Alamp ?&en9-.pe (,
uint minVal,
uint ma(Val@
Returns min ?max ?(, minVal@, ma(Val@.
Results are unde'ined i' minVal H ma(Val.
85
8 Built$in %unctions
S&nta) -escription
&en-.pe mix ?&en-.pe (,
&en-.pe y,
&en-.pe a@
&en-.pe mix ?&en-.pe (,
&en-.pe y,
'loat a@
Returns the linear blend o' ( and y$ i.e.,
(1aya
&en-.pe mix ?&en-.pe (,
&en-.pe .,
&en"-.pe a@
Selects #hich vector each returned component comes
'rom. 4or a component o' a that is false, the
correspondin& component o' ( is returned. 4or a
component o' a that is true, the correspondin&
component o' y is returned. %omponents o' ( and y that
are not selected are allo#ed to be invalid 'loatin& point
values and #ill have no e''ect on the results. -hus, this
provides di''erent 'unctionalit. than
&en-.pe mix?&en-.pe (, &en-.pe y, &en-.pe?a@@
#here a is a "oolean vector.
&en-.pe step ?&en-.pe e#ge, &en-.pe (@
&en-.pe step ?'loat e#ge, &en-.pe (@
Returns ).) i' ( G e#ge, other#ise it returns 1.).
&en-.pe smootBstep ?&en-.pe e#ge0,
&en-.pe e#ge1,
&en-.pe (@
&en-.pe smootBstep ?'loat e#ge0,
'loat e#ge1,
&en-.pe (@
Returns ).) i' ( GT e#ge0 and 1.) i' ( HT e#ge1 and
per'orms smooth ,ermite interpolation bet#een ) and 1
#hen e#ge0 G ( G e#ge1. -his is use'ul in cases #here
.ou #ould #ant a threshold 'unction #ith a smooth
transition. -his is eCuivalent to:
&en-.pe tM
t T clamp ??2 ^ ed&e)@ > ?ed&e1 ^ ed&e)@, ), 1@M
return t R t R ?( ^ * R t@M
Results are un#e*ine# i* e#ge0 D= e#ge1.
&en"-.pe isnan ?&en-.pe (@ Returns true i' ( holds a :a:. Returns false other#ise.
&en"-.pe isinf ?&en-.pe (@ Returns true i' ( holds a positive in'init. or ne&ative
in'init.. Returns false other#ise.
86
8 Built$in %unctions
S&nta) -escription
&en$-.pe floatHitsToNnt ?&en-.pe value@
&en9-.pe floatHitsToUint ?&en-.pe value@
Returns a si&ned or unsi&ned hi&hp inte&er value
representin& the encodin& o' a 'loatin&;point value. 4or
hi&hp 'loatin& point, the valueDs bit level representation
is preserved. 4or mediump and lo#p, the value is 'irst
converted to hi&hp 'loatin& point and the encodin& o'
that value is returned.
&en-.pe intHitsToLloat ?&en$-.pe value@
&en-.pe uintHitsToLloat ?&en9-.pe value@
Returns a hi&hp 'loatin&;point value correspondin& to a
si&ned or unsi&ned inte&er encodin& o' a 'loatin&;point
value. $' an in' or :a: is passed in, it #ill not si&nal,
and the resultin& 'loatin& point value is unspeci'ied.
Other#ise, the bit;level representation is preserved. 4or
lo#p and mediump, the value is 'irst converted to the
correspondin& si&ned or unsi&ned hi&hp inte&er and then
reinterpreted as a hi&hp 'loatin& point value as be'ore.
87
8 Built$in %unctions
4.% +loating8*oint *ac1 and 6npac1 +unctions
-hese 'unctions do not operate component;#ise, rather as described in each case.
S&nta) -escription
hi&hp uint paAC;normDxGY ?vec* v@ 4irst, converts each component o' the normaliEed
'loatin&;point value v into 17;bit inte&er values. -hen,
the results are pac8ed into the returned (*;bit unsi&ned
inte&er.
-he conversion 'or component c o' v to 'i2ed point is
done as 'ollo#s:
paAC;normDxGY: round?clamp?c, ;1, O1@ R (*070.)@
-he 'irst component o' the vector #ill be #ritten to the
least si&ni'icant bits o' the outputM the last component
#ill be #ritten to the most si&ni'icant bits.
hi&hp vec* unpaAC;normDxGY ?hi&hp uint
p@
4irst, unpac8s a sin&le (*;bit unsi&ned inte&er p into a
pair o' 17;bit unsi&ned inte&ers. -hen, each component
is converted to a normaliEed 'loatin&;point value to
&enerate the returned t#o;component vector.
-he conversion 'or unpac8ed 'i2ed;point value * to
'loatin& point is done as 'ollo#s:
unpaAC;normDxGY5 clamp?* > (*070.), ;1,O1@
-he 'irst component o' the returned vector #ill be
e2tracted 'rom the least si&ni'icant bits o' the inputM the
last component #ill be e2tracted 'rom the most
si&ni'icant bits.
hi&hp uint paACUnormDxGY ?vec* v@ 4irst, converts each component o' the normaliEed
'loatin&;point value v into 17;bit inte&er values. -hen,
the results are pac8ed into the returned (*;bit unsi&ned
inte&er.
-he conversion 'or component c o' v to 'i2ed point is
done as 'ollo#s:
paACUnormDxGY: round?clamp?c, ), O1@ R 7//(/.)@
-he 'irst component o' the vector #ill be #ritten to the
least si&ni'icant bits o' the outputM the last component
#ill be #ritten to the most si&ni'icant bits.
88
8 Built$in %unctions
S&nta) -escription
hi&hp vec* unpaACUnormDxGY ?hi&hp uint
p@
4irst, unpac8s a sin&le (*;bit unsi&ned inte&er p into a
pair o' 17;bit unsi&ned inte&ers. -hen, each component
is converted to a normaliEed 'loatin&;point value to
&enerate the returned t#o;component vector.
-he conversion 'or unpac8ed 'i2ed;point value * to
'loatin& point is done as 'ollo#s:
unpaACUnormDxGY5 * > 7//(/.)
-he 'irst component o' the returned vector #ill be
e2tracted 'rom the least si&ni'icant bits o' the inputM the
last component #ill be e2tracted 'rom the most
si&ni'icant bits.
hi&hp uint paACMalfDxGY ?mediump vec* v@ Returns an unsi&ned inte&er obtained b. convertin& the
components o' a t#o;component 'loatin&;point vector to
the 17;bit 'loatin&;point representation 'ound in the
OpenGL ES Speci'ication, and then pac8in& these t#o
17;bit inte&ers into a (*;bit unsi&ned inte&er.
-he 'irst vector component speci'ies the 17 least;
si&ni'icant bits o' the resultM the second component
speci'ies the 17 most;si&ni'icant bits.
mediump vec* unpaACMalfDxGY ?hi&hp uint
v@
Returns a t#o;component 'loatin&;point vector #ith
components obtained b. unpac8in& a (*;bit unsi&ned
inte&er into a pair o' 17;bit values, interpretin& those
values as 17;bit 'loatin&;point numbers accordin& to the
OpenGL ES Speci'ication, and convertin& them to (*;bit
'loatin&;point values.
-he 'irst component o' the vector is obtained 'rom the
17 least;si&ni'icant bits o' vM the second component is
obtained 'rom the 17 most;si&ni'icant bits o' v.
4.' Geometric +unctions
-hese operate on vectors as vectors, not component;#ise.
S&nta) -escription
'loat lengtB ?&en-.pe (@ Returns the len&th o' vector (, i.e.,
([ )]
*
([ 1]
*
...
89
8 Built$in %unctions
S&nta) -escription
'loat distanAe ?&en-.pe p0, &en-.pe p1@ Returns the distance bet#een p0 and p1, i.e.,
lengtB ?p0 E p1@
'loat dot ?&en-.pe (, &en-.pe y@ Returns the dot product o' ( and y, i.e.,
([ )]y [)]+([ 1]y [1]+...
vec( Aross ?vec( (, vec( y@ Returns the cross product o' 2 and ., i.e.,
[
( [1]y[ *]y [1]( [*]
([ *]y[ )]y [ *]([ )]
([)]y [1]y [)]( [1]
]
&en-.pe normalize ?&en-.pe (@ Returns a vector in the same direction as ( but #ith a
len&th o' 1 i.e.
(
length( ()
&en-.pe faAefor?ard?&en-.pe -,
&en-.pe ,,
&en-.pe -re*@
$' dot?-re*, ,@ G ) return -$ other#ise return ^-.
&en-.pe refleAt ?&en-.pe ,, &en-.pe -@ 4or the incident vector , and sur'ace orientation -,
returns the re'lection direction:
, ^ * dot?-, ,@ -
- must alread. be normaliEed in order to achieve the
desired result.
&en-.pe refraAt?&en-.pe ,, &en-.pe -,
'loat eta@
4or the incident vector , and sur'ace normal -, and the
ratio o' indices o' re'raction eta$ return the re'raction
vector. -he result is computed b.
8 T 1.) ; eta R eta R ?1.) ; dot?-, ,@ R dot?-, ,@@
i' ?8 G ).)@
return &en-.pe?).)@
else
return eta R , ; ?eta R dot?-, ,@ O sJrt?8@@ R -
-he input parameters 'or the incident vector , and the
sur'ace normal - must alread. be normaliEed to &et the
desired results.
90
8 Built$in %unctions
4.0 9atri) +unctions
S&nta) -escription
mat matrixFompMult ?mat (, mat y@ <ultipl. matri2 ( b. matri2 y component;#ise, i.e.,
resultZi[ZF[ is the scalar product o' (Zi[ZF[ and yZi[ZF[.
:ote: to &et linear al&ebraic matri2 multiplication, use
the multipl. operator ?%@.
mat* outerSroduAt?vec* c, vec* r@
mat( outerSroduAt?vec( c, vec( r@
mat+ outerSroduAt?vec+ c, vec+ r@
mat*2( outerSroduAt?vec( c, vec* r@
mat(2* outerSroduAt?vec* c, vec( r@
mat*2+ outerSroduAt?vec+ c, vec* r@
mat+2* outerSroduAt?vec* c, vec+ r@
mat(2+ outerSroduAt?vec+ c, vec( r@
mat+2( outerSroduAt?vec( c, vec+ r@
-reats the 'irst parameter c as a column vector ?matri2
#ith one column@ and the second parameter r as a ro#
vector ?matri2 #ith one ro#@ and does a linear al&ebraic
matri2 multipl. c R r, .ieldin& a matri2 #hose number o'
ro#s is the number o' components in c and #hose
number o' columns is the number o' components in r.
mat* transpose?mat* m@
mat( transpose?mat( m@
mat+ transpose?mat+ m@
mat*2( transpose?mat(2* m@
mat(2* transpose?mat*2( m@
mat*2+ transpose?mat+2* m@
mat+2* transpose?mat*2+ m@
mat(2+ transpose?mat+2( m@
mat+2( transpose?mat(2+ m@
Returns a matri2 that is the transpose o' m. -he input
matri2 m is not modi'ied.
'loat determinant?mat* m@
'loat determinant?mat( m@
'loat determinant?mat+ m@
Returns the determinant o' m.
mat* inverse?mat* m@
mat( inverse?mat( m@
mat+ inverse?mat+ m@
Returns a matri2 that is the inverse o' m. -he input
matri2 m is not modi'ied. -he values in the returned
matri2 are unde'ined i' m is sin&ular or poorl.;
conditioned ?nearl. sin&ular@.
91
8 Built$in %unctions
4.2 (ector :elational +unctions
Relational and eCualit. operators ?', '3, (, (3, 33, 43@ are de'ined to produce scalar "oolean results. 4or
vector results, use the 'ollo#in& built;in 'unctions. "elo#, IbvecJ is a placeholder 'or one o' @veAD,
@veA8, or @veAE, IivecJ is a placeholder 'or one o' iveAD, iveA8, or iveAE, IuvecJ is a placeholder 'or
uveAD, uveA8, or uveAE, and IvecJ is a placeholder 'or veAD, veA8, or veAE. $n all cases, the siEes o' the
input and return vectors 'or an. particular call must match.
S&nta) -escription
bvec lessTBan?vec 2, vec .@
bvec lessTBan?ivec 2, ivec .@
bvec lessTBan?uvec 2, uvec .@
Returns the component;#ise compare o' ( G y.
bvec lessTBanKJual?vec 2, vec .@
bvec lessTBanKJual?ivec 2, ivec .@
bvec lessTBanKJual?uvec 2, uvec .@
Returns the component;#ise compare o' ( GT y.
bvec greaterTBan?vec 2, vec .@
bvec greaterTBan?ivec 2, ivec .@
bvec greaterTBan?uvec 2, uvec .@
Returns the component;#ise compare o' ( H y.
bvec greaterTBanKJual?vec 2, vec .@
bvec greaterTBanKJual?ivec 2, ivec .@
bvec greaterTBanKJual?uvec 2, uvec .@
Returns the component;#ise compare o' ( HT y.
bvec eJual?vec 2, vec .@
bvec eJual?ivec 2, ivec .@
bvec eJual?uvec 2, uvec .@
bvec eJual?bvec 2, bvec .@
bvec notKJual?vec 2, vec .@
bvec notKJual?ivec 2, ivec .@
bvec notKJual?uvec 2, uvec .@
bvec notKJual?bvec 2, bvec .@
Returns the component;#ise compare o' ( TT y.
Returns the component;#ise compare o' ( QT y.
bool any?bvec 2@ Returns true i' an. component o' ( is true.
bool all?bvec 2@ Returns true onl. i' all components o' ( are true.
bvec not?bvec 2@ Returns the component;#ise lo&ical complement o' (.
92
8 Built$in %unctions
4.4 Te)ture Loo1up +unctions
-e2ture loo8up 'unctions are available to verte2 and 'ra&ment shaders. ,o#ever, level o' detail is not
implicitl. computed 'or verte2 shaders. -he 'unctions in the table belo# provide access to te2tures
throu&h samplers, as set up throu&h the OpenGL ES 3$. -e2ture properties such as siEe, pi2el 'ormat,
number o' dimensions, 'ilterin& method, number o' mip;map levels, depth comparison, and so on are also
de'ined b. OpenGL ES 3$ calls. Such properties are ta8en into account as the te2ture is accessed via the
built;in 'unctions de'ined belo#.
-e2ture data can be stored b. the GL as 'loatin& point, unsi&ned normaliEed inte&er, unsi&ned inte&er or
si&ned inte&er data. -his is determined b. the t.pe o' the internal 'ormat o' the te2ture. -e2ture loo8ups
on unsi&ned normaliEed inte&er and 'loatin& point data return 'loatin& point values in the ran&e Z), 1[.
-e2ture loo8up 'unctions are provided that can return their result as 'loatin& point, unsi&ned inte&er or
si&ned inte&er, dependin& on the sampler t.pe passed to the loo8up 'unction. %are must be ta8en to use
the ri&ht sampler t.pe 'or te2ture access. -he 'ollo#in& table lists the supported combinations o' sampler
t.pes and te2ture internal 'ormats. "lan8 entries are unsupported. !oin& a te2ture loo8up #ill return
unde'ined values 'or unsupported combinations.
$nternal -e2ture 4ormat
4loatin& 3oint
Sampler -.pes
Si&ned $nte&er
Sampler -.pes
9nsi&ned $nte&er
Sampler -.pes
4loatin& point Supported
:ormaliEed $nte&er Supported
Si&ned $nte&er Supported
9nsi&ned $nte&er Supported
$' an inte&er sampler t.pe is used, the result o' a te2ture loo8up is an iveAE. $' an unsi&ned inte&er
sampler t.pe is used, the result o' a te2ture loo8up is a uveAE. $' a 'loatin& point sampler t.pe is used, the
result o' a te2ture loo8up is a veAE, #here each component is in the ran&e Z), 1[.
$n the protot.pes belo#, the IgJ in the return t.pe Igvec:J is used as a placeholder 'or nothin&, IiJ, or IuJ
ma8in& a return t.pe o' veAE, iveAE, or uveAE. $n these cases, the sampler ar&ument t.pe also starts #ith
IgJ, indicatin& the same substitution done on the return t.peM it is either a 'loatin& point, si&ned inte&er, or
unsi&ned inte&er sampler, matchin& the basic t.pe o' the return t.pe, as described above.
4or shado# 'orms ?the sampler parameter is a shado#;t.pe@, a depth comparison loo8up on the depth
te2ture bound to sampler is done as described in section (.5.17 I-e2ture %omparison <odesJ o' the
OpenGL ES Graphics S.stem Speci'ication. See the table belo# 'or #hich component speci'ies Dre*. -he
te2ture bound to sampler must be a depth te2ture, or results are unde'ined. $' a non;shado# te2ture call is
made to a sampler that represents a depth te2ture #ith depth comparisons turned on, then results are
unde'ined. $' a shado# te2ture call is made to a sampler that represents a depth te2ture #ith depth
comparisons turned o'', then results are unde'ined. $' a shado# te2ture call is made to a sampler that does
not represent a depth te2ture, then results are unde'ined.
93
8 Built$in %unctions
$n all 'unctions belo#, the )ias parameter is optional 'or 'ra&ment shaders. -he )ias parameter is not
accepted in a verte2 shader. 4or a 'ra&ment shader, i' )ias is present, it is added to the implicit level o'
detail prior to per'ormin& the te2ture access operation.
-he implicit level o' detail is selected as 'ollo#s: 4or a te2ture that is not mip;mapped, the te2ture is used
directl.. $' it is mip;mapped and runnin& in a 'ra&ment shader, the LO! computed b. the implementation
is used to do the te2ture loo8up. $' it is mip;mapped and runnin& on the verte2 shader, then the base
te2ture is used.
Some te2ture 'unctions ?non;I>odJ and non;I=radJ versions@ ma. reCuire implicit derivatives. $mplicit
derivatives are unde'ined #ithin non;uni'orm control 'lo# and 'or verte2 te2ture 'etches.
4or Fu@e 'orms, the direction o' ' is used to select #hich 'ace to do a *;dimensional te2ture loo8up in, as
described in section (.5.1) I%ube <ap -e2ture SelectionJ in the OpenGL ES Graphics S.stem
Speci'ication.
4or Array 'orms, the arra. la.er used #ill be
ma( ),min # 1, *loorlayer)./
#here # is the depth o' the te2ture arra. and layer comes 'rom the component indicated in the tables
belo#.
94
8 Built$in %unctions
S&nta) -escription

hi&hp vec* texture;ize ?&sampler*! sampler, int lo#@
hi&hp vec( texture;ize ?&sampler(! sampler, int lo#@
hi&hp vec* texture;ize ?&sampler%ube sampler, int lo#@

hi&hp vec* texture;ize ?sampler*!Shado# sampler, int lo#@
hi&hp vec* texture;ize ?sampler%ubeShado# sampler, int lo#@
hi&hp vec( texture;ize ?&sampler*!rra. sampler, int lo#@
hi&hp vec( texture;ize ?sampler*!rra.Shado# sampler, int lo#@
Returns the dimensions o'
level lo# 'or the te2ture bound
to sampler, as described in
section *.11.6 IShader
E2ecutionJ o' the OpenGL ES
(.) Graphics S.stem
Speci'ication, under I-e2ture
SiEe Quer.J.
-he components in the return
value are 'illed in, in order,
#ith the #idth, hei&ht, depth o'
the te2ture.
4or the arra. 'orms, the last
component o' the return value
is the number o' la.ers in the
te2ture arra..
&vec+ texture ?&sampler*! sampler, vec* ' Z, 'loat )ias[ @
&vec+ texture ?&sampler(! sampler, vec( ' Z, 'loat )ias[ @
&vec+ texture ?&sampler%ube sampler, vec( ' Z, 'loat )ias[ @

'loat texture ?sampler*!Shado# sampler$ vec( ' Z, 'loat )ias[ @
'loat texture ?sampler%ubeShado# sampler$ vec+ ' Z, 'loat )ias[ @
&vec+ texture ?&sampler*!rra. sampler, vec( ' Z, 'loat )ias[ @

'loat texture ?sampler*!rra.Shado# sampler$ vec+ '@
9se the te2ture coordinate ' to
do a te2ture loo8up in the
te2ture currentl. bound to
sampler. -he last component
o' ' is used as Dre* 'or the
shado# 'orms. 4or arra.
'orms, the arra. la.er comes
'rom the last component o' '
in the non;shado# 'orms, and
the second to last component
o' ' in the shado# 'orms.
&vec+ textureSroI ?&sampler*! sampler, vec( ' Z, 'loat )ias[ @
&vec+ textureSroI ?&sampler*! sampler, vec+ ' Z, 'loat )ias[ @
&vec+ textureSroI ?&sampler(! sampler, vec+ ' Z, 'loat )ias[ @

'loat textureSroI ?sampler*!Shado# sampler$ vec+ '
Z, 'loat )ias[ @
!o a te2ture loo8up #ith
proFection. -he te2ture
coordinates consumed 'rom ',
not includin& the last
component o' ', are divided
b. the last component o' ' to
'orm proFected coordinates '4.
-he resultin& third component
o' '4 in the shado# 'orms is
used as Dre*. -he third
component o' 3 is i&nored
#hen sampler has t.pe
&sampler*! and ' has t.pe
vec+. 'ter these values are
computed, te2ture loo8up
proceeds as in texture.
95
8 Built$in %unctions
S&nta) -escription
&vec+ texture>od ?&sampler*! sampler, vec* ', 'loat lo#@
&vec+ texture>od ?&sampler(! sampler, vec( ', 'loat lo#@
&vec+ texture>od ?&sampler%ube sampler, vec( ', 'loat lo#@

'loat texture>od ?sampler*!Shado# sampler$ vec( ', 'loat lo#@
&vec+ texture>od ?&sampler*!rra. sampler, vec( ', 'loat lo#@

!o a te2ture loo8up as in
texture but #ith e2plicit LO!M
lo# speci'ies F)ase and sets the
partial derivatives as 'ollo#s.
?See section (.5.6 I-e2ture
<ini'icationJ and eCuation
(.1+ in the OpenGL ES (.)
Graphics S.stem
Speci'ication.@
u
(
= )
v
(
= )
/
(
= )
u
y
= )
v
y
= )
/
y
= )
&vec+ textureRffset ?&sampler*! sampler, vec* ',
ivec* o**set Z, 'loat )ias[ @
&vec+ textureRffset ?&sampler(! sampler, vec( ',
ivec( o**set Z, 'loat )ias[ @


'loat textureRffset ?sampler*!Shado# sampler$ vec( ',
ivec* o**set Z, 'loat )ias[ @
&vec+ textureRffset ?&sampler*!rra. sampler, vec( ',
ivec* o**set Z, 'loat )ias[ @

!o a te2ture loo8up as in
texture but #ith o**set added
to the ?u,v,/@ te2el coordinates
be'ore loo8in& up each te2el.
-he o''set value must be a
constant e2pression. limited
ran&e o' o''set values are
supportedM the minimum and
ma2imum o''set values are
implementation;dependent and
&iven b.
<$:A3ROGR<A-EYELAO44SE- and
<YA3ROGR<A-EYELAO44SE-,
respectivel..
:ote that o**set does not appl.
to the la.er coordinate 'or
te2ture arra.s. -his is
e2plained in detail in section
(.5.6 I-e2ture <ini'icationJ
o' the OpenGL ES Graphics
S.stem Speci'ication, #here
o**set is
u
$
v
$
/
. :ote
that te2el o''sets are also not
supported 'or cube maps.
96
8 Built$in %unctions
S&nta) -escription
&vec+ texelLetAB ?&sampler*! sampler, ivec* ', int lo#@
&vec+ texelLetAB ?&sampler(! sampler, ivec( ', int lo#@
&vec+ texelLetAB ?&sampler*!rra. sampler, ivec( ', int lo#@
9se inte&er te2ture coordinate
' to loo8up a sin&le te2el 'rom
sampler. -he arra. la.er
comes 'rom the last component
o' ' 'or the arra. 'orms. -he
level;o';detail lo# is as
described in sections *.11.6
IShader E2ecutionJ under
-e2el 4etches and (.5
I-e2turin&J o' the OpenGL ES
(.) Graphics S.stem
Speci'ication.
&vec+ texelLetABRffset ?&sampler*! sampler, ivec* ', int lo#,
ivec* o**set@
&vec+ texelLetABRffset ?&sampler(! sampler, ivec( ', int lo#,
ivec( o**set@
&vec+ texelLetABRffset ?&sampler*!rra. sampler, ivec( ', int lo#,
ivec* o**set@
4etch a sin&le te2el as in
texelLetAB o''set b. o**set as
described in textureRffset.
&vec+ textureSroIRffset ?&sampler*! sampler, vec( ',
ivec* o**set Z, 'loat )ias[ @
&vec+ textureSroIRffset ?&sampler*! sampler, vec+ ',
ivec* o**set Z, 'loat )ias[ @
&vec+ textureSroIRffset ?&sampler(! sampler, vec+ ',
ivec( o**set Z, 'loat )ias[ @

'loat textureSroIRffset ?sampler*!Shado# sampler$ vec+ ',
ivec* o**set Z, 'loat )ias[ @
!o a proFective te2ture loo8up
as described in textureSroI
o''set b. o**set as described in
textureRffset.
&vec+ texture>odRffset ?&sampler*! sampler, vec* ',
'loat lo#, ivec* o**set@
&vec+ texture>odRffset ?&sampler(! sampler, vec( ',
'loat lo#, ivec( o**set@

'loat texture>odRffset ?sampler*!Shado# sampler$ vec( ',
'loat lo#, ivec* o**set@
&vec+ texture>odRffset ?&sampler*!rra. sampler, vec( ',
'loat lo#, ivec* o**set@

!o an o''set te2ture loo8up
#ith e2plicit LO!. See
texture>od and
textureRffset.
97
8 Built$in %unctions
S&nta) -escription
&vec+ textureSroI>od ?&sampler*! sampler, vec( ', 'loat lo#@
&vec+ textureSroI>od ?&sampler*! sampler, vec+ ', 'loat lo#@
&vec+ textureSroI>od ?&sampler(! sampler, vec+ ', 'loat lo#@
'loat textureSroI>od ?sampler*!Shado# sampler$ vec+ '$ 'loat lo#@
!o a proFective te2ture loo8up
#ith e2plicit LO!. See
textureSroI and texture>od.
&vec+ textureSroI>odRffset ?&sampler*! sampler, vec( ',
'loat lo#, ivec* o**set@
&vec+ textureSroI>odRffset ?&sampler*! sampler, vec+ ',
'loat lo#, ivec* o**set@
&vec+ textureSroI>odRffset ?&sampler(! sampler, vec+ ',
'loat lo#, ivec( o**set@

'loat textureSroI>odRffset ?sampler*!Shado# sampler$ vec+ ',
'loat lo#, ivec* o**set@
!o an o''set proFective te2ture
loo8up #ith e2plicit LO!. See
textureSroI, texture>od, and
textureRffset.
&vec+ texture=rad ?&sampler*! sampler$ vec* ',
vec* #'#(, vec* #'#y@
&vec+ texture=rad ?&sampler(! sampler$ vec( ',
vec( #'#(, vec( #'#y@
&vec+ texture=rad ?&sampler%ube sampler, vec( ',
vec( #'#(, vec( #'#y@

'loat texture=rad ?sampler*!Shado# sampler, vec( ',
vec* #'#(, vec* #'#y@
'loat texture=rad ?sampler%ubeShado# sampler, vec+ ',
vec( #'#(, vec( #'#y@
&vec+ texture=rad ?&sampler*!rra. sampler$ vec( ',
vec* #'#(, vec* #'#y@

'loat texture=rad ?sampler*!rra.Shado# sampler, vec+ ',
vec* #'#(, vec* #'#y@
!o a te2ture loo8up as in
texture but #ith e2plicit
&radients. -he partial
derivatives o' ' are #ith
respect to #indo# 2 and
#indo# .. Set
s
(
=
'.s
(
s
y
=
'.s
y
t
(
=
'.t
(
t
y
=
'.t
y
r
(
=
'.p
(
cube
r
y
=
'.p
y
cube
4or the cube version, the
partial derivatives o' ' are
assumed to be in the
coordinate s.stem used be'ore
te2ture coordinates are
proFected onto the appropriate
cube 'ace.
98
8 Built$in %unctions
S&nta) -escription
&vec+ texture=radRffset ?&sampler*! sampler, vec* ',
vec* #'#(, vec* #'#y$ ivec* o**set@
&vec+ texture=radRffset ?&sampler(! sampler, vec( ',
vec( #'#(, vec( #'#y$ ivec( o**set@

'loat texture=radRffset ?sampler*!Shado# sampler$ vec( ',
vec* #'#(, vec* #'#y$ ivec* o**set@
&vec+ texture=radRffset ?&sampler*!rra. sampler, vec( ',
vec* #'#(, vec* #'#y$ ivec* o**set@

'loat texture=radRffset ?sampler*!rra.Shado# sampler$ vec+ ',
vec* #'#(, vec* #'#y$ ivec* o**set@
!o a te2ture loo8up #ith both
e2plicit &radient and o''set, as
described in texture=rad and
textureRffset.
&vec+ textureSroI=rad ?&sampler*! sampler$ vec( ',
vec* #'#(, vec* #'#y@
&vec+ textureSroI=rad ?&sampler*! sampler$ vec+ ',
vec* #'#(, vec* #'#y@
&vec+ textureSroI=rad ?&sampler(! sampler$ vec+ ',
vec( #'#(, vec( #'#y@

'loat textureSroI=rad ?sampler*!Shado# sampler, vec+ ',
vec* #'#(, vec* #'#y@
!o a te2ture loo8up both
proFectivel., as described in
textureSroI, and #ith e2plicit
&radient as described in
texture=rad. -he partial
derivatives #'#( and #'#y are
assumed to be alread.
proFected.
&vec+ textureSroI=radRffset ?&sampler*! sampler$ vec( ',
vec* #'#(, vec* #'#y, ivec* o**set@
&vec+ textureSroI=radRffset ?&sampler*! sampler$ vec+ ',
vec* #'#(, vec* #'#y, ivec* o**set@
&vec+ textureSroI=radRffset ?&sampler(! sampler$ vec+ ',
vec( #'#(, vec( #'#y, ivec( o**set@

'loat textureSroI=radRffset ?sampler*!Shado# sampler, vec+ ',
vec* #'#(, vec* #'#y, ivec* o**set@
!o a te2ture loo8up
proFectivel. and #ith e2plicit
&radient as described in
textureSroI=rad, as #ell as
#ith o''set, as described in
textureRffset.
4.5 +ragment *rocessing +unctions
4ra&ment processin& 'unctions are onl. available in 'ra&ment shaders.
!erivatives ma. be computationall. e2pensive and>or numericall. unstable. -here'ore, an OpenGL ES
implementation ma. appro2imate the true derivatives b. usin& a 'ast but not entirel. accurate derivative
computation. !erivatives are unde'ined #ithin non;uni'orm control 'lo#.
-he e2pected behavior o' a derivative is speci'ied usin& 'or#ard>bac8#ard di''erencin&.
99
8 Built$in %unctions
4or#ard di''erencin&:
0 (#(0 ( ~#0#( (#( 1a
#0#( ( ~
0 (#(0 (
#(
1b
"ac8#ard di''erencin&:
0 (#(0 ( ~#0#( (#( *a
#0#( ( ~
0 (0 (#(
#(
*b
=ith sin&le;sample rasteriEation, #( GT 1.) in eCuations 1b and *b. 4or multi;sample rasteriEation, #( G
*.) in eCuations 1b and *b.
dLdy is appro2imated similarl., #ith y replacin& (.
n OpenGL ES implementation ma. use the above or other methods to per'orm the calculation, subFect to
the 'ollo#in& conditions:
1. -he method ma. use piece#ise linear appro2imations. Such linear appro2imations impl. that hi&her
order derivatives, dLdx?dLdx?(@@ and above, are unde'ined.
*. -he method ma. assume that the 'unction evaluated is continuous. -here'ore derivatives #ithin the
bod. o' a non;uni'orm conditional are unde'ined.
(. -he method ma. di''er per 'ra&ment, subFect to the constraint that the method ma. var. b. #indo#
coordinates, not screen coordinates. -he invariance reCuirement described in section (.* I$nvarianceJ
o' the OpenGL ES Graphics S.stem Speci'ication, is rela2ed 'or derivative calculations, because the
method ma. be a 'unction o' 'ra&ment location.
Other properties that are desirable, but not reCuired, are:
+. 4unctions should be evaluated #ithin the interior o' a primitive ?interpolated, not e2trapolated@.
/. 4unctions 'or dLdx should be evaluated #hile holdin& . constant. 4unctions 'or dLdy should be
evaluated #hile holdin& 2 constant. ,o#ever, mi2ed hi&her order derivatives, li8e dLdx?dLdy?y@@
and dLdy?dLdx?(@@ are unde'ined.
7. !erivatives o' constant ar&uments should be ).
$n some implementations, var.in& de&rees o' derivative accurac. ma. be obtained b. providin& GL hints
?section /.( I,intsJ o' the OpenGL ES (.) Graphics S.stem Speci'ication@, allo#in& a user to ma8e an
ima&e Cualit. versus speed trade o''.
100
8 Built$in %unctions
S&nta) -escription
&en-.pe dLdx ?&en-.pe p@ Returns the derivative in 2 usin& local di''erencin& 'or
the input ar&ument p.
&en-.pe dLdy ?&en-.pe p@ Returns the derivative in . usin& local di''erencin& 'or
the input ar&ument p.
-hese t#o 'unctions are commonl. used to estimate the
'ilter #idth used to anti;alias procedural te2tures. =e
are assumin& that the e2pression is bein& evaluated in
parallel on a S$<! arra. so that at an. &iven point in
time the value o' the 'unction is 8no#n at the &rid points
represented b. the S$<! arra.. Local di''erencin&
bet#een S$<! arra. elements can there'ore be used to
derive d4d2, d4d., etc.
&en-.pe f?idtB ?&en-.pe p@ Returns the sum o' the absolute derivative in 2 and .
usin& local di''erencin& 'or the input ar&ument p, i.e.,
a@s ?dLdx ?p@@ O a@s ?dLdy ?p@@M
101
5 Shading Language Grammar
-he &rammar is 'ed 'rom the output o' le2ical anal.sis. -he to8ens returned 'rom le2ical anal.sis are
HONS 5OOL FLO? IN KIN
5RE?N HONINKE !O ELSE FOR IF !ISH?R! REKRN SOIHP H?SE !EF?KL
5VEH8 5VEH; 5VEH7 IVEH8 IVEH; IVEH7 KVEH8 KVEH; KVEH7 VEH8 VEH; VEH7
@?8 @?; @?7 HENROI! IN OK INOK KNIFOR@
FL? S@OOP L?QOK
@?8R8 @?8R; @?8R7
@?;R8 @?;R; @?;R7
@?7R8 @?7R; @?7R7
S?@CLER8! S?@CLER;! S?@CLERHK5E S?@CLER8!SP?!OO
S?@CLERHK5ESP?!OO S?@CLER8!?RR?Q
S?@CLER8!?RR?QSP?!OO IS?@CLER8! IS?@CLER;! IS?@CLERHK5E
IS?@CLER8!?RR?Q KS?@CLER8! KS?@CLER;!
KS?@CLERHK5E KS?@CLER8!?RR?Q

SRKH VOI! OPILE
I!ENIFIER QCE_N?@E FLO?HONS?N INHONS?N KINHONS?N 5OOLHONS?N
FIEL!_SELEHION
LEF_OC RIGP_OC
INH_OC !EH_OC LE_OC GE_OC ES_OC NE_OC
?N!_OC OR_OC ROR_OC @KL_?SSIGN !IV_?SSIGN ?!!_?SSIGN
@O!_?SSIGN LEF_?SSIGN RIGP_?SSIGN ?N!_?SSIGN ROR_?SSIGN OR_?SSIGN
SK5_?SSIGN
LEF_C?REN RIGP_C?REN LEF_5R?HNE RIGP_5R?HNE LEF_5R?HE RIGP_5R?HE !O
HO@@? HOLON ESK?L SE@IHOLON 5?NG !?SP IL!E CLKS S?R SL?SP CERHEN
LEF_?NGLE RIGP_?NGLE VERIH?L_5?R H?RE ?@CERS?N! SKESION
INV?RI?N
PIGP_CREHISION @E!IK@_CREHISION LOO_CREHISION CREHISION
-he 'ollo#in& describes the &rammar 'or the OpenGL ES Shadin& Lan&ua&e in terms o' the above to8ens.
varia)le+i#enti*ier:
,D!-T,0,!R
primary+e(pression:
varia)le+i#enti*ier
102
9 "&adin' (an'ua'e )ra##ar
,-T%O-"TG-T
A,-T%O-"TG-T
0LOGT%O-"TG-T
BOOL%O-"TG-T
L!0T+'GR!- e(pression R,HT+'GR!-
post*i(+e(pression:
primary+e(pression
post*i(+e(pression L!0T+BRG%I!T integer+e(pression R,HT+BRG%I!T
*unction+call
post*i(+e(pression DOT 0,!LD+"!L!%T,O-
post*i(+e(pression ,-%+O'
post*i(+e(pression D!%+O'
integer+e(pression:
e(pression
*unction+call:
*unction+call+or+metho#
*unction+call+or+metho#:
*unction+call+generic
post*i(+e(pression DOT *unction+call+generic
*unction+call+generic:
*unction+call+hea#er+/ith+parameters R,HT+'GR!-
*unction+call+hea#er+no+parameters R,HT+'GR!-
*unction+call+hea#er+no+parameters:
*unction+call+hea#er VO,D
*unction+call+hea#er
*unction+call+hea#er+/ith+parameters:
*unction+call+hea#er assignment+e(pression
*unction+call+hea#er+/ith+parameters %O99G assignment+e(pression
*unction+call+hea#er:
*unction+i#enti*ier L!0T+'GR!-
103
9 "&adin' (an'ua'e )ra##ar
@@ rammar -ote: %onstructors loo& li&e *unctions$ )ut le(ical analysis recogni1e# most o* them as
@@ &ey/or#s. They are no/ recogni1e# through Jtype+speci*ierK.
@@ 9etho#s 5.length6 an# i#enti*iers are recogni1e# through post*i(+e(pression.
*unction+i#enti*ier:
type+speci*ier
,D!-T,0,!R
0,!LD+"!L!%T,O-
unary+e(pression:
post*i(+e(pression
,-%+O' unary+e(pression
D!%+O' unary+e(pression
unary+operator unary+e(pression
@@ rammar -ote: -o tra#itional style type casts.
unary+operator:
'LA"
DG"H
BG-
T,LD!
@@ rammar -ote: -o 4<4 or 4L4 unary ops. 'ointers are not supporte#.
multiplicative+e(pression:
unary+e(pression
multiplicative+e(pression "TGR unary+e(pression
multiplicative+e(pression "LG"H unary+e(pression
multiplicative+e(pression '!R%!-T unary+e(pression
a##itive+e(pression:
multiplicative+e(pression
a##itive+e(pression 'LA" multiplicative+e(pression
a##itive+e(pression DG"H multiplicative+e(pression
shi*t+e(pression:
a##itive+e(pression
shi*t+e(pression L!0T+O' a##itive+e(pression
shi*t+e(pression R,HT+O' a##itive+e(pression
104
9 "&adin' (an'ua'e )ra##ar
relational+e(pression:
shi*t+e(pression
relational+e(pression L!0T+G-L! shi*t+e(pression
relational+e(pression R,HT+G-L! shi*t+e(pression
relational+e(pression L!+O' shi*t+e(pression
relational+e(pression !+O' shi*t+e(pression
e2uality+e(pression:
relational+e(pression
e2uality+e(pression !M+O' relational+e(pression
e2uality+e(pression -!+O' relational+e(pression
an#+e(pression:
e2uality+e(pression
an#+e(pression G9'!R"G-D e2uality+e(pression
e(clusive+or+e(pression:
an#+e(pression
e(clusive+or+e(pression %GR!T an#+e(pression
inclusive+or+e(pression:
e(clusive+or+e(pression
inclusive+or+e(pression V!RT,%GL+BGR e(clusive+or+e(pression
logical+an#+e(pression:
inclusive+or+e(pression
logical+an#+e(pression G-D+O' inclusive+or+e(pression
logical+(or+e(pression:
logical+an#+e(pression
logical+(or+e(pression NOR+O' logical+an#+e(pression
logical+or+e(pression:
logical+(or+e(pression
logical+or+e(pression OR+O' logical+(or+e(pression
con#itional+e(pression:
logical+or+e(pression
logical+or+e(pression MA!"T,O- e(pression %OLO- assignment+e(pression
105
9 "&adin' (an'ua'e )ra##ar
assignment+e(pression:
con#itional+e(pression
unary+e(pression assignment+operator assignment+e(pression
assignment+operator:
!MAGL
9AL+G"",-
D,V+G"",-
9OD+G"",-
GDD+G"",-
"AB+G"",-
L!0T+G"",-
R,HT+G"",-
G-D+G"",-
NOR+G"",-
OR+G"",-
e(pression:
assignment+e(pression
e(pression %O99G assignment+e(pression
constant+e(pression:
con#itional+e(pression
#eclaration:
*unction+prototype "!9,%OLO-
init+#eclarator+list "!9,%OLO-
'R!%,",O- precision+2uali*ier type+speci*ier+no+prec "!9,%OLO-
type+2uali*ier ,D!-T,0,!R L!0T+BRG%! struct+#eclaration+list R,HT+BRG%! "!9,%OLO-
type+2uali*ier ,D!-T,0,!R L!0T+BRG%! struct+#eclaration+list R,HT+BRG%!
,D!-T,0,!R "!9,%OLO-
type+2uali*ier ,D!-T,0,!R L!0T+BRG%! struct+#eclaration+list R,HT+BRG%!
,D!-T,0,!R L!0T+BRG%I!T constant+e(pression R,HT+BRG%I!T "!9,%OLO-
type+2uali*ier "!9,%OLO-
*unction+prototype:
*unction+#eclarator R,HT+'GR!-
106
9 "&adin' (an'ua'e )ra##ar
*unction+#eclarator:
*unction+hea#er
*unction+hea#er+/ith+parameters
*unction+hea#er+/ith+parameters:
*unction+hea#er parameter+#eclaration
*unction+hea#er+/ith+parameters %O99G parameter+#eclaration
*unction+hea#er:
*ully+speci*ie#+type ,D!-T,0,!R L!0T+'GR!-
parameter+#eclarator:
type+speci*ier ,D!-T,0,!R
type+speci*ier ,D!-T,0,!R L!0T+BRG%I!T constant+e(pression R,HT+BRG%I!T
parameter+#eclaration:
parameter+type+2uali*ier parameter+2uali*ier parameter+#eclarator
parameter+2uali*ier parameter+#eclarator
parameter+type+2uali*ier parameter+2uali*ier parameter+type+speci*ier
parameter+2uali*ier parameter+type+speci*ier
parameter+2uali*ier:
@< empty <@
,-
OAT
,-OAT
parameter+type+speci*ier:
type+speci*ier
init+#eclarator+list:
single+#eclaration
init+#eclarator+list %O99G ,D!-T,0,!R

init+#eclarator+list %O99G ,D!-T,0,!R L!0T+BRG%I!T constant+e(pression
R,HT+BRG%I!T
init+#eclarator+list %O99G ,D!-T,0,!R L!0T+BRG%I!T
R,HT+BRG%I!T !MAGL initiali1er
init+#eclarator+list %O99G ,D!-T,0,!R L!0T+BRG%I!T constant+e(pression
R,HT+BRG%I!T !MAGL initiali1er
107
9 "&adin' (an'ua'e )ra##ar
init+#eclarator+list %O99G ,D!-T,0,!R !MAGL initiali1er
single+#eclaration:
*ully+speci*ie#+type
*ully+speci*ie#+type ,D!-T,0,!R

*ully+speci*ie#+type ,D!-T,0,!R L!0T+BRG%I!T constant+e(pression R,HT+BRG%I!T
*ully+speci*ie#+type ,D!-T,0,!R L!0T+BRG%I!T R,HT+BRG%I!T !MAGL initiali1er
*ully+speci*ie#+type ,D!-T,0,!R L!0T+BRG%I!T constant+e(pression
R,HT+BRG%I!T !MAGL initiali1er
*ully+speci*ie#+type ,D!-T,0,!R !MAGL initiali1er
,-VGR,G-T ,D!-T,0,!R
@@ rammar -ote: -o 4enum4$ or 4type#e*4.
*ully+speci*ie#+type:
type+speci*ier
type+2uali*ier type+speci*ier
invariant+2uali*ier:
,-VGR,G-T
interpolation+2uali*ier:
"9OOTH
0LGT

layout+2uali*ier:
LGOOAT L!0T+'GR!- layout+2uali*ier+i#+list R,HT+'GR!-
layout+2uali*ier+i#+list:
layout+2uali*ier+i#
layout+2uali*ier+i#+list %O99G layout+2uali*ier+i#
layout+2uali*ier+i#:
,D!-T,0,!R
,D!-T,0,!R !MAGL ,-T%O-"TG-T
,D!-T,0,!R !MAGL A,-T%O-"TG-T
parameter+type+2uali*ier:
%O-"T
108
9 "&adin' (an'ua'e )ra##ar
type+2uali*ier:
storage+2uali*ier
layout+2uali*ier
layout+2uali*ier storage+2uali*ier
interpolation+2uali*ier storage+2uali*ier
interpolation+2uali*ier
invariant+2uali*ier storage+2uali*ier
invariant+2uali*ier interpolation+2uali*ier storage+2uali*ier
storage+2uali*ier:
%O-"T
,-
OAT
%!-TRO,D ,-
%!-TRO,D OAT
A-,0OR9
type+speci*ier:
type+speci*ier+no+prec
precision+2uali*ier type+speci*ier+no+prec
type+speci*ier+no+prec:
type+speci*ier+nonarray
type+speci*ier+nonarray L!0T+BRG%I!T R,HT+BRG%I!T
type+speci*ier+nonarray L!0T+BRG%I!T constant+e(pression R,HT+BRG%I!T
type+speci*ier+nonarray:
VO,D
0LOGT
,-T
A,-T
BOOL
V!%2
V!%3
V!%:
BV!%2
BV!%3
109
9 "&adin' (an'ua'e )ra##ar
BV!%:
,V!%2
,V!%3
,V!%:
AV!%2
AV!%3
AV!%:
9GT2
9GT3
9GT:
9GT2N2
9GT2N3
9GT2N:
9GT3N2
9GT3N3
9GT3N:
9GT:N2
9GT:N3
9GT:N:
"G9'L!R2D
"G9'L!R3D
"G9'L!R%AB!
"G9'L!R2D"HGDOP
"G9'L!R%AB!"HGDOP
"G9'L!R2DGRRGO
"G9'L!R2DGRRGO"HGDOP
,"G9'L!R2D
,"G9'L!R3D
,"G9'L!R%AB!
,"G9'L!R2DGRRGO
A"G9'L!R2D
A"G9'L!R3D
A"G9'L!R%AB!
A"G9'L!R2DGRRGO
struct+speci*ier
TO'!+-G9!
110
9 "&adin' (an'ua'e )ra##ar
precision+2uali*ier:
H,H+'R!%,",O-
9!D,A9+'R!%,",O-
LOP+'R!%,",O-
struct+speci*ier:
"TRA%T ,D!-T,0,!R L!0T+BRG%! struct+#eclaration+list R,HT+BRG%!
"TRA%T L!0T+BRG%! struct+#eclaration+list R,HT+BRG%!
struct+#eclaration+list:
struct+#eclaration
struct+#eclaration+list struct+#eclaration
struct+#eclaration:
type+speci*ier struct+#eclarator+list "!9,%OLO-
type+2uali*ier type+speci*ier struct+#eclarator+list "!9,%OLO-
struct+#eclarator+list:
struct+#eclarator
struct+#eclarator+list %O99G struct+#eclarator
struct+#eclarator:
,D!-T,0,!R
,D!-T,0,!R L!0T+BRG%I!T R,HT+BRG%I!T
,D!-T,0,!R L!0T+BRG%I!T constant+e(pression R,HT+BRG%I!T
initiali1er:
assignment+e(pression
#eclaration+statement:
#eclaration
statement:
compoun#+statement+/ith+scope
simple+statement
statement+no+ne/+scope:
compoun#+statement+no+ne/+scope
simple+statement
statement+/ith+scope:
111
9 "&adin' (an'ua'e )ra##ar
compoun#+statement+no+ne/+scope
simple+statement
@@ rammar -ote: la)ele# statements *or "P,T%H only3 4goto4 is not supporte#.
simple+statement:
#eclaration+statement
e(pression+statement
selection+statement
s/itch+statement
case+la)el
iteration+statement
;ump+statement
compoun#+statement+/ith+scope:
L!0T+BRG%! R,HT+BRG%!
L!0T+BRG%! statement+list R,HT+BRG%!
compoun#+statement+no+ne/+scope:
L!0T+BRG%! R,HT+BRG%!
L!0T+BRG%! statement+list R,HT+BRG%!
statement+list:
statement
statement+list statement
e(pression+statement:
"!9,%OLO-
e(pression "!9,%OLO-
selection+statement:
,0 L!0T+'GR!- e(pression R,HT+'GR!- selection+rest+statement
selection+rest+statement:
statement+/ith+scope !L"! statement+/ith+scope
statement+/ith+scope
con#ition:
e(pression
*ully+speci*ie#+type ,D!-T,0,!R !MAGL initiali1er
112
9 "&adin' (an'ua'e )ra##ar
s/itch+statement:
"P,T%H L!0T+'GR!- e(pression R,HT+'GR!- L!0T+BRG%! s/itch+statement+list
R,HT+BRG%!
s/itch+statement+list:
@< nothing <@
statement+list
case+la)el:
%G"! e(pression %OLO-
D!0GALT %OLO-
iteration+statement:
PH,L! L!0T+'GR!- con#ition R,HT+'GR!- statement+no+ne/+scope
DO statement+/ith+scope PH,L! L!0T+'GR!- e(pression R,HT+'GR!- "!9,%OLO-
0OR L!0T+'GR!- *or+init+statement *or+rest+statement R,HT+'GR!-
statement+no+ne/+scope
*or+init+statement:
e(pression+statement
#eclaration+statement
con#itionopt:
con#ition
@< empty <@
*or+rest+statement:
con#itionopt "!9,%OLO-
con#itionopt "!9,%OLO- e(pression
;ump+statement:
%O-T,-A! "!9,%OLO-
BR!GI "!9,%OLO-
R!TAR- "!9,%OLO-
R!TAR- e(pression "!9,%OLO-
D,"%GRD "!9,%OLO- @@ 0ragment sha#er only.
@@ rammar -ote: -o 4goto4. otos are not supporte#.
translation+unit:
e(ternal+#eclaration
translation+unit e(ternal+#eclaration
113
9 "&adin' (an'ua'e )ra##ar
e(ternal+#eclaration:
*unction+#e*inition
#eclaration
*unction+#e*inition:
*unction+prototype compoun#+statement+no+ne/+scope
$n &eneral the above &rammar describes a super set o' the GLSL ES lan&ua&e. %ertain constructs that are
valid purel. in terms o' the &rammar are disallo#ed b. statements else#here in this speci'ication.
Rules speci'.in& the scopin& are present onl. to assist the understandin& o' scopin& and the. do not a''ect
the lan&ua&e accepted b. the &rammar. $' reCuired, the &rammar can be simpli'ied b. ma8in& the
'ollo#in& substitutions:
Replace compoun#+statement+/ith+scope and compoun#+statement+no+ne/+scope #ith a ne#
rule compoun#+statement
Replace statement+/ith+scope and statement+no+ne/+scope #ith the e2istin& rule statement.
114
10 rrors
10 Errors
-his section lists errors that must be detected b. the compiler or lin8er. !evelopment s.stems must
report all &rammatical errors are compile time but other#ise, it is implementation;dependent #hether an
error is reported at compile time or lin8 time and there is no &uarantee o' consistenc..
-he error strin& returned is implementation;dependent.
10.1 *reprocessor Errors
3)))1: 3reprocessor s.nta2 error
3)))*: Nerror
3)))(: Ne2tension i' a reCuired e2tension e2tensionAname is not supported, or i' all is speci'ied.
3)))/: $nvalid Nversion construct
3)))7: Nline has #ron& parameters
3)))0: Lan&ua&e version not supported
3)))5: 9se o' unde'ined macro
3)))6: <acro name too lon&
10.! Le)er>*arser Errors
Grammatical errors occurs #henever the &rammar rules are not 'ollo#ed. -he. are not listed individuall.
here.
L)))1: S.nta2 error
-he parser also detects the 'ollo#in& errors:
L)))*: 9nde'ined identi'ier.
L)))(: 9se o' reserved 8e.#ords
L)))+: $denti'ier too lon&
L)))/: $nte&er constant too lon&
10.3 Semantic Errors
S)))1: -.pe mismatch in e2pression e.&. 1 O 1.)
S)))*: rra. parameter must be an inte&er
S)))(: %onditional Fump parameter ?if, for, ?Bile, do;?Bile@ must be a boolean
115
10 rrors
S)))+: Operator not supported 'or operand t.pes ?e.&. mat+ R vec(@
S)))/: 75 parameter must be a boolean
S)))7: *nd and (rd parameters o' 75 must have the same t.pe
S)))0: =ron& ar&uments 'or constructor
S)))5: r&ument unused in constructor
S)))6: -oo 'e# ar&uments 'or constructor
S))11: r&uments in #ron& order 'or structure constructor
S))1*: E2pression must be a constant e2pression
S))1(: $nitialiEer 'or constant variable must be a constant e2pression
S))1/: E2pression must be a constant inte&ral e2pression
S))10: rra. siEe must be &reater than Eero
S))15: rra. siEe not de'ined
S))*): $nde2in& an arra. #ith a constant inte&ral e2pression &reater than its declared siEe
S))*1: $nde2in& an arra. #ith a ne&ative constant inte&ral e2pression
S))**: Rede'inition o' variable in same scope
S))*(: Rede'inition o' 'unction in same scope
S))*+: Rede'inition o' name in same scope ?e.&. declarin& a 'unction #ith the same name as a struct@
S))*/: 4ield selectors must be 'rom the same set ?cannot mi2 2.E# #ith r&ba@
S))*7: $lle&al 'ield selector ?e.&. usin& .E #ith a veAD@
S))*0: -ar&et o' assi&nment is not an l;value
S))*5: 3recision used #ith t.pe other than int, float or sampler t.pe
S))*6: !eclarin& a main 'unction #ith the #ron& si&nature or return t.pe
S))(1: Aonst variable does not have initialiEer
S))(*: 9se o' float or int #ithout a precision Cuali'ier #here the de'ault precision is not de'ined
S))((: E2pression that does not have an intrinsic precision #here the de'ault precision is not de'ined
S))(+: 1ariable cannot be declared invariant
116
10 rrors
S))(/: ll uses o' invariant must be at the &lobal scope
S))(0: L;value contains duplicate components ?e.&. v.22 T CM@
S))(5: 4unction declared #ith a return value but return statement has no ar&ument
S))(6: 4unction declared void but return statement has an ar&ument
S))+): 4unction declared #ith a return value but not all paths return a value
S))+*: Return t.pe o' 'unction de'inition must match return t.pe o' 'unction declaration.
S))+(: 3arameter Cuali'iers o' 'unction de'inition must match parameter Cuali'iers o' 'unction
declaration.
S))+/: !eclarin& an input inside a 'unction
S))+7: !eclarin& a uni'orm inside a 'unction
S))+0: !eclarin& an output inside a 'unction
S))+5: $lle&al data t.pe 'or verte2 output or 'ra&ment input
S))+6: $lle&al data t.pe 'or verte2 input ?can onl. use float, 'loatin&;point vectors, matrices, si&ned and
unsi&ned inte&ers and inte&er vectors@
S))/): $nitialiEer 'or input
S))/1: $nitialiEer 'or output
S))/*: $nitialiEer 'or uni'orm
S))/(: Static recursion present
S))/+: Overloadin& built;in 'unctions not allo#ed.
S))//: 1erte2 output #ith inte&er t.pe must be declared as 'lat
S))/7: 4ra&ment intput #ith inte&er t.pe must be declared as 'lat
S))/0: init.e(pression in s#itch statement must be a scalar inte&er
S))/5: $lle&al data t.pe 'or 'ra&ment output
S))/6: $nvalid la.out Cuali'ier
S))7): $nvalid use o' la.out Cuali'ier ?e.&. on verte2 shader outputs or 'ra&ment shader inputs@
10.% Lin1er
L)))1: Global variables must have the same t.pe ?includin& the same names 'or structure and 'ield
names and the same siEe 'or arra.s@ and precision.
L)))(: -oo man. verte2 input values
L)))+: -oo man. verte2 output values
117
10 rrors
L)))/: -oo man. uni'orm values
L)))7: -oo man. verte2 output values
L)))0: 4ra&ment shader uses an input #here there is no correspondin& verte2 output
L)))5: -.pe mismatch bet#een verte2 output and 'ra&ment input
L)))6: <issin& main 'unction 'or shader
118
11 *ountin' o+ Inputs and Outputs
11 Counting of Inputs and Outputs
-his section applies to verte2 shader outputs and 'ra&ment shader inputs.
GLSL ES (.) speci'ies the stora&e available 'or verte2 shader outputs and 'ra&ment shader inputs in
terms o' an arra. o' +;vectors. -he assumption is that variables #ill be pac8ed into these arra.s #ithout
#astin& space. -his places si&ni'icant burden on implementations since optimal pac8in& is
computationall. intensive. $mplementations ma. have more internal resources than e2posed to the
application and so avoid the need to per'orm pac8in& but this is also considered an e2pensive solution.
GLSL ES (.) there'ore rela2es the reCuirements 'or pac8in& b. speci'.in& a simpler al&orithm that ma.
be used. -his al&orithm speci'ies a minimum reCuirement 'or #hen a set o' variables must be supported
b. an implementation. -he implementation is allo#ed to support more than the minimum and so ma. use
a more e''icient al&orithm and>or ma. support more re&isters than the virtual tar&et machine.
$n all cases, 'ailin& resource allocation 'or variables must result in an error.
-he resource allocation o' variables must succeed 'or all cases #here the 'ollo#in& pac8in& al&orithm
succeeds:
-he tar&et architecture consists o' a &rid o' re&isters, 17 ro#s b. + columns 'or verte2 output and
'ra&ment input variables. Each re&ister can contain a 'loat value.
1ariables are pac8ed into the re&isters one at a time so that the. each occup. a conti&uous sub;
rectan&le. :o splittin& o' variables is permitted.
-he orientation o' variables is 'i2ed. 1ectors al#a.s occup. re&isters in a sin&le ro#. Elements
o' an arra. must be in di''erent ro#s. E.&. vec+ #ill al#a.s occup. one ro#M 'loatZ17[ #ill
occup. one column. Since it is not permitted to split a variable, lar&e arra.s e.&. 'loatZ(*[ #ill
al#a.s 'ail #ith this al&orithm.
:on;sCuare matrices o' t.pe mat%2R consume the same space as a sCuare matri2 o' t.pe mat:
#here : is the &reater o' % and R. 1ariables o' t.pe mat* occupies * complete ro#s. -hese
rules allo# implementations more 'le2ibilit. in ho# variables are stored.
Other variables consume onl. the minimum space reCuired.
rra.s o' siEe : are assumed to ta8e : times the siEe o' the base t.pe.
119
11 *ountin' o+ Inputs and Outputs
1ariables are pac8ed in the 'ollo#in& order:
1. rra.s o' mat+ and mat+
*. rra.s o' mat* and mat* ?since the. occup. 'ull ro#s@
(. rra.s o' vec+ and vec+
+. rra.s o' mat( and mat(
/. rra.s o' vec( and vec(
7. rra.s o' vec* and vec*
0. rra.s o' 'loat and 'loat
4or each o' the above t.pes, the arra.s are processed in order o' siEe, lar&est 'irst. rra.s o' siEe
1 and the base t.pe are considered eCuivalent. -he 'irst t.pe to be pac8ed #ill be mat+Z+[,
mat+Z([, matZ*[ 'ollo#ed b. mat+, mat*Z+[...mat*Z*[, mat*, vec+Z5[, ve+Z0[,...vec+Z1[, vec+,
mat(Z*[, mat( and so on. -he last variables to be pac8ed #ill be 'loat ?and 'loatZ1[@.
4or *,( and + component variables pac8in& is started usin& the 1
st
column o' the 1
st
ro#.
1ariables are then allocated to successive ro#s, ali&nin& them to the 1
st
column.
4or * component variables, #hen there are no spare ro#s, the strate&. is s#itched to usin& the
hi&hest numbered ro# and the lo#est numbered column #here the variable #ill 'it. ?$n practice,
this means the. #ill be ali&ned to the 2 or E component.@ 3ac8in& o' an. 'urther ( or +
component variables #ill 'ail at this point.
1 component variables ?i.e. 'loats and arra.s o' 'loats@ have their o#n pac8in& rule. -he. are
pac8ed in order o' siEe, lar&est 'irst. Each variable is placed in the column that leaves the least
amount o' space in the column and ali&ned to the lo#est available ro#s #ithin that column.
!urin& this phase o' pac8in&, space #ill be available in up to + columns. -he space #ithin each
column is al#a.s conti&uous.
$' at an. time the pac8in& o' a variable 'ails, the compiler or lin8er must report an error.
120
11 *ountin' o+ Inputs and Outputs
E2ample: pac8 the 'ollo#in& t.pes:
out ve'7 a- && top left
out mat; $- && align to left0 lo.est num$ered ro.s
out mat8x; '- && same si"e as mat;0 align to left
out ve'8 dALB- && align to left0 lo.est num$ered ro.s
out ve'8 eA7B- && Hannot align to left so align to " 'olumn0 (ig(est
&& num$ered ro.s
out ve'8 f- && ?lign to left0 lo.est num$ered ro.s,
out float gA;B && Holumn .it( minimum spa'e
out float (A8B- && Holumn .it( minimum spa'e #'(oi'e of ;0 an9
&& 'an $e used)
out float i- && Holumn .it( minimum spa'e
$n this e2ample, the variables happen to be listed in the order in #hich the. are pac8ed. 3ac8in& is
independent o' the order o' declaration.
) & ? #
0 a a a a
G @ @ @
D @ @ @
8 @ @ @
E A A A
X A A A
Y A A A
Z d d g
[ d d g
" d d g
G! d d
GG d d
GD d d e e
G8 f f e e
GE B i e e
GX B e e
Some t.pes e.&. mat+Z5[ #ill be too lar&e to 'it. -hese al#a.s 'ail #ith this al&orithm.
$' re'erenced in the 'ra&ment shader ?a'ter preprocessin&@, the built;in special variables ?&lA4ra&%oord,
&lA4ront4acin& and &lA3oint%oord@ are included #hen calculatin& the stora&e reCuirements o' 'ra&ment
inputs.
121
11 *ountin' o+ Inputs and Outputs
1erte2 outputs and 'ra&ment inputs are counted separatel.. -he. are onl. counted i' the. are staticall.
used #ithin the shader.
122
12 Issues
1! Issues
1!.1 Compatibilit& #ith OpenGL ES !.0
,o# should OpenGL ES (.) support shaders #ritten 'or OpenGL ES *.)B
Option 1: Retain all GLSL ES 1.) constructs in the ne# lan&ua&e.
Option *: llo# GLSL ES 1.) shaders to run in the OpenGL ES (.) 3$.
RESOL9-$O:: Option *. -his minimiEes the comple2it. o' the lan&ua&e #ith onl. a small increase in
s.stem comple2it.. $t also leaves open the option o' deprecatin& the old lan&ua&e in 'uture versions o'
the 3$.
1!.! Conergence #ith OpenGL
,o# much should GLSL ES be in'luenced b. the GLSL speci'icationB
OpenGL ES (.) is principall. tar&eted at mobile devices such as smartphones and tablets. s such, it is
e2pected that the maFor use;cases #ill include &amin& and user;inter'aces. $t is to be e2pected that
content #ill be ported to and 'rom des8top devices.
RESOL9-$O:: $n the absence o' an. other reCuirements, GLSL ES (.) should 'ollo# GLSL (.(. -he
main e2ceptions to this are:
-he speci'ication should adhere to the principle that 'unctionalit. should not be duplicated.
4unctionalit. speci'ic to mobile devices ?such as reduced precision@ can be added.
$mprovements 'ound in later versions o' GLSL can be considered 'or inclusion.
1!.3 7umeric *recision
Should the Open GL ES *.) precision reCuirements be increasedB
<ost current implementations support a subset o' $EEE 0/+ (*;bit 'loatin& point. <an. implementations
also support reduced precision.
RESOL9-$O:S:
hi&hp 'loat should be speci'ied as a subset o' $EEE 0/+ 'loatin& point.
hi&hp int should be e2actl. (* bits.
lo#p and mediump should be retained. <ediump to have increased precision.
Should there be a de'ined 'ormat 'or mediumpB
Option: `es, this #ould increase portabilit. and encoura&e the use o' mediump on mobile devices.
123
12 Issues
Option: :o, this #ould be e2pensive to implement on devices that do not nativel. support it.
RESOL9-$O:: :o. -he speci'ication should allo# e''icient implementation o' mediump 'loat on 17;bit
'loatin& point hard#are but must also be implementable on devices #hich onl. nativel. support (*;bit
'loatin& point.
Should the 'ra&ment shader have a de'ault precisionB
1erte2 shaders have a de'ault hi&h precision because lo#er precisions are not su''icient 'or the maForit. o'
&raphics applications. ,o#ever, man. 'ra&ment shader operations do not bene'it 'rom hi&h precision and
developers should be encoura&ed to use lo#er precision #here possible as this ma. increases per'ormance
or reduce po#er consumption. $n particular, blend operations normall. onl. reCuire lo# precision and
man. te2ture address calculations can be per'ormed at medium precision.
,o#ever OpenGL ES ma. also be used in hi&her per'ormance devices #here the bene'it is limited.
-here'ore there appears to be no sin&le precision that #ould be applicable to all situations.
RESOL9-$O:::o, there #ill be no de'ault precision 'or 'ra&ment shaders.
1!.% +loating *oint :epresentation and +unctionalit&
Should $EEE 0/+ representation be mandatedB
-he internal 'ormat used b. an implementation mi&ht not be visible to an application so it is meanin&less
to speci'. this. %ertain 'unctionalit. $EEE 0/+ must be present thou&h.
RESOL9-$O:: $n &eneral, hi&hp 'loat must behave as i* it is in $EEE 0/+ 'ormat.
=hich 'eatures should be mandatedB
<ost o' the $EEE 0/+ is relativel. ine2pensive to implement &iven that (*;bit 'loatin& point is a
reCuirement. ,o#ever some implementations do not implement si&ned Eeros, roundin& modes and :a:s
because o' hard#are cost. $n addition, there are certain compiler optimiEations that the $EEE 0+/
speci'ication prohibits.
RESOL9-$O:: <andate support o' si&ned in'inities. Support o' si&ned Eeros, :a:s.
Should the support o' :a:s be consistentB
Should the speci'ication allo# either 'ull $EEE :a: support or no support but nothin& in bet#eenB
RESOL9-$O:: :o, implementations ma. have partial support and there is no &uarantee o' consistenc..
-he onl. reCuirement is that isnan?@ must return 'alse i' :a:s are not supported.
Should subnormal numbers ?also 8no#n as DdenormsD@ be supportedB
RESOL9-$O:: :o, subnormal numbers ma.be 'lushed to Eero at an. time.
124
12 Issues
,o# should the roundin& mode be speci'iedB
<ost current implementations support round;to;nearest. Some but not all also support round;to;nearest;
even.
RESOL9-$O:: =ithin the accurac. speci'ication, the roundin& mode should be unde'ined.
Should there be &eneral invariance rules 'or numeric 'ormats and operationsB
-he GLSL ES speci'ication allo#s the implementation a de&ree o' 'le2ibilit.. %onseCuentl. the results o'
a computation ma. be di''erent on di''erent implementations. ,o#ever, it is not stated #hether a sin&le
implementation is allo#ed to var. the results o' a &iven computation, either in di''erent shaders or
di''erent parts o' the same shader. OpenGL has a &eneral invariance rule that prevents the results o' a
computation var.in& i' no state ?includin& the choice o' shader@ is unchan&ed.
RESOL9-$O:: Operations and 'ormats are in &eneral considered to be variant.
1!.' *recision ;ualifiers
Should the precisions be speci'ied as 'loat17, 'loat(* etc.B -his #ould help portabilit.. $t implies
di''erent t.pes rather than hints. $t #ill reCuire all implementations to use the same or similar al&orithms
and reduces the scope 'or innovation.
RESOL9-$O:: :o, the precision should not speci'. a 'ormat. StandardiEed arithmetic is not ?.et@ a
reCuirement 'or &raphics.
!o inte&ers have precision Cuali'iersB OpenGL ES (.) hard#are is e2pected to have native inte&er
support and some implementations ma. have reduced precision available.
RESOL9-$O:: `es, inte&ers have precision Cuali'iers.
,o# should #rappin& behavior o' inte&ers be de'inedB $' an application relies on #rappin& on one
implementation this ma. cause portabilit. problems.
Option: -he standard should speci'. either #rappin& or clampin&. -his allo#s 'or ma2imum
implementation 'le2ibilit..
Option: <andate #rappin&. -here is a trend to#ards more comple2 shaders and developers #ill e2pect
inte&ers to behave as in %OO.
RESOL9-$O:: <andate #rappin&.
re precision Cuali'iers available in the verte2 shaderB
RESOL9-$O:: `es. Reduced precision ma. be available in the verte2 shader in some implementations
and it 8eeps the lan&ua&es consistent.
125
12 Issues
Should di''erent precisions create di''erent t.pes and e.&. reCuire e2plicit conversion bet#een themB
Option1: :o, the. are Fust hints. "ut hintin& hi&h precision is meanin&less i' the implementation can
i&nore it.
Option *: `es the. are di''erent t.pes. "ut this introduces comple2it..
RESOL9-$O:: -he precision Cuali'ier can si&ni'icantl. a''ect behavior in man. implementations.
BigBp means (*;bit $EEE 0+( 'loatin& pointis used but mediump means that at least medium precision is
used ?and similarl. 'or lo?p, so precision Cuali'iers are more than Fust hints. s 'ar as the lan&ua&e is
concerned it doesnDt a''ect the behavior so the. can either be considered as hints or as di''erent t.pes #ith
implicit t.pe conversion. $n an. case, implementations are 'ree to calculate ever.thin& at hi&h precision.
Should precisions be considered #hen resolvin& 'unction callsB
RESOL9-$O:: :o, the. should be considered more as hints. 4unction declarations cannot be
overloaded based on precision.
,o# should precisions be propa&ated in an e2pressionB
Option 1: Onl. consider the inputs to an operation. 4or operands that have no de'ined precision,
determination o' precision starts at the lea' nodes o' the e2pression tree and proceeds to the root until the
precision is 'ound. $' necessar. this includes the l;value in an assi&nment. %onstant e2pressions must be
invariant and it is e2pected that the. #ill be evaluated at compile time. -here'ore the. must be evaluated
at the hi&hest precision ?either lo?p or BigBp@ supported b. the tar&et, or above.
Option *: l#a.s ta8e the tar&et o' the e2pression into account. -he compiler should be able to #or8 out
ho# to avoid losin& precision.
RESOL9-$O:: Option 1. -his ma8es it easier 'or the developer to speci'. #hich precisions are used in
a comple2 e2pression.
=hat i' there is no precision in an e2pressionB
Option 1: Leave this as unde'ined.
Option *: 9se the de'ault precision.
RESOL9-$O:: 9se the de'ault precision. $t is an error i' this is not de'ined ?in the 'ra&ment shader@.
!o precision Cuali'iers 'or uni'orms need to matchB
Option 1: `es.
9ni'orms are de'ined to behave as i' the. are usin& the same stora&e in the verte2 and 'ra&ment processors
and ma. be implemented this #a..
$' uni'orms are used in both the verte2 and 'ra&ment shaders, developers should be #arned i' the
precisions are di''erent. %onversion o' precision should never be implicit.
126
12 Issues
Option *: :o.
9ni'orms ma. be used b. both shaders but the same precision ma. not be available in both so there is a
Fusti'ication 'or allo#in& them to be di''erent.
9sin& the same uni'orm in the verte2 and 'ra&ment shaders #ill al#a.s reCuire the precision to be
speci'ied in the verte2 shader ?since the de'ault precision is hi&hp@. -his is an unnecessar. burden on
developers.
RESOL9-$O:: `es, precision Cuali'iers 'or uni'orms must match.
!o precision Cuali'iers 'or verte2 outputs and the correspondin& 'ra&ment inputs ?previousl. 8no#n as
Dvar.in&sD@ need to matchB
Option 1: `es. 1ar.in&s are #ritten b. the verte2 shader and read b. the 'ra&ment shader so there are no
situations #here the precision needs to be di''erent.
Option *: :o, the verte2 outputs #ritten b. the verte2 shader should not be considered to be the same
variables as those read b. the 'ra&ment shader ?there can be no shared stora&e@. ,ence the. can be
speci'ied to have di''erent precisions.
RESOL9-$O: 3recision Cuali'iers 'or verte2 outputs and 'ra&ment inputs do not need to match.
lo?p int
lo?p float has a ran&e o' O>; *.) but lo?p int has a ran&e o' O>; */7. -his becomes problematic i'
conversion 'orm lo?p float to lo?p int is reCuired. !irect conversion i.e. lo?p int T int?lo?p float@
loses almost all the precision and multipl.in& be'ore conversion e.&. lo?p int T int?lo?p float R */7@
causes an over'lo# and hence an unde'ined result. -he onl. #a. to maintain precision is to 'irst convert
to mediump float.
Option 1: Keep this behavior. ccept that conversion o' lo?p float to lo? int loses precision and is
there'ore not use'ul.
Options *: <a8e lo?p int consistent #ith mediump and BigBp int b. settin& its ran&e to O>; 1
Options (: Rede'ine the conversion o' lo?p float to lo?p int to include an 5;bit le't shi't. -he
conversion o' lo?p int to lo?p float then contains an 5;bit ri&ht shi't.
Option +: Option 1 but add built;in 'unctions to shi't;convert bet#een the t#o 'ormats.
Option /: Rede'ine the lo?p float to be a true 'loatin& point 'ormat. $t #ould then be eCuivalent to a
'loatin& point value #ith a 1) bit mantissa and a ( bit unsi&ned e2ponent.
RESOL9-$O:: Option 1 %onversion #ill lose most o' the precision.
3recision o' built;in te2ture 'unctions.
127
12 Issues
<ost built;in 'unctions ta8e a sin&le parameter and it is sensible 'or the precision o' the return value to be
the same as the precision o' the parameter. -he te2ture 'unctions ta8e sampler and coordinate parameters.
-he return value should be completel. independent o' the precision o' the coordinates. ,o# should the
precision o' the return value be speci'iedB
RESOL9-$O:: llo# sampler t.pes to ta8e a precision Cuali'ier. -he return value o' the te2ture
'unctions have the same precision as the precision o' the sampler parameter.
=hat should the de'ault precision o' sampler t.pes beB
Option 1: lo?p. -his #ill be 'aster on some implementations. $n &eneral, OpenGL ES should de'ault to
'ast operation rather than precise operation. $t is usuall. easier to detect and correct a 'unctional error
than a per'ormance issue.
Option *: lo?p 'or te2tures that are e2pected to contain color values. BigBp 'or te2tures that are e2pected
to contain other values e.&. depth.
Option *: :o de'ault precision. lthou&h this reCuires that the precision be speci'ied in ever. shader, it
#ill 'orce the developer to consider the reCuirements.
RESOL9-$O:: -he de'ault precision o' all sampler t.pes present in GLSL ES 1.) should also be lo?p
in GLSL ES (.). :e# sampler t.pes in GLSL ES (.) should have no de'ault precision.
1!.0 +unction and (ariable 7ame Spaces
!o variables and 'unctions share the same name spaceB GLSL ES doesnDt support 'unction pointers so the
&rammar can al#a.s be used to distin&uish cases. ,o#ever this is a departure 'rom %OO.
RESOL9-$O:: 4unctions and variables share the same name space.
Should redeclarations o' the same names be permitted #ithin the same scopeB -his #ould be compatible
#ith %. -here are several cases e.&.:
1. Redeclarin& a 'unction. 'unction protot.pe is a declaration but not a de'inition. 'unction
de'inition is both a declaration and a de'inition. %onseCuentl. a 'unction protot.pe and a
'unction de'inition ?o' the same 'unction@ #ithin the same scope Cuali'ies as redeclaration.
*. !eclarin& a name as a 'unction and then redeclarin& it as a structure.
(. !eclarin& a name as a variable and then redeclarin& it as a structure.
!isallo#in& multiple 'unction declarations ?includin& allo#in& a separate 'unction protot.pe and 'unction
de'inition@ #ould prevent static recursion b. desi&n. ,o#ever it imposes constraints on the structure o'
shaders.
GLSL ES 1.)) allo#s a sin&le 'unction de'inition plus a sin&le optional 'unction declaration.
RESOL9-$O:: <ultiple de'initions are disallo#ed. <ultiple 'unction declarations ?'unction
protot.pes@ are allo#ed. -his is in line #ith %OO.
128
12 Issues
1!.2 Local +unction -eclarations and +unction $iding
Should local 'unctions hide all 'unctions o' the same sameB
-his is considered use'ul i' local 'unction declarations are allo#ed. ,o#ever, the onl. use 'or local
'unction declarations in GLSL ES is to unhide 'unctions that have been hidden b. variable or structure
declarations. -his is not a compellin& reason to include them.
RESOL9-$O:: !isallo# local 'unction declarations.
1!.4 Oerloading main@A
Should it be possible 'or the user to overload the main?@ 'unctionB
RESOL9-$O:: :o. -he main 'unction cannot be overloaded.
1!.5 Error :eporting
$n &eneral #hich errors must be reported b. the compilerB
Some errors are eas. to detect. ll &rammar errors and t.pe matchin& errors #ill normall. be detected as
part o' the normal compilation process. Other semantic errors #ill reCuire speci'ic code in the compiler.
-he bul8 o' the #or8 in a compiler occurs a'ter parsin& so addin& some error detection should not
increase the total cost o' compilation si&ni'icantl.. ,o#ever, it is e2pected that development s.stems #ill
have sophisticated error and #arnin& reportin& and it is not necessar. to repeat this process 'or on;tar&et
compilers.
RESOL9-$O:: ll &rammar, t.pe mismatch and other speci'ic semantic errors as listed in this
speci'ication must be reported. Reportin& o' other errors or #arnin&s is optional.
Should compilers report i' ma2ima are e2ceeded, even i' the implementation supports themB -his could
aid portabilit..
RESOL9-$O:: :o, hi&h;end implementations ma. Cuite le&itimatel. &o be.ond the speci'ication in
these areas and mandatin& the use o' the e2tension mechanism #ould cause needless comple2it..
!evelopment s.stems should issue portabilit. #arnin&s.
Should static recursion be detectedB
RESOL9-$O:: `es, the compiler #ill normall. &enerate the necessar. control 'lo# &raph so detection
is eas..
1!.10 Structure -eclarations
Should structures #ith the same name and same member variables be considered as the same t.peB
RESOL9-$O:: :o, 'ollo# the %OO rules. 1ariables onl. have the same t.pe i' the. have been declared
#ith the same t.pe and not i' the. have been declared #ith di''erent t.pes that have the same name. -his
does not appl. to lin8in& ?'or uni'orms and var.in&s@ #hich has its o#n rules.
129
12 Issues
Should structure declarations be allo#ed in 'unction parametersB
RESOL9-$O:: :o, 'ollo#in& the previous resolution it #ould be impossible to call such a 'unction
because it #ould be impossible to declare a variable #ith the same structure t.pe.
1!.11 Embedded Structure -efinitions
Should embedded structure de'initions be allo#edB
e.&.
stru't S
*
stru't
*
int a-
2 t-
int $%
2-
$n order to access the constructor, the structure name #ould have to be scoped at the same level as the
outer level structure. -his is inconsistent.
Option 1: !isallo# embedded structure de'initions.
Option *: llo# embedded structure de'initions but accept that the constructor is not accessible.
Option (: Scope embedded structure names at the same level as the outermost scope name.
RESOL9-$O:: Remove embedded structure de'initions.
1!.1! :edefining ,uilt8in +unctions
Should it be possible to rede'ine or overload built;in 'unctionsB
-here ma. be some applications #here it is use'ul to rede'ine the built;in 'unctions but the lan&ua&e does
not include the reCuired 'unctionalit. 'or all cases. "uilt;in 'unctions are li8el. to be e''icientl. mapped
to the hard#are. 9ser;de'ined 'unctions ma. not be as e''icient but ma. be able to o''er &reater precision
?e.&. 'or the tri& 'unctions@. -he application ma. then #ant access to both the ori&inal and ne# 'unction.
Some user;de'ined 'unctions #ould bene'it 'rom access to the ori&inal 'unction. Once the ne# 'unction
has been declared, the ori&inal 'unction is hidden so both these use cases are impossible #ith the current
speci'ication.
Option 1: llo# both rede'inition and overloadin& o' built;in 'unctions.
Option *: !isallo# rede'inition o' built;in 'unctions. llo# them to be overloaded. -his ma. be use'ul
#here it is reCuired to e2tend the 'unctionalit. o' a built;in 'unction. ,o#ever it creates a subtle
incompatibilit. #ith the des8top:
130
12 Issues
int sin#int x) *return x-2
void main#)
*
float a + sin#<,=)- && legal in GLSL ES0 not legal in des:top GLSL,
2
$t is also a potential source o' bac8#ards;incompatibilit. i' a 'uture version o' the lan&ua&e introduces
ne# overloads.
Option (: Remove the abilit. to rede'ine or overload 'unctions.
RESOL9-$O:: !isallo# both overloadin& and rede'inin& built;in 'unctions. -here is no compellin& use
case.
1!.13 Global Scope
,o# should the scopin& levels 'or user;de'ined and built;in names be de'inedB
GLSL ES 1.)) and most versions o' GLSL have a &lobal scope 'or user;de'ined 'unctions and variables
and a distinct DouterD scope #here the built;in 'unctions reside. -his is di''erent 'rom %OO. Since GLSL
ES (.)) does not allo# the rede'inition o' built;in 'unctions, a sin&le &lobal scope is su''icient.
RESOL9-$O:: sin&le &lobal scope #ill be used 'or user;de'ined and built;in names.
1!.1% Constant E)pressions
Should user and built;in 'unctions be allo#ed in constant e2pressionsB e.&.
'onst float a + sin#<,=)-
-he compiler must be able to evaluate all possible constant e2pressions as the. can potentiall. be used to
siEe arra.s and 'unctions resolution is dependent on arra. siEe. %ompile;time evaluation o' built;in
'unctions is e2pensive in terms o' code siEe. -he comple2it. o' compile;time evaluation o' user;de'ined
'unctions is potentiall. unbounded.
RESOL9-$O:: llo# built;in 'unctions to be included in constant e2pressions. Rede'inition o' built;in
'unctions is prohibited. 9ser;de'ined 'unctions are not allo#ed in constant e2pressions.
1!.1' (ar&ing Lin1age
$n the verte2 shader, a particular var.in& ma. be either 1@ not declared, *@ declared but not #ritten, (@
declared and #ritten but not in all possible paths or +@ declared and #ritten in all paths. Li8e#ise a
var.in& in a 'ra&ment shader ma. be either a@ not declared, b@ declared but not read, c@ declared and read
in some paths or d@ declared and read in all paths. =hich o' these 17 combinations should &enerate an
errorB
-he compiler should not attempt to discover i' a var.in& is read or #ritten in all possible paths. -his is
considered too comple2 'or OpenGL ES.
131
12 Issues
-he same verte2 shader ma. be paired #ith di''erent 'ra&ment shaders. -hese 'ra&ment shaders ma. use a
subset o' the available input var.in&s. -his behavior should be supported #ithout causin& errors.
-here'ore i' the verte2 shader #rites to a var.in& that the 'ra&ment shader doesnDt declare or declared but
doesnDt read then this is not an error.
$' the verte2 shader declares but doesnDt #rite to a var.in& and the 'ra&ment shader declares and reads it, is
this an errorB
RESOL9-$O:: :o.
RESOL9-$O:: -he onl. error case is #hen a var.in& is declared and read b. the 'ra&ment shader but is
not declared in the verte2 shader.
1!.10 glB*osition
$s it an error i' the verte2 shader doesnDt #rite to &lA3ositionB =hether a shader #rites to &lA3osition
cannot al#a.s be determined e.&. i' there is dependence on an attribute.
Option 1: :o it is not an error. -he behavior is unde'ined in this case. !evelopment s.stems should
issue a #arnin& in this case but the on;tar&et compiler should not have to detect this.
Option *: $t is an error i' the verte2 shader does not staticall. #rite to &lA3osition
Option (: $t is an error i' there is an. static path throu&h the shader #here &lA3osition is not #ritten.
RESOL9-$O:: :o error ?option 1@. -he nature o' the unde'ined behavior must be speci'ied.
1!.12 *re8processor
$s the preprocessor necessar.B
r&uments 'or removin& or simpli'.in& the preprocessor:
-he preprocessor is moderatel. comple2 to implement. $n particular, 'unction;li8e macros ma.
have arbitrar. comple2it. and reCuire si&ni'icant resources to compile.
-he %OO standard does not 'ull. speci'. the preprocessor. $n particular, the situations #here
preprocessor to8ens are subFect to macro e2pansion are not 'ull. de'ined. :either is the e''ect o'
macro de'initions encountered durin& macro e2pansion.
Over;use o' the preprocessor is a common source o' pro&rammin& errors because there is limited
compile;time chec8in&.
r&uments 'or retainin& the preprocessor:
-he e2tension mechanism relies on the preprocessor so this #ould need to be replaced.
-he #define, #ifdef, #ifndef, #elsif and #endif constructs are commonl. used 'or mana&in&
di''erent versions and 'or include &uards.
-here is no template mechanism in GLSL ES so macros are o'ten used instead.
GLSL ES 1.)) removed to8en pastin& and other 'unctionalit..
132
12 Issues
RESOL9-$O:: Keep the basic preprocessor as de'ined in the GLSL ES 1.)) speci'ication.
1!.14 Character set
GLSL ES 1.)) onl. allo#ed a subset o' the ascii character set to be used in shaders. -hat included names
and comments. -he #ritten lan&ua&es o' man. countries include other characters or use a completel.
di''erent character set. -his ma8es it di''icult or impossible to #rite comments in those lan&ua&es.
=here should the ne# characters be allo#edB $t #ould be possible to decide independentl. 'or comments,
identi'iers and macros. 4or macros, the. could be allo#ed as part o' macro de'initions but prohibited in
the 'inal output o' macro e2pansion.
RESOL9-$O:: -he ne# characters are onl. allo#ed inside comments.
=hich character set should be used to de'ine the ne# characters.
9-4;5 has the advanta&e that it is bac8#ards;compatible #ith S%$$. ll S%$$ characters are valid
9-4;5 sin&le;b.te characters and 9-4;5 multi;b.te characters all have the hi&hest bit set to D1D in each
b.te. -he disadvanta&e is that 9-4;5 is variable len&th.
RESOL9-$O:: 9-4;5
,o# should the e2tended character set be speci'iedB
Options include 'ull 9-4;5 or b. e2plicitl. listin& the allo#ed characters.
RESOL9-$O:: 4ull 9-4;5
Should the compiler chec8 'or the presence o' invalid 9-4;5 b.te seCuencesB
Since an. multi;b.te characters #ill onl. occur #ithin comments and so not reCuired 'urther processin&, it
#ould be ine2pensive to chec8 'or valid 9-4;5 characters. %onversel., there appears to be no advanta&e
to doin& so. -he issue o' validit. is onl. o' concern to te2t editors.
RESOL9-$O:: -he compiler must not chec8 'or invalid 9-4;5 characters. ".tes D)D and ne#line
characters #ill be interpreted as such #herever the. occur.
,o# does the Nversion directive interact #ith the use o' 9-4;5 in commentsB
4ollo#in& %OO, the Dphases o' translationD speci'ication de'ines comment processin& to be per'ormed
be'ore macro directives are processed. ,o#ever 9-4;5 is le&al in GLSL ES (.)), identi'ied b. Nversion
()) but not in GLSL ES 1.)), identi'ied b. Nversion 1)) ?or b. absence o' a Nversion directive@.
Option: -he shader is processed in * passes. -he 'irst determines the shader version and the second
per'orms compilation as be'ore.
Option: Replace the current version directive mechanism #ith a b.te seCuence that must al#a.s occur at
the start o' the shader. -his is similar to other standards that have multiple versions e.&. http.
Option: <a8e 9-4;5 characters an optional 'eature o' GLSL ES 1.))
133
12 Issues
RESOL9-$O:: 9-4;5 #ill be made optional in GLSL ES 1.))
1!.15 Line Continuation
Should the line continuation character DKD be included in the speci'icationB
Line continuation #as deliberatel. e2cluded 'rom previous versions o' GLSL and GLSL ES in order to
discoura&e e2cessive use o' the pre;processor. ,o#ever, 'unction;li8e macros are commonl. used
because there is no DtemplateD mechanism, #hich #ould allo# 'unctions to be parametriEed b. a t.pe.
Lon& macro de'initions are there'ore not uncommon and the line;continuation character ma. aid
readabilit..
Given that shader source is stored in a list o' character strin&s, the ne#line character can be omitted and
this has the same e''ect as a ne#line 'ollo#ed b. a line;continuation.
RESOL9-$O:: $nclude line;continuation.
,o# does this interact #ith NversionB
RESOL9-$O:: Same issue as #ith 9-4;5in &eneral. Line;continuation to be made optional in GLSL ES
1.))
1!.!0 *hases of Compilation
Should the preprocessor run as the ver. 'irst sta&e o' compilation or a'ter conversion to preprocessor
to8ens as #ith %>%OOB
-he cases #here the result is di''erent are not common.
#define e D<
int n + <e-
ccordin& to the cOO standard, D1eD should be converted to a preprocessor to8en #hich then 'ails
conversion to a number. $' the preprocessor is run 'irst, D1eD is e2panded to D1O1D #hich is then parsed
success'ull..
RESOL9-$O:: 4ollo# cOO rules.
1!.!1 9a)imum 7umber of (ar&ings
,o# should &lA<a21ar.in&4loats be de'inedB Ori&inall. this #as speci'ied as (* 'loats but currentl.
some des8top implementations 'ail to implement this correctl.. <an. implementations use 5 vec+
re&isters and it is di''icult to split var.in&s across multiple re&isters #ithout losin& per'ormance.
Option 1: Speci'. the ma2imum as 5 +;vectors. $t is then up to the application to pac8 var.in&s. Other
lan&ua&es reCuire the pac8in& to be done b. the application. !evelopers have not reported this as a
problem.
134
12 Issues
Option *: Speci'. the ma2imum accordin& to a pac8in& rule. -he developer ma. use a non;optimal
pac8in& so it is better to do this in the driver. ReCuirin& the application to pac8 var.in&s is problematic
#hen shaders are automaticall. &enerated. $t is easier 'or the driver to implement this.
RESOL9-$O:: -he ma2imum #ill be speci'ied accordin& to a pac8in& rule.
Should attributes and uni'orms 'ollo# this ruleB
RESOL9-$O:: ttributes should not 'ollo# this rule. -he. #ill be continued to be speci'ied as vec+s.
RESOL9-$O:: 9ni'orms should not 'ollo# this rule 'or GLSL ES (.)). $mplementations are e2pected
to virtualiEe such resources.
Should the built;in special variables ?&lA4ra&%oord, &lA4ront4acin&, &lA3oint%oord@ be included in this
pac8in& al&orithmB "uilt;in special variables are implemented in a variet. o' #a.s. Some
implementations 8eep them in separate hard#are, some do not.
RESOL9-$O:: n. built;in special variables that are staticall. used in the shader should be included in
the pac8in& al&orithm.
Should &lA4ra&%oord be included in the pac8in& al&orithmB -he 2 and . components #ill al#a.s be
reCuired 'or rasteriEation. -he E and # components #ill o'ten be reCuired.
RESOL9-$O:: &lA4ra&%oord is included in the count o' var.in&s.
,o# should matD var.in&s be pac8edB
Option 1: 3ac8 them as *2*.
Option *: 3ac8 them as + columns 2 1 ro#. -his is usuall. more e''icient 'or an implementation.
Option (: llocate a + column 2 * ro# space. -his is ine''icient but allo#s 'le2ibilit. in ho#
implementations map them to re&isters.
Option +: s above but pac8 * matD var.in&s into each + column 2 * ro# bloc8. n. unpaired matD
ta8es a #hole +2* bloc8.
RESOL9-$O:: Option (
Should mat8 ta8e ( #hole ro#sB
-his #ould a&ain allo# 'le2ibilit. in implementation but it #astes space that could be used 'or 'loats or
'loat arra.s.
RESOL9-$O:: :o, mat8 should ta8e a (2( bloc8.
Should veA8 ta8e a #hole ro#B
135
12 Issues
RESOL9-$O:: :o.
Should &lA<a21erte29ni'orms%omponents be chan&ed ?'rom des8top GLSL@ to re'lect the pac8in&
rulesB
RESOL9-$O:: Rename &lA<a21erte29ni'orm%omponents to &lA<a21erte29ni'orm1ectors. Rename
&lA<a24ra&ment9ni'orm%omponents to &lA<a24ra&ment9ni'orm1ectors.
1!.!! .rra& -eclarations
9nsiEed arra. declarations.
!es8top GLSL allo#s arra.s to be declared #ithout a siEe and these can then be accessed #ith constant
inte&ral e2pressions. -he siEe never needs to be declared. -his #as to support &lA-e2coord e.&.
var9ing ve'7 gl_exHoordAB-
,,,
gl_FragHolor + texture #tex0 gl_exHoordA=B,x9)-
-his allo#s &lA-e2%oord to be used #ithout havin& to declare the number o' te2ture units.
&lA-e2%oord is part o' the 'i2ed 'unctionalit. so unsiEed arra.s should be removed 'or GLSL ES
RESOL9-$O:: Remove unsiEed arra. declarations.
=hich 'orms o' arra. declarations should be permittedB
float aA>B-
,,,
float $AB + a- && $ is expli'itl9 si"e >
or
float aAB + floatAB #<,=0 8,=0 ;,=)-
RESOL9-$O:: ll above constructs are valid. ,o#ever, an. declaration that leaves the siEe unde'ined
is disallo#ed as this #ould add comple2it. and there are no use;cases.
1!.!3 Inariance
,o# should invariance bet#een shaders be handledB
1ersion 1.1) o' des8top GLSL uses 'trans'orm?@ to &uarantee that &lA3osition can be &uaranteed to be
calculated the same #a. in di''erent verte2 shaders. -his relies on the 'i2ed 'unction that has been
removed 'rom ES. $t is also ver. restrictive in that it onl. allo#s verte2 trans'orms based on matrices. $t
does not appl. to other values such as those used to &enerate te2ture coordinates.
Option 1: Speci'. all operations to be invariant. :o, this is too restrictive. Optimum use o' resources
becomes impossible 'or some implementations.
136
12 Issues
Option *: dd an invariance Cuali'ier to 'unctions that reCuire invariance. :o, this does not #or8 as the
inputs to the 'unctions and operations per'ormed on the outputs ma. not be invariant.
Option (: dd an invariance Cuali'ier to all variables ?includin& shader outputs@.
RESOL9-$O:: dd an invariance Cuali'ier to variables but permit its use onl. 'or outputs 'rom the
verte2 and 'ra&ment shaders. dd a &lobal invariance option 'or use #hen complete invariance is
reCuired.
Should the invariance Cuali'ier be permitted on parameters to te2ture 'unctionsB
<an. al&orithms rel. on t#o or more te2tures bein& e2actl. ali&ned, either #ithin a sin&le invocation o' a
shader or usin& multi;pass techniCues. -his could be &uaranteed b. usin& the invariant Cuali'ier on
variables that are used as parameters to the te2ture 'unction.
9sin& the &lobal invariance pra&ma also &uarantees ali&nment o' the te2tures. $t is not clear #hether
allo#in& 'iner control o' invariance is use'ul in practice. %ompilers ma. revert to &lobal invariance and
there ma. be other speci'ic cases that need to be considered.
RESOL9-$O:: 9se o' a variable as a parameter to a te2ture 'unction does not impl. that it ma. be
Cuali'ied as invariant.
!o invariance Cuali'iers 'or declarations in the verte2 and 'ra&ment shaders need to matchB
Option 1: Onl. allo# invariance declarations on var.in&s in the verte2 shader. -he invariance o' the
var.in& in the 'ra&ment shader should then be &uaranteed automaticall..
Option *: Speci'. that the. must match.
RESOL9-$O:: $nvariance Cuali'iers 'or var.in& declarations must match.
Should this rule appl. i' the var.in& is declared but not usedB
RESOL9-$O:: `es, this rule applies 'or declarations, independent o' usa&e.
,o# does this rule appl. to the built;in special variables.
Option 1: $t should be the same as 'or var.in&s. "ut &lA3osition is used internall. b. the rasteriEer as
#ell as 'or &lA4ra&%oord so there ma. be cases #here rasteriEation is reCuired to be invariant but
&lA4ra&%oord is not.
Option *: &lA4ra&%oord and &lA3oint%oord can be Cuali'ied as invariance i' and onl. i' &lA3osition and
&lA3ointSiEe are Cuali'ied invariant, respectivel..
%an unde'ined values be made invariantB
$' a t.pe is implemented b. a lar&er native t.pe and due to lac8 o' initialiEation, a variable o' that t.pe has
an ille&al value, it is possible 'or variant behaviour to occur.
137
12 Issues
4or e2ample suppose a boolean is represented b. a (*;bit inte&er #ith D'alseD represented as ) and DtrueD
represented as D1D. $' the compiler uses both an DeCuals )D and an DeCuals 1D test, the 'ollo#in& ma. occur:
$ool $- && (e implementation sets t(is to an illegal value e,g, ;

if #$) && implementation tests G$ ++ <G .(i'( is false
*
f#)-
2
else && implementation tests G$ ++ =G .(i'( is also false
*
g#)-
2
:either '?@ nor &?@ are e2ecuted #hich is une2pected behaviour. Such cases could be made invariant but
#ould 'or e2ample reCuire the compiler to initialiEe unde'ined values #hich is a per'ormance cost.
RESOL9-$O:: 9nde'ined values cannot be made invariant. -hese shaders are mal'ormed and there'ore
have unde'ined behavior.
1!.!% Inariance <ithin a shader
,o# should invariance #ithin a shader be speci'iedB
%ompilers ma. decide to recalculate a value rather than store it in a re&ister ?rematerialiEation@. -he ne#
value ma. not be e2actl. the same as the ori&inal value.
Option 1: 3rohibit this behavior.
Option *: 9se the invariance Cuali'ier on variables to control this. -his is consistent #ith the des8top.
RESOL9-$O:: 1alues #ith in a shader are in variant be de'ault. -he invariance Cuali'ier or pra&ma
ma. be used to ma8e them invariant.
Should constant e2pressions be invariantB $n the 'ollo#in& e2ample, it is not de'ined #hether the literal
e2pression should al#a.s evaluate to the same value.
pre'ision mediump int-
pre'ision mediump float-
'onst int si"e + int#'eil#7,=&;,= I =,;;;;;;))-
int aAsi"eB-
for #int i+=- i/int#'eil#7,=&;,= I =,;;;;;;))- iDD) *a AiB + i-2
138
12 Issues
$mplementations must usuall. be able to evaluate constant e2pressions at compile time since the. can be
used to declare the siEe o' arra.s. ,ard#are ma. compute a less accurate value compared #ith maths
libraries available in %. $t #ould ho#ever be e2pected that 'unctions such as sine and cosine return
similar results #hether or not the. are part o' a constant e2pression. -his su&&ests that the
implementation mi&ht #ant to evaluate these 'unctions onl. on the hard#are. ,o#ever, there are no
situations, even #ith &lobal invariance, #here compile time evaluation and runtime evaluation must match
e2actl..
RESOL9-$O:: `es, constant e2pressions must be invariant.
1!.!' <hile8loop -eclarations
=hat is the purpose o' allo#in& variable declarations in a #hile statementB
.(ile #$ool $ + f#)) *,,,2
"oolean b #ill al#a.s be true until the point #here it is destro.ed. $t is use'ul in %OO since inte&ers are
implicitl. converted to booleans.
RESOL9-$O:: Keep this behavior. =ill be reCuired i' implicit t.pe conversion is added to a 'uture
version.
similar issue e2ists in 'or;loops. -he &rammar allo#s constructs such as
for#-$ool x + a / $-) -
1!.!0 Cross Lin1ing ,et#een Shaders
Should it be permissible 'or a 'ra&ment shader to call a 'unction de'ined in a verte2 shader or vice versaB
RESOL9-$O:: :o, there is no need 'or this behavior.
1!.!2 (isibilit& of -eclarations
t #hat point should a declaration ta8e e''ectB
int x+<-
*
int x+80 9+x- && 'ase ?
int "+"- && 'ase 5
2
Option 1: -he name should be visible immediatel. a'ter the identi'ier. "oth cases above are le&al. $n
case , . is initialiEed to the value *. -his is consistent #ith cOO. 4or case ", the use case is to initialiEe
a variable to point to itsel' e.&. voidR p T UpM -his is not relevant to GLSL ES.
Option *: -he name should be visible a'ter the initialiEer ?i' present@, other#ise immediatel. a'ter the
identi'ier. $n case , . is initialiEed to *. %ase " is an error ?assumin& no prior declaration o' E@.
Option (: -he name should be visible a'ter the declaration. $n case , . is initialiEed to 1. %ase " is an
error i' E is has no prior declaration.
139
12 Issues
RESOL9-$O:: Option *. !eclarations are visible a'ter the initialiEer i' present, other#ise a'ter the
identi'ier.
1!.!4 Language (ersion
=hat version number should the lan&ua&e haveB -his version o' the lan&ua&e is based on version (.() o'
the des8top GLSL. ,o#ever it includes a number o' 'eatures that are in version +.*) but not (.(). -he
previous version o' GLSL ES #as version 1.)) so this version could be called version *.)).
RESOL9-$O:: 4ollo# the des8top GLSL convention so that the lan&ua&e version matches the 3$
version. ,ence this version #ill be called (.))
1!.!5 Samplers
Should samplers be allo#ed as l;valuesB -he speci'ication alread. allo#s an eCuivalent behavior:
%urrent speci'ication:
uniform sampler8! samplerAMB-
int index + f#,,,)-
ve'7 tex + texture#samplerAindexB0 x9)- && allo.ed
9sin& assi&nment o' sampler t.pes:
uniform sampler8! s-
s + g#,,,)-
ve'7 tex + texture#s0 x9)- && not allo.ed
RESOL9-$O:: !.namic inde2in& o' sampler arra.s is no# prohibited b. the speci'ication. Restrict
inde2in& o' sampler arra.s to constant inte&ral e2pressions.
1!.30 -&namic Inde)ing
4or GLSL ES 1.)), support o' d.namic inde2in& o' arra.s, vectors and matrices #as not mandated
because it #as not directl. supported b. some implementations. So't#are solutions ?via pro&ram
trans'orms@ e2ist 'or a subset o' cases but lead to poor per'ormance. Should support 'or d.namic inde2in&
be mandated 'or GLSL ES (.))B
RESOL9-$O:: <andate support 'or d.namic inde2in& o' arra.s e2cept 'or sampler arra.s, 'ra&ment
output arra.s and uni'orm bloc8 arra.s.
Should support 'or d.namic inde2in& o' vectors and matrices be mandated in GLSL ES (.))B
RESOL9-$O:: `es.
$nde2in& o' arra.s o' samplers b. constant;inde2;e2pressions is supported in GLSL ES 1.)). constant;
inde2;e2pression is an e2pression 'ormed 'rom constant;e2pressions and certain loop indices, de'ined 'or
a subset o' loop constructs. Should this 'unctionalit. be included in GLSL ES (.))B
140
12 Issues
RESOL9-$O:: :o. rra.s o' samplers ma. onl. be inde2ed b. constant;inte&ral;e2pressions.
1!.31 9a)imum 7umber of Te)ture 6nits
-he minimum number o' te2ture units that must be supported in the 'ra&ment shader is currentl. * as
de'ined b. &lA<a2-e2ture$ma&e9nits T 5. $s this too lo# 'or GLSL ES (.)B
Option 1: `es, the number o' te2turin& units is the limitin& 'actor 'or 'ra&ment shaders. -he number o'
te2ture units #as increased 'rom 1 to * &oin& 'rom OpenGL ES 1.) to OpenGL ES 1.1 and increased to 5
'or OpenGL ES *.)
RESOL9-$O:: $ncrease to 17
1!.3! On8target Error :eporting
Should compilers be reCuired to report an. errors at compile time or can errors be de'erred until lin8
timeB
RESOL9-$O:: $' a pro&ram cannot be compiled, on;tar&et compilers are onl. reCuired to report that an
error has occurred. -his error ma. be reported at compile time or lin8 time or both. !evelopment
s.stems must &enerate &rammar errors at compile time.
1!.33 :ounding of Integer -iision
Should the roundin& mode be speci'ied 'or inte&er divisionB
-he roundin& mode 'or division is related to the de'inition o' the remainder operator. -he important
relation in most lan&ua&es ?but not relevant in this version o' GLSL ES@ is:
?a > b@ R b O a S b T a ?a and b are inte&ers@
9suall. the remainder operator is de'ined to have the same si&n as the dividend #hich implies that divide
must round to#ards Eero. ?:ote that the modulo 'unction is not the same as the remainder 'unction.
<odulo is de'ined to have the same si&n as the divisor@.
-he remainder operator #as not part o' GLSL ES 1.)), so it #as not necessar. to speci'. the roundin&
mode. $n GLSL ES (.)), the remainder operator is included but the results are unde'ined i' either or both
operands are ne&ative.
RESOL9-$O:: -he roundin& mode is unde'ined 'or this version o' the speci'ication.
1!.3% 6ndefined :eturn (alues
$' a 'unction is declared #ith a non;void return t.pe, an. return statements #ithin the de'inition must
speci'. a return e2pression #ith a t.pe matchin& the return t.pe. ,o#ever i' the 'unction returns #ithout
e2ecutin& a return statement the behaviour is unde'ined. Should the compiler attempt to chec8 'or these
cases and report them as an errorB
141
12 Issues
E2ample:
int f#)
*
&& no return statement
2
,,,
int a + f#)-
Option 1: n unde'ined value is returned to the caller. :o error is &enerated. -his is #hat most cOO
compilers do in practice ?althou&h the cOO standard actuall. speci'ies Dunde'ined behaviourD@.
Option *: -here must be a return statement at the end o' all 'unction de'initions that return a value.
:o, this reCuires statements to be added that ma. be impossible to e2ecute.
Option (: return statement at the end o' a 'unction de'inition is reCuired onl. i' it is possible 'or
e2ecution to reaches the end o' the 'unction:
E.&.
int f#$ool $)
*
if #$)
return <-
else
return =-
&& No error, (e exe'ution 'an never rea'( t(e end of t(e fun'tion so
&& t(e impli'it return statement is never exe'uted,
2
-his becomes impossible to determine in the presence o' loops.
Option +: ll 'inite static paths throu&h a 'unction de'inition must end #ith a return statement. static
path is a path that could potentiall. be ta8en i' each branch in the code could be controlled independentl..
RESOL9-$O:: Option 1: -he 'unction returns an unde'ined value.
1!.3' *recisions of Operations
Should the precision o' operations such as add and multipl. be de'inedB
-hese are not de'ined b. the %OO standard but it is &enerall. assumed that %OO implementations #ill use
$EEE 0/+ arithmetic. -his is not true 'or G39s #hich &enerall. support onl. a subset o' $EEE 0/+. $n
addition, man. operations such as the transcendental 'unctions are considered too e2pensive to implement
#ith more than 1) si&ni'icant bits o' precision. !ivision is commonl. implemented b. reciprocal and
multiplication.
RESOL9-$O:: $nclude a table o' precisions 'or operations.
142
12 Issues
1!.30 Compiler Transforms
=hat compiler trans'orms should be allo#edB
%OO prohibits compiler trans'orms o' e2pressions that alter the 'inal result. ?:ote that %OO allo#s hi&her
precisions than speci'ied to be used but this is a di''erent issue.@ G39s commonl. ma8e use o' such
trans'orms, 'or e2ample #hen mappin& seCuential code to vector;based architectures.
RESOL9-$O:: speci'ied set o' trans'orms ?in addition to those permitted b. %OO@ are allo#ed.
1!.32 E)pansion of +unction8li1e 9acros in the *reprocessor
=hen e2pandin& macros, each macro can onl. be applied once to the ori&inal to8en or an. to8en
&enerated 'rom that to8en. -o implement this, the e2pansion o' 'unction;li8e macros reCuires a list o'
applied macros 'or each to8en to be maintained. -his is a lar&e overhead.
RESOL9-$O:: 4ollo# the %OO speci'ication.
=hat should the behaviour be i' a directive is encountered durin& e2pansion o' 'unction;li8e macrosB
-his is currentl. speci'ied as unde'ined in %OO althou&h several compilers implement the e2pected
behavior.
RESOL9-$O:: Leave as unde'ined behavior.
1!.34 Should E)tension 9acros be Globall& -efinedC
4or each e2tension there is an associated macro that the shader can use to determine i' an e2tension is
available on a &iven implementation. Should this macro be de'ined &loball. or should it be de'ined #hen
the e2tension is ?success'ull.@ enabledB
"oth alternatives are usable since attemptin& to enable an unimplemented e2tension onl. results in a
#arnin&.
Option 1: Globall. de'ined
#ifdef GL_OES_/extension1nameT
#extension GL_OES_/extension1nameT % ena$le
,,,
#endif
Option *: !e'ined as part o' Ne2tension
#extension GL_OES_/extension1nameT % ena$le && .arning if not availa$le
#ifdef GL_OES_/extension1nameT
,,,
#endif
RESOL9-$O:: -he macros are de'ined &loball.. -here should be a #arnin&;'ree path 'or all le&al cases.
143
12 Issues
1!.35 9inimum :e/uirements
GLSL ES 1.)) speci'ied a set o' minimum reCuirements that e''ectivel. made parts o' the speci'ication
optional. -he purpose #as to enable lo# cost implementations #hile allo#in& hi&her per'ormance
devices to e2pose 'eatures #ithout recourse to e2tensions. -hat 'le2ibilit. came at the cost o' portabilit..
Should the minimum reCuirements section be included as part o' GLSL ES (.))B
RESOL9-$O:: :o, e2cept 'or the section on countin& o' var.in&s.
1!.%0 *ac1ing +unctions
1!.%1 ,oolean logical ector operations
-he lo&ical binar. operators and ?UU@, or ? W W @, and e2clusive or ?VV@ operate onl. on t#o boolean
e2pressions and result in a boolean e2pression. Should the. be e2tended to operate on boolean vectorsB
-he *
nd
operand is conditionall. evaluated 'or these operators.
$ve'7 f#)-
$ve'7 g#)-

f#) UU g#)- && g#) gets GrunG for some 'omponents $ut not ot(ers,
&& (is isnGt .ell defined,
RESOL9-$O:: :o, these should not be part o' the lan&ua&e.
1!.%! :ange Chec1ing of literals
Should an error be &enerated i' a literal inte&er is outside the ran&e o' a (*;bit inte&erB
-his can be easil. chec8ed b. the compiler. ,o#ever, there is a complication because the literal does not
include the minus si&n 'or ne&ative constants. Si&ned inte&ers can be distin&uished 'rom unsi&ned
inte&ers b. the DuD su''i2 but the value )25)))))) is onl. valid i' preceded b. a unar. minus.
Option: %hec8 onl. that the numeric part o' a literal inte&er ?si&ned or unsi&ned@ is representable b. (*
bits.
Option: $nclude an. precedin& unar. minus and chec8 that the literal is #ithin the ran&e o' a si&ned or
unsi&ned inte&er as appropriate.
Option: E2tend the chec8in& to an. constant inte&ral e2pression.
RESOL9-$O:: $t is an error to have a literal unsi&ned inte&er outside the ran&e o' a (*;bit inte&er.
Should this appl. to 'loatin&;point numbersB
-he GLSL spec allo#s an arbitrar. number o' di&its be'ore the decimal point. $t there'ore possible 'or a
'loat literal to have an arbitraril. lar&e number o' characters but still be representable e.&.
144
12 Issues
</< million "erosT,=e1<======
1. 3arsin& constraints. Should the number o' characters in each 'ield be limited in some #a.B
1. Should the mantissa be limited to e.&. 17 charactersB
*. Should the unsi&ned part o' the mantissa be reCuired to 'it into a (* bit inte&erB
*. Ran&e chec8s.
1. $' the value is lar&er than (.+)*5*(+0e(5, should it be reCuired to return $:4B Or return
an errorB
RESOL9-$O:: :o limit on the number o' characters in the mantissa or e2ponent in a 'loat literal.
RESOL9-$O:: 1alues lar&er than representable in a 'loat (* must return $:4 ?O or ; as appropriate@.
1alues #ith a ma&nitude too small to be representable in a 'loat (* must return Eero.
1!.%3 Se/uence operator and constant e)pressions
Should the 'ollo#in& construct be allo#edB
float aA80;B-
-he e2pression #ithin the brac8ets uses the seCuence operator ?D,D@ and returns the inte&er ( so the
construct is declarin& a sin&le;dimensional arra. o' siEe (. $n some lan&ua&es, the construct declares a
t#o;dimensional arra.. $t #ould be pre'erable to ma8e this construct ille&al to avoid con'usion.
One possibilit. is to chan&e the de'inition o' the seCuence operator so that it does not return a constant;
e2pression and hence cannot be used to declare an arra. siEe.
RESOL9-$O:: -he result o' a seCuence operator is not a constant;e2pression.
1!.%% (ersion -irectie
-he version directive in GLSL ES 1.)) has been 'ound to be unsuitable in cases #here certain 'eatures o'
the lan&ua&e speci'ication are chan&ed. -he e2istin& mechanism relies on a pre;processor directive but,
'ollo#in& the order o' operations speci'ied b. the Dphases o' translationD section in the %OO speci'ication,
it is di''icult or perhaps impossible to chan&e 'eatures o' the lan&ua&e that are processed be'ore such
directives are invo8ed. Such 'eatures include the introduction o' the line;continuation character ?DKD@ and
the e2tension o' the character set.
-here are several options 'or an improved version mechanism. ll speci'. the version in the 'irst line o'
the shader and reCuire that the version directive is 'ollo#ed b. a ne#line.
Option 1: dd a b.te seCuence to the start o' the shader. -his #ould allo# an. chan&e to be made to the
lan&ua&e, includin& chan&in& the character set. -his mechanism is o'ten used in 'ile 'ormats 'or ima&es.
Option *: dd a character strin& seCuence to the start o' the shader. !e'ine it to appear to be a pre;
processor directive e.&.
145
12 Issues
#version ;== es
Option (: s option * but allo# some 'le2ibilit. in the 'ormat so that e2tra #hite;space #ould still be
allo#ed.
Option +: s option * but use a distinctive non;pre;processor 'ormat e.&.
version1;==1es
Option /: s option + but include the characters DglslD to aid identi'ication e.&.
glsl1version1;==1es
RESOL9-$O:: Option (. -he version directive is a strin&, present as the onl. non;#hite;space in the
'irst line o' the shader. $t is ver. unli8el. that the character set #ill be chan&ed in an incompatible #a.
'rom 9-4;5 in the 'uture. Option ( is the closest in appearance to the current mechanism.
1!.%' 6se of 6nsigned Integers
Should 'unctions that can onl. return a positive value e.&. te2tureSiEe?@ and the len&th?@ method, return
si&ned or unsi&ned valuesB
Option 1: 9nsi&ned inte&er. -his allo#s 'or some de&ree o' compile;time chec8in&. 4or e2ample it
#ould be impossible to accidentall. access an arra. element #ith a ne&ative inde2 in a t.pical
initialiEation loop such as:
float aA>B-
for #unit i+=u- i/a,lengt( #)- iDD)
aAiB + =,=-
Option *: Si&ned inte&er. -his allo#s &reater 'le2ibilit. in calculatin& arra. indices #ithout the need 'or
t.pe conversions e.&.
float aASIVEB-
,,,
int index + a,lengt(#) 1 ;- && Li$rar9 'ode, SIVE ma9 not $e :no.n .(en
&& t(is 'ode is .ritten
if #index T+ =) && .ould not .or: .it( an unsigned integer
f#aAindexB)-
RESOL9-$O:: Option *. -he principle is that inte&ers that represent values and hence ma. 'orm part o'
arithmetic e2pressions should al#a.s be si&ned, even i' it is 8no#n that the. #ill al#a.s be positive.
1alues that represent bit vectors should al#a.s be unsi&ned.
-he e2tra chec8in& made available b. the use o' unsi&ned inte&ers 'or values 8no#n to be positive is
minimal. $t #ould be pre'erable to include a range mechanism in a 'uture version o' the lan&ua&e.
146
13 ,c-no.led'#ents
13 .c1no#ledgments
-his speci'ication is based on the #or8 o' those #ho contributed to the OpenGL (.( Lan&ua&e
Speci'ication, the OpenGL ES *.) Lan&ua&e Speci'ication, and the 'ollo#in& contributors to this version:
corn 3oole., :1$!$
lberto <oreira, Qualcomm
le8sandra Krstic, Qualcomm
lon Or;"ach, :o8ia
ndrEeF Kacpro#s8i, $ntel
rEhan&e Sa'darEadeh, $ntel
s8e Simon %hristensen, R<
vi Shapira, Graphic Remed.
"arthold Lichtenbelt, :1$!$
"en "o#man, $ma&ination
-echnolo&ies
"en "rierton, "roadcom
"enF Lipcha8, pple
"enson -ao, 1ivante
"ill Licea;Kane, <!
"rent $ns8o, $ntel
"rian <urra., 4reescale
"ruce <err., R<
%arlos Santa, -$
%ass Everitt, Epic Games U
:1$!$
%emil EiEo&lu, -$
%han&;,.o `u, Samsun&
%hris !odd, :1$!$
%hris Kno2, :1$!$
%hris -sern&, -$
%la. <ont&omer., -$
%li'' Gibson, $ma&ination
-echnolo&ies
!aniel Kartch, :1$!$
!aniel Koch, -rans&amin&
!ao2ian& Gon&, $ma&ination
-echnolo&ies
!ave Shreiner, R<
!avid Garcia, <!
!avid Jarmon, 1ivante
!ere8 %ornish, Epic Games
Eben 9pton, "roadcom
Ed 3lo#man, $ntel U R<
Eisa8u Ohbuchi, !<3
Elan Lennard, R<
Eri8 4a.e;Lund, R<
Geor& Kollin&, $ma&ination
-echnolo&ies
Graham %onnor, $ma&ination
-echnolo&ies
Graham Sellers, <!
Gre& Roth, :1$!$
Guillaume 3ortier, ,i
Guo'an& Jiao, Qualcomm
,ans;<artin =ill, 1incent
,#an.on& Lee, ,uone
$;Gene Leon&, :1$!$
$an Romanic8, $ntel
$an South;!ic8inson, :1$!$
$lan elion;E2ch, Samsun&
$n8.un Lee, ,uone
Jacob Strm, Ericsson
James dams, "roadcom
James Jones, $ma&ination
-echnolo&ies
James <c%ombe, $ma&ination
-echnolo&ies
Jamie Gennis, Goo&le
Jan;,arald 4redri8sen, R<
Jani 1aisanen, :o8ia
Jar88o Kemppainen, S.mbio
Jau8o K.lmaoFa, S.mbio
Je'' "olE, :1$!$
Je'' Le&er, Qualcomm
Je'' 1i&il, Qualcomm
Jerem. Sandmel, pple
Jerem. -horne, "roadcom
Jim ,au2#ell, "roadcom
Jinsun& Kim, ,uone
Ji.oun& `oon, ,uone
John Kessenich, LunarG
Jon Kenned., (!Labs
147
13 ,c-no.led'#ents
Jon Leech, Khronos
Jonathan 3utsman, $ma&ination
-echnolo&ies
Jrn :.stad, R<
Jussi Rasanen, :1$!$
Kalle Raita, dra#Elements
Kari 3ulli, :o8ia
Keith =hit#ell, 1<#are
Kent <iller, :etlo&ic
<icros.stems
Kimmo :i88anen, :o8ia
Konsta Karsisto, :o8ia
KrE.sEto' Kamins8i, $ntel
Larr. Seiler, $ntel
Lars Remes, S.mbio
Lee -homason, dobe
Le'an ahon&, 1ivante
<arcus LorentEon, Ericsson
<ar8 "utler, $ma&ination
-echnolo&ies
<ar8 %allo#, ,i
<ar8 %ress#ell, "roadcom
<ar8 Sn.der, lt So't#are
<ar8 `oun&, <!
<athieu Robart, S-<
<att :etsch, Qualcomm
<att Russo, <atro2
<aurice Ribble, <! U
Qualcomm
<a2 KaEa8ov, !<3
<i8a 3esonen, :o8ia
<i8e %ai, 1ivante
<i8e =eiblen, aebra $ma&in&
<ila Smith, <!
:a8hoon "ae8, K.un&poo8
9niverist.
:ate ,uan&, :1$!$
:eil -revett, :1$!$
:elson Kidd, $ntel
:ic8 ,aemel, <! U
:1$!$
:ic8 3en#arden, Epic Games
:i8las Smedber&, Epic Games
:iEar Romdan, R<
Oliver B, 4uFitsu
3at "ro#n, :1$!$
3aul Ru&&ieri, Qualcomm
3er =ennersten, Ericsson
3etri -alalla, S.mbio
3hil ,u2le., aiiLabs
3hilip ,atcher, 4reescale
3iers !aniell, :1$!$
3iotr -omasEe#s8i, Ericsson
3iotr 9mins8i, $ntel
Rami <a.er, Samsun&
Rauli Laati8ainen, Ri&ht=are
Rob "arris, :1$!$
Rob Simpson, Qualcomm
RoF Lan&hi, 1ivante
Rune ,olm, R<
Sami K.ostila, :o8ia
Sean Ellis, R<
Sheree' Shehata, -$
Sila Ka.o, :o8ia
Sla#omir GraFe#s8i, $ntel
Steve ,ill, S-< U "roadcom
Steven Olne., !<3
Suman Sharma, $ntel
-apani 3alli, :o8ia
-eemu Laa8so, S.mbio
-ero Karras, :1$!$
-imo Suoranta, $ma&ination
-echnolo&ies
-om %oo8se., R<
-om <cRe.nolds, :1$!$
-om Olson, -$ U R<
-omi arnio, :o8ia
-omm. sano, -a8umi
=es "an&, :o8ia
`anFun ahan&, 1ivante
`uan =an&, $ma&ination
-echnolo&ies
148
14 /or#ati0e 1e+erences
1% 7ormatie :eferences
1. -he OpenGLb ES Graphics S.stem 1ersion (.))
*. -he OpenGLb ES Shadin& Lan&ua&e 1ersion 1.))
(. -he OpenGLb Graphics S.stem: Speci'ication ?1ersions (.( ^ +.*@
+. $nternational Standard $SO>$E% 1+55*:1665?E@. 3ro&rammin& Lan&ua&es ^ %OO
/. $nternational Standard $SO>$E% 7+7:1661. $n'ormation technolo&. ; $SO 0;bit coded character
set 'or in'ormation interchan&e -he 9nicode Standard 1ersion 7.) ^ %ore Speci'ication
149