Reuse previously loaded techniques

This commit is contained in:
Jan 2022-03-27 16:17:36 +02:00
parent 99f3f6fe0c
commit a47370613b
2 changed files with 40 additions and 8 deletions

View File

@ -4,6 +4,7 @@
#include <sstream> #include <sstream>
#include <type_traits> #include <type_traits>
#include "Utils/ClassUtils.h"
#include "ObjLoading.h" #include "ObjLoading.h"
#include "Game/IW4/IW4.h" #include "Game/IW4/IW4.h"
#include "Game/IW4/TechsetConstantsIW4.h" #include "Game/IW4/TechsetConstantsIW4.h"
@ -12,6 +13,29 @@
using namespace IW4; using namespace IW4;
namespace IW4
{
class TechniqueZoneLoadingState final : IZoneAssetLoaderState
{
std::map<std::string, MaterialTechnique*> m_loaded_techniques;
public:
_NODISCARD MaterialTechnique* FindLoadedTechnique(const std::string& techniqueName) const
{
const auto loadedTechnique = m_loaded_techniques.find(techniqueName);
if (loadedTechnique != m_loaded_techniques.end())
return loadedTechnique->second;
return nullptr;
}
void AddLoadedTechnique(std::string techniqueName, MaterialTechnique* technique)
{
m_loaded_techniques.emplace(std::make_pair(std::move(techniqueName), technique));
}
};
}
void* AssetLoaderTechniqueSet::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) void* AssetLoaderTechniqueSet::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
{ {
auto* techset = memory->Create<MaterialTechniqueSet>(); auto* techset = memory->Create<MaterialTechniqueSet>();
@ -34,8 +58,9 @@ std::string AssetLoaderTechniqueSet::GetTechsetFileName(const std::string& techs
return ss.str(); return ss.str();
} }
MaterialTechnique* AssetLoaderTechniqueSet::LoadTechniqueWithName(const std::string& techniqueName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager) MaterialTechnique* AssetLoaderTechniqueSet::LoadTechniqueFromRaw(const std::string& techniqueName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager)
{ {
const auto techniqueFileName = GetTechniqueFileName(techniqueName);
// TODO: Load technique or use previously loaded one // TODO: Load technique or use previously loaded one
return nullptr; return nullptr;
} }
@ -47,14 +72,21 @@ bool AssetLoaderTechniqueSet::CreateTechsetFromDefinition(const std::string& ass
memset(techset, 0, sizeof(MaterialTechniqueSet)); memset(techset, 0, sizeof(MaterialTechniqueSet));
techset->name = memory->Dup(assetName.c_str()); techset->name = memory->Dup(assetName.c_str());
auto* techniqueZoneLoadingState = manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<TechniqueZoneLoadingState>();
for (auto i = 0u; i < std::extent_v<decltype(MaterialTechniqueSet::techniques)>; i++) for (auto i = 0u; i < std::extent_v<decltype(MaterialTechniqueSet::techniques)>; i++)
{ {
std::string techniqueName; std::string techniqueName;
if (definition.GetTechniqueByIndex(i, techniqueName)) if (definition.GetTechniqueByIndex(i, techniqueName))
{ {
auto* technique = LoadTechniqueWithName(techniqueName, searchPath, memory, manager); auto* technique = techniqueZoneLoadingState->FindLoadedTechnique(techniqueName);
if (!technique)
{
technique = LoadTechniqueFromRaw(techniqueName, searchPath, memory, manager);
if (technique == nullptr) if (technique == nullptr)
return false; return false;
techniqueZoneLoadingState->AddLoadedTechnique(techniqueName, technique);
}
techset->techniques[i] = technique; techset->techniques[i] = technique;
} }
} }

View File

@ -11,7 +11,7 @@ namespace IW4
{ {
static std::string GetTechniqueFileName(const std::string& techniqueName); static std::string GetTechniqueFileName(const std::string& techniqueName);
static std::string GetTechsetFileName(const std::string& techsetAssetName); static std::string GetTechsetFileName(const std::string& techsetAssetName);
static MaterialTechnique* LoadTechniqueWithName(const std::string& techniqueName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager); static MaterialTechnique* LoadTechniqueFromRaw(const std::string& techniqueName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager);
static bool CreateTechsetFromDefinition(const std::string& assetName, const techset::TechsetDefinition& definition, ISearchPath* searchPath, MemoryManager* memory, static bool CreateTechsetFromDefinition(const std::string& assetName, const techset::TechsetDefinition& definition, ISearchPath* searchPath, MemoryManager* memory,
IAssetLoadingManager* manager); IAssetLoadingManager* manager);