本文由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. java之jvm学习笔记二(类装载器的体系结构)

    java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...

  2. POJ2828 Buy Tickets 【线段树】+【单点更新】+【逆序】

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12296   Accepted: 6071 Desc ...

  3. nginx 源码安装

    安装环境: 操作系统:Ubuntu 12.04 Nginx:     V1.4.2 PCRE:    V8.33 zlib:         V1.2.8 下载上述源包到当前用户主目录(本机:/hom ...

  4. struts2文件上传限制大小问题

    struts2默认文件上传大小为2M,如需改动默认大小,解决方法例如以下: <struts> <constant name="struts.multipart.maxSiz ...

  5. hdu3790最短路径问题 (用优先队列实现的)

    Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的.   Inp ...

  6. Hadoop-2.2.0中国文献—— MapReduce 下一代 -- 公平调度

    目的 此文档描写叙述了 FairScheduler, Hadoop 的一个可插入式的调度器.同意 YARN 应用在一个大集群中公平地共享资源. 简单介绍 公平调度是一种分配资源给应用的方法,以致到最后 ...

  7. JVM学习03_new对象的内存图讲解,以及引出static方法(转)

    目录 -=-讲解对象创建过程中,-=-堆内存和栈内存的情况 -=-构造函数对类对象的成员变量的初始化过程 -=-构造函数出栈 -=-类的方法在不访问类对象的成员变量时造成的内存资源浪费怎么解决? -= ...

  8. HDU 4454 - Stealing a Cake(三分)

    我比较快速的想到了三分,但是我是从0到2*pi区间进行三分,并且漏了一种点到边距离的情况,一直WA了好几次 后来画了下图才发现,0到2*pi区间内是有两个极值的,每个半圆存在一个极值 以下是代码 #i ...

  9. Codeforces 549G. Happy Line 馋

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

  10. MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录

    分组排序查询最大记录 //对 "catagory" 不等于 null 的数据进行分组查询,且查询结果倒序 db.getCollection('userAccount').aggre ...