gl2 shading language bill licea-kane ati research, inc. bill@ati.com
Post on 01-Jan-2016
221 Views
Preview:
TRANSCRIPT
GL2 Shading LanguageGL2 Shading Language
Bill Licea-KaneBill Licea-Kane
ATI Research, Inc.ATI Research, Inc.
bill@ati.combill@ati.com
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
““As far as generating pictures from data is As far as generating pictures from data is concerned, we feel the display processor should concerned, we feel the display processor should be a specialized device, capable only of generating be a specialized device, capable only of generating pictures from read-only representations in core.”pictures from read-only representations in core.”
Myer and Sutherland, On the Design of Display Processors.Communications of the ACM, Volume 11 Number 6, June, 1968
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
• June 30, 1992June 30, 1992– OpenGL 1.0 Final Draft publishedOpenGL 1.0 Final Draft published
• June 30, 1992June 30, 1992– OpenGL 1.0 Final Draft publishedOpenGL 1.0 Final Draft published
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
““OpenGL does not provide a programming language. OpenGL does not provide a programming language. Its function may be controlled by turning operations Its function may be controlled by turning operations on or off or specifying parameters to operations, but on or off or specifying parameters to operations, but the rendering algorithms are essentially fixed. One the rendering algorithms are essentially fixed. One reason for this decision is that, for performance reason for this decision is that, for performance reasons, graphics hardware is usually designed to reasons, graphics hardware is usually designed to apply certain operations in a specific order; replacing apply certain operations in a specific order; replacing these operations with arbitrary algorithms is usually these operations with arbitrary algorithms is usually infeasible.infeasible. Programmability would conflict with Programmability would conflict with keeping the API close to the hardware and thus with keeping the API close to the hardware and thus with the goal of maximum performance.”the goal of maximum performance.”
Segal and Akeley, Segal and Akeley, The Design of the OpenGL Graphics The Design of the OpenGL Graphics InterfaceInterface, 1994, 1994
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
• August 23, 1993August 23, 1993– First OpenGL 1.0 implementation First OpenGL 1.0 implementation
shipsships
• August 23, 1993August 23, 1993– First OpenGL 1.0 implementation First OpenGL 1.0 implementation
shipsships
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
GL2 Shading LanguageGL2 Shading LanguageBack to the futureBack to the future
billa@entropy.sps.mot.com (William C. Archibald) writes:billa@entropy.sps.mot.com (William C. Archibald) writes:
|> 1)Quite a few modern rendering techniques and renderers|> 1)Quite a few modern rendering techniques and renderers|> support shader function calls at arbitrary points|> support shader function calls at arbitrary points|> across a surface being rendered. ... What I would|> across a surface being rendered. ... What I would|> _like_ to do is call the shader at visible|> _like_ to do is call the shader at visible|> (front-facing) points. Is there _any_ way to do|> (front-facing) points. Is there _any_ way to do|> this kind of thing in OpenGL?|> this kind of thing in OpenGL?
Not with the current API. One could extend OpenGL in this Not with the current API. One could extend OpenGL in this manner...manner...
Akeley, comp.graphics.opengl posting, 13 Oct 1993Akeley, comp.graphics.opengl posting, 13 Oct 1993
GL2 Shading LanguageGL2 Shading LanguageBack to the presentBack to the present
GL2 Shading LanguageGL2 Shading LanguageBack to the presentBack to the present
• February 27, 2003February 27, 2003– arb-gl2 workgroup publishesarb-gl2 workgroup publishes
Draft OpenGL Shading LanguageDraft OpenGL Shading Language– End construction zoneEnd construction zone
• February 27, 2003February 27, 2003– arb-gl2 workgroup publishesarb-gl2 workgroup publishes
Draft OpenGL Shading LanguageDraft OpenGL Shading Language– End construction zoneEnd construction zone
GL2 Shading LanguageGL2 Shading LanguageTodayToday
GL2 Shading LanguageGL2 Shading LanguageTodayToday
• How we replace Fixed FunctionHow we replace Fixed Function• Review Shading LanguageReview Shading Language• Shading Language examplesShading Language examples• Procedural Shader demoProcedural Shader demo
• How we replace Fixed FunctionHow we replace Fixed Function• Review Shading LanguageReview Shading Language• Shading Language examplesShading Language examples• Procedural Shader demoProcedural Shader demo
OpenGLOpenGLFixed Function VertexFixed Function Vertex
TransformTransform[MVP],[MV],[MV][MVP],[MV],[MV]-T-T
LightinLightingg
[0,1][0,1]
[0,1][0,1]
TexgenTexgen TextureTextureMatrixMatrixnn
ColorColorSecondaryColSecondaryColoror
TexCoordTexCoordnn
EdgeFlagEdgeFlag
NormalNormal
Vertex Vertex (object)(object)
TexCoordTexCoordnn
EdgeFlagEdgeFlag
Vertex (eye)Vertex (eye)
Vertex (clip)Vertex (clip)
Front&BackFront&BackColorColor
Front&BackFront&BackSecondaryColSecondaryColoror
GL2 Vertex ProcessorGL2 Vertex Processor
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
ColorColorSecondaryColSecondaryColoror
TexCoordTexCoordnn
EdgeFlagEdgeFlag
NormalNormal
Vertex Vertex (object)(object)
TexCoordTexCoordnn
EdgeFlagEdgeFlag
Vertex (eye)Vertex (eye)
Vertex (clip)Vertex (clip)
Front&BackFront&BackColorColor
Front&BackFront&BackSecondaryColSecondaryColoror
GL2 Vertex ProcessorGL2 Vertex ProcessorNamesNames
VertexVertex
ClipVertexClipVertex
PositionPosition
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
ColorColorSecondaryColSecondaryColoror
MultiTexCoorMultiTexCoorddnn
EdgeFlagEdgeFlag
NormalNormal
TexCoord[TexCoord[nn]]
EdgeFlagEdgeFlag
Front&BackFront&BackColorColor
Front&BackFront&BackSecondaryColSecondaryColoror
GL2 Vertex ProcessorGL2 Vertex ProcessorAdd Fog CoordinateAdd Fog Coordinate
VertexVertex
ClipVertexClipVertex
PositionPosition
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
ColorColorSecondaryColSecondaryColoror
MultiTexCoorMultiTexCoorddnn
EdgeFlagEdgeFlag
NormalNormal
TexCoord[TexCoord[nn]]
EdgeFlagEdgeFlag
Front&BackFront&BackColorColor
Front&BackFront&BackSecondaryColSecondaryColoror
FogCoordFogCoord FogFragCoordFogFragCoord
GL2 Vertex ProcessorGL2 Vertex ProcessorGeneralize InputGeneralize Input
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
ClipVertexClipVertex
PositionPosition
Attribute2Attribute2
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
EdgeFlagEdgeFlag EdgeFlagEdgeFlag
TexCoord[TexCoord[nn]]
Front&BackFront&BackColorColor
Front&BackFront&BackSecondaryColSecondaryColoror
……
FogFragCoordFogFragCoord
GL2 Vertex ProcessorGL2 Vertex ProcessorGeneralize OutputGeneralize Output
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2
……
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
Attribute2Attribute2
EdgeFlagEdgeFlag
TemporariesTemporaries
VertexVertexShaderShader
UniformUniformClipVertexClipVertex
PositionPosition
EdgeFlagEdgeFlag
……
GL2 Vertex ProcessorGL2 Vertex ProcessorAdditional OutputAdditional Output
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2……
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
Attribute2Attribute2
EdgeFlagEdgeFlag
TemporariesTemporaries
VertexVertexShaderShader
UniformUniformClipVertexClipVertex
PositionPosition
EdgeFlagEdgeFlag
PointSizePointSize
……
GL2 Vertex LimitsGL2 Vertex LimitsActive Attributes (16 Active Attributes (16
vec4)vec4)
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
Attribute2Attribute2
EdgeFlagEdgeFlag
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
EdgeFlagEdgeFlag
……
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2……
ClipVertexClipVertex
PositionPosition
PointSizePointSize
GL2 Vertex LimitsGL2 Vertex LimitsUniform (512 words)Uniform (512 words)
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
Attribute2Attribute2
EdgeFlagEdgeFlag
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
EdgeFlagEdgeFlag
……
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2……
ClipVertexClipVertex
PositionPosition
PointSizePointSize
GL2 Vertex LimitsGL2 Vertex LimitsVarying (32 floats)Varying (32 floats)
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
Attribute2Attribute2
EdgeFlagEdgeFlag
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
EdgeFlagEdgeFlag
……
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2……
ClipVertexClipVertex
PositionPosition
PointSizePointSize
GL2 Vertex LimitsGL2 Vertex LimitsInstructions (Virtual)Instructions (Virtual)
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
Attribute2Attribute2
EdgeFlagEdgeFlag
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
EdgeFlagEdgeFlag
……
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2……
ClipVertexClipVertex
PositionPosition
PointSizePointSize
GL2 Vertex LimitsGL2 Vertex LimitsTemporaries (Virtual)Temporaries (Virtual)
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
Attribute2Attribute2
EdgeFlagEdgeFlag
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
EdgeFlagEdgeFlag
……
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2……
ClipVertexClipVertex
PositionPosition
PointSizePointSize
GL2 Vertex LimitsGL2 Vertex LimitsTexture Units (1)Texture Units (1)
AttributeAttributenn
Attribute1Attribute1
Attribute0Attribute0
Attribute2Attribute2
EdgeFlagEdgeFlag
TemporariesTemporaries
VertexVertexShaderShader
UniformUniform
EdgeFlagEdgeFlag
TextureTexture
……
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2……
ClipVertexClipVertex
PositionPosition
PointSizePointSize
ClipVertexClipVertex
PositionPosition
EdgeFlagEdgeFlag
VaryingVaryingnn
ClipVertexClipVertex
PositionPosition
EdgeFlagEdgeFlag
VaryingVaryingnn
OpenGLOpenGLFixed Function Fixed Function RasterizationRasterization
PrimitivePrimitiveAssemblyAssembly
FlatshadeFlatshade
Culling orCulling orClippingClipping
FaceFaceProcessingProcessing
VaryingVaryingnn
CoordCoord
PointPointLineLine
PolygonPolygonRasterizationRasterization
Pixel Pixel RectangleRectangle
BitmapBitmapRasterizationRasterization
ClipVertexClipVertex
PositionPosition
EdgeFlagEdgeFlag
VaryingVaryingnn
RasterPosRasterPos
VaryingVaryingnn
FrontFacing
ColorColor
Coord
Depth
OpenGLOpenGLFixed Function FragmentFixed Function Fragment
TexTexnn
TETEnn
SuSumm
FogFog[0,1][0,1]
Coord
FrontFacing
ColorColor
SecondaryColSecondaryColoror
TexCoord[TexCoord[nn]]
zz (|zzee|,f )
Depth
GL2 Fragment ProcessorGL2 Fragment Processor
TexCoord[TexCoord[nn]]
FrontFacing
zz (|zzee|,f )
Coord
FrontFacing
ColorColor
Coord
ColorColor
SecondaryColSecondaryColoror
Depth Depth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
GL2 Fragment ProcessorGL2 Fragment ProcessorNamesNames
TexCoord[TexCoord[nn]]
FrontFacing
FogFragCoord
FragCoord
FrontFacing
FragColorFragColor
FragCoord
Color0Color0
Color1Color1
FragDepth FragDepth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
GL2 Fragment ProcessorGL2 Fragment ProcessorGeneralize InputGeneralize Input
FrontFacing
FragCoord
FrontFacing
FragColorFragColor
FragCoord
FragDepth FragDepth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2
……
GL2 Fragment LimitsGL2 Fragment LimitsUniforms (64 words)Uniforms (64 words)
FrontFacing
FragCoord
FrontFacing
FragColorFragColor
FragCoord
FragDepth FragDepth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2
……
GL2 Fragment LimitsGL2 Fragment LimitsVarying (32 floats)Varying (32 floats)
FrontFacing
FragCoord
FrontFacing
FragColorFragColor
FragCoord
FragDepth FragDepth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2
……
GL2 Fragment LimitsGL2 Fragment LimitsInstructions (virtual)Instructions (virtual)
FrontFacing
FragCoord
FrontFacing
FragColorFragColor
FragCoord
FragDepth FragDepth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2
……
GL2 Fragment LimitsGL2 Fragment LimitsTemporaries (virtual)Temporaries (virtual)
FrontFacing
FragCoord
FrontFacing
FragColorFragColor
FragCoord
FragDepth FragDepth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2
……
GL2 Fragment LimitsGL2 Fragment LimitsTexture Units (2)Texture Units (2)
FrontFacing
FragCoord
FrontFacing
FragColorFragColor
FragCoord
FragDepth FragDepth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2
……
GL2 Fragment LimitsGL2 Fragment LimitsFragment Output (fixed)Fragment Output (fixed)
FrontFacing
FragCoord
FrontFacing
FragColorFragColor
FragCoord
FragDepth FragDepth
TemporariesTemporaries
FragmenFragmentt
ShaderShader
UniformUniform TextureTexture
VaryingVaryingnn
Varying1Varying1
Varying0Varying0
Varying2Varying2
……
GL2 Shading LanguageGL2 Shading LanguageTodayToday
GL2 Shading LanguageGL2 Shading LanguageTodayToday
• How we replace Fixed FunctionHow we replace Fixed Function• Review Shading LanguageReview Shading Language• Shading Language examplesShading Language examples• Procedural Shader demoProcedural Shader demo
• How we replace Fixed FunctionHow we replace Fixed Function• Review Shading LanguageReview Shading Language• Shading Language examplesShading Language examples• Procedural Shader demoProcedural Shader demo
GL2 Shading LanguageGL2 Shading LanguagePreprocessor directivesPreprocessor directivesGL2 Shading LanguageGL2 Shading LanguagePreprocessor directivesPreprocessor directives
##
#define#define
#undef#undef
#if#if
#ifdef#ifdef
#ifndef#ifndef
#else#else
#elif#elif
#endif#endif
##
#define#define
#undef#undef
#if#if
#ifdef#ifdef
#ifndef#ifndef
#else#else
#elif#elif
#endif#endif
#error#error
#pragma optimize(on|off)#pragma optimize(on|off)
#pragma debug(on|off)#pragma debug(on|off)
#line line #line line filefile
__LINE____LINE__
__FILE____FILE__
__VERSION____VERSION__
// comment// comment
/* comment *//* comment */
#error#error
#pragma optimize(on|off)#pragma optimize(on|off)
#pragma debug(on|off)#pragma debug(on|off)
#line line #line line filefile
__LINE____LINE__
__FILE____FILE__
__VERSION____VERSION__
// comment// comment
/* comment *//* comment */
GL2 Shading LanguageGL2 Shading LanguageTypesTypes
GL2 Shading LanguageGL2 Shading LanguageTypesTypes
• voidvoid
• float vec2 vec3 vec4float vec2 vec3 vec4
• mat2 mat3 mat4mat2 mat3 mat4
• int ivec2 ivec3 ivec4int ivec2 ivec3 ivec4
• bool bvec2 bvec3 bvec4bool bvec2 bvec3 bvec4
• samplersamplernnD samplerCube D samplerCube samplerShadowsamplerShadownnDD
• voidvoid
• float vec2 vec3 vec4float vec2 vec3 vec4
• mat2 mat3 mat4mat2 mat3 mat4
• int ivec2 ivec3 ivec4int ivec2 ivec3 ivec4
• bool bvec2 bvec3 bvec4bool bvec2 bvec3 bvec4
• samplersamplernnD samplerCube D samplerCube samplerShadowsamplerShadownnDD
GL2 Shading LanguageGL2 Shading LanguageTypesTypes
GL2 Shading LanguageGL2 Shading LanguageTypesTypes
• struct (with some minor restrictions)struct (with some minor restrictions)
– No qualifiersNo qualifiers
– No bit fieldsNo bit fields
– No forward referencesNo forward references
– No in-place definitionsNo in-place definitions
– No anonymous structuresNo anonymous structures
• struct (with some minor restrictions)struct (with some minor restrictions)
– No qualifiersNo qualifiers
– No bit fieldsNo bit fields
– No forward referencesNo forward references
– No in-place definitionsNo in-place definitions
– No anonymous structuresNo anonymous structures
GL2 Shading LanguageGL2 Shading LanguageTypesTypes
GL2 Shading LanguageGL2 Shading LanguageTypesTypes
• arrays ( with some minor restrictions)arrays ( with some minor restrictions)
– one dimensionalone dimensional
– size is integral constant expressionsize is integral constant expression
– can declare unsized array, but…can declare unsized array, but…
– specify size and type of array ONCEspecify size and type of array ONCE
– any basic type and structany basic type and struct
– NO initialization at declarationNO initialization at declaration
• arrays ( with some minor restrictions)arrays ( with some minor restrictions)
– one dimensionalone dimensional
– size is integral constant expressionsize is integral constant expression
– can declare unsized array, but…can declare unsized array, but…
– specify size and type of array ONCEspecify size and type of array ONCE
– any basic type and structany basic type and struct
– NO initialization at declarationNO initialization at declaration
GL2 Shading LanguageGL2 Shading LanguageReserved TypesReserved Types
GL2 Shading LanguageGL2 Shading LanguageReserved TypesReserved Types
• half hvec2 hvec3 hvec4half hvec2 hvec3 hvec4
• fixed fvec2 fvec3 fvec4fixed fvec2 fvec3 fvec4
• double dvec2 dvec3 dvec4double dvec2 dvec3 dvec4
• sampler2DRect sampler3DRectsampler2DRect sampler3DRect
• half hvec2 hvec3 hvec4half hvec2 hvec3 hvec4
• fixed fvec2 fvec3 fvec4fixed fvec2 fvec3 fvec4
• double dvec2 dvec3 dvec4double dvec2 dvec3 dvec4
• sampler2DRect sampler3DRectsampler2DRect sampler3DRect
GL2 Shading LanguageGL2 Shading LanguageScopeScope
GL2 Shading LanguageGL2 Shading LanguageScopeScope
• globalglobal
– outside functionoutside function
– shared globals must be same typeshared globals must be same type
• local (nested)local (nested)
– within function definitionwithin function definition
– within functionwithin function
– within compound statementwithin compound statement
• globalglobal
– outside functionoutside function
– shared globals must be same typeshared globals must be same type
• local (nested)local (nested)
– within function definitionwithin function definition
– within functionwithin function
– within compound statementwithin compound statement
GL2 Shading LanguageGL2 Shading LanguageType QualifiersType Qualifiers
GL2 Shading LanguageGL2 Shading LanguageType QualifiersType Qualifiers
• constconst
• attributeattribute
• uniformuniform
• varyingvarying
• (default)(default)
• constconst
• attributeattribute
• uniformuniform
• varyingvarying
• (default)(default)
GL2 Shading LanguageGL2 Shading LanguageOperatorsOperators
GL2 Shading LanguageGL2 Shading LanguageOperatorsOperators
• grouping: ()grouping: ()
• array subscript: []array subscript: []
• function call and constructor: ()function call and constructor: ()
• field selector and swizzle: .field selector and swizzle: .
• postfix: ++ --postfix: ++ --
• prefix: ++ -- + - !prefix: ++ -- + - !
• grouping: ()grouping: ()
• array subscript: []array subscript: []
• function call and constructor: ()function call and constructor: ()
• field selector and swizzle: .field selector and swizzle: .
• postfix: ++ --postfix: ++ --
• prefix: ++ -- + - !prefix: ++ -- + - !
GL2 Shading LanguageGL2 Shading LanguageOperatorsOperators
GL2 Shading LanguageGL2 Shading LanguageOperatorsOperators
• binary: * / + -binary: * / + -
• relational: < <= > >=relational: < <= > >=
• equality: == != equality: == !=
• logical: && ^^ ||logical: && ^^ ||
• selection: ?:selection: ?:
• assignment: = *= /= += -=assignment: = *= /= += -=
• binary: * / + -binary: * / + -
• relational: < <= > >=relational: < <= > >=
• equality: == != equality: == !=
• logical: && ^^ ||logical: && ^^ ||
• selection: ?:selection: ?:
• assignment: = *= /= += -=assignment: = *= /= += -=
GL2 Shading LanguageGL2 Shading LanguageReserved OperatorsReserved Operators
GL2 Shading LanguageGL2 Shading LanguageReserved OperatorsReserved Operators
• prefix: ~ prefix: ~
• binary: %binary: %
• bitwise: << >> & ^ |bitwise: << >> & ^ |
• assignment: %= <<= >>= &= ^= |=assignment: %= <<= >>= &= ^= |=
• prefix: ~ prefix: ~
• binary: %binary: %
• bitwise: << >> & ^ |bitwise: << >> & ^ |
• assignment: %= <<= >>= &= ^= |=assignment: %= <<= >>= &= ^= |=
GL2 Shading LanguageGL2 Shading LanguageConstructorsConstructors
GL2 Shading LanguageGL2 Shading LanguageConstructorsConstructors
• float()float()
• int()int()
• bool()bool()
• vec2() vec3() vec4()vec2() vec3() vec4()
• mat2() mat3() mat4()mat2() mat3() mat4()
• float()float()
• int()int()
• bool()bool()
• vec2() vec3() vec4()vec2() vec3() vec4()
• mat2() mat3() mat4()mat2() mat3() mat4()
GL2 Shading LanguageGL2 Shading LanguageScalar ConstructorsScalar Constructors
GL2 Shading LanguageGL2 Shading LanguageScalar ConstructorsScalar Constructors
float f; int i; bool b;float f; int i; bool b;
float( b)float( b) // b=true?1.0:0.0;// b=true?1.0:0.0;
int( b)int( b) // b=true?1:0;// b=true?1:0;
bool( b)bool( b) // identity// identity
float( i)float( i) // int to float// int to float
int( i)int( i) // identity// identity
bool( i)bool( i) // i!=0?true:false;// i!=0?true:false;
float( f)float( f) // identity// identity
int( f)int( f) // float to int// float to int
bool( f)bool( f) // f!=0.0?true:false;// f!=0.0?true:false;
GL2 Shading LanguageGL2 Shading LanguageVector ConstructorsVector Constructors
GL2 Shading LanguageGL2 Shading LanguageVector ConstructorsVector Constructors
vec2 v2; vec3 v3; vec4 v4;vec2 v2; vec3 v3; vec4 v4;
vec2( 1.0 ,2.0)vec2( 1.0 ,2.0)
vec3( 0.0 ,0.0 ,1.0)vec3( 0.0 ,0.0 ,1.0)
vec4( 1.0 ,0.5 ,0.0 ,1.0)vec4( 1.0 ,0.5 ,0.0 ,1.0)
vec4( 1.0)vec4( 1.0) // all 1.0// all 1.0
vec4( v2 ,v2)vec4( v2 ,v2) // different...// different...
vec4( v3 ,1.0)vec4( v3 ,1.0) // ...types// ...types
vec2( v4)vec2( v4)
float( v4)float( v4)
GL2 Shading LanguageGL2 Shading LanguageMatrix ConstructorsMatrix Constructors
GL2 Shading LanguageGL2 Shading LanguageMatrix ConstructorsMatrix Constructors
vec4 v4; mat4 m4;vec4 v4; mat4 m4;
mat4( 1.0, 2.0, 3.0, 4.0, mat4( 1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0, 5.0, 6.0, 7.0, 8.0,
9.0, 10., 11., 12., 9.0, 10., 11., 12.,
13., 14., 15., 16.) 13., 14., 15., 16.) // row major// row major
mat4( v4, v4, v4, v4)mat4( v4, v4, v4, v4)
mat4( 1.0)mat4( 1.0) // identity matrix// identity matrix
mat3( m4)mat3( m4) // upper 3x3// upper 3x3
vec4( m4)vec4( m4) // 1st column// 1st column
float( m4) float( m4) // upper 1x1// upper 1x1
GL2 Shading LanguageGL2 Shading LanguageStructure ConstructorsStructure ConstructorsGL2 Shading LanguageGL2 Shading LanguageStructure ConstructorsStructure Constructors
struct light {struct light {
float intensity;float intensity;
vec3 position;vec3 position;
}}
light headLight = light( 0.2light headLight = light( 0.2
,vec3( 0.0 ,0.0, 1.0));,vec3( 0.0 ,0.0, 1.0));
// same as// same as
light headLight;light headLight;
headLight.intensity = 0.2;headLight.intensity = 0.2;
headLight.position = vec3( 0.0 ,0.0, 1.0);headLight.position = vec3( 0.0 ,0.0, 1.0);
GL2 Shading LanguageGL2 Shading LanguageComponentsComponents
GL2 Shading LanguageGL2 Shading LanguageComponentsComponents
• component accessor for vectorscomponent accessor for vectors– xyzw rgba stpq [i]xyzw rgba stpq [i]
• component accessor for matricescomponent accessor for matrices
– [i] [i][j][i] [i][j]
• component accessor for vectorscomponent accessor for vectors– xyzw rgba stpq [i]xyzw rgba stpq [i]
• component accessor for matricescomponent accessor for matrices
– [i] [i][j][i] [i][j]
GL2 Shading LanguageGL2 Shading LanguageVector componentsVector components
GL2 Shading LanguageGL2 Shading LanguageVector componentsVector components
vec2 v2;vec2 v2;
vec3 v3;vec3 v3;
vec4 v4;vec4 v4;
v2.xv2.x // is a float// is a float
v2.zv2.z // // wrong: component undefined for typewrong: component undefined for type
v4.rgbav4.rgba // is a vec4// is a vec4
v4.stpv4.stp // is a vec3// is a vec3
v4.bv4.b // is a float// is a float
v4.xyv4.xy // is a vec2// is a vec2
v4.xgp v4.xgp // // wrong: mismatched component setswrong: mismatched component sets
GL2 Shading LanguageGL2 Shading LanguageVector components (r-Vector components (r-
value)value)
GL2 Shading LanguageGL2 Shading LanguageVector components (r-Vector components (r-
value)value)vec2 v2;vec2 v2;
vec3 v3;vec3 v3;
vec4 v4;vec4 v4;
v4.wzyxv4.wzyx // swizzles, is a vec4// swizzles, is a vec4
v4.bgrav4.bgra // swizzles, is a vec4// swizzles, is a vec4
v4.xxxxv4.xxxx // smears x, is a vec4// smears x, is a vec4
v4.xxxv4.xxx // smears x, is a vec3// smears x, is a vec3
v4.yyxxv4.yyxx // duplicates x and y, is a vec4// duplicates x and y, is a vec4
v2.yyyyv2.yyyy // // wrong: too many components for typewrong: too many components for type
GL2 Shading LanguageGL2 Shading LanguageVector components (l-Vector components (l-
value)value)
GL2 Shading LanguageGL2 Shading LanguageVector components (l-Vector components (l-
value)value)vec4 v4 = vec4( 1.0, 2.0, 3.0, 4.0);vec4 v4 = vec4( 1.0, 2.0, 3.0, 4.0);
v4.xw = vec2( 5.0, 6.0);v4.xw = vec2( 5.0, 6.0); // (5.0, 2.0, 3.0, 6.0)// (5.0, 2.0, 3.0, 6.0)
v4.wx = vec2( 7.0, 8.0);v4.wx = vec2( 7.0, 8.0); // (8.0, 2.0, 3.0, 7.0)// (8.0, 2.0, 3.0, 7.0)
v4.xx = vec2( 9.0,10.0);v4.xx = vec2( 9.0,10.0); // // wrong: x used twicewrong: x used twice
v4.yz = 11.0;v4.yz = 11.0; // // wrong: type mismatchwrong: type mismatch
v4.yz = vec2( 12.0 ); v4.yz = vec2( 12.0 ); // (8.0,12.0,12.0, 7.0)// (8.0,12.0,12.0, 7.0)
GL2 Shading LanguageGL2 Shading LanguageVector components Vector components
(array)(array)
GL2 Shading LanguageGL2 Shading LanguageVector components Vector components
(array)(array)vec4 v4 = vec4( 1.0, 2.0, 3.0, 4.0);vec4 v4 = vec4( 1.0, 2.0, 3.0, 4.0);
float f;float f;
int i;int i;
f = v4[0];f = v4[0]; // 1.0// 1.0
f = v4[3]; f = v4[3]; // 4.0// 4.0
f = v4[4]; f = v4[4]; // // undefinedundefined
// ...// ...
f = v4[i+1]; f = v4[i+1]; // defined for -1<i<3// defined for -1<i<3
GL2 Shading LanguageGL2 Shading LanguageMatrix components Matrix components
(array)(array)
GL2 Shading LanguageGL2 Shading LanguageMatrix components Matrix components
(array)(array)mat4 m4;mat4 m4;
m4[1] = vec4(2.0);m4[1] = vec4(2.0); // 2nd column = 2.0// 2nd column = 2.0
m4[0][0] = 1.0; m4[0][0] = 1.0; // upper 1x1 = 1.0// upper 1x1 = 1.0
m4[4][4] = 3.0; m4[4][4] = 3.0; // // undefinedundefined
GL2 Shading LanguageGL2 Shading LanguageComponents (arrays)Components (arrays)
GL2 Shading LanguageGL2 Shading LanguageComponents (arrays)Components (arrays)
vec3 v3[2];vec3 v3[2];
mat3 m3[2];mat3 m3[2];
v3[0]v3[0] // is a vec3// is a vec3
v3[0][0]v3[0][0] // is a float// is a float
V3[0].xV3[0].x // is a float // is a float
m3[0]m3[0] // is a mat3// is a mat3
m3[0][0]m3[0][0] // is a vec3, 1st column// is a vec3, 1st column
m3[0][0][0]m3[0][0][0] // is a float, upper 1x1// is a float, upper 1x1
// :-(// :-(
GL2 Shading LanguageGL2 Shading LanguageVector and Matrix Vector and Matrix
OperationsOperations
GL2 Shading LanguageGL2 Shading LanguageVector and Matrix Vector and Matrix
OperationsOperationsmat4 m4,n4;mat4 m4,n4;
vec4 v4vec4 v4
v4 * m4v4 * m4 // is a vec4// is a vec4
m4 * n4m4 * n4 // is a mat4// is a mat4
GL2 Shading LanguageGL2 Shading LanguageFlow-control (scalar)Flow-control (scalar)
GL2 Shading LanguageGL2 Shading LanguageFlow-control (scalar)Flow-control (scalar)
• expression ? trueExpression : expression ? trueExpression : falseExpressionfalseExpression
• if, if-elseif, if-else• for, while, do-whilefor, while, do-while• return, break, continuereturn, break, continue• discard (fragment only)discard (fragment only)
• expression ? trueExpression : expression ? trueExpression : falseExpressionfalseExpression
• if, if-elseif, if-else• for, while, do-whilefor, while, do-while• return, break, continuereturn, break, continue• discard (fragment only)discard (fragment only)
GL2 Shading LanguageGL2 Shading LanguageUser-defined functionsUser-defined functionsGL2 Shading LanguageGL2 Shading LanguageUser-defined functionsUser-defined functions
• Call by value-returnCall by value-return• in out inout keywordsin out inout keywords• Function overloadingFunction overloading• One return value (can be any type)One return value (can be any type)• Scope rules same as CScope rules same as C
• Call by value-returnCall by value-return• in out inout keywordsin out inout keywords• Function overloadingFunction overloading• One return value (can be any type)One return value (can be any type)• Scope rules same as CScope rules same as C
GL2 Shading LanguageGL2 Shading LanguageBuilt-in Vertex SpecialBuilt-in Vertex SpecialGL2 Shading LanguageGL2 Shading LanguageBuilt-in Vertex SpecialBuilt-in Vertex Special
vec4 gl_Position; vec4 gl_Position; // must be written// must be written
vec4 gl_ClipPosition; vec4 gl_ClipPosition; // may be written// may be written
float gl_PointSize; float gl_PointSize; // may be written// may be written
GL2 Shading LanguageGL2 Shading LanguageBuilt-in Fragment SpecialBuilt-in Fragment SpecialGL2 Shading LanguageGL2 Shading Language
Built-in Fragment SpecialBuilt-in Fragment Specialbool gl_FrontFacing; bool gl_FrontFacing; // may be read// may be read
float gl_FragColor; float gl_FragColor; // may be written// may be written
float gl_FragDepth; float gl_FragDepth; // may be read/written// may be read/written
float gl_FragStencil; float gl_FragStencil; // may be read/written// may be read/written
vec4 gl_FragDatavec4 gl_FragDatan; n; // may be written// may be written
vec4 gl_FragCoord; vec4 gl_FragCoord; // may be read// may be read
vec4 gl_FBColor; vec4 gl_FBColor; // may be read// may be read
float gl_FBDepth; float gl_FBDepth; // may be read // may be read
float gl_FBStencil; float gl_FBStencil; // may be read// may be read
vec4 gl_FBDatavec4 gl_FBDatan; n; // may be read// may be read
GL2 Shading LanguageGL2 Shading LanguageBuilt-in AttributesBuilt-in Attributes
GL2 Shading LanguageGL2 Shading LanguageBuilt-in AttributesBuilt-in Attributes
// Vertex Attributes, p. 19// Vertex Attributes, p. 19
attribute vec4 gl_Vertex;attribute vec4 gl_Vertex;
attribute vec3 gl_Normal;attribute vec3 gl_Normal;
attribute vec4 gl_Color;attribute vec4 gl_Color;
attribute vec4 gl_SecondaryColor;attribute vec4 gl_SecondaryColor;
attribute vec4 gl_MultiTexCoordattribute vec4 gl_MultiTexCoordnn;;
attribute float gl_FogCoord;attribute float gl_FogCoord;
GL2 Shading LanguageGL2 Shading LanguageUser-defined AttributesUser-defined AttributesGL2 Shading LanguageGL2 Shading LanguageUser-defined AttributesUser-defined Attributes
attribute vec3 myTangent;attribute vec3 myTangent;
attribute vec3 myBinormal;attribute vec3 myBinormal;
attribute float myTemperature;attribute float myTemperature;
attribute float myPressure;attribute float myPressure;
attribute float myRainfall;attribute float myRainfall;
attribute float myTime;attribute float myTime;
// etc...// etc...
GL2 Shading LanguageGL2 Shading LanguageBuilt-in ConstantsBuilt-in Constants
GL2 Shading LanguageGL2 Shading LanguageBuilt-in ConstantsBuilt-in Constants
// Implementation dependent constants// Implementation dependent constants
// Minimum Maximums// Minimum Maximums
const int gl_MaxLights = 8;const int gl_MaxLights = 8;
const int gl_MaxClipPlanes = 6;const int gl_MaxClipPlanes = 6;
const int gl_MaxTextureUnits = 2;const int gl_MaxTextureUnits = 2;
const int gl_MaxTextureCoordsARB = 2;const int gl_MaxTextureCoordsARB = 2;
// GL2 Minimum Maximums// GL2 Minimum Maximums
const int gl_MaxVertexAttributesGL2 = 16;const int gl_MaxVertexAttributesGL2 = 16;
const int gl_MaxVertexUniformWordsGL2 = 512;const int gl_MaxVertexUniformWordsGL2 = 512;
const int gl_MaxVaryingFloatsGL2 = 32;const int gl_MaxVaryingFloatsGL2 = 32;
const int gl_MaxVertexTextureUnitsGL2 = 1;const int gl_MaxVertexTextureUnitsGL2 = 1;
const int gl_MaxFragmentTextureUnitsGL2 = 2;const int gl_MaxFragmentTextureUnitsGL2 = 2;
const int gl_MaxFragmentUniformWordsGL2 = 64;const int gl_MaxFragmentUniformWordsGL2 = 64;
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
// Matrix state, p. 31, 32, 37, 39, 40// Matrix state, p. 31, 32, 37, 39, 40
uniform mat4 gl_ModelViewMatrix;uniform mat4 gl_ModelViewMatrix;
uniform mat4 gl_ProjectionMatrix;uniform mat4 gl_ProjectionMatrix;
uniform mat4 gl_ModelViewProjectionMatrix;uniform mat4 gl_ModelViewProjectionMatrix;
uniform mat3 gl_NormalMatrix;uniform mat3 gl_NormalMatrix;
uniform mat4 gl_TextureMatrix[uniform mat4 gl_TextureMatrix[nn];];
// Normal scaling, p. 33// Normal scaling, p. 33
uniform float gl_NormalScale;uniform float gl_NormalScale;
// Depth range in window coordinates, p. 33// Depth range in window coordinates, p. 33
struct gl_DepthRangeParameters {struct gl_DepthRangeParameters {
float near; float near; // n// n
float far; float far; // f// f
float diff; float diff; // f – n// f – n
};};
uniform gl_DepthRangeParameters gl_DepthRange;uniform gl_DepthRangeParameters gl_DepthRange;
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
// Clip planes, p. 32// Clip planes, p. 32
uniform vec4 gl_ClipPlanes[gl_MaxClipPlanes]uniform vec4 gl_ClipPlanes[gl_MaxClipPlanes]
// Point Size, p. 66, 67// Point Size, p. 66, 67
struct gl_PointParameters {struct gl_PointParameters {
float size;float size;
float sizeMin;float sizeMin;
float sizeMax;float sizeMax;
float fadeThreshold;float fadeThreshold;
float distanceConstantAttenuation;float distanceConstantAttenuation;
float distanceLinearAttenuation;float distanceLinearAttenuation;
float distanceQuadraticAttenuation;float distanceQuadraticAttenuation;
};};
uniform gl_PointParameters gl_Point;uniform gl_PointParameters gl_Point;
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
// Material State, p. 50, 55// Material State, p. 50, 55
struct gl_MaterialParameters {struct gl_MaterialParameters {
vec4 emission;vec4 emission;
vec4 ambient;vec4 ambient;
vec4 diffuse;vec4 diffuse;
vec4 specular;vec4 specular;
float shininess;float shininess;
};};
uniform gl_MaterialParameters gl_FrontMaterial;uniform gl_MaterialParameters gl_FrontMaterial;
uniform gl_MaterialParameters gl_BackMaterial;uniform gl_MaterialParameters gl_BackMaterial;
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
// Light State, p. 50, 53, 55// Light State, p. 50, 53, 55
struct gl_LightSourceParameters {struct gl_LightSourceParameters {
vec4 ambient; vec4 ambient; // Acli // Acli
vec4 diffuse; vec4 diffuse; // Dcli // Dcli
vec4 specular; vec4 specular; // Scli // Scli
vec4 position; vec4 position; // Ppli // Ppli
vec4 halfVector; vec4 halfVector; // Derived: Hi// Derived: Hi
vec3 spotDirection; vec3 spotDirection; // Sdli // Sdli
float spotExponent; float spotExponent; // Srli // Srli
float spotCutoff; float spotCutoff; // Crli ([0.0,90.0],180.0) // Crli ([0.0,90.0],180.0)
float spotCosCutoff; float spotCosCutoff; // Derived: cos(Crli) ([1.0, 0.0],-1.0)// Derived: cos(Crli) ([1.0, 0.0],-1.0)
float constantAttenuation float constantAttenuation // K0// K0
float linearAttenuation float linearAttenuation // K1// K1
float quadraticAttenuation float quadraticAttenuation // K2// K2
};};
uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
struct gl_LightModelParameters {struct gl_LightModelParameters {
vec4 ambient; vec4 ambient; // Acs// Acs
};};
uniform gl_LightModelParameters gl_LightModel;uniform gl_LightModelParameters gl_LightModel;
struct gl_LightModelProducts {struct gl_LightModelProducts {
vec4 sceneColor; vec4 sceneColor; // Ecm + Acm * Acs// Ecm + Acm * Acs
};};
uniform gl_LightModelProducts gl_FrontLightModelProduct;uniform gl_LightModelProducts gl_FrontLightModelProduct;
uniform gl_LightModelProducts gl_BackLightModelProduct;uniform gl_LightModelProducts gl_BackLightModelProduct;
struct gl_LightProducts {struct gl_LightProducts {
vec4 ambient;vec4 ambient; // Acm * Acl // Acm * Acl
vec4 diffuse;vec4 diffuse; // Dcm * Dcl // Dcm * Dcl
vec4 specular; vec4 specular; // Scm * Scl // Scm * Scl
};};
uniform gl_LightProducts gl_FrontLightProduct[gl_MaxLights];uniform gl_LightProducts gl_FrontLightProduct[gl_MaxLights];
uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];uniform gl_LightProducts gl_BackLightProduct[gl_MaxLights];
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
// Texture Environment and Generation, p. 152, p. 40-42// Texture Environment and Generation, p. 152, p. 40-42
uniform vec4 gl_TextureEnvColor[gl_MaxFragmentTextureUnitsGL2];uniform vec4 gl_TextureEnvColor[gl_MaxFragmentTextureUnitsGL2];
uniform vec4 gl_EyePlaneS[gl_MaxTextureCoordsARB]uniform vec4 gl_EyePlaneS[gl_MaxTextureCoordsARB]
,gl_EyePlaneT[gl_MaxTextureCoordsARB],gl_EyePlaneT[gl_MaxTextureCoordsARB]
,gl_EyePlaneR[gl_MaxTextureCoordsARB],gl_EyePlaneR[gl_MaxTextureCoordsARB]
,gl_EyePlaneQ[gl_MaxTextureCoordsARB];,gl_EyePlaneQ[gl_MaxTextureCoordsARB];
uniform vec4 gl_ObjectPlaneS[gl_MaxTextureCoordsARB]uniform vec4 gl_ObjectPlaneS[gl_MaxTextureCoordsARB]
,gl_ObjectPlaneT[gl_MaxTextureCoordsARB],gl_ObjectPlaneT[gl_MaxTextureCoordsARB]
,gl_ObjectPlaneR[gl_MaxTextureCoordsARB],gl_ObjectPlaneR[gl_MaxTextureCoordsARB]
,gl_ObjectPlaneQ[gl_MaxTextureCoordsARB];,gl_ObjectPlaneQ[gl_MaxTextureCoordsARB];
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
GL2 Shading LanguageGL2 Shading LanguageBuilt-in uniformsBuilt-in uniforms
// Fog, p. 161// Fog, p. 161
struct gl_FogParameters {struct gl_FogParameters {
vec4 color;vec4 color;
float density;float density;
float start;float start;
float end;float end;
float scale; float scale; // 1.0/(end-start)// 1.0/(end-start)
};};
uniform gl_FogParameters gl_Fog; uniform gl_FogParameters gl_Fog;
GL2 Shading LanguageGL2 Shading LanguageUser-defined uniformsUser-defined uniformsGL2 Shading LanguageGL2 Shading LanguageUser-defined uniformsUser-defined uniforms
uniform mat4 myGlobalMatrix;uniform mat4 myGlobalMatrix;
uniform vec4 myAmbient;uniform vec4 myAmbient;
uniform vec4 myDiffuseMatLight;uniform vec4 myDiffuseMatLight;
uniform vec4 mySpecularMatLight;uniform vec4 mySpecularMatLight;
uniform float myGlow;uniform float myGlow;
uniform vec4 myWarm;uniform vec4 myWarm;
uniform vec4 myCool;uniform vec4 myCool;
uniform float myCurrentTime;uniform float myCurrentTime;
// etc...// etc...
GL2 Shading LanguageGL2 Shading LanguageUniformsUniforms
Program ObjectProgram Object
gl_ModelViewMatrixgl_ModelViewMatrix
gl_NormalMatrix; gl_NormalMatrix;
myWarm;myWarm;
myCurrentTime;myCurrentTime;
// etc...// etc...
VertexVertexShaderShader
FragmenFragmentt
ShaderShader
Uniform ValuesUniform Values
GL2 Shading LanguageGL2 Shading LanguageBuilt-in varyingBuilt-in varying
GL2 Shading LanguageGL2 Shading LanguageBuilt-in varyingBuilt-in varying
varying vec4 gl_FrontColorvarying vec4 gl_FrontColor // vertex// vertex
varying vec4 gl_BackColor;varying vec4 gl_BackColor; // vertex// vertex
varying vec4 gl_FrontSecColor;varying vec4 gl_FrontSecColor; //// vertexvertex
varying vec4 gl_BackSecColor;varying vec4 gl_BackSecColor; // vertex// vertex
varying vec4 gl_Color;varying vec4 gl_Color; // fragment // fragment
varying vec4 gl_SecondaryColor;varying vec4 gl_SecondaryColor; // fragment // fragment
varying vec4 gl_TexCoord[];varying vec4 gl_TexCoord[]; // both// both
varying float gl_FogFragCoord;varying float gl_FogFragCoord; // both// both
GL2 Shading LanguageGL2 Shading LanguageUser-defined varyingUser-defined varying
GL2 Shading LanguageGL2 Shading LanguageUser-defined varyingUser-defined varying
varying vec3 myNormalPrime;varying vec3 myNormalPrime;
varying vec3 myTangentPrime;varying vec3 myTangentPrime;
varying vec3 myBinormalPrime;varying vec3 myBinormalPrime;
varying float myPressurePrime;varying float myPressurePrime;
// etc...// etc...
GL2 Built-in FunctionsGL2 Built-in FunctionsAngles and TrigonometryAngles and Trigonometry
GL2 Built-in FunctionsGL2 Built-in FunctionsAngles and TrigonometryAngles and TrigonometrygenType radians( genType degrees)genType radians( genType degrees)
genType degrees( genType radians)genType degrees( genType radians)
genType sin( genType angle)genType sin( genType angle) // radians// radians
genType cos( genType angle)genType cos( genType angle)
genType tan( genType angle)genType tan( genType angle)
genType asin( genType angle)genType asin( genType angle)
genType acos( genType angle)genType acos( genType angle)
genType atan( genType y ,genType x)genType atan( genType y ,genType x)
genType atan( genType y_over_x)genType atan( genType y_over_x)
GL2 Built-in Functions GL2 Built-in Functions Exponentials, etc.Exponentials, etc.
GL2 Built-in Functions GL2 Built-in Functions Exponentials, etc.Exponentials, etc.
genType pow( genType x ,genType y)genType pow( genType x ,genType y)
genType exp2( genType x)genType exp2( genType x)
genType log2( genType x)genType log2( genType x)
genType sqrt( genType x)genType sqrt( genType x)
genType inversesqrt( genType x)genType inversesqrt( genType x)
GL2 Built-in Functions GL2 Built-in Functions CommonCommon
GL2 Built-in Functions GL2 Built-in Functions CommonCommon
genType abs( genType x)genType abs( genType x)
genType sign( genType x)genType sign( genType x)
genType floor( genType x)genType floor( genType x)
genType ceil( genType x)genType ceil( genType x)
genType fract( genType x)genType fract( genType x)
GL2 Built-in Functions GL2 Built-in Functions CommonCommon
GL2 Built-in Functions GL2 Built-in Functions CommonCommon
genType mod( genType x ,float y)genType mod( genType x ,float y)
genType mod( genType x ,genType y)genType mod( genType x ,genType y)
genType min( genType x ,genType y)genType min( genType x ,genType y)
genType max( genType x ,genType y)genType max( genType x ,genType y)
genType clamp( genType xgenType clamp( genType x
,float v0, float v1),float v0, float v1)
genType clamp( genType xgenType clamp( genType x
,genType v0, genType v1),genType v0, genType v1)
GL2 Built-in Functions GL2 Built-in Functions InterpolationsInterpolations
GL2 Built-in Functions GL2 Built-in Functions InterpolationsInterpolations
// return x*( 1.0-a) + y*a// return x*( 1.0-a) + y*a
genType mix( genType x ,genType y ,float a)genType mix( genType x ,genType y ,float a)
genType mix( genType x ,genType y ,genType a)genType mix( genType x ,genType y ,genType a)
// x <= edge ? 0.0 : 1.0// x <= edge ? 0.0 : 1.0
genType step( float edge ,genType x)genType step( float edge ,genType x)
genType step( genType edge ,genType x)genType step( genType edge ,genType x)
GL2 Built-in Functions GL2 Built-in Functions InterpolationsInterpolations
GL2 Built-in Functions GL2 Built-in Functions InterpolationsInterpolations
// genType t;// genType t;
// t = (x-edge0)/(edge1-edge0);// t = (x-edge0)/(edge1-edge0);
// t = clamp( t, 0.0, 1.0);// t = clamp( t, 0.0, 1.0);
// return t*t*(3.0-2.0*t);// return t*t*(3.0-2.0*t);
genType smoothstep( float edge0genType smoothstep( float edge0
,float edge1,float edge1
,genType x),genType x)
genType smoothstep( genType edge0genType smoothstep( genType edge0
,genType edge1,genType edge1
,genType x),genType x)
GL2 Built-in Functions GL2 Built-in Functions GeometricGeometric
GL2 Built-in Functions GL2 Built-in Functions GeometricGeometric
float length( genType x)float length( genType x)
float distance( genType p0 ,genType p1)float distance( genType p0 ,genType p1)
float dot( genType x ,genType y)float dot( genType x ,genType y)
vec3 cross( vec3 x ,vec3 y)vec3 cross( vec3 x ,vec3 y)
genType normalize( genType x)genType normalize( genType x)
genType faceforward( genType NgenType faceforward( genType N
,genType I,genType I
,genType Nref),genType Nref)
genType reflect( genType IgenType reflect( genType I
,genType N),genType N)
GL2 Built-in FunctionsGL2 Built-in FunctionsMatrixMatrix
GL2 Built-in FunctionsGL2 Built-in FunctionsMatrixMatrix
mat matrixCompMult( mat x ,mat y)mat matrixCompMult( mat x ,mat y)
GL2 Built-in FunctionsGL2 Built-in FunctionsVector RelationalVector Relational
GL2 Built-in FunctionsGL2 Built-in FunctionsVector RelationalVector Relational
bGenType lessThan( genType x ,genType y)bGenType lessThan( genType x ,genType y)
bGenType lessThanEqual( genType xbGenType lessThanEqual( genType x
,genType y),genType y)
bGenType greaterThan( genType x ,genType y)bGenType greaterThan( genType x ,genType y)
bGenType greaterThanEqual( genType xbGenType greaterThanEqual( genType x
,genType y),genType y)
bGenType equal( genType x ,genType y)bGenType equal( genType x ,genType y)
bGenType notEqual( genType x , genType y)bGenType notEqual( genType x , genType y)
bGenType not( genType x)bGenType not( genType x)
bool any( bGenType x)bool any( bGenType x)
bool all( bGenType x)bool all( bGenType x)
GL2 Built-in FunctionsGL2 Built-in FunctionsTextureTexture
GL2 Built-in FunctionsGL2 Built-in FunctionsTextureTexture
vec4 texture1D( sampler1D samplervec4 texture1D( sampler1D sampler
,float coord ,float coord ,float bias,float bias))
vec4 texture2D( sampler2D samplervec4 texture2D( sampler2D sampler
,vec2 coord ,vec2 coord ,float bias,float bias))
vec4 texture3D( sampler3D samplervec4 texture3D( sampler3D sampler
,vec3 coord,vec3 coord ,float bias ,float bias))
vec4 textureCube( samplerCube samplervec4 textureCube( samplerCube sampler
,vec3 coord ,vec3 coord ,float bias,float bias))
GL2 Built-in Functions GL2 Built-in Functions TextureTexture
GL2 Built-in Functions GL2 Built-in Functions TextureTexture
vec4 texture1DProj( sampler1D samplervec4 texture1DProj( sampler1D sampler
,vec2 coord ,vec2 coord ,float bias,float bias))
vec4 texture2DProj( sampler2D samplervec4 texture2DProj( sampler2D sampler
,vec3 coord ,vec3 coord ,float bias,float bias))
vec4 texture3DProj( sampler3D samplervec4 texture3DProj( sampler3D sampler
,vec4 coord,vec4 coord ,float bias ,float bias))
vec4 textureCubeProj( samplerCube samplervec4 textureCubeProj( samplerCube sampler
,vec4 coord ,vec4 coord ,float bias,float bias))
GL2 Built-in Functions GL2 Built-in Functions TextureTexture
GL2 Built-in Functions GL2 Built-in Functions TextureTexture
vec4 texture1DProj( sampler1D samplervec4 texture1DProj( sampler1D sampler
,vec4 coord ,vec4 coord ,float bias,float bias))
vec4 texture2DProj( sampler2D samplervec4 texture2DProj( sampler2D sampler
,vec4 coord ,vec4 coord ,float bias,float bias))
GL2 Built-in Functions GL2 Built-in Functions ShadowShadow
GL2 Built-in Functions GL2 Built-in Functions ShadowShadow
vec4 shadow1D( samplerShadow1D samplervec4 shadow1D( samplerShadow1D sampler
,vec3 coord ,vec3 coord ,float bias,float bias))
vec4 shadow2D( samplerShadow2D samplervec4 shadow2D( samplerShadow2D sampler
,vec3 coord ,vec3 coord ,float bias,float bias))
vec4 shadow1DProj( samplerShadow1D samplervec4 shadow1DProj( samplerShadow1D sampler
,vec4 coord ,vec4 coord ,float bias,float bias))
vec4 shadow2DProj( samplerShadow2D samplervec4 shadow2DProj( samplerShadow2D sampler
,vec4 coord ,vec4 coord ,float bias,float bias))
GL2 Built-in FunctionsGL2 Built-in FunctionsVertexVertex
GL2 Built-in FunctionsGL2 Built-in FunctionsVertexVertex
vec4 ftransform()vec4 ftransform()
// Example usage:// Example usage:
gl_Position = ftransform();gl_Position = ftransform();
GL2 Built-in Functions GL2 Built-in Functions FragmentFragment
GL2 Built-in Functions GL2 Built-in Functions FragmentFragment
genType dFdx( genType p)genType dFdx( genType p)
genType dFdy( genType p)genType dFdy( genType p)
genType fwidth( genType p)genType fwidth( genType p)
GL2 Built-in FunctionsGL2 Built-in FunctionsNoiseNoise
GL2 Built-in FunctionsGL2 Built-in FunctionsNoiseNoise
float noise1( genType coord)float noise1( genType coord)
vec2 noise2( genType coord)vec2 noise2( genType coord)
vec3 noise3( genType coord)vec3 noise3( genType coord)
vec4 noise4( genType coord)vec4 noise4( genType coord)
GL2 Shading LanguageGL2 Shading LanguageTodayToday
GL2 Shading LanguageGL2 Shading LanguageTodayToday
• How we replace Fixed FunctionHow we replace Fixed Function• Review Shading LanguageReview Shading Language• Shading Language examplesShading Language examples• Procedural Shader demoProcedural Shader demo
• How we replace Fixed FunctionHow we replace Fixed Function• Review Shading LanguageReview Shading Language• Shading Language examplesShading Language examples• Procedural Shader demoProcedural Shader demo
GL2 Trivial ExampleGL2 Trivial ExampleVertex ShaderVertex Shader
GL2 Trivial ExampleGL2 Trivial ExampleVertex ShaderVertex Shader
varying vec3 Normal;varying vec3 Normal; // output// output
#define MVP gl_ModelViewProjectionMatrix#define MVP gl_ModelViewProjectionMatrix
#define MV gl_ModelViewMatrix#define MV gl_ModelViewMatrix
#define MV_IT gl_NormalMatrix#define MV_IT gl_NormalMatrix
void main( void) void main( void) // Vertex Shader// Vertex Shader
{{
gl_Position = MVP * gl_Vertex;gl_Position = MVP * gl_Vertex;
//gl_ClipVertex = MV * gl_Vertex;//gl_ClipVertex = MV * gl_Vertex;
Normal = normalize( MV_IT * gl_Normal);Normal = normalize( MV_IT * gl_Normal);
}}
GL2 Trivial ExampleGL2 Trivial ExampleFragment ShaderFragment Shader
GL2 Trivial ExampleGL2 Trivial ExampleFragment ShaderFragment Shader
varying vec3 Normal;varying vec3 Normal; // input // input
uniform vec3 LightColor;uniform vec3 LightColor; // Light Color// Light Color
uniform vec3 LightPos;uniform vec3 LightPos; // Light Position// Light Position
void main( void) void main( void) // Fragment Shader// Fragment Shader
{{
vec3 color = LightColor;vec3 color = LightColor;
color *=color *=
max(0.0,dot(normalize(Normal),LightPos));max(0.0,dot(normalize(Normal),LightPos));
gl_FragColor = vec4( color,1.0);gl_FragColor = vec4( color,1.0);
}}
GL2 ToyBall ExampleGL2 ToyBall ExampleVertex ShaderVertex Shader
GL2 ToyBall ExampleGL2 ToyBall ExampleVertex ShaderVertex Shader
varying vec4 v_ObjPos;varying vec4 v_ObjPos;
varying vec4 v_EyePos;varying vec4 v_EyePos;
void main( void) void main( void) // Vertex Shader // Vertex Shader
{ {
vec4 ObjPos;vec4 ObjPos;
vec4 EyePosvec4 EyePos
ObjPos = gl_Vertex;ObjPos = gl_Vertex;
gl_Position = gl_ModelViewProjectionMatrix * ObjPos;gl_Position = gl_ModelViewProjectionMatrix * ObjPos;
v_ObjPos = ObjPos;v_ObjPos = ObjPos;
EyePos = gl_ModelViewMatrix * ObjPos;EyePos = gl_ModelViewMatrix * ObjPos;
v_EyePos = EyePos;v_EyePos = EyePos;
}}
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
varying vec4 v_EyePos;varying vec4 v_EyePos;
varying vec4 v_ObjPos;varying vec4 v_ObjPos;
uniform vec4 myLightDir; uniform vec4 myLightDir; // NOTA BENE: please normalize// NOTA BENE: please normalize
uniform vec4 myEyeDir; uniform vec4 myEyeDir; // (0.0, 0.0, 1.0, 0.0)// (0.0, 0.0, 1.0, 0.0)
uniform vec4 myHVector;uniform vec4 myHVector;
uniform vec4 myEyeCenter;uniform vec4 myEyeCenter;
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
uniform vec4 mySpecularColor;uniform vec4 mySpecularColor;
uniform vec4 myRed, myYellow, myBlue;uniform vec4 myRed, myYellow, myBlue;
uniform vec4 myHalfSpace0;uniform vec4 myHalfSpace0;
uniform vec4 myHalfSpace1;uniform vec4 myHalfSpace1;
uniform vec4 myHalfSpace2;uniform vec4 myHalfSpace2;
uniform vec4 myHalfSpace3;uniform vec4 myHalfSpace3;
uniform vec4 myHalfSpace4;uniform vec4 myHalfSpace4;
uniform vec4 toyBallConst; uniform vec4 toyBallConst; // (-3.0, 0.4, 0.0, 0.0)// (-3.0, 0.4, 0.0, 0.0)
uniform vec4 one;uniform vec4 one;
uniform float myFWidth;uniform float myFWidth;
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
void main( void) void main( void) // Fragment Shader // Fragment Shader
{{
vec4 NNormal;vec4 NNormal;
vec4 P; vec4 P; // The point in shader space// The point in shader space
vec4 SurfColor;vec4 SurfColor;
float Intensity;float Intensity;
vec4 PDistance;vec4 PDistance;
float InOrOut;float InOrOut;
P = v_ObjPos; P = v_ObjPos; // Analytically calculate P// Analytically calculate P
P.w = 0.0;P.w = 0.0;
P = normalize( P);P = normalize( P);
P.w = 1.0;P.w = 1.0;
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
InOrOut = toyBallConst.x;InOrOut = toyBallConst.x;
PDistance.x = dot( P, myHalfSpace0); PDistance.x = dot( P, myHalfSpace0); // Fig 1// Fig 1
PDistance.y = dot( P, myHalfSpace1); PDistance.y = dot( P, myHalfSpace1); // Fig 2// Fig 2
PDistance.z = dot( P, myHalfSpace2); PDistance.z = dot( P, myHalfSpace2); // Fig 3// Fig 3
PDistance.w = dot( P, myHalfSpace3); PDistance.w = dot( P, myHalfSpace3); // Fig 4// Fig 4
PDistance = smoothstep( -myFWidth, myFWidth, PDistance);PDistance = smoothstep( -myFWidth, myFWidth, PDistance);
InOrOut += dot( PDistance, one);InOrOut += dot( PDistance, one);
PDistance.x = dot( P, myHalfSpace4); PDistance.x = dot( P, myHalfSpace4); // Fig 5// Fig 5
PDistance.y = toyBallConst.y - abs( P.z);PDistance.y = toyBallConst.y - abs( P.z);
PDistance = smoothstep( -myFWidth, myFWidth, PDistance);PDistance = smoothstep( -myFWidth, myFWidth, PDistance);
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
InOrOut += PDistance.x; InOrOut += PDistance.x; // Fig 1// Fig 1
InOrOut = clamp( InOrOut, 0.0, 1.0); InOrOut = clamp( InOrOut, 0.0, 1.0); // Fig 2// Fig 2
SurfColor = mix( myYellow, myRed, InOrOut); SurfColor = mix( myYellow, myRed, InOrOut); // Fig 3// Fig 3
SurfColor = mix( SurfColor, myBlue, PDistance.y ); SurfColor = mix( SurfColor, myBlue, PDistance.y ); // Fig 4// Fig 4
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
////
// Analytically calculate the sphere NNormal in eye coordinates// Analytically calculate the sphere NNormal in eye coordinates
NNormal = v_EyePos – myEyeCenter;NNormal = v_EyePos – myEyeCenter;
NNormal = normalize( NNormal);NNormal = normalize( NNormal);
////
// Per fragment diffuse lighting// Per fragment diffuse lighting
Intensity = 0.2; // ambientIntensity = 0.2; // ambient
Intensity += clamp( dot( myLightDir, NNormal), 0.0, 1.0) * 0.8;Intensity += clamp( dot( myLightDir, NNormal), 0.0, 1.0) * 0.8;
SurfColor *= Intensity; SurfColor *= Intensity; // Fig 1// Fig 1
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
GL2 ToyBall ExampleGL2 ToyBall ExampleFragment ShaderFragment Shader
////
// Per fragment specular lighting// Per fragment specular lighting
Intensity = clamp( dot( myHVector, NNormal), 0.0, 1.0);Intensity = clamp( dot( myHVector, NNormal), 0.0, 1.0);
Intensity = pow ( Intensity, mySpecularColor.a);Intensity = pow ( Intensity, mySpecularColor.a);
SurfColor += mySpecularColor * Intensity; SurfColor += mySpecularColor * Intensity; // Fig 1// Fig 1
SurfColor.a = 1.0;SurfColor.a = 1.0;
gl_FragColor = SurfColor;gl_FragColor = SurfColor;
}}
GL2 ToyBall DemoGL2 ToyBall DemoGL2 ToyBall DemoGL2 ToyBall Demo
GL2 Shading LanguageGL2 Shading LanguageQuestions?Questions?
GL2 Shading LanguageGL2 Shading LanguageQuestions?Questions?
• How we replace Fixed FunctionHow we replace Fixed Function• Review Shading LanguageReview Shading Language• Trivial Shading Language exampleTrivial Shading Language example• Procedural Shader demoProcedural Shader demo
• bill@ati.combill@ati.com
• How we replace Fixed FunctionHow we replace Fixed Function• Review Shading LanguageReview Shading Language• Trivial Shading Language exampleTrivial Shading Language example• Procedural Shader demoProcedural Shader demo
• bill@ati.combill@ati.com
top related