From a58cc33ed0c72833e6478fbb27585ccaad14c438 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 13 Jan 2016 10:58:31 +0100 Subject: [PATCH 1/7] WIP, multithreaded loading based on exceptions, very slow probably. --- include/Engine/Core/ResourceManager.h | 95 +++++++++---- .../Schema/Entities/CollisionTestLevel.xml | 2 +- src/Engine/Core/ResourceManager.cpp | 129 ++++++++++++++---- src/Engine/Rendering/RenderQueueFactory.cpp | 3 +- src/Game/Game.cpp | 1 + 5 files changed, 178 insertions(+), 52 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index 86ed6254..5ba6ba5a 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -40,6 +40,30 @@ private: ResourceManager(); public: + //TODO: Check if this is ever used, and remove it if it isn't. + //Why would a resource load another resource async. in the ctor? + //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. + //Eg. If a Model loads a Texture asyncronously in the constructor, and it is not done yet. + struct StillLoadingException : public std::exception + { + virtual const char* what() const throw() + { + return "Resource is still loading."; + } + }; + //Should be thrown in a Resource's constructor if certain work needs to be handled by + //the main thread, and not a parallel worker thread. Eg. opengl commands. + struct WorkerCannotExecute : public std::exception + { + virtual const char* what() const throw() + { + return "Worker thread is unable to execute, main thread need to handle the code."; + } + }; + + static void AssertIsMainThread(); + static bool IsMainThread(); + /*static ResourceManager& Instance() { static ResourceManager s; @@ -49,15 +73,6 @@ public: template static void RegisterType(std::string typeName); - /** Preloads a resource and caches it for future use - - @tparam T Resource type. - @param resourceName Fully qualified name of the resource to preload. - */ - template - static void Preload(std::string resourceName); - static void Preload(std::string resourceType, std::string resourceName); - /** Checks if a resource is in cache @param resourceType Resource type as string. @@ -66,14 +81,25 @@ public: // TODO: Templateify static bool IsResourceLoaded(std::string resourceType, std::string resourceName); - /** Hot-loads a resource and caches it for future use + /** If the resource is not in cache, starts loading the resource and returns nullptr immediately. + If the resource has been loaded already, return a pointer to it. + + @tparam T Resource type. + @param resourceName Fully qualified name of the resource to load. + */ + template + static T* LoadAsync(std::string resourceName, Resource* parent = nullptr); + static Resource* LoadAsync(std::string resourceType, std::string resourceName, Resource* parent = nullptr); + + /** Hot-loads a resource and caches it for future use. + Fairly safe to assume that return value is a valid pointer, will only return nullptr on error. @tparam T Resource type. @param resourceName Fully qualified name of the resource to load. */ template static T* Load(std::string resourceName, Resource* parent = nullptr); - static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); + static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -87,19 +113,40 @@ public: static void Update(); private: + //Represents pointer values to signify that a resource haven't failed, but is not fully loaded. + class SpecialResourcePointer + { + public: + SpecialResourcePointer() + : m_Val(new Resource()) + {} + ~SpecialResourcePointer() + { + delete m_Val; + } + //Make this class implicitly convertible to the Resource*. + operator Resource* const() const { return m_Val; } + private: + Resource* const m_Val; + }; + //TODO: Check if this is ever used, and remove it if it isn't. + static SpecialResourcePointer m_StillLoading; + static SpecialResourcePointer m_LoadWithMainThread; + static std::unordered_map m_CompilerTypenameToResourceType; static std::unordered_map> m_FactoryFunctions; // type -> factory function static std::unordered_map, Resource*> m_ResourceCache; // (type, name) -> resource static std::unordered_map m_ResourceFromName; // name -> resource static std::unordered_map m_ResourceParents; // resource -> parent resource + static std::unordered_map, boost::thread> m_LoadingThreads; // (type, name) -> loading thread + static boost::recursive_mutex m_Mutex; + // TODO: Getters for IDs static unsigned int m_CurrentResourceTypeID; static std::unordered_map m_ResourceTypeIDs; // Number of resources of a type. Doubles as local ID. static std::unordered_map m_ResourceCount; - // Flag to suppress hot-load warnings when a preloading resource chain loads another resource - static bool m_Preloading; static FileWatcher m_FileWatcher; static void fileWatcherCallback(std::string path, FileWatcher::FileEventFlags flags); @@ -108,20 +155,20 @@ private: static unsigned int GetNewResourceID(unsigned int typeID); // Internal: Create a resource and cache it - static Resource* CreateResource(std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent); }; template T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) { - auto resourceTypename = typeid(T).name(); - auto it = m_CompilerTypenameToResourceType.find(resourceTypename); - if (it == m_CompilerTypenameToResourceType.end()) { - LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return nullptr; - } + auto resourceTypename = typeid(T).name(); + auto it = m_CompilerTypenameToResourceType.find(resourceTypename); + if (it == m_CompilerTypenameToResourceType.end()) { + LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); + return nullptr; + } - return static_cast(Load(it->second, resourceName, parent)); + return static_cast(Load(it->second, resourceName, parent)); } template @@ -132,16 +179,16 @@ void ResourceManager::RegisterType(std::string typeName) } template -void ResourceManager::Preload(std::string resourceName) +T* ResourceManager::LoadAsync(std::string resourceName, Resource* parent /* = nullptr */) { auto resourceTypename = typeid(T).name(); auto it = m_CompilerTypenameToResourceType.find(resourceTypename); if (it == m_CompilerTypenameToResourceType.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return; + return nullptr; } - Preload(it->second, resourceName); + return static_cast(LoadAsync(it->second, resourceName, parent)); } #endif diff --git a/resources/Schema/Entities/CollisionTestLevel.xml b/resources/Schema/Entities/CollisionTestLevel.xml index 99842b88..d5e932c2 100644 --- a/resources/Schema/Entities/CollisionTestLevel.xml +++ b/resources/Schema/Entities/CollisionTestLevel.xml @@ -17,7 +17,7 @@ - Models/ScaleWidget.obj + Models/Core/UnitSphere.obj diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index 62a60f14..32af073d 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -1,4 +1,7 @@ #include "Core/ResourceManager.h" +#include "boost/thread/thread.hpp" +#include "boost/thread/mutex.hpp" +#include "boost/thread/lock_guard.hpp" std::unordered_map ResourceManager::m_CompilerTypenameToResourceType; std::unordered_map> ResourceManager::m_FactoryFunctions; @@ -8,8 +11,11 @@ std::unordered_map ResourceManager::m_ResourceParents; unsigned int ResourceManager::m_CurrentResourceTypeID = 0; std::unordered_map ResourceManager::m_ResourceTypeIDs; std::unordered_map ResourceManager::m_ResourceCount; -bool ResourceManager::m_Preloading = false; FileWatcher ResourceManager::m_FileWatcher; +std::unordered_map, boost::thread> ResourceManager::m_LoadingThreads; +boost::recursive_mutex ResourceManager::m_Mutex; +ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading; +ResourceManager::SpecialResourcePointer ResourceManager::m_LoadWithMainThread; unsigned int ResourceManager::GetTypeID(std::string resourceType) { @@ -74,37 +80,90 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -void ResourceManager::Preload(std::string resourceType, std::string resourceName) +Resource* ResourceManager::LoadAsync(std::string resourceType, std::string resourceName, Resource* parent /*= nullptr*/) { - if (IsResourceLoaded(resourceType, resourceName)) { - //LOG_WARNING("Attempted to preload resource \"%s\" multiple times!", resourceName.c_str()); - return; - } + auto cacheKey = std::make_pair(resourceType, resourceName); + decltype(m_ResourceCache)::iterator it; + //If a thread has already been launched to load this resource. + auto tIt = m_LoadingThreads.find(cacheKey); + if (tIt != m_LoadingThreads.end()) { + //If the thread is still working. + if (tIt->second.joinable()) { + return nullptr; + } + //Else, the thread is done. + m_LoadingThreads.erase(tIt); + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + //If the thread is done, but it cannot complete the rest, main thread must complete construction. + if (it->second == m_LoadWithMainThread) { + AssertIsMainThread(); + return createResource(resourceType, resourceName, parent); + } + return it->second; + } else { + //If cache is still empty at cacheKey after thread finishes, it failed. + return nullptr; + } + } - m_Preloading = true; - LOG_INFO("Preloading resource \"%s\"", resourceName.c_str()); - CreateResource(resourceType, resourceName, nullptr); - m_Preloading = false; + //If resource has already been loaded and cached. + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + //if ConstructByMainThread, createResource from Main. + return it->second; + } + + //Create a thread that loads the resource into cache. + m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + return nullptr; } Resource* ResourceManager::Load(std::string resourceType, std::string resourceName, Resource* parent /*= nullptr*/) { - auto it = m_ResourceCache.find(std::make_pair(resourceType, resourceName)); - if (it != m_ResourceCache.end()) { - return it->second; - } + Resource* resource; + auto cacheKey = std::make_pair(resourceType, resourceName); + decltype(m_ResourceCache)::iterator it; + //If a thread has already been launched to load this resource. + auto tIt = m_LoadingThreads.find(cacheKey); + if (tIt != m_LoadingThreads.end()) { + //Wait for the thread to finish loading. + tIt->second.join(); + //Then delete the thread. + m_LoadingThreads.erase(tIt); + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + //If the thread is done, but it cannot complete the rest, main thread must complete construction. + if (it->second == m_LoadWithMainThread) { + AssertIsMainThread(); + return createResource(resourceType, resourceName, parent); + } + return it->second; + } else { + //If cache is still empty at cacheKey after thread finishes, it failed. + return nullptr; + } + } + //If resource has already been loaded and cached. + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + return it->second; + } - if (m_Preloading) { - LOG_INFO("Preloading resource \"%s\"", resourceName.c_str()); - } else { - LOG_WARNING("Hot-loading resource \"%s\"", resourceName.c_str()); - } - - return CreateResource(resourceType, resourceName, parent); + //If resource is not cached, load and return it. + resource = createResource(resourceType, resourceName, parent); + if (resource == m_LoadWithMainThread) { + //If we entered here then we know the caller is a worker thread. + //And we know the resource must be loaded from master thread. + throw(WorkerCannotExecute()); + } + return resource; } -Resource* ResourceManager::CreateResource(std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent) { + //Lock the mutex immediately, and unlock it when leaving the function. + boost::lock_guard guard(m_Mutex); auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str()); @@ -112,25 +171,43 @@ Resource* ResourceManager::CreateResource(std::string resourceType, std::string } // Call the factory function - Resource* resource; + Resource* resource = nullptr; try { resource = facIt->second(resourceName); + } catch (const WorkerCannotExecute& e) { + resource = m_LoadWithMainThread; + } catch (const std::exception& e) { + LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); + } + if (resource != nullptr && resource != m_LoadWithMainThread) { // Store IDs resource->TypeID = GetTypeID(resourceType); resource->ResourceID = GetNewResourceID(resource->TypeID); - } catch (const std::exception& e) { - resource = nullptr; - LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); } + // Cache m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; m_ResourceFromName[resourceName] = resource; if (parent != nullptr) { m_ResourceParents[resource] = parent; } + if (!boost::filesystem::is_directory(resourceName)) { LOG_DEBUG("Adding watch for %s", resourceName.c_str()); m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); } return resource; } + +bool ResourceManager::IsMainThread() +{ + static boost::thread::id MainThreadId = boost::this_thread::get_id(); + return boost::this_thread::get_id() == MainThreadId; +} + +void ResourceManager::AssertIsMainThread() +{ + if (!IsMainThread()) { + throw WorkerCannotExecute(); + } +} \ No newline at end of file diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 8d5bb420..27651a72 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -84,7 +84,8 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) continue; } glm::vec4 color = modelC["Color"]; - Model* model = ResourceManager::Load(resource); + //Model* model = ResourceManager::Load(resource); + Model* model = ResourceManager::LoadAsync(resource); if (model == nullptr) { model = ResourceManager::Load("Models/Core/Error.obj"); } diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 033db642..ee4c7cbf 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -4,6 +4,7 @@ Game::Game(int argc, char* argv[]) { + ResourceManager::AssertIsMainThread(); ResourceManager::RegisterType("ConfigFile"); ResourceManager::RegisterType("Model"); ResourceManager::RegisterType("Texture"); From a143ae7c858dfd115e7570cfde5d317d951f10c0 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 13 Jan 2016 15:53:55 +0100 Subject: [PATCH 2/7] Changed to relative #include paths. --- include/Engine/Collision/Collision.h | 8 ++++---- include/Engine/Collision/CollisionSystem.h | 8 ++++---- include/Engine/Collision/TriggerSystem.h | 4 ++-- include/Engine/Core/Ray.h | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/Engine/Collision/Collision.h b/include/Engine/Collision/Collision.h index 714cee3f..a441c998 100644 --- a/include/Engine/Collision/Collision.h +++ b/include/Engine/Collision/Collision.h @@ -7,10 +7,10 @@ #include -#include "Core/Ray.h" -#include "Core/AABB.h" -#include "Engine/Rendering/RawModel.h" -#include "Core/Entity.h" +#include "../Core/Ray.h" +#include "../Core/AABB.h" +#include "../Rendering/RawModel.h" +#include "../Core/Entity.h" class World; struct ComponentWrapper; diff --git a/include/Engine/Collision/CollisionSystem.h b/include/Engine/Collision/CollisionSystem.h index 254a2461..f4752ecf 100644 --- a/include/Engine/Collision/CollisionSystem.h +++ b/include/Engine/Collision/CollisionSystem.h @@ -4,10 +4,10 @@ #include #include -#include "Common.h" -#include "Core/System.h" -#include "Core/EventBroker.h" -#include "Core/EKeyUp.h" +#include "../Common.h" +#include "../Core/System.h" +#include "../Core/EventBroker.h" +#include "../Core/EKeyUp.h" class CollisionSystem : public PureSystem { diff --git a/include/Engine/Collision/TriggerSystem.h b/include/Engine/Collision/TriggerSystem.h index 7e6ef008..dfb56a2c 100644 --- a/include/Engine/Collision/TriggerSystem.h +++ b/include/Engine/Collision/TriggerSystem.h @@ -4,8 +4,8 @@ #include #include -#include "Core/System.h" -#include "Core/EventBroker.h" +#include "../Core/System.h" +#include "../Core/EventBroker.h" #include "ETrigger.h" class AABB; diff --git a/include/Engine/Core/Ray.h b/include/Engine/Core/Ray.h index 0fcef01e..a234a488 100644 --- a/include/Engine/Core/Ray.h +++ b/include/Engine/Core/Ray.h @@ -2,7 +2,7 @@ #define Ray_h__ #include "../GLM.h" -#include "Common.h" +#include "../Common.h" class Ray { From 9f185a1e35452404a3987e1349b86e4a751d4f97 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Wed, 13 Jan 2016 18:51:45 +0100 Subject: [PATCH 3/7] Wrecked the last commit. Async loading works now. --- include/Engine/Core/ResourceManager.h | 145 ++++++++++++++------ include/Engine/Rendering/Model.h | 1 + include/Engine/Rendering/RawModel.h | 1 + include/Engine/Rendering/Texture.h | 3 + src/Engine/Collision/Collision.cpp | 4 +- src/Engine/Core/ResourceManager.cpp | 93 +------------ src/Engine/Rendering/Model.cpp | 101 +++++++------- src/Engine/Rendering/RawModel.cpp | 16 +++ src/Engine/Rendering/RenderQueueFactory.cpp | 1 - src/Engine/Rendering/Texture.cpp | 27 ++-- src/Game/Game.cpp | 1 - 11 files changed, 194 insertions(+), 199 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index 5ba6ba5a..32122092 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -12,14 +12,33 @@ /** Base Resource class. Implement this class for every resource to be handled by the resource manager. - Implement Create() to return a new object of that type. + + If it should be possible to load the resource asyncronously (on a separate thread in the background), + using LoadAsync() then any necessary OpenGL calls (Eg. glGenBuffers(), glBindBuffer(), etc.) must be + made in GlCommands() method, and not inside the constructor (see Texture.cpp for an example). */ class Resource { friend class ResourceManager; +private: + bool m_FullyConstructed; + protected: - Resource() { } + Resource() : m_FullyConstructed(false) { } + //This method only needs to be overridden if: + // 1: a) It should be possible to load the resource with LoadAsync(), or + // b) This resource will be loaded inside the constructor of another resource that can be loaded with LoadAsync(). + // c) Same as above but recursively. + // 2: a) The resource needs to make OpenGL calls, or + // b) The resource contains a resource that makes OGL calls, or + // c) The resource contains a resource that contains ... ... a resource that makes OGL calls, or + // + //If 2.a: any calls should be made in the GlCommands. + //If 2.b or 2.c: PostCtorGLCommands() should be called on the contained resource(s). + //If GlCommands needs to be overridden and GlCommands is also overridden by a baseclass then the baseclass + //implementation should be called in from the derived class implementation (see Model.cpp for an example). + virtual void GlCommands() { } public: // Pretend that this is a pure virtual function that you have to implement @@ -28,6 +47,16 @@ public: virtual void Reload() { } virtual void OnChildReloaded(Resource* child) { } + //If this resource implements GlCommands and it is loaded in another resource, + //then the containing resource must also implement GlCommands and + //call this method in it (see RawModel.cpp for an example). + void PostCtorGLCommands() + { + if (!m_FullyConstructed) { + GlCommands(); + } + m_FullyConstructed = true; + } unsigned int TypeID; unsigned int ResourceID; @@ -40,30 +69,6 @@ private: ResourceManager(); public: - //TODO: Check if this is ever used, and remove it if it isn't. - //Why would a resource load another resource async. in the ctor? - //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. - //Eg. If a Model loads a Texture asyncronously in the constructor, and it is not done yet. - struct StillLoadingException : public std::exception - { - virtual const char* what() const throw() - { - return "Resource is still loading."; - } - }; - //Should be thrown in a Resource's constructor if certain work needs to be handled by - //the main thread, and not a parallel worker thread. Eg. opengl commands. - struct WorkerCannotExecute : public std::exception - { - virtual const char* what() const throw() - { - return "Worker thread is unable to execute, main thread need to handle the code."; - } - }; - - static void AssertIsMainThread(); - static bool IsMainThread(); - /*static ResourceManager& Instance() { static ResourceManager s; @@ -89,7 +94,6 @@ public: */ template static T* LoadAsync(std::string resourceName, Resource* parent = nullptr); - static Resource* LoadAsync(std::string resourceType, std::string resourceName, Resource* parent = nullptr); /** Hot-loads a resource and caches it for future use. Fairly safe to assume that return value is a valid pointer, will only return nullptr on error. @@ -99,7 +103,6 @@ public: */ template static T* Load(std::string resourceName, Resource* parent = nullptr); - static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -113,25 +116,15 @@ public: static void Update(); private: - //Represents pointer values to signify that a resource haven't failed, but is not fully loaded. - class SpecialResourcePointer + //This is a haxy way to make sure that IsMainThread is run when the program starts, so the master thread id is set. + struct MasterThreadChecker { - public: - SpecialResourcePointer() - : m_Val(new Resource()) - {} - ~SpecialResourcePointer() + MasterThreadChecker() { - delete m_Val; + ResourceManager::IsMainThread(); } - //Make this class implicitly convertible to the Resource*. - operator Resource* const() const { return m_Val; } - private: - Resource* const m_Val; }; - //TODO: Check if this is ever used, and remove it if it isn't. - static SpecialResourcePointer m_StillLoading; - static SpecialResourcePointer m_LoadWithMainThread; + static MasterThreadChecker m_Checker; static std::unordered_map m_CompilerTypenameToResourceType; static std::unordered_map> m_FactoryFunctions; // type -> factory function @@ -156,6 +149,10 @@ private: // Internal: Create a resource and cache it static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent); + + static bool IsMainThread(); + template + static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); }; template @@ -168,7 +165,7 @@ T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr return nullptr; } - return static_cast(Load(it->second, resourceName, parent)); + return static_cast(Load(it->second, resourceName, parent)); } template @@ -188,7 +185,65 @@ T* ResourceManager::LoadAsync(std::string resourceName, Resource* parent /* = nu return nullptr; } - return static_cast(LoadAsync(it->second, resourceName, parent)); + return static_cast(Load(it->second, resourceName, parent)); +} + +template +static Resource* ResourceManager::Load(std::string resourceType, std::string resourceName, Resource* parent /* = nullptr */) +{ + auto cacheKey = std::make_pair(resourceType, resourceName); + decltype(m_ResourceCache)::iterator it; + //If a thread has already been launched to load this resource. + auto tIt = m_LoadingThreads.find(cacheKey); + if (tIt != m_LoadingThreads.end()) { + if (Async) { + //Return null if the thread is still working. + if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { + return nullptr; + } + //Else we know the thread has completed. + } else { + //Wait for the thread to finish loading. + tIt->second.join(); + } + //When the thread is done, delete the thread. + m_LoadingThreads.erase(tIt); + //Find the resource that the thread loaded. + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + //At this point the resource may not be completely + //done since the worker threads cannot do opengl commands. + if (IsMainThread()) { + //Do the gl commands to complete the resource if we are not a worker thread. + it->second->PostCtorGLCommands(); + } + return it->second; + } else { + //If resource is still null at cacheKey after thread finishes, it failed. + return nullptr; + } + } + + //If resource has already been loaded and cached. + it = m_ResourceCache.find(cacheKey); + if (it != m_ResourceCache.end()) { + return it->second; + } + + //If resource is not cached.. + if (Async) { + //Create a thread that loads the resource into cache. + m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + return nullptr; + } else { + //load and return the resource. + Resource* res = createResource(resourceType, resourceName, parent); + if (IsMainThread() && res != nullptr) { + //Do the gl commands to complete the resource if we are not a worker thread. + res->PostCtorGLCommands(); + } + return res; + } } #endif diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index 9cc145af..ffef745c 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -10,6 +10,7 @@ class Model : public RawModel private: Model(std::string fileName); + virtual void GlCommands() override; public: ~Model(); diff --git a/include/Engine/Rendering/RawModel.h b/include/Engine/Rendering/RawModel.h index c8226168..379b81ab 100644 --- a/include/Engine/Rendering/RawModel.h +++ b/include/Engine/Rendering/RawModel.h @@ -24,6 +24,7 @@ class RawModel : public Resource protected: RawModel(std::string fileName); + virtual void GlCommands() override; public: ~RawModel(); diff --git a/include/Engine/Rendering/Texture.h b/include/Engine/Rendering/Texture.h index 16892afc..8cdcaef0 100644 --- a/include/Engine/Rendering/Texture.h +++ b/include/Engine/Rendering/Texture.h @@ -11,7 +11,10 @@ class Texture : public BaseTexture private: Texture(std::string path); + virtual void GlCommands() override; + GLint m_Format; + Image* m_Image; public: ~Texture(); diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index c4af6258..7473c67f 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -220,7 +220,7 @@ bool attachAABBComponentFromModel(World* world, EntityID id) } ComponentWrapper model = world->GetComponent(id, "Model"); ComponentWrapper collision = world->AttachComponent(id, "AABB"); - Model* modelRes = ResourceManager::Load(model["Resource"]); + Model* modelRes = ResourceManager::LoadAsync(model["Resource"]); if (modelRes == nullptr) { return false; } @@ -247,7 +247,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) { ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform"); ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model"); - Model* modelRes = ResourceManager::Load(model["Resource"]); + Model* modelRes = ResourceManager::LoadAsync(model["Resource"]); outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]); glm::vec3 mini = outBox.MinCorner(); glm::vec3 maxi = outBox.MaxCorner(); diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index 32af073d..102e99ba 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -14,8 +14,6 @@ std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; std::unordered_map, boost::thread> ResourceManager::m_LoadingThreads; boost::recursive_mutex ResourceManager::m_Mutex; -ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading; -ResourceManager::SpecialResourcePointer ResourceManager::m_LoadWithMainThread; unsigned int ResourceManager::GetTypeID(std::string resourceType) { @@ -80,86 +78,6 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -Resource* ResourceManager::LoadAsync(std::string resourceType, std::string resourceName, Resource* parent /*= nullptr*/) -{ - auto cacheKey = std::make_pair(resourceType, resourceName); - decltype(m_ResourceCache)::iterator it; - //If a thread has already been launched to load this resource. - auto tIt = m_LoadingThreads.find(cacheKey); - if (tIt != m_LoadingThreads.end()) { - //If the thread is still working. - if (tIt->second.joinable()) { - return nullptr; - } - //Else, the thread is done. - m_LoadingThreads.erase(tIt); - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - //If the thread is done, but it cannot complete the rest, main thread must complete construction. - if (it->second == m_LoadWithMainThread) { - AssertIsMainThread(); - return createResource(resourceType, resourceName, parent); - } - return it->second; - } else { - //If cache is still empty at cacheKey after thread finishes, it failed. - return nullptr; - } - } - - //If resource has already been loaded and cached. - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - //if ConstructByMainThread, createResource from Main. - return it->second; - } - - //Create a thread that loads the resource into cache. - m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); - return nullptr; -} - -Resource* ResourceManager::Load(std::string resourceType, std::string resourceName, Resource* parent /*= nullptr*/) -{ - Resource* resource; - auto cacheKey = std::make_pair(resourceType, resourceName); - decltype(m_ResourceCache)::iterator it; - //If a thread has already been launched to load this resource. - auto tIt = m_LoadingThreads.find(cacheKey); - if (tIt != m_LoadingThreads.end()) { - //Wait for the thread to finish loading. - tIt->second.join(); - //Then delete the thread. - m_LoadingThreads.erase(tIt); - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - //If the thread is done, but it cannot complete the rest, main thread must complete construction. - if (it->second == m_LoadWithMainThread) { - AssertIsMainThread(); - return createResource(resourceType, resourceName, parent); - } - return it->second; - } else { - //If cache is still empty at cacheKey after thread finishes, it failed. - return nullptr; - } - } - //If resource has already been loaded and cached. - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - return it->second; - } - - //If resource is not cached, load and return it. - resource = createResource(resourceType, resourceName, parent); - if (resource == m_LoadWithMainThread) { - //If we entered here then we know the caller is a worker thread. - //And we know the resource must be loaded from master thread. - throw(WorkerCannotExecute()); - } - return resource; -} - Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent) { //Lock the mutex immediately, and unlock it when leaving the function. @@ -174,12 +92,10 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string Resource* resource = nullptr; try { resource = facIt->second(resourceName); - } catch (const WorkerCannotExecute& e) { - resource = m_LoadWithMainThread; } catch (const std::exception& e) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); } - if (resource != nullptr && resource != m_LoadWithMainThread) { + if (resource != nullptr) { // Store IDs resource->TypeID = GetTypeID(resourceType); resource->ResourceID = GetNewResourceID(resource->TypeID); @@ -204,10 +120,3 @@ bool ResourceManager::IsMainThread() static boost::thread::id MainThreadId = boost::this_thread::get_id(); return boost::this_thread::get_id() == MainThreadId; } - -void ResourceManager::AssertIsMainThread() -{ - if (!IsMainThread()) { - throw WorkerCannotExecute(); - } -} \ No newline at end of file diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index f346d9e1..c2aab44f 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -3,58 +3,65 @@ Model::Model(std::string fileName) : RawModel(fileName) { - // Generate GL buffers - GLuint buffer; - glGenBuffers(1, &buffer); - glBindBuffer(GL_ARRAY_BUFFER, buffer); - glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(Vertex), &m_Vertices[0], GL_STATIC_DRAW); +} - glGenBuffers(1, &ElementBuffer); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Indices.size() * sizeof(unsigned int), &m_Indices[0], GL_STATIC_DRAW); +void Model::GlCommands() +{ + //Call the base class method. + RawModel::GlCommands(); - glGenVertexArrays(1, &VAO); - glBindVertexArray(VAO); - GLERROR("GLEW: BufferFail4"); + // Generate GL buffers + GLuint buffer; + glGenBuffers(1, &buffer); + glBindBuffer(GL_ARRAY_BUFFER, buffer); + glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(Vertex), &m_Vertices[0], GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, buffer); - std::vector structSizes = { 3, 3, 3, 3, 2, 4, 4, 4, 4, 4, 4 }; - int stride = 0; - for (int size : structSizes) { - stride += size; - } - stride *= sizeof(GLfloat); - int offset = 0; - { - int element = 0; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * offset)); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - } - GLERROR("GLEW: BufferFail5"); + glGenBuffers(1, &ElementBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Indices.size() * sizeof(unsigned int), &m_Indices[0], GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); - glEnableVertexAttribArray(5); - glEnableVertexAttribArray(6); - glEnableVertexAttribArray(7); - glEnableVertexAttribArray(8); - glEnableVertexAttribArray(9); - glEnableVertexAttribArray(10); - GLERROR("GLEW: BufferFail5"); + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + GLERROR("GLEW: BufferFail4"); - //CreateBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, buffer); + std::vector structSizes = { 3, 3, 3, 3, 2, 4, 4, 4, 4, 4, 4 }; + int stride = 0; + for (int size : structSizes) { + stride += size; + } + stride *= sizeof(GLfloat); + int offset = 0; + { + int element = 0; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * offset)); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; + } + GLERROR("GLEW: BufferFail5"); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glEnableVertexAttribArray(3); + glEnableVertexAttribArray(4); + glEnableVertexAttribArray(5); + glEnableVertexAttribArray(6); + glEnableVertexAttribArray(7); + glEnableVertexAttribArray(8); + glEnableVertexAttribArray(9); + glEnableVertexAttribArray(10); + GLERROR("GLEW: BufferFail5"); + + //CreateBuffers(); } Model::~Model() diff --git a/src/Engine/Rendering/RawModel.cpp b/src/Engine/Rendering/RawModel.cpp index 95a75a15..9428f21e 100644 --- a/src/Engine/Rendering/RawModel.cpp +++ b/src/Engine/Rendering/RawModel.cpp @@ -292,6 +292,22 @@ RawModel::RawModel(std::string fileName) } } +void RawModel::GlCommands() +{ + //Since RawModel contains Textures that was loaded in the constructor, their GlCommands must be run. + for (auto& texGroup : TextureGroups) { + if (texGroup.Texture) { + texGroup.Texture->PostCtorGLCommands(); + } + if (texGroup.NormalMap) { + texGroup.NormalMap->PostCtorGLCommands(); + } + if (texGroup.SpecularMap) { + texGroup.SpecularMap->PostCtorGLCommands(); + } + } +} + RawModel::~RawModel() { if (m_Skeleton) { diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 27651a72..71b2e086 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -84,7 +84,6 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) continue; } glm::vec4 color = modelC["Color"]; - //Model* model = ResourceManager::Load(resource); Model* model = ResourceManager::LoadAsync(resource); if (model == nullptr) { model = ResourceManager::Load("Models/Core/Error.obj"); diff --git a/src/Engine/Rendering/Texture.cpp b/src/Engine/Rendering/Texture.cpp index 19c829bf..7e1cd65b 100644 --- a/src/Engine/Rendering/Texture.cpp +++ b/src/Engine/Rendering/Texture.cpp @@ -2,39 +2,44 @@ Texture::Texture(std::string path) { - PNG image(path); + m_Image = new PNG(path); - if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { - image = PNG("Textures/Core/ErrorTexture.png"); - if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { + if (m_Image->Width == 0 && m_Image->Height == 0 || m_Image->Format == Image::ImageFormat::Unknown) { + delete m_Image; + m_Image = new PNG("Textures/Core/ErrorTexture.png"); + if (m_Image->Width == 0 && m_Image->Height == 0 || m_Image->Format == Image::ImageFormat::Unknown) { LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed."); return; } } - this->Width = image.Width; - this->Height = image.Height; + this->Width = m_Image->Width; + this->Height = m_Image->Height; - GLint format; - switch (image.Format) { + switch (m_Image->Format) { case Image::ImageFormat::RGB: - format = GL_RGB; + m_Format = GL_RGB; break; case Image::ImageFormat::RGBA: - format = GL_RGBA; + m_Format = GL_RGBA; break; } +} +void Texture::GlCommands() +{ // Construct the OpenGL texture glGenTextures(1, &m_Texture); glBindTexture(GL_TEXTURE_2D, m_Texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, format, image.Width, image.Height, 0, format, GL_UNSIGNED_BYTE, image.Data); + glTexImage2D(GL_TEXTURE_2D, 0, m_Format, Width, Height, 0, m_Format, GL_UNSIGNED_BYTE, m_Image->Data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); GLERROR("Texture load"); + delete m_Image; + m_Image = nullptr; } Texture::~Texture() diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index ee4c7cbf..033db642 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -4,7 +4,6 @@ Game::Game(int argc, char* argv[]) { - ResourceManager::AssertIsMainThread(); ResourceManager::RegisterType("ConfigFile"); ResourceManager::RegisterType("Model"); ResourceManager::RegisterType("Texture"); From 71047b08ec5d4c9b986d49c5533aa491f19581ef Mon Sep 17 00:00:00 2001 From: William Moberg Date: Thu, 14 Jan 2016 16:27:45 +0100 Subject: [PATCH 4/7] Async load works, using exceptions for special resources. --- include/Engine/Core/ResourceManager.h | 177 +++++++++----------- include/Engine/Rendering/BaseTexture.h | 2 +- include/Engine/Rendering/Model.h | 7 +- include/Engine/Rendering/RawModel.h | 4 +- include/Engine/Rendering/Texture.h | 3 - src/Engine/Collision/Collision.cpp | 10 +- src/Engine/Core/ResourceManager.cpp | 3 + src/Engine/Rendering/Model.cpp | 27 ++- src/Engine/Rendering/RawModel.cpp | 28 +--- src/Engine/Rendering/RenderQueueFactory.cpp | 6 +- src/Engine/Rendering/Renderer.cpp | 4 +- src/Engine/Rendering/Texture.cpp | 69 ++++---- src/Game/Game.cpp | 1 + 13 files changed, 160 insertions(+), 181 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index 32122092..9ebf545a 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -12,33 +12,13 @@ /** Base Resource class. Implement this class for every resource to be handled by the resource manager. - - If it should be possible to load the resource asyncronously (on a separate thread in the background), - using LoadAsync() then any necessary OpenGL calls (Eg. glGenBuffers(), glBindBuffer(), etc.) must be - made in GlCommands() method, and not inside the constructor (see Texture.cpp for an example). */ class Resource { friend class ResourceManager; -private: - bool m_FullyConstructed; - protected: - Resource() : m_FullyConstructed(false) { } - //This method only needs to be overridden if: - // 1: a) It should be possible to load the resource with LoadAsync(), or - // b) This resource will be loaded inside the constructor of another resource that can be loaded with LoadAsync(). - // c) Same as above but recursively. - // 2: a) The resource needs to make OpenGL calls, or - // b) The resource contains a resource that makes OGL calls, or - // c) The resource contains a resource that contains ... ... a resource that makes OGL calls, or - // - //If 2.a: any calls should be made in the GlCommands. - //If 2.b or 2.c: PostCtorGLCommands() should be called on the contained resource(s). - //If GlCommands needs to be overridden and GlCommands is also overridden by a baseclass then the baseclass - //implementation should be called in from the derived class implementation (see Model.cpp for an example). - virtual void GlCommands() { } + Resource() { } public: // Pretend that this is a pure virtual function that you have to implement @@ -47,21 +27,30 @@ public: virtual void Reload() { } virtual void OnChildReloaded(Resource* child) { } - //If this resource implements GlCommands and it is loaded in another resource, - //then the containing resource must also implement GlCommands and - //call this method in it (see RawModel.cpp for an example). - void PostCtorGLCommands() - { - if (!m_FullyConstructed) { - GlCommands(); - } - m_FullyConstructed = true; - } unsigned int TypeID; unsigned int ResourceID; }; +//Any class inheriting from this class will always be loaded on the master thread, not on a parallel worker thread. +//This is important in case some instructions must be executed on the main thread, e.g. OpenGL commands, like glBindBuffer. +//This resource can still be loaded asyncronously, but it will not be loaded in a thread, instead it's constructor will +//be called once on every ResourceManager::Load, just throw StillLoadingException in the constructor if it is not done yet. +class ThreadUnsafeResource : public Resource +{ + friend class ResourceManager; +protected: + //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. + //Not actually an error, just a message to the ResourceManager. + struct StillLoadingException : public std::exception + { + virtual const char* what() const throw() + { + return "Resource is still loading."; + } + }; +}; + /** Singleton resource manager to keep track of and cache any external engine assets */ class ResourceManager { @@ -85,24 +74,20 @@ public: */ // TODO: Templateify static bool IsResourceLoaded(std::string resourceType, std::string resourceName); - - /** If the resource is not in cache, starts loading the resource and returns nullptr immediately. + + /** If Async is false: Hot-loads a resource and caches it for future use. + Fairly safe to assume that return value is always a valid pointer, will only return nullptr on error. + + If Async is true: If the resource is not loaded yet, starts loading the resource + in the background and returns nullptr immediately. If the resource has been loaded already, return a pointer to it. - @tparam T Resource type. - @param resourceName Fully qualified name of the resource to load. - */ - template - static T* LoadAsync(std::string resourceName, Resource* parent = nullptr); - - /** Hot-loads a resource and caches it for future use. - Fairly safe to assume that return value is a valid pointer, will only return nullptr on error. - @tparam T Resource type. + @tparam Async Set this to true if the resource should be loaded asyncronously. @param resourceName Fully qualified name of the resource to load. */ - template - static T* Load(std::string resourceName, Resource* parent = nullptr); + template + static T* Load(std::string resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -116,6 +101,22 @@ public: static void Update(); private: + //Represents a pointer value to signify that a resource haven't failed, but is not fully loaded. + class SpecialResourcePointer + { + public: + SpecialResourcePointer() + : m_Val(new Resource()) + {} + ~SpecialResourcePointer() + { + delete m_Val; + } + //Make this class implicitly convertible to the Resource*. + operator Resource* const() const { return m_Val; } + private: + Resource* const m_Val; + }; //This is a haxy way to make sure that IsMainThread is run when the program starts, so the master thread id is set. struct MasterThreadChecker { @@ -124,7 +125,8 @@ private: ResourceManager::IsMainThread(); } }; - static MasterThreadChecker m_Checker; + const static SpecialResourcePointer m_StillLoading; + const static MasterThreadChecker m_Checker; static std::unordered_map m_CompilerTypenameToResourceType; static std::unordered_map> m_FactoryFunctions; // type -> factory function @@ -151,23 +153,8 @@ private: static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent); static bool IsMainThread(); - template - static Resource* Load(std::string resourceType, std::string resourceName, Resource* parent = nullptr); }; -template -T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) -{ - auto resourceTypename = typeid(T).name(); - auto it = m_CompilerTypenameToResourceType.find(resourceTypename); - if (it == m_CompilerTypenameToResourceType.end()) { - LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return nullptr; - } - - return static_cast(Load(it->second, resourceName, parent)); -} - template void ResourceManager::RegisterType(std::string typeName) { @@ -175,28 +162,29 @@ void ResourceManager::RegisterType(std::string typeName) m_FactoryFunctions[typeName] = [](std::string resourceName) { return new T(resourceName); }; } -template -T* ResourceManager::LoadAsync(std::string resourceName, Resource* parent /* = nullptr */) +template +static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) { - auto resourceTypename = typeid(T).name(); - auto it = m_CompilerTypenameToResourceType.find(resourceTypename); - if (it == m_CompilerTypenameToResourceType.end()) { - LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return nullptr; - } + auto resourceTypename = typeid(T).name(); + auto iter = m_CompilerTypenameToResourceType.find(resourceTypename); + if (iter == m_CompilerTypenameToResourceType.end()) { + LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); + return nullptr; + } - return static_cast(Load(it->second, resourceName, parent)); -} + std::string resourceType = iter->second; + constexpr bool mustNotLoadInThread = std::is_base_of::value; + if (mustNotLoadInThread && !IsMainThread()) { + LOG_ERROR("Failed to Load \"%s\": ThreadUnsafeResource type \"%s\" load in the constructor of another resource that is loaded asyncronously.", resourceName.c_str(), iter->second.c_str()); + return nullptr; + } -template -static Resource* ResourceManager::Load(std::string resourceType, std::string resourceName, Resource* parent /* = nullptr */) -{ auto cacheKey = std::make_pair(resourceType, resourceName); decltype(m_ResourceCache)::iterator it; //If a thread has already been launched to load this resource. auto tIt = m_LoadingThreads.find(cacheKey); if (tIt != m_LoadingThreads.end()) { - if (Async) { + if (async) { //Return null if the thread is still working. if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { return nullptr; @@ -211,38 +199,39 @@ static Resource* ResourceManager::Load(std::string resourceType, std::string res //Find the resource that the thread loaded. it = m_ResourceCache.find(cacheKey); if (it != m_ResourceCache.end()) { - //At this point the resource may not be completely - //done since the worker threads cannot do opengl commands. - if (IsMainThread()) { - //Do the gl commands to complete the resource if we are not a worker thread. - it->second->PostCtorGLCommands(); - } - return it->second; + //Threads should not be able to throw StillLoadingException, so no check should be needed. + return static_cast(it->second); } else { - //If resource is still null at cacheKey after thread finishes, it failed. + //If cacheKey does not exist after thread finishes, it failed. return nullptr; } } - //If resource has already been loaded and cached. + //If resource has already been cached and completely loaded. it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - return it->second; + if (it != m_ResourceCache.end() && it->second != m_StillLoading) { + return static_cast(it->second); } + Resource* res = nullptr; //If resource is not cached.. - if (Async) { - //Create a thread that loads the resource into cache. - m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + if (async) { + if (mustNotLoadInThread) { + res = createResource(resourceType, resourceName, parent); + if (res != m_StillLoading) { + return static_cast(res); + } + } else { + //Create a thread that loads the resource into cache. + m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + } return nullptr; } else { //load and return the resource. - Resource* res = createResource(resourceType, resourceName, parent); - if (IsMainThread() && res != nullptr) { - //Do the gl commands to complete the resource if we are not a worker thread. - res->PostCtorGLCommands(); - } - return res; + do { + res = createResource(resourceType, resourceName, parent); + } while (res == m_StillLoading); + return static_cast(res); } } diff --git a/include/Engine/Rendering/BaseTexture.h b/include/Engine/Rendering/BaseTexture.h index 26ae399d..96df5561 100644 --- a/include/Engine/Rendering/BaseTexture.h +++ b/include/Engine/Rendering/BaseTexture.h @@ -3,7 +3,7 @@ #include "../Core/ResourceManager.h" -class BaseTexture : public Resource +class BaseTexture : public ThreadUnsafeResource { friend class ResourceManager; diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index ffef745c..77e3df76 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -4,21 +4,24 @@ #include "RawModel.h" #include "../OpenGL.h" -class Model : public RawModel +class Model : public ThreadUnsafeResource { friend class ResourceManager; private: Model(std::string fileName); - virtual void GlCommands() override; public: ~Model(); + const std::vector& TextureGroups() const { return m_RawModel->TextureGroups; } + const glm::mat4& Matrix() const { return m_RawModel->m_Matrix; } + const std::vector& Vertices() const { return m_RawModel->m_Vertices; } GLuint VAO; GLuint ElementBuffer; private: + RawModel* m_RawModel; GLuint VertexBuffer; GLuint DiffuseVertexColorBuffer; GLuint SpecularVertexColorBuffer; diff --git a/include/Engine/Rendering/RawModel.h b/include/Engine/Rendering/RawModel.h index 379b81ab..82619c86 100644 --- a/include/Engine/Rendering/RawModel.h +++ b/include/Engine/Rendering/RawModel.h @@ -24,7 +24,6 @@ class RawModel : public Resource protected: RawModel(std::string fileName); - virtual void GlCommands() override; public: ~RawModel(); @@ -47,8 +46,11 @@ public: struct MaterialGroup { float Shininess; + std::string TexturePath; std::shared_ptr<::Texture> Texture; + std::string NormalMapPath; std::shared_ptr<::Texture> NormalMap; + std::string SpecularMapPath; std::shared_ptr<::Texture> SpecularMap; unsigned int StartIndex; unsigned int EndIndex; diff --git a/include/Engine/Rendering/Texture.h b/include/Engine/Rendering/Texture.h index 8cdcaef0..16892afc 100644 --- a/include/Engine/Rendering/Texture.h +++ b/include/Engine/Rendering/Texture.h @@ -11,10 +11,7 @@ class Texture : public BaseTexture private: Texture(std::string path); - virtual void GlCommands() override; - GLint m_Format; - Image* m_Image; public: ~Texture(); diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index 7473c67f..7abc63ea 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -220,16 +220,16 @@ bool attachAABBComponentFromModel(World* world, EntityID id) } ComponentWrapper model = world->GetComponent(id, "Model"); ComponentWrapper collision = world->AttachComponent(id, "AABB"); - Model* modelRes = ResourceManager::LoadAsync(model["Resource"]); + Model* modelRes = ResourceManager::Load(model["Resource"]); if (modelRes == nullptr) { return false; } - glm::mat4 modelMatrix = modelRes->m_Matrix; + glm::mat4 modelMatrix = modelRes->Matrix(); glm::vec3 mini = glm::vec3(INFINITY, INFINITY, INFINITY); glm::vec3 maxi = glm::vec3(-INFINITY, -INFINITY, -INFINITY); - for (const auto& v : modelRes->m_Vertices) { + for (const auto& v : modelRes->Vertices()) { const auto& wPos = modelMatrix * glm::vec4(v.Position.x, v.Position.y, v.Position.z, 1); maxi.x = std::max(wPos.x, maxi.x); maxi.y = std::max(wPos.y, maxi.y); @@ -247,7 +247,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) { ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform"); ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model"); - Model* modelRes = ResourceManager::LoadAsync(model["Resource"]); + Model* modelRes = ResourceManager::Load(model["Resource"]); outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]); glm::vec3 mini = outBox.MinCorner(); glm::vec3 maxi = outBox.MaxCorner(); @@ -255,7 +255,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) if (modelRes == nullptr) { return false; } - glm::mat4 modelMatrix = modelRes->m_Matrix * + glm::mat4 modelMatrix = modelRes->Matrix() * glm::translate(glm::mat4(), (glm::vec3)cTrans["Position"]) * glm::scale((glm::vec3)cTrans["Scale"]); diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index 102e99ba..ffd6c76c 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -14,6 +14,7 @@ std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; std::unordered_map, boost::thread> ResourceManager::m_LoadingThreads; boost::recursive_mutex ResourceManager::m_Mutex; +const ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading; unsigned int ResourceManager::GetTypeID(std::string resourceType) { @@ -92,6 +93,8 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string Resource* resource = nullptr; try { resource = facIt->second(resourceName); + } catch (const ThreadUnsafeResource::StillLoadingException&) { + resource = m_StillLoading; } catch (const std::exception& e) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); } diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index c2aab44f..8e24b704 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -1,24 +1,35 @@ #include "Rendering/Model.h" Model::Model(std::string fileName) - : RawModel(fileName) { -} + //Load the RawModel asyncronously, this will be done on a separate thread in the background. + m_RawModel = ResourceManager::Load(fileName); + //If it is null, the model is not done yet, so tell resourceManager to try constructing me again later. + if (m_RawModel == nullptr) { + throw StillLoadingException(); + } -void Model::GlCommands() -{ - //Call the base class method. - RawModel::GlCommands(); + for (auto& group : m_RawModel->TextureGroups) { + if (!group.TexturePath.empty()) { + group.Texture = std::shared_ptr(ResourceManager::Load(group.TexturePath)); + } + if (!group.NormalMapPath.empty()) { + group.NormalMap = std::shared_ptr(ResourceManager::Load(group.NormalMapPath)); + } + if (!group.SpecularMapPath.empty()) { + group.SpecularMap = std::shared_ptr(ResourceManager::Load(group.SpecularMapPath)); + } + } // Generate GL buffers GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); - glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(Vertex), &m_Vertices[0], GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, m_RawModel->m_Vertices.size() * sizeof(RawModel::Vertex), &m_RawModel->m_Vertices[0], GL_STATIC_DRAW); glGenBuffers(1, &ElementBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_Indices.size() * sizeof(unsigned int), &m_Indices[0], GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_RawModel->m_Indices.size() * sizeof(unsigned int), &m_RawModel->m_Indices[0], GL_STATIC_DRAW); glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); diff --git a/src/Engine/Rendering/RawModel.cpp b/src/Engine/Rendering/RawModel.cpp index 9428f21e..fbb406f6 100644 --- a/src/Engine/Rendering/RawModel.cpp +++ b/src/Engine/Rendering/RawModel.cpp @@ -141,9 +141,7 @@ RawModel::RawModel(std::string fileName) aiString path; aiTextureMapping mapping; material->GetTexture(aiTextureType_DIFFUSE, 0, &path, &mapping); - std::string absolutePath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); - //LOG_DEBUG("Diffuse texture: %s", absolutePath.c_str()); - matGroup.Texture = std::shared_ptr(ResourceManager::Load(absolutePath)); + matGroup.TexturePath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); } // Normal map //LOG_DEBUG("%i normal maps found", material->GetTextureCount(aiTextureType_HEIGHT)); @@ -151,9 +149,7 @@ RawModel::RawModel(std::string fileName) aiString path; aiTextureMapping mapping; material->GetTexture(aiTextureType_HEIGHT, 0, &path, &mapping); - std::string absolutePath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); - //LOG_DEBUG("Normal map: %s", absolutePath.c_str()); - matGroup.NormalMap = std::shared_ptr(ResourceManager::Load(absolutePath)); + matGroup.NormalMapPath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); } // Specular map //LOG_DEBUG("%i specular maps found", material->GetTextureCount(aiTextureType_SPECULAR)); @@ -161,9 +157,7 @@ RawModel::RawModel(std::string fileName) aiString path; aiTextureMapping mapping; material->GetTexture(aiTextureType_SPECULAR, 0, &path, &mapping); - std::string absolutePath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); - //LOG_DEBUG("Specular map: %s", absolutePath.c_str()); - matGroup.SpecularMap = std::shared_ptr(ResourceManager::Load(absolutePath)); + matGroup.SpecularMapPath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); } TextureGroups.push_back(matGroup); @@ -292,22 +286,6 @@ RawModel::RawModel(std::string fileName) } } -void RawModel::GlCommands() -{ - //Since RawModel contains Textures that was loaded in the constructor, their GlCommands must be run. - for (auto& texGroup : TextureGroups) { - if (texGroup.Texture) { - texGroup.Texture->PostCtorGLCommands(); - } - if (texGroup.NormalMap) { - texGroup.NormalMap->PostCtorGLCommands(); - } - if (texGroup.SpecularMap) { - texGroup.SpecularMap->PostCtorGLCommands(); - } - } -} - RawModel::~RawModel() { if (m_Skeleton) { diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 71b2e086..66eacd67 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -84,12 +84,12 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) continue; } glm::vec4 color = modelC["Color"]; - Model* model = ResourceManager::LoadAsync(resource); + Model* model = ResourceManager::Load(resource); if (model == nullptr) { model = ResourceManager::Load("Models/Core/Error.obj"); } - for (auto texGroup : model->TextureGroups) { + for (auto texGroup : model->TextureGroups()) { ModelJob job; job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; job.DiffuseTexture = texGroup.Texture.get(); @@ -98,7 +98,7 @@ void RenderQueueFactory::FillModels(World* world, RenderQueue* renderQueue) job.Model = model; job.StartIndex = texGroup.StartIndex; job.EndIndex = texGroup.EndIndex; - job.ModelMatrix = model->m_Matrix * ModelMatrix(world, modelC.EntityID); + job.ModelMatrix = model->Matrix() * ModelMatrix(world, modelC.EntityID); job.Color = color; //TODO: RENDERER: Not sure if the best solution for pickingColor to entity link is this diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 208f339b..d0788b94 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -161,8 +161,8 @@ void Renderer::DrawScreenQuad(GLuint textureToDraw) glBindVertexArray(m_ScreenQuad->VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer); - glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->TextureGroups[0].EndIndex - m_ScreenQuad->TextureGroups[0].StartIndex +1 - , GL_UNSIGNED_INT, 0, m_ScreenQuad->TextureGroups[0].StartIndex); + glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->TextureGroups()[0].EndIndex - m_ScreenQuad->TextureGroups()[0].StartIndex +1 + , GL_UNSIGNED_INT, 0, m_ScreenQuad->TextureGroups()[0].StartIndex); } void Renderer::InitializeTextures() diff --git a/src/Engine/Rendering/Texture.cpp b/src/Engine/Rendering/Texture.cpp index 7e1cd65b..57f3ca36 100644 --- a/src/Engine/Rendering/Texture.cpp +++ b/src/Engine/Rendering/Texture.cpp @@ -2,53 +2,48 @@ Texture::Texture(std::string path) { - m_Image = new PNG(path); + PNG image(path); - if (m_Image->Width == 0 && m_Image->Height == 0 || m_Image->Format == Image::ImageFormat::Unknown) { - delete m_Image; - m_Image = new PNG("Textures/Core/ErrorTexture.png"); - if (m_Image->Width == 0 && m_Image->Height == 0 || m_Image->Format == Image::ImageFormat::Unknown) { - LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed."); - return; - } - } + if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { + image = PNG("Textures/Core/ErrorTexture.png"); + if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { + LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed."); + return; + } + } - this->Width = m_Image->Width; - this->Height = m_Image->Height; + this->Width = image.Width; + this->Height = image.Height; - switch (m_Image->Format) { - case Image::ImageFormat::RGB: - m_Format = GL_RGB; - break; - case Image::ImageFormat::RGBA: - m_Format = GL_RGBA; - break; - } -} + GLint format; + switch (image.Format) { + case Image::ImageFormat::RGB: + format = GL_RGB; + break; + case Image::ImageFormat::RGBA: + format = GL_RGBA; + break; + } -void Texture::GlCommands() -{ - // Construct the OpenGL texture - glGenTextures(1, &m_Texture); - glBindTexture(GL_TEXTURE_2D, m_Texture); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, m_Format, Width, Height, 0, m_Format, GL_UNSIGNED_BYTE, m_Image->Data); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - GLERROR("Texture load"); - delete m_Image; - m_Image = nullptr; + // Construct the OpenGL texture + glGenTextures(1, &m_Texture); + glBindTexture(GL_TEXTURE_2D, m_Texture); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, 0, format, image.Width, image.Height, 0, format, GL_UNSIGNED_BYTE, image.Data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + GLERROR("Texture load"); } Texture::~Texture() { - glDeleteTextures(1, &m_Texture); + glDeleteTextures(1, &m_Texture); } void Texture::Bind(GLenum textureUnit /* = GL_TEXTURE0 */) { - glActiveTexture(textureUnit); - glBindTexture(GL_TEXTURE_2D, m_Texture); + glActiveTexture(textureUnit); + glBindTexture(GL_TEXTURE_2D, m_Texture); } diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 033db642..b6c8b21e 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -6,6 +6,7 @@ Game::Game(int argc, char* argv[]) { ResourceManager::RegisterType("ConfigFile"); ResourceManager::RegisterType("Model"); + ResourceManager::RegisterType("RawModel"); ResourceManager::RegisterType("Texture"); ResourceManager::RegisterType("EntityXMLFile"); ResourceManager::RegisterType("ShaderProgram"); From 76f90ab0317d35871a19fc4c7a0aed014c00373b Mon Sep 17 00:00:00 2001 From: William Moberg Date: Fri, 15 Jan 2016 11:29:46 +0100 Subject: [PATCH 5/7] The main thread is not mutex blocked while child threads load anymore. --- assets | 2 +- include/Engine/Rendering/Model.h | 2 +- include/Engine/Rendering/ModelJob.h | 14 ++++---- include/Engine/Rendering/RawModel.h | 2 +- resources/Schema/Entities/ThreadTestMap.xml | 30 ++++++++++++++++ src/Engine/Core/ResourceManager.cpp | 38 ++++++++++++--------- src/Engine/Rendering/Model.cpp | 2 +- src/Engine/Rendering/RawModel.cpp | 2 +- src/Engine/Rendering/RenderSystem.cpp | 6 ++-- src/Engine/Rendering/Renderer.cpp | 4 +-- 10 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 resources/Schema/Entities/ThreadTestMap.xml diff --git a/assets b/assets index 6cbf2365..a3c92ac8 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6cbf2365d49e6280750ea3bcd0f9c271779e6f15 +Subproject commit a3c92ac876dd061776c36d1594bd82264372f028 diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index 77e3df76..280fe5af 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -13,7 +13,7 @@ private: public: ~Model(); - const std::vector& TextureGroups() const { return m_RawModel->TextureGroups; } + const std::vector& MaterialGroups() const { return m_RawModel->MaterialGroups; } const glm::mat4& Matrix() const { return m_RawModel->m_Matrix; } const std::vector& Vertices() const { return m_RawModel->m_Vertices; } diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index d598766f..353d1bbe 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -15,16 +15,16 @@ struct ModelJob : RenderJob { - ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::Model::MaterialGroup texGroup, ComponentWrapper modelComponent, World* world) + ModelJob(Model* model, Camera* camera, glm::mat4 matrix, ::RawModel::MaterialGroup matGroup, ComponentWrapper modelComponent, World* world) : RenderJob() { Model = model; - TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; - DiffuseTexture = texGroup.Texture.get(); - NormalTexture = texGroup.NormalMap.get(); - SpecularTexture = texGroup.SpecularMap.get(); - StartIndex = texGroup.StartIndex; - EndIndex = texGroup.EndIndex; + TextureID = (matGroup.Texture) ? matGroup.Texture->ResourceID : 0; + DiffuseTexture = matGroup.Texture.get(); + NormalTexture = matGroup.NormalMap.get(); + SpecularTexture = matGroup.SpecularMap.get(); + StartIndex = matGroup.StartIndex; + EndIndex = matGroup.EndIndex; Matrix = matrix; Color = modelComponent["Color"]; Entity = modelComponent.EntityID; diff --git a/include/Engine/Rendering/RawModel.h b/include/Engine/Rendering/RawModel.h index 5117f953..0477edb1 100644 --- a/include/Engine/Rendering/RawModel.h +++ b/include/Engine/Rendering/RawModel.h @@ -57,7 +57,7 @@ public: unsigned int EndIndex; }; - std::vector TextureGroups; + std::vector MaterialGroups; std::vector m_Vertices; std::vector m_Indices; diff --git a/resources/Schema/Entities/ThreadTestMap.xml b/resources/Schema/Entities/ThreadTestMap.xml new file mode 100644 index 00000000..b4b358c0 --- /dev/null +++ b/resources/Schema/Entities/ThreadTestMap.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index ffd6c76c..30614e5c 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -81,8 +81,6 @@ void ResourceManager::Update() Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent) { - //Lock the mutex immediately, and unlock it when leaving the function. - boost::lock_guard guard(m_Mutex); auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str()); @@ -98,23 +96,29 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string } catch (const std::exception& e) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); } - if (resource != nullptr) { - // Store IDs - resource->TypeID = GetTypeID(resourceType); - resource->ResourceID = GetNewResourceID(resource->TypeID); + + { + //Lock the mutex immediately, and unlock it when leaving the code block. + boost::lock_guard guard(m_Mutex); + if (resource != nullptr) { + // Store IDs + resource->TypeID = GetTypeID(resourceType); + resource->ResourceID = GetNewResourceID(resource->TypeID); + } + + // Cache + m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; + m_ResourceFromName[resourceName] = resource; + if (parent != nullptr) { + m_ResourceParents[resource] = parent; + } + + if (!boost::filesystem::is_directory(resourceName)) { + LOG_DEBUG("Adding watch for %s", resourceName.c_str()); + m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); + } } - // Cache - m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; - m_ResourceFromName[resourceName] = resource; - if (parent != nullptr) { - m_ResourceParents[resource] = parent; - } - - if (!boost::filesystem::is_directory(resourceName)) { - LOG_DEBUG("Adding watch for %s", resourceName.c_str()); - m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); - } return resource; } diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index 8e24b704..c1afa037 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -9,7 +9,7 @@ Model::Model(std::string fileName) throw StillLoadingException(); } - for (auto& group : m_RawModel->TextureGroups) { + for (auto& group : m_RawModel->MaterialGroups) { if (!group.TexturePath.empty()) { group.Texture = std::shared_ptr(ResourceManager::Load(group.TexturePath)); } diff --git a/src/Engine/Rendering/RawModel.cpp b/src/Engine/Rendering/RawModel.cpp index 5b0b7260..256346f1 100644 --- a/src/Engine/Rendering/RawModel.cpp +++ b/src/Engine/Rendering/RawModel.cpp @@ -163,7 +163,7 @@ RawModel::RawModel(std::string fileName) material->GetTexture(aiTextureType_SPECULAR, 0, &path, &mapping); matGroup.SpecularMapPath = (boost::filesystem::path(fileName).branch_path() / path.C_Str()).string(); } - TextureGroups.push_back(matGroup); + MaterialGroups.push_back(matGroup); // Bones std::map>> vertexWeights; diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 1f2bac9d..e228b33e 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -84,15 +84,15 @@ void RenderSystem::fillModels(std::list>& jobs, World continue; } - Model* model = ResourceManager::Load<::Model>(resource); + Model* model = ResourceManager::Load<::Model, true>(resource); if (model == nullptr) { model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); } glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, world); - for (auto texGroup : model->TextureGroups) { - std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, texGroup, modelComponent, world)); + for (auto matGroup : model->MaterialGroups()) { + std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, matGroup, modelComponent, world)); jobs.push_back(modelJob); } } diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index a59a6bca..867398bd 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -131,8 +131,8 @@ void Renderer::DrawScreenQuad(GLuint textureToDraw) glBindVertexArray(m_ScreenQuad->VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ScreenQuad->ElementBuffer); - glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->TextureGroups()[0].EndIndex - m_ScreenQuad->TextureGroups()[0].StartIndex +1 - , GL_UNSIGNED_INT, 0, m_ScreenQuad->TextureGroups()[0].StartIndex); + glDrawElementsBaseVertex(GL_TRIANGLES, m_ScreenQuad->MaterialGroups()[0].EndIndex - m_ScreenQuad->MaterialGroups()[0].StartIndex +1 + , GL_UNSIGNED_INT, 0, m_ScreenQuad->MaterialGroups()[0].StartIndex); } void Renderer::InitializeTextures() From f5ea4d513cf176ce2ae8f82f081a7fc740d1c3bb Mon Sep 17 00:00:00 2001 From: William Moberg Date: Fri, 15 Jan 2016 15:25:01 +0100 Subject: [PATCH 6/7] ResourceManager::Load never returns null, throws exceptions instead. --- include/Engine/Core/ResourceManager.h | 118 +++++++++++++------------- src/Engine/Collision/Collision.cpp | 4 +- src/Engine/Core/ResourceManager.cpp | 70 +++++++++------ src/Engine/Rendering/Model.cpp | 6 +- src/Engine/Rendering/RenderSystem.cpp | 16 +++- 5 files changed, 117 insertions(+), 97 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index 9ebf545a..cb78ce13 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -21,6 +21,23 @@ protected: Resource() { } public: + //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. + //Not actually an error, just a message to the ResourceManager. + struct StillLoadingException : public std::exception + { + virtual const char* what() const throw() + { + return "Resource is still loading."; + } + }; + struct FailedLoadingException : public std::exception + { + virtual const char* what() const throw() + { + return "Resource is failed to load."; + } + }; + // Pretend that this is a pure virtual function that you have to implement // FIXME: Why did we do this again instead of just using the constructor? // static Resource* Create(std::string resourceName); @@ -39,16 +56,6 @@ public: class ThreadUnsafeResource : public Resource { friend class ResourceManager; -protected: - //Should be thrown in a Resource's constructor if it cannot complete because another resource is still loading. - //Not actually an error, just a message to the ResourceManager. - struct StillLoadingException : public std::exception - { - virtual const char* what() const throw() - { - return "Resource is still loading."; - } - }; }; /** Singleton resource manager to keep track of and cache any external engine assets */ @@ -75,18 +82,18 @@ public: // TODO: Templateify static bool IsResourceLoaded(std::string resourceType, std::string resourceName); - /** If Async is false: Hot-loads a resource and caches it for future use. - Fairly safe to assume that return value is always a valid pointer, will only return nullptr on error. - + /** Return value should always be a valid pointer, will throw an exception on error. + If the resource has been loaded already, returns a pointer to it. + + If Async is false: Hot-loads a resource, caches it for future use, and returns a pointer to it. If Async is true: If the resource is not loaded yet, starts loading the resource - in the background and returns nullptr immediately. - If the resource has been loaded already, return a pointer to it. + in the background and throws Resource::StillLoadingException immediately. @tparam T Resource type. - @tparam Async Set this to true if the resource should be loaded asyncronously. + @tparam async Set this to true if the resource should be loaded asyncronously. @param resourceName Fully qualified name of the resource to load. */ - template + template static T* Load(std::string resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -101,22 +108,6 @@ public: static void Update(); private: - //Represents a pointer value to signify that a resource haven't failed, but is not fully loaded. - class SpecialResourcePointer - { - public: - SpecialResourcePointer() - : m_Val(new Resource()) - {} - ~SpecialResourcePointer() - { - delete m_Val; - } - //Make this class implicitly convertible to the Resource*. - operator Resource* const() const { return m_Val; } - private: - Resource* const m_Val; - }; //This is a haxy way to make sure that IsMainThread is run when the program starts, so the master thread id is set. struct MasterThreadChecker { @@ -125,7 +116,6 @@ private: ResourceManager::IsMainThread(); } }; - const static SpecialResourcePointer m_StillLoading; const static MasterThreadChecker m_Checker; static std::unordered_map m_CompilerTypenameToResourceType; @@ -135,6 +125,7 @@ private: static std::unordered_map m_ResourceParents; // resource -> parent resource static std::unordered_map, boost::thread> m_LoadingThreads; // (type, name) -> loading thread + static std::unordered_map, std::exception_ptr> m_LoadingThreadExceptions; // (type, name) -> exceptions static boost::recursive_mutex m_Mutex; // TODO: Getters for IDs @@ -150,7 +141,9 @@ private: static unsigned int GetNewResourceID(unsigned int typeID); // Internal: Create a resource and cache it - static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception); + static Resource* cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent); static bool IsMainThread(); }; @@ -169,14 +162,14 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = auto iter = m_CompilerTypenameToResourceType.find(resourceTypename); if (iter == m_CompilerTypenameToResourceType.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceTypename); - return nullptr; + throw Resource::FailedLoadingException(); } std::string resourceType = iter->second; constexpr bool mustNotLoadInThread = std::is_base_of::value; if (mustNotLoadInThread && !IsMainThread()) { LOG_ERROR("Failed to Load \"%s\": ThreadUnsafeResource type \"%s\" load in the constructor of another resource that is loaded asyncronously.", resourceName.c_str(), iter->second.c_str()); - return nullptr; + throw Resource::FailedLoadingException(); } auto cacheKey = std::make_pair(resourceType, resourceName); @@ -185,9 +178,9 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = auto tIt = m_LoadingThreads.find(cacheKey); if (tIt != m_LoadingThreads.end()) { if (async) { - //Return null if the thread is still working. + //Throw StillLoadingException if the thread is still working. if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { - return nullptr; + throw Resource::StillLoadingException(); } //Else we know the thread has completed. } else { @@ -196,42 +189,51 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = } //When the thread is done, delete the thread. m_LoadingThreads.erase(tIt); - //Find the resource that the thread loaded. - it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end()) { - //Threads should not be able to throw StillLoadingException, so no check should be needed. - return static_cast(it->second); - } else { - //If cacheKey does not exist after thread finishes, it failed. - return nullptr; + //Rethrow the thread exception if it threw any. + auto excIt = m_LoadingThreadExceptions.find(cacheKey); + std::exception_ptr exception = excIt->second; + m_LoadingThreadExceptions.erase(excIt); + if (exception) { + std::rethrow_exception(exception); } } //If resource has already been cached and completely loaded. it = m_ResourceCache.find(cacheKey); - if (it != m_ResourceCache.end() && it->second != m_StillLoading) { - return static_cast(it->second); + if (it != m_ResourceCache.end()) { + if (it->second != nullptr) { + return static_cast(it->second); + } else { + //Don't return null on failure, exception instead. + throw Resource::FailedLoadingException(); + } } Resource* res = nullptr; //If resource is not cached.. if (async) { if (mustNotLoadInThread) { - res = createResource(resourceType, resourceName, parent); - if (res != m_StillLoading) { - return static_cast(res); + try { + return static_cast(createResourceThrowing(resourceType, resourceName, parent)); + } catch (const Resource::StillLoadingException&) { + throw; } } else { //Create a thread that loads the resource into cache. - m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent); + m_LoadingThreads[cacheKey] = boost::thread(createResource, resourceType, resourceName, parent, m_LoadingThreadExceptions[cacheKey]); + throw Resource::StillLoadingException(); } - return nullptr; } else { //load and return the resource. - do { - res = createResource(resourceType, resourceName, parent); - } while (res == m_StillLoading); - return static_cast(res); + while (true) { + try { + return static_cast(createResourceThrowing(resourceType, resourceName, parent)); + } catch (const Resource::StillLoadingException&) { + continue; + } catch (const std::exception&) { + throw; + } + } } } diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index c0b2c658..c74af3ad 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -220,7 +220,7 @@ bool attachAABBComponentFromModel(World* world, EntityID id) } ComponentWrapper model = world->GetComponent(id, "Model"); ComponentWrapper collision = world->AttachComponent(id, "AABB"); - Model* modelRes = ResourceManager::Load(model["Resource"]); + Model* modelRes = ResourceManager::Load(model["Resource"]); if (modelRes == nullptr) { return false; } @@ -247,7 +247,7 @@ bool GetEntityBox(World* world, ComponentWrapper& AABBComponent, AABB& outBox) { ComponentWrapper& cTrans = world->GetComponent(AABBComponent.EntityID, "Transform"); ComponentWrapper model = world->GetComponent(AABBComponent.EntityID, "Model"); - Model* modelRes = ResourceManager::Load(model["Resource"]); + Model* modelRes = ResourceManager::Load(model["Resource"]); outBox.CreateFromCenter(AABBComponent["BoxCenter"], AABBComponent["BoxSize"]); glm::vec3 mini = outBox.MinCorner(); glm::vec3 maxi = outBox.MaxCorner(); diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index 30614e5c..ed79e4f7 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -13,8 +13,8 @@ std::unordered_map ResourceManager::m_ResourceTypeIDs std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; std::unordered_map, boost::thread> ResourceManager::m_LoadingThreads; +std::unordered_map, std::exception_ptr> ResourceManager::m_LoadingThreadExceptions; boost::recursive_mutex ResourceManager::m_Mutex; -const ResourceManager::SpecialResourcePointer ResourceManager::m_StillLoading; unsigned int ResourceManager::GetTypeID(std::string resourceType) { @@ -79,47 +79,61 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception) { auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": type not registered", resourceName.c_str(), resourceType.c_str()); - return nullptr; + cacheResource(nullptr, resourceType, resourceName, parent); + //This basically throws an exception. + exception = std::make_exception_ptr(Resource::FailedLoadingException()); return nullptr; } // Call the factory function - Resource* resource = nullptr; try { - resource = facIt->second(resourceName); - } catch (const ThreadUnsafeResource::StillLoadingException&) { - resource = m_StillLoading; + return cacheResource(facIt->second(resourceName), resourceType, resourceName, parent); + } catch (const Resource::StillLoadingException&) { + exception = std::current_exception(); return nullptr; } catch (const std::exception& e) { LOG_ERROR("Failed to load resource \"%s\" of type \"%s\": %s", resourceName.c_str(), resourceType.c_str(), e.what()); + cacheResource(nullptr, resourceType, resourceName, parent); + exception = std::current_exception(); return nullptr; + } +} + + +Resource* ResourceManager::createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent) +{ + std::exception_ptr exception; + Resource* res = createResource(resourceType, resourceName, parent, exception); + if (exception) { + std::rethrow_exception(exception); + } + return res; +} + +Resource* ResourceManager::cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent) +{ + //Lock the mutex immediately, and unlock it when leaving the code block. + boost::lock_guard guard(m_Mutex); + if (resource != nullptr) { + // Store IDs + resource->TypeID = GetTypeID(resourceType); + resource->ResourceID = GetNewResourceID(resource->TypeID); } - { - //Lock the mutex immediately, and unlock it when leaving the code block. - boost::lock_guard guard(m_Mutex); - if (resource != nullptr) { - // Store IDs - resource->TypeID = GetTypeID(resourceType); - resource->ResourceID = GetNewResourceID(resource->TypeID); - } - - // Cache - m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; - m_ResourceFromName[resourceName] = resource; - if (parent != nullptr) { - m_ResourceParents[resource] = parent; - } - - if (!boost::filesystem::is_directory(resourceName)) { - LOG_DEBUG("Adding watch for %s", resourceName.c_str()); - m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); - } + // Cache + m_ResourceCache[std::make_pair(resourceType, resourceName)] = resource; + m_ResourceFromName[resourceName] = resource; + if (parent != nullptr) { + m_ResourceParents[resource] = parent; } - return resource; + //if (!boost::filesystem::is_directory(resourceName)) { + // LOG_DEBUG("Adding watch for %s", resourceName.c_str()); + // m_FileWatcher.AddWatch(resourceName, fileWatcherCallback); + //} + return resource; } bool ResourceManager::IsMainThread() diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index c1afa037..33539a14 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -2,12 +2,8 @@ Model::Model(std::string fileName) { - //Load the RawModel asyncronously, this will be done on a separate thread in the background. + //Try loading the model asyncronously, if it throws any exceptions then let it propagate back to caller. m_RawModel = ResourceManager::Load(fileName); - //If it is null, the model is not done yet, so tell resourceManager to try constructing me again later. - if (m_RawModel == nullptr) { - throw StillLoadingException(); - } for (auto& group : m_RawModel->MaterialGroups) { if (!group.TexturePath.empty()) { diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index e228b33e..6431bde2 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -84,13 +84,21 @@ void RenderSystem::fillModels(std::list>& jobs, World continue; } - Model* model = ResourceManager::Load<::Model, true>(resource); - if (model == nullptr) { - model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); + Model* model; + try { + model = ResourceManager::Load<::Model, true>(resource); + } catch (const Resource::StillLoadingException&) { + //continue; + model = ResourceManager::Load<::Model>("Models/Core/UnitRaptor.obj"); + } catch (const std::exception&) { + try { + model = ResourceManager::Load<::Model>("Models/Core/Error.obj"); + } catch (const std::exception&) { + continue; + } } glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, world); - for (auto matGroup : model->MaterialGroups()) { std::shared_ptr modelJob = std::shared_ptr(new ModelJob(model, m_Camera, modelMatrix, matGroup, modelComponent, world)); jobs.push_back(modelJob); From c09fe4ae558223c063407010860cf4d01bc08081 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Fri, 15 Jan 2016 16:18:39 +0100 Subject: [PATCH 7/7] Added a bool option to disable threads in in the configfile regarding resource loading. --- include/Engine/Core/ResourceManager.h | 16 ++++++++-------- resources/DefaultConfig.ini | 5 ++++- src/Engine/Core/ResourceManager.cpp | 7 ++++--- src/Game/Game.cpp | 1 + 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/Engine/Core/ResourceManager.h b/include/Engine/Core/ResourceManager.h index cb78ce13..15a551e7 100644 --- a/include/Engine/Core/ResourceManager.h +++ b/include/Engine/Core/ResourceManager.h @@ -65,6 +65,7 @@ private: ResourceManager(); public: + static bool UseThreading; /*static ResourceManager& Instance() { static ResourceManager s; @@ -94,7 +95,7 @@ public: @param resourceName Fully qualified name of the resource to load. */ template - static T* Load(std::string resourceName, Resource* parent = nullptr); + static T* Load(const std::string& resourceName, Resource* parent = nullptr); /** Reloads an already loaded resource, keeping its resource ID intact. @@ -141,9 +142,9 @@ private: static unsigned int GetNewResourceID(unsigned int typeID); // Internal: Create a resource and cache it - static Resource* createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent); - static Resource* createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception); - static Resource* cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent); + static Resource* createResourceThrowing(const std::string& resourceType, const std::string& resourceName, Resource* parent); + static Resource* createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception); + static Resource* cacheResource(Resource* resource, const std::string& resourceType, const std::string& resourceName, Resource* parent); static bool IsMainThread(); }; @@ -156,7 +157,7 @@ void ResourceManager::RegisterType(std::string typeName) } template -static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = nullptr */) +static T* ResourceManager::Load(const std::string& resourceName, Resource* parent /* = nullptr */) { auto resourceTypename = typeid(T).name(); auto iter = m_CompilerTypenameToResourceType.find(resourceTypename); @@ -176,7 +177,7 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = decltype(m_ResourceCache)::iterator it; //If a thread has already been launched to load this resource. auto tIt = m_LoadingThreads.find(cacheKey); - if (tIt != m_LoadingThreads.end()) { + if (UseThreading && tIt != m_LoadingThreads.end()) { if (async) { //Throw StillLoadingException if the thread is still working. if (!tIt->second.try_join_for(boost::chrono::nanoseconds(1))) { @@ -209,9 +210,8 @@ static T* ResourceManager::Load(std::string resourceName, Resource* parent /* = } } - Resource* res = nullptr; //If resource is not cached.. - if (async) { + if (UseThreading && async) { if (mustNotLoadInThread) { try { return static_cast(createResourceThrowing(resourceType, resourceName, parent)); diff --git a/resources/DefaultConfig.ini b/resources/DefaultConfig.ini index 3ab402ad..d84b15bb 100644 --- a/resources/DefaultConfig.ini +++ b/resources/DefaultConfig.ini @@ -16,4 +16,7 @@ StartNetwork=false IsServer=false Name=Bob Address=127.0.0.1 -Port=13 \ No newline at end of file +Port=13 + +[Multithreading] +ResourceLoading=true diff --git a/src/Engine/Core/ResourceManager.cpp b/src/Engine/Core/ResourceManager.cpp index ed79e4f7..54228efa 100644 --- a/src/Engine/Core/ResourceManager.cpp +++ b/src/Engine/Core/ResourceManager.cpp @@ -9,6 +9,7 @@ std::unordered_map, Resource*> ResourceManag std::unordered_map ResourceManager::m_ResourceFromName; std::unordered_map ResourceManager::m_ResourceParents; unsigned int ResourceManager::m_CurrentResourceTypeID = 0; +bool ResourceManager::UseThreading = false; std::unordered_map ResourceManager::m_ResourceTypeIDs; std::unordered_map ResourceManager::m_ResourceCount; FileWatcher ResourceManager::m_FileWatcher; @@ -79,7 +80,7 @@ void ResourceManager::Update() m_FileWatcher.Check(); } -Resource* ResourceManager::createResource(std::string resourceType, std::string resourceName, Resource* parent, std::exception_ptr& exception) +Resource* ResourceManager::createResource(const std::string& resourceType, const std::string& resourceName, Resource* parent, std::exception_ptr& exception) { auto facIt = m_FactoryFunctions.find(resourceType); if (facIt == m_FactoryFunctions.end()) { @@ -102,7 +103,7 @@ Resource* ResourceManager::createResource(std::string resourceType, std::string } -Resource* ResourceManager::createResourceThrowing(std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::createResourceThrowing(const std::string& resourceType, const std::string& resourceName, Resource* parent) { std::exception_ptr exception; Resource* res = createResource(resourceType, resourceName, parent, exception); @@ -112,7 +113,7 @@ Resource* ResourceManager::createResourceThrowing(std::string resourceType, std: return res; } -Resource* ResourceManager::cacheResource(Resource* resource, std::string resourceType, std::string resourceName, Resource* parent) +Resource* ResourceManager::cacheResource(Resource* resource, const std::string& resourceType, const std::string& resourceName, Resource* parent) { //Lock the mutex immediately, and unlock it when leaving the code block. boost::lock_guard guard(m_Mutex); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index efa773f5..e6589676 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -14,6 +14,7 @@ Game::Game(int argc, char* argv[]) ResourceManager::RegisterType("EntityFile"); m_Config = ResourceManager::Load("Config.ini"); + ResourceManager::UseThreading = m_Config->Get("Multithreading.ResourceLoading", true); LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get("Debug.LogLevel", 1)); // Create the core event broker