You are on page 1of 125

The OpenGL

Shading Language
Language Version: 1.50
Document Revision: 11
04-Dec-2009
John Kessenich
Version 1.1 Authors: John Kessenich, Dave Baldwin, Randi Rost
Copyright (c) 2008-2009 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 Acknowledgments................................................................................................................ 1
1. !hanges ...............................................................................................................................
1..1 !hanges "rom revision # o" version 1.$........................................................................
1.. !hanges "rom revision $ o" version 1.$........................................................................
1..% !hanges "rom revision & o" version 1.$........................................................................
1..& 'ummar( o" )unctionalit( di""erences "rom version 1.&..............................................
1.% *verview.............................................................................................................................. %
1.& +rror ,andling......................................................................................................................%
1.$ -(.ogra.hical !onventions................................................................................................. %
1.# De.recation.......................................................................................................................... &
*verview o" *.en/0 'hading....................................................................................................$
.1 Verte1 2rocessor.................................................................................................................. $
. /eometr( 2rocessor............................................................................................................. $
.% )ragment 2rocessor.............................................................................................................. $
% Basics.......................................................................................................................................... 3
%.1 !haracter 'et........................................................................................................................ 3
%. 'ource 'trings...................................................................................................................... 3
%.% 2re.rocessor......................................................................................................................... 4
%.& !omments.......................................................................................................................... 1%
%.$ -okens................................................................................................................................ 1&
%.# Ke(words............................................................................................................................1&
%.3 Identi"iers........................................................................................................................... 1#
%.4 'tatic 5se........................................................................................................................... 1#
& Varia6les and -(.es..................................................................................................................13
&.1 Basic -(.es........................................................................................................................ 13
&.1.1 Void............................................................................................................................ 7
&.1. Booleans..................................................................................................................... 7
&.1.% Integers....................................................................................................................... 7
&.1.& )loats..........................................................................................................................
&.1.$ Vectors........................................................................................................................%
&.1.# 8atrices...................................................................................................................... %
&.1.3 'am.lers..................................................................................................................... %
&.1.4 'tructures.................................................................................................................... &
&.1.9 Arra(s......................................................................................................................... $
&.1.17 Im.licit !onversions................................................................................................ 3
&. 'co.ing...............................................................................................................................3
&.% 'torage :uali"iers...............................................................................................................9
&.%.1 De"ault 'torage :uali"ier............................................................................................%7
&.%. !onstant :uali"ier...................................................................................................... %7
3
&.%.% !onstant +1.ressions................................................................................................. %7
&.%.& In.uts.......................................................................................................................... %1
&.%.$ 5ni"orm...................................................................................................................... %
&.%.# *ut.uts....................................................................................................................... %%
&.%.3 Inter"ace Blocks.......................................................................................................... %&
&.%.4 0a(out :uali"iers........................................................................................................ %3
&.%.4.1 In.ut 0a(out :uali"iers....................................................................................... %3
&.%.4. *ut.ut 0a(out :uali"iers.................................................................................... &7
&.%.4.% 5ni"orm Block 0a(out :uali"iers....................................................................... &7
&.%.9 Inter.olation................................................................................................................&
&.%.9.1 Redeclaring Built;in Inter.olation Varia6les in the !om.ati6ilit( 2ro"ile.........&%
&.& 2arameter :uali"iers...........................................................................................................&%
&.$ 2recision and 2recision :uali"iers..................................................................................... &&
&.$.1 Range and 2recision................................................................................................... &&
&.$. 2recision :uali"iers.................................................................................................... &&
&.$.% De"ault 2recision :uali"iers....................................................................................... &&
&.$.& Availa6le 2recision :uali"iers....................................................................................&$
&.# Variance and the Invariant :uali"ier.................................................................................. &$
&.#.1 -he Invariant :uali"ier............................................................................................... &#
&.#. Invariance o" !onstant +1.ressions........................................................................... &3
&.3 *rder o" :uali"ication........................................................................................................ &3
$ *.erators and +1.ressions........................................................................................................&4
$.1 *.erators............................................................................................................................ &4
$. Arra( *.erations............................................................................................................... &9
$.% )unction !alls.................................................................................................................... &9
$.& !onstructors....................................................................................................................... &9
$.&.1 !onversion and 'calar !onstructors.......................................................................... &9
$.&. Vector and 8atri1 !onstructors................................................................................. $7
$.&.% 'tructure !onstructors................................................................................................ $
$.&.& Arra( !onstructors..................................................................................................... $%
$.$ Vector !om.onents............................................................................................................$%
$.# 8atri1 !om.onents............................................................................................................$$
$.3 'tructure and Arra( *.erations..........................................................................................$$
$.4 Assignments....................................................................................................................... $#
$.9 +1.ressions........................................................................................................................ $3
$.17 Vector and 8atri1 *.erations..........................................................................................#7
# 'tatements and 'tructure...........................................................................................................#
#.1 )unction De"initions...........................................................................................................#%
#.1.1 )unction !alling !onventions.................................................................................... #&
#. 'election............................................................................................................................. ##
#.% Iteration.............................................................................................................................. #3
#.& Jum.s..................................................................................................................................#4
4
3 Built;in Varia6les......................................................................................................................#9
3.1 Verte1 and /eometr( 'hader '.ecial Varia6les................................................................ #9
3.1.1 !om.ati6ilit( 2ro"ile Verte1 and /eometr( 'hader '.ecial Varia6les..................... 31
3. )ragment 'hader '.ecial Varia6les................................................................................... 3
3.% !om.ati6ilit( 2ro"ile Verte1 'hader Built;In In.uts......................................................... 3&
3.& Built;In !onstants.............................................................................................................. 3&
3.&.1 !om.ati6ilit( 2ro"ile Built;In !onstants....................................................................3$
3.$ Built;In 5ni"orm 'tate....................................................................................................... 3$
3.$.1 !om.ati6ilit( 2ro"ile 'tate......................................................................................... 3$
3.# !om.ati6ilit( 2ro"ile Verte1 and )ragment Inter"ace........................................................34
4 Built;in )unctions..................................................................................................................... 41
4.1 Angle and -rigonometr( )unctions....................................................................................4
4. +1.onential )unctions........................................................................................................4%
4.% !ommon )unctions............................................................................................................ 4&
4.& /eometric )unctions.......................................................................................................... 43
4.$ 8atri1 )unctions................................................................................................................ 49
4.# Vector Relational )unctions...............................................................................................97
4.3 -e1ture 0ooku. )unctions................................................................................................. 91
4.4 )ragment 2rocessing )unctions........................................................................................171
4.9 <oise )unctions................................................................................................................17%
4.17 /eometr( 'hader )unctions........................................................................................... 17&
9 'hading 0anguage /rammar...................................................................................................17$
17 Issues.....................................................................................................................................113
5
1 Introduction
-his document s.eci"ies onl( version 1.$7 o" the *.en/0 'hading 0anguage. It re=uires >>V+R'I*<>>
to su6stitute 1$7, and re=uires #version to acce.t onl( 1$7. I" #version is declared with a smaller
num6er, the language acce.ted is a .revious version o" the shading language, which will 6e su..orted
de.ending on the version and t(.e o" conte1t in the *.en/0 A2I. 'ee the *.en/0 /ra.hics '(stem
'.eci"ication, Version %., "or details on what language versions are su..orted.
All *.en/0 /ra.hics '(stem '.eci"ication re"erences in this s.eci"ication are to version %..
1.1 Acknowledgments
-his s.eci"ication is 6ased on the work o" those who contri6uted to .ast versions o" the *.en/0
0anguage '.eci"ication, the *.en/0 +' .7 0anguage '.eci"ication, and the "ollowing contri6utors to
this version:
Ro6 Barris
2ierre Boudier
2at Brown
<ick Burns
!hris Dodd
8ichael /old
<ick ,aemel
James ,el"ert(
Brent Insko
Je"" Juliano
Jon 0eech
Bill 0icea;Kane
Ben?amin 0i.chak
Barthold 0ichten6elt
Bruce 8err(
Daniel Koch
8arc *lano
Ian Romanick
John Rosasco
Dave 'hreiner
Jerem( 'andmel
Ro6ert 'im.son
1
1 Introduction
1.2 Changes
1.2.1 Changes from revision 9 of version 1.
!lari"( that discard control "low e1its the shader.
+1.lain how to redeclare gl_Texoor!"#.
Remove accidental inclusion o" textureGradOffset on samplerCubeSadow.
!lari"( it is generall( an error to redeclare varia6les, including 6uilt;ins.
1.2.2 Changes from revision ! of version 1.
2ut gl>8a1-e1ture5nits and gl>8a1-e1ture!oords into the com.ati6ilit( .ro"ile. It was an
oversight to not have them there in the .revious release.
!hange ARB>com.ati6ilit( e1tension to com.ati6ilit( .ro"ile.
'mall clari"(ing comments o" e1isting s.eci"ication.
Add new minimum ma1imums "or gl>8a1Verte1*ut.ut!om.onents,
gl>8a1/eometr(In.ut!om.onents, gl>8a1/eometr(*ut.ut!om.onents, and
gl>8a1)ragmentIn.ut!om.onents, rather than rel(ing on gl>8a1Var(ing!om.onents. Also,
corrected gl>8a1Var(ing!om.onents to 6e #7 instead o" #&.
1.2." Changes from revision of version 1.
!om.leted "ull u.date to grammar section. -ested s.ec e1am.les against it:
add multi;sam.ler sam.ler t(.es
add unsi@ed arra(s "or 6lock mem6ers
allow .artial =uali"ication o" 6lock mem6ers
add assignments within the la(out =uali"ier id list
allow instances names on 6locks, no arra(, unsi@ed arra(, and arra(
Brought ARB>com.ati6ilit( u. to "ull 1.$ "unctionalit(.
!orrected A2I trans"ormA"eed6ack e1am.le in section &.%.3.
!lari"( that when redeclaring 6uilt;ins Binvariant, "lat, etc.C that 6lock instance names are not used
!lari"( that invariant can 6e used on out.ut varia6les that are then in.ut into a su6se=uent shader
stage and 6oth the in.ut and out.ut declarations o" that varia6le must 6e declared invariant or it is a
link error.
1.2.# Changes from revision # of version 1.
gl>2rimitiveID comes "rom the .rovoking verte1, while gl>0a(er comes "rom some verte1.
Re=uire De1tension E/0>ARB>com.ati6ilit( : ena6leF to use ARB>com.ati6ilit( "eatures.
2
1 Introduction
5.dated grammar "or assignments in la(out =uali"iers and new te1ture ke(words.
0ots o" editorial changes.
1.2. Summar$ of %unctionalit$ differences from version 1.#
!reated .ro"iles. -urned the com.ati6ilit( e1tension into the com.ati6ilit( .ro"ile.
5.date geometr( shader la(outs: the( must 6e declared, telling the s(stem the .rimitive in.ut and
out.ut t(.es and ma1imum num6er o" vertices.
Added geometr( shader constants.
!hanged gl_$ragoor! =uali"iers to use the la!out"# mechanism instead.
8ade version 6e 1$7, 1.$7, etc.
Added geometr( shaders. -his includes targeting la(ers in )B* rendering.
Added gl_%rimitive&D as an in.ut to "ragment shaders.
Added determinantBC 6uilt;in.
Broaden arra( usage to include verte1 shader in.uts Bverte1 inC.
Broaden structure usage to include "ragment and geometr( in.uts and verte1 and geometr( out.uts.
Broadened inter"ace 6locks "rom ?ust uni"orms to in and out inter"aces as well.
Added gl_$ragoor! =uali"iers origin$upper$left, and pixel$center$integer to modi"( the values
returned 6( gl_$ragoor! Band have no a""ect on an( other as.ect o" the .i.eline or languageC.
Removed the a6ilit( to have statements 6e"ore the "irst case in a switch.
Added su..ort "or multi;sam.le te1tures through sampler%&'S and sampler%&'SArra! su..ort
in texel(etcBC and textureSi)eBC.
De.recated gl>8a1Var(ing!om.onentsG
1." Overvie&
-his document descri6es T'e ()en*L +'a!ing Language, version 1.50.
Inde.endent com.ilation units written in this language are called s'a!ers. A )rogram is a com.lete set o"
shaders that are com.iled and linked together. -he aim o" this document is to thoroughl( s.eci"( the
.rogramming language. -he *.en/0 /ra.hics '(stem '.eci"ication will s.eci"( the *.en/0 entr(
.oints used to mani.ulate and communicate with .rograms and shaders.
1.# 'rror (andling
!om.ilers, in general, acce.t .rograms that are ill;"ormed, due to the im.ossi6ilit( o" detecting all ill;
"ormed .rograms. 2orta6ilit( is onl( ensured "or well;"ormed .rograms, which this s.eci"ication
descri6es. !om.ilers are encouraged to detect ill;"ormed .rograms and issue diagnostic messages, 6ut are
not re=uired to do so "or all cases. !om.ilers are re=uired to return messages regarding le1icall(,
grammaticall(, or semanticall( incorrect shaders.
3
1 Introduction
1. T$pographical Conventions
Italic, 6old, and "ont choices have 6een used in this s.eci"ication .rimaril( to im.rove reada6ilit(. !ode
"ragments use a "i1ed width "ont. Identi"iers em6edded in te1t are italici@ed. Ke(words em6edded in te1t
are 6old. *.erators are called 6( their name, "ollowed 6( their s(m6ol in 6old in .arentheses. -he
clari"(ing grammar "ragments in the te1t use 6old "or literals and italics "or non;terminals. -he o""icial
grammar in 'ection 9 E'hading 0anguage /rammarF uses all ca.itals "or terminals and lower case "or
non;terminals.
1.! )eprecation
2revious versions o" the *.en/0 'hading 0anguage de.recated some "eatures. -hese are clearl( called
out in this s.eci"ication as Ede.recatedF. -he( are still .resent in this version o" the language, 6ut are
targeted "or .otential removal in a "uture version o" the shading language. -he *.en/0 A2I has a
"orward com.ati6ilit( mode that will disallow use o" de.recated "eatures. I" com.iling in a mode where
use o" de.recated "eatures is disallowed, their use causes com.ile time errors. 'ee the *.en/0 /ra.hics
'(stem '.eci"ication "or details on what causes de.recated language "eatures to 6e acce.ted or to return
an error.
4
2 Overvie& of OpenGL Shading
-he *.en/0 'hading 0anguage is actuall( several closel( related languages. -hese languages are used
to create shaders "or each o" the .rogramma6le .rocessors contained in the *.en/0 .rocessing .i.eline.
!urrentl(, these .rocessors are the verte1, geometr(, and "ragment .rocessors.
5nless otherwise noted in this .a.er, a language "eature a..lies to all languages, and common usage will
re"er to these languages as a single language. -he s.eci"ic languages will 6e re"erred to 6( the name o"
the .rocessor the( target: verte1, geometr(, or "ragment.
8ost *.en/0 state is not tracked or made availa6le to shaders. -(.icall(, user;de"ined varia6les will 6e
used "or communicating 6etween di""erent stages o" the *.en/0 .i.eline. ,owever, a small amount o"
state is still tracked and automaticall( made availa6le to shaders, and there are a "ew 6uilt;in varia6les "or
inter"aces 6etween di""erent stages o" the *.en/0 .i.eline.
2.1 *erte+ ,rocessor
-he vertex )rocessor is a .rogramma6le unit that o.erates on incoming vertices and their associated data.
!om.ilation units written in the *.en/0 'hading 0anguage to run on this .rocessor are called vertex
s'a!ers. Hhen a com.lete set o" verte1 shaders are com.iled and linked, the( result in a vertex s'a!er
executa-le that runs on the verte1 .rocessor.
-he verte1 .rocessor o.erates on one verte1 at a time. It does not re.lace gra.hics o.erations that re=uire
knowledge o" several vertices at a time.
2.2 Geometr$ ,rocessor
-he geometr. )rocessor is a .rogramma6le unit that o.erates on data "or incoming vertices "or a .rimitive
assem6led a"ter verte1 .rocessing and out.uts a se=uence o" vertices "orming out.ut .rimitives.
!om.ilation units written in the *.en/0 'hading 0anguage to run on this .rocessor are called geometr.
s'a!ers. Hhen a com.lete set o" geometr( shaders are com.iled and linked, the( result in a geometr.
s'a!er executa-le that runs on the geometr( .rocessor.
A single invocation o" the geometr( shader e1ecuta6le on the geometr( .rocessor will o.erate on a
declared in.ut .rimitive with a "i1ed num6er o" vertices. -his single invocation can emit a varia6le
num6er o" vertices that are assem6led into .rimitives o" a declared out.ut .rimitive t(.e and .assed to
su6se=uent .i.eline stages.
2." %ragment ,rocessor
-he /ragment )rocessor is a .rogramma6le unit that o.erates on "ragment values and their associated
data. !om.ilation units written in the *.en/0 'hading 0anguage to run on this .rocessor are called
/ragment s'a!ers. Hhen a com.lete set o" "ragment shaders are com.iled and linked, the( result in a
/ragment s'a!er executa-le that runs on the "ragment .rocessor.
5
2 Overview of OpenGL Shading
A "ragment shader cannot change a "ragmentIs Bx, .C .osition. Access to neigh6oring "ragments is not
allowed. -he values com.uted 6( the "ragment shader are ultimatel( used to u.date "rame6u""er memor(
or te1ture memor(, de.ending on the current *.en/0 state and the *.en/0 command that caused the
"ragments to 6e generated.
6
" -asics
".1 Character Set
-he source character set used "or the *.en/0 shading languages is a su6set o" A'!II. It includes the
"ollowing characters:
-he letters a*), A*+, and the underscore B > C.
-he num6ers -*..
-he s(m6ols .eriod B.C, .lus B/C, dash B*C, slash B0C, asterisk B1C, .ercent B2C, angled 6rackets B3 and
4C, s=uare 6rackets B 5 and 6 C, .arentheses B " and # C, 6races B 7 and 8 C, caret B9C, vertical 6ar B : C,
am.ersand B;C, tilde B<C, e=uals B=C, e1clamation .oint B>C, colon B?C, semicolon B@C, comma B,C, and
=uestion mark BAC.
-he num6er sign B#C "or .re.rocessor use.
Hhite s.ace: the s.ace character, hori@ontal ta6, vertical ta6, "orm "eed, carriage;return, and line;
"eed.
0ines are relevant "or com.iler diagnostic messages and the .re.rocessor. -he( are terminated 6(
carriage;return or line;"eed. I" 6oth are used together, it will count as onl( a single line termination. )or
the remainder o" this document, an( o" these com6inations is sim.l( re"erred to as a new;line. -here is no
line continuation character.
In general, the languageJs use o" this character set is case sensitive.
-here are no character or string data t(.es, so no =uoting characters are included.
-here is no end;o";"ile character.
".2 Source Strings
-he source "or a single shader is an arra( o" strings o" characters "rom the character set. A single shader
is made "rom the concatenation o" these strings. +ach string can contain multi.le lines, se.arated 6( new;
lines. <o new;lines need 6e .resent in a stringG a single line can 6e "ormed "rom multi.le strings. <o
new;lines or other characters are inserted 6( the im.lementation when it concatenates the strings to "orm a
single shader. 8ulti.le shaders can 6e linked together to "orm a single .rogram.
Diagnostic messages returned "rom com.iling a shader must identi"( 6oth the line num6er within a string
and which source string the message a..lies to. 'ource strings are counted se=uentiall( with the "irst
string 6eing string 7. 0ine num6ers are one more than the num6er o" new;lines that have 6een .rocessed.
7
3 Basics
"." ,reprocessor
-here is a .re.rocessor that .rocesses the source strings as .art o" the com.ilation .rocess.
-he com.lete list o" .re.rocessor directives is as "ollows.
#
#define
#undef
#if
#ifdef
#ifndef
#else
#elif
#endif
#error
#pragma
#extension
#version
#line
-he "ollowing o.erators are also availa6le
defined
##
+ach num6er sign B#C can 6e .receded in its line onl( 6( s.aces or hori@ontal ta6s. It ma( also 6e
"ollowed 6( s.aces and hori@ontal ta6s, .receding the directive. +ach directive is terminated 6( a new;
line. 2re.rocessing does not change the num6er or relative location o" new;lines in a source string.
-he num6er sign B#C on a line 6( itsel" is ignored. An( directive not listed a6ove will cause a diagnostic
message and make the im.lementation treat the shader as ill;"ormed.
#define and #undef "unctionalit( are de"ined as is standard "or !KK .re.rocessors "or macro de"initions
6oth with and without macro .arameters.
-he "ollowing .rede"ined macros are availa6le
__LINE__
__FILE__
__VERSION__
__L&01__ will su6stitute a decimal integer constant that is one more than the num6er o" .receding new;
lines in the current source string.
__$&L1__ will su6stitute a decimal integer constant that sa(s which source string num6er is currentl(
6eing .rocessed.

3 Basics
__V1R+&(0__ will su6stitute a decimal integer re"lecting the version num6er o" the *.en/0 shading
language. -he version o" the shading language descri6ed in this document will have __V1R+&(0__
su6stitute the decimal integer 1$7.
All macro names containing two consecutive underscores B $$ C are reserved "or "uture use as .rede"ined
macro names. All macro names .re"i1ed with E/0>F BE/0F "ollowed 6( a single underscoreC are also
reserved.
#if, #ifdef, #ifndef, #else, #elif, and #endif are de"ined to o.erate as is standard "or !KK .re.rocessors.
+1.ressions "ollowing #if and #elif are "urther restricted to e1.ressions o.erating on literal integer
constants, .lus identi"iers consumed 6( the defined o.erator. It is an error to use #if or #elif on
e1.ressions containing unde"ined macro names, other than as arguments to the defined o.erator.
!haracter constants are not su..orted. -he o.erators availa6le are as "ollows.
,recedence Operator class Operators .ssociativit$
1 BhighestC .arenthetical grou.ing B C <A
unar( de"ined
K ; L M
Right to 0e"t
% multi.licative N A O 0e"t to Right
& additive K ; 0e"t to Right
$ 6it;wise shi"t PP QQ 0e"t to Right
# relational P Q PR QR 0e"t to Right
3 e=ualit( RR MR 0e"t to Right
4 6it;wise and S 0e"t to Right
9 6it;wise e1clusive or T 0e"t to Right
17 6it;wise inclusive or U 0e"t to Right
11 logical and SS 0e"t to Right
1 BlowestC logical inclusive or U U 0e"t to Right
-he defined o.erator can 6e used in either o" the "ollowing wa(s:
defined identifier
defined ( identifier )
-wo tokens in a macro can 6e concatenated into one token using the token .asting B##C o.erator, as is
standard "or !KK .re.rocessors. -he result must 6e a valid single token, which will then 6e su6?ect to
macro e1.ansion. -hat is, macro e1.ansion ha..ens onl( a"ter token .asting. -here are no other num6er
sign 6ased o.erators Be.g., no # or #BC, nor is there a si)eof o.erator.
-he semantics o" a..l(ing o.erators to integer literals in the .re.rocessor match those standard in the
!KK .re.rocessor, not those in the *.en/0 'hading 0anguage.
!
3 Basics
2re.rocessor e1.ressions will 6e evaluated according to the 6ehavior o" the host .rocessor, not the
.rocessor targeted 6( the shader.
#error will cause the im.lementation to .ut a diagnostic message into the shader o6?ectJs in"ormation log
Bsee section #.1.17 'hader and 2rogram :ueries in the *.en/0 /ra.hics '(stem '.eci"ication "or how to
access a shader o6?ectJs in"ormation logC. -he message will 6e the tokens "ollowing the #error directive,
u. to the "irst new;line. -he im.lementation must then consider the shader to 6e ill;"ormed.
#pragma allows im.lementation de.endent com.iler control. -okens "ollowing #pragma are not su6?ect
to .re.rocessor macro e1.ansion. I" an im.lementation does not recogni@e the tokens "ollowing
#pragma, then it will ignore that .ragma. -he "ollowing .ragmas are de"ined as .art o" the language.
#pragma ST!L
-he SC&GD .ragma is used to reserve .ragmas "or use 6( "uture revisions o" this language. <o
im.lementation ma( use a .ragma whose "irst token is SC&GD.
#pragma optimi"e#on)
#pragma optimi"e#off)
can 6e used to turn o"" o.timi@ations as an aid in develo.ing and de6ugging shaders. It can onl( 6e used
outside "unction de"initions. B( de"ault, o.timi@ation is turned on "or all shaders. -he de6ug .ragma
#pragma de$ug#on)
#pragma de$ug#off)
can 6e used to ena6le com.iling and annotating a shader with de6ug in"ormation, so that it can 6e used
with a de6ugger. It can onl( 6e used outside "unction de"initions. B( de"ault, de6ug 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 6(
#version number profile
opt
where num-er must 6e a version o" the language, "ollowing the same convention as __V1R+&(0__ a6ove.
-he directive E#version 1E-F is re=uired in an( shader that uses version 1.$7 o" the language. An(
num-er re.resenting a version o" the language a com.iler does not su..ort will cause an error to 6e
generated. Version 1.17 o" the language does not re=uire shaders to include this directive, and shaders that
do not include a #version directive will 6e treated as targeting version 1.17.
'haders declaring version 1.& o" the shading language can 6e linked with shaders declaring version 1.$ in
the same .rogram. 'haders targeting earlier versions o" the shading language cannot 6e linked with
version 1.$ shaders.
1"
3 Basics
I" the o.tional )ro/ile argument is .rovided, it must 6e the name o" an *.en/0 .ro"ile. !urrentl(, there
are two choices:
%ore
%ompati$ilit&
I" no )ro/ile argument is .rovided, the de"ault is core. 5nless otherwise s.eci"ied, this s.eci"ication is
documenting the core .ro"ile, and ever(thing s.eci"ied "or the core .ro"ile is also availa6le in the
com.ati6ilit( .ro"ile. )eatures s.eci"ied as 6elonging s.eci"icall( to the com.ati6ilit( .ro"ile are not
availa6le in the core .ro"ile.
-here is a 6uilt;in macro de"inition "or each .ro"ile the im.lementation su..orts. All im.lementations
.rovide the "ollowing macro:
#define !L_%ore_profile '
Im.lementations .roviding the com.ati6ilit( .ro"ile .rovide the "ollowing macro:
#define !L_%ompati$ilit&_profile '
-he #version directive must occur in a shader 6e"ore an(thing else, e1ce.t "or comments and white s.ace.
11
3 Basics
B( de"ault, com.ilers o" this language must issue com.ile time s(ntactic, grammatical, and semantic
errors "or shaders that do not con"orm to this s.eci"ication. An( e1tended 6ehavior must "irst 6e ena6led.
Directives to control the 6ehavior o" the com.iler with res.ect to e1tensions are declared with the
#extension directive
#extension extension_name : behavior
#extension all : behavior
where extension_name is the name o" an e1tension. +1tension names are not documented in this
s.eci"ication. -he token all means the 6ehavior a..lies to all e1tensions su..orted 6( the com.iler. -he
-e'avior can 6e one o" the "ollowing
behavior 'ffect
re/uire Behave as s.eci"ied 6( the e1tension extension_name.
/ive an error on the #extension i" the e1tension extension_name is not
su..orted, or i" all is s.eci"ied.
enable Behave as s.eci"ied 6( the e1tension extension_name.
Harn on the #extension i" the e1tension extension_name is not su..orted.
/ive an error on the #extension i" all is s.eci"ied.
&arn Behave as s.eci"ied 6( the e1tension extension_name, e1ce.t issue warnings
on an( detecta6le use o" that e1tension, unless such use is su..orted 6( other
ena6led or re=uired e1tensions.
I" all is s.eci"ied, then warn on all detecta6le uses o" an( e1tension used.
Harn on the #extension i" the e1tension extension_name is not su..orted.
disable Behave Bincluding issuing errors and warningsC as i" the e1tension
extension_name is not .art o" the language de"inition.
I" all is s.eci"ied, then 6ehavior must revert 6ack to that o" the non;e1tended
core version o" the language 6eing com.iled to.
Harn on the #extension i" the e1tension extension_name is not su..orted.
-he extension directive is a sim.le, low;level mechanism to set the 6ehavior "or each e1tension. It does
not de"ine .olicies such as which com6inations are a..ro.riate, those must 6e de"ined elsewhere. *rder
o" directives matters in setting the 6ehavior "or each e1tension: Directives that occur later override those
seen earlier. -he all variant sets the 6ehavior "or all e1tensions, overriding all .reviousl( issued
extension directives, 6ut onl( "or the -e'aviors warn and disable.
12
3 Basics
-he initial state o" the com.iler is as i" the directive
#extension all ( disa$le
was issued, telling the com.iler that all error and warning re.orting must 6e done according to this
s.eci"ication, ignoring an( e1tensions.
+ach e1tension can de"ine its allowed granularit( o" sco.e. I" nothing is said, the granularit( is a shader
Bthat is, a single com.ilation unitC, and the e1tension directives must occur 6e"ore an( non;.re.rocessor
tokens. I" necessar(, the linker can en"orce granularities larger than a single com.ilation unit, in which
case each involved shader will have to contain the necessar( e1tension directive.
8acro e1.ansion is not done on lines containing #extension and #version directives.
#line must have, a"ter macro su6stitution, one o" the "ollowing "orms:
#line line
#line line source-string-number
where line and source-string-num-er are constant integer e1.ressions. A"ter .rocessing this directive
Bincluding its new;lineC, the im.lementation will 6ehave as i" it is com.iling at line num6er line21 and
source string num6er source-string-num-er. 'u6se=uent source strings will 6e num6ered se=uentiall(,
until another #line directive overrides that num6ering.
".# Comments
!omments are delimited 6( AN and NA, or 6( AA and a new;line. -he 6egin comment delimiters BAN or AAC are
not recogni@ed as comment delimiters inside o" a comment, hence comments cannot 6e nested. I" a
comment resides entirel( within a single line, it is treated s(ntacticall( as a single s.ace. <ew;lines are
not eliminated 6( comments.
13
3 Basics
". To0ens
-he language is a se=uence o" tokens. A token can 6e
to3en:
3e.4or!
i!enti/ier
integer-constant
/loating-constant
o)erator
@ 7 8
".! 1e$&ords
-he "ollowing are the ke(words in the language, and cannot 6e used "or an( other .ur.ose than that
de"ined 6( this document:
attribute const uniform var!ing
la!out
centroid flat smoot noperspective
break continue do for wile switc case default
if else
in out inout
float int void bool true false
invariant
discard return
mat% matF matG
mat%x% mat%xF mat%xG
matFx% matFxF matFxG
matGx% matGxF matGxG
vec% vecF vecG ivec% ivecF ivecG bvec% bvecF bvecG
uint uvec% uvecF uvecG
lowp mediump igp precision
sampler1& sampler%& samplerF& samplerCube
sampler1&Sadow sampler%&Sadow samplerCubeSadow
sampler1&Arra! sampler%&Arra!
sampler1&Arra!Sadow sampler%&Arra!Sadow
isampler1& isampler%& isamplerF& isamplerCube
isampler1&Arra! isampler%&Arra!
14
3 Basics
usampler1& usampler%& usamplerF& usamplerCube
usampler1&Arra! usampler%&Arra!
sampler%&Hect sampler%&HectSadow isampler%&Hect usampler%&Hect
samplerIuffer isamplerIuffer usamplerIuffer
sampler%&'S isampler%&'S usampler%&'S
sampler%&'SArra! isampler%&'SArra! usampler%&'SArra!
struct
-he "ollowing are the ke(words reserved "or "uture use. 5sing them will result in an error:
common partition active
asm
class union enum t!pedef template tis packed
goto
inline noinline volatile public static extern external interface
long sort double alf fixed unsigned superp
input output
vec% vecF vecG dvec% dvecF dvecG fvec% fvecF fvecG
samplerF&Hect
filter
image1& image%& imageF& imageCube
iimage1& iimage%& iimageF& iimageCube
uimage1& uimage%& uimageF& uimageCube
image1&Arra! image%&Arra!
iimage1&Arra! iimage%&Arra! uimage1&Arra! uimage%&Arra!
image1&Sadow image%&Sadow
image1&Arra!Sadow image%&Arra!Sadow
imageIuffer iimageIuffer uimageIuffer
si)eof cast
namespace using
row$maJor
In addition, all identi"iers containing two consecutive underscores B$$C are reserved as .ossi6le "uture
ke(words.
15
3 Basics
".2 Identifiers
Identi"iers are used "or varia6le names, "unction names, structure names, and "ield selectors B"ield
selectors select com.onents o" vectors and matrices similar to structure "ields, as discussed in 'ection $.$
EVector !om.onentsF and 'ection $.# E8atri1 !om.onentsF C. Identi"iers have the "orm
i!enti/ier
non!igit
i!enti/ier non!igit
i!enti/ier !igit
non!igit: one o"
$ a b c d e f g i J k l m n o p K r s t u v w x ! )
A I C & L ( G M N O P D ' Q O R S H S C T U V W X +
!igit: one o"
- 1 % F G E Y Z [ .
Identi"iers starting with Egl>F are reserved "or use 6( *.en/0, and ma( not 6e declared in a shader as
either a varia6le or a "unction. ,owever, as noted in the s.eci"ication, there are some cases where
.reviousl( declared varia6les can 6e redeclared to change or add some .ro.ert(, and .redeclared Vgl>V
names are allowed to 6e redeclared in a shader. onl( "or these s.eci"ic .ur.oses. 8ore generall(, it is an
error to redeclare a varia6le, including those starting Egl>F.
".3 Static 4se
'ome language rules descri6ed 6elow de.end on whether something is staticall. written or used.
A shader contains a static use o" Bor static assignment toC a varia6le x i", a"ter .re.rocessing, the shader
contains a statement that would read Bor writeC x, whether or not run;time "low o" control will cause that
statement to 6e e1ecuted.
16
# *ariables and T$pes
All varia6les and "unctions must 6e declared 6e"ore 6eing used. Varia6le and "unction names are
identi"iers.
-here are no de"ault t(.es. All varia6le and "unction declarations must have a declared t(.e, and
o.tionall( =uali"iers. A varia6le is declared 6( s.eci"(ing its t(.e "ollowed 6( one or more names
se.arated 6( commas. In man( cases, a varia6le can 6e initiali@ed as .art o" its declaration 6( using the
assignment o.erator B=C. -he grammar near the end o" this document .rovides a "ull re"erence "or the
s(nta1 o" declaring varia6les.
5ser;de"ined t(.es ma( 6e de"ined using struct to aggregate a list o" e1isting t(.es into a single name.
-he *.en/0 'hading 0anguage is t(.e sa"e. -here are no im.licit conversions 6etween t(.es, with the
e1ce.tion that an integer value ma( a..ear where a "loating;.oint t(.e is e1.ected, and 6e converted to a
"loating;.oint value. +1actl( how and when this can occur is descri6ed in 'ection &.1.17 EIm.licit
!onversionsF and as re"erenced 6( other sections in this s.eci"ication.
#.1 -asic T$pes
-he *.en/0 'hading 0anguage su..orts the "ollowing 6asic data t(.es, grou.ed as "ollows.
-rans.arent t(.es
T$pe 5eaning
void "or "unctions that do not return a value
bool a conditional t(.e, taking on values o" true or "alse
int a signed integer
uint an unsigned integer
float a single "loating;.oint scalar
vec% a two;com.onent "loating;.oint vector
vecF a three;com.onent "loating;.oint vector
vecG a "our;com.onent "loating;.oint vector
bvec% a two;com.onent Boolean vector
bvecF a three;com.onent Boolean vector
bvecG a "our;com.onent Boolean vector
ivec% a two;com.onent signed integer vector
ivecF a three;com.onent signed integer vector
ivecG a "our;com.onent signed integer vector
17
4 #aria$%es and &'pes
T$pe 5eaning
uvec% a two;com.onent unsigned integer vector
uvecF a three;com.onent unsigned integer vector
uvecG a "our;com.onent unsigned integer vector
mat% a W "loating;.oint matri1
matF a %W% "loating;.oint matri1
matG a &W& "loating;.oint matri1
mat%x% same as a mat%
mat%xF a "loating;.oint matri1 with columns and % rows
mat%xG a "loating;.oint matri1 with columns and & rows
matFx% a "loating;.oint matri1 with % columns and rows
matFxF same as a matF
matFxG a "loating;.oint matri1 with % columns and & rows
matGx% a "loating;.oint matri1 with & columns and rows
matGxF a "loating;.oint matri1 with & columns and % rows
matGxG same as a matG
)loating 2oint 'am.ler -(.es Bo.a=ueC
T$pe 5eaning
sampler1& a handle "or accessing a 1D te1ture
sampler%& a handle "or accessing a D te1ture
samplerF& a handle "or accessing a %D te1ture
samplerCube a handle "or accessing a cu6e ma..ed te1ture
sampler%&Hect a handle "or accessing a rectangular te1ture
sampler1&Sadow a handle "or accessing a 1D de.th te1ture with com.arison
sampler%&Sadow a handle "or accessing a D de.th te1ture with com.arison
sampler%&HectSadow a handle "or accessing a rectangular te1ture with com.arison
sampler1&Arra! a handle "or accessing a 1D arra( te1ture
sampler%&Arra! a handle "or accessing a D arra( te1ture
sampler1&Arra!Sadow a handle "or accessing a 1D arra( de.th te1ture with com.arison
sampler%&Arra!Sadow a handle "or accessing a D arra( de.th te1ture with com.arison
samplerIuffer a handle "or accessing a 6u""er te1ture
sampler%&'S a handle "or accessing a D multi;sam.le te1ture
1
4 #aria$%es and &'pes
T$pe 5eaning
sampler%&'SArra! a handle "or accessing a D multi;sam.le arra( te1ture
'igned Integer 'am.ler -(.es Bo.a=ueC
T$pe 5eaning
isampler1& a handle "or accessing an integer 1D te1ture
isampler%& a handle "or accessing an integer D te1ture
isamplerF& a handle "or accessing an integer %D te1ture
isamplerCube a handle "or accessing an integer cu6e ma..ed te1ture
isampler%&Hect a handle "or accessing an integer D rectangular te1ture
isampler1&Arra! a handle "or accessing an integer 1D arra( te1ture
isampler%&Arra! a handle "or accessing an integer D arra( te1ture
isamplerIuffer a handle "or accessing an integer 6u""er te1ture
isampler%&'S a handle "or accessing an integer D multi;sam.le te1ture
isampler%&'SArra! a handle "or accessing an integer D multi;sam.le arra( te1ture
5nsigned Integer 'am.ler -(.es Bo.a=ueC
T$pe 5eaning
usampler1& a handle "or accessing an unsigned integer 1D te1ture
usampler%& a handle "or accessing an unsigned integer D te1ture
usamplerF& a handle "or accessing an unsigned integer %D te1ture
usamplerCube a handle "or accessing an unsigned integer cu6e ma..ed te1ture
usampler%&Hect a handle "or accessing an unsigned integer rectangular te1ture
usampler1&Arra! a handle "or accessing an unsigned integer 1D arra( te1ture
usampler%&Arra! a handle "or accessing an unsigned integer D arra( te1ture
usamplerIuffer a handle "or accessing an unsigned integer 6u""er te1ture
usampler%&'S a handle "or accessing an unsigned integer D multi;sam.le te1ture
usampler%&'SArra! a handle "or accessing an unsigned integer D multi;sam.le te1ture
arra(
In addition, a shader can aggregate these using arra(s and structures to 6uild more com.le1 t(.es.
-here are no .ointer t(.es.
1!
4 #aria$%es and &'pes
#.1.1 *oid
)unctions that do not return a value must 6e declared as void. -here is no de"ault "unction return t(.e.
-he ke(word void cannot 6e used in an( other declarations Be1ce.t "or em.t( "ormal or actual .arameter
listsC.
#.1.2 -ooleans
-o make conditional e1ecution o" code easier to e1.ress, the t(.e bool is su..orted. -here is no
e1.ectation that hardware directl( su..orts varia6les o" this t(.e. It is a genuine Boolean t(.e, holding
onl( one o" two values meaning either true or "alse. -wo ke(words true and false can 6e used as literal
Boolean constants. Booleans are declared and o.tionall( initiali@ed as in the "ollow e1am.le:
$ool su%%ess) ** de%lare +su%%ess, to $e a -oolean
$ool done . false) ** de%lare and initiali"e +done,
-he right side o" the assignment o.erator B = C must 6e an e1.ression whose t(.e is bool.
+1.ressions used "or conditional ?um.s Bif, for, A?, wile, do*wileC must evaluate to the t(.e bool.
#.1." Integers
'igned and unsigned integer varia6les are "ull( su..orted. In this document, the term integer is meant to
generall( include 6oth signed and unsigned integers. 5nsigned integers have e1actl( % 6its o" .recision.
'igned integers use % 6its, including a sign 6it, in twoIs com.lement "orm. *.erations resulting in
over"low or under"low will not cause an( e1ce.tion, nor will the( saturate, rather the( will Ewra.F to (ield
the low;order % 6its o" the result.
Integers are declared and o.tionall( initiali@ed with integer e1.ressions, as in the "ollowing e1am.le:
int i/ 0 . 12) ** default integer literal t&pe is int
uint 3 . 4u) ** +u, esta$lis5es t5e t&pe as uint
2"
4 #aria$%es and &'pes
0iteral integer constants can 6e e1.ressed in decimal B6ase 17C, octal B6ase 4C, or he1adecimal B6ase 1#C
as "ollows.
integer-constant :
!ecimal-constant integer-su//ixo)t
octal-constant integer-su//ixo)t
'exa!ecimal-constant integer-su//ixo)t
integer-su//ix: one o"
u T
!ecimal-constant :
non5ero-!igit
!ecimal-constant !igit
octal-constant :
-
octal-constant octal-!igit
'exa!ecimal-constant :
71 'exa!ecimal-!igit
7X 'exa!ecimal-!igit
'exa!ecimal-constant 'exa!ecimal-!igit
!igit :
-
non5ero-!igit
non5ero-!igit : one o"
1 % F G E Y Z [ .
octal-!igit ? one o"
- 1 % F G E Y Z
'exa!ecimal-!igit ? one o"
- 1 % F G E Y Z [ .
a b c d e f
A I C & L (
<o white s.ace is allowed 6etween the digits o" an integer constant, including a"ter the leading - or a"ter
the leading -x or -W o" a constant, or 6e"ore the su""i1 u or T. Hhen the su""i1 u or T is .resent, the
literal has t(.e uint, otherwise the t(.e is int. A leading unar( minus sign B;C is inter.reted as an
arithmetic unar( negation, not as .art o" the constant.
It is an error to .rovide a literal integer whose magnitude is too large to store in a varia6le o" matching
signed or unsigned t(.e.
21
4 #aria$%es and &'pes
#.1.# %loats
)loats are availa6le "or use in a variet( o" scalar calculations. )loating;.oint varia6les are de"ined as in the
"ollowing e1am.le:
float a/ $ . '67)
As an in.ut value to one o" the .rocessing units, a "loating;.oint varia6le is e1.ected to match the I+++
single .recision "loating;.oint de"inition "or .recision and d(namic range. It is not re=uired that the
.recision o" internal .rocessing match the I+++ "loating;.oint s.eci"ication "or "loating;.oint o.erations,
6ut the guidelines "or .recision esta6lished 6( the *.en/0 1.& s.eci"ication must 6e met. 'imilarl(,
treatment o" conditions such as divide 6( 7 ma( lead to an uns.eci"ied result, 6ut in no case should such a
condition lead to the interru.tion or termination o" .rocessing.
)loating;.oint constants are de"ined as "ollows.
/loating-constant :
/ractional-constant ex)onent-)art
o)t
/loating-su//ixo)t

!igit-se6uence ex)onent-)art /loating-su//ixo)t
/ractional-constant :
!igit-se6uence . !igit-se6uence
!igit-se6uence .
. !igit-se6uence
ex)onent-)art :
e sign
o)t
!igit-se6uence
E sign
o)t
!igit-se6uence
sign : one o"
/ \
!igit-se6uence :
!igit
!igit-se6uence !igit
/loating-su//ix: one o"
f (
A decimal .oint B . C is not needed i" the e1.onent .art is .resent. <o white s.ace ma( a..ear an(where
within a "loating;.oint constant, including 6e"ore a su""i1. A leading unar( minus sign B*C is inter.reted as
a unar( o.erator and is not .art o" the "loating;.oint constant
22
4 #aria$%es and &'pes
#.1. *ectors
-he *.en/0 'hading 0anguage includes data t(.es "or generic ;, %;, and &;com.onent vectors o"
"loating;.oint values, integers, or Booleans. )loating;.oint vector varia6les can 6e used to store colors,
normals, .ositions, te1ture coordinates, te1ture looku. results and the like. Boolean vectors can 6e used
"or com.onent;wise com.arisons o" numeric vectors. 'ome e1am.les o" vector declaration are:
ve%2 tex%oord'/ tex%oord2)
ve%4 position)
ve%1 m&R!-8)
ive%2 textureLoo3up)
$ve%4 less)
Initiali@ation o" vectors can 6e done with constructors, which are discussed shortl(.
#.1.! 5atrices
-he *.en/0 'hading 0anguage has 6uilt;in t(.es "or W, W%, W&, %W, %W%, %W&, &W, &W%, and &W&
matrices o" "loating;.oint num6ers. -he "irst num6er in the t(.e is the num6er o" columns, the second is
the num6er o" rows. +1am.le matri1 declarations:
mat2 mat2)
mat4 opt9atrix)
mat1 vie:/ pro0e%tion)
mat1x1 vie:) ** an alternate :a& of de%laring a mat1
mat4x2 m) ** a matrix :it5 4 %olumns and 2 ro:s
Initiali@ation o" matri1 values is done with constructors Bdescri6ed in 'ection $.& E!onstructorsF C in
column;ma?or order.
#.1.2 Samplers
'am.ler t(.es Be.g., sampler%&C are e""ectivel( o.a=ue handles to te1tures and their "ilters. -he( are
used with the 6uilt;in te1ture "unctions Bdescri6ed in 'ection 4.3 E-e1ture 0ooku. )unctionsF C to s.eci"(
which te1ture to access and how it is to 6e "iltered. -he( can onl( 6e declared as "unction .arameters or
uniform varia6les Bsee 'ection &.%.$ E5ni"ormF C. +1ce.t "or arra( inde1ing, structure "ield selection,
and .arentheses, sam.lers are not allowed to 6e o.erands in e1.ressions. 'am.lers aggregated into arra(s
within a shader Busing s=uare 6rackets 5 6C can onl( 6e inde1ed with integral constant e1.ressions Bsee
'ection &.%.% E!onstant +1.ressionsFC. 'am.lers cannot 6e treated as l;valuesG hence cannot 6e used as
out or inout "unction .arameters, nor can the( 6e assigned into. As uni"orms, the( are initiali@ed onl(
with the *.en/0 A2IG the( cannot 6e declared with an initiali@er in a shader. As "unction .arameters,
onl( sam.lers ma( 6e .assed to sam.lers o" matching t(.e. -his ena6les consistenc( checking 6etween
shader te1ture accesses and *.en/0 te1ture state 6e"ore a shader is run.
23
4 #aria$%es and &'pes
#.1.3 Structures
5ser;de"ined t(.es can 6e created 6( aggregating other alread( de"ined t(.es into a structure using the
struct ke(word. )or e1am.le,
stru%t lig5t ;
float intensit&)
ve%4 position)
< lig5tVar)
In this e1am.le, lig't 6ecomes the name o" the new t(.e, and lig'tVar 6ecomes a varia6le o" t(.e lig't.
-o declare varia6les o" the new t(.e, use its name Bwithout the ke(word structC.
lig5t lig5tVar2)
8ore "ormall(, structures are declared as "ollows. ,owever, the com.lete correct grammar is as given in
'ection 9 E'hading 0anguage /rammarF .
struct-!e/inition :
6uali/ier
o)t
struct name
o)t
7 mem-er-list 8 !eclarators
o)t
7
mem-er-list :
mem-er-!eclaration7
mem-er-!eclaration mem-er-list7
mem-er-!eclaration :
-asic-t.)e !eclarators7
where name 6ecomes the user;de"ined t(.e, and can 6e used to declare varia6les to 6e o" this new t(.e.
-he name shares the same name s.ace as other varia6les, t(.es, and "unctions. All .reviousl( visi6le
varia6les, t(.es, constructors, or "unctions with that name are hidden. -he o.tional 6uali/ier onl( a..lies
to an( !eclarators, and is not .art o" the t(.e 6eing de"ined "or name.
'tructures must have at least one mem6er declaration. 8em6er declarators ma( contain .recision
=uali"iers, 6ut ma( not contain an( other =uali"iers. Bit "ields are not su..orted. 8em6er t(.es must 6e
alread( de"ined Bthere are no "orward re"erencesC. 8em6er declarations cannot contain initiali@ers.
8em6er declarators can contain arra(s. 'uch arra(s must have a si@e s.eci"ied, and the si@e must 6e an
integral constant e1.ression thatIs greater than @ero Bsee 'ection &.%.% E!onstant +1.ressionsFC. +ach
level o" structure has its own name s.ace "or names given in mem6er declaratorsG such names need onl(
6e uni=ue within that name s.ace.
24
4 #aria$%es and &'pes
Anon(mous structures are not su..orted. +m6edded structure de"initions are not su..orted.
stru%t S ; float f) <)
stru%t T ;
S) ** Error( anon&mous stru%tures disallo:ed
stru%t ; 666 <) ** Error( em$edded stru%tures disallo:ed
S s) ** O3a&( nested stru%tures :it5 name are allo:ed
<)
'tructures can 6e initiali@ed at declaration time using constructors, as discussed in 'ection $.&.%
E'tructure !onstructorsF .
#.1.9 .rra$s
Varia6les o" the same t(.e can 6e aggregated into arra(s 6( declaring a name "ollowed 6( 6rackets B 5 6 C
enclosing an o.tional si@e. Hhen an arra( si@e is s.eci"ied in a declaration, it must 6e an integral constant
e1.ression Bsee 'ection &.%.% E!onstant +1.ressionsF C greater than @ero. I" an arra( is inde1ed with an
e1.ression that is not an integral constant e1.ression, or i" an arra( is .assed as an argument to a "unction,
then its si@e must 6e declared 6e"ore an( such use. It is legal to declare an arra( without a si@e and then
later re;declare the same name as an arra( o" the same t(.e and s.eci"( a si@e. It is illegal to declare an
arra( with a si@e, and then later Bin the same shaderC inde1 the same arra( with an integral constant
e1.ression greater than or e=ual to the declared si@e. It is also illegal to inde1 an arra( with a negative
constant e1.ression. Arra(s declared as "ormal .arameters in a "unction declaration must s.eci"( a si@e.
5nde"ined 6ehavior results "rom inde1ing an arra( with a non;constant e1.ression thatJs greater than or
e=ual to the arra(Js si@e or less than 7. *nl( one;dimensional arra(s ma( 6e declared. All 6asic t(.es and
structures can 6e "ormed into arra(s. 'ome e1am.les are:
float fre=uen%ies>4?)
uniform ve%1 lig5t@osition>1?)
lig5t lig5ts>?)
%onst int numLig5ts . 2)
lig5t lig5ts>numLig5ts?)
An arra( t(.e can 6e "ormed 6( s.eci"(ing a t(.e "ollowed 6( s=uare 6rackets BY ZC and including a si@e:
float>7?
-his t(.e can 6e used an(where an( other t(.e can 6e used, including as the return value "rom a "unction
float>7? foo#) ; <
as a constructor o" an arra(
float>7?#461/ 162/ 76A/ 762/ '6')
as an unnamed .arameter
void foo#float>7?)
25
4 #aria$%es and &'pes
and as an alternate wa( o" declaring a varia6le or "unction .arameter.
float>7? a)
It is an error to declare arra(s o" arra(s:
float a>7?>4?) ** illegal
float>7? a>4?) ** illegal
Arra(s can have initiali@ers "ormed "rom arra( constructors:
float a>7? . float>7?#461/ 162/ 76A/ 762/ '6'))
float a>7? . float>?#461/ 162/ 76A/ 762/ '6')) ** same t5ing
5nsi@ed arra(s can 6e e1.licitl( si@ed 6( an initiali@er at declaration time:
float a>7?)
666
float $>? . a) ** $ is expli%itl& si"e 7
float $>7? . a) ** means t5e same t5ing
,owever, im.licitl( si@ed arra(s cannot 6e assigned to. <ote, this is a rare case that initiali@ers and
assignments a..ear to have di""erent semantics.
Arra(s know the num6er o" elements the( contain. -his can 6e o6tained 6( using the length method:
a6lengt5#)) ** returns 7 for t5e a$ove de%larations
-he length method cannot 6e called on an arra( that has not 6een e1.licitl( si@ed.
26
4 #aria$%es and &'pes
#.1.16 Implicit Conversions
In some situations, an e1.ression and its t(.e will 6e im.licitl( converted to a di""erent t(.e. -he
"ollowing ta6le shows all allowed im.licit conversions:
T$pe of e+pression Can be implicitl$ converted to
int
uint
float
ivec%
uvec%
vec%
ivecF
uvecF
vecF
ivecG
uvecG
vecG
-here are no im.licit arra( or structure conversions. )or e1am.le, an arra( o" int cannot 6e im.licitl(
converted to an arra( o" float. -here are no im.licit conversions 6etween signed and unsigned integers.
Hhen an im.licit conversion is done, it is not a re;inter.retation o" the e1.ressionIs 6it .attern, 6ut a
conversion o" its value to an e=uivalent value in the new t(.e. )or e1am.le, the integer value ;E will 6e
converted to the "loating;.oint value ;E.-. Integer values having more 6its o" .recision than a "loating
.oint mantissa will lose .recision when converted to float.
-he conversions in the ta6le a6ove are done onl( as indicated 6( other sections o" this s.eci"ication.
#.2 Scoping
-he sco.e o" a varia6le is determined 6( where it is declared. I" it is declared outside all "unction
de"initions, it has glo6al sco.e, which starts "rom where it is declared and .ersists to the end o" the shader
it is declared in. I" it is declared in a wile test or a for statement, then it is sco.ed to the end o" the
"ollowing su6;statement. *therwise, i" it is declared as a statement within a com.ound statement, it is
sco.ed to the end o" that com.ound statement. I" it is declared as a .arameter in a "unction de"inition, it is
sco.ed until the end o" that "unction de"inition. A "unction 6od( has a sco.e nested inside the "unctionJs
de"inition. -he if statementJs e1.ression does not allow new varia6les to 6e declared, hence does not
"orm a new sco.e.
27
4 #aria$%es and &'pes
Hithin a declaration, the sco.e o" a name starts immediatel( a"ter the initiali@er i" .resent or immediatel(
a"ter the name 6eing declared i" not. 'everal e1am.les:
int x . ')
;
int x . 2/ & . x) ** & is initiali"ed to 2
<
stru%t S
;
int x)
<)
;
S S . S#A)) ** BSB is onl& visi$le as a stru%t and %onstru%tor
S) ** BSB is no: visi$le as a varia$le
<
int x . x) ** Error if x 5as not $een previousl& defined6
All varia6le names, structure t(.e names, and "unction names in a given sco.e share the same name s.ace.
)unction names can 6e redeclared in the same sco.e, with the same or di""erent .arameters, without error.
An im.licitl( si@ed arra( can 6e re;declared in the same sco.e as an arra( o" the same 6ase t(.e.
*therwise, within one com.ilation unit, a declared name cannot 6e redeclared in the same sco.eG doing so
results in a redeclaration error. I" a nested sco.e redeclares a name used in an outer sco.e, it hides all
e1isting uses o" that name. -here is no wa( to access the hidden name or make it unhidden, without
e1iting the sco.e that hid it.
-he 6uilt;in "unctions are sco.ed in a sco.e outside the glo6al sco.e users declare glo6al varia6les in.
-hat is, a shaderIs glo6al sco.e, availa6le "or user;de"ined "unctions and glo6al varia6les, is nested inside
the sco.e containing the 6uilt;in "unctions. Hhen a "unction name is redeclared in a nested sco.e, it hides
all "unctions declared with that name in the outer sco.e. )unction declarations B.rotot(.esC cannot occur
inside o" "unctionsG the( must 6e at glo6al sco.e, or "or the 6uilt;in "unctions, outside the glo6al sco.e.
'hared glo6als are glo6al varia6les declared with the same name in inde.endentl( com.iled units
BshadersC within the same language Bverte1, geometr(, or "ragmentC that are linked together when making
a single .rogram. B/lo6als "orming the inter"ace 6etween two di""erent shader languages are discussed in
other sections.C 'hared glo6als share the same name s.ace, and must 6e declared with the same t(.e.
-he( will share the same storage. 'hared glo6al arra(s must have the same 6ase t(.e and the same
e1.licit si@e. An arra( im.licitl( si@ed in one shader can 6e e1.licitl( si@ed 6( another shader. I" no
shader has an e1.licit si@e "or the arra(, the largest im.licit si@e is used. 'calars must have e1actl( the
same t(.e name and t(.e de"inition. 'tructures must have the same name, se=uence o" t(.e names, and
t(.e de"initions, and "ield names to 6e considered the same t(.e. -his rule a..lies recursivel( "or nested
or em6edded t(.es. All initiali@ers "or a shared glo6al must have the same value, or a link error will
result.
2
4 #aria$%es and &'pes
#." Storage 7ualifiers
Varia6le declarations ma( have one storage =uali"ier s.eci"ied in "ront o" the t(.e. -hese are summari@ed
as
7ualifier 5eaning
P none: de"ault Q local readAwrite memor(, or an in.ut .arameter to a "unction
const a com.ile;time constant, or a "unction .arameter that is read;onl(
in
centroid in
linkage into a shader "rom a .revious stage, varia6le is co.ied in
linkage with centroid 6ased inter.olation
out
centroid out
linkage out o" a shader to a su6se=uent stage, varia6le is co.ied out
linkage with centroid 6ased inter.olation
attribute de.recatedG linkage 6etween a verte1 shader and *.en/0 "or .er;verte1
data
uniform value does not change across the .rimitive 6eing .rocessed, uni"orms
"orm the linkage 6etween a shader, *.en/0, and the a..lication
var!ing
centroid var!ing
de.recatedG linkage 6etween a verte1 shader and a "ragment shader "or
inter.olated data
*ut.uts "rom shader BoutC and in.uts to a shader BinC can 6e "urther =uali"ied with one o" these
inter.olation =uali"iers
7ualifier 5eaning
smoot .ers.ective correct inter.olation
flat no inter.olation
noperspective linear inter.olation
-hese inter.olation =uali"iers ma( onl( .recede the =uali"iers in, centroid in, out, or centroid out in a
declaration. -he( do not a..l( to the de.recated storage =uali"iers var!ing or centroid var!ing. -he(
also do not a..l( to in.uts into a verte1 shader or out.uts "rom a "ragment shader.
0ocal varia6les can onl( use the const storage =uali"ier.
)unction .arameters can use const, in, and out =uali"iers, 6ut as )arameter 6uali/iers. 2arameter
=uali"iers are discussed in 'ection #.1.1 E)unction !alling !onventionsF.
)unction return t(.es and structure "ields do not use storage =uali"iers.
Data t(.es "or communication "rom one run o" a shader e1ecuta6le to its ne1t run Bto communicate
6etween "ragments or 6etween verticesC do not e1ist. -his would .revent .arallel e1ecution o" the same
shader e1ecuta6le on multi.le vertices or "ragments.
2!
4 #aria$%es and &'pes
Initiali@ers ma( onl( 6e used in declarations o" glo6als with no storage =uali"ier, with a const =uali"ier or
with a uniform =uali"ier. /lo6al varia6les without storage =uali"iers that are not initiali@ed in their
declaration or 6( the a..lication will not 6e initiali@ed 6( *.en/0, 6ut rather will enter main89 with
unde"ined values.
#.".1 )efault Storage 7ualifier
I" no =uali"ier is .resent on a glo6al varia6le, then the varia6le has no linkage to the a..lication or shaders
running on other .i.eline stages. )or either glo6al or local un=uali"ied varia6les, the declaration will
a..ear to allocate memor( associated with the .rocessor it targets. -his varia6le will .rovide readAwrite
access to this allocated memor(.
#.".2 Constant 7ualifier
<amed com.ile;time constants can 6e declared using the const =uali"ier. An( varia6les =uali"ied as
constant are read;onl( varia6les "or that shader. Declaring varia6les as constant allows more descri.tive
shaders than using hard;wired numerical constants. -he const =uali"ier can 6e used with an( o" the 6asic
data t(.es. It is an error to write to a const varia6le outside o" its declaration, so the( must 6e initiali@ed
when declared. )or e1am.le,
%onst ve%4 "8xis . ve%4 #A6A/ A6A/ '6A))
'tructure "ields ma( not 6e =uali"ied with const. 'tructure varia6les can 6e declared as const, and
initiali@ed with a structure constructor.
Initiali@ers "or const declarations must 6e constant e1.ressions, as de"ined in 'ection &.%.% E!onstant
+1.ressions.F
#."." Constant '+pressions
A constant ex)ression is one o"
a literal value Be.g., E or trueC
a glo6al or local varia6le =uali"ied as const Bi.e., not including "unction .arametersC
an e1.ression "ormed 6( an o.erator on o.erands that are all constant e1.ressions, including getting an
element or length o" a constant arra(, or a "ield o" a constant structure, or com.onents o" a constant
vector.
a constructor whose arguments are all constant e1.ressions
a 6uilt;in "unction call whose arguments are all constant e1.ressions, with the e1ce.tion o" the te1ture
looku. "unctions and the noise "unctions. -he 6uilt;in "unctions d(dx, d(d!, and fwidt must return
7 when evaluated inside an initiali@er with an argument that is a constant e1.ression.
)unction calls to user;de"ined "unctions Bnon;6uilt;in "unctionsC cannot 6e used to "orm constant
e1.ressions.
An integral constant ex)ression is a constant e1.ression that evaluates to a scalar signed or unsigned
integer.
3"
4 #aria$%es and &'pes
!onstant e1.ressions will 6e evaluated in an invariant wa( so as to create the same value in multi.le
shaders when the same constant e1.ressions a..ear in those shaders. 'ee section &.#.1 E-he Invariant
:uali"ierF "or more details on how to create invariant e1.ressions.
#.".# Inputs
'hader in.ut varia6les are declared with the in storage =uali"ier or the centroid in storage =uali"ier. -he(
"orm the in.ut inter"ace 6etween .revious stages o" the *.en/0 .i.eline and the declaring shader. In.ut
varia6les must 6e declared at glo6al sco.e. Values "rom the .revious .i.eline stage are co.ied into in.ut
varia6les at the 6eginning o" shader e1ecution. Varia6les declared as in or centroid in ma( not 6e written
to during shader e1ecution. *nl( the in.ut varia6les that are actuall( read need to 6e written 6( the
.revious stageG it is allowed to have su.er"luous declarations o" in.ut varia6les.
'ee 'ection 3 EBuilt;in Varia6lesF "or a list o" the 6uilt;in in.ut names.
Verte1 shader in.ut varia6les Bor attri6utesC receive .er;verte1 data. -he( are declared in a verte1 shader
with the in =uali"ier or the de.recated attribute =uali"ier. It is an error to use centroid in or inter.olation
=uali"iers in a verte1 shader in.ut. -he values co.ied in are esta6lished 6( the *.en/0 A2I. It is an
error to use attribute in a non;verte1 shader. Verte1 shader in.uts can onl( 6e float, "loating;.oint
vectors, matrices, signed and unsigned integers and integer vectors. Verte1 shader in.uts can also "orm
arra(s o" these t(.es, 6ut not structures.
+1am.le declarations in a verte1 shader:
in ve%1 position)
in ve%4 normal)
in ve%2 texCoord>1?)
It is e1.ected that gra.hics hardware will have a small num6er o" "i1ed vector locations "or .assing verte1
in.uts. -here"ore, the *.en/0 'hading language de"ines each non;matri1 in.ut varia6le as taking u. one
such vector location. -here is an im.lementation de.endent limit on the num6er o" locations that can 6e
used, and i" this is e1ceeded it will cause a link error. BDeclared in.ut varia6les that are not staticall( used
do not count against this limit.C A scalar in.ut counts the same amount against this limit as a vecG, so
a..lications ma( want to consider .acking grou.s o" "our unrelated "loat in.uts together into a vector to
6etter utili@e the ca.a6ilities o" the underl(ing hardware. A matri1 in.ut will use u. multi.le locations.
-he num6er o" locations used will e=ual the num6er o" columns in the matri1.
/eometr( shader in.ut varia6les get the .er;verte1 values written out 6( verte1 shader out.ut varia6les o"
the same names. 'ince a geometr( shader o.erates on a set o" vertices, each in.ut var(ing varia6le Bor
in.ut 6lock, see inter"ace 6locks 6elowC needs to 6e declared as an arra(. )or e1am.le,
in float foo>?) ** geometr& s5ader input for vertex +out float foo,
+ach element o" such an arra( corres.onds to one verte1 o" the .rimitive 6eing .rocessed. +ach arra( can
o.tionall( have a si@e declared. -he arra( si@e will 6e set 6(, Bor i" .rovided must 6e consistent withC the
in.ut la!out declarationBsC esta6lishing the t(.e o" in.ut .rimitive, as descri6ed later in section &.%.4.1
In.ut 0a(out :uali"iers.
31
4 #aria$%es and &'pes
)or the inter"ace 6etween a verte1 shader and a geometr( shader, verte1 shader out.ut varia6les and
geometr( shader in.ut varia6les o" the same name must match in t(.e and =uali"ication, e1ce.t that the
verte1 shader name cannot 6e declared as an arra( while the geometr( shader name must 6e declared as an
arra(. *therwise, a link error will occur.
I" the out.ut o" a verte1 shader is itsel" an arra( to 6e consumed 6( a geometr( shader, then it must a..ear
in an out.ut 6lock Bsee inter"ace 6locks 6elowC in the verte1 shader and in an in.ut 6lock in the geometr(
shader with a 6lock instance name declared as an arra(. -his is re=uired "or arra(s out.ut "rom a verte1
shader 6ecause two;dimensional arra(s are not su..orted.
)ragment shader in.uts get .er;"ragment values, t(.icall( inter.olated "rom a .revious stageIs out.uts.
-he( are declared in "ragment shaders with the in storage =uali"ier, the centroid in storage =uali"ier, or
the de.recated var!ing and centroid var!ing storage =uali"iers. )ragment in.uts can onl( 6e signed and
unsigned integers and integer vectors, float, "loating;.oint vectors, matrices, or arra(s or structures o"
these. )ragment shader in.uts that are signed or unsigned integers or integer vectors must 6e =uali"ied
with the inter.olation =uali"ier flat.
)ragment in.uts are declared as in the "ollowing e1am.les:
in ve%4 normal)
%entroid in ve%2 TexCoord)
invariant %entroid in ve%1 Color)
noperspe%tive in float temperature)
flat in ve%4 m&Color)
noperspe%tive %entroid in ve%2 m&TexCoord)
I" a geometr( shader is not .resent in a .rogram, 6ut a verte1 and "ragment shader are .resent, then the
out.ut o" the verte1 shader and the in.ut o" the "ragment shader "orm an inter"ace. )or this inter"ace,
verte1 shader out.ut varia6les and "ragment shader in.ut varia6les o" the same name must match in t(.e
and =uali"ication Bother than out matching to inC.
#.". 4niform
-he uniform =uali"ier is used to declare glo6al varia6les whose values are the same across the entire
.rimitive 6eing .rocessed. All uniform varia6les are read;onl( and are initiali@ed e1ternall( either at link
time or through the A2I. -he link time initial value is either the value o" the varia6leIs initiali@er, i"
.resent, or 7 i" no initiali@er is .resent. 'am.ler t(.es cannot have initiali@ers.
+1am.le declarations are:
uniform ve%1 lig5t@osition)
uniform ve%4 %olor . ve%4#A6D/ A6D/ A62)) ** value assigned at lin3 time
-he uniform =uali"ier can 6e used with an( o" the 6asic data t(.es, or when declaring a varia6le whose
t(.e is a structure, or an arra( o" an( o" these.
-here is an im.lementation de.endent limit on the amount o" storage "or uni"orms that can 6e used "or
each t(.e o" shader and i" this is e1ceeded it will cause a com.ile;time or link;time error. 5ni"orm
varia6les that are declared 6ut not used do not count against this limit. -he num6er o" user;de"ined
uni"orm varia6les and the num6er o" 6uilt;in uni"orm varia6les that are used within a shader are added
together to determine whether availa6le uni"orm storage has 6een e1ceeded.
32
4 #aria$%es and &'pes
I" multi.le shaders are linked together, then the( will share a single glo6al uni"orm name s.ace, including
within a language as well as across languages. ,ence, the t(.es and initiali@ers o" uni"orm varia6les with
the same name must match across all shaders that are linked into a single .rogram.
It is legal "or some shaders to .rovide an initiali@er "or a .articular uni"orm varia6le, while another shader
does not, 6ut all .rovided initiali@ers must 6e e=ual.
#.".! Outputs
'hader out.ut varia6les are declared with the out or centroid out storage =uali"iers. -he( "orm the
out.ut inter"ace 6etween the declaring shader and the su6se=uent stages o" the *.en/0 .i.eline. *ut.ut
varia6les must 6e declared at glo6al sco.e. During shader e1ecution the( will 6ehave as normal
un=uali"ied glo6al varia6les. -heir values are co.ied out to the su6se=uent .i.eline stage on shader e1it.
*nl( out.ut varia6les that are read 6( the su6se=uent .i.eline stage need to 6e writtenG it is allowed to
have su.er"luous declarations o" out.ut varia6les.
-here is not an inout storage =uali"ier at glo6al sco.e "or declaring a single varia6le name as 6oth in.ut
and out.ut to a shader. *ut.ut varia6les must 6e declared with di""erent names than in.ut varia6les.
,owever, nesting an in.ut or out.ut inside an inter"ace 6lock with an instance name allows the same
names with one re"erenced through a 6lock instance name.
Verte1 and geometr( out.ut varia6les out.ut .er;verte1 data and are declared using the out storage
=uali"ier, the centroid out storage =uali"ier, or the de.recated var!ing storage =uali"ier. -he( can onl( 6e
float, "loating;.oint vectors, matrices, signed or unsigned integers or integer vectors, or arra(s or
structures o" an( these.
Individual verte1 and geometr( out.uts are declared as in the "ollowing e1am.les:
out ve%4 normal)
%entroid out ve%2 TexCoord)
invariant %entroid out ve%1 Color)
noperspe%tive out float temperature) ** var&ing is depre%ated
flat out ve%4 m&Color)
noperspe%tive %entroid out ve%2 m&TexCoord)
-hese can also a..ear in inter"ace 6locks, as descri6ed in the EInter"ace BlocksF section. Inter"ace 6locks
allow sim.ler addition o" arra(s to the inter"ace "rom verte1 to geometr( shader. -he( also allow a
"ragment shader to have the same in.ut inter"ace as a geometr( shader "or a given verte1 shader.
)ragment out.uts out.ut .er;"ragment data and are declared using the out storage =uali"ier. It is an error
to use centroid out in a "ragment shader. )ragment out.uts can onl( 6e float, "loating;.oint vectors,
signed or unsigned integers or integer vectors, or arra(s o" an( these. 8atrices and structures cannot 6e
out.ut. )ragment out.uts are declared as in the "ollowing e1am.les:
out ve%1 FragmentColor)
out uint Luminosit&)
33
4 #aria$%es and &'pes
#.".2 Interface -loc0s
In.ut, out.ut, and uni"orm varia6le declarations can 6e grou.ed into named inter"ace 6locks to .rovide
coarser granularit( 6acking than is achieva6le with individual declarations. -he( can have an o.tional
instance name, used in the shader to re"erence their mem6ers. An out.ut 6lock o" one .rogramma6le
stage is 6acked 6( a corres.onding in.ut 6lock in the su6se=uent .rogramma6le stage. A uni"orm 6lock is
6acked 6( the a..lication with a 6u""er o6?ect. It is illegal to have an in.ut 6lock in a verte1 shader or an
out.ut 6lock in a "ragment shaderG these uses are reserved "or "uture use.
An inter"ace 6lock is started 6( an in, out, or uniform ke(word, "ollowed 6( a 6lock name, "ollowed 6(
an o.en curl( 6race B 7 C as "ollows:
inter/ace--loc3 :
la.out-6uali/ieropt inter/ace-6uali/ier -loc3-name 7 mem-er-list 8 instance-nameopt @
la.out-6uali/ier :
la!out " la.out-6uali/ier-i!-list #
inter/ace-6uali/ier :
in
out
uniform
la.out-6uali/ier-i!-list
comma se.arated list o" la.out-6uali/ier-i!
mem-er-list :
mem-er-!eclaration
mem-er-!eclaration mem-er-list
mem-er-!eclaration :
la.out-6uali/ieropt 6uali/iersopt t.)e !eclarators @
instance-name :
i!enti/ier
i!enti/ier " #
i!enti/ier " integral-constant-ex)ression #
+ach o" the a6ove elements is discussed 6elow, with the e1ce.tion o" la(out =uali"iers Bla.out-6uali/ier9,
which are de"ined in the ne1t section.
)irst, an e1am.le,
uniform Transform ;
mat1 9odelVie:9atrix)
mat1 9odelVie:@ro0e%tion9atrix)
uniform mat4 Normal9atrix) ** allo:ed restatement of =ualifier
float eformation)
<)
-he a6ove esta6lishes a uni"orm 6lock named E-rans"ormF with "our uni"orms grou.ed inside it.
34
4 #aria$%es and &'pes
-(.es and declarators are the same as "or other in.ut, out.ut, and uni"orm varia6le declarations outside
6locks, with these e1ce.tions:
initiali@ers are not allowed
sam.ler t(.es are not allowed
structure de"initions cannot 6e nested inside a 6lock
*therwise, 6uilt;in t(.es, .reviousl( declared structures, and arra(s o" these are allowed as the t(.e o" a
declarator in the same manner the( are allowed outside a 6lock.
I" no o.tional =uali"ier is used in a mem6er;declaration, the =uali"ication o" the varia6le is ?ust in, out, or
uniform as determined 6( inter/ace-6uali/ier. I" o.tional =uali"iers are used, the( can include
inter.olation and storage =uali"iers and the( must declare an in.ut, out.ut, or uni"orm varia6le consistent
with the inter"ace =uali"ier o" the 6lock: In.ut varia6les, out.ut varia6les, and uni"orm varia6les can onl(
6e in in 6locks, out 6locks, and uniform 6locks, res.ectivel(. Re.eating the in, out, or uniform
inter"ace =uali"ier "or a mem6erIs storage =uali"ier is o.tional. Declarations using the de.recated
attribute and var!ing =uali"iers are not allowed. )or e1am.le,
in 9aterial ;
smoot5 in ve%1 Color') ** legal/ input inside in $lo%3
smoot5 ve%1 Color2) ** legal/ BinB in5erited from Bin 9aterialB
ve%2 TexCoord) ** legal/ TexCoord is an input
uniform float 8tten) ** illegal/ mismat%5ed interfa%es
var&ing ve%2 TexCoord2)**illegal/ depre%ated 3e&:ords donBt get ne: uses
<)
)or this section, de"ine an inter/ace to 6e one o" these
All the uni"orms o" a .rogram. -his s.ans all com.ilation units linked together within one .rogram.
-he 6oundar( 6etween ad?acent .rogramma6le .i.eline stages: -his s.ans all the out.uts in all
com.ilation units o" the "irst stage and all the in.uts in all com.ilation units o" the second stage.
-he 6lock name B-loc3-nameC is used to match inter"aces: an out.ut 6lock o" one .i.eline stage will 6e
matched to an in.ut 6lock with the same name in the su6se=uent .i.eline stage. )or uni"orm 6locks, the
a..lication uses the 6lock name to identi"( the 6lock. Block names have no other use within a shader
6e(ond inter"ace matchingG it is an error to use a 6lock name at glo6al sco.e "or an(thing other than as a
6lock name. 8atched 6lock names within an inter"ace Bas de"ined a6oveC must match in terms o" having
the same num6er o" declarations with the same se=uence o" t(.es and the same se=uence o" mem6er
names, as well as having the same mem6er;wise la(out =uali"ication Bsee ne1t sectionC. )urthermore, i" a
matching 6lock is declared as an arra(, then the arra( si@es must also match Bor "ollow arra( matching
rules "or the inter"ace 6etween a verte1 and a geometr( shaderC. An( mismatch will generate a link error.
A 6lock name is allowed to have di""erent de"initions in di""erent inter"aces.
35
4 #aria$%es and &'pes
I" an instance name Binstance-nameC is not used, the names declared inside the 6lock are sco.ed at the
glo6al level and accessed as i" the( were declared outside the 6lock. I" an instance name Binstance-nameC
is used, then it .uts all the mem6ers inside a sco.e within its own name s.ace, accessed with the "ield
selector B . C o.erator Banalogousl( to structuresC. )or e1am.le,
in Lig5t ;
ve%1 Lig5t@os)
ve%4 Lig5tColor)
<)
in ColoredTexture ;
ve%1 Color)
ve%2 TexCoord)
< 9aterial) ** instan%e name
ve%4 Color) ** different Color t5an 9aterial6Color
ve%1 Lig5t@os) ** illegal/ alread& defined
666
666 . Lig5t@os) ** a%%essing Lig5t@os
666 . 9aterial6Color) ** a%%essing Color in ColoredTexture $lo%3
*utside the shading language Bi.e., in the A2IC, mem6ers are similarl( identi"ied e1ce.t the 6lock name is
alwa(s used in .lace o" the instance name BA2I accesses are to inter"aces, not to shadersC. I" there is no
instance name, then the A2I does not use the 6lock name to access a mem6er, ?ust the mem6er name.
out Vertex ;
ve%1 @osition) ** 8@I transform*feed$a%3 :ill use +Vertex6@osition,
ve%2 Texture)
< Coords) ** s5ader :ill use +Coords6@osition,
out Vertex2 ;
ve%1 Color) ** 8@I :ill use +Color,
<)
)or 6locks declared as arra(s, the arra( inde1 must also 6e included when accessing mem6ers, as in this
e1am.le
uniform Transform ; ** 8@I uses +Transform>2?, to refer to instan%e 2
mat1 9odelVie:9atrix)
mat1 9odelVie:@ro0e%tion9atrix)
float eformation)
< transforms>1?)
666
666 . transforms>2?69odelVie:9atrix) ** s5ader a%%ess of instan%e 2
** 8@I uses +Transform69odelVie:9atrix, to =uer& an offset or ot5er =uer&
)or uni"orm 6locks declared an arra(, each individual arra( element corres.onds to a se.arate 6u""er
o6?ect 6acking one instance o" the 6lock. As the arra( si@e indicates the num6er o" 6u""er o6?ects needed,
uni"orm 6lock arra( declarations must s.eci"( an arra( si@e. All inde1es used to inde1 a uni"orm 6lock
arra( must 6e integral constant e1.ressions.
36
4 #aria$%es and &'pes
Hhen using *.en/0 A2I entr( .oints to identi"( the name o" an individual 6lock in an arra( o" 6locks,
the name string must include an arra( inde1 Be.g., Trans/orm"2#C. Hhen using *.en/0 A2I entr( .oints
to re"er to o""sets or other characteristics o" a 6lock mem6er, an arra( inde1 must not 6e s.eci"ied Be.g.,
Trans/orm.:o!elVie4:atrixC.
/eometr( shader in.ut 6locks must 6e declared as arra(s and "ollow the arra( declaration and linking
rules "or all geometr( shader in.uts. All other in.ut and out.ut 6lock arra(s must s.eci"( an arra( si@e.
-here is an im.lementation de.endent limit on the num6er o" uni"orm 6locks that can 6e used .er stage.
I" this limit is e1ceeded, it will cause a link error.
#.".3 La$out 7ualifiers
0a(out =uali"iers can a..ear in several "orms o" declaration. -he( can a..ear as .art o" an inter"ace
6lock de"inition or 6lock mem6er, as shown in the grammar in the .revious section. -he( can also a..ear
with ?ust an inter"ace =uali"ier to esta6lish la(outs o" other declarations made with that inter"ace =uali"ier:
la.out-6uali/ier inter/ace-6uali/ier @
*r, the( can a..ear with an individual varia6le declared with an inter"ace =uali"ier:
la.out-6uali/ier inter/ace-6uali/ier !eclaration @
Declarations o" la(outs can onl( 6e made at glo6al sco.e, and onl( where indicated in the "ollowing
su6sectionsG 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-6uali/ier e1.ands to
la.out-6uali/ier :
la!out " la.out-6uali/ier-i!-list #
-he tokens in an( la.out-6uali/ier-i!-list are identi"iers, not ke(words. /enerall(, the( can 6e listed in
an( order. *rder;de.endent meanings e1ist onl( i" e1.licitl( called out 6elow. 'imilarl(, these identi"iers
are not case sensitive, unless e1.licitl( noted otherwise.
#.".3.1 Input La$out 7ualifiers
Verte1 shaders do not have an( in.ut la(out =uali"iers.
/eometr( shaders allow in.ut la(out =uali"iers onl( on the inter"ace =uali"ier in, not on an in.ut 6lock,
6lock mem6er, or varia6le. -he la(out =uali"ier identi"iers "or geometr( shader in.uts are
la.out-6uali/ier-i!
points
lines
lines$adJacenc!
triangles
triangles$adJacenc!
37
4 #aria$%es and &'pes
*nl( one argument is acce.ted. )or e1am.le,
la&out#triangles) in)
will esta6lish that all in.uts to the geometr( shader are triangles.
At least one geometr( shader Bcom.ilation unitC in a .rogram must declare an in.ut la(out, and all
geometr( shader in.ut la(out declarations in a .rogram must declare the same la(out. It is not re=uired
that all geometr( shaders in a .rogram declare an in.ut la(out.
All geometr( shader in.ut unsi@ed arra( declarations will 6e si@ed 6( an earlier in.ut la(out =uali"ier,
when .resent, as .er the "ollowing ta6le.
Da!out Si)e of Nnput Arra!s
.oints 1
lines
lines>ad?acenc( &
triangles %
triangles>ad?acenc( #
-he intrinsicall( declared in.ut arra( gl_in"# will also 6e si@ed 6( an( in.ut la(out declaration. ,ence,
the e1.ression
gl_in6lengt5#)
will return the value "rom the ta6le a6ove.
)or in.uts declared without an arra( si@e, including intrinsicall( declared in.uts Bi.e., gl_inC, a la(out must
6e declared 6e"ore an( use o" the method lengt'89 or other arra( use re=uiring its si@e 6e known.
It is a com.ile;time error i" a la(out declarationIs arra( si@e B"rom ta6le a6oveC does not match an( arra(
si@e s.eci"ied in declarations o" an in.ut varia6le in the same shader. -he "ollowing are all e1am.les o"
com.ile time errors:
3
4 #aria$%es and &'pes
** %ode se=uen%e :it5in one s5ader666
in ve%1 Color'>?) ** si"e un3no:n
666Color'6lengt5#)666** illegal/ lengt5#) un3no:n
in ve%1 Color2>2?) ** si"e is 2
666Color'6lengt5#)666** illegal/ Color' still 5as no si"e
in ve%1 Color4>4?) ** illegal/ input si"es are in%onsistent
la&out#lines) in) ** legal/ input si"e is 2/ mat%5ing
in ve%1 Color1>4?) ** illegal/ %ontradi%ts la&out
666Color'6lengt5#)666** legal/ lengt5#) is 2/ Color' si"ed $& la&out#)
la&out#lines) in) ** legal/ mat%5es ot5er la&out#) de%laration
la&out#triangles) in)** illegal/ does not mat%5 earlier la&out#) de%laration
It is a link;time error i" not all .rovided si@es Bsi@ed in.ut arra(s and la(out si@eC match across all
geometr( shaders in the .rogram.
)ragment shaders can have an in.ut la(out onl( "or redeclaring the 6uilt;in varia6le gl_$ragoor! Bsee
section 3. )ragment 'hader '.ecial Varia6lesC. -he la(out =uali"ier identi"iers "or gl_$ragoor! are
la.out-6uali/ier-i!
origin$upper$left
pixel$center$integer
B( de"ault, gl_$ragoor! assumes a lower;le"t origin "or window coordinates and assumes .i1el centers
are located at hal";.i1el coordinates. )or e1am.le, the Bx, .C location B7.$, 7.$C is returned "or the lower;
le"t;most .i1el in a window. -he origin can 6e changed 6( redeclaring gl_$ragoor! with the
origin$upper$left identi"ier, moving the origin o" gl_$ragoor! to the u..er le"t o" the window, with .
increasing in value toward the 6ottom o" the window. -he values returned can also 6e shi"ted 6( hal" a
.i1el in 6oth x and . 6( pixel$center$integer so it a..ears the .i1els are centered at whole num6er .i1el
o""sets. -his moves the Bx, .C value returned 6( gl_$ragoor! o" B7.$, 7.$C 6( de"ault, to B7.7, 7.7C with
pixel$center$integer. Redeclarations are done as "ollows
in ve%1 gl_FragCoord) ** rede%laration t5at %5anges not5ing is allo:ed
** 8ll t5e follo:ing are allo:ed rede%laration t5at %5ange $e5avior
la&out#origin_upper_left) in ve%1 gl_FragCoord)
la&out#pixel_%enter_integer) in ve%1 gl_FragCoord)
la&out#origin_upper_left/ pixel_%enter_integer) in ve%1 gl_FragCoord)
I" gl_$ragoor! is redeclared in an( "ragment shader in a .rogram, it must 6e redeclared in all the
"ragment shaders in that .rogram that have a static use gl_$ragoor!. All redeclarations o"
gl_$ragoor! in all "ragment shaders in a single .rogram must have the same set o" =uali"iers. Hithin
an( shader, the "irst redeclarations o" gl_$ragoor! must a..ear 6e"ore an( use o" gl_$ragoor!. -he
6uilt;in gl_$ragoor! is onl( .redeclared in "ragment shaders, so redeclaring it in an( other shader
language will 6e illegal.
Redeclaring gl_$ragoor! with origin$upper$left andAor pixel$center$integer =uali"iers onl( a""ects
gl_$ragoor!.x and gl_$ragoor!... It has no a""ect on rasteri@ation, trans"ormation, or an( other .art
o" the *.en/0 .i.eline or language "eatures.
3!
4 #aria$%es and &'pes
#.".3.2 Output La$out 7ualifiers
Verte1 and "ragment shaders cannot have out.ut la(out =uali"iers.
/eometr( shaders can have out.ut la(out =uali"iers onl( on the inter"ace =uali"ier out, not on an out.ut
6lock or varia6le declaration.
-he la(out =uali"ier identi"iers "or geometr( shader out.uts are
la.out-6uali/ier-i!
points
line$strip
triangle$strip
max$vertices = integer-constant
*ne declaration can declare either a .rimitive t(.e Bpoints, line$strip, or triangle$stripC, or
max$vertices, or 6oth. 5se max$vertices to declare the ma1imum num6er o" vertices this shader will
ever emit in a single e1ecution. )or e1am.le,
la&out#triangle_strip/ max_verti%es . EA) out) ** order does not matter
la&out#max_verti%es . EA) out) ** rede%laration o3a&
la&out#triangle_strip) out) ** rede%laration o3a&
la&out#points) out) ** error/ %ontradi%ts triangle_strip
la&out#max_verti%es . 4A) out) ** error/ %ontradi%ts EA
these will esta6lish that all out.uts "rom the geometr( shader are triangles and at most #7 vertices will 6e
emitted 6( the shader. It is an error "or the ma1imum num6er o" vertices to 6e greater than
gl$'axGeometr!OutputUertices.
All geometr( shader out.ut la(out declarations in a .rogram must declare the same la(out and same value
"or max$vertices. -here must 6e at least one geometr( out.ut la(out declaration somewhere in a
.rogram, 6ut not all geometr( shaders Bcom.ilation unitsC are re=uired to declare it.
#.".3." 4niform -loc0 La$out 7ualifiers
0a(out =uali"iers can 6e used "or uni"orm 6locks, 6ut not "or non;6lock uni"orm declarations. -he la(out
=uali"ier identi"iers "or uni"orm 6locks are
la.out-6uali/ier-i!
sared
packed
std1G-
row$maJor
column$maJor
<one o" these have an( semantic a""ect at all on the usage o" the varia6les 6eing declaredG the( onl(
descri6e how data is laid out in memor(. )or e1am.le, matri1 semantics are alwa(s column;6ased, as
descri6ed in the rest o" this s.eci"ication, no matter what la(out =uali"iers are 6eing used.
5ni"orm 6lock la(out =uali"iers can 6e declared "or glo6al sco.e, on a single uni"orm 6lock, or on a single
6lock mem6er declaration.
4"
4 #aria$%es and &'pes
De"ault la(outs are esta6lished at glo6al sco.e "or uni"orm 6locks as
la&out#la.out-6uali/ier-i!-list) uniform)
Hhen this is done, the .revious de"ault =uali"ication is "irst inherited and then overridden as .er the
override rules listed 6elow "or each =uali"ier listed in the declaration. -he result 6ecomes the new de"ault
=uali"ication sco.ed to su6se=uent uni"orm 6lock de"initions.
-he initial state o" com.ilation is as i" the "ollowing were declared:
la&out#s5ared/ %olumn_ma0or) uniform)
+1.licitl( declaring this in a shader will return de"aults 6ack to their initial state.
5ni"orm 6locks can 6e declared with o.tional la(out =uali"iers, and so can their individual mem6er
declarations. 'uch 6lock la(out =uali"ication is sco.ed onl( to the content o" the 6lock. As with glo6al
la(out declarations, 6lock la(out =uali"ication "irst inherits "rom the current de"ault =uali"ication and then
overrides it. 'imilarl(, individual mem6er la(out =uali"ication is sco.ed ?ust to the mem6er declaration,
and inherits "rom and overrides the 6lockIs =uali"ication.
-he s'are! =uali"ier overrides onl( the st!140 and )ac3e! =uali"iersG other =uali"iers are inherited. -he
com.ilerAlinker will ensure that multi.le .rograms and .rogramma6le stages containing this de"inition
will share the same memor( la(out "or this 6lock, as long as the( also matched in their ro4_ma;or andAor
column_ma;or =uali"ications. -his allows use o" the same 6u""er to 6ack the same 6lock de"inition across
di""erent .rograms.
-he )ac3e! =uali"ier overrides onl( st!140 and s'are!G other =uali"iers are inherited. Hhen )ac3e! is
used, no sharea6le la(out is guaranteed. -he com.iler and linker can o.timi@e memor( use 6ased on what
varia6les activel( get used and on other criteria. *""sets must 6e =ueried, as there is no other wa( o"
guaranteeing where Band whichC varia6les reside within the 6lock. Attem.ts to share a .acked uni"orm
6lock across .rograms or stages will generall( "ail. ,owever, im.lementations ma( aid a..lication
management o" .acked 6locks 6( using canonical la(outs "or .acked 6locks.
-he st!140 =uali"ier overrides onl( the )ac3e! and s'are! =uali"iersG other =uali"iers are inherited. -he
la(out is e1.licitl( determined 6( this, as descri6ed in section .11.& under 'tandard 5ni"orm Block
0a(out o" the *.en/0 /ra.hics '(stem '.eci"ication. ,ence, as in s'are! a6ove, the resulting la(out is
sharea6le across .rograms.
0a(out =uali"iers on mem6er declarations cannot use the s'are!, )ac3e!, or st!140 =uali"iers. -hese can
onl( 6e used at glo6al sco.e or on a 6lock declaration.
-he ro4_ma;or =uali"ier overrides onl( the column_ma;or =uali"ierG other =uali"iers are inherited. It onl(
a""ects the la(out o" matrices. +lements within a matri1 row will 6e contiguous in memor(.
-he column_ma;or =uali"ier overrides onl( the ro4_ma;or =uali"ierG other =uali"iers are inherited. It onl(
a""ects the la(out o" matrices. +lements within a matri1 column will 6e contiguous in memor(.
Hhen multi.le arguments are listed in a la!out declaration, the a""ect will 6e the same as i" the( were
declared one at a time, in order "rom le"t to right, each in turn inheriting "rom and overriding the result
"rom the .revious =uali"ication.
41
4 #aria$%es and &'pes
)or e1am.le
la&out#ro:_ma0or/ %olumn_ma0or)
results in the =uali"ication 6eing column_ma;or. *ther e1am.les:
la&out#s5ared/ ro:_ma0or) uniform) ** default is no: s5ared and ro:_ma0or
la&out#std'1A) uniform Transform ; ** la&out of t5is $lo%3 is std'1A
mat1 9') ** ro:_ma0or
la&out#%olumn_ma0or) mat1 92) ** %olumn ma0or
mat4 N') ** ro:_ma0or
<)
uniform T2 ; ** la&out of t5is $lo%3 is s5ared
666
<)
la&out#%olumn_ma0or) uniform T4 ; ** s5ared and %olumn_ma0or
mat1 94) ** %olumn_ma0or
la&out#ro:_ma0or) mat1 m1) ** ro: ma0or
mat4 N2) ** %olumn_ma0or
<)
#.".9 Interpolation
-he .resence o" and t(.e o" inter.olation is controlled 6( the storage =uali"iers centroid in and centroid
out, and 6( the o.tional inter.olation =uali"iers smoot, flat, and noperspective as well as 6( de"ault
6ehaviors esta6lished through the *.en/0 A2I when no inter.olation =uali"ier is .resent. Hhen an
inter.olation =uali"ier is used, it overrides settings esta6lished through the *.en/0 A2I. It is a com.ile;
time error to use more than one inter.olation =uali"ier.
A varia6le =uali"ied as flat will not 6e inter.olated. Instead, it will have the same value "or ever(
"ragment within a triangle. -his value will come "rom a single .rovoking verte1, as descri6ed 6( the
*.en/0 /ra.hics '(stem '.eci"ication. A varia6le ma( 6e =uali"ied as flat centroid, which will mean
the same thing as =uali"(ing it onl( as flat.
A varia6le =uali"ied as smoot will 6e inter.olated in a .ers.ective;correct manner over the .rimitive
6eing rendered. Inter.olation in a .ers.ective correct manner is s.eci"ied in e=uations %.# in the *.en/0
/ra.hics '(stem '.eci"ication, section %.$.
A varia6le =uali"ied as noperspective must 6e inter.olated linearl( in screen s.ace, as descri6ed in
e=uation %.3 in the *.en/0 /ra.hics '(stem '.eci"ication, section %.$.
-his .aragra.h onl( a..lies i" inter.olation is 6eing done: I" single;sam.ling, the value is inter.olated to
the .i1elIs center, and the centroid =uali"ier, i" .resent, is ignored. I" multi;sam.ling and the varia6le is
not =uali"ied with centroid, then the value must 6e inter.olated to the .i1elIs center, or an(where within
the .i1el, or to one o" the .i1elIs sam.les. I" multi;sam.ling and the varia6le is =uali"ied with centroid,
then the value must 6e inter.olated to a .oint that lies in 6oth the .i1el and in the .rimitive 6eing
rendered, or to one o" the .i1elIs sam.les that "alls within the .rimitive. Due to the less regular location o"
centroids, their derivatives ma( 6e less accurate than non;centroid inter.olated varia6les.
42
4 #aria$%es and &'pes
-he t(.e and .resence o" the inter.olation =uali"iers and storage =uali"iers and invariant =uali"iers o"
varia6les with the same name declared in all linked shaders must match, otherwise the link command will
"ail.
#.".9.1 8edeclaring -uilt9in Interpolation *ariables in the Compatibilit$ ,rofile
-he "ollowing .redeclared varia6les can 6e redeclared with an inter.olation =uali"ier when using the
com.ati6ilit( .ro"ile:
Verte1 and geometr( languages:
gl_FrontColor
gl_-a%3Color
gl_FrontSe%ondar&Color
gl_-a%3Se%ondar&Color
)ragment language:
gl_Color
gl_Se%ondar&Color
)or e1am.le,
in ve%1 gl_Color) ** prede%lared $& t5e fragment language
flat in ve%1 gl_Color) ** rede%lared $& user to $e flat
flat in ve%1 gl_FrontColor) ** input to geometr& s5ader/ no +gl_in>?,
flat out ve%1 gl_FrontColor) ** output from geometr& s5ader
In.ut or out.ut instance names on 6locks are not used when redeclaring 6uilt;in varia6les.
I" gl_olor is redeclared with an inter.olation =uali"ier, then gl_$rontolor and gl_<ac3olor Bi" the(
are written toC must also 6e redeclared with the same inter.olation =uali"ier, and vice versa. I"
gl_+econ!ar.olor is redeclared with an inter.olation =uali"ier, then gl_$ront+econ!ar.olor and
gl_<ac3+econ!ar.olor Bi" the( are written toC must also 6e redeclared with the same inter.olation
=uali"ier, and vice versa. -his =uali"ier matching on .redeclared varia6les is onl( re=uired "or varia6les
that are staticall( used within the shaders in a .rogram.
#.# ,arameter 7ualifiers
2arameters can have these =uali"iers.
7ualifier 5eaning
P none: de"ault Q same is in
in "or "unction .arameters .assed into a "unction
out "or "unction .arameters .assed 6ack out o" a "unction, 6ut not initiali@ed
"or use when .assed in
inout "or "unction .arameters .assed 6oth into and out o" a "unction
43
4 #aria$%es and &'pes
2arameter =uali"iers are discussed in more detail in 'ection #.1.1 E)unction !alling !onventionsF.
#. ,recision and ,recision 7ualifiers
2recision =uali"iers are added "or code .orta6ilit( with *.en/0 +', not "or "unctionalit(. -he( have the
same s(nta1 as in *.en/0 +', as descri6ed 6elow, 6ut the( have no semantic meaning, which includes no
e""ect on the .recision used to store or o.erate on varia6les.
I" an e1tension adds in the same semantics and "unctionalit( in the *.en/0 +' .7 s.eci"ication "or
.recision =uali"iers, then the e1tension is allowed to reuse the ke(words 6elow "or that .ur.ose.
#..1 8ange and ,recision
'ection num6er reserved "or "uture use.
#..2 ,recision 7ualifiers
An( "loating .oint or an( integer declaration can have the t(.e .receded 6( one o" these .recision
=uali"iers:
7ualifier 5eaning
igp <one.
mediump <one.
lowp <one.
)or e1am.le:
lo:p float %olor)
out mediump ve%2 @)
lo:p ive%2 foo#lo:p mat4))
5ig5p mat1 m)
0iteral constants do not have .recision =uali"iers. <either do Boolean varia6les. <either do "loating .oint
constructors nor integer constructors when none o" the constructor arguments have .recision =uali"iers.
2recision =uali"iers, as with other =uali"iers, do not e""ect the 6asic t(.e o" the varia6le. In .articular,
there are no constructors "or .recision conversionsG constructors onl( convert t(.es. 'imilarl(, .recision
=uali"iers, as with other =uali"iers, do not contri6ute to "unction overloading 6ased on .arameter t(.es. As
discussed in the ne1t cha.ter, "unction in.ut and out.ut is done through co.ies, and there"ore =uali"iers do
not have to match.
-he same o6?ect declared in di""erent shaders that are linked together must have the same .recision
=uali"ication. -his a..lies to in.uts, out.uts, uni"orms, and glo6als.
#.." )efault ,recision 7ualifiers
-he .recision statement
44
4 #aria$%es and &'pes
pre%ision pre%isionF=ualifier t&pe)
can 6e used to esta6lish a de"ault .recision =uali"ier. -he t!pe "ield can 6e either int or float, and the
)recision-6uali/ier can 6e lowp, mediump, or igp. An( other t(.es or =uali"iers will result in an error.
I" t.)e is float, the directive a..lies to non;.recision;=uali"ied "loating .oint t(.e Bscalar, vector, and
matri1C declarations. I" t.)e is int, the directive a..lies to all non;.recision;=uali"ied integer t(.e Bscalar,
vector, signed, and unsignedC declarations. -his includes glo6al varia6le declarations, "unction return
declarations, "unction .arameter declarations, and local varia6le declarations.
<on;.recision =uali"ied declarations will use the .recision =uali"ier s.eci"ied in the most recent precision
statement that is still in sco.e. -he precision statement has the same sco.ing rules as varia6le
declarations. I" it is declared inside a com.ound statement, its e""ect sto.s at the end o" the innermost
statement it was declared in. 2recision statements in nested sco.es override .recision statements in outer
sco.es. 8ulti.le .recision statements "or the same 6asic t(.e can a..ear inside the same sco.e, with later
statements overriding earlier statements within that sco.e.
-he verte1 and geometr( languages have the "ollowing .redeclared glo6all( sco.ed de"ault .recision
statements:
pre%ision 5ig5p float)
pre%ision 5ig5p int)
-he "ragment language has the "ollowing .redeclared glo6all( sco.ed de"ault .recision statements:
pre%ision mediump int)
pre%ision 5ig5p float)
#..# .vailable ,recision 7ualifiers
-he 6uilt;in macro /0>)RA/8+<->2R+!I'I*<>,I/, is de"ined to 1:
#define !L_FR8!9ENT_@RECISION_GI!G '
-his macro is availa6le in the verte1, geometr(, and "ragment languages.
#.! *ariance and the Invariant 7ualifier
In this section, variance re"ers to the .ossi6ilit( o" getting di""erent values "rom the same e1.ression in
di""erent .rograms. )or e1am.le, sa( two verte1 shaders, in di""erent .rograms, each set gl_%osition with
the same e1.ression in 6oth shaders, and the in.ut values into that e1.ression are the same when 6oth
shaders run. It is .ossi6le, due to inde.endent com.ilation o" the two shaders, that the values assigned to
gl_%osition are not e1actl( the same when the two shaders run. In this e1am.le, this can cause .ro6lems
with alignment o" geometr( in a multi;.ass algorithm.
In general, such variance 6etween shaders is allowed. Hhen such variance does not e1ist "or a .articular
out.ut varia6le, that varia6le is said to 6e invariant.
45
4 #aria$%es and &'pes
#.!.1 The Invariant 7ualifier
-o ensure that a .articular out.ut varia6le is invariant, it is necessar( to use the invariant =uali"ier. It can
either 6e used to =uali"( a .reviousl( declared varia6le as 6eing invariant
invariant gl_@osition) ** ma3e existing gl_@osition $e invariant
out ve%4 Color)
invariant Color) ** ma3e existing Color $e invariant
or as .art o" a declaration when a varia6le is declared
invariant %entroid out ve%4 Color)
-he invariant =uali"ier must a..ear 6e"ore an( inter.olation =uali"iers or storage =uali"iers when
com6ined with a declaration. *nl( varia6les out.ut "rom a shader Bincluding those that are then in.ut to a
su6se=uent shaderC can 6e candidates "or invariance. -his includes user;de"ined out.ut varia6les and the
6uilt;in out.ut varia6les. )or varia6les leaving one shader and coming into another shader, the invariant
ke(word has to 6e used in 6oth shaders, or a link error will result.
In.ut or out.ut instance names on 6locks are not used when redeclaring 6uilt;in varia6les.
-he invariant ke(word can 6e "ollowed 6( a comma se.arated list o" .reviousl( declared identi"iers. All
uses o" invariant must 6e at the glo6al sco.e, and 6e"ore an( use o" the varia6les 6eing declared as
invariant.
-o guarantee invariance o" a .articular out.ut varia6le across two .rograms, the "ollowing must also 6e
true:
-he out.ut varia6le is declared as invariant in 6oth .rograms.
-he same values must 6e in.ut to all shader in.ut varia6les consumed 6( e1.ressions and "low control
contri6uting to the value assigned to the out.ut varia6le.
-he te1ture "ormats, te1el values, and te1ture "iltering are set the same wa( "or an( te1ture "unction
calls contri6uting to the value o" the out.ut varia6le.
All in.ut values are all o.erated on in the same wa(. All o.erations in the consuming e1.ressions and
an( intermediate e1.ressions must 6e the same, with the same order o" o.erands and same
associativit(, to give the same order o" evaluation. Intermediate varia6les and "unctions must 6e
declared as the same t(.e with the same e1.licit or im.licit .recision =uali"iers. An( control "low
a""ecting the out.ut value must 6e the same, and an( e1.ressions consumed to determine this control
"low must also "ollow these invariance rules.
All the data "low and control "low leading to setting the invariant out.ut varia6le reside in a single
com.ilation unit.
+ssentiall(, all the data "low and control "low leading to an invariant out.ut must match.
Initiall(, 6( de"ault, all out.ut varia6les are allowed to 6e variant. -o "orce all out.ut varia6les to 6e
invariant, use the .ragma
46
4 #aria$%es and &'pes
#pragma ST!L invariant#all)
6e"ore all declarations in a shader. I" this .ragma is used a"ter the declaration o" an( varia6les or
"unctions, then the set o" out.uts that 6ehave as invariant is unde"ined. It is an error to use this .ragma in
a "ragment shader.
/enerall(, invariance is ensured at the cost o" "le1i6ilit( in o.timi@ation, so .er"ormance can 6e degraded
6( use o" invariance. ,ence, use o" this .ragma is intended as a de6ug aid, to avoid individuall( declaring
all out.ut varia6les as invariant.
#.!.2 Invariance of Constant '+pressions
Invariance must 6e guaranteed "or constant e1.ressions. A .articular constant e1.ression must evaluate to
the same result i" it a..ears again in the same shader or a di""erent shader. -his includes the same
e1.ression a..earing two shaders o" the same language or shaders o" two di""erent languages.
!onstant e1.ressions must evaluate to the same result when o.erated on as alread( descri6ed a6ove "or
invariant varia6les.
#.2 Order of 7ualification
Hhen multi.le =uali"ications are .resent, the( must "ollow a strict order. -his order is as "ollows.
invariant-6uali/ier inter)olation-6uali/ier storage-6uali/ier )recision-6uali/ier
storage-6uali/ier )arameter-6uali/ier )recision-6uali/ier
47
Operators and '+pressions
.1 Operators
-he *.en/0 'hading 0anguage has the "ollowing o.erators.
,recedence Operator Class Operators .ssociativit$
1 BhighestC .arenthetical grou.ing " # <A

arra( su6scri.t
"unction call and constructor structure
"ield or method selector, swi@@ler
.ost "i1 increment and decrement
5 6
" #
.
// **
0e"t to Right
%
.re"i1 increment and decrement
unar(
// **
/ * < >
Right to 0e"t
& multi.licative 1 0 2 0e"t to Right
$ additive / * 0e"t to Right
# 6it;wise shi"t 33 44 0e"t to Right
3 relational 3 4 3= 4= 0e"t to Right
4 e=ualit( == >= 0e"t to Right
9 6it;wise and ; 0e"t to Right
17 6it;wise e1clusive or 9 0e"t to Right
11 6it;wise inclusive or : 0e"t to Right
1 logical and ;; 0e"t to Right
1% logical e1clusive or 99 0e"t to Right
1& logical inclusive or : : 0e"t to Right
1$ selection A ? Right to 0e"t
1#
Assignment
arithmetic assignments
=
/= *=
1= 0=
2= 33= 44=
;= 9= :=
Right to 0e"t
13 BlowestC se=uence , 0e"t to Right
-here is no address;o" o.erator nor a dere"erence o.erator. -here is no t(.ecast o.eratorG constructors
are used instead.
4
5 Operators and ()pressions
.2 .rra$ Operations
-hese are now descri6ed in 'ection $.3 E'tructure and Arra( *.erationsF.
." %unction Calls
I" a "unction returns a value, then a call to that "unction ma( 6e used as an e1.ression, whose t(.e will 6e
the t(.e that was used to declare or de"ine the "unction.
)unction de"initions and calling conventions are discussed in 'ection #.1 E)unction De"initionsF .
.# Constructors
!onstructors use the "unction call s(nta1, where the "unction name is a t(.e, and the call makes an o6?ect
o" that t(.e. !onstructors are used the same wa( in 6oth initiali@ers and e1.ressions. B'ee 'ection 9
E'hading 0anguage /rammarF "or details.C -he .arameters are used to initiali@e the constructed value.
!onstructors can 6e used to re=uest a data t(.e conversion to change "rom one scalar t(.e to another
scalar t(.e, or to 6uild larger t(.es out o" smaller t(.es, or to reduce a larger t(.e to a smaller t(.e.
In general, constructors are not 6uilt;in "unctions with .redetermined .rotot(.es. )or arra(s and
structures, there must 6e e1actl( one argument in the constructor "or each element or "ield. )or the other
t(.es, the arguments must .rovide a su""icient num6er o" com.onents to .er"orm the initiali@ation, and it
is an error to include so man( arguments that the( cannot all 6e used. Detailed rules "ollow. -he
.rotot(.es actuall( listed 6elow are merel( a su6set o" e1am.les.
.#.1 Conversion and Scalar Constructors
!onverting 6etween scalar t(.es is done as the "ollowing .rotot(.es indicate:
int#$ool) ** %onverts a -oolean value to an int
int#float) ** %onverts a float value to an int
float#$ool) ** %onverts a -oolean value to a float
float#int) ** %onverts a signed integer value to a float
$ool#float) ** %onverts a float value to a -oolean
$ool#int) ** %onverts a signed integer value to a -oolean
uint#$ool) ** %onverts a -oolean value to an unsigned integer
uint#float) ** %onverts a float value to an unsigned integer
uint#int) ** %onverts a signed integer value to an unsigned integer
int#uint) ** %onverts an unsigned integer to a signed integer
$ool#uint) ** %onverts an unsigned integer value to a -oolean value
float#uint) ** %onverts an unsigned integer value to a float value
Hhen constructors are used to convert a float to an int or uint, the "ractional .art o" the "loating;.oint
value is dro..ed. It is unde"ined to convert a negative "loating .oint value to an uint.
Hhen a constructor is used to convert an int, uint, or a float to a bool, 7 and 7.7 are converted to false,
and non;@ero values are converted to true. Hhen a constructor is used to convert a bool to an int, uint,
or float, false is converted to 7 or 7.7, and true is converted to 1 or 1.7.
4!
5 Operators and ()pressions
-he constructor int"uint# .reserves the 6it .attern in the argument, which will change the argumentIs
value i" its sign 6it is set. -he constructor uint"int# .reserves the 6it .attern in the argument, which will
change its value i" it is negative.
Identit( constructors, like floatBfloatC are also legal, 6ut o" little use.
'calar constructors with non;scalar .arameters can 6e used to take the "irst element "rom a non;scalar.
)or e1am.le, the constructor floatBvecFC will select the "irst com.onent o" the vecF .arameter.
.#.2 *ector and 5atri+ Constructors
!onstructors can 6e used to create vectors or matrices "rom a set o" scalars, vectors, or matrices. -his
includes the a6ilit( to shorten vectors.
I" there is a single scalar .arameter to a vector constructor, it is used to initiali@e all com.onents o" the
constructed vector to that scalarJs value. I" there is a single scalar .arameter to a matri1 constructor, it is
used to initiali@e all the com.onents on the matri1Js diagonal, with the remaining com.onents initiali@ed
to 7.7.
I" a vector is constructed "rom multi.le scalars, one or more vectors, or one or more matrices, or a mi1ture
o" these, the vectorIs com.onents will 6e constructed in order "rom the com.onents o" the arguments. -he
arguments will 6e consumed le"t to right, and each argument will have all its com.onents consumed, in
order, 6e"ore an( com.onents "rom the ne1t argument are consumed. 'imilarl( "or constructing a matri1
"rom multi.le scalars or vectors, or a mi1ture o" these. 8atri1 com.onents will 6e constructed and
consumed in column ma?or order. In these cases, there must 6e enough com.onents .rovided in the
arguments to .rovide an initiali@er "or ever( com.onent in the constructed value. It is an error to .rovide
e1tra arguments 6e(ond this last used argument.
I" a matri1 is constructed "rom a matri1, then each com.onent Bcolumn i, row ;C in the result that has a
corres.onding com.onent Bcolumn i, row ;C in the argument will 6e initiali@ed "rom there. All other
com.onents will 6e initiali@ed to the identit( matri1. I" a matri1 argument is given to a matri1 constructor,
it is an error to have an( other arguments.
I" the 6asic t(.e Bbool, int, or floatC o" a .arameter to a constructor does not match the 6asic t(.e o" the
o6?ect 6eing constructed, the scalar construction rules Ba6oveC are used to convert the .arameters.
5"
5 Operators and ()pressions
'ome use"ul vector constructors are as "ollows:
ve%4#float) ** initiali"es ea%5 %omponent of t5e ve%4 :it5 t5e float
ve%1#ive%1) ** ma3es a ve%1 :it5 %omponentF:ise %onversion
ve%1#mat2) ** t5e ve%1 is %olumn A follo:ed $& %olumn '
ve%2#float/ float) ** initiali"es a ve%2 :it5 2 floats
ive%4#int/ int/ int) ** initiali"es an ive%4 :it5 4 ints
$ve%1#int/ int/ float/ float) ** uses 1 -oolean %onversions
ve%2#ve%4) ** drops t5e t5ird %omponent of a ve%4
ve%4#ve%1) ** drops t5e fourt5 %omponent of a ve%1
ve%4#ve%2/ float) ** ve%46x . ve%26x/ ve%46& . ve%26&/ ve%46" . float
ve%4#float/ ve%2) ** ve%46x . float/ ve%46& . ve%26x/ ve%46" . ve%26&
ve%1#ve%4/ float)
ve%1#float/ ve%4)
ve%1#ve%2/ ve%2)
'ome e1am.les o" these are:
ve%1 %olor . ve%1#A6A/ '6A/ A6A/ '6A))
ve%1 rg$a . ve%1#'6A)) ** sets ea%5 %omponent to '6A
ve%4 rg$ . ve%4#%olor)) ** drop t5e 1t5 %omponent
-o initiali@e the diagonal o" a matri1 with all other elements set to @ero:
mat2#float)
mat4#float)
mat1#float)
-hat is, result"i#";# is set to the "loat argument "or all i = ; and set to 7 "or all i ;.
51
5 Operators and ()pressions
-o initiali@e a matri1 6( s.eci"(ing vectors or scalars, the com.onents are assigned to the matri1 elements
in column;ma?or order.
mat2#ve%2/ ve%2)) ** one %olumn per argument
mat4#ve%4/ ve%4/ ve%4)) ** one %olumn per argument
mat1#ve%1/ ve%1/ ve%1/ ve%1)) ** one %olumn per argument
mat4x2#ve%2/ ve%2/ ve%2)) ** one %olumn per argument
mat2#float/ float/ ** first %olumn
float/ float)) ** se%ond %olumn
mat4#float/ float/ float/ ** first %olumn
float/ float/ float/ ** se%ond %olumn
float/ float/ float)) ** t5ird %olumn
mat1#float/ float/ float/ float/ ** first %olumn
float/ float/ float/ float/ ** se%ond %olumn
float/ float/ float/ float/ ** t5ird %olumn
float/ float/ float/ float)) ** fourt5 %olumn
mat2x4#ve%2/ float/ ** first %olumn
ve%2/ float)) ** se%ond %olumn
A wide range o" other .ossi6ilities e1ist, to construct a matri1 "rom vectors and scalars, as long as enough
com.onents are .resent to initiali@e the matri1. -o construct a matri1 "rom a matri1:
mat4x4#mat1x1)) ** ta3es t5e upperFleft 4x4 of t5e mat1x1
mat2x4#mat1x2)) ** ta3es t5e upperFleft 2x2 of t5e mat1x1/ last ro: is A/A
mat1x1#mat4x4)) ** puts t5e mat4x4 in t5e upperFleft/ sets t5e lo:er rig5t
** %omponent to '/ and t5e rest to A
.#." Structure Constructors
*nce a structure is de"ined, and its t(.e is given a name, a constructor is availa6le with the same name to
construct instances o" that structure. )or e1am.le:
stru%t lig5t ;
float intensit&)
ve%4 position)
<)
lig5t lig5tVar . lig5t#46A/ ve%4#'6A/ 26A/ 46A)))
-he arguments to the constructor will 6e used to set the structureIs "ields, in order, using one argument .er
"ield. +ach argument must 6e the same t(.e as the "ield it sets, or 6e a t(.e that can 6e converted to the
"ieldIs t(.e according to 'ection &.1.17 EIm.licit !onversions.F
'tructure constructors can 6e used as initiali@ers or in e1.ressions.
52
5 Operators and ()pressions
.#.# .rra$ Constructors
Arra( t(.es can also 6e used as constructor names, which can then 6e used in e1.ressions or initiali@ers.
)or e1am.le,
%onst float %>4? . float>4?#76A/ D62/ '6'))
%onst float d>4? . float>?#76A/ D62/ '6'))
float g)
666
float a>7? . float>7?#g/ '/ g/ 264/ g))
float $>4?)
$ . float>4?#g/ g H '6A/ g H 26A))
-here must 6e e1actl( the same num6er o" arguments as the si@e o" the arra( 6eing constructed. I" no si@e
is .resent in the constructor, then the arra( is e1.licitl( si@ed to the num6er o" arguments .rovided. -he
arguments are assigned in order, starting at element 7, to the elements o" the constructed arra(. +ach
argument must 6e the same t(.e as the element t(.e o" the arra(, or 6e a t(.e that can 6e converted to the
element t(.e o" the arra( according to 'ection &.1.17 EIm.licit !onversions.F
. *ector Components
-he names o" the com.onents o" a vector are denoted 6( a single letter. As a notational convenience,
several letters are associated with each com.onent 6ased on common usage o" .osition, color or te1ture
coordinate vectors. -he individual com.onents o" a vector can 6e selected 6( "ollowing the varia6le
name with .eriod B . C and then the com.onent name.
-he com.onent names su..orted are:
>x, ., 5, 4? 5se"ul when accessing vectors that re.resent .oints or normals
>r, g, -, a? 5se"ul when accessing vectors that re.resent colors
>s, t, ), 6? 5se"ul when accessing vectors that re.resent te1ture coordinates
-he com.onent names x, r, and s are, "or e1am.le, s(non(ms "or the same B"irstC com.onent in a vector.
<ote that the third com.onent o" the te1ture coordinate set, r in *.en/0, has 6een renamed ) so as to
avoid the con"usion with r B"or redC in a color.
Accessing com.onents 6e(ond those declared "or the vector t(.e is an error so, "or e1am.le:
ve%2 pos)
pos6x ** is legal
pos6" ** is illegal
53
5 Operators and ()pressions
-he com.onent selection s(nta1 allows multi.le com.onents to 6e selected 6( a..ending their names
B"rom the same name setC a"ter the .eriod B . C.
ve%1 v1)
v16rg$a) ** is a ve%1 and t5e same as 0ust using v1/
v16rg$) ** is a ve%4/
v16$) ** is a float/
v16x&) ** is a ve%2/
v16xg$a) ** is illegal F t5e %omponent names do not %ome from
** t5e same set6
-he order o" the com.onents can 6e di""erent to swi@@le them, or re.licated:
ve%1 pos . ve%1#'6A/ 26A/ 46A/ 16A))
ve%1 s:i". pos6:"&x) ** s:i" . #16A/ 46A/ 26A/ '6A)
ve%1 dup . pos6xx&&) ** dup . #'6A/ '6A/ 26A/ 26A)
-his notation is more concise than the constructor s(nta1. -o "orm an r;value, it can 6e a..lied to an(
e1.ression that results in a vector r;value.
-he com.onent grou. notation can occur on the le"t hand side o" an e1.ression.
ve%1 pos . ve%1#'6A/ 26A/ 46A/ 16A))
pos6x: . ve%2#76A/ E6A)) ** pos . #76A/ 26A/ 46A/ E6A)
pos6:x . ve%2#D6A/ I6A)) ** pos . #I6A/ 26A/ 46A/ D6A)
pos6xx . ve%2#46A/ 16A)) ** illegal F BxB used t:i%e
pos6x& . ve%4#'6A/ 26A/ 46A)) ** illegal F mismat%5 $et:een ve%2 and ve%4
-o "orm an l;value, swi@@ling must 6e a..lied to an l;value o" vector t(.e, contain no du.licate
com.onents, and it results in an l;value o" scalar or vector t(.e, de.ending on num6er o" com.onents
s.eci"ied.
Arra( su6scri.ting s(nta1 can also 6e a..lied to vectors to .rovide numeric inde1ing. 'o in
ve%1 pos)
)os"2# re"ers to the third element o" .os and is e=uivalent to )os.5. -his allows varia6le inde1ing into a
vector, as well as a generic wa( o" accessing com.onents. An( integer e1.ression can 6e used as the
su6scri.t. -he "irst com.onent is at inde1 @ero. Reading "rom or writing to a vector using a constant
integral e1.ression with a value that is negative or greater than or e=ual to the si@e o" the vector is illegal.
Hhen inde1ing with non;constant e1.ressions, 6ehavior is unde"ined i" the inde1 is negative, or greater
than or e=ual to the si@e o" the vector.
54
5 Operators and ()pressions
.! 5atri+ Components
-he com.onents o" a matri1 can 6e accessed using arra( su6scri.ting s(nta1. A..l(ing a single su6scri.t
to a matri1 treats the matri1 as an arra( o" column vectors, and selects a single column, whose t(.e is a
vector o" the same si@e as the matri1. -he le"tmost column is column 7. A second su6scri.t would then
o.erate on the resulting vector, as de"ined earlier "or vectors. ,ence, two su6scri.ts select a column and
then a row.
mat1 m)
m>'? . ve%1#26A)) ** sets t5e se%ond %olumn to all 26A
m>A?>A? . '6A) ** sets t5e upper left element to '6A
m>2?>4? . 26A) ** sets t5e 1t5 element of t5e t5ird %olumn to 26A
Behavior is unde"ined when accessing a com.onent outside the 6ounds o" a matri1 with a non;constant
e1.ression. It is an error to access a matri1 with a constant e1.ression that is outside the 6ounds o" the
matri1.
.2 Structure and .rra$ Operations
-he "ields o" a structure and the lengt method o" an arra( are selected using the .eriod B . C.
In total, onl( the "ollowing o.erators are allowed to o.erate on arra(s and structures as whole entities:
"ield or method selector .
e=ualit( == >=
assignment =
inde1ing Barra(s onl(C Y Z
-he e=ualit( o.erators and assignment o.erator are onl( allowed i" the two o.erands are same si@e and
t(.e. 'tructure t(.es must 6e o" the same declared structure. Both arra( o.erands must 6e e1.licitl(
si@ed. Hhen using the e=ualit( o.erators, two structures are e=ual i" and onl( i" all the "ields are
com.onent;wise e=ual, and two arra(s are e=ual i" and onl( i" all the elements are element;wise e=ual.
Arra( elements are accessed using the arra( su6scri.t o.erator B 5 6 C. An e1am.le o" accessing an arra(
element is
diffuseColor H. lig5tIntensit&>4? J NdotL)
Arra( indices start at @ero. Arra( elements are accessed using an e1.ression whose t(.e is int or uint.
Behavior is unde"ined i" a shader su6scri.ts an arra( with an inde1 less than 7 or greater than or e=ual to
the si@e the arra( was declared with.
55
5 Operators and ()pressions
Arra(s can also 6e accessed with the method o.erator B . C and the lengt method to =uer( the si@e o" the
arra(:
lig5tIntensit&6lengt5#) ** return t5e si"e of t5e arra&
.3 .ssignments
Assignments o" values to varia6le names are done with the assignment o.erator B = C:
lvalueFexpression . rvalueFexpression
-he lvalue-ex)ression evaluates to an l;value. -he assignment o.erator stores the value o" rvalue-
ex)ression into the l;value and returns an r;value with the t(.e and .recision o" lvalue-ex)ression. -he
lvalue-ex)ression and rvalue-ex)ression must have the same t(.e, or the e1.ression must have a t(.e in
the ta6le in 'ection &.1.17 EIm.licit !onversionsF that converts to the t(.e o" lvalue-ex)ression, in which
case an im.licit conversion will 6e done on the rvalue-ex)ression 6e"ore the assignment is done. An(
other desired t(.e;conversions must 6e s.eci"ied e1.licitl( via a constructor. 0;values must 6e writa6le.
Varia6les that are 6uilt;in t(.es, entire structures or arra(s, structure "ields, l;values with the "ield selector
B . C a..lied to select com.onents or swi@@les without re.eated "ields, l;values within .arentheses, and l;
values dere"erenced with the arra( su6scri.t o.erator B 5 6 C are all l;values. *ther 6inar( or unar(
e1.ressions, "unction names, swi@@les with re.eated "ields, and constants cannot 6e l;values. -he ternar(
o.erator BA?C is also not allowed as an l;value.
+1.ressions on the le"t o" an assignment are evaluated 6e"ore e1.ressions on the right o" the assignment.
-he other assignment o.erators are
add into B/=C
su6tract "rom B*=C
multi.l( into B1=C
divide into B0=C
modulus into B2=C
le"t shi"t 6( B33=C
right shi"t 6( B44=C
and into B;=C
inclusive;or into B:=C
e1clusive;or into B9=C
56
5 Operators and ()pressions
where the general e1.ression
lvalue op. expression
is e=uivalent to
lvalue . lvalue op expression
where o) is as descri6ed 6elow, and the l;value and e1.ression must satis"( the semantic re=uirements o"
6oth o) and e=uals B=C.
Reading a varia6le 6e"ore writing Bor initiali@ingC it is legal, however the value is unde"ined.
.9 '+pressions
+1.ressions in the shading language are 6uilt "rom the "ollowing:
!onstants o" t(.e bool, int, uint, float, all vector t(.es, and all matri1 t(.es.
!onstructors o" all t(.es.
Varia6le names o" all t(.es.
An arra( name with the length method a..lied.
'u6scri.ted arra( names.
)unction calls that return values.
!om.onent "ield selectors and arra( su6scri.t results.
2arenthesi@ed e1.ression. An( e1.ression can 6e .arenthesi@ed. 2arentheses can 6e used to grou.
o.erations. *.erations within .arentheses are done 6e"ore o.erations across .arentheses.
-he arithmetic 6inar( o.erators add B/C, su6tract B*C, multi.l( B1C, and divide B0C o.erate on integer and
"loating;.oint scalars, vectors, and matrices. I" one o.erand is "loating;.oint 6ased and the other is
not, then the conversions "rom 'ection &.1.17 EIm.licit !onversionsF are a..lied to the non;"loating;
.oint;6ased o.erand. I" the o.erands are integer t(.es, the( must 6oth 6e signed or 6oth 6e unsigned.
All arithmetic 6inar( o.erators result in the same "undamental t(.e Bsigned integer, unsigned integer,
or "loating;.ointC 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 matri1. In this case, the scalar o.eration is
a..lied inde.endentl( to each com.onent o" the vector or matri1, resulting in the same si@e vector
or matri1.
-he two o.erands are vectors o" the same si@e. In this case, the o.eration is done com.onent;wise
resulting in the same si@e vector.
-he o.erator is add B/C, su6tract B*C, or divide B0C, and the o.erands are matrices with the same
num6er o" rows and the same num6er o" columns. In this case, the o.eration is done com.onent;
wise resulting in the same si@e matri1.
57
5 Operators and ()pressions
-he o.erator is multi.l( B1C, where 6oth o.erands are matrices or one o.erand is a vector and the
other a matri1. A right vector o.erand is treated as a column vector and a le"t vector o.erand as a
row vector. In all these cases, it is re=uired that the num6er o" columns o" the le"t o.erand is e=ual
to the num6er o" rows o" the right o.erand. -hen, the multi.l( B1C o.eration does a linear
alge6raic multi.l(, (ielding an o6?ect that has the same num6er o" rows as the le"t o.erand and the
same num6er o" columns as the right o.erand. 'ection $.17 EVector and 8atri1 *.erationsF
e1.lains in more detail how vectors and matrices are o.erated on.
All other cases are illegal.
Dividing 6( @ero does not cause an e1ce.tion 6ut does result in an uns.eci"ied value. 5se the 6uilt;in
"unctions dot, cross, matrixComp'ult, and outerRroduct, to get, res.ectivel(, vector dot .roduct,
vector cross .roduct, matri1 com.onent;wise multi.lication, and the matri1 .roduct o" a column
vector times a row vector.
-he o.erator modulus B2C o.erates on signed or unsigned integers or integer vectors. -he o.erand
t(.es must 6oth 6e signed or 6oth 6e unsigned. -he o.erands cannot 6e vectors o" di""ering si@e. I"
one o.erand is a scalar and the other vector, then the scalar is a..lied com.onent;wise to the vector,
resulting in the same t(.e as the vector. I" 6oth are vectors o" the same si@e, the result is com.uted
com.onent;wise. -he resulting value is unde"ined "or an( com.onent com.uted with a second
o.erand that is @ero, while results "or other com.onents with non;@ero second o.erands remain
de"ined. I" 6oth o.erands are non;negative, then the remainder is non;negative. Results are unde"ined
i" one or 6oth o.erands are negative. -he o.erator modulus B2C is not de"ined "or an( other data
t(.es Bnon;integer t(.esC.
-he arithmetic unar( o.erators negate B;C, .ost; and .re;increment and decrement B** and //C o.erate
on integer or "loating;.oint values Bincluding vectors and matricesC. All unar( o.erators work
com.onent;wise on their o.erands. -hese result with the same t(.e the( o.erated on. )or .ost; and
.re;increment and decrement, the e1.ression must 6e one that could 6e assigned to Ban l;valueC. 2re;
increment and .re;decrement add or su6tract 1 or 1.7 to the contents o" the e1.ression the( o.erate on,
and the value o" the .re;increment or .re;decrement e1.ression is the resulting value o" that
modi"ication. 2ost;increment and .ost;decrement e1.ressions add or su6tract 1 or 1.7 to the contents
o" the e1.ression the( o.erate on, 6ut the resulting e1.ression has the e1.ressionJs value 6e"ore the
.ost;increment or .ost;decrement was e1ecuted.
-he relational o.erators greater than B4C, less than B3C, greater than or e=ual B4=C, and less than or
e=ual B3=C o.erate onl( on scalar integer and scalar "loating;.oint e1.ressions. -he result is scalar
Boolean. +ither the o.erandsJ t(.es must match, or the conversions "rom 'ection &.1.17 EIm.licit
!onversionsF will 6e a..lied to the integer o.erand, a"ter which the t(.es must match. -o do
com.onent;wise relational com.arisons on vectors, use the 6uilt;in "unctions lessCan,
lessCanLKual, greaterCan, and greaterCanLKual.
-he e=ualit( o.erators eKual "==C, and not e=ual B>=C o.erate on all t(.es. -he( result in a scalar
Boolean. I" the o.erand t(.es do not match, then there must 6e a conversion "rom 'ection &.1.17
EIm.licit !onversionsF a..lied to one o.erand that can make them match, in which case this
conversion is done. )or vectors, matrices, structures, and arra(s, all com.onents, "ields, or elements o"
one o.erand must e=ual the corres.onding com.onents, "ields, or elements in the other o.erand "or the
o.erands to 6e considered e=ual. -o get a vector o" com.onent;wise e=ualit( results "or vectors, use
the 6uilt;in "unctions eKual and notLKual.
5
5 Operators and ()pressions
-he logical 6inar( o.erators and B;;C, or B : : C, and e1clusive or B99C o.erate onl( on two Boolean
e1.ressions and result in a Boolean e1.ression. And B;;C will onl( evaluate the right hand o.erand
i" the le"t hand o.erand evaluated to true. *r B : : C will onl( evaluate the right hand o.erand i" the le"t
hand o.erand evaluated to false. +1clusive or B99C will alwa(s evaluate 6oth o.erands.
-he logical unar( o.erator not B>C. It o.erates onl( on a Boolean e1.ression and results in a Boolean
e1.ression. -o o.erate on a vector, use the 6uilt;in "unction not.
-he se=uence B , C o.erator that o.erates on e1.ressions 6( returning the t(.e and value o" the right;
most e1.ression in a comma se.arated list o" e1.ressions. All e1.ressions are evaluated, in order,
"rom le"t to right.
-he ternar( selection o.erator BA?C. It o.erates on three e1.ressions Bex)1 A ex)2 ? ex)@C. -his
o.erator evaluates the "irst e1.ression, which must result in a scalar Boolean. I" the result is true, it
selects to evaluate the second e1.ression, otherwise it selects to evaluate the third e1.ression. *nl(
one o" the second and third e1.ressions is evaluated. -he second and third e1.ressions can 6e an(
t(.e, as long their t(.es match, or there is a conversion in 'ection &.1.17 EIm.licit !onversionsF that
can 6e a..lied to one o" the e1.ressions to make their t(.es match. -his resulting matching t(.e is the
t(.e o" the entire e1.ression.
-he oneIs com.lement o.erator B<C. -he o.erand must 6e o" t(.e signed or unsigned integer or integer
vector, and the result is the oneIs com.lement o" its o.erandG each 6it o" each com.onent is
com.lemented, including an( sign 6its.
-he shi"t o.erators B33C and B44C. )or 6oth o.erators, the o.erands must 6e signed or unsigned
integers or integer vectors. *ne o.erand can 6e signed while the other is unsigned. In all cases, the
resulting t(.e will 6e the same t(.e as the le"t o.erand. I" the "irst o.erand is a scalar, the second
o.erand has to 6e a scalar as well. I" the "irst o.erand is a vector, the second o.erand must 6e a scalar
or a vector, and the result is com.uted com.onent;wise. -he result is unde"ined i" the right o.erand is
negative, or greater than or e=ual to the num6er o" 6its in the le"t e1.ressionIs 6ase t(.e. -he value o"
+1 PP + is +1 Binter.reted as a 6it .atternC le"t;shi"ted 6( + 6its. -he value o" +1 QQ + is +1 right;
shi"ted 6( + 6it .ositions. I" +1 is a signed integer, the right;shi"t will e1tend the sign 6it. I" +1 is an
unsigned integer, the right;shi"t will @ero;e1tend.
-he 6itwise o.erators and B;C, e1clusive;or B9C, and inclusive;or B:C. -he o.erands must 6e o" t(.e
signed or unsigned integers or integer vectors. -he o.erands cannot 6e vectors o" di""ering si@e. I" one
o.erand is a scalar and the other a vector, the scalar is a..lied com.onent;wise to the vector, resulting
in the same t(.e as the vector. -he "undamental t(.es o" the o.erands Bsigned or unsignedC must
match, and will 6e the resulting "undamental t(.e. )or and B;C, the result is the 6itwise;and "unction
o" the o.erands. )or e1clusive;or B9C, the result is the 6itwise e1clusive;or "unction o" the o.erands.
)or inclusive;or B:C, the result is the 6itwise inclusive;or "unction o" the o.erands.
)or a com.lete s.eci"ication o" the s(nta1 o" e1.ressions, see 'ection 9 E'hading 0anguage /rammar.F
5!
5 Operators and ()pressions
.16 *ector and 5atri+ Operations
Hith a "ew e1ce.tions, o.erations are com.onent;wise. 5suall(, when an o.erator o.erates on a vector or
matri1, it is o.erating inde.endentl( on each com.onent o" the vector or matri1, in a com.onent;wise
"ashion. )or e1am.le,
ve%4 v/ u)
float f)
v . u H f)
will 6e e=uivalent to
v6x . u6x H f)
v6& . u6& H f)
v6" . u6" H f)
And
ve%4 v/ u/ :)
: . v H u)
will 6e e=uivalent to
:6x . v6x H u6x)
:6& . v6& H u6&)
:6" . v6" H u6")
and likewise "or most o.erators and all integer and "loating .oint vector and matri1 t(.es. -he e1ce.tions
are matri1 multi.lied 6( vector, vector multi.lied 6( matri1, and matri1 multi.lied 6( matri1. -hese do
not o.erate com.onent;wise, 6ut rather .er"orm the correct linear alge6raic multi.l(.
ve%4 v/ u)
mat4 m)
u . v J m)
is e=uivalent to
u6x . dot#v/ m>A?)) ** m>A? is t5e left %olumn of m
u6& . dot#v/ m>'?)) ** dot#a/$) is t5e inner #dot) produ%t of a and $
u6" . dot#v/ m>2?))
And
u . m J v)
is e=uivalent to
u6x . m>A?6x J v6x H m>'?6x J v6& H m>2?6x J v6")
u6& . m>A?6& J v6x H m>'?6& J v6& H m>2?6& J v6")
u6" . m>A?6" J v6x H m>'?6" J v6& H m>2?6" J v6")
6"
5 Operators and ()pressions
And
mat4 m/ n/ r)
r . m J n)
is e=uivalent to
r>A?6x . m>A?6x J n>A?6x H m>'?6x J n>A?6& H m>2?6x J n>A?6")
r>'?6x . m>A?6x J n>'?6x H m>'?6x J n>'?6& H m>2?6x J n>'?6")
r>2?6x . m>A?6x J n>2?6x H m>'?6x J n>2?6& H m>2?6x J n>2?6")
r>A?6& . m>A?6& J n>A?6x H m>'?6& J n>A?6& H m>2?6& J n>A?6")
r>'?6& . m>A?6& J n>'?6x H m>'?6& J n>'?6& H m>2?6& J n>'?6")
r>2?6& . m>A?6& J n>2?6x H m>'?6& J n>2?6& H m>2?6& J n>2?6")
r>A?6" . m>A?6" J n>A?6x H m>'?6" J n>A?6& H m>2?6" J n>A?6")
r>'?6" . m>A?6" J n>'?6x H m>'?6" J n>'?6& H m>2?6" J n>'?6")
r>2?6" . m>A?6" J n>2?6x H m>'?6" J n>2?6& H m>2?6" J n>2?6")
and similarl( "or other si@es o" vectors and matrices.
61
! Statements and Structure
-he "undamental 6uilding 6locks o" the *.en/0 'hading 0anguage are:
statements and declarations
"unction de"initions
selection Bif*else and switc*case*default#
iteration "for, wile, and do*wile#
?um.s "discard, return, break, and continueC
-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 6odies. )unction 6odies are de"ined as
/unction-!e/inition:
/unction-)rotot.)e > statement-list ?
statement-list:
statement
statement-list statement
statement:
com)oun!-statement
sim)le-statement
!url( 6races are used to grou. se=uences o" statements into com.ound statements.
com)oun!-statement:
> statement-list ?
sim)le-statement:
!eclaration-statement
ex)ression-statement
selection-statement
62
6 State*ents and Structure
iteration-statement
;um)-statement
'im.le declaration, e1.ression, and ?um. statements end in a semi;colon.
-his a6ove is slightl( sim.li"ied, and the com.lete grammar s.eci"ied in 'ection 9 E'hading 0anguage
/rammarF should 6e used as the de"initive s.eci"ication.
Declarations and e1.ressions have alread( 6een discussed.
!.1 %unction )efinitions
As indicated 6( the grammar a6ove, a valid shader is a se=uence o" glo6al declarations and "unction
de"initions. A "unction is declared as the "ollowing e1am.le shows:
** protot&pe
returnT&pe fun%tionName #t&peA argA/ t&pe' arg'/ 666/ t&pen argn))
and a "unction is de"ined like
** definition
returnT&pe fun%tionName #t&peA argA/ t&pe' arg'/ 666/ t&pen argn)
;
** do some %omputation
return returnValue)
<
where returnT.)e must 6e .resent and include a t(.e. +ach o" the t.)e0 must include a t(.e and can
o.tionall( include a .arameter =uali"ier andAor const.
A "unction is called 6( using its name "ollowed 6( a list o" arguments in .arentheses.
Arra(s are allowed as arguments and as the return t(.e. In 6oth cases, the arra( must 6e e1.licitl( si@ed.
An arra( is .assed or returned 6( using ?ust its name, without 6rackets, and the si@e o" the arra( must
match the si@e s.eci"ied in the "unctionIs declaration.
'tructures are also allowed as argument t(.es. -he return t(.e can also 6e structure.
'ee 'ection 9 E'hading 0anguage /rammarF "or the de"initive re"erence on the s(nta1 to declare and
de"ine "unctions.
All "unctions must 6e either declared with a .rotot(.e or de"ined with a 6od( 6e"ore the( are called. )or
e1am.le:
float m&fun% #float f/ ** f is an input parameter
out float g)) ** g is an output parameter
)unctions that return no value must 6e declared as void. )unctions that acce.t no in.ut arguments need
not use void in the argument list 6ecause .rotot(.es Bor de"initionsC are re=uired and there"ore there is no
am6iguit( when an em.t( argument list VB CV is declared. -he idiom EBvoidCF as a .arameter list is
.rovided "or convenience.
63
6 State*ents and Structure
)unction names can 6e overloaded. -he same "unction name can 6e used "or multi.le "unctions, as long
as the .arameter t(.es di""er. I" a "unction name is declared twice with the same .arameter t(.es, then the
return t(.es and all =uali"iers must also match, and it is the same "unction 6eing declared. Hhen "unction
calls are resolved, an e1act t(.e match "or all the arguments is sought. I" an e1act match is "ound, all
other "unctions are ignored, and the e1act match is used. I" no e1act match is "ound, then the im.licit
conversions in 'ection &.1.17 EIm.licit !onversionsF will 6e a..lied to "ind a match. 8ismatched t(.es
on in.ut .arameters Bin or inout or de"ault# must have a conversion "rom the calling argument t(.e to the
"ormal .arameter t(.e. 8ismatched t(.es on out.ut .arameters Bout or inoutC must have a conversion
"rom the "ormal .arameter t(.e to the calling argument t(.e. Hhen argument conversions are used to "ind
a match, it is a semantic error i" there are multi.le wa(s to a..l( these conversions to make the call match
more than one "unction.
)or e1am.le,
ve%1 f#in ve%1 x/ out ve%1 &))
ve%1 f#in ve%1 x/ out ive%1 &)) ** o3a&/ different argument t&pe
int f#in ve%1 x/ out ive%1 &)) ** error/ onl& return t&pe differs
ve%1 f#in ve%1 x/ in ive%1 &)) ** error/ onl& =ualifier differs
int f#%onst in ve%1 x/ out ive%1 &)) ** error/ onl& =ualifier differs
!alling the "irst two "unctions a6ove with the "ollowing argument t(.es (ields
f#ve%1/ ve%1) ** exa%t mat%5 of ve%1 f#in ve%1 x/ out ve%1 &)
f#ve%1/ ive%1) ** exa%t mat%5 of ve%1 f#in ve%1 x/ out ive%1 &)
f#ive%1/ ve%1) ** error/ %onverti$le to $ot5
f#ive%1/ ive%1) ** o3a&/ %onverti$le onl& to ve%1 f#in ve%1 x/ out ive%1 &)
5ser;de"ined "unctions can have multi.le declarations, 6ut onl( one de"inition. A shader can rede"ine
6uilt;in "unctions. I" a 6uilt;in "unction is redeclared in a shader Bi.e., a .rotot(.e is visi6leC 6e"ore a call
to it, then the linker will onl( attem.t to resolve that call within the set o" shaders that are linked with it.
-he "unction main is used as the entr( .oint to a shader e1ecuta6le. A shader need not contain a "unction
named main, 6ut one shader in a set o" shaders linked together to "orm a single shader e1ecuta6le must.
-his "unction takes no arguments, returns no value, and must 6e declared as t(.e void?
void main#)
;
666
<
-he "unction main can contain uses o" return. 'ee 'ection #.& EJum.sF "or more details.
It is an error to declare or de"ine a "unction main with an( other .arameters or return t(.e.
!.1.1 %unction Calling Conventions
)unctions are called 6( value;return. -his means in.ut arguments are co.ied into the "unction at call time,
and out.ut arguments are co.ied 6ack to the caller 6e"ore "unction e1it. Because the "unction works with
local co.ies o" .arameters, there are no issues regarding aliasing o" varia6les within a "unction. -o
control what .arameters are co.ied in andAor out through a "unction de"inition or declaration:
-he ke(word in is used as a =uali"ier to denote a .arameter is to 6e co.ied in, 6ut not co.ied out.
64
6 State*ents and Structure
-he ke(word out is used as a =uali"ier to denote a .arameter is to 6e co.ied out, 6ut not co.ied in.
-his should 6e used whenever .ossi6le to avoid unnecessaril( co.(ing .arameters in.
-he ke(word inout is used as a =uali"ier to denote the .arameter is to 6e 6oth co.ied in and co.ied
out.
A "unction .arameter declared with no such =uali"ier means the same thing as s.eci"(ing in.
All arguments are evaluated at call time, e1actl( once, in order, "rom le"t to right. +valuation o" an in
.arameter results in a value that is co.ied to the "ormal .arameter. +valuation o" an out .arameter results
in an l;value that is used to co.( out a value when the "unction returns. +valuation o" an inout .arameter
results in 6oth a value and an l;valueG the value is co.ied to the "ormal .arameter at call time and the l;
value is used to co.( out a value when the "unction returns.
-he order in which out.ut .arameters are co.ied 6ack to the caller is unde"ined.
I" the "unction matching descri6ed in the .revious section re=uired argument t(.e conversions, these
conversions are a..lied at co.(;in and co.(;out times.
In a "unction, writing to an in.ut;onl( .arameter is allowed. *nl( the "unctionJs co.( is modi"ied. -his
can 6e .revented 6( declaring a .arameter with the const =uali"ier.
Hhen calling a "unction, e1.ressions that do not evaluate to l;values cannot 6e .assed to .arameters
declared as out or inout.
<o =uali"ier is allowed on the return t(.e o" a "unction.
/unction-)rotot.)e :
)recision-6uali/ier t.)e /unction-name8const-6uali/ier )arameter-6uali/ier )recision-6uali/ier
t.)e name arra.-s)eci/ier, ... 9
t.)e :
an( 6asic t(.e, arra( t(.e, structure name, or structure !e/inition
const-6uali/ier :
em.t(
const
)arameter-6uali/ier :
em.t(
in
out
inout
name :
em.t(
identi"ier
arra.-s)eci/ier :
em.t(
5 integral-constant-ex)ression 6
65
6 State*ents and Structure
,owever, the const =uali"ier cannot 6e used with out or inout. -he a6ove is used "or "unction
declarations Bi.e., .rotot(.esC and "or "unction de"initions. ,ence, "unction de"initions can have unnamed
arguments.
Recursion is not allowed, not even staticall(. 'tatic recursion is .resent i" the static "unction call gra.h o"
the .rogram contains c(cles.
!.2 Selection
!onditional control "low in the shading language is done 6( either if, if;else, or switc statements:
selection-statement :
if B -ool-ex)ression C statement
if B -ool-ex)ression C statement else statement
switc B init-ex)ression C [ s4itc'-statement-listo)t

\
Hhere s4itc'-statement-list is a list o" @ero or more s4itc'-statement and other statements de"ined 6( the
language, where s4itc'-statement adds some "orms o" la6els. -hat is
s4itc'-statement-list :
s4itc'-statement
s4itc'-statement-list s4itc'-statement
s4itc'-statement :
case constant-ex)ression ?
default ?
statement
I" an if*e1.ression evaluates to true, then the "irst statement is e1ecuted. I" it evaluates to false and there
is an else .art then the second statement is e1ecuted.
An( e1.ression whose t(.e evaluates to a Boolean can 6e used as the conditional e1.ression -ool-
ex)ression. Vector t(.es are not acce.ted as the e1.ression to if.
!onditionals can 6e nested.
-he t(.e o" init-ex)ression in a switch statement must 6e a scalar integer. I" a case la6el has a constant-
ex)ression o" e=ual value, then e1ecution will continue a"ter that la6el. *therwise, i" there is a default
la6el, e1ecution will continue a"ter that la6el. *therwise, e1ecution ski.s the rest o" the switch statement.
It is an error to have more than one default or a re.licated constant-ex)ression. A break statement not
nested in a loo. or other switch statement Beither not nested or nested onl( in if or if;else statementsC will
also ski. the rest o" the switch statement. )all through la6els are allowed, 6ut it is an error to have no
statement 6etween a la6el and the end o" the switc statement. <o statements are allowed in a switch
statement 6e"ore the "irst case statement.
<o case or default la6els can 6e nested inside other "low control nested within their corres.onding
switc.
66
6 State*ents and Structure
!." Iteration
)or, while, and do loo.s are allowed as "ollows:
for #initFexpression) %onditionFexpression) loopFexpression)
su$Fstatement
:5ile #%onditionFexpression)
su$Fstatement
do
statement
:5ile #%onditionFexpression)
'ee 'ection 9 E'hading 0anguage /rammarF "or the de"initive s.eci"ication o" loo.s.
-he for loo. "irst evaluates the init-ex)ression, then the con!ition-ex)ression. I" the con!ition-
ex)ression evaluates to true, then the 6od( o" the loo. is e1ecuted. A"ter the 6od( is e1ecuted, a for loo.
will then evaluate the loo)-ex)ression, and then loo. 6ack to evaluate the con!ition-ex)ression, re.eating
until the con!ition-ex)ression evaluates to "alse. -he loo. is then e1ited, ski..ing its 6od( and ski..ing
its loo)-ex)ression. Varia6les modi"ied 6( the loo)-ex)ression maintain their value a"ter the loo. is
e1ited, .rovided the( are still in sco.e. Varia6les declared in init-ex)ression or con!ition-ex)ression are
onl( in sco.e until the end o" the su6;statement o" the for loo..
-he wile loo. "irst evaluates the con!ition-ex)ression. I" true, then the 6od( is e1ecuted. -his is then
re.eated, until the con!ition-ex)ression evaluates to "alse, e1iting the loo. and ski..ing its 6od(.
Varia6les declared in the con!ition-ex)ression are onl( in sco.e until the end o" the su6;statement o" the
while loo..
-he do*wile loo. "irst e1ecutes the 6od(, then e1ecutes the con!ition-ex)ression. -his is re.eated until
con!ition-ex)ression evaluates to "alse, and then the loo. is e1ited.
+1.ressions "or con!ition-ex)ression must evaluate to a Boolean.
Both the con!ition-ex)ression and the init-ex)ression can declare and initiali@e a varia6le, e1ce.t in the
do*wile loo., which cannot declare a varia6le in its con!ition-ex)ression. -he varia6leJs sco.e lasts
onl( until the end o" the su6;statement that "orms the 6od( o" the loo..
0oo.s can 6e nested.
<on;terminating loo.s are allowed. -he conse=uences o" ver( long or non;terminating loo.s are .lat"orm
de.endent.
67
6 State*ents and Structure
!.# :umps
-hese are the ?um.s:
;um)_statement:
continue@
break@
return@
return ex)ression@
discard@ 00 in the "ragment shader language onl(
-here is no EgotoF nor other non;structured "low o" control.
-he continue ?um. is used onl( in loo.s. It ski.s the remainder o" the 6od( o" the inner most loo. o"
which it is inside. )or wile and do*wile loo.s, this ?um. is to the ne1t evaluation o" the loo.
con!ition-ex)ression "rom which the loo. continues as .reviousl( de"ined. )or for loo.s, the ?um. is to
the loo)-ex)ression, "ollowed 6( the con!ition-ex)ression.
-he break ?um. can also 6e used onl( in loo.s and switch statements. It is sim.l( an immediate e1it o"
the inner;most loo. or switch statements containing the break. <o "urther e1ecution o" con!ition-
ex)ression, loo)-ex)ression, or s4itc'-statement is done.
-he discard ke(word is onl( allowed within "ragment shaders. It can 6e used within a "ragment shader to
a6andon the o.eration on the current "ragment. -his ke(word causes the "ragment to 6e discarded and no
u.dates to an( 6u""ers will occur. !ontrol "low e1its the shader, and su6se=uent im.licit or e1.licit
derivatives are unde"ined when this control "low is non;uni"orm Bmeaning di""erent "ragments within the
.rimitive take di""erent control .athsC. It would t(.icall( 6e used within a conditional statement, "or
e1am.le:
if #intensit& K A6A)
dis%ard)
A "ragment shader ma( test a "ragmentJs al.ha value and discard the "ragment 6ased on that test.
,owever, it should 6e noted that coverage testing occurs a"ter the "ragment shader runs, and the coverage
test can change the al.ha value.
-he return ?um. causes immediate e1it o" the current "unction. I" it has ex)ression then that is the return
value "or the "unction.
-he "unction main can use return. -his sim.l( causes main to e1it in the same wa( as when the end o"
the "unction had 6een reached. It does not im.l( a use o" discard in a "ragment shader. 5sing return in
main 6e"ore de"ining out.uts will have the same 6ehavior as reaching the end o" main 6e"ore de"ining
out.uts.
6
2 -uilt9in *ariables
2.1 *erte+ and Geometr$ Shader Special *ariables
'ome *.en/0 o.erations occur in "i1ed "unctionalit( 6etween the verte1 .rocessor and the "ragment
.rocessor. 'haders communicate with the "i1ed "unctionalit( o" *.en/0 through the use o" 6uilt;in
varia6les.
-he 6uilt;in verte1 shader varia6les "or communicating with "i1ed "unctionalit( are intrinsicall( declared
as "ollows in the verte1 language:
in int gl_VertexI)
in int gl_Instan%eI)
out gl_@erVertex ;
ve%1 gl_@osition)
float gl_@ointSi"e)
float gl_Clipistan%e>?)
<)
In the geometr( language, the s.ecial varia6les are intrinsicall( declared as:
in gl_@erVertex ;
ve%1 gl_@osition)
float gl_@ointSi"e)
float gl_Clipistan%e>?)
< gl_in>?)
in int gl_@rimitiveIIn)
out gl_@erVertex ;
ve%1 gl_@osition)
float gl_@ointSi"e)
float gl_Clipistan%e>?)
<)
out int gl_@rimitiveI)
out int gl_La&er)
5nless otherwise noted elsewhere, these varia6les are onl( availa6le in the verte1 and geometr( languages
as declared a6ove.
6!
7 Bui%t+in #aria$%es
-he varia6le gl_%osition is intended "or writing the homogeneous verte1 .osition. It can 6e written at an(
time during shader e1ecution. -his value will 6e used 6( .rimitive assem6l(, cli..ing, culling, and other
"i1ed "unctionalit( o.erations, i" .resent, that o.erate on .rimitives a"ter verte1 .rocessing has occurred.
Its value is unde"ined a"ter the verte1 .rocessing stage i" the verte1 shader e1ecuta6le does not write
gl_%osition, and it is unde"ined a"ter geometr( .rocessing i" the geometr( e1ecuta6le calls LmitUertexBC
without having written gl_%osition since the last LmitUertexBC Bor hasnIt written it at allC.
-he varia6le gl_%oint+i5e is intended "or a shader to write the si@e o" the .oint to 6e rasteri@ed. It is
measured in .i1els. I" gl_%oint+i5e is not written to, its value is unde"ined in su6se=uent .i.e stages.
-he varia6le gl_Vertex&D is a verte1 shader in.ut varia6le that holds an integer inde1 "or the verte1, as
de"ined under 'hader In.uts in section .11.3 in the *.en/0 /ra.hics '(stem '.eci"ication. Hhile the
varia6le gl_Vertex&D is alwa(s .resent, its value is not alwa(s de"ined.
-he varia6le gl_&nstance&D is a verte1 shader in.ut varia6le that holds the integer inde1 o" the current
.rimitive in an instanced draw call Bsee 'hader In.uts in section .11.3 in the *.en/0 /ra.hics '(stem
'.eci"icationC. I" the current .rimitive does not come "rom an instanced draw call, the value o"
gl_&nstance&D is @ero.
-he varia6le gl_li)Distance .rovides the "orward com.ati6le mechanism "or controlling user cli..ing.
-o use this, a verte1 or geometr( shader is res.onsi6le "or maintaining a set o" cli. .lanes, com.uting the
distance "rom the verte1 to each cli. .lane, and storing distances to the .lane in gl_li)Distance"i# "or
each .lane i. A distance o" 7 means the verte1 is on the .lane, a .ositive distance means the verte1 is
inside the cli. .lane, and a negative distance means the .oint is outside the cli. .lane. -he cli. distances
will 6e linearl( inter.olated across the .rimitive and the .ortion o" the .rimitive with inter.olated
distances less than 7 will 6e cli..ed.
-he gl_li)Distance arra( is .redeclared as unsi@ed and must 6e si@ed 6( the shader either redeclaring it
with a si@e or inde1ing it onl( with integral constant e1.ressions. -his needs to si@e the arra( to include
all the cli. .lanes that are ena6led via the *.en/0 A2IG i" the si@e does not include all ena6led .lanes,
results are unde"ined. -he si@e can 6e at most gl_:axli)Distances. -he num6er o" var(ing com.onents
Bsee gl_:axVar.ingom)onents9 consumed 6( gl_li)Distance will match the si@e o" the arra(, no
matter how man( .lanes are ena6led. -he shader must also set all values in gl_li)Distance that have
6een ena6led via the *.en/0 A2I, or results are unde"ined. Values written into gl_li)Distance "or
.lanes that are not ena6led have no e""ect.
-he in.ut varia6le gl_%rimitive&D&n is availa6le onl( in the geometr( language and is "illed with the
num6er o" .rimitives .rocessed 6( the geometr( shader since the current set o" rendering .rimitives was
started.
-he out.ut varia6le gl_%rimitive&D is availa6le onl( in the geometr( language and .rovides a single
integer that serves as a .rimitive identi"ier. -his is then availa6le to "ragment shaders as the "ragment
in.ut gl_%rimitive&D, which will select the written .rimitive ID "rom the .rovoking verte1 in the .rimitive
6eing shaded. I" a "ragment shader using gl_%rimitive&D is active and a geometr( shader is also active,
the geometr( shader must write to gl_%rimitive&D or the "ragment shader in.ut gl_%rimitive&D is
unde"ined. 'ee section .1.& Bunder /eometr( 'hader *ut.utsC and section %.9. Bunder 'hader In.utsC
o" the *.en/0 /ra.hics '(stem '.eci"ication "or more in"ormation.
7"
7 Bui%t+in #aria$%es
-he out.ut varia6le gl_La.er is availa6le onl( in the geometr( language, and is used to select a s.eci"ic
la(er o" a multi;la(er "rame6u""er attachment. -he actual la(er used will come "rom one o" vertices in the
.rimitive 6eing shaded. Hhich verte1 the la(er comes "rom is unde"ined, so it is 6est to write the same
la(er value "or all vertices o" a .rimitive. I" a shader staticall( assigns a value to gl_La.er, la(ered
rendering mode is ena6led. 'ee section .1.& Bunder /eometr( 'hader *ut.utsC and section &.&.3
0a(ered )rame6u""ers o" the *.en/0 /ra.hics '(stem '.eci"ication "or more in"ormation. I" a shader
staticall( assigns a value to gl_La.er, and there is an e1ecution .ath through the shader that does not set
gl_La.er, then the value o" gl_La.er is unde"ined "or e1ecutions o" the shader that take that .ath.
2.1.1 Compatibilit$ ,rofile *erte+ and Geometr$ Shader Special *ariables
Hhen using the com.ati6ilit( .ro"ile, the "ollowing additional 6uilt;in varia6les are added to the out.ut
gl_%erVertex 6lock in the verte1 language:
out gl_@erVertex ; ** part of t5e gl_@erVertex $lo%3 des%ri$ed in D6'
** in addition to ot5er gl_@erVertex mem$ers666
ve%1 gl_ClipVertex)
<)
Hhen using the com.ati6ilit( .ro"ile, the "ollowing additional 6uilt;in varia6les are added to the in.ut
gl_%erVertex 6lock in the geometr( language:
in gl_@erVertex ; ** part of t5e gl_@erVertex $lo%3 des%ri$ed in D6'
** in addition to ot5er gl_@erVertex mem$ers666
ve%1 gl_ClipVertex)
< gl_in>?)
out ve%1 gl_ClipVertex)
-he varia6le gl_li)Vertex is availa6le onl( in the verte1 and geometr( languages and .rovides a .lace
"or verte1 and geometr( shaders to write the coordinate to 6e used with the user cli..ing .lanes.
/eometr( shaders can read the values written 6( verte1 shaders.
-he user must ensure the cli. verte1 and user cli..ing .lanes are de"ined in the same coordinate s.ace.
5ser cli. .lanes work .ro.erl( onl( under linear trans"orm. It is unde"ined what ha..ens under non;
linear trans"orm.
I" a linked set o" shaders "orming a .rogram contains no static write to gl_li)Vertex or gl_li)Distance,
6ut the a..lication has re=uested cli..ing against user cli. .lanes through the A2I, then the coordinate
written to gl_%osition is used "or com.arison against the user cli. .lanes. Hriting to gl_li)Distance is
the .re"erred method "or user cli..ing. It is an error "or the set o" shaders "orming a .rogram to staticall(
read or write 6oth gl_li)Vertex and gl_li)Distance.
71
7 Bui%t+in #aria$%es
2.2 %ragment Shader Special *ariables
-he 6uilt;in s.ecial varia6les that are accessi6le "rom a "ragment shader are intrinsicall( declared as
"ollows:
in ve%1 gl_FragCoord)
in $ool gl_FrontFa%ing)
in float gl_Clipistan%e>?)
out ve%1 gl_FragColor) ** depre%ated
out ve%1 gl_Fragata>gl_9axra:-uffers?) ** depre%ated
out float gl_Fragept5)
in ve%2 gl_@ointCoord)
in int gl_@rimitiveI)
+1ce.t as noted 6elow, the( 6ehave as other in.ut and out.ut varia6les.
-he out.ut o" the "ragment shader e1ecuta6le is .rocessed 6( the "i1ed "unction o.erations at the 6ack end
o" the *.en/0 .i.eline.
)ragment shaders out.ut values to the *.en/0 .i.eline using the 6uilt;in varia6les gl_$ragolor,
gl_$ragData, and gl_$ragDe)t', unless the discard statement is e1ecuted. Both gl_$ragolor and
gl_$ragData are de.recatedG the .re"erred usage is to e1.licitl( declare these out.uts in the "ragment
shader using the out storage =uali"ier.
-he "i1ed "unctionalit( com.uted de.th "or a "ragment ma( 6e o6tained 6( reading gl_$ragoor!.5,
descri6ed 6elow.
De.recated: Hriting to gl_$ragolor s.eci"ies the "ragment color that will 6e used 6( the su6se=uent
"i1ed "unctionalit( .i.eline. I" su6se=uent "i1ed "unctionalit( consumes "ragment color and an e1ecution
o" the "ragment shader e1ecuta6le does not write a value to gl_$ragolor then the "ragment color
consumed is unde"ined.
Hriting to gl_$ragDe)t' will esta6lish the de.th value "or the "ragment 6eing .rocessed. I" de.th
6u""ering is ena6led, and no shader writes gl_$ragDe)t', then the "i1ed "unction value "or de.th will 6e
used as the "ragmentJs de.th value. I" a shader staticall( assigns a value to gl_$ragDe)t', and there is an
e1ecution .ath through the shader that does not set gl_$ragDe)t', then the value o" the "ragmentJs de.th
ma( 6e unde"ined "or e1ecutions o" the shader that take that .ath. -hat is, i" the set o" linked "ragment
shaders staticall( contain a write to gl_$ragDe)t', then it is res.onsi6le "or alwa(s writing it.
De.recated: -he varia6le gl_$ragData is an arra(. Hriting to gl_$ragData"n# s.eci"ies the "ragment
data that will 6e used 6( the su6se=uent "i1ed "unctionalit( .i.eline "or data n. I" su6se=uent "i1ed
"unctionalit( consumes "ragment data and an e1ecution o" a "ragment shader e1ecuta6le does not write a
value to it, then the "ragment data consumed is unde"ined.
I" a shader staticall( assigns a value to gl_$ragolor, it ma( not assign a value to an( element o"
gl_$ragData. I" a shader staticall( writes a value to an( element o" gl_$ragData, it ma( not assign a
value to gl_$ragolor. -hat is, a shader ma( assign values to either gl_$ragolor or gl_$ragData, 6ut
not 6oth. 8ulti.le shaders linked together must also consistentl( write ?ust one o" these varia6les.
'imilarl(, i" user declared out.ut varia6les are in use Bstaticall( assigned toC, then the 6uilt;in varia6les
gl_$ragolor and gl_$ragData ma( not 6e assigned to. -hese incorrect usages all generate com.ile time
errors.
72
7 Bui%t+in #aria$%es
I" a shader e1ecutes the discard ke(word, the "ragment is discarded, and the values o" an( user;de"ined
"ragment out.uts, gl_$ragDe)t', gl_$ragolor, and gl_$ragData 6ecome irrelevant.
-he varia6le gl_$ragoor! is availa6le as an in.ut varia6le "rom within "ragment shaders and it holds the
window relative coordinates Bx, ., 5, 1A4C values "or the "ragment. I" multi;sam.ling, this value can 6e "or
an( location within the .i1el, or one o" the "ragment sam.les. -he use o" centroid in does not "urther
restrict this value to 6e inside the current .rimitive. -his value is the result o" the "i1ed "unctionalit( that
inter.olates .rimitives a"ter verte1 .rocessing to generate "ragments. -he 5 com.onent is the de.th value
that would 6e used "or the "ragmentJs de.th i" no shader contained an( writes to gl_$ragDe)t'. -his is
use"ul "or invariance i" a shader conditionall( com.utes gl_$ragDe)t' 6ut otherwise wants the "i1ed
"unctionalit( "ragment de.th.
)ragment shaders have access to the in.ut 6uilt;in varia6le gl_$ront$acing, whose value is true i" the
"ragment 6elongs to a "ront;"acing .rimitive. *ne use o" this is to emulate two;sided lighting 6( selecting
one o" two colors calculated 6( a verte1 or geometr( shader.
-he 6uilt;in in.ut varia6le gl_li)Distance arra( contains linearl( inter.olated values "or the verte1
values written 6( a shader to the gl_li)Distance verte1 out.ut varia6le. -his arra( must 6e si@ed either
im.licitl( or e1.licitl( to 6e the same si@e in all shaders. *nl( elements in this arra( that have cli..ing
ena6led will have de"ined values.
-he values in gl_%ointoor! are two;dimensional coordinates indicating where within a .oint .rimitive
the current "ragment is located, when .oint s.rites are ena6led. -he( range "rom 7.7 to 1.7 across the
.oint. I" the current .rimitive is not a .oint, or i" .oint s.rites are not ena6led, then the values read "rom
gl_%ointoor! are unde"ined.
-he in.ut varia6le gl_%rimitive&D holds the ID o" the currentl( .rocessed .rimitive. I" no geometr(
shader is .resent, it is "illed with the num6er o" .rimitives .rocessed 6( the verte1 shader since the current
set o" rendering .rimitives was started. I" a geometr( shader is .resent, it is taken "rom the
gl_%rimitive&D geometr( shader out.ut, as descri6ed in section 3.1 Verte1 and /eometr( 'hader '.ecial
Varia6les.
73
7 Bui%t+in #aria$%es
2." Compatibilit$ ,rofile *erte+ Shader -uilt9In Inputs
-he "ollowing .redeclared in.ut names can 6e used "rom within a verte1 shader to access the current
values o" *.en/0 state when using the com.ati6ilit( .ro"ile.
in ve%1 gl_Color)
in ve%1 gl_Se%ondar&Color)
in ve%4 gl_Normal)
in ve%1 gl_Vertex)
in ve%1 gl_9ultiTexCoordA)
in ve%1 gl_9ultiTexCoord')
in ve%1 gl_9ultiTexCoord2)
in ve%1 gl_9ultiTexCoord4)
in ve%1 gl_9ultiTexCoord1)
in ve%1 gl_9ultiTexCoord7)
in ve%1 gl_9ultiTexCoordE)
in ve%1 gl_9ultiTexCoordD)
in float gl_FogCoord)
2.# -uilt9In Constants
-he "ollowing 6uilt;in constants are .rovided to all shaders. -he actual values used are im.lementation
de.endent, 6ut must 6e at least the value shown. 'ome are de.recated, as indicated in comments.
**
** Implementation dependent %onstants6 T5e example values $elo:
** are t5e minimum values allo:ed for t5ese maximums6
**
%onst int gl_9axVertex8ttri$s . 'E)
%onst int gl_9axVertexLniformComponents . 'A21)
%onst int gl_9axVar&ingFloats . EA) ** epre%ated
%onst int gl_9axVar&ingComponents . EA) ** epre%ated
%onst int gl_9axVertexOutputComponents . E1)
%onst int gl_9ax!eometr&InputComponents . E1)
%onst int gl_9ax!eometr&OutputComponents . '2I)
%onst int gl_9axFragmentInputComponents . '2I)
%onst int gl_9axVertexTextureImageLnits . 'E)
%onst int gl_9axCom$inedTextureImageLnits . 1I)
%onst int gl_9axTextureImageLnits . 'E)
%onst int gl_9axFragmentLniformComponents . 'A21)
%onst int gl_9axra:-uffers . I)
%onst int gl_9axClipistan%es . I)
%onst int gl_9ax!eometr&TextureImageLnits . 'E)
%onst int gl_9ax!eometr&OutputVerti%es . 27E)
%onst int gl_9ax!eometr&TotalOutputComponents . 'A21)
%onst int gl_9ax!eometr&LniformComponents . 'A21)
%onst int gl_9ax!eometr&Var&ingComponents . E1)
-he constant gl_:axVar.ing$loats is de.recated, use gl_:axVar.ingom)onents instead.
74
7 Bui%t+in #aria$%es
2.#.1 Compatibilit$ ,rofile -uilt9In Constants
%onst int gl_9axTextureLnits . 2)
%onst int gl_9axTextureCoords . I)
%onst int gl_9axClip@lanes . I)
2. -uilt9In 4niform State
As an aid to accessing *.en/0 .rocessing state, the "ollowing uni"orm varia6les are 6uilt into the
*.en/0 'hading 0anguage.
**
** ept5 range in :indo: %oordinates/
** se%tion 26'46' in t5e Open!L !rap5i%s S&stem Spe%ifi%ation6
**
stru%t gl_ept5Range@arameters ;
float near) ** n
float far) ** f
float diff) ** f F n
<)
uniform gl_ept5Range@arameters gl_ept5Range)
2..1 Compatibilit$ ,rofile State
-hese varia6les are .resent onl( in the com.ati6ilit( .ro"ile.
**
** %ompati$ilit& profile onl&
**
uniform mat1 gl_9odelVie:9atrix)
uniform mat1 gl_@ro0e%tion9atrix)
uniform mat1 gl_9odelVie:@ro0e%tion9atrix)
uniform mat1 gl_Texture9atrix>gl_9axTextureCoords?)
**
** %ompati$ilit& profile onl&
**
uniform mat4 gl_Normal9atrix) ** transpose of t5e inverse of t5e
** upper leftmost 4x4 of gl_9odelVie:9atrix
uniform mat1 gl_9odelVie:9atrixInverse)
uniform mat1 gl_@ro0e%tion9atrixInverse)
uniform mat1 gl_9odelVie:@ro0e%tion9atrixInverse)
uniform mat1 gl_Texture9atrixInverse>gl_9axTextureCoords?)
uniform mat1 gl_9odelVie:9atrixTranspose)
uniform mat1 gl_@ro0e%tion9atrixTranspose)
uniform mat1 gl_9odelVie:@ro0e%tion9atrixTranspose)
uniform mat1 gl_Texture9atrixTranspose>gl_9axTextureCoords?)
75
7 Bui%t+in #aria$%es
uniform mat1 gl_9odelVie:9atrixInverseTranspose)
uniform mat1 gl_@ro0e%tion9atrixInverseTranspose)
uniform mat1 gl_9odelVie:@ro0e%tion9atrixInverseTranspose)
uniform mat1 gl_Texture9atrixInverseTranspose>gl_9axTextureCoords?)
**
** %ompati$ilit& profile onl&
**
uniform float gl_NormalS%ale)
**
** %ompati$ilit& profile onl&
**
uniform ve%1 gl_Clip@lane>gl_9axClip@lanes?)
**
** %ompati$ilit& profile onl&
**
stru%t gl_@oint@arameters ;
float si"e)
float si"e9in)
float si"e9ax)
float fadeT5res5oldSi"e)
float distan%eConstant8ttenuation)
float distan%eLinear8ttenuation)
float distan%eMuadrati%8ttenuation)
<)

uniform gl_@oint@arameters gl_@oint)
**
** %ompati$ilit& profile onl&
**
stru%t gl_9aterial@arameters ;
ve%1 emission) ** E%m
ve%1 am$ient) ** 8%m
ve%1 diffuse) ** %m
ve%1 spe%ular) ** S%m
float s5ininess) ** Srm
<)
uniform gl_9aterial@arameters gl_Front9aterial)
uniform gl_9aterial@arameters gl_-a%39aterial)
76
7 Bui%t+in #aria$%es
**
** %ompati$ilit& profile onl&
**
stru%t gl_Lig5tSour%e@arameters ;
ve%1 am$ient) ** 8%li
ve%1 diffuse) ** %li
ve%1 spe%ular) ** S%li
ve%1 position) ** @pli
ve%1 5alfVe%tor) ** erived( Gi
ve%4 spotire%tion) ** Sdli
float spotExponent) ** Srli
float spotCutoff) ** Crli
** #range( >A6A/NA6A?/ 'IA6A)
float spotCosCutoff) ** erived( %os#Crli)
** #range( >'6A/A6A?/F'6A)
float %onstant8ttenuation) ** OA
float linear8ttenuation) ** O'
float =uadrati%8ttenuation)** O2
<)
uniform gl_Lig5tSour%e@arameters gl_Lig5tSour%e>gl_9axLig5ts?)
stru%t gl_Lig5t9odel@arameters ;
ve%1 am$ient) ** 8%s
<)
uniform gl_Lig5t9odel@arameters gl_Lig5t9odel)
**
** %ompati$ilit& profile onl&
**
** erived state from produ%ts of lig5t and material6
**
stru%t gl_Lig5t9odel@rodu%ts ;
ve%1 s%eneColor) ** erived6 E%m H 8%m J 8%s
<)
uniform gl_Lig5t9odel@rodu%ts gl_FrontLig5t9odel@rodu%t)
uniform gl_Lig5t9odel@rodu%ts gl_-a%3Lig5t9odel@rodu%t)
stru%t gl_Lig5t@rodu%ts ;
ve%1 am$ient) ** 8%m J 8%li
ve%1 diffuse) ** %m J %li
ve%1 spe%ular) ** S%m J S%li
<)
uniform gl_Lig5t@rodu%ts gl_FrontLig5t@rodu%t>gl_9axLig5ts?)
uniform gl_Lig5t@rodu%ts gl_-a%3Lig5t@rodu%t>gl_9axLig5ts?)
77
7 Bui%t+in #aria$%es
**
** %ompati$ilit& profile onl&
**
uniform ve%1 gl_TextureEnvColor>gl_9axTextureLnits?)
uniform ve%1 gl_E&e@laneS>gl_9axTextureCoords?)
uniform ve%1 gl_E&e@laneT>gl_9axTextureCoords?)
uniform ve%1 gl_E&e@laneR>gl_9axTextureCoords?)
uniform ve%1 gl_E&e@laneM>gl_9axTextureCoords?)
uniform ve%1 gl_O$0e%t@laneS>gl_9axTextureCoords?)
uniform ve%1 gl_O$0e%t@laneT>gl_9axTextureCoords?)
uniform ve%1 gl_O$0e%t@laneR>gl_9axTextureCoords?)
uniform ve%1 gl_O$0e%t@laneM>gl_9axTextureCoords?)
**
** %ompati$ilit& profile onl&
**
stru%t gl_Fog@arameters ;
ve%1 %olor)
float densit&)
float start)
float end)
float s%ale) ** erived( '6A * #end F start)
<)

uniform gl_Fog@arameters gl_Fog)
2.! Compatibilit$ ,rofile *erte+ and %ragment Interface
Hhen using the com.ati6ilit( .ro"ile, the /0 can .rovide "i1ed "unctionalit( 6ehavior "or an(
.rogramma6le .i.eline stage. )or e1am.le, mi1ing a "i1ed "unctionalit( verte1 stage with a
.rogramma6le "ragment stage.
7
7 Bui%t+in #aria$%es
-he "ollowing 6uilt;in verte1 and geometr( in.ut and out.ut varia6les are availa6le to e""ect the in.uts to
a "ragment shader or "i1ed "unctionalit( "ragment stage. A .articular one should 6e written to i" an(
"unctionalit( in a corres.onding "ragment shader or "i1ed .i.eline uses it or state derived "rom it.
*therwise, 6ehavior is unde"ined. -hese are added to the out.ut gl_%erVertex 6lock in the verte1
language:
out gl_@erVertex ;
** in addition to ot5er gl_@erVertex mem$ers666
ve%1 gl_FrontColor)
ve%1 gl_-a%3Color)
ve%1 gl_FrontSe%ondar&Color)
ve%1 gl_-a%3Se%ondar&Color)
ve%1 gl_TexCoord>?)
float gl_FogFragCoord)
<)
and similarl( to the in.ut o" the geometr( language:
in gl_@erVertex ;
** in addition to ot5er gl_@erVertex mem$ers666
ve%1 gl_FrontColor)
ve%1 gl_-a%3Color)
ve%1 gl_FrontSe%ondar&Color)
ve%1 gl_-a%3Se%ondar&Color)
ve%1 gl_TexCoord>?)
float gl_FogFragCoord)
< gl_in>?)
)or gl_$og$ragoor!, the value written will 6e used as the EcF value in section %.11 o" the *.en/0
/ra.hics '(stem '.eci"ication, 6( the "i1ed "unctionalit( .i.eline. )or e1am.le, i" the @;coordinate o" the
"ragment in e(e s.ace is desired as EcF, then thatIs what the verte1 shader e1ecuta6le should write into
gl_$og$ragoor!.
As with all arra(s, indices used to su6scri.t gl_Texoor! must either 6e an integral constant e1.ressions,
or this arra( must 6e re;declared 6( the shader with a si@e. -he si@e can 6e at most
gl_:axTextureoor!s. 5sing inde1es close to 7 ma( aid the im.lementation in .reserving var(ing
resources. -he redeclaration o" gl_Texoor! is done at glo6al sco.e as, "or e1am.le,
in ve%1 gl_TexCoord>4?)
out ve%1 gl_TexCoord>1?)
which is the same wa( it was done in .revious releases. -his treatment is a s.ecial case "or
gl_Texoor!"#, not a general method "or redeclaring mem6ers o" 6locks.
-he "ollowing "ragment in.uts are also availa6le in a "ragment shader when using the com.ati6ilit(
.ro"ile:
7!
7 Bui%t+in #aria$%es
in float gl_FogFragCoord)
in ve%1 gl_TexCoord>?)
in ve%1 gl_Color)
in ve%1 gl_Se%ondar&Color)
-he values in gl_olor and gl_+econ!ar.olor will 6e derived automaticall( 6( the s(stem "rom
gl_$rontolor, gl_<ac3olor, gl_$ront+econ!ar.olor, and gl_<ac3+econ!ar.olor 6ased on which
"ace is visi6le. I" "i1ed "unctionalit( is used "or verte1 .rocessing, then gl_$og$ragoor! will either 6e
the @;coordinate o" the "ragment in e(e s.ace, or the inter.olation o" the "og coordinate, as descri6ed in
section %.11 o" the *.en/0 /ra.hics '(stem '.eci"ication. -he gl_Texoor!"# values are the
inter.olated gl_Texoor!"# values "rom a verte1 shader or the te1ture coordinates o" an( "i1ed .i.eline
6ased verte1 "unctionalit(.
Indices to the "ragment shader gl_Texoor! arra( are as descri6ed a6ove in the verte1 shader te1t.
"
3 -uilt9in %unctions
-he *.en/0 'hading 0anguage de"ines an assortment o" 6uilt;in convenience "unctions "or scalar and
vector o.erations. 8an( o" these 6uilt;in "unctions can 6e used in more than one t(.e o" shader, 6ut some
are intended to .rovide a direct ma..ing to hardware and so are availa6le onl( "or a s.eci"ic t(.e o"
shader.
-he 6uilt;in "unctions 6asicall( "all into three categories:
-he( e1.ose some necessar( hardware "unctionalit( in a convenient wa( such as accessing a te1ture
ma.. -here is no wa( in the language "or these "unctions to 6e emulated 6( a shader.
-he( re.resent a trivial o.eration Bclam., mi1, etc.C that is ver( sim.le "or the user to write, 6ut the(
are ver( common and ma( have direct hardware su..ort. It is a ver( hard .ro6lem "or the com.iler to
ma. e1.ressions to com.le1 assem6ler instructions.
-he( re.resent an o.eration gra.hics hardware is likel( to accelerate at some .oint. -he trigonometr(
"unctions "all into this categor(.
8an( o" the "unctions are similar to the same named ones in common ! li6raries, 6ut the( su..ort vector
in.ut as well as the more traditional scalar in.ut.
A..lications should 6e encouraged to use the 6uilt;in "unctions rather than do the e=uivalent com.utations
in their own shader code since the 6uilt;in "unctions are assumed to 6e o.timal Be.g., .erha.s su..orted
directl( in hardwareC.
5ser code can re.lace 6uilt;in "unctions with their own i" the( choose, 6( sim.l( re;declaring and de"ining
the same name and argument list. Because 6uilt;in "unctions are in a more outer sco.e than user 6uilt;in
"unctions, doing this will hide all 6uilt;in "unctions with the same name as the re;declared "unction.
Hhen the 6uilt;in "unctions are s.eci"ied 6elow, where the in.ut arguments Band corres.onding out.utC
can 6e float, vec%, vecF, or vecG, genT.)e is used as the argument. Hhere the in.ut arguments Band
corres.onding out.utC can 6e int, ivec%, ivecF, or ivecG, gen&T.)e is used as the argument. Hhere the
in.ut arguments Band corres.onding out.utC can 6e uint, uvec%, uvecF, or uvecG, genBT.)e is used as the
argument. Hhere the in.ut arguments Bor corres.onding out.utC can 6e bool, bvec%, bvecF, or bvecG,
gen<T.)e is used as the argument. )or an( s.eci"ic use o" a "unction, the actual t(.es su6stituted "or
genT.)e, gen&T.)e, genBT.)e, or gen<T.)e have to have the same num6er o" com.onents "or all
arguments and "or the return t(.e. 'imilarl( "or mat, which can 6e an( matri1 6asic t(.e.
1
Bui%t+in ,unctions
3.1 .ngle and Trigonometr$ %unctions
)unction .arameters s.eci"ied as angle are assumed to 6e in units o" radians. In no case will an( o" these
"unctions result in a divide 6( @ero error. I" the divisor o" a ratio is 7, then results will 6e unde"ined.
-hese all o.erate com.onent;wise. -he descri.tion is .er com.onent.
S$nta+ )escription
gen-(.e radians Bgen-(.e !egreesC
!onverts !egrees to radians, i.e.,

147
!egrees
gen-(.e degrees Bgen-(.e ra!iansC
!onverts ra!ians to degrees, i.e.,
147

ra!ians
gen-(.e sin Bgen-(.e angleC -he standard trigonometric sine "unction.
gen-(.e cos Bgen-(.e angleC -he standard trigonometric cosine "unction.
gen-(.e tan Bgen-(.e angleC -he standard trigonometric tangent.
gen-(.e asin Bgen-(.e xC Arc sine. Returns an angle whose sine is x. -he range
o" values returned 6( this "unction is
[

]
Results are unde"ined i" x1.
gen-(.e acos Bgen-(.e xC Arc cosine. Returns an angle whose cosine is x. -he
range o" values returned 6( this "unction is Y7, Z.
Results are unde"ined i" x1.
gen-(.e atan Bgen-(.e ., gen-(.e xC Arc tangent. Returns an angle whose tangent is .Ax. -he
signs o" x and . are used to determine what =uadrant the
angle is in. -he range o" values returned 6( this
"unction is [, ]. Results are unde"ined i" x and
. are 6oth 7.
gen-(.e atan Bgen-(.e ._over_xC Arc tangent. Returns an angle whose tangent is
._over_x. -he range o" values returned 6( this "unction
is
[

]
.
2
Bui%t+in ,unctions
S$nta+ )escription
gen-(.e sin Bgen-(.e xC Returns the h(.er6olic sine "unction
e
x
e
x

gen-(.e cos Bgen-(.e xC Returns the h(.er6olic cosine "unction


e
x
e
x

gen-(.e tan Bgen-(.e xC Returns the h(.er6olic tangent "unction


sinh x
cosh x
gen-(.e asin Bgen-(.e xC Arc h(.er6olic sineG returns the inverse o" sin.
gen-(.e acos Bgen-(.e xC Arc h(.er6olic cosineG returns the non;negative inverse
o" cos. Results are unde"ined i" x P 1.
gen-(.e atan Bgen-(.e xC Arc h(.er6olic tangentG returns the inverse o" tan.
Results are unde"ined i" x1.
3.2 '+ponential %unctions
-hese all o.erate com.onent;wise. -he descri.tion is .er com.onent.
S$nta+ )escription
gen-(.e pow Bgen-(.e x, gen-(.e .C Returns x raised to the . .ower, i.e., x
.
Results are unde"ined i" x C 0.
Results are unde"ined i" x = 0 and . C= 0.
gen-(.e exp Bgen-(.e xC Returns the natural e1.onentiation o" x, i.e., e
x
.
gen-(.e log Bgen-(.e xC Returns the natural logarithm o" x, i.e., returns the value
. which satis"ies the e=uation x R e
.
.
Results are unde"ined i" x C= 0.
gen-(.e exp% Bgen-(.e xC Returns raised to the x .ower, i.e.,
x
gen-(.e log% Bgen-(.e xC Returns the 6ase logarithm o" x, i.e., returns the value
. which satis"ies the e=uation x=
.
Results are unde"ined i" x C= 0.
3
Bui%t+in ,unctions
S$nta+ )escription
gen-(.e sKrt Bgen-(.e xC Returns
x .
Results are unde"ined i" x C 0.
gen-(.e inversesKrt Bgen-(.e xC
Returns
1
x
.
Results are unde"ined i" x C= 0.
3." Common %unctions
-hese all o.erate com.onent;wise. -he descri.tion is .er com.onent.
S$nta+ )escription
gen-(.e abs Bgen-(.e xC
genI-(.e abs BgenI-(.e xC
Returns x i" x QR 7, otherwise it returns ]x.
gen-(.e sign Bgen-(.e xC
genI-(.e sign BgenI-(.e xC
Returns 1.7 i" x Q 7, 7.7 i" x R 7, or ]1.7 i" x P 7.
gen-(.e floor Bgen-(.e xC Returns a value e=ual to the nearest integer that is less
than or e=ual to x.
gen-(.e trunc Bgen-(.e xC Returns a value e=ual to the nearest integer to x whose
a6solute value is not larger than the a6solute value o" x.
gen-(.e round Bgen-(.e xC Returns a value e=ual to the nearest integer to x. -he
"raction 7.$ will round in a direction chosen 6( the
im.lementation, .resuma6l( the direction that is "astest.
-his includes the .ossi6ilit( that roundBxC returns the
same value as roundLvenBxC "or all values o" x.
gen-(.e roundLven Bgen-(.e xC Returns a value e=ual to the nearest integer to x. A
"ractional .art o" 7.$ will round toward the nearest even
integer. BBoth %.$ and &.$ "or 1 will return &.7.C
gen-(.e ceil Bgen-(.e xC Returns a value e=ual to the nearest integer that is
greater than or e=ual to x.
gen-(.e fract Bgen-(.e xC Returns x ] floor BxC.
4
Bui%t+in ,unctions
S$nta+ )escription
gen-(.e mod Bgen-(.e x, "loat .C
gen-(.e mod Bgen-(.e x, gen-(.e .C
8odulus. Returns x ] . floor BxA.C.
gen-(.e modf Bgen-(.e x, out gen-(.e iC Returns the "ractional .art o" x and sets i to the integer
.art Bas a whole num6er "loating .oint valueC. Both the
return value and the out.ut .arameter will have the same
sign as x.
gen-(.e min Bgen-(.e x, gen-(.e .C
gen-(.e min Bgen-(.e x, "loat .C
genI-(.e min BgenI-(.e x, genI-(.e .C
genI-(.e min BgenI-(.e x, int .C
gen5-(.e min Bgen5-(.e x, gen5-(.e .C
gen5-(.e min Bgen5-(.e x, uint .C
Returns . i" . P x, otherwise it returns x.
gen-(.e max Bgen-(.e x, gen-(.e .C
gen-(.e max Bgen-(.e x, "loat .C
genI-(.e max BgenI-(.e x, genI-(.e .C
genI-(.e max BgenI-(.e x, int .C
gen5-(.e max Bgen5-(.e x, gen5-(.e .C
gen5-(.e max Bgen5-(.e x, uint .C
Returns . i" x P ., otherwise it returns x.
gen-(.e clamp Bgen-(.e x,
gen-(.e minVal,
gen-(.e maxValC
gen-(.e clamp Bgen-(.e x,
"loat minVal,
"loat maxValC
genI-(.e clamp BgenI-(.e x,
genI-(.e minVal,
genI-(.e maxValC
genI-(.e clamp BgenI-(.e x,
int minVal,
int maxValC
gen5-(.e clamp Bgen5-(.e x,
gen5-(.e minVal,
gen5-(.e maxValC
gen5-(.e clamp Bgen5-(.e x,
uint minVal,
uint maxValC
Returns min Bmax Bx, minValC, maxValC.
Results are unde"ined i" minVal Q maxVal.
5
Bui%t+in ,unctions
S$nta+ )escription
gen-(.e mix Bgen-(.e x,
gen-(.e .,
gen-(.e aC
gen-(.e mix Bgen-(.e x,
gen-(.e .,
"loat aC
Returns the linear 6lend o" x and ., i.e.,
x1a.a
gen-(.e mix Bgen-(.e x,
gen-(.e (,
genB-(.e aC
'elects which vector each returned com.onent comes
"rom. )or a com.onent o" a that is false, the
corres.onding com.onent o" x is returned. )or a
com.onent o" a that is true, the corres.onding
com.onent o" . is returned. !om.onents o" x and . that
are not selected are allowed to 6e invalid "loating .oint
values and will have no e""ect on the results. -hus, this
.rovides di""erent "unctionalit( than
gen-(.e mixBgen-(.e x, gen-(.e ., gen-(.eBaCC
where a is a Boolean vector.
gen-(.e step Bgen-(.e e!ge, gen-(.e xC
gen-(.e step B"loat e!ge, gen-(.e xC
Returns 7.7 i" x P e!ge, otherwise it returns 1.7.
gen-(.e smootstep Bgen-(.e e!ge0,
gen-(.e e!ge1,
gen-(.e xC
gen-(.e smootstep B"loat e!ge0,
"loat e!ge1,
gen-(.e xC
Returns 7.7 i" x PR e!ge0 and 1.7 i" x QR e!ge1 and
.er"orms smooth ,ermite inter.olation 6etween 7 and 1
when e!ge0 P x P e!ge1. -his is use"ul in cases where
(ou would want a threshold "unction with a smooth
transition. -his is e=uivalent to:
gen-(.e tG
t R clam. BB1 ] edge7C A Bedge1 ] edge7C, 7, 1CG
return t N t N B% ] N tCG
Results are un!e/ine! i/ e!ge0 D= e!ge1.
genB-(.e isnan Bgen-(.e xC Returns true i" x holds a <a< Bnot a num6erC
re.resentation in the underl(ing im.lementationIs set o"
"loating .oint re.resentations. Returns false otherwise,
including "or im.lementations with no <a<
re.resentations.
genB-(.e isinf Bgen-(.e xC Returns true i" x holds a .ositive in"init( or negative
in"init( re.resentation in the underl(ing im.lementationIs
set o" "loating .oint re.resentations. Returns false
otherwise, including "or im.lementations with no in"init(
re.resentations.
6
Bui%t+in ,unctions
3.# Geometric %unctions
-hese o.erate on vectors as vectors, not com.onent;wise.
S$nta+ )escription
"loat lengt Bgen-(.e xC Returns the length o" vector x, i.e.,
x[ 7]

x[ 1]

...
"loat distance Bgen-(.e )0, gen-(.e )1C Returns the distance 6etween )0 and )1, i.e.,
lengt B)0 E )1C
"loat dot Bgen-(.e x, gen-(.e .C Returns the dot .roduct o" x and ., i.e.,
x[ 7]. [7]x [1].[1]...
vec% cross Bvec% x, vec% .C Returns the cross .roduct o" 1 and (, i.e.,
[
x [1].[ ]. [1]x []
x[ ].[ 7]. [ ]x[ 7]
x[7]. [1]. [7]x [1]
]
gen-(.e normali)e Bgen-(.e xC Returns a vector in the same direction as x 6ut with a
length o" 1.
com.ati6ilit( .ro"ile onl(
vec& ftransformBC
Availa6le onl( when using the com.ati6ilit( .ro"ile. )or
core *.en/0, use invariant.
)or verte1 shaders onl(. -his "unction will ensure that
the incoming verte1 value will 6e trans"ormed in a wa(
that .roduces e1actl( the same result as would 6e
.roduced 6( *.en/0Js "i1ed "unctionalit( trans"orm. It
is intended to 6e used to com.ute gl_%osition, e.g.,
gl>2osition R ftransformBC
-his "unction should 6e used, "or e1am.le, when an
a..lication is rendering the same geometr( in se.arate
.asses, and one .ass uses the "i1ed "unctionalit( .ath to
render and another .ass uses .rogramma6le shaders.
gen-(.e faceforwardBgen-(.e 0,
gen-(.e &,
gen-(.e 0re/C
I" dotB0re/, &C P 7 return 0, otherwise return ]0.
7
Bui%t+in ,unctions
S$nta+ )escription
gen-(.e reflect Bgen-(.e &, gen-(.e 0C )or the incident vector & and sur"ace orientation 0,
returns the re"lection direction:
& ] dotB0, &C 0
0 must alread( 6e normali@ed in order to achieve the
desired result.
gen-(.e refractBgen-(.e &, gen-(.e 0,
"loat etaC
)or the incident vector & and sur"ace normal 0, and the
ratio o" indices o" re"raction eta, return the re"raction
vector. -he result is com.uted 6(
k R 1.7 ; eta N eta N B1.7 ; dotB0, &C N dotB0, &CC
i" Bk P 7.7C
return gen-(.eB7.7C
else
return eta N & ; Beta N dotB0, &C K sKrtBkCC N 0
-he in.ut .arameters "or the incident vector & and the
sur"ace normal 0 must alread( 6e normali@ed to get the
desired results.

Bui%t+in ,unctions
3. 5atri+ %unctions
S$nta+ )escription
mat matrixComp'ult Bmat x, mat .C 8ulti.l( matri1 x 6( matri1 . com.onent;wise, i.e.,
resultYiZY?Z is the scalar .roduct o" xYiZY?Z and .YiZY?Z.
<ote: to get linear alge6raic matri1 multi.lication, use
the multi.l( o.erator B1C.
mat outerRroductBvec c, vec rC
mat% outerRroductBvec% c, vec% rC
mat& outerRroductBvec& c, vec& rC
mat1% outerRroductBvec% c, vec rC
mat%1 outerRroductBvec c, vec% rC
mat1& outerRroductBvec& c, vec rC
mat&1 outerRroductBvec c, vec& rC
mat%1& outerRroductBvec& c, vec% rC
mat&1% outerRroductBvec% c, vec& rC
-reats the "irst .arameter c as a column vector Bmatri1
with one columnC and the second .arameter r as a row
vector Bmatri1 with one rowC and does a linear alge6raic
matri1 multi.l( c N r, (ielding a matri1 whose num6er o"
rows is the num6er o" com.onents in c and whose
num6er o" columns is the num6er o" com.onents in r.
mat transposeBmat mC
mat% transposeBmat% mC
mat& transposeBmat& mC
mat1% transposeBmat%1 mC
mat%1 transposeBmat1% mC
mat1& transposeBmat&1 mC
mat&1 transposeBmat1& mC
mat%1& transposeBmat&1% mC
mat&1% transposeBmat%1& mC
Returns a matri1 that is the trans.ose o" m. -he in.ut
matri1 m is not modi"ied.
"loat determinantBmat mC
"loat determinantBmat% mC
"loat determinantBmat& mC
Returns the determinant o" m.
mat inverseBmat mC
mat% inverseBmat% mC
mat& inverseBmat& mC
Returns a matri1 that is the inverse o" m. -he in.ut
matri1 m is not modi"ied. -he values in the returned
matri1 are unde"ined i" m is singular or .oorl(;
conditioned Bnearl( singularC.
!
Bui%t+in ,unctions
3.! *ector 8elational %unctions
Relational and e=ualit( o.erators B3, 3=, 4, 4=, ==, >=C are de"ined to .roduce scalar Boolean results. )or
vector results, use the "ollowing 6uilt;in "unctions. Below, E6vecF is a .laceholder "or one o" bvec%,
bvecF, or bvecG, EivecF is a .laceholder "or one o" ivec%, ivecF, or ivecG, EuvecF is a .laceholder "or
uvec%, uvecF, or uvecG, and EvecF is a .laceholder "or vec%, vecF, or vecG. In all cases, the si@es o" the
in.ut and return vectors "or an( .articular call must match.
S$nta+ )escription
6vec lessCanBvec 1, vec (C
6vec lessCanBivec 1, ivec (C
6vec lessCanBuvec 1, uvec (C
Returns the com.onent;wise com.are o" x P ..
6vec lessCanLKualBvec 1, vec (C
6vec lessCanLKualBivec 1, ivec (C
6vec lessCanLKualBuvec 1, uvec (C
Returns the com.onent;wise com.are o" x PR ..
6vec greaterCanBvec 1, vec (C
6vec greaterCanBivec 1, ivec (C
6vec greaterCanBuvec 1, uvec (C
Returns the com.onent;wise com.are o" x Q ..
6vec greaterCanLKualBvec 1, vec (C
6vec greaterCanLKualBivec 1, ivec (C
6vec greaterCanLKualBuvec 1, uvec (C
Returns the com.onent;wise com.are o" x QR ..
6vec eKualBvec 1, vec (C
6vec eKualBivec 1, ivec (C
6vec eKualBuvec 1, uvec (C
6vec eKualB6vec 1, 6vec (C
6vec notLKualBvec 1, vec (C
6vec notLKualBivec 1, ivec (C
6vec notLKualBuvec 1, uvec (C
6vec notLKualB6vec 1, 6vec (C
Returns the com.onent;wise com.are o" x RR ..
Returns the com.onent;wise com.are o" x MR ..
6ool an!B6vec 1C Returns true i" an( com.onent o" x is true.
6ool allB6vec 1C Returns true onl( i" all com.onents o" x are true.
6vec notB6vec 1C Returns the com.onent;wise logical com.lement o" x.
!"
Bui%t+in ,unctions
3.2 Te+ture Loo0up %unctions
-e1ture looku. "unctions are availa6le to verte1, geometr(, and "ragment shaders. ,owever, level o"
detail is not im.licitl( com.uted "or verte1 or geometr( shaders. -he "unctions in the ta6le 6elow .rovide
access to te1tures through sam.lers, as set u. through the *.en/0 A2I. -e1ture .ro.erties such as si@e,
.i1el "ormat, num6er o" dimensions, "iltering method, num6er o" mi.;ma. levels, de.th com.arison, and
so on are also de"ined 6( *.en/0 A2I calls. 'uch .ro.erties are taken into account as the te1ture is
accessed via the 6uilt;in "unctions de"ined 6elow.
-e1ture data can 6e stored 6( the /0 as "loating .oint, unsigned normali@ed integer, unsigned integer or
signed integer data. -his is determined 6( the t(.e o" the internal "ormat o" the te1ture. -e1ture looku.s
on unsigned normali@ed integer and "loating .oint data return "loating .oint values in the range Y7, 1Z.
-e1ture looku. "unctions are .rovided that can return their result as "loating .oint, unsigned integer or
signed integer, de.ending on the sam.ler t(.e .assed to the looku. "unction. !are must 6e taken to use
the right sam.ler t(.e "or te1ture access. -he "ollowing ta6le lists the su..orted com6inations o" sam.ler
t(.es and te1ture internal "ormats. Blank entries are unsu..orted. Doing a te1ture looku. will return
unde"ined values "or unsu..orted com6inations.
Internal -e1ture )ormat
)loating 2oint
'am.ler -(.es
'igned Integer
'am.ler -(.es
5nsigned Integer
'am.ler -(.es
)loating .oint 'u..orted
<ormali@ed Integer 'u..orted
'igned Integer 'u..orted
5nsigned Integer 'u..orted
I" an integer sam.ler t(.e is used, the result o" a te1ture looku. is an ivecG. I" an unsigned integer sam.ler
t(.e is used, the result o" a te1ture looku. is a uvecG. I" a "loating .oint sam.ler t(.e is used, the result o"
a te1ture looku. is a vecG, where each com.onent is in the range Y7, 1Z.
In the .rotot(.es 6elow, the EgF in the return t(.e Egvec4F is used as a .laceholder "or nothing, EiF, or EuF
making a return t(.e o" vecG, ivecG, or uvecG. In these cases, the sam.ler argument t(.e also starts with
EgF, indicating the same su6stitution done on the return t(.eG it is either a "loating .oint, signed integer, or
unsigned integer sam.ler, matching the 6asic t(.e o" the return t(.e, as descri6ed a6ove.
)or shadow "orms Bthe sam.ler .arameter is a shadow;t(.eC, a de.th com.arison looku. on the de.th
te1ture 6ound to sam)ler is done as descri6ed in section %.4.1$ o" the *.en/0 /ra.hics '(stem
'.eci"ication. 'ee the ta6le 6elow "or which com.onent s.eci"ies Dre/. -he te1ture 6ound to sam)ler
must 6e a de.th te1ture, or results are unde"ined. I" a non;shadow te1ture call is made to a sam.ler that
re.resents a de.th te1ture with de.th com.arisons turned on, then results are unde"ined. I" a shadow
te1ture call is made to a sam.ler that re.resents a de.th te1ture with de.th com.arisons turned o"", then
results are unde"ined. I" a shadow te1ture call is made to a sam.ler that does not re.resent a de.th
te1ture, then results are unde"ined.
!1
Bui%t+in ,unctions
In all "unctions 6elow, the -ias .arameter is o.tional "or "ragment shaders. -he -ias .arameter is not
acce.ted in a verte1 or geometr( shader. )or a "ragment shader, i" -ias is .resent, it is added to the
im.licit level o" detail .rior to .er"orming the te1ture access o.eration. <o -ias or lo! .arameters "or
rectangular te1tures, multi;sam.le te1tures, or te1ture 6u""ers are su..orted 6ecause mi.;ma.s are not
allowed "or these t(.es o" te1tures.
-he im.licit level o" detail is selected as "ollows: )or a te1ture that is not mi.;ma..ed, the te1ture is used
directl(. I" it is mi.;ma..ed and running in a "ragment shader, the 0*D com.uted 6( the im.lementation
is used to do the te1ture looku.. I" it is mi.;ma..ed and running on the verte1 shader, then the 6ase
te1ture is used.
'ome te1ture "unctions Bnon;EDodF and non;EGradF versionsC ma( re=uire im.licit derivatives. Im.licit
derivatives are unde"ined within non;uni"orm control "low and "or verte1 and geometr( shader te1ture
"etches.
)or Cube "orms, the direction o" % is used to select which "ace to do a ;dimensional te1ture looku. in, as
descri6ed in section %.4.4 !u6e 8a. -e1ture 'election in the *.en/0 /ra.hics '(stem '.eci"ication.
)or Arra! "orms, the arra( la(er used will 6e
max 7, min ! 1, /loorla.er7.$
where ! is the de.th o" the te1ture arra( and la.er comes "rom the com.onent indicated in the ta6les
6elow.
!2
Bui%t+in ,unctions
S$nta+ )escription
int textureSi)e Bgsam.ler1D sam)ler, int lo!C
ivec textureSi)e Bgsam.lerD sam)ler, int lo!C
ivec% textureSi)e Bgsam.ler%D sam)ler, int lo!C
ivec textureSi)e Bgsam.ler!u6e sam)ler, int lo!C
int textureSi)e Bsam.ler1D'hadow sam)ler, int lo!C
ivec textureSi)e Bsam.lerD'hadow sam)ler, int lo!C
ivec textureSi)e Bsam.ler!u6e'hadow sam)ler, int lo!C
ivec textureSi)e Bgsam.lerDRect sam)lerC
ivec textureSi)e Bsam.lerDRect'hadow sam)lerC
ivec textureSi)e Bgsam.ler1DArra( sam)ler, int lo!C
ivec% textureSi)e Bgsam.lerDArra( sam)ler, int lo!C
ivec textureSi)e Bsam.ler1DArra('hadow sam)ler, int lo!C
ivec% textureSi)e Bsam.lerDArra('hadow sam)ler, int lo!C
int textureSi)e Bgsam.lerBu""er sam)lerC
ivec textureSi)e Bgsam.lerD8' sam)lerC
ivec textureSi)e Bgsam.lerD8'Arra( sam)lerC
Returns the dimensions o" level
lo! Bi" .resentC "or the te1ture
6ound to sam)ler, as descri6ed
in section .11.3 o" the *.en/0
/ra.hics '(stem '.eci"ication,
under -e1ture 'i@e :uer(.
-he com.onents in the return
value are "illed in, in order, with
the width, height, de.th o" the
te1ture.
)or the arra( "orms, the last
com.onent o" the return value is
the num6er o" la(ers in the
te1ture arra(.
gvec& texture Bgsam.ler1D sam)ler, "loat % Y, "loat -iasZ C
gvec& texture Bgsam.lerD sam)ler, vec % Y, "loat -iasZ C
gvec& texture Bgsam.ler%D sam)ler, vec% % Y, "loat -iasZ C
gvec& texture Bgsam.ler!u6e sam)ler, vec% % Y, "loat -iasZ C
"loat texture Bsam.ler1D'hadow sam)ler, vec% % Y, "loat -iasZ C
"loat texture Bsam.lerD'hadow sam)ler, vec% % Y, "loat -iasZ C
"loat texture Bsam.ler!u6e'hadow sam)ler, vec& % Y, "loat -iasZ C
gvec& texture Bgsam.ler1DArra( sam)ler, vec % Y, "loat -iasZ C
gvec& texture Bgsam.lerDArra( sam)ler, vec% % Y, "loat -iasZ C
"loat texture Bsam.ler1DArra('hadow sam)ler, vec% %
Y, "loat -iasZ C
"loat texture Bsam.lerDArra('hadow sam)ler, vec& %C
gvec& texture Bgsam.lerDRect sam)ler, vec %C
"loat texture Bsam.lerDRect'hadow sam)ler, vec% %C
5se the te1ture coordinate % to
do a te1ture looku. in the
te1ture currentl( 6ound to
sam)ler. -he last com.onent o"
% is used as Dre/ "or the shadow
"orms. )or arra( "orms, the arra(
la(er comes "rom the last
com.onent o" % in the non;
shadow "orms, and the second to
last com.onent o" % in the
shadow "orms.
gvec& textureRroJ Bgsam.ler1D sam)ler, vec % Y, "loat -iasZ C
gvec& textureRroJ Bgsam.ler1D sam)ler, vec& % Y, "loat -iasZ C
gvec& textureRroJ Bgsam.lerD sam)ler, vec% % Y, "loat -iasZ C
gvec& textureRroJ Bgsam.lerD sam)ler, vec& % Y, "loat -iasZ C
gvec& textureRroJ Bgsam.ler%D sam)ler, vec& % Y, "loat -iasZ C
"loat textureRroJ Bsam.ler1D'hadow sam)ler, vec& %
Y, "loat -iasZ C
"loat textureRroJ Bsam.lerD'hadow sam)ler, vec& %
Y, "loat -iasZ C
gvec& textureRroJ Bgsam.lerDRect sam)ler, vec% %C
gvec& textureRroJ Bgsam.lerDRect sam)ler, vec& %C
"loat textureRroJ Bsam.lerDRect'hadow sam)ler, vec& %C
Do a te1ture looku. with
.ro?ection. -he te1ture
coordinates consumed "rom %,
not including the last com.onent
o" %, are divided 6( the last
com.onent o" %. -he resulting
%
rd
com.onent o" % in the
shadow "orms is used as Dre/.
A"ter these values are com.uted,
te1ture looku. .roceeds as in
texture.
!3
Bui%t+in ,unctions
S$nta+ )escription
gvec& textureDod Bgsam.ler1D sam)ler, "loat %, "loat lo!C
gvec& textureDod Bgsam.lerD sam)ler, vec %, "loat lo!C
gvec& textureDod Bgsam.ler%D sam)ler, vec% %, "loat lo!C
gvec& textureDod Bgsam.ler!u6e sam)ler, vec% %, "loat lo!C
"loat textureDod Bsam.ler1D'hadow sam)ler, vec% %, "loat lo!C
"loat textureDod Bsam.lerD'hadow sam)ler, vec% %, "loat lo!C
gvec& textureDod Bgsam.ler1DArra( sam)ler, vec %, "loat lo!C
gvec& textureDod Bgsam.lerDArra( sam)ler, vec% %, "loat lo!C
"loat textureDod Bsam.ler1DArra('hadow sam)ler, vec% %,
"loat lo!C
Do a te1ture looku. as in
texture 6ut with e1.licit 0*DG
lo! s.eci"ies F-ase Bsee e=uation
%.14 in *.en/0 /ra.hics
'(stem '.eci"icationC and set
the .artial derivatives in section
%.4.9 as "ollows.
u
x
= 7
v
x
= 7
4
x
= 7
u
.
= 7
v
.
= 7
4
.
= 7
gvec& textureOffset Bgsam.ler1D sam)ler, "loat %,
int o//set Y, "loat -iasZ C
gvec& textureOffset Bgsam.lerD sam)ler, vec %,
ivec o//set Y, "loat -iasZ C
gvec& textureOffset Bgsam.ler%D sam)ler, vec% %,
ivec% o//set Y, "loat -iasZ C
gvec& textureOffset Bgsam.lerDRect sam)ler, vec %,
ivec o//set C
"loat textureOffset Bsam.lerDRect'hadow sam)ler, vec% %,
ivec o//set C
"loat textureOffset Bsam.ler1D'hadow sam)ler, vec% %,
int o//set Y, "loat -iasZ C
"loat textureOffset Bsam.lerD'hadow sam)ler, vec% %,
ivec o//set Y, "loat -iasZ C
gvec& textureOffset Bgsam.ler1DArra( sam)ler, vec %,
int o//set Y, "loat -iasZ C
gvec& textureOffset Bgsam.lerDArra( sam)ler, vec% %,
ivec o//set Y, "loat -iasZ C
"loat textureOffset Bsam.ler1DArra('hadow sam)ler, vec% %,
int o//set Y, "loat -iasZ C
Do a te1ture looku. as in
texture 6ut with o//set added to
the Bu,v,4C te1el coordinates
6e"ore looking u. each te1el.
-he o""set value must 6e a
constant e1.ression. A limited
range o" o""set values are
su..ortedG the minimum and
ma1imum o""set values are
im.lementation;de.endent and
given 6(
8I<>2R*/RA8>-+X+0>*))'+- and
8AX>2R*/RA8>-+X+0>*))'+-,
res.ectivel(.
<ote that o//set does not a..l(
to the la(er coordinate "or
te1ture arra(s. -his is e1.lained
in detail in section %.4.9 o" the
*.en/0 /ra.hics '(stem
'.eci"ication, where o//set is

u
,
v
,
4
. <ote that te1el
o""sets are also not su..orted
"or cu6e ma.s.
!4
Bui%t+in ,unctions
S$nta+ )escription
gvec& texel(etc Bgsam.ler1D sam)ler, int %, int lo!C
gvec& texel(etc Bgsam.lerD sam)ler, ivec %, int lo!C
gvec& texel(etc Bgsam.ler%D sam)ler, ivec% %, int lo!C
gvec& texel(etc Bgsam.lerDRect sam)ler, ivec %C
gvec& texel(etc Bgsam.ler1DArra( sam)ler, ivec %, int lo!C
gvec& texel(etc Bgsam.lerDArra( sam)ler, ivec% %, int lo!C
gvec& texel(etc Bgsam.lerBu""er sam)ler, int %C
gvec& texel(etc Bgsam.lerD8' sam)ler, ivec %, int sam)leC
gvec& texel(etc Bgsam.lerD8'Arra( sam)ler, ivec% %,
int sam)leC
5se integer te1ture coordinate %
to looku. a single te1el "rom
sam)ler. -he arra( la(er comes
"rom the last com.onent o" % "or
the arra( "orms. -he level;o";
detail lo! Bi" .resentC is as
descri6ed in sections .11.3
under -e1el )etches and section
%.4 -e1turing o" the *.en/0
/ra.hics '(stem '.eci"ication.
gvec& texel(etcOffset Bgsam.ler1D sam)ler, int %, int lo!,
int o//setC
gvec& texel(etcOffset Bgsam.lerD sam)ler, ivec %, int lo!,
ivec o//setC
gvec& texel(etcOffset Bgsam.ler%D sam)ler, ivec% %, int lo!,
ivec% o//setC
gvec& texel(etcOffset Bgsam.lerDRect sam)ler, ivec %,
ivec o//setC
gvec& texel(etcOffset Bgsam.ler1DArra( sam)ler, ivec %, int lo!,
int o//setC
gvec& texel(etcOffset Bgsam.lerDArra( sam)ler, ivec% %, int lo!,
ivec o//setC
)etch a single te1el as in
texel(etc o""set 6( o//set as
descri6ed in textureOffset.
gvec& textureRroJOffset Bgsam.ler1D sam)ler, vec %,
int o//set Y, "loat -iasZ C
gvec& textureRroJOffset Bgsam.ler1D sam)ler, vec& %,
int o//set Y, "loat -iasZ C
gvec& textureRroJOffset Bgsam.lerD sam)ler, vec% %,
ivec o//set Y, "loat -iasZ C
gvec& textureRroJOffset Bgsam.lerD sam)ler, vec& %,
ivec o//set Y, "loat -iasZ C
gvec& textureRroJOffset Bgsam.ler%D sam)ler, vec& %,
ivec% o//set Y, "loat -iasZ C
gvec& textureRroJOffset Bgsam.lerDRect sam)ler, vec% %,
ivec o//set C
gvec& textureRroJOffset Bgsam.lerDRect sam)ler, vec& %,
ivec o//set C
"loat textureRroJOffset Bsam.lerDRect'hadow sam)ler, vec& %,
ivec o//set C
"loat textureRroJOffset Bsam.ler1D'hadow sam)ler, vec& %,
int o//set Y, "loat -iasZ C
"loat textureRroJOffset Bsam.lerD'hadow sam)ler, vec& %,
ivec o//set Y, "loat -iasZ C
Do a .ro?ective te1ture looku.
as descri6ed in textureRroJ
o""set 6( o//set as descri6ed in
textureOffset.
!5
Bui%t+in ,unctions
S$nta+ )escription
gvec& textureDodOffset Bgsam.ler1D sam)ler, "loat %,
"loat lo!, int o//setC
gvec& textureDodOffset Bgsam.lerD sam)ler, vec %,
"loat lo!, ivec o//setC
gvec& textureDodOffset Bgsam.ler%D sam)ler, vec% %,
"loat lo!, ivec% o//setC
"loat textureDodOffset Bsam.ler1D'hadow sam)ler, vec% %,
"loat lo!, int o//setC
"loat textureDodOffset Bsam.lerD'hadow sam)ler, vec% %,
"loat lo!, ivec o//setC
gvec& textureDodOffset Bgsam.ler1DArra( sam)ler, vec %,
"loat lo!, int o//setC
gvec& textureDodOffset Bgsam.lerDArra( sam)ler, vec% %,
"loat lo!, ivec o//setC
"loat textureDodOffset Bsam.ler1DArra('hadow sam)ler, vec% %,
"loat lo!, int o//setC
Do an o""set te1ture looku. with
e1.licit 0*D. 'ee textureDod
and textureOffset.
gvec& textureRroJDod Bgsam.ler1D sam)ler, vec %, "loat lo!C
gvec& textureRroJDod Bgsam.ler1D sam)ler, vec& %, "loat lo!C
gvec& textureRroJDod Bgsam.lerD sam)ler, vec% %, "loat lo!C
gvec& textureRroJDod Bgsam.lerD sam)ler, vec& %, "loat lo!C
gvec& textureRroJDod Bgsam.ler%D sam)ler, vec& %, "loat lo!C
"loat textureRroJDod Bsam.ler1D'hadow sam)ler, vec& %, "loat lo!C
"loat textureRroJDod Bsam.lerD'hadow sam)ler, vec& %, "loat lo!C
Do a .ro?ective te1ture looku.
with e1.licit 0*D. 'ee
textureRroJ and textureDod.
gvec& textureRroJDodOffset Bgsam.ler1D sam)ler, vec %,
"loat lo!, int o//setC
gvec& textureRroJDodOffset Bgsam.ler1D sam)ler, vec& %,
"loat lo!, int o//setC
gvec& textureRroJDodOffset Bgsam.lerD sam)ler, vec% %,
"loat lo!, ivec o//setC
gvec& textureRroJDodOffset Bgsam.lerD sam)ler, vec& %,
"loat lo!, ivec o//setC
gvec& textureRroJDodOffset Bgsam.ler%D sam)ler, vec& %,
"loat lo!, ivec% o//setC
"loat textureRroJDodOffset Bsam.ler1D'hadow sam)ler, vec& %,
"loat lo!, int o//setC
"loat textureRroJDodOffset Bsam.lerD'hadow sam)ler, vec& %,
"loat lo!, ivec o//setC
Do an o""set .ro?ective te1ture
looku. with e1.licit 0*D. 'ee
textureRroJ, textureDod, and
textureOffset.
!6
Bui%t+in ,unctions
S$nta+ )escription
gvec& textureGrad Bgsam.ler1D sam)ler, "loat %,
"loat !%!x, "loat !%!.C
gvec& textureGrad Bgsam.lerD sam)ler, vec %,
vec !%!x, vec !%!.C
gvec& textureGrad Bgsam.ler%D sam)ler, vec% %,
vec% !%!x, vec% !%!.C
gvec& textureGrad Bgsam.ler!u6e sam)ler, vec% %,
vec% !%!x, vec% !%!.C
gvec& textureGrad Bgsam.lerDRect sam)ler, vec %,
vec !%!x, vec !%!.C
"loat textureGrad Bsam.lerDRect'hadow sam)ler, vec% %,
vec !%!x, vec !%!.C
"loat textureGrad Bsam.ler1D'hadow sam)ler, vec% %,
"loat !%!x, "loat !%!.C
"loat textureGrad Bsam.lerD'hadow sam)ler, vec% %,
vec !%!x, vec !%!.C
"loat textureGrad Bsam.ler!u6e'hadow sam)ler, vec& %,
vec% !%!x, vec% !%!.C
gvec& textureGrad Bgsam.ler1DArra( sam)ler, vec %,
"loat !%!x, "loat !%!.C
gvec& textureGrad Bgsam.lerDArra( sam)ler, vec% %,
vec !%!x, vec !%!.C
"loat textureGrad Bsam.ler1DArra('hadow sam)ler, vec% %,
"loat !%!x, "loat !%!.C
"loat textureGrad Bsam.lerDArra('hadow sam)ler, vec& %,
vec !%!x, vec !%!.C
Do a te1ture looku. as in
texture 6ut with e1.licit
gradients. -he .artial
derivatives o" % are with res.ect
to window 1 and window (. 'et
s
x
=
{
%
x
"or a 1D te1ture
%.s
x
otherwise
s
.
=
{
%
.
"or a 1D te1ture
%.s
.
otherwise
t
x
=
{
7.7 "or a 1D te1ture
%.t
x
otherwise
t
.
=
{
7.7 "or a 1D te1ture
%.t
.
otherwise
r
x
=
{
7.7 "or 1D or D
%.)
x
cu6e, other
r
.
=
{
7.7 "or 1D or D
%.)
.
cu6e, other
)or the cu6e version, the .artial
derivatives o" % are assumed to
6e in the coordinate s(stem used
6e"ore te1ture coordinates are
.ro?ected onto the a..ro.riate
cu6e "ace.
!7
Bui%t+in ,unctions
S$nta+ )escription
gvec& textureGradOffset Bgsam.ler1D sam)ler, "loat %,
"loat !%!x, "loat !%!., int o//setC
gvec& textureGradOffset Bgsam.lerD sam)ler, vec %,
vec !%!x, vec !%!., ivec o//setC
gvec& textureGradOffset Bgsam.ler%D sam)ler, vec% %,
vec% !%!x, vec% !%!., ivec% o//setC
gvec& textureGradOffset Bgsam.lerDRect sam)ler, vec %,
vec !%!x, vec !%!., ivec o//setC
"loat textureGradOffset Bsam.lerDRect'hadow sam)ler, vec% %,
vec !%!x, vec !%!., ivec o//setC
"loat textureGradOffset Bsam.ler1D'hadow sam)ler, vec% %,
"loat !%!x, "loat !%!., int o//set C
"loat textureGradOffset Bsam.lerD'hadow sam)ler, vec% %,
vec !%!x, vec !%!., ivec o//setC
"loat textureGradOffset Bsam.ler!u6e'hadow sam)ler, vec& %,
vec% !%!x, vec% !%!., ivec o//setC
gvec& textureGradOffset Bgsam.ler1DArra( sam)ler, vec %,
"loat !%!x, "loat !%!., int o//setC
gvec& textureGradOffset Bgsam.lerDArra( sam)ler, vec% %,
vec !%!x, vec !%!., ivec o//setC
"loat textureGradOffset Bsam.ler1DArra('hadow sam)ler, vec% %,
"loat !%!x, "loat !%!., int o//setC
"loat textureGradOffset Bsam.lerDArra('hadow sam)ler, vec& %,
vec !%!x, vec !%!., ivec o//setC
Do a te1ture looku. with 6oth
e1.licit gradient and o""set, as
descri6ed in textureGrad and
textureOffset.
gvec& textureRroJGrad Bgsam.ler1D sam)ler, vec %,
"loat !%!x, "loat !%!.C
gvec& textureRroJGrad Bgsam.ler1D sam)ler, vec& %,
"loat !%!x, "loat !%!.C
gvec& textureRroJGrad Bgsam.lerD sam)ler, vec% %,
vec !%!x, vec !%!.C
gvec& textureRroJGrad Bgsam.lerD sam)ler, vec& %,
vec !%!x, vec !%!.C
gvec& textureRroJGrad Bgsam.ler%D sam)ler, vec& %,
vec% !%!x, vec% !%!.C
gvec& textureRroJGrad Bgsam.lerDRect sam)ler, vec% %,
vec !%!x, vec !%!.C
gvec& textureRroJGrad Bgsam.lerDRect sam)ler, vec& %,
vec !%!x, vec !%!.C
"loat textureRroJGrad Bsam.lerDRect'hadow sam)ler, vec& %,
vec !%!x, vec !%!.C
"loat textureRroJGrad Bsam.ler1D'hadow sam)ler, vec& %,
"loat !%!x, "loat !%!.C
"loat textureRroJGrad Bsam.lerD'hadow sam)ler, vec& %,
vec !%!x, vec !%!.C
Do a te1ture looku. 6oth
.ro?ectivel(, as descri6ed in
textureRroJ, and with e1.licit
gradient as descri6ed in
textureGrad. -he .artial
derivatives !%!x and !%!. are
assumed to 6e alread( .ro?ected.
!
Bui%t+in ,unctions
S$nta+ )escription
gvec& textureRroJGradOffset Bgsam.ler1D sam)ler, vec %,
"loat !%!x, "loat !%!., int o//setC
gvec& textureRroJGradOffset Bgsam.ler1D sam)ler, vec& %,
"loat !%!x, "loat !%!., int o//setC
gvec& textureRroJGradOffset Bgsam.lerD sam)ler, vec% %,
vec !%!x, vec !%!., vec o//setC
gvec& textureRroJGradOffset Bgsam.lerD sam)ler, vec& %,
vec !%!x, vec !%!., vec o//setC
gvec& textureRroJGradOffset Bgsam.lerDRect sam)ler, vec% %,
vec !%!x, vec !%!., ivec o//setC
gvec& textureRroJGradOffset Bgsam.lerDRect sam)ler, vec& %,
vec !%!x, vec !%!., ivec o//setC
"loat textureRroJGradOffset Bsam.lerDRect'hadow sam)ler,
vec& %,
vec !%!x, vec !%!., ivec o//setC
gvec& textureRroJGradOffset Bgsam.ler%D sam)ler, vec& %,
vec% !%!x, vec% !%!., vec% o//setC
"loat textureRroJGradOffset Bsam.ler1D'hadow sam)ler, vec& %,
"loat !%!x, "loat !%!., int o//setC
"loat textureRroJGradOffset Bsam.lerD'hadow sam)ler, vec& %,
vec !%!x, vec !%!., vec o//setC
Do a te1ture looku. .ro?ectivel(
and with e1.licit gradient as
descri6ed in textureRroJGrad,
as well as with o""set, as
descri6ed in textureOffset.
!!
Bui%t+in ,unctions
-he "ollowing te1ture "unctions are de.recated.
S$nta+ )escription
vec& texture1& Bsam.ler1D sam)ler,
"loat coor! Y, "loat -iasZ C
vec& texture1&RroJ Bsam.ler1D sam)ler,
vec coor! Y, "loat -iasZ C
vec& texture1&RroJ Bsam.ler1D sam)ler,
vec& coor! Y, "loat -iasZ C
vec& texture1&Dod Bsam.ler1D sam)ler,
"loat coor!, "loat lo!C
vec& texture1&RroJDod Bsam.ler1D sam)ler,
vec coor!, "loat lo!C
vec& texture1&RroJDod Bsam.ler1D sam)ler,
vec& coor!, "loat lo!C
De.recated. 'ee corres.onding signature
a6ove without E1DF in the name.
vec& texture%& Bsam.lerD sam)ler,
vec coor! Y, "loat -iasZ C
vec& texture%&RroJ Bsam.lerD sam)ler,
vec% coor! Y, "loat -iasZ C
vec& texture%&RroJ Bsam.lerD sam)ler,
vec& coor! Y, "loat -iasZ C
vec& texture%&Dod Bsam.lerD sam)ler,
vec coor!, "loat lo!C
vec& texture%&RroJDod Bsam.lerD sam)ler,
vec% coor!, "loat lo!C
vec& texture%&RroJDod Bsam.lerD sam)ler,
vec& coor!, "loat lo!C
De.recated. 'ee corres.onding signature
a6ove without EDF in the name.
vec& textureF& Bsam.ler%D sam)ler,
vec% coor! Y, "loat -iasZ C
vec& textureF&RroJ Bsam.ler%D sam)ler,
vec& coor! Y, "loat -iasZ C
vec& textureF&Dod Bsam.ler%D sam)ler,
vec% coor!, "loat lo!C
vec& textureF&RroJDod Bsam.ler%D sam)ler,
vec& coor!, "loat lo!C
De.recated. 'ee corres.onding signature
a6ove without E%DF in the name.
5se the te1ture coordinate coor! to do a
te1ture looku. in the %D te1ture currentl(
6ound to sam)ler. )or the .ro?ective
BERroJFC versions, the te1ture coordinate is
divided 6( coor!.6.
vec& textureCube Bsam.ler!u6e sam)ler,
vec% coor! Y, "loat -iasZ C
vec& textureCubeDod Bsam.ler!u6e sam)ler,
vec% coor!, "loat lo!C
De.recated. 'ee corres.onding signature
a6ove without E!u6eF in the name.
1""
Bui%t+in ,unctions
S$nta+ )escription
vec& sadow1& Bsam.ler1D'hadow sam)ler,
vec% coor! Y, "loat -iasZ C
vec& sadow%& Bsam.lerD'hadow sam)ler,
vec% coor! Y, "loat -iasZ C
vec& sadow1&RroJ Bsam.ler1D'hadow sam)ler,
vec& coor! Y, "loat -iasZ C
vec& sadow%&RroJ Bsam.lerD'hadow sam)ler,
vec& coor! Y, "loat -iasZ C
vec& sadow1&Dod Bsam.ler1D'hadow sam)ler,
vec% coor!, "loat lo!C
vec& sadow%&Dod Bsam.lerD'hadow sam)ler,
vec% coor!, "loat lo!C
vec& sadow1&RroJDodBsam.ler1D'hadow sam)ler,
vec& coor!, "loat lo!C
vec& sadow%&RroJDodBsam.lerD'hadow sam)ler,
vec& coor!, "loat lo!C
De.recated. 'ame "unctionalit( as the
Etexture] 6ased names a6ove with the same
signature.
3.3 %ragment ,rocessing %unctions
)ragment .rocessing "unctions are onl( availa6le in "ragment shaders.
Derivatives ma( 6e com.utationall( e1.ensive andAor numericall( unsta6le. -here"ore, an *.en/0
im.lementation ma( a..ro1imate the true derivatives 6( using a "ast 6ut not entirel( accurate derivative
com.utation. Derivatives are unde"ined within non;uni"orm control "low.
-he e1.ected 6ehavior o" a derivative is s.eci"ied using "orwardA6ackward di""erencing.
)orward di""erencing:
$ x!x$ x ~!$!x x!x 1a
!$!x x ~
$ x!x$ x
!x
16
Backward di""erencing:
$ x!x$ x ~!$!x x!x a
!$!x x ~
$ x$ x!x
!x
6
Hith single;sam.le rasteri@ation, !x PR 1.7 in e=uations 16 and 6. )or multi;sam.le rasteri@ation, !x P
.7 in e=uations 16 and 6.
d(d! is a..ro1imated similarl(, with . re.lacing x.
1"1
Bui%t+in ,unctions
A /0 im.lementation ma( use the a6ove or other methods to .er"orm the calculation, su6?ect to the
"ollowing conditions:
1. -he method ma( use .iecewise linear a..ro1imations. 'uch linear a..ro1imations im.l( that higher
order derivatives, d(dxBd(dxBxCC and a6ove, are unde"ined.
. -he method ma( assume that the "unction evaluated is continuous. -here"ore derivatives within the
6od( o" a non;uni"orm conditional are unde"ined.
%. -he method ma( di""er .er "ragment, su6?ect to the constraint that the method ma( var( 6( window
coordinates, not screen coordinates. -he invariance re=uirement descri6ed in section %. o" the
*.en/0 /ra.hics '(stem '.eci"ication, is rela1ed "or derivative calculations, 6ecause the method
ma( 6e a "unction o" "ragment location.
*ther .ro.erties that are desira6le, 6ut not re=uired, are:
&. )unctions should 6e evaluated within the interior o" a .rimitive Binter.olated, not e1tra.olatedC.
$. )unctions "or d(dx should 6e evaluated while holding ( constant. )unctions "or d(d! should 6e
evaluated while holding 1 constant. ,owever, mi1ed higher order derivatives, like d(dxBd(d!B.CC
and d(d!Bd(dxBxCC are unde"ined.
#. Derivatives o" constant arguments should 6e 7.
In some im.lementations, var(ing degrees o" derivative accurac( ma( 6e o6tained 6( .roviding /0 hints
Bsection $.% o" the *.en/0 /ra.hics '(stem '.eci"icationC, allowing a user to make an image =ualit(
versus s.eed trade o"".
S$nta+ )escription
gen-(.e d(dx Bgen-(.e )C Returns the derivative in 1 using local di""erencing "or
the in.ut argument ).
gen-(.e d(d! Bgen-(.e )C Returns the derivative in ( using local di""erencing "or
the in.ut argument ).
-hese two "unctions are commonl( used to estimate the
"ilter width used to anti;alias .rocedural te1tures. He
are assuming that the e1.ression is 6eing evaluated in
.arallel on a 'I8D arra( so that at an( given .oint in
time the value o" the "unction is known at the grid .oints
re.resented 6( the 'I8D arra(. 0ocal di""erencing
6etween 'I8D arra( elements can there"ore 6e used to
derive d)d1, d)d(, etc.
gen-(.e fwidt Bgen-(.e )C Returns the sum o" the a6solute derivative in 1 and (
using local di""erencing "or the in.ut argument ), i.e.,
abs Bd(dx B)CC K abs Bd(d! B)CCG
1"2
Bui%t+in ,unctions
3.9 ;oise %unctions
<oise "unctions are availa6le to "ragment, geometr(, and verte1 shaders. -he( are stochastic "unctions that
can 6e used to increase visual com.le1it(. Values returned 6( the "ollowing noise "unctions give the
a..earance o" randomness, 6ut are not trul( random. -he noise "unctions 6elow are de"ined to have the
"ollowing characteristics:
-he return valueBsC are alwa(s in the range Y;1.7,1.7Z, and cover at least the range Y;7.#, 7.#Z, with a
/aussian;like distri6ution.
-he return valueBsC have an overall average o" 7.7
-he( are re.eata6le, in that a .articular in.ut value will alwa(s .roduce the same return value
-he( are statisticall( invariant under rotation Bi.e., no matter how the domain is rotated, it has the same
statistical characterC
-he( have a statistical invariance under translation Bi.e., no matter how the domain is translated, it has
the same statistical characterC
-he( t(.icall( give di""erent results under translation.
-he s.atial "re=uenc( is narrowl( concentrated, centered somewhere 6etween 7.$ to 1.7.
-he( are !
1
continuous ever(where Bi.e., the "irst derivative is continuousC
S$nta+ )escription
"loat noise1 Bgen-(.e xC Returns a 1D noise value 6ased on the in.ut value x.
vec noise% Bgen-(.e xC Returns a D noise value 6ased on the in.ut value x.
vec% noiseF Bgen-(.e xC Returns a %D noise value 6ased on the in.ut value x.
vec& noiseG Bgen-(.e xC Returns a &D noise value 6ased on the in.ut value x.
1"3
Bui%t+in ,unctions
3.16 Geometr$ Shader %unctions
-hese "unctions are onl( availa6le in geometr( shaders.
S$nta+ )escription
void LmitUertex BC +mit the current values o" out.ut varia6les to the current out.ut .rimitive.
*n return "rom this call, the values o" out.ut varia6les are unde"ined. 'ee
6elow "or more discussion.
void LndRrimitive BC !om.letes the current out.ut .rimitive and starts a new one. <o verte1 is
emitted. 'ee 6elow "or more discussion.
-he "unction LmitUertexBC s.eci"ies that a verte1 is com.leted. A verte1 is added to the current out.ut
.rimitive using the current values o" the geometr( shaderIs out.ut varia6les, including gl_%oint+i5e,
gl_li)Distance, gl_La.er, gl_%osition and gl_%rimitive&D. -he values o" all these out.ut varia6les are
unde"ined a"ter a call to LmitUertexBC. I" a geometr( shader invocation has emitted more vertices than
.ermitted 6( the out.ut la(out =uali"ier max$vertices, the results o" calling LmitUertexBC are unde"ined.
-he "unction LndRrimitiveBC s.eci"ies that the current out.ut .rimitive is com.leted and a new out.ut
.rimitive Bo" the same t(.eC will started 6( an( su6se=uent LmitUertexBC. -his "unction does not emit a
verte1. I" the out.ut la(out is declared to 6e E.ointsF, calling LndRrimitiveBC is o.tional.
A geometr( shader starts with an out.ut .rimitive containing no vertices. Hhen a geometr( shader
terminates, the current out.ut .rimitive is automaticall( com.leted. It is not necessar( to call
LndRrimitiveBC i" the geometr( shader writes onl( a single .rimitive.
1"4
9 Shading Language Grammar
-he grammar is "ed "rom the out.ut o" le1ical anal(sis. -he tokens returned "rom le1ical anal(sis are
8TTRI-LTE CONST -OOL FLO8T INT LINT
-RE8O CONTINLE O ELSE FOR IF ISC8R RETLRN SPITCG C8SE EF8LLT
-VEC2 -VEC4 -VEC1 IVEC2 IVEC4 IVEC1 LVEC2 LVEC4 LVEC1 VEC2 VEC4 VEC1
98T2 98T4 98T1 CENTROI IN OLT INOLT LNIFOR9 V8RQIN!
NO@ERS@ECTIVE FL8T S9OOTG L8QOLT
98T2R2 98T2R4 98T2R1
98T4R2 98T4R4 98T4R1
98T1R2 98T1R4 98T1R1
S89@LER' S89@LER2 S89@LER4 S89@LERCL-E S89@LER'SG8OP S89@LER2SG8OP
S89@LERCL-ESG8OP S89@LER'8RR8Q S89@LER28RR8Q S89@LER'8RR8QSG8OP
S89@LER28RR8QSG8OP IS89@LER' IS89@LER2 IS89@LER4 IS89@LERCL-E
IS89@LER'8RR8Q IS89@LER28RR8Q LS89@LER' LS89@LER2 LS89@LER4
LS89@LERCL-E LS89@LER'8RR8Q LS89@LER28RR8Q
S89@LER2RECT S89@LER2RECTSG8OP IS89@LER2RECT LS89@LER2RECT
S89@LER-LFFER IS89@LER-LFFER LS89@LER-LFFER
S89@LER29S IS89@LER29S LS89@LER29S
S89@LER29S8rra& IS89@LER29S8rra& LS89@LER29S8rra&
STRLCT VOI PGILE
IENTIFIER TQ@E_N89E FLO8TCONST8NT INTCONST8NT LINTCONST8NT -OOLCONST8NT
FIEL_SELECTION
LEFT_O@ RI!GT_O@
INC_O@ EC_O@ LE_O@ !E_O@ EM_O@ NE_O@
8N_O@ OR_O@ ROR_O@ 9LL_8SSI!N IV_8SSI!N 8_8SSI!N
9O_8SSI!N LEFT_8SSI!N RI!GT_8SSI!N 8N_8SSI!N ROR_8SSI!N OR_8SSI!N
SL-_8SSI!N
LEFT_@8REN RI!GT_@8REN LEFT_-R8COET RI!GT_-R8COET LEFT_-R8CE RI!GT_-R8CE OT
CO998 COLON EML8L SE9ICOLON -8N! 8SG TILE @LLS ST8R SL8SG @ERCENT
LEFT_8N!LE RI!GT_8N!LE VERTIC8L_-8R C8RET 89@ERS8N MLESTION
INV8RI8NT
GI!G_@RECISION 9EIL9_@RECISION LOP_@RECISION @RECISION
-he "ollowing descri6es the grammar "or the *.en/0 'hading 0anguage in terms o" the a6ove tokens.
varia-le_i!enti/ier:
&D10T&$&1R
1"5
! Shading Language Gra**ar
)rimar._ex)ression:
varia-le_i!enti/ier
&0T(0+TG0T
B&0T(0+TG0T
$L(GT(0+TG0T
<((L(0+TG0T
L1$T_%GR10 ex)ression R&*HT_%GR10
)ost/ix_ex)ression:
)rimar._ex)ression
)ost/ix_ex)ression L1$T_<RGI1T integer_ex)ression R&*HT_<RGI1T
/unction_call
)ost/ix_ex)ression D(T $&1LD_+1L1T&(0
)ost/ix_ex)ression &0_(%
)ost/ix_ex)ression D1_(%
integer_ex)ression:
ex)ression
/unction_call:
/unction_call_or_met'o!
/unction_call_or_met'o!:
/unction_call_generic
)ost/ix_ex)ression D(T /unction_call_generic
/unction_call_generic:
/unction_call_'ea!er_4it'_)arameters R&*HT_%GR10
/unction_call_'ea!er_no_)arameters R&*HT_%GR10
/unction_call_'ea!er_no_)arameters:
/unction_call_'ea!er V(&D
/unction_call_'ea!er
/unction_call_'ea!er_4it'_)arameters:
/unction_call_'ea!er assignment_ex)ression
/unction_call_'ea!er_4it'_)arameters (::G assignment_ex)ression
/unction_call_'ea!er:
1"6
! Shading Language Gra**ar
/unction_i!enti/ier L1$T_%GR10
AA *rammar 0ote: onstructors loo3 li3e /unctions, -ut lexical anal.sis recogni5e! most o/ t'em as
AA 3e.4or!s. T'e. are no4 recogni5e! t'roug' Jt.)e_s)eci/ierK.
/unction_i!enti/ier:
t.)e_s)eci/ier
&D10T&$&1R
$&1LD_+1L1T&(0
unar._ex)ression:
)ost/ix_ex)ression
&0_(% unar._ex)ression
D1_(% unar._ex)ression
unar._o)erator unar._ex)ression
AA *rammar 0ote: 0o tra!itional st.le t.)e casts.
unar._o)erator:
%LB+
DG+H
<G0*
T&LD1
AA *rammar 0ote: 0o LML or LNL unar. o)s. %ointers are not su))orte!.
multi)licative_ex)ression:
unar._ex)ression
multi)licative_ex)ression +TGR unar._ex)ression
multi)licative_ex)ression +LG+H unar._ex)ression
multi)licative_ex)ression %1R10T unar._ex)ression
a!!itive_ex)ression:
multi)licative_ex)ression
a!!itive_ex)ression %LB+ multi)licative_ex)ression
a!!itive_ex)ression DG+H multi)licative_ex)ression
s'i/t_ex)ression:
a!!itive_ex)ression
s'i/t_ex)ression L1$T_(% a!!itive_ex)ression
1"7
! Shading Language Gra**ar
s'i/t_ex)ression R&*HT_(% a!!itive_ex)ression
relational_ex)ression:
s'i/t_ex)ression
relational_ex)ression L1$T_G0*L1 s'i/t_ex)ression
relational_ex)ression R&*HT_G0*L1 s'i/t_ex)ression
relational_ex)ression L1_(% s'i/t_ex)ression
relational_ex)ression *1_(% s'i/t_ex)ression
e6ualit._ex)ression:
relational_ex)ression
e6ualit._ex)ression 1O_(% relational_ex)ression
e6ualit._ex)ression 01_(% relational_ex)ression
an!_ex)ression:
e6ualit._ex)ression
an!_ex)ression G:%1R+G0D e6ualit._ex)ression
exclusive_or_ex)ression:
an!_ex)ression
exclusive_or_ex)ression GR1T an!_ex)ression
inclusive_or_ex)ression:
exclusive_or_ex)ression
inclusive_or_ex)ression V1RT&GL_<GR exclusive_or_ex)ression
logical_an!_ex)ression:
inclusive_or_ex)ression
logical_an!_ex)ression G0D_(% inclusive_or_ex)ression
logical_xor_ex)ression:
logical_an!_ex)ression
logical_xor_ex)ression P(R_(% logical_an!_ex)ression
logical_or_ex)ression:
logical_xor_ex)ression
logical_or_ex)ression (R_(% logical_xor_ex)ression
con!itional_ex)ression:
logical_or_ex)ression
1"
! Shading Language Gra**ar
logical_or_ex)ression OB1+T&(0 ex)ression (L(0 assignment_ex)ression
assignment_ex)ression:
con!itional_ex)ression
unar._ex)ression assignment_o)erator assignment_ex)ression
assignment_o)erator:
1OBGL
:BL_G++&*0
D&V_G++&*0
:(D_G++&*0
GDD_G++&*0
+B<_G++&*0
L1$T_G++&*0
R&*HT_G++&*0
G0D_G++&*0
P(R_G++&*0
(R_G++&*0
ex)ression:
assignment_ex)ression
ex)ression (::G assignment_ex)ression
constant_ex)ression:
con!itional_ex)ression
!eclaration:
/unction_)rotot.)e +1:&(L(0
init_!eclarator_list +1:&(L(0
%R1&+&(0 )recision_6uali/ier t.)e_s)eci/ier_no_)rec +1:&(L(0
t.)e_6uali/ier &D10T&$&1R L1$T_<RG1 struct_!eclaration_list R&*HT_<RG1 +1:&(L(0
t.)e_6uali/ier &D10T&$&1R L1$T_<RG1 struct_!eclaration_list R&*HT_<RG1
&D10T&$&1R +1:&(L(0
t.)e_6uali/ier &D10T&$&1R L1$T_<RG1 struct_!eclaration_list R&*HT_<RG1
&D10T&$&1R L1$T_<RGI1T R&*HT_<RGI1T +1:&(L(0
t.)e_6uali/ier &D10T&$&1R L1$T_<RG1 struct_!eclaration_list R&*HT_<RG1
&D10T&$&1R L1$T_<RGI1T constant_ex)ression R&*HT_<RGI1T +1:&(L(0
t.)e_6uali/ier +1:&(L(0
/unction_)rotot.)e:
1"!
! Shading Language Gra**ar
/unction_!eclarator R&*HT_%GR10
/unction_!eclarator:
/unction_'ea!er
/unction_'ea!er_4it'_)arameters
/unction_'ea!er_4it'_)arameters:
/unction_'ea!er )arameter_!eclaration
/unction_'ea!er_4it'_)arameters (::G )arameter_!eclaration
/unction_'ea!er:
/ull._s)eci/ie!_t.)e &D10T&$&1R L1$T_%GR10
)arameter_!eclarator:
t.)e_s)eci/ier &D10T&$&1R
t.)e_s)eci/ier &D10T&$&1R L1$T_<RGI1T constant_ex)ression R&*HT_<RGI1T
)arameter_!eclaration:
)arameter_t.)e_6uali/ier )arameter_6uali/ier )arameter_!eclarator
)arameter_6uali/ier )arameter_!eclarator
)arameter_t.)e_6uali/ier )arameter_6uali/ier )arameter_t.)e_s)eci/ier
)arameter_6uali/ier )arameter_t.)e_s)eci/ier
)arameter_6uali/ier:
AM em)t. MA
&0
(BT
&0(BT
)arameter_t.)e_s)eci/ier:
t.)e_s)eci/ier
init_!eclarator_list:
single_!eclaration
init_!eclarator_list (::G &D10T&$&1R
init_!eclarator_list (::G &D10T&$&1R L1$T_<RGI1T R&*HT_<RGI1T
init_!eclarator_list (::G &D10T&$&1R L1$T_<RGI1T constant_ex)ression
R&*HT_<RGI1T
init_!eclarator_list (::G &D10T&$&1R L1$T_<RGI1T
R&*HT_<RGI1T 1OBGL initiali5er
11"
! Shading Language Gra**ar
init_!eclarator_list (::G &D10T&$&1R L1$T_<RGI1T constant_ex)ression
R&*HT_<RGI1T 1OBGL initiali5er
init_!eclarator_list (::G &D10T&$&1R 1OBGL initiali5er
single_!eclaration:
/ull._s)eci/ie!_t.)e
/ull._s)eci/ie!_t.)e &D10T&$&1R
/ull._s)eci/ie!_t.)e &D10T&$&1R L1$T_<RGI1T R&*HT_<RGI1T
/ull._s)eci/ie!_t.)e &D10T&$&1R L1$T_<RGI1T constant_ex)ression R&*HT_<RGI1T
/ull._s)eci/ie!_t.)e &D10T&$&1R L1$T_<RGI1T R&*HT_<RGI1T 1OBGL initiali5er
/ull._s)eci/ie!_t.)e &D10T&$&1R L1$T_<RGI1T constant_ex)ression
R&*HT_<RGI1T 1OBGL initiali5er
/ull._s)eci/ie!_t.)e &D10T&$&1R 1OBGL initiali5er
&0VGR&G0T &D10T&$&1R
AA *rammar 0ote: 0o LenumL, or Lt.)e!e/L.
/ull._s)eci/ie!_t.)e:
t.)e_s)eci/ier
t.)e_6uali/ier t.)e_s)eci/ier
invariant_6uali/ier:
&0VGR&G0T
inter)olation_6uali/ier:
+:((TH
$LGT
0(%1R+%1T&V1
la.out_6uali/ier:
LGQ(BT L1$T_%GR10 la.out_6uali/ier_i!_list R&*HT_%GR10
la.out_6uali/ier_i!_list:
la.out_6uali/ier_i!
la.out_6uali/ier_i!_list (::G la.out_6uali/ier_i!
la.out_6uali/ier_i!:
&D10T&$&1R
&D10T&$&1R 1OBGL &0T(0+TG0T
111
! Shading Language Gra**ar
)arameter_t.)e_6uali/ier:
(0+T
t.)e_6uali/ier:
storage_6uali/ier
la.out_6uali/ier
la.out_6uali/ier storage_6uali/ier
inter)olation_6uali/ier storage_6uali/ier
inter)olation_6uali/ier
invariant_6uali/ier storage_6uali/ier
invariant_6uali/ier inter)olation_6uali/ier storage_6uali/ier
invariant
storage_6uali/ier:
(0+T
GTTR&<BT1 AA Vertex onl..
VGRQ&0*
10TR(&D VGRQ&0*
&0
(BT
10TR(&D &0
10TR(&D (BT
B0&$(R:
t.)e_s)eci/ier:
t.)e_s)eci/ier_no_)rec
)recision_6uali/ier t.)e_s)eci/ier_no_)rec
t.)e_s)eci/ier_no_)rec:
t.)e_s)eci/ier_nonarra.
t.)e_s)eci/ier_nonarra. L1$T_<RGI1T R&*HT_<RGI1T
t.)e_s)eci/ier_nonarra. L1$T_<RGI1T constant_ex)ression R&*HT_<RGI1T
t.)e_s)eci/ier_nonarra.:
V(&D
$L(GT
&0T
112
! Shading Language Gra**ar
B&0T
<((L
V12
V1@
V14
<V12
<V1@
<V14
&V12
&V1@
&V14
BV12
BV1@
BV14
:GT2
:GT@
:GT4
:GT2P2
:GT2P@
:GT2P4
:GT@P2
:GT@P@
:GT@P4
:GT4P2
:GT4P@
:GT4P4
+G:%L1R1D
+G:%L1R2D
+G:%L1R@D
+G:%L1RB<1
+G:%L1R1D+HGD(R
+G:%L1R2D+HGD(R
+G:%L1RB<1+HGD(R
+G:%L1R1DGRRGQ
+G:%L1R2DGRRGQ
+G:%L1R1DGRRGQ+HGD(R
+G:%L1R2DGRRGQ+HGD(R
113
! Shading Language Gra**ar
&+G:%L1R1D
&+G:%L1R2D
&+G:%L1R@D
&+G:%L1RB<1
&+G:%L1R1DGRRGQ
&+G:%L1R2DGRRGQ
B+G:%L1R1D
B+G:%L1R2D
B+G:%L1R@D
B+G:%L1RB<1
B+G:%L1R1DGRRGQ
B+G:%L1R2DGRRGQ
+G:%L1R2DR1T
+G:%L1R2DR1T+HGD(R
&+G:%L1R2DR1T
B+G:%L1R2DR1T
+G:%L1R<B$$1R
&+G:%L1R<B$$1R
B+G:%L1R<B$$1R
+G:%L1R2D:+
&+G:%L1R2D:+
B+G:%L1R2D:+
+G:%L1R2D:+Grra.
&+G:%L1R2D:+Grra.
B+G:%L1R2D:+Grra.
struct_s)eci/ier
TQ%1_0G:1
)recision_6uali/ier:
H&*H_%R1&+&(0
:1D&B:_%R1&+&(0
L(R_%R1&+&(0
struct_s)eci/ier:
+TRBT &D10T&$&1R L1$T_<RG1 struct_!eclaration_list R&*HT_<RG1
+TRBT L1$T_<RG1 struct_!eclaration_list R&*HT_<RG1
struct_!eclaration_list:
struct_!eclaration
struct_!eclaration_list struct_!eclaration
114
! Shading Language Gra**ar
struct_!eclaration:
t.)e_s)eci/ier struct_!eclarator_list +1:&(L(0
t.)e_6uali/ier t.)e_s)eci/ier struct_!eclarator_list +1:&(L(0
struct_!eclarator_list:
struct_!eclarator
struct_!eclarator_list (::G struct_!eclarator
struct_!eclarator:
&D10T&$&1R
&D10T&$&1R L1$T_<RGI1T R&*HT_<RGI1T
&D10T&$&1R L1$T_<RGI1T constant_ex)ression R&*HT_<RGI1T
initiali5er:
assignment_ex)ression
!eclaration_statement:
!eclaration
statement:
com)oun!_statement
sim)le_statement
AA *rammar 0ote: la-ele! statements /or +R&TH onl.7 LgotoL is not su))orte!.
sim)le_statement:
!eclaration_statement
ex)ression_statement
selection_statement
s4itc'_statement
case_la-el
iteration_statement
;um)_statement
com)oun!_statement:
L1$T_<RG1 R&*HT_<RG1
L1$T_<RG1 statement_list R&*HT_<RG1
statement_no_ne4_sco)e:
115
! Shading Language Gra**ar
com)oun!_statement_no_ne4_sco)e
sim)le_statement
com)oun!_statement_no_ne4_sco)e:
L1$T_<RG1 R&*HT_<RG1
L1$T_<RG1 statement_list R&*HT_<RG1
statement_list:
statement
statement_list statement
ex)ression_statement:
+1:&(L(0
ex)ression +1:&(L(0
selection_statement:
&$ L1$T_%GR10 ex)ression R&*HT_%GR10 selection_rest_statement
selection_rest_statement:
statement 1L+1 statement
statement
con!ition:
ex)ression
/ull._s)eci/ie!_t.)e &D10T&$&1R 1OBGL initiali5er
s4itc'_statement:
+R&TH L1$T_%GR10 ex)ression R&*HT_%GR10 L1$T_<RG1 s4itc'_statement_list
R&*HT_<RG1
s4itc'_statement_list:
AM not'ing MA
statement_list
case_la-el:
G+1 ex)ression (L(0
D1$GBLT (L(0
iteration_statement:
RH&L1 L1$T_%GR10 con!ition R&*HT_%GR10 statement_no_ne4_sco)e
D( statement RH&L1 L1$T_%GR10 ex)ression R&*HT_%GR10 +1:&(L(0
$(R L1$T_%GR10 /or_init_statement /or_rest_statement R&*HT_%GR10
statement_no_ne4_sco)e
116
! Shading Language Gra**ar
/or_init_statement:
ex)ression_statement
!eclaration_statement
con!itiono)t:
con!ition
AM em)t. MA
/or_rest_statement:
con!itiono)t +1:&(L(0
con!itiono)t +1:&(L(0 ex)ression
;um)_statement:
(0T&0B1 +1:&(L(0
<R1GI +1:&(L(0
R1TBR0 +1:&(L(0
R1TBR0 ex)ression +1:&(L(0
D&+GRD +1:&(L(0 AA $ragment s'a!er onl..
AA *rammar 0ote: 0o LgotoL. *otos are not su))orte!.
translation_unit:
external_!eclaration
translation_unit external_!eclaration
external_!eclaration:
/unction_!e/inition
!eclaration
/unction_!e/inition:
/unction_)rotot.)e com)oun!_statement_no_ne4_sco)e

16 Issues
Nssue 1: Is it oka( to reserve verte1 in.ut 6locks and "ragment out.ut 6locks "or "uture use^
117
1" Issues
Resolution: _es, leave these reserved "or now.
Nssue %: Do we allow .artial =uali"ications on 6lock mem6ers, with .art e1.licit and .art inherited^ )or
e1am.le,
in "oo [
smooth vec tG
\G
Resolution: _es, i" the current method o" doing the grammar can su..ort it. *therwise: <o, until Issue %
is resolved, kee. this sim.le: <eed "ull =uali"ication on mem6ers Bor no =uali"icationC.
Nssue F: )ollow;on to issue : 'hould we unhook `centroidJ "rom `inJ to make order o" =uali"ication not
matter, thus assuring no grammar issues HR- inheriting =uali"ication "rom the =uali"ier o" the inter"ace^
Resolution: 5nless we have time, no, this is a cree.ing "eature.
Nssue G? 'hould we allow 6lock names to 6e used as other things^
Resolution: <o.
Nssue E: Do we reall( want to call inter"ace 6locks `inter"aceJ^
Resolution: _es, until someone suggests a 6etter name.
Nssue Y? ,ow does the A2I re"er to 6lock mem6ers^
Resolution: _ou e1.ress it the wa( (ou would in the shader, 6ut in the A2I, use 6lock name where
instance name is used in the shader.
Nssue Z? Do multi;sam.le te1tures have 0*D^ Hill the(^ I" so, can we have lod arguments^
Resolution: <o 0*Ds now or "uture, treat like te1ture rectangle.
Nssue [? gl>Vertices, the num6er o" vertices in.ut to the geometr( shader, is a com.ile time constant set
6( the linker: -he linker cannot set a com.ile time constant, it is too late "or that. It does not su..ort a
model o" com.iling at com.ile time.
2ossi6le Resolution A: 0eave all arra(s as unsi@ed and use a uni"orm "or gl>Vertices.
2ossi6le Resolution B: -he coder makes their own const int declaration in shaders to match their
intended .ur.ose. -he linker veri"ies the in.ut arra(Is si@e match the .rimitive t(.e.
2ossi6le Resolution !: -he shader o6?ect on the A2I side has a new .arameter sa(ing the si@e used at
com.ile time and the linker sees i" the( all match.
Resolution D: Add directive to sa( what the in.ut t(.e is, and that ends u. setting the constant at com.ile
time. -he constant is not intrinsicall( declared unless the in.ut t(.e is declared. Also allow declaring
out.ut t(.e and a ma1 vertices out.ut count. +rror to have in.ut arra( si@ed to an inconsistent si@e o" that
declared. 'a( that .lengthBC works 6ecause in.uts declared a"ter an in.ut .rimitive t(.e e1.licitl( si@es
those arra(s. At least one shader is re=uired to sa( the .rimitive t(.e "or in.ut and "or out.ut and "or the
ma1 si@e.
Nssue .? Hhere is the "ragment read o" gl_%rimitive&D^ -his is currentl( missing in the language
s.eci"ication.
Resolution: 2ut it in. 'ee g.u>shader&.
11
1" Issues
Nssue 1-? Do we have consistent su..ort "or gl>0a(er^
Resolution: _es, ARB>geometr(>shader "ull( descri6es gl>0a(er.
11!

You might also like