DIRECTX11 Note 6: Blend Blend

Source: Internet
Author: User

The idea of mixing is to solve some material problems that need to be superimposed, and there is a good example in the book: Fog.

Of course there are other uses, such as stacking 2 different materials to produce magical effects (see Section 9.5.2).

Mixed code operations:

In accordance with the usual logic, we create a thing in the D3D to fill a lot of structure, then use a function to initialize, and then in the rendering step to further processing. The same here ....

Use Createblendstate to initialize the description of the blend:

Virtual HRESULT stdmethodcalltype createblendstate (             /**/             _in_   Const D3d11_blend_desc *Pblendstatedesc,            /* */             _out_opt_   **ppblendstate)

This is not much to say, the first parameter is a description of the structure, the second parameter is used to return.

The main one is to look at the first structural body:

struct D3d11_blend_desc {  BOOL                           alphatocoverageenable;  BOOL                           independentblendenable;  D3d11_render_target_blend_desc rendertarget[8];} D3d11_blend_desc;

There is an explanation in the book that the first parameter indicates whether to use a multi-sampling technique called alpha-to-coverage, which is now not set to false, and the second parameter indicates whether to render with multiple targets at the same time. Set to False, you can only use rendertarget[0] for rendering.

In other words, the first two parameters are now in my technology, nothing soft.

The third parameter is also a struct:

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;

This is what we are mainly discussing:

You can see the MSDN explanation.

Blendenable default False, set to true to indicate the use of mixed

Srcblend,destblend and Srcblendalpha,destblendalpha are all members of the enumeration type D3d11_blend specify the blending factor.

Blendop and Blendopalpha are members of the D3d11_blend, specifying a mixed operator

First look at the mathematical equation of mixing:

C = csrc⊗fsrc? Cdst⊗fdst

Here CSRC refers to the color of the pixel we are dealing with, cdes refers to the color that has been processed and placed in the back buffer (there will be a depth problem, not to be considered first). Without turning on blending, the color we're working with will overwrite the background color. When blending is used, the background color and current color will produce a new color through the above formula.

Look at the mixed coefficients fsrc and FDST are the srcblend,destblend of the enumeration body definition:

typedefenumD3d11_blend {D3d11_blend_zero=1, D3d11_blend_one=2, D3d11_blend_src_color=3, D3d11_blend_inv_src_color=4, D3d11_blend_src_alpha=5, D3d11_blend_inv_src_alpha=6, D3d11_blend_dest_alpha=7, D3d11_blend_inv_dest_alpha=8, D3d11_blend_dest_color=9, D3d11_blend_inv_dest_color=Ten, D3d11_blend_src_alpha_sat= One, D3d11_blend_blend_factor= -, D3d11_blend_inv_blend_factor= the, D3d11_blend_src1_color= -, D3d11_blend_inv_src1_color= -, D3d11_blend_src1_alpha= -, D3d11_blend_inv_src1_alpha= +} d3d11_blend;

Here I quote other people's translation, address:

D3d11_blend_zero:f = (0,0,0) and F =0d3d11_blend_one:f= (1,1,1) and F =1d3d11_blend_src_color:f=(RS, GS, BS) d3d11_blend_inv_src_color:f= (1-RS,1-GS,1-BS) D3d11_blend_src_alpha:f= ( as, as, as) and F = asd3d11_blend_inv_src_alpha:f= (1− as,1− as,1− as) and F =1− asd3d11_blend_dest_alpha:f= (AD, ad, AD) and F =add3d11_blend_inv_dest_alpha:f= (1–ad,1−ad,1–ad) and F =1–add3d11_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= (as?, as?, as?) and F = as?, where, as? = Clamp ( as,0,1) D3d11_blend_blend_factor:f= (r,g,b) and F = A, where the color (r,g,b,a) is specified by the 2nd parameter of the Id3d11devicecontext::omsetblendstate method (9. Section 4). That is, we can specify a color as a blending factor, and the color is valid until you modify the blending state. D3d11_blend_inv_blend_factor:f= (1-R,1-G,1-B) and F =1-A, where the color (r,g,b,a) is specified by the 2nd parameter of the Id3d11devicecontext::omsetblendstate method (9. Section 4). That is, we can specify a color as a blending factor, and the color is valid until you modify the blending state.



Then there is the definition of the operator, in mathematical expressions, the body of the structure is BLENDOP.

enum d3d11_blend_op {   d3d11_blend_op_add           1,//c = csrc⊗fsrc + cdst⊗fdst  plus  d3d11_b Lend_op_subtract      2,//c = cdst⊗fdst-csrc⊗fsrc  minus  d3d11_blend_op_rev_subtract
    3,//c = Csrc⊗fsrc-cdst⊗fdst  subtraction, in  turn           d3d11_blend_op_min4
    ,//Take small  d3d11_blend_op_max           
    5//Fetch large 
    } d3d11_blend_op;


Finally, there is an element:

Rendertargetwritemask, This parameter restricts the type of write, such as red color write, can be a combination of multiple flags, as follows:

enum d3d11_color_write_enable {     1,     2,     4,      8 ,      =      (d3d11_color_write_enable_red| d3d11_color_write_enable_green|        | D3d11_color_write_enable_alpha)} d3d11_color_write_enable;

Then I create the code:

HRESULT drawfunction::createblending (void) {HRESULT hr= S_OK;//ret//Set the blend descriptionD3d11_blend_desc Blenddesc; ZeroMemory (&blenddesc,sizeof(BLENDDESC)); Blenddesc.alphatocoverageenable=FALSE; Blenddesc.independentblendenable=FALSE; blenddesc.rendertarget[0]. Blendenable =TRUE; blenddesc.rendertarget[0]. Srcblend =D3d11_blend_one; blenddesc.rendertarget[0]. Destblend =D3d11_blend_one; blenddesc.rendertarget[0]. Blendop =d3d11_blend_op_subtract; blenddesc.rendertarget[0]. Srcblendalpha =D3d11_blend_one; blenddesc.rendertarget[0]. Destblendalpha =D3d11_blend_one; blenddesc.rendertarget[0]. Blendopalpha =d3d11_blend_op_subtract; blenddesc.rendertarget[0]. Rendertargetwritemask =D3d11_color_write_enable_all; HR= Dev->createblendstate (&blenddesc, &pblendstatesub); blenddesc.rendertarget[0]. Srcblend =D3d11_blend_src_alpha; blenddesc.rendertarget[0]. Destblend =D3d11_blend_inv_src_alpha; blenddesc.rendertarget[0]. Srcblendalpha =D3d11_blend_src_alpha; blenddesc.rendertarget[0]. Destblendalpha =D3d11_blend_inv_src_alpha; HR= Dev->createblendstate (&blenddesc, &Pblendstatealpha); returnhr;}

Finally, you can use the Omsetblendstate setting to enable/disable blending.

void omsetblendstate (  [in]       id3d10blendstate *pblendstate,  [in  Const FLOAT            blendfactor[4],  [in]       UINT             samplemask) ;

is primarily the first parameter, set to NULL to turn off blending (set to default state?). )

Other look at the simple demon I've uploaded.

Project file Download

DIRECTX11 Note 6: Blend Blend

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.