Skip to content

Commit

Permalink
OptiX resource cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
favreau committed Sep 22, 2023
1 parent a82a552 commit dc3174a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 56 deletions.
12 changes: 9 additions & 3 deletions platform/engines/optix6/OptiXCameraProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <optixu/optixpp_namespace.h>

#include "OptiXUtils.h"

namespace core
{
class OptiXCamera;
Expand All @@ -33,14 +35,18 @@ class OptiXCamera;
class OptiXCameraProgram
{
public:
virtual ~OptiXCameraProgram() = default;
virtual ~OptiXCameraProgram()
{
RT_DESTROY(_rayGenerationProgram);
RT_DESTROY(_missProgram);
RT_DESTROY(_exceptionProgram);
}

::optix::Program getRayGenerationProgram() { return _rayGenerationProgram; }
::optix::Program getMissProgram() { return _missProgram; }
::optix::Program getExceptionProgram() { return _exceptionProgram; }
/**
* @brief commit Virtual method for commiting camera specific variables to
* the context
* @brief commit Virtual method for committing camera specific variables to the context
* @param camera The main core camera
* @param context The OptiX context
*/
Expand Down
46 changes: 24 additions & 22 deletions platform/engines/optix6/OptiXContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ OptiXContext::OptiXContext()

OptiXContext::~OptiXContext()
{
RT_DESTROY_MAP(_bounds);
RT_DESTROY_MAP(_intersects);
_rendererPrograms.clear();
_cameraPrograms.clear();
RT_DESTROY_MAP(_optixBoundsPrograms);
RT_DESTROY_MAP(_optixIntersectionPrograms);
RT_DESTROY_MAP(_optixTextureSamplers);
RT_DESTROY(_optixContext);
}
Expand All @@ -147,26 +149,26 @@ ::optix::Material OptiXContext::createMaterial()

void OptiXContext::addRenderer(const std::string& name, OptiXShaderProgramPtr program)
{
_rendererProgram[name] = program;
_rendererPrograms[name] = program;
}

OptiXShaderProgramPtr OptiXContext::getRenderer(const std::string& name)
{
auto it = _rendererProgram.find(name);
if (it == _rendererProgram.end())
auto it = _rendererPrograms.find(name);
if (it == _rendererPrograms.end())
throw std::runtime_error("Shader program not found for renderer '" + name + "'");
return it->second;
}

void OptiXContext::addCamera(const std::string& name, OptiXCameraProgramPtr program)
{
_cameraProgram[name] = program;
_cameraPrograms[name] = program;
}

OptiXCameraProgramPtr OptiXContext::getCamera(const std::string& name)
{
auto it = _cameraProgram.find(name);
if (it == _cameraProgram.end())
auto it = _cameraPrograms.find(name);
if (it == _cameraPrograms.end())
throw std::runtime_error("Camera program not found for camera '" + name + "'");
return it->second;
}
Expand Down Expand Up @@ -348,34 +350,34 @@ void OptiXContext::_initialize()
_optixContext->setStackSize(OPTIX_STACK_SIZE);
_optixContext->setMaxTraceDepth(OPTIX_MAX_TRACE_DEPTH);

_bounds[OptixGeometryType::cone] =
_optixBoundsPrograms[OptixGeometryType::cone] =
_optixContext->createProgramFromPTXString(CUDA_CONES, OPTIX_CUDA_FUNCTION_BOUNDS);
_intersects[OptixGeometryType::cone] =
_optixIntersectionPrograms[OptixGeometryType::cone] =
_optixContext->createProgramFromPTXString(CUDA_CONES, OPTIX_CUDA_FUNCTION_INTERSECTION);

_bounds[OptixGeometryType::cylinder] =
_optixBoundsPrograms[OptixGeometryType::cylinder] =
_optixContext->createProgramFromPTXString(CUDA_CYLINDERS, OPTIX_CUDA_FUNCTION_BOUNDS);
_intersects[OptixGeometryType::cylinder] =
_optixIntersectionPrograms[OptixGeometryType::cylinder] =
_optixContext->createProgramFromPTXString(CUDA_CYLINDERS, OPTIX_CUDA_FUNCTION_INTERSECTION);

_bounds[OptixGeometryType::sphere] =
_optixBoundsPrograms[OptixGeometryType::sphere] =
_optixContext->createProgramFromPTXString(CUDA_SPHERES, OPTIX_CUDA_FUNCTION_BOUNDS);
_intersects[OptixGeometryType::sphere] =
_optixIntersectionPrograms[OptixGeometryType::sphere] =
_optixContext->createProgramFromPTXString(CUDA_SPHERES, OPTIX_CUDA_FUNCTION_INTERSECTION);

_bounds[OptixGeometryType::triangleMesh] =
_optixBoundsPrograms[OptixGeometryType::triangleMesh] =
_optixContext->createProgramFromPTXString(CUDA_TRIANGLES_MESH, OPTIX_CUDA_FUNCTION_BOUNDS);
_intersects[OptixGeometryType::triangleMesh] =
_optixIntersectionPrograms[OptixGeometryType::triangleMesh] =
_optixContext->createProgramFromPTXString(CUDA_TRIANGLES_MESH, OPTIX_CUDA_FUNCTION_INTERSECTION);

_bounds[OptixGeometryType::volume] =
_optixBoundsPrograms[OptixGeometryType::volume] =
_optixContext->createProgramFromPTXString(CUDA_VOLUMES, OPTIX_CUDA_FUNCTION_BOUNDS);
_intersects[OptixGeometryType::volume] =
_optixIntersectionPrograms[OptixGeometryType::volume] =
_optixContext->createProgramFromPTXString(CUDA_VOLUMES, OPTIX_CUDA_FUNCTION_INTERSECTION);

_bounds[OptixGeometryType::streamline] =
_optixBoundsPrograms[OptixGeometryType::streamline] =
_optixContext->createProgramFromPTXString(CUDA_STREAMLINES, OPTIX_CUDA_FUNCTION_BOUNDS);
_intersects[OptixGeometryType::streamline] =
_optixIntersectionPrograms[OptixGeometryType::streamline] =
_optixContext->createProgramFromPTXString(CUDA_STREAMLINES, OPTIX_CUDA_FUNCTION_INTERSECTION);

// Exceptions
Expand Down Expand Up @@ -466,8 +468,8 @@ void OptiXContext::_printSystemInformation() const
::optix::Geometry OptiXContext::createGeometry(const OptixGeometryType type)
{
::optix::Geometry geometry = _optixContext->createGeometry();
geometry->setBoundingBoxProgram(_bounds[type]);
geometry->setIntersectionProgram(_intersects[type]);
geometry->setBoundingBoxProgram(_optixBoundsPrograms[type]);
geometry->setIntersectionProgram(_optixIntersectionPrograms[type]);
return geometry;
}

Expand Down
17 changes: 13 additions & 4 deletions platform/engines/optix6/OptiXContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "OptiXProperties.h"
#include "OptiXTypes.h"
#include "OptiXUtils.h"

#include <platform/core/common/Properties.h>

Expand Down Expand Up @@ -148,6 +149,14 @@ enum class OptixGeometryType

struct OptixShaderProgram
{
~OptixShaderProgram()
{
RT_DESTROY(any_hit);
RT_DESTROY(closest_hit);
RT_DESTROY(closest_hit_textured);
RT_DESTROY(exception_program);
}

::optix::Program any_hit{nullptr};
::optix::Program closest_hit{nullptr};
::optix::Program closest_hit_textured{nullptr};
Expand Down Expand Up @@ -193,11 +202,11 @@ class OptiXContext

::optix::Context _optixContext{nullptr};

std::map<std::string, OptiXShaderProgramPtr> _rendererProgram;
std::map<std::string, OptiXCameraProgramPtr> _cameraProgram;
std::map<std::string, OptiXShaderProgramPtr> _rendererPrograms;
std::map<std::string, OptiXCameraProgramPtr> _cameraPrograms;

std::map<OptixGeometryType, ::optix::Program> _bounds;
std::map<OptixGeometryType, ::optix::Program> _intersects;
std::map<OptixGeometryType, ::optix::Program> _optixBoundsPrograms;
std::map<OptixGeometryType, ::optix::Program> _optixIntersectionPrograms;

std::unordered_map<void*, ::optix::TextureSampler> _optixTextureSamplers;
std::mutex _mutex;
Expand Down
33 changes: 17 additions & 16 deletions platform/engines/optix6/OptiXEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,20 @@ void OptiXEngine::_createRenderers()
PLUGIN_INFO("Registering '" << RENDERER_PROPERTY_TYPE_ADVANCED << "' renderer");
const std::string CUDA_ADVANCED_SIMULATION_RENDERER = OptiX6Engine_generated_Advanced_cu_ptx;

auto osp = std::make_shared<OptixShaderProgram>();
osp->closest_hit =
auto renderer = std::make_shared<OptixShaderProgram>();
renderer->closest_hit =
context.getOptixContext()->createProgramFromPTXString(CUDA_ADVANCED_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_CLOSEST_HIT_RADIANCE);
osp->closest_hit_textured =
renderer->closest_hit_textured =
context.getOptixContext()->createProgramFromPTXString(CUDA_ADVANCED_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_CLOSEST_HIT_RADIANCE_TEXTURED);
osp->any_hit = context.getOptixContext()->createProgramFromPTXString(CUDA_ADVANCED_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_ANY_HIT_SHADOW);
osp->exception_program =
renderer->any_hit = context.getOptixContext()->createProgramFromPTXString(CUDA_ADVANCED_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_ANY_HIT_SHADOW);
renderer->exception_program =
context.getOptixContext()->createProgramFromPTXString(CUDA_ADVANCED_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_EXCEPTION);
context.getOptixContext()->setExceptionProgram(0, osp->exception_program);
context.addRenderer(RENDERER_PROPERTY_TYPE_ADVANCED, osp);
context.getOptixContext()->setExceptionProgram(0, renderer->exception_program);
context.addRenderer(RENDERER_PROPERTY_TYPE_ADVANCED, renderer);

PropertyMap properties;
properties.setProperty(RENDERER_PROPERTY_ALPHA_CORRECTION);
Expand All @@ -187,19 +187,20 @@ void OptiXEngine::_createRenderers()
PLUGIN_INFO("Registering '" << RENDERER_PROPERTY_TYPE_BASIC << "' renderer");
const std::string CUDA_BASIC_SIMULATION_RENDERER = OptiX6Engine_generated_Basic_cu_ptx;

auto osp = std::make_shared<OptixShaderProgram>();
osp->closest_hit =
auto renderer = std::make_shared<OptixShaderProgram>();
renderer->closest_hit =
context.getOptixContext()->createProgramFromPTXString(CUDA_BASIC_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_CLOSEST_HIT_RADIANCE);
osp->closest_hit_textured =
renderer->closest_hit_textured =
context.getOptixContext()->createProgramFromPTXString(CUDA_BASIC_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_CLOSEST_HIT_RADIANCE_TEXTURED);
osp->any_hit = context.getOptixContext()->createProgramFromPTXString(CUDA_BASIC_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_ANY_HIT_SHADOW);
osp->exception_program = context.getOptixContext()->createProgramFromPTXString(CUDA_BASIC_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_EXCEPTION);
renderer->any_hit = context.getOptixContext()->createProgramFromPTXString(CUDA_BASIC_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_ANY_HIT_SHADOW);
renderer->exception_program =
context.getOptixContext()->createProgramFromPTXString(CUDA_BASIC_SIMULATION_RENDERER,
OPTIX_CUDA_FUNCTION_EXCEPTION);

context.addRenderer(RENDERER_PROPERTY_TYPE_BASIC, osp);
context.addRenderer(RENDERER_PROPERTY_TYPE_BASIC, renderer);

PropertyMap properties;
properties.setProperty(COMMON_PROPERTY_EXPOSURE);
Expand Down
14 changes: 9 additions & 5 deletions platform/engines/optix6/OptiXUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

namespace core
{
#define BRAYNS_OPTIX_SAMPLE_NAME "braynsOptix7Engine"
#define CORE_OPTIX_SAMPLE_NAME "braynsOptix7Engine"

#define RT_DESTROY(__object) \
{ \
Expand All @@ -46,10 +46,14 @@ namespace core
__object = nullptr; \
}

#define RT_DESTROY_MAP(__map) \
for (auto obj : __map) \
{ \
RT_DESTROY(obj.second); \
#define RT_DESTROY_MAP(__map) \
{ \
for (auto obj : __map) \
{ \
RT_DESTROY(obj.second); \
obj.second = nullptr; \
} \
__map.clear(); \
}

static void context_log_cb(unsigned int level, const char* tag, const char* message, void* /*cbdata */)
Expand Down
10 changes: 5 additions & 5 deletions platform/engines/optix7_experimental/OptiXContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ void OptiXContext::_createCameraModules()
size_t sizeof_log = sizeof(log);
size_t inputSize = 0;
const char* input =
sutil::getInputData(BRAYNS_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "PerspectiveCamera.cu", inputSize);
sutil::getInputData(CORE_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "PerspectiveCamera.cu", inputSize);

PLUGIN_CHECK_LOG(optixModuleCreateFromPTX(_state.context, &_state.module_compile_options,
&_state.pipeline_compile_options, input, inputSize, log, &sizeof_log,
Expand Down Expand Up @@ -417,7 +417,7 @@ void OptiXContext::_createShadingModules()
char log[2048];
size_t sizeof_log = sizeof(log);
size_t inputSize = 0;
const char* input = sutil::getInputData(BRAYNS_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "Material.cu", inputSize);
const char* input = sutil::getInputData(CORE_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "Material.cu", inputSize);
PLUGIN_CHECK_LOG(optixModuleCreateFromPTX(_state.context, &_state.module_compile_options,
&_state.pipeline_compile_options, input, inputSize, log, &sizeof_log,
&_state.shading_module));
Expand Down Expand Up @@ -535,20 +535,20 @@ void OptiXContext::_createGeometryModules()
PLUGIN_INFO("Creating OptiX Geometry Modules");
// Spheres
size_t inputSize = 0;
const char* spheres = sutil::getInputData(BRAYNS_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "Spheres.cu", inputSize);
const char* spheres = sutil::getInputData(CORE_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "Spheres.cu", inputSize);
char log[2048];
size_t sizeof_log = sizeof(log);
PLUGIN_CHECK_LOG(optixModuleCreateFromPTX(_state.context, &_state.module_compile_options,
&_state.pipeline_compile_options, spheres, inputSize, log, &sizeof_log,
&_state.sphere_module));
// Cylinders
const char* cylinders = sutil::getInputData(BRAYNS_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "Cylinders.cu", inputSize);
const char* cylinders = sutil::getInputData(CORE_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "Cylinders.cu", inputSize);
PLUGIN_CHECK_LOG(optixModuleCreateFromPTX(_state.context, &_state.module_compile_options,
&_state.pipeline_compile_options, cylinders, inputSize, log, &sizeof_log,
&_state.cylinder_module));

// Cones
const char* cones = sutil::getInputData(BRAYNS_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "Cones.cu", inputSize);
const char* cones = sutil::getInputData(CORE_OPTIX_SAMPLE_NAME, OPTIX_SAMPLE_DIR, "Cones.cu", inputSize);
PLUGIN_CHECK_LOG(optixModuleCreateFromPTX(_state.context, &_state.module_compile_options,
&_state.pipeline_compile_options, cones, inputSize, log, &sizeof_log,
&_state.cone_module));
Expand Down
2 changes: 1 addition & 1 deletion platform/engines/optix7_experimental/OptiXUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <iomanip>
#include <iostream>

#define BRAYNS_OPTIX_SAMPLE_NAME "braynsOptix7Engine"
#define CORE_OPTIX_SAMPLE_NAME "coreOptix7Engine"

static void context_log_cb(unsigned int level, const char* tag, const char* message, void* /*cbdata */)
{
Expand Down

0 comments on commit dc3174a

Please sign in to comment.