2
\$\begingroup\$

I want to set anisotropic filtering to my scene. I use SharpDX (DirectX 11) and C#.

How do i set up anisotropic filtering in my shader?

Currently i try that in the shader:

Texture2D tex;
sampler textureSampler = sampler_state
{
  Texture = (tex);
  MipFilter = Anisotropic;
  MagFilter = Anisotropic;
  MinFilter = Anisotropic;
  MaxAnisotropy = 16;
};

float4 PShader(float4 position : SV_POSITION, float4 color:COLOR, float2 tex0 : TEXCOORD0) : SV_TARGET
{
  float4 textureColor;
  textureColor = tex.Sample(textureSampler, tex0) * color;

  return textureColor;
}

I get my object, textured, but it is not filtered anisotropic. I can write everything in the Parameters, even invalid things and i don't get any errors. The result is the same, objects without applied anisotropic filtering.

Do i have to set that in the shader? Can i do that also with SamplerState? I tested that but i didn't get a result too.

Some steps what i have to set would be helpful.


Edit:

Here the current result. You can see the blurred planes of the cube in the middle: SharpDX

As comparison the same scene in OpenGL with 16x anisotropic filtering. The planes of the cube are not blurred: OpenGL


Second Edit:

Based on the idea, that in OpenGL the anisotropic filtering is a texture environment variable, maybe i have set some settings in the texture wrong?

Texture2D texture = (Texture2D)Texture2D.FromFile("Test.png");
ShaderResourceView tSRV = new ShaderResourceView(device, texture);
device.ImmediateContext.PixelShader.SetShaderResource(0, tSRV);
\$\endgroup\$
  • \$\begingroup\$ Out of curiosity, how do you know that ansiotropy is not being factored into texture filtering? Have you actually tried drawing false-colored mipmaps? You will see a difference in an image that renders nothing but false-colored mipmaps at extreme angles, as the sample neighborhood changes shape as maximum anisotropy increases. The worst-case scenario would be a texture that is applied parallel to the image plane, anisotropy will be 0 in this case and you will see no difference no matter what you set your maximum anisotropy to. The best (test) case would be nearly perpendicular. \$\endgroup\$ – Andon M. Coleman Jun 3 '14 at 16:12
  • \$\begingroup\$ @AndonM.Coleman I also have the same scene in OpenGL and WPF. WPF has anisotropy by default and with OpenGL i set that with TextureMaxAnisotropyExt in the TexParameter. The image with SharpDX is worse than the others. I add some images. I did not tried false-colored mipmaps, but i will try to test it. \$\endgroup\$ – fedab Jun 3 '14 at 16:49
  • 1
    \$\begingroup\$ A sampler state specified in HLSL only works if you're using the effect framework. Are you using that, or are you using "loose" shaders? Have you tried constructing a sampler state object and setting it from C#, instead of specifying it in HLSL? \$\endgroup\$ – Nathan Reed Jun 3 '14 at 19:51
  • \$\begingroup\$ @NathanReed I use "loose" shaders. I didn't know the effect framework. I set a SamplerState, and experimented with the SamplerStateDescription, but got no better results... \$\endgroup\$ – fedab Jun 3 '14 at 20:19
  • \$\begingroup\$ What register is the sampler on, and what slot are you binding it to? \$\endgroup\$ – Maximus Minimus Jun 3 '14 at 21:37
2
\$\begingroup\$

As already mentioned in the comments, you cannot specify your sampler states in the shader unless you use the effect framework.

To utilize sampler states without the effect framework specify their location with the register keyword:

sampler anisotropySampler : register(s0);
sampler pointSampler : register(s1);

Then initialize the slots with the following C# code:

var anisotropySampler = new SamplerState(device, new SamplerStateDescription() {
    AddressU = TextureAddressMode.Wrap,
    AddressV = TextureAddressMode.Wrap,
    AddressW = TextureAddressMode.Wrap,
    Filter = Filter.Anisotropic,
    MaximumAnisotropy = 16,
});
var pointSampler = new SamplerState(device, new SamplerStateDescription() {
    AddressU = TextureAddressMode.Clamp,
    AddressV = TextureAddressMode.Clamp,
    AddressW = TextureAddressMode.Clamp,
    Filter = Filter.MinMagMipPoint,
});
Context.PixelShader.SetSamplers(0, anisotropySampler, pointSampler);

Each pipeline stage has a total of 16 sampler slots available.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.