From 6eb0ec41790ec9e7e841ca8d9adece35d9b8a466 Mon Sep 17 00:00:00 2001 From: Jan Laupetin Date: Wed, 4 Mar 2026 22:12:39 +0000 Subject: [PATCH] chore: reorganize precompiled index logic into separate file --- .../Game/T6/Techset/PrecompiledIndexT6.cpp | 256 ++++++++++++++++++ .../Game/T6/Techset/PrecompiledIndexT6.h | 8 + .../Game/T6/Techset/TechniqueCompilerT6.cpp | 240 +--------------- 3 files changed, 265 insertions(+), 239 deletions(-) create mode 100644 src/ObjCompiling/Game/T6/Techset/PrecompiledIndexT6.cpp create mode 100644 src/ObjCompiling/Game/T6/Techset/PrecompiledIndexT6.h diff --git a/src/ObjCompiling/Game/T6/Techset/PrecompiledIndexT6.cpp b/src/ObjCompiling/Game/T6/Techset/PrecompiledIndexT6.cpp new file mode 100644 index 00000000..30b463cb --- /dev/null +++ b/src/ObjCompiling/Game/T6/Techset/PrecompiledIndexT6.cpp @@ -0,0 +1,256 @@ +#include "PrecompiledIndexT6.h" + +#include "Game/T6/Techset/TechsetConstantsT6.h" +#include "Techset/CommonTechnique.h" + +#include +#include + +using namespace T6; + +namespace +{ + class PrecompiledIndexMapping + { + public: + VertexShaderPrecompiledIndex m_value; + std::optional m_material_type; + std::vector m_vertex_const_constraints; + std::vector m_custom_sampler_constraints; + }; + + // clang-format off + inline PrecompiledIndexMapping precompiledIndexMappings[]{ + { + .m_value = VERTEX_SHADER_MODEL_LIT, + .m_material_type = MTL_TYPE_MODEL_VERTCOL, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + CONST_SRC_CODE_GRID_LIGHTING_SH_2, + }, + .m_custom_sampler_constraints = {}, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT, + .m_material_type = MTL_TYPE_MODEL_VERTCOL, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, + }, + .m_custom_sampler_constraints = {}, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT, + .m_material_type = MTL_TYPE_MODEL_VERTCOL, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + CONST_SRC_CODE_GRID_LIGHTING_SH_2, + }, + .m_custom_sampler_constraints = {}, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT, + .m_material_type = MTL_TYPE_MODEL_VERTCOL, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + }, + .m_custom_sampler_constraints = {}, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT, + .m_material_type = MTL_TYPE_MODEL_VERTCOL, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + CONST_SRC_CODE_GRID_LIGHTING_SH_2, + }, + .m_custom_sampler_constraints = { + CUSTOM_SAMPLER_REFLECTION_PROBE, + }, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT, + .m_material_type = MTL_TYPE_MODEL_VERTCOL, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + }, + .m_custom_sampler_constraints = { + CUSTOM_SAMPLER_REFLECTION_PROBE, + }, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT, + .m_material_type = MTL_TYPE_MODEL_VERTCOL, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + CONST_SRC_CODE_GRID_LIGHTING_SH_2, + }, + .m_custom_sampler_constraints = { + CUSTOM_SAMPLER_REFLECTION_PROBE, + }, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, + .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + CONST_SRC_CODE_GRID_LIGHTING_SH_2, + }, + .m_custom_sampler_constraints = {}, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, + .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, + }, + .m_custom_sampler_constraints = {}, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, + .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + CONST_SRC_CODE_GRID_LIGHTING_SH_2, + }, + .m_custom_sampler_constraints = {}, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, + .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + }, + .m_custom_sampler_constraints = {}, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, + .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + CONST_SRC_CODE_GRID_LIGHTING_SH_2, + }, + .m_custom_sampler_constraints = { + CUSTOM_SAMPLER_REFLECTION_PROBE, + }, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, + .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + }, + .m_custom_sampler_constraints = { + CUSTOM_SAMPLER_REFLECTION_PROBE, + }, + }, + { + .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, + .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + CONST_SRC_CODE_GRID_LIGHTING_SH_0, + CONST_SRC_CODE_GRID_LIGHTING_SH_1, + CONST_SRC_CODE_GRID_LIGHTING_SH_2, + }, + .m_custom_sampler_constraints = { + CUSTOM_SAMPLER_REFLECTION_PROBE, + }, + }, + { + .m_value = VERTEX_SHADER_MODEL_UNLIT, + .m_material_type = std::nullopt, + .m_vertex_const_constraints = { + CONST_SRC_CODE_WORLD_MATRIX, + }, + .m_custom_sampler_constraints = {}, + }, + }; + // clang-format on + + [[nodiscard]] bool MappingMatches(const PrecompiledIndexMapping& mapping, + const MaterialType materialType, + const std::vector& passCodeConsts, + const unsigned samplerFlags) + { + if (mapping.m_material_type && *mapping.m_material_type != materialType) + return false; + if (mapping.m_vertex_const_constraints.size() > passCodeConsts.size()) + return false; + + auto passCodeConstIndex = 0u; + for (const auto codeConstConstraint : mapping.m_vertex_const_constraints) + { + if (passCodeConsts[passCodeConstIndex++] != codeConstConstraint) + return false; + } + + auto expectedSamplerFlags = 0u; + for (const auto customSampler : mapping.m_custom_sampler_constraints) + expectedSamplerFlags |= (1 << customSampler); + + return samplerFlags == expectedSamplerFlags; + } +} // namespace + +namespace T6 +{ + void ApplyPrecompiledIndex(MaterialPass& pass) + { + if (!pass.args) + return; + + std::vector codeConstants; + const auto argCount = static_cast(pass.perPrimArgCount); + for (auto argIndex = 0u; argIndex < argCount; argIndex++) + { + const auto& arg = pass.args[argIndex]; + if (arg.type != MTL_ARG_CODE_VERTEX_CONST) + continue; + + const auto codeConst = static_cast(arg.u.codeConst.index); + + // Make sure we are agnostic of the used transposed versions + const auto codeConstInfo = commonCodeSourceInfos.GetInfoForCodeConstSource(codeConst); + if (codeConstInfo && codeConstInfo->transposedMatrix) + codeConstants.emplace_back(std::min(codeConst, *codeConstInfo->transposedMatrix)); + else + codeConstants.emplace_back(codeConst); + } + + const auto end = std::end(precompiledIndexMappings); + const auto foundMapping = std::find_if(std::begin(precompiledIndexMappings), + end, + [&codeConstants, pass](const PrecompiledIndexMapping& mapping) -> bool + { + return MappingMatches(mapping, pass.materialType, codeConstants, pass.customSamplerFlags); + }); + + if (foundMapping != end) + pass.precompiledIndex = foundMapping->m_value; + } +} // namespace T6 diff --git a/src/ObjCompiling/Game/T6/Techset/PrecompiledIndexT6.h b/src/ObjCompiling/Game/T6/Techset/PrecompiledIndexT6.h new file mode 100644 index 00000000..1f192906 --- /dev/null +++ b/src/ObjCompiling/Game/T6/Techset/PrecompiledIndexT6.h @@ -0,0 +1,8 @@ +#pragma once + +#include "Game/T6/T6.h" + +namespace T6 +{ + void ApplyPrecompiledIndex(MaterialPass& pass); +} diff --git a/src/ObjCompiling/Game/T6/Techset/TechniqueCompilerT6.cpp b/src/ObjCompiling/Game/T6/Techset/TechniqueCompilerT6.cpp index 44640a04..6b61a657 100644 --- a/src/ObjCompiling/Game/T6/Techset/TechniqueCompilerT6.cpp +++ b/src/ObjCompiling/Game/T6/Techset/TechniqueCompilerT6.cpp @@ -2,6 +2,7 @@ #include "Game/T6/T6.h" #include "Game/T6/Techset/TechsetConstantsT6.h" +#include "PrecompiledIndexT6.h" #include "Techset/CommonShaderArgCreator.h" #include "Techset/CommonTechniqueLoader.h" #include "Techset/LiteralConstsZoneState.h" @@ -16,210 +17,6 @@ using namespace T6; namespace { - class PrecompiledIndexMapping - { - public: - [[nodiscard]] bool - Matches(const MaterialType materialType, const std::vector& passCodeConsts, const unsigned samplerFlags) const - { - if (m_material_type && materialType != *m_material_type) - return false; - if (m_vertex_const_constraints.size() > passCodeConsts.size()) - return false; - - auto passCodeConstIndex = 0u; - for (const auto codeConstConstraint : m_vertex_const_constraints) - { - if (passCodeConsts[passCodeConstIndex++] != codeConstConstraint) - return false; - } - - auto expectedSamplerFlags = 0u; - for (const auto customSampler : m_custom_sampler_constraints) - expectedSamplerFlags |= (1 << customSampler); - - return samplerFlags == expectedSamplerFlags; - } - - VertexShaderPrecompiledIndex m_value; - std::optional m_material_type; - std::vector m_vertex_const_constraints; - std::vector m_custom_sampler_constraints; - }; - - // clang-format off - inline PrecompiledIndexMapping precompiledIndexMappings[]{ - { - .m_value = VERTEX_SHADER_MODEL_LIT, - .m_material_type = MTL_TYPE_MODEL_VERTCOL, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - CONST_SRC_CODE_GRID_LIGHTING_SH_2, - }, - .m_custom_sampler_constraints = {}, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT, - .m_material_type = MTL_TYPE_MODEL_VERTCOL, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, - }, - .m_custom_sampler_constraints = {}, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT, - .m_material_type = MTL_TYPE_MODEL_VERTCOL, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - CONST_SRC_CODE_GRID_LIGHTING_SH_2, - }, - .m_custom_sampler_constraints = {}, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT, - .m_material_type = MTL_TYPE_MODEL_VERTCOL, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - }, - .m_custom_sampler_constraints = {}, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT, - .m_material_type = MTL_TYPE_MODEL_VERTCOL, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - CONST_SRC_CODE_GRID_LIGHTING_SH_2, - }, - .m_custom_sampler_constraints = { - CUSTOM_SAMPLER_REFLECTION_PROBE, - }, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT, - .m_material_type = MTL_TYPE_MODEL_VERTCOL, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - }, - .m_custom_sampler_constraints = { - CUSTOM_SAMPLER_REFLECTION_PROBE, - }, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT, - .m_material_type = MTL_TYPE_MODEL_VERTCOL, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - CONST_SRC_CODE_GRID_LIGHTING_SH_2, - }, - .m_custom_sampler_constraints = { - CUSTOM_SAMPLER_REFLECTION_PROBE, - }, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, - .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - CONST_SRC_CODE_GRID_LIGHTING_SH_2, - }, - .m_custom_sampler_constraints = {}, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, - .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, - }, - .m_custom_sampler_constraints = {}, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, - .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - CONST_SRC_CODE_GRID_LIGHTING_SH_2, - }, - .m_custom_sampler_constraints = {}, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, - .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - }, - .m_custom_sampler_constraints = {}, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, - .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_COORDS_AND_VIS, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - CONST_SRC_CODE_GRID_LIGHTING_SH_2, - }, - .m_custom_sampler_constraints = { - CUSTOM_SAMPLER_REFLECTION_PROBE, - }, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, - .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - }, - .m_custom_sampler_constraints = { - CUSTOM_SAMPLER_REFLECTION_PROBE, - }, - }, - { - .m_value = VERTEX_SHADER_MODEL_LIT_LIGHTMAP_VC, - .m_material_type = MTL_TYPE_MODEL_LIGHTMAP_VC, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - CONST_SRC_CODE_GRID_LIGHTING_SH_0, - CONST_SRC_CODE_GRID_LIGHTING_SH_1, - CONST_SRC_CODE_GRID_LIGHTING_SH_2, - }, - .m_custom_sampler_constraints = { - CUSTOM_SAMPLER_REFLECTION_PROBE, - }, - }, - { - .m_value = VERTEX_SHADER_MODEL_UNLIT, - .m_material_type = std::nullopt, - .m_vertex_const_constraints = { - CONST_SRC_CODE_WORLD_MATRIX, - }, - .m_custom_sampler_constraints = {}, - }, - }; - // clang-format on - unsigned ConvertArgumentType(const techset::CommonShaderArgumentType& type) { if (type.m_shader_type == techset::CommonTechniqueShaderType::VERTEX) @@ -439,41 +236,6 @@ namespace } } - void ApplyPrecompiledIndex(MaterialPass& pass) - { - if (!pass.args) - return; - - std::vector codeConstants; - const auto argCount = static_cast(pass.perPrimArgCount); - for (auto argIndex = 0u; argIndex < argCount; argIndex++) - { - const auto& arg = pass.args[argIndex]; - if (arg.type != MTL_ARG_CODE_VERTEX_CONST) - continue; - - const auto codeConst = static_cast(arg.u.codeConst.index); - - // Make sure we are agnostic of the used transposed versions - const auto codeConstInfo = commonCodeSourceInfos.GetInfoForCodeConstSource(codeConst); - if (codeConstInfo && codeConstInfo->transposedMatrix) - codeConstants.emplace_back(std::min(codeConst, *codeConstInfo->transposedMatrix)); - else - codeConstants.emplace_back(codeConst); - } - - const auto end = std::end(precompiledIndexMappings); - const auto foundMapping = std::find_if(std::begin(precompiledIndexMappings), - end, - [&codeConstants, pass](const PrecompiledIndexMapping& mapping) -> bool - { - return mapping.Matches(pass.materialType, codeConstants, pass.customSamplerFlags); - }); - - if (foundMapping != end) - pass.precompiledIndex = foundMapping->m_value; - } - class TechniqueShaderLoaderT6 final : public techset::ITechniqueShaderLoader { public: