Sei sulla pagina 1di 157

The OpenGL

Shading Language
Language Version: 4.00
Document Revision: 9
24-Jul-2010
Editor: John Kessenich, Intel
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
Copyright (c) 2008-2010 The Khronos Group Inc. All Rights Reserve.
This speci!ic"tion is protecte #y copyright l"$s "n cont"ins %"teri"l propriet"ry to the Khronos Group&
Inc. It or "ny co%ponents %"y not #e reprouce& repu#lishe& istri#ute& tr"ns%itte& ispl"ye&
#ro"c"st or other$ise e'ploite in "ny %"nner $ithout the e'press prior $ritten per%ission o! Khronos
Group. (ou %"y use this speci!ic"tion !or i%ple%enting the !unction"lity therein& $ithout "ltering or
re%oving "ny tr"e%"r)& copyright or other notice !ro% the speci!ic"tion& #ut the receipt or possession o!
this speci!ic"tion oes not convey "ny rights to reprouce& isclose& or istri#ute its contents& or to
%"nu!"cture& use& or sell "nything th"t it %"y escri#e& in $hole or in p"rt.
Khronos Group gr"nts e'press per%ission to "ny current *ro%oter& Contri#utor or Aopter %e%#er o!
Khronos to copy "n reistri#ute +,-./I0I1/ versions o! this speci!ic"tion in "ny !"shion& provie th"t
,. C2ARG1 is %"e !or the speci!ic"tion "n the l"test "v"il"#le up"te o! the speci!ic"tion !or "ny
version o! the A*I is use $henever possi#le. 3uch istri#ute speci!ic"tion %"y #e re-!or%"tte A3
4.,G A3 the contents o! the speci!ic"tion "re not ch"nge in "ny $"y. The speci!ic"tion %"y #e
incorpor"te into " prouct th"t is sol "s long "s such prouct inclues signi!ic"nt inepenent $or)
evelope #y the seller. A lin) to the current version o! this speci!ic"tion on the Khronos Group $e#-site
shoul #e inclue $henever possi#le $ith speci!ic"tion istri#utions.
Khronos Group %")es no& "n e'pressly iscl"i%s "ny& represent"tions or $"rr"nties& e'press or
i%plie& reg"ring this speci!ic"tion& incluing& $ithout li%it"tion& "ny i%plie $"rr"nties o! %erch"nt"#ility
or !itness !or " p"rticul"r purpose or non-in!ringe%ent o! "ny intellectu"l property. Khronos Group %")es
no& "n e'pressly iscl"i%s "ny& $"rr"nties& e'press or i%plie& reg"ring the correctness& "ccur"cy&
co%pleteness& ti%eliness& "n reli"#ility o! the speci!ic"tion. +ner no circu%st"nces $ill the Khronos
Group& or "ny o! its *ro%oters& Contri#utors or -e%#ers or their respective p"rtners& o!!icers& irectors&
e%ployees& "gents or represent"tives #e li"#le !or "ny "%"ges& $hether irect& inirect& speci"l or
conse5uenti"l "%"ges !or lost revenues& lost pro!its& or other$ise& "rising !ro% or in connection $ith
these %"teri"ls.
Khronos& .penK./1& .penK.G3& .pen6G& .pen-A7& .pen34 13 "n .pen80 "re tr"e%"r)s o!
the Khronos Group Inc. C.44A/A is " tr"e%"r) o! 3ony Co%puter 1ntert"in%ent Inc. use #y
per%ission #y Khronos. .penG4 "n .pen-4 "re registere tr"e%"r)s "n the .penG4 13 logo is "
tr"e%"r) o! 3ilicon Gr"phics Inc. use #y per%ission #y Khronos. All other prouct n"%es& tr"e%"r)s&
"n9or co%p"ny n"%es "re use solely !or ienti!ic"tion "n #elong to their respective o$ners.
2
Table of Contents
1 Introduction.................................................................................................................................1
1.1 Acknowledgents................................................................................................................ !
1.! "hanges ............................................................................................................................... !
1.!.1 "hanges #ro Revision $............................................................................................. !
1.!.! %uar& o# "hanges #ro Version '.'(..................................................................... '
1.' )verview.............................................................................................................................. *
1.* Error +andling......................................................................................................................*
1., -&.ogra.hical "onventions................................................................................................. *
1./ De.recation.......................................................................................................................... *
! )verview o# ).en01 %hading....................................................................................................,
!.1 Verte2 3rocessor.................................................................................................................. ,
!.! -essellation "ontrol 3rocessor.............................................................................................,
!.' -essellation Evaluation 3rocessor......................................................................................../
!.* 0eoetr& 3rocessor............................................................................................................. /
!., 4ragent 3rocessor.............................................................................................................. /
' Basics.......................................................................................................................................... 5
'.1 "haracter %et........................................................................................................................ 5
'.! %ource %trings...................................................................................................................... 5
'.' 3re.rocessor......................................................................................................................... $
'.* "oents.......................................................................................................................... 1'
'., -okens................................................................................................................................ 1'
'./ Ke&words............................................................................................................................1'
'.5 Identi#iers........................................................................................................................... 1,
'.$ De#initions..........................................................................................................................1/
'.$.1 %tatic 6se....................................................................................................................1/
'.$.! 6ni#or and 7on86ni#or "ontrol 4low.................................................................. 1/
'.$.' D&naicall& 6ni#or E2.ressions.............................................................................15
* Varia9les and -&.es..................................................................................................................1$
*.1 Basic -&.es........................................................................................................................ 1$
*.1.1 Void............................................................................................................................ !1
*.1.! Booleans..................................................................................................................... !1
*.1.' Integers....................................................................................................................... !!
*.1.* 4loats.......................................................................................................................... !*
*.1., Vectors........................................................................................................................!,
*.1./ :atrices...................................................................................................................... !,
*.1.5 %a.lers..................................................................................................................... !,
*.1.$ %tructures.................................................................................................................... !/
*.1.; Arra&s......................................................................................................................... !5
*.1.1( I.licit "onversions................................................................................................ !;
*.! %co.ing...............................................................................................................................'(
3
*.' %torage <uali#iers...............................................................................................................'!
*.'.1 De#ault %torage <uali#ier............................................................................................''
*.'.! "onstant <uali#ier...................................................................................................... ''
*.'.' "onstant E2.ressions................................................................................................. ''
*.'.* In.uts.......................................................................................................................... '*
*.'., 6ni#or...................................................................................................................... '/
*.'./ )ut.uts....................................................................................................................... '/
*.'.5 Inter#ace Blocks.......................................................................................................... '$
*.'.$ 1a&out <uali#iers........................................................................................................ *!
*.'.$.1 In.ut 1a&out <uali#iers....................................................................................... *!
*.'.$.! )ut.ut 1a&out <uali#iers.................................................................................... */
*.'.$.' 6ni#or Block 1a&out <uali#iers....................................................................... *;
*.'.; Inter.olation................................................................................................................,1
*.'.;.1 Redeclaring Built8in Inter.olation Varia9les in the "o.ati9ilit& 3ro#ile.........,!
*.* 3araeter <uali#iers...........................................................................................................,'
*., 3recision and 3recision <uali#iers..................................................................................... ,'
*.,.1 Range and 3recision................................................................................................... ,'
*.,.! 3recision <uali#iers.................................................................................................... ,'
*.,.' De#ault 3recision <uali#iers....................................................................................... ,*
*.,.* Availa9le 3recision <uali#iers....................................................................................,,
*./ Variance and the Invariant <uali#ier.................................................................................. ,,
*./.1 -he Invariant <uali#ier............................................................................................... ,,
*./.! Invariance o# "onstant E2.ressions........................................................................... ,/
*.5 -he 3recise <uali#ier..........................................................................................................,/
*.$ )rder o# <uali#ication........................................................................................................ ,$
, ).erators and E2.ressions........................................................................................................,;
,.1 ).erators............................................................................................................................ ,;
,.! Arra& ).erations............................................................................................................... /(
,.' 4unction "alls.................................................................................................................... /(
,.* "onstructors....................................................................................................................... /(
,.*.1 "onversion and %calar "onstructors.......................................................................... /(
,.*.! Vector and :atri2 "onstructors................................................................................. /1
,.*.' %tructure "onstructors................................................................................................ /'
,.*.* Arra& "onstructors..................................................................................................... /*
,., Vector "o.onents............................................................................................................/*
,./ :atri2 "o.onents............................................................................................................//
,.5 %tructure and Arra& ).erations..........................................................................................//
,.$ Assignents....................................................................................................................... /5
,.; E2.ressions........................................................................................................................ /$
,.1( Vector and :atri2 ).erations..........................................................................................51
/ %tateents and %tructure...........................................................................................................5'
/.1 4unction De#initions...........................................................................................................5*
4
/.1.1 4unction "alling "onventions.................................................................................... 5/
/.1.! %u9routines................................................................................................................. 5$
/.! %election............................................................................................................................. 5;
/.' Iteration.............................................................................................................................. $(
/.* Ju.s..................................................................................................................................$1
5 Built8in Varia9les......................................................................................................................$!
5.1 Built8In 1anguage Varia9les.............................................................................................. $!
5.1.1 "o.ati9ilit& 3ro#ile Built8In 1anguage Varia9les....................................................$$
5.! "o.ati9ilit& 3ro#ile Verte2 %hader Built8In In.uts......................................................... ;(
5.' Built8In "onstants.............................................................................................................. ;(
5.'.1 "o.ati9ilit& 3ro#ile Built8In "onstants....................................................................;1
5.* Built8In 6ni#or %tate....................................................................................................... ;1
5.*.1 "o.ati9ilit& 3ro#ile %tate......................................................................................... ;!
$ Built8in 4unctions..................................................................................................................... ;/
$.1 Angle and -rigonoetr& 4unctions....................................................................................;5
$.! E2.onential 4unctions........................................................................................................;;
$.' "oon 4unctions.......................................................................................................... 1((
$.* 4loating83oint 3ack and 6n.ack 4unctions..................................................................... 1(/
$., 0eoetric 4unctions........................................................................................................ 1($
$./ :atri2 4unctions.............................................................................................................. 11(
$.5 Vector Relational 4unctions.............................................................................................11!
$.$ Integer 4unctions.............................................................................................................. 11*
$.; -e2ture 4unctions.............................................................................................................11/
$.;.1 -e2ture <uer& 4unctions.......................................................................................... 115
$.;.! -e2el 1ooku. 4unctions........................................................................................... 11;
$.;.' -e2ture 0ather Instructions...................................................................................... 1!/
$.;.* -he #ollowing te2ture #unctions are de.recated....................................................... 1!;
$.1( 4ragent 3rocessing 4unctions......................................................................................1'1
$.1(.1 Derivative 4unctions.............................................................................................. 1'1
$.1(.! Inter.olation 4unctions...........................................................................................1'!
$.11 7oise 4unctions..............................................................................................................1''
$.1! 0eoetr& %hader 4unctions........................................................................................... 1',
$.1' %hader Invocation "ontrol 4unctions.............................................................................1'5
; %hading 1anguage 0raar...................................................................................................1'$
5
1 Introduction
-his docuent s.eci#ies onl& version *.(( o# the ).en01 %hading 1anguage. It re=uires >>VER%I)7>>
to su9stitute *((, and re=uires #version to acce.t onl& *((. I# #version is declared with a saller
nu9er, the language acce.ted is a .revious version o# the shading language, which will 9e su..orted
de.ending on the version and t&.e o# conte2t in the ).en01 A3I. %ee the ).en01 0ra.hics %&ste
%.eci#ication, Version *.(, #or details on what language versions are su..orted.
All ).en01 0ra.hics %&ste %.eci#ication re#erences in this s.eci#ication are to version *.(.
1
1 Introduction
1.1 Acknowledgments
-his s.eci#ication is 9ased on the work o# those who contri9uted to .ast versions o# the ).en01
1anguage %.eci#ication, the ).en01 E% !.( 1anguage %.eci#ication, and the #ollowing contri9utors to
this version:
3at Brown, 7vidia
Je## Bolt?, 7vidia
3ierre Boudier, A:D
Eric Bouaour, A:D
:urat Balci, A:D
Ignacio "astano, 7vidia
Ale2 "hal#in, A:D
3iers Daniell, 7vidia
"hris Dodd, 7vidia
Evan +art, 7vidia
Ben@ 1i.chak, A..le
Eric Aerness, 7vidia
7ick +aeel, A:D
Brent Insko, Intel
Jon 1eech
Bill 1icea8Kane, A:D
Barthold 1ichten9elt, 7vidia
Bruce :err&, AR:
Daniel Koch, -ransgaing
:aurice Ri99le, <ualco
Ian Roanick, Intel
0reg Roth, 7vidia
0raha %ellers, A:D
Dave %hreiner, AR:
Jere& %andel, A..le
Ro9ert %i.son, <ualco
1i@un <u, A:D
:ark Boung, A:D
Bun@un Chang, %'
1.2 Changes
1.2.1 Changes from Revision
"orrected a re#erence to version '.* to 9e *.(.
Reove D#i2edE #ro the descri.tion o# interpolateAtOffset, it was e2traneous.
Added samplerCubeShadow to the t&.e ta9le in section *.1. It was alread& .art o# the s.eci#ication,
@ust issing #ro this ta9le.
2
1 Introduction
1.2.2 Summar! of Changes from "ersion #.#$
7ote: 7o #eatures were reoved or de.recated 9etween versions '.'( and *.((.
-he #ollowing #eatures are added:
-essellation, which includes two new .rograa9le stages, tessellation control stage and tessellation
evaluation stage. Includes barrierFG 9uilt8in #or s&nchroni?ation.
3ol&or.hic #unctions: Run8tie selection o# what #unction gets called, through the new ke&word
subroutine.
/*9it #loating .oint nu9ers with the new t&.e ke&word double. Built8in #unctions e2tended #or
dou9les, and new #unction atching rules are added to 9oth allow i.licit conversions when calling a
#unction and .reserve ost e2isting #unction atching once dou9les are included.
:ore i.licit conversions
float to double, and siilarl& #or all #loating8.oint vector and atri2 t&.es
int to uint, and siilarl& #or all integer vector t&.es
int to double, and siilarl& #or all vectors o# integers and dou9les.
unint to double, and siilarl& #or all vectors o# integers and dou9les.
"u9e a. arra& te2tures and te2ture #unctions tetureFG, tetureSi!eFG, teture"odFG, and
teture#radFG.
%a.ler arra&s can take a varia9le inde2 now, as long as itHs value is uni#or #or all uses.
3er8sa.le shading. Including sa.le in.ut ask gl$Sample%ask&n'( and .er8sa.le
inter.olation, with e2.licit inter.olation 9uilt8ins interpolateAtCentroidFG, interpolateAtSampleFG,
and interpolateAtOffsetFG.
7ew precise =uali#ier to disallow o.tii?ations that re8order o.erations or treat di##erent instances o#
the sae o.erator with di##erent .recision.
Add a #used ulti.l& and add 9uilt8in, fmaFG, in relation to the new precise =uali#ier. FBecause Da I 9
J cE will re=uire two o.erations under new rules #or precise.G
Added new 9uilt8in #loating8.oint #unctions
frepFG and ldepFG
pack)norm*1+FG, pack)norm,-FG, packSnorm,-FG, and pack.ouble*/*FG
unpack)norm*1+FG, unpack)norm,-FG, unpackSnorm,-FG, and unpack.ouble*/*FG
Add new 9uilt8in integer #unctions
uaddCarr0FG and usub1orrowFG
umul2tendedFG and imul2tendedFG
bitfield2tractFG and bitfield&nsertFG
bitfield3everseFG
bitCountFG, find"S1FG, and find%S1FG
7ew 9uilt8in to =uer& 1)D, teture4uer0"odFG.
7ew overloaded #unction atching algorith, handling selection #ro an& valid ulti.le choices.
3
1 Introduction
-e2ture gather #unctions that return #our te2els with a single call.
teture#atherFG
teture#atherOffsetFG
teture#atherOffsetsFG
0eoetr& shading
Add streas out #ro geoetr& shader. )ut.ut can 9e directed to streas through
2mitStream5erteFG and 2ndStream6rimitiveFG.
1.# Overvie%
-his docuent descri9es The Oen!L "ha#ing Language$ version 4.00.
Inde.endent co.ilation units written in this language are called sha#ers. A rogram is a co.lete set o#
shaders that are co.iled and linked together. -he ai o# this docuent is to thoroughl& s.eci#& the
.rograing language. -he ).en01 0ra.hics %&ste %.eci#ication will s.eci#& the ).en01 entr&
.oints used to ani.ulate and counicate with .rogras and shaders.
1.& 'rror (andling
"o.ilers, in general, acce.t .rogras that are ill8#ored, due to the i.ossi9ilit& o# detecting all ill8
#ored .rogras. 3orta9ilit& is onl& ensured #or well8#ored .rogras, which this s.eci#ication
descri9es. "o.ilers are encouraged to detect ill8#ored .rogras and issue diagnostic essages, 9ut are
not re=uired to do so #or all cases. "o.ilers are re=uired to return essages regarding le2icall&,
graaticall&, or seanticall& incorrect shaders.
1.) T!pographical Conventions
Italic, 9old, and #ont choices have 9een used in this s.eci#ication .riaril& to i.rove reada9ilit&. "ode
#ragents use a #i2ed width #ont. Identi#iers e9edded in te2t are italici?ed. Ke&words e9edded in te2t
are 9old. ).erators are called 9& their nae, #ollowed 9& their s&9ol in 9old in .arentheses. -he
clari#&ing graar #ragents in the te2t use 9old #or literals and italics #or non8terinals. -he o##icial
graar in section ; D%hading 1anguage 0raarE uses all ca.itals #or terinals and lower case #or
non8terinals.
1.* +eprecation
3revious versions o# the ).en01 %hading 1anguage de.recated soe #eatures. -hese are clearl& called
out in this s.eci#ication as Dde.recatedE. -he& are still .resent in this version o# the language, 9ut are
targeted #or .otential reoval in a #uture version o# the shading language. -he ).en01 A3I has a
#orward co.ati9ilit& ode that will disallow use o# de.recated #eatures. I# co.iling in a ode where
use o# de.recated #eatures is disallowed, their use causes co.ile tie errors. %ee the ).en01 0ra.hics
%&ste %.eci#ication #or details on what causes de.recated language #eatures to 9e acce.ted or to return
an error.
4
2 Overvie% of OpenGL Shading
-he ).en01 %hading 1anguage is actuall& several closel& related languages. -hese languages are used
to create shaders #or each o# the .rograa9le .rocessors contained in the ).en01 .rocessing .i.eline.
"urrentl&, these .rocessors are the verte2, tessellation control, tessellation evaluation, geoetr&, and
#ragent .rocessors.
6nless otherwise noted in this .a.er, a language #eature a..lies to all languages, and coon usage will
re#er to these languages as a single language. -he s.eci#ic languages will 9e re#erred to 9& the nae o#
the .rocessor the& target: verte2, tessellation control, tessellation evaluation, geoetr&, or #ragent.
:ost ).en01 state is not tracked or ade availa9le to shaders. -&.icall&, user8de#ined varia9les will 9e
used #or counicating 9etween di##erent stages o# the ).en01 .i.eline. +owever, a sall aount o#
state is still tracked and autoaticall& ade availa9le to shaders, and there are a #ew 9uilt8in varia9les #or
inter#aces 9etween di##erent stages o# the ).en01 .i.eline.
2.1 "erte, -rocessor
-he verte% rocessor is a .rograa9le unit that o.erates on incoing vertices and their associated data.
"o.ilation units written in the ).en01 %hading 1anguage to run on this .rocessor are called verte%
sha#ers. Ahen a co.lete set o# verte2 shaders are co.iled and linked, the& result in a verte% sha#er
e%ecuta&le that runs on the verte2 .rocessor.
-he verte2 .rocessor o.erates on one verte2 at a tie. It does not re.lace gra.hics o.erations that re=uire
knowledge o# several vertices at a tie.
2.2 Tessellation Control -rocessor
-he tessellation control .rocessor is a .rograa9le unit that o.erates on a .atch o# incoing vertices
and their associated data, eitting a new out.ut .atch. "o.ilation units written in the ).en01 %hading
1anguage to run on this .rocessor are called tessellation control shaders. Ahen a co.lete set o#
tessellation control shaders are co.iled and linked, the& result in a tessellation control shader e2ecuta9le
that runs on the tessellation control .rocessor.
-he tessellation control shader is invoked #or each verte2 o# the out.ut .atch. Each invocation can read
the attri9utes o# an& verte2 in the in.ut or out.ut .atches, 9ut can onl& write .er8verte2 attri9utes #or the
corres.onding out.ut .atch verte2. -he shader invocations collectivel& .roduce a set o# .er8.atch
attri9utes #or the out.ut .atch. A#ter all tessellation control shader invocations have co.leted, the out.ut
vertices and .er8.atch attri9utes are asse9led to #or a .atch to 9e used 9& su9se=uent .i.eline stages.
5
2 Overview of OpenGL Shading
-essellation control shader invocation run ostl& inde.endentl&, with unde#ined relative e2ecution order.
+owever, the 9uilt8in #unction 9arrierFG can 9e used to control e2ecution order 9& s&nchroni?ing
invocations, e##ectivel& dividing tessellation control shader e2ecution into a set o# .hases. -essellation
control shaders will get unde#ined results i# one invocation reads a .er8verte2 or .er8.atch attri9ute
written 9& another invocation at an& .oint during the sae .hase, or i# two invocations atte.t to write
di##erent values to the sae .er8.atch out.ut in a single .hase.
2.# Tessellation 'valuation -rocessor
-he tessellation evaluation .rocessor is a .rograa9le unit that evaluates the .osition and other
attri9utes o# a verte2 generated 9& the tessellation .riitive generator, using a .atch o# incoing vertices
and their associated data. "o.ilation units written in the ).en01 %hading 1anguage to run on this
.rocessor are called tessellation evaluation shaders. Ahen a co.lete set o# tessellation evaluation
shaders are co.iled and linked, the& result in a tessellation evaluation shader e2ecuta9le that runs on the
tessellation evaluation .rocessor.
Each invocation o# the tessellation evaluation e2ecuta9le co.utes the .osition and attri9utes o# a single
verte2 generated 9& the tessellation .riitive generator. -he e2ecuta9le can read the attri9utes o# an&
verte2 in the in.ut .atch, .lus the tessellation coordinate, which is the relative location o# the verte2 in the
.riitive 9eing tessellated. -he e2ecuta9le writes the .osition and other attri9utes o# the verte2.
2.& Geometr! -rocessor
-he geometr' rocessor is a .rograa9le unit that o.erates on data #or incoing vertices #or a .riitive
asse9led a#ter verte2 .rocessing and out.uts a se=uence o# vertices #oring out.ut .riitives.
"o.ilation units written in the ).en01 %hading 1anguage to run on this .rocessor are called geometr'
sha#ers. Ahen a co.lete set o# geoetr& shaders are co.iled and linked, the& result in a geometr'
sha#er e%ecuta&le that runs on the geoetr& .rocessor.
A single invocation o# the geoetr& shader e2ecuta9le on the geoetr& .rocessor will o.erate on a
declared in.ut .riitive with a #i2ed nu9er o# vertices. -his single invocation can eit a varia9le
nu9er o# vertices that are asse9led into .riitives o# a declared out.ut .riitive t&.e and .assed to
su9se=uent .i.eline stages.
2.) .ragment -rocessor
-he (ragment rocessor is a .rograa9le unit that o.erates on #ragent values and their associated
data. "o.ilation units written in the ).en01 %hading 1anguage to run on this .rocessor are called
(ragment sha#ers. Ahen a co.lete set o# #ragent shaders are co.iled and linked, the& result in a
(ragment sha#er e%ecuta&le that runs on the #ragent .rocessor.
A #ragent shader cannot change a #ragentHs F%, 'G .osition. Access to neigh9oring #ragents is not
allowed. -he values co.uted 9& the #ragent shader are ultiatel& used to u.date #rae9u##er eor&
or te2ture eor&, de.ending on the current ).en01 state and the ).en01 coand that caused the
#ragents to 9e generated.
6
# /asics
#.1 Character Set
-he source character set used #or the ).en01 shading languages is a su9set o# A%"II. It includes the
#ollowing characters:
-he letters a7!, A78, and the underscore F > G.
-he nu9ers 97:.
-he s&9ols .eriod F.G, .lus F;G, dash F7G, slash F<G, asterisk F=G, .ercent F>G, angled 9rackets F? and
@G, s=uare 9rackets F ' and ( G, .arentheses F A and B G, 9races F C and D G, caret FEG, vertical 9ar F F G,
a.ersand FGG, tilde FHG, e=uals FIG, e2claation .oint FJG, colon FKG, seicolon FLG, coa F,G, and
=uestion ark FMG.
-he nu9er sign F#G #or .re.rocessor use.
Ahite s.ace: the s.ace character, hori?ontal ta9, vertical ta9, #or #eed, carriage8return, and line8
#eed.
1ines are relevant #or co.iler diagnostic essages and the .re.rocessor. -he& are terinated 9&
carriage8return or line8#eed. I# 9oth are used together, it will count as onl& a single line terination. 4or
the reainder o# this docuent, an& o# these co9inations is si.l& re#erred to as a new8line. -here is no
line continuation character.
In general, the languageKs use o# this character set is case sensitive.
-here are no character or string data t&.es, so no =uoting characters are included.
-here is no end8o#8#ile character.
#.2 Source Strings
-he source #or a single shader is an arra& o# strings o# characters #ro the character set. A single shader
is ade #ro the concatenation o# these strings. Each string can contain ulti.le lines, se.arated 9& new8
lines. 7o new8lines need 9e .resent in a stringL a single line can 9e #ored #ro ulti.le strings. 7o
new8lines or other characters are inserted 9& the i.leentation when it concatenates the strings to #or a
single shader. :ulti.le shaders can 9e linked together to #or a single .rogra.
Diagnostic essages returned #ro co.iling a shader ust identi#& 9oth the line nu9er within a string
and which source string the essage a..lies to. %ource strings are counted se=uentiall& with the #irst
string 9eing string (. 1ine nu9ers are one ore than the nu9er o# new8lines that have 9een .rocessed.
7
3 Basics
#.# -reprocessor
-here is a .re.rocessor that .rocesses the source strings as .art o# the co.ilation .rocess.
-he co.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 availa9le
defined
##
Each nu9er sign F#G can 9e .receded in its line onl& 9& s.aces or hori?ontal ta9s. It a& also 9e
#ollowed 9& s.aces and hori?ontal ta9s, .receding the directive. Each directive is terinated 9& a new8
line. 3re.rocessing does not change the nu9er or relative location o# new8lines in a source string.
-he nu9er sign F#G on a line 9& itsel# is ignored. An& directive not listed a9ove will cause a diagnostic
essage and ake the i.leentation treat the shader as ill8#ored.
#define and #undef #unctionalit& are de#ined as is standard #or "JJ .re.rocessors #or acro de#initions
9oth with and without acro .araeters.
-he #ollowing .rede#ined acros are availa9le
__LINE__
__FILE__
__VERSION__
))L*+,)) will su9stitute a decial integer constant that is one ore than the nu9er o# .receding new8
lines in the current source string.
))-*L,)) will su9stitute a decial integer constant that sa&s which source string nu9er is currentl&
9eing .rocessed.

3 Basics
))V,R"*O+)) will su9stitute a decial integer re#lecting the version nu9er o# the ).en01 shading
language. -he version o# the shading language descri9ed in this docuent will have ))V,R"*O+))
su9stitute the decial integer *((.
All acro naes containing two consecutive underscores F $$ G are reserved #or #uture use as .rede#ined
acro naes. All acro naes .re#i2ed with D01>E FD01E #ollowed 9& a single underscoreG are also
reserved.
#if, #ifdef, #ifndef, #else, #elif, and #endif are de#ined to o.erate as is standard #or "JJ .re.rocessors.
E2.ressions #ollowing #if and #elif are #urther restricted to e2.ressions o.erating on literal integer
constants, .lus identi#iers consued 9& the defined o.erator. It is an error to use #if or #elif on
e2.ressions containing unde#ined acro naes, other than as arguents to the defined o.erator.
"haracter constants are not su..orted. -he o.erators availa9le are as #ollows.
-recedence Operator class Operators 0ssociativit!
1 FhighestG .arenthetical grou.ing F G 7A
! unar& de#ined
J 8 M N
Right to 1e#t
' ulti.licative I O P 1e#t to Right
* additive J 8 1e#t to Right
, 9it8wise shi#t QQ RR 1e#t to Right
/ relational Q R QS RS 1e#t to Right
5 e=ualit& SS NS 1e#t to Right
$ 9it8wise and T 1e#t to Right
; 9it8wise e2clusive or U 1e#t to Right
1( 9it8wise inclusive or V 1e#t to Right
11 logical and TT 1e#t to Right
1! FlowestG logical inclusive or V V 1e#t to Right
-he defined o.erator can 9e used in either o# the #ollowing wa&s:
defined identifier
defined ( identifier )
-wo tokens in a acro can 9e concatenated into one token using the token .asting F##G o.erator, as is
standard #or "JJ .re.rocessors. -he result ust 9e a valid single token, which will then 9e su9@ect to
acro e2.ansion. -hat is, acro e2.ansion ha..ens onl& a#ter token .asting. -here are no other nu9er
sign 9ased o.erators Fe.g., no # or #NG, nor is there a si!eof o.erator.
-he seantics o# a..l&ing o.erators to integer literals in the .re.rocessor atch those standard in the
"JJ .re.rocessor, not those in the ).en01 %hading 1anguage.
!
3 Basics
3re.rocessor e2.ressions will 9e evaluated according to the 9ehavior o# the host .rocessor, not the
.rocessor targeted 9& the shader.
#error will cause the i.leentation to .ut a diagnostic essage into the shader o9@ectKs in#oration log
Fsection /.1.1! D%hader and 3rogra <ueriesE in the ).en01 0ra.hics %&ste %.eci#ication #or how to
access a shader o9@ectKs in#oration logG. -he essage will 9e the tokens #ollowing the #error directive,
u. to the #irst new8line. -he i.leentation ust then consider the shader to 9e ill8#ored.
#pragma allows i.leentation de.endent co.iler control. -okens #ollowing #pragma are not su9@ect
to .re.rocessor acro e2.ansion. I# an i.leentation does not recogni?e the tokens #ollowing
#pragma, then it will ignore that .raga. -he #ollowing .ragas are de#ined as .art o# the language.
#pragma ST!L
-he SO.#" .raga is used to reserve .ragas #or use 9& #uture revisions o# this language. 7o
i.leentation a& use a .raga whose #irst token is SO.#".
#pragma optimi"e#on)
#pragma optimi"e#off)
can 9e used to turn o## o.tii?ations as an aid in develo.ing and de9ugging shaders. It can onl& 9e used
outside #unction de#initions. B& de#ault, o.tii?ation is turned on #or all shaders. -he de9ug .raga
#pragma de$ug#on)
#pragma de$ug#off)
can 9e used to ena9le co.iling and annotating a shader with de9ug in#oration, so that it can 9e used
with a de9ugger. It can onl& 9e used outside #unction de#initions. B& de#ault, de9ug 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 9&
#version number profile
opt
where num&er ust 9e a version o# the language, #ollowing the sae convention as ))V,R"*O+)) a9ove.
-he directive D#version ,99E is re=uired in an& shader that uses version *.(( o# the language. An&
num&er re.resenting a version o# the language a co.iler does not su..ort will cause an error to 9e
generated. Version 1.1( o# the language does not re=uire shaders to include this directive, and shaders that
do not include a #version directive will 9e treated as targeting version 1.1(.
%haders declaring version 1.*(, 1.,(, or '.'( o# the shading language can 9e linked with shaders declaring
version *.(( in the sae .rogra. %haders targeting earlier versions F1.'( or earlierG o# the shading
language cannot 9e linked with version *.(( shaders.
1"
3 Basics
I# the o.tional ro(ile arguent is .rovided, it ust 9e the nae o# an ).en01 .ro#ile. "urrentl&, there
are two choices:
%ore
%ompati$ilit&
I# no ro(ile arguent is .rovided, the de#ault is core. 6nless otherwise s.eci#ied, this s.eci#ication is
docuenting the core .ro#ile, and ever&thing s.eci#ied #or the core .ro#ile is also availa9le in the
co.ati9ilit& .ro#ile. 4eatures s.eci#ied as 9elonging s.eci#icall& to the co.ati9ilit& .ro#ile are not
availa9le in the core .ro#ile.
-here is a 9uilt8in acro de#inition #or each .ro#ile the i.leentation su..orts. All i.leentations
.rovide the #ollowing acro:
#define !L_%ore_profile '
I.leentations .roviding the co.ati9ilit& .ro#ile .rovide the #ollowing acro:
#define !L_%ompati$ilit&_profile '
-he #version directive ust occur in a shader 9e#ore an&thing else, e2ce.t #or coents and white s.ace.
11
3 Basics
B& de#ault, co.ilers o# this language ust issue co.ile tie s&ntactic, graatical, and seantic
errors #or shaders that do not con#or to this s.eci#ication. An& e2tended 9ehavior ust #irst 9e ena9led.
Directives to control the 9ehavior o# the co.iler with res.ect to e2tensions are declared with the
#etension directive
#extension extension_name : behavior
#extension all : behavior
where e%tension)name is the nae o# an e2tension. E2tension naes are not docuented in this
s.eci#ication. -he token all eans the 9ehavior a..lies to all e2tensions su..orted 9& the co.iler. -he
&ehavior can 9e one o# the #ollowing
behavior 'ffect
re1uire Behave as s.eci#ied 9& the e2tension e%tension)name.
0ive an error on the #etension i# the e2tension e%tension)name is not
su..orted, or i# all is s.eci#ied.
enable Behave as s.eci#ied 9& the e2tension e%tension)name.
Aarn on the #etension i# the e2tension e%tension)name is not su..orted.
0ive an error on the #etension i# all is s.eci#ied.
%arn Behave as s.eci#ied 9& the e2tension e%tension)name, e2ce.t issue warnings
on an& detecta9le use o# that e2tension, unless such use is su..orted 9& other
ena9led or re=uired e2tensions.
I# all is s.eci#ied, then warn on all detecta9le uses o# an& e2tension used.
Aarn on the #etension i# the e2tension e%tension)name is not su..orted.
disable Behave Fincluding issuing errors and warningsG as i# the e2tension
e%tension)name is not .art o# the language de#inition.
I# all is s.eci#ied, then 9ehavior ust revert 9ack to that o# the non8e2tended
core version o# the language 9eing co.iled to.
Aarn on the #etension i# the e2tension e%tension)name is not su..orted.
-he etension directive is a si.le, low8level echanis to set the 9ehavior #or each e2tension. It does
not de#ine .olicies such as which co9inations are a..ro.riate, those ust 9e de#ined elsewhere. )rder
o# directives atters in setting the 9ehavior #or each e2tension: Directives that occur later override those
seen earlier. -he all variant sets the 9ehavior #or all e2tensions, overriding all .reviousl& issued
etension directives, 9ut onl& #or the &ehaviors warn and disable.
12
3 Basics
-he initial state o# the co.iler is as i# the directive
#extension all ( disa$le
was issued, telling the co.iler that all error and warning re.orting ust 9e done according to this
s.eci#ication, ignoring an& e2tensions.
Each e2tension can de#ine its allowed granularit& o# sco.e. I# nothing is said, the granularit& is a shader
Fthat is, a single co.ilation unitG, and the e2tension directives ust occur 9e#ore an& non8.re.rocessor
tokens. I# necessar&, the linker can en#orce granularities larger than a single co.ilation unit, in which
case each involved shader will have to contain the necessar& e2tension directive.
:acro e2.ansion is not done on lines containing #etension and #version directives.
#line ust have, a#ter acro su9stitution, one o# the #ollowing #ors:
#line line
#line line source-string-number
where line and source-string-num&er are constant integer e2.ressions. A#ter .rocessing this directive
Fincluding its new8lineG, the i.leentation will 9ehave as i# it is co.iling at line nu9er line and
source string nu9er source-string-num&er. %u9se=uent source strings will 9e nu9ered se=uentiall&,
until another #line directive overrides that nu9ering.
#.& Comments
"oents are deliited 9& OI and IO, or 9& OO and a new8line. -he 9egin coent deliiters FOI or OOG are
not recogni?ed as coent deliiters inside o# a coent, hence coents cannot 9e nested. I# a
coent resides entirel& within a single line, it is treated s&ntacticall& as a single s.ace. 7ew8lines are
not eliinated 9& coents.
#.) To2ens
-he language is a se=uence o# tokens. A token can 9e
to.en:
.e'/or#
i#enti(ier
integer-constant
(loating-constant
oerator
L C D
#.* 3e!%ords
-he #ollowing are the ke&words in the language, and cannot 9e used #or an& other .ur.ose than that
de#ined 9& this docuent:
attribute const uniform var0ing
la0out
13
3 Basics
centroid flat smooth noperspective
patch sample
break continue do for while switch case default
if else
subroutine
in out inout
float double int void bool true false
invariant
discard return
mat* mat/ mat, dmat* dmat/ dmat,
mat** mat*/ mat*, dmat** dmat*/ dmat*,
mat/* mat// mat/, dmat/* dmat// dmat/,
mat,* mat,/ mat,, dmat,* dmat,/ dmat,,
vec* vec/ vec, ivec* ivec/ ivec, bvec* bvec/ bvec, dvec* dvec/ dvec,
uint uvec* uvec/ uvec,
lowp mediump highp precision
sampler1. sampler*. sampler/. samplerCube
sampler1.Shadow sampler*.Shadow samplerCubeShadow
sampler1.Arra0 sampler*.Arra0
sampler1.Arra0Shadow sampler*.Arra0Shadow
isampler1. isampler*. isampler/. isamplerCube
isampler1.Arra0 isampler*.Arra0
usampler1. usampler*. usampler/. usamplerCube
usampler1.Arra0 usampler*.Arra0
sampler*.3ect sampler*.3ectShadow isampler*.3ect usampler*.3ect
sampler1uffer isampler1uffer usampler1uffer
sampler*.%S isampler*.%S usampler*.%S
sampler*.%SArra0 isampler*.%SArra0 usampler*.%SArra0
samplerCubeArra0 samplerCubeArra0Shadow isamplerCubeArra0 usamplerCubeArra0
struct
14
3 Basics
-he #ollowing are the ke&words reserved #or #uture use. 6sing the will result in an error:
common partition active
asm
class union enum t0pedef template this packed
goto
inline noinline volatile public static etern eternal interface
long short half fied unsigned superp
input output
hvec* hvec/ hvec, fvec* fvec/ fvec,
sampler/.3ect
filter
image1. image*. image/. imageCube
iimage1. iimage*. iimage/. iimageCube
uimage1. uimage*. uimage/. uimageCube
image1.Arra0 image*.Arra0
iimage1.Arra0 iimage*.Arra0 uimage1.Arra0 uimage*.Arra0
image1.Shadow image*.Shadow
image1.Arra0Shadow image*.Arra0Shadow
image1uffer iimage1uffer uimage1uffer
si!eof cast
namespace using
row$maPor
In addition, all identi#iers containing two consecutive underscores F$$G are reserved as .ossi9le #uture
ke&words.
#.4 Identifiers
Identi#iers are used #or varia9le naes, #unction naes, structure naes, and #ield selectors F#ield
selectors select co.onents o# vectors and atrices siilar to structure #ields, as discussed in section ,.,
DVector "o.onentsE and section ,./ D:atri2 "o.onentsE G. Identi#iers have the #or
i#enti(ier
non#igit
i#enti(ier non#igit
i#enti(ier #igit
15
3 Basics
non#igit: one o#
$ a b c d e f g h i P k l m n o p Q r s t u v w 0 !
A 1 C . 2 R # S & T U " % V O 6 4 3 S O ) 5 W X Y 8
#igit: one o#
9 1 * / , Z + [ - :
Identi#iers starting with Dgl>E are reserved #or use 9& ).en01, and a& not 9e declared in a shader as
either a varia9le or a #unction. +owever, as noted in the s.eci#ication, there are soe cases where
.reviousl& declared varia9les can 9e redeclared to change or add soe .ro.ert&, and .redeclared Wgl>W
naes are allowed to 9e redeclared in a shader onl& #or these s.eci#ic .ur.oses. :ore generall&, it is an
error to redeclare a varia9le, including those starting Dgl>E.
#. +efinitions
%oe language rules descri9ed 9elow de.end on the #ollowing de#initions.
#..1 Static 5se
A shader contains a static use o# For static assignment toG a varia9le % i#, a#ter .re.rocessing, the shader
contains a stateent that would read For writeG %, whether or not run8tie #low o# control will cause that
stateent to 9e e2ecuted.
#..2 5niform and 6on75niform Control .lo%
Ahen e2ecuting stateents in a #ragent shader, control #low starts as uni(orm control (lo/L all #ragents
enter the sae control .ath into mainFG. "ontrol #low 9ecoes non-uni(orm when di##erent #ragents
take di##erent .aths through control8#low stateents Fselection, iteration, and @u.sG. "ontrol #low
su9se=uentl& returns to 9eing uni#or a#ter such divergent su98stateents or ski..ed code co.letes,
until the ne2t tie di##erent control .aths are taken.
4or e2a.le:
main#)
)
float a * +++,-- t.is is uniform flo/ %ontrol
if #a 0 $) ) -- t.is expression is true for some fragments1 not all
++++, -- non2uniform flo/ %ontrol
3 else )
++++, -- non2uniform flo/ %ontrol
3
++++, -- uniform flo/ %ontrol again
3
)ther e2a.les o# non8uni#or #low control can occur within switch stateents and a#ter conditional
9reaks, continues, earl& returns, and a#ter #ragent discards, when the condition is true #or soe
#ragents 9ut not others. 1oo. iterations that onl& soe #ragents e2ecute are also non8uni#or #low
control.
16
3 Basics
-his is siilarl& de#ined #or other shader stages, 9ased on the .er8instance data ites the& .rocess.
#..# +!namicall! 5niform ',pressions
A #ragent8shader e2.ression is #'namicall' uni(orm i# all #ragents evaluating it get the sae resulting
value. Ahen loo.s are involved, this re#ers to the e2.ressionHs value #or the sae loo. iteration. Ahen
#unctions are involved, this re#ers to calls #ro the sae call .oint.
-his is siilarl& de#ined #or other shader stages, 9ased on the .er8instance data the& .rocess.
7ote that constant e2.ressions are triviall& d&naicall& uni#or. It #ollows that t&.ical loo. counters
9ased on these are also d&naicall& uni#or.
17
& "ariables and T!pes
All varia9les and #unctions ust 9e declared 9e#ore 9eing used. Varia9le and #unction naes are
identi#iers.
-here are no de#ault t&.es. All varia9le and #unction declarations ust have a declared t&.e, and
o.tionall& =uali#iers. A varia9le is declared 9& s.eci#&ing its t&.e #ollowed 9& one or ore naes
se.arated 9& coas. In an& cases, a varia9le can 9e initiali?ed as .art o# its declaration 9& using the
assignent o.erator FIG. -he graar near the end o# this docuent .rovides a #ull re#erence #or the
s&nta2 o# declaring varia9les.
6ser8de#ined t&.es a& 9e de#ined using struct to aggregate a list o# e2isting t&.es into a single nae.
-he ).en01 %hading 1anguage is t&.e sa#e. -here are no i.licit conversions 9etween t&.es, with the
e2ce.tion that an integer value a& a..ear where a #loating8.oint t&.e is e2.ected, and 9e converted to a
#loating8.oint value. E2actl& how and when this can occur is descri9ed in section *.1.1( DI.licit
"onversionsE and as re#erenced 9& other sections in this s.eci#ication.
&.1 /asic T!pes
-he ).en01 %hading 1anguage su..orts the #ollowing 9asic data t&.es, grou.ed as #ollows.
-rans.arent t&.es
T!pe 8eaning
void #or #unctions that do not return a value
bool a conditional t&.e, taking on values o# true or #alse
int a signed integer
uint an unsigned integer
float a single #loating8.oint scalar
double a single dou9le8.recision #loating .oint scalar
vec* a two8co.onent #loating8.oint vector
vec/ a three8co.onent #loating8.oint vector
vec, a #our8co.onent #loating8.oint vector
dvec* a two8co.onent dou9le .recision #loating8.oint vector
dvec/ a three8co.onent dou9le .recision #loating8.oint vector
dvec, a #our8co.onent dou9le .recision #loating8.oint vector
bvec* a two8co.onent Boolean vector
bvec/ a three8co.onent Boolean vector
1
4 #aria$%es and &'pes
T!pe 8eaning
bvec, a #our8co.onent Boolean vector
ivec* a two8co.onent signed integer vector
ivec/ a three8co.onent signed integer vector
ivec, a #our8co.onent signed integer vector
uvec* a two8co.onent unsigned integer vector
uvec/ a three8co.onent unsigned integer vector
uvec, a #our8co.onent unsigned integer vector
mat* a !X! #loating8.oint atri2
mat/ a 'X' #loating8.oint atri2
mat, a *X* #loating8.oint atri2
mat** sae as a mat*
mat*/ a #loating8.oint atri2 with ! coluns and ' rows
mat*, a #loating8.oint atri2 with ! coluns and * rows
mat/* a #loating8.oint atri2 with ' coluns and ! rows
mat// sae as a mat/
mat/, a #loating8.oint atri2 with ' coluns and * rows
mat,* a #loating8.oint atri2 with * coluns and ! rows
mat,/ a #loating8.oint atri2 with * coluns and ' rows
mat,, sae as a mat,
dmat* a !X! dou9le8.recision #loating8.oint atri2
dmat/ a 'X' dou9le8.recision #loating8.oint atri2
dmat, a *X* dou9le8.recision #loating8.oint atri2
dmat** sae as a dmat*
dmat*/ a dou9le8.recision #loating8.oint atri2 with ! coluns and ' rows
dmat*, a dou9le8.recision #loating8.oint atri2 with ! coluns and * rows
dmat/* a dou9le8.recision #loating8.oint atri2 with ' coluns and ! rows
dmat// sae as a dmat/
dmat/, a dou9le8.recision #loating8.oint atri2 with ' coluns and * rows
dmat,* a dou9le8.recision #loating8.oint atri2 with * coluns and ! rows
dmat,/ a dou9le8.recision #loating8.oint atri2 with * coluns and ' rows
dmat,, sae as a dmat,
1!
4 #aria$%es and &'pes
4loating 3oint %a.ler -&.es Fo.a=ueG
T!pe 8eaning
sampler1. a handle #or accessing a 1D te2ture
sampler*. a handle #or accessing a !D te2ture
sampler/. a handle #or accessing a 'D te2ture
samplerCube a handle #or accessing a cu9e a..ed te2ture
sampler*.3ect a handle #or accessing a rectangular te2ture
sampler1.Shadow a handle #or accessing a 1D de.th te2ture with co.arison
sampler*.Shadow a handle #or accessing a !D de.th te2ture with co.arison
sampler*.3ectShadow a handle #or accessing a rectangular te2ture with co.arison
sampler1.Arra0 a handle #or accessing a 1D arra& te2ture
sampler*.Arra0 a handle #or accessing a !D arra& te2ture
sampler1.Arra0Shadow a handle #or accessing a 1D arra& de.th te2ture with co.arison
sampler*.Arra0Shadow a handle #or accessing a !D arra& de.th te2ture with co.arison
sampler1uffer a handle #or accessing a 9u##er te2ture
sampler*.%S a handle #or accessing a !D ulti8sa.le te2ture
sampler*.%SArra0 a handle #or accessing a !D ulti8sa.le arra& te2ture
samplerCubeShadow a handle #or accessing a cu9e a. de.th te2ture with co.arison
samplerCubeArra0 a handle #or accessing a cu9e a. arra& te2ture
samplerCubeArra0Shadow a handle #or accessing a cu9e a. arra& de.th te2ture with
co.arison
%igned Integer %a.ler -&.es Fo.a=ueG
T!pe 8eaning
isampler1. a handle #or accessing an integer 1D te2ture
isampler*. a handle #or accessing an integer !D te2ture
isampler/. a handle #or accessing an integer 'D te2ture
isamplerCube a handle #or accessing an integer cu9e a..ed te2ture
isampler*.3ect a handle #or accessing an integer !D rectangular te2ture
isampler1.Arra0 a handle #or accessing an integer 1D arra& te2ture
isampler*.Arra0 a handle #or accessing an integer !D arra& te2ture
isampler1uffer a handle #or accessing an integer 9u##er te2ture
isampler*.%S a handle #or accessing an integer !D ulti8sa.le te2ture
isampler*.%SArra0 a handle #or accessing an integer !D ulti8sa.le arra& te2ture
2"
4 #aria$%es and &'pes
T!pe 8eaning
isamplerCubeArra0 a handle #or accessing an integer cu9e a. arra& te2ture
6nsigned Integer %a.ler -&.es Fo.a=ueG
T!pe 8eaning
usampler1. a handle #or accessing an unsigned integer 1D te2ture
usampler*. a handle #or accessing an unsigned integer !D te2ture
usampler/. a handle #or accessing an unsigned integer 'D te2ture
usamplerCube a handle #or accessing an unsigned integer cu9e a..ed te2ture
usampler*.3ect a handle #or accessing an unsigned integer rectangular te2ture
usampler1.Arra0 a handle #or accessing an unsigned integer 1D arra& te2ture
usampler*.Arra0 a handle #or accessing an unsigned integer !D arra& te2ture
usampler1uffer a handle #or accessing an unsigned integer 9u##er te2ture
usampler*.%S a handle #or accessing an unsigned integer !D ulti8sa.le te2ture
usampler*.%SArra0 a handle #or accessing an unsigned integer !D ulti8sa.le te2ture
arra&
usamplerCubeArra0 a handle #or accessing an unsigned integer cu9e a. arra& te2ture
In addition, a shader can aggregate these using arra&s and structures to 9uild ore co.le2 t&.es.
-here are no .ointer t&.es.
&.1.1 "oid
4unctions that do not return a value ust 9e declared as void. -here is no de#ault #unction return t&.e.
-he ke&word void cannot 9e used in an& other declarations Fe2ce.t #or e.t& #oral or actual .araeter
listsG.
&.1.2 /ooleans
-o ake 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 varia9les o# this t&.e. It is a genuine Boolean t&.e, holding
onl& one o# two values eaning either true or #alse. -wo ke&words true and false can 9e used as literal
Boolean constants. Booleans are declared and o.tionall& initiali?ed as in the #ollow e2a.le:
$ool su%%ess, -- de%lare 4su%%ess5 to $e a 6oolean
$ool done * false, -- de%lare and initiali"e 4done5
-he right side o# the assignent o.erator F I G ust 9e an e2.ression whose t&.e is bool.
E2.ressions used #or conditional @u.s Fif, for, MK, while, do7whileG ust evaluate to the t&.e bool.
21
4 #aria$%es and &'pes
&.1.# Integers
%igned and unsigned integer varia9les are #ull& su..orted. In this docuent, the ter integer is eant to
generall& include 9oth signed and unsigned integers. 6nsigned integers have e2actl& '! 9its o# .recision.
%igned integers use '! 9its, including a sign 9it, in twoHs co.leent #or. ).erations resulting in
over#low or under#low will not cause an& e2ce.tion, nor will the& saturate, rather the& will Dwra.E to &ield
the low8order '! 9its o# the result.
Integers are declared and o.tionall& initiali?ed with integer e2.ressions, as in the #ollowing e2a.le:
int i1 7 * 89, -- default integer literal t&pe is int
uint : * ;u, -- 4u5 esta$lis.es t.e t&pe as uint
22
4 #aria$%es and &'pes
1iteral integer constants can 9e e2.ressed in decial F9ase 1(G, octal F9ase $G, or he2adecial F9ase 1/G
as #ollows.
integer-constant :
#ecimal-constant integer-su((i%ot
octal-constant integer-su((i%ot
he%a#ecimal-constant integer-su((i%ot
integer-su((i%: one o#
u )
#ecimal-constant :
non0ero-#igit
#ecimal-constant #igit
octal-constant :
9
octal-constant octal-#igit
he%a#ecimal-constant :
(2 he%a#ecimal-#igit
(Y he%a#ecimal-#igit
he%a#ecimal-constant he%a#ecimal-#igit
#igit :
9
non0ero-#igit
non0ero-#igit : one o#
1 * / , Z + [ - :
octal-#igit K one o#
9 1 * / , Z + [
he%a#ecimal-#igit K one o#
9 1 * / , Z + [ - :
a b c d e f
A 1 C . 2 R
7o white s.ace is allowed 9etween the digits o# an integer constant, including a#ter the leading 9 or a#ter
the leading 9 or 9X o# a constant, or 9e#ore the su##i2 u or ). Ahen the su##i2 u or ) is .resent, the
literal has t&.e uint, otherwise the t&.e is int. A leading unar& inus sign F8G is inter.reted as an
arithetic unar& negation, not as .art o# the constant.
It is an error to .rovide a literal integer whose agnitude is too large to store in a varia9le o# atching
signed or unsigned t&.e.
23
4 #aria$%es and &'pes
&.1.& .loats
%ingle8.recision and dou9le8.recision #loating .oint varia9les are availa9le #or use in a variet& o# scalar
calculations. 4loating8.oint varia9les are de#ined as in the #ollowing e2a.le:
float a1 $ * '+<,
dou$le %1 d * 9+=LF,
As an in.ut value to one o# the .rocessing units, a single8.recision or dou9le8.recision #loating8.oint
varia9le is e2.ected to atch the corres.onding IEEE 5,* #loating8.oint de#inition #or .recision and
d&naic range. 4loating8.oint varia9les within a shader are also encoded according to the IEEE 5,*
s.eci#ication #or single8.recision #loating8.oint values. +owever, it is not re=uired that the .recision o#
internal .rocessing atch the IEEE 5,* #loating8.oint s.eci#ication #or #loating8.oint o.erations, 9ut the
guidelines #or .recision esta9lished 9& the ).en01 0ra.hics %&ste %.eci#ication ust 9e et.
%iilarl&, treatent o# conditions such as divide 9& ( a& lead to an uns.eci#ied result, 9ut in no case
should such a condition lead to the interru.tion or terination o# .rocessing. 0enerall&, there are no
signaling 7a7s, and o.erating on 7a7s F7ot a 7u9erG or in#s F.ositive or negative in#initiesG gives
unde#ined results.
4loating8.oint constants are de#ined as #ollows.
(loating-constant :
(ractional-constant e%onent-art
ot
(loating-su((i%ot

#igit-se1uence e%onent-art (loating-su((i%ot
(ractional-constant :
#igit-se1uence . #igit-se1uence
#igit-se1uence .
. #igit-se1uence
e%onent-art :
e sign
ot
#igit-se1uence
E sign
ot
#igit-se1uence
sign : one o#
; \
#igit-se1uence :
#igit
#igit-se1uence #igit
(loating-su((i%: one o#
f R lf "R
A decial .oint F . G is not needed i# the e2.onent .art is .resent. 7o white s.ace a& a..ear an&where
within a #loating8.oint constant, including 9e#ore a su##i2. Ahen the su##i2 Wl#W or W14W is .resent, the
literal has t&.e double. )therwise, the literal has t&.e float. A leading unar& inus sign F7G is inter.reted
as a unar& o.erator and is not .art o# the #loating8.oint constant
24
4 #aria$%es and &'pes
&.1.) "ectors
-he ).en01 %hading 1anguage includes data t&.es #or generic !8, '8, and *8co.onent vectors o#
#loating8.oint values, integers, or Booleans. 4loating8.oint vector varia9les can 9e used to store colors,
norals, .ositions, te2ture coordinates, te2ture looku. results and the like. Boolean vectors can 9e used
#or co.onent8wise co.arisons o# nueric vectors. %oe e2a.les o# vector declaration are:
ve%9 tex%oord'1 tex%oord9,
ve%; position,
ve%8 m&R!6>,
ive%9 textureLoo:up,
$ve%; less,
Initiali?ation o# vectors can 9e done with constructors, which are discussed shortl&.
&.1.* 8atrices
-he ).en01 %hading 1anguage has 9uilt8in t&.es #or !X!, !X', !X*, 'X!, 'X', 'X*, *X!, *X', and *X*
atrices o# #loating8.oint nu9ers. :atri2 t&.es 9eginning with WatW have single8.recision co.onents
while atri2 t&.es 9eginning with WdatW have dou9le8.recision co.onents. -he #irst nu9er in the
t&.e is the nu9er o# coluns, the second is the nu9er o# rows. I# there is onl& one nu9er, the atri2
is s=uare. E2a.le atri2 declarations:
mat9 mat9,
mat; opt?atrix,
mat8 vie/1 pro7e%tion,
mat8x8 vie/, -- an alternate /a& of de%laring a mat8
mat;x9 m, -- a matrix /it. ; %olumns and 9 ro/s
dmat8 .ig.@re%ision?V@,
dmat9x8 dm,
Initiali?ation o# atri2 values is done with constructors Fdescri9ed in section ,.* D"onstructorsE G in
colun8a@or order.
&.1.4 Samplers
%a.ler t&.es Fe.g., sampler*.G are e##ectivel& o.a=ue handles to te2tures and their #ilters. -he& are
used with the 9uilt8in te2ture #unctions Fdescri9ed in section $.5 D-e2ture 1ooku. 4unctionsE G to s.eci#&
which te2ture to access and how it is to 9e #iltered. -he& can onl& 9e declared as #unction .araeters or
uniform varia9les Fsee section *.'., D6ni#orE G. E2ce.t #or arra& inde2ing, structure #ield selection, and
.arentheses, sa.lers are not allowed to 9e o.erands in e2.ressions. %a.lers aggregated into arra&s
within a shader Fusing s=uare 9rackets ' (G can onl& 9e inde2ed with a d&naicall& uni#or integral
e2.ression, otherwise results are unde#ined. %a.lers cannot 9e treated as l8valuesL hence cannot 9e used
as out or inout #unction .araeters, nor can the& 9e assigned into. As uni#ors, the& are initiali?ed onl&
with the ).en01 A3IL the& cannot 9e declared with an initiali?er in a shader. As #unction .araeters,
onl& sa.lers a& 9e .assed to sa.lers o# atching t&.e. -his ena9les consistenc& checking 9etween
shader te2ture accesses and ).en01 te2ture state 9e#ore a shader is run.
25
4 #aria$%es and &'pes
&.1. Structures
6ser8de#ined t&.es can 9e created 9& aggregating other alread& de#ined t&.es into a structure using the
struct ke&word. 4or e2a.le,
stru%t lig.t )
float intensit&,
ve%; position,
3 lig.tVar,
In this e2a.le, light 9ecoes the nae o# the new t&.e, and lightVar 9ecoes a varia9le o# t&.e light.
-o declare varia9les o# the new t&.e, use its nae Fwithout the ke&word structG.
lig.t lig.tVar9,
:ore #orall&, structures are declared as #ollows. +owever, the co.lete correct graar is as given in
section ; D%hading 1anguage 0raarE .
struct-#e(inition :
1uali(ier
ot
struct name
ot
C mem&er-list D #eclarators
ot
2
mem&er-list :
mem&er-#eclaration2
mem&er-#eclaration mem&er-list2
mem&er-#eclaration :
&asic-t'e #eclarators2
where name 9ecoes the user8de#ined t&.e, and can 9e used to declare varia9les to 9e o# this new t&.e.
-he name shares the sae nae s.ace as other varia9les, t&.es, and #unctions. All .reviousl& visi9le
varia9les, t&.es, constructors, or #unctions with that nae are hidden. -he o.tional 1uali(ier onl& a..lies
to an& #eclarators, and is not .art o# the t&.e 9eing de#ined #or name.
%tructures ust have at least one e9er declaration. :e9er declarators a& contain .recision
=uali#iers, 9ut a& not contain an& other =uali#iers. Bit #ields are not su..orted. :e9er t&.es ust 9e
alread& de#ined Fthere are no #orward re#erencesG. :e9er declarations cannot contain initiali?ers.
:e9er declarators can contain arra&s. %uch arra&s ust have a si?e s.eci#ied, and the si?e ust 9e an
integral constant e2.ression thatHs greater than ?ero Fsee section *.'.' D"onstant E2.ressionsEG. Each
level o# structure has its own nae s.ace #or naes given in e9er declaratorsL such naes need onl&
9e uni=ue within that nae s.ace.
26
4 #aria$%es and &'pes
Anon&ous structures are not su..orted. E9edded structure de#initions are not su..orted.
stru%t S ) float f, 3,
stru%t T )
S, -- Error( anon&mous stru%tures disallo/ed
stru%t ) +++ 3, -- Error( em$edded stru%tures disallo/ed
S s, -- O:a&( nested stru%tures /it. name are allo/ed
3,
%tructures can 9e initiali?ed at declaration tie using constructors, as discussed in section ,.*.' D%tructure
"onstructorsE .
&.1.9 0rra!s
Varia9les o# the sae t&.e can 9e aggregated into arra&s 9& declaring a nae #ollowed 9& 9rackets F ' ( G
enclosing an o.tional si?e. Ahen an arra& si?e is s.eci#ied in a declaration, it ust 9e an integral constant
e2.ression Fsee section *.'.' D"onstant E2.ressionsE G 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 arguent to a #unction,
then its si?e ust 9e declared 9e#ore an& such use. It is legal to declare an arra& without a si?e and then
later re8declare the sae nae as an arra& o# the sae t&.e and s.eci#& a si?e. It is illegal to declare an
arra& with a si?e, and then later Fin the sae shaderG inde2 the sae arra& with an integral constant
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 #oral .araeters in a #unction declaration ust s.eci#& a si?e.
6nde#ined 9ehavior results #ro inde2ing an arra& with a non8constant e2.ression thatKs greater than or
e=ual to the arra&Ks si?e or less than (. )nl& one8diensional arra&s a& 9e declared. All 9asic t&.es and
structures can 9e #ored into arra&s. %oe e2a.les are:
float freAuen%iesB;C,
uniform ve%8 lig.t@ositionB8C,
lig.t lig.tsBC,
%onst int numLig.ts * 9,
lig.t lig.tsBnumLig.tsC,
An arra& t&.e can 9e #ored 9& s.eci#&ing a t&.e #ollowed 9& s=uare 9rackets FZ [G and including a si?e:
floatB<C
-his t&.e can 9e used an&where an& other t&.e can 9e used, including as the return value #ro a #unction
floatB<C foo#) ) 3
as a constructor o# an arra&
floatB<C#;+81 8+91 <+=1 <+91 '+')
as an unnaed .araeter
void foo#floatB<C)
27
4 #aria$%es and &'pes
and as an alternate wa& o# declaring a varia9le or #unction .araeter.
floatB<C a,
It is an error to declare arra&s o# arra&s:
float aB<CB;C, -- illegal
floatB<C aB;C, -- illegal
Arra&s can have initiali?ers #ored #ro arra& constructors:
float aB<C * floatB<C#;+81 8+91 <+=1 <+91 '+'),
float aB<C * floatBC#;+81 8+91 <+=1 <+91 '+'), -- same t.ing
6nsi?ed arra&s can 9e e2.licitl& si?ed 9& an initiali?er at declaration tie:
float aB<C,
+++
float $BC * a, -- $ is expli%itl& si"e <
float $B<C * a, -- means t.e same t.ing
+owever, i.licitl& si?ed arra&s cannot 9e assigned to. 7ote, this is a rare case that initiali?ers and
assignents a..ear to have di##erent seantics.
Arra&s know the nu9er o# eleents the& contain. -his can 9e o9tained 9& using the length ethod:
a+lengt.#), -- returns < for t.e a$ove de%larations
-he length ethod cannot 9e called on an arra& that has not 9een e2.licitl& si?ed.
2
4 #aria$%es and &'pes
&.1.1$ Implicit Conversions
In soe situations, an e2.ression and its t&.e will 9e i.licitl& converted to a di##erent t&.e. -he
#ollowing ta9le shows all allowed i.licit conversions:
T!pe of e,pression Can be implicitl! converted to
int uint
int
uint
float
int
uint
float
double
ivec* uvec*
ivec/ uvec/
ivec, uvec,
ivec*
uvec*
vec*
ivec/
uvec/
vec/
ivec,
uvec,
vec,
ivec*
uvec*
vec*
dvec*
ivec/
uvec/
vec/
dvec/
ivec,
uvec,
vec,
dvec,
mat* dmat*
mat/ dmat/
mat, dmat,
mat*/ dmat*/
mat*, dmat*,
mat/* dmat/*
mat/, dmat/,
mat,* dmat,*
mat,/ dmat,/
2!
4 #aria$%es and &'pes
-here are no i.licit arra& or structure conversions. 4or e2a.le, an arra& o# int cannot 9e i.licitl&
converted to an arra& o# float.
Ahen an i.licit conversion is done, it is not a re8inter.retation o# the e2.ressionHs 9it .attern, 9ut a
conversion o# its value to an e=uivalent value in the new t&.e. 4or e2a.le, the integer value 8Z will 9e
converted to the #loating8.oint value 8Z.9. Integer values having ore 9its o# .recision than a #loating
.oint antissa will lose .recision when converted to float.
Ahen .er#oring i.licit conversion #or 9inar& o.erators, there a& 9e ulti.le data t&.es to which the
two o.erands can 9e converted. 4or e2a.le, when adding an int value to a uint value, 9oth values can
9e i.licitl& converted to uint, float, and double. In such cases, a #loating8.oint t&.e is chosen i# either
o.erand has a #loating8.oint t&.e. )therwise, an unsigned integer t&.e is chosen i# either o.erand has an
unsigned integer t&.e. )therwise, a signed integer t&.e is chosen. I# o.erands can 9e i.licitl& converted
to ulti.le data t&.es deriving #ro the sae 9ase data t&.e, the t&.e with the sallest co.onent si?e is
used.
-he conversions in the ta9le a9ove are done onl& as indicated 9& other sections o# this s.eci#ication.
&.2 Scoping
-he sco.e o# a varia9le is deterined 9& where it is declared. I# it is declared outside all #unction
de#initions, it has glo9al sco.e, which starts #ro where it is declared and .ersists to the end o# the shader
it is declared in. I# it is declared in a while test or a for stateent, then it is sco.ed to the end o# the
#ollowing su98stateent. )therwise, i# it is declared as a stateent within a co.ound stateent, it is
sco.ed to the end o# that co.ound stateent. I# it is declared as a .araeter in a #unction de#inition, it is
sco.ed until the end o# that #unction de#inition. A #unction 9od& has a sco.e nested inside the #unctionKs
de#inition. -he if stateentKs e2.ression does not allow new varia9les to 9e declared, hence does not
#or a new sco.e.
3"
4 #aria$%es and &'pes
Aithin a declaration, the sco.e o# a nae starts iediatel& a#ter the initiali?er i# .resent or iediatel&
a#ter the nae 9eing declared i# not. %everal e2a.les:
int x * ',
)
int x * 91 & * x, -- & is initiali"ed to 9
3
stru%t S
)
int x,
3,
)
S S * S#=), -- DSD is onl& visi$le as a stru%t and %onstru%tor
S, -- DSD is no/ visi$le as a varia$le
3
int x * x, -- Error if x .as not $een previousl& defined+
All varia9le naes, structure t&.e naes, and #unction naes in a given sco.e share the sae nae s.ace.
4unction naes can 9e redeclared in the sae sco.e, with the sae or di##erent .araeters, without error.
An i.licitl& si?ed arra& can 9e re8declared in the sae sco.e as an arra& o# the sae 9ase t&.e.
)therwise, within one co.ilation unit, a declared nae cannot 9e redeclared in the sae sco.eL doing so
results in a redeclaration error. I# a nested sco.e redeclares a nae used in an outer sco.e, it hides all
e2isting uses o# that nae. -here is no wa& to access the hidden nae or ake it unhidden, without
e2iting the sco.e that hid it.
-he 9uilt8in #unctions are sco.ed in a sco.e outside the glo9al sco.e users declare glo9al varia9les in.
-hat is, a shaderHs glo9al sco.e, availa9le #or user8de#ined #unctions and glo9al varia9les, is nested inside
the sco.e containing the 9uilt8in #unctions. Ahen a #unction nae is redeclared in a nested sco.e, it hides
all #unctions declared with that nae in the outer sco.e. 4unction declarations F.rotot&.esG cannot occur
inside o# #unctionsL the& ust 9e at glo9al sco.e, or #or the 9uilt8in #unctions, outside the glo9al sco.e.
%hared glo9als are glo9al varia9les declared with the sae nae in inde.endentl& co.iled units
FshadersG within the sae language Fe.g., verte2G that are linked together when aking a single .rogra.
F0lo9als #oring the inter#ace 9etween two di##erent shader languages are discussed in other sections.G
%hared glo9als share the sae nae s.ace, and ust 9e declared with the sae t&.e. -he& will share the
sae storage. %hared glo9al arra&s ust have the sae 9ase t&.e and the sae e2.licit si?e. An arra&
i.licitl& si?ed in one shader can 9e e2.licitl& si?ed 9& another shader. I# no shader has an e2.licit si?e
#or the arra&, the largest i.licit si?e is used. %calars ust have e2actl& the sae t&.e nae and t&.e
de#inition. %tructures ust have the sae nae, se=uence o# t&.e naes, and t&.e de#initions, and #ield
naes to 9e considered the sae t&.e. -his rule a..lies recursivel& #or nested or e9edded t&.es. All
initiali?ers #or a shared glo9al ust have the sae value, or a link error will result.
31
4 #aria$%es and &'pes
&.# Storage :ualifiers
Varia9le declarations a& have one storage =uali#ier s.eci#ied in #ront o# the t&.e. -hese are suari?ed
as
:ualifier 8eaning
Q none: de#ault R local readOwrite eor&, or an in.ut .araeter to a #unction
const a co.ile8tie constant, or a #unction .araeter that is read8onl&
in
centroid in
sample in
linkage into a shader #ro a .revious stage, varia9le is co.ied in
linkage with centroid 9ased inter.olation
in.ut linkage with .er8sa.le inter.olation
out
centroid out
sample out
linkage out o# a shader to a su9se=uent stage, varia9le is co.ied out
linkage with centroid 9ased inter.olation
out.ut linkage with .er8sa.le inter.olation
attribute de.recatedL linkage 9etween a verte2 shader and ).en01 #or .er8verte2
data
uniform value does not change across the .riitive 9eing .rocessed, uni#ors
#or the linkage 9etween a shader, ).en01, and the a..lication
var0ing
centroid var0ing
de.recatedL linkage 9etween a verte2 shader and a #ragent shader #or
inter.olated data
patch in tessellation evaluation shader in.ut #or .er8.atch attri9utes
patch out tessellation control shader out.ut #or .er8.atch attri9utes
)ut.uts #ro shader FoutG and in.uts to a shader FinG can 9e #urther =uali#ied with one o# these
inter.olation =uali#iers
:ualifier 8eaning
smooth .ers.ective correct inter.olation
flat no inter.olation
noperspective linear inter.olation
-hese inter.olation =uali#iers a& onl& .recede the =uali#iers in, centroid in, sample in, out, centroid
out, or sample out in a declaration. -he& do not a..l& to the de.recated storage =uali#iers var0ing or
centroid var0ing. -he& also do not a..l& to in.uts into a verte2 shader or out.uts #ro a #ragent
shader.
1ocal varia9les can onl& use the const storage =uali#ier.
4unction .araeters can use const, in, and out =uali#iers, 9ut as arameter 1uali(iers. 3araeter
=uali#iers are discussed in section /.1.1 D4unction "alling "onventionsE.
4unction return t&.es and structure #ields do not use storage =uali#iers.
32
4 #aria$%es and &'pes
Data t&.es #or counication #ro one run o# a shader e2ecuta9le to its ne2t run Fto counicate
9etween #ragents or 9etween verticesG do not e2ist. -his would .revent .arallel e2ecution o# the sae
shader e2ecuta9le on ulti.le vertices or #ragents.
Initiali?ers a& onl& 9e used in declarations o# glo9als with no storage =uali#ier, with a const =uali#ier or
with a uniform =uali#ier. 0lo9al varia9les without storage =uali#iers that are not initiali?ed in their
declaration or 9& the a..lication will not 9e initiali?ed 9& ).en01, 9ut rather will enter main34 with
unde#ined values.
&.#.1 +efault Storage :ualifier
I# no =uali#ier is .resent on a glo9al varia9le, then the varia9le has no linkage to the a..lication or shaders
running on other .i.eline stages. 4or either glo9al or local un=uali#ied varia9les, the declaration will
a..ear to allocate eor& associated with the .rocessor it targets. -his varia9le will .rovide readOwrite
access to this allocated eor&.
&.#.2 Constant :ualifier
7aed co.ile8tie constants can 9e declared using the const =uali#ier. An& varia9les =uali#ied as
constant are read8onl& varia9les #or that shader. Declaring varia9les as constant allows ore descri.tive
shaders than using hard8wired nuerical constants. -he const =uali#ier can 9e used with an& o# the 9asic
data t&.es. It is an error to write to a const varia9le outside o# its declaration, so the& ust 9e initiali?ed
when declared. 4or e2a.le,
%onst ve%; ">xis * ve%; #=+=1 =+=1 '+=),
%tructure #ields a& not 9e =uali#ied with const. %tructure varia9les can 9e declared as const, and
initiali?ed with a structure constructor.
Initiali?ers #or const declarations ust 9e constant e2.ressions, as de#ined in section *.'.' D"onstant
E2.ressions.E
&.#.# Constant ',pressions
A constant e%ression is one o#
a literal value Fe.g., Z or trueG
a glo9al or local varia9le =uali#ied as const Fi.e., not including #unction .araetersG
an e2.ression #ored 9& an o.erator on o.erands that are all constant e2.ressions, including getting an
eleent or length o# a constant arra&, or a #ield o# a constant structure, or co.onents o# a constant
vector.
a constructor whose arguents are all constant e2.ressions
a 9uilt8in #unction call whose arguents are all constant e2.ressions, with the e2ce.tion o# the te2ture
looku. #unctions and the noise #unctions. -he 9uilt8in #unctions dRd, dRd0, and fwidth ust return
( when evaluated inside an initiali?er with an arguent that is a constant e2.ression.
4unction calls to user8de#ined #unctions Fnon89uilt8in #unctionsG cannot 9e used to #or constant
e2.ressions.
33
4 #aria$%es and &'pes
An integral constant e%ression is a constant e2.ression that evaluates to a scalar signed or unsigned
integer.
"onstant e2.ressions will 9e evaluated in an invariant wa& so as to create the sae value in ulti.le
shaders when the sae constant e2.ressions a..ear in those shaders. %ee section *./.1 D-he Invariant
<uali#ierE #or ore details on how to create invariant e2.ressions.
&.#.& Inputs
%hader in.ut varia9les are declared with a storage =uali#ier using the ke&word in. -he& #or the in.ut
inter#ace 9etween .revious stages o# the ).en01 .i.eline and the declaring shader. In.ut varia9les ust
9e declared at glo9al sco.e. Values #ro the .revious .i.eline stage are co.ied into in.ut varia9les at the
9eginning o# shader e2ecution. Varia9les declared as in.uts cannot 9e written to during shader e2ecution.
)nl& the in.ut varia9les that are actuall& read need to 9e written 9& the .revious stageL it is allowed to
have su.er#luous declarations o# in.ut varia9les.
%ee section 5 DBuilt8in Varia9lesE #or a list o# the 9uilt8in in.ut naes.
Verte2 shader in.ut varia9les For attri9utesG receive .er8verte2 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 an& other in.ut storage
=uali#ier or an& inter.olation =uali#iers as a verte2 shader in.ut. -he values co.ied in are esta9lished 9&
the ).en01 A3I or through the use o# the la&out identi#ier location. It is an error to use attribute in a
non8verte2 shader. Verte2 shader in.uts can onl& 9e single8.recision #loating8.oint scalars, single8
.recision #loating8.oint vectors, atrices, signed and unsigned integers and integer vectors. Verte2
shader in.uts can also #or arra&s o# these t&.es, 9ut not structures. -here are no dou9le8.recision
#loating8.oint in.ut t&.es in the verte2 shading language.
E2a.le declarations in a verte2 shader:
in ve%8 position,
in ve%; normal,
in ve%9 texEoordB8C,
It is e2.ected that gra.hics hardware will have a sall nu9er o# #i2ed vector locations #or .assing verte2
in.uts. -here#ore, the ).en01 %hading language de#ines each non8atri2 in.ut varia9le as taking u. one
such vector location. -here is an i.leentation de.endent liit on the nu9er o# locations that can 9e
used, and i# this is e2ceeded it will cause a link error. FDeclared in.ut varia9les that are not staticall& used
do not count against this liit.G A scalar in.ut counts the sae aount against this liit as a vec,, so
a..lications a& want to consider .acking grou.s o# #our unrelated #loat in.uts together into a vector to
9etter utili?e the ca.a9ilities o# the underl&ing hardware. A atri2 in.ut will use u. ulti.le locations.
-he nu9er o# locations used will e=ual the nu9er o# coluns in the atri2.
34
4 #aria$%es and &'pes
-essellation control, evaluation, and geoetr& shader in.ut varia9les get the .er8verte2 values written out
9& out.ut varia9les o# the sae naes in the .revious active shader stage. 4or these in.uts, centroid in
and inter.olation =uali#iers are allowed, 9ut have no e##ect. %ince tessellation control, tessellation
evaluation, and geoetr& shaders o.erate on a set o# vertices, each in.ut var&ing varia9le For in.ut 9lock,
see inter#ace 9locks 9elowG needs to 9e declared as an arra&. 4or e2a.le,
in float fooBC, -- geometr& s.ader input for vertex 4out float foo5
Each eleent o# such an arra& corres.onds to one verte2 o# the .riitive 9eing .rocessed. Each arra& can
o.tionall& have a si?e declared. -he arra& si?e will 9e set 9&, For i# .rovided ust 9e consistent withG the
in.ut la0out declarationFsG esta9lishing the t&.e o# in.ut .riitive, as descri9ed later in section *.'.$.1
DIn.ut 1a&out <uali#iersE.
%oe in.uts and out.uts are arra'e#, eaning that #or an inter#ace 9etween two shader stages either the
in.ut or out.ut declaration re=uires an e2tra level o# arra& inde2ing #or the declarations to atch. 4or
e2a.le, with the inter#ace 9etween a verte2 shader and a geoetr& shader, verte2 shader out.ut varia9les
and geoetr& shader in.ut varia9les o# the sae nae ust atch in t&.e and =uali#ication, e2ce.t that
the verte2 shader nae cannot 9e declared as an arra& while the geoetr& shader nae ust 9e declared
as an arra&, to allow #or verte2 inde2ing. It is a link error i# a non8arra&ed in.ut is not declared with the
sae t&.e, =uali#ication, and arra& diensionalit& as the atching out.ut. It is an error i# an arra&ed in.ut
is not declared as an arra& o# the sae t&.e and =uali#ication as the corres.onding Fnon8arra&G out.ut.
%&etricall&, it is an error i# an arra&ed out.ut is not declared as an arra& o# the sae t&.e and
=uali#ication as the corres.onding Fnon8arra&G in.ut.
I# the out.ut corres.onding to an arra&ed in.ut is itsel# an arra&, it ust a..ear in an out.ut 9lock Fsee
inter#ace 9locks 9elowG in the out.utting shader and in an in.ut 9lock in the in.utting shader with a 9lock
instance nae declared as an arra&. -his is re=uired 9ecause two8diensional arra&s are not su..orted.
Additionall&, tessellation evaluation shaders su..ort .er8.atch in.ut varia9les declared with the patch in
=uali#ier. 3er8.atch in.ut varia9les are #illed with the values o# .er8.atch out.ut varia9les written 9& the
tessellation control shader. 3er8.atch in.uts a& 9e declared as one8diensional arra&s, 9ut are not
inde2ed 9& verte2 nu9er. In.ut varia9les a& not 9e declared using the patch in =uali#ier in tessellation
control or geoetr& shaders. As with other in.ut varia9les, .er8.atch in.uts ust 9e declared using the
sae t&.e and =uali#ication as .er8.atch out.uts #ro the .revious Ftessellation controlG shader stage.
4ragent shader in.uts get .er8#ragent values, t&.icall& inter.olated #ro a .revious stageHs out.uts.
-he& are declared in #ragent shaders with the in storage =uali#ier, the centroid in storage =uali#ier, or
the de.recated var0ing and centroid var0ing storage =uali#iers. It is an error to use patch in in a
#ragent shader. 4ragent in.uts can onl& 9e signed and unsigned integers and integer vectors, #loating
.oint scalars, #loating8.oint vectors, atrices, or arra&s or structures o# these. 4ragent shader in.uts that
are signed or unsigned integers, integer vectors, or an& dou9le8.recision #loating8.oint t&.e ust 9e
=uali#ied with the inter.olation =uali#ier flat.
35
4 #aria$%es and &'pes
4ragent in.uts are declared as in the #ollowing e2a.les:
in ve%; normal,
%entroid in ve%9 TexEoord,
invariant %entroid in ve%8 Eolor,
noperspe%tive in float temperature,
flat in ve%; m&Eolor,
noperspe%tive %entroid in ve%9 m&TexEoord,
-he #ragent shader in.uts #or an inter#ace with the last active shader in the verte2 .rocessing .i.eline.
4or this inter#ace, the last active shader stage out.ut varia9les and #ragent shader in.ut varia9les o# the
sae nae ust atch in t&.e and =uali#ication Fother than out atching to inG.
&.#.) 5niform
-he uniform =uali#ier is used to declare glo9al varia9les whose values are the sae across the entire
.riitive 9eing .rocessed. All uniform varia9les are read8onl& and are initiali?ed e2ternall& either at link
tie or through the A3I. -he link tie initial value is either the value o# the varia9leHs initiali?er, i#
.resent, or ( i# no initiali?er is .resent. %a.ler t&.es cannot have initiali?ers.
E2a.le declarations are:
uniform ve%8 lig.t@osition,
uniform ve%; %olor * ve%;#=+F1 =+F1 =+9), -- value assigned at lin: time
-he uniform =uali#ier can 9e used with an& o# the 9asic data t&.es, or when declaring a varia9le whose
t&.e is a structure, or an arra& o# an& o# these.
-here is an i.leentation de.endent liit on the aount o# storage #or uni#ors that can 9e used #or
each t&.e o# shader and i# this is e2ceeded it will cause a co.ile8tie or link8tie error. 6ni#or
varia9les that are declared 9ut not used do not count against this liit. -he nu9er o# user8de#ined
uni#or varia9les and the nu9er o# 9uilt8in uni#or varia9les that are used within a shader are added
together to deterine whether availa9le uni#or storage has 9een e2ceeded.
I# ulti.le shaders are linked together, then the& will share a single glo9al uni#or nae s.ace, including
within a language as well as across languages. +ence, the t&.es and initiali?ers o# uni#or varia9les with
the sae nae ust atch across all shaders that are linked into a single .rogra.
It is legal #or soe shaders to .rovide an initiali?er #or a .articular uni#or varia9le, while another shader
does not, 9ut all .rovided initiali?ers ust 9e e=ual.
&.#.* Outputs
%hader out.ut varia9les are declared with a storage =uali#ier using the ke&word out. -he& #or the out.ut
inter#ace 9etween the declaring shader and the su9se=uent stages o# the ).en01 .i.eline. )ut.ut
varia9les ust 9e declared at glo9al sco.e. During shader e2ecution the& will 9ehave as noral
un=uali#ied glo9al varia9les. -heir values are co.ied out to the su9se=uent .i.eline stage on shader e2it.
)nl& out.ut varia9les that are read 9& the su9se=uent .i.eline stage need to 9e writtenL it is allowed to
have su.er#luous declarations o# out.ut varia9les.
36
4 #aria$%es and &'pes
-here is not an inout storage =uali#ier at glo9al sco.e #or declaring a single varia9le nae as 9oth in.ut
and out.ut to a shader. )ut.ut varia9les ust 9e declared with di##erent naes than in.ut varia9les.
+owever, nesting an in.ut or out.ut inside an inter#ace 9lock with an instance nae allows the sae
naes with one re#erenced through a 9lock instance nae.
Verte2, tessellation evaluation, and geoetr& out.ut varia9les out.ut .er8verte2 data and are declared
using the out, centroid out, or sample out storage =uali#iers, or the de.recated var0ing storage =uali#ier.
It is an error to use patch out in a verte2, tessellation evaluation, or geoetr& shader. )ut.ut varia9les
can onl& 9e #loating8.oint scalars, #loating8.oint vectors, atrices, signed or unsigned integers or integer
vectors, or arra&s or structures o# an& these.
Individual verte2, tessellation evaluation, and geoetr& out.uts are declared as in the #ollowing e2a.les:
out ve%; normal,
%entroid out ve%9 TexEoord,
invariant %entroid out ve%8 Eolor,
noperspe%tive out float temperature, -- var&ing is depre%ated
flat out ve%; m&Eolor,
noperspe%tive %entroid out ve%9 m&TexEoord,
sample out ve%8 perSampleEolor,
-hese can also a..ear in inter#ace 9locks, as descri9ed in section *.'.5 DInter#ace BlocksE. Inter#ace
9locks allow si.ler addition o# arra&s to the inter#ace #ro verte2 to geoetr& shader. -he& also allow a
#ragent shader to have the sae in.ut inter#ace as a geoetr& shader #or a given verte2 shader.
-essellation control shader out.ut varia9les are a& 9e used to out.ut .er8verte2 and .er8.atch data. 3er8
verte2 out.ut varia9les are arra&ed Fsee arra'e# under *.'.* In.utsG and declared using out or centroid
out storage =uali#iers. 3er8.atch out.ut varia9les are declared using the patch out storage =uali#ier. 3er8
verte2 and .er8.atch out.ut varia9les can onl& 9e #loating8.oint scalars, #loating8.oint vectors, atrices,
signed or unsigned integers or integer vectors, or arra&s or structures o# an& these. %ince tessellation
control shaders .roduce an arra&ed .riitive co.rising ulti.le vertices, each .er8verte2 out.ut varia9le
For out.ut 9lock, see inter#ace 9locks 9elowG needs to 9e declared as an arra&. 4or e2a.le,
out float fooBC, -- feeds next stage input 4in float fooBC5
Each eleent o# such an arra& corres.onds to one verte2 o# the .riitive 9eing .roduced. Each arra& can
o.tionall& have a si?e declared. -he arra& si?e will 9e set 9& For i# .rovided ust 9e consistent withG the
out.ut la&out declarationFsG esta9lishing the nu9er o# vertices in the out.ut .atch, as descri9ed later in
section *.'.$.! D)ut.ut 1a&out <uali#iersE.
As descri9ed under the section *.'.* DIn.utsE a9ove, i# a .er8verte2 out.ut o# the tessellation control
shader is itsel# an arra& with ulti.le values .er verte2, it ust a..ear in an out.ut 9lock Fsee inter#ace
9locks 9elowG in the tessellation control shader with a 9lock instance nae declared as an arra&.
Each tessellation control shader invocation has a corres.onding out.ut .atch verte2, and a& assign
values to .er8verte2 out.uts onl& i# the& 9elong to that corres.onding verte2. I# a .er8verte2 out.ut
varia9le is used as an l8value, it is an error i# the e2.ression indicating the verte2 inde2 is not the identi#ier
gl)*nvocation*D.
37
4 #aria$%es and &'pes
-he order o# e2ecution o# a tessellation control shader invocation relative to the other invocations #or the
sae in.ut .atch is unde#ined unless the 9uilt8in #unction barrierFG is used. -his .rovides soe control
over relative e2ecution order. Ahen a shader invocation calls barrierFG, its e2ecution .auses until all
other invocations have reached the sae .oint o# e2ecution. )ut.ut varia9le assignents .er#ored 9&
an& invocation e2ecuted .rior to calling barrierFG will 9e visi9le to an& other invocation a#ter the call to
barrierFG returns.
Because tessellation control shader invocations e2ecute in unde#ined order 9etween 9arriers, the values o#
.er8verte2 or .er8.atch out.ut varia9les will soeties 9e unde#ined. "onsider the 9eginning and end o#
shader e2ecution and each call to barrierFG as s&nchroni?ation .oints. -he value o# an out.ut varia9le
will 9e unde#ined in an& o# the three #ollowing cases:
1. At the 9eginning o# e2ecution.
!. At each s&nchroni?ation .oint, unless
the value was well8de#ined a#ter the .revious s&nchroni?ation .oint and was not written 9& an&
invocation since, or
the value was written 9& e2actl& one shader invocation since the .revious s&nchroni?ation
.oint, or
the value was written 9& ulti.le shader invocations since the .revious s&nchroni?ation .oint,
and the last write .er#ored 9& all such invocations wrote the sae value.
'. Ahen read 9& a shader invocation, i#
the value was unde#ined at the .revious s&nchroni?ation .oint and has not 9een writen 9& the
sae shader invocation since, or
the out.ut varia9le is written to 9& an& other shader invocation 9etween the .revious and ne2t
s&nchroni?ation .oints, even i# that assignent occurs in code #ollowing the read.
4ragent out.uts out.ut .er8#ragent data and are declared using the out storage =uali#ier. It is an error
to use centroid out, sample out, or patch out in a #ragent shader. 4ragent out.uts can onl& 9e float,
#loating8.oint vectors, signed or unsigned integers or integer vectors, or arra&s o# an& these. :atrices and
structures cannot 9e out.ut. 4ragent out.uts are declared as in the #ollowing e2a.les:
out ve%8 FragmentEolor,
out uint Luminosit&,
&.#.4 Interface /loc2s
In.ut, out.ut, and uni#or varia9le declarations can 9e grou.ed into naed inter#ace 9locks to .rovide
coarser granularit& 9acking than is achieva9le with individual declarations. -he& can have an o.tional
instance nae, used in the shader to re#erence their e9ers. An out.ut 9lock o# one .rograa9le
stage is 9acked 9& a corres.onding in.ut 9lock in the su9se=uent .rograa9le stage. A uni#or 9lock is
9acked 9& the a..lication with a 9u##er o9@ect. It is illegal to have an in.ut 9lock in a verte2 shader or an
out.ut 9lock in a #ragent shaderL these uses are reserved #or #uture use.
An inter#ace 9lock is started 9& an in, out, or uniform ke&word, #ollowed 9& a 9lock nae, #ollowed 9&
an o.en curl& 9race F C G as #ollows:
inter(ace-&loc. :
la'out-1uali(ieropt inter(ace-1uali(ier &loc.-name C mem&er-list D instance-nameopt L
3
4 #aria$%es and &'pes
la'out-1uali(ier :
la0out A la'out-1uali(ier-i#-list B
inter(ace-1uali(ier :
in
out
uniform
la'out-1uali(ier-i#-list
coa se.arated list o# la'out-1uali(ier-i#
mem&er-list :
mem&er-#eclaration
mem&er-#eclaration mem&er-list
mem&er-#eclaration :
la'out-1uali(ieropt 1uali(iersopt t'e #eclarators L
instance-name :
i#enti(ier
i#enti(ier 5 6
i#enti(ier 5 integral-constant-e%ression 6
Each o# the a9ove eleents is discussed 9elow, with the e2ce.tion o# la&out =uali#iers Fla'out-1uali(ier4,
which are de#ined in the ne2t section.
4irst, an e2a.le,
uniform Transform )
mat8 ?odelVie/?atrix,
mat8 ?odelVie/@ro7e%tion?atrix,
uniform mat; Normal?atrix, -- allo/ed restatement of Aualifier
float eformation,
3,
-he a9ove esta9lishes a uni#or 9lock naed D-rans#orE with #our uni#ors grou.ed inside it.
-&.es and declarators are the sae as #or other in.ut, out.ut, and uni#or varia9le declarations outside
9locks, with these e2ce.tions:
initiali?ers are not allowed
sa.ler t&.es are not allowed
structure de#initions cannot 9e nested inside a 9lock
)therwise, 9uilt8in t&.es, .reviousl& declared structures, and arra&s o# these are allowed as the t&.e o# a
declarator in the sae anner the& are allowed outside a 9lock.
3!
4 #aria$%es and &'pes
I# no o.tional =uali#ier is used in a e9er8declaration, the =uali#ication o# the varia9le is @ust in, out, or
uniform as deterined 9& inter(ace-1uali(ier. I# o.tional =uali#iers are used, the& can include
inter.olation and storage =uali#iers and the& ust declare an in.ut, out.ut, or uni#or varia9le consistent
with the inter#ace =uali#ier o# the 9lock: In.ut varia9les, out.ut varia9les, and uni#or varia9les can onl&
9e in in 9locks, out 9locks, and uniform 9locks, res.ectivel&. Re.eating the in, out, or uniform
inter#ace =uali#ier #or a e9erHs storage =uali#ier is o.tional. Declarations using the de.recated
attribute and var0ing =uali#iers are not allowed. 4or e2a.le,
in ?aterial )
smoot. in ve%8 Eolor', -- legal1 input inside in $lo%:
smoot. ve%8 Eolor9, -- legal1 DinD in.erited from Din ?aterialD
ve%9 TexEoord, -- legal1 TexEoord is an input
uniform float >tten, -- illegal1 mismat%.ed interfa%es
var&ing ve%9 TexEoord9,--illegal1 depre%ated :e&/ords donDt get ne/ uses
3,
4or this section, de#ine an inter(ace to 9e one o# these
All the uni#ors o# a .rogra. -his s.ans all co.ilation units linked together within one .rogra.
-he 9oundar& 9etween ad@acent .rograa9le .i.eline stages: -his s.ans all the out.uts in all
co.ilation units o# the #irst stage and all the in.uts in all co.ilation units o# the second stage.
-he 9lock nae F&loc.-nameG is used to atch inter#aces: an out.ut 9lock o# one .i.eline stage will 9e
atched to an in.ut 9lock with the sae nae in the su9se=uent .i.eline stage. 4or uni#or 9locks, the
a..lication uses the 9lock nae to identi#& the 9lock. Block naes have no other use within a shader
9e&ond inter#ace atchingL it is an error to use a 9lock nae at glo9al sco.e #or an&thing other than as a
9lock nae Fe.g., use o# a 9lock nae #or a glo9al varia9le nae or #unction nae is currentl& reservedG.
:atched 9lock naes within an inter#ace Fas de#ined a9oveG ust atch in ters o# having the sae
nu9er o# declarations with the sae se=uence o# t&.es and the sae se=uence o# e9er naes, as well
as having the sae e9er8wise la&out =uali#ication Fsee ne2t sectionG. 4urtherore, i# a atching 9lock
is declared as an arra&, then the arra& si?es ust also atch For #ollow arra& atching rules #or the
inter#ace 9etween a verte2 and a geoetr& shaderG. An& isatch will generate a link error. A 9lock
nae is allowed to have di##erent de#initions in di##erent inter#aces within the sae shader, allowing, #or
e2a.le, an in.ut 9lock and out.ut 9lock to have the sae nae.
4"
4 #aria$%es and &'pes
I# an instance nae Finstance-nameG is not used, the naes declared inside the 9lock are sco.ed at the
glo9al level and accessed as i# the& were declared outside the 9lock. I# an instance nae Finstance-nameG
is used, then it .uts all the e9ers inside a sco.e within its own nae s.ace, accessed with the #ield
selector F . G o.erator Fanalogousl& to structuresG. 4or e2a.le,
in Lig.t )
ve%8 Lig.t@os,
ve%; Lig.tEolor,
3,
in EoloredTexture )
ve%8 Eolor,
ve%9 TexEoord,
3 ?aterial, -- instan%e name
ve%; Eolor, -- different Eolor t.an ?aterial+Eolor
ve%8 Lig.t@os, -- illegal1 alread& defined
+++
+++ * Lig.t@os, -- a%%essing Lig.t@os
+++ * ?aterial+Eolor, -- a%%essing Eolor in EoloredTexture $lo%:
)utside the shading language Fi.e., in the A3IG, e9ers are siilarl& identi#ied e2ce.t the 9lock nae is
alwa&s used in .lace o# the instance nae FA3I accesses are to inter#aces, not to shadersG. I# there is no
instance nae, then the A3I does not use the 9lock nae to access a e9er, @ust the e9er nae.
out Vertex )
ve%8 @osition, -- >@I transform-feed$a%: /ill use 4Vertex+@osition5
ve%9 Texture,
3 Eoords, -- s.ader /ill use 4Eoords+@osition5
out Vertex9 )
ve%8 Eolor, -- >@I /ill use 4Eolor5
3,
4or 9locks declared as arra&s, the arra& inde2 ust also 9e included when accessing e9ers, as in this
e2a.le
uniform Transform ) -- >@I uses 4TransformB9C5 to refer to instan%e 9
mat8 ?odelVie/?atrix,
mat8 ?odelVie/@ro7e%tion?atrix,
float eformation,
3 transformsB8C,
+++
+++ * transformsB9C+?odelVie/?atrix, -- s.ader a%%ess of instan%e 9
-- >@I uses 4Transform+?odelVie/?atrix5 to Auer& an offset or ot.er Auer&
4or uni#or 9locks declared as an arra&, each individual arra& eleent corres.onds to a se.arate 9u##er
o9@ect 9acking one instance o# the 9lock. As the arra& si?e indicates the nu9er o# 9u##er o9@ects needed,
uni#or 9lock arra& declarations ust s.eci#& an arra& si?e. An& integral e2.ression can 9e used to inde2
a uni#or 9lock arra&, as .er section *.1.; WArra&sW.
41
4 #aria$%es and &'pes
Ahen using ).en01 A3I entr& .oints to identi#& the nae o# an individual 9lock in an arra& o# 9locks,
the nae string ust include an arra& inde2 Fe.g., Trans(orm526G. Ahen using ).en01 A3I entr& .oints
to re#er to o##sets or other characteristics o# a 9lock e9er, an arra& inde2 ust not 9e s.eci#ied Fe.g.,
Trans(orm.7o#elVie/7atri%G.
0eoetr& shader in.ut 9locks ust 9e declared as arra&s and #ollow the arra& declaration and linking
rules #or all geoetr& shader in.uts. All other in.ut and out.ut 9lock arra&s ust s.eci#& an arra& si?e.
-here is an i.leentation de.endent liit on the nu9er o# uni#or 9locks that can 9e used .er stage.
I# this liit is e2ceeded, it will cause a link error.
&.#. La!out :ualifiers
1a&out =uali#iers can a..ear in several #ors o# declaration. -he& can a..ear as .art o# an inter#ace
9lock de#inition or 9lock e9er, as shown in the graar in the .revious section. -he& can also a..ear
with @ust an inter#ace =uali#ier to esta9lish la&outs o# other declarations ade with that inter#ace =uali#ier:
la'out-1uali(ier inter(ace-1uali(ier L
)r, the& can a..ear with an individual varia9le declared with an inter#ace =uali#ier:
la'out-1uali(ier inter(ace-1uali(ier #eclaration L
Declarations o# la&outs can onl& 9e ade at glo9al sco.e, and onl& where indicated in the #ollowing
su9sectionsL their details are s.eci#ic to what the inter#ace =uali#ier is, and are discussed individuall&.
As shown in the .revious section, la'out-1uali(ier e2.ands to
la'out-1uali(ier :
la0out A la'out-1uali(ier-i#-list B
-he tokens in an& la'out-1uali(ier-i#-list are identi#iers, not ke&words. 0enerall&, the& can 9e listed in
an& order. )rder8de.endent eanings e2ist onl& i# e2.licitl& called out 9elow. %iilarl&, these identi#iers
are not case sensitive, unless e2.licitl& noted otherwise.
&.#..1 Input La!out :ualifiers
-essellation control shaders do not have an& in.ut la&out =uali#iers.
Verte2 shaders allow in.ut la&out =uali#iers on in.ut varia9le declarations. -he la&out =uali#ier identi#ier
#or verte2 shader in.uts is:
la'out-1uali(ier-i#
location I integer-constant
)nl& one arguent is acce.ted. 4or e2a.le,
la&out#lo%ation * ;) in ve%8 normal,
will esta9lish that the verte2 shader in.ut normal is co.ied in #ro vector location nu9er '.
42
4 #aria$%es and &'pes
I# the declared in.ut is an arra&, it will 9e assigned consecutive locations starting with the location
s.eci#ied. 4or e2a.le,
la&out#lo%ation * G) in ve%8 %olorsB;C,
will esta9lish that the verte2 shader in.ut colors is co.ied in #ro vector location nu9ers /, 5, and $.
I# an in.ut varia9le with no location assigned in the shader te2t has a location s.eci#ied through the
).en01 A3I, the A3I8assigned location will 9e used. )therwise, such varia9les will 9e assigned a
location 9& the linker. %ee section !.11.' DVerte2 Attri9utesE o# the ).en01 0ra.hics %&ste
%.eci#ication #or ore details. A link error will occur i# an in.ut varia9le is declared in ulti.le verte2
shaders with con#licting locations.
-essellation evaluation shaders allow in.ut la&out =uali#iers onl& on the inter#ace =uali#ier in, not on an
in.ut 9lock, 9lock e9er, or varia9le declarations. -he in.ut la&out =uali#ier identi#iers allowed #or
tessellation evaluation shaders are:
la'out-1uali(ier-i#
triangles
Quads
isolines
eQual$spacing
fractional$even$spacing
fractional$odd$spacing
cw
ccw
point$mode
)ne su9set o# these identi#iers, rimitive mo#e, is used to s.eci#& a tessellation .riitive ode to 9e used
9& the tessellation .riitive generator. -o s.eci#& a .riitive ode, the identi#ier ust 9e one o#
triangles, Quads, or isolines, which s.eci#& that the tessellation .riitive generator should su9divide a
triangle into saller triangles, a =uad into triangles, or a =uad into a collection o# lines, res.ectivel&.
A second su9set o# these identi#iers, verte% sacing, is used to s.eci#& the s.acing used 9& the tessellation
.riitive generator when su9dividing an edge. -o s.eci#& verte2 s.acing, the identi#ier ust 9e one o#
the #ollowing.
eQual$spacing signi#&ing that edges should 9e divided into a collection o# e=ual8si?ed segents.
fractional$even$spacing signi#&ing that edges should 9e divided into an even nu9er o# e=ual8
length segents .lus two additional shorter W#ractionalW segents.
fractional$odd$spacing signi#&ing that edges should 9e divided into an odd nu9er o# e=ual8
length segents .lus two additional shorter W#ractionalW segents.
A third su9set o# these identi#iers, or#ering, s.eci#ies whether the tessellation .riitive generator
.roduces triangles in clockwise or counter8clockwise order, according to the coordinate s&ste de.icted
in the ).en01 s.eci#ication. -he ordering identi#iers cw and ccw indicate clockwise and counter8
clockwise triangles, res.ectivel&. I# the tessellation .riitive generator does not .roduce triangles,
ordering is ignored.
43
4 #aria$%es and &'pes
4inall&, oint mo#e$ is s.eci#ied with the identi#ier point$mode indicating the tessellation .riitive
generator should .roduce a .oint #or each uni=ue verte2 in the su9divided .riitive, rather than
generating lines or triangles.
An& or all o# these identi#iers a& 9e s.eci#ied one or ore ties in a single in.ut la&out declaration. I#
.riitive ode, verte2 s.acing, or ordering is declared ore than once in the tessellation evaluation
shaders o# a .rogra, all such declarations ust use the sae identi#ier.
At least one tessellation evaluation shader Fco.ilation unitG in a .rogra ust declare a .riitive ode
in its in.ut la&out. Declaring verte2 s.acing, ordering, or .oint ode identi#iers is o.tional. It is not
re=uired that all tessellation evaluation shaders in a .rogra declare a .riitive ode. I# s.acing or
verte2 ordering declarations are oitted, the tessellation .riitive generator will use e=ual s.acing or
counter8clockwise verte2 ordering, res.ectivel&. I# a .oint ode declaration is oitted, the tessellation
.riitive generator will .roduce lines or triangles according to the .riitive ode.
0eoetr& shaders allow in.ut la&out =uali#iers onl& on the inter#ace =uali#ier in, not on an in.ut 9lock,
9lock e9er, or varia9le. -he la&out =uali#ier identi#iers #or geoetr& shader in.uts include rimitive
identi#iers and an invocation count identi#ier:
la'out-1uali(ier-i#
points
lines
lines$adPacenc0
triangles
triangles$adPacenc0
invocations I integer-constant
-he identi#iers points, lines, lines$adPacenc0, triangles, and triangles$adPacenc0 are used to s.eci#& the
t&.e o# in.ut rimitive acce.ted 9& the geoetr& shader, and onl& one o# these is acce.ted. At least one
geoetr& shader Fco.ilation unitG in a .rogra ust declare this in.ut .riitive la&out, and all geoetr&
shader in.ut la&out declarations in a .rogra ust declare the sae la&out. It is not re=uired that all
geoetr& shaders in a .rogra declare an in.ut .riitive la&out.
-he identi#ier invocations is used to s.eci#& the nu9er o# ties the geoetr& shader e2ecuta9le is
invoked #or each in.ut .riitive received. Invocation count declarations are o.tional. I# no invocation
count is declared in an& geoetr& shader in the .rogra, the geoetr& shader will 9e run once #or each
in.ut .riitive. I# an invocation count is declared, all such declarations ust s.eci#& the sae count. I# a
shader s.eci#ies an invocation count greater than the i.leentation8de.endent a2iu, it will #ail to
co.ile.
4or e2a.le,
la&out#triangles1 invo%ations * G) in,
will esta9lish that all in.uts to the geoetr& shader are triangles and that the geoetr& shader e2ecuta9le
is run si2 ties #or each triangle .rocessed.
All geoetr& shader in.ut unsi?ed arra& declarations will 9e si?ed 9& an earlier in.ut .riitive la&out
=uali#ier, when .resent, as .er the #ollowing ta9le.
44
4 #aria$%es and &'pes
"a0out Si!e of &nput Arra0s
.oints 1
lines !
lines>ad@acenc& *
triangles '
triangles>ad@acenc& /
-he intrinsicall& declared in.ut arra& gl)in56 will also 9e si?ed 9& an& in.ut .riitive8la&out declaration.
+ence, the e2.ression
gl_in+lengt.#)
will return the value #ro the ta9le a9ove.
4or in.uts declared without an arra& si?e, including intrinsicall& declared in.uts Fi.e., gl)inG, a la&out ust
9e declared 9e#ore an& use o# the ethod length34 or other arra& use re=uiring its si?e 9e known.
It is a co.ile8tie error i# a la&out declarationHs arra& si?e F#ro ta9le a9oveG does not atch an& arra&
si?e s.eci#ied in declarations o# an in.ut varia9le in the sae shader. -he #ollowing are all e2a.les o#
co.ile tie errors:
-- %ode seAuen%e /it.in one s.ader+++
in ve%8 Eolor'BC, -- si"e un:no/n
+++Eolor'+lengt.#)+++-- illegal1 lengt.#) un:no/n
in ve%8 Eolor9B9C, -- si"e is 9
+++Eolor'+lengt.#)+++-- illegal1 Eolor' still .as no si"e
in ve%8 Eolor;B;C, -- illegal1 input si"es are in%onsistent
la&out#lines) in, -- legal1 input si"e is 91 mat%.ing
in ve%8 Eolor8B;C, -- illegal1 %ontradi%ts la&out
+++Eolor'+lengt.#)+++-- legal1 lengt.#) is 91 Eolor' si"ed $& la&out#)
la&out#lines) in, -- legal1 mat%.es ot.er la&out#) de%laration
la&out#triangles) in,-- illegal1 does not mat%. earlier la&out#) de%laration
It is a link8tie error i# not all .rovided si?es Fsi?ed in.ut arra&s and la&out si?eG atch across all
geoetr& shaders in the .rogra.
4ragent shaders can have an in.ut la&out onl& #or redeclaring the 9uilt8in varia9le gl)-rag8oor# Fsee
section 5.1 DBuilt8In 1anguage Varia9lesEG. -he la&out =uali#ier identi#iers #or gl)-rag8oor# are
la'out-1uali(ier-i#
origin$upper$left
piel$center$integer
45
4 #aria$%es and &'pes
B& de#ault, gl)-rag8oor# assues a lower8le#t origin #or window coordinates and assues .i2el centers
are located at hal#8.i2el coordinates. 4or e2a.le, the F%$ 'G location F(.,, (.,G is returned #or the lower8
le#t8ost .i2el in a window. -he origin can 9e changed 9& redeclaring gl)-rag8oor# with the
origin$upper$left identi#ier, oving the origin o# gl)-rag8oor# to the u..er le#t o# the window, with '
increasing in value toward the 9otto o# the window. -he values returned can also 9e shi#ted 9& hal# a
.i2el in 9oth % and ' 9& piel$center$integer so it a..ears the .i2els are centered at whole nu9er .i2el
o##sets. -his oves the F%, 'G value returned 9& gl)-rag8oor# o# F(.,, (.,G 9& de#ault, to F(.(, (.(G with
piel$center$integer. Redeclarations are done as #ollows
in ve%8 gl_FragEoord, -- rede%laration t.at %.anges not.ing is allo/ed
-- >ll t.e follo/ing are allo/ed rede%laration t.at %.ange $e.avior
la&out#origin_upper_left) in ve%8 gl_FragEoord,
la&out#pixel_%enter_integer) in ve%8 gl_FragEoord,
la&out#origin_upper_left1 pixel_%enter_integer) in ve%8 gl_FragEoord,
I# gl)-rag8oor# is redeclared in an& #ragent shader in a .rogra, it ust 9e redeclared in all the
#ragent shaders in that .rogra that have a static use gl)-rag8oor#. All redeclarations o#
gl)-rag8oor# in all #ragent shaders in a single .rogra ust have the sae set o# =uali#iers. Aithin
an& shader, the #irst redeclarations o# gl)-rag8oor# ust a..ear 9e#ore an& use o# gl)-rag8oor#. -he
9uilt8in gl)-rag8oor# is onl& .redeclared in #ragent shaders, so redeclaring it in an& other shader
language will 9e illegal.
Redeclaring gl)-rag8oor# with origin$upper$left andOor piel$center$integer =uali#iers onl& a##ects
gl)-rag8oor#.% and gl)-rag8oor#.'. It has no a##ect on rasteri?ation, trans#oration, or an& other .art
o# the ).en01 .i.eline or language #eatures.
&.#..2 Output La!out :ualifiers
Verte2 and tessellation evaluation shaders cannot have out.ut la&out =uali#iers.
-essellation control shaders allow out.ut la&out =uali#iers onl& on the inter#ace =uali#ier out, not on an
out.ut 9lock, 9lock e9er, or varia9le declaration. -he out.ut la&out =uali#ier identi#iers allowed #or
tessellation control shaders are:
la'out-1uali(ier-i#
vertices I integer-constant
-he identi#ier vertices s.eci#ies the nu9er o# vertices in the out.ut .atch .roduced 9& the tessellation
control shader, which also s.eci#ies the nu9er o# ties the tessellation control shader is invoked. It is an
error #or the out.ut verte2 count to 9e less than or e=ual to ?ero, or greater than the i.leentation8
de.endent a2iu .atch si?e.
-he intrinsicall& declared tessellation control out.ut arra& gl$out'( will also 9e si?ed 9& an& out.ut la&out
declaration. +ence, the e2.ression
gl_out+lengt.#)
will return the out.ut .atch verte2 count s.eci#ied in a .revious out.ut la&out =uali#ier. 4or out.uts
declared without an arra& si?e, including intrinsicall& declared out.uts Fi.e., gl$outG, a la&out ust 9e
ust 9e declared 9e#ore an& use o# the ethod lengthFG or other arra& use re=uires its si?e 9e known.
46
4 #aria$%es and &'pes
It is a co.ile8tie error i# the out.ut .atch verte2 count s.eci#ied in an out.ut la&out =uali#ier does not
atch the arra& si?e s.eci#ied in an& out.ut varia9le declaration in the sae shader.
All tessellation control shader la&out declarations in a .rogra ust s.eci#& the sae out.ut .atch verte2
count. -here ust 9e at least one la&out =uali#ier s.eci#&ing an out.ut .atch verte2 count in an& .rogra
containing tessellation control shadersL however, such a declaration is not re=uired in all tessellation
control shaders.
4ragent shaders allow out.ut la&out =uali#iers onl& on the inter#ace =uali#ier out. -he la&out =uali#ier
identi#ier #or #ragent shader out.uts is:
la'out-1uali(ier-i#
location I integer-constant
inde I integer-constant
Each o# these =uali#iers a& a..ear at ost once. I# inde is s.eci#ied, location ust also 9e s.eci#ied.
I# inde is not s.eci#ied, the value ( is used. 4or e2a.le,
la&out#lo%ation * ;) out ve%8 %olor,
will esta9lish that the #ragent shader out.ut color is co.ied out to #ragent color ' as the #irst Finde2
?eroG in.ut to the 9lend e=uation. And,
la&out#lo%ation * ;1 index * ') out ve%8 fa%tor,
will esta9lish that the #ragent shader out.ut (actor is co.ied out to #ragent color ' as the second Finde2
oneG in.ut to the 9lend e=uation.
I# the naed #ragent shader out.ut is an arra&, it will 9e assigned consecutive locations starting with the
location s.eci#ied. 4or e2a.le,
la&out#lo%ation * 9) out ve%8 %olorsB;C,
will esta9lish that colors is co.ied out to vector location nu9ers !, ', and *.
I# an out.ut varia9le with no location or inde2 assigned in the shader te2t has a location s.eci#ied through
the ).en01 A3I, the A3I8assigned location will 9e used. )therwise, such varia9les will 9e assigned a
location 9& the linker. All such assignents will have a color inde2 o# ?ero. %ee section '.;.! D%hader
E2ecutionE o# the ).en01 0ra.hics %&ste %.eci#ication #or ore details. A link error will occur i# an
in.ut varia9le is declared in ulti.le verte2 shaders with con#licting location or inde2 values.
0eoetr& shaders can have three t&.es o# out.ut la&out identi#iers: an out.ut rimitive t'e, a a2iu
out.ut verte% count, and .er8out.ut stream nu9ers. -he .riitive t&.e and verte2 count identi#iers are
allowed onl& on the inter#ace =uali#ier out, not on an out.ut 9lock, 9lock e9er, or varia9le declaration.
-he strea identi#ier is allowed on the inter#ace =uali#ier out, on out.ut 9locks, and on varia9le
declarations.
-he la&out =uali#ier identi#iers #or geoetr& shader out.uts are
47
4 #aria$%es and &'pes
la'out-1uali(ier-i#
points
line$strip
triangle$strip
ma$vertices I integer-constant
stream I integer-constant
-he .riitive t&.e identi#iers points, line$strip, and triangle$strip are used to s.eci#& the t&.e o# out.ut
.riitive .roduced 9& the geoetr& shader, and onl& one o# these is acce.ted. At least one geoetr&
shader Fco.ilation unitG in a .rogra ust declare an out.ut .riitive t&.e, and all geoetr& shader
out.ut .riitive t&.e declarations in a .rogra ust declare the sae .riitive t&.e. It is not re=uired
that all geoetr& shaders in a .rogra declare an out.ut .riitive t&.e.
-he verte2 count identi#ier ma$vertices is used to s.eci#& the a2iu nu9er o# vertices the shader
will ever eit in a single invocation. At least one geoetr& shader Fco.ilation unitG in a .rogra ust
declare a a2iu out.ut verte2 count, and all geoetr& shader out.ut verte2 count declarations in a
.rogra ust declare the sae count. It is not re=uired that all geoetr& shaders in a .rogra declare a
count.
In this e2a.le,
la&out#triangle_strip1 max_verti%es * G=) out, -- order does not matter
la&out#max_verti%es * G=) out, -- rede%laration o:a&
la&out#triangle_strip) out, -- rede%laration o:a&
la&out#points) out, -- error1 %ontradi%ts triangle_strip
la&out#max_verti%es * ;=) out, -- error1 %ontradi%ts G=
all out.uts #ro the geoetr& shader are triangles and at ost /( vertices will 9e eitted 9& the shader. It
is an error #or the a2iu nu9er o# vertices to 9e greater than gl$%a#eometr0Output5ertices.
-he identi#ier stream is used to s.eci#& that a geoetr& shader out.ut varia9le or 9lock is associated with
a .articular verte2 strea Fnu9ered 9eginning with ?eroG. A de#ault strea nu9er a& 9e declared at
glo9al sco.e 9& =uali#&ing inter#ace =uali#ier out as in this e2a.le:
la&out#stream * ') out,
-he strea nu9er s.eci#ied in such a declaration re.laces an& .revious de#ault and a..lies to all
su9se=uent 9lock and varia9le declarations until a new de#ault is esta9lished. -he initial de#ault strea
nu9er is ?ero.
4
4 #aria$%es and &'pes
Each out.ut 9lock or non89lock out.ut varia9le is associated with a verte2 strea. I# the 9lock or varia9le
is declared with the strea identi#ier, it is associated with the s.eci#ied streaL otherwise, it is associated
with the current de#ault strea. A 9lock e9er a& 9e declared with a strea identi#ier, 9ut the
s.eci#ied strea ust atch the strea associated with the containing 9lock. )ne e2a.le:
la&out#stream*') out, -- default is no/ stream '
out ve%8 var', -- var' gets default stream #')
la&out#stream*9) out 6lo%:' ) -- H6lo%:'H $elongs to stream 9
la&out#stream*9) ve%8 var9, -- redundant $lo%: mem$er stream de%l
la&out#stream*;) ve%9 var;, -- ILLE!>L #must mat%. $lo%: stream)
ve%; var8, -- $elongs to stream 9
3,
la&out#stream*=) out, -- default is no/ stream =
out ve%8 var<, -- var< gets default stream #=)
out 6lo%:9 ) -- H6lo%:9H gets default stream #=)
ve%8 varG,
3,
la&out#stream*;) out ve%8 varF, -- varF $elongs to stream ;
Each verte2 eitted 9& the geoetr& shader is assigned to a s.eci#ic strea, and the attri9utes o# the
eitted verte2 are taken #ro the set o# out.ut 9locks and varia9les assigned to the targeted strea. A#ter
each verte2 is eitted, the values o# all out.ut varia9les 9ecoe unde#ined. Additionall&, the out.ut
varia9les associated with each verte2 strea a& share storage. Ariting to an out.ut varia9le associated
with one strea a& overwrite out.ut varia9les associated with an& other strea. Ahen eitting each
verte2, a geoetr& shader should write to all out.uts associated with the strea to which the verte2 will
9e eitted and to no out.uts associated with an& other strea.
I# a geoetr& shader out.ut 9lock or varia9le is declared ore than once, all such declarations ust
associate the varia9le with the sae verte2 strea. I# an& strea declaration s.eci#ies a non8e2istent
strea nu9er, the shader will #ail to co.ile.
Built8in geoetr& shader out.uts are alwa&s associated with verte2 strea ?ero.
All geoetr& shader out.ut la&out declarations in a .rogra ust declare the sae la&out and sae value
#or ma$vertices. I# geoetr& shaders are in a .rogra, there ust 9e at least one geoetr& out.ut
la&out declaration soewhere in the .rogra, 9ut not all geoetr& shaders Fco.ilation unitsG are
re=uired to declare it.
&.#..# 5niform /loc2 La!out :ualifiers
1a&out =uali#iers can 9e used #or uni#or 9locks, 9ut not #or non89lock uni#or declarations. -he la&out
=uali#ier identi#iers #or uni#or 9locks are
la'out-1uali(ier-i#
shared
packed
std1,9
row$maPor
column$maPor
4!
4 #aria$%es and &'pes
7one o# these have an& seantic a##ect at all on the usage o# the varia9les 9eing declaredL the& onl&
descri9e how data is laid out in eor&. 4or e2a.le, atri2 seantics are alwa&s colun89ased, as
descri9ed in the rest o# this s.eci#ication, no atter what la&out =uali#iers are 9eing used.
6ni#or 9lock la&out =uali#iers can 9e declared #or glo9al sco.e, on a single uni#or 9lock, or on a single
9lock e9er declaration.
De#ault la&outs are esta9lished at glo9al sco.e #or uni#or 9locks as
la&out#la'out-1uali(ier-i#-list) uniform,
Ahen this is done, the .revious de#ault =uali#ication is #irst inherited and then overridden as .er the
override rules listed 9elow #or each =uali#ier listed in the declaration. -he result 9ecoes the new de#ault
=uali#ication sco.ed to su9se=uent uni#or 9lock de#initions.
-he initial state o# co.ilation is as i# the #ollowing were declared:
la&out#s.ared1 %olumn_ma7or) uniform,
E2.licitl& declaring this in a shader will return de#aults 9ack to their initial state.
6ni#or 9locks can 9e declared with o.tional la&out =uali#iers, and so can their individual e9er
declarations. %uch 9lock la&out =uali#ication is sco.ed onl& to the content o# the 9lock. As with glo9al
la&out declarations, 9lock la&out =uali#ication #irst inherits #ro the current de#ault =uali#ication and then
overrides it. %iilarl&, individual e9er la&out =uali#ication is sco.ed @ust to the e9er declaration,
and inherits #ro and overrides the 9lockHs =uali#ication.
-he share# =uali#ier overrides onl& the st#140 and ac.e# =uali#iersL other =uali#iers are inherited. -he
co.ilerOlinker will ensure that ulti.le .rogras and .rograa9le stages containing this de#inition
will share the sae eor& la&out #or this 9lock, as long as the& also atched in their ro/)ma9or andOor
column)ma9or =uali#ications. -his allows use o# the sae 9u##er to 9ack the sae 9lock de#inition across
di##erent .rogras.
-he ac.e# =uali#ier overrides onl& st#140 and share#L other =uali#iers are inherited. Ahen ac.e# is
used, no sharea9le la&out is guaranteed. -he co.iler and linker can o.tii?e eor& use 9ased on what
varia9les activel& get used and on other criteria. )##sets ust 9e =ueried, as there is no other wa& o#
guaranteeing where Fand whichG varia9les reside within the 9lock. Atte.ts to share a .acked uni#or
9lock across .rogras or stages will generall& #ail. +owever, i.leentations a& aid a..lication
anageent o# .acked 9locks 9& using canonical la&outs #or .acked 9locks.
-he st#140 =uali#ier overrides onl& the ac.e# and share# =uali#iersL other =uali#iers are inherited. -he
la&out is e2.licitl& deterined 9& this, as descri9ed in section !.11.* D6ni#or Varia9lesE under %tandard
6ni#or Block 1a&out o# the ).en01 0ra.hics %&ste %.eci#ication. +ence, as in share# a9ove, the
resulting la&out is sharea9le across .rogras.
1a&out =uali#iers on e9er declarations cannot use the share#, ac.e#, or st#140 =uali#iers. -hese can
onl& 9e used at glo9al sco.e or on a 9lock declaration.
-he ro/)ma9or =uali#ier overrides onl& the column)ma9or =uali#ierL other =uali#iers are inherited. It onl&
a##ects the la&out o# atrices. Eleents within a atri2 row will 9e contiguous in eor&.
5"
4 #aria$%es and &'pes
-he column)ma9or =uali#ier overrides onl& the ro/)ma9or =uali#ierL other =uali#iers are inherited. It onl&
a##ects the la&out o# atrices. Eleents within a atri2 colun will 9e contiguous in eor&.
Ahen ulti.le arguents are listed in a la0out declaration, the a##ect will 9e the sae as i# the& were
declared one at a tie, in order #ro le#t to right, each in turn inheriting #ro and overriding the result
#ro the .revious =uali#ication.
4or e2a.le
la&out#ro/_ma7or1 %olumn_ma7or)
results in the =uali#ication 9eing column)ma9or. )ther e2a.les:
la&out#s.ared1 ro/_ma7or) uniform, -- default is no/ s.ared and ro/_ma7or
la&out#std'8=) uniform Transform ) -- la&out of t.is $lo%: is std'8=
mat8 ?', -- ro/_ma7or
la&out#%olumn_ma7or) mat8 ?9, -- %olumn ma7or
mat; N', -- ro/_ma7or
3,
uniform T9 ) -- la&out of t.is $lo%: is s.ared
+++
3,
la&out#%olumn_ma7or) uniform T; ) -- s.ared and %olumn_ma7or
mat8 ?;, -- %olumn_ma7or
la&out#ro/_ma7or) mat8 m8, -- ro/ ma7or
mat; N9, -- %olumn_ma7or
3,
&.#.9 Interpolation
-he .resence o# and t&.e o# inter.olation is controlled 9& the storage =uali#iers centroid in, sample in,
centroid out, and sample out, 9& the o.tional inter.olation =uali#iers smooth, flat, and noperspective,
and 9& de#ault 9ehaviors esta9lished through the ).en01 A3I when no inter.olation =uali#ier is .resent.
Ahen an inter.olation =uali#ier is used, it overrides settings esta9lished through the ).en01 A3I. It is a
co.ile8tie error to use ore than one inter.olation =uali#ier.
A varia9le =uali#ied as flat will not 9e inter.olated. Instead, it will have the sae value #or ever&
#ragent within a triangle. -his value will coe #ro a single .rovoking verte2, as descri9ed 9& the
).en01 0ra.hics %&ste %.eci#ication. A varia9le a& 9e =uali#ied as flat centroid or flat sample,
which will ean the sae thing as =uali#&ing it onl& as flat.
A varia9le =uali#ied as smooth will 9e inter.olated in a .ers.ective8correct anner over the .riitive
9eing rendered. Inter.olation in a .ers.ective correct anner is s.eci#ied in e=uation './ in the ).en01
0ra.hics %&ste %.eci#ication, section '., D1ine %egentsE.
A varia9le =uali#ied as noperspective ust 9e inter.olated linearl& in screen s.ace, as descri9ed in
e=uation '.5 in the ).en01 0ra.hics %&ste %.eci#ication, section '., D1ine %egentsE.
51
4 #aria$%es and &'pes
Ahen ulti8sa.le rasteri?ation is disa9led, or #or #ragent shader in.ut varia9les =uali#ied with neither
centroid in nor sample in, the value o# the assigned varia9le a& 9e inter.olated an&where within the
.i2el and a single value a& 9e assigned to each sa.le within the .i2el, to the e2tent .eritted 9& the
).en01 0ra.hics %&ste %.eci#ication.
Ahen ultisa.le rasteri?ation is ena9led, centroid and sample a& 9e used to control the location and
#re=uenc& o# the sa.ling o# the =uali#ied #ragent shader in.ut. I# a #ragent shader in.ut is =uali#ied
with centroid in, a single value a& 9e assigned to that varia9le #or all sa.les in the .i2el, 9ut that value
ust 9e inter.olated to a location that lies in 9oth the .i2el and in the .riitive 9eing rendered, including
an& o# the .i2elHs sa.les covered 9& the .riitive. Because the sa.le to which the varia9le is
inter.olated a& 9e di##erent in neigh9oring .i2els, derivatives o# centroid8sa.led in.uts a& 9e less
accurate than those #or non8centroid inter.olated varia9les. I# a #ragent shader in.ut is =uali#ied with
sample in, a se.arate value ust 9e assigned to that varia9le #or each covered sa.le in the .i2el, and
that value ust 9e sa.led at the location o# the individual sa.le.
-he t&.e and .resence o# the inter.olation =uali#iers and storage =uali#iers and invariant =uali#iers o#
varia9les with the sae nae declared in all linked shaders ust atch, otherwise the link coand will
#ail.
&.#.9.1 Redeclaring /uilt7in Interpolation "ariables in the Compatibilit! -rofile
-he #ollowing .redeclared varia9les can 9e redeclared with an inter.olation =uali#ier when using the
co.ati9ilit& .ro#ile:
Verte2 and geoetr& languages:
gl_FrontEolor
gl_6a%:Eolor
gl_FrontSe%ondar&Eolor
gl_6a%:Se%ondar&Eolor
4ragent language:
gl_Eolor
gl_Se%ondar&Eolor
4or e2a.le,
in ve%8 gl_Eolor, -- prede%lared $& t.e fragment language
flat in ve%8 gl_Eolor, -- rede%lared $& user to $e flat
flat in ve%8 gl_FrontEolor, -- input to geometr& s.ader1 no 4gl_inBC5
flat out ve%8 gl_FrontEolor, -- output from geometr& s.ader
In.ut or out.ut instance naes on 9locks are not used when redeclaring 9uilt8in varia9les.
I# gl)8olor is redeclared with an inter.olation =uali#ier, then gl)-ront8olor and gl):ac.8olor Fi# the&
are written toG ust also 9e redeclared with the sae inter.olation =uali#ier, and vice versa. I#
gl)"econ#ar'8olor is redeclared with an inter.olation =uali#ier, then gl)-ront"econ#ar'8olor and
gl):ac."econ#ar'8olor Fi# the& are written toG ust also 9e redeclared with the sae inter.olation
=uali#ier, and vice versa. -his =uali#ier atching on .redeclared varia9les is onl& re=uired #or varia9les
that are staticall& used within the shaders in a .rogra.
52
4 #aria$%es and &'pes
&.& -arameter :ualifiers
3araeters can have these =uali#iers.
:ualifier 8eaning
Q none: de#ault R sae is in
in #or #unction .araeters .assed into a #unction
out #or #unction .araeters .assed 9ack out o# a #unction, 9ut not initiali?ed
#or use when .assed in
inout #or #unction .araeters .assed 9oth into and out o# a #unction
3araeter =uali#iers are discussed in ore detail in section /.1.1 D4unction "alling "onventionsE.
&.) -recision and -recision :ualifiers
3recision =uali#iers are added #or code .orta9ilit& with ).en01 E%, not #or #unctionalit&. -he& have the
sae s&nta2 as in ).en01 E%, as descri9ed 9elow, 9ut the& have no seantic eaning, which includes no
e##ect on the .recision used to store or o.erate on varia9les.
I# an e2tension adds in the sae seantics and #unctionalit& in the ).en01 E% !.( s.eci#ication #or
.recision =uali#iers, then the e2tension is allowed to reuse the ke&words 9elow #or that .ur.ose.
&.).1 Range and -recision
%ection nu9er reserved #or #uture use.
&.).2 -recision :ualifiers
An& #loating .oint or an& integer declaration can have the t&.e .receded 9& one o# these .recision
=uali#iers:
:ualifier 8eaning
highp 7one.
mediump 7one.
lowp 7one.
53
4 #aria$%es and &'pes
4or e2a.le:
lo/p float %olor,
out mediump ve%9 @,
lo/p ive%9 foo#lo/p mat;),
.ig.p mat8 m,
1iteral constants do not have .recision =uali#iers. 7either do Boolean varia9les. 7either do #loating .oint
constructors nor integer constructors when none o# the constructor arguents have .recision =uali#iers.
3recision =uali#iers, as with other =uali#iers, do not e##ect the 9asic t&.e o# the varia9le. In .articular,
there are no constructors #or .recision conversionsL constructors onl& convert t&.es. %iilarl&, .recision
=uali#iers, as with other =uali#iers, do not contri9ute to #unction overloading 9ased on .araeter 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 atch.
-he sae o9@ect declared in di##erent shaders that are linked together ust have the sae .recision
=uali#ication. -his a..lies to in.uts, out.uts, uni#ors, and glo9als.
&.).# +efault -recision :ualifiers
-he .recision stateent
pre%ision pre%ision2Aualifier t&pe,
can 9e used to esta9lish a de#ault .recision =uali#ier. -he t0pe #ield can 9e either int or float, and the
recision-1uali(ier can 9e 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 non8.recision8=uali#ied #loating .oint t&.e Fscalar, vector, and
atri2G declarations. I# t'e is int, the directive a..lies to all non8.recision8=uali#ied integer t&.e Fscalar,
vector, signed, and unsignedG declarations. -his includes glo9al varia9le declarations, #unction return
declarations, #unction .araeter declarations, and local varia9le declarations.
7on8.recision =uali#ied declarations will use the .recision =uali#ier s.eci#ied in the ost recent precision
stateent that is still in sco.e. -he precision stateent has the sae sco.ing rules as varia9le
declarations. I# it is declared inside a co.ound stateent, its e##ect sto.s at the end o# the innerost
stateent it was declared in. 3recision stateents in nested sco.es override .recision stateents in outer
sco.es. :ulti.le .recision stateents #or the sae 9asic t&.e can a..ear inside the sae sco.e, with later
stateents overriding earlier stateents within that sco.e.
-he verte2 and geoetr& languages have the #ollowing .redeclared glo9all& sco.ed de#ault .recision
stateents:
pre%ision .ig.p float,
pre%ision .ig.p int,
-he #ragent language has the #ollowing .redeclared glo9all& sco.ed de#ault .recision stateents:
pre%ision mediump int,
pre%ision .ig.p float,
54
4 #aria$%es and &'pes
&.).& 0vailable -recision :ualifiers
-he 9uilt8in acro 01>4RA0:E7->3RE"I%I)7>+I0+ is de#ined to 1:
#define !L_FR>!?ENT_@REEISION_II!I '
-his acro is availa9le in the verte2, geoetr&, and #ragent languages.
&.* "ariance and the Invariant :ualifier
In this section, variance re#ers to the .ossi9ilit& o# getting di##erent values #ro the sae e2.ression in
di##erent .rogras. 4or e2a.le, sa& two verte2 shaders, in di##erent .rogras, each set gl);osition with
the sae e2.ression in 9oth shaders, and the in.ut values into that e2.ression are the sae when 9oth
shaders run. It is .ossi9le, due to inde.endent co.ilation o# the two shaders, that the values assigned to
gl);osition are not e2actl& the sae when the two shaders run. In this e2a.le, this can cause .ro9les
with alignent o# geoetr& in a ulti8.ass algorith.
In general, such variance 9etween shaders is allowed. Ahen such variance does not e2ist #or a .articular
out.ut varia9le, that varia9le is said to 9e invariant.
&.*.1 The Invariant :ualifier
-o ensure that a .articular out.ut varia9le is invariant, it is necessar& to use the invariant =uali#ier. It can
either 9e used to =uali#& a .reviousl& declared varia9le as 9eing invariant
invariant gl_@osition, -- ma:e existing gl_@osition $e invariant
out ve%; Eolor,
invariant Eolor, -- ma:e existing Eolor $e invariant
or as .art o# a declaration when a varia9le is declared
invariant %entroid out ve%; Eolor,
-he invariant =uali#ier ust a..ear 9e#ore an& inter.olation =uali#iers or storage =uali#iers when
co9ined with a declaration. )nl& varia9les out.ut #ro a shader Fincluding those that are then in.ut to a
su9se=uent shaderG can 9e candidates #or invariance. -his includes user8de#ined out.ut varia9les and the
9uilt8in out.ut varia9les. 4or varia9les leaving one shader and coing into another shader, the invariant
ke&word has to 9e used in 9oth shaders, or a link error will result.
In.ut or out.ut instance naes on 9locks are not used when redeclaring 9uilt8in varia9les.
-he invariant ke&word can 9e #ollowed 9& a coa se.arated list o# .reviousl& declared identi#iers. All
uses o# invariant ust 9e at the glo9al sco.e, and 9e#ore an& use o# the varia9les 9eing declared as
invariant.
-o guarantee invariance o# a .articular out.ut varia9le across two .rogras, the #ollowing ust also 9e
true:
-he out.ut varia9le is declared as invariant in 9oth .rogras.
-he sae values ust 9e in.ut to all shader in.ut varia9les consued 9& e2.ressions and #low control
contri9uting to the value assigned to the out.ut varia9le.
55
4 #aria$%es and &'pes
-he te2ture #orats, te2el values, and te2ture #iltering are set the sae wa& #or an& te2ture #unction
calls contri9uting to the value o# the out.ut varia9le.
All in.ut values are all o.erated on in the sae wa&. All o.erations in the consuing e2.ressions and
an& interediate e2.ressions ust 9e the sae, with the sae order o# o.erands and sae
associativit&, to give the sae order o# evaluation. Interediate varia9les and #unctions ust 9e
declared as the sae t&.e with the sae e2.licit or i.licit .recision =uali#iers. An& control #low
a##ecting the out.ut value ust 9e the sae, and an& e2.ressions consued to deterine this control
#low ust also #ollow these invariance rules.
All the data #low and control #low leading to setting the invariant out.ut varia9le reside in a single
co.ilation unit.
Essentiall&, all the data #low and control #low leading to an invariant out.ut ust atch.
Initiall&, 9& de#ault, all out.ut varia9les are allowed to 9e variant. -o #orce all out.ut varia9les to 9e
invariant, use the .raga
#pragma ST!L invariant#all)
9e#ore all declarations in a shader. I# this .raga is used a#ter the declaration o# an& varia9les or
#unctions, then the set o# out.uts that 9ehave as invariant is unde#ined. It is an error to use this .raga in
a #ragent shader.
0enerall&, invariance is ensured at the cost o# #le2i9ilit& in o.tii?ation, so .er#orance can 9e degraded
9& use o# invariance. +ence, use o# this .raga is intended as a de9ug aid, to avoid individuall& declaring
all out.ut varia9les as invariant.
&.*.2 Invariance of Constant ',pressions
Invariance ust 9e guaranteed #or constant e2.ressions. A .articular constant e2.ression ust evaluate to
the sae result i# it a..ears again in the sae shader or a di##erent shader. -his includes the sae
e2.ression a..earing two shaders o# the sae language or shaders o# two di##erent languages.
"onstant e2.ressions ust evaluate to the sae result when o.erated on as alread& descri9ed a9ove #or
invariant varia9les.
&.4 The -recise :ualifier
%oe algoriths re=uire #loating8.oint co.utations to e2actl& #ollow the order o# o.erations s.eci#ied in
the source code and to treat all o.erations consistentl&, even i# the i.leentation su..orts o.tii?ations
that could .roduce nearl& e=uivalent results with higher .er#orance. 4or e2a.le, an& 01
i.leentations su..ort a Wulti.l&8addW instruction that can co.ute a #loating8.oint e2.ression such as
result * #a J $) K #% J d),
56
4 #aria$%es and &'pes
in two o.erations instead o# three o.erationsL one ulti.l& and one ulti.l&8add instead o# two ulti.lies
and one add. -he result o# a #loating8.oint ulti.l&8add a& not alwa&s 9e identical to #irst doing a
ulti.l& &ielding a #loating8.oint result and then doing a #loating8.oint add. +ence, in this e2a.le, the
two ulti.l& o.erations would not 9e treated consistentl&L the two ulti.lies could e##ectivel& a..ear to
have di##ering .recisions. Aithout an& =uali#iers, i.leentations are .eritted to .er#or such
o.tii?ations that e##ectivel& odi#& the order or nu9er o# o.erations used to evaluate an e2.ression,
even i# those o.tii?ations a& .roduce slightl& di##erent results relative to uno.tii?ed code.
-he =uali#ier precise will ensure that o.erations contri9uting to a varia9leHs value are done in their stated
order and are done with o.erator consistenc&. )rder is deterined 9& o.erator .recedence and
.arenthesis, as descri9ed in section ,.1 D).eratorsE. ).erator consistenc& eans #or each .articular
o.erator, #or e2a.le the ulti.l& o.erator F = G, its o.eration is alwa&s co.uted with the sae
.recision. 7ote this e##ectivel& .revents #using ulti.le o.erations into a single o.eration.
4or e2a.le,
pre%ise out ve%8 position,
declares that o.erations used to .roduce the value o# osition ust 9e .er#ored in e2actl& the order
s.eci#ied in the source code and with all o.erators 9eing treated consistentl&. As with the invariant
=uali#ier Fsection *./.1G, the .recise =uali#ier a& 9e used to =uali#& a 9uilt8in or .reviousl& declared user8
de#ined varia9le as 9eing .recise:
out ve%; Eolor,
pre%ise Eolor, -- ma:e existing Eolor $e pre%ise
-his =uali#ier will a##ect the evaluation o# an r8value in a .articular #unction i# and onl& i# the result is
eventuall& consued in the sae #unction 9& an l8value =uali#ied as precise. An& other e2.ressions
within a #unction are not a##ected, including return values and out.ut .araeters not declared as precise
9ut that are eventuall& consued outside the #unction 9& an varia9le =uali#ied as precise.
57
4 #aria$%es and &'pes
%oe e2a.les o# the use o# precise:
in ve%8 a1 $1 %1 d,
pre%ise out ve%8 v,
float fun%#float e1 float f1 float g1 float .)
)
return #eJf) K #gJ.), -- no %onstraint on order or
-- operator %onsisten%&
3
float fun%9#float e1 float f1 float g1 float .)
)
pre%ise float result * #eJf) K #gJ.), -- ensures same pre%ision for
-- t.e t/o multiplies
return result,
3
float fun%;#float i1 float 71 pre%ise out float :)
)
: * i J i K 7, -- pre%ise1 due to 0:L de%laration
3
void main#)
)
ve%8 r * ve%;#a J $), -- pre%ise1 used to %ompute v+x&"
ve%8 s * ve%;#% J d), -- pre%ise1 used to %ompute v+x&"
v+x&" * r K s, -- pre%ise
v+/ * #a+/ J $+/) K #%+/ J d+/), -- pre%ise
v+x * fun%#a+x1 $+x1 %+x1 d+x), -- values %omputed in fun%#)
-- are NOT pre%ise
v+x * fun%9#a+x1 $+x1 %+x1 d+x), -- pre%iseM
fun%;#a+x J $+x1 %+x J d+x1 v+x), -- pre%iseM
3
&. Order of :ualification
Ahen ulti.le =uali#ications are .resent, the& ust #ollow a strict order. -his order is as #ollows.
recise-1uali(ier invariant-1uali(ier interolation-1uali(ier storage-1uali(ier recision-1uali(ier
storage-1uali(ier arameter-1uali(ier recision-1uali(ier
5
) Operators and ',pressions
).1 Operators
-he ).en01 %hading 1anguage has the #ollowing o.erators.
-recedence Operator Class Operators 0ssociativit!
1 FhighestG .arenthetical grou.ing A B 7A
!
arra& su9scri.t
#unction call and constructor structure
#ield or ethod selector, swi??ler
.ost #i2 increent and decreent
' (
A B
.
;; 77
1e#t to Right
'
.re#i2 increent and decreent
unar&
;; 77
; 7 H J
Right to 1e#t
* ulti.licative = < > 1e#t to Right
, additive ; 7 1e#t to Right
/ 9it8wise shi#t ?? @@ 1e#t to Right
5 relational ? @ ?I @I 1e#t to Right
$ e=ualit& II JI 1e#t to Right
; 9it8wise and G 1e#t to Right
1( 9it8wise e2clusive or E 1e#t to Right
11 9it8wise inclusive or F 1e#t to Right
1! logical and GG 1e#t to Right
1' logical e2clusive or EE 1e#t to Right
1* logical inclusive or F F 1e#t to Right
1, selection M K Right to 1e#t
1/
Assignent
arithetic assignents
I
;I 7I
=I <I
>I ??I @@I
GI EI FI
Right to 1e#t
15 FlowestG se=uence , 1e#t to Right
-here is no address8o# o.erator nor a dere#erence o.erator. -here is no t&.ecast o.eratorL constructors
are used instead.
5!
5 Operators and ()pressions
).2 0rra! Operations
-hese are now descri9ed in section ,.5 D%tructure and Arra& ).erationsE.
).# .unction Calls
I# a #unction returns a value, then a call to that #unction a& 9e used as an e2.ression, whose t&.e will 9e
the t&.e that was used to declare or de#ine the #unction.
4unction de#initions and calling conventions are discussed in section /.1 D4unction De#initionsE .
).& Constructors
"onstructors use the #unction call s&nta2, where the #unction nae is a t&.e, and the call akes an o9@ect
o# that t&.e. "onstructors are used the sae wa& in 9oth initiali?ers and e2.ressions. F%ee section ;
D%hading 1anguage 0raarE #or details.G -he .araeters are used to initiali?e the constructed value.
"onstructors can 9e used to re=uest a data t&.e conversion to change #ro one scalar t&.e to another
scalar t&.e, or to 9uild larger t&.es out o# saller t&.es, or to reduce a larger t&.e to a saller t&.e.
In general, constructors are not 9uilt8in #unctions with .redeterined .rotot&.es. 4or arra&s and
structures, there ust 9e e2actl& one arguent in the constructor #or each eleent or #ield. 4or the other
t&.es, the arguents ust .rovide a su##icient nu9er o# co.onents to .er#or the initiali?ation, and it
is an error to include so an& arguents that the& cannot all 9e used. Detailed rules #ollow. -he
.rotot&.es actuall& listed 9elow are erel& a su9set o# e2a.les.
).&.1 Conversion and Scalar Constructors
"onverting 9etween scalar t&.es is done as the #ollowing .rotot&.es indicate:
int#uint) -- %onverts an unsigned integer to a signed integer
int#$ool) -- %onverts a 6oolean value to an int
int#float) -- %onverts a float value to an int
int#dou$le) -- %onverts a dou$le value to a signed integer
uint#int) -- %onverts a signed integer value to an unsigned integer
uint#$ool) -- %onverts a 6oolean value to an unsigned integer
uint#float) -- %onverts a float value to an unsigned integer
uint#dou$le) -- %onverts a dou$le value to an unsigned integer
$ool#int) -- %onverts a signed integer value to a 6oolean
$ool#uint) -- %onverts an unsigned integer value to a 6oolean value
$ool#float) -- %onverts a float value to a 6oolean
$ool#dou$le) -- %onverts a dou$le value to a 6oolean
float#int) -- %onverts a signed integer value to a float
float#uint) -- %onverts an unsigned integer value to a float value
float#$ool) -- %onverts a 6oolean value to a float
float#dou$le)-- %onverts a dou$le value to a float
dou$le#int) -- %onverts a signed integer value to a dou$le
dou$le#uint) -- %onverts an unsigned integer value to a dou$le
dou$le#$ool) -- %onverts a 6oolean value to a dou$le
dou$le#float)-- %onverts a float value to a dou$le
6"
5 Operators and ()pressions
Ahen constructors are used to convert an& #loating8.oint t&.e to an integer t&.e, the #ractional .art o# the
#loating8.oint value is dro..ed. It is unde#ined to convert a negative #loating .oint value to an uint.
Ahen a constructor is used to convert an& integer or #loating8.oint t&.e to a bool, ( and (.( are converted
to false, and non8?ero values are converted to true. Ahen a constructor is used to convert a bool to an&
integer or #loating8.oint t&.e, false is converted to ( or (.(, and true is converted to 1 or 1.(.
-he constructor intFuintG .reserves the 9it .attern in the arguent, which will change the arguentHs
value i# its sign 9it is set. -he constructor uintFintG .reserves the 9it .attern in the arguent, which will
change its value i# it is negative.
Identit& constructors, like floatFfloatG are also legal, 9ut o# little use.
%calar constructors with non8scalar .araeters can 9e used to take the #irst eleent #ro a non8scalar.
4or e2a.le, the constructor floatFvec/G will select the #irst co.onent o# the vec/ .araeter.
).&.2 "ector and 8atri, Constructors
"onstructors can 9e used to create vectors or atrices #ro a set o# scalars, vectors, or atrices. -his
includes the a9ilit& to shorten vectors.
I# there is a single scalar .araeter to a vector constructor, it is used to initiali?e all co.onents o# the
constructed vector to that scalarKs value. I# there is a single scalar .araeter to a atri2 constructor, it is
used to initiali?e all the co.onents on the atri2Ks diagonal, with the reaining co.onents initiali?ed
to (.(.
I# a vector is constructed #ro ulti.le scalars, one or ore vectors, or one or ore atrices, or a i2ture
o# these, the vectorHs co.onents will 9e constructed in order #ro the co.onents o# the arguents. -he
arguents will 9e consued le#t to right, and each arguent will have all its co.onents consued, in
order, 9e#ore an& co.onents #ro the ne2t arguent are consued. %iilarl& #or constructing a atri2
#ro ulti.le scalars or vectors, or a i2ture o# these. :atri2 co.onents will 9e constructed and
consued in colun a@or order. In these cases, there ust 9e enough co.onents .rovided in the
arguents to .rovide an initiali?er #or ever& co.onent in the constructed value. It is an error to .rovide
e2tra arguents 9e&ond this last used arguent.
I# a atri2 is constructed #ro a atri2, then each co.onent Fcolun i$ row 9G in the result that has a
corres.onding co.onent Fcolun i$ row 9G in the arguent will 9e initiali?ed #ro there. All other
co.onents will 9e initiali?ed to the identit& atri2. I# a atri2 arguent is given to a atri2 constructor,
it is an error to have an& other arguents.
I# the 9asic t&.e Fbool, int, float, or doubleG o# a .araeter to a constructor does not atch the 9asic t&.e
o# the o9@ect 9eing constructed, the scalar construction rules Fa9oveG are used to convert the .araeters.
61
5 Operators and ()pressions
%oe use#ul vector constructors are as #ollows:
ve%;#float) -- initiali"es ea%. %omponent of t.e ve%; /it. t.e float
ve%8#ive%8) -- ma:es a ve%8 /it. %omponent2/ise %onversion
ve%8#mat9) -- t.e ve%8 is %olumn = follo/ed $& %olumn '
ve%9#float1 float) -- initiali"es a ve%9 /it. 9 floats
ive%;#int1 int1 int) -- initiali"es an ive%; /it. ; ints
$ve%8#int1 int1 float1 float) -- uses 8 6oolean %onversions
ve%9#ve%;) -- drops t.e t.ird %omponent of a ve%;
ve%;#ve%8) -- drops t.e fourt. %omponent of a ve%8
ve%;#ve%91 float) -- ve%;+x * ve%9+x1 ve%;+& * ve%9+&1 ve%;+" * float
ve%;#float1 ve%9) -- ve%;+x * float1 ve%;+& * ve%9+x1 ve%;+" * ve%9+&
ve%8#ve%;1 float)
ve%8#float1 ve%;)
ve%8#ve%91 ve%9)
%oe e2a.les o# these are:
ve%8 %olor * ve%8#=+=1 '+=1 =+=1 '+=),
ve%8 rg$a * ve%8#'+=), -- sets ea%. %omponent to '+=
ve%; rg$ * ve%;#%olor), -- drop t.e 8t. %omponent
-o initiali?e the diagonal o# a atri2 with all other eleents set to ?ero:
mat9#float)
mat;#float)
mat8#float)
-hat is, result5i6596 is set to the #loat arguent #or all i < 9 and set to ( #or all i 9.
62
5 Operators and ()pressions
-o initiali?e a atri2 9& s.eci#&ing vectors or scalars, the co.onents are assigned to the atri2 eleents
in colun8a@or order.
mat9#ve%91 ve%9), -- one %olumn per argument
mat;#ve%;1 ve%;1 ve%;), -- one %olumn per argument
mat8#ve%81 ve%81 ve%81 ve%8), -- one %olumn per argument
mat;x9#ve%91 ve%91 ve%9), -- one %olumn per argument
dmat9#dve%91 dve%9),
dmat;#dve%;1 dve%;1 dve%;),
dmat8#dve%81 dve%81 dve%81 dve%8),
mat9#float1 float1 -- first %olumn
float1 float), -- se%ond %olumn
mat;#float1 float1 float1 -- first %olumn
float1 float1 float1 -- se%ond %olumn
float1 float1 float), -- t.ird %olumn
mat8#float1 float1 float1 float1 -- first %olumn
float1 float1 float1 float1 -- se%ond %olumn
float1 float1 float1 float1 -- t.ird %olumn
float1 float1 float1 float), -- fourt. %olumn
mat9x;#ve%91 float1 -- first %olumn
ve%91 float), -- se%ond %olumn
dmat9x8#dve%;1 dou$le1 -- first %olumn
dou$le1 dve%;) -- se%ond %olumn
A wide range o# other .ossi9ilities e2ist, to construct a atri2 #ro vectors and scalars, as long as enough
co.onents are .resent to initiali?e the atri2. -o construct a atri2 #ro a atri2:
mat;x;#mat8x8), -- ta:es t.e upper2left ;x; of t.e mat8x8
mat9x;#mat8x9), -- ta:es t.e upper2left 9x9 of t.e mat8x81 last ro/ is =1=
mat8x8#mat;x;), -- puts t.e mat;x; in t.e upper2left1 sets t.e lo/er rig.t
-- %omponent to '1 and t.e rest to =
).&.# Structure Constructors
)nce a structure is de#ined, and its t&.e is given a nae, a constructor is availa9le with the sae nae to
construct instances o# that structure. 4or e2a.le:
stru%t lig.t )
float intensit&,
ve%; position,
3,
lig.t lig.tVar * lig.t#;+=1 ve%;#'+=1 9+=1 ;+=)),
63
5 Operators and ()pressions
-he arguents to the constructor will 9e used to set the structureHs #ields, in order, using one arguent .er
#ield. Each arguent ust 9e the sae t&.e as the #ield it sets, or 9e a t&.e that can 9e converted to the
#ieldHs t&.e according to section *.1.1( DI.licit "onversions.E
%tructure constructors can 9e used as initiali?ers or in e2.ressions.
).&.& 0rra! Constructors
Arra& t&.es can also 9e used as constructor naes, which can then 9e used in e2.ressions or initiali?ers.
4or e2a.le,
%onst float %B;C * floatB;C#<+=1 F+91 '+'),
%onst float dB;C * floatBC#<+=1 F+91 '+'),
float g,
+++
float aB<C * floatB<C#g1 '1 g1 9+;1 g),
float $B;C,
$ * floatB;C#g1 g K '+=1 g K 9+=),
-here ust 9e e2actl& the sae nu9er o# arguents as the si?e o# the arra& 9eing constructed. I# no si?e
is .resent in the constructor, then the arra& is e2.licitl& si?ed to the nu9er o# arguents .rovided. -he
arguents are assigned in order, starting at eleent (, to the eleents o# the constructed arra&. Each
arguent ust 9e the sae t&.e as the eleent t&.e o# the arra&, or 9e a t&.e that can 9e converted to the
eleent t&.e o# the arra& according to section *.1.1( DI.licit "onversions.E
).) "ector Components
-he naes o# the co.onents o# a vector are denoted 9& a single letter. As a notational convenience,
several letters are associated with each co.onent 9ased on coon usage o# .osition, color or te2ture
coordinate vectors. -he individual co.onents o# a vector can 9e selected 9& #ollowing the varia9le
nae with .eriod F . G and then the co.onent nae.
-he co.onent naes su..orted are:
=%$ '$ 0$ /> 6se#ul when accessing vectors that re.resent .oints or norals
=r$ g$ &$ a> 6se#ul when accessing vectors that re.resent colors
=s$ t$ $ 1> 6se#ul when accessing vectors that re.resent te2ture coordinates
-he co.onent naes %$ r$ and s are, #or e2a.le, s&non&s #or the sae F#irstG co.onent in a vector.
7ote that the third co.onent o# the te2ture coordinate set, r in ).en01, has 9een renaed so as to
avoid the con#usion with r F#or redG in a color.
Accessing co.onents 9e&ond those declared #or the vector t&.e is an error so, #or e2a.le:
64
5 Operators and ()pressions
ve%9 pos,
pos+x -- is legal
pos+" -- is illegal
-he co.onent selection s&nta2 allows ulti.le co.onents to 9e selected 9& a..ending their naes
F#ro the sae nae setG a#ter the .eriod F . G.
ve%8 v8,
v8+rg$a, -- is a ve%8 and t.e same as 7ust using v81
v8+rg$, -- is a ve%;1
v8+$, -- is a float1
v8+x&, -- is a ve%91
v8+xg$a, -- is illegal 2 t.e %omponent names do not %ome from
-- t.e same set+
-he order o# the co.onents can 9e di##erent to swi??le the, or re.licated:
ve%8 pos * ve%8#'+=1 9+=1 ;+=1 8+=),
ve%8 s/i"* pos+/"&x, -- s/i" * #8+=1 ;+=1 9+=1 '+=)
ve%8 dup * pos+xx&&, -- dup * #'+=1 '+=1 9+=1 9+=)
-his notation is ore concise than the constructor s&nta2. -o #or an r8value, it can 9e a..lied to an&
e2.ression that results in a vector r8value.
-he co.onent grou. notation can occur on the le#t hand side o# an e2.ression.
ve%8 pos * ve%8#'+=1 9+=1 ;+=1 8+=),
pos+x/ * ve%9#<+=1 G+=), -- pos * #<+=1 9+=1 ;+=1 G+=)
pos+/x * ve%9#F+=1 N+=), -- pos * #N+=1 9+=1 ;+=1 F+=)
pos+xx * ve%9#;+=1 8+=), -- illegal 2 DxD used t/i%e
pos+x& * ve%;#'+=1 9+=1 ;+=), -- illegal 2 mismat%. $et/een ve%9 and ve%;
-o #or an l8value, swi??ling ust 9e a..lied to an l8value o# vector t&.e, contain no du.licate
co.onents, and it results in an l8value o# scalar or vector t&.e, de.ending on nu9er o# co.onents
s.eci#ied.
Arra& su9scri.ting s&nta2 can also 9e a..lied to vectors to .rovide nueric inde2ing. %o in
ve%8 pos,
os526 re#ers to the third eleent o# .os and is e=uivalent to os.0. -his allows varia9le inde2ing into a
vector, as well as a generic wa& o# accessing co.onents. An& integer e2.ression can 9e used as the
su9scri.t. -he #irst co.onent is at inde2 ?ero. Reading #ro 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.
Ahen inde2ing with non8constant e2.ressions, 9ehavior is unde#ined i# the inde2 is negative, or greater
than or e=ual to the si?e o# the vector.
65
5 Operators and ()pressions
).* 8atri, Components
-he co.onents o# a atri2 can 9e accessed using arra& su9scri.ting s&nta2. A..l&ing a single su9scri.t
to a atri2 treats the atri2 as an arra& o# colun vectors, and selects a single colun, whose t&.e is a
vector o# the sae si?e as the atri2. -he le#tost colun is colun (. A second su9scri.t would then
o.erate on the resulting vector, as de#ined earlier #or vectors. +ence, two su9scri.ts select a colun and
then a row.
mat8 m,
mB'C * ve%8#9+=), -- sets t.e se%ond %olumn to all 9+=
mB=CB=C * '+=, -- sets t.e upper left element to '+=
mB9CB;C * 9+=, -- sets t.e 8t. element of t.e t.ird %olumn to 9+=
Behavior is unde#ined when accessing a co.onent outside the 9ounds o# a atri2 with a non8constant
e2.ression. It is an error to access a atri2 with a constant e2.ression that is outside the 9ounds o# the
atri2.
).4 Structure and 0rra! Operations
-he #ields o# a structure and the length ethod o# an arra& are selected using the .eriod F . G.
In total, onl& the #ollowing o.erators are allowed to o.erate on arra&s and structures as whole entities:
#ield or ethod selector .
e=ualit& II JI
assignent I
inde2ing Farra&s onl&G Z [
-he e=ualit& o.erators and assignent o.erator are onl& allowed i# the two o.erands are sae si?e and
t&.e. %tructure t&.es ust 9e o# the sae declared structure. Both arra& o.erands ust 9e e2.licitl&
si?ed. Ahen using the e=ualit& o.erators, two structures are e=ual i# and onl& i# all the #ields are
co.onent8wise e=ual, and two arra&s are e=ual i# and onl& i# all the eleents are eleent8wise e=ual.
Arra& eleents are accessed using the arra& su9scri.t o.erator F ' ( G. An e2a.le o# accessing an arra&
eleent is
diffuseEolor K* lig.tIntensit&B;C J NdotL,
Arra& indices start at ?ero. Arra& eleents are accessed using an e2.ression whose t&.e is int or uint.
Behavior is unde#ined i# a shader su9scri.ts an arra& with an inde2 less than ( or greater than or e=ual to
the si?e the arra& was declared with.
66
5 Operators and ()pressions
Arra&s can also 9e accessed with the ethod o.erator F . G and the length ethod to =uer& the si?e o# the
arra&:
lig.tIntensit&+lengt.#) -- return t.e si"e of t.e arra&
). 0ssignments
Assignents o# values to varia9le naes are done with the assignent o.erator F I G:
lvalue2expression * rvalue2expression
-he lvalue-e%ression evaluates to an l8value. -he assignent o.erator stores the value o# rvalue-
e%ression into the l8value and returns an r8value with the t&.e and .recision o# lvalue-e%ression. -he
lvalue-e%ression and rvalue-e%ression ust have the sae t&.e, or the e2.ression ust have a t&.e in
the ta9le in section *.1.1( DI.licit "onversionsE that converts to the t&.e o# lvalue-e%ression, in which
case an i.licit conversion will 9e done on the rvalue-e%ression 9e#ore the assignent is done. An&
other desired t&.e8conversions ust 9e s.eci#ied e2.licitl& via a constructor. 18values ust 9e writa9le.
Varia9les that are 9uilt8in t&.es, entire structures or arra&s, structure #ields, l8values with the #ield selector
F . G a..lied to select co.onents or swi??les without re.eated #ields, l8values within .arentheses, and l8
values dere#erenced with the arra& su9scri.t o.erator F ' ( G are all l8values. )ther 9inar& or unar&
e2.ressions, #unction naes, swi??les with re.eated #ields, and constants cannot 9e l8values. -he ternar&
o.erator FMKG is also not allowed as an l8value.
E2.ressions on the le#t o# an assignent are evaluated 9e#ore e2.ressions on the right o# the assignent.
-he other assignent o.erators are
add into F;IG
su9tract #ro F7IG
ulti.l& into F=IG
divide into F<IG
odulus into F>IG
le#t shi#t 9& F??IG
right shi#t 9& F@@IG
and into FGIG
inclusive8or into FFIG
e2clusive8or into FEIG
67
5 Operators and ()pressions
where the general e2.ression
lvalue op* expression
is e=uivalent to
lvalue * lvalue op expression
where o is as descri9ed 9elow, and the l8value and e2.ression ust satis#& the seantic re=uireents o#
9oth o and e=uals FIG.
Reading a varia9le 9e#ore writing For initiali?ingG it is legal, however the value is unde#ined.
).9 ',pressions
E2.ressions in the shading language are 9uilt #ro the #ollowing:
"onstants o# t&.e bool, all integer t&.es, all #loating8.oint t&.es, all vector t&.es, and all atri2 t&.es.
"onstructors o# all t&.es.
Varia9le naes o# all t&.es.
An arra& nae with the length ethod a..lied.
%u9scri.ted arra& naes.
4unction calls that return values.
"o.onent #ield selectors and arra& su9scri.t results.
3arenthesi?ed e2.ression. An& e2.ression can 9e .arenthesi?ed. 3arentheses can 9e used to grou.
o.erations. ).erations within .arentheses are done 9e#ore o.erations across .arentheses.
-he arithetic 9inar& o.erators add F;G, su9tract F7G, ulti.l& F=G, and divide F<G o.erate on integer and
#loating8.oint scalars, vectors, and atrices. I# the #undaental t&.es in the o.erands do not atch,
then the conversions #ro section *.1.1( DI.licit "onversionsE are a..lied to create atching t&.es.
All arithetic 9inar& o.erators result in the sae #undaental t&.e Fsigned integer, unsigned integer,
single8.recision #loating .oint, or dou9le8.recision #loating .ointG 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 atri2. In this case, the scalar o.eration is
a..lied inde.endentl& to each co.onent o# the vector or atri2, resulting in the sae si?e vector
or atri2.
-he two o.erands are vectors o# the sae si?e. In this case, the o.eration is done co.onent8wise
resulting in the sae si?e vector.
-he o.erator is add F;G, su9tract F7G, or divide F<G, and the o.erands are atrices with the sae
nu9er o# rows and the sae nu9er o# coluns. In this case, the o.eration is done co.onent8
wise resulting in the sae si?e atri2.
6
5 Operators and ()pressions
-he o.erator is ulti.l& F=G, where 9oth o.erands are atrices or one o.erand is a vector and the
other a atri2. A right vector o.erand is treated as a colun vector and a le#t vector o.erand as a
row vector. In all these cases, it is re=uired that the nu9er o# coluns o# the le#t o.erand is e=ual
to the nu9er o# rows o# the right o.erand. -hen, the ulti.l& F=G o.eration does a linear
alge9raic ulti.l&, &ielding an o9@ect that has the sae nu9er o# rows as the le#t o.erand and the
sae nu9er o# coluns as the right o.erand. %ection ,.1( DVector and :atri2 ).erationsE
e2.lains in ore detail how vectors and atrices are o.erated on.
All other cases are illegal.
Dividing 9& ?ero does not cause an e2ce.tion 9ut does result in an uns.eci#ied value. 6se the 9uilt8in
#unctions dot, cross, matriComp%ult, and outer6roduct, to get, res.ectivel&, vector dot .roduct,
vector cross .roduct, atri2 co.onent8wise ulti.lication, and the atri2 .roduct o# a colun
vector ties a row vector.
-he o.erator odulus F>G o.erates on signed or unsigned integer scalars or integer vectors. I# the
#undaental t&.es in the o.erands do not atch, then the conversions #ro section *.1.1( DI.licit
"onversionsE are a..lied to create atching t&.es. -he o.erands cannot 9e vectors o# di##ering si?e.
I# one o.erand is a scalar and the other vector, then the scalar is a..lied co.onent8wise to the vector,
resulting in the sae t&.e as the vector. I# 9oth are vectors o# the sae si?e, the result is co.uted
co.onent8wise. -he resulting value is unde#ined #or an& co.onent co.uted with a second
o.erand that is ?ero, while results #or other co.onents with non8?ero second o.erands reain
de#ined. I# 9oth o.erands are non8negative, then the reainder is non8negative. Results are unde#ined
i# one or 9oth o.erands are negative. -he o.erator odulus F>G is not de#ined #or an& other data
t&.es Fnon8integer t&.esG.
-he arithetic unar& o.erators negate F8G, .ost8 and .re8increent and decreent F77 and ;;G o.erate
on integer or #loating8.oint values Fincluding vectors and atricesG. All unar& o.erators work
co.onent8wise on their o.erands. -hese result with the sae t&.e the& o.erated on. 4or .ost8 and
.re8increent and decreent, the e2.ression ust 9e one that could 9e assigned to Fan l8valueG. 3re8
increent and .re8decreent add or su9tract 1 or 1.( to the contents o# the e2.ression the& o.erate on,
and the value o# the .re8increent or .re8decreent e2.ression is the resulting value o# that
odi#ication. 3ost8increent and .ost8decreent e2.ressions add or su9tract 1 or 1.( to the contents
o# the e2.ression the& o.erate on, 9ut the resulting e2.ression has the e2.ressionKs value 9e#ore the
.ost8increent or .ost8decreent was e2ecuted.
-he relational o.erators greater than F@G, less than F?G, greater than or e=ual F@IG, and less than or
e=ual F?IG o.erate onl& on scalar integer and scalar #loating8.oint e2.ressions. -he result is scalar
Boolean. Either the o.erandsK t&.es ust atch, or the conversions #ro section *.1.1( DI.licit
"onversionsE will 9e a..lied to o9tain atching t&.es. -o do co.onent8wise relational co.arisons
on vectors, use the 9uilt8in #unctions lessOhan, lessOhan2Qual, greaterOhan, and
greaterOhan2Qual.
-he e=ualit& o.erators eQual FIIG, and not e=ual FJIG o.erate on all t&.es. -he& result in a scalar
Boolean. I# the o.erand t&.es do not atch, then there ust 9e a conversion #ro section *.1.1(
DI.licit "onversionsE a..lied to one o.erand that can ake the atch, in which case this
conversion is done. 4or vectors, atrices, structures, and arra&s, all co.onents, #ields, or eleents o#
one o.erand ust e=ual the corres.onding co.onents, #ields, or eleents in the other o.erand #or the
o.erands to 9e considered e=ual. -o get a vector o# co.onent8wise e=ualit& results #or vectors, use
the 9uilt8in #unctions eQual and not2Qual.
6!
5 Operators and ()pressions
-he logical 9inar& o.erators and FGGG, or F F F G, and e2clusive or FEEG o.erate onl& on two Boolean
e2.ressions and result in a Boolean e2.ression. And FGGG will onl& evaluate the right hand o.erand
i# the le#t hand o.erand evaluated to true. )r F F F G will onl& evaluate the right hand o.erand i# the le#t
hand o.erand evaluated to false. E2clusive or FEEG will alwa&s evaluate 9oth o.erands.
-he logical unar& o.erator not FJG. It o.erates onl& on a Boolean e2.ression and results in a Boolean
e2.ression. -o o.erate on a vector, use the 9uilt8in #unction not.
-he se=uence F , G o.erator that o.erates on e2.ressions 9& returning the t&.e and value o# the right8
ost e2.ression in a coa se.arated list o# e2.ressions. All e2.ressions are evaluated, in order,
#ro le#t to right.
-he ternar& selection o.erator FMKG. It o.erates on three e2.ressions Fe%1 M e%2 K e%?G. -his
o.erator evaluates the #irst e2.ression, which ust 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 9e an&
t&.e, as long their t&.es atch, or there is a conversion in section *.1.1( DI.licit "onversionsE that
can 9e a..lied to one o# the e2.ressions to ake their t&.es atch. -his resulting atching t&.e is the
t&.e o# the entire e2.ression.
-he oneHs co.leent o.erator FHG. -he o.erand ust 9e o# t&.e signed or unsigned integer or integer
vector, and the result is the oneHs co.leent o# its o.erandL each 9it o# each co.onent is
co.leented, including an& sign 9its.
-he shi#t o.erators F??G and F@@G. 4or 9oth o.erators, the o.erands ust 9e signed or unsigned
integers or integer vectors. )ne o.erand can 9e signed while the other is unsigned. In all cases, the
resulting t&.e will 9e the sae t&.e as the le#t o.erand. I# the #irst o.erand is a scalar, the second
o.erand has to 9e a scalar as well. I# the #irst o.erand is a vector, the second o.erand ust 9e a scalar
or a vector, and the result is co.uted co.onent8wise. -he result is unde#ined i# the right o.erand is
negative, or greater than or e=ual to the nu9er o# 9its in the le#t e2.ressionHs 9ase t&.e. -he value o#
E1 QQ E! is E1 Finter.reted as a 9it .atternG le#t8shi#ted 9& E! 9its. -he value o# E1 RR E! is E1 right8
shi#ted 9& E! 9it .ositions. I# E1 is a signed integer, the right8shi#t will e2tend the sign 9it. I# E1 is an
unsigned integer, the right8shi#t will ?ero8e2tend.
-he 9itwise o.erators and FGG, e2clusive8or FEG, and inclusive8or FFG. -he o.erands ust 9e o# t&.e
signed or unsigned integers or integer vectors. -he o.erands cannot 9e vectors o# di##ering si?e. I# one
o.erand is a scalar and the other a vector, the scalar is a..lied co.onent8wise to the vector, resulting
in the sae t&.e as the vector. -he #undaental t&.es o# the o.erands Fsigned or unsignedG ust
atch, and will 9e the resulting #undaental t&.e. 4or and FGG, the result is the 9itwise8and #unction
o# the o.erands. 4or e2clusive8or FEG, the result is the 9itwise e2clusive8or #unction o# the o.erands.
4or inclusive8or FFG, the result is the 9itwise inclusive8or #unction o# the o.erands.
4or a co.lete s.eci#ication o# the s&nta2 o# e2.ressions, see section ; D%hading 1anguage 0raar.E
7"
5 Operators and ()pressions
).1$ "ector and 8atri, Operations
Aith a #ew e2ce.tions, o.erations are co.onent8wise. 6suall&, when an o.erator o.erates on a vector or
atri2, it is o.erating inde.endentl& on each co.onent o# the vector or atri2, in a co.onent8wise
#ashion. 4or e2a.le,
ve%; v1 u,
float f,
v * u K f,
will 9e e=uivalent to
v+x * u+x K f,
v+& * u+& K f,
v+" * u+" K f,
And
ve%; v1 u1 /,
/ * v K u,
will 9e e=uivalent to
/+x * v+x K u+x,
/+& * v+& K u+&,
/+" * v+" K u+",
and likewise #or ost o.erators and all integer and #loating .oint vector and atri2 t&.es. -he e2ce.tions
are atri2 ulti.lied 9& vector, vector ulti.lied 9& atri2, and atri2 ulti.lied 9& atri2. -hese do
not o.erate co.onent8wise, 9ut rather .er#or the correct linear alge9raic ulti.l&.
ve%; v1 u,
mat; m,
u * v J m,
is e=uivalent to
u+x * dot#v1 mB=C), -- mB=C is t.e left %olumn of m
u+& * dot#v1 mB'C), -- dot#a1$) is t.e inner #dot) produ%t of a and $
u+" * dot#v1 mB9C),
And
u * m J v,
is e=uivalent to
u+x * mB=C+x J v+x K mB'C+x J v+& K mB9C+x J v+",
u+& * mB=C+& J v+x K mB'C+& J v+& K mB9C+& J v+",
u+" * mB=C+" J v+x K mB'C+" J v+& K mB9C+" J v+",
71
5 Operators and ()pressions
And
mat; m1 n1 r,
r * m J n,
is e=uivalent to
rB=C+x * mB=C+x J nB=C+x K mB'C+x J nB=C+& K mB9C+x J nB=C+",
rB'C+x * mB=C+x J nB'C+x K mB'C+x J nB'C+& K mB9C+x J nB'C+",
rB9C+x * mB=C+x J nB9C+x K mB'C+x J nB9C+& K mB9C+x J nB9C+",
rB=C+& * mB=C+& J nB=C+x K mB'C+& J nB=C+& K mB9C+& J nB=C+",
rB'C+& * mB=C+& J nB'C+x K mB'C+& J nB'C+& K mB9C+& J nB'C+",
rB9C+& * mB=C+& J nB9C+x K mB'C+& J nB9C+& K mB9C+& J nB9C+",
rB=C+" * mB=C+" J nB=C+x K mB'C+" J nB=C+& K mB9C+" J nB=C+",
rB'C+" * mB=C+" J nB'C+x K mB'C+" J nB'C+& K mB9C+" J nB'C+",
rB9C+" * mB=C+" J nB9C+x K mB'C+" J nB9C+& K mB9C+" J nB9C+",
and siilarl& #or other si?es o# vectors and atrices.
72
* Statements and Structure
-he #undaental 9uilding 9locks o# the ).en01 %hading 1anguage are:
stateents and declarations
#unction de#initions
selection Fif7else and switch7case7defaultG
iteration Ffor, while, and do7whileG
@u.s Fdiscard, return, break, and continueG
-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 9odies. 4unction 9odies are de#ined as
(unction-#e(inition:
(unction-rotot'e = statement-list >
statement-list:
statement
statement-list statement
statement:
comoun#-statement
simle-statement
"url& 9races are used to grou. se=uences o# stateents into co.ound stateents.
comoun#-statement:
= statement-list >
simle-statement:
#eclaration-statement
e%ression-statement
selection-statement
73
6 State*ents and Structure
iteration-statement
9um-statement
%i.le declaration, e2.ression, and @u. stateents end in a sei8colon.
-his a9ove is slightl& si.li#ied, and the co.lete graar s.eci#ied in section ; D%hading 1anguage
0raarE should 9e used as the de#initive s.eci#ication.
Declarations and e2.ressions have alread& 9een discussed.
*.1 .unction +efinitions
As indicated 9& the graar a9ove, a valid shader is a se=uence o# glo9al declarations and #unction
de#initions. A #unction is declared as the #ollowing e2a.le shows:
-- protot&pe
returnT&pe fun%tionName #t&pe= arg=1 t&pe' arg'1 +++1 t&pen argn),
and a #unction is de#ined like
-- definition
returnT&pe fun%tionName #t&pe= arg=1 t&pe' arg'1 +++1 t&pen argn)
)
-- do some %omputation
return returnValue,
3
where returnT'e ust 9e .resent and include a t&.e. Each o# the t'e+ ust include a t&.e and can
o.tionall& include a .araeter =uali#ier andOor const. -he #oral arguent naes Fargs a9oveG in the
declarations are o.tional #or 9oth the declaration and de#inition #ors.
A #unction is called 9& using its nae #ollowed 9& a list o# arguents in .arentheses.
Arra&s are allowed as arguents and as the return t&.e. In 9oth cases, the arra& ust 9e e2.licitl& si?ed.
An arra& is .assed or returned 9& using @ust its nae, without 9rackets, and the si?e o# the arra& ust
atch the si?e s.eci#ied in the #unctionHs declaration.
%tructures are also allowed as arguent t&.es. -he return t&.e can also 9e structure.
%ee section ; D%hading 1anguage 0raarE #or the de#initive re#erence on the s&nta2 to declare and
de#ine #unctions.
All #unctions ust 9e either declared with a .rotot&.e or de#ined with a 9od& 9e#ore the& are called. 4or
e2a.le:
float m&fun% #float f1 -- f is an input parameter
out float g), -- g is an output parameter
4unctions that return no value ust 9e declared as void. 4unctions that acce.t no in.ut arguents need
not use void in the arguent list 9ecause .rotot&.es For de#initionsG are re=uired and there#ore there is no
a9iguit& when an e.t& arguent list WF GW is declared. -he idio DFvoidGE as a .araeter list is
.rovided #or convenience.
74
6 State*ents and Structure
4unction naes can 9e overloaded. -he sae #unction nae can 9e used #or ulti.le #unctions, as long
as the .araeter t&.es di##er. I# a #unction nae is declared twice with the sae .araeter t&.es, then the
return t&.es and all =uali#iers ust also atch, and it is the sae #unction 9eing declared. 4or e2a.le,
ve%8 f#in ve%8 x1 out ve%8 &), -- #>)
ve%8 f#in ve%8 x1 out uve%8 &), -- #6) o:a&1 different argument t&pe
ve%8 f#in ive%8 x1 out uve%8 &), -- #E) o:a&1 different argument t&pe
int f#in ve%8 x1 out ive%8 &), -- error1 onl& return t&pe differs
ve%8 f#in ve%8 x1 in ve%8 &), -- error1 onl& Aualifier differs
ve%8 f#%onst in ve%8 x1 out ve%8 &), -- error1 onl& Aualifier differs
Ahen #unction calls are resolved, an e2act t&.e atch #or all the arguents is sought. I# an e2act atch is
#ound, all other #unctions are ignored, and the e2act atch is used. I# no e2act atch is #ound, then the
i.licit conversions in section *.1.1( DI.licit "onversionsE will 9e a..lied to #ind a atch.
:isatched t&.es on in.ut .araeters Fin or inout or de#aultB ust have a conversion #ro the calling
arguent t&.e to the #oral .araeter t&.e. :isatched t&.es on out.ut .araeters Fout or inoutG ust
have a conversion #ro the #oral .araeter t&.e to the calling arguent t&.e.
I# i.licit conversions can 9e used to #ind ore than one atching #unction, a single 9est8atching
#unction is sought. -o deterine a 9est atch, the conversions 9etween calling arguent and #oral
.araeter t&.es are co.ared #or each #unction arguent and .air o# atching #unctions. A#ter these
co.arisons are .er#ored, each .air o# atching #unctions are co.ared. A #unction declaration @ is
considered a 9etter atch than #unction declaration : i#
#or at least one #unction arguent, the conversion #or that arguent in @ is 9etter than the
corres.onding conversion in :L and
there is no #unction arguent #or which the conversion in : is 9etter than the corres.onding
conversion in @.
I# a single #unction declaration is considered a 9etter atch than ever& other atching #unction
declaration, it will 9e used. )therwise, a seantic error #or an a9iguous overloaded #unction call occurs
and the shader will #ail to co.ile.
-o deterine whether the conversion #or a single arguent in one atch is 9etter than that #or another
atch, the #ollowing rules are a..lied, in order:
1. An e2act atch is 9etter than a atch involving an& i.licit conversion.
!. A atch involving an i.licit conversion #ro float to double is 9etter than a atch involving
an& other i.licit conversion.
'. A atch involving an i.licit conversion #ro either int or uint to float is 9etter than a atch
involving an i.licit conversion #ro either int or uint to double.
I# none o# the rules a9ove a..l& to a .articular .air o# conversions, neither conversion is considered 9etter
than the other.
4or the e2a.le #unction .rotot&.es FAG, FBG, and F"G a9ove, the #ollowing e2a.les show how the rules
a..l& to di##erent sets o# calling arguent t&.es:
75
6 State*ents and Structure
f#ve%81 ve%8), -- exa%t mat%. of ve%8 f#in ve%8 x1 out ve%8 &)
f#ve%81 uve%8), -- exa%t mat%. of ve%8 f#in ve%8 x1 out ive%8 &)
f#ve%81 ive%8), -- mat%.ed to ve%8 f#in ve%8 x1 out ve%8 &)
-- #E) not relevant1 %anDt %onvert ve%8 to
-- ive%8+ #>) $etter t.an #6) for 9nd
-- argument #rule 9)1 same on first argument+
f#ive%81 ve%8), -- NOT mat%.ed+ >ll t.ree mat%. $& impli%it
-- %onversion+ #E) is $etter t.an #>) and #6)
-- on t.e first argument+ #>) is $etter t.an
-- #6) and #E)+
6ser8de#ined #unctions can have ulti.le declarations, 9ut onl& one de#inition. A shader can rede#ine
9uilt8in #unctions. I# a 9uilt8in #unction is redeclared in a shader Fi.e., a .rotot&.e is visi9leG 9e#ore a call
to it, then the linker will onl& atte.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 e2ecuta9le. A shader need not contain a #unction
naed main, 9ut one shader in a set o# shaders linked together to #or a single shader e2ecuta9le ust.
-his #unction takes no arguents, returns no value, and ust 9e declared as t&.e voidK
void main#)
)
+++
3
-he #unction main can contain uses o# return. %ee section /.* DJu.sE #or ore details.
It is an error to declare or de#ine a #unction main with an& other .araeters or return t&.e.
*.1.1 .unction Calling Conventions
4unctions are called 9& value8return. -his eans in.ut arguents are co.ied into the #unction at call tie,
and out.ut arguents are co.ied 9ack to the caller 9e#ore #unction e2it. Because the #unction works with
local co.ies o# .araeters, there are no issues regarding aliasing o# varia9les within a #unction. -o
control what .araeters are co.ied in andOor out through a #unction de#inition or declaration:
-he ke&word in is used as a =uali#ier to denote a .araeter is to 9e co.ied in, 9ut not co.ied out.
-he ke&word out is used as a =uali#ier to denote a .araeter is to 9e co.ied out, 9ut not co.ied in.
-his should 9e used whenever .ossi9le to avoid unnecessaril& co.&ing .araeters in.
-he ke&word inout is used as a =uali#ier to denote the .araeter is to 9e 9oth co.ied in and co.ied
out.
A #unction .araeter declared with no such =uali#ier eans the sae thing as s.eci#&ing in.
All arguents are evaluated at call tie, e2actl& once, in order, #ro le#t to right. Evaluation o# an in
.araeter results in a value that is co.ied to the #oral .araeter. Evaluation o# an out .araeter results
in an l8value that is used to co.& out a value when the #unction returns. Evaluation o# an inout .araeter
results in 9oth a value and an l8valueL the value is co.ied to the #oral .araeter at call tie and the l8
value is used to co.& out a value when the #unction returns.
-he order in which out.ut .araeters are co.ied 9ack to the caller is unde#ined.
76
6 State*ents and Structure
I# the #unction atching descri9ed in the .revious section re=uired arguent t&.e conversions, these
conversions are a..lied at co.&8in and co.&8out ties.
In a #unction, writing to an in.ut8onl& .araeter is allowed. )nl& the #unctionKs co.& is odi#ied. -his
can 9e .revented 9& declaring a .araeter with the const =uali#ier.
Ahen calling a #unction, e2.ressions that do not evaluate to l8values cannot 9e .assed to .araeters
declared as out or inout.
7o =uali#ier is allowed on the return t&.e o# a #unction.
(unction-rotot'e :
recision-1uali(ier t'e (unction-name3const-1uali(ier arameter-1uali(ier recision-1uali(ier
t'e name arra'-seci(ier$ ... 4
t'e :
an& 9asic t&.e, arra& t&.e, structure nae, or structure #e(inition
const-1uali(ier :
e.t&
const
arameter-1uali(ier :
e.t&
in
out
inout
name :
e.t&
identi#ier
arra'-seci(ier :
e.t&
' integral-constant-e%ression (
+owever, the const =uali#ier cannot 9e used with out or inout. -he a9ove is used #or #unction
declarations Fi.e., .rotot&.esG and #or #unction de#initions. +ence, #unction de#initions can have unnaed
arguents.
Recursion is not allowed, not even staticall&. %tatic recursion is .resent i# the static #unction8call gra.h o#
the .rogra contains c&cles. -his includes all .otential #unction calls through varia9les declared as
subroutine uniform Fdescri9ed 9elowG.
77
6 State*ents and Structure
*.1.2 Subroutines
%u9routines .rovide a echanis allowing shaders to 9e co.iled in a anner where the target o# one or
ore #unction calls can 9e changed at run8tie without re=uiring an& shader reco.ilation. 4or e2a.le,
a single shader a& 9e co.iled with su..ort #or ulti.le illuination algoriths to handle di##erent
kinds o# lights or sur#ace aterials. An a..lication using such a shader a& switch illuination
algoriths 9& changing the value o# its su9routine uni#ors. -o use su9routines, a su9routine t&.e is
declared, one or ore #unctions are associated with that su9routine t&.e, and a su9routine varia9le o# that
t&.e is declared. -he #unction currentl& assigned to the varia9le #unction is then called 9& using #unction
calling s&nta2 re.lacing a #unction nae with the nae o# the su9routine varia9le. %u9routine varia9les
are uni#ors, and are assigned to s.eci#ic #unctions onl& through coands F)niformSubroutinesuivG in
the ).en01 A3I.
%u9routine t&.es are declared using a stateent siilar to a #unction declaration, with the subroutine
ke&word, as #ollows:
su$routine returnT&pe su$routineT&peName#t&pe= arg=1 t&pe' arg'1
+++1 t&pen argn),
As with #unction declarations, the #oral arguent naes Fargs a9oveG are o.tional. 4unctions are
associated with su9routine t&.es o# atching declarations 9& de#ining the #unction with the subroutine
ke&word and a list o# su9routine t&.es the #unction atches:
su$routine#su$routineT&peName=1 +++1 su$routineT&peNameN)
returnT&pe fun%tionName#t&pe= arg=1 t&pe' arg'1 +++1 t&pen argn)
) +++ 3 -- fun%tion $od&
It is an error i# arguents and return t&.e donHt atch 9etween the #unction and each associated su9routine
t&.e.
4unctions declared with subroutine ust include a 9od&. An overloaded #unction cannot 9e declared
with subroutineL a .rogra will #ail to co.ile or link i# an& shader or stage contains two or ore
#unctions with the sae nae i# the nae is associated with a su9routine t&.e.
%u9routine t&.e varia9les are re=uired to 9e su&routine uni(orms, and are declared with a s.eci#ic
su9routine t&.e in a su9routine uni#or varia9le declaration:
su$routine uniform su$routineT&peName su$routineVarName,
%u9routine uni#or varia9les are called the sae wa& #unctions are called. Ahen a su9routine varia9le
For an eleent o# a su9routine varia9le arra&G is associated with a .articular #unction, all #unction calls
through that varia9le will call that .articular #unction.
6nlike other uni#or varia9les, su9routine uni#or varia9les are sco.ed to the shader e2ecution stage the
varia9le is declared in.
%u9routine varia9les a& 9e declared as e2.licitl&8si?ed arra&s, which can 9e d&naicall& inde2ed at use.
7
6 State*ents and Structure
*.2 Selection
"onditional control #low in the shading language is done 9& either if, if8else, or switch stateents:
selection-statement :
if F &ool-e%ression G statement
if F &ool-e%ression G statement else statement
switch F init-e%ression G \ s/itch-statement-listot

]
Ahere s/itch-statement-list is a list o# ?ero or ore s/itch-statement and other stateents de#ined 9& the
language, where s/itch-statement adds soe #ors o# la9els. -hat is
s/itch-statement-list :
s/itch-statement
s/itch-statement-list s/itch-statement
s/itch-statement :
case constant-e%ression K
default K
statement
I# an if7e2.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 9e used as the conditional e2.ression &ool-
e%ression. Vector t&.es are not acce.ted as the e2.ression to if.
"onditionals can 9e nested.
-he t&.e o# init-e%ression in a switch stateent ust 9e a scalar integer. I# a case la9el has a constant-
e%ression o# e=ual value, then e2ecution will continue a#ter that la9el. )therwise, i# there is a default
la9el, e2ecution will continue a#ter that la9el. )therwise, e2ecution ski.s the rest o# the switch stateent.
It is an error to have ore than one default or a re.licated constant-e%ression. A break stateent not
nested in a loo. or other switch stateent Feither not nested or nested onl& in if or if8else stateentsG will
also ski. the rest o# the switch stateent. 4all through la9els are allowed, 9ut it is an error to have no
stateent 9etween a la9el and the end o# the switch stateent. 7o stateents are allowed in a switch
stateent 9e#ore the #irst case stateent.
7o case or default la9els can 9e nested inside other #low control nested within their corres.onding
switch.
7!
6 State*ents and Structure
*.# Iteration
4or, while, and do loo.s are allowed as #ollows:
for #init2expression, %ondition2expression, loop2expression)
su$2statement
/.ile #%ondition2expression)
su$2statement
do
statement
/.ile #%ondition2expression)
%ee section ; D%hading 1anguage 0raarE #or the de#initive s.eci#ication o# loo.s.
-he for loo. #irst evaluates the init-e%ression, then the con#ition-e%ression. I# the con#ition-
e%ression evaluates to true, then the 9od& o# the loo. is e2ecuted. A#ter the 9od& is e2ecuted, a for loo.
will then evaluate the loo-e%ression, and then loo. 9ack to evaluate the con#ition-e%ression, re.eating
until the con#ition-e%ression evaluates to #alse. -he loo. is then e2ited, ski..ing its 9od& and ski..ing
its loo-e%ression. Varia9les odi#ied 9& the loo-e%ression aintain their value a#ter the loo. is
e2ited, .rovided the& are still in sco.e. Varia9les declared in init-e%ression or con#ition-e%ression are
onl& in sco.e until the end o# the su98stateent o# the for loo..
-he while loo. #irst evaluates the con#ition-e%ression. I# true, then the 9od& is e2ecuted. -his is then
re.eated, until the con#ition-e%ression evaluates to #alse, e2iting the loo. and ski..ing its 9od&.
Varia9les declared in the con#ition-e%ression are onl& in sco.e until the end o# the su98stateent o# the
while loo..
-he do7while loo. #irst e2ecutes the 9od&, then e2ecutes the con#ition-e%ression. -his is re.eated until
con#ition-e%ression evaluates to #alse, and then the loo. is e2ited.
E2.ressions #or con#ition-e%ression ust evaluate to a Boolean.
Both the con#ition-e%ression and the init-e%ression can declare and initiali?e a varia9le, e2ce.t in the
do7while loo., which cannot declare a varia9le in its con#ition-e%ression. -he varia9leKs sco.e lasts
onl& until the end o# the su98stateent that #ors the 9od& o# the loo..
1oo.s can 9e nested.
7on8terinating loo.s are allowed. -he conse=uences o# ver& long or non8terinating loo.s are .lat#or
de.endent.
"
6 State*ents and Structure
*.& ;umps
-hese are the @u.s:
9um)statement:
continueL
breakL
returnL
return e%ressionL
discardL << in the #ragent shader language onl&
-here is no DgotoE nor other non8structured #low o# control.
-he continue @u. is used onl& in loo.s. It ski.s the reainder o# the 9od& o# the inner ost loo. o#
which it is inside. 4or while and do7while loo.s, this @u. is to the ne2t evaluation o# the loo.
con#ition-e%ression #ro which the loo. continues as .reviousl& de#ined. 4or for loo.s, the @u. is to
the loo-e%ression, #ollowed 9& the con#ition-e%ression.
-he break @u. can also 9e used onl& in loo.s and switch stateents. It is si.l& an iediate e2it o#
the inner8ost loo. or switch stateents containing the break. 7o #urther e2ecution o# con#ition-
e%ression$ loo-e%ression, or s/itch-statement is done.
-he discard ke&word is onl& allowed within #ragent shaders. It can 9e used within a #ragent shader to
a9andon the o.eration on the current #ragent. -his ke&word causes the #ragent to 9e discarded and no
u.dates to an& 9u##ers will occur. "ontrol #low e2its the shader, and su9se=uent i.licit or e2.licit
derivatives are unde#ined when this e2it is non8uni#or. It would t&.icall& 9e used within a conditional
stateent, #or e2a.le:
if #intensit& 0 =+=)
dis%ard,
A #ragent shader a& test a #ragentKs al.ha value and discard the #ragent 9ased on that test.
+owever, it should 9e noted that coverage testing occurs a#ter the #ragent shader runs, and the coverage
test can change the al.ha value.
-he return @u. causes iediate e2it o# the current #unction. I# it has e%ression then that is the return
value #or the #unction.
-he #unction main can use return. -his si.l& causes main to e2it in the sae wa& as when the end o#
the #unction had 9een reached. It does not i.l& a use o# discard in a #ragent shader. 6sing return in
ain 9e#ore de#ining out.uts will have the sae 9ehavior as reaching the end o# ain 9e#ore de#ining
out.uts.
1
4 /uilt7in "ariables
4.1 /uilt7In Language "ariables
%oe ).en01 o.erations occur in #i2ed #unctionalit& and need to .rovide values to or receive values
#ro shader e2ecuta9les. %haders counicate with #i2ed8#unction ).en01 .i.eline stages, and
o.tionall& with other shader e2ecuta9les, through the use o# 9uilt8in in.ut and out.ut varia9les.
In the verte2 language, the 9uilt8ins are intrinsicall& declared as:
in int gl_VertexI,
in int gl_Instan%eI,
out gl_@erVertex )
ve%8 gl_@osition,
float gl_@ointSi"e,
float gl_Elipistan%eBC,
3,
In the geoetr& language, the 9uilt8in varia9les are intrinsicall& declared as:
in gl_@erVertex )
ve%8 gl_@osition,
float gl_@ointSi"e,
float gl_Elipistan%eBC,
3 gl_inBC,
in int gl_@rimitiveIIn,
in int gl_Invo%ationI,
out gl_@erVertex )
ve%8 gl_@osition,
float gl_@ointSi"e,
float gl_Elipistan%eBC,
3,
out int gl_@rimitiveI,
out int gl_La&er,
2
7 Bui%t+in #aria$%es
In the tessellation control language, 9uilt8in varia9les are intrinsicall& declared as:
in gl_@erVertex )
ve%8 gl_@osition,
float gl_@ointSi"e,
float gl_Elipistan%eBC,
3 gl_inBgl_?ax@at%.Verti%esC,
in int gl_@at%.Verti%esIn,
in int gl_@rimitiveI,
in int gl_Invo%ationI,
out gl_@erVertex )
ve%8 gl_@osition,
float gl_@ointSi"e,
float gl_Elipistan%eBC,
3 gl_outBC,
pat%. out float gl_TessLevelOuterB8C,
pat%. out float gl_TessLevelInnerB9C,
In the tessellation evaluation language, 9uilt8in varia9les are intrinsicall& declared as:
in gl_@erVertex )
ve%8 gl_@osition,
float gl_@ointSi"e,
float gl_Elipistan%eBC,
3 gl_inBgl_?ax@at%.Verti%esC,
in int gl_@at%.Verti%esIn,
in int gl_@rimitiveI,
in ve%; gl_TessEoord,
pat%. in float gl_TessLevelOuterB8C,
pat%. in float gl_TessLevelInnerB9C,
out gl_@erVertex )
ve%8 gl_@osition,
float gl_@ointSi"e,
float gl_Elipistan%eBC,
3,
3
7 Bui%t+in #aria$%es
In the #ragent language, 9uilt8in varia9les are intrinsicall& declared as:
in ve%8 gl_FragEoord,
in $ool gl_FrontFa%ing,
in float gl_Elipistan%eBC,
in ve%9 gl_@ointEoord,
in int gl_@rimitiveI,
in int gl_SampleI,
in ve%9 gl_Sample@osition,
out ve%8 gl_FragEolor, -- depre%ated
out ve%8 gl_FragataBgl_?axra/6uffersC, -- depre%ated
out float gl_Fragept.,
out int gl_Sample?as:BC,
Each o# the a9ove varia9les is discussed 9elow.
-he varia9le gl)Verte%*D is a verte2 language in.ut varia9le that holds an integer inde2 #or the verte2, as
de#ined under D%hader In.utsE in section !.11.5 DVar&ing Varia9lesE in the ).en01 0ra.hics %&ste
%.eci#ication. Ahile the varia9le gl)Verte%*D is alwa&s .resent, its value is not alwa&s de#ined.
-he varia9le gl)*nstance*D is a verte2 language in.ut varia9le that holds the integer inde2 o# the current
.riitive in an instanced draw call Fsee D%hader In.utsE in section !.11.5 DVar&ing Varia9lesE in the
).en01 0ra.hics %&ste %.eci#icationG. I# the current .riitive does not coe #ro an instanced draw
call, the value o# gl)*nstance*D is ?ero.
As an out.ut varia9le, gl);osition is intended #or writing the hoogeneous verte2 .osition. It can 9e
written at an& tie during shader e2ecution. -his value will 9e used 9& .riitive asse9l&, cli..ing,
culling, and other #i2ed #unctionalit& o.erations, i# .resent, that o.erate on .riitives a#ter verte2
.rocessing has occurred. Its value is unde#ined a#ter the verte2 .rocessing stage i# the verte2 shader
e2ecuta9le does not write gl);osition, and it is unde#ined a#ter geoetr& .rocessing i# the geoetr&
e2ecuta9le calls 2mit5erteFG without having written gl);osition since the last 2mit5erteFG For hasnHt
written it at allG. As an in.ut varia9le, gl);osition reads the out.ut written in the .revious shader stage to
gl);osition.
As an out.ut varia9le, gl);oint"i0e is intended #or a shader to write the si?e o# the .oint to 9e rasteri?ed.
It is easured in .i2els. I# gl);oint"i0e is not written to, its value is unde#ined in su9se=uent .i.e stages.
As an in.ut varia9le, gl);oint"i0e reads the out.ut written in the .revious shader stage to gl);osition.
-he varia9le gl)8liDistance .rovides the #orward co.ati9le echanis #or controlling user cli..ing.
-he eleent gl)8liDistance5i6 s.eci#ies a cli. distance #or each .lane i. A distance o# ( eans the
verte2 is on the .lane, a .ositive distance eans the verte2 is inside the cli. .lane, and a negative distance
eans the .oint is outside the cli. .lane. -he cli. distances will 9e linearl& inter.olated across the
.riitive and the .ortion o# the .riitive with inter.olated distances less than ( will 9e cli..ed.
4
7 Bui%t+in #aria$%es
-he gl)8liDistance arra& is .redeclared as unsi?ed and ust 9e si?ed 9& 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 ena9led via the ).en01 A3IL i# the si?e does not include all ena9led .lanes,
results are unde#ined. -he si?e can 9e at ost gl)7a%8liDistances. -he nu9er o# var&ing co.onents
Fsee gl)7a%Var'ing8omonents4 consued 9& gl)8liDistance will atch the si?e o# the arra&, no
atter how an& .lanes are ena9led. -he shader ust also set all values in gl)8liDistance that have
9een ena9led via the ).en01 A3I, or results are unde#ined. Values written into gl)8liDistance #or
.lanes that are not ena9led have no e##ect.
As an out.ut varia9le, gl)8liDistance .rovides the .lace #or the shader to write these distances. As an
in.ut in all 9ut the #ragent language, it reads the values written in the .revious shader stage. In the
#ragent language, gl)8liDistance arra& contains linearl& inter.olated values #or the verte2 values
written 9& a shader to the gl)8liDistance verte2 out.ut varia9le. )nl& eleents in this arra& that have
cli..ing ena9led will have de#ined values.
-he out.ut varia9le gl);rimitive*D is availa9le onl& in the geoetr& language and .rovides a single
integer that serves as a .riitive identi#ier. -his is then availa9le to #ragent shaders as the #ragent
in.ut gl);rimitive*D, which will select the written .riitive ID #ro the .rovoking verte2 in the .riitive
9eing shaded. I# a #ragent shader using gl);rimitive*D is active and a geoetr& shader is also active,
the geoetr& shader ust write to gl);rimitive*D or the #ragent shader in.ut gl);rimitive*D is
unde#ined. %ee section !.1'.* D0eoetr& %hader E2ecution EnvironentE Funder D0eoetr& %hader
)ut.utsEG and section '.;.! D%hader E2ecutionE Funder D%hader In.utsEG o# the ).en01 0ra.hics %&ste
%.eci#ication #or ore in#oration.
4or tessellation control and evaluation languages the in.ut varia9le gl);rimitive*D is #illed with the
nu9er o# .riitives .rocessed 9& the shader since the current set o# rendering .riitives was started.
4or the #ragent language, it is #illed with the value written to the gl);rimitive*D geoetr& shader out.ut
i# a geoetr& shader is .resent. )therwise, it is assigned in the sae anner as with tessellation control
and evaluation shaders.
-he geoetr& language in.ut varia9le gl);rimitive*D*n 9ehaves identicall& to the tessellation control and
evaluation language in.ut varia9le gl);rimitive*D.
-he in.ut varia9le gl)*nvocation*D is availa9le onl& in the tessellation control and geoetr& languages.
In the tessellation control shader, it identi#ies the nu9er o# the out.ut .atch verte2 assigned to the
tessellation control shader invocation. In the geoetr& shader, it identi#ies the invocation nu9er
assigned to the geoetr& shader invocation. In 9oth cases, gl)*nvocation*D is assigned integer values in
the range 50$ +-16, where + is the nu9er o# out.ut .atch vertices or geoetr& shader invocations .er
.riitive.
5
7 Bui%t+in #aria$%es
-he out.ut varia9le gl)La'er is availa9le onl& in the geoetr& language, and is used to select a s.eci#ic
la&er For #ace and la&er o# a cu9e a.G o# a ulti8la&er #rae9u##er attachent. -he actual la&er used will
coe #ro one o# the vertices in the .riitive 9eing shaded. Ahich verte2 the la&er coes #ro is
unde#ined, so it is 9est to write the sae la&er value #or all vertices o# a .riitive. I# a shader staticall&
assigns a value to gl)La'er, la&ered rendering ode is ena9led. %ee section !.1'.* D0eoetr& %hader
E2ecution EnvironentE Funder D0eoetr& %hader )ut.utsEG and section *.*.5 D1a&ered 4rae9u##ersE
o# the ).en01 0ra.hics %&ste %.eci#ication #or ore in#oration. I# a shader staticall& assigns a value
to gl)La'er, and there is an e2ecution .ath through the shader that does not set gl)La'er, then the value o#
gl)La'er is unde#ined #or e2ecutions o# the shader that take that .ath.
-he out.ut varia9le gl)La'er takes on a s.ecial value when used with an arra& o# cu9e a. te2tures.
Instead o# onl& re#erring to the la&er, it is used to select a cu9e a. #ace and a la&er. %etting gl)La'er to
the value la'erABC(ace will render to #ace (ace o# the cu9e de#ined in la&er la'er. -he #ace values are
de#ined in -a9le *.1! o# section *.*.5 D1a&ered 4rae9u##ersE o# the ).en01 0ra.hics %&ste
%.eci#ication, 9ut re.eated 9elow #or clarit&.
Race 5alue 3esulting Oarget
(
TEOTPRE_EP6E_?>@_@OSITIVE_O
1
TEOTPRE_EP6E_?>@_NE!>TIVE_O
!
TEOTPRE_EP6E_?>@_@OSITIVE_Q
'
TEOTPRE_EP6E_?>@_NE!>TIVE_Q
*
TEOTPRE_EP6E_?>@_@OSITIVE_R
,
TEOTPRE_EP6E_?>@_NE!>TIVE_R
4or e2a.le, to render to the .ositive ' cu9e a. #ace located in the ,th la&er o# the cu9e a. arra&,
gl)La'er should 9e set to DABC2.
-he varia9le gl);atchVertices*n is availa9le onl& in the tessellation control and evaluation languages. It
is an integer s.eci#&ing the nu9er o# vertices in the in.ut .atch 9eing .rocessed 9& the shader. A single
tessellation control or evaluation shader can read .atches o# di##ering si?es, so the value o#
gl);atchVertices*n a& di##er 9etween .atches.
-he out.ut varia9les gl)TessLevelOuter56 and gl)TessLevel*nner56 are availa9le onl& in the tessellation
control language. -he values written to these varia9les are assigned to the corres.onding outer and inner
tessellation levels o# the out.ut .atch. -he& are used 9& the tessellation .riitive generator to control
.riitive tessellation and a& 9e read 9& tessellation evaluation shaders.
-he varia9le gl)Tess8oor# is availa9le onl& in the tessellation evaluation language. It s.eci#ies a three8
co.onent 3u$v$/4 vector identi#&ing the .osition o# the verte2 9eing .rocessed 9& the shader relative to
the .riitive 9eing tessellated.
-he in.ut varia9les gl)TessLevelOuter56 and gl)TessLevel*nner56 are availa9le onl& in the tessellation
evaluation shader. I# a tessellation control shader is active, these varia9les are #illed with corres.onding
out.uts written 9& the tessellation control shader. )therwise, the& are assigned with de#ault tessellation
levels s.eci#ied in section !.1! D-essellationE in the ).en01 0ra.hics %&ste %.eci#ication.
6
7 Bui%t+in #aria$%es
4ragent shaders out.ut values to the ).en01 .i.eline using the 9uilt8in varia9les gl)-rag8olor$
gl)-ragData$ and gl)-ragDeth, unless the discard stateent is e2ecuted. Both gl)-rag8olor and
gl)-ragData are de.recatedL the .re#erred usage is to e2.licitl& declare these out.uts in the #ragent
shader using the out storage =uali#ier.
-he #i2ed #unctionalit& co.uted de.th #or a #ragent a& 9e o9tained 9& reading gl)-rag8oor#.0$
descri9ed 9elow.
De.recated: Ariting to gl)-rag8olor s.eci#ies the #ragent color that will 9e used 9& the su9se=uent
#i2ed #unctionalit& .i.eline. I# su9se=uent #i2ed #unctionalit& consues #ragent color and an e2ecution
o# the #ragent shader e2ecuta9le does not write a value to gl)-rag8olor then the #ragent color
consued is unde#ined.
Ariting to gl)-ragDeth will esta9lish the de.th value #or the #ragent 9eing .rocessed. I# de.th
9u##ering is ena9led, and no shader writes gl)-ragDeth, then the #i2ed #unction value #or de.th will 9e
used as the #ragentKs de.th value. I# a shader staticall& assigns a value to gl)-ragDeth, and there is an
e2ecution .ath through the shader that does not set gl)-ragDeth, then the value o# the #ragentKs de.th
a& 9e unde#ined #or e2ecutions o# the shader that take that .ath. -hat is, i# the set o# linked #ragent
shaders staticall& contain a write to gl)-ragDeth, then it is res.onsi9le #or alwa&s writing it.
De.recated: -he varia9le gl)-ragData is an arra&. Ariting to gl)-ragData5n6 s.eci#ies the #ragent
data that will 9e used 9& the su9se=uent #i2ed #unctionalit& .i.eline #or data n. I# su9se=uent #i2ed
#unctionalit& consues #ragent data and an e2ecution o# a #ragent shader e2ecuta9le does not write a
value to it, then the #ragent data consued is unde#ined.
I# a shader staticall& assigns a value to gl)-rag8olor, it a& not assign a value to an& eleent o#
gl)-ragData. I# a shader staticall& writes a value to an& eleent o# gl)-ragData, it a& not assign a
value to gl)-rag8olor. -hat is, a shader a& assign values to either gl)-rag8olor or gl)-ragData, 9ut
not 9oth. :ulti.le shaders linked together ust also consistentl& write @ust one o# these varia9les.
%iilarl&, i# user declared out.ut varia9les are in use Fstaticall& assigned toG, then the 9uilt8in varia9les
gl)-rag8olor and gl)-ragData a& not 9e assigned to. -hese incorrect usages all generate co.ile tie
errors.
I# a shader e2ecutes the discard ke&word, the #ragent is discarded, and the values o# an& user8de#ined
#ragent out.uts, gl)-ragDeth, gl)-rag8olor, and gl)-ragData 9ecoe irrelevant.
-he varia9le gl)-rag8oor# is availa9le as an in.ut varia9le #ro within #ragent shaders and it holds the
window relative coordinates F%, ', 0, 1E/G values #or the #ragent. I# ulti8sa.ling, this value can 9e #or
an& location within the .i2el, or one o# the #ragent sa.les. -he use o# centroid in does not #urther
restrict this value to 9e inside the current .riitive. -his value is the result o# the #i2ed #unctionalit& that
inter.olates .riitives a#ter verte2 .rocessing to generate #ragents. -he 0 co.onent is the de.th value
that would 9e used #or the #ragentKs de.th i# no shader contained an& writes to gl)-ragDeth. -his is
use#ul #or invariance i# a shader conditionall& co.utes gl)-ragDeth 9ut otherwise wants the #i2ed
#unctionalit& #ragent de.th.
4ragent shaders have access to the in.ut 9uilt8in varia9le gl)-ront-acing$ whose value is true i# the
#ragent 9elongs to a #ront8#acing .riitive. )ne use o# this is to eulate two8sided lighting 9& selecting
one o# two colors calculated 9& a verte2 or geoetr& shader.
7
7 Bui%t+in #aria$%es
-he values in gl);oint8oor# are two8diensional coordinates indicating where within a .oint .riitive
the current #ragent is located, when .oint s.rites are ena9led. -he& range #ro (.( to 1.( across the
.oint. I# the current .riitive is not a .oint, or i# .oint s.rites are not ena9led, then the values read #ro
gl);oint8oor# are unde#ined.
-he out.ut arra& gl)"amle7as.56 sets the sa.le ask #or the #ragent 9eing .rocessed. "overage #or
the current #ragent will 9ecoe the logical A7D o# the coverage ask and the out.ut gl)"amle7as..
Bit : o# ask gl)"amle7as.576 corres.onds to sa.le ?2A7C:. -his arra& ust 9e si?ed in the
#ragent shader either i.licitl& or e2.licitl& to 9e the sae si?e as the i.leentation8de.endent
a2iu sa.le8ask Fas an arra& o# '!9it eleentsG, deterined 9& the a2iu nu9er o# sa.les.
I# the #ragent shader staticall& assigns a value to gl)"amle7as., the sa.le ask will 9e unde#ined #or
an& arra& eleents o# an& #ragent shader invocations that #ail to assign a value. I# a shader does not
staticall& assign a value to gl)"amle7as., the sa.le ask has no e##ect on the .rocessing o# a
#ragent.
-he in.ut varia9le gl)"amle*D is #illed with the sa.le nu9er o# the sa.le currentl& 9eing .rocessed.
-his varia9le is in the range 0 to gl)+um"amles-1, where gl)+um"amles is the total nu9er o# sa.les
in the #rae9u##er, or 1 i# rendering to a non8ultisa.le #rae9u##er. An& static use o# this varia9le in a
#ragent shader causes the entire shader to 9e evaluated .er8sa.le.
-he in.ut varia9le gl)"amle;osition contains the .osition o# the current sa.le within the ulti8sa.le
draw 9u##er. -he % and ' co.onents o# gl)"amle;osition contain the su98.i2el coordinate o# the current
sa.le and will have values in the range (.( to 1.(. An& static use o# this varia9le in a #ragent shader
causes the entire shader to 9e evaluated .er sa.le.
4.1.1 Compatibilit! -rofile /uilt7In Language "ariables
Ahen using the co.ati9ilit& .ro#ile, the 01 can .rovide #i2ed #unctionalit& 9ehavior #or the verte2 and
#ragent .rograa9le .i.eline stages. 4or e2a.le, i2ing a #i2ed #unctionalit& verte2 stage with a
.rograa9le #ragent stage.
-he #ollowing 9uilt8in verte2, tessellation control, tessellation evaluation, and geoetr& out.ut varia9les
are availa9le to s.eci#& in.uts #or the su9se=uent .rograa9le shader stage or the #i2ed #unctionalit&
#ragent stage. A .articular one should 9e written to i# an& #unctionalit& in a corres.onding #ragent
shader or #i2ed .i.eline uses it or state derived #ro it. )therwise, 9ehavior is unde#ined. -he #ollowing
e9ers are added to the out.ut gl);erVerte% 9lock in these languages:
out gl_@erVertex ) -- part of t.e gl_@erVertex $lo%: des%ri$ed in F+'
-- in addition to ot.er gl_@erVertex mem$ers+++
ve%8 gl_ElipVertex,
ve%8 gl_FrontEolor,
ve%8 gl_6a%:Eolor,
ve%8 gl_FrontSe%ondar&Eolor,
ve%8 gl_6a%:Se%ondar&Eolor,
ve%8 gl_TexEoordBC,
float gl_FogFragEoord,
3,
-he out.ut varia9le gl)8liVerte% .rovides a .lace #or verte2 and geoetr& shaders to write the
coordinate to 9e used with the user cli..ing .lanes.

7 Bui%t+in #aria$%es
-he user ust ensure the cli. verte2 and user cli..ing .lanes are de#ined in the sae coordinate s.ace.
6ser cli. .lanes work .ro.erl& onl& under linear trans#or. It is unde#ined what ha..ens under non8
linear trans#or.
I# a linked set o# shaders #oring a .rogra contains no static write to gl)8liVerte% or gl)8liDistance$
9ut 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 co.arison against the user cli. .lanes. Ariting to gl)8liDistance is
the .re#erred ethod #or user cli..ing. It is an error #or the set o# shaders #oring a .rogra to staticall&
read or write 9oth gl)8liVerte% and gl)8liDistance.
-he out.ut varia9les gl)-ront8olor, gl-ront"econ#ar'8olor, gl):ac.8olor, and gl:ac."econ#ar'8olor
assign .riar& and secondar& colors #or #ront and 9ack #aces o# .riitives containing the verte2 9eing
.rocessed. -he out.ut varia9le gl)Te%8oor# assigns te2ture coordinates #or the verte2 9eing .rocessed.
4or gl)-og-rag8oor#, the value written will 9e used as the DcE value in section '.11 D4ogE o# the
co.ati9ilit& .ro#ile o# the ).en01 0ra.hics %&ste %.eci#ication, 9& the #i2ed #unctionalit& .i.eline.
4or e2a.le, i# the ?8coordinate o# the #ragent in e&e s.ace is desired as DcE, then thatHs what the verte2
shader e2ecuta9le should write into gl)-og-rag8oor#.
As with all arra&s, indices used to su9scri.t gl)Te%8oor# ust either 9e an integral constant e2.ressions,
or this arra& ust 9e re8declared 9& the shader with a si?e. -he si?e can 9e at ost
gl)7a%Te%ture8oor#s. 6sing inde2es close to ( a& aid the i.leentation in .reserving var&ing
resources. -he redeclaration o# gl)Te%8oor# is done at glo9al sco.e as, #or e2a.le,
in ve%8 gl_TexEoordB;C,
out ve%8 gl_TexEoordB8C,
which is the sae wa& it was done in .revious releases. -his treatent is a s.ecial case #or
gl)Te%8oor#56, not a general ethod #or redeclaring e9ers o# 9locks.
In the tessellation control, evaluation, and geoetr& shaders, the out.uts o# the .revious stage descri9ed
a9ove are also availa9le in the in.ut gl);erVerte% 9lock in these languages.
in gl_@erVertex ) -- part of t.e gl_@erVertex $lo%: des%ri$ed in F+'
-- in addition to ot.er gl_@erVertex mem$ers+++
ve%8 gl_ElipVertex,
ve%8 gl_FrontEolor,
ve%8 gl_6a%:Eolor,
ve%8 gl_FrontSe%ondar&Eolor,
ve%8 gl_6a%:Se%ondar&Eolor,
ve%8 gl_TexEoordBC,
float gl_FogFragEoord,
3 gl_inBC,
-he #ollowing #ragent in.uts are also availa9le in a #ragent shader when using the co.ati9ilit&
.ro#ile:
!
7 Bui%t+in #aria$%es
in float gl_FogFragEoord,
in ve%8 gl_TexEoordBC,
in ve%8 gl_Eolor,
in ve%8 gl_Se%ondar&Eolor,
-he values in gl)8olor and gl)"econ#ar'8olor will 9e derived autoaticall& 9& the s&ste #ro
gl)-ront8olor$ gl):ac.8olor$ gl)-ront"econ#ar'8olor$ and gl):ac."econ#ar'8olor 9ased on which
#ace is visi9le in the .riitive .roducing the #ragent. I# #i2ed #unctionalit& is used #or verte2 .rocessing,
then gl)-og-rag8oor# will either 9e the ?8coordinate o# the #ragent in e&e s.ace, or the inter.olation o#
the #og coordinate, as descri9ed in section '.11 D4ogE o# the co.ati9ilit& .ro#ile o# the ).en01
0ra.hics %&ste %.eci#ication. -he gl)Te%8oor#56 values are the inter.olated gl)Te%8oor#56 values
#ro a verte2 shader or the te2ture coordinates o# an& #i2ed .i.eline 9ased verte2 #unctionalit&.
*n#ices to the (ragment sha#er gl)Te%8oor# arra' are as #escri&e# a&ove in the verte% sha#er te%t.
4.2 Compatibilit! -rofile "erte, Shader /uilt7In Inputs
-he #ollowing .redeclared in.ut naes can 9e used #ro within a verte2 shader to access the current
values o# ).en01 state when using the co.ati9ilit& .ro#ile.
in ve%8 gl_Eolor,
in ve%8 gl_Se%ondar&Eolor,
in ve%; gl_Normal,
in ve%8 gl_Vertex,
in ve%8 gl_?ultiTexEoord=,
in ve%8 gl_?ultiTexEoord',
in ve%8 gl_?ultiTexEoord9,
in ve%8 gl_?ultiTexEoord;,
in ve%8 gl_?ultiTexEoord8,
in ve%8 gl_?ultiTexEoord<,
in ve%8 gl_?ultiTexEoordG,
in ve%8 gl_?ultiTexEoordF,
in float gl_FogEoord,
4.# /uilt7In Constants
-he #ollowing 9uilt8in constants are .rovided to all shaders. -he actual values used are i.leentation
de.endent, 9ut ust 9e at least the value shown. %oe are de.recated, as indicated in coents.
--
-- Implementation dependent %onstants+ T.e example values $elo/
-- are t.e minimum values allo/ed for t.ese maximums+
--
%onst int gl_?axVertex>ttri$s * 'G,
%onst int gl_?axVertexPniformEomponents * '=98,
%onst int gl_?axVar&ingFloats * G=, -- epre%ated
!"
7 Bui%t+in #aria$%es
%onst int gl_?axVar&ingEomponents * G=, -- epre%ated
%onst int gl_?axVertexOutputEomponents * G8,
%onst int gl_?ax!eometr&InputEomponents * G8,
%onst int gl_?ax!eometr&OutputEomponents * '9N,
%onst int gl_?axFragmentInputEomponents * '9N,
%onst int gl_?axVertexTextureImagePnits * 'G,
%onst int gl_?axEom$inedTextureImagePnits * N=,
%onst int gl_?axTextureImagePnits * 'G,
%onst int gl_?axFragmentPniformEomponents * '=98,
%onst int gl_?axra/6uffers * N,
%onst int gl_?axElipistan%es * N,
%onst int gl_?ax!eometr&TextureImagePnits * 'G,
%onst int gl_?ax!eometr&OutputVerti%es * 9<G,
%onst int gl_?ax!eometr&TotalOutputEomponents * '=98,
%onst int gl_?ax!eometr&PniformEomponents * '=98,
%onst int gl_?ax!eometr&Var&ingEomponents * G8,
%onst int gl_?axTessEontrolInputEomponents * '9N,
%onst int gl_?axTessEontrolOutputEomponents * '9N,
%onst int gl_?axTessEontrolTextureImagePnits * 'G,
%onst int gl_?axTessEontrolPniformEomponents * '=98,
%onst int gl_?axTessEontrolTotalOutputEomponents * 8=SG,
%onst int gl_?axTessEvaluationInputEomponents * '9N,
%onst int gl_?axTessEvaluationOutputEomponents * '9N,
%onst int gl_?axTessEvaluationTextureImagePnits * 'G,
%onst int gl_?axTessEvaluationPniformEomponents * '=98,
%onst int gl_?axTess@at%.Eomponents * '9=,
%onst int gl_?ax@at%.Verti%es * ;9,
%onst int gl_?axTess!enLevel * G8,
-he constant gl)7a%Var'ing-loats is de.recated, use gl)7a%Var'ing8omonents instead.
4.#.1 Compatibilit! -rofile /uilt7In Constants
%onst int gl_?axTexturePnits * 9,
%onst int gl_?axTextureEoords * N,
%onst int gl_?axElip@lanes * N,
4.& /uilt7In 5niform State
As an aid to accessing ).en01 .rocessing state, the #ollowing uni#or varia9les are 9uilt into the
).en01 %hading 1anguage.
!1
7 Bui%t+in #aria$%es
--
-- ept. range in /indo/ %oordinates1
-- se%tion 9+'8+' 4Eontrolling t.e Vie/port5 in t.e
-- Open!L !rap.i%s S&stem Spe%ifi%ation+
--
stru%t gl_ept.Range@arameters )
float near, -- n
float far, -- f
float diff, -- f 2 n
3,
uniform gl_ept.Range@arameters gl_ept.Range,
4.&.1 Compatibilit! -rofile State
-hese varia9les are .resent onl& in the co.ati9ilit& .ro#ile.
--
-- %ompati$ilit& profile onl&
--
uniform mat8 gl_?odelVie/?atrix,
uniform mat8 gl_@ro7e%tion?atrix,
uniform mat8 gl_?odelVie/@ro7e%tion?atrix,
uniform mat8 gl_Texture?atrixBgl_?axTextureEoordsC,
--
-- %ompati$ilit& profile onl&
--
uniform mat; gl_Normal?atrix, -- transpose of t.e inverse of t.e
-- upper leftmost ;x; of gl_?odelVie/?atrix
uniform mat8 gl_?odelVie/?atrixInverse,
uniform mat8 gl_@ro7e%tion?atrixInverse,
uniform mat8 gl_?odelVie/@ro7e%tion?atrixInverse,
uniform mat8 gl_Texture?atrixInverseBgl_?axTextureEoordsC,
uniform mat8 gl_?odelVie/?atrixTranspose,
uniform mat8 gl_@ro7e%tion?atrixTranspose,
uniform mat8 gl_?odelVie/@ro7e%tion?atrixTranspose,
uniform mat8 gl_Texture?atrixTransposeBgl_?axTextureEoordsC,
uniform mat8 gl_?odelVie/?atrixInverseTranspose,
uniform mat8 gl_@ro7e%tion?atrixInverseTranspose,
uniform mat8 gl_?odelVie/@ro7e%tion?atrixInverseTranspose,
uniform mat8 gl_Texture?atrixInverseTransposeBgl_?axTextureEoordsC,
--
-- %ompati$ilit& profile onl&
--
uniform float gl_NormalS%ale,
!2
7 Bui%t+in #aria$%es
--
-- %ompati$ilit& profile onl&
--
uniform ve%8 gl_Elip@laneBgl_?axElip@lanesC,
--
-- %ompati$ilit& profile onl&
--
stru%t gl_@oint@arameters )
float si"e,
float si"e?in,
float si"e?ax,
float fadeT.res.oldSi"e,
float distan%eEonstant>ttenuation,
float distan%eLinear>ttenuation,
float distan%eTuadrati%>ttenuation,
3,

uniform gl_@oint@arameters gl_@oint,
--
-- %ompati$ilit& profile onl&
--
stru%t gl_?aterial@arameters )
ve%8 emission, -- E%m
ve%8 am$ient, -- >%m
ve%8 diffuse, -- %m
ve%8 spe%ular, -- S%m
float s.ininess, -- Srm
3,
uniform gl_?aterial@arameters gl_Front?aterial,
uniform gl_?aterial@arameters gl_6a%:?aterial,
!3
7 Bui%t+in #aria$%es
--
-- %ompati$ilit& profile onl&
--
stru%t gl_Lig.tSour%e@arameters )
ve%8 am$ient, -- >%li
ve%8 diffuse, -- %li
ve%8 spe%ular, -- S%li
ve%8 position, -- @pli
ve%8 .alfVe%tor, -- erived( Ii
ve%; spotire%tion, -- Sdli
float spotExponent, -- Srli
float spotEutoff, -- Erli
-- #range( B=+=1S=+=C1 'N=+=)
float spotEosEutoff, -- erived( %os#Erli)
-- #range( B'+=1=+=C12'+=)
float %onstant>ttenuation, -- U=
float linear>ttenuation, -- U'
float Auadrati%>ttenuation,-- U9
3,
uniform gl_Lig.tSour%e@arameters gl_Lig.tSour%eBgl_?axLig.tsC,
stru%t gl_Lig.t?odel@arameters )
ve%8 am$ient, -- >%s
3,
uniform gl_Lig.t?odel@arameters gl_Lig.t?odel,
--
-- %ompati$ilit& profile onl&
--
-- erived state from produ%ts of lig.t and material+
--
stru%t gl_Lig.t?odel@rodu%ts )
ve%8 s%eneEolor, -- erived+ E%m K >%m J >%s
3,
uniform gl_Lig.t?odel@rodu%ts gl_FrontLig.t?odel@rodu%t,
uniform gl_Lig.t?odel@rodu%ts gl_6a%:Lig.t?odel@rodu%t,
stru%t gl_Lig.t@rodu%ts )
ve%8 am$ient, -- >%m J >%li
ve%8 diffuse, -- %m J %li
ve%8 spe%ular, -- S%m J S%li
3,
uniform gl_Lig.t@rodu%ts gl_FrontLig.t@rodu%tBgl_?axLig.tsC,
uniform gl_Lig.t@rodu%ts gl_6a%:Lig.t@rodu%tBgl_?axLig.tsC,
!4
7 Bui%t+in #aria$%es
--
-- %ompati$ilit& profile onl&
--
uniform ve%8 gl_TextureEnvEolorBgl_?axTexturePnitsC,
uniform ve%8 gl_E&e@laneSBgl_?axTextureEoordsC,
uniform ve%8 gl_E&e@laneTBgl_?axTextureEoordsC,
uniform ve%8 gl_E&e@laneRBgl_?axTextureEoordsC,
uniform ve%8 gl_E&e@laneTBgl_?axTextureEoordsC,
uniform ve%8 gl_O$7e%t@laneSBgl_?axTextureEoordsC,
uniform ve%8 gl_O$7e%t@laneTBgl_?axTextureEoordsC,
uniform ve%8 gl_O$7e%t@laneRBgl_?axTextureEoordsC,
uniform ve%8 gl_O$7e%t@laneTBgl_?axTextureEoordsC,
--
-- %ompati$ilit& profile onl&
--
stru%t gl_Fog@arameters )
ve%8 %olor,
float densit&,
float start,
float end,
float s%ale, -- erived( '+= - #end 2 start)
3,

uniform gl_Fog@arameters gl_Fog,
!5
/uilt7in .unctions
-he ).en01 %hading 1anguage de#ines an assortent o# 9uilt8in convenience #unctions #or scalar and
vector o.erations. :an& o# these 9uilt8in #unctions can 9e used in ore than one t&.e o# shader, 9ut soe
are intended to .rovide a direct a..ing to hardware and so are availa9le onl& #or a s.eci#ic t&.e o#
shader.
-he 9uilt8in #unctions 9asicall& #all into three categories:
-he& e2.ose soe necessar& hardware #unctionalit& in a convenient wa& such as accessing a te2ture
a.. -here is no wa& in the language #or these #unctions to 9e eulated 9& a shader.
-he& re.resent a trivial o.eration Fcla., i2, etc.G that is ver& si.le #or the user to write, 9ut the&
are ver& coon and a& have direct hardware su..ort. It is a ver& hard .ro9le #or the co.iler to
a. e2.ressions to co.le2 asse9ler instructions.
-he& re.resent an o.eration gra.hics hardware is likel& to accelerate at soe .oint. -he trigonoetr&
#unctions #all into this categor&.
:an& o# the #unctions are siilar to the sae naed ones in coon " li9raries, 9ut the& su..ort vector
in.ut as well as the ore traditional scalar in.ut.
A..lications should 9e encouraged to use the 9uilt8in #unctions rather than do the e=uivalent co.utations
in their own shader code since the 9uilt8in #unctions are assued to 9e o.tial Fe.g., .erha.s su..orted
directl& in hardwareG.
6ser code can re.lace 9uilt8in #unctions with their own i# the& choose, 9& si.l& re8declaring and de#ining
the sae nae and arguent list. Because 9uilt8in #unctions are in a ore outer sco.e than user 9uilt8in
#unctions, doing this will hide all 9uilt8in #unctions with the sae nae as the re8declared #unction.
Ahen the 9uilt8in #unctions are s.eci#ied 9elow, where the in.ut arguents Fand corres.onding out.utG
can 9e float, vec*, vec/, or vec,, genT'e is used as the arguent. Ahere the in.ut arguents Fand
corres.onding out.utG can 9e int, ivec*, ivec/, or ivec,, gen*T'e is used as the arguent. Ahere the
in.ut arguents Fand corres.onding out.utG can 9e uint, uvec*, uvec/, or uvec,, genFT'e is used as the
arguent. Ahere the in.ut arguents For corres.onding out.utG can 9e bool, bvec*, bvec/, or bvec,,
gen:T'e is used as the arguent. Ahere the in.ut arguents Fand corres.onding out.utG can 9e double,
dvec*, dvec/, dvec,, genDT'e is used as the arguent. 4or an& s.eci#ic use o# a #unction, the actual
t&.es su9stituted #or genT'e, gen*T'e, genFT'e$ or gen:T'e have to have the sae nu9er o#
co.onents #or all arguents and #or the return t&.e. %iilarl&, mat is used #or an& atri2 9asic t&.e
with single8.recision co.onents and #mat is used #or an& atri2 9asic t&.e with dou9le8.recision
co.onents.
!6
Bui%t+in ,unctions
.1 0ngle and Trigonometr! .unctions
4unction .araeters s.eci#ied as angle are assued to 9e in units o# radians. In no case will an& o# these
#unctions result in a divide 9& ?ero error. I# the divisor o# a ratio is (, then results will 9e unde#ined.
-hese all o.erate co.onent8wise. -he descri.tion is .er co.onent.
S!nta, +escription
gen-&.e radians Fgen-&.e #egreesG
"onverts #egrees to radians, i.e.,

1$(
#egrees
gen-&.e degrees Fgen-&.e ra#iansG
"onverts ra#ians to degrees, i.e.,
1$(

ra#ians
gen-&.e sin Fgen-&.e angleG -he standard trigonoetric sine #unction.
gen-&.e cos Fgen-&.e angleG -he standard trigonoetric cosine #unction.
gen-&.e tan Fgen-&.e angleG -he standard trigonoetric tangent.
gen-&.e asin Fgen-&.e %G Arc sine. Returns an angle whose sine is %. -he range
o# values returned 9& this #unction is
[

!
$

!
]
Results are unde#ined i# %1.
gen-&.e acos Fgen-&.e %G Arc cosine. Returns an angle whose cosine is %. -he
range o# values returned 9& this #unction is Z(, [.
Results are unde#ined i# %1.
gen-&.e atan Fgen-&.e ', gen-&.e %G Arc tangent. Returns an angle whose tangent is 'E%. -he
signs o# % and ' are used to deterine what =uadrant the
angle is in. -he range o# values returned 9& this
#unction is [$ ]. Results are unde#ined i# % and
' are 9oth (.
gen-&.e atan Fgen-&.e ')over)%G Arc tangent. Returns an angle whose tangent is
')over)%. -he range o# values returned 9& this #unction
is
[

!
$

!
]
.
!7
Bui%t+in ,unctions
S!nta, +escription
gen-&.e sinh Fgen-&.e %G Returns the h&.er9olic sine #unction
e
%
e
%
!
gen-&.e cosh Fgen-&.e %G Returns the h&.er9olic cosine #unction
e
%
e
%
!
gen-&.e tanh Fgen-&.e %G Returns the h&.er9olic tangent #unction
sinh %
cosh %
gen-&.e asinh Fgen-&.e %G Arc h&.er9olic sineL returns the inverse o# sinh.
gen-&.e acosh Fgen-&.e %G Arc h&.er9olic cosineL returns the non8negative inverse
o# cosh. Results are unde#ined i# % Q 1.
gen-&.e atanh Fgen-&.e %G Arc h&.er9olic tangentL returns the inverse o# tanh.
Results are unde#ined i# %1.
!
Bui%t+in ,unctions
.2 ',ponential .unctions
-hese all o.erate co.onent8wise. -he descri.tion is .er co.onent.
S!nta, +escription
gen-&.e pow Fgen-&.e %, gen-&.e 'G Returns % raised to the ' .ower, i.e., %
'
Results are unde#ined i# % G 0.
Results are unde#ined i# % < 0 and ' G< 0.
gen-&.e ep Fgen-&.e %G Returns the natural e2.onentiation o# %, i.e., e
%
.
gen-&.e log Fgen-&.e %G Returns the natural logarith o# %$ i.e., returns the value
' which satis#ies the e=uation % S e
'
.
Results are unde#ined i# % G< 0.
gen-&.e ep* Fgen-&.e %G Returns ! raised to the % .ower, i.e., !
%
gen-&.e log* Fgen-&.e %G Returns the 9ase ! logarith o# %$ i.e., returns the value
' which satis#ies the e=uation %=!
'
Results are unde#ined i# % G< 0.
gen-&.e sQrt Fgen-&.e %G
genD-&.e sQrt FgenD-&.e %G
Returns
% .
Results are unde#ined i# % G 0.
gen-&.e inversesQrt Fgen-&.e %G
genD-&.e inversesQrt FgenD-&.e %G
Returns
1
%
.
Results are unde#ined i# % G< 0.
!!
Bui%t+in ,unctions
.# Common .unctions
-hese all o.erate co.onent8wise. -he descri.tion is .er co.onent.
S!nta, +escription
gen-&.e abs Fgen-&.e %G
genI-&.e abs FgenI-&.e %G
genD-&.e abs FgenD-&.e %G
Returns % i# % RS (, otherwise it returns ^%.
gen-&.e sign Fgen-&.e %G
genI-&.e sign FgenI-&.e %G
genD-&.e sign FgenD-&.e %G
Returns 1.( i# % R (, (.( i# % S (, or ^1.( i# % Q (.
gen-&.e floor Fgen-&.e %G
genD-&.e floor FgenD-&.e %G
Returns a value e=ual to the nearest integer that is less
than or e=ual to %.
gen-&.e trunc Fgen-&.e %G
genD-&.e trunc FgenD-&.e %G
Returns a value e=ual to the nearest integer to % whose
a9solute value is not larger than the a9solute value o# %.
gen-&.e round Fgen-&.e %G
genD-&.e round FgenD-&.e %G
Returns a value e=ual to the nearest integer to %. -he
#raction (., will round in a direction chosen 9& the
i.leentation, .resua9l& the direction that is #astest.
-his includes the .ossi9ilit& that roundF%G returns the
sae value as round2venF%G #or all values o# %.
gen-&.e round2ven Fgen-&.e %G
genD-&.e round2ven FgenD-&.e %G
Returns a value e=ual to the nearest integer to %. A
#ractional .art o# (., will round toward the nearest even
integer. FBoth '., and *., #or 2 will return *.(.G
gen-&.e ceil Fgen-&.e %G
genD-&.e ceil FgenD-&.e %G
Returns a value e=ual to the nearest integer that is
greater than or e=ual to %.
gen-&.e fract Fgen-&.e %G
genD-&.e fract FgenD-&.e %G
Returns % ^ floor F%G.
gen-&.e mod Fgen-&.e %, #loat 'G
gen-&.e mod Fgen-&.e %, gen-&.e 'G
genD-&.e mod FgenD-&.e %, dou9le 'G
genD-&.e mod FgenD-&.e %, genD-&.e 'G
:odulus. Returns % ^ ' floor F%O'G.
1""
Bui%t+in ,unctions
S!nta, +escription
gen-&.e modf Fgen-&.e %, out gen-&.e iG
genD-&.e modf FgenD-&.e %,
out genD-&.e iG
Returns the #ractional .art o# % and sets i to the integer
.art Fas a whole nu9er #loating .oint valueG. Both the
return value and the out.ut .araeter will have the sae
sign as %.
gen-&.e min Fgen-&.e %, gen-&.e 'G
gen-&.e min Fgen-&.e %, #loat 'G
genD-&.e min FgenD-&.e %, genD-&.e 'G
genD-&.e min FgenD-&.e %, dou9le 'G
genI-&.e min FgenI-&.e %, genI-&.e 'G
genI-&.e min FgenI-&.e %, int 'G
gen6-&.e min Fgen6-&.e %, gen6-&.e 'G
gen6-&.e min Fgen6-&.e %, uint 'G
Returns ' i# ' Q %, otherwise it returns %.
gen-&.e ma Fgen-&.e %, gen-&.e 'G
gen-&.e ma Fgen-&.e %, #loat 'G
genD-&.e ma FgenD-&.e %, genD-&.e 'G
genD-&.e ma FgenD-&.e %, dou9le 'G
genI-&.e ma FgenI-&.e %, genI-&.e 'G
genI-&.e ma FgenI-&.e %, int 'G
gen6-&.e ma Fgen6-&.e %, gen6-&.e 'G
gen6-&.e ma Fgen6-&.e %, uint 'G
Returns ' i# % Q ', otherwise it returns %.
1"1
Bui%t+in ,unctions
S!nta, +escription
gen-&.e clamp Fgen-&.e %,
gen-&.e minVal,
gen-&.e ma%ValG
gen-&.e clamp Fgen-&.e %,
#loat minVal,
#loat ma%ValG
genD-&.e clamp FgenD-&.e %,
genD-&.e minVal,
genD-&.e ma%ValG
genD-&.e clamp FgenD-&.e %,
dou9le minVal,
dou9le ma%ValG
genI-&.e clamp FgenI-&.e %,
genI-&.e minVal,
genI-&.e ma%ValG
genI-&.e clamp FgenI-&.e %,
int minVal,
int ma%ValG
gen6-&.e clamp Fgen6-&.e %,
gen6-&.e minVal,
gen6-&.e ma%ValG
gen6-&.e clamp Fgen6-&.e %,
uint minVal,
uint ma%ValG
Returns min Fma F%, minValG, ma%ValG.
Results are unde#ined i# minVal R ma%Val.
gen-&.e mi Fgen-&.e %,
gen-&.e ',
gen-&.e aG
gen-&.e mi Fgen-&.e %,
gen-&.e ',
#loat aG
genD-&.e mi FgenD-&.e %,
genD-&.e ',
genD-&.e aG
genD-&.e mi FgenD-&.e %,
genD-&.e ',
dou9le aG
Returns the linear 9lend o# % and '$ i.e.,
%1a'a
1"2
Bui%t+in ,unctions
S!nta, +escription
gen-&.e mi Fgen-&.e %,
gen-&.e &,
genB-&.e aG
genD-&.e mi FgenD-&.e %,
genD-&.e &,
genB-&.e aG
%elects which vector each returned co.onent coes
#ro. 4or a co.onent o# a that is false, the
corres.onding co.onent o# % is returned. 4or a
co.onent o# a that is true, the corres.onding
co.onent o# ' is returned. "o.onents o# % and ' that
are not selected are allowed to 9e invalid #loating .oint
values and will have no e##ect on the results. -hus, this
.rovides di##erent #unctionalit& than, #or e2a.le,
gen-&.e miFgen-&.e %, gen-&.e ', gen-&.eFaGG
where a is a Boolean vector.
gen-&.e step Fgen-&.e e#ge, gen-&.e %G
gen-&.e step F#loat e#ge, gen-&.e %G
genD-&.e step FgenD-&.e e#ge,
genD-&.e %G
genD-&.e step Fdou9le e#ge, genD-&.e %G
Returns (.( i# % Q e#ge, otherwise it returns 1.(.
gen-&.e smoothstep Fgen-&.e e#ge0,
gen-&.e e#ge1,
gen-&.e %G
gen-&.e smoothstep F#loat e#ge0,
#loat e#ge1,
gen-&.e %G
genD-&.e smoothstep FgenD-&.e e#ge0,
genD-&.e e#ge1,
genD-&.e %G
genD-&.e smoothstep Fdou9le e#ge0,
dou9le e#ge1,
genD-&.e %G
Returns (.( i# % QS e#ge0 and 1.( i# % RS e#ge1 and
.er#ors sooth +erite inter.olation 9etween ( and 1
when e#ge0 Q % Q e#ge1. -his is use#ul in cases where
&ou would want a threshold #unction with a sooth
transition. -his is e=uivalent to:
gen-&.e tL
t S cla. FF2 ^ edge(G O Fedge1 ^ edge(G, (, 1GL
return t I t I F' ^ ! I tGL
FAnd siilarl& #or dou9les.G
Results are un#e(ine# i( e#ge0 H< e#ge1.
genB-&.e isnan Fgen-&.e %G
genB-&.e isnan FgenD-&.e %G
Returns true i# % holds a 7a7. Returns false otherwise.
genB-&.e isinf Fgen-&.e %G
genB-&.e isinf FgenD-&.e %G
Returns true i# % holds a .ositive in#init& or negative
in#init&. Returns false otherwise.
genI-&.e float1itsOo&nt Fgen-&.e valueG
gen6-&.e float1itsOo)int Fgen-&.e valueG
Returns a signed or unsigned integer value re.resenting
the encoding o# a #loating8.oint value. -he #loating8
.oint valueHs 9it8level re.resentation is .reserved.
1"3
Bui%t+in ,unctions
S!nta, +escription
gen-&.e int1itsOoRloat FgenI-&.e valueG
gen-&.e uint1itsOoRloat Fgen6-&.e valueG
Returns a #loating8.oint value corres.onding to a signed
or unsigned integer encoding o# a #loating8.oint value.
I# an in# or 7a7 is .assed in, it will not signal, and the
resulting #loating .oint value is uns.eci#ied. )therwise,
the 9it8level re.resentation is .reserved.
gen-&.e fma Fgen-&.e a, gen-&.e &,
gen-&.e cG
genD-&.e fma FgenD-&.e a, genD-&.e &,
genD-&.e cG
"o.utes and returns aA& C c.
In uses where the return value is eventuall& consued 9&
a varia9le declared as precise:
fmaFG is considered a single o.eration, whereas the
e2.ression DaI& J cE consued 9& a varia9le
declared precise is considered two o.erations.
-he .recision o# fmaFG can di##er #ro the .recision
o# the the e2.ression DaI& J cE.
fmaFG will 9e co.uted with the sae .recision as
an& other fmaFG consued 9& a precise varia9le,
giving invariant results #or the sae in.ut values o#
a, &, and c.
)therwise, in the a9sence o# precise consu.tion, there
are no s.ecial constraints on the nu9er o# o.erations or
di##erence in .recision 9etween fmaFG and the e2.ression
DaI& J cE.
gen-&.e frep Fgen-&.e %,
out genI-&.e e%G
genD-&.e frep FgenD-&.e %,
out genI-&.e e%G
%.lits % into a #loating8.oint signi#icand in the range
Z(.,, 1.(G and an integral e2.onent o# two, such that:
%=signi(ican#!
e%onent
-he signi#icand is returned 9& the #unction and the
e2.onent is returned in the .araeter e%. 4or a
#loating8.oint value o# ?ero, the signi#icant and e2.onent
are 9oth ?ero. 4or a #loating8.oint value that is an
in#init& or is not a nu9er, the results are unde#ined.
gen-&.e ldep Fgen-&.e %,
in genI-&.e e%G
genD-&.e ldep FgenD-&.e %,
in genI-&.e e%G
Builds a #loating8.oint nu9er #ro % and the
corres.onding integral e2.onent o# two in e%, returning:
signi(ican#!
e%onent
I# this .roduct is too large to 9e re.resented in the
#loating8.oint t&.e, the result is unde#ined.
1"4
Bui%t+in ,unctions
1"5
Bui%t+in ,unctions
.& .loating7-oint -ac2 and 5npac2 .unctions
-hese #unctions do not o.erate co.onent8wise, rather as descri9ed in each case.
S!nta, +escription
uint pack)norm*1+ Fvec! vG
uint pack)norm,- Fvec* vG
uint packSnorm,- Fvec* vG
4irst, converts each co.onent o# the norali?ed
#loating8.oint value v into $8 or 1/89it integer values.
-hen, the results are .acked into the returned '!89it
unsigned integer.
-he conversion #or co.onent c o# v to #i2ed .oint is
done as #ollows:
pack)norm*1+: roundFcla.Fc, (, J1G I /,,',.(G
pack)norm,-: roundFcla.Fc, (, J1G I !,,.(G
packSnorm,-: roundFcla.Fc, 81, J1G I 1!5.(G
-he #irst co.onent o# the vector will 9e written to the
least signi#icant 9its o# the out.utL the last co.onent
will 9e written to the ost signi#icant 9its.
vec! unpack)norm*1+ Fuint G
vec* unpack)norm,- Fuint G
vec* unpackSnorm,- Fuint G
4irst, un.acks a single '!89it unsigned integer into a
.air o# 1/89it unsigned integers, #our $89it unsigned
integers, or #our $89it signed integers. -hen, each
co.onent is converted to a norali?ed #loating8.oint
value to generate the returned two8 or #our8co.onent
vector.
-he conversion #or un.acked #i2ed8.oint value ( to
#loating .oint is done as #ollows:
unpack)norm*1+K ( O /,,',.(
unpack)norm,-K ( O !,,.(
unpackSnorm,-K cla.F( O 1!5.(, 81, J1G
-he #irst co.onent o# the returned vector will 9e
e2tracted #ro the least signi#icant 9its o# the in.utL the
last co.onent will 9e e2tracted #ro the ost
signi#icant 9its.
dou9le pack.ouble*/* Fuvec! vG Returns a dou9le8.recision value o9tained 9& .acking
the co.onents o# v into a /*89it value. I# an IEEE 5,*
in# or 7a7 is created, it will not signal, and the resulting
#loating .oint value is uns.eci#ied. )therwise, the 9it8
level re.resentation o# v is .reserved. -he #irst vector
co.onent s.eci#ies the '! least signi#icant 9itsL the
second co.onent s.eci#ies the '! ost signi#icant 9its.
1"6
Bui%t+in ,unctions
S!nta, +escription
uvec! unpack.ouble*/* Fdou9le v4 Returns a two8co.onent unsigned integer vector
re.resentation o# v. -he 9it8level re.resentation o# v is
.reserved. -he #irst co.onent o# the vector contains
the '! least signi#icant 9its o# the dou9leL the second
co.onent consists the '! ost signi#icant 9its.
1"7
Bui%t+in ,unctions
.) Geometric .unctions
-hese o.erate on vectors as vectors, not co.onent8wise.
S!nta, +escription
#loat length Fgen-&.e %G
dou9le length FgenD-&.e %G
Returns the length o# vector %, i.e.,
%[ (]
!
%[ 1]
!
...
#loat distance Fgen-&.e 0, gen-&.e 1G
dou9le distance FgenD-&.e 0,
genD-&.e 1G
Returns the distance 9etween 0 and 1, i.e.,
length F0 I 1G
#loat dot Fgen-&.e %, gen-&.e 'G
dou9le dot FgenD-&.e %, genD-&.e 'G
Returns the dot .roduct o# % and ', i.e.,
%[ (]' [(]% [1]'[1]...
vec' cross Fvec' %, vec' 'G
dvec' cross Fdvec' %, dvec' 'G
Returns the cross .roduct o# 2 and &, i.e.,
[
% [1]'[ !]' [1]% [!]
%[ !]'[ (]' [ !]%[ (]
%[(]' [1]' [(]% [1]
]
gen-&.e normali!e Fgen-&.e %G
genD-&.e normali!e FgenD-&.e %G
Returns a vector in the sae direction as % 9ut with a
length o# 1.
co.ati9ilit& .ro#ile onl&
vec* ftransformFG
Availa9le onl& when using the co.ati9ilit& .ro#ile. 4or
core ).en01, use invariant.
4or verte2 shaders onl&. -his #unction will ensure that
the incoing verte2 value will 9e trans#ored in a wa&
that .roduces e2actl& the sae result as would 9e
.roduced 9& ).en01Ks #i2ed #unctionalit& trans#or. It
is intended to 9e used to co.ute gl);osition, e.g.,
gl>3osition S ftransformFG
-his #unction should 9e used, #or e2a.le, when an
a..lication is rendering the sae geoetr& in se.arate
.asses, and one .ass uses the #i2ed #unctionalit& .ath to
render and another .ass uses .rograa9le shaders.
1"
Bui%t+in ,unctions
S!nta, +escription
gen-&.e faceforward Fgen-&.e +,
gen-&.e *,
gen-&.e +re(G
genD-&.e faceforward FgenD-&.e +,
genD-&.e *,
genD-&.e +re(G
I# dotF+re(, *G Q ( return +$ otherwise return ^+.
gen-&.e reflect Fgen-&.e *, gen-&.e +G
genD-&.e reflect FgenD-&.e *,
genD-&.e +G
4or the incident vector * and sur#ace orientation +,
returns the re#lection direction:
* ^ ! dotF+, *G +
+ ust alread& 9e norali?ed in order to achieve the
desired result.
gen-&.e refract Fgen-&.e *, gen-&.e +,
#loat etaG
genD-&.e refract FgenD-&.e *,
genD-&.e +,
#loat etaG
4or the incident vector * and sur#ace noral +, and the
ratio o# indices o# re#raction eta$ return the re#raction
vector. -he result is co.uted 9&
k S 1.( 8 eta I eta I F1.( 8 dotF+, *G I dotF+, *GG
i# Fk Q (.(G
return gen-&.eF(.(G OO or genD-&.eF(.(G
else
return eta I * 8 Feta I dotF+, *G J sQrtFkGG I +
-he in.ut .araeters #or the incident vector * and the
sur#ace noral + ust alread& 9e norali?ed to get the
desired results.
1"!
Bui%t+in ,unctions
.* 8atri, .unctions
4or each o# the #ollowing 9uilt8in atri2 #unctions, there is 9oth a single8.recision #loating .oint version,
where all arguents and return values are single .recision, and a dou9le8.recision #loating version, where
all arguents and return values are dou9le .recision. )nl& the single8.recision #loating .oint version is
shown.
S!nta, +escription
at matriComp%ult Fat %, at 'G :ulti.l& atri2 % 9& atri2 ' co.onent8wise, i.e.,
resultZi[Z@[ is the scalar .roduct o# %Zi[Z@[ and 'Zi[Z@[.
7ote: to get linear alge9raic atri2 ulti.lication, use
the ulti.l& o.erator F=G.
at! outer6roductFvec! c, vec! rG
at' outer6roductFvec' c, vec' rG
at* outer6roductFvec* c, vec* rG
at!2' outer6roductFvec' c, vec! rG
at'2! outer6roductFvec! c, vec' rG
at!2* outer6roductFvec* c, vec! rG
at*2! outer6roductFvec! c, vec* rG
at'2* outer6roductFvec* c, vec' rG
at*2' outer6roductFvec' c, vec* rG
-reats the #irst .araeter c as a colun vector Fatri2
with one colunG and the second .araeter r as a row
vector Fatri2 with one rowG and does a linear alge9raic
atri2 ulti.l& c I r, &ielding a atri2 whose nu9er o#
rows is the nu9er o# co.onents in c and whose
nu9er o# coluns is the nu9er o# co.onents in r.
at! transposeFat! mG
at' transposeFat' mG
at* transposeFat* mG
at!2' transposeFat'2! mG
at'2! transposeFat!2' mG
at!2* transposeFat*2! mG
at*2! transposeFat!2* mG
at'2* transposeFat*2' mG
at*2' transposeFat'2* mG
Returns a atri2 that is the trans.ose o# m. -he in.ut
atri2 m is not odi#ied.
#loat determinantFat! mG
#loat determinantFat' mG
#loat determinantFat* mG
Returns the deterinant o# m.
11"
Bui%t+in ,unctions
S!nta, +escription
at! inverseFat! mG
at' inverseFat' mG
at* inverseFat* mG
Returns a atri2 that is the inverse o# m. -he in.ut
atri2 m is not odi#ied. -he values in the returned
atri2 are unde#ined i# m is singular or .oorl&8
conditioned Fnearl& singularG.
111
Bui%t+in ,unctions
.4 "ector Relational .unctions
Relational and e=ualit& o.erators F?, ?I, @, @I, II, JIG are de#ined to o.erate on scalars and .roduce
scalar Boolean results. 4or vector results, use the #ollowing 9uilt8in #unctions. Below, the #ollowing
.laceholders are used #or the listed s.eci#ic t&.es:
6laceholder Specific O0pes Allowed
9vec 9vec!, 9vec', 9vec*
ivec ivec!, ivec', ivec*
uvec uvec!, uvec', uvec*
vec vec!, vec', vec*, dvec!, dvec', dvec*
In all cases, the si?es o# all the in.ut and return vectors #or an& .articular call ust atch.
S!nta, +escription
9vec lessOhanFvec 2, vec &G
9vec lessOhanFivec 2, ivec &G
9vec lessOhanFuvec 2, uvec &G
Returns the co.onent8wise co.are o# % Q '.
9vec lessOhan2QualFvec 2, vec &G
9vec lessOhan2QualFivec 2, ivec &G
9vec lessOhan2QualFuvec 2, uvec &G
Returns the co.onent8wise co.are o# % QS '.
9vec greaterOhanFvec 2, vec &G
9vec greaterOhanFivec 2, ivec &G
9vec greaterOhanFuvec 2, uvec &G
Returns the co.onent8wise co.are o# % R '.
9vec greaterOhan2QualFvec 2, vec &G
9vec greaterOhan2QualFivec 2, ivec &G
9vec greaterOhan2QualFuvec 2, uvec &G
Returns the co.onent8wise co.are o# % RS '.
9vec eQualFvec 2, vec &G
9vec eQualFivec 2, ivec &G
9vec eQualFuvec 2, uvec &G
9vec eQualF9vec 2, 9vec &G
9vec not2QualFvec 2, vec &G
9vec not2QualFivec 2, ivec &G
9vec not2QualFuvec 2, uvec &G
9vec not2QualF9vec 2, 9vec &G
Returns the co.onent8wise co.are o# % SS '.
Returns the co.onent8wise co.are o# % NS '.
9ool an0F9vec 2G Returns true i# an& co.onent o# % is true.
112
Bui%t+in ,unctions
S!nta, +escription
9ool allF9vec 2G Returns true onl& i# all co.onents o# % are true.
9vec notF9vec 2G Returns the co.onent8wise logical co.leent o# %.
113
Bui%t+in ,unctions
. Integer .unctions
-hese all o.erate co.onent8wise. -he descri.tion is .er co.onent. -he notation Za, &[ eans the set
o# 9its #ro 9it8nu9er a through 9it8nu9er &, inclusive. -he lowest8order 9it is 9it (.
S!nta, +escription
gen6-&.e uaddCarr0 Fgen6-&.e %,
gen6-&.e ',
out gen6-&.e carr'G
Adds '!89it unsigned integer % and ', returning the su
odulo !
'!
. -he value carr' is set to ( i# the su was
less than !
'!
, or to 1 otherwise.
gen6-&.e usub1orrow Fgen6-&.e %,
gen6-&.e ',
out gen6-&.e &orro/G
%u9tracts the '!89it unsigned integer ' #ro %, returning
the di##erence i# non8negative, or !
'!
.lus the di##erence
otherwise. -he value &orro/ is set to ( i# % H< ', or to
1 otherwise.
void umul2tended Fgen6-&.e %,
gen6-&.e ',
out gen6-&.e ms&,
out gen6-&.e ls&G
void imul2tended FgenI-&.e %,
genI-&.e ',
out genI-&.e ms&,
out genI-&.e ls&G
:ulti.lies '!89it integers % and ', .roducing a /*89it
result. -he '! least8signi#icant 9its are returned in ls&.
-he '! ost8signi#icant 9its are returned in ms&.
genI-&.e bitfield2tract FgenI-&.e value,
int o((set, int &itsG
gen6-&.e bitfield2tract Fgen6-&.e value,
int o((set, int &itsG
E2tracts 9its Zo((set, o((set J &its - 1[ #ro value,
returning the in the least signi#icant 9its o# the result.
4or unsigned data t&.es, the ost signi#icant 9its o# the
result will 9e set to ?ero. 4or signed data t&.es, the
ost signi#icant 9its will 9e set to the value o# 9it o((set
J &ase I 1.
I# &its is ?ero, the result will 9e ?ero. -he result will 9e
unde#ined i# o((set or &its is negative, or i# the su o#
o((set and &its is greater than the nu9er o# 9its used
to store the o.erand.
114
Bui%t+in ,unctions
S!nta, +escription
genI-&.e bitfield&nsert FgenI-&.e &ase,
genI-&.e insert,
int o((set, int &itsG
gen6-&.e bitfield&nsert Fgen6-&.e &ase,
gen6-&.e insert,
int o((set, int &itsG
Returns the insertion the &its least8signi#icant 9its o#
insert into &ase.
-he result will have 9its Zo((set, o((set J &its - 1[ taken
#ro 9its Z(, &its I 1[ o# insert, and all other 9its taken
directl& #ro the corres.onding 9its o# &ase. I# &its is
?ero, the result will si.l& 9e &ase. -he result will 9e
unde#ined i# o((set or &its is negative, or i# the su o#
o((set and &its is greater than the nu9er o# 9its used to
store the o.erand.
genI-&.e bitfield3everse FgenI-&.e valueG
gen6-&.e bitfield3everse Fgen6-&.e valueG
Returns the reversal o# the 9its o# value. -he 9it
nu9ered n o# the result will 9e taken #ro 9it F&its - 1G
- n o# value, where &its is the total nu9er o# 9its used
to re.resent value.
genI-&.e bitCount FgenI-&.e valueG
genI-&.e bitCount Fgen6-&.e valueG
Returns the nu9er o# 9its set to 1 in the 9inar&
re.resentation o# value.
genI-&.e find"S1 FgenI-&.e valueG
genI-&.e find"S1 Fgen6-&.e valueG
Returns the 9it nu9er o# the least signi#icant 9it set to
1 in the 9inar& re.resentation o# value. I# value is ?ero,
81will 9e returned.
genI-&.e find%S1 FgenI-&.e valueG
genI-&.e find%S1 Fgen6-&.e valueG
Returns the 9it nu9er o# the ost signi#icant 9it in the
9inar& re.resentation o# value.
4or .ositive integers, the result will 9e the 9it nu9er o#
the ost signi#icant 9it set to 1. 4or negative integers,
the result will 9e the 9it nu9er o# the ost signi#icant
9it set to (. 4or a value o# ?ero or negative one, 81 will
9e returned.
115
Bui%t+in ,unctions
.9 Te,ture .unctions
-e2ture looku. #unctions are availa9le in all shading stages. +owever, autoatic level o# detail is
co.uted onl& #or #ragent shaders. )ther shaders o.erate as though the 9ase level o# detail were
co.uted as ?ero. -he #unctions in the ta9le 9elow .rovide access to te2tures through sa.lers, as set u.
through the ).en01 A3I. -e2ture .ro.erties such as si?e, .i2el #orat, nu9er o# diensions, #iltering
ethod, nu9er o# i.8a. levels, de.th co.arison, and so on are also de#ined 9& ).en01 A3I calls.
%uch .ro.erties are taken into account as the te2ture is accessed via the 9uilt8in #unctions de#ined 9elow.
-e2ture data can 9e stored 9& the 01 as #loating .oint, unsigned norali?ed integer, unsigned integer or
signed integer data. -his is deterined 9& the t&.e o# the internal #orat o# the te2ture. -e2ture looku.s
on unsigned norali?ed integer and #loating .oint data return #loating .oint values in the range Z(, 1[.
-e2ture looku. #unctions are .rovided that can return their result as #loating .oint, unsigned integer or
signed integer, de.ending on the sa.ler t&.e .assed to the looku. #unction. "are ust 9e taken to use
the right sa.ler t&.e #or te2ture access. -he #ollowing ta9le lists the su..orted co9inations o# sa.ler
t&.es and te2ture internal #orats. Blank entries are unsu..orted. Doing a te2ture looku. will return
unde#ined values #or unsu..orted co9inations.
Internal -e2ture 4orat
4loating 3oint
%a.ler -&.es
%igned Integer
%a.ler -&.es
6nsigned Integer
%a.ler -&.es
4loating .oint %u..orted
7orali?ed Integer %u..orted
%igned Integer %u..orted
6nsigned Integer %u..orted
I# an integer sa.ler t&.e is used, the result o# a te2ture looku. is an ivec,. I# an unsigned integer sa.ler
t&.e is used, the result o# a te2ture looku. is a uvec,. I# a #loating .oint sa.ler t&.e is used, the result o#
a te2ture looku. is a vec,, where each co.onent is in the range Z(, 1[.
In the .rotot&.es 9elow, the DgE in the return t&.e Dgvec4E is used as a .laceholder #or nothing, DiE, or DuE
aking a return t&.e o# vec,, ivec,, or uvec,. In these cases, the sa.ler arguent t&.e also starts with
DgE, indicating the sae su9stitution done on the return t&.eL it is either a #loating .oint, signed integer, or
unsigned integer sa.ler, atching the 9asic t&.e o# the return t&.e, as descri9ed a9ove.
4or shadow #ors Fthe sa.ler .araeter is a shadow8t&.eG, a de.th co.arison looku. on the de.th
te2ture 9ound to samler is done as descri9ed in section '.$.1/ D-e2ture "o.arison :odesE o# the
).en01 0ra.hics %&ste %.eci#ication. %ee the ta9le 9elow #or which co.onent s.eci#ies Dre(. -he
te2ture 9ound to samler ust 9e a de.th te2ture, or results are unde#ined. I# a non8shadow te2ture call is
ade to a sa.ler that re.resents a de.th te2ture with de.th co.arisons turned on, then results are
unde#ined. I# a shadow te2ture call is ade to a sa.ler that re.resents a de.th te2ture with de.th
co.arisons turned o##, then results are unde#ined. I# a shadow te2ture call is ade to a sa.ler that does
not re.resent a de.th te2ture, then results are unde#ined.
116
Bui%t+in ,unctions
In all #unctions 9elow, the &ias .araeter is o.tional #or #ragent shaders. -he &ias .araeter is not
acce.ted in an& other shaders. 4or a #ragent shader, i# &ias is .resent, it is added to the i.licit level o#
detail .rior to .er#oring the te2ture access o.eration. 7o &ias or lo# .araeters #or rectangular te2tures,
ulti8sa.le te2tures, or te2ture 9u##ers are su..orted 9ecause i.8a.s are not allowed #or these t&.es
o# te2tures.
-he i.licit level o# detail is selected as #ollows: 4or a te2ture that is not i.8a..ed, the te2ture is used
directl&. I# it is i.8a..ed and running in a #ragent shader, the 1)D co.uted 9& the i.leentation
is used to do the te2ture looku.. I# it is i.8a..ed and running on the verte2 shader, then the 9ase
te2ture is used.
%oe te2ture #unctions Fnon8D"odE and non8D#radE versionsG a& re=uire i.licit derivatives. I.licit
derivatives are unde#ined within non8uni#or control #low and #or non8#ragent8shader te2ture #etches.
4or Cube #ors, the direction o# ; is used to select which #ace to do a !8diensional te2ture looku. in, as
descri9ed in section '.$.1( D"u9e :a. -e2ture %electionE in the ).en01 0ra.hics %&ste %.eci#ication.
4or Arra0 #ors, the arra& la&er used will 9e
ma% (, min # 1, (loorla'er(.,
where # is the de.th o# the te2ture arra& and la'er coes #ro the co.onent indicated in the ta9les
9elow.
.9.1 Te,ture :uer! .unctions
-he tetureSi!e #unctions =uer& the diensions o# a s.eci#ic te2ture level #or a sa.ler.
-he teture4uer0"od #unctions are availa9le onl& in a #ragent shader. -he& take the co.onents o# ;
and co.ute the level o# detail in#oration that the te2ture .i.e would use to access that te2ture through a
noral te2ture looku.. -he level o# detail JK Fe=uation '.1$ in the ).en01 0ra.hics %&ste
%.eci#icationG is o9tained a#ter an& 1)D 9ias, 9ut .rior to cla.ing to Z-EY-6RE>:I7>1)D,
-EY-6RE>:AY>1)D[. -he i.a. arra&FsG that would 9e accessed are also co.uted. I# a single
level o# detail would 9e accessed, the level8o#8detail nu9er relative to the 9ase level is returned. I#
ulti.le levels o# detail would 9e accessed, a #loating8.oint nu9er 9etween the two levels is returned,
with the #ractional .art e=ual to the #ractional .art o# the co.uted and cla.ed level o# detail. -he
algorith used is given 9& the #ollowing .seudo8code:
117
Bui%t+in ,unctions
float Eompute>%%essedLod#float %omputedLod)
)
-- Elamp t.e %omputed LO a%%ording to t.e texture LO %lamps+
if #%omputedLod 0 TEOTPRE_?IN_LO) %omputedLod * TEOTPRE_?IN_LO,
if #%omputedLod L TEOTPRE_?>O_LO) %omputedLod * TEOTPRE_?>O_LO,
-- Elamp t.e %omputed LO to t.e range of a%%essi$le levels+
if #%omputedLod 0 =)
%omputedLod * =+=,
if #%omputedLod L #float)
max>%%essi$leLevel) %omputedLod * #float) max>%%essi$leLevel,
-- Return a value a%%ording to t.e min filter+
if #TEOTPRE_?IN_FILTER is LINE>R or NE>REST) )
return =+=,
3 else if #TEOTPRE_?IN_FILTER is NE>REST_?I@?>@_NE>REST
or LINE>R_?I@?>@_NE>REST) )
return %eil#%omputedLod K =+<) 2 '+=,
3 else )
return %omputedLod,
3
3
-he value ma%@ccessi&leLevel is the level nu9er o# the sallest accessi9le level o# the i.a. arra&
Fthe value 1 in section '.$.; D-e2ture :ini#icationE o# the ).en01 0ra.hics %&ste %.eci#icationG inus
the 9ase level.
S!nta, +escription
int tetureSi!e Fgsa.ler1D samler, int lo#G
ivec! tetureSi!e Fgsa.ler!D samler, int lo#G
ivec' tetureSi!e Fgsa.ler'D samler, int lo#G
ivec! tetureSi!e Fgsa.ler"u9e samler, int lo#G
int tetureSi!e Fsa.ler1D%hadow samler, int lo#G
ivec! tetureSi!e Fsa.ler!D%hadow samler, int lo#G
ivec! tetureSi!e Fsa.ler"u9e%hadow samler, int lo#G
ivec' tetureSi!e Fsa.ler"u9eArra& samler, int lo#G
ivec' tetureSi!e Fsa.ler"u9eArra&%hadow samler, int lo#G
ivec! tetureSi!e Fgsa.ler!DRect samlerG
ivec! tetureSi!e Fsa.ler!DRect%hadow samlerG
ivec! tetureSi!e Fgsa.ler1DArra& samler, int lo#G
ivec' tetureSi!e Fgsa.ler!DArra& samler, int lo#G
ivec! tetureSi!e Fsa.ler1DArra&%hadow samler, int lo#G
ivec' tetureSi!e Fsa.ler!DArra&%hadow samler, int lo#G
int tetureSi!e Fgsa.lerBu##er samlerG
ivec! tetureSi!e Fgsa.ler!D:% samlerG
ivec! tetureSi!e Fgsa.ler!D:%Arra& samlerG
Returns the diensions o# level
lo# Fi# .resentG #or the te2ture
9ound to samler, as descri9ed
in section !.11.5 D%hader
E2ecutionE o# the ).en01
0ra.hics %&ste %.eci#ication,
under D-e2ture %i?e <uer&E.
-he co.onents in the return
value are #illed in, in order, with
the width, height, de.th o# the
te2ture.
4or the arra& #ors, the last
co.onent o# the return value is
the nu9er o# la&ers in the
te2ture arra&.
11
Bui%t+in ,unctions
S!nta, +escription
vec! teture4uer0"odFgsa.ler1D samler, #loat ;G
vec! teture4uer0"odFgsa.ler!D samler, vec! ;G
vec! teture4uer0"odFgsa.ler'D samler, vec' ;G
vec! teture4uer0"odFgsa.ler"u9e samler, vec' ;G
vec! teture4uer0"odFgsa.ler1DArra& samler, #loat ;G
vec! teture4uer0"odFgsa.ler!DArra& samler, vec! ;G
vec! teture4uer0"odFgsa.ler"u9eArra& samler, vec' ;G
vec! teture4uer0"odFsa.ler1D%hadow samler, #loat ;G
vec! teture4uer0"odFsa.ler!D%hadow samler, vec! ;G
vec! teture4uer0"odFsa.ler"u9e%hadow samler, vec' ;G
vec! teture4uer0"odFsa.ler1DArra&%hadow samler, #loat ;G
vec! teture4uer0"odFsa.ler!DArra&%hadow samler, vec! ;G
vec! teture4uer0"odFsa.ler"u9eArra&%hadow samler, vec' ;G
Returns the i.a. arra&FsG
that would 9e accessed in the %
co.onent o# the return value.
Returns the co.uted level o#
detail relative to the 9ase level
in the ' co.onent o# the return
value.
I# called on an inco.lete
te2ture, the results are
unde#ined.
.9.2 Te,el Loo2up .unctions
S!nta, +escription
gvec* teture Fgsa.ler1D samler$ #loat ; Z, #loat &ias[ G
gvec* teture Fgsa.ler!D samler, vec! ; Z, #loat &ias[ G
gvec* teture Fgsa.ler'D samler, vec' ; Z, #loat &ias[ G
gvec* teture Fgsa.ler"u9e samler, vec' ; Z, #loat &ias[ G
#loat teture Fsa.ler1D%hadow samler$ vec' ; Z, #loat &ias[ G
#loat teture Fsa.ler!D%hadow samler$ vec' ; Z, #loat &ias[ G
#loat teture Fsa.ler"u9e%hadow samler$ vec* ; Z, #loat &ias[ G
gvec* teture Fgsa.ler1DArra& samler$ vec! ; Z, #loat &ias[ G
gvec* teture Fgsa.ler!DArra& samler, vec' ; Z, #loat &ias[ G
gvec* teture Fgsa.ler"u9eArra& samler, vec* ; Z, #loat &ias[ G
#loat teture Fsa.ler1DArra&%hadow samler$ vec' ;
Z, #loat &ias[ G
#loat teture Fsa.ler!DArra&%hadow samler$ vec* ;G
gvec* teture Fgsa.ler!DRect samler, vec! ;G
#loat teture Fsa.ler!DRect%hadow samler, vec' ;G
#loat teture Fgsa.ler"u9eArra&%hadow samler, vec* ;,
#loat comareG
6se the te2ture coordinate ; to
do a te2ture looku. in the
te2ture currentl& 9ound to
samler.
4or shadow #ors: Ahen
comare is .resent, it is used as
Dre( and the arra& la&er coes
#ro ;./. Ahen comare is not
.resent, the last co.onent o#
; is used as Dre( and the arra&
la&er coes #ro the second to
last co.onent o# ;. F-he
second co.onent o# ; is
unused #or 1. shadow looku.s.G
4or non8shadow #ors: the arra&
la&er coes #ro the last
co.onent o# ;.
11!
Bui%t+in ,unctions
S!nta, +escription
gvec* teture6roP Fgsa.ler1D samler, vec! ; Z, #loat &ias[ G
gvec* teture6roP Fgsa.ler1D samler, vec* ; Z, #loat &ias[ G
gvec* teture6roP Fgsa.ler!D samler, vec' ; Z, #loat &ias[ G
gvec* teture6roP Fgsa.ler!D samler, vec* ; Z, #loat &ias[ G
gvec* teture6roP Fgsa.ler'D samler, vec* ; Z, #loat &ias[ G
#loat teture6roP Fsa.ler1D%hadow samler$ vec* ;
Z, #loat &ias[ G
#loat teture6roP Fsa.ler!D%hadow samler$ vec* ;
Z, #loat &ias[ G
gvec* teture6roP Fgsa.ler!DRect samler, vec' ;G
gvec* teture6roP Fgsa.ler!DRect samler, vec* ;G
#loat teture6roP Fsa.ler!DRect%hadow samler, vec* ;G
Do a te2ture looku. with
.ro@ection. -he te2ture
coordinates consued #ro ;,
not including the last co.onent
o# ;, are divided 9& the last
co.onent o# ;. -he resulting
'
rd
co.onent o# ; in the
shadow #ors is used as Dre(.
A#ter these values are co.uted,
te2ture looku. .roceeds as in
teture.
gvec* teture"od Fgsa.ler1D samler, #loat ;, #loat lo#G
gvec* teture"od Fgsa.ler!D samler, vec! ;, #loat lo#G
gvec* teture"od Fgsa.ler'D samler, vec' ;, #loat lo#G
gvec* teture"od Fgsa.ler"u9e samler, vec' ;, #loat lo#G
#loat teture"od Fsa.ler1D%hadow samler$ vec' ;, #loat lo#G
#loat teture"od Fsa.ler!D%hadow samler$ vec' ;, #loat lo#G
gvec* teture"od Fgsa.ler1DArra& samler, vec! ;, #loat lo#G
gvec* teture"od Fgsa.ler!DArra& samler, vec' ;, #loat lo#G
#loat teture"od Fsa.ler1DArra&%hadow samler$ vec' ;,
#loat lo#G
gvec* teture"od Fgsa.ler"u9eArra& samler, vec* ;, #loat lo#G
Do a te2ture looku. as in
teture 9ut with e2.licit 1)DL
lo# s.eci#ies J&ase and sets the
.artial derivatives as #ollows.
F%ee section '.$.11 D-e2ture
:ini#icationE and e=uation '.15
in the ).en01 0ra.hics %&ste
%.eci#ication.G
u
%
= (
v
%
= (
/
%
= (
u
'
= (
v
'
= (
/
'
= (
12"
Bui%t+in ,unctions
S!nta, +escription
gvec* tetureOffset Fgsa.ler1D samler$ #loat ;$
int o((set Z, #loat &ias[ G
gvec* tetureOffset Fgsa.ler!D samler, vec! ;,
ivec! o((set Z, #loat &ias[ G
gvec* tetureOffset Fgsa.ler'D samler, vec' ;,
ivec' o((set Z, #loat &ias[ G
gvec* tetureOffset Fgsa.ler!DRect samler, vec! ;,
ivec! o((set G
#loat tetureOffset Fsa.ler!DRect%hadow samler, vec' ;,
ivec! o((set G
#loat tetureOffset Fsa.ler1D%hadow samler$ vec' ;,
int o((set Z, #loat &ias[ G
#loat tetureOffset Fsa.ler!D%hadow samler$ vec' ;,
ivec! o((set Z, #loat &ias[ G
gvec* tetureOffset Fgsa.ler1DArra& samler$ vec! ;$
int o((set Z, #loat &ias[ G
gvec* tetureOffset Fgsa.ler!DArra& samler, vec' ;,
ivec! o((set Z, #loat &ias[ G
#loat tetureOffset Fsa.ler1DArra&%hadow samler$ vec' ;,
int o((set Z, #loat &ias[ G
Do a te2ture looku. as in
teture 9ut with o((set added to
the Fu,v,/G te2el coordinates
9e#ore looking u. each te2el.
-he o##set value ust 9e a
constant e2.ression. A liited
range o# o##set values are
su..ortedL the iniu and
a2iu o##set values are
i.leentation8de.endent and
given 9&
:I7>3R)0RA:>-EYE1>)44%E- and
:AY>3R)0RA:>-EYE1>)44%E-,
res.ectivel&.
7ote that o((set does not a..l&
to the la&er coordinate #or
te2ture arra&s. -his is e2.lained
in detail in %ection '.$.11
D-e2ture :ini#icationE o# the
).en01 0ra.hics %&ste
%.eci#ication, where o((set is

u
$
v
$
/
. 7ote that te2el
o##sets are also not su..orted
#or cu9e a.s.
gvec* teelRetch Fgsa.ler1D samler, int ;, int lo#G
gvec* teelRetch Fgsa.ler!D samler, ivec! ;, int lo#G
gvec* teelRetch Fgsa.ler'D samler, ivec' ;, int lo#G
gvec* teelRetch Fgsa.ler!DRect samler, ivec! ;G
gvec* teelRetch Fgsa.ler1DArra& samler, ivec! ;, int lo#G
gvec* teelRetch Fgsa.ler!DArra& samler, ivec' ;, int lo#G
gvec* teelRetch Fgsa.lerBu##er samler, int ;G
gvec* teelRetch Fgsa.ler!D:% samler, ivec! ;, int samleG
gvec* teelRetch Fgsa.ler!D:%Arra& samler, ivec' ;,
int samleG
6se integer te2ture coordinate ;
to looku. a single te2el #ro
samler. -he arra& la&er coes
#ro the last co.onent o# ; #or
the arra& #ors. -he level8o#8
detail lo# Fi# .resentG is as
descri9ed in sections !.11.$
D%hader E2ecutionE under -e2el
4etches and '.$ D-e2turingE o#
the ).en01 0ra.hics %&ste
%.eci#ication.
121
Bui%t+in ,unctions
S!nta, +escription
gvec* teelRetchOffset Fgsa.ler1D samler, int ;, int lo#,
int o((setG
gvec* teelRetchOffset Fgsa.ler!D samler, ivec! ;, int lo#,
ivec! o((setG
gvec* teelRetchOffset Fgsa.ler'D samler, ivec' ;, int lo#,
ivec' o((setG
gvec* teelRetchOffset Fgsa.ler!DRect samler, ivec! ;,
ivec! o((setG
gvec* teelRetchOffset Fgsa.ler1DArra& samler, ivec! ;, int lo#,
int o((setG
gvec* teelRetchOffset Fgsa.ler!DArra& samler, ivec' ;, int lo#,
ivec! o((setG
4etch a single te2el as in
teelRetch o##set 9& o((set as
descri9ed in tetureOffset.
gvec* teture6roPOffset Fgsa.ler1D samler$ vec! ;$
int o((set Z, #loat &ias[ G
gvec* teture6roPOffset Fgsa.ler1D samler$ vec* ;$
int o((set Z, #loat &ias[ G
gvec* teture6roPOffset Fgsa.ler!D samler, vec' ;,
ivec! o((set Z, #loat &ias[ G
gvec* teture6roPOffset Fgsa.ler!D samler, vec* ;,
ivec! o((set Z, #loat &ias[ G
gvec* teture6roPOffset Fgsa.ler'D samler, vec* ;,
ivec' o((set Z, #loat &ias[ G
gvec* teture6roPOffset Fgsa.ler!DRect samler, vec' ;,
ivec! o((set G
gvec* teture6roPOffset Fgsa.ler!DRect samler, vec* ;,
ivec! o((set G
#loat teture6roPOffset Fsa.ler!DRect%hadow samler, vec* ;,
ivec! o((set G
#loat teture6roPOffset Fsa.ler1D%hadow samler$ vec* ;,
int o((set Z, #loat &ias[ G
#loat teture6roPOffset Fsa.ler!D%hadow samler$ vec* ;,
ivec! o((set Z, #loat &ias[ G
Do a .ro@ective te2ture looku.
as descri9ed in teture6roP
o##set 9& o((set as descri9ed in
tetureOffset.
122
Bui%t+in ,unctions
S!nta, +escription
gvec* teture"odOffset Fgsa.ler1D samler$ #loat ;,
#loat lo#, int o((setG
gvec* teture"odOffset Fgsa.ler!D samler, vec! ;,
#loat lo#, ivec! o((setG
gvec* teture"odOffset Fgsa.ler'D samler, vec' ;,
#loat lo#, ivec' o((setG
#loat teture"odOffset Fsa.ler1D%hadow samler$ vec' ;,
#loat lo#, int o((setG
#loat teture"odOffset Fsa.ler!D%hadow samler$ vec' ;,
#loat lo#, ivec! o((setG
gvec* teture"odOffset Fgsa.ler1DArra& samler$ vec! ;,
#loat lo#, int o((setG
gvec* teture"odOffset Fgsa.ler!DArra& samler, vec' ;,
#loat lo#, ivec! o((setG
#loat teture"odOffset Fsa.ler1DArra&%hadow samler$ vec' ;,
#loat lo#, int o((setG
Do an o##set te2ture looku. with
e2.licit 1)D. %ee teture"od
and tetureOffset.
gvec* teture6roP"od Fgsa.ler1D samler, vec! ;, #loat lo#G
gvec* teture6roP"od Fgsa.ler1D samler, vec* ;, #loat lo#G
gvec* teture6roP"od Fgsa.ler!D samler, vec' ;, #loat lo#G
gvec* teture6roP"od Fgsa.ler!D samler, vec* ;, #loat lo#G
gvec* teture6roP"od Fgsa.ler'D samler, vec* ;, #loat lo#G
#loat teture6roP"od Fsa.ler1D%hadow samler$ vec* ;, #loat lo#G
#loat teture6roP"od Fsa.ler!D%hadow samler$ vec* ;$ #loat lo#G
Do a .ro@ective te2ture looku.
with e2.licit 1)D. %ee
teture6roP and teture"od.
gvec* teture6roP"odOffset Fgsa.ler1D samler$ vec! ;,
#loat lo#, int o((setG
gvec* teture6roP"odOffset Fgsa.ler1D samler$ vec* ;,
#loat lo#, int o((setG
gvec* teture6roP"odOffset Fgsa.ler!D samler, vec' ;,
#loat lo#, ivec! o((setG
gvec* teture6roP"odOffset Fgsa.ler!D samler, vec* ;,
#loat lo#, ivec! o((setG
gvec* teture6roP"odOffset Fgsa.ler'D samler, vec* ;,
#loat lo#, ivec' o((setG
#loat teture6roP"odOffset Fsa.ler1D%hadow samler$ vec* ;,
#loat lo#, int o((setG
#loat teture6roP"odOffset Fsa.ler!D%hadow samler$ vec* ;,
#loat lo#, ivec! o((setG
Do an o##set .ro@ective te2ture
looku. with e2.licit 1)D. %ee
teture6roP, teture"od, and
tetureOffset.
123
Bui%t+in ,unctions
S!nta, +escription
gvec* teture#rad Fgsa.ler1D samler$ #loat ;,
#loat #;#%, #loat #;#'G
gvec* teture#rad Fgsa.ler!D samler$ vec! ;,
vec! #;#%, vec! #;#'G
gvec* teture#rad Fgsa.ler'D samler$ vec' ;,
vec' #;#%, vec' #;#'G
gvec* teture#rad Fgsa.ler"u9e samler, vec' ;,
vec' #;#%, vec' #;#'G
gvec* teture#rad Fgsa.ler!DRect samler, vec! ;,
vec! #;#%, vec! #;#'G
#loat teture#rad Fsa.ler!DRect%hadow samler, vec' ;,
vec! #;#%, vec! #;#'G
#loat teture#rad Fsa.ler1D%hadow samler, vec' ;,
#loat #;#%, #loat #;#'G
#loat teture#rad Fsa.ler!D%hadow samler, vec' ;,
vec! #;#%, vec! #;#'G
#loat teture#rad Fsa.ler"u9e%hadow samler, vec* ;,
vec' #;#%, vec' #;#'G
gvec* teture#rad Fgsa.ler1DArra& samler$ vec! ;,
#loat #;#%, #loat #;#'G
gvec* teture#rad Fgsa.ler!DArra& samler$ vec' ;,
vec! #;#%, vec! #;#'G
#loat teture#rad Fsa.ler1DArra&%hadow samler, vec' ;,
#loat #;#%, #loat #;#'G
#loat teture#rad Fsa.ler!DArra&%hadow samler, vec* ;,
vec! #;#%, vec! #;#'G
gvec* teture#rad Fgsa.ler"u9eArra& samler, vec* ;,
vec' #;#%, vec' #;#'G
Do a te2ture looku. as in
teture 9ut with e2.licit
gradients. -he .artial
derivatives o# ; are with res.ect
to window 2 and window &. %et
s
%
=
{
;
%
#or a 1D te2ture
;.s
%
otherwise
s
'
=
{
;
'
#or a 1D te2ture
;.s
'
otherwise
t
%
=
{
(.( #or a 1D te2ture
;.t
%
otherwise
t
'
=
{
(.( #or a 1D te2ture
;.t
'
otherwise
r
%
=
{
(.( #or 1D or !D
;.
%
cu9e, other
r
'
=
{
(.( #or 1D or !D
;.
'
cu9e, other
4or the cu9e version, the .artial
derivatives o# ; are assued to
9e in the coordinate s&ste used
9e#ore te2ture coordinates are
.ro@ected onto the a..ro.riate
cu9e #ace.
124
Bui%t+in ,unctions
S!nta, +escription
gvec* teture#radOffset Fgsa.ler1D samler$ #loat ;$
#loat #;#%, #loat #;#'$ int o((setG
gvec* teture#radOffset Fgsa.ler!D samler, vec! ;,
vec! #;#%, vec! #;#'$ ivec! o((setG
gvec* teture#radOffset Fgsa.ler'D samler, vec' ;,
vec' #;#%, vec' #;#'$ ivec' o((setG
gvec* teture#radOffset Fgsa.ler!DRect samler, vec! ;,
vec! #;#%, vec! #;#'$ ivec! o((setG
#loat teture#radOffset Fsa.ler!DRect%hadow samler, vec' ;,
vec! #;#%, vec! #;#'$ ivec! o((setG
#loat teture#radOffset Fsa.ler1D%hadow samler$ vec' ;,
#loat #;#%, #loat #;#'$ int o((set G
#loat teture#radOffset Fsa.ler!D%hadow samler$ vec' ;,
vec! #;#%, vec! #;#'$ ivec! o((setG
gvec* teture#radOffset Fgsa.ler1DArra& samler$ vec! ;$
#loat #;#%, #loat #;#'$ int o((setG
gvec* teture#radOffset Fgsa.ler!DArra& samler, vec' ;,
vec! #;#%, vec! #;#'$ ivec! o((setG
#loat teture#radOffset Fsa.ler1DArra&%hadow samler$ vec'
;,
#loat #;#%, #loat #;#'$ int o((setG
#loat teture#radOffset Fsa.ler!DArra&%hadow samler$ vec*
;,
vec! #;#%, vec! #;#'$ ivec! o((setG
Do a te2ture looku. with 9oth
e2.licit gradient and o##set, as
descri9ed in teture#rad and
tetureOffset.
gvec* teture6roP#rad Fgsa.ler1D samler$ vec! ;,
#loat #;#%, #loat #;#'G
gvec* teture6roP#rad Fgsa.ler1D samler$ vec* ;,
#loat #;#%, #loat #;#'G
gvec* teture6roP#rad Fgsa.ler!D samler$ vec' ;,
vec! #;#%, vec! #;#'G
gvec* teture6roP#rad Fgsa.ler!D samler$ vec* ;,
vec! #;#%, vec! #;#'G
gvec* teture6roP#rad Fgsa.ler'D samler$ vec* ;,
vec' #;#%, vec' #;#'G
gvec* teture6roP#rad Fgsa.ler!DRect samler$ vec' ;,
vec! #;#%, vec! #;#'G
gvec* teture6roP#rad Fgsa.ler!DRect samler$ vec* ;,
vec! #;#%, vec! #;#'G
#loat teture6roP#rad Fsa.ler!DRect%hadow samler$ vec* ;,
vec! #;#%, vec! #;#'G
#loat teture6roP#rad Fsa.ler1D%hadow samler, vec* ;,
#loat #;#%, #loat #;#'G
#loat teture6roP#rad Fsa.ler!D%hadow samler, vec* ;,
vec! #;#%, vec! #;#'G
Do a te2ture looku. 9oth
.ro@ectivel&, as descri9ed in
teture6roP, and with e2.licit
gradient as descri9ed in
teture#rad. -he .artial
derivatives #;#% and #;#' are
assued to 9e alread& .ro@ected.
125
Bui%t+in ,unctions
S!nta, +escription
gvec* teture6roP#radOffset Fgsa.ler1D samler$ vec! ;,
#loat #;#%, #loat #;#', int o((setG
gvec* teture6roP#radOffset Fgsa.ler1D samler$ vec* ;,
#loat #;#%, #loat #;#', int o((setG
gvec* teture6roP#radOffset Fgsa.ler!D samler$ vec' ;,
vec! #;#%, vec! #;#', vec! o((setG
gvec* teture6roP#radOffset Fgsa.ler!D samler$ vec* ;,
vec! #;#%, vec! #;#', vec! o((setG
gvec* teture6roP#radOffset Fgsa.ler!DRect samler, vec' ;,
vec! #;#%, vec! #;#'$ ivec! o((setG
gvec* teture6roP#radOffset Fgsa.ler!DRect samler, vec* ;,
vec! #;#%, vec! #;#'$ ivec! o((setG
#loat teture6roP#radOffset Fsa.ler!DRect%hadow samler,
vec* ;,
vec! #;#%, vec! #;#'$ ivec! o((setG
gvec* teture6roP#radOffset Fgsa.ler'D samler$ vec* ;,
vec' #;#%, vec' #;#', vec' o((setG
#loat teture6roP#radOffset Fsa.ler1D%hadow samler, vec* ;,
#loat #;#%, #loat #;#', int o((setG
#loat teture6roP#radOffset Fsa.ler!D%hadow samler, vec* ;,
vec! #;#%, vec! #;#', vec! o((setG
Do a te2ture looku. .ro@ectivel&
and with e2.licit gradient as
descri9ed in teture6roP#rad,
as well as with o##set, as
descri9ed in tetureOffset.
.9.# Te,ture Gather Instructions
-he te2ture gather #unctions take co.onents o# a single #loating8.oint vector o.erand as a te2ture
coordinate, deterine a set o# #our te2els to sa.le #ro the 9ase level o# detail o# the s.eci#ied te2ture
iage, and return one co.onent #ro each te2el in a #our8co.onent result vector.
Ahen .er#oring a te2ture gather o.eration, the ini#ication and agni#ication #ilters are ignored, and
the rules #or 1I7EAR #iltering in the ).en01 %.eci#ication are a..lied to the 9ase level o# the te2ture
iage to identi#& the #our te2els i091$ i191$ i190$ and i090. -he te2els are then converted to te2ture source
colors FR
s
, !
s
, :
s
, @
s
G according to -a9le '.!(, #ollowed 9& a..lication o# the te2ture swi??le as descri9ed
in %ection '.;.! D%hader E2ecutionE o# the ).en01 0ra.hics %&ste %.eci#ication. A #our8co.onent
vector is asse9led 9& taking the selected co.onent #ro each o# the swi??led te2ture source colors in
the order Fi091$ i191$ i190$ i090G.
4or te2ture gather #unctions using a shadow sa.ler t&.e, each o# the #our te2el looku.s .er#ors a de.th
co.arison against the de.th re#erence value .assed in Fre(LG, and returns the result o# that co.arison in
the a..ro.riate co.onent o# the result vector.
As with other te2ture looku. #unctions, the results o# a te2ture gather are unde#ined #or shadow sa.lers i#
the te2ture re#erenced is not a de.th te2ture or has de.th co.arisons disa9ledL or #or non8shadow
sa.lers i# the te2ture re#erenced is a de.th te2ture with de.th co.arisons ena9led.
126
Bui%t+in ,unctions
S!nta, +escription
gvec* teture#atherFgsa.ler!D samler, vec! ;
Z, int com[G
gvec* teture#atherFgsa.ler!DArra& samler,
vec' ; Z, int com[G
gvec* teture#atherFgsa.ler"u9e samler,
vec' ; Z, int com[G
gvec* teture#atherFgsa.ler"u9eArra& samler,
vec* ;Z, int com[G
gvec* teture#atherFgsa.ler!DRect samler,
vec' ;Z, int com[G
vec* teture#atherFsa.ler!D%hadow samler,
vec! ;, #loat re(LG
vec* teture#atherFsa.ler!DArra&%hadow samler,
vec' ;, #loat re(LG
vec* teture#atherFsa.ler"u9e%hadow samler,
vec' ;, #loat re(LG
vec* teture#atherF
sa.ler"u9eArra&%hadow samler,
vec* ;, #loat re(LGL
vec* teture#atherFsa.ler!DRect%hadow samler,
vec! ;, #loat re(LGL
Returns the value
vec*F%a.le>i(>@1F;, 9aseG.com,
%a.le>i1>@1F;, 9aseG.com,
%a.le>i1>@(F;, 9aseG.com,
%a.le>i(>@(F;, 9aseG.comG
I# s.eci#ied, the value o# com ust 9e a
constant integer e2.ression with a value o# (,
1, !, or ', identi#&ing the %, ', 0, or /
co.onent o# the #our8co.onent vector
looku. result #or each te2el, res.ectivel&. I#
com is not s.eci#ied, it is treated as (,
selecting the % co.onent o# each te2el to
generate the result.
gvec* teture#atherOffsetFgsa.ler!D samler,
vec! ;, ivec! o((set
Z, int com[G
gvec* teture#atherOffsetFgsa.ler!DArra& samler,
vec' ;, ivec! o((set
Z, int com[G
gvec* teture#atherOffsetFgsa.ler!DRect samler$
vec' ;, ivec! o((set
Z, int com[G
vec* teture#atherOffsetF
sa.ler!D%hadow samler,
vec! ;, #loat re(L, ivec! o((setG
vec* teture#atherOffsetF
sa.ler!DArra&%hadow samler,
vec' ;, #loat re(L, ivec! o((setG
vec* teture#atherOffsetF
sa.ler!DRect%hadow samler,
vec! ;, #loat re(L, ivec! o((setG
3er#or a te2ture gather o.eration as in
teture#ather 9& o((set as descri9ed in
tetureOffset e2ce.t that the
i.leentation8de.endent iniu and
a2iu o##set values are given 9&
:I7>3R)0RA:>-EY-6RE>0A-+ER>)44%E-
and
:AY>3R)0RA:>-EY-6RE>0A-+ER>)44%E-,
res.ectivel&.
127
Bui%t+in ,unctions
S!nta, +escription
gvec* teture#atherOffsetsF
gsa.ler!D samler,
vec! ;, ivec! o((setZ*[
Z, int com[G
gvec* teture#atherOffsetsF
gsa.ler!DArra& samler,
vec' ;, ivec! o((setZ*[
Z, int com[G
gvec* teture#atherOffsetsF
gsa.ler!DRect samler$
vec' ;, ivec! o((setZ*[
Z, int com[G
vec* teture#atherOffsetsF
sa.ler!D%hadow samler,
vec! ;, #loat re(L, ivec! o((setZ*[G
vec* teture#atherOffsetsF
sa.ler!DArra&%hadow samler,
vec' ;, #loat re(L, ivec! o((setZ*[G
vec* teture#atherOffsetsF
sa.ler!DRect%hadow samler,
vec! ;, #loat re(L, ivec! o((setZ*[G
).erate identicall& to teture#atherOffset
e2ce.t that o((sets is used to deterine the
location o# the #our te2els to sa.le. Each
o# the #our te2els is o9tained 9& a..l&ing the
corres.onding o##set in o((sets as a Fu$ vG
coordinate o##set to ;, identi#&ing the #our8
te2el 1I7EAR #oot.rint, and then selecting
the te2el i090 o# that #oot.rint. -he s.eci#ied
values in o((sets ust 9e set with constant
integral e2.ressions.
12
Bui%t+in ,unctions
.9.& The follo%ing te,ture functions are deprecated.
S!nta, <deprecated= +escription <deprecated=
vec* teture1. Fsa.ler1D samler$
#loat coor# Z, #loat &ias[ G
vec* teture1.6roP Fsa.ler1D samler,
vec! coor# Z, #loat &ias[ G
vec* teture1.6roP Fsa.ler1D samler,
vec* coor# Z, #loat &ias[ G
vec* teture1."od Fsa.ler1D samler,
#loat coor#, #loat lo#G
vec* teture1.6roP"od Fsa.ler1D samler,
vec! coor#, #loat lo#G
vec* teture1.6roP"od Fsa.ler1D samler,
vec* coor#, #loat lo#G
De.recated. %ee corres.onding signature
a9ove without D1DE in the nae.
vec* teture*. Fsa.ler!D samler,
vec! coor# Z, #loat &ias[ G
vec* teture*.6roP Fsa.ler!D samler,
vec' coor# Z, #loat &ias[ G
vec* teture*.6roP Fsa.ler!D samler,
vec* coor# Z, #loat &ias[ G
vec* teture*."od Fsa.ler!D samler,
vec! coor#, #loat lo#G
vec* teture*.6roP"od Fsa.ler!D samler,
vec' coor#, #loat lo#G
vec* teture*.6roP"od Fsa.ler!D samler,
vec* coor#, #loat lo#G
De.recated. %ee corres.onding signature
a9ove without D!DE in the nae.
vec* teture/. Fsa.ler'D samler,
vec' coor# Z, #loat &ias[ G
vec* teture/.6roP Fsa.ler'D samler,
vec* coor# Z, #loat &ias[ G
vec* teture/."od Fsa.ler'D samler,
vec' coor#, #loat lo#G
vec* teture/.6roP"od Fsa.ler'D samler,
vec* coor#, #loat lo#G
De.recated. %ee corres.onding signature
a9ove without D'DE in the nae.
6se the te2ture coordinate coor# to do a
te2ture looku. in the 'D te2ture currentl&
9ound to samler. 4or the .ro@ective
FD6roPEG versions, the te2ture coordinate is
divided 9& coor#.1.
vec* tetureCube Fsa.ler"u9e samler,
vec' coor# Z, #loat &ias[ G
vec* tetureCube"od Fsa.ler"u9e samler,
vec' coor#, #loat lo#G
De.recated. %ee corres.onding signature
a9ove without D"u9eE in the nae.
12!
Bui%t+in ,unctions
S!nta, <deprecated= +escription <deprecated=
vec* shadow1. Fsa.ler1D%hadow samler$
vec' coor# Z, #loat &ias[ G
vec* shadow*. Fsa.ler!D%hadow samler$
vec' coor# Z, #loat &ias[ G
vec* shadow1.6roP Fsa.ler1D%hadow samler$
vec* coor# Z, #loat &ias[ G
vec* shadow*.6roP Fsa.ler!D%hadow samler$
vec* coor# Z, #loat &ias[ G
vec* shadow1."od Fsa.ler1D%hadow samler$
vec' coor#, #loat lo#G
vec* shadow*."od Fsa.ler!D%hadow samler$
vec' coor#, #loat lo#G
vec* shadow1.6roP"odFsa.ler1D%hadow samler$
vec* coor#, #loat lo#G
vec* shadow*.6roP"odFsa.ler!D%hadow samler$
vec* coor#$ #loat lo#G
De.recated. %ae #unctionalit& as the
Dteture] 9ased naes a9ove with the sae
signature.
13"
Bui%t+in ,unctions
.1$ .ragment -rocessing .unctions
4ragent .rocessing #unctions are onl& availa9le in #ragent shaders.
.1$.1 +erivative .unctions
Derivatives a& 9e co.utationall& e2.ensive andOor nuericall& unsta9le. -here#ore, an ).en01
i.leentation a& a..ro2iate the true derivatives 9& using a #ast 9ut not entirel& accurate derivative
co.utation. Derivatives are unde#ined within non8uni#or control #low.
-he e2.ected 9ehavior o# a derivative is s.eci#ied using #orwardO9ackward di##erencing.
4orward di##erencing:
- %#%- % ~#-#% %#% 1a
#-#% % ~
- %#%- %
#%
19
Backward di##erencing:
- %#%- % ~#-#% %#% !a
#-#% % ~
- %- %#%
#%
!9
Aith single8sa.le rasteri?ation, #% QS 1.( in e=uations 19 and !9. 4or ulti8sa.le rasteri?ation, #% Q
!.( in e=uations 19 and !9.
dRd0 is a..ro2iated siilarl&, with ' re.lacing %.
A 01 i.leentation a& use the a9ove or other ethods to .er#or the calculation, su9@ect to the
#ollowing conditions:
1. -he ethod a& use .iecewise linear a..ro2iations. %uch linear a..ro2iations i.l& that higher
order derivatives, dRdFdRdF%GG and a9ove, are unde#ined.
!. -he ethod a& assue that the #unction evaluated is continuous. -here#ore derivatives within non8
uni#or control #low are unde#ined.
'. -he ethod a& di##er .er #ragent, su9@ect to the constraint that the ethod a& var& 9& window
coordinates, not screen coordinates. -he invariance re=uireent descri9ed in section '.! DInvarianceE
o# the ).en01 0ra.hics %&ste %.eci#ication, is rela2ed #or derivative calculations, 9ecause the
ethod a& 9e a #unction o# #ragent location.
)ther .ro.erties that are desira9le, 9ut not re=uired, are:
*. 4unctions should 9e evaluated within the interior o# a .riitive Finter.olated, not e2tra.olatedG.
,. 4unctions #or dRd should 9e evaluated while holding & constant. 4unctions #or dRd0 should 9e
evaluated while holding 2 constant. +owever, i2ed higher order derivatives, like dRdFdRd0F'GG
and dRd0FdRdF%GG are unde#ined.
131
Bui%t+in ,unctions
/. Derivatives o# constant arguents should 9e (.
In soe i.leentations, var&ing degrees o# derivative accurac& a& 9e o9tained 9& .roviding 01 hints
Fsection ,.* D+intsE o# the ).en01 0ra.hics %&ste %.eci#icationG, allowing a user to ake an iage
=ualit& versus s.eed trade o##.
S!nta, +escription
gen-&.e dRd Fgen-&.e G Returns the derivative in 2 using local di##erencing #or
the in.ut arguent .
gen-&.e dRd0 Fgen-&.e G Returns the derivative in & using local di##erencing #or
the in.ut arguent .
-hese two #unctions are coonl& used to estiate the
#ilter width used to anti8alias .rocedural te2tures. Ae
are assuing that the e2.ression is 9eing evaluated in
.arallel on a %I:D arra& so that at an& given .oint in
tie the value o# the #unction is known at the grid .oints
re.resented 9& the %I:D arra&. 1ocal di##erencing
9etween %I:D arra& eleents can there#ore 9e used to
derive d4d2, d4d&, etc.
gen-&.e fwidth Fgen-&.e G Returns the su o# the a9solute derivative in 2 and &
using local di##erencing #or the in.ut arguent , i.e.,
abs FdRd FGG J abs FdRd0 FGGL
.1$.2 Interpolation .unctions
Built8in inter.olation #unctions are availa9le to co.ute an inter.olated value o# a #ragent shader in.ut
varia9le at a shader8s.eci#ied F%, 'G location. A se.arate F%, 'G location a& 9e used #or each invocation o#
the 9uilt8in #unction, and those locations a& di##er #ro the de#ault F%, 'G location used to .roduce the
de#ault value o# the in.ut.
4or all o# the inter.olation #unctions, interolant ust 9e an in.ut varia9le or an eleent o# an in.ut
varia9le declared as an arra&. "o.onent selection o.erators Fe.g., .%'G a& not 9e used when s.eci#&ing
interolant. I# interolant is declared with a flat or centroid =uali#ier, the =uali#ier will have no e##ect on
the inter.olated value. I# interolant is declared with the noperspective =uali#ier, the inter.olated value
will 9e co.uted without .ers.ective correction.
132
Bui%t+in ,unctions
S!nta, +escription
#loat interpolateAtCentroid F#loat interolantG
vec! interpolateAtCentroid Fvec! interolantG
vec' interpolateAtCentroid Fvec' interolantG
vec* interpolateAtCentroid Fvec* interolantG
Return the value o# the in.ut var&ing interolant
sa.led at a location inside the 9oth the .i2el and the
.riitive 9eing .rocessed. -he value o9tained would
9e the sae value assigned to the in.ut varia9le i#
declared with the centroid =uali#ier.
#loat interpolateAtSample F#loat interolant,
int samleG
vec! interpolateAtSample Fvec! interolant,
int samleG
vec' interpolateAtSample Fvec' interolant,
int samleG
vec* interpolateAtSample Fvec* interolant,
int samleG
Return the value o# the in.ut var&ing interolant at
the location o# sa.le nu9er samle. I#
ultisa.le 9u##ers are not availa9le, the in.ut
var&ing will 9e evaluated at the center o# the .i2el. I#
sa.le samle does not e2ist, the .osition used to
inter.olate the in.ut var&ing is unde#ined.
#loat interpolateAtOffset F#loat interolant,
vec! o##setG
vec! interpolateAtOffset Fvec! interolant,
vec! o##setG
vec' interpolateAtOffset Fvec' interolant,
vec! o##setG
vec* interpolateAtOffset Fvec* interolant,
vec! o##setG
Return the value o# the in.ut var&ing interolant
sa.led an o##set #ro the center o# the .i2el
s.eci#iced 9& o((set. -he two #loating8.oint
co.onents o# o((set, give the o##set in .i2els in the %
and ' directions, res.ectivel&. An o##set o# F(, (G
identi#ies the center o# the .i2el. -he range and
granularit& o# o##sets su..orted 9& this #unction is
i.leentation8de.endent.
.11 6oise .unctions
7oise #unctions are availa9le to #ragent, geoetr&, and verte2 shaders. -he& are stochastic #unctions that
can 9e used to increase visual co.le2it&. Values returned 9& the #ollowing noise #unctions give the
a..earance o# randoness, 9ut are not trul& rando. -he noise #unctions 9elow are de#ined to have the
#ollowing characteristics:
-he return valueFsG are alwa&s in the range Z81.(,1.([, and cover at least the range Z8(./, (./[, with a
0aussian8like distri9ution.
-he return valueFsG have an overall average o# (.(
-he& are re.eata9le, in that a .articular in.ut value will alwa&s .roduce the sae return value
-he& are statisticall& invariant under rotation Fi.e., no atter how the doain is rotated, it has the sae
statistical characterG
-he& have a statistical invariance under translation Fi.e., no atter how the doain is translated, it has
the sae statistical characterG
-he& t&.icall& give di##erent results under translation.
133
Bui%t+in ,unctions
-he s.atial #re=uenc& is narrowl& concentrated, centered soewhere 9etween (., to 1.(.
-he& are "
1
continuous ever&where Fi.e., the #irst derivative is continuousG
S!nta, +escription
#loat noise1 Fgen-&.e %G Returns a 1D noise value 9ased on the in.ut value %.
vec! noise* Fgen-&.e %G Returns a !D noise value 9ased on the in.ut value %.
vec' noise/ Fgen-&.e %G Returns a 'D noise value 9ased on the in.ut value %.
vec* noise, Fgen-&.e %G Returns a *D noise value 9ased on the in.ut value %.
134
Bui%t+in ,unctions
.12 Geometr! Shader .unctions
-hese #unctions are onl& availa9le in geoetr& shaders. -he& are descri9ed in ore de.th #ollowing the
ta9le.
S!nta, +escription
void 2mitStream5erte Fint streamG Eit the current values o# out.ut varia9les to the current
out.ut .riitive on strea stream. -he arguent to stream
ust 9e a constant integral e2.ression. )n return #ro this
call, the values o# all out.ut varia9les are unde#ined.
"an onl& 9e used i# ulti.le out.ut streas are su..orted.
void 2ndStream6rimitive Fint streamG "o.letes the current out.ut .riitive on strea stream and
starts a new one. -he arguent to stream ust 9e a constant
integral e2.ression. 7o verte2 is eitted.
"an onl& 9e used i# ulti.le out.ut streas are su..orted.
void 2mit5erte FG Eit the current values o# out.ut varia9les to the current
out.ut .riitive. )n return #ro this call, the values o#
out.ut varia9les are unde#ined.
Ahen ulti.le out.ut streas are su..orted, this is
e=uivalent to calling 2mitStream5erteF(G.
void 2nd6rimitive FG "o.letes the current out.ut .riitive and starts a new one.
7o verte2 is eitted.
Ahen ulti.le out.ut streas are su..orted, this is
e=uivalent to calling 2ndStream6rimitiveF(G.
-he #unction 2mitStream5erteFG s.eci#ies that a verte2 is co.leted. A verte2 is added to the current
out.ut .riitive in verte2 strea stream using the current values o# all out.ut varia9les associated with
stream. -he values o# all out.ut varia9les #or all out.ut streas are unde#ined a#ter a call to
2mitStream5erteFG. I# a geoetr& shader invocation has eitted ore vertices than .eritted 9& the
out.ut la&out =uali#ier ma$vertices, the results o# calling 2mitStream5erteFG are unde#ined.
-he #unction 2ndStream6rimitiveFG s.eci#ies that the current out.ut .riitive #or verte2 strea stream is
co.leted and a new out.ut .riitive Fo# the sae t&.eG will started 9& an& su9se=uent
2mitStream5erteFG. -his #unction does not eit a verte2. I# the out.ut la&out is declared to 9e
D.ointsE, calling 2ndStream6rimitiveFG is o.tional.
A geoetr& shader starts with an out.ut .riitive containing no vertices #or each strea. Ahen a
geoetr& shader terinates, the current out.ut .riitive #or each strea is autoaticall& co.leted. It is
not necessar& to call 2ndStream6rimitiveFG i# the geoetr& shader writes onl& a single .riitive.
135
Bui%t+in ,unctions
:ulti.le out.ut streas are su..orted onl& i# the out.ut .riitive t&.e is declared to 9e oints. A
.rogra will #ail to link i# it contains a geoetr& shader calling 2mitStream5erteFG or
2ndStream6rimitiveFG i# its in.ut .riitive t&.e is not oints.
136
Bui%t+in ,unctions
.1# Shader Invocation Control .unctions
-he shader invocation control #unction is availa9le onl& in tessellation control shaders. It is used to
control the relative e2ecution order o# ulti.le shader invocations used to .rocess a .atch, which are
otherwise e2ecuted with an unde#ined relative order.
S!nta, +escription
void barrier FG 4or an& given static instance o# barrierFG, all tessellation control shader
invocations #or a single in.ut .atch ust enter it 9e#ore an& will 9e
allowed to continue 9e&ond it.
-he #unction barrierFG .rovides a .artiall& de#ined order o# e2ecution 9etween shader invocations. -his
ensures that values written 9& one invocation .rior to a given static instance o# barrierFG can 9e sa#el&
read 9& other invocations a#ter their call to the sae static instance barrierFG. Because invocations a&
e2ecute in unde#ined order 9etween these 9arrier calls, the values o# a .er8verte2 or .er8.atch out.ut
varia9le will 9e unde#ined in a nu9er o# cases enuerated in section *.'./ D)ut.utsE.
-he barrierFG #unction a& onl& 9e .laced inside the #unction mainFG o# the tessellation control shader
and a& not 9e called within an& control #low. Barriers are also disallowed a#ter a return stateent in the
#unction mainFG.
137
9 Shading Language Grammar
-BD: su9routines
-he graar is #ed #ro the out.ut o# le2ical anal&sis. -he tokens returned #ro le2ical anal&sis are
>TTRI6PTE EONST 6OOL FLO>T OP6LE INT PINT
6RE>U EONTINPE O ELSE FOR IF ISE>R RETPRN SVITEI E>SE EF>PLT SP6ROPTINE
6VEE9 6VEE; 6VEE8 IVEE9 IVEE; IVEE8 PVEE9 PVEE; PVEE8 VEE9 VEE; VEE8
?>T9 ?>T; ?>T8 EENTROI IN OPT INOPT PNIFOR? V>RQIN! @>TEI S>?@LE
VEE9 VEE; VEE8 ?>T9 ?>T; ?>T8
NO@ERS@EETIVE FL>T S?OOTI L>QOPT
?>T9O9 ?>T9O; ?>T9O8
?>T;O9 ?>T;O; ?>T;O8
?>T8O9 ?>T8O; ?>T8O8
?>T9O9 ?>T9O; ?>T9O8
?>T;O9 ?>T;O; ?>T;O8
?>T8O9 ?>T8O; ?>T8O8
S>?@LER' S>?@LER9 S>?@LER; S>?@LEREP6E S>?@LER'SI>OV S>?@LER9SI>OV
S>?@LEREP6ESI>OV S>?@LER'>RR>Q S>?@LER9>RR>Q S>?@LER'>RR>QSI>OV
S>?@LER9>RR>QSI>OV IS>?@LER' IS>?@LER9 IS>?@LER; IS>?@LEREP6E
IS>?@LER'>RR>Q IS>?@LER9>RR>Q PS>?@LER' PS>?@LER9 PS>?@LER;
PS>?@LEREP6E PS>?@LER'>RR>Q PS>?@LER9>RR>Q
S>?@LER9REET S>?@LER9REETSI>OV IS>?@LER9REET PS>?@LER9REET
S>?@LER6PFFER IS>?@LER6PFFER PS>?@LER6PFFER
S>?@LEREP6E>RR>Q S>?@LEREP6E>RR>QSI>OV
IS>?@LEREP6E>RR>Q PS>?@LEREP6E>RR>Q
S>?@LER9?S IS>?@LER9?S PS>?@LER9?S
S>?@LER9?S>rra& IS>?@LER9?S>rra& PS>?@LER9?S>rra&
13
! Shading Language Gra**ar
STRPET VOI VIILE
IENTIFIER TQ@E_N>?E FLO>TEONST>NT INTEONST>NT PINTEONST>NT 6OOLEONST>NT
FIEL_SELEETION
LEFT_O@ RI!IT_O@
INE_O@ EE_O@ LE_O@ !E_O@ ET_O@ NE_O@
>N_O@ OR_O@ OOR_O@ ?PL_>SSI!N IV_>SSI!N >_>SSI!N
?O_>SSI!N LEFT_>SSI!N RI!IT_>SSI!N >N_>SSI!N OOR_>SSI!N OR_>SSI!N
SP6_>SSI!N
LEFT_@>REN RI!IT_@>REN LEFT_6R>EUET RI!IT_6R>EUET LEFT_6R>EE RI!IT_6R>EE OT
EO??> EOLON ETP>L SE?IEOLON 6>N! >SI TILE @LPS ST>R SL>SI @EREENT
LEFT_>N!LE RI!IT_>N!LE VERTIE>L_6>R E>RET >?@ERS>N TPESTION
INV>RI>NT
II!I_@REEISION ?EIP?_@REEISION LOV_@REEISION @REEISION
-he #ollowing descri9es the graar #or the ).en01 %hading 1anguage in ters o# the a9ove tokens.
varia&le)i#enti(ier:
*D,+T*-*,R
rimar')e%ression:
varia&le)i#enti(ier
*+T8O+"T@+T
F*+T8O+"T@+T
-LO@T8O+"T@+T
:OOL8O+"T@+T
L,-T);@R,+ e%ression R*!MT);@R,+
ost(i%)e%ression:
rimar')e%ression
ost(i%)e%ression L,-T):R@8N,T integer)e%ression R*!MT):R@8N,T
(unction)call
ost(i%)e%ression DOT -*,LD)",L,8T*O+
ost(i%)e%ression *+8)O;
ost(i%)e%ression D,8)O;
integer)e%ression:
e%ression
(unction)call:
13!
! Shading Language Gra**ar
(unction)call)or)metho#
(unction)call)or)metho#:
(unction)call)generic
ost(i%)e%ression DOT (unction)call)generic
(unction)call)generic:
(unction)call)hea#er)/ith)arameters R*!MT);@R,+
(unction)call)hea#er)no)arameters R*!MT);@R,+
(unction)call)hea#er)no)arameters:
(unction)call)hea#er VO*D
(unction)call)hea#er
(unction)call)hea#er)/ith)arameters:
(unction)call)hea#er assignment)e%ression
(unction)call)hea#er)/ith)arameters 8O77@ assignment)e%ression
(unction)call)hea#er:
(unction)i#enti(ier L,-T);@R,+
EE !rammar +ote: 8onstructors loo. li.e (unctions$ &ut le%ical anal'sis recogni0e# most o( them as
EE .e'/or#s. The' are no/ recogni0e# through Ot'e)seci(ierP.
(unction)i#enti(ier:
t'e)seci(ier
*D,+T*-*,R
-*,LD)",L,8T*O+
unar')e%ression:
ost(i%)e%ression
*+8)O; unar')e%ression
D,8)O; unar')e%ression
unar')oerator unar')e%ression
EE !rammar +ote: +o tra#itional st'le t'e casts.
unar')oerator:
;LF"
D@"M
14"
! Shading Language Gra**ar
:@+!
T*LD,
EE !rammar +ote: +o KAK or KQK unar' os. ;ointers are not suorte#.
multilicative)e%ression:
unar')e%ression
multilicative)e%ression "T@R unar')e%ression
multilicative)e%ression "L@"M unar')e%ression
multilicative)e%ression ;,R8,+T unar')e%ression
a##itive)e%ression:
multilicative)e%ression
a##itive)e%ression ;LF" multilicative)e%ression
a##itive)e%ression D@"M multilicative)e%ression
shi(t)e%ression:
a##itive)e%ression
shi(t)e%ression L,-T)O; a##itive)e%ression
shi(t)e%ression R*!MT)O; a##itive)e%ression
relational)e%ression:
shi(t)e%ression
relational)e%ression L,-T)@+!L, shi(t)e%ression
relational)e%ression R*!MT)@+!L, shi(t)e%ression
relational)e%ression L,)O; shi(t)e%ression
relational)e%ression !,)O; shi(t)e%ression
e1ualit')e%ression:
relational)e%ression
e1ualit')e%ression ,R)O; relational)e%ression
e1ualit')e%ression +,)O; relational)e%ression
an#)e%ression:
e1ualit')e%ression
an#)e%ression @7;,R"@+D e1ualit')e%ression
e%clusive)or)e%ression:
an#)e%ression
e%clusive)or)e%ression 8@R,T an#)e%ression
141
! Shading Language Gra**ar
inclusive)or)e%ression:
e%clusive)or)e%ression
inclusive)or)e%ression V,RT*8@L):@R e%clusive)or)e%ression
logical)an#)e%ression:
inclusive)or)e%ression
logical)an#)e%ression @+D)O; inclusive)or)e%ression
logical)%or)e%ression:
logical)an#)e%ression
logical)%or)e%ression SOR)O; logical)an#)e%ression
logical)or)e%ression:
logical)%or)e%ression
logical)or)e%ression OR)O; logical)%or)e%ression
con#itional)e%ression:
logical)or)e%ression
logical)or)e%ression RF,"T*O+ e%ression 8OLO+ assignment)e%ression
assignment)e%ression:
con#itional)e%ression
unar')e%ression assignment)oerator assignment)e%ression
assignment)oerator:
,RF@L
7FL)@""*!+
D*V)@""*!+
7OD)@""*!+
@DD)@""*!+
"F:)@""*!+
L,-T)@""*!+
R*!MT)@""*!+
@+D)@""*!+
SOR)@""*!+
OR)@""*!+
e%ression:
assignment)e%ression
142
! Shading Language Gra**ar
e%ression 8O77@ assignment)e%ression
constant)e%ression:
con#itional)e%ression
#eclaration:
(unction)rotot'e ",7*8OLO+
init)#eclarator)list ",7*8OLO+
;R,8*"*O+ recision)1uali(ier t'e)seci(ier)no)rec ",7*8OLO+
t'e)1uali(ier *D,+T*-*,R L,-T):R@8, struct)#eclaration)list R*!MT):R@8, ",7*8OLO+
t'e)1uali(ier *D,+T*-*,R L,-T):R@8, struct)#eclaration)list R*!MT):R@8,
*D,+T*-*,R ",7*8OLO+
t'e)1uali(ier *D,+T*-*,R L,-T):R@8, struct)#eclaration)list R*!MT):R@8,
*D,+T*-*,R L,-T):R@8N,T R*!MT):R@8N,T ",7*8OLO+
t'e)1uali(ier *D,+T*-*,R L,-T):R@8, struct)#eclaration)list R*!MT):R@8,
*D,+T*-*,R L,-T):R@8N,T constant)e%ression R*!MT):R@8N,T ",7*8OLO+
t'e)1uali(ier ",7*8OLO+
(unction)rotot'e:
(unction)#eclarator R*!MT);@R,+
(unction)#eclarator:
(unction)hea#er
(unction)hea#er)/ith)arameters
(unction)hea#er)/ith)arameters:
(unction)hea#er arameter)#eclaration
(unction)hea#er)/ith)arameters 8O77@ arameter)#eclaration
(unction)hea#er:
(ull')seci(ie#)t'e *D,+T*-*,R L,-T);@R,+
arameter)#eclarator:
t'e)seci(ier *D,+T*-*,R
t'e)seci(ier *D,+T*-*,R L,-T):R@8N,T constant)e%ression R*!MT):R@8N,T
arameter)#eclaration:
arameter)t'e)1uali(ier arameter)1uali(ier arameter)#eclarator
arameter)1uali(ier arameter)#eclarator
arameter)t'e)1uali(ier arameter)1uali(ier arameter)t'e)seci(ier
143
! Shading Language Gra**ar
arameter)1uali(ier arameter)t'e)seci(ier
arameter)1uali(ier:
EA emt' AE
*+
OFT
*+OFT
arameter)t'e)seci(ier:
t'e)seci(ier
init)#eclarator)list:
single)#eclaration
init)#eclarator)list 8O77@ *D,+T*-*,R
init)#eclarator)list 8O77@ *D,+T*-*,R L,-T):R@8N,T R*!MT):R@8N,T
init)#eclarator)list 8O77@ *D,+T*-*,R L,-T):R@8N,T constant)e%ression
R*!MT):R@8N,T
init)#eclarator)list 8O77@ *D,+T*-*,R L,-T):R@8N,T
R*!MT):R@8N,T ,RF@L initiali0er
init)#eclarator)list 8O77@ *D,+T*-*,R L,-T):R@8N,T constant)e%ression
R*!MT):R@8N,T ,RF@L initiali0er
init)#eclarator)list 8O77@ *D,+T*-*,R ,RF@L initiali0er
single)#eclaration:
(ull')seci(ie#)t'e
(ull')seci(ie#)t'e *D,+T*-*,R
(ull')seci(ie#)t'e *D,+T*-*,R L,-T):R@8N,T R*!MT):R@8N,T
(ull')seci(ie#)t'e *D,+T*-*,R L,-T):R@8N,T constant)e%ression R*!MT):R@8N,T
(ull')seci(ie#)t'e *D,+T*-*,R L,-T):R@8N,T R*!MT):R@8N,T ,RF@L initiali0er
(ull')seci(ie#)t'e *D,+T*-*,R L,-T):R@8N,T constant)e%ression
R*!MT):R@8N,T ,RF@L initiali0er
(ull')seci(ie#)t'e *D,+T*-*,R ,RF@L initiali0er
*+V@R*@+T *D,+T*-*,R
EE !rammar +ote: +o KenumK$ or Kt'e#e(K.
(ull')seci(ie#)t'e:
t'e)seci(ier
t'e)1uali(ier t'e)seci(ier
144
! Shading Language Gra**ar
invariant)1uali(ier:
*+V@R*@+T
interolation)1uali(ier:
"7OOTM
-L@T
+O;,R";,8T*V,
la'out)1uali(ier:
L@TOFT L,-T);@R,+ la'out)1uali(ier)i#)list R*!MT);@R,+
la'out)1uali(ier)i#)list:
la'out)1uali(ier)i#
la'out)1uali(ier)i#)list 8O77@ la'out)1uali(ier)i#
la'out)1uali(ier)i#:
*D,+T*-*,R
*D,+T*-*,R ,RF@L *+T8O+"T@+T
arameter)t'e)1uali(ier:
8O+"T
t'e)1uali(ier:
storage)1uali(ier
la'out)1uali(ier
la'out)1uali(ier storage)1uali(ier
interolation)1uali(ier storage)1uali(ier
interolation)1uali(ier
invariant)1uali(ier storage)1uali(ier
invariant)1uali(ier interolation)1uali(ier storage)1uali(ier
invariant
storage)1uali(ier:
8O+"T
@TTR*:FT, EE Verte% onl'.
V@RT*+!
8,+TRO*D V@RT*+!
145
! Shading Language Gra**ar
*+
OFT
8,+TRO*D *+
8,+TRO*D OFT
;@T8M *+
;@T8M OFT
"@7;L, *+
"@7;L, OFT
F+*-OR7
t'e)seci(ier:
t'e)seci(ier)no)rec
recision)1uali(ier t'e)seci(ier)no)rec
t'e)seci(ier)no)rec:
t'e)seci(ier)nonarra'
t'e)seci(ier)nonarra' L,-T):R@8N,T R*!MT):R@8N,T
t'e)seci(ier)nonarra' L,-T):R@8N,T constant)e%ression R*!MT):R@8N,T
t'e)seci(ier)nonarra':
VO*D
-LO@T
DOF:L,
*+T
F*+T
:OOL
V,82
V,8?
V,84
DV,82
DV,8?
DV,84
:V,82
:V,8?
:V,84
*V,82
*V,8?
*V,84
146
! Shading Language Gra**ar
FV,82
FV,8?
FV,84
7@T2
7@T?
7@T4
7@T2S2
7@T2S?
7@T2S4
7@T?S2
7@T?S?
7@T?S4
7@T4S2
7@T4S?
7@T4S4
D7@T2
D7@T?
D7@T4
D7@T2S2
D7@T2S?
D7@T2S4
D7@T?S2
D7@T?S?
D7@T?S4
D7@T4S2
D7@T4S?
D7@T4S4
"@7;L,R1D
"@7;L,R2D
"@7;L,R?D
"@7;L,R8F:,
"@7;L,R1D"M@DOU
"@7;L,R2D"M@DOU
"@7;L,R8F:,"M@DOU
"@7;L,R1D@RR@T
"@7;L,R2D@RR@T
"@7;L,R1D@RR@T"M@DOU
147
! Shading Language Gra**ar
"@7;L,R2D@RR@T"M@DOU
"@7;L,R8F:,@RR@T
"@7;L,R8F:,@RR@T"M@DOU
*"@7;L,R1D
*"@7;L,R2D
*"@7;L,R?D
*"@7;L,R8F:,
*"@7;L,R1D@RR@T
*"@7;L,R2D@RR@T
*"@7;L,R8F:,@RR@T
F"@7;L,R1D
F"@7;L,R2D
F"@7;L,R?D
F"@7;L,R8F:,
F"@7;L,R1D@RR@T
F"@7;L,R2D@RR@T
F"@7;L,R8F:,@RR@T
"@7;L,R2DR,8T
"@7;L,R2DR,8T"M@DOU
*"@7;L,R2DR,8T
F"@7;L,R2DR,8T
"@7;L,R:F--,R
*"@7;L,R:F--,R
F"@7;L,R:F--,R
"@7;L,R2D7"
*"@7;L,R2D7"
F"@7;L,R2D7"
"@7;L,R2D7"@rra'
*"@7;L,R2D7"@rra'
F"@7;L,R2D7"@rra'
struct)seci(ier
TT;,)+@7,
recision)1uali(ier:
M*!M);R,8*"*O+
7,D*F7);R,8*"*O+
LOU);R,8*"*O+
struct)seci(ier:
"TRF8T *D,+T*-*,R L,-T):R@8, struct)#eclaration)list R*!MT):R@8,
14
! Shading Language Gra**ar
"TRF8T L,-T):R@8, struct)#eclaration)list R*!MT):R@8,
struct)#eclaration)list:
struct)#eclaration
struct)#eclaration)list struct)#eclaration
struct)#eclaration:
t'e)seci(ier struct)#eclarator)list ",7*8OLO+
t'e)1uali(ier t'e)seci(ier struct)#eclarator)list ",7*8OLO+
struct)#eclarator)list:
struct)#eclarator
struct)#eclarator)list 8O77@ struct)#eclarator
struct)#eclarator:
*D,+T*-*,R
*D,+T*-*,R L,-T):R@8N,T R*!MT):R@8N,T
*D,+T*-*,R L,-T):R@8N,T constant)e%ression R*!MT):R@8N,T
initiali0er:
assignment)e%ression
#eclaration)statement:
#eclaration
statement:
comoun#)statement
simle)statement
EE !rammar +ote: la&ele# statements (or "U*T8M onl'2 KgotoK is not suorte#.
simle)statement:
#eclaration)statement
e%ression)statement
selection)statement
s/itch)statement
case)la&el
iteration)statement
9um)statement
14!
! Shading Language Gra**ar
comoun#)statement:
L,-T):R@8, R*!MT):R@8,
L,-T):R@8, statement)list R*!MT):R@8,
statement)no)ne/)scoe:
comoun#)statement)no)ne/)scoe
simle)statement
comoun#)statement)no)ne/)scoe:
L,-T):R@8, R*!MT):R@8,
L,-T):R@8, statement)list R*!MT):R@8,
statement)list:
statement
statement)list statement
e%ression)statement:
",7*8OLO+
e%ression ",7*8OLO+
selection)statement:
*- L,-T);@R,+ e%ression R*!MT);@R,+ selection)rest)statement
selection)rest)statement:
statement ,L", statement
statement
con#ition:
e%ression
(ull')seci(ie#)t'e *D,+T*-*,R ,RF@L initiali0er
s/itch)statement:
"U*T8M L,-T);@R,+ e%ression R*!MT);@R,+ L,-T):R@8, s/itch)statement)list
R*!MT):R@8,
s/itch)statement)list:
EA nothing AE
statement)list
case)la&el:
8@", e%ression 8OLO+
D,-@FLT 8OLO+
15"
! Shading Language Gra**ar
iteration)statement:
UM*L, L,-T);@R,+ con#ition R*!MT);@R,+ statement)no)ne/)scoe
DO statement UM*L, L,-T);@R,+ e%ression R*!MT);@R,+ ",7*8OLO+
-OR L,-T);@R,+ (or)init)statement (or)rest)statement R*!MT);@R,+
statement)no)ne/)scoe
(or)init)statement:
e%ression)statement
#eclaration)statement
con#itionot:
con#ition
EA emt' AE
(or)rest)statement:
con#itionot ",7*8OLO+
con#itionot ",7*8OLO+ e%ression
9um)statement:
8O+T*+F, ",7*8OLO+
:R,@N ",7*8OLO+
R,TFR+ ",7*8OLO+
R,TFR+ e%ression ",7*8OLO+
D*"8@RD ",7*8OLO+ EE -ragment sha#er onl'.
EE !rammar +ote: +o KgotoK. !otos are not suorte#.
translation)unit:
e%ternal)#eclaration
translation)unit e%ternal)#eclaration
e%ternal)#eclaration:
(unction)#e(inition
#eclaration
(unction)#e(inition:
(unction)rotot'e comoun#)statement)no)ne/)scoe
151

Potrebbero piacerti anche