GLSL Tutorial – Creating a Program
Prev: Creating a Shader | Next: Setup Example |
The following figure shows the necessary steps to get a shader program ready and going.
The first step is creating an object which will act as a program container. The function available for this purpose returns a handle for the container.
The syntax is as follows:
[stextbox]GLuint glCreateProgram(void)
;
Return Value:
- the program handler
[/stextbox]
We can create as many programs as we want. Once rendering, we can switch from program to program during a single frame. For instance you may want to draw a teapot with refraction and reflection shaders, while having a cube map displayed for background using another shader.
The next step involves attaching the shaders created in the previous subsection to the program we’ve just created. The shaders do not need to be compiled at this time; they don’t even have to have source code. All that is required to attach a shader to a program is the shader handler.
To attach a shader to a program we use the following function:
[stextbox]void glAttachShader(GLuint program, GLuint shader);
Parameters:
program
– the handler to the programshader
– the handler to the shader
[/stextbox]
We can have many shaders of the same type attached to the same program, just like a C program can have many modules. For each shader type there can only be one shader with a main function, also as in C.
We can also attach a shader to multiple programs, for instance if we plan to use the same vertex shader in several programs.
The final step is to link the program. In order to carry out this step the shaders must be compiled as described in the previous subsection.
The syntax for the link function is as follows:
[stextbox]void glLinkProgram(GLuint program);
Parameter:
program
– the handler to the program.
[/stextbox]
After the link operation the shader’s source can be modified, and the shaders recompiled without affecting the program. For the modifications to take effect we must link the program again, just like in C.
As shown in the figure above, after linking the program, there is a function to actually load and use the program, glUseProgram
. Each program is assigned an handler, and we can have as many programs linked and ready to use as we want (and our hardware allows), but only one can be active.
The syntax for this function is as follows:
[stextbox]void glUseProgram(GLuint prog);
Parameter:
prog
– the handler to the program you want to use, or zero
[/stextbox]
Note that calling glUseProgram
with a zero as parameter, although possible, it will leave the shaders undefined, which doesn’t sound like a good thing. One other thing, the program must have been successfully linked to be put to use.
The geometry, and both tesselation shaders, are optional, hence not required for a successfully linked program. The vertex shader is required. The fragment shader is also required unless transform feedback is being used, in which case we may not need the fragment stage.
If a program is in use, and it is linked again, it will automatically be placed in use again, so in this case we don’t need to call this function again.
The following code executes all the required operations, assuming that s1
and s2
are shader handlers created as described in the previous section.
GLuint p; p = glCreateProgram(); glAttachShader(p,s1); glAttachShader(p,s2); glLinkProgram(p); ... // and later on glUseProgram(p);
Prev: Creating a Shader | Next: Setup Example |
2 Responses to “GLSL Tutorial – Creating a Program”
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Passing 0 to glUseProgram returns GL to using the fixed function pipeline.
Hi Richard,
In practice it may be so, but the API page (https://www.opengl.org/wiki/GLAPI/glUseProgram) states that ” the results of shader execution are undefined”. And this is a tutorial for the core version, shaders are required 🙂
Thanks, I guess I should have made it more clear.
Antonio