Question on any hit and closest hit

I have a hard time understand the any hit and closest hit program. For example in the examples inside the SDK there are declared several anyhit and closehit programs. Does it mean that after the program starts running, after calling rTrace it will execute all the anyhit programs i declared? So for example if I declare a anyhit program that I want to be run when intersecting a sphere and a different anyhit program for intersection with a box, will both anyhit programs be triggered at each intersection?

Hit programs are “shaders” for given material. Closest hit is called for object that is closest to the ray origin. Any hit is typically used for shadow rays when you don’t care about closest hit, just want to know is there is some occluder withing given distance (e.g. on the way to light).

Check the quick start guide and go through tutorial programs. I saw that OptiX wizard generates programs with bunch of comments, might find some clarifications there.

Hi, I understand the use of the program. I don’t really understand if there is a way to call a specific anyhit program or if they are all called at once every time there is a intersection. Let’s say I declare 10 different anyhit programs will they all be called every time rTrace is run?

For example:

Material diffuse = m_context->createMaterial();
  Program diffuse_ch = m_context->createProgramFromPTXFile( ptxpath( "path_tracer", "" ), "diffuse" );
  diffuse->setClosestHitProgram( 0, diffuse_ch );

Material diffuse_light = m_context->createMaterial();
  Program diffuse_em = m_context->createProgramFromPTXFile( ptxpath( "path_tracer", "" ), "diffuseEmitter" );
  diffuse_light->setClosestHitProgram( 0, diffuse_em );

Are both closehit programs going to be called everytime rTrace identifies a closest hit?

You’re asking about any hit and presenting an example with closest hit programs. They’re not the same.
Check the Programming Guide section 3.4.2 Material.

Any hit is called for all potential closest intersections. Hit programs are defined per ray type (number 0 your example). If you have 10 any hit programs for 10 ray types only one of the will be called. If you have 10 any hits for 10 materials and same one type of ray, all of them (potential closest hits) will be called.