Yes there are ways, you don’t need to flatten your meshes.
You can use the
primitiveIndexOffset field of each build input, which will be similar to what you said - you will still need the boundary index of each mesh, and you can do a runtime comparison – or – if you have room in your indexing scheme and you know the maximum mesh size, you can encode the build input id into the high bits and your primitive id into the low bits. The resulting encoded primitive id has to fit in 32 bits. You recover the encoded primitive id using
There are alternatives, for what it’s worth. You could use instances, if it’s acceptable to put each mesh into a separate GAS. If your meshes don’t overlap a lot spatially, then this should be okay. Using instances, you can then use device functions like
optixGetInstanceIndex(). If your meshes have a lot of overlap, then it’s probably better to keep them in a single GAS with multiple build inputs.
You could also use unique SBT offsets for each mesh if you want different shader behavior for each build input. The shader you call can be the same code, while still giving you the ability to pass different data to the shaders for each mesh.