Multithreaded scene initialization

This commit is contained in:
Emil Axelsson
2017-12-17 17:34:46 +01:00
parent 7cc05ba2e0
commit df259f6d02
11 changed files with 199 additions and 11 deletions

View File

@@ -33,13 +33,14 @@
#include <mutex>
#include <openspace/scene/scenegraphnode.h>
#include <openspace/scene/sceneinitializer.h>
#include <openspace/scene/scenelicense.h>
#include <openspace/scene/scenelicensewriter.h>
#include <openspace/scripting/scriptengine.h>
#include <openspace/util/camera.h>
#include <openspace/util/updatestructures.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/programobject.h>
namespace ghoul { class Dictionary; }
@@ -65,7 +66,7 @@ public:
};
// constructors & destructor
Scene();
Scene(std::unique_ptr<SceneInitializer> initializer);
~Scene();
/**
@@ -160,6 +161,11 @@ public:
*/
SceneGraphNode* loadNode(const ghoul::Dictionary& nodeDictionary);
/**
* Initialize a scene graph node.
*/
void initializeNode(SceneGraphNode* node);
/**
* Returns the Lua library that contains all Lua functions available to change the
* scene graph. The functions contained are
@@ -188,6 +194,7 @@ private:
std::unordered_map<std::string, SceneGraphNode*> _nodesByName;
bool _dirtyNodeRegistry;
SceneGraphNode _rootDummy;
std::unique_ptr<SceneInitializer> _initializer;
std::vector<SceneLicense> _licenses;

View File

@@ -54,6 +54,12 @@ namespace documentation { struct Documentation; }
class SceneGraphNode : public properties::PropertyOwner {
public:
enum class State : int {
Loaded,
Initialized,
GLInitialized
};
using UpdateScene = ghoul::Boolean;
struct PerformanceRecord {
@@ -138,6 +144,7 @@ private:
glm::dmat3 calculateWorldRotation() const;
double calculateWorldScale() const;
std::atomic<State> _state;
std::vector<std::unique_ptr<SceneGraphNode>> _children;
SceneGraphNode* _parent;
std::vector<SceneGraphNode*> _dependencies;

View File

@@ -0,0 +1,65 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_CORE___SCENEINITIALIZER___H__
#define __OPENSPACE_CORE___SCENEINITIALIZER___H__
#include <openspace/scene/scenegraphnode.h>
#include <ghoul/misc/threadpool.h>
#include <mutex>
namespace openspace {
class SceneInitializer {
public:
virtual ~SceneInitializer() = default;
virtual void initializeNode(SceneGraphNode* node) = 0;
virtual std::vector<SceneGraphNode*> getInitializedNodes() = 0;
};
class SingleThreadedSceneInitializer : public SceneInitializer {
public:
void initializeNode(SceneGraphNode* node) override;
std::vector<SceneGraphNode*> getInitializedNodes() override;
private:
std::vector<SceneGraphNode*> _initializedNodes;
};
class MultiThreadedSceneInitializer : public SceneInitializer {
public:
MultiThreadedSceneInitializer(unsigned int nThreads);
void initializeNode(SceneGraphNode* node) override;
std::vector<SceneGraphNode*> getInitializedNodes() override;
private:
std::vector<SceneGraphNode*> _initializedNodes;
ghoul::ThreadPool _threadPool;
std::mutex _mutex;
};
} // namespace openspace
#endif // __OPENSPACE_CORE___SCENEINITIALIZER___H__

View File

@@ -113,7 +113,7 @@ void TorrentClient::pollAlerts() {
}
}
size_t TorrentClient::addTorrentFile(std::string torrentFile, std::string destination, TorrentProgressCallback cb) {
TorrentClient::TorrentId TorrentClient::addTorrentFile(std::string torrentFile, std::string destination, TorrentProgressCallback cb) {
std::lock_guard<std::mutex> guard(_mutex);
if (!_session) {
@@ -136,7 +136,9 @@ size_t TorrentClient::addTorrentFile(std::string torrentFile, std::string destin
return id;
}
size_t TorrentClient::addMagnetLink(std::string magnetLink, std::string destination, TorrentProgressCallback cb) {
TorrentClient::TorrentId TorrentClient::addMagnetLink(std::string magnetLink,
std::string destination,
TorrentProgressCallback cb) {
std::lock_guard<std::mutex> guard(_mutex);
// TODO: register callback!

View File

@@ -64,8 +64,8 @@ public:
TorrentClient();
~TorrentClient();
void initialize();
size_t addTorrentFile(std::string torrentFile, std::string destination, TorrentProgressCallback cb);
size_t addMagnetLink(std::string magnetLink, std::string destination, TorrentProgressCallback cb);
TorrentId addTorrentFile(std::string torrentFile, std::string destination, TorrentProgressCallback cb);
TorrentId addMagnetLink(std::string magnetLink, std::string destination, TorrentProgressCallback cb);
void removeTorrent(TorrentId id);
void pollAlerts();
private:

View File

@@ -139,6 +139,7 @@ set(OPENSPACE_SOURCE
${OPENSPACE_BASE_DIR}/src/scene/scene.cpp
${OPENSPACE_BASE_DIR}/src/scene/scene_doc.inl
${OPENSPACE_BASE_DIR}/src/scene/scene_lua.inl
${OPENSPACE_BASE_DIR}/src/scene/sceneinitializer.cpp
${OPENSPACE_BASE_DIR}/src/scene/scenelicense.cpp
${OPENSPACE_BASE_DIR}/src/scene/scenelicensewriter.cpp
${OPENSPACE_BASE_DIR}/src/scene/scenegraphnode.cpp
@@ -310,6 +311,7 @@ set(OPENSPACE_HEADER
${OPENSPACE_BASE_DIR}/include/openspace/scene/rotation.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/scale.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/scene.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/sceneinitializer.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/scenelicense.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/scenelicensewriter.h
${OPENSPACE_BASE_DIR}/include/openspace/scene/scenegraphnode.h

View File

@@ -607,7 +607,22 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) {
_scene->clear();
}
_scene = std::make_unique<Scene>();
bool multiThreadedInitialization = configurationManager().hasKeyAndValue<bool>(
ConfigurationManager::KeyUseMultithreadedInitialization
) && configurationManager().value<bool>(
ConfigurationManager::KeyUseMultithreadedInitialization
);
std::unique_ptr<SceneInitializer> sceneInitializer;
if (multiThreadedInitialization) {
unsigned int nAvailableThreads = std::thread::hardware_concurrency();
unsigned int nThreads = nAvailableThreads == 0 ? 2 : nAvailableThreads - 1;
sceneInitializer = std::make_unique<MultiThreadedSceneInitializer>(nThreads);
} else {
sceneInitializer = std::make_unique<SingleThreadedSceneInitializer>();
}
_scene = std::make_unique<Scene>(std::move(sceneInitializer));
_scene->setCamera(std::make_unique<Camera>());
Camera* camera = _scene->camera();
camera->setParent(_scene->root());

View File

@@ -76,7 +76,7 @@ namespace {
namespace openspace {
Scene::Scene()
Scene::Scene(std::unique_ptr<SceneInitializer> initializer)
: DocumentationGenerator(
"Documented",
"propertyOwners",
@@ -96,6 +96,7 @@ Scene::Scene()
},
"${OPENSPACE_DATA}/web/properties/script.js"
)
, _initializer(std::move(initializer))
, _dirtyNodeRegistry(false)
{
_rootDummy.setName(SceneGraphNode::RootNodeName);
@@ -228,6 +229,10 @@ void Scene::sortTopologically() {
_topologicallySortedNodes = nodes;
}
void Scene::initializeNode(SceneGraphNode* node) {
_initializer->initializeNode(node);
}
/*
void Scene::initialize() {
@@ -299,6 +304,12 @@ void Scene::initializeGL() {
*/
void Scene::update(const UpdateData& data) {
std::vector<SceneGraphNode*> initializedNodes =
_initializer->getInitializedNodes();
for (SceneGraphNode* node : initializedNodes) {
node->initializeGL();
}
if (_dirtyNodeRegistry) {
updateNodeRegistry();
}

View File

@@ -363,8 +363,7 @@ int addSceneGraphNode(lua_State* L) {
return luaL_error(L, "Error loading scene graph node");
}
node->initialize();
node->initializeGL();
OsEng.renderEngine().scene()->initializeNode(node);
} catch (const ghoul::RuntimeError& e) {
return luaL_error(L, "Error loading scene graph node: %s", e.what());
}

View File

@@ -173,6 +173,7 @@ std::unique_ptr<SceneGraphNode> SceneGraphNode::createFromDictionary(
SceneGraphNode::SceneGraphNode()
: properties::PropertyOwner({ "" })
, _state(State::Loaded)
, _parent(nullptr)
, _scene(nullptr)
, _performanceRecord({0, 0, 0, 0, 0})
@@ -195,19 +196,20 @@ void SceneGraphNode::initialize() {
if (_transform.translation) {
_transform.translation->initialize();
}
if (_transform.rotation) {
_transform.rotation->initialize();
}
if (_transform.scale) {
_transform.scale->initialize();
}
_state = State::Initialized;
}
void SceneGraphNode::initializeGL() {
if (_renderable) {
_renderable->initializeGL();
}
_state = State::GLInitialized;
}
void SceneGraphNode::deinitialize() {
@@ -243,6 +245,10 @@ void SceneGraphNode::traversePostOrder(std::function<void(SceneGraphNode*)> fn)
}
void SceneGraphNode::update(const UpdateData& data) {
State s = _state;
if (s != State::Initialized && _state != State::GLInitialized) {
return;
}
if (_transform.translation) {
if (data.doPerformanceMeasurement) {
glFinish();
@@ -328,6 +334,10 @@ void SceneGraphNode::update(const UpdateData& data) {
}
void SceneGraphNode::render(const RenderData& data, RendererTasks& tasks) {
State s = _state;
if (_state != State::GLInitialized) {
return;
}
const psc thisPositionPSC = psc::CreatePowerScaledCoordinate(
_worldPositionCached.x,
_worldPositionCached.y,

View File

@@ -0,0 +1,70 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <openspace/scene/sceneinitializer.h>
#include <ghoul/logging/logmanager.h>
namespace {
const char* _loggerCat = "SceneInitializer";
}
namespace openspace {
void SingleThreadedSceneInitializer::initializeNode(SceneGraphNode* node) {
node->initialize();
}
std::vector<SceneGraphNode*> SingleThreadedSceneInitializer::getInitializedNodes() {
std::vector<SceneGraphNode*> nodes = std::move(_initializedNodes);
return nodes;
}
MultiThreadedSceneInitializer::MultiThreadedSceneInitializer(unsigned int nThreads)
: _threadPool(nThreads)
{}
void MultiThreadedSceneInitializer::initializeNode(SceneGraphNode* node) {
auto initFunction = [this](SceneGraphNode* node) {
node->initialize();
std::lock_guard<std::mutex> g(_mutex);
LDEBUG("Thread Initialized " << node->name());
_initializedNodes.push_back(node);
};
std::lock_guard<std::mutex> g(_mutex);
_threadPool.queue(initFunction, node);
node->initialize();
}
std::vector<SceneGraphNode*> MultiThreadedSceneInitializer::getInitializedNodes() {
std::lock_guard<std::mutex> g(_mutex);
std::vector<SceneGraphNode*> nodes = std::move(_initializedNodes);
if (!nodes.empty()) {
LDEBUG("Returning nodes...");
}
return nodes;
}
} // namespace openspace