Advanced coloring language HLSL (1

Source: Internet
Author: User

We wroteProgramVertices and pixels are very small objects.GPUIs part of the fixed function pipeline. We use our own coloring program to replace some of the fixed function pipelines, which gives us great flexibility in rendering results. We are no longer limited to predefined"Fixed"Operation.

A high-level shading language (HLSL) is required to write a coloring program ). InDirectX 8Medium, the colorant is written in the assembly language of the low-level colorant. Fortunately, we don't have to use assembly languages to write down the shadow,DirectX 9Supports writing in an advanced coloring machine language. UseHLSLThe same advantages of writing a coloring er program as using advanced languages, suchC ++Beyond the assembly language, that is:

Increase Productivity-Advanced languages are faster and easier to write programs than low-level languages. We can spend more time focusing onAlgorithmInsteadCode.

Increase readability-Programs Written in advanced languages are easier to read, which means programming in advanced languages is easier to debug and maintain.

In most cases, the compilation code generated by the compiler is more efficient than that produced by handwriting.

UseHLSLCompiler, We can compile our code to any availableShaderVersion, we will have to port the code for each required version using the assembly language.

HLSLSameCAndC ++Similar syntax,Therefore, the learning curve is shortened.

Finally, if your video card does not support vertices and pixel pasters, you will need to convert them to execute the example program of the shader.RefDevice. UseRefThe device means that the example of the coloring tool runs slowly, but it can display the result at least, so let's check whether the code can be executed.

Tip: vertexShadersSoftware can be used for simulation ――D3DCREATE_SOFTWARE_VERTEX-PROCESSING.


16.1 WriteHLSL Coloring er

We can directly write long strings in the program source fileHLSLColoring erCode, but the more convenient and modular way is to separate it from the program code. Therefore, we writeColoring erAnd save it as a generalASCIIText file, and then you can useD3dxcompileshaderfromfileFunction(Section 16.2.2)To compile them.

As an introduction, we useHLSLA simpleVertexColoring tool, which is generated and saved as a text file "vertexshader. cxx" in notepad ".Vertex coloring tool converts vertices with a combination view and Projection MatrixAnd set the vertex diffuse lightRedColor.

Note: This is an example of a vertex coloring machine. You don't have to worry about what the vertex coloring machine has done. The current goal is to be familiar with it.HLSLProgramming syntax and format.

/*************************************** **************************************** *****
Vertex shader that transforms a vertex by the view and projection transformation,
And sets the vertex color to red.
**************************************** **************************************** ****/

// Global variable to store a combined view and projection transformation matrix,
// We initialize this variable from the application.
Matrix g_view_proj_matrix;

// Initialize a global blue color vector
Const Vector red = {1.0f, 0.0f, 0.0f, 1.0f };

// Input Structure describes the vertex that is input into the shader.
// Here the input vertex contains a position component only.
Struct Svertexinput
{
Vector position: position;
};

// Output structure describes the vertex that is output from the shader.
// Here the output vertex contains a position and color component.
Struct Svertexoutput
{
Vector position: position;
Vector diffuse: color;
};

// Main entry point, observe the main function when es a copy of the input vertex through
// Its Parameter and returns a copy of the output vertex It computes.
Svertexoutput main (svertexinput input)
{
// Zero out members of output
Svertexoutput output = (svertexoutput) 0;

// Transform to view space and Project
Output. Position = MUL (input. Position, g_view_proj_matrix );

// Set vertex diffuse color to blue
Output. Diffuse = red;

Return Output;
} 16.1.1 Global Variables

First2Global variables:
// Global variable to store a combined view and projection transformation matrix.
// We initialize this variable from the application.
Matrix g_view_proj_matrix;

// Initialize a global blue color vector.
Const vector Blue = {0.0f, 0.0f, 1.0f, 1.0f };

The1Variable (s)G_view_proj_matrixIs a matrix type, it isHLSLCreated in4×4. This variable stores the matrix of views and projections, which describes the transformation between the two. To use this method, we only need to multiply a vector and a matrix (instead of two ). Note thatSource codeThe variable is not initialized anywhere, because it is set in the application source code, rather than in the shader. It is a common operation to communicate from an application to a colorant program.

Second variableBlueYesBuilt-in(Built-in) Type4dVector, We initialize it to blue, it isRgbaColor vector.

16.1.2 Input and Output Structures

After global variables are defined2Special structure. We call the input and output structures. For vertex pasters, these structures define vertex data, which are:
// Input Structure describes the vertex that is input into the shader.
// Here the input vertex contains a position component only.
Struct svertexinput
{
Vector position: position;
};

// Output structure describes the vertex that is output from the shader.
// Here the output vertex contains a position and color component.
Struct svertexoutput
{
Vector position: position;
Vector diffuse: color;
};

Note: Define Input and Output pixel data for the structure of the pixel shader.

In the example,InputVertex shader only containsLocationMember (Position),OutputVertex shader containsLocation and color members(Position and color).

A special colon is a syntax that declares variables. This correspondsVertexFree vertex format in the structure (Fvf. For example, svertexinput has the following members:Vector position: position;

":Color"It means that the diffuse light of vertices is usedSvertexoutputStructureColorMembers.
Note: At the underlying layer, the semantics and syntax of the shader variables are associated with the hardware registers. That is,InputVariables andInputRegister Association,OutputVariables andOutputRegister Association. For example,SvertexinputInPositionMembers and verticesInputOfPositionRegisters are associated. Similarly,DiffuseAnd VertexOutputOfColorRegister Association.

16.1.3 Function entry point

InC ++In the program, eachHLSLThe program has an entry point. In our example, we call the entry point function.Main. However, the name is not mandatory. The entry point function name can be any valid function name. The entry point function must haveInputStructure parameter, which usesInputThe vertex enters the shader. The entry point function must returnOutputStructure instance, used in the coloring ToolOutputOperation vertex.

Svertexoutput main (svertexinput input)
{

Note: In fact, using the input and output structures is not mandatory. For example, sometimes you will see a syntax similar to the following, especially in the pixel shader:

 

Float4 main (in float2 base: texcoord0,

In float2 spot: texcoord1,

In float2 text: texcoord2): Color

{

...

}

 

In this example, the parameters entered in the coloring tool are three texture coordinates. The color is output (returned) by the color statement after the declaration of the function. This definition is similar:

 

 

Struct Input

 

{

Float2 base: texcoord0;

Float2 spot: texcoord1;

Float2 text: texcoord2;

};

 

Struct output

{

Float4 C: color;

};

 

Output main (input)

{

...

}

The input point function is responsibleInputVertex computingOutputVertex. In this example, the coloring tool is simple transformation.InputSet the vertex colorRedColor, and return the result vertex. First, we defineSvertexoutput instance and initialize all Members0.

// Zero out members of output
Svertexoutput output = (svertexoutput) 0;

Then the coloring er changesInputG_view_proj_matrixVariable,UseMulFunction. It isBuilt-in(Built-in) function to multiply a vector and a matrix, or multiply a matrix and a matrix. We save the result transform vector (inOutputInstancePositionMember ).

// Transform to view space and Project
Output. Position = MUL (input. Position, g_view_proj_matrix );


Then setOutputMemberDiffuseThe color isRedColor:

// Set vertex diffuse color to red
Output. Diffuse = red;

Finally, the returned result vector is:

Return output;
}

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.