PDA

View Full Version : Compile time shader stage information in GLSL



_blitz
02-23-2013, 11:51 AM
As far as I know there currently is no way to retrieve the stage for which the GLSL compiler is generating code.
Such a feature could ease the development of GLSL libraries whose functions can be evaluated accross multiple stages.
Here's an example of source code which could be included at any stage and is safe to use:
// check GLSL compiler version
#if (__VERSION__ < 150)
#error Unsupported GLSL version
#endif

// some function (shading function for example)
vec3 shade(...) {
#if (__STAGE__ == GL_vertex_stage)
perform per-vertex operations
#elif (__STAGE__ == GL_fragment_stage)
per-fragment operations
#else
// either halt compilation...
#error Invalid stage
// ... or return something
return vec3(0);
#endif
}
On the client side, one would simply use

const GLchar * files[] = {"library.glsl", "main_function.glsl"};
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shader, 2, load_strings(2, files), NULL);
Granted, this can already be accomplished by including a string in glShaderSource, but having the compiler doing it would save some time and ease the development of safer and more portable GLSL libraries... What do you think ?

Alfonse Reinheart
02-23-2013, 04:25 PM
It's already been discussed (http://www.opengl.org/discussion_boards/showthread.php/179244-GLSL-variable-telling-which-shader-stage-is-compiled). In the future, you should look around a bit to see if something you want to proposed has already been proposed.

_blitz
02-23-2013, 05:04 PM
It's already been discussed (http://www.opengl.org/discussion_boards/showthread.php/179244-GLSL-variable-telling-which-shader-stage-is-compiled). In the future, you should look around a bit to see if something you want to proposed has already been proposed.I should have linked this in my post as it is indeed related to what I am asking for. I guess I am adding another argument in favour of this proposal: users can still use multiple strings, compile time stage improves shader library code portability. There already are some (https://github.com/ashima/webgl-noise) great (https://github.com/BrianSharpe/GPU-Noise-Lib) GLSL libraries available which are easy to share since they do not use built-in functions whose behaviour vary depending on the shader stage (texture, dFdx, etc.). Adding such a feature could allow richer libraries to emerge without forcing users to print strings for each library they use.