Tutorials to .com

Tutorials to .com » Software » Game-developer » Shaders and effects - HLSL shader compiler 1.2

Shaders and effects - HLSL shader compiler 1.2

Print View , by: iSee ,Total views: 54 ,Word Count: 2060 ,Date: Thu, 28 May 2009 Time: 4:43 PM

HLSL shader compiler 1.2

Read this article indicates that you have agreed to the statement at the end

Chang Scale 1.2.1

Each shader has a regular scale (constant table), it is used to store shader variables. D3DX library provides us with an application to visit often scale shader interface ID3DXConstantTable. Through this interface, we can code the application to set up (set) the source code shader variables. (Translator's Note: The "Settings" can be understood as "value for ...", such as the one above can also be understood as "we can in the application code for the shader source code in the variable assignment", the same below )

We now describe a ID3DXConstantTable deleted the list of the method. A complete list please see the Direct3D documentation. access to handle the constant

Application code in order to set up a shader-specific variables, we need a way to invoke it. We can in the application cited by D3DXHANDLE a shader variables. The following method returns a shader variable D3DXHANDLE, if given the (Translator's Note: "it" color device in the pointing variable) name:

D3DXHANDLE ID3DXConstantTable:: GetConstantByName (

D3DXHANDLE hConstant, / / scope of constant

LPCSTR pName / / name of constant


hConstant - need to handle variable load structure of the parent D3DXHANDLE logo. For example, if we are to get a specific data structure of a single instance of a member of the handle, we can pass this structure into the instance handle. If we are to get a top-level variable (top-level variable) of the handle to the parameter 0 be just fine.

pName - the need to handle the return of the shader source code in the name of the variable.

For example, if a shader variable name is ViewProjMatrix, and it is a top-level parameters, we can write:

/ / Get a handle to the ViewProjMatrix variable in the shader.


h0 = ConstTable-> GetConstantByName (0, "ViewProjMatrix"); set constant

Once the application shader code was quoted D3DXHANDLE handle specific variables, we can use ID3DXConstantTable:: SetXXX method in the application set this variable, which on behalf of XXX to the type of variable settings. For example, if we are to set up an array of variables is a vector, and method name should be SetVectorArray.

ID3DXConstantTable:: SetXXX the form of the general syntax is as follows:

HRESULT ID3DXConstantTable:: SetXXX (


D3DXHANDLE hConstant,

XXX value


n pDevice - scale associated with the regular pointer devices

n hConstant - a handle, it quoted that we are setting the variable

n value - we want to set the value of variables, in which XXX is replaced by the variables we need to set the type by name. For some types (bool, int, float) value, we send a copy (a copy of the value), but other types (vectors, matrices, structures), we send a pointer value (a pointer to the value).

When we set up an array of time, SetXXX method requires an additional fourth parameter, which specified the number of array elements. For example, for the creation of a 4D array of methods, the prototype is as follows:

HRESULT ID3DXConstantTable:: SetVectorArray (

LPDIRECT3DDEVICE9 pDevice, / / associated device

D3DXHANDLE hConstant, / / handle to shader variable

CONST D3DXVECTOR4 * pVector, / / pointer to array

UINT Count / / number of elements in array


The following list describes the interface we can set the type of ID3DXConstantTable list. Here assume that we have a set of effective use of equipment, and a valid handle.

SetBool-Used to set a Boolean value. Sample call:

bool b = true;

ConstTable-> SetBool (Device, handle, b);

SetBoolArray-Used to set a Boolean array. Sample call:

bool b [3] = (true, false, true);

ConstTable-> SetBoolArray (Device, handle, b, 3);

SetFloat-Used to set a float. Sample call:

float f = 3.14f;

ConstTable-> SetFloat (Device, handle, f);

SetFloatArray-Used to set a float array. Sample call:

float f [2] = (1.0f, 2.0f);

ConstTable-> SetFloatArray (Device, handle, f, 2);

SetInt-Used to set an integer. Sample call:

int x = 4;

ConstTable-> SetInt (Device, handle, x);

SetIntArray-Used to set an integer array. Sample call:

int x [4] = (1, 2, 3, 4);

ConstTable-> SetIntArray (Device, handle, x, 4);

SetMatrix-Used to set a 4 × 4 matrix. Sample call:


ConstTable-> SetMatrix (Device, handle, & M);

SetMatrixArray-Used to set a 4 × 4 matrix array. Sample call:


/ / ... Initialize matrices

ConstTable-> SetMatrixArray (Device, handle, M, 4);

SetMatrixPointerArray-Used to set an array of 4 × 4 matrix pointers. Sample call:


/ / ... Allocate and initialize matrix pointers

ConstTable-> SetMatrixPointerArray (Device, handle, M, 4);

SetMatrixTranspose-Used to set a transposed 4 × 4 matrix. Sample call:


D3DXMatrixTranspose (& M, & M);

ConstTable-> SetMatrixTranspose (Device, handle, & M);

SetMatrixTransposeArray-Used to set an array of 4 × 4 transposed matrices. Sample call:


/ / ... Initialize matrices and transpose them.

ConstTable-> SetMatrixTransposeArray (Device, handle, M, 4);

SetMatrixTransposePointerArray-Used to set an array of pointers to 4 × 4 transposed matrices. Sample call:


/ / ... Allocate, initialize matrix pointers and transpose them.

ConstTable-> SetMatrixTransposePointerArray (Device, handle, M, 4);

SetVector-Used to set a variable of type D3DXVECTOR4. Sample call:

D3DXVECTOR4 v (1.0f, 2.0f, 3.0f, 4.0f);

ConstTable-> SetVector (Device, handle, & v);

SetVectorArray-Used to set a variable that is a vector array. Sample call:

D3DXVECTOR4 v [3];

/ / ... Initialize vectors

ConstTable-> SetVectorArray (Device, handle, v, 3);

SetValue-Used to set an arbitrarily sized type, such as a structure. In the sample call, we use SetValue to set a D3DXMATRIX:


ConstTable-> SetValue (Device, handle, (void *) & M, sizeof (M)); Set the default value constant

One way is to set up under the constant default value for them, these default values when initialized in a statement. This method should be set up in the application (setup) was a one-time during the call (called once).

HRESULT ID3DXConstantTable:: SetDefaults (



n pDevice - often related to the scale of the indicator device.

HLSL shader compiler 1.2.2

We can compile a shader - with our shader saved the text file - use the following function:

HRESULT D3DXCompileShaderFromFile (

LPCSTR pSrcFile,



LPCSTR pFunctionName,

LPCSTR pTarget,

DWORD Flags,

LPD3DXBUFFER * ppShader,

LPD3DXBUFFER * ppErrorMsgs,



n pSrcFile - to compile the shader source code that contains the text of the document file name

n pDefines - optional parameters, designated as a space in this book.

n pInclude - ID3DXInclude interface pointer. This interface is designed to achieve by the application, so we can include the default behavior of heavy-duty. Typically, the default behavior is good, and we can be designated as Air ignore this parameter.

n pFunctionName - the designated entry point function name string. For example, if the shader entry point function called Main, we can pass this parameter to the "Main".

n pTarget - designated to compile the HLSL shaders into a source file version string. Effective vertex shader version: vs_1_1, vs_2_0, vs_2_sw. Effective pixel shader version is 2.0, we can give this parameter vs_2_0.

Note: ability to compile different versions of the HLSL shader language go beyond the compilation of one of the main benefits. We used a simple HLSL compiler for the need to re-target, can be quickly transplanted to different versions of shader. The use of the compilation, then we may need to manually transfer the code.

n Flags - compiler optional tags, designated as 0 not marked logo. Effective options are:

l D3DXSHADER_DEBUG - to inform the compiler to write debugging information

l D3DXSHADER_SKIPVALIDATION - to inform the compiler checks the code will not do anything. The only known shader you can work

l D3DXSHADER_SKIPOPTIMIZATION - to inform the compiler will not perform any code optimization. Practice, this option should be used only for debugging, because this case you do not want the compiler to modify the code in any way.

n ppShader - to return to the compiled shader code ID3DXBuffer pointer. The compiled shader code will serve as the actual creation of a vertex / pixel shader function parameters

n ppErrorMsgs - return contains an error code and error message string pointer ID3DXBuffer

n ppConstantTable - to return to the shader contains regular scale data pointer ID3DXConstantTable

Here is an example of D3DXCompileShaderFromFile call:

/ /

/ / Compile shader

/ /

ID3DXConstantTable * TransformConstantTable = 0;

ID3DXBuffer * shader = 0;

ID3DXBuffer * errorBuffer = 0;

hr = D3DXCompileShaderFromFile (

"transform.txt", / / shader filename



"Main", / / entry point function name

"vs 2 0", / / shader version to compile to

D3DXSHADER_DEBUG, / / debug compile

& shader,

& errorBuffer,

& TransformConstantTable);

/ / Output any error messages

if (errorBuffer)


:: MessageBox (0, (char *) errorBuffer-> GetBufferPointer (), 0, 0);

d3d:: Release <ID3DXBuffer*> (errorBuffer);


if (FAILED (hr))


:: MessageBox (0, "D3DXCreateEffectFromFile () - FAILED", 0, 0);

return false;


[Statement]: In this paper, Frank Luna translated from the "Introduction to 3D Game Programming with DirectX 9.0", limited to the level translator, the text of the inevitable mistakes, criticism welcome all friends; this article only for the exchange of learning and reference purposes, shall not be used in any form of commercial use; For reprint take prior consent of the author and the translator's consent, to maintain the integrity of the article, and indicate the author, translator and the source, who violate the terms of the consequences of the above, the translator of this no responsibility. My email address is Raymond_King123@hotmail.com, welcome the love of 3D graphics and games, and graphics programming experience in a certain friend a letter from the exchange.

Game Developer Tutorial Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.