• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2017 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 // RegenerateStructNames_test.cpp:
7 //   Tests for regenerating struct names.
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 RegenerateStructNamesTest : public MatchOutputCodeTest
18 {
19   public:
RegenerateStructNamesTest()20     RegenerateStructNamesTest() : MatchOutputCodeTest(GL_FRAGMENT_SHADER, SH_ESSL_OUTPUT)
21     {
22         ShCompileOptions defaultCompileOptions      = {};
23         defaultCompileOptions.regenerateStructNames = true;
24         setDefaultCompileOptions(defaultCompileOptions);
25     }
26 };
27 
28 // Test that a struct defined in a function scope is renamed. The global struct that's used as a
29 // type of a uniform cannot be renamed.
TEST_F(RegenerateStructNamesTest,GlobalStructAndLocalStructWithTheSameName)30 TEST_F(RegenerateStructNamesTest, GlobalStructAndLocalStructWithTheSameName)
31 {
32     const std::string &shaderString =
33         R"(precision mediump float;
34 
35         struct myStruct
36         {
37             float foo;
38         };
39 
40         uniform myStruct us;
41 
42         void main()
43         {
44             struct myStruct
45             {
46                 vec2 bar;
47             };
48             myStruct scoped;
49             scoped.bar = vec2(1.0, 2.0) * us.foo;
50             gl_FragColor = vec4(scoped.bar, 0.0, 1.0);
51         })";
52     compile(shaderString);
53     EXPECT_TRUE(foundInCode("struct _umyStruct"));
54     EXPECT_TRUE(foundInCode("struct _u_webgl_struct_"));
55 }
56 
57 // Test that a nameless struct is handled gracefully.
TEST_F(RegenerateStructNamesTest,NamelessStruct)58 TEST_F(RegenerateStructNamesTest, NamelessStruct)
59 {
60     const std::string &shaderString =
61         R"(precision mediump float;
62 
63         uniform float u;
64 
65         void main()
66         {
67             struct
68             {
69                 vec2 bar;
70             } scoped;
71             scoped.bar = vec2(1.0, 2.0) * u;
72             gl_FragColor = vec4(scoped.bar, 0.0, 1.0);
73         })";
74     compile(shaderString);
75     EXPECT_TRUE(foundInCode("struct"));
76 }
77