Home > database >  Is there a way to check if a platform supports an OpenGL function?
Is there a way to check if a platform supports an OpenGL function?

Time:01-01

I want to load some textures using glTexStorageXX(), but also fall back to glTexImageXX() if that feature isn't available to the platform.

Is there a way to check if those functions are available on a platform? I think glew.h might try to load the GL_ARB_texture_storage extensions into the same function pointer if using OpenGL 3.3, but I'm not sure how to check if it succeeded. Is it as simple as checking the function pointer, or is it more complicated?

(Also, I'm making some guesses at how glew.h works that might be wrong, it might not use function pointers and this might not be a run-time check I can make? If so, would I just... need to compile executables for different versions of OpenGL?)

if (glTexStorage2D) {
    // ... calls that assume all glTexStorageXX also exist, 
    // ... either as core functions or as ARB extensions
} else {
    // ... calls that fallback to glTexImage2D() and such.
}

CodePudding user response:

You need to check if the OpenGL extension is supported. The number of extensions supported by the GL implementation can be called up with glGetIntegerv(GL_NUM_EXTENSIONS, ...). The name of an extension can be queried with glGetStringi(GL_EXTENSIONS, ...).

Read the extensions into a std::set

#include <set>
#include <string>
GLint no_of_extensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &no_of_extensions);

std::set<std::string> ogl_extensions;
for (int i = 0; i < no_of_extensions;   i)
    ogl_extensions.insert((const char*)glGetStringi(GL_EXTENSIONS, i));

Check if an extension is supported:

bool texture_storage = 
    ogl_extensions.find("GL_ARB_texture_storage") != ogl_extensions.end();

glTexStorage2D is in core since OpenGL version 4.2. So if you've created at least an OpenGL 4.2 context, there's no need to look for the extension.
When an extension is supported, all of the features and functions specified in the extension specification are supported. (see GL_ARB_texture_storage)


GLEW makes this a little easier because it provides a Boolean state for each extension.
(see GLEW - Checking for Extensions) e.g.:

if (GLEW_ARB_texture_storage)
{
    // [...]
}
  • Related