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/TechniqueFileReader.h"
#include "Techset/TechsetFileReader.h" #include "Techset/TechsetFileReader.h"
#include "Shader/D3D9ShaderAnalyser.h" #include "Shader/D3D9ShaderAnalyser.h"
#include "StateMap/StateMapReader.h"
#include "Techset/TechniqueStateMapCache.h"
#include "Techset/TechsetDefinitionCache.h" #include "Techset/TechsetDefinitionCache.h"
#include "Utils/Alignment.h" #include "Utils/Alignment.h"
@ -118,10 +120,12 @@ namespace IW4
class TechniqueCreator final : public techset::ITechniqueDefinitionAcceptor class TechniqueCreator final : public techset::ITechniqueDefinitionAcceptor
{ {
const std::string& m_technique_name;
ISearchPath* const m_search_path; ISearchPath* const m_search_path;
MemoryManager* const m_memory; MemoryManager* const m_memory;
IAssetLoadingManager* const m_manager; IAssetLoadingManager* const m_manager;
TechniqueZoneLoadingState* const m_zone_state; TechniqueZoneLoadingState* const m_zone_state;
techset::TechniqueStateMapCache* const m_state_map_cache;
ShaderInfoFromFileSystemCacheState* const m_shader_info_cache; ShaderInfoFromFileSystemCacheState* const m_shader_info_cache;
public: public:
@ -201,11 +205,15 @@ namespace IW4
std::vector<Pass> m_passes; std::vector<Pass> m_passes;
std::vector<XAssetInfoGeneric*> m_dependencies; std::vector<XAssetInfoGeneric*> m_dependencies;
TechniqueCreator(ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, TechniqueZoneLoadingState* zoneState, ShaderInfoFromFileSystemCacheState* shaderInfoCache) TechniqueCreator(const std::string& techniqueName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, TechniqueZoneLoadingState* zoneState,
: m_search_path(searchPath), ShaderInfoFromFileSystemCacheState* shaderInfoCache,
techset::TechniqueStateMapCache* stateMapCache)
: m_technique_name(techniqueName),
m_search_path(searchPath),
m_memory(memory), m_memory(memory),
m_manager(manager), m_manager(manager),
m_zone_state(zoneState), m_zone_state(zoneState),
m_state_map_cache(stateMapCache),
m_shader_info_cache(shaderInfoCache) m_shader_info_cache(shaderInfoCache)
{ {
} }
@ -406,9 +414,20 @@ namespace IW4
return true; 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) 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; IAssetLoadingManager* m_manager;
TechniqueZoneLoadingState* m_zone_state; TechniqueZoneLoadingState* m_zone_state;
ShaderInfoFromFileSystemCacheState* m_shader_info_cache; ShaderInfoFromFileSystemCacheState* m_shader_info_cache;
techset::TechniqueStateMapCache* m_state_map_cache;
static std::string GetTechniqueFileName(const std::string& techniqueName) static std::string GetTechniqueFileName(const std::string& techniqueName)
{ {
@ -1185,7 +1205,7 @@ namespace IW4
if (!file.IsOpen()) if (!file.IsOpen())
return nullptr; 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); const techset::TechniqueFileReader reader(*file.m_stream, techniqueFileName, &creator);
if (!reader.ReadTechniqueDefinition()) if (!reader.ReadTechniqueDefinition())
return nullptr; return nullptr;
@ -1199,7 +1219,8 @@ namespace IW4
m_memory(memory), m_memory(memory),
m_manager(manager), m_manager(manager),
m_zone_state(manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<TechniqueZoneLoadingState>()), 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(); 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, bool AssetLoaderTechniqueSet::CreateTechsetFromDefinition(const std::string& assetName, const techset::TechsetDefinition& definition, ISearchPath* searchPath, MemoryManager* memory,
IAssetLoadingManager* manager) IAssetLoadingManager* manager)
{ {
@ -1285,6 +1313,29 @@ techset::TechsetDefinition* AssetLoaderTechniqueSet::LoadTechsetDefinition(const
return techsetDefinitionPtr; 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 bool AssetLoaderTechniqueSet::CanLoadFromRaw() const
{ {
return true; return true;

View File

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

View File

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

View File

@ -2,11 +2,12 @@
using namespace state_map; using namespace state_map;
StateMapParserState::StateMapParserState(const StateMapLayout& layout) StateMapParserState::StateMapParserState(std::string stateMapName, const StateMapLayout& layout)
: m_layout(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_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++) 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); 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; size_t m_current_entry_index;
StateMapRule* m_current_rule; 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(); return m_values.empty();
} }
StateMapDefinition::StateMapDefinition(const size_t entryCount) StateMapDefinition::StateMapDefinition(std::string name, const size_t entryCount)
: m_state_map_entries(entryCount) : m_name(std::move(name)),
m_state_map_entries(entryCount)
{ {
} }

View File

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

View File

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

View File

@ -13,13 +13,14 @@ namespace state_map
{ {
class StateMapReader class StateMapReader
{ {
std::string m_state_map_name;
std::string m_file_name; std::string m_file_name;
const StateMapLayout& m_state_map_layout; const StateMapLayout& m_state_map_layout;
std::unique_ptr<IParserLineStream> m_base_stream; std::unique_ptr<IParserLineStream> m_base_stream;
std::unique_ptr<IParserLineStream> m_comment_proxy; std::unique_ptr<IParserLineStream> m_comment_proxy;
public: 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 bool IsValidEndState(const StateMapParserState* state) const;
_NODISCARD std::unique_ptr<StateMapDefinition> ReadStateMapDefinition() const; _NODISCARD std::unique_ptr<StateMapDefinition> ReadStateMapDefinition() const;

View File

@ -29,7 +29,7 @@ namespace techset
assert(state->m_in_pass == true); assert(state->m_in_pass == true);
std::string errorMessage; std::string errorMessage;
if(!state->m_acceptor->AcceptEndPass(errorMessage)) if (!state->m_acceptor->AcceptEndPass(errorMessage))
throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), errorMessage); throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), errorMessage);
state->m_in_pass = false; state->m_in_pass = false;
@ -38,7 +38,8 @@ namespace techset
class SequenceStateMap final : public TechniqueParser::sequence_t 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: public:
SequenceStateMap() SequenceStateMap()
@ -46,7 +47,7 @@ namespace techset
const SimpleMatcherFactory create(this); const SimpleMatcherFactory create(this);
AddMatchers({ AddMatchers({
create.Keyword("stateMap"), create.Keyword("stateMap").Capture(CAPTURE_START),
create.String().Capture(CAPTURE_STATE_MAP_NAME), create.String().Capture(CAPTURE_STATE_MAP_NAME),
create.Char(';') create.Char(';')
}); });
@ -55,7 +56,13 @@ namespace techset
protected: protected:
void ProcessMatch(TechniqueParserState* state, SequenceResult<SimpleParserValue>& result) const override 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 void AcceptNextPass() = 0;
virtual bool AcceptEndPass(std::string& errorMessage) = 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 AcceptVertexShader(const std::string& vertexShaderName, std::string& errorMessage) = 0;
virtual bool AcceptPixelShader(const std::string& pixelShaderName, std::string& errorMessage) = 0; virtual bool AcceptPixelShader(const std::string& pixelShaderName, std::string& errorMessage) = 0;

View File

@ -1,3 +1,33 @@
#include "TechniqueStateMapCache.h" #include "TechniqueStateMapCache.h"
using namespace techset; 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 #pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include "AssetLoading/IZoneAssetLoaderState.h"
#include "Utils/ClassUtils.h"
#include "StateMap/StateMapDefinition.h"
namespace techset namespace techset
{ {
class TechniqueStateMapCache class TechniqueStateMapCache final : public IZoneAssetLoaderState
{ {
// TODO: Cache which state map is being used for which technique public:
// TODO: Cache state map data _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;
}; };
} }