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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With