Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When i'm compiling a shader does it have to be complete? Can i use glCompileShader on a shader without a main() function? The OpenGL reference documentation has a nice writeup on program linking errors, but i can't find one for shader linking, so i have to ask here.

I want to have the ability to have each part of a shader in a different file. So f.e. i'll have a material calculating function and i'll have a "main" shader with the main function that'll only reference the material calculating function. Right now i have my shaderes as arrays of strings that they can read from a file, then i put together an array of string pointeres and compile that into a single shader (vertex, fragment, geometry). But if i could compile each invidividual shaders (parts of vertex shaders, instead of the whole vertex shader put together in an array of strings) and put them together when i'm linking the program, that would make the code much clearer and i could move the shader compilation code from the program managing code to the shader object itself (the one that load the string from file and exposes it).

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

The compilation model for OpenGL is described on the Wiki. But for your needs, I would suggest you simply use the fact that glShaderSource takes multiple strings. These strings effectively act as header files; the shader stitches them together and compiles them as a single unit.

The "linking" model within a shader stage is not something I would... rely on. Not because of the specification, but because it's not widely used. And that means it's more likely to have driver bugs in it. Stick to the path most commonly used and just use multiple source strings. That way, your code is more likely to work.

share|improve this answer
    
thanks for clarifying, i guess i am stuck with string arrays, it makes for ugly program creation code :/ –  dreta May 7 '12 at 10:16
    
@dreta: How would having multiple shader objects be any less ugly? It's more or less the same thing, except one is more likely to actually work. –  Nicol Bolas May 7 '12 at 10:38
    
i wanted to create a shader object that would only take the file name in the constructor and shader type and let it do all the data reading ,allocating, compiling and cleaning after itself, then all i'd have to do is create a shader object with the file name and type and just attach the pointer it'd have, it looks cleaner in the program managing code and it's RAII –  dreta May 7 '12 at 14:04
    
@dreta: I still don't understand how that's less ugly. One filename, an array of filenames, what does it matter? How does that not make it "RAII"? You'd still have to pass multiple pointers to the program object, which is the more important, more useful object. So either way, you're passing someone a list of things in its constructor. –  Nicol Bolas May 7 '12 at 16:37
add comment

You can compile shaders without main. This makes sense since a program can only have one main per shader type (i.e. vertex, fragment...) but several shaders of the same type can be linked.

share|improve this answer
    
I think you still mean several mains? Main in both vertex and fragment shaders? Just not in the same source. –  ktodisco May 7 '12 at 2:28
    
@ktodisco yep, I'll make sure to make that clear. I got it right? –  kaoD May 7 '12 at 2:38
    
i just want few files for the vertex shader, one with main, one with a function f.e., i want to know if it's possible to compile these separate files and then link them together in a program –  dreta May 7 '12 at 2:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.