# Matching Tessellation Factors Along Edges

This topic is 2107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey everyone,

For the past few weeks or so I've been working on setting up tessellation with displacement mapping in my deferred renderer.  Everything works, the displacement is correct, etc, etc but the one thing that's been giving me terrible difficulty is calculating my tessellation factors for my triangles based on the distance from the camera.

In particular I'm seeing a lot of gaps between triangle edges whose tessellation factors are different.  So I know that at its most basic level, the problem is that adjacent triangles that "share" an edge (share as in the triangles don't share vertices but just have edges at the same position) aren't being calculated to have the same tessellation factors.  The part that has me stumped is that by my understanding I should be handling this case correctly.

The two adjacent triangles are actually two separate sets of 3 vertices.  I know its not efficient and could be better used if I had just 4 vertices and 6 indices, but the 4 vertices that make up a set of 2 adjacent edges should be identical in terms of position, so I don't think that could be the source of the problem.  But in the interest of completion here's the code that creates the mesh:

    int cellCount = 25;
m_totalSize = 320.f;
float cellSize = m_totalSize / cellCount;

std::vector<Vertex> finalVertices(6 * cellCount * cellCount);
float deltaX = cellSize, deltaZ = cellSize;
float deltaU = 1.f / cellCount, deltaV = 1.f / cellCount;

float minX = -m_totalSize * 0.5f, maxX = minX + deltaX;
float minU = 0.f, maxU = deltaU;
float minZ = -m_totalSize * 0.5f, maxZ = minZ + deltaZ;
float minV = 0.f, maxV = deltaV;

int startingIndex = 0;
for(int row = 0; row < cellCount; ++row)
{
for(int col = 0; col < cellCount; ++col)
{
finalVertices[startingIndex++].SetParameters( maxX, 0.f, maxZ, maxU, maxV);
finalVertices[startingIndex++].SetParameters( minX, 0.f, minZ, minU, minV);
finalVertices[startingIndex++].SetParameters( minX, 0.f, maxZ, minU, maxV);

finalVertices[startingIndex++].SetParameters( minX, 0.f, minZ, minU, minV);
finalVertices[startingIndex++].SetParameters( maxX, 0.f, maxZ, maxU, maxV);
finalVertices[startingIndex++].SetParameters( maxX, 0.f, minZ, maxU, minV);

minX += deltaX;
maxX += deltaX;
minU += deltaU;
maxU += deltaU;
}

minZ += deltaZ;
maxZ += deltaZ;
minV += deltaV;
maxV += deltaV;

minX = -m_totalSize * 0.5f;
maxX = minX + deltaX;
minU = 0.f;
maxU = deltaU;
}


My Hull constant function takes in a patch of 3 points for each triangle and calculates the midpoint of each edge from those three points.  It then calculates the distance from the position of the camera and each midpoint and uses that distance to lerp between a minimum and a maximum tessellation factor (each of which has a corresponding range associated with it).  Since this is where the actual tessellation factors are calculated I'm guessing there's a good chance that its the culprit.  Here is the code from that portion of my shader:

float maxDistance = 150.0f;
float minDistance = 0.f;

HS_CONSTANT_FUNC_OUT HSConstFunc(InputPatch<VS_OUTPUT, 3> patch, uint PatchID : SV_PrimitiveID)
{
HS_CONSTANT_FUNC_OUT output = (HS_CONSTANT_FUNC_OUT)0;

float distanceRange = maxDistance - minDistance;
float minLOD = 1.0f;
float maxLOD = 32.0f;

float3 midpoint01 = patch[0].Position + 0.5 * (patch[1].Position - patch[0].Position);
float3 midpoint12 = patch[1].Position + 0.5 * (patch[2].Position - patch[1].Position);
float3 midpoint20 = patch[2].Position + 0.5 * (patch[0].Position - patch[2].Position);
float3 centerpoint = (patch[0].Position + patch[1].Position + patch[2].Position) / 3;

// calculate the distance from camera position to each edge midpoint and the center of the triangle
float e0Distance = distance(cameraPosition, midpoint01) - minDistance;
float e1Distance = distance(cameraPosition, midpoint12) - minDistance;
float e2Distance = distance(cameraPosition, midpoint20) - minDistance;
float eIDistance = distance(cameraPosition, centerpoint) - minDistance;

float tf0 = lerp(minLOD, maxLOD, (1.0f - (saturate(e0Distance / distanceRange))));
float tf1 = lerp(minLOD, maxLOD, (1.0f - (saturate(e1Distance / distanceRange))));
float tf2 = lerp(minLOD, maxLOD, (1.0f - (saturate(e1Distance / distanceRange))));
float tfInterior = lerp(minLOD, maxLOD, (1.0f - (saturate(eIDistance / distanceRange))));

output.edgeTesselation[0] = tf0;
output.edgeTesselation[1] = tf1;
output.edgeTesselation[2] = tf2;
output.insideTesselation  = tfInterior;

return output;
}


Assuming that the math is correct, that makes me think that the other problem area could be my hull shader's partitioning method.  Currently I'm using the integer method as it seems the simplest and easiest to debug, though I know that it will eventually lead to visual popping.

[domain("tri")]
[partitioning("integer")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(3)]
[patchconstantfunc("HSConstFunc")]
HS_OUTPUT HSMain(InputPatch<VS_OUTPUT, 3> patch, uint i : SV_OutputControlPointID)
{
HS_OUTPUT output = (HS_OUTPUT)0;

output.Position = patch[i].Position;
output.UVCoords = patch[i].UVCoords;
output.tc1 = patch[i].tc1;
output.tc2 = patch[i].tc2;
output.normal = patch[i].normal;

return output;
}


Could it be that the integer partitioning method is the cause for the gaps?  I know that if you specify a tessellation factor of 3.1 for an integer partitioning method, it gets bumped down to a value of 3.***  But even in this case it confuses me that any two edges between two sets of identical points would return differing tessellation factors.

Thanks to anyone who takes a look.  Let me know if I can provide any other code or explain anything.

*** I have a very basic and disgusting understanding of the various tessellation partitioning types.  If some kind, knowledgeable stranger happens to understand these and wants to throw a bit of an explanation for the advantages/disadvantages of pow and fractional_even/odd then that would be amazing.

Edited by lukabratzi

##### Share on other sites

I think you're doing it in a more complicated way than you need to.  I worked from Luna's DX11 book, and the code he presents does the LOD calculations in the vertex shader, and caches it in a field of his vs out/hs in vertex structure, so you end up with a per-vertex tessellation factor.  Thus, in the hull shader, you only need to average the two vertex tessellation factors, rather than doing the full LOD calculation, which should save you some performance.

http://www.richardssoftware.net/2013/09/bump-and-displacement-mapping-with.html

Also, as I was going back over my code, I noticed this comment in the hlsl:

    PatchTess pt;

// Average tess factors along edges, and pick an edge tess factor for
// the interior tessellation.  It is important to do the tess factor
// calculation based on the edge properties so that edges shared by
// more than one triangle will have the same tessellation factor.
// Otherwise, gaps can appear.
pt.EdgeTess[0] = 0.5f*(patch[1].TessFactor + patch[2].TessFactor);
pt.EdgeTess[1] = 0.5f*(patch[2].TessFactor + patch[0].TessFactor);
pt.EdgeTess[2] = 0.5f*(patch[0].TessFactor + patch[1].TessFactor);
pt.InsideTess  = pt.EdgeTess[0];

return pt;


So I think that calculating the interior tessellation factor directly might be fouling you up as well.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 18
• 15
• 11
• 9