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