TomatoMato
TomatoMato

Reputation: 713

GLSL: check if an extension is supported

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)

Upvotes: 8

Views: 8575

Answers (2)

datenwolf
datenwolf

Reputation: 162164

GLSL versions directly map to OpenGL versions. Prior to and inclusing OpenGL-3.2 the mapping is

OpenGL Version  GLSL Version
           2.0  1.10
           2.1  1.20
           3.0  1.30
           3.1  1.40
           3.2  1.50

Since OpenGL-3.3 the OpenGL version is identical to the supported GLSL version.

In a similar way GLSL extensions map to OpenGL extensions, reported in the OpenGL extension string.

Given this information you can load the apropriate shaders, or even add proprocessor definitions that support conditional compilation.

Update: glShaderSource takes an array of strings, which are concatenated internally. This can be used to pass a string with preprocessor definitions before the actual shader code. Of course the #version … token must still come before everything else.

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 473352

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

Upvotes: 22

Related Questions