GLSL/SPV: Fix #1310: don't create unnecessary integer matrices.
authorJohn Kessenich <cepheus@frii.com>
Sun, 8 Apr 2018 00:49:54 +0000 (18:49 -0600)
committerJohn Kessenich <cepheus@frii.com>
Sun, 8 Apr 2018 00:49:54 +0000 (18:49 -0600)
Test/baseResults/spv.vecMatConstruct.frag.out [new file with mode: 0755]
Test/spv.vecMatConstruct.frag [new file with mode: 0644]
glslang/MachineIndependent/ParseHelper.cpp
gtests/Spv.FromFile.cpp

diff --git a/Test/baseResults/spv.vecMatConstruct.frag.out b/Test/baseResults/spv.vecMatConstruct.frag.out
new file mode 100755 (executable)
index 0000000..d7e892d
--- /dev/null
@@ -0,0 +1,87 @@
+spv.vecMatConstruct.frag
+// Module Version 10000
+// Generated by (magic number): 80006
+// Id's are bound by 62
+
+                              Capability Shader
+               1:             ExtInstImport  "GLSL.std.450"
+                              MemoryModel Logical GLSL450
+                              EntryPoint Fragment 4  "main"
+                              ExecutionMode 4 OriginUpperLeft
+                              Source GLSL 450
+                              Name 4  "main"
+                              Name 9  "v2"
+                              Name 13  "m"
+                              Name 19  "v3"
+                              Name 27  "v4"
+                              Name 37  "iv2"
+                              Name 45  "iv3"
+                              Name 54  "iv4"
+               2:             TypeVoid
+               3:             TypeFunction 2
+               6:             TypeFloat 32
+               7:             TypeVector 6(float) 2
+               8:             TypePointer Function 7(fvec2)
+              10:             TypeVector 6(float) 3
+              11:             TypeMatrix 10(fvec3) 4
+              12:             TypePointer Function 11
+              18:             TypePointer Function 10(fvec3)
+              25:             TypeVector 6(float) 4
+              26:             TypePointer Function 25(fvec4)
+              34:             TypeInt 32 1
+              35:             TypeVector 34(int) 2
+              36:             TypePointer Function 35(ivec2)
+              43:             TypeVector 34(int) 3
+              44:             TypePointer Function 43(ivec3)
+              52:             TypeVector 34(int) 4
+              53:             TypePointer Function 52(ivec4)
+         4(main):           2 Function None 3
+               5:             Label
+           9(v2):      8(ptr) Variable Function
+           13(m):     12(ptr) Variable Function
+          19(v3):     18(ptr) Variable Function
+          27(v4):     26(ptr) Variable Function
+         37(iv2):     36(ptr) Variable Function
+         45(iv3):     44(ptr) Variable Function
+         54(iv4):     53(ptr) Variable Function
+              14:          11 Load 13(m)
+              15:    6(float) CompositeExtract 14 0 0
+              16:    6(float) CompositeExtract 14 0 1
+              17:    7(fvec2) CompositeConstruct 15 16
+                              Store 9(v2) 17
+              20:          11 Load 13(m)
+              21:    6(float) CompositeExtract 20 0 0
+              22:    6(float) CompositeExtract 20 0 1
+              23:    6(float) CompositeExtract 20 0 2
+              24:   10(fvec3) CompositeConstruct 21 22 23
+                              Store 19(v3) 24
+              28:          11 Load 13(m)
+              29:    6(float) CompositeExtract 28 0 0
+              30:    6(float) CompositeExtract 28 0 1
+              31:    6(float) CompositeExtract 28 0 2
+              32:    6(float) CompositeExtract 28 1 0
+              33:   25(fvec4) CompositeConstruct 29 30 31 32
+                              Store 27(v4) 33
+              38:          11 Load 13(m)
+              39:    6(float) CompositeExtract 38 0 0
+              40:    6(float) CompositeExtract 38 0 1
+              41:    7(fvec2) CompositeConstruct 39 40
+              42:   35(ivec2) ConvertFToS 41
+                              Store 37(iv2) 42
+              46:          11 Load 13(m)
+              47:    6(float) CompositeExtract 46 0 0
+              48:    6(float) CompositeExtract 46 0 1
+              49:    6(float) CompositeExtract 46 0 2
+              50:   10(fvec3) CompositeConstruct 47 48 49
+              51:   43(ivec3) ConvertFToS 50
+                              Store 45(iv3) 51
+              55:          11 Load 13(m)
+              56:    6(float) CompositeExtract 55 0 0
+              57:    6(float) CompositeExtract 55 0 1
+              58:    6(float) CompositeExtract 55 0 2
+              59:    6(float) CompositeExtract 55 1 0
+              60:   25(fvec4) CompositeConstruct 56 57 58 59
+              61:   52(ivec4) ConvertFToS 60
+                              Store 54(iv4) 61
+                              Return
+                              FunctionEnd
diff --git a/Test/spv.vecMatConstruct.frag b/Test/spv.vecMatConstruct.frag
new file mode 100644 (file)
index 0000000..54a0017
--- /dev/null
@@ -0,0 +1,14 @@
+#version 450\r
+\r
+void main()\r
+{\r
+    mat4x3 m;\r
+\r
+    vec2 v2 = vec2(m);\r
+    vec3 v3 = vec3(m);\r
+    vec4 v4 = vec4(m);\r
+\r
+    ivec2 iv2 = ivec2(m);\r
+    ivec3 iv3 = ivec3(m);\r
+    ivec4 iv4 = ivec4(m);\r
+}\r
index 2f5b449e00720a6d81a8bde9d271a84129644e76..f19280d975d14e84d0d16d1b641ccfade5dda93d 100644 (file)
@@ -5771,8 +5771,22 @@ TIntermTyped* TParseContext::addConstructor(const TSourceLoc& loc, TIntermNode*
 //
 // Returns nullptr for an error or the constructed node.
 //
-TIntermTyped* TParseContext::constructBuiltIn(const TType& type, TOperator op, TIntermTyped* node, const TSourceLoc& loc, bool subset)
-{
+TIntermTyped* TParseContext::constructBuiltIn(const TType& type, TOperator op, TIntermTyped* node, const TSourceLoc& loc,
+    bool subset)
+{
+    // If we are changing a matrix in both domain of basic type and to a non matrix,
+    // do the shape change first (by default, below, basic type is changed before shape).
+    // This avoids requesting a matrix of a new type that is going to be discarded anyway.
+    // TODO: This could be generalized to more type combinations, but that would require
+    // more extensive testing and full algorithm rework. For now, the need to do two changes makes
+    // the recursive call work, and avoids the most aggregious case of creating integer matrices.
+    if (node->getType().isMatrix() && (type.isScalar() || type.isVector()) &&
+            type.isFloatingDomain() != node->getType().isFloatingDomain()) {
+        TType transitionType(node->getBasicType(), glslang::EvqTemporary, type.getVectorSize(), 0, 0, node->isVector());
+        TOperator transitionOp = intermediate.mapTypeToConstructorOp(transitionType);
+        node = constructBuiltIn(transitionType, transitionOp, node, loc, false);
+    }
+
     TIntermTyped* newNode;
     TOperator basicOp;
 
index c2b22d4afcfc471d20cc5cf659af32b3d6fbe51e..4b1ff462cc782c209f0cc5e7dbf9b3c1a0e3f392 100755 (executable)
@@ -322,6 +322,7 @@ INSTANTIATE_TEST_CASE_P(
         "spv.variableArrayIndex.frag",
         "spv.varyingArray.frag",
         "spv.varyingArrayIndirect.frag",
+        "spv.vecMatConstruct.frag",
         "spv.voidFunction.frag",
         "spv.whileLoop.frag",
         "spv.AofA.frag",