optixGetPrimitiveIndex() return 0 when using mutiple OptixBuildInput

I created a std::vector<OptixBuildInput> build_inputs(m_num_obj); for optixAccelBuild
When I call optixGetPrimitiveIndex in cuda, the first shape stored in build_inputs return the correct triangleID, the other shape always return 0 for the triangleID in the ray hit function. In addition optix do detect a ray hit for all shapes. Is there any possible idea how can I debug this problem?

That is a little sparse on the information required to say what is going on.
That would require the exact code of the build input parameters and the shader binding table setup to see if there is something is missing.
E.g. OptixBuildInput structure initialization, flags is an array, how many SBT records per build input, matching SBT setup?

https://raytracing-docs.nvidia.com/optix7/guide/index.html#acceleration_structures#primitive-build-inputs

1 Like

Here is my code, if this can be helpful

Please try to initialize all OptixBuildInput elements inside the build_inputs vector with null.

build_inputs[i].triangleArray.numIndexTriplets = static_cast<uint32_t>( m_meshes[i]->m_vertex_buffer.size()/3/3 );
build_inputs[i].triangleArray.indexBuffer      = (CUdeviceptr) m_meshes[i]->m_face_buffer.data();

That setup of numVertices/3 == numTriangles means independent triangles.
Is that matching the data inside the m_face_buffer?
Depending of the m_face_buffer elements type (uint3 or uint) I would have expected
numIndexTriplets = m_meshes[i]->m_face_buffer.size(); // or m_meshes[i]->m_face_buffer.size() / 3; there.

Also you mentioned SBT records, I think I only use one sbt record for miss and hit. Is there any difference when using mutiple shape?

I fix this problem by add hitgroupsbt for each mesh