本文由zhangbaochong原创,转载请注明出处http://www.cnblogs.com/zhangbaochong/p/5634580.html

在d3d11中是按frame来渲染物体的,在同一frame中又可能不止一种primitive,例如下图:

gpu实际渲染时,会按帧渲染,像上图中的一帧中含有两个三角形 ,经过vs以后,PA(primitive assemble) block会进行体元装配,然后进行光栅化操作,光栅化操作时候,会比较depth buffer的值,红色三角形的z值更小,所以会将黑色三角形覆盖一部分。

而当混合功能启用时,决定最终的颜色方法会变得不同,当一个片段通过深度测试时,并不是直接取代后缓冲的颜色,而是通过一定的方法比例与之混合,从而显示混合后的颜色。

1.混合方程

混合方程如下:

C是混合结果,Csrc是源颜色,也就是当前要处理的片段颜色,而Cdst是目标颜色,也就是后缓冲区的颜色。F则是对应的混合因子,等会儿会详细说明。这里的可以看作是分量相乘,即颜色中的R、G、B值分别相乘。则是混合操作,和四则运算操作类似。

2.混合操作

对于混合操作,在d3d中定义在一个枚举中,原型如下:

typedef enum D3D11_BLEND_OP {
D3D11_BLEND_OP_ADD = ,
D3D11_BLEND_OP_SUBTRACT = ,
D3D11_BLEND_OP_REV_SUBTRACT = ,
D3D11_BLEND_OP_MIN = ,
D3D11_BLEND_OP_MAX =
} D3D11_BLEND_OP;

在MSDN中解释如下:

D3D11_BLEND_OP_ADD

Add source 1 and source 2.

D3D11_BLEND_OP_SUBTRACT

Subtract source 1 from source 2.

D3D11_BLEND_OP_REV_SUBTRACT

Subtract source 2 from source 1.

D3D11_BLEND_OP_MIN

Find the minimum of source 1 and source 2.

D3D11_BLEND_OP_MAX

Find the maximum of source 1 and source 2.

3.混合因子

对于混合因子F,在d3d中也定义为枚举,原型如下:

typedef enum D3D11_BLEND {
D3D11_BLEND_ZERO = ,
D3D11_BLEND_ONE = ,
D3D11_BLEND_SRC_COLOR = ,
D3D11_BLEND_INV_SRC_COLOR = ,
D3D11_BLEND_SRC_ALPHA = ,
D3D11_BLEND_INV_SRC_ALPHA = ,
D3D11_BLEND_DEST_ALPHA = ,
D3D11_BLEND_INV_DEST_ALPHA = ,
D3D11_BLEND_DEST_COLOR = ,
D3D11_BLEND_INV_DEST_COLOR = ,
D3D11_BLEND_SRC_ALPHA_SAT = ,
D3D11_BLEND_BLEND_FACTOR = ,
D3D11_BLEND_INV_BLEND_FACTOR = ,
D3D11_BLEND_SRC1_COLOR = ,
D3D11_BLEND_INV_SRC1_COLOR = ,
D3D11_BLEND_SRC1_ALPHA = ,
D3D11_BLEND_INV_SRC1_ALPHA =
} D3D11_BLEND;

在MSDN中解释如下:

D3D11_BLEND_ZERO

The blend factor is (0, 0, 0, 0). No pre-blend operation.

D3D11_BLEND_ONE

The blend factor is (1, 1, 1, 1). No pre-blend operation.

D3D11_BLEND_SRC_COLOR

The blend factor is (Rₛ, Gₛ, Bₛ, Aₛ), that is color data (RGB) from a pixel shader. No pre-blend operation.

D3D11_BLEND_INV_SRC_COLOR

The blend factor is (1 - Rₛ, 1 - Gₛ, 1 - Bₛ, 1 - Aₛ), that is color data (RGB) from a pixel shader. The pre-blend operation inverts the data, generating 1 - RGB.

D3D11_BLEND_SRC_ALPHA

The blend factor is (Aₛ, Aₛ, Aₛ, Aₛ), that is alpha data (A) from a pixel shader. No pre-blend operation.

D3D11_BLEND_INV_SRC_ALPHA

The blend factor is ( 1 - Aₛ, 1 - Aₛ, 1 - Aₛ, 1 - Aₛ), that is alpha data (A) from a pixel shader. The pre-blend operation inverts the data, generating 1 - A.

D3D11_BLEND_DEST_ALPHA

The blend factor is (Ad Ad Ad Ad), that is alpha data from a render target. No pre-blend operation.

D3D11_BLEND_INV_DEST_ALPHA

The blend factor is (1 - Ad 1 - Ad 1 - Ad 1 - Ad), that is alpha data from a render target. The pre-blend operation inverts the data, generating 1 - A.

D3D11_BLEND_DEST_COLOR

The blend factor is (Rd, Gd, Bd, Ad), that is color data from a render target. No pre-blend operation.

D3D11_BLEND_INV_DEST_COLOR

The blend factor is (1 - Rd, 1 - Gd, 1 - Bd, 1 - Ad), that is color data from a render target. The pre-blend operation inverts the data, generating 1 - RGB.

D3D11_BLEND_SRC_ALPHA_SAT

The blend factor is (f, f, f, 1); where f = min(Aₛ, 1 - Ad). The pre-blend operation clamps the data to 1 or less.

D3D11_BLEND_BLEND_FACTOR

The blend factor is the blend factor set with ID3D11DeviceContext::OMSetBlendState. No pre-blend operation.

D3D11_BLEND_INV_BLEND_FACTOR

The blend factor is the blend factor set with ID3D11DeviceContext::OMSetBlendState. The pre-blend operation inverts the blend factor, generating 1 - blend_factor.

D3D11_BLEND_SRC1_COLOR

The blend factor is data sources both as color data output by a pixel shader. There is no pre-blend operation. This blend factor supports dual-source color blending.

D3D11_BLEND_INV_SRC1_COLOR

The blend factor is data sources both as color data output by a pixel shader. The pre-blend operation inverts the data, generating 1 - RGB. This blend factor supports dual-source color blending.

D3D11_BLEND_SRC1_ALPHA

The blend factor is data sources as alpha data output by a pixel shader. There is no pre-blend operation. This blend factor supports dual-source color blending.

D3D11_BLEND_INV_SRC1_ALPHA

The blend factor is data sources as alpha data output by a pixel shader. The pre-blend operation inverts the data, generating 1 - A. This blend factor supports dual-source color blending.

4.具体使用方法

在d3d11中,要使用混合首先要创建混合状态接口ID3D11BlendState,创建要调用CreateBlendState函数,原型如下:

HRESULT CreateBlendState(
[in] const D3D11_BLEND_DESC *pBlendStateDesc,
[out, optional] ID3D11BlendState **ppBlendState
);

D3D11_BLEND_DESC 是描述混合状态的结构,原型如下:

typedef struct D3D11_BLEND_DESC {
BOOL AlphaToCoverageEnable;
BOOL IndependentBlendEnable;
D3D11_RENDER_TARGET_BLEND_DESC RenderTarget[];
} D3D11_BLEND_DESC;

第一个参数设置是否打开AlphaToCoverage,AlphaToCoverage在后面会详细介绍,暂时先不用,设置为false;

第二个参数是针对不同的RenderTarget使用不同的混合方式,最多支持8个不同的RenderTarget,我们暂时还用不到设为false;

第三个参数为针对8个不同RenderTarget分别指定的混合状态参数,当第二个参数为false时,这里我们只需要设置第一个元素即可。

D3D11_RENDER_TARGET_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;

MSDN中解释如下:

BlendEnable

Type: BOOL

Enable (or disable) blending.

SrcBlend

Type: D3D11_BLEND

This blend option specifies the operation to perform on the RGB value that the pixel shader outputs. The BlendOp member defines how to combine the SrcBlend and DestBlend operations.

DestBlend

Type: D3D11_BLEND

This blend option specifies the operation to perform on the current RGB value in the render target. The BlendOp member defines how to combine the SrcBlend and DestBlend operations.

BlendOp

Type: D3D11_BLEND_OP

This blend operation defines how to combine the SrcBlend and DestBlend operations.

SrcBlendAlpha

Type: D3D11_BLEND

This blend option specifies the operation to perform on the alpha value that the pixel shader outputs. Blend options that end in _COLOR are not allowed. The BlendOpAlpha member defines how to combine the SrcBlendAlpha and DestBlendAlpha operations.

DestBlendAlpha

Type: D3D11_BLEND

This blend option specifies the operation to perform on the current alpha value in the render target. Blend options that end in _COLOR are not allowed. The BlendOpAlpha member defines how to combine the SrcBlendAlpha and DestBlendAlpha operations.

BlendOpAlpha

Type: D3D11_BLEND_OP

This blend operation defines how to combine the SrcBlendAlpha and DestBlendAlpha operations.

RenderTargetWriteMask

Type: UINT8

A write mask.

创建好ID3D11BlendState接口后,通过OMSetBlendState函数来设置为指定的状态,原型如下:

void OMSetBlendState(
[in] ID3D10BlendState *pBlendState,
[in] const FLOAT BlendFactor[],
[in] UINT SampleMask
);

其中第二个参数,为手动指定的混合因子,如果在刚才指定混合因子时使用D3D11_BLEND_BLEND_FACTOR或D3D11_BLEND_INV_BLEND_FACTOR,则使用第二个参数作为混合因子。

第三个参数为采样点掩码。在d3d11中最多可以支持32重采样,通过该参数来指定使用哪些采样点,参数类型为UINT32位,每位1和0代表使用或丢弃该采样点,如果我们想使用所有采样点,则可以设该参数为0xffffffff。

下面是demo中使用混合的部分代码:

首先在类中定义了渲染状态接口:

ID3D11BlendState*        m_pBlendState;    //混合状态

然后自定义了一个函数用于创建渲染状态:

bool BlendDemo::BuildBlendState()
{
D3D11_BLEND_DESC blendStateDescription;
// 初始化blend描述符
ZeroMemory(&blendStateDescription, sizeof(D3D11_BLEND_DESC)); // 创建一个alpha blend状态.
blendStateDescription.RenderTarget[].BlendEnable = TRUE;
blendStateDescription.RenderTarget[].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendStateDescription.RenderTarget[].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendStateDescription.RenderTarget[].BlendOp = D3D11_BLEND_OP_ADD;
blendStateDescription.RenderTarget[].SrcBlendAlpha = D3D11_BLEND_ONE;
blendStateDescription.RenderTarget[].DestBlendAlpha = D3D11_BLEND_ZERO;
blendStateDescription.RenderTarget[].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendStateDescription.RenderTarget[].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; if (FAILED(m_pd3dDevice->CreateBlendState(&blendStateDescription, &m_pBlendState)))
{
MessageBox(NULL, L"Create 'Transparent' blend state failed!", L"Error", MB_OK);
return false;
} return true;
}

我们利用混合,实现水面的透明效果。当绘制的图形中有透明物体时,绘制的先后顺序就显得尤为重要,一般我们遵循以下原则:首先绘制非透明物体。然后,根据透明物体与摄像机之间的距离进行排序,按照从后向前的顺序绘制透明物体。之所以要按照从后向前的顺序进行绘制,是为了让前面的物体和后面的物体进行混合。如果一个物体是透明的,那么我们就会透过这个物体看到它后面的其他物体。所以,必须将透明物体后面的所有物体先绘制出来,然后才能将透明的源像素和后台缓冲区中的目标像素进行混合。

在demo中,我们一共有3个物体,箱子、地面还有水面。因此我们先绘制箱子和地面,最后在绘制水面的时候开启混合,绘制完毕再关闭混合。

       //绘制箱子...
//绘制地面...
//绘制水面
//开启创建好的blend状态 效果为透明
float blendFactor[] = { .f,.f,.f,.f };
m_pImmediateContext->OMSetBlendState(m_pBlendState, blendFactor, 0xffffffff);
Effects::ms_pBasicEffect->m_pFxWorld->SetMatrix(reinterpret_cast<const float*>(&m_worldWater));
Effects::ms_pBasicEffect->m_pFxWorldViewProj->SetMatrix(reinterpret_cast<const float*>(&m_worldViewProjWater));
Effects::ms_pBasicEffect->m_pFxWorldInvTranspose->SetMatrix(reinterpret_cast<const float*>(&m_worldInvTransposeWater));
Effects::ms_pBasicEffect->m_pFxMaterial->SetRawValue(&m_materialWater, , sizeof(m_materialWater));
Effects::ms_pBasicEffect->m_pFxTexTrans->SetMatrix(reinterpret_cast<const float*>(&m_texTransWater));
Effects::ms_pBasicEffect->m_pFxSR->SetResource(m_pSRVWater);
tech->GetPassByIndex(i)->Apply(, m_pImmediateContext);
m_pImmediateContext->DrawIndexed(m_water.indices.size(), m_waterIStart, m_waterVStart);
//恢复状态
m_pImmediateContext->OMSetBlendState(, , 0xffffffff);

5.示例程序

5.1运行效果

由于水面是静态的,截图看的话效果不是很好/(ㄒoㄒ)/~~

5.2源码下载

地址:http://files.cnblogs.com/files/zhangbaochong/BlendDemo.zip

由于源码上传在了博客园的文件管理中,提供的空间很小,因此就不上传整个工程了,只是把代码文件上传了,要想运行的话配置一下用vs重新编译吧O(∩_∩)O~

ps:之前代码中,创建顶点索引缓冲、加载shader、加载图片等我全都给放在一个函数中实现了,随着代码越来越多,感觉易读性非常差。于是就参考龙书的架构,重构了下代码,显得条理了不少…

Directx11学习笔记【十八】 Blending混合的更多相关文章

  1. python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置

    python3.4学习笔记(十八) pycharm 安装使用.注册码.显示行号和字体大小等常用设置Download JetBrains Python IDE :: PyCharmhttp://www. ...

  2. Directx11学习笔记【八】 龙书D3DApp的实现

    原文:Directx11学习笔记[八] 龙书D3DApp的实现 directx11龙书中的初始化程序D3DApp跟我们上次写的初始化程序大体一致,只是包含了计时器的内容,而且使用了深度模板缓冲. D3 ...

  3. (C/C++学习笔记) 十八. 继承和多态

    十八. 继承和多态 ● 继承的概念 继承(inheritance): 以旧类为基础创建新类, 新类包含了旧类的数据成员和成员函数(除了构造函数和析构函数), 并且可以派生类中定义新成员. 形式: cl ...

  4. Java基础学习笔记十八 异常处理

    什么是异常?Java代码在运行时期发生的问题就是异常. 在Java中,把异常信息封装成了一个类.当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置.原因等). 异常的继承体系 在 ...

  5. MYSQL进阶学习笔记十八:MySQL备份和还原!(视频序号:进阶_37)

    知识点十九:MySQL的备份的还原(38) 一.mysql的备份 1.通过使用mysqldump的命令备份 使用mysqldump命令备份,mysqldump命令将数据库中的数据备份成一个文本文件.表 ...

  6. JavaScript权威设计--事件冒泡,捕获,事件句柄,事件源,事件对象(简要学习笔记十八)

    1.事件冒泡与事件捕获 2.事件与事件句柄   3.事件委托:利用事件的冒泡技术.子元素的事件最终会冒泡到父元素直到跟节点.事件监听会分析从子元素冒泡上来的事件. 事件委托的好处:     1.每个函 ...

  7. python 学习笔记十八 django深入学习三 分页,自定义标签,权限机制

    django  Pagination(分页) django 自带的分页功能非常强大,我们来看一个简单的练习示例: #导入Paginator>>> from django.core.p ...

  8. SharpGL学习笔记(十八) 解析3ds模型并显示

    笔者设想的3D仿真中的元件,是不可能都是“画”出来的.这样就玩复杂了,应该把任务分包出去,让善于制作模型的软件来制作三维模型,我们只需要解析并且显示它即可. 3dsmax制作三维模型的方便,快捷,专业 ...

  9. PHP学习笔记十八【构造函数】

    <?php class Person{ public $name; public $age; //定义构造函数 function 空格__construct 构造方法没有返回值,对象自动调用 p ...

  10. Python3学习笔记十八

    1.    MTV M:   model     与数据库相关 T:   Template    与html相关 V:   views      与逻辑相关 一.    URL配置 启动:python ...

随机推荐

  1. AW笔记本升级SSD,外接双屏中的一些注意事项

    自己留一个mark,以后提醒用. 1)机械硬盘状态下利用alien sprawn创建的系统恢复U盘,无法在SSD下使用,由于SSD中没有recovery分区,仅仅能使用随机携带的系统恢复光盘: 2)最 ...

  2. Nginx 进程间通信

    Linux下的IPC非常多,nginx的进程都是有亲缘关系的进程,对于他们的通信我们选择TCP socket进行通信.   TCP socket 用来做进程通信的优点有,   1.socket是文件描 ...

  3. curl的封装

    首先要搭建一个httpserver,这里採用tomcat6为例: 过程:新建一个Servlet,并使用tomcat的默认port号8080监听,最后写一个jsp来測试能否够訪问该server 1)新建 ...

  4. RequireJS和JQuery的模块化编程

    基于RequireJS和JQuery的模块化编程 由于js的代码逻辑越来越重,一个js文件可能会有上千行,十分不利于开发与维护.最近正在把逻辑很重的js拆分成模块,在一顿纠结是使用requirejs还 ...

  5. Cocos2d-x精华教程汇总(第三期) cocos2d-x最新离线API文档下载(最新版3.6更新。。。)

    其实使用doxygen在Cocos2d-x引擎的doc目录下可以生成离线文档,但是可能每个人为了生成一个离线文档去安装甚至编译doxygen毕竟麻烦,而且现有的doxygen无法生成多语言版本的离线文 ...

  6. TF卡分区

    http://bbs.gfan.com/android-5176910-1-1.html http://www.miui.com/thread-2302600-1-1.html http://www. ...

  7. Objective-c 中的算术函数和常数的表示

    常数 常数名 说明 M_PI 圆周率(=π) M_PI_2 圆周率的1/2(=π/2) M_PI_4 圆周率的1/4(=π/4) M_1_PI =1/π M_2_PI =2/π M_E =e M_LO ...

  8. Android 访问Android Wear数据层Api——同步Data Items

    Data Items它被用来同步手机和wear数据接口,一个Date Items通常包含以下几个部分: Payload 字节数组.无论你需要设置数据类型,我们同意对象序列化和反序列化,大小不能超过10 ...

  9. TestThreadPoolExecutor.java

    package           ; import java.io.IOException;import java.io.InputStream;import java.util.List;impo ...

  10. HUNNU11351:Pythagoras's Revenge

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11351&courseid=0 Problem des ...