2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-03-05 04:23:02 +00:00

chore: add logic for computing precompiled index on material pass

This commit is contained in:
Jan Laupetin
2026-03-02 22:07:55 +00:00
parent 925e51cb10
commit 1611c222cc
10 changed files with 291 additions and 12 deletions

View File

@@ -3001,7 +3001,7 @@ namespace T6
VERTEX_SHADER_MODEL_UNLIT, VERTEX_SHADER_MODEL_UNLIT,
}; };
enum CustomSamplers enum CustomSampler
{ {
CUSTOM_SAMPLER_REFLECTION_PROBE = 0, CUSTOM_SAMPLER_REFLECTION_PROBE = 0,
CUSTOM_SAMPLER_LIGHTMAP_SECONDARY, CUSTOM_SAMPLER_LIGHTMAP_SECONDARY,
@@ -6191,7 +6191,7 @@ namespace T6
unsigned int nameHash; unsigned int nameHash;
}; };
enum MaterialShaderArgumentType enum MaterialShaderArgumentType : uint16_t
{ {
MTL_ARG_MATERIAL_VERTEX_CONST = 0x0, MTL_ARG_MATERIAL_VERTEX_CONST = 0x0,
MTL_ARG_LITERAL_VERTEX_CONST = 0x1, MTL_ARG_LITERAL_VERTEX_CONST = 0x1,
@@ -6213,7 +6213,7 @@ namespace T6
struct MaterialShaderArgument struct MaterialShaderArgument
{ {
uint16_t type; MaterialShaderArgumentType type;
MaterialArgumentLocation location; MaterialArgumentLocation location;
uint16_t size; uint16_t size;
uint16_t buffer; uint16_t buffer;

View File

@@ -280,12 +280,14 @@ namespace techset
std::string stateMap, std::string stateMap,
CommonTechniqueShader vertexShader, CommonTechniqueShader vertexShader,
CommonTechniqueShader pixelShader, CommonTechniqueShader pixelShader,
CommonVertexDeclaration vertexDeclaration) CommonVertexDeclaration vertexDeclaration,
std::string comment)
: m_sampler_flags(samplerFlags), : m_sampler_flags(samplerFlags),
m_state_map(std::move(stateMap)), m_state_map(std::move(stateMap)),
m_vertex_shader(std::move(vertexShader)), m_vertex_shader(std::move(vertexShader)),
m_pixel_shader(std::move(pixelShader)), m_pixel_shader(std::move(pixelShader)),
m_vertex_declaration(std::move(vertexDeclaration)) m_vertex_declaration(std::move(vertexDeclaration)),
m_comment(std::move(comment))
{ {
} }

View File

@@ -283,7 +283,8 @@ namespace techset
std::string stateMap, std::string stateMap,
CommonTechniqueShader vertexShader, CommonTechniqueShader vertexShader,
CommonTechniqueShader pixelShader, CommonTechniqueShader pixelShader,
CommonVertexDeclaration vertexDeclaration); CommonVertexDeclaration vertexDeclaration,
std::string comment);
[[nodiscard]] FrequencyCounts_t GetFrequencyCounts(const CommonCodeSourceInfos& infos) const; [[nodiscard]] FrequencyCounts_t GetFrequencyCounts(const CommonCodeSourceInfos& infos) const;
@@ -292,6 +293,7 @@ namespace techset
CommonTechniqueShader m_vertex_shader; CommonTechniqueShader m_vertex_shader;
CommonTechniqueShader m_pixel_shader; CommonTechniqueShader m_pixel_shader;
CommonVertexDeclaration m_vertex_declaration; CommonVertexDeclaration m_vertex_declaration;
std::string m_comment;
std::vector<CommonShaderArg> m_args; std::vector<CommonShaderArg> m_args;
}; };

View File

@@ -8,12 +8,218 @@
#include "Utils/StringUtils.h" #include "Utils/StringUtils.h"
#include <cassert> #include <cassert>
#include <optional>
#include <sstream> #include <sstream>
#include <vector>
using namespace T6; using namespace T6;
namespace namespace
{ {
class PrecompiledIndexMapping
{
public:
[[nodiscard]] bool
Matches(const MaterialType materialType, const std::vector<techset::CommonStreamSource>& 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<MaterialType> m_material_type;
std::vector<MaterialConstantSource> m_vertex_const_constraints;
std::vector<CustomSampler> 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) unsigned ConvertArgumentType(const techset::CommonShaderArgumentType& type)
{ {
if (type.m_shader_type == techset::CommonTechniqueShaderType::VERTEX) if (type.m_shader_type == techset::CommonTechniqueShaderType::VERTEX)
@@ -233,6 +439,41 @@ namespace
} }
} }
void ApplyPrecompiledIndex(MaterialPass& pass)
{
if (!pass.args)
return;
std::vector<techset::CommonStreamSource> codeConstants;
const auto argCount = static_cast<unsigned>(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<techset::CommonStreamSource>(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 class TechniqueShaderLoaderT6 final : public techset::ITechniqueShaderLoader
{ {
public: public:
@@ -314,6 +555,16 @@ namespace
{ {
ApplyTechFlagsFromMaterial(*materialAsset->Asset(), m_zone); 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: private:

View File

@@ -3,6 +3,7 @@
#include "Asset/IZoneAssetCreationState.h" #include "Asset/IZoneAssetCreationState.h"
#include "AssetRegistration.h" #include "AssetRegistration.h"
#include "Game/IAsset.h" #include "Game/IAsset.h"
#include "Pool/AssetPool.h"
#include "Pool/XAssetInfo.h" #include "Pool/XAssetInfo.h"
#include "Zone/AssetList/AssetList.h" #include "Zone/AssetList/AssetList.h"
#include "Zone/ZoneTypes.h" #include "Zone/ZoneTypes.h"
@@ -89,6 +90,11 @@ public:
XAssetInfoGeneric* ForceLoadDependencyGeneric(asset_type_t assetType, const std::string& assetName); XAssetInfoGeneric* ForceLoadDependencyGeneric(asset_type_t assetType, const std::string& assetName);
template<SubAssetDefinition SubAsset_t> [[nodiscard]] AssetPoolIterators<SubAsset_t> PoolSubAssets() const
{
return AssetPoolIterators<SubAsset_t>(*m_sub_asset_pools[SubAsset_t::EnumEntry]);
}
private: private:
[[nodiscard]] XAssetInfoGeneric* LoadDefaultAssetDependency(asset_type_t assetType, const std::string& assetName); [[nodiscard]] XAssetInfoGeneric* LoadDefaultAssetDependency(asset_type_t assetType, const std::string& assetName);

View File

@@ -193,7 +193,8 @@ namespace
"passthrough", "passthrough",
ConvertToCommonShader(pass.vertexShader), ConvertToCommonShader(pass.vertexShader),
ConvertToCommonShader(pass.pixelShader), ConvertToCommonShader(pass.pixelShader),
ConvertToCommonVertexDeclaration(pass.vertexDecl)); ConvertToCommonVertexDeclaration(pass.vertexDecl),
std::string());
if (pass.args) if (pass.args)
{ {

View File

@@ -232,19 +232,28 @@ namespace
return result; return result;
} }
techset::CommonTechnique ConvertToCommonTechnique(const MaterialTechnique& technique) techset::CommonTechnique ConvertToCommonTechnique(const MaterialTechnique& technique, const bool debug)
{ {
techset::CommonTechnique commonTechnique(technique.name ? technique.name : std::string(), technique.flags); techset::CommonTechnique commonTechnique(technique.name ? technique.name : std::string(), technique.flags);
for (auto passIndex = 0u; passIndex < technique.passCount; passIndex++) for (auto passIndex = 0u; passIndex < technique.passCount; passIndex++)
{ {
const auto& pass = technique.passArray[passIndex]; const auto& pass = technique.passArray[passIndex];
std::string comment;
if (debug)
{
comment = std::format(
"MaterialType: {}; PrecompiledIndex: {}", static_cast<unsigned>(pass.materialType), static_cast<unsigned>(pass.precompiledIndex));
}
techset::CommonPass commonPass(pass.customSamplerFlags, techset::CommonPass commonPass(pass.customSamplerFlags,
// No clue what the actual state map was // No clue what the actual state map was
"passthrough", "passthrough",
ConvertToCommonShader(pass.vertexShader), ConvertToCommonShader(pass.vertexShader),
ConvertToCommonShader(pass.pixelShader), ConvertToCommonShader(pass.pixelShader),
ConvertToCommonVertexDeclaration(pass.vertexDecl)); ConvertToCommonVertexDeclaration(pass.vertexDecl),
std::move(comment));
if (pass.args) if (pass.args)
{ {
@@ -268,7 +277,7 @@ namespace
{ {
if (technique && techniqueState->ShouldDumpTechnique(technique)) if (technique && techniqueState->ShouldDumpTechnique(technique))
{ {
const auto commonTechnique = ConvertToCommonTechnique(*technique); const auto commonTechnique = ConvertToCommonTechnique(*technique, debug);
techset::DumpCommonTechnique( techset::DumpCommonTechnique(
context, commonTechnique, techset::DxVersion::DX11, commonCodeSourceInfos, commonRoutingInfos, *materialConstantState, debug); context, commonTechnique, techset::DxVersion::DX11, commonCodeSourceInfos, commonRoutingInfos, *materialConstantState, debug);

View File

@@ -67,6 +67,12 @@ namespace
m_stream << std::format("// CUSTOM SAMPLER FLAGS: 0x{:x}\n", mask); m_stream << std::format("// CUSTOM SAMPLER FLAGS: 0x{:x}\n", mask);
} }
} }
if (!pass.m_comment.empty())
{
Indent();
m_stream << std::format("// {}\n", pass.m_comment);
}
} }
DumpStateMap(); DumpStateMap();

View File

@@ -67,7 +67,7 @@ private:
std::vector<std::unique_ptr<XAssetInfoGeneric>> m_assets; std::vector<std::unique_ptr<XAssetInfoGeneric>> m_assets;
}; };
template<AssetDefinition Asset_t> class AssetPoolIterator template<AssetOrSubAssetDefinition Asset_t> class AssetPoolIterator
{ {
public: public:
explicit AssetPoolIterator(AssetPool::Iterator i) explicit AssetPoolIterator(AssetPool::Iterator i)
@@ -94,7 +94,7 @@ private:
AssetPool::Iterator m_iterator; AssetPool::Iterator m_iterator;
}; };
template<AssetDefinition Asset_t> class AssetPoolIterators template<AssetOrSubAssetDefinition Asset_t> class AssetPoolIterators
{ {
public: public:
explicit AssetPoolIterators(AssetPool& assetPool) explicit AssetPoolIterators(AssetPool& assetPool)

View File

@@ -309,6 +309,7 @@ TEST_CASE("TechsetDumperT6", "[t6][techset][dumper]")
// TECHNIQUE FLAGS: 0x4 // TECHNIQUE FLAGS: 0x4
// TECHNIQUE FLAGS: 0x80 // TECHNIQUE FLAGS: 0x80
{ {
// MaterialType: 2; PrecompiledIndex: 3
stateMap "passthrough"; // TODO stateMap "passthrough"; // TODO
vertexShader 4.0 "simple.hlsl" vertexShader 4.0 "simple.hlsl"
@@ -339,6 +340,7 @@ TEST_CASE("TechsetDumperT6", "[t6][techset][dumper]")
{ {
// CUSTOM SAMPLER FLAGS: 0x1 // CUSTOM SAMPLER FLAGS: 0x1
// CUSTOM SAMPLER FLAGS: 0x2 // CUSTOM SAMPLER FLAGS: 0x2
// MaterialType: 0; PrecompiledIndex: 0
stateMap "passthrough"; // TODO stateMap "passthrough"; // TODO
vertexShader 4.0 "advanced.hlsl" vertexShader 4.0 "advanced.hlsl"