35#ifndef DVD_RESOURCE_CACHE_INL_
36#define DVD_RESOURCE_CACHE_INL_
92 eastl::fixed_vector<Entry, ResourcePoolSize, true>
_resPool;
101 template <
typename T>
requires std::is_base_of_v<CachedResource, T>
104 template <
typename T>
requires std::is_base_of_v<CachedResource, T>
111 template <
typename T>
requires std::is_base_of_v<CachedResource, T>
122 if ( handle != INVALID_HANDLE<T> )
124 _deletionQueue.enqueue(handle);
125 handle = INVALID_HANDLE<T>;
133 while (_deletionQueue.try_dequeue( handle ))
135 deallocate( handle );
145 const size_t poolSize = _freeList.size();
146 for (
size_t i = 0u; i < poolSize; ++i)
148 if (!_freeList[i].first)
185 return _resPool[handle.
_index]._ptr;
199 _freeList.resize( size, std::make_pair(
true, 0u ) );
200 _resPool.resize( size, {} );
207 for (
auto& it : _freeList )
214 entry._descriptorHash = descriptorHash;
215 entry._refCount = 1u;
223 resize( _freeList.size() + ResourcePoolSize );
225 return allocateLocked(descriptorHash);
232 return allocateLocked( descriptorHash );
235 template <
typename T>
238 GetMemPool<T>().deleteElement( ptr );
265 template <
typename T>
268 if ( handle == INVALID_HANDLE<T> )
275 size_t descriptorHash = 0u;
286 if ( --
entry._refCount == 0u)
289 descriptorHash =
entry._descriptorHash;
292 ++_freeList[handle.
_index].second;
293 _freeList[handle.
_index].first =
true;
300 handle = INVALID_HANDLE<T>;
302 if ( ptr !=
nullptr )
321 deallocateInternal( ptr );
329 for (
const auto&[free, generation] : _freeList )
335 if (
entry._descriptorHash == descriptorHash )
337 ret._generation = generation;
347 return INVALID_HANDLE<T>;
350 template <
typename T>
353 _resPool[handle.
_index]._ptr = ptr;
356 template <
typename T>
requires std::is_base_of_v<CachedResource, T>
359 if ( handle != INVALID_HANDLE<T>)
374 template<
typename T>
requires std::is_base_of_v<CachedResource, T>
381 if ( ret != INVALID_HANDLE<T> )
391 if ( ret != INVALID_HANDLE<T> )
402 template <
typename T>
requires std::is_base_of_v<CachedResource, T>
405 if ( handle != INVALID_HANDLE<T> ) [[likely]]
418 template <
typename T>
requires std::is_base_of_v<CachedResource, T>
421 if ( handle == INVALID_HANDLE<T> ) [[unlikely]]
428 if ( immediate || !
s_enabled ) [[unlikely]]
439 template<
typename T>
requires std::is_base_of_v<Resource, T>
442 return GetMemPool<T>().newElement( descriptor );
461 inline ResourcePtr<Texture> ResourceCache::AllocateInternal<Texture>(
const ResourceDescriptor<Texture>& descriptor )
474 template<
typename T>
requires std::is_base_of_v<Resource, T>
481 if ( ptr !=
nullptr )
489 template<
typename T>
requires std::is_base_of_v<Resource, T>
495 if ( ptr !=
nullptr )
499 if ( ptr->load( *
s_context ) ) [[likely]]
509 const F32 durationMS = Time::MicrosecondsToMilliseconds<F32>( loadTimer.get() );
514 descriptor.resourceName(),
523 descriptor.resourceName(),
528 template<
typename T>
requires std::is_base_of_v<Resource, T>
535 const F32 durationMS = Time::MicrosecondsToMilliseconds<F32>( loadTimer.get() );
537 if ( ptr !=
nullptr ) [[likely]]
540 descriptor.resourceName(),
549 descriptor.resourceName(),
556 template <
typename T>
requires std::is_base_of_v<CachedResource, T>
561 taskCounter.fetch_add( 1u );
564 const size_t loadingHash = descriptor.
getHash();
572 Handle<T> ret = RetrieveOrAllocateHandle<T>( loadingHash, wasInCache );
575 taskCounter.fetch_sub( 1u );
581 ResourcePtr<T> ptr = ResourceCache::Allocate<T>(ret, descriptor, loadingHash);
583 if ( ptr !=
nullptr )
587 ResourceCache::Build<T>( ptr, descriptor );
591 [ptr, ret, &taskCounter, resName = descriptor.resourceName()]()
606 taskCounter.fetch_sub( 1u );
TaskPool & taskPool(const TaskPoolType type) noexcept
static ResourcePtr< T > AllocateInternal(const ResourceDescriptor< T > &descriptor)
static Handle< T > RetrieveFromCache(Handle< T > handle)
static void Destroy(Handle< T > &handle, const bool immediate)
static Handle< T > RetrieveOrAllocateHandle(size_t descriptorHash, bool &wasInCache)
static T * Get(Handle< T > handle)
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 Build(ResourcePtr< T > ptr, const ResourceDescriptor< T > &descriptor)
static ResourcePtr< T > AllocateAndCommit(Handle< T > handle, const ResourceDescriptor< T > &descriptor)
Handle console commands that start with a forward slash.
std::lock_guard< mutex > LockGuard
bool SafeToDelete(Resource *res)
ResourcePool< T > & GetPool(const RenderAPI api)
MemPool< T > & GetMemPool()
@ RES_UNLOADING
The resource is unloading, deleting data, etc.
@ RES_LOADED
The resource is available for usage.
@ RES_THREAD_LOADED
The resource is loaded but not yet available.
@ RES_CREATED
The pointer has been created and instantiated, but no data has been loaded.
@ RES_LOADING
The resource is loading, creating data, parsing scripts, etc.
@ RES_UNKNOWN
The resource exists, but it's state is undefined.
Task * CreateTask(Predicate &&threadedFunction, bool allowedInIdle=true)
std::shared_mutex SharedMutex
std::unique_lock< mutex > UniqueLock
constexpr U32 prevPOW2(U32 n) noexcept
std::shared_lock< mutex > SharedLock
MemoryPool< T, prevPOW2(sizeof(T)) *1u<< 5u > MemPool
@ Vulkan
not supported yet
@ None
No rendering. Used for testing or server code.
void Start(Task &task, TaskPool &pool, TaskPriority priority=TaskPriority::DONT_CARE, const DELEGATE< void > &onCompletionFunction={})
Project const SceneEntry & entry
static NO_INLINE void errorfn(const char *format, T &&... args)
static NO_INLINE void printfn(const char *format, T &&... args)
size_t getHash() const final
virtual ~ResourcePoolBase()
virtual void printResources(bool error)=0
virtual void processDeletionQueue()=0
void deallocate(Handle< T > &handle)
eastl::fixed_vector< Entry, ResourcePoolSize, true > _resPool
void processDeletionQueue() override
void deallocateInternal(ResourcePtr< T > ptr)
static constexpr size_t ResourcePoolSize
Handle< T > retrieveHandleLocked(const size_t descriptorHash)
ResourcePtr< T > get(Handle< T > handle)
eastl::fixed_vector< std::pair< bool, U8 >, ResourcePoolSize, true > _freeList
moodycamel::ConcurrentQueue< Handle< T > > _deletionQueue
void queueDeletion(Handle< T > &handle)
void commitLocked(Handle< T > handle, ResourcePtr< T > ptr)
ResourcePool(RenderAPI api)
Handle< T > allocateLocked(size_t descriptorHash)
Handle< T > allocate(size_t descriptorHash)
void printResources(bool error) final