Game Development BASICS (19th)

Source: Internet
Author: User
Tags pixel coloring

Chapter 2
A rendering effect is usually composed of the following components: a vertex coloring device and a pixel coloring device, a list of device statuses to be set, and one or more drawing paths (rendering pass ).
In addition, we hope to adopt an inefficient operating mechanism (fallback mechanism) to plot the effects of different levels of graphics hardware (that is, to make the best use of the current hardware conditions, all rendering tasks are related to a specific effect. Therefore, it is logical to encapsulate these tasks into a unit.

Direct3d effect framework provides a mechanism for the preceding task encapsulation. This mechanism can encapsulate effects-related tasks into an effect file. there are many advantages in implementing various effects in the Results file. One of them is that the implementation of a certain effect can be changed without re-compiling the application source code, which makes the effect update process, whether it is a bug fix, it is easier to simply improve the effect or use the latest 3D hardware performance. Secondly, it encapsulates all the components related to the effect into a file, this brings great convenience to program maintenance.

Performance files can also be stored in any ASCII file like HLSL programs.

Method and Path:
An effect file contains one or more techniques (technique), which are specific methods for rendering certain effects, that is, the effect file provides one or more different ways to draw the same effect. This is because some hardware may not support specific effects, it is necessary to implement different versions with the same effect for different levels of hardware.

For example, you may want to implement two versions of a certain effect, one is implemented by the coloring tool and the other is implemented by the fixed function pipeline. In this way, if your graphics card supports the coloring tool, you can use the coloring tool, and use a fixed pipeline in other cases.

This ability of all different versions that can achieve a certain effect in an effect file allows you to completely encapsulate all the effects. encapsulation is also one of the goals of the effect framework.

Each method contains one or more drawing paths (rendering pass ). the draw path encapsulates the device status, samplerger, and (or) the shader used to draw the ry for the specific draw path.

The effect is not limited to the use in a programmable assembly line. For example, the effect can also be used to control the device status (such as lighting, material, and texture) in a fixed functional assembly line.

The reason for using multiple paths is that to implement some special effects, you must draw the same ry multiple times with different rendering statuses and the shader for each path. For example, in Chapter 8th, in order to obtain the mirror reflection effect, different drawing States are used in each frame of the image to draw the same ry multiple times.

For example, in the effect file, there is a framework with two methods. The first method contains a path, and the second method contains two paths.

// Cmdt.txt
Technique t0
{
// First and only pass for this technique
Pass P0
{
... [Specify pass device states, shaders, samplers, etc.]
}
}

Technique T1
{
// First pass
Pass P0
{
... [Specify pass device states, shaders, samplers, etc.]
}
// Second pass
Pass p1
{
... [Specify pass device states, shaders, samplers, etc.]
}
}

More built-in HLSL objects

There are some additional built-in object types in HLSL, which are mainly applied in the performance framework.

Texture object:
The built-in texture object of HLSL represents an idirect3dtexture9 interface object. By using a texture object, you can associate the texture directly with a specific sampling level in the effect file. Texture objects have accessible data members:
# Type Texture type (for example, 2D or 3D)
# Format texture pixel format
# Width: The texture width, in pixels.
# Height the texture height, in pixels
# Depth texture depth value (if the texture is a 3D stereo texture), in pixels

Extended: textures not only store image data, but also store any table information. textures are just a data table, but they do not have to store image data. For example, in the concave-convex texture ing, a line chart (normal map) is used. This line chart is a texture map that saves the normal vectors.

Sampler object and sampler status
The results framework proposes a new keyword sampler_state, through which a sampler object can be initialized (that is, the texture and sampler state can be directly set for a sampler object in the effect file)

Example:
Texture Tex;
Sampler S0 = sampler_state
{
Texture = (Tex );
Minfilter = Linear;
Magfilter = Linear;
Mipfilter = Linear;
}

Above, the texture Tex is associated with the texture layer corresponding to S0, and the sampling level of S0 is set.

Vertex coloring er object and pixel coloring er object

In HLSL, the built-in vertexshader and pixelshader represent vertex pasters and pixel pasters respectively. In the effect frame, they are used to reference a specific vertex or pixel shader used in a specific path. Vertexshader or pixelshader type can be set by using the id3dxeffect method id3dxeffect: setvertexshader and id3dxeffect: setpixelshader in the program.

For example, effect is a valid id3dxeffect object, and vshandle is a handle of the d3dxhandle type that references a vertexshader object in the effect file, in this way, the vertex shader referenced by vshandle can be initialized.
Effect-> setvertexshader (vshandle, );

You can also directly write the vertex coloring tool and the pixel coloring tool into the effect file, and then set the coloring tool variables by using a special compilation syntax.
For example, how to initialize a pixelshader type variable PS
// Define main
Output main (input ){...}

// Compile main:
Pixelshader PS = compile ps_2_0 main ();

After the compile keyword, the version name is specified, and the final entry function of the shader is used. Note that when you use this style to initialize a vertex or pixel shader object, the entry function must be defined in the effect file.

For example, associate a shader with a specific path:
// Define main:
Output main (input ){...}
// Compile main:
Vertexshader Vs = compile vs_2_0 main ();
Pass P0
{
// Set 'vs' as the vertex shader for this pass.
Vertexshader = ();
...
}

Or in a more compact form:
Pass P0
{
// Set the vertex shader whose entry point is 'main () 'as
// Vertex shader for this pass
Vertexshader = compile vs_2_0 main ();
...
}

Note. You can also use the following syntax to initialize vertexshader and pixelshader variables.
Vertexshader Vs = ASM {/* assembly instructions go here */};
Pixelshader PS = ASM {/* assembly instructions go here */};

String:
String filename = "texname.bmp ";
Although the string type is not used by any HLSL function, it can be read by the application, which further encapsulates multiple references into the database file used by one effect, for example, the texture file name and XFile file name.

Note
Annotations are not prepared for HLSL, but can be accessed by applications through the results framework. They only associate the "Description" added by the application for a variable with the variable, available syntax <annotation> Add comments to variables
Example:
Texture tex0 <string name = "tiger.bmp";>;
In this example, the annotation is <string name = "tiger.bmp";>, so that a string (that is, the file name that stores the texture data) can be associated with the variable tex0. Obviously, it is very good to use the file name corresponding to a texture object as the annotation of the object.
The application can obtain comments in the effect file using the following methods:
D3dxhandle id3dxeffect: getannotationbyname (
D3dxhandle hobject, // handle of the parent code segment (parent block, such as method, path, or top-level parameter) where the comment is located
Lpcstr pname // name of the comment whose handle is to be obtained
);
The handle is obtained. You can use the id3deffect: getparameterdesc method to fill in the d3dxconstant_desc structure.

Setting status in the effect File

In general, to correctly implement a certain effect, you must set the setting status (such as the drawing status, texture status, material, illumination, texture, and so on. To support the ability to encapsulate a complete effect in an effect file, the effect framework allows you to set the device status in the effect file. The device status should be set in a code segment of a drawing path,
For example,
State = value;

To view the complete list of statuses, you can enter "States" in the [Index] Tab Of The DirectX SDK documentation and then query: you can also find the path DirectX graphics \ direct3d 9 \ reference \ effect format \ effect states in the Directory tab.

Fillmode status. If you query the status according to the preceding method, you can understand that the value is basically the same as that of the Members in the enumerated d3dfillmode. The only difference is that the former has no prefix d3dfill _, if you look for the enumerated d3dfillmode In the SDK documentation, you will see that this type has the following members: d3dfill_point, d3dfill_wireframe, d3dfill_solid. Therefore, for the effect file, you only need to remove the prefix of these members, the following valid fillmode values are obtained: Point, wireframe, and solid.

For example, use these values in the effect file.
Fillmode = wireframe;
Fillmode = point;
Fillmode = solid;

Create an effect
The effect can be expressed using the id3dxeffet interface, which can be created using the following d3dx method:
Hresult d3dxcreateeffectfromfile (
Lpdirect3ddevice9 pdevice, // set pointer associated with the created id3dxeffect Interface
Lpctstr psrcfile, // name of the text file (effect file) containing the source code of the effect to be compiled
Const d3dxmacro * pdefines, // this parameter is optional
Lpd3dxinclude pinclude, // point to the pointer of the id3dxinclude interface, which is implemented by the application. In this way, you can reload the default include row //. The default behavior has already met the requirements, you can specify the parameter as null.
DWORD flags, // optional marker used to compile the coloring tool in the effect file. 0 indicates no options are used.
// D3dxshader_debug indicates that the compiler writes debugging information.
// D3dxshader_skipvalidation indicates that the compiler does not need to perform any code verification. this parameter is used only when you are using a determined /// colorant.
// D3dxshader_skipoptimization instructs the compiler not to optimize the Code. In fact, this option is only useful during debugging. This is because you do not want the compiler to change the code during debugging.
Lpd3dxeffectpool ppool, // this parameter is optional. It is a pointer to the id3dxeffectpool interface, which defines how the effect parameter is shared by // other effect instances. this parameter is null, it indicates that this parameter is not shared between effect files.
Lpd3dxeffect * ppeffect, // returns a pointer to the id3dxeffect interface, which represents the created Effect
Lpd3dxbuffer * ppcompliationerrors // returns a pointer to the id3dxbuffer interface, which contains a string that stores error codes // and messages.
);

For example, a function d3dxcreateeffectfromfile is called.
// Create Effect
Id3dxbuffer * errorbuffer = 0;
HR = d3dxcreateeffectfromfile (
Device,
"Maid ",
0,
0,
D3dxshader_debug,
0,
& Effect,
& Errorbuffer
);

// Output any error message
If (errorbuffer)
{
: MessageBox (0, (char *) errorbuffer-> getbufferpointer (), 0, 0 );
D3d: Release <id3dxbuffer *> (errorbuffer );
}

If (failed (HR ))
{
: MessageBox (0, "d3dxcreateeffectfromfile ()-failed", 0, 0 );
Return false;
}

Constant settings

The variables in the source code of the application must be initialized. The id3dxeffect interface has some built-in methods for setting variables.

Hresult id3dxeffect: setfloat (d3dxhandle hparameter, float F); // set the floating point type variable marked with hparameter in the effect file to F
Hresult id3dxeffect: setmatrix (d3dxhandle hparameter, const d3dxmatrix * pmatrix); // set the matrix variable marked with hparameter in the effect file to the value pointed to by pmatrix
Hresult id3dxeffect: setstring (d3dxhandle hparameter, const lpcstr pstring); // set the string variable identified by hparameter in the effect file to the value pointed to by pstring.
Hresult id3dxeffect: settexture (d3dxhandle hparameter, lpdirect3dbasetexture9 ptexture); // set the texture variable identified by hparameter in the effect file to the value pointed to by ptexture
Hresult id3dxeffect: setvector (d3dxhandle hparameter, const d3dxvector4 * pvector );
Hresult id3dxeffect: setvectorshader (d3dxhandle hparameter, lpdirect3dvertexshader9 pvectorshader );
Hresult id3dxeffect: setpixelshader (d3dxhandle hparameter, lpdirect3dpixelshader9 ppshader );

You can use the following method to obtain the variable handle in the effect file:
D3dxhandle id3dxeffect: getparameterbyname (
D3dxhandle hparameter, // scope of Variable-parent structure
Lpcstr pname // name of the Variable
);

The signature of this function is exactly the same as that of the id3dxconstanttable: getconstantbyname method.
For example, if you set the variables in the effect file
// Some data to set

D3dxmatrix m;
D3dxmatrixidentity (& M );

D3dxvector4 color (1.0f, 0.0f, 1.0f, 1.0f );
Idirect3dtexture9 * Tex = 0;
D3dxcreatetexturefromfile (device, "shade.bmp", & Tex );

// Get handles to parameters
D3dxhandle matrixhandle = effect-> getparameterbyname (0, "matrix ");
D3dxhandle mtrlhandle = effect-> getparameterbyname (0, "mtrl ");
D3dxhandle texhandle = effect-> getparameterbyname (0, "Tex ");

// Set parameters
Effect-> setmatrix (matrixhandle, & M );
Effect-> setvector (matrixhandle, & color );
Effect-> settexture (texhandle, Tex );

Note that each id3dxeffect: Set * method has an id3dxeffet: Get * method, which can be used to obtain the value of the effect variable in the effect file.
For example, to obtain the value of a matrix variable, use this method:
Hresult id3dxeffect: getmatrix (d3dxhandle hparameter, d3dxmatrix * pmatrix );

Use an effect

1. Obtain the handle of the method to be used in the effect file.
2. Activate the desired Method
3. Enable the active method
4. Draw the target ry based on each draw path in the active method. One method may contain multiple draw paths. Therefore, you must draw the ry once in each draw path.
5. Method of terminating the current active state

Result handle acquisition
The first step to using a method is to obtain the d3dxhandle handle of the method.
D3dxhandle id3dxeffect: gettechniquebyname (lpcstr pname );

Note: In an application, an effect file generally contains multiple methods. Each method corresponds to a specific subset of hardware performance. Therefore, applications usually need to perform some hardware performance tests in the system, and then select the best method based on the tests.

Effect Activation
Obtain the handle of the method used, and then activate the method.
Hresult id3dxeffect: settechnique (d3dxhandle htechnique );

Note: before activating a method, you must use the current settings to verify it. That is, you need to ensure that the hardware supports the features required for this method and the configuration of these features. The verification is completed using the following methods:
Hresult id3dxeffect: validatetechnique (d3dxhandle htechnique );

Enable Effects
To use a certain effect to draw a ry, you must write all the call operations for the plotting function between the id3dxeffect: Begin and id3dxeffect: end. These functions are enabled and disabled respectively.
Hresult id3dxeffect: Begin (
Uint * ppasses, // returns the number of paths in the currently active method
DWORD flags // 0, indicating that the effect needs to save the current device status and the Shadow status, and restore these statuses after the effect is completed (after the id3dxeffect: end function is called, d3dxfx_donotsavestate indicates that the effect is not saved and you do not need to restore the set state (except for the color State)
// D3dxfx_donotsaveshaderstate indicates that the effect is not saved or you do not need to restore the coloring ER status.
);

Settings of the current drawing path

You must specify the painting path to be used before using an effect to draw any ry. As mentioned above, a method may contain one or more drawing paths, each of which encapsulates different device statuses, samplerases, or the coloring devices used in that path. To specify an active path in an application, call the id3dxeffect: beginpass method before creating a ry. This method receives a parameter that identifies the active path, after the ry is drawn, you must call the id3dxeffect: endpass method to terminate the current active path, that is, the id3dxeffect: beginpass and id3dxeffect: endpass methods must appear in pairs, the actual drawn code should be placed between the two functions, and the two functions must be located between the function pair id3dxeffect: Begin and id3dxeffect: end.

Inpass and endpass prototype
Hresult id3dxeffect: beginpass (uint pass );
Hresult endpass ();

Effect termination
Finally, after drawing the ry in each path, call the id3dxeffect: end function to disable or terminate the effect.
Hresult id3dxeffect: end (void );

Example: demonstrate the above five steps for using an effect
// In effect File
Technique t0
{
Pass P0
{
...
}

}

// In application source code:
// Get technique handle

D3dxhandle htech = 0;
Htech = effect-> gettechniquebyname ("t0 ");

// Activate Technique
Effect-> settechnique (htech );

// Begin the active technique.
Uint numpasses = 0;
Effect-> begin (& numpasses, 0 );

// For each rendering pass
For (INT I = 0; I <numpasses; I ++)
{
// Set the current pass
Effect-> beginpass (I );

// Render the geometry for the ith pass
Sphere-> draw ();

// End current active pass
Effect-> endpass ();
}
// End the effect
Effect-> end ();

(End)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.