Graphics / Reference / Shader program creation and linking
Shader program creation and linking
A shader program is formed by attaching compiled shader objects to a program object and linking them. The linked program must then be made active before it is used for drawing.
The vertex shader and fragment shader are compiled separately into shader objects. A shader program object is then created, both compiled shader objects are attached to it, and the program is linked.
A helper of the form linkProgram(vs,fs) performs:
glCreateProgram()
glAttachShader(prog,vs)
glAttachShader(prog,fs)
glLinkProgram(prog)
and returns the linked program object.
Application-side use is:
program = linkProgram(vs,fs);
glUseProgram(program);
glCreateProgram creates the program object. glAttachShader associates the compiled vertex and fragment shader objects with that program. glLinkProgram links the attached stages so their compiled shader code can be used together. glUseProgram selects that linked program as the active program for subsequent rendering commands.
The program object is distinct from the individual compiled shader objects: the shader objects provide the separate stage implementations, while the linked program contains the compatible executable shader stages used together during rendering.
Builds on
Read
- Read the chapter: Shader program creation and linking