From a88055c6f5190ca62bb4aa853b4f0fa11546278f Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 5 Sep 2024 10:26:59 -0700 Subject: Respect matrix layout in uniform and in/out parameters for HLSL target. (#5013) * Respect matrix layout in uniform and in/out parameters for HLSL target. * Update test. * Fix test. * fix test. * Fix metal layout calculation. * Fix compile error. * Fix compiler error. --------- Co-authored-by: Yong He --- source/slang/slang-emit-hlsl.cpp | 71 ++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 25 deletions(-) (limited to 'source/slang/slang-emit-hlsl.cpp') diff --git a/source/slang/slang-emit-hlsl.cpp b/source/slang/slang-emit-hlsl.cpp index a867d4c06..f9765a555 100644 --- a/source/slang/slang-emit-hlsl.cpp +++ b/source/slang/slang-emit-hlsl.cpp @@ -959,15 +959,37 @@ void HLSLSourceEmitter::emitSimpleTypeImpl(IRType* type) case kIROp_MatrixType: { auto matType = (IRMatrixType*)type; - - // TODO(tfoley): should really emit these with sugar - m_writer->emit("matrix<"); - emitType(matType->getElementType()); - m_writer->emit(","); - emitVal(matType->getRowCount(), getInfo(EmitOp::General)); - m_writer->emit(","); - emitVal(matType->getColumnCount(), getInfo(EmitOp::General)); - m_writer->emit("> "); + bool canUseSugar = true; + switch (matType->getElementType()->getOp()) + { + case kIROp_IntType: + case kIROp_UIntType: + case kIROp_FloatType: + canUseSugar = true; + break; + default: + canUseSugar = false; + break; + } + if (!as(matType->getRowCount()) || !as(matType->getColumnCount())) + canUseSugar = false; + if (canUseSugar) + { + emitType(matType->getElementType()); + m_writer->emitInt64(getIntVal(matType->getRowCount())); + m_writer->emit("x"); + m_writer->emitInt64(getIntVal(matType->getColumnCount())); + } + else + { + m_writer->emit("matrix<"); + emitType(matType->getElementType()); + m_writer->emit(","); + emitVal(matType->getRowCount(), getInfo(EmitOp::General)); + m_writer->emit(","); + emitVal(matType->getColumnCount(), getInfo(EmitOp::General)); + m_writer->emit("> "); + } return; } case kIROp_SamplerStateType: @@ -1306,25 +1328,24 @@ void HLSLSourceEmitter::emitVarDecorationsImpl(IRInst* varDecl) } } -void HLSLSourceEmitter::emitMatrixLayoutModifiersImpl(IRVarLayout* layout) +void HLSLSourceEmitter::emitMatrixLayoutModifiersImpl(IRType* type) { - // When a variable has a matrix type, we want to emit an explicit - // layout qualifier based on what the layout has been computed to be. - // - - auto typeLayout = layout->getTypeLayout()->unwrapArray(); - - if (auto matrixTypeLayout = as(typeLayout)) + auto matType = as(type); + if (!matType) + return; + auto matrixLayout = getIntVal(matType->getLayout()); + if (getTargetProgram()->getOptionSet().getMatrixLayoutMode() != (MatrixLayoutMode)matrixLayout) { - switch (matrixTypeLayout->getMode()) + switch (matrixLayout) { - case kMatrixLayoutMode_ColumnMajor: - m_writer->emit("column_major "); - break; - - case kMatrixLayoutMode_RowMajor: - m_writer->emit("row_major "); - break; + case SLANG_MATRIX_LAYOUT_COLUMN_MAJOR: + m_writer->emit("column_major "); + break; + case SLANG_MATRIX_LAYOUT_ROW_MAJOR: + m_writer->emit("row_major "); + break; + default: + break; } } } -- cgit v1.2.3