View Full Version : Multiple shader objects in a program

12-16-2003, 12:52 AM

This one has been bugging me for a while now. In the GL_ARB_shader_objects spec, under 2.14.2 Program Objects, it says

It is permissible to attach multiple shader objects of the same type to a single program object, and it is permissible to attach a shader object to more than one program object.

The latter half of this is fine (sharing vertex/fragment shaders across multiple programs).

The former half, though, has got me exercising those little grey cells. So I think I must be missing something fundamental.

Why would you want to attach two (or more) vertex/fragment shaders to a program object? What purpose does this achieve?

Are they run on each vertex/fragment in the order in which they are attached?

Is it for having "combo" effects, such that you don't have to write specialist shaders for each combination; instead just attach different effects?

What are the performance implications of doing so?

If anyone can shed any light on this, it'd be appreciated.



12-16-2003, 01:28 AM
I think the meaning of this to have a shader object for a function. For example in Vertex Shader one function that translates the gl_vertex to gl_position. I think that you are able to use one seperate shader object for this function and this function can then be called by another shader object.

Maybe I'm wrong but I think this is the idea behind that.

12-16-2003, 01:38 AM
Ooh, right, so instead of inlining functions other than 'main' in a single shader object, you separate them (hence allowing code sharing) into different source files and hence different shader objects.

There's no #include preprocessor directive in the language so I guess you won't need prototypes... and I guess any missing functions would be picked up as a linkage error.

That's a rather plausible explanation. Thanks!


12-16-2003, 01:53 AM
Yeah, I just tried that with the following shaders:

// Test Vertex Shader 1

vec4 Translate();

void main()
gl_Position = Translate();

// Test Vertex Shader 2

vec4 Translate()
return gl_ModelViewProjectionMatrix * gl_Vertex;

// Test Fragment Shader

void main()
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);

It works without problems with ATI on a R350.

12-16-2003, 01:58 AM

Do you need the

vec4 Translate();

prototype at the top of vertex shader 1?

Is there a compiler error if you omit it?


12-16-2003, 02:01 AM
Yeah, you have to define the function.
Without that the shader won't be compiled. Info Log:

ERROR: 0:7: 'Translate' : no matching overloaded function found
ERROR: 0:7: 'assign' : cannot convert from 'const float' to 'Position 4-component vector of float'
ERROR: 2 compilation errors. No code generated.

[This message has been edited by Corrail (edited 12-16-2003).]

12-20-2003, 07:55 PM
There isn't a #include header definition, but you are allowed to pass multiple strings in to the shader compiler for a single shader. That way, you can create standard includes that you can share between different shaders that are going to be linked together.