ResourceManager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ANKI_CVAR(NumericCVar<PtrSize>, Rsrc, TransferScratchMemorySize, 256_MB, 1_MB, 4_GB, "Memory that is used fot texture and buffer uploads")
  24. ANKI_CVAR(BoolCVar, Rsrc, TrackFileUpdates, false, "If true the resource manager is able to track file update times")
  25. // Resource manager. It holds a few global variables
  26. class ResourceManager : public MakeSingleton<ResourceManager>
  27. {
  28. template<typename T>
  29. friend class ResourcePtrDeleter;
  30. friend class ResourceObject;
  31. template<typename>
  32. friend class MakeSingleton;
  33. public:
  34. Error init(AllocAlignedCallback allocCallback, void* allocCallbackData);
  35. // Load a resource.
  36. // Note: Thread-safe against itself, freeResource() and refreshFileUpdateTimes()
  37. template<typename T>
  38. Error loadResource(CString filename, ResourcePtr<T>& out, Bool async = true);
  39. // Iterate all loaded resource and check if the files have been updated since they were loaded.
  40. // Note: Thread-safe against itself, loadResource() and freeResource()
  41. void refreshFileUpdateTimes();
  42. // Internals:
  43. // Note: Thread-safe against itself, loadResource() and refreshFileUpdateTimes()
  44. template<typename T>
  45. ANKI_INTERNAL void freeResource(T* ptr);
  46. private:
  47. template<typename Type>
  48. class TypeData
  49. {
  50. public:
  51. class Entry
  52. {
  53. public:
  54. DynamicArray<Type*> m_resources; // Hosts multiple versions of a resource. The last element is the newest
  55. SpinLock m_mtx;
  56. U64 m_fileUpdateTime = 0;
  57. };
  58. ResourceHashMap<CString, U32> m_map;
  59. ResourceBlockArray<Entry> m_entries;
  60. RWMutex m_mtx;
  61. TypeData()
  62. {
  63. for([[maybe_unused]] const Entry& e : m_entries)
  64. {
  65. ANKI_ASSERT(e.m_resources.getSize() > 0 && "Forgot to release some resource");
  66. }
  67. }
  68. };
  69. class AllTypeData:
  70. #define ANKI_INSTANTIATE_RESOURCE(className) \
  71. public \
  72. TypeData<className>
  73. #define ANKI_INSTANSIATE_RESOURCE_DELIMITER() ,
  74. #include <AnKi/Resource/Resources.def.h>
  75. {
  76. };
  77. AllTypeData m_allTypes;
  78. Atomic<U32> m_uuid = {1};
  79. Bool m_trackFileUpdateTimes = false;
  80. ResourceManager();
  81. ~ResourceManager();
  82. template<typename T>
  83. void refreshFileUpdateTimesInternal();
  84. };
  85. } // end namespace anki