optixTrace: calculate intersections and max distance

I would like to ask some very simple questions that will significantly increase my understanding on some basic concepts of optix ray trace.
Suppose the scenario that I want to send a ray from a 3D point to a GAS and I want to know how many intersections occurred and what’s the distance of the longest intersection.

  1. Which program groups do I actually need for this?
  2. If I choose to use the closest hit group and a ray intersects with the GAS 3 times, how many times is the __closesthit__ch() called? Once and then it stops or 3 times?
  3. Which OPTIX_RAY_FLAG describes best this scenario?
    Thanks!

There are multiple ways to calculate all intersections along a ray.

One way is to use an anyhit shader. If do you that, your anyhit shader will be called at least once for every intersection, until a closest hit is found. Because geometry might be split for performance reasons, it’s possible to get more than one anyhit call corresponding to an intersection. If that could be a problem, you can disable geometry splitting and guarantee a single anyhit call using OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL. To ensure the ray doesn’t stop at the closest hit, and continues to report all the hits, you need to call optixIgnoreIntersection() in your anyhit shader; this way the ray will report all the hits along the ray (and will not call closest hit unless you allow it.)

Another way to calculate all intersections along a ray is to pass a t_min value equal to the previous intersection’s t_hit value, while calling optixTrace() or optixTraverse() in a loop and passing the same ray every time. By advancing t_min to the previous hit point, you skip over that intersection while allowing subsequent intersections. This method can be useful but is even slower than using anyhit.

You can also use a hybrid of both of these methods, and collect a small number of anyhits before casting a new ray. All of these methods can be relatively expensive, but if you need all hits along a ray, then it’s the price to pay. Sometimes there are alternatives to collecting all hit points along a ray, so we recommend considering the problem’s requirements carefully.

2- If you use a closest hit shader program, it will be called at most once per ray. It won’t be called if the ray misses, or if you’ve used an anyhit shader and called optixIgnoreIntersection() for all hits.

3- The ray flags are mostly orthogonal to this discussion; you don’t need any ray flags in order to find all hits along a ray. You can enable or disable anyhit calls using ray flags, and you can disable closest hit calls with a ray flag, and you can also terminate your ray after the first hit is found, which will call closest hit with whatever hit was found (and it might not be the closest one). The terminate-on-first-hit flag’s intent is for shadow rays, for example, where you only need to know if the light source sample is blocked or not, and you don’t care what the t value of the hit point on the occluder is.

Hope that helps. It is worth revisiting the Programming Guide and SDK samples as the concepts fall into place. I know there’s a learning curve and that it’s a lot to absorb, but it is helpful to review things that didn’t make sense the first time. I still review the Programming Guide all the time.


David.