xref: /aosp_15_r20/external/angle/src/tests/compiler_tests/UnfoldShortCircuitAST_test.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 unfoldShortCircuit 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() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_GLSL_330_CORE_OUTPUT)
20     {
21         ShCompileOptions defaultCompileOptions   = {};
22         defaultCompileOptions.unfoldShortCircuit = true;
23         setDefaultCompileOptions(defaultCompileOptions);
24     }
25 };
26 
27 // Test unfolding the && operator.
TEST_F(UnfoldShortCircuitASTTest,UnfoldAnd)28 TEST_F(UnfoldShortCircuitASTTest, UnfoldAnd)
29 {
30     const std::string &shaderString =
31         R"(#version 300 es
32         precision highp float;
33 
34         out vec4 color;
35         uniform bool b;
36         uniform bool b2;
37 
38         void main()
39         {
40             color = vec4(0, 0, 0, 1);
41             if (b && b2)
42             {
43                 color = vec4(0, 1, 0, 1);
44             }
45         })";
46     compile(shaderString);
47     ASSERT_TRUE(foundInCode("(_ub) ? (_ub2) : (false)"));
48 }
49 
50 // Test unfolding the || operator.
TEST_F(UnfoldShortCircuitASTTest,UnfoldOr)51 TEST_F(UnfoldShortCircuitASTTest, UnfoldOr)
52 {
53     const std::string &shaderString =
54         R"(#version 300 es
55         precision highp float;
56 
57         out vec4 color;
58         uniform bool b;
59         uniform bool b2;
60 
61         void main()
62         {
63             color = vec4(0, 0, 0, 1);
64             if (b || b2)
65             {
66                 color = vec4(0, 1, 0, 1);
67             }
68         })";
69     compile(shaderString);
70     ASSERT_TRUE(foundInCode("(_ub) ? (true) : (_ub2)"));
71 }
72 
73 // Test unfolding nested && and || operators. Both should be unfolded.
TEST_F(UnfoldShortCircuitASTTest,UnfoldNested)74 TEST_F(UnfoldShortCircuitASTTest, UnfoldNested)
75 {
76     const std::string &shaderString =
77         R"(#version 300 es
78         precision highp float;
79 
80         out vec4 color;
81         uniform bool b;
82         uniform bool b2;
83         uniform bool b3;
84 
85         void main()
86         {
87             color = vec4(0, 0, 0, 1);
88             if (b && (b2 || b3))
89             {
90                 color = vec4(0, 1, 0, 1);
91             }
92         })";
93     compile(shaderString);
94     ASSERT_TRUE(foundInCode("(_ub) ? (((_ub2) ? (true) : (_ub3))) : (false)"));
95 }
96