像素着色器:Pixel Shader(PS),它替换了固定功能管线的多纹理化阶段(the multitexturing stage)。
1. 纹理设置:
HRESULT IDirect3DDevice::SetTexture( DWORD stage,IDirect3DBaseTexture9 *pTexture);
采样器状态:
HRESULT IDirect3DDevice::SetSamplerState( DWORD sampler,D3DSAMPLERSTATETYPE Type, DWORD value);
可以一次使用多个纹理,stage分别设置为0,1,2,...
2.像素着色器的输入和输出
INPUT:颜色和纹理坐标
struct PS_INPUT
{
vector c0:COLOR0;
vector c1:COLOR1;
float2 t0:TEXCOORD0;
float2 t1:TEXCOORD1;
float2 t2:TEXCOORD2;
};
OUTPUT:一个计算过的该像素的颜色值
struct PS_OUTPUT
{
vector finalPixelColor:COLOR0;
};
3. 使用像素着色器的步骤
(1)创建PS
HRESULT IDirect3DDevice::CreatePixelShader( const DWORD *pFunction,IDirect3DPixelShader9 **ppShader);
pFunction:已编码着色器代码的指针 ppShader:返回一个接口指针
(2)建立PS
HRESULT IDirect3DDevice::SetPixelShader(IDirect3DPixelShader9 *pShader);
(3)销毁PS
d3d::Release<IDirect3DPixelShader9 *>(MultiTexPS);
e.g. IDirect3DPixelShader9* MultiTexPS = 0;
hr = Device->CreatePixelShader((DWORD*)shader->GetBufferPointer(),&MultiTexPS);
Device->SetPixelShader(MultiTexPS);
d3d::Release<IDirect3DPixelShader9 *>(MultiTexPS);