ResourceManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/resource/Common.h>
  7. #include <anki/util/List.h>
  8. #include <anki/util/Functions.h>
  9. #include <anki/util/String.h>
  10. namespace anki
  11. {
  12. // Forward
  13. class ConfigSet;
  14. class GrManager;
  15. class PhysicsWorld;
  16. class ResourceManager;
  17. class AsyncLoader;
  18. class ResourceManagerModel;
  19. class Renderer;
  20. /// @addtogroup resource
  21. /// @{
  22. /// Manage resources of a certain type
  23. template<typename Type>
  24. class TypeResourceManager
  25. {
  26. protected:
  27. /// @privatesection
  28. using Container = List<Type*>;
  29. TypeResourceManager()
  30. {
  31. }
  32. ~TypeResourceManager()
  33. {
  34. ANKI_ASSERT(m_ptrs.isEmpty() && "Forgot to delete some resources");
  35. m_ptrs.destroy(m_alloc);
  36. }
  37. Type* findLoadedResource(const CString& filename)
  38. {
  39. auto it = find(filename);
  40. return (it != m_ptrs.end()) ? *it : nullptr;
  41. }
  42. void registerResource(Type* ptr)
  43. {
  44. ANKI_ASSERT(ptr->getRefcount().load() == 0);
  45. ANKI_ASSERT(find(ptr->getFilename()) == m_ptrs.getEnd());
  46. m_ptrs.pushBack(m_alloc, ptr);
  47. }
  48. void unregisterResource(Type* ptr)
  49. {
  50. auto it = find(ptr->getFilename());
  51. ANKI_ASSERT(it != m_ptrs.end());
  52. m_ptrs.erase(m_alloc, it);
  53. }
  54. void init(ResourceAllocator<U8> alloc)
  55. {
  56. m_alloc = alloc;
  57. }
  58. private:
  59. ResourceAllocator<U8> m_alloc;
  60. Container m_ptrs;
  61. typename Container::Iterator find(const CString& filename)
  62. {
  63. typename Container::Iterator it;
  64. for(it = m_ptrs.getBegin(); it != m_ptrs.getEnd(); ++it)
  65. {
  66. if((*it)->getFilename() == filename)
  67. {
  68. break;
  69. }
  70. }
  71. return it;
  72. }
  73. };
  74. class ResourceManagerInitInfo
  75. {
  76. public:
  77. GrManager* m_gr = nullptr;
  78. PhysicsWorld* m_physics = nullptr;
  79. ResourceFilesystem* m_resourceFs = nullptr;
  80. const ConfigSet* m_config = nullptr;
  81. CString m_cacheDir;
  82. AllocAlignedCallback m_allocCallback = 0;
  83. void* m_allocCallbackData = nullptr;
  84. };
  85. /// Resource manager. It holds a few global variables
  86. class ResourceManager : public TypeResourceManager<Animation>,
  87. public TypeResourceManager<TextureResource>,
  88. public TypeResourceManager<ShaderResource>,
  89. public TypeResourceManager<Material>,
  90. public TypeResourceManager<Mesh>,
  91. public TypeResourceManager<Skeleton>,
  92. public TypeResourceManager<ParticleEmitterResource>,
  93. public TypeResourceManager<Model>,
  94. public TypeResourceManager<Script>,
  95. public TypeResourceManager<DummyRsrc>,
  96. public TypeResourceManager<CollisionResource>,
  97. public TypeResourceManager<GenericResource>
  98. {
  99. template<typename T>
  100. friend class ResourcePtrDeleter;
  101. public:
  102. ResourceManager();
  103. ~ResourceManager();
  104. ANKI_USE_RESULT Error create(ResourceManagerInitInfo& init);
  105. /// Load a resource.
  106. template<typename T>
  107. ANKI_USE_RESULT Error loadResource(
  108. const CString& filename, ResourcePtr<T>& out);
  109. /// Load a resource to cache.
  110. template<typename T, typename... TArgs>
  111. ANKI_USE_RESULT Error loadResourceToCache(
  112. ResourcePtr<T>& out, TArgs&&... args);
  113. anki_internal:
  114. U32 getMaxTextureSize() const
  115. {
  116. return m_maxTextureSize;
  117. }
  118. U32 getTextureAnisotropy() const
  119. {
  120. return m_textureAnisotropy;
  121. }
  122. ResourceAllocator<U8>& getAllocator()
  123. {
  124. return m_alloc;
  125. }
  126. TempResourceAllocator<U8>& getTempAllocator()
  127. {
  128. return m_tmpAlloc;
  129. }
  130. GrManager& getGrManager()
  131. {
  132. ANKI_ASSERT(m_gr);
  133. return *m_gr;
  134. }
  135. PhysicsWorld& _getPhysicsWorld()
  136. {
  137. ANKI_ASSERT(m_physics);
  138. return *m_physics;
  139. }
  140. ResourceFilesystem& getFilesystem()
  141. {
  142. ANKI_ASSERT(m_fs);
  143. return *m_fs;
  144. }
  145. const String& _getCacheDirectory() const
  146. {
  147. return m_cacheDir;
  148. }
  149. /// Set it with information from the renderer
  150. void _setShadersPrependedSource(const CString& cstr)
  151. {
  152. m_shadersPrependedSource.create(m_alloc, cstr);
  153. }
  154. void setRenderer(Renderer* r)
  155. {
  156. m_r = r;
  157. }
  158. const Renderer& getRenderer() const
  159. {
  160. ANKI_ASSERT(m_r);
  161. return *m_r;
  162. }
  163. const String& _getShadersPrependedSource() const
  164. {
  165. return m_shadersPrependedSource;
  166. }
  167. template<typename T>
  168. T* findLoadedResource(const CString& filename)
  169. {
  170. return TypeResourceManager<T>::findLoadedResource(filename);
  171. }
  172. template<typename T>
  173. void registerResource(T* ptr)
  174. {
  175. TypeResourceManager<T>::registerResource(ptr);
  176. }
  177. template<typename T>
  178. void unregisterResource(T* ptr)
  179. {
  180. TypeResourceManager<T>::unregisterResource(ptr);
  181. }
  182. AsyncLoader& getAsyncLoader()
  183. {
  184. return *m_asyncLoader;
  185. }
  186. private:
  187. GrManager* m_gr = nullptr;
  188. PhysicsWorld* m_physics = nullptr;
  189. ResourceFilesystem* m_fs = nullptr;
  190. Renderer* m_r = nullptr;
  191. ResourceAllocator<U8> m_alloc;
  192. TempResourceAllocator<U8> m_tmpAlloc;
  193. String m_cacheDir;
  194. U32 m_maxTextureSize;
  195. U32 m_textureAnisotropy;
  196. String m_shadersPrependedSource;
  197. AsyncLoader* m_asyncLoader = nullptr; ///< Async loading thread
  198. U64 m_uuid = 0;
  199. };
  200. /// @}
  201. } // end namespace anki
  202. #include <anki/resource/ResourceManager.inl.h>