Tutorials to .com

Tutorials to .com » Software » Game-developer » Shaders and effects - the preparation of HLSL shader 1.1

Shaders and effects - the preparation of HLSL shader 1.1

Print View , by: iSee ,Total views: 21 ,Word Count: 1757 ,Date: Thu, 28 May 2009 Time: 11:42 AM

The preparation of HLSL shader 1.1

Read this article indicates that you have agreed to the statement at the end

We can directly HLSL shader code string as the preparation of a long list of applications to our source file. However, more convenient and modular approach is the shader code from the application code separate. Therefore, we can be prepared in Notepad in our shader code, and into the general preservation of the ASCII text file. And then use D3DXCompileShaderFromFile function (section 1.2.2) to compile our shaders.

As introduced, the following gives a brief prepared by HLSL vertex shader, and its source file is generated from the text file from Transform.txt. Complete the project can be found under the heading Transform. Vertex shader through a combination of view and projection matrix transformation (transform) vertex, and vertex color set of diffuse reflectance components (the diffuse color component of the vertex) for the blue.

Note: This example uses vertex shader as an example, but the vertex shader pipes do not worry what to do, they will in the next chapter describe. As for now, you have to do is familiar with the procedures HLSL grammar and format.

////////////////////////////////////////////////// ///////////////////

/ /

/ / 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 projection

/ / 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 only.

struct VS_INPUT

(

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 VS_OUTPUT

(

vector 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 colored vertex.

return output;

)

1.1.1 Global Variables

First, we initialize two global variables:

matrix ViewProjMatrix;

vector Blue = (0.0f, 0.0f, 1.0f, 1.0f);

The first variable, ViewProjMatrix, is the matrix type, HLSL it is a built-in 4 × 4 matrix type. The variables view and projection matrix storage combination colleagues so that it describes the transformation of these two. In this way so that we only use a vector matrix multiplication (vector-matrix multiplicatio), rather than two. It should be noted that the source code shader do not have any place to initialize the variables. That is because we it is we in the application's source code in the set - and not in the shader. Shader applications and direct communication is a common operation, as detailed in section 1.2.1.

The second variable, Blue, is the built-in vector types, it is a 4D array. We will be simple ingredients (Translator's Note: This refers to the composition of the diffuse reflection vertex color components) is initialized for the blue, as an array of RGBA colors.

1.1.2 Input and output structure

Statement in the global variable, we define two special structure, we call them "input" (input) and "output" (output) structure. The vertex shader is concerned, the definition of these structures, respectively, of our shaders to be the culmination of input and output data (vertex data).

struct VS_INPUT

(

vector position: POSITION;

);

struct VS_OUTPUT

(

vector position: POSITION;

vector diffuse: COLOR;

);

Note: pixel data (pixel data) from the pixel shader input and output structure definition.

In this case, we enter into the vertex of the vertex shader includes only the components of a location (position component). We output shader contains a location component and a color component.

Special representative of the colon, a semantics, it is used to specify the use of variables. This is similar to the free vertex format (flexible vertex format, referred to as FVF) vertex structure. For example, in VS_INPUT, a member of:

vector position: POSITION;

": POSITION" syntax that is used to describe the position vector input the location of the vertex. In another example, in VS_OUTPUT, we have:

vector diffuse: COLOR;

Here, ": COLOR" said vector is used to describe the output diffuse color of the vertex. The next two chapters we will discuss in detail the use of identifier can be used (usage identifier).

Note: from the bottom point of view, syntax and semantics of variables associated with the hardware registers. In other words, the input variables associated with the input register, the output variables associated with the output register. For example, VS_INPUT members and vertex position of the input location register (the vertex input position register) connected. Similar, diffuse peak with a specific color output register (vertex output color register) connected.

1.1.3 entry point function

And a c + + program, each has a HLSL procedure entry point. In our example shader, the entry point function Main; However, this name is not mandatory. Shader entry point function name can be any valid function names. Entry point function must have input structure parameters, for transmission to the vertex shader input. This entry point must return to the example of an output structure (instance), it is used for output from the shader vertex processed.

VS_OUTPUT Main (VS_INPUT input)

(

Note: In fact, the structure of input and output use is not mandatory. For example, you can sometimes see similar syntax like the following, especially in the pixel shader in:

float4 Main (in float2 base: TEXCOORD0,

in float2 spot: TEXCOORD1,

in float2 text: TEXCOORD2): COLOR

(

...

)

Three parameters into the shader in this case the importation of three texture coordinates. The return of shaders, only a single color, its function signature (function signature) later: COLOR grammar for instructions. 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)

(

...

)

Entry point function of the function body has the responsibility to calculate the input given the output of vertex vertex. In this case a simple transformation shader input vertices to view space and projection space for the vertex color blue, and return as a result of the vertex. First of all, we initialize an instance of VS_OUTPUT all members of its own zero.

VS_OUTPUT output = (VS_OUTPUT) 0; / / zero out all members

Then we use the shader function through mul transform input variables ViewProjMatrix vertex position, mul function is a built-in function, it can perform the matrix vector multiplication (vector-matrix multiplication), and can perform the matrix multiplication matrix (matrix -matrix multiplication). Our position in the output variables have been saved a member of the vertex transformation vector:

/ / 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 blue

output.diffuse = Blue;

Finally, we return our resulting vertex:

return output;

)

[Statement]: In this paper, Frank Luna translated from the "Introduction to 3D Game Programming with DirectX 9.0", limited to the level translator, the text of the inevitable mistakes, criticism welcome all friends; this article only for the exchange of learning and reference purposes, shall not be used in any form of commercial use; For reprint take prior consent of the author and the translator's consent, to maintain the integrity of the article, and indicate the author, translator and the source, who violate the terms of the consequences of the above, the translator of this no responsibility. My email address is Raymond_King123@hotmail.com, welcome the love of 3D graphics and games, and graphics programming experience in a certain friend a letter from the exchange.


Game Developer Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.