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

xiaoxiao2021-03-06  89

Type modifier

There are several optional type modifiers in HLSL intended in your shader. Usually set the code modified by the shader to a const (constant) type modifier. A compilation error is generated in the left side of the assignment symbol (for example, as a LVAL).

You can use the Row_major (row priority) type modifier to specify a matrix format in the storage constant hardware. Row_major type modifier indicates that each line in the matrix is ​​stored in a single constant register. Similarly, using a col_major representing each column in the matrix is ​​stored in a single constant register. By default, it is prioritized.

Storage class modifier

Storage class modifier notifies the scope and survival of the compiler to give a variable. These modifiers are optional and can appear in any order before the variable type.

Like a C language, a variable can be declared as static (static variable) or extern (external variable). (These two modifications are mutually exclusive) In the global scope, Static Category modifiers indicate that variables can only be accessed by shader without access by the application. Any non-static variable in global declaration can be modified by the application via an API. Like a C language, the data contained in the local range using static modifiers indicates that the data contained in the declaration function (the translator Note: The survival is globally, the scope is within functions).

Use extern modifiers in the global scope indicating that the external shader can be modified by the API. However, this belongs to this one, because the variable in the global scope statement is the case.

Use Shared modifiers to set global variables that will be shared by two effects.

The prefix is ​​first initialized outside, and then enter the HLSL shader. (For example, through the set * shaderconstant * () API). Declaring global variables as unified. However, since the value can be modified in the shader, it is impossible to be a constant.

For example, assume that you declare the following variables in the global scope:

Extern float translucencycoeff;

Const float gloss_bias;

Static float gloss_scale;

Float Diffuse;

Variables Diffuse and TranslucencyCoeff can be set by Set * ShadercConstant * () API, or by the shader itself. The constant gloss_bias can be set by the set * shadeconstant * () API, but cannot be modified by the shader code. Finally, static variables Gloss_scale cannot be set by set * shaderconstant * () API, but it can only be modified in the shader.

initialization

As shown in the previous example, the habits in the C language can be initialized when the variable declaration is declared. E.g:

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

2.0F, 1.0F}; // row 2

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

FLOAT FFACTOR = 0.2F;

Vector operation

In HLSL, you need to pay attention to some program traps when performing a mathematical operation of the vector. If the shader is written for 3D graphics, most of the profile traps can discover intuition. For example, define a standard binary operator to perform each dimensional operation.

FLOAT4 VTONE = Vbrightness * VEXPOSURE;

Assume that Vbrightness and VEXPOSURE are Float4 types, equivalent to:

Float4 vtone;

vtone.x = vbrightness.x * vexposure.x;

Vtone.y = vbrightness.y * vexposure.y;

Vtone.z = vbrightness.z * vexposure.z;

Vtone.w = vbrightness.w * vexposure.w;

Be careful in the 4D vector Vbrightness and VEXPOSUREs. In addition, multiplying the matrix variables in this manner that does not cause matrix multiplication. Point multiplication and matrix multiplication is achieved by internal function MUL (), which will be discussed later.

Constructor

It is often seen in the HLSL shader to be other language features, and there is similar to C that increases some of the contents of processing complex data types. Examples of constructor use:

FLOAT3 VPOS = float3 (4.0F, 1.0F, 2.0F);

Float fdiffuse = DOT (vnormal, float3 (1.0F, 0.0F, 0.0F));

FLOAT4 VPACK = float4 (VPOS, FDIFFUSE);

The constructor is usually used in: I want to temporarily define a constant (DOT (Vnormal, Float3 (1.0F, 0.0F, 0.0F)))) or wants to explicitly compress a smaller data type. (Float4 (VPOS, FDIFFUSE) on the top). In this example, the constructor float4 receives a float3 type and a FLOAT type while returning a data compressed float4 type. original:

http://msdn.microsoft.com/library/en-us/dnhlsl/html/shaderx2_introductionto.asp

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

New Post(0)