Shadder and Effect - 1.3 Variable Types

zhaozj2021-02-16  51

1.3 variable type

Read this article shows that you have agreed to the statement

Note: In addition to the types described in the following sections, HLSL has some built-in object types (such as texture objects). However, since these object types are primarily used for the effect framework, we will be discussed in Chapter 4.

1.3.1 numeric type

HLSL supports the following numerical types:

n Bool - True or False value. Note that HLSL provides True and false keywords

N INT - 32-bit symbolic integer

N HALF - 16-bit floating point

N float - 32-bit floating point number

N Double - 64-bit floating point number

Note: Some platforms may not support int, half and double. If this is the case, these types will use Float to simulate.

1.3.2 Vector Type

HLSL has the following internal vector types (Vector Type):

n Vector - Different components 4d vector for float types

n Vector - an n-dimensional vector, each of which is a value (scalar) type T. N-dimensional must be between 1 and 4. Here is a 2D Double vector:

Vector vec2;

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

VEC [i] = 2.0f;

In addition, we can access one component of the vector VEC as a member of the access structure, using the defined components 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;

The components of the names R, G, B, and A correspond to the components of X, Y, Z, and W, respectively. When using the vector to represent the color, the RGBA symbol is more in line with this vector representing a color.

As a choice, we can use some other predefined separates to represent 2D, 3D, and 4D vectors:

Float2 vec2;

FLOAT3 VEC3;

Float4 vec4;

Consider the vector u = (UX, UY, UZ, UW), assuming that all components we want to copy u to a vector V like V = (UX, UY, UY, UW). The most direct way may be to copy each component from u to V separately. But in any case, HLSL provides a special syntax to do this type of unordered copy, it is 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 you copy array, we don't have to copy each component. For example, we can copy only 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} 1.3.3 matrix type

HLSL has the following built-in matrix type:

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

n matrix - a M × N matrix, its types are numeric Type T. The matrix dimensions m and n must be between 1 to 4. Here is an example of a 2 × 2 integer matrix:

Matrix m2x2;

As a selection, we can define a M × N matrix, between M and N between 1 to 4, using the following syntax:

FLOATMXN MATMXN;

Example:

FLOAT2X2 MAT2X2;

Float3x3 mat3x3;

FLOAT4X4 MAT4X4;

FLOAT2X4 MAT2X4;

Note: The type does not have to be a Float type - we can use other types. For example, we can use this, write this:

INT2X2 I2X2;

INT2X2 I3X3;

INT2X2 I2X4;

We can access items in the matrix with a two-dimensional number of subscript syntax. For example, to set the first, J item of the matrix M, we can write:

M [I] [J] = Value;

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

Take 1 as 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;

Take 0 as 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 row in the matrix. We can do it with the subscript syntax of the one-dimensional array. For example, to reference the vector of the nest in the matrix M, we can write:

Vector ithrow = m [i]; // Get the ith row vector in m

Note: You can use two syntax to initialize the variable in HLSL:

VECTOR U = {0.6F, 0.3F, 1.0F, 1.0F};

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

Others, equivalence, use the syntax of the constructed style:

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

We can declare a specific type of array with a syntax similar to C . E.g:

Float M [4] [4];

HALF P [4];

Vector v [12];

1.3.5 structure

The definition of the structure is the same as in C . However, the structure in HLSL cannot have a member function. This is an example of a structure in HLSL:

Struct MyStruct

{

Matrix T;

Vector n;

Float f;

INT X;

BOOL B;

}

MyStruct S; // instantiate

S.f = 5.0f; // Member Access

1.3.6 TypedEf keyword

HLSL's typedef keyword function is exactly the same as C . For example, we can give the type vector named the following syntax:

TypedEf Vector Point;

Then, no need to write:

Vector mypoint;

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

Point mypoint;

Here is two other examples, which show how to use TypedEf keywords for constants and array types:

Typedef const float cfloat;

Typedef float point2 [2];

1.3.7 variable prefix

The following keywords can do a variable declaration prefix:

n static - If the global variable with the Static keyword prefix, it means that it is not exposed to the shader. In other words, it is partially shared. If a part variable is prefixed in a static key, it has the same behavior as the Static partial variable in C . That is, the variable is initialized once when the function is executed first, and then maintain its value in all function calls. If the variable is not initialized, it automatically initials to 0.

Static int x = 5;

n uniform - If the variable is prefixed in a UNIFORM keyword, this variable is initialized outside the shader, such as initialization by the C application, and then enter the shader.

n extern - If the variable is prefixed in an extern key, it means that the variable can be accessed outside the shader, such as a C application. Only global variables can be prefixed in an extern key. Not the global variable of static is extern.

n Shared - If the variable is prefixed in a shared keyword, the effect framework is prompted (see Chapter 19): The variable will be shared between multiple effects. Global variables can be prefixed in Shared.

n volatile - If the variable is prefixed in a Volatile keyword, the effect framework (see Chapter 19): Variables will be modified often. Only global variables can be prefixed in volatile.

N const - the meaning of Const keywords in HLSL and the meaning of C . That is, if the variable is prefixed in const, this variable is constant and cannot be changed.

Const float pi = 3.14f;

转载请注明原文地址:https://www.9cbs.com/read-23183.html

New Post(0)