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
#versionfrom 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-Dcompiler switch available) is the main intent behindglShaderSourcetaking an array of pointers rather than a simplechar*.The GLSL specification (chapter 3.3) requires that
#versionbe the first thing in a shader source, except for whitespace and comments.Thus, no such thing as
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.