ResourceManager.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Resource/ResourceManager.h>
  6. #include <AnKi/Resource/AsyncLoader.h>
  7. #include <AnKi/Resource/ShaderProgramResourceSystem.h>
  8. #include <AnKi/Resource/AnimationResource.h>
  9. #include <AnKi/Resource/AccelerationStructureScratchAllocator.h>
  10. #include <AnKi/Util/Logger.h>
  11. #include <AnKi/Util/CVarSet.h>
  12. #include <AnKi/Resource/MaterialResource.h>
  13. #include <AnKi/Resource/MeshResource.h>
  14. #include <AnKi/Resource/CpuMeshResource.h>
  15. #include <AnKi/Resource/ScriptResource.h>
  16. #include <AnKi/Resource/DummyResource.h>
  17. #include <AnKi/Resource/ParticleEmitterResource.h>
  18. #include <AnKi/Resource/ImageResource.h>
  19. #include <AnKi/Resource/GenericResource.h>
  20. #include <AnKi/Resource/ImageAtlasResource.h>
  21. #include <AnKi/Resource/ShaderProgramResource.h>
  22. #include <AnKi/Resource/SkeletonResource.h>
  23. namespace anki {
  24. ResourceManager::ResourceManager()
  25. {
  26. }
  27. ResourceManager::~ResourceManager()
  28. {
  29. ANKI_RESOURCE_LOGI("Destroying resource manager");
  30. AsyncLoader::freeSingleton();
  31. ShaderProgramResourceSystem::freeSingleton();
  32. TransferGpuAllocator::freeSingleton();
  33. ResourceFilesystem::freeSingleton();
  34. #define ANKI_INSTANTIATE_RESOURCE(className) \
  35. static_cast<TypeData<className>&>(m_allTypes).m_entries.destroy(); \
  36. static_cast<TypeData<className>&>(m_allTypes).m_map.destroy();
  37. #include <AnKi/Resource/Resources.def.h>
  38. AccelerationStructureScratchAllocator::freeSingleton();
  39. ResourceMemoryPool::freeSingleton();
  40. }
  41. Error ResourceManager::init(AllocAlignedCallback allocCallback, void* allocCallbackData)
  42. {
  43. ANKI_RESOURCE_LOGI("Initializing resource manager");
  44. ResourceMemoryPool::allocateSingleton(allocCallback, allocCallbackData);
  45. ResourceFilesystem::allocateSingleton();
  46. ANKI_CHECK(ResourceFilesystem::getSingleton().init());
  47. // Init the thread
  48. AsyncLoader::allocateSingleton();
  49. TransferGpuAllocator::allocateSingleton();
  50. ANKI_CHECK(TransferGpuAllocator::getSingleton().init(g_cvarRsrcTransferScratchMemorySize));
  51. // Init the programs
  52. ShaderProgramResourceSystem::allocateSingleton();
  53. ANKI_CHECK(ShaderProgramResourceSystem::getSingleton().init());
  54. if(GrManager::getSingleton().getDeviceCapabilities().m_rayTracingEnabled)
  55. {
  56. AccelerationStructureScratchAllocator::allocateSingleton();
  57. }
  58. return Error::kNone;
  59. }
  60. template<typename T>
  61. Error ResourceManager::loadResource(const CString& filename, ResourcePtr<T>& out, Bool async)
  62. {
  63. ANKI_ASSERT(!out.isCreated() && "Already loaded");
  64. TypeData<T>& type = static_cast<TypeData<T>&>(m_allTypes);
  65. // Check if registered
  66. using EntryType = typename TypeData<T>::Entry;
  67. EntryType* entry;
  68. {
  69. RLockGuard lock(type.m_mtx);
  70. auto it = type.m_map.find(filename);
  71. entry = (it != type.m_map.getEnd()) ? &type.m_entries[*it] : nullptr;
  72. }
  73. if(!entry)
  74. {
  75. // Resource entry doesn't exist, create one
  76. WLockGuard lock(type.m_mtx);
  77. auto it = type.m_map.find(filename); // Search again
  78. if(it != type.m_map.getEnd())
  79. {
  80. entry = &type.m_entries[*it];
  81. }
  82. else
  83. {
  84. auto arrit = type.m_entries.emplace();
  85. type.m_map.emplace(filename, arrit.getArrayIndex());
  86. entry = &(*arrit);
  87. }
  88. }
  89. ANKI_ASSERT(entry);
  90. // Try loading the resource
  91. Error err = Error::kNone;
  92. {
  93. LockGuard lock(entry->m_mtx);
  94. if(entry->m_resource == nullptr)
  95. {
  96. // Resource hasn't been loaded, load it
  97. T* rsrc = newInstance<T>(ResourceMemoryPool::getSingleton(), filename, m_uuid.fetchAdd(1));
  98. // Increment the refcount in that case where async jobs increment it and decrement it in the scope of a load()
  99. rsrc->retain();
  100. err = rsrc->load(filename, async);
  101. // Decrement because of the increment happened a few lines above
  102. rsrc->release();
  103. if(err)
  104. {
  105. ANKI_RESOURCE_LOGE("Failed to load resource: %s", filename.cstr());
  106. deleteInstance(ResourceMemoryPool::getSingleton(), rsrc);
  107. }
  108. else
  109. {
  110. entry->m_resource = rsrc;
  111. }
  112. }
  113. }
  114. if(!err)
  115. {
  116. out.reset(entry->m_resource);
  117. }
  118. return err;
  119. }
  120. template<typename T>
  121. void ResourceManager::freeResource(T* ptr)
  122. {
  123. ANKI_ASSERT(ptr);
  124. ANKI_ASSERT(ptr->m_refcount.load() == 0);
  125. TypeData<T>& type = static_cast<TypeData<T>&>(m_allTypes);
  126. typename TypeData<T>::Entry* entry = nullptr;
  127. {
  128. RLockGuard lock(type.m_mtx);
  129. auto it = type.m_map.find(ptr->m_fname);
  130. ANKI_ASSERT(it != type.m_map.getEnd());
  131. entry = &type.m_entries[*it];
  132. }
  133. {
  134. LockGuard lock(entry->m_mtx);
  135. ANKI_ASSERT(entry->m_resource);
  136. deleteInstance(ResourceMemoryPool::getSingleton(), entry->m_resource);
  137. entry->m_resource = nullptr;
  138. }
  139. }
  140. // Instansiate
  141. #define ANKI_INSTANTIATE_RESOURCE(className) \
  142. template Error ResourceManager::loadResource<className>(const CString& filename, ResourcePtr<className>& out, Bool async); \
  143. template void ResourceManager::freeResource<className>(className * ptr);
  144. #include <AnKi/Resource/Resources.def.h>
  145. } // end namespace anki