Add TechsetDefinitionCache to cache loaded techset definitions

This commit is contained in:
Jan 2022-08-06 10:52:35 +02:00
parent b474109452
commit a3a01660d6
5 changed files with 69 additions and 14 deletions

View File

@ -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<MaterialTechniqueSet>::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<scr_string_t>());
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<decltype(techniqueTypeNames)>);
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<decltype(techniqueTypeNames)>);
const auto techsetDefinition = reader.ReadTechsetDefinition();
auto* definitionCache = manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<techset::TechsetDefinitionCache>();
const auto* techsetDefinition = LoadTechsetDefinition(assetName, searchPath, definitionCache);
if (techsetDefinition)
return CreateTechsetFromDefinition(assetName, *techsetDefinition, searchPath, memory, manager);

View File

@ -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;

View File

@ -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<TechsetDefinition> definition)
{
m_cache.emplace(std::make_pair(std::move(name), std::move(definition)));
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <unordered_map>
#include <string>
#include <memory>
#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<TechsetDefinition> definition);
private:
std::unordered_map<std::string, std::unique_ptr<TechsetDefinition>> m_cache;
};
}

View File

@ -20,6 +20,6 @@ namespace techset
public:
TechsetFileReader(std::istream& stream, std::string fileName, const char** validTechniqueTypeNames, size_t validTechniqueTypeNameCount);
_NODISCARD std::unique_ptr<techset::TechsetDefinition> ReadTechsetDefinition() const;
_NODISCARD std::unique_ptr<TechsetDefinition> ReadTechsetDefinition() const;
};
}