Tuesday, February 17, 2009

A Typical Rendering Pipeline

Three conceptual stages of the rendering pipeline are application, geometry and rasterization.



The Application Stage




The input at this stage is a scene description which is a set of meshes, materials, textures, lights etc. The lesser the number of polygons the lesser the burden on geometry stage. Occlusion culling, View Frustum Culling, Hidden Surface Removal, and LOD selection are the main techniques that helps the geometry stage from overloading. Culling means to select a portion of the geometry from the current scene for rendering. If an object is outside the viewing volume, the object is culled. Occlusion culling methods eliminate a bunch of objects hidden by groups of other objects. Tessellation, game objects movement, camera movement, AI and physics calculations are some other tasks done at this stage.

Geometry stage and rasterization stage is hardwired in modern GPUs;

The Geometry Stage


The triangles will contain vertices with information such as position, color, normals and texture coordinates. In addition to this a 3D scene may contain other information such as light sources and materials. The Geometry stage takes this input. At first note that in our game there will be a coordinate system. It is called world coordinate system. The 3D models that are to be used inside the game use another coordinate system chosen by the modeler or level designer. It is called modeling coordinate system or local coordinate system. Transformations like scaling, rotation, translation and skewing of the objects are easy to do at the model space. This is the first step at the geometry stage. The next task is to convert modeling coordinates into world coordinates. This is called the world transformation. Next the world space coordinates are to be translated and rotated around the camera's view. This is called the view transformation. The viewing coordinate axes can be calculated easily from three vectors: -eye, center and up. Orthographic or perspective transformation is applied next. For orthographic projections the viewing volume is a parallelepiped and for perspective projections the viewing volume is a frustum of a pyramid. The perspective projection gives the illusion of distant objects to look small and near objects to look big. Now a test is done on each triangle to determine if their vertices are completely outside the view volume. This test is trivial for most of the triangles in the scene but can result in re-tessellation of some of the triangles. Lighting calculation is to be done next. For that first step is back face culling for reducing the load. After that per vertex lighting calculation is done using the current shading model. Perspective division and clipping is performed next. This clipping can also result in re-tessellation of some of the triangles. The result is a 2D image residing on the projection window. Now it is to be mapped to the pc monitor as per the users’ requirements. This is the final transform called viewport transform. This result contains more than (x, y) values and send to the rasterization stage.

The Rasterization Stage


Once a model's vertices have been clipped and transformed into window space, the renderer must determine what pixels in the view-port are covered by each graphics primitive. The process of filling in the horizontal spans of pixels belonging to a primitive is called scan-conversion. The renderer calculates the depth, interpolated vertex colors and interpolated texture coordinates for each pixel. This information combined with the location of the pixel itself is called a fragment. A graphics application specifies how the fragment data is used to determine the final color and depth of each pixel during rasterization. This process is called pixel shading. After this the renderer performs a set of operations per fragment. This is called fragment operations. The main fragment operations are: - Pixel ownership test, Scissor test, Alpha test, Stencil test, Depth test Fog calculation, Antialiasing and Blending. The final output is a sent to the frame buffer for display.
Final Note: - We can replace some parts of the fixed pipeline discussed so far with custom programs. These programs execute on the GPU are called shaders. We can achieve huge amount of flexibility in the graphical effects by writing shaders.
Vertex shader and Geometry shader can replace the T&L portion, Pixel shader (fragment shader) can replace shading portion in the rasterization part of the pipeline. The geometry shader allows the GPU to create and destroy geometric primitives on the fly.
Creative Commons License
A Typical Rendering Pipeline by Manoj MJ is licensed under a Creative Commons Attribution-Share Alike 2.5 India License.
Based on a work at gamedev1001.blogspot.com.