mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-03-28 08:37:12 +00:00
Implement generic caching system
This commit is contained in:
parent
ddcdbccd84
commit
2ed05a5195
@ -12,16 +12,15 @@ namespace MWRender
|
|||||||
{
|
{
|
||||||
|
|
||||||
LandManager::LandManager(int loadFlags)
|
LandManager::LandManager(int loadFlags)
|
||||||
: ResourceManager(nullptr)
|
: GenericResourceManager<std::pair<int, int> >(nullptr)
|
||||||
, mLoadFlags(loadFlags)
|
, mLoadFlags(loadFlags)
|
||||||
{
|
{
|
||||||
|
mCache = new CacheType;
|
||||||
}
|
}
|
||||||
|
|
||||||
osg::ref_ptr<ESMTerrain::LandObject> LandManager::getLand(int x, int y)
|
osg::ref_ptr<ESMTerrain::LandObject> LandManager::getLand(int x, int y)
|
||||||
{
|
{
|
||||||
std::string idstr = std::to_string(x) + " " + std::to_string(y);
|
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(std::make_pair(x,y));
|
||||||
|
|
||||||
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(idstr);
|
|
||||||
if (obj)
|
if (obj)
|
||||||
return static_cast<ESMTerrain::LandObject*>(obj.get());
|
return static_cast<ESMTerrain::LandObject*>(obj.get());
|
||||||
else
|
else
|
||||||
@ -30,7 +29,7 @@ osg::ref_ptr<ESMTerrain::LandObject> LandManager::getLand(int x, int y)
|
|||||||
if (!land)
|
if (!land)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
osg::ref_ptr<ESMTerrain::LandObject> landObj (new ESMTerrain::LandObject(land, mLoadFlags));
|
osg::ref_ptr<ESMTerrain::LandObject> landObj (new ESMTerrain::LandObject(land, mLoadFlags));
|
||||||
mCache->addEntryToObjectCache(idstr, landObj.get());
|
mCache->addEntryToObjectCache(std::make_pair(x,y), landObj.get());
|
||||||
return landObj;
|
return landObj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <osg/Object>
|
#include <osg/Object>
|
||||||
|
|
||||||
|
#include <components/resource/objectcache.hpp>
|
||||||
#include <components/resource/resourcemanager.hpp>
|
#include <components/resource/resourcemanager.hpp>
|
||||||
#include <components/esmterrain/storage.hpp>
|
#include <components/esmterrain/storage.hpp>
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ namespace ESM
|
|||||||
namespace MWRender
|
namespace MWRender
|
||||||
{
|
{
|
||||||
|
|
||||||
class LandManager : public Resource::ResourceManager
|
class LandManager : public Resource::GenericResourceManager<std::pair<int, int> >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LandManager(int loadFlags);
|
LandManager(int loadFlags);
|
||||||
|
@ -1,165 +0,0 @@
|
|||||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
||||||
*
|
|
||||||
* This library is open source and may be redistributed and/or modified under
|
|
||||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
||||||
* (at your option) any later version. The full license is in LICENSE file
|
|
||||||
* included with this distribution, and on the openscenegraph.org website.
|
|
||||||
*
|
|
||||||
* This library is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* OpenSceneGraph Public License for more details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "objectcache.hpp"
|
|
||||||
|
|
||||||
#include <osg/Object>
|
|
||||||
#include <osg/Node>
|
|
||||||
|
|
||||||
namespace Resource
|
|
||||||
{
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
//
|
|
||||||
// ObjectCache
|
|
||||||
//
|
|
||||||
ObjectCache::ObjectCache():
|
|
||||||
osg::Referenced(true)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
ObjectCache::~ObjectCache()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectCache::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp)
|
|
||||||
{
|
|
||||||
if (!object)
|
|
||||||
{
|
|
||||||
OSG_ALWAYS << " trying to add NULL object to cache for " << filename << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
_objectCache[filename]=ObjectTimeStampPair(object,timestamp);
|
|
||||||
}
|
|
||||||
|
|
||||||
osg::ref_ptr<osg::Object> ObjectCache::getRefFromObjectCache(const std::string& fileName)
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
|
||||||
if (itr!=_objectCache.end())
|
|
||||||
{
|
|
||||||
return itr->second.first;
|
|
||||||
}
|
|
||||||
else return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ObjectCache::checkInObjectCache(const std::string &fileName, double timeStamp)
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
|
||||||
if (itr!=_objectCache.end())
|
|
||||||
{
|
|
||||||
itr->second.second = timeStamp;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectCache::updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime)
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
|
|
||||||
// look for objects with external references and update their time stamp.
|
|
||||||
for(ObjectCacheMap::iterator itr=_objectCache.begin();
|
|
||||||
itr!=_objectCache.end();
|
|
||||||
++itr)
|
|
||||||
{
|
|
||||||
// If ref count is greater than 1, the object has an external reference.
|
|
||||||
// If the timestamp is yet to be initialized, it needs to be updated too.
|
|
||||||
if (itr->second.first->referenceCount()>1 || itr->second.second == 0.0)
|
|
||||||
{
|
|
||||||
// So update it.
|
|
||||||
itr->second.second = referenceTime;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectCache::removeExpiredObjectsInCache(double expiryTime)
|
|
||||||
{
|
|
||||||
std::vector<osg::ref_ptr<osg::Object> > objectsToRemove;
|
|
||||||
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
|
|
||||||
// Remove expired entries from object cache
|
|
||||||
ObjectCacheMap::iterator oitr = _objectCache.begin();
|
|
||||||
while(oitr != _objectCache.end())
|
|
||||||
{
|
|
||||||
if (oitr->second.second<=expiryTime)
|
|
||||||
{
|
|
||||||
objectsToRemove.push_back(oitr->second.first);
|
|
||||||
_objectCache.erase(oitr++);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
++oitr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// note, actual unref happens outside of the lock
|
|
||||||
objectsToRemove.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectCache::removeFromObjectCache(const std::string& fileName)
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
ObjectCacheMap::iterator itr = _objectCache.find(fileName);
|
|
||||||
if (itr!=_objectCache.end()) _objectCache.erase(itr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectCache::clear()
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
_objectCache.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectCache::releaseGLObjects(osg::State* state)
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
|
|
||||||
for(ObjectCacheMap::iterator itr = _objectCache.begin();
|
|
||||||
itr != _objectCache.end();
|
|
||||||
++itr)
|
|
||||||
{
|
|
||||||
osg::Object* object = itr->second.first.get();
|
|
||||||
object->releaseGLObjects(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjectCache::accept(osg::NodeVisitor &nv)
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
|
|
||||||
for(ObjectCacheMap::iterator itr = _objectCache.begin();
|
|
||||||
itr != _objectCache.end();
|
|
||||||
++itr)
|
|
||||||
{
|
|
||||||
osg::Object* object = itr->second.first.get();
|
|
||||||
if (object)
|
|
||||||
{
|
|
||||||
osg::Node* node = dynamic_cast<osg::Node*>(object);
|
|
||||||
if (node)
|
|
||||||
node->accept(nv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int ObjectCache::getCacheSize() const
|
|
||||||
{
|
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
|
||||||
return _objectCache.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,5 +1,8 @@
|
|||||||
// Resource ObjectCache for OpenMW, forked from osgDB ObjectCache by Robert Osfield, see copyright notice below.
|
// Resource ObjectCache for OpenMW, forked from osgDB ObjectCache by Robert Osfield, see copyright notice below.
|
||||||
// The main change from the upstream version is that removeExpiredObjectsInCache no longer keeps a lock while the unref happens.
|
// Changes:
|
||||||
|
// - removeExpiredObjectsInCache no longer keeps a lock while the unref happens.
|
||||||
|
// - template allows customized KeyType.
|
||||||
|
// - objects with uninitialized time stamp are not removed.
|
||||||
|
|
||||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
||||||
*
|
*
|
||||||
@ -19,6 +22,7 @@
|
|||||||
|
|
||||||
#include <osg/Referenced>
|
#include <osg/Referenced>
|
||||||
#include <osg/ref_ptr>
|
#include <osg/ref_ptr>
|
||||||
|
#include <osg/Node>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -32,11 +36,13 @@ namespace osg
|
|||||||
|
|
||||||
namespace Resource {
|
namespace Resource {
|
||||||
|
|
||||||
class ObjectCache : public osg::Referenced
|
template <typename KeyType>
|
||||||
|
class GenericObjectCache : public osg::Referenced
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
ObjectCache();
|
GenericObjectCache()
|
||||||
|
: osg::Referenced(true) {}
|
||||||
|
|
||||||
/** For each object in the cache which has an reference count greater than 1
|
/** For each object in the cache which has an reference count greater than 1
|
||||||
* (and therefore referenced by elsewhere in the application) set the time stamp
|
* (and therefore referenced by elsewhere in the application) set the time stamp
|
||||||
@ -44,59 +50,149 @@ class ObjectCache : public osg::Referenced
|
|||||||
* This would typically be called once per frame by applications which are doing database paging,
|
* This would typically be called once per frame by applications which are doing database paging,
|
||||||
* and need to prune objects that are no longer required.
|
* and need to prune objects that are no longer required.
|
||||||
* The time used should be taken from the FrameStamp::getReferenceTime().*/
|
* The time used should be taken from the FrameStamp::getReferenceTime().*/
|
||||||
void updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime);
|
void updateTimeStampOfObjectsInCacheWithExternalReferences(double referenceTime)
|
||||||
|
{
|
||||||
|
// look for objects with external references and update their time stamp.
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
for(typename ObjectCacheMap::iterator itr=_objectCache.begin(); itr!=_objectCache.end(); ++itr)
|
||||||
|
{
|
||||||
|
// If ref count is greater than 1, the object has an external reference.
|
||||||
|
// If the timestamp is yet to be initialized, it needs to be updated too.
|
||||||
|
if (itr->second.first->referenceCount()>1 || itr->second.second == 0.0)
|
||||||
|
itr->second.second = referenceTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Removed object in the cache which have a time stamp at or before the specified expiry time.
|
/** Removed object in the cache which have a time stamp at or before the specified expiry time.
|
||||||
* This would typically be called once per frame by applications which are doing database paging,
|
* This would typically be called once per frame by applications which are doing database paging,
|
||||||
* and need to prune objects that are no longer required, and called after the a called
|
* and need to prune objects that are no longer required, and called after the a called
|
||||||
* after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(expirtyTime).*/
|
* after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(expirtyTime).*/
|
||||||
void removeExpiredObjectsInCache(double expiryTime);
|
void removeExpiredObjectsInCache(double expiryTime)
|
||||||
|
{
|
||||||
|
std::vector<osg::ref_ptr<osg::Object> > objectsToRemove;
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
// Remove expired entries from object cache
|
||||||
|
typename ObjectCacheMap::iterator oitr = _objectCache.begin();
|
||||||
|
while(oitr != _objectCache.end())
|
||||||
|
{
|
||||||
|
if (oitr->second.second<=expiryTime)
|
||||||
|
{
|
||||||
|
objectsToRemove.push_back(oitr->second.first);
|
||||||
|
_objectCache.erase(oitr++);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
++oitr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// note, actual unref happens outside of the lock
|
||||||
|
objectsToRemove.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/** Remove all objects in the cache regardless of having external references or expiry times.*/
|
/** Remove all objects in the cache regardless of having external references or expiry times.*/
|
||||||
void clear();
|
void clear()
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
_objectCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/** Add a filename,object,timestamp triple to the Registry::ObjectCache.*/
|
/** Add a key,object,timestamp triple to the Registry::ObjectCache.*/
|
||||||
void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0);
|
void addEntryToObjectCache(const KeyType& key, osg::Object* object, double timestamp = 0.0)
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
_objectCache[key]=ObjectTimeStampPair(object,timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
/** Remove Object from cache.*/
|
/** Remove Object from cache.*/
|
||||||
void removeFromObjectCache(const std::string& fileName);
|
void removeFromObjectCache(const KeyType& key)
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
||||||
|
if (itr!=_objectCache.end()) _objectCache.erase(itr);
|
||||||
|
}
|
||||||
|
|
||||||
/** Get an ref_ptr<Object> from the object cache*/
|
/** Get an ref_ptr<Object> from the object cache*/
|
||||||
osg::ref_ptr<osg::Object> getRefFromObjectCache(const std::string& fileName);
|
osg::ref_ptr<osg::Object> getRefFromObjectCache(const KeyType& key)
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
||||||
|
if (itr!=_objectCache.end())
|
||||||
|
return itr->second.first;
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/** Check if an object is in the cache, and if it is, update its usage time stamp. */
|
/** Check if an object is in the cache, and if it is, update its usage time stamp. */
|
||||||
bool checkInObjectCache(const std::string& fileName, double timeStamp);
|
bool checkInObjectCache(const KeyType& key, double timeStamp)
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
typename ObjectCacheMap::iterator itr = _objectCache.find(key);
|
||||||
|
if (itr!=_objectCache.end())
|
||||||
|
{
|
||||||
|
itr->second.second = timeStamp;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
/** call releaseGLObjects on all objects attached to the object cache.*/
|
/** call releaseGLObjects on all objects attached to the object cache.*/
|
||||||
void releaseGLObjects(osg::State* state);
|
void releaseGLObjects(osg::State* state)
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
for(typename ObjectCacheMap::iterator itr = _objectCache.begin(); itr != _objectCache.end(); ++itr)
|
||||||
|
{
|
||||||
|
osg::Object* object = itr->second.first.get();
|
||||||
|
object->releaseGLObjects(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** call node->accept(nv); for all nodes in the objectCache. */
|
/** call node->accept(nv); for all nodes in the objectCache. */
|
||||||
void accept(osg::NodeVisitor& nv);
|
void accept(osg::NodeVisitor& nv)
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
for(typename ObjectCacheMap::iterator itr = _objectCache.begin(); itr != _objectCache.end(); ++itr)
|
||||||
|
{
|
||||||
|
osg::Object* object = itr->second.first.get();
|
||||||
|
if (object)
|
||||||
|
{
|
||||||
|
osg::Node* node = dynamic_cast<osg::Node*>(object);
|
||||||
|
if (node)
|
||||||
|
node->accept(nv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** call operator()(osg::Object*) for each object in the cache. */
|
/** call operator()(osg::Object*) for each object in the cache. */
|
||||||
template <class Functor>
|
template <class Functor>
|
||||||
void call(Functor& f)
|
void call(Functor& f)
|
||||||
{
|
{
|
||||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
for (ObjectCacheMap::iterator it = _objectCache.begin(); it != _objectCache.end(); ++it)
|
for (typename ObjectCacheMap::iterator it = _objectCache.begin(); it != _objectCache.end(); ++it)
|
||||||
f(it->second.first.get());
|
f(it->second.first.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the number of objects in the cache. */
|
/** Get the number of objects in the cache. */
|
||||||
unsigned int getCacheSize() const;
|
unsigned int getCacheSize() const
|
||||||
|
{
|
||||||
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||||
|
return _objectCache.size();
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
virtual ~ObjectCache();
|
virtual ~GenericObjectCache() {}
|
||||||
|
|
||||||
typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair;
|
typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair;
|
||||||
typedef std::map<std::string, ObjectTimeStampPair > ObjectCacheMap;
|
typedef std::map<KeyType, ObjectTimeStampPair > ObjectCacheMap;
|
||||||
|
|
||||||
ObjectCacheMap _objectCache;
|
ObjectCacheMap _objectCache;
|
||||||
mutable OpenThreads::Mutex _objectCacheMutex;
|
mutable OpenThreads::Mutex _objectCacheMutex;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ObjectCache : public GenericObjectCache<std::string>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
#include "resourcemanager.hpp"
|
|
||||||
|
|
||||||
#include "objectcache.hpp"
|
|
||||||
|
|
||||||
namespace Resource
|
|
||||||
{
|
|
||||||
|
|
||||||
ResourceManager::ResourceManager(const VFS::Manager *vfs)
|
|
||||||
: mVFS(vfs)
|
|
||||||
, mCache(new Resource::ObjectCache)
|
|
||||||
, mExpiryDelay(0.0)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
ResourceManager::~ResourceManager()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void ResourceManager::updateCache(double referenceTime)
|
|
||||||
{
|
|
||||||
mCache->updateTimeStampOfObjectsInCacheWithExternalReferences(referenceTime);
|
|
||||||
mCache->removeExpiredObjectsInCache(referenceTime - mExpiryDelay);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ResourceManager::clearCache()
|
|
||||||
{
|
|
||||||
mCache->clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ResourceManager::setExpiryDelay(double expiryDelay)
|
|
||||||
{
|
|
||||||
mExpiryDelay = expiryDelay;
|
|
||||||
}
|
|
||||||
|
|
||||||
const VFS::Manager* ResourceManager::getVFS() const
|
|
||||||
{
|
|
||||||
return mVFS;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ResourceManager::releaseGLObjects(osg::State *state)
|
|
||||||
{
|
|
||||||
mCache->releaseGLObjects(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <osg/ref_ptr>
|
#include <osg/ref_ptr>
|
||||||
|
|
||||||
|
#include "objectcache.hpp"
|
||||||
|
|
||||||
namespace VFS
|
namespace VFS
|
||||||
{
|
{
|
||||||
class Manager;
|
class Manager;
|
||||||
@ -16,37 +18,67 @@ namespace osg
|
|||||||
|
|
||||||
namespace Resource
|
namespace Resource
|
||||||
{
|
{
|
||||||
class ObjectCache;
|
|
||||||
|
class BaseResourceManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~BaseResourceManager() {}
|
||||||
|
virtual void updateCache(double referenceTime) {}
|
||||||
|
virtual void clearCache() {}
|
||||||
|
virtual void setExpiryDelay(double expiryDelay) {}
|
||||||
|
virtual void reportStats(unsigned int frameNumber, osg::Stats* stats) const {}
|
||||||
|
virtual void releaseGLObjects(osg::State* state) {}
|
||||||
|
};
|
||||||
|
|
||||||
/// @brief Base class for managers that require a virtual file system and object cache.
|
/// @brief Base class for managers that require a virtual file system and object cache.
|
||||||
/// @par This base class implements clearing of the cache, but populating it and what it's used for is up to the individual sub classes.
|
/// @par This base class implements clearing of the cache, but populating it and what it's used for is up to the individual sub classes.
|
||||||
class ResourceManager
|
template <class KeyType>
|
||||||
|
class GenericResourceManager : public BaseResourceManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ResourceManager(const VFS::Manager* vfs);
|
typedef GenericObjectCache<KeyType> CacheType;
|
||||||
virtual ~ResourceManager();
|
|
||||||
|
GenericResourceManager(const VFS::Manager* vfs)
|
||||||
|
: mVFS(vfs)
|
||||||
|
, mCache(new CacheType)
|
||||||
|
, mExpiryDelay(0.0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~GenericResourceManager() {}
|
||||||
|
|
||||||
/// Clear cache entries that have not been referenced for longer than expiryDelay.
|
/// Clear cache entries that have not been referenced for longer than expiryDelay.
|
||||||
virtual void updateCache(double referenceTime);
|
virtual void updateCache(double referenceTime)
|
||||||
|
{
|
||||||
|
mCache->updateTimeStampOfObjectsInCacheWithExternalReferences(referenceTime);
|
||||||
|
mCache->removeExpiredObjectsInCache(referenceTime - mExpiryDelay);
|
||||||
|
}
|
||||||
|
|
||||||
/// Clear all cache entries.
|
/// Clear all cache entries.
|
||||||
virtual void clearCache();
|
virtual void clearCache() { mCache->clear(); }
|
||||||
|
|
||||||
/// How long to keep objects in cache after no longer being referenced.
|
/// How long to keep objects in cache after no longer being referenced.
|
||||||
void setExpiryDelay (double expiryDelay);
|
void setExpiryDelay (double expiryDelay) { mExpiryDelay = expiryDelay; }
|
||||||
|
|
||||||
const VFS::Manager* getVFS() const;
|
const VFS::Manager* getVFS() const { return mVFS; }
|
||||||
|
|
||||||
virtual void reportStats(unsigned int frameNumber, osg::Stats* stats) const {}
|
virtual void reportStats(unsigned int frameNumber, osg::Stats* stats) const {}
|
||||||
|
|
||||||
virtual void releaseGLObjects(osg::State* state);
|
virtual void releaseGLObjects(osg::State* state) { mCache->releaseGLObjects(state); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const VFS::Manager* mVFS;
|
const VFS::Manager* mVFS;
|
||||||
osg::ref_ptr<Resource::ObjectCache> mCache;
|
osg::ref_ptr<CacheType> mCache;
|
||||||
double mExpiryDelay;
|
double mExpiryDelay;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceManager : public GenericResourceManager<std::string>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ResourceManager(const VFS::Manager* vfs) : GenericResourceManager<std::string>(vfs) {}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -54,7 +54,7 @@ namespace Resource
|
|||||||
|
|
||||||
void ResourceSystem::setExpiryDelay(double expiryDelay)
|
void ResourceSystem::setExpiryDelay(double expiryDelay)
|
||||||
{
|
{
|
||||||
for (std::vector<ResourceManager*>::iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
for (std::vector<BaseResourceManager*>::iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
||||||
(*it)->setExpiryDelay(expiryDelay);
|
(*it)->setExpiryDelay(expiryDelay);
|
||||||
|
|
||||||
// NIF files aren't needed any more once the converted objects are cached in SceneManager / BulletShapeManager,
|
// NIF files aren't needed any more once the converted objects are cached in SceneManager / BulletShapeManager,
|
||||||
@ -64,24 +64,24 @@ namespace Resource
|
|||||||
|
|
||||||
void ResourceSystem::updateCache(double referenceTime)
|
void ResourceSystem::updateCache(double referenceTime)
|
||||||
{
|
{
|
||||||
for (std::vector<ResourceManager*>::iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
for (std::vector<BaseResourceManager*>::iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
||||||
(*it)->updateCache(referenceTime);
|
(*it)->updateCache(referenceTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResourceSystem::clearCache()
|
void ResourceSystem::clearCache()
|
||||||
{
|
{
|
||||||
for (std::vector<ResourceManager*>::iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
for (std::vector<BaseResourceManager*>::iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
||||||
(*it)->clearCache();
|
(*it)->clearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResourceSystem::addResourceManager(ResourceManager *resourceMgr)
|
void ResourceSystem::addResourceManager(BaseResourceManager *resourceMgr)
|
||||||
{
|
{
|
||||||
mResourceManagers.push_back(resourceMgr);
|
mResourceManagers.push_back(resourceMgr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResourceSystem::removeResourceManager(ResourceManager *resourceMgr)
|
void ResourceSystem::removeResourceManager(BaseResourceManager *resourceMgr)
|
||||||
{
|
{
|
||||||
std::vector<ResourceManager*>::iterator found = std::find(mResourceManagers.begin(), mResourceManagers.end(), resourceMgr);
|
std::vector<BaseResourceManager*>::iterator found = std::find(mResourceManagers.begin(), mResourceManagers.end(), resourceMgr);
|
||||||
if (found != mResourceManagers.end())
|
if (found != mResourceManagers.end())
|
||||||
mResourceManagers.erase(found);
|
mResourceManagers.erase(found);
|
||||||
}
|
}
|
||||||
@ -93,13 +93,13 @@ namespace Resource
|
|||||||
|
|
||||||
void ResourceSystem::reportStats(unsigned int frameNumber, osg::Stats *stats) const
|
void ResourceSystem::reportStats(unsigned int frameNumber, osg::Stats *stats) const
|
||||||
{
|
{
|
||||||
for (std::vector<ResourceManager*>::const_iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
for (std::vector<BaseResourceManager*>::const_iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
||||||
(*it)->reportStats(frameNumber, stats);
|
(*it)->reportStats(frameNumber, stats);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResourceSystem::releaseGLObjects(osg::State *state)
|
void ResourceSystem::releaseGLObjects(osg::State *state)
|
||||||
{
|
{
|
||||||
for (std::vector<ResourceManager*>::const_iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
for (std::vector<BaseResourceManager*>::const_iterator it = mResourceManagers.begin(); it != mResourceManagers.end(); ++it)
|
||||||
(*it)->releaseGLObjects(state);
|
(*it)->releaseGLObjects(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ namespace Resource
|
|||||||
class ImageManager;
|
class ImageManager;
|
||||||
class NifFileManager;
|
class NifFileManager;
|
||||||
class KeyframeManager;
|
class KeyframeManager;
|
||||||
class ResourceManager;
|
class BaseResourceManager;
|
||||||
|
|
||||||
/// @brief Wrapper class that constructs and provides access to the most commonly used resource subsystems.
|
/// @brief Wrapper class that constructs and provides access to the most commonly used resource subsystems.
|
||||||
/// @par Resource subsystems can be used with multiple OpenGL contexts, just like the OSG equivalents, but
|
/// @par Resource subsystems can be used with multiple OpenGL contexts, just like the OSG equivalents, but
|
||||||
@ -48,10 +48,10 @@ namespace Resource
|
|||||||
|
|
||||||
/// Add this ResourceManager to be handled by the ResourceSystem.
|
/// Add this ResourceManager to be handled by the ResourceSystem.
|
||||||
/// @note Does not transfer ownership.
|
/// @note Does not transfer ownership.
|
||||||
void addResourceManager(ResourceManager* resourceMgr);
|
void addResourceManager(BaseResourceManager* resourceMgr);
|
||||||
/// @note Do nothing if resourceMgr does not exist.
|
/// @note Do nothing if resourceMgr does not exist.
|
||||||
/// @note Does not delete resourceMgr.
|
/// @note Does not delete resourceMgr.
|
||||||
void removeResourceManager(ResourceManager* resourceMgr);
|
void removeResourceManager(BaseResourceManager* resourceMgr);
|
||||||
|
|
||||||
/// How long to keep objects in cache after no longer being referenced.
|
/// How long to keep objects in cache after no longer being referenced.
|
||||||
void setExpiryDelay(double expiryDelay);
|
void setExpiryDelay(double expiryDelay);
|
||||||
@ -72,7 +72,7 @@ namespace Resource
|
|||||||
|
|
||||||
// Store the base classes separately to get convenient access to the common interface
|
// Store the base classes separately to get convenient access to the common interface
|
||||||
// Here users can register their own resourcemanager as well
|
// Here users can register their own resourcemanager as well
|
||||||
std::vector<ResourceManager*> mResourceManagers;
|
std::vector<BaseResourceManager*> mResourceManagers;
|
||||||
|
|
||||||
const VFS::Manager* mVFS;
|
const VFS::Manager* mVFS;
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ namespace Terrain
|
|||||||
{
|
{
|
||||||
|
|
||||||
ChunkManager::ChunkManager(Storage *storage, Resource::SceneManager *sceneMgr, TextureManager* textureManager, CompositeMapRenderer* renderer)
|
ChunkManager::ChunkManager(Storage *storage, Resource::SceneManager *sceneMgr, TextureManager* textureManager, CompositeMapRenderer* renderer)
|
||||||
: ResourceManager(nullptr)
|
: GenericResourceManager<ChunkId>(nullptr)
|
||||||
, mStorage(storage)
|
, mStorage(storage)
|
||||||
, mSceneManager(sceneMgr)
|
, mSceneManager(sceneMgr)
|
||||||
, mTextureManager(textureManager)
|
, mTextureManager(textureManager)
|
||||||
@ -35,12 +35,9 @@ ChunkManager::ChunkManager(Storage *storage, Resource::SceneManager *sceneMgr, T
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
osg::ref_ptr<osg::Node> ChunkManager::getChunk(float size, const osg::Vec2f ¢er, int lod, unsigned int lodFlags)
|
osg::ref_ptr<osg::Node> ChunkManager::getChunk(float size, const osg::Vec2f ¢er, unsigned char lod, unsigned int lodFlags)
|
||||||
{
|
{
|
||||||
std::ostringstream stream;
|
ChunkId id = std::make_tuple(center, lod, lodFlags);
|
||||||
stream << size << " " << center.x() << " " << center.y() << " " << lod << " " << lodFlags;
|
|
||||||
std::string id = stream.str();
|
|
||||||
|
|
||||||
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(id);
|
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(id);
|
||||||
if (obj)
|
if (obj)
|
||||||
return obj->asNode();
|
return obj->asNode();
|
||||||
@ -59,14 +56,14 @@ void ChunkManager::reportStats(unsigned int frameNumber, osg::Stats *stats) cons
|
|||||||
|
|
||||||
void ChunkManager::clearCache()
|
void ChunkManager::clearCache()
|
||||||
{
|
{
|
||||||
ResourceManager::clearCache();
|
GenericResourceManager<ChunkId>::clearCache();
|
||||||
|
|
||||||
mBufferCache.clearCache();
|
mBufferCache.clearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChunkManager::releaseGLObjects(osg::State *state)
|
void ChunkManager::releaseGLObjects(osg::State *state)
|
||||||
{
|
{
|
||||||
ResourceManager::releaseGLObjects(state);
|
GenericResourceManager<ChunkId>::releaseGLObjects(state);
|
||||||
mBufferCache.releaseGLObjects(state);
|
mBufferCache.releaseGLObjects(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,7 +161,7 @@ std::vector<osg::ref_ptr<osg::StateSet> > ChunkManager::createPasses(float chunk
|
|||||||
return ::Terrain::createPasses(useShaders, &mSceneManager->getShaderManager(), layers, blendmapTextures, blendmapScale, blendmapScale);
|
return ::Terrain::createPasses(useShaders, &mSceneManager->getShaderManager(), layers, blendmapTextures, blendmapScale, blendmapScale);
|
||||||
}
|
}
|
||||||
|
|
||||||
osg::ref_ptr<osg::Node> ChunkManager::createChunk(float chunkSize, const osg::Vec2f &chunkCenter, int lod, unsigned int lodFlags)
|
osg::ref_ptr<osg::Node> ChunkManager::createChunk(float chunkSize, const osg::Vec2f &chunkCenter, unsigned char lod, unsigned int lodFlags)
|
||||||
{
|
{
|
||||||
osg::Vec2f worldCenter = chunkCenter*mStorage->getCellWorldSize();
|
osg::Vec2f worldCenter = chunkCenter*mStorage->getCellWorldSize();
|
||||||
osg::ref_ptr<SceneUtil::PositionAttitudeTransform> transform (new SceneUtil::PositionAttitudeTransform);
|
osg::ref_ptr<SceneUtil::PositionAttitudeTransform> transform (new SceneUtil::PositionAttitudeTransform);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#ifndef OPENMW_COMPONENTS_TERRAIN_CHUNKMANAGER_H
|
#ifndef OPENMW_COMPONENTS_TERRAIN_CHUNKMANAGER_H
|
||||||
#define OPENMW_COMPONENTS_TERRAIN_CHUNKMANAGER_H
|
#define OPENMW_COMPONENTS_TERRAIN_CHUNKMANAGER_H
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
#include <components/resource/resourcemanager.hpp>
|
#include <components/resource/resourcemanager.hpp>
|
||||||
|
|
||||||
#include "buffercache.hpp"
|
#include "buffercache.hpp"
|
||||||
@ -24,13 +26,15 @@ namespace Terrain
|
|||||||
class Storage;
|
class Storage;
|
||||||
class CompositeMap;
|
class CompositeMap;
|
||||||
|
|
||||||
|
typedef std::tuple<osg::Vec2f, unsigned char, unsigned int> ChunkId; // Center, Lod, Lod Flags
|
||||||
|
|
||||||
/// @brief Handles loading and caching of terrain chunks
|
/// @brief Handles loading and caching of terrain chunks
|
||||||
class ChunkManager : public Resource::ResourceManager
|
class ChunkManager : public Resource::GenericResourceManager<ChunkId>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ChunkManager(Storage* storage, Resource::SceneManager* sceneMgr, TextureManager* textureManager, CompositeMapRenderer* renderer);
|
ChunkManager(Storage* storage, Resource::SceneManager* sceneMgr, TextureManager* textureManager, CompositeMapRenderer* renderer);
|
||||||
|
|
||||||
osg::ref_ptr<osg::Node> getChunk(float size, const osg::Vec2f& center, int lod, unsigned int lodFlags);
|
osg::ref_ptr<osg::Node> getChunk(float size, const osg::Vec2f& center, unsigned char lod, unsigned int lodFlags);
|
||||||
|
|
||||||
void setCullingActive(bool active) { mCullingActive = active; }
|
void setCullingActive(bool active) { mCullingActive = active; }
|
||||||
void setCompositeMapSize(unsigned int size) { mCompositeMapSize = size; }
|
void setCompositeMapSize(unsigned int size) { mCompositeMapSize = size; }
|
||||||
@ -44,7 +48,7 @@ namespace Terrain
|
|||||||
void releaseGLObjects(osg::State* state) override;
|
void releaseGLObjects(osg::State* state) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
osg::ref_ptr<osg::Node> createChunk(float size, const osg::Vec2f& center, int lod, unsigned int lodFlags);
|
osg::ref_ptr<osg::Node> createChunk(float size, const osg::Vec2f& center, unsigned char lod, unsigned int lodFlags);
|
||||||
|
|
||||||
osg::ref_ptr<osg::Texture2D> createCompositeMapRTT();
|
osg::ref_ptr<osg::Texture2D> createCompositeMapRTT();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user