The any_hit programs are not hit in a specific order. (Depends on the acceleration structure traversal.)

Not sure what you mean with “first pass” and “second pass”. When entering the any_hit program?

If you do not know how many intersections can be along a ray, you cannot reliably ignore an intersection! (If there were only one intersection, your algorithm would never reach the closest hit program.)

If you’re only interested in the number of hits along a ray and their distances, you could store the number of intersections and put their distances into an array at the per ray data payload, don’t provide a closest hit program for that “probe” ray type and handle the resulting calculations inside the program which spawned that ray.

That would happen inside the ray generation program or a closest hit program, where ever you called rtTrace() with that probe ray type to determine the number and distances of intersections.

The distances would need to be sorted there.

If all hits along the ray should be handled the same (no multiple materials) and there isn’t any surface interaction with the rays, that would be the fastest method.

The simpler but much more expensive approach would be to not have an any_hit program at all, the closest hit program will then be called for the closest of all hits and you could start a new ray from that point in the same direction for the next intersection until you reached the t_max distance or the miss program.

When using the rtCurrentRay data, be aware you understand in which coordinate systems the ray is handled inside the different program domains: See OptiX Programming Guide: 4.1.6. Program Variable Transformation

For possible other approaches you might want to describe the original problem you want to solve.