Anisotropic Filtering with Sampler Objects

Hello Everyone!

Since a while I have added anisotropic filtering for textures to my small engine by setting the GL_TEXTURE_MAX_ANISOTROPY parameter on texture creation.
Btw. i use OpenGL 4.6 (Core), so i don’t need any extension for anisotropic filtering.

Now, I want the max used anisotropy to be configurable for the user while the application is running.
I think configuring the anisotropy through a sampler object would be the best solution, but i don’t get it running somehow.

At first I show you some code:

Texture creation:


glGenerateMipmap(GL_TEXTURE_2D);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 16.0f); // -> The old way of applying anisotropic filtering;

sampler object creation:


GLuint sampler;
glGenSamplers(1, &sampler);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_REPEAT);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_REPEAT);
glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY, 16.0f);

sampler and texture binding:


glBindSampler(0, sampler);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, my_texture);
       
// render mesh etc.

// Restore default sampler state
glBindSampler(0, GL_FALSE);

The problem now is, that apparently no anisotropic filtering is used: The textures are blurred as if GL_TEXTURE_MAX_ANISOTROPY would be set to 1.0 .

If i set


glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

the texture is filtered by nearest neighbor on minification. So the sampler is definitely used. But apparently the value of GL_TEXTURE_MAX_ANISOTROPY is ignored.

I also tested that the GL_TEXTURE_MAX_ANISOTROPY field gets updated by evaluating


float currentValue= 0.0f;
glGetSamplerParameterfv(sampler, GL_TEXTURE_MAX_ANISOTROPY, &currentValue);

// currentValue is 16.0f as expected

I don’t understand that behaviour and thought that maybe someone has a hint or solution :slight_smile: