giawa / opengl4csharp

OpenGL 4 Bindings (partially based on OpenTK) for C#
Other
234 stars 61 forks source link

Anisotropic filtering not supported #6

Closed falki147 closed 7 years ago

falki147 commented 7 years ago

I know it's not a core extension but it is present in most drivers and it really improves texture quality. It's also easy to implement (two enums, TEXTURE_MAX_ANISOTROPY_EXT and MAX_TEXTURE_MAX_ANISOTROPY_EXT, see spec)

giawa commented 7 years ago

I can check to see if the extension exists by calling GetString(StringName.Extensions) and checking for GL_EXT_texture_filter_anisotropic. However, what would you expect should happen if the extension does not exist? Should it simply fall back to GL_LINEAR?

falki147 commented 7 years ago

I would let the programmer decide what to do to be honest.

giawa commented 7 years ago

Ya, this is a tough one. I've added a bunch of new code to check for extensions. So now you can do something like:

if (Gl.Extensions.TextureFilterAnisotropic_EXT)
{
   // initialize the texture manually with Anisotropy
}

However, do I now add MaxAnisotropy to the TextureParameter enum? Or do I simply let the programmer decide to call Gl.TexParameteri directly with the correct value? So your code might end up something like:

int textureParameter = (Gl.Extensions.TextureFilterAnisotropic_EXT ? 0x84FE : (int)TextureParameter.Nearest);
Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, textureParameter);
Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, textureParameter);

What do you think?

Edit: As an aside, you can also check for extensions this way:

Gl.IsExtensionSupported(Extension.GL_EXT_texture_filter_anisotropic);
falki147 commented 7 years ago

Anisotropic filtering is not really a filter you can set but a different TextureParameter. Here's an exmaple of how I've used it:

// Set filter etc.

if (Graphics.hasAnisotropicFiltering)
    Graphics.setAnisotropicFiltering(TextureTarget.Texture2D, Math.Min(8f, Graphics.maxAnisotropicFiltering));

hasAnisotropicFiltering is the extension check, setAnisotropicFiltering is the Gl.TexParameterf(target, (TextureParameterName) 0x84FE, value) call and maxAnisotropicFiltering the Gl.GetFloatv((GetPName) 0x84FF, aniso) call.

MaxAnisotropy should be implemented in TextureParameterName imo. It would be cool if MAX_TEXTURE_MAX_ANISOTROPY_EXT was implemented too.

giawa commented 7 years ago

The issue I foresee with adding it to TextureParameterName is that now the OpenGL library has to check to see if it is valid, and then fall back to do something else. Do I rely upon the programmer to check for Anisotropy before actually making calls to it? Or do I start doing extension checking all over the place as we add support for more extensions? It's the start of a slippery slope.

falki147 commented 7 years ago

Yeah that's true. I still think the user is responsible. If you would add a check in the library you would have to throw a exception which might be counterproductive since it's just a texture paramter. Also every OpenGL implementation will silently ignore the call if it isn't supported.

giawa commented 7 years ago

Ok, I've added those enumerations to GetPName, TextureParameter and TextureParameterName. Do you mind taking a look and trying it out? I also made it a bit simpler to call GetFloat and GetDouble by pre-allocating a single element array and using that for the call to GetFloatv.

if (Gl.Extensions.TextureFilterAnisotropic_EXT)
{
    Console.WriteLine("Your OpenGL context supports a maximum anisotropy of {0}!", Gl.GetFloat(GetPName.MaxTextureMaxAnisotropyExt));
    Gl.TexParameterf(TextureTarget.Texture2D, TextureParameterName.MaxAnisotropyExt, Math.Min(8f, Gl.GetFloat(GetPName.MaxTextureMaxAnisotropyExt)));
}

What do you think?

falki147 commented 7 years ago

That's exactly what I imagined. It seems to be working too. Thanks for the effort :)

giawa commented 7 years ago

Awesome, glad to hear it. If you find other common extensions perhaps we can add them in a similar manner.