# Tessellation Controll Shader how to get maximum subdivisions limit?

I have a low-poly mesh and I want to add structure on the surface.

The setup: The mesh consists of triangles. Within the TCS the pixel-length for each of the three edges will be calculated and subdivided according to the given allowed u_MAX_LENGTH_TRIANGLE_EDGE. This will be done in the following way:

vec4 vertexPosScreen;
for (int i = 0; i < 3; i++)
{
vertexPosScreen[i] = vec4(mvp * gl_in[i].gl_Position);
vertexPosScreen[i] = vertexPosScreen[i] / vertexPosScreen[i].w;
vertexPosScreen[i].x *= u_viewportWidth;
vertexPosScreen[i].y *= u_viewportHeight;
}
float edgeLength;
edgeLength = length(vertexPosScreen.xy - vertexPosScreen.xy);
edgeLength = length(vertexPosScreen.xy - vertexPosScreen.xy);
edgeLength = length(vertexPosScreen.xy - vertexPosScreen.xy);

float edgeLod;
for (int i = 0; i < 3; ++i)
{
edgeLod[i] = max(1.0, edgeLength[i] / u_MAX_LENGTH_TRIANGLE_EDGE;
}

gl_TessLevelOuter = edgeLod;
gl_TessLevelOuter = edgeLod;
gl_TessLevelOuter = edgeLod;

gl_TessLevelInner = max(edgeLod, max(edgeLod, edgeLod));


This guarantees, that in the final image all triangles edges are smaller than u_MAX_LENGTH_TRIANGLE_EDGE.

But:
The GPU is not able to subdivide as mush as wanted. So if we are too close to a triangle, or a triangle is too tall at all, the TCS will subdivide it as mush as possible, but not enough.

My Idea:
After tessellation is done, the new generated triangles should be stored into a new VBO during the TES step. Afterwards the fresh stored triangles in the new VBO will be rendered again and subdivided as well until all of them are small enough.

Now the big question
How are the tessellation limits defined? per edge division? And how can I ask for the maximum division on CPU? glGetInteger? Or is it limited during the number of generated vertices?

I think the easiest solution would be check directly after "edgeLod" is calculated, if edgeLod is too high. If this is the case, write something into a SSBO and loop the shaderprogram again.

The tessellation limit is GPU dependent..Any number above the GPU limit is technically an error but most gpu's just clamp to the upper limit. To get beyond the limit either use a Level Of Detail (LOD) approach where you have different models that are used based on distance, or save and recompute like you suggest.

To get the max just set all the edges to the limit AND set all the internals to the limit in the tessellation control shader:

#version 460 core

layout(vertices = 4) out;

void main(void)
{
gl_TessLevelOuter = 64.0;
gl_TessLevelOuter = 64.0;
gl_TessLevelOuter = 64.0;
gl_TessLevelOuter = 64.0;

gl_TessLevelInner = 64.0;
gl_TessLevelInner = 64.0;

gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}


And how can I ask for the maximum division on CPU?

Yes use glGetInteger. At the bottom of this page is all the values you would be intrested in: Khoronos Tessellation limits, search for tessellation on the page

• Thanks a lot! GL_MAX_TESS_GEN_LEVEL is what I was looking for. Oct 8 '20 at 15:05