blob: 450981c2f1f1a63653f8e6971aa9ba110ee75aab [file] [log] [blame]
Jamie Madill96509e42014-05-29 14:33:27 -04001#include "ANGLETest.h"
2
Jamie Madill2bf8b372014-06-16 17:18:51 -04003class GLSLTest : public ANGLETest
Jamie Madill96509e42014-05-29 14:33:27 -04004{
5protected:
Jamie Madill2bf8b372014-06-16 17:18:51 -04006 GLSLTest()
Jamie Madill96509e42014-05-29 14:33:27 -04007 {
8 setWindowWidth(128);
9 setWindowHeight(128);
10 setConfigRedBits(8);
11 setConfigGreenBits(8);
12 setConfigBlueBits(8);
13 setConfigAlphaBits(8);
14 }
Jamie Madillbfa91f42014-06-05 15:45:18 -040015
16 virtual void SetUp()
17 {
18 ANGLETest::SetUp();
19
Jamie Madill2bf8b372014-06-16 17:18:51 -040020 mSimpleVSSource = SHADER_SOURCE
Jamie Madillbfa91f42014-06-05 15:45:18 -040021 (
22 attribute vec4 inputAttribute;
23 void main()
24 {
25 gl_Position = inputAttribute;
26 }
27 );
28 }
29
Jamie Madill2bf8b372014-06-16 17:18:51 -040030 std::string mSimpleVSSource;
Jamie Madill96509e42014-05-29 14:33:27 -040031};
32
Jamie Madillb759a742014-07-17 10:40:49 -040033TEST_F(GLSLTest, NamelessScopedStructs)
Jamie Madill96509e42014-05-29 14:33:27 -040034{
Jamie Madillbfa91f42014-06-05 15:45:18 -040035 const std::string fragmentShaderSource = SHADER_SOURCE
Jamie Madill96509e42014-05-29 14:33:27 -040036 (
Jamie Madillbfa91f42014-06-05 15:45:18 -040037 precision mediump float;
38
Jamie Madill96509e42014-05-29 14:33:27 -040039 void main()
40 {
Jamie Madillbfa91f42014-06-05 15:45:18 -040041 struct
42 {
43 float q;
44 } b;
45
46 gl_FragColor = vec4(1, 0, 0, 1);
47 gl_FragColor.a += b.q;
Jamie Madill96509e42014-05-29 14:33:27 -040048 }
49 );
50
Jamie Madill2bf8b372014-06-16 17:18:51 -040051 GLuint program = compileProgram(mSimpleVSSource, fragmentShaderSource);
Jamie Madillbfa91f42014-06-05 15:45:18 -040052 EXPECT_NE(0u, program);
53}
Jamie Madillb759a742014-07-17 10:40:49 -040054TEST_F(GLSLTest, ScopedStructsOrderBug)
Jamie Madillbfa91f42014-06-05 15:45:18 -040055{
56 const std::string fragmentShaderSource = SHADER_SOURCE
57 (
58 precision mediump float;
59
60 struct T
61 {
62 float f;
63 };
64
65 void main()
66 {
67 T a;
68
69 struct T
70 {
71 float q;
72 };
73
74 T b;
75
76 gl_FragColor = vec4(1, 0, 0, 1);
77 gl_FragColor.a += a.f;
78 gl_FragColor.a += b.q;
79 }
80 );
81
Jamie Madill2bf8b372014-06-16 17:18:51 -040082 GLuint program = compileProgram(mSimpleVSSource, fragmentShaderSource);
Jamie Madillbfa91f42014-06-05 15:45:18 -040083 EXPECT_NE(0u, program);
84}
85
Jamie Madillb759a742014-07-17 10:40:49 -040086TEST_F(GLSLTest, ScopedStructsBug)
Jamie Madillbfa91f42014-06-05 15:45:18 -040087{
Jamie Madill96509e42014-05-29 14:33:27 -040088 const std::string fragmentShaderSource = SHADER_SOURCE
89 (
90 precision mediump float;
91
92 struct T_0
93 {
94 float f;
95 };
96
97 void main()
98 {
99 gl_FragColor = vec4(1, 0, 0, 1);
100
101 struct T
102 {
103 vec2 v;
104 };
105
106 T_0 a;
107 T b;
108
109 gl_FragColor.a += a.f;
110 gl_FragColor.a += b.v.x;
111 }
112 );
113
Jamie Madill2bf8b372014-06-16 17:18:51 -0400114 GLuint program = compileProgram(mSimpleVSSource, fragmentShaderSource);
115 EXPECT_NE(0u, program);
116}
117
Jamie Madillb759a742014-07-17 10:40:49 -0400118TEST_F(GLSLTest, DxPositionBug)
Jamie Madill2bf8b372014-06-16 17:18:51 -0400119{
120 const std::string &vertexShaderSource = SHADER_SOURCE
121 (
122 attribute vec4 inputAttribute;
123 varying float dx_Position;
124 void main()
125 {
126 gl_Position = vec4(inputAttribute);
127 dx_Position = 0.0;
128 }
129 );
130
131 const std::string &fragmentShaderSource = SHADER_SOURCE
132 (
133 precision mediump float;
134
135 varying float dx_Position;
136
137 void main()
138 {
139 gl_FragColor = vec4(dx_Position, 0, 0, 1);
140 }
141 );
142
143 GLuint program = compileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill96509e42014-05-29 14:33:27 -0400144 EXPECT_NE(0u, program);
145}
Jamie Madill4836d222014-07-24 06:55:51 -0400146
147TEST_F(GLSLTest, ElseIfRewriting)
148{
149 const std::string &vertexShaderSource =
150 "attribute vec4 a_position;\n"
151 "varying float v;\n"
152 "void main() {\n"
153 " gl_Position = a_position;\n"
154 " v = 1.0;\n"
155 " if (a_position.x <= 0.5) {\n"
156 " v = 0.0;\n"
157 " } else if (a_position.x >= 0.5) {\n"
158 " v = 2.0;\n"
159 " }\n"
160 "}\n";
161
162 const std::string &fragmentShaderSource =
163 "precision highp float;\n"
164 "varying float v;\n"
165 "void main() {\n"
166 " vec4 color = vec4(1.0, 0.0, 0.0, 1.0);\n"
167 " if (v >= 1.0) color = vec4(0.0, 1.0, 0.0, 1.0);\n"
168 " if (v >= 2.0) color = vec4(0.0, 0.0, 1.0, 1.0);\n"
169 " gl_FragColor = color;\n"
170 "}\n";
171
172 GLuint program = compileProgram(vertexShaderSource, fragmentShaderSource);
173 ASSERT_NE(0u, program);
174
175 drawQuad(program, "a_position", 0.5f);
176 swapBuffers();
177
178 EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
179 EXPECT_PIXEL_EQ(getWindowWidth()-1, 0, 0, 255, 0, 255);
180}
181
182TEST_F(GLSLTest, TwoElseIfRewriting)
183{
184 const std::string &vertexShaderSource =
185 "attribute vec4 a_position;\n"
186 "varying float v;\n"
187 "void main() {\n"
188 " gl_Position = a_position;\n"
189 " if (a_position.x == 0.0`) {\n"
190 " v = 1.0;\n"
191 " } else if (a_position.x > 0.5) {\n"
192 " v = 0.0;\n"
193 " } else if (a_position.x > 0.75) {\n"
194 " v = 0.5;\n"
195 " }\n"
196 "}\n";
197
198 const std::string &fragmentShaderSource =
199 "precision highp float;\n"
200 "varying float v;\n"
201 "void main() {\n"
202 " gl_FragColor = vec4(v, 0.0, 0.0, 1.0);\n"
203 "}\n";
204
205 GLuint program = compileProgram(vertexShaderSource, fragmentShaderSource);
206 EXPECT_NE(0u, program);
207}
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400208
209TEST_F(GLSLTest, InvariantVaryingOut)
210{
211 const std::string fragmentShaderSource = SHADER_SOURCE
212 (
213 precision mediump float;
214 varying float v_varying;
215 void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
216 );
217
218 const std::string vertexShaderSource = SHADER_SOURCE
219 (
220 attribute vec4 a_position;
221 invariant varying float v_varying;
222 void main() { v_varying = a_position.x; gl_Position = a_position; }
223 );
224
225 GLuint program = compileProgram(vertexShaderSource, fragmentShaderSource);
226 EXPECT_NE(0u, program);
227}
228
229TEST_F(GLSLTest, InvariantVaryingIn)
230{
231 const std::string fragmentShaderSource = SHADER_SOURCE
232 (
233 precision mediump float;
234 invariant varying float v_varying;
235 void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
236 );
237
238 const std::string vertexShaderSource = SHADER_SOURCE
239 (
240 attribute vec4 a_position;
241 varying float v_varying;
242 void main() { v_varying = a_position.x; gl_Position = a_position; }
243 );
244
245 GLuint program = compileProgram(vertexShaderSource, fragmentShaderSource);
246 EXPECT_NE(0u, program);
247}
248
249TEST_F(GLSLTest, InvariantVaryingBoth)
250{
251 const std::string fragmentShaderSource = SHADER_SOURCE
252 (
253 precision mediump float;
254 invariant varying float v_varying;
255 void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
256 );
257
258 const std::string vertexShaderSource = SHADER_SOURCE
259 (
260 attribute vec4 a_position;
261 invariant varying float v_varying;
262 void main() { v_varying = a_position.x; gl_Position = a_position; }
263 );
264
265 GLuint program = compileProgram(vertexShaderSource, fragmentShaderSource);
266 EXPECT_NE(0u, program);
267}
268
269TEST_F(GLSLTest, InvariantGLPosition)
270{
271 const std::string fragmentShaderSource = SHADER_SOURCE
272 (
273 precision mediump float;
274 varying float v_varying;
275 void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
276 );
277
278 const std::string vertexShaderSource = SHADER_SOURCE
279 (
280 attribute vec4 a_position;
281 invariant gl_Position;
282 varying float v_varying;
283 void main() { v_varying = a_position.x; gl_Position = a_position; }
284 );
285
286 GLuint program = compileProgram(vertexShaderSource, fragmentShaderSource);
287 EXPECT_NE(0u, program);
288}
289
290TEST_F(GLSLTest, InvariantAll)
291{
292 const std::string fragmentShaderSource = SHADER_SOURCE
293 (
294 precision mediump float;
295 varying float v_varying;
296 void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }
297 );
298
299 const std::string vertexShaderSource =
300 "#pragma STDGL invariant(all)\n"
301 "attribute vec4 a_position;\n"
302 "varying float v_varying;\n"
303 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
304
305 GLuint program = compileProgram(vertexShaderSource, fragmentShaderSource);
306 EXPECT_NE(0u, program);
307}