Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Simple question:

In GLSL, is there a way to share functions across multiple shaders, or do I have to define all functions in every shader that needs them?

share|improve this question
up vote 12 down vote accepted

You can define some functions in a header file and #include them into your shader. It's a bit different from C/C++ in that you'd put the bodies of your functions in the headers, not just their prototypes (since shaders have no concept of separate compilation & linking), but other than that it's just like C/C++ headers.

share|improve this answer
Note at least that #include is not actually supported in any version of GLSL up through 3.30, except via an extension that is not universally available. Any decent shader framework should extend the language to provide this feature, though (including Cg). – Sean Middleditch Apr 10 '12 at 7:06

If #include is not available you would use the arguments of glShaderSource to specify the shared stuff. Example:

char *sharedcode = "...shared code here...";
char *fs1 = "...fragment shader 1...";
char *fs2 = "...fragment shader 2...";

char *awesomeeffect1[] = {sharedcode, fs1};
char *awesomeeffect2[] = {sharedcode, fs2};

glShaderSource (shader1, 2, awesomeeffect1, NULL);
glShaderSource (shader2, 2, awesomeeffect2, NULL);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.