i
i
i
i
i
i
i
i
2.4. The Rasterizer Stage 21
coordinate system where 0.0 is the center of the pixel, meaning that a range
of pixels [0, 9] cover a span from [0.5, 9.5). Heckbert [520] presents a more
logically consistent scheme. Given a horizontal array of pixels and using
Cartesian coordinates, the left edge of the leftmost pixel is 0.0 in floating
point coordinates. OpenGL has always used this scheme, and DirectX 10
and its successors use it. The center of this pixel is at 0.5. So a range of
pixels [0, 9] cover a span from [0.0, 10.0). The conversions are simply
d = floor(c), (2.1)
c = d +0.5, (2.2)
where d is the discrete (integer) index of the pixel and c is the continuous
(floating point) value within the pixel.
While all APIs have pixel location values that increase going from left
to right, the location of zero for the top and bottom edges is inconsistent
in some cases between OpenGL and DirectX.
6
OpenGL favors the Carte-
sian system throughout, treating the lower left corner as the lowest-valued
element, while DirectX sometimes defines the upper left corner as this el-
ement, depending on the context. There is a logic to each, and no right
answer exists where they differ. As an example, (0, 0) is located at the
lower left corner of an image in OpenGL, while it is upper left for DirectX.
The reasoning for DirectX is that a number of phenomena go from top to
bottom on the screen: Microsoft Windows uses this coordinate system, we
read in this direction, and many image file formats store their buffers in
this way. The key point is that the difference exists and is important to
take into account when moving from one API to the other.
2.4 The Rasterizer Stage
Given the transformed and projected vertices with their associated shad-
ing data (all from the geometry stage), the goal of the rasterizer stage is
to compute and set colors for the pixels
7
covered by the object. This pro-
cess is called rasterization or scan conversion, which is thus the conversion
from two-dimensional vertices in screen space—each with a z-value (depth-
value), and various shading information associated with each vertex—into
pixels on the screen.
6
“Direct3D is the three-dimensional graphics API component of DirectX. DirectX
includes other API elements, such an input and audio control. Rather than differen-
tiate between writing “DirectX” when specifying a particular release and “Direct3D”
when discussing this particular API, we follow common usage by writing “DirectX”
throughout.
7
Short for picture elements.
i
i
i
i
i
i
i
i
22 2. The Graphics Rendering Pipeline
Merging
Triangle
Setup
Triangle
Traversal
Pixel
Shading
Figure 2.8. The rasterizer stage subdivided into a pipeline of functional stages.
Similar to the geometry stage, this stage is divided into several func-
tional stages: triangle setup, triangle traversal, pixel shading, and merging
(Figure 2.8).
2.4.1 Triangle Setup
In this stage the differentials and other data for the triangle’s surface are
computed. This data is used for scan conversion, as well as for interpolation
of the various shading data produced by the geometry stage. This process
is performed by fixed-operation hardware dedicated to this task.
2.4.2 Triangle Traversal
Here is where each pixel that has its center (or a sample) covered by the
triangle is checked and a fragment generated for the part of the pixel that
overlaps the triangle. Finding which samples or pixels are inside a triangle
is often called triangle traversal or scan conversion. Each triangle frag-
ment’s properties are generated using data interpolated among the three
triangle vertices (see Chapter 5). These properties include the fragment’s
depth, as well as any shading data from the geometry stage. Akeley and
Jermoluk [7] and Rogers [1077] offer more information on triangle traversal.
2.4.3 Pixel Shading
Any per-pixel shading computations are performed here, using the inter-
polated shading data as input. The end result is one or more colors to
be passed on to the next stage. Unlike the triangle setup and traversal
stages, which are usually performed by dedicated, hardwired silicon, the
pixel shading stage is executed by programmable GPU cores. A large va-
riety of techniques can be employed here, one of the most important of
which is texturing. Texturing is treated in more detail in Chapter 6. Sim-
ply put, texturing an object means “gluing” an image onto that object.
This process is depicted in Figure 2.9. The image may be one-, two-, or
three-dimensional, with two-dimensional images being the most common.
i
i
i
i
i
i
i
i
2.4. The Rasterizer Stage 23
Figure 2.9. A dragon model without textures is shown in the upper left. The pieces in
the image texture are “glued” onto the dragon, and the result is shown in the lower left.
2.4.4 Merging
The information for each pixel is stored in the color buer,whichisa
rectangular array of colors (a red, a green, and a blue component for each
color). It is the responsibility of the merging stage to combine the fragment
color produced by the shading stage with the color currently stored in the
buffer. Unlike the shading stage, the GPU subunit that typically performs
this stage is not fully programmable. However, it is highly configurable,
enabling various effects.
This stage is also responsible for resolving visibility. This means that
when the whole scene has been rendered, the color buffer should contain
the colors of the primitives in the scene that are visible from the point of
view of the camera. For most graphics hardware, this is done with the
Z-buffer (also called depth buffer) algorithm [162].
8
A Z-buffer is the same
size and shape as the color buffer, and for each pixel it stores the z-value
from the camera to the currently closest primitive. This means that when a
primitive is being rendered to a certain pixel, the z-value on that primitive
at that pixel is being computed and compared to the contents of the Z-
buffer at the same pixel. If the new z-value is smaller than the z-value
in the Z-buffer, then the primitive that is being rendered is closer to the
camera than the primitive that was previously closest to the camera at
that pixel. Therefore, the z-value and the color of that pixel are updated
8
When a Z-buffer is not available, a BSP tree can be used to help render a scene in
back-to-front order. See Section 14.1.2 for information about BSP trees.
i
i
i
i
i
i
i
i
24 2. The Graphics Rendering Pipeline
with the z-value and color from the primitive that is being drawn. If the
computed z-value is greater than the z-value in the Z-buffer, then the color
buffer and the Z-buffer are left untouched. The Z-buffer algorithm is very
simple, has O(n) convergence (where n is the number of primitives being
rendered), and works for any drawing primitive for which a z-value can be
computed for each (relevant) pixel. Also note that this algorithm allows
most primitives to be rendered in any order, which is another reason for its
popularity. However, partially transparent primitives cannot be rendered
in just any order. They must be rendered after all opaque primitives, and
in back-to-front order (Section 5.7). This is one of the major weaknesses
of the Z-buffer.
We have mentioned that the color buffer is used to store colors and
that the Z-buffer stores z-values for each pixel. However, there are other
channels and buffers that can be used to filter and capture fragment infor-
mation. The alpha channel is associated with the color buffer and stores
a related opacity value for each pixel (Section 5.7). An optional alpha test
can be performed on an incoming fragment before the depth test is per-
formed.
9
The alpha value of the fragment is compared by some specified
test (equals, greater than, etc.) to a reference value. If the fragment fails to
pass the test, it is removed from further processing. This test is typically
used to ensure that fully transparent fragments do not affect the Z-buffer
(see Section 6.6).
The stencil buffer is an offscreen buffer used to record the locations of
the rendered primitive. It typically contains eight bits per pixel. Primitives
can be rendered into the stencil buffer using various functions, and the
buffer’s contents can then be used to control rendering into the color buffer
and Z-buffer. As an example, assume that a filled circle has been drawn
into the stencil buffer. This can be combined with an operator that allows
rendering of subsequent primitives into the color buffer only where the
circle is present. The stencil buffer is a powerful tool for generating special
effects. All of these functions at the end of the pipeline are called raster
operations (ROP) or blend operations.
The frame buffer generally consists of all the buffers on a system, but
it is sometimes used to mean just the color buffer and Z-buffer as a set.
In 1990, Haeberli and Akeley [474] presented another complement to the
frame buffer, called the accumulation buffer. In this buffer, images can be
accumulated using a set of operators. For example, a set of images showing
an object in motion can be accumulated and averaged in order to generate
motion blur. Other effects that can be generated include depth of field,
antialiasing, soft shadows, etc.
9
In DirectX 10, the alpha test is no longer part of this stage, but rather a function
of the pixel shader.
i
i
i
i
i
i
i
i
2.5. Through the Pipeline 25
When the primitives have reached and passed the rasterizer stage, those
that are visible from the point of view of the camera are displayed on screen.
The screen displays the contents of the color buffer. To avoid allowing the
human viewer to see the primitives as they are being rasterized and sent
to the screen, double buffering is used. This means that the rendering of
a scene takes place off screen, in a back buffer. Once the scene has been
rendered in the back buffer, the contents of the back buffer are swapped
with the contents of the front buffer that was previously displayed on the
screen. The swapping occurs during vertical retrace, a time when it is safe
to do so.
For more information on different buffers and buffering methods, see
Sections 5.6.2 and 18.1.
2.5 Through the Pipeline
Points, lines, and triangles are the rendering primitives from which a model
or an object is built. Imagine that the application is an interactive com-
puter aided design (CAD) application, and that the user is examining a
design for a cell phone. Here we will follow this model through the entire
graphics rendering pipeline, consisting of the three major stages: applica-
tion, geometry, and the rasterizer. The scene is rendered with perspective
into a window on the screen. In this simple example, the cell phone model
includes both lines (to show the edges of parts) and triangles (to show the
surfaces). Some of the triangles are textured by a two-dimensional im-
age, to represent the keyboard and screen. For this example, shading is
computed completely in the geometry stage, except for application of the
texture, which occurs in the rasterization stage.
Application
CAD applications allow the user to select and move parts of the model. For
example, the user might select the top part of the phone and then move
the mouse to flip the phone open. The application stage must translate the
mouse move to a corresponding rotation matrix, then see to it that this
matrix is properly applied to the lid when it is rendered. Another example:
An animation is played that moves the camera along a predefined path to
show the cell phone from different views. The camera parameters, such
as position and view direction, must then be updated by the application,
dependent upon time. For each frame to be rendered, the application stage
feeds the camera position, lighting, and primitives of the model to the next
major stage in the pipeline—the geometry stage.
Geometry
The view transform was computed in the application stage, along with a
model matrix for each object that specifies its location and orientation. For
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset