PDA

View Full Version : Entry-Points



jonasmr
03-08-2004, 01:29 PM
Hi.

Is it somehow possible to have multiple entry points in a single shader?

CG has the feature, and it is very convenient, to keep several shaders related to the same effect in one file. I have not been able to find any documentation on how to do it?

Jonas

Korval
03-08-2004, 05:27 PM
What you want can be done in a different way in glslang. A cleaner way.

A compiled shader is like a .o file. It doesn't need to have a main() in it. It can just have some common functions. At link-time, (program building), you can link together multiple compiled shaders, one of which has a main() function. So, effectively, you can have a few shader files that you use as libraries that you link into the final program building section.

mrbill
03-10-2004, 04:38 AM
First, Korval is exactly right. All "main()" vertex or fragment shaders can be compiled as separate shader objects. You attach whichever main shader object you wish to a program object, and then link. The disadvantage is that requires the application to split up the "single text file" into multiple parts.

Another approach is to pass your "single text file" as the *second* string to glShaderSourceARB. (Remember, it takes an array of pointers to strings.) Let's say your "single text file" contains the string



"void foo( void ) { /* ... */ } void bar( void ) { / * ... */ } void foobar( void ) { / * ... */ }\n"
To select the main entry point use the preprocessor and the first string passed.


"#define foo main\n" or "#define bar main\n"

To load your shader strings, you call


glShaderSourceARB( shader, 2, &strings, NULL );

If you choose this approach, the disadvantage is you'll be compiling the "same" source multiple times.

-mr. bill

[This message has been edited by mrbill (edited 03-10-2004).]