# Sharing functions across multiple shaders

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?

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.

• 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};
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.17347514629364014, "perplexity": 5331.873209436696}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-34/segments/1596439735909.19/warc/CC-MAIN-20200805035535-20200805065535-00281.warc.gz"}