Shadder and Effect - 1.1 Write HLSL Shadder

zhaozj2021-02-16  52

1.1 Write HLSL shader

Read this article shows that you have agreed to the statement

We can write the HLSL shader code as a long string string into our application source file. However, more convenient and modular methods are to separate the collillary code from the application code. Therefore, we can write our shading code in Notepad and save it in a general ASCII text file. Then recompile our shading using the D3DxCompileshaderfromFile function (1.2.2).

As an introduction, the vertex shader written in HLSL is given below, which is generated from the text file transform.txt. A complete project can be found under the TRANSFORM title. The vertex shader passes a combination view and the transformal transform vertex and sets the Diffuse Color Component Of the Vertex for the vertex color.

Note: This example uses the vertex shader as an example, but what do you have to do the vertex shader, which will tell them in the next chapter. As for now, what you have to do is familiar with the syntax and format of the HLSL program.

/

//

// file: Transform.txt

//

// Author: Frank D. Luna (c) All Rights Reserved

//

// System: AMD Athlon 1800 XP, 512 DDR, Geforce 3, Windows XP,

// MSVC 7.0

//

// Desc: Vertex Shader That Transforms a Vertex by the view and

// Projection Transformation, and sets the vertex color to blue.

//

/

//

// globals

//

// Global Variable to Store A Combined View and Project

// Transformation Matrix. We Initialize this variable

// from the application.

Matrix ViewProjmatrix;

// Initialize A Global Blue Color Vector.

Vector blue = {0.0F, 0.0F, 1.0F, 1.0F};

//

// structures

//

// INPUT STRUCTURE DESCRIBES The Vertex That IS INPUT

// INTO the shader. Here the Input Vertex Contains

// a position component online.

Struct vs_INPUT

{

Position: position;

}

// Output Structure Describes the Vertex That IS

// Output from the shader. Here The Output

// Vertex Contains a position and color component.

Struct vs_output

{

Position: position;

Vector Diffuse: Color;

}

//

// main entry point, Observe the main function

// Receives a Copy of the Input Vertex Through

// ITS Parameter and Returns a copy of the output // Vertex It Computes.

//

Vs_output main (vs_input input)

{

// Zero Out MEMBERS OF OUTPUT

Vs_output output = (vs_output) 0;

// Transform to View Space and Project

Output.position = MUL (Input.position, ViewProjmatrix);

// set Vertex Diffuse Color to Blue

Output.diffuse = blue;

// Output the projected and color Vertex.

Return Output;

}

1.1.1 global variable

First, we initialize two global variables:

Matrix ViewProjmatrix;

Vector blue = {0.0F, 0.0F, 1.0F, 1.0F};

The first variable, viewprojmatrix, is a Matrix type, which is a 4 × 4 matrix type of HLSL built-in. This variable stores a combination of views and projection matrices so that it describes these two transformations on colleagues. This way allows us to use only one vector matrix multiplicative, not two. It should be noted that no initialization of this variable anywhere in the source code. That is because we are setting up in the source code of the application - not in the shader. Direct communication directly in applications and shader is a common operation, see Section 1.2.1.

The second variable, Blue is a built-in Vector type, which is a 4D array. We simplify its ingredients (translator Note: The ingredients herein refer to the ambient reflection ingredients of the vertex color) are initialized to blue and will be considered as a RGBA color array.

1.1.2 Input and Output Structure

After the global variable declaration, we define two special structures, we call them "Input" and "Output" structure. These structures define our shader to input and output vertex data (Vertex Data).

Struct vs_INPUT

{

Position: position;

}

Struct vs_output

{

Position: position;

Vector Diffuse: Color;

}

Note: Pixel Data is defined by the input and output structure of the pixel shader.

In this example, we enter the vertex of the vertex shader only includes a position component. Our output shader contains a positional component and a color component.

The special colon represents a semantic, which is used to specify the use of variables. This is similar to the vertex structure of the Flexible Vertex Format, referred to as FVF. For example, in VS_INPUT, there is a member:

Position: position;

The ": position" syntax indicates that the vector POSITION is the location for describing the vertices of the input. For example, in VS_OUTPUT, we have:

Vector Diffuse: Color;

Here, ": Color" means vector Diffuse is for describing the color of the output vertex. We will discuss usage Identifier in detail in the next chapter. Note: From the viewpoint of the underlying, the hardware register is associated with the semantics of the variable syntax. That is, the input variable is associated with the input register, and the output variable is associated with the output register. For example, the Position member of VS_INPUT is connected to the vertex input position register (the Vertex Input Position Register). Similarly, DIFFUSE is connected to a Vertex Output Color REGISTER.

1.1.3 Entrance Point Function

As with a C program, each HLSL program has an entry point. In our sample shader, the entry point function is main; however, this name is not mandatory. The entry point function name of the shader can be any valid function name. The entry point function must have an input structural parameter for transmitting the input vertex to the collier. This entry point must also return an instance of an output structure (INSTANCE) that is used to output the processed vertex from the shader.

Vs_output main (vs_input input)

{

Note: In fact, the use of input and output structures is not mandatory. For example, you may sometimes see a syntax similar to that is like below, especially in the pixel shader:

Float4

Main

(in float2 base: texcoord0,

IN float2 spot: texcoord1,

In Float2 Text: Texcoord2): Color

{

...

}

The three parameters are input into the chromor, which is input to three texture coordinates. The shader returns only a separate color, which is used after function signature (Function Signature). Its definition is equivalent to:

Struct Input

{

FLOAT2 BASE: TEXCOORD0;

Float2 spot: texcoord1;

FLOAT2 TEXT: TEXCOORD2;

}

Struct Output

{

Float4 C: Color;

}

OUTPUT

Main

(Input INPUT)

{

...

}

The function of the entry point function is responsible for calculating the output vertex of the given input vertex. In this case, the shader is simple to transform the vertices to the view space and projection space, set the vertex color as blue, and return the vertex as the result. First we initialize a vs_output instance and set all its members.

Vs_output output = (vs_output) 0; // Zero Out All Members

Then our shader uses the MUL function and transforms the input of the vertex via the ViewProjmatrix variable. The MUL function is a built-in function, which can perform the vector matrix multiplication, and can perform matrix multiplication of matrix (Matrix) -matrix multiplication. We save the transformed vertex vector in the Position member of the Output variable:

// Transform and Project

Output.position = MUL (Input.position, ViewProjmatrix);

Next We set The Diffuse Color Member of Output To Blue:

// set Vertex Diffuse Color to Blueoutput.diffuse = Blue;

Finally, We returnur resulting vertex:

Return Output;

}

[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-23185.html

New Post(0)