SPV: Fix #723: construct vectors from matrices.
authorJohn Kessenich <cepheus@frii.com>
Sat, 18 Feb 2017 02:06:21 +0000 (19:06 -0700)
committerJohn Kessenich <cepheus@frii.com>
Sat, 18 Feb 2017 02:06:21 +0000 (19:06 -0700)
SPIRV/SpvBuilder.cpp
Test/baseResults/spv.matrix.frag.out
Test/spv.matrix.frag
glslang/Include/revision.h

index c0ca970ffbb175f5f0280efc68c3c2db68d4c481..3c23cffd553896f45f9956c1bb30862e26adbe5a 100644 (file)
@@ -1832,34 +1832,72 @@ Id Builder::createConstructor(Decoration precision, const std::vector<Id>& sourc
     if (sources.size() == 1 && isScalar(sources[0]) && numTargetComponents > 1)
         return smearScalar(precision, sources[0], resultTypeId);
 
+    // accumulate the arguments for OpCompositeConstruct
+    std::vector<Id> constituents;
     Id scalarTypeId = getScalarTypeId(resultTypeId);
-    std::vector<Id> constituents;  // accumulate the arguments for OpCompositeConstruct
-    for (unsigned int i = 0; i < sources.size(); ++i) {
-        assert(! isAggregate(sources[i]));
-        unsigned int sourceSize = getNumComponents(sources[i]);
+
+    // lambda to store the result of visiting an argument component
+    const auto latchResult = [&](Id comp) {
+        if (numTargetComponents > 1)
+            constituents.push_back(comp);
+        else
+            result = comp;
+        ++targetComponent;
+    };
+
+    // lambda to visit a vector argument's components
+    const auto accumulateVectorConstituents = [&](Id sourceArg) {
+        unsigned int sourceSize = getNumComponents(sourceArg);
         unsigned int sourcesToUse = sourceSize;
         if (sourcesToUse + targetComponent > numTargetComponents)
             sourcesToUse = numTargetComponents - targetComponent;
 
         for (unsigned int s = 0; s < sourcesToUse; ++s) {
-            Id arg = sources[i];
-            if (sourceSize > 1) {
-                std::vector<unsigned> swiz;
-                swiz.push_back(s);
-                arg = createRvalueSwizzle(precision, scalarTypeId, arg, swiz);
-            }
+            std::vector<unsigned> swiz;
+            swiz.push_back(s);
+            latchResult(createRvalueSwizzle(precision, scalarTypeId, sourceArg, swiz));
+        }
+    };
 
-            if (numTargetComponents > 1)
-                constituents.push_back(arg);
-            else
-                result = arg;
-            ++targetComponent;
+    // lambda to visit a matrix argument's components
+    const auto accumulateMatrixConstituents = [&](Id sourceArg) {
+        unsigned int sourceSize = getNumColumns(sourceArg) * getNumRows(sourceArg);
+        unsigned int sourcesToUse = sourceSize;
+        if (sourcesToUse + targetComponent > numTargetComponents)
+            sourcesToUse = numTargetComponents - targetComponent;
+
+        int col = 0;
+        int row = 0;
+        for (unsigned int s = 0; s < sourcesToUse; ++s) {
+            if (row >= getNumRows(sourceArg)) {
+                row = 0;
+                col++;
+            }
+            std::vector<Id> indexes;
+            indexes.push_back(col);
+            indexes.push_back(row);
+            latchResult(createCompositeExtract(sourceArg, scalarTypeId, indexes));
+            row++;
         }
+    };
+
+    // Go through the source arguments, each one could have either
+    // a single or multiple components to contribute.
+    for (unsigned int i = 0; i < sources.size(); ++i) {
+        if (isScalar(sources[i]))
+            latchResult(sources[i]);
+        else if (isVector(sources[i]))
+            accumulateVectorConstituents(sources[i]);
+        else if (isMatrix(sources[i]))
+            accumulateMatrixConstituents(sources[i]);
+        else
+            assert(0);
 
         if (targetComponent >= numTargetComponents)
             break;
     }
 
+    // If the result is a vector, make it from the gathered constituents.
     if (constituents.size() > 0)
         result = createCompositeConstruct(resultTypeId, constituents);
 
index 700e90ebcdc2e98425138d594d4edf8b1ba0900b..c7077b9a0665930297e0664ba8facaa43aa33861 100644 (file)
@@ -3,7 +3,7 @@ Warning, version 420 is not yet complete; most version-specific features are pre
 
 // Module Version 10000
 // Generated by (magic number): 80001
-// Id's are bound by 261
+// Id's are bound by 286
 
                               Capability Shader
                               Capability Float64
@@ -55,6 +55,9 @@ Warning, version 420 is not yet complete; most version-specific features are pre
              186:             TypePointer Output 7(fvec4)
       187(color):    186(ptr) Variable Output
              208:    6(float) Constant 0
+             270:             TypeVector 6(float) 2
+             271:             TypeMatrix 270(fvec2) 2
+             279:    6(float) Constant 1088841318
          4(main):           2 Function None 3
                5:             Label
        10(sum34):      9(ptr) Variable Function
@@ -305,5 +308,29 @@ Warning, version 420 is not yet complete; most version-specific features are pre
              259:    7(fvec4) Load 187(color)
              260:    7(fvec4) FAdd 259 258
                               Store 187(color) 260
+             261:         172 Load 174(m43)
+             262:    6(float) CompositeExtract 261 0 0
+             263:    6(float) CompositeExtract 261 0 1
+             264:    6(float) CompositeExtract 261 0 2
+             265:    6(float) CompositeExtract 261 1 0
+             266:    7(fvec4) CompositeConstruct 262 263 264 265
+             267:    7(fvec4) Load 187(color)
+             268:    7(fvec4) FAdd 267 266
+                              Store 187(color) 268
+             269:    6(float) Load 28(f)
+             272:  270(fvec2) CompositeConstruct 269 208
+             273:  270(fvec2) CompositeConstruct 208 269
+             274:         271 CompositeConstruct 272 273
+             275:    6(float) CompositeExtract 274 0 0
+             276:    6(float) CompositeExtract 274 0 1
+             277:    6(float) CompositeExtract 274 1 0
+             278:  157(fvec3) CompositeConstruct 275 276 277
+             280:    6(float) CompositeExtract 278 0
+             281:    6(float) CompositeExtract 278 1
+             282:    6(float) CompositeExtract 278 2
+             283:    7(fvec4) CompositeConstruct 280 281 282 279
+             284:    7(fvec4) Load 187(color)
+             285:    7(fvec4) FAdd 284 283
+                              Store 187(color) 285
                               Return
                               FunctionEnd
index 10a52566f9d2a01d7007af30ca1af5355f0289b4..3157173788ea6e8e41bb15c82b91f0c605aa3f5f 100644 (file)
@@ -43,4 +43,7 @@ void main()
     sum34 += mat3x4(v3, f, v3, f, v3, f);\r
 \r
     color += sum3 * m43 + sum4;\r
+\r
+    color += vec4(m43);\r
+    color += vec4(vec3(mat2(f)), 7.2);\r
 }\r
index dd060abc1b603237014c6ad2f69fa72c93d22fbf..0b08d22109e7224cbb8242f4a9565b9b7bfa48e4 100644 (file)
@@ -2,5 +2,5 @@
 // For the version, it uses the latest git tag followed by the number of commits.
 // For the date, it uses the current date (when then script is run).
 
-#define GLSLANG_REVISION "Overload400-PrecQual.1828"
-#define GLSLANG_DATE "13-Feb-2017"
+#define GLSLANG_REVISION "Overload400-PrecQual.1842"
+#define GLSLANG_DATE "17-Feb-2017"