Tutorials to .com

Tutorials to .com » Software » Game-developer » Shaders and effects - 1.3 Variable types

Shaders and effects - 1.3 Variable types

Print View , by: iSee ,Total views: 14 ,Word Count: 1829 ,Date: Sun, 31 May 2009 Time: 8:07 PM

1.3 Variable types

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

Note: In addition to the following section describes the types of things, HLSL and some built-in object types (such as: texture object (texture object)). However, because these objects primarily for the effect of the type of framework, we will be postponed until its Chapter 4 discussion.

1.3.1 Numerical types

HLSL supports the following numerical type (scalar type):

n bool - True or false value. Note, HLSL provides keywords true and false

n int - 32 bit signed integer

n half - 16 bit floating point

n float - 32 bit floating point

n double - 64 bit floating point

Note: Some platforms may not support int, half and double. If this is the case, these types of simulation will be used to float.

1.3.2 Vector Type

HLSL has the following built-in vector type (vector type):

n vector - the weight for the float type of 4D vector

n vector <T, n> - a n-dimensional vector, for each of its component values (scalar) type T. n-dimensional must be between 1-4. Here is a 2D double vector:

vector <double, 2> vec2;

We can use the array syntax of subject access to a weight vector. For example, to set up the first vec vector of weight i, we can write:

vec [i] = 2.0f;

In addition, we can access structure members to visit a weight vector vec, the use of the definition of the weight of x, y, z, w, r, g, b and a.

vec.x = vec.r = 1.0f;

vec.y = vec.g = 2.0f;

vec.z = vec.b = 3.0f;

vec.w = vec.a = 4.0f;

Known as the r, g, b and a corresponding weight of x, y, z and w the weight. When using vectors to show that when the color, RGBA vector symbols more in line with the fact that the representative of a color.

Alternatively, we can use some other pre-defined, respectively, used to represent the 2D, 3D and 4D vector of the type:

float2 vec2;

float3 vec3;

float4 vec4;

Consider the vector u = (ux, uy, uz, uw), we assume that all u want to copy to a component as v = (ux, uy, uy, uw) such a vector v. The most direct way may be a separate on-demand copy from u to v for each component. In any case, HLSL provides a special syntax to do this type of copy work of disorder, it is called "cocktail" (swizzles):

vector u = (l.0f, 2.0f, 3.0f, 4.0f);

vector v = (0.0f, 0.0f, 5.0f, 6.0f);

v = u.xyyw; / / v = (1.0f, 2.0f, 2.0f, 4.0f)

Copy of the array, we do not have to copy each component End. For example, we can only copy of x and y components, the code segment, for example as follows:

vector u = (1.0f, 2.0f, 3.0f, 4.0f);

vector v = (0.0f, 0.0f, 5.0f, 6.0f);

v.xy = u; / / v = (l.0f, 2.0f, 5.0f, 6.0f)

1.3.3 Matrix type

Matrix has the following built-in HLSL type:

n matrix - a 4 × 4 matrix, its type is float

n matrix <T, m, n> - an m × n matrix, its value type of type T. Matrix of dimension m and n must be between 1-4. Here is a 2 × 2 integer matrix example:

matrix <int, 2, 2> m2x2;

Alternatively, we can define an m × n matrix, the m and n in between 1-4, use the following syntax:

floatmxn matmxn;


float2x2 mat2x2;

float3x3 mat3x3;

float4x4 mat4x4;

float2x4 mat2x4;

Note: The type must be type float - we can use other types. For example, we can use integer, wrote this:

int2x2 i2x2;

int2x2 i3x3;

int2x2 i2x4;

We can use two-dimensional array of law under the slogan of the visit matrix. For example, to set the matrix M of the first i, j of item, we can write:

M [i] [j] = value;

In addition, we can visit as a member of the structure of the visit of the M matrix. Defined the following entries:

1 for the base:

M._11 = M._12 = M._13 = M._14 = 0.0f;

M._21 = M._22 = M._23 = M._24 = 0.0f;

M._31 = M._32 = M._33 = M._34 = 0.0f;

M._41 = M._42 = M._43 = M._44 = 0.0f;

To 0 for the base:

M._m00 = M._m01 = M._m02 = M._m03 = 0.0f;

M._m10 = M._m11 = M._m12 = M._m13 = 0.0f;

M._m20 = M._m21 = M._m22 = M._m23 = 0.0f;

M._m30 = M._m31 = M._m32 = M._m33 = 0.0f;

Sometimes, we want to access a specific matrix line. We can use an array of one-dimensional method to do the next slogan. For example, to quote the matrix M in the first line of the vector i, we can write:

vector ithRow = M [i]; / / get the ith row vector in M

Note: You can use two types of syntax in the initialization variables in HLSL:

vector u = (0.6f, 0.3f, 1.0f, 1.0f);

vector v = (1.0f, 5.0f, 0.2f, 1.0f);

Can also be, equivalent to the use of structure-style syntax:

vector u = vector (0.6f, 0.3f, 1.0f, 1.0f);

vector v = vector (1.0f, 5.0f, 0.2f, 1.0f);

Some other examples:

float2x2 f2x2 = float2x2 (1.0f, 2.0f, 3.0f, 4.0f);

int2x2 m = (1, 2, 3, 4);

int n = int (5);

int a = (5);

float3 x = float3 (0, 0, 0);

1.3.4 array

We can use similar to c + + syntax statement a specific type of an array. For example:

float M [4] [4];

half p [4];

vector v [12];

1.3.5 Structure

The definition of the structure and in the C++ + + in the same. However, HLSL where members of the structure can not function. This is a structure in the HLSL examples:

struct MyStruct


matrix T;

vector n;

float f;

int x;

bool b;


MyStruct s; / / instantiate

sf = 5.0f; / / member access

1.3.6 typedef keyword

Typedef keyword in HLSL functions and C + + in exactly the same. For example, we can give the type of vector <float, 3> named with the following syntax:

typedef vector <float, 3> point;

Then, do not have to read:

vector <float, 3> myPoint;

... ... We just need to read:

point myPoint;

Here are two other examples, it demonstrates how the use of constants and array type typedef keyword:

typedef const float CFLOAT;

typedef float point2 [2];

1.3.7 variable prefix

Can do the following keywords prefix variable declarations:

n static - if the global variables with static keyword prefix, it means that it is not exposed to outside the shader. In other words, it is a partial shader. If a local variable as a prefix to the keyword static, it is, and C + + in the static local variables the same act. In other words, the variables in the function the first time when he was a one-time initialization, and then in all function calls to maintain its value. If the variable has not been initialized, it automatically initialized to 0.

static int x = 5;

n uniform - in uniform if the variable for the prefix keyword, it means that this variable is initialized outside the shader, for example, by C + + application program to initialize, and then input into the shader.

n extern - if the variable for the prefix to the keyword extern, it means that the variables can be accessed outside the shader, for example, by C + + applications. Only global variables can be for the prefix extern keyword. Is not static is the default global variable extern.

n shared - if a shared variable for the prefix keyword on the prompt effect of the framework (see 19 chapters): the variable will be shared between a number of effects. Only global variables can be shared as a prefix.

n volatile - a volatile keyword if the variable for the prefix, prompt effect on the framework (see Chapter 19): the variable will be modified from time to time. Only global variables can be volatile as a prefix.

n const - HLSL and the const keyword in C + + means the same as. In other words, if the variable for the prefix to const, then this variable is constant, and can not be changed.

const float pi = 3.14f;

[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


No comments posted.