ResourceManager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Resource/TransferGpuAllocator.h>
  7. #include <AnKi/Util/List.h>
  8. #include <AnKi/Util/Functions.h>
  9. #include <AnKi/Util/String.h>
  10. namespace anki {
  11. // Forward
  12. class ConfigSet;
  13. class GrManager;
  14. class PhysicsWorld;
  15. class ResourceManager;
  16. class AsyncLoader;
  17. class ResourceManagerModel;
  18. class ShaderCompilerCache;
  19. class ShaderProgramResourceSystem;
  20. class VertexGpuMemoryPool;
  21. /// @addtogroup resource
  22. /// @{
  23. /// Manage resources of a certain type
  24. template<typename Type>
  25. class TypeResourceManager
  26. {
  27. protected:
  28. TypeResourceManager()
  29. {
  30. }
  31. ~TypeResourceManager()
  32. {
  33. ANKI_ASSERT(m_ptrs.isEmpty() && "Forgot to delete some resources");
  34. m_ptrs.destroy(m_alloc);
  35. }
  36. Type* findLoadedResource(const CString& filename)
  37. {
  38. auto it = find(filename);
  39. return (it != m_ptrs.end()) ? *it : nullptr;
  40. }
  41. void registerResource(Type* ptr)
  42. {
  43. ANKI_ASSERT(find(ptr->getFilename()) == m_ptrs.getEnd());
  44. m_ptrs.pushBack(m_alloc, ptr);
  45. }
  46. void unregisterResource(Type* ptr)
  47. {
  48. auto it = find(ptr->getFilename());
  49. ANKI_ASSERT(it != m_ptrs.end());
  50. m_ptrs.erase(m_alloc, it);
  51. }
  52. void init(ResourceAllocator<U8> alloc)
  53. {
  54. m_alloc = alloc;
  55. }
  56. private:
  57. using Container = List<Type*>;
  58. ResourceAllocator<U8> m_alloc;
  59. Container m_ptrs;
  60. typename Container::Iterator find(const CString& filename)
  61. {
  62. typename Container::Iterator it;
  63. for(it = m_ptrs.getBegin(); it != m_ptrs.getEnd(); ++it)
  64. {
  65. if((*it)->getFilename() == filename)
  66. {
  67. break;
  68. }
  69. }
  70. return it;
  71. }
  72. };
  73. class ResourceManagerInitInfo
  74. {
  75. public:
  76. GrManager* m_gr = nullptr;
  77. PhysicsWorld* m_physics = nullptr;
  78. ResourceFilesystem* m_resourceFs = nullptr;
  79. ConfigSet* m_config = nullptr;
  80. VertexGpuMemoryPool* m_vertexMemory = nullptr;
  81. AllocAlignedCallback m_allocCallback = 0;
  82. void* m_allocCallbackData = nullptr;
  83. };
  84. /// Resource manager. It holds a few global variables
  85. class ResourceManager:
  86. #define ANKI_INSTANTIATE_RESOURCE(rsrc_, ptr_) \
  87. public \
  88. TypeResourceManager<rsrc_>
  89. #define ANKI_INSTANSIATE_RESOURCE_DELIMITER() ,
  90. #include <AnKi/Resource/InstantiationMacros.h>
  91. #undef ANKI_INSTANTIATE_RESOURCE
  92. #undef ANKI_INSTANSIATE_RESOURCE_DELIMITER
  93. {
  94. template<typename T>
  95. friend class ResourcePtrDeleter;
  96. public:
  97. ResourceManager();
  98. ~ResourceManager();
  99. Error init(ResourceManagerInitInfo& init);
  100. /// Load a resource.
  101. template<typename T>
  102. Error loadResource(const CString& filename, ResourcePtr<T>& out, Bool async = true);
  103. // Internals:
  104. ANKI_INTERNAL ResourceAllocator<U8>& getAllocator()
  105. {
  106. return m_alloc;
  107. }
  108. ANKI_INTERNAL TempResourceAllocator<U8>& getTempAllocator()
  109. {
  110. return m_tmpAlloc;
  111. }
  112. ANKI_INTERNAL GrManager& getGrManager()
  113. {
  114. ANKI_ASSERT(m_gr);
  115. return *m_gr;
  116. }
  117. ANKI_INTERNAL TransferGpuAllocator& getTransferGpuAllocator()
  118. {
  119. return *m_transferGpuAlloc;
  120. }
  121. ANKI_INTERNAL PhysicsWorld& getPhysicsWorld()
  122. {
  123. ANKI_ASSERT(m_physics);
  124. return *m_physics;
  125. }
  126. ANKI_INTERNAL ResourceFilesystem& getFilesystem()
  127. {
  128. ANKI_ASSERT(m_fs);
  129. return *m_fs;
  130. }
  131. template<typename T>
  132. ANKI_INTERNAL T* findLoadedResource(const CString& filename)
  133. {
  134. return TypeResourceManager<T>::findLoadedResource(filename);
  135. }
  136. template<typename T>
  137. ANKI_INTERNAL void registerResource(T* ptr)
  138. {
  139. TypeResourceManager<T>::registerResource(ptr);
  140. }
  141. template<typename T>
  142. ANKI_INTERNAL void unregisterResource(T* ptr)
  143. {
  144. TypeResourceManager<T>::unregisterResource(ptr);
  145. }
  146. ANKI_INTERNAL AsyncLoader& getAsyncLoader()
  147. {
  148. return *m_asyncLoader;
  149. }
  150. /// Get the number of times loadResource() was called.
  151. ANKI_INTERNAL U64 getLoadingRequestCount() const
  152. {
  153. return m_loadRequestCount;
  154. }
  155. /// Get the total number of completed async tasks.
  156. ANKI_INTERNAL U64 getAsyncTaskCompletedCount() const;
  157. /// Return the container of program libraries.
  158. const ShaderProgramResourceSystem& getShaderProgramResourceSystem() const
  159. {
  160. return *m_shaderProgramSystem;
  161. }
  162. VertexGpuMemoryPool& getVertexGpuMemory()
  163. {
  164. ANKI_ASSERT(m_vertexMem);
  165. return *m_vertexMem;
  166. }
  167. const ConfigSet& getConfig() const
  168. {
  169. ANKI_ASSERT(m_config);
  170. return *m_config;
  171. }
  172. private:
  173. GrManager* m_gr = nullptr;
  174. PhysicsWorld* m_physics = nullptr;
  175. ResourceFilesystem* m_fs = nullptr;
  176. ConfigSet* m_config = nullptr;
  177. ResourceAllocator<U8> m_alloc;
  178. TempResourceAllocator<U8> m_tmpAlloc;
  179. AsyncLoader* m_asyncLoader = nullptr; ///< Async loading thread
  180. ShaderProgramResourceSystem* m_shaderProgramSystem = nullptr;
  181. VertexGpuMemoryPool* m_vertexMem = nullptr;
  182. U64 m_uuid = 0;
  183. U64 m_loadRequestCount = 0;
  184. TransferGpuAllocator* m_transferGpuAlloc = nullptr;
  185. };
  186. /// @}
  187. } // end namespace anki