Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to tell if the OpenGL version is OpenGL ES within the shader code?

Is there any way to tell within the source code for a shader that the shader is being compiled for OpenGL ES? I want to be able to define the version using the #version preprocessor directive to be 100 for OpenGL ES (so that the shader compiles for OpenGL ES 2.0), but is version 110 for OpenGL 2.1).

Is the best way to do this to place the #version as a separate string which is fed in at the application level, or is there a way to to do this within the shader?

Another useful, related thing to be able to do would be to say something like #if version == 100 compile this code, else compile this code. Is this possible within GLSL?

Thanks.

like image 727
James Bedford Avatar asked Dec 01 '11 10:12

James Bedford


2 Answers

Prepending #version from the main program as PeterT suggested in the above comment is the only way that will work. Being able to do this (and being able to define constants without having something like a -D compiler switch available) is the main intent behind glShaderSource taking an array of pointers rather than a simple char*.

The GLSL specification (chapter 3.3) requires that #version be the first thing in a shader source, except for whitespace and comments.

Thus, no such thing as

#ifdef foo
    #version 123
#endif

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

About your second question, conditional compilation certainly works and using it in the way you intend to do is a good thing.

like image 164
Damon Avatar answered Sep 28 '22 07:09

Damon


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

OpenGL ES 2.0 implementations are required to have a GL_ES macro predefined in the shaders.

like image 20
juzzlin Avatar answered Sep 28 '22 06:09

juzzlin