primitive id duplication for multiple geometries.

Hello developers,
I am trying to add multiple geometry models in my simulation for heater transfer. The problem is I am doing my calculations for each triangular primitives which are differentiated by their primitive id’s.
The problem is when I add multiple geometries with a same material and properties , primitive id’s are duplicated so I cannot perform my calculations for separate primitives. Can you suggest any method to differentiate the primitive id’s of two geometries having same intersection program and materials.

Thank you

1.) Simplest solution: If these are separate GeometryInstances you can assign a unique identifier to each GeometryInstance as a variable and use both the primitive ID and that GeometryInstance ID together to uniquely identify a triangle inside the closest hit and any hit programs.

The OptiX Programming Guide chapter 4.1.5 Program Variable Scoping explains in which order variable scopes are evaluated in case they appear at multiple levels. As you can see in Table 6 there, closest hit and any hit program are able to read from variables attached to a GeometryInstance.

2.) A little more involved: If the Geometries are actually different, you can change the triangle index from uint3 to uint4 and use the .w component in that index as unique identifier per triangle, which you would need to fill with a unique value when building the triangle index buffers.
That would require changes in the bounding box and intersection programs to use the new triangle index type. Then that unique per-triangle ID can be generated as attribute in the intersection program instead of the primitive ID and used inside the closest hit and any hit programs.

3.) What’s not going to work easily: If the same GeometryInstance is instanced multiple times with different Transform nodes, then the whole sub-tree would be reused and neither of the above identification mechanism would result in unique identifiers. A solution for that would be more involved.

Thank you so much Detlef for your detailed reply, I was exactly thinking about this method but I didn’t know that we can use geometryInstance in the closest hit program.
Thanks again.

Btw Is there any already implemented example in SDK which used geometryInstance in any hit or closest hit program?

Almost all examples inside OptiX use GeometryInstance nodes. Only those can have Geometry and Material nodes as children.
You do not have access to the GeometryInstance nodes itself inside the device programs, but to variables you attached to them, and that works exactly the same way as with all other OptiX objects which can hold variables.

I don’t think there is an example inside the OptiX SDK which uses variables at GeometryInstance scope.
Simply set your variable at the GeometryInstance object on the host, declare it inside your closest hit and/or any hit program and that’s going to contain the value you set on the host when you hit any of the Geometry underneath the respective GeometryInstance in your scene hierarchy.

Thank you Detlef for you guidance. it worked.