Intrinsics 内置函数

 

As mentioned in the preceding section, there are a number of

intrinsics built into the DirectX High Level Shading Language for

your convenience. Many intrinsics, such as mathematical functions,

are provided for convenience, while others, such as the

tex1D() and tex2D() functions mentioned above, are necessary for

accessing texture data via samplers.

 

我们在以往的章节提到过,HLSL内置了大量的内置函数(intrinsics

以提高便利性。许多内置函数,例如数学函数是为了方便而提供的;

而另一些,例如前面提到的tex1D() tex2D()函数是通过采样器存取纹理

而必须的。

 

Math Intrinsics 数学内置函数

 

The math intrinsics listed in the table below will be converted to

micro operations by the HLSL compiler. In some cases, such as

abs() and dot(), these intrinsics will map directly to single assembly-

level operations, while in other cases, such as refract() and

step(), they will map to multiple assembly instructions. There are

even a couple of cases, notably ddx(), ddy(), and fwidth(), that are

not supported for all compile targets. The math intrinsics are

shown below:

 

在下表列出的数学内置函数会被HLSL编译器转换成微代码。

在某些情况下,例如abs() dot(),这些内置函数会被直接映射成

单个汇编级操作指令;但是在另外一些情况下,例如refract() step()

它们会被映射成多条汇编指令。甚至有一些情况,ddx(),ddy()

fwidth()等,它们并非对所有编译目标都有效。

 

表略。

 

Texture Sampling Intrinsics 纹理采样函数

 

There are 16 texture sampling intrinsics used for sampling texture

data into a shader. There are four types of textures (1D, 2D,

3D, and cube map) and four types of loads (regular, with derivatives,

projective, and biased) with an intrinsic for each of the 16

combinations:

 

16个纹理采样内置函数可以在着色器内用来采样纹理。

4种纹理类型(1维,2维,3维和cube map)和4种类型的加载

(regular, with derivatives, projective, biased)可以组合。

 

表略。

 

The tex1D(), tex2D(), tex3D(), and texCUBE() intrinsics are the

most commonly used to sample textures. The texture loading

intrinsics that take ddx and ddy parameters compute texture LOD

using these explicit derivatives, which would typically have been

previously calculated with the ddx() and ddy() math intrinsics.

These are particularly important when writing procedural pixel

shaders, but they are not supported on ps_2_0 or lower compile

targets.

 

内置函数tex1D(), tex2D(), tex3D(), and texCUBE()是最常用的采样函数。

而使用ddyddy参数的纹理加载函数通过显式的已经被ddx()ddy()

所预先计算的导数,来计算纹理的LOD(Level-Of-Detail)。当写像素着色器

代码时这尤其重要,但是ps_2_0或更低的编译目标不支持。

 

The tex*proj() intrinsics are used to do projective texture

reads, where the texture coordinates used to sample the texture

are divided by the last component prior to accessing the texture.

Of these, tex2Dproj() is the most commonly used, since it is necessary

for projective shadow maps and similar effects.

The tex*bias() intrinsics are used to perform biased texture

sampling, where the bias can be computed per pixel. This is typically

done to induce some over-blurring of the texture for a

special effect. For example, as discussed in ShaderX2: Shader Programming

Tips & Tricks with DirectX 9, the pixel shader used on

the motion-blurred balls in the Radeon 9700 Animusic Pipe

Dream demo uses the texCUBEbias() intrinsic to access the cubic

environment map of the local scene:

 

tex*proj()内置函数被用来执行投影纹理读取,其中的用来采样的纹理坐标

在使用之前会除以第四个分量。当然,tex2Dproj()是最常用的,因为对于

投影阴影贴图和类似的效果它是必须的。tex*bias()被用来进行bias采样,

其中的bias可以逐像素计算。当使用过度-模糊之类的特效是,会用到它。

例如,在《ShaderX2: Shader Programming Tips & Tricks with DirectX 9

里讨论的, Animusic Pipe Dream 演示里面的实现运动模糊的球

的像素着色器使用了texCUBEbias()函数来存取局部场景的立体环境贴图:

 

...

// Blur reflection by extension amount.

float3 vCubeLookup = vReflection + i.Pos/fEnvMapRadius;

float4 cReflection = texCUBEbias(tCubeEnv, float4(vCubeLookup,

fBlur * fTextureBlur)) * vReflectionColor;

...

 

In this code snippet, fBlur * fTextureBlur is stored in the fourth

component of the texture coordinate used in the texCUBEbias()

call and determines the bias to be used when accessing the cube

map.

 

在这段代码中,fBlur * fTextureBlur被储存在纹理坐标的第四个分量中,

后者被texCUBEbias()所调用,它决定了存取cube map时使用的偏差。