Exporting Functions within a Shader Program.

Hello all.

I'm currently trying to minimize Compile time and resources by reusing Shaders.  My target is not GLSL 4+ with Subroutines, but, I do want to emulate multiple effects per Shader Program.

The Setup is:

  • Shader Program

    • Shader1
    • Shader2

    Assume Shader1 has function, “myfunc”, and Shader2 wants to use it. Without adding the source code together and compiling it into one Shader Program, how do you ‘import’ per-se, the function in Shader2? I’ve seen on the good ol’ interwebs that it could be:


void myfunc() {
  // Do something


void myfunc();

void main() {

Then attaching the objects/linking the program. This, however, comes up as an error as:

error C3002: call to undefined function “void myfunc();”

Any thoughts as to why this is happening?

  • Dennis

how do you ‘import’ per-se, the function in Shader2

You can’t and OpenGL does not support includes which is really annoying ( I believe DirectX does). I wrote a preprocessor to do my own includes so I can have shared code.

Hmm… It’s not so much Includes that I want, it’s the ability to compile Shaders into Modules, and, like a ‘c’ program the GLSL tries to emulate, you can export functions and variables between Objects (with the ‘external’ keyword). The documentation states that including no storage qualifier will result in a global variable (I’m guessing variables that get shared within the program?). If variables are shared within the program, you would think functions would be too. This would save on a lot of source code concatenation magick, resulting in a faster compile (I would assume).

I actually ended up getting it to work. I set it up so each Shader Module can have a ‘library’ of other Modules, and it automatically builds its “main” with the other Shaders functions. Each Shader library has a ‘VertexMain_[Name]’ and ‘PixelMain_[Name]’, and the main Shader Module will automatically external reference them. For example:

void VertexMain_Default();
void VertexMain_Lighting();

void main() {

Each Shader ‘library’ is compiled independently and attached to a Main Shader. In the example above, the “Default” Shader (that has a Lighting Library added to it).

Thank you bearing with my noob-ish beginner mistakes.