D3D9 effect (hlsl)(转)
?
轉(zhuǎn):http://blog.csdn.net/leonwei/article/details/8212800
effect其實(shí)整合了shader和render state的控制兩大部分內(nèi)容
9.1 effect文件基本框架
part1 :shader state包括全局變量 shader數(shù)據(jù)結(jié)構(gòu)定義,shader的實(shí)現(xiàn)
part2 :texture and sampler state,這個(gè)通常是全局變量,用于shader中,通常要定義在shader的實(shí)現(xiàn)之前
part3 其他的render state設(shè)置,寫(xiě)在technique的pass里,這才是effect文件的主入口
effet中任何一個(gè)變量后面都可以接一個(gè)語(yǔ)意:XXX
1.可以使用的變量類型
Data : Buffer Scalar Vector Matrix Sample Shader Texture Struct UserDefined
9.1.1參數(shù)類型
hlsl中使用的參數(shù)有兩種:Uniform和 varying
Uniform存儲(chǔ)在常量緩存中的,是對(duì)shader的每一次調(diào)用值都是一樣的,可以在D3D中訪問(wèn)的 前面可以加uniform(可不加),通常是全局的
varying:只對(duì)一次調(diào)用起作用的,是來(lái)自vertext stream中的,通常要有語(yǔ)意的修飾,通產(chǎn)是寫(xiě)在shader的輸入輸出參數(shù)里的
9.1.2 sampler紋理采樣器
要包含三個(gè)信息
- A texture :紋理物體(tex0 tex1 tex2...NULL)
- A sampler (with sampler state)如
- A sampling instruction
例如定義這樣一個(gè)sampler
? sampler s = sampler_state { ? texture = NULL; ? mipfilter = LINEAR; }; ? 就可以在shader中使用 float2 sample_2D(float2 tex : TEXCOORD0) : COLOR { ? return tex2D(s, tex); } 來(lái)采樣紋理9.1.3 shader的函數(shù)體
函數(shù)通常要包含這樣幾部分:
- A return type
- A function name
- An argument list (optional)
- An output semantic (optional)
- An annotation (optional)
9.2D3D9中使用effect
1.首先從effect file創(chuàng)建effect(編譯),使用函數(shù)
D3DXCreateEffectFromFile( ??????? pd3dDevice, ??????? str, ??????? NULL, // CONST D3DXMACRO* pDefines, ??????? NULL, // LPD3DXINCLUDE pInclude, ??????? dwShaderFlags, ?????????????? NULL, // LPD3DXEFFECTPOOL pPool, ??????? &g_pEffect, ??????? NULL ); ?dwShaderFlags:
- Skipping validation, 如果已知這是沒(méi)有問(wèn)題的 那么就跳過(guò)檢查
- Skipping optimization 有時(shí)開(kāi)啟優(yōu)化會(huì)導(dǎo)致debug 困難,就可以跳過(guò)優(yōu)化
- Requesting debug information to be included in the shader so it can be debugged.
這個(gè)函數(shù)的調(diào)用實(shí)際上是在從文件編譯出一個(gè)effect
你還可以利用工具fxc.exe離線的編譯出一個(gè)effect,他會(huì)直接編出一個(gè)匯編的shader指令,這樣在程序中就不用編譯了。發(fā)布產(chǎn)品時(shí)這樣做應(yīng)該更好。
2.調(diào)用一個(gè)effect來(lái)render
ID3DXEffect::Begin set technique
ID3DXEffect::BeginPass set pass
ID3DXEffect::CommitChanges提交對(duì)當(dāng)前pass的各種參數(shù)修改,要在draw之前調(diào)用
ID3DXEffect::EndPass
ID3DXEffect::End
一個(gè)典型調(diào)用
3? effect 的參數(shù)設(shè)置(指effect文件中定義的全局變量(uniform))
設(shè)置與獲取
m_pEffect->SetValue
m_pEffect->GetValue
函數(shù)的第一個(gè)參數(shù)都是變量的名字
? 不同的effect 可以共享參數(shù):如果一個(gè)effect變量定義為shared,并且多個(gè)effect創(chuàng)建的時(shí)候使用同一個(gè)pool(D3DXCreateEffectFromFile的第六個(gè)參數(shù))就可以共享這個(gè)參數(shù)。 ? 創(chuàng)建參數(shù)block,可以像使用render state的block那樣 創(chuàng)建parament state,這時(shí)是通過(guò)創(chuàng)建Parameter Blocks的方式。把effect 的參數(shù)設(shè)置操作寫(xiě)在 m_pEffect->BeginParameterBlock();與m_pEffect->EndParameterBlock(); 之間。得到的block可以用在effect 上4 用戶注釋
用戶可以在effect pass 或者某個(gè)變量上附加一個(gè)用戶的注釋。他其實(shí)就是一個(gè)string或numeric格式的變量。格式
? texture Tex0 < string name = "tiger.bmp"; >;意味著給tex0 附加一個(gè)注釋,告訴這個(gè)texture使用的紋理文件的路徑。注釋可以理解為用戶在D3D層面去擴(kuò)充effect 文件的定義。ID3DXBaseEffect::GetAnnotation orID3DXBaseEffect::GetAnnotationByName可以獲取
5 preshader技術(shù)
對(duì)于shader中的常量計(jì)算,D3D會(huì)將其提取出來(lái),在shader執(zhí)行之前,先由CPU計(jì)算,這樣會(huì)提高GPU的效率,防止每個(gè)頂點(diǎn)或pixcel都重復(fù)這個(gè)同樣的運(yùn)算,使不使用preshader可一在編譯時(shí)指定優(yōu)化的flag,關(guān)閉使用參數(shù)D3DXSHADER_NO_PRESHADER。所以基于這個(gè)技術(shù)我們可以在shader中做常量矩陣運(yùn)算,不用擔(dān)心這個(gè)運(yùn)算會(huì)在每個(gè)頂點(diǎn)處做一次。
9.3 語(yǔ)法備忘
9.3.1 語(yǔ)義
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數(shù)據(jù)的訪問(wèn)方式
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 rowJust 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 // validAs 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參數(shù)
9.4編譯shader
在VS(2012以下)中直接DEBUG是不能發(fā)現(xiàn)shader 的語(yǔ)法錯(cuò)誤的,這需要使用fxc工具
常用語(yǔ)法是
fxc /T fx_2_0 file.fxWill compile the entire file with vs and ps 2.0.
fxc /T ps_2_0 /E PixelShaderFunction file.fxWill compile PixelShaderFunction with ps 2.0.
9.5調(diào)試
可以使用dx sdk中帶的pix
總結(jié)
以上是生活随笔為你收集整理的D3D9 effect (hlsl)(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 正则表达式 Mather类的使用
- 下一篇: FireEye:2012年下半年高级威胁