ResourceManager.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #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. static NumericCVar<PtrSize> g_transferScratchMemorySizeCVar(CVarSubsystem::kResource, "TransferScratchMemorySize", 256_MB, 1_MB, 4_GB,
  25. "Memory that is used fot texture and buffer uploads");
  26. ResourceManager::ResourceManager()
  27. {
  28. }
  29. ResourceManager::~ResourceManager()
  30. {
  31. ANKI_RESOURCE_LOGI("Destroying resource manager");
  32. deleteInstance(ResourceMemoryPool::getSingleton(), m_asyncLoader);
  33. deleteInstance(ResourceMemoryPool::getSingleton(), m_shaderProgramSystem);
  34. deleteInstance(ResourceMemoryPool::getSingleton(), m_transferGpuAlloc);
  35. deleteInstance(ResourceMemoryPool::getSingleton(), m_fs);
  36. ResourceMemoryPool::freeSingleton();
  37. }
  38. Error ResourceManager::init(AllocAlignedCallback allocCallback, void* allocCallbackData)
  39. {
  40. ANKI_RESOURCE_LOGI("Initializing resource manager");
  41. ResourceMemoryPool::allocateSingleton(allocCallback, allocCallbackData);
  42. m_fs = newInstance<ResourceFilesystem>(ResourceMemoryPool::getSingleton());
  43. ANKI_CHECK(m_fs->init());
  44. // Init the thread
  45. m_asyncLoader = newInstance<AsyncLoader>(ResourceMemoryPool::getSingleton());
  46. m_transferGpuAlloc = newInstance<TransferGpuAllocator>(ResourceMemoryPool::getSingleton());
  47. ANKI_CHECK(m_transferGpuAlloc->init(g_transferScratchMemorySizeCVar.get()));
  48. // Init the programs
  49. m_shaderProgramSystem = newInstance<ShaderProgramResourceSystem>(ResourceMemoryPool::getSingleton());
  50. ANKI_CHECK(m_shaderProgramSystem->init());
  51. return Error::kNone;
  52. }
  53. U64 ResourceManager::getAsyncTaskCompletedCount() const
  54. {
  55. return m_asyncLoader->getCompletedTaskCount();
  56. }
  57. template<typename T>
  58. Error ResourceManager::loadResource(const CString& filename, ResourcePtr<T>& out, Bool async)
  59. {
  60. ANKI_ASSERT(!out.isCreated() && "Already loaded");
  61. Error err = Error::kNone;
  62. ++m_loadRequestCount;
  63. T* const other = findLoadedResource<T>(filename);
  64. if(other)
  65. {
  66. // Found
  67. out.reset(other);
  68. }
  69. else
  70. {
  71. // Allocate ptr
  72. T* ptr = newInstance<T>(ResourceMemoryPool::getSingleton());
  73. ANKI_ASSERT(ptr->getRefcount() == 0);
  74. // Increment the refcount in that case where async jobs increment it and decrement it in the scope of a load()
  75. ptr->retain();
  76. err = ptr->load(filename, async);
  77. if(err)
  78. {
  79. ANKI_RESOURCE_LOGE("Failed to load resource: %s", &filename[0]);
  80. deleteInstance(ResourceMemoryPool::getSingleton(), ptr);
  81. return err;
  82. }
  83. ptr->setFilename(filename);
  84. ptr->setUuid(++m_uuid);
  85. // Register resource
  86. registerResource(ptr);
  87. out.reset(ptr);
  88. // Decrement because of the increment happened a few lines above
  89. ptr->release();
  90. }
  91. return err;
  92. }
  93. // Instansiate the ResourceManager::loadResource()
  94. #define ANKI_INSTANTIATE_RESOURCE(rsrc_, ptr_) \
  95. template Error ResourceManager::loadResource<rsrc_>(const CString& filename, ResourcePtr<rsrc_>& out, Bool async);
  96. #define ANKI_INSTANSIATE_RESOURCE_DELIMITER()
  97. #include <AnKi/Resource/InstantiationMacros.h>
  98. #undef ANKI_INSTANTIATE_RESOURCE
  99. #undef ANKI_INSTANSIATE_RESOURCE_DELIMITER
  100. } // end namespace anki