Sie sind auf Seite 1von 115

The OpenGL

Shading Language
Language Version: 1.30
Document Revision: 10
22-Nov-2009
John Kessenich
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
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 Khronos
Group. &ou #y use this specifiction for i#ple#enting the functionlity therein$ "ithout ltering or
re#oving ny trde#r'$ copyright or other notice fro# the specifiction$ !ut the receipt or possession of
this 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 of
Khronos to copy nd redistri!ute )*+,-I.I/- versions of this specifiction in ny fshion$ provided tht
*, C0ARG/ is #de for the specifiction nd the ltest vil!le updte of the specifiction for ny
version of the A(I is used "henever possi!le. 1uch distri!uted specifiction #y !e re2for#tted A1
3,*G A1 the contents of the specifiction re not chnged in ny "y. The specifiction #y !e
incorported 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!2site
should !e included "henever possi!le "ith specifiction distri!utions.
Khronos Group #'es no$ nd e%pressly discli#s ny$ representtions or "rrnties$ e%press or
i#plied$ regrding this specifiction$ including$ "ithout li#ittion$ ny i#plied "rrnties of #erchnt!ility
or fitness for prticulr purpose or non2infringe#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 Khronos
Group$ 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 or
conse4uentil d#ges for lost revenues$ lost profits$ or other"ise$ rising fro# or in connection "ith
these #terils.
Khronos$ ,penK,-/$ ,penK,G1$ ,pen5G$ ,pen+A6$ ,pen13 /1 nd ,pen7. re trde#r's of
the Khronos Group Inc. C,33A-A is trde#r' of 1ony Co#puter /ntertin#ent Inc. used !y
per#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$
nd8or co#pny n#es re used solely for identifiction nd !elong to their respective o"ners.
2
Table of Contents
1 Introduction.................................................................................................................................1
1.1 Acknowledgments................................................................................................................ 1
1. !hanges ............................................................................................................................... 1
1..1 "ummar# o$ %unctionalit# di$$erences $rom version 1............................................... 1
1.. !hange histor# o$ this revision..................................................................................... &
1.& 'verview.............................................................................................................................. (
1.) *rror +andling......................................................................................................................(
1., -#.ogra.hical !onventions................................................................................................. (
1.( De.recation.......................................................................................................................... (
'verview o$ '.en/0 "hading....................................................................................................1
.1 Verte2 3rocessor.................................................................................................................. 1
. %ragment 3rocessor.............................................................................................................. 1
& Basics.......................................................................................................................................... 4
&.1 !haracter "et........................................................................................................................ 4
&. "ource "trings...................................................................................................................... 4
&.& 3re.rocessor......................................................................................................................... 5
&.) !omments.......................................................................................................................... 1&
&., -okens................................................................................................................................ 1&
&.( Ke#words............................................................................................................................1)
&.1 Identi$iers........................................................................................................................... 1,
&.4 "tatic 6se........................................................................................................................... 1(
) Varia7les and -#.es..................................................................................................................11
).1 Basic -#.es........................................................................................................................ 11
).1.1 Void............................................................................................................................ 15
).1. Booleans..................................................................................................................... 15
).1.& Integers....................................................................................................................... 8
).1.) %loats.......................................................................................................................... 1
).1., Vectors........................................................................................................................
).1.( 9atrices......................................................................................................................
).1.1 "am.lers.....................................................................................................................
).1.4 "tructures.................................................................................................................... &
).1.5 Arra#s......................................................................................................................... )
).1.18 Im.licit !onversions................................................................................................ ,
). "co.ing...............................................................................................................................(
).& "torage :uali$iers...............................................................................................................1
).&.1 De$ault "torage :uali$ier............................................................................................4
).&. !onst...........................................................................................................................4
).&.& !onstant *2.ressions................................................................................................. 4
).&.) In.uts.......................................................................................................................... 5
).&., 6ni$orm...................................................................................................................... &8
3
).&.( 'ut.uts....................................................................................................................... &8
).&.1 Inter.olation................................................................................................................&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 *2.ressions........................................................................... &(
).1 'rder o$ :uali$ication........................................................................................................ &(
, '.erators and *2.ressions........................................................................................................&1
,.1 '.erators............................................................................................................................ &1
,. 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 !om.onents............................................................................................................)
,.( 9atri2 !om.onents............................................................................................................)&
,.1 "tructure and Arra# '.erations..........................................................................................))
,.4 Assignments....................................................................................................................... ))
,.5 *2.ressions........................................................................................................................ ),
,.18 Vector and 9atri2 '.erations..........................................................................................)4
( "tatements and "tructure...........................................................................................................,1
(.1 %unction De$initions...........................................................................................................,
(.1.1 %unction !alling !onventions.................................................................................... ,&
(. "election............................................................................................................................. ,,
(.& Iteration.............................................................................................................................. ,,
(.) Jum.s..................................................................................................................................,(
1 Built;in Varia7les......................................................................................................................,4
1.1 Verte2 "hader ".ecial Varia7les........................................................................................,4
1. %ragment "hader ".ecial Varia7les................................................................................... ,5
1.& Verte2 "hader Built;In In.uts........................................................................................... (8
1.) Built;In !onstants.............................................................................................................. (1
1., Built;In 6ni$orm "tate....................................................................................................... (
1.( Built;In Verte2 'ut.ut and %ragment In.ut Varia7les.......................................................(,
4 Built;in %unctions..................................................................................................................... (1
4.1 Angle and -rigonometr# %unctions....................................................................................(4
4
4. *2.onential %unctions........................................................................................................(5
4.& !ommon %unctions............................................................................................................ 18
4.) /eometric %unctions.......................................................................................................... 1&
4., 9atri2 %unctions................................................................................................................ 1,
4.( Vector Relational %unctions...............................................................................................1(
4.1 -e2ture 0ooku. %unctions................................................................................................. 11
4.4 %ragment 3rocessing %unctions..........................................................................................41
4.5 <oise %unctions..................................................................................................................45
5 "hading 0anguage /rammar.....................................................................................................58
18 Issues.....................................................................................................................................18
5
1 Introduction
-his document s.eci$ies onl# version 1.&8 o$ the '.en/0 "hading 0anguage. It re=uires >>V*R"I'<>>
to su7stitute 1&8, and re=uires #version to acce.t onl# 1&8. I$ #version is declared with 118 or 18, the
language acce.ted is a .revious version o$ the shading language, which will 7e su..orted de.ending on
the version and t#.e o$ conte2t in the '.en/0 A3I. "ee the '.en/0 /ra.hics "#stem ".eci$ication,
Version &.8, $or details on what language versions are su..orted.
1.1 Acknowledgments
-his s.eci$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
<ick Burns
!hris Dodd
9ichael /old
<ick +aemel
James +el$ert#
Brent Insko
Je$$ Juliano
Jon 0eech
Bill 0icea;Kane
Barthold 0ichten7elt
Daniel Koch
9arc 'lano
Ian Romanick
John Rosasco
Dave "hreiner
Jerem# "andmel
Ro7ert "im.son
*skil "teen7erg
1.2 Changes
1.2.1 Summary of Functionality differences from ersion 1.2
-he $ollowing is a summar# o$ $eatures added in version 1.&:
Integer su..ort:
1
1 Introduction
native signed and unsigned integers, integer vectors, and o.erations
7itwise shi$ts and masking
te2ture indices
te2ture return values
integer uni$orms, verte2 in.uts, verte2 out.uts, $ragment in.uts, and $ragment out.uts
7uilt;in $unction su..ort: a7s, sign, min, ma2, clam., ...
'ther te2ture su..ort:
"i?e =ueries.
-e2ture arra#s.
'$$setting.
*2.licit 0'D and derivative controls
switch@case@default statements.
<ew 7uilt;ins: truncAB, roundAB, roundEvenAB, isnanAB, isinfAB, modfAB
h#.er7olic trigonometric $unctions
3re.rocessor token .asting A##B.
6ser;de$ined $ragment out.ut varia7les.
"hader in.ut and out.ut declarations via in and out.
Im.roved com.ati7ilit# with '.en/0 *"
non;.ers.ective AlinearB inter.olation AnosperspectiveB
new verte2 in.ut gl_VertexID.
<ew verte2 out.ut and $ragment shader in.ut gl_li!Distance"#.
-he $ollowing is a summar# o$ $eatures de.recated in version 1.&:
6se o$ the ke#words attribute and varying Ause in and outB.
6se o$ gl_li!Vertex Ause gl_li!DistanceB
6se o$ gl_$ragData and gl_$ragolor Ause user;de$ined out insteadB.
Built;in attri7utes. 6se user;de$ined verte2 in.uts instead.
%i2ed $unction verte2 or $ragment stages mi2ed with shader .rograms. 3rovide shaders $or all active
.rogramma7le .i.eline stages.
All 7uilt;in te2ture $unction names. "ee new names.
6se o$ the 7uilt;in var#ings gl_$og$ragoor% and gl_&exoor%. 6se user;de$ined varia7le instead.
-he 7uilt in $unction ftransform. 6se the invariant =uali$ier on a verte2 out.ut instead.
9ost 7uilt;in state.
2
1 Introduction
gl>9a2Var#ing%loats Ause gl>9a2Var#ing!om.onents insteadB
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.&:
<one, onl# de.recations occurred in this release.
1.2.2 Change history of this reision
!hanges $rom revision and 5 o$ version 1.&8 o$ the '.en/0 "hading 0anguage
!lari$# that discard control $low e2its the shader.
Added the inclusion o$ gl_li!Distance"# in the di$$erences $rom 1, section.
Remove accidental inclusion o$ textureGradffset on sampler!ube"hadow.
!lari$# it is generall# an error to redeclare varia7les, including 7uilt;ins.
!hanges $rom revision and 4 o$ version 1.&8 o$ the '.en/0 "hading 0anguage
%i2 1.& release omission: De.recate all 7uilt;in coloring to match A3I de.recation and $or
consistenc#.
%i2 1.& release omission: Allow .recision =uali$iers in structures and $unction declarations.
!hanges $rom revisions ( and 1 o$ version 1.&8 o$ the '.en/0 "hading 0anguage
%i2 all re$erences to the '.en/0 /ra.hics "#stem s.eci$ication, including matching notation $or
te2turing .arameters.
!hanges $rom revision , o$ version 1.&8 o$ the '.en/0 "hading 0anguage
Reserved superp.
9ade it an error to s.eci$# integer literals too 7ig $or an integer varia7le.
Increased
gl>9a2Var#ing!om.onents to ()
gl>9a2DrawBu$$ers to 4
gl>9a2-e2ture!oords to 4
%i2ed some t#.os.
!hanges $rom revision ) o$ version 1.&8 o$ the '.en/0 "hading 0anguage
6.dated acknowledgmentsC let me know i$ an#one is missing.
Added summar# lists o$ whatDs de.recated, removed, and added
De.recated $i2ed $unctionalit# control o$ a .rogramma7le stage
$lat is $or 7oth user and .redeclared 7uilt;in in@out varia7les
onl# staticall# used 7uilt;ins have to 7e redeclared as $lat
3
1 Introduction
9ade more clear that 1.1 and 1. shaders work, de.ending on state o$ the A3I
9ade clear EE does macro e2.ansion a$ter .asting not 7e$ore
$trans$ormAB is de.recated instead o$ removed
7uilt;in state is de.recated instead o$ removed
high. is alwa#s .resent in the $ragment language, the de$ault is high.
order o$ =uali$ication is either Ainvariant;=uali$ier inter.olation;=uali$ier storage;=uali$ier
.recision;=uali$ierB or Astorage;=uali$ier .arameter;=uali$ier .recision;=uali$ierB
uint and int can 7e mi2ed $or FF, GG 7ut not $or other o.erators
com7ined descri.tions o$ FF and GG, and also o$ H, I, and J
switch statements can 7e em.t#, must have a statement 7etween a la7el and the end o$ the switch,
allows $low control to $all through
u.dated the minimum ma2imums and added gl>9a2Var#ing!om.onents and de.recated
gl>9a2Var#ing%loats
added gl>!li.DistanceKL to the $ragment side
Removed Einclude su..ort
Removed row>maMor
Removed common 7locks
'.en/0 *" s#nchroni?ation
Aa N 7B is an r;value and never an l;value
6.dated the grammar with I have added these to the grammar
switch statement
case@de$ault la7els, which are mi2ed with other statements Aneeds semantic check $or in
switchB
uint, unsigned literals, unsigned vectors
11 new sam.ler t#.es
new storage =uali$iers in, out, centroid in, centroid out Auntangled $rom .arameter in@out@inoutB
inter.olation =uali$iers no.ers.ective, $lat, smooth
.recision =uali$iers
allowed 7itwise and shi$t o.erators
!hanges $rom revision & o$ version 1.&8 o$ the '.en/0 "hading 0anguage
Added de.recation section 1.(
Added user;de$ined $ragment shader out.uts.
Remove most 7uilt;in state.
4
1 Introduction
De.recated 7uilt;in verte2 in.uts Aattri7utesB and some out.uts Avar#ingsB.
Added gl>!li.Distance.
De.recated mi2ing $i2ed verte2@$ragment stage with .rogramma7le $ragment@verte2 stage.
Removed su..ort $or multi.le .rograms tiling the .i.eline Astill original 1. model o$ one .rogram
$or the whole .i.elineB.
Removed inout as a wa# o$ declaring inter$ace varia7les, to avoid the .ro7lem o$ things like
inter.olation =uali$iers not knowing i$ the# are modi$#ing the co.# in or the co.# out. Also removes
the .ro7lem o$ im.licit .ass through $or a varia7le declared inout 7ut never used.
-rue native integer su..ort
signed and unsigned integer semantics
7itwise o.erators and shi$ts
7uilt;in $unctions o.erating on integers, abs# sign# min# max# clamp#
integer;7ased te2ture looku. $unctions, te2el $etch
te2ture arra#s
.roMective cu7e ma. te2ture and shadow
e2.licit gradient te2ture looku.
o$$set;te2el te2ture looku.
te2ture si?e $unctions
add noperspective inter.olation =uali$ier
Added trunc, round, roundEven, modf
Removed ftransform
Added isinf and isnan.
Added h#.er7olic $unctions sinh# cosh# tanh# asinh# acosh# atanh.
"ome s#nchroni?ation with *" Ainout .arameter evaluation order, $ooAvoidB, othersB
De.recated gl_li!Vertex
Added gl_VertexID
ItOs an error to use Eif etc. on an unde$ined name
!hanges $rom revision o$ version 1.&8 o$ the '.en/0 "hading 0anguage
0arge rework o$ section 4.1 -e2ture 0ooku. %unctions. Dro..ed dimensionalit#@shadow $rom the
names, organi?ed 7# t#.e instead o$ dimensionalit#, added in 0od control.
6se gl>3osition $or cli..ing i$ gl>!li.Verte2 is not staticall# written.
Remove language a7out the $i2ed .i.eline in the descri.tion o$ $trans$ormAB.
!hanges $rom revision 18 o$ version 1.8 o$ the '.en/0 "hading 0anguage
5
1 Introduction
in, out, and inout are used at glo7al sco.e as the .re$erred wa# o$ declaring attri7utes, var#ings,
and $ragment shader out.uts. -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 inter.olation, and scales to additional $uture .rogramma7le
.i.e 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$ sam.lers with a varia7le inde2 is removed.
-oken .asting A##B is added to the .re.rocessor.
Add row$ma%or to su..ort row;maMor matrices to allow .acking o$ a &;row );column matri2 into &
uni$orms or & attri7utes.
"u..ort #include via named source strings.
Acce.t the .recision =uali$iers $rom '.en/0 *" with no e2.ectation that an#thing is done with
them.
switch statements are added $or integer scalars onl#
mix&' is e2.anded to o.erate on a Boolean &
rd
argument that does not inter.olate 7ut selects.
1.! Oerie"
-his document descri7es &'e (!en)L *'a%ing Language+ version 1.30.
Inde.endent com.ilation units written in this language are called s'a%ers. A !rogram is a com.lete set o$
shaders that are com.iled and linked together. -he aim o$ this document is to thoroughl# s.eci$# the
.rogramming language. -he '.en/0 /ra.hics "#stem ".eci$ication will s.eci$# the '.en/0 entr#
.oints used to mani.ulate and communicate with .rograms and shaders.
1.# $rror %andling
!om.ilers, in general, acce.t .rograms that are ill;$ormed, due to the im.ossi7ilit# o$ detecting all ill;
$ormed .rograms. 3orta7ilit# is onl# ensured $or well;$ormed .rograms, which this s.eci$ication
descri7es. !om.ilers are encouraged to detect ill;$ormed .rograms and issue diagnostic messages, 7ut are
not re=uired to do so $or all cases. !om.ilers 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 s.eci$ication .rimaril# to im.rove 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 ca.itals $or terminals and lower case $or
non;terminals.
6
1 Introduction
1.' (eprecation
-his version o$ the '.en/0 "hading 0anguage de.recates some $eatures. -hese are clearl# called out in
this s.eci$ication as Pde.recatedQ. -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
com.ati7ilit# mode that will disallow use o$ de.recated $eatures. I$ com.iling in a mode where use o$
de.recated $eatures is disallowed, their use causes com.ile time errors. "ee the '.en/0 /ra.hics "#stem
".eci$ication $or details on what causes de.recated language $eatures to 7e acce.ted or to return an error.
7
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 .i.eline.
6nless otherwise noted in this .a.er, a language $eature a..lies to all languages, and common usage will
re$er to these languages as a single language. -he s.eci$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 .i.eline. +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 .i.eline.
2.1 )erte* +rocessor
-he vertex !rocessor is a .rogramma7le unit that o.erates on incoming vertices and their associated data.
!om.ilation units written in the '.en/0 "hading 0anguage to run on this .rocessor are called vertex
s'a%ers. Rhen a com.lete set o$ verte2 shaders are com.iled and linked, the# result in a vertex s'a%er
executa,le that runs on the verte2 .rocessor.
-he verte2 .rocessor o.erates on one verte2 at a time. It does not re.lace gra.hics o.erations that re=uire
knowledge o$ several vertices at a time. -he verte2 shaders running on the verte2 .rocessor must
com.ute the homogeneous .osition o$ the incoming verte2.
2.2 Fragment +rocessor
-he -ragment !rocessor is a .rogramma7le unit that o.erates on $ragment values and their associated
data. !om.ilation units written in the '.en/0 "hading 0anguage to run on this .rocessor are called
-ragment shaders. Rhen a com.lete set o$ $ragment shaders are com.iled and linked, the# result in a
-ragment s'a%er executa,le that runs on the $ragment .rocessor.
A $ragment shader cannot change a $ragmentOs Ax, .B .osition. Access to neigh7oring $ragments is not
allowed. -he values com.uted 7# the $ragment shader are ultimatel# used to u.date $rame;7u$$er memor#
or te2ture memor#, de.ending on the current '.en/0 state and the '.en/0 command that caused the
$ragments to 7e generated.
8
! ,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 A > B.
-he num7ers +(,.
-he s#m7ols .eriod A.B, .lus A-B, dash A(B, slash A.B, asterisk A/B, .ercent A0B, angled 7rackets A1 and
2B, s=uare 7rackets A 3 and 4 B, .arentheses A & and ' B, 7races A 5 and 6 B, caret A7B, vertical 7ar A 8 B,
am.ersand A9B, tilde A:B, e=uals A;B, e2clamation .oint A<B, colon A=B, semicolon A>B, comma A#B, and
=uestion mark A?B.
-he num7er sign A#B $or .re.rocessor use.
Rhite s.ace: the s.ace character, hori?ontal ta7, vertical ta7, $orm $eed, carriage;return, and line;
$eed.
0ines are relevant $or com.iler diagnostic messages and the .re.rocessor. -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 sim.l# re$erred to as a new;line. -here is no
line continuation character.
In general, the languageDs 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 multi.le lines, se.arated 7# new;
lines. <o new;lines need 7e .resent in a stringC a single line can 7e $ormed $rom multi.le strings. <o
new;lines or other characters are inserted 7# the im.lementation when it concatenates the strings to $orm a
single shader. 9ulti.le shaders can 7e linked together to $orm a single .rogram.
Diagnostic messages returned $rom com.iling a shader must identi$# 7oth the line num7er within a string
and which source string the message a..lies to. "ource strings are counted se=uentiall# with the $irst
string 7eing string 8. 0ine num7ers are one more than the num7er o$ new;lines that have 7een .rocessed.
9
3 Basics
!.! +reprocessor
-here is a .re.rocessor that .rocesses the source strings as .art o$ the com.ilation .rocess.
-he com.lete list o$ .re.rocessor directives is as $ollows.
#
#define
#undef
#if
#ifdef
#ifndef
#else
#elif
#endif
#error
#pragma
#extension
#version
#line
-he $ollowing o.erators are also availa7le
defined
##
*ach num7er sign A#B can 7e .receded in its line onl# 7# s.aces or hori?ontal ta7s. It ma# also 7e
$ollowed 7# s.aces and hori?ontal ta7s, .receding the directive. *ach directive is terminated 7# a new;
line. 3re.rocessing does not change the num7er or relative location o$ new;lines in a source string.
-he num7er sign A#B on a line 7# itsel$ is ignored. An# directive not listed a7ove will cause a diagnostic
message and make the im.lementation treat the shader as ill;$ormed.
#define and #undef $unctionalit# are de$ined as is standard $or !II .re.rocessors $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
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 A $$ B are reserved $or $uture use as .rede$ined
macro names. All macro names .re$i2ed with P/0>Q AP/0Q $ollowed 7# a single underscoreB are also
reserved.
#if# #ifdef# #ifndef# #else# #elif# and #endif are de$ined to o.erate as is standard $or !II .re.rocessors.
*2.ressions $ollowing #if and #elif are $urther restricted to e2.ressions o.erating on literal integer
constants, .lus identi$iers consumed 7# the defined o.erator. It is an error to use #if or #elif on
e2.ressions containing unde$ined macro names, other than as arguments to the defined o.erator.
!haracter constants are not su..orted. -he o.erators availa7le are as $ollows.
+recedence Operator class Operators -ssociatiity
1 AhighestB .arenthetical grou.ing A B <A
unar# de$ined
I ; S T
Right to 0e$t
& multi.licative U @ V 0e$t to Right
) additive I ; 0e$t to Right
, 7it;wise shi$t FF GG 0e$t to Right
( relational F G FN GN 0e$t to Right
1 e=ualit# NN TN 0e$t to Right
4 7it;wise and H 0e$t to Right
5 7it;wise e2clusive or J 0e$t to Right
18 7it;wise inclusive or W 0e$t to Right
11 logical and HH 0e$t to Right
1 AlowestB logical inclusive or W W 0e$t to Right
-he defined o.erator can 7e used in either o$ the $ollowing wa#s:
defined identifier
defined ( identifier )
-wo tokens in a macro can 7e concatenated into one token using the token .asting A##B o.erator, as is
standard $or !II .re.rocessors. -he result must 7e a valid single token, which will then 7e su7Mect to
macro e2.ansion. -hat is, macro e2.ansion ha..ens a$ter token .asting and does not ha..en 7e$ore token
.asting. -here are no other num7er sign 7ased o.erators Ae.g. no # or #@B, nor is there a si)eof o.erator.
-he semantics o$ a..l#ing o.erators to integer literals in the .re.rocessor match those standard in the
!II .re.rocessor, not those in the '.en/0 "hading 0anguage.
11
3 Basics
3re.rocessor e2.ressions will 7e evaluated according to the 7ehavior o$ the host .rocessor, not the
.rocessor targeted 7# the shader.
#error will cause the im.lementation to .ut a diagnostic message into the shader o7MectDs in$ormation log
Asee the '.en/0 /ra.hics "#stem ".eci$ication $or how to access a shader o7MectDs in$ormation logB.
-he message will 7e the tokens $ollowing the #error directive, u. to the $irst new;line. -he
im.lementation must then consider the shader to 7e ill;$ormed.
#pragma allows im.lementation de.endent com.iler control. -okens $ollowing #pragma are not su7Mect
to .re.rocessor macro e2.ansion. I$ an im.lementation 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 "ABGC .ragma is used to reserve .ragmas $or use 7# $uture revisions o$ this language. <o
im.lementation ma# use a .ragma whose $irst token is "ABGC.
#pragma optimi"e#on)
#pragma optimi"e#off)
can 7e used to turn o$$ o.timi?ations as an aid in develo.ing and de7ugging shaders. It can onl# 7e used
outside $unction de$initions. B# de$ault, o.timi?ation is turned on $or all shaders. -he de7ug .ragma
#pragma de$ug#on)
#pragma de$ug#off)
can 7e used to ena7le com.iling and annotating a shader with de7ug in$ormation, so that it can 7e used
with a de7ugger. It can onl# 7e used outside $unction de$initions. B# de$ault, de7ug is turned o$$.
"haders should declare the version o$ the language the# are written to. -he language version a shader is
written to is s.eci$ied 7#
#version number
where num,er must 7e a version o$ the language, $ollowing the same convention as __V/R*I(N__ a7ove.
-he directive P#version 1&8Q is re=uired in an# shader that uses version 1.&8 o$ the language. An#
num,er re.resenting a version o$ the language a com.iler does not su..ort will cause an error to 7e
generated. Version 1.18 o$ the language does not re=uire shaders to include this directive, and shaders that
do not include a #version directive will 7e treated as targeting version 1.18. Di$$erent shaders
Acom.ilation unitsB that are linked together in the same .rogram must 7e the same version.
-he #version directive must occur in a shader 7e$ore an#thing else, e2ce.t $or comments and white s.ace.
12
3 Basics
B# de$ault, com.ilers o$ this language must issue com.ile time s#ntactic, grammatical, and semantic
errors $or shaders that do not con$orm to this s.eci$ication. An# e2tended 7ehavior must $irst 7e ena7led.
Directives to control the 7ehavior o$ the com.iler with res.ect to e2tensions are declared with the
#extension directive
#extension extension_name : behavior
#extension all : behavior
where extension_name is the name o$ an e2tension. *2tension names are not documented in this
s.eci$ication. -he token all means the 7ehavior a..lies to all e2tensions su..orted 7# the com.iler. -he
,e'avior can 7e one o$ the $ollowing
behavior $ffect
re.uire Behave as s.eci$ied 7# the e2tension extension_name.
/ive an error on the #extension i$ the e2tension extension_name is not
su..orted, or i$ all is s.eci$ied.
enable Behave as s.eci$ied 7# the e2tension extension_name.
Rarn on the #extension i$ the e2tension extension_name is not su..orted.
/ive an error on the #extension i$ all is s.eci$ied.
"arn Behave as s.eci$ied 7# the e2tension extension_name, e2ce.t issue warnings
on an# detecta7le use o$ that e2tension, unless such use is su..orted 7# other
ena7led or re=uired e2tensions.
I$ all is s.eci$ied, then warn on all detecta7le uses o$ an# e2tension used.
Rarn on the #extension i$ the e2tension extension_name is not su..orted.
disable Behave Aincluding issuing errors and warningsB as i$ the e2tension
extension_name is not .art o$ the language de$inition.
I$ all is s.eci$ied, then 7ehavior must revert 7ack to that o$ the non;e2tended
core version o$ the language 7eing com.iled to.
Rarn on the #extension i$ the e2tension extension_name is not su..orted.
-he extension directive is a sim.le, low;level mechanism to set the 7ehavior $or each e2tension. It does
not de$ine .olicies such as which com7inations are a..ro.riate, 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 all variant sets the 7ehavior $or all e2tensions, overriding all .reviousl# issued
extension directives, 7ut onl# $or the ,e'aviors warn and disable.
13
3 Basics
-he initial state o$ the com.iler is as i$ the directive
#extension all % disa$le
was issued, telling the com.iler that all error and warning re.orting must 7e done according to this
s.eci$ication, ignoring an# e2tensions.
*ach e2tension can de$ine its allowed granularit# o$ sco.e. I$ nothing is said, the granularit# is a shader
Athat is, a single com.ilation unitB, and the e2tension directives must occur 7e$ore an# non;.re.rocessor
tokens. I$ necessar#, the linker can en$orce granularities larger than a single com.ilation unit, in which
case each involved shader will have to contain the necessar# e2tension directive.
9acro e2.ansion is not done on lines containing #extension and #version directives.
#line must have, a$ter macro su7stitution, one o$ the $ollowing $orms:
#line line
#line line source-string-number
where line and source-string-num,er are constant integer e2.ressions. A$ter .rocessing this directive
Aincluding its new;lineB, the im.lementation will 7ehave as i$ it is com.iling at line num7er line01 and
source string num7er source-string-num,er. "u7se=uent source strings will 7e num7ered se=uentiall#,
until another #line directive overrides that num7ering.
!.# Comments
!omments are delimited 7# @U and U@, or 7# @@ and a new;line. -he 7egin comment delimiters A@U or @@B 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 s.ace. <ew;lines are
not eliminated 7# comments.
!.& To/ens
-he language is a se=uence o$ tokens. A token can 7e
to1en:
1e.2or%
i%enti-ier
integer-constant
-loating-constant
o!erator
> 5 6
14
3 Basics
!.' 0ey"ords
-he $ollowing are the ke#words in the language, and cannot 7e used $or an# other .ur.ose 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 matE matF
matDxD matDxE matDxF
matExD matExE matExF
matFxD matFxE matFxF
vecD vecE vecF ivecD ivecE ivecF bvecD bvecE bvecF
uint uvecD uvecE uvecF
lowp mediump highp precision
sampler1B samplerDB samplerEB sampler!ube
sampler1B"hadow samplerDB"hadow sampler!ube"hadow
sampler1BArray samplerDBArray
sampler1BArray"hadow samplerDBArray"hadow
isampler1B isamplerDB isamplerEB isampler!ube
isampler1BArray isamplerDBArray
usampler1B usamplerDB usamplerEB 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
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 hvecE hvecF dvecD dvecE dvecF fvecD fvecE fvecF
samplerDBGect samplerEBGect samplerDBGect"hadow
samplerHuffer
filter
image1B imageDB imageEB image!ube
iimage1B iimageDB iimageEB iimage!ube
uimage1B uimageDB uimageEB uimage!ube
image1BArray 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 A$$B are reserved as .ossi7le $uture
ke#words.
!.1 Identifiers
Identi$iers are used $or varia7le names, $unction names, structure names, and $ield selectors A$ield
selectors select com.onents o$ vectors and matrices similar to structure $ields, as discussed in "ection ,.,
PVector !om.onentsQ and "ection ,.( P9atri2 !om.onentsQ B. Identi$iers have the $orm
i%enti-ier
non%igit
i%enti-ier non%igit
i%enti-ier %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 G " A S T U V W *
%igit: one o$
16
3 Basics
+ 1 D E 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 as
either a varia7le or a $unction. +owever, as noted in the s.eci$ication, there are some cases where
.reviousl# declared varia7les can 7e redeclared to change or add some .ro.ert#, and .redeclared Xgl>X
names are allowed to 7e redeclared in a shader onl# $or these s.eci$ic .ur.oses. 9ore generall#, it is an
error to redeclare a varia7le, including those starting Pgl>Q.
!.2 Static 3se
"ome language rules descri7ed 7elow de.end on whether something is staticall. written or used.
A shader contains a static use o$ Aor static assignment toB a varia7le x i$, a$ter .re.rocessing, the shader
contains a statement that would read Aor writeB x, whether or not run;time $low o$ control will cause that
statement to 7e e2ecuted.
17
# )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
o.tionall# =uali$iers. A varia7le is declared 7# s.eci$#ing its t#.e $ollowed 7# one or more names
se.arated 7# commas. In man# cases, a varia7le can 7e initiali?ed as .art o$ its declaration 7# using the
assignment o.erator A;B. -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 struct to aggregate a list o$ e2isting t#.es into a single name.
-he '.en/0 "hading 0anguage is t#.e sa$e. -here are no im.licit conversions 7etween t#.es, with the
e2ce.tion that an integer value ma# a..ear where a $loating;.oint t#.e is e2.ected, and 7e converted to a
$loating;.oint value. *2actl# how and when this can occur is descri7ed in "ection ).1.18 PIm.licit
!onversionsQ and as re$erenced 7# other sections in this s.eci$ication.
#.1 ,asic Types
-he '.en/0 "hading 0anguage su..orts the $ollowing 7asic data t#.es, grou.ed as $ollows.
-rans.arent 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;com.onent $loating;.oint vector
vecE a three;com.onent $loating;.oint vector
vecF a $our;com.onent $loating;.oint vector
bvecD a two;com.onent Boolean vector
bvecE a three;com.onent Boolean vector
bvecF a $our;com.onent Boolean vector
ivecD a two;com.onent signed integer vector
ivecE a three;com.onent signed integer vector
ivecF a $our;com.onent signed integer vector
18
4 Variables and Types
Type 4eaning
uvecD a two;com.onent unsigned integer vector
uvecE a three;com.onent unsigned integer vector
uvecF a $our;com.onent unsigned integer vector
matD a Y $loating;.oint matri2
matE a &Y& $loating;.oint matri2
matF a )Y) $loating;.oint matri2
matDxD same as a matD
matDxE a $loating;.oint matri2 with columns and & rows
matDxF a $loating;.oint matri2 with columns and ) rows
matExD a $loating;.oint matri2 with & columns and rows
matExE same as a matE
matExF a $loating;.oint matri2 with & columns and ) rows
matFxD a $loating;.oint matri2 with ) columns and rows
matFxE a $loating;.oint matri2 with ) columns and & rows
matFxF same as a matF
%loating 3oint "am.ler -#.es Ao.a=ueB
Type 4eaning
sampler1B a handle $or accessing a 1D te2ture
samplerDB a handle $or accessing a D te2ture
samplerEB a handle $or accessing a &D te2ture
sampler!ube a handle $or accessing a cu7e ma..ed te2ture
sampler1B"hadow a handle $or accessing a 1D de.th te2ture with com.arison
samplerDB"hadow a handle $or accessing a D de.th te2ture with com.arison
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# de.th te2ture with com.arison
samplerDBArray"hadow a handle $or accessing a D arra# de.th te2ture with com.arison
"igned Integer "am.ler -#.es Ao.a=ueB
Type 4eaning
isampler1B a handle $or accessing an integer 1D te2ture
19
4 Variables and Types
Type 4eaning
isamplerDB a handle $or accessing an integer D te2ture
isamplerEB a handle $or accessing an integer &D te2ture
isampler!ube a handle $or accessing an integer cu7e ma..ed te2ture
isampler1BArray a handle $or accessing an integer 1D arra# te2ture
isamplerDBArray a handle $or accessing an integer D arra# te2ture
6nsigned Integer "am.ler -#.es Ao.a=ueB
Type 4eaning
usampler1B a handle $or accessing an unsigned integer 1D te2ture
usamplerDB a handle $or accessing an unsigned integer D te2ture
usamplerEB a handle $or accessing an unsigned integer &D te2ture
usampler!ube a handle $or accessing an unsigned integer cu7e ma..ed 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 com.le2 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 void cannot 7e used in an# other declarations Ae2ce.t $or em.t# $ormal or actual .arameter
listsB.
#.1.2 ,ooleans
-o make conditional e2ecution o$ code easier to e2.ress, the t#.e bool is su..orted. -here is no
e2.ectation that hardware directl# su..orts 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 true and false can 7e used as literal
Boolean constants. Booleans are declared and o.tionall# initiali?ed as in the $ollow e2am.le:
$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 o.erator A ; B must 7e an e2.ression whose t#.e is bool.
*2.ressions used $or conditional Mum.s Aif# for# ?=# while# do(whileB must evaluate to the t#.e bool.
20
4 Variables and Types
#.1.! Integers
"igned and unsigned integer varia7les are $ull# su..orted. 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 com.lement $orm. '.erations resulting in
over$low or under$low will not cause an# e2ce.tion, nor will the# saturate, rather the# will Pwra.Q to #ield
the low;order & 7its o$ the result.
Integers are declared and o.tionall# initiali?ed with integer e2.ressions, as in the $ollowing e2am.le:
int i- . , /0' (( default integer literal t1pe is int
uint 2 , 3u' (( )u* esta$lis4es t4e t1pe as uint
0iteral integer constants can 7e e2.ressed in decimal A7ase 18B, octal A7ase 4B, or he2adecimal A7ase 1(B
as $ollows.
integer-constant :
%ecimal-constant integer-su--ixo!t
octal-constant integer-su--ixo!t
'exa%ecimal-constant integer-su--ixo!t
integer-su--ix: one o$
u S
%ecimal-constant :
non3ero-%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 :
+
non3ero-%igit
non3ero-%igit : one o$
1 D E F X Y Z [ ,
octal-%igit = one o$
+ 1 D E F X Y Z
'exa%ecimal-%igit = one o$
+ 1 D E F X Y Z [ ,
a b c d e f
21
4 Variables and Types
A H ! B E J
<o white s.ace is allowed 7etween the digits o$ an integer constant, including a$ter the leading + or a$ter
the leading +x or +V o$ a constant, or 7e$ore the su$$i2 u or S. Rhen the su$$i2 u or S is .resent, the
literal has t#.e uint, otherwise the t#.e is int. A leading unar# minus sign A;B is inter.reted as an
arithmetic unar# negation, not as .art o$ the constant.
It is an error to .rovide a literal integer whose magnitude is too large to store in a varia7le o$ matching
signed or unsigned t#.e.
#.1.# Floats
%loats are availa7le $or use in a variet# o$ scalar calculations. %loating;.oint varia7les are de$ined as in the
$ollowing e2am.le:
float a- $ , 567'
As an in.ut value to one o$ the .rocessing units, a $loating;.oint varia7le is e2.ected to match the I***
single .recision $loating;.oint de$inition $or .recision and d#namic range. It is not re=uired that the
.recision o$ internal .rocessing match the I*** $loating;.oint s.eci$ication $or $loating;.oint o.erations,
7ut the guidelines $or .recision esta7lished 7# the '.en/0 1.) s.eci$ication must 7e met. "imilarl#,
treatment o$ conditions such as divide 7# 8 ma# lead to an uns.eci$ied result, 7ut in no case should such a
condition lead to the interru.tion or termination o$ .rocessing.
%loating;.oint constants are de$ined as $ollows.
-loating-constant :
-ractional-constant ex!onent-!art
o!t
-loating-su--ixo!t

%igit-se4uence ex!onent-!art -loating-su--ixo!t
-ractional-constant :
%igit-se4uence . %igit-se4uence
%igit-se4uence .
. %igit-se4uence
ex!onent-!art :
e sign
o!t
%igit-se4uence
E sign
o!t
%igit-se4uence
sign : one o$
- \
%igit-se4uence :
%igit
%igit-se4uence %igit
-loating-su--ix: one o$
f J
22
4 Variables and Types
A decimal .oint A . B is not needed i$ the e2.onent .art is .resent. <o white s.ace ma# a..ear an#where
within a $loating;.oint constant, including 7e$ore a su$$i2. A leading unar# minus sign A(B is inter.reted as
a unar# o.erator and is not .art o$ the $loating;.oint constant
#.1.& )ectors
-he '.en/0 "hading 0anguage includes data t#.es $or generic ;, &;, and );com.onent vectors o$
$loating;.oint values, integers, or Booleans. %loating;.oint vector varia7les can 7e used to store colors,
normals, .ositions, te2ture coordinates, te2ture looku. results and the like. Boolean vectors can 7e used
$or com.onent;wise com.arisons o$ numeric vectors. "ome e2am.les o$ vector declaration are:
ve&0 tex&oord5- tex&oord0'
ve&3 position'
ve&/ m1R!+8'
ive&0 textureLoo2up'
$ve&3 less'
Initiali?ation o$ vectors can 7e done with constructors, which are discussed shortl#.
#.1.' 4atrices
-he '.en/0 "hading 0anguage has 7uilt;in t#.es $or Y, Y&, Y), &Y, &Y&, &Y), )Y, )Y&, and )Y)
matrices o$ $loating;.oint num7ers. -he $irst num7er in the t#.e is the num7er o$ columns, the second is
the num7er o$ rows. *2am.le matri2 declarations:
mat0 mat0'
mat3 opt9atrix'
mat/ vie:- pro.e&tion'
mat/x/ vie:' (( an alternate :a1 of de&laring a mat/
mat3x0 m' (( a matrix :it4 3 &olumns and 0 ro:s
Initiali?ation o$ matri2 values is done with constructors Adescri7ed in "ection ,.) P!onstructorsQ B in
column;maMor order.
#.1.1 Samplers
"am.ler t#.es Ae.g. samplerDBB are e$$ectivel# o.a=ue handles to te2tures and their $ilters. -he# are used
with the 7uilt;in te2ture $unctions Adescri7ed in "ection 4.1 P-e2ture 0ooku. %unctionsQ B to s.eci$#
which te2ture to access and how it is to 7e $iltered. -he# can onl# 7e declared as $unction .arameters or
uniform varia7les Asee "ection ).&., P6ni$ormQ B. *2ce.t $or arra# inde2ing, structure $ield selection,
and .arentheses, sam.lers are not allowed to 7e o.erands in e2.ressions. "am.lers aggregated into arra#s
within a shader Ausing s=uare 7rackets 3 4B can onl# 7e inde2ed with integral constant e2.ressions Asee
"ection ).&.& P!onstant *2.ressionsQB. "am.lers cannot 7e treated as l;valuesC hence cannot 7e used as
out or inout $unction .arameters, nor can the# 7e assigned into. As uni$orms, the# are initiali?ed onl#
with the '.en/0 A3IC the# cannot 7e declared with an initiali?er in a shader. As $unction .arameters,
onl# sam.lers ma# 7e .assed to sam.lers o$ matching t#.e. -his ena7les consistenc# checking 7etween
shader te2ture accesses and '.en/0 te2ture state 7e$ore a shader is run.
23
4 Variables and Types
#.1.2 Structures
6ser;de$ined t#.es can 7e created 7# aggregating other alread# de$ined t#.es into a structure using the
struct ke#word. %or e2am.le,
stru&t lig4t ;
float intensit1'
ve&3 position'
< lig4tVar'
In this e2am.le, lig't 7ecomes the name o$ the new t#.e, and lig'tVar 7ecomes a varia7le o$ t#.e lig't.
-o declare varia7les o$ the new t#.e, use its name Awithout the ke#word structB.
lig4t lig4tVar0'
9ore $ormall#, structures are declared as $ollows. +owever, the com.lete correct grammar is as given in
"ection 5 P"hading 0anguage /rammarQ .
struct-%e-inition :
4uali-ier
o!t
struct name
o!t
5 mem,er-list 6 %eclarators
o!t
5
mem,er-list :
mem,er-%eclaration5
mem,er-%eclaration mem,er-list5
mem,er-%eclaration :
,asic-t.!e %eclarators5
where name 7ecomes the user;de$ined t#.e, and can 7e used to declare varia7les to 7e o$ this new t#.e.
-he name shares the same name s.ace as other varia7les, t#.es, and $unctions, with the same sco.ing
rules. -he o.tional 4uali-ier onl# a..lies 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. <or do the# contain an# 7it $ields. 9em7er t#.es
must 7e alread# de$ined Athere are no $orward re$erencesB. 9em7er declarations cannot contain
initiali?ers. 9em7er declarators can contain arra#s. "uch arra#s must have a si?e s.eci$ied, and the si?e
must 7e an integral constant e2.ression thatOs greater than ?ero Asee "ection ).&.& P!onstant
*2.ressionsQ B. *ach level o$ structure has its own name s.ace $or names given in mem7er declaratorsC
such names need onl# 7e uni=ue within that name s.ace.
Anon#mous structures are not su..orted. *m7edded structure de$initions are not su..orted.
24
4 Variables and Types
stru&t S ; float f' <'
stru&t T ;
S' (( Error% anon1mous stru&tures disallo:ed
stru&t ; 666 <' (( Error% em$edded stru&tures disallo:ed
S s' (( O2a1% nested stru&tures :it4 name are allo:ed
<'
"tructures can 7e initiali?ed at declaration time using constructors, as discussed in "ection ,.).&
P"tructure !onstructorsQ .
#.1.5 -rrays
Varia7les o$ the same t#.e can 7e aggregated into arra#s 7# declaring a name $ollowed 7# 7rackets A 3 4 B
enclosing an o.tional si?e. Rhen an arra# si?e is s.eci$ied in a declaration, it must 7e an integral constant
e2.ression Asee "ection ).&.& P!onstant *2.ressionsQ B greater than ?ero. I$ an arra# is inde2ed with an
e2.ression that is not an integral constant e2.ression, or i$ an arra# is .assed as an argument to a $unction,
then its si?e must 7e declared 7e$ore an# such use. It is legal to declare an arra# without a si?e and then
later re;declare the same name as an arra# o$ the same t#.e and s.eci$# a si?e. It is illegal to declare an
arra# with a si?e, and then later Ain the same shaderB inde2 the same arra# with an integral constant
e2.ression greater than or e=ual to the declared si?e. It is also illegal to inde2 an arra# with a negative
constant e2.ression. Arra#s declared as $ormal .arameters in a $unction declaration must s.eci$# a si?e.
6nde$ined 7ehavior results $rom inde2ing an arra# with a non;constant e2.ression thatDs greater than or
e=ual to the arra#Ds si?e or less than 8. 'nl# one;dimensional arra#s ma# 7e declared. All 7asic t#.es and
structures can 7e $ormed into arra#s. "ome e2am.les are:
float fre=uen&ies>3?'
uniform ve&/ lig4t@osition>/?'
lig4t lig4ts>?'
&onst int numLig4ts , 0'
lig4t lig4ts>numLig4ts?'
An arra# t#.e can 7e $ormed 7# s.eci$#ing a t#.e $ollowed 7# s=uare 7rackets AK LB and including a si?e:
float>7?
-his t#.e can 7e used an#where an# other t#.e can 7e used, including as the return value $rom a $unction
float>7? foo#) ; <
as a constructor o$ an arra#
float>7?#36/- /60- 76A- 760- 565)
as an unnamed .arameter
void foo#float>7?)
and as an alternate wa# o$ declaring a varia7le or $unction .arameter.
25
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 e2.licitl# 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, im.licitl# si?ed arra#s cannot 7e assigned to. <ote, this is a rare case that initiali?ers and
assignments a..ear to have di$$erent semantics.
Arra#s know the num7er o$ elements the# contain. -his can 7e o7tained 7# using the length method:
a6lengt4#)' (( returns 7 for t4e a$ove de&larations
-he length method cannot 7e called on an arra# that has not 7een e2.licitl# si?ed.
26
4 Variables and Types
#.1.16 Implicit Conersions
In some situations, an e2.ression and its t#.e will 7e im.licitl# converted to a di$$erent t#.e. -he
$ollowing ta7le shows all allowed im.licit conversions:
Type of e*pression Can be implicitly conerted to
int
uint
float
ivecD
uvecD
vecD
ivecE
uvecE
vecE
ivecF
uvecF
vecF
-here are no im.licit arra# or structure conversions. %or e2am.le, an arra# o$ int cannot 7e im.licitl#
converted to an arra# o$ float. -here are no im.licit conversions 7etween signed and unsigned integers.
Rhen an im.licit conversion is done, it is not a re;inter.retation o$ the e2.ressionOs 7it .attern, 7ut a
conversion o$ its value to an e=uivalent value in the new t#.e. %or e2am.le, the integer value ;X will 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 s.eci$ication.
#.2 Scoping
-he sco.e o$ a varia7le is determined 7# where it is declared. I$ it is declared outside all $unction
de$initions, it has glo7al sco.e, 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 while test or a for statement, then it is sco.ed to the end o$ the
$ollowing su7;statement. 'therwise, i$ it is declared as a statement within a com.ound statement, it is
sco.ed to the end o$ that com.ound statement. I$ it is declared as a .arameter in a $unction de$inition, it is
sco.ed until the end o$ that $unction de$inition. A $unction 7od# has a sco.e nested inside the $unctionDs
de$inition. -he if statementDs e2.ression does not allow new varia7les to 7e declared, hence does not
$orm a new sco.e.
27
4 Variables and Types
Rithin a declaration, the sco.e o$ a name starts immediatel# a$ter the initiali?er i$ .resent or immediatel#
a$ter the name 7eing declared i$ not. "everal e2am.les:
int x , 5'
;
int x , 0- 1 , x' (( 1 is initiali"ed to 0
<
stru&t S
;
int x'
<'
;
S S , S#A-A)' (( BSB is onl1 visi$le as a stru&t and &onstru&tor
S' (( BSB is no: visi$le as a varia$le
<
int x , x' (( Error if x 4as not $een previousl1 defined6
All varia7le names, structure t#.e names, and $unction names in a given sco.e share the same name s.ace.
%unction names can 7e redeclared in the same sco.e, with the same or di$$erent .arameters, without error.
An im.licitl# si?ed arra# can 7e re;declared in the same sco.e as an arra# o$ the same 7ase t#.e.
'therwise, within one com.ilation unit, a declared name cannot 7e redeclared in the same sco.eC doing so
results in a redeclaration error. I$ a nested sco.e redeclares a name used in an outer sco.e, it hides all
e2isting uses o$ that name. -here is no wa# to access the hidden name or make it unhidden, without
e2iting the sco.e that hid it.
-he 7uilt;in $unctions are sco.ed in a sco.e outside the glo7al sco.e users declare glo7al varia7les in.
-hat is, a shaderOs glo7al sco.e, availa7le $or user;de$ined $unctions and glo7al varia7les, is nested inside
the sco.e containing the 7uilt;in $unctions. Rhen a $unction name is redeclared in a nested sco.e, it hides
all $unctions declared with that name in the outer sco.e. %unction declarations A.rotot#.esB cannot occur
inside o$ $unctionsC the# must 7e at glo7al sco.e, or $or the 7uilt;in $unctions, outside the glo7al sco.e.
"hared glo7als are glo7al varia7les declared with the same name in inde.endentl# com.iled units
AshadersB o$ the same language Averte2 or $ragmentB that are linked together to make a single .rogram.
"hared glo7als share the same name s.ace, and must 7e declared with the same t#.e. -he# will share the
same storage. "hared glo7al arra#s must have the same 7ase t#.e and the same e2.licit si?e. An arra#
im.licitl# si?ed in one shader can 7e e2.licitl# si?ed 7# another shader. I$ no shader has an e2.licit si?e
$or the arra#, the largest im.licit si?e is used. "calars must have e2actl# the same t#.e name and t#.e
de$inition. "tructures must have the same name, se=uence o$ t#.e names, and t#.e de$initions, and $ield
names to 7e considered the same t#.e. -his rule a..lies recursivel# $or nested or em7edded t#.es. All
initiali?ers $or a shared glo7al must have the same value, or a link error will result.
28
4 Variables and Types
#.! Storage 7ualifiers
Varia7le declarations ma# have one storage =uali$ier s.eci$ied in $ront o$ the t#.e. -hese are summari?ed
as
7ualifier 4eaning
F none: de$ault G local read@write memor#, or an in.ut .arameter to a $unction
const a com.ile;time constant, or a $unction .arameter that is read;onl#
in
centroid in
linkage into a shader $rom a .revious stage, varia7le is co.ied in
linkage with centroid 7ased inter.olation
out
centroid out
linkage out o$ a shader to a su7se=uent stage, varia7le is co.ied out
linkage with centroid 7ased inter.olation
attribute de.recatedC linkage 7etween a verte2 shader and '.en/0 $or .er;verte2
data
uniform value does not change across the .rimitive 7eing .rocessed, uni$orms
$orm the linkage 7etween a shader, '.en/0, and the a..lication
varying
centroid varying
de.recatedC linkage 7etween a verte2 shader and a $ragment shader $or
inter.olated data
'ut.uts $rom a verte2 shader AoutB and in.uts to a $ragment shader AinB can 7e $urther =uali$ied with one
or more o$ these inter.olation =uali$iers
7ualifier 4eaning
smooth .ers.ective correct inter.olation
flat no inter.olation
noperspective linear inter.olation
-hese inter.olation =uali$iers ma# onl# .recede the =uali$iers in, centroid in, out, or centroid out in a
declaration. -he# do not a..l# to the de.recated storage =uali$iers varying or centroid varying. -he#
also do not a..l# to in.uts into a verte2 shader or out.uts $rom a $ragment shader.
0ocal varia7les can onl# use the const storage =uali$ier.
%unction .arameters can use const, in, and out =uali$iers, 7ut as !arameter 4uali-iers. 3arameter
=uali$iers are discussed in "ection (.1.1 P%unction !alling !onventionsQ.
%unction return t#.es and structure $ields do not use storage =uali$iers.
Data t#.es $or communication $rom one run o$ a shader e2ecuta7le to its ne2t run Ato communicate
7etween $ragments or 7etween verticesB do not e2ist. -his would .revent .arallel e2ecution o$ the same
shader e2ecuta7le on multi.le vertices or $ragments.
29
4 Variables and Types
Initiali?ers ma# onl# 7e used in declarations o$ glo7als with no storage =uali$ier, with a const =uali$ier or
with a uniform =uali$ier. /lo7al varia7les without storage =uali$iers that are not initiali?ed in their
declaration or 7# the a..lication will not 7e initiali?ed 7# '.en/0, 7ut rather will enter main67 with
unde$ined values.
#.!.1 (efault Storage 7ualifier
I$ no =uali$ier is .resent on a glo7al varia7le, then the varia7le has no linkage to the a..lication or shaders
running on other .i.eline stages. %or either glo7al or local un=uali$ied varia7les, the declaration will
a..ear to allocate memor# associated with the .rocessor it targets. -his varia7le will .rovide read@write
access to this allocated memor#.
#.!.2 Const
<amed com.ile;time constants can 7e declared using the const =uali$ier. An# varia7les =uali$ied as
constant are read;onl# varia7les $or that shader. Declaring varia7les as constant allows more descri.tive
shaders than using hard;wired numerical constants. -he const =uali$ier can 7e used with an# o$ the 7asic
data t#.es. It is an error to write to a const varia7le outside o$ its declaration, so the# must 7e initiali?ed
when declared. %or e2am.le,
&onst ve&3 "8xis , ve&3 #A6A- A6A- 56A)'
"tructure $ields ma# not 7e =uali$ied with const. "tructure varia7les can 7e declared as const, and
initiali?ed with a structure constructor.
Initiali?ers $or const declarations must 7e constant e2.ressions, as de$ined in "ection ).&.& P!onstant
*2.ressions.Q
#.!.! Constant $*pressions
A constant ex!ression is one o$
a literal value Ae.g., X or trueB
a glo7al or local varia7le =uali$ied as const Ai.e. not including $unction .arametersB
an e2.ression $ormed 7# an o.erator on o.erands that are all constant e2.ressions, including getting an
element or length o$ a constant arra#, or a $ield o$ a constant structure, or com.onents o$ a constant
vector.
a constructor whose arguments are all constant e2.ressions
a 7uilt;in $unction call whose arguments are all constant e2.ressions, with the e2ce.tion o$ the te2ture
looku. $unctions, the noise $unctions, and ftransform. -he 7uilt;in $unctions dJdx, dJdy, and
fwidth must return 8 when evaluated inside an initiali?er with an argument that is a constant
e2.ression.
%unction calls to user;de$ined $unctions Anon;7uilt;in $unctionsB cannot 7e used to $orm constant
e2.ressions.
An integral constant ex!ression is a constant e2.ression that evaluates to a scalar signed or unsigned
integer.
30
4 Variables and Types
!onstant e2.ressions will 7e evaluated in an invariant wa# so as to create the same value in multi.le
shaders when the same constant e2.ressions a..ear in those shaders. "ee section ).(.1 P-he Invariant
:uali$ierQ $or more details on how to create invariant e2.ressions.
#.!.# Inputs
"hader in.ut varia7les are declared with the in storage =uali$ier or the centroid in storage =uali$ier. -he#
$orm the in.ut inter$ace 7etween .revious stages o$ the '.en/0 .i.eline and the declaring shader. In.ut
varia7les must 7e declared at glo7al sco.e. Values $rom the .revious .i.eline stage are co.ied into in.ut
varia7les at the 7eginning o$ shader e2ecution. Varia7les declared as in or centroid in ma# not 7e written
to during shader e2ecution.
Verte2 shader in.ut varia7les Aor attri7utesB receive .er;verte2 data. -he# are declared in a verte2 shader
with the in =uali$ier or the de.recated attribute =uali$ier. It is an error to use centroid in in a verte2
shader. -he values co.ied in are esta7lished 7# the '.en/0 A3I. It is an error to use attribute in a non;
verte2 shader. Verte2 shader in.uts can onl# 7e float, $loating;.oint vectors, matrices, signed and
unsigned integers and integer vectors. -he# cannot 7e arra#s or structures.
*2am.le declarations in a verte2 shader:
in ve&/ position'
in ve&3 normal'
in ve&0 texCoord'
"ee "ection 1 PBuilt;in Varia7lesQ $or a list o$ the 7uilt;in in.ut names.
%ragment shader in.uts Aor var#ingsB get .er;$ragment values, t#.icall# inter.olated $rom a .revious
stageOs out.uts. -he# are declared in $ragment shaders with the in storage =uali$ier, the centroid in
storage =uali$ier, or the de.recated varying and centroid varying storage =uali$iers. %ragment in.uts
can onl# 7e signed and unsigned integers and integer vectors, float, $loating;.oint vectors, matrices, or
arra#s o$ these. "tructures cannot 7e in.ut.
%ragment in.uts are declared as in the $ollowing e2am.les:
in ve&3 normal'
&entroid in ve&0 TexCoord'
invariant &entroid in ve&/ Color'
noperspe&tive out float temperature'
flat in ve&3 m1Color'
&entroid noperspe&tive in ve&0 m1TexCoord'
It is e2.ected that gra.hics hardware will have a small num7er o$ $i2ed vector locations $or .assing verte2
in.uts. -here$ore, the '.en/0 "hading language de$ines each non;matri2 in.ut varia7le as taking u. one
such vector location . -here is an im.lementation de.endent limit on the num7er o$ locations that can 7e
used, and i$ this is e2ceeded it will cause a link error. ADeclared in.ut varia7les that are not staticall# used
do not count against this limit.B A scalar in.ut counts the same amount against this limit as a vecF, so
a..lications ma# want to consider .acking grou.s o$ $our unrelated $loat in.uts together into a vector to
7etter utili?e the ca.a7ilities o$ the underl#ing hardware. A matri2 in.ut will use u. multi.le locations.
-he num7er o$ locations used will e=ual the num7er o$ columns in the matri2.
31
4 Variables and Types
#.!.& 3niform
-he uniform =uali$ier is used to declare glo7al varia7les whose values are the same across the entire
.rimitive 7eing .rocessed. All uniform varia7les are read;onl# and are initiali?ed e2ternall# either at link
time or through the A3I. -he link time initial value is either the value o$ the varia7leOs initiali?er, i$
.resent, or 8 i$ no initiali?er is .resent. "am.ler t#.es cannot have initiali?ers.
*2am.le declarations are:
uniform ve&/ lig4t@osition'
uniform ve&3 &olor , ve&3#A6D- A6D- A60)' (( value assigned at lin2 time
-he uniform =uali$ier can 7e used with an# o$ the 7asic data t#.es, or when declaring a varia7le whose
t#.e is a structure, or an arra# o$ an# o$ these.
-here is an im.lementation de.endent limit on the amount o$ storage $or uni$orms that can 7e used $or
each t#.e o$ shader and i$ this is e2ceeded it will cause a com.ile;time or link;time error. 6ni$orm
varia7les that are declared 7ut not used do not count against this limit. -he num7er o$ user;de$ined
uni$orm varia7les and the num7er o$ 7uilt;in uni$orm varia7les that are used within a shader are added
together to determine whether availa7le uni$orm storage has 7een e2ceeded.
I$ multi.le shaders are linked together, then the# will share a single glo7al uni$orm name s.ace. +ence,
the t#.es and initiali?ers o$ uni$orm varia7les with the same name must match across all shaders that are
linked into a single e2ecuta7le. It is legal $or some shaders to .rovide an initiali?er $or a .articular
uni$orm varia7le, while another shader does not, 7ut all .rovided initiali?ers must 7e e=ual.
#.!.' Outputs
"hader out.ut varia7les are declared with the out or centroid out storage =uali$iers. -he# $orm the
out.ut inter$ace 7etween the declaring shader and the su7se=uent stages o$ the '.en/0 .i.eline. 'ut.ut
varia7les must 7e declared at glo7al sco.e. During shader e2ecution the# will 7ehave as normal
un=uali$ied glo7al varia7les. -heir values are co.ied out to the su7se=uent .i.eline stage on shader e2it.
-here is not an inout storage =uali$ier at glo7al sco.e $or declaring a single varia7le name as 7oth in.ut
and out.ut to a shader. 'ut.ut varia7les must 7e declared with di$$erent names than in.ut varia7les.
Verte2 out.ut varia7les out.ut .er;verte2 data and are declared using the out storage =uali$ier, the
centroid out storage =uali$ier, or the de.recated varying storage =uali$ier. -he# can onl# 7e float,
$loating;.oint vectors, matrices, signed or unsigned integers or integer vectors, or arra#s o$ an# these. I$ a
verte2 out.ut is a signed or unsigned integer or integer vector, then it must 7e =uali$ied with the
inter.olation =uali$ier flat. "tructures cannot 7e out.ut.
Verte2 out.uts are declared as in the $ollowing e2am.les:
32
4 Variables and Types
out ve&3 normal'
&entroid out ve&0 TexCoord'
invariant &entroid out ve&/ Color'
noperspe&tive out float temperature' (( var1ing is depre&ated
flat out ve&3 m1Color'
noperspe&tive &entroid out ve&0 m1TexCoord'
%ragment out.uts out.ut .er;$ragment data and are declared using the out storage =uali$ier. It is an error
to use centroid out in a $ragment shader. %ragment out.uts can onl# 7e float, $loating;.oint vectors,
signed or unsigned integers or integer vectors, or arra#s o$ an# these. 9atrices and structures cannot 7e
out.ut. %ragment out.uts are declared as in the $ollowing e2am.les:
out ve&/ FragmentColor'
out uint Luminosit1'
#.!.1 Interpolation
-he .resence o$ and t#.e o$ inter.olation is controlled 7# the storage =uali$iers centroid in and centroid
out, and 7# the o.tional inter.olation =uali$iers smooth, flat, and noperspective as well as 7# de$ault
7ehaviors esta7lished through the '.en/0 A3I when no inter.olation =uali$ier is .resent. Rhen an
inter.olation =uali$ier is used, it overrides settings esta7lished through the '.en/0 A3I. It is a com.ile;
time error to use more than one inter.olation =uali$ier.
-he $ollowing .redeclared varia7les can 7e redeclared with an inter.olation =uali$ier:
Verte2 language:
gl_FrontColor #depre&ated)
gl_+a&2Color #depre&ated)
gl_FrontSe&ondar1Color #depre&ated)
gl_+a&2Se&ondar1Color #depre&ated)
%ragment language:
gl_Color #depre&ated)
gl_Se&ondar1Color #depre&ated)
%or e2am.le,
in ve&/ gl_Color' (( prede&lared $1 t4e fragment language
flat in ve&/ gl_Color' (( rede&lared $1 user to $e flat
I$ gl_olor is redeclared with an inter.olation =uali$ier, then gl_$rontolor and gl_8ac1olor Ai$ the#
are written toB must also 7e redeclared with the same inter.olation =uali$ier, and vice versa. I$
gl_*econ%ar.olor is redeclared with an inter.olation =uali$ier, then gl_$ront*econ%ar.olor and
gl_8ac1*econ%ar.olor Ai$ the# are written toB must also 7e redeclared with the same inter.olation
=uali$ier, and vice versa. -his =uali$ier matching on .redeclared varia7les is onl# re=uired $or varia7les
that are staticall# used within the shaders in a .rogram.
33
4 Variables and Types
A varia7le =uali$ied as flat will not 7e inter.olated. Instead, it will have the same value $or ever#
$ragment within a triangle. -his value will come $rom a single .rovoking verte2, as descri7ed 7# the
'.en/0 /ra.hics "#stem ".eci$ication. 6ser;declared varia7les can 7e =uali$ied as flat and the
.redeclared varia7les listed a7ove and can 7e redeclared as flat. It is an error to declare an# other 7uilt;in
varia7le as flat. A varia7le ma# 7e =uali$ied as flat centroid, which will mean the same thing as
=uali$#ing it onl# as flat.
A varia7le =uali$ied as smooth will 7e inter.olated in a .ers.ective;correct manner over the .rimitive
7eing rendered. Inter.olation in a .ers.ective correct manner is s.eci$ied in e=uations &.( and &.4 in the
'.en/0 /ra.hics "#stem ".eci$ication, Version &.8.
A varia7le =uali$ied as noperspective must 7e inter.olated linearl# in screen s.ace, as descri7ed in
e=uation &.1 and the a..ro2imation that $ollows e=uation &.4 in the '.en/0 /ra.hics "#stem
".eci$ication, Version &.8.
-his .aragra.h onl# a..lies i$ inter.olation is 7eing done: I$ single;sam.ling, the value is inter.olated to
the .i2elOs center, and the centroid =uali$ier, i$ .resent, is ignored. I$ multi;sam.ling and the varia7le is
not =uali$ied with centroid, then the value must 7e inter.olated to the .i2elOs center, or an#where within
the .i2el, or to one o$ the .i2elOs sam.les. I$ multi;sam.ling and the varia7le is =uali$ied with centroid,
then the value must 7e inter.olated to a .oint that lies in 7oth the .i2el and in the .rimitive 7eing
rendered, or to one o$ the .i2elOs sam.les that $alls within the .rimitive. Due to the less regular location o$
centroids, their derivatives ma# 7e less accurate than non;centroid inter.olated varia7les.
-he t#.e and .resence o$ the inter.olation =uali$iers and storage =uali$iers and invariant =uali$iers o$
varia7les with the same name declared in linked verte2 and $ragments shaders must match, otherwise the
link command will $ail. 'nl# those in.ut varia7les read in the $ragment shader e2ecuta7le must 7e written
to 7# the verte2 shader e2ecuta7leC declaring su.er$luous out.ut varia7les in a verte2 shader is
.ermissi7le.
#.# +arameter 7ualifiers
3arameters can have these =uali$iers.
7ualifier 4eaning
F none: de$ault G same is in
in $or $unction .arameters .assed into a $unction
out $or $unction .arameters .assed 7ack out o$ a $unction, 7ut not initiali?ed
$or use when .assed in
inout $or $unction .arameters .assed 7oth into and out o$ a $unction
34
4 Variables and Types
3arameter =uali$iers are discussed in more detail in "ection (.1.1 P%unction !alling !onventionsQ.
#.& +recision and +recision 7ualifiers
3recision =uali$iers are added $or code .orta7ilit# with '.en/0 *", not $or $unctionalit#. -he# have the
same s#nta2 as in '.en/0 *", as descri7ed 7elow, 7ut the# have no semantic meaning, which includes no
e$$ect on the .recision used to store or o.erate on varia7les.
I$ an e2tension adds in the same semantics and $unctionalit# in the '.en/0 *" .8 s.eci$ication $or
.recision =uali$iers, then the e2tension is allowed to reuse the ke#words 7elow $or that .ur.ose.
#.&.1 8ange and +recision
"ection num7er reserved $or $uture use.
#.&.2 +recision 7ualifiers
An# $loating .oint or an# integer declaration can have the t#.e .receded 7# one o$ these .recision
=uali$iers:
7ualifier 4eaning
highp <one.
mediump <one.
lowp <one.
%or e2am.le:
lo:p float &olor'
out mediump ve&0 @'
lo:p ive&0 foo#lo:p mat3)'
4ig4p mat/ m'
0iteral constants do not have .recision =uali$iers. <either do Boolean varia7les. <either do $loating .oint
constructors nor integer constructors when none o$ the constructor arguments have .recision =uali$iers.
3recision =uali$iers, as with other =uali$iers, do not e$$ect the 7asic t#.e o$ the varia7le. In .articular,
there are no constructors $or .recision conversionsC constructors onl# convert t#.es. "imilarl#, .recision
=uali$iers, as with other =uali$iers, do not contri7ute to $unction overloading 7ased on .arameter t#.es. As
discussed in the ne2t cha.ter, $unction in.ut and out.ut is done through co.ies, and there$ore =uali$iers do
not have to match.
-he same o7Mect declared in di$$erent shaders that are linked together must have the same .recision
=uali$ication. -his a..lies to in.uts, out.uts, uni$orms, and glo7als.
#.&.! (efault +recision 7ualifiers
-he .recision statement
35
4 Variables and Types
pre&ision pre&isionE=ualifier t1pe'
can 7e used to esta7lish a de$ault .recision =uali$ier. -he type $ield can 7e either int or float, and the
!recision-4uali-ier can 7e lowp, mediump, or highp. An# other t#.es or =uali$iers will result in an error.
I$ t.!e is float, the directive a..lies to non;.recision;=uali$ied $loating .oint t#.e Ascalar, vector, and
matri2B declarations. I$ t.!e is int, the directive a..lies to all non;.recision;=uali$ied integer t#.e Ascalar,
vector, signed, and unsignedB declarations. -his includes glo7al varia7le declarations, $unction return
declarations, $unction .arameter declarations, and local varia7le declarations.
<on;.recision =uali$ied declarations will use the .recision =uali$ier s.eci$ied in the most recent precision
statement that is still in sco.e. -he precision statement has the same sco.ing rules as varia7le
declarations. I$ it is declared inside a com.ound statement, its e$$ect sto.s at the end o$ the innermost
statement it was declared in. 3recision statements in nested sco.es override .recision statements in outer
sco.es. 9ulti.le .recision statements $or the same 7asic t#.e can a..ear inside the same sco.e, with later
statements overriding earlier statements within that sco.e.
-he verte2 language has the $ollowing .redeclared glo7all# sco.ed de$ault .recision statements:
pre&ision 4ig4p float'
pre&ision 4ig4p int'
-he $ragment language has the $ollowing .redeclared glo7all# sco.ed de$ault .recision statement:
pre&ision mediump int'
-he $ragment language has no de$ault .recision =uali$ier $or $loating .oint t#.es. +ence $or float, $loating
.oint vector and matri2 varia7le declarations, either the declaration must include a .recision =uali$ier or
the de$ault $loat .recision must have 7een .reviousl# declared.
#.&.# -ailable +recision 7ualifiers
-he 7uilt;in macro /0>%RA/9*<->3R*!I"I'<>+I/+ is de$ined to 1:
#define !L_FR8!9ENT_@RECISION_FI!F 5
-his macro is availa7le in 7oth the verte2 and $ragment languages.
#.' )ariance and the Inariant 7ualifier
In this section, variance re$ers to the .ossi7ilit# o$ getting di$$erent values $rom the same e2.ression in
di$$erent .rograms. %or e2am.le, sa# two verte2 shaders, in di$$erent .rograms, each set gl$Qosition with
the same e2.ression in 7oth shaders, and the in.ut values into that e2.ression are the same when 7oth
shaders run. It is .ossi7le, due to inde.endent com.ilation o$ the two shaders, that the values assigned to
gl$Qosition are not e2actl# the same when the two shaders run. In this e2am.le, this can cause .ro7lems
with alignment o$ geometr# in a multi;.ass algorithm.
In general, such variance 7etween shaders is allowed. Rhen such variance does not e2ist $or a .articular
out.ut varia7le, that varia7le is said to 7e invariant.
36
4 Variables and Types
#.'.1 The Inariant 7ualifier
-o ensure that a .articular out.ut varia7le is invariant, it is necessar# to use the invariant =uali$ier. It can
either 7e used to =uali$# a .reviousl# declared varia7le as 7eing invariant
invariant gl_@osition' (( ma2e existing gl_@osition $e invariant
out ve&3 Color'
invariant Color' (( ma2e existing Color $e invariant
or as .art o$ a declaration when a varia7le is declared
invariant &entroid out ve&3 Color'
-he invariant =uali$ier must a..ear 7e$ore an# inter.olation =uali$iers or storage =uali$iers when
com7ined with a declaration. 'nl# varia7les out.ut $rom a shader can 7e candidates $or invariance. -his
includes user;de$ined out.ut varia7les and the 7uilt;in out.ut varia7les. %or varia7les leaving a verte2
shader and coming into a $ragment shader with the same name, the invariant ke#word has to 7e used in
7oth the verte2 and $ragment shaders.
-he invariant ke#word can 7e $ollowed 7# a comma se.arated list o$ .reviousl# declared identi$iers. All
uses o$ invariant must 7e at the glo7al sco.e, and 7e$ore an# use o$ the varia7les 7eing declared as
invariant.
-o guarantee invariance o$ a .articular out.ut varia7le across two .rograms, the $ollowing must also 7e
true:
-he out.ut varia7le is declared as invariant in 7oth .rograms.
-he same values must 7e in.ut to all shader in.ut varia7les consumed 7# e2.ressions and $low control
contri7uting to the value assigned to the out.ut varia7le.
-he te2ture $ormats, te2el values, and te2ture $iltering are set the same wa# $or an# te2ture $unction
calls contri7uting to the value o$ the out.ut varia7le.
All in.ut values are all o.erated on in the same wa#. All o.erations in the consuming e2.ressions and
an# intermediate e2.ressions must 7e the same, with the same order o$ o.erands and same
associativit#, to give the same order o$ evaluation. Intermediate varia7les and $unctions must 7e
declared as the same t#.e with the same e2.licit or im.licit .recision =uali$iers. An# control $low
a$$ecting the out.ut value must 7e the same, and an# e2.ressions consumed to determine this control
$low must also $ollow these invariance rules.
All the data $low and control $low leading to setting the invariant out.ut varia7le reside in a single
com.ilation unit.
*ssentiall#, all the data $low and control $low leading to an invariant out.ut must match.
Initiall#, 7# de$ault, all out.ut varia7les are allowed to 7e variant. -o $orce all out.ut varia7les to 7e
invariant, use the .ragma
37
4 Variables and Types
#pragma ST!L invariant#all)
7e$ore all declarations in a shader. I$ this .ragma is used a$ter the declaration o$ an# varia7les or
$unctions, then the set o$ out.uts that 7ehave as invariant is unde$ined. It is an error to use this .ragma in
a $ragment shader.
/enerall#, invariance is ensured at the cost o$ $le2i7ilit# in o.timi?ation, so .er$ormance can 7e degraded
7# use o$ invariance. +ence, use o$ this .ragma is intended as a de7ug aid, to avoid individuall# declaring
all out.ut varia7les as invariant.
#.'.2 Inariance of Constant $*pressions
Invariance must 7e guaranteed $or constant e2.ressions. A .articular constant e2.ression must evaluate to
the same result i$ it a..ears again in the same shader or a di$$erent shader. -his includes the same
e2.ression a..earing in 7oth a verte2 and $ragment shader or the same e2.ression a..earing in di$$erent
verte2 or $ragment shaders.
!onstant e2.ressions must evaluate to the same result when o.erated on as alread# descri7ed a7ove $or
invariant varia7les.
#.1 Order of 7ualification
Rhen multi.le =uali$ications are .resent, the# must $ollow a strict order. -his order is as $ollows.
invariant-4uali-ier inter!olation-4uali-ier storage-4uali-ier !recision 4uali-ier
storage-4uali-ier !arameter-4uali-ier !recision 4uali-ier
38
& Operators and $*pressions
&.1 Operators
-he '.en/0 "hading 0anguage has the $ollowing o.erators.
+recedence Operator Class Operators -ssociatiity
1 AhighestB .arenthetical grou.ing & ' <A

arra# su7scri.t
$unction call and constructor structure
$ield or method selector, swi??ler
.ost $i2 increment and decrement
3 4
& '
.
-- ((
0e$t to Right
&
.re$i2 increment and decrement
unar#
-- ((
- ( : <
Right to 0e$t
) multi.licative / . 0 0e$t to Right
, additive - ( 0e$t to Right
( 7it;wise shi$t 11 22 0e$t to Right
1 relational 1 2 1; 2; 0e$t to Right
4 e=ualit# ;; <; 0e$t to Right
5 7it;wise and 9 0e$t to Right
18 7it;wise e2clusive or 7 0e$t to Right
11 7it;wise inclusive or 8 0e$t to Right
1 logical and 99 0e$t to Right
1& logical e2clusive or 77 0e$t to Right
1) logical inclusive or 8 8 0e$t to Right
1, selection ? = Right to 0e$t
1(
Assignment
arithmetic assignments
;
-; (;
/; .;
0; 11; 22;
9; 7; 8;
Right to 0e$t
11 AlowestB se=uence # 0e$t to Right
-here is no address;o$ o.erator nor a dere$erence o.erator. -here is no t#.ecast o.eratorC constructors
are used instead.
39
5 Operators and !pressions
&.2 -rray Operations
-hese are now descri7ed in "ection ,.1 P"tructure and Arra# '.erationsQ.
&.! Function Calls
I$ a $unction returns a value, then a call to that $unction ma# 7e used as an e2.ression, whose t#.e will 7e
the t#.e that was used to declare or de$ine the $unction.
%unction de$initions and calling conventions are discussed in "ection (.1 P%unction De$initionsQ .
&.# Constructors
!onstructors use the $unction call s#nta2, where the $unction name is a t#.e, and the call makes an o7Mect
o$ that t#.e. !onstructors are used the same wa# in 7oth initiali?ers and e2.ressions. A"ee "ection 5
P"hading 0anguage /rammarQ $or details.B -he .arameters are used to initiali?e the constructed value.
!onstructors can 7e used to re=uest a data t#.e conversion to change $rom one scalar t#.e to another
scalar t#.e, or to 7uild larger t#.es out o$ smaller t#.es, or to reduce a larger t#.e to a smaller t#.e.
In general, constructors are not 7uilt;in $unctions with .redetermined .rotot#.es. %or arra#s and
structures, there must 7e e2actl# one argument in the constructor $or each element or $ield. %or the other
t#.es, the arguments must .rovide a su$$icient num7er o$ com.onents to .er$orm the initiali?ation, and it
is an error to include so man# arguments that the# cannot all 7e used. Detailed rules $ollow. -he
.rotot#.es actuall# listed 7elow are merel# a su7set o$ e2am.les.
&.#.1 Conersion and Scalar Constructors
!onverting 7etween scalar t#.es is done as the $ollowing .rotot#.es indicate:
int#$ool) (( &onverts a +oolean value to an int
int#float) (( &onverts a float value to an int
float#$ool) (( &onverts a +oolean value to a float
float#int) (( &onverts a signed integer value to a float
$ool#float) (( &onverts a float value to a +oolean
$ool#int) (( &onverts a signed integer value to a +oolean
uint#$ool) (( &onverts a +oolean value to an unsigned integer
uint#float) (( &onverts a float value to an unsigned integer
uint#int) (( &onverts a signed integer value to an unsigned integer
int#uint) (( &onverts an unsigned integer to a signed integer
$ool#uint) (( &onverts an unsigned integer value to a +oolean value
float#uint) (( &onverts an unsigned integer value to a float value
Rhen constructors are used to convert a float to an int or uint, the $ractional .art o$ the $loating;.oint
value is dro..ed. It is unde$ined to convert a negative $loating .oint value to an uint.
Rhen a constructor is used to convert an int, uint, or a float to a bool, 8 and 8.8 are converted to false,
and non;?ero values are converted to true. Rhen a constructor is used to convert a bool to an int, uint,
or float, false is converted to 8 or 8.8, and true is converted to 1 or 1.8.
40
5 Operators and !pressions
-he constructor int&uint' .reserves the 7it .attern in the argument, which will change the argumentOs
value i$ its sign 7it is set. -he constructor uint&int' .reserves the 7it .attern in the argument, which will
change its value i$ it is negative.
Identit# constructors, like $loatA$loatB are also legal, 7ut o$ little use.
"calar constructors with non;scalar .arameters can 7e used to take the $irst element $rom a non;scalar.
%or e2am.le, the constructor $loatAvec&B will select the $irst com.onent o$ the vec& .arameter.
&.#.2 )ector and 4atri* Constructors
!onstructors can 7e used to create vectors or matrices $rom a set o$ scalars, vectors, or matrices. -his
includes the a7ilit# to shorten vectors.
I$ there is a single scalar .arameter to a vector constructor, it is used to initiali?e all com.onents o$ the
constructed vector to that scalarDs value. I$ there is a single scalar .arameter to a matri2 constructor, it is
used to initiali?e all the com.onents on the matri2Ds diagonal, with the remaining com.onents initiali?ed
to 8.8.
I$ a vector is constructed $rom multi.le scalars, one or more vectors, or one or more matrices, or a mi2ture
o$ these, the vectorsO com.onents will 7e constructed in order $rom the com.onents o$ the arguments. -he
arguments will 7e consumed le$t to right, and each argument will have all itOs com.onents consumed, in
order, 7e$ore an# com.onents $rom the ne2t argument are consumed. "imilarl# $or constructing a matri2
$rom multi.le scalars or vectors, or a mi2ture o$ these. 9atri2 com.onents will 7e constructed and
consumed in column maMor order. In these cases, there must 7e enough com.onents .rovided in the
arguments to .rovide an initiali?er $or ever# com.onent in the constructed value. It is an error to .rovide
e2tra arguments 7e#ond this last used argument.
I$ a matri2 is constructed $rom a matri2, then each com.onent Acolumn i+ row 9B in the result that has a
corres.onding com.onent Acolumn i+ row 9B in the argument will 7e initiali?ed $rom there. All other
com.onents will 7e initiali?ed to the identit# matri2. I$ a matri2 argument is given to a matri2 constructor,
it is an error to have an# other arguments.
I$ the 7asic t#.e Abool# int# or floatB o$ a .arameter to a constructor does not match the 7asic t#.e o$ the
o7Mect 7eing constructed, the scalar construction rules Aa7oveB are used to convert the .arameters.
41
5 Operators and !pressions
"ome use$ul vector constructors are as $ollows:
ve&3#float) (( initiali"es ea&4 &omponent of :it4 t4e float
ve&/#ive&/) (( ma2es a ve&/ :it4 &omponentE:ise &onversion
ve&/#mat0) (( t4e ve&/ is &olumn A follo:ed $1 &olumn 5
ve&0#float- float) (( initiali"es a ve&0 :it4 0 floats
ive&3#int- int- int) (( initiali"es an ive&3 :it4 3 ints
$ve&/#int- int- float- float) (( uses / +oolean &onversions
ve&0#ve&3) (( drops t4e t4ird &omponent of a ve&3
ve&3#ve&/) (( drops t4e fourt4 &omponent of a ve&/
ve&3#ve&0- float) (( ve&36x , ve&06x- ve&361 , ve&061- ve&36" , float
ve&3#float- ve&0) (( ve&36x , float- ve&361 , ve&06x- ve&36" , ve&061
ve&/#ve&3- float)
ve&/#float- ve&3)
ve&/#ve&0- ve&0)
"ome e2am.les o$ these are:
ve&/ &olor , ve&/#A6A- 56A- A6A- 56A)'
ve&/ rg$a , ve&/#56A)' (( sets ea&4 &omponent to 56A
ve&3 rg$ , ve&3#&olor)' (( drop t4e /t4 &omponent
-o initiali?e the diagonal o$ a matri2 with all other elements set to ?ero:
mat0#float)
mat3#float)
mat/#float)
-hat is, result"i#"9# is set to the $loat argument $or all i : 9 and set to 8 $or all i 9.
42
5 Operators and !pressions
-o initiali?e a matri2 7# s.eci$#ing vectors or scalars, the com.onents are assigned to the matri2 elements
in column;maMor order.
mat0#ve&0- ve&0)' (( one &olumn per argument
mat3#ve&3- ve&3- ve&3)' (( one &olumn per argument
mat/#ve&/- ve&/- ve&/- ve&/)' (( one &olumn per argument
mat3x0#ve&0- ve&0- ve&0)' (( one &olumn per argument
mat0#float- float- (( first &olumn
float- float)' (( se&ond &olumn
mat3#float- float- float- (( first &olumn
float- float- float- (( se&ond &olumn
float- float- float)' (( t4ird &olumn
mat/#float- float- float- float- (( first &olumn
float- float- float- float- (( se&ond &olumn
float- float- float- float- (( t4ird &olumn
float- float- float- float)' (( fourt4 &olumn
mat0x3#ve&0- float- (( first &olumn
ve&0- float)' (( se&ond &olumn
A wide range o$ other .ossi7ilities e2ist, to construct a matri2 $rom vectors and scalars, as long as enough
com.onents are .resent to initiali?e the matri2. -o construct a matri2 $rom a matri2:
mat3x3#mat/x/)' (( ta2es t4e upperEleft 3x3 of t4e mat/x/
mat0x3#mat/x0)' (( ta2es t4e upperEleft 0x0 of t4e mat/x/- last ro: is A-A
mat/x/#mat3x3)' (( puts t4e mat3x3 in t4e upperEleft- sets t4e lo:er rig4t
(( &omponent to 5- and t4e rest to A
&.#.! Structure Constructors
'nce a structure is de$ined, and its t#.e is given a name, a constructor is availa7le with the same name to
construct instances o$ that structure. %or e2am.le:
stru&t lig4t ;
float intensit1'
ve&3 position'
<'
lig4t lig4tVar , lig4t#36A- ve&3#56A- 06A- 36A))'
-he arguments to the constructor will 7e used to set the structureOs $ields, in order, using one argument .er
$ield. *ach argument must 7e the same t#.e as the $ield it sets, or 7e a t#.e that can 7e converted to the
$ieldOs t#.e according to "ection ).1.18 PIm.licit !onversions.Q
"tructure constructors can 7e used as initiali?ers or in e2.ressions.
43
5 Operators and !pressions
&.#.# -rray Constructors
Arra# t#.es can also 7e used as constructor names, which can then 7e used in e2.ressions or initiali?ers.
%or e2am.le,
&onst float &>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 e2.licitl# si?ed to the num7er o$ arguments .rovided. -he
arguments 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.18 PIm.licit !onversions.Q
&.& )ector Components
-he names o$ the com.onents o$ a vector are denoted 7# a single letter. As a notational convenience,
several letters are associated with each com.onent 7ased on common usage o$ .osition, color or te2ture
coordinate vectors. -he individual com.onents o$ a vector can 7e selected 7# $ollowing the varia7le
name with .eriod A . B and then the com.onent name.
-he com.onent names su..orted are:
;x+ .+ 3+ 2< 6se$ul when accessing vectors that re.resent .oints or normals
;r+ g+ ,+ a< 6se$ul when accessing vectors that re.resent colors
;s+ t+ !+ 4< 6se$ul when accessing vectors that re.resent te2ture coordinates
-he com.onent names x+ r+ and s are, $or e2am.le, s#non#ms $or the same A$irstB com.onent in a vector.
<ote that the third com.onent o$ the te2ture coordinate set, r in '.en/0, has 7een renamed ! so as to
avoid the con$usion with r A$or redB in a color.
Accessing com.onents 7e#ond those declared $or the vector t#.e is an error so, $or e2am.le:
ve&0 pos'
pos6x (( is legal
pos6" (( is illegal
44
5 Operators and !pressions
-he com.onent selection s#nta2 allows multi.le com.onents to 7e selected 7# a..ending their names
A$rom the same name setB a$ter the .eriod A . B.
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 E t4e &omponent names do not &ome from
(( t4e same set6
-he order o$ the com.onents can 7e di$$erent to swi??le them, or re.licated:
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 a..lied to an#
e2.ression that results in a vector r;value.
-he com.onent grou. notation can occur on the le$t hand side o$ an e2.ression.
ve&/ pos , ve&/#56A- 06A- 36A- /6A)'
pos6x: , ve&0#76A- H6A)' (( pos , #76A- 06A- 36A- H6A)
pos6:x , ve&0#D6A- I6A)' (( pos , #I6A- 06A- 36A- D6A)
pos6xx , ve&0#36A- /6A)' (( illegal E BxB used t:i&e
pos6x1 , ve&3#56A- 06A- 36A)' (( illegal E mismat&4 $et:een ve&0 and ve&3
-o $orm an l;value, swi??ling must 7e a..lied to an l;value o$ vector t#.e, contain no du.licate
com.onents, and it results in an l;value o$ scalar or vector t#.e, de.ending on num7er o$ com.onents
s.eci$ied.
Arra# su7scri.ting s#nta2 can also 7e a..lied 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.3. -his allows varia7le inde2ing into a
vector, as well as a generic wa# o$ accessing com.onents. An# integer e2.ression can 7e used as the
su7scri.t. -he $irst com.onent is at inde2 ?ero. Reading $rom or writing to a vector using a constant
integral e2.ression with a value that is negative or greater than or e=ual to the si?e o$ the vector is illegal.
Rhen inde2ing with non;constant e2.ressions, 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 com.onents o$ a matri2 can 7e accessed using arra# su7scri.ting s#nta2. A..l#ing a single su7scri.t
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 su7scri.t would then
o.erate on the resulting vector, as de$ined earlier $or vectors. +ence, two su7scri.ts select a column and
then a row.
45
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 com.onent outside the 7ounds o$ a matri2 with a non;constant
e2.ression. It is an error to access a matri2 with a constant e2.ression that is outside the 7ounds o$ the
matri2.
&.1 Structure and -rray Operations
-he $ields o$ a structure and the length method o$ an arra# are selected using the .eriod A . B.
In total, onl# the $ollowing o.erators are allowed to o.erate on arra#s and structures as whole entities:
$ield or method selector .
e=ualit# ;; <;
assignment ;
inde2ing Aarra#s onl#B K L
-he e=ualit# o.erators and assignment o.erator are onl# allowed i$ the two o.erands are same si?e and
t#.e. "tructure t#.es must 7e o$ the same declared structure. Both arra# o.erands must 7e e2.licitl#
si?ed. Rhen using the e=ualit# o.erators, two structures are e=ual i$ and onl# i$ all the $ields are
com.onent;wise e=ual, and two arra#s are e=ual i$ and onl# i$ all the elements are element;wise e=ual.
Arra# elements are accessed using the arra# su7scri.t o.erator A 3 4 B. An e2am.le o$ accessing an arra#
element is
diffuseColor G, lig4tIntensit1>3? J NdotL'
Arra# indices start at ?ero. Arra# elements are accessed using an e2.ression whose t#.e is int or uint.
Behavior is unde$ined i$ a shader su7scri.ts an arra# with an inde2 less than 8 or greater than or e=ual to
the si?e the arra# was declared with.
Arra#s can also 7e accessed with the method o.erator A . B and the length method to =uer# the si?e o$ the
arra#:
lig4tIntensit16lengt4#) (( return t4e si"e of t4e arra1
&.2 -ssignments
Assignments o$ values to varia7le names are done with the assignment o.erator A ; B:
lvalueEexpression , rvalueEexpression
46
5 Operators and !pressions
-he lvalue-ex!ression evaluates to an l;value. -he assignment o.erator stores the value o$ rvalue-
ex!ression into the l;value and returns an r;value with the t#.e and .recision o$ lvalue-ex!ression. -he
lvalue-ex!ression and rvalue-ex!ression must have the same t#.e, or the e2.ression must have a t#.e in
the ta7le in "ection ).1.18 PIm.licit !onversionsQ that converts to the t#.e o$ lvalue-ex!ression, in which
case an im.licit conversion will 7e done on the rvalue-ex!ression 7e$ore the assignment is done. An#
other desired t#.e;conversions must 7e s.eci$ied e2.licitl# 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
A . B a..lied to select com.onents or swi??les without re.eated $ields, l;values within .arentheses, and l;
values dere$erenced with the arra# su7scri.t o.erator A 3 4 B are all l;values. 'ther 7inar# or unar#
e2.ressions, $unction names, swi??les with re.eated $ields, and constants cannot 7e l;values. -he ternar#
o.erator A?=B is also not allowed as an l;value.
*2.ressions on the le$t o$ an assignment are evaluated 7e$ore e2.ressions on the right o$ the assignment.
-he other assignment o.erators are
add into A-;B
su7tract $rom A(;B
multi.l# into A/;B
divide into A.;B
modulus into A0;B
le$t shi$t 7# A11;B
right shi$t 7# A22;B
and into A9;B
inclusive;or into A8;B
e2clusive;or into A7;B
where the general e2.ression
lvalue op, expression
is e=uivalent to
lvalue , lvalue op expression
where o! is as descri7ed 7elow, and the l;value and e2.ression must satis$# the semantic re=uirements o$
7oth o! and e=uals A;B.
Reading a varia7le 7e$ore writing Aor initiali?ingB it is legal, however the value is unde$ined.
&.5 $*pressions
*2.ressions 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
5 Operators and !pressions
!onstructors o$ all t#.es.
Varia7le names o$ all t#.es.
An arra# name with the length method a..lied.
"u7scri.ted arra# names.
%unction calls that return values.
!om.onent $ield selectors and arra# su7scri.t results.
3arenthesi?ed e2.ression. An# e2.ression can 7e .arenthesi?ed. 3arentheses can 7e used to grou.
o.erations. '.erations within .arentheses are done 7e$ore o.erations across .arentheses.
-he arithmetic 7inar# o.erators add A-B, su7tract A(B, multi.l# A/B, and divide A.B o.erate on integer and
$loating;.oint scalars, vectors, and matrices. I$ one o.erand is $loating;.oint 7ased and the other is
not, then the conversions $rom "ection ).1.18 PIm.licit !onversionsQ are a..lied to the non;$loating;
.oint;7ased o.erand. I$ the o.erands are integer t#.es, the# must 7oth 7e signed or 7oth 7e unsigned.
All arithmetic 7inar# o.erators result in the same $undamental t#.e Asigned integer, unsigned integer,
or $loating;.ointB as the o.erands the# o.erate on, a$ter o.erand t#.e conversion. A$ter conversion,
the $ollowing cases are valid
-he two o.erands are scalars. In this case the o.eration is a..lied, resulting in a scalar.
'ne o.erand is a scalar, and the other is a vector or matri2. In this case, the scalar o.eration is
a..lied inde.endentl# to each com.onent o$ the vector or matri2, resulting in the same si?e vector
or matri2.
-he two o.erands are vectors o$ the same si?e. In this case, the o.eration is done com.onent;wise
resulting in the same si?e vector.
-he o.erator is add A-B, su7tract A(B, or divide A.B, and the o.erands are matrices with the same
num7er o$ rows and the same num7er o$ columns. In this case, the o.eration is done com.onent;
wise resulting in the same si?e matri2.
-he o.erator is multi.l# A/B, where 7oth o.erands are matrices or one o.erand is a vector and the
other a matri2. A right vector o.erand is treated as a column vector and a le$t vector o.erand as a
row vector. In all these cases, it is re=uired that the num7er o$ columns o$ the le$t o.erand is e=ual
to the num7er o$ rows o$ the right o.erand. -hen, the multi.l# A/B o.eration does a linear
alge7raic multi.l#, #ielding an o7Mect that has the same num7er o$ rows as the le$t o.erand and the
same num7er o$ columns as the right o.erand. "ection ,.18 PVector and 9atri2 '.erationsQ
e2.lains in more detail how vectors and matrices are o.erated on.
All other cases are illegal.
Dividing 7# ?ero does not cause an e2ce.tion 7ut does result in an uns.eci$ied value. 6se the 7uilt;in
$unctions dot# cross# matrix!ompOult# and outerQroduct, to get, res.ectivel#, vector dot .roduct,
vector cross .roduct, matri2 com.onent;wise multi.lication, and the matri2 .roduct o$ a column
vector times a row vector.
48
5 Operators and !pressions
-he o.erator modulus A0B o.erates on signed or unsigned integers or integer vectors. -he o.erand
t#.es must 7oth 7e signed or 7oth 7e unsigned. -he o.erands cannot 7e vectors o$ di$$ering si?e. I$
one o.erand is a scalar and the other vector, then the scalar is a..lied com.onent;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 com.uted
com.onent;wise. -he resulting value is unde$ined $or an# com.onent com.uted with a second
o.erand that is ?ero, while results $or other com.onents with non;?ero second o.erands remain
de$ined. I$ 7oth o.erands are non;negative, then the remainder is non;negative. Results are unde$ined
i$ one or 7oth o.erands are negative. -he o.erator modulus A0B is not de$ined $or an# other data
t#.es Anon;integer t#.esB.
-he arithmetic unar# o.erators negate A;B, .ost; and .re;increment and decrement A(( and --B o.erate
on integer or $loating;.oint values Aincluding vectors and matricesB. All unar# o.erators work
com.onent;wise on their o.erands. -hese result with the same t#.e the# o.erated on. %or .ost; and
.re;increment and decrement, the e2.ression must 7e one that could 7e assigned to Aan l;valueB. 3re;
increment and .re;decrement add or su7tract 1 or 1.8 to the contents o$ the e2.ression the# o.erate on,
and the value o$ the .re;increment or .re;decrement e2.ression is the resulting value o$ that
modi$ication. 3ost;increment and .ost;decrement e2.ressions add or su7tract 1 or 1.8 to the contents
o$ the e2.ression the# o.erate on, 7ut the resulting e2.ression has the e2.ressionDs value 7e$ore the
.ost;increment or .ost;decrement was e2ecuted.
-he relational o.erators greater than A2B, less than A1B, greater than or e=ual A2;B, and less than or
e=ual A1;B o.erate onl# on scalar integer and scalar $loating;.oint e2.ressions. -he result is scalar
Boolean. *ither the o.erandsD t#.es must match, or the conversions $rom "ection ).1.18 PIm.licit
!onversionsQ will 7e a..lied to the integer o.erand, a$ter which the t#.es must match. -o do
com.onent;wise relational com.arisons on vectors, use the 7uilt;in $unctions lessAhan#
lessAhanEIual# greaterAhan# and greaterAhanEIual.
-he e=ualit# o.erators eIual &;;B, and not e=ual A<;B o.erate on all t#.es. -he# result in a scalar
Boolean. I$ the o.erand t#.es do not match, then there must 7e a conversion $rom "ection ).1.18
PIm.licit !onversionsQ a..lied to one o.erand that can make them match, in which case this
conversion is done. %or vectors, matrices, structures, and arra#s, all com.onents, $ields, or elements o$
one o.erand must e=ual the corres.onding com.onents, $ields, or elements in the other o.erand $or the
o.erands to 7e considered e=ual. -o get a vector o$ com.onent;wise e=ualit# results $or vectors, use
the 7uilt;in $unctions eIual and notEIual.
-he logical 7inar# o.erators and A99B, or A 8 8 B, and e2clusive or A77B o.erate onl# on two Boolean
e2.ressions and result in a Boolean e2.ression. And A99B will onl# evaluate the right hand o.erand
i$ the le$t hand o.erand evaluated to true. 'r A 8 8 B will onl# evaluate the right hand o.erand i$ the le$t
hand o.erand evaluated to false. *2clusive or A77B will alwa#s evaluate 7oth o.erands.
-he logical unar# o.erator not A<B. It o.erates onl# on a Boolean e2.ression and results in a Boolean
e2.ression. -o o.erate on a vector, use the 7uilt;in $unction not.
-he se=uence A # B o.erator that o.erates on e2.ressions 7# returning the t#.e and value o$ the right;
most e2.ression in a comma se.arated list o$ e2.ressions. All e2.ressions are evaluated, in order,
$rom le$t to right.
49
5 Operators and !pressions
-he ternar# selection o.erator A?=B. It o.erates on three e2.ressions Aex!1 ? ex!2 = ex!3B. -his
o.erator evaluates the $irst e2.ression, which must result in a scalar Boolean. I$ the result is true, it
selects to evaluate the second e2.ression, otherwise it selects to evaluate the third e2.ression. 'nl#
one o$ the second and third e2.ressions is evaluated. -he second and third e2.ressions can 7e an#
t#.e, as long their t#.es match, or there is a conversion in "ection ).1.18 PIm.licit !onversionsQ that
can 7e a..lied to one o$ the e2.ressions to make their t#.es match. -his resulting matching t#.e is the
t#.e o$ the entire e2.ression.
-he oneOs com.lement o.erator A:B. -he o.erand must 7e o$ t#.e signed or unsigned integer or integer
vector, and the result is the oneOs com.lement o$ its o.erandC each 7it o$ each com.onent is
com.lemented, including an# sign 7its.
-he shi$t o.erators A11B and A22B. %or 7oth o.erators, the o.erands must 7e signed or unsigned
integers or integer vectors. 'ne o.erand 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 o.erand. I$ the $irst o.erand is a scalar, the second
o.erand has to 7e a scalar as well. I$ the $irst o.erand is a vector, the second o.erand must 7e a scalar
or a vector, and the result is com.uted com.onent;wise. -he result is unde$ined i$ the right o.erand is
negative, or greater than or e=ual to the num7er o$ 7its in the le$t e2.ressionOs 7ase t#.e. -he value o$
*1 FF * is *1 Ainter.reted as a 7it .atternB 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 o.erators and A9B, e2clusive;or A7B, and inclusive;or A8B. -he o.erands must 7e o$ t#.e
signed or unsigned integers or integer vectors. -he o.erands cannot 7e vectors o$ di$$ering si?e. I$ one
o.erand is a scalar and the other a vector, the scalar is a..lied com.onent;wise to the vector, resulting
in the same t#.e as the vector. -he $undamental t#.es o$ the o.erands Asigned or unsignedB must
match, and will 7e the resulting $undamental t#.e. %or and A9B, the result is the 7itwise;and $unction
o$ the o.erands. %or e2clusive;or A7B, the result is the 7itwise e2clusive;or $unction o$ the o.erands.
%or inclusive;or A8B, the result is the 7itwise inclusive;or $unction o$ the o.erands.
%or a com.lete s.eci$ication o$ the s#nta2 o$ e2.ressions, see "ection 5 P"hading 0anguage /rammar.Q
&.16 )ector and 4atri* Operations
Rith a $ew e2ce.tions, o.erations are com.onent;wise. 6suall#, when an o.erator o.erates on a vector or
matri2, it is o.erating inde.endentl# on each com.onent o$ the vector or matri2, in a com.onent;wise
$ashion. %or e2am.le,
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
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 o.erators and all integer and $loating .oint vector and matri2 t#.es. -he e2ce.tions
are matri2 multi.lied 7# vector, vector multi.lied 7# matri2, and matri2 multi.lied 7# matri2. -hese do
not o.erate com.onent;wise, 7ut rather .er$orm the correct linear alge7raic multi.l#.
ve&3 v- u'
mat3 m'
u , v J m'
is e=uivalent to
u6x , dot#v- m>A?)' (( m>A? is t4e left &olumn of m
u61 , dot#v- m>5?)' (( dot#a-$) is t4e inner #dot) produ&t of a and $
u6" , dot#v- 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
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
' Statements and Structure
-he $undamental 7uilding 7locks o$ the '.en/0 "hading 0anguage are:
statements and declarations
$unction de$initions
selection Aif(else and switch(case(default'
iteration &for# while# and do(while'
Mum.s &discard# return# break# and continueB
-he overall structure o$ a shader is as $ollows
translation-unit:
glo,al-%eclaration
translation-unit glo,al-%eclaration
glo,al-%eclaration:
-unction-%e-inition
%eclaration
-hat is, a shader is a se=uence o$ declarations and $unction 7odies. %unction 7odies are de$ined as
-unction-%e-inition:
-unction-!rotot.!e ; statement-list <
statement-list:
statement
statement-list statement
statement:
com!oun%-statement
sim!le-statement
!url# 7races are used to grou. se=uences o$ statements into com.ound statements.
com!oun%-statement:
; statement-list <
sim!le-statement:
%eclaration-statement
ex!ression-statement
selection-statement
53
6 "tate#ents and "tructure
iteration-statement
9um!-statement
"im.le declaration, e2.ression, and Mum. statements end in a semi;colon.
-his a7ove is slightl# sim.li$ied, and the com.lete grammar s.eci$ied in "ection 5 P"hading 0anguage
/rammarQ should 7e used as the de$initive s.eci$ication.
Declarations and e2.ressions have alread# 7een discussed.
'.1 Function (efinitions
As indicated 7# the grammar a7ove, a valid shader is a se=uence o$ glo7al declarations and $unction
de$initions. A $unction is declared as the $ollowing e2am.le shows:
(( protot1pe
returnT1pe fun&tionName #t1peA argA- t1pe5 arg5- 666- t1pen argn)'
and a $unction is de$ined like
(( definition
returnT1pe fun&tionName #t1peA argA- t1pe5 arg5- 666- t1pen argn)
;
(( do some &omputation
return returnValue'
<
where return&.!e must 7e .resent and include a t#.e. *ach o$ the t.!eN must include a t#.e and can
o.tionall# include a .arameter =uali$ier and@or const.
A $unction is called 7# using its name $ollowed 7# a list o$ arguments in .arentheses.
Arra#s are allowed as arguments and as the return t#.e. In 7oth cases, the arra# must 7e e2.licitl# si?ed.
An arra# is .assed or returned 7# using Must its name, without 7rackets, and the si?e o$ the arra# must
match the si?e s.eci$ied in the $unctionOs declaration.
"tructures are also allowed as argument t#.es. -he return t#.e can also 7e structure.
"ee "ection 5 P"hading 0anguage /rammarQ $or the de$initive re$erence on the s#nta2 to declare and
de$ine $unctions.
All $unctions must 7e either declared with a .rotot#.e or de$ined with a 7od# 7e$ore the# are called. %or
e2am.le:
float m1fun& #float f- (( f is an input parameter
out float g)' (( g is an output parameter
%unctions that return no value must 7e declared as void. %unctions that acce.t no in.ut arguments need
not use void in the argument list 7ecause .rotot#.es Aor de$initionsB are re=uired and there$ore there is no
am7iguit# when an em.t# argument list XA BX is declared. -he idiom PAvoidBQ as a .arameter list is
.rovided $or convenience.
54
6 "tate#ents and "tructure
%unction names can 7e overloaded. -he same $unction name can 7e used $or multi.le $unctions, as long
as the .arameter t#.es di$$er. I$ a $unction name is declared twice with the same .arameter t#.es, then the
return t#.es and all =uali$iers must also match, and it is the same $unction 7eing declared. Rhen $unction
calls are resolved, an e2act t#.e match $or all the arguments is sought. I$ an e2act match is $ound, all
other $unctions are ignored, and the e2act match is used. I$ no e2act match is $ound, then the im.licit
conversions in "ection ).1.18 PIm.licit !onversionsQ will 7e a..lied to $ind a match. 9ismatched t#.es
on in.ut .arameters Ain or inout or de$ault' must have a conversion $rom the calling argument t#.e to the
$ormal .arameter t#.e. 9ismatched t#.es on out.ut .arameters Aout or inoutB must have a conversion
$rom the $ormal .arameter t#.e to the calling argument t#.e. Rhen argument conversions are used to $ind
a match, it is a semantic error i$ there are multi.le wa#s to a..l# these conversions to make the call match
more than one $unction.
%or e2am.le,
ve&/ f#in ve&/ x- out ve&/ 1)'
ve&/ f#in ve&/ x- out ive&/ 1)' (( o2a1- different argument t1pe
int f#in ve&/ x- out ive&/ 1)' (( error- onl1 return t1pe differs
ve&/ f#in ve&/ x- in ive&/ 1)' (( error- onl1 =ualifier differs
int f#&onst in ve&/ x- out ive&/ 1)' (( error- onl1 =ualifier differs
!alling the $irst two $unctions a7ove with the $ollowing argument t#.es #ields
f#ve&/- ve&/) (( exa&t mat&4 of ve&/ f#in ve&/ x- out ve&/ 1)
f#ve&/- ive&/) (( exa&t mat&4 of ve&/ f#in ve&/ x- out ive&/ 1)
f#ive&/- ve&/) (( error- &onverti$le to $ot4
f#ive&/- ive&/) (( o2a1- &onverti$le onl1 to ve&/ f#in ve&/ x- out ive&/ 1)
6ser;de$ined $unctions can have multi.le declarations, 7ut onl# one de$inition. A shader can rede$ine
7uilt;in $unctions. I$ a 7uilt;in $unction is redeclared in a shader Ai.e. a .rotot#.e is visi7leB 7e$ore a call
to it, then the linker will onl# attem.t to resolve that call within the set o$ shaders that are linked with it.
-he $unction main is used as the entr# .oint to a shader e2ecuta7le. A shader need not contain a $unction
named main, 7ut one shader in a set o$ shaders linked together to $orm a single shader e2ecuta7le must.
-his $unction takes no arguments, returns no value, and must 7e declared as t#.e void=
void main#)
;
666
<
-he $unction main can contain uses o$ return. "ee "ection (.) PJum.sQ $or more details.
It is an error to declare or de$ine a $unction main with an# other .arameters or return t#.e.
'.1.1 Function Calling Conentions
%unctions are called 7# value;return. -his means in.ut arguments are co.ied into the $unction at call time,
and out.ut arguments are co.ied 7ack to the caller 7e$ore $unction e2it. Because the $unction works with
local co.ies o$ .arameters, there are no issues regarding aliasing o$ varia7les within a $unction. -o
control what .arameters are co.ied in and@or out through a $unction de$inition or declaration:
-he ke#word in is used as a =uali$ier to denote a .arameter is to 7e co.ied in, 7ut not co.ied out.
55
6 "tate#ents and "tructure
-he ke#word out is used as a =uali$ier to denote a .arameter is to 7e co.ied out, 7ut not co.ied in.
-his should 7e used whenever .ossi7le to avoid unnecessaril# co.#ing .arameters in.
-he ke#word inout is used as a =uali$ier to denote the .arameter is to 7e 7oth co.ied in and co.ied
out.
A $unction .arameter declared with no such =uali$ier means the same thing as s.eci$#ing in.
All arguments are evaluated at call time, e2actl# once, in order, $rom le$t to right. *valuation o$ an in
.arameter results in a value that is co.ied to the $ormal .arameter. *valuation o$ an out .arameter results
in an l;value that is used to co.# out a value when the $unction returns. *valuation o$ an inout .arameter
results in 7oth a value and an l;valueC the value is co.ied to the $ormal .arameter at call time and the l;
value is used to co.# out a value when the $unction returns.
-he order in which out.ut .arameters are co.ied 7ack to the caller is unde$ined.
I$ the $unction matching descri7ed in the .revious section re=uired argument t#.e conversions, these
conversions are a..lied at co.#;in and co.#;out times.
In a $unction, writing to an in.ut;onl# .arameter is allowed. 'nl# the $unctionDs co.# is modi$ied. -his
can 7e .revented 7# declaring a .arameter with the const =uali$ier.
Rhen calling a $unction, e2.ressions that do not evaluate to l;values cannot 7e .assed to .arameters
declared as out or inout.
<o =uali$ier is allowed on the return t#.e o$ a $unction.
-unction-!rotot.!e :
!recision-4uali-ier t.!e -unction-name6const-4uali-ier !arameter-4uali-ier !recision-4uali-ier
t.!e name arra.-s!eci-ier+ ... 7
t.!e :
an# 7asic t#.e, arra# t#.e, structure name, or structure %e-inition
const-4uali-ier :
em.t#
const
!arameter-4uali-ier :
em.t#
in
out
inout
name :
em.t#
identi$ier
arra.-s!eci-ier :
em.t#
3 integral-constant-ex!ression 4
56
6 "tate#ents and "tructure
+owever, the const =uali$ier cannot 7e used with out or inout. -he a7ove is used $or $unction
declarations Ai.e. .rotot#.esB and $or $unction de$initions. +ence, $unction de$initions can have unnamed
arguments.
Recursion is not allowed, not even staticall#. "tatic recursion is .resent i$ the static $unction call gra.h o$
the .rogram contains c#cles.
'.2 Selection
!onditional control $low in the shading language is done 7# either if, if;else, or switch statements:
selection-statement :
if A ,ool-ex!ression B statement
if A ,ool-ex!ression B statement else statement
switch A init-ex!ression B [ s2itc'-statement-listo!t

\
Rhere s2itc'-statement-list is a list o$ ?ero or more s2itc'-statement and other statements de$ined 7# the
language, where s2itc'-statement adds some $orms o$ la7els. -hat is
s2itc'-statement-list :
s2itc'-statement
s2itc'-statement-list s2itc'-statement
s2itc'-statement :
case constant-ex!ression =
default =
statement
I$ an if(e2.ression evaluates to true, then the $irst statement is e2ecuted. I$ it evaluates to false and there
is an else .art then the second statement is e2ecuted.
An# e2.ression whose t#.e evaluates to a Boolean can 7e used as the conditional e2.ression ,ool-
ex!ression. Vector t#.es are not acce.ted as the e2.ression to if.
!onditionals can 7e nested.
-he t#.e o$ init-ex!ression in a switch statement must 7e a scalar integer. I$ a case la7el has a constant-
ex!ression o$ e=ual value, then e2ecution will continue a$ter that la7el. 'therwise, i$ there is a default
la7el, e2ecution will continue a$ter that la7el. 'therwise, e2ecution ski.s the rest o$ the switch statement.
It is an error to have more than one default or a re.licated constant-ex!ression. A break statement not
nested in a loo. or other switch statement Aeither not nested or nested onl# in if or if;else statementsB will
also ski. the rest o$ the switch statement. %all through la7els are allowed, 7ut it is an error to have no
statement 7etween a la7el and the end o$ the switch statement.
<o case or default la7els can 7e nested inside other $low control nested within their corres.onding
switch.
'.! Iteration
%or, while, and do loo.s are allowed as $ollows:
57
6 "tate#ents and "tructure
for #initEexpression' &onditionEexpression' loopEexpression)
su$Estatement
:4ile #&onditionEexpression)
su$Estatement
do
statement
:4ile #&onditionEexpression)
"ee "ection 5 P"hading 0anguage /rammarQ $or the de$initive s.eci$ication o$ loo.s.
-he for loo. $irst evaluates the init-ex!ression, then the con%ition-ex!ression. I$ the con%ition-
ex!ression evaluates to true, then the 7od# o$ the loo. is e2ecuted. A$ter the 7od# is e2ecuted, a for loo.
will then evaluate the loo!-ex!ression, and then loo. 7ack to evaluate the con%ition-ex!ression, re.eating
until the con%ition-ex!ression evaluates to $alse. -he loo. is then e2ited, ski..ing its 7od# and ski..ing
its loo!-ex!ression. Varia7les modi$ied 7# the loo!-ex!ression maintain their value a$ter the loo. is
e2ited, .rovided the# are still in sco.e. Varia7les declared in init-ex!ression or con%ition-ex!ression are
onl# in sco.e until the end o$ the su7;statement o$ the for loo..
-he while loo. $irst evaluates the con%ition-ex!ression. I$ true, then the 7od# is e2ecuted. -his is then
re.eated, until the con%ition-ex!ression evaluates to $alse, e2iting the loo. and ski..ing its 7od#.
Varia7les declared in the con%ition-ex!ression are onl# in sco.e until the end o$ the su7;statement o$ the
while loo..
-he do(while loo. $irst e2ecutes the 7od#, then e2ecutes the con%ition-ex!ression. -his is re.eated until
con%ition-ex!ression evaluates to $alse, and then the loo. is e2ited.
*2.ressions $or con%ition-ex!ression must evaluate to a Boolean.
Both the con%ition-ex!ression and the init-ex!ression can declare and initiali?e a varia7le, e2ce.t in the
do(while loo., which cannot declare a varia7le in its con%ition-ex!ression. -he varia7leDs sco.e lasts
onl# until the end o$ the su7;statement that $orms the 7od# o$ the loo..
0oo.s can 7e nested.
<on;terminating loo.s are allowed. -he conse=uences o$ ver# long or non;terminating loo.s are .lat$orm
de.endent.
'.# 9umps
-hese are the Mum.s:
9um!_statement:
continue>
break>
return>
return ex!ression>
discard> .. in the $ragment shader language onl#
58
6 "tate#ents and "tructure
-here is no PgotoQ nor other non;structured $low o$ control.
-he continue Mum. is used onl# in loo.s. It ski.s the remainder o$ the 7od# o$ the inner most loo. o$
which it is inside. %or while and do(while loo.s, this Mum. is to the ne2t evaluation o$ the loo.
con%ition-ex!ression $rom which the loo. continues as .reviousl# de$ined. %or for loo.s, the Mum. is to
the loo!-ex!ression, $ollowed 7# the con%ition-ex!ression.
-he break Mum. can also 7e used onl# in loo.s and switch statements. It is sim.l# an immediate e2it o$
the inner;most loo. or switch statements containing the break. <o $urther e2ecution o$ con%ition-
ex!ression+ loo!-ex!ression, or s2itc'-statement is done.
-he discard ke#word is onl# allowed within $ragment shaders. It can 7e used within a $ragment shader to
a7andon the o.eration on the current $ragment. -his ke#word causes the $ragment to 7e discarded and no
u.dates to an# 7u$$ers will occur. !ontrol $low e2its the shader, and su7se=uent im.licit or e2.licit
derivatives are unde$ined when this control $low is non;uni$orm Ameaning di$$erent $ragments within the
.rimitive take di$$erent control .athsB. It would t#.icall# 7e used within a conditional statement, $or
e2am.le:
if #intensit1 K A6A)
dis&ard'
A $ragment shader ma# test a $ragmentDs al.ha value and discard the $ragment 7ased on that test.
+owever, it should 7e noted that coverage testing occurs a$ter the $ragment shader runs, and the coverage
test can change the al.ha value.
-he return Mum. causes immediate e2it o$ the current $unction. I$ it has ex!ression then that is the return
value $or the $unction.
-he $unction main can use return. -his sim.l# causes main to e2it in the same wa# as when the end o$
the $unction had 7een reached. It does not im.l# a use o$ discard in a $ragment shader. 6sing return in
main 7e$ore de$ining out.uts will have the same 7ehavior as reaching the end o$ main 7e$ore de$ining
out.uts.
59
1 ,uilt:in )ariables
1.1 )erte* Shader Special )ariables
"ome '.en/0 o.erations occur in $i2ed $unctionalit# 7etween the verte2 .rocessor and the $ragment
.rocessor. "haders communicate with the $i2ed $unctionalit# o$ '.en/0 through the use o$ 7uilt;in
varia7les.
-hese 7uilt;in verte2 shader varia7les $or communicating with $i2ed $unctionalit# are intrinsicall#
declared as $ollows:
out ve&/ gl_@osition' (( must $e :ritten to
out float gl_@ointSi"e' (( ma1 $e :ritten to
in int gl_VertexI'
out float gl_Clipistan&e>?' (( ma1 $e :ritten to
out ve&/ gl_ClipVertex' (( ma1 $e :ritten to- depre&ated
-he varia7le gl_=osition is availa7le onl# in the verte2 language and is intended $or writing the
homogeneous verte2 .osition. All e2ecutions o$ a well;$ormed verte2 shader e2ecuta7le must write a
value into this varia7le. It can 7e written at an# time during shader e2ecution. It ma# also 7e read 7ack
7# a verte2 shader a$ter 7eing written. -his value will 7e used 7# .rimitive assem7l#, cli..ing, culling,
and other $i2ed $unctionalit# o.erations that o.erate on .rimitives a$ter verte2 .rocessing has occurred.
!om.ilers ma# generate a diagnostic message i$ the# detect gl_=osition is not written, or read 7e$ore
7eing written, 7ut not all such cases are detecta7le. Its value is unde$ined i$ the verte2 shader e2ecuta7le
does not write gl_=osition.
-he varia7le gl_=oint*i3e is availa7le onl# in the verte2 language and is intended $or a verte2 shader to
write the si?e o$ the .oint to 7e rasteri?ed. It is measured in .i2els.
-he varia7le gl_VertexID is a verte2 shader an in.ut varia7le that holds an integer inde2 $or the verte2, as
de$ined 7# the '.en/0 /ra.hics "#stem ".eci$ication. Rhile the varia7le gl>Verte2ID is alwa#s
.resent, its value is not alwa#s de$ined. %or details on when it is de$ined, see the X"hader In.utsX
su7section o$ section .8.& X"hader *2ecutionX o$ the '.en/0 /ra.hics "#stem ".eci$ication, Version
&.8.
-he varia7le gl_li!Distance .rovides the $orward com.ati7le mechanism in the verte2 shader $or
controlling user cli..ing. -o use this, a verte2 shader is res.onsi7le $or maintaining a set o$ cli. .lanes,
com.uting the distance $rom the verte2 to each cli. .lane, and storing distances to the .lane in
gl_li!Distance"i# $or each .lane i. A distance o$ 8 means the verte2 is on the .lane, a .ositive distance
means the verte2 is inside the cli. .lane, and a negative distance means the .oint is outside the cli. .lane.
-he cli. distances will 7e linearl# inter.olated across the .rimitive and the .ortion o$ the .rimitive with
inter.olated distances less than 8 will 7e cli..ed.
60
7 Built$in Variables
-he gl_li!Distance arra# is .redeclared as unsi?ed and must 7e si?ed 7# the shader either redeclaring it
with a si?e or inde2ing it onl# with integral constant e2.ressions. -his needs to si?e the arra# to include
all the cli. .lanes that are ena7led via the '.en/0 A3IC i$ the si?e does not include all ena7led .lanes,
results are unde$ined. -he si?e can 7e at most gl_>axli!Distances. -he num7er o$ var#ing com.onents
Asee gl_>axVar.ingom!onents7 consumed 7# gl_li!Distance will match the si?e o$ the arra#, no
matter how man# .lanes are ena7led. -he shader must also set all values in gl_li!Distance that have
7een ena7led via the '.en/0 A3I, or results are unde$ined. Values written into gl_li!Distance $or
.lanes that are not ena7led have no e$$ect.
-he varia7le gl_li!Vertex is de.recated. It is availa7le onl# in the verte2 language and .rovides a .lace
$or verte2 shaders to write the coordinate to 7e used with the user cli..ing .lanes. -he user must ensure
the cli. verte2 and user cli..ing .lanes are de$ined in the same coordinate s.ace. 6ser cli. .lanes work
.ro.erl# onl# under linear trans$orm. It is unde$ined what ha..ens under non;linear trans$orm.
I$ a linked set o$ shaders $orming the verte2 stage contains no static write to gl_li!Vertex or
gl_li!Distance+ 7ut the a..lication has re=uested cli..ing against user cli. .lanes through the A3I, then
the coordinate written to gl_=osition is used $or com.arison against the user cli. .lanes. -his 7ehavior is
also de.recated. Rriting to gl_li!Distance is the .re$erred method $or user cli..ing. It is an error $or a
shader to staticall# write 7oth gl_li!Vertex and gl_li!Distance.
I$ gl_=oint*i3e is not written to, its value is unde$ined in su7se=uent .i.e stages.
1.2 Fragment Shader Special )ariables
-he 7uilt;in s.ecial varia7les that are accessi7le $rom a $ragment shader are intrinsicall# declared as
$ollows:
in ve&/ gl_FragCoord'
in $ool gl_FrontFa&ing'
in float gl_Clipistan&e>?'
out ve&/ gl_FragColor' (( depre&ated
out ve&/ gl_Fragata>gl_9axra:+uffers?' (( depre&ated
out float gl_Fragept4'
*2ce.t as noted 7elow, the# 7ehave as other in.ut and out.ut varia7les.
-he out.ut o$ the $ragment shader e2ecuta7le is .rocessed 7# the $i2ed $unction o.erations at the 7ack end
o$ the '.en/0 .i.eline.
%ragment shaders out.ut values to the '.en/0 .i.eline using the 7uilt;in varia7les gl_$ragolor+
gl_$ragData+ and gl_$ragDe!t', unless the discard statement is e2ecuted. Both gl_$ragolor and
gl_$ragData are de.recatedC the .re$erred usage is to e2.licitl# declare these out.uts in the $ragment
shader using the out storage =uali$ier.
-he $i2ed $unctionalit# com.uted de.th $or a $ragment ma# 7e o7tained 7# reading gl_$ragoor%.3+
descri7ed 7elow.
De.recated: Rriting to gl_$ragolor s.eci$ies the $ragment color that will 7e used 7# the su7se=uent
$i2ed $unctionalit# .i.eline. I$ su7se=uent $i2ed $unctionalit# consumes $ragment color and an e2ecution
o$ the $ragment shader e2ecuta7le does not write a value to gl_$ragolor then the $ragment color
consumed is unde$ined.
61
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.
Rriting to gl_$ragDe!t' will esta7lish the de.th value $or the $ragment 7eing .rocessed. I$ de.th
7u$$ering is ena7led, and no shader writes gl_$ragDe!t', then the $i2ed $unction value $or de.th will 7e
used as the $ragmentDs de.th value. I$ a shader staticall# assigns a value to gl_$ragDe!t', and there is an
e2ecution .ath through the shader that does not set gl_$ragDe!t', then the value o$ the $ragmentDs de.th
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 to gl_$ragDe!t', then it is res.onsi7le $or alwa#s writing it.
De.recated: -he varia7le gl_$ragData is an arra#. Rriting to gl_$ragData"n# s.eci$ies the $ragment
data that will 7e used 7# the su7se=uent $i2ed $unctionalit# .i.eline $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 to gl_$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 to gl_$ragolor. -hat is, a shader ma# assign values to either gl_$ragolor or gl_$ragData, 7ut
not 7oth. 9ulti.le shaders linked together must also consistentl# write Must one o$ these varia7les.
"imilarl#, i$ user declared out.ut varia7les are in use Astaticall# assigned toB, then the 7uilt;in varia7les
gl_$ragolor and gl_$ragData ma# not 7e assigned to. -hese incorrect usages all generate com.ile time
errors.
I$ a shader e2ecutes the discard ke#word, the $ragment is discarded, and the values o$ an# user;de$ined
$ragment out.uts, gl_$ragDe!t', gl_$ragolor, and gl_$ragData 7ecome irrelevant.
-he varia7le gl_$ragoor% is availa7le as an in.ut varia7le $rom within $ragment shaders and it holds the
window relative coordinates 2, #, ?, and 1@w values $or the $ragment. I$ multi;sam.ling, this value can 7e
$or an# location within the .i2el, or one o$ the $ragment sam.les. -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
inter.olates .rimitives a$ter verte2 .rocessing to generate $ragments. -he 3 com.onent is the de.th value
that would 7e used $or the $ragmentDs de.th i$ no shader contained an# writes to gl_$ragDe!t'. -his is
use$ul $or invariance i$ a shader conditionall# com.utes gl_$ragDe!t' 7ut otherwise wants the $i2ed
$unctionalit# $ragment de.th.
%ragment shaders have access to the in.ut 7uilt;in varia7le gl_$ront$acing+ whose value is true i$ 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 in.ut varia7le gl_li!Distance arra# contains linearl# inter.olated values $or the verte2
values written 7# the verte2 shader to the gl_li!Distance verte2 out.ut varia7le. -his arra# must 7e
si?ed in the $ragment shader either im.licitl# or e2.licitl# to 7e the same si?e as it was si?ed in the verte2
shader. 'nl# elements in this arra# that have cli..ing ena7led will have de$ined values.
1.! )erte* Shader ,uilt:In Inputs
De.recated: -he $ollowing .redeclared in.ut names can 7e used $rom within a verte2 shader to access the
current values o$ '.en/0 state.
62
7 Built$in Variables
in ve&/ gl_Color' (( depre&ated
in ve&/ gl_Se&ondar1Color' (( depre&ated
in ve&3 gl_Normal' (( depre&ated
in 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
1.# ,uilt:In Constants
-he $ollowing 7uilt;in constants are .rovided to verte2 and $ragment shaders. -he actual values used are
im.lementation de.endent, 7ut must 7e at least the value shown. "ome are de.recated, as indicated in
comments.
((
(( Implementation dependent &onstants6 T4e example values $elo:
(( are t4e minimum values allo:ed for t4ese maximums6
((
&onst int gl_9axTextureLnits , 5H'
&onst int gl_9axVertex8ttri$s , 5H'
&onst int gl_9axVertexLniformComponents , 5A0/'
&onst int gl_9axVar1ingFloats , H/' (( epre&ated
&onst int gl_9axVar1ingComponents , H/'
&onst int gl_9axVertexTextureImageLnits , 5H'
&onst int gl_9axCom$inedTextureImageLnits , 5H'
&onst int gl_9axTextureImageLnits , 5H'
&onst int gl_9axFragmentLniformComponents , 5A0/'
&onst int gl_9axra:+uffers , I'
&onst int gl_9axClipistan&es , I'
((
(( T4e follo:ing are depre&ated6
((
&onst int gl_9axClip@lanes , I' (( depre&ated
&onst int gl_9axTextureCoords , I' (( depre&ated
-he constant gl_>axVar.ing$loats is de.recated, use gl_>axVar.ingom!onents instead. -he constant
gl_>axli!=lanes is de.recated along with user cli. .lanes, use cli. distances and gl_>axli!Distances
instead. -he constant gl_>ax&extureoor%s is de.recated, use user;de$ined inter.olants instead.
63
7 Built$in Variables
1.& ,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 /ra.hics
"#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 E n
<'
uniform gl_ept4Range@arameters gl_ept4Range'
-he $ollowing state is de.recated:
((
(( epre&ated6
((
uniform mat/ gl_9odelVie:9atrix'
uniform mat/ gl_@ro.e&tion9atrix'
uniform mat/ gl_9odelVie:@ro.e&tion9atrix'
uniform mat/ gl_Texture9atrix>gl_9axTextureCoords?'
((
(( epre&ated6
((
uniform mat3 gl_Normal9atrix' (( transpose of t4e inverse of t4e
(( upper leftmost 3x3 of gl_9odelVie:9atrix
uniform mat/ gl_9odelVie:9atrixInverse'
uniform mat/ gl_@ro.e&tion9atrixInverse'
uniform mat/ gl_9odelVie:@ro.e&tion9atrixInverse'
uniform mat/ gl_Texture9atrixInverse>gl_9axTextureCoords?'
uniform mat/ gl_9odelVie:9atrixTranspose'
uniform mat/ gl_@ro.e&tion9atrixTranspose'
uniform mat/ gl_9odelVie:@ro.e&tion9atrixTranspose'
uniform mat/ gl_Texture9atrixTranspose>gl_9axTextureCoords?'
uniform mat/ gl_9odelVie:9atrixInverseTranspose'
uniform mat/ gl_@ro.e&tion9atrixInverseTranspose'
uniform mat/ gl_9odelVie:@ro.e&tion9atrixInverseTranspose'
uniform mat/ gl_Texture9atrixInverseTranspose>gl_9axTextureCoords?'
64
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'
<'

uniform gl_@oint@arameters gl_@oint'
((
(( epre&ated6
((
stru&t gl_9aterial@arameters ;
ve&/ emission' (( E&m
ve&/ am$ient' (( 8&m
ve&/ diffuse' (( &m
ve&/ spe&ular' (( S&m
float s4ininess' (( Srm
<'
uniform gl_9aterial@arameters gl_Front9aterial'
uniform gl_9aterial@arameters gl_+a&29aterial'
65
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% Fi
ve&3 spotire&tion' (( Sdli
float spotExponent' (( Srli
float spotCutoff' (( Crli
(( #range% >A6A-NA6A?- 5IA6A)
float spotCosCutoff' (( erived% &os#Crli)
(( #range% >56A-A6A?-E56A)
float &onstant8ttenuation' (( OA
float linear8ttenuation' (( O5
float =uadrati&8ttenuation'(( O0
<'
uniform gl_Lig4tSour&e@arameters gl_Lig4tSour&e>gl_9axLig4ts?'
stru&t gl_Lig4t9odel@arameters ;
ve&/ am$ient' (( 8&s
<'
uniform gl_Lig4t9odel@arameters gl_Lig4t9odel'
((
(( epre&ated6
(( erived state from produ&ts of lig4t and material6
((
stru&t gl_Lig4t9odel@rodu&ts ;
ve&/ s&eneColor' (( erived6 E&m G 8&m J 8&s
<'
uniform gl_Lig4t9odel@rodu&ts gl_FrontLig4t9odel@rodu&t'
uniform gl_Lig4t9odel@rodu&ts gl_+a&2Lig4t9odel@rodu&t'
stru&t gl_Lig4t@rodu&ts ;
ve&/ am$ient' (( 8&m J 8&li
ve&/ diffuse' (( &m J &li
ve&/ spe&ular' (( S&m J S&li
<'
uniform gl_Lig4t@rodu&ts gl_FrontLig4t@rodu&t>gl_9axLig4ts?'
uniform gl_Lig4t@rodu&ts gl_+a&2Lig4t@rodu&t>gl_9axLig4ts?'
66
7 Built$in Variables
((
(( epre&ated6
((
uniform ve&/ gl_TextureEnvColor>gl_9axTextureLnits?'
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 E start)
<'

uniform gl_Fog@arameters gl_Fog'
1.' ,uilt:In )erte* Output and Fragment Input )ariables
6nlike user;de$ined inter.olated varia7les, the ma..ing 7etween the 7uilt;in verte2 out.ut varia7les to the
7uilt;in $ragment in.ut varia7les doesnOt have a strict one;to;one corres.ondence. -wo sets are .rovided,
one $or each language. -heir relationshi. is descri7ed 7elow.
It is de.recated to have the /0 .rovide $i2ed $unctionalit# 7ehavior $or a .rogramma7le .i.eline stage.
%or e2am.le, mi2ing a $i2ed $unctionalit# verte2 stage with a .rogramma7le $ragment stage is de.recated.
3i.eline con$igurations where onl# a .ro.er su7set o$ stages are 7eing used do not re=uire the unused
stages to have shaders.
-he $ollowing 7uilt;in verte2 out.ut varia7les are availa7le, 7ut de.recated. A .articular one should 7e
written to i$ an# $unctionalit# in a corres.onding $ragment shader or $i2ed .i.eline uses it or state derived
$rom it. 'therwise, 7ehavior is unde$ined.
67
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
%or gl_$og$ragoor% Ade.recatedB, the value written will 7e used as the PcQ value in section &.11 o$ the
'.en/0 /ra.hics "#stem ".eci$ication, Version &.8, 7# the $i2ed $unctionalit# .i.eline. %or e2am.le, i$
the ?;coordinate o$ the $ragment in e#e s.ace is desired as PcQ, then thatOs what the verte2 shader
e2ecuta7le should write into gl_$og$ragoor%.
As with all arra#s, indices used to su7scri.t gl_&exoor% Ade.recatedB must either 7e an integral constant
e2.ressions, 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 im.lementation in .reserving var#ing
resources.
-he $ollowing $ragment in.ut varia7les are availa7le in a $ragment shader.
in ve&0 gl_@ointCoord'
-he $ollowing $ragment in.uts are also availa7le in a $ragment shader, 7ut are de.recated:
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
De.recated: -he values in gl_olor and gl_*econ%ar.olor will 7e derived automaticall# 7# the s#stem
$rom gl_$rontolor+ gl_8ac1olor+ gl_$ront*econ%ar.olor+ and gl_8ac1*econ%ar.olor 7ased on
which $ace is visi7le. I$ $i2ed $unctionalit# is used $or verte2 .rocessing, then gl_$og$ragoor% will
either 7e the ?;coordinate o$ the $ragment in e#e s.ace, or the inter.olation o$ the $og coordinate, as
descri7ed in section &.11 o$ the '.en/0 /ra.hics "#stem ".eci$ication, Version &.8. -he
gl_&exoor%"# values are the inter.olated gl_&exoor%"# values $rom a verte2 shader or the te2ture
coordinates o$ an# $i2ed .i.eline 7ased verte2 $unctionalit#.
Indices to the $ragment shader gl_&exoor% arra# are as descri7ed a7ove in the verte2 shader te2t.
-he values in gl_=ointoor% are two;dimensional coordinates indicating where within a .oint .rimitive
the current $ragment is located, when .oint s.rites are ena7led. -he# range $rom 8.8 to 1.8 across the
.oint. I$ the current .rimitive is not a .oint, or i$ .oint s.rites are not ena7led, then the values read $rom
gl_=ointoor% are unde$ined.
68
2 ,uilt:in Functions
-he '.en/0 "hading 0anguage de$ines an assortment o$ 7uilt;in convenience $unctions $or scalar and
vector o.erations. 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 ma..ing to hardware and so are availa7le onl# $or a s.eci$ic t#.e o$
shader.
-he 7uilt;in $unctions 7asicall# $all into three categories:
-he# e2.ose 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# re.resent a trivial o.eration Aclam., mi2, etc.B that is ver# sim.le $or the user to write, 7ut the#
are ver# common and ma# have direct hardware su..ort. It is a ver# hard .ro7lem $or the com.iler to
ma. e2.ressions to com.le2 assem7ler instructions.
-he# re.resent an o.eration gra.hics 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# su..ort vector
in.ut as well as the more traditional scalar in.ut.
A..lications should 7e encouraged to use the 7uilt;in $unctions rather than do the e=uivalent com.utations
in their own shader code since the 7uilt;in $unctions are assumed to 7e o.timal Ae.g., .erha.s su..orted
directl# in hardwareB.
6ser code can re.lace 7uilt;in $unctions with their own i$ the# choose, 7# sim.l# re;declaring and de$ining
the same name and argument list. Because 7uilt;in $unctions are in a more outer sco.e than user 7uilt;in
$unctions, doing this will hide all 7uilt;in $unctions with the same name as the re;declared $unction.
Rhen the 7uilt;in $unctions are s.eci$ied 7elow, where the in.ut arguments Aand corres.onding out.utB
can 7e float, vecD, vecE, or vecF, gen&.!e is used as the argument. Rhere the in.ut arguments Aand
corres.onding out.utB can 7e int, ivecD, ivecE, or ivecF, genI&.!e is used as the argument. Rhere the
in.ut arguments Aand corres.onding out.utB can 7e uint, uvecD, uvecE, or uvecF, gen?&.!e is used as the
argument. %or an# s.eci$ic use o$ a $unction, the actual t#.e su7stituted $or gen&.!e, genI&.!e, or
gen?&.!e has 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 Built$in %unctions
2.1 -ngle and Trigonometry Functions
%unction .arameters s.eci$ied as angle are 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 o.erate com.onent;wise. -he descri.tion is .er com.onent.
Synta* (escription
gen-#.e radians Agen-#.e %egreesB
!onverts %egrees to radians, i.e.

148
%egrees
gen-#.e degrees Agen-#.e ra%iansB
!onverts ra%ians to degrees, i.e.
148

ra%ians
gen-#.e sin Agen-#.e angleB -he standard trigonometric sine $unction.
gen-#.e cos Agen-#.e angleB -he standard trigonometric cosine $unction.
gen-#.e tan Agen-#.e angleB -he standard trigonometric tangent.
gen-#.e asin Agen-#.e xB Arc sine. Returns an angle whose sine is x. -he range
o$ values returned 7# this $unction is
[

]
Results are unde$ined i$ x1.
gen-#.e acos Agen-#.e xB Arc cosine. Returns an angle whose cosine is x. -he
range o$ values returned 7# this $unction is K8, L.
Results are unde$ined i$ x1.
gen-#.e atan Agen-#.e ., gen-#.e xB Arc tangent. Returns an angle whose tangent is .@x. -he
signs o$ x and . are used to determine what =uadrant the
angle is in. -he range o$ values returned 7# this
$unction is [+ ]. Results are unde$ined i$ x and
. are 7oth 8.
gen-#.e atan Agen-#.e ._over_xB Arc tangent. Returns an angle whose tangent is
._over_x. -he range o$ values returned 7# this $unction
is
[

]
.
70
8 Built$in %unctions
Synta* (escription
gen-#.e sinh Agen-#.e xB Returns the h#.er7olic sine $unction
e
x
e
x

gen-#.e cosh Agen-#.e xB Returns the h#.er7olic cosine $unction


e
x
e
x

gen-#.e tanh Agen-#.e xB Returns the h#.er7olic tangent $unction


sinh x
cosh x
gen-#.e asinh Agen-#.e xB Arc h#.er7olic sineC returns the inverse o$ sinh.
gen-#.e acosh Agen-#.e xB Arc h#.er7olic cosineC returns the non;negative inverse
o$ cosh. Results are unde$ined i$ x F 1.
gen-#.e atanh Agen-#.e xB Arc h#.er7olic tangentC returns the inverse o$ tanh.
Results are unde$ined i$ x1.
2.2 $*ponential Functions
-hese all o.erate com.onent;wise. -he descri.tion is .er com.onent.
Synta* (escription
gen-#.e pow Agen-#.e x, gen-#.e .B Returns x raised to the . .ower, i.e., x
.
Results are unde$ined i$ x A 0.
Results are unde$ined i$ x : 0 and . A: 0.
gen-#.e exp Agen-#.e xB Returns the natural e2.onentiation o$ x, i.e., e
x
.
gen-#.e log Agen-#.e xB Returns the natural logarithm o$ x+ i.e., returns the value
. which satis$ies the e=uation x N e
.
.
Results are unde$ined i$ x A: 0.
gen-#.e expD Agen-#.e xB Returns raised to the x .ower, i.e.,
x
gen-#.e logD Agen-#.e xB Returns the 7ase logarithm o$ x+ i.e., returns the value
. which satis$ies the e=uation x=
.
Results are unde$ined i$ x A: 0.
71
8 Built$in %unctions
Synta* (escription
gen-#.e sIrt Agen-#.e xB Returns
x .
Results are unde$ined i$ x A 0.
gen-#.e inversesIrt Agen-#.e xB
Returns
1
x
.
Results are unde$ined i$ x A: 0.
2.! Common Functions
-hese all o.erate com.onent;wise. -he descri.tion is .er com.onent.
Synta* (escription
gen-#.e abs Agen-#.e xB
genI-#.e abs AgenI-#.e xB
Returns x i$ x GN 8, otherwise it returns ]x.
gen-#.e sign Agen-#.e xB
genI-#.e sign AgenI-#.e xB
Returns 1.8 i$ x G 8, 8.8 i$ x N 8, or ]1.8 i$ x F 8.
gen-#.e floor Agen-#.e xB Returns a value e=ual to the nearest integer that is less
than or e=ual to x.
gen-#.e trunc Agen-#.e xB Returns a value e=ual to the nearest integer to x whose
a7solute value is not larger than the a7solute value o$ x.
gen-#.e round Agen-#.e xB Returns a value e=ual to the nearest integer to x. -he
$raction 8., will round in a direction chosen 7# the
im.lementation, .resuma7l# the direction that is $astest.
-his includes the .ossi7ilit# that roundAxB returns the
same value as roundEvenAxB $or all values o$ x.
gen-#.e roundEven Agen-#.e xB Returns a value e=ual to the nearest integer to x. A
$ractional .art o$ 8., will round toward the nearest even
integer. ABoth &., and )., $or 2 will return ).8.B
gen-#.e ceil Agen-#.e xB Returns a value e=ual to the nearest integer that is
greater than or e=ual to x.
gen-#.e fract Agen-#.e xB Returns x ] floor AxB.
72
8 Built$in %unctions
Synta* (escription
gen-#.e mod Agen-#.e x, $loat .B
gen-#.e mod Agen-#.e x, gen-#.e .B
9odulus. Returns x ] . floor Ax@.B.
gen-#.e modf Agen-#.e x, out gen-#.e iB Returns the $ractional .art o$ x and sets i to the integer
.art Aas a whole num7er $loating .oint valueB. Both the
return value and the out.ut .arameter will have the same
sign as x.
gen-#.e min Agen-#.e x, gen-#.e .B
gen-#.e min Agen-#.e x, $loat .B
genI-#.e min AgenI-#.e x, genI-#.e .B
genI-#.e min AgenI-#.e x, int .B
gen6-#.e min Agen6-#.e x, gen6-#.e .B
gen6-#.e min Agen6-#.e x, uint .B
Returns . i$ . F x, otherwise it returns x.
gen-#.e max Agen-#.e x, gen-#.e .B
gen-#.e max Agen-#.e x, $loat .B
genI-#.e max AgenI-#.e x, genI-#.e .B
genI-#.e max AgenI-#.e x, int .B
gen6-#.e max Agen6-#.e x, gen6-#.e .B
gen6-#.e max Agen6-#.e x, uint .B
Returns . i$ x F ., otherwise it returns x.
gen-#.e clamp Agen-#.e x,
gen-#.e minVal,
gen-#.e maxValB
gen-#.e clamp Agen-#.e x,
$loat minVal,
$loat maxValB
genI-#.e clamp AgenI-#.e x,
genI-#.e minVal,
genI-#.e maxValB
genI-#.e clamp AgenI-#.e x,
int minVal,
int maxValB
gen6-#.e clamp Agen6-#.e x,
gen6-#.e minVal,
gen6-#.e maxValB
gen6-#.e clamp Agen6-#.e x,
uint minVal,
uint maxValB
Returns min Amax Ax, minValB, maxValB.
Results are unde$ined i$ minVal G maxVal.
73
8 Built$in %unctions
Synta* (escription
gen-#.e mix Agen-#.e x,
gen-#.e .,
gen-#.e aB
gen-#.e mix Agen-#.e x,
gen-#.e .,
$loat aB
Returns the linear 7lend o$ x and .+ i.e.
x1a.a
gen-#.e mix Agen-#.e x,
gen-#.e #,
7vec aB
"elects which vector each returned com.onent comes
$rom. %or a com.onent o$ a that is false, the
corres.onding com.onent o$ x is returned. %or a
com.onent o$ a that is true, the corres.onding
com.onent o$ . is returned. !om.onents o$ x and . that
are not selected are allowed to 7e invalid $loating .oint
values and will have no e$$ect on the results. -hus, this
.rovides di$$erent $unctionalit# than
gen-#.e mixAgen-#.e x, gen-#.e ., gen-#.eAaBB
where a is a Boolean vector.
gen-#.e step Agen-#.e e%ge, gen-#.e xB
gen-#.e step A$loat e%ge, gen-#.e xB
Returns 8.8 i$ x F e%ge, otherwise it returns 1.8.
gen-#.e smoothstep Agen-#.e e%ge0,
gen-#.e e%ge1,
gen-#.e xB
gen-#.e smoothstep A$loat e%ge0,
$loat e%ge1,
gen-#.e xB
Returns 8.8 i$ x FN e%ge0 and 1.8 i$ x GN e%ge1 and
.er$orms smooth +ermite inter.olation 7etween 8 and 1
when e%ge0 F x F e%ge1. -his is use$ul in cases where
#ou would want a threshold $unction with a smooth
transition. -his is e=uivalent to:
gen-#.e tC
t N clam. AA2 ] edge8B @ Aedge1 ] edge8B, 8, 1BC
return t U t U A& ] U tBC
Results are un%e-ine% i- e%ge0 B: e%ge1.
7vec isnan Agen-#.e xB Returns true i$ x holds a <a< Anot a num7erB
re.resentation in the underl#ing im.lementationOs set o$
$loating .oint re.resentations. Returns false otherwise,
including $or im.lementations with no <a<
re.resentations.
7vec isinf Agen-#.e xB Returns true i$ x holds a .ositive in$init# or negative
in$init# re.resentation in the underl#ing im.lementationOs
set o$ $loating .oint re.resentations. Returns false
otherwise, including $or im.lementations with no in$init#
re.resentations.
74
8 Built$in %unctions
2.# Geometric Functions
-hese o.erate on vectors as vectors, not com.onent;wise.
Synta* (escription
$loat length Agen-#.e xB Returns the length o$ vector x, i.e.,
x[ 8]

x[ 1]

...
$loat distance Agen-#.e !0, gen-#.e !1B Returns the distance 7etween !0 and !1, i.e.
length A!0 C !1B
$loat dot Agen-#.e x, gen-#.e .B Returns the dot .roduct o$ x and ., i.e.,
x[ 8]. [8]x [1].[1]...
vec& cross Avec& x, vec& .B Returns the cross .roduct o$ 2 and #, i.e.
[
x [1].[ ]. [1]x []
x[ ].[ 8]. [ ]x[ 8]
x[8]. [1]. [8]x [1]
]
gen-#.e normali)e Agen-#.e xB Returns a vector in the same direction as x 7ut with a
length o$ 1.
vec) ftransformAB De.recatedC use invariant.
%or verte2 shaders onl#. -his $unction will ensure that
the incoming verte2 value will 7e trans$ormed in a wa#
that .roduces e2actl# the same result as would 7e
.roduced 7# '.en/0Ds $i2ed $unctionalit# trans$orm. It
is intended to 7e used to com.ute gl>3osition, e.g.,
gl>3osition N ftransformAB
-his $unction should 7e used, $or e2am.le, when an
a..lication is rendering the same geometr# in se.arate
.asses, and one .ass uses the $i2ed $unctionalit# .ath to
render and another .ass uses .rogramma7le shaders.
gen-#.e faceforwardAgen-#.e N,
gen-#.e I,
gen-#.e Nre-B
I$ dotANre-, IB F 8 return N+ otherwise return ]N.
75
8 Built$in %unctions
Synta* (escription
gen-#.e reflect Agen-#.e I, gen-#.e NB %or the incident vector I and sur$ace orientation N,
returns the re$lection direction:
I ] dotAN, IB N
N must alread# 7e normali?ed in order to achieve the
desired result.
gen-#.e refractAgen-#.e I, gen-#.e N,
$loat etaB
%or the incident vector I and sur$ace normal N, and the
ratio o$ indices o$ re$raction eta+ return the re$raction
vector. -he result is com.uted 7#
k N 1.8 ; eta U eta U A1.8 ; dotAN, IB U dotAN, IBB
i$ Ak F 8.8B
return gen-#.eA8.8B
else
return eta U I ; Aeta U dotAN, IB I sIrtAkBB U N
-he in.ut .arameters $or the incident vector I and the
sur$ace normal N must alread# 7e normali?ed to get the
desired results.
76
8 Built$in %unctions
2.& 4atri* Functions
Synta* (escription
mat matrix!ompOult Amat x, mat .B 9ulti.l# matri2 x 7# matri2 . com.onent;wise, i.e.,
resultKiLKML is the scalar .roduct o$ xKiLKML and .KiLKML.
<ote: to get linear alge7raic matri2 multi.lication, use
the multi.l# o.erator A/B.
mat outerQroductAvec c, vec rB
mat& outerQroductAvec& c, vec& rB
mat) outerQroductAvec) c, vec) rB
mat2& outerQroductAvec& c, vec rB
mat&2 outerQroductAvec c, vec& rB
mat2) outerQroductAvec) c, vec rB
mat)2 outerQroductAvec c, vec) rB
mat&2) outerQroductAvec) c, vec& rB
mat)2& outerQroductAvec& c, vec) rB
-reats the $irst .arameter c as a column vector Amatri2
with one columnB and the second .arameter r as a row
vector Amatri2 with one rowB and does a linear alge7raic
matri2 multi.l# c U r, #ielding a matri2 whose num7er o$
rows is the num7er o$ com.onents in c and whose
num7er o$ columns is the num7er o$ com.onents in r.
mat transposeAmat mB
mat& transposeAmat& mB
mat) transposeAmat) mB
mat2& transposeAmat&2 mB
mat&2 transposeAmat2& mB
mat2) transposeAmat)2 mB
mat)2 transposeAmat2) mB
mat&2) transposeAmat)2& mB
mat)2& transposeAmat&2) mB
Returns a matri2 that is the trans.ose o$ m. -he in.ut
matri2 m is not modi$ied.
77
8 Built$in %unctions
2.' )ector 8elational Functions
Relational and e=ualit# o.erators A1# 1;# 2# 2;# ;;# <;B are de$ined to .roduce scalar Boolean results. %or
vector results, use the $ollowing 7uilt;in $unctions. Below, P7vecQ is a .laceholder $or one o$ bvecD,
bvecE, or bvecF, PivecQ is a .laceholder $or one o$ ivecD, ivecE, or ivecF, PuvecQ is a .laceholder $or
uvecD, uvecE, or uvecF, and PvecQ is a .laceholder $or vecD, vecE, or vecF. In all cases, the si?es o$ the
in.ut and return vectors $or an# .articular call must match.
Synta* (escription
7vec lessAhanAvec 2, vec #B
7vec lessAhanAivec 2, ivec #B
7vec lessAhanAuvec 2, uvec #B
Returns the com.onent;wise com.are o$ x F ..
7vec lessAhanEIualAvec 2, vec #B
7vec lessAhanEIualAivec 2, ivec #B
7vec lessAhanEIualAuvec 2, uvec #B
Returns the com.onent;wise com.are o$ x FN ..
7vec greaterAhanAvec 2, vec #B
7vec greaterAhanAivec 2, ivec #B
7vec greaterAhanAuvec 2, uvec #B
Returns the com.onent;wise com.are o$ x G ..
7vec greaterAhanEIualAvec 2, vec #B
7vec greaterAhanEIualAivec 2, ivec #B
7vec greaterAhanEIualAuvec 2, uvec #B
Returns the com.onent;wise com.are o$ x GN ..
7vec eIualAvec 2, vec #B
7vec eIualAivec 2, ivec #B
7vec eIualAuvec 2, uvec #B
7vec eIualA7vec 2, 7vec #B
7vec notEIualAvec 2, vec #B
7vec notEIualAivec 2, ivec #B
7vec notEIualAuvec 2, uvec #B
7vec notEIualA7vec 2, 7vec #B
Returns the com.onent;wise com.are o$ x NN ..
Returns the com.onent;wise com.are o$ x TN ..
7ool anyA7vec 2B Returns true i$ an# com.onent o$ x is true.
7ool allA7vec 2B Returns true onl# i$ all com.onents o$ x are true.
7vec notA7vec 2B Returns the com.onent;wise logical com.lement o$ x.
78
8 Built$in %unctions
2.1 Te*ture Loo/up Functions
-e2ture looku. $unctions are availa7le to 7oth verte2 and $ragment shaders. +owever, level o$ detail is
not im.licitl# com.uted $or verte2 shaders. -he $unctions in the ta7le 7elow .rovide access to te2tures
through sam.lers, as set u. through the '.en/0 A3I. -e2ture .ro.erties such as si?e, .i2el $ormat,
num7er o$ dimensions, $iltering method, num7er o$ mi.;ma. levels, de.th com.arison, and so on are also
de$ined 7# '.en/0 A3I calls. "uch .ro.erties are taken into account as the te2ture is accessed via the
7uilt;in $unctions de$ined 7elow.
-e2ture data can 7e stored 7# the /0 as $loating .oint, unsigned normali?ed integer, unsigned integer or
signed integer data. -his is determined 7# the t#.e o$ the internal $ormat o$ the te2ture. -e2ture looku.s
on unsigned normali?ed integer and $loating .oint data return $loating .oint values in the range K8, 1L.
-e2ture looku. $unctions are .rovided that can return their result as $loating .oint, unsigned integer or
signed integer, de.ending on the sam.ler t#.e .assed to the looku. $unction. !are must 7e taken to use
the right sam.ler t#.e $or te2ture access. -he $ollowing ta7le lists the su..orted com7inations o$ sam.ler
t#.es and te2ture internal $ormats. Blank entries are unsu..orted. Doing a te2ture looku. will return
unde$ined values $or unsu..orted com7inations.
Internal -e2ture %ormat
%loating 3oint
"am.ler -#.es
"igned Integer
"am.ler -#.es
6nsigned Integer
"am.ler -#.es
%loating .oint "u..orted
<ormali?ed Integer "u..orted
"igned Integer "u..orted
6nsigned Integer "u..orted
I$ an integer sam.ler t#.e is used, the result o$ a te2ture looku. is an ivecF. I$ an unsigned integer sam.ler
t#.e is used, the result o$ a te2ture looku. is a uvecF. I$ a $loating .oint sam.ler t#.e is used, the result o$
a te2ture looku. is a vecF, where each com.onent is in the range K8, 1L.
In the .rotot#.es 7elow, the PgQ in the return t#.e PgvecDQ is used as a .laceholder $or nothing, PiQ, or PuQ
making a return t#.e o$ vecF, ivecF, or uvecF. In these cases, the sam.ler argument t#.e also starts with
PgQ, indicating the same su7stitution done on the return t#.eC it is either a $loating .oint, signed integer, or
unsigned integer sam.ler, matching the 7asic t#.e o$ the return t#.e, as descri7ed a7ove.
%or shadow $orms Athe sam.ler .arameter is a shadow;t#.eB, a de.th com.arison looku. on the de.th
te2ture 7ound to sam!ler is done as descri7ed in section &.5.1) o$ the '.en/0 /ra.hics "#stem
".eci$ication, Version &.8. "ee the ta7le 7elow $or which com.onent s.eci$ies Dre-. -he te2ture 7ound to
sam!ler must 7e a de.th te2ture, or results are unde$ined. I$ a non;shadow te2ture call is made to a
sam.ler that re.resents a de.th te2ture with de.th com.arisons turned on, then results are unde$ined. I$ a
shadow te2ture call is made to a sam.ler that re.resents a de.th te2ture with de.th com.arisons turned
o$$, then results are unde$ined. I$ a shadow te2ture call is made to a sam.ler that does not re.resent a
de.th te2ture, then results are unde$ined.
79
8 Built$in %unctions
In all $unctions 7elow, the ,ias .arameter is o.tional $or $ragment shaders. -he ,ias .arameter is not
acce.ted in a verte2 shader. %or a $ragment shader, i$ ,ias is .resent, it is added to the im.licit level o$
detail .rior to .er$orming the te2ture access o.eration.
-he im.licit level o$ detail is selected as $ollows: %or a te2ture that is not mi.;ma..ed, the te2ture is used
directl#. I$ it is mi.;ma..ed and running in a $ragment shader, the 0'D com.uted 7# the im.lementation
is used to do the te2ture looku.. I$ it is mi.;ma..ed and running on the verte2 shader, then the 7ase
te2ture is used.
"ome te2ture $unctions Anon;PCodQ and non;PGradQ versionsB ma# re=uire im.licit derivatives. Im.licit
derivatives are unde$ined within non;uni$orm control $low and $or verte2 shader te2ture $etches.
%or !ube $orms, the direction o$ = is used to select which $ace to do a ;dimensional te2ture looku. in, as
descri7ed in section &.5.( in the '.en/0 /ra.hics "#stem ".eci$ication, Version &.8.
%or Array $orms, the arra# la#er used will 7e
max 8, min % 1, -loorla.er8.,
where % is the de.th o$ the te2ture arra# and la.er comes $rom the com.onent indicated in the ta7les
7elow.
80
8 Built$in %unctions
Synta* (escription
int texture"i)e Agsam.ler1D sam!ler, int lo%B
ivec texture"i)e Agsam.lerD sam!ler, int lo%B
ivec& texture"i)e Agsam.ler&D sam!ler, int lo%B
ivec texture"i)e Agsam.ler!u7e sam!ler, int lo%B
int texture"i)e Asam.ler1D"hadow sam!ler, int lo%B
ivec texture"i)e Asam.lerD"hadow sam!ler, int lo%B
ivec texture"i)e Asam.ler!u7e"hadow sam!ler, int lo%B
ivec texture"i)e Agsam.ler1DArra# sam!ler, int lo%B
ivec& texture"i)e Agsam.lerDArra# sam!ler, int lo%B
ivec texture"i)e Asam.ler1DArra#"hadow sam!ler, int lo%B
ivec& texture"i)e Asam.lerDArra#"hadow sam!ler, int lo%B
Returns the dimensions o$ level
lo% $or the te2ture 7ound to
sam!ler, as descri7ed in section
.8.) o$ the '.en/0 /ra.hics
"#stem ".eci$ication, Version
&.8, under X-e2ture "i?e
:uer#X.
-he com.onents in the return
value are $illed in, in order, with
the width, height, de.th o$ the
te2ture.
%or the arra# $orms, the last
com.onent o$ the return value is
the num7er o$ la#ers in the
te2ture arra#.
gvec) texture Agsam.ler1D sam!ler+ $loat = K, $loat ,iasL B
gvec) texture Agsam.lerD sam!ler, vec = K, $loat ,iasL B
gvec) texture Agsam.ler&D sam!ler, vec& = K, $loat ,iasL B
gvec) texture Agsam.ler!u7e sam!ler, vec& = K, $loat ,iasL B
$loat texture Asam.ler1D"hadow sam!ler+ vec& = K, $loat ,iasL B
$loat texture Asam.lerD"hadow sam!ler+ vec& = K, $loat ,iasL B
$loat texture Asam.ler!u7e"hadow sam!ler+ vec) = K, $loat ,iasL B
gvec) texture Agsam.ler1DArra# sam!ler+ vec = K, $loat ,iasL B
gvec) texture Agsam.lerDArra# sam!ler, vec& = K, $loat ,iasL B
$loat texture Asam.ler1DArra#"hadow sam!ler+ vec& =
K, $loat ,iasL B
$loat texture Asam.lerDArra#"hadow sam!ler+ vec) =B
6se the te2ture coordinate = to
do a te2ture looku. in the
te2ture currentl# 7ound to
sam!ler. -he last com.onent o$
= is used as Dre- $or the shadow
$orms. %or arra# $orms, the arra#
la#er comes $rom the last
com.onent o$ = in the non;
shadow $orms, and the second to
last com.onent o$ = in the
shadow $orms.
gvec) textureQro% Agsam.ler1D sam!ler, vec = K, $loat ,iasL B
gvec) textureQro% Agsam.ler1D sam!ler, vec) = K, $loat ,iasL B
gvec) textureQro% Agsam.lerD sam!ler, vec& = K, $loat ,iasL B
gvec) textureQro% Agsam.lerD sam!ler, vec) = K, $loat ,iasL B
gvec) textureQro% Agsam.ler&D sam!ler, vec) = K, $loat ,iasL B
$loat textureQro% Asam.ler1D"hadow sam!ler+ vec) =
K, $loat ,iasL B
$loat textureQro% Asam.lerD"hadow sam!ler+ vec) =
K, $loat ,iasL B
Do a te2ture looku. with
.roMection. -he te2ture
coordinates consumed $rom =,
not including the last com.onent
o$ =, are divided 7# the last
com.onent o$ =. -he resulting
&
rd
com.onent o$ = in the
shadow $orms is used as Dre-.
A$ter these values are com.uted,
te2ture looku. .roceeds as in
texture.
81
8 Built$in %unctions
Synta* (escription
gvec) textureCod Agsam.ler1D sam!ler, $loat =, $loat lo%B
gvec) textureCod Agsam.lerD sam!ler, vec =, $loat lo%B
gvec) textureCod Agsam.ler&D sam!ler, vec& =, $loat lo%B
gvec) textureCod Agsam.ler!u7e sam!ler, vec& =, $loat lo%B
$loat textureCod Asam.ler1D"hadow sam!ler+ vec& =, $loat lo%B
$loat textureCod Asam.lerD"hadow sam!ler+ vec& =, $loat lo%B
gvec) textureCod Agsam.ler1DArra# sam!ler, vec =, $loat lo%B
gvec) textureCod Agsam.lerDArra# sam!ler, vec& =, $loat lo%B
$loat textureCod Asam.ler1DArra#"hadow sam!ler+ vec& =,
$loat lo%B
Do a te2ture looku. as in
texture 7ut with e2.licit 0'DC
lo% s.eci$ies E,ase Asee e=uation
&.1( in '.en/0 /ra.hics
"#stem ".eci$ication, Version
&.8B and set the .artial
derivatives in section &.5.1 as
$ollows.
u
x
= 8
v
x
= 8
2
x
= 8
u
.
= 8
v
.
= 8
2
.
= 8
gvec) textureffset Agsam.ler1D sam!ler+ $loat =+
int o--set K, $loat ,iasL B
gvec) textureffset Agsam.lerD sam!ler, vec =,
ivec o--set K, $loat ,iasL B
gvec) textureffset Agsam.ler&D sam!ler, vec& =,
ivec& o--set K, $loat ,iasL B
$loat textureffset Asam.ler1D"hadow sam!ler+ vec& =,
int o--set K, $loat ,iasL B
$loat textureffset Asam.lerD"hadow sam!ler+ vec& =,
ivec o--set K, $loat ,iasL B
gvec) textureffset Agsam.ler1DArra# sam!ler+ vec =+
int o--set K, $loat ,iasL B
gvec) textureffset Agsam.lerDArra# sam!ler, vec& =,
ivec o--set K, $loat ,iasL B
$loat textureffset Asam.ler1DArra#"hadow sam!ler+ vec& =,
int o--set K, $loat ,iasL B
Do a te2ture looku. as in
texture 7ut with o--set added to
the Au,v,2B te2el coordinates
7e$ore looking u. each te2el.
-he o$$set value must 7e a
constant e2.ression. A limited
range o$ o$$set values are
su..ortedC the minimum and
ma2imum o$$set values are
im.lementation;de.endent and
given 7#
9I<>3R'/RA9>-*Z*0>'%%"*- and
9AZ>3R'/RA9>-*Z*0>'%%"*-,
res.ectivel#.
<ote that o--set does not a..l#
to the la#er coordinate $or
te2ture arra#s. -his is e2.lained
in detail in section &.5.1 o$ the
'.en/0 /ra.hics "#stem
".eci$ication, Version &.8,
where o--set is
u
+
v
+
2
.
<ote that te2el o$$sets are also
not su..orted $or cu7e ma.s.
82
8 Built$in %unctions
Synta* (escription
gvec) texelJetch Agsam.ler1D sam!ler, int =, int lo%B
gvec) texelJetch Agsam.lerD sam!ler, ivec =, int lo%B
gvec) texelJetch Agsam.ler&D sam!ler, ivec& =, int lo%B
gvec) texelJetch Agsam.ler1DArra# sam!ler, ivec =, int lo%B
gvec) texelJetch Agsam.lerDArra# sam!ler, ivec& =, int lo%B
6se integer te2ture coordinate =
to looku. a single te2el $rom
sam!ler. -he arra# la#er comes
$rom the last com.onent o$ = $or
the arra# $orms. -he level;o$;
detail lo% as descri7ed in section
.8.) o$ the '.en/0 /ra.hics
"#stem ".eci$ication, Version
&.8, under X-e2el %etchesX.
gvec) texelJetchffset Agsam.ler1D sam!ler, int =, int lo%,
int o--setB
gvec) texelJetchffset Agsam.lerD sam!ler, ivec =, int lo%,
ivec o--setB
gvec) texelJetchffset Agsam.ler&D sam!ler, ivec& =, int lo%,
ivec& o--setB
gvec) texelJetchffset Agsam.ler1DArra# sam!ler, ivec =, int lo%,
int o--setB
gvec) texelJetchffset Agsam.lerDArra# sam!ler, ivec& =, int lo%,
ivec o--setB
%etch a single te2el as in
texelJetch o$$set 7# o--set as
descri7ed in textureffset.
gvec) textureQro%ffset Agsam.ler1D sam!ler+ vec =+
int o--set K, $loat ,iasL B
gvec) textureQro%ffset Agsam.ler1D sam!ler+ vec) =+
int o--set K, $loat ,iasL B
gvec) textureQro%ffset Agsam.lerD sam!ler, vec& =,
ivec o--set K, $loat ,iasL B
gvec) textureQro%ffset Agsam.lerD sam!ler, vec) =,
ivec o--set K, $loat ,iasL B
gvec) textureQro%ffset Agsam.ler&D sam!ler, vec) =,
ivec& o--set K, $loat ,iasL B
$loat textureQro%ffset Asam.ler1D"hadow sam!ler+ vec) =,
int o--set K, $loat ,iasL B
$loat textureQro%ffset Asam.lerD"hadow sam!ler+ vec) =,
ivec o--set K, $loat ,iasL B
Do a .roMective te2ture looku.
as descri7ed in textureQro%
o$$set 7# o--set as descri7ed in
textureffset.
83
8 Built$in %unctions
Synta* (escription
gvec) textureCodffset Agsam.ler1D sam!ler+ $loat =,
$loat lo%, int o--setB
gvec) textureCodffset Agsam.lerD sam!ler, vec =,
$loat lo%, ivec o--setB
gvec) textureCodffset Agsam.ler&D sam!ler, vec& =,
$loat lo%, ivec& o--setB
$loat textureCodffset Asam.ler1D"hadow sam!ler+ vec& =,
$loat lo%, int o--setB
$loat textureCodffset Asam.lerD"hadow sam!ler+ vec& =,
$loat lo%, ivec o--setB
gvec) textureCodffset Agsam.ler1DArra# sam!ler+ vec =,
$loat lo%, int o--setB
gvec) textureCodffset Agsam.lerDArra# sam!ler, vec& =,
$loat lo%, ivec o--setB
$loat textureCodffset Asam.ler1DArra#"hadow sam!ler+ vec& =,
$loat lo%, int o--setB
Do an o$$set te2ture looku. with
e2.licit 0'D. "ee textureCod
and textureffset.
gvec) textureQro%Cod Agsam.ler1D sam!ler, vec =, $loat lo%B
gvec) textureQro%Cod Agsam.ler1D sam!ler, vec) =, $loat lo%B
gvec) textureQro%Cod Agsam.lerD sam!ler, vec& =, $loat lo%B
gvec) textureQro%Cod Agsam.lerD sam!ler, vec) =, $loat lo%B
gvec) textureQro%Cod Agsam.ler&D sam!ler, vec) =, $loat lo%B
$loat textureQro%Cod Asam.ler1D"hadow sam!ler+ vec) =, $loat lo%B
$loat textureQro%Cod Asam.lerD"hadow sam!ler+ vec) =+ $loat lo%B
Do a .roMective te2ture looku.
with e2.licit 0'D. "ee
textureQro% and textureCod.
gvec) textureQro%Codffset Agsam.ler1D sam!ler+ vec =,
$loat lo%, int o--setB
gvec) textureQro%Codffset Agsam.ler1D sam!ler+ vec) =,
$loat lo%, int o--setB
gvec) textureQro%Codffset Agsam.lerD sam!ler, vec& =,
$loat lo%, ivec o--setB
gvec) textureQro%Codffset Agsam.lerD sam!ler, vec) =,
$loat lo%, ivec o--setB
gvec) textureQro%Codffset Agsam.ler&D sam!ler, vec) =,
$loat lo%, ivec& o--setB
$loat textureQro%Codffset Asam.ler1D"hadow sam!ler+ vec) =,
$loat lo%, int o--setB
$loat textureQro%Codffset Asam.lerD"hadow sam!ler+ vec) =,
$loat lo%, ivec o--setB
Do an o$$set .roMective te2ture
looku. with e2.licit 0'D. "ee
textureQro%, textureCod, and
textureffset.
84
8 Built$in %unctions
Synta* (escription
gvec) textureGrad Agsam.ler1D sam!ler+ $loat =,
$loat %=%x, $loat %=%.B
gvec) textureGrad Agsam.lerD sam!ler+ vec =,
vec %=%x, vec %=%.B
gvec) textureGrad Agsam.ler&D sam!ler+ vec& =,
vec& %=%x, vec& %=%.B
gvec) textureGrad Agsam.ler!u7e sam!ler, vec& =,
vec& %=%x, vec& %=%.B
$loat textureGrad Asam.ler1D"hadow sam!ler, vec& =,
$loat %=%x, $loat %=%.B
$loat textureGrad Asam.lerD"hadow sam!ler, vec& =,
vec %=%x, vec %=%.B
$loat textureGrad Asam.ler!u7e"hadow sam!ler, vec) =,
vec& %=%x, vec& %=%.B
gvec) textureGrad Agsam.ler1DArra# sam!ler+ vec =,
$loat %=%x, $loat %=%.B
gvec) textureGrad Agsam.lerDArra# sam!ler+ vec& =,
vec %=%x, vec %=%.B
$loat textureGrad Asam.ler1DArra#"hadow sam!ler, vec& =,
$loat %=%x, $loat %=%.B
$loat textureGrad Asam.lerDArra#"hadow sam!ler, vec) =,
vec %=%x, vec %=%.B
Do a te2ture looku. as in
texture 7ut with e2.licit
gradients. -he .artial
derivatives o$ = are with res.ect
to window 2 and window #. "et
s
x
=
{
=
x
$or a 1D te2ture
=.s
x
otherwise
s
.
=
{
=
.
$or a 1D te2ture
=.s
.
otherwise
t
x
=
{
8.8 $or a 1D te2ture
=.t
x
otherwise
t
.
=
{
8.8 $or a 1D te2ture
=.t
.
otherwise
r
x
=
{
8.8 $or 1D or D
=.!
x
cu7e, other
r
.
=
{
8.8 $or 1D or D
=.!
.
cu7e, other
%or the cu7e version, the .artial
derivatives o$ = are assumed to
7e in the coordinate s#stem used
7e$ore te2ture coordinates are
.roMected onto the a..ro.riate
cu7e $ace.
85
8 Built$in %unctions
Synta* (escription
gvec) textureGradffset Agsam.ler1D sam!ler+ $loat =+
$loat %=%x, $loat %=%.+ int o--setB
gvec) textureGradffset Agsam.lerD sam!ler, vec =,
vec %=%x, vec %=%.+ ivec o--setB
gvec) textureGradffset Agsam.ler&D sam!ler, vec& =,
vec& %=%x, vec& %=%.+ ivec& o--setB
$loat textureGradffset Asam.ler1D"hadow sam!ler+ vec& =,
$loat %=%x, $loat %=%.+ int o--set B
$loat textureGradffset Asam.lerD"hadow sam!ler+ vec& =,
vec %=%x, vec %=%.+ ivec o--setB
$loat textureGradffset Asam.ler!u7e"hadow sam!ler+ vec) =,
vec& %=%x, vec& %=%.+ ivec o--setB
gvec) textureGradffset Agsam.ler1DArra# sam!ler+ vec =+
$loat %=%x, $loat %=%.+ int o--setB
gvec) textureGradffset Agsam.lerDArra# sam!ler, vec& =,
vec %=%x, vec %=%.+ ivec o--setB
$loat textureGradffset Asam.ler1DArra#"hadow sam!ler+ vec& =,
$loat %=%x, $loat %=%.+ int o--setB
$loat textureGradffset Asam.lerDArra#"hadow sam!ler+ vec) =,
vec %=%x, vec %=%.+ ivec o--setB
Do a te2ture looku. with 7oth
e2.licit gradient and o$$set, as
descri7ed in textureGrad and
textureffset.
gvec) textureQro%Grad Agsam.ler1D sam!ler+ vec =,
$loat %=%x, $loat %=%.B
gvec) textureQro%Grad Agsam.ler1D sam!ler+ vec) =,
$loat %=%x, $loat %=%.B
gvec) textureQro%Grad Agsam.lerD sam!ler+ vec& =,
vec %=%x, vec %=%.B
gvec) textureQro%Grad Agsam.lerD sam!ler+ vec) =,
vec %=%x, vec %=%.B
gvec) textureQro%Grad Agsam.ler&D sam!ler+ vec) =,
vec& %=%x, vec& %=%.B
$loat textureQro%Grad Asam.ler1D"hadow sam!ler, vec) =,
$loat %=%x, $loat %=%.B
$loat textureQro%Grad Asam.lerD"hadow sam!ler, vec) =,
vec %=%x, vec %=%.B
Do a te2ture looku. 7oth
.roMectivel#, as descri7ed in
textureQro%, and with e2.licit
gradient as descri7ed in
textureGrad. -he .artial
derivatives %=%x and %=%. are
assumed to 7e alread# .roMected.
86
8 Built$in %unctions
Synta* (escription
gvec) textureQro%Gradffset Agsam.ler1D sam!ler+ vec =,
$loat %=%x, $loat %=%., int o--setB
gvec) textureQro%Gradffset Agsam.ler1D sam!ler+ vec) =,
$loat %=%x, $loat %=%., int o--setB
gvec) textureQro%Gradffset Agsam.lerD sam!ler+ vec& =,
vec %=%x, vec %=%., vec o--setB
gvec) textureQro%Gradffset Agsam.lerD sam!ler+ vec) =,
vec %=%x, vec %=%., vec o--setB
gvec) textureQro%Gradffset Agsam.ler&D sam!ler+ vec) =,
vec& %=%x, vec& %=%., vec& o--setB
$loat textureQro%Gradffset Asam.ler1D"hadow sam!ler, vec) =,
$loat %=%x, $loat %=%., int o--setB
$loat textureQro%Gradffset Asam.lerD"hadow sam!ler, vec) =,
vec %=%x, vec %=%., vec o--setB
Do a te2ture looku. .roMectivel#
and with e2.licit gradient as
descri7ed in textureQro%Grad,
as well as with o$$set, as
descri7ed in textureffset.
87
8 Built$in %unctions
-he $ollowing te2ture $unctions are de.recated.
Synta* (escription
vec) texture1B Asam.ler1D sam!ler+
$loat coor% K, $loat ,iasL B
vec) texture1BQro% Asam.ler1D sam!ler,
vec coor% K, $loat ,iasL B
vec) texture1BQro% Asam.ler1D sam!ler,
vec) coor% K, $loat ,iasL B
vec) texture1BCod Asam.ler1D sam!ler,
$loat coor%, $loat lo%B
vec) texture1BQro%Cod Asam.ler1D sam!ler,
vec coor%, $loat lo%B
vec) texture1BQro%Cod Asam.ler1D sam!ler,
vec) coor%, $loat lo%B
De.recated. "ee corres.onding signature
a7ove without P1DQ in the name.
vec) textureDB Asam.lerD sam!ler,
vec coor% K, $loat ,iasL B
vec) textureDBQro% Asam.lerD sam!ler,
vec& coor% K, $loat ,iasL B
vec) textureDBQro% Asam.lerD sam!ler,
vec) coor% K, $loat ,iasL B
vec) textureDBCod Asam.lerD sam!ler,
vec coor%, $loat lo%B
vec) textureDBQro%Cod Asam.lerD sam!ler,
vec& coor%, $loat lo%B
vec) textureDBQro%Cod Asam.lerD sam!ler,
vec) coor%, $loat lo%B
De.recated. "ee corres.onding signature
a7ove without PDQ in the name.
vec) textureEB Asam.ler&D sam!ler,
vec& coor% K, $loat ,iasL B
vec) textureEBQro% Asam.ler&D sam!ler,
vec) coor% K, $loat ,iasL B
vec) textureEBCod Asam.ler&D sam!ler,
vec& coor%, $loat lo%B
vec) textureEBQro%Cod Asam.ler&D sam!ler,
vec) coor%, $loat lo%B
De.recated. "ee corres.onding signature
a7ove without P&DQ in the name.
6se the te2ture coordinate coor% to do a
te2ture looku. in the &D te2ture currentl#
7ound to sam!ler. %or the .roMective
APQro%QB versions, the te2ture coordinate is
divided 7# coor%.4.
vec) texture!ube Asam.ler!u7e sam!ler,
vec& coor% K, $loat ,iasL B
vec) texture!ubeCod Asam.ler!u7e sam!ler,
vec& coor%, $loat lo%B
De.recated. "ee corres.onding signature
a7ove without P!u7eQ in the name.
88
8 Built$in %unctions
Synta* (escription
vec) shadow1B Asam.ler1D"hadow sam!ler+
vec& coor% K, $loat ,iasL B
vec) shadowDB Asam.lerD"hadow sam!ler+
vec& coor% K, $loat ,iasL B
vec) shadow1BQro% Asam.ler1D"hadow sam!ler+
vec) coor% K, $loat ,iasL B
vec) shadowDBQro% Asam.lerD"hadow sam!ler+
vec) coor% K, $loat ,iasL B
vec) shadow1BCod Asam.ler1D"hadow sam!ler+
vec& coor%, $loat lo%B
vec) shadowDBCod Asam.lerD"hadow sam!ler+
vec& coor%, $loat lo%B
vec) shadow1BQro%CodAsam.ler1D"hadow sam!ler+
vec) coor%, $loat lo%B
vec) shadowDBQro%CodAsam.lerD"hadow sam!ler+
vec) coor%+ $loat lo%B
De.recated. "ame $unctionalit# as the
Ptexture] 7ased names a7ove with the same
signature.
2.2 Fragment +rocessing Functions
%ragment .rocessing $unctions are onl# availa7le in $ragment shaders.
Derivatives ma# 7e com.utationall# e2.ensive and@or numericall# unsta7le. -here$ore, an '.en/0
im.lementation ma# a..ro2imate the true derivatives 7# using a $ast 7ut not entirel# accurate derivative
com.utation. Derivatives are unde$ined within non;uni$orm control $low.
-he e2.ected 7ehavior o$ a derivative is s.eci$ied using $orward@7ackward di$$erencing.
%orward di$$erencing:
$ x%x$ x ~%$%x x%x 1a
%$%x x ~
$ x%x$ x
%x
17
Backward di$$erencing:
$ x%x$ x ~%$%x x%x a
%$%x x ~
$ x$ x%x
%x
7
Rith single;sam.le rasteri?ation, %x FN 1.8 in e=uations 17 and 7. %or multi;sam.le rasteri?ation, %x F
.8 in e=uations 17 and 7.
dJdy is a..ro2imated similarl#, with . re.lacing x.
89
8 Built$in %unctions
A /0 im.lementation ma# use the a7ove or other methods to .er$orm the calculation, su7Mect to the
$ollowing conditions:
1. -he method ma# use .iecewise linear a..ro2imations. "uch linear a..ro2imations im.l# that higher
order derivatives, dJdxAdJdxAxBB and a7ove, are unde$ined.
. -he method ma# assume that the $unction evaluated is continuous. -here$ore derivatives within the
7od# o$ a non;uni$orm conditional are unde$ined.
&. -he method ma# di$$er .er $ragment, su7Mect to the constraint that the method ma# var# 7# window
coordinates, not screen coordinates. -he invariance re=uirement descri7ed in section &. o$ the
'.en/0 /ra.hics "#stem ".eci$ication, Version &.8, is rela2ed $or derivative calculations, 7ecause
the method ma# 7e a $unction o$ $ragment location.
'ther .ro.erties that are desira7le, 7ut not re=uired, are:
). %unctions should 7e evaluated within the interior o$ a .rimitive Ainter.olated, not e2tra.olatedB.
,. %unctions $or dJdx should 7e evaluated while holding # constant. %unctions $or dJdy should 7e
evaluated while holding 2 constant. +owever, mi2ed higher order derivatives, like dJdxAdJdyA.BB
and dJdyAdJdxAxBB are unde$ined.
(. Derivatives o$ constant arguments should 7e 8.
In some im.lementations, var#ing degrees o$ derivative accurac# ma# 7e o7tained 7# .roviding /0 hints
Asection ,.( o$ the '.en/0 /ra.hics "#stem ".eci$ication, Version &.8B, allowing a user to make an
image =ualit# versus s.eed trade o$$.
Synta* (escription
gen-#.e dJdx Agen-#.e !B Returns the derivative in 2 using local di$$erencing $or
the in.ut argument !.
gen-#.e dJdy Agen-#.e !B Returns the derivative in # using local di$$erencing $or
the in.ut argument !.
-hese two $unctions are commonl# used to estimate the
$ilter width used to anti;alias .rocedural te2tures. Re
are assuming that the e2.ression is 7eing evaluated in
.arallel on a "I9D arra# so that at an# given .oint in
time the value o$ the $unction is known at the grid .oints
re.resented 7# the "I9D arra#. 0ocal di$$erencing
7etween "I9D arra# elements can there$ore 7e used to
derive d%d2, d%d#, etc.
gen-#.e fwidth Agen-#.e !B Returns the sum o$ the a7solute derivative in 2 and #
using local di$$erencing $or the in.ut argument !, i.e.:
abs AdJdx A!BB I abs AdJdy A!BBC
90
8 Built$in %unctions
2.5 ;oise Functions
<oise $unctions are availa7le to 7oth $ragment and verte2 shaders. -he# are stochastic $unctions that can
7e used to increase visual com.le2it#. Values returned 7# the $ollowing noise $unctions give the
a..earance o$ randomness, 7ut are not trul# random. -he noise $unctions 7elow are de$ined to have the
$ollowing characteristics:
-he return valueAsB are alwa#s in the range K;1.8,1.8L, and cover at least the range K;8.(, 8.(L, with a
/aussian;like distri7ution.
-he return valueAsB have an overall average o$ 8.8
-he# are re.eata7le, in that a .articular in.ut value will alwa#s .roduce the same return value
-he# are statisticall# invariant under rotation Ai.e., no matter how the domain is rotated, it has the same
statistical characterB
-he# have a statistical invariance under translation Ai.e., no matter how the domain is translated, it has
the same statistical characterB
-he# t#.icall# give di$$erent results under translation.
-he s.atial $re=uenc# is narrowl# concentrated, centered somewhere 7etween 8., to 1.8.
-he# are !
1
continuous ever#where Ai.e., the $irst derivative is continuousB
Synta* (escription
$loat noise1 Agen-#.e xB Returns a 1D noise value 7ased on the in.ut value x.
vec noiseD Agen-#.e xB Returns a D noise value 7ased on the in.ut value x.
vec& noiseE Agen-#.e xB Returns a &D noise value 7ased on the in.ut value x.
vec) noiseF Agen-#.e xB Returns a )D noise value 7ased on the in.ut value x.
91
5 Shading Language Grammar
-he grammar is $ed $rom the out.ut o$ le2ical anal#sis. -he tokens returned $rom le2ical anal#sis are
8TTRI+LTE CONST +OOL FLO8T INT LINT
+RE8O CONTINLE O ELSE FOR IF ISC8R RETLRN SPITCF C8SE EF8LLT
+VEC0 +VEC3 +VEC/ IVEC0 IVEC3 IVEC/ LVEC0 LVEC3 LVEC/ VEC0 VEC3 VEC/
98T0 98T3 98T/ CENTROI IN OLT INOLT LNIFOR9 V8RQIN!
NO@ERS@ECTIVE FL8T S9OOTF
98T0R0 98T0R3 98T0R/
98T3R0 98T3R3 98T3R/
98T/R0 98T/R3 98T/R/
S89@LER5 S89@LER0 S89@LER3 S89@LERCL+E S89@LER5SF8OP S89@LER0SF8OP
S89@LERCL+ESF8OP S89@LER58RR8Q S89@LER08RR8Q S89@LER58RR8QSF8OP
S89@LER08RR8QSF8OP IS89@LER5 IS89@LER0 IS89@LER3 IS89@LERCL+E
IS89@LER58RR8Q IS89@LER08RR8Q LS89@LER5 LS89@LER0 LS89@LER3
LS89@LERCL+E LS89@LER58RR8Q LS89@LER08RR8Q
STRLCT VOI PFILE
IENTIFIER TQ@E_N89E FLO8TCONST8NT INTCONST8NT LINTCONST8NT +OOLCONST8NT
FIEL_SELECTION
LEFT_O@ RI!FT_O@
INC_O@ EC_O@ LE_O@ !E_O@ EM_O@ NE_O@
8N_O@ OR_O@ ROR_O@ 9LL_8SSI!N IV_8SSI!N 8_8SSI!N
9O_8SSI!N LEFT_8SSI!N RI!FT_8SSI!N 8N_8SSI!N ROR_8SSI!N OR_8SSI!N
SL+_8SSI!N
LEFT_@8REN RI!FT_@8REN LEFT_+R8COET RI!FT_+R8COET LEFT_+R8CE RI!FT_+R8CE OT
CO998 COLON EML8L SE9ICOLON +8N! 8SF TILE @LLS ST8R SL8SF @ERCENT
LEFT_8N!LE RI!FT_8N!LE VERTIC8L_+8R C8RET 89@ERS8N MLESTION
INV8RI8NT
FI!F_@RECISION 9EIL9_@RECISION LOP_@RECISION @RECISION
-he $ollowing descri7es the grammar $or the '.en/0 "hading 0anguage in terms o$ the a7ove tokens.
varia,le_i%enti-ier:
ID/N&I$I/R
!rimar._ex!ression:
varia,le_i%enti-ier
IN&(N*&FN&
92
9 "&adin' (an'ua'e )ra##ar
?IN&(N*&FN&
$L(F&(N*&FN&
8((L(N*&FN&
L/$&_=FR/N ex!ression RI)G&_=FR/N
!ost-ix_ex!ression:
!rimar._ex!ression
!ost-ix_ex!ression L/$&_8RFH/& integer_ex!ression RI)G&_8RFH/&
-unction_call
!ost-ix_ex!ression D(& $I/LD_*/L/&I(N
!ost-ix_ex!ression IN_(=
!ost-ix_ex!ression D/_(=
integer_ex!ression:
ex!ression
-unction_call:
-unction_call_or_met'o%
-unction_call_or_met'o%:
-unction_call_generic
!ost-ix_ex!ression D(& -unction_call_generic
-unction_call_generic:
-unction_call_'ea%er_2it'_!arameters RI)G&_=FR/N
-unction_call_'ea%er_no_!arameters RI)G&_=FR/N
-unction_call_'ea%er_no_!arameters:
-unction_call_'ea%er V(ID
-unction_call_'ea%er
-unction_call_'ea%er_2it'_!arameters:
-unction_call_'ea%er assignment_ex!ression
-unction_call_'ea%er_2it'_!arameters (>>F assignment_ex!ression
-unction_call_'ea%er:
-unction_i%enti-ier L/$&_=FR/N
@@ )rammar Note: onstructors loo1 li1e -unctions+ ,ut lexical anal.sis recogni3e% most o- t'em as
93
9 "&adin' (an'ua'e )ra##ar
@@ 1e.2or%s. &'e. are no2 recogni3e% t'roug' It.!e_s!eci-ierJ.
-unction_i%enti-ier:
t.!e_s!eci-ier
ID/N&I$I/R
$I/LD_*/L/&I(N
unar._ex!ression:
!ost-ix_ex!ression
IN_(= unar._ex!ression
D/_(= unar._ex!ression
unar._o!erator unar._ex!ression
@@ )rammar Note: No tra%itional st.le t.!e casts.
unar._o!erator:
=L?*
DF*G
8FN)
&ILD/
@@ )rammar Note: No KLK or KMK unar. o!s. =ointers are not su!!orte%.
multi!licative_ex!ression:
unar._ex!ression
multi!licative_ex!ression *&FR unar._ex!ression
multi!licative_ex!ression *LF*G unar._ex!ression
multi!licative_ex!ression =/R/N& unar._ex!ression
a%%itive_ex!ression:
multi!licative_ex!ression
a%%itive_ex!ression =L?* multi!licative_ex!ression
a%%itive_ex!ression DF*G multi!licative_ex!ression
s'i-t_ex!ression:
a%%itive_ex!ression
s'i-t_ex!ression L/$&_(= a%%itive_ex!ression
s'i-t_ex!ression RI)G&_(= a%%itive_ex!ression
relational_ex!ression:
94
9 "&adin' (an'ua'e )ra##ar
s'i-t_ex!ression
relational_ex!ression L/$&_FN)L/ s'i-t_ex!ression
relational_ex!ression RI)G&_FN)L/ s'i-t_ex!ression
relational_ex!ression L/_(= s'i-t_ex!ression
relational_ex!ression )/_(= s'i-t_ex!ression
e4ualit._ex!ression:
relational_ex!ression
e4ualit._ex!ression /N_(= relational_ex!ression
e4ualit._ex!ression N/_(= relational_ex!ression
an%_ex!ression:
e4ualit._ex!ression
an%_ex!ression F>=/R*FND e4ualit._ex!ression
exclusive_or_ex!ression:
an%_ex!ression
exclusive_or_ex!ression FR/& an%_ex!ression
inclusive_or_ex!ression:
exclusive_or_ex!ression
inclusive_or_ex!ression V/R&IFL_8FR exclusive_or_ex!ression
logical_an%_ex!ression:
inclusive_or_ex!ression
logical_an%_ex!ression FND_(= inclusive_or_ex!ression
logical_xor_ex!ression:
logical_an%_ex!ression
logical_xor_ex!ression O(R_(= logical_an%_ex!ression
logical_or_ex!ression:
logical_xor_ex!ression
logical_or_ex!ression (R_(= logical_xor_ex!ression
con%itional_ex!ression:
logical_or_ex!ression
logical_or_ex!ression N?/*&I(N ex!ression (L(N assignment_ex!ression
assignment_ex!ression:
95
9 "&adin' (an'ua'e )ra##ar
con%itional_ex!ression
unar._ex!ression assignment_o!erator assignment_ex!ression
assignment_o!erator:
/N?FL
>?L_F**I)N
DIV_F**I)N
>(D_F**I)N
FDD_F**I)N
*?8_F**I)N
L/$&_F**I)N
RI)G&_F**I)N
FND_F**I)N
O(R_F**I)N
(R_F**I)N
ex!ression:
assignment_ex!ression
ex!ression (>>F assignment_ex!ression
constant_ex!ression:
con%itional_ex!ression
%eclaration:
-unction_!rotot.!e */>I(L(N
init_%eclarator_list */>I(L(N
=R/I*I(N !recision_4uali-ier t.!e_s!eci-ier_no_!rec */>I(L(N
-unction_!rotot.!e:
-unction_%eclarator RI)G&_=FR/N
-unction_%eclarator:
-unction_'ea%er
-unction_'ea%er_2it'_!arameters
-unction_'ea%er_2it'_!arameters:
-unction_'ea%er !arameter_%eclaration
-unction_'ea%er_2it'_!arameters (>>F !arameter_%eclaration
-unction_'ea%er:
96
9 "&adin' (an'ua'e )ra##ar
-ull._s!eci-ie%_t.!e ID/N&I$I/R L/$&_=FR/N
!arameter_%eclarator:
t.!e_s!eci-ier ID/N&I$I/R
t.!e_s!eci-ier ID/N&I$I/R L/$&_8RFH/& constant_ex!ression RI)G&_8RFH/&
!arameter_%eclaration:
!arameter_t.!e_4uali-ier !arameter_4uali-ier !arameter_%eclarator
!arameter_4uali-ier !arameter_%eclarator
!arameter_t.!e_4uali-ier !arameter_4uali-ier !arameter_t.!e_s!eci-ier
!arameter_4uali-ier !arameter_t.!e_s!eci-ier
!arameter_4uali-ier:
@L em!t. L@
IN
(?&
IN(?&
!arameter_t.!e_s!eci-ier:
t.!e_s!eci-ier
init_%eclarator_list:
single_%eclaration
init_%eclarator_list (>>F ID/N&I$I/R
init_%eclarator_list (>>F ID/N&I$I/R L/$&_8RFH/& RI)G&_8RFH/&
init_%eclarator_list (>>F ID/N&I$I/R L/$&_8RFH/& constant_ex!ression
RI)G&_8RFH/&
init_%eclarator_list (>>F ID/N&I$I/R L/$&_8RFH/&
RI)G&_8RFH/& /N?FL initiali3er
init_%eclarator_list (>>F ID/N&I$I/R L/$&_8RFH/& constant_ex!ression
RI)G&_8RFH/& /N?FL initiali3er
init_%eclarator_list (>>F ID/N&I$I/R /N?FL initiali3er
single_%eclaration:
-ull._s!eci-ie%_t.!e
-ull._s!eci-ie%_t.!e ID/N&I$I/R
-ull._s!eci-ie%_t.!e ID/N&I$I/R L/$&_8RFH/& RI)G&_8RFH/&
-ull._s!eci-ie%_t.!e ID/N&I$I/R L/$&_8RFH/& constant_ex!ression RI)G&_8RFH/&
-ull._s!eci-ie%_t.!e ID/N&I$I/R L/$&_8RFH/& RI)G&_8RFH/& /N?FL initiali3er
-ull._s!eci-ie%_t.!e ID/N&I$I/R L/$&_8RFH/& constant_ex!ression
97
9 "&adin' (an'ua'e )ra##ar
RI)G&_8RFH/& /N?FL initiali3er
-ull._s!eci-ie%_t.!e ID/N&I$I/R /N?FL initiali3er
INVFRIFN& ID/N&I$I/R @@ Vertex onl..
@@ )rammar Note: No KenumK+ or Kt.!e%e-K.
-ull._s!eci-ie%_t.!e:
t.!e_s!eci-ier
t.!e_4uali-ier t.!e_s!eci-ier
invariant_4uali-ier:
INVFRIFN&
inter!olation_4uali-ier:
*>((&G
$LF&
N(=/R*=/&IV/
!arameter_t.!e_4uali-ier:
(N*&
t.!e_4uali-ier:
storage_4uali-ier
inter!olation_4uali-ier t.!e_4uali-ier
invariant_4uali-ier t.!e_4uali-ier
invariant_4uali-ier inter!olation_4uali-ier t.!e_4uali-ier
storage_4uali-ier:
(N*&
F&&RI8?&/ @@ Vertex onl..
VFRPIN)
/N&R(ID VFRPIN)
IN
(?&
/N&R(ID IN
/N&R(ID (?&


?NI$(R>
98
9 "&adin' (an'ua'e )ra##ar
t.!e_s!eci-ier:
t.!e_s!eci-ier_no_!rec
!recision_4uali-ier t.!e_s!eci-ier_no_!rec
t.!e_s!eci-ier_no_!rec:
t.!e_s!eci-ier_nonarra.
t.!e_s!eci-ier_nonarra. L/$&_8RFH/& RI)G&_8RFH/&
t.!e_s!eci-ier_nonarra. L/$&_8RFH/& constant_ex!ression RI)G&_8RFH/&
t.!e_s!eci-ier_nonarra.:
V(ID
$L(F&
IN&
?IN&
8((L
V/2
V/3
V/D
8V/2
8V/3
8V/D
IV/2
IV/3
IV/D
?V/2
?V/3
?V/D
>F&2
>F&3
>F&D
>F&2O2
>F&2O3
>F&2OD
>F&3O2
>F&3O3
>F&3OD
99
9 "&adin' (an'ua'e )ra##ar
>F&DO2
>F&DO3
>F&DOD
*F>=L/R1D
*F>=L/R2D
*F>=L/R3D
*F>=L/R?8/
*F>=L/R1D*GFD(Q
*F>=L/R2D*GFD(Q
*F>=L/R?8/*GFD(Q
*F>=L/R1DFRRFP
*F>=L/R2DFRRFP
*F>=L/R1DFRRFP*GFD(Q
*F>=L/R2DFRRFP*GFD(Q
I*F>=L/R1D
I*F>=L/R2D
I*F>=L/R3D
I*F>=L/R?8/
I*F>=L/R1DFRRFP
I*F>=L/R2DFRRFP
?*F>=L/R1D
?*F>=L/R2D
?*F>=L/R3D
?*F>=L/R?8/
?*F>=L/R1DFRRFP
?*F>=L/R2DFRRFP
struct_s!eci-ier
&P=/_NF>/
!recision_4uali-ier:
GI)G_=R/I*I(N
>/DI?>_=R/I*I(N
L(Q_=R/I*I(N
struct_s!eci-ier:
*&R?& ID/N&I$I/R L/$&_8RF/ struct_%eclaration_list RI)G&_8RF/
*&R?& L/$&_8RF/ struct_%eclaration_list RI)G&_8RF/
struct_%eclaration_list:
struct_%eclaration
100
9 "&adin' (an'ua'e )ra##ar
struct_%eclaration_list struct_%eclaration
struct_%eclaration:
t.!e_s!eci-ier struct_%eclarator_list */>I(L(N
struct_%eclarator_list:
struct_%eclarator
struct_%eclarator_list (>>F struct_%eclarator
struct_%eclarator:
ID/N&I$I/R
ID/N&I$I/R L/$&_8RFH/& constant_ex!ression RI)G&_8RFH/&
initiali3er:
assignment_ex!ression
%eclaration_statement:
%eclaration
statement:
com!oun%_statement
sim!le_statement
@@ )rammar Note: la,ele% statements -or *QI&G onl.5 KgotoK is not su!!orte%.
sim!le_statement:
%eclaration_statement
ex!ression_statement
selection_statement
s2itc'_statement
case_la,el
iteration_statement
9um!_statement
com!oun%_statement:
L/$&_8RF/ RI)G&_8RF/
L/$&_8RF/ statement_list RI)G&_8RF/
statement_no_ne2_sco!e:
com!oun%_statement_no_ne2_sco!e
101
9 "&adin' (an'ua'e )ra##ar
sim!le_statement
com!oun%_statement_no_ne2_sco!e:
L/$&_8RF/ RI)G&_8RF/
L/$&_8RF/ statement_list RI)G&_8RF/
statement_list:
statement
statement_list statement
ex!ression_statement:
*/>I(L(N
ex!ression */>I(L(N
selection_statement:
I$ L/$&_=FR/N ex!ression RI)G&_=FR/N selection_rest_statement
selection_rest_statement:
statement /L*/ statement
statement
con%ition:
ex!ression
-ull._s!eci-ie%_t.!e ID/N&I$I/R /N?FL initiali3er
s2itc'_statement:
*QI&G L/$&_=FR/N ex!ression RI)G&_=FR/N L/$&_8RF/ s2itc'_statement_list
RI)G&_8RF/
s2itc'_statement_list:
@L not'ing L@
statement_list
case_la,el:
F*/ ex!ression (L(N
D/$F?L& (L(N
iteration_statement:
QGIL/ L/$&_=FR/N con%ition RI)G&_=FR/N statement_no_ne2_sco!e
D( statement QGIL/ L/$&_=FR/N ex!ression RI)G&_=FR/N */>I(L(N
$(R L/$&_=FR/N -or_init_statement -or_rest_statement RI)G&_=FR/N
102
9 "&adin' (an'ua'e )ra##ar
statement_no_ne2_sco!e
-or_init_statement:
ex!ression_statement
%eclaration_statement
con%itiono!t:
con%ition
@L em!t. L@
-or_rest_statement:
con%itiono!t */>I(L(N
con%itiono!t */>I(L(N ex!ression
9um!_statement:
(N&IN?/ */>I(L(N
8R/FH */>I(L(N
R/&?RN */>I(L(N
R/&?RN ex!ression */>I(L(N
DI*FRD */>I(L(N @@ $ragment s'a%er onl..
@@ )rammar Note: No KgotoK. )otos are not su!!orte%.
translation_unit:
external_%eclaration
translation_unit external_%eclaration
external_%eclaration:
-unction_%e-inition
%eclaration
-unction_%e-inition:
-unction_!rotot.!e com!oun%_statement_no_ne2_sco!e
103
10 Issues
16 Issues
. +ow do we do $lat shading^
Alternate Resolution: Add flat =uali$ier $or var#ings.
Alternate Resolution: Add flat =uali$ier $or var#ings that o7e#s shade mode.
Resolution: Add flat, 7ut restrict it to gl>Ucolor var#ings with the same semantics as $lat in .2. In the A3I, there
is immuta7le state in the .rogram o7Mect that sets the de$ault. Also add smooth to the language $or overriding
when the de$ault is flat.
&. Rhat is the com.lete list o$ $undamental data t#.es trans.arentl# e2changea7le^
Alternate Resolution: Just $loating .oint t#.es.
Resolution: $loat, int, and 7ool.
). Are there an# cache $lushing issues around writing to 7ound .artitions^ ;G "ee issue 5.
,. !om.le2 matri2 ;G matri2 constructors.
Resolution: De$er.
(. +ow do we do two;sided coloring^ -his interacts with clam.ing, 7ecause $i2ed hardware dealing with this would
also clam..
<o language im.act.
Resolution: A two;sided ena7le goes into .rogram o7Mect as immuta7le state. It is not a link error i$ one;sided is
ena7led and 7ack;color is written. All other mis;matches are Must unde$ined.
1. Do we do clam.ing^ +ow is this determined^
<o language im.act.
Resolution: +ave immuta7le state in the .rogram o7Mect that sa#s whether color in.ut to the rasteri?er is
clam.ed. Add another $or $ragment out.ut.
5. Rhat a7out cache $lushing common 7locks^ Is it 7ound during writes^ Do we e2.ect all shader hardware to
have to write to the same resource shaders read common 7locks $rom^
Re might e2.ect se.arate resources. But, this is .art o$ a much 7igger .ro7lem that needs to 7e resolved in the
A3I.
Alternative 1: Im.lementations use A3I calls like draw to 7e res.onsi7le $or im.licitl# re$reshing the resource.
Alternative : Add an e2.licit call to sa# #ou are done changing it.
Resolution: "ee A3I s.ec. resolution.
18. 3ro7lem with 7uilt;in te2ture $unction names, how do we reduce^
; we will at least dro. the te2ture t#.e $rom the $unction name
104
10 Issues
; consider using named .arameters
; consider using .rograms to s.eci$# the looku.
11. Do we want a..lication named $ragment;shader out.uts.
Resolution: _es.
1. "hould we switch to a..lication;assigned slots $or the var#ing linkage 7etween verte2 and $ragment shaders^
Alternate Resolution: <o. Kee. doing it the wa# .2 does it.
Resolution: -his is an A3I issue. "ee A3I s.ec.
1&. !an we =uer# declared var#ings^
Resolution: _es. -his is an A3I issue. "ee A3I s.ec.
1). Are there an# sR/B im.acts to the language^
Resolution: <o. -he shader .i.e assumes linear s.ace. sR/B is in $iltering state going into the shader and in
out.ut s.ace a$ter leaving the shader.
1,. +ow do #ou declare a flat varying^ Re said the onl# varia7les that can 7e $lat are Xgl>UcolorX, 7ut those are
alread# declared.
Resolution: Just like invariant, allow a 7uilt;in varia7le to 7e redeclared as long as the redeclaration Must
adds@changes the =uali$iers. <ote we also allow redeclarations to give an arra# e2.licit si?e.
;G donOt allow 7ad redeclarations o$ 7uilt;ins
1(. "hould allow variations in the value o$ a $lat var#ing across a triangle^ A%or the case where real inter.olators are
still used and might not get e2actl# the same result $or each .i2el.B
Resolution: <o. -he value is invariant across the triangle.
11. Do .recision =uali$iers have to match^ I$ the intention is to aid .orting, not reall# save s.ace@.ower, it is
actuall# more hel.$ul i$ the# donOt have to match like the# do in the *" s.ec.
Alternate Resolution: <o. Allow .recision mismatches. -his would im.l# all .recisions are interchangea7le,
hence we could $ormali?e that and Must sa# the language recogni?es the ke#words, 7ut the# donOt have an#
meaning. -his means the s.eci$ication is ver# sim.le and eas# to understand.
;G get the most recent *" s.ec to see what it does.
Resolution: .recision =uali$iers have no semantics.
14. Does an a..lication running on mi2ed hardware have the same la#out $or a common 7lock $or all shaders the
common 7lock is in^ Di$$erent conte2ts can 7e $or di$$erent ada.ters.
Resolution: '$$sets could 7e di$$erent on di$$erent hardware. But, i$ the# are success$ull# created, then the
o$$sets will match, and common 7locks can 7e shared. %uture could consider things like P.acked commonQ or
Pvec) commonQ ...
15. Do we su..ort initiali?ers inside o$ common 7locks^ +ow does the A3I handle them^ Re alread# chose the
language s#nta2 that makes it eas# to .rovide.
Resolution: _es. %ace;to;$ace straw.oll: $or 5, against &, a7stain ,.
105
10 Issues
1. Does $lat@smooth@de$ault have to match across stages^
Alternate Resolution A. <o. *2clude $lat@smooth $rom other stageOs languages, and sa# itOs reall# a $ragment
shader control. 3ro7lem is that de.ends on im.lementation and where the inter.olation ha..ens.
Alternate Resolution B. It is inter$aces whose sides have to match, not shaders. -he out.ut o$ one stage has to
match the in.ut o$ the ne2t stage. -he in.ut to a stage does not have to match the out.ut o$ that stage. "emantics
have 7een, 7ut can 7e more clearl# stated as: the varia7le the shader is mani.ulating was co.ied in $rom the in.ut
inter$ace at the 7eginning and co.ied out to the out.ut inter$ace at the end.
Alternate Resolution !. Build on B, take inout and in and out into account, with itOs 7ene$it o$ allowing reuse o$
names. -hat means allowing 7oth Pin colorQ and Pout colorQ in the same shader to mean the same thing as Pinout
colorQ. -hen, the# can 7e decorated di$$erentl# Pin colorQ and Pout $lat colorQ without contradiction 7ecause the
decorations are modi$#ing the inter$ace, not the varia7le the shader is mani.ulating. <ote that even without this
issue we need to sa# what ha..ens with either Pinout colorQ or Pin colorC out colorQ. "ee issue ).
Resolution: Inter$aces match. In.uts and out.uts within a shader do not have to match.
. Does invariant still have to match across stages^
Resolution: _es.
&. Do we want to get rid o$ gl>De.thRange^ /etting rid o$ it onl# reall# makes sense i$ itOs the onl# remaining
state that has to 7e tracked.
Resolution: <o, kee. it.
). I$ a shader declares Pinout colorQ and then does not use the varia7le color A7ut the .revious and@or $ollowing
stage did use colorB, what ha..ens^
Alternate Resolution. -here is an im.licit co.# $rom the in.ut color to the out.ut color. <ote this is a corollar#
an#wa# o$ the idea that in.ut varia7les are co.ied in at start o$ shader and co.ied out to the out.ut at the end o$
the shader. <ote the interaction with this and 7asing set o$ var#ings o$$ o$ declarations vs. static or active use.
Resolution: Remove inout. -his also $i2es inter.olation =uali$ier am7iguities.
,. Rhat A3I mechanism do we use to do Einclude.
Alternate Resolution A: "im.le data7ase o$ names ma..ed to include strings on the server in the conte2t.
"hader has includes that use those names, and com.ile time does string su7stitutions $rom the sim.le data7ase.
Alternate Resolution B: Ro7Os .ro.osal. "imilar to A, 7ut strings are stored as Pte2t o7MectsQ. 9ore
di$$erences... see Ro7Os email.
Alternative Resolution !: 3ass in each time a ma..ing o$ names to strings thatOs used 7# that com.ile.
Alternative Resolution D:
Alternative Resolution *: <o Einclude.
Alternative Resolution %: Re;use shader o7Mects: give them names, include 7# name. Re=uires changing the
semantics o$ shader o7Mect to not .arse shaders that have a name. "haders that are the roots o$ com.ile cannot
have names.
Resolution: -his is handled 7# the A3I s.ec., through new o7Mects $or storing te2t strings.
(. Do we want to restrict switch to something sim.ler than !^
106
10 Issues
Resolution: _es. <o case inside $low control.
1. !an .recision =uali$iers 7e s.eci$ied to do nothing instead o$ *" $unctionalit#^
Resolution: leave *";like $unctionalit# to e2tensions.
&8. Are diagnostics $or included $iles identi$#ing locations as a tri.le Aname string, string num7er, line num7erB or a
.air Aname string, line num7erB. -he im.lication 7eing that the A3I sets the included te2t either as an arra# o$ strings
or a single string. Rhat does >>%I0*>> return
Resolution: As a .air. >>%I0*>> can return a string or a num7er, de.ending on the conte2t.
&. Are we allowing 7ool@int in common 7locks now, Must like we do $or the de$ault .artition^
Resolution: _es, .ut 7ool@int 7ack into common 7locks.
&,. Do we want to sa# Pcentroid inQ or Pin centroidQ, and similarl# $or smooth and $lat^ 9a# have im.lications to
e2.ressing aliasing o$ var#ing with longer chains o$ =uali$iers.
Resolution: Pcentroid inQ
&(. Rhat .acking rules go in the language s.ec.^
Resolution: It de.ends on whether $uture mechanisms $or in$luencing .acking are in the A3I or in the language.
%or now, the language s.ec. will s.eci$# the .acking rules i$ a common 7lock contains nothing 7ut vec)s.
&1. Are we removing user cli. .lanes^ <eed to remove gl>!li.Verte2.
Resolution: <o. %or 1.&8, kee. gl>!li.Verte2 with itOs .revious semantics. I$ gl>!li.Verte2 is not staticall#
written, then gl>3osition is used instead $or cli..ing against user .lanes i$ the#Ove 7een ena7led. Also, add
gl>!li.Distance.
&4. Do we need to add an# guarantees o$ .recision o$ .ass through data^ 0ike 1( 7its o$ color^
Resolution: De$erred.
&5. intAuintB and uintAintB are not conversions, the# are reinter.rets. -his is a .oor consistenc# in the language.
Resolution: -hese are constructors, not t#.e casts, which can do what the# want. In this case the# reinter.ret,
even though in other cases the# convert.
)8. +ow should the num7er o$ cli. distances 7e set^ +ow does this relate to A3I side ena7les^ +ow does it relate to
si?ing the gl>!li.Distance arra# in the language^
Resolution: 6se ena7les.
)1. Do we need to s.eci$# the .recision o$ integer division^
). Is the A3I adding su..ort $or Einclude^ I$ not, remove $rom language.
Resolution: Remove.
)&. "ome gl_VertexID descri.tion seems like it does not 7elong in the language s.ec.
Done.
)). Rhat needs to 7e said a7out trans$orm $eed7ack o$ 7uilt;in verte2 out.uts^
Resolution: <othing.
107
10 Issues
),. "till need to $inish some *" s#nchroni?ation Aa set o$ .rett# small detailsB.
)(. 0acking a .artition scheme thatOs memor# e$$icient $or the case o$ lots o$ inactive uni$orms declared in a common
7lock A.artitionB.
Resolution: -his is unnecessar#. +aving Must the de$ault .artition Anon;common glo7alsB su..ort this s.ace
o.timi?ation is su$$icient, as it is not $or sharing 7etween .rograms and o$$sets alwa#s have to 7e =ueried.
)1. <eed more clarit# on the e$$ects o$ uint vs int. It a..ears to allow 7inar# o.erations 7etween an int and a uint,
7ut doesnOt reall# sa# what ha..ens.
Resolution: <o im.licit conversionsC some o.erators are de$ined to take mi2ed o.erands, most are not.
)4. 0ist de.recated $eatures in one .lace.
Resolution. _es.
)5. "hould the token;.asting language sa# that that results o$ the token .asting are then $urther .rocessed 7# the
.re.rocessor^
Resolution: _es.
,8. $lat, no.ers.ective, centroid, smooth a2es, com7inations, etc. Is there an issue here^
,1. 7roader su..ort $or mi2ing signed and unsigned in an e2.ression
du.. o$ )1
,. de.recating gl>%rag!olor de.recates 7roadcast. Is that oka#^
Resolution: _es.
,&. Rhat does X<ot having shaders $or .rogramming all .rogramma7le stages...X mean to commands like
Draw3i2els that toda# run a $ragment shader without a verte2 shader. Do #ou have to make u. a dumm# verte2
shader^ '$ course, with $i2ed;$unction verte2 attri7utes Aand thus Raster3os attri7utesB gone, it seems like
Draw3i2els Aand Bitma.B as we know and love A^B it toda# would have to go. <ot having seen a list o$ de.recated
$eatures $or '.en/0 &.8, I have no idea i$ itOs alread# on the list.
Resolution: All .rogramma7le stages that are 7eing used must have shaders .rovided $or those stages. ADonOt
mention .rograms.B
,). "hould we remove row>maMor $rom the language.
Resolution: _es.
,,. "hould we add gl>!li.Distance as in.ut to the $ragment shader.
Resolution: _es.
,(. DonOt sa# 1. and 1.1 are not acce.ted.
Resolution: _es, acce.t 1.1 and 1..
,1. Do we have user;de$ined $lat varia7les^
Resolution: _es. %i2 s.ec. inconsistenc#. 'nl# wa# to have integer var#ings.
,4. "hould we reall# remove $unctionalit# instead o$ de.recate it^
108
10 Issues
Resolution: De.recate 7e$ore remove. Read A3I s.ec. a..endi2.
,5. !an a glo7al out redeclare the same name as a glo7al in^
Resolution: <o.
109

Das könnte Ihnen auch gefallen