How to Use Effect
This article Article It will teach you how to build and use effect. It will introduce some powerful and interesting details of effect.
. Create an effect
The following is the example of the dx sdk in basichlsl sample.Code
Id3dxeffect * g_peffect = NULL;
DWORD dwshaderflags = 0;
// Set flag
Dwshaderflags | = d3dxshader_force_vs_software_noopt;
Dwshaderflags | = d3dxshader_force_ps_software_noopt;
Dwshaderflags | = d3dxshader_no_preshader;
// Read the d3dx effect File
Wchar STR [max_path];
Dxutfinddxsdkmediafilecch (STR, max_path, l "basichlsl. FX ");
D3dxcreateeffectfromfile (
1 pd3ddevice,
2 STR,
3 Null, // const d3dxmacro * pdefines,
4 null, // lpd3dxinclude pinclude,
5 dwshaderflags,
6 null, // effect pool
7 & g_peffect,
8 null );
Parameter 6: effect pool. If multiple effect uses a pool pointer pointing to the same memory, these effect will share these variables.
This function is not available if it is null. For more information about other parameters, see SDK.
. Rendering Effect
The general steps for rendering with effect are:
Begin sets the current techinque
Beginpass: set the current pass
Commitchanges changes the effect state in a pass before rendering the code.
Endpass end pass
End end tuchinque
The effect rendering code is often simpler than the corresponding code without effect. The following is an example:
// Apply the technique contained in the effect
G_peffect-> begin (& cpasses, 0 );
For (IPASS = 0; IPASS <cpasses; IPASS ++)
{
G_peffect-> beginpass (IPASS );
// Only call commitchanges if any State changes have happened
// After beginpass is called
G_peffect-> commitchanges ();
// Render the mesh with the Applied Technique
G_pmesh-> drawsubset (0 );
G_peffect-> endpass ();
}
G_peffect-> end ();
There can be multiple Technique in the rendering loop, and one technique can also have multiple pass.
. Effect semantics (semantic)
Effect can find corresponding variables based on the semantics of semantics. A variable can have at most one semantic, and the semantic is of the type:
Following the variable:
Float4x4 matworldviewproj: worldviewproj;
The effect interface can obtain the variable Handle Based on Semantic as follows:
D3dhandle handle =
M_peffect-> getparameterbysemantic (null, "worldviewproj ");
Effect also has other methods to query variables.
. Variable comment
We can add comments to variables, pass and techinque in effect.
It is easy to add comments to variables, and the information can be read and used for other purposes. Annotations can be dynamically added to any data type.
The Declaration of the comment is limited to <>. A comment should include:
1. Type
2. variable name
3. = No.
4. Numeric Value
5.; No.
For example, texture tex0 <string name = "tiger.bmp";>;
<> The inner is an annotation, which is only appended to the variable as a user comment. Through getannotation or
Getannotationbyname function. Annotation can also be applied throughProgramTo add,
Note the following:
Must be either numeric or strings.
Must always be initialized with a default value.
Can be associated with techniques and passes and top-level effect parameters.
Can be written to and read from with either id3dxeffect or id3dxeffectcompiler.
Can be added with id3dxeffect.
Cannot be referenced inside the effect.
Cannot have sub-semantics or sub-annotations.
. Shared effect variables
Effect parameters is some non-static variables declared in effect. These include global variables and annotations.
Effect parameters can be used in different effect versions, but the premise is that these parameters are added with the "shared" keyword.
And must have the same name, type, and semantics. The effect pool contains the shared effect parameter. Pass
D3dxcreateeffectpool:
Id3dxeffectpool * g_peffectpool = NULL; // effect pool for sharing Parameters
D3dxcreateeffectpool (& g_peffectpool );
In effect, these sharing parameters must use the same device. When effect release shares parameters, these parameters are also deleted in the pool.
Parameters. If there is no need to share the parameter, set the pool parameter to null when creating the effect.
. Compile Effect
In application, when you call d3dxcreateeffect, the compilation of effect is included. You can also use
For offline compilation.
Fxc.exe compiles HLSL shaders.
Flink.exe links HLSL fragments.
Vsa.exe compiles Assembly vertex shaders.
Psa.exe compiles Assembly pixel shaders.
These tools are in the (SDK root) \ utilities \ bin \ x86 \ directory.
. Improve performance through pre-rendering.
Preshader improves shader efficiency by pre-calculating regular expressions. The effect compiler automatically extracts it from the shader body.
Let the cup pre-process. This is the same as proposing a static expression from a circular code. Preshader only works in effect
. Preshader can reduce the number of commands and registers required for each rendering, thus improving the shader code execution efficiency.
The effect compiler can be imagined as a collection of two compilers: one compilation CPU type and one compilation GPU type. As
In preshader, Some GPU computing is used by the CPU.
. Parameter Blocks
Parameter blocks can save the effect status.
M_peffect-> settechnique ("renderscene ");
M_peffect-> beginparameterblock ();
D3dxvector4 V4 (diffuse. R, diffuse. G, diffuse. B, diffuse. );
M_peffect-> setvector ("g_vdiffuse", & V4 );
M_peffect-> setfloat ("g_freflectivity", freflectivity vity );
M_peffect-> setfloat ("g_fanimspeed", fanimspeed );
M_peffect-> setfloat ("g_fsizemul", fsize );
M_hparameters = m_peffect-> endparameterblock ();
The above code saves four parameter values and returns a handle. You can call the API applyparameterblock to maintain
Status. The following code is used:
Cobj g_aobj [num_objs]; // object instances
If (succeeded (pd3ddevice-> beginscene ()))
{
// Set the shared parameters using the first mesh's effect.
// Render the mesh objects
For (INT I = 0; I <num_objs; ++ I)
{
Id3dxeffect * PEffect = g_aobj [I]. m_peffect;
// Apply the parameters
PEffect-> applyparameterblock (g_aobj [I]. m_hparameters );
...
PEffect-> begin (& cpasses, 0 );
For (IPASS = 0; IPASS <cpasses; IPASS ++)
{
...
}
PEffect-> end ();
}
...
Pd3ddevice-> endscene ();
}
The above code intuitively sets N parameters with one line of code.