Rasterization
Rasterization is the process of converting vector graphics descriptions (geometric primitives like triangles, lines, and points) into a raster image, which is a grid of pixels. This is a fundamental step in the 3D graphics rendering pipeline. The graphics hardware is highly optimized to perform rasterization efficiently.
The Rasterization Process
The rasterization stage takes geometric primitives, typically defined by vertices in screen space coordinates, and determines which pixels on the screen the primitive covers. For each covered pixel, it also interpolates attribute values (like color, texture coordinates, or depth) from the primitive's vertices.
Triangle Rasterization
Triangle rasterization is the most common form. For a given triangle with vertices P1, P2, and P3:
- Edge Walking: The algorithm traverses the edges of the triangle to identify the bounding box of pixels it might cover.
- Point-in-Triangle Test: For each pixel within the bounding box, a test is performed to determine if the pixel's center lies inside the triangle. This is often done using barycentric coordinates or cross products.
- Attribute Interpolation: If a pixel is inside the triangle, its attributes (e.g., color) are calculated by interpolating the vertex attributes based on the pixel's position within the triangle. Barycentric coordinates are commonly used for this interpolation.
The formula for interpolating an attribute A at a point P inside a triangle with vertices P1, P2, P3 and corresponding attributes A1, A2, A3 using barycentric coordinates (u, v, w) is:
A = u * A1 + v * A2 + w * A3
where u + v + w = 1.
Key Aspects and Considerations
- Edge Rules: Determining which pixels lie exactly on an edge requires consistent rules to avoid drawing edges multiple times or missing them entirely. Common rules involve checking the sign of cross products or using specific inequalities.
- Sampling: Rasterization typically samples at the center of pixels. Anti-aliasing techniques often involve sampling at multiple sub-pixel locations to produce smoother edges.
- Depth Testing: After rasterization, the depth value of the fragment (pixel) is compared against the depth buffer. If the new fragment is closer to the viewer, it overwrites the existing fragment and its depth value is written to the depth buffer.
- Performance: Hardware rasterizers are highly parallelized. They can process multiple primitives and pixels concurrently, making it a critical performance bottleneck in real-time graphics if not handled efficiently.
Rasterization in DirectX
In DirectX, the graphics pipeline includes a dedicated rasterizer stage. This stage receives primitives from the vertex shader (after clipping and perspective division) and generates fragments. These fragments are then passed to the pixel shader for per-pixel processing before being written to the render target (framebuffer) after depth, stencil, and alpha testing.
DirectX provides APIs and control over certain aspects of rasterization, such as:
- Culling: Back-face culling can be enabled to discard primitives whose front faces are not visible to the camera, saving rasterization effort.
- Fill Mode: You can control how primitives are rasterized: as solid polygons, wireframes, or points.
- Scissor Rectangles: Define a rectangular region of the screen to which rasterization is confined.
Understanding rasterization is crucial for optimizing rendering performance and achieving the desired visual quality in DirectX applications.