Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.
}

>Solution :

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)
{
    // [...]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading