Shadder and Effect - 1.2 Compiling HLSL Shaders

zhaozj2021-02-16  53

1.2 compile HLSL shader

Read this article shows that you have agreed to the statement

1.2.1 constant table

Each shader has a constant table, which is used to store shader variables. The D3DX library provides us with an application access to the shader constant table interface ID3dxConstantTable. With this interface, we can set the variables in the source code in the application's code. (Translator Note: The "setting" here can be understood as "for ... assignment", such as the above sentence, "We can" we can assign a variable in the source code in the application code ", below)

We now describe a list of methods implemented by deleted ID3DXConstantTable. See the Direct3D documentation for a complete list.

1.2.1.1 Get a constant handle

In order to set a specific variable in the shader in the application code, we need a method to reference it. We can reference a variable of the shader in the application through the D3DxHandle. The following methods returns a D3DXHANDLE of a shader in a shader, as long as it is given (the translator Note: "The" its "pointer to the variable in the shader) name:

D3DXHANDLE ID3DXCONSTANTTABLE :: getConstantByname

D3DXHANDLE HCONSTANT, / / ​​Scope of Constant

LPCSTR PNAME // Name of Constant

);

HConstant - the D3DxHandle ID of the parent structure of the variable that needs the handle. For example, if we have to get a handle of a single data member of a particular structure instance, we can pass the handle of this structure instance into it. If we have to get a handle of top-level variable, it is good to pass 0 to this parameter.

PNAME - requires the name of the variable in the color source code returned by the handle.

For example, if the name of the variable in the shader is ViewProjmatrix, and it is a top-level parameter, we can write this:

// Get a Handle to the ViewProjmatrix Variable in The Shader.

D3DXHANDLE H0;

H0 = consttTable-> getConstantByName (0, "ViewProjmatrix");

1.2.1.2 Set constants

Once the application gets the D3DxHandle handle of a specific variable referenced in the shader code, we can set this variable in the application with the ID3dxConstantTable :: SetXXX method, where xxx represents the type of variable to be set. For example, if the variable we want to set is a vector array, the method name should be setVectorRray.

The general grammar form of ID3DXConstantTable :: SetXXX is as follows:

HRESULT ID3DXCONSTANTTABLE :: SetXXX (

LPDIRECT3DDEVICE9 PDEVICE,

D3DXHANDLE HCONSTANT,

XXX Value

);

n pDevice - pointer to the device associated with a constant table

n hconstant - a handle that references the variables we are setting up

n value-- We have to set the variable, where xxx replaces the name of the type of variable we want to set. For some types (BOOL, INT, FLOAT), we pass a value of a copy of the value, but for other types (Vectors, Matrices, Structures), we pass a value of a value of a value (a pointer to the value). When we set an array, the SetXXX method requires an additional fourth parameter, which specifies the number of elements in the array. For example, for a 4D array method, 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 a list of the types we can set with the ID3DXConstantTable interface. Here, we have a valid device for setting with a valid handle.

SetBool-useed to set a boolean value. Sample Call:

BOOL B = true;

ConstTable-> SetBool (Device, Handle, B);

SetBoolarray-useed to set a boolean array. Sample Call:

BOOL B [3] = {True, False, True};

ConstTable-> SetBoolarray (Device, Handle, B, 3);

SetFloat-useed to set a float. Sample Call:

Float f = 3.14f;

ConstTable-> setfloat (Device, Handle, F);

SetFloatArray-useed to set a float array. Sample Call:

FLOAT F [2] = {1.0F, 2.0F};

ConstTable-> SetFloatArray (Device, Handle, F, 2);

Setint-useed to set an integer. Sample Call:

INT x = 4;

ConstTable-> Setint (Device, Handle, X);

SetInTarray-useed to set an integer array. Sample Call:

INT x [4] = {1, 2, 3, 4};

ConstTable-> SetInTarray (Device, Handle, X, 4);

Setmatrix-useed to set a 4 × 4 matrix. Sample Call:

D3DXMAMATRIX M (...);

ConstTable-> Setmatrix (Device, Handle, & M);

SetmatrixArray-useed to set a 4 × 4 Matrix Array. Sample Call:

D3DXMAMATRIX M [4];

// ... Initialize Matrices

ConstTable-> SetmatrixArray (Device, Handle, M, 4); SetMatrixPointerarray-useed to set an array of 4 × 4 matrix pointers. Sample Call:

D3DXMAMATRIX * M [4];

// ... allocate and initialize Matrix Pointers

ConstTable-> SetMatrixPointerarray (Device, Handle, M, 4);

SetMatrixTranspose-useed to set a transposed 4 × 4 Matrix. Sample Call:

D3DXMAMATRIX M (...);

D3DXMAMATRIXTRANSPOSPPOSE (& M, & M);

ConstTable-> SetmatrixTranspose (Device, Handle, & M);

SetMatrixTransposeArray-use to set an array of 4 × 4 Transposed Matrice. Sample Call:

D3DXMAMATRIX M [4];

// ... Initialize Matrices and Transpose.

ConstTable-> SetmatrixTransposeArray (Device, Handle, M, 4);

SetMatrixTransposepointerarray-useed to set an array of pointers to 4 × 4 Transposed Matrices. Sample Call:

D3DXMAMATRIX * M [4];

// ... allocate, Initialize Matrix Pointers and Transpose.

ConstTable-> SetmatrixTransposepointerarray (Device, Handle, M, 4);

SetVector-useed 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-useed to set a variable what is a vector array. Sample Call:

D3DXVECTOR4 V [3];

// ... Initialize Vectors

ConstTable-> SetVectrarray (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:

D3DXMAMATRIX M (...);

ConstTable-> SetValue (Device, Handle, (Void *) & M, SizeOf (M));

1.2.1.3 Set constant default values

The next method is to set the constant for their default values, which initializes when declaring. This method should be called Called OnCE during application establishment (SETUP).

HRESULT ID3DXCONSTANTTABLE :: setDefaults

LPDIRECT3DDEVICE9 PDEVICE

);

n PDEvice - a pointer to the device associated into a constant table.

1.2.2 Compile HLSL shader

We can compile a shader - text file with our saved shader - use the following functions: HRESULT D3DXCompileshaderfromFile

LPCSTR PSRCFILE,

Const d3dxmacro * pdefines,

LPD3DXINCLUDE PINCLUDE,

LPCSTR PFUNCTIONNAME,

LPCSTR PTARGET,

DWORD FLAGS,

LPD3DXBUFFER * PPSHADER,

LPD3DXBUFFER * PPERRORMSGS,

LPD3DXCONSTANTTABLE * PPCONSTANTTABLE

);

n psrcfile - the file name of the text file containing the shader source code

n pdefines - parameter is optional, specified in this book is empty.

n Pinclude - ID3DXINCLUDE interface pointer. This interface is designed to be implemented by an application, so we can overload the default include behavior. Typically, the default behavior is good, and we can ignore this parameter by specifying it as empty.

n pfunctionName - Specify the string of the entry point function name. For example, if the inlet point function of the shader is called main, we can deliver "main" to this parameter.

n ptarget - Specifies the string of the version of the HLSL shader source file to be compiled. Effective vertex shader versions are: vs_1_1, vs_2_0, vs_2_sw. Effective pixel shader version is 2.0, we can deliver vs_2_0 to this parameter.

Remarks: Ability to compile to different shader versions is a major benefit of HLSL beyond assembly languages. With HLSL, we only need to simply recompile to a Target, you can quickly gray to different versions. Using compilation, we may need to manually transplant code.

n flags - an optional compilation tag, specified as a 0 identification is not marked. Effective options are:

l D3DXSHADER_DEBUG - Notify the compiler write debugging information

l D3DXSHADER_SKIPVALIDATION - Notify the compiler Do not do any code check. This item is only used for you know that the shader is working.

l D3dxShader_skipoptimization - Notifying the compiler Do not perform any code optimization. In practice, this option should be used only for debugging, because in this case you don't want the compiler to modify the code in any way.

n PPShader - Returns the ID3DXBuffer pointer of the compiled shader code. This compiled shader code will be a parameter for another actual creation of a value of a vertex / pixel shader.

n pPerrorMsgs - returns the ID3DXBuffer pointer containing the error code and error message string

n PPConstantTable - Returns ID3DXCONSTANTTABLE pointer to which this shader constant table data is included

Here is an example of calling d3dxcompileshaderfromfile:

//

// compile shader

//

ID3DXCONSTANTTABLE * TransformConstantTable = 0;

ID3DXBUFFER * shader = 0;

ID3DXBUFFER * ERRORBUFFER = 0;

HR = D3DXCompileshaderfromfile

"Transform.txt", // Shader FileName

0,

0,

"Main", // entry point function name "vs 2 0", // Shader Version to Compile 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 (ErrorBuffer);

}

IF (Failed (HR))

{

:: Messagebox (0, "D3DXCREATEEFFECTFROMFILE () - failed", 0, 0);

Return False;

}

[Declaration]: Introduction to 3D Game Programming With DirectX 9.0 "in this article, it is limited to the translator level, and it is inevitable that there is a mistake in the text. Welcome all netizen criticism; this article is only used for learning exchange and reference usage, not to use In any form of commercial use; if you need to reprint the author's own and translator's consent, maintain the integrity of the article, indicate the author, translator and source, for the consequences of violating the above terms, the translator Do not bear any responsibility. My email address is raymond_king123@hotmade.com, welcome 3D graphics and games, and friends with a certain graphical programming experience to communicate.

转载请注明原文地址:https://www.9cbs.com/read-23184.html

New Post(0)