Is it possible to determine if the OpenGL version of OpenGL ES is inside shader code?

Is there a way to tell the shader source that the shader is compiling for OpenGL ES? I want to be able to determine the version using the #version preprocessor directive equal to 100 for OpenGL ES (so the shader compiles for OpenGL ES 2.0), but this is version 110 for OpenGL 2.1).

Is this the best way to do this to place #version as a separate line that is served at the application level or is there a way to do this in the shader?

Another useful thing related to this is to say something like #if version == 100 compile this code, else compile this code. Is this possible in GLSL?

Thanks.

+6
source share
2 answers

Providing #version from the main program suggested by PeterT in the above comment is the only way that will work. The ability to do this (and the ability to define constants without having anything like the one available for the -D compiler) is the main intention of glShaderSource to take an array of pointers, not a simple char* .

The GLSL specification (chapter 3.3) requires that #version be the first in the shader source, with the exception of spaces and comments.

So a thing like

 #ifdef foo #version 123 #endif 

is valid and no such thing will compile (unless the shader compiler is overly permissive, i.e. broken).

On your second question, conditional compilation certainly works and uses it the way you intend to do, which is good.

+5
source

This is also related information:

http://blog.beuc.net/posts/OpenGL_ES_2.0_using_Android_NDK/

You can, for example:

 #ifdef GL_ES precision mediump float; #endif 

To implement OpenGL ES 2.0, you must have the GL_ES macro predefined in the shaders.

+3
source

Source: https://habr.com/ru/post/902829/


All Articles