GLSL: check if an extension is supported

狂风中的少年 提交于 2019-12-10 14:34:28

问题


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)


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/18038224/glsl-check-if-an-extension-is-supported

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!