OpenGL Rendering Pipeline
Tessellation is the stage in the OpenGL rendering pipeline where patches of vertex data are subdivided into smaller Primitives. This process is governed by two shader stages and a fixed-function stage.
|This article is a stub. You can help the OpenGL Wiki by expanding it.|
The tessellation process is divided into three stages which form an optional part of the rendering pipeline. Two of the stages are programmable; between them is a fixed function stage. They are described below, in the order they are processed.
Tessellation stages operate on patches, a primitive type denoted by the constant GL_PATCHES. These are arrays of vertices and user defined per-vertex attributes written by a vertex shader. The number of vertices per patch can be defined on the application-level using:
void glPatchParameteri(GLenum pname, GLint value);
with GL_PATCH_VERTICES as target and a value which has is on the half-open range [1, GL_MAX_PATCH_VERTICES]. The maximum number of patch vertices is implementation-dependent, but will never be less than 3.
Tessellation control shader
The first step of tessellation is the optional invocation of a tessellation control shader (TCS). The TCS has two jobs:
- Determine the amount of tessellation that a primitive should have.
- Perform any special transformations on the input patch data.
The TCS can change the size of a patch, adding more vertices per-patch or providing fewer. However, a TCS cannot discard a patch (directly; it can do so indirectly), nor can it write multiple patches. Therefore, for each patch provided by the application, one patch will be provided to the next tessellation stage.
The TCS is optional. If no TCS is active in the current program or program pipeline, then the patch data is passed directly from the Vertex Shader invocations to the tessellation primitive generation step. The amount of tessellation done in this case is taken from default values set into the context. These are defined by the following function:
void glPatchParameterfv(GLenum pname, const GLfloat *values);
When pname is GL_PATCH_DEFAULT_OUTER_LEVEL, values is a 4-element array of floats defining the four outer tessellation levels. When pname is GL_PATCH_DEFAULT_INNER_LEVEL, values is a 2-element array of floats defining the two inner tessellation levels.
These default values correspond to the TCS per-patch output variables gl_TessLevelOuter and gl_TessLevelInner.
Tessellation primitive generation
Primitive generation is a fixed-function stage responsible for creating a set of new primitives from the input patch. This stage is only executed if a tessellation evaluation shader (TES) is active in the current program or program pipeline. Primitive generation is affected by two factors:
- the input primitive type defined by the subsequent TES which may be one of triangles, quads or isolines
- the tessellation levels, provided either by the TCS or the default values, as stated above.
Depending on the primitive type, the primitive generator evaluates a different number of tessellation levels and applies different tessellation algorithms. Each generated vertex is assigned a normalized position (i.e. in [0, 1]) denoted (u, v, w) or (u, v). The coordinates are accessible using the built-in in vec3 gl_TessCoord during tessellation evaluation where gl_TessCoord.xyz is equivalent to the triple (u,v,w).
The first inner tessellation level and the first three outer tessellation levels are used. The algorithm is required to produce smaller triangles. The resulting vertex positions are barycentric coordinates, which specify weights of the vertices of the input triangle for which the condition u + v + w = 1 holds.
For quads all six tessellation levels are used to subdivide the input rectangle into smaller triangles.
They are only affected by the first two outer tessellation levels. The generator will create independent lines, which will be subdivided into m segments with m + 1 vertices, where:
The coordinates of the vertices are determined by subdividing the line along the -axis and determining a constant value along the -axis for every vertex on the current line using the formula:
As can be seen, the the generator will not create a line at v = 1. During isoline tessellation, the w-coordinate is undefined.
Tessellation evaluation shader
The tessellation primitive generator takes a primitive and tessellates it. However, it has no idea of the algorithm that you intend to use to compute the new positions/normals/texture coordinates/etc. It's a purely fixed-function system.
The burden on computing the final position of all of the vertices in the tessellated patch falls to the tessellation evaluation shader (TES). Each TES invocation takes a coordinate representing where that vertex is within the tessellated patch, and each TES invocation can access every vertex output by the TCS as well as any per-patch data. The purpose of the TES is to actually do the hard part of tessellation: computing the position, texture coordinates, normals, and so forth, of all of the resultant vertices.
The TES is rather like a vertex shader, in that each invocation operates on a distinct vertex within the tessellated patch. Also, the TES cannot cull vertices or in any way modify them.
A TES is required to have tessellation at all.