Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>GL2</strong> <strong>Shading</strong> <strong>Language</strong><br />
Bill Licea-Kane<br />
Licea Kane<br />
ATI Research, Inc.<br />
bill@ati bill@ati.com<br />
.com
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Back Back to to the the future<br />
future<br />
“As As far as generating pictures from data is concerned, we<br />
feel the display processor should be a specialized device,<br />
capable only of generating pictures from read-only read only<br />
representations in core.”<br />
Myer and Sutherland, On the Design of Display Processors.<br />
Communications of the ACM, Volume 11 Number 6, June, 1968
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Back Back to to the the future<br />
future<br />
• June June 30, 30, 1992<br />
1992<br />
– OpenGL OpenGL 1.0 1.0 Final Final Draft Draft published<br />
published
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Back Back to to the the future<br />
future<br />
“OpenGL OpenGL does not provide a programming language. Its<br />
function may be controlled by turning operations on or off or<br />
specifying parameters to operations, but the rendering<br />
algorithms are essentially fixed. One reason for this decision<br />
is that, for performance reasons, graphics hardware is usually<br />
designed to apply certain operations in a specific order;<br />
replacing these operations with arbitrary algorithms is<br />
usually infeasible. Programmability would conflict with<br />
keeping the API close to the hardware and thus with the goal<br />
of maximum performance.”<br />
Segal and Akeley, Akeley,<br />
The The Design Design of of the the OpenGL OpenGL Graphics Graphics<br />
Interface, Interface,<br />
1994
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Back Back to to the the future<br />
future<br />
• August August 23, 23, 1993<br />
1993<br />
– First First OpenGL OpenGL 1.0 1.0 implementation implementation ships<br />
ships
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Back Back to to the the future<br />
future<br />
billa@entropy.<br />
billa@entropy.sps<br />
sps.mot.com .mot.com (William C. Archibald) writes:<br />
|> 1)Quite a few modern rendering techniques and renderers<br />
|> support shader function calls at arbitrary points<br />
|> across a surface being rendered. ... What I would<br />
|> _like_ to do is call the shader at visible<br />
|> (front-facing) (front facing) points. Is there _any_ way to do<br />
|> this kind of thing in OpenGL?<br />
Not with the current API. One could extend OpenGL in this<br />
manner...<br />
Akeley, Akeley,<br />
comp.graphics.opengl<br />
comp.graphics. opengl posting, 13 Oct 1993
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Back Back to to the the present<br />
present<br />
• February February 27, 27, 2003<br />
2003<br />
– arb-gl2 arb gl2 workgroup publishes<br />
Draft Draft OpenGL <strong>Shading</strong> <strong>Language</strong><br />
– End End construction construction zone<br />
zone
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Today<br />
Today<br />
• How How we we replace replace Fixed Fixed Function<br />
Function<br />
• Review Review <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
• <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong> <strong>Language</strong> examples<br />
examples<br />
• Procedural Procedural Shader Shader demo<br />
demo
Vertex (object)<br />
Normal<br />
Color<br />
SecondaryColor<br />
TexCoord TexCoordnn<br />
EdgeFlag<br />
OpenGL<br />
Fixed Function Vertex<br />
Transform<br />
[MVP],[MV],[MV] -T<br />
[MVP],[MV],[MV]<br />
Lighting<br />
Texgen Texture<br />
Matrix Matrixnn<br />
[0,1]<br />
[0,1]<br />
Vertex (clip)<br />
Vertex (eye)<br />
Front&Back<br />
Color<br />
Front&Back<br />
SecondaryColor<br />
TexCoord TexCoordnn<br />
EdgeFlag
Vertex (object)<br />
Normal<br />
Color<br />
SecondaryColor<br />
TexCoord TexCoordnn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Processor<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Vertex (clip)<br />
Vertex (eye)<br />
Front&Back<br />
Color<br />
Front&Back<br />
SecondaryColor<br />
TexCoord TexCoordnn<br />
EdgeFlag
Vertex<br />
Normal<br />
Color<br />
SecondaryColor<br />
MultiTexCoord<br />
MultiTexCoordnn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Processor<br />
Names<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
Front&Back<br />
Color<br />
Front&Back<br />
SecondaryColor<br />
TexCoord[nn]<br />
TexCoord<br />
EdgeFlag
Vertex<br />
Normal<br />
Color<br />
SecondaryColor<br />
MultiTexCoord<br />
MultiTexCoordnn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Processor<br />
Add Fog Coordinate<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
Front&Back<br />
Color<br />
Front&Back<br />
SecondaryColor<br />
FogCoord FogFragCoord<br />
TexCoord[nn]<br />
TexCoord<br />
EdgeFlag
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
<strong>GL2</strong> Vertex Processor<br />
Generalize Input<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
Front&Back<br />
Color<br />
Front&Back<br />
SecondaryColor<br />
FogFragCoord<br />
TexCoord[nn]<br />
TexCoord<br />
EdgeFlag EdgeFlag
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Processor<br />
Generalize Output<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
EdgeFlag
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Processor<br />
Additional Output<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
PointSize<br />
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
EdgeFlag
<strong>GL2</strong> Vertex Limits<br />
Active Attributes (16 vec4)<br />
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
EdgeFlag<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
PointSize<br />
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
EdgeFlag
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Limits<br />
Uniform (512 words)<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
PointSize<br />
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
EdgeFlag
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Limits<br />
Varying (32 floats)<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
PointSize<br />
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
EdgeFlag
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Limits<br />
Instructions (Virtual)<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
PointSize<br />
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
EdgeFlag
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Limits<br />
Temporaries (Virtual)<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Position<br />
ClipVertex<br />
PointSize<br />
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
EdgeFlag
Attribute0<br />
Attribute1<br />
Attribute2<br />
…<br />
Attribute Attributenn<br />
EdgeFlag<br />
<strong>GL2</strong> Vertex Limits<br />
Texture Units (1)<br />
Uniform<br />
Vertex<br />
Shader<br />
Temporaries<br />
Texture<br />
Position<br />
ClipVertex<br />
PointSize<br />
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
EdgeFlag
Position<br />
Position<br />
Position<br />
ClipVertex<br />
ClipVertex<br />
ClipVertex<br />
Varying Varyingnn<br />
Varying Varyingnn<br />
Varying Varyingnn<br />
EdgeFlag<br />
EdgeFlag<br />
EdgeFlag<br />
OpenGL<br />
Fixed Function Rasterization<br />
RasterPos<br />
Varying Varyingnn<br />
Primitive<br />
Assembly<br />
Flatshade<br />
Culling or<br />
Clipping<br />
Face<br />
Processing<br />
Point<br />
Line<br />
Polygon<br />
Rasterization<br />
Pixel Rectangle<br />
Bitmap<br />
Rasterization<br />
Varying Varyingnn<br />
Coord
Color<br />
OpenGL<br />
Fixed Function Fragment<br />
SecondaryColor<br />
TexCoord[nn]<br />
TexCoord<br />
z(|ze |,f )<br />
Depth<br />
Coord<br />
FrontFacing<br />
Tex<br />
nn<br />
TE<br />
nn<br />
Sum Fog<br />
[0,1]<br />
Color<br />
Depth<br />
Coord<br />
FrontFacing
Color<br />
TexCoord[nn]<br />
TexCoord<br />
z(|ze |,f )<br />
Coord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Processor<br />
SecondaryColor<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
Color<br />
Depth Depth<br />
Temporaries<br />
Coord<br />
FrontFacing
Color0<br />
Color1<br />
TexCoord[nn]<br />
TexCoord<br />
FogFragCoord<br />
FragCoord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Processor<br />
Names<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
FragColor<br />
FragDepth FragDepth<br />
Temporaries<br />
FragCoord<br />
FrontFacing
Varying0<br />
Varying1<br />
Varying2<br />
FragCoord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Processor<br />
…<br />
Varying Varyingnn<br />
Generalize Input<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
FragColor<br />
FragDepth FragDepth<br />
Temporaries<br />
FragCoord<br />
FrontFacing
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
FragCoord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Limits<br />
Uniforms (64 words)<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
FragColor<br />
FragDepth FragDepth<br />
Temporaries<br />
FragCoord<br />
FrontFacing
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
FragCoord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Limits<br />
Varying (32 floats)<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
FragColor<br />
FragDepth FragDepth<br />
Temporaries<br />
FragCoord<br />
FrontFacing
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
FragCoord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Limits<br />
Instructions (virtual)<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
FragColor<br />
FragDepth FragDepth<br />
Temporaries<br />
FragCoord<br />
FrontFacing
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
FragCoord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Limits<br />
Temporaries (virtual)<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
FragColor<br />
FragDepth FragDepth<br />
Temporaries<br />
FragCoord<br />
FrontFacing
Varying0<br />
Varying1<br />
Varying2<br />
…<br />
Varying Varyingnn<br />
FragCoord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Limits<br />
Texture Units (2)<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
FragColor<br />
FragDepth FragDepth<br />
Temporaries<br />
FragCoord<br />
FrontFacing
Varying0<br />
Varying1<br />
Varying2<br />
FragCoord<br />
FrontFacing<br />
<strong>GL2</strong> Fragment Limits<br />
Fragment Output (fixed)<br />
…<br />
Varying Varyingnn<br />
Uniform Texture<br />
Fragment<br />
Shader<br />
FragColor<br />
FragDepth FragDepth<br />
Temporaries<br />
FragCoord<br />
FrontFacing
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Today<br />
Today<br />
• How How we we replace replace Fixed Fixed Function<br />
Function<br />
• Review Review <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
• <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong> <strong>Language</strong> examples<br />
examples<br />
• Procedural Procedural Shader Shader demo<br />
demo
#<br />
#define<br />
#undef<br />
undef<br />
#if<br />
#ifdef<br />
ifdef<br />
#ifndef<br />
ifndef<br />
#else<br />
#elif<br />
elif<br />
#endif<br />
endif<br />
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Preprocessor Preprocessor directives<br />
directives<br />
#error<br />
#pragma<br />
pragma optimize(on|off)<br />
#pragma<br />
pragma debug(on|off)<br />
#line line file<br />
__LINE__<br />
__FILE__<br />
__VERSION__<br />
// comment<br />
/* comment */
• void<br />
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Types<br />
Types<br />
• float vec2 vec3 vec4<br />
• mat2 mat3 mat4<br />
• int ivec2 ivec2 ivec3 ivec3 ivec4<br />
• bool bvec2 bvec2 bvec3 bvec3 bvec4<br />
bvec4<br />
• samplernnD sampler samplernD D samplerCube samplerCube samplerShadownnD<br />
samplerShadownD<br />
samplerShadow
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Types<br />
Types<br />
• struct struct (with (with some some minor minor restrictions)<br />
restrictions)<br />
– No No qualifiers<br />
qualifiers<br />
– No No bit bit fields<br />
fields<br />
– No No forward forward references<br />
references<br />
– No No in-place in-place in place definitions<br />
definitions<br />
– No anonymous structures
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Types<br />
Types<br />
• arrays arrays ( ( with with some some minor minor restrictions)<br />
restrictions)<br />
– one one dimensional<br />
dimensional<br />
– size size is is integral integral constant constant expression<br />
expression<br />
– can can declare unsized unsized array, array, but…<br />
– specify size and type of array ONCE<br />
– any basic basic type type and and struct<br />
struct<br />
– NO initialization at declaration
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Reserved Reserved Types<br />
Types<br />
• half half hvec2 hvec2 hvec3 hvec3 hvec4<br />
hvec4<br />
• fixed fixed fvec2 fvec2 fvec3 fvec3 fvec4<br />
fvec4<br />
• double double dvec2 dvec2 dvec3 dvec3 dvec4<br />
dvec4<br />
• sampler2DRect sampler2DRect sampler3DRect<br />
sampler3DRect
• global<br />
global<br />
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
– outside outside function<br />
function<br />
Scope<br />
Scope<br />
– shared shared globals globals must must be same type<br />
• local local (nested)<br />
(nested)<br />
– within within function function definition<br />
definition<br />
– within within function<br />
function<br />
– within within compound compound statement<br />
statement
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
• const<br />
const<br />
• attribute<br />
attribute<br />
• uniform<br />
uniform<br />
• varying<br />
varying<br />
• (default)<br />
(default)<br />
Type Type Qualifiers<br />
Qualifiers
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
• grouping: grouping: ()<br />
()<br />
• array array subscript: subscript: []<br />
[]<br />
Operators<br />
Operators<br />
• function function call call and and constructor: constructor: ()<br />
()<br />
• field field selector selector and and swizzle: swizzle: .<br />
.<br />
• postfix: postfix: ++ --<br />
--<br />
• prefix: prefix: ++ ++ -- -- + + - !
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Operators<br />
Operators<br />
• binary: binary: * * / / + + -<br />
• relational: relational: < < >=<br />
>=<br />
• equality: equality: == == !=<br />
!=<br />
• logical: logical: && && ^^ ^^ ||<br />
||<br />
• selection: selection: ?:<br />
?:<br />
• assignment: assignment: = *= /= += -=
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
• prefix: prefix: ~<br />
~<br />
• binary: binary: %<br />
%<br />
Reserved Reserved Operators<br />
Operators<br />
• bitwise: bitwise:<br />
& & ^ ^ |<br />
|<br />
• assignment: assignment: %= %= = &= &= ^= ^= |=<br />
|=
• float()<br />
float()<br />
• int()<br />
int()<br />
• bool()<br />
bool()<br />
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
• vec2() vec2() vec3() vec3() vec4()<br />
vec4()<br />
Constructors<br />
Constructors<br />
• mat2() mat2() mat3() mat3() mat4()<br />
mat4()
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Scalar Scalar Constructors<br />
Constructors<br />
float f; int i; bool b;<br />
float( b) // b=true?1.0:0.0;<br />
int( int(<br />
b) // b=true?1:0;<br />
bool( bool(<br />
b) // identity<br />
float( i) // int to float<br />
int( int(<br />
i) // identity<br />
bool( bool(<br />
i) // i!=0?true:false;<br />
float( f) // identity<br />
int( int(<br />
f) // float to int<br />
bool( bool(<br />
f) // f!=0.0?true:false;
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Vector Vector Constructors<br />
Constructors<br />
vec2 v2; vec3 v3; vec4 v4;<br />
vec2( 1.0 ,2.0)<br />
vec3( 0.0 ,0.0 ,1.0)<br />
vec4( 1.0 ,0.5 ,0.0 ,1.0)<br />
vec4( 1.0) // all 1.0<br />
vec4( v2 ,v2) // different...<br />
vec4( v3 ,1.0) // ...types<br />
vec2( v4)<br />
float( v4)
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Matrix Matrix Constructors<br />
Constructors<br />
vec4 v4; mat4 m4;<br />
mat4( 1.0, 2.0, 3.0, 4.0,<br />
5.0, 6.0, 7.0, 8.0,<br />
9.0, 10., 11., 12.,<br />
13., 14., 15., 16.) // row major<br />
mat4( v4, v4, v4, v4)<br />
mat4( 1.0) // identity matrix<br />
mat3( m4) // upper 3x3<br />
vec4( m4) // 1st column<br />
float( m4) // upper 1x1
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Structure Structure Constructors<br />
Constructors<br />
struct light {<br />
float intensity;<br />
vec3 position;<br />
}<br />
light headLight = light( 0.2<br />
,vec3( 0.0 ,0.0, 1.0));<br />
// same as<br />
light headLight;<br />
headLight<br />
headLight.intensity headLight.intensity<br />
= 0.2;<br />
headLight.position headLight.position<br />
= vec3( 0.0 ,0.0, 1.0);
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Components<br />
Components<br />
• component component accessor for vectors<br />
– xyzw xyzw rgba rgba stpq stpq [i]<br />
[i]<br />
• component component accessor for matrices<br />
– [i] [i] [i][j]<br />
[i][j]
vec2 v2;<br />
vec3 v3;<br />
vec4 v4;<br />
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Vector Vector components<br />
components<br />
v2.x // is a float<br />
v2.z // wrong: component undefined for type<br />
v4.rgba v4. rgba // is a vec4<br />
v4.stp v4. stp // is a vec3<br />
v4.b // is a float<br />
v4.xy v4. xy // is a vec2<br />
v4.xgp v4. xgp // wrong: mismatched component sets
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Vector Vector components components (r-value)<br />
(r value)<br />
vec2 v2;<br />
vec3 v3;<br />
vec4 v4;<br />
v4.wzyx v4. wzyx // swizzles, is a vec4<br />
v4.bgra v4. bgra // swizzles, is a vec4<br />
v4.xxxx v4. xxxx // smears x, is a vec4<br />
v4.xxx v4. xxx // smears x, is a vec3<br />
v4.yyxx v4. yyxx // duplicates x and y, is a vec4<br />
v2.yyyy v2. yyyy // wrong: too many components for type
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Vector Vector components components (l-value)<br />
(l value)<br />
vec4 v4 = vec4( 1.0, 2.0, 3.0, 4.0);<br />
v4.xw v4. xw = vec2( 5.0, 6.0); // (5.0, 2.0, 3.0, 6.0)<br />
v4.wx v4. wx = vec2( 7.0, 8.0); // (8.0, 2.0, 3.0, 7.0)<br />
v4.xx v4. xx = vec2( 9.0,10.0); // wrong: x used twice<br />
v4.yz v4. yz = 11.0; // wrong: type mismatch<br />
v4.yz v4. yz = vec2( 12.0 ); // (8.0,12.0,12.0, 7.0)
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Vector Vector components components (array)<br />
(array)<br />
vec4 v4 = vec4( 1.0, 2.0, 3.0, 4.0);<br />
float f;<br />
int i;<br />
f = v4[0]; // 1.0<br />
f = v4[3]; // 4.0<br />
f = v4[4]; // undefined<br />
// ...<br />
f = v4[i+1]; // defined for -1
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Matrix Matrix components components (array)<br />
(array)<br />
mat4 m4;<br />
m4[1] = vec4(2.0); // 2nd column = 2.0<br />
m4[0][0] = 1.0; // upper 1x1 = 1.0<br />
m4[4][4] = 3.0; // undefined
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
vec3 v3[2];<br />
mat3 m3[2];<br />
Components Components (arrays)<br />
(arrays)<br />
v3[0] // is a vec3<br />
v3[0][0] // is a float<br />
V3[0].x // is a float<br />
m3[0] // is a mat3<br />
m3[0][0] // is a vec3, 1st column<br />
m3[0][0][0] // is a float, upper 1x1<br />
// :-(
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Vector Vector and and Matrix Matrix Operations<br />
Operations<br />
mat4 m4,n4;<br />
vec4 v4<br />
v4 * m4 // is a vec4<br />
m4 * n4 // is a mat4
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Flow-control Flow control (scalar)<br />
(scalar)<br />
• expression expression ? trueExpression : falseExpression<br />
• if, if-else if if-else else<br />
• for, for, while, while, do-while<br />
do while<br />
• return, break, continue<br />
continue<br />
• discard (fragment only)
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
User-defined User defined functions<br />
functions<br />
• Call Call by by value-return<br />
value return<br />
• in in out inout keywords<br />
• Function Function overloading<br />
overloading<br />
• One One return return value value (can (can be be any any type)<br />
type)<br />
• Scope Scope rules rules same same as as C<br />
C
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in Vertex Vertex Special<br />
Special<br />
vec4 gl_Position;<br />
gl_Position;<br />
// must be written<br />
vec4 gl_ClipPosition<br />
gl ClipPosition; ; // may be written<br />
float gl_PointSize<br />
gl PointSize; ; // may be written
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in Fragment Fragment Special<br />
Special<br />
bool gl_FrontFacing<br />
gl FrontFacing; ; // may be read<br />
float gl_FragColor<br />
gl FragColor; ; // may be written<br />
float gl_FragDepth<br />
gl FragDepth; ; // may be read/written<br />
float gl_FragStencil<br />
gl FragStencil; ; // may be read/written<br />
vec4 gl_FragData<br />
gl FragDatan; ; // may be written<br />
vec4 gl_FragCoord<br />
gl FragCoord; ; // may be read<br />
vec4 gl_FBColor<br />
gl FBColor; ; // may be read<br />
float gl_FBDepth<br />
gl FBDepth; ; // may be read<br />
float gl_FBStencil<br />
gl FBStencil; ; // may be read<br />
vec4 gl_FBData<br />
gl FBDatan; ; // may be read
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in Attributes<br />
Attributes<br />
// Vertex Attributes, p. 19<br />
attribute vec4 gl_Vertex;<br />
gl_Vertex;<br />
attribute vec3 gl_Normal;<br />
gl_Normal;<br />
attribute vec4 gl_Color;<br />
gl_Color;<br />
attribute vec4 gl_SecondaryColor<br />
gl SecondaryColor;<br />
attribute vec4 gl_MultiTexCoord<br />
gl MultiTexCoordn;<br />
attribute float gl_FogCoord<br />
gl FogCoord;
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
User-defined User defined Attributes<br />
Attributes<br />
attribute vec3 myTangent;<br />
myTangent<br />
attribute vec3 myBinormal;<br />
myBinormal<br />
attribute float myTemperature;<br />
myTemperature<br />
attribute float myPressure;<br />
myPressure<br />
attribute float myRainfall;<br />
myRainfall<br />
attribute float myTime; myTime<br />
// etc...
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in Constants<br />
Constants<br />
// Implementation dependent constants<br />
// Minimum Maximums<br />
const int gl_MaxLights<br />
gl MaxLights = 8;<br />
const int gl_MaxClipPlanes<br />
gl MaxClipPlanes = 6;<br />
const int gl_MaxTextureUnits<br />
gl MaxTextureUnits = 2;<br />
const int gl_MaxTextureCoordsARB<br />
gl MaxTextureCoordsARB = 2;<br />
// <strong>GL2</strong> Minimum Maximums<br />
const int gl_MaxVertexAttributes<strong>GL2</strong> gl_MaxVertexAttributes<strong>GL2</strong><br />
= 16;<br />
const int gl_MaxVertexUniformWords<strong>GL2</strong> gl_MaxVertexUniformWords<strong>GL2</strong><br />
= 512;<br />
const int gl_MaxVaryingFloats<strong>GL2</strong> gl_MaxVaryingFloats<strong>GL2</strong><br />
= 32;<br />
const int gl_MaxVertexTextureUnits<strong>GL2</strong> gl_MaxVertexTextureUnits<strong>GL2</strong><br />
= 1;<br />
const int gl_MaxFragmentTextureUnits<strong>GL2</strong> gl_MaxFragmentTextureUnits<strong>GL2</strong><br />
= 2;<br />
const int gl_MaxFragmentUniformWords<strong>GL2</strong> gl_MaxFragmentUniformWords<strong>GL2</strong><br />
= 64;
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in uniforms<br />
uniforms<br />
// Matrix state, p. 31, 32, 37, 39, 40<br />
uniform mat4 gl_ModelViewMatrix<br />
gl ModelViewMatrix;<br />
uniform mat4 gl_ProjectionMatrix<br />
gl ProjectionMatrix;<br />
uniform mat4 gl_ModelViewProjectionMatrix<br />
gl ModelViewProjectionMatrix;<br />
uniform mat3 gl_NormalMatrix<br />
gl NormalMatrix;<br />
uniform mat4 gl_TextureMatrix<br />
gl TextureMatrix[n]; ];<br />
// Normal scaling, p. 33<br />
uniform float gl_NormalScale<br />
gl NormalScale;<br />
// Depth range in window coordinates, p. 33<br />
struct gl_DepthRangeParameters<br />
gl DepthRangeParameters {<br />
float near; // n<br />
float far; // f<br />
float diff; // f – n<br />
};<br />
uniform gl_DepthRangeParameters gl DepthRangeParameters gl_DepthRange<br />
gl DepthRange;
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in uniforms<br />
uniforms<br />
// Clip planes, p. 32<br />
uniform vec4 gl_ClipPlanes<br />
gl ClipPlanes[gl gl_MaxClipPlanes<br />
MaxClipPlanes]<br />
// Point Size, p. 66, 67<br />
struct gl_PointParameters<br />
gl PointParameters {<br />
float size;<br />
float sizeMin; sizeMin<br />
float sizeMax; sizeMax<br />
float fadeThreshold;<br />
fadeThreshold<br />
float distanceConstantAttenuation;<br />
distanceConstantAttenuation<br />
float distanceLinearAttenuation;<br />
distanceLinearAttenuation<br />
float distanceQuadraticAttenuation;<br />
distanceQuadraticAttenuation<br />
};<br />
uniform gl_PointParameters gl PointParameters gl_Point;<br />
gl_Point;
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in uniforms<br />
uniforms<br />
// Material State, p. 50, 55<br />
struct gl_MaterialParameters<br />
gl MaterialParameters {<br />
vec4 emission;<br />
vec4 ambient;<br />
vec4 diffuse;<br />
vec4 specular;<br />
specular<br />
float shininess;<br />
};<br />
uniform gl_MaterialParameters gl MaterialParameters gl_FrontMaterial<br />
gl FrontMaterial;<br />
uniform gl_MaterialParameters gl MaterialParameters gl_BackMaterial<br />
gl BackMaterial;
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in uniforms<br />
uniforms<br />
// Light State, p. 50, 53, 55<br />
struct gl_LightSourceParameters<br />
gl LightSourceParameters {<br />
vec4 ambient; // Acli<br />
vec4 diffuse; // Dcli<br />
vec4 specular;<br />
specular;<br />
// Scli<br />
vec4 position; // Ppli<br />
vec4 halfVector;<br />
halfVector;<br />
// Derived: Hi<br />
vec3 spotDirection;<br />
spotDirection;<br />
// Sdli<br />
float spotExponent;<br />
spotExponent;<br />
// Srli<br />
float spotCutoff;<br />
spotCutoff;<br />
// Crli ([0.0,90.0],180.0)<br />
float spotCosCutoff;<br />
spotCosCutoff;<br />
// Derived: cos(Crli cos Crli) ) ([1.0, 0.0],-1.0)<br />
0.0], 1.0)<br />
float constantAttenuation // K0<br />
float linearAttenuation // K1<br />
float quadraticAttenuation // K2<br />
};<br />
uniform gl_LightSourceParameters gl LightSourceParameters gl_LightSource<br />
gl LightSource[gl gl_MaxLights<br />
MaxLights]; ];
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in uniforms<br />
uniforms<br />
struct gl_LightModelParameters<br />
gl LightModelParameters {<br />
vec4 ambient; // Acs<br />
};<br />
uniform gl_LightModelParameters gl LightModelParameters gl_LightModel<br />
gl LightModel;<br />
struct gl_LightModelProducts<br />
gl LightModelProducts {<br />
vec4 };<br />
sceneColor;<br />
sceneColor;<br />
// Ecm + Acm * Acs<br />
uniform gl_LightModelProducts gl LightModelProducts gl_FrontLightModelProduct<br />
gl FrontLightModelProduct;<br />
uniform gl_LightModelProducts gl LightModelProducts gl_BackLightModelProduct<br />
gl BackLightModelProduct;<br />
struct gl_LightProducts<br />
gl LightProducts {<br />
vec4 ambient; // Acm * Acl<br />
vec4 diffuse; // Dcm * Dcl<br />
vec4 };<br />
specular;<br />
specular;<br />
// Scm * Scl<br />
uniform gl_LightProducts gl LightProducts gl_FrontLightProduct<br />
gl FrontLightProduct[gl gl_MaxLights<br />
MaxLights]; ];<br />
uniform gl_LightProducts gl LightProducts gl_BackLightProduct<br />
gl BackLightProduct[gl gl_MaxLights<br />
MaxLights]; ];
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in uniforms<br />
uniforms<br />
// Texture Environment and Generation, p. 152, p. 40-42 40 42<br />
uniform vec4 gl_TextureEnvColor<br />
gl TextureEnvColor[gl gl_MaxFragmentTextureUnits<strong>GL2</strong>];<br />
_MaxFragmentTextureUnits<strong>GL2</strong>];<br />
uniform vec4 gl_EyePlaneS<br />
gl EyePlaneS[gl gl_MaxTextureCoordsARB<br />
MaxTextureCoordsARB]<br />
,gl gl_EyePlaneT<br />
EyePlaneT[gl gl_MaxTextureCoordsARB<br />
MaxTextureCoordsARB]<br />
,gl gl_EyePlaneR<br />
EyePlaneR[gl gl_MaxTextureCoordsARB<br />
MaxTextureCoordsARB]<br />
,gl gl_EyePlaneQ<br />
EyePlaneQ[gl gl_MaxTextureCoordsARB<br />
MaxTextureCoordsARB]; ];<br />
uniform vec4 gl_ObjectPlaneS<br />
gl ObjectPlaneS[gl gl_MaxTextureCoordsARB<br />
MaxTextureCoordsARB]<br />
,gl gl_ObjectPlaneT<br />
ObjectPlaneT[gl gl_MaxTextureCoordsARB<br />
MaxTextureCoordsARB]<br />
,gl gl_ObjectPlaneR<br />
ObjectPlaneR[gl gl_MaxTextureCoordsARB<br />
MaxTextureCoordsARB]<br />
,gl gl_ObjectPlaneQ<br />
ObjectPlaneQ[gl gl_MaxTextureCoordsARB<br />
MaxTextureCoordsARB]; ];
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in uniforms<br />
uniforms<br />
// Fog, p. 161<br />
struct gl_FogParameters<br />
gl FogParameters {<br />
vec4 color;<br />
float density;<br />
float start;<br />
float end;<br />
float scale; // 1.0/(end-start)<br />
1.0/(end start)<br />
};<br />
uniform gl_FogParameters gl FogParameters gl_Fog; gl_Fog;
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
User-defined User defined uniforms<br />
uniforms<br />
uniform mat4 myGlobalMatrix;<br />
myGlobalMatrix<br />
uniform vec4 myAmbient;<br />
myAmbient<br />
uniform vec4 myDiffuseMatLight;<br />
myDiffuseMatLight<br />
uniform vec4 mySpecularMatLight;<br />
mySpecularMatLight<br />
uniform float myGlow; myGlow<br />
uniform vec4 myWarm; myWarm<br />
uniform vec4 myCool; myCool<br />
uniform float myCurrentTime;<br />
myCurrentTime<br />
// etc...
<strong>GL2</strong> <strong>Shading</strong> <strong>Language</strong><br />
gl_ModelViewMatrix<br />
gl ModelViewMatrix<br />
gl_NormalMatrix<br />
gl NormalMatrix; ;<br />
myWarm; myWarm<br />
myCurrentTime;<br />
myCurrentTime<br />
// etc...<br />
Uniforms<br />
Program Object<br />
Uniform Values<br />
Vertex<br />
Shader<br />
Fragment<br />
Shader
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Built-in Built in varying<br />
varying<br />
varying vec4 gl_FrontColor<br />
gl FrontColor // vertex<br />
varying vec4 gl_BackColor<br />
gl BackColor; // vertex<br />
varying vec4 gl_FrontSecColor<br />
gl FrontSecColor; // vertex<br />
varying vec4 gl_BackSecColor<br />
gl BackSecColor; // vertex<br />
varying vec4 gl_Color;<br />
gl_Color;<br />
// fragment<br />
varying vec4 gl_SecondaryColor<br />
gl SecondaryColor; // fragment<br />
varying vec4 gl_TexCoord<br />
gl TexCoord[]; []; // both<br />
varying float gl_FogFragCoord<br />
gl FogFragCoord; // both
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
User-defined User defined varying<br />
varying<br />
varying vec3 myNormalPrime;<br />
myNormalPrime<br />
varying vec3 myTangentPrime;<br />
myTangentPrime<br />
varying vec3 myBinormalPrime;<br />
myBinormalPrime<br />
varying float myPressurePrime;<br />
myPressurePrime<br />
// etc...
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Angles Angles and and Trigonometry<br />
Trigonometry<br />
genType radians( genType degrees)<br />
genType degrees( genType radians)<br />
genType sin( genType angle) // radians<br />
genType cos( cos(<br />
genType angle)<br />
genType tan( genType angle)<br />
genType asin( asin(<br />
genType angle)<br />
genType acos( acos(<br />
genType angle)<br />
genType atan( atan(<br />
genType y ,genType , genType x)<br />
genType atan( atan(<br />
genType y_over_x)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Exponentials, Exponentials, etc.<br />
etc.<br />
genType pow( pow(<br />
genType x ,genType , genType y)<br />
genType exp2( genType x)<br />
genType log2( genType x)<br />
genType sqrt( sqrt(<br />
genType x)<br />
genType inversesqrt(<br />
inversesqrt(<br />
genType x)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Common<br />
Common<br />
genType abs( genType x)<br />
genType sign( genType x)<br />
genType floor( genType x)<br />
genType ceil( genType x)<br />
genType fract( fract(<br />
genType x)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Common<br />
Common<br />
genType mod( genType x ,float y)<br />
genType mod( genType x ,genType , genType y)<br />
genType min( genType x ,genType , genType y)<br />
genType max( genType x ,genType , genType y)<br />
genType clamp( genType x<br />
,float v0, float v1)<br />
genType clamp( genType x<br />
,genType genType v0, genType v1)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Interpolations<br />
Interpolations<br />
// return x*( 1.0-a) 1.0 a) + y*a<br />
genType mix( genType x ,genType , genType y ,float a)<br />
genType mix( genType x ,genType , genType y ,genType , genType a)<br />
// x
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Interpolations<br />
Interpolations<br />
// genType t;<br />
// t = (x-edge0)/(edge1<br />
(x edge0)/(edge1-edge0); edge0);<br />
// t = clamp( t, 0.0, 1.0);<br />
// return t*t*(3.0-2.0*t);<br />
t*t*(3.0 2.0*t);<br />
genType smoothstep( smoothstep(<br />
float edge0<br />
,float edge1<br />
,genType genType x)<br />
genType smoothstep( smoothstep genType edge0<br />
,genType genType edge1<br />
,genType genType x)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Geometric<br />
Geometric<br />
float length( genType x)<br />
float distance( genType p0 ,genType , genType p1)<br />
float dot( genType x ,genType , genType y)<br />
vec3 cross( vec3 x ,vec3 y)<br />
genType normalize( genType x)<br />
genType faceforward(<br />
faceforward(<br />
genType N<br />
,genType genType I<br />
,genType genType Nref) Nref<br />
genType reflect( genType I<br />
,genType genType N)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Matrix<br />
Matrix<br />
mat matrixCompMult( matrixCompMult(<br />
mat x ,mat y)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Vector Vector Relational<br />
Relational<br />
bGenType lessThan(<br />
lessThan(<br />
genType x ,genType , genType y)<br />
bGenType lessThanEqual(<br />
lessThanEqual(<br />
genType x<br />
,genType genType y)<br />
bGenType greaterThan(<br />
greaterThan(<br />
genType x ,genType , genType y)<br />
bGenType greaterThanEqual(<br />
greaterThanEqual(<br />
genType x<br />
,genType genType y)<br />
bGenType equal( genType x ,genType , genType y)<br />
bGenType notEqual(<br />
notEqual(<br />
genType x , genType y)<br />
bGenType not( genType x)<br />
bool any( bGenType x)<br />
bool all( bGenType x)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Texture<br />
Texture<br />
vec4 texture1D( sampler1D sampler<br />
,float coord ,float bias) bias<br />
vec4 texture2D( sampler2D sampler<br />
,vec2 coord ,float bias) bias<br />
vec4 texture3D( sampler3D sampler<br />
,vec3 coord ,float bias) bias<br />
vec4 textureCube(<br />
textureCube(<br />
samplerCube sampler<br />
,vec3 coord ,float bias)<br />
bias
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Texture<br />
Texture<br />
vec4 texture1DProj( sampler1D sampler<br />
,vec2 coord ,float bias) bias<br />
vec4 texture2DProj( sampler2D sampler<br />
,vec3 coord ,float bias) bias<br />
vec4 texture3DProj( sampler3D sampler<br />
,vec4 coord ,float bias) bias<br />
vec4 textureCubeProj(<br />
textureCubeProj(<br />
samplerCube sampler<br />
,vec4 coord ,float bias)<br />
bias
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Texture<br />
Texture<br />
vec4 texture1DProj( sampler1D sampler<br />
,vec4 coord ,float bias) bias<br />
vec4 texture2DProj( sampler2D sampler<br />
,vec4 coord ,float bias)<br />
bias
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Shadow<br />
Shadow<br />
vec4 shadow1D( samplerShadow1D sampler<br />
,vec3 coord ,float bias) bias<br />
vec4 shadow2D( samplerShadow2D sampler<br />
,vec3 coord ,float bias) bias<br />
vec4 shadow1DProj( samplerShadow1D sampler<br />
,vec4 coord ,float bias) bias<br />
vec4 shadow2DProj( samplerShadow2D sampler<br />
,vec4 coord ,float bias)<br />
bias
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
vec4 ftransform()<br />
ftransform()<br />
Vertex<br />
Vertex<br />
// Example usage:<br />
gl_Position gl_Position<br />
= ftransform();<br />
ftransform();
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Fragment<br />
Fragment<br />
genType dFdx( dFdx genType p)<br />
genType dFdy( dFdy(<br />
genType p)<br />
genType fwidth( fwidth(<br />
genType p)
<strong>GL2</strong> <strong>GL2</strong> Built-in Built in Functions<br />
Functions<br />
Noise<br />
Noise<br />
float noise1( genType coord) coord<br />
vec2 noise2( genType coord) coord<br />
vec3 noise3( genType coord) coord<br />
vec4 noise4( genType coord)<br />
coord
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Today<br />
Today<br />
• How How we we replace replace Fixed Fixed Function<br />
Function<br />
• Review Review <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
• <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong> <strong>Language</strong> examples<br />
examples<br />
• Procedural Procedural Shader Shader demo<br />
demo
<strong>GL2</strong> <strong>GL2</strong> Trivial Trivial Example<br />
Example<br />
Vertex Vertex Shader<br />
Shader<br />
varying vec3 Normal; // output<br />
#define MVP gl_ModelViewProjectionMatrix<br />
gl ModelViewProjectionMatrix<br />
#define MV gl_ModelViewMatrix<br />
gl ModelViewMatrix<br />
#define MV_IT gl_NormalMatrix<br />
gl NormalMatrix<br />
void main( void) // Vertex Shader<br />
{<br />
gl_Position gl_Position<br />
= MVP * gl_Vertex;<br />
gl_Vertex;<br />
//gl // gl_ClipVertex<br />
ClipVertex = MV * gl_Vertex;<br />
gl_Vertex;<br />
Normal = normalize( MV_IT * gl_Normal);<br />
gl_Normal);<br />
}
<strong>GL2</strong> <strong>GL2</strong> Trivial Trivial Example<br />
Example<br />
Fragment Fragment Shader<br />
Shader<br />
varying vec3 Normal; // input<br />
uniform vec3 LightColor; LightColor // Light Color<br />
uniform vec3 LightPos; LightPos // Light Position<br />
void main( void) // Fragment Shader<br />
{<br />
vec3 color = LightColor;<br />
LightColor<br />
color *=<br />
max(0.0,dot(normalize(Normal),LightPos<br />
max(0.0,dot(normalize(Normal), LightPos)); ));<br />
gl_FragColor<br />
gl FragColor = vec4( color,1.0);<br />
}
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Example<br />
Example<br />
varying vec4 v_ObjPos v_ObjPos;<br />
varying vec4 v_EyePos v_EyePos;<br />
Vertex Vertex Shader<br />
Shader<br />
void main( void) // Vertex Shader<br />
{<br />
vec4 ObjPos; ObjPos<br />
vec4 EyePos<br />
}<br />
ObjPos = gl_Vertex;<br />
gl_Vertex;<br />
gl_Position gl_Position<br />
= gl_ModelViewProjectionMatrix<br />
gl ModelViewProjectionMatrix * ObjPos; ObjPos<br />
v_ObjPos v_ObjPos<br />
= ObjPos; ObjPos<br />
EyePos = gl_ModelViewMatrix<br />
gl ModelViewMatrix * ObjPos; ObjPos<br />
v_EyePos v_EyePos<br />
= EyePos; EyePos
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Example<br />
Example<br />
varying vec4 v_EyePos v_EyePos;<br />
varying vec4 v_ObjPos v_ObjPos;<br />
Fragment Fragment Shader<br />
Shader<br />
uniform vec4 myLightDir;<br />
myLightDir;<br />
// NOTA BENE: please normalize<br />
uniform vec4 myEyeDir;<br />
myEyeDir;<br />
// (0.0, 0.0, 1.0, 0.0)<br />
uniform vec4 myHVector;<br />
myHVector<br />
uniform vec4 myEyeCenter;<br />
myEyeCenter
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Example<br />
Example<br />
Fragment Fragment Shader<br />
Shader<br />
uniform vec4 mySpecularColor;<br />
mySpecularColor<br />
uniform vec4 myRed, myRed,<br />
myYellow, myYellow myBlue; myBlue<br />
uniform vec4 myHalfSpace0;<br />
uniform vec4 myHalfSpace1;<br />
uniform vec4 myHalfSpace2;<br />
uniform vec4 myHalfSpace3;<br />
uniform vec4 myHalfSpace4;<br />
uniform vec4 toyBallConst;<br />
toyBallConst;<br />
// (-3.0, ( 3.0, 0.4, 0.0, 0.0)<br />
uniform vec4 one;<br />
uniform float myFWidth;<br />
myFWidth
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Example<br />
Example<br />
Fragment Fragment Shader<br />
Shader<br />
void main( void) // Fragment Shader<br />
{<br />
vec4 NNormal; NNormal<br />
vec4 P; // The point in shader space<br />
vec4 SurfColor;<br />
SurfColor<br />
float Intensity;<br />
vec4 PDistance;<br />
PDistance<br />
float InOrOut; InOrOut<br />
P = v_ObjPos v_ObjPos;<br />
; // Analytically calculate P<br />
P.w = 0.0;<br />
P = normalize( P);<br />
P.w = 1.0;
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Example<br />
Example<br />
Fragment Fragment Shader<br />
Shader<br />
InOrOut = toyBallConst.x;<br />
toyBallConst.x;<br />
PDistance.x PDistance.x<br />
= dot( P, myHalfSpace0); // Fig 1<br />
PDistance.y PDistance.y<br />
= dot( P, myHalfSpace1); // Fig 2<br />
PDistance.z PDistance.z<br />
= dot( P, myHalfSpace2); // Fig 3<br />
PDistance.w PDistance.w<br />
= dot( P, myHalfSpace3); // Fig 4<br />
PDistance = smoothstep(<br />
smoothstep(<br />
-myFWidth myFWidth, myFWidth,<br />
myFWidth,<br />
PDistance);<br />
PDistance);<br />
InOrOut += dot( PDistance, PDistance,<br />
one);<br />
PDistance.x PDistance.x<br />
= dot( P, myHalfSpace4); // Fig 5<br />
PDistance.y PDistance.y<br />
= toyBallConst.y<br />
toyBallConst.y<br />
- abs( P.z);<br />
PDistance = smoothstep(<br />
smoothstep(<br />
-myFWidth myFWidth, myFWidth,<br />
myFWidth,<br />
PDistance);<br />
PDistance);
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Example<br />
Example<br />
Fragment Fragment Shader<br />
Shader<br />
InOrOut += PDistance.x;<br />
PDistance.x;<br />
// Fig 1<br />
InOrOut = clamp( InOrOut, InOrOut,<br />
0.0, 1.0); // Fig 2<br />
SurfColor = mix( myYellow, myYellow myRed, myRed,<br />
InOrOut);<br />
InOrOut);<br />
// Fig 3<br />
SurfColor = mix( SurfColor, SurfColor myBlue, myBlue,<br />
PDistance.y PDistance.y<br />
); // Fig 4
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Example<br />
Example<br />
Fragment Fragment Shader<br />
Shader<br />
//<br />
// Analytically calculate the sphere NNormal in eye coordinates<br />
NNormal = v_EyePos v_EyePos<br />
– myEyeCenter;<br />
myEyeCenter<br />
NNormal = normalize( NNormal);<br />
NNormal);<br />
//<br />
// Per fragment diffuse lighting<br />
Intensity = 0.2; // ambient<br />
Intensity += clamp( dot( myLightDir,<br />
myLightDir,<br />
NNormal), NNormal),<br />
0.0, 1.0) * 0.8;<br />
SurfColor *= Intensity; // Fig 1
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Example<br />
Example<br />
Fragment Fragment Shader<br />
Shader<br />
//<br />
// Per fragment specular lighting<br />
Intensity = clamp( dot( myHVector,<br />
myHVector,<br />
NNormal), NNormal),<br />
0.0, 1.0);<br />
Intensity = pow ( Intensity, mySpecularColor.a);<br />
mySpecularColor.a);<br />
}<br />
SurfColor += mySpecularColor * Intensity; // Fig 1<br />
SurfColor.a SurfColor.a<br />
= 1.0;<br />
gl_FragColor<br />
gl FragColor = SurfColor;<br />
SurfColor
<strong>GL2</strong> <strong>GL2</strong> ToyBall ToyBall Demo<br />
Demo
<strong>GL2</strong> <strong>GL2</strong> <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
Questions?<br />
Questions?<br />
• How How we we replace replace Fixed Fixed Function<br />
Function<br />
• Review Review <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong><br />
<strong>Language</strong><br />
• Trivial Trivial <strong>Shading</strong> <strong>Shading</strong> <strong>Language</strong> <strong>Language</strong> example<br />
example<br />
• Procedural Procedural Shader Shader demo<br />
demo<br />
• bill@ati bill@ati.com<br />
.com