Optix, retrieve triangle ID during raycasting for scientific analysis

I am working on a project where I need to calculate what percentage of the image each triangle is occupying.
I couldn’t find anything similar on the SDK examples or on the documentation pages.

Some of the requirements are that I don’t want to change the mesh topology by specify per vertex colours and that I will be importing the geometry using objs.

My question is: Is there a way retrieve for each Ray hit the triangle ID? Given that can I also get the mesh ID?

Please have a look into this thread which explains possible options:

With OptiX 7 that wouldn’t need a variable per GeometryInstance to identify a mesh because the instance acceleration structure (IAS) itself can be queried for a user supplied instance ID with optixGetInstanceId() or for the instance index under the bottom-most IAS with optixGetInstanceIndex(), and the primitive index is always available inside the device code in any OptiX version.


For retrieving the triangle ID in your closest hit program when using built-in hardware accelerated triangles, see rtGetPrimitiveIndex() in OptiX 6, or optixGetPrimitiveIndex() in OptiX 7.