Load state maps when loading techniques

This commit is contained in:
Jan 2022-08-13 20:50:40 +02:00
parent 6d15ddcd08
commit 595af125b9
14 changed files with 142 additions and 28 deletions

View File

@ -17,6 +17,8 @@
#include "Techset/TechniqueFileReader.h"
#include "Techset/TechsetFileReader.h"
#include "Shader/D3D9ShaderAnalyser.h"
#include "StateMap/StateMapReader.h"
#include "Techset/TechniqueStateMapCache.h"
#include "Techset/TechsetDefinitionCache.h"
#include "Utils/Alignment.h"
@ -118,10 +120,12 @@ namespace IW4
class TechniqueCreator final : public techset::ITechniqueDefinitionAcceptor
{
const std::string& m_technique_name;
ISearchPath* const m_search_path;
MemoryManager* const m_memory;
IAssetLoadingManager* const m_manager;
TechniqueZoneLoadingState* const m_zone_state;
techset::TechniqueStateMapCache* const m_state_map_cache;
ShaderInfoFromFileSystemCacheState* const m_shader_info_cache;
public:
@ -201,11 +205,15 @@ namespace IW4
std::vector<Pass> m_passes;
std::vector<XAssetInfoGeneric*> m_dependencies;
TechniqueCreator(ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, TechniqueZoneLoadingState* zoneState, ShaderInfoFromFileSystemCacheState* shaderInfoCache)
: m_search_path(searchPath),
TechniqueCreator(const std::string& techniqueName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, TechniqueZoneLoadingState* zoneState,
ShaderInfoFromFileSystemCacheState* shaderInfoCache,
techset::TechniqueStateMapCache* stateMapCache)
: m_technique_name(techniqueName),
m_search_path(searchPath),
m_memory(memory),
m_manager(manager),
m_zone_state(zoneState),
m_state_map_cache(stateMapCache),
m_shader_info_cache(shaderInfoCache)
{
}
@ -406,9 +414,20 @@ namespace IW4
return true;
}
void AcceptStateMap(const std::string& stateMapName) override
bool AcceptStateMap(const std::string& stateMapName, std::string& errorMessage) override
{
// TODO: State maps currently are not used
const auto* stateMap = AssetLoaderTechniqueSet::LoadStateMapDefinition(stateMapName, m_search_path, m_state_map_cache);
if (!stateMap)
{
std::ostringstream ss;
ss << "Failed to load specified state map \"" << stateMapName << "\"";
errorMessage = ss.str();
return false;
}
m_state_map_cache->SetTechniqueUsesStateMap(m_technique_name, stateMap);
return true;
}
static void InitializeArgumentState(const d3d9::ShaderInfo& shaderInfo, std::vector<size_t>& argumentHandledOffsetVector, std::vector<bool>& argumentHandledVector)
@ -986,6 +1005,7 @@ namespace IW4
IAssetLoadingManager* m_manager;
TechniqueZoneLoadingState* m_zone_state;
ShaderInfoFromFileSystemCacheState* m_shader_info_cache;
techset::TechniqueStateMapCache* m_state_map_cache;
static std::string GetTechniqueFileName(const std::string& techniqueName)
{
@ -1185,7 +1205,7 @@ namespace IW4
if (!file.IsOpen())
return nullptr;
TechniqueCreator creator(m_search_path, m_memory, m_manager, m_zone_state, m_shader_info_cache);
TechniqueCreator creator(techniqueName, m_search_path, m_memory, m_manager, m_zone_state, m_shader_info_cache, m_state_map_cache);
const techset::TechniqueFileReader reader(*file.m_stream, techniqueFileName, &creator);
if (!reader.ReadTechniqueDefinition())
return nullptr;
@ -1199,7 +1219,8 @@ namespace IW4
m_memory(memory),
m_manager(manager),
m_zone_state(manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<TechniqueZoneLoadingState>()),
m_shader_info_cache(manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<ShaderInfoFromFileSystemCacheState>())
m_shader_info_cache(manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<ShaderInfoFromFileSystemCacheState>()),
m_state_map_cache(manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<techset::TechniqueStateMapCache>())
{
}
@ -1234,6 +1255,13 @@ std::string AssetLoaderTechniqueSet::GetTechsetFileName(const std::string& techs
return ss.str();
}
std::string AssetLoaderTechniqueSet::GetStateMapFileName(const std::string& stateMapName)
{
std::ostringstream ss;
ss << "statemaps/" << stateMapName << ".sm";
return ss.str();
}
bool AssetLoaderTechniqueSet::CreateTechsetFromDefinition(const std::string& assetName, const techset::TechsetDefinition& definition, ISearchPath* searchPath, MemoryManager* memory,
IAssetLoadingManager* manager)
{
@ -1285,6 +1313,29 @@ techset::TechsetDefinition* AssetLoaderTechniqueSet::LoadTechsetDefinition(const
return techsetDefinitionPtr;
}
const state_map::StateMapDefinition* AssetLoaderTechniqueSet::LoadStateMapDefinition(const std::string& stateMapName, ISearchPath* searchPath, techset::TechniqueStateMapCache* stateMapCache)
{
auto* cachedStateMap = stateMapCache->GetCachedStateMap(stateMapName);
if (cachedStateMap)
return cachedStateMap;
const auto stateMapFileName = GetStateMapFileName(stateMapName);
const auto file = searchPath->Open(stateMapFileName);
if (!file.IsOpen())
return nullptr;
const state_map::StateMapReader reader(*file.m_stream, stateMapFileName, stateMapName, stateMapLayout);
auto stateMapDefinition = reader.ReadStateMapDefinition();
if (!stateMapDefinition)
return nullptr;
const auto* stateMapDefinitionPtr = stateMapDefinition.get();
stateMapCache->AddStateMapToCache(std::move(stateMapDefinition));
return stateMapDefinitionPtr;
}
bool AssetLoaderTechniqueSet::CanLoadFromRaw() const
{
return true;

View File

@ -3,6 +3,8 @@
#include "Game/IW4/IW4.h"
#include "AssetLoading/BasicAssetLoader.h"
#include "SearchPath/ISearchPath.h"
#include "StateMap/StateMapDefinition.h"
#include "Techset/TechniqueStateMapCache.h"
#include "Techset/TechsetDefinition.h"
#include "Techset/TechsetDefinitionCache.h"
@ -11,11 +13,13 @@ namespace IW4
class AssetLoaderTechniqueSet final : public BasicAssetLoader<ASSET_TYPE_TECHNIQUE_SET, MaterialTechniqueSet>
{
static std::string GetTechsetFileName(const std::string& techsetAssetName);
static std::string GetStateMapFileName(const std::string& stateMapName);
static bool CreateTechsetFromDefinition(const std::string& assetName, const techset::TechsetDefinition& definition, ISearchPath* searchPath, MemoryManager* memory,
IAssetLoadingManager* manager);
public:
static techset::TechsetDefinition* LoadTechsetDefinition(const std::string& assetName, ISearchPath* searchPath, techset::TechsetDefinitionCache* definitionCache);
static const state_map::StateMapDefinition* LoadStateMapDefinition(const std::string& stateMapName, ISearchPath* searchPath, techset::TechniqueStateMapCache* stateMapCache);
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
_NODISCARD bool CanLoadFromRaw() const override;

View File

@ -142,8 +142,8 @@ namespace state_map
};
}
StateMapParser::StateMapParser(SimpleLexer* lexer, const StateMapLayout& layout)
: AbstractParser(lexer, std::make_unique<StateMapParserState>(layout))
StateMapParser::StateMapParser(SimpleLexer* lexer, std::string stateMapName, const StateMapLayout& layout)
: AbstractParser(lexer, std::make_unique<StateMapParserState>(std::move(stateMapName), layout))
{
}

View File

@ -15,7 +15,7 @@ namespace state_map
const std::vector<sequence_t*>& GetTestsForState() override;
public:
StateMapParser(SimpleLexer* lexer, const StateMapLayout& layout);
StateMapParser(SimpleLexer* lexer, std::string stateMapName, const StateMapLayout& layout);
_NODISCARD std::unique_ptr<StateMapDefinition> GetStateMapDefinition() const;
_NODISCARD StateMapParserState* GetState() const;
};

View File

@ -2,11 +2,12 @@
using namespace state_map;
StateMapParserState::StateMapParserState(const StateMapLayout& layout)
StateMapParserState::StateMapParserState(std::string stateMapName, const StateMapLayout& layout)
: m_layout(layout),
m_definition(std::make_unique<StateMapDefinition>(layout.m_layout_entries.size())),
m_definition(std::make_unique<StateMapDefinition>(std::move(stateMapName), layout.m_layout_entries.size())),
m_in_entry(false),
m_current_entry_index(0u)
m_current_entry_index(0u),
m_current_rule(nullptr)
{
for (auto i = 0u; i < m_layout.m_layout_entries.size(); i++)
m_valid_state_map_entry_names.emplace(m_layout.m_layout_entries[i].m_name, i);

View File

@ -20,6 +20,6 @@ namespace state_map
size_t m_current_entry_index;
StateMapRule* m_current_rule;
explicit StateMapParserState(const StateMapLayout& layout);
StateMapParserState(std::string stateMapName, const StateMapLayout& layout);
};
}

View File

@ -7,7 +7,8 @@ bool StateMapRule::IsPassthrough() const
return m_values.empty();
}
StateMapDefinition::StateMapDefinition(const size_t entryCount)
: m_state_map_entries(entryCount)
StateMapDefinition::StateMapDefinition(std::string name, const size_t entryCount)
: m_name(std::move(name)),
m_state_map_entries(entryCount)
{
}

View File

@ -1,4 +1,5 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
@ -26,8 +27,9 @@ namespace state_map
class StateMapDefinition
{
public:
explicit StateMapDefinition(size_t entryCount);
StateMapDefinition(std::string name, size_t entryCount);
std::string m_name;
std::vector<StateMapEntry> m_state_map_entries;
};
}

View File

@ -8,8 +8,9 @@
using namespace state_map;
StateMapReader::StateMapReader(std::istream& stream, std::string fileName, const StateMapLayout& layout)
: m_file_name(std::move(fileName)),
StateMapReader::StateMapReader(std::istream& stream, std::string fileName, std::string stateMapName, const StateMapLayout& layout)
: m_state_map_name(std::move(stateMapName)),
m_file_name(std::move(fileName)),
m_state_map_layout(layout)
{
m_base_stream = std::make_unique<ParserSingleInputStream>(stream, m_file_name);
@ -42,7 +43,7 @@ std::unique_ptr<StateMapDefinition> StateMapReader::ReadStateMapDefinition() con
lexerConfig.m_read_floating_point_numbers = false;
const auto lexer = std::make_unique<SimpleLexer>(m_comment_proxy.get(), std::move(lexerConfig));
const auto parser = std::make_unique<StateMapParser>(lexer.get(), m_state_map_layout);
const auto parser = std::make_unique<StateMapParser>(lexer.get(), m_state_map_name, m_state_map_layout);
const auto success = parser->Parse();
if (!success)

View File

@ -13,13 +13,14 @@ namespace state_map
{
class StateMapReader
{
std::string m_state_map_name;
std::string m_file_name;
const StateMapLayout& m_state_map_layout;
std::unique_ptr<IParserLineStream> m_base_stream;
std::unique_ptr<IParserLineStream> m_comment_proxy;
public:
StateMapReader(std::istream& stream, std::string fileName, const StateMapLayout& layout);
StateMapReader(std::istream& stream, std::string fileName, std::string stateMapName, const StateMapLayout& layout);
_NODISCARD bool IsValidEndState(const StateMapParserState* state) const;
_NODISCARD std::unique_ptr<StateMapDefinition> ReadStateMapDefinition() const;

View File

@ -38,7 +38,8 @@ namespace techset
class SequenceStateMap final : public TechniqueParser::sequence_t
{
static constexpr auto CAPTURE_STATE_MAP_NAME = 1;
static constexpr auto CAPTURE_START = 1;
static constexpr auto CAPTURE_STATE_MAP_NAME = 2;
public:
SequenceStateMap()
@ -46,7 +47,7 @@ namespace techset
const SimpleMatcherFactory create(this);
AddMatchers({
create.Keyword("stateMap"),
create.Keyword("stateMap").Capture(CAPTURE_START),
create.String().Capture(CAPTURE_STATE_MAP_NAME),
create.Char(';')
});
@ -55,7 +56,13 @@ namespace techset
protected:
void ProcessMatch(TechniqueParserState* state, SequenceResult<SimpleParserValue>& result) const override
{
state->m_acceptor->AcceptStateMap(result.NextCapture(CAPTURE_STATE_MAP_NAME).StringValue());
const auto& firstToken = result.NextCapture(CAPTURE_START);
std::string errorMessage;
const auto acceptorResult = state->m_acceptor->AcceptStateMap(result.NextCapture(CAPTURE_STATE_MAP_NAME).StringValue(), errorMessage);
if (!acceptorResult)
throw ParsingException(firstToken.GetPos(), std::move(errorMessage));
}
};

View File

@ -76,7 +76,7 @@ namespace techset
virtual void AcceptNextPass() = 0;
virtual bool AcceptEndPass(std::string& errorMessage) = 0;
virtual void AcceptStateMap(const std::string& stateMapName) = 0;
virtual bool AcceptStateMap(const std::string& stateMapName, std::string& errorMessage) = 0;
virtual bool AcceptVertexShader(const std::string& vertexShaderName, std::string& errorMessage) = 0;
virtual bool AcceptPixelShader(const std::string& pixelShaderName, std::string& errorMessage) = 0;

View File

@ -1,3 +1,33 @@
#include "TechniqueStateMapCache.h"
using namespace techset;
const state_map::StateMapDefinition* TechniqueStateMapCache::GetCachedStateMap(const std::string& name) const
{
const auto foundStateMap = m_state_map_cache.find(name);
if (foundStateMap != m_state_map_cache.end())
return foundStateMap->second.get();
return nullptr;
}
void TechniqueStateMapCache::AddStateMapToCache(std::unique_ptr<state_map::StateMapDefinition> stateMap)
{
m_state_map_cache.emplace(std::make_pair(stateMap->m_name, std::move(stateMap)));
}
const state_map::StateMapDefinition* TechniqueStateMapCache::GetStateMapForTechnique(const std::string& techniqueName) const
{
const auto foundTechnique = m_state_map_per_technique.find(techniqueName);
if (foundTechnique != m_state_map_per_technique.end())
return foundTechnique->second;
return nullptr;
}
void TechniqueStateMapCache::SetTechniqueUsesStateMap(std::string techniqueName, const state_map::StateMapDefinition* stateMap)
{
m_state_map_per_technique.emplace(std::make_pair(std::move(techniqueName), stateMap));
}

View File

@ -1,10 +1,26 @@
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include "AssetLoading/IZoneAssetLoaderState.h"
#include "Utils/ClassUtils.h"
#include "StateMap/StateMapDefinition.h"
namespace techset
{
class TechniqueStateMapCache
class TechniqueStateMapCache final : public IZoneAssetLoaderState
{
// TODO: Cache which state map is being used for which technique
// TODO: Cache state map data
public:
_NODISCARD const state_map::StateMapDefinition* GetCachedStateMap(const std::string& name) const;
void AddStateMapToCache(std::unique_ptr<state_map::StateMapDefinition> stateMap);
_NODISCARD const state_map::StateMapDefinition* GetStateMapForTechnique(const std::string& techniqueName) const;
void SetTechniqueUsesStateMap(std::string techniqueName, const state_map::StateMapDefinition* stateMap);
private:
std::unordered_map<std::string, const state_map::StateMapDefinition*> m_state_map_per_technique;
std::unordered_map<std::string, std::unique_ptr<state_map::StateMapDefinition>> m_state_map_cache;
};
}