Rasterization
Understanding Rasterization
Rasterization is the fundamental process in computer graphics where vector or geometric descriptions of an image are converted into a raster image, which is a grid of pixels. In the context of DirectX, this is a core stage within the graphics pipeline responsible for rendering 3D scenes onto a 2D screen.
The Rasterization Stage
The rasterization stage follows the geometry processing stages (vertex shader, tessellation, geometry shader) and precedes the pixel shader. Its primary role is to determine which pixels on the screen are covered by geometric primitives (typically triangles) and to interpolate vertex attributes across those covered pixels.
Key Concepts in Rasterization
- Triangle Traversal: The process of iterating through the pixels that lie within the boundaries of a triangle.
- Attribute Interpolation: Calculating the values of attributes (like color, texture coordinates, normals) for each pixel based on the values at its vertices. This is commonly done using barycentric coordinates.
- Clipping: Primitives are clipped against the view frustum and near/far planes to ensure only visible geometry is processed.
- Depth Testing (Z-buffering): After rasterization, depth information is used to determine which fragments (potential pixels) are closer to the camera and should be drawn.
- Culling: Back-face culling discards primitives that are facing away from the camera, improving performance.
How DirectX Handles Rasterization
The DirectX API provides mechanisms to control aspects of rasterization, though much of the core process is handled internally by the graphics hardware. Developers primarily interact with rasterization indirectly through:
- Input Assembler (IA): Defines how vertex data is organized and fed into the pipeline.
- Fixed-Function Rasterizer: The hardware-based unit that performs the actual pixel coverage and attribute interpolation.
- Render States: Various states set via Direct3D objects (like Rasterizer State, Depth-Stencil State) can influence how rasterization behaves.
Example: Rasterization State
The D3D11_RASTERIZER_DESC structure is used to configure the rasterizer. Key properties include:
FillMode: Specifies whether primitives should be rendered as filled polygons, wireframes, or points.D3D11_FILL_SOLID: Fills the primitive.D3D11_FILL_WIREFRAME: Renders the primitive as lines.
CullMode: Determines which faces (front or back) are culled.D3D11_CULL_NONE: No culling.D3D11_CULL_FRONT: Cull front faces.D3D11_CULL_BACK: Cull back faces (default).
FrontCounterClockwise: Specifies the winding order for front faces.DepthClipEnable: Controls whether depth clipping is performed.
D3D11_RASTERIZER_DESC rsDesc = {};
rsDesc.FillMode = D3D11_FILL_SOLID;
rsDesc.CullMode = D3D11_CULL_BACK;
rsDesc.FrontCounterClockwise = FALSE;
rsDesc.DepthClipEnable = TRUE;
ID3D11RasterizerState* pRasterizerState;
HRESULT hr = pd3dDevice->CreateRasterizerState(&rsDesc, &pRasterizerState);
if (SUCCEEDED(hr))
{
pd3dImmediateContext->RSSetState(pRasterizerState);
}