Click to Rate and Give Feedback
MSDN
MSDN Library
DirectX
SDK Documentation
DirectX Graphics
Direct3D 10
Programming Guide
Pipeline Stages
Output-Merger Stage
 Configuring Blending Functionality ...

  Switch on low bandwidth view
Configuring Blending Functionality (Direct3D 10)
Bb205072.XDK_CHM_BANNER_left(en-us,VS.85).jpgBb205072.XDK_CHM_BANNER_right(en-us,VS.85).jpg

Configuring Blending Functionality (Direct3D 10)

Blending operations are performed on every pixel shader output (RGBA value) before the output value is written to a render target. If multisampling is enabled, blending is done on each multisample; otherwise, blending is performed on each pixel.

Create the Blend State

The blend state is a collection of states used to control blending. These states (defined in D3D10_BLEND_DESC) are used to create the blend state object by calling ID3D10Device::CreateBlendState.

For instance, here is a very simple example of blend-state creation that disables alpha blending and uses no per-component pixel masking.

ID3D10BlendState* g_pBlendStateNoBlend = NULL;

D3D10_BLEND_DESC BlendState;
ZeroMemory(&BlendState, sizeof(D3D10_BLEND_DESC));
BlendState.BlendEnable[0] = FALSE;
BlendState.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
pd3dDevice->CreateBlendState(&BlendState, &g_pBlendStateNoBlend);

This example is from the HLSLWithoutFX10 Sample.

Bind the Blend State

Once the blend-state object is created, bind the blend-state object to the output-merger stage by calling ID3D10Device::OMSetBlendState.

float blendFactor = 0;
UINT sampleMask   = 0xffffffff;

pd3dDevice->OMSetBlendState(g_pBlendStateNoBlend, blendFactor, sampleMask);

This API takes three parameters: the blend-state object, a four-component blend factor, and a sample mask. You may pass in NULL for the blend-state object to specify default blend state or pass in a blend-state object. The blend factor gives you per-component control over blending the new per-pixel values with the existing value. The sample mask is a user-defined mask that determines how to sample the existing render target before updating it. The default sampling mask is 0xffffffff which designates point sampling.

In most depth buffering schemes, the pixel closest to the camera is the one that gets drawn. When setting up the depth stencil state, the DepthFunc parameter of D3D10_DEPTH_STENCIL_DESC can be any D3D10_COMPARISON_FUNC. Normally, you would want the DepthFunc to be D3D10_COMPARISON_LESS, so that the pixels closest to the camera will overwrite the pixels behind them. However, depending on the needs of your application, any of the other comparison functions may be used to do the depth test.

Advanced Blending Topics

Alpha-To-Coverage

Alpha-to-coverage is a multisampling technique that is most useful for situations such as dense foliage where there are several overlapping polygons. It can be turned on by setting the AlphaToCoverageEnable variable to true in the blend description. Alpha-to-coverage will work regardless of whether or not multisampling is also turned on in the rasterizer state (by setting MultisampleEnable to true or false).

Alpha-to-coverage works by taking the alpha component of an rgba value after it is output from a pixel shader and converting it into an n-step coverage mask (where n is the sample count). This n-step coverage mask is then ANDed with the multisample coverage mask and the result is used to determine which samples should get updated for all of the render targets currently bound to the output merger. The original alpha value in the output of the pixel shader is not changed when that alpha value is used to create the n-step coverage mask (alpha blending will still occur on a per-sample basis). Alpha-to-coverage multisampling is essentially the same as regular multisampling except that the n-step coverage mask is generated and ANDed with the multisample coverage mask.

For an example of alpha-to-coverage, see the Instancing10 Sample.

Blending Pixel Shader Outputs

This feature enables the output merger to use both the pixel shader outputs simultaneously as input sources to a blending operation with the single render target at slot 0.

This example takes two results and combines them in a single pass, blending one into the destination with a multiply and the other with an add:

SrcBlend = D3D10_BLEND_ONE;
DestBlend = D3D10_BLEND_SRC1COLOR;

This example configures the first pixel shader output as the source color and the second output as a per-color component blend factor.

SrcBlend = D3D10_BLEND_SRC1COLOR;
DestBlend = D3D10_BLEND_INVSRC1COLOR;

This example illustrates how the blend factors must match the shader swizzles:

SrcFactor = D3D10_BLEND_SRC1ALPHA;
DestFactor = D3D10_BLEND_SRCCOLOR;
OutputWriteMask[0] = .ra; // pseudocode for setting the mask at
                          // RenderTarget slot 0 to .ra

Together, the blend factors and the shader code imply that the pixel shader is required to output at least o0.r and o1.a. Extra output components can be output by the shader but would be ignored, fewer components would produce undefined results.

See Also

Pipeline Stages (Direct3D 10)

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker