Skip to content

Commit

Permalink
Fix Clang build and clean up some unused params in resource construction
Browse files Browse the repository at this point in the history
  • Loading branch information
IonutCava committed May 15, 2024
1 parent b73c8d2 commit 9f2b6c9
Show file tree
Hide file tree
Showing 53 changed files with 500 additions and 351 deletions.
9 changes: 4 additions & 5 deletions Localisation/enGB.ini
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,13 @@ ERROR_XML_INVALID_MAG_FILTER = Invalid mag filter parameter for [ {} ]
STOP_RESOURCE_CACHE = Emptying the resource cache ...
WARN_RESOURCE_LEAKED = Resource not unloaded properly at shutdown: [ {} : {} ]
RESOURCE_CACHE_ALLOCATE = Allocating resource [{}] of type [ {} ]. GUID: [ {} ]. Hash: [{}]. Total load time: [ {:5.2f}ms ].
RESOURCE_CACHE_ALLOCATE_FAILED = Allocating resource [ {} ] failed!
RESOURCE_CACHE_ALLOCATE_FAILED = Allocating resource [ {} ] failed! Total load time: [ {:5.2f}ms ]
RESOURCE_CACHE_BUILD = Building resource [{}] of type [ {} ]. GUID: [ {} ]. Hash: [{}]. Total load time: [ {:5.2f}ms ].
RESOURCE_CACHE_BUILD_FAILED = Resource [ {} ] building failed!
RESOURCE_CACHE_GET_RES_INC = Returning resource [ {} ]. Ref count: [ {} ]
RESOURCE_CACHE_REM_RES_DEC = Reducing resource [ {} ] reference count: [{}]
RESOURCE_CACHE_BUILD_FAILED = Resource [ {} ] building failed! Total load time: [ {:5.2f}ms ]
RESOURCE_CACHE_GET_RES_INC = Increasing ref count for resource [ {} ] to [ {} ]
RESOURCE_CACHE_REM_RES_DEC = Decreasing ref count for resource [ {} ] to [ {} ]
RESOURCE_CACHE_GET_RES = Loading resource [ {} ] [ {} ]
RESOURCE_CACHE_REM_RES = Removing resource: [ {} ] [ {} ]
RESOURCE_CACHE_RETRIEVE = Resource [ {} ] was retrieved from cache. Ref count: [ {} ]
ERROR_RESOURCE_CACHE_LOAD_RES = Resource creation failed!
RESOURCE_CACHE_POOL_TYPE = Resource pool of type [ {} ].
ERROR_RESOURCE_CACHE_LOAD_RES_NAME = Resource [ {} ]creation failed!
Expand Down
15 changes: 12 additions & 3 deletions Source/Core/Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ void Kernel::onLoop()
{
Time::ScopedTimer timer(_appLoopTimerMain);

if (!keepAlive()) {
ResourceCache::OnFrameStart();
SCOPE_EXIT
{
ResourceCache::OnFrameEnd();
};

if (!keepAlive())
{
// exiting the rendering loop will return us to the last control point
_platformContext.app().mainLoopActive(false);

Expand All @@ -196,7 +203,8 @@ void Kernel::onLoop()
return;
}

if constexpr(!Config::Build::IS_SHIPPING_BUILD) {
if constexpr(!Config::Build::IS_SHIPPING_BUILD)
{
// Check for any file changes (shaders, scripts, etc)
FileWatcherManager::update();
}
Expand Down Expand Up @@ -942,13 +950,14 @@ void Kernel::shutdown()
_renderPassManager.reset();

SceneEnvironmentProbePool::OnShutdown(_platformContext.gfx());
ResourceCache::Stop();
_platformContext.terminate();
Camera::DestroyPool();
ResourceCache::Clear();
for ( U8 i = 0u; i < to_U8( TaskPoolType::COUNT ); ++i )
{
_platformContext.taskPool( static_cast<TaskPoolType>(i) ).shutdown();
}
ResourceCache::PrintLeakedResources();
Console::printfn(LOCALE_STR("STOP_ENGINE_OK"));
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Resources/Headers/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ namespace Divide
explicit Resource( std::string_view resourceName, std::string_view typeName );

[[nodiscard]] ResourceState getState() const noexcept;
void waitForReady() const;

bool safeToDelete();

PROPERTY_R( Str<32>, typeName );
PROPERTY_R( Str<256>, resourceName );
Expand All @@ -84,6 +81,9 @@ namespace Divide
std::atomic<ResourceState> _resourceState;
};

void WaitForReady( Resource* res );
[[nodiscard]] bool SafeToDelete( Resource* res );

struct ResourceDescriptorBase;

class CachedResource : public Resource
Expand Down
33 changes: 18 additions & 15 deletions Source/Core/Resources/Headers/ResourceCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,44 @@ namespace Divide
static eastl::set<size_t> s_loadingHashes;
};

class ResourceCache final : private NonMovable
class ResourceCache final : private NonMovable, private NonCopyable
{
public:
static void Init(RenderAPI renderAPI, PlatformContext& context);
static void Clear();
static void Stop();
static void OnFrameStart();
static void OnFrameEnd();
static void PrintLeakedResources();

template <typename T> requires std::is_base_of_v<CachedResource, T>
[[nodiscard]] static T* Get( Handle<T> handle);

template <typename T> requires std::is_base_of_v<CachedResource, T>
static void Destroy( Handle<T>& handle );
static void Destroy( Handle<T>& handle, const bool immediate );

template <typename T> requires std::is_base_of_v<CachedResource, T>
[[nodiscard]] static Handle<T> LoadResource( const ResourceDescriptor<T>& descriptor, bool& wasInCache, std::atomic_uint& taskCounter );


template <typename T> requires std::is_base_of_v<CachedResource, T>
[[nodiscard]] static Handle<T> RetrieveFromCache( size_t descriptorHash, bool& wasInCache );
[[nodiscard]] static Handle<T> RetrieveFromCache( Handle<T> handle );

protected:
friend struct ResourcePoolBase;
static void RegisterPool( ResourcePoolBase* pool );

private:
template <typename T> requires std::is_base_of_v<CachedResource, T>
[[nodiscard]] static Handle<T> RetrieveOrAllocateHandle( size_t descriptorHash, bool& wasInCache );

template<typename Base, typename Derived> requires std::is_base_of_v<Resource, Base> && std::is_base_of_v<Base, Derived>
[[nodiscard]] static ResourcePtr<Base> AllocateInternal( const ResourceDescriptor<Base>& descriptor );

template<typename Base, typename Derived> requires std::is_base_of_v<Resource, Base> && std::is_base_of_v<Base, Derived>
[[nodiscard]] static void BuildInternal( ResourcePtr<Base> ptr );
template<typename T> requires std::is_base_of_v<Resource, T>
[[nodiscard]] static ResourcePtr<T> AllocateAndCommit( Handle<T> handle, const ResourceDescriptor<T>& descriptor );

template<typename T> requires std::is_base_of_v<Resource, T>
static ResourcePtr<T> Allocate( Handle<T> handle, const ResourceDescriptor<T>& descriptor, size_t descriptorHash );

template<typename T> requires std::is_base_of_v<Resource, T>
static void Deallocate(Handle<T> handle);
[[nodiscard]] static ResourcePtr<T> AllocateInternal( const ResourceDescriptor<T>& descriptor );

template<typename T> requires std::is_base_of_v<Resource, T>
static void Build( ResourcePtr<T> ptr, const ResourceDescriptor<T>& descriptor );
Expand All @@ -102,6 +105,7 @@ namespace Divide

static PlatformContext* s_context;
static RenderAPI s_renderAPI;
static bool s_enabled;
};

template <typename T> requires std::is_base_of_v<CachedResource, T>
Expand Down Expand Up @@ -133,16 +137,15 @@ namespace Divide
}

template <typename T> requires std::is_base_of_v<CachedResource, T>
[[nodiscard]] FORCE_INLINE Handle<T> GetResourceRef( const size_t descriptorHash )
[[nodiscard]] FORCE_INLINE Handle<T> GetResourceRef( const Handle<T> handle )
{
bool wasInCache = false;
return ResourceCache::RetrieveFromCache<T>(descriptorHash, wasInCache);
return ResourceCache::RetrieveFromCache<T>( handle );
}

template <typename T> requires std::is_base_of_v<CachedResource, T>
FORCE_INLINE void DestroyResource( Handle<T>& handle)
FORCE_INLINE void DestroyResource( Handle<T>& handle, const bool immediate = false)
{
ResourceCache::Destroy<T>(handle);
ResourceCache::Destroy<T>(handle, immediate);
}

template <typename T> requires std::is_base_of_v<CachedResource, T>
Expand Down

0 comments on commit 9f2b6c9

Please sign in to comment.