转:http://blog.csdn.net/leonwei/article/details/8212800

effect其实整合了shader和render state的控制两大部分内容

9.1 effect文件基本框架

part1 :shader state包括全局变量 shader数据结构定义,shader的实现

part2 :texture and sampler state,这个通常是全局变量,用于shader中,通常要定义在shader的实现之前

part3 其他的render state设置,写在technique的pass里,这才是effect文件的主入口

effet中任何一个变量后面都可以接一个语意:XXX

1.可以使用的变量类型

Data : Buffer Scalar Vector Matrix Sample Shader Texture Struct UserDefined

9.1.1参数类型

hlsl中使用的参数有两种:Uniform和 varying

Uniform存储在常量缓存中的,是对shader的每一次调用值都是一样的,可以在D3D中访问的 前面可以加uniform(可不加),通常是全局的

varying:只对一次调用起作用的,是来自vertext stream中的,通常要有语意的修饰,通产是写在shader的输入输出参数里的

9.1.2 sampler纹理采样器

要包含三个信息

例如定义这样一个sampler

 
sampler s = sampler_state 
{ 
  texture = NULL; 
  mipfilter = LINEAR; 
};
 
就可以在shader中使用
float2 sample_2D(float2 tex : TEXCOORD0) : COLOR
{
  return tex2D(s, tex);
}
来采样纹理
9.1.3 shader的函数体

函数通常要包含这样几部分:

float4 VertexShader_Tutorial_1(float4 inPos : POSITION ) : POSITION
{
   ...
}
struct VS_OUTPUT
{
    float4  vPosition        : POSITION;
    float4  vDiffuse         : COLOR;
}; 
 
VS_OUTPUT VertexShader_Tutorial_1(float4 inPos : POSITION )
{
   ...
}

9.2D3D9中使用effect

1.首先从effect file创建effect(编译),使用函数

D3DXCreateEffectFromFile( 
        pd3dDevice, 
        str, 
        NULL, // CONST D3DXMACRO* pDefines,
        NULL, // LPD3DXINCLUDE pInclude,
        dwShaderFlags, 
               NULL, // LPD3DXEFFECTPOOL pPool,
        &g_pEffect, 
        NULL );
 

dwShaderFlags

  • Skipping validation, 如果已知这是没有问题的 那么就跳过检查
  • Skipping optimization 有时开启优化会导致debug 困难,就可以跳过优化
  • Requesting debug information to be included in the shader so it can be debugged.

这个函数的调用实际上是在从文件编译出一个effect

你还可以利用工具fxc.exe离线的编译出一个effect,他会直接编出一个汇编的shader指令,这样在程序中就不用编译了。发布产品时这样做应该更好。

2.调用一个effect来render

ID3DXEffect::Begin set technique

ID3DXEffect::BeginPass set pass

ID3DXEffect::CommitChanges提交对当前pass的各种参数修改,要在draw之前调用

ID3DXEffect::EndPass

ID3DXEffect::End

一个典型调用

3  effect 的参数设置(指effect文件中定义的全局变量(uniform))

设置与获取

m_pEffect->SetValue

m_pEffect->GetValue

函数的第一个参数都是变量的名字

 
不同的effect 可以共享参数:如果一个effect变量定义为shared,并且多个effect创建的时候使用同一个pool(D3DXCreateEffectFromFile的第六个参数)就可以共享这个参数。
 
创建参数block,可以像使用render state的block那样 创建parament state,这时是通过创建Parameter Blocks的方式。把effect 的参数设置操作写在
m_pEffect->BeginParameterBlock();与m_pEffect->EndParameterBlock();
之间。得到的block可以用在effect 上

4 用户注释

用户可以在effect pass 或者某个变量上附加一个用户的注释。他其实就是一个string或numeric格式的变量。格式

 
texture Tex0 < string name = "tiger.bmp"; >;意味着给tex0 附加一个注释,告诉这个texture使用的纹理文件的路径。注释可以理解为用户在D3D层面去扩充effect 文件的定义。

ID3DXBaseEffect::GetAnnotation orID3DXBaseEffect::GetAnnotationByName可以获取

5 preshader技术

对于shader中的常量计算,D3D会将其提取出来,在shader执行之前,先由CPU计算,这样会提高GPU的效率,防止每个顶点或pixcel都重复这个同样的运算,使不使用preshader可一在编译时指定优化的flag,关闭使用参数D3DXSHADER_NO_PRESHADER。所以基于这个技术我们可以在shader中做常量矩阵运算,不用担心这个运算会在每个顶点处做一次。

9.3 语法备忘

9.3.1 语义

VS:

输入:

BINORMAL[n]
Binormal
float4

BLENDINDICES[n]
Blend indices
uint

BLENDWEIGHT[n]
Blend weights
float

COLOR[n]
Diffuse and specular color
float4

NORMAL[n]
Normal vector
float4

POSITION[n]
Vertex position in object space.
float4

POSITIONT
Transformed vertex position.
float4

PSIZE[n]
Point size
float

TANGENT[n]
Tangent
float4

TEXCOORD[n]

输出

COLOR[n]
Diffuse or specular color
float4

FOG
Vertex fog
float

POSITION[n]
Position of a vertex in homogenous space. Compute position in screen-space by dividing (x,y,z) by w. Every vertex shader must write out a parameter with this semantic.
float4

PSIZE
Point size
float

TESSFACTOR[n]
Tessellation factor
float

TEXCOORD[n]
Texture coordinates
float4

PS

输入:

COLOR[n]
Diffuse or specular color.
float4

TEXCOORD[n]
Texture coordinates
float4

VFACE
Floating-point scalar that indicates a back-facing primitive. A negative value faces backwards, while a positive value faces the camera.
float

VPOS
The pixel location (x,y) in screen space. To convert a Direct3D 9 shader (that uses this semantic) to a Direct3D 10 shader, seeDirect3D 9 VPOS and Direct3D 10 SV_Position)
float2

输出:

COLOR[n]
Output color
float4

DEPTH[n]
Output depth
float

9.3.2数据的访问方式

vector

bool bVector = false;int1 iVector = 1;float3 fVector = { 0.2f, 0.3f, 0.4f };double4 dVector = { 0.2, 0.3, 0.4, 0.5 };

vector <bool, 1> bVector = false;vector <int, 1> iVector = 1;vector <float, 3> fVector = { 0.2f, 0.3f, 0.4f };vector <double, 4> dVector = { 0.2, 0.3, 0.4, 0.5 };


// Given
float4 pos = float4(0,0,2,1);pos.z    // value is 2
pos.b    // value is 2

// Given
float4 pos = float4(0,0,2,1);
float2 temp;temp = pos.xy  // valid
temp = pos.rg  // validtemp = pos.xg  // NOT VALID because the position and color sets were used.

float4 pos = float4(0,0,2,1);
float4 f_4D;
f_4D    = pos;     // write four components          f_4D.xz = pos.xz;  // write two components
f_4D.zx = pos.xz;  // change the write orderf_4D.xzyw = pos.w; // write one component to more than one component
f_4D.wzyx = pos;

matrix

A matrix can be initialized when it is declared:


float2x2 fMatrix = { 0.0f, 0.1, // row 12.1f, 2.2f // row 2};

Or, the matrix type can be used to make the same declarations:


matrix <float, 2, 2> fMatrix = { 0.0f, 0.1, // row 12.1f, 2.2f // row 2};
  • The zero-based row-column position:

    • _m00, _m01, _m02, _m03
    • _m10, _m11, _m12, _m13
    • _m20, _m21, _m22, _m23
    • _m30, _m31, _m32, _m33
  • The one-based row-column position:
    • _11, _12, _13, _14
    • _21, _22, _23, _24
    • _31, _32, _33, _34
    • _41, _42, _43, _44
    • [0][0], [0][1], [0][2], [0][3]
    • [1][0], [1][1], [1][2], [1][3]
    • [2][0], [2][1], [2][2], [2][3]
    • [3][0], [3][1], [3][2], [3][3]

However, array accessing can read a multi-component vector.


float2 temp;
float2x2 fMatrix;
temp = fMatrix[0] // read the first row

Just like vectors, naming sets can use one or more components from either naming set.


// Given
float2x2 fMatrix = { 1.0f, 1.1f, // row 12.0f, 2.1f  // row 2};
float2 temp;temp = fMatrix._m00_m11 // valid
temp = fMatrix._m11_m00 // valid
temp = fMatrix._11_22   // valid
temp = fMatrix._22_11   // valid

As with vectors, reading more than one matrix component is called swizzling. More than one component can be assigned, assuming only one name space is used. These are all valid assignments:


// Given these variables
float4x4 worldMatrix = float4( {0,0,0,0}, {1,1,1,1}, {2,2,2,2}, {3,3,3,3} );
float4x4 tempMatrix;tempMatrix._m00_m11 = worldMatrix._m00_m11; // multiple components
tempMatrix._m00_m11 = worldMatrix.m13_m23;tempMatrix._11_22_33 = worldMatrix._11_22_33; // any order on swizzles
tempMatrix._11_22_33 = worldMatrix._24_23_22;
9.3.3 定义shader类型

PixelShader = compile ShaderTarget ShaderFunction(...);

VertexShader = compile ShaderTarget ShaderFunction(...);

ShaderTarget :shade_model 例如vs_2_0

...就是shader中的uniform参数

9.4编译shader

在VS(2012以下)中直接DEBUG是不能发现shader 的语法错误的,这需要使用fxc工具

常用语法是

fxc /T fx_2_0 file.fx

Will compile the entire file with vs and ps 2.0.

fxc /T ps_2_0 /E PixelShaderFunction file.fx

Will compile PixelShaderFunction with ps 2.0.

9.5调试

可以使用dx sdk中带的pix

D3D9 effect (hlsl)(转)相关推荐

  1. DXUT实战2:HLSL(withoutEffect)+D3D9+DXUT(june_2010) .

    不好意思,昨天<<Pro OGRE 3D Programming>>第4章没有搞定,本来想晚上搞出来,一起写博客的,但是最终还是睡觉去了,看来晚上真心不适合学新知识,还是写博客 ...

  2. 【HLSL学习笔记】WPF Shader Effect Library算法解读之[DirectionalBlur]

    原文:[HLSL学习笔记]WPF Shader Effect Library算法解读之[DirectionalBlur] 方位模糊是一个按照指定角度循环位移并叠加纹理,最后平均颜色值并输出的一种特效. ...

  3. DXUT实战3:HLSL(withEffect)+D3D9+DXUT(june_2010) . .

    好了,这是DXUT的最后一个实战,接下来我可能会学习下NVDIA FX Composer 2.5以及Shader Debugger,另外准备每周至少一个shader吧,今天任务把<<Pro ...

  4. 《The Complete Effect and HLSL Guide》翻译连载(二)

    The Complete Effect and HLSL Guide(二) 本文版权归原作者所有,仅供个人学习使用,请勿转载,勿用于任何商业用途. 由于本人水平有限,难免出错,不清楚的地方请大家以原著 ...

  5. Directx中HLSL效果(Effect)的D3DXHANDLE

    由于Directx的效果(ID3DXEffect)中已经封入了常量表,因此只要设置D3DXHANDLE就可以了. 不过偶然下看到不设置D3DXHANDLE,直接写变量名的做法居然也可以通过. 例: 标 ...

  6. hlsl之ambient

    最简单的hlsl. effect code: //--------------------------------------------------------------------------- ...

  7. D3D9 Shader实例教程

    啥是Shader? Shader是一段运行在GPU上的小程序,是运行在GPU上的Pipeline上的特定的可编程单元的小程序. 从D3D9 API层面学习Shader编程 随着Unity3D的流行,很 ...

  8. 6-6.用HLSL定义点光源

    6-6.用HLSL定义点光源 问题 直到现在,你已经用定向光照亮你的场景,它对添加阳光到3D世界很有用.常常,你也将需要一个单点光照,例如手电筒或爆炸.这种光源叫点光源. 方案 从你的XNA项目传递点 ...

  9. Silverlight HLSL实现背景滚动

    一个Silverlight HLSL的简单例子,通过HLSL实现图片上的像素点的的水平移动,从而实现一个滚动背景的效果. 首先把Shader写出来吧.我这里借助了Shazzam ,界面如下: 下面是我 ...

最新文章

  1. html 重置画布坐标系,html5清空画布方法(三种)
  2. 【Java 基础篇】【第一课】HelloWorld
  3. MongoDB副本集
  4. Python教程:多态与多态性
  5. Hibernate持久化对象三种状态
  6. (十九)TCPIP面试宝典-进入大厂必备总结(上)
  7. js二维数组arr中表示读取第i行第j列的是:_c++ c语言 数组与字符串
  8. 40. Use multiple inheritance judiciously
  9. 计算机真有趣作文,真有趣作文10篇
  10. android 删除wifi文件,删除无线网络连接信号(wifi)(图文教程)
  11. 韦东山70天驱动笔记(1)
  12. 日本电产尼得科Nidec研发出超薄直线振动马达
  13. Android-蓝牙通信
  14. QCAD与libreCAD的试用及比较,QCAD比libreCAD更好用,功能更强大
  15. t420i升级固态硬盘提升_技术丨如何升级固态硬盘,提高电脑性能
  16. qls的魔法(区间dp)
  17. linux下用mpg123听音乐
  18. 怎么更新opengl.dll文件_安装累积更新丢文件似乎已成为惯例 KB4556799同样出现文件丢失问题...
  19. 【深度学习经典网络架构—8】:注意力机制之SE_Block
  20. Qt:77---Demo(MDI多文档界面案例)

热门文章

  1. anaconda中怎么sh_【好工具】 深度学习炼丹,你怎么能少了这款工具!JupyterLab 远程访问指南...
  2. C语言深度剖析书籍学习记录 第六章 函数
  3. STL源码剖析 5中迭代器型别
  4. Java JUC工具类--ForkJoin
  5. Android设计模式之——原型模式
  6. 从程序员角度看ELF
  7. mybatis3 中 @Provider 的使用方式
  8. FastJson 中 jsonArray 转换成 list 集合的方法
  9. Overload重載和Override重写的区别。Overloaded的方法是否可以改变返回值的类型?
  10. webpack自动化构建脚本指令npm run dev/build