本文由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. [Android学习笔记]子线程更新UI线程方法之Handler

    关于此笔记 不讨论: 1.不讨论Handler实现细节 2.不讨论android线程派发细节 讨论: 子线程如何简单的使用Handler更新UI 问题: android开发时,如何在子线程更新UI? ...

  2. Selenium来抓取动态加载的页面

    一般的爬虫都是直接使用http协议,下载指定url的html内容,并对内容进行分析和抽取.在我写的爬虫框架webmagic里也使用了HttpClient来完成这样的任务. 但是有些页面是通过js以及a ...

  3. linux通过使用mail发送电子邮件

    通过外部方法发送的电子邮件 bin/mail默认为本地sendmail发送电子邮件,求本地的机器必须安装和启动Sendmail服务.配置很麻烦,并且会带来不必要的 资源占用.而通过改动配置文件能够使用 ...

  4. VSTO学习笔记(九)浅谈Excel内容比较

    原文:VSTO学习笔记(九)浅谈Excel内容比较 说起文件内容比较,或许我们首先想到的是UltraCompare这类专业比较的软件,其功能非常强大,能够对基于文本的文件内容作出快速.准确的比较,有详 ...

  5. linux上svn连接visual svn server时ssl鉴权失败,问题解决(转)

    场景:1.在windows 7上安装了visual svn server作为自己的svn服务器. 2.在虚拟机centos 6.3上使用svn客户端check代码,报错: [plain] view p ...

  6. hdu4586(概率、期望)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4586 题意:有一个色子,n面,每面有个分值a[i],其中有m面比较特殊,当该面出现时,可以再投一次.求 ...

  7. premake 使用clang替换gcc

    接着前文:premake在Ubuntu和GCC环境下创建简单的C++工程 由于clang支持gcc所有参数,所以使得在premake中替换gcc变得很简单.基本上就是通过传递参数或者设置环境变量的方式 ...

  8. HDU 3639 Hawk-and-Chicken(良好的沟通)

    HDU 3639 Hawk-and-Chicken 题目链接 题意:就是在一个有向图上,满足传递关系,比方a->b, b->c,那么c能够得到2的支持,问得到支持最大的是谁,而且输出这些人 ...

  9. Spark技术内幕:Stage划分及提交源代码分析

    当触发一个RDD的action后.以count为例,调用关系例如以下: org.apache.spark.rdd.RDD#count org.apache.spark.SparkContext#run ...

  10. Codeforces 549G. Happy Line 馋

    非常有趣的贪婪: Let's reformulate the condition in terms of a certain height the towers, which will be on t ...