GLSL and multi file linking

Sage
Posts: 1,199
Joined: 2004.10
Post: #1
Somehow, long ago, I got the impression that Apple's GLSL didn't support multi file linking. E.g., having a library of common user-specified functions and linking them with your code. I don't know if I got that impression here, or on Apple's mac-opengl mailing list.

So, I'm curious, is this actually true?

I've got a lot of code duplication in my shaders and as my shader library grows, the duplication just gets worse and worse. Wacko

My shader loading code is flexible enough that I could fake linking, by inserting common code directly as text. But if Apple's GLSL supports multi file linking I'd prefer to do this the right way.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
AFAIK, Apple have been doing this right for a while. I recall it being buggy in the very early days, but I think it got straightened out. If you find a bug, file it Wink

NVIDIA/PC however was (and may still be) a different story. I had to do the "#include" approach to multi-file linking you mention on at least one occasion.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #3
I'll give it a shot!
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #4
OK, time for results.

I initially took the lazy route and implemented support for #include "path/to/shader" syntax, which does the good old preprocessor crapulence by inserting the included file directly into my shader. It worked, so I left it alone. But it made shader debugging harder since line-numbers were off.

So, I decided to give a stab at proper multi-file linking. I implemented support for the following syntax:

Code:
#pragma link "relative/path/to/file"
#pragma link <file/in/search/paths>

What's interesting is that this both does and does not work. The linked file is compiled and the whole program links correctly. If, however, I attempt to invoke a custom function from that file, the GLSL compiler complains that it doesn't know anything about that function.

However, if I prototype the functions from the linked file in my shader code, it works well.

For example:
File red.fs
Code:
vec4 Red()
{
    return vec4(1,0,0,1);
}

File shader.fs
Code:
#pragma link "red.fs"

// prototype the function I need to call
vec4 Red();

void main()
{    
    gl_FragColor = Red();
}

Is this just how it has to be? E.g., any functions defined in the linked files have to be prototyped in the calling shader before they can be used? Or is there some sort of C-style so-long-as-you-get-your-params-right-it-works magic linker available?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
I believe prototypes are required.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Problem linking GLSL Ilyasim 4 3,775 Dec 18, 2007 12:46 PM
Last Post: OneSadCookie
  Linking multiple glsl source files into one program TomorrowPlusX 5 7,055 Nov 2, 2006 02:18 PM
Last Post: OneSadCookie
  ARB Multi textures NYGhost 2 2,490 Mar 19, 2003 01:52 PM
Last Post: NYGhost