Added SHMaterialInstanceCache

This commit is contained in:
Kah Wei 2022-09-25 22:55:14 +08:00
parent a197ae842c
commit fc5ff763f3
6 changed files with 158 additions and 10 deletions

View File

@ -36,7 +36,7 @@ namespace Sandbox
graphicsSystem->BuildMeshBuffers();
// Create Materials
auto matInst = graphicsSystem->AddMaterialInstance();
auto matInst = graphicsSystem->AddOrGetBaseMaterialInstance();
// Create Stress Test Objects
static const SHVec3 TEST_OBJ_SCALE = { 0.2f, 0.2f, 0.2f };

View File

@ -415,14 +415,19 @@ namespace SHADE
resourceManager.Free(material);
}
Handle<SHMaterialInstance> SHGraphicsSystem::AddMaterialInstance(Handle<SHMaterial> material)
Handle<SHMaterialInstance> SHGraphicsSystem::AddOrGetBaseMaterialInstance(Handle<SHMaterial> material)
{
return resourceManager.Create<SHMaterialInstance>(material);
return materialInstanceCache.CreateOrGet(resourceManager, material);
}
SHADE::Handle<SHADE::SHMaterialInstance> SHGraphicsSystem::AddMaterialInstance()
SHADE::Handle<SHADE::SHMaterialInstance> SHGraphicsSystem::AddOrGetBaseMaterialInstance()
{
return AddMaterialInstance(defaultMaterial);
return AddOrGetBaseMaterialInstance(defaultMaterial);
}
SHADE::Handle<SHADE::SHMaterialInstance> SHGraphicsSystem::AddMaterialInstanceCopy(Handle<SHMaterialInstance> materialInst)
{
return resourceManager.Create<SHMaterialInstance>(materialInst->GetBaseMaterial());
}
void SHGraphicsSystem::RemoveMaterialInstance(Handle<SHMaterialInstance> materialInstance)

View File

@ -28,6 +28,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/MiddleEnd/Shaders/SHShaderSourceLibrary.h"
#include "Graphics/MiddleEnd/Shaders/SHShaderModuleLibrary.h"
#include "SHMeshLibrary.h"
#include "Graphics/MiddleEnd/Materials/SHMaterialInstanceCache.h"
namespace SHADE
{
@ -119,9 +120,10 @@ namespace SHADE
/* Material Creation Functions */
/*-----------------------------------------------------------------------------*/
Handle<SHMaterial> AddMaterial(Handle<SHVkShaderModule> vertShader, Handle<SHVkShaderModule> fragShader, Handle<SHSubpass> subpass);
void RemoveMaterial(Handle<SHMaterial> material);;
Handle<SHMaterialInstance> AddMaterialInstance();
Handle<SHMaterialInstance> AddMaterialInstance(Handle<SHMaterial> material);
void RemoveMaterial(Handle<SHMaterial> material);
Handle<SHMaterialInstance> AddOrGetBaseMaterialInstance();
Handle<SHMaterialInstance> AddOrGetBaseMaterialInstance(Handle<SHMaterial> material);
Handle<SHMaterialInstance> AddMaterialInstanceCopy(Handle<SHMaterialInstance> materialInst);
void RemoveMaterialInstance(Handle<SHMaterialInstance> materialInstance);
/*-----------------------------------------------------------------------------*/
@ -228,8 +230,9 @@ namespace SHADE
Handle<SHGraphicsGlobalData> globalData;
// Middle End Resources
ResourceManager resourceManager;
ResourceManager resourceManager;
SHMeshLibrary meshLibrary;
SHMaterialInstanceCache materialInstanceCache;
// Viewports
Handle<SHViewport> defaultViewport; // Whole screen
std::vector<Handle<SHViewport>> viewports; // Additional viewports

View File

@ -81,7 +81,7 @@ namespace SHADE
if (!material)
{
SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
material = gfxSystem->AddMaterialInstance(sharedMaterial->GetBaseMaterial());
material = gfxSystem->AddOrGetBaseMaterialInstance(sharedMaterial->GetBaseMaterial());
}
return material;

View File

@ -0,0 +1,47 @@
/************************************************************************************//*!
\file SHMaterialInstanceCache.cpp
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 25, 2022
\brief Contains the definition of SHMaterialInstanceCache's functions.
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"
#include "SHMaterialInstanceCache.h"
#include "Graphics/MiddleEnd/Interface/SHMaterialInstance.h"
#include "Resource/ResourceLibrary.h"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
SHADE::Handle<SHADE::SHMaterialInstance> SHMaterialInstanceCache::CreateOrGet(ResourceManager& manager, Handle<SHMaterial> material)
{
// Check if there is already an existing instance
auto matInst = cache.find(material);
if (matInst == cache.end())
{
// Create and return
return cache.emplace(material, manager.Create<SHMaterialInstance>(material)).first->second;
}
return matInst->second;
}
void SHMaterialInstanceCache::Remove(Handle<SHMaterial> material)
{
cache.erase(material);
}
void SHMaterialInstanceCache::Clear()
{
cache.clear();
}
}

View File

@ -0,0 +1,93 @@
/************************************************************************************//*!
\file SHMaterialInstanceCache.h
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 25, 2022
\brief Contains the definition of SHMaterialInstanceCache.
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 "Resource/Handle.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Forward Declarations */
/*-----------------------------------------------------------------------------------*/
class SHMaterial;
class SHMaterialInstance;
class ResourceManager;
/*-----------------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------------*/
/*************************************************************************************/
/*!
\brief
Creates and caches base SHMaterialInstances. Note that base SHMaterialInstances
refer to SHMaterialInstances with no overrides.
*/
/*************************************************************************************/
class SHMaterialInstanceCache
{
public:
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
/***********************************************************************************/
/*!
\brief
\param material
Material to get the SHMaterialInstance for.
\return
Handle to the base SHMaterialInstance that is mapped to SHMaterial.
*/
/***********************************************************************************/
Handle<SHMaterialInstance> CreateOrGet(ResourceManager& manager, Handle<SHMaterial> material);
/***********************************************************************************/
/*!
\brief
Removes a SHMaterialInstance from the cache with a matching material.
\param material
Handle to a SHMaterial that is used to check for removal.
*/
/***********************************************************************************/
void Remove(Handle<SHMaterial> material);
/***********************************************************************************/
/*!
\brief
Removes all SHMaterialInstances in the cache.
*/
/***********************************************************************************/
void Clear();
private:
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
using MaterialMap = std::unordered_map<Handle<SHMaterial>, Handle<SHMaterialInstance>>;
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
MaterialMap cache;
};
}