Game Development Reference
In-Depth Information
16.2 Compiling an HLSL Shader
16.2.1 The Constant Table
Every shader has a constant table that is used to store its variables.
The D3DX library provides our application access to a shader's con-
stant table through the ID3DXConstantTable interface. Via this
interface we can set variables in the shader source code from our appli-
cation's code.
We now describe an abridged list of the methods that ID3DXCon-
stantTable implements. For a complete list, see the Direct3D
documentation.
16.2.1.1 Getting a Handle to a Constant
In order to set a particular variable in a shader from our application
code, we need a way to refer to it. We can refer to a variable in the
shader from our application with a D3DXHANDLE . The following method
returns a D3DXHANDLE to a variable in the shader when given its name:
D3DXHANDLE ID3DXConstantTable::GetConstantByName(
D3DXHANDLE hConstant, // scope of constant
LPCSTR pName
// name of constant
);
hConstant —A D3DXHANDLE that identifies the parent structure
in which the variable that we want a handle to lives. For example, if
we wanted to get a handle to a single data member of a particular
structure instance, we would pass in the handle to the structure
instance here. If we are obtaining a handle to a top-level variable,
we can pass 0 for this parameter.
pName —The name of the variable in the shader source code that
we want to obtain a handle to
For example, if the name of the variable in the shader is ViewProj-
Matrix and it was a top level parameter, we would write:
// Get a handle to the ViewProjMatrix variable in the shader.
D3DXHANDLE h0;
h0 = ConstTable->GetConstantByName(0, "ViewProjMatrix");
16.2.1.2 Setting Constants
Once our application has a D3DXHANDLE that refers to a particular vari-
able in the shader code, we can set that variable from our application
using the ID3DXConstantTable::SetXXX methods, where the XXX
is replaced by a type name to indicate the type of variable being set. For
Search WWH ::




Custom Search