From 629564073c4ac764c79c44e525b42fd446413f3f Mon Sep 17 00:00:00 2001 From: Jan Laupetin Date: Sat, 14 Mar 2026 10:50:12 +0100 Subject: [PATCH] fix: auto creating dx9 shader args with too many elements --- .../Techset/CommonShaderArgCreator.cpp | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/ObjCompiling/Techset/CommonShaderArgCreator.cpp b/src/ObjCompiling/Techset/CommonShaderArgCreator.cpp index e2531448..43045cd7 100644 --- a/src/ObjCompiling/Techset/CommonShaderArgCreator.cpp +++ b/src/ObjCompiling/Techset/CommonShaderArgCreator.cpp @@ -2,6 +2,7 @@ #include "Shader/D3D11ShaderAnalyser.h" #include "Shader/D3D9ShaderAnalyser.h" +#include "Utils/Alignment.h" #include "Utils/Djb2.h" #include "Utils/Logging/Log.h" @@ -422,19 +423,20 @@ namespace if (!constInfo) return result::Unexpected(std::format("Missing info for code const {}", shaderArg.m_name)); - const auto variableElementCount = std::max(shaderArg.m_type_elements, 1); + const auto elementSize = ElementSizeForArg(shaderArg); + const auto elementCount = utils::Align(shaderArg.m_register_count, elementSize) / elementSize; const auto infoArrayCount = std::max(constInfo->arrayCount, 1); - if (variableElementCount > infoArrayCount) + if (elementCount > infoArrayCount) { return result::Unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})", shaderArg.m_name, - variableElementCount, + elementCount, infoArrayCount)); } techset::CommonShaderArgDestination commonDestination; const auto isTransposed = shaderArg.m_class == d3d9::ParameterClass::MATRIX_COLUMNS; - for (auto elementIndex = 0u; elementIndex < variableElementCount; elementIndex++) + for (auto elementIndex = 0u; elementIndex < elementCount; elementIndex++) { commonDestination.dx9.m_destination_register = shaderArg.m_register_index + elementIndex; auto result = AcceptShaderConstantArgument(commonDestination, isTransposed, shaderArg.m_register_count, *maybeCodeConst, elementIndex); @@ -448,6 +450,18 @@ namespace return NoResult{}; } + [[nodiscard]] static unsigned ElementSizeForArg(const d3d9::ShaderConstant& arg) + { + switch (arg.m_class) + { + case d3d9::ParameterClass::MATRIX_COLUMNS: + case d3d9::ParameterClass::MATRIX_ROWS: + return 4; + default: + return 1; + } + } + result::Expected AutoCreateSamplerArg(const d3d9::ShaderConstant& shaderArg) { const auto maybeCodeSampler = m_common_code_source_infos.GetCodeSamplerSourceForAccessor(shaderArg.m_name);