Home > 计算机与 Internet > zz::Applying Textures onto HLSL Shader Without Effect

zz::Applying Textures onto HLSL Shader Without Effect

 
HLSL中的采样器实际上一个结构类型,PixelShader程序使用采样器指定被采样纹理、纹理过滤方式和纹理寻址模式,采样器的使用语法大致如下:
texture tex0; // 声明一个纹理对象
sampler2D s_2D = sampler_state // 声明一个采样器对象
{
Texture = ; // 指定被采样的纹理
MinFilter = Linear; // 纹理过滤方式
MagFilter = Linear;
AddressU = Wrap; // 纹理寻址模式
AddressV = Wrap;
};
float4 PS_Main(float2 tex : TEXCOORD0) : COLOR
{
return tex2D(s_2D texcoord); // 像素渲染程序返回结果为纹理采样颜色
}
以上像素渲染程序代码中,并没有明确指定采样器s_2D对应的是纹理(tex0对象并没有被赋值),然而在多纹理采样的程序中,是需要明确指定采样器所采样的纹理对应第几个通道(最多8个通道,通过Device的SetTexture指定)的纹理。 有两种方法指定采样器对应的纹理:
1)通过指定采样器寄存器来绑定所采样的纹理:
sampler2D sColor : register(s0); // 绑定通道0中的纹理
sampler2D sNormal : register(s1); // 绑定通道1中的纹理
2)通过ConstantTable接口获取像素渲染程序中全局texture对象的RegisterIndex,从而把纹理设置到相应的通道:
LPD3DXCONSTANTTABLE pConstantTable = NULL;
D3DXCompileShaderFromFile(
"ps_multitex.txt"
0
0
"PS_Main"
"ps_1_1"
D3DXSHADER_DEBUG
&shader
&errorBuffer
&pConstantTable);
D3DXHANDLE hTex0 = pConstantTable->GetConstantByName(0 "tex0");
UINT count;
D3DXCONSTANT_DESC Tex0Desc;
pConstantTable->GetConstantDesc(hTex0 &Tex0Desc &count);
pDevice->SetTexture(Tex0Desc.RegisterIndex pTexture0); // 设置纹理到tex0对象对应的通道
 
Advertisements
Categories: 计算机与 Internet
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: