1 //
2 // Copyright 2018 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 // UnfoldShortCircuitAST_test.cpp:
7 //  Tests shader compilation with SH_UNFOLD_SHORT_CIRCUIT workaround on.
8 
9 #include "GLSLANG/ShaderLang.h"
10 #include "angle_gl.h"
11 #include "gtest/gtest.h"
12 #include "tests/test_utils/compiler_test.h"
13 
14 using namespace sh;
15 
16 class UnfoldShortCircuitASTTest : public MatchOutputCodeTest
17 {
18   public:
UnfoldShortCircuitASTTest()19     UnfoldShortCircuitASTTest()
20         : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_UNFOLD_SHORT_CIRCUIT, SH_GLSL_330_CORE_OUTPUT)
21     {}
22 };
23 
24 // Test unfolding the && operator.
TEST_F(UnfoldShortCircuitASTTest,UnfoldAnd)25 TEST_F(UnfoldShortCircuitASTTest, UnfoldAnd)
26 {
27     const std::string &shaderString =
28         R"(#version 300 es
29         precision highp float;
30 
31         out vec4 color;
32         uniform bool b;
33         uniform bool b2;
34 
35         void main()
36         {
37             color = vec4(0, 0, 0, 1);
38             if (b && b2)
39             {
40                 color = vec4(0, 1, 0, 1);
41             }
42         })";
43     compile(shaderString);
44     ASSERT_TRUE(foundInCode("(_ub) ? (_ub2) : (false)"));
45 }
46 
47 // Test unfolding the || operator.
TEST_F(UnfoldShortCircuitASTTest,UnfoldOr)48 TEST_F(UnfoldShortCircuitASTTest, UnfoldOr)
49 {
50     const std::string &shaderString =
51         R"(#version 300 es
52         precision highp float;
53 
54         out vec4 color;
55         uniform bool b;
56         uniform bool b2;
57 
58         void main()
59         {
60             color = vec4(0, 0, 0, 1);
61             if (b || b2)
62             {
63                 color = vec4(0, 1, 0, 1);
64             }
65         })";
66     compile(shaderString);
67     ASSERT_TRUE(foundInCode("(_ub) ? (true) : (_ub2)"));
68 }
69 
70 // Test unfolding nested && and || operators. Both should be unfolded.
TEST_F(UnfoldShortCircuitASTTest,UnfoldNested)71 TEST_F(UnfoldShortCircuitASTTest, UnfoldNested)
72 {
73     const std::string &shaderString =
74         R"(#version 300 es
75         precision highp float;
76 
77         out vec4 color;
78         uniform bool b;
79         uniform bool b2;
80         uniform bool b3;
81 
82         void main()
83         {
84             color = vec4(0, 0, 0, 1);
85             if (b && (b2 || b3))
86             {
87                 color = vec4(0, 1, 0, 1);
88             }
89         })";
90     compile(shaderString);
91     ASSERT_TRUE(foundInCode("(_ub) ? (((_ub2) ? (true) : (_ub3))) : (false)"));
92 }
93