blob: 12a339ea4bc350225b41ec972010c6f6150c0a27 [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 Madill1048e432016-07-23 18:51:28 -04009#include "test_utils/gl_raii.h"
Jamie Madill55def582015-05-04 11:24:57 -040010
Jamie Madillfa05f602015-05-07 13:47:11 -040011using namespace angle;
Austin Kinross18b931d2014-09-29 12:58:31 -070012
Jamie Madill6c9503e2016-08-16 14:06:32 -040013namespace
14{
15
Jamie Madill2bf8b372014-06-16 17:18:51 -040016class GLSLTest : public ANGLETest
Jamie Madill96509e42014-05-29 14:33:27 -040017{
Jamie Madillfa05f602015-05-07 13:47:11 -040018 protected:
19 GLSLTest()
Jamie Madill96509e42014-05-29 14:33:27 -040020 {
21 setWindowWidth(128);
22 setWindowHeight(128);
23 setConfigRedBits(8);
24 setConfigGreenBits(8);
25 setConfigBlueBits(8);
26 setConfigAlphaBits(8);
27 }
Jamie Madillbfa91f42014-06-05 15:45:18 -040028
29 virtual void SetUp()
30 {
31 ANGLETest::SetUp();
32
Jamie Madill2bf8b372014-06-16 17:18:51 -040033 mSimpleVSSource = SHADER_SOURCE
Jamie Madillbfa91f42014-06-05 15:45:18 -040034 (
35 attribute vec4 inputAttribute;
36 void main()
37 {
38 gl_Position = inputAttribute;
39 }
40 );
41 }
42
Austin Kinrossaf875522014-08-25 21:06:07 -070043 std::string GenerateVaryingType(GLint vectorSize)
44 {
45 char varyingType[10];
46
47 if (vectorSize == 1)
48 {
49 sprintf(varyingType, "float");
50 }
51 else
52 {
53 sprintf(varyingType, "vec%d", vectorSize);
54 }
55
56 return std::string(varyingType);
57 }
58
59 std::string GenerateVectorVaryingDeclaration(GLint vectorSize, GLint arraySize, GLint id)
60 {
61 char buff[100];
62
63 if (arraySize == 1)
64 {
65 sprintf(buff, "varying %s v%d;\n", GenerateVaryingType(vectorSize).c_str(), id);
66 }
67 else
68 {
69 sprintf(buff, "varying %s v%d[%d];\n", GenerateVaryingType(vectorSize).c_str(), id, arraySize);
70 }
71
72 return std::string(buff);
73 }
74
75 std::string GenerateVectorVaryingSettingCode(GLint vectorSize, GLint arraySize, GLint id)
76 {
77 std::string returnString;
78 char buff[100];
79
80 if (arraySize == 1)
81 {
82 sprintf(buff, "\t v%d = %s(1.0);\n", id, GenerateVaryingType(vectorSize).c_str());
83 returnString += buff;
84 }
85 else
86 {
87 for (int i = 0; i < arraySize; i++)
88 {
89 sprintf(buff, "\t v%d[%d] = %s(1.0);\n", id, i, GenerateVaryingType(vectorSize).c_str());
90 returnString += buff;
91 }
92 }
93
94 return returnString;
95 }
96
97 std::string GenerateVectorVaryingUseCode(GLint arraySize, GLint id)
98 {
99 if (arraySize == 1)
100 {
101 char buff[100];
102 sprintf(buff, "v%d + ", id);
103 return std::string(buff);
104 }
105 else
106 {
107 std::string returnString;
108 for (int i = 0; i < arraySize; i++)
109 {
110 char buff[100];
111 sprintf(buff, "v%d[%d] + ", id, i);
112 returnString += buff;
113 }
114 return returnString;
115 }
116 }
117
Austin Kinross8b695ee2015-03-12 13:12:20 -0700118 void GenerateGLSLWithVaryings(GLint floatCount, GLint floatArrayCount, GLint vec2Count, GLint vec2ArrayCount, GLint vec3Count, GLint vec3ArrayCount,
119 GLint vec4Count, GLint vec4ArrayCount, bool useFragCoord, bool usePointCoord, bool usePointSize,
120 std::string* fragmentShader, std::string* vertexShader)
Austin Kinrossaf875522014-08-25 21:06:07 -0700121 {
122 // Generate a string declaring the varyings, to share between the fragment shader and the vertex shader.
123 std::string varyingDeclaration;
124
125 unsigned int varyingCount = 0;
126
127 for (GLint i = 0; i < floatCount; i++)
128 {
129 varyingDeclaration += GenerateVectorVaryingDeclaration(1, 1, varyingCount);
130 varyingCount += 1;
131 }
132
133 for (GLint i = 0; i < floatArrayCount; i++)
134 {
135 varyingDeclaration += GenerateVectorVaryingDeclaration(1, 2, varyingCount);
136 varyingCount += 1;
137 }
138
139 for (GLint i = 0; i < vec2Count; i++)
140 {
141 varyingDeclaration += GenerateVectorVaryingDeclaration(2, 1, varyingCount);
142 varyingCount += 1;
143 }
144
145 for (GLint i = 0; i < vec2ArrayCount; i++)
146 {
147 varyingDeclaration += GenerateVectorVaryingDeclaration(2, 2, varyingCount);
148 varyingCount += 1;
149 }
150
151 for (GLint i = 0; i < vec3Count; i++)
152 {
153 varyingDeclaration += GenerateVectorVaryingDeclaration(3, 1, varyingCount);
154 varyingCount += 1;
155 }
156
157 for (GLint i = 0; i < vec3ArrayCount; i++)
158 {
159 varyingDeclaration += GenerateVectorVaryingDeclaration(3, 2, varyingCount);
160 varyingCount += 1;
161 }
162
Austin Kinross8b695ee2015-03-12 13:12:20 -0700163 for (GLint i = 0; i < vec4Count; i++)
164 {
165 varyingDeclaration += GenerateVectorVaryingDeclaration(4, 1, varyingCount);
166 varyingCount += 1;
167 }
168
169 for (GLint i = 0; i < vec4ArrayCount; i++)
170 {
171 varyingDeclaration += GenerateVectorVaryingDeclaration(4, 2, varyingCount);
172 varyingCount += 1;
173 }
174
Austin Kinrossaf875522014-08-25 21:06:07 -0700175 // Generate the vertex shader
176 vertexShader->clear();
177 vertexShader->append(varyingDeclaration);
178 vertexShader->append("\nvoid main()\n{\n");
179
180 unsigned int currentVSVarying = 0;
181
182 for (GLint i = 0; i < floatCount; i++)
183 {
184 vertexShader->append(GenerateVectorVaryingSettingCode(1, 1, currentVSVarying));
185 currentVSVarying += 1;
186 }
187
188 for (GLint i = 0; i < floatArrayCount; i++)
189 {
190 vertexShader->append(GenerateVectorVaryingSettingCode(1, 2, currentVSVarying));
191 currentVSVarying += 1;
192 }
193
194 for (GLint i = 0; i < vec2Count; i++)
195 {
196 vertexShader->append(GenerateVectorVaryingSettingCode(2, 1, currentVSVarying));
197 currentVSVarying += 1;
198 }
199
200 for (GLint i = 0; i < vec2ArrayCount; i++)
201 {
202 vertexShader->append(GenerateVectorVaryingSettingCode(2, 2, currentVSVarying));
203 currentVSVarying += 1;
204 }
205
206 for (GLint i = 0; i < vec3Count; i++)
207 {
208 vertexShader->append(GenerateVectorVaryingSettingCode(3, 1, currentVSVarying));
209 currentVSVarying += 1;
210 }
211
212 for (GLint i = 0; i < vec3ArrayCount; i++)
213 {
214 vertexShader->append(GenerateVectorVaryingSettingCode(3, 2, currentVSVarying));
215 currentVSVarying += 1;
216 }
217
Austin Kinross8b695ee2015-03-12 13:12:20 -0700218 for (GLint i = 0; i < vec4Count; i++)
219 {
220 vertexShader->append(GenerateVectorVaryingSettingCode(4, 1, currentVSVarying));
221 currentVSVarying += 1;
222 }
223
224 for (GLint i = 0; i < vec4ArrayCount; i++)
225 {
226 vertexShader->append(GenerateVectorVaryingSettingCode(4, 2, currentVSVarying));
227 currentVSVarying += 1;
228 }
229
230 if (usePointSize)
231 {
232 vertexShader->append("gl_PointSize = 1.0;\n");
233 }
234
Austin Kinrossaf875522014-08-25 21:06:07 -0700235 vertexShader->append("}\n");
236
237 // Generate the fragment shader
238 fragmentShader->clear();
239 fragmentShader->append("precision highp float;\n");
240 fragmentShader->append(varyingDeclaration);
241 fragmentShader->append("\nvoid main() \n{ \n\tvec4 retColor = vec4(0,0,0,0);\n");
242
243 unsigned int currentFSVarying = 0;
244
245 // Make use of the float varyings
246 fragmentShader->append("\tretColor += vec4(");
247
248 for (GLint i = 0; i < floatCount; i++)
249 {
250 fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
251 currentFSVarying += 1;
252 }
253
254 for (GLint i = 0; i < floatArrayCount; i++)
255 {
256 fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
257 currentFSVarying += 1;
258 }
259
260 fragmentShader->append("0.0, 0.0, 0.0, 0.0);\n");
261
262 // Make use of the vec2 varyings
263 fragmentShader->append("\tretColor += vec4(");
264
265 for (GLint i = 0; i < vec2Count; i++)
266 {
267 fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
268 currentFSVarying += 1;
269 }
270
271 for (GLint i = 0; i < vec2ArrayCount; i++)
272 {
273 fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
274 currentFSVarying += 1;
275 }
276
277 fragmentShader->append("vec2(0.0, 0.0), 0.0, 0.0);\n");
278
279 // Make use of the vec3 varyings
280 fragmentShader->append("\tretColor += vec4(");
281
282 for (GLint i = 0; i < vec3Count; i++)
283 {
284 fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
285 currentFSVarying += 1;
286 }
287
288 for (GLint i = 0; i < vec3ArrayCount; i++)
289 {
290 fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
291 currentFSVarying += 1;
292 }
293
294 fragmentShader->append("vec3(0.0, 0.0, 0.0), 0.0);\n");
Austin Kinross8b695ee2015-03-12 13:12:20 -0700295
296 // Make use of the vec4 varyings
297 fragmentShader->append("\tretColor += ");
298
299 for (GLint i = 0; i < vec4Count; i++)
300 {
301 fragmentShader->append(GenerateVectorVaryingUseCode(1, currentFSVarying));
302 currentFSVarying += 1;
303 }
304
305 for (GLint i = 0; i < vec4ArrayCount; i++)
306 {
307 fragmentShader->append(GenerateVectorVaryingUseCode(2, currentFSVarying));
308 currentFSVarying += 1;
309 }
310
311 fragmentShader->append("vec4(0.0, 0.0, 0.0, 0.0);\n");
312
313 // Set gl_FragColor, and use special variables if requested
314 fragmentShader->append("\tgl_FragColor = retColor");
315
316 if (useFragCoord)
317 {
318 fragmentShader->append(" + gl_FragCoord");
319 }
320
321 if (usePointCoord)
322 {
323 fragmentShader->append(" + vec4(gl_PointCoord, 0.0, 0.0)");
324 }
325
326 fragmentShader->append(";\n}");
327 }
328
329 void VaryingTestBase(GLint floatCount, GLint floatArrayCount, GLint vec2Count, GLint vec2ArrayCount, GLint vec3Count, GLint vec3ArrayCount,
330 GLint vec4Count, GLint vec4ArrayCount, bool useFragCoord, bool usePointCoord, bool usePointSize, bool expectSuccess)
331 {
332 std::string fragmentShaderSource;
333 std::string vertexShaderSource;
334
335 GenerateGLSLWithVaryings(floatCount, floatArrayCount, vec2Count, vec2ArrayCount, vec3Count, vec3ArrayCount,
336 vec4Count, vec4ArrayCount, useFragCoord, usePointCoord, usePointSize,
337 &fragmentShaderSource, &vertexShaderSource);
338
339 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
340
341 if (expectSuccess)
342 {
343 EXPECT_NE(0u, program);
344 }
345 else
346 {
347 EXPECT_EQ(0u, program);
348 }
Austin Kinrossaf875522014-08-25 21:06:07 -0700349 }
350
Austin Kinross7a3e8e22015-10-08 15:50:06 -0700351 void CompileGLSLWithUniformsAndSamplers(GLint vertexUniformCount,
352 GLint fragmentUniformCount,
353 GLint vertexSamplersCount,
354 GLint fragmentSamplersCount,
355 bool expectSuccess)
356 {
357 std::stringstream vertexShader;
358 std::stringstream fragmentShader;
359
360 // Generate the vertex shader
361 vertexShader << "precision mediump float;\n";
362
363 for (int i = 0; i < vertexUniformCount; i++)
364 {
365 vertexShader << "uniform vec4 v" << i << ";\n";
366 }
367
368 for (int i = 0; i < vertexSamplersCount; i++)
369 {
370 vertexShader << "uniform sampler2D s" << i << ";\n";
371 }
372
373 vertexShader << "void main()\n{\n";
374
375 for (int i = 0; i < vertexUniformCount; i++)
376 {
377 vertexShader << " gl_Position += v" << i << ";\n";
378 }
379
380 for (int i = 0; i < vertexSamplersCount; i++)
381 {
382 vertexShader << " gl_Position += texture2D(s" << i << ", vec2(0.0, 0.0));\n";
383 }
384
385 if (vertexUniformCount == 0 && vertexSamplersCount == 0)
386 {
387 vertexShader << " gl_Position = vec4(0.0);\n";
388 }
389
390 vertexShader << "}\n";
391
392 // Generate the fragment shader
393 fragmentShader << "precision mediump float;\n";
394
395 for (int i = 0; i < fragmentUniformCount; i++)
396 {
397 fragmentShader << "uniform vec4 v" << i << ";\n";
398 }
399
400 for (int i = 0; i < fragmentSamplersCount; i++)
401 {
402 fragmentShader << "uniform sampler2D s" << i << ";\n";
403 }
404
405 fragmentShader << "void main()\n{\n";
406
407 for (int i = 0; i < fragmentUniformCount; i++)
408 {
409 fragmentShader << " gl_FragColor += v" << i << ";\n";
410 }
411
412 for (int i = 0; i < fragmentSamplersCount; i++)
413 {
414 fragmentShader << " gl_FragColor += texture2D(s" << i << ", vec2(0.0, 0.0));\n";
415 }
416
417 if (fragmentUniformCount == 0 && fragmentSamplersCount == 0)
418 {
419 fragmentShader << " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n";
420 }
421
422 fragmentShader << "}\n";
423
424 GLuint program = CompileProgram(vertexShader.str(), fragmentShader.str());
425
426 if (expectSuccess)
427 {
428 EXPECT_NE(0u, program);
429 }
430 else
431 {
432 EXPECT_EQ(0u, program);
433 }
434 }
435
Jamie Madill2bf8b372014-06-16 17:18:51 -0400436 std::string mSimpleVSSource;
Jamie Madill96509e42014-05-29 14:33:27 -0400437};
438
Jamie Madille1faacb2016-12-13 12:42:14 -0500439class GLSLTestNoValidation : public GLSLTest
440{
441 public:
442 GLSLTestNoValidation() { setNoErrorEnabled(true); }
443};
444
Jamie Madillfa05f602015-05-07 13:47:11 -0400445class GLSLTest_ES3 : public GLSLTest
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000446{
Olli Etuahoe1d199b2016-07-19 17:14:27 +0300447 void SetUp() override
448 {
449 ANGLETest::SetUp();
450
451 mSimpleVSSource =
452 "#version 300 es\n"
453 "in vec4 inputAttribute;"
454 "void main()"
455 "{"
456 " gl_Position = inputAttribute;"
457 "}";
458 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000459};
460
Jamie Madillfa05f602015-05-07 13:47:11 -0400461TEST_P(GLSLTest, NamelessScopedStructs)
Jamie Madill96509e42014-05-29 14:33:27 -0400462{
Jamie Madillbfa91f42014-06-05 15:45:18 -0400463 const std::string fragmentShaderSource = SHADER_SOURCE
Jamie Madill96509e42014-05-29 14:33:27 -0400464 (
Jamie Madillbfa91f42014-06-05 15:45:18 -0400465 precision mediump float;
466
Jamie Madill96509e42014-05-29 14:33:27 -0400467 void main()
468 {
Jamie Madillbfa91f42014-06-05 15:45:18 -0400469 struct
470 {
471 float q;
472 } b;
473
474 gl_FragColor = vec4(1, 0, 0, 1);
475 gl_FragColor.a += b.q;
Jamie Madill96509e42014-05-29 14:33:27 -0400476 }
477 );
478
Jamie Madill5599c8f2014-08-26 13:16:39 -0400479 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
Jamie Madillbfa91f42014-06-05 15:45:18 -0400480 EXPECT_NE(0u, program);
481}
Austin Kinross18b931d2014-09-29 12:58:31 -0700482
Jamie Madillfa05f602015-05-07 13:47:11 -0400483TEST_P(GLSLTest, ScopedStructsOrderBug)
Jamie Madillbfa91f42014-06-05 15:45:18 -0400484{
Geoff Lange0cc2a42016-01-20 10:58:17 -0500485 // TODO(geofflang): Find out why this doesn't compile on Apple OpenGL drivers
486 // (http://anglebug.com/1292)
Geoff Lang5103f4c2016-01-26 11:40:18 -0500487 // TODO(geofflang): Find out why this doesn't compile on AMD OpenGL drivers
Geoff Lange0cc2a42016-01-20 10:58:17 -0500488 // (http://anglebug.com/1291)
Corentin Wallezc7f59d02016-06-20 10:12:08 -0400489 if (IsDesktopOpenGL() && (IsOSX() || !IsNVIDIA()))
Geoff Lange0cc2a42016-01-20 10:58:17 -0500490 {
Jamie Madill518b9fa2016-03-02 11:26:02 -0500491 std::cout << "Test disabled on this OpenGL configuration." << std::endl;
Geoff Lange0cc2a42016-01-20 10:58:17 -0500492 return;
493 }
Geoff Lange0cc2a42016-01-20 10:58:17 -0500494
Jamie Madillbfa91f42014-06-05 15:45:18 -0400495 const std::string fragmentShaderSource = SHADER_SOURCE
496 (
497 precision mediump float;
498
499 struct T
500 {
501 float f;
502 };
503
504 void main()
505 {
506 T a;
507
508 struct T
509 {
510 float q;
511 };
512
513 T b;
514
515 gl_FragColor = vec4(1, 0, 0, 1);
516 gl_FragColor.a += a.f;
517 gl_FragColor.a += b.q;
518 }
519 );
520
Jamie Madill5599c8f2014-08-26 13:16:39 -0400521 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
Jamie Madillbfa91f42014-06-05 15:45:18 -0400522 EXPECT_NE(0u, program);
523}
524
Jamie Madillfa05f602015-05-07 13:47:11 -0400525TEST_P(GLSLTest, ScopedStructsBug)
Jamie Madillbfa91f42014-06-05 15:45:18 -0400526{
Jamie Madill96509e42014-05-29 14:33:27 -0400527 const std::string fragmentShaderSource = SHADER_SOURCE
528 (
529 precision mediump float;
530
531 struct T_0
532 {
533 float f;
534 };
535
536 void main()
537 {
538 gl_FragColor = vec4(1, 0, 0, 1);
539
540 struct T
541 {
542 vec2 v;
543 };
544
545 T_0 a;
546 T b;
547
548 gl_FragColor.a += a.f;
549 gl_FragColor.a += b.v.x;
550 }
551 );
552
Jamie Madill5599c8f2014-08-26 13:16:39 -0400553 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
Jamie Madill2bf8b372014-06-16 17:18:51 -0400554 EXPECT_NE(0u, program);
555}
556
Jamie Madillfa05f602015-05-07 13:47:11 -0400557TEST_P(GLSLTest, DxPositionBug)
Jamie Madill2bf8b372014-06-16 17:18:51 -0400558{
559 const std::string &vertexShaderSource = SHADER_SOURCE
560 (
561 attribute vec4 inputAttribute;
562 varying float dx_Position;
563 void main()
564 {
565 gl_Position = vec4(inputAttribute);
566 dx_Position = 0.0;
567 }
568 );
569
570 const std::string &fragmentShaderSource = SHADER_SOURCE
571 (
572 precision mediump float;
573
574 varying float dx_Position;
575
576 void main()
577 {
578 gl_FragColor = vec4(dx_Position, 0, 0, 1);
579 }
580 );
581
Jamie Madill5599c8f2014-08-26 13:16:39 -0400582 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill96509e42014-05-29 14:33:27 -0400583 EXPECT_NE(0u, program);
584}
Jamie Madill4836d222014-07-24 06:55:51 -0400585
Jamie Madillfa05f602015-05-07 13:47:11 -0400586TEST_P(GLSLTest, ElseIfRewriting)
Jamie Madill4836d222014-07-24 06:55:51 -0400587{
588 const std::string &vertexShaderSource =
589 "attribute vec4 a_position;\n"
590 "varying float v;\n"
591 "void main() {\n"
592 " gl_Position = a_position;\n"
593 " v = 1.0;\n"
594 " if (a_position.x <= 0.5) {\n"
595 " v = 0.0;\n"
596 " } else if (a_position.x >= 0.5) {\n"
597 " v = 2.0;\n"
598 " }\n"
599 "}\n";
600
601 const std::string &fragmentShaderSource =
602 "precision highp float;\n"
603 "varying float v;\n"
604 "void main() {\n"
605 " vec4 color = vec4(1.0, 0.0, 0.0, 1.0);\n"
606 " if (v >= 1.0) color = vec4(0.0, 1.0, 0.0, 1.0);\n"
607 " if (v >= 2.0) color = vec4(0.0, 0.0, 1.0, 1.0);\n"
608 " gl_FragColor = color;\n"
609 "}\n";
610
Jamie Madill5599c8f2014-08-26 13:16:39 -0400611 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill4836d222014-07-24 06:55:51 -0400612 ASSERT_NE(0u, program);
613
614 drawQuad(program, "a_position", 0.5f);
Jamie Madill4836d222014-07-24 06:55:51 -0400615
616 EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
617 EXPECT_PIXEL_EQ(getWindowWidth()-1, 0, 0, 255, 0, 255);
618}
619
Jamie Madillfa05f602015-05-07 13:47:11 -0400620TEST_P(GLSLTest, TwoElseIfRewriting)
Jamie Madill4836d222014-07-24 06:55:51 -0400621{
622 const std::string &vertexShaderSource =
623 "attribute vec4 a_position;\n"
624 "varying float v;\n"
625 "void main() {\n"
626 " gl_Position = a_position;\n"
Jamie Madill778d5272014-08-04 13:13:25 -0400627 " if (a_position.x == 0.0) {\n"
Jamie Madill4836d222014-07-24 06:55:51 -0400628 " v = 1.0;\n"
629 " } else if (a_position.x > 0.5) {\n"
630 " v = 0.0;\n"
631 " } else if (a_position.x > 0.75) {\n"
632 " v = 0.5;\n"
633 " }\n"
634 "}\n";
635
636 const std::string &fragmentShaderSource =
637 "precision highp float;\n"
638 "varying float v;\n"
639 "void main() {\n"
640 " gl_FragColor = vec4(v, 0.0, 0.0, 1.0);\n"
641 "}\n";
642
Jamie Madill5599c8f2014-08-26 13:16:39 -0400643 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill4836d222014-07-24 06:55:51 -0400644 EXPECT_NE(0u, program);
645}
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400646
Jamie Madillfa05f602015-05-07 13:47:11 -0400647TEST_P(GLSLTest, FrontFacingAndVarying)
Jamie Madille6256f82014-09-17 10:31:15 -0400648{
Geoff Langdd323e92015-06-09 15:16:31 -0400649 EGLPlatformParameters platform = GetParam().eglParameters;
Austin Kinross8b695ee2015-03-12 13:12:20 -0700650
Jamie Madille6256f82014-09-17 10:31:15 -0400651 const std::string vertexShaderSource = SHADER_SOURCE
652 (
653 attribute vec4 a_position;
654 varying float v_varying;
655 void main()
656 {
657 v_varying = a_position.x;
658 gl_Position = a_position;
659 }
660 );
661
662 const std::string fragmentShaderSource = SHADER_SOURCE
663 (
664 precision mediump float;
665 varying float v_varying;
666 void main()
667 {
668 vec4 c;
669
670 if (gl_FrontFacing)
671 {
672 c = vec4(v_varying, 0, 0, 1.0);
673 }
674 else
675 {
676 c = vec4(0, v_varying, 0, 1.0);
677 }
678 gl_FragColor = c;
679 }
680 );
681
682 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Austin Kinross02df7962015-07-01 10:03:42 -0700683
684 // Compilation should fail on D3D11 feature level 9_3, since gl_FrontFacing isn't supported.
685 if (platform.renderer == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE)
686 {
687 if (platform.majorVersion == 9 && platform.minorVersion == 3)
688 {
689 EXPECT_EQ(0u, program);
690 return;
691 }
692 }
693
694 // Otherwise, compilation should succeed
Jamie Madille6256f82014-09-17 10:31:15 -0400695 EXPECT_NE(0u, program);
696}
697
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400698// Verify that linking shaders declaring different shading language versions fails.
699TEST_P(GLSLTest_ES3, VersionMismatch)
700{
701 const std::string fragmentShaderSource100 =
702 "precision mediump float;\n"
703 "varying float v_varying;\n"
704 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
705
706 const std::string vertexShaderSource100 =
707 "attribute vec4 a_position;\n"
708 "varying float v_varying;\n"
709 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
710
711 const std::string fragmentShaderSource300 =
712 "#version 300 es\n"
713 "precision mediump float;\n"
714 "in float v_varying;\n"
715 "out vec4 my_FragColor;\n"
716 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
717
718 const std::string vertexShaderSource300 =
719 "#version 300 es\n"
720 "in vec4 a_position;\n"
721 "out float v_varying;\n"
722 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
723
724 GLuint program = CompileProgram(vertexShaderSource300, fragmentShaderSource100);
725 EXPECT_EQ(0u, program);
726
727 program = CompileProgram(vertexShaderSource100, fragmentShaderSource300);
728 EXPECT_EQ(0u, program);
729}
730
731// Verify that declaring varying as invariant only in vertex shader fails in ESSL 1.00.
732TEST_P(GLSLTest, InvariantVaryingOut)
733{
734 const std::string fragmentShaderSource =
735 "precision mediump float;\n"
736 "varying float v_varying;\n"
737 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
738
739 const std::string vertexShaderSource =
740 "attribute vec4 a_position;\n"
741 "invariant varying float v_varying;\n"
742 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
743
744 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
745 EXPECT_EQ(0u, program);
746}
747
748// Verify that declaring varying as invariant only in vertex shader succeeds in ESSL 3.00.
749TEST_P(GLSLTest_ES3, InvariantVaryingOut)
750{
751 // TODO: ESSL 3.00 -> GLSL 1.20 translation should add "invariant" in fragment shader
752 // for varyings which are invariant in vertex shader (http://anglebug.com/1293)
Corentin Wallezc7f59d02016-06-20 10:12:08 -0400753 if (IsDesktopOpenGL())
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400754 {
755 std::cout << "Test disabled on OpenGL." << std::endl;
756 return;
757 }
758
759 const std::string fragmentShaderSource =
760 "#version 300 es\n"
761 "precision mediump float;\n"
762 "in float v_varying;\n"
763 "out vec4 my_FragColor;\n"
764 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
765
766 const std::string vertexShaderSource =
767 "#version 300 es\n"
768 "in vec4 a_position;\n"
769 "invariant out float v_varying;\n"
770 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
771
772 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
773 EXPECT_NE(0u, program);
774}
775
776// Verify that declaring varying as invariant only in fragment shader fails in ESSL 1.00.
Jamie Madillfa05f602015-05-07 13:47:11 -0400777TEST_P(GLSLTest, InvariantVaryingIn)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400778{
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400779 const std::string fragmentShaderSource =
780 "precision mediump float;\n"
781 "invariant varying float v_varying;\n"
782 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
Geoff Lange0cc2a42016-01-20 10:58:17 -0500783
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400784 const std::string vertexShaderSource =
785 "attribute vec4 a_position;\n"
786 "varying float v_varying;\n"
787 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400788
Jamie Madill5599c8f2014-08-26 13:16:39 -0400789 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400790 EXPECT_EQ(0u, program);
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400791}
792
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400793// Verify that declaring varying as invariant only in fragment shader fails in ESSL 3.00.
794TEST_P(GLSLTest_ES3, InvariantVaryingIn)
795{
796 const std::string fragmentShaderSource =
797 "#version 300 es\n"
798 "precision mediump float;\n"
799 "invariant in float v_varying;\n"
800 "out vec4 my_FragColor;\n"
801 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
802
803 const std::string vertexShaderSource =
804 "#version 300 es\n"
805 "in vec4 a_position;\n"
806 "out float v_varying;\n"
807 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
808
809 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
810 EXPECT_EQ(0u, program);
811}
812
813// Verify that declaring varying as invariant in both shaders succeeds in ESSL 1.00.
Jamie Madillfa05f602015-05-07 13:47:11 -0400814TEST_P(GLSLTest, InvariantVaryingBoth)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400815{
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400816 const std::string fragmentShaderSource =
817 "precision mediump float;\n"
818 "invariant varying float v_varying;\n"
819 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400820
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400821 const std::string vertexShaderSource =
822 "attribute vec4 a_position;\n"
823 "invariant varying float v_varying;\n"
824 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400825
Jamie Madill5599c8f2014-08-26 13:16:39 -0400826 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400827 EXPECT_NE(0u, program);
828}
829
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400830// Verify that declaring varying as invariant in both shaders fails in ESSL 3.00.
831TEST_P(GLSLTest_ES3, InvariantVaryingBoth)
832{
833 const std::string fragmentShaderSource =
834 "#version 300 es\n"
835 "precision mediump float;\n"
836 "invariant in float v_varying;\n"
837 "out vec4 my_FragColor;\n"
838 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
839
840 const std::string vertexShaderSource =
841 "#version 300 es\n"
842 "in vec4 a_position;\n"
843 "invariant out float v_varying;\n"
844 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
845
846 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
847 EXPECT_EQ(0u, program);
848}
849
850// Verify that declaring gl_Position as invariant succeeds in ESSL 1.00.
Jamie Madillfa05f602015-05-07 13:47:11 -0400851TEST_P(GLSLTest, InvariantGLPosition)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400852{
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400853 const std::string fragmentShaderSource =
854 "precision mediump float;\n"
855 "varying float v_varying;\n"
856 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400857
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400858 const std::string vertexShaderSource =
859 "attribute vec4 a_position;\n"
860 "invariant gl_Position;\n"
861 "varying float v_varying;\n"
862 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400863
Jamie Madill5599c8f2014-08-26 13:16:39 -0400864 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400865 EXPECT_NE(0u, program);
866}
867
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400868// Verify that declaring gl_Position as invariant succeeds in ESSL 3.00.
869TEST_P(GLSLTest_ES3, InvariantGLPosition)
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400870{
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400871 const std::string fragmentShaderSource =
872 "#version 300 es\n"
873 "precision mediump float;\n"
874 "in float v_varying;\n"
875 "out vec4 my_FragColor;\n"
876 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
877
878 const std::string vertexShaderSource =
879 "#version 300 es\n"
880 "in vec4 a_position;\n"
881 "invariant gl_Position;\n"
882 "out float v_varying;\n"
883 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
884
885 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
886 EXPECT_NE(0u, program);
887}
888
889// Verify that using invariant(all) in both shaders succeeds in ESSL 1.00.
890TEST_P(GLSLTest, InvariantAllBoth)
891{
892 // TODO: ESSL 1.00 -> GLSL 1.20 translation should add "invariant" in fragment shader
893 // for varyings which are invariant in vertex shader individually,
894 // and remove invariant(all) from fragment shader (http://anglebug.com/1293)
Corentin Wallezc7f59d02016-06-20 10:12:08 -0400895 if (IsDesktopOpenGL())
Geoff Lange0cc2a42016-01-20 10:58:17 -0500896 {
897 std::cout << "Test disabled on OpenGL." << std::endl;
898 return;
899 }
900
Yuly Novikova1f6dc92016-06-15 23:27:04 -0400901 const std::string fragmentShaderSource =
902 "#pragma STDGL invariant(all)\n"
903 "precision mediump float;\n"
904 "varying float v_varying;\n"
905 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400906
907 const std::string vertexShaderSource =
908 "#pragma STDGL invariant(all)\n"
909 "attribute vec4 a_position;\n"
910 "varying float v_varying;\n"
911 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
912
Jamie Madill5599c8f2014-08-26 13:16:39 -0400913 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400914 EXPECT_NE(0u, program);
915}
Austin Kinrossaf875522014-08-25 21:06:07 -0700916
Geoff Lang156d7192016-07-21 16:11:00 -0400917// Verify that functions without return statements still compile
918TEST_P(GLSLTest, MissingReturnFloat)
919{
920 const std::string vertexShaderSource =
921 "varying float v_varying;\n"
922 "float f() { if (v_varying > 0.0) return 1.0; }\n"
923 "void main() { gl_Position = vec4(f(), 0, 0, 1); }\n";
924
925 const std::string fragmentShaderSource =
926 "precision mediump float;\n"
927 "void main() { gl_FragColor = vec4(0, 0, 0, 1); }\n";
928
929 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
930 EXPECT_NE(0u, program);
931}
932
933// Verify that functions without return statements still compile
934TEST_P(GLSLTest, MissingReturnVec2)
935{
936 const std::string vertexShaderSource =
937 "varying float v_varying;\n"
938 "vec2 f() { if (v_varying > 0.0) return vec2(1.0, 1.0); }\n"
939 "void main() { gl_Position = vec4(f().x, 0, 0, 1); }\n";
940
941 const std::string fragmentShaderSource =
942 "precision mediump float;\n"
943 "void main() { gl_FragColor = vec4(0, 0, 0, 1); }\n";
944
945 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
946 EXPECT_NE(0u, program);
947}
948
949// Verify that functions without return statements still compile
950TEST_P(GLSLTest, MissingReturnVec3)
951{
952 const std::string vertexShaderSource =
953 "varying float v_varying;\n"
954 "vec3 f() { if (v_varying > 0.0) return vec3(1.0, 1.0, 1.0); }\n"
955 "void main() { gl_Position = vec4(f().x, 0, 0, 1); }\n";
956
957 const std::string fragmentShaderSource =
958 "precision mediump float;\n"
959 "void main() { gl_FragColor = vec4(0, 0, 0, 1); }\n";
960
961 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
962 EXPECT_NE(0u, program);
963}
964
965// Verify that functions without return statements still compile
966TEST_P(GLSLTest, MissingReturnVec4)
967{
968 const std::string vertexShaderSource =
969 "varying float v_varying;\n"
970 "vec4 f() { if (v_varying > 0.0) return vec4(1.0, 1.0, 1.0, 1.0); }\n"
971 "void main() { gl_Position = vec4(f().x, 0, 0, 1); }\n";
972
973 const std::string fragmentShaderSource =
974 "precision mediump float;\n"
975 "void main() { gl_FragColor = vec4(0, 0, 0, 1); }\n";
976
977 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
978 EXPECT_NE(0u, program);
979}
980
981// Verify that functions without return statements still compile
982TEST_P(GLSLTest, MissingReturnIVec4)
983{
984 const std::string vertexShaderSource =
985 "varying float v_varying;\n"
986 "ivec4 f() { if (v_varying > 0.0) return ivec4(1, 1, 1, 1); }\n"
987 "void main() { gl_Position = vec4(f().x, 0, 0, 1); }\n";
988
989 const std::string fragmentShaderSource =
990 "precision mediump float;\n"
991 "void main() { gl_FragColor = vec4(0, 0, 0, 1); }\n";
992
993 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
994 EXPECT_NE(0u, program);
995}
996
997// Verify that functions without return statements still compile
998TEST_P(GLSLTest, MissingReturnMat4)
999{
1000 const std::string vertexShaderSource =
1001 "varying float v_varying;\n"
1002 "mat4 f() { if (v_varying > 0.0) return mat4(1.0); }\n"
1003 "void main() { gl_Position = vec4(f()[0][0], 0, 0, 1); }\n";
1004
1005 const std::string fragmentShaderSource =
1006 "precision mediump float;\n"
1007 "void main() { gl_FragColor = vec4(0, 0, 0, 1); }\n";
1008
1009 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1010 EXPECT_NE(0u, program);
1011}
1012
1013// Verify that functions without return statements still compile
1014TEST_P(GLSLTest, MissingReturnStruct)
1015{
1016 const std::string vertexShaderSource =
1017 "varying float v_varying;\n"
1018 "struct s { float a; int b; vec2 c; };\n"
1019 "s f() { if (v_varying > 0.0) return s(1.0, 1, vec2(1.0, 1.0)); }\n"
1020 "void main() { gl_Position = vec4(f().a, 0, 0, 1); }\n";
1021
1022 const std::string fragmentShaderSource =
1023 "precision mediump float;\n"
1024 "void main() { gl_FragColor = vec4(0, 0, 0, 1); }\n";
1025
1026 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1027 EXPECT_NE(0u, program);
1028}
1029
1030// Verify that functions without return statements still compile
1031TEST_P(GLSLTest_ES3, MissingReturnArray)
1032{
1033 const std::string vertexShaderSource =
1034 "#version 300 es\n"
1035 "in float v_varying;\n"
1036 "vec2[2] f() { if (v_varying > 0.0) { return vec2[2](vec2(1.0, 1.0), vec2(1.0, 1.0)); } }\n"
1037 "void main() { gl_Position = vec4(f()[0].x, 0, 0, 1); }\n";
1038
1039 const std::string fragmentShaderSource =
1040 "#version 300 es\n"
1041 "precision mediump float;\n"
1042 "out vec4 my_FragColor;\n"
1043 "void main() { my_FragColor = vec4(0, 0, 0, 1); }\n";
1044
1045 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1046 EXPECT_NE(0u, program);
1047}
1048
1049// Verify that functions without return statements still compile
1050TEST_P(GLSLTest_ES3, MissingReturnArrayOfStructs)
1051{
1052 const std::string vertexShaderSource =
1053 "#version 300 es\n"
1054 "in float v_varying;\n"
1055 "struct s { float a; int b; vec2 c; };\n"
1056 "s[2] f() { if (v_varying > 0.0) { return s[2](s(1.0, 1, vec2(1.0, 1.0)), s(1.0, 1, "
1057 "vec2(1.0, 1.0))); } }\n"
1058 "void main() { gl_Position = vec4(f()[0].a, 0, 0, 1); }\n";
1059
1060 const std::string fragmentShaderSource =
1061 "#version 300 es\n"
1062 "precision mediump float;\n"
1063 "out vec4 my_FragColor;\n"
1064 "void main() { my_FragColor = vec4(0, 0, 0, 1); }\n";
1065
1066 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1067 EXPECT_NE(0u, program);
1068}
1069
Corentin Wallez509e4562016-08-25 14:55:44 -04001070// Verify that functions without return statements still compile
1071TEST_P(GLSLTest_ES3, MissingReturnStructOfArrays)
1072{
1073 // TODO(cwallez) remove the suppression once NVIDIA removes the restriction for
1074 // GLSL >= 300. It was defined only in GLSL 2.0, section 6.1.
1075 if (IsNVIDIA() && IsOpenGLES())
1076 {
1077 std::cout << "Test skipped on NVIDIA OpenGL ES because it disallows returning "
1078 "structure of arrays"
1079 << std::endl;
1080 return;
1081 }
1082
1083 const std::string vertexShaderSource =
1084 "#version 300 es\n"
1085 "in float v_varying;\n"
1086 "struct s { float a[2]; int b[2]; vec2 c[2]; };\n"
1087 "s f() { if (v_varying > 0.0) { return s(float[2](1.0, 1.0), int[2](1, 1),"
1088 "vec2[2](vec2(1.0, 1.0), vec2(1.0, 1.0))); } }\n"
1089 "void main() { gl_Position = vec4(f().a[0], 0, 0, 1); }\n";
1090
1091 const std::string fragmentShaderSource =
1092 "#version 300 es\n"
1093 "precision mediump float;\n"
1094 "out vec4 my_FragColor;\n"
1095 "void main() { my_FragColor = vec4(0, 0, 0, 1); }\n";
1096
1097 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1098 EXPECT_NE(0u, program);
1099}
1100
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001101// Verify that using invariant(all) in both shaders fails in ESSL 3.00.
1102TEST_P(GLSLTest_ES3, InvariantAllBoth)
1103{
1104 const std::string fragmentShaderSource =
1105 "#version 300 es\n"
1106 "#pragma STDGL invariant(all)\n"
1107 "precision mediump float;\n"
1108 "in float v_varying;\n"
1109 "out vec4 my_FragColor;\n"
1110 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
1111
1112 const std::string vertexShaderSource =
1113 "#version 300 es\n"
1114 "#pragma STDGL invariant(all)\n"
1115 "in vec4 a_position;\n"
1116 "out float v_varying;\n"
1117 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
1118
1119 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1120 EXPECT_EQ(0u, program);
1121}
1122
1123// Verify that using invariant(all) only in fragment shader fails in ESSL 1.00.
1124TEST_P(GLSLTest, InvariantAllIn)
1125{
1126 const std::string fragmentShaderSource =
1127 "#pragma STDGL invariant(all)\n"
1128 "precision mediump float;\n"
1129 "varying float v_varying;\n"
1130 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
1131
1132 const std::string vertexShaderSource =
1133 "attribute vec4 a_position;\n"
1134 "varying float v_varying;\n"
1135 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
1136
1137 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1138 EXPECT_EQ(0u, program);
1139}
1140
1141// Verify that using invariant(all) only in fragment shader fails in ESSL 3.00.
1142TEST_P(GLSLTest_ES3, InvariantAllIn)
1143{
1144 const std::string fragmentShaderSource =
1145 "#version 300 es\n"
1146 "#pragma STDGL invariant(all)\n"
1147 "precision mediump float;\n"
1148 "in float v_varying;\n"
1149 "out vec4 my_FragColor;\n"
1150 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
1151
1152 const std::string vertexShaderSource =
1153 "#version 300 es\n"
1154 "in vec4 a_position;\n"
1155 "out float v_varying;\n"
1156 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
1157
1158 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1159 EXPECT_EQ(0u, program);
1160}
1161
1162// Verify that using invariant(all) only in vertex shader fails in ESSL 1.00.
1163TEST_P(GLSLTest, InvariantAllOut)
1164{
1165 const std::string fragmentShaderSource =
1166 "precision mediump float;\n"
1167 "varying float v_varying;\n"
1168 "void main() { gl_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
1169
1170 const std::string vertexShaderSource =
1171 "#pragma STDGL invariant(all)\n"
1172 "attribute vec4 a_position;\n"
1173 "varying float v_varying;\n"
1174 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
1175
1176 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1177 EXPECT_EQ(0u, program);
1178}
1179
1180// Verify that using invariant(all) only in vertex shader succeeds in ESSL 3.00.
1181TEST_P(GLSLTest_ES3, InvariantAllOut)
1182{
1183 // TODO: ESSL 3.00 -> GLSL 1.20 translation should add "invariant" in fragment shader
1184 // for varyings which are invariant in vertex shader,
1185 // because of invariant(all) being used in vertex shader (http://anglebug.com/1293)
Corentin Wallezc7f59d02016-06-20 10:12:08 -04001186 if (IsDesktopOpenGL())
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001187 {
1188 std::cout << "Test disabled on OpenGL." << std::endl;
1189 return;
1190 }
1191
1192 const std::string fragmentShaderSource =
1193 "#version 300 es\n"
1194 "precision mediump float;\n"
1195 "in float v_varying;\n"
1196 "out vec4 my_FragColor;\n"
1197 "void main() { my_FragColor = vec4(v_varying, 0, 0, 1.0); }\n";
1198
1199 const std::string vertexShaderSource =
1200 "#version 300 es\n"
1201 "#pragma STDGL invariant(all)\n"
1202 "in vec4 a_position;\n"
1203 "out float v_varying;\n"
1204 "void main() { v_varying = a_position.x; gl_Position = a_position; }\n";
1205
1206 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1207 EXPECT_NE(0u, program);
1208}
1209
Jamie Madillfa05f602015-05-07 13:47:11 -04001210TEST_P(GLSLTest, MaxVaryingVec4)
Austin Kinross8b695ee2015-03-12 13:12:20 -07001211{
Geoff Lang69accbd2016-01-25 16:22:32 -05001212#if defined(__APPLE__)
1213 // TODO(geofflang): Find out why this doesn't compile on Apple AND OpenGL drivers
1214 // (http://anglebug.com/1291)
Jamie Madill518b9fa2016-03-02 11:26:02 -05001215 if (IsAMD() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
Geoff Lang69accbd2016-01-25 16:22:32 -05001216 {
1217 std::cout << "Test disabled on Apple AMD OpenGL." << std::endl;
1218 return;
1219 }
1220#endif
1221
Austin Kinross8b695ee2015-03-12 13:12:20 -07001222 GLint maxVaryings = 0;
1223 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1224
1225 VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings, 0, false, false, false, true);
1226}
1227
Jamie Madillfa05f602015-05-07 13:47:11 -04001228TEST_P(GLSLTest, MaxMinusTwoVaryingVec4PlusTwoSpecialVariables)
Austin Kinross8b695ee2015-03-12 13:12:20 -07001229{
1230 GLint maxVaryings = 0;
1231 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1232
1233 // Generate shader code that uses gl_FragCoord and gl_PointCoord, two special fragment shader variables.
1234 VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings - 2, 0, true, true, false, true);
1235}
1236
Jamie Madillfa05f602015-05-07 13:47:11 -04001237TEST_P(GLSLTest, MaxMinusTwoVaryingVec4PlusThreeSpecialVariables)
Austin Kinross8b695ee2015-03-12 13:12:20 -07001238{
Geoff Lange0cc2a42016-01-20 10:58:17 -05001239 // TODO(geofflang): Figure out why this fails on OpenGL AMD (http://anglebug.com/1291)
Jamie Madill518b9fa2016-03-02 11:26:02 -05001240 if (IsAMD() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
Geoff Lange0cc2a42016-01-20 10:58:17 -05001241 {
1242 std::cout << "Test disabled on OpenGL." << std::endl;
1243 return;
1244 }
1245
Austin Kinross8b695ee2015-03-12 13:12:20 -07001246 GLint maxVaryings = 0;
1247 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1248
1249 // Generate shader code that uses gl_FragCoord, gl_PointCoord and gl_PointSize.
1250 VaryingTestBase(0, 0, 0, 0, 0, 0, maxVaryings - 2, 0, true, true, true, true);
1251}
1252
Jamie Madillfa05f602015-05-07 13:47:11 -04001253TEST_P(GLSLTest, MaxVaryingVec3)
Austin Kinrossaf875522014-08-25 21:06:07 -07001254{
1255 GLint maxVaryings = 0;
1256 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1257
Austin Kinross8b695ee2015-03-12 13:12:20 -07001258 VaryingTestBase(0, 0, 0, 0, maxVaryings, 0, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001259}
1260
Jamie Madillfa05f602015-05-07 13:47:11 -04001261TEST_P(GLSLTest, MaxVaryingVec3Array)
Austin Kinrossaf875522014-08-25 21:06:07 -07001262{
1263 GLint maxVaryings = 0;
1264 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1265
Austin Kinross8b695ee2015-03-12 13:12:20 -07001266 VaryingTestBase(0, 0, 0, 0, 0, maxVaryings / 2, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001267}
1268
Jamie Madillbee59e02014-10-02 10:44:18 -04001269// Disabled because of a failure in D3D9
Jamie Madill9fc36822015-11-18 13:08:07 -05001270TEST_P(GLSLTest, MaxVaryingVec3AndOneFloat)
Austin Kinrossaf875522014-08-25 21:06:07 -07001271{
Jamie Madill518b9fa2016-03-02 11:26:02 -05001272 if (IsD3D9())
Jamie Madill9fc36822015-11-18 13:08:07 -05001273 {
1274 std::cout << "Test disabled on D3D9." << std::endl;
1275 return;
1276 }
1277
Austin Kinrossaf875522014-08-25 21:06:07 -07001278 GLint maxVaryings = 0;
1279 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1280
Austin Kinross8b695ee2015-03-12 13:12:20 -07001281 VaryingTestBase(1, 0, 0, 0, maxVaryings, 0, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001282}
1283
Jamie Madillbee59e02014-10-02 10:44:18 -04001284// Disabled because of a failure in D3D9
Jamie Madill9fc36822015-11-18 13:08:07 -05001285TEST_P(GLSLTest, MaxVaryingVec3ArrayAndOneFloatArray)
Austin Kinrossaf875522014-08-25 21:06:07 -07001286{
Jamie Madill518b9fa2016-03-02 11:26:02 -05001287 if (IsD3D9())
Jamie Madill9fc36822015-11-18 13:08:07 -05001288 {
1289 std::cout << "Test disabled on D3D9." << std::endl;
1290 return;
1291 }
1292
Austin Kinrossaf875522014-08-25 21:06:07 -07001293 GLint maxVaryings = 0;
1294 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1295
Austin Kinross8b695ee2015-03-12 13:12:20 -07001296 VaryingTestBase(0, 1, 0, 0, 0, maxVaryings / 2, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001297}
1298
Jamie Madillbee59e02014-10-02 10:44:18 -04001299// Disabled because of a failure in D3D9
Jamie Madill9fc36822015-11-18 13:08:07 -05001300TEST_P(GLSLTest, TwiceMaxVaryingVec2)
Austin Kinrossaf875522014-08-25 21:06:07 -07001301{
Jamie Madill518b9fa2016-03-02 11:26:02 -05001302 if (IsD3D9())
Jamie Madill9fc36822015-11-18 13:08:07 -05001303 {
1304 std::cout << "Test disabled on D3D9." << std::endl;
1305 return;
1306 }
1307
Geoff Lange0cc2a42016-01-20 10:58:17 -05001308 if (getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)
1309 {
1310 // TODO(geofflang): Figure out why this fails on NVIDIA's GLES driver
1311 std::cout << "Test disabled on OpenGL ES." << std::endl;
1312 return;
1313 }
1314
Geoff Lang69accbd2016-01-25 16:22:32 -05001315#if defined(__APPLE__)
1316 // TODO(geofflang): Find out why this doesn't compile on Apple AND OpenGL drivers
1317 // (http://anglebug.com/1291)
Jamie Madill518b9fa2016-03-02 11:26:02 -05001318 if (IsAMD() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
Geoff Lang69accbd2016-01-25 16:22:32 -05001319 {
1320 std::cout << "Test disabled on Apple AMD OpenGL." << std::endl;
1321 return;
1322 }
1323#endif
1324
Austin Kinrossaf875522014-08-25 21:06:07 -07001325 GLint maxVaryings = 0;
1326 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1327
Austin Kinross8b695ee2015-03-12 13:12:20 -07001328 VaryingTestBase(0, 0, 2 * maxVaryings, 0, 0, 0, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001329}
1330
Jamie Madillbee59e02014-10-02 10:44:18 -04001331// Disabled because of a failure in D3D9
Jamie Madill9fc36822015-11-18 13:08:07 -05001332TEST_P(GLSLTest, MaxVaryingVec2Arrays)
Austin Kinrossaf875522014-08-25 21:06:07 -07001333{
Jamie Madill518b9fa2016-03-02 11:26:02 -05001334 if (IsD3DSM3())
Jamie Madill9fc36822015-11-18 13:08:07 -05001335 {
1336 std::cout << "Test disabled on SM3." << std::endl;
1337 return;
1338 }
1339
Geoff Lange0cc2a42016-01-20 10:58:17 -05001340 if (getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)
1341 {
1342 // TODO(geofflang): Figure out why this fails on NVIDIA's GLES driver
1343 std::cout << "Test disabled on OpenGL ES." << std::endl;
1344 return;
1345 }
1346
Geoff Lang69accbd2016-01-25 16:22:32 -05001347#if defined(__APPLE__)
1348 // TODO(geofflang): Find out why this doesn't compile on Apple AND OpenGL drivers
1349 // (http://anglebug.com/1291)
Jamie Madill518b9fa2016-03-02 11:26:02 -05001350 if (IsAMD() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
Geoff Lang69accbd2016-01-25 16:22:32 -05001351 {
1352 std::cout << "Test disabled on Apple AMD OpenGL." << std::endl;
1353 return;
1354 }
1355#endif
1356
Austin Kinrossaf875522014-08-25 21:06:07 -07001357 GLint maxVaryings = 0;
1358 glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);
1359
Austin Kinross8b695ee2015-03-12 13:12:20 -07001360 VaryingTestBase(0, 0, 0, maxVaryings, 0, 0, 0, 0, false, false, false, true);
Austin Kinrossaf875522014-08-25 21:06:07 -07001361}
1362
Geoff Langf60fab62014-11-24 11:21:20 -05001363// Verify shader source with a fixed length that is less than the null-terminated length will compile.
Jamie Madillfa05f602015-05-07 13:47:11 -04001364TEST_P(GLSLTest, FixedShaderLength)
Geoff Langf60fab62014-11-24 11:21:20 -05001365{
1366 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1367
1368 const std::string appendGarbage = "abcasdfasdfasdfasdfasdf";
1369 const std::string source = "void main() { gl_FragColor = vec4(0, 0, 0, 0); }" + appendGarbage;
1370 const char *sourceArray[1] = { source.c_str() };
Corentin Wallez973402f2015-05-11 13:42:22 -04001371 GLint lengths[1] = { static_cast<GLint>(source.length() - appendGarbage.length()) };
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001372 glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
Geoff Langf60fab62014-11-24 11:21:20 -05001373 glCompileShader(shader);
1374
1375 GLint compileResult;
1376 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1377 EXPECT_NE(compileResult, 0);
1378}
1379
1380// Verify that a negative shader source length is treated as a null-terminated length.
Jamie Madillfa05f602015-05-07 13:47:11 -04001381TEST_P(GLSLTest, NegativeShaderLength)
Geoff Langf60fab62014-11-24 11:21:20 -05001382{
1383 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1384
1385 const char *sourceArray[1] = { "void main() { gl_FragColor = vec4(0, 0, 0, 0); }" };
1386 GLint lengths[1] = { -10 };
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001387 glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
Geoff Langf60fab62014-11-24 11:21:20 -05001388 glCompileShader(shader);
1389
1390 GLint compileResult;
1391 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1392 EXPECT_NE(compileResult, 0);
1393}
1394
Corentin Wallez9a9c0482016-04-12 10:36:25 -04001395// Check that having an invalid char after the "." doesn't cause an assert.
1396TEST_P(GLSLTest, InvalidFieldFirstChar)
1397{
1398 GLuint shader = glCreateShader(GL_VERTEX_SHADER);
1399 const char *source = "void main() {vec4 x; x.}";
1400 glShaderSource(shader, 1, &source, 0);
1401 glCompileShader(shader);
1402
1403 GLint compileResult;
1404 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1405 EXPECT_EQ(0, compileResult);
1406}
1407
Geoff Langf60fab62014-11-24 11:21:20 -05001408// Verify that a length array with mixed positive and negative values compiles.
Jamie Madillfa05f602015-05-07 13:47:11 -04001409TEST_P(GLSLTest, MixedShaderLengths)
Geoff Langf60fab62014-11-24 11:21:20 -05001410{
1411 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1412
1413 const char *sourceArray[] =
1414 {
1415 "void main()",
1416 "{",
1417 " gl_FragColor = vec4(0, 0, 0, 0);",
1418 "}",
1419 };
1420 GLint lengths[] =
1421 {
1422 -10,
1423 1,
Corentin Wallez973402f2015-05-11 13:42:22 -04001424 static_cast<GLint>(strlen(sourceArray[2])),
Geoff Langf60fab62014-11-24 11:21:20 -05001425 -1,
1426 };
1427 ASSERT_EQ(ArraySize(sourceArray), ArraySize(lengths));
1428
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001429 glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
Geoff Langf60fab62014-11-24 11:21:20 -05001430 glCompileShader(shader);
1431
1432 GLint compileResult;
1433 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1434 EXPECT_NE(compileResult, 0);
1435}
1436
1437// Verify that zero-length shader source does not affect shader compilation.
Jamie Madillfa05f602015-05-07 13:47:11 -04001438TEST_P(GLSLTest, ZeroShaderLength)
Geoff Langf60fab62014-11-24 11:21:20 -05001439{
1440 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1441
1442 const char *sourceArray[] =
1443 {
1444 "adfasdf",
1445 "34534",
1446 "void main() { gl_FragColor = vec4(0, 0, 0, 0); }",
1447 "",
1448 "asdfasdfsdsdf",
1449 };
1450 GLint lengths[] =
1451 {
1452 0,
1453 0,
1454 -1,
1455 0,
1456 0,
1457 };
1458 ASSERT_EQ(ArraySize(sourceArray), ArraySize(lengths));
1459
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001460 glShaderSource(shader, static_cast<GLsizei>(ArraySize(sourceArray)), sourceArray, lengths);
Geoff Langf60fab62014-11-24 11:21:20 -05001461 glCompileShader(shader);
1462
1463 GLint compileResult;
1464 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1465 EXPECT_NE(compileResult, 0);
1466}
Jamie Madill21c1e452014-12-29 11:33:41 -05001467
1468// Tests that bad index expressions don't crash ANGLE's translator.
1469// https://code.google.com/p/angleproject/issues/detail?id=857
Jamie Madillfa05f602015-05-07 13:47:11 -04001470TEST_P(GLSLTest, BadIndexBug)
Jamie Madill21c1e452014-12-29 11:33:41 -05001471{
1472 const std::string &fragmentShaderSourceVec =
1473 "precision mediump float;\n"
1474 "uniform vec4 uniformVec;\n"
1475 "void main()\n"
1476 "{\n"
1477 " gl_FragColor = vec4(uniformVec[int()]);\n"
1478 "}";
1479
1480 GLuint shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceVec);
1481 EXPECT_EQ(0u, shader);
1482
1483 if (shader != 0)
1484 {
1485 glDeleteShader(shader);
1486 }
1487
1488 const std::string &fragmentShaderSourceMat =
1489 "precision mediump float;\n"
1490 "uniform mat4 uniformMat;\n"
1491 "void main()\n"
1492 "{\n"
1493 " gl_FragColor = vec4(uniformMat[int()]);\n"
1494 "}";
1495
1496 shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceMat);
1497 EXPECT_EQ(0u, shader);
1498
1499 if (shader != 0)
1500 {
1501 glDeleteShader(shader);
1502 }
1503
1504 const std::string &fragmentShaderSourceArray =
1505 "precision mediump float;\n"
1506 "uniform vec4 uniformArray;\n"
1507 "void main()\n"
1508 "{\n"
1509 " gl_FragColor = vec4(uniformArray[int()]);\n"
1510 "}";
1511
1512 shader = CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSourceArray);
1513 EXPECT_EQ(0u, shader);
1514
1515 if (shader != 0)
1516 {
1517 glDeleteShader(shader);
1518 }
Jamie Madill37997142015-01-28 10:06:34 -05001519}
1520
Jamie Madill2e295e22015-04-29 10:41:33 -04001521// Test that structs defined in uniforms are translated correctly.
Jamie Madillfa05f602015-05-07 13:47:11 -04001522TEST_P(GLSLTest, StructSpecifiersUniforms)
Jamie Madill2e295e22015-04-29 10:41:33 -04001523{
1524 const std::string fragmentShaderSource = SHADER_SOURCE
1525 (
1526 precision mediump float;
1527
1528 uniform struct S { float field;} s;
1529
1530 void main()
1531 {
1532 gl_FragColor = vec4(1, 0, 0, 1);
1533 gl_FragColor.a += s.field;
1534 }
1535 );
1536
1537 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
1538 EXPECT_NE(0u, program);
1539}
Jamie Madill55def582015-05-04 11:24:57 -04001540
1541// Test that gl_DepthRange is not stored as a uniform location. Since uniforms
1542// beginning with "gl_" are filtered out by our validation logic, we must
1543// bypass the validation to test the behaviour of the implementation.
1544// (note this test is still Impl-independent)
Jamie Madille1faacb2016-12-13 12:42:14 -05001545TEST_P(GLSLTestNoValidation, DepthRangeUniforms)
Jamie Madill55def582015-05-04 11:24:57 -04001546{
1547 const std::string fragmentShaderSource = SHADER_SOURCE
1548 (
1549 precision mediump float;
1550
1551 void main()
1552 {
1553 gl_FragColor = vec4(gl_DepthRange.near, gl_DepthRange.far, gl_DepthRange.diff, 1);
1554 }
1555 );
1556
Jamie Madille1faacb2016-12-13 12:42:14 -05001557 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShaderSource);
Jamie Madill55def582015-05-04 11:24:57 -04001558
Jamie Madille1faacb2016-12-13 12:42:14 -05001559 // We need to bypass validation for this call.
1560 GLint nearIndex = glGetUniformLocation(program.get(), "gl_DepthRange.near");
Jamie Madill55def582015-05-04 11:24:57 -04001561 EXPECT_EQ(-1, nearIndex);
1562
1563 // Test drawing does not throw an exception.
Jamie Madille1faacb2016-12-13 12:42:14 -05001564 drawQuad(program.get(), "inputAttribute", 0.5f);
Jamie Madill55def582015-05-04 11:24:57 -04001565
1566 EXPECT_GL_NO_ERROR();
Jamie Madill55def582015-05-04 11:24:57 -04001567}
Jamie Madill4052dfc2015-05-06 15:18:49 -04001568
Jamie Madill6c9503e2016-08-16 14:06:32 -04001569std::string GenerateSmallPowShader(double base, double exponent)
1570{
1571 std::stringstream stream;
1572
1573 stream.precision(8);
1574
1575 double result = pow(base, exponent);
1576
1577 stream << "precision highp float;\n"
1578 << "float fun(float arg)\n"
1579 << "{\n"
1580 << " return pow(arg, " << std::fixed << exponent << ");\n"
1581 << "}\n"
1582 << "\n"
1583 << "void main()\n"
1584 << "{\n"
1585 << " const float a = " << std::scientific << base << ";\n"
1586 << " float b = fun(a);\n"
1587 << " if (abs(" << result << " - b) < " << std::abs(result * 0.001) << ")\n"
1588 << " {\n"
1589 << " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
1590 << " }\n"
1591 << " else\n"
1592 << " {\n"
1593 << " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
1594 << " }\n"
1595 << "}\n";
1596
1597 return stream.str();
1598}
1599
Jamie Madill4052dfc2015-05-06 15:18:49 -04001600// Covers the WebGL test 'glsl/bugs/pow-of-small-constant-in-user-defined-function'
Jamie Madill1048e432016-07-23 18:51:28 -04001601// See http://anglebug.com/851
1602TEST_P(GLSLTest, PowOfSmallConstant)
Jamie Madill4052dfc2015-05-06 15:18:49 -04001603{
Jamie Madill6c9503e2016-08-16 14:06:32 -04001604 std::vector<double> bads;
1605 for (int eps = -1; eps <= 1; ++eps)
1606 {
1607 for (int i = -4; i <= 5; ++i)
Jamie Madill4052dfc2015-05-06 15:18:49 -04001608 {
Jamie Madill6c9503e2016-08-16 14:06:32 -04001609 if (i >= -1 && i <= 1)
1610 continue;
1611 const double epsilon = 1.0e-8;
1612 double bad = static_cast<double>(i) + static_cast<double>(eps) * epsilon;
1613 bads.push_back(bad);
Jamie Madill4052dfc2015-05-06 15:18:49 -04001614 }
Jamie Madill6c9503e2016-08-16 14:06:32 -04001615 }
Jamie Madill4052dfc2015-05-06 15:18:49 -04001616
Jamie Madill6c9503e2016-08-16 14:06:32 -04001617 for (double bad : bads)
1618 {
1619 const std::string &fragmentShaderSource = GenerateSmallPowShader(1.0e-6, bad);
Jamie Madill4052dfc2015-05-06 15:18:49 -04001620
Jamie Madill6c9503e2016-08-16 14:06:32 -04001621 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShaderSource);
Jamie Madill4052dfc2015-05-06 15:18:49 -04001622
Jamie Madill6c9503e2016-08-16 14:06:32 -04001623 drawQuad(program.get(), "inputAttribute", 0.5f);
1624
1625 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
1626 EXPECT_GL_NO_ERROR();
1627 }
Jamie Madill4052dfc2015-05-06 15:18:49 -04001628}
Jamie Madillfa05f602015-05-07 13:47:11 -04001629
Cooper Partina5ef8d82015-08-19 14:52:21 -07001630// Test that fragment shaders which contain non-constant loop indexers and compiled for FL9_3 and
1631// below
1632// fail with a specific error message.
1633// Additionally test that the same fragment shader compiles successfully with feature levels greater
1634// than FL9_3.
1635TEST_P(GLSLTest, LoopIndexingValidation)
1636{
1637 const std::string fragmentShaderSource = SHADER_SOURCE
1638 (
1639 precision mediump float;
1640
1641 uniform float loopMax;
1642
1643 void main()
1644 {
1645 gl_FragColor = vec4(1, 0, 0, 1);
1646 for (float l = 0.0; l < loopMax; l++)
1647 {
1648 if (loopMax > 3.0)
1649 {
1650 gl_FragColor.a += 0.1;
1651 }
1652 }
1653 }
1654 );
1655
1656 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
1657
1658 const char *sourceArray[1] = {fragmentShaderSource.c_str()};
1659 glShaderSource(shader, 1, sourceArray, nullptr);
1660 glCompileShader(shader);
1661
1662 GLint compileResult;
1663 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
1664
1665 // If the test is configured to run limited to Feature Level 9_3, then it is
1666 // assumed that shader compilation will fail with an expected error message containing
1667 // "Loop index cannot be compared with non-constant expression"
Olli Etuaho814a54d2015-08-27 16:23:09 +03001668 if ((GetParam() == ES2_D3D11_FL9_3() || GetParam() == ES2_D3D9()))
Cooper Partina5ef8d82015-08-19 14:52:21 -07001669 {
1670 if (compileResult != 0)
1671 {
1672 FAIL() << "Shader compilation succeeded, expected failure";
1673 }
1674 else
1675 {
1676 GLint infoLogLength;
1677 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
1678
1679 std::string infoLog;
1680 infoLog.resize(infoLogLength);
1681 glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]);
1682
1683 if (infoLog.find("Loop index cannot be compared with non-constant expression") ==
1684 std::string::npos)
1685 {
1686 FAIL() << "Shader compilation failed with unexpected error message";
1687 }
1688 }
1689 }
1690 else
1691 {
1692 EXPECT_NE(0, compileResult);
1693 }
1694
1695 if (shader != 0)
1696 {
1697 glDeleteShader(shader);
1698 }
1699}
1700
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001701// Tests that the maximum uniforms count returned from querying GL_MAX_VERTEX_UNIFORM_VECTORS
1702// can actually be used.
1703TEST_P(GLSLTest, VerifyMaxVertexUniformVectors)
1704{
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001705 int maxUniforms = 10000;
1706 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
1707 EXPECT_GL_NO_ERROR();
1708 std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS = " << maxUniforms << std::endl;
1709
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001710 CompileGLSLWithUniformsAndSamplers(maxUniforms, 0, 0, 0, true);
1711}
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001712
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001713// Tests that the maximum uniforms count returned from querying GL_MAX_VERTEX_UNIFORM_VECTORS
1714// can actually be used along with the maximum number of texture samplers.
1715TEST_P(GLSLTest, VerifyMaxVertexUniformVectorsWithSamplers)
1716{
Geoff Lange0cc2a42016-01-20 10:58:17 -05001717 if (GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE ||
1718 GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)
1719 {
1720 std::cout << "Test disabled on OpenGL." << std::endl;
1721 return;
1722 }
1723
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001724 int maxUniforms = 10000;
1725 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
1726 EXPECT_GL_NO_ERROR();
1727 std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS = " << maxUniforms << std::endl;
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001728
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001729 int maxTextureImageUnits = 0;
1730 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001731
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001732 CompileGLSLWithUniformsAndSamplers(maxUniforms, 0, maxTextureImageUnits, 0, true);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001733}
1734
1735// Tests that the maximum uniforms count + 1 from querying GL_MAX_VERTEX_UNIFORM_VECTORS
1736// fails shader compilation.
1737TEST_P(GLSLTest, VerifyMaxVertexUniformVectorsExceeded)
1738{
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001739 int maxUniforms = 10000;
1740 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &maxUniforms);
1741 EXPECT_GL_NO_ERROR();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001742 std::cout << "Validating GL_MAX_VERTEX_UNIFORM_VECTORS + 1 = " << maxUniforms + 1 << std::endl;
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001743
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001744 CompileGLSLWithUniformsAndSamplers(maxUniforms + 1, 0, 0, 0, false);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001745}
1746
1747// Tests that the maximum uniforms count returned from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
1748// can actually be used.
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001749TEST_P(GLSLTest, VerifyMaxFragmentUniformVectors)
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001750{
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001751 int maxUniforms = 10000;
1752 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
1753 EXPECT_GL_NO_ERROR();
1754 std::cout << "Validating GL_MAX_FRAGMENT_UNIFORM_VECTORS = " << maxUniforms << std::endl;
1755
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001756 CompileGLSLWithUniformsAndSamplers(0, maxUniforms, 0, 0, true);
1757}
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001758
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001759// Tests that the maximum uniforms count returned from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
1760// can actually be used along with the maximum number of texture samplers.
1761TEST_P(GLSLTest, VerifyMaxFragmentUniformVectorsWithSamplers)
1762{
Geoff Lange0cc2a42016-01-20 10:58:17 -05001763 if (GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE ||
1764 GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE)
1765 {
1766 std::cout << "Test disabled on OpenGL." << std::endl;
1767 return;
1768 }
1769
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001770 int maxUniforms = 10000;
1771 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
1772 EXPECT_GL_NO_ERROR();
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001773
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001774 int maxTextureImageUnits = 0;
1775 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001776
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001777 CompileGLSLWithUniformsAndSamplers(0, maxUniforms, 0, maxTextureImageUnits, true);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001778}
1779
1780// Tests that the maximum uniforms count + 1 from querying GL_MAX_FRAGMENT_UNIFORM_VECTORS
1781// fails shader compilation.
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001782TEST_P(GLSLTest, VerifyMaxFragmentUniformVectorsExceeded)
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001783{
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001784 int maxUniforms = 10000;
1785 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &maxUniforms);
1786 EXPECT_GL_NO_ERROR();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001787 std::cout << "Validating GL_MAX_FRAGMENT_UNIFORM_VECTORS + 1 = " << maxUniforms + 1
1788 << std::endl;
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001789
Austin Kinross7a3e8e22015-10-08 15:50:06 -07001790 CompileGLSLWithUniformsAndSamplers(0, maxUniforms + 1, 0, 0, false);
Cooper Partin69f9b2c2015-08-20 13:25:41 -07001791}
1792
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001793// Test that two constructors which have vec4 and mat2 parameters get disambiguated (issue in
1794// HLSL).
1795TEST_P(GLSLTest_ES3, AmbiguousConstructorCall2x2)
1796{
1797 const std::string fragmentShaderSource =
1798 "#version 300 es\n"
1799 "precision highp float;\n"
1800 "out vec4 my_FragColor;\n"
1801 "void main()\n"
1802 "{\n"
1803 " my_FragColor = vec4(0.0);\n"
1804 "}";
1805
1806 const std::string vertexShaderSource =
1807 "#version 300 es\n"
1808 "precision highp float;\n"
1809 "in vec4 a_vec;\n"
1810 "in mat2 a_mat;\n"
1811 "void main()\n"
1812 "{\n"
1813 " gl_Position = vec4(a_vec) + vec4(a_mat);\n"
1814 "}";
1815
1816 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1817 EXPECT_NE(0u, program);
1818}
1819
1820// Test that two constructors which have mat2x3 and mat3x2 parameters get disambiguated.
1821// This was suspected to be an issue in HLSL, but HLSL seems to be able to natively choose between
1822// the function signatures in this case.
1823TEST_P(GLSLTest_ES3, AmbiguousConstructorCall2x3)
1824{
1825 const std::string fragmentShaderSource =
1826 "#version 300 es\n"
1827 "precision highp float;\n"
1828 "out vec4 my_FragColor;\n"
1829 "void main()\n"
1830 "{\n"
1831 " my_FragColor = vec4(0.0);\n"
1832 "}";
1833
1834 const std::string vertexShaderSource =
1835 "#version 300 es\n"
1836 "precision highp float;\n"
1837 "in mat3x2 a_matA;\n"
1838 "in mat2x3 a_matB;\n"
1839 "void main()\n"
1840 "{\n"
1841 " gl_Position = vec4(a_matA) + vec4(a_matB);\n"
1842 "}";
1843
1844 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1845 EXPECT_NE(0u, program);
1846}
1847
1848// Test that two functions which have vec4 and mat2 parameters get disambiguated (issue in HLSL).
1849TEST_P(GLSLTest_ES3, AmbiguousFunctionCall2x2)
1850{
1851 const std::string fragmentShaderSource =
1852 "#version 300 es\n"
1853 "precision highp float;\n"
1854 "out vec4 my_FragColor;\n"
1855 "void main()\n"
1856 "{\n"
1857 " my_FragColor = vec4(0.0);\n"
1858 "}";
1859
1860 const std::string vertexShaderSource =
1861 "#version 300 es\n"
1862 "precision highp float;\n"
1863 "in vec4 a_vec;\n"
1864 "in mat2 a_mat;\n"
1865 "vec4 foo(vec4 a)\n"
1866 "{\n"
1867 " return a;\n"
1868 "}\n"
1869 "vec4 foo(mat2 a)\n"
1870 "{\n"
1871 " return vec4(a[0][0]);\n"
1872 "}\n"
1873 "void main()\n"
1874 "{\n"
1875 " gl_Position = foo(a_vec) + foo(a_mat);\n"
1876 "}";
1877
1878 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1879 EXPECT_NE(0u, program);
1880}
1881
1882// Test that an user-defined function with a large number of float4 parameters doesn't fail due to
1883// the function name being too long.
1884TEST_P(GLSLTest_ES3, LargeNumberOfFloat4Parameters)
1885{
1886 const std::string fragmentShaderSource =
1887 "#version 300 es\n"
1888 "precision highp float;\n"
1889 "out vec4 my_FragColor;\n"
1890 "void main()\n"
1891 "{\n"
1892 " my_FragColor = vec4(0.0);\n"
1893 "}";
1894
1895 std::stringstream vertexShaderStream;
1896 const unsigned int paramCount = 1024u;
1897
1898 vertexShaderStream << "#version 300 es\n"
1899 "precision highp float;\n"
1900 "in vec4 a_vec;\n"
1901 "vec4 lotsOfVec4Parameters(";
1902 for (unsigned int i = 0; i < paramCount; ++i)
1903 {
1904 vertexShaderStream << "vec4 a" << i << ", ";
1905 }
1906 vertexShaderStream << "vec4 aLast)\n"
1907 "{\n"
1908 " return ";
1909 for (unsigned int i = 0; i < paramCount; ++i)
1910 {
1911 vertexShaderStream << "a" << i << " + ";
1912 }
1913 vertexShaderStream << "aLast;\n"
1914 "}\n"
1915 "void main()\n"
1916 "{\n"
1917 " gl_Position = lotsOfVec4Parameters(";
1918 for (unsigned int i = 0; i < paramCount; ++i)
1919 {
1920 vertexShaderStream << "a_vec, ";
1921 }
1922 vertexShaderStream << "a_vec);\n"
1923 "}";
1924
1925 GLuint program = CompileProgram(vertexShaderStream.str(), fragmentShaderSource);
1926 EXPECT_NE(0u, program);
1927}
1928
Olli Etuahod4f4c112016-04-15 15:11:24 +03001929// This test was written specifically to stress DeferGlobalInitializers AST transformation.
1930// Test a shader where a global constant array is initialized with an expression containing array
1931// indexing. This initializer is tricky to constant fold, so if it's not constant folded it needs to
1932// be handled in a way that doesn't generate statements in the global scope in HLSL output.
1933// Also includes multiple array initializers in one declaration, where only the second one has
1934// array indexing. This makes sure that the qualifier for the declaration is set correctly if
1935// transformations are applied to the declaration also in the case of ESSL output.
1936TEST_P(GLSLTest_ES3, InitGlobalArrayWithArrayIndexing)
1937{
Yuly Novikov41db2242016-06-25 00:14:28 -04001938 // TODO(ynovikov): re-enable once root cause of http://anglebug.com/1428 is fixed
1939 if (IsAndroid() && IsAdreno() && IsOpenGLES())
1940 {
1941 std::cout << "Test skipped on Adreno OpenGLES on Android." << std::endl;
1942 return;
1943 }
1944
Olli Etuahod4f4c112016-04-15 15:11:24 +03001945 const std::string vertexShaderSource =
1946 "#version 300 es\n"
1947 "precision highp float;\n"
1948 "in vec4 a_vec;\n"
1949 "void main()\n"
1950 "{\n"
1951 " gl_Position = vec4(a_vec);\n"
1952 "}";
1953
1954 const std::string fragmentShaderSource =
1955 "#version 300 es\n"
1956 "precision highp float;\n"
1957 "out vec4 my_FragColor;\n"
1958 "const highp float f[2] = float[2](0.1, 0.2);\n"
1959 "const highp float[2] g = float[2](0.3, 0.4), h = float[2](0.5, f[1]);\n"
1960 "void main()\n"
1961 "{\n"
1962 " my_FragColor = vec4(h[1]);\n"
1963 "}";
1964
1965 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
1966 EXPECT_NE(0u, program);
1967}
1968
Corentin Wallez419bfc92016-06-28 10:54:45 -07001969// Test that index-constant sampler array indexing is supported.
1970TEST_P(GLSLTest, IndexConstantSamplerArrayIndexing)
1971{
1972 if (IsD3D11_FL93()) {
1973 std::cout << "Test skipped on D3D11 FL 9.3." << std::endl;
1974 return;
1975 }
1976
1977 const std::string vertexShaderSource =
1978 "attribute vec4 vPosition;\n"
1979 "void main()\n"
1980 "{\n"
1981 " gl_Position = vPosition;\n"
1982 "}";
1983
1984 const std::string fragmentShaderSource =
1985 "precision mediump float;\n"
1986 "uniform sampler2D uni[2];\n"
1987 "\n"
1988 "float zero(int x)\n"
1989 "{\n"
1990 " return float(x) - float(x);\n"
1991 "}\n"
1992 "\n"
1993 "void main()\n"
1994 "{\n"
1995 " vec4 c = vec4(0,0,0,0);\n"
1996 " for (int ii = 1; ii < 3; ++ii) {\n"
1997 " if (c.x > 255.0) {\n"
1998 " c.x = 255.0 + zero(ii);\n"
1999 " break;\n"
2000 " }\n"
2001 // Index the sampler array with a predictable loop index (index-constant) as opposed to
2002 // a true constant. This is valid in OpenGL ES but isn't in many Desktop OpenGL versions,
2003 // without an extension.
2004 " c += texture2D(uni[ii - 1], vec2(0.5, 0.5));\n"
2005 " }\n"
2006 " gl_FragColor = c;\n"
2007 "}";
2008
2009 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
2010 EXPECT_NE(0u, program);
2011}
2012
Corentin Wallezb00dcee2016-07-11 17:42:58 -04002013// Test that the #pragma directive is supported and doesn't trigger a compilation failure on the
2014// native driver. The only pragma that gets passed to the OpenGL driver is "invariant" but we don't
2015// want to test its behavior, so don't use any varyings.
2016TEST_P(GLSLTest, PragmaDirective)
2017{
2018 const std::string vertexShaderSource =
2019 "#pragma STDGL invariant(all)\n"
2020 "void main()\n"
2021 "{\n"
2022 " gl_Position = vec4(1.0, 0.0, 0.0, 1.0);\n"
2023 "}\n";
2024
2025 const std::string fragmentShaderSource =
2026 "precision mediump float;\n"
2027 "void main()\n"
2028 "{\n"
2029 " gl_FragColor = vec4(1.0);\n"
2030 "}\n";
2031
2032 GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
2033 EXPECT_NE(0u, program);
2034}
2035
Olli Etuahoe1d199b2016-07-19 17:14:27 +03002036// Sequence operator evaluates operands from left to right (ESSL 3.00 section 5.9).
2037// The function call that returns the array needs to be evaluated after ++j for the expression to
2038// return the correct value (true).
2039TEST_P(GLSLTest_ES3, SequenceOperatorEvaluationOrderArray)
2040{
2041 const std::string &fragmentShaderSource =
2042 "#version 300 es\n"
2043 "precision mediump float;\n"
2044 "out vec4 my_FragColor; \n"
2045 "int[2] func(int param) {\n"
2046 " return int[2](param, param);\n"
2047 "}\n"
2048 "void main() {\n"
2049 " int a[2]; \n"
2050 " for (int i = 0; i < 2; ++i) {\n"
2051 " a[i] = 1;\n"
2052 " }\n"
2053 " int j = 0; \n"
2054 " bool result = ((++j), (a == func(j)));\n"
2055 " my_FragColor = vec4(0.0, (result ? 1.0 : 0.0), 0.0, 1.0);\n"
2056 "}\n";
2057
2058 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
2059 ASSERT_NE(0u, program);
2060
2061 drawQuad(program, "inputAttribute", 0.5f);
2062
2063 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2064}
2065
2066// Sequence operator evaluates operands from left to right (ESSL 3.00 section 5.9).
2067// The short-circuiting expression needs to be evaluated after ++j for the expression to return the
2068// correct value (true).
2069TEST_P(GLSLTest_ES3, SequenceOperatorEvaluationOrderShortCircuit)
2070{
2071 const std::string &fragmentShaderSource =
2072 "#version 300 es\n"
2073 "precision mediump float;\n"
2074 "out vec4 my_FragColor; \n"
2075 "void main() {\n"
2076 " int j = 0; \n"
2077 " bool result = ((++j), (j == 1 ? true : (++j == 3)));\n"
2078 " my_FragColor = vec4(0.0, ((result && j == 1) ? 1.0 : 0.0), 0.0, 1.0);\n"
2079 "}\n";
2080
2081 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
2082 ASSERT_NE(0u, program);
2083
2084 drawQuad(program, "inputAttribute", 0.5f);
2085
2086 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2087}
2088
Jamie Madill666f65a2016-08-26 01:34:37 +00002089// Sequence operator evaluates operands from left to right (ESSL 3.00 section 5.9).
2090// Indexing the vector needs to be evaluated after func() for the right result.
2091TEST_P(GLSLTest_ES3, SequenceOperatorEvaluationOrderDynamicVectorIndexingInLValue)
2092{
2093 const std::string &fragmentShaderSource =
2094 "#version 300 es\n"
2095 "precision mediump float;\n"
2096 "out vec4 my_FragColor;\n"
2097 "uniform int u_zero;\n"
2098 "int sideEffectCount = 0;\n"
2099 "float func() {\n"
2100 " ++sideEffectCount;\n"
2101 " return -1.0;\n"
2102 "}\n"
2103 "void main() {\n"
2104 " vec4 v = vec4(0.0, 2.0, 4.0, 6.0); \n"
2105 " float f = (func(), (++v[u_zero + sideEffectCount]));\n"
2106 " bool green = abs(f - 3.0) < 0.01 && abs(v[1] - 3.0) < 0.01 && sideEffectCount == 1;\n"
2107 " my_FragColor = vec4(0.0, (green ? 1.0 : 0.0), 0.0, 1.0);\n"
2108 "}\n";
2109
2110 GLuint program = CompileProgram(mSimpleVSSource, fragmentShaderSource);
2111 ASSERT_NE(0u, program);
2112
2113 drawQuad(program, "inputAttribute", 0.5f);
2114
2115 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2116}
2117
Jamie Madillc9bde922016-07-24 17:58:50 -04002118// Test that using gl_PointCoord with GL_TRIANGLES doesn't produce a link error.
2119// From WebGL test conformance/rendering/point-specific-shader-variables.html
2120// See http://anglebug.com/1380
2121TEST_P(GLSLTest, RenderTrisWithPointCoord)
2122{
2123 const std::string &vert =
2124 "attribute vec2 aPosition;\n"
2125 "void main()\n"
2126 "{\n"
2127 " gl_Position = vec4(aPosition, 0, 1);\n"
2128 " gl_PointSize = 1.0;\n"
2129 "}";
2130 const std::string &frag =
2131 "void main()\n"
2132 "{\n"
2133 " gl_FragColor = vec4(gl_PointCoord.xy, 0, 1);\n"
2134 " gl_FragColor = vec4(0, 1, 0, 1);\n"
2135 "}";
2136
2137 ANGLE_GL_PROGRAM(prog, vert, frag);
2138 drawQuad(prog.get(), "aPosition", 0.5f);
2139 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2140}
2141
Jamie Madill5655b842016-08-02 11:00:07 -04002142// Convers a bug with the integer pow statement workaround.
2143TEST_P(GLSLTest, NestedPowStatements)
2144{
2145 const std::string &vert =
2146 "attribute vec2 position;\n"
2147 "void main()\n"
2148 "{\n"
2149 " gl_Position = vec4(position, 0, 1);\n"
2150 "}";
2151 const std::string &frag =
2152 "precision mediump float;\n"
2153 "float func(float v)\n"
2154 "{\n"
2155 " float f1 = pow(v, 2.0);\n"
2156 " return pow(f1 + v, 2.0);\n"
2157 "}\n"
2158 "void main()\n"
2159 "{\n"
2160 " float v = func(2.0);\n"
2161 " gl_FragColor = abs(v - 36.0) < 0.001 ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);\n"
2162 "}";
2163
2164 ANGLE_GL_PROGRAM(prog, vert, frag);
2165 drawQuad(prog.get(), "position", 0.5f);
2166 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2167}
2168
Qiankun Miaof52fe932016-12-07 13:39:15 +08002169// Test that -float calculation is correct.
2170TEST_P(GLSLTest_ES3, UnaryMinusOperatorFloat)
2171{
Qiankun Miaof52fe932016-12-07 13:39:15 +08002172 const std::string &vert =
2173 "#version 300 es\n"
2174 "in highp vec4 position;\n"
2175 "void main() {\n"
2176 " gl_Position = position;\n"
2177 "}\n";
2178 const std::string &frag =
2179 "#version 300 es\n"
2180 "out highp vec4 o_color;\n"
2181 "void main() {\n"
2182 " highp float f = -1.0;\n"
2183 " // atan(tan(0.5), -f) should be 0.5.\n"
2184 " highp float v = atan(tan(0.5), -f);\n"
2185 " o_color = abs(v - 0.5) < 0.001 ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);\n"
2186 "}\n";
2187
2188 ANGLE_GL_PROGRAM(prog, vert, frag);
2189 drawQuad(prog.get(), "position", 0.5f);
2190 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2191}
2192
Olli Etuahoda9fb092016-12-09 17:32:29 +00002193// Test that atan(vec2, vec2) calculation is correct.
2194TEST_P(GLSLTest_ES3, AtanVec2)
2195{
2196 const std::string &vert =
2197 "#version 300 es\n"
2198 "in highp vec4 position;\n"
2199 "void main() {\n"
2200 " gl_Position = position;\n"
2201 "}\n";
2202 const std::string &frag =
2203 "#version 300 es\n"
2204 "out highp vec4 o_color;\n"
2205 "void main() {\n"
2206 " highp float f = 1.0;\n"
2207 " // atan(tan(0.5), f) should be 0.5.\n"
2208 " highp vec2 v = atan(vec2(tan(0.5)), vec2(f));\n"
2209 " o_color = (abs(v[0] - 0.5) < 0.001 && abs(v[1] - 0.5) < 0.001) ? vec4(0, 1, 0, 1) : "
2210 "vec4(1, 0, 0, 1);\n"
2211 "}\n";
2212
2213 ANGLE_GL_PROGRAM(prog, vert, frag);
2214 drawQuad(prog.get(), "position", 0.5f);
2215 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2216}
2217
Jiawei-Shaoe292e902016-09-07 10:49:01 +08002218// Convers a bug with the unary minus operator on signed integer workaround.
2219TEST_P(GLSLTest_ES3, UnaryMinusOperatorSignedInt)
2220{
2221 const std::string &vert =
2222 "#version 300 es\n"
2223 "in highp vec4 position;\n"
2224 "out mediump vec4 v_color;\n"
2225 "uniform int ui_one;\n"
2226 "uniform int ui_two;\n"
2227 "uniform int ui_three;\n"
2228 "void main() {\n"
2229 " int s[3];\n"
2230 " s[0] = ui_one;\n"
2231 " s[1] = -(-(-ui_two + 1) + 1);\n" // s[1] = -ui_two
2232 " s[2] = ui_three;\n"
2233 " int result = 0;\n"
2234 " for (int i = 0; i < ui_three; i++) {\n"
2235 " result += s[i];\n"
2236 " }\n"
2237 " v_color = (result == 2) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);\n"
2238 " gl_Position = position;\n"
2239 "}\n";
2240 const std::string &frag =
2241 "#version 300 es\n"
2242 "in mediump vec4 v_color;\n"
2243 "layout(location=0) out mediump vec4 o_color;\n"
2244 "void main() {\n"
2245 " o_color = v_color;\n"
2246 "}\n";
2247
2248 ANGLE_GL_PROGRAM(prog, vert, frag);
2249
Jamie Madille1faacb2016-12-13 12:42:14 -05002250 GLint oneIndex = glGetUniformLocation(prog.get(), "ui_one");
Jiawei-Shaoe292e902016-09-07 10:49:01 +08002251 ASSERT_NE(-1, oneIndex);
Jamie Madille1faacb2016-12-13 12:42:14 -05002252 GLint twoIndex = glGetUniformLocation(prog.get(), "ui_two");
Jiawei-Shaoe292e902016-09-07 10:49:01 +08002253 ASSERT_NE(-1, twoIndex);
Jamie Madille1faacb2016-12-13 12:42:14 -05002254 GLint threeIndex = glGetUniformLocation(prog.get(), "ui_three");
Jiawei-Shaoe292e902016-09-07 10:49:01 +08002255 ASSERT_NE(-1, threeIndex);
2256 glUseProgram(prog.get());
2257 glUniform1i(oneIndex, 1);
2258 glUniform1i(twoIndex, 2);
2259 glUniform1i(threeIndex, 3);
2260
2261 drawQuad(prog.get(), "position", 0.5f);
2262 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2263}
2264
2265// Convers a bug with the unary minus operator on unsigned integer workaround.
2266TEST_P(GLSLTest_ES3, UnaryMinusOperatorUnsignedInt)
2267{
2268 const std::string &vert =
2269 "#version 300 es\n"
2270 "in highp vec4 position;\n"
2271 "out mediump vec4 v_color;\n"
2272 "uniform uint ui_one;\n"
2273 "uniform uint ui_two;\n"
2274 "uniform uint ui_three;\n"
2275 "void main() {\n"
2276 " uint s[3];\n"
2277 " s[0] = ui_one;\n"
2278 " s[1] = -(-(-ui_two + 1u) + 1u);\n" // s[1] = -ui_two
2279 " s[2] = ui_three;\n"
2280 " uint result = 0u;\n"
2281 " for (uint i = 0u; i < ui_three; i++) {\n"
2282 " result += s[i];\n"
2283 " }\n"
2284 " v_color = (result == 2u) ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);\n"
2285 " gl_Position = position;\n"
2286 "}\n";
2287 const std::string &frag =
2288 "#version 300 es\n"
2289 "in mediump vec4 v_color;\n"
2290 "layout(location=0) out mediump vec4 o_color;\n"
2291 "void main() {\n"
2292 " o_color = v_color;\n"
2293 "}\n";
2294
2295 ANGLE_GL_PROGRAM(prog, vert, frag);
2296
Jamie Madille1faacb2016-12-13 12:42:14 -05002297 GLint oneIndex = glGetUniformLocation(prog.get(), "ui_one");
Jiawei-Shaoe292e902016-09-07 10:49:01 +08002298 ASSERT_NE(-1, oneIndex);
Jamie Madille1faacb2016-12-13 12:42:14 -05002299 GLint twoIndex = glGetUniformLocation(prog.get(), "ui_two");
Jiawei-Shaoe292e902016-09-07 10:49:01 +08002300 ASSERT_NE(-1, twoIndex);
Jamie Madille1faacb2016-12-13 12:42:14 -05002301 GLint threeIndex = glGetUniformLocation(prog.get(), "ui_three");
Jiawei-Shaoe292e902016-09-07 10:49:01 +08002302 ASSERT_NE(-1, threeIndex);
2303 glUseProgram(prog.get());
2304 glUniform1ui(oneIndex, 1u);
2305 glUniform1ui(twoIndex, 2u);
2306 glUniform1ui(threeIndex, 3u);
2307
2308 drawQuad(prog.get(), "position", 0.5f);
2309 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2310}
2311
Olli Etuahoab481642016-08-26 12:09:10 +03002312// Test a nested sequence operator with a ternary operator inside. The ternary operator is
2313// intended to be such that it gets converted to an if statement on the HLSL backend.
2314TEST_P(GLSLTest, NestedSequenceOperatorWithTernaryInside)
2315{
2316 const std::string &vert =
2317 "attribute vec2 position;\n"
2318 "void main()\n"
2319 "{\n"
2320 " gl_Position = vec4(position, 0, 1);\n"
2321 "}";
2322
2323 // Note that the uniform keep_flop_positive doesn't need to be set - the test expects it to have
2324 // its default value false.
2325 const std::string &frag =
2326 "precision mediump float;\n"
2327 "uniform bool keep_flop_positive;\n"
2328 "float flop;\n"
2329 "void main() {\n"
2330 " flop = -1.0,\n"
2331 " (flop *= -1.0,\n"
2332 " keep_flop_positive ? 0.0 : flop *= -1.0),\n"
2333 " gl_FragColor = vec4(0, -flop, 0, 1);\n"
2334 "}";
2335
2336 ANGLE_GL_PROGRAM(prog, vert, frag);
2337 drawQuad(prog.get(), "position", 0.5f);
2338 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2339}
2340
Geoff Lang28a97ee2016-09-22 13:01:26 -04002341// Test that using a sampler2D and samplerExternalOES in the same shader works (anglebug.com/1534)
2342TEST_P(GLSLTest, ExternalAnd2DSampler)
2343{
2344 if (!extensionEnabled("GL_OES_EGL_image_external"))
2345 {
2346 std::cout << "Test skipped because GL_OES_EGL_image_external is not available."
2347 << std::endl;
2348 return;
2349 }
2350
2351 const std::string fragmentShader =
2352 "precision mediump float;\n"
2353 "uniform samplerExternalOES tex0;\n"
2354 "uniform sampler2D tex1;\n"
2355 "void main(void)\n"
2356 "{\n"
2357 " vec2 uv = vec2(0.0, 0.0);"
2358 " gl_FragColor = texture2D(tex0, uv) + texture2D(tex1, uv);\n"
2359 "}\n";
2360
2361 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShader);
2362}
2363
Jamie Madill44ebf6b2016-09-22 13:00:02 -04002364// Test that using an invalid constant right-shift produces an error.
2365TEST_P(GLSLTest_ES3, FoldedInvalidRightShift)
2366{
2367 const std::string &fragmentShader =
2368 "#version 300 es\n"
2369 "precision mediump float;\n"
2370 "out vec4 color;\n"
2371 "void main(void)\n"
2372 "{\n"
2373 " int diff = -100 >> -100;\n"
2374 " color = vec4(float(diff));\n"
2375 "}\n";
2376
2377 GLuint program = CompileProgram(mSimpleVSSource, fragmentShader);
2378 EXPECT_EQ(0u, program);
2379 glDeleteProgram(program);
2380}
2381
2382// Test that using an invalid constant left-shift produces an error.
2383TEST_P(GLSLTest_ES3, FoldedInvalidLeftShift)
2384{
2385 const std::string &fragmentShader =
2386 "#version 300 es\n"
2387 "precision mediump float;\n"
2388 "out vec4 color;\n"
2389 "void main(void)\n"
2390 "{\n"
2391 " int diff = -100 << -100;\n"
2392 " color = vec4(float(diff));\n"
2393 "}\n";
2394
2395 GLuint program = CompileProgram(mSimpleVSSource, fragmentShader);
2396 EXPECT_EQ(0u, program);
2397 glDeleteProgram(program);
2398}
2399
Olli Etuaho56a2f952016-12-08 12:16:27 +00002400// Test that literal infinity can be written out from the shader translator.
2401// A similar test can't be made for NaNs, since ESSL 3.00.6 requirements for NaNs are very loose.
2402TEST_P(GLSLTest_ES3, LiteralInfinityOutput)
2403{
2404 const std::string &fragmentShader =
2405 "#version 300 es\n"
2406 "precision highp float;\n"
2407 "out vec4 out_color;\n"
2408 "uniform float u;\n"
2409 "void main()\n"
2410 "{\n"
2411 " float infVar = 1.0e40 - u;\n"
2412 " bool correct = isinf(infVar) && infVar > 0.0;\n"
2413 " out_color = correct ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0);\n"
2414 "}\n";
2415
2416 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShader);
2417 drawQuad(program.get(), "inputAttribute", 0.5f);
2418 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2419}
2420
2421// Test that literal negative infinity can be written out from the shader translator.
2422// A similar test can't be made for NaNs, since ESSL 3.00.6 requirements for NaNs are very loose.
2423TEST_P(GLSLTest_ES3, LiteralNegativeInfinityOutput)
2424{
2425 const std::string &fragmentShader =
2426 "#version 300 es\n"
2427 "precision highp float;\n"
2428 "out vec4 out_color;\n"
2429 "uniform float u;\n"
2430 "void main()\n"
2431 "{\n"
2432 " float infVar = -1.0e40 + u;\n"
2433 " bool correct = isinf(infVar) && infVar < 0.0;\n"
2434 " out_color = correct ? vec4(0.0, 1.0, 0.0, 1.0) : vec4(1.0, 0.0, 0.0, 1.0);\n"
2435 "}\n";
2436
2437 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShader);
2438 drawQuad(program.get(), "inputAttribute", 0.5f);
2439 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2440}
2441
Corentin Wallez36fd1002016-12-08 11:30:44 -05002442// The following MultipleDeclaration* tests are testing TranslatorHLSL specific simplification
2443// passes. Because the interaction of multiple passes must be tested, it is difficult to write
2444// a unittest for them. Instead we add the tests as end2end so will in particular test
2445// TranslatorHLSL when run on Windows.
2446
2447// Test that passes splitting multiple declarations and comma operators are correctly ordered.
2448TEST_P(GLSLTest_ES3, MultipleDeclarationWithCommaOperator)
2449{
2450 const std::string &fragmentShader =
2451 "#version 300 es\n"
2452 "precision mediump float;\n"
2453 "out vec4 color;\n"
2454 "void main(void)\n"
2455 "{\n"
2456 " float a = 0.0, b = ((gl_FragCoord.x < 0.5 ? a : 0.0), 1.0);\n"
2457 " color = vec4(b);\n"
2458 "}\n";
2459
2460 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShader);
2461}
2462
2463// Test that passes splitting multiple declarations and comma operators and for loops are
2464// correctly ordered.
2465TEST_P(GLSLTest_ES3, MultipleDeclarationWithCommaOperatorInForLoop)
2466{
2467 const std::string &fragmentShader =
2468 "#version 300 es\n"
2469 "precision mediump float;\n"
2470 "out vec4 color;\n"
2471 "void main(void)\n"
2472 "{\n"
2473 " for(float a = 0.0, b = ((gl_FragCoord.x < 0.5 ? a : 0.0), 1.0); a < 10.0; a++)\n"
2474 " {\n"
2475 " b += 1.0;\n"
2476 " color = vec4(b);\n"
2477 " }\n"
2478 "}\n";
2479
2480 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShader);
2481}
2482
2483// Test that splitting multiple declaration in for loops works with no loop condition
2484TEST_P(GLSLTest_ES3, MultipleDeclarationInForLoopEmptyCondition)
2485{
2486 const std::string &fragmentShader =
2487 "#version 300 es\n"
2488 "precision mediump float;\n"
2489 "out vec4 color;\n"
2490 "void main(void)\n"
2491 "{\n"
2492 " for(float a = 0.0, b = 1.0;; a++)\n"
2493 " {\n"
2494 " b += 1.0;\n"
2495 " if (a > 10.0) {break;}\n"
2496 " color = vec4(b);\n"
2497 " }\n"
2498 "}\n";
2499
2500 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShader);
2501}
2502
2503// Test that splitting multiple declaration in for loops works with no loop expression
2504TEST_P(GLSLTest_ES3, MultipleDeclarationInForLoopEmptyExpression)
2505{
2506 const std::string &fragmentShader =
2507 "#version 300 es\n"
2508 "precision mediump float;\n"
2509 "out vec4 color;\n"
2510 "void main(void)\n"
2511 "{\n"
2512 " for(float a = 0.0, b = 1.0; a < 10.0;)\n"
2513 " {\n"
2514 " b += 1.0;\n"
2515 " a += 1.0;\n"
2516 " color = vec4(b);\n"
2517 " }\n"
2518 "}\n";
2519
2520 ANGLE_GL_PROGRAM(program, mSimpleVSSource, fragmentShader);
2521}
2522
Jamie Madill6c9503e2016-08-16 14:06:32 -04002523} // anonymous namespace
2524
Jamie Madillfa05f602015-05-07 13:47:11 -04002525// 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 -05002526ANGLE_INSTANTIATE_TEST(GLSLTest,
2527 ES2_D3D9(),
2528 ES2_D3D11(),
2529 ES2_D3D11_FL9_3(),
2530 ES2_OPENGL(),
2531 ES3_OPENGL(),
2532 ES2_OPENGLES(),
2533 ES3_OPENGLES());
Jamie Madillfa05f602015-05-07 13:47:11 -04002534
2535// 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 -05002536ANGLE_INSTANTIATE_TEST(GLSLTest_ES3, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGLES());