Skip to content

Commit

Permalink
Cache file fixes (#229)
Browse files Browse the repository at this point in the history
* Cache file fixes

* Optimize reading speed, total loading time went from ~140 to ~60 seconds
* Fix missing progress update
* Don't build default scene when loading cache file

* CR
  • Loading branch information
tribal-tec committed Oct 18, 2017
1 parent afc3edc commit b0f7247
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 72 deletions.
24 changes: 18 additions & 6 deletions brayns/Brayns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,19 @@ struct Brayns::Impl
scene.buildDefault();
}

scene.buildMaterials();
scene.commitVolumeData();
scene.commitSimulationData();
scene.buildEnvironment();

loadingProgress.setMessage("Building geometry ...");
scene.buildGeometry();
const auto& geomParams = _parametersManager->getGeometryParameters();
if (geomParams.getLoadCacheFile().empty())
{
scene.buildMaterials();
loadingProgress.setMessage("Building geometry ...");
scene.buildGeometry();

if (!geomParams.getSaveCacheFile().empty())
scene.saveToCacheFile();
}

loadingProgress += LOADING_PROGRESS_STEP;

loadingProgress.setMessage("Building acceleration structure ...");
Expand Down Expand Up @@ -444,6 +450,12 @@ struct Brayns::Impl
}
scene.commitTransferFunctionData();

if (!geometryParameters.getLoadCacheFile().empty())
{
scene.loadFromCacheFile();
loadingProgress += tic;
}

if (!geometryParameters.getPDBFile().empty())
{
_loadPDBFile(geometryParameters.getPDBFile());
Expand Down Expand Up @@ -1172,7 +1184,7 @@ struct Brayns::Impl
void _saveSceneToCacheFile()
{
auto& scene = _engine->getScene();
scene.saveSceneToCacheFile();
scene.saveToCacheFile();
}

void _resetCamera()
Expand Down
8 changes: 7 additions & 1 deletion brayns/common/scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,17 @@ class Scene
/** Unloads geometry, materials, lights, models, etc. to free memory. */
BRAYNS_API virtual void unload();

/**
Loads geometry a binary cache file defined by the --load-cache-file
command line parameter
*/
BRAYNS_API virtual void loadFromCacheFile() = 0;

/**
Saves geometry a binary cache file defined by the --save-cache-file
command line parameter
*/
BRAYNS_API virtual void saveSceneToCacheFile() = 0;
BRAYNS_API virtual void saveToCacheFile() = 0;

/**
* @return true if the given volume file is supported by the engines' scene.
Expand Down
6 changes: 5 additions & 1 deletion plugins/engines/livre/LivreScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ void LivreScene::commitSimulationData()
{
}

void LivreScene::saveSceneToCacheFile()
void LivreScene::loadFromCacheFile()
{
}

void LivreScene::saveToCacheFile()
{
}

Expand Down
5 changes: 4 additions & 1 deletion plugins/engines/livre/LivreScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class LivreScene : public Scene
void commitSimulationData() final;

/** Unsupported by Livre. */
void saveSceneToCacheFile() final;
void loadFromCacheFile() final;

/** Unsupported by Livre. */
void saveToCacheFile() final;

/** @copydoc Scene::isVolumeSupported */
bool isVolumeSupported(const std::string& volumeFile) const final;
Expand Down
9 changes: 6 additions & 3 deletions plugins/engines/optix/OptiXScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ void OptiXScene::unload()
_optixTextureSamplers.clear();
}

void OptiXScene::loadFromCacheFile()
{
}

void OptiXScene::commit()
{
if (_geometryGroup)
Expand Down Expand Up @@ -1030,10 +1034,9 @@ uint64_t OptiXScene::_getBvhSize(const uint64_t nbElements) const
return 64 * 1.3f * nbElements;
}

void OptiXScene::saveSceneToCacheFile()
void OptiXScene::saveToCacheFile()
{
BRAYNS_ERROR << "OptiXScene::saveSceneToCacheFile not implemented"
<< std::endl;
BRAYNS_ERROR << "OptiXScene::saveToCacheFile not implemented" << std::endl;
}

bool OptiXScene::isVolumeSupported(const std::string& volumeFile) const
Expand Down
7 changes: 5 additions & 2 deletions plugins/engines/optix/OptiXScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ class OptiXScene : public brayns::Scene
/** @copydoc Scene::unload */
void unload() final;

/** @copydoc Scene::saveSceneToCacheFile */
void saveSceneToCacheFile() final;
/** @copydoc Scene::loadFromCacheFile */
void loadFromCacheFile() final;

/** @copydoc Scene::saveToCacheFile */
void saveToCacheFile() final;

/** @copydoc Scene::isVolumeSupported */
bool isVolumeSupported(const std::string& volumeFile) const final;
Expand Down
83 changes: 30 additions & 53 deletions plugins/engines/ospray/OSPRayScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ OSPModel OSPRayScene::modelImpl(const uint32_t index)
return lastModel->second;
}

void OSPRayScene::_saveCacheFile()
void OSPRayScene::saveToCacheFile()
{
const std::string& filename =
_parametersManager.getGeometryParameters().getSaveCacheFile();
Expand Down Expand Up @@ -369,10 +369,11 @@ void OSPRayScene::_saveCacheFile()
BRAYNS_INFO << "Scene successfully saved" << std::endl;
}

void OSPRayScene::_loadCacheFile()
void OSPRayScene::loadFromCacheFile()
{
const std::string& filename =
_parametersManager.getGeometryParameters().getLoadCacheFile();
const auto& geomParams = _parametersManager.getGeometryParameters();

const std::string& filename = geomParams.getLoadCacheFile();
BRAYNS_INFO << "Loading scene from binary file: " << filename << std::endl;
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (!file.good())
Expand Down Expand Up @@ -404,6 +405,9 @@ void OSPRayScene::_loadCacheFile()
_models[ts] = ospNewModel();
}

if (geomParams.getCircuitUseSimulationModel() && !_simulationModel)
_simulationModel = ospNewModel();

size_t nbMaterials;
file.read((char*)&nbMaterials, sizeof(size_t));
BRAYNS_INFO << nbMaterials << " materials" << std::endl;
Expand Down Expand Up @@ -519,49 +523,33 @@ void OSPRayScene::_loadCacheFile()
_serializeCones(materialId);

// Vertices
_trianglesMeshes[materialId].getVertices().clear();
bufferSize = 0;
file.read((char*)&bufferSize, sizeof(size_t));
for (size_t i = 0; i < bufferSize / sizeof(Vector3f); ++i)
{
Vector3f vertex;
file.read((char*)&vertex, sizeof(Vector3f));
_trianglesMeshes[materialId].getVertices().push_back(vertex);
}
_trianglesMeshes[materialId].getVertices().resize(bufferSize /
sizeof(Vector3f));
file.read((char*)_trianglesMeshes[materialId].getVertices().data(),
bufferSize);

// Indices
_trianglesMeshes[materialId].getIndices().clear();
bufferSize = 0;
file.read((char*)&bufferSize, sizeof(size_t));
for (size_t i = 0; i < bufferSize / sizeof(Vector3ui); ++i)
{
Vector3ui index;
file.read((char*)&index, sizeof(Vector3ui));
_trianglesMeshes[materialId].getIndices().push_back(index);
}
_trianglesMeshes[materialId].getIndices().resize(bufferSize /
sizeof(Vector3ui));
file.read((char*)_trianglesMeshes[materialId].getIndices().data(),
bufferSize);

// Normals
_trianglesMeshes[materialId].getNormals().clear();
bufferSize = 0;
file.read((char*)&bufferSize, sizeof(size_t));
for (size_t i = 0; i < bufferSize / sizeof(Vector3f); ++i)
{
Vector3f normal;
file.read((char*)&normal, sizeof(Vector3f));
_trianglesMeshes[materialId].getNormals().push_back(normal);
}
_trianglesMeshes[materialId].getNormals().resize(bufferSize /
sizeof(Vector3f));
file.read((char*)_trianglesMeshes[materialId].getNormals().data(),
bufferSize);

// Texture coordinates
_trianglesMeshes[materialId].getTextureCoordinates().clear();
bufferSize = 0;
file.read((char*)&bufferSize, sizeof(size_t));
for (size_t i = 0; i < bufferSize / sizeof(Vector2f); ++i)
{
Vector2f texCoord;
file.read((char*)&texCoord, sizeof(Vector2f));
_trianglesMeshes[materialId].getTextureCoordinates().push_back(
texCoord);
}
_trianglesMeshes[materialId].getTextureCoordinates().resize(
bufferSize / sizeof(Vector2f));
file.read(
(char*)_trianglesMeshes[materialId].getTextureCoordinates().data(),
bufferSize);

_buildMeshOSPGeometry(materialId);
}
Expand All @@ -571,7 +559,6 @@ void OSPRayScene::_loadCacheFile()

BRAYNS_INFO << _bounds << std::endl;
BRAYNS_INFO << "Scene successfully loaded" << std::endl;
file.close();
}

void OSPRayScene::_createModel(const uint32_t index)
Expand Down Expand Up @@ -862,7 +849,9 @@ void OSPRayScene::buildGeometry()

commitMaterials();

if (_parametersManager.getGeometryParameters().getGenerateMultipleModels())
const auto& geomParams = _parametersManager.getGeometryParameters();

if (geomParams.getGenerateMultipleModels())
// Initialize models according to timestamps
for (auto& material : _materials)
{
Expand All @@ -878,16 +867,12 @@ void OSPRayScene::buildGeometry()
// If no timestamp is available, create a default model at timestamp 0
_models[0] = ospNewModel();

if (_parametersManager.getGeometryParameters()
.getCircuitUseSimulationModel())
if (geomParams.getCircuitUseSimulationModel() && !_simulationModel)
_simulationModel = ospNewModel();

BRAYNS_INFO << "Models to process: " << _models.size() << std::endl;

uint64_t size = serializeGeometry();

if (!_parametersManager.getGeometryParameters().getLoadCacheFile().empty())
_loadCacheFile();
const size_t size = serializeGeometry();

size_t totalNbSpheres = 0;
size_t totalNbCylinders = 0;
Expand Down Expand Up @@ -920,9 +905,6 @@ void OSPRayScene::buildGeometry()
BRAYNS_INFO << "Total : " << size << " bytes" << std::endl;
BRAYNS_INFO << "---------------------------------------------------"
<< std::endl;

if (!_parametersManager.getGeometryParameters().getSaveCacheFile().empty())
_saveCacheFile();
}

uint64_t OSPRayScene::_buildMeshOSPGeometry(const size_t materialId)
Expand Down Expand Up @@ -1328,11 +1310,6 @@ OSPTexture2D OSPRayScene::_createTexture2D(const std::string& textureName)
return ospTexture;
}

void OSPRayScene::saveSceneToCacheFile()
{
_saveCacheFile();
}

bool OSPRayScene::isVolumeSupported(const std::string& volumeFile) const
{
return boost::algorithm::ends_with(volumeFile, ".raw");
Expand Down
10 changes: 5 additions & 5 deletions plugins/engines/ospray/OSPRayScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ class OSPRayScene : public brayns::Scene
/** @copydoc Scene::unload */
void unload() final;

/** @copydoc Scene::saveSceneToCacheFile */
void saveSceneToCacheFile() final;
/** @copydoc Scene::loadFromCacheFile */
void loadFromCacheFile() final;

/** @copydoc Scene::saveToCacheFile */
void saveToCacheFile() final;

/** @copydoc Scene::isVolumeSupported */
bool isVolumeSupported(const std::string& volumeFile) const final;
Expand All @@ -91,9 +94,6 @@ class OSPRayScene : public brayns::Scene
uint64_t _serializeCones(const size_t materialId);
uint64_t _buildMeshOSPGeometry(const size_t materialId);

void _loadCacheFile();
void _saveCacheFile();

std::map<uint32_t, OSPModel> _models;
OSPModel _simulationModel;
std::vector<OSPMaterial> _ospMaterials;
Expand Down

0 comments on commit b0f7247

Please sign in to comment.