Title: Graphics Hardware
1Graphics Hardware
4ba6, March 1st 2006
2Outline
- Graphics Card buses
- OpenGL extensions
- Graphics Hardware pipeline
- GPU programmability
- The OpenGL shading language
3Graphics adapters
- Buses for graphics adapters
- PCI
- AGP
- PCI Express
4PCI
- Peripheral Component Interconnect
- Replaced ISA as standard peripheral bus in early
90s - Designed as a general bus, not necessarily for
graphics - 33.33 MHz clock
- Peak transfer rate 133MB/s
- Shared between every PCI device
- Network card, sound card, etc
5AGP
- Accelerated Graphics Port
- Imroved bandwidth, dedicated bus
- AGP aperture allows system memory to be mapped
as AGP memory - 66 Mhz clock, variants
- 1x 266MB/s
- 2x 533MB/s
- 4x 1066MB/s
- 8x 2GB/s
6PCI Express
- PCIe, not to be confused with PCI-X
- 1x 250MB/s
- 2x 500MB/s
- 4x 1GB/s
- 16x 4GB/s
- Current standard for graphics cards
- 32x 8GB/s
- Can split bandwidth between read and write
- vs AGPs read OR write
- Scalable Link Interface (SLI) possible
7OpenGL ARB
- Architecture Review Board
- Comprised of representatives from
- 3DLabs, Apple, ATI, Dell, IBM, Intel, nVIDIA,
SGI, Sun - Microsoft resigned in March 2003
- Decides on all core OpenGL features API specs
- Meets once every three months
8OpenGL extensions
- Allow hardware vendors to rapidly expose new
features - Reviewed promoted by the ARB
- Hierarchy, specified by extension prefix
- NV_, ATI_, etc
- EXT_
- ARB_
- core
- Eg. Multitexturing
- SGIS_multitexture
- ARB_multitexture
- Core multitexturing
9Programmable Graphics Hardware
- Major growth in graphics hardware power
- (Moores Law)³
- Progression from Fixed Function Pipeline to
Programmable Pipeline - Allows for programmability at essential stages of
the graphics pipeline - Vertex stage
- Fragment stage
10Basic graphics hardware pipeline
Vertex Stage
Vertex
Fragment Stage
Camera Model Transformations, Lighting etc
Pixel!
Position, Color, Normal Tex Coords,
Texture Addressing, Colour Combining etc
Triangle Setup
Pixel Tests
Textures
11OpenGL programmability
- Texture Shaders
- Register Combiners
- Assembly programs
- ARB_vertex_program
- ARB_fragment_program
- Messy!
- GL Shading Language (GLSL)
12ARB_vertex_program
transform the positions DP4 out.x, in,
mvp0 DP4 out.y, in, mvp1 DP4 out.z, in,
mvp2 DP4 out.w, in, mvp3 transform the
position to eye-space DP4 tmp.x, in, mv0 DP4
tmp.y, in, mv1 DP4 tmp.z, in, mv2 DP4
tmp.w, in, mv3 view direction MOV vDir,
-tmp transfrom the normal to eye-space DP3
oNrm.x, mv0, nrm DP3 oNrm.y, mv1, nrm DP3
oNrm.z, mv2, nrm output the light vector MOV
lVec, light output the texcoords MOV oTex,
tex END
- !!ARBvp1.0
- Phong shading example
- Declarations
- ATTRIB in vertex.position
- ATTRIB nrm vertex.normal
- ATTRIB tex vertex.texcoord0
- PARAM mvp4 state.matrix.mvp
- PARAM mv4 state.matrix.modelview
- PARAM light 0.57735, 0.57735, 0.57735, 0.0
- PARAM const 0.0, 0.0, 0.0, 0.2
- OUTPUT out result.position
- OUTPUT oTex result.texcoord0
- OUTPUT oNrm result.texcoord1
- OUTPUT lVec result.texcoord2
- OUTPUT vDir result.texcoord3
13ARB_fragment_program
normalize the view direction DP3 view.a, vDir,
vDir RSQ view.a, view.a MUL view.rgb, vDir,
view.a compute half angle vector ADD spec.rgb,
view, lVec DP3 spec.a, spec, spec RSQ spec.a,
spec.a MUL spec.rgb, spec, spec.a compute
specular intensisty DP3_SAT spec.a, spec,
tmp LG2 spec.a, spec.a MUL spec.a, spec.a,
const.w EX2 spec.a, spec.a compute diffuse
illum DP3_SAT dif, tmp, lVec ADD_SAT dif.rgb,
dif, const sum MAD_SAT dif.rgb, color, dif,
spec.a MOV dif.a, color.a MOV out, dif END
- !!ARBfp1.0
- Phong shading example
- Declarations
- TEMP tmp
- TEMP dif
- TEMP spec
- TEMP view
- ATTRIB tex fragment.texcoord0
- ATTRIB nrm fragment.texcoord1
- ATTRIB lVec fragment.texcoord2
- ATTRIB vDir fragment.texcoord3
- PARAM color 0.2, 0.3, 1.0, 1.0
- PARAM const 0.2, 0.2, 0.2, 32.0
- OUTPUT out result.color
14GLSL Who? When? Why?!
- ARB GL2 workgroup
- For inclusion in next major OpenGL revision
- OpenGL 2.0
- February 27, 2003
- OpenGL Shading Language draft released
- Advances in hardware
- Just not feasible before now
- Specific operations in specific order fast
hardware
15Fixed Function Vertex Processor
Vertex (object)
Vertex (clip)
TransformMVP,MV,MV-T
Normal
Vertex (eye)
ColorSecondaryColor
FrontBackColor
0,1
Lighting
FrontBackSecondaryColor
0,1
Texgen
TextureMatrixn
TexCoordn
TexCoordn
EdgeFlag
EdgeFlag
16GL2 Vertex Processor
Attribute0
Position
Texture
Uniform
Attribute1
ClipVertex
Attribute2
PointSize
VertexShader
Varying0
Attributen
Varying1
Varying2
Temporaries
Varyingn
EdgeFlag
EdgeFlag
17Fixed Function Fragment Processor
Color
SecondaryColor
Texn
TEn
Sum
Fog
Color
TexCoordn
0,1
z (ze,f )
Depth
Depth
Coord
Coord
FrontFacing
FrontFacing
18GL2 Fragment Processor
Varying0
Uniform
Texture
Varying1
Varying2
FragmentShader
FragColor
Varyingn
FragDepth
FragDepth
Temporaries
FragCoord
FragCoord
FrontFacing
FrontFacing
19In General
- Vertex processes bypassed
- Vertex Transformation
- Normal Transformation, Normalization
- Lighting
- Texture Coordinate Generation and Transformation
- Fragment processes bypassed
- Texture accesses application
- Fog
20Many possibilities
- Arbitrary vertex transformation
21Many possibilities
- Customizable lighting equations
- Not the fixed Blinn-Phong lighting Gouraud
shading of standard OpenGL
22Many possibilities
- Customizable lighting equations
- Not the fixed Blinn-Phong lighting Gouraud
shading of standard OpenGL
23Many possibilities
- Customizable lighting equations
- Not the fixed Blinn-Phong lighting Gouraud
shading of standard OpenGL
24Many possibilities
25Many possibilities
26Many possibilities
27Many possibilities
28Example Vertex Shader
- varying vec4 diffuseColor
- varying vec3 fragNormal
- varying vec3 lightVector
- uniform vec3 eyeSpaceLightVector
- void main()
-
- vec3 eyeSpaceVertex vec3(gl_ModelViewMatrix
gl_Vertex) - lightVector vec3(normalize(eyeSpaceLightVector
- eyeSpaceVertex)) - fragNormal normalize(gl_NormalMatrix
gl_Normal) -
- diffuseColor gl_Color
- gl_Position gl_ModelViewProjectionMatrix
gl_Vertex
29Example Fragment Shader
- varying vec4 diffuseColor
- varying vec3 lightVector
- varying vec3 fragNormal
- void main()
- float perFragmentLightingmax(dot(lightVector,fra
gNormal),0.0) - gl_FragColor diffuseColor lightingFactor
30Types
- void
- float vec2 vec3 vec4
- mat2 mat3 mat4
- int ivec2 ivec3 ivec4
- bool bvec2 bvec3 bvec4
- samplernD, samplerCube, samplerShadownD
31Types
- Structs
- Arrays
- One dimensional
- Constant size (ie float array4)
- Reserved types
- half hvec2 hvec3 hvec4
- fixed fvec2 fvec3 fvec4
- double dvec2 dvec3 dvec4
32Type qualifiers
- attribute
- Changes per-vertex
- eg. position, normal etc.
- uniform
- Does not change between vertices of a batch
- eg light position, texture unit, other constants
- varying
- Passed from VS to FS, interpolated
- eg texture coordinates, vertex color
33Built-in variables
- Attributes uniforms
- For ease of programming
- OpenGL state mapped to variables
- Some special variables are required to be written
to, others are optional
34Special built-ins
- Vertex shader
- vec4 gl_Position // must be written
- vec4 gl_ClipPosition // may be written
- float gl_PointSize // may be written
- Fragment shader
- float gl_FragColor // may be written
- float gl_FragDepth // may be read/written
- vec4 gl_FragCoord // may be read
- bool gl_FrontFacing // may be read
35Attributes
- Built-in
- attribute vec4 gl_Vertex
- attribute vec3 gl_Normal
- attribute vec4 gl_Color
- attribute vec4 gl_SecondaryColor
- attribute vec4 gl_MultiTexCoordn
- attribute float gl_FogCoord
- User-defined
- attribute vec3 myTangent
- attribute vec3 myBinormal
- Etc
36Built-in Uniforms
- uniform mat4 gl_ModelViewMatrix
- uniform mat4 gl_ProjectionMatrix
- uniform mat4 gl_ModelViewProjectionMatrix
- uniform mat3 gl_NormalMatrix
- uniform mat4 gl_TextureMatrixn
- struct gl_MaterialParameters
- vec4 emission
- vec4 ambient
- vec4 diffuse
- vec4 specular
- float shininess
-
- uniform gl_MaterialParameters gl_FrontMaterial
- uniform gl_MaterialParameters gl_BackMaterial
37Built-in Uniforms
- struct gl_LightSourceParameters
- vec4 ambient
- vec4 diffuse
- vec4 specular
- vec4 position
- vec4 halfVector
- vec3 spotDirection
- float spotExponent
- float spotCutoff
- float spotCosCutoff
- float constantAttenuation
- float linearAttenuation
- float quadraticAttenuation
-
- Uniform gl_LightSourceParameters
gl_LightSourcegl_MaxLights
38Built-in Varyings
- varying vec4 gl_FrontColor // vertex
- varying vec4 gl_BackColor // vertex
- varying vec4 gl_FrontSecColor // vertex
- varying vec4 gl_BackSecColor // vertex
- varying vec4 gl_Color // fragment
- varying vec4 gl_SecondaryColor // fragment
- varying vec4 gl_TexCoord // both
- varying float gl_FogFragCoord // both
39Built-in functions
- Angles Trigonometry
- radians, degrees, sin, cos, tan, asin, acos, atan
- Exponentials
- pow, exp2, log2, sqrt, inversesqrt
- Common
- abs, sign, floor, ceil, fract, mod, min, max,
clamp
40Built-in functions
- Interpolations
- mix(x,y,a) x( 1.0-a) ya)
- step(edge,x) x lt edge ? 0.0 1.0
- smoothstep(edge0,edge1,x)
- t (x-edge0)/(edge1-edge0)
- t clamp( t, 0.0, 1.0)
- return tt(3.0-2.0t)
41Built-in functions
- Geometric
- length, distance, cross, dot, normalize,
faceForward, reflect - Matrix
- matrixCompMult
- Vector relational
- lessThan, lessThanEqual, greaterThan,
greaterThanEqual, equal, notEqual, notEqual, any,
all
42Built-in functions
- Texture
- texture1D, texture2D, texture3D, textureCube
- texture1DProj, texture2DProj, texture3DProj,
textureCubeProj - shadow1D, shadow2D, shadow1DProj, shadow2Dproj
- Vertex
- ftransform
43Windows API
- Available now through 4 extensions
- GL_ARB_shader_objects, GL_ARB_shading_language_100
, GL_ARB_vertex_shader, GL_ARB_fragment_shader - Core in OpenGL 2.0
- With very minor modifications
44Basic method
- 2 basic object types
- Shader object
- Program object
- Create Vertex Fragment Shader Objects
- Compile both
- Create program object attach shaders
- Link program
- Use program
45Creating objects
- GLhandleARB glCreateProgramObjectARB()
- GLhandleARB glCreateShaderObjectARB(GL_VERTEX_SHAD
ER_ARB) - GLhandleARB glCreateShaderObjectARB(GL_FRAGMENT_SH
ADER_ARB)
46Compiling
- void glShaderSourceARB(GLhandleARB shader,
GLsizei nstrings, const GLcharARB strings,
const GLint lengths) - //if lengthsNULL, assumed to be null-terminated
- void glCompileShaderARB(GLhandleARB shader)
47Attaching Linking
- void glAttachObjectARB(GLhandleARB program,
GLhandleARB shader) - //twice, once for vertex shader once for
fragment shader - void glLinkProgramARB(GLhandleARB program)
- //program now ready to use
- void glUseProgramObjectARB(GLhandleARB program)
- //switches on shader, bypasses FFP
- //if program0, shaders turned off, returns to
FFP
48In short
- GLhandleARB programObject
- GLhandleARB vertexShaderObject
- GLhandleARB fragmentShaderObject
- unsigned char vertexShaderSource
readShaderFile(vertexShaderFilename) - unsigned char fragmentShaderSource
readShaderFile(fragmentShaderFilename) - programObjectglCreateProgramObjectARB()
- vertexShaderObjectglCreateShaderObjectARB(GL_VERT
EX_SHADER_ARB) - fragmentShaderObjectglCreateShaderObjectARB(GL_FR
AGMENT_SHADER_ARB) - glShaderSourceARB(vertexShaderObject,1,(const
char)vertexShaderSource,NULL) - glShaderSourceARB(fragmentShaderObject,1,(const
char)fragmentShaderSource,NULL) - glCompileShaderARB(vertexShaderObject)
- glCompileShaderARB(fragmentShaderObject)
- glAttachObjectARB(programObject,
vertexShaderObject) - glAttachObjectARB(programObject,
fragmentShaderObject)
49In short
- glUseProgramObjectARB(programObject)
- // enables shaders attached to programObject
- doRendering()
- glUseProgramObjectARB(0)
- // return to fixed function pipeline
50Loading Uniforms
- void glUniform1234fiARB(GLint
location,) - Location obtained with
- GLint glGetUniformLocationARB(GLhandleARB
program, const GLcharARB name) - Shader must be enabled with glUseProgramObjectARB(
) before uniforms can be loaded
51Loading Attributes
- void glVertexAttrib1234sfdARB(GLint
index,) - Index obtained with
- GLint glGetAttribLocationARB(GLhandleARB
program, const GLcharARB name) - Alternate method
- void glBindAttribLocationARB(GLhandleARB program,
GLuint index, const GLcharARB name) - Program must be linked after binding attrib
locations
52Loading Textures
- Bind textures to different units as usual
- glActiveTexture(GL_TEXTURE0)
- glBindTexture(GL_TEXTURE_2D,myFirstTexture)
- glActiveTexture(GL_TEXTURE1)
- glBindTexture(GL_TEXTURE_2D,mySecondTexture)
- Then load corresponding sampler with texture unit
that texture is bound to - glUniform1iARB(glGetUniformLocationARB(
programObject,myFirstSampler),0) - glUniform1iARB(glGetUniformLocationARB(
programObject,mySecondSampler),1)
53Other functions
- Clean-up
- void glDetachObjectARB(GLhandleARB container,
GLhandleARB attached) - void glDeleteObjectARB(GLhandleARB object)
- Info Log
- void glGetInfoLogARB(GLhandleARB object,
GLsizei maxLength, GLsizei length, GLcharARB
infoLog) - Returns compile linking information, errors