Understanding gl_TessCoord Coordinates in GLSL

In the OpenGL Shading Language (GLSL), gl_TessCoord is a built-in vec3 input variable available exclusively within the Tessellation Evaluation Shader (TES) that defines the relative location of a newly generated vertex within an abstract patch domain. This article explains the exact coordinates stored in gl_TessCoord and demonstrates how its components change meaning based on whether the tessellation primitive mode is set to triangles, quads, or isolines.

The Structure of gl_TessCoord

The variable is defined internally as:

in vec3 gl_TessCoord;

While gl_TessCoord is always a three-component floating-point vector, the number of active components and their mathematical interpretations depend on the primitive execution mode specified in the TES input layout qualifier.


1. Triangle Patches (layout(triangles) in;)

When tessellating triangles, gl_TessCoord stores barycentric coordinates:

  • gl_TessCoord.x (\(u\)): Weight of the first corner vertex.
  • gl_TessCoord.y (\(v\)): Weight of the second corner vertex.
  • gl_TessCoord.z (\(w\)): Weight of the third corner vertex.

In this mode, all three components are in the range \([0.0, 1.0]\), and their sum always equals one:

\[u + v + w = 1.0\]

These coordinates are primarily used to interpolate attributes across the triangle's surface:

vec3 position = gl_TessCoord.x * p0 +
                gl_TessCoord.y * p1 +
                gl_TessCoord.z * p2;

2. Quad Patches (layout(quads) in;)

When tessellating quads, gl_TessCoord stores normalized Cartesian \((u, v)\) coordinates:

  • gl_TessCoord.x (\(u\)): Horizontal position across the quad, ranging from \(0.0\) to \(1.0\).
  • gl_TessCoord.y (\(v\)): Vertical position across the quad, ranging from \(0.0\) to \(1.0\).
  • gl_TessCoord.z: Unused and always set to \(0.0\).

These coordinates represent positions on a 2D unit square and are commonly used to perform bilinear interpolation or evaluate 2D parametric surfaces (such as Bézier patches or terrain heightmaps).


3. Isoline Patches (layout(isolines) in;)

When generating isolines (lines), gl_TessCoord also functions as a 2D coordinate vector:

  • gl_TessCoord.x (\(u\)): Normalized position along the current line segment, ranging from \(0.0\) to \(1.0\).
  • gl_TessCoord.y (\(v\)): Normalized index identifying which isoline the vertex belongs to, ranging from \(0.0\) to \(1.0\).
  • gl_TessCoord.z: Unused and always set to \(0.0\).

The tessellator generates lines along the horizontal \(u\)-axis, spaced across the vertical \(v\)-axis according to the outer tessellation levels.


Summary of Coordinate Formats

Primitive Domain gl_TessCoord.x gl_TessCoord.y gl_TessCoord.z Coordinate Type
triangles \(u \in [0, 1]\) \(v \in [0, 1]\) \(w \in [0, 1]\) Barycentric (\(u + v + w = 1\))
quads \(u \in [0, 1]\) \(v \in [0, 1]\) \(0.0\) 2D Cartesian UV
isolines Segment parameter \(u \in [0, 1]\) Line index parameter \(v \in [0, 1]\) \(0.0\) 2D Parametric