Any Hit Shader

A shader that is invoked when ray intersections are not opaque.

Any hit shaders must declare a payload parameter followed by an attributes parameter. Each of these parameters must be a user-defined structure type matching types used for TraceRay and ReportHit respectively, or the Intersection Attributes Structure when fixed function triangle intersection is used.

Any hit shaders may perform the following kinds of operations:

  • Read and modify the ray payload: (inout payload_t rayPayload)
  • Read the intersection attributes: (in attr_t attributes)
  • Call AcceptHitAndEndSearch, which accepts the current hit, ends the any hit shader, ends the intersection shader if one is present, and executes the closest hit shader on the closest hit so far if it is active.
  • Call IgnoreHit, which ends the any hit shader and tells the system to continue searching for hits, including returning control to an intersection shader, if it is currently executing, returning false from the ReportHit* call site.
  • Return without calling either of these intrinsics, which accepts the current hit and tells the system to continue searching for hits, including returning control to the intersection shader if there is one, returning true at the ReportHit call site to indicate that the hit was accepted.

Even if an any hit shader invocation is ended by IgnoreHit or AcceptHitAndEndSearch, any modifications made to the ray payload so far must still be retained.

Shader Type attribute

[shader("anyhit")]

Example

[shader("anyhit")]
void anyhit_main( inout MyPayload payload, in MyAttributes attr )
{
    float3 hitLocation = ObjectRayOrigin() + ObjectRayDirection() * RayTCurrent();
    float alpha = computeAlpha(hitLocation, attr, ...);

    // Processing shadow and only care if a hit is registered?
    if (TerminateShadowRay(alpha))
        AcceptHitAndEndSearch(); // aborts function

    // Save alpha contribution and ignoring hit?
    if (SaveAndIgnore(payload, RayTCurrent(), alpha, attr, ...))
        IgnoreHit(); // aborts function

    // do something else
    // return to accept and update closest hit
}