What is a Geometry Shader in OpenGL?

Unlike a Vertex or Fragment shader, a geometry shader can create new geometry. The geometry shader receive its input from the Vertex/Tessellation shaders and provides its output to the rasterizer.

The geometry shader, although able to generate new geometry, are not intended to provide Level Of Detail (LOD) capability. The reasons are the following:

• Geometry Shaders have a limit to the number of new vertices they can create.
• Geometry Shaders have limited access to the surrounding vertex information.
Figure 1. Geometry Shader Stage Topologies of a Geometry Shader

Geometry shaders can accept limited topologies. The acceptable topologies are:

• Points
• Lines
• Triangles

In the same manner, geometry shaders can only generate a limited number of topologies such as:

• Points
• Line strips
• Triangle strips

There is no correlation between the input topologies and the output topologies. For example, points can generate triangles, triangles can generate triangle strips.

Figure 2 illustrates the operations that geometry shaders can do. A geometry shader needs to be told what topology to expect and it needs to be told what topology to generate. This information can be supplied to the shader either in the shader program or through the OpenGL API.

The GLSL Layout statements informs the geometry shader the incoming/outgoing primitive topology. For example, the code in listing 1 informs the shader what topology is receiving:

Listing 1. Input topology
layout( topology ) in;

where the input topology must be one of the following:

• points
• lines
• triangles

The geometry shader must also be inform of the topology to generate. This is done by using the following GLSL statement as shown in listing 2:

Listing 2. Output topology
layout( topology, max_vertices=num ) out;

where topology must be one of the following:

• points
• line_strip
• triangle_strip

and num is the maximum number of vertices this shader will emit.

OpenGL API

Aside from the GLSL layout statements, you can also use OpenGL API functions to inform the geometry shader what type of topology to receive or generate. Such OpenGL API function is glProgramParameteri(). This function sets various parameters concerning the operation of the geometry shader.

To use this function, you must:

-Provide the number of vertices the geometry shader will be producing. This is done as follows:

glProgramParameteri(programName,GL_GEOMETRY_VERTICES_OUT,intvalue)

where intvalue is the number of vertices.

-Provide information about the primitive type that will be sent to the geometry shader. This is done as follows:

glProgramParameteri(programName,GL_GEOMETRY_INPUT_TYPE,intvalue)

where intvalue can be any of the following values:

• GL_POINTS
• GL_LINES
• GL_TRIANGLES

-Finally, you must also provide the primitive type that will be generated by the geometry shader. This is done as follows:

glProgramParameteri(programName,GL_GEOMETRY_OUTPUT_TYPE,intvalue)

where intvalue can be any of the following values:

• GL_POINTS
• GL_LINE_STRIP
• GL_TRIANGLE_STRIP

If you want to know more about geometry shaders, I recommend you read the following book. It is one of my favorite books. Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.