Merge branch 'SP3-13-ResourceManager' into SP3-1-SerialisedRenderable

This commit is contained in:
Kah Wei 2022-10-21 17:12:00 +08:00
commit c47a68947a
64 changed files with 655 additions and 146 deletions

View File

@ -1,4 +1,4 @@
#include "SBpch.h"
#include "SBpch.h"
#include "SBTestScene.h"
#include "ECS_Base/Managers/SHSystemManager.h"
@ -14,6 +14,7 @@
#include "Physics/Components/SHColliderComponent.h"
#include "Assets/SHAssetManager.h"
#include "Resource/SHResourceManager.h"
using namespace SHADE;
@ -40,34 +41,22 @@ namespace Sandbox
const auto CUBE_MESH = SHADE::SHPrimitiveGenerator::Cube(*graphicsSystem);
//Test Racoon mesh
auto meshes = SHADE::SHAssetManager::GetAllMeshes();
std::vector<Handle<SHMesh>> handles;
for (auto const& mesh : meshes)
std::vector<Handle<SHTexture>> texHandles;
for (const auto& asset : SHAssetManager::GetAllAssets())
{
if (mesh.header.meshName == "Cube.012")
switch (asset.type)
{
handles.push_back(graphicsSystem->AddMesh(
mesh.header.vertexCount,
mesh.vertexPosition.data(),
mesh.texCoords.data(),
mesh.vertexTangent.data(),
mesh.vertexNormal.data(),
mesh.header.indexCount,
mesh.indices.data()
));
case AssetType::MESH:
if (asset.name == "Cube.012")
handles.emplace_back(SHResourceManager::LoadOrGet<SHMesh>(asset.id));
break;
case AssetType::TEXTURE:
texHandles.emplace_back(SHResourceManager::LoadOrGet<SHTexture>(asset.id));
break;
}
}
graphicsSystem->BuildMeshBuffers();
// Load Textures
auto textures = SHADE::SHAssetManager::GetAllTextures();
std::vector<Handle<SHTexture>> texHandles;
for (const auto& tex : textures)
{
auto texture = graphicsSystem->Add(tex);
texHandles.push_back(texture);
}
graphicsSystem->BuildTextures();
SHResourceManager::FinaliseChanges();
// Create Materials
auto matInst = graphicsSystem->AddOrGetBaseMaterialInstance();
@ -78,8 +67,8 @@ namespace Sandbox
// Create Stress Test Objects
static const SHVec3 TEST_OBJ_SCALE = SHVec3::One * 0.5f;
constexpr int NUM_ROWS = 10;
constexpr int NUM_COLS = 10;
constexpr int NUM_ROWS = 0;
constexpr int NUM_COLS = 0;
static const SHVec3 TEST_OBJ_SPACING = { 0.1f, 0.1f, 0.1f };
static const SHVec3 TEST_OBJ_START_POS = { -(NUM_COLS / 2 * TEST_OBJ_SPACING.x) + 1.0f, -2.0f, -1.0f };

View File

@ -62,7 +62,12 @@ namespace SHADE
}
}
AssetPath SHAssetManager::GenerateLocalPath(AssetPath path) noexcept
void SHAssetManager::Unload(AssetID assetId) noexcept
{
// TODO
}
AssetPath SHAssetManager::GenerateLocalPath(AssetPath path) noexcept
{
if (!IsRecognised(path.extension().string().c_str()))
{

View File

@ -32,6 +32,7 @@ namespace SHADE
* \brief Deallocate all memory used by resource data
****************************************************************************/
static void Unload() noexcept;
static void Unload(AssetID assetId) noexcept;
/****************************************************************************
* \brief Load all resources that are in the folder

View File

@ -13,7 +13,7 @@
#include "ECS_Base/SHECSMacros.h"
#include "ECS_Base/System/SHSystem.h"
#include "ECS_Base/System/SHSystemRoutine.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "EditorWindow/SHEditorWindow.h"
#include "Tools/SHLogger.h"

View File

@ -3,7 +3,7 @@
#include "Graphics/SHVulkanIncludes.h"
#include "vk_mem_alloc.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -4,7 +4,7 @@
#include "Graphics/SHVulkanIncludes.h"
#include "Graphics/SHVulkanDefines.h"
#include "SHCommandPoolResetMode.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
#include "Graphics/Pipeline/SHVkPipelineLayout.h"
namespace SHADE
@ -36,7 +36,7 @@ namespace SHADE
class SHVkCommandBuffer
{
friend class SHVkCommandPool;
friend class ResourceLibrary<SHVkCommandBuffer>;
friend class SHResourceLibrary<SHVkCommandBuffer>;
static constexpr uint16_t PUSH_CONSTANT_SIZE = 512;
private:

View File

@ -2,7 +2,7 @@
#include "SHVkCommandPool.h"
#include "Graphics/Devices/SHVkLogicalDevice.h"
#include "Graphics/Instance/SHVkInstance.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
#include "Tools/SHLogger.h"
namespace SHADE

View File

@ -6,7 +6,7 @@
#include "Graphics/Queues/SHVkQueue.h"
#include "SHCommandPoolResetMode.h"
#include "SHVkCommandBuffer.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
namespace SHADE
{

View File

@ -2,7 +2,7 @@
// Project Includes
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -4,7 +4,7 @@
// Project Includes
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/Shaders/SHShaderReflected.h"
#include "SHDescriptorSetUpdater.h"

View File

@ -2,7 +2,7 @@
// Project Includes
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -8,8 +8,8 @@
#include "Graphics/SHVulkanIncludes.h"
#include "Graphics/Devices/SHVkPhysicalDevice.h"
#include "Graphics/Queues/SHVkQueue.h"
#include "Resource/Handle.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHHandle.h"
#include "Resource/SHResourceLibrary.h"
#include "Graphics/Swapchain/SHSwapchainParams.h"
#include "Graphics/Commands/SHCommandPoolResetMode.h"
#include "Graphics/Commands/SHVkCommandPool.h"

View File

@ -2,7 +2,7 @@
#define SH_VK_FRAMEBUFFER_H
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include <span>
namespace SHADE

View File

@ -3,7 +3,7 @@
#include "SHImageViewDetails.h"
#include "Graphics/SHVulkanDefines.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
#include "vk_mem_alloc.h"
namespace SHADE

View File

@ -2,7 +2,7 @@
#define SH_VK_IMAGE_VIEW_H
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "SHImageViewDetails.h"
namespace SHADE

View File

@ -15,7 +15,7 @@ of DigiPen Institute of Technology is prohibited.
#include <vector>
// Project Includes
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -15,7 +15,7 @@ namespace SHADE
bool SHVkInstance::validationLayersOn;
vk::Instance SHVkInstance::vkInstance;
SHVkDebugMessenger SHVkInstance::debugMessenger;
ResourceManager SHVkInstance::resourceManager;
SHResourceHub SHVkInstance::resourceManager;
/***************************************************************************/
/*!
@ -258,7 +258,7 @@ namespace SHADE
return vkInstance;
}
ResourceManager& SHVkInstance::GetResourceManager(void) noexcept
SHResourceHub& SHVkInstance::GetResourceManager(void) noexcept
{
return resourceManager;
}

View File

@ -18,7 +18,7 @@ written consent of DigiPen Institute of Technology is prohibited.
#include "Graphics/Debugging/SHVkDebugMessenger.h"
#include "Graphics/Devices/SHVkPhysicalDevice.h"
#include "Graphics/Devices/SHVkLogicalDevice.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
namespace SHADE
@ -61,7 +61,7 @@ namespace SHADE
static SHVkDebugMessenger debugMessenger;
//! Resource management for vulkan project
static ResourceManager resourceManager;
static SHResourceHub resourceManager;
/*-----------------------------------------------------------------------*/
/* PRIVATE MEMBER FUNCTIONS */
@ -85,7 +85,7 @@ namespace SHADE
/* Getters and Setters */
/*-----------------------------------------------------------------------*/
static vk::Instance const& GetVkInstance (void) noexcept;
static ResourceManager& GetResourceManager(void) noexcept;
static SHResourceHub& GetResourceManager(void) noexcept;
};
}

View File

@ -18,7 +18,7 @@ of DigiPen Institute of Technology is prohibited.
// External Dependencies
#include "Graphics/SHVulkanIncludes.h"
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/MiddleEnd/Interface/SHMaterial.h"
#include "Math/SHMatrix.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h"

View File

@ -15,7 +15,7 @@ of DigiPen Institute of Technology is prohibited.
// STL Includes
#include <vector>
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -15,7 +15,7 @@ of DigiPen Institute of Technology is prohibited.
// External Dependencies
#include "Graphics/SHVulkanIncludes.h"
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "SHBatch.h"
#include "Graphics/Pipeline/SHVkPipeline.h"

View File

@ -585,19 +585,19 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* 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()) });
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()) });
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);
}

View File

@ -17,7 +17,7 @@ of DigiPen Institute of Technology is prohibited.
#include <array>
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/SHVulkanIncludes.h"
#include "Graphics/MiddleEnd/PerFrame/SHRenderContext.h"
#include "Graphics/RenderGraph/SHRenderGraph.h"
@ -231,8 +231,8 @@ namespace SHADE
*/
/*******************************************************************************/
Handle<SHTexture> Add(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(const SHTextureAsset& texAsset);
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);
/***************************************************************************/
/*!
@ -313,7 +313,7 @@ namespace SHADE
SHWindow* window = nullptr;
// Middle End Resources
ResourceManager resourceManager;
SHResourceHub resourceManager;
SHMeshLibrary meshLibrary;
SHTextureLibrary texLibrary;
SHSamplerCache samplerCache;

View File

@ -15,7 +15,7 @@ of DigiPen Institute of Technology is prohibited.
// STL Includes
#include <unordered_map>
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "SHCommonTypes.h"
namespace SHADE

View File

@ -13,7 +13,7 @@ of DigiPen Institute of Technology is prohibited.
// STL Includes
#include <memory>
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/Shaders/BlockInterface/SHShaderBlockInterface.h"
#include "SH_API.h"

View File

@ -15,8 +15,8 @@ of DigiPen Institute of Technology is prohibited.
// STL Includes
#include <vector>
// Project Includes
#include "Resource/Handle.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHHandle.h"
#include "Resource/SHResourceLibrary.h"
#include "Math/SHMath.h"
namespace SHADE
@ -167,7 +167,7 @@ namespace SHADE
std::vector<MeshAddJob> meshAddJobs;
std::vector<Handle<SHMesh>> meshRemoveJobs;
// Tracking
ResourceLibrary<SHMesh> meshes{};
SHResourceLibrary<SHMesh> meshes{};
std::vector<Handle<SHMesh>> meshOrder;
// CPU Storage
std::vector<SHMesh::VertexPosition> vertPosStorage;

View File

@ -1,6 +1,6 @@
#pragma once
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -12,7 +12,7 @@ of DigiPen Institute of Technology is prohibited.
#pragma once
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
//#include "SHTransform.h"
#include "ECS_Base/Components/SHComponent.h"
#include "Math/SHMatrix.h"

View File

@ -17,7 +17,7 @@ of DigiPen Institute of Technology is prohibited.
// Project Includes
#include "SHCamera.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/RenderGraph/SHRenderGraph.h"
#include "Math/SHMath.h"
#include <vector>

View File

@ -17,7 +17,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/Instance/SHVkInstance.h"
#include "Tools/SHLogger.h"
#include "SHRenderer.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
#include "Graphics/RenderGraph/SHRenderGraph.h"
namespace SHADE
@ -49,7 +49,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Renderer Registration Functions */
/*---------------------------------------------------------------------------------*/
Handle<SHRenderer> SHViewport::AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, std::vector<Handle<SHVkCommandPool>>& cmdPools, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph)
Handle<SHRenderer> SHViewport::AddRenderer(SHResourceHub& resourceManager, uint32_t numFrames, std::vector<Handle<SHVkCommandPool>>& cmdPools, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph)
{
// Create the renderer
auto renderer = resourceManager.Create<SHRenderer>(device, numFrames, cmdPools, descriptorPool, cameraDescLayout, GetHandle(), renderGraph);

View File

@ -17,7 +17,7 @@ of DigiPen Institute of Technology is prohibited.
// External Dependencies
#include "Graphics/SHVulkanIncludes.h"
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{
@ -28,7 +28,7 @@ namespace SHADE
class SHVkCommandBuffer;
class SHVkLogicalDevice;
class SHVkImageView;
class ResourceManager;
class SHResourceHub;
class SHRenderGraph;
class SHVkDescriptorPool;
class SHVkDescriptorSetLayout;
@ -59,7 +59,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Renderers Registration Functions */
/*-----------------------------------------------------------------------------*/
Handle<SHRenderer> AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, std::vector<Handle<SHVkCommandPool>>& cmdPools, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph);
Handle<SHRenderer> AddRenderer(SHResourceHub& resourceManager, uint32_t numFrames, std::vector<Handle<SHVkCommandPool>>& cmdPools, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph);
void RemoveRenderer(Handle<SHRenderer> renderer);
/*-----------------------------------------------------------------------------*/

View File

@ -14,7 +14,7 @@ of DigiPen Institute of Technology is prohibited.
#include "SHMaterialInstanceCache.h"
#include "Graphics/MiddleEnd/Interface/SHMaterialInstance.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
namespace SHADE
@ -22,7 +22,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
SHADE::Handle<SHADE::SHMaterialInstance> SHMaterialInstanceCache::CreateOrGet(ResourceManager& manager, Handle<SHMaterial> material)
SHADE::Handle<SHADE::SHMaterialInstance> SHMaterialInstanceCache::CreateOrGet(SHResourceHub& manager, Handle<SHMaterial> material)
{
// Check if there is already an existing instance
auto matInst = cache.find(material);

View File

@ -15,7 +15,7 @@ of DigiPen Institute of Technology is prohibited.
// STL Includes
#include <unordered_map>
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{
@ -24,7 +24,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
class SHMaterial;
class SHMaterialInstance;
class ResourceManager;
class SHResourceHub;
/*-----------------------------------------------------------------------------------*/
/* Type Definitions */
@ -56,7 +56,7 @@ namespace SHADE
*/
/***********************************************************************************/
Handle<SHMaterialInstance> CreateOrGet(ResourceManager& manager, Handle<SHMaterial> material);
Handle<SHMaterialInstance> CreateOrGet(SHResourceHub& manager, Handle<SHMaterial> material);
/***********************************************************************************/
/*!

View File

@ -17,8 +17,8 @@ of DigiPen Institute of Technology is prohibited.
// External Dependencies
#include "tinyddsloader.h"
// Project Includes
#include "Resource/Handle.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHHandle.h"
#include "Resource/SHResourceLibrary.h"
#include "Math/SHMath.h"
#include "Graphics/SHVulkanIncludes.h"
@ -156,7 +156,7 @@ namespace SHADE
std::vector<AddJob> addJobs;
std::vector<Handle<SHTexture>> removeJobs;
// Tracking
ResourceManager resourceManager;
SHResourceHub resourceManager;
std::vector<Handle<SHTexture>> texOrder;
// CPU Storage
std::vector<std::tuple<Handle<SHVkImageView>, Handle<SHVkSampler>, vk::ImageLayout>> combinedImageSamplers;

View File

@ -16,7 +16,7 @@ of DigiPen Institute of Technology is prohibited.
// External Dependencies
#include "Graphics/SHVulkanIncludes.h"
// Project Includes
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -2,7 +2,7 @@
#define SH_PIPELINE_LAYOUT_PARAMS_H
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/Descriptors/SHVkDescriptorSetLayout.h"
namespace SHADE

View File

@ -3,7 +3,7 @@
#include "SHPipelineState.h"
#include "SHPipelineType.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/Pipeline/SHVkPipelineLayout.h"
namespace SHADE

View File

@ -3,7 +3,7 @@
#include "Graphics/SHVulkanIncludes.h"
#include "Graphics/SHVulkanDefines.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -1,6 +1,6 @@
#pragma once
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -367,7 +367,7 @@ namespace SHADE
, graphResources{}
, resourceManager{nullptr}
{
resourceManager = std::make_shared<ResourceManager>();
resourceManager = std::make_shared<SHResourceHub>();
}
SHRenderGraph::SHRenderGraph(SHRenderGraph&& rhs) noexcept

View File

@ -2,7 +2,7 @@
#define SH_RENDER_GRAPH_H
#include "Graphics/Renderpass/SHVkRenderpass.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
#include "SH_API.h"
#include "Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h"
#include "Graphics/MiddleEnd/Batching/SHSuperBatch.h"
@ -71,7 +71,7 @@ namespace SHADE
std::unordered_map<std::string, Handle<SHRenderGraphResource>> graphResources;
//! Resource library for graph handles
std::shared_ptr<ResourceManager> resourceManager;
std::shared_ptr<SHResourceHub> resourceManager;
public:
/*-----------------------------------------------------------------------*/

View File

@ -104,7 +104,7 @@ namespace SHADE
*/
/***************************************************************************/
SHRenderGraphNode::SHRenderGraphNode(std::shared_ptr<ResourceManager> rm, Handle<SHVkLogicalDevice> const& logicalDevice, Handle<SHVkSwapchain> const& swapchain, std::vector<SHAttachmentDescInitParams> attDescInitParams, std::vector<Handle<SHRenderGraphNode>> predecessors, std::unordered_map<std::string, Handle<SHRenderGraphResource>> const* resources) noexcept
SHRenderGraphNode::SHRenderGraphNode(std::shared_ptr<SHResourceHub> rm, Handle<SHVkLogicalDevice> const& logicalDevice, Handle<SHVkSwapchain> const& swapchain, std::vector<SHAttachmentDescInitParams> attDescInitParams, std::vector<Handle<SHRenderGraphNode>> predecessors, std::unordered_map<std::string, Handle<SHRenderGraphResource>> const* resources) noexcept
: logicalDeviceHdl{ logicalDevice }
, renderpass{}
, framebuffers{}

View File

@ -13,7 +13,7 @@
namespace SHADE
{
class ResourceManager;
class SHResourceHub;
class SHVkFramebuffer;
class SHRenderGraphResource;
class SHVkLogicalDevice;
@ -26,7 +26,7 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* PRIVATE MEMBER VARIABLES */
/*-----------------------------------------------------------------------*/
std::shared_ptr<ResourceManager> resourceManager;
std::shared_ptr<SHResourceHub> resourceManager;
//! For Vulkan object creation
Handle<SHVkLogicalDevice> logicalDeviceHdl;
@ -88,7 +88,7 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* CTORS AND DTORS */
/*-----------------------------------------------------------------------*/
SHRenderGraphNode(std::shared_ptr<ResourceManager> rm, Handle<SHVkLogicalDevice> const& logicalDevice, Handle<SHVkSwapchain> const& swapchain, std::vector<SHAttachmentDescInitParams> attDescInitParams, std::vector<Handle<SHRenderGraphNode>> predecessors, std::unordered_map<std::string, Handle<SHRenderGraphResource>> const* resources) noexcept;
SHRenderGraphNode(std::shared_ptr<SHResourceHub> rm, Handle<SHVkLogicalDevice> const& logicalDevice, Handle<SHVkSwapchain> const& swapchain, std::vector<SHAttachmentDescInitParams> attDescInitParams, std::vector<Handle<SHRenderGraphNode>> predecessors, std::unordered_map<std::string, Handle<SHRenderGraphResource>> const* resources) noexcept;
SHRenderGraphNode(SHRenderGraphNode&& rhs) noexcept;
SHRenderGraphNode& operator= (SHRenderGraphNode&& rhs) noexcept;

View File

@ -3,7 +3,7 @@
#include <string>
#include "SHAttachmentDescriptionType.h"
#include <vector>
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/SHVulkanIncludes.h"
#include "SH_API.h"

View File

@ -182,7 +182,7 @@ namespace SHADE
exteriorDrawCalls.push_back(newDrawCall);
}
void SHSubpass::Init(ResourceManager& resourceManager) noexcept
void SHSubpass::Init(SHResourceHub& resourceManager) noexcept
{
superBatch = resourceManager.Create<SHSuperBatch>(GetHandle());

View File

@ -3,7 +3,7 @@
#include "SHAttachmentDescriptionType.h"
#include <string>
#include "SH_API.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "Graphics/SHVulkanIncludes.h"
@ -78,7 +78,7 @@ namespace SHADE
void Execute(Handle<SHVkCommandBuffer>& commandBuffer, Handle<SHVkDescriptorPool> descPool, uint32_t frameIndex) noexcept;
void AddExteriorDrawCalls(std::function<void(Handle<SHVkCommandBuffer>&)> const& newDrawCall) noexcept;
void Init(ResourceManager& resourceManager) noexcept;
void Init(SHResourceHub& resourceManager) noexcept;
/*-----------------------------------------------------------------------*/
/* GETTERS AND SETTERS */

View File

@ -1,6 +1,6 @@
#pragma once
#include <Resource/Handle.h>
#include <Resource/SHHandle.h>
namespace SHADE
{

View File

@ -2,7 +2,7 @@
#define SH_VK_ATTACHMENT_DESC_GEN_H
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -3,7 +3,7 @@
#include "SHVkAttachDescGen.h"
#include "SHVkSubpassParams.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include <span>
namespace SHADE

View File

@ -2,7 +2,7 @@
#define SH_VK_SUBPASS_PARAMS_H
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include <span>
namespace SHADE

View File

@ -3,7 +3,7 @@
#include "SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -2,7 +2,7 @@
#define SH_VK_SHADER_MODULE_H
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
#include "SHShaderReflected.h"
#include <vector>

View File

@ -4,7 +4,7 @@
#include <cstdint>
#include <vector>
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
#include "Graphics/Swapchain/SHSwapchainParams.h"
namespace SHADE

View File

@ -2,7 +2,7 @@
#define SH_VK_FENCE_H
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -2,7 +2,7 @@
#define SH_VK_SEMAPHORE_H
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/Handle.h"
#include "Resource/SHHandle.h"
namespace SHADE
{

View File

@ -3,7 +3,7 @@
#include <windows.h>
#include "Graphics/SHVulkanIncludes.h"
#include "Resource/ResourceLibrary.h"
#include "Resource/SHResourceLibrary.h"
namespace SHADE
{

View File

@ -8,8 +8,9 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------------------*/
class SHResourceLibraryBase;
template<typename T>
class ResourceLibrary;
class SHResourceLibrary;
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
@ -25,6 +26,20 @@ namespace SHADE
using std::runtime_error::runtime_error;
};
/// <summary>
/// Exception thrown when a generic Handle is being casted to the wrong type.
/// </summary>
class BadHandleCastException : std::runtime_error
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
BadHandleCastException()
: std::runtime_error("Attempted to cast a generic Handle to the wrong type. ")
{}
};
/// <summary>
/// Base implementation of the Handle that is not templated to allow for holding
/// generic non-type-specific Handles.
@ -80,7 +95,7 @@ namespace SHADE
/// Generic implementation of a Handle object
/// </summary>
/// <typeparam name="T">Type of the handle.</typeparam>
template<typename T>
template<typename T = void>
class Handle : public HandleBase
{
public:
@ -88,6 +103,16 @@ namespace SHADE
/* Constructors */
/*-----------------------------------------------------------------------------*/
Handle() = default;
/// <summary>
/// Converts a generic/void Handle to a specific type.
/// Runtime type checking is enabled to ensure that Handles are only being casted
/// to the correct type.
/// </summary>
/// <param name="genericHandle">Generic handle to convert.</param>
/// <exception cref="std::bad_cast">
/// Thrown if an invalid conversion is made.
/// </exception>
explicit Handle(const Handle<void>& genericHandle);
~Handle() = default;
/*-----------------------------------------------------------------------------*/
@ -140,13 +165,48 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
ResourceLibrary<T>* library = nullptr;
SHResourceLibrary<T>* library = nullptr;
/*-----------------------------------------------------------------------------*/
/* Friend Declarations */
/*-----------------------------------------------------------------------------*/
friend class ResourceLibrary<T>;
};
friend class SHResourceLibrary<T>;
friend class Handle<void>;
};
/// <summary>
/// Template Specialization for Handle that represents a type-less Handle.
/// </summary>
template<>
class Handle<void> : public HandleBase
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
Handle() = default;
template<typename T>
explicit Handle(const Handle<T>& handle);
~Handle() = default;
/*-----------------------------------------------------------------------------*/
/* Overloaded Operators */
/*-----------------------------------------------------------------------------*/
template<typename T>
inline bool operator==(const Handle<T>& rhs) const noexcept;
protected:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
SHResourceLibraryBase* library = nullptr;
/*-----------------------------------------------------------------------------*/
/* Friend Declarations */
/*-----------------------------------------------------------------------------*/
template<typename T>
friend class Handle;
};
/// <summary>
/// Interface that needs to be implemented by classes that want to store a Handle to
@ -155,7 +215,7 @@ namespace SHADE
template<typename T>
class ISelfHandle
{
public:
public:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
@ -183,7 +243,7 @@ namespace SHADE
/// </summary>
/// <param name="rscLib">Required to lock usage to ResourceLibrary only.</param>
/// <param name="hdl">Handle to set.</param>
void SetHandle(const ResourceLibrary<T>& rscLib, Handle<T> hdl);
void SetHandle(const SHResourceLibrary<T>& rscLib, Handle<T> hdl);
private:
/*-----------------------------------------------------------------------------*/
@ -220,4 +280,4 @@ namespace std
};
}
#include "Handle.hpp"
#include "SHHandle.hpp"

View File

@ -1,7 +1,7 @@
#pragma once
// Primary Header
#include "Handle.h"
#include "ResourceLibrary.h"
#include "SHHandle.h"
#include "SHResourceLibrary.h"
namespace SHADE
{
@ -20,7 +20,21 @@ namespace SHADE
{
return id.Raw != INVALID_ID.Raw;
}
/*---------------------------------------------------------------------------------*/
/* Handle<T> - Constructors */
/*---------------------------------------------------------------------------------*/
template<typename T>
Handle<T>::Handle(const Handle<void>& genericHandle)
: library { reinterpret_cast<SHResourceLibrary<T>*>(genericHandle.library) }
{
id = genericHandle.id;
// Check if valid
if (library != nullptr && library->GetType() != typeid(T))
throw BadHandleCastException();
}
/*---------------------------------------------------------------------------------*/
/* Handle<T> - Usage Functions */
/*---------------------------------------------------------------------------------*/
@ -63,6 +77,28 @@ namespace SHADE
return &library->Get(*this);
}
/*---------------------------------------------------------------------------------*/
/* Handle<void> - Constructors */
/*---------------------------------------------------------------------------------*/
template<typename T>
Handle<void>::Handle(const Handle<T>& handle)
: library{ static_cast<SHResourceLibraryBase*>(handle.library) }
{
id = handle.id;
}
/*---------------------------------------------------------------------------------*/
/* Handle<void> - Overloaded Operators */
/*---------------------------------------------------------------------------------*/
template<typename T>
bool SHADE::Handle<void>::operator==(const Handle<T>& rhs) const noexcept
{
return id.Raw == rhs.id.Raw && library == static_cast<void*>(rhs.library);
}
/*---------------------------------------------------------------------------------*/
/* ISelfHandle<T> - Constructors */
/*---------------------------------------------------------------------------------*/
template<typename T>
inline ISelfHandle<T>::ISelfHandle(const ISelfHandle& rhs)
: handle { rhs.handle }
@ -73,6 +109,9 @@ namespace SHADE
: handle { rhs.handle }
{}
/*---------------------------------------------------------------------------------*/
/* ISelfHandle<T> - Overloaded Operators */
/*---------------------------------------------------------------------------------*/
template<typename T>
inline ISelfHandle<T>& ISelfHandle<T>::operator=(const ISelfHandle& rhs)
{
@ -96,7 +135,7 @@ namespace SHADE
return handle;
}
template<typename T>
inline void ISelfHandle<T>::SetHandle(const ResourceLibrary<T>&, Handle<T> hdl)
inline void ISelfHandle<T>::SetHandle(const SHResourceLibrary<T>&, Handle<T> hdl)
{
handle = hdl;
}

View File

@ -1,11 +1,11 @@
#include "SHPch.h"
#include "ResourceLibrary.h"
#include "SHResourceLibrary.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------*/
/* Constructors/Destructors */
/*-----------------------------------------------------------------------------*/
ResourceManager::~ResourceManager()
SHResourceHub::~SHResourceHub()
{
// Delete all resources libraries
for (auto iter = deleters.rbegin(); iter != deleters.rend(); ++iter)

View File

@ -6,24 +6,42 @@
#include <queue>
// Project Headers
#include "Handle.h"
#include "SHHandle.h"
#include "Resource/SparseSet.h"
namespace SHADE
{
/// <summary>
/// Base class for SHResourceLibrary that holds information about the library type.
/// </summary>
class SHResourceLibraryBase
{
public:
/*-----------------------------------------------------------------------------*/
/* Getter Functions */
/*-----------------------------------------------------------------------------*/
inline std::type_index GetType() { return libraryType; }
protected:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
std::type_index libraryType = typeid(void);
};
/// <summary>
/// Generic Resource Library for a specified type of Resource. This object will own
/// any resources created using it.
/// </summary>
/// <typeparam name="T">Type of resources that this library stores.</typeparam>
template<typename T>
class ResourceLibrary
class SHResourceLibrary : public SHResourceLibraryBase
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructor */
/*-----------------------------------------------------------------------------*/
ResourceLibrary();
SHResourceLibrary();
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
@ -75,13 +93,13 @@ namespace SHADE
/// <summary>
/// Manages all resources in multiple ResourceLibraries.
/// </summary>
class ResourceManager final
class SHResourceHub final
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructors/Destructors */
/*-----------------------------------------------------------------------------*/
~ResourceManager();
~SHResourceHub();
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
@ -136,10 +154,10 @@ namespace SHADE
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
template<typename T>
ResourceLibrary<T>& getLibrary();
SHResourceLibrary<T>& getLibrary();
template<typename T>
const ResourceLibrary<T>& getLibrary() const;
const SHResourceLibrary<T>& getLibrary() const;
};
}
#include "ResourceLibrary.hpp"
#include "SHResourceLibrary.hpp"

View File

@ -1,6 +1,6 @@
#pragma once
// Primary Header
#include "ResourceLibrary.h"
#include "SHResourceLibrary.h"
// Standard Library
#include <utility>
@ -10,13 +10,14 @@ namespace SHADE
/* ResourceLibrary - Constructor */
/*---------------------------------------------------------------------------------*/
template <typename T>
ResourceLibrary<T>::ResourceLibrary()
SHResourceLibrary<T>::SHResourceLibrary()
{
// Type Checking
//static_assert(std::is_copy_assignable_v<T>, "Resource Library's resources must be copy assignable.");
//static_assert(std::is_copy_constructible_v<T>, "Resource Library's resources must be copy constructible.");
static_assert(std::is_move_assignable_v<T>, "Resource Library's resources must be move assignable.");
static_assert(std::is_move_constructible_v<T>, "Resource Library's resources must be move constructible.");
// Keep track of the type for conversions
libraryType = typeid(T);
}
/*---------------------------------------------------------------------------------*/
@ -24,7 +25,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
template <typename T>
template <typename ... Args>
Handle<T> ResourceLibrary<T>::Create(Args&&... args)
Handle<T> SHResourceLibrary<T>::Create(Args&&... args)
{
// Create the handle
Handle<T> handle;
@ -55,7 +56,7 @@ namespace SHADE
}
template <typename T>
void ResourceLibrary<T>::Free(Handle<T> handle)
void SHResourceLibrary<T>::Free(Handle<T> handle)
{
assertHandleValid(handle);
@ -63,7 +64,7 @@ namespace SHADE
}
template <typename T>
T& ResourceLibrary<T>::Get(Handle<T> handle)
T& SHResourceLibrary<T>::Get(Handle<T> handle)
{
assertHandleValid(handle);
@ -71,7 +72,7 @@ namespace SHADE
}
template <typename T>
const T& ResourceLibrary<T>::Get(Handle<T> handle) const
const T& SHResourceLibrary<T>::Get(Handle<T> handle) const
{
assertHandleValid(handle);
@ -82,14 +83,14 @@ namespace SHADE
/* ResourceLibrary - Helper Functions */
/*---------------------------------------------------------------------------------*/
template <typename T>
void ResourceLibrary<T>::assertHandleValid(Handle<T> handle) const
void SHResourceLibrary<T>::assertHandleValid(Handle<T> handle) const
{
if (!handle || handle.id.Data.Version != versionCounts[handle.id.Data.Index])
throw std::invalid_argument("Invalid handle provided!");
}
template<typename T>
inline uint32_t ResourceLibrary<T>::getAvailableFreeIndex()
inline uint32_t SHResourceLibrary<T>::getAvailableFreeIndex()
{
// Get from the free list if present
if (!freeList.empty())
@ -107,25 +108,25 @@ namespace SHADE
/* ResourceManager - Usage Functions */
/*---------------------------------------------------------------------------------*/
template <typename T, typename ... Args>
Handle<T> ResourceManager::Create(Args&&... args)
Handle<T> SHResourceHub::Create(Args&&... args)
{
return getLibrary<T>().Create(std::forward<Args>(args) ...);
}
template <typename T>
void ResourceManager::Free(Handle<T> handle)
void SHResourceHub::Free(Handle<T> handle)
{
getLibrary<T>().Free(handle);
}
template <typename T>
T& ResourceManager::Get(Handle<T> handle)
T& SHResourceHub::Get(Handle<T> handle)
{
return getLibrary<T>().Get(handle);
}
template <typename T>
const T& ResourceManager::Get(Handle<T> handle) const
const T& SHResourceHub::Get(Handle<T> handle) const
{
return getLibrary<T>().Get(handle);
}
@ -134,18 +135,18 @@ namespace SHADE
/* ResourceManager - Helper Functions */
/*-----------------------------------------------------------------------------*/
template <typename T>
ResourceLibrary<T>& ResourceManager::getLibrary()
SHResourceLibrary<T>& SHResourceHub::getLibrary()
{
// Attempt to retrieve the library
const std::type_index RSC_TYPE = typeid(T);
if (resourceLibs.contains(RSC_TYPE))
{
return *static_cast<ResourceLibrary<T>*>(resourceLibs.at(RSC_TYPE));
return *static_cast<SHResourceLibrary<T>*>(resourceLibs.at(RSC_TYPE));
}
else
{
// Construct library if doesn't exist
ResourceLibrary<T>* lib = new ResourceLibrary<T>();
SHResourceLibrary<T>* lib = new SHResourceLibrary<T>();
resourceLibs.emplace(RSC_TYPE, static_cast<void*>(lib));
// Construct deleter to properly delete objects with void*
@ -156,8 +157,8 @@ namespace SHADE
}
template <typename T>
const ResourceLibrary<T>& ResourceManager::getLibrary() const
const SHResourceLibrary<T>& SHResourceHub::getLibrary() const
{
return const_cast<ResourceManager*>(this).getLibrary<T>();
return const_cast<SHResourceHub*>(this).getLibrary<T>();
}
}

View File

@ -0,0 +1,95 @@
/************************************************************************************//*!
\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, Handle<void>>> SHResourceManager::handlesMap;
std::unordered_map<std::type_index, SHADE::SHResourceManager::HandleAssetMap> SHResourceManager::assetIdMap;
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
Handle handle;
for (auto& typedHandleMap : handlesMap)
{
if (typedHandleMap.second.contains(assetId))
{
// Save handle for later
handle = typedHandleMap.second[assetId];
// Dispose
typedFreeFuncMap[typedHandleMap.first](assetId);
typedHandleMap.second.erase(assetId);
}
}
// No handles were found
if (!handle)
return;
for (auto& typedAssetIdsMap : assetIdMap)
{
if (typedAssetIdsMap.second.contains(handle))
{
// Dispose
typedAssetIdsMap.second.erase(handle);
}
}
}
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();
}
/*-----------------------------------------------------------------------------------*/
/* Query Functions */
/*-----------------------------------------------------------------------------------*/
std::optional<AssetID> SHResourceManager::GetAssetID(Handle<void> handle)
{
const Handle GENERIC_HANDLE = Handle(handle);
// Search each library for the asset ID and try to free it
for (auto& typedAssetIdsMap : assetIdMap)
{
if (typedAssetIdsMap.second.contains(GENERIC_HANDLE))
{
return typedAssetIdsMap.second[GENERIC_HANDLE];
}
}
return {};
}
}

View File

@ -0,0 +1,130 @@
/************************************************************************************//*!
\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();
/*---------------------------------------------------------------------------------*/
/* Query Functions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Retrieves the AssetID associated with a specified Handle.
/// Faster than the untemplated version.
/// </summary>
/// <typeparam name="ResourceType">Type of resource to get the ID of.</typeparam>
/// <param name="handle">Handle to get the AssetID of.</param>
/// <return>
/// AssetID for the specified Handle. If the Handle is invalid, there will be no
/// value.
/// </return>
template<typename T>
static std::optional<AssetID> GetAssetID(Handle<T> handle);
/// <summary>
/// Retrieves the AssetID associated with a specified Handle.
/// Compared to the templated version, this function is slower as it requires
/// searching through the storage of all resource types.
/// </summary>
/// <param name="handle">Handle to get the AssetID of.</param>
/// <return>
/// AssetID for the specified Handle. If the Handle is invalid, there will be no
/// value.
/// </return>
static std::optional<AssetID> GetAssetID(Handle<void> handle);
private:
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
using AssetHandleMap = std::unordered_map<AssetID, Handle<void>>;
using HandleAssetMap = std::unordered_map<Handle<void>, AssetID>;
using AssetHandleMapRef = std::reference_wrapper<AssetHandleMap>;
using HandleAssetMapRef = std::reference_wrapper<HandleAssetMap>;
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
// Handles
static SHResourceHub resourceHub;
static std::unordered_map<std::type_index, AssetHandleMap> handlesMap;
static std::unordered_map<std::type_index, HandleAssetMap> assetIdMap;
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 std::pair<AssetHandleMapRef, HandleAssetMapRef> getAssetHandleMap();
};
}
#include "SHResourceManager.hpp"

View File

@ -0,0 +1,171 @@
/************************************************************************************//*!
\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 */
auto [typedHandleMap, typedAssetIdMap] = getAssetHandleMap<ResourceType>();
if (typedHandleMap.get().contains(assetId))
return Handle<ResourceType>(typedHandleMap.get()[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
const 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()
);
Handle genericHandle = Handle(meshHandle);
typedHandleMap.get().emplace(assetId, genericHandle);
typedAssetIdMap.get().emplace(genericHandle, assetId);
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
const 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.get().emplace(assetId, Handle(texHandle));
return texHandle;
}
}
template<typename ResourceType>
void SHResourceManager::Unload(Handle<ResourceType> 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 */
auto [typedHandleMap, typedAssetIdMap] = getAssetHandleMap<ResourceType>();
if (typedHandleMap.get().contains(assetId))
{
// Dispose
Handle handle = typedHandleMap.get()[assetId];
Handle<ResourceType> typedHandle = static_cast<Handle<ResourceType>>(handle);
typedHandle.Free();
typedAssetIdMap.get().erase(handle);
typedHandleMap.get().erase(assetId);
}
else
{
// There's nothing to remove
SHLog::Warning("[SHResourceManager] Attempted to unload an invalid resource. Ignoring.");
}
}
/*-----------------------------------------------------------------------------------*/
/* Query Functions */
/*-----------------------------------------------------------------------------------*/
template<typename T>
static std::optional<AssetID> SHResourceManager::GetAssetID(Handle<T> handle)
{
const Handle GENERIC_HANDLE = Handle(handle);
auto [typedHandleMap, typedAssetIdMap] = getAssetHandleMap<T>();
if (typedAssetIdMap.get().contains(GENERIC_HANDLE))
{
return typedAssetIdMap.GetId()[GENERIC_HANDLE];
}
return {};
}
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
template<typename ResourceType>
std::pair<SHResourceManager::AssetHandleMapRef, SHResourceManager::HandleAssetMapRef> SHResourceManager::getAssetHandleMap()
{
const std::type_index TYPE = typeid(ResourceType);
if (!handlesMap.contains(TYPE))
{
handlesMap.emplace(TYPE, AssetHandleMap{});
assetIdMap.emplace(TYPE, HandleAssetMap{});
typedFreeFuncMap.emplace
(
TYPE,
[TYPE](AssetID assetId)
{
static_cast<Handle<ResourceType>>(SHResourceManager::handlesMap[TYPE][assetId]).Free();
}
);
}
return std::make_pair(std::ref(handlesMap[TYPE]), std::ref(assetIdMap[TYPE]));
}
}