Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error when allocating too large ospray buffer. #437

Merged
merged 3 commits into from
Jun 22, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 75 additions & 98 deletions plugins/engines/ospray/OSPRayModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,72 +26,63 @@
#include <brayns/common/material/Material.h>
#include <brayns/common/scene/Scene.h>

namespace brayns
namespace
{
OSPRayModel::~OSPRayModel()
template <typename VecT>
OSPData allocateVectorData(const std::vector<VecT>& vec,
const OSPDataType ospType,
const size_t memoryManagementFlags)
{
if (_useSimulationModel)
{
for (auto geom : _ospExtendedSpheres)
ospRemoveGeometry(_simulationModel, geom.second);
for (auto geom : _ospExtendedCylinders)
ospRemoveGeometry(_simulationModel, geom.second);
for (auto geom : _ospExtendedCones)
ospRemoveGeometry(_simulationModel, geom.second);
for (auto geom : _ospSDFGeometryRefs)
ospRemoveGeometry(_simulationModel, geom.second);
}
else
{
for (auto geom : _ospExtendedSpheres)
ospRemoveGeometry(_model, geom.second);
for (auto geom : _ospExtendedCylinders)
ospRemoveGeometry(_model, geom.second);
for (auto geom : _ospExtendedCones)
ospRemoveGeometry(_model, geom.second);
for (auto geom : _ospSDFGeometryRefs)
ospRemoveGeometry(_model, geom.second);
}
const size_t totBytes = vec.size() * sizeof(decltype(vec.back()));

for (auto& geom : _ospExtendedSpheres)
ospRelease(geom.second);
_ospExtendedSpheres.clear();
for (auto& geom : _ospExtendedSpheresData)
ospRelease(geom.second);
_ospExtendedSpheresData.clear();
for (auto& geom : _ospExtendedCylinders)
ospRelease(geom.second);
_ospExtendedCylinders.clear();
for (auto& geom : _ospExtendedCylindersData)
ospRelease(geom.second);
_ospExtendedCylindersData.clear();
for (auto& geom : _ospExtendedCones)
ospRelease(geom.second);
_ospExtendedCones.clear();
for (auto& geom : _ospExtendedConesData)
ospRelease(geom.second);
_ospExtendedConesData.clear();
for (auto& geom : _ospMeshes)
ospRelease(geom.second);
_ospMeshes.clear();
for (auto& geom : _ospSDFGeometryRefsData)
ospRelease(geom.second);
_ospSDFGeometryRefsData.clear();

if (_simulationModel)
ospRelease(_simulationModel);
if (totBytes >= INT_MAX)
throw std::runtime_error("Buffer allocation too big.");

if (_boundingBoxModel)
ospRelease(_boundingBoxModel);
return ospNewData(totBytes / ospray::sizeOf(ospType), ospType, vec.data(),
memoryManagementFlags);
}
}

namespace brayns
{
OSPRayModel::~OSPRayModel()
{
const auto removeGeometry = [&](const auto& geometryMap) {
auto& model = _useSimulationModel ? _simulationModel : _model;
for (auto geom : geometryMap)
ospRemoveGeometry(model, geom.second);
};

if (_ospSDFGeometryData)
ospRelease(_ospSDFGeometryData);
const auto releaseAndClearGeometry = [&](auto& geometryMap) {
for (auto geom : geometryMap)
ospRelease(geom.second);
geometryMap.clear();
};

if (_ospSDFNeighboursData)
ospRelease(_ospSDFNeighboursData);
const auto releaseModel = [&](const auto& model) {
if (model)
ospRelease(model);
};

if (_model)
ospRelease(_model);
removeGeometry(_ospExtendedSpheres);
removeGeometry(_ospExtendedCylinders);
removeGeometry(_ospExtendedCones);
removeGeometry(_ospSDFGeometryRefs);

releaseAndClearGeometry(_ospExtendedSpheres);
releaseAndClearGeometry(_ospExtendedSpheresData);
releaseAndClearGeometry(_ospExtendedCylinders);
releaseAndClearGeometry(_ospExtendedCylindersData);
releaseAndClearGeometry(_ospExtendedCones);
releaseAndClearGeometry(_ospExtendedConesData);
releaseAndClearGeometry(_ospMeshes);
releaseAndClearGeometry(_ospSDFGeometryRefsData);

releaseModel(_simulationModel);
releaseModel(_boundingBoxModel);
releaseModel(_ospSDFGeometryData);
releaseModel(_ospSDFNeighboursData);
releaseModel(_model);
}

void OSPRayModel::setMemoryFlags(const size_t memoryManagementFlags)
Expand Down Expand Up @@ -147,14 +138,13 @@ void OSPRayModel::buildBoundingBox()
void OSPRayModel::_commitSpheres(const size_t materialId)
{
const auto& spheres = _spheres[materialId];
const auto bufferSize = spheres.size() * sizeof(Sphere);

if (_ospExtendedSpheres.find(materialId) != _ospExtendedSpheres.end())
ospRemoveGeometry(_model, _ospExtendedSpheres[materialId]);

_ospExtendedSpheres[materialId] = ospNewGeometry("extendedspheres");
_ospExtendedSpheresData[materialId] =
ospNewData(bufferSize / sizeof(float), OSP_FLOAT, spheres.data(),
_memoryManagementFlags);
allocateVectorData(spheres, OSP_FLOAT, _memoryManagementFlags);

ospSetObject(_ospExtendedSpheres[materialId], "extendedspheres",
_ospExtendedSpheresData[materialId]);
Expand All @@ -175,14 +165,12 @@ void OSPRayModel::_commitSpheres(const size_t materialId)
void OSPRayModel::_commitCylinders(const size_t materialId)
{
const auto& cylinders = _cylinders[materialId];
const auto bufferSize = cylinders.size() * sizeof(Cylinder);
if (_ospExtendedCylinders.find(materialId) != _ospExtendedCylinders.end())
ospRemoveGeometry(_model, _ospExtendedCylinders[materialId]);

_ospExtendedCylinders[materialId] = ospNewGeometry("extendedcylinders");
_ospExtendedCylindersData[materialId] =
ospNewData(bufferSize / sizeof(float), OSP_FLOAT, cylinders.data(),
_memoryManagementFlags);
allocateVectorData(cylinders, OSP_FLOAT, _memoryManagementFlags);
ospSetObject(_ospExtendedCylinders[materialId], "extendedcylinders",
_ospExtendedCylindersData[materialId]);

Expand All @@ -203,14 +191,13 @@ void OSPRayModel::_commitCylinders(const size_t materialId)
void OSPRayModel::_commitCones(const size_t materialId)
{
const auto& cones = _cones[materialId];
const auto bufferSize = cones.size() * sizeof(Cone);
if (_ospExtendedCones.find(materialId) != _ospExtendedCones.end())
ospRemoveGeometry(_model, _ospExtendedCones[materialId]);

_ospExtendedCones[materialId] = ospNewGeometry("extendedcones");
_ospExtendedConesData[materialId] =
ospNewData(bufferSize / sizeof(float), OSP_FLOAT, cones.data(),
_memoryManagementFlags);
allocateVectorData(cones, OSP_FLOAT, _memoryManagementFlags);

ospSetObject(_ospExtendedCones[materialId], "extendedcones",
_ospExtendedConesData[materialId]);

Expand All @@ -232,37 +219,32 @@ void OSPRayModel::_commitMeshes(const size_t materialId)
_ospMeshes[materialId] = ospNewGeometry("trianglemesh");

auto& trianglesMesh = _trianglesMeshes[materialId];
OSPData vertices =
ospNewData(trianglesMesh.vertices.size(), OSP_FLOAT3,
trianglesMesh.vertices.data(), _memoryManagementFlags);
OSPData vertices = allocateVectorData(trianglesMesh.vertices, OSP_FLOAT3,
_memoryManagementFlags);

if (!trianglesMesh.normals.empty())
{
OSPData normals =
ospNewData(trianglesMesh.normals.size(), OSP_FLOAT3,
trianglesMesh.normals.data(), _memoryManagementFlags);
OSPData normals = allocateVectorData(trianglesMesh.normals, OSP_FLOAT3,
_memoryManagementFlags);
ospSetObject(_ospMeshes[materialId], "vertex.normal", normals);
}

OSPData indices =
ospNewData(trianglesMesh.indices.size(), OSP_INT3,
trianglesMesh.indices.data(), _memoryManagementFlags);
OSPData indices = allocateVectorData(trianglesMesh.indices, OSP_INT3,
_memoryManagementFlags);

if (!trianglesMesh.colors.empty())
{
OSPData colors =
ospNewData(trianglesMesh.colors.size(), OSP_FLOAT3A,
trianglesMesh.colors.data(), _memoryManagementFlags);
OSPData colors = allocateVectorData(trianglesMesh.colors, OSP_FLOAT3A,
_memoryManagementFlags);
ospSetObject(_ospMeshes[materialId], "vertex.color", colors);
ospRelease(colors);
}

if (!trianglesMesh.textureCoordinates.empty())
{
OSPData texCoords =
ospNewData(trianglesMesh.textureCoordinates.size(), OSP_FLOAT2,
trianglesMesh.textureCoordinates.data(),
_memoryManagementFlags);
allocateVectorData(trianglesMesh.textureCoordinates, OSP_FLOAT2,
_memoryManagementFlags);
ospSetObject(_ospMeshes[materialId], "vertex.texcoord", texCoords);
ospRelease(texCoords);
}
Expand All @@ -288,10 +270,7 @@ void OSPRayModel::_commitSDFGeometries()
assert(_ospSDFNeighboursData == nullptr);

_ospSDFGeometryData =
ospNewData(_sdf.geometries.size() * sizeof(_sdf.geometries) /
sizeof(OSP_CHAR),
OSP_CHAR, _sdf.geometries.data(), _memoryManagementFlags);

allocateVectorData(_sdf.geometries, OSP_CHAR, _memoryManagementFlags);
ospCommit(_ospSDFGeometryData);

// Create and upload flat list of neighbours
Expand All @@ -311,12 +290,12 @@ void OSPRayModel::_commitSDFGeometries()
}
}

_ospSDFNeighboursData =
ospNewData(_sdf.neighboursFlat.size() *
sizeof(decltype(_sdf.neighboursFlat.back())) /
sizeof(OSP_CHAR),
OSP_CHAR, _sdf.neighboursFlat.data(),
_memoryManagementFlags);
// Make sure we don't create an empty buffer in the case of no neighbours
if (_sdf.neighboursFlat.empty())
_sdf.neighboursFlat.resize(1, 0);

_ospSDFNeighboursData = allocateVectorData(_sdf.neighboursFlat, OSP_CHAR,
_memoryManagementFlags);

ospCommit(_ospSDFNeighboursData);

Expand All @@ -327,17 +306,15 @@ void OSPRayModel::_commitSDFGeometries()
if (_sdf.geometryIndices.find(materialId) == _sdf.geometryIndices.end())
continue;

const auto& sdfRefs = _sdf.geometryIndices[materialId];
const auto bufferSize =
sdfRefs.size() * sizeof(decltype(sdfRefs.back()));
if (_ospSDFGeometryRefs.find(materialId) != _ospSDFGeometryRefs.end())
ospRemoveGeometry(_model, _ospSDFGeometryRefs[materialId]);

_ospSDFGeometryRefs[materialId] =
ospNewGeometry("extendedsdfgeometries");
const auto dSize = bufferSize / sizeof(uint32_t);

_ospSDFGeometryRefsData[materialId] =
ospNewData(dSize, OSP_UINT, sdfRefs.data(), _memoryManagementFlags);
allocateVectorData(_sdf.geometryIndices[materialId], OSP_UINT,
_memoryManagementFlags);

ospSetObject(_ospSDFGeometryRefs[materialId], "extendedsdfgeometries",
_ospSDFGeometryRefsData[materialId]);
Expand Down