ezEngine  Milestone 7
ResourceManager.h
1 #pragma once
2 
3 #include <Core/Basics.h>
4 #include <Core/ResourceManager/ResourceBase.h>
5 #include <Core/ResourceManager/ResourceHandle.h>
6 #include <Core/ResourceManager/ResourceTypeLoader.h>
7 #include <Foundation/Containers/HashTable.h>
8 #include <Foundation/Threading/TaskSystem.h>
9 
10 
12 class EZ_CORE_DLL ezResourceManagerWorker : public ezTask
13 {
14 private:
15  friend class ezResourceManager;
16 
18 
19  virtual void Execute() override;
20 };
21 
23 class EZ_CORE_DLL ezResourceManagerWorkerGPU : public ezTask
24 {
25 public:
26  ezResourceLoadData m_LoaderData;
27  ezResourceBase* m_pResourceToLoad;
28  ezResourceTypeLoader* m_pLoader;
29 
30 private:
31  friend class ezResourceManager;
33 
34  virtual void Execute() override;
35 };
36 
37 class EZ_CORE_DLL ezResourceManager
38 {
39 public:
40  enum class ResourceEventType;
41  enum class ManagerEventType;
42  struct ResourceEvent;
43  struct ManagerEvent;
44 
45  static ezEvent<const ResourceEvent&> s_ResourceEvents;
46  static ezEvent<const ManagerEvent&> s_ManagerEvents;
47 
48  static void BroadcastResourceEvent(const ResourceEvent& e);
49 
50  template<typename ResourceType>
51  static ezResourceHandle<ResourceType> LoadResource(const char* szResourceID);
52 
53  template<typename ResourceType>
54  static ezResourceHandle<ResourceType> LoadResource(const char* szResourceID, ezResourcePriority Priority, ezResourceHandle<ResourceType> hFallbackResource);
55 
56  template<typename ResourceType>
57  static ezResourceHandle<ResourceType> CreateResource(const char* szResourceID, const typename ResourceType::DescriptorType& descriptor, const char* szResourceDescription = nullptr);
58 
59  template<typename ResourceType>
60  static ezResourceHandle<ResourceType> GetExistingResource(const char* szResourceID);
61 
62  template<typename ResourceType>
63  static ResourceType* BeginAcquireResource(const ezResourceHandle<ResourceType>& hResource, ezResourceAcquireMode mode = ezResourceAcquireMode::AllowFallback, const ezResourceHandle<ResourceType>& hFallbackResource = ezResourceHandle<ResourceType>(), ezResourcePriority Priority = ezResourcePriority::Unchanged);
64 
65  template<typename ResourceType>
66  static void EndAcquireResource(ResourceType* pResource);
67 
68  template<typename ResourceType>
69  static void SetResourceTypeLoader(ezResourceTypeLoader* pCreator);
70 
71  static void SetDefaultResourceLoader(ezResourceTypeLoader* pDefaultLoader);
72 
73  static ezResourceTypeLoader* GetDefaultResourceLoader() { return m_pDefaultResourceLoader; }
74 
75  static void OnEngineShutdown();
76 
77  static void OnCoreShutdown();
78 
79  static void OnCoreStartup();
80 
81  template<typename ResourceType>
82  static void PreloadResource(const ezResourceHandle<ResourceType>& hResource, ezTime tShouldBeAvailableIn);
83 
84  static ezUInt32 FreeUnusedResources(bool bFreeAllUnused);
85 
86  template<typename ResourceType>
87  static void ReloadResource(const ezResourceHandle<ResourceType>& hResource);
88 
89  template<typename ResourceType>
90  static void ReloadResourcesOfType();
91 
92  static void ReloadResourcesOfType(const ezRTTI* pType);
93 
94  static void ReloadAllResources();
95 
96  //static void CleanUpResources();
97 
98  static void PerFrameUpdate();
99 
100 
103  static void BroadcastExistsEvent();
104 
106  {
107  ezString m_sName;
108  ezUInt64 m_uiMemoryLimitCPU;
109  ezUInt64 m_uiMemoryLimitGPU;
110  ezAtomicInteger64 m_uiMemoryUsageCPU;
111  ezAtomicInteger64 m_uiMemoryUsageGPU;
112  };
113 
117  static void ConfigureResourceCategory(const char* szCategoryName, ezUInt64 uiMemoryLimitCPU, ezUInt64 uiMemoryLimitGPU);
118 
119  static const ResourceCategory& GetResourceCategory(const char* szCategoryName);
120 
121 private:
122  friend class ezResourceManagerWorker;
123  friend class ezResourceManagerWorkerGPU;
124 
125  static void ReloadResource(ezResourceBase* pResource);
126 
127  static void PreloadResource(ezResourceBase* pResource, ezTime tShouldBeAvailableIn);
128 
129  template<typename ResourceType>
130  static ResourceType* GetResource(const char* szResourceID, bool bIsReloadable);
131 
132  static void InternalPreloadResource(ezResourceBase* pResource, bool bHighestPriority);
133 
134  static void RunWorkerTask();
135 
136  static void UpdateLoadingDeadlines();
137 
138  static ezResourceTypeLoader* GetResourceTypeLoader(const ezRTTI* pRTTI);
139 
141  static ezMap<ezString, ezResourceTypeLoader*> m_ResourceTypeLoader;
142 
143  static ezResourceLoaderFromFile m_FileResourceLoader;
144 
145  static ezResourceTypeLoader* m_pDefaultResourceLoader;
146 
147  struct LoadingInfo
148  {
149  ezTime m_DueDate;
150  ezResourceBase* m_pResource;
151 
152  EZ_FORCE_INLINE bool operator==(const LoadingInfo& rhs) const
153  {
154  return m_pResource == rhs.m_pResource;
155  }
156 
157  inline bool operator<(const LoadingInfo& rhs) const
158  {
159  if (m_DueDate < rhs.m_DueDate)
160  return true;
161  if (m_DueDate > rhs.m_DueDate)
162  return false;
163 
164  return m_pResource < rhs.m_pResource;
165  }
166  };
167 
168  static ezDeque<LoadingInfo> m_RequireLoading;
169 
170  static bool m_bTaskRunning;
171  static bool m_bStop;
172  static ezResourceManagerWorker m_WorkerTask[2];
173  static ezResourceManagerWorkerGPU m_WorkerGPU[16];
174  static ezUInt8 m_iCurrentWorkerGPU;
175  static ezUInt8 m_iCurrentWorker;
176  static ezTime m_LastDeadLineUpdate;
177  static ezTime m_LastFrameUpdate;
178  static bool m_bBroadcastExistsEvent;
179  static ezHashTable<ezUInt32, ResourceCategory> m_ResourceCategories;
180  static ezMutex s_ResourceMutex;
181 };
182 
183 enum class ezResourceManager::ResourceEventType
184 {
185  ResourceExists,
186  ResourceCreated,
187  ResourceDeleted,
188  ResourceContentUpdated,
189  ResourceContentUnloaded,
190  ResourceInPreloadQueue,
191  ResourceOutOfPreloadQueue,
192  ResourcePriorityChanged,
193  ResourceDueDateChanged,
194 };
195 
196 enum class ezResourceManager::ManagerEventType
197 {
198  ManagerShuttingDown,
199  ResourceCategoryChanged,
200 };
201 
203 {
204  ResourceEventType m_EventType;
205  const ezResourceBase* m_pResource;
206 };
207 
209 {
210  ManagerEventType m_EventType;
211  const ResourceCategory* m_pCategory;
212 };
213 
218 template<class RESOURCE_TYPE>
220 {
221 public:
222  ezResourceLock(const ezResourceHandle<RESOURCE_TYPE>& hResource, ezResourceAcquireMode mode = ezResourceAcquireMode::AllowFallback, const ezResourceHandle<RESOURCE_TYPE>& hFallbackResource = ezResourceHandle<RESOURCE_TYPE>(), ezResourcePriority Priority = ezResourcePriority::Unchanged)
223  {
224  m_pResource = ezResourceManager::BeginAcquireResource(hResource, mode, hFallbackResource, Priority);
225  }
226 
227  ~ezResourceLock()
228  {
229  if (m_pResource)
230  ezResourceManager::EndAcquireResource(m_pResource);
231  }
232 
233  RESOURCE_TYPE* operator->()
234  {
235  return m_pResource;
236  }
237 
238 private:
239  RESOURCE_TYPE* m_pResource;
240 };
241 
242 #include <Core/ResourceManager/Implementation/ResourceManager_inl.h>
243 
244