Working with Vectors 使用向量

In HLSL, there are a few "gotchas" to look out for when performing

math on vectors. Fortunately, most of them are quite intuitive,

given that we are writing shaders for 3D graphics. For example,

standard binary operators are defined to work per component:

 

HLSL里,在用向量进行数学计算时有一些*。幸运的是,

当考虑到我们正在为3维图形写着色器代码,它们之中的大

多数都是很直观的。例如,标准二元操作符被定义为对各个

分量进行运算:

 

float4 vTone = vBrightness * vExposure;

 

Assuming vBrightness and vExposure are both of type float4, this

is equivalent to:

 

假设vBrightnessvExposure都是float4类型的,这就等于:

 

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;

 

Note that this is not a dot product between the 4D vectors

vBrightness and vExposure. Additionally, multiplying matrix variables

in this way does not result in a matrix multiply. Dot products

and matrix multiplies are applied via the intrinsic function mul(),

which we discuss later in the chapter.

 

需要注意的是这不是一个在4维向量vBrightness vExposure

之间的点积运算(dot product)。另外,矩阵变量以这种方式

相乘不会得到矩阵相乘的结果(译注:指用mul函数得到的结果)。

点积和矩阵乘法会通过内置函数mul()进行,我们会在本章的晚些

时候进行讨论。

 

 

Constructors 构造函数

 

Another language feature that you often see in HLSL shaders is

the constructor, which is similar to C++ but has some enhancements

to deal with complex data types. Example uses of

constructors include:

 

另一种你可能会经常在着色器代码中看到的语言特征是构造函数。

它和C++类似,但是被加强以处理复杂的数据类型。构造函数的

使用例如:

 

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

 

Constructors are commonly used when a shader writer wants to

temporarily define a quantity with literal values (as in

dot(vNormal, float3(1.0f, 0.0f, 0.0f)) above) or when a shader

writer wants to explicitly pack smaller data types together (as in

float4(vPos, fDiffuse) above). In this case, the float4 constructor

takes in a float3 and a float and returns a float4 with the data

packed together.

 

通常当着色器代码作者想要临时地定义一个使用字面值的变量

(例如上面展示的dot(vNormal, float3(1.0f, 0.0f, 0.0f))或者想要

显式地把小的数据类型组合在一起(例如上面展示的float4(

vPos, fDiffuse))时会使用构造函数。在这种情况下,float4

构造函数将一个float3类型和一个float类型组合在一起而后

返回一个组合好的float4类型变量。

 

 

Type Casting 类型转换

 

To aid in shader writing and the efficiency of the generated code,

it is a good idea to be familiar with HLSL's type casting behavior.

Type casting often happens in order to promote or demote a given

variable to match a variable to which it is being assigned. For

example, in the following case, a literal float 0.0f is being cast to

a float4 {0.0f , 0.0f , 0.0f , 0.0f } to initialize vResult.

 

为了辅助着色器代码的编写和提高产生的代码的效率,熟悉

HLSL的类型转换规则是一个好主意。通常,当为了提升或者

降低一个给定的变量去匹配另一个给定的变量时,会发生类

型转换。例如,在下面的例程里,一个float类型的字面值0.0f

被转换成一个float4类型以初始化变量vResult

 

float4 vResult = 0.0f;

 

Similar casting can occur when assigning a higher dimensional

data type like a vector or matrix to a lower dimensional data type.

In these cases, the extra data is effectively omitted. For example,

we may write the following code:

 

当把一个高维度的数据类型譬如向量或者矩阵转换到一个低

维度数据类型时会发生相似的转换。在这些例程中,额外的数据

被有效地忽略掉了。例如,我们可以写下如下代码:

 

float3 vLight;

float fFinal, fColor;

fFinal = vLight * fColor;

 

In this case, vLight is cast to a float by using only the first component

in the multiply with the scalar float fColor. In this case,

fFinal is equal to vLight.x * fColor.

 

在这种情况下,vLight被转换成一个float,乘法中仅仅使用了第一个分量

float标量fColor相乘。因此,fFinal等于vLight.x * fColor

 

It is a good idea to be familiar with the following table of type

casting rules for HLSL:

 

熟悉下列表中的转换规则是一个好主意:

Type of Cast

Casting Behavior

Scalar-to-scalar

Always valid. When casting from bool type to an integer or

floating-point type, false is considered to be zero and true is

considered to be one. When casting from an integer or

floating-point type to bool, a zero value is considered to be

false and a nonzero value is considered to be true. When

casting from a floating-point type to an integer type, the value

is rounded toward zero. This is the same truncation behavior

as in C.

Scalar-to-vector

Always valid. This cast operates by replicating the scalar to fill

the vector.

Scalar-to-matrix

Always valid. This cast operates by replicating the scalar to fill

the matrix.

Scalar-to-structure

This cast operates by replicating the scalar to fill the structure.

Vector-to-scalar

Always valid. This selects the first component of the vector.

Vector-to-vector

The destination vector must not be larger than the source

vector. The cast operates by keeping the leftmost values and

truncating the rest. For the purposes of this cast, column

matrices, row matrices, and numeric structures are treated as

vectors.

Vector-to-matrix

The size of the vector must be equal to the size of the matrix.

Vector-to-structure

This is valid if the structure is not larger than the vector, and all

components of the structure are numeric.

Matrix-to-scalar

Always valid. This selects the upper-left component of the

matrix.

Matrix-to-vector

The size of the matrix must be equal to the size of the vector.

Matrix-to-matrix

The destination matrix must not be larger than the source

matrix in both dimensions. The cast operates by keeping the

upper-left values and truncating the rest.

Matrix-to-structure

The size of the structure must be equal to the size of the matrix,

and all components of the structure are numeric.

Structure-to-scalar

The structure must contain at least one member.

Structure-to-vector

The structure must be at least the size of the vector. The first

components must be numeric, up to the size of the vector.

Structure-to-matrix

The structure must be at least the size of the matrix. The first

components must be numeric, up to the size of the matrix.

Structure-to-object

The structure must contain at least one member. The type of

this member must be identical to the type of the object.

Structure-to-structure

The destination structure must not be larger than the source

structure. A valid cast must exist between all respective source

and destination components.