Geometry Shaders

Introduced in OpenGL 3.2

Geometry shaders are the final programmable pipeline stage prior to the rasterization step that feeds the fragment shader. Regardless of the "GLenum mode" that was used on a glDrawArrays of glDrawElements call, the only shapes that actually are in the pipeline at that point are individual points, lines, or triangles. Geometry shaders allow the incoming shape to be modified in some way, including changing its type (e.g., point to triangle).

Geometry shaders are specific to a given input shape type (point, line, or triangle), hence care must be taken to have a geometry shader active only when a matching draw call has been used. For example, if I have a geometry shader that modifies an incoming triangle in some way, the shader can only be active (i.e., from a glUseProgram call) when a glDrawArrays or glDrawElements call has been made in which the type is some GL_TRIANGLE*. Errors will be recorded otherwise. It is common to have multiple shaders in an OpenGL program, and then to do a glUseProgram to establish the program needed for a given primitive call.

What follows is one simple example each of a "type-changing" and "type-preserving" geometry shader.