[OptiX] Is it possible to get the all primitive IDs contained by an hit AABB of a ray?

As the title, now I shoot a ray to many primitives, and the ray is very possible to hit multiple primitives. I want to get all hit primitives’ ID, now I’m using setPayLoad in every ANY_HIT shader. However, with the increases of amount of hit primitives, the performance degrade quickly. Now I want to do: i) just get the first primitive ID with only one ANY_HIT shader, and ii) get the AABB containing this primitive, finally iii) approximate the true primitive hits by returning all primitives contained in this AABB.
So, I wonder is there OptiX APIs that i) return the AABB ID a ray hit, ii) return all primitive IDs contained in an AABB.

Hi @794906124,

If you are using custom primitives (meaning you’ve written your own intersector) then in your shading code, the device function optixGetPrimitiveIndex() returns the ID of the AABB that you passed in to optixAccelBuild(). If you wanted to have multiple primitives inside this AABB, then it would be up to you to decide which primitives and how many, meaning that finding the list of primitives would also be entirely up to you to design.

If you are using the built-in hardware-accelerated triangles, or OptiX curve primitives, then there is no API for querying the AABB bounds of your primitives.

The BVH in general might not have what you’re looking for. The leaf AABB might always have only 1 primitive, so maybe you’re thinking about using the next one or two levels up the tree? But the next level up may have no guarantees whatsoever about how much space there is between primitives in the box, so depending on it to approximate your neighbors when hit might yield very disappointing results.

Some other approaches you might want to consider, depending on what you need:

  • Use closest-hit instead of any-hit, and re-shoot the ray after every collision (you could decide to advance the ray before re-shooting it, to skip over immediate neighbors).
  • If you’re okay with an approximation, use any-hit but sample your geometry stochastically, i.e., you could decide whether to process each collision randomly in proportion to how much the surface should transmit.
  • Extending that last one a bit further, use one of the ‘null-scattering’ volume rendering algorithms that samples ray distances from an exponential distribution (this way you can skip over collisions to speed things up, but with many rays still converge at the right answer).


Okay thanks!That helps me alot.

Hello David,
I have a similar question. I know that if I use the built-in triangles, the built-in intersection method will be called. Is there a way to get all hit primitives’ID without using anyhit method?
Thanks in advance.

That would require using multiple optixTrace calls following the same ray direction with different intervals or origins because reaching the closest hit or miss program is the end of the ray traversal.
That would be a very simple iterative algorithm with the benefit of getting all intersections in sorted order along the ray direction.

Please have a look into all forum threads linked in these posts which discussed algorithms and caveats of this before:

1 Like