Add support for non-square matrices to the shader translator.

TRAC #23081

Signed-off-by: Geoff Lang
Signed-off-by: Nicolas Capens
Author: Jamie Madill

git-svn-id: https://angleproject.googlecode.com/svn/branches/es3proto@2394 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/compiler/parseConst.cpp b/src/compiler/parseConst.cpp
index d7b3498..77d996f 100644
--- a/src/compiler/parseConst.cpp
+++ b/src/compiler/parseConst.cpp
@@ -22,8 +22,9 @@
           infoSink(sink),
           symbolTable(symTable),
           size(0),
-          isMatrix(false),
-          matrixSize(0) {
+          isDiagonalMatrixInit(false),
+          matrixCols(0),
+          matrixRows(0) {
     }
 
     bool error;
@@ -46,8 +47,9 @@
     TInfoSink& infoSink;
     TSymbolTable& symbolTable;
     int size; // size of the constructor ( 4 for vec4)
-    bool isMatrix;
-    int matrixSize; // dimension of the matrix (nominal size and not the instance size)
+    bool isDiagonalMatrixInit;
+    int matrixCols; // columns of the matrix
+    int matrixRows; // rows of the matrix
 };
 
 //
@@ -118,8 +120,9 @@
         size = node->getType().getObjectSize();
 
         if (node->getType().isMatrix()) {
-            isMatrix = true;
-            matrixSize = node->getType().getRows();
+            isDiagonalMatrixInit = true;
+            matrixCols = node->getType().getCols();
+            matrixRows = node->getType().getRows();
         }
     }       
 
@@ -136,8 +139,9 @@
         singleConstantParam = false;   
         constructorType = EOpNull;
         size = 0;
-        isMatrix = false;
-        matrixSize = 0;
+        isDiagonalMatrixInit = false;
+        matrixCols = 0;
+        matrixRows = 0;
     }
     return false;
 }
@@ -178,7 +182,7 @@
     } else {
         int totalSize = index + size;
         ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
-        if (!isMatrix) {
+        if (!isDiagonalMatrixInit) {
             int count = 0;
             for (int i = index; i < totalSize; i++) {
                 if (i >= instanceSize)
@@ -191,21 +195,25 @@
                 if (node->getType().getObjectSize() > 1)
                     count++;
             }
-        } else {  // for matrix constructors
-            int count = 0;
-            int element = index;
-            for (int i = index; i < totalSize; i++) {
-                if (i >= instanceSize)
-                    return;
-                if (element - i == 0 || (i - element) % (matrixSize + 1) == 0 )
-                    leftUnionArray[i] = rightUnionArray[count];
-                else 
-                    leftUnionArray[i].setFConst(0.0f);
+        }
+        else
+        {
+            // for matrix diagonal constructors from a single scalar
+            for (int i = 0, col = 0; col < matrixCols; col++)
+            {
+                for (int row = 0; row < matrixRows; row++, i++)
+                {
+                    if (col == row)
+                    {
+                        leftUnionArray[i] = rightUnionArray[0];
+                    }
+                    else
+                    {
+                        leftUnionArray[i].setFConst(0.0f);
+                    }
 
-                (index)++;
-
-                if (node->getType().getObjectSize() > 1)
-                    count++;                
+                    (index)++;
+                }
             }
         }
     }