We know that in d3d11, objects are rendered by frame. Each frame may contain several primitive values, as shown in the following figure:
In actual rendering, the GPU will be rendered by frame. For example, in frame0, there are two primitive (triangle). After vs, the PA (primitive assemble) block will be assembled by the body element, then perform the raster operation. During the raster operation, the value of depth buffer will be compared. Because the Z value of the red triangle is smaller, it will cover a part of the black triangle.
What should we do if we want to mix two triangles for display? In this case, the Alpha blend operation is required, that is, mixing the content in the buffer with the pixel color of the current PS output. With the Alpha blend operation, we can achieve transparency and other effects.
Now let's take a look at the implementation principle of Alpha blend in d3d11:
1. Mixed Equations
Assume that the pixel color output by PS is CSRC = (RS, GS, BS), and the corresponding pixel color value in the buffer is cdst = (Rd, Gd, BD ),
The colors FSRC and fdst are called the blend factor and the blend factor, indicating that the color is multiplied by component, indicating any operators, such as addition and subtraction.
After the equation is calculated, C is the final color value, which overwrites the buffered pixel color.
The above equation is for the RGB color and corresponds to the Alpha value:
2. Blend operation
The mixed equation is one of the following operators:
typedef enum D3D11_BLEND_OP{ D3D11_BLEND_OP_ADD = 1, D3D11_BLEND_OP_SUBTRACT = 2, D3D11_BLEND_OP_REV_SUBTRACT = 3, D3D11_BLEND_OP_MIN = 4, D3D11_BLEND_OP_MAX = 5,} D3D11_BLEND_OP;
Constants
-
D3d11_blend_op_add: Add two colors.
-
D3d11_blend_op_subtract, source1 minus source2.
-
D3d11_blend_op_rev_subtract, source2 minus source1
-
D3d11_blend_op_min.
-
D3d11_blend_op_max is the maximum color value.
3. Blend factor
D3d11_blend_zero F = (0, 0, 0, 0)
D3d11_blend_one F = (1, 1, 1, 1)
D3d11_blend_src_color F = (RS, GS, BS)
D3d11_blend_inv_src_color F = (1-Rs, 1-GS, 1-BS)
D3d11_blend_src_alpha F = (Alphas, Alphas, Alphas). The blending factor is the Alpha value of the source color.
D3d11_blend_inv_src_alpha F = (1-Alphas, 1-Alphas, 1-Alphas)
D3d11_blend_dest_alpha F = (alphad, alphad, alphad). The blending factor is the Alpha value of the target color.
D3d11_blend_inv_dest_alpha F = (1-alphad, 1-alphad, 1-alphad ),
D3d11_blend_dest_color F = (Rd, Gd, BD)
D3d11_blend_inv_dest_color F = (1-Rd, 1-Gd, 1-BD)
D3d11_blend_src_alpha_sat F = (alpha's, Alpha's, Alpha's), alpha's = clamp (Alphas, 0, 1)
Here are some other blend factor settings. For details, refer to the DirectX documentation. Note that the alpha blend factor, whose end is _ color, cannot be used.
4. Blend status
Before using alpha blend, we need to set the blend state and enalbe it.
First, enter a d3d11_blend_desc structure and call createblendstate to create the blend state.
It is important to set the blend factor and Operation Method in the MRT. (Generally, it is the MRT [0], which corresponds to an output. If multiple MRT servers are used, you can set different blend factors or operation methods ).
Typedef struct d3d11_blend_desc
{Bool alphatocoverageenable;
Bool independentblendenable;
D3d11_render_target_blend_desc rendertarget [8];
} D3d11_blend_desc;
Typedef struct d3d11_render_target_blend_desc
{
Bool blendenable;
D3d11_blend srcblend;
D3d11_blend destblend;
D3d11_blend_op blendop;
D3d11_blend srcblendalpha;
D3d11_blend destblendalpha;
D3d11_blend_op blendopalpha;
Uint8 rendertargetwritemask;
} D3d11_render_target_blend_desc;
Now we modify the code of mytutoriald3d11_38 and add blend support to make the water transparent. In fact, the code is very simple. First, add two functions to the d3dclass class.
Void d3dclass: turnonalphablending ()
{
Float blendfactor [4];
// Set the blend factor
Blendfactor [0] = 0.0f;
Blendfactor [1] = 0.0f;
Blendfactor [2] = 0.0f;
Blendfactor [3] = 0.0f;
// Open Alpha Blend
M_devicecontext-> omsetblendstate (m_alphaenableblendingstate, blendfactor, 0 xffffffff );
Return;
}
Void d3dclass: turnoffalphablending ()
{
Float blendfactor [4];
// Set the blend factor
Blendfactor [0] = 0.0f;
Blendfactor [1] = 0.0f;
Blendfactor [2] = 0.0f;
Blendfactor [3] = 0.0f;
// Disable Alpha Blend
M_devicecontext-> omsetblendstate (m_alphadisableblendingstate, blendfactor, 0 xffffffff );
Return;
}
We also create two blend states in the initialization function, one for enable blend and the other for disable blend.
D3d11_blend_desc blendstatedescription;
...
// Initialize the blend Descriptor
Zeromemory (& blendstatedescription, sizeof (d3d11_blend_desc ));
// Create an Alpha blend status.
Blendstatedescription. rendertarget [0]. blendenable = true;
Blendstatedescription. rendertarget [0]. srcblend = d3d11_blend_src_alpha;
Blendstatedescription. rendertarget [0]. destblend = d3d11_blend_inv_src_alpha;
Blendstatedescription. rendertarget [0]. blendop = d3d11_blend_op_add;
Blendstatedescription. rendertarget [0]. srcblendalpha = d3d11_blend_one;
Blendstatedescription. rendertarget [0]. destblendalpha = d3d11_blend_zero;
Blendstatedescription. rendertarget [0]. blendopalpha = d3d11_blend_op_add;
Blendstatedescription. rendertarget [0]. rendertargetwritemask = d3d11_color_write_enable_all; // 0x0f;
// Create an Alpha blend state with the descriptor
Result = m_device-> createblendstate (& blendstatedescription, & m_alphaenableblendingstate );
If (failed (result ))
{
Return false;
}
// Modify the descriptor.
Blendstatedescription. rendertarget [0]. blendenable = false;
// Create a new blend status.
Result = m_device-> createblendstate (& blendstatedescription, & m_alphadisableblendingstate );
In the graphicsclass class, open Alpha blend when rendering the water
// Open Alpha blend.
M_d3d-> turnonalphablending ();
// Place model vertices and index Buffering in the pipeline for rendering.
M_watermodel-> render (m_d3d-> getdevicecontext ());
Result = m_lighttexshader-> render (m_d3d-> getdevicecontext (), m_watermodel-> getindexcount (), worldmatrix, viewmatrix, projectionmatrix,
Light, material, camera, m_texmanager-> createtex (m_d3d-> getdevice (), string ("water2.dds ")));
If (! Result)
{
Return false;
}
// Disable Alpha blend.
M_d3d-> turnoffalphablending ();
The last point is to make a small change in lighttex. PS,
Float4 finalcolor1;
Finalcolor1 = float4 (finalcolor. XYZ, 0.5 );
Return finalcolor1;
This is because our mixing factor is derived from the Alpha value.
Blendstatedescription. rendertarget [0]. srcblend = d3d11_blend_src_alpha;
Blendstatedescription. rendertarget [0]. destblend = d3d11_blend_inv_src_alpha;
The final execution result of the program is as follows:
Complete code can be found:
Project File mytutoriald3d11_39
Download Code:
Http://files.cnblogs.com/mikewolf2002/d3d1139-49.zip
Http://files.cnblogs.com/mikewolf2002/pictures.zip