OpenGL SDK: Simple Tessellation Shader
In the last OpenGL SDK post I introduced the new SDK, now let me introduce our first new sample, “Simple Tessellation Shader”. As the name implies, it demonstrates the minimum parts necessary to use tessellation shaders in OpenGL. The sample only demonstrates tessellating the quad domain (rather than the triangular domain), and it shows two modes: one with a flat plane and one where the plane is wrapped into a torus.
About Tessellation Shading
OpenGL recently added the concept of tessellation shading to its definition of the graphics pipeline. It expands the pipeline by two additional programmable stages and a single fixed-function stage that handle the specification and evaluation of a tessellated surface. It all fits into the pipeline like the diagram below.
These new tessellation stages operate between the vertex and geometry shader stages previously available in the pipeline. The initial stage is the known as the “tessellation control shader” (TCS). This shader accepts a list of vertices defining a patch from the vertex shader and controls the amount of tessellation applied to the patch. The next tessellation stage is the fixed-function tessellator. The tessellator takes the amount of tessellation provided by the TCS and computes a pattern of triangles in a parametric space. (For our example today, the space is a quadrilateral with u and v directions.) Finally, the “tessellation evaluation shader” (TES) is executed at least once for each vertex that was created in the parametric space. The TES takes the parametric coordinate and the patch data output by the TCS to generate a final position for the surface.
Diving in a bit deeper, the TCS is one of the more complex shader stages in the pipeline due to all of the options it controls. The TCS specifies the number of vertices it creates in a patch via a layout statement. (e.g. layout (vertices=1) out ) In the TCS, each output will have its own thread to generate the output vertices. One thing that makes the TCS special, is that the threads can all see all of the input data, and at the end of the shader, they can share their results to allow group computations for items like patch-wide tessellation levels. (These are more advanced features that we’ll skip in this sample)
Moving on the tessellator, it is very much a black-box from the point of view of the graphics programmer. All it really does is generate a sequence of u.v coordinates and an associated topology map to control how the patch is converted to triangles. While the amount of tessellation is controlled by data output by the TCS, the TES declares an input which controls the pattern of triangles generated. Once the points are generated, the tessellator launches one thread per point to the TES.
Finally, the TES evaluates and transforms points into clip space to de...