Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ResourceCache.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_RESOURCE_CACHE_H_
34#define DVD_RESOURCE_CACHE_H_
35
36#include "Resource.h"
41
42namespace Divide
43{
45 {
46 public:
47 explicit ResourceLoadLock( size_t hash, PlatformContext& context );
49
50 private:
51 [[nodiscard]] static bool SetLoading( size_t hash );
52 [[nodiscard]] static bool SetLoadingFinished( size_t hash );
53
54 private:
55 const size_t _loadingHash;
56
58 static eastl::set<size_t> s_loadingHashes;
59 };
60
61 struct ResourcePoolBase;
62 class ResourceCache final : private NonMovable, private NonCopyable
63 {
64 public:
65 static void Init(RenderAPI renderAPI, PlatformContext& context);
66 static void Stop();
67 static void OnFrameStart();
68 static void OnFrameEnd();
69 static void PrintLeakedResources();
70
71 template <typename T> requires std::is_base_of_v<CachedResource, T>
72 [[nodiscard]] static T* Get( Handle<T> handle);
73
74 template <typename T> requires std::is_base_of_v<CachedResource, T>
75 static void Destroy( Handle<T>& handle, const bool immediate );
76
77 template <typename T> requires std::is_base_of_v<CachedResource, T>
78 [[nodiscard]] static Handle<T> LoadResource( const ResourceDescriptor<T>& descriptor, bool& wasInCache, std::atomic_uint& taskCounter );
79
80
81 template <typename T> requires std::is_base_of_v<CachedResource, T>
82 [[nodiscard]] static Handle<T> RetrieveFromCache( Handle<T> handle );
83
84 protected:
85 friend struct ResourcePoolBase;
86 static void RegisterPool( ResourcePoolBase* pool );
87
88 private:
89 template <typename T> requires std::is_base_of_v<CachedResource, T>
90 [[nodiscard]] static Handle<T> RetrieveOrAllocateHandle( size_t descriptorHash, bool& wasInCache );
91
92 template<typename T> requires std::is_base_of_v<Resource, T>
93 [[nodiscard]] static ResourcePtr<T> AllocateAndCommit( Handle<T> handle, const ResourceDescriptor<T>& descriptor );
94
95 template<typename T> requires std::is_base_of_v<Resource, T>
96 static ResourcePtr<T> Allocate( Handle<T> handle, const ResourceDescriptor<T>& descriptor, size_t descriptorHash );
97
98 template<typename T> requires std::is_base_of_v<Resource, T>
99 [[nodiscard]] static ResourcePtr<T> AllocateInternal( const ResourceDescriptor<T>& descriptor );
100
101 template<typename T> requires std::is_base_of_v<Resource, T>
102 static void Build( ResourcePtr<T> ptr, const ResourceDescriptor<T>& descriptor );
103
106
109 static bool s_enabled;
110 };
111
112 template <typename T> requires std::is_base_of_v<CachedResource, T>
113 [[nodiscard]] FORCE_INLINE Handle<T> CreateResource( const ResourceDescriptor<T>& descriptor, bool& wasInCache, std::atomic_uint& taskCounter )
114 {
115 return ResourceCache::LoadResource<T>( descriptor, wasInCache, taskCounter );
116 }
117
118 template <typename T> requires std::is_base_of_v<CachedResource, T>
119 [[nodiscard]] FORCE_INLINE Handle<T> CreateResource( const ResourceDescriptor<T>& descriptor, bool& wasInCache )
120 {
121 std::atomic_uint taskCounter = 0u;
122 return CreateResource( descriptor, wasInCache, taskCounter );
123 }
124
125 template <typename T> requires std::is_base_of_v<CachedResource, T>
126 [[nodiscard]] FORCE_INLINE Handle<T> CreateResource( const ResourceDescriptor<T>& descriptor, std::atomic_uint& taskCounter )
127 {
128 bool wasInCache = false;
129 return CreateResource( descriptor, wasInCache, taskCounter );
130 }
131
132 template <typename T> requires std::is_base_of_v<CachedResource, T>
134 {
135 bool wasInCache = false;
136 std::atomic_uint taskCounter = 0u;
137 return CreateResource(descriptor, wasInCache, taskCounter);
138 }
139
140 template <typename T> requires std::is_base_of_v<CachedResource, T>
141 [[nodiscard]] FORCE_INLINE Handle<T> GetResourceRef( const Handle<T> handle )
142 {
143 return ResourceCache::RetrieveFromCache<T>( handle );
144 }
145
146 template <typename T> requires std::is_base_of_v<CachedResource, T>
147 FORCE_INLINE void DestroyResource( Handle<T>& handle, const bool immediate = false)
148 {
149 ResourceCache::Destroy<T>(handle, immediate);
150 }
151
152 template <typename T> requires std::is_base_of_v<CachedResource, T>
153 [[nodiscard]] FORCE_INLINE T* Get( const Handle<T> handle )
154 {
155 return ResourceCache::Get( handle );
156 }
157} // namespace Divide
158
159#endif //DVD_RESOURCE_CACHE_H_
160
161#include "ResourceCache.inl"
#define FORCE_INLINE
static ResourcePtr< T > AllocateInternal(const ResourceDescriptor< T > &descriptor)
static Handle< T > RetrieveFromCache(Handle< T > handle)
static void OnFrameEnd()
static Mutex s_poolLock
static void Destroy(Handle< T > &handle, const bool immediate)
static vector< ResourcePoolBase * > s_resourcePools
static Handle< T > RetrieveOrAllocateHandle(size_t descriptorHash, bool &wasInCache)
static void OnFrameStart()
static T * Get(Handle< T > handle)
static void RegisterPool(ResourcePoolBase *pool)
static PlatformContext * s_context
static Handle< T > LoadResource(const ResourceDescriptor< T > &descriptor, bool &wasInCache, std::atomic_uint &taskCounter)
static ResourcePtr< T > Allocate(Handle< T > handle, const ResourceDescriptor< T > &descriptor, size_t descriptorHash)
static RenderAPI s_renderAPI
static void Init(RenderAPI renderAPI, PlatformContext &context)
static void PrintLeakedResources()
static void Build(ResourcePtr< T > ptr, const ResourceDescriptor< T > &descriptor)
static ResourcePtr< T > AllocateAndCommit(Handle< T > handle, const ResourceDescriptor< T > &descriptor)
static eastl::set< size_t > s_loadingHashes
Definition: ResourceCache.h:58
static SharedMutex s_hashLock
Definition: ResourceCache.h:57
static bool SetLoadingFinished(size_t hash)
static bool SetLoading(size_t hash)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
FORCE_INLINE void DestroyResource(Handle< T > &handle, const bool immediate=false)
std::mutex Mutex
Definition: SharedMutex.h:40
T * ResourcePtr
Definition: Resource.h:112
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
FORCE_INLINE Handle< T > GetResourceRef(const Handle< T > handle)
eastl::vector< Type > vector
Definition: Vector.h:42
FORCE_INLINE Handle< T > CreateResource(const ResourceDescriptor< T > &descriptor, bool &wasInCache, std::atomic_uint &taskCounter)
FORCE_INLINE T * Get(const Handle< T > handle)