Skip to content

Commit

Permalink
Merge pull request #342 from favreau/master
Browse files Browse the repository at this point in the history
Fixed user data management in OptiX engine
  • Loading branch information
favreau authored Dec 11, 2023
2 parents 7719b59 + cb6a752 commit 333f75e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 94 deletions.
12 changes: 11 additions & 1 deletion platform/engines/optix6/OptiXModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ OptiXModel::~OptiXModel()

RT_DESTROY_MAP(_optixTextures)
RT_DESTROY_MAP(_optixTextureSamplers)
RT_DESTROY(_userDataBuffer);
_resetUserDataBuffer();

RT_DESTROY(_geometryGroup);
RT_DESTROY(_boundingBoxGroup);
Expand Down Expand Up @@ -174,6 +174,16 @@ void OptiXModel::commitGeometry()
CORE_DEBUG("Model memory footprint: " << memoryFootPrint / 1024 / 1024 << " MB");
}

void OptiXModel::_resetUserDataBuffer()
{
RT_DESTROY(_userDataBuffer);
// Buffer needs to be bound. Initialize it to size 1 if user data is empty
floats frameData(1, 0);
auto context = OptiXContext::get().getOptixContext();
setBufferRaw(RT_BUFFER_INPUT, RT_FORMAT_FLOAT, _userDataBuffer, context[CONTEXT_USER_DATA], frameData.data(),
frameData.size(), frameData.size() * sizeof(float));
}

uint64_t OptiXModel::_commitSpheres(const size_t materialId)
{
uint64_t memoryFootPrint = 0;
Expand Down
1 change: 1 addition & 0 deletions platform/engines/optix6/OptiXModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class OptiXModel : public Model
void _commitVolumesBuffers(const size_t materialId);

private:
void _resetUserDataBuffer();
uint64_t _commitSpheres(const size_t materialId);
uint64_t _commitCylinders(const size_t materialId);
uint64_t _commitCones(const size_t materialId);
Expand Down
190 changes: 97 additions & 93 deletions platform/engines/optix6/OptiXScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,124 +214,128 @@ void OptiXScene::commit()
{
auto& model = _modelDescriptors[i]->getModel();
model.commitSimulationData();

_commitVolumeParameters();
if (model.commitTransferFunction())
markModified();
}

commitLights();
_commitGeometryParameters();
_commitClippingPlanes();

if (!isModified())
return;

// Remove all models marked for removal
for (auto& model : _modelDescriptors)
if (model->isMarkedForRemoval())
model->callOnRemoved();

_modelDescriptors.erase(std::remove_if(_modelDescriptors.begin(), _modelDescriptors.end(),
[](const auto& m) { return m->isMarkedForRemoval(); }),
_modelDescriptors.end());

auto context = OptiXContext::get().getOptixContext();

auto values = std::map<TextureType, std::string>{{TextureType::diffuse, "envmap"},
{TextureType::radiance, "envmap_radiance"},
{TextureType::irradiance, "envmap_irradiance"},
{TextureType::brdf_lut, "envmap_brdf_lut"}};
if (hasEnvironmentMap())
_backgroundMaterial->commit();

auto optixMat = std::static_pointer_cast<OptiXMaterial>(_backgroundMaterial);
for (const auto& i : values)
if (isModified())
{
auto sampler = _dummyTextureSampler;
if (hasEnvironmentMap() && optixMat->hasTexture(i.first))
sampler = optixMat->getTextureSampler(i.first);
context[i.second]->setInt(sampler->getId());
if (i.first == TextureType::radiance && _backgroundMaterial->hasTexture(TextureType::radiance))
// Remove all models marked for removal
for (auto& model : _modelDescriptors)
if (model->isMarkedForRemoval())
model->callOnRemoved();

_modelDescriptors.erase(std::remove_if(_modelDescriptors.begin(), _modelDescriptors.end(),
[](const auto& m) { return m->isMarkedForRemoval(); }),
_modelDescriptors.end());

auto context = OptiXContext::get().getOptixContext();

auto values = std::map<TextureType, std::string>{{TextureType::diffuse, "envmap"},
{TextureType::radiance, "envmap_radiance"},
{TextureType::irradiance, "envmap_irradiance"},
{TextureType::brdf_lut, "envmap_brdf_lut"}};
if (hasEnvironmentMap())
_backgroundMaterial->commit();

auto optixMat = std::static_pointer_cast<OptiXMaterial>(_backgroundMaterial);
for (const auto& i : values)
{
const auto& radianceTex = _backgroundMaterial->getTexture(TextureType::radiance);
context[CONTEXT_MATERIAL_RADIANCE_LODS]->setUint(radianceTex->getMipLevels() - 1);
auto sampler = _dummyTextureSampler;
if (hasEnvironmentMap() && optixMat->hasTexture(i.first))
sampler = optixMat->getTextureSampler(i.first);
context[i.second]->setInt(sampler->getId());
if (i.first == TextureType::radiance && _backgroundMaterial->hasTexture(TextureType::radiance))
{
const auto& radianceTex = _backgroundMaterial->getTexture(TextureType::radiance);
context[CONTEXT_MATERIAL_RADIANCE_LODS]->setUint(radianceTex->getMipLevels() - 1);
}
}
}

context[CONTEXT_USE_ENVIRONMENT_MAP]->setUint(hasEnvironmentMap() ? 1 : 0);
context[CONTEXT_USE_ENVIRONMENT_MAP]->setUint(hasEnvironmentMap() ? 1 : 0);

// Geometry
if (_rootGroup)
_rootGroup->destroy();
// Geometry
if (_rootGroup)
_rootGroup->destroy();

_rootGroup = OptiXContext::get().createGroup();
_rootGroup = OptiXContext::get().createGroup();

for (size_t i = 0; i < _modelDescriptors.size(); ++i)
{
auto& modelDescriptor = _modelDescriptors[i];
if (!modelDescriptor->getEnabled())
continue;
for (size_t i = 0; i < _modelDescriptors.size(); ++i)
{
auto& modelDescriptor = _modelDescriptors[i];
if (!modelDescriptor->getEnabled())
continue;

auto& impl = static_cast<OptiXModel&>(modelDescriptor->getModel());
auto& impl = static_cast<OptiXModel&>(modelDescriptor->getModel());

CORE_DEBUG("Committing " << modelDescriptor->getName());
CORE_DEBUG("Committing " << modelDescriptor->getName());

impl.commitGeometry();
impl.logInformation();
impl.commitGeometry();
impl.logInformation();

if (modelDescriptor->getVisible())
{
const auto geometryGroup = impl.getGeometryGroup();
const auto& instances = modelDescriptor->getInstances();
size_t count{0};
for (const auto& instance : instances)
if (modelDescriptor->getVisible())
{
auto modelTransformation = instance.getTransformation();
if (count == 0)
modelTransformation = modelDescriptor->getTransformation();
const ::glm::mat4 matrix = modelTransformation.toMatrix(true);
::optix::Matrix4x4 optixMatrix(glm::value_ptr(matrix));
::optix::Transform instanceTransformation = context->createTransform();
instanceTransformation->setChild(geometryGroup);
instanceTransformation->setMatrix(true, optixMatrix.getData(), optixMatrix.inverse().getData());
_rootGroup->addChild(instanceTransformation);
++count;
const auto geometryGroup = impl.getGeometryGroup();
const auto& instances = modelDescriptor->getInstances();
size_t count{0};
for (const auto& instance : instances)
{
auto modelTransformation = instance.getTransformation();
if (count == 0)
modelTransformation = modelDescriptor->getTransformation();
const ::glm::mat4 matrix = modelTransformation.toMatrix(true);
::optix::Matrix4x4 optixMatrix(glm::value_ptr(matrix));
::optix::Transform instanceTransformation = context->createTransform();
instanceTransformation->setChild(geometryGroup);
instanceTransformation->setMatrix(true, optixMatrix.getData(), optixMatrix.inverse().getData());
_rootGroup->addChild(instanceTransformation);
++count;
}
CORE_DEBUG("Group has " << geometryGroup->getChildCount() << " children");
}
CORE_DEBUG("Group has " << geometryGroup->getChildCount() << " children");
}

if (modelDescriptor->getBoundingBox())
{
// scale and move the unit-sized bounding box geometry to the model size/scale first, then apply the
// instance transform
const auto boundingBoxGroup = impl.getBoundingBoxGroup();
::optix::Transform transformation = context->createTransform();

const auto& modelBounds = modelDescriptor->getModel().getBounds();
Transformation modelTransformation;
modelTransformation.setTranslation(modelBounds.getCenter() / modelBounds.getSize() - Vector3d(0.5));
modelTransformation.setScale(modelBounds.getSize());

Matrix4f modelMatrix = modelTransformation.toMatrix(true);
modelMatrix = glm::transpose(modelMatrix);
const auto trf = glm::value_ptr(modelMatrix);

transformation->setMatrix(false, trf, 0);
transformation->setChild(boundingBoxGroup);
_rootGroup->addChild(transformation);
if (modelDescriptor->getBoundingBox())
{
// scale and move the unit-sized bounding box geometry to the model size/scale first, then apply the
// instance transform
const auto boundingBoxGroup = impl.getBoundingBoxGroup();
::optix::Transform transformation = context->createTransform();

const auto& modelBounds = modelDescriptor->getModel().getBounds();
Transformation modelTransformation;
modelTransformation.setTranslation(modelBounds.getCenter() / modelBounds.getSize() - Vector3d(0.5));
modelTransformation.setScale(modelBounds.getSize());

Matrix4f modelMatrix = modelTransformation.toMatrix(true);
modelMatrix = glm::transpose(modelMatrix);
const auto trf = glm::value_ptr(modelMatrix);

transformation->setMatrix(false, trf, 0);
transformation->setChild(boundingBoxGroup);
_rootGroup->addChild(transformation);
}
}
}
computeBounds();
computeBounds();

CORE_DEBUG("Root has " << _rootGroup->getChildCount() << " children");
CORE_DEBUG("Root has " << _rootGroup->getChildCount() << " children");

context[CONTEXT_SCENE_TOP_OBJECT]->set(_rootGroup);
context[CONTEXT_SCENE_TOP_SHADOWER]->set(_rootGroup);
context[CONTEXT_SCENE_TOP_OBJECT]->set(_rootGroup);
context[CONTEXT_SCENE_TOP_SHADOWER]->set(_rootGroup);

// TODO: triggers the change callback to re-broadcast the scene if the clip planes have changed. Provide an RPC to
// update/set clip planes.
markModified();
// TODO: triggers the change callback to re-broadcast the scene if the clip planes have changed. Provide an RPC
// to update/set clip planes.
markModified();
}

for (size_t i = 0; i < _modelDescriptors.size(); ++i)
{
auto& model = _modelDescriptors[i]->getModel();
if (model.commitTransferFunction())
markModified();
}
}
} // namespace optix
} // namespace engine
Expand Down

0 comments on commit 333f75e

Please sign in to comment.