0
\$\begingroup\$

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

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ thanks for clarifying, i guess i am stuck with string arrays, it makes for ugly program creation code :/ \$\endgroup\$
    – dreta
    May 7, 2012 at 10:16
  • \$\begingroup\$ @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. \$\endgroup\$ May 7, 2012 at 10:38
  • \$\begingroup\$ 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 \$\endgroup\$
    – dreta
    May 7, 2012 at 14:04
  • \$\begingroup\$ @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. \$\endgroup\$ May 7, 2012 at 16:37
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ I think you still mean several mains? Main in both vertex and fragment shaders? Just not in the same source. \$\endgroup\$ May 7, 2012 at 2:28
  • \$\begingroup\$ @ktodisco yep, I'll make sure to make that clear. I got it right? \$\endgroup\$
    – kaoD
    May 7, 2012 at 2:38
  • \$\begingroup\$ 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 \$\endgroup\$
    – dreta
    May 7, 2012 at 2:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .