1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // RecordConstantPrecision_test.cpp:
7 // Test for recording constant variable precision when it affects consuming expression.
8 //
9
10 #include "GLSLANG/ShaderLang.h"
11 #include "angle_gl.h"
12 #include "gtest/gtest.h"
13 #include "tests/test_utils/compiler_test.h"
14
15 using namespace sh;
16
17 class RecordConstantPrecisionTest : public MatchOutputCodeTest
18 {
19 public:
RecordConstantPrecisionTest()20 RecordConstantPrecisionTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, 0, SH_ESSL_OUTPUT) {}
21 };
22
23 // The constant cannot be folded if its precision is higher than the other operands, since it
24 // increases the precision of the consuming expression.
TEST_F(RecordConstantPrecisionTest,HigherPrecisionConstantAsParameter)25 TEST_F(RecordConstantPrecisionTest, HigherPrecisionConstantAsParameter)
26 {
27 const std::string &shaderString =
28 "uniform mediump float u;"
29 "void main()\n"
30 "{\n"
31 " const highp float a = 4096.5;\n"
32 " mediump float b = fract(a + u);\n"
33 " gl_FragColor = vec4(b);\n"
34 "}\n";
35 compile(shaderString);
36 ASSERT_TRUE(foundInCode("const highp float s"));
37 ASSERT_FALSE(foundInCode("fract(4096.5"));
38 ASSERT_FALSE(foundInCode("fract((4096.5"));
39 }
40
41 // The constant can be folded if its precision is equal to the other operands, as it does not
42 // increase the precision of the consuming expression.
TEST_F(RecordConstantPrecisionTest,EqualPrecisionConstantAsParameter)43 TEST_F(RecordConstantPrecisionTest, EqualPrecisionConstantAsParameter)
44 {
45 const std::string &shaderString =
46 "uniform mediump float u;"
47 "void main()\n"
48 "{\n"
49 " const mediump float a = 4096.5;\n"
50 " mediump float b = fract(a + u);\n"
51 " gl_FragColor = vec4(b);\n"
52 "}\n";
53 compile(shaderString);
54 ASSERT_FALSE(foundInCode("const mediump float s"));
55 ASSERT_TRUE(foundInCode("fract((4096.5"));
56 }
57
58 // The constant cannot be folded if its precision is higher than the other operands, since it
59 // increases the precision of the consuming expression. This applies also when the constant is
60 // part of a constant expression that can be folded.
TEST_F(RecordConstantPrecisionTest,FoldedBinaryConstantPrecisionIsHigher)61 TEST_F(RecordConstantPrecisionTest, FoldedBinaryConstantPrecisionIsHigher)
62 {
63 const std::string &shaderString =
64 "uniform mediump float u;"
65 "void main()\n"
66 "{\n"
67 " const highp float a = 4095.5;\n"
68 " mediump float b = fract((a + 1.0) + u);\n"
69 " gl_FragColor = vec4(b);\n"
70 "}\n";
71 compile(shaderString);
72 ASSERT_TRUE(foundInCode("const highp float s"));
73 ASSERT_FALSE(foundInCode("fract(4096.5"));
74 ASSERT_FALSE(foundInCode("fract((4096.5"));
75 }
76
77 // The constant cannot be folded if its precision is higher than the other operands, since it
78 // increases the precision of the consuming expression. This applies also when the constant is
79 // part of a constant expression that can be folded.
TEST_F(RecordConstantPrecisionTest,FoldedUnaryConstantPrecisionIsHigher)80 TEST_F(RecordConstantPrecisionTest, FoldedUnaryConstantPrecisionIsHigher)
81 {
82 const std::string &shaderString =
83 "uniform mediump float u;"
84 "void main()\n"
85 "{\n"
86 " const highp float a = 0.5;\n"
87 " mediump float b = sin(fract(a) + u);\n"
88 " gl_FragColor = vec4(b);\n"
89 "}\n";
90 compile(shaderString);
91 ASSERT_TRUE(foundInCode("const highp float s"));
92 ASSERT_FALSE(foundInCode("sin(0.5"));
93 ASSERT_FALSE(foundInCode("sin((0.5"));
94 }
95