[每日Shader]ShaderX.2.Introductions and Tutorials with DirectX 9 (7)Language Basics-3

时间:2021-10-25 04:38:13

Type Modifiers 类型修饰符

There are a couple of optional type modifiers in the HLSL that

you may want to use in your shaders. The familiar const type

modifier is used to specify a variable whose value cannot be

changed by the shader code. Using such a variable on the left side

of an assignment (i.e., as an lval) will result in a compilation error.

 

你可以在你的着色器代码中使用大量的类型修饰符。我们熟悉的const类型就是被用来指定一个变量,使它的值不能被着色器代码所修改。在赋值语句的左边使用这样一个变量会导致编译错误。

 

The row_major and col_major type modifiers can be used to

specify the expected layout of a matrix within the hardware constant

store. The row_major type modifier indicates that each row of

the matrix will be stored in a single constant register. Likewise,

using col_major indicates that each column of the matrix will be

stored in a single constant register. Column major is the default.

 

Row_majorcol_major类型修饰符可以被用来指定矩阵的布局在硬件常量的存储。row_major类型修饰符表示矩阵的每一行将会被存储在一个单个的常量寄存器内。类似的,col_major类型修饰符表示矩阵的每列将会被单独存放在一个常量寄存器内。列主方式是默认的方式。

 

Storage Class Modifiers 存储类修饰符

Storage class modifiers inform the compiler about the intended

scope and lifetime of a given variable. These modifiers are

optional and may appear in any order, as long as they appear

before the variable type.

 

存储类型修饰符通知编译器关于未来的域和给定变量的生命期。这些修饰符是可选的,可以以任何顺序出现,只要它们出现在变量类型的前面。

 

As in C, a variable may be declared as static or extern.

(These two modifiers are mutually exclusive.) At global scope, the

static storage class modifier indicates that the variable is only to

be accessed by the shader and not by the application via the API.

Any non-static variable that is declared at global scope may be

modified by the application through the API. As with C, using the

static modifier at local scope indicates that the variable contains

data that is to persist between invocations of the declaring

function.

 

像在C语言里面一样,一个变量可以被声明为static或者extern类型。(这两个修饰符是互斥的)。在全局域中,static存储类修饰符表示变量仅能够被着色器代码自己存取,不能够被应用程序通过API存取。任何声明在全局域中的非static变量可以被应用程序通过API进行修改。将在C语言里面一样,使用static修饰符在局部域内表示变量包含数据,这些数据会在声明函数的调用期间内存在。

 

The extern modifier can be used on a global variable to indicate

that it can be modified from outside of the shader via the API.

This is redundant, however, as this is the default behavior for variables

declared at global scope.

 

Extern修饰符可以被用在全局变量上,表示它能够被应用程序从外部通过API进行修改。但是这是多余的,因为这是声明在全局域中的变量的默认行为。

 

The shared modifier is used to specify that a given global variable

is to be shared between effects.

 

Shared修饰符被用来表示一个给定的全局变量可以在效果文件*享。

 

A variable that is uniform is assumed to have been set externally

to the HLSL shader (i.e., via the Set*ShaderConstant*()

API). Global variables are treated as if they were declared uniform.

Such variables are not assumed to be const, however, as their values

can be modified in the shader.

 

一个由uniform类型修饰的变量被假设已经从着色器代码外部进行了设置。例如,通过Set*ShaderConstant*() API。全局变量被当作已经用uniform修饰符进行了修饰。这样的变量不能被假设成为const类型,因为它们的值可以在着色器代码内部被修改。

 

For example, say you declare the following variables at global

scope:

 

例如,假设你在全局中声明了下列变量:

extern float translucencyCoeff;

const float gloss_bias;

static float gloss_scale;

float diffuse;

 

The variables diffuse and translucencyCoeff are settable by the

Set*ShaderConstant*() API and can be modified by the shader

itself. The const variable gloss_bias is settable by the Set*Shader-

Constant*() API but cannot be modified in the shader code.

Finally, the static variable gloss_scale is not settable by the

Set*ShaderConstant*() API but can be modified within the shader

only.

 

变量diffusetranslucencyCoeff可以使用Set*ShaderConstant*() API

进行设置,而且可以从着色器代码内部进行修改。const 变量gloss_bias

可以使用Set*ShaderConstant*() API进行设置,但是不能从着色器代码

内部进行修改。最后,static变量gloss_scale不能使用Set*ShaderConstant*()

API进行设置,但可以从着色器代码内部进行修改。

 

Initializers 初始化

As we have shown in some of the preceding examples, it is possible

to initialize variables at declaration time in the same manner

used in C. For example:

 

如我们在前面的例程中所看到的某些代码,可以在声明变量的时刻

用和在C语言中相同的方式初始化变量。例如:

 

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;