Front-end: Implement compile-time constant folding for any() and all().
authorJohn Kessenich <cepheus@frii.com>
Tue, 12 Jul 2016 07:26:43 +0000 (01:26 -0600)
committerJohn Kessenich <cepheus@frii.com>
Tue, 12 Jul 2016 07:27:48 +0000 (01:27 -0600)
Test/baseResults/constFold.frag.out
Test/constFold.frag
glslang/MachineIndependent/Constant.cpp

index f5c3e9c..2e78468 100644 (file)
@@ -374,6 +374,18 @@ ERROR: node is still EOpNull!
 0:?       1 (const int)
 0:?       9.000000
 0:?       false (const bool)
+0:?     'cval1' (const bool)
+0:?       true (const bool)
+0:?     'cval2' (const bool)
+0:?       false (const bool)
+0:?     'cval3' (const bool)
+0:?       false (const bool)
+0:?     'cval4' (const bool)
+0:?       true (const bool)
+0:?     'cval5' (const bool)
+0:?       false (const bool)
+0:?     'cval6' (const bool)
+0:?       true (const bool)
 
 
 Linked fragment stage:
@@ -744,4 +756,16 @@ ERROR: node is still EOpNull!
 0:?       1 (const int)
 0:?       9.000000
 0:?       false (const bool)
+0:?     'cval1' (const bool)
+0:?       true (const bool)
+0:?     'cval2' (const bool)
+0:?       false (const bool)
+0:?     'cval3' (const bool)
+0:?       false (const bool)
+0:?     'cval4' (const bool)
+0:?       true (const bool)
+0:?     'cval5' (const bool)
+0:?       false (const bool)
+0:?     'cval6' (const bool)
+0:?       true (const bool)
 
index 97c3b2b..7306078 100644 (file)
@@ -138,4 +138,11 @@ const cag a0[3] = cag[3](cag(3, 2.0, true), cag(1, 5.0, true), cag(1, 9.0, false
 void foo4()\r
 {\r
     int a = int(a0[2].f);\r
-}
\ No newline at end of file
+}\r
+\r
+const bool cval1 = all(bvec4(true, true, true, true));\r
+const bool cval2 = all(bvec4(false, false, false, false));\r
+const bool cval3 = all(bvec4(true, true, false, true));\r
+const bool cval4 = any(bvec4(true, true, true, true));\r
+const bool cval5 = any(bvec4(false, false, false, false));\r
+const bool cval6 = any(bvec4(false, true, false, false));\r
index 03033bc..4adfd47 100644 (file)
@@ -399,6 +399,27 @@ TIntermTyped* TIntermConstantUnion::fold(TOperator op, const TType& returnType)
         break;
     }
 
+    case EOpAny:
+    {
+        bool result = false;
+        for (int i = 0; i < objectSize; i++) {
+            if (unionArray[i].getBConst())
+                result = true;
+        }
+        newConstArray[0].setBConst(result);
+        break;
+    }
+    case EOpAll:
+    {
+        bool result = true;
+        for (int i = 0; i < objectSize; i++) {
+            if (! unionArray[i].getBConst())
+                result = false;
+        }
+        newConstArray[0].setBConst(result);
+        break;
+    }
+
     // TODO: 3.0 Functionality: unary constant folding: the rest of the ops have to be fleshed out
 
     case EOpPackSnorm2x16:
@@ -412,11 +433,8 @@ TIntermTyped* TIntermConstantUnion::fold(TOperator op, const TType& returnType)
     case EOpDeterminant:
     case EOpMatrixInverse:
     case EOpTranspose:
-
-    case EOpAny:
-    case EOpAll:
         return 0;
-    
+
     default:
         assert(componentWise);
         break;