| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Resource/TransferGpuAllocator.h>
- #include <AnKi/Util/List.h>
- #include <AnKi/Util/Functions.h>
- #include <AnKi/Util/String.h>
- namespace anki {
- // Forward
- class GrManager;
- class PhysicsWorld;
- class ResourceManager;
- class AsyncLoader;
- class ResourceManagerModel;
- class ShaderCompilerCache;
- class ShaderProgramResourceSystem;
- class UnifiedGeometryMemoryPool;
- /// @addtogroup resource
- /// @{
- /// Manage resources of a certain type
- template<typename Type>
- class TypeResourceManager
- {
- protected:
- TypeResourceManager()
- {
- }
- ~TypeResourceManager()
- {
- ANKI_ASSERT(m_ptrs.isEmpty() && "Forgot to delete some resources");
- m_ptrs.destroy(*m_pool);
- }
- Type* findLoadedResource(const CString& filename)
- {
- auto it = find(filename);
- return (it != m_ptrs.end()) ? *it : nullptr;
- }
- void registerResource(Type* ptr)
- {
- ANKI_ASSERT(find(ptr->getFilename()) == m_ptrs.getEnd());
- m_ptrs.pushBack(*m_pool, ptr);
- }
- void unregisterResource(Type* ptr)
- {
- auto it = find(ptr->getFilename());
- ANKI_ASSERT(it != m_ptrs.end());
- m_ptrs.erase(*m_pool, it);
- }
- void init(HeapMemoryPool* pool)
- {
- ANKI_ASSERT(pool);
- m_pool = pool;
- }
- private:
- using Container = List<Type*>;
- HeapMemoryPool* m_pool = nullptr;
- Container m_ptrs;
- typename Container::Iterator find(const CString& filename)
- {
- typename Container::Iterator it;
- for(it = m_ptrs.getBegin(); it != m_ptrs.getEnd(); ++it)
- {
- if((*it)->getFilename() == filename)
- {
- break;
- }
- }
- return it;
- }
- };
- class ResourceManagerInitInfo : public ResourceManagerExternalSubsystems
- {
- public:
- AllocAlignedCallback m_allocCallback = 0;
- void* m_allocCallbackData = nullptr;
- };
- /// Resource manager. It holds a few global variables
- class ResourceManager:
- #define ANKI_INSTANTIATE_RESOURCE(rsrc_, ptr_) \
- public \
- TypeResourceManager<rsrc_>
- #define ANKI_INSTANSIATE_RESOURCE_DELIMITER() ,
- #include <AnKi/Resource/InstantiationMacros.h>
- #undef ANKI_INSTANTIATE_RESOURCE
- #undef ANKI_INSTANSIATE_RESOURCE_DELIMITER
- {
- template<typename T>
- friend class ResourcePtrDeleter;
- friend class ResourceObject;
- public:
- ResourceManager();
- ~ResourceManager();
- Error init(ResourceManagerInitInfo& init);
- /// Load a resource.
- template<typename T>
- Error loadResource(const CString& filename, ResourcePtr<T>& out, Bool async = true);
- // Internals:
- ANKI_INTERNAL HeapMemoryPool& getMemoryPool() const
- {
- return m_pool;
- }
- ANKI_INTERNAL StackMemoryPool& getTempMemoryPool() const
- {
- return m_tmpPool;
- }
- ANKI_INTERNAL TransferGpuAllocator& getTransferGpuAllocator()
- {
- return *m_transferGpuAlloc;
- }
- template<typename T>
- ANKI_INTERNAL T* findLoadedResource(const CString& filename)
- {
- return TypeResourceManager<T>::findLoadedResource(filename);
- }
- template<typename T>
- ANKI_INTERNAL void registerResource(T* ptr)
- {
- TypeResourceManager<T>::registerResource(ptr);
- }
- template<typename T>
- ANKI_INTERNAL void unregisterResource(T* ptr)
- {
- TypeResourceManager<T>::unregisterResource(ptr);
- }
- ANKI_INTERNAL AsyncLoader& getAsyncLoader()
- {
- return *m_asyncLoader;
- }
- /// Get the number of times loadResource() was called.
- ANKI_INTERNAL U64 getLoadingRequestCount() const
- {
- return m_loadRequestCount;
- }
- /// Get the total number of completed async tasks.
- ANKI_INTERNAL U64 getAsyncTaskCompletedCount() const;
- /// Return the container of program libraries.
- const ShaderProgramResourceSystem& getShaderProgramResourceSystem() const
- {
- return *m_shaderProgramSystem;
- }
- private:
- ResourceManagerExternalSubsystems m_subsystems;
- mutable HeapMemoryPool m_pool; ///< Mutable because it's thread-safe and is may be called by const methods.
- mutable StackMemoryPool m_tmpPool; ///< Same as above.
- AsyncLoader* m_asyncLoader = nullptr; ///< Async loading thread
- ShaderProgramResourceSystem* m_shaderProgramSystem = nullptr;
- U64 m_uuid = 0;
- U64 m_loadRequestCount = 0;
- TransferGpuAllocator* m_transferGpuAlloc = nullptr;
- };
- /// @}
- } // end namespace anki
|