ResourceManager.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (C) 2009-2023, 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 GrManager;
  13. class PhysicsWorld;
  14. class ResourceManager;
  15. class AsyncLoader;
  16. class ResourceManagerModel;
  17. class ShaderCompilerCache;
  18. class ShaderProgramResourceSystem;
  19. class UnifiedGeometryMemoryPool;
  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_pool);
  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_pool, 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_pool, it);
  50. }
  51. void init(HeapMemoryPool* pool)
  52. {
  53. ANKI_ASSERT(pool);
  54. m_pool = pool;
  55. }
  56. private:
  57. using Container = List<Type*>;
  58. HeapMemoryPool* m_pool = nullptr;
  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 : public ResourceManagerExternalSubsystems
  74. {
  75. public:
  76. AllocAlignedCallback m_allocCallback = 0;
  77. void* m_allocCallbackData = nullptr;
  78. };
  79. /// Resource manager. It holds a few global variables
  80. class ResourceManager:
  81. #define ANKI_INSTANTIATE_RESOURCE(rsrc_, ptr_) \
  82. public \
  83. TypeResourceManager<rsrc_>
  84. #define ANKI_INSTANSIATE_RESOURCE_DELIMITER() ,
  85. #include <AnKi/Resource/InstantiationMacros.h>
  86. #undef ANKI_INSTANTIATE_RESOURCE
  87. #undef ANKI_INSTANSIATE_RESOURCE_DELIMITER
  88. {
  89. template<typename T>
  90. friend class ResourcePtrDeleter;
  91. friend class ResourceObject;
  92. public:
  93. ResourceManager();
  94. ~ResourceManager();
  95. Error init(ResourceManagerInitInfo& init);
  96. /// Load a resource.
  97. template<typename T>
  98. Error loadResource(const CString& filename, ResourcePtr<T>& out, Bool async = true);
  99. // Internals:
  100. ANKI_INTERNAL HeapMemoryPool& getMemoryPool() const
  101. {
  102. return m_pool;
  103. }
  104. ANKI_INTERNAL StackMemoryPool& getTempMemoryPool() const
  105. {
  106. return m_tmpPool;
  107. }
  108. ANKI_INTERNAL TransferGpuAllocator& getTransferGpuAllocator()
  109. {
  110. return *m_transferGpuAlloc;
  111. }
  112. template<typename T>
  113. ANKI_INTERNAL T* findLoadedResource(const CString& filename)
  114. {
  115. return TypeResourceManager<T>::findLoadedResource(filename);
  116. }
  117. template<typename T>
  118. ANKI_INTERNAL void registerResource(T* ptr)
  119. {
  120. TypeResourceManager<T>::registerResource(ptr);
  121. }
  122. template<typename T>
  123. ANKI_INTERNAL void unregisterResource(T* ptr)
  124. {
  125. TypeResourceManager<T>::unregisterResource(ptr);
  126. }
  127. ANKI_INTERNAL AsyncLoader& getAsyncLoader()
  128. {
  129. return *m_asyncLoader;
  130. }
  131. /// Get the number of times loadResource() was called.
  132. ANKI_INTERNAL U64 getLoadingRequestCount() const
  133. {
  134. return m_loadRequestCount;
  135. }
  136. /// Get the total number of completed async tasks.
  137. ANKI_INTERNAL U64 getAsyncTaskCompletedCount() const;
  138. /// Return the container of program libraries.
  139. const ShaderProgramResourceSystem& getShaderProgramResourceSystem() const
  140. {
  141. return *m_shaderProgramSystem;
  142. }
  143. private:
  144. ResourceManagerExternalSubsystems m_subsystems;
  145. mutable HeapMemoryPool m_pool; ///< Mutable because it's thread-safe and is may be called by const methods.
  146. mutable StackMemoryPool m_tmpPool; ///< Same as above.
  147. AsyncLoader* m_asyncLoader = nullptr; ///< Async loading thread
  148. ShaderProgramResourceSystem* m_shaderProgramSystem = nullptr;
  149. U64 m_uuid = 0;
  150. U64 m_loadRequestCount = 0;
  151. TransferGpuAllocator* m_transferGpuAlloc = nullptr;
  152. };
  153. /// @}
  154. } // end namespace anki