2
0

ResourceManager.cpp 4.4 KB

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