teichgraf's picture

Test if extension is available

Hello,

I am asking me right now, if OpenTK has a helper method, which tests if a certain OpenGL extension is available?

Something like:

public static bool IsExtAvailable(string ext)
{
   return GL.GetString(StringName.Extensions).Contains(ext);
}

Or is there another way to test for extensions at runtime?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Inertia's picture

Assuming this ain't an april fool's joke and the question is serious: This is covered by the functions GL.SupportsExtension / GL.SupportsFunction

It might be useful to create an overload with enum as parameter, instead of a string. (to avoid typing errors) Anyone besides objarni votes for that idea? ;)

teichgraf's picture

Thanks!
I looked for Is* or Has* kind of method signature. But I didn't found GL.SupportsExtension. Sorry!

An enum overload would be really useful.

kanato's picture

Yeah, I second the enum overload. That would be very useful, if just for the intellisense.

georgwaechter's picture

i agree ... an enum overload would be helpful

objarni's picture

I STRONGLY DISAGREEE!!!!

.

.

.

.

.

.

.

.

.

.

.

.

Fooled ya! ;)

Inertia's picture

It's known that you're into coder pr0n and overloads like this are your thing ;)

objarni's picture

thanks really cool demo given 256 bytes :)

the Fiddler's picture

Something like this would work:

// types found in Source/Bind/Specifications/GL2/gl.tm
enum Extension
{
    VERSION_1_2,
    VERSION_1_3,
    ...
    EXT_texture_integer
}
 
public static bool SupportsExtension(Extension e)
{
    return SupportsExtension(e.ToString());
}

A prettier solution would be to use a struct with constants:

struct Extension
{
    public const string Version12 = "VERSION_1_2";
    public const string Version13 = "VERSION_1_3";
}

The latter could be added to the generator, although it might be simpler to write by hand (but more boring).

Also happy April's fool!

Inertia's picture

Mhmm I don't know why the discussion moved to pm's *apologizes*

Imo both solutions have their benefits:

  1. Enum
    The advantage of easy iteration through all extensions for debugging purposes.

    public enum Extension: uint
    {
        VERSION_1_2 =0,
        VERSION_1_3,
        ...
        EXT_texture_integer,
        Last, // reserved for marking the highest/last value in the enum
    }

    The disadvantage of this is obviously that it's not as nice as Extension.Version12 and not "C#-ish" (C) objarni. But on the other hand, it's exactly the token name that GL expects.

    Example:
    GL.SupportsExtension( Extension.WGL_ARB_randomly_chosen_token ); // new enum overload
    GL.SupportsExtension( Extension.WGL_ARB_randomly_chosen_token.ToString() ); // string param

  2. Struct
    Nicer to handle, but requires some extra thinking/understanding how Extension.WglArbRandomlyChosenToken translates into WGL_ARB_randomly_chosen_token.

    Example:
    GL.SupportsExtension( Extension.WglArbRandomlyChosenToken ); // string param

I have no clear favorite, but if you put a pistol to my head I'd say 1) because it's closer to the C API.

Inertia's picture

Sleeping over it, I created the function using an enum for now. Imo it will be much less confusing to the user, since it's more logical to query for presence of GL_ARB_vertex_buffer_object than GLArbVertexBufferObject. It should also aid in looking up the spec on opengl.org, or googling for tutorials.

The overload can be used like this:

GL.SupportsExtension( GL.Extension.WGL_ARB_randomly_chosen_token ); // new enum overload
GL.SupportsExtension( GL.Extension.WGL_ARB_randomly_chosen_token.ToString() ); // string param
 
// also nice, we can iterate through all available GL extensions and dump that into a debug log.
for ( int i = 0; i < (int) GL.Extension.Last; i++ )
{
  Debug.WriteLine( ( (GL.Extension) i ).ToString( ) + " available: " + GL.SupportsExtension( (GL.Extension) i ) );
}