Reputation: 3218
I'm currently trying to reduce the size of my main function in a GLSL OpenGL ES shader on iOS. Therefore I extracted some helper functions and declared and compiled them in a different shader object. Afterwards I wanted to re-link them back together by using glAttachShader before linking the program that then is used for rendering. All the setup is already done, unfortunately the shader containing my main function won't compile since it can't find the reference to the functions declared in a separate shader. How do I tell the compiler to include the references?
Upvotes: 2
Views: 2072
Reputation: 54572
There is some mixed up terminology in your question. There are two related but different constructs involved here:
A function declaration (aka prototype), which only declares the return type, function name, and arguments. It has the form:
returnType functionName(type0 arg0, type1 arg1, ..., typen argn);
A function definition, which has all parts of the declaration, as well as the body that implements the function. It has the form:
returnType functionName(type0 arg0, type1 arg1, ..., typen argn) {
// do some computation
return returnValue;
}
If you split off function implementations into different shaders, the function definition is in that separate shader that contains the actual code for the function. But you will still need a declaration in the shader that contains the main function, or any other shader that calls it.
Once the function is declared, you can call it, even if the implementation is in a different shader. From the spec:
All functions must be either declared with a prototype or defined with a body before they are called.
Upvotes: 3