mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-05-02 08:29:36 +00:00
chore: use templating for t6 techset compilation
This commit is contained in:
@@ -326,7 +326,7 @@ namespace
|
||||
const techset::CommonShaderArgCreatorDestination& input) override
|
||||
{
|
||||
assert(m_shader_info);
|
||||
// TODO
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -577,7 +577,6 @@ namespace
|
||||
return std::move(result);
|
||||
}
|
||||
|
||||
// TODO
|
||||
return NoResult{};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,378 @@
|
||||
#options GAME(T6)
|
||||
|
||||
#filename "Game/" + GAME + "/Techset/TechniqueCompiler" + GAME + ".cpp"
|
||||
|
||||
#set COMPILER_HEADER "\"TechniqueCompiler" + GAME + ".h\""
|
||||
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
|
||||
#set TECHSET_CONSTANTS_HEADER "\"Game/" + GAME + "/Techset/TechsetConstants" + GAME + ".h\""
|
||||
|
||||
#if GAME == "IW3"
|
||||
#define FEATURE_IW3
|
||||
#elif GAME == "IW4"
|
||||
#define FEATURE_IW4
|
||||
#elif GAME == "IW5"
|
||||
#define FEATURE_IW5
|
||||
#elif GAME == "T5"
|
||||
#define FEATURE_T5
|
||||
#elif GAME == "T6"
|
||||
#define FEATURE_T6
|
||||
#endif
|
||||
|
||||
// This file was templated.
|
||||
// See TechniqueCompiler.cpp.template.
|
||||
// Do not modify, changes will be lost.
|
||||
|
||||
#include COMPILER_HEADER
|
||||
|
||||
#include GAME_HEADER
|
||||
#include TECHSET_CONSTANTS_HEADER
|
||||
#include "Techset/CommonShaderArgCreator.h"
|
||||
#include "Techset/CommonTechniqueLoader.h"
|
||||
#include "Techset/LiteralConstsZoneState.h"
|
||||
#include "Utils/StringUtils.h"
|
||||
|
||||
#if defined(FEATURE_T6)
|
||||
#set PRECOMPILED_INDEX_HEADER "\"Game/" + GAME + "/Techset/PrecompiledIndex" + GAME + ".h\""
|
||||
#include PRECOMPILED_INDEX_HEADER
|
||||
#endif
|
||||
|
||||
#include <cassert>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
using namespace GAME;
|
||||
|
||||
#set SHADER_LOADER_CLASS_NAME "TechniqueShaderLoader" + GAME
|
||||
#set COMPILER_CLASS_NAME "TechniqueCompiler" + GAME
|
||||
|
||||
namespace
|
||||
{
|
||||
unsigned ConvertArgumentType(const techset::CommonShaderArgumentType& type)
|
||||
{
|
||||
if (type.m_shader_type == techset::CommonTechniqueShaderType::VERTEX)
|
||||
{
|
||||
switch (type.m_value_type)
|
||||
{
|
||||
case techset::CommonShaderValueType::LITERAL_CONST:
|
||||
return MTL_ARG_LITERAL_VERTEX_CONST;
|
||||
case techset::CommonShaderValueType::MATERIAL_CONST:
|
||||
return MTL_ARG_MATERIAL_VERTEX_CONST;
|
||||
case techset::CommonShaderValueType::CODE_CONST:
|
||||
return MTL_ARG_CODE_VERTEX_CONST;
|
||||
case techset::CommonShaderValueType::MATERIAL_SAMPLER:
|
||||
case techset::CommonShaderValueType::CODE_SAMPLER:
|
||||
default:
|
||||
assert(false);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
assert(type.m_shader_type == techset::CommonTechniqueShaderType::PIXEL);
|
||||
|
||||
switch (type.m_value_type)
|
||||
{
|
||||
case techset::CommonShaderValueType::LITERAL_CONST:
|
||||
return MTL_ARG_LITERAL_PIXEL_CONST;
|
||||
case techset::CommonShaderValueType::MATERIAL_CONST:
|
||||
return MTL_ARG_MATERIAL_PIXEL_CONST;
|
||||
case techset::CommonShaderValueType::CODE_CONST:
|
||||
return MTL_ARG_CODE_PIXEL_CONST;
|
||||
case techset::CommonShaderValueType::MATERIAL_SAMPLER:
|
||||
return MTL_ARG_MATERIAL_PIXEL_SAMPLER;
|
||||
case techset::CommonShaderValueType::CODE_SAMPLER:
|
||||
return MTL_ARG_CODE_PIXEL_SAMPLER;
|
||||
default:
|
||||
assert(false);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void ConvertArgumentValue(MaterialArgumentDef& argValue, const techset::CommonShaderArg& commonArg, MemoryManager& memory, AssetCreationContext& context)
|
||||
{
|
||||
switch (commonArg.m_type.m_value_type)
|
||||
{
|
||||
case techset::CommonShaderValueType::LITERAL_CONST:
|
||||
{
|
||||
const techset::LiteralConst literal(commonArg.m_value.literal_value);
|
||||
argValue.literalConst = context.GetZoneAssetCreationState<techset::LiteralConstsZoneState<float>>().GetAllocatedLiteral(literal)->GamePtr();
|
||||
break;
|
||||
}
|
||||
case techset::CommonShaderValueType::CODE_CONST:
|
||||
argValue.codeConst.index = static_cast<decltype(MaterialArgumentCodeConst::index)>(commonArg.m_value.code_const_source.m_index);
|
||||
argValue.codeConst.firstRow = static_cast<decltype(MaterialArgumentCodeConst::firstRow)>(commonArg.m_value.code_const_source.m_first_row);
|
||||
argValue.codeConst.rowCount = static_cast<decltype(MaterialArgumentCodeConst::rowCount)>(commonArg.m_value.code_const_source.m_row_count);
|
||||
break;
|
||||
case techset::CommonShaderValueType::CODE_SAMPLER:
|
||||
argValue.codeSampler = static_cast<decltype(MaterialArgumentDef::codeSampler)>(commonArg.m_value.code_sampler_source);
|
||||
break;
|
||||
case techset::CommonShaderValueType::MATERIAL_CONST:
|
||||
case techset::CommonShaderValueType::MATERIAL_SAMPLER:
|
||||
argValue.nameHash = static_cast<decltype(MaterialArgumentDef::nameHash)>(commonArg.m_value.name_hash);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ConvertMaterialArgs(MaterialPass& pass, const techset::CommonPass& commonPass, MemoryManager& memory, AssetCreationContext& context)
|
||||
{
|
||||
pass.args = memory.Alloc<MaterialShaderArgument>(commonPass.m_args.size());
|
||||
|
||||
const auto frequencyCount = commonPass.GetFrequencyCounts(commonCodeSourceInfos);
|
||||
|
||||
pass.perObjArgCount = static_cast<unsigned char>(frequencyCount[std::to_underlying(techset::CommonCodeSourceUpdateFrequency::PER_OBJECT)]);
|
||||
pass.perPrimArgCount = static_cast<unsigned char>(frequencyCount[std::to_underlying(techset::CommonCodeSourceUpdateFrequency::PER_PRIM)]);
|
||||
pass.stableArgCount = static_cast<unsigned char>(frequencyCount[std::to_underlying(techset::CommonCodeSourceUpdateFrequency::RARELY)]);
|
||||
|
||||
const auto commonArgCount = commonPass.m_args.size();
|
||||
for (auto argIndex = 0u; argIndex < commonArgCount; argIndex++)
|
||||
{
|
||||
auto& arg = pass.args[argIndex];
|
||||
const auto& commonArg = commonPass.m_args[argIndex];
|
||||
|
||||
arg.type = static_cast<decltype(MaterialShaderArgument::type)>(ConvertArgumentType(commonArg.m_type));
|
||||
|
||||
arg.size = static_cast<decltype(MaterialShaderArgument::size)>(commonArg.m_destination.dx11.m_size);
|
||||
arg.buffer = static_cast<decltype(MaterialShaderArgument::buffer)>(commonArg.m_destination.dx11.m_buffer);
|
||||
|
||||
if (techset::IsConstValueType(commonArg.m_type.m_value_type))
|
||||
{
|
||||
arg.location.offset = static_cast<decltype(MaterialArgumentLocation::offset)>(commonArg.m_destination.dx11.m_location.constant_buffer_offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(techset::IsSamplerValueType(commonArg.m_type.m_value_type));
|
||||
arg.location.textureIndex =
|
||||
static_cast<decltype(MaterialArgumentLocation::textureIndex)>(commonArg.m_destination.dx11.m_location.texture_index);
|
||||
arg.location.samplerIndex =
|
||||
static_cast<decltype(MaterialArgumentLocation::samplerIndex)>(commonArg.m_destination.dx11.m_location.sampler_index);
|
||||
}
|
||||
|
||||
ConvertArgumentValue(arg.u, commonArg, memory, context);
|
||||
}
|
||||
}
|
||||
|
||||
void ConvertVertexDecl(MaterialPass& pass, const techset::CommonVertexDeclaration& commonDecl, AssetCreationContext& context)
|
||||
{
|
||||
std::ostringstream nameStream;
|
||||
for (const auto& entry : commonDecl.m_routing)
|
||||
{
|
||||
nameStream << commonRoutingInfos.GetSourceAbbreviation(entry.m_source);
|
||||
nameStream << commonRoutingInfos.GetDestinationAbbreviation(entry.m_destination);
|
||||
}
|
||||
|
||||
const std::string declName(nameStream.str());
|
||||
auto* vertexDeclAsset = context.LoadSubAsset<SubAssetVertexDecl>(declName);
|
||||
assert(vertexDeclAsset);
|
||||
pass.vertexDecl = vertexDeclAsset ? vertexDeclAsset->Asset() : nullptr;
|
||||
}
|
||||
|
||||
void ConvertMaterialPass(MaterialPass& pass, const techset::CommonPass& commonPass, AssetCreationContext& context, MemoryManager& memory)
|
||||
{
|
||||
ConvertVertexDecl(pass, commonPass.m_vertex_declaration, context);
|
||||
|
||||
if (!commonPass.m_vertex_shader.m_name.empty())
|
||||
{
|
||||
auto* vertexShaderAsset = context.LoadSubAsset<SubAssetVertexShader>(commonPass.m_vertex_shader.m_name);
|
||||
assert(vertexShaderAsset);
|
||||
pass.vertexShader = vertexShaderAsset ? vertexShaderAsset->Asset() : nullptr;
|
||||
}
|
||||
|
||||
if (!commonPass.m_pixel_shader.m_name.empty())
|
||||
{
|
||||
auto* pixelShaderAsset = context.LoadSubAsset<SubAssetPixelShader>(commonPass.m_pixel_shader.m_name);
|
||||
assert(pixelShaderAsset);
|
||||
pass.pixelShader = pixelShaderAsset ? pixelShaderAsset->Asset() : nullptr;
|
||||
}
|
||||
|
||||
ConvertMaterialArgs(pass, commonPass, memory, context);
|
||||
pass.customSamplerFlags = static_cast<decltype(MaterialPass::customSamplerFlags)>(commonPass.m_sampler_flags);
|
||||
}
|
||||
|
||||
bool AnyDeclHasOptionalSource(const MaterialTechnique& technique)
|
||||
{
|
||||
for (auto passIndex = 0u; passIndex < technique.passCount; passIndex++)
|
||||
{
|
||||
const auto& pass = technique.passArray[passIndex];
|
||||
if (!pass.vertexDecl)
|
||||
continue;
|
||||
|
||||
if (pass.vertexDecl->hasOptionalSource)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void UpdateTechniqueFlags(MaterialTechnique& technique, const techset::CommonTechnique& commonTechnique)
|
||||
{
|
||||
std::string lowerTechniqueName(commonTechnique.m_name);
|
||||
utils::MakeStringLowerCase(lowerTechniqueName);
|
||||
|
||||
// Not a particularly cool way to do this but...
|
||||
// the game actually does this :shrug:
|
||||
if (lowerTechniqueName == "zprepass" || lowerTechniqueName.starts_with("pimp_technique_zprepass_")
|
||||
|| lowerTechniqueName.starts_with("pimp_technique_layer_zprepass_") || lowerTechniqueName.starts_with("pimp_technique_buildshadowmap_"))
|
||||
{
|
||||
technique.flags |= TECHNIQUE_FLAG_4;
|
||||
}
|
||||
|
||||
if (AnyDeclHasOptionalSource(technique))
|
||||
technique.flags |= TECHNIQUE_FLAG_8;
|
||||
}
|
||||
|
||||
MaterialTechnique* ConvertTechnique(const techset::CommonTechnique& commonTechnique, AssetCreationContext& context, MemoryManager& memory)
|
||||
{
|
||||
const auto additionalPassCount = std::max(commonTechnique.m_passes.size(), 1uz) - 1uz;
|
||||
auto* technique = static_cast<MaterialTechnique*>(memory.AllocRaw(sizeof(MaterialTechnique) + additionalPassCount * sizeof(MaterialPass)));
|
||||
|
||||
const auto passCount = static_cast<decltype(MaterialTechnique::passCount)>(commonTechnique.m_passes.size());
|
||||
|
||||
technique->name = memory.Dup(commonTechnique.m_name.c_str());
|
||||
|
||||
technique->passCount = passCount;
|
||||
|
||||
for (auto passIndex = 0u; passIndex < passCount; passIndex++)
|
||||
ConvertMaterialPass(technique->passArray[passIndex], commonTechnique.m_passes[passIndex], context, memory);
|
||||
|
||||
// Take common flags and apply further logic
|
||||
technique->flags = static_cast<decltype(MaterialTechnique::flags)>(commonTechnique.m_flags);
|
||||
UpdateTechniqueFlags(*technique, commonTechnique);
|
||||
|
||||
return technique;
|
||||
}
|
||||
|
||||
void ApplyTechFlagsFromMaterial(const Material& material, const Zone& zone)
|
||||
{
|
||||
if (!material.techniqueSet || !material.techniqueSet->name || !material.stateBitsTable)
|
||||
return;
|
||||
|
||||
// Find the techniqueset asset from our zone since the material may link to a different one
|
||||
const auto techniqueSetAsset = zone.m_pools.GetAsset<AssetTechniqueSet>(material.techniqueSet->name);
|
||||
if (!techniqueSetAsset)
|
||||
return;
|
||||
|
||||
for (auto techType = 0u; techType < TECHNIQUE_COUNT; techType++)
|
||||
{
|
||||
auto* technique = techniqueSetAsset->Asset()->techniques[techType];
|
||||
const auto stateBitsEntry = material.stateBitsEntry[techType];
|
||||
|
||||
if (!technique || stateBitsEntry < 0 || static_cast<decltype(Material::stateBitsCount)>(stateBitsEntry) >= material.stateBitsCount)
|
||||
continue;
|
||||
|
||||
const auto& stateBits = material.stateBitsTable[static_cast<decltype(Material::stateBitsCount)>(stateBitsEntry)].loadBits;
|
||||
const bool shouldSetFlag80 = stateBits.structured.depthTestDisabled == 0 || stateBits.structured.depthWrite > 0
|
||||
|| stateBits.structured.depthTest > 0 || stateBits.structured.polygonOffset > 0;
|
||||
if (shouldSetFlag80)
|
||||
technique->flags |= TECHNIQUE_FLAG_80;
|
||||
}
|
||||
}
|
||||
|
||||
class SHADER_LOADER_CLASS_NAME final : public techset::ITechniqueShaderLoader
|
||||
{
|
||||
public:
|
||||
explicit SHADER_LOADER_CLASS_NAME(AssetCreationContext& context)
|
||||
: m_context(context)
|
||||
{
|
||||
}
|
||||
|
||||
std::optional<techset::CommonTechniqueShaderBin> LoadVertexShader(const std::string& name) override
|
||||
{
|
||||
auto* shaderAsset = m_context.LoadSubAsset<SubAssetVertexShader>(name);
|
||||
if (!shaderAsset)
|
||||
return std::nullopt;
|
||||
|
||||
const auto* shader = shaderAsset->Asset();
|
||||
assert(shader->prog.loadDef.program && shader->prog.loadDef.programSize > 0);
|
||||
if (!shader->prog.loadDef.program || shader->prog.loadDef.programSize == 0)
|
||||
return std::nullopt;
|
||||
|
||||
return techset::CommonTechniqueShaderBin{
|
||||
.m_shader_bin = shader->prog.loadDef.program,
|
||||
.m_shader_bin_size = shader->prog.loadDef.programSize,
|
||||
};
|
||||
}
|
||||
|
||||
std::optional<techset::CommonTechniqueShaderBin> LoadPixelShader(const std::string& name) override
|
||||
{
|
||||
auto* shaderAsset = m_context.LoadSubAsset<SubAssetPixelShader>(name);
|
||||
if (!shaderAsset)
|
||||
return std::nullopt;
|
||||
|
||||
const auto* shader = shaderAsset->Asset();
|
||||
assert(shader->prog.loadDef.program && shader->prog.loadDef.programSize > 0);
|
||||
if (!shader->prog.loadDef.program || shader->prog.loadDef.programSize == 0)
|
||||
return std::nullopt;
|
||||
|
||||
return techset::CommonTechniqueShaderBin{
|
||||
.m_shader_bin = shader->prog.loadDef.program,
|
||||
.m_shader_bin_size = shader->prog.loadDef.programSize,
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
AssetCreationContext& m_context;
|
||||
};
|
||||
|
||||
class COMPILER_CLASS_NAME final : public SubAssetCreator<SubAssetTechnique>
|
||||
{
|
||||
public:
|
||||
COMPILER_CLASS_NAME(MemoryManager& memory, Zone& zone, ISearchPath& searchPath)
|
||||
: m_memory(memory),
|
||||
m_zone(zone),
|
||||
m_search_path(searchPath)
|
||||
{
|
||||
}
|
||||
|
||||
AssetCreationResult CreateSubAsset(const std::string& subAssetName, AssetCreationContext& context) override
|
||||
{
|
||||
bool failure = false;
|
||||
SHADER_LOADER_CLASS_NAME shaderLoader(context);
|
||||
const auto commonShaderArgCreator = techset::CommonShaderArgCreator::CreateDx11(shaderLoader, context, commonCodeSourceInfos);
|
||||
|
||||
const auto commonTechnique =
|
||||
techset::LoadCommonTechnique(subAssetName, commonCodeSourceInfos, commonRoutingInfos, *commonShaderArgCreator, m_search_path, failure);
|
||||
|
||||
if (!commonTechnique)
|
||||
return failure ? AssetCreationResult::Failure() : AssetCreationResult::NoAction();
|
||||
|
||||
auto* convertedTechnique = ConvertTechnique(*commonTechnique, context, m_memory);
|
||||
assert(convertedTechnique);
|
||||
|
||||
return AssetCreationResult::Success(context.AddSubAsset(AssetRegistration<SubAssetTechnique>(subAssetName, convertedTechnique)));
|
||||
}
|
||||
|
||||
void FinalizeZone(AssetCreationContext& context) override
|
||||
{
|
||||
const auto materials = m_zone.m_pools.PoolAssets<AssetMaterial>();
|
||||
for (auto* materialAsset : materials)
|
||||
{
|
||||
ApplyTechFlagsFromMaterial(*materialAsset->Asset(), m_zone);
|
||||
}
|
||||
|
||||
const auto techniques = context.PoolSubAssets<SubAssetTechnique>();
|
||||
for (auto* techniqueSubAsset : techniques)
|
||||
{
|
||||
auto& technique = *techniqueSubAsset->Asset();
|
||||
for (auto passIndex = 0u; passIndex < technique.passCount; passIndex++)
|
||||
{
|
||||
ApplyPrecompiledIndex(technique.passArray[passIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryManager& m_memory;
|
||||
Zone& m_zone;
|
||||
ISearchPath& m_search_path;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
#set CREATE_COMPILER_METHOD "CreateTechniqueCompiler" + GAME
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<ISubAssetCreator> CREATE_COMPILER_METHOD(MemoryManager& memory, Zone& zone, ISearchPath& searchPath)
|
||||
{
|
||||
return std::make_unique<COMPILER_CLASS_NAME>(memory, zone, searchPath);
|
||||
}
|
||||
} // namespace techset
|
||||
@@ -0,0 +1,36 @@
|
||||
#options GAME(T6)
|
||||
|
||||
#filename "Game/" + GAME + "/Techset/TechniqueCompiler" + GAME + ".h"
|
||||
|
||||
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
|
||||
|
||||
#if GAME == "IW3"
|
||||
#define FEATURE_IW3
|
||||
#elif GAME == "IW4"
|
||||
#define FEATURE_IW4
|
||||
#elif GAME == "IW5"
|
||||
#define FEATURE_IW5
|
||||
#elif GAME == "T5"
|
||||
#define FEATURE_T5
|
||||
#elif GAME == "T6"
|
||||
#define FEATURE_T6
|
||||
#endif
|
||||
|
||||
// This file was templated.
|
||||
// See TechniqueCompiler.h.template.
|
||||
// Do not modify, changes will be lost.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Asset/IAssetCreator.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#set CREATE_COMPILER_METHOD "CreateTechniqueCompiler" + GAME
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<ISubAssetCreator> CREATE_COMPILER_METHOD(MemoryManager& memory, Zone& zone, ISearchPath& searchPath);
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
#options GAME(T6)
|
||||
|
||||
#filename "Game/" + GAME + "/Techset/TechsetCompiler" + GAME + ".cpp"
|
||||
|
||||
#set COMPILER_HEADER "\"TechsetCompiler" + GAME + ".h\""
|
||||
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
|
||||
#set TECHSET_CONSTANTS_HEADER "\"Game/" + GAME + "/Techset/TechsetConstants" + GAME + ".h\""
|
||||
|
||||
#if GAME == "IW3"
|
||||
#define FEATURE_IW3
|
||||
#elif GAME == "IW4"
|
||||
#define FEATURE_IW4
|
||||
#elif GAME == "IW5"
|
||||
#define FEATURE_IW5
|
||||
#elif GAME == "T5"
|
||||
#define FEATURE_T5
|
||||
#elif GAME == "T6"
|
||||
#define FEATURE_T6
|
||||
#endif
|
||||
|
||||
// This file was templated.
|
||||
// See TechsetCompiler.cpp.template.
|
||||
// Do not modify, changes will be lost.
|
||||
|
||||
#include COMPILER_HEADER
|
||||
|
||||
#include GAME_HEADER
|
||||
#include TECHSET_CONSTANTS_HEADER
|
||||
#include "Techset/CommonTechsetLoader.h"
|
||||
#include "Techset/TechsetCommon.h"
|
||||
|
||||
using namespace GAME;
|
||||
|
||||
#set COMPILER_CLASS_NAME "TechsetCompiler" + GAME
|
||||
|
||||
namespace
|
||||
{
|
||||
MaterialWorldVertexFormat GetWorldVertexFormat(const std::string& name)
|
||||
{
|
||||
if (name.contains("lit_"))
|
||||
{
|
||||
size_t texCount = 0u, normalCount = 0u;
|
||||
techset::CountWorldVertFormatParameters(name, texCount, normalCount);
|
||||
|
||||
// 0 and 1 seem to be treated equally
|
||||
texCount = std::max(texCount, 1uz);
|
||||
normalCount = std::max(normalCount, 1uz);
|
||||
|
||||
if (texCount == 1 && normalCount == 1)
|
||||
return MTL_WORLDVERT_TEX_1_NRM_1;
|
||||
if (texCount == 2 && normalCount == 1)
|
||||
return MTL_WORLDVERT_TEX_2_NRM_1;
|
||||
if (texCount == 2 && normalCount == 2)
|
||||
return MTL_WORLDVERT_TEX_2_NRM_2;
|
||||
if (texCount == 3 && normalCount == 1)
|
||||
return MTL_WORLDVERT_TEX_3_NRM_1;
|
||||
if (texCount == 3 && normalCount == 2)
|
||||
return MTL_WORLDVERT_TEX_3_NRM_2;
|
||||
if (texCount == 3 && normalCount == 3)
|
||||
return MTL_WORLDVERT_TEX_3_NRM_3;
|
||||
if (texCount == 4 && normalCount == 1)
|
||||
return MTL_WORLDVERT_TEX_4_NRM_1;
|
||||
if (texCount == 4 && normalCount == 2)
|
||||
return MTL_WORLDVERT_TEX_4_NRM_2;
|
||||
if (texCount == 4 && normalCount == 3)
|
||||
return MTL_WORLDVERT_TEX_4_NRM_3;
|
||||
}
|
||||
|
||||
return static_cast<MaterialWorldVertexFormat>(0);
|
||||
}
|
||||
|
||||
MaterialType GetMaterialType(const std::string& name)
|
||||
{
|
||||
for (unsigned materialTypeIndex = MTL_TYPE_MODEL; materialTypeIndex < MTL_TYPE_COUNT; materialTypeIndex++)
|
||||
{
|
||||
if (name.starts_with(g_materialTypeInfo[materialTypeIndex].techniqueSetPrefix))
|
||||
return static_cast<MaterialType>(materialTypeIndex);
|
||||
}
|
||||
|
||||
return MTL_TYPE_DEFAULT;
|
||||
}
|
||||
|
||||
void ApplyMaterialTypeToTechnique(MaterialTechnique& technique, const MaterialType materialType)
|
||||
{
|
||||
for (auto passIndex = 0u; passIndex < technique.passCount; passIndex++)
|
||||
{
|
||||
technique.passArray[passIndex].materialType = materialType;
|
||||
}
|
||||
}
|
||||
|
||||
MaterialTechniqueSet* ConvertTechniqueSet(const techset::CommonTechset& commonTechset, MemoryManager& memory)
|
||||
{
|
||||
auto* techset = memory.Alloc<MaterialTechniqueSet>();
|
||||
techset->name = memory.Dup(commonTechset.m_name.c_str());
|
||||
techset->worldVertFormat = GetWorldVertexFormat(commonTechset.m_name);
|
||||
|
||||
return techset;
|
||||
}
|
||||
|
||||
class COMPILER_CLASS_NAME final : public AssetCreator<AssetTechniqueSet>
|
||||
{
|
||||
public:
|
||||
COMPILER_CLASS_NAME(ISearchPath& searchPath, MemoryManager& memory)
|
||||
: m_search_path(searchPath),
|
||||
m_memory(memory)
|
||||
{
|
||||
}
|
||||
|
||||
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
|
||||
{
|
||||
bool failure = false;
|
||||
const auto commonTechset = techset::LoadCommonTechset(assetName, commonTechniqueTypeNames, m_search_path, failure);
|
||||
if (!commonTechset)
|
||||
return failure ? AssetCreationResult::Failure() : AssetCreationResult::NoAction();
|
||||
|
||||
auto* techset = ConvertTechniqueSet(*commonTechset, m_memory);
|
||||
const auto materialType = GetMaterialType(assetName);
|
||||
|
||||
for (auto techniqueIndex = 0u; techniqueIndex < std::extent_v<decltype(MaterialTechniqueSet::techniques)>; techniqueIndex++)
|
||||
{
|
||||
const auto& techniqueName = commonTechset->m_technique_names[techniqueIndex];
|
||||
if (techniqueName.empty())
|
||||
continue;
|
||||
|
||||
auto* technique = context.LoadSubAsset<SubAssetTechnique>(techniqueName);
|
||||
if (!technique)
|
||||
return AssetCreationResult::Failure();
|
||||
|
||||
techset->techniques[techniqueIndex] = technique->Asset();
|
||||
|
||||
// Another techset may override this for the technique
|
||||
// but the game determines the material type by techset name.
|
||||
// So this may just be a constraint that cannot be changed.
|
||||
ApplyMaterialTypeToTechnique(*techset->techniques[techniqueIndex], materialType);
|
||||
}
|
||||
|
||||
return AssetCreationResult::Success(context.AddAsset(AssetRegistration<AssetTechniqueSet>(assetName, techset)));
|
||||
}
|
||||
|
||||
private:
|
||||
ISearchPath& m_search_path;
|
||||
MemoryManager& m_memory;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
#set CREATE_COMPILER_METHOD "CreateTechsetCompiler" + GAME
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<IAssetCreator> CREATE_COMPILER_METHOD(MemoryManager& memory, ISearchPath& searchPath)
|
||||
{
|
||||
return std::make_unique<COMPILER_CLASS_NAME>(searchPath, memory);
|
||||
}
|
||||
} // namespace techset
|
||||
@@ -0,0 +1,36 @@
|
||||
#options GAME(T6)
|
||||
|
||||
#filename "Game/" + GAME + "/Techset/TechsetCompiler" + GAME + ".h"
|
||||
|
||||
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
|
||||
|
||||
#if GAME == "IW3"
|
||||
#define FEATURE_IW3
|
||||
#elif GAME == "IW4"
|
||||
#define FEATURE_IW4
|
||||
#elif GAME == "IW5"
|
||||
#define FEATURE_IW5
|
||||
#elif GAME == "T5"
|
||||
#define FEATURE_T5
|
||||
#elif GAME == "T6"
|
||||
#define FEATURE_T6
|
||||
#endif
|
||||
|
||||
// This file was templated.
|
||||
// See TechsetCompiler.h.template.
|
||||
// Do not modify, changes will be lost.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Asset/IAssetCreator.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#set CREATE_COMPILER_METHOD "CreateTechsetCompiler" + GAME
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<IAssetCreator> CREATE_COMPILER_METHOD(MemoryManager& memory, ISearchPath& searchPath);
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
#options GAME(T6)
|
||||
|
||||
#filename "Game/" + GAME + "/Techset/VertexDeclCompiler" + GAME + ".cpp"
|
||||
|
||||
#set COMPILER_HEADER "\"VertexDeclCompiler" + GAME + ".h\""
|
||||
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
|
||||
#set TECHSET_CONSTANTS_HEADER "\"Game/" + GAME + "/Techset/TechsetConstants" + GAME + ".h\""
|
||||
|
||||
#if GAME == "IW3"
|
||||
#define FEATURE_IW3
|
||||
#elif GAME == "IW4"
|
||||
#define FEATURE_IW4
|
||||
#elif GAME == "IW5"
|
||||
#define FEATURE_IW5
|
||||
#elif GAME == "T5"
|
||||
#define FEATURE_T5
|
||||
#elif GAME == "T6"
|
||||
#define FEATURE_T6
|
||||
#endif
|
||||
|
||||
// This file was templated.
|
||||
// See VertexDeclCompiler.cpp.template.
|
||||
// Do not modify, changes will be lost.
|
||||
|
||||
#include COMPILER_HEADER
|
||||
|
||||
#include GAME_HEADER
|
||||
#include TECHSET_CONSTANTS_HEADER
|
||||
#include "Techset/CommonVertexDeclCreator.h"
|
||||
#include "Utils/Logging/Log.h"
|
||||
|
||||
using namespace GAME;
|
||||
|
||||
#set COMPILER_CLASS_NAME "VertexDeclCompiler" + GAME
|
||||
|
||||
#if defined(FEATURE_T6)
|
||||
#define ABSTRACT_CREATOR_NAME SubAssetCreator
|
||||
#define OVERRIDEN_CREATOR_METHOD CreateSubAsset
|
||||
#define ASSET_NAME SubAssetVertexDecl
|
||||
#define INTERFACE_NAME ISubAssetCreator
|
||||
#else
|
||||
#define ABSTRACT_CREATOR_NAME AssetCreator
|
||||
#define OVERRIDEN_CREATOR_METHOD CreateAsset
|
||||
#define ASSET_NAME AssetVertexDecl
|
||||
#define INTERFACE_NAME IAssetCreator
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
class COMPILER_CLASS_NAME final : public ABSTRACT_CREATOR_NAME<ASSET_NAME>
|
||||
{
|
||||
public:
|
||||
explicit COMPILER_CLASS_NAME(MemoryManager& memory)
|
||||
: m_memory(memory)
|
||||
{
|
||||
}
|
||||
|
||||
AssetCreationResult OVERRIDEN_CREATOR_METHOD(const std::string& assetName, AssetCreationContext& context) override
|
||||
{
|
||||
const auto commonVertexDecl = techset::CreateVertexDeclFromName(assetName, commonRoutingInfos);
|
||||
if (!commonVertexDecl)
|
||||
return AssetCreationResult::Failure();
|
||||
|
||||
if (commonVertexDecl->m_routing.size() > std::extent_v<decltype(MaterialVertexStreamRouting::data)>)
|
||||
{
|
||||
con::error("Vertex declaration can only have up to {} routing entries", std::extent_v<decltype(MaterialVertexStreamRouting::data)>);
|
||||
return AssetCreationResult::Failure();
|
||||
}
|
||||
|
||||
auto* vertexDecl = m_memory.Alloc<MaterialVertexDeclaration>();
|
||||
|
||||
for (const auto& commonRoutingEntry : commonVertexDecl->m_routing)
|
||||
{
|
||||
vertexDecl->routing.data[vertexDecl->streamCount].source = commonRoutingEntry.m_source;
|
||||
vertexDecl->routing.data[vertexDecl->streamCount].dest = commonRoutingEntry.m_destination;
|
||||
vertexDecl->hasOptionalSource = vertexDecl->hasOptionalSource || commonRoutingEntry.m_source >= STREAM_SRC_OPTIONAL_BEGIN;
|
||||
|
||||
vertexDecl->streamCount++;
|
||||
}
|
||||
|
||||
return AssetCreationResult::Success(context.AddSubAsset(AssetRegistration<ASSET_NAME>(assetName, vertexDecl)));
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryManager& m_memory;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
#set CREATE_COMPILER_METHOD "CreateVertexDeclCompiler" + GAME
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<INTERFACE_NAME> CREATE_COMPILER_METHOD(MemoryManager& memory)
|
||||
{
|
||||
return std::make_unique<COMPILER_CLASS_NAME>(memory);
|
||||
}
|
||||
} // namespace techset
|
||||
@@ -0,0 +1,41 @@
|
||||
#options GAME(T6)
|
||||
|
||||
#filename "Game/" + GAME + "/Techset/VertexDeclCompiler" + GAME + ".h"
|
||||
|
||||
#set GAME_HEADER "\"Game/" + GAME + "/" + GAME + ".h\""
|
||||
|
||||
#if GAME == "IW3"
|
||||
#define FEATURE_IW3
|
||||
#elif GAME == "IW4"
|
||||
#define FEATURE_IW4
|
||||
#elif GAME == "IW5"
|
||||
#define FEATURE_IW5
|
||||
#elif GAME == "T5"
|
||||
#define FEATURE_T5
|
||||
#elif GAME == "T6"
|
||||
#define FEATURE_T6
|
||||
#endif
|
||||
|
||||
// This file was templated.
|
||||
// See VertexDeclCompiler.h.template.
|
||||
// Do not modify, changes will be lost.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Asset/IAssetCreator.h"
|
||||
#include "Utils/MemoryManager.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#set CREATE_COMPILER_METHOD "CreateVertexDeclCompiler" + GAME
|
||||
|
||||
#if defined(FEATURE_T6)
|
||||
#define INTERFACE_NAME ISubAssetCreator
|
||||
#else
|
||||
#define INTERFACE_NAME IAssetCreator
|
||||
#endif
|
||||
|
||||
namespace techset
|
||||
{
|
||||
std::unique_ptr<INTERFACE_NAME> CREATE_COMPILER_METHOD(MemoryManager& memory);
|
||||
}
|
||||
Reference in New Issue
Block a user