D3D9 Samples(12)--CompiledEffect

来源:互联网 发布:淘宝定制商品是什么 编辑:程序博客网 时间:2024/06/05 04:40

 

 

 

D3D9 Samples(12)--CompiledEffect

 

 

 

         打开CompiledEffect项目。编译运行:

 

 

        这个例子演示使用编译好的effect文件。Shader/Effect的编译分为在线编译(Runtime compile)和离线编译(Offline compile)。这个例子展示离线编译,也叫生成时编译(Build-time compile)。就是不调用D3DXCompileShader等编译shader/effect文件,而是先用fxc.exe先编译二进制文件,然后读入内存中,用这个内存创建shader/effect对象。

 

 

1. 检查硬件是否满足条件

 

IsDeviceAcceptable

要求PS必须是2.0以上。

D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING

要求支持像素混合(包括alpha测试、像素雾、渲染目标混合等)

 

 

 

2. 离线编译

 

打开CompiledEffect.fx的属性,在Custom Build Step -> General,在Command Line中看到用fxc编译的命令行:

 fxc.exe "$(InputPath)" /nologo /Zi /Tfx_2_0 /Fo"CompiledEffect.fxo"

注意这里需要把fxc.exe文件拷贝到C:\Program Files\Microsoft Visual Studio 9.0\VC\bin,或者指定到DXSDK安装目录的fxc.exe。

    常用的命令行参数:

/T  指定profile。比如vs_3_0、ps_3_0、fx_4_0等。

/E  指定Entry point入口函数的名称。比如一个shader文件中的VS入口函数叫VSMain,那么这里就设成 /E "VSMain"

/Fo 指定输出文件。

/Zi 启用调试信息。便于在PIX中调试Shader。

 

其他参数可查MSDN文档:

Effect-Compiler Tool-Syntax

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509709(v=vs.85).aspx#Profiles

 

 

 

3. 创建Shader/Effect

 

上面已经用fxc.exe把shader或effect编译成了二进制目标文件,可以直接被读入内存,生成shader或effect。

D3DXCreateEffectFromFile

 

 

 

4. Shader代码分析

 

4.1 全局变量

 

材质颜色和贴图:

float4 g_MaterialAmbientColor;   // Material's ambient color

float4 g_MaterialDiffuseColor;   // Material's diffuse color

texture g_MeshTexture;           // Color texture for mesh

 

灯光:

float3 g_LightDir = normalize(float3(1.0f, 1.0f, -1.0f));    // Light's direction

float4 g_LightAmbient = { 0.2f, 0.2f, 0.2f, 0.2f };          // Light's ambient color

float4 g_LightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };          // Light's diffuse color

 

空间矩阵:

float4x4 g_mWorld;                   // World matrix for object

float4x4 g_mWorldViewProjection; // World * View * Projection matrix

 

程序运行时间:

float    g_fTime;                   // App's time in seconds

 

纹理采样器:

第一个RenderTargetSampler未使用

 

sampler MeshTextureSampler =

sampler_state

{

    Texture = <g_MeshTexture>;   

    MipFilter = LINEAR;

    MinFilter = LINEAR;

    MagFilter = LINEAR;

};

 

 

4.2 Vertex Shader (RenderSceneVS)

 

struct VS_OUTPUT

{

    float4 Position   : POSITION;   // vertex position

    float4 Diffuse    : COLOR0;     // vertex diffuse color

    float2 TextureUV  : TEXCOORD0;  // vertex texture coords

};

 

函数输出:这次是定义了一个结构,包括顶点位置坐标、顶点漫反射颜色、顶点纹理坐标。

 

函数输入:也可以采用输出的结构模式,这里采用的是形参模式。

 

函数逻辑:

A. 将模型顶点坐标用正弦余弦增加一个变化量:

float4 vAnimatedPos = vPos;

vAnimatedPos.x *= (1.0 + cos(g_fTime)/10);

vAnimatedPos.y *= (1.0 + sin(g_fTime)/10);

vAnimatedPos.z *= (1.0 + cos(g_fTime)/10);

 

 

B. 将模型顶点坐标转换到观察空间:

Output.Position = mul(vAnimatedPos, g_mWorldViewProjection);

 

 

C. 把传入的顶点法线向量转换到观察空间:

vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld));

 

 

D. 计算光照得到每个顶点最终颜色值:

Output.Diffuse.rgb = g_MaterialDiffuseColor * g_LightDiffuse * max(0,dot(vNormalWorldSpace, g_LightDir)) + g_MaterialAmbientColor * g_LightAmbient;  

Output.Diffuse.a = 1.0f;

 

 

4.3 Pixel Shader (RenderScenePS)

 

将VS中通过光照计算等得出的颜色值和纹理采样混合:

Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;

 

4.4 technique

 

以vs_2_0/ps_2_0的配置编译shader。

 

 

 

 

参考资料

 

http://msdn.microsoft.com/en-us/library/windows/desktop/bb232919(v=vs.85).aspx

 

 

原创粉丝点击