<p>After a whole day of brain stress, I found a method to enumerate them:</p>
<pre><code>//floats equal? with respect of epsilon
bool isNearlyTheSame(float value0, float value1) 
{
    return abs(value0 - value1) &lt; 0.001;
}

uint getMinIndex(vec3 vector)
{
    uint y = uint(vector.y &lt;= vector.x + 0.001 &amp;&amp; vector.y &lt; vector.z);
    uint z = uint(vector.z &lt; vector.x &amp;&amp; vector.z &lt;= vector.y + 0.001)*2;
    return y + z;
}

//result: x: 0 =&gt; inner triangles, 1 =&gt; inner most vertex, 2 =&gt; outer vertices
//result: y: lowest barycentric value is: 0 =&gt; x, 1 =&gt; y, 2 =&gt; z  
uvec2 getBarycentricCase()
{
    uvec2 baryCase = uvec2(0,0);
    baryCase.y = getMinIndex(gl_TessCoord);
    if(gl_TessCoord.x == 0 || gl_TessCoord.y == 0 || gl_TessCoord.z == 0)
        baryCase.x = 2;
    if(isNearlyTheSame(gl_TessCoord.x, gl_TessCoord.y) &amp;&amp; isNearlyTheSame(gl_TessCoord.y, gl_TessCoord.z))
        baryCase.x = 1;
    return baryCase;
}

//returns a third of the count of the inner vertices with respect to the tessLevel
uint getNummTessPart(uint tessLevel)
{
    float oddAdd = mod(tessLevel, 2) == 0 ? 0 : 1;
    return tessLevel / 2.0 * (tessLevel / 2.0 - 1) + oddAdd;
}

//returns the current patch vertex id with respect to this algorithm
uint getPatchVertexID()
{
    uint result;
    uvec2 baryCase = getBarycentricCase();
    uint outerTesslevel0 = uint(gl_TessLevelOuter[0]);
    uint outerTesslevel1 = uint(gl_TessLevelOuter[1]);
    uint outerTesslevel2 = uint(gl_TessLevelOuter[2]);
    uint innerTesslevel = uint(gl_TessLevelInner[0]);

    uint numTessPartInner = uint(getNummTessPart(innerTesslevel));
    float baseValue = min(min(gl_TessCoord.x,gl_TessCoord.y), gl_TessCoord.z);
    float leftValue = baryCase.y == 0 ? gl_TessCoord.z : baryCase.y == 1 ? gl_TessCoord.x : baryCase.y == 2 ? gl_TessCoord.y : 0;

    if(baryCase.x == 0) //inner vertices
    {
        float stepUp = 2.0 / 3.0 / innerTesslevel;
        float stepLeft = 1.0 / innerTesslevel;
        uint positionUp = uint(round(baseValue / stepUp));
        uint positionLeft = uint(round(leftValue / stepLeft + (positionUp) * stepUp * innerTesslevel / 2 - positionUp));
        result = numTessPartInner - uint(getNummTessPart(innerTesslevel - ((positionUp-1) * 2)));
        result += positionLeft;
        result += baryCase.y * numTessPartInner;
    }
    else if(baryCase.x == 1) //center vertex
    {
        result = numTessPartInner * 3;
    }
    else if(baryCase.x == 2) //outer
    {
        uint maxOuterTessLevel = max(max(outerTesslevel0, outerTesslevel1), outerTesslevel2);
        bool hasCenter = (mod(innerTesslevel,2) == 0 &amp;&amp; innerTesslevel &gt; 1) || (maxOuterTessLevel &gt; 1 &amp;&amp; innerTesslevel &lt; 1);
        bool hasInner = innerTesslevel &gt; 2;
        uint outerAdd[3] = {0, outerTesslevel0, outerTesslevel0 + outerTesslevel1};
        result = uint(hasInner) * numTessPartInner * 3;
        result += uint(hasCenter);    
        result += uint(round(leftValue * gl_TessLevelOuter[baryCase.y])) + outerAdd[baryCase.y];
    }
    return result;
}
</code></pre>
<p>As can be seen in Figure 1, the algorithm first enumerates the inner vertices, dividing them into 3 parts. After the inner vertices, the innermost vertex (in case of an even innerTessLevel) is enumerated. At the end, the outer vertices are enumerated.</p>
<p><a href="https://i.sstatic.net/OvC91.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OvC91.png" alt="Insert image description here" /></a></p>
<p><strong>Figure 1:</strong> Enumeration sceme for: <code>gl_TessLevelInner[0] = 10</code>, <code>gl_TessLevelOuter[0] = 10</code>, <code>gl_TessLevelOuter[1] = 10</code>, <code>gl_TessLevelOuter[2] = 10</code>.</p>
<p>This algorithm works for all types of triangles tessellation, where innerTessLevel and outerTessLevels are integer values. The enumeration only works for <code>equal_spacing</code>. As you can see from the code, the enumerated value is calculated by the following built-in variables of TES:</p>
<ul>
<li><code>in vec3 gl_TessCoord</code></li>
<li><code>patch in float gl_TessLevelOuter[3]</code></li>
<li><code>patch in float gl_TessLevelInner[1]</code></li>
</ul>
<p>I will try to improve the algorithm from time to time.... at the moment it is very confusing and full of branches... maybe we can avoid branching in the end.</p>
<p><strong>Also important, when using this algorithm:</strong>
Multiple generated vertices within TES stage are on the same position. So the output of this algorithm gives you an enumeration with respect to the position. But not with respect to the vertex id. So multiple generated vertices will receive the same enumarated number at the end.</p>
<p>Please feel free to give me tips on how to improve the algorithm =)</p>

CC BY-SA 4.0
2023-09-23T11:24:46.020Z
That is a really interesting question and answer! Together with the other primitive types (which might be easier to enumerate due to being more regular) this could let you generate an arbitrary specific number of points from tesselation (which together with `point_mode` can be an interesting tool for generating arbitrary numbers of points).
Christian Rau
CC BY-SA 4.0
2023-09-23T16:51:35.037Z
@ChristianRau thanks, but I think there are better ways to enumerate the vertices. I hoped that somebody else did this before and share his successes to reduce Branching.
Thomas
CC BY-SA 4.0
2023-08-25T12:30:11.607Z
2023-08-31T11:06:12.963Z
2023-08-31T11:06:12.963Z
Thomas
<p>Is there any way to enumerate the generated vertices of a tessellation patch (triangles only) inside the tessellation evaluation shader? The tessellation levels will only be integer values with equal_spacing.</p>
<p>I am thinking of something like <code>gl_VertexID</code>, but only within that one patch. Something that is unique and exactly the same for each rendering. And the enumeration should not contain unused numbers.</p>
<p>The built-in variables of TES that seem to be useful for creating such a function are:</p>
<ul>
<li>in vec3 gl_TessCoord</li>
<li>patch in float gl_TessLevelOuter[3]</li>
<li>patch in float gl_TessLevelInner[1]</li>
</ul>
<p>So we know the barycentric coordinates and the tess levels.
How can we create such an enumeration function?
Or does something like this already exist?</p>

<tesselation>
enumerate vertices from tessellation evaluation stage (Triangle patch)
Thomas
# enumerate vertices from tessellation evaluation stage (Triangle patch)

(Asked by: username_0 on 2023-08-23)

Is there any way to enumerate the generated vertices of a tessellation patch (triangles only) inside the tessellation evaluation shader? The tessellation levels will only be integer values with equal\_spacing.

I am thinking of something like `gl_VertexID`, but only within that one patch. Something that is unique and exactly the same for each rendering. And the enumeration should not contain unused numbers.

The built-in variables of TES that seem to be useful for creating such a function are:

* in vec3 gl\_TessCoord
* patch in float gl\_TessLevelOuter[3]
* patch in float gl\_TessLevelInner[1]

So we know the barycentric coordinates and the tess levels.
How can we create such an enumeration function?
Or does something like this already exist?

**Tags:** tesselation


---

## 1 answers

### Answer by username_0 on 2023-08-25. Score: 2 | Accepted answer

After a whole day of brain stress, I found a method to enumerate them:

```
//floats equal? with respect of epsilon
bool isNearlyTheSame(float value0, float value1) 
{
    return abs(value0 - value1) < 0.001;
}

uint getMinIndex(vec3 vector)
{
    uint y = uint(vector.y <= vector.x + 0.001 && vector.y < vector.z);
    uint z = uint(vector.z < vector.x && vector.z <= vector.y + 0.001)*2;
    return y + z;
}

//result: x: 0 => inner triangles, 1 => inner most vertex, 2 => outer vertices
//result: y: lowest barycentric value is: 0 => x, 1 => y, 2 => z  
uvec2 getBarycentricCase()
{
    uvec2 baryCase = uvec2(0,0);
    baryCase.y = getMinIndex(gl_TessCoord);
    if(gl_TessCoord.x == 0 || gl_TessCoord.y == 0 || gl_TessCoord.z == 0)
        baryCase.x = 2;
    if(isNearlyTheSame(gl_TessCoord.x, gl_TessCoord.y) && isNearlyTheSame(gl_TessCoord.y, gl_TessCoord.z))
        baryCase.x = 1;
    return baryCase;
}

//returns a third of the count of the inner vertices with respect to the tessLevel
uint getNummTessPart(uint tessLevel)
{
    float oddAdd = mod(tessLevel, 2) == 0 ? 0 : 1;
    return tessLevel / 2.0 * (tessLevel / 2.0 - 1) + oddAdd;
}

//returns the current patch vertex id with respect to this algorithm
uint getPatchVertexID()
{
    uint result;
    uvec2 baryCase = getBarycentricCase();
    uint outerTesslevel0 = uint(gl_TessLevelOuter[0]);
    uint outerTesslevel1 = uint(gl_TessLevelOuter[1]);
    uint outerTesslevel2 = uint(gl_TessLevelOuter[2]);
    uint innerTesslevel = uint(gl_TessLevelInner[0]);

    uint numTessPartInner = uint(getNummTessPart(innerTesslevel));
    float baseValue = min(min(gl_TessCoord.x,gl_TessCoord.y), gl_TessCoord.z);
    float leftValue = baryCase.y == 0 ? gl_TessCoord.z : baryCase.y == 1 ? gl_TessCoord.x : baryCase.y == 2 ? gl_TessCoord.y : 0;

    if(baryCase.x == 0) //inner vertices
    {
        float stepUp = 2.0 / 3.0 / innerTesslevel;
        float stepLeft = 1.0 / innerTesslevel;
        uint positionUp = uint(round(baseValue / stepUp));
        uint positionLeft = uint(round(leftValue / stepLeft + (positionUp) * stepUp * innerTesslevel / 2 - positionUp));
        result = numTessPartInner - uint(getNummTessPart(innerTesslevel - ((positionUp-1) * 2)));
        result += positionLeft;
        result += baryCase.y * numTessPartInner;
    }
    else if(baryCase.x == 1) //center vertex
    {
        result = numTessPartInner * 3;
    }
    else if(baryCase.x == 2) //outer
    {
        uint maxOuterTessLevel = max(max(outerTesslevel0, outerTesslevel1), outerTesslevel2);
        bool hasCenter = (mod(innerTesslevel,2) == 0 && innerTesslevel > 1) || (maxOuterTessLevel > 1 && innerTesslevel < 1);
        bool hasInner = innerTesslevel > 2;
        uint outerAdd[3] = {0, outerTesslevel0, outerTesslevel0 + outerTesslevel1};
        result = uint(hasInner) * numTessPartInner * 3;
        result += uint(hasCenter);    
        result += uint(round(leftValue * gl_TessLevelOuter[baryCase.y])) + outerAdd[baryCase.y];
    }
    return result;
}

```

As can be seen in Figure 1, the algorithm first enumerates the inner vertices, dividing them into 3 parts. After the inner vertices, the innermost vertex (in case of an even innerTessLevel) is enumerated. At the end, the outer vertices are enumerated.

[![Insert image description here](https://i.sstatic.net/OvC91.png)](https://i.sstatic.net/OvC91.png)

**Figure 1:** Enumeration sceme for: `gl_TessLevelInner[0] = 10`, `gl_TessLevelOuter[0] = 10`, `gl_TessLevelOuter[1] = 10`, `gl_TessLevelOuter[2] = 10`.

This algorithm works for all types of triangles tessellation, where innerTessLevel and outerTessLevels are integer values. The enumeration only works for `equal_spacing`. As you can see from the code, the enumerated value is calculated by the following built-in variables of TES:

* `in vec3 gl_TessCoord`
* `patch in float gl_TessLevelOuter[3]`
* `patch in float gl_TessLevelInner[1]`

I will try to improve the algorithm from time to time.... at the moment it is very confusing and full of branches... maybe we can avoid branching in the end.

**Also important, when using this algorithm:**
Multiple generated vertices within TES stage are on the same position. So the output of this algorithm gives you an enumeration with respect to the position. But not with respect to the vertex id. So multiple generated vertices will receive the same enumarated number at the end.

Please feel free to give me tips on how to improve the algorithm =)

> **Comments:**
> **username_1**: That is a really interesting question and answer! Together with the other primitive types (which might be easier to enumerate due to being more regular) this could let you generate an arbitrary specific number of points from tesselation (which together with `point_mode` can be an interesting tool for generating arbitrary numbers of points).  
> **username_0**: @username_1 thanks, but I think there are better ways to enumerate the vertices. I hoped that somebody else did this before and share his successes to reduce Branching.  

---

