Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL: check if an extension is supported

Tags:

opengl

glsl

You can't use an unsupported extension, driver will return compilation error. But can you check availability of some extension directly from GLSL code? Is there something like this?

#version XXX core
#if supported(EXT_some_extension)
#extension EXT_some_extension: enable
#endif

...

UPDATE: According to Nicol's Bolas answer. Yes, that appeared in my mind too, but for some reason, it is not working

#version 150 core
#extension ARB_explicit_attrib_location : enable
#ifdef ARB_explicit_attrib_location
#define useLayout layout(location = 2)
#else
#define useLayout  //thats an empty space
#endif

in vec2 in_Position;
useLayout in vec2 in_TextureCoord;
...

Macro "useLayout" is always set to empty space, but if I left only #enable directive without conditions it will use it (my driver supports it). Looks like extensions do not being defined, it is something else (probably?) (#if defined(ARB_explicit_attrib_location) does not work too)

like image 226
TomatoMato Avatar asked Aug 03 '13 23:08

TomatoMato


1 Answers

#if supported(EXT_some_extension)
#extension GL_EXT_some_extension: enable
#endif

You are trying to write a shader that conditionally uses a certain extension. The correct way to do what you're trying to do is this:

#extension EXT_some_extension: enable

#ifdef GL_EXT_some_extension
//Code that uses the extension.
#endif //GL_EXT_some_extension

Every OpenGL extension that has GLSL features will have a specific #define for it. And the enable flag will only emit a warning if the extension isn't around. If it's not active, the #ifdef won't trigger.

like image 193
Nicol Bolas Avatar answered Nov 15 '22 10:11

Nicol Bolas