ShaderX2翻译六 连载中

Smplers

每一个不同的纹理贴图你可能在像素着色器中进行采样.你必须声明一个采样器.回忆先前着色器描述的hlsl_rings().

float4 lightWood; // xyz == Light Wood Color

float4 darkWood; // xyz == Dark Wood Color

float ringFreq; // ring frequency

sampler PulseTrainSampler;

float4 hlsl_rings (float4 Pshade : TEXCOORD0) : COLOR

{

float scaledDistFromZAxis = sqrt(dot(Pshade.xy, Pshade.xy)) * ringFreq;

float blendFactor = tex1D (PulseTrainSampler, scaledDistFromZAxis);

return lerp (darkWood, lightWood, blendFactor);

}

在这个着色中, 我们声明了一个全局的采样器PulseTrainSampler,并且是内部函数tex1D的第一个参数(我们将在下一章讨论内部函数).每个着色器必须为每个你准备在着色器中使用的纹理贴图而被定义.但是你可能在着色器中使用一个被多次采样的采样器.在图片处理程序中,这种用法非常通用,例如在ShaderX2: Shader Programming Tips & Tricks with DirectX 9,中讨论的,输入图片通过不同的纹理坐标被多次采样.例如下面的着色器使用光栅化器来转换高度贴图到向量贴图.

sampler InputImage;

float4 main( float2 topLeft : TEXCOORD0, float2 left : TEXCOORD1,

float2 bottomLeft : TEXCOORD2, float2 top : TEXCOORD3,

float2 bottom : TEXCOORD4, float2 topRight : TEXCOORD5,

float2 right : TEXCOORD6, float2 bottomRight : TEXCOORD7):COLOR

{

// Take all eight taps

float4 tl = tex2D (InputImage, topLeft);

float4 l= tex2D (InputImage, left);

float4 bl = tex2D (InputImage, bottomLeft);

float4 t = tex2D (InputImage, top);

float4 b = tex2D (InputImage, bottom);

float4 tr = tex2D (InputImage, topRight);

float4 r = tex2D (InputImage, right);

float4 br = tex2D (InputImage, bottomRight);

// Compute dx using Sobel operator:

//

// -1 0 1

// -2 0 2

// -1 0 1

float dX = -tl.a - 2.0f*l.a - bl.a + tr.a + 2.0f*r.a + br.a;

// Compute dy using Sobel operator:

//

// -1 -2 -1

// 0 0 0

// 1 2 1

float dY = -tl.a - 2.0f*t.a - tr.a + bl.a + 2.0f*b.a + br.a;

// Compute cross product and renormalize

float4 N = float4(normalize(float3(-dX, -dY, 1)), tl.a);

// Convert signed values from -1..1 to 0..1 range and return

return N * 0.5f + 0.5f;

}

 

该着色器只使用了一个采样器,一个输入图片,但是采样器使用了8次tex2D()内部函数.

 

Intrinsics

就如先前篇章提及到的,在DirectX 高级着色器语言中有一些内部函数方便你使用.一些内部函数,比如数学函数,为了方便而被提供,而一些比如上面提到的tex1D()和tex2D函数是采样器处理纹理数据必须使用的.

 

Math Intrinsics

数学内部函数请参考msdn

 

Texture Sampling Intrinsics

有16种纹理采样器内部函数.有4种类型纹理(1D,2D,3D,和cube贴图),有4种类型重载(regular, derivatives, projective, biased).

Intrinsic

Oescription

tex1D(s,f)

1D纹理寻址.s是一个采样器.f是标量

tex1D(s,f,ddx,ddy)

1D纹理寻址,派生.s是一个采样器.f,add和addy是标量

tex1Dproj(s,f)

1D projective纹理寻址.s是一个采样器.f是一个4D向量.

tex1Dbias(s,f)

1D biased纹理寻址.s是一个采样器.f是一个4D向量.

tex2D(s,f)

2D纹理寻址.s是一个采样器.f是一个2D纹理坐标.

tex2D(s,f,ddx,ddy)

2D纹理寻址,派生.s是一个采样器.f,add和addy是2D vector

tex2Dproj(s,f)

2D projective纹理寻址.s是一个采样器.f是一个4D向量.

tex2Dbias(s,f)

2D biased纹理寻址.s是一个采样器.f是一个4D向量.

tex3D(s,f)

3D体积纹理寻址.s是一个采样器.f是一个3D纹理坐标.

tex3D(s,f,ddx,ddy)

3D纹理寻址,派生.s是一个采样器.f,add和addy是3D vector

tex3Dproj(s,f)

3D projective纹理寻址.s是一个采样器.f是一个4D向量.

tex3Dbias(s,f)

3D biased纹理寻址.s是一个采样器.f是一个4D向量..

texCUBE(s,f)

Cυbe 贴图寻址.s是一个采样器.f是一个3D纹理坐标

texCUBE(s,f,ddx,ddy)

Cube贴图寻址,派生.s是一个采样器.f,add和addy是3D vector

texCUBEproj(s,f)

Projective  Cube贴图寻址.s是一个采样器.f是一个4D向量.

texCUBEbias(s, t)

biased   cube 贴图寻址.s是一个采样器.f是一个4D向

 

posted @ 2012-06-19 20:41  wuhongxing  阅读(238)  评论(0)    收藏  举报