8/12/2019 GLSL Specification 1.30.10.Full
1/115
The OpenGLShading Language
Language Version: 1.30
Document Revision: 10
22-Nov-2009
John Kessenich
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
8/12/2019 GLSL Specification 1.30.10.Full
2/115
Copyright (c) 2008 The Khronos Group Inc. All Rights Reserved.
This specifiction is protected !y copyright l"s nd contins #teril proprietry to the Khronos Group$Inc. It or ny co#ponents #y not !e reproduced$ repu!lished$ distri!uted$ trns#itted$ displyed$!rodcst or other"ise e%ploited in ny #nner "ithout the e%press prior "ritten per#ission of KhronosGroup. &ou #y use this specifiction for i#ple#enting the functionlity therein$ "ithout ltering orre#oving ny trde#r'$ copyright or other notice fro# the specifiction$ !ut the receipt or possession ofthis specifiction does not convey ny rights to reproduce$ disclose$ or distri!ute its contents$ or to#nufcture$ use$ or sell nything tht it #y descri!e$ in "hole or in prt.
Khronos Group grnts e%press per#ission to ny current ro#oter$ Contri!utor or Adopter #e#!er ofKhronos to copy nd redistri!ute *+,-II/- versions of this specifiction in ny fshion$ provided tht*, CARG/ is #de for the specifiction nd the ltest vil!le updte of the specifiction for nyversion of the AI is used "henever possi!le. 1uch distri!uted specifiction #y !e refor#tted A13,*G A1 the contents of the specifiction re not chnged in ny "y. The specifiction #y !eincorported into product tht is sold s long s such product includes significnt independent "or'developed !y the seller. A lin' to the current version of this specifiction on the Khronos Group "e!siteshould !e included "henever possi!le "ith specifiction distri!utions.
Khronos Group #'es no$ nd e%pressly discli#s ny$ representtions or "rrnties$ e%press ori#plied$ regrding this specifiction$ including$ "ithout li#ittion$ ny i#plied "rrnties of #erchnt!ilityor fitness for prticulr purpose or noninfringe#ent of ny intellectul property. Khronos Group #'es
no$ nd e%pressly discli#s ny$ "rrnties$ e%press or i#plied$ regrding the correctness$ ccurcy$co#pleteness$ ti#eliness$ nd reli!ility of the specifiction. nder no circu#stnces "ill the KhronosGroup$ or ny of its ro#oters$ Contri!utors or +e#!ers or their respective prtners$ officers$ directors$e#ployees$ gents or representtives !e li!le for ny d#ges$ "hether direct$ indirect$ specil orconse4uentil d#ges for lost revenues$ lost profits$ or other"ise$ rising fro# or in connection "iththese #terils.
Khronos$ ,penK,-/$ ,penK,G1$ ,pen5G$ ,pen+A6$ ,pen13 /1 nd ,pen7 re trde#r's ofthe Khronos Group Inc. C,33A-A is trde#r' of 1ony Co#puter /ntertin#ent Inc. used !yper#ission !y Khronos. ,penG3 nd ,pen+3 re registered trde#r's nd the ,penG3 /1 logo is trde#r' of 1ilicon Grphics Inc. used !y per#ission !y Khronos. All other product n#es$ trde#r's$ndor co#pny n#es re used solely for identifiction nd !elong to their respective o"ners.
2
8/12/2019 GLSL Specification 1.30.10.Full
3/115
Table of Contents
1 Introduction.................................................................................................................................1
1.1 Acknowledgments................................................................................................................11. !hanges ............................................................................................................................... 1
1..1 "ummar# o$ %unctionalit# di$$erences $rom version 1...............................................11.. !hange histor# o$ this revision.....................................................................................&
1.& 'verview.............................................................................................................................. (1.) *rror +andling......................................................................................................................(1. -#ograhical !onventions................................................................................................. (1.( Derecation.......................................................................................................................... (
'verview o$ 'en/0 "hading.....................................................................................................1 Verte2 3rocessor.................................................................................................................. . %ragment 3rocessor..............................................................................................................
& Basics.......................................................................................................................................... 4&.1 !haracter "et........................................................................................................................ 4&. "ource "trings...................................................................................................................... 4&.& 3rerocessor......................................................................................................................... 5&.) !omments.......................................................................................................................... 1&&. -okens................................................................................................................................ 1&&.( Ke#words............................................................................................................................1)&. Identi$iers........................................................................................................................... 1&.4 "tatic 6se........................................................................................................................... 1(
) Varia7les and -#es..................................................................................................................1).1 Basic -#es........................................................................................................................ 1
).1.1 Void............................................................................................................................ 15).1. Booleans..................................................................................................................... 15).1.& Integers....................................................................................................................... 8).1.) %loats.......................................................................................................................... 1).1. Vectors........................................................................................................................).1.( 9atrices...................................................................................................................... ).1. "amlers..................................................................................................................... ).1.4 "tructures....................................................................................................................&).1.5 Arra#s......................................................................................................................... )).1.18 Imlicit !onversions................................................................................................
). "coing...............................................................................................................................(
).& "torage uali$iers...............................................................................................................).&.1 De$ault "torage uali$ier............................................................................................4).&. !onst...........................................................................................................................4).&.& !onstant *2ressions................................................................................................. 4).&.) Inuts.......................................................................................................................... 5).&. 6ni$orm...................................................................................................................... &8
3
8/12/2019 GLSL Specification 1.30.10.Full
4/115
).&.( 'ututs....................................................................................................................... &8).&. Interolation................................................................................................................&1
).) 3arameter uali$iers...........................................................................................................&&
). 3recision and 3recision uali$iers..................................................................................... &&)..1 Range and 3recision................................................................................................... &&).. 3recision uali$iers.................................................................................................... &&)..& De$ault 3recision uali$iers....................................................................................... &))..) Availa7le 3recision uali$iers....................................................................................&
).( Variance and the Invariant uali$ier.................................................................................. &).(.1 -he Invariant uali$ier............................................................................................... &).(. Invariance o$ !onstant *2ressions........................................................................... &(
). 'rder o$ uali$ication........................................................................................................ &( 'erators and *2ressions........................................................................................................&
.1 'erators............................................................................................................................ &
. Arra# 'erations............................................................................................................... &4.& %unction !alls.................................................................................................................... &4.) !onstructors....................................................................................................................... &4
.).1 !onversion and "calar !onstructors.......................................................................... &4.). Vector and 9atri2 !onstructors................................................................................. &5.).& "tructure !onstructors................................................................................................)1.).) Arra# !onstructors..................................................................................................... )
. Vector !omonents............................................................................................................).( 9atri2 !omonents............................................................................................................)&. "tructure and Arra# 'erations..........................................................................................)).4 Assignments....................................................................................................................... ))
.5 *2ressions........................................................................................................................ ).18 Vector and 9atri2 'erations..........................................................................................)4
( "tatements and "tructure...........................................................................................................1(.1 %unction De$initions...........................................................................................................
(.1.1 %unction !alling !onventions....................................................................................&(. "election.............................................................................................................................(.& Iteration.............................................................................................................................. (.) Jums..................................................................................................................................(
Built;in Varia7les......................................................................................................................4.1 Verte2 "hader "ecial Varia7les........................................................................................4. %ragment "hader "ecial Varia7les................................................................................... 5
.& Verte2 "hader Built;In Inuts........................................................................................... (8.) Built;In !onstants.............................................................................................................. (1. Built;In 6ni$orm "tate....................................................................................................... (.( Built;In Verte2 'utut and %ragment Inut Varia7les.......................................................(
4 Built;in %unctions..................................................................................................................... (4.1 Angle and -rigonometr# %unctions....................................................................................(4
4
8/12/2019 GLSL Specification 1.30.10.Full
5/115
4. *2onential %unctions........................................................................................................(54.& !ommon %unctions............................................................................................................ 84.) /eometric %unctions.......................................................................................................... &
4. 9atri2 %unctions................................................................................................................ 4.( Vector Relational %unctions...............................................................................................(4. -e2ture 0ooku %unctions................................................................................................. 4.4 %ragment 3rocessing %unctions..........................................................................................44.5
8/12/2019 GLSL Specification 1.30.10.Full
6/115
8/12/2019 GLSL Specification 1.30.10.Full
7/115
1 Introduction
-his document seci$ies onl# version 1.&8 o$ the 'en/0 "hading 0anguage. It re=uires >>V*R"I'>
to su7stitute 1&8, and re=uires #versionto accet onl# 1&8. I$ #version is declared with 118 or 18, the
language acceted is a revious version o$ the shading language, which will 7e suorted deending on
the version and t#e o$ conte2t in the 'en/0 A3I. "ee the 'en/0 /rahics "#stem "eci$ication,
Version &.8, $or details on what language versions are suorted.
1.1 Acknowledgments
-his seci$ication is 7ased on the work o$ those who contri7uted to version 1.18 o$ the 'en/0 0anguage
"eci$ication, the 'en/0 *" .8 0anguage "eci$ication, version 1.18, and the $ollowing contri7utors to
this version:
Ro7 Barris
3ierre Boudier
3at Brown
8/12/2019 GLSL Specification 1.30.10.Full
8/115
1 Introduction
native signed and unsigned integers, integer vectors, and oerations
7itwise shi$ts and masking
te2ture indices
te2ture return values
integer uni$orms, verte2 inuts, verte2 oututs, $ragment inuts, and $ragment oututs
7uilt;in $unction suort: a7s, sign, min, ma2, clam, ...
'ther te2ture suort:
"i?e =ueries.
-e2ture arra#s.
'$$setting.
*2licit 0'D and derivative controls
switch@case@defaultstatements.
8/12/2019 GLSL Specification 1.30.10.Full
9/115
1 Introduction
gl>9a2Var#ing%loats use gl>9a2Var#ing!omonents instead
Built;in coloring: gl>%ront!olor, gl>%ront"econdar#!olor, gl>!olor, gl>"econdar#!olor,
gl>Back!olor and gl>Back"econdar#!olor.
-he $ollowing is a summar# o$ $eatures that have 7een removed in version 1.&:
8/12/2019 GLSL Specification 1.30.10.Full
10/115
1 Introduction
9ade more clear that 1.1 and 1. shaders work, deending on state o$ the A3I
9ade clear EE does macro e2ansion a$ter asting not 7e$ore
$trans$orm is derecated instead o$ removed
7uilt;in state is derecated instead o$ removed
high is alwa#s resent in the $ragment language, the de$ault is high
order o$ =uali$ication is either invariant;=uali$ier interolation;=uali$ier storage;=uali$ier
recision;=uali$ier or storage;=uali$ier arameter;=uali$ier recision;=uali$ier
uint and int can 7e mi2ed $or FF, GG 7ut not $or other oerators
com7ined descritions o$ FF and GG, and also o$ H, , and
switch statements can 7e emt#, must have a statement 7etween a la7el and the end o$ the switch,
allows $low control to $all through
udated the minimum ma2imums and added gl>9a2Var#ing!omonents and derecatedgl>9a2Var#ing%loats
added gl>!liDistanceL to the $ragment side
Removed Einclude suort
Removed row>maMor
Removed common 7locks
'en/0 *" s#nchroni?ation
a N 7 is an r;value and never an l;value
6dated the grammar with I have added these to the grammar
switch statement
case@de$ault la7els, which are mi2ed with other statements needs semantic check $or in
switch
uint, unsigned literals, unsigned vectors
1 new samler t#es
new storage =uali$iers in, out, centroid in, centroid out untangled $rom arameter in@out@inout
interolation =uali$iers noersective, $lat, smooth
recision =uali$iers
allowed 7itwise and shi$t oerators
!hanges $rom revision & o$ version 1.&8 o$ the 'en/0 "hading 0anguage
Added derecation section 1.(
Added user;de$ined $ragment shader oututs.
Remove most 7uilt;in state.
4
8/12/2019 GLSL Specification 1.30.10.Full
11/115
1 Introduction
Derecated 7uilt;in verte2 inuts attri7utes and some oututs var#ings.
Added gl>!liDistance.
Derecated mi2ing $i2ed verte2@$ragment stage with rogramma7le $ragment@verte2 stage.
Removed suort $or multile rograms tiling the ieline still original 1. model o$ one rogram
$or the whole ieline.
Removed inout as a wa# o$ declaring inter$ace varia7les, to avoid the ro7lem o$ things like
interolation =uali$iers not knowing i$ the# are modi$#ing the co# in or the co# out. Also removes
the ro7lem o$ imlicit ass through $or a varia7le declared inout 7ut never used.
-rue native integer suort
signed and unsigned integer semantics
7itwise oerators and shi$ts
7uilt;in $unctions oerating on integers, abs sign min max clamp
integer;7ased te2ture looku $unctions, te2el $etch
te2ture arra#s
roMective cu7e ma te2ture and shadow
e2licit gradient te2ture looku
o$$set;te2el te2ture looku
te2ture si?e $unctions
add noperspectiveinterolation =uali$ier
Added trunc, round,roundEven, modf
Removed ftransform
Added isinfand isnan.
Added h#er7olic $unctions sinh cosh tanh asinh acosh atanh.
"ome s#nchroni?ation with *" inoutarameter evaluation order, $oovoid, others
Derecatedgl_li!Vertex
Addedgl_VertexID
ItOs an error to use Eifetc. on an unde$ined name
!hanges $rom revision o$ version 1.&8 o$ the 'en/0 "hading 0anguage
0arge rework o$ section 4. -e2ture 0ooku %unctions. Droed dimensionalit#@shadow $rom thenames, organi?ed 7# t#e instead o$ dimensionalit#, added in 0od control.
6se gl>3osition $or cliing i$ gl>!liVerte2 is not staticall# written.
Remove language a7out the $i2ed ieline in the descrition o$ $trans$orm.
!hanges $rom revision 18 o$ version 1.8 o$ the 'en/0 "hading 0anguage
5
8/12/2019 GLSL Specification 1.30.10.Full
12/115
1 Introduction
in, out, and inoutare used at glo7al scoe as the re$erred wa# o$ declaring attri7utes, var#ings,
and $ragment shader oututs. -his eases the usage o$ centroid flat smooth invariant etc.7#
reducing the num7er o$ ke#words needed to declare a varia7le, removes the misnomer that flat
varia7les var#, rovides $or a de$ault interolation, and scales to additional $uture rogramma7leie stages.
!ommon 7locks are added and can 7e 7acked 7# 7u$$ers in the A3I.
Pgl>Q re$i2ed uni$orms and attri7utes and several o$ the var#ings no longer re$lect 7uilt;in state,
7ut are redeclared 7# the language as a convenience to the user.
-he a7ilit# to inde2 into an arra# o$ samlers with a varia7le inde2 is removed.
-oken asting ## is added to the rerocessor.
Add row$ma%or to suort row;maMor matrices to allow acking o$ a &;row );column matri2 into &
uni$orms or & attri7utes.
"uort #includevia named source strings.
Accet the recision =uali$iers $rom 'en/0 *" with no e2ectation that an#thing is done with
them.
switch statements are added $or integer scalars onl#
mix&'is e2anded to oerate on a Boolean &rdargument that does not interolate 7ut selects.
1.! Oerie"
-his document descri7es &'e (!en)L *'a%ing Language+ version 1.30.
Indeendent comilation units written in this language are calleds'a%ers. A!rogramis a comlete set o$
shaders that are comiled and linked together. -he aim o$ this document is to thoroughl# seci$# the
rogramming language. -he 'en/0 /rahics "#stem "eci$ication will seci$# the 'en/0 entr#oints used to maniulate and communicate with rograms and shaders.
1.# $rror %andling
!omilers, in general, accet rograms that are ill;$ormed, due to the imossi7ilit# o$ detecting all ill;
$ormed rograms. 3orta7ilit# is onl# ensured $or well;$ormed rograms, which this seci$ication
descri7es. !omilers are encouraged to detect ill;$ormed rograms and issue diagnostic messages, 7ut are
not re=uired to do so $or all cases. !omilers are re=uired to return messages regarding le2icall#,
grammaticall#, or semanticall# incorrect shaders.
1.& Typographical Conentions
Italic, 7old, and $ont choices have 7een used in this seci$ication rimaril# to imrove reada7ilit#. !ode$ragments use a $i2ed width $ont. Identi$iers em7edded in te2t are italici?ed. Ke#words em7edded in te2t
are 7old. 'erators are called 7# their name, $ollowed 7# their s#m7ol in 7old in arentheses. -he
clari$#ing grammar $ragments in the te2t use 7old $or literals and italics $or non;terminals. -he o$$icial
grammar in "ection 5 P"hading 0anguage /rammarQ uses all caitals $or terminals and lower case $or
non;terminals.
6
8/12/2019 GLSL Specification 1.30.10.Full
13/115
1 Introduction
1.' (eprecation
-his version o$ the 'en/0 "hading 0anguage derecates some $eatures. -hese are clearl# called out in
this seci$ication as PderecatedQ. -he# are still resent in this version o$ the language, 7ut are targeted$or otential removal in a $uture version o$ the shading language. -he 'en/0 A3I has a $orward
comati7ilit# mode that will disallow use o$ derecated $eatures. I$ comiling in a mode where use o$
derecated $eatures is disallowed, their use causes comile time errors. "ee the 'en/0 /rahics "#stem
"eci$ication $or details on what causes derecated language $eatures to 7e acceted or to return an error.
7
8/12/2019 GLSL Specification 1.30.10.Full
14/115
2 Oerie" of OpenGL Shading
-he 'en/0 "hading 0anguage is actuall# two closel# related languages. -hese languages are used to
create shaders $or the rogramma7le rocessors contained in the 'en/0 rocessing ieline.
6nless otherwise noted in this aer, a language $eature alies to all languages, and common usage will
re$er to these languages as a single language. -he seci$ic languages will 7e re$erred to 7# the name o$
the rocessor the# target: verte2 or $ragment.
9ost 'en/0 state is not tracked or made availa7le to shaders. -#icall#, user;de$ined varia7les will 7e
used $or communicating 7etween di$$erent stages o$ the 'en/0 ieline. +owever, a small amount o$
state is still tracked and automaticall# made availa7le to shaders, and there are a $ew 7uilt;in varia7les $or
inter$aces 7etween di$$erent stages o$ the 'en/0 ieline.
2.1 )erte* +rocessor
-he vertex !rocessoris a rogramma7le unit that oerates on incoming vertices and their associated data.
!omilation units written in the 'en/0 "hading 0anguage to run on this rocessor are called vertex
s'a%ers. hen a comlete set o$ verte2 shaders are comiled and linked, the# result in a vertex s'a%er
executa,lethat runs on the verte2 rocessor.
-he verte2 rocessor oerates on one verte2 at a time. It does not relace grahics oerations that re=uire
knowledge o$ several vertices at a time. -he verte2 shaders running on the verte2 rocessor must
comute the homogeneous osition o$ the incoming verte2.
2.2 Fragment +rocessor-heragment !rocessoris a rogramma7le unit that oerates on $ragment values and their associated
data. !omilation units written in the 'en/0 "hading 0anguage to run on this rocessor are called
ragment shaders. hen a comlete set o$ $ragment shaders are comiled and linked, the# result in a
ragment s'a%er executa,lethat runs on the $ragment rocessor.
A $ragment shader cannot change a $ragmentOs x, osition. Access to neigh7oring $ragments is not
allowed. -he values comuted 7# the $ragment shader are ultimatel# used to udate $rame;7u$$er memor#
or te2ture memor#, deending on the current 'en/0 state and the 'en/0 command that caused the
$ragments to 7e generated.
8
8/12/2019 GLSL Specification 1.30.10.Full
15/115
! ,asics
!.1 Character Set
-he source character set used $or the 'en/0 shading languages is a su7set o$ A"!II. It includes the
$ollowing characters:
-he letters a(), A(* and the underscore >.
-he num7ers+(,.
-he s#m7ols eriod ., lus -, dash (, slash , asterisk /, ercent 0, angled 7rackets and
2, s=uare 7rackets 3and 4 , arentheses &and ', 7races 5and 6, caret 7, vertical 7ar 8,
amersand 9, tilde :, e=uals ;, e2clamation oint , comma , and=uestion mark ?.
-he num7er sign # $or rerocessor use.
hite sace: the sace character, hori?ontal ta7, vertical ta7, $orm $eed, carriage;return, and line;
$eed.
0ines are relevant $or comiler diagnostic messages and the rerocessor. -he# are terminated 7#
carriage;return or line;$eed. I$ 7oth are used together, it will count as onl# a single line termination. %or
the remainder o$ this document, an# o$ these com7inations is siml# re$erred to as a new;line. -here is no
line continuation character.
In general, the languages use o$ this character set is case sensitive.
-here are no character or string data t#es, so no =uoting characters are included.
-here is no end;o$;$ile character.
!.2 Source Strings
-he source $or a single shader is an arra# o$ strings o$ characters $rom the character set. A single shader
is made $rom the concatenation o$ these strings. *ach string can contain multile lines, searated 7# new;
lines.
8/12/2019 GLSL Specification 1.30.10.Full
16/115
3 Basics
!.! +reprocessor
-here is a rerocessor that rocesses the source strings as art o$ the comilation rocess.
-he comlete list o$ rerocessor directives is as $ollows.
#
#define
#undef
#if
#ifdef
#ifndef
#else
#elif
#endif
#error
#pragma
#extension
#version
#line
-he $ollowing oerators are also availa7le
defined
##
*ach num7er sign # can 7e receded in its line onl# 7# saces or hori?ontal ta7s. It ma# also 7e
$ollowed 7# saces and hori?ontal ta7s, receding the directive. *ach directive is terminated 7# a new;
line. 3rerocessing does not change the num7er or relative location o$ new;lines in a source string.
-he num7er sign # on a line 7# itsel$ is ignored. An# directive not listed a7ove will cause a diagnostic
message and make the imlementation treat the shader as ill;$ormed.
#defineand #undef$unctionalit# are de$ined as is standard $or ! rerocessors $or macro de$initions
7oth with and without macro arameters.
-he $ollowing rede$ined macros are availa7le
__LINE__
__FILE__
__VERSION__
__LIN/__will su7stitute a decimal integer constant that is one more than the num7er o$ receding new;lines in the current source string.
__$IL/__will su7stitute a decimal integer constant that sa#s which source string num7er is currentl#
7eing rocessed.
10
8/12/2019 GLSL Specification 1.30.10.Full
17/115
3 Basics
__V/R*I(N__will su7stitute a decimal integer re$lecting the version num7er o$ the 'en/0 shading
language. -he version o$ the shading language descri7ed in this document will have__V/R*I(N__
su7stitute the decimal integer 1&8.
All macro names containing two consecutive underscores $$ are reserved $or $uture use as rede$ined
macro names. All macro names re$i2ed with P/0>Q P/0Q $ollowed 7# a single underscore are also
reserved.
#if #ifdef #ifndef #else #elif and#endifare de$ined to oerate as is standard $or ! rerocessors.
*2ressions $ollowing #ifand #elifare $urther restricted to e2ressions oerating on literal integer
constants, lus identi$iers consumed 7# the definedoerator. It is an error to use #if or #elif on
e2ressions containing unde$ined macro names, other than as arguments to the defined oerator.
!haracter constants are not suorted. -he oerators availa7le are as $ollows.
+recedence Operator class Operators -ssociatiity
1 highest arenthetical grouing
8/12/2019 GLSL Specification 1.30.10.Full
18/115
3 Basics
3rerocessor e2ressions will 7e evaluated according to the 7ehavior o$ the host rocessor, not the
rocessor targeted 7# the shader.
#error will cause the imlementation to ut a diagnostic message into the shader o7Mects in$ormation log
see the 'en/0 /rahics "#stem "eci$ication $or how to access a shader o7Mects in$ormation log.
-he message will 7e the tokens $ollowing the #errordirective, u to the $irst new;line. -he
imlementation must then consider the shader to 7e ill;$ormed.
#pragmaallows imlementation deendent comiler control. -okens $ollowing #pragmaare not su7Mect
to rerocessor macro e2ansion. I$ an imlementation does not recogni?e the tokens $ollowing
#pragma, then it will ignore that ragma. -he $ollowing ragmas are de$ined as art o$ the language.
#pragma ST!L
-he "BGCragma is used to reserve ragmas $or use 7# $uture revisions o$ this language.
8/12/2019 GLSL Specification 1.30.10.Full
19/115
3 Basics
B# de$ault, comilers o$ this language must issue comile time s#ntactic, grammatical, and semantic
errors $or shaders that do not con$orm to this seci$ication. An# e2tended 7ehavior must $irst 7e ena7led.
Directives to control the 7ehavior o$ the comiler with resect to e2tensions are declared with the
#extensiondirective
#extension extension_name:behavior
#extension all :behavior
where extension_nameis the name o$ an e2tension. *2tension names are not documented in this
seci$ication. -he token allmeans the 7ehavior alies to all e2tensions suorted 7# the comiler. -he
,e'avior can 7e one o$ the $ollowing
behavior $ffect
reuire Behave as seci$ied 7# the e2tension extension_name.
/ive an error on the #extension i$ the e2tension extension_name is notsuorted, or i$ allis seci$ied.
enable Behave as seci$ied 7# the e2tension extension_name.
arn on the #extensioni$ the e2tension extension_nameis not suorted.
/ive an error on the #extension i$ allis seci$ied.
"arn Behave as seci$ied 7# the e2tension extension_name, e2cet issue warningson an# detecta7le use o$ that e2tension, unless such use is suorted 7# otherena7led or re=uired e2tensions.
I$ all is seci$ied, then warn on all detecta7le uses o$ an# e2tension used.
arn on the #extensioni$ the e2tension extension_nameis not suorted.
disable Behave including issuing errors and warnings as i$ the e2tensionextension_nameis not art o$ the language de$inition.
I$ all is seci$ied, then 7ehavior must revert 7ack to that o$ the non;e2tendedcore version o$ the language 7eing comiled to.
arn on the #extension i$ the e2tension extension_nameis not suorted.
-he extensiondirective is a simle, low;level mechanism to set the 7ehavior $or each e2tension. It does
not de$ine olicies such as which com7inations are aroriate, those must 7e de$ined elsewhere. 'rder
o$ directives matters in setting the 7ehavior $or each e2tension: Directives that occur later override those
seen earlier. -he allvariant sets the 7ehavior $or all e2tensions, overriding all reviousl# issued
extension directives, 7ut onl# $or the ,e'aviorswarnand disable.
13
8/12/2019 GLSL Specification 1.30.10.Full
20/115
3 Basics
-he initial state o$ the comiler is as i$ the directive
#extension all % disa$le
was issued, telling the comiler that all error and warning reorting must 7e done according to thisseci$ication, ignoring an# e2tensions.
*ach e2tension can de$ine its allowed granularit# o$ scoe. I$ nothing is said, the granularit# is a shader
that is, a single comilation unit, and the e2tension directives must occur 7e$ore an# non;rerocessor
tokens. I$ necessar#, the linker can en$orce granularities larger than a single comilation unit, in which
case each involved shader will have to contain the necessar# e2tension directive.
9acro e2ansion is not done on lines containing #extensionand #versiondirectives.
#linemust have, a$ter macro su7stitution, one o$ the $ollowing $orms:
#line line
#line line source-string-number
where line andsource-string-num,er are constant integer e2ressions. A$ter rocessing this directiveincluding its new;line, the imlementation will 7ehave as i$ it is comiling at line num7er line1and
source string num7ersource-string-num,er. "u7se=uent source strings will 7e num7ered se=uentiall#,
until another #linedirective overrides that num7ering.
!.# Comments
!omments are delimited 7# @U and U@, or 7# @@ and a new;line. -he 7egin comment delimiters @U or @@ are
not recogni?ed as comment delimiters inside o$ a comment, hence comments cannot 7e nested. I$ a
comment resides entirel# within a single line, it is treated s#ntacticall# as a single sace. 5 6
14
8/12/2019 GLSL Specification 1.30.10.Full
21/115
3 Basics
!.' 0ey"ords
-he $ollowing are the ke#words in the language, and cannot 7e used $or an# other urose than that
de$ined 7# this document: attribute const uniform varying
centroid flat smooth noperspective
break continue do for while switch case default
if else
in out inout
float int void bool true false
invariant
discard return
matD mat matF
matDxD matDx matDxF
matxD matx matxF
matFxD matFx matFxF
vecD vec vecF ivecD ivec ivecF bvecD bvec bvecF
uint uvecD uvec uvecF
lowp mediump highp precision
sampler1B samplerDB samplerB sampler!ube
sampler1B"hadow samplerDB"hadow sampler!ube"hadow
sampler1BArray samplerDBArray
sampler1BArray"hadow samplerDBArray"hadow
isampler1B isamplerDB isamplerB isampler!ube
isampler1BArray isamplerDBArray
usampler1B usamplerDB usamplerB usampler!ube
usampler1BArray usamplerDBArray
struct
-he $ollowing are the ke#words reserved $or $uture use. 6sing them will result in an error:
common partition active
asm
15
8/12/2019 GLSL Specification 1.30.10.Full
22/115
3 Basics
class union enum typedef template this packed
goto
inline noinline volatile public static extern external interface long short double half fixed unsigned superp
input output
hvecD hvec hvecF dvecD dvec dvecF fvecD fvec fvecF
samplerDBect samplerBect samplerDBect"hadow
samplerHuffer
filter
image1B imageDB imageB image!ube
iimage1B iimageDB iimageB iimage!ube
uimage1B uimageDB uimageB uimage!ubeimage1BArray imageDBArray
iimage1BArray iimageDBArray uimage1BArray uimageDBArray
image1B"hadow imageDB"hadow
image1BArray"hadow imageDBArray"hadow
imageHuffer iimageHuffer uimageHuffer
si)eof cast
namespace using
row$ma%or
In addition, all identi$iers containing two consecutive underscores $$ are reserved as ossi7le $utureke#words.
!. Identifiers
Identi$iers are used $or varia7le names, $unction names, structure names, and $ield selectors $ield
selectors select comonents o$ vectors and matrices similar to structure $ields, as discussed in "ection .
PVector !omonentsQ and "ection .( P9atri2 !omonentsQ . Identi$iers have the $orm
i%entiier
non%igit
i%entiier non%igit
i%entiier %igit
non%igit: one o$
$ a b c d e f g h i % k l m n o p I r s t u v w x y )
A H ! B E J G K L M N C O P Q R " S T U V W *
%igit: one o$
16
8/12/2019 GLSL Specification 1.30.10.Full
23/115
3 Basics
+ 1 D F X Y Z [ ,
Identi$iers starting with Pgl>Q are reserved $or use 7# 'en/0, and ma# not 7e declared in a shader aseither a varia7le or a $unction. +owever, as noted in the seci$ication, there are some cases where
reviousl# declared varia7les can 7e redeclared to change or add some roert#, and redeclared Xgl>X
names are allowed to 7e redeclared in a shaderonl# $or these seci$ic uroses. 9ore generall#, it is an
error to redeclare a varia7le, including those starting Pgl>Q.
!. Static 3se
"ome language rules descri7ed 7elow deend on whether something isstaticall written or used.
A shader contains astatic useo$ orstatic assignmentto a varia7lexi$, a$ter rerocessing, the shader
contains a statement that would read or writex, whether or not run;time $low o$ control will cause that
statement to 7e e2ecuted.
17
8/12/2019 GLSL Specification 1.30.10.Full
24/115
# )ariables and Types
All varia7les and $unctions must 7e declared 7e$ore 7eing used. Varia7le and $unction names are
identi$iers.
-here are no de$ault t#es. All varia7le and $unction declarations must have a declared t#e, and
otionall# =uali$iers. A varia7le is declared 7# seci$#ing its t#e $ollowed 7# one or more names
searated 7# commas. In man# cases, a varia7le can 7e initiali?ed as art o$ its declaration 7# using the
assignment oerator ;. -he grammar near the end o$ this document rovides a $ull re$erence $or the
s#nta2 o$ declaring varia7les.
6ser;de$ined t#es ma# 7e de$ined using structto aggregate a list o$ e2isting t#es into a single name.
-he 'en/0 "hading 0anguage is t#e sa$e. -here are no imlicit conversions 7etween t#es, with thee2cetion that an integer value ma# aear where a $loating;oint t#e is e2ected, and 7e converted to a
$loating;oint value. *2actl# how and when this can occur is descri7ed in "ection ).1.18PImlicit
!onversionsQ and as re$erenced 7# other sections in this seci$ication.
#.1 ,asic Types
-he 'en/0 "hading 0anguage suorts the $ollowing 7asic data t#es, groued as $ollows.
-ransarent t#es
Type 4eaning
void $or $unctions that do not return a value
bool a conditional t#e, taking on values o$ true or $alse
int a signed integer
uint an unsigned integer
float a single $loating;oint scalar
vecD a two;comonent $loating;oint vector
vec a three;comonent $loating;oint vector
vecF a $our;comonent $loating;oint vector
bvecD a two;comonent Boolean vector
bvec a three;comonent Boolean vector
bvecF a $our;comonent Boolean vector
ivecD a two;comonent signed integer vector
ivec a three;comonent signed integer vector
ivecF a $our;comonent signed integer vector
18
8/12/2019 GLSL Specification 1.30.10.Full
25/115
4 Variables and Types
Type 4eaning
uvecD a two;comonent unsigned integer vector
uvec a three;comonent unsigned integer vectoruvecF a $our;comonent unsigned integer vector
matD a Y $loating;oint matri2
mat a &Y& $loating;oint matri2
matF a )Y) $loating;oint matri2
matDxD same as a matD
matDx a $loating;oint matri2 with columns and & rows
matDxF a $loating;oint matri2 with columns and ) rows
matxD a $loating;oint matri2 with & columns and rows
matx same as a matmatxF a $loating;oint matri2 with & columns and ) rows
matFxD a $loating;oint matri2 with ) columns and rows
matFx a $loating;oint matri2 with ) columns and & rows
matFxF same as a matF
%loating 3oint "amler -#es oa=ue
Type 4eaning
sampler1B a handle $or accessing a 1D te2ture
samplerDB a handle $or accessing a D te2ture
samplerB a handle $or accessing a &D te2ture
sampler!ube a handle $or accessing a cu7e maed te2ture
sampler1B"hadow a handle $or accessing a 1D deth te2ture with comarison
samplerDB"hadow a handle $or accessing a D deth te2ture with comarison
sampler1BArray a handle $or accessing a 1D arra# te2ture
samplerDBArray a handle $or accessing a D arra# te2ture
sampler1BArray"hadow a handle $or accessing a 1D arra# deth te2ture with comarison
samplerDBArray"hadow a handle $or accessing a D arra# deth te2ture with comarison
"igned Integer "amler -#es oa=ue
Type 4eaning
isampler1B a handle $or accessing an integer 1D te2ture
19
8/12/2019 GLSL Specification 1.30.10.Full
26/115
4 Variables and Types
Type 4eaning
isamplerDB a handle $or accessing an integer D te2ture
isamplerB a handle $or accessing an integer &D te2tureisampler!ube a handle $or accessing an integer cu7e maed te2ture
isampler1BArray a handle $or accessing an integer 1D arra# te2ture
isamplerDBArray a handle $or accessing an integer D arra# te2ture
6nsigned Integer "amler -#es oa=ue
Type 4eaning
usampler1B a handle $or accessing an unsigned integer 1D te2ture
usamplerDB a handle $or accessing an unsigned integer D te2ture
usamplerB a handle $or accessing an unsigned integer &D te2ture
usampler!ube a handle $or accessing an unsigned integer cu7e maed te2ture
usampler1BArray a handle $or accessing an unsigned integer 1D arra# te2ture
usamplerDBArray a handle $or accessing an unsigned integer D arra# te2ture
In addition, a shader can aggregate these using arra#s and structures to 7uild more comle2 t#es.
-here are no ointer t#es.
#.1.1 )oid
%unctions that do not return a value must 7e declared as void. -here is no de$ault $unction return t#e.
-he ke#word voidcannot 7e used in an# other declarations e2cet $or emt# $ormal or actual arameterlists.
#.1.2 ,ooleans
-o make conditional e2ecution o$ code easier to e2ress, the t#e boolis suorted. -here is no
e2ectation that hardware directl# suorts varia7les o$ this t#e. It is a genuine Boolean t#e, holding
onl# one o$ two values meaning either true or $alse. -wo ke#words trueand falsecan 7e used as literal
Boolean constants. Booleans are declared and otionall# initiali?ed as in the $ollow e2amle:
$ool su&&ess' (( de&lare su&&ess* to $e a +oolean
$ool done , false' (( de&lare and initiali"e done*
-he right side o$ the assignment oerator ; must 7e an e2ression whose t#e is bool.
*2ressions used $or conditional Mums if for ?= while do(while must evaluate to the t#e bool.
20
8/12/2019 GLSL Specification 1.30.10.Full
27/115
4 Variables and Types
#.1.! Integers
"igned and unsigned integer varia7les are $ull# suorted. In this document, the term integer is meant to
generall# include 7oth signed and unsigned integers. 6nsigned integers have e2actl# & 7its o$ recision.
"igned integers use & 7its, including a sign 7it, in twoOs comlement $orm. 'erations resulting in
over$low or under$low will not cause an# e2cetion, nor will the# saturate, rather the# will PwraQ to #ield
the low;order & 7its o$ the result.
Integers are declared and otionall# initiali?ed with integer e2ressions, as in the $ollowing e2amle:
int i- . , /0' (( default integer literal t1pe is int
uint 2 , 3u' (( u* esta$lis4es t4e t1pe as uint
0iteral integer constants can 7e e2ressed in decimal 7ase 18, octal 7ase 4, or he2adecimal 7ase 1(
as $ollows.
integer-constant :
%ecimal-constant integer-suixo!t
octal-constant integer-suixo!t
'exa%ecimal-constant integer-suixo!t
integer-suix: one o$
u S
%ecimal-constant :
nonero-%igit
%ecimal-constant %igit
octal-constant :
+
octal-constant octal-%igit
'exa%ecimal-constant :
82'exa%ecimal-%igit
8Z'exa%ecimal-%igit
'exa%ecimal-constant 'exa%ecimal-%igit
%igit :
+
nonero-%igit
nonero-%igit : one o$
1 D F X Y Z [ ,
octal-%igit= one o$+ 1 D F X Y Z
'exa%ecimal-%igit=one o$
+ 1 D F X Y Z [ ,
a b c d e f
21
8/12/2019 GLSL Specification 1.30.10.Full
28/115
4 Variables and Types
A H ! B E J
8/12/2019 GLSL Specification 1.30.10.Full
29/115
4 Variables and Types
A decimal oint . is not needed i$ the e2onent art is resent.
8/12/2019 GLSL Specification 1.30.10.Full
30/115
4 Variables and Types
#.1. Structures
6ser;de$ined t#es can 7e created 7# aggregating other alread# de$ined t#es into a structure using the
structke#word. %or e2amle,
stru&t lig4t ;
float intensit1'
ve&3 position'
< lig4tVar'
In this e2amle, lig't7ecomes the name o$ the new t#e, and lig'tVar7ecomes a varia7le o$ t#e lig't.
-o declare varia7les o$ the new t#e, use its name without the ke#word struct.
lig4t lig4tVar0'
9ore $ormall#, structures are declared as $ollows. +owever, the comlete correct grammar is as given in
"ection 5 P"hading 0anguage /rammarQ .
struct-%einition :4ualiier
o!t structname
o!t5mem,er-list 6%eclarators
o!t5
mem,er-list :
mem,er-%eclaration5
mem,er-%eclaration mem,er-list5
mem,er-%eclaration :
,asic-t!e %eclarators5
where name7ecomes the user;de$ined t#e, and can 7e used to declare varia7les to 7e o$ this new t#e.
-he nameshares the same name sace as other varia7les, t#es, and $unctions, with the same scoing
rules. -he otional 4ualiieronl# alies to an# %eclarators, and is not art o$ the t#e 7eing de$ined $or
name.
"tructures must have at least one mem7er declaration. 9em7er declarators ma# contain recision
=uali$iers, 7ut ma# not contain an# other =uali$iers.
8/12/2019 GLSL Specification 1.30.10.Full
31/115
4 Variables and Types
stru&t S ; float f'
8/12/2019 GLSL Specification 1.30.10.Full
32/115
4 Variables and Types
float>7? a'
It is an error to declare arra#s o$ arra#s:
float a>7?>3?' (( illegal
float>7? a>3?' (( illegal
Arra#s can have initiali?ers $ormed $rom arra# constructors:
float a>7? , float>7?36/- /60- 76A- 760- 565)'
float a>7? , float>?36/- /60- 76A- 760- 565)' (( same t4ing
6nsi?ed arra#s can 7e e2licitl# si?ed 7# an initiali?er at declaration time:
float a>7?'
666
float $>? , a' (( $ is expli&itl1 si"e 7
float $>7? , a' (( means t4e same t4ing
+owever, imlicitl# si?ed arra#s cannot 7e assigned to.
8/12/2019 GLSL Specification 1.30.10.Full
33/115
4 Variables and Types
#.1.16 Implicit Conersions
In some situations, an e2ression and its t#e will 7e imlicitl# converted to a di$$erent t#e. -he
$ollowing ta7le shows all allowed imlicit conversions:
Type of e*pression Can be implicitly conerted to
int
uint
float
ivecDuvecD
vecD
ivecuvec
vec
ivecFuvecF
vecF
-here are no imlicit arra# or structure conversions. %or e2amle, an arra# o$ intcannot 7e imlicitl#converted to an arra# o$ float. -here are no imlicit conversions 7etween signed and unsigned integers.
hen an imlicit conversion is done, it is not a re;interretation o$ the e2ressionOs 7it attern, 7ut a
conversion o$ its value to an e=uivalent value in the new t#e. %or e2amle, the integer value ;Xwill 7e
converted to the $loating;oint value ;X.+. Integer values having more 7its o$ recision than a $loating
oint mantissa will lose recision when converted to float.
-he conversions in the ta7le a7ove are done onl# as indicated 7# other sections o$ this seci$ication.
#.2 Scoping
-he scoe o$ a varia7le is determined 7# where it is declared. I$ it is declared outside all $unction
de$initions, it has glo7al scoe, which starts $rom where it is declared and ersists to the end o$ the shader
it is declared in. I$ it is declared in a whiletest or a for statement, then it is scoed to the end o$ the
$ollowing su7;statement. 'therwise, i$ it is declared as a statement within a comound statement, it is
scoed to the end o$ that comound statement. I$ it is declared as a arameter in a $unction de$inition, it is
scoed until the end o$ that $unction de$inition. A $unction 7od# has a scoe nested inside the $unctions
de$inition. -he ifstatements e2ression does not allow new varia7les to 7e declared, hence does not
$orm a new scoe.
27
8/12/2019 GLSL Specification 1.30.10.Full
34/115
4 Variables and Types
ithin a declaration, the scoe o$ a name starts immediatel# a$ter the initiali?er i$ resent or immediatel#
a$ter the name 7eing declared i$ not. "everal e2amles:
int x , 5'
;
int x , 0- 1 , x' (( 1 is initiali"ed to 0
3? , float>3?76A- D60- 565)'
&onst float d>3? , float>?76A- D60- 565)'
float g'
666
float a>7? , float>7?g- 5- g- 063- g)'
float $>3?'
$ , float>3?g- g G 56A- g G 06A)'
-here must 7e e2actl# the same num7er o$ arguments as the si?e o$ the arra# 7eing constructed. I$ no si?e
is resent in the constructor, then the arra# is e2licitl# si?ed to the num7er o$ arguments rovided. -hearguments are assigned in order, starting at element 8, to the elements o$ the constructed arra#. *ach
argument must 7e the same t#e as the element t#e o$ the arra#, or 7e a t#e that can 7e converted to the
element t#e o$ the arra# according to "ection ).1.18PImlicit !onversions.Q
&.& )ector Components
-he names o$ the comonents o$ a vector are denoted 7# a single letter. As a notational convenience,
several letters are associated with each comonent 7ased on common usage o$ osition, color or te2ture
coordinate vectors. -he individual comonents o$ a vector can 7e selected 7# $ollowing the varia7le
name with eriod . and then the comonent name.
-he comonent names suorted are:
;x+ + + < 6se$ul when accessing vectors that reresent oints or normals
;r+ g+ ,+ a< 6se$ul when accessing vectors that reresent colors
;s+ t+ !+ 4< 6se$ul when accessing vectors that reresent te2ture coordinates
-he comonent namesx+ r+ andsare, $or e2amle, s#non#ms $or the same $irst comonent in a vector.
8/12/2019 GLSL Specification 1.30.10.Full
51/115
5 Operators and !pressions
-he comonent selection s#nta2 allows multile comonents to 7e selected 7# aending their names
$rom the same name set a$ter the eriod ..
ve&/ v/'
v/6rg$a' (( is a ve&/ and t4e same as .ust using v/-
v/6rg$' (( is a ve&3-
v/6$' (( is a float-
v/6x1' (( is a ve&0-
v/6xg$a' (( is illegal t4e &omponent names do not &ome from
(( t4e same set6
-he order o$ the comonents can 7e di$$erent to swi??le them, or relicated:
ve&/ pos , ve&/56A- 06A- 36A- /6A)'
ve&/ s:i", pos6:"1x' (( s:i" , /6A- 36A- 06A- 56A)
ve&/ dup , pos6xx11' (( dup , 56A- 56A- 06A- 06A)
-his notation is more concise than the constructor s#nta2. -o $orm an r;value, it can 7e alied to an#
e2ression that results in a vector r;value.
-he comonent grou notation can occur on the le$t hand side o$ an e2ression.
ve&/ pos , ve&/56A- 06A- 36A- /6A)'
pos6x: , ve&076A- H6A)' (( pos , 76A- 06A- 36A- H6A)
pos6:x , ve&0D6A- 6A)' (( pos , 6A- 06A- 36A- D6A)
pos6xx , ve&036A- /6A)' (( illegal BxB used t:i&e
pos6x1 , ve&356A- 06A- 36A)' (( illegal mismat&4 $et:een ve&0 and ve&3
-o $orm an l;value, swi??ling must 7e alied to an l;value o$ vector t#e, contain no dulicate
comonents, and it results in an l;value o$ scalar or vector t#e, deending on num7er o$ comonents
seci$ied.
Arra# su7scriting s#nta2 can also 7e alied to vectors to rovide numeric inde2ing. "o in
ve&/ pos'
!os"2#re$ers to the third element o$ os and is e=uivalent to !os.. -his allows varia7le inde2ing into a
vector, as well as a generic wa# o$ accessing comonents. An# integer e2ression can 7e used as the
su7scrit. -he $irst comonent is at inde2 ?ero. Reading $rom or writing to a vector using a constant
integral e2ression with a value that is negative or greater than or e=ual to the si?e o$ the vector is illegal.
hen inde2ing with non;constant e2ressions, 7ehavior is unde$ined i$ the inde2 is negative, or greater
than or e=ual to the si?e o$ the vector.
&.' 4atri* Components
-he comonents o$ a matri2 can 7e accessed using arra# su7scriting s#nta2. Al#ing a single su7scrit
to a matri2 treats the matri2 as an arra# o$ column vectors, and selects a single column, whose t#e is a
vector o$ the same si?e as the matri2. -he le$tmost column is column 8. A second su7scrit would then
oerate on the resulting vector, as de$ined earlier $or vectors. +ence, two su7scrits select a column and
then a row.
45
8/12/2019 GLSL Specification 1.30.10.Full
52/115
5 Operators and !pressions
mat/ m'
m>5? , ve&/06A)' (( sets t4e se&ond &olumn to all 06A
m>A?>A? , 56A' (( sets t4e upper left element to 56A
m>0?>3? , 06A' (( sets t4e /t4 element of t4e t4ird &olumn to 06A
Behavior is unde$ined when accessing a comonent outside the 7ounds o$ a matri2 with a non;constant
e2ression. It is an error to access a matri2 with a constant e2ression that is outside the 7ounds o$ the
matri2.
&. Structure and -rray Operations
-he $ields o$ a structure and the lengthmethod o$ an arra# are selected using the eriod . .
In total, onl# the $ollowing oerators are allowed to oerate on arra#s and structures as whole entities:
$ield or method selector .
e=ualit# ;;
8/12/2019 GLSL Specification 1.30.10.Full
53/115
5 Operators and !pressions
-he lvalue-ex!ression evaluates to an l;value. -he assignment oerator stores the value o$ rvalue-
ex!ressionintothel;value and returns an r;value with the t#e and recision o$ lvalue-ex!ression. -he
lvalue-ex!ressionand rvalue-ex!ressionmust have the same t#e, or the e2ression must have a t#e in
the ta7le in "ection ).1.18PImlicit !onversionsQ that converts to the t#e o$ lvalue-ex!ression, in whichcase an imlicit conversion will 7e done on the rvalue-ex!ression7e$ore the assignment is done. An#
other desired t#e;conversions must 7e seci$ied e2licitl# via a constructor. 0;values must 7e writa7le.
Varia7les that are 7uilt;in t#es, entire structures or arra#s, structure $ields, l;values with the $ield selector
. alied to select comonents or swi??les without reeated $ields, l;values within arentheses, and l;
values dere$erenced with the arra# su7scrit oerator 3 4 are all l;values. 'ther 7inar# or unar#
e2ressions, $unction names, swi??les with reeated $ields, and constants cannot 7e l;values. -he ternar#
oerator ?= is also not allowed as an l;value.
*2ressions on the le$t o$ an assignment are evaluated 7e$ore e2ressions on the right o$ the assignment.
-he other assignment oerators are
add into -;
su7tract $rom (;
multil# into /;
divide into ;
modulus into 0;
le$t shi$t 7# ;
right shi$t 7# 22;
and into 9;
inclusive;or into 8;
e2clusive;or into 7;
where the general e2ression
lvalue op, expression
is e=uivalent to
lvalue , lvalue opexpression
where o! is as descri7ed 7elow, and the l;value and e2ression must satis$# the semantic re=uirements o$
7oth o!and e=uals ;.
Reading a varia7le 7e$ore writing or initiali?ing it is legal, however the value is unde$ined.
&.5 $*pressions
*2ressions in the shading language are 7uilt $rom the $ollowing:
!onstants o$ t#e bool int uint float all vector t#es, and all matri2 t#es.
47
8/12/2019 GLSL Specification 1.30.10.Full
54/115
5 Operators and !pressions
!onstructors o$ all t#es.
Varia7le names o$ all t#es.
An arra# name with the length method alied.
"u7scrited arra# names.
%unction calls that return values.
!omonent $ield selectors and arra# su7scrit results.
3arenthesi?ed e2ression. An# e2ression can 7e arenthesi?ed. 3arentheses can 7e used to grou
oerations. 'erations within arentheses are done 7e$ore oerations across arentheses.
-he arithmetic 7inar# oerators add -, su7tract (, multil# /, and divide oerate on integer and
$loating;oint scalars, vectors, and matrices. I$ one oerand is $loating;oint 7ased and the other is
not, then the conversions $rom "ection ).1.18PImlicit !onversionsQ are alied to the non;$loating;
oint;7ased oerand. I$ the oerands are integer t#es, the# must 7oth 7e signed or 7oth 7e unsigned.
All arithmetic 7inar# oerators result in the same $undamental t#e signed integer, unsigned integer,or $loating;oint as the oerands the# oerate on, a$ter oerand t#e conversion. A$ter conversion,
the $ollowing cases are valid
-he two oerands are scalars. In this case the oeration is alied, resulting in a scalar.
'ne oerand is a scalar, and the other is a vector or matri2. In this case, the scalar oeration is
alied indeendentl# to each comonent o$ the vector or matri2, resulting in the same si?e vector
or matri2.
-he two oerands are vectors o$ the same si?e. In this case, the oeration is done comonent;wise
resulting in the same si?e vector.
-he oerator is add -, su7tract (, or divide , and the oerands are matrices with the same
num7er o$ rows and the same num7er o$ columns. In this case, the oeration is done comonent;
wise resulting in the same si?e matri2.
-he oerator is multil# /, where 7oth oerands are matrices or one oerand is a vector and the
other a matri2. A right vector oerand is treated as a column vector and a le$t vector oerand as a
row vector. In all these cases, it is re=uired that the num7er o$ columns o$ the le$t oerand is e=ual
to the num7er o$ rows o$ the right oerand. -hen, the multil# / oeration does a linear
alge7raic multil#, #ielding an o7Mect that has the same num7er o$ rows as the le$t oerand and the
same num7er o$ columns as the right oerand. "ection .18 PVector and 9atri2 'erationsQ
e2lains in more detail how vectors and matrices are oerated on.
All other cases are illegal.
Dividing 7# ?ero does not cause an e2cetion 7ut does result in an unseci$ied value. 6se the 7uilt;in
$unctions dot cross matrix!ompOult and outerQroduct, to get, resectivel#, vector dot roduct,
vector cross roduct, matri2 comonent;wise multilication, and the matri2 roduct o$ a column
vector times a row vector.
48
8/12/2019 GLSL Specification 1.30.10.Full
55/115
5 Operators and !pressions
-he oerator modulus 0 oerates on signed or unsigned integers or integer vectors. -he oerand
t#es must 7oth 7e signed or 7oth 7e unsigned. -he oerands cannot 7e vectors o$ di$$ering si?e. I$
one oerand is a scalar and the other vector, then the scalar is alied comonent;wise to the vector,
resulting in the same t#e as the vector. I$ 7oth are vectors o$ the same si?e, the result is comutedcomonent;wise. -he resulting value is unde$ined $or an# comonent comuted with a second
oerand that is ?ero, while results $or other comonents with non;?ero second oerands remain
de$ined. I$ 7oth oerands are non;negative, then the remainder is non;negative. Results are unde$ined
i$ one or 7oth oerands are negative. -he oerator modulus 0 is not de$ined $or an# other data
t#es non;integer t#es.
-he arithmetic unar# oerators negate ;, ost; and re;increment and decrement ((and -- oerate
on integer or $loating;oint values including vectors and matrices. All unar# oerators work
comonent;wise on their oerands. -hese result with the same t#e the# oerated on. %or ost; and
re;increment and decrement, the e2ression must 7e one that could 7e assigned to an l;value. 3re;
increment and re;decrement add or su7tract 1 or 1.8 to the contents o$ the e2ression the# oerate on,
and the value o$ the re;increment or re;decrement e2ression is the resulting value o$ that
modi$ication. 3ost;increment and ost;decrement e2ressions add or su7tract 1 or 1.8 to the contentso$ the e2ression the# oerate on, 7ut the resulting e2ression has the e2ressions value 7e$ore the
ost;increment or ost;decrement was e2ecuted.
-he relational oerators greater than 2, less than , greater than or e=ual 2;, and less than or
e=ual ; oerate onl# on scalar integer and scalar $loating;oint e2ressions. -he result is scalar
Boolean. *ither the oerands t#es must match, or the conversions $rom "ection ).1.18PImlicit
!onversionsQ will 7e alied to the integer oerand, a$ter which the t#es must match. -o do
comonent;wise relational comarisons on vectors, use the 7uilt;in $unctions lesshan
lesshanEIual greaterhan andgreaterhanEIual.
-he e=ualit# oerators eIual &;;, and not e=ual
8/12/2019 GLSL Specification 1.30.10.Full
56/115
5 Operators and !pressions
-he ternar# selection oerator ?=. It oerates on three e2ressions ex!1?ex!2=ex!3. -his
oerator evaluates the $irst e2ression, which must result in a scalar Boolean. I$ the result is true, it
selects to evaluate the second e2ression, otherwise it selects to evaluate the third e2ression. 'nl#
one o$ the second and third e2ressions is evaluated. -he second and third e2ressions can 7e an#t#e, as long their t#es match, or there is a conversion in "ection ).1.18PImlicit !onversionsQ that
can 7e alied to one o$ the e2ressions to make their t#es match. -his resulting matching t#e is the
t#e o$ the entire e2ression.
-he oneOs comlement oerator :. -he oerand must 7e o$ t#e signed or unsigned integer or integer
vector, and the result is the oneOs comlement o$ its oerandC each 7it o$ each comonent is
comlemented, including an# sign 7its.
-he shi$t oerators and 22. %or 7oth oerators, the oerands must 7e signed or unsigned
integers or integer vectors. 'ne oerand can 7e signed while the other is unsigned. In all cases, the
resulting t#e will 7e the same t#e as the le$t oerand. I$ the $irst oerand is a scalar, the second
oerand has to 7e a scalar as well. I$ the $irst oerand is a vector, the second oerand must 7e a scalar
or a vector, and the result is comuted comonent;wise. -he result is unde$ined i$ the right oerand is
negative, or greater than or e=ual to the num7er o$ 7its in the le$t e2ressionOs 7ase t#e. -he value o$
*1 FF * is *1 interreted as a 7it attern le$t;shi$ted 7# * 7its. -he value o$ *1 GG * is *1 right;
shi$ted 7# * 7it ositions. I$ *1 is a signed integer, the right;shi$t will e2tend the sign 7it. I$ *1 is an
unsigned integer, the right;shi$t will ?ero;e2tend.
-he 7itwise oerators and 9, e2clusive;or 7, and inclusive;or 8. -he oerands must 7e o$ t#e
signed or unsigned integers or integer vectors. -he oerands cannot 7e vectors o$ di$$ering si?e. I$ one
oerand is a scalar and the other a vector, the scalar is alied comonent;wise to the vector, resulting
in the same t#e as the vector. -he $undamental t#es o$ the oerands signed or unsigned must
match, and will 7e the resulting $undamental t#e. %or and 9, the result is the 7itwise;and $unction
o$ the oerands. %or e2clusive;or 7, the result is the 7itwise e2clusive;or $unction o$ the oerands.
%or inclusive;or 8, the result is the 7itwise inclusive;or $unction o$ the oerands.
%or a comlete seci$ication o$ the s#nta2 o$ e2ressions, see "ection 5 P"hading 0anguage /rammar.Q
&.16 )ector and 4atri* Operations
ith a $ew e2cetions, oerations are comonent;wise. 6suall#, when an oerator oerates on a vector or
matri2, it is oerating indeendentl# on each comonent o$ the vector or matri2, in a comonent;wise
$ashion. %or e2amle,
ve&3 v- u'
float f'
v , u G f'
will 7e e=uivalent to
v6x , u6x G f'
v61 , u61 G f'
v6" , u6" G f'
And
50
8/12/2019 GLSL Specification 1.30.10.Full
57/115
5 Operators and !pressions
ve&3 v- u- :'
: , v G u'
will 7e e=uivalent to
:6x , v6x G u6x'
:61 , v61 G u61'
:6" , v6" G u6"'
and likewise $or most oerators and all integer and $loating oint vector and matri2 t#es. -he e2cetions
are matri2 multilied 7# vector, vector multilied 7# matri2, and matri2 multilied 7# matri2. -hese do
not oerate comonent;wise, 7ut rather er$orm the correct linear alge7raic multil#.
ve&3 v- u'
mat3 m'
u , v J m'
is e=uivalent to
u6x , dotv- m>A?)' (( m>A? is t4e left &olumn of m
u61 , dotv- m>5?)' (( dota-$) is t4e inner dot) produ&t of a and $
u6" , dotv- m>0?)'
And
u , m J v'
is e=uivalent to
u6x , m>A?6x J v6x G m>5?6x J v61 G m>0?6x J v6"'
u61 , m>A?61 J v6x G m>5?61 J v61 G m>0?61 J v6"'
u6" , m>A?6" J v6x G m>5?6" J v61 G m>0?6" J v6"'
51
8/12/2019 GLSL Specification 1.30.10.Full
58/115
5 Operators and !pressions
And
mat3 m- n- r'
r , m J n'
is e=uivalent to
r>A?6x , m>A?6x J n>A?6x G m>5?6x J n>A?61 G m>0?6x J n>A?6"'
r>5?6x , m>A?6x J n>5?6x G m>5?6x J n>5?61 G m>0?6x J n>5?6"'
r>0?6x , m>A?6x J n>0?6x G m>5?6x J n>0?61 G m>0?6x J n>0?6"'
r>A?61 , m>A?61 J n>A?6x G m>5?61 J n>A?61 G m>0?61 J n>A?6"'
r>5?61 , m>A?61 J n>5?6x G m>5?61 J n>5?61 G m>0?61 J n>5?6"'
r>0?61 , m>A?61 J n>0?6x G m>5?61 J n>0?61 G m>0?61 J n>0?6"'
r>A?6" , m>A?6" J n>A?6x G m>5?6" J n>A?61 G m>0?6" J n>A?6"'
r>5?6" , m>A?6" J n>5?6x G m>5?6" J n>5?61 G m>0?6" J n>5?6"'
r>0?6" , m>A?6" J n>0?6x G m>5?6" J n>0?61 G m>0?6" J n>0?6"'
and similarl# $or other si?es o$ vectors and matrices.
52
8/12/2019 GLSL Specification 1.30.10.Full
59/115
' Statements and Structure
-he $undamental 7uilding 7locks o$ the 'en/0 "hading 0anguage are:
statements and declarations
$unction de$initions
selection if(else and switch(case(default'
iteration&for while and do(while'
Mums&discard return break and continue
-he overall structure o$ a shader is as $ollows
translation-unit:
glo,al-%eclaration
translation-unit glo,al-%eclaration
glo,al-%eclaration:
unction-%einition
%eclaration
-hat is, a shader is a se=uence o$ declarations and $unction 7odies. %unction 7odies are de$ined as
unction-%einition:
unction-!rotot!e ; statement-list ?'
out ve&/ gl_FragColor' (( depre&ated
out ve&/ gl_Fragata>gl_9axra:+uffers?' (( depre&ated
out float gl_Fragept4'
*2cet as noted 7elow, the# 7ehave as other inut and outut varia7les.
-he outut o$ the $ragment shader e2ecuta7le is rocessed 7# the $i2ed $unction oerations at the 7ack end
o$ the 'en/0 ieline.
%ragment shaders outut values to the 'en/0 ieline using the 7uilt;in varia7lesgl_$ragolor+
gl_$ragData+andgl_$ragDe!t', unless the discardstatement is e2ecuted. Bothgl_$ragolor and
gl_$ragDataare derecatedC the re$erred usage is to e2licitl# declare these oututs in the $ragment
shader using the out storage =uali$ier.
-he $i2ed $unctionalit# comuted deth $or a $ragment ma# 7e o7tained 7# readinggl_$ragoor%.+
descri7ed 7elow.
Derecated: riting togl_$ragolorseci$ies the $ragment color that will 7e used 7# the su7se=uent
$i2ed $unctionalit# ieline. I$ su7se=uent $i2ed $unctionalit# consumes $ragment color and an e2ecution
o$ the $ragment shader e2ecuta7le does not write a value togl_$ragolorthen the $ragment color
consumed is unde$ined.
61
8/12/2019 GLSL Specification 1.30.10.Full
68/115
7 Built$in Variables
I$ the $rame 7u$$er is con$igured as a color inde2 7u$$er then 7ehavior is unde$ined when using a $ragment
shader.
riting togl_$ragDe!t'will esta7lish the deth value $or the $ragment 7eing rocessed. I$ deth
7u$$ering is ena7led, and no shader writesgl_$ragDe!t', then the $i2ed $unction value $or deth will 7e
used as the $ragments deth value. I$ a shader staticall# assigns a value togl_$ragDe!t', and there is an
e2ecution ath through the shader that does not setgl_$ragDe!t', then the value o$ the $ragments deth
ma# 7e unde$ined $or e2ecutions o$ the shader that take that ath. -hat is, i$ the set o$ linked $ragment
shaders staticall# contain a write togl_$ragDe!t', then it is resonsi7le $or alwa#s writing it.
Derecated: -he varia7legl_$ragData is an arra#. riting togl_$ragData"n#seci$ies the $ragment
data that will 7e used 7# the su7se=uent $i2ed $unctionalit# ieline $or data n. I$ su7se=uent $i2ed
$unctionalit# consumes $ragment data and an e2ecution o$ a $ragment shader e2ecuta7le does not write a
value to it, then the $ragment data consumed is unde$ined.
I$ a shader staticall# assigns a value togl_$ragolor, it ma# not assign a value to an# element o$
gl_$ragData. I$ a shader staticall# writes a value to an# element o$ gl_$ragData, it ma# not assign a
value togl_$ragolor. -hat is, a shader ma# assign values to eithergl_$ragolororgl_$ragData, 7utnot 7oth. 9ultile shaders linked together must also consistentl# write Must one o$ these varia7les.
"imilarl#, i$ user declared outut varia7les are in use staticall# assigned to, then the 7uilt;in varia7les
gl_$ragolor andgl_$ragDatama# not 7e assigned to. -hese incorrect usages all generate comile time
errors.
I$ a shader e2ecutes the discardke#word, the $ragment is discarded, and the values o$ an# user;de$ined
$ragment oututs,gl_$ragDe!t', gl_$ragolor, andgl_$ragData7ecome irrelevant.
-he varia7legl_$ragoor%is availa7le as an inut varia7le $rom within $ragment shaders and it holds the
window relative coordinates 2, #, ?, and 1@w values $or the $ragment. I$ multi;samling, this value can 7e
$or an# location within the i2el, or one o$ the $ragment samles. -he use o$ centroid in does not $urther
restrict this value to 7e inside the current rimitive. -his value is the result o$ the $i2ed $unctionalit# that
interolates rimitives a$ter verte2 rocessing to generate $ragments. -hecomonent is the deth value
that would 7e used $or the $ragments deth i$ no shader contained an# writes to gl_$ragDe!t'. -his is
use$ul $or invariance i$ a shader conditionall# comutesgl_$ragDe!t'7ut otherwise wants the $i2ed
$unctionalit# $ragment deth.
%ragment shaders have access to the inut 7uilt;in varia7legl_$ront$acing+whose value is truei$ the
$ragment 7elongs to a $ront;$acing rimitive. 'ne use o$ this is to emulate two;sided lighting 7# selecting
one o$ two colors calculated 7# a verte2 shader.
-he 7uilt;in inut varia7legl_li!Distance arra# contains linearl# interolated values $or the verte2
values written 7# the verte2 shader to thegl_li!Distance verte2 outut varia7le. -his arra# must 7e
si?ed in the $ragment shader either imlicitl# or e2licitl# to 7e the same si?e as it was si?ed in the verte2
shader. 'nl# elements in this arra# that have cliing ena7led will have de$ined values.
.! )erte* Shader ,uilt:In InputsDerecated: -he $ollowing redeclared inut names can 7e used $rom within a verte2 shader to access the
current values o$ 'en/0 state.
62
8/12/2019 GLSL Specification 1.30.10.Full
69/115
7 Built$in Variables
in ve&/ gl_Color' (( depre&ated
in ve&/ gl_Se&ondar1Color' (( depre&ated
in ve&3 gl_Normal' (( depre&atedin ve&/ gl_Vertex' (( depre&ated
in ve&/ gl_9ultiTexCoordA' (( depre&ated
in ve&/ gl_9ultiTexCoord5' (( depre&ated
in ve&/ gl_9ultiTexCoord0' (( depre&ated
in ve&/ gl_9ultiTexCoord3' (( depre&ated
in ve&/ gl_9ultiTexCoord/' (( depre&ated
in ve&/ gl_9ultiTexCoord7' (( depre&ated
in ve&/ gl_9ultiTexCoordH' (( depre&ated
in ve&/ gl_9ultiTexCoordD' (( depre&ated
in float gl_FogCoord' (( depre&ated
.# ,uilt:In Constants
-he $ollowing 7uilt;in constants are rovided to verte2 and $ragment shaders. -he actual values used are
imlementation deendent, 7ut must 7e at least the value shown. "ome are derecated, as indicated in
comments.
((
(( Implementation dependent &onstants6 T4e example values $elo:
(( are t4e minimum values allo:ed for t4ese maximums6
((
&onst int gl_9axTexturenits , 5H'
&onst int gl_9axVertex8ttri$s , 5H'
&onst int gl_9axVertexniformComponents , 5A0/'
&onst int gl_9axVar1ingFloats , H/' (( epre&ated
&onst int gl_9axVar1ingComponents , H/'&onst int gl_9axVertexTextureImagenits , 5H'
&onst int gl_9axCom$inedTextureImagenits , 5H'
&onst int gl_9axTextureImagenits , 5H'
&onst int gl_9axFragmentniformComponents , 5A0/'
&onst int gl_9axra:+uffers , '
&onst int gl_9axClipistan&es , '
((
(( T4e follo:ing are depre&ated6
((
&onst int gl_9axClip@lanes , ' (( depre&ated
&onst int gl_9axTextureCoords , ' (( depre&ated
-he constantgl_>axVaring$loats is derecated, usegl_>axVaringom!onents instead. -he constant
gl_>axli!=lanes is derecated along with user cli lanes, use cli distances andgl_>axli!Distances
instead. -he constantgl_>ax&extureoor%sis derecated, use user;de$ined interolants instead.
63
8/12/2019 GLSL Specification 1.30.10.Full
70/115
7 Built$in Variables
.& ,uilt:In 3niform State
As an aid to accessing 'en/0 rocessing state, the $ollowing uni$orm varia7les are 7uilt into the
'en/0 "hading 0anguage. All section num7ers and notations are re$erences to the 'en/0 /rahics"#stem "eci$ication, Version &.8.
((
(( ept4 range in :indo: &oordinates- se&tion 065065
((
stru&t gl_ept4Range@arameters ;
float near' (( n
float far' (( f
float diff' (( f n
gl_9axTextureCoords?'
uniform mat/ gl_9odelVie:9atrixTranspose'
uniform mat/ [email protected]&tion9atrixTranspose'
uniform mat/ gl_9odelVie:@ro.e&tion9atrixTranspose'
uniform mat/ gl_Texture9atrixTranspose>gl_9axTextureCoords?'
uniform mat/ gl_9odelVie:9atrixInverseTranspose'
uniform mat/ [email protected]&tion9atrixInverseTranspose'
uniform mat/ gl_9odelVie:@ro.e&tion9atrixInverseTranspose'uniform mat/ gl_Texture9atrixInverseTranspose>gl_9axTextureCoords?'
64
8/12/2019 GLSL Specification 1.30.10.Full
71/115
7 Built$in Variables
((
(( epre&ated6
((
uniform float gl_NormalS&ale'
((
(( epre&ated6
((
uniform ve&/ gl_Clip@lane>gl_9axClip@lanes?'
((
(( epre&ated6
((
stru&t gl_@oint@arameters ;
float si"e'
float si"e9in'
float si"e9ax'
float fadeT4res4oldSi"e'
float distan&eConstant8ttenuation'
float distan&eLinear8ttenuation'
float distan&eMuadrati&8ttenuation'
8/12/2019 GLSL Specification 1.30.10.Full
72/115
7 Built$in Variables
((
(( epre&ated6
((
stru&t gl_Lig4tSour&e@arameters ;
ve&/ am$ient' (( 8&li
ve&/ diffuse' (( &li
ve&/ spe&ular' (( S&li
ve&/ position' (( @pli
ve&/ 4alfVe&tor' (( erived% i
ve&3 spotire&tion' (( Sdli
float spotExponent' (( Srli
float spotCutoff' (( Crli
(( range% >A6A-A6A?- 5A6A)
float spotCosCutoff' (( erived% &osCrli)
(( range% >56A-A6A?-56A)
float &onstant8ttenuation' (( A
float linear8ttenuation' (( 5
float =uadrati&8ttenuation'(( 0
gl_9axLig4ts?'
66
8/12/2019 GLSL Specification 1.30.10.Full
73/115
7 Built$in Variables
((
(( epre&ated6
((uniform ve&/ gl_TextureEnvColor>gl_9axTexturenits?'
uniform ve&/ gl_E1e@laneS>gl_9axTextureCoords?'
uniform ve&/ gl_E1e@laneT>gl_9axTextureCoords?'
uniform ve&/ gl_E1e@laneR>gl_9axTextureCoords?'
uniform ve&/ gl_E1e@laneM>gl_9axTextureCoords?'
uniform ve&/ gl_O$.e&t@laneS>gl_9axTextureCoords?'
uniform ve&/ gl_O$.e&t@laneT>gl_9axTextureCoords?'
uniform ve&/ gl_O$.e&t@laneR>gl_9axTextureCoords?'
uniform ve&/ gl_O$.e&t@laneM>gl_9axTextureCoords?'
((
(( epre&ated6
((
stru&t gl_Fog@arameters ;
ve&/ &olor'
float densit1'
float start'
float end'
float s&ale' (( erived% 56A ( end start)
8/12/2019 GLSL Specification 1.30.10.Full
74/115
7 Built$in Variables
out ve&/ gl_FrontColor' (( depre&ated
out ve&/ gl_+a&2Color' (( depre&ated
out ve&/ gl_FrontSe&ondar1Color' (( depre&ated
out ve&/ gl_+a&2Se&ondar1Color' (( depre&ated
out ve&/ gl_TexCoord>?' (( depre&ated- at most :ill $e gl_9axTextureCoords
out float gl_FogFragCoord'(( depre&ated
%orgl_$og$ragoor% derecated, the value written will 7e used as the PcQ value in section &.11 o$ the
'en/0 /rahics "#stem "eci$ication, Version &.8, 7# the $i2ed $unctionalit# ieline. %or e2amle, i$
the ?;coordinate o$ the $ragment in e#e sace is desired as PcQ, then thatOs what the verte2 shader
e2ecuta7le should write intogl_$og$ragoor%.
As with all arra#s, indices used to su7scritgl_&exoor%derecated must either 7e an integral constant
e2ressions, or this arra# must 7e re;declared 7# the shader with a si?e. -he si?e can 7e at most
gl_>ax&extureoor%s. 6sing inde2es close to 8 ma# aid the imlementation in reserving var#ing
resources.
-he $ollowing $ragment inut varia7les are availa7le in a $ragment shader.
in ve&0 gl_@ointCoord'
-he $ollowing $ragment inuts are also availa7le in a $ragment shader, 7ut are derecated:
in float gl_FogFragCoord' (( depre&ated
in ve&/ gl_TexCoord>?' (( depre&ated
in ve&/ gl_Color' (( depre&ated
in ve&/ gl_Se&ondar1Color' (( depre&ated
Derecated: -he values ingl_olorandgl_*econ%arolorwill 7e derived automaticall# 7# the s#stem
$romgl_$rontolor+ gl_8acolor+ gl_$ront*econ%arolor+ andgl_8ac*econ%arolor7ased on
which $ace is visi7le. I$ $i2ed $unctionalit# is used $or verte2 rocessing, thengl_$og$ragoor%willeither 7e the ?;coordinate o$ the $ragment in e#e sace, or the interolation o$ the $og coordinate, as
descri7ed in section &.11 o$ the 'en/0 /rahics "#stem "eci$ication, Version &.8. -he
gl_&exoor%"# values are the interolatedgl_&exoor%"#values $rom a verte2 shader or the te2ture
coordinates o$ an# $i2ed ieline 7ased verte2 $unctionalit#.
Indices to the $ragment shadergl_&exoor%arra# are as descri7ed a7ove in the verte2 shader te2t.
-he values ingl_=ointoor% are two;dimensional coordinates indicating where within a oint rimitive
the current $ragment is located, when oint srites are ena7led. -he# range $rom 8.8 to 1.8 across the
oint. I$ the current rimitive is not a oint, or i$ oint srites are not ena7led, then the values read $rom
gl_=ointoor%are unde$ined.
68
8/12/2019 GLSL Specification 1.30.10.Full
75/115
,uilt:in Functions
-he 'en/0 "hading 0anguage de$ines an assortment o$ 7uilt;in convenience $unctions $or scalar and
vector oerations. 9an# o$ these 7uilt;in $unctions can 7e used in more than one t#e o$ shader, 7ut some
are intended to rovide a direct maing to hardware and so are availa7le onl# $or a seci$ic t#e o$
shader.
-he 7uilt;in $unctions 7asicall# $all into three categories:
-he# e2ose some necessar# hardware $unctionalit# in a convenient wa# such as accessing a te2ture
ma. -here is no wa# in the language $or these $unctions to 7e emulated 7# a shader.
-he# reresent a trivial oeration clam, mi2, etc. that is ver# simle $or the user to write, 7ut the#
are ver# common and ma# have direct hardware suort. It is a ver# hard ro7lem $or the comiler toma e2ressions to comle2 assem7ler instructions.
-he# reresent an oeration grahics hardware is likel# to accelerate at some oint. -he trigonometr#
$unctions $all into this categor#.
9an# o$ the $unctions are similar to the same named ones in common ! li7raries, 7ut the# suort vector
inut as well as the more traditional scalar inut.
Alications should 7e encouraged to use the 7uilt;in $unctions rather than do the e=uivalent comutations
in their own shader code since the 7uilt;in $unctions are assumed to 7e otimal e.g., erhas suorted
directl# in hardware.
6ser code can relace 7uilt;in $unctions with their own i$ the# choose, 7# siml# re;declaring and de$ining
the same name and argument list. Because 7uilt;in $unctions are in a more outer scoe than user 7uilt;in$unctions, doing this will hide all 7uilt;in $unctions with the same name as the re;declared $unction.
hen the 7uilt;in $unctions are seci$ied 7elow, where the inut arguments and corresonding outut
can 7e float, vecD, vec, or vecF,gen&!e is used as the argument. here the inut arguments and
corresonding outut can 7e int, ivecD, ivec,or ivecF,genI&!eis used as the argument. here the
inut arguments and corresonding outut can 7e uint, uvecD, uvec, or uvecF,gen?&!eis used as the
argument. %or an# seci$ic use o$ a $unction, the actual t#e su7stituted $orgen&!e,genI&!e, or
gen?&!ehas to 7e the same $or all arguments and $or the return t#e. "imilarl# $or mat+which can 7e
an# matri2 7asic t#e.
69
8/12/2019 GLSL Specification 1.30.10.Full
76/115
8 Built$in %unctions
.1 -ngle and Trigonometry Functions
%unction arameters seci$ied as angleare assumed to 7e in units o$ radians. In no case will an# o$ these
$unctions result in a divide 7# ?ero error. I$ the divisor o$ a ratio is 8, then results will 7e unde$ined.
-hese all oerate comonent;wise. -he descrition is er comonent.
Synta* (escription
gen-#e radiansgen-#e %egrees!onverts %egreesto radians, i.e.
148%egrees
gen-#e degreesgen-#e ra%ians!onverts ra%iansto degrees, i.e.
148
ra%ians
gen-#e singen-#e angle -he standard trigonometric sine $unction.
gen-#e cosgen-#e angle -he standard trigonometric cosine $unction.
gen-#e tangen-#e angle -he standard trigonometric tangent.
gen-#e asingen-#ex Arc sine. Returns an angle whose sine isx. -he range
o$ values returned 7# this $unction is [ +
]Results are unde$ined i$ x1.
gen-#e acosgen-#ex Arc cosine. Returns an angle whose cosine isx. -he
range o$ values returned 7# this $unction is 8 , L.Results are unde$ined i$ x1.
gen-#e atangen-#e, gen-#ex Arc tangent. Returns an angle whose tangent is@x. -hesigns o$x andare used to determine what =uadrant theangle is in. -he range o$ values returned 7# this
$unction is [+] . Results are unde$ined i$xandare 7oth 8.
gen-#e atangen-#e_over_x Arc tangent. Returns an angle whose tangent is_over_x. -he range o$ values returned 7# this $unction
is [ +
] .
70
8/12/2019 GLSL Specification 1.30.10.Full
77/115
8 Built$in %unctions
Synta* (escription
gen-#e sinh gen-#ex Returns the h#er7olic sine $unction
exe
x
gen-#e cosh gen-#ex Returns the h#er7olic cosine $unction
exe
x
gen-#e tanh gen-#ex Returns the h#er7olic tangent $unctionsinhx
cosh x
gen-#e asinh gen-#ex Arc h#er7olic sineC returns the inverse o$ sinh.
gen-#e acosh gen-#ex Arc h#er7olic cosineC returns the non;negative inverseo$ cosh. Results are unde$ined i$x F 1.
gen-#e atanh gen-#ex Arc h#er7olic tangentC returns the inverse o$ tanh.Results are unde$ined i$ x1.
.2 $*ponential Functions
-hese all oerate comonent;wise. -he descrition is er comonent.
Synta* (escription
gen-#e powgen-#ex, gen-#e Returnsxraised to theower, i.e., x
Results are unde$ined i$x A 0.
Results are unde$ined i$x 0and A 0.
gen-#e expgen-#ex Returns the natural e2onentiation o$x, i.e., ex.
gen-#e loggen-#ex Returns the natural logarithm o$x+ i.e., returns the valuewhich satis$ies the e=uationxN e.
Results are unde$ined i$x A 0.
gen-#e expDgen-#ex Returns raised to thexower, i.e., x
gen-#e l