高级着色语言HLSL入门(2)
16.2 编译HLSL 着色器
16.2.1 常量表
每个着色器有一个常量表,用来保存它的变量。D3DX库通过ID3DXConstantTable接口,提供给应用程序访问着色器的常量表。通过这个接口我们能够在应用程序中设置着色器源代码中的变量。
我们现在描述ID3DXConstantTable接口的方法列表的实现,全部的列表请查阅Direct3D文档。
16.2.1.1 取得常量句柄
为了在应用程序中设置着色器中的一个特定变量,需要有一种方法去引用它,我们能够在应用程序中用D3DXHANDLE引用一个在着色器中的变量,下面的方法返回一个着色器中的变量的D3DXHANDLE,使用时,需要传递一个变量的名字作为参数:
Hconstant——我们要取得的父结构中变量句柄的D3DXHANDLE标识。例如,如果我们想获得一个特定数据结构中单一数据成员的句柄,我们可以传递结构实例的句柄。如果我们获得一个顶级变量的句柄,给这个参数设为NULL。
PName——我们想获得的句柄的着色器代码中的变量的名字。
Gets a constant by looking up its name.
Parameters
hConstant
[in] Unique identifier to the parent data structure. If the constant is a top-level parameter (there is no parent data structure), use NULL. pName
[in] Name of the constant.
Return Values
Returns a unique identifier to the constant.
例如,如果在着色器中变量的名字为ViewProjMatrix,并且这是顶级变量,我们这么写:
16.2.1.2 设置常量
一旦应用程序有了一个D3DXHANDLE,要引用着色器代码中的具体变量,我们可以在应用程序中使用ID3DXConstantTable::SetXXX方法设置变量。如果我们想设置一个向量数组类型的变量,方法名是SetVectorArray。
ID3DXConstantTable::SetXXX的一般语法是:
PDevice:常量表所关联的设备的指针。
HConstant:我们正在设置的变量句柄的引用。
Value:我们要把变量设置成的值,XXX是我们设置的要替换的变量类型名,对于有些类型(bool, int, float),传递变量值的COPY,另外一些类型(vectors, matrices, structures),传递值的指针。
下面的列表描述了我们能用ID3DXConstantTable接口设置的类型列表。这里假定我们有一个有效的设备,和一个有效句柄。
SetBool—Used to set a Boolean value. Sample call:
Sets a Boolean value.
Parameters
pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table. hConstant
[in] Unique identifier to the constant. See D3DXHANDLE. b
[in] Boolean value.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
SetBoolArray—Used to set a Boolean array. Sample call:
Sets an array of Boolean values.
Parameters
pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table. hConstant
[in] Unique identifier to the array of constants. See D3DXHANDLE. pB
[in] Array of Boolean values. Count
[in] Number of Boolean values in the array.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
SetFloat—Used to set a float. Sample call:
Sets a floating-point number.
Parameters
pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table. hConstant
[in] Unique identifier to the constant. See D3DXHANDLE. f
[in] Floating-point number.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
SetFloatArray—Used to set a float array. Sample call:
Sets an array of floating-point numbers.
Parameters
pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table. hConstant
[in] Unique identifier to the array of constants. See D3DXHANDLE. pf
[in] Array of floating-point numbers. Count
[in] Number of floating-point values in the array.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
SetInt—Used to set an integer. Sample call:
Sets an integer value.
Parameters
pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table. hConstant
[in] Unique identifier to the constant. See D3DXHANDLE. n
[in] Integer.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
SetMatrix—Used to set a 4 × 4 matrix. Sample call:
Sets a nontransposed matrix.
Parameters
pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table. hConstant
[in] Unique identifier to the matrix of constants. See D3DXHANDLE. pMatrix
[in] Pointer to a nontransposed matrix. See D3DXMATRIX.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
SetMatrixArray—Used to set a 4 × 4 matrix array. Sample call:
Sets an array of nontransposed matrices.
Parameters
pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table. hConstant
[in] Unique identifier to the array of constant matrices. pMatrix
[in] Array of nontransposed matrices. Count
[in] Number of matrices in the array.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
SetMatrixPointerArray—Used to set an array of 4 × 4 matrix pointers. Sample call:
Sets an array of pointers to nontransposed matrices.
Parameters
pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the constant table. hConstant
[in] Unique identifier to an array of constant matrices. ppMatrix
[in] Array of pointers to nontransposed matrices. Count
[in] Number of matrices in the array.
Return Values
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
Remarks
A nontransposed matrix contains row-major data; that is, each vector is contained in a row.