I have been trying unsuccessfully for several weeks to get instance acceleration structures working in my application. For reference, I started with the Optix 7 SDK optixTriangle example code and added the following code right after the single triangle geometry acceleration structure was created:
//---------------------------------------------------------------------------
// Added code - create an IAS instance of the above GAS
//---------------------------------------------------------------------------
OptixTraversableHandle ias_handle;
OptixInstance optixInstance = {};
optixInstance.flags = OPTIX_INSTANCE_FLAG_NONE;
float identityXform[12]
{
1.f, 0.f, 0.f,
0.f, 1.f, 0.f,
0.f, 0.f, 1.f,
0.f, 0.f, 0.f
};
memcpy(optixInstance.transform, identityXform, sizeof(float) * 12);
optixInstance.visibilityMask = 255;
optixInstance.sbtOffset = 0;
optixInstance.instanceId = 0;
optixInstance.traversableHandle = gas_handle; // Triangle geometry created above
// Copy OptixInstance to the GPU
CUdeviceptr d_optixInstance;
const size_t optixInstancesSize = sizeof(OptixInstance);
CUDA_CHECK(cudaMalloc(reinterpret_cast<void**>(&d_optixInstance), optixInstancesSize));
CUDA_CHECK(cudaMemcpy(
reinterpret_cast<void*>(d_optixInstance),
&optixInstance,
optixInstancesSize,
cudaMemcpyHostToDevice
));
OptixBuildInput build_input = {};
build_input.type = OPTIX_BUILD_INPUT_TYPE_INSTANCES;
build_input.instanceArray.instances = d_optixInstance;
build_input.instanceArray.numInstances = 1;
OptixAccelBuildOptions accel_options = {};
accel_options.buildFlags = OPTIX_BUILD_FLAG_ALLOW_COMPACTION;
accel_options.operation = OPTIX_BUILD_OPERATION_BUILD;
OptixAccelBufferSizes ias_buffer_sizes;
OPTIX_CHECK(optixAccelComputeMemoryUsage(context, &accel_options, &build_input,
1, // Number of build inputs
&ias_buffer_sizes));
CUdeviceptr d_temp_buffer_ias;
CUDA_CHECK(cudaMalloc(reinterpret_cast<void**>(&d_temp_buffer_ias), ias_buffer_sizes.tempSizeInBytes));
// non-compacted output
CUdeviceptr d_buffer_temp_output_ias_and_compacted_size;
size_t compactedSizeOffset = roundUp<size_t>(ias_buffer_sizes.outputSizeInBytes, 8ull);
CUDA_CHECK(cudaMalloc(reinterpret_cast<void**>(
&d_buffer_temp_output_ias_and_compacted_size),
compactedSizeOffset + 8
));
OptixAccelEmitDesc emitProperty = {};
emitProperty.type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE;
emitProperty.result = (CUdeviceptr)((char*)d_buffer_temp_output_ias_and_compacted_size + compactedSizeOffset);
OPTIX_CHECK(optixAccelBuild(
context,
0, // CUDA stream
&accel_options,
&build_input,
1, // num build inputs
d_temp_buffer_ias,
ias_buffer_sizes.tempSizeInBytes,
d_buffer_temp_output_ias_and_compacted_size,
ias_buffer_sizes.outputSizeInBytes,
&ias_handle, // Optix handle to instance hierarchy structure on GPU
&emitProperty, // emitted property list
1 // num emitted properties
));
//---------------------------------------------------------------------------
// End added code
//---------------------------------------------------------------------------
then I modified the optix launch to use the IAS in place of the GAS:
//-------------------------------------------------------------------
// Ray trace using identity transformed instance of triangle geometry
params.handle = ias_handle;
// params.handle = gas_handle;
//-------------------------------------------------------------------
I get the same result I have been getting in my own app–ray tracing is not getting to the triangle geometry. Any help would be greatly appreciated.