ResourceManager.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #pragma once
  6. #include <AnKi/Resource/TransferGpuAllocator.h>
  7. #include <AnKi/Resource/ResourceFilesystem.h>
  8. #include <AnKi/Util/List.h>
  9. #include <AnKi/Util/Functions.h>
  10. #include <AnKi/Util/String.h>
  11. #include <AnKi/Util/HashMap.h>
  12. #include <AnKi/Util/BlockArray.h>
  13. namespace anki {
  14. // Forward
  15. class GrManager;
  16. class PhysicsWorld;
  17. class ResourceManager;
  18. class AsyncLoader;
  19. class ResourceManagerModel;
  20. class ShaderCompilerCache;
  21. class ShaderProgramResourceSystem;
  22. class AccelerationStructureScratchAllocator;
  23. /// @addtogroup resource
  24. /// @{
  25. ANKI_CVAR(NumericCVar<PtrSize>, Rsrc, TransferScratchMemorySize, 256_MB, 1_MB, 4_GB, "Memory that is used fot texture and buffer uploads")
  26. /// Resource manager. It holds a few global variables
  27. class ResourceManager : public MakeSingleton<ResourceManager>
  28. {
  29. template<typename T>
  30. friend class ResourcePtrDeleter;
  31. friend class ResourceObject;
  32. template<typename>
  33. friend class MakeSingleton;
  34. public:
  35. Error init(AllocAlignedCallback allocCallback, void* allocCallbackData);
  36. /// Load a resource.
  37. /// @node Thread-safe against itself and freeResource.
  38. template<typename T>
  39. Error loadResource(const CString& filename, ResourcePtr<T>& out, Bool async = true);
  40. // Internals:
  41. /// @node Thread-safe against itself and loadResource.
  42. template<typename T>
  43. ANKI_INTERNAL void freeResource(T* ptr);
  44. private:
  45. template<typename Type>
  46. class TypeData
  47. {
  48. public:
  49. class Entry
  50. {
  51. public:
  52. Type* m_resource = nullptr;
  53. SpinLock m_mtx;
  54. };
  55. ResourceHashMap<CString, U32> m_map;
  56. ResourceBlockArray<Entry> m_entries;
  57. RWMutex m_mtx;
  58. TypeData()
  59. {
  60. for([[maybe_unused]] const Entry& e : m_entries)
  61. {
  62. ANKI_ASSERT(e.m_resource == nullptr && "Forgot to release some resource");
  63. }
  64. }
  65. };
  66. class AllTypeData:
  67. #define ANKI_INSTANTIATE_RESOURCE(className) \
  68. public \
  69. TypeData<className>
  70. #define ANKI_INSTANSIATE_RESOURCE_DELIMITER() ,
  71. #include <AnKi/Resource/Resources.def.h>
  72. {
  73. };
  74. AllTypeData m_allTypes;
  75. Atomic<U32> m_uuid = {1};
  76. ResourceManager();
  77. ~ResourceManager();
  78. };
  79. /// @}
  80. } // end namespace anki