D3D9 effect (hlsl)(转)

 

转: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纹理采样器

要包含三个信息

  • A texture :纹理物体(tex0 tex1 tex2...NULL)
  • A sampler (with sampler state)如
  • A sampling instruction

例如定义这样一个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的函数体

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

  • A return type
  • A function name
  • An argument list (optional)
  • An output semantic (optional)
  • An annotation (optional)
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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/462345.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

boost_1.47在VS2010下的安装

第一步&#xff1a;到官网www.boost.org&#xff0c;下载boost最新版boost_1_47_0.zip第二步&#xff1a;将boost压缩包解压到C盘根目录下(在C盘操作简单&#xff0c;不会出错)&#xff0c;目录为C:\BOOST_1_47_0,里面就是boost的源码和需要编译的文件及文档资料第三步&#xf…

创建 WPF 不规则窗口

创建 WPF 不规则窗口 本文为khler原作&#xff0c;转载必须确保本文完整并完整保留原作者信息及本文原始链接  E-mail: khler163.com  QQ: 23381103  MSN: pragmachotmail.com   相对于用MFC创建不规则窗口&#xff0c;WPF创建不规则窗体的过程就显得相当享受了&…

公司运作 - 利润率、周转率

公司一般由市场部、研发部、财务部、人力资源部及其他辅助部门组成。分成了几个层面&#xff0c;如下&#xff1a; 宏观层面&#xff1a;业务范围、业务定位、专业化、多元化部门层面&#xff1a;各部门绩效、部门职责主体层面&#xff1a;跨部门事务&#xff0c;如产品研发涉及…

ffmpeg编解码详细过程

1. 注册所有容器格式和CODEC:av_register_all() 2. 打开文件:av_open_input_file() 3. 从文件中提取流信息:av_find_stream_info() 4. 穷举所有的流&#xff0c;查找其中种类为CODEC_TYPE_VIDEO 5. 查找对应的解码器:avcodec_find_decoder() 6. 打开编解码器:avcodec_open() 7.…

使用docker 起容器配置负载均衡(加权)

首先要准备三个nginx的容器&#xff1b; 第二个容器&#xff1a; 第三个容器&#xff1a; 进入第一个容器&#xff08;主容器&#xff09; 要配置的容器&#xff08;docker exec -it 容器id /bin/bash&#xff09; vi/etc/nginx/nginx.conf 修改配置 在http{ }中加入 vi/etc/…

茴香豆的n种写法之②——Sql据多个条件 查询每个条件的前N条记录

----------------------------------- Author: liangCK 小梁-- Title : 查每个分组前N条记录-- Date : 2008-11-13 17:19:23-----------------------------------> 生成测试数据: #TIF OBJECT_ID(tempdb.dbo.#T) IS NOT NULL DROP TABLE #TCREATE TABLE #T (ID VARCHAR(3),G…

PostgreSQL建表动作分析

首先&#xff0c;建立表&#xff1a; pgsql# create table tab10(id integer); CREATE TABLE pgsql# select 147525::regclass;regclass ----------tab10 (1 row)pgsql# 查看此时的文件信息&#xff1a; [pgsqllocalhost 16384]$ pwd /home/pgsql/DemoDir/base/16384[pgsqllo…

http 断点续传,Windows下HTTP方式单线程下载

http 断点续传www.diybl.com 时间 &#xff1a; 2011-05-20 作者&#xff1a;匿名 编辑&#xff1a;hawk 点击&#xff1a; 1128 [ 评论 ]-- 原理&#xff1a; 1. 打开本地文件fopen&#xff0c;移动文件指针到文件尾fseek 2. 获得文件大小ftell, 格式化HTTP请求头 &…

给创业者的30条建议

http://www.cocoachina.com/programmer/20150206/11119.html 去年年底的时候&#xff0c;我&#xff08;Firstround Review 主编&#xff09;在 Facebook 公司的咖啡厅里和 Caryn Marooney 交流着创业公司应该注意些什么事情。Caryn Marooney 现在是 Facebook 公司科技交流部门…

php swoole websocket vue 实现聊天室案例

代码地址: https://github.com/9499574/demo_chat_room 转载于:https://www.cnblogs.com/phper8/p/11017892.html

数据结构 练习21-trie的原理分析和应用

前言 今天具体分析一下trie树&#xff0c;包括&#xff1a;原理分析&#xff0c;应用场合&#xff0c;复杂度分析&#xff0c;与hash的比较&#xff0c;源码展现。大部分内容来自互联网&#xff0c;文中会注明出处。 原理分析 主要是hash树的变种&#xff0c;先看下图&#xff…

在辞职后的旅途中:我写了个App 创立了一家公司

http://www.cocoachina.com/programmer/20150206/11119.html 英文原文&#xff1a;How I built a startup while traveling to 20 countries 一年前&#xff0c;我离开了旧金山&#xff0c;变卖或者送掉了一切我所拥有的东西&#xff0c;然后买了一只 40 升的登山包。 我旅行到…

Android找工作系列之自定义View

...转载于:https://www.cnblogs.com/hbolin/p/11019959.html

POJ 1088-滑雪

矩阵里的数字代表当前点的高度&#xff0c;只能从高的点滑到低的点&#xff0c;求最长能滑的距离。初始点 不规定。我们可以向每个点的四周搜索&#xff0c;能走则就在当前距离加1。并将已经求的值保存在 二维数组中。&#xff08;记忆化搜索&#xff09; /*Accepted 252K …

PostgreSQL的 initdb 源代码分析之二十一

继续分析&#xff1a; setup_schema(); 展开&#xff1a; 实质就是创建info_schema。 cmd 是&#xff1a; "/home/pgsql/project/bin/postgres" --single -F -O -c search_pathpg_catalog -c exit_on_errortrue -j template1 >/dev/null infor_schem_file是&…

Be My Eyes app:我是你的眼

http://www.cocoachina.com/industry/20150122/10979.html Be My Eyes是丹麦软件工作室Robocat为一家同名非营利性企业推出的一款应用&#xff0c;主要通过视频聊天的方式将志愿者和视力受损的患者联系起来&#xff0c;从而实现远程协助的功能。 Be My Eyes的核心概念非常简单-…

C#面试题整理(不带答案)

1.维护数据库的完整性、一致性、你喜欢用触发器还是自写业务逻辑&#xff1f;为什么? 2.什么是事务&#xff1f;什么是锁&#xff1f; 3.什么是索引&#xff0c;有什么优点&#xff1f; 4.视图是什么&#xff1f;游标是什么&#xff1f; 5.什么是存储过程&#xff1f;有什么优…

nRF905

nRF905[1]无线芯片是有挪威NORDIC公司出品的低于1GHz无线数传芯片&#xff0c;主要工作于433MHz、868MHz和915MHz的ISM频段。芯片内置频率合成器、功率放大器、晶体振荡器和调制器等功能模块&#xff0c;输出功率和通信频道可通过程序进行配置。非常适合于低功耗、低成本的系统…

用户界面概述

视图是提供了良好定义的功能集合的内容区域。 控件则是能够触发即时动作或可视化结果的图形对象 无论是什么类型的应用程序&#xff0c;都有一个应用程序窗口&#xff0c;该窗口为您提供了一个能够呈现应用程序的所有信息的背景。但是用户对这个窗口没有概念&#xff0c;他们对…

Firefox for iOS现身Github 使用Swift编写

http://www.cocoachina.com/industry/20141208/10545.html 自从Mozilla新CEO走马上任以来&#xff0c;该公司对于发展路线显然与以往有所不同&#xff0c;对该公司最重要的产品Firefox浏览器来说&#xff0c;也有了很多大的改变&#xff0c;包括前几天Mozilla宣布&#xff0c;它…