ResourceManager.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (C) 2009-2022, 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/Util/Logger.h>
  10. #include <AnKi/Core/ConfigSet.h>
  11. #include <AnKi/Resource/MaterialResource.h>
  12. #include <AnKi/Resource/MeshResource.h>
  13. #include <AnKi/Resource/CpuMeshResource.h>
  14. #include <AnKi/Resource/ModelResource.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. m_alloc.deleteInstance(m_asyncLoader);
  31. m_alloc.deleteInstance(m_shaderProgramSystem);
  32. m_alloc.deleteInstance(m_transferGpuAlloc);
  33. }
  34. Error ResourceManager::init(ResourceManagerInitInfo& init)
  35. {
  36. ANKI_RESOURCE_LOGI("Initializing resource manager");
  37. m_gr = init.m_gr;
  38. m_physics = init.m_physics;
  39. m_fs = init.m_resourceFs;
  40. m_config = init.m_config;
  41. m_vertexMem = init.m_vertexMemory;
  42. m_alloc = ResourceAllocator<U8>(init.m_allocCallback, init.m_allocCallbackData, "Resources");
  43. m_tmpAlloc = TempResourceAllocator<U8>(init.m_allocCallback, init.m_allocCallbackData, 10_MB);
  44. // Init type resource managers
  45. #define ANKI_INSTANTIATE_RESOURCE(rsrc_, ptr_) TypeResourceManager<rsrc_>::init(m_alloc);
  46. #define ANKI_INSTANSIATE_RESOURCE_DELIMITER()
  47. #include <AnKi/Resource/InstantiationMacros.h>
  48. #undef ANKI_INSTANTIATE_RESOURCE
  49. #undef ANKI_INSTANSIATE_RESOURCE_DELIMITER
  50. // Init the thread
  51. m_asyncLoader = m_alloc.newInstance<AsyncLoader>();
  52. m_asyncLoader->init(m_alloc);
  53. m_transferGpuAlloc = m_alloc.newInstance<TransferGpuAllocator>();
  54. ANKI_CHECK(m_transferGpuAlloc->init(m_config->getRsrcTransferScratchMemorySize(), m_gr, m_alloc));
  55. // Init the programs
  56. m_shaderProgramSystem = m_alloc.newInstance<ShaderProgramResourceSystem>(m_alloc);
  57. ANKI_CHECK(m_shaderProgramSystem->init(*m_fs, *m_gr));
  58. return Error::NONE;
  59. }
  60. U64 ResourceManager::getAsyncTaskCompletedCount() const
  61. {
  62. return m_asyncLoader->getCompletedTaskCount();
  63. }
  64. template<typename T>
  65. Error ResourceManager::loadResource(const CString& filename, ResourcePtr<T>& out, Bool async)
  66. {
  67. ANKI_ASSERT(!out.isCreated() && "Already loaded");
  68. Error err = Error::NONE;
  69. ++m_loadRequestCount;
  70. T* const other = findLoadedResource<T>(filename);
  71. if(other)
  72. {
  73. // Found
  74. out.reset(other);
  75. }
  76. else
  77. {
  78. // Allocate ptr
  79. T* ptr = m_alloc.newInstance<T>(this);
  80. ANKI_ASSERT(ptr->getRefcount().load() == 0);
  81. // Increment the refcount in that case where async jobs increment it and decrement it in the scope of a load()
  82. ptr->getRefcount().fetchAdd(1);
  83. // Populate the ptr. Use a block to cleanup temp_pool allocations
  84. auto& pool = m_tmpAlloc.getMemoryPool();
  85. {
  86. U allocsCountBefore = pool.getAllocationCount();
  87. (void)allocsCountBefore;
  88. err = ptr->load(filename, async);
  89. if(err)
  90. {
  91. ANKI_RESOURCE_LOGE("Failed to load resource: %s", &filename[0]);
  92. m_alloc.deleteInstance(ptr);
  93. return err;
  94. }
  95. ANKI_ASSERT(pool.getAllocationCount() == allocsCountBefore && "Forgot to deallocate");
  96. }
  97. ptr->setFilename(filename);
  98. ptr->setUuid(++m_uuid);
  99. // Reset the memory pool if no-one is using it.
  100. // NOTE: Check because resources load other resources
  101. if(pool.getAllocationCount() == 0)
  102. {
  103. pool.reset();
  104. }
  105. // Register resource
  106. registerResource(ptr);
  107. out.reset(ptr);
  108. // Decrement because of the increment happened a few lines above
  109. ptr->getRefcount().fetchSub(1);
  110. }
  111. return err;
  112. }
  113. // Instansiate the ResourceManager::loadResource()
  114. #define ANKI_INSTANTIATE_RESOURCE(rsrc_, ptr_) \
  115. template Error ResourceManager::loadResource<rsrc_>(const CString& filename, ResourcePtr<rsrc_>& out, Bool async);
  116. #define ANKI_INSTANSIATE_RESOURCE_DELIMITER()
  117. #include <AnKi/Resource/InstantiationMacros.h>
  118. #undef ANKI_INSTANTIATE_RESOURCE
  119. #undef ANKI_INSTANSIATE_RESOURCE_DELIMITER
  120. } // end namespace anki