Graphics Reference
In-Depth Information
float3 faceNormal
= normalize
(
cross
(
ip[2].position - ip[0].position
, ip[l].position - ip[0].position
)
);
float3 viewDirection = normalize(cameraLookAt - cameraPosition);
float backFace = sign(0.2 + dot(faceNormal,viewDirection));
output.Edges[0] = EdgeFactors.x * backFace;
output.Edges[l] = EdgeFactors.y * backFace;
output.Edges[2] = EdgeFactors.z * backFace;
output.Inside = EdgeFactors.w * backFace;
return output;
}
Listing 9.22. Modified hull shader constant function.
Hull shader constant functions that output zero or negative edge tessellation factors
will be culled by the pipeline. In Listing 9.22, this is determined by a simple check on the
face normal for the incoming triangle. If the inner product of this normal and the current
view direction is positive (both point in the same direction), the patch is considered back
facing. There are two details to pay attention to with this code. First, the sign function
which will return either -1.0 or +1.0 and can thus be used to multiply with the edge factors
and avoid unnecessary conditionals/branching. Second, the test for being a back face has a
"magic" 0.2 factor included, which acts as a simple threshold to stop premature culling of
the patch. Due to the curvature of patches, having them culled on the precise turning point
can lead to noticeable artifacts in the final image. This factor can be tweaked as necessary,
closer to 0.0 will remove more patches, but with increased chance of visual artifacts.
It is important to note that the control point phase of hull shader execution occurs
before evaluation of the per-patch constant function. Consequently, the hull shader may
perform significant work before it is culled, and further downstream processing can be
avoided. Figure 9.47 shows the results of this optimization (right side).
Note that by definition, the difference is hard to detect, but the wireframe rendering
appears to be less dense, indicative of the fewer patches being rendered. In this example, the
number of domain shader invocations drops from 19,488 to 9,570, and the number of trian-
gles rasterized falls from 16,800 to 8,250—a 51% reduction of both of the unoptimized totals.
Search WWH ::




Custom Search