Sign in to follow this  

Help on Implement anisotropic texture filtering with shader

This topic is 3428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There are some reasons I have to implement anisotropic texture filtering with shader instead of hardware's built-in capabilities. I have read some papers about it, and code it based on "Implementing an anisotropic texture filter".But the result is much worse than hardware's, and when I move camera forward, the aliasing is more apparent. (The texture is created from file with a complete mipmap chain) the result is http://picasaweb.google.com/lizeyu840508/AnisotropicFilter/photo#5227035713737632418 Then, I modify the color of texture's mipmap chain to see whether I choose the right level, and it's almost the same as hardware's which means that the poor result is caused by sampling. Level result is http://picasaweb.google.com/lizeyu840508/AnisotropicFilter/photo#5227035715830428786 Now, I'm totally confused! Does anybody have some experiences about it? Any help will be appreciated! Here is my pixel shader(HLSL): float2 textureSize = float2(2048.0f, 2048.0f); float MAX_ANISOTROPY = 4.0f; texture tex; sampler texSampler = sampler_state { Texture = (tex); MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = Clamp; AddressV = Clamp; }; PS_Out AnisotropicPS(PS_In In) { PS_Out Out = (PS_Out)0; // custom anisotropic float2 dx = ddx(In.texCoord); float2 dy = ddy(In.texCoord); float2 dxLevel0 = dx * textureSize.x; float2 dyLevel0 = dy * textureSize.y; float2 rDis = float2(dot(dxLevel0, dxLevel0), dot(dyLevel0, dyLevel0)); rDis = sqrt(rDis); float minorLength, majorLength; float2 minorV, majorV; if(rDis.x > rDis.y) { minorLength = rDis.y; majorLength = rDis.x; minorV = dyLevel0; majorV = dxLevel0; }else { minorLength = rDis.x; majorLength = rDis.y; minorV = dxLevel0; majorV = dyLevel0; } float anisotropy = majorLength / minorLength; anisotropy = min(anisotropy, MAX_ANISOTROPY); minorLength = majorLength / anisotropy; float mipMapLevel = log2(minorLength); majorV = majorV / exp2(mipMapLevel); majorV = majorV / textureSize.x; int sampleN = round(anisotropy); float2 step = majorV / sampleN * 0.5f; if(sampleN == 1) { Out.color = tex2Dlod(texSampler, float4(In.texCoord, mipMapLevel, mipMapLevel)); }else if(sampleN == 2) { Out.color = tex2Dlod(texSampler, float4(In.texCoord + step, mipMapLevel, mipMapLevel)); Out.color += tex2Dlod(texSampler, float4(In.texCoord - step, mipMapLevel, mipMapLevel)); }else if(sampleN == 3) { Out.color = tex2Dlod(texSampler, float4(In.texCoord, mipMapLevel, mipMapLevel)); Out.color += tex2Dlod(texSampler, float4(In.texCoord + 2 * step, mipMapLevel, mipMapLevel)); Out.color += tex2Dlod(texSampler, float4(In.texCoord - 2 * step, mipMapLevel, mipMapLevel)); }else if(sampleN == 4) { Out.color += tex2Dlod(texSampler, float4(In.texCoord + step, mipMapLevel, mipMapLevel)); Out.color += tex2Dlod(texSampler, float4(In.texCoord - step, mipMapLevel, mipMapLevel)); Out.color += tex2Dlod(texSampler, float4(In.texCoord + 3 * step, mipMapLevel, mipMapLevel)); Out.color += tex2Dlod(texSampler, float4(In.texCoord - 3 * step, mipMapLevel, mipMapLevel)); }else { // no chance to execute Out.color = float4(0.0f, 0.0f, 0.0f, 1.0f); } Out.color = Out.color / Out.color.a; return Out; }

Share this post


Link to post
Share on other sites

This topic is 3428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this