Advanced coloring language HLSL (4)

Source: Internet
Author: User
Document directory
  • 16.3.2 vector type
  • 16.3.3 Matrix Type
  • 16.3.4 Array
  • 16.3.5 Structure
  • 16.3.6 typedef keyword

Note: In addition to the types described in the following sections, HLSL also has some built-in object types (such as texture objects ).

16.3.1 numeric type

HLSL supports the following numeric types ):

Bool-True or false value. Note that HLSL provides the true and false keywords.

Signed integer int-32bit

Half-16bit floating-point number

Float-32bit floating-point number

Double-64bit floating-point number

Note: Some platforms do not support int, half, and double types. In this case, we use float type simulation.

16.3.2 vector type

HLSL has the following built-in vector types ):

Vector-4D vector with float Components

Vector <t, n> -- an n-dimensional vector. Each component is of the T type. The N-dimensional value must be between 1 and 4. Here is an example of a 2D Double Vector:

Vector <double, 2> vec2;

We can use the syntax below the array to access a component of the vector. For example, to set the I-th component of vector VEC, we can write it:

VEC [I] = 2.0f;

In addition, we can access a component of vector VEC like a member of the access structure, using the defined component names X, Y, Z, W, R, G, B, and.

VEC. x = Vec. r = 1.0f;

VEC. Y = Vec. G = 2.0f;

VEC. z = Vec. B = 3.0f;

VEC. W = Vec. A = 4.0f;

The components of R, G, B, And a correspond to the components of X, Y, Z, and W respectively. When the vector is used to represent the color, the rgba symbol is more suitable because it enhances the color represented by the vector.

We can use other predefined types to represent 2D, 3D, and 4d vectors:

Float2 vec2;

Float3 vec3;

Float4 vec4;

Consider the vector u = (UX, Uy, uz, UW). Suppose we want to copy all the components of u to a V = (UX, Uy, Uy, UW) such a vector v. The most direct method may be to copy each component from u to V one by one. However, HLSL provides a special syntax for unordered copies, called 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}

When copying an array, we do not have to copy each component. For example, we can copy only the X and Y components, and the code segment is 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}

 

16.3.3 Matrix Type

HLSL has the following built-in matrix types:

Matrix -- a 4 × 4 matrix with various types of float

Matrix <t, m, n> -- An m × n matrix. Each member is of the type T. The matrix Dimensions m and n must be between 1 and 4.

Here is an example of a 2x2 integer matrix:

Matrix <int, 2, 2> m2x2;

We can define an m x n matrix with M and N between 1 and 4 using the following syntax:

Floatmxn matmxn;

Instance:

Float2x2 mat2x2;

Float3x3 mat3x3;

Float4x4 mat4x4;

Float2x4 mat2x4;

Note: The type does not need to be float-we can use another type. For example, we can use an integer as follows:

Int2x2 i2x2;

Int2x2 i3x3;

Int2x2 i2x4;

We can use the subscript Syntax of a two-dimensional array to access the items in the matrix. For example, to set the I and j items of matrix m, we can write:

M [I] [J] = value;

In addition, we can access the m items of the matrix as the members of the access structure. The following entries are defined:

1-based:

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;

Based on 0:

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 row in the matrix. We can use the subscript Syntax of a one-dimensional array. For example, to reference the vector of row I in matrix m, we can write:

Vector ithrow = m [I]; // get the ith row vector in m

Note: You can use two syntaxes to initialize variables in HLSL:

Vector u = {0.6f, 0.3f, 1.0f, 1.0f };

Vector v = {1.0f, 5.0f, 0.2f, 1.0f };

Equivalent: Use the constructor Syntax:

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

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

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 );

 

16.3.4 Array

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

Float M [4] [4];

Half P [4];

Vector V [12];

 

16.3.5 Structure

The structure is defined in the same way as in C ++. However, the structure in HLSL cannot have member functions. This is an example of the structure in HLSL:

Struct mystruct

{

Matrix T;

Vector N;

Float F;

Int X;

Bool B;

};

Mystruct s; // instantiate

S. F = 5.0f; // member access

 

16.3.6 typedef keyword

The typedef keyword function of HLSL is exactly the same as that in C ++. For example, we can name the type vector <float, 3> with the following syntax:

Typedef vector <float, 3> point;

Then, you do not need to write it as follows:

Vector <float, 3> mypoint;

...... We only need to write:

Point mypoint;

 

Here are two other examples, which show how to use the typedef keyword for constants and array types:

Typedef const float cfloat;

Typedef float point2 [2];

 

1.3.7 variable prefix

The following keywords can be prefix of variable declaration:

Static -- if it has a static keyword prefix and if it is a global variable, it indicates that it is not exposed to the coloring er. In other words, it is partial to the shader. If a local variable is prefixed with the static keyword, it has the same behavior as the static local variable in C ++. That is to say, this variable is initialized at one time during the first function execution, and then maintained in all function calls. If the variable is not initialized, it is automatically initialized to 0.

Uniform -- if the variable is prefixed with the uniform keyword, it means that the variable is initialized outside the shadow, for example, it is initialized by the C ++ application, and then entered into the shadow.

Extern -- if the variable is prefixed with the extern keyword, it means that the variable can be accessed outside the colorant, for example, by the C ++ application. Only global variables can be prefixed with the extern keyword. If the global variable is not static, the default value is extern.

Shared -- if the variable is prefixed with the shared keyword, it indicates the effect framework: the variable will be shared among multiple effects. Only global variables can be prefixed with shared.

Volatile -- if the variable is prefixed with the volatile keyword, the system prompts the effect framework: the variable will be changed frequently. Only global variables can be prefixed with volatile.

Const -- the const keyword in HLSL is the same as that in C ++. That is to say, if a variable is prefixed with const, it is a constant and cannot be changed.

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.