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.