ResourceManager.cpp 4.4 KB

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