Adjust matrix addressing to account for RowMajor/ColMajor

Bug: b/128690261
Test: dEQP-VK.glsl.*
Test: dEQP-VK.ubo.*
Test: dEQP-VK.ssbo.*
Change-Id: Ibafc5f64263cd627a3a2cd961af226cb5b110ea0
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/28968
Tested-by: Chris Forbes <chrisforbes@google.com>
Presubmit-Ready: Chris Forbes <chrisforbes@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Pipeline/SpirvShader.cpp b/src/Pipeline/SpirvShader.cpp
index 31824c2..11b91ff 100644
--- a/src/Pipeline/SpirvShader.cpp
+++ b/src/Pipeline/SpirvShader.cpp
@@ -997,18 +997,25 @@
 			f(index++, offset);
 			break;
 		case spv::OpTypeVector:
+		{
+			auto elemStride = (d.InsideMatrix && d.HasRowMajor && d.RowMajor) ? d.MatrixStride / sizeof(float) : 1;
 			for (auto i = 0u; i < type.definition.word(3); i++)
 			{
-				VisitMemoryObjectInner(type.definition.word(2), d, index, offset + i, f);
+				VisitMemoryObjectInner(type.definition.word(2), d, index, offset + elemStride * i, f);
 			}
 			break;
+		}
 		case spv::OpTypeMatrix:
+		{
+			auto columnStride = (d.HasRowMajor && d.RowMajor) ? 1 : d.MatrixStride / sizeof(float);
+			d.InsideMatrix = true;
 			for (auto i = 0u; i < type.definition.word(3); i++)
 			{
 				ASSERT(d.HasMatrixStride);
-				VisitMemoryObjectInner(type.definition.word(2), d, index, offset + i * d.MatrixStride / sizeof(float), f);
+				VisitMemoryObjectInner(type.definition.word(2), d, index, offset + columnStride * i, f);
 			}
 			break;
+		}
 		case spv::OpTypeStruct:
 			for (auto i = 0u; i < type.definition.wordCount() - 2; i++)
 			{
@@ -1118,10 +1125,13 @@
 			}
 			case spv::OpTypeArray:
 			case spv::OpTypeRuntimeArray:
-			case spv::OpTypeMatrix:
 			case spv::OpTypeVector:
 				typeId = type.element;
 				break;
+			case spv::OpTypeMatrix:
+				typeId = type.element;
+				d->InsideMatrix = true;
+				break;
 			default:
 				UNIMPLEMENTED("Unexpected type '%s' in ApplyDecorationsForAccessChain",
 							  OpcodeName(type.definition.opcode()).c_str());
@@ -1190,21 +1200,24 @@
 			{
 				// TODO: b/127950082: Check bounds.
 				ASSERT(d.HasMatrixStride);
+				d.InsideMatrix = true;
+				auto columnStride = (d.HasRowMajor && d.RowMajor) ? 1 : d.MatrixStride/sizeof(float);
 				auto & obj = getObject(indexIds[i]);
 				if (obj.kind == Object::Kind::Constant)
-					constantOffset += d.MatrixStride/sizeof(float) * GetConstantInt(indexIds[i]);
+					constantOffset += columnStride * GetConstantInt(indexIds[i]);
 				else
-					ptr.offset += SIMD::Int(d.MatrixStride / sizeof(float)) * routine->getIntermediate(indexIds[i]).Int(0);
+					ptr.offset += SIMD::Int(columnStride) * routine->getIntermediate(indexIds[i]).Int(0);
 				typeId = type.element;
 				break;
 			}
 			case spv::OpTypeVector:
 			{
+				auto elemStride = (d.InsideMatrix && d.HasRowMajor && d.RowMajor) ? d.MatrixStride / sizeof(float) : 1;
 				auto & obj = getObject(indexIds[i]);
 				if (obj.kind == Object::Kind::Constant)
-					constantOffset += GetConstantInt(indexIds[i]);
+					constantOffset += elemStride * GetConstantInt(indexIds[i]);
 				else
-					ptr.offset += routine->getIntermediate(indexIds[i]).Int(0);
+					ptr.offset += SIMD::Int(elemStride) * routine->getIntermediate(indexIds[i]).Int(0);
 				typeId = type.element;
 				break;
 			}
@@ -1428,6 +1441,7 @@
 		Block |= src.Block;
 		BufferBlock |= src.BufferBlock;
 		RelaxedPrecision |= src.RelaxedPrecision;
+		InsideMatrix |= src.InsideMatrix;
 	}
 
 	void SpirvShader::DescriptorDecorations::Apply(const sw::SpirvShader::DescriptorDecorations &src)
diff --git a/src/Pipeline/SpirvShader.hpp b/src/Pipeline/SpirvShader.hpp
index b0c5da2..35120af 100644
--- a/src/Pipeline/SpirvShader.hpp
+++ b/src/Pipeline/SpirvShader.hpp
@@ -425,6 +425,7 @@
 			bool BufferBlock : 1;
 			bool RelaxedPrecision : 1;
 			bool RowMajor : 1;			// RowMajor if true; ColMajor if false
+			bool InsideMatrix : 1;		// pseudo-decoration for whether we're inside a matrix.
 
 			Decorations()
 					: Location{-1}, Component{0},
@@ -436,7 +437,8 @@
 					  HasRowMajor{false},
 					  Flat{false}, Centroid{false}, NoPerspective{false},
 					  Block{false}, BufferBlock{false},
-					  RelaxedPrecision{false}, RowMajor{false}
+					  RelaxedPrecision{false}, RowMajor{false},
+					  InsideMatrix{false}
 			{
 			}