DirectX 9 High-rise Coloring Language Introduction 2 - Language Basics (1)

xiaoxiao2021-03-06  76

Language foundation

Now that you have learned what HLSL vertices and pixel shaders and how they are interacting with low-level assembly shaders, we will discuss details of some languages ​​themselves.

Keyword

The keyword is a predefined identifier whose HLSL language is reserved and cannot be used as an identifier in your program. Keywords marked with '*' are not case sensitive.

Table 2. Keywords reserved by HLSL language

asm * boolcompileconstdecl * dodoubleelseexternfalsefloatforhalfifininlineinoutintmatrix * outpass * pixelshader * returnsamplersharedstaticstring * structtechnique * texture * truetypedefuniformvector * vertexshader * voidvolatilewhile

The following keywords are currently not used, but they are reserved for future use:

Table 3. Reserved Keywords currently not used

autobreakcompileconstcharclasscasecatchdefaultdeleteconst_castcontinueexplicitfrienddynamic_castenummutablenamespacegotolongprivateprotectednewoperatorreinterpret_castshortpublicregisterstatic_castswitchsignedsizeofthrowtrytemplatethistypenameunsignedusingunionvirtual

type of data

HLSL supports various data types, from simple scales to more complex types such as vector and matrices.

Scalar type

Language supports the following scalar data type:

Table 4. Scalar data type

BOOLTRUE OR FALSEINT32-bit Signed Integerhalf16-bit floating point valuefloat32-bit floating point valuedouble64-bit floating point value

If you are familiar with the compilation layer programming model, you will know that not all graphics processors are born to support these data types. Therefore, integers may need to be simulated by floating point hardware. This means that an integer operation that exceeds an integer (represented by floating point numbers on these platforms) is operated as expected. In addition, not all object platforms support half precision or double precision values. If you do not support, the number of single-precision floating point will be used.

Vector type

Vector variables are often declared in the HLSL shader. Disclaimer These vectors have many methods, including the following:

Table 5. Vector Type

Vector a four-dimensional vector; each dimension is the floating point type vector dimension is vector; each dimension is Type type

The most common method of disclaimer is the integer of 2-4 after the type. For example, to declare a 4 yuan group single-precision floating point, you can declare the following:

FLOAT4 FVECTOR0;

FLOAT FVECTOR1 [4];

Vector FVECTOR2;

Vector fvector3;

For example, to declare a 3 yuan group Boolean, you can declare the following:

Bool3 Bvector0;

Bool BVector1 [3];

Vector BVector2;

Once a vector is defined, you can access its separate dimension by using a syntax similar to access arrays or using a SWIZZLE. In this SWIZZLE example, the dimension must come from {x. Y, z, w} or {r, g, b, and a} namespace (but not both). E.g:

FLOAT4 POS = {3.0F, 5.0F, 2.0F, 1.0F};

FLOAT VALUE0 = POS [0]; // Value0 IS 3.0F

FLOAT VALUE1 = POS.X; // Value1 IS 3.0F

Float value2 = pos.g; // value2 is 5.0f

FLOAT2 VEC0 = pos.xy; // VEC0 IS {3.0F, 5.0F}

FLOAT2 VEC1 = POS.RY; // Invalid Because of Bad SWIZLE

It should be noted that the PS_2_0 and the lower pixel shader model do not support Arbitrary Swizzles (Translator Note: Arbitrary Swizzle can actually be considered as a "Modifier), which is used to modify the instructions and registers. Its main features It is reduced in the number of instructions used in a shader, thereby increasing efficiency.) Therefore, when compiling into these objects, the originally concise high-level code (using SWIZLE) may become a fairly difficult to understand binary assembly code. You should be familiar with Swizzle you can use in these assembly models.

Matrix type

The type of variable that is often used in the HLSL shader is a matrix, which is a two-dimensional array. Like the scale and vector, the matrix consists of some other basic data types: Boolean, integer, semi-precision, single-precision or double precision floating point. The matrix can be arbitrary, but the matrix of 4 rows 4 columns is usually used. You can call an example of the top vertex shader in this chapter, declare two 4 × 4 single-precision floating-point matrices in the global scope:

Float4x4 view_proj_matrix;

FLOAT4X4 TEXTURE_MATRIX0;

Naturally, other dimensions can also be used. For example, we declare a single 4-column single-precision floating-point matrix:

FLOAT3X4 MAT0;

Matrix mat1;

Like the vector, you can use a single element in the matrix using an array or structural / swizzle. For example, to access the upper left corner element of the matrix view_proj_matrix can use the method of the inventive group as the inventory:

FLOAT FVALUE = view_proj_matrix [0] [0];

Also useful to the syntax of the structure, defining structures are due to access and SWIZZLING OF matrix elements. The ranchers starting from 0, as follows:

_M00, _M01, _M02, _M03

_M10, _M11, _m12, _m13

_M20, _M21, _m22, _m23

_M30, _M31, _M32, _M33

The ranchers starting from 1, as follows:

_11, _12, _13, _14

_21, _22, _23, _24

_31, _32, _33, _34

_41, _42, _43, _44

You can also use array symbols to access matrices: for example:

FLOAT2X2 FMAT = {3.0F, 5.0F, // ROW 1

2.0F, 1.0F}; // row 2

Float value0 = fmat [0]; // value0 is 3.0f

FLOAT VALUE1 = FMAT._M00; // Value1 IS 3.0F

FLOAT VALUE2 = FMAT._12 // Value2 IS 5.0F

Float value3 = fmat [1] [1] // value3 is 1.0F

FLOAT2 VEC0 = FMAT._21_22; // VEC0 IS {2.0F, 1.0F}

Float2 vec1 = fmat [1]; // vec1 IS {2.0F, 1.0F}

original:

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

New Post(0)