ResourceManager.h 4.7 KB

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