MSDN Community

Windows API / Graphics / Rasterizer State

Rasterizer State

The rasterizer state is a collection of settings that control how the GPU processes geometry after it has been transformed and clipped. It defines how primitives (triangles, lines, points) are converted into pixels on the screen.

Key Concepts

The rasterizer state object, often represented by a structure like D3D11_RASTERIZER_DESC in Direct3D 11, governs several aspects of the rendering pipeline:

  • Fill Mode: Determines whether primitives are rendered as solid polygons, wireframes, or points. Common values include D3D11_FILL_SOLID, D3D11_FILL_WIREFRAME, and D3D11_FILL_POINT.
  • Cull Mode: Specifies which faces of a primitive to discard based on their winding order. This helps in optimizing rendering by not drawing back-facing or front-facing polygons that are not visible. Options are D3D11_CULL_NONE, D3D11_CULL_FRONT, and D3D11_CULL_BACK.
  • Front Counter-Clockwise: A boolean flag that, when true, reverses the interpretation of front-facing primitives.
  • Depth Bias: Used to prevent or reduce "z-fighting" or depth fighting issues, where two polygons are very close in depth and can cause flickering. It offsets the depth value of fragments.
  • Depth Clip Enable: Controls whether clipping occurs based on depth after rasterization.
  • Scissor Enable: Enables scissor testing, where only pixels within a defined rectangular region are rendered.
  • Multisample Enable: Controls coverage sampling for anti-aliasing techniques like Multi-Sample Anti-Aliasing (MSAA).
  • Antialiased Line Enable: Enables anti-aliased line rendering.

Creating a Rasterizer State Object

In Direct3D 11, you create a rasterizer state object using the ID3D11Device::CreateRasterizerState method. This method takes a pointer to a D3D11_RASTERIZER_DESC structure, which you populate with your desired settings, and an output pointer to an ID3D11RasterizerState object.

Example: Creating a Solid, Back-Cull Rasterizer State


D3D11_RASTERIZER_DESC rsDesc;
ZeroMemory(&rsDesc, sizeof(rsDesc)); // Initialize to zeros

rsDesc.FillMode = D3D11_FILL_SOLID;         // Fill triangles with solid color
rsDesc.CullMode = D3D11_CULL_BACK;          // Cull back faces
rsDesc.FrontCounterClockwise = FALSE;       // Standard winding order (clockwise is front)
rsDesc.DepthBias = 0;
rsDesc.DepthBiasClamp = 0.0f;
rsDesc.SlopeScaleDepthBias = 0.0f;
rsDesc.DepthClipEnable = TRUE;              // Clip geometry at near/far planes
rsDesc.ScissorEnable = FALSE;               // Disable scissor test
rsDesc.MultisampleEnable = FALSE;           // Disable multisampling
rsDesc.AntialiasedLineEnable = FALSE;       // Disable antialiased lines

ID3D11RasterizerState* pRasterizerState = NULL;
HRESULT hr = pDevice->CreateRasterizerState(&rsDesc, &pRasterizerState);
if (SUCCEEDED(hr))
{
    // Rasterizer state created successfully
}
                    

Setting the Rasterizer State

Once created, the rasterizer state object is bound to the pipeline using the ID3D11DeviceContext::RSSetState method. This makes your configured rasterizer state active for subsequent draw calls.

Example: Binding the Rasterizer State


pImmediateContext->RSSetState(pRasterizerState);
// Subsequent draw calls will use this rasterizer state.
                    

Common Use Cases

  • Wireframe Rendering: Setting FillMode to D3D11_FILL_WIREFRAME is useful for debugging or stylistic effects.
  • Performance Optimization: Using CullMode effectively reduces the number of primitives processed by the GPU.
  • Preventing Z-Fighting: Adjusting DepthBias and SlopeScaleDepthBias can resolve flickering issues with overlapping surfaces.
  • Screen-Space Effects: Enabling ScissorEnable can clip rendering to specific screen regions for UI elements or post-processing effects.

Understanding and configuring the rasterizer state is crucial for fine-grained control over how your 3D scenes are rendered, impacting both visual fidelity and performance.