HLSL编程实现PhotoShop滤镜效果

文章为it168稿件.地址为
http://tech.it168.com/n/2007-03-29/200703291522292.shtml

Direct3D提高篇之:HLSL编程实现PhotoShop滤镜效果

潘李亮 <chsdate w:st="on" year="2007" month="3" day="16" islunardate="False" isrocdate="False">2007-3-16</chsdate>

xheartblue@163.com

关于学习,中国有句古话叫“学以致用”,可见把学到的东西用于实际实践中是多么的重要,现在学习Direct3D/HLSL的人非常多,教程也非常多。但是很多人不知道看完这些教程后该干什么,或者说可以怎么利用学到的知识,本文针对已经学习过Direct3D/HLSL的初学者,讲述如果将HLSL用于数字图像处理,带领大家一起体会HLSL的强大。

本文会对Direct3D/HLSL做一个简单的介绍,但是假设读者已经了解和掌握了Direct3D/HLSL的基本知识。

简介.

1)Direct3DHLSL

众所周知,Direct3D是微软开发的用于编写Windows下高性能图形程序的3D API。通过Direct3D,我们可以访问高速的图形加速卡。它是DirectX众多成员的一部分。

HLSL 全称High Level Shading Language . MS推出Direct3D 9时的一个重要更新。所谓的Shading Language还需要从Direct3D的图形管道说起,Direct3DDirect3D 8以前只能工作在固定管道(Fixed Function Pipe-line)的模式下,在固定管道模式下,图元从提交到被转化成可以显示的像素是按照实现定义好的流程和算法来完成,可以认为是固化在硬件中的死功能。

Direct3D 8开始,微软在Direct3D中引入了可编程管道(Programable Function Pipeline)的概念,在可编程管道中,开发人员可以自己编写用于处理顶点和像素的程序,这些程序是运行在GPU上而不是CPU上的。在Direct3D里面,用于处理顶点的程序叫Vertex Shader,用于处理像素的叫Pixel Shader。(目前最新的Direct3D10中又引入了Geometry Shader的概念)。因为硬件的水平在进步,所以可编程管道的处理能力也在不断的提高,根据不同的硬件能力,Shader的版本也已经有对应的不同版本。从Direct3D发布的最早的Shader Model 1.0到现在主流的Shader Model 3.0,可编程管道已经能提供一点范围的通用编程能力了,这就是所谓的GPGPU

从名字上可知,HLSL是一种高级语言(High Level),那么必然有与之对应的Low Level Shading Language,这个低级的语言就是ASMShader。它是类似于汇编语言,难以编写和维护,而HLSL则跟我们熟悉的C/C++语言非常类似。大大降低了开发人员学习的成本。HLSL本身就是微软和nVidia联合开发的,nVidia的版本称为Cg,也就是C for Graphics。可想而知,它和C是有同样的血统的。

本文不是Direct3DHLSL的教程,如果读者觉得以上的概念还比较陌生,请先学习Direct3D的基础知识。同时关于如何在Direct3D应用程序中使用HLSL编写的Vertex ShaderPixel Shader,请参阅其它的教程和微软的DirectX SDK

2RenderMonkey简介

现在的开发人员可能都比较熟悉IDE的工作模式,尤其是使用Visual Studio一类开发工具的Windows程序开发人员。在一个统一的开发环境中,可以编写和调试程序。HLSL作为一种新的语言,GPU编程作为一种新事物,目前还没有很好的IDE能完整的支持编写,调试一体化的工作方式。在本文我们将使用ATI的一个相对比较好用的开发HLSLIDE: RenderMonkey

RenderMonkey是由前ATI开发的,用于编写Shader,并调试Shader的一个工具。由于RenderMonkey支持插件,所以RenderMonkey既可以编写OpenGLGLSL也可以编写Direct3DHLSL。它能支持创建RenderTarget,多Pass渲染,可以自由选择用哪个shader model来编译代码。并能加亮显示shader代码。

经典的RenderMonkey界面如下图

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 414.75pt; HEIGHT: 372pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CUsers%5CSTANLY~1%5CAppData%5CLocal%5CTemp%5Cmsohtml1%5C04%5Cclip_image001.png"></imagedata></shape>

左边为工作区,右边为预览区域。下面为信息输出区。在左边的工作区里可以看到。我们可以对Shader的工程进行分组,其中每一个可以独立工作的工程称为一个Effect。在同一时候预览区中只能预览当前激活的Effect。每个Effect由不同的对象组成,其中比较重要的对象如下:

1) Pass . 这个pass就是渲染中常提到的pass.代表一遍的渲染

2) 几何体。就是类红色茶壶表示的,它代表在渲染中使用的几何体。

3) 纹理对象和RenderTarget对象(用一个铅笔表示)

4) Shader中用到的参数,这些参数可以是自定义的,也可以是预定义的(比如当前的观察矩阵,摄像机的位置等参数)。

5) 每个pass中用到的Shader。这些shader可以在RenderMonkey的代码编辑器中进行编辑,并调用命令来编译。

因为文章篇幅的关系,也不采用编写Direct3D程序加载HLSL的方式来做演示程序,而是直接使用RenderMonkey来作为演示的平台。关于如何使用RenderMonkey,请参照RenderMonkey的帮助,或者打开RenderMonkey自带的例子,很容易就能掌握这个工具的使用方法。

GPGPU

本文将要介绍的是如何用HLSL来实现PhotoShop的滤镜效果,也就是说需要通过GPU来进行数字图像处理。这是目前很流行的GPGPU的应用的一种。

我们知道,GPUCPU的工作方式和用途都是不同的,CPU是通用的处理器,而GPU是专用于处理3D图形显示的,因此CPU的指令集更加丰富,而GPU的指令集更加有针对性,因此这就决定了GPU在牺牲了CPU的灵活性的前提上有更快的运行速度。GPU特别适合处理那种可以大规模并行的算法,比如某些数字图像处理算法。

因为目前我们的程序只能通过Direct3DAPI才能访问到GPU,一般我们采用Pixel Shader来进行GPGPU,所以我们要使用GPU来处理数据的时候,必须完成以下几件事:

1).将数据提交给GPU

2).调用对应命令让GPU开始处理数据

3).GPU哪里取回处理完毕的数据。

我们可以通过两种方法将数据提交到GPU,纹理和shader的参数,纹理中一般保存我们需要进行处理的数据,而shader参数则一般是用于数据处理算法需要用到的一些参数。当然这也不是绝对的。

当数据已经准备完毕后,我们调用Direct3DdrawPrimitive函数在屏幕上绘制一个纹理相同大小的矩形,把GPGPU的算法写到用于绘制这个矩形的Pixel Shader中。当Direct3D开始绘制这个矩形以后,会为每一个象素调用一次整个Pixel Shader,然后把Pixel Shader的输出写入到RenderTarget中,因为我们绘制的矩形的大小和纹理的大小是一致的,所以输出象素和纹理的象素可以做到一一对应的关系,也就是说纹理中的每一个象素在经过Pixel Shader的运算后被输出掉RenderTarget里,等于对这个数据调用了一次我们需要的算法。我们知道现代的GPU中往往有大量的Pixel Shader处理单元,而这些处理都是可以并行运行的,可想而知,这个处理是非常快速的。

经过前面的步骤,处理完的数据已经到了RenderTarget里了,我们可以事先自己创建一个RenderTarget(通常和输入纹理等大)来接受步骤2中的数据,然后Lock这个RenderTarget取回数据。也可以在步骤二中直接把图象绘制到屏幕上,通过Capture屏幕来得到输出(对于图象处理也够了,就是速度慢,而且显得非常傻)。

GPGPU简单介绍到这里。详细的GPGPU资料请参考www.gpgpu.org。 同时nvidia的网站和发布的SDK上也有很多关于GPGPU的例子。

接下来我们使用RenderMonkey来搭建一个用于数字图像处理的架子,以实现类似PhotoShop的滤镜效果

RenderMonkey图像处理的架子-图像黑白化

下面我们通过一个简单的例子,先来完成一个最简单的图像处理-把图像黑白化。来说明RenderMonkey如何来处理数字图像。

Render MonkeyVC类似,内置了一些工程代码。在这里我们在RenderMonkey的工作区菜单里选择Add Effect -> DirectX->Screen-AlignedQuad. 在生成的工程中,我们看到RenderMonkey为我们显示了一个默认的图片,首先我们就是要修改这个图片,我们双击那个base图片对应的节点,选择一个我们要演示的图片。如下图。

<shape id="_x0000_i1026" style="WIDTH: 415.5pt; HEIGHT: 351.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CUsers%5CSTANLY~1%5CAppData%5CLocal%5CTemp%5Cmsohtml1%5C04%5Cclip_image003.png"><font size="3"></font></imagedata></shape>

接下来,我们要开始进行我们关键的一步,编写处理图像的算法,我们双击刚才建立的项目中的single pass -> pixel shader . 开始编辑Pixel Shader的代码。

我们知道,一个RGB颜色的亮度和各个分量之间的关系的公式为:

GrayValue = 0.3 * R + 0.59*G + 0.11 *B

根据这个公式,我们的代码如下:

<shapetype id="_x0000_t202" path="m,l,21600r21600,l21600,xe" o:spt="202" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><path o:connecttype="rect" gradientshapeok="t"></path></shapetype><shape id="_x0000_s1026" style="MARGIN-TOP: 5.45pt; Z-INDEX: 1; LEFT: 0px; MARGIN-LEFT: 23.35pt; WIDTH: 403pt; TEXT-INDENT: 0px; POSITION: absolute; HEIGHT: 134.95pt; TEXT-ALIGN: left" type="#_x0000_t202" fillcolor="#fde9d9"><fill type="gradient" focus="100%" rotate="t" color2="#fde9d9"></fill><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #f0f0f0; BORDER-TOP: #f0f0f0; BORDER-LEFT: #f0f0f0; BORDER-BOTTOM: #f0f0f0; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">sampler2D</span></strong><span lang="EN-US"> Texture0;</span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">float4</span></strong><span lang="EN-US"> <span style="mso-spacerun: yes"></span>main( <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong> texCoord<span style="mso-spacerun: yes"> </span>: <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">TEXCOORD0</span></strong> ) : <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">COLOR</span></strong></span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font face="Calibri" size="3">{</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong> _inColor = <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">tex2D</span></strong>( Texture0, texCoord );</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float</span></strong> h = 0.3 * _inColor.x + 0.59 * _inColor.y + 0.11* _inColor.z;</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong> _outColor = <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong>(h,h,h,1.0);</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">return</span></strong> _outColor;</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font face="Calibri" size="3">}</font></span></p> </div> </td> </tr></tbody></table></textbox></shape>


我来详细的解释一下这个Pixel Shader,首先我们定义的的sampler2D Texture0Texture0就是代表我们输入的图像。这个图像在RenderMonkey的工作区中用两部分表示,首先需要在工作区中创建一个图像对象bas,然后需要在用到这个纹理图像的pass中创建一个纹理对象Texture0,然后让这个Texture0指向我们刚才创建的纹理图像bas,读者应该注意到了纹理对象的名字就是我们Shader里的sampler2D变量的名字,不错,RenderMonkey就是以这种方法把shader代码中的变量名字和工作区中的对象关联起来.不光纹理如此,其它的float4/float3/float2/float变量都如此.

接下来的main函数中,我们通过纹理采样的方式得到当前需要绘制的像素,float4 _inColor = tex2D( Texture0, texCoord ); 也就是输入的颜色。得到输入颜色后,我们可以通过上面给出的公式来计算出这个颜色的灰度值,并用这个值构造一个灰度颜色返回给Direct3D。系统就会把这个颜色作为最终的色彩显示在窗口中,也就是得到一个黑白的图像。最终结果如下图:

<shape id="_x0000_i1027" style="WIDTH: 208.5pt; HEIGHT: 246pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CUsers%5CSTANLY~1%5CAppData%5CLocal%5CTemp%5Cmsohtml1%5C04%5Cclip_image006.png"><font size="3"></font></imagedata></shape>

图:图像去色效果

(注:这个例子是最简单的HLSL用于图像处理的例子,如果读者觉得到目前为止还很有难度,建议重新温习一遍Direct3DHLSL的知识)。

通过这个例子,我们已经基本了解了RenderMonkey处理图像的步骤和流程,下面我们通过分析一些更加复杂一点的例子来体会HLSL的强大能力

l 入门效果之浮雕

"浮雕"图象效果是指图像的前景前向凸出背景。常见于一些纪念碑的雕刻上,要实现浮雕其实非常简单。我们把图象的一个象素和左上方的象素进行求差运算,并加上一个灰度。这个灰度就是表示背景颜色。这里我们设置这个插值为128 (图象RGB的值是0-255)。同时,我们还应该把这两个颜色的差值转换为亮度信息.否则浮雕图像会出现彩色J

在使用HLSL处理浮雕效果的时候,两个问题我们需要注意一下。

其中一个图象边界,写过C++实现浮雕效果的朋友都知道,在处理边界象素的时候可能是取不到左上角象素的, 这个时候就应该左做特殊处理,通常我们把边界位置的浮雕结果设置成背景颜色,但是使用HLSL的时候我们不需要在HLSLshader中去对图象的边界做特殊处理,但是我们需要对纹理设置滤波器, 这个滤波器我们设置为CLAMP模式就可以了.

第二个需要处理的问题是, 我们知道PixelShader,纹理的采样坐标是0-1.0, 如果我们要取到左上我们需要知道纹理图象的大小,这样才能把一个象素的的偏移转换成0-1.0的值是多少, 假设纹理的大小是[w,h],当前纹理坐标是[u,v],那么它左上角的象素的纹理坐标就是[u -1.0/w, v – 1.0/h].RenderMonkey中无法知道这个纹理图像的大小,当然如果我们自己用VC++写一个程序的话,我们可以在加载图像或者从IDirect3DTexture9对象中得到纹理大小.然后当作一个constant常量设置给HLSL就可以了. 当然也可以偷懒假设纹理的大小就是1024 x 1024—得到的效果也是可以接受的.

好了,现在我来展示一下用来得到浮雕效果的HLSL的代码:

<shape id="_x0000_s1027" style="MARGIN-TOP: 7.8pt; Z-INDEX: 2; LEFT: 0px; MARGIN-LEFT: 0px; WIDTH: 450pt; TEXT-INDENT: 0px; POSITION: absolute; HEIGHT: 241.8pt; TEXT-ALIGN: left" type="#_x0000_t202" fillcolor="#fde9d9"><fill type="gradient" focus="100%" rotate="t" color2="#fde9d9"></fill><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #f0f0f0; BORDER-TOP: #f0f0f0; BORDER-LEFT: #f0f0f0; BORDER-BOTTOM: #f0f0f0; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">sampler2D </span></strong><span lang="EN-US">Texture0;<strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd"><p></p></span></strong></span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">float2<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"></span><span style="mso-spacerun: yes"></span></span></strong><span lang="EN-US">TexSize;</span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">float4</span></strong><span lang="EN-US"> main( <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong> texCoord<span style="mso-spacerun: yes"> </span>: <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">TEXCOORD0</span></strong> ) : <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">COLOR</span></strong></span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font face="Calibri" size="3">{</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong><span style="mso-spacerun: yes"> </span>upLeftUV = <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong>(texCoord.x - 1.0/TexSize.x , texCoord.y - 1.0/TexSize.y);</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong> <span style="mso-spacerun: yes"></span>bkColor = <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong>(0.5 , 0.5 , 0.5 , 1.0);</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong> <span style="mso-spacerun: yes"></span>curColor<span style="mso-spacerun: yes"> </span>=<span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">tex2D</span></strong>( Texture0, texCoord );</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong> <span style="mso-spacerun: yes"></span>upLeftColor =<span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">tex2D</span></strong>( Texture0, upLeftUV );</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><span lang="EN-US"><font face="Calibri"><span style="mso-spacerun: yes"> </span>//</font></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">相减得到颜色的差</span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong> <span style="mso-spacerun: yes"></span>delColor = curColor - upLeftColor;</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><span lang="EN-US"><font face="Calibri"><span style="mso-spacerun: yes"> </span>//</font></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">需要把这个颜色的差设置</span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float</span></strong> <span style="mso-spacerun: yes"></span>h = 0.3 * delColor.x + 0.59 * delColor.y + 0.11* delColor.z;</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong> <span style="mso-spacerun: yes"></span>_outColor =<span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float4</span></strong>(h,h,h,0.0)+ bkColor;</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">return</span></strong> <span style="mso-spacerun: yes"></span>_outColor;</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri">}<strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd"><p></p></span></strong></font></font></span></p> </div> </td> </tr></tbody></table></textbox></shape>


<shape id="_x0000_i1028" style="WIDTH: 174.75pt; HEIGHT: 229.5pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CUsers%5CSTANLY~1%5CAppData%5CLocal%5CTemp%5Cmsohtml1%5C04%5Cclip_image009.png"></imagedata></shape> <shape id="_x0000_i1029" style="WIDTH: 161.25pt; HEIGHT: 227.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:%5CUsers%5CSTANLY~1%5CAppData%5CLocal%5CTemp%5Cmsohtml1%5C04%5Cclip_image011.png"></imagedata></shape>

原图 浮雕化后的图像

读者应该会发现,相对于C++版本的代码,HLSL的代码显得非常的干净和利索.没有分支,没有循环.最重要的是它的速度非常快,对一个2048x2048的图像完全可以做到>30fps的实时处理能力而不会耗费很多的CPU时间!

l 入门效果之马赛克

接下来我们完成一个更加常见的效果马赛克.图片的马赛克就是把图片的一个相当大小的区域用同一个点的颜色来表示.可以认为是大规模的降低图像的分辨率,而让图像的一些细节隐藏起来, 比如电视中要秀一下某个罪犯的身材,却又不能展示他的脸,这个时候我们就可以给他的脸加一个马赛克.

HLSL代码实现马赛克是非常简单的,但是同样的,我们需要一些额外的步骤,第一步就是先把纹理坐标转换成图像实际大小的整数坐标.接下来,我们要把图像这个坐标量化---比如马赛克块的大小是8x8象素。那么我们可以用下列方法来得到马赛克后的图像采样值,假设[x.y]为图像的整数坐标:

[x,y]mosaic = [ int(x/8)*8 , int(y/8)*8].

得到这个坐标后,我们只要用相反的方法,把整数坐标转换回到0-1.0的纹理坐标。

具体的马赛克效果代码如下:

<shape id="_x0000_s1028" style="MARGIN-TOP: 7.8pt; Z-INDEX: 3; LEFT: 0px; MARGIN-LEFT: 18pt; WIDTH: 392.25pt; TEXT-INDENT: 0px; POSITION: absolute; HEIGHT: 241.8pt; TEXT-ALIGN: left" type="#_x0000_t202" fillcolor="#fde9d9"><fill type="gradient" focus="100%" rotate="t" color2="#fde9d9"></fill><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #f0f0f0; BORDER-TOP: #f0f0f0; BORDER-LEFT: #f0f0f0; BORDER-BOTTOM: #f0f0f0; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">sampler2D </span></strong><span lang="EN-US">Texture0;<strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd"><p></p></span></strong></span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">float2</span></strong><span lang="EN-US"><span style="mso-spacerun: yes"> </span>TexSize;</span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">float2</span></strong><span lang="EN-US"><span style="mso-spacerun: yes"> </span>mosaicSize = <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong>(8,8);</span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">float4</span></strong><span lang="EN-US"> main( <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong> texCoord<span style="mso-spacerun: yes"> </span>: <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">TEXCOORD0</span></strong> ) : <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">COLOR</span></strong></span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font face="Calibri" size="3">{</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><span lang="EN-US"><font face="Calibri"><span style="mso-spacerun: yes"> </span>//</font></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">得到当前纹理坐标相对图像大小整数值。</span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong><span style="mso-spacerun: yes"> </span>intXY = float2(texCoord.x * TexSize.x , texCoord.y * TexSize.y);</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><span lang="EN-US"><font face="Calibri"><span style="mso-spacerun: yes"> </span>//</font></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">根据马赛克块大小进行取整。</span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong><span style="mso-spacerun: yes"> </span>XYMosaic<span style="mso-spacerun: yes"> </span>= <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong>(<strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">int</span></strong>(intXY.x/mosaicSize.x) * mosaicSize.x,</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">int</span></strong>(intXY.y/mosaicSize.y) * mosaicSize.y );</font></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><font size="3"><span lang="EN-US"><font face="Calibri"><span style="mso-spacerun: yes"> </span>//</font></span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri">把整数坐标转换回纹理采样坐标</span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15.5pt; mso-char-indent-count: 1.47"><font size="3"><font face="Calibri"><strong style="mso-bidi-font-weight: normal"><span lang="EN-US" style="COLOR: #4f81bd">float2</span></strong><span lang="EN-US"><span style="mso-spacerun: yes"> </span>UVMosaic<span style="mso-spacerun: yes"> </span>= <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">float2</span></strong>(XYMosaic.x/TexSize.x , XYMosaic.y/TexSize.y);</span></font></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><font size="3"><font face="Calibri"><span style="mso-spacerun: yes"> </span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">return</span></strong> <strong style="mso-bidi-font-weight: normal"><span style="COLOR: #4f81bd">tex2D</span></strong>( Texture0, UVMosaic );</font></font></span></p> <p class="MsoNormal"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值