Multiple pipelines and shared instancing

If I understand correctly, each OptixInstance needs the SBT offset which could be different for each of my SBTs.

The question is then, what is the layout of your SBT and how can you change it to not have this problem.

The only problematic case is when the SBTs have different numbers of hit records where the instance sbtOffset selects the shader. But there are many other possible SBT layouts.

There are three ways to solve this:

  1. Pragmatic: Build a different IAS for each SBT to have the sbtOffsets match to your resp. SBT layouts. The GAS are all shared so this isn’t too bad.
  2. If possible make the SBTs all the same size and layout by duplicating the shader headers and potentially other SBT record data to match the sbtOffsets of the biggest SBT.
  3. Change the SBT to have one entry per instance by assigning the shader header and additional record data for each instance with its unique sbtOffset. That allows to switch SBTs or freely exchange shaders and data per instance without rebuilding the IAS. (I’m doing that in my OptiX 7 examples. Links in the sticky posts.)

There would be even more ways, like folding all shaders into one pipeline and setting up the SBT with the necessary strides to use same offsets inside the instance but different raygen programs and each using it’s own “ray type” offset in the optixTrace. Though there can only be one ray generation program inside the SBT and switching the pointer or shader header means a synchronization.
There are also performance considerations to keep in mind:
https://forums.developer.nvidia.com/t/how-to-handle-multiple-ray-generators/83446
https://forums.developer.nvidia.com/t/multiple-raygen-functions-within-same-pipeline-in-optix-7/122305
I would keep different pipelines and SBTs if the shaders are completely different.