summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2024-07-10 14:10:33 -0700
committerGitHub <noreply@github.com>2024-07-10 14:10:33 -0700
commit82f308ca692878bfe9844b86629c6536b4cd0f0a (patch)
tree001eb3d90ea98e945c73c2659bc9415d254da366 /source
parentb89421cb3b803165455020f5b70d582b6aec6e76 (diff)
Fix the invalid spirv generation for matrix cast (#4588)
Spirv doesn't have instruction to do the float cast for the matrix type. So we have to convert the matrix row by row, and then construct them to a new matrix. Update the unit test to make sure the cast won't miss any elements. Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source')
-rw-r--r--source/slang/slang-emit-spirv.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp
index 8a0b9f2ed..8c7232963 100644
--- a/source/slang/slang-emit-spirv.cpp
+++ b/source/slang/slang-emit-spirv.cpp
@@ -5190,6 +5190,32 @@ struct SPIRVEmitContext
SLANG_UNREACHABLE(__func__);
}
+ SpvInst* emitFloatCastForMatrix(SpvInstParent* parent, IRFloatCast* inst, IRMatrixType* fromTypeM, IRMatrixType* toTypeM)
+ {
+ // Because there is no spirv instruction to convert matrix to matrix, we need to convert it row by row.
+ auto rowCount = getIntVal(fromTypeM->getRowCount());
+ auto colCount = getIntVal(fromTypeM->getColumnCount());
+
+ IRBuilder builder(m_irModule);
+ // Get from and to type of the row vector
+ auto fromTypeV = builder.getVectorType(fromTypeM->getElementType(), colCount);
+ auto toVectorV = builder.getVectorType(toTypeM->getElementType(), colCount);
+
+ List<SpvInst*> rowVectorsConverted;
+ // convert each row vector to toType.
+ for (uint32_t i = 0; i < rowCount; i++)
+ {
+ auto rowVector = emitOpCompositeExtract(parent, nullptr, fromTypeV,
+ inst->getOperand(0), makeArray(SpvLiteralInteger::from32(i)));
+
+ auto rowVectorConverted = emitOpFConvert(parent, nullptr, toVectorV, rowVector);
+ rowVectorsConverted.add(rowVectorConverted);
+ }
+
+ // construct a matrix from the converted row vectors.
+ return emitCompositeConstruct(parent, inst, toTypeM, rowVectorsConverted);
+ }
+
SpvInst* emitFloatCast(SpvInstParent* parent, IRFloatCast* inst)
{
const auto fromTypeV = inst->getOperand(0)->getDataType();
@@ -5198,6 +5224,7 @@ struct SPIRVEmitContext
IRType* fromType = nullptr;
IRType* toType = nullptr;
+ bool isMatrixCast = false;
if (as<IRVectorType>(fromTypeV) || as<IRVectorType>(toTypeV))
{
fromType = getVectorElementType(fromTypeV);
@@ -5207,6 +5234,7 @@ struct SPIRVEmitContext
{
fromType = getMatrixElementType(fromTypeV);
toType = getMatrixElementType(toTypeV);
+ isMatrixCast = true;
}
else
{
@@ -5228,6 +5256,11 @@ struct SPIRVEmitContext
SLANG_ASSERT(isFloatingType(toType));
SLANG_ASSERT(!isTypeEqual(fromType, toType));
+ if (isMatrixCast)
+ {
+ return emitFloatCastForMatrix(parent, inst, as<IRMatrixType>(fromTypeV), as<IRMatrixType>(toTypeV));
+ }
+
return emitOpFConvert(parent, inst, toTypeV, inst->getOperand(0));
}