From a3a01660d69283ce6b073fa22b38f821e5ba13b1 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 6 Aug 2022 10:52:35 +0200 Subject: [PATCH] Add TechsetDefinitionCache to cache loaded techset definitions --- .../AssetLoaders/AssetLoaderTechniqueSet.cpp | 38 ++++++++++++------- .../AssetLoaders/AssetLoaderTechniqueSet.h | 3 ++ .../Techset/TechsetDefinitionCache.cpp | 18 +++++++++ .../Techset/TechsetDefinitionCache.h | 22 +++++++++++ src/ObjLoading/Techset/TechsetFileReader.h | 2 +- 5 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 src/ObjLoading/Techset/TechsetDefinitionCache.cpp create mode 100644 src/ObjLoading/Techset/TechsetDefinitionCache.h diff --git a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderTechniqueSet.cpp b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderTechniqueSet.cpp index 44905d32..d555c112 100644 --- a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderTechniqueSet.cpp +++ b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderTechniqueSet.cpp @@ -17,6 +17,7 @@ #include "Techset/TechniqueFileReader.h" #include "Techset/TechsetFileReader.h" #include "Shader/D3D9ShaderAnalyser.h" +#include "Techset/TechsetDefinitionCache.h" #include "Utils/Alignment.h" using namespace IW4; @@ -1083,9 +1084,9 @@ namespace IW4 break; } } - else if(arg.m_arg.type == MTL_ARG_CODE_VERTEX_CONST || arg.m_arg.type == MTL_ARG_CODE_PIXEL_CONST) + else if (arg.m_arg.type == MTL_ARG_CODE_VERTEX_CONST || arg.m_arg.type == MTL_ARG_CODE_PIXEL_CONST) { - switch(arg.m_arg.u.codeConst.index) + switch (arg.m_arg.u.codeConst.index) { case CONST_SRC_CODE_LIGHT_SPOTDIR: case CONST_SRC_CODE_LIGHT_SPOTFACTORS: @@ -1259,14 +1260,31 @@ bool AssetLoaderTechniqueSet::CreateTechsetFromDefinition(const std::string& ass } } - const auto* disAsset = GlobalAssetPool::GetAssetByName("distortion_scale"); - const auto* dis = disAsset ? disAsset->Asset() : nullptr; - manager->AddAsset(ASSET_TYPE_TECHNIQUE_SET, assetName, techset, std::vector(dependencies.begin(), dependencies.end()), std::vector()); return true; } +techset::TechsetDefinition* AssetLoaderTechniqueSet::LoadTechsetDefinition(const std::string& assetName, ISearchPath* searchPath, techset::TechsetDefinitionCache* definitionCache) +{ + auto* cachedTechsetDefinition = definitionCache->GetCachedTechsetDefinition(assetName); + if (cachedTechsetDefinition) + return cachedTechsetDefinition; + + const auto techsetFileName = GetTechsetFileName(assetName); + const auto file = searchPath->Open(techsetFileName); + if (!file.IsOpen()) + return nullptr; + + const techset::TechsetFileReader reader(*file.m_stream, techsetFileName, techniqueTypeNames, std::extent_v); + auto techsetDefinition = reader.ReadTechsetDefinition(); + auto* techsetDefinitionPtr = techsetDefinition.get(); + + definitionCache->AddTechsetDefinitionToCache(assetName, std::move(techsetDefinition)); + + return techsetDefinitionPtr; +} + bool AssetLoaderTechniqueSet::CanLoadFromRaw() const { return true; @@ -1274,14 +1292,8 @@ bool AssetLoaderTechniqueSet::CanLoadFromRaw() const bool AssetLoaderTechniqueSet::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const { - const auto techsetFileName = GetTechsetFileName(assetName); - const auto file = searchPath->Open(techsetFileName); - if (!file.IsOpen()) - return false; - - const techset::TechsetFileReader reader(*file.m_stream, techsetFileName, techniqueTypeNames, std::extent_v); - const auto techsetDefinition = reader.ReadTechsetDefinition(); - + auto* definitionCache = manager->GetAssetLoadingContext()->GetZoneAssetLoaderState(); + const auto* techsetDefinition = LoadTechsetDefinition(assetName, searchPath, definitionCache); if (techsetDefinition) return CreateTechsetFromDefinition(assetName, *techsetDefinition, searchPath, memory, manager); diff --git a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderTechniqueSet.h b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderTechniqueSet.h index 6660a791..c11d95dc 100644 --- a/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderTechniqueSet.h +++ b/src/ObjLoading/Game/IW4/AssetLoaders/AssetLoaderTechniqueSet.h @@ -4,6 +4,7 @@ #include "AssetLoading/BasicAssetLoader.h" #include "SearchPath/ISearchPath.h" #include "Techset/TechsetDefinition.h" +#include "Techset/TechsetDefinitionCache.h" namespace IW4 { @@ -14,6 +15,8 @@ namespace IW4 IAssetLoadingManager* manager); public: + static techset::TechsetDefinition* LoadTechsetDefinition(const std::string& assetName, ISearchPath* searchPath, techset::TechsetDefinitionCache* definitionCache); + _NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override; _NODISCARD bool CanLoadFromRaw() const override; bool LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override; diff --git a/src/ObjLoading/Techset/TechsetDefinitionCache.cpp b/src/ObjLoading/Techset/TechsetDefinitionCache.cpp new file mode 100644 index 00000000..b4041b35 --- /dev/null +++ b/src/ObjLoading/Techset/TechsetDefinitionCache.cpp @@ -0,0 +1,18 @@ +#include "TechsetDefinitionCache.h" + +using namespace techset; + +TechsetDefinition* TechsetDefinitionCache::GetCachedTechsetDefinition(const std::string& techsetName) const +{ + const auto foundTechset = m_cache.find(techsetName); + + if (foundTechset != m_cache.end()) + return foundTechset->second.get(); + + return nullptr; +} + +void TechsetDefinitionCache::AddTechsetDefinitionToCache(std::string name, std::unique_ptr definition) +{ + m_cache.emplace(std::make_pair(std::move(name), std::move(definition))); +} diff --git a/src/ObjLoading/Techset/TechsetDefinitionCache.h b/src/ObjLoading/Techset/TechsetDefinitionCache.h new file mode 100644 index 00000000..9ca3ad96 --- /dev/null +++ b/src/ObjLoading/Techset/TechsetDefinitionCache.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + +#include "Utils/ClassUtils.h" +#include "TechsetDefinition.h" +#include "AssetLoading/IZoneAssetLoaderState.h" + +namespace techset +{ + class TechsetDefinitionCache final : public IZoneAssetLoaderState + { + public: + _NODISCARD TechsetDefinition* GetCachedTechsetDefinition(const std::string& techsetName) const; + void AddTechsetDefinitionToCache(std::string name, std::unique_ptr definition); + + private: + std::unordered_map> m_cache; + }; +} diff --git a/src/ObjLoading/Techset/TechsetFileReader.h b/src/ObjLoading/Techset/TechsetFileReader.h index d98f76de..d8eacb5c 100644 --- a/src/ObjLoading/Techset/TechsetFileReader.h +++ b/src/ObjLoading/Techset/TechsetFileReader.h @@ -20,6 +20,6 @@ namespace techset public: TechsetFileReader(std::istream& stream, std::string fileName, const char** validTechniqueTypeNames, size_t validTechniqueTypeNameCount); - _NODISCARD std::unique_ptr ReadTechsetDefinition() const; + _NODISCARD std::unique_ptr ReadTechsetDefinition() const; }; }