05 Land Surface Creation
This Adaptive Tesselation Mesh (ATM) described in Episode 5 generates a icosphere based framework which scales with distance and is flexible enough to generate ATM Patches down to 100 meters per side.
The Leaf Patches that are added and released with each pass provide a list of areas which need Land Surface added to them so that they can be rendered.
All ATM Patches have identical land surface geometry, differing only by height-above-ground and the surface normal. Because each patch is identical we create a single indexed list of triangle vertices in a buffer shared by all ATM Patches.

For the height and normal values, we create a single large per-vertex, per-patch buffer to store this; this is the Geometry Buffer.
Data Structures
The Geometry buffer has the following structure;
struct PatchVertex
{
// Position on the Geoid Surface. This is relative to the ATM Patch Datum position.
float3 GeoidSurfaceRelativePosition : POSITION0;
// Height above the geoid surface. This is added to the GeoidSurfaceRelativePosition and ATM Patch Datum to get an absolute world location.
float Height : HEIGHT0;
// UV coordinate for texture mapping.
float2 TextureUV : TEXCOORD1;
// Steepness of the slope of this vertex. This value is used to determine various landscape textureing decisions
float Slope : SLOPE0;
// The Normal direction of the landscape at this point. This is relative to the Tangent (flat surface) of the landscape, not the globe based geometry
float3 WorldSpaceNormal : NORMAL0;
/*
Steepness Approx Angle
0.05 18°
0.10 26°
0.20 37°
0.30 46°
0.50 60°
0.70 73°
1.00 90°
*/
// Helper function to calculate the strength of Scarp (cliff face, or terrain broken up becuase of steepness)
static float CalculateScarpStrength(float steepness)
{
return smoothstep(Const_ScarpSteepnessMinValue, Const_ScarpSteepnessMinValue + Const_ScarpSteepnessBlendBand, steepness);
}
};
This is held in a single Structured Buffer.
ATM Patches have a PatchID value which is assigned when they are created, and released when they are destroyed. While it is tempting to use this PatchID value as an index into a block within a single large shared Height/Normal buffer, we want to retain the option for ATM Patches to have no geometry assigned to them, so breaking the simple link between ATM Patch ID and Geometry Block ID.
To avoid linking ATM Patch ID to the geometry block, an Append/Consume buffer of available Geometry Block IDs is created.
Stage 1 - Height Generation
After the ATM Pass we have a buffer of newly created ATM Patches. This feeds into our Height Generation pass.
In a compute shader, parse the New ATM Patch IDs
- If a set of geometry is required, Consume a new Geometry Block ID, locate the start of the ATM Patch's owned geometry block.
- For each Geometry Vertex, calculate its position on the globe surface (using barycentric coordinates to generate an interpolation between ATM Patch P0, P1, P2, and then normalizing and projecting)
// Calculate the row Col pair within the triangle
static int2 CalculateColRow(uint index, uint baseRowCount)
{
// Corrected row computation without iteration
int row = (int) floor((2 * baseRowCount + 1 - sqrt((2 * baseRowCount + 1) * (2 * baseRowCount + 1) - 8 * index)) / 2);
// Compute row start index
int rowStartIndex = (row * (2 * baseRowCount - row + 1)) / 2;
// Compute column
int column = index - rowStartIndex;
return int2(column, row);
}
// Create a posiiton interpolated between P0,P1,P2 assuming a baseline unit length and height
static float3 CalculateTrianglePosition(uint index, float baselinePointCount, float3 P1, float3 P2, float3 P3)
{
int2 colRow = CalculateColRow(index, baselinePointCount);
float3 incrementP1P2 = (P2 - P1) / (baselinePointCount - 1);
float3 incrementP1P3 = (P3 - P1) / (baselinePointCount - 1);
float3 P1OffsetPosition = (incrementP1P2 * colRow.x) + (incrementP1P3 * colRow.y);
return P1 + P1OffsetPosition;
}
...
// call the above using
float3 planarRelativePosition = CalculateTrianglePosition(
vertexOffset, // The index of the vertex in the triangle mesh
PatchGeometryBaselineVertexCount, // 17 typically
patch.P0Position,
patch.P1Position,
patch.P2Position);
// Now project the planar coordinate onto the world sphere
float3 geoidAbsolutePosition = normalize(planarRelativePosition + atmPatch.PatchDatum) * worldRadius;
- Calculate the height-above-ground by passing the geoid absolute position into your favoured FbM calculation.
Note At this point it is optimal for the FbM calculation to return both the height and the derivatives. The derivatives describe the change in height in the three orthogonal axis, and this is the Surface Normal.
- Fill in the rest of the properties of the vertex, and stored in the global buffer.
- Update the ATM Patch with the start index of the Geometry Buffer
Performance Tip It is tempting to generate the geometry in a compute shader in a loop like this
[numthreads(64, 1, 1)]
void cs(uint3 dispatchThreadID : SV_DispatchThreadID)
{
// ---- Bounds check against actual leaf list length ----
if (dispatchThreadID.x >= NewATMLeafCount)
return;
// Do a for..next loop around the hundred or so vertexes, generate height and store.
This is sub-optimal. At most we expect 10-50 ATM Patches to be created each pass, and the above approach will only generate one GPU thread per ATM Patch, wasting huge GPU bandwidth.
As a general approach, avoid all loops in compute shaders where the loop updates a Structured Buffer. It is far more performant to do this
[numthreads(64, 8, 1)]
void cs(uint3 dispatchThreadID : SV_DispatchThreadID)
{
// ---- Bounds check against actual leaf list length ----
if (dispatchThreadID.x >= NewATMLeafCount)
return;
if (dispatchThreadID.y >= FixedVertexCount)
return;
// Calculate and update the one vertex indicated by the dispatchThread.y value
The above fires off a GPU thread per ATM Patch and per Height Vertex using the full width of the GPU dispatch wave.
Stage 3 - Releasing
When the ATM pass generates a list of released ATM Patches we must release the Geometry Buffer index. We dont need to overwrite the actual Geometry Buffer records.
In a compute shader, parse the Released ATM Patch IDs
- Obtain the Geometry Buffer ID from the ATM Patch record.
- Append this ID to the Append/Consume buffer of available Geometry block IDs.