Completed initial implementation of SHResourceManager
This commit is contained in:
parent
2cf9c87509
commit
7e15edb052
|
@ -64,7 +64,7 @@ namespace Sandbox
|
||||||
std::vector<Handle<SHTexture>> texHandles;
|
std::vector<Handle<SHTexture>> texHandles;
|
||||||
for (const auto& tex : textures)
|
for (const auto& tex : textures)
|
||||||
{
|
{
|
||||||
auto texture = graphicsSystem->Add(tex);
|
auto texture = graphicsSystem->AddTexture(tex);
|
||||||
texHandles.push_back(texture);
|
texHandles.push_back(texture);
|
||||||
}
|
}
|
||||||
graphicsSystem->BuildTextures();
|
graphicsSystem->BuildTextures();
|
||||||
|
|
|
@ -62,6 +62,11 @@ namespace SHADE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SHAssetManager::Unload(AssetID assetId) noexcept
|
||||||
|
{
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
AssetPath SHAssetManager::GenerateLocalPath(AssetPath path) noexcept
|
AssetPath SHAssetManager::GenerateLocalPath(AssetPath path) noexcept
|
||||||
{
|
{
|
||||||
if (!IsRecognised(path.extension().string().c_str()))
|
if (!IsRecognised(path.extension().string().c_str()))
|
||||||
|
|
|
@ -32,6 +32,7 @@ namespace SHADE
|
||||||
* \brief Deallocate all memory used by resource data
|
* \brief Deallocate all memory used by resource data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
static void Unload() noexcept;
|
static void Unload() noexcept;
|
||||||
|
static void Unload(AssetID assetId) noexcept;
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* \brief Load all resources that are in the folder
|
* \brief Load all resources that are in the folder
|
||||||
|
|
|
@ -585,19 +585,19 @@ namespace SHADE
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Texture Registration Functions */
|
/* Texture Registration Functions */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
Handle<SHTexture> SHGraphicsSystem::Add(const SHTextureAsset& texAsset)
|
Handle<SHTexture> SHGraphicsSystem::AddTexture(const SHTextureAsset& texAsset)
|
||||||
{
|
{
|
||||||
auto sampler = samplerCache.GetSampler(device, SHVkSamplerParams { .maxLod = static_cast<float>(texAsset.mipOffsets.size()) });
|
auto sampler = samplerCache.GetSampler(device, SHVkSamplerParams { .maxLod = static_cast<float>(texAsset.mipOffsets.size()) });
|
||||||
return texLibrary.Add(texAsset, sampler);
|
return texLibrary.Add(texAsset, sampler);
|
||||||
}
|
}
|
||||||
|
|
||||||
SHADE::Handle<SHADE::SHTexture> SHGraphicsSystem::Add(uint32_t pixelCount, const SHTexture::PixelChannel* const pixelData, uint32_t width, uint32_t height, SHTexture::TextureFormat format, std::vector<uint32_t> mipOffsets)
|
SHADE::Handle<SHADE::SHTexture> SHGraphicsSystem::AddTexture(uint32_t pixelCount, const SHTexture::PixelChannel* const pixelData, uint32_t width, uint32_t height, SHTexture::TextureFormat format, std::vector<uint32_t> mipOffsets)
|
||||||
{
|
{
|
||||||
auto sampler = samplerCache.GetSampler(device, SHVkSamplerParams{ .maxLod = static_cast<float>(mipOffsets.size()) });
|
auto sampler = samplerCache.GetSampler(device, SHVkSamplerParams{ .maxLod = static_cast<float>(mipOffsets.size()) });
|
||||||
return texLibrary.Add(pixelCount, pixelData, width, height, format, mipOffsets, sampler);
|
return texLibrary.Add(pixelCount, pixelData, width, height, format, mipOffsets, sampler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHGraphicsSystem::Remove(Handle<SHTexture> tex)
|
void SHGraphicsSystem::RemoveTexture(Handle<SHTexture> tex)
|
||||||
{
|
{
|
||||||
texLibrary.Remove(tex);
|
texLibrary.Remove(tex);
|
||||||
}
|
}
|
||||||
|
|
|
@ -231,8 +231,8 @@ namespace SHADE
|
||||||
|
|
||||||
*/
|
*/
|
||||||
/*******************************************************************************/
|
/*******************************************************************************/
|
||||||
Handle<SHTexture> Add(const SHTextureAsset& texAsset);
|
Handle<SHTexture> AddTexture(const SHTextureAsset& texAsset);
|
||||||
Handle<SHTexture> Add(uint32_t pixelCount, const SHTexture::PixelChannel* const pixelData, uint32_t width, uint32_t height, SHTexture::TextureFormat format, std::vector<uint32_t> mipOffsets);
|
Handle<SHTexture> AddTexture(uint32_t pixelCount, const SHTexture::PixelChannel* const pixelData, uint32_t width, uint32_t height, SHTexture::TextureFormat format, std::vector<uint32_t> mipOffsets);
|
||||||
/*******************************************************************************/
|
/*******************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ namespace SHADE
|
||||||
|
|
||||||
*/
|
*/
|
||||||
/*******************************************************************************/
|
/*******************************************************************************/
|
||||||
void Remove(Handle<SHTexture> tex);
|
void RemoveTexture(Handle<SHTexture> tex);
|
||||||
/***************************************************************************/
|
/***************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
/************************************************************************************//*!
|
||||||
|
\file SHResourceManager.cpp
|
||||||
|
\author Tng Kah Wei, kahwei.tng, 390009620
|
||||||
|
\par email: kahwei.tng\@digipen.edu
|
||||||
|
\date Oct 21, 2022
|
||||||
|
\brief Contains the definition of the functions of the SHResourceManager static
|
||||||
|
class.
|
||||||
|
|
||||||
|
Copyright (C) 2022 DigiPen Institute of Technology.
|
||||||
|
Reproduction or disclosure of this file or its contents without the prior written consent
|
||||||
|
of DigiPen Institute of Technology is prohibited.
|
||||||
|
*//*************************************************************************************/
|
||||||
|
#include "SHpch.h"
|
||||||
|
// Primary Include
|
||||||
|
#include "SHResourceManager.h"
|
||||||
|
|
||||||
|
namespace SHADE
|
||||||
|
{
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* Static Data Member Definitions */
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
SHResourceHub SHResourceManager::resourceHub;
|
||||||
|
std::unordered_map<std::type_index, std::unordered_map<AssetID, SHADE::HandleBase>> SHResourceManager::handlesMap;
|
||||||
|
std::unordered_map<std::type_index, std::function<void(AssetID)>> SHResourceManager::typedFreeFuncMap;
|
||||||
|
std::vector<AssetID> SHResourceManager::loadedAssetData;
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* Function Definitions */
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
void SHResourceManager::Unload(AssetID assetId)
|
||||||
|
{
|
||||||
|
// Search each library for the asset ID and try to free it
|
||||||
|
for (auto& typedHandleMap : handlesMap)
|
||||||
|
{
|
||||||
|
if (typedHandleMap.second.contains(assetId))
|
||||||
|
{
|
||||||
|
// Dispose
|
||||||
|
typedFreeFuncMap[typedHandleMap.first](assetId);
|
||||||
|
typedHandleMap.second.erase(assetId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHResourceManager::FinaliseChanges()
|
||||||
|
{
|
||||||
|
SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
|
||||||
|
if (gfxSystem == nullptr)
|
||||||
|
throw std::runtime_error("[SHResourceManager] Attempted to load graphics resource without a SHGraphicsSystem installed.");
|
||||||
|
gfxSystem->BuildMeshBuffers();
|
||||||
|
gfxSystem->BuildTextures();
|
||||||
|
|
||||||
|
// Free CPU Resources
|
||||||
|
for (auto assetId : loadedAssetData)
|
||||||
|
{
|
||||||
|
SHAssetManager::Unload(assetId);
|
||||||
|
}
|
||||||
|
loadedAssetData.clear();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,99 @@
|
||||||
|
/************************************************************************************//*!
|
||||||
|
\file SHResourceManager.h
|
||||||
|
\author Tng Kah Wei, kahwei.tng, 390009620
|
||||||
|
\par email: kahwei.tng\@digipen.edu
|
||||||
|
\date Oct 21, 2022
|
||||||
|
\brief Contains the definition of the SHResourceManager static class.
|
||||||
|
|
||||||
|
Copyright (C) 2022 DigiPen Institute of Technology.
|
||||||
|
Reproduction or disclosure of this file or its contents without the prior written consent
|
||||||
|
of DigiPen Institute of Technology is prohibited.
|
||||||
|
*//*************************************************************************************/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// STL Includes
|
||||||
|
#include <unordered_map>
|
||||||
|
// Project Includes
|
||||||
|
#include "SH_API.h"
|
||||||
|
#include "SHResourceLibrary.h"
|
||||||
|
#include "Assets/SHAssetMacros.h"
|
||||||
|
|
||||||
|
namespace SHADE
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Static class responsible for loading and caching runtime resources from their
|
||||||
|
/// serialised Asset IDs.
|
||||||
|
/// </summary>
|
||||||
|
class SH_API SHResourceManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
/* Loading Functions */
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
/// <summary>
|
||||||
|
/// Loads or retrieves an existing loaded object of the specified type with the
|
||||||
|
/// specified asset ID.
|
||||||
|
/// Note that for specific types, the retrieved Handle may not be valid until after
|
||||||
|
/// FinaliseChanges() is called.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="ResourceType">
|
||||||
|
/// Type of resource to load.
|
||||||
|
/// </typeparam>
|
||||||
|
/// <param name="assetId">Asset ID of the resource to load.</param>
|
||||||
|
/// <returns>Handle to a loaded runtime asset.</returns>
|
||||||
|
template<typename ResourceType>
|
||||||
|
static Handle<ResourceType> LoadOrGet(AssetID assetId);
|
||||||
|
/// <summary>
|
||||||
|
/// Unloads an existing loaded asset. Attempting to unload an invalid Handle will
|
||||||
|
/// simply do nothing except emit a warning.
|
||||||
|
/// Faster than the untemplated version.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="ResourceType">Type of resource to unload.</typeparam>
|
||||||
|
/// <param name="assetId">Handle to the resource to unload.</param>
|
||||||
|
template<typename ResourceType>
|
||||||
|
static void Unload(Handle<ResourceType> assetId);
|
||||||
|
/// <summary>
|
||||||
|
/// Unloads an existing loaded asset. Attempting to unload an invalid Handle will
|
||||||
|
/// simply do nothing except emit a warning.
|
||||||
|
/// Compared to the templated version, this function is slower as it requires
|
||||||
|
/// searching through the storage of all resource types.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assetId">Handle to the resource to unload.</param>
|
||||||
|
static void Unload(AssetID assetId);
|
||||||
|
/// <summary>
|
||||||
|
/// Needs to be called to finalise all changes to loads.
|
||||||
|
/// </summary>
|
||||||
|
static void FinaliseChanges();
|
||||||
|
|
||||||
|
private:
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
/* Type Definitions */
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
using AssetHandleMap = std::unordered_map<AssetID, HandleBase>;
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
/* Data Members */
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
// Handles
|
||||||
|
static SHResourceHub resourceHub;
|
||||||
|
static std::unordered_map<std::type_index, AssetHandleMap> handlesMap;
|
||||||
|
static std::unordered_map<std::type_index, std::function<void(AssetID)>> typedFreeFuncMap;
|
||||||
|
// Pointers to temp CPU resources
|
||||||
|
static std::vector<AssetID> loadedAssetData;
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
/* Helper Functions */
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves or creates the AssetHandleMap for the specific type if it doesn't exist
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="ResourceType">
|
||||||
|
/// The type of AssetHandleMap to retrieve.
|
||||||
|
/// </typeparam>
|
||||||
|
/// <returns>Reference to the AssetHandleMap of the specified type.</returns>
|
||||||
|
template<typename ResourceType>
|
||||||
|
static AssetHandleMap& getAssetHandleMap();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "SHResourceManager.hpp"
|
|
@ -0,0 +1,147 @@
|
||||||
|
/************************************************************************************//*!
|
||||||
|
\file SHResourceManager.hpp
|
||||||
|
\author Tng Kah Wei, kahwei.tng, 390009620
|
||||||
|
\par email: kahwei.tng\@digipen.edu
|
||||||
|
\date Oct 21, 2022
|
||||||
|
\brief Contains the definition of the function templates of the
|
||||||
|
SHResourceManager static class.
|
||||||
|
|
||||||
|
Copyright (C) 2022 DigiPen Institute of Technology.
|
||||||
|
Reproduction or disclosure of this file or its contents without the prior written consent
|
||||||
|
of DigiPen Institute of Technology is prohibited.
|
||||||
|
*//*************************************************************************************/
|
||||||
|
#pragma once
|
||||||
|
// Primary Include
|
||||||
|
#include "SHResourceManager.h"
|
||||||
|
// Project Includes
|
||||||
|
#include "Assets/SHAssetManager.h"
|
||||||
|
#include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h"
|
||||||
|
#include "ECS_Base/Managers/SHSystemManager.h"
|
||||||
|
#include "Tools/SHLog.h"
|
||||||
|
|
||||||
|
namespace SHADE
|
||||||
|
{
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* Loading Functions */
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
template<typename ResourceType>
|
||||||
|
Handle<ResourceType> SHResourceManager::LoadOrGet(AssetID assetId)
|
||||||
|
{
|
||||||
|
// Check if it is an unsupported type
|
||||||
|
if (!std::is_same_v<ResourceType, SHMesh> && !std::is_same_v<ResourceType, SHTexture>)
|
||||||
|
{
|
||||||
|
static_assert(true, "Unsupported Resource Type specified for SHResourceManager.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Attempt to get existing loaded asset */
|
||||||
|
AssetHandleMap& typedHandleMap = getAssetHandleMap<ResourceType>();
|
||||||
|
if (typedHandleMap.contains(assetId))
|
||||||
|
return typedHandleMap[assetId];
|
||||||
|
|
||||||
|
/* Otherwise, we need to load it! */
|
||||||
|
// Meshes
|
||||||
|
if constexpr (std::is_same_v<ResourceType, SHMesh>)
|
||||||
|
{
|
||||||
|
// Get system
|
||||||
|
SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
|
||||||
|
if (gfxSystem == nullptr)
|
||||||
|
throw std::runtime_error("[SHResourceManager] Attempted to load graphics resource without a SHGraphicsSystem installed.");
|
||||||
|
|
||||||
|
// Load
|
||||||
|
SHMeshAsset* assetData = SHAssetManager::GetMesh(assetId);
|
||||||
|
if (assetData == nullptr)
|
||||||
|
{
|
||||||
|
SHLog::Warning("[SHResourceManager] Attempted to load an asset with an invalid Asset ID.");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
loadedAssetData.emplace_back(assetId);
|
||||||
|
|
||||||
|
Handle<SHMesh> meshHandle = gfxSystem->AddMesh
|
||||||
|
(
|
||||||
|
assetData->vertexPosition.size(),
|
||||||
|
assetData->vertexPosition.data(),
|
||||||
|
assetData->texCoords.data(),
|
||||||
|
assetData->vertexTangent.data(),
|
||||||
|
assetData->vertexNormal.data(),
|
||||||
|
assetData->indices.size(),
|
||||||
|
assetData->indices.data()
|
||||||
|
);
|
||||||
|
typedHandleMap.insert(assetId, meshHandle);
|
||||||
|
return meshHandle;
|
||||||
|
}
|
||||||
|
// Textures
|
||||||
|
else if constexpr (std::is_same_v<ResourceType, SHTexture>)
|
||||||
|
{
|
||||||
|
// Get system
|
||||||
|
SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
|
||||||
|
if (gfxSystem == nullptr)
|
||||||
|
throw std::runtime_error("[SHResourceManager] Attempted to load graphics resource without a SHGraphicsSystem installed.");
|
||||||
|
|
||||||
|
// Load
|
||||||
|
SHTextureAsset* assetData = SHAssetManager::GetTexture(assetId);
|
||||||
|
if (assetData == nullptr)
|
||||||
|
{
|
||||||
|
SHLog::Warning("[SHResourceManager] Attempted to load an asset with an invalid Asset ID.");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
loadedAssetData.emplace_back(assetId);
|
||||||
|
|
||||||
|
Handle<SHTexture> texHandle = gfxSystem->AddTexture
|
||||||
|
(
|
||||||
|
assetData->numBytes,
|
||||||
|
assetData->pixelData,
|
||||||
|
assetData->width,
|
||||||
|
assetData->height,
|
||||||
|
assetData->format,
|
||||||
|
assetData->mipOffsets
|
||||||
|
);
|
||||||
|
typedHandleMap.insert(assetId, texHandle);
|
||||||
|
return texHandle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ResourceType>
|
||||||
|
void SHResourceManager::Unload(Handle<ResourceType> assetId)
|
||||||
|
{
|
||||||
|
// Check if it is an usupported type
|
||||||
|
if (!std::is_same_v<ResourceType, SHMesh> && !std::is_same_v<ResourceType, SHTexture>)
|
||||||
|
{
|
||||||
|
static_assert(true, "Unsupported Resource Type specified for SHResourceManager.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Attempt to get existing loaded asset */
|
||||||
|
AssetHandleMap& typedHandleMap = getAssetHandleMap<ResourceType>();
|
||||||
|
if (typedHandleMap.contains(assetId))
|
||||||
|
{
|
||||||
|
// Dispose
|
||||||
|
static_cast<Handle<ResourceType>>(typedHandleMap[assetId]).Free();
|
||||||
|
typedHandleMap.erase(assetId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// There's nothing to remove
|
||||||
|
SHLog::Warning("[SHResourceManager] Attempted to unload an invalid resource. Ignoring.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* Helper Functions */
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
template<typename ResourceType>
|
||||||
|
SHResourceManager::AssetHandleMap& SHResourceManager::getAssetHandleMap()
|
||||||
|
{
|
||||||
|
if (!handlesMap.contains(typeid(ResourceType)))
|
||||||
|
{
|
||||||
|
handlesMap.insert(typeid(ResourceType));
|
||||||
|
typedFreeFuncMap.insert
|
||||||
|
(
|
||||||
|
typeid(ResourceType),
|
||||||
|
[](AssetID assetId)
|
||||||
|
{
|
||||||
|
static_cast<Handle<ResourceType>>(SHResourceManager::handlesMap[typeid(ResourceType)][assetId]).Free();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return handlesMap[typeid(ResourceType)];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue