Skip to content

[HLSL] Matrix constructors is using the initalizer index when it needs to convert the index to a column major index #165663

@farzonl

Description

@farzonl

Today Matrix initalization is just taking in the index from the initalizer list:

while (NumEltsInit < MaxElts && Index < IList->getNumInits()) {
// Not a sublist: just consume directly.
ElemEnt.setElementIndex(Index);
CheckSubElementType(ElemEnt, IList, ElemTy, Index, StructuredList,
StructuredIndex);
++NumEltsInit;
}

This means we aren't reordering the index to account for layout DXC is expecting.

See https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math#matrix-ordering

The fix seems to be

@@ -1906,10 +1906,12 @@ void InitListChecker::CheckMatrixType(const InitializedEntity &Entity,
 
   while (NumEltsInit < MaxElts && Index < IList->getNumInits()) {
     // Not a sublist: just consume directly.
-    ElemEnt.setElementIndex(Index);
-    CheckSubElementType(ElemEnt, IList, ElemTy, Index, StructuredList,
+    unsigned ColMajorIndex = (Index % MT->getNumRows()) * MT->getNumColumns() + (Index / MT->getNumRows());
+    ElemEnt.setElementIndex(ColMajorIndex);
+    CheckSubElementType(ElemEnt, IList, ElemTy, ColMajorIndex, StructuredList,
                         StructuredIndex);
     ++NumEltsInit;
+    ++Index;
   }

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Support

Type

No type

Projects

Status

Active

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions