I’m trying to modify the optixTriangle SDK example to render a rectangle by this cod but it does not work. I feel like it’s something I do wrong in building the curve input.
//
// accel handling
//
OptixTraversableHandle gas_handle;
CUdeviceptr d_gas_output_buffer;
{
const int NUM_KEYS = 1, degree = 1;
// Use default options for simplicity. In a real use case we would want to
// enable compaction, etc
OptixAccelBuildOptions accel_options = {};
accel_options.buildFlags = OPTIX_BUILD_FLAG_NONE;
accel_options.operation = OPTIX_BUILD_OPERATION_BUILD;
accel_options.motionOptions.numKeys = NUM_KEYS;
// rectangle build input
std::vector<float3> vertices;
std::vector<float> widths;
vertices.push_back( make_float3( 0.0f, 0.0f, 0.0f ) );
widths.push_back(0.1);
vertices.push_back( make_float3( 0.5f, 0.0f, 0.0f ) );
widths.push_back(0.1);
vertices.push_back( make_float3( 0.5f, 0.5f, 0.0f ) );
widths.push_back(0.1);
vertices.push_back( make_float3( 0.0f, 0.5f, 0.0f ) );
widths.push_back(0.1);
vertices.push_back( make_float3( 0.0f, 0.0f, 0.0f ) );
widths.push_back(0.1);
int nPrimitives = vertices.size() - 1;
const size_t vertices_size = sizeof( float3 ) * vertices.size();
CUdeviceptr d_vertices = 0;
CUDA_CHECK( cudaMalloc( reinterpret_cast<void**>( &d_vertices ), vertices_size ) );
CUDA_CHECK( cudaMemcpy( reinterpret_cast<void*>( d_vertices ), vertices.data(), vertices_size, cudaMemcpyHostToDevice ) );
const size_t widthsSize = sizeof( float ) * widths.size();
CUdeviceptr d_widths = 0;
CUDA_CHECK( cudaMalloc( reinterpret_cast<void**>( &d_widths ), widthsSize ) );
CUDA_CHECK( cudaMemcpy( reinterpret_cast<void*>( d_widths ), widths.data(), widthsSize, cudaMemcpyHostToDevice ) );
CUdeviceptr vertexBufferPointers[NUM_KEYS];
CUdeviceptr widthBufferPointers[NUM_KEYS];
for( int i = 0; i < NUM_KEYS; ++i ) {
vertexBufferPointers[i] = d_vertices + i * (degree + 1) * sizeof(float3);
widthBufferPointers[i] = d_widths + i * (degree + 1) * sizeof(float);
}
// Curve build intput: with a single segment the index array
// contains index of first vertex.
const std::array<int, 4> segmentIndices = {0,1,2,3};
const size_t segmentIndicesSize = sizeof( int ) * segmentIndices.size();
CUdeviceptr d_segementIndices = 0;
CUDA_CHECK( cudaMalloc( reinterpret_cast<void**>( &d_segementIndices ), segmentIndicesSize ) );
CUDA_CHECK( cudaMemcpy( reinterpret_cast<void*>( d_segementIndices ), segmentIndices.data(),
segmentIndicesSize, cudaMemcpyHostToDevice ) );
OptixBuildInput curve_input = {};
curve_input.type = OPTIX_BUILD_INPUT_TYPE_CURVES;
curve_input.curveArray.curveType = OPTIX_PRIMITIVE_TYPE_ROUND_LINEAR;
curve_input.curveArray.numPrimitives = nPrimitives;
curve_input.curveArray.vertexBuffers = vertexBufferPointers;
curve_input.curveArray.numVertices = static_cast<uint32_t>( vertices.size() );
curve_input.curveArray.vertexStrideInBytes = sizeof( float3 );
curve_input.curveArray.widthBuffers = widthBufferPointers;
curve_input.curveArray.widthStrideInBytes = sizeof( float );
curve_input.curveArray.normalBuffers = 0;
curve_input.curveArray.normalStrideInBytes = 0;
curve_input.curveArray.indexBuffer = d_segementIndices;
curve_input.curveArray.indexStrideInBytes = sizeof( int );
curve_input.curveArray.flag = OPTIX_GEOMETRY_FLAG_NONE;
curve_input.curveArray.primitiveIndexOffset = 0;
OptixAccelBufferSizes gas_buffer_sizes;
OPTIX_CHECK( optixAccelComputeMemoryUsage(
context,
&accel_options,
&curve_input,
1, // Number of build inputs
&gas_buffer_sizes
) );
CUdeviceptr d_temp_buffer_gas;
CUDA_CHECK( cudaMalloc(
reinterpret_cast<void**>( &d_temp_buffer_gas ),
gas_buffer_sizes.tempSizeInBytes
) );
CUDA_CHECK( cudaMalloc(
reinterpret_cast<void**>( &d_gas_output_buffer ),
gas_buffer_sizes.outputSizeInBytes
) );
OPTIX_CHECK( optixAccelBuild(
context,
0, // CUDA stream
&accel_options,
&curve_input,
1, // num build inputs
d_temp_buffer_gas,
gas_buffer_sizes.tempSizeInBytes,
d_gas_output_buffer,
gas_buffer_sizes.outputSizeInBytes,
&gas_handle,
nullptr, // emitted property list
0 // num emitted properties
) );
// We can now free the scratch space buffer used during build and the vertex
// inputs, since they are not needed by our trivial shading method
CUDA_CHECK( cudaFree( reinterpret_cast<void*>( d_temp_buffer_gas ) ) );
CUDA_CHECK( cudaFree( reinterpret_cast<void*>( d_vertices ) ) );
}