GLSL - named entry point

Currently the entry point for all GLSL shaders must be called “main”. This can make shader management in your code something of a pain in the ass, as you can’t just drop a bunch of related shaders into a single source file and have glCompileShader pick up the correct one without doing some fancy footwork to get around it.

So a simple proposal: glCompileShaderNamedEntryPoint (GLuint shader, const GLchar *entrypoint) to allow user-supplied named entry points. This is proposed for glCompileShader rather than glShaderSource as convenience can be had by specifying a single glShaderSource call followed by a bunch of glCompileShaderNamedEntryPoint calls, thereby allowing shader source to be read from and specified from a single file for multiple shaders. The orginal glCompileShader is functionally identical to glCompileShaderNamedEntryPoint (shader, “main”).

This can make shader management in your code something of a pain in the ass, as you can’t just drop a bunch of related shaders into a single source file and have glCompileShader pick up the correct one without doing some fancy footwork to get around it.


#define TheActualEntrypoint main

It’s that simple. Just stick that string in front of the shader when you compile it. Odds are good that in any system based on putting a bunch of stuff in a single shader file that you’re going to have specialized #defines anyway. So it’s not like one more will be noticed.

Plus, this will work with glCreateShaderProgramv, whereas your version needs two new functions.

This is not something that needs to be a feature.

The trick with using a #define directive is good and I should try that.

Another option that I have been using is GL shader wrangler: prideout .

[QUOTE=Alfonse Reinheart;1242190]


#define TheActualEntrypoint main

It’s that simple. Just stick that string in front of the shader when you compile it. Odds are good that in any system based on putting a bunch of stuff in a single shader file that you’re going to have specialized #defines anyway. So it’s not like one more will be noticed.

Plus, this will work with glCreateShaderProgramv, whereas your version needs two new functions.[/quote]

Except it doesn’t work with current AMD drivers. Oh, it works OK if there’s only one faked “main” function in the file, but as soon as you put 2 or more in, it breaks.

It would be a well-specified part of the API that is required to be supported. Who cares if it needs an extra entry point?

Except it doesn’t work with current AMD drivers. Oh, it works OK if there’s only one faked “main” function in the file, but as soon as you put 2 or more in, it breaks.

Then that’s something that should be taken up with AMD. What kind of error do you get when that happens?

We shouldn’t create features based on driver bugs.

[QUOTE=Alfonse Reinheart;1242190]


#define TheActualEntrypoint main

It’s that simple. Just stick that string in front of the shader when you compile it. [/QUOTE]

You can add a #define, however, not as the first line of your shader (unless you are talking version 1.10). Section 3.3 of the GLSL specs state that “The #version directive must occur in a shader before anything else, except for comments and white space.” - so, no #defines are allowed before that.

Using OpenGL 4.x, shader subroutines might be an alternative:



subroutine uniform myMain MyMain;       

subroutine(myMain)
void main1() {...}

subroutine(myMain)
void main2() {...}

void main() {
    MyMain();
}