glsl specificacion 1.20.8
TRANSCRIPT
-
8/12/2019 GLSL Specificacion 1.20.8
1/93
The OpenGLShading Language
Language Version: 1.20
Document Revision: 8
07-Sept-2006
John Kessenich
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
-
8/12/2019 GLSL Specificacion 1.20.8
2/93
Copyright !""!#!""$ %Dla&s, 'nc. (td.
)his docu*ent contains unpu&lished in+or*ation o+ %Dla&s, 'nc. (td.
)his docu*ent is protected &y copyright, and contains in+or*ation proprietary to %Dla&s, 'nc. (td.Any copying, adaptation, distri&ution, pu&lic per+or*ance, or pu&lic display o+ this docu*ent
without the epress written consent o+ %Dla&s, 'nc. (td. is strictly prohi&ited. )he receipt orpossession o+ this docu*ent does not convey any rights to reproduce, disclose, or distri&ute itscontents, or to *anu+acture, use, or sell anything that it *ay descri&e, in whole or in part.
)his docu*ent contains intellectual property o+ %Dla&s 'nc. (td., &ut does not grant any license toany intellectual property +ro* %Dla&s or any third party. 't is %Dla&s- intent that should an pen/(!." A0' speci+ication &e rati+ied &y the ARB incorporating all or part o+ the contents o+ thisdocu*ent, then %Dla&s would grant a royalty +ree license to ilicon /raphics, 'nc., according to theARB &ylaws, +or only that %Dla&s intellectual property as is re2uired to produce a con+or*anti*ple*entation.
)his speci+ication is provided 3A '3 4')5 6 4ARRA6)'7 45A)7V7R, 457)57R780R7, '90('7D, )A))R;, R )57R4'7, %D(AB 780R7(; D'C(A'9A6; 4ARRA6); < 97RC5A6)AB'(');, 66#'6?>.
pen/( is a registered trade*arE o+ ilicon /raphics 'nc.
ii
-
8/12/2019 GLSL Specificacion 1.20.8
3/93
-
8/12/2019 GLSL Specificacion 1.20.8
4/93
F.> 0recision and 0recision Guali+iers...................................................................................... !$F.$ Variance and the 'nvariant Guali+ier................................................................................... !$
F.$.1 )he 'nvariant Guali+ier................................................................................................ !$
F.$.! 'nvariance o+ Constant 7pressions............................................................................ !F.? rder o+ Guali+ication......................................................................................................... !
> perators and 7pressions.........................................................................................................!@>.1 perators............................................................................................................................. !@>.! Array perations................................................................................................................ %">.% .F.1 Conversion and calar Constructors........................................................................... %">.F.! Vector and 9atri Constructors.................................................................................. %">.F.% tructure Constructors.................................................................................................%!>.F.F Array Constructors...................................................................................................... %%
>.> Vector Co*ponents.............................................................................................................%%>.$ 9atri Co*ponents.............................................................................................................%F>.? tructure and Array perations...........................................................................................%>>. Assign*ents........................................................................................................................ %>>.@ 7pressions......................................................................................................................... %$>.1" Vector and 9atri perations...........................................................................................%
$ tate*ents and tructure............................................................................................................F1$.1 "?.$ Varying Varia&les................................................................................................................>%
Built#in >.1 Angle and )rigono*etry $.! 7ponential ?
.% Co**on ?.F /eo*etric @.> 9atri
-
8/12/2019 GLSL Specificacion 1.20.8
5/93
.$ Vector Relational
-
8/12/2019 GLSL Specificacion 1.20.8
6/93
-
8/12/2019 GLSL Specificacion 1.20.8
7/93
1 Introduction
)his docu*ent speci+ies version 1.!" o+ the pen/( hading (anguage. 't re2uires HHV7R'6HH to &e
1!", and #versionto accept 11" or 1!".
1.1 Acknowledgments
)his speci+ication is &ased on the worE o+ those who contri&uted to version 1.1" o+ the pen/( (anguage
peci+ication, the pen/( 7 !." (anguage peci+ication, version 1.1", and the +ollowing contri&utors to
this version:
6icE Burns
Chris Dodd9ichael /old
Je++ Juliano
Jon (eech
Bill (icea#Kane
Barthold (ichten&elt
Ben=a*in (ipchaE
'an Ro*anicE
John Rosasco
Jere*y and*el
Ro&ert i*pson
7sEil teen&erg
1.2 Changes
Changes +ro* revision ? o+ version 1.!"
'ssue 1%-s resolution is &rought up to date with the change agreed on in revision ? regarding nu*&er o+
attri&ute slots &eing the nu*&er o+ colu*ns in a *atri.
Restated the arith*etic operator &ehavior in section >.@ with su&ullets instead o+ too long o+ a
paragraph.
Changes +ro* revision $ o+ version 1.!"
)he gra**ar is &rought up to date:
*ethod support +or I.length
constructors si*pli+ied and recognied through typeHspeci+ier
array type synta is supported I+loatL>M, and array initialiers are added
-
8/12/2019 GLSL Specificacion 1.20.8
8/93
1 Introduction
Deleted the last paragraph o+ section >.1", which redundantly or inconsistently re#stated section >.@,
and *ade sure all its valid contents were incorporated into the arith*etic#&inary#operator &ullet in
section >.@.
Clari+y that despite invariant only &eing +or verte outputs, the invariant declarations have to *atch
&etween the verte and +rag*ent sides, +or *atching na*es.
Changes +ro* revision > o+ version 1.!"
Re*oved notation showing additions and deletions to the speci+ication, added ta&le o+ contents, and
cleaned up so*e cross#re+erences and so*e resulting tet#+low issues. 6o +unctional changes.
Changes +ro* revision F o+ version 1.!"
pdated the gra**ar. 't still has to &e validated.
Re*oved e*&edded structures to *atch 7, waiting +or scoping operator to access e*&edded type.
Constant epressions are co*puted in an invariant way.
se o+ invariantand centroid*ust *atch &etween verte and +rag*ent shaders.
9ade the distinction &etween a shader as a co*pilation unit and a shader as an eecuta&le.
Clari+ied there is no line continuation character, and that co**ents don-t delete new lines.
9any changes to reduce di++erences when co*pared to the 7 speci+ication.
Changes +ro* revision % o+ version 1.!"
Add the invariantEeyword and its support.
Allow unsied array constructors. )his still *aEes an eplicitly sied array.
Re2uire eplicitly sied arrays +or assign*ent and co*parison.
Allow siing unsied array declaration through initialier.
Di++erent co*pilation units can &e di++erent language versions
Add CNN style na*e hiding rules
Reserve lowp, mediump, highp, and precision.
Changes +ro* revision 1 o+ version 1.!"
Disallow other signaturesreturn#values o+ main.
Clari+y that O: can have the sa*e type !ndand %rdoperands e.g. conversion is not re2uired.
ay that point sprites have to &e ena&led to get de+ined values +ro* glH0ointCoord
eparate out and distinguish &etween storage and para*eter 2uali+iers, *aEing it easier to add the
invariant 2uali+ier.
#version 120is re2uired to use version 1.!"
*at!% *eans ! colu*ns, % rows
*atri construction +ro* *atri allowed
2
-
8/12/2019 GLSL Specificacion 1.20.8
9/93
1 Introduction
added transpose
signature *atching taEes type conversions into account, a*&iguity is an error
Changes +ro* revision $" o+ version 1.1"
Accept 3+3 as part o+ a +loating#point constant. 7.g. 3+loat g P %.>+3.
Auto*atically convert integer types to +loat types, as needed &y contet.
Allow initialiers on uni+or* declarations. )he value is set at linE ti*e.
Allow &uilt#in +unction calls in const initialiers.
upport non#s2uare *atrices.
Add matrixProduct Lnow outerProduct(M +or *ultiplying vectors to yield a *atri.
Arrays &eco*e +irst class o&=ects, with constructors, co*parison, length, etc.
Add glH0ointCoord +or +rag*ent shaders to 2uery a +rag*ent-s position within a point sprite.
upport centroid interpolation on *ulti#sa*ple varyings.
1.3 Overview
)his docu*ent descri&es The pen!L Sha"ing Language# version 1.20.
'ndependent co*pilation units written in this language are calledsha"ers. Aprogramis a co*plete set o+
shaders that are co*piled and linEed together. )he ai* o+ this docu*ent is to thoroughly speci+y the
progra**ing language. )he pen/( entry points used to *anipulate and co**unicate with progra*s
and shaders are de+ined in a separate speci+ication.
1. !rror "andling
Co*pilers, in general, accept progra*s that are ill#+or*ed, due to the i*possi&ility o+ detecting all ill#
+or*ed progra*s. 0orta&ility is only ensured +or well#+or*ed progra*s, which this speci+ication
descri&es. Co*pilers are encouraged to detect ill#+or*ed progra*s and issue diagnostic *essages, &ut are
not re2uired to do so +or all cases. Co*pilers are re2uired to return *essages regarding leically,
gra**atically, or se*antically incorrect shaders.
1.# T$pographical Conventions
'talic, &old, and +ont choices have &een used in this speci+ication pri*arily to i*prove reada&ility. Code
+rag*ents use a +ied width +ont. 'denti+iers e*&edded in tet are italicied. Keywords e*&edded in tet
are &old. perators are called &y their na*e, +ollowed &y their sy*&ol in &old in parentheses. )he
clari+ying gra**ar +rag*ents in the tet use &old +or literals and italics +or non#ter*inals. )he o++icial
gra**ar in ection @ Ihading (anguage /ra**ar uses all capitals +or ter*inals and lower case +ornon#ter*inals.
3
-
8/12/2019 GLSL Specificacion 1.20.8
10/93
2 Overview of OpenGL Shading
)he pen/( hading (anguage is actually two closely related languages. )hese languages are used to
create shaders +or the progra**a&le processors contained in the pen/( processing pipeline.
nless otherwise noted in this paper, a language +eature applies to all languages, and co**on usage will
re+er to these languages as a single language. )he speci+ic languages will &e re+erred to &y the na*e o+
the processor they target: verte or +rag*ent.
Any pen/( state used &y the shader is auto*atically tracEed and *ade availa&le to shaders. )his
auto*atic state tracEing *echanis* allows the application to use eisting pen/( state co**ands +or
state *anage*ent and have the current values o+ such state auto*atically availa&le +or use in a shader.
2.1 %erte& 'rocessor
)he verte$ processoris a progra**a&le unit that operates on inco*ing vertices and their associated data.
Co*pilation units written in the pen/( hading (anguage to run on this processor are called verte$
sha"ers. 4hen a co*plete set o+ verte shaders are co*piled and linEed, they result in a verte$ sha"er
e$ecuta%ðat runs on the verte processor.
)he verte processor operates on one verte at a ti*e. 't does not replace graphics operations that re2uire
Enowledge o+ several vertices at a ti*e. )he verte shaders running on the verte processor *ust
co*pute the ho*ogeneous position o+ the inco*ing verte.
2.2 (rag)ent 'rocessor
)he'ragment processoris a progra**a&le unit that operates on +rag*ent values and their associated
data. Co*pilation units written in the pen/( hading (anguage to run on this processor are called
'ragment shaders. 4hen a co*plete set o+ +rag*ent shaders are co*piled and linEed, they result in a
'ragment sha"er e$ecuta%ðat runs on the +rag*ent processor.
A +rag*ent shader cannot change a +rag*ent-s y position. Access to neigh&oring +rag*ents is not
allowed. )he values co*puted &y the +rag*ent shader are ulti*ately used to update +ra*e#&u++er *e*ory
or teture *e*ory, depending on the current pen/( state and the pen/( co**and that caused the
+rag*ents to &e generated.
4
-
8/12/2019 GLSL Specificacion 1.20.8
11/93
3 *asics
3.1 Character Set
)he source character set used +or the pen/( shading languages is a su&set o+ AC''. 't includes the
+ollowing characters:
)he letters a!", A!, and the underscore H.
)he nu*&ers0!$.
)he sy*&ols period ., plus %, dash !, slash &, asterisE ', percent , angled &racEets )and
*, s2uare &racEets +and , parentheses (and , &races -and , caret /, vertical &ar ,
a*persand , tilde , e2uals 3, ecla*ation point 4, colon 5, se*icolon 6, co**a ,, and2uestion *arE 7.
)he nu*&er sign # +or preprocessor use.
4hite space: the space character, horiontal ta&, vertical ta&, +or* +eed, carriage#return, and line#
+eed.
(ines are relevant +or co*piler diagnostic *essages and the preprocessor. )hey are ter*inated &y
carriage#return or line#+eed. '+ &oth are used together, it will count as only a single line ter*ination.
-
8/12/2019 GLSL Specificacion 1.20.8
12/93
3 Basics
3.3 'reprocessor
)here is a preprocessor that processes the source strings as part o+ the co*pilation process.
)he co*plete list o+ preprocessor directives is as +ollows.
#
#define
#undef
#if
#ifdef
#ifndef
#else
#elif
#endif
#error
#pragma
#extension
#version
#line
)he +ollowing operators are also availa&le
defined
7ach nu*&er sign # can &e preceded in its line only &y spaces or horiontal ta&s. 't *ay also &e
+ollowed &y spaces and horiontal ta&s, preceding the directive. 7ach directive is ter*inated &y a new#
line. 0reprocessing does not change the nu*&er or relative location o+ new#lines in a source string.)he nu*&er sign # on a line &y itsel+ is ignored. Any directive not listed a&ove will cause a diagnostic
*essage and *aEe the i*ple*entation treat the shader as ill#+or*ed.
#de8ineand #unde8+unctionality are de+ined as is standard +or CNN preprocessors +or *acro de+initions
&oth with and without *acro para*eters.
)he +ollowing prede+ined *acros are availa&le
__LINE__
__FILE__
__VERSION__
((L)*+((will su&stitute a deci*al integer constant that is one *ore than the nu*&er o+ preceding new#
lines in the current source string.((,)L+((will su&stitute a deci*al integer constant that says which source string nu*&er is currently
&eing processed.
6
-
8/12/2019 GLSL Specificacion 1.20.8
13/93
3 Basics
((V+RS)*((will su&stitute a deci*al integer re+lecting the version nu*&er o+ the pen/( shading
language. )he version o+ the shading language descri&ed in this docu*ent will have((V+RS)*((
su&stitute the deci*al integer 1!".
All *acro na*es containing two consecutive underscores 99 are reserved +or +uture use as prede+ined
*acro na*es. All *acro na*es pre+ied with I/(H I/( +ollowed &y a single underscore are also
reserved.
#i8, #i8de8, #i8nde8, #else, #eli8, and#endi8are de+ined to operate as is standard +or CNN preprocessors.
7pressions +ollowing #i8and #eli8are restricted to epressions operating on literal integer constants, plus
identi+iers consu*ed &y the de8inedoperator. Character constants are not supported. )he operators
availa&le are as +ollows.
'recedence Operator class Operators +ssociativit$
1 highest parenthetical grouping 6A
! unary de+inedN # S T
Right to (e+t
% *ultiplicative U (e+t to Right
F additive N # (e+t to Right
> &it#wise shi+t WW XX (e+t to Right
$ relational W X WP XP (e+t to Right
? e2uality PP TP (e+t to Right
&it#wise and Y (e+t to Right
@ &it#wise eclusive or Z (e+t to Right
1" &it#wise inclusive or [ (e+t to Right11 logical and YY (e+t to Right
1! lowest logical inclusive or [ [ (e+t to Right
)he de8inedoperator can &e used in either o+ the +ollowing ways:
defined identifier
defined( identifier )
)here are no nu*&er sign &ased operators no #, #:, ##, etc., nor is there a si"eo8operator.
)he se*antics o+ applying operators to integer literals in the preprocessor *atch those standard in the
CNN preprocessor, not those in the pen/( hading (anguage.
0reprocessor epressions will &e evaluated according to the &ehavior o+ the host processor, not the
processor targeted &y the shader.
7
-
8/12/2019 GLSL Specificacion 1.20.8
14/93
3 Basics
#error will cause the i*ple*entation to put a diagnostic *essage into the shader o&=ectQs in+or*ation log
see the A0' in eternal docu*entation +or how to access a shader o&=ectQs in+or*ation log. )he *essage
will &e the toEens +ollowing the #errordirective, up to the +irst new#line. )he i*ple*entation *ust then
consider the shader to &e ill#+or*ed.
#pragmaallows i*ple*entation dependent co*piler control. )oEens +ollowing #pragmaare not su&=ect
to preprocessor *acro epansion. '+ an i*ple*entation does not recognie the toEens +ollowing
#pragma, then it will ignore that prag*a. )he +ollowing prag*as are de+ined as part o+ the language.
#pragma ST!L
)he ;?prag*a is used to reserve prag*as +or use &y +uture revisions o+ this language. 6o
i*ple*entation *ay use a prag*a whose +irst toEen is ;?.
#pragma optimi"eon)
#pragma optimi"eoff)
can &e used to turn o++ opti*iations as an aid in developing and de&ugging shaders. 't can only &e used
outside +unction de+initions. By de+ault, opti*iation is turned on +or all shaders. )he de&ug prag*a
#pragma de$ugon)
#pragma de$ugoff)
can &e used to ena&le co*piling and annotating a shader with de&ug in+or*ation, so that it can &e used
with a de&ugger. 't can only &e used outside +unction de+initions. By de+ault, de&ug is turned o++.
haders should declare the version o+ the language they are written to. )he language version a shader is
written to is speci+ied &y
#version number
where num%er*ust &e a version o+ the language, +ollowing the sa*e convention as((V+RS)*((a&ove.
)he directive I#version 1!" is re2uired in any shader that uses version 1.!" o+ the language. Anynum%errepresenting a version o+ the language a co*piler does not support will cause an error to &e
generated. Version 1.1" o+ the language does not re2uire shaders to include this directive, and shaders that
do not include a #versiondirective will &e treated as targeting version 1.1". )he &ehavior o+ shaders
targeting version 1.1" will not &e e++ected &y any changes introduced in version 1.!". Di++erent shaders
co*pilation units that are linEed together in the sa*e progra* do not have to have the sa*e version
they can &e a *i o+ version 1.1" and version 1.!" shaders.
)he #versiondirective *ust occur in a shader &e+ore anything else, ecept +or co**ents and white space.
8
-
8/12/2019 GLSL Specificacion 1.20.8
15/93
3 Basics
By de+ault, co*pilers o+ this language *ust issue co*pile ti*e syntactic, gra**atical, and se*antic
errors +or shaders that do not con+or* to this speci+ication. Any etended &ehavior *ust +irst &e ena&led.
Directives to control the &ehavior o+ the co*piler with respect to etensions are declared with the
#extensiondirective
#extension extension_name:behavior
#extension all :behavior
where e$tension(nameis the na*e o+ an etension. 7tension na*es are not docu*ented in this
speci+ication. )he toEen all*eans the &ehavior applies to all etensions supported &y the co*piler. )he
%ehavior can &e one o+ the +ollowing
behavior !ffect
re,uire Behave as speci+ied &y the etension e$tension(name.
/ive an error on the #extension i+ the etension e$tension(name is notsupported, or i+ allis speci+ied.
enable Behave as speci+ied &y the etension e$tension(name.
4arn on the #extensioni+ the etension e$tension(nameis not supported.
/ive an error on the #extension i+ allis speci+ied.
warn Behave as speci+ied &y the etension e$tension(name, ecept issue warningson any detecta&le use o+ that etension, unless such use is supported &y otherena&led or re2uired etensions.
'+ all is speci+ied, then warn on all detecta&le uses o+ any etension used.
4arn on the #extensioni+ the etension e$tension(nameis not supported.
disable Behave including issuing errors and warnings as i+ the etensione$tension(nameis not part o+ the language de+inition.
'+ all is speci+ied, then &ehavior *ust revert &acE to that o+ the non#etendedcore version o+ the language &eing co*piled to.
4arn on the #extension i+ the etension e$tension(nameis not supported.
)he extensiondirective is a si*ple, low#level *echanis* to set the &ehavior +or each etension. 't does
not de+ine policies such as which co*&inations are appropriate, those *ust &e de+ined elsewhere. rder
o+ directives *atters in setting the &ehavior +or each etension: Directives that occur later override those
seen earlier. )he allvariant sets the &ehavior +or all etensions, overriding all previously issued
extension directives, &ut only +or the %ehaviorswarnand disa@le.
9
-
8/12/2019 GLSL Specificacion 1.20.8
16/93
3 Basics
)he initial state o+ the co*piler is as i+ the directive
#extension all % disa$le
was issued, telling the co*piler that all error and warning reporting *ust &e done according to thisspeci+ication, ignoring any etensions.
7ach etension can de+ine its allowed granularity o+ scope. '+ nothing is said, the granularity is a shader
that is, a single co*pilation unit, and the etension directives *ust occur &e+ore any non#preprocessor
toEens. '+ necessary, the linEer can en+orce granularities larger than a single co*pilation unit, in which
case each involved shader will have to contain the necessary etension directive.
9acro epansion is not done on lines containing #extensionand #versiondirectives.
#line*ust have, a+ter *acro su&stitution, one o+ the +ollowing two +or*s:
#line line
#line line source-string-number
where &ine andsource-string-num%er are constant integer epressions. A+ter processing this directiveincluding its new#line, the i*ple*entation will &ehave as i+ it is co*piling at line nu*&er &ine1and
source string nu*&ersource-string-num%er. u&se2uent source strings will &e nu*&ered se2uentially,
until another #linedirective overrides that nu*&ering.
3. Co))ents
Co**ents are deli*ited &y U and U, or &y and a new#line. )he &egin co**ent deli*iters U or are
not recognied as co**ent deli*iters inside o+ a co**ent, hence co**ents cannot &e nested. '+ a
co**ent resides entirely within a single line, it is treated syntactically as a single space. 6ew#lines are
not eli*inated &y co**ents.
3.# To-ens)he language is a se2uence o+ toEens. A toEen can &e
toen:
e/or"
i"enti'ier
integer-constant
'&oating-constant
operator
6 -
10
-
8/12/2019 GLSL Specificacion 1.20.8
17/93
3 Basics
3. /e$words
)he +ollowing are the Eeywords in the language, and cannot &e used +or any other purpose than that
de+ined &y this docu*ent: attri@ute const uni8orm varing
centroid
@reak continue do 8or while
i8 else
in out inout
8loat int void @ool true 8alse
invariant
discard return
mat2 matB matC
mat2x2 mat2xB mat2xC
matBx2 matBxB matBxC
matCx2 matCxB matCxC
vec2 vecB vecC ivec2 ivecB ivecC @vec2 @vecB @vecC
sampler1= sampler2= samplerB= samplerDu@e
sampler1=;hadow sampler2=;hadow
struct
)he +ollowing are the Eeywords reserved +or +uture use. sing the* will result in an error:
asm
class union enum tpede8 template this packed
goto switch de8ault
inline noinline volatile pu@lic static extern external inter8ace
long short dou@le hal8 8ixed unsigned
lowp mediump highp precision
input output
hvec2 hvecB hvecC dvec2 dvecB dvecC 8vec2 8vecB 8vecC
sampler2=Eect samplerB=Eect sampler2=Eect;hadow
si"eo8 cast
namespace using
'n addition, all identi+iers containing two consecutive underscores 99 are reserved as possi&le +uture
Eeywords.
11
-
8/12/2019 GLSL Specificacion 1.20.8
18/93
3 Basics
3.0 Identifiers
'denti+iers are used +or varia&le na*es, +unction na*es, struct na*es, and +ield selectors +ield selectors
select co*ponents o+ vectors and *atrices si*ilar to structure +ields, as discussed in ection >.> IVectorCo*ponents and ection >.$ I9atri Co*ponents . 'denti+iers have the +or*
i"enti'ier
non"igit
i"enti'ier non"igit
i"enti'ier "igit
non"igit: one o+
9 a @ c d e 8 g h i F k l m n o p G r s t u v w x "
A H D = I J > K L M N ? O Q P R E ; < S T U V W
"igit: one o+
0 1 2 B C X Y Z [ $
'denti+iers starting with IglH are reserved +or use &y pen/(, and *ay not &e declared in a shader as
either a varia&le or a +unction.
12
-
8/12/2019 GLSL Specificacion 1.20.8
19/93
%ariables and T$pes
All varia&les and +unctions *ust &e declared &e+ore &eing used. Varia&le and +unction na*es are
identi+iers.
)here are no de+ault types. All varia&le and +unction declarations *ust have a declared type, and
optionally 2uali+iers. A varia&le is declared &y speci+ying its type +ollowed &y one or *ore na*es
separated &y co**as. 'n *any cases, a varia&le can &e initialied as part o+ its declaration &y using the
assign*ent operator 3. )he gra**ar near the end o+ this docu*ent provides a +ull re+erence +or the
synta o+ declaring varia&les.
ser#de+ined types *ay &e de+ined using structto aggregate a list o+ eisting types into a single na*e.
)he pen/( hading (anguage is type sa+e. )here are no i*plicit conversions &etween types, with theeception that an integer value *ay appear where a +loating#point type is epected, and &e converted to a
+loating#point value. 7actly how and when this can occur is descri&ed in ection F.1.1"I'*plicit
Conversions and as re+erenced &y other sections in this speci+ication.
.1 *asic T$pes
)he pen/( hading (anguage supports the +ollowing &asic data types.
T$pe eaning
void +or +unctions that do not return a value
@ool a conditional type, taEing on values o+ true or +alse
int a signed integer
8loat a single +loating#point scalar
vec2 a two co*ponent +loating#point vector
vecB a three co*ponent +loating#point vector
vecC a +our co*ponent +loating#point vector
@vec2 a two co*ponent Boolean vector
@vecB a three co*ponent Boolean vector
@vecC a +our co*ponent Boolean vector
ivec2 a two co*ponent integer vector
ivecB a three co*ponent integer vector
ivecC a +our co*ponent integer vector
mat2 a !\! +loating#point *atri
matB a %\% +loating#point *atri
13
-
8/12/2019 GLSL Specificacion 1.20.8
20/93
4 Variables and Types
T$pe eaning
matC a F\F +loating#point *atri
mat2x2 sa*e as a mat2mat2xB a +loating#point *atri with ! colu*ns and % rows
mat2xC a +loating#point *atri with ! colu*ns and F rows
matBx2 a +loating#point *atri with % colu*ns and ! rows
matBxB sa*e as a matB
matBxC a +loating#point *atri with % colu*ns and F rows
matCx2 a +loating#point *atri with F colu*ns and ! rows
matCxB a +loating#point *atri with F colu*ns and % rows
matCxC sa*e as a matC
sampler1= a handle +or accessing a 1D teturesampler2= a handle +or accessing a !D teture
samplerB= a handle +or accessing a %D teture
samplerDu@e a handle +or accessing a cu&e *apped teture
sampler1=;hadow a handle +or accessing a 1D depth teture with co*parison
sampler2=;hadow a handle +or accessing a !D depth teture with co*parison
'n addition, a shader can aggregate these using arrays and structures to &uild *ore co*ple types.
)here are no pointer types.
.1.1 %oid
-
8/12/2019 GLSL Specificacion 1.20.8
21/93
4 Variables and Types
.1.3 Integers
'ntegers are *ainly supported as a progra**ing aid. At the hardware level, real integers would aid
e++icient i*ple*entation o+ loops and array indices, and re+erencing teture units. 5owever, there is no
re2uire*ent that integers in the language *ap to an integer type in hardware. 't is not epected that
underlying hardware has +ull support +or a wide range o+ integer operations. Because o+ their intended
li*ited purpose, integers are li*ited to 1$ &its o+ precision, plus a sign representation in &oth the verte
and +rag*ent languages. An pen/( hading (anguage i*ple*entation *ay convert integers to +loats to
operate on the*. An i*ple*entation is allowed to use *ore than 1$ &its o+ precision to *anipulate
integers. 5ence, there is no porta&le wrapping &ehavior. haders that over+low the 1$ &its o+ precision
*ay not &e porta&le.
'ntegers are declared and optionally initialied with integer epressions as in the +ollowing ea*ple:
int i- . , /0'
(iteral integer constants can &e epressed in deci*al &ase 1", octal &ase , or headeci*al &ase 1$
as +ollows.
integer-constant :
"ecima&-constant
octa&-constant
he$a"ecima&-constant
"ecima&-constant :
nonero-"igit
"ecima&-constant "igit
octa&-constant :
0
octa&-constant octa&-"igit
he$a"ecima&-constant :
"he$a"ecima&-"igit
"8he$a"ecima&-"igit
he$a"ecima&-constant he$a"ecima&-"igit
"igit :
0
nonero-"igit
nonero-"igit : one o+
1 2 B C X Y Z [ $
octa&-"igit5 one o+
0 1 2 B C X Y Z
15
-
8/12/2019 GLSL Specificacion 1.20.8
22/93
4 Variables and Types
he$a"ecima&-"igit5one o+
0 1 2 B C X Y Z [ $
a @ c d e 8
A H D = I J
6o white space is allowed &etween the digits o+ an integer constant, including a+ter the leading 0or a+ter
the leading 0xor 0Vo+ a constant. A leading unary *inus sign # is interpreted as an arith*etic unary
negation, not as part o+ the constant. )here are no letter su++ies.
.1. (loats
-
8/12/2019 GLSL Specificacion 1.20.8
23/93
4 Variables and Types
A deci*al point . is not needed i+ the eponent part is present. 6o white space *ay appear anywhere
within a +loating#point constant. A leading unary *inus sign ! is interpreted as a unary operator and is
not part o+ the +loating#point constant
.1.# %ectors
)he pen/( hading (anguage includes data types +or generic !#, %#, and F#co*ponent vectors o+
+loating#point values, integers, or Booleans.
-
8/12/2019 GLSL Specificacion 1.20.8
24/93
4 Variables and Types
.1. Structures
ser#de+ined types can &e created &y aggregating other already de+ined types into a structure using the
structEeyword.
-
8/12/2019 GLSL Specificacion 1.20.8
25/93
4 Variables and Types
.1. +rra$s
Varia&les o+ the sa*e type can &e aggregated into arrays &y declaring a na*e +ollowed &y &racEets +
enclosing an optional sie. 4hen an array sie is speci+ied in a declaration, it *ust &e an integral constant
epression see ection F.%.% IConstant 7pressions greater than ero. '+ an array is indeed with an
epression that is not an integral constant epression, or i+ an array is passed as an argu*ent to a +unction,
then its sie *ust &e declared &e+ore any such use. 't is legal to declare an array without a sie and then
later re#declare the sa*e na*e as an array o+ the sa*e type and speci+y a sie. 't is illegal to declare an
array with a sie, and then later in the sa*e shader inde the sa*e array with an integral constant
epression greater than or e2ual to the declared sie. 't is also illegal to inde an array with a negative
constant epression. Arrays declared as +or*al para*eters in a +unction declaration *ust speci+y a sie.
nde+ined &ehavior results +ro* indeing an array with a non#constant epression thatQs greater than or
e2ual to the arrayQs sie or less than ". nly one#di*ensional arrays *ay &e declared. All &asic types and
structures can &e +or*ed into arrays. o*e ea*ples are:
float fre=uen&ies>4?'
uniform ve&/ lig:t@osition>/?'
lig:t lig:ts>?'
&onst int numLig:ts , 0'
lig:t lig:ts>numLig:ts?'
An array type can &e +or*ed &y speci+ying a type +ollowed &y s2uare &racEets L M and including a sie:
float>3?
)his type can &e used anywhere any other type can &e used, including as the return value +ro* a +unction
float>3? foo) ; 3?42/- /20- 32A- 320- 121)
as an unna*ed para*eter
void foofloat>3?)
and as an alternate way o+ declaring a varia&le or +unction para*eter.
float>3? a'
't is an error to declare arrays o+ arrays:
float a>3?>4?' (( illegal
float>3? a>4?' (( illegal
19
-
8/12/2019 GLSL Specificacion 1.20.8
26/93
4 Variables and Types
Arrays can have initialiers +or*ed +ro* array constructors:
float a>3? , float>3?42/- /20- 32A- 320- 121)'
float a>3? , float>?42/- /20- 32A- 320- 121)' (( same t:ing
nsied arrays can &e eplicitly sied &y an initialier at declaration ti*e:
float a>3?'
222
float $>? , a' (( $ is expli&itl5 si"e 3
float $>3? , a' (( means t:e same t:ing
5owever, i*plicitly sied arrays cannot &e assigned to. 6ote, this is a rare case that initialiers and
assign*ents appear to have di++erent se*antics.
Arrays Enow the nu*&er o+ ele*ents they contain. )his can &e o&tained &y using the length *ethod:
a2lengt:)' (( returns 3 for t:e a$ove de&larations
)he length *ethod cannot &e called on an array that has not &een eplicitly sied.
.1.14 I)plicit Conversions
'n so*e situations, an epression and its type will &e i*plicitly converted to a di++erent type. )he
+ollowing ta&le shows all allowed i*plicit conversions:
T$pe of e&pression Can be i)plicitl$ converted to
int 8loat
ivec2 vec2
ivecB vecBivecC vecC
)here are no i*plicit array or structure conversions.
-
8/12/2019 GLSL Specificacion 1.20.8
27/93
4 Variables and Types
.2 Scoping
)he scope o+ a varia&le is deter*ined &y where it is declared. '+ it is declared outside all +unction
de+initions, it has glo&al scope, which starts +ro* where it is declared and persists to the end o+ the shaderit is declared in. '+ it is declared in a whiletest or a 8or state*ent, then it is scoped to the end o+ the
+ollowing sustate*ent. therwise, i+ it is declared as a state*ent within a co*pound state*ent, it is
scoped to the end o+ that co*pound state*ent. '+ it is declared as a para*eter in a +unction de+inition, it is
scoped until the end o+ that +unction de+inition. A +unction &ody has a scope nested inside the +unctionQs
de+inition. )he i8state*entQs epression does not allow new varia&les to &e declared, hence does not
+or* a new scope.
4ithin a declaration, the scope o+ a na*e starts i**ediately a+ter the initialier i+ present or i**ediately
a+ter the na*e &eing declared i+ not. everal ea*ples:
int x , 1'
;
int x , 0- 5 , x' (( 5 is initiali"ed to 0
additive % ! (e+t to Right
$ &it#wise shi+t reserved )) ** (e+t to Right
? relational ) * )3 *3 (e+t to Right
e2uality 33 43 (e+t to Right
@ &it#wise and reserved (e+t to Right
1" &it#wise eclusive or reserved / (e+t to Right
11 &it#wise inclusive or reserved (e+t to Right
1! logical and (e+t to Right
1% logical eclusive or // (e+t to Right
1F logical inclusive or (e+t to Right
1> selection 7 5 Right to (e+t
1$
Assign*entarith*etic assign*ents *odulus, shi+t,and &it#wise are reserved
3
%3 !3'3 &3
3 ))3 **33 /3 3
Right to (e+t
1? lowest se2uence , (e+t to Right
)here is no address#o+ operator nor a dere+erence operator. )here is no typecast operator, constructors
are used instead.
29
-
8/12/2019 GLSL Specificacion 1.20.8
36/93
5 perators and !"pressions
#.2 +rra$ Operations
)hese are now descri&ed in ection >.? Itructure and Array perations.
#.3 (unction Calls
'+ a +unction returns a value, then a call to that +unction *ay &e used as an epression, whose type will &e
the type that was used to declare or de+ine the +unction.
-
8/12/2019 GLSL Specificacion 1.20.8
37/93
5 perators and !"pressions
'+ there is a single scalar para*eter to a vector constructor, it is used to initialie all co*ponents o+ the
constructed vector to that scalarQs value. '+ there is a single scalar para*eter to a *atri constructor, it is
used to initialie all the co*ponents on the *atriQs diagonal, with the re*aining co*ponents initialied
to ".".
'+ a vector is constructed +ro* *ultiple scalars, vectors, or *atrices, or a *iture o+ these, the vectors-
co*ponents will &e constructed in order +ro* the co*ponents o+ the argu*ents. )he argu*ents will &e
consu*ed le+t to right, and each argu*ent will have all it-s co*ponents consu*ed, in order, &e+ore any
co*ponents +ro* the net argu*ent are consu*ed. i*ilarly +or constructing a *atri +ro* *ultiple
scalars or vectors, or a *iture o+ these. 9atri co*ponents will &e constructed and consu*ed in colu*n
*a=or order. 'n these cases, there *ust &e enough co*ponents provided in the argu*ents to provide an
initialier +or every co*ponent in the constructed value. 't is an error to provide etra argu*ents &eyond
this last used argu*ent.
'+ a *atri is constructed +ro* a *atri, then each co*ponent colu*n i#row in the result that has a
corresponding co*ponent colu*n i#row in the argu*ent will &e initialied +ro* there. All other
co*ponents will &e initialied to the identity *atri. '+ a *atri argu*ent is given to a *atri constructor,
it is an error to have any other argu*ents.
'+ the &asic type @ool, int, or 8loat o+ a para*eter to a constructor does not *atch the &asic type o+ the
o&=ect &eing constructed, the scalar construction rules a&ove are used to convert the para*eters.
o*e use+ul vector constructors are as +ollows:
ve&4float) (( initiali"es ea&: &omponent of 9it: t:e float
ve&/ive&/) (( ma7es a ve&/ 9it: &omponent9ise &onversion
ve&0float- float) (( initiali"es a ve&0 9it: 0 floats
ive&4int- int- int) (( initiali"es an ive&4 9it: 4 ints
$ve&/int- int- float- float) (( uses / +oolean &onversions
ve&0ve&4) (( drops t:e t:ird &omponent of a ve&4ve&4ve&/) (( drops t:e fourt: &omponent of a ve&/
ve&4ve&0- float) (( ve&42x , ve&02x- ve&425 , ve&025- ve&42" , float
ve&4float- ve&0) (( ve&42x , float- ve&425 , ve&02x- ve&42" , ve&025
ve&/ve&4- float)
ve&/float- ve&4)
ve&/ve&0- ve&0)
o*e ea*ples o+ these are:
ve&/ &olor , ve&/A2A- 12A- A2A- 12A)'
ve&/ rg$a , ve&/12A)' (( sets ea&: &omponent to 12A
ve&4 rg$ , ve&4&olor)' (( drop t:e /t: &omponent
)o initialie the diagonal o+ a *atri with all other ele*ents set to ero:
mat0float)
mat4float)
mat/float)
31
-
8/12/2019 GLSL Specificacion 1.20.8
38/93
5 perators and !"pressions
)o initialie a *atri &y speci+ying vectors or scalars, the co*ponents are assigned to the *atri ele*ents
in colu*n#*a=or order.
mat0ve&0- ve&0)' (( one &olumn per argument
mat4ve&4- ve&4- ve&4)' (( one &olumn per argument
mat/ve&/- ve&/- ve&/- ve&/)' (( one &olumn per argument
mat4x0ve&0- ve&0- ve&0)' (( one &olumn per argument
mat0float- float- (( first &olumn
float- float)' (( se&ond &olumn
mat4float- float- float- (( first &olumn
float- float- float- (( se&ond &olumn
float- float- float)' (( t:ird &olumn
mat/float- float- float- float- (( first &olumn
float- float- float- float- (( se&ond &olumn
float- float- float- float- (( t:ird &olumn float- float- float- float)' (( fourt: &olumn
mat0x4ve&0- float- (( first &olumn
ve&0- float)' (( se&ond &olumn
A wide range o+ other possi&ilities eist, to construct a *atri +ro* vectors and scalars, as long as enough
co*ponents are present to initialie the *atri. )o construct a *atri +ro* a *atri:
mat4x4mat/x/)' (( ta7es t:e upperleft 4x4 of t:e mat/x/
mat0x4mat/x0)' (( ta7es t:e upperleft 0x0 of t:e mat/x/- last ro9 is A-A
mat/x/mat4x4)' (( puts t:e mat4x4 in t:e upperleft- sets t:e lo9er rig:t
(( &omponent to 1- and t:e rest to A
#..3 Structure Constructors
nce a structure is de+ined, and its type is given a na*e, a constructor is availa&le with the sa*e na*e to
construct instances o+ that structure.
-
8/12/2019 GLSL Specificacion 1.20.8
39/93
5 perators and !"pressions
#.. +rra$ Constructors
Array types can also &e used as constructor na*es, which can then &e used in epressions or initialiers.
4? , float>4?32A- D20- 121)'
&onst float d>4? , float>?32A- D20- 121)'
float g'
222
float a>3? , float>3?g- 1- g- 024- g)'
float $>4?'
$ , float>4?g- g 12A- g 02A)'
)here *ust &e eactly the sa*e nu*&er o+ argu*ents as the sie o+ the array &eing constructed. '+ no sie
is present in the constructor, then the array is eplicitly sied to the nu*&er o+ argu*ents provided. )heargu*ents are assigned in order, starting at ele*ent ", to the ele*ents o+ the constructed array. 7ach
argu*ent *ust &e the sa*e type as the ele*ent type o+ the array, or &e a type that can &e converted to the
ele*ent type o+ the array according to ection F.1.1"I'*plicit Conversions.
#.# %ector Co)ponents
)he na*es o+ the co*ponents o+ a vector are denoted &y a single letter. As a notational convenience,
several letters are associated with each co*ponent &ased on co**on usage o+ position, color or teture
coordinate vectors. )he individual co*ponents o+ a vector can &e selected &y +ollowing the varia&le
na*e with period . and then the co*ponent na*e.
)he co*ponent na*es supported are:
$# /# # 9 se+ul when accessing vectors that represent points or nor*als
r# g# %# a9 se+ul when accessing vectors that represent colors
s# t# p# 9 se+ul when accessing vectors that represent teture coordinates
)he co*ponent na*es$# r# andsare, +or ea*ple, synony*s +or the sa*e +irst co*ponent in a vector.
6ote that the third co*ponent o+ a teture,rin pen/(, has &een rena*edpso as to avoid the con+usion
with r+or red in a color.
Accessing co*ponents &eyond those declared +or the vector type is an error so, +or ea*ple:
ve&0 pos'
pos2x (( is legalpos2" (( is illegal
33
-
8/12/2019 GLSL Specificacion 1.20.8
40/93
5 perators and !"pressions
)he co*ponent selection synta allows *ultiple co*ponents to &e selected &y appending their na*es
+ro* the sa*e na*e set a+ter the period ..
ve&/ v/'
v/2rg$a' (( is a ve&/ and t:e same as .ust using v/-
v/2rg$' (( is a ve&4-
v/2$' (( is a float-
v/2x5' (( is a ve&0-
v/2xg$a' (( is illegal t:e &omponent names do not &ome from
(( t:e same set2
)he order o+ the co*ponents can &e di++erent to swile the*, or replicated:
ve&/ pos , ve&/12A- 02A- 42A- /2A)'
ve&/ s9i", pos29"5x' (( s9i" , /2A- 42A- 02A- 12A)
ve&/ dup , pos2xx55' (( dup , 12A- 12A- 02A- 02A)
)his notation is *ore concise than the constructor synta. )o +or* an r#value, it can &e applied to any
epression that results in a vector r#value.
)he co*ponent group notation can occur on the le+t hand side o+ an epression.
ve&/ pos , ve&/12A- 02A- 42A- /2A)'
pos2x9 , ve&032A- G2A)' (( pos , 32A- 02A- 42A- G2A)
pos29x , ve&0D2A- H2A)' (( pos , H2A- 02A- 42A- D2A)
pos2xx , ve&042A- /2A)' (( illegal BxB used t9i&e
pos2x5 , ve&412A- 02A- 42A)' (( illegal mismat&: $et9een ve&0 and ve&4
)o +or* an l#value, swiling *ust &e applied to an l#value o+ vector type, contain no duplicate
co*ponents, and it results in an l#value o+ scalar or vector type, depending on nu*&er o+ co*ponents
speci+ied.
Array su&scripting synta can also &e applied to vectors to provide nu*eric indeing. o in
ve&/ pos'
pos2;re+ers to the third ele*ent o+ pos and is e2uivalent to pos.. )his allows varia&le indeing into a
vector, as well as a generic way o+ accessing co*ponents. Any integer epression can &e used as the
su&script. )he +irst co*ponent is at inde ero. Reading +ro* or writing to a vector using a constant
integral epression with a value that is negative or greater than or e2ual to the sie o+ the vector is illegal.
4hen indeing with non#constant epressions, &ehavior is unde+ined i+ the inde is negative, or greater
than or e2ual to the sie o+ the vector.
#. atri& Co)ponents
)he co*ponents o+ a *atri can &e accessed using array su&scripting synta. Applying a single su&script
to a *atri treats the *atri as an array o+ colu*n vectors, and selects a single colu*n, whose type is a
vector o+ the sa*e sie as the *atri. )he le+t*ost colu*n is colu*n ". A second su&script would then
operate on the colu*n vector, as de+ined earlier +or vectors. 5ence, two su&scripts select a colu*n and
then a row.
34
-
8/12/2019 GLSL Specificacion 1.20.8
41/93
5 perators and !"pressions
mat/ m'
m>1? , ve&/02A)' (( sets t:e se&ond &olumn to all 02A
m>A?>A? , 12A' (( sets t:e upper left element to 12A
m>0?>4? , 02A' (( sets t:e /t: element of t:e t:ird &olumn to 02A
Behavior is unde+ined when accessing a co*ponent outside the &ounds o+ a *atri with a non#constant
epression. 't is an error to access a *atri with a constant epression that is outside the &ounds o+ the
*atri.
#.0 Structure and +rra$ Operations
)he +ields o+ a structure and the length*ethod o+ an array are selected using the period . .
'n total, only the +ollowing operators are allowed to operate on arrays and structures as whole entities:
+ield or *ethod selector .
e2uality 33 43
assign*ent 3
indeing arrays only L M
)he e2uality operators and assign*ent operator are only allowed i+ the two operands are sa*e sie and
type. tructure types *ust &e o+ the sa*e declared structure. Both array operands *ust &e eplicitly
sied. 4hen using the e2uality operators, two structures are e2ual i+ and only i+ all the +ields are
co*ponent#wise e2ual, and two arrays are e2ual i+ and only i+ all the ele*ents are ele*ent#wise e2ual.
Array ele*ents are accessed using the array su&script operator + . An ea*ple o+ accessing an array
ele*ent is
diffuseColor , lig:tIntensit5>4? NdotL'
Array indices start at ero. Array ele*ents are accessed using an epression whose type is an integer.
Behavior is unde+ined i+ a shader su&scripts an array with an inde less than " or greater than or e2ual to
the sie the array was declared with.
Arrays can also accessed with the *ethod operator . and the length *ethod to 2uery the sie o+ the
array:
lig:tIntensit52lengt:) (( return t:e si"e of t:e arra5
#. +ssign)ents
Assign*ents o+ values to varia&le na*es are done with the assign*ent operator 3 , liEe
lvalue , expression
35
-
8/12/2019 GLSL Specificacion 1.20.8
42/93
5 perators and !"pressions
)he assign*ent operator stores the value o+ e$pressioninto &va&ue. )he e$pressionand &va&ue*ust have
the sa*e type, or the epression *ust have a type in the ta&le in ection F.1.1"I'*plicit Conversions
that converts to the type o+ &va&ue, in which case an i*plicit conversion will &e done on the epression
&e+ore the assign*ent is done. Any other desired type#conversions *ust &e speci+ied eplicitly via aconstructor. (#values *ust &e writa&le. Varia&les that are &uilt#in types, entire structures or arrays,
structure +ields, l#values with the +ield selector . applied to select co*ponents or swiles without
repeated +ields, l#values within parentheses, and l#values dere+erenced with the array su&script operator +
are all l#values. ther &inary or unary epressions, +unction na*es, swiles with repeated +ields, and
constants cannot &e l#values. )he ternary operator 75 is also not allowed as an l#value.
7pressions on the le+t o+ an assign*ent are evaluated &e+ore epressions on the right o+ the assign*ent.
ther assign*ent operators are
)he arith*etic assign*ents add into %3, su&tract +ro* !3, *ultiply into '3, and divide into &3.
)he epression
lvalue op, expression
is e2uivalent to
lvalue , lvalue opexpression
and the l#value and epression *ust satis+y the se*antic re2uire*ents o+ &oth opand e2uals 3.
)he assign*ents *odulus into 3, le+t shi+t &y ))P, right shi+t &y **P, inclusive or into 3,
and eclusive or into /3 are reserved +or +uture use.
Reading a varia&le &e+ore writing or initialiing it is legal, however the value is unde+ined.
#. !&pressions
7pressions in the shading language are &uilt +ro* the +ollowing:
Constants o+ type @ool, int, 8loat, all vector types, and all *atri types.
Constructors o+ all types.
Varia&le na*es o+ all types.
An array na*e with the length *ethod applied.
u&scripted array na*es.
-
8/12/2019 GLSL Specificacion 1.20.8
43/93
5 perators and !"pressions
)he arith*etic &inary operators add %, su&tract !, *ultiply ', and divide & operate on integer and
+loating#point scalars, vectors, and *atrices. '+ one operand is +loating#point &ased and the other is
not, then the conversions +ro* ection F.1.1"I'*plicit Conversions are applied to the non#+loating#
point#&ased operand. All arith*etic &inary operators result in the sa*e +unda*ental type integer or+loating#point as the operands they operate on, a+ter operand type conversion. A+ter conversion, the
+ollowing cases are valid
)he two operands are scalars. 'n this case the operation is applied, resulting in a scalar.
ne operand is a scalar, and the other is a vector or *atri. 'n this case, the scalar operation is
applied independently to each co*ponent o+ the vector or *atri, resulting in the sa*e sie vector
or *atri.
)he two operands are vectors o+ the sa*e sie. 'n this case, the operation is done co*ponent#wise
resulting in the sa*e sie vector.
)he operator is add %, su&tract !, or divide &, and the operands are *atrices with the sa*e
nu*&er o+ rows and the sa*e nu*&er o+ colu*ns. 'n this case, the operation is done co*ponent#
wise resulting in the sa*e sie *atri.
)he operator is *ultiply ', where &oth operands are *atrices or one operand is a vector and the
other a *atri. A right vector operand is treated as a colu*n vector and a le+t vector operand as a
row vector. 'n all these cases, it is re2uired that the nu*&er o+ colu*ns o+ the le+t operand is e2ual
to the nu*&er o+ rows o+ the right operand. )hen, the *ultiply ' operation does a linear
alge&raic *ultiply, yielding an o&=ect that has the sa*e nu*&er o+ rows as the le+t operand and the
sa*e nu*&er o+ colu*ns as the right operand. ection >.1" IVector and 9atri perations
eplains in *ore detail how vectors and *atrices are operated on.
All other cases are illegal.
Dividing &y ero does not cause an eception &ut does result in an unspeci+ied value. se the &uilt#in
+unctions dot, cross, matrixDompOult, and outerProduct, to get, respectively, vector dot product,
vector cross product, *atri co*ponent#wise *ultiplication, and the *atri product o+ a colu*n
vector ti*es a row vector.
)he operator *odulus is reserved +or +uture use.
)he arith*etic unary operators negate #, post# and pre#incre*ent and decre*ent !!and %% operate
on integer or +loating#point values including vectors and *atrices. All unary operators worE
co*ponent#wise on their operands. )hese result with the sa*e type they operated on.
-
8/12/2019 GLSL Specificacion 1.20.8
44/93
-
8/12/2019 GLSL Specificacion 1.20.8
45/93
5 perators and !"pressions
will &e e2uivalent to
v2x , u2x f'
v25 , u25 f'
v2" , u2" f'
And
ve&4 v- u- 9'
9 , v u'
will &e e2uivalent to
92x , v2x u2x'
925 , v25 u25'
92" , v2" u2"'
and liEewise +or *ost operators and all integer and +loating point vector and *atri types. )he eceptions
are *atri *ultiplied &y vector, vector *ultiplied &y *atri, and *atri *ultiplied &y *atri. )hese donot operate co*ponent#wise, &ut rather per+or* the correct linear alge&raic *ultiply.
ve&4 v- u'
mat4 m'
u , v m'
is e2uivalent to
u2x , dotv- m>A?)' (( m>A? is t:e left &olumn of m
u25 , dotv- m>1?)' (( dota-$) is t:e inner dot) produ&t of a and $
u2" , dotv- m>0?)'
And
u , m v'
is e2uivalent to
u2x , m>A?2x v2x m>1?2x v25 m>0?2x v2"'
u25 , m>A?25 v2x m>1?25 v25 m>0?25 v2"'
u2" , m>A?2" v2x m>1?2" v25 m>0?2" v2"'
And
mat4 m- n- r'
r , m n'
is e2uivalent to
39
-
8/12/2019 GLSL Specificacion 1.20.8
46/93
5 perators and !"pressions
r>A?2x , m>A?2x n>A?2x m>1?2x n>A?25 m>0?2x n>A?2"'
r>1?2x , m>A?2x n>1?2x m>1?2x n>1?25 m>0?2x n>1?2"'
r>0?2x , m>A?2x n>0?2x m>1?2x n>0?25 m>0?2x n>0?2"'
r>A?25 , m>A?25 n>A?2x m>1?25 n>A?25 m>0?25 n>A?2"'
r>1?25 , m>A?25 n>1?2x m>1?25 n>1?25 m>0?25 n>1?2"'
r>0?25 , m>A?25 n>0?2x m>1?25 n>0?25 m>0?25 n>0?2"'
r>A?2" , m>A?2" n>A?2x m>1?2" n>A?25 m>0?2" n>A?2"'
r>1?2" , m>A?2" n>1?2x m>1?2" n>1?25 m>0?2" n>1?2"'
r>0?2" , m>A?2" n>0?2x m>1?2" n>0?25 m>0?2" n>0?2"'
and si*ilarly +or other sies o+ vectors and *atrices.
40
-
8/12/2019 GLSL Specificacion 1.20.8
47/93
State)ents and Structure
)he +unda*ental &uilding &locEs o+ the pen/( hading (anguage are:
state*ents and declarations
+unction de+initions
selection i8!else
iteration(8or, while, and do!while
=u*ps(discard, return, @reak, and continue
)he overall structure o+ a shader is as +ollows
trans&ation-unit:
g&o%a&-"ec&aration
trans&ation-unit g&o%a&-"ec&aration
g&o%a&-"ec&aration:
'unction-"e'inition
"ec&aration
)hat is, a shader is a se2uence o+ declarations and +unction &odies.
-
8/12/2019 GLSL Specificacion 1.20.8
48/93
6 #tate$ents and #tructure
simp&e-statement:
"ec&aration-statement
e$pression-statement
se&ection-statement
iteration-statement
ump-statement
i*ple declaration, epression, and =u*p state*ents end in a se*i#colon.
)his a&ove is slightly si*pli+ied, and the co*plete gra**ar speci+ied in ection @ Ihading (anguage
/ra**ar should &e used as the de+initive speci+ication.
Declarations and epressions have already &een discussed.
.1 (unction 6efinitions
As indicated &y the gra**ar a&ove, a valid shader is a se2uence o+ glo&al declarations and +unction
de+initions. A +unction is declared as the +ollowing ea*ple shows:
(( protot5pe
returnT5pe fun&tionName t5peA argA- t5pe1 arg1- 222- t5pen argn)'
and a +unction is de+ined liEe
(( definition
returnT5pe fun&tionName t5peA argA- t5pe1 arg1- 222- t5pen argn)
;
(( do some &omputation
return returnValue'
gl_8axTextureCoords?'
((
(( erived matrix state t:at provides inverse and transposed versions
(( of t:e matri&es a$ove2 @oorl5 &onditioned matri&es ma5 result
(( in unpredi&ta$le values in t:eir inverse forms2
((
uniform mat4 gl_Normal8atrix' (( transpose of t:e inverse of t:e
(( upper leftmost 4x4 of gl_8odelVie98atrix
uniform mat/ gl_8odelVie98atrixInverse'
uniform mat/ [email protected]&tion8atrixInverse'
uniform mat/ [email protected]&tion8atrixInverse'
uniform mat/ gl_Texture8atrixInverse>gl_8axTextureCoords?'
uniform mat/ gl_8odelVie98atrixTranspose'
uniform mat/ [email protected]&tion8atrixTranspose'
uniform mat/ [email protected]&tion8atrixTranspose'
uniform mat/ gl_Texture8atrixTranspose>gl_8axTextureCoords?'
uniform mat/ gl_8odelVie98atrixInverseTranspose'
uniform mat/ [email protected]&tion8atrixInverseTranspose'
uniform mat/ [email protected]&tion8atrixInverseTranspose'
uniform mat/ gl_Texture8atrixInverseTranspose>gl_8axTextureCoords?'
((
(( Normal s&aling p2 4K2
((
uniform float gl_NormalS&ale'
50
-
8/12/2019 GLSL Specificacion 1.20.8
57/93
7 Built%in Variables
((
(( ept: range in 9indo9 &oordinates- p2 44
((
stru&t gl_ept:Range@arameters ; float near' (( n
float far' (( f
float diff' (( f n
gl_8axClip@lanes?'
((
(( @oint Si"e- p2 GG- GD2
((
stru&t gl_@oint@arameters ;
float si"e'
float si"e8in'
float si"e8ax'
float fadeT:res:oldSi"e'
float distan&eConstant6ttenuation'
float distan&eLinear6ttenuation'
float distan&eMuadrati&6ttenuation'
-
8/12/2019 GLSL Specificacion 1.20.8
58/93
-
8/12/2019 GLSL Specificacion 1.20.8
59/93
7 Built%in Variables
((
(( Texture Environment and !eneration- p2 130- p2 /A/02
((
uniform ve&/ gl_TextureEnvColor>gl_8axTexturenits?'uniform ve&/ gl_E5e@laneS>gl_8axTextureCoords?'
uniform ve&/ gl_E5e@laneT>gl_8axTextureCoords?'
uniform ve&/ gl_E5e@laneR>gl_8axTextureCoords?'
uniform ve&/ gl_E5e@laneM>gl_8axTextureCoords?'
uniform ve&/ gl_O$.e&t@laneS>gl_8axTextureCoords?'
uniform ve&/ gl_O$.e&t@laneT>gl_8axTextureCoords?'
uniform ve&/ gl_O$.e&t@laneR>gl_8axTextureCoords?'
uniform ve&/ gl_O$.e&t@laneM>gl_8axTextureCoords?'
((
(( Fog p2 1G1
((
stru&t gl_Fog@arameters ;
ve&/ &olor'
float densit5'
float start'
float end'
float s&ale' (( erived% 12A ( end start)
?' (( at most 9ill $e gl_8axTextureCoords
var5ing float gl_FogFragCoord'
-
8/12/2019 GLSL Specificacion 1.20.8
60/93
7 Built%in Variables
As with all arrays, indices used to su&scriptg&(Te$=oor"*ust either &e an integral constant epressions,
or this array *ust &e re#declared &y the shader with a sie. )he sie can &e at *ost
g&(>a$Te$ture=oor"s. sing indees close to " *ay aid the i*ple*entation in preserving varying
resources.
)he +ollowing varying varia&les are availa&le to read +ro* in a +rag*ent shader. )heg&(=o&orand
g&(Secon"ar/=o&orna*es are the sa*e na*es as attri&utes passed to the verte shader language.
5owever, there is no na*e con+lict, &ecause attri&utes are visi&le only in verte shaders and the +ollowing
are only visi&le in a +rag*ent shader.
var5ing ve&/ gl_Color'
var5ing ve&/ gl_Se&ondar5Color'
var5ing ve&/ gl_TexCoord>?' (( at most 9ill $e gl_8axTextureCoords
var5ing float gl_FogFragCoord'
var5ing ve&0 gl_@ointCoord'
)he values ing&(=o&orandg&(Secon"ar/=o&orwill &e derived auto*atically &y the syste* +ro*
g&(,ront=o&or# g&(?ac=o&or# g&(,rontSecon"ar/=o&or# andg&(?acSecon"ar/=o&or&ased on which+ace is visi&le. '+ +ied +unctionality is used +or verte processing, theng&(,og,rag=oor"will either &e
the #coordinate o+ the +rag*ent in eye space, or the interpolation o+ the +og coordinate, as descri&ed in
section %.1" o+ the pen/( 1.F peci+ication. )heg&(Te$=oor"; values are the interpolated
g&(Te$=oor";values +ro* a verte shader or the teture coordinates o+ any +ied pipeline &ased verte
+unctionality.
'ndices to the +rag*ent shaderg&(Te$=oor"array are as descri&ed a&ove in the verte shader tet.
)he values ing&(oint=oor" are two#di*ensional coordinates indicating where within a point pri*itive
the current +rag*ent is located, when point sprites are ena&led. )hey range +ro* "." to 1." across the
point. '+ the current pri*itive is not a point, or i+ point sprites are not ena&led, then the values read +ro*
glH0ointCoord are unde+ined.
54
-
8/12/2019 GLSL Specificacion 1.20.8
61/93
*uilt9in (unctions
)he pen/( hading (anguage de+ines an assort*ent o+ &uilt#in convenience +unctions +or scalar and
vector operations. 9any o+ these &uilt#in +unctions can &e used in *ore than one type o+ shader, &ut so*e
are intended to provide a direct *apping to hardware and so are availa&le only +or a speci+ic type o+
shader.
)he &uilt#in +unctions &asically +all into three categories:
)hey epose so*e necessary hardware +unctionality in a convenient way such as accessing a teture
*ap. )here is no way in the language +or these +unctions to &e e*ulated &y a shader.
)hey represent a trivial operation cla*p, *i, etc. that is very si*ple +or the user to write, &ut they
are very co**on and *ay have direct hardware support. 't is a very hard pro&le* +or the co*piler to*ap epressions to co*ple asse*&ler instructions.
)hey represent an operation graphics hardware is liEely to accelerate at so*e point. )he trigono*etry
+unctions +all into this category.
9any o+ the +unctions are si*ilar to the sa*e na*ed ones in co**on C li&raries, &ut they support vector
input as well as the *ore traditional scalar input.
Applications should &e encouraged to use the &uilt#in +unctions rather than do the e2uivalent co*putations
in their own shader code since the &uilt#in +unctions are assu*ed to &e opti*al e.g., perhaps supported
directly in hardware.
ser code can replace &uilt#in +unctions with their own i+ they choose, &y si*ply re#declaring and de+ining
the sa*e na*e and argu*ent list. Because &uilt#in +unctions are in a *ore outer scope than user &uilt#in+unctions, doing this will hide all &uilt#in +unctions with the sa*e na*e as the re#declared +unction.
4hen the &uilt#in +unctions are speci+ied &elow, where the input argu*ents and corresponding output
can &e 8loat, vec2, vecB, or vecC,genT/pe is used as the argu*ent.
-
8/12/2019 GLSL Specificacion 1.20.8
62/93
8 Built%in &unctions
.1 +ngle and Trigono)etr$ (unctions
-
8/12/2019 GLSL Specificacion 1.20.8
63/93
-
8/12/2019 GLSL Specificacion 1.20.8
64/93
8 Built%in &unctions
S$nta& 6escription
gen)ype 8loorgen)ype$ Returns a value e2ual to the nearest integer that is lessthan or e2ual to$
gen)ypeceilgen)ype$ Returns a value e2ual to the nearest integer that isgreater than or e2ual to$
gen)ype8ractgen)ype$ Returns$] 8loor$
gen)ype modgen)ype$, +loat/ 9odulus. Returns$]/8loor$/
gen)ype modgen)ype$, gen)ype/ 9odulus. Returns$]/8loor$/
gen)ype mingen)ype$, gen)ype/gen)ype mingen)ype$, +loat/5
Returns/i+/W$, otherwise it returns$
gen)ypemaxgen)ype$, gen)ype/gen)ype maxgen)ype$, +loat/
Returns/i+$W/, otherwise it returns$.
gen)ypeclampgen)ype$, gen)ype minVa&, gen)ype ma$Va&gen)ype clampgen)ype$,
+loat minVa&,+loat ma$Va&
Returns minmax$, minVa&, ma$Va&
Results are unde+ined i+ minVa& C ma$Va&.
gen)ype mixgen)ype$, gen)ype/, gen)ype agen)ype mixgen)ype$, gen)ype/, +loat a
Returns the linear &lend o+$and/#i.e.$1a/a
gen)ype stepgen)ype e"ge, gen)ype$gen)ype step+loat e"ge, gen)ype$
Returns "." i+$W e"ge, otherwise it returns 1."
gen)ype smoothstepgen)ype e"ge0,gen)ype e"ge1,gen)ype$
gen)ype smoothstep+loat e"ge0,+loat e"ge1,gen)ype$
Returns "." i+$WP e"ge0and 1." i+$XP e"ge1andper+or*s s*ooth 5er*ite interpolation &etween " and 1when e"ge0W$W e"ge1. )his is use+ul in cases whereyou would want a threshold +unction with a s*oothtransition. )his is e2uivalent to:
gen)ype t t P cla*p ] edge" edge1 ] edge", ", 1 return t U t U % ] ! U t
Results are unde+ined i+ e"ge0 CB e"ge1.
58
-
8/12/2019 GLSL Specificacion 1.20.8
65/93
8 Built%in &unctions
. Geo)etric (unctions
)hese operate on vectors as vectors, not co*ponent#wise.
S$nta& 6escription
+loat lengthgen)ype$ Returns the length o+ vector$, i.e.,
$["]!$[1]!...
+loat distancegen)ypep0, gen)ypep1 Returns the distance &etweenp0andp1, i.e.length p0 p1
+loat dotgen)ype$, gen)ype/ Returns the dot product o+$and/, i.e.,$ ["]/ [" ]$ [1]/ [1 ]...
vec% crossvec%$, vec%/ Returns the cross product o+ and y, i.e.
[$ [1]/[!]/ [1]$ [!]$[!]/["]/ [!]$["]$["]/ [1]/ ["]$ [1]]
gen)ype normali"egen)ype$ Returns a vector in the sa*e direction as$&ut with alength o+ 1.
vecF 8trans8orm
-
8/12/2019 GLSL Specificacion 1.20.8
66/93
-
8/12/2019 GLSL Specificacion 1.20.8
67/93
8 Built%in &unctions
.# atri& (unctions
S$nta& 6escription
*at matrixDompOult*at$, *at/ 9ultiply *atri$&y *atri/ co*ponent#wise, i.e.,resultLiML=M is the scalar product o+$LiML=Mand /LiML=M.
6ote: to get linear alge&raic *atri *ultiplication, usethe *ultiply operator '.
*at! outerProductvec! c, vec! r*at% outerProductvec% c, vec% r*atF outerProductvecF c, vecF r
*at!% outerProductvec% c, vec! r*at%! outerProductvec! c, vec% r
*at!F outerProductvecF c, vec! r*atF! outerProductvec! c, vecF r
*at%F outerProductvecF c, vec% r*atF% outerProductvec% c, vecF r
)reats the +irst para*eter cas a colu*n vector *atriwith one colu*n and the second para*eter ras a rowvector *atri with one row and does a linear alge&raic*atri *ultiply cU r, yielding a *atri whose nu*&er o+rows is the nu*&er o+ co*ponents in cand whosenu*&er o+ colu*ns is the nu*&er o+ co*ponents in r.
*at! transpose*at! m*at% transpose*at% m*atF transpose*atF m
*at!% transpose*at%! m*at%! transpose*at!% m
*at!F transpose*atF! m*atF! transpose*at!F m
*at%F transpose*atF% m*atF% transpose*at%F m
Returns a *atri that is the transpose o+ m. )he input*atri mis not *odi+ied.
61
-
8/12/2019 GLSL Specificacion 1.20.8
68/93
8 Built%in &unctions
. %ector :elational (unctions
Relational and e2uality operators ), )3, *, *3, 33, 43 are de+ined or reserved to produce scalar
Boolean results.
-
8/12/2019 GLSL Specificacion 1.20.8
69/93
8 Built%in &unctions
.0 Te&ture Loo-up (unctions
)eture looEup +unctions are availa&le to &oth verte and +rag*ent shaders. 5owever, level o+ detail is
not co*puted &y +ied +unctionality +or verte shaders, so there are so*e di++erences in operation &etweenverte and +rag*ent teture looEups. )he +unctions in the ta&le &elow provide access to tetures through
sa*plers, as set up through the pen/( A0'. )eture properties such as sie, piel +or*at, nu*&er o+
di*ensions, +iltering *ethod, nu*&er o+ *ip#*ap levels, depth co*parison, and so on are also de+ined &y
pen/( A0' calls. uch properties are taEen into account as the teture is accessed via the &uilt#in
+unctions de+ined &elow.
'+ a non#shadow teture call is *ade to a sa*pler that represents a depth teture with depth co*parisons
turned on, then results are unde+ined. '+ a shadow teture call is *ade to a sa*pler that represents a depth
teture with depth co*parisons turned o++, then results are unde+ined. '+ a shadow teture call is *ade to
a sa*pler that does not represent a depth teture, then results are unde+ined.
'n all +unctions &elow, the %iaspara*eter is optional +or +rag*ent shaders. )he %iaspara*eter is not
accepted in a verte shader.
-
8/12/2019 GLSL Specificacion 1.20.8
70/93
8 Built%in &unctions
S$nta& 6escription
vecF texture2=sa*pler!Dsamp&er,vec! coor" L, +loat %iasM
vecF texture2=ProFsa*pler!Dsamp&er,vec% coor" L, +loat %iasM
vecF texture2=ProFsa*pler!Dsamp&er,vecF coor" L, +loat %iasM
vecF texture2=?odsa*pler!Dsamp&er,vec! coor", +loat &o"
vecF texture2=ProF?odsa*pler!Dsamp&er,vec% coor", +loat &o"
vecF texture2=ProF?odsa*pler!Dsamp&er,vecF coor", +loat &o"
se the teture coordinate coor"to do ateture looEup in the !D teture currently
&ound to samp&er.
-
8/12/2019 GLSL Specificacion 1.20.8
71/93
8 Built%in &unctions
S$nta& 6escription
vecF shadow1=sa*pler1Dhadowsamp&er# vec% coor"L, +loat%iasMvecF shadow2= sa*pler!Dhadowsamp&er# vec% coor" L, +loat %iasM vecF shadow1=ProFsa*pler1Dhadowsamp&er# vecF coor"L, +loat%iasM vecF shadow2=ProF sa*pler!Dhadowsamp&er# vecF coor" L, +loat %iasM vecF shadow1=?odsa*pler1Dhadowsamp&er# vec% coor", +loat&o"vecF shadow2=?odsa*pler!Dhadowsamp&er# vec% coor", +loat &o"vecF shadow1=ProF?odsa*pler1Dhadowsamp&er# vecF coor", +loat&o"vecF shadow2=ProF?odsa*pler!Dhadowsamp&er#
vecF coor"# '&oat &o"5
se teture coordinate coor"to do a depthco*parison looEup on the depth teture
&ound tosamp&er, as descri&ed in section%..1F o+ version 1.F o+ the pen/(speci+ication. )he %rd co*ponent o+ coor"coor".p is used as the R value. )he teture
&ound tosamp&er*ust &e a depth teture, orresults are unde+ined.
-
8/12/2019 GLSL Specificacion 1.20.8
72/93
8 Built%in &unctions
A /( i*ple*entation *ay use the a&ove or other *ethods to per+or* the calculation, su&=ect to the
+ollowing conditions:
1. )he *ethod *ay use piecewise linear approi*ations. uch linear approi*ations i*ply that higher
order derivatives, dJdxdJdx$ and a&ove, are unde+ined.
!. )he *ethod *ay assu*e that the +unction evaluated is continuous. )here+ore derivatives within the
&ody o+ a non#uni+or* conditional are unde+ined.
%. )he *ethod *ay di++er per +rag*ent, su&=ect to the constraint that the *ethod *ay vary &y window
coordinates, not screen coordinates. )he invariance re2uire*ent descri&ed in section %.1 o+ the
pen/( 1.F speci+ication is relaed +or derivative calculations, &ecause the *ethod *ay &e a +unction
o+ +rag*ent location.
ther properties that are desira&le, &ut not re2uired, are:
F. .
-
8/12/2019 GLSL Specificacion 1.20.8
73/93
8 Built%in &unctions
. ;oise (unctions
6oise +unctions are availa&le to &oth +rag*ent and verte shaders. )hey are stochastic +unctions that can
&e used to increase visual co*pleity. Values returned &y the +ollowing noise +unctions give theappearance o+ rando*ness, &ut are not truly rando*. )he noise +unctions &elow are de+ined to have the
+ollowing characteristics:
)he return values are always in the range L#1.",1."M, and cover at least the range L#".$, ".$M, with a
/aussian#liEe distri&ution.
)he return values have an overall average o+ "."
)hey are repeata&le, in that a particular input value will always produce the sa*e return value
)hey are statistically invariant under rotation i.e., no *atter how the do*ain is rotated, it has the sa*e
statistical character
)hey have a statistical invariance under translation i.e., no *atter how the do*ain is translated, it has
the sa*e statistical character )hey typically give di++erent results under translation.
)he spatial +re2uency is narrowly concentrated, centered so*ewhere &etween ".> to 1.".
)hey are C1continuous everywhere i.e., the +irst derivative is continuous
S$nta& 6escription
+loat noise1gen)ype$ Returns a 1D noise value &ased on the input value$.
vec! noise2gen)ype$ Returns a !D noise value &ased on the input value$.
vec% noiseBgen)ype$ Returns a %D noise value &ased on the input value$.
vecF noiseCgen)ype$ Returns a FD noise value &ased on the input value$.
67
-
8/12/2019 GLSL Specificacion 1.20.8
74/93
Shading Language Gra))ar
)he gra**ar is +ed +ro* the output o+ leical analysis. )he toEens returned +ro* leical analysis are
6TTRI+TE CONST +OOL FLO6T INT
+RE6 CONTINE O ELSE FOR IF ISC6R RETRN
+VEC0 +VEC4 +VEC/ IVEC0 IVEC4 IVEC/ VEC0 VEC4 VEC/
86T0 86T4 86T/ IN OT INOT NIFOR8 V6RPIN!
CENTROI
86T0Q0 86T0Q4 86T0Q/
86T4Q0 86T4Q4 86T4Q/
86T/Q0 86T/Q4 86T/Q/
S68@LER1 S68@LER0 S68@LER4 S68@LERC+E S68@LER1S6O S68@LER0S6OSTRCT VOI ILE
IENTIFIER TP@E_N68E FLO6TCONST6NT INTCONST6NT +OOLCONST6NT
FIEL_SELECTION
LEFT_O@ RI!T_O@
INC_O@ EC_O@ LE_O@ !E_O@ EM_O@ NE_O@
6N_O@ OR_O@ QOR_O@ 8L_6SSI!N IV_6SSI!N 6_6SSI!N
8O_6SSI!N LEFT_6SSI!N RI!T_6SSI!N 6N_6SSI!N QOR_6SSI!N OR_6SSI!N
S+_6SSI!N
LEFT_@6REN RI!T_@6REN LEFT_+R6CET RI!T_+R6CET LEFT_+R6CE RI!T_+R6CE OT
CO886 COLON EM6L SE8ICOLON +6N! 6S TILE @LS ST6R SL6S @ERCENT
LEFT_6N!LE RI!T_6N!LE VERTIC6L_+6R C6RET 68@ERS6N MESTION
INV6RI6NT
)he +ollowing descri&es the gra**ar +or the pen/( hading (anguage in ter*s o+ the a&ove toEens.
varia%&e(i"enti'ier: )D+*T),)+R
primar/(e$pression:
varia%&e(i"enti'ier
)*T=*STG*T
,LGT=*STG*T
?L=*STG*T
L+,T(GR+* e$pression R)!HT(GR+*
68
-
8/12/2019 GLSL Specificacion 1.20.8
75/93
9 #'adin( )an(ua(e *ra$$ar
post'i$(e$pression:
primar/(e$pression
post'i$(e$pression L+,T(?RG=I+T integer(e$pression R)!HT(?RG=I+T
'unction(ca&&
post'i$(e$pression DT ,)+LD(S+L+=T)*
post'i$(e$pression )*=(
post'i$(e$pression D+=(
integer(e$pression:
e$pression
'unction(ca&&:
'unction(ca&&(or(metho"
'unction(ca&&(or(metho":
'unction(ca&&(generic
post'i$(e$pression DT 'unction(ca&&(generic
'unction(ca&&(generic:
'unction(ca&&(hea"er(ith(parameters R)!HT(GR+*
'unction(ca&&(hea"er(no(parameters R)!HT(GR+*
'unction(ca&&(hea"er(no(parameters:
'unction(ca&&(hea"er V)D
'unction(ca&&(hea"er
'unction(ca&&(hea"er(ith(parameters:
'unction(ca&&(hea"er assignment(e$pression
'unction(ca&&(hea"er(ith(parameters =>>G assignment(e$pression
'unction(ca&&(hea"er:
'unction(i"enti'ier L+,T(GR+*
@@ !rammar *ote: =onstructors &oo &ie 'unctions# %ut &e$ica& ana&/sis recognie" most o' them as@@ e/or"s. The/ are no recognie" through Jt/pe(speci'ierK.
'unction(i"enti'ier:
t/pe(speci'ier
)D+*T),)+R
,)+LD(S+L+=T)*
69
-
8/12/2019 GLSL Specificacion 1.20.8
76/93
9 #'adin( )an(ua(e *ra$$ar
unar/(e$pression:
post'i$(e$pression
)*=( unar/(e$pression
D+=( unar/(e$pression
unar/(operator unar/(e$pression
@@ !rammar *ote: *o tra"itiona& st/&e t/pe casts.
unar/(operator:
LS
DGSH
?G*!
T)LD+ @@ reserve"
@@ !rammar *ote: *o MNM or MOM unar/ ops. ointers are not supporte".
mu&tip&icative(e$pression:
unar/(e$pression
mu&tip&icative(e$pression STGR unar/(e$pression
mu&tip&icative(e$pression SLGSH unar/(e$pression
mu&tip&icative(e$pression +R=+*T unar/(e$pression @@ reserve"
a""itive(e$pression:
mu&tip&icative(e$pressiona""itive(e$pression LS mu&tip&icative(e$pression
a""itive(e$pression DGSH mu&tip&icative(e$pression
shi't(e$pression:
a""itive(e$pression
shi't(e$pression L+,T( a""itive(e$pression @@ reserve"
shi't(e$pression R)!HT( a""itive(e$pression @@ reserve"
re&ationa&(e$pression:
shi't(e$pression
re&ationa&(e$pression L+,T(G*!L+ shi't(e$pression
re&ationa&(e$pression R)!HT(G*!L+ shi't(e$pression
re&ationa&(e$pression L+( shi't(e$pression
re&ationa&(e$pression !+( shi't(e$pression
70
-
8/12/2019 GLSL Specificacion 1.20.8
77/93
9 #'adin( )an(ua(e *ra$$ar
eua&it/(e$pression:
re&ationa&(e$pression
eua&it/(e$pression +P( re&ationa&(e$pression
eua&it/(e$pression *+( re&ationa&(e$pression
an"(e$pression:
eua&it/(e$pression
an"(e$pression G>+RSG*D eua&it/(e$pression @@ reserve"
e$c&usive(or(e$pression:
an"(e$pression
e$c&usive(or(e$pression =GR+T an"(e$pression @@ reserve"
inc&usive(or(e$pression: e$c&usive(or(e$pression
inc&usive(or(e$pression V+RT)=GL(?GR e$c&usive(or(e$pression @@ reserve"
&ogica&(an"(e$pression:
inc&usive(or(e$pression
&ogica&(an"(e$pression G*D( inc&usive(or(e$pression
&ogica&($or(e$pression:
&ogica&(an"(e$pression
&ogica&($or(e$pression QR( &ogica&(an"(e$pression
&ogica&(or(e$pression:
&ogica&($or(e$pression
&ogica&(or(e$pression R( &ogica&($or(e$pression
con"itiona&(e$pression:
&ogica&(or(e$pression
&ogica&(or(e$pression P+ST)* e$pression =L* assignment(e$pression
assignment(e$pression:
con"itiona&(e$pression
unar/(e$pression assignment(operator assignment(e$pression
assignment(operator:
+PGL
>L(GSS)!*
71
-
8/12/2019 GLSL Specificacion 1.20.8
78/93
9 #'adin( )an(ua(e *ra$$ar
D)V(GSS)!*
>D(GSS)!* @@ reserve"
GDD(GSS)!*
S?(GSS)!*
L+,T(GSS)!* @@ reserve"
R)!HT(GSS)!* @@ reserve"
G*D(GSS)!* @@ reserve"
QR(GSS)!* @@ reserve"
R(GSS)!* @@ reserve"
e$pression:
assignment(e$pression
e$pression =>>G assignment(e$pression
constant(e$pression:
con"itiona&(e$pression
"ec&aration:
'unction(protot/pe S+>)=L*
init("ec&arator(&ist S+>)=L*
'unction(protot/pe:
'unction("ec&arator R)!HT(GR+*
'unction("ec&arator:
'unction(hea"er
'unction(hea"er(ith(parameters
'unction(hea"er(ith(parameters:
'unction(hea"er parameter("ec&aration
'unction(hea"er(ith(parameters =>>G parameter("ec&aration
'unction(hea"er:
'u&&/(speci'ie"(t/pe )D+*T),)+R L+,T(GR+*
parameter("ec&arator:
t/pe(speci'ier )D+*T),)+R
t/pe(speci'ier )D+*T),)+R L+,T(?RG=I+T constant(e$pression R)!HT(?RG=I+T
parameter("ec&aration:
72
-
8/12/2019 GLSL Specificacion 1.20.8
79/93
9 #'adin( )an(ua(e *ra$$ar
t/pe(ua&i'ier parameter(ua&i'ier parameter("ec&arator
parameter(ua&i'ier parameter("ec&arator
t/pe(ua&i'ier parameter(ua&i'ier parameter(t/pe(speci'ier
parameter(ua&i'ier parameter(t/pe(speci'ier
parameter(ua&i'ier:
@N empt/ N@
)*
T
)*T
parameter(t/pe(speci'ier:
t/pe(speci'ier
init("ec&arator(&ist:
sing&e("ec&aration
init("ec&arator(&ist =>>G )D+*T),)+R
init("ec&arator(&ist =>>G )D+*T),)+R L+,T(?RG=I+T R)!HT(?RG=I+T
init("ec&arator(&ist =>>G )D+*T),)+R L+,T(?RG=I+T constant(e$pression R)!HT(?RG=I+T
init("ec&arator(&ist =>>G )D+*T),)+R L+,T(?RG=I+TR)!HT(?RG=I+T +PGL initia&ier
init("ec&arator(&ist =>>G )D+*T),)+R L+,T(?RG=I+T constant(e$pression R)!HT(?RG=I+T +PGL initia&ier
init("ec&arator(&ist =>>G )D+*T),)+R +PGL initia&ier
sing&e("ec&aration:
'u&&/(speci'ie"(t/pe
'u&&/(speci'ie"(t/pe )D+*T),)+R
'u&&/(speci'ie"(t/pe )D+*T),)+R L+,T(?RG=I+T R)!HT(?RG=I+T
'u&&/(speci'ie"(t/pe )D+*T),)+R L+,T(?RG=I+T constant(e$pression R)!HT(?RG=I+T
'u&&/(speci'ie"(t/pe )D+*T),)+R L+,T(?RG=I+T R)!HT(?RG=I+T +PGL initia&ier
'u&&/(speci'ie"(t/pe )D+*T),)+R L+,T(?RG=I+T constant(e$pressionR)!HT(?RG=I+T +PGL initia&ier
'u&&/(speci'ie"(t/pe )D+*T),)+R +PGL initia&ier)*VGR)G*T )D+*T),)+R @@ Verte$ on&/.
@@ !rammar *ote: *o MenumM# or Mt/pe"e'M.
'u&&/(speci'ie"(t/pe:
73
-
8/12/2019 GLSL Specificacion 1.20.8
80/93
-
8/12/2019 GLSL Specificacion 1.20.8
81/93
9 #'adin( )an(ua(e *ra$$ar
>GTGTFQ2
>GTFQGTFQF
SG>L+R1D
SG>L+R2D
SG>L+RL+R=?+
SG>L+R1DSHGD
SG>L+R2DSHGD
struct(speci'ier
T+(*G>+
struct(speci'ier:
STR=T )D+*T),)+R L+,T(?RG=+ struct("ec&aration(&ist R)!HT(?RG=+
STR=T L+,T(?RG=+ struct("ec&aration(&ist R)!HT(?RG=+
struct("ec&aration(&ist:
struct("ec&aration
struct("ec&aration(&ist struct("ec&aration
struct("ec&aration:
t/pe(speci'ier struct("ec&arator(&ist S+>)=L*
struct("ec&arator(&ist:
struct("ec&arator
struct("ec&arator(&ist =>>G struct("ec&arator
struct("ec&arator:
)D+*T),)+R
)D+*T),)+R L+,T(?RG=I+T constant(e$pression R)!HT(?RG=I+T
initia&ier:
assignment(e$pression
"ec&aration(statement:
"ec&aration
statement:
75
-
8/12/2019 GLSL Specificacion 1.20.8
82/93
-
8/12/2019 GLSL Specificacion 1.20.8
83/93
9 #'adin( )an(ua(e *ra$$ar
con"ition:
e$pression
'u&&/(speci'ie"(t/pe )D+*T),)+R +PGL initia&ier
iteration(statement:
H)L+ L+,T(GR+* con"ition R)!HT(GR+* statement(no(ne(scope
D statement H)L+ L+,T(GR+* e$pression R)!HT(GR+* S+>)=L*
,R L+,T(GR+* 'or(init(statement 'or(rest(statement R)!HT(GR+*statement(no(ne(scope
'or(init(statement:
e$pression(statement
"ec&aration(statement
con"itionopt:
con"ition
@N empt/ N@
'or(rest(statement:
con"itionopt S+>)=L*
con"itionopt S+>)=L* e$pression
ump(statement:
=*T)*+ S+>)=L*
?R+GI S+>)=L*
R+TR* S+>)=L*
R+TR* e$pression S+>)=L*
D)S=GRD S+>)=L* @@ ,ragment sha"er on&/.
@@ !rammar *ote: *o MgotoM. !otos are not supporte".
trans&ation(unit:
e$terna&("ec&aration
trans&ation(unit e$terna&("ec&aration
e$terna&("ec&aration:
'unction("e'inition
"ec&aration
77
-
8/12/2019 GLSL Specificacion 1.20.8
84/93
9 #'adin( )an(ua(e *ra$$ar
'unction("e'inition:
'unction(protot/pe compoun"(statement(no(ne(scope
78
-
8/12/2019 GLSL Specificacion 1.20.8
85/93
10 Issues
14 Issues
1. 's it an error to have a*&iguous +unction para*eter