Multiple shader objects in a program


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.



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.

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!


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.


Do you need the

vec4 Translate();

prototype at the top of vertex shader 1?

Is there a compiler error if you omit it?


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).]

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.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.