Graphics Pipeline & APIs Pipelines • One person prepares Xmas cards:
fold write pack fold write pack
CPU • Three persons prepare Xmas cars: Vertex Processing glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); fold fold fold fold glPushMatrix (); write write write glTranslatef (-0.15, -0.15, solidZ); Rasterization glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero); pack pack glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid); glCallList (sphereList); glPopMatrix (); Fragment Processing •Ideal: n stages → speedup n • Bottleneck: slowest stage Display • Graphics: Bottleneck determines frames/s 2
1. Graphics APIs
attributed geometry - ∆ The Graphics Pipeline The Graphics Pipeline
Vertices Pixels Database Traversal Hidden-Lines and Surfaces
Geometry Geometry Fragment Frame Application Rasterizer Storage Processor Processor buffer Model Transform Perspective Projection
Pre-Sorting Scan Conversion Texture Storage + Filtering Viewing Transform
3D Clipping CPU GPU image -pixels
3 4 Lighting, Shading, Texturing 1. Graphics APIs The Graphics Pipeline
Application Geometry Storage
• Generate database •Command buffering – Usually only once – Load from disk • Command interpretation – Build acceleration structures (hierarchies,…) • Unpack and perform format conversion • Database traversal • Maintain graphics state • Input event handlers •Modify data structures • Simulation • Primitive generation (e.g. triangles) • other functions..
5 6
The Graphics Pipeline The Graphics Pipeline Geometry Processor Geometry Processor (2)
• Evaluation of polynomials for curved surfaces • Evaluation of polynomials for curved surfaces • Transformation and projection • Transformation and projection • Clipping, culling, and primitive assembly
Modelview Projection Perspective Viewport Vertex Matrix Matrix Divison Transform
Modelview Projection
Modelview
7 8
The Graphics Pipeline The Graphics Pipeline
Geometry Processor (3) Rasterization
• Evaluation of polynomials for curved surfaces • Setup (per-triangle) • Transformation and projection • Sampling (triangle = {fragments}) • Clipping, culling, and primitive assembly • Interpolation (interpolate colors and • Lighting coordinates) • Texture coordinate generation
transform
screen-space lit triangles object-space triangles screen-space lit triangles 9 10 fragments
The Graphics Pipeline The Graphics Pipeline
Rasterization (2) Texture
• Separate rule for each • Texture transformation and projection primitive • Texture address calculation • Non-ambiguous! • Texture filtering • Polygons: – Pixel center contained in polygon – On-edge pixels only one is rasterized
fragments texture fragments
11 12
The Graphics Pipeline The Graphics Pipeline Texture (2) Fragment - Processing
• Texture combiners • Texture combiners and fog • Owner, scissor, depth, alpha, stencil tests • Blending or compositing • Dithering and logical operations
texture fragments
textured fragments
13 14 fragments textured fragments Framebuffer pixels The Graphics Pipeline The Graphics Pipeline
Fragment – Processing Diagram Display
fragment pixel and alpha test ownership scissor test associated (RGBA only) • Frame buffer pixel format test data –RGBA vs. indexed color • Bits: 16-bit, 32-bit, 128-bit floating point depth test stencil test • Double buffer vs. single buffer
depth buffer stencil buffer • Quad-buffer stereo • Overlays (extra bit planes) • Auxiliary buffers: alpha, stencil blending frame dithering logic op (RGBA only) buffer
15 16 frame buffer frame buffer The Graphics Pipeline The Graphics Pipeline
Contemporary Graphics Pipeline Contemporary Graphics Pipeline (2)
CPU CPU • Vertex Processing = per-vertex – Transform & Lighting (T&L) Geometry Storage – Historically: hardwired complex operations (floating point) Vertex Processing – Today: programmable Geometry – flow control, texture lookup – 400 million vertices per second Rasterization Rasterization • Fragment Processing = per-fragment Texture – Blending and texture combination Fragment – Historically: fixed point and limited operations Processing Fragment – Today: programmable – 4 billion fragments (“Gigapixel”) per second Display Display – New: floating point, complex operations 17 18
The Graphics Pipeline The Graphics Pipeline Graphics Application Graphics Libraries (API’s) Programming Interfaces (APIs) • Give access to graphics hardware • Declarative (what, not how) – Describe the scene glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); – Scene Graph API (retained mode)
glPushMatrix (); – e.g. SGI Open Inventor, SGI Performer, Renderman glTranslatef (-0.15, -0.15, solidZ); glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid); glCallList (sphereList); • Imperative (how, not what) glPopMatrix (); – Sequence of drawing commands – Immediate mode API – e.g. OpenGL, DirectX (D3D), Postscript
– More direct control 20
Graphics APIs
OpenGL (Open Graphics Library) OpenGL (2)
• Initially defined by Silicon Graphics Inc. • Scales from PC to image engines – http://www.opengl.org/ – http://www.sgi.com/software/opengl/ • Intuitive, procedural interface •Versions •OpenGL Architectural Review Board (ARB) – 3DLabs, Apple, ATI, Compaq, Evans&Sutherland, hp, IBM, Intel, – 1.4 widespread Microsoft, nVidia, sgi, SUN – 2.0 available since August 2004
• Available on many platforms • Language bindings – Windows NT/v4, Windows XP/2000/98/95 – C, Java, Ada, Fortran, Perl, Python – various UNIX (IRIX, Solaris, etc.) – Linux (Debian, RedHat, SuSE, Caldera)
– Mac OS 8/9/X 21 22
OpenGL OpenGL
A 2D OpenGL Example Integration
• gl.lib: main library windowed application program #include
OpenGL OpenGL Basics Data Flow
• OpenGL is a state machine – State encapsulates control for lighting, shading, Unpack Pixel Image texturing, etc. Image pixels operations rasterization – Current state affects transforms, color, lighting, shading, etc. Display Texture Fragment to • Vertices and pixels are fundamental primitives lists memory operations framebuffer • Display lists to optimize performance Unpack Vertex Geometric – glNewList(
25 26 [ fragment = pixel ] OpenGL OpenGL
Geometric Primitives Rendering Features
glBegin(X); • Materials & colors X: GL_POINTS, • Texture mapping GL_LINES, • Texture animation GL_POLYGON, •MipMaps GL_TRIANGLES, • Advanced shading and lighting GL_QUADS, •Fog GL_TRIANGLE_STRIP, • Antialiasing GL_TRIANGLE_FAN, • Framebuffer Ops glEnd(); 27 28
OpenGL OpenGL
A 3D OpenGL Example OpenGL Extensions glClearColor(0.0, 0.0, 0.0, 0.0); GLfloat gray[] = {0.5, 0.5, 0.5, 1.0}; GLfloat white[] = {1.0, 1.0, 1.0, 1.0}; Lighting • Enables access to hardware-specific features GLfloat ldir[] = {1.0, 1.0, 1.0, 0.0}; glEnable(GL_LIGHTING); • Specified by 3D video card manufacturers & glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, gray); OpenGL vendors glLightfv(GL_LIGHT0, GL_DIFFUSE, white); glLightfv(GL_LIGHT0, GL_POSITION, ldir); •Extensions defined in glext.h glEnable(GL_COLOR_MATERIAL); • Extensions have manufacturer’s postfix, e.g. glEnable(GL_DEPTH_TEST); – ARB = official extensions by OpenGL Architectural Review Board glMatrixMode(GL_PROJECTION); Transforms – EXT = agreed upon by multiple OpenGL vendors glLoadIdentity(); gluPerspective(40, 1.0, 0.1, 10); // fovy, aspect, near, far – SGI = Silicon Graphics glMatrixMode(GL_MODELVIEW); – NV = nVidia Corporation glLoadIdentity(); gluLookAt(0.0,0.0,2.0, 0.0,0.0,0.0, 0.0,1.0,0.0); // eye, center, up •Examples: glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); – glEnable(GL_VERTEX_PROGRAM_NV) glColor3f(0.5, 0.3, 0.3); glMatrixMode(GL_MODELVIEW); – glPushMatrix(); glLoadProgramNV() glRotatef(30, 1.0, 1.0, 1.0); glutSolidTeapot(0.5); Geometry glPopMatrix(); 29 30
OpenGL OpenGL