# Difference between revisions of "Tessellation"

Core in version 4.5 4.0 ARB_tessellation_shader
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.

## Overview

The tessellation process is divided into three stages which form an optional part of the rendering pipeline, two of which are programmable and one which is fixed as follows (in order).

The first step of tessellation is the invocation of an optional tessellation control shader (TCS). The purpose of this stage is to do any special transformation on the patch and to determine how much tessellation the particular patch should have.

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[4]​ and gl_TessLevelInner[2]​.

## 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|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 tesselation evalutation where gl_TessCoord.xyz​ is equivalent to the triple (u,v,w).

### Triangles

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.

### Isolines

They are only affected by the first two outer tessellation levels. The generator will create $n$ independent lines, which will be subdivided into m segments with m + 1 vertices, where:

$m\,= OuterTessLevel[0]$
$n\,\,= OuterTessLevel[1]$

The coordinates of the vertices are determined by subdividing the line along the $u$-axis and determining a constant value along the $v$-axis for every vertex on the current line using the formula:

$0{,}\frac{1}{n}{,}\frac{2}{n}{,} .. {,}\frac{n-1}{n}$

As can be seen, the the generator will not create a line at v = 1. During isoline tessellation, the w-coordinate is undefined.

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.

## Patches

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.