DirectX - Blending



DirectX includes a blending property which defines the combination of creation from the existing one. Consider that there are two sources namely: Source 1 and Source 2 where Source 1 refers to the data source for source pixel blending factor and Source 2 is the data source for destination pixel blending factor. DirectX does the blending in such a way that the source pixel is always the fragment coming from the pixel shader’s output and the destination pixel is always the pixel from the current active render target.

DirectX from version 1 has a blend state structure: ID3D10BlendState which is a standalone structure which describes the state of the flow chart or rather the mentioned sources. To create this blend state, the user needs to first fill out a D3D10_BLEND_DESC structure which describes the various options of the blend state. This seems to be confusing as SDK docs mention different documentation states of D3D10_BLEND_DESC.

The simple structure is mentioned in the code below −

struct D3D10_BLEND_DESC{
   BOOL AlphaToCoverageEnable;
   BOOL BlendEnable[8];
   D3D10_BLEND SrcBlend;
   D3D10_BLEND DestBlend;
   D3D10_BLEND_OP BlendOp;
   D3D10_BLEND SrcBlendAlpha;
   D3D10_BLEND DestBlendAlpha;
   D3D10_BLEND_OP BlendOpAlpha;
   UINT8 RenderTargetWriteMask[8];
}

The factors to work on or rather the parameters which are included with required explanation are mentioned below −

SrcBlend − This blend option is used as the source blending factor data source and consists of an optional pre-blend operation.

DestBlend − This blend option includes the destination blending factor data source and a pre-blend operation.

SrcBlendAlpha − This blend option specifies the source alpha blending factor data source and includes an operation of pre-blending situation. Blend options which end in _COLOR are usually not considered.

DestBlendAlpha − This blend option specifies the destination alpha blending factor data source and includes an optional pre-blend operation. Blend options which end in _COLOR are usually not considered.

There are two more parameters (SrcBlendAlpha & DestBlendAlpha) which actually define the blending factors for the source and destination alpha channels. The blending of the alpha values is done in a complete different format from the blending of color values. Now, it is important that user should use your final image as a flat bitmap.

Creating the custom blend state is explained perfectly below −

ID3D10BlendState* g_pBlendState = NULL;
D3D10_BLEND_DESC BlendState;
ZeroMemory(&BlendState, sizeof(D3D10_BLEND_DESC));

BlendState.BlendEnable[0] = TRUE;
BlendState.SrcBlend = D3D10_BLEND_SRC_ALPHA;
BlendState.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
BlendState.BlendOp = D3D10_BLEND_OP_ADD;
BlendState.SrcBlendAlpha = D3D10_BLEND_ZERO;
BlendState.DestBlendAlpha = D3D10_BLEND_ZERO;
BlendState.BlendOpAlpha = D3D10_BLEND_OP_ADD;
BlendState.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;

pd3dDevice->CreateBlendState(&BlendState, &g_pBlendState);

In the above mentioned code snippet, blendState is used frequently in every line. It is really simple to understand the OMSetBlendState function of our D3D device. This sets up the blending stage to the state that we have described in our blend state object.

pd3dDevice->OMSetBlendState(g_pBlendState, 0, 0xffffffff);

The first parameter is obviously considered as the blend state. The second is an array of blend factors to use in special case which is used as color channel blending; while the third parameter includes a sample mask which defines bits to be written during blending with respect to render targets.

Advertisements