16.2
編譯HLSL 著色器
16.2.1
常量表
每個著色器有一個常量表,用來保存它的變量。D3DX庫通過ID3DXConstantTable接口,提供給應用程序訪問著色器的常量表。通過這個接口我們能夠在應用程序中設置著色器源代碼中的變量。
我們現在描述ID3DXConstantTable接口的方法列表的實現,全部的列表請查閱Direct3D文檔。
16.2.1.1
取得常量句柄
為了在應用程序中設置著色器中的一個特定變量,需要有一種方法去引用它,我們能夠在應用程序中用D3DXHANDLE引用一個在著色器中的變量,下面的方法返回一個著色器中的變量的D3DXHANDLE,使用時,需要傳遞一個變量的名字作為參數:
D3DXHANDLE ID3DXConstantTable::GetConstantByName(
D3DXHANDLE hConstant, // scope of constant
LPCSTR pName // name of constant
);
|
Hconstant——我們要取得的父結構中變量句柄的D3DXHANDLE標識。例如,如果我們想獲得一個特定數據結構中單一數據成員的句柄,我們可以傳遞結構實例的句柄。如果我們獲得一個頂級變量的句柄,給這個參數設為NULL。
PName——我們想獲得的句柄的著色器代碼中的變量的名字。
Gets a constant by looking up its name.
D3DXHANDLE GetConstantByName(
D3DXHANDLE hConstant,
LPCSTR pName
);
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,并且這是頂級變量,我們這么寫:
//
取得著色器中ViewProjMatrix變量的句柄
D3DXHANDLE h0;
h0 =
ConstTable->GetConstantByName(0, "ViewProjMatrix");
|
16.2.1.2
設置常量
一旦應用程序有了一個D3DXHANDLE,要引用著色器代碼中的具體變量,我們可以在應用程序中使用ID3DXConstantTable::SetXXX方法設置變量。如果我們想設置一個向量數組類型的變量,方法名是SetVectorArray。
ID3DXConstantTable::SetXXX的一般語法是:
HRESULT ID3DXConstantTable::SetXXX(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
XXX value
);
|
PDevice:常量表所關聯的設備的指針。
HConstant:我們正在設置的變量句柄的引用。
Value:我們要把變量設置成的值,XXX是我們設置的要替換的變量類型名,對于有些類型(bool,
int, float),傳遞變量值的COPY,另外一些類型(vectors,
matrices, structures),傳遞值的指針。
下面的列表描述了我們能用ID3DXConstantTable接口設置的類型列表。這里假定我們有一個有效的設備,和一個有效句柄。
SetBool—Used
to set a Boolean value. Sample call:
bool b = true;
ConstTable->SetBool(Device, handle, b);
|
Sets a Boolean value.
HRESULT SetBool(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
BOOL b
);
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:
bool b[3] = {true, false, true};
ConstTable->SetBoolArray(Device, handle, b, 3);
|
Sets an array of Boolean values.
HRESULT SetBoolArray(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
CONST BOOL* pB,
UINT Count
);
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:
float f = 3.14f;
ConstTable->SetFloat(Device, handle, f);
|
Sets a floating-point number.
HRESULT SetFloat(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
FLOAT f
);
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:
float
f[2] = {1.0f, 2.0f};
ConstTable->SetFloatArray(Device, handle, f, 2);
|
Sets an array of floating-point numbers.
HRESULT SetFloatArray(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
CONST FLOAT* pf,
UINT Count
);
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:
int x
= 4;
ConstTable->SetInt(Device, handle, x);
|
Sets an integer value.
HRESULT SetInt(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
INT n
);
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:
D3DXMATRIX M(…);
ConstTable->SetMatrix(Device, handle, &M);
|
Sets a nontransposed matrix.
HRESULT SetMatrix(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
CONST D3DXMATRIX* pMatrix
);
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:
D3DXMATRIX M[4];
//
...Initialize matrices
ConstTable->SetMatrixArray(Device, handle, M, 4);
|
Sets an array of nontransposed matrices.
HRESULT SetMatrixArray(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
CONST D3DXMATRIX* pMatrix,
UINT Count
);
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:
D3DXMATRIX* M[4];
//
...Allocate and initialize matrix pointers
ConstTable->SetMatrixPointerArray(Device, handle, M, 4);
|
Sets an array of pointers to nontransposed matrices.
HRESULT SetMatrixPointerArray(
LPDIRECT3DDEVICE9 pDevice,
D3DXHANDLE hConstant,
CONST D3DXMATRIX ** ppMatrix,
UINT Count
);
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.