2
0

ResourceManager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. {
  12. // Forward
  13. class ConfigSet;
  14. class GrManager;
  15. class PhysicsWorld;
  16. class ResourceManager;
  17. class AsyncLoader;
  18. class ResourceManagerModel;
  19. class ShaderCompilerCache;
  20. class ShaderProgramResourceSystem;
  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. const ConfigSet* m_config = nullptr;
  80. CString m_cacheDir;
  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. ANKI_USE_RESULT Error init(ResourceManagerInitInfo& init);
  100. /// Load a resource.
  101. template<typename T>
  102. ANKI_USE_RESULT Error loadResource(const CString& filename, ResourcePtr<T>& out, Bool async = true);
  103. // Internals:
  104. ANKI_INTERNAL U32 getMaxImageSize() const
  105. {
  106. return m_maxImageSize;
  107. }
  108. ANKI_INTERNAL Bool getDumpShaderSource() const
  109. {
  110. return m_dumpShaderSource;
  111. }
  112. ANKI_INTERNAL ResourceAllocator<U8>& getAllocator()
  113. {
  114. return m_alloc;
  115. }
  116. ANKI_INTERNAL TempResourceAllocator<U8>& getTempAllocator()
  117. {
  118. return m_tmpAlloc;
  119. }
  120. ANKI_INTERNAL GrManager& getGrManager()
  121. {
  122. ANKI_ASSERT(m_gr);
  123. return *m_gr;
  124. }
  125. ANKI_INTERNAL TransferGpuAllocator& getTransferGpuAllocator()
  126. {
  127. return *m_transferGpuAlloc;
  128. }
  129. ANKI_INTERNAL PhysicsWorld& getPhysicsWorld()
  130. {
  131. ANKI_ASSERT(m_physics);
  132. return *m_physics;
  133. }
  134. ANKI_INTERNAL ResourceFilesystem& getFilesystem()
  135. {
  136. ANKI_ASSERT(m_fs);
  137. return *m_fs;
  138. }
  139. ANKI_INTERNAL const String& getCacheDirectory() const
  140. {
  141. return m_cacheDir;
  142. }
  143. template<typename T>
  144. ANKI_INTERNAL T* findLoadedResource(const CString& filename)
  145. {
  146. return TypeResourceManager<T>::findLoadedResource(filename);
  147. }
  148. template<typename T>
  149. ANKI_INTERNAL void registerResource(T* ptr)
  150. {
  151. TypeResourceManager<T>::registerResource(ptr);
  152. }
  153. template<typename T>
  154. ANKI_INTERNAL void unregisterResource(T* ptr)
  155. {
  156. TypeResourceManager<T>::unregisterResource(ptr);
  157. }
  158. ANKI_INTERNAL AsyncLoader& getAsyncLoader()
  159. {
  160. return *m_asyncLoader;
  161. }
  162. /// Get the number of times loadResource() was called.
  163. ANKI_INTERNAL U64 getLoadingRequestCount() const
  164. {
  165. return m_loadRequestCount;
  166. }
  167. /// Get the total number of completed async tasks.
  168. ANKI_INTERNAL U64 getAsyncTaskCompletedCount() const;
  169. /// Return the container of program libraries.
  170. const ShaderProgramResourceSystem& getShaderProgramResourceSystem() const
  171. {
  172. return *m_shaderProgramSystem;
  173. }
  174. private:
  175. GrManager* m_gr = nullptr;
  176. PhysicsWorld* m_physics = nullptr;
  177. ResourceFilesystem* m_fs = nullptr;
  178. ResourceAllocator<U8> m_alloc;
  179. TempResourceAllocator<U8> m_tmpAlloc;
  180. String m_cacheDir;
  181. U32 m_maxImageSize;
  182. AsyncLoader* m_asyncLoader = nullptr; ///< Async loading thread
  183. ShaderProgramResourceSystem* m_shaderProgramSystem = nullptr;
  184. U64 m_uuid = 0;
  185. U64 m_loadRequestCount = 0;
  186. TransferGpuAllocator* m_transferGpuAlloc = nullptr;
  187. Bool m_dumpShaderSource = false;
  188. };
  189. /// @}
  190. } // end namespace anki