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 // AppendixALimitations_test.cpp:
7 // Tests for validating ESSL 1.00 Appendix A limitations.
8 //
9
10 #include "gtest/gtest.h"
11
12 #include "GLSLANG/ShaderLang.h"
13 #include "angle_gl.h"
14 #include "gtest/gtest.h"
15 #include "tests/test_utils/ShaderCompileTreeTest.h"
16
17 using namespace sh;
18
19 class AppendixALimitationsTest : public ShaderCompileTreeTest
20 {
21 public:
AppendixALimitationsTest()22 AppendixALimitationsTest() {}
23
24 protected:
getShaderType() const25 ::GLenum getShaderType() const override { return GL_FRAGMENT_SHADER; }
getShaderSpec() const26 ShShaderSpec getShaderSpec() const override { return SH_WEBGL_SPEC; }
27 };
28
29 // Test an invalid shader where a for loop index is used as an out parameter.
TEST_F(AppendixALimitationsTest,IndexAsFunctionOutParameter)30 TEST_F(AppendixALimitationsTest, IndexAsFunctionOutParameter)
31 {
32 const std::string &shaderString =
33 "precision mediump float;\n"
34 "void fun(out int a)\n"
35 "{\n"
36 " a = 2;\n"
37 "}\n"
38 "void main()\n"
39 "{\n"
40 " for (int i = 0; i < 2; ++i)\n"
41 " {\n"
42 " fun(i);\n"
43 " }\n"
44 " gl_FragColor = vec4(0.0);\n"
45 "}\n";
46 if (compile(shaderString))
47 {
48 FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
49 }
50 }
51
52 // Test an invalid shader where a for loop index is used as an inout parameter.
TEST_F(AppendixALimitationsTest,IndexAsFunctionInOutParameter)53 TEST_F(AppendixALimitationsTest, IndexAsFunctionInOutParameter)
54 {
55 const std::string &shaderString =
56 "precision mediump float;\n"
57 "void fun(int b, inout int a)\n"
58 "{\n"
59 " a += b;\n"
60 "}\n"
61 "void main()\n"
62 "{\n"
63 " for (int i = 0; i < 2; ++i)\n"
64 " {\n"
65 " fun(2, i);\n"
66 " }\n"
67 " gl_FragColor = vec4(0.0);\n"
68 "}\n";
69 if (compile(shaderString))
70 {
71 FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
72 }
73 }
74
75 // Test a valid shader where a for loop index is used as an in parameter in a function that also has
76 // an out parameter.
TEST_F(AppendixALimitationsTest,IndexAsFunctionInParameter)77 TEST_F(AppendixALimitationsTest, IndexAsFunctionInParameter)
78 {
79 const std::string &shaderString =
80 "precision mediump float;\n"
81 "void fun(int b, inout int a)\n"
82 "{\n"
83 " a += b;\n"
84 "}\n"
85 "void main()\n"
86 "{\n"
87 " for (int i = 0; i < 2; ++i)\n"
88 " {\n"
89 " int a = 1;"
90 " fun(i, a);\n"
91 " }\n"
92 " gl_FragColor = vec4(0.0);\n"
93 "}\n";
94 if (!compile(shaderString))
95 {
96 FAIL() << "Shader compilation failed, expecting success:\n" << mInfoLog;
97 }
98 }
99
100 // Test an invalid shader where a for loop index is used as a target of assignment.
TEST_F(AppendixALimitationsTest,IndexAsTargetOfAssignment)101 TEST_F(AppendixALimitationsTest, IndexAsTargetOfAssignment)
102 {
103 const std::string &shaderString =
104 "precision mediump float;\n"
105 "void main()\n"
106 "{\n"
107 " for (int i = 0; i < 2; ++i)\n"
108 " {\n"
109 " i = 2;\n"
110 " }\n"
111 " gl_FragColor = vec4(0.0);\n"
112 "}\n";
113 if (compile(shaderString))
114 {
115 FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
116 }
117 }
118
119 // Test an invalid shader where a for loop index is incremented inside the loop.
TEST_F(AppendixALimitationsTest,IndexIncrementedInLoopBody)120 TEST_F(AppendixALimitationsTest, IndexIncrementedInLoopBody)
121 {
122 const std::string &shaderString =
123 "precision mediump float;\n"
124 "void main()\n"
125 "{\n"
126 " for (int i = 0; i < 2; ++i)\n"
127 " {\n"
128 " ++i;\n"
129 " }\n"
130 " gl_FragColor = vec4(0.0);\n"
131 "}\n";
132 if (compile(shaderString))
133 {
134 FAIL() << "Shader compilation succeeded, expecting failure:\n" << mInfoLog;
135 }
136 }
137