blob: b4c53f3a49e3f063210e6b9b1328ea162d230bea [file] [log] [blame]
Jamie Madill55def582015-05-04 11:24:57 -04001//
2// Copyright 2015 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
Corentin Wallezd3970de2015-05-14 11:07:48 -04007#include "test_utils/ANGLETest.h"
Jamie Madill96509e42014-05-29 14:33:27 -04008
Jamie Madill55def582015-05-04 11:24:57 -04009#include "libANGLE/Context.h"
10#include "libANGLE/Program.h"
11
Jamie Madillfa05f602015-05-07 13:47:11 -040012using namespace angle;
Austin Kinross18b931d2014-09-29 12:58:31 -070013
Jamie Madill2bf8b372014-06-16 17:18:51 -040014class GLSLTest : public ANGLETest
Jamie Madill96509e42014-05-29 14:33:27 -040015{
Jamie Madillfa05f602015-05-07 13:47:11 -040016 protected:
17 GLSLTest()
Jamie Madill96509e42014-05-29 14:33:27 -040018 {
19 setWindowWidth(128);
20 setWindowHeight(128);
21 setConfigRedBits(8);
22 setConfigGreenBits(8);
23 setConfigBlueBits(8);
24 setConfigAlphaBits(8);
25 }
Jamie Madillbfa91f42014-06-05 15:45:18 -040026
27 virtual void SetUp()
28 {
29 ANGLETest::SetUp();
30
Jamie Madill2bf8b372014-06-16 17:18:51 -040031 mSimpleVSSource = SHADER_SOURCE
Jamie Madillbfa91f42014-06-05 15:45:18 -040032 (
33 attribute vec4 inputAttribute;
34 void main()
35 {
36 gl_Position = inputAttribute;
37 }
38 );
39 }
40
Austin Kinrossaf875522014-08-25 21:06:07 -070041 std::string GenerateVaryingType(GLint vectorSize)
42 {
43 char varyingType[10];
44
45 if (vectorSize == 1)
46 {
47 sprintf(varyingType, "float");
48 }
49 else
50 {
51 sprintf(varyingType, "vec%d", vectorSize);
52 }
53
54 return std::string(varyingType);
55 }
56
57 std::string GenerateVectorVaryingDeclaration(GLint vectorSize, GLint arraySize, GLint id)
58 {
59 char buff[100];
60
61 if (arraySize == 1)
62 {
63 sprintf(buff, "varying %s v%d;\n", GenerateVaryingType(vectorSize).c_str(), id);
64 }
65 else
66 {
67 sprintf(buff, "varying %s v%d[%d];\n", GenerateVaryingType(vectorSize).c_str(), id, arraySize);
68 }
69
70 return std::string(buff);
71 }
72
73 std::string GenerateVectorVaryingSettingCode(GLint vectorSize, GLint arraySize, GLint id)
74 {
75 std::string returnString;
76 char buff[100];
77
78 if (arraySize == 1)
79 {
80 sprintf(buff, "\t v%d = %s(1.0);\n", id, GenerateVaryingType(vectorSize).c_str());
81 returnString += buff;
82 }
83 else
84 {
85 for (int i = 0; i < arraySize; i++)
86 {
87 sprintf(buff, "\t v%d[%d] = %s(1.0);\n", id, i, GenerateVaryingType(vectorSize).c_str());
88 returnString += buff;
89 }
90 }
91
92 return returnString;
93 }
94
95 std::string GenerateVectorVaryingUseCode(GLint arraySize, GLint id)
96 {
97 if (arraySize == 1)
98 {
99 char buff[100];
100 sprintf(buff, "v%d + ", id);
101 return std::string(buff);
102 }
103 else
104 {
105 std::string returnString;
106 for (int i = 0; i < arraySize; i++)
107 {
108 char buff[100];
109 sprintf(buff, "v%d[%d] + ", id, i);
110 returnString += buff;
111 }
112 return returnString;
113 }
114 }
115
Austin Kinross8b695ee2015-03-12 13:12:20 -0700116 void GenerateGLSLWithVaryings(GLint floatCount, GLint floatArrayCount, GLint vec2Count, GLint vec2ArrayCount, GLint vec3Count, GLint vec3ArrayCount,
117 GLint vec4Count, GLint vec4ArrayCount, bool useFragCoord, bool usePointCoord, bool usePointSize,
118 std::string* fragmentShader, std::string* vertexShader)
Austin Kinrossaf875522014-08-25 21:06:07 -0700119 {
120 // Generate a string declaring the varyings, to share between the fragment shader and the vertex shader.
121 std::string varyingDeclaration;
122
123 unsigned int varyingCount = 0;
124
125 for (GLint i = 0; i < floatCount; i++)
126 {
127 varyingDeclaration += GenerateVectorVaryingDeclaration(1, 1, varyingCount);
128 varyingCount += 1;
129 }
130
131 for (GLint i = 0; i < floatArrayCount; i++)
132 {
133 varyingDeclaration += GenerateVectorVaryingDeclaration(1, 2, varyingCount);
134 varyingCount += 1;
135 }
136
137 for (GLint i = 0; i < vec2Count; i++)
138 {
139 varyingDeclaration += GenerateVectorVaryingDeclaration(2, 1, varyingCount);
140 varyingCount += 1;
141 }
142
143 for (GLint i = 0; i < vec2ArrayCount; i++)
144 {
145 varyingDeclaration += GenerateVectorVaryingDeclaration(2, 2, varyingCount);
146 varyingCount += 1;
147 }
148
149 for (GLint i = 0; i < vec3Count; i++)
150 {
151 varyingDeclaration += GenerateVectorVaryingDeclaration(3, 1, varyingCount);
152 varyingCount += 1;
153 }
154
155 for (GLint i = 0; i < vec3ArrayCount; i++)
156 {
157 varyingDeclaration += GenerateVectorVaryingDeclaration(3, 2, varyingCount);
158 varyingCount += 1;
159 }
160
Austin Kinross8b695ee2015-03-12 13:12:20 -0700161 for (GLint i = 0; i < vec4Count; i++)
162 {
163 varyingDeclaration += GenerateVectorVaryingDeclaration(4, 1, varyingCount);
164 varyingCount += 1;
165 }
166
167 for (GLint i = 0; i < vec4ArrayCount; i++)
168 {
169 varyingDeclaration += GenerateVectorVaryingDeclaration(4, 2, varyingCount);
170 varyingCount += 1;
171 }
172
Austin Kinrossaf875522014-08-25 21:06:07 -0700173 // Generate the vertex shader
174 vertexShader->clear();
175 vertexShader->append(varyingDeclaration);
176 vertexShader->append("\nvoid main()\n{\n");
177
178 unsigned int currentVSVarying = 0;
179
180 for (GLint i = 0; i < floatCount; i++)
181 {
182 vertexShader->append(GenerateVectorVaryingSettingCode(1, 1, currentVSVarying));
183 currentVSVarying += 1;
184 }
185
186 for (GLint i = 0; i < floatArrayCount; i++)
187 {
188 vertexShader->append(GenerateVectorVaryingSettingCode(1, 2, currentVSVarying));
189 currentVSVarying += 1;
190 }
191
192 for (GLint i = 0; i < vec2Count; i++)
193 {
194 vertexShader->append(GenerateVectorVaryingSettingCode(2, 1, currentVSVarying));
195 currentVSVarying += 1;
196 }
197
198 for (GLint i = 0; i < vec2ArrayCount; i++)
199 {
200 vertexShader->append(GenerateVectorVaryingSettingCode(2, 2, currentVSVarying));
201 currentVSVarying += 1;
202 }
203
204 for (GLint i = 0; i < vec3Count; i++)
205 {
206 vertexShader->append(GenerateVectorVaryingSettingCode(3, 1, currentVSVarying));
207 currentVSVarying += 1;
208 }
209
210 for (GLint i = 0; i < vec3ArrayCount; i++)
211 {
212 vertexShader->append(GenerateVectorVaryingSettingCode(3, 2, currentVSVarying));
213 currentVSVarying += 1;
214 }
215
Austin Kinross8b695ee2015-03-12 13:12:20 -0700216 for (GLint i = 0; i < vec4Count; i++)
217 {
218 vertexShader->append(GenerateVectorVaryingSettingCode(4, 1, currentVSVarying));
219 currentVSVarying += 1;
220 }
221
222 for (GLint i = 0; i < vec4ArrayCount; i++)
223 {
224 vertexShader->append(GenerateVectorVaryingSettingCode(4, 2, currentVSVarying));
225 currentVSVarying += 1;
226 }
227
228 if (usePointSize)
229 {
230 vertexShader->append("gl_PointSize = 1.0;\n");
231 }
232
Austin Kinrossaf875522014-08-25 21:06:07 -0700233 vertexShader->append("}\n");
234
235 // Generate the fragment shader
236 fragmentShader->clear();
237 fragmentShader->append("precision highp float;\n");
238 fragmentShader->append(varyingDeclaration);
239 fragmentShader->append("\nvoid main() \n{ \n\tvec4 retColor = vec4(0,0,0,0);\n");
240
241 unsigned int currentFSVarying = 0;
242
243 // Make use of the float varyings
244 fragmentShader->append("\tretColor += vec4(");
245
246 for (GLint i = 0; i < floatCount; i++)
247 {
248 fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
249 currentFSVarying += 1;
250 }
251
252 for (GLint i = 0; i < floatArrayCount; i++)
253 {
254 fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
255 currentFSVarying += 1;
256 }
257
258 fragmentShader->append("0.0, 0.0, 0.0, 0.0);\n");
259
260 // Make use of the vec2 varyings
261 fragmentShader->append("\tretColor += vec4(");
262
263 for (GLint i = 0; i < vec2Count; i++)
264 {
265 fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
266 currentFSVarying += 1;
267 }
268
269 for (GLint i = 0; i < vec2ArrayCount; i++)
270 {
271 fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
272 currentFSVarying += 1;
273 }
274
275 fragmentShader->append("vec2(0.0, 0.0), 0.0, 0.0);\n");
276
277 // Make use of the vec3 varyings
278 fragmentShader->append("\tretColor += vec4(");
279
280 for (GLint i = 0; i < vec3Count; i++)
281 {
282 fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
283 currentFSVarying += 1;
284 }
285
286 for (GLint i = 0; i < vec3ArrayCount; i++)
287 {
288 fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
289 currentFSVarying += 1;
290 }
291
292 fragmentShader->append("vec3(0.0, 0.0, 0.0), 0.0);\n");
Austin Kinross8b695ee2015-03-12 13:12:20 -0700293
294 // Make use of the vec4 varyings
295 fragmentShader->append("\tretColor += ");
296
297 for (GLint i = 0; i < vec4Count; i++)
298 {
299 fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
300 currentFSVarying += 1;
301 }
302
303 for (GLint i = 0; i < vec4ArrayCount; i++)
304 {
305 fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
306 currentFSVarying += 1;
307 }
308
309 fragmentShader->append("vec4(0.0, 0.0, 0.0, 0.0);\n");
310
311 // Set gl_FragColor, and use special variables if requested
312 fragmentShader->append("\tgl_FragColor = retColor");
313
314 if (useFragCoord)
315 {
316 fragmentShader->append(" + gl_FragCoord");
317 }
318
319 if (usePointCoord)
320 {
321 fragmentShader->append(" + vec4(gl_PointCoord, 0.0, 0.0)");
322 }
323
324 fragmentShader->append(";\n}");
325 }
326
327 void VaryingTestBase(GLint floatCount, GLint floatArrayCount, GLint vec2Count, GLint vec2ArrayCount, GLint vec3Count, GLint vec3ArrayCount,
328 GLint vec4Count, GLint vec4ArrayCount, bool useFragCoord, bool usePointCoord, bool usePointSize, bool expectSuccess)
329 {
330 std::string fragmentShaderSource;
331 std::string vertexShaderSource;
332
333 GenerateGLSLWithVaryings(floatCount, floatArrayCount, vec2Count, vec2ArrayCount, vec3Count, vec3ArrayCount,
334 vec4Count, vec4ArrayCount, useFragCoord, usePointCoord, usePointSize,
335 &fragmentShaderSource, &vertexShaderSource);
336
337 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
338
339 if (expectSuccess)
340 {
341 EXPECT_NE(0u, program);
342 }
343 else
344 {
345 EXPECT_EQ(0u, program);
346 }
Austin Kinrossaf875522014-08-25 21:06:07 -0700347 }
348
Austin Kinross7a3e8e22015-10-08 15:50:06 -0700349 void CompileGLSLWithUniformsAndSamplers(GLint vertexUniformCount,
350 GLint fragmentUniformCount,
351 GLint vertexSamplersCount,
352 GLint fragmentSamplersCount,
353 bool expectSuccess)
354 {
355 std::stringstream vertexShader;
356 std::stringstream fragmentShader;
357
358 // Generate the vertex shader
359 vertexShader << "precision mediump float;\n";
360
361 for (int i = 0; i < vertexUniformCount; i++)
362 {
363 vertexShader << "uniform vec4 v" << i << ";\n";
364 }
365
366 for (int i = 0; i < vertexSamplersCount; i++)
367 {
368 vertexShader << "uniform sampler2D s" << i << ";\n";
369 }
370
371 vertexShader << "void main()\n{\n";
372
373 for (int i = 0; i < vertexUniformCount; i++)
374 {
375 vertexShader << " gl_Position += v" << i << ";\n";
376 }
377
378 for (int i = 0; i < vertexSamplersCount; i++)
379 {
380 vertexShader << " gl_Position += texture2D(s" << i << ", vec2(0.0, 0.0));\n";
381 }
382
383 if (vertexUniformCount == 0 && vertexSamplersCount == 0)
384 {
385 vertexShader << " gl_Position = vec4(0.0);\n";
386 }
387
388 vertexShader << "}\n";
389
390 // Generate the fragment shader
391 fragmentShader << "precision mediump float;\n";
392
393 for (int i = 0; i < fragmentUniformCount; i++)
394 {
395 fragmentShader << "uniform vec4 v" << i << ";\n";
396 }
397
398 for (int i = 0; i < fragmentSamplersCount; i++)
399 {
400 fragmentShader << "uniform sampler2D s" << i << ";\n";
401 }
402
403 fragmentShader << "void main()\n{\n";
404
405 for (int i = 0; i < fragmentUniformCount; i++)
406 {
407 fragmentShader << " gl_FragColor += v" << i << ";\n";
408 }
409
410 for (int i = 0; i < fragmentSamplersCount; i++)
411 {
412 fragmentShader << " gl_FragColor += texture2D(s" << i << ", vec2(0.0, 0.0));\n";
413 }
414
415 if (fragmentUniformCount == 0 && fragmentSamplersCount == 0)
416 {
417 fragmentShader << " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n";
418 }
419
420 fragmentShader << "}\n";
421
422 GLuint program = CompileProgram(vertexShader.str(), fragmentShader.str());
423
424 if (expectSuccess)
425 {
426 EXPECT_NE(0u, program);
427 }
428 else
429 {
430 EXPECT_EQ(0u, program);
431 }
432 }
433
Jamie Madill2bf8b372014-06-16 17:18:51 -0400434 std::string mSimpleVSSource;
Jamie Madill96509e42014-05-29 14:33:27 -0400435};
436
Jamie Madillfa05f602015-05-07 13:47:11 -0400437class GLSLTest_ES3 : public GLSLTest
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000438{
Olli Etuahoe1d199b2016-07-19 17:14:27 +0300439 void SetUp() override
440 {
441 ANGLETest::SetUp();
442
443 mSimpleVSSource =
444 "#version 300 es\n"
445 "in vec4 inputAttribute;"
446 "void main()"
447 "{"
448 " gl_Position = inputAttribute;"
449 "}";
450 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000451};
452
Jamie Madillfa05f602015-05-07 13:47:11 -0400453TEST_P(GLSLTest, NamelessScopedStructs)
Jamie Madill96509e42014-05-29 14:33:27 -0400454{
Jamie Madillbfa91f42014-06-05 15:45:18 -0400455 const std::string fragmentShaderSource = SHADER_SOURCE
Jamie Madill96509e42014-05-29 14:33:27 -0400456 (
Jamie Madillbfa91f42014-06-05 15:45:18 -0400457 precision mediump float;
458
Jamie Madill96509e42014-05-29 14:33:27 -0400459 void main()
460 {
Jamie Madillbfa91f42014-06-05 15:45:18 -0400461 struct
462 {
463 float q;
464 } b;
465
466 gl_FragColor = vec4(1, 0, 0, 1);
467 gl_FragColor.a += b.q;
Jamie Madill96509e42014-05-29 14:33:27 -0400468 }
469 );
470
Jamie Madill5599c8f2014-08-26 13:16:39 -0400471 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
Jamie Madillbfa91f42014-06-05 15:45:18 -0400472 EXPECT_NE(0u, program);
473}
Austin Kinross18b931d2014-09-29 12:58:31 -0700474
Jamie Madillfa05f602015-05-07 13:47:11 -0400475TEST_P(GLSLTest, ScopedStructsOrderBug)
Jamie Madillbfa91f42014-06-05 15:45:18 -0400476{
Geoff Lange0cc2a42016-01-20 10:58:17 -0500477 // TODO(geofflang): Find out why this doesn't compile on Apple OpenGL drivers
478 // (http://anglebug.com/1292)
Geoff Lang5103f4c2016-01-26 11:40:18 -0500479 // TODO(geofflang): Find out why this doesn't compile on AMD OpenGL drivers
Geoff Lange0cc2a42016-01-20 10:58:17 -0500480 // (http://anglebug.com/1291)
Corentin Wallezc7f59d02016-06-20 10:12:08 -0400481 if (IsDesktopOpenGL() && (IsOSX() || !IsNVIDIA()))
Geoff Lange0cc2a42016-01-20 10:58:17 -0500482 {
Jamie Madill518b9fa2016-03-02 11:26:02 -0500483 std::cout << "Test disabled on this OpenGL configuration." << std::endl;
Geoff Lange0cc2a42016-01-20 10:58:17 -0500484 return;
485 }
Geoff Lange0cc2a42016-01-20 10:58:17 -0500486
Jamie Madillbfa91f42014-06-05 15:45:18 -0400487 const std::string fragmentShaderSource = SHADER_SOURCE
488 (
489 precision mediump float;
490
491 struct T
492 {
493 float f;
494 };
495
496 void main()
497 {
498 T a;
499
500 struct T
501 {
502 float q;
503 };
504
505 T b;
506
507 gl_FragColor = vec4(1, 0, 0, 1);
508 gl_FragColor.a += a.f;
509 gl_FragColor.a += b.q;
510 }
511 );
512
Jamie Madill5599c8f2014-08-26 13:16:39 -0400513 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
Jamie Madillbfa91f42014-06-05 15:45:18 -0400514 EXPECT_NE(0u, program);
515}
516
Jamie Madillfa05f602015-05-07 13:47:11 -0400517TEST_P(GLSLTest, ScopedStructsBug)
Jamie Madillbfa91f42014-06-05 15:45:18 -0400518{
Jamie Madill96509e42014-05-29 14:33:27 -0400519 const std::string fragmentShaderSource = SHADER_SOURCE
520 (
521 precision mediump float;
522
523 struct T_0
524 {
525 float f;
526 };
527
528 void main()
529 {
530 gl_FragColor = vec4(1, 0, 0, 1);
531
532 struct T
533 {
534 vec2 v;
535 };
536
537 T_0 a;
538 T b;
539
540 gl_FragColor.a += a.f;
541 gl_FragColor.a += b.v.x;
542 }
543 );
544
Jamie Madill5599c8f2014-08-26 13:16:39 -0400545 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
Jamie Madill2bf8b372014-06-16 17:18:51 -0400546 EXPECT_NE(0u, program);
547}
548
Jamie Madillfa05f602015-05-07 13:47:11 -0400549TEST_P(GLSLTest, DxPositionBug)
Jamie Madill2bf8b372014-06-16 17:18:51 -0400550{
551 const std::string &vertexShaderSource = SHADER_SOURCE
552 (
553 attribute vec4 inputAttribute;
554 varying float dx_Position;
555 void main()
556 {
557 gl_Position = vec4(inputAttribute);
558 dx_Position = 0.0;
559 }
560 );
561
562 const std::string &fragmentShaderSource = SHADER_SOURCE
563 (
564 precision mediump float;
565
566 varying float dx_Position;
567
568 void main()
569 {
570 gl_FragColor = vec4(dx_Position, 0, 0, 1);
571 }
572 );
573
Jamie Madill5599c8f2014-08-26 13:16:39 -0400574 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill96509e42014-05-29 14:33:27 -0400575 EXPECT_NE(0u, program);
576}
Jamie Madill4836d222014-07-24 06:55:51 -0400577
Jamie Madillfa05f602015-05-07 13:47:11 -0400578TEST_P(GLSLTest, ElseIfRewriting)
Jamie Madill4836d222014-07-24 06:55:51 -0400579{
580 const std::string &vertexShaderSource =
581 "attribute vec4 a_position;\n"
582 "varying float v;\n"
583 "void main() {\n"
584 " gl_Position = a_position;\n"
585 " v = 1.0;\n"
586 " if (a_position.x <= 0.5) {\n"
587 " v = 0.0;\n"
588 " } else if (a_position.x >= 0.5) {\n"
589 " v = 2.0;\n"
590 " }\n"
591 "}\n";
592
593 const std::string &fragmentShaderSource =
594 "precision highp float;\n"
595 "varying float v;\n"
596 "void main() {\n"
597 " vec4 color = vec4(1.0, 0.0, 0.0, 1.0);\n"
598 " if (v >= 1.0) color = vec4(0.0, 1.0, 0.0, 1.0);\n"
599 " if (v >= 2.0) color = vec4(0.0, 0.0, 1.0, 1.0);\n"
600 " gl_FragColor = color;\n"
601 "}\n";
602
Jamie Madill5599c8f2014-08-26 13:16:39 -0400603 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill4836d222014-07-24 06:55:51 -0400604 ASSERT_NE(0u, program);
605
606 drawQuad(program, "a_position", 0.5f);
Jamie Madill4836d222014-07-24 06:55:51 -0400607
608 EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
609 EXPECT_PIXEL_EQ(getWindowWidth()-1, 0, 0, 255, 0, 255);
610}
611
Jamie Madillfa05f602015-05-07 13:47:11 -0400612TEST_P(GLSLTest, TwoElseIfRewriting)
Jamie Madill4836d222014-07-24 06:55:51 -0400613{
614 const std::string &vertexShaderSource =
615 "attribute vec4 a_position;\n"
616 "varying float v;\n"
617 "void main() {\n"
618 " gl_Position = a_position;\n"
Jamie Madill778d5272014-08-04 13:13:25 -0400619 " if (a_position.x == 0.0) {\n"
Jamie Madill4836d222014-07-24 06:55:51 -0400620 " v = 1.0;\n"
621 " } else if (a_position.x > 0.5) {\n"
622 " v = 0.0;\n"
623 " } else if (a_position.x > 0.75) {\n"
624 " v = 0.5;\n"
625 " }\n"
626 "}\n";
627
628 const std::string &fragmentShaderSource =
629 "precision highp float;\n"
630 "varying float v;\n"
631 "void main() {\n"
632 " gl_FragColor = vec4(v, 0.0, 0.0, 1.0);\n"
633 "}\n";
634
Jamie Madill5599c8f2014-08-26 13:16:39 -0400635 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill4836d222014-07-24 06:55:51 -0400636 EXPECT_NE(0u, program);
637}
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400638
Jamie Madillfa05f602015-05-07 13:47:11 -0400639TEST_P(GLSLTest, FrontFacingAndVarying)
Jamie Madille6256f82014-09-17 10:31:15 -0400640{
Geoff Langdd323e92015-06-09 15:16:31 -0400641 EGLPlatformParameters platform = GetParam().eglParameters;
Austin Kinross8b695ee2015-03-12 13:12:20 -0700642
Jamie Madille6256f82014-09-17 10:31:15 -0400643 const std::string vertexShaderSource = SHADER_SOURCE
644 (
645 attribute vec4 a_position;
646 varying float v_varying;
647 void main()
648 {
649 v_varying = a_position.x;
650 gl_Position = a_position;
651 }
652 );
653
654 const std::string fragmentShaderSource = SHADER_SOURCE
655 (
656 precision mediump float;
657 varying float v_varying;
658 void main()
659 {
660 vec4 c;
661
662 if (gl_FrontFacing)
663 {
664 c = vec4(v_varying, 0, 0, 1.0);
665 }
666 else
667 {
668 c = vec4(0, v_varying, 0, 1.0);
669 }
670 gl_FragColor = c;
671 }
672 );
673
674 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Austin Kinross02df7962015-07-01 10:03:42 -0700675
676 // Compilation should fail on D3D11 feature level 9_3, since gl_FrontFacing isn't supported.
677 if (platform.renderer == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
678 {
679 if (platform.majorVersion == 9 && platform.minorVersion == 3)
680 {
681 EXPECT_EQ(0u, program);
682 return;
683 }
684 }
685
686 // Otherwise, compilation should succeed
Jamie Madille6256f82014-09-17 10:31:15 -0400687 EXPECT_NE(0u, program);
688}
689
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400690// Verify that linking shaders declaring different shading language versions fails.
691TEST_P(GLSLTest_ES3, VersionMismatch)
692{
693 const std::string fragmentShaderSource100 =
694 "precision mediump float;\n"
695 "varying float v_varying;\n"
696 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
697
698 const std::string vertexShaderSource100 =
699 "attribute vec4 a_position;\n"
700 "varying float v_varying;\n"
701 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
702
703 const std::string fragmentShaderSource300 =
704 "#version 300 es\n"
705 "precision mediump float;\n"
706 "in float v_varying;\n"
707 "out vec4 my_FragColor;\n"
708 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
709
710 const std::string vertexShaderSource300 =
711 "#version 300 es\n"
712 "in vec4 a_position;\n"
713 "out float v_varying;\n"
714 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
715
716 GLuint program = CompileProgram(vertexShaderSource300, fragmentShaderSource100);
717 EXPECT_EQ(0u, program);
718
719 program = CompileProgram(vertexShaderSource100, fragmentShaderSource300);
720 EXPECT_EQ(0u, program);
721}
722
723// Verify that declaring varying as invariant only in vertex shader fails in ESSL 1.00.
724TEST_P(GLSLTest, InvariantVaryingOut)
725{
726 const std::string fragmentShaderSource =
727 "precision mediump float;\n"
728 "varying float v_varying;\n"
729 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
730
731 const std::string vertexShaderSource =
732 "attribute vec4 a_position;\n"
733 "invariant varying float v_varying;\n"
734 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
735
736 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
737 EXPECT_EQ(0u, program);
738}
739
740// Verify that declaring varying as invariant only in vertex shader succeeds in ESSL 3.00.
741TEST_P(GLSLTest_ES3, InvariantVaryingOut)
742{
743 // TODO: ESSL 3.00 -> GLSL 1.20 translation should add "invariant" in fragment shader
744 // for varyings which are invariant in vertex shader (http://anglebug.com/1293)
Corentin Wallezc7f59d02016-06-20 10:12:08 -0400745 if (IsDesktopOpenGL())
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400746 {
747 std::cout << "Test disabled on OpenGL." << std::endl;
748 return;
749 }
750
751 const std::string fragmentShaderSource =
752 "#version 300 es\n"
753 "precision mediump float;\n"
754 "in float v_varying;\n"
755 "out vec4 my_FragColor;\n"
756 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
757
758 const std::string vertexShaderSource =
759 "#version 300 es\n"
760 "in vec4 a_position;\n"
761 "invariant out float v_varying;\n"
762 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
763
764 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
765 EXPECT_NE(0u, program);
766}
767
768// Verify that declaring varying as invariant only in fragment shader fails in ESSL 1.00.
Jamie Madillfa05f602015-05-07 13:47:11 -0400769TEST_P(GLSLTest, InvariantVaryingIn)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400770{
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400771 const std::string fragmentShaderSource =
772 "precision mediump float;\n"
773 "invariant varying float v_varying;\n"
774 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
Geoff Lange0cc2a42016-01-20 10:58:17 -0500775
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400776 const std::string vertexShaderSource =
777 "attribute vec4 a_position;\n"
778 "varying float v_varying;\n"
779 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400780
Jamie Madill5599c8f2014-08-26 13:16:39 -0400781 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400782 EXPECT_EQ(0u, program);
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400783}
784
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400785// Verify that declaring varying as invariant only in fragment shader fails in ESSL 3.00.
786TEST_P(GLSLTest_ES3, InvariantVaryingIn)
787{
788 const std::string fragmentShaderSource =
789 "#version 300 es\n"
790 "precision mediump float;\n"
791 "invariant in float v_varying;\n"
792 "out vec4 my_FragColor;\n"
793 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
794
795 const std::string vertexShaderSource =
796 "#version 300 es\n"
797 "in vec4 a_position;\n"
798 "out float v_varying;\n"
799 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
800
801 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
802 EXPECT_EQ(0u, program);
803}
804
805// Verify that declaring varying as invariant in both shaders succeeds in ESSL 1.00.
Jamie Madillfa05f602015-05-07 13:47:11 -0400806TEST_P(GLSLTest, InvariantVaryingBoth)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400807{
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400808 const std::string fragmentShaderSource =
809 "precision mediump float;\n"
810 "invariant varying float v_varying;\n"
811 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400812
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400813 const std::string vertexShaderSource =
814 "attribute vec4 a_position;\n"
815 "invariant varying float v_varying;\n"
816 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400817
Jamie Madill5599c8f2014-08-26 13:16:39 -0400818 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400819 EXPECT_NE(0u, program);
820}
821
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400822// Verify that declaring varying as invariant in both shaders fails in ESSL 3.00.
823TEST_P(GLSLTest_ES3, InvariantVaryingBoth)
824{
825 const std::string fragmentShaderSource =
826 "#version 300 es\n"
827 "precision mediump float;\n"
828 "invariant in float v_varying;\n"
829 "out vec4 my_FragColor;\n"
830 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
831
832 const std::string vertexShaderSource =
833 "#version 300 es\n"
834 "in vec4 a_position;\n"
835 "invariant out float v_varying;\n"
836 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
837
838 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
839 EXPECT_EQ(0u, program);
840}
841
842// Verify that declaring gl_Position as invariant succeeds in ESSL 1.00.
Jamie Madillfa05f602015-05-07 13:47:11 -0400843TEST_P(GLSLTest, InvariantGLPosition)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400844{
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400845 const std::string fragmentShaderSource =
846 "precision mediump float;\n"
847 "varying float v_varying;\n"
848 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400849
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400850 const std::string vertexShaderSource =
851 "attribute vec4 a_position;\n"
852 "invariant gl_Position;\n"
853 "varying float v_varying;\n"
854 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400855
Jamie Madill5599c8f2014-08-26 13:16:39 -0400856 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400857 EXPECT_NE(0u, program);
858}
859
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400860// Verify that declaring gl_Position as invariant succeeds in ESSL 3.00.
861TEST_P(GLSLTest_ES3, InvariantGLPosition)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400862{
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400863 const std::string fragmentShaderSource =
864 "#version 300 es\n"
865 "precision mediump float;\n"
866 "in float v_varying;\n"
867 "out vec4 my_FragColor;\n"
868 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
869
870 const std::string vertexShaderSource =
871 "#version 300 es\n"
872 "in vec4 a_position;\n"
873 "invariant gl_Position;\n"
874 "out float v_varying;\n"
875 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
876
877 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
878 EXPECT_NE(0u, program);
879}
880
881// Verify that using invariant(all) in both shaders succeeds in ESSL 1.00.
882TEST_P(GLSLTest, InvariantAllBoth)
883{
884 // TODO: ESSL 1.00 -> GLSL 1.20 translation should add "invariant" in fragment shader
885 // for varyings which are invariant in vertex shader individually,
886 // and remove invariant(all) from fragment shader (http://anglebug.com/1293)
Corentin Wallezc7f59d02016-06-20 10:12:08 -0400887 if (IsDesktopOpenGL())
Geoff Lange0cc2a42016-01-20 10:58:17 -0500888 {
889 std::cout << "Test disabled on OpenGL." << std::endl;
890 return;
891 }
892
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400893 const std::string fragmentShaderSource =
894 "#pragma STDGL invariant(all)\n"
895 "precision mediump float;\n"
896 "varying float v_varying;\n"
897 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400898
899 const std::string vertexShaderSource =
900 "#pragma STDGL invariant(all)\n"
901 "attribute vec4 a_position;\n"
902 "varying float v_varying;\n"
903 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
904
Jamie Madill5599c8f2014-08-26 13:16:39 -0400905 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400906 EXPECT_NE(0u, program);
907}
Austin Kinrossaf875522014-08-25 21:06:07 -0700908
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400909// Verify that using invariant(all) in both shaders fails in ESSL 3.00.
910TEST_P(GLSLTest_ES3, InvariantAllBoth)
911{
912 const std::string fragmentShaderSource =
913 "#version 300 es\n"
914 "#pragma STDGL invariant(all)\n"
915 "precision mediump float;\n"
916 "in float v_varying;\n"
917 "out vec4 my_FragColor;\n"
918 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
919
920 const std::string vertexShaderSource =
921 "#version 300 es\n"
922 "#pragma STDGL invariant(all)\n"
923 "in vec4 a_position;\n"
924 "out float v_varying;\n"
925 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
926
927 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
928 EXPECT_EQ(0u, program);
929}
930
931// Verify that using invariant(all) only in fragment shader fails in ESSL 1.00.
932TEST_P(GLSLTest, InvariantAllIn)
933{
934 const std::string fragmentShaderSource =
935 "#pragma STDGL invariant(all)\n"
936 "precision mediump float;\n"
937 "varying float v_varying;\n"
938 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
939
940 const std::string vertexShaderSource =
941 "attribute vec4 a_position;\n"
942 "varying float v_varying;\n"
943 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
944
945 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
946 EXPECT_EQ(0u, program);
947}
948
949// Verify that using invariant(all) only in fragment shader fails in ESSL 3.00.
950TEST_P(GLSLTest_ES3, InvariantAllIn)
951{
952 const std::string fragmentShaderSource =
953 "#version 300 es\n"
954 "#pragma STDGL invariant(all)\n"
955 "precision mediump float;\n"
956 "in float v_varying;\n"
957 "out vec4 my_FragColor;\n"
958 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
959
960 const std::string vertexShaderSource =
961 "#version 300 es\n"
962 "in vec4 a_position;\n"
963 "out float v_varying;\n"
964 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
965
966 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
967 EXPECT_EQ(0u, program);
968}
969
970// Verify that using invariant(all) only in vertex shader fails in ESSL 1.00.
971TEST_P(GLSLTest, InvariantAllOut)
972{
973 const std::string fragmentShaderSource =
974 "precision mediump float;\n"
975 "varying float v_varying;\n"
976 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
977
978 const std::string vertexShaderSource =
979 "#pragma STDGL invariant(all)\n"
980 "attribute vec4 a_position;\n"
981 "varying float v_varying;\n"
982 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
983
984 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
985 EXPECT_EQ(0u, program);
986}
987
988// Verify that using invariant(all) only in vertex shader succeeds in ESSL 3.00.
989TEST_P(GLSLTest_ES3, InvariantAllOut)
990{
991 // TODO: ESSL 3.00 -> GLSL 1.20 translation should add "invariant" in fragment shader
992 // for varyings which are invariant in vertex shader,
993 // because of invariant(all) being used in vertex shader (http://anglebug.com/1293)
Corentin Wallezc7f59d02016-06-20 10:12:08 -0400994 if (IsDesktopOpenGL())
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400995 {
996 std::cout << "Test disabled on OpenGL." << std::endl;
997 return;
998 }
999
1000 const std::string fragmentShaderSource =
1001 "#version 300 es\n"
1002 "precision mediump float;\n"
1003 "in float v_varying;\n"
1004 "out vec4 my_FragColor;\n"
1005 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
1006
1007 const std::string vertexShaderSource =
1008 "#version 300 es\n"
1009 "#pragma STDGL invariant(all)\n"
1010 "in vec4 a_position;\n"
1011 "out float v_varying;\n"
1012 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
1013
1014 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1015 EXPECT_NE(0u, program);
1016}
1017
Jamie Madillfa05f602015-05-07 13:47:11 -04001018TEST_P(GLSLTest, MaxVaryingVec4)
Austin Kinross8b695ee2015-03-12 13:12:20 -07001019{
Geoff Lang69accbd2016-01-25 16:22:32 -05001020#if defined(__APPLE__)
1021 // TODO(geofflang): Find out why this doesn't compile on Apple AND OpenGL drivers
1022 // (http://anglebug.com/1291)
Jamie Madill518b9fa2016-03-02 11:26:02 -05001023 if (IsAMD() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
Geoff Lang69accbd2016-01-25 16:22:32 -05001024 {
1025 std::cout << "Test disabled on Apple AMD OpenGL." << std::endl;
1026 return;
1027 }
1028#endif
1029
Austin Kinross8b695ee2015-03-12 13:12:20 -07001030 GLint maxVaryings = 0;
1031 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1032
1033 VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings, 0, false, false, false, true);
1034}
1035
Jamie Madillfa05f602015-05-07 13:47:11 -04001036TEST_P(GLSLTest, MaxMinusTwoVaryingVec4PlusTwoSpecialVariables)
Austin Kinross8b695ee2015-03-12 13:12:20 -07001037{
1038 GLint maxVaryings = 0;
1039 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1040
1041 // Generate shader code that uses gl_FragCoord and gl_PointCoord, two special fragment shader variables.
1042 VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings - 2, 0, true, true, false, true);
1043}
1044
Jamie Madillfa05f602015-05-07 13:47:11 -04001045TEST_P(GLSLTest, MaxMinusTwoVaryingVec4PlusThreeSpecialVariables)
Austin Kinross8b695ee2015-03-12 13:12:20 -07001046{
Geoff Lange0cc2a42016-01-20 10:58:17 -05001047 // TODO(geofflang): Figure out why this fails on OpenGL AMD (http://anglebug.com/1291)
Jamie Madill518b9fa2016-03-02 11:26:02 -05001048 if (IsAMD() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
Geoff Lange0cc2a42016-01-20 10:58:17 -05001049 {
1050 std::cout << "Test disabled on OpenGL." << std::endl;
1051 return;
1052 }
1053
Austin Kinross8b695ee2015-03-12 13:12:20 -07001054 GLint maxVaryings = 0;
1055 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1056
1057 // Generate shader code that uses gl_FragCoord, gl_PointCoord and gl_PointSize.
1058 VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings - 2, 0, true, true, true, true);
1059}
1060
Geoff Lange0cc2a42016-01-20 10:58:17 -05001061// Disabled because drivers are allowed to successfully compile shaders that have more than the
1062// maximum number of varyings. (http://anglebug.com/1296)
1063TEST_P(GLSLTest, DISABLED_MaxVaryingVec4PlusFragCoord)
Austin Kinross8b695ee2015-03-12 13:12:20 -07001064{
1065 GLint maxVaryings = 0;
1066 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1067
1068 // Generate shader code that uses gl_FragCoord, a special fragment shader variables.
1069 // This test should fail, since we are really using (maxVaryings + 1) varyings.
1070 VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings, 0, true, false, false, false);
1071}
1072
Geoff Lange0cc2a42016-01-20 10:58:17 -05001073// Disabled because drivers are allowed to successfully compile shaders that have more than the
1074// maximum number of varyings. (http://anglebug.com/1296)
1075TEST_P(GLSLTest, DISABLED_MaxVaryingVec4PlusPointCoord)
Austin Kinross8b695ee2015-03-12 13:12:20 -07001076{
1077 GLint maxVaryings = 0;
1078 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1079
1080 // Generate shader code that uses gl_FragCoord, a special fragment shader variables.
1081 // This test should fail, since we are really using (maxVaryings + 1) varyings.
1082 VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings, 0, false, true, false, false);
1083}
1084
Jamie Madillfa05f602015-05-07 13:47:11 -04001085TEST_P(GLSLTest, MaxVaryingVec3)
Austin Kinrossaf875522014-08-25 21:06:07 -07001086{
1087 GLint maxVaryings = 0;
1088 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1089
Austin Kinross8b695ee2015-03-12 13:12:20 -07001090 VaryingTestBase(0, 0, 0, 0, maxVaryings, 0, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001091}
1092
Jamie Madillfa05f602015-05-07 13:47:11 -04001093TEST_P(GLSLTest, MaxVaryingVec3Array)
Austin Kinrossaf875522014-08-25 21:06:07 -07001094{
1095 GLint maxVaryings = 0;
1096 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1097
Austin Kinross8b695ee2015-03-12 13:12:20 -07001098 VaryingTestBase(0, 0, 0, 0, 0, maxVaryings / 2, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001099}
1100
Jamie Madillbee59e02014-10-02 10:44:18 -04001101// Disabled because of a failure in D3D9
Jamie Madill9fc36822015-11-18 13:08:07 -05001102TEST_P(GLSLTest, MaxVaryingVec3AndOneFloat)
Austin Kinrossaf875522014-08-25 21:06:07 -07001103{
Jamie Madill518b9fa2016-03-02 11:26:02 -05001104 if (IsD3D9())
Jamie Madill9fc36822015-11-18 13:08:07 -05001105 {
1106 std::cout << "Test disabled on D3D9." << std::endl;
1107 return;
1108 }
1109
Austin Kinrossaf875522014-08-25 21:06:07 -07001110 GLint maxVaryings = 0;
1111 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1112
Austin Kinross8b695ee2015-03-12 13:12:20 -07001113 VaryingTestBase(1, 0, 0, 0, maxVaryings, 0, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001114}
1115
Jamie Madillbee59e02014-10-02 10:44:18 -04001116// Disabled because of a failure in D3D9
Jamie Madill9fc36822015-11-18 13:08:07 -05001117TEST_P(GLSLTest, MaxVaryingVec3ArrayAndOneFloatArray)
Austin Kinrossaf875522014-08-25 21:06:07 -07001118{
Jamie Madill518b9fa2016-03-02 11:26:02 -05001119 if (IsD3D9())
Jamie Madill9fc36822015-11-18 13:08:07 -05001120 {
1121 std::cout << "Test disabled on D3D9." << std::endl;
1122 return;
1123 }
1124
Austin Kinrossaf875522014-08-25 21:06:07 -07001125 GLint maxVaryings = 0;
1126 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1127
Austin Kinross8b695ee2015-03-12 13:12:20 -07001128 VaryingTestBase(0, 1, 0, 0, 0, maxVaryings / 2, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001129}
1130
Jamie Madillbee59e02014-10-02 10:44:18 -04001131// Disabled because of a failure in D3D9
Jamie Madill9fc36822015-11-18 13:08:07 -05001132TEST_P(GLSLTest, TwiceMaxVaryingVec2)
Austin Kinrossaf875522014-08-25 21:06:07 -07001133{
Jamie Madill518b9fa2016-03-02 11:26:02 -05001134 if (IsD3D9())
Jamie Madill9fc36822015-11-18 13:08:07 -05001135 {
1136 std::cout << "Test disabled on D3D9." << std::endl;
1137 return;
1138 }
1139
Geoff Lange0cc2a42016-01-20 10:58:17 -05001140 if (getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)
1141 {
1142 // TODO(geofflang): Figure out why this fails on NVIDIA's GLES driver
1143 std::cout << "Test disabled on OpenGL ES." << std::endl;
1144 return;
1145 }
1146
Geoff Lang69accbd2016-01-25 16:22:32 -05001147#if defined(__APPLE__)
1148 // TODO(geofflang): Find out why this doesn't compile on Apple AND OpenGL drivers
1149 // (http://anglebug.com/1291)
Jamie Madill518b9fa2016-03-02 11:26:02 -05001150 if (IsAMD() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
Geoff Lang69accbd2016-01-25 16:22:32 -05001151 {
1152 std::cout << "Test disabled on Apple AMD OpenGL." << std::endl;
1153 return;
1154 }
1155#endif
1156
Austin Kinrossaf875522014-08-25 21:06:07 -07001157 GLint maxVaryings = 0;
1158 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1159
Austin Kinross8b695ee2015-03-12 13:12:20 -07001160 VaryingTestBase(0, 0, 2 * maxVaryings, 0, 0, 0, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001161}
1162
Jamie Madillbee59e02014-10-02 10:44:18 -04001163// Disabled because of a failure in D3D9
Jamie Madill9fc36822015-11-18 13:08:07 -05001164TEST_P(GLSLTest, MaxVaryingVec2Arrays)
Austin Kinrossaf875522014-08-25 21:06:07 -07001165{
Jamie Madill518b9fa2016-03-02 11:26:02 -05001166 if (IsD3DSM3())
Jamie Madill9fc36822015-11-18 13:08:07 -05001167 {
1168 std::cout << "Test disabled on SM3." << std::endl;
1169 return;
1170 }
1171
Geoff Lange0cc2a42016-01-20 10:58:17 -05001172 if (getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)
1173 {
1174 // TODO(geofflang): Figure out why this fails on NVIDIA's GLES driver
1175 std::cout << "Test disabled on OpenGL ES." << std::endl;
1176 return;
1177 }
1178
Geoff Lang69accbd2016-01-25 16:22:32 -05001179#if defined(__APPLE__)
1180 // TODO(geofflang): Find out why this doesn't compile on Apple AND OpenGL drivers
1181 // (http://anglebug.com/1291)
Jamie Madill518b9fa2016-03-02 11:26:02 -05001182 if (IsAMD() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
Geoff Lang69accbd2016-01-25 16:22:32 -05001183 {
1184 std::cout << "Test disabled on Apple AMD OpenGL." << std::endl;
1185 return;
1186 }
1187#endif
1188
Austin Kinrossaf875522014-08-25 21:06:07 -07001189 GLint maxVaryings = 0;
1190 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1191
Austin Kinross8b695ee2015-03-12 13:12:20 -07001192 VaryingTestBase(0, 0, 0, maxVaryings, 0, 0, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001193}
1194
Geoff Lange0cc2a42016-01-20 10:58:17 -05001195// Disabled because drivers are allowed to successfully compile shaders that have more than the
1196// maximum number of varyings. (http://anglebug.com/1296)
1197TEST_P(GLSLTest, DISABLED_MaxPlusOneVaryingVec3)
Austin Kinrossaf875522014-08-25 21:06:07 -07001198{
1199 GLint maxVaryings = 0;
1200 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1201
Austin Kinross8b695ee2015-03-12 13:12:20 -07001202 VaryingTestBase(0, 0, 0, 0, maxVaryings + 1, 0, 0, 0, false, false, false, false);
Austin Kinrossaf875522014-08-25 21:06:07 -07001203}
1204
Geoff Lange0cc2a42016-01-20 10:58:17 -05001205// Disabled because drivers are allowed to successfully compile shaders that have more than the
1206// maximum number of varyings. (http://anglebug.com/1296)
1207TEST_P(GLSLTest, DISABLED_MaxPlusOneVaryingVec3Array)
Austin Kinrossaf875522014-08-25 21:06:07 -07001208{
1209 GLint maxVaryings = 0;
1210 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1211
Austin Kinross8b695ee2015-03-12 13:12:20 -07001212 VaryingTestBase(0, 0, 0, 0, 0, maxVaryings / 2 + 1, 0, 0, false, false, false, false);
Austin Kinrossaf875522014-08-25 21:06:07 -07001213}
1214
Geoff Lange0cc2a42016-01-20 10:58:17 -05001215// Disabled because drivers are allowed to successfully compile shaders that have more than the
1216// maximum number of varyings. (http://anglebug.com/1296)
1217TEST_P(GLSLTest, DISABLED_MaxVaryingVec3AndOneVec2)
Austin Kinrossaf875522014-08-25 21:06:07 -07001218{
1219 GLint maxVaryings = 0;
1220 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1221
Austin Kinross8b695ee2015-03-12 13:12:20 -07001222 VaryingTestBase(0, 0, 1, 0, maxVaryings, 0, 0, 0, false, false, false, false);
Austin Kinrossaf875522014-08-25 21:06:07 -07001223}
1224
Geoff Lange0cc2a42016-01-20 10:58:17 -05001225// Disabled because drivers are allowed to successfully compile shaders that have more than the
1226// maximum number of varyings. (http://anglebug.com/1296)
1227TEST_P(GLSLTest, DISABLED_MaxPlusOneVaryingVec2)
Austin Kinrossaf875522014-08-25 21:06:07 -07001228{
1229 GLint maxVaryings = 0;
1230 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1231
Austin Kinross8b695ee2015-03-12 13:12:20 -07001232 VaryingTestBase(0, 0, 2 * maxVaryings + 1, 0, 0, 0, 0, 0, false, false, false, false);
Austin Kinrossaf875522014-08-25 21:06:07 -07001233}
1234
Geoff Lange0cc2a42016-01-20 10:58:17 -05001235// Disabled because drivers are allowed to successfully compile shaders that have more than the
1236// maximum number of varyings. (http://anglebug.com/1296)
1237TEST_P(GLSLTest, DISABLED_MaxVaryingVec3ArrayAndMaxPlusOneFloatArray)
Austin Kinrossaf875522014-08-25 21:06:07 -07001238{
1239 GLint maxVaryings = 0;
1240 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1241
Austin Kinross8b695ee2015-03-12 13:12:20 -07001242 VaryingTestBase(0, maxVaryings / 2 + 1, 0, 0, 0, 0, 0, maxVaryings / 2, false, false, false, false);
Austin Kinrossaf875522014-08-25 21:06:07 -07001243}
Geoff Langf60fab62014-11-24 11:21:20 -05001244
1245// Verify shader source with a fixed length that is less than the null-terminated length will compile.
Jamie Madillfa05f602015-05-07 13:47:11 -04001246TEST_P(GLSLTest, FixedShaderLength)
Geoff Langf60fab62014-11-24 11:21:20 -05001247{
1248 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1249
1250 const std::string appendGarbage = "abcasdfasdfasdfasdfasdf";
1251 const std::string source = "void main() { gl_FragColor = vec4(0, 0, 0, 0); }" + appendGarbage;
1252 const char *sourceArray[1] = { source.c_str() };
Corentin Wallez973402f2015-05-11 13:42:22 -04001253 GLint lengths[1] = { static_cast<GLint>(source.length() - appendGarbage.length()) };
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001254 glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
Geoff Langf60fab62014-11-24 11:21:20 -05001255 glCompileShader(shader);
1256
1257 GLint compileResult;
1258 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1259 EXPECT_NE(compileResult, 0);
1260}
1261
1262// Verify that a negative shader source length is treated as a null-terminated length.
Jamie Madillfa05f602015-05-07 13:47:11 -04001263TEST_P(GLSLTest, NegativeShaderLength)
Geoff Langf60fab62014-11-24 11:21:20 -05001264{
1265 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1266
1267 const char *sourceArray[1] = { "void main() { gl_FragColor = vec4(0, 0, 0, 0); }" };
1268 GLint lengths[1] = { -10 };
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001269 glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
Geoff Langf60fab62014-11-24 11:21:20 -05001270 glCompileShader(shader);
1271
1272 GLint compileResult;
1273 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1274 EXPECT_NE(compileResult, 0);
1275}
1276
Corentin Wallez9a9c0482016-04-12 10:36:25 -04001277// Check that having an invalid char after the "." doesn't cause an assert.
1278TEST_P(GLSLTest, InvalidFieldFirstChar)
1279{
1280 GLuint shader = glCreateShader(GL_VERTEX_SHADER);
1281 const char *source = "void main() {vec4 x; x.}";
1282 glShaderSource(shader, 1, &source, 0);
1283 glCompileShader(shader);
1284
1285 GLint compileResult;
1286 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1287 EXPECT_EQ(0, compileResult);
1288}
1289
Geoff Langf60fab62014-11-24 11:21:20 -05001290// Verify that a length array with mixed positive and negative values compiles.
Jamie Madillfa05f602015-05-07 13:47:11 -04001291TEST_P(GLSLTest, MixedShaderLengths)
Geoff Langf60fab62014-11-24 11:21:20 -05001292{
1293 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1294
1295 const char *sourceArray[] =
1296 {
1297 "void main()",
1298 "{",
1299 " gl_FragColor = vec4(0, 0, 0, 0);",
1300 "}",
1301 };
1302 GLint lengths[] =
1303 {
1304 -10,
1305 1,
Corentin Wallez973402f2015-05-11 13:42:22 -04001306 static_cast<GLint>(strlen(sourceArray[2])),
Geoff Langf60fab62014-11-24 11:21:20 -05001307 -1,
1308 };
1309 ASSERT_EQ(ArraySize(sourceArray), ArraySize(lengths));
1310
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001311 glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
Geoff Langf60fab62014-11-24 11:21:20 -05001312 glCompileShader(shader);
1313
1314 GLint compileResult;
1315 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1316 EXPECT_NE(compileResult, 0);
1317}
1318
1319// Verify that zero-length shader source does not affect shader compilation.
Jamie Madillfa05f602015-05-07 13:47:11 -04001320TEST_P(GLSLTest, ZeroShaderLength)
Geoff Langf60fab62014-11-24 11:21:20 -05001321{
1322 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1323
1324 const char *sourceArray[] =
1325 {
1326 "adfasdf",
1327 "34534",
1328 "void main() { gl_FragColor = vec4(0, 0, 0, 0); }",
1329 "",
1330 "asdfasdfsdsdf",
1331 };
1332 GLint lengths[] =
1333 {
1334 0,
1335 0,
1336 -1,
1337 0,
1338 0,
1339 };
1340 ASSERT_EQ(ArraySize(sourceArray), ArraySize(lengths));
1341
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001342 glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
Geoff Langf60fab62014-11-24 11:21:20 -05001343 glCompileShader(shader);
1344
1345 GLint compileResult;
1346 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1347 EXPECT_NE(compileResult, 0);
1348}
Jamie Madill21c1e452014-12-29 11:33:41 -05001349
1350// Tests that bad index expressions don't crash ANGLE's translator.
1351// https://code.google.com/p/angleproject/issues/detail?id=857
Jamie Madillfa05f602015-05-07 13:47:11 -04001352TEST_P(GLSLTest, BadIndexBug)
Jamie Madill21c1e452014-12-29 11:33:41 -05001353{
1354 const std::string &fragmentShaderSourceVec =
1355 "precision mediump float;\n"
1356 "uniform vec4 uniformVec;\n"
1357 "void main()\n"
1358 "{\n"
1359 " gl_FragColor = vec4(uniformVec[int()]);\n"
1360 "}";
1361
1362 GLuint shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceVec);
1363 EXPECT_EQ(0u, shader);
1364
1365 if (shader != 0)
1366 {
1367 glDeleteShader(shader);
1368 }
1369
1370 const std::string &fragmentShaderSourceMat =
1371 "precision mediump float;\n"
1372 "uniform mat4 uniformMat;\n"
1373 "void main()\n"
1374 "{\n"
1375 " gl_FragColor = vec4(uniformMat[int()]);\n"
1376 "}";
1377
1378 shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceMat);
1379 EXPECT_EQ(0u, shader);
1380
1381 if (shader != 0)
1382 {
1383 glDeleteShader(shader);
1384 }
1385
1386 const std::string &fragmentShaderSourceArray =
1387 "precision mediump float;\n"
1388 "uniform vec4 uniformArray;\n"
1389 "void main()\n"
1390 "{\n"
1391 " gl_FragColor = vec4(uniformArray[int()]);\n"
1392 "}";
1393
1394 shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceArray);
1395 EXPECT_EQ(0u, shader);
1396
1397 if (shader != 0)
1398 {
1399 glDeleteShader(shader);
1400 }
Jamie Madill37997142015-01-28 10:06:34 -05001401}
1402
Jamie Madill2e295e22015-04-29 10:41:33 -04001403// Test that structs defined in uniforms are translated correctly.
Jamie Madillfa05f602015-05-07 13:47:11 -04001404TEST_P(GLSLTest, StructSpecifiersUniforms)
Jamie Madill2e295e22015-04-29 10:41:33 -04001405{
1406 const std::string fragmentShaderSource = SHADER_SOURCE
1407 (
1408 precision mediump float;
1409
1410 uniform struct S { float field;} s;
1411
1412 void main()
1413 {
1414 gl_FragColor = vec4(1, 0, 0, 1);
1415 gl_FragColor.a += s.field;
1416 }
1417 );
1418
1419 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
1420 EXPECT_NE(0u, program);
1421}
Jamie Madill55def582015-05-04 11:24:57 -04001422
1423// Test that gl_DepthRange is not stored as a uniform location. Since uniforms
1424// beginning with "gl_" are filtered out by our validation logic, we must
1425// bypass the validation to test the behaviour of the implementation.
1426// (note this test is still Impl-independent)
Jamie Madillfa05f602015-05-07 13:47:11 -04001427TEST_P(GLSLTest, DepthRangeUniforms)
Jamie Madill55def582015-05-04 11:24:57 -04001428{
1429 const std::string fragmentShaderSource = SHADER_SOURCE
1430 (
1431 precision mediump float;
1432
1433 void main()
1434 {
1435 gl_FragColor = vec4(gl_DepthRange.near, gl_DepthRange.far, gl_DepthRange.diff, 1);
1436 }
1437 );
1438
1439 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
1440 EXPECT_NE(0u, program);
1441
1442 // dive into the ANGLE internals, so we can bypass validation.
1443 gl::Context *context = reinterpret_cast<gl::Context *>(getEGLWindow()->getContext());
1444 gl::Program *glProgram = context->getProgram(program);
1445 GLint nearIndex = glProgram->getUniformLocation("gl_DepthRange.near");
1446 EXPECT_EQ(-1, nearIndex);
1447
1448 // Test drawing does not throw an exception.
1449 drawQuad(program, "inputAttribute", 0.5f);
1450
1451 EXPECT_GL_NO_ERROR();
1452
1453 glDeleteProgram(program);
1454}
Jamie Madill4052dfc2015-05-06 15:18:49 -04001455
1456// Covers the WebGL test 'glsl/bugs/pow-of-small-constant-in-user-defined-function'
1457// See https://code.google.com/p/angleproject/issues/detail?id=851
1458// TODO(jmadill): ANGLE constant folding can fix this
Jamie Madillfa05f602015-05-07 13:47:11 -04001459TEST_P(GLSLTest, DISABLED_PowOfSmallConstant)
Jamie Madill4052dfc2015-05-06 15:18:49 -04001460{
1461 const std::string &fragmentShaderSource = SHADER_SOURCE
1462 (
1463 precision highp float;
1464
1465 float fun(float arg)
1466 {
1467 // These values are still easily within the highp range.
1468 // The minimum range in terms of 10's exponent is around -19 to 19, and IEEE-754 single precision range is higher than that.
1469 return pow(arg, 2.0);
1470 }
1471
1472 void main()
1473 {
1474 // Note that the bug did not reproduce if an uniform was passed to the function instead of a constant,
1475 // or if the expression was moved outside the user-defined function.
1476 const float a = 1.0e-6;
1477 float b = 1.0e12 * fun(a);
1478 if (abs(b - 1.0) < 0.01)
1479 {
1480 gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); // green
1481 }
1482 else
1483 {
1484 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // red
1485 }
1486 }
1487 );
1488
1489 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
1490 EXPECT_NE(0u, program);
1491
1492 drawQuad(program, "inputAttribute", 0.5f);
1493 EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);
1494 EXPECT_GL_NO_ERROR();
1495}
Jamie Madillfa05f602015-05-07 13:47:11 -04001496
Cooper Partina5ef8d82015-08-19 14:52:21 -07001497// Test that fragment shaders which contain non-constant loop indexers and compiled for FL9_3 and
1498// below
1499// fail with a specific error message.
1500// Additionally test that the same fragment shader compiles successfully with feature levels greater
1501// than FL9_3.
1502TEST_P(GLSLTest, LoopIndexingValidation)
1503{
1504 const std::string fragmentShaderSource = SHADER_SOURCE
1505 (
1506 precision mediump float;
1507
1508 uniform float loopMax;
1509
1510 void main()
1511 {
1512 gl_FragColor = vec4(1, 0, 0, 1);
1513 for (float l = 0.0; l < loopMax; l++)
1514 {
1515 if (loopMax > 3.0)
1516 {
1517 gl_FragColor.a += 0.1;
1518 }
1519 }
1520 }
1521 );
1522
1523 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1524
1525 const char *sourceArray[1] = {fragmentShaderSource.c_str()};
1526 glShaderSource(shader, 1, sourceArray, nullptr);
1527 glCompileShader(shader);
1528
1529 GLint compileResult;
1530 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1531
1532 // If the test is configured to run limited to Feature Level 9_3, then it is
1533 // assumed that shader compilation will fail with an expected error message containing
1534 // "Loop index cannot be compared with non-constant expression"
Olli Etuaho814a54d2015-08-27 16:23:09 +03001535 if ((GetParam() == ES2_D3D11_FL9_3() || GetParam() == ES2_D3D9()))
Cooper Partina5ef8d82015-08-19 14:52:21 -07001536 {
1537 if (compileResult != 0)
1538 {
1539 FAIL() << "Shader compilation succeeded, expected failure";
1540 }
1541 else
1542 {
1543 GLint infoLogLength;
1544 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
1545
1546 std::string infoLog;
1547 infoLog.resize(infoLogLength);
1548 glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]);
1549
1550 if (infoLog.find("Loop index cannot be compared with non-constant expression") ==
1551 std::string::npos)
1552 {
1553 FAIL() << "Shader compilation failed with unexpected error message";
1554 }
1555 }
1556 }
1557 else
1558 {
1559 EXPECT_NE(0, compileResult);
1560 }
1561
1562 if (shader != 0)
1563 {
1564 glDeleteShader(shader);
1565 }
1566}
1567
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001568// Tests that the maximum uniforms count returned from querying GL_MAX_VERTEX_UNIFORM_VECTORS
1569// can actually be used.
1570TEST_P(GLSLTest, VerifyMaxVertexUniformVectors)
1571{
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001572 int maxUniforms = 10000;
1573 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
1574 EXPECT_GL_NO_ERROR();
1575 std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS = " << maxUniforms << std::endl;
1576
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001577 CompileGLSLWithUniformsAndSamplers(maxUniforms, 0, 0, 0, true);
1578}
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001579
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001580// Tests that the maximum uniforms count returned from querying GL_MAX_VERTEX_UNIFORM_VECTORS
1581// can actually be used along with the maximum number of texture samplers.
1582TEST_P(GLSLTest, VerifyMaxVertexUniformVectorsWithSamplers)
1583{
Geoff Lange0cc2a42016-01-20 10:58:17 -05001584 if (GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE ||
1585 GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)
1586 {
1587 std::cout << "Test disabled on OpenGL." << std::endl;
1588 return;
1589 }
1590
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001591 int maxUniforms = 10000;
1592 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
1593 EXPECT_GL_NO_ERROR();
1594 std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS = " << maxUniforms << std::endl;
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001595
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001596 int maxTextureImageUnits = 0;
1597 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001598
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001599 CompileGLSLWithUniformsAndSamplers(maxUniforms, 0, maxTextureImageUnits, 0, true);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001600}
1601
1602// Tests that the maximum uniforms count + 1 from querying GL_MAX_VERTEX_UNIFORM_VECTORS
1603// fails shader compilation.
1604TEST_P(GLSLTest, VerifyMaxVertexUniformVectorsExceeded)
1605{
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001606 int maxUniforms = 10000;
1607 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
1608 EXPECT_GL_NO_ERROR();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001609 std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS + 1 = " << maxUniforms + 1 << std::endl;
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001610
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001611 CompileGLSLWithUniformsAndSamplers(maxUniforms + 1, 0, 0, 0, false);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001612}
1613
1614// Tests that the maximum uniforms count returned from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
1615// can actually be used.
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001616TEST_P(GLSLTest, VerifyMaxFragmentUniformVectors)
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001617{
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001618 int maxUniforms = 10000;
1619 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
1620 EXPECT_GL_NO_ERROR();
1621 std::cout << "Validating GL_MAX_FRAGMENT_UNIFORM_VECTORS = " << maxUniforms << std::endl;
1622
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001623 CompileGLSLWithUniformsAndSamplers(0, maxUniforms, 0, 0, true);
1624}
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001625
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001626// Tests that the maximum uniforms count returned from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
1627// can actually be used along with the maximum number of texture samplers.
1628TEST_P(GLSLTest, VerifyMaxFragmentUniformVectorsWithSamplers)
1629{
Geoff Lange0cc2a42016-01-20 10:58:17 -05001630 if (GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE ||
1631 GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)
1632 {
1633 std::cout << "Test disabled on OpenGL." << std::endl;
1634 return;
1635 }
1636
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001637 int maxUniforms = 10000;
1638 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
1639 EXPECT_GL_NO_ERROR();
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001640
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001641 int maxTextureImageUnits = 0;
1642 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001643
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001644 CompileGLSLWithUniformsAndSamplers(0, maxUniforms, 0, maxTextureImageUnits, true);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001645}
1646
1647// Tests that the maximum uniforms count + 1 from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
1648// fails shader compilation.
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001649TEST_P(GLSLTest, VerifyMaxFragmentUniformVectorsExceeded)
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001650{
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001651 int maxUniforms = 10000;
1652 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
1653 EXPECT_GL_NO_ERROR();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001654 std::cout << "Validating GL_MAX_FRAGMENT_UNIFORM_VECTORS + 1 = " << maxUniforms + 1
1655 << std::endl;
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001656
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001657 CompileGLSLWithUniformsAndSamplers(0, maxUniforms + 1, 0, 0, false);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001658}
1659
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001660// Test that two constructors which have vec4 and mat2 parameters get disambiguated (issue in
1661// HLSL).
1662TEST_P(GLSLTest_ES3, AmbiguousConstructorCall2x2)
1663{
1664 const std::string fragmentShaderSource =
1665 "#version 300 es\n"
1666 "precision highp float;\n"
1667 "out vec4 my_FragColor;\n"
1668 "void main()\n"
1669 "{\n"
1670 " my_FragColor = vec4(0.0);\n"
1671 "}";
1672
1673 const std::string vertexShaderSource =
1674 "#version 300 es\n"
1675 "precision highp float;\n"
1676 "in vec4 a_vec;\n"
1677 "in mat2 a_mat;\n"
1678 "void main()\n"
1679 "{\n"
1680 " gl_Position = vec4(a_vec) + vec4(a_mat);\n"
1681 "}";
1682
1683 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1684 EXPECT_NE(0u, program);
1685}
1686
1687// Test that two constructors which have mat2x3 and mat3x2 parameters get disambiguated.
1688// This was suspected to be an issue in HLSL, but HLSL seems to be able to natively choose between
1689// the function signatures in this case.
1690TEST_P(GLSLTest_ES3, AmbiguousConstructorCall2x3)
1691{
1692 const std::string fragmentShaderSource =
1693 "#version 300 es\n"
1694 "precision highp float;\n"
1695 "out vec4 my_FragColor;\n"
1696 "void main()\n"
1697 "{\n"
1698 " my_FragColor = vec4(0.0);\n"
1699 "}";
1700
1701 const std::string vertexShaderSource =
1702 "#version 300 es\n"
1703 "precision highp float;\n"
1704 "in mat3x2 a_matA;\n"
1705 "in mat2x3 a_matB;\n"
1706 "void main()\n"
1707 "{\n"
1708 " gl_Position = vec4(a_matA) + vec4(a_matB);\n"
1709 "}";
1710
1711 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1712 EXPECT_NE(0u, program);
1713}
1714
1715// Test that two functions which have vec4 and mat2 parameters get disambiguated (issue in HLSL).
1716TEST_P(GLSLTest_ES3, AmbiguousFunctionCall2x2)
1717{
1718 const std::string fragmentShaderSource =
1719 "#version 300 es\n"
1720 "precision highp float;\n"
1721 "out vec4 my_FragColor;\n"
1722 "void main()\n"
1723 "{\n"
1724 " my_FragColor = vec4(0.0);\n"
1725 "}";
1726
1727 const std::string vertexShaderSource =
1728 "#version 300 es\n"
1729 "precision highp float;\n"
1730 "in vec4 a_vec;\n"
1731 "in mat2 a_mat;\n"
1732 "vec4 foo(vec4 a)\n"
1733 "{\n"
1734 " return a;\n"
1735 "}\n"
1736 "vec4 foo(mat2 a)\n"
1737 "{\n"
1738 " return vec4(a[0][0]);\n"
1739 "}\n"
1740 "void main()\n"
1741 "{\n"
1742 " gl_Position = foo(a_vec) + foo(a_mat);\n"
1743 "}";
1744
1745 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1746 EXPECT_NE(0u, program);
1747}
1748
1749// Test that an user-defined function with a large number of float4 parameters doesn't fail due to
1750// the function name being too long.
1751TEST_P(GLSLTest_ES3, LargeNumberOfFloat4Parameters)
1752{
1753 const std::string fragmentShaderSource =
1754 "#version 300 es\n"
1755 "precision highp float;\n"
1756 "out vec4 my_FragColor;\n"
1757 "void main()\n"
1758 "{\n"
1759 " my_FragColor = vec4(0.0);\n"
1760 "}";
1761
1762 std::stringstream vertexShaderStream;
1763 const unsigned int paramCount = 1024u;
1764
1765 vertexShaderStream << "#version 300 es\n"
1766 "precision highp float;\n"
1767 "in vec4 a_vec;\n"
1768 "vec4 lotsOfVec4Parameters(";
1769 for (unsigned int i = 0; i < paramCount; ++i)
1770 {
1771 vertexShaderStream << "vec4 a" << i << ", ";
1772 }
1773 vertexShaderStream << "vec4 aLast)\n"
1774 "{\n"
1775 " return ";
1776 for (unsigned int i = 0; i < paramCount; ++i)
1777 {
1778 vertexShaderStream << "a" << i << " + ";
1779 }
1780 vertexShaderStream << "aLast;\n"
1781 "}\n"
1782 "void main()\n"
1783 "{\n"
1784 " gl_Position = lotsOfVec4Parameters(";
1785 for (unsigned int i = 0; i < paramCount; ++i)
1786 {
1787 vertexShaderStream << "a_vec, ";
1788 }
1789 vertexShaderStream << "a_vec);\n"
1790 "}";
1791
1792 GLuint program = CompileProgram(vertexShaderStream.str(), fragmentShaderSource);
1793 EXPECT_NE(0u, program);
1794}
1795
Olli Etuahod4f4c112016-04-15 15:11:24 +03001796// This test was written specifically to stress DeferGlobalInitializers AST transformation.
1797// Test a shader where a global constant array is initialized with an expression containing array
1798// indexing. This initializer is tricky to constant fold, so if it's not constant folded it needs to
1799// be handled in a way that doesn't generate statements in the global scope in HLSL output.
1800// Also includes multiple array initializers in one declaration, where only the second one has
1801// array indexing. This makes sure that the qualifier for the declaration is set correctly if
1802// transformations are applied to the declaration also in the case of ESSL output.
1803TEST_P(GLSLTest_ES3, InitGlobalArrayWithArrayIndexing)
1804{
Yuly Novikov41db2242016-06-25 00:14:28 -04001805 // TODO(ynovikov): re-enable once root cause of http://anglebug.com/1428 is fixed
1806 if (IsAndroid() && IsAdreno() && IsOpenGLES())
1807 {
1808 std::cout << "Test skipped on Adreno OpenGLES on Android." << std::endl;
1809 return;
1810 }
1811
Olli Etuahod4f4c112016-04-15 15:11:24 +03001812 const std::string vertexShaderSource =
1813 "#version 300 es\n"
1814 "precision highp float;\n"
1815 "in vec4 a_vec;\n"
1816 "void main()\n"
1817 "{\n"
1818 " gl_Position = vec4(a_vec);\n"
1819 "}";
1820
1821 const std::string fragmentShaderSource =
1822 "#version 300 es\n"
1823 "precision highp float;\n"
1824 "out vec4 my_FragColor;\n"
1825 "const highp float f[2] = float[2](0.1, 0.2);\n"
1826 "const highp float[2] g = float[2](0.3, 0.4), h = float[2](0.5, f[1]);\n"
1827 "void main()\n"
1828 "{\n"
1829 " my_FragColor = vec4(h[1]);\n"
1830 "}";
1831
1832 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1833 EXPECT_NE(0u, program);
1834}
1835
Corentin Wallez419bfc92016-06-28 10:54:45 -07001836// Test that index-constant sampler array indexing is supported.
1837TEST_P(GLSLTest, IndexConstantSamplerArrayIndexing)
1838{
1839 if (IsD3D11_FL93()) {
1840 std::cout << "Test skipped on D3D11 FL 9.3." << std::endl;
1841 return;
1842 }
1843
1844 const std::string vertexShaderSource =
1845 "attribute vec4 vPosition;\n"
1846 "void main()\n"
1847 "{\n"
1848 " gl_Position = vPosition;\n"
1849 "}";
1850
1851 const std::string fragmentShaderSource =
1852 "precision mediump float;\n"
1853 "uniform sampler2D uni[2];\n"
1854 "\n"
1855 "float zero(int x)\n"
1856 "{\n"
1857 " return float(x) - float(x);\n"
1858 "}\n"
1859 "\n"
1860 "void main()\n"
1861 "{\n"
1862 " vec4 c = vec4(0,0,0,0);\n"
1863 " for (int ii = 1; ii < 3; ++ii) {\n"
1864 " if (c.x > 255.0) {\n"
1865 " c.x = 255.0 + zero(ii);\n"
1866 " break;\n"
1867 " }\n"
1868 // Index the sampler array with a predictable loop index (index-constant) as opposed to
1869 // a true constant. This is valid in OpenGL ES but isn't in many Desktop OpenGL versions,
1870 // without an extension.
1871 " c += texture2D(uni[ii - 1], vec2(0.5, 0.5));\n"
1872 " }\n"
1873 " gl_FragColor = c;\n"
1874 "}";
1875
1876 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1877 EXPECT_NE(0u, program);
1878}
1879
Corentin Wallezb00dcee2016-07-11 17:42:58 -04001880// Test that the #pragma directive is supported and doesn't trigger a compilation failure on the
1881// native driver. The only pragma that gets passed to the OpenGL driver is "invariant" but we don't
1882// want to test its behavior, so don't use any varyings.
1883TEST_P(GLSLTest, PragmaDirective)
1884{
1885 const std::string vertexShaderSource =
1886 "#pragma STDGL invariant(all)\n"
1887 "void main()\n"
1888 "{\n"
1889 " gl_Position = vec4(1.0, 0.0, 0.0, 1.0);\n"
1890 "}\n";
1891
1892 const std::string fragmentShaderSource =
1893 "precision mediump float;\n"
1894 "void main()\n"
1895 "{\n"
1896 " gl_FragColor = vec4(1.0);\n"
1897 "}\n";
1898
1899 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1900 EXPECT_NE(0u, program);
1901}
1902
Olli Etuahoe1d199b2016-07-19 17:14:27 +03001903// Sequence operator evaluates operands from left to right (ESSL 3.00 section 5.9).
1904// The function call that returns the array needs to be evaluated after ++j for the expression to
1905// return the correct value (true).
1906TEST_P(GLSLTest_ES3, SequenceOperatorEvaluationOrderArray)
1907{
1908 const std::string &fragmentShaderSource =
1909 "#version 300 es\n"
1910 "precision mediump float;\n"
1911 "out vec4 my_FragColor; \n"
1912 "int[2] func(int param) {\n"
1913 " return int[2](param, param);\n"
1914 "}\n"
1915 "void main() {\n"
1916 " int a[2]; \n"
1917 " for (int i = 0; i < 2; ++i) {\n"
1918 " a[i] = 1;\n"
1919 " }\n"
1920 " int j = 0; \n"
1921 " bool result = ((++j), (a == func(j)));\n"
1922 " my_FragColor = vec4(0.0, (result ? 1.0 : 0.0), 0.0, 1.0);\n"
1923 "}\n";
1924
1925 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
1926 ASSERT_NE(0u, program);
1927
1928 drawQuad(program, "inputAttribute", 0.5f);
1929
1930 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
1931}
1932
1933// Sequence operator evaluates operands from left to right (ESSL 3.00 section 5.9).
1934// The short-circuiting expression needs to be evaluated after ++j for the expression to return the
1935// correct value (true).
1936TEST_P(GLSLTest_ES3, SequenceOperatorEvaluationOrderShortCircuit)
1937{
1938 const std::string &fragmentShaderSource =
1939 "#version 300 es\n"
1940 "precision mediump float;\n"
1941 "out vec4 my_FragColor; \n"
1942 "void main() {\n"
1943 " int j = 0; \n"
1944 " bool result = ((++j), (j == 1 ? true : (++j == 3)));\n"
1945 " my_FragColor = vec4(0.0, ((result && j == 1) ? 1.0 : 0.0), 0.0, 1.0);\n"
1946 "}\n";
1947
1948 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
1949 ASSERT_NE(0u, program);
1950
1951 drawQuad(program, "inputAttribute", 0.5f);
1952
1953 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
1954}
1955
Jamie Madillfa05f602015-05-07 13:47:11 -04001956// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
Geoff Lange0cc2a42016-01-20 10:58:17 -05001957ANGLE_INSTANTIATE_TEST(GLSLTest,
1958 ES2_D3D9(),
1959 ES2_D3D11(),
1960 ES2_D3D11_FL9_3(),
1961 ES2_OPENGL(),
1962 ES3_OPENGL(),
1963 ES2_OPENGLES(),
1964 ES3_OPENGLES());
Jamie Madillfa05f602015-05-07 13:47:11 -04001965
1966// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
Geoff Lange0cc2a42016-01-20 10:58:17 -05001967ANGLE_INSTANTIATE_TEST(GLSLTest_ES3, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGLES());