ResourceManager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/Resource/ResourceFilesystem.h>
  8. #include <AnKi/Util/List.h>
  9. #include <AnKi/Util/Functions.h>
  10. #include <AnKi/Util/String.h>
  11. namespace anki {
  12. // Forward
  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();
  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(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(it);
  50. }
  51. private:
  52. using Container = ResourceList<Type*>;
  53. Container m_ptrs;
  54. typename Container::Iterator find(const CString& filename)
  55. {
  56. typename Container::Iterator it;
  57. for(it = m_ptrs.getBegin(); it != m_ptrs.getEnd(); ++it)
  58. {
  59. if((*it)->getFilename() == filename)
  60. {
  61. break;
  62. }
  63. }
  64. return it;
  65. }
  66. };
  67. /// Resource manager. It holds a few global variables
  68. class ResourceManager : public MakeSingleton<ResourceManager>,
  69. #define ANKI_INSTANTIATE_RESOURCE(rsrc_, ptr_) \
  70. public \
  71. TypeResourceManager<rsrc_>
  72. #define ANKI_INSTANSIATE_RESOURCE_DELIMITER() ,
  73. #include <AnKi/Resource/InstantiationMacros.h>
  74. #undef ANKI_INSTANTIATE_RESOURCE
  75. #undef ANKI_INSTANSIATE_RESOURCE_DELIMITER
  76. {
  77. template<typename T>
  78. friend class ResourcePtrDeleter;
  79. friend class ResourceObject;
  80. template<typename>
  81. friend class MakeSingleton;
  82. public:
  83. Error init(AllocAlignedCallback allocCallback, void* allocCallbackData);
  84. /// Load a resource.
  85. template<typename T>
  86. Error loadResource(const CString& filename, ResourcePtr<T>& out, Bool async = true);
  87. // Internals:
  88. ANKI_INTERNAL TransferGpuAllocator& getTransferGpuAllocator()
  89. {
  90. return *m_transferGpuAlloc;
  91. }
  92. template<typename T>
  93. ANKI_INTERNAL T* findLoadedResource(const CString& filename)
  94. {
  95. return TypeResourceManager<T>::findLoadedResource(filename);
  96. }
  97. template<typename T>
  98. ANKI_INTERNAL void registerResource(T* ptr)
  99. {
  100. TypeResourceManager<T>::registerResource(ptr);
  101. }
  102. template<typename T>
  103. ANKI_INTERNAL void unregisterResource(T* ptr)
  104. {
  105. TypeResourceManager<T>::unregisterResource(ptr);
  106. }
  107. ANKI_INTERNAL AsyncLoader& getAsyncLoader()
  108. {
  109. return *m_asyncLoader;
  110. }
  111. /// Get the number of times loadResource() was called.
  112. ANKI_INTERNAL U64 getLoadingRequestCount() const
  113. {
  114. return m_loadRequestCount;
  115. }
  116. /// Get the total number of completed async tasks.
  117. ANKI_INTERNAL U64 getAsyncTaskCompletedCount() const;
  118. /// Return the container of program libraries.
  119. ANKI_INTERNAL const ShaderProgramResourceSystem& getShaderProgramResourceSystem() const
  120. {
  121. return *m_shaderProgramSystem;
  122. }
  123. ANKI_INTERNAL ResourceFilesystem& getFilesystem()
  124. {
  125. return *m_fs;
  126. }
  127. private:
  128. ResourceFilesystem* m_fs = nullptr;
  129. AsyncLoader* m_asyncLoader = nullptr; ///< Async loading thread
  130. ShaderProgramResourceSystem* m_shaderProgramSystem = nullptr;
  131. TransferGpuAllocator* m_transferGpuAlloc = nullptr;
  132. U64 m_uuid = 0;
  133. U64 m_loadRequestCount = 0;
  134. ResourceManager();
  135. ~ResourceManager();
  136. };
  137. /// @}
  138. } // end namespace anki