Search code examples
openglincludeglslshaderpragma

OpenGL / GLSL - using glShaderSource with multiple source to support include files


Since GLSL doesn't have an include-file option I'm trying to add this by using a "#pragma include" parser. (I want this because I have some generic methods I'd like to implement only once but might use in many different shaders, like Ken Perlin's smootherstep)

Parsing the shader files to get the included files works fine. But when setting the ShaderSource with the multiple source files I always run into problems; since my main shader normally starts with "#version 330" and the #version-line always has to be the first line in the shader, I can only add my main file as the first in the array of strings passed to glShadersource, and after that all the included files. But then my main file can't use any functions implemented in those included files, since they'll essentially be concatenated after my main file so the compiler complains that it doesn't know the functions my main file is using.

The only way I can think of to get it to work is to read the main file, parse the include pragma, and then replace that pragma line with the file-contents of the file to be included (recursively applying this same method to all included files that might include other files themselves) - but that would mean the linenumbers in compilation-errors would not match the real linenumbers of the main file anymore.

Has anybody set up some sort of "include" functionality for GLSL that works AND keeps the linenumbers ? And if so - how ?


Solution

  • What about parsing for the #version tag and making this a seperate (and the first) source string? This way you may also have different version tags in the includes (depending on their functions requirements) and just reduce them to only one version tag (the one with the highest version number).