Now that you have a sense of what HLSL vertex and pixel shaders

look like and how they interact with the low-level assembly

shaders, we can discuss some of the details of the language itself.

现在你已经对HLSL顶点和像素着色器的样子和他们如何和低阶汇编着色器交互有了了解,我们可以讨论语言本身的一些细节了。

 

Keywords关键字

Keywords are predefined identifiers that are reserved for the

HLSL language and cannot be used as identifiers in your program.

Keywords marked with an asterisk (*) are case insensitive.

关键字是预定义的标识符,为HLSL语言保留而不能作为您的程序中的标识符、关键字以星号(*)标识的是大小写敏感的。

asm* bool compile const decl* do double else extern false float for

half if in inline inout int matrix* out pass* pixelshader* return sampler

shared static string* struct technique* texture* true typedef

uniform vector* vertexshader* void volatile while

 

The following keywords are currently unused but reserved for

potential future use:

 

下列的关键字现在没有使用但是为将来可能的使用而保留。

auto break case catch

char class compile const

const_cast continue default delete

dynamic_cast enum explicit friend

goto long mutable namespace

new operator private protected

public register reinterpret_cast short

signed sizeof static_cast switch

template this throw try

typename union unsigned using

virtual

 

 

Data Types 数据类型

 

The HLSL has support for a variety of data types, from simple

scalars to more complex types, such as vectors and matrices.

HLSL支持大量的数据类型,从简单的标量到更复杂的类型,例如向量和矩阵。

 

 

Scalar Types 标量类型

The language supports the following scalar data types:

语言支持下列标量数据类型:

Data Type

Representable Values

bool

true or false

int

32-bit signed integer

half

16-bit floating-point value

float

32-bit floating-point value

double

64-bit floating-point value

 

If you are already familiar with the assembly-level programming

models, you should know that graphics processors do not currently

have native support for all of these data types. As a result,

integers may need to be emulated using floating-point hardware.

This means that integer operations that go outside the range of

integers that can be expressed as floats on these platforms are not

guaranteed to function as expected. Additionally, not all target

platforms have native support for half or double values. If the target

platform does not, these will be emulated using float.

 

如果你已经熟悉了汇编层级上的编程模型,那么你应该知道图形

处理器现在对所有这些数据类型没有原生(native)地支持。结果,

整数也许需要被使用浮点硬件进行模拟。这意味着超出整数范围之外

的整数操作可以被作为浮点数进行表示,但是在某些平台上不能确保如所期望

地正常工作。另外,不是所有的目标平台都对halfdouble值有原生的

支持。如果目标平台没有这样的支持,那么它们会被使用float类型进行模拟。

 

 

Vector Types 向量类型

 

You will often find yourself declaring vector variables in your

HLSL shaders. There are a variety of ways that these vectors can

be declared, including the following:

你应当会经常发现自己在HLSL着色器代码中声明向量变量。

这里有大量的方式可以声明变量,包括以下几种:

Vector

Declared as

vector

A vector of dimension 4; each component is of type

float.

vector<type, size>

A vector of dimension size; each component is of

scalar type type.

The most common way that you see shader authors declare vectors,

however, is by using the name of a type followed by an

integer from 2 to 4. To declare a 4-tuple of floats, for example,

you could use any of the following vector declarations:

但是,你可以看到的着色器作者声明向量地最通常的方式是使用类型的名字,后面跟着

一个从24的整数。例如,为了声明一个float类型的4元组,可以使用下列向量声明的任何一种:

 

float4 fVector0;

float fVector1[4];

vector fVector2;

vector <float, 4> fVector3;

 

To declare a 3-tuple of bools, for example, you could use any of the

following declarations:

例如,为了声明一个bool类型的3元组,可以使用下列声明方式的任何一种:

 

bool3 bVector0;

bool bVector1[3];

vector <bool, 3> bVector2;

 

Once you have defined a vector, you may access its individual

components by using the array access syntax or a swizzle. In the

swizzle case, the components must come from either the {x, y, z,

w} or {r, g, b, a} namespace (but not both). For example:

 

一旦定义了一个向量,你可以通过使用数组存取语法或者

一个swizzle方法存取它的各独立分量(component)。在

Swizzle方法情况下,各分量必须来自{x, y, z,w} 或者 {r, g, b, a}

名称空间(不能同时存在)。例如:

 

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; // 无效,由于同时来自两个名称空间

 

It should be noted that the ps_2_0 and lower pixel shader models

do not have native support for arbitrary swizzles. Hence, concise

high-level code that uses swizzles can result in fairly nasty binary

asm when compiling to these targets. You should familiarize yourself

with the native swizzles available in these assembly models.

 

应当注意的是ps_2_0以及更低的像素着色器模型对于swizzle

方式没有原生支持。因此,使用swizzle方式的简单的高阶代码会导致

当为这些目标编译时产生令人厌恶的汇编代码。你应当使自己熟悉

原生swizzle方式在这些汇编模型中的使用。