Below is my code that loads a mesh into an optix::context for rendering. How can I remove this geom_instance and all its children (materials/triangles) from my context?
I couldn’t find an ctx->destroyGeometryInstance or any method on GeometryInstance to do the trick.
optix::GeometryTriangles geom_tri = ctx->createGeometryTriangles();
geom_tri->setVertices(num_vertices, buffers.positions, buffers.positions->getFormat());
size_t num_matls = optix_materials.size();
geom_tri->setMaterialIndices(buffers.mat_indices, 0, sizeof(unsigned), RT_FORMAT_UNSIGNED_INT);
geom_instance = ctx->createGeometryInstance();
// Set the materials
for (size_t idx = 0; idx < num_matls; ++idx)
geom_instance->setMaterial((unsigned int)idx, optix_materials[idx]);
How can I remove this geom_instance and all its children (materials/triangles) from my context?
The OptiX C++ wrappers in OptiX 1 to 6 versions are not reference-counting the underlying OptiX C objects, but only themselves, which is one of my pet peeves with that old API. They are not deleting the objects in a sub-tree when deleting the root C++ object. That’s the main reason for memory leaks when using those C++ wrappers.
You either need to track all these C++ objects yourself (recommended) or query them from the parent in your OptiX render graph and delete them recursively depth-first, or change the wrappers to do the proper reference counting, or use the C API and do this yourself.
The C++ wrapper classes over the OptiX objects which can be destroyed have an explicit
destroy() member function which calls the resp.
rt<Object>Destroy C API function.
Please find them inside the
OptiX SDK 6.5.0\include\optixu\optixpp_namespace.h header.
Means in your code you would need to call things like these to destroy the underlying OptiX objects:
for (size_t idx = 0; idx < num_matls; ++idx) optix_materials[idx]->destroy();
// Similar for buffers[index]->destroy();
That’s explained in Chapter 10 of the OptiX 6.5.0 Programing Guide
I wouldn’t recommend adding multiple materials to a GeometryInstance if you can avoid that. (I’m showing how to do that in my OptiX Introduction samples. See links in the sticky posts.)
Finally, if you’re starting new with OptiX, please consider using an OptiX 7 version instead, which was changed to a more modern API.
Please read the forum entries on OptiX 7 for more information about that.
Your answer is very helpful!