TEST_F(ConstantFoldingTest, FoldIntegerModulus) { const std::string &shaderString = "#version 300 es\n" "precision mediump float;\n" "out int my_Int;\n" "void main() {\n" " const int i = 1124 % 5;\n" " my_Int = i;\n" "}\n"; compile(shaderString); ASSERT_FALSE(constantFoundInAST(1124)); ASSERT_FALSE(constantFoundInAST(5)); ASSERT_TRUE(constantFoundInAST(4)); }
// Check if the matrix 'determinant' operation on 4x4 matrix is constant folded. TEST_F(ConstantFoldingTest, Fold4x4MatrixDeterminant) { const std::string &shaderString = "#version 300 es\n" "precision mediump float;\n" "out float my_Float;" "void main() {\n" " const float f = determinant(mat4(29.0f, 31.0f, 37.0f, 41.0f,\n" " 43.0f, 47.0f, 53.0f, 59.0f,\n" " 61.0f, 67.0f, 71.0f, 73.0f,\n" " 79.0f, 83.0f, 89.0f, 97.0f));\n" " my_Float = f;\n" "}\n"; compile(shaderString); float inputElements[] = { 29.0f, 31.0f, 37.0f, 41.0f, 43.0f, 47.0f, 53.0f, 59.0f, 61.0f, 67.0f, 71.0f, 73.0f, 79.0f, 83.0f, 89.0f, 97.0f }; std::vector<float> input(inputElements, inputElements + 16); ASSERT_FALSE(constantVectorFoundInAST(input)); ASSERT_TRUE(constantFoundInAST(-2520.0f)); }
TEST_F(ConstantFoldingTest, FoldIntegerDiv) { const std::string &shaderString = "#version 300 es\n" "precision mediump float;\n" "out int my_Int;\n" "void main() {\n" " const int i = 1124 / 5;\n" " my_Int = i;\n" "}\n"; compile(shaderString); ASSERT_FALSE(constantFoundInAST(1124)); ASSERT_FALSE(constantFoundInAST(5)); // Rounding mode of division is undefined in the spec but ANGLE can be expected to round down. ASSERT_TRUE(constantFoundInAST(224)); }
// FoldMxNMatrixDeterminant tests check if the matrix 'determinant' operation // on MxN matrix is constant folded when argument is constant expression and also // checks the correctness of the result returned by the constant folding operation. // All the matrices including matrices in the shader code are in column-major order. TEST_F(ConstantFoldingTest, Fold2x2MatrixDeterminant) { const std::string &shaderString = "#version 300 es\n" "precision mediump float;\n" "out float my_Float;" "void main() {\n" " const float f = determinant(mat2(2.0f, 3.0f,\n" " 5.0f, 7.0f));\n" " my_Float = f;\n" "}\n"; compile(shaderString); float inputElements[] = { 2.0f, 3.0f, 5.0f, 7.0f }; std::vector<float> input(inputElements, inputElements + 4); ASSERT_FALSE(constantVectorFoundInAST(input)); ASSERT_TRUE(constantFoundInAST(-1.0f)); }
// Check if the matrix 'determinant' operation on 3x3 matrix is constant folded. TEST_F(ConstantFoldingTest, Fold3x3MatrixDeterminant) { const std::string &shaderString = "#version 300 es\n" "precision mediump float;\n" "out float my_Float;" "void main() {\n" " const float f = determinant(mat3(11.0f, 13.0f, 19.0f,\n" " 23.0f, 29.0f, 31.0f,\n" " 37.0f, 41.0f, 43.0f));\n" " my_Float = f;\n" "}\n"; compile(shaderString); float inputElements[] = { 11.0f, 13.0f, 19.0f, 23.0f, 29.0f, 31.0f, 37.0f, 41.0f, 43.0f }; std::vector<float> input(inputElements, inputElements + 9); ASSERT_FALSE(constantVectorFoundInAST(input)); ASSERT_TRUE(constantFoundInAST(-680.0f)); }
bool constantFoundInAST(int i) { ConstantUnion c; c.setIConst(i); return constantFoundInAST(c); }