blob: 600697ded1a7f59af924179ba0cad755aa6c2c06 [file] [log] [blame]
Geoff Langc287ea62016-09-16 14:46:51 -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
7// WebGLCompatibilityTest.cpp : Tests of the GL_ANGLE_webgl_compatibility extension.
8
9#include "test_utils/ANGLETest.h"
10
11#include "test_utils/gl_raii.h"
12
Frank Henigman146e8a12017-03-02 23:22:37 -050013namespace
14{
15
16bool ConstantColorAndAlphaBlendFunctions(GLenum first, GLenum second)
17{
18 return (first == GL_CONSTANT_COLOR || first == GL_ONE_MINUS_CONSTANT_COLOR) &&
19 (second == GL_CONSTANT_ALPHA || second == GL_ONE_MINUS_CONSTANT_ALPHA);
20}
21
22void CheckBlendFunctions(GLenum src, GLenum dst)
23{
24 if (ConstantColorAndAlphaBlendFunctions(src, dst) ||
25 ConstantColorAndAlphaBlendFunctions(dst, src))
26 {
27 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
28 }
29 else
30 {
31 ASSERT_GL_NO_ERROR();
32 }
33}
34
35} // namespace
36
Geoff Langc287ea62016-09-16 14:46:51 -040037namespace angle
38{
39
40class WebGLCompatibilityTest : public ANGLETest
41{
42 protected:
43 WebGLCompatibilityTest()
44 {
45 setWindowWidth(128);
46 setWindowHeight(128);
47 setConfigRedBits(8);
48 setConfigGreenBits(8);
49 setConfigBlueBits(8);
50 setConfigAlphaBits(8);
51 setWebGLCompatibilityEnabled(true);
52 }
53
54 void SetUp() override
55 {
56 ANGLETest::SetUp();
Geoff Langc339c4e2016-11-29 10:37:36 -050057 glRequestExtensionANGLE = reinterpret_cast<PFNGLREQUESTEXTENSIONANGLEPROC>(
58 eglGetProcAddress("glRequestExtensionANGLE"));
Geoff Langc287ea62016-09-16 14:46:51 -040059 }
60
61 void TearDown() override { ANGLETest::TearDown(); }
62
Jamie Madillcad97ee2017-02-02 18:52:44 -050063 // Called from RenderingFeedbackLoopWithDrawBuffersEXT.
64 void drawBuffersEXTFeedbackLoop(GLuint program,
65 const std::array<GLenum, 2> &drawBuffers,
66 GLenum expectedError);
67
Jamie Madill07be8bf2017-02-02 19:59:57 -050068 // Called from RenderingFeedbackLoopWithDrawBuffers.
69 void drawBuffersFeedbackLoop(GLuint program,
70 const std::array<GLenum, 2> &drawBuffers,
71 GLenum expectedError);
72
Geoff Langc339c4e2016-11-29 10:37:36 -050073 PFNGLREQUESTEXTENSIONANGLEPROC glRequestExtensionANGLE = nullptr;
Geoff Langc287ea62016-09-16 14:46:51 -040074};
75
Corentin Wallezfd456442016-12-21 17:57:00 -050076class WebGL2CompatibilityTest : public WebGLCompatibilityTest
77{
78};
79
Geoff Langc287ea62016-09-16 14:46:51 -040080// Context creation would fail if EGL_ANGLE_create_context_webgl_compatibility was not available so
81// the GL extension should always be present
82TEST_P(WebGLCompatibilityTest, ExtensionStringExposed)
83{
84 EXPECT_TRUE(extensionEnabled("GL_ANGLE_webgl_compatibility"));
85}
86
87// Verify that all extension entry points are available
88TEST_P(WebGLCompatibilityTest, EntryPoints)
89{
Geoff Langc339c4e2016-11-29 10:37:36 -050090 if (extensionEnabled("GL_ANGLE_request_extension"))
Geoff Langc287ea62016-09-16 14:46:51 -040091 {
Geoff Langc339c4e2016-11-29 10:37:36 -050092 EXPECT_NE(nullptr, eglGetProcAddress("glRequestExtensionANGLE"));
Geoff Langc287ea62016-09-16 14:46:51 -040093 }
94}
95
96// WebGL 1 allows GL_DEPTH_STENCIL_ATTACHMENT as a valid binding point. Make sure it is usable,
97// even in ES2 contexts.
98TEST_P(WebGLCompatibilityTest, DepthStencilBindingPoint)
99{
100 GLRenderbuffer renderbuffer;
101 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer.get());
102 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 32, 32);
103
104 GLFramebuffer framebuffer;
105 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
106 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
107 renderbuffer.get());
108
109 EXPECT_GL_NO_ERROR();
110}
111
112// Test that attempting to enable an extension that doesn't exist generates GL_INVALID_OPERATION
113TEST_P(WebGLCompatibilityTest, EnableExtensionValidation)
114{
Geoff Langc339c4e2016-11-29 10:37:36 -0500115 glRequestExtensionANGLE("invalid_extension_string");
Geoff Langc287ea62016-09-16 14:46:51 -0400116 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
117}
118
119// Test enabling the GL_OES_element_index_uint extension
120TEST_P(WebGLCompatibilityTest, EnableExtensionUintIndices)
121{
122 if (getClientMajorVersion() != 2)
123 {
124 // This test only works on ES2 where uint indices are not available by default
125 return;
126 }
127
128 EXPECT_FALSE(extensionEnabled("GL_OES_element_index_uint"));
129
130 GLBuffer indexBuffer;
131 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
132
133 GLuint data[] = {0, 1, 2, 1, 3, 2};
134 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
135
136 ANGLE_GL_PROGRAM(program, "void main() { gl_Position = vec4(0, 0, 0, 1); }",
137 "void main() { gl_FragColor = vec4(0, 1, 0, 1); }")
138 glUseProgram(program.get());
139
140 glDrawElements(GL_TRIANGLES, 2, GL_UNSIGNED_INT, nullptr);
141 EXPECT_GL_ERROR(GL_INVALID_ENUM);
142
Geoff Langc339c4e2016-11-29 10:37:36 -0500143 if (extensionRequestable("GL_OES_element_index_uint"))
Geoff Langc287ea62016-09-16 14:46:51 -0400144 {
Geoff Langc339c4e2016-11-29 10:37:36 -0500145 glRequestExtensionANGLE("GL_OES_element_index_uint");
Geoff Langc287ea62016-09-16 14:46:51 -0400146 EXPECT_GL_NO_ERROR();
147 EXPECT_TRUE(extensionEnabled("GL_OES_element_index_uint"));
148
149 glDrawElements(GL_TRIANGLES, 2, GL_UNSIGNED_INT, nullptr);
150 EXPECT_GL_NO_ERROR();
151 }
152}
153
Bryan Bernhart87c182e2016-11-02 11:23:22 -0700154// Verify that shaders are of a compatible spec when the extension is enabled.
155TEST_P(WebGLCompatibilityTest, ExtensionCompilerSpec)
156{
157 EXPECT_TRUE(extensionEnabled("GL_ANGLE_webgl_compatibility"));
158
159 // Use of reserved _webgl prefix should fail when the shader specification is for WebGL.
160 const std::string &vert =
161 "struct Foo {\n"
162 " int _webgl_bar;\n"
163 "};\n"
164 "void main()\n"
165 "{\n"
166 " Foo foo = Foo(1);\n"
167 "}";
168
169 // Default fragement shader.
170 const std::string &frag =
171 "void main()\n"
172 "{\n"
173 " gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n"
174 "}";
175
176 GLuint program = CompileProgram(vert, frag);
177 EXPECT_EQ(0u, program);
178 glDeleteProgram(program);
179}
180
Corentin Wallez327411e2016-12-09 11:09:17 -0500181// Test that client-side array buffers are forbidden in WebGL mode
182TEST_P(WebGLCompatibilityTest, ForbidsClientSideArrayBuffer)
183{
184 const std::string &vert =
185 "attribute vec3 a_pos;\n"
186 "void main()\n"
187 "{\n"
188 " gl_Position = vec4(a_pos, 1.0);\n"
189 "}\n";
190
191 const std::string &frag =
192 "precision highp float;\n"
193 "void main()\n"
194 "{\n"
195 " gl_FragColor = vec4(1.0);\n"
196 "}\n";
197
198 ANGLE_GL_PROGRAM(program, vert, frag);
199
200 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
201 ASSERT_NE(-1, posLocation);
202 glUseProgram(program.get());
203
204 const auto &vertices = GetQuadVertices();
Corentin Wallezfd456442016-12-21 17:57:00 -0500205 glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 4, vertices.data());
Corentin Wallez327411e2016-12-09 11:09:17 -0500206 glEnableVertexAttribArray(posLocation);
207
208 ASSERT_GL_NO_ERROR();
209 glDrawArrays(GL_TRIANGLES, 0, 6);
210 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
211}
212
213// Test that client-side element array buffers are forbidden in WebGL mode
214TEST_P(WebGLCompatibilityTest, ForbidsClientSideElementBuffer)
215{
216 const std::string &vert =
217 "attribute vec3 a_pos;\n"
218 "void main()\n"
219 "{\n"
220 " gl_Position = vec4(a_pos, 1.0);\n"
221 "}\n";
222
223 const std::string &frag =
224 "precision highp float;\n"
225 "void main()\n"
226 "{\n"
227 " gl_FragColor = vec4(1.0);\n"
228 "}\n";
229
230 ANGLE_GL_PROGRAM(program, vert, frag);
231
232 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
233 ASSERT_NE(-1, posLocation);
234 glUseProgram(program.get());
235
236 const auto &vertices = GetQuadVertices();
237
238 GLBuffer vertexBuffer;
239 glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.get());
240 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(),
241 GL_STATIC_DRAW);
242
243 glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
244 glEnableVertexAttribArray(posLocation);
245
246 const GLubyte indices[] = {0, 1, 2, 3, 4, 5};
247
248 ASSERT_GL_NO_ERROR();
249 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
250 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
251}
252
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500253// Tests the WebGL requirement of having the same stencil mask, writemask and ref for fron and back
254TEST_P(WebGLCompatibilityTest, RequiresSameStencilMaskAndRef)
255{
256 // Run the test in an FBO to make sure we have some stencil bits.
257 GLRenderbuffer renderbuffer;
258 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer.get());
259 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 32, 32);
260
261 GLFramebuffer framebuffer;
262 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
263 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
264 renderbuffer.get());
265
266 ANGLE_GL_PROGRAM(program, "void main() { gl_Position = vec4(0, 0, 0, 1); }",
267 "void main() { gl_FragColor = vec4(0, 1, 0, 1); }")
268 glUseProgram(program.get());
269 ASSERT_GL_NO_ERROR();
270
271 // Having ref and mask the same for front and back is valid.
272 glStencilMask(255);
273 glStencilFunc(GL_ALWAYS, 0, 255);
274 glDrawArrays(GL_TRIANGLES, 0, 6);
275 ASSERT_GL_NO_ERROR();
276
277 // Having a different front - back write mask generates an error.
278 glStencilMaskSeparate(GL_FRONT, 1);
279 glDrawArrays(GL_TRIANGLES, 0, 6);
280 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
281
282 // Setting both write masks separately to the same value is valid.
283 glStencilMaskSeparate(GL_BACK, 1);
284 glDrawArrays(GL_TRIANGLES, 0, 6);
285 ASSERT_GL_NO_ERROR();
286
287 // Having a different stencil front - back mask generates an error
288 glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 0, 1);
289 glDrawArrays(GL_TRIANGLES, 0, 6);
290 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
291
292 // Setting both masks separately to the same value is valid.
293 glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 0, 1);
294 glDrawArrays(GL_TRIANGLES, 0, 6);
295 ASSERT_GL_NO_ERROR();
296
297 // Having a different stencil front - back reference generates an error
298 glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 255, 1);
299 glDrawArrays(GL_TRIANGLES, 0, 6);
300 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
301
302 // Setting both references separately to the same value is valid.
303 glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 255, 1);
304 glDrawArrays(GL_TRIANGLES, 0, 6);
305 ASSERT_GL_NO_ERROR();
306
307 // Using different stencil funcs, everything being equal is valid.
308 glStencilFuncSeparate(GL_BACK, GL_NEVER, 255, 1);
309 glDrawArrays(GL_TRIANGLES, 0, 6);
310 ASSERT_GL_NO_ERROR();
311}
312
Corentin Wallez506fc9c2016-12-21 16:53:33 -0500313// Test that GL_FIXED is forbidden
314TEST_P(WebGLCompatibilityTest, ForbidsGLFixed)
315{
316 GLBuffer buffer;
317 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
318 glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
319
320 glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
321 ASSERT_GL_NO_ERROR();
322
323 glVertexAttribPointer(0, 1, GL_FIXED, GL_FALSE, 0, nullptr);
324 EXPECT_GL_ERROR(GL_INVALID_ENUM);
325}
326
327// Test the WebGL limit of 255 for the attribute stride
328TEST_P(WebGLCompatibilityTest, MaxStride)
329{
330 GLBuffer buffer;
331 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
332 glBufferData(GL_ARRAY_BUFFER, 1024, nullptr, GL_STATIC_DRAW);
333
334 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 255, nullptr);
335 ASSERT_GL_NO_ERROR();
336
337 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 256, nullptr);
338 EXPECT_GL_ERROR(GL_INVALID_VALUE);
339}
340
Corentin Wallezfd456442016-12-21 17:57:00 -0500341// Test the checks for OOB reads in the vertex buffers, non-instanced version
342TEST_P(WebGLCompatibilityTest, DrawArraysBufferOutOfBoundsNonInstanced)
343{
344 const std::string &vert =
345 "attribute float a_pos;\n"
346 "void main()\n"
347 "{\n"
348 " gl_Position = vec4(a_pos, a_pos, a_pos, 1.0);\n"
349 "}\n";
350
351 const std::string &frag =
352 "precision highp float;\n"
353 "void main()\n"
354 "{\n"
355 " gl_FragColor = vec4(1.0);\n"
356 "}\n";
357
358 ANGLE_GL_PROGRAM(program, vert, frag);
359
360 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
361 ASSERT_NE(-1, posLocation);
362 glUseProgram(program.get());
363
364 GLBuffer buffer;
365 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
366 glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
367
368 glEnableVertexAttribArray(posLocation);
369
370 const uint8_t* zeroOffset = nullptr;
371
372 // Test touching the last element is valid.
373 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 12);
374 glDrawArrays(GL_POINTS, 0, 4);
375 ASSERT_GL_NO_ERROR();
376
377 // Test touching the last element + 1 is invalid.
378 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 13);
379 glDrawArrays(GL_POINTS, 0, 4);
380 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
381
382 // Test touching the last element is valid, using a stride.
383 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 9);
384 glDrawArrays(GL_POINTS, 0, 4);
385 ASSERT_GL_NO_ERROR();
386
387 // Test touching the last element + 1 is invalid, using a stride.
388 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 10);
389 glDrawArrays(GL_POINTS, 0, 4);
390 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
391
392 // Test any offset is valid if no vertices are drawn.
393 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 32);
394 glDrawArrays(GL_POINTS, 0, 0);
395 ASSERT_GL_NO_ERROR();
396}
397
Corentin Wallez0844f2d2017-01-31 17:02:59 -0500398// Test the checks for OOB reads in the index buffer
399TEST_P(WebGLCompatibilityTest, DrawElementsBufferOutOfBoundsInIndexBuffer)
Geoff Lang5f319a42017-01-09 16:49:19 -0500400{
Corentin Wallez0844f2d2017-01-31 17:02:59 -0500401 const std::string &vert =
402 "attribute float a_pos;\n"
403 "void main()\n"
404 "{\n"
405 " gl_Position = vec4(a_pos, a_pos, a_pos, 1.0);\n"
406 "}\n";
Geoff Lang5f319a42017-01-09 16:49:19 -0500407
Corentin Wallez0844f2d2017-01-31 17:02:59 -0500408 const std::string &frag =
409 "precision highp float;\n"
410 "void main()\n"
411 "{\n"
412 " gl_FragColor = vec4(1.0);\n"
413 "}\n";
414
415 ANGLE_GL_PROGRAM(program, vert, frag);
416
417 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
418 ASSERT_NE(-1, posLocation);
419 glUseProgram(program.get());
420
421 GLBuffer vertexBuffer;
422 glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.get());
423 glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
424
425 glEnableVertexAttribArray(posLocation);
426
427 const uint8_t *zeroOffset = nullptr;
428 const uint8_t zeroIndices[] = {0, 0, 0, 0, 0, 0, 0, 0};
429
430 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset);
431
432 GLBuffer indexBuffer;
433 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
434 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(zeroIndices), zeroIndices, GL_STATIC_DRAW);
Geoff Lang5f319a42017-01-09 16:49:19 -0500435 ASSERT_GL_NO_ERROR();
436
Corentin Wallez0844f2d2017-01-31 17:02:59 -0500437 // Test touching the last index is valid
438 glDrawElements(GL_POINTS, 4, GL_UNSIGNED_BYTE, zeroOffset + 4);
439 ASSERT_GL_NO_ERROR();
Geoff Lang5f319a42017-01-09 16:49:19 -0500440
Corentin Wallez0844f2d2017-01-31 17:02:59 -0500441 // Test touching the last + 1 element is invalid
442 glDrawElements(GL_POINTS, 4, GL_UNSIGNED_BYTE, zeroOffset + 5);
443 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
Geoff Lang5f319a42017-01-09 16:49:19 -0500444
Corentin Wallez0844f2d2017-01-31 17:02:59 -0500445 // Test any offset if valid if count is zero
446 glDrawElements(GL_POINTS, 0, GL_UNSIGNED_BYTE, zeroOffset + 42);
447 ASSERT_GL_NO_ERROR();
Corentin Wallezfe9306a2017-02-01 17:41:05 -0500448
449 // Test touching the first index is valid
450 glDrawElements(GL_POINTS, 4, GL_UNSIGNED_BYTE, zeroOffset + 4);
451 ASSERT_GL_NO_ERROR();
452
453 // Test touching the first - 1 index is invalid
454 // The error ha been specified to be INVALID_VALUE instead of INVALID_OPERATION because it was
455 // the historic behavior of WebGL implementations
456 glDrawElements(GL_POINTS, 4, GL_UNSIGNED_BYTE, zeroOffset - 1);
457 EXPECT_GL_ERROR(GL_INVALID_VALUE);
Geoff Lang5f319a42017-01-09 16:49:19 -0500458}
459
Frank Henigman6137ddc2017-02-10 18:55:07 -0500460// Test depth range with 'near' more or less than 'far.'
461TEST_P(WebGLCompatibilityTest, DepthRange)
462{
463 glDepthRangef(0, 1);
464 ASSERT_GL_NO_ERROR();
465
466 glDepthRangef(.5, .5);
467 ASSERT_GL_NO_ERROR();
468
469 glDepthRangef(1, 0);
470 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
471}
472
Frank Henigman146e8a12017-03-02 23:22:37 -0500473// Test all blend function combinations.
474// In WebGL it is invalid to combine constant color with constant alpha.
475TEST_P(WebGLCompatibilityTest, BlendWithConstantColor)
476{
477 constexpr GLenum srcFunc[] = {
478 GL_ZERO,
479 GL_ONE,
480 GL_SRC_COLOR,
481 GL_ONE_MINUS_SRC_COLOR,
482 GL_DST_COLOR,
483 GL_ONE_MINUS_DST_COLOR,
484 GL_SRC_ALPHA,
485 GL_ONE_MINUS_SRC_ALPHA,
486 GL_DST_ALPHA,
487 GL_ONE_MINUS_DST_ALPHA,
488 GL_CONSTANT_COLOR,
489 GL_ONE_MINUS_CONSTANT_COLOR,
490 GL_CONSTANT_ALPHA,
491 GL_ONE_MINUS_CONSTANT_ALPHA,
492 GL_SRC_ALPHA_SATURATE,
493 };
494
495 constexpr GLenum dstFunc[] = {
496 GL_ZERO, GL_ONE,
497 GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR,
498 GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR,
499 GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
500 GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
501 GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
502 GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA,
503 };
504
505 for (GLenum src : srcFunc)
506 {
507 for (GLenum dst : dstFunc)
508 {
509 glBlendFunc(src, dst);
510 CheckBlendFunctions(src, dst);
511 glBlendFuncSeparate(src, dst, GL_ONE, GL_ONE);
512 CheckBlendFunctions(src, dst);
513 }
514 }
515}
516
Corentin Wallezfd456442016-12-21 17:57:00 -0500517// Test the checks for OOB reads in the vertex buffers, instanced version
518TEST_P(WebGL2CompatibilityTest, DrawArraysBufferOutOfBoundsInstanced)
519{
520 const std::string &vert =
521 "attribute float a_pos;\n"
522 "void main()\n"
523 "{\n"
524 " gl_Position = vec4(a_pos, a_pos, a_pos, 1.0);\n"
525 "}\n";
526
527 const std::string &frag =
528 "precision highp float;\n"
529 "void main()\n"
530 "{\n"
531 " gl_FragColor = vec4(1.0);\n"
532 "}\n";
533
534 ANGLE_GL_PROGRAM(program, vert, frag);
535
536 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
537 ASSERT_NE(-1, posLocation);
538 glUseProgram(program.get());
539
540 GLBuffer buffer;
541 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
542 glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
543
544 glEnableVertexAttribArray(posLocation);
545 glVertexAttribDivisor(posLocation, 1);
546
547 const uint8_t* zeroOffset = nullptr;
548
549 // Test touching the last element is valid.
550 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 12);
551 glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
552 ASSERT_GL_NO_ERROR();
553
554 // Test touching the last element + 1 is invalid.
555 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 13);
556 glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
557 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
558
559 // Test touching the last element is valid, using a stride.
560 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 9);
561 glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
562 ASSERT_GL_NO_ERROR();
563
564 // Test touching the last element + 1 is invalid, using a stride.
565 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 10);
566 glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
567 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
568
569 // Test any offset is valid if no vertices are drawn.
570 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 32);
571 glDrawArraysInstanced(GL_POINTS, 0, 1, 0);
572 ASSERT_GL_NO_ERROR();
573}
574
Corentin Wallez0844f2d2017-01-31 17:02:59 -0500575// Tests that NPOT is not enabled by default in WebGL 1 and that it can be enabled
576TEST_P(WebGLCompatibilityTest, NPOT)
577{
578 EXPECT_FALSE(extensionEnabled("GL_OES_texture_npot"));
579
580 // Create a texture and set an NPOT mip 0, should always be acceptable.
581 GLTexture texture;
582 glBindTexture(GL_TEXTURE_2D, texture.get());
583 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 10, 10, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
584 ASSERT_GL_NO_ERROR();
585
586 // Try setting an NPOT mip 1 and verify the error if WebGL 1
587 glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 5, 5, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
588 if (getClientMajorVersion() < 3)
589 {
590 ASSERT_GL_ERROR(GL_INVALID_VALUE);
591 }
592 else
593 {
594 ASSERT_GL_NO_ERROR();
595 }
596
597 if (extensionRequestable("GL_OES_texture_npot"))
598 {
599 glRequestExtensionANGLE("GL_OES_texture_npot");
600 ASSERT_GL_NO_ERROR();
601
602 // Try again to set NPOT mip 1
603 glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 5, 5, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
604 ASSERT_GL_NO_ERROR();
605 }
606}
607
Jamie Madillcad97ee2017-02-02 18:52:44 -0500608template <typename T>
609void FillTexture2D(GLuint texture,
610 GLsizei width,
611 GLsizei height,
612 const T &onePixelData,
613 GLint level,
614 GLint internalFormat,
615 GLenum format,
616 GLenum type)
617{
618 std::vector<T> allPixelsData(width * height, onePixelData);
619
620 glBindTexture(GL_TEXTURE_2D, texture);
621 glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width, height, 0, format, type,
622 allPixelsData.data());
623 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
624 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
625 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
626 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
627}
628
Frank Henigman875bbba2017-02-08 16:38:17 -0500629// Test that unset gl_Position defaults to (0,0,0,0).
630TEST_P(WebGLCompatibilityTest, DefaultPosition)
631{
632 // Draw a quad where each vertex is red if gl_Position is (0,0,0,0) before it is set,
633 // and green otherwise. The center of each quadrant will be red if and only if all
634 // four corners are red.
635 const std::string vertexShader =
636 "attribute vec3 pos;\n"
637 "varying vec4 color;\n"
638 "void main() {\n"
639 " if (gl_Position == vec4(0,0,0,0)) {\n"
640 " color = vec4(1,0,0,1);\n"
641 " } else {\n"
642 " color = vec4(0,1,0,1);\n"
643 " }\n"
644 " gl_Position = vec4(pos,1);\n"
645 "}\n";
646
647 const std::string fragmentShader =
648 "precision mediump float;\n"
649 "varying vec4 color;\n"
650 "void main() {\n"
651 " gl_FragColor = color;\n"
652 "}\n";
653
654 ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
655 drawQuad(program.get(), "pos", 0.0f, 1.0f, true);
656 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() * 1 / 4, getWindowHeight() * 1 / 4, GLColor::red);
657 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() * 1 / 4, getWindowHeight() * 3 / 4, GLColor::red);
658 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() * 3 / 4, getWindowHeight() * 1 / 4, GLColor::red);
659 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() * 3 / 4, getWindowHeight() * 3 / 4, GLColor::red);
660}
661
Jamie Madilla4595b82017-01-11 17:36:34 -0500662// Tests that a rendering feedback loop triggers a GL error under WebGL.
663// Based on WebGL test conformance/renderbuffers/feedback-loop.html.
664TEST_P(WebGLCompatibilityTest, RenderingFeedbackLoop)
665{
666 const std::string vertexShader =
667 "attribute vec4 a_position;\n"
668 "varying vec2 v_texCoord;\n"
669 "void main() {\n"
670 " gl_Position = a_position;\n"
671 " v_texCoord = (a_position.xy * 0.5) + 0.5;\n"
672 "}\n";
673
674 const std::string fragmentShader =
675 "precision mediump float;\n"
676 "varying vec2 v_texCoord;\n"
677 "uniform sampler2D u_texture;\n"
678 "void main() {\n"
679 " // Shader swizzles color channels so we can tell if the draw succeeded.\n"
680 " gl_FragColor = texture2D(u_texture, v_texCoord).gbra;\n"
681 "}\n";
682
683 GLTexture texture;
Jamie Madillcad97ee2017-02-02 18:52:44 -0500684 FillTexture2D(texture.get(), 1, 1, GLColor::red, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
Jamie Madilla4595b82017-01-11 17:36:34 -0500685
686 ASSERT_GL_NO_ERROR();
687
688 GLFramebuffer framebuffer;
689 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
690 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.get(), 0);
691
692 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
693
694 ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
695
696 GLint uniformLoc = glGetUniformLocation(program.get(), "u_texture");
697 ASSERT_NE(-1, uniformLoc);
698
699 glUseProgram(program.get());
700 glUniform1i(uniformLoc, 0);
701 glDisable(GL_BLEND);
702 glDisable(GL_DEPTH_TEST);
703 ASSERT_GL_NO_ERROR();
704
705 // Drawing with a texture that is also bound to the current framebuffer should fail
706 glBindTexture(GL_TEXTURE_2D, texture.get());
707 drawQuad(program.get(), "a_position", 0.5f, 1.0f, true);
708 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
709
710 // Ensure that the texture contents did not change after the previous render
711 glBindFramebuffer(GL_FRAMEBUFFER, 0);
712 drawQuad(program.get(), "a_position", 0.5f, 1.0f, true);
713 ASSERT_GL_NO_ERROR();
714 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
715
716 // Drawing when texture is bound to an inactive uniform should succeed
717 GLTexture texture2;
Jamie Madillcad97ee2017-02-02 18:52:44 -0500718 FillTexture2D(texture2.get(), 1, 1, GLColor::green, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
Jamie Madilla4595b82017-01-11 17:36:34 -0500719
720 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
721 glActiveTexture(GL_TEXTURE1);
722 glBindTexture(GL_TEXTURE_2D, texture.get());
723 drawQuad(program.get(), "a_position", 0.5f, 1.0f, true);
724 ASSERT_GL_NO_ERROR();
725 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
726}
727
Bryan Bernhart58806562017-01-05 13:09:31 -0800728// Test for the max draw buffers and color attachments.
729TEST_P(WebGLCompatibilityTest, MaxDrawBuffersAttachmentPoints)
730{
731 // This test only applies to ES2.
732 if (getClientMajorVersion() != 2)
733 {
734 return;
735 }
736
737 GLFramebuffer fbo[2];
738 glBindFramebuffer(GL_FRAMEBUFFER, fbo[0].get());
739
740 // Test that is valid when we bind with a single attachment point.
741 GLTexture texture;
742 glBindTexture(GL_TEXTURE_2D, texture.get());
743 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
744 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.get(), 0);
745 ASSERT_GL_NO_ERROR();
746
747 // Test that enabling the draw buffers extension will allow us to bind with a non-zero
748 // attachment point.
749 if (extensionRequestable("GL_EXT_draw_buffers"))
750 {
751 glRequestExtensionANGLE("GL_EXT_draw_buffers");
752 EXPECT_GL_NO_ERROR();
753 EXPECT_TRUE(extensionEnabled("GL_EXT_draw_buffers"));
754
755 glBindFramebuffer(GL_FRAMEBUFFER, fbo[1].get());
756
757 GLTexture texture2;
758 glBindTexture(GL_TEXTURE_2D, texture2.get());
759 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
760 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, texture2.get(),
761 0);
762 ASSERT_GL_NO_ERROR();
763 }
764}
765
Corentin Wallez3f6d4df2017-01-30 18:04:36 -0500766// Test that the offset in the index buffer is forced to be a multiple of the element size
767TEST_P(WebGLCompatibilityTest, DrawElementsOffsetRestriction)
768{
769 const std::string &vert =
770 "attribute vec3 a_pos;\n"
771 "void main()\n"
772 "{\n"
773 " gl_Position = vec4(a_pos, 1.0);\n"
774 "}\n";
775
776 const std::string &frag =
777 "precision highp float;\n"
778 "void main()\n"
779 "{\n"
780 " gl_FragColor = vec4(1.0);\n"
781 "}\n";
782
783 ANGLE_GL_PROGRAM(program, vert, frag);
784
785 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
786 ASSERT_NE(-1, posLocation);
787 glUseProgram(program.get());
788
789 const auto &vertices = GetQuadVertices();
790
791 GLBuffer vertexBuffer;
792 glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.get());
793 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(),
794 GL_STATIC_DRAW);
795
796 glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
797 glEnableVertexAttribArray(posLocation);
798
799 GLBuffer indexBuffer;
800 const GLubyte indices[] = {0, 0, 0, 0, 0, 0, 0};
801 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
802 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
803
804 ASSERT_GL_NO_ERROR();
805
806 const char *zeroIndices = nullptr;
807
808 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, zeroIndices);
809 ASSERT_GL_NO_ERROR();
810
811 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, zeroIndices);
812 ASSERT_GL_NO_ERROR();
813
814 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, zeroIndices + 1);
815 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
816}
817
818// Test that the offset and stride in the vertex buffer is forced to be a multiple of the element
819// size
820TEST_P(WebGLCompatibilityTest, VertexAttribPointerOffsetRestriction)
821{
822 const char *zeroOffset = nullptr;
823
824 // Base case, vector of two floats
825 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset);
826 ASSERT_GL_NO_ERROR();
827
828 // Test setting a non-multiple offset
829 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 1);
830 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
831 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 2);
832 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
833 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, zeroOffset + 3);
834 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
835
836 // Test setting a non-multiple stride
837 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 1, zeroOffset);
838 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
839 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2, zeroOffset);
840 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
841 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 3, zeroOffset);
842 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
843}
844
Jamie Madillcad97ee2017-02-02 18:52:44 -0500845void WebGLCompatibilityTest::drawBuffersEXTFeedbackLoop(GLuint program,
846 const std::array<GLenum, 2> &drawBuffers,
847 GLenum expectedError)
848{
849 glDrawBuffersEXT(2, drawBuffers.data());
850
851 // Make sure framebuffer is complete before feedback loop detection
852 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
853
854 drawQuad(program, "aPosition", 0.5f, 1.0f, true);
855
856 // "Rendering to a texture where it samples from should geneates INVALID_OPERATION. Otherwise,
857 // it should be NO_ERROR"
858 EXPECT_GL_ERROR(expectedError);
859}
860
861// This tests that rendering feedback loops works as expected with GL_EXT_draw_buffers.
862// Based on WebGL test conformance/extensions/webgl-draw-buffers-feedback-loop.html
863TEST_P(WebGLCompatibilityTest, RenderingFeedbackLoopWithDrawBuffersEXT)
864{
865 const std::string vertexShader =
866 "attribute vec4 aPosition;\n"
867 "varying vec2 texCoord;\n"
868 "void main() {\n"
869 " gl_Position = aPosition;\n"
870 " texCoord = (aPosition.xy * 0.5) + 0.5;\n"
871 "}\n";
872
873 const std::string fragmentShader =
874 "#extension GL_EXT_draw_buffers : require\n"
875 "precision mediump float;\n"
876 "uniform sampler2D tex;\n"
877 "varying vec2 texCoord;\n"
878 "void main() {\n"
879 " gl_FragData[0] = texture2D(tex, texCoord);\n"
880 " gl_FragData[1] = texture2D(tex, texCoord);\n"
881 "}\n";
882
883 GLsizei width = 8;
884 GLsizei height = 8;
885
886 // This shader cannot be run in ES3, because WebGL 2 does not expose the draw buffers
887 // extension and gl_FragData semantics are changed to enforce indexing by zero always.
888 // TODO(jmadill): This extension should be disabled in WebGL 2 contexts.
889 if (/*!extensionEnabled("GL_EXT_draw_buffers")*/ getClientMajorVersion() != 2)
890 {
891 // No WEBGL_draw_buffers support -- this is legal.
892 return;
893 }
894
895 GLint maxDrawBuffers = 0;
896 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
897
898 if (maxDrawBuffers < 2)
899 {
900 std::cout << "Test skipped because MAX_DRAW_BUFFERS is too small." << std::endl;
901 return;
902 }
903
904 ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
905 glUseProgram(program.get());
906 glViewport(0, 0, width, height);
907
908 GLTexture tex0;
909 GLTexture tex1;
910 GLFramebuffer fbo;
911 FillTexture2D(tex0.get(), width, height, GLColor::red, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
912 FillTexture2D(tex1.get(), width, height, GLColor::green, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
913 ASSERT_GL_NO_ERROR();
914
915 glBindTexture(GL_TEXTURE_2D, tex1.get());
916 GLint texLoc = glGetUniformLocation(program.get(), "tex");
917 ASSERT_NE(-1, texLoc);
918 glUniform1i(texLoc, 0);
919 ASSERT_GL_NO_ERROR();
920
921 // The sampling texture is bound to COLOR_ATTACHMENT1 during resource allocation
922 glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
923 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex0.get(), 0);
924 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, tex1.get(), 0);
925
926 drawBuffersEXTFeedbackLoop(program.get(), {{GL_NONE, GL_COLOR_ATTACHMENT1}},
927 GL_INVALID_OPERATION);
928 drawBuffersEXTFeedbackLoop(program.get(), {{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}},
929 GL_INVALID_OPERATION);
930 drawBuffersEXTFeedbackLoop(program.get(), {{GL_COLOR_ATTACHMENT0, GL_NONE}}, GL_NO_ERROR);
931}
932
Jamie Madill07be8bf2017-02-02 19:59:57 -0500933// Test tests that texture copying feedback loops are properly rejected in WebGL.
934// Based on the WebGL test conformance/textures/misc/texture-copying-feedback-loops.html
935TEST_P(WebGLCompatibilityTest, TextureCopyingFeedbackLoops)
936{
937 GLTexture texture;
938 glBindTexture(GL_TEXTURE_2D, texture.get());
939 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
940 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
941 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
942 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
943 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
944
945 GLTexture texture2;
946 glBindTexture(GL_TEXTURE_2D, texture2.get());
947 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
948 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
949 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
950 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
951 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
952
953 GLFramebuffer framebuffer;
954 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
955 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.get(), 0);
956
957 // framebuffer should be FRAMEBUFFER_COMPLETE.
958 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
959 ASSERT_GL_NO_ERROR();
960
961 // testing copyTexImage2D
962
963 // copyTexImage2D to same texture but different level
964 glBindTexture(GL_TEXTURE_2D, texture.get());
965 glCopyTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 0, 0, 2, 2, 0);
966 EXPECT_GL_NO_ERROR();
967
968 // copyTexImage2D to same texture same level, invalid feedback loop
969 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 2, 2, 0);
970 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
971
972 // copyTexImage2D to different texture
973 glBindTexture(GL_TEXTURE_2D, texture2.get());
974 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 2, 2, 0);
975 EXPECT_GL_NO_ERROR();
976
977 // testing copyTexSubImage2D
978
979 // copyTexSubImage2D to same texture but different level
980 glBindTexture(GL_TEXTURE_2D, texture.get());
981 glCopyTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, 0, 0, 1, 1);
982 EXPECT_GL_NO_ERROR();
983
984 // copyTexSubImage2D to same texture same level, invalid feedback loop
985 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
986 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
987
988 // copyTexSubImage2D to different texture
989 glBindTexture(GL_TEXTURE_2D, texture2.get());
990 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 1, 1);
991 EXPECT_GL_NO_ERROR();
992}
993
994void WebGLCompatibilityTest::drawBuffersFeedbackLoop(GLuint program,
995 const std::array<GLenum, 2> &drawBuffers,
996 GLenum expectedError)
997{
998 glDrawBuffers(2, drawBuffers.data());
999
1000 // Make sure framebuffer is complete before feedback loop detection
1001 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
1002
1003 drawQuad(program, "aPosition", 0.5f, 1.0f, true);
1004
1005 // "Rendering to a texture where it samples from should geneates INVALID_OPERATION. Otherwise,
1006 // it should be NO_ERROR"
1007 EXPECT_GL_ERROR(expectedError);
1008}
1009
Yuly Novikov817232e2017-02-22 18:36:10 -05001010// Tests invariance matching rules between built in varyings.
1011// Based on WebGL test conformance/glsl/misc/shaders-with-invariance.html.
1012TEST_P(WebGLCompatibilityTest, BuiltInInvariant)
1013{
1014 const std::string vertexShaderVariant =
1015 "varying vec4 v_varying;\n"
1016 "void main()\n"
1017 "{\n"
1018 " gl_PointSize = 1.0;\n"
1019 " gl_Position = v_varying;\n"
1020 "}";
1021 const std::string fragmentShaderInvariantGlFragCoord =
1022 "invariant gl_FragCoord;\n"
1023 "void main()\n"
1024 "{\n"
1025 " gl_FragColor = gl_FragCoord;\n"
1026 "}";
1027 const std::string fragmentShaderInvariantGlPointCoord =
1028 "invariant gl_PointCoord;\n"
1029 "void main()\n"
1030 "{\n"
1031 " gl_FragColor = vec4(gl_PointCoord, 0.0, 0.0);\n"
1032 "}";
1033
1034 GLuint program = CompileProgram(vertexShaderVariant, fragmentShaderInvariantGlFragCoord);
1035 EXPECT_EQ(0u, program);
1036
1037 program = CompileProgram(vertexShaderVariant, fragmentShaderInvariantGlPointCoord);
1038 EXPECT_EQ(0u, program);
1039}
1040
Jamie Madill07be8bf2017-02-02 19:59:57 -05001041// This tests that rendering feedback loops works as expected with WebGL 2.
1042// Based on WebGL test conformance2/rendering/rendering-sampling-feedback-loop.html
1043TEST_P(WebGL2CompatibilityTest, RenderingFeedbackLoopWithDrawBuffers)
1044{
1045 const std::string vertexShader =
1046 "#version 300 es\n"
1047 "in vec4 aPosition;\n"
1048 "out vec2 texCoord;\n"
1049 "void main() {\n"
1050 " gl_Position = aPosition;\n"
1051 " texCoord = (aPosition.xy * 0.5) + 0.5;\n"
1052 "}\n";
1053
1054 const std::string fragmentShader =
1055 "#version 300 es\n"
1056 "precision mediump float;\n"
1057 "uniform sampler2D tex;\n"
1058 "in vec2 texCoord;\n"
1059 "out vec4 oColor;\n"
1060 "void main() {\n"
1061 " oColor = texture(tex, texCoord);\n"
1062 "}\n";
1063
1064 GLsizei width = 8;
1065 GLsizei height = 8;
1066
1067 GLint maxDrawBuffers = 0;
1068 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
1069 // ES3 requires a minimum value of 4 for MAX_DRAW_BUFFERS.
1070 ASSERT_GE(maxDrawBuffers, 2);
1071
1072 ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
1073 glUseProgram(program.get());
1074 glViewport(0, 0, width, height);
1075
1076 GLTexture tex0;
1077 GLTexture tex1;
1078 GLFramebuffer fbo;
1079 FillTexture2D(tex0.get(), width, height, GLColor::red, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
1080 FillTexture2D(tex1.get(), width, height, GLColor::green, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
1081 ASSERT_GL_NO_ERROR();
1082
1083 glBindTexture(GL_TEXTURE_2D, tex1.get());
1084 GLint texLoc = glGetUniformLocation(program.get(), "tex");
1085 ASSERT_NE(-1, texLoc);
1086 glUniform1i(texLoc, 0);
1087
1088 // The sampling texture is bound to COLOR_ATTACHMENT1 during resource allocation
1089 glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
1090 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex0.get(), 0);
1091 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, tex1.get(), 0);
1092 ASSERT_GL_NO_ERROR();
1093
1094 drawBuffersFeedbackLoop(program.get(), {{GL_NONE, GL_COLOR_ATTACHMENT1}}, GL_INVALID_OPERATION);
1095 drawBuffersFeedbackLoop(program.get(), {{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}},
1096 GL_INVALID_OPERATION);
1097 drawBuffersFeedbackLoop(program.get(), {{GL_COLOR_ATTACHMENT0, GL_NONE}}, GL_NO_ERROR);
1098}
1099
Jamie Madill1d37bc52017-02-02 19:59:58 -05001100// This test covers detection of rendering feedback loops between the FBO and a depth Texture.
1101// Based on WebGL test conformance2/rendering/depth-stencil-feedback-loop.html
1102TEST_P(WebGL2CompatibilityTest, RenderingFeedbackLoopWithDepthStencil)
1103{
1104 const std::string vertexShader =
1105 "#version 300 es\n"
1106 "in vec4 aPosition;\n"
1107 "out vec2 texCoord;\n"
1108 "void main() {\n"
1109 " gl_Position = aPosition;\n"
1110 " texCoord = (aPosition.xy * 0.5) + 0.5;\n"
1111 "}\n";
1112
1113 const std::string fragmentShader =
1114 "#version 300 es\n"
1115 "precision mediump float;\n"
1116 "uniform sampler2D tex;\n"
1117 "in vec2 texCoord;\n"
1118 "out vec4 oColor;\n"
1119 "void main() {\n"
1120 " oColor = texture(tex, texCoord);\n"
1121 "}\n";
1122
1123 GLsizei width = 8;
1124 GLsizei height = 8;
1125
1126 ANGLE_GL_PROGRAM(program, vertexShader, fragmentShader);
1127 glUseProgram(program.get());
1128
1129 glViewport(0, 0, width, height);
1130
1131 GLint texLoc = glGetUniformLocation(program.get(), "tex");
1132 glUniform1i(texLoc, 0);
1133
1134 // Create textures and allocate storage
1135 GLTexture tex0;
1136 GLTexture tex1;
1137 GLRenderbuffer rb;
1138 FillTexture2D(tex0.get(), width, height, GLColor::black, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
1139 FillTexture2D(tex1.get(), width, height, 0x80, 0, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT,
1140 GL_UNSIGNED_INT);
1141 glBindRenderbuffer(GL_RENDERBUFFER, rb.get());
1142 glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height);
1143 ASSERT_GL_NO_ERROR();
1144
1145 GLFramebuffer fbo;
1146 glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
1147 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex0.get(), 0);
1148
1149 // Test rendering and sampling feedback loop for depth buffer
1150 glBindTexture(GL_TEXTURE_2D, tex1.get());
1151 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex1.get(), 0);
1152 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
1153
1154 // The same image is used as depth buffer during rendering.
1155 glEnable(GL_DEPTH_TEST);
1156 drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
1157 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1158
1159 // The same image is used as depth buffer. But depth mask is false.
1160 glDepthMask(GL_FALSE);
1161 drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
1162 EXPECT_GL_NO_ERROR();
1163
1164 // The same image is used as depth buffer. But depth test is not enabled during rendering.
1165 glDepthMask(GL_TRUE);
1166 glDisable(GL_DEPTH_TEST);
1167 drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
1168 EXPECT_GL_NO_ERROR();
1169
1170 // Test rendering and sampling feedback loop for stencil buffer
1171 glBindTexture(GL_RENDERBUFFER, rb.get());
1172 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
1173 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb.get());
1174 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
1175 constexpr GLint stencilClearValue = 0x40;
1176 glClearBufferiv(GL_STENCIL, 0, &stencilClearValue);
1177
1178 // The same image is used as stencil buffer during rendering.
1179 glEnable(GL_STENCIL_TEST);
1180 drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
1181 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1182
1183 // The same image is used as stencil buffer. But stencil mask is zero.
1184 glStencilMask(0x0);
1185 drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
1186 EXPECT_GL_NO_ERROR();
1187
1188 // The same image is used as stencil buffer. But stencil test is not enabled during rendering.
1189 glStencilMask(0xffff);
1190 glDisable(GL_STENCIL_TEST);
1191 drawQuad(program.get(), "aPosition", 0.5f, 1.0f, true);
1192 EXPECT_GL_NO_ERROR();
1193}
1194
Jamie Madillfd3dd432017-02-02 19:59:59 -05001195// The source and the target for CopyTexSubImage3D are the same 3D texture.
1196// But the level of the 3D texture != the level of the read attachment.
1197TEST_P(WebGL2CompatibilityTest, NoTextureCopyingFeedbackLoopBetween3DLevels)
1198{
1199 GLTexture texture;
1200 GLFramebuffer framebuffer;
1201
1202 glBindTexture(GL_TEXTURE_3D, texture.get());
1203 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
1204
1205 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1206 glTexImage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1207 glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture.get(), 0, 0);
1208 ASSERT_GL_NO_ERROR();
1209
1210 glCopyTexSubImage3D(GL_TEXTURE_3D, 1, 0, 0, 0, 0, 0, 2, 2);
1211 EXPECT_GL_NO_ERROR();
1212}
1213
1214// The source and the target for CopyTexSubImage3D are the same 3D texture.
1215// But the zoffset of the 3D texture != the layer of the read attachment.
1216TEST_P(WebGL2CompatibilityTest, NoTextureCopyingFeedbackLoopBetween3DLayers)
1217{
1218 GLTexture texture;
1219 GLFramebuffer framebuffer;
1220
1221 glBindTexture(GL_TEXTURE_3D, texture.get());
1222 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
1223
1224 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1225 glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture.get(), 0, 1);
1226 ASSERT_GL_NO_ERROR();
1227
1228 glCopyTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, 0, 0, 2, 2);
1229 EXPECT_GL_NO_ERROR();
1230}
1231
1232// The source and the target for CopyTexSubImage3D are the same 3D texture.
1233// And the level / zoffset of the 3D texture is equal to the level / layer of the read attachment.
1234TEST_P(WebGL2CompatibilityTest, TextureCopyingFeedbackLoop3D)
1235{
1236 GLTexture texture;
1237 GLFramebuffer framebuffer;
1238
1239 glBindTexture(GL_TEXTURE_3D, texture.get());
1240 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
1241
1242 glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 4, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1243 glTexImage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 2, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1244 glTexImage3D(GL_TEXTURE_3D, 2, GL_RGBA8, 1, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1245 glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture.get(), 1, 0);
1246 ASSERT_GL_NO_ERROR();
1247
1248 glCopyTexSubImage3D(GL_TEXTURE_3D, 1, 0, 0, 0, 0, 0, 2, 2);
1249 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1250}
1251
Geoff Langc287ea62016-09-16 14:46:51 -04001252// Use this to select which configurations (e.g. which renderer, which GLES major version) these
1253// tests should be run against.
1254ANGLE_INSTANTIATE_TEST(WebGLCompatibilityTest,
1255 ES2_D3D9(),
1256 ES2_D3D11(),
1257 ES3_D3D11(),
1258 ES2_D3D11_FL9_3(),
1259 ES2_OPENGL(),
1260 ES3_OPENGL(),
1261 ES2_OPENGLES(),
1262 ES3_OPENGLES());
1263
Jamie Madill07be8bf2017-02-02 19:59:57 -05001264ANGLE_INSTANTIATE_TEST(WebGL2CompatibilityTest, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGLES());
Geoff Langc287ea62016-09-16 14:46:51 -04001265} // namespace