Attach Shader to multiple Programs

Just wondering if there is any real benefit to attaching a shader to multiple programs? Or maybe there’s even a cost sometimes?
I assume it’s to use less resources, but does it actually in current implementations?

I am not sure what you mean…

Are you referring to sharing the OpenGL context between applications?

Or re-using the same vertex, fragment or geometry shader across different linked shader programs?

If I have misunderstood can you elaborate?

Maybe Stuart means, compile a vertex shader then attach it multiple times to different programs. For instance, the (same) pass through vertex shader could perhaos be useful in many different programs that do image processing (no need to write and compile the same source code over and over). In theory, it uses less resources (time) because you only compile it once. In practice, and I’m really guessing here, the shaders you’re likely to share are simple shaders. I’m also taking a wild guess that the compiler still has some heavy stuff to do when the program is being linked (optimizations, resource allocation/scheduling).

Yes I mean taking the shader ID from glCreateShader and using it in multiple glAttachShader calls. As mentioned I would have thought the main task is linking so is there really any benefit to doing this? I allow it in my code, e.g. to “share” a shader that implements the fixed function fog, but I’m thinking it’s maybe just a complication.

I tried a similar functionality in our engine but didn’t find any actual gain. The real work is only done when the linking occurs. This was tested on Nvidia G80+ hardware.

I am under the impressive that this is the case. See this post on shader stitching. As someone mentioned earlier, I’ve also found limited use to sharing entire shader objects across shader programs. Typically, I share individual functions, which are included using string concatenation (linking together several shader objects of the same type had bugs when I tried it).

Patrick