在HLSL中拉伸纹理时如何实现线性插值?

时间:2015-04-05 19:15:59

标签: direct3d hlsl pixel-shader

这是像素着色器代码:

sampler s0 : register(s0);

float4 main(float2 tex : TEXCOORD0) : COLOR
{
tex.x=tex.x/8 +0.25;

float4 l = tex2D(s0, tex);

return l;
}

运行上述代码时,我得到以下内容:

Filtering sample image

我尝试更改采样器状态过滤器但未成功:

sampler s0 : register(s0) = sampler_state
{ 
  Texture = (s0); 

  MinFilter = Linear; 
  MagFilter = Linear; 

  AddressU = Clamp; 
  AddressV = Clamp; 
}; 

我试过立方过滤,但非常昂贵:

sampler s0;
float c0;
float c1;
#define sp(a, b) float4 a = tex2D(s0, float2(coord + b * fx * c1.x, tex.y));

float4 main(float2 tex : TEXCOORD0) : COLOR
{
    float coord = (tex.x/2)*c0;             // assign the output position, normalized to texture width in pixels
    float t = frac(coord);          // calculate the difference between the output pixel and the original surrounding two pixels
                                                    // adjust sampling matrix to put the output pixel on Q2+.25
    float fx;
    if(t > .5)  {
    coord = (coord-t+1.5)*c1; fx = -1;
    }   else    {
    coord = (coord-t+0.5)*c1; fx = 1;
    }

    sp(P0, -2) 
    sp(P1, -1) 
    sp(P2, 0) 
    sp(P3, 1) 
    sp(P4, 2)       // original pixels

    return (P0 + P2*216 + P3*66 - P1*18 - P4*9)/256;                // output interpolated value
}

感谢。

1 个答案:

答案 0 :(得分:0)

您很可能需要在采样器desc中指定“MipFilter = LINEAR”,并且需要确保使用mipmap提供纹理...