Anisotropic textures in a global shader

I can’t match the result of a simple texture fetch in a regular material and my debug view mode shader. This problem only occurs when using default texture filtering. How can I get anisotropic filtering to work in my shader?

According to BaseDeviceProfiles.ini, the default settings for texture filtering are


To match this I set TStaticSamplerState to SF_AnisotropicPoint when binding the texture hoping to get the same result as my material

FRHISamplerState* SamplerStateLinear = TStaticSamplerState<SF_AnisotropicPoint>::GetRHI();
ShaderBindings.AddTexture(Shader.MipLevelTextureParameter, Shader.MipLevelTextureSampler, SamplerStateLinear, GEngine->TextureMipmapLevelTexture->Resource->TextureRHI);

Here is a comparison of the result, a default material and debug mode shader result using different texture filter and TStaticSamplerState settings.

The nearest and trilinear settings work fine. But anisotropic only works in a normal material, and not in a debug view mode. SF_AnisotropicPoint and SF_Trilinear look the same.

Both materials only do a basic fetch from a texture with custom mips where mip 0 is black, mip 7 is white, and the other mips are linear values between them, to check the rendered mip level. I sample this texture in a debug view mode pixel shader. This is the texture I used.

Is there something I can change, for example in DebugViewModeRendering.cpp to make anisotropy work? I need this for debugging texture mips and resolutions.


I found the answer in RHIStaticStates.h. TStaticSamplerState can be defined with a MaxAnisotropy parameter which defaults to 1.

class TStaticSamplerState : public TStaticStateRHI<TStaticSamplerState<Filter,AddressU,AddressV,AddressW,MipBias,MaxAnisotropy,BorderColor,SamplerComparisonFunction>,FSamplerStateRHIRef, FRHISamplerState*>

In D3D11 code, Anisotropic filtering is replaced with trilinear for this default value (D3D11State.cpp). So it is necessary to specify MaxAnisotropy with a value higher than 1 when initializing a texture sampler in order for anistropy to work.

FRHISamplerState* SamplerStateLinear = TStaticSamplerState<SF_AnisotropicPoint, AM_Clamp, AM_Clamp, AM_Clamp, 0, 16, 0, SCF_Never>::GetRHI();

The above code gives parity between custom shader and material editor output.